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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
34 * @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 #include "opt_quota.h"
42 #include "opt_suiddir.h"
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>
55 #include <sys/mount.h>
57 #include <sys/refcount.h>
58 #include <sys/unistd.h>
59 #include <sys/vnode.h>
60 #include <sys/dirent.h>
61 #include <sys/lockf.h>
66 #include <machine/mutex.h>
68 #include <security/mac/mac_framework.h>
70 #include <sys/file.h> /* XXX */
73 #include <vm/vm_extern.h>
75 #include <fs/fifofs/fifo.h>
77 #include <ufs/ufs/acl.h>
78 #include <ufs/ufs/extattr.h>
79 #include <ufs/ufs/quota.h>
80 #include <ufs/ufs/inode.h>
81 #include <ufs/ufs/dir.h>
82 #include <ufs/ufs/ufsmount.h>
83 #include <ufs/ufs/ufs_extern.h>
85 #include <ufs/ufs/dirhash.h>
88 #include <ufs/ufs/gjournal.h>
91 #include <ufs/ffs/ffs_extern.h>
93 static vop_access_t ufs_access;
94 static vop_advlock_t ufs_advlock;
95 static int ufs_chmod(struct vnode *, int, struct ucred *, struct thread *);
96 static int ufs_chown(struct vnode *, uid_t, gid_t, struct ucred *, struct thread *);
97 static vop_close_t ufs_close;
98 static vop_create_t ufs_create;
99 static vop_getattr_t ufs_getattr;
100 static vop_link_t ufs_link;
101 static int ufs_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
102 static vop_mkdir_t ufs_mkdir;
103 static vop_mknod_t ufs_mknod;
104 static vop_open_t ufs_open;
105 static vop_pathconf_t ufs_pathconf;
106 static vop_print_t ufs_print;
107 static vop_readlink_t ufs_readlink;
108 static vop_remove_t ufs_remove;
109 static vop_rename_t ufs_rename;
110 static vop_rmdir_t ufs_rmdir;
111 static vop_setattr_t ufs_setattr;
112 static vop_strategy_t ufs_strategy;
113 static vop_symlink_t ufs_symlink;
114 static vop_whiteout_t ufs_whiteout;
115 static vop_close_t ufsfifo_close;
116 static vop_kqfilter_t ufsfifo_kqfilter;
119 * A virgin directory (no blushing please).
121 static struct dirtemplate mastertemplate = {
122 0, 12, DT_DIR, 1, ".",
123 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
125 static struct odirtemplate omastertemplate = {
127 0, DIRBLKSIZ - 12, 2, ".."
131 ufs_itimes_locked(struct vnode *vp)
136 ASSERT_VI_LOCKED(vp, __func__);
139 if ((vp->v_mount->mnt_flag & MNT_RDONLY) != 0)
141 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
144 if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
145 ip->i_flag |= IN_LAZYMOD;
146 else if (((vp->v_mount->mnt_kern_flag &
147 (MNTK_SUSPENDED | MNTK_SUSPEND)) == 0) ||
148 (ip->i_flag & (IN_CHANGE | IN_UPDATE)))
149 ip->i_flag |= IN_MODIFIED;
150 else if (ip->i_flag & IN_ACCESS)
151 ip->i_flag |= IN_LAZYACCESS;
153 if (ip->i_flag & IN_ACCESS) {
154 DIP_SET(ip, i_atime, ts.tv_sec);
155 DIP_SET(ip, i_atimensec, ts.tv_nsec);
157 if (ip->i_flag & IN_UPDATE) {
158 DIP_SET(ip, i_mtime, ts.tv_sec);
159 DIP_SET(ip, i_mtimensec, ts.tv_nsec);
162 if (ip->i_flag & IN_CHANGE) {
163 DIP_SET(ip, i_ctime, ts.tv_sec);
164 DIP_SET(ip, i_ctimensec, ts.tv_nsec);
168 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
172 ufs_itimes(struct vnode *vp)
176 ufs_itimes_locked(vp);
181 * Create a regular file
185 struct vop_create_args /* {
187 struct vnode **a_vpp;
188 struct componentname *a_cnp;
195 ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
196 ap->a_dvp, ap->a_vpp, ap->a_cnp);
208 struct vop_mknod_args /* {
210 struct vnode **a_vpp;
211 struct componentname *a_cnp;
215 struct vattr *vap = ap->a_vap;
216 struct vnode **vpp = ap->a_vpp;
221 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
222 ap->a_dvp, vpp, ap->a_cnp);
226 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
227 if (vap->va_rdev != VNOVAL) {
229 * Want to be able to use this to make badblock
230 * inodes, so don't truncate the dev number.
232 DIP_SET(ip, i_rdev, vap->va_rdev);
235 * Remove inode, then reload it through VFS_VGET so it is
236 * checked to see if it is an alias of an existing entry in
237 * the inode cache. XXX I don't believe this is necessary now.
239 (*vpp)->v_type = VNON;
240 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
243 error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
256 ufs_open(struct vop_open_args *ap)
258 struct vnode *vp = ap->a_vp;
261 if (vp->v_type == VCHR || vp->v_type == VBLK)
266 * Files marked append-only must be opened for appending.
268 if ((ip->i_flags & APPEND) &&
269 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
271 vnode_create_vobject(vp, DIP(ip, i_size), ap->a_td);
278 * Update the times on the inode.
283 struct vop_close_args /* {
286 struct ucred *a_cred;
290 struct vnode *vp = ap->a_vp;
294 usecount = vp->v_usecount;
296 ufs_itimes_locked(vp);
303 struct vop_access_args /* {
306 struct ucred *a_cred;
310 struct vnode *vp = ap->a_vp;
311 struct inode *ip = VTOI(vp);
312 mode_t mode = ap->a_mode;
319 * Disallow write attempts on read-only filesystems;
320 * unless the file is a socket, fifo, or a block or
321 * character device resident on the filesystem.
324 switch (vp->v_type) {
328 if (vp->v_mount->mnt_flag & MNT_RDONLY)
336 /* If immutable bit set, nobody gets to write it. */
337 if ((mode & VWRITE) && (ip->i_flags & (IMMUTABLE | SF_SNAPSHOT)))
341 if ((vp->v_mount->mnt_flag & MNT_ACLS) != 0) {
342 acl = uma_zalloc(acl_zone, M_WAITOK);
343 error = VOP_GETACL(vp, ACL_TYPE_ACCESS, acl, ap->a_cred,
347 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid,
348 ip->i_gid, ap->a_mode, ap->a_cred, NULL);
351 error = vaccess_acl_posix1e(vp->v_type, ip->i_uid,
352 ip->i_gid, acl, ap->a_mode, ap->a_cred, NULL);
356 "ufs_access(): Error retrieving ACL on object (%d).\n",
359 * XXX: Fall back until debugged. Should
360 * eventually possibly log an error, and return
363 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid,
364 ip->i_gid, ap->a_mode, ap->a_cred, NULL);
366 uma_zfree(acl_zone, acl);
368 #endif /* !UFS_ACL */
369 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
370 ap->a_mode, ap->a_cred, NULL);
377 struct vop_getattr_args /* {
380 struct ucred *a_cred;
384 struct vnode *vp = ap->a_vp;
385 struct inode *ip = VTOI(vp);
386 struct vattr *vap = ap->a_vap;
389 ufs_itimes_locked(vp);
390 if (ip->i_ump->um_fstype == UFS1) {
391 vap->va_atime.tv_sec = ip->i_din1->di_atime;
392 vap->va_atime.tv_nsec = ip->i_din1->di_atimensec;
394 vap->va_atime.tv_sec = ip->i_din2->di_atime;
395 vap->va_atime.tv_nsec = ip->i_din2->di_atimensec;
399 * Copy from inode table
401 vap->va_fsid = dev2udev(ip->i_dev);
402 vap->va_fileid = ip->i_number;
403 vap->va_mode = ip->i_mode & ~IFMT;
404 vap->va_nlink = ip->i_effnlink;
405 vap->va_uid = ip->i_uid;
406 vap->va_gid = ip->i_gid;
407 if (ip->i_ump->um_fstype == UFS1) {
408 vap->va_rdev = ip->i_din1->di_rdev;
409 vap->va_size = ip->i_din1->di_size;
410 vap->va_mtime.tv_sec = ip->i_din1->di_mtime;
411 vap->va_mtime.tv_nsec = ip->i_din1->di_mtimensec;
412 vap->va_ctime.tv_sec = ip->i_din1->di_ctime;
413 vap->va_ctime.tv_nsec = ip->i_din1->di_ctimensec;
414 vap->va_birthtime.tv_sec = 0;
415 vap->va_birthtime.tv_nsec = 0;
416 vap->va_bytes = dbtob((u_quad_t)ip->i_din1->di_blocks);
418 vap->va_rdev = ip->i_din2->di_rdev;
419 vap->va_size = ip->i_din2->di_size;
420 vap->va_mtime.tv_sec = ip->i_din2->di_mtime;
421 vap->va_mtime.tv_nsec = ip->i_din2->di_mtimensec;
422 vap->va_ctime.tv_sec = ip->i_din2->di_ctime;
423 vap->va_ctime.tv_nsec = ip->i_din2->di_ctimensec;
424 vap->va_birthtime.tv_sec = ip->i_din2->di_birthtime;
425 vap->va_birthtime.tv_nsec = ip->i_din2->di_birthnsec;
426 vap->va_bytes = dbtob((u_quad_t)ip->i_din2->di_blocks);
428 vap->va_flags = ip->i_flags;
429 vap->va_gen = ip->i_gen;
430 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
431 vap->va_type = IFTOVT(ip->i_mode);
432 vap->va_filerev = ip->i_modrev;
437 * Set attribute vnode op. called from several syscalls
441 struct vop_setattr_args /* {
444 struct ucred *a_cred;
448 struct vattr *vap = ap->a_vap;
449 struct vnode *vp = ap->a_vp;
450 struct inode *ip = VTOI(vp);
451 struct ucred *cred = ap->a_cred;
452 struct thread *td = ap->a_td;
456 * Check for unsettable attributes.
458 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
459 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
460 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
461 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
465 * Mark for update the file's access time for vfs_mark_atime().
466 * We are doing this here to avoid some of the checks done
467 * below -- this operation is done by request of the kernel and
468 * should bypass some security checks. Things like read-only
469 * checks get handled by other levels (e.g., ffs_update()).
471 if (vap->va_vaflags & VA_MARK_ATIME) {
472 ip->i_flag |= IN_ACCESS;
475 if (vap->va_flags != VNOVAL) {
476 if (vp->v_mount->mnt_flag & MNT_RDONLY)
479 * Callers may only modify the file flags on objects they
480 * have VADMIN rights for.
482 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
485 * Unprivileged processes are not permitted to unset system
486 * flags, or modify flags if any system flags are set.
487 * Privileged non-jail processes may not modify system flags
488 * if securelevel > 0 and any existing system flags are set.
489 * Privileged jail processes behave like privileged non-jail
490 * processes if the security.jail.chflags_allowed sysctl is
491 * is non-zero; otherwise, they behave like unprivileged
494 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
496 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
497 error = securelevel_gt(cred, 0);
501 /* Snapshot flag cannot be set or cleared */
502 if (((vap->va_flags & SF_SNAPSHOT) != 0 &&
503 (ip->i_flags & SF_SNAPSHOT) == 0) ||
504 ((vap->va_flags & SF_SNAPSHOT) == 0 &&
505 (ip->i_flags & SF_SNAPSHOT) != 0))
507 ip->i_flags = vap->va_flags;
508 DIP_SET(ip, i_flags, vap->va_flags);
511 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
512 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
514 ip->i_flags &= SF_SETTABLE;
515 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
516 DIP_SET(ip, i_flags, ip->i_flags);
518 ip->i_flag |= IN_CHANGE;
519 if (vap->va_flags & (IMMUTABLE | APPEND))
522 if (ip->i_flags & (IMMUTABLE | APPEND))
525 * Go through the fields and update iff not VNOVAL.
527 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
528 if (vp->v_mount->mnt_flag & MNT_RDONLY)
530 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred,
534 if (vap->va_size != VNOVAL) {
536 * XXX most of the following special cases should be in
537 * callers instead of in N filesystems. The VDIR check
540 switch (vp->v_type) {
546 * Truncation should have an effect in these cases.
547 * Disallow it if the filesystem is read-only or
548 * the file is being snapshotted.
550 if (vp->v_mount->mnt_flag & MNT_RDONLY)
552 if ((ip->i_flags & SF_SNAPSHOT) != 0)
557 * According to POSIX, the result is unspecified
558 * for file types other than regular files,
559 * directories and shared memory objects. We
560 * don't support shared memory objects in the file
561 * system, and have dubious support for truncating
562 * symlinks. Just ignore the request in other cases.
566 if ((error = UFS_TRUNCATE(vp, vap->va_size, IO_NORMAL,
570 if (vap->va_atime.tv_sec != VNOVAL ||
571 vap->va_mtime.tv_sec != VNOVAL ||
572 vap->va_birthtime.tv_sec != VNOVAL) {
573 if (vp->v_mount->mnt_flag & MNT_RDONLY)
575 if ((ip->i_flags & SF_SNAPSHOT) != 0)
579 * If times is NULL, ... The caller must be the owner of
580 * the file, have permission to write the file, or be the
582 * If times is non-NULL, ... The caller must be the owner of
583 * the file or be the super-user.
585 * Possibly for historical reasons, try to use VADMIN in
586 * preference to VWRITE for a NULL timestamp. This means we
587 * will return EACCES in preference to EPERM if neither
590 if (vap->va_vaflags & VA_UTIMES_NULL) {
591 error = VOP_ACCESS(vp, VADMIN, cred, td);
593 error = VOP_ACCESS(vp, VWRITE, cred, td);
595 error = VOP_ACCESS(vp, VADMIN, cred, td);
598 if (vap->va_atime.tv_sec != VNOVAL)
599 ip->i_flag |= IN_ACCESS;
600 if (vap->va_mtime.tv_sec != VNOVAL)
601 ip->i_flag |= IN_CHANGE | IN_UPDATE;
602 if (vap->va_birthtime.tv_sec != VNOVAL &&
603 ip->i_ump->um_fstype == UFS2)
604 ip->i_flag |= IN_MODIFIED;
606 if (vap->va_atime.tv_sec != VNOVAL) {
607 DIP_SET(ip, i_atime, vap->va_atime.tv_sec);
608 DIP_SET(ip, i_atimensec, vap->va_atime.tv_nsec);
610 if (vap->va_mtime.tv_sec != VNOVAL) {
611 DIP_SET(ip, i_mtime, vap->va_mtime.tv_sec);
612 DIP_SET(ip, i_mtimensec, vap->va_mtime.tv_nsec);
614 if (vap->va_birthtime.tv_sec != VNOVAL &&
615 ip->i_ump->um_fstype == UFS2) {
616 ip->i_din2->di_birthtime = vap->va_birthtime.tv_sec;
617 ip->i_din2->di_birthnsec = vap->va_birthtime.tv_nsec;
619 error = UFS_UPDATE(vp, 0);
624 if (vap->va_mode != (mode_t)VNOVAL) {
625 if (vp->v_mount->mnt_flag & MNT_RDONLY)
627 if ((ip->i_flags & SF_SNAPSHOT) != 0 && (vap->va_mode &
628 (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP | S_IXOTH | S_IWOTH)))
630 error = ufs_chmod(vp, (int)vap->va_mode, cred, td);
636 * Change the mode on a file.
637 * Inode must be locked before calling.
640 ufs_chmod(vp, mode, cred, td)
646 struct inode *ip = VTOI(vp);
650 * To modify the permissions on a file, must possess VADMIN
653 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
656 * Privileged processes may set the sticky bit on non-directories,
657 * as well as set the setgid bit on a file with a group that the
658 * process is not a member of. Both of these are allowed in
661 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
662 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0))
665 if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
666 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
670 ip->i_mode &= ~ALLPERMS;
671 ip->i_mode |= (mode & ALLPERMS);
672 DIP_SET(ip, i_mode, ip->i_mode);
673 ip->i_flag |= IN_CHANGE;
678 * Perform chown operation on inode ip;
679 * inode must be locked prior to call.
682 ufs_chown(vp, uid, gid, cred, td)
689 struct inode *ip = VTOI(vp);
698 if (uid == (uid_t)VNOVAL)
700 if (gid == (gid_t)VNOVAL)
703 * To modify the ownership of a file, must possess VADMIN for that
706 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
709 * To change the owner of a file, or change the group of a file to a
710 * group of which we are not a member, the caller must have
713 if ((uid != ip->i_uid ||
714 (gid != ip->i_gid && !groupmember(gid, cred))) &&
715 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
720 if ((error = getinoquota(ip)) != 0)
723 dqrele(vp, ip->i_dquot[USRQUOTA]);
724 ip->i_dquot[USRQUOTA] = NODQUOT;
727 dqrele(vp, ip->i_dquot[GRPQUOTA]);
728 ip->i_dquot[GRPQUOTA] = NODQUOT;
730 change = DIP(ip, i_blocks);
731 (void) chkdq(ip, -change, cred, CHOWN);
732 (void) chkiq(ip, -1, cred, CHOWN);
733 for (i = 0; i < MAXQUOTAS; i++) {
734 dqrele(vp, ip->i_dquot[i]);
735 ip->i_dquot[i] = NODQUOT;
739 DIP_SET(ip, i_gid, gid);
741 DIP_SET(ip, i_uid, uid);
743 if ((error = getinoquota(ip)) == 0) {
745 dqrele(vp, ip->i_dquot[USRQUOTA]);
746 ip->i_dquot[USRQUOTA] = NODQUOT;
749 dqrele(vp, ip->i_dquot[GRPQUOTA]);
750 ip->i_dquot[GRPQUOTA] = NODQUOT;
752 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
753 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
756 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
758 for (i = 0; i < MAXQUOTAS; i++) {
759 dqrele(vp, ip->i_dquot[i]);
760 ip->i_dquot[i] = NODQUOT;
764 DIP_SET(ip, i_gid, ogid);
766 DIP_SET(ip, i_uid, ouid);
767 if (getinoquota(ip) == 0) {
769 dqrele(vp, ip->i_dquot[USRQUOTA]);
770 ip->i_dquot[USRQUOTA] = NODQUOT;
773 dqrele(vp, ip->i_dquot[GRPQUOTA]);
774 ip->i_dquot[GRPQUOTA] = NODQUOT;
776 (void) chkdq(ip, change, cred, FORCE|CHOWN);
777 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
778 (void) getinoquota(ip);
783 panic("ufs_chown: lost quota");
785 ip->i_flag |= IN_CHANGE;
786 if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
787 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0)) {
788 ip->i_mode &= ~(ISUID | ISGID);
789 DIP_SET(ip, i_mode, ip->i_mode);
797 struct vop_remove_args /* {
800 struct componentname *a_cnp;
804 struct vnode *vp = ap->a_vp;
805 struct vnode *dvp = ap->a_dvp;
811 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
812 (VTOI(dvp)->i_flags & APPEND)) {
817 ufs_gjournal_orphan(vp);
819 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
820 if (ip->i_nlink <= 0)
821 vp->v_vflag |= VV_NOSYNC;
822 if ((ip->i_flags & SF_SNAPSHOT) != 0) {
824 * Avoid deadlock where another thread is trying to
825 * update the inodeblock for dvp and is waiting on
826 * snaplk. Temporary unlock the vnode lock for the
827 * unlinked file and sync the directory. This should
828 * allow vput() of the directory to not block later on
829 * while holding the snapshot vnode locked, assuming
830 * that the directory hasn't been unlinked too.
832 VOP_UNLOCK(vp, 0, td);
833 (void) VOP_FSYNC(dvp, MNT_WAIT, td);
834 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
845 struct vop_link_args /* {
846 struct vnode *a_tdvp;
848 struct componentname *a_cnp;
851 struct vnode *vp = ap->a_vp;
852 struct vnode *tdvp = ap->a_tdvp;
853 struct componentname *cnp = ap->a_cnp;
855 struct direct newdir;
859 if ((cnp->cn_flags & HASBUF) == 0)
860 panic("ufs_link: no name");
862 if (tdvp->v_mount != vp->v_mount) {
867 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
871 if (ip->i_flags & (IMMUTABLE | APPEND)) {
877 DIP_SET(ip, i_nlink, ip->i_nlink);
878 ip->i_flag |= IN_CHANGE;
879 if (DOINGSOFTDEP(vp))
880 softdep_change_linkcnt(ip);
881 error = UFS_UPDATE(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
883 ufs_makedirentry(ip, cnp, &newdir);
884 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
890 DIP_SET(ip, i_nlink, ip->i_nlink);
891 ip->i_flag |= IN_CHANGE;
892 if (DOINGSOFTDEP(vp))
893 softdep_change_linkcnt(ip);
900 * whiteout vnode call
904 struct vop_whiteout_args /* {
906 struct componentname *a_cnp;
910 struct vnode *dvp = ap->a_dvp;
911 struct componentname *cnp = ap->a_cnp;
912 struct direct newdir;
915 switch (ap->a_flags) {
917 /* 4.4 format directories support whiteout operations */
918 if (dvp->v_mount->mnt_maxsymlinklen > 0)
923 /* create a new directory whiteout */
925 if ((cnp->cn_flags & SAVENAME) == 0)
926 panic("ufs_whiteout: missing name");
927 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
928 panic("ufs_whiteout: old format filesystem");
932 newdir.d_namlen = cnp->cn_namelen;
933 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
934 newdir.d_type = DT_WHT;
935 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
939 /* remove an existing directory whiteout */
941 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
942 panic("ufs_whiteout: old format filesystem");
945 cnp->cn_flags &= ~DOWHITEOUT;
946 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
949 panic("ufs_whiteout: unknown op");
955 * Rename system call.
956 * rename("foo", "bar");
959 * link("foo", "bar");
961 * but ``atomically''. Can't do full commit without saving state in the
962 * inode on disk which isn't feasible at this time. Best we can do is
963 * always guarantee the target exists.
965 * Basic algorithm is:
967 * 1) Bump link count on source while we're linking it to the
968 * target. This also ensure the inode won't be deleted out
969 * from underneath us while we work (it may be truncated by
970 * a concurrent `trunc' or `open' for creation).
971 * 2) Link source to destination. If destination already exists,
973 * 3) Unlink source reference to inode if still around. If a
974 * directory was moved and the parent of the destination
975 * is different from the source, patch the ".." entry in the
980 struct vop_rename_args /* {
981 struct vnode *a_fdvp;
983 struct componentname *a_fcnp;
984 struct vnode *a_tdvp;
986 struct componentname *a_tcnp;
989 struct vnode *tvp = ap->a_tvp;
990 struct vnode *tdvp = ap->a_tdvp;
991 struct vnode *fvp = ap->a_fvp;
992 struct vnode *fdvp = ap->a_fdvp;
993 struct componentname *tcnp = ap->a_tcnp;
994 struct componentname *fcnp = ap->a_fcnp;
995 struct thread *td = fcnp->cn_thread;
996 struct inode *ip, *xp, *dp;
997 struct direct newdir;
998 int doingdirectory = 0, oldparent = 0, newparent = 0;
999 int error = 0, ioflag;
1002 if ((tcnp->cn_flags & HASBUF) == 0 ||
1003 (fcnp->cn_flags & HASBUF) == 0)
1004 panic("ufs_rename: no name");
1007 * Check for cross-device rename.
1009 if ((fvp->v_mount != tdvp->v_mount) ||
1010 (tvp && (fvp->v_mount != tvp->v_mount))) {
1024 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1025 (VTOI(tdvp)->i_flags & APPEND))) {
1031 * Renaming a file to itself has no effect. The upper layers should
1032 * not call us in that case. Temporarily just warn if they do.
1035 printf("ufs_rename: fvp == tvp (can't happen)\n");
1040 if ((error = vn_lock(fvp, LK_EXCLUSIVE, td)) != 0)
1044 if (ip->i_nlink >= LINK_MAX) {
1045 VOP_UNLOCK(fvp, 0, td);
1049 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
1050 || (dp->i_flags & APPEND)) {
1051 VOP_UNLOCK(fvp, 0, td);
1055 if ((ip->i_mode & IFMT) == IFDIR) {
1057 * Avoid ".", "..", and aliases of "." for obvious reasons.
1059 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1060 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
1061 (ip->i_flag & IN_RENAME)) {
1062 VOP_UNLOCK(fvp, 0, td);
1066 ip->i_flag |= IN_RENAME;
1067 oldparent = dp->i_number;
1073 * When the target exists, both the directory
1074 * and target vnodes are returned locked.
1082 * 1) Bump link count while we're moving stuff
1083 * around. If we crash somewhere before
1084 * completing our work, the link count
1085 * may be wrong, but correctable.
1089 DIP_SET(ip, i_nlink, ip->i_nlink);
1090 ip->i_flag |= IN_CHANGE;
1091 if (DOINGSOFTDEP(fvp))
1092 softdep_change_linkcnt(ip);
1093 if ((error = UFS_UPDATE(fvp, !(DOINGSOFTDEP(fvp) |
1094 DOINGASYNC(fvp)))) != 0) {
1095 VOP_UNLOCK(fvp, 0, td);
1100 * If ".." must be changed (ie the directory gets a new
1101 * parent) then the source directory must not be in the
1102 * directory hierarchy above the target, as this would
1103 * orphan everything below the source directory. Also
1104 * the user must have write permission in the source so
1105 * as to be able to change "..". We must repeat the call
1106 * to namei, as the parent directory is unlocked by the
1107 * call to checkpath().
1109 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
1110 VOP_UNLOCK(fvp, 0, td);
1111 if (oldparent != dp->i_number)
1112 newparent = dp->i_number;
1113 if (doingdirectory && newparent) {
1114 if (error) /* write access check above */
1118 error = ufs_checkpath(ip, dp, tcnp->cn_cred);
1121 if ((tcnp->cn_flags & SAVESTART) == 0)
1122 panic("ufs_rename: lost to startdir");
1124 error = relookup(tdvp, &tvp, tcnp);
1134 * 2) If target doesn't exist, link the target
1135 * to the source and unlink the source.
1136 * Otherwise, rewrite the target directory
1137 * entry to reference the source inode and
1138 * expunge the original entry's existence.
1141 if (dp->i_dev != ip->i_dev)
1142 panic("ufs_rename: EXDEV");
1144 * Account for ".." in new directory.
1145 * When source and destination have the same
1146 * parent we don't fool with the link count.
1148 if (doingdirectory && newparent) {
1149 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1155 DIP_SET(dp, i_nlink, dp->i_nlink);
1156 dp->i_flag |= IN_CHANGE;
1157 if (DOINGSOFTDEP(tdvp))
1158 softdep_change_linkcnt(dp);
1159 error = UFS_UPDATE(tdvp, !(DOINGSOFTDEP(tdvp) |
1164 ufs_makedirentry(ip, tcnp, &newdir);
1165 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1167 if (doingdirectory && newparent) {
1170 DIP_SET(dp, i_nlink, dp->i_nlink);
1171 dp->i_flag |= IN_CHANGE;
1172 if (DOINGSOFTDEP(tdvp))
1173 softdep_change_linkcnt(dp);
1174 (void)UFS_UPDATE(tdvp, 1);
1180 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1181 panic("ufs_rename: EXDEV");
1183 * Short circuit rename(foo, foo).
1185 if (xp->i_number == ip->i_number)
1186 panic("ufs_rename: same file");
1188 * If the parent directory is "sticky", then the caller
1189 * must possess VADMIN for the parent directory, or the
1190 * destination of the rename. This implements append-only
1193 if ((dp->i_mode & S_ISTXT) &&
1194 VOP_ACCESS(tdvp, VADMIN, tcnp->cn_cred, td) &&
1195 VOP_ACCESS(tvp, VADMIN, tcnp->cn_cred, td)) {
1200 * Target must be empty if a directory and have no links
1201 * to it. Also, ensure source and target are compatible
1202 * (both directories, or both not directories).
1204 if ((xp->i_mode&IFMT) == IFDIR) {
1205 if ((xp->i_effnlink > 2) ||
1206 !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1210 if (!doingdirectory) {
1215 } else if (doingdirectory) {
1219 error = ufs_dirrewrite(dp, xp, ip->i_number,
1221 (doingdirectory && newparent) ? newparent : doingdirectory);
1224 if (doingdirectory) {
1227 if (DOINGSOFTDEP(tdvp))
1228 softdep_change_linkcnt(dp);
1231 if (DOINGSOFTDEP(tvp))
1232 softdep_change_linkcnt(xp);
1234 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1236 * Truncate inode. The only stuff left in the directory
1237 * is "." and "..". The "." reference is inconsequential
1238 * since we are quashing it. We have removed the "."
1239 * reference and the reference in the parent directory,
1240 * but there may be other hard links. The soft
1241 * dependency code will arrange to do these operations
1242 * after the parent directory entry has been deleted on
1243 * disk, so when running with that code we avoid doing
1248 DIP_SET(dp, i_nlink, dp->i_nlink);
1249 dp->i_flag |= IN_CHANGE;
1252 DIP_SET(xp, i_nlink, xp->i_nlink);
1253 xp->i_flag |= IN_CHANGE;
1255 if (!DOINGASYNC(tvp))
1257 if ((error = UFS_TRUNCATE(tvp, (off_t)0, ioflag,
1258 tcnp->cn_cred, tcnp->cn_thread)) != 0)
1267 * 3) Unlink the source.
1269 fcnp->cn_flags &= ~MODMASK;
1270 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1271 if ((fcnp->cn_flags & SAVESTART) == 0)
1272 panic("ufs_rename: lost from startdir");
1274 error = relookup(fdvp, &fvp, fcnp);
1282 * From name has disappeared. IN_RENAME is not sufficient
1283 * to protect against directory races due to timing windows,
1284 * so we have to remove the panic. XXX the only real way
1285 * to solve this issue is at a much higher level. By the
1286 * time we hit ufs_rename() it's too late.
1290 panic("ufs_rename: lost dir entry");
1296 * Ensure that the directory entry still exists and has not
1297 * changed while the new name has been entered. If the source is
1298 * a file then the entry may have been unlinked or renamed. In
1299 * either case there is no further work to be done. If the source
1300 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1301 * flag ensures that it cannot be moved by another rename or removed
1306 * From name resolves to a different inode. IN_RENAME is
1307 * not sufficient protection against timing window races
1308 * so we can't panic here. XXX the only real way
1309 * to solve this issue is at a much higher level. By the
1310 * time we hit ufs_rename() it's too late.
1314 panic("ufs_rename: lost dir entry");
1318 * If the source is a directory with a
1319 * new parent, the link count of the old
1320 * parent directory must be decremented
1321 * and ".." set to point to the new parent.
1323 if (doingdirectory && newparent) {
1324 xp->i_offset = mastertemplate.dot_reclen;
1325 ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1328 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1329 xp->i_flag &= ~IN_RENAME;
1344 ip->i_flag &= ~IN_RENAME;
1345 if (vn_lock(fvp, LK_EXCLUSIVE, td) == 0) {
1348 DIP_SET(ip, i_nlink, ip->i_nlink);
1349 ip->i_flag |= IN_CHANGE;
1350 ip->i_flag &= ~IN_RENAME;
1351 if (DOINGSOFTDEP(fvp))
1352 softdep_change_linkcnt(ip);
1364 struct vop_mkdir_args /* {
1365 struct vnode *a_dvp;
1366 struct vnode **a_vpp;
1367 struct componentname *a_cnp;
1368 struct vattr *a_vap;
1371 struct vnode *dvp = ap->a_dvp;
1372 struct vattr *vap = ap->a_vap;
1373 struct componentname *cnp = ap->a_cnp;
1374 struct inode *ip, *dp;
1377 struct dirtemplate dirtemplate, *dtp;
1378 struct direct newdir;
1380 struct acl *acl, *dacl;
1386 if ((cnp->cn_flags & HASBUF) == 0)
1387 panic("ufs_mkdir: no name");
1390 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1394 dmode = vap->va_mode & 0777;
1397 * Must simulate part of ufs_makeinode here to acquire the inode,
1398 * but not have it entered in the parent directory. The entry is
1399 * made later after writing "." and ".." entries.
1401 error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
1405 ip->i_gid = dp->i_gid;
1406 DIP_SET(ip, i_gid, dp->i_gid);
1410 struct ucred ucred, *ucp;
1414 * If we are hacking owners here, (only do this where told to)
1415 * and we are not giving it TO root, (would subvert quotas)
1416 * then go ahead and give it to the other user.
1417 * The new directory also inherits the SUID bit.
1418 * If user's UID and dir UID are the same,
1419 * 'give it away' so that the SUID is still forced on.
1421 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1422 (dp->i_mode & ISUID) && dp->i_uid) {
1424 ip->i_uid = dp->i_uid;
1425 DIP_SET(ip, i_uid, dp->i_uid);
1427 if (dp->i_uid != cnp->cn_cred->cr_uid) {
1429 * Make sure the correct user gets charged
1431 * Make a dummy credential for the victim.
1432 * XXX This seems to never be accessed out of
1433 * our context so a stack variable is ok.
1435 refcount_init(&ucred.cr_ref, 1);
1436 ucred.cr_uid = ip->i_uid;
1437 ucred.cr_ngroups = 1;
1438 ucred.cr_groups[0] = dp->i_gid;
1443 ip->i_uid = cnp->cn_cred->cr_uid;
1444 DIP_SET(ip, i_uid, ip->i_uid);
1447 if ((error = getinoquota(ip)) ||
1448 (error = chkiq(ip, 1, ucp, 0))) {
1449 UFS_VFREE(tvp, ip->i_number, dmode);
1455 #else /* !SUIDDIR */
1456 ip->i_uid = cnp->cn_cred->cr_uid;
1457 DIP_SET(ip, i_uid, ip->i_uid);
1459 if ((error = getinoquota(ip)) ||
1460 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1461 UFS_VFREE(tvp, ip->i_number, dmode);
1466 #endif /* !SUIDDIR */
1467 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1470 if ((dvp->v_mount->mnt_flag & MNT_ACLS) != 0) {
1471 acl = uma_zalloc(acl_zone, M_WAITOK);
1472 dacl = uma_zalloc(acl_zone, M_WAITOK);
1475 * Retrieve default ACL from parent, if any.
1477 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cnp->cn_cred,
1482 * Retrieved a default ACL, so merge mode and ACL if
1483 * necessary. If the ACL is empty, fall through to
1484 * the "not defined or available" case.
1486 if (acl->acl_cnt != 0) {
1487 dmode = acl_posix1e_newfilemode(dmode, acl);
1489 DIP_SET(ip, i_mode, dmode);
1491 ufs_sync_acl_from_inode(ip, acl);
1498 * Just use the mode as-is.
1501 DIP_SET(ip, i_mode, dmode);
1502 uma_zfree(acl_zone, acl);
1503 uma_zfree(acl_zone, dacl);
1508 UFS_VFREE(tvp, ip->i_number, dmode);
1510 uma_zfree(acl_zone, acl);
1511 uma_zfree(acl_zone, dacl);
1515 #endif /* !UFS_ACL */
1517 DIP_SET(ip, i_mode, dmode);
1521 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1524 DIP_SET(ip, i_nlink, 2);
1525 if (DOINGSOFTDEP(tvp))
1526 softdep_change_linkcnt(ip);
1527 if (cnp->cn_flags & ISWHITEOUT) {
1528 ip->i_flags |= UF_OPAQUE;
1529 DIP_SET(ip, i_flags, ip->i_flags);
1533 * Bump link count in parent directory to reflect work done below.
1534 * Should be done before reference is created so cleanup is
1535 * possible if we crash.
1539 DIP_SET(dp, i_nlink, dp->i_nlink);
1540 dp->i_flag |= IN_CHANGE;
1541 if (DOINGSOFTDEP(dvp))
1542 softdep_change_linkcnt(dp);
1543 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1547 if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
1548 error = mac_vnode_create_extattr(cnp->cn_cred, dvp->v_mount,
1557 * XXX: If we abort now, will Soft Updates notify the extattr
1558 * code that the EAs for the file need to be released?
1560 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cnp->cn_cred,
1563 error = VOP_SETACL(tvp, ACL_TYPE_DEFAULT, dacl,
1564 cnp->cn_cred, cnp->cn_thread);
1571 * XXX: This should not happen, as EOPNOTSUPP above
1572 * was supposed to free acl.
1574 printf("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()\n");
1576 panic("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()");
1581 uma_zfree(acl_zone, acl);
1582 uma_zfree(acl_zone, dacl);
1586 uma_zfree(acl_zone, acl);
1587 uma_zfree(acl_zone, dacl);
1590 #endif /* !UFS_ACL */
1593 * Initialize directory with "." and ".." from static template.
1595 if (dvp->v_mount->mnt_maxsymlinklen > 0)
1596 dtp = &mastertemplate;
1598 dtp = (struct dirtemplate *)&omastertemplate;
1600 dirtemplate.dot_ino = ip->i_number;
1601 dirtemplate.dotdot_ino = dp->i_number;
1602 if ((error = UFS_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred,
1603 BA_CLRBUF, &bp)) != 0)
1605 ip->i_size = DIRBLKSIZ;
1606 DIP_SET(ip, i_size, DIRBLKSIZ);
1607 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1608 vnode_pager_setsize(tvp, (u_long)ip->i_size);
1609 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1610 if (DOINGSOFTDEP(tvp)) {
1612 * Ensure that the entire newly allocated block is a
1613 * valid directory so that future growth within the
1614 * block does not have to ensure that the block is
1615 * written before the inode.
1618 while (blkoff < bp->b_bcount) {
1620 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1621 blkoff += DIRBLKSIZ;
1624 if ((error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) |
1625 DOINGASYNC(tvp)))) != 0) {
1630 * Directory set up, now install its entry in the parent directory.
1632 * If we are not doing soft dependencies, then we must write out the
1633 * buffer containing the new directory body before entering the new
1634 * name in the parent. If we are doing soft dependencies, then the
1635 * buffer containing the new directory body will be passed to and
1636 * released in the soft dependency code after the code has attached
1637 * an appropriate ordering dependency to the buffer which ensures that
1638 * the buffer is written before the new name is written in the parent.
1640 if (DOINGASYNC(dvp))
1642 else if (!DOINGSOFTDEP(dvp) && ((error = bwrite(bp))))
1644 ufs_makedirentry(ip, cnp, &newdir);
1645 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1653 uma_zfree(acl_zone, acl);
1655 uma_zfree(acl_zone, dacl);
1659 DIP_SET(dp, i_nlink, dp->i_nlink);
1660 dp->i_flag |= IN_CHANGE;
1661 if (DOINGSOFTDEP(dvp))
1662 softdep_change_linkcnt(dp);
1664 * No need to do an explicit VOP_TRUNCATE here, vrele will
1665 * do this for us because we set the link count to 0.
1669 DIP_SET(ip, i_nlink, 0);
1670 ip->i_flag |= IN_CHANGE;
1671 if (DOINGSOFTDEP(tvp))
1672 softdep_change_linkcnt(ip);
1680 * Rmdir system call.
1684 struct vop_rmdir_args /* {
1685 struct vnode *a_dvp;
1687 struct componentname *a_cnp;
1690 struct vnode *vp = ap->a_vp;
1691 struct vnode *dvp = ap->a_dvp;
1692 struct componentname *cnp = ap->a_cnp;
1693 struct inode *ip, *dp;
1700 * Do not remove a directory that is in the process of being renamed.
1701 * Verify the directory is empty (and valid). Rmdir ".." will not be
1702 * valid since ".." will contain a reference to the current directory
1703 * and thus be non-empty. Do not allow the removal of mounted on
1704 * directories (this can happen when an NFS exported filesystem
1705 * tries to remove a locally mounted on directory).
1708 if ((ip->i_flag & IN_RENAME) || ip->i_effnlink < 2) {
1712 if (!ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1716 if ((dp->i_flags & APPEND)
1717 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1721 if (vp->v_mountedhere != 0) {
1726 ufs_gjournal_orphan(vp);
1729 * Delete reference to directory before purging
1730 * inode. If we crash in between, the directory
1731 * will be reattached to lost+found,
1735 if (DOINGSOFTDEP(vp)) {
1736 softdep_change_linkcnt(dp);
1737 softdep_change_linkcnt(ip);
1739 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1743 if (DOINGSOFTDEP(vp)) {
1744 softdep_change_linkcnt(dp);
1745 softdep_change_linkcnt(ip);
1751 * Truncate inode. The only stuff left in the directory is "." and
1752 * "..". The "." reference is inconsequential since we are quashing
1753 * it. The soft dependency code will arrange to do these operations
1754 * after the parent directory entry has been deleted on disk, so
1755 * when running with that code we avoid doing them now.
1757 if (!DOINGSOFTDEP(vp)) {
1759 DIP_SET(dp, i_nlink, dp->i_nlink);
1760 dp->i_flag |= IN_CHANGE;
1762 DIP_SET(ip, i_nlink, ip->i_nlink);
1763 ip->i_flag |= IN_CHANGE;
1765 if (!DOINGASYNC(vp))
1767 error = UFS_TRUNCATE(vp, (off_t)0, ioflag, cnp->cn_cred,
1772 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1773 if (ip->i_dirhash != NULL)
1774 ufsdirhash_free(ip);
1781 * symlink -- make a symbolic link
1785 struct vop_symlink_args /* {
1786 struct vnode *a_dvp;
1787 struct vnode **a_vpp;
1788 struct componentname *a_cnp;
1789 struct vattr *a_vap;
1793 struct vnode *vp, **vpp = ap->a_vpp;
1797 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1802 len = strlen(ap->a_target);
1803 if (len < vp->v_mount->mnt_maxsymlinklen) {
1805 bcopy(ap->a_target, SHORTLINK(ip), len);
1807 DIP_SET(ip, i_size, len);
1808 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1810 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1811 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1812 ap->a_cnp->cn_cred, NOCRED, (int *)0, (struct thread *)0);
1819 * Vnode op for reading directories.
1821 * The routine below assumes that the on-disk format of a directory
1822 * is the same as that defined by <sys/dirent.h>. If the on-disk
1823 * format changes, then it will be necessary to do a conversion
1824 * from the on-disk format that read returns to the format defined
1825 * by <sys/dirent.h>.
1829 struct vop_readdir_args /* {
1832 struct ucred *a_cred;
1838 struct uio *uio = ap->a_uio;
1843 if (ap->a_ncookies != NULL)
1845 * Ensure that the block is aligned. The caller can use
1846 * the cookies to determine where in the block to start.
1848 uio->uio_offset &= ~(DIRBLKSIZ - 1);
1849 off = uio->uio_offset;
1850 count = uio->uio_resid;
1851 /* Make sure we don't return partial entries. */
1852 if (count <= ((uio->uio_offset + count) & (DIRBLKSIZ -1)))
1854 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
1855 lost = uio->uio_resid - count;
1856 uio->uio_resid = count;
1857 uio->uio_iov->iov_len = count;
1858 # if (BYTE_ORDER == LITTLE_ENDIAN)
1859 if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) {
1860 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1862 struct dirent *dp, *edp;
1870 auio.uio_iov = &aiov;
1871 auio.uio_iovcnt = 1;
1872 auio.uio_segflg = UIO_SYSSPACE;
1873 aiov.iov_len = count;
1874 MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
1875 aiov.iov_base = dirbuf;
1876 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
1878 readcnt = count - auio.uio_resid;
1879 edp = (struct dirent *)&dirbuf[readcnt];
1880 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
1882 dp->d_namlen = dp->d_type;
1884 if (dp->d_reclen > 0) {
1885 dp = (struct dirent *)
1886 ((char *)dp + dp->d_reclen);
1893 error = uiomove(dirbuf, readcnt, uio);
1895 FREE(dirbuf, M_TEMP);
1898 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1900 if (!error && ap->a_ncookies != NULL) {
1901 struct dirent* dpStart;
1902 struct dirent* dpEnd;
1908 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1909 panic("ufs_readdir: unexpected uio from NFS server");
1910 dpStart = (struct dirent *)
1911 ((char *)uio->uio_iov->iov_base - (uio->uio_offset - off));
1912 dpEnd = (struct dirent *) uio->uio_iov->iov_base;
1913 for (dp = dpStart, ncookies = 0;
1915 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen))
1917 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1919 for (dp = dpStart, cookiep = cookies;
1921 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
1922 off += dp->d_reclen;
1923 *cookiep++ = (u_long) off;
1925 *ap->a_ncookies = ncookies;
1926 *ap->a_cookies = cookies;
1928 uio->uio_resid += lost;
1930 *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
1935 * Return target name of a symbolic link
1939 struct vop_readlink_args /* {
1942 struct ucred *a_cred;
1945 struct vnode *vp = ap->a_vp;
1946 struct inode *ip = VTOI(vp);
1950 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1951 DIP(ip, i_blocks) == 0) { /* XXX - for old fastlink support */
1952 return (uiomove(SHORTLINK(ip), isize, ap->a_uio));
1954 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1958 * Calculate the logical to physical mapping if not done already,
1959 * then call the device strategy routine.
1961 * In order to be able to swap to a file, the ufs_bmaparray() operation may not
1962 * deadlock on memory. See ufs_bmap() for details.
1966 struct vop_strategy_args /* {
1971 struct buf *bp = ap->a_bp;
1972 struct vnode *vp = ap->a_vp;
1979 if (bp->b_blkno == bp->b_lblkno) {
1980 error = ufs_bmaparray(vp, bp->b_lblkno, &blkno, bp, NULL, NULL);
1981 bp->b_blkno = blkno;
1983 bp->b_error = error;
1984 bp->b_ioflags |= BIO_ERROR;
1988 if ((long)bp->b_blkno == -1)
1991 if ((long)bp->b_blkno == -1) {
1995 bp->b_iooffset = dbtob(bp->b_blkno);
1996 bo = ip->i_umbufobj;
1997 BO_STRATEGY(bo, bp);
2002 * Print out the contents of an inode.
2006 struct vop_print_args /* {
2010 struct vnode *vp = ap->a_vp;
2011 struct inode *ip = VTOI(vp);
2013 printf("\tino %lu, on dev %s", (u_long)ip->i_number,
2014 devtoname(ip->i_dev));
2015 if (vp->v_type == VFIFO)
2022 * Close wrapper for fifos.
2024 * Update the times on the inode then do device close.
2028 struct vop_close_args /* {
2031 struct ucred *a_cred;
2032 struct thread *a_td;
2035 struct vnode *vp = ap->a_vp;
2039 usecount = vp->v_usecount;
2041 ufs_itimes_locked(vp);
2043 return (fifo_specops.vop_close(ap));
2047 * Kqfilter wrapper for fifos.
2049 * Fall through to ufs kqfilter routines if needed
2052 ufsfifo_kqfilter(ap)
2053 struct vop_kqfilter_args *ap;
2057 error = fifo_specops.vop_kqfilter(ap);
2059 error = vfs_kqfilter(ap);
2064 * Return POSIX pathconf information applicable to ufs filesystems.
2068 struct vop_pathconf_args /* {
2077 switch (ap->a_name) {
2079 *ap->a_retval = LINK_MAX;
2082 *ap->a_retval = NAME_MAX;
2085 *ap->a_retval = PATH_MAX;
2088 *ap->a_retval = PIPE_BUF;
2090 case _PC_CHOWN_RESTRICTED:
2096 case _PC_ACL_EXTENDED:
2098 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
2106 case _PC_ACL_PATH_MAX:
2108 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
2109 *ap->a_retval = ACL_MAX_ENTRIES;
2116 case _PC_MAC_PRESENT:
2118 if (ap->a_vp->v_mount->mnt_flag & MNT_MULTILABEL)
2127 /* _PC_ASYNC_IO should have been handled by upper layers. */
2128 KASSERT(0, ("_PC_ASYNC_IO should not get here"));
2137 case _PC_ALLOC_SIZE_MIN:
2138 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
2140 case _PC_FILESIZEBITS:
2143 case _PC_REC_INCR_XFER_SIZE:
2144 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2146 case _PC_REC_MAX_XFER_SIZE:
2147 *ap->a_retval = -1; /* means ``unlimited'' */
2149 case _PC_REC_MIN_XFER_SIZE:
2150 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2152 case _PC_REC_XFER_ALIGN:
2153 *ap->a_retval = PAGE_SIZE;
2155 case _PC_SYMLINK_MAX:
2156 *ap->a_retval = MAXPATHLEN;
2167 * Advisory record locking support
2171 struct vop_advlock_args /* {
2179 struct inode *ip = VTOI(ap->a_vp);
2181 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
2185 * Initialize the vnode associated with a new inode, handle aliased
2189 ufs_vinit(mntp, fifoops, vpp)
2191 struct vop_vector *fifoops;
2199 vp->v_type = IFTOVT(ip->i_mode);
2200 if (vp->v_type == VFIFO)
2202 ASSERT_VOP_LOCKED(vp, "ufs_vinit");
2203 if (ip->i_number == ROOTINO)
2204 vp->v_vflag |= VV_ROOT;
2205 ip->i_modrev = init_va_filerev();
2211 * Allocate a new inode.
2212 * Vnode dvp must be locked.
2215 ufs_makeinode(mode, dvp, vpp, cnp)
2219 struct componentname *cnp;
2221 struct inode *ip, *pdir;
2222 struct direct newdir;
2231 if ((cnp->cn_flags & HASBUF) == 0)
2232 panic("ufs_makeinode: no name");
2235 if ((mode & IFMT) == 0)
2238 error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2242 ip->i_gid = pdir->i_gid;
2243 DIP_SET(ip, i_gid, pdir->i_gid);
2247 struct ucred ucred, *ucp;
2251 * If we are not the owner of the directory,
2252 * and we are hacking owners here, (only do this where told to)
2253 * and we are not giving it TO root, (would subvert quotas)
2254 * then go ahead and give it to the other user.
2255 * Note that this drops off the execute bits for security.
2257 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2258 (pdir->i_mode & ISUID) &&
2259 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2260 ip->i_uid = pdir->i_uid;
2261 DIP_SET(ip, i_uid, ip->i_uid);
2265 * Make sure the correct user gets charged
2267 * Quickly knock up a dummy credential for the victim.
2268 * XXX This seems to never be accessed out of our
2269 * context so a stack variable is ok.
2271 refcount_init(&ucred.cr_ref, 1);
2272 ucred.cr_uid = ip->i_uid;
2273 ucred.cr_ngroups = 1;
2274 ucred.cr_groups[0] = pdir->i_gid;
2278 ip->i_uid = cnp->cn_cred->cr_uid;
2279 DIP_SET(ip, i_uid, ip->i_uid);
2283 if ((error = getinoquota(ip)) ||
2284 (error = chkiq(ip, 1, ucp, 0))) {
2285 UFS_VFREE(tvp, ip->i_number, mode);
2291 #else /* !SUIDDIR */
2292 ip->i_uid = cnp->cn_cred->cr_uid;
2293 DIP_SET(ip, i_uid, ip->i_uid);
2295 if ((error = getinoquota(ip)) ||
2296 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2297 UFS_VFREE(tvp, ip->i_number, mode);
2302 #endif /* !SUIDDIR */
2303 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2306 if ((dvp->v_mount->mnt_flag & MNT_ACLS) != 0) {
2307 acl = uma_zalloc(acl_zone, M_WAITOK);
2310 * Retrieve default ACL for parent, if any.
2312 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cnp->cn_cred,
2317 * Retrieved a default ACL, so merge mode and ACL if
2320 if (acl->acl_cnt != 0) {
2322 * Two possible ways for default ACL to not
2323 * be present. First, the EA can be
2324 * undefined, or second, the default ACL can
2325 * be blank. If it's blank, fall through to
2326 * the it's not defined case.
2328 mode = acl_posix1e_newfilemode(mode, acl);
2330 DIP_SET(ip, i_mode, mode);
2331 ufs_sync_acl_from_inode(ip, acl);
2338 * Just use the mode as-is.
2341 DIP_SET(ip, i_mode, mode);
2342 uma_zfree(acl_zone, acl);
2347 UFS_VFREE(tvp, ip->i_number, mode);
2349 uma_zfree(acl_zone, acl);
2356 DIP_SET(ip, i_mode, mode);
2360 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2363 DIP_SET(ip, i_nlink, 1);
2364 if (DOINGSOFTDEP(tvp))
2365 softdep_change_linkcnt(ip);
2366 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2367 priv_check_cred(cnp->cn_cred, PRIV_VFS_SETGID, 0)) {
2368 ip->i_mode &= ~ISGID;
2369 DIP_SET(ip, i_mode, ip->i_mode);
2372 if (cnp->cn_flags & ISWHITEOUT) {
2373 ip->i_flags |= UF_OPAQUE;
2374 DIP_SET(ip, i_flags, ip->i_flags);
2378 * Make sure inode goes to disk before directory entry.
2380 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2384 if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
2385 error = mac_vnode_create_extattr(cnp->cn_cred, dvp->v_mount,
2394 * XXX: If we abort now, will Soft Updates notify the extattr
2395 * code that the EAs for the file need to be released?
2397 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cnp->cn_cred,
2405 * XXX: This should not happen, as EOPNOTSUPP above was
2406 * supposed to free acl.
2408 printf("ufs_makeinode: VOP_GETACL() but no "
2410 /* panic("ufs_makeinode: VOP_GETACL() but no "
2415 uma_zfree(acl_zone, acl);
2418 uma_zfree(acl_zone, acl);
2420 #endif /* !UFS_ACL */
2421 ufs_makedirentry(ip, cnp, &newdir);
2422 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2430 * Write error occurred trying to update the inode
2431 * or the directory so must deallocate the inode.
2435 DIP_SET(ip, i_nlink, 0);
2436 ip->i_flag |= IN_CHANGE;
2437 if (DOINGSOFTDEP(tvp))
2438 softdep_change_linkcnt(ip);
2443 /* Global vfs data structures for ufs. */
2444 struct vop_vector ufs_vnodeops = {
2445 .vop_default = &default_vnodeops,
2446 .vop_fsync = VOP_PANIC,
2447 .vop_read = VOP_PANIC,
2448 .vop_reallocblks = VOP_PANIC,
2449 .vop_write = VOP_PANIC,
2450 .vop_access = ufs_access,
2451 .vop_advlock = ufs_advlock,
2452 .vop_bmap = ufs_bmap,
2453 .vop_cachedlookup = ufs_lookup,
2454 .vop_close = ufs_close,
2455 .vop_create = ufs_create,
2456 .vop_getattr = ufs_getattr,
2457 .vop_inactive = ufs_inactive,
2458 .vop_link = ufs_link,
2459 .vop_lookup = vfs_cache_lookup,
2460 .vop_mkdir = ufs_mkdir,
2461 .vop_mknod = ufs_mknod,
2462 .vop_open = ufs_open,
2463 .vop_pathconf = ufs_pathconf,
2464 .vop_poll = vop_stdpoll,
2465 .vop_print = ufs_print,
2466 .vop_readdir = ufs_readdir,
2467 .vop_readlink = ufs_readlink,
2468 .vop_reclaim = ufs_reclaim,
2469 .vop_remove = ufs_remove,
2470 .vop_rename = ufs_rename,
2471 .vop_rmdir = ufs_rmdir,
2472 .vop_setattr = ufs_setattr,
2474 .vop_setlabel = vop_stdsetlabel_ea,
2476 .vop_strategy = ufs_strategy,
2477 .vop_symlink = ufs_symlink,
2478 .vop_whiteout = ufs_whiteout,
2480 .vop_getextattr = ufs_getextattr,
2481 .vop_deleteextattr = ufs_deleteextattr,
2482 .vop_setextattr = ufs_setextattr,
2485 .vop_getacl = ufs_getacl,
2486 .vop_setacl = ufs_setacl,
2487 .vop_aclcheck = ufs_aclcheck,
2491 struct vop_vector ufs_fifoops = {
2492 .vop_default = &fifo_specops,
2493 .vop_fsync = VOP_PANIC,
2494 .vop_access = ufs_access,
2495 .vop_close = ufsfifo_close,
2496 .vop_getattr = ufs_getattr,
2497 .vop_inactive = ufs_inactive,
2498 .vop_kqfilter = ufsfifo_kqfilter,
2499 .vop_print = ufs_print,
2500 .vop_read = VOP_PANIC,
2501 .vop_reclaim = ufs_reclaim,
2502 .vop_setattr = ufs_setattr,
2504 .vop_setlabel = vop_stdsetlabel_ea,
2506 .vop_write = VOP_PANIC,
2508 .vop_getextattr = ufs_getextattr,
2509 .vop_deleteextattr = ufs_deleteextattr,
2510 .vop_setextattr = ufs_setextattr,
2513 .vop_getacl = ufs_getacl,
2514 .vop_setacl = ufs_setacl,
2515 .vop_aclcheck = ufs_aclcheck,