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$");
40 #include "opt_quota.h"
41 #include "opt_suiddir.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
48 #include <sys/namei.h>
49 #include <sys/kernel.h>
50 #include <sys/fcntl.h>
54 #include <sys/mount.h>
56 #include <sys/refcount.h>
57 #include <sys/unistd.h>
58 #include <sys/vnode.h>
59 #include <sys/dirent.h>
60 #include <sys/lockf.h>
64 #include <machine/mutex.h>
66 #include <security/mac/mac_framework.h>
68 #include <sys/file.h> /* XXX */
71 #include <vm/vm_extern.h>
73 #include <fs/fifofs/fifo.h>
75 #include <ufs/ufs/acl.h>
76 #include <ufs/ufs/extattr.h>
77 #include <ufs/ufs/quota.h>
78 #include <ufs/ufs/inode.h>
79 #include <ufs/ufs/dir.h>
80 #include <ufs/ufs/ufsmount.h>
81 #include <ufs/ufs/ufs_extern.h>
83 #include <ufs/ufs/dirhash.h>
86 #include <ufs/ufs/gjournal.h>
89 #include <ufs/ffs/ffs_extern.h>
91 static vop_access_t ufs_access;
92 static int ufs_chmod(struct vnode *, int, struct ucred *, struct thread *);
93 static int ufs_chown(struct vnode *, uid_t, gid_t, struct ucred *, struct thread *);
94 static vop_close_t ufs_close;
95 static vop_create_t ufs_create;
96 static vop_getattr_t ufs_getattr;
97 static vop_link_t ufs_link;
98 static int ufs_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
99 static vop_markatime_t ufs_markatime;
100 static vop_mkdir_t ufs_mkdir;
101 static vop_mknod_t ufs_mknod;
102 static vop_open_t ufs_open;
103 static vop_pathconf_t ufs_pathconf;
104 static vop_print_t ufs_print;
105 static vop_readlink_t ufs_readlink;
106 static vop_remove_t ufs_remove;
107 static vop_rename_t ufs_rename;
108 static vop_rmdir_t ufs_rmdir;
109 static vop_setattr_t ufs_setattr;
110 static vop_strategy_t ufs_strategy;
111 static vop_symlink_t ufs_symlink;
112 static vop_whiteout_t ufs_whiteout;
113 static vop_close_t ufsfifo_close;
114 static vop_kqfilter_t ufsfifo_kqfilter;
115 static vop_pathconf_t ufsfifo_pathconf;
118 * A virgin directory (no blushing please).
120 static struct dirtemplate mastertemplate = {
121 0, 12, DT_DIR, 1, ".",
122 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
124 static struct odirtemplate omastertemplate = {
126 0, DIRBLKSIZ - 12, 2, ".."
130 ufs_itimes_locked(struct vnode *vp)
135 ASSERT_VI_LOCKED(vp, __func__);
140 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
143 if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
144 ip->i_flag |= IN_LAZYMOD;
145 else if (((vp->v_mount->mnt_kern_flag &
146 (MNTK_SUSPENDED | MNTK_SUSPEND)) == 0) ||
147 (ip->i_flag & (IN_CHANGE | IN_UPDATE)))
148 ip->i_flag |= IN_MODIFIED;
149 else if (ip->i_flag & IN_ACCESS)
150 ip->i_flag |= IN_LAZYACCESS;
152 if (ip->i_flag & IN_ACCESS) {
153 DIP_SET(ip, i_atime, ts.tv_sec);
154 DIP_SET(ip, i_atimensec, ts.tv_nsec);
156 if (ip->i_flag & IN_UPDATE) {
157 DIP_SET(ip, i_mtime, ts.tv_sec);
158 DIP_SET(ip, i_mtimensec, ts.tv_nsec);
160 if (ip->i_flag & IN_CHANGE) {
161 DIP_SET(ip, i_ctime, ts.tv_sec);
162 DIP_SET(ip, i_ctimensec, ts.tv_nsec);
163 DIP_SET(ip, i_modrev, DIP(ip, i_modrev) + 1);
167 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
171 ufs_itimes(struct vnode *vp)
175 ufs_itimes_locked(vp);
180 * Create a regular file
184 struct vop_create_args /* {
186 struct vnode **a_vpp;
187 struct componentname *a_cnp;
194 ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
195 ap->a_dvp, ap->a_vpp, ap->a_cnp);
207 struct vop_mknod_args /* {
209 struct vnode **a_vpp;
210 struct componentname *a_cnp;
214 struct vattr *vap = ap->a_vap;
215 struct vnode **vpp = ap->a_vpp;
220 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
221 ap->a_dvp, vpp, ap->a_cnp);
225 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
226 if (vap->va_rdev != VNOVAL) {
228 * Want to be able to use this to make badblock
229 * inodes, so don't truncate the dev number.
231 DIP_SET(ip, i_rdev, vap->va_rdev);
234 * Remove inode, then reload it through VFS_VGET so it is
235 * checked to see if it is an alias of an existing entry in
236 * the inode cache. XXX I don't believe this is necessary now.
238 (*vpp)->v_type = VNON;
239 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
242 error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
255 ufs_open(struct vop_open_args *ap)
257 struct vnode *vp = ap->a_vp;
260 if (vp->v_type == VCHR || vp->v_type == VBLK)
265 * Files marked append-only must be opened for appending.
267 if ((ip->i_flags & APPEND) &&
268 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
270 vnode_create_vobject(vp, DIP(ip, i_size), ap->a_td);
277 * Update the times on the inode.
282 struct vop_close_args /* {
285 struct ucred *a_cred;
289 struct vnode *vp = ap->a_vp;
293 usecount = vp->v_usecount;
295 ufs_itimes_locked(vp);
302 struct vop_access_args /* {
305 struct ucred *a_cred;
309 struct vnode *vp = ap->a_vp;
310 struct inode *ip = VTOI(vp);
311 accmode_t accmode = ap->a_accmode;
321 * Disallow write attempts on read-only filesystems;
322 * unless the file is a socket, fifo, or a block or
323 * character device resident on the filesystem.
325 if (accmode & VWRITE) {
326 switch (vp->v_type) {
330 if (vp->v_mount->mnt_flag & MNT_RDONLY)
334 * Inode is accounted in the quotas only if struct
335 * dquot is attached to it. VOP_ACCESS() is called
336 * from vn_open_cred() and provides a convenient
337 * point to call getinoquota().
339 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
342 * Upgrade vnode lock, since getinoquota()
343 * requires exclusive lock to modify inode.
347 vn_lock(vp, LK_UPGRADE | LK_RETRY);
349 if (vp->v_iflag & VI_DOOMED) {
357 error = getinoquota(ip);
360 vn_lock(vp, LK_DOWNGRADE | LK_RETRY);
370 /* If immutable bit set, nobody gets to write it. */
371 if ((accmode & VWRITE) && (ip->i_flags & (IMMUTABLE | SF_SNAPSHOT)))
375 if ((vp->v_mount->mnt_flag & MNT_ACLS) != 0) {
376 acl = acl_alloc(M_WAITOK);
377 error = VOP_GETACL(vp, ACL_TYPE_ACCESS, acl, ap->a_cred,
381 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid,
382 ip->i_gid, ap->a_accmode, ap->a_cred, NULL);
385 error = vaccess_acl_posix1e(vp->v_type, ip->i_uid,
386 ip->i_gid, acl, ap->a_accmode, ap->a_cred, NULL);
390 "ufs_access(): Error retrieving ACL on object (%d).\n",
393 * XXX: Fall back until debugged. Should
394 * eventually possibly log an error, and return
397 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid,
398 ip->i_gid, ap->a_accmode, ap->a_cred, NULL);
402 #endif /* !UFS_ACL */
403 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
404 ap->a_accmode, ap->a_cred, NULL);
411 struct vop_getattr_args /* {
414 struct ucred *a_cred;
417 struct vnode *vp = ap->a_vp;
418 struct inode *ip = VTOI(vp);
419 struct vattr *vap = ap->a_vap;
422 ufs_itimes_locked(vp);
423 if (ip->i_ump->um_fstype == UFS1) {
424 vap->va_atime.tv_sec = ip->i_din1->di_atime;
425 vap->va_atime.tv_nsec = ip->i_din1->di_atimensec;
427 vap->va_atime.tv_sec = ip->i_din2->di_atime;
428 vap->va_atime.tv_nsec = ip->i_din2->di_atimensec;
432 * Copy from inode table
434 vap->va_fsid = dev2udev(ip->i_dev);
435 vap->va_fileid = ip->i_number;
436 vap->va_mode = ip->i_mode & ~IFMT;
437 vap->va_nlink = ip->i_effnlink;
438 vap->va_uid = ip->i_uid;
439 vap->va_gid = ip->i_gid;
440 if (ip->i_ump->um_fstype == UFS1) {
441 vap->va_rdev = ip->i_din1->di_rdev;
442 vap->va_size = ip->i_din1->di_size;
443 vap->va_mtime.tv_sec = ip->i_din1->di_mtime;
444 vap->va_mtime.tv_nsec = ip->i_din1->di_mtimensec;
445 vap->va_ctime.tv_sec = ip->i_din1->di_ctime;
446 vap->va_ctime.tv_nsec = ip->i_din1->di_ctimensec;
447 vap->va_bytes = dbtob((u_quad_t)ip->i_din1->di_blocks);
448 vap->va_filerev = ip->i_din1->di_modrev;
450 vap->va_rdev = ip->i_din2->di_rdev;
451 vap->va_size = ip->i_din2->di_size;
452 vap->va_mtime.tv_sec = ip->i_din2->di_mtime;
453 vap->va_mtime.tv_nsec = ip->i_din2->di_mtimensec;
454 vap->va_ctime.tv_sec = ip->i_din2->di_ctime;
455 vap->va_ctime.tv_nsec = ip->i_din2->di_ctimensec;
456 vap->va_birthtime.tv_sec = ip->i_din2->di_birthtime;
457 vap->va_birthtime.tv_nsec = ip->i_din2->di_birthnsec;
458 vap->va_bytes = dbtob((u_quad_t)ip->i_din2->di_blocks);
459 vap->va_filerev = ip->i_din2->di_modrev;
461 vap->va_flags = ip->i_flags;
462 vap->va_gen = ip->i_gen;
463 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
464 vap->va_type = IFTOVT(ip->i_mode);
469 * Set attribute vnode op. called from several syscalls
473 struct vop_setattr_args /* {
476 struct ucred *a_cred;
479 struct vattr *vap = ap->a_vap;
480 struct vnode *vp = ap->a_vp;
481 struct inode *ip = VTOI(vp);
482 struct ucred *cred = ap->a_cred;
483 struct thread *td = curthread;
487 * Check for unsettable attributes.
489 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
490 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
491 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
492 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
495 if (vap->va_flags != VNOVAL) {
496 if (vp->v_mount->mnt_flag & MNT_RDONLY)
499 * Callers may only modify the file flags on objects they
500 * have VADMIN rights for.
502 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
505 * Unprivileged processes are not permitted to unset system
506 * flags, or modify flags if any system flags are set.
507 * Privileged non-jail processes may not modify system flags
508 * if securelevel > 0 and any existing system flags are set.
509 * Privileged jail processes behave like privileged non-jail
510 * processes if the security.jail.chflags_allowed sysctl is
511 * is non-zero; otherwise, they behave like unprivileged
514 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
516 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
517 error = securelevel_gt(cred, 0);
521 /* Snapshot flag cannot be set or cleared */
522 if (((vap->va_flags & SF_SNAPSHOT) != 0 &&
523 (ip->i_flags & SF_SNAPSHOT) == 0) ||
524 ((vap->va_flags & SF_SNAPSHOT) == 0 &&
525 (ip->i_flags & SF_SNAPSHOT) != 0))
527 ip->i_flags = vap->va_flags;
528 DIP_SET(ip, i_flags, vap->va_flags);
531 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
532 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
534 ip->i_flags &= SF_SETTABLE;
535 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
536 DIP_SET(ip, i_flags, ip->i_flags);
538 ip->i_flag |= IN_CHANGE;
539 if (vap->va_flags & (IMMUTABLE | APPEND))
542 if (ip->i_flags & (IMMUTABLE | APPEND))
545 * Go through the fields and update iff not VNOVAL.
547 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
548 if (vp->v_mount->mnt_flag & MNT_RDONLY)
550 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred,
554 if (vap->va_size != VNOVAL) {
556 * XXX most of the following special cases should be in
557 * callers instead of in N filesystems. The VDIR check
560 switch (vp->v_type) {
566 * Truncation should have an effect in these cases.
567 * Disallow it if the filesystem is read-only or
568 * the file is being snapshotted.
570 if (vp->v_mount->mnt_flag & MNT_RDONLY)
572 if ((ip->i_flags & SF_SNAPSHOT) != 0)
577 * According to POSIX, the result is unspecified
578 * for file types other than regular files,
579 * directories and shared memory objects. We
580 * don't support shared memory objects in the file
581 * system, and have dubious support for truncating
582 * symlinks. Just ignore the request in other cases.
586 if ((error = UFS_TRUNCATE(vp, vap->va_size, IO_NORMAL,
590 if (vap->va_atime.tv_sec != VNOVAL ||
591 vap->va_mtime.tv_sec != VNOVAL ||
592 vap->va_birthtime.tv_sec != VNOVAL) {
593 if (vp->v_mount->mnt_flag & MNT_RDONLY)
595 if ((ip->i_flags & SF_SNAPSHOT) != 0)
599 * If times is NULL, ... The caller must be the owner of
600 * the file, have permission to write the file, or be the
602 * If times is non-NULL, ... The caller must be the owner of
603 * the file or be the super-user.
605 * Possibly for historical reasons, try to use VADMIN in
606 * preference to VWRITE for a NULL timestamp. This means we
607 * will return EACCES in preference to EPERM if neither
610 if (vap->va_vaflags & VA_UTIMES_NULL) {
611 error = VOP_ACCESS(vp, VADMIN, cred, td);
613 error = VOP_ACCESS(vp, VWRITE, cred, td);
615 error = VOP_ACCESS(vp, VADMIN, cred, td);
618 if (vap->va_atime.tv_sec != VNOVAL)
619 ip->i_flag |= IN_ACCESS;
620 if (vap->va_mtime.tv_sec != VNOVAL)
621 ip->i_flag |= IN_CHANGE | IN_UPDATE;
622 if (vap->va_birthtime.tv_sec != VNOVAL &&
623 ip->i_ump->um_fstype == UFS2)
624 ip->i_flag |= IN_MODIFIED;
626 if (vap->va_atime.tv_sec != VNOVAL) {
627 DIP_SET(ip, i_atime, vap->va_atime.tv_sec);
628 DIP_SET(ip, i_atimensec, vap->va_atime.tv_nsec);
630 if (vap->va_mtime.tv_sec != VNOVAL) {
631 DIP_SET(ip, i_mtime, vap->va_mtime.tv_sec);
632 DIP_SET(ip, i_mtimensec, vap->va_mtime.tv_nsec);
634 if (vap->va_birthtime.tv_sec != VNOVAL &&
635 ip->i_ump->um_fstype == UFS2) {
636 ip->i_din2->di_birthtime = vap->va_birthtime.tv_sec;
637 ip->i_din2->di_birthnsec = vap->va_birthtime.tv_nsec;
639 error = UFS_UPDATE(vp, 0);
644 if (vap->va_mode != (mode_t)VNOVAL) {
645 if (vp->v_mount->mnt_flag & MNT_RDONLY)
647 if ((ip->i_flags & SF_SNAPSHOT) != 0 && (vap->va_mode &
648 (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP | S_IXOTH | S_IWOTH)))
650 error = ufs_chmod(vp, (int)vap->va_mode, cred, td);
656 * Mark this file's access time for update for vfs_mark_atime(). This
657 * is called from execve() and mmap().
661 struct vop_markatime_args /* {
665 struct vnode *vp = ap->a_vp;
666 struct inode *ip = VTOI(vp);
669 ip->i_flag |= IN_ACCESS;
675 * Change the mode on a file.
676 * Inode must be locked before calling.
679 ufs_chmod(vp, mode, cred, td)
685 struct inode *ip = VTOI(vp);
689 * To modify the permissions on a file, must possess VADMIN
692 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
695 * Privileged processes may set the sticky bit on non-directories,
696 * as well as set the setgid bit on a file with a group that the
697 * process is not a member of. Both of these are allowed in
700 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
701 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0))
704 if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
705 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
709 ip->i_mode &= ~ALLPERMS;
710 ip->i_mode |= (mode & ALLPERMS);
711 DIP_SET(ip, i_mode, ip->i_mode);
712 ip->i_flag |= IN_CHANGE;
717 * Perform chown operation on inode ip;
718 * inode must be locked prior to call.
721 ufs_chown(vp, uid, gid, cred, td)
728 struct inode *ip = VTOI(vp);
737 if (uid == (uid_t)VNOVAL)
739 if (gid == (gid_t)VNOVAL)
742 * To modify the ownership of a file, must possess VADMIN for that
745 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
748 * To change the owner of a file, or change the group of a file to a
749 * group of which we are not a member, the caller must have
752 if ((uid != ip->i_uid ||
753 (gid != ip->i_gid && !groupmember(gid, cred))) &&
754 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
759 if ((error = getinoquota(ip)) != 0)
762 dqrele(vp, ip->i_dquot[USRQUOTA]);
763 ip->i_dquot[USRQUOTA] = NODQUOT;
766 dqrele(vp, ip->i_dquot[GRPQUOTA]);
767 ip->i_dquot[GRPQUOTA] = NODQUOT;
769 change = DIP(ip, i_blocks);
770 (void) chkdq(ip, -change, cred, CHOWN);
771 (void) chkiq(ip, -1, cred, CHOWN);
772 for (i = 0; i < MAXQUOTAS; i++) {
773 dqrele(vp, ip->i_dquot[i]);
774 ip->i_dquot[i] = NODQUOT;
778 DIP_SET(ip, i_gid, gid);
780 DIP_SET(ip, i_uid, uid);
782 if ((error = getinoquota(ip)) == 0) {
784 dqrele(vp, ip->i_dquot[USRQUOTA]);
785 ip->i_dquot[USRQUOTA] = NODQUOT;
788 dqrele(vp, ip->i_dquot[GRPQUOTA]);
789 ip->i_dquot[GRPQUOTA] = NODQUOT;
791 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
792 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
795 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
797 for (i = 0; i < MAXQUOTAS; i++) {
798 dqrele(vp, ip->i_dquot[i]);
799 ip->i_dquot[i] = NODQUOT;
803 DIP_SET(ip, i_gid, ogid);
805 DIP_SET(ip, i_uid, ouid);
806 if (getinoquota(ip) == 0) {
808 dqrele(vp, ip->i_dquot[USRQUOTA]);
809 ip->i_dquot[USRQUOTA] = NODQUOT;
812 dqrele(vp, ip->i_dquot[GRPQUOTA]);
813 ip->i_dquot[GRPQUOTA] = NODQUOT;
815 (void) chkdq(ip, change, cred, FORCE|CHOWN);
816 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
817 (void) getinoquota(ip);
822 panic("ufs_chown: lost quota");
824 ip->i_flag |= IN_CHANGE;
825 if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
826 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0)) {
827 ip->i_mode &= ~(ISUID | ISGID);
828 DIP_SET(ip, i_mode, ip->i_mode);
836 struct vop_remove_args /* {
839 struct componentname *a_cnp;
843 struct vnode *vp = ap->a_vp;
844 struct vnode *dvp = ap->a_dvp;
850 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
851 (VTOI(dvp)->i_flags & APPEND)) {
856 ufs_gjournal_orphan(vp);
858 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
859 if (ip->i_nlink <= 0)
860 vp->v_vflag |= VV_NOSYNC;
861 if ((ip->i_flags & SF_SNAPSHOT) != 0) {
863 * Avoid deadlock where another thread is trying to
864 * update the inodeblock for dvp and is waiting on
865 * snaplk. Temporary unlock the vnode lock for the
866 * unlinked file and sync the directory. This should
867 * allow vput() of the directory to not block later on
868 * while holding the snapshot vnode locked, assuming
869 * that the directory hasn't been unlinked too.
872 (void) VOP_FSYNC(dvp, MNT_WAIT, td);
873 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
884 struct vop_link_args /* {
885 struct vnode *a_tdvp;
887 struct componentname *a_cnp;
890 struct vnode *vp = ap->a_vp;
891 struct vnode *tdvp = ap->a_tdvp;
892 struct componentname *cnp = ap->a_cnp;
894 struct direct newdir;
898 if ((cnp->cn_flags & HASBUF) == 0)
899 panic("ufs_link: no name");
901 if (tdvp->v_mount != vp->v_mount) {
906 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
910 if (ip->i_flags & (IMMUTABLE | APPEND)) {
916 DIP_SET(ip, i_nlink, ip->i_nlink);
917 ip->i_flag |= IN_CHANGE;
918 if (DOINGSOFTDEP(vp))
919 softdep_change_linkcnt(ip);
920 error = UFS_UPDATE(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
922 ufs_makedirentry(ip, cnp, &newdir);
923 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
929 DIP_SET(ip, i_nlink, ip->i_nlink);
930 ip->i_flag |= IN_CHANGE;
931 if (DOINGSOFTDEP(vp))
932 softdep_change_linkcnt(ip);
939 * whiteout vnode call
943 struct vop_whiteout_args /* {
945 struct componentname *a_cnp;
949 struct vnode *dvp = ap->a_dvp;
950 struct componentname *cnp = ap->a_cnp;
951 struct direct newdir;
954 switch (ap->a_flags) {
956 /* 4.4 format directories support whiteout operations */
957 if (dvp->v_mount->mnt_maxsymlinklen > 0)
962 /* create a new directory whiteout */
964 if ((cnp->cn_flags & SAVENAME) == 0)
965 panic("ufs_whiteout: missing name");
966 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
967 panic("ufs_whiteout: old format filesystem");
971 newdir.d_namlen = cnp->cn_namelen;
972 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
973 newdir.d_type = DT_WHT;
974 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
978 /* remove an existing directory whiteout */
980 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
981 panic("ufs_whiteout: old format filesystem");
984 cnp->cn_flags &= ~DOWHITEOUT;
985 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
988 panic("ufs_whiteout: unknown op");
994 * Rename system call.
995 * rename("foo", "bar");
998 * link("foo", "bar");
1000 * but ``atomically''. Can't do full commit without saving state in the
1001 * inode on disk which isn't feasible at this time. Best we can do is
1002 * always guarantee the target exists.
1004 * Basic algorithm is:
1006 * 1) Bump link count on source while we're linking it to the
1007 * target. This also ensure the inode won't be deleted out
1008 * from underneath us while we work (it may be truncated by
1009 * a concurrent `trunc' or `open' for creation).
1010 * 2) Link source to destination. If destination already exists,
1012 * 3) Unlink source reference to inode if still around. If a
1013 * directory was moved and the parent of the destination
1014 * is different from the source, patch the ".." entry in the
1019 struct vop_rename_args /* {
1020 struct vnode *a_fdvp;
1021 struct vnode *a_fvp;
1022 struct componentname *a_fcnp;
1023 struct vnode *a_tdvp;
1024 struct vnode *a_tvp;
1025 struct componentname *a_tcnp;
1028 struct vnode *tvp = ap->a_tvp;
1029 struct vnode *tdvp = ap->a_tdvp;
1030 struct vnode *fvp = ap->a_fvp;
1031 struct vnode *fdvp = ap->a_fdvp;
1032 struct componentname *tcnp = ap->a_tcnp;
1033 struct componentname *fcnp = ap->a_fcnp;
1034 struct thread *td = fcnp->cn_thread;
1035 struct inode *ip, *xp, *dp;
1036 struct direct newdir;
1037 int doingdirectory = 0, oldparent = 0, newparent = 0;
1038 int error = 0, ioflag;
1042 if ((tcnp->cn_flags & HASBUF) == 0 ||
1043 (fcnp->cn_flags & HASBUF) == 0)
1044 panic("ufs_rename: no name");
1047 * Check for cross-device rename.
1049 if ((fvp->v_mount != tdvp->v_mount) ||
1050 (tvp && (fvp->v_mount != tvp->v_mount))) {
1064 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1065 (VTOI(tdvp)->i_flags & APPEND))) {
1071 * Renaming a file to itself has no effect. The upper layers should
1072 * not call us in that case. Temporarily just warn if they do.
1075 printf("ufs_rename: fvp == tvp (can't happen)\n");
1080 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
1084 if (ip->i_nlink >= LINK_MAX) {
1089 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
1090 || (dp->i_flags & APPEND)) {
1095 if ((ip->i_mode & IFMT) == IFDIR) {
1097 * Avoid ".", "..", and aliases of "." for obvious reasons.
1099 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1100 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
1101 (ip->i_flag & IN_RENAME)) {
1106 ip->i_flag |= IN_RENAME;
1107 oldparent = dp->i_number;
1113 * When the target exists, both the directory
1114 * and target vnodes are returned locked.
1122 * 1) Bump link count while we're moving stuff
1123 * around. If we crash somewhere before
1124 * completing our work, the link count
1125 * may be wrong, but correctable.
1129 DIP_SET(ip, i_nlink, ip->i_nlink);
1130 ip->i_flag |= IN_CHANGE;
1131 if (DOINGSOFTDEP(fvp))
1132 softdep_change_linkcnt(ip);
1133 if ((error = UFS_UPDATE(fvp, !(DOINGSOFTDEP(fvp) |
1134 DOINGASYNC(fvp)))) != 0) {
1140 * If ".." must be changed (ie the directory gets a new
1141 * parent) then the source directory must not be in the
1142 * directory hierarchy above the target, as this would
1143 * orphan everything below the source directory. Also
1144 * the user must have write permission in the source so
1145 * as to be able to change "..". We must repeat the call
1146 * to namei, as the parent directory is unlocked by the
1147 * call to checkpath().
1149 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
1150 fvp_ino = ip->i_number;
1152 if (oldparent != dp->i_number)
1153 newparent = dp->i_number;
1154 if (doingdirectory && newparent) {
1155 if (error) /* write access check above */
1159 error = ufs_checkpath(fvp_ino, dp, tcnp->cn_cred);
1162 if ((tcnp->cn_flags & SAVESTART) == 0)
1163 panic("ufs_rename: lost to startdir");
1165 error = relookup(tdvp, &tvp, tcnp);
1175 * 2) If target doesn't exist, link the target
1176 * to the source and unlink the source.
1177 * Otherwise, rewrite the target directory
1178 * entry to reference the source inode and
1179 * expunge the original entry's existence.
1182 if (dp->i_dev != ip->i_dev)
1183 panic("ufs_rename: EXDEV");
1185 * Account for ".." in new directory.
1186 * When source and destination have the same
1187 * parent we don't fool with the link count.
1189 if (doingdirectory && newparent) {
1190 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1196 DIP_SET(dp, i_nlink, dp->i_nlink);
1197 dp->i_flag |= IN_CHANGE;
1198 if (DOINGSOFTDEP(tdvp))
1199 softdep_change_linkcnt(dp);
1200 error = UFS_UPDATE(tdvp, !(DOINGSOFTDEP(tdvp) |
1205 ufs_makedirentry(ip, tcnp, &newdir);
1206 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1208 if (doingdirectory && newparent) {
1211 DIP_SET(dp, i_nlink, dp->i_nlink);
1212 dp->i_flag |= IN_CHANGE;
1213 if (DOINGSOFTDEP(tdvp))
1214 softdep_change_linkcnt(dp);
1215 (void)UFS_UPDATE(tdvp, 1);
1221 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1222 panic("ufs_rename: EXDEV");
1224 * Short circuit rename(foo, foo).
1226 if (xp->i_number == ip->i_number)
1227 panic("ufs_rename: same file");
1229 * If the parent directory is "sticky", then the caller
1230 * must possess VADMIN for the parent directory, or the
1231 * destination of the rename. This implements append-only
1234 if ((dp->i_mode & S_ISTXT) &&
1235 VOP_ACCESS(tdvp, VADMIN, tcnp->cn_cred, td) &&
1236 VOP_ACCESS(tvp, VADMIN, tcnp->cn_cred, td)) {
1241 * Target must be empty if a directory and have no links
1242 * to it. Also, ensure source and target are compatible
1243 * (both directories, or both not directories).
1245 if ((xp->i_mode&IFMT) == IFDIR) {
1246 if ((xp->i_effnlink > 2) ||
1247 !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1251 if (!doingdirectory) {
1256 } else if (doingdirectory) {
1260 error = ufs_dirrewrite(dp, xp, ip->i_number,
1262 (doingdirectory && newparent) ? newparent : doingdirectory);
1265 if (doingdirectory) {
1268 if (DOINGSOFTDEP(tdvp))
1269 softdep_change_linkcnt(dp);
1272 if (DOINGSOFTDEP(tvp))
1273 softdep_change_linkcnt(xp);
1275 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1277 * Truncate inode. The only stuff left in the directory
1278 * is "." and "..". The "." reference is inconsequential
1279 * since we are quashing it. We have removed the "."
1280 * reference and the reference in the parent directory,
1281 * but there may be other hard links. The soft
1282 * dependency code will arrange to do these operations
1283 * after the parent directory entry has been deleted on
1284 * disk, so when running with that code we avoid doing
1289 DIP_SET(dp, i_nlink, dp->i_nlink);
1290 dp->i_flag |= IN_CHANGE;
1293 DIP_SET(xp, i_nlink, xp->i_nlink);
1294 xp->i_flag |= IN_CHANGE;
1296 if (!DOINGASYNC(tvp))
1298 if ((error = UFS_TRUNCATE(tvp, (off_t)0, ioflag,
1299 tcnp->cn_cred, tcnp->cn_thread)) != 0)
1308 * 3) Unlink the source.
1310 fcnp->cn_flags &= ~MODMASK;
1311 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1312 if ((fcnp->cn_flags & SAVESTART) == 0)
1313 panic("ufs_rename: lost from startdir");
1315 error = relookup(fdvp, &fvp, fcnp);
1323 * From name has disappeared. IN_RENAME is not sufficient
1324 * to protect against directory races due to timing windows,
1325 * so we have to remove the panic. XXX the only real way
1326 * to solve this issue is at a much higher level. By the
1327 * time we hit ufs_rename() it's too late.
1331 panic("ufs_rename: lost dir entry");
1337 * Ensure that the directory entry still exists and has not
1338 * changed while the new name has been entered. If the source is
1339 * a file then the entry may have been unlinked or renamed. In
1340 * either case there is no further work to be done. If the source
1341 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1342 * flag ensures that it cannot be moved by another rename or removed
1347 * From name resolves to a different inode. IN_RENAME is
1348 * not sufficient protection against timing window races
1349 * so we can't panic here. XXX the only real way
1350 * to solve this issue is at a much higher level. By the
1351 * time we hit ufs_rename() it's too late.
1355 panic("ufs_rename: lost dir entry");
1359 * If the source is a directory with a
1360 * new parent, the link count of the old
1361 * parent directory must be decremented
1362 * and ".." set to point to the new parent.
1364 if (doingdirectory && newparent) {
1365 xp->i_offset = mastertemplate.dot_reclen;
1366 ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1369 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1370 xp->i_flag &= ~IN_RENAME;
1385 ip->i_flag &= ~IN_RENAME;
1386 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1389 DIP_SET(ip, i_nlink, ip->i_nlink);
1390 ip->i_flag |= IN_CHANGE;
1391 ip->i_flag &= ~IN_RENAME;
1392 if (DOINGSOFTDEP(fvp))
1393 softdep_change_linkcnt(ip);
1405 struct vop_mkdir_args /* {
1406 struct vnode *a_dvp;
1407 struct vnode **a_vpp;
1408 struct componentname *a_cnp;
1409 struct vattr *a_vap;
1412 struct vnode *dvp = ap->a_dvp;
1413 struct vattr *vap = ap->a_vap;
1414 struct componentname *cnp = ap->a_cnp;
1415 struct inode *ip, *dp;
1418 struct dirtemplate dirtemplate, *dtp;
1419 struct direct newdir;
1421 struct acl *acl, *dacl;
1427 if ((cnp->cn_flags & HASBUF) == 0)
1428 panic("ufs_mkdir: no name");
1431 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1435 dmode = vap->va_mode & 0777;
1438 * Must simulate part of ufs_makeinode here to acquire the inode,
1439 * but not have it entered in the parent directory. The entry is
1440 * made later after writing "." and ".." entries.
1442 error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
1446 ip->i_gid = dp->i_gid;
1447 DIP_SET(ip, i_gid, dp->i_gid);
1451 struct ucred ucred, *ucp;
1455 * If we are hacking owners here, (only do this where told to)
1456 * and we are not giving it TO root, (would subvert quotas)
1457 * then go ahead and give it to the other user.
1458 * The new directory also inherits the SUID bit.
1459 * If user's UID and dir UID are the same,
1460 * 'give it away' so that the SUID is still forced on.
1462 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1463 (dp->i_mode & ISUID) && dp->i_uid) {
1465 ip->i_uid = dp->i_uid;
1466 DIP_SET(ip, i_uid, dp->i_uid);
1468 if (dp->i_uid != cnp->cn_cred->cr_uid) {
1470 * Make sure the correct user gets charged
1472 * Make a dummy credential for the victim.
1473 * XXX This seems to never be accessed out of
1474 * our context so a stack variable is ok.
1476 refcount_init(&ucred.cr_ref, 1);
1477 ucred.cr_uid = ip->i_uid;
1478 ucred.cr_ngroups = 1;
1479 ucred.cr_groups[0] = dp->i_gid;
1484 ip->i_uid = cnp->cn_cred->cr_uid;
1485 DIP_SET(ip, i_uid, ip->i_uid);
1488 if ((error = getinoquota(ip)) ||
1489 (error = chkiq(ip, 1, ucp, 0))) {
1490 UFS_VFREE(tvp, ip->i_number, dmode);
1496 #else /* !SUIDDIR */
1497 ip->i_uid = cnp->cn_cred->cr_uid;
1498 DIP_SET(ip, i_uid, ip->i_uid);
1500 if ((error = getinoquota(ip)) ||
1501 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1502 UFS_VFREE(tvp, ip->i_number, dmode);
1507 #endif /* !SUIDDIR */
1508 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1511 if ((dvp->v_mount->mnt_flag & MNT_ACLS) != 0) {
1512 acl = acl_alloc(M_WAITOK);
1513 dacl = acl_alloc(M_WAITOK);
1516 * Retrieve default ACL from parent, if any.
1518 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cnp->cn_cred,
1523 * Retrieved a default ACL, so merge mode and ACL if
1524 * necessary. If the ACL is empty, fall through to
1525 * the "not defined or available" case.
1527 if (acl->acl_cnt != 0) {
1528 dmode = acl_posix1e_newfilemode(dmode, acl);
1530 DIP_SET(ip, i_mode, dmode);
1532 ufs_sync_acl_from_inode(ip, acl);
1539 * Just use the mode as-is.
1542 DIP_SET(ip, i_mode, dmode);
1549 UFS_VFREE(tvp, ip->i_number, dmode);
1556 #endif /* !UFS_ACL */
1558 DIP_SET(ip, i_mode, dmode);
1562 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1565 DIP_SET(ip, i_nlink, 2);
1566 if (DOINGSOFTDEP(tvp))
1567 softdep_change_linkcnt(ip);
1568 if (cnp->cn_flags & ISWHITEOUT) {
1569 ip->i_flags |= UF_OPAQUE;
1570 DIP_SET(ip, i_flags, ip->i_flags);
1574 * Bump link count in parent directory to reflect work done below.
1575 * Should be done before reference is created so cleanup is
1576 * possible if we crash.
1580 DIP_SET(dp, i_nlink, dp->i_nlink);
1581 dp->i_flag |= IN_CHANGE;
1582 if (DOINGSOFTDEP(dvp))
1583 softdep_change_linkcnt(dp);
1584 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1588 if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
1589 error = mac_vnode_create_extattr(cnp->cn_cred, dvp->v_mount,
1598 * XXX: If we abort now, will Soft Updates notify the extattr
1599 * code that the EAs for the file need to be released?
1601 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cnp->cn_cred,
1604 error = VOP_SETACL(tvp, ACL_TYPE_DEFAULT, dacl,
1605 cnp->cn_cred, cnp->cn_thread);
1612 * XXX: This should not happen, as EOPNOTSUPP above
1613 * was supposed to free acl.
1615 printf("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()\n");
1617 panic("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()");
1631 #endif /* !UFS_ACL */
1634 * Initialize directory with "." and ".." from static template.
1636 if (dvp->v_mount->mnt_maxsymlinklen > 0)
1637 dtp = &mastertemplate;
1639 dtp = (struct dirtemplate *)&omastertemplate;
1641 dirtemplate.dot_ino = ip->i_number;
1642 dirtemplate.dotdot_ino = dp->i_number;
1643 if ((error = UFS_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred,
1644 BA_CLRBUF, &bp)) != 0)
1646 ip->i_size = DIRBLKSIZ;
1647 DIP_SET(ip, i_size, DIRBLKSIZ);
1648 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1649 vnode_pager_setsize(tvp, (u_long)ip->i_size);
1650 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1651 if (DOINGSOFTDEP(tvp)) {
1653 * Ensure that the entire newly allocated block is a
1654 * valid directory so that future growth within the
1655 * block does not have to ensure that the block is
1656 * written before the inode.
1659 while (blkoff < bp->b_bcount) {
1661 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1662 blkoff += DIRBLKSIZ;
1665 if ((error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) |
1666 DOINGASYNC(tvp)))) != 0) {
1671 * Directory set up, now install its entry in the parent directory.
1673 * If we are not doing soft dependencies, then we must write out the
1674 * buffer containing the new directory body before entering the new
1675 * name in the parent. If we are doing soft dependencies, then the
1676 * buffer containing the new directory body will be passed to and
1677 * released in the soft dependency code after the code has attached
1678 * an appropriate ordering dependency to the buffer which ensures that
1679 * the buffer is written before the new name is written in the parent.
1681 if (DOINGASYNC(dvp))
1683 else if (!DOINGSOFTDEP(dvp) && ((error = bwrite(bp))))
1685 ufs_makedirentry(ip, cnp, &newdir);
1686 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1700 DIP_SET(dp, i_nlink, dp->i_nlink);
1701 dp->i_flag |= IN_CHANGE;
1702 if (DOINGSOFTDEP(dvp))
1703 softdep_change_linkcnt(dp);
1705 * No need to do an explicit VOP_TRUNCATE here, vrele will
1706 * do this for us because we set the link count to 0.
1710 DIP_SET(ip, i_nlink, 0);
1711 ip->i_flag |= IN_CHANGE;
1712 if (DOINGSOFTDEP(tvp))
1713 softdep_change_linkcnt(ip);
1721 * Rmdir system call.
1725 struct vop_rmdir_args /* {
1726 struct vnode *a_dvp;
1728 struct componentname *a_cnp;
1731 struct vnode *vp = ap->a_vp;
1732 struct vnode *dvp = ap->a_dvp;
1733 struct componentname *cnp = ap->a_cnp;
1734 struct inode *ip, *dp;
1741 * Do not remove a directory that is in the process of being renamed.
1742 * Verify the directory is empty (and valid). Rmdir ".." will not be
1743 * valid since ".." will contain a reference to the current directory
1744 * and thus be non-empty. Do not allow the removal of mounted on
1745 * directories (this can happen when an NFS exported filesystem
1746 * tries to remove a locally mounted on directory).
1749 if ((ip->i_flag & IN_RENAME) || ip->i_effnlink < 2) {
1753 if (!ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1757 if ((dp->i_flags & APPEND)
1758 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1762 if (vp->v_mountedhere != 0) {
1767 ufs_gjournal_orphan(vp);
1770 * Delete reference to directory before purging
1771 * inode. If we crash in between, the directory
1772 * will be reattached to lost+found,
1776 if (DOINGSOFTDEP(vp)) {
1777 softdep_change_linkcnt(dp);
1778 softdep_change_linkcnt(ip);
1780 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1784 if (DOINGSOFTDEP(vp)) {
1785 softdep_change_linkcnt(dp);
1786 softdep_change_linkcnt(ip);
1792 * Truncate inode. The only stuff left in the directory is "." and
1793 * "..". The "." reference is inconsequential since we are quashing
1794 * it. The soft dependency code will arrange to do these operations
1795 * after the parent directory entry has been deleted on disk, so
1796 * when running with that code we avoid doing them now.
1798 if (!DOINGSOFTDEP(vp)) {
1800 DIP_SET(dp, i_nlink, dp->i_nlink);
1801 dp->i_flag |= IN_CHANGE;
1803 DIP_SET(ip, i_nlink, ip->i_nlink);
1804 ip->i_flag |= IN_CHANGE;
1806 if (!DOINGASYNC(vp))
1808 error = UFS_TRUNCATE(vp, (off_t)0, ioflag, cnp->cn_cred,
1813 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1814 if (ip->i_dirhash != NULL)
1815 ufsdirhash_free(ip);
1822 * symlink -- make a symbolic link
1826 struct vop_symlink_args /* {
1827 struct vnode *a_dvp;
1828 struct vnode **a_vpp;
1829 struct componentname *a_cnp;
1830 struct vattr *a_vap;
1834 struct vnode *vp, **vpp = ap->a_vpp;
1838 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1843 len = strlen(ap->a_target);
1844 if (len < vp->v_mount->mnt_maxsymlinklen) {
1846 bcopy(ap->a_target, SHORTLINK(ip), len);
1848 DIP_SET(ip, i_size, len);
1849 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1851 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1852 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1853 ap->a_cnp->cn_cred, NOCRED, NULL, NULL);
1860 * Vnode op for reading directories.
1862 * The routine below assumes that the on-disk format of a directory
1863 * is the same as that defined by <sys/dirent.h>. If the on-disk
1864 * format changes, then it will be necessary to do a conversion
1865 * from the on-disk format that read returns to the format defined
1866 * by <sys/dirent.h>.
1870 struct vop_readdir_args /* {
1873 struct ucred *a_cred;
1879 struct uio *uio = ap->a_uio;
1884 if (ap->a_ncookies != NULL)
1886 * Ensure that the block is aligned. The caller can use
1887 * the cookies to determine where in the block to start.
1889 uio->uio_offset &= ~(DIRBLKSIZ - 1);
1890 off = uio->uio_offset;
1891 count = uio->uio_resid;
1892 /* Make sure we don't return partial entries. */
1893 if (count <= ((uio->uio_offset + count) & (DIRBLKSIZ -1)))
1895 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
1896 lost = uio->uio_resid - count;
1897 uio->uio_resid = count;
1898 uio->uio_iov->iov_len = count;
1899 # if (BYTE_ORDER == LITTLE_ENDIAN)
1900 if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) {
1901 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1903 struct dirent *dp, *edp;
1911 auio.uio_iov = &aiov;
1912 auio.uio_iovcnt = 1;
1913 auio.uio_segflg = UIO_SYSSPACE;
1914 aiov.iov_len = count;
1915 dirbuf = malloc(count, M_TEMP, M_WAITOK);
1916 aiov.iov_base = dirbuf;
1917 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
1919 readcnt = count - auio.uio_resid;
1920 edp = (struct dirent *)&dirbuf[readcnt];
1921 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
1923 dp->d_namlen = dp->d_type;
1925 if (dp->d_reclen > 0) {
1926 dp = (struct dirent *)
1927 ((char *)dp + dp->d_reclen);
1934 error = uiomove(dirbuf, readcnt, uio);
1936 free(dirbuf, M_TEMP);
1939 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1941 if (!error && ap->a_ncookies != NULL) {
1942 struct dirent* dpStart;
1943 struct dirent* dpEnd;
1949 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1950 panic("ufs_readdir: unexpected uio from NFS server");
1951 dpStart = (struct dirent *)
1952 ((char *)uio->uio_iov->iov_base - (uio->uio_offset - off));
1953 dpEnd = (struct dirent *) uio->uio_iov->iov_base;
1954 for (dp = dpStart, ncookies = 0;
1956 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen))
1958 cookies = malloc(ncookies * sizeof(u_long), M_TEMP,
1960 for (dp = dpStart, cookiep = cookies;
1962 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
1963 off += dp->d_reclen;
1964 *cookiep++ = (u_long) off;
1966 *ap->a_ncookies = ncookies;
1967 *ap->a_cookies = cookies;
1969 uio->uio_resid += lost;
1971 *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
1976 * Return target name of a symbolic link
1980 struct vop_readlink_args /* {
1983 struct ucred *a_cred;
1986 struct vnode *vp = ap->a_vp;
1987 struct inode *ip = VTOI(vp);
1991 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1992 DIP(ip, i_blocks) == 0) { /* XXX - for old fastlink support */
1993 return (uiomove(SHORTLINK(ip), isize, ap->a_uio));
1995 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1999 * Calculate the logical to physical mapping if not done already,
2000 * then call the device strategy routine.
2002 * In order to be able to swap to a file, the ufs_bmaparray() operation may not
2003 * deadlock on memory. See ufs_bmap() for details.
2007 struct vop_strategy_args /* {
2012 struct buf *bp = ap->a_bp;
2013 struct vnode *vp = ap->a_vp;
2020 if (bp->b_blkno == bp->b_lblkno) {
2021 error = ufs_bmaparray(vp, bp->b_lblkno, &blkno, bp, NULL, NULL);
2022 bp->b_blkno = blkno;
2024 bp->b_error = error;
2025 bp->b_ioflags |= BIO_ERROR;
2029 if ((long)bp->b_blkno == -1)
2032 if ((long)bp->b_blkno == -1) {
2036 bp->b_iooffset = dbtob(bp->b_blkno);
2037 bo = ip->i_umbufobj;
2038 BO_STRATEGY(bo, bp);
2043 * Print out the contents of an inode.
2047 struct vop_print_args /* {
2051 struct vnode *vp = ap->a_vp;
2052 struct inode *ip = VTOI(vp);
2054 printf("\tino %lu, on dev %s", (u_long)ip->i_number,
2055 devtoname(ip->i_dev));
2056 if (vp->v_type == VFIFO)
2063 * Close wrapper for fifos.
2065 * Update the times on the inode then do device close.
2069 struct vop_close_args /* {
2072 struct ucred *a_cred;
2073 struct thread *a_td;
2076 struct vnode *vp = ap->a_vp;
2080 usecount = vp->v_usecount;
2082 ufs_itimes_locked(vp);
2084 return (fifo_specops.vop_close(ap));
2088 * Kqfilter wrapper for fifos.
2090 * Fall through to ufs kqfilter routines if needed
2093 ufsfifo_kqfilter(ap)
2094 struct vop_kqfilter_args *ap;
2098 error = fifo_specops.vop_kqfilter(ap);
2100 error = vfs_kqfilter(ap);
2105 * Return POSIX pathconf information applicable to fifos.
2108 ufsfifo_pathconf(ap)
2109 struct vop_pathconf_args /* {
2116 switch (ap->a_name) {
2117 case _PC_ACL_EXTENDED:
2118 case _PC_ACL_PATH_MAX:
2119 case _PC_MAC_PRESENT:
2120 return (ufs_pathconf(ap));
2122 return (fifo_specops.vop_pathconf(ap));
2128 * Return POSIX pathconf information applicable to ufs filesystems.
2132 struct vop_pathconf_args /* {
2141 switch (ap->a_name) {
2143 *ap->a_retval = LINK_MAX;
2146 *ap->a_retval = NAME_MAX;
2149 *ap->a_retval = PATH_MAX;
2152 *ap->a_retval = PIPE_BUF;
2154 case _PC_CHOWN_RESTRICTED:
2160 case _PC_ACL_EXTENDED:
2162 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
2170 case _PC_ACL_PATH_MAX:
2172 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
2173 *ap->a_retval = ACL_MAX_ENTRIES;
2180 case _PC_MAC_PRESENT:
2182 if (ap->a_vp->v_mount->mnt_flag & MNT_MULTILABEL)
2191 /* _PC_ASYNC_IO should have been handled by upper layers. */
2192 KASSERT(0, ("_PC_ASYNC_IO should not get here"));
2201 case _PC_ALLOC_SIZE_MIN:
2202 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
2204 case _PC_FILESIZEBITS:
2207 case _PC_REC_INCR_XFER_SIZE:
2208 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2210 case _PC_REC_MAX_XFER_SIZE:
2211 *ap->a_retval = -1; /* means ``unlimited'' */
2213 case _PC_REC_MIN_XFER_SIZE:
2214 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2216 case _PC_REC_XFER_ALIGN:
2217 *ap->a_retval = PAGE_SIZE;
2219 case _PC_SYMLINK_MAX:
2220 *ap->a_retval = MAXPATHLEN;
2231 * Initialize the vnode associated with a new inode, handle aliased
2235 ufs_vinit(mntp, fifoops, vpp)
2237 struct vop_vector *fifoops;
2245 vp->v_type = IFTOVT(ip->i_mode);
2246 if (vp->v_type == VFIFO)
2248 ASSERT_VOP_LOCKED(vp, "ufs_vinit");
2249 if (ip->i_number == ROOTINO)
2250 vp->v_vflag |= VV_ROOT;
2256 * Allocate a new inode.
2257 * Vnode dvp must be locked.
2260 ufs_makeinode(mode, dvp, vpp, cnp)
2264 struct componentname *cnp;
2266 struct inode *ip, *pdir;
2267 struct direct newdir;
2276 if ((cnp->cn_flags & HASBUF) == 0)
2277 panic("ufs_makeinode: no name");
2280 if ((mode & IFMT) == 0)
2283 error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2287 ip->i_gid = pdir->i_gid;
2288 DIP_SET(ip, i_gid, pdir->i_gid);
2292 struct ucred ucred, *ucp;
2297 * If we are not the owner of the directory,
2298 * and we are hacking owners here, (only do this where told to)
2299 * and we are not giving it TO root, (would subvert quotas)
2300 * then go ahead and give it to the other user.
2301 * Note that this drops off the execute bits for security.
2303 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2304 (pdir->i_mode & ISUID) &&
2305 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2306 ip->i_uid = pdir->i_uid;
2307 DIP_SET(ip, i_uid, ip->i_uid);
2311 * Make sure the correct user gets charged
2313 * Quickly knock up a dummy credential for the victim.
2314 * XXX This seems to never be accessed out of our
2315 * context so a stack variable is ok.
2317 refcount_init(&ucred.cr_ref, 1);
2318 ucred.cr_uid = ip->i_uid;
2319 ucred.cr_ngroups = 1;
2320 ucred.cr_groups = &ucred_group;
2321 ucred.cr_groups[0] = pdir->i_gid;
2325 ip->i_uid = cnp->cn_cred->cr_uid;
2326 DIP_SET(ip, i_uid, ip->i_uid);
2330 if ((error = getinoquota(ip)) ||
2331 (error = chkiq(ip, 1, ucp, 0))) {
2332 UFS_VFREE(tvp, ip->i_number, mode);
2338 #else /* !SUIDDIR */
2339 ip->i_uid = cnp->cn_cred->cr_uid;
2340 DIP_SET(ip, i_uid, ip->i_uid);
2342 if ((error = getinoquota(ip)) ||
2343 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2344 UFS_VFREE(tvp, ip->i_number, mode);
2349 #endif /* !SUIDDIR */
2350 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2353 if ((dvp->v_mount->mnt_flag & MNT_ACLS) != 0) {
2354 acl = acl_alloc(M_WAITOK);
2357 * Retrieve default ACL for parent, if any.
2359 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cnp->cn_cred,
2364 * Retrieved a default ACL, so merge mode and ACL if
2367 if (acl->acl_cnt != 0) {
2369 * Two possible ways for default ACL to not
2370 * be present. First, the EA can be
2371 * undefined, or second, the default ACL can
2372 * be blank. If it's blank, fall through to
2373 * the it's not defined case.
2375 mode = acl_posix1e_newfilemode(mode, acl);
2377 DIP_SET(ip, i_mode, mode);
2378 ufs_sync_acl_from_inode(ip, acl);
2385 * Just use the mode as-is.
2388 DIP_SET(ip, i_mode, mode);
2394 UFS_VFREE(tvp, ip->i_number, mode);
2403 DIP_SET(ip, i_mode, mode);
2407 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2410 DIP_SET(ip, i_nlink, 1);
2411 if (DOINGSOFTDEP(tvp))
2412 softdep_change_linkcnt(ip);
2413 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2414 priv_check_cred(cnp->cn_cred, PRIV_VFS_SETGID, 0)) {
2415 ip->i_mode &= ~ISGID;
2416 DIP_SET(ip, i_mode, ip->i_mode);
2419 if (cnp->cn_flags & ISWHITEOUT) {
2420 ip->i_flags |= UF_OPAQUE;
2421 DIP_SET(ip, i_flags, ip->i_flags);
2425 * Make sure inode goes to disk before directory entry.
2427 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2431 if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
2432 error = mac_vnode_create_extattr(cnp->cn_cred, dvp->v_mount,
2441 * XXX: If we abort now, will Soft Updates notify the extattr
2442 * code that the EAs for the file need to be released?
2444 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cnp->cn_cred,
2452 * XXX: This should not happen, as EOPNOTSUPP above was
2453 * supposed to free acl.
2455 printf("ufs_makeinode: VOP_GETACL() but no "
2457 /* panic("ufs_makeinode: VOP_GETACL() but no "
2467 #endif /* !UFS_ACL */
2468 ufs_makedirentry(ip, cnp, &newdir);
2469 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2477 * Write error occurred trying to update the inode
2478 * or the directory so must deallocate the inode.
2482 DIP_SET(ip, i_nlink, 0);
2483 ip->i_flag |= IN_CHANGE;
2484 if (DOINGSOFTDEP(tvp))
2485 softdep_change_linkcnt(ip);
2490 /* Global vfs data structures for ufs. */
2491 struct vop_vector ufs_vnodeops = {
2492 .vop_default = &default_vnodeops,
2493 .vop_fsync = VOP_PANIC,
2494 .vop_read = VOP_PANIC,
2495 .vop_reallocblks = VOP_PANIC,
2496 .vop_write = VOP_PANIC,
2497 .vop_access = ufs_access,
2498 .vop_bmap = ufs_bmap,
2499 .vop_cachedlookup = ufs_lookup,
2500 .vop_close = ufs_close,
2501 .vop_create = ufs_create,
2502 .vop_getattr = ufs_getattr,
2503 .vop_inactive = ufs_inactive,
2504 .vop_link = ufs_link,
2505 .vop_lookup = vfs_cache_lookup,
2506 .vop_markatime = ufs_markatime,
2507 .vop_mkdir = ufs_mkdir,
2508 .vop_mknod = ufs_mknod,
2509 .vop_open = ufs_open,
2510 .vop_pathconf = ufs_pathconf,
2511 .vop_poll = vop_stdpoll,
2512 .vop_print = ufs_print,
2513 .vop_readdir = ufs_readdir,
2514 .vop_readlink = ufs_readlink,
2515 .vop_reclaim = ufs_reclaim,
2516 .vop_remove = ufs_remove,
2517 .vop_rename = ufs_rename,
2518 .vop_rmdir = ufs_rmdir,
2519 .vop_setattr = ufs_setattr,
2521 .vop_setlabel = vop_stdsetlabel_ea,
2523 .vop_strategy = ufs_strategy,
2524 .vop_symlink = ufs_symlink,
2525 .vop_whiteout = ufs_whiteout,
2527 .vop_getextattr = ufs_getextattr,
2528 .vop_deleteextattr = ufs_deleteextattr,
2529 .vop_setextattr = ufs_setextattr,
2532 .vop_getacl = ufs_getacl,
2533 .vop_setacl = ufs_setacl,
2534 .vop_aclcheck = ufs_aclcheck,
2538 struct vop_vector ufs_fifoops = {
2539 .vop_default = &fifo_specops,
2540 .vop_fsync = VOP_PANIC,
2541 .vop_access = ufs_access,
2542 .vop_close = ufsfifo_close,
2543 .vop_getattr = ufs_getattr,
2544 .vop_inactive = ufs_inactive,
2545 .vop_kqfilter = ufsfifo_kqfilter,
2546 .vop_markatime = ufs_markatime,
2547 .vop_pathconf = ufsfifo_pathconf,
2548 .vop_print = ufs_print,
2549 .vop_read = VOP_PANIC,
2550 .vop_reclaim = ufs_reclaim,
2551 .vop_setattr = ufs_setattr,
2553 .vop_setlabel = vop_stdsetlabel_ea,
2555 .vop_write = VOP_PANIC,
2557 .vop_getextattr = ufs_getextattr,
2558 .vop_deleteextattr = ufs_deleteextattr,
2559 .vop_setextattr = ufs_setextattr,
2562 .vop_getacl = ufs_getacl,
2563 .vop_setacl = ufs_setacl,
2564 .vop_aclcheck = ufs_aclcheck,