1 /*-
2 * Copyright (c) 1982, 1986, 1989, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
35 */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: releng/6.0/sys/ufs/ufs/ufs_vnops.c 147198 2005-06-09 20:20:31Z ssouhlal $");
39
40 #include "opt_mac.h"
41 #include "opt_quota.h"
42 #include "opt_suiddir.h"
43 #include "opt_ufs.h"
44 #include "opt_ffs.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #include <sys/namei.h>
50 #include <sys/kernel.h>
51 #include <sys/fcntl.h>
52 #include <sys/stat.h>
53 #include <sys/bio.h>
54 #include <sys/buf.h>
55 #include <sys/mount.h>
56 #include <sys/unistd.h>
57 #include <sys/vnode.h>
58 #include <sys/dirent.h>
59 #include <sys/lockf.h>
60 #include <sys/conf.h>
61 #include <sys/acl.h>
62 #include <sys/mac.h>
63 #include <sys/jail.h>
64
65 #include <machine/mutex.h>
66
67 #include <sys/file.h> /* XXX */
68
69 #include <vm/vm.h>
70 #include <vm/vm_extern.h>
71
72 #include <fs/fifofs/fifo.h>
73
74 #include <ufs/ufs/acl.h>
75 #include <ufs/ufs/extattr.h>
76 #include <ufs/ufs/quota.h>
77 #include <ufs/ufs/inode.h>
78 #include <ufs/ufs/dir.h>
79 #include <ufs/ufs/ufsmount.h>
80 #include <ufs/ufs/ufs_extern.h>
81 #ifdef UFS_DIRHASH
82 #include <ufs/ufs/dirhash.h>
83 #endif
84
85 #include <ufs/ffs/ffs_extern.h>
86
87 static vop_access_t ufs_access;
88 static vop_advlock_t ufs_advlock;
89 static int ufs_chmod(struct vnode *, int, struct ucred *, struct thread *);
90 static int ufs_chown(struct vnode *, uid_t, gid_t, struct ucred *, struct thread *);
91 static vop_close_t ufs_close;
92 static vop_create_t ufs_create;
93 static vop_getattr_t ufs_getattr;
94 static vop_link_t ufs_link;
95 static int ufs_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
96 static vop_mkdir_t ufs_mkdir;
97 static vop_mknod_t ufs_mknod;
98 static vop_open_t ufs_open;
99 static vop_pathconf_t ufs_pathconf;
100 static vop_print_t ufs_print;
101 static vop_readlink_t ufs_readlink;
102 static vop_remove_t ufs_remove;
103 static vop_rename_t ufs_rename;
104 static vop_rmdir_t ufs_rmdir;
105 static vop_setattr_t ufs_setattr;
106 static vop_strategy_t ufs_strategy;
107 static vop_symlink_t ufs_symlink;
108 static vop_whiteout_t ufs_whiteout;
109 static vop_close_t ufsfifo_close;
110 static vop_kqfilter_t ufsfifo_kqfilter;
111
112 /*
113 * A virgin directory (no blushing please).
114 */
115 static struct dirtemplate mastertemplate = {
116 0, 12, DT_DIR, 1, ".",
117 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
118 };
119 static struct odirtemplate omastertemplate = {
120 0, 12, 1, ".",
121 0, DIRBLKSIZ - 12, 2, ".."
122 };
123
124 void
125 ufs_itimes(vp)
126 struct vnode *vp;
127 {
128 struct inode *ip;
129 struct timespec ts;
130
131 ip = VTOI(vp);
132 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
133 return;
134 if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
135 ip->i_flag |= IN_LAZYMOD;
136 else
137 ip->i_flag |= IN_MODIFIED;
138 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
139 vfs_timestamp(&ts);
140 if (ip->i_flag & IN_ACCESS) {
141 DIP_SET(ip, i_atime, ts.tv_sec);
142 DIP_SET(ip, i_atimensec, ts.tv_nsec);
143 }
144 if (ip->i_flag & IN_UPDATE) {
145 DIP_SET(ip, i_mtime, ts.tv_sec);
146 DIP_SET(ip, i_mtimensec, ts.tv_nsec);
147 ip->i_modrev++;
148 }
149 if (ip->i_flag & IN_CHANGE) {
150 DIP_SET(ip, i_ctime, ts.tv_sec);
151 DIP_SET(ip, i_ctimensec, ts.tv_nsec);
152 }
153 }
154 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
155 }
156
157 /*
158 * Create a regular file
159 */
160 static int
161 ufs_create(ap)
162 struct vop_create_args /* {
163 struct vnode *a_dvp;
164 struct vnode **a_vpp;
165 struct componentname *a_cnp;
166 struct vattr *a_vap;
167 } */ *ap;
168 {
169 int error;
170
171 error =
172 ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
173 ap->a_dvp, ap->a_vpp, ap->a_cnp);
174 if (error)
175 return (error);
176 return (0);
177 }
178
179 /*
180 * Mknod vnode call
181 */
182 /* ARGSUSED */
183 static int
184 ufs_mknod(ap)
185 struct vop_mknod_args /* {
186 struct vnode *a_dvp;
187 struct vnode **a_vpp;
188 struct componentname *a_cnp;
189 struct vattr *a_vap;
190 } */ *ap;
191 {
192 struct vattr *vap = ap->a_vap;
193 struct vnode **vpp = ap->a_vpp;
194 struct inode *ip;
195 ino_t ino;
196 int error;
197
198 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
199 ap->a_dvp, vpp, ap->a_cnp);
200 if (error)
201 return (error);
202 ip = VTOI(*vpp);
203 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
204 if (vap->va_rdev != VNOVAL) {
205 /*
206 * Want to be able to use this to make badblock
207 * inodes, so don't truncate the dev number.
208 */
209 DIP_SET(ip, i_rdev, vap->va_rdev);
210 }
211 /*
212 * Remove inode, then reload it through VFS_VGET so it is
213 * checked to see if it is an alias of an existing entry in
214 * the inode cache. XXX I don't believe this is necessary now.
215 */
216 (*vpp)->v_type = VNON;
217 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
218 vgone(*vpp);
219 vput(*vpp);
220 error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
221 if (error) {
222 *vpp = NULL;
223 return (error);
224 }
225 return (0);
226 }
227
228 /*
229 * Open called.
230 */
231 /* ARGSUSED */
232 static int
233 ufs_open(struct vop_open_args *ap)
234 {
235 struct vnode *vp = ap->a_vp;
236 struct inode *ip;
237
238 if (vp->v_type == VCHR || vp->v_type == VBLK)
239 return (EOPNOTSUPP);
240
241 ip = VTOI(vp);
242 /*
243 * Files marked append-only must be opened for appending.
244 */
245 if ((ip->i_flags & APPEND) &&
246 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
247 return (EPERM);
248 vnode_create_vobject(vp, DIP(ip, i_size), ap->a_td);
249 return (0);
250 }
251
252 /*
253 * Close called.
254 *
255 * Update the times on the inode.
256 */
257 /* ARGSUSED */
258 static int
259 ufs_close(ap)
260 struct vop_close_args /* {
261 struct vnode *a_vp;
262 int a_fflag;
263 struct ucred *a_cred;
264 struct thread *a_td;
265 } */ *ap;
266 {
267 struct vnode *vp = ap->a_vp;
268
269 VI_LOCK(vp);
270 if (vp->v_usecount > 1)
271 ufs_itimes(vp);
272 VI_UNLOCK(vp);
273 return (0);
274 }
275
276 static int
277 ufs_access(ap)
278 struct vop_access_args /* {
279 struct vnode *a_vp;
280 int a_mode;
281 struct ucred *a_cred;
282 struct thread *a_td;
283 } */ *ap;
284 {
285 struct vnode *vp = ap->a_vp;
286 struct inode *ip = VTOI(vp);
287 mode_t mode = ap->a_mode;
288 int error;
289 #ifdef UFS_ACL
290 struct acl *acl;
291 #endif
292
293 /*
294 * Disallow write attempts on read-only filesystems;
295 * unless the file is a socket, fifo, or a block or
296 * character device resident on the filesystem.
297 */
298 if (mode & VWRITE) {
299 switch (vp->v_type) {
300 case VDIR:
301 case VLNK:
302 case VREG:
303 if (vp->v_mount->mnt_flag & MNT_RDONLY)
304 return (EROFS);
305 #ifdef QUOTA
306 if ((error = getinoquota(ip)) != 0)
307 return (error);
308 #endif
309 break;
310 default:
311 break;
312 }
313 }
314
315 /* If immutable bit set, nobody gets to write it. */
316 if ((mode & VWRITE) && (ip->i_flags & (IMMUTABLE | SF_SNAPSHOT)))
317 return (EPERM);
318
319 #ifdef UFS_ACL
320 if ((vp->v_mount->mnt_flag & MNT_ACLS) != 0) {
321 MALLOC(acl, struct acl *, sizeof(*acl), M_ACL, M_WAITOK);
322 error = VOP_GETACL(vp, ACL_TYPE_ACCESS, acl, ap->a_cred,
323 ap->a_td);
324 switch (error) {
325 case EOPNOTSUPP:
326 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid,
327 ip->i_gid, ap->a_mode, ap->a_cred, NULL);
328 break;
329 case 0:
330 error = vaccess_acl_posix1e(vp->v_type, ip->i_uid,
331 ip->i_gid, acl, ap->a_mode, ap->a_cred, NULL);
332 break;
333 default:
334 printf(
335 "ufs_access(): Error retrieving ACL on object (%d).\n",
336 error);
337 /*
338 * XXX: Fall back until debugged. Should
339 * eventually possibly log an error, and return
340 * EPERM for safety.
341 */
342 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid,
343 ip->i_gid, ap->a_mode, ap->a_cred, NULL);
344 }
345 FREE(acl, M_ACL);
346 } else
347 #endif /* !UFS_ACL */
348 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
349 ap->a_mode, ap->a_cred, NULL);
350 return (error);
351 }
352
353 /* ARGSUSED */
354 static int
355 ufs_getattr(ap)
356 struct vop_getattr_args /* {
357 struct vnode *a_vp;
358 struct vattr *a_vap;
359 struct ucred *a_cred;
360 struct thread *a_td;
361 } */ *ap;
362 {
363 struct vnode *vp = ap->a_vp;
364 struct inode *ip = VTOI(vp);
365 struct vattr *vap = ap->a_vap;
366
367 ufs_itimes(vp);
368 /*
369 * Copy from inode table
370 */
371 vap->va_fsid = dev2udev(ip->i_dev);
372 vap->va_fileid = ip->i_number;
373 vap->va_mode = ip->i_mode & ~IFMT;
374 vap->va_nlink = ip->i_effnlink;
375 vap->va_uid = ip->i_uid;
376 vap->va_gid = ip->i_gid;
377 if (ip->i_ump->um_fstype == UFS1) {
378 vap->va_rdev = ip->i_din1->di_rdev;
379 vap->va_size = ip->i_din1->di_size;
380 vap->va_atime.tv_sec = ip->i_din1->di_atime;
381 vap->va_atime.tv_nsec = ip->i_din1->di_atimensec;
382 vap->va_mtime.tv_sec = ip->i_din1->di_mtime;
383 vap->va_mtime.tv_nsec = ip->i_din1->di_mtimensec;
384 vap->va_ctime.tv_sec = ip->i_din1->di_ctime;
385 vap->va_ctime.tv_nsec = ip->i_din1->di_ctimensec;
386 vap->va_birthtime.tv_sec = 0;
387 vap->va_birthtime.tv_nsec = 0;
388 vap->va_bytes = dbtob((u_quad_t)ip->i_din1->di_blocks);
389 } else {
390 vap->va_rdev = ip->i_din2->di_rdev;
391 vap->va_size = ip->i_din2->di_size;
392 vap->va_atime.tv_sec = ip->i_din2->di_atime;
393 vap->va_atime.tv_nsec = ip->i_din2->di_atimensec;
394 vap->va_mtime.tv_sec = ip->i_din2->di_mtime;
395 vap->va_mtime.tv_nsec = ip->i_din2->di_mtimensec;
396 vap->va_ctime.tv_sec = ip->i_din2->di_ctime;
397 vap->va_ctime.tv_nsec = ip->i_din2->di_ctimensec;
398 vap->va_birthtime.tv_sec = ip->i_din2->di_birthtime;
399 vap->va_birthtime.tv_nsec = ip->i_din2->di_birthnsec;
400 vap->va_bytes = dbtob((u_quad_t)ip->i_din2->di_blocks);
401 }
402 vap->va_flags = ip->i_flags;
403 vap->va_gen = ip->i_gen;
404 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
405 vap->va_type = IFTOVT(ip->i_mode);
406 vap->va_filerev = ip->i_modrev;
407 return (0);
408 }
409
410 /*
411 * Set attribute vnode op. called from several syscalls
412 */
413 static int
414 ufs_setattr(ap)
415 struct vop_setattr_args /* {
416 struct vnode *a_vp;
417 struct vattr *a_vap;
418 struct ucred *a_cred;
419 struct thread *a_td;
420 } */ *ap;
421 {
422 struct vattr *vap = ap->a_vap;
423 struct vnode *vp = ap->a_vp;
424 struct inode *ip = VTOI(vp);
425 struct ucred *cred = ap->a_cred;
426 struct thread *td = ap->a_td;
427 int error;
428
429 /*
430 * Check for unsettable attributes.
431 */
432 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
433 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
434 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
435 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
436 return (EINVAL);
437 }
438 /*
439 * Update the file's access time when it has been executed. We are
440 * doing this here to specifically avoid some of the checks done
441 * below -- this operation is done by request of the kernel and
442 * should bypass some security checks. Things like read-only
443 * checks get handled by other levels (e.g., ffs_update()).
444 */
445 if (vap->va_vaflags & VA_EXECVE_ATIME) {
446 ip->i_flag |= IN_ACCESS;
447 return (0);
448 }
449 if (vap->va_flags != VNOVAL) {
450 if (vp->v_mount->mnt_flag & MNT_RDONLY)
451 return (EROFS);
452 /*
453 * Callers may only modify the file flags on objects they
454 * have VADMIN rights for.
455 */
456 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
457 return (error);
458 /*
459 * Unprivileged processes are not permitted to unset system
460 * flags, or modify flags if any system flags are set.
461 * Privileged non-jail processes may not modify system flags
462 * if securelevel > 0 and any existing system flags are set.
463 * Privileged jail processes behave like privileged non-jail
464 * processes if the security.jail.chflags_allowed sysctl is
465 * is non-zero; otherwise, they behave like unprivileged
466 * processes.
467 */
468 if (!suser_cred(cred,
469 jail_chflags_allowed ? SUSER_ALLOWJAIL : 0)) {
470 if (ip->i_flags
471 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
472 error = securelevel_gt(cred, 0);
473 if (error)
474 return (error);
475 }
476 /* Snapshot flag cannot be set or cleared */
477 if (((vap->va_flags & SF_SNAPSHOT) != 0 &&
478 (ip->i_flags & SF_SNAPSHOT) == 0) ||
479 ((vap->va_flags & SF_SNAPSHOT) == 0 &&
480 (ip->i_flags & SF_SNAPSHOT) != 0))
481 return (EPERM);
482 ip->i_flags = vap->va_flags;
483 DIP_SET(ip, i_flags, vap->va_flags);
484 } else {
485 if (ip->i_flags
486 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
487 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
488 return (EPERM);
489 ip->i_flags &= SF_SETTABLE;
490 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
491 DIP_SET(ip, i_flags, ip->i_flags);
492 }
493 ip->i_flag |= IN_CHANGE;
494 if (vap->va_flags & (IMMUTABLE | APPEND))
495 return (0);
496 }
497 if (ip->i_flags & (IMMUTABLE | APPEND))
498 return (EPERM);
499 /*
500 * Go through the fields and update iff not VNOVAL.
501 */
502 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
503 if (vp->v_mount->mnt_flag & MNT_RDONLY)
504 return (EROFS);
505 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred,
506 td)) != 0)
507 return (error);
508 }
509 if (vap->va_size != VNOVAL) {
510 /*
511 * Disallow write attempts on read-only filesystems;
512 * unless the file is a socket, fifo, or a block or
513 * character device resident on the filesystem.
514 */
515 switch (vp->v_type) {
516 case VDIR:
517 return (EISDIR);
518 case VLNK:
519 case VREG:
520 if (vp->v_mount->mnt_flag & MNT_RDONLY)
521 return (EROFS);
522 if ((ip->i_flags & SF_SNAPSHOT) != 0)
523 return (EPERM);
524 break;
525 default:
526 break;
527 }
528 if ((error = UFS_TRUNCATE(vp, vap->va_size, IO_NORMAL,
529 cred, td)) != 0)
530 return (error);
531 }
532 if (vap->va_atime.tv_sec != VNOVAL ||
533 vap->va_mtime.tv_sec != VNOVAL ||
534 vap->va_birthtime.tv_sec != VNOVAL) {
535 if (vp->v_mount->mnt_flag & MNT_RDONLY)
536 return (EROFS);
537 if ((ip->i_flags & SF_SNAPSHOT) != 0)
538 return (EPERM);
539 /*
540 * From utimes(2):
541 * If times is NULL, ... The caller must be the owner of
542 * the file, have permission to write the file, or be the
543 * super-user.
544 * If times is non-NULL, ... The caller must be the owner of
545 * the file or be the super-user.
546 */
547 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)) &&
548 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
549 (error = VOP_ACCESS(vp, VWRITE, cred, td))))
550 return (error);
551 if (vap->va_atime.tv_sec != VNOVAL)
552 ip->i_flag |= IN_ACCESS;
553 if (vap->va_mtime.tv_sec != VNOVAL)
554 ip->i_flag |= IN_CHANGE | IN_UPDATE;
555 if (vap->va_birthtime.tv_sec != VNOVAL &&
556 ip->i_ump->um_fstype == UFS2)
557 ip->i_flag |= IN_MODIFIED;
558 ufs_itimes(vp);
559 if (vap->va_atime.tv_sec != VNOVAL) {
560 DIP_SET(ip, i_atime, vap->va_atime.tv_sec);
561 DIP_SET(ip, i_atimensec, vap->va_atime.tv_nsec);
562 }
563 if (vap->va_mtime.tv_sec != VNOVAL) {
564 DIP_SET(ip, i_mtime, vap->va_mtime.tv_sec);
565 DIP_SET(ip, i_mtimensec, vap->va_mtime.tv_nsec);
566 }
567 if (vap->va_birthtime.tv_sec != VNOVAL &&
568 ip->i_ump->um_fstype == UFS2) {
569 ip->i_din2->di_birthtime = vap->va_birthtime.tv_sec;
570 ip->i_din2->di_birthnsec = vap->va_birthtime.tv_nsec;
571 }
572 error = UFS_UPDATE(vp, 0);
573 if (error)
574 return (error);
575 }
576 error = 0;
577 if (vap->va_mode != (mode_t)VNOVAL) {
578 if (vp->v_mount->mnt_flag & MNT_RDONLY)
579 return (EROFS);
580 if ((ip->i_flags & SF_SNAPSHOT) != 0 && (vap->va_mode &
581 (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP | S_IXOTH | S_IWOTH)))
582 return (EPERM);
583 error = ufs_chmod(vp, (int)vap->va_mode, cred, td);
584 }
585 return (error);
586 }
587
588 /*
589 * Change the mode on a file.
590 * Inode must be locked before calling.
591 */
592 static int
593 ufs_chmod(vp, mode, cred, td)
594 struct vnode *vp;
595 int mode;
596 struct ucred *cred;
597 struct thread *td;
598 {
599 struct inode *ip = VTOI(vp);
600 int error;
601
602 /*
603 * To modify the permissions on a file, must possess VADMIN
604 * for that file.
605 */
606 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
607 return (error);
608 /*
609 * Privileged processes may set the sticky bit on non-directories,
610 * as well as set the setgid bit on a file with a group that the
611 * process is not a member of. Both of these are allowed in
612 * jail(8).
613 */
614 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
615 if (suser_cred(cred, SUSER_ALLOWJAIL))
616 return (EFTYPE);
617 }
618 if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
619 error = suser_cred(cred, SUSER_ALLOWJAIL);
620 if (error)
621 return (error);
622 }
623 ip->i_mode &= ~ALLPERMS;
624 ip->i_mode |= (mode & ALLPERMS);
625 DIP_SET(ip, i_mode, ip->i_mode);
626 ip->i_flag |= IN_CHANGE;
627 return (0);
628 }
629
630 /*
631 * Perform chown operation on inode ip;
632 * inode must be locked prior to call.
633 */
634 static int
635 ufs_chown(vp, uid, gid, cred, td)
636 struct vnode *vp;
637 uid_t uid;
638 gid_t gid;
639 struct ucred *cred;
640 struct thread *td;
641 {
642 struct inode *ip = VTOI(vp);
643 uid_t ouid;
644 gid_t ogid;
645 int error = 0;
646 #ifdef QUOTA
647 int i;
648 ufs2_daddr_t change;
649 #endif
650
651 if (uid == (uid_t)VNOVAL)
652 uid = ip->i_uid;
653 if (gid == (gid_t)VNOVAL)
654 gid = ip->i_gid;
655 /*
656 * To modify the ownership of a file, must possess VADMIN
657 * for that file.
658 */
659 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
660 return (error);
661 /*
662 * To change the owner of a file, or change the group of a file
663 * to a group of which we are not a member, the caller must
664 * have privilege.
665 */
666 if ((uid != ip->i_uid ||
667 (gid != ip->i_gid && !groupmember(gid, cred))) &&
668 (error = suser_cred(cred, SUSER_ALLOWJAIL)))
669 return (error);
670 ogid = ip->i_gid;
671 ouid = ip->i_uid;
672 #ifdef QUOTA
673 if ((error = getinoquota(ip)) != 0)
674 return (error);
675 if (ouid == uid) {
676 dqrele(vp, ip->i_dquot[USRQUOTA]);
677 ip->i_dquot[USRQUOTA] = NODQUOT;
678 }
679 if (ogid == gid) {
680 dqrele(vp, ip->i_dquot[GRPQUOTA]);
681 ip->i_dquot[GRPQUOTA] = NODQUOT;
682 }
683 change = DIP(ip, i_blocks);
684 (void) chkdq(ip, -change, cred, CHOWN);
685 (void) chkiq(ip, -1, cred, CHOWN);
686 for (i = 0; i < MAXQUOTAS; i++) {
687 dqrele(vp, ip->i_dquot[i]);
688 ip->i_dquot[i] = NODQUOT;
689 }
690 #endif
691 ip->i_gid = gid;
692 DIP_SET(ip, i_gid, gid);
693 ip->i_uid = uid;
694 DIP_SET(ip, i_uid, uid);
695 #ifdef QUOTA
696 if ((error = getinoquota(ip)) == 0) {
697 if (ouid == uid) {
698 dqrele(vp, ip->i_dquot[USRQUOTA]);
699 ip->i_dquot[USRQUOTA] = NODQUOT;
700 }
701 if (ogid == gid) {
702 dqrele(vp, ip->i_dquot[GRPQUOTA]);
703 ip->i_dquot[GRPQUOTA] = NODQUOT;
704 }
705 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
706 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
707 goto good;
708 else
709 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
710 }
711 for (i = 0; i < MAXQUOTAS; i++) {
712 dqrele(vp, ip->i_dquot[i]);
713 ip->i_dquot[i] = NODQUOT;
714 }
715 }
716 ip->i_gid = ogid;
717 DIP_SET(ip, i_gid, ogid);
718 ip->i_uid = ouid;
719 DIP_SET(ip, i_uid, ouid);
720 if (getinoquota(ip) == 0) {
721 if (ouid == uid) {
722 dqrele(vp, ip->i_dquot[USRQUOTA]);
723 ip->i_dquot[USRQUOTA] = NODQUOT;
724 }
725 if (ogid == gid) {
726 dqrele(vp, ip->i_dquot[GRPQUOTA]);
727 ip->i_dquot[GRPQUOTA] = NODQUOT;
728 }
729 (void) chkdq(ip, change, cred, FORCE|CHOWN);
730 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
731 (void) getinoquota(ip);
732 }
733 return (error);
734 good:
735 if (getinoquota(ip))
736 panic("ufs_chown: lost quota");
737 #endif /* QUOTA */
738 ip->i_flag |= IN_CHANGE;
739 if (suser_cred(cred, SUSER_ALLOWJAIL) && (ouid != uid || ogid != gid)) {
740 ip->i_mode &= ~(ISUID | ISGID);
741 DIP_SET(ip, i_mode, ip->i_mode);
742 }
743 return (0);
744 }
745
746 static int
747 ufs_remove(ap)
748 struct vop_remove_args /* {
749 struct vnode *a_dvp;
750 struct vnode *a_vp;
751 struct componentname *a_cnp;
752 } */ *ap;
753 {
754 struct inode *ip;
755 struct vnode *vp = ap->a_vp;
756 struct vnode *dvp = ap->a_dvp;
757 int error;
758
759 ip = VTOI(vp);
760 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
761 (VTOI(dvp)->i_flags & APPEND)) {
762 error = EPERM;
763 goto out;
764 }
765 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
766 if (ip->i_nlink <= 0)
767 vp->v_vflag |= VV_NOSYNC;
768 out:
769 return (error);
770 }
771
772 /*
773 * link vnode call
774 */
775 static int
776 ufs_link(ap)
777 struct vop_link_args /* {
778 struct vnode *a_tdvp;
779 struct vnode *a_vp;
780 struct componentname *a_cnp;
781 } */ *ap;
782 {
783 struct vnode *vp = ap->a_vp;
784 struct vnode *tdvp = ap->a_tdvp;
785 struct componentname *cnp = ap->a_cnp;
786 struct inode *ip;
787 struct direct newdir;
788 int error;
789
790 #ifdef DIAGNOSTIC
791 if ((cnp->cn_flags & HASBUF) == 0)
792 panic("ufs_link: no name");
793 #endif
794 if (tdvp->v_mount != vp->v_mount) {
795 error = EXDEV;
796 goto out;
797 }
798 ip = VTOI(vp);
799 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
800 error = EMLINK;
801 goto out;
802 }
803 if (ip->i_flags & (IMMUTABLE | APPEND)) {
804 error = EPERM;
805 goto out;
806 }
807 ip->i_effnlink++;
808 ip->i_nlink++;
809 DIP_SET(ip, i_nlink, ip->i_nlink);
810 ip->i_flag |= IN_CHANGE;
811 if (DOINGSOFTDEP(vp))
812 softdep_change_linkcnt(ip);
813 error = UFS_UPDATE(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
814 if (!error) {
815 ufs_makedirentry(ip, cnp, &newdir);
816 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
817 }
818
819 if (error) {
820 ip->i_effnlink--;
821 ip->i_nlink--;
822 DIP_SET(ip, i_nlink, ip->i_nlink);
823 ip->i_flag |= IN_CHANGE;
824 if (DOINGSOFTDEP(vp))
825 softdep_change_linkcnt(ip);
826 }
827 out:
828 return (error);
829 }
830
831 /*
832 * whiteout vnode call
833 */
834 static int
835 ufs_whiteout(ap)
836 struct vop_whiteout_args /* {
837 struct vnode *a_dvp;
838 struct componentname *a_cnp;
839 int a_flags;
840 } */ *ap;
841 {
842 struct vnode *dvp = ap->a_dvp;
843 struct componentname *cnp = ap->a_cnp;
844 struct direct newdir;
845 int error = 0;
846
847 switch (ap->a_flags) {
848 case LOOKUP:
849 /* 4.4 format directories support whiteout operations */
850 if (dvp->v_mount->mnt_maxsymlinklen > 0)
851 return (0);
852 return (EOPNOTSUPP);
853
854 case CREATE:
855 /* create a new directory whiteout */
856 #ifdef DIAGNOSTIC
857 if ((cnp->cn_flags & SAVENAME) == 0)
858 panic("ufs_whiteout: missing name");
859 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
860 panic("ufs_whiteout: old format filesystem");
861 #endif
862
863 newdir.d_ino = WINO;
864 newdir.d_namlen = cnp->cn_namelen;
865 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
866 newdir.d_type = DT_WHT;
867 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
868 break;
869
870 case DELETE:
871 /* remove an existing directory whiteout */
872 #ifdef DIAGNOSTIC
873 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
874 panic("ufs_whiteout: old format filesystem");
875 #endif
876
877 cnp->cn_flags &= ~DOWHITEOUT;
878 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
879 break;
880 default:
881 panic("ufs_whiteout: unknown op");
882 }
883 return (error);
884 }
885
886 /*
887 * Rename system call.
888 * rename("foo", "bar");
889 * is essentially
890 * unlink("bar");
891 * link("foo", "bar");
892 * unlink("foo");
893 * but ``atomically''. Can't do full commit without saving state in the
894 * inode on disk which isn't feasible at this time. Best we can do is
895 * always guarantee the target exists.
896 *
897 * Basic algorithm is:
898 *
899 * 1) Bump link count on source while we're linking it to the
900 * target. This also ensure the inode won't be deleted out
901 * from underneath us while we work (it may be truncated by
902 * a concurrent `trunc' or `open' for creation).
903 * 2) Link source to destination. If destination already exists,
904 * delete it first.
905 * 3) Unlink source reference to inode if still around. If a
906 * directory was moved and the parent of the destination
907 * is different from the source, patch the ".." entry in the
908 * directory.
909 */
910 static int
911 ufs_rename(ap)
912 struct vop_rename_args /* {
913 struct vnode *a_fdvp;
914 struct vnode *a_fvp;
915 struct componentname *a_fcnp;
916 struct vnode *a_tdvp;
917 struct vnode *a_tvp;
918 struct componentname *a_tcnp;
919 } */ *ap;
920 {
921 struct vnode *tvp = ap->a_tvp;
922 struct vnode *tdvp = ap->a_tdvp;
923 struct vnode *fvp = ap->a_fvp;
924 struct vnode *fdvp = ap->a_fdvp;
925 struct componentname *tcnp = ap->a_tcnp;
926 struct componentname *fcnp = ap->a_fcnp;
927 struct thread *td = fcnp->cn_thread;
928 struct inode *ip, *xp, *dp;
929 struct direct newdir;
930 int doingdirectory = 0, oldparent = 0, newparent = 0;
931 int error = 0, ioflag;
932
933 #ifdef DIAGNOSTIC
934 if ((tcnp->cn_flags & HASBUF) == 0 ||
935 (fcnp->cn_flags & HASBUF) == 0)
936 panic("ufs_rename: no name");
937 #endif
938 /*
939 * Check for cross-device rename.
940 */
941 if ((fvp->v_mount != tdvp->v_mount) ||
942 (tvp && (fvp->v_mount != tvp->v_mount))) {
943 error = EXDEV;
944 abortit:
945 if (tdvp == tvp)
946 vrele(tdvp);
947 else
948 vput(tdvp);
949 if (tvp)
950 vput(tvp);
951 vrele(fdvp);
952 vrele(fvp);
953 return (error);
954 }
955
956 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
957 (VTOI(tdvp)->i_flags & APPEND))) {
958 error = EPERM;
959 goto abortit;
960 }
961
962 /*
963 * Renaming a file to itself has no effect. The upper layers should
964 * not call us in that case. Temporarily just warn if they do.
965 */
966 if (fvp == tvp) {
967 printf("ufs_rename: fvp == tvp (can't happen)\n");
968 error = 0;
969 goto abortit;
970 }
971
972 if ((error = vn_lock(fvp, LK_EXCLUSIVE, td)) != 0)
973 goto abortit;
974 dp = VTOI(fdvp);
975 ip = VTOI(fvp);
976 if (ip->i_nlink >= LINK_MAX) {
977 VOP_UNLOCK(fvp, 0, td);
978 error = EMLINK;
979 goto abortit;
980 }
981 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
982 || (dp->i_flags & APPEND)) {
983 VOP_UNLOCK(fvp, 0, td);
984 error = EPERM;
985 goto abortit;
986 }
987 if ((ip->i_mode & IFMT) == IFDIR) {
988 /*
989 * Avoid ".", "..", and aliases of "." for obvious reasons.
990 */
991 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
992 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
993 (ip->i_flag & IN_RENAME)) {
994 VOP_UNLOCK(fvp, 0, td);
995 error = EINVAL;
996 goto abortit;
997 }
998 ip->i_flag |= IN_RENAME;
999 oldparent = dp->i_number;
1000 doingdirectory = 1;
1001 }
1002 vrele(fdvp);
1003
1004 /*
1005 * When the target exists, both the directory
1006 * and target vnodes are returned locked.
1007 */
1008 dp = VTOI(tdvp);
1009 xp = NULL;
1010 if (tvp)
1011 xp = VTOI(tvp);
1012
1013 /*
1014 * 1) Bump link count while we're moving stuff
1015 * around. If we crash somewhere before
1016 * completing our work, the link count
1017 * may be wrong, but correctable.
1018 */
1019 ip->i_effnlink++;
1020 ip->i_nlink++;
1021 DIP_SET(ip, i_nlink, ip->i_nlink);
1022 ip->i_flag |= IN_CHANGE;
1023 if (DOINGSOFTDEP(fvp))
1024 softdep_change_linkcnt(ip);
1025 if ((error = UFS_UPDATE(fvp, !(DOINGSOFTDEP(fvp) |
1026 DOINGASYNC(fvp)))) != 0) {
1027 VOP_UNLOCK(fvp, 0, td);
1028 goto bad;
1029 }
1030
1031 /*
1032 * If ".." must be changed (ie the directory gets a new
1033 * parent) then the source directory must not be in the
1034 * directory heirarchy above the target, as this would
1035 * orphan everything below the source directory. Also
1036 * the user must have write permission in the source so
1037 * as to be able to change "..". We must repeat the call
1038 * to namei, as the parent directory is unlocked by the
1039 * call to checkpath().
1040 */
1041 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
1042 VOP_UNLOCK(fvp, 0, td);
1043 if (oldparent != dp->i_number)
1044 newparent = dp->i_number;
1045 if (doingdirectory && newparent) {
1046 if (error) /* write access check above */
1047 goto bad;
1048 if (xp != NULL)
1049 vput(tvp);
1050 error = ufs_checkpath(ip, dp, tcnp->cn_cred);
1051 if (error)
1052 goto out;
1053 if ((tcnp->cn_flags & SAVESTART) == 0)
1054 panic("ufs_rename: lost to startdir");
1055 VREF(tdvp);
1056 error = relookup(tdvp, &tvp, tcnp);
1057 if (error)
1058 goto out;
1059 vrele(tdvp);
1060 dp = VTOI(tdvp);
1061 xp = NULL;
1062 if (tvp)
1063 xp = VTOI(tvp);
1064 }
1065 /*
1066 * 2) If target doesn't exist, link the target
1067 * to the source and unlink the source.
1068 * Otherwise, rewrite the target directory
1069 * entry to reference the source inode and
1070 * expunge the original entry's existence.
1071 */
1072 if (xp == NULL) {
1073 if (dp->i_dev != ip->i_dev)
1074 panic("ufs_rename: EXDEV");
1075 /*
1076 * Account for ".." in new directory.
1077 * When source and destination have the same
1078 * parent we don't fool with the link count.
1079 */
1080 if (doingdirectory && newparent) {
1081 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1082 error = EMLINK;
1083 goto bad;
1084 }
1085 dp->i_effnlink++;
1086 dp->i_nlink++;
1087 DIP_SET(dp, i_nlink, dp->i_nlink);
1088 dp->i_flag |= IN_CHANGE;
1089 if (DOINGSOFTDEP(tdvp))
1090 softdep_change_linkcnt(dp);
1091 error = UFS_UPDATE(tdvp, !(DOINGSOFTDEP(tdvp) |
1092 DOINGASYNC(tdvp)));
1093 if (error)
1094 goto bad;
1095 }
1096 ufs_makedirentry(ip, tcnp, &newdir);
1097 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1098 if (error) {
1099 if (doingdirectory && newparent) {
1100 dp->i_effnlink--;
1101 dp->i_nlink--;
1102 DIP_SET(dp, i_nlink, dp->i_nlink);
1103 dp->i_flag |= IN_CHANGE;
1104 if (DOINGSOFTDEP(tdvp))
1105 softdep_change_linkcnt(dp);
1106 (void)UFS_UPDATE(tdvp, 1);
1107 }
1108 goto bad;
1109 }
1110 vput(tdvp);
1111 } else {
1112 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1113 panic("ufs_rename: EXDEV");
1114 /*
1115 * Short circuit rename(foo, foo).
1116 */
1117 if (xp->i_number == ip->i_number)
1118 panic("ufs_rename: same file");
1119 /*
1120 * If the parent directory is "sticky", then the caller
1121 * must possess VADMIN for the parent directory, or the
1122 * destination of the rename. This implements append-only
1123 * directories.
1124 */
1125 if ((dp->i_mode & S_ISTXT) &&
1126 VOP_ACCESS(tdvp, VADMIN, tcnp->cn_cred, td) &&
1127 VOP_ACCESS(tvp, VADMIN, tcnp->cn_cred, td)) {
1128 error = EPERM;
1129 goto bad;
1130 }
1131 /*
1132 * Target must be empty if a directory and have no links
1133 * to it. Also, ensure source and target are compatible
1134 * (both directories, or both not directories).
1135 */
1136 if ((xp->i_mode&IFMT) == IFDIR) {
1137 if ((xp->i_effnlink > 2) ||
1138 !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1139 error = ENOTEMPTY;
1140 goto bad;
1141 }
1142 if (!doingdirectory) {
1143 error = ENOTDIR;
1144 goto bad;
1145 }
1146 cache_purge(tdvp);
1147 } else if (doingdirectory) {
1148 error = EISDIR;
1149 goto bad;
1150 }
1151 error = ufs_dirrewrite(dp, xp, ip->i_number,
1152 IFTODT(ip->i_mode),
1153 (doingdirectory && newparent) ? newparent : doingdirectory);
1154 if (error)
1155 goto bad;
1156 if (doingdirectory) {
1157 if (!newparent) {
1158 dp->i_effnlink--;
1159 if (DOINGSOFTDEP(tdvp))
1160 softdep_change_linkcnt(dp);
1161 }
1162 xp->i_effnlink--;
1163 if (DOINGSOFTDEP(tvp))
1164 softdep_change_linkcnt(xp);
1165 }
1166 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1167 /*
1168 * Truncate inode. The only stuff left in the directory
1169 * is "." and "..". The "." reference is inconsequential
1170 * since we are quashing it. We have removed the "."
1171 * reference and the reference in the parent directory,
1172 * but there may be other hard links. The soft
1173 * dependency code will arrange to do these operations
1174 * after the parent directory entry has been deleted on
1175 * disk, so when running with that code we avoid doing
1176 * them now.
1177 */
1178 if (!newparent) {
1179 dp->i_nlink--;
1180 DIP_SET(dp, i_nlink, dp->i_nlink);
1181 dp->i_flag |= IN_CHANGE;
1182 }
1183 xp->i_nlink--;
1184 DIP_SET(xp, i_nlink, xp->i_nlink);
1185 xp->i_flag |= IN_CHANGE;
1186 ioflag = IO_NORMAL;
1187 if (DOINGASYNC(tvp))
1188 ioflag |= IO_SYNC;
1189 if ((error = UFS_TRUNCATE(tvp, (off_t)0, ioflag,
1190 tcnp->cn_cred, tcnp->cn_thread)) != 0)
1191 goto bad;
1192 }
1193 vput(tdvp);
1194 vput(tvp);
1195 xp = NULL;
1196 }
1197
1198 /*
1199 * 3) Unlink the source.
1200 */
1201 fcnp->cn_flags &= ~MODMASK;
1202 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1203 if ((fcnp->cn_flags & SAVESTART) == 0)
1204 panic("ufs_rename: lost from startdir");
1205 VREF(fdvp);
1206 error = relookup(fdvp, &fvp, fcnp);
1207 if (error == 0)
1208 vrele(fdvp);
1209 if (fvp != NULL) {
1210 xp = VTOI(fvp);
1211 dp = VTOI(fdvp);
1212 } else {
1213 /*
1214 * From name has disappeared. IN_RENAME is not sufficient
1215 * to protect against directory races due to timing windows,
1216 * so we have to remove the panic. XXX the only real way
1217 * to solve this issue is at a much higher level. By the
1218 * time we hit ufs_rename() it's too late.
1219 */
1220 #if 0
1221 if (doingdirectory)
1222 panic("ufs_rename: lost dir entry");
1223 #endif
1224 vrele(ap->a_fvp);
1225 return (0);
1226 }
1227 /*
1228 * Ensure that the directory entry still exists and has not
1229 * changed while the new name has been entered. If the source is
1230 * a file then the entry may have been unlinked or renamed. In
1231 * either case there is no further work to be done. If the source
1232 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1233 * flag ensures that it cannot be moved by another rename or removed
1234 * by a rmdir.
1235 */
1236 if (xp != ip) {
1237 /*
1238 * From name resolves to a different inode. IN_RENAME is
1239 * not sufficient protection against timing window races
1240 * so we can't panic here. XXX the only real way
1241 * to solve this issue is at a much higher level. By the
1242 * time we hit ufs_rename() it's too late.
1243 */
1244 #if 0
1245 if (doingdirectory)
1246 panic("ufs_rename: lost dir entry");
1247 #endif
1248 } else {
1249 /*
1250 * If the source is a directory with a
1251 * new parent, the link count of the old
1252 * parent directory must be decremented
1253 * and ".." set to point to the new parent.
1254 */
1255 if (doingdirectory && newparent) {
1256 xp->i_offset = mastertemplate.dot_reclen;
1257 ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1258 cache_purge(fdvp);
1259 }
1260 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1261 xp->i_flag &= ~IN_RENAME;
1262 }
1263 if (dp)
1264 vput(fdvp);
1265 if (xp)
1266 vput(fvp);
1267 vrele(ap->a_fvp);
1268 return (error);
1269
1270 bad:
1271 if (xp)
1272 vput(ITOV(xp));
1273 vput(ITOV(dp));
1274 out:
1275 if (doingdirectory)
1276 ip->i_flag &= ~IN_RENAME;
1277 if (vn_lock(fvp, LK_EXCLUSIVE, td) == 0) {
1278 ip->i_effnlink--;
1279 ip->i_nlink--;
1280 DIP_SET(ip, i_nlink, ip->i_nlink);
1281 ip->i_flag |= IN_CHANGE;
1282 ip->i_flag &= ~IN_RENAME;
1283 if (DOINGSOFTDEP(fvp))
1284 softdep_change_linkcnt(ip);
1285 vput(fvp);
1286 } else
1287 vrele(fvp);
1288 return (error);
1289 }
1290
1291 /*
1292 * Mkdir system call
1293 */
1294 static int
1295 ufs_mkdir(ap)
1296 struct vop_mkdir_args /* {
1297 struct vnode *a_dvp;
1298 struct vnode **a_vpp;
1299 struct componentname *a_cnp;
1300 struct vattr *a_vap;
1301 } */ *ap;
1302 {
1303 struct vnode *dvp = ap->a_dvp;
1304 struct vattr *vap = ap->a_vap;
1305 struct componentname *cnp = ap->a_cnp;
1306 struct inode *ip, *dp;
1307 struct vnode *tvp;
1308 struct buf *bp;
1309 struct dirtemplate dirtemplate, *dtp;
1310 struct direct newdir;
1311 #ifdef UFS_ACL
1312 struct acl *acl, *dacl;
1313 #endif
1314 int error, dmode;
1315 long blkoff;
1316
1317 #ifdef DIAGNOSTIC
1318 if ((cnp->cn_flags & HASBUF) == 0)
1319 panic("ufs_mkdir: no name");
1320 #endif
1321 dp = VTOI(dvp);
1322 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1323 error = EMLINK;
1324 goto out;
1325 }
1326 dmode = vap->va_mode & 0777;
1327 dmode |= IFDIR;
1328 /*
1329 * Must simulate part of ufs_makeinode here to acquire the inode,
1330 * but not have it entered in the parent directory. The entry is
1331 * made later after writing "." and ".." entries.
1332 */
1333 error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
1334 if (error)
1335 goto out;
1336 ip = VTOI(tvp);
1337 ip->i_gid = dp->i_gid;
1338 DIP_SET(ip, i_gid, dp->i_gid);
1339 #ifdef SUIDDIR
1340 {
1341 #ifdef QUOTA
1342 struct ucred ucred, *ucp;
1343 ucp = cnp->cn_cred;
1344 #endif
1345 /*
1346 * If we are hacking owners here, (only do this where told to)
1347 * and we are not giving it TO root, (would subvert quotas)
1348 * then go ahead and give it to the other user.
1349 * The new directory also inherits the SUID bit.
1350 * If user's UID and dir UID are the same,
1351 * 'give it away' so that the SUID is still forced on.
1352 */
1353 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1354 (dp->i_mode & ISUID) && dp->i_uid) {
1355 dmode |= ISUID;
1356 ip->i_uid = dp->i_uid;
1357 DIP_SET(ip, i_uid, dp->i_uid);
1358 #ifdef QUOTA
1359 if (dp->i_uid != cnp->cn_cred->cr_uid) {
1360 /*
1361 * Make sure the correct user gets charged
1362 * for the space.
1363 * Make a dummy credential for the victim.
1364 * XXX This seems to never be accessed out of
1365 * our context so a stack variable is ok.
1366 */
1367 ucred.cr_ref = 1;
1368 ucred.cr_uid = ip->i_uid;
1369 ucred.cr_ngroups = 1;
1370 ucred.cr_groups[0] = dp->i_gid;
1371 ucp = &ucred;
1372 }
1373 #endif
1374 } else {
1375 ip->i_uid = cnp->cn_cred->cr_uid;
1376 DIP_SET(ip, i_uid, ip->i_uid);
1377 }
1378 #ifdef QUOTA
1379 if ((error = getinoquota(ip)) ||
1380 (error = chkiq(ip, 1, ucp, 0))) {
1381 UFS_VFREE(tvp, ip->i_number, dmode);
1382 vput(tvp);
1383 return (error);
1384 }
1385 #endif
1386 }
1387 #else /* !SUIDDIR */
1388 ip->i_uid = cnp->cn_cred->cr_uid;
1389 DIP_SET(ip, i_uid, ip->i_uid);
1390 #ifdef QUOTA
1391 if ((error = getinoquota(ip)) ||
1392 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1393 UFS_VFREE(tvp, ip->i_number, dmode);
1394 vput(tvp);
1395 return (error);
1396 }
1397 #endif
1398 #endif /* !SUIDDIR */
1399 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1400 #ifdef UFS_ACL
1401 acl = dacl = NULL;
1402 if ((dvp->v_mount->mnt_flag & MNT_ACLS) != 0) {
1403 MALLOC(acl, struct acl *, sizeof(*acl), M_ACL, M_WAITOK);
1404 MALLOC(dacl, struct acl *, sizeof(*dacl), M_ACL, M_WAITOK);
1405
1406 /*
1407 * Retrieve default ACL from parent, if any.
1408 */
1409 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cnp->cn_cred,
1410 cnp->cn_thread);
1411 switch (error) {
1412 case 0:
1413 /*
1414 * Retrieved a default ACL, so merge mode and ACL if
1415 * necessary. If the ACL is empty, fall through to
1416 * the "not defined or available" case.
1417 */
1418 if (acl->acl_cnt != 0) {
1419 dmode = acl_posix1e_newfilemode(dmode, acl);
1420 ip->i_mode = dmode;
1421 DIP_SET(ip, i_mode, dmode);
1422 *dacl = *acl;
1423 ufs_sync_acl_from_inode(ip, acl);
1424 break;
1425 }
1426 /* FALLTHROUGH */
1427
1428 case EOPNOTSUPP:
1429 /*
1430 * Just use the mode as-is.
1431 */
1432 ip->i_mode = dmode;
1433 DIP_SET(ip, i_mode, dmode);
1434 FREE(acl, M_ACL);
1435 FREE(dacl, M_ACL);
1436 dacl = acl = NULL;
1437 break;
1438
1439 default:
1440 UFS_VFREE(tvp, ip->i_number, dmode);
1441 vput(tvp);
1442 FREE(acl, M_ACL);
1443 FREE(dacl, M_ACL);
1444 return (error);
1445 }
1446 } else {
1447 #endif /* !UFS_ACL */
1448 ip->i_mode = dmode;
1449 DIP_SET(ip, i_mode, dmode);
1450 #ifdef UFS_ACL
1451 }
1452 #endif
1453 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1454 ip->i_effnlink = 2;
1455 ip->i_nlink = 2;
1456 DIP_SET(ip, i_nlink, 2);
1457 if (DOINGSOFTDEP(tvp))
1458 softdep_change_linkcnt(ip);
1459 if (cnp->cn_flags & ISWHITEOUT) {
1460 ip->i_flags |= UF_OPAQUE;
1461 DIP_SET(ip, i_flags, ip->i_flags);
1462 }
1463
1464 /*
1465 * Bump link count in parent directory to reflect work done below.
1466 * Should be done before reference is created so cleanup is
1467 * possible if we crash.
1468 */
1469 dp->i_effnlink++;
1470 dp->i_nlink++;
1471 DIP_SET(dp, i_nlink, dp->i_nlink);
1472 dp->i_flag |= IN_CHANGE;
1473 if (DOINGSOFTDEP(dvp))
1474 softdep_change_linkcnt(dp);
1475 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1476 if (error)
1477 goto bad;
1478 #ifdef MAC
1479 if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
1480 error = mac_create_vnode_extattr(cnp->cn_cred, dvp->v_mount,
1481 dvp, tvp, cnp);
1482 if (error)
1483 goto bad;
1484 }
1485 #endif
1486 #ifdef UFS_ACL
1487 if (acl != NULL) {
1488 /*
1489 * XXX: If we abort now, will Soft Updates notify the extattr
1490 * code that the EAs for the file need to be released?
1491 */
1492 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cnp->cn_cred,
1493 cnp->cn_thread);
1494 if (error == 0)
1495 error = VOP_SETACL(tvp, ACL_TYPE_DEFAULT, dacl,
1496 cnp->cn_cred, cnp->cn_thread);
1497 switch (error) {
1498 case 0:
1499 break;
1500
1501 case EOPNOTSUPP:
1502 /*
1503 * XXX: This should not happen, as EOPNOTSUPP above
1504 * was supposed to free acl.
1505 */
1506 printf("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()\n");
1507 /*
1508 panic("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()");
1509 */
1510 break;
1511
1512 default:
1513 FREE(acl, M_ACL);
1514 FREE(dacl, M_ACL);
1515 dacl = acl = NULL;
1516 goto bad;
1517 }
1518 FREE(acl, M_ACL);
1519 FREE(dacl, M_ACL);
1520 dacl = acl = NULL;
1521 }
1522 #endif /* !UFS_ACL */
1523
1524 /*
1525 * Initialize directory with "." and ".." from static template.
1526 */
1527 if (dvp->v_mount->mnt_maxsymlinklen > 0)
1528 dtp = &mastertemplate;
1529 else
1530 dtp = (struct dirtemplate *)&omastertemplate;
1531 dirtemplate = *dtp;
1532 dirtemplate.dot_ino = ip->i_number;
1533 dirtemplate.dotdot_ino = dp->i_number;
1534 if ((error = UFS_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred,
1535 BA_CLRBUF, &bp)) != 0)
1536 goto bad;
1537 ip->i_size = DIRBLKSIZ;
1538 DIP_SET(ip, i_size, DIRBLKSIZ);
1539 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1540 vnode_pager_setsize(tvp, (u_long)ip->i_size);
1541 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1542 if (DOINGSOFTDEP(tvp)) {
1543 /*
1544 * Ensure that the entire newly allocated block is a
1545 * valid directory so that future growth within the
1546 * block does not have to ensure that the block is
1547 * written before the inode.
1548 */
1549 blkoff = DIRBLKSIZ;
1550 while (blkoff < bp->b_bcount) {
1551 ((struct direct *)
1552 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1553 blkoff += DIRBLKSIZ;
1554 }
1555 }
1556 if ((error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) |
1557 DOINGASYNC(tvp)))) != 0) {
1558 (void)bwrite(bp);
1559 goto bad;
1560 }
1561 /*
1562 * Directory set up, now install its entry in the parent directory.
1563 *
1564 * If we are not doing soft dependencies, then we must write out the
1565 * buffer containing the new directory body before entering the new
1566 * name in the parent. If we are doing soft dependencies, then the
1567 * buffer containing the new directory body will be passed to and
1568 * released in the soft dependency code after the code has attached
1569 * an appropriate ordering dependency to the buffer which ensures that
1570 * the buffer is written before the new name is written in the parent.
1571 */
1572 if (DOINGASYNC(dvp))
1573 bdwrite(bp);
1574 else if (!DOINGSOFTDEP(dvp) && ((error = bwrite(bp))))
1575 goto bad;
1576 ufs_makedirentry(ip, cnp, &newdir);
1577 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1578
1579 bad:
1580 if (error == 0) {
1581 *ap->a_vpp = tvp;
1582 } else {
1583 #ifdef UFS_ACL
1584 if (acl != NULL)
1585 FREE(acl, M_ACL);
1586 if (dacl != NULL)
1587 FREE(dacl, M_ACL);
1588 #endif
1589 dp->i_effnlink--;
1590 dp->i_nlink--;
1591 DIP_SET(dp, i_nlink, dp->i_nlink);
1592 dp->i_flag |= IN_CHANGE;
1593 if (DOINGSOFTDEP(dvp))
1594 softdep_change_linkcnt(dp);
1595 /*
1596 * No need to do an explicit VOP_TRUNCATE here, vrele will
1597 * do this for us because we set the link count to 0.
1598 */
1599 ip->i_effnlink = 0;
1600 ip->i_nlink = 0;
1601 DIP_SET(ip, i_nlink, 0);
1602 ip->i_flag |= IN_CHANGE;
1603 if (DOINGSOFTDEP(tvp))
1604 softdep_change_linkcnt(ip);
1605 vput(tvp);
1606 }
1607 out:
1608 return (error);
1609 }
1610
1611 /*
1612 * Rmdir system call.
1613 */
1614 static int
1615 ufs_rmdir(ap)
1616 struct vop_rmdir_args /* {
1617 struct vnode *a_dvp;
1618 struct vnode *a_vp;
1619 struct componentname *a_cnp;
1620 } */ *ap;
1621 {
1622 struct vnode *vp = ap->a_vp;
1623 struct vnode *dvp = ap->a_dvp;
1624 struct componentname *cnp = ap->a_cnp;
1625 struct inode *ip, *dp;
1626 int error, ioflag;
1627
1628 ip = VTOI(vp);
1629 dp = VTOI(dvp);
1630
1631 /*
1632 * Do not remove a directory that is in the process of being renamed.
1633 * Verify the directory is empty (and valid). Rmdir ".." will not be
1634 * valid since ".." will contain a reference to the current directory
1635 * and thus be non-empty. Do not allow the removal of mounted on
1636 * directories (this can happen when an NFS exported filesystem
1637 * tries to remove a locally mounted on directory).
1638 */
1639 error = 0;
1640 if ((ip->i_flag & IN_RENAME) || ip->i_effnlink < 2) {
1641 error = EINVAL;
1642 goto out;
1643 }
1644 if (!ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1645 error = ENOTEMPTY;
1646 goto out;
1647 }
1648 if ((dp->i_flags & APPEND)
1649 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1650 error = EPERM;
1651 goto out;
1652 }
1653 if (vp->v_mountedhere != 0) {
1654 error = EINVAL;
1655 goto out;
1656 }
1657 /*
1658 * Delete reference to directory before purging
1659 * inode. If we crash in between, the directory
1660 * will be reattached to lost+found,
1661 */
1662 dp->i_effnlink--;
1663 ip->i_effnlink--;
1664 if (DOINGSOFTDEP(vp)) {
1665 softdep_change_linkcnt(dp);
1666 softdep_change_linkcnt(ip);
1667 }
1668 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1669 if (error) {
1670 dp->i_effnlink++;
1671 ip->i_effnlink++;
1672 if (DOINGSOFTDEP(vp)) {
1673 softdep_change_linkcnt(dp);
1674 softdep_change_linkcnt(ip);
1675 }
1676 goto out;
1677 }
1678 cache_purge(dvp);
1679 /*
1680 * Truncate inode. The only stuff left in the directory is "." and
1681 * "..". The "." reference is inconsequential since we are quashing
1682 * it. The soft dependency code will arrange to do these operations
1683 * after the parent directory entry has been deleted on disk, so
1684 * when running with that code we avoid doing them now.
1685 */
1686 if (!DOINGSOFTDEP(vp)) {
1687 dp->i_nlink--;
1688 DIP_SET(dp, i_nlink, dp->i_nlink);
1689 dp->i_flag |= IN_CHANGE;
1690 ip->i_nlink--;
1691 DIP_SET(ip, i_nlink, ip->i_nlink);
1692 ip->i_flag |= IN_CHANGE;
1693 ioflag = IO_NORMAL;
1694 if (DOINGASYNC(vp))
1695 ioflag |= IO_SYNC;
1696 error = UFS_TRUNCATE(vp, (off_t)0, ioflag, cnp->cn_cred,
1697 cnp->cn_thread);
1698 }
1699 cache_purge(vp);
1700 #ifdef UFS_DIRHASH
1701 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1702 if (ip->i_dirhash != NULL)
1703 ufsdirhash_free(ip);
1704 #endif
1705 out:
1706 return (error);
1707 }
1708
1709 /*
1710 * symlink -- make a symbolic link
1711 */
1712 static int
1713 ufs_symlink(ap)
1714 struct vop_symlink_args /* {
1715 struct vnode *a_dvp;
1716 struct vnode **a_vpp;
1717 struct componentname *a_cnp;
1718 struct vattr *a_vap;
1719 char *a_target;
1720 } */ *ap;
1721 {
1722 struct vnode *vp, **vpp = ap->a_vpp;
1723 struct inode *ip;
1724 int len, error;
1725
1726 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1727 vpp, ap->a_cnp);
1728 if (error)
1729 return (error);
1730 vp = *vpp;
1731 len = strlen(ap->a_target);
1732 if (len < vp->v_mount->mnt_maxsymlinklen) {
1733 ip = VTOI(vp);
1734 bcopy(ap->a_target, SHORTLINK(ip), len);
1735 ip->i_size = len;
1736 DIP_SET(ip, i_size, len);
1737 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1738 } else
1739 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1740 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1741 ap->a_cnp->cn_cred, NOCRED, (int *)0, (struct thread *)0);
1742 if (error)
1743 vput(vp);
1744 return (error);
1745 }
1746
1747 /*
1748 * Vnode op for reading directories.
1749 *
1750 * The routine below assumes that the on-disk format of a directory
1751 * is the same as that defined by <sys/dirent.h>. If the on-disk
1752 * format changes, then it will be necessary to do a conversion
1753 * from the on-disk format that read returns to the format defined
1754 * by <sys/dirent.h>.
1755 */
1756 int
1757 ufs_readdir(ap)
1758 struct vop_readdir_args /* {
1759 struct vnode *a_vp;
1760 struct uio *a_uio;
1761 struct ucred *a_cred;
1762 int *a_eofflag;
1763 int *ncookies;
1764 u_long **a_cookies;
1765 } */ *ap;
1766 {
1767 struct uio *uio = ap->a_uio;
1768 int error;
1769 size_t count, lost;
1770 off_t off;
1771
1772 if (ap->a_ncookies != NULL)
1773 /*
1774 * Ensure that the block is aligned. The caller can use
1775 * the cookies to determine where in the block to start.
1776 */
1777 uio->uio_offset &= ~(DIRBLKSIZ - 1);
1778 off = uio->uio_offset;
1779 count = uio->uio_resid;
1780 /* Make sure we don't return partial entries. */
1781 if (count <= ((uio->uio_offset + count) & (DIRBLKSIZ -1)))
1782 return (EINVAL);
1783 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
1784 lost = uio->uio_resid - count;
1785 uio->uio_resid = count;
1786 uio->uio_iov->iov_len = count;
1787 # if (BYTE_ORDER == LITTLE_ENDIAN)
1788 if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) {
1789 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1790 } else {
1791 struct dirent *dp, *edp;
1792 struct uio auio;
1793 struct iovec aiov;
1794 caddr_t dirbuf;
1795 int readcnt;
1796 u_char tmp;
1797
1798 auio = *uio;
1799 auio.uio_iov = &aiov;
1800 auio.uio_iovcnt = 1;
1801 auio.uio_segflg = UIO_SYSSPACE;
1802 aiov.iov_len = count;
1803 MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
1804 aiov.iov_base = dirbuf;
1805 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
1806 if (error == 0) {
1807 readcnt = count - auio.uio_resid;
1808 edp = (struct dirent *)&dirbuf[readcnt];
1809 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
1810 tmp = dp->d_namlen;
1811 dp->d_namlen = dp->d_type;
1812 dp->d_type = tmp;
1813 if (dp->d_reclen > 0) {
1814 dp = (struct dirent *)
1815 ((char *)dp + dp->d_reclen);
1816 } else {
1817 error = EIO;
1818 break;
1819 }
1820 }
1821 if (dp >= edp)
1822 error = uiomove(dirbuf, readcnt, uio);
1823 }
1824 FREE(dirbuf, M_TEMP);
1825 }
1826 # else
1827 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1828 # endif
1829 if (!error && ap->a_ncookies != NULL) {
1830 struct dirent* dpStart;
1831 struct dirent* dpEnd;
1832 struct dirent* dp;
1833 int ncookies;
1834 u_long *cookies;
1835 u_long *cookiep;
1836
1837 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1838 panic("ufs_readdir: unexpected uio from NFS server");
1839 dpStart = (struct dirent *)
1840 ((char *)uio->uio_iov->iov_base - (uio->uio_offset - off));
1841 dpEnd = (struct dirent *) uio->uio_iov->iov_base;
1842 for (dp = dpStart, ncookies = 0;
1843 dp < dpEnd;
1844 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen))
1845 ncookies++;
1846 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1847 M_WAITOK);
1848 for (dp = dpStart, cookiep = cookies;
1849 dp < dpEnd;
1850 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
1851 off += dp->d_reclen;
1852 *cookiep++ = (u_long) off;
1853 }
1854 *ap->a_ncookies = ncookies;
1855 *ap->a_cookies = cookies;
1856 }
1857 uio->uio_resid += lost;
1858 if (ap->a_eofflag)
1859 *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
1860 return (error);
1861 }
1862
1863 /*
1864 * Return target name of a symbolic link
1865 */
1866 static int
1867 ufs_readlink(ap)
1868 struct vop_readlink_args /* {
1869 struct vnode *a_vp;
1870 struct uio *a_uio;
1871 struct ucred *a_cred;
1872 } */ *ap;
1873 {
1874 struct vnode *vp = ap->a_vp;
1875 struct inode *ip = VTOI(vp);
1876 doff_t isize;
1877
1878 isize = ip->i_size;
1879 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1880 DIP(ip, i_blocks) == 0) { /* XXX - for old fastlink support */
1881 return (uiomove(SHORTLINK(ip), isize, ap->a_uio));
1882 }
1883 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1884 }
1885
1886 /*
1887 * Calculate the logical to physical mapping if not done already,
1888 * then call the device strategy routine.
1889 *
1890 * In order to be able to swap to a file, the ufs_bmaparray() operation may not
1891 * deadlock on memory. See ufs_bmap() for details.
1892 */
1893 static int
1894 ufs_strategy(ap)
1895 struct vop_strategy_args /* {
1896 struct vnode *a_vp;
1897 struct buf *a_bp;
1898 } */ *ap;
1899 {
1900 struct buf *bp = ap->a_bp;
1901 struct vnode *vp = ap->a_vp;
1902 struct bufobj *bo;
1903 struct inode *ip;
1904 ufs2_daddr_t blkno;
1905 int error;
1906
1907 ip = VTOI(vp);
1908 if (bp->b_blkno == bp->b_lblkno) {
1909 error = ufs_bmaparray(vp, bp->b_lblkno, &blkno, bp, NULL, NULL);
1910 bp->b_blkno = blkno;
1911 if (error) {
1912 bp->b_error = error;
1913 bp->b_ioflags |= BIO_ERROR;
1914 bufdone(bp);
1915 return (error);
1916 }
1917 if ((long)bp->b_blkno == -1)
1918 vfs_bio_clrbuf(bp);
1919 }
1920 if ((long)bp->b_blkno == -1) {
1921 bufdone(bp);
1922 return (0);
1923 }
1924 bp->b_iooffset = dbtob(bp->b_blkno);
1925 bo = ip->i_umbufobj;
1926 BO_STRATEGY(bo, bp);
1927 return (0);
1928 }
1929
1930 /*
1931 * Print out the contents of an inode.
1932 */
1933 static int
1934 ufs_print(ap)
1935 struct vop_print_args /* {
1936 struct vnode *a_vp;
1937 } */ *ap;
1938 {
1939 struct vnode *vp = ap->a_vp;
1940 struct inode *ip = VTOI(vp);
1941
1942 printf("\tino %lu, on dev %s", (u_long)ip->i_number,
1943 devtoname(ip->i_dev));
1944 if (vp->v_type == VFIFO)
1945 fifo_printinfo(vp);
1946 printf("\n");
1947 return (0);
1948 }
1949
1950 /*
1951 * Close wrapper for fifos.
1952 *
1953 * Update the times on the inode then do device close.
1954 */
1955 static int
1956 ufsfifo_close(ap)
1957 struct vop_close_args /* {
1958 struct vnode *a_vp;
1959 int a_fflag;
1960 struct ucred *a_cred;
1961 struct thread *a_td;
1962 } */ *ap;
1963 {
1964 struct vnode *vp = ap->a_vp;
1965
1966 VI_LOCK(vp);
1967 if (vp->v_usecount > 1)
1968 ufs_itimes(vp);
1969 VI_UNLOCK(vp);
1970 return (fifo_specops.vop_close(ap));
1971 }
1972
1973 /*
1974 * Kqfilter wrapper for fifos.
1975 *
1976 * Fall through to ufs kqfilter routines if needed
1977 */
1978 static int
1979 ufsfifo_kqfilter(ap)
1980 struct vop_kqfilter_args *ap;
1981 {
1982 int error;
1983
1984 error = fifo_specops.vop_kqfilter(ap);
1985 if (error)
1986 error = vfs_kqfilter(ap);
1987 return (error);
1988 }
1989
1990 /*
1991 * Return POSIX pathconf information applicable to ufs filesystems.
1992 */
1993 static int
1994 ufs_pathconf(ap)
1995 struct vop_pathconf_args /* {
1996 struct vnode *a_vp;
1997 int a_name;
1998 int *a_retval;
1999 } */ *ap;
2000 {
2001 int error;
2002
2003 error = 0;
2004 switch (ap->a_name) {
2005 case _PC_LINK_MAX:
2006 *ap->a_retval = LINK_MAX;
2007 break;
2008 case _PC_NAME_MAX:
2009 *ap->a_retval = NAME_MAX;
2010 break;
2011 case _PC_PATH_MAX:
2012 *ap->a_retval = PATH_MAX;
2013 break;
2014 case _PC_PIPE_BUF:
2015 *ap->a_retval = PIPE_BUF;
2016 break;
2017 case _PC_CHOWN_RESTRICTED:
2018 *ap->a_retval = 1;
2019 break;
2020 case _PC_NO_TRUNC:
2021 *ap->a_retval = 1;
2022 break;
2023 case _PC_ACL_EXTENDED:
2024 #ifdef UFS_ACL
2025 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
2026 *ap->a_retval = 1;
2027 else
2028 *ap->a_retval = 0;
2029 #else
2030 *ap->a_retval = 0;
2031 #endif
2032 break;
2033 case _PC_ACL_PATH_MAX:
2034 #ifdef UFS_ACL
2035 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
2036 *ap->a_retval = ACL_MAX_ENTRIES;
2037 else
2038 *ap->a_retval = 3;
2039 #else
2040 *ap->a_retval = 3;
2041 #endif
2042 break;
2043 case _PC_MAC_PRESENT:
2044 #ifdef MAC
2045 if (ap->a_vp->v_mount->mnt_flag & MNT_MULTILABEL)
2046 *ap->a_retval = 1;
2047 else
2048 *ap->a_retval = 0;
2049 #else
2050 *ap->a_retval = 0;
2051 #endif
2052 break;
2053 case _PC_ASYNC_IO:
2054 /* _PC_ASYNC_IO should have been handled by upper layers. */
2055 KASSERT(0, ("_PC_ASYNC_IO should not get here"));
2056 error = EINVAL;
2057 break;
2058 case _PC_PRIO_IO:
2059 *ap->a_retval = 0;
2060 break;
2061 case _PC_SYNC_IO:
2062 *ap->a_retval = 0;
2063 break;
2064 case _PC_ALLOC_SIZE_MIN:
2065 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
2066 break;
2067 case _PC_FILESIZEBITS:
2068 *ap->a_retval = 64;
2069 break;
2070 case _PC_REC_INCR_XFER_SIZE:
2071 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2072 break;
2073 case _PC_REC_MAX_XFER_SIZE:
2074 *ap->a_retval = -1; /* means ``unlimited'' */
2075 break;
2076 case _PC_REC_MIN_XFER_SIZE:
2077 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2078 break;
2079 case _PC_REC_XFER_ALIGN:
2080 *ap->a_retval = PAGE_SIZE;
2081 break;
2082 case _PC_SYMLINK_MAX:
2083 *ap->a_retval = MAXPATHLEN;
2084 break;
2085
2086 default:
2087 error = EINVAL;
2088 break;
2089 }
2090 return (error);
2091 }
2092
2093 /*
2094 * Advisory record locking support
2095 */
2096 static int
2097 ufs_advlock(ap)
2098 struct vop_advlock_args /* {
2099 struct vnode *a_vp;
2100 caddr_t a_id;
2101 int a_op;
2102 struct flock *a_fl;
2103 int a_flags;
2104 } */ *ap;
2105 {
2106 struct inode *ip = VTOI(ap->a_vp);
2107
2108 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
2109 }
2110
2111 /*
2112 * Initialize the vnode associated with a new inode, handle aliased
2113 * vnodes.
2114 */
2115 int
2116 ufs_vinit(mntp, fifoops, vpp)
2117 struct mount *mntp;
2118 struct vop_vector *fifoops;
2119 struct vnode **vpp;
2120 {
2121 struct inode *ip;
2122 struct vnode *vp;
2123
2124 vp = *vpp;
2125 ip = VTOI(vp);
2126 vp->v_type = IFTOVT(ip->i_mode);
2127 if (vp->v_type == VFIFO)
2128 vp->v_op = fifoops;
2129 ASSERT_VOP_LOCKED(vp, "ufs_vinit");
2130 if (ip->i_number == ROOTINO)
2131 vp->v_vflag |= VV_ROOT;
2132 ip->i_modrev = init_va_filerev();
2133 *vpp = vp;
2134 return (0);
2135 }
2136
2137 /*
2138 * Allocate a new inode.
2139 * Vnode dvp must be locked.
2140 */
2141 static int
2142 ufs_makeinode(mode, dvp, vpp, cnp)
2143 int mode;
2144 struct vnode *dvp;
2145 struct vnode **vpp;
2146 struct componentname *cnp;
2147 {
2148 struct inode *ip, *pdir;
2149 struct direct newdir;
2150 struct vnode *tvp;
2151 #ifdef UFS_ACL
2152 struct acl *acl;
2153 #endif
2154 int error;
2155
2156 pdir = VTOI(dvp);
2157 #ifdef DIAGNOSTIC
2158 if ((cnp->cn_flags & HASBUF) == 0)
2159 panic("ufs_makeinode: no name");
2160 #endif
2161 *vpp = NULL;
2162 if ((mode & IFMT) == 0)
2163 mode |= IFREG;
2164
2165 error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2166 if (error)
2167 return (error);
2168 ip = VTOI(tvp);
2169 ip->i_gid = pdir->i_gid;
2170 DIP_SET(ip, i_gid, pdir->i_gid);
2171 #ifdef SUIDDIR
2172 {
2173 #ifdef QUOTA
2174 struct ucred ucred, *ucp;
2175 ucp = cnp->cn_cred;
2176 #endif
2177 /*
2178 * If we are not the owner of the directory,
2179 * and we are hacking owners here, (only do this where told to)
2180 * and we are not giving it TO root, (would subvert quotas)
2181 * then go ahead and give it to the other user.
2182 * Note that this drops off the execute bits for security.
2183 */
2184 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2185 (pdir->i_mode & ISUID) &&
2186 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2187 ip->i_uid = pdir->i_uid;
2188 DIP_SET(ip, i_uid, ip->i_uid);
2189 mode &= ~07111;
2190 #ifdef QUOTA
2191 /*
2192 * Make sure the correct user gets charged
2193 * for the space.
2194 * Quickly knock up a dummy credential for the victim.
2195 * XXX This seems to never be accessed out of our
2196 * context so a stack variable is ok.
2197 */
2198 ucred.cr_ref = 1;
2199 ucred.cr_uid = ip->i_uid;
2200 ucred.cr_ngroups = 1;
2201 ucred.cr_groups[0] = pdir->i_gid;
2202 ucp = &ucred;
2203 #endif
2204 } else {
2205 ip->i_uid = cnp->cn_cred->cr_uid;
2206 DIP_SET(ip, i_uid, ip->i_uid);
2207 }
2208
2209 #ifdef QUOTA
2210 if ((error = getinoquota(ip)) ||
2211 (error = chkiq(ip, 1, ucp, 0))) {
2212 UFS_VFREE(tvp, ip->i_number, mode);
2213 vput(tvp);
2214 return (error);
2215 }
2216 #endif
2217 }
2218 #else /* !SUIDDIR */
2219 ip->i_uid = cnp->cn_cred->cr_uid;
2220 DIP_SET(ip, i_uid, ip->i_uid);
2221 #ifdef QUOTA
2222 if ((error = getinoquota(ip)) ||
2223 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2224 UFS_VFREE(tvp, ip->i_number, mode);
2225 vput(tvp);
2226 return (error);
2227 }
2228 #endif
2229 #endif /* !SUIDDIR */
2230 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2231 #ifdef UFS_ACL
2232 acl = NULL;
2233 if ((dvp->v_mount->mnt_flag & MNT_ACLS) != 0) {
2234 MALLOC(acl, struct acl *, sizeof(*acl), M_ACL, M_WAITOK);
2235
2236 /*
2237 * Retrieve default ACL for parent, if any.
2238 */
2239 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cnp->cn_cred,
2240 cnp->cn_thread);
2241 switch (error) {
2242 case 0:
2243 /*
2244 * Retrieved a default ACL, so merge mode and ACL if
2245 * necessary.
2246 */
2247 if (acl->acl_cnt != 0) {
2248 /*
2249 * Two possible ways for default ACL to not
2250 * be present. First, the EA can be
2251 * undefined, or second, the default ACL can
2252 * be blank. If it's blank, fall through to
2253 * the it's not defined case.
2254 */
2255 mode = acl_posix1e_newfilemode(mode, acl);
2256 ip->i_mode = mode;
2257 DIP_SET(ip, i_mode, mode);
2258 ufs_sync_acl_from_inode(ip, acl);
2259 break;
2260 }
2261 /* FALLTHROUGH */
2262
2263 case EOPNOTSUPP:
2264 /*
2265 * Just use the mode as-is.
2266 */
2267 ip->i_mode = mode;
2268 DIP_SET(ip, i_mode, mode);
2269 FREE(acl, M_ACL);
2270 acl = NULL;
2271 break;
2272
2273 default:
2274 UFS_VFREE(tvp, ip->i_number, mode);
2275 vput(tvp);
2276 FREE(acl, M_ACL);
2277 acl = NULL;
2278 return (error);
2279 }
2280 } else {
2281 #endif
2282 ip->i_mode = mode;
2283 DIP_SET(ip, i_mode, mode);
2284 #ifdef UFS_ACL
2285 }
2286 #endif
2287 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2288 ip->i_effnlink = 1;
2289 ip->i_nlink = 1;
2290 DIP_SET(ip, i_nlink, 1);
2291 if (DOINGSOFTDEP(tvp))
2292 softdep_change_linkcnt(ip);
2293 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2294 suser_cred(cnp->cn_cred, SUSER_ALLOWJAIL)) {
2295 ip->i_mode &= ~ISGID;
2296 DIP_SET(ip, i_mode, ip->i_mode);
2297 }
2298
2299 if (cnp->cn_flags & ISWHITEOUT) {
2300 ip->i_flags |= UF_OPAQUE;
2301 DIP_SET(ip, i_flags, ip->i_flags);
2302 }
2303
2304 /*
2305 * Make sure inode goes to disk before directory entry.
2306 */
2307 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2308 if (error)
2309 goto bad;
2310 #ifdef MAC
2311 if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
2312 error = mac_create_vnode_extattr(cnp->cn_cred, dvp->v_mount,
2313 dvp, tvp, cnp);
2314 if (error)
2315 goto bad;
2316 }
2317 #endif
2318 #ifdef UFS_ACL
2319 if (acl != NULL) {
2320 /*
2321 * XXX: If we abort now, will Soft Updates notify the extattr
2322 * code that the EAs for the file need to be released?
2323 */
2324 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cnp->cn_cred,
2325 cnp->cn_thread);
2326 switch (error) {
2327 case 0:
2328 break;
2329
2330 case EOPNOTSUPP:
2331 /*
2332 * XXX: This should not happen, as EOPNOTSUPP above was
2333 * supposed to free acl.
2334 */
2335 printf("ufs_makeinode: VOP_GETACL() but no "
2336 "VOP_SETACL()\n");
2337 /* panic("ufs_makeinode: VOP_GETACL() but no "
2338 "VOP_SETACL()"); */
2339 break;
2340
2341 default:
2342 FREE(acl, M_ACL);
2343 goto bad;
2344 }
2345 FREE(acl, M_ACL);
2346 }
2347 #endif /* !UFS_ACL */
2348 ufs_makedirentry(ip, cnp, &newdir);
2349 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2350 if (error)
2351 goto bad;
2352 *vpp = tvp;
2353 return (0);
2354
2355 bad:
2356 /*
2357 * Write error occurred trying to update the inode
2358 * or the directory so must deallocate the inode.
2359 */
2360 ip->i_effnlink = 0;
2361 ip->i_nlink = 0;
2362 DIP_SET(ip, i_nlink, 0);
2363 ip->i_flag |= IN_CHANGE;
2364 if (DOINGSOFTDEP(tvp))
2365 softdep_change_linkcnt(ip);
2366 vput(tvp);
2367 return (error);
2368 }
2369
2370 /* Global vfs data structures for ufs. */
2371 struct vop_vector ufs_vnodeops = {
2372 .vop_default = &default_vnodeops,
2373 .vop_fsync = VOP_PANIC,
2374 .vop_read = VOP_PANIC,
2375 .vop_reallocblks = VOP_PANIC,
2376 .vop_write = VOP_PANIC,
2377 .vop_access = ufs_access,
2378 .vop_advlock = ufs_advlock,
2379 .vop_bmap = ufs_bmap,
2380 .vop_cachedlookup = ufs_lookup,
2381 .vop_close = ufs_close,
2382 .vop_create = ufs_create,
2383 .vop_getattr = ufs_getattr,
2384 .vop_inactive = ufs_inactive,
2385 .vop_link = ufs_link,
2386 .vop_lookup = vfs_cache_lookup,
2387 .vop_mkdir = ufs_mkdir,
2388 .vop_mknod = ufs_mknod,
2389 .vop_open = ufs_open,
2390 .vop_pathconf = ufs_pathconf,
2391 .vop_poll = vop_stdpoll,
2392 .vop_print = ufs_print,
2393 .vop_readdir = ufs_readdir,
2394 .vop_readlink = ufs_readlink,
2395 .vop_reclaim = ufs_reclaim,
2396 .vop_remove = ufs_remove,
2397 .vop_rename = ufs_rename,
2398 .vop_rmdir = ufs_rmdir,
2399 .vop_setattr = ufs_setattr,
2400 #ifdef MAC
2401 .vop_setlabel = vop_stdsetlabel_ea,
2402 #endif
2403 .vop_strategy = ufs_strategy,
2404 .vop_symlink = ufs_symlink,
2405 .vop_whiteout = ufs_whiteout,
2406 #ifdef UFS_EXTATTR
2407 .vop_getextattr = ufs_getextattr,
2408 .vop_deleteextattr = ufs_deleteextattr,
2409 .vop_setextattr = ufs_setextattr,
2410 #endif
2411 #ifdef UFS_ACL
2412 .vop_getacl = ufs_getacl,
2413 .vop_setacl = ufs_setacl,
2414 .vop_aclcheck = ufs_aclcheck,
2415 #endif
2416 };
2417
2418 struct vop_vector ufs_fifoops = {
2419 .vop_default = &fifo_specops,
2420 .vop_fsync = VOP_PANIC,
2421 .vop_access = ufs_access,
2422 .vop_close = ufsfifo_close,
2423 .vop_getattr = ufs_getattr,
2424 .vop_inactive = ufs_inactive,
2425 .vop_kqfilter = ufsfifo_kqfilter,
2426 .vop_print = ufs_print,
2427 .vop_read = VOP_PANIC,
2428 .vop_reclaim = ufs_reclaim,
2429 .vop_setattr = ufs_setattr,
2430 #ifdef MAC
2431 .vop_setlabel = vop_stdsetlabel_ea,
2432 #endif
2433 .vop_write = VOP_PANIC,
2434 #ifdef UFS_EXTATTR
2435 .vop_getextattr = ufs_getextattr,
2436 .vop_deleteextattr = ufs_deleteextattr,
2437 .vop_setextattr = ufs_setextattr,
2438 #endif
2439 #ifdef UFS_ACL
2440 .vop_getacl = ufs_getacl,
2441 .vop_setacl = ufs_setacl,
2442 .vop_aclcheck = ufs_aclcheck,
2443 #endif
2444 };
Cache object: e746b93639754882092c5013ce00a326
|