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;
1456 * If we are hacking owners here, (only do this where told to)
1457 * and we are not giving it TO root, (would subvert quotas)
1458 * then go ahead and give it to the other user.
1459 * The new directory also inherits the SUID bit.
1460 * If user's UID and dir UID are the same,
1461 * 'give it away' so that the SUID is still forced on.
1463 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1464 (dp->i_mode & ISUID) && dp->i_uid) {
1466 ip->i_uid = dp->i_uid;
1467 DIP_SET(ip, i_uid, dp->i_uid);
1469 if (dp->i_uid != cnp->cn_cred->cr_uid) {
1471 * Make sure the correct user gets charged
1473 * Make a dummy credential for the victim.
1474 * XXX This seems to never be accessed out of
1475 * our context so a stack variable is ok.
1477 refcount_init(&ucred.cr_ref, 1);
1478 ucred.cr_uid = ip->i_uid;
1479 ucred.cr_ngroups = 1;
1480 ucred.cr_groups = &ucred_group;
1481 ucred.cr_groups[0] = dp->i_gid;
1486 ip->i_uid = cnp->cn_cred->cr_uid;
1487 DIP_SET(ip, i_uid, ip->i_uid);
1490 if ((error = getinoquota(ip)) ||
1491 (error = chkiq(ip, 1, ucp, 0))) {
1492 UFS_VFREE(tvp, ip->i_number, dmode);
1498 #else /* !SUIDDIR */
1499 ip->i_uid = cnp->cn_cred->cr_uid;
1500 DIP_SET(ip, i_uid, ip->i_uid);
1502 if ((error = getinoquota(ip)) ||
1503 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1504 UFS_VFREE(tvp, ip->i_number, dmode);
1509 #endif /* !SUIDDIR */
1510 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1513 if ((dvp->v_mount->mnt_flag & MNT_ACLS) != 0) {
1514 acl = acl_alloc(M_WAITOK);
1515 dacl = acl_alloc(M_WAITOK);
1518 * Retrieve default ACL from parent, if any.
1520 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cnp->cn_cred,
1525 * Retrieved a default ACL, so merge mode and ACL if
1526 * necessary. If the ACL is empty, fall through to
1527 * the "not defined or available" case.
1529 if (acl->acl_cnt != 0) {
1530 dmode = acl_posix1e_newfilemode(dmode, acl);
1532 DIP_SET(ip, i_mode, dmode);
1534 ufs_sync_acl_from_inode(ip, acl);
1541 * Just use the mode as-is.
1544 DIP_SET(ip, i_mode, dmode);
1551 UFS_VFREE(tvp, ip->i_number, dmode);
1558 #endif /* !UFS_ACL */
1560 DIP_SET(ip, i_mode, dmode);
1564 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1567 DIP_SET(ip, i_nlink, 2);
1568 if (DOINGSOFTDEP(tvp))
1569 softdep_change_linkcnt(ip);
1570 if (cnp->cn_flags & ISWHITEOUT) {
1571 ip->i_flags |= UF_OPAQUE;
1572 DIP_SET(ip, i_flags, ip->i_flags);
1576 * Bump link count in parent directory to reflect work done below.
1577 * Should be done before reference is created so cleanup is
1578 * possible if we crash.
1582 DIP_SET(dp, i_nlink, dp->i_nlink);
1583 dp->i_flag |= IN_CHANGE;
1584 if (DOINGSOFTDEP(dvp))
1585 softdep_change_linkcnt(dp);
1586 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1590 if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
1591 error = mac_vnode_create_extattr(cnp->cn_cred, dvp->v_mount,
1600 * XXX: If we abort now, will Soft Updates notify the extattr
1601 * code that the EAs for the file need to be released?
1603 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cnp->cn_cred,
1606 error = VOP_SETACL(tvp, ACL_TYPE_DEFAULT, dacl,
1607 cnp->cn_cred, cnp->cn_thread);
1614 * XXX: This should not happen, as EOPNOTSUPP above
1615 * was supposed to free acl.
1617 printf("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()\n");
1619 panic("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()");
1633 #endif /* !UFS_ACL */
1636 * Initialize directory with "." and ".." from static template.
1638 if (dvp->v_mount->mnt_maxsymlinklen > 0)
1639 dtp = &mastertemplate;
1641 dtp = (struct dirtemplate *)&omastertemplate;
1643 dirtemplate.dot_ino = ip->i_number;
1644 dirtemplate.dotdot_ino = dp->i_number;
1645 if ((error = UFS_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred,
1646 BA_CLRBUF, &bp)) != 0)
1648 ip->i_size = DIRBLKSIZ;
1649 DIP_SET(ip, i_size, DIRBLKSIZ);
1650 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1651 vnode_pager_setsize(tvp, (u_long)ip->i_size);
1652 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1653 if (DOINGSOFTDEP(tvp)) {
1655 * Ensure that the entire newly allocated block is a
1656 * valid directory so that future growth within the
1657 * block does not have to ensure that the block is
1658 * written before the inode.
1661 while (blkoff < bp->b_bcount) {
1663 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1664 blkoff += DIRBLKSIZ;
1667 if ((error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) |
1668 DOINGASYNC(tvp)))) != 0) {
1673 * Directory set up, now install its entry in the parent directory.
1675 * If we are not doing soft dependencies, then we must write out the
1676 * buffer containing the new directory body before entering the new
1677 * name in the parent. If we are doing soft dependencies, then the
1678 * buffer containing the new directory body will be passed to and
1679 * released in the soft dependency code after the code has attached
1680 * an appropriate ordering dependency to the buffer which ensures that
1681 * the buffer is written before the new name is written in the parent.
1683 if (DOINGASYNC(dvp))
1685 else if (!DOINGSOFTDEP(dvp) && ((error = bwrite(bp))))
1687 ufs_makedirentry(ip, cnp, &newdir);
1688 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1702 DIP_SET(dp, i_nlink, dp->i_nlink);
1703 dp->i_flag |= IN_CHANGE;
1704 if (DOINGSOFTDEP(dvp))
1705 softdep_change_linkcnt(dp);
1707 * No need to do an explicit VOP_TRUNCATE here, vrele will
1708 * do this for us because we set the link count to 0.
1712 DIP_SET(ip, i_nlink, 0);
1713 ip->i_flag |= IN_CHANGE;
1714 if (DOINGSOFTDEP(tvp))
1715 softdep_change_linkcnt(ip);
1723 * Rmdir system call.
1727 struct vop_rmdir_args /* {
1728 struct vnode *a_dvp;
1730 struct componentname *a_cnp;
1733 struct vnode *vp = ap->a_vp;
1734 struct vnode *dvp = ap->a_dvp;
1735 struct componentname *cnp = ap->a_cnp;
1736 struct inode *ip, *dp;
1743 * Do not remove a directory that is in the process of being renamed.
1744 * Verify the directory is empty (and valid). Rmdir ".." will not be
1745 * valid since ".." will contain a reference to the current directory
1746 * and thus be non-empty. Do not allow the removal of mounted on
1747 * directories (this can happen when an NFS exported filesystem
1748 * tries to remove a locally mounted on directory).
1751 if ((ip->i_flag & IN_RENAME) || ip->i_effnlink < 2) {
1755 if (!ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1759 if ((dp->i_flags & APPEND)
1760 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1764 if (vp->v_mountedhere != 0) {
1769 ufs_gjournal_orphan(vp);
1772 * Delete reference to directory before purging
1773 * inode. If we crash in between, the directory
1774 * will be reattached to lost+found,
1778 if (DOINGSOFTDEP(vp)) {
1779 softdep_change_linkcnt(dp);
1780 softdep_change_linkcnt(ip);
1782 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1786 if (DOINGSOFTDEP(vp)) {
1787 softdep_change_linkcnt(dp);
1788 softdep_change_linkcnt(ip);
1794 * Truncate inode. The only stuff left in the directory is "." and
1795 * "..". The "." reference is inconsequential since we are quashing
1796 * it. The soft dependency code will arrange to do these operations
1797 * after the parent directory entry has been deleted on disk, so
1798 * when running with that code we avoid doing them now.
1800 if (!DOINGSOFTDEP(vp)) {
1802 DIP_SET(dp, i_nlink, dp->i_nlink);
1803 dp->i_flag |= IN_CHANGE;
1805 DIP_SET(ip, i_nlink, ip->i_nlink);
1806 ip->i_flag |= IN_CHANGE;
1808 if (!DOINGASYNC(vp))
1810 error = UFS_TRUNCATE(vp, (off_t)0, ioflag, cnp->cn_cred,
1815 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1816 if (ip->i_dirhash != NULL)
1817 ufsdirhash_free(ip);
1824 * symlink -- make a symbolic link
1828 struct vop_symlink_args /* {
1829 struct vnode *a_dvp;
1830 struct vnode **a_vpp;
1831 struct componentname *a_cnp;
1832 struct vattr *a_vap;
1836 struct vnode *vp, **vpp = ap->a_vpp;
1840 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1845 len = strlen(ap->a_target);
1846 if (len < vp->v_mount->mnt_maxsymlinklen) {
1848 bcopy(ap->a_target, SHORTLINK(ip), len);
1850 DIP_SET(ip, i_size, len);
1851 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1853 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1854 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1855 ap->a_cnp->cn_cred, NOCRED, NULL, NULL);
1862 * Vnode op for reading directories.
1864 * The routine below assumes that the on-disk format of a directory
1865 * is the same as that defined by <sys/dirent.h>. If the on-disk
1866 * format changes, then it will be necessary to do a conversion
1867 * from the on-disk format that read returns to the format defined
1868 * by <sys/dirent.h>.
1872 struct vop_readdir_args /* {
1875 struct ucred *a_cred;
1881 struct uio *uio = ap->a_uio;
1886 if (ap->a_ncookies != NULL)
1888 * Ensure that the block is aligned. The caller can use
1889 * the cookies to determine where in the block to start.
1891 uio->uio_offset &= ~(DIRBLKSIZ - 1);
1892 off = uio->uio_offset;
1893 count = uio->uio_resid;
1894 /* Make sure we don't return partial entries. */
1895 if (count <= ((uio->uio_offset + count) & (DIRBLKSIZ -1)))
1897 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
1898 lost = uio->uio_resid - count;
1899 uio->uio_resid = count;
1900 uio->uio_iov->iov_len = count;
1901 # if (BYTE_ORDER == LITTLE_ENDIAN)
1902 if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) {
1903 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1905 struct dirent *dp, *edp;
1913 auio.uio_iov = &aiov;
1914 auio.uio_iovcnt = 1;
1915 auio.uio_segflg = UIO_SYSSPACE;
1916 aiov.iov_len = count;
1917 dirbuf = malloc(count, M_TEMP, M_WAITOK);
1918 aiov.iov_base = dirbuf;
1919 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
1921 readcnt = count - auio.uio_resid;
1922 edp = (struct dirent *)&dirbuf[readcnt];
1923 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
1925 dp->d_namlen = dp->d_type;
1927 if (dp->d_reclen > 0) {
1928 dp = (struct dirent *)
1929 ((char *)dp + dp->d_reclen);
1936 error = uiomove(dirbuf, readcnt, uio);
1938 free(dirbuf, M_TEMP);
1941 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1943 if (!error && ap->a_ncookies != NULL) {
1944 struct dirent* dpStart;
1945 struct dirent* dpEnd;
1951 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1952 panic("ufs_readdir: unexpected uio from NFS server");
1953 dpStart = (struct dirent *)
1954 ((char *)uio->uio_iov->iov_base - (uio->uio_offset - off));
1955 dpEnd = (struct dirent *) uio->uio_iov->iov_base;
1956 for (dp = dpStart, ncookies = 0;
1958 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen))
1960 cookies = malloc(ncookies * sizeof(u_long), M_TEMP,
1962 for (dp = dpStart, cookiep = cookies;
1964 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
1965 off += dp->d_reclen;
1966 *cookiep++ = (u_long) off;
1968 *ap->a_ncookies = ncookies;
1969 *ap->a_cookies = cookies;
1971 uio->uio_resid += lost;
1973 *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
1978 * Return target name of a symbolic link
1982 struct vop_readlink_args /* {
1985 struct ucred *a_cred;
1988 struct vnode *vp = ap->a_vp;
1989 struct inode *ip = VTOI(vp);
1993 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1994 DIP(ip, i_blocks) == 0) { /* XXX - for old fastlink support */
1995 return (uiomove(SHORTLINK(ip), isize, ap->a_uio));
1997 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
2001 * Calculate the logical to physical mapping if not done already,
2002 * then call the device strategy routine.
2004 * In order to be able to swap to a file, the ufs_bmaparray() operation may not
2005 * deadlock on memory. See ufs_bmap() for details.
2009 struct vop_strategy_args /* {
2014 struct buf *bp = ap->a_bp;
2015 struct vnode *vp = ap->a_vp;
2022 if (bp->b_blkno == bp->b_lblkno) {
2023 error = ufs_bmaparray(vp, bp->b_lblkno, &blkno, bp, NULL, NULL);
2024 bp->b_blkno = blkno;
2026 bp->b_error = error;
2027 bp->b_ioflags |= BIO_ERROR;
2031 if ((long)bp->b_blkno == -1)
2034 if ((long)bp->b_blkno == -1) {
2038 bp->b_iooffset = dbtob(bp->b_blkno);
2039 bo = ip->i_umbufobj;
2040 BO_STRATEGY(bo, bp);
2045 * Print out the contents of an inode.
2049 struct vop_print_args /* {
2053 struct vnode *vp = ap->a_vp;
2054 struct inode *ip = VTOI(vp);
2056 printf("\tino %lu, on dev %s", (u_long)ip->i_number,
2057 devtoname(ip->i_dev));
2058 if (vp->v_type == VFIFO)
2065 * Close wrapper for fifos.
2067 * Update the times on the inode then do device close.
2071 struct vop_close_args /* {
2074 struct ucred *a_cred;
2075 struct thread *a_td;
2078 struct vnode *vp = ap->a_vp;
2082 usecount = vp->v_usecount;
2084 ufs_itimes_locked(vp);
2086 return (fifo_specops.vop_close(ap));
2090 * Kqfilter wrapper for fifos.
2092 * Fall through to ufs kqfilter routines if needed
2095 ufsfifo_kqfilter(ap)
2096 struct vop_kqfilter_args *ap;
2100 error = fifo_specops.vop_kqfilter(ap);
2102 error = vfs_kqfilter(ap);
2107 * Return POSIX pathconf information applicable to fifos.
2110 ufsfifo_pathconf(ap)
2111 struct vop_pathconf_args /* {
2118 switch (ap->a_name) {
2119 case _PC_ACL_EXTENDED:
2120 case _PC_ACL_PATH_MAX:
2121 case _PC_MAC_PRESENT:
2122 return (ufs_pathconf(ap));
2124 return (fifo_specops.vop_pathconf(ap));
2130 * Return POSIX pathconf information applicable to ufs filesystems.
2134 struct vop_pathconf_args /* {
2143 switch (ap->a_name) {
2145 *ap->a_retval = LINK_MAX;
2148 *ap->a_retval = NAME_MAX;
2151 *ap->a_retval = PATH_MAX;
2154 *ap->a_retval = PIPE_BUF;
2156 case _PC_CHOWN_RESTRICTED:
2162 case _PC_ACL_EXTENDED:
2164 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
2172 case _PC_ACL_PATH_MAX:
2174 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
2175 *ap->a_retval = ACL_MAX_ENTRIES;
2182 case _PC_MAC_PRESENT:
2184 if (ap->a_vp->v_mount->mnt_flag & MNT_MULTILABEL)
2193 /* _PC_ASYNC_IO should have been handled by upper layers. */
2194 KASSERT(0, ("_PC_ASYNC_IO should not get here"));
2203 case _PC_ALLOC_SIZE_MIN:
2204 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
2206 case _PC_FILESIZEBITS:
2209 case _PC_REC_INCR_XFER_SIZE:
2210 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2212 case _PC_REC_MAX_XFER_SIZE:
2213 *ap->a_retval = -1; /* means ``unlimited'' */
2215 case _PC_REC_MIN_XFER_SIZE:
2216 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2218 case _PC_REC_XFER_ALIGN:
2219 *ap->a_retval = PAGE_SIZE;
2221 case _PC_SYMLINK_MAX:
2222 *ap->a_retval = MAXPATHLEN;
2233 * Initialize the vnode associated with a new inode, handle aliased
2237 ufs_vinit(mntp, fifoops, vpp)
2239 struct vop_vector *fifoops;
2247 vp->v_type = IFTOVT(ip->i_mode);
2248 if (vp->v_type == VFIFO)
2250 ASSERT_VOP_LOCKED(vp, "ufs_vinit");
2251 if (ip->i_number == ROOTINO)
2252 vp->v_vflag |= VV_ROOT;
2258 * Allocate a new inode.
2259 * Vnode dvp must be locked.
2262 ufs_makeinode(mode, dvp, vpp, cnp)
2266 struct componentname *cnp;
2268 struct inode *ip, *pdir;
2269 struct direct newdir;
2278 if ((cnp->cn_flags & HASBUF) == 0)
2279 panic("ufs_makeinode: no name");
2282 if ((mode & IFMT) == 0)
2285 error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2289 ip->i_gid = pdir->i_gid;
2290 DIP_SET(ip, i_gid, pdir->i_gid);
2294 struct ucred ucred, *ucp;
2299 * If we are not the owner of the directory,
2300 * and we are hacking owners here, (only do this where told to)
2301 * and we are not giving it TO root, (would subvert quotas)
2302 * then go ahead and give it to the other user.
2303 * Note that this drops off the execute bits for security.
2305 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2306 (pdir->i_mode & ISUID) &&
2307 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2308 ip->i_uid = pdir->i_uid;
2309 DIP_SET(ip, i_uid, ip->i_uid);
2313 * Make sure the correct user gets charged
2315 * Quickly knock up a dummy credential for the victim.
2316 * XXX This seems to never be accessed out of our
2317 * context so a stack variable is ok.
2319 refcount_init(&ucred.cr_ref, 1);
2320 ucred.cr_uid = ip->i_uid;
2321 ucred.cr_ngroups = 1;
2322 ucred.cr_groups = &ucred_group;
2323 ucred.cr_groups[0] = pdir->i_gid;
2327 ip->i_uid = cnp->cn_cred->cr_uid;
2328 DIP_SET(ip, i_uid, ip->i_uid);
2332 if ((error = getinoquota(ip)) ||
2333 (error = chkiq(ip, 1, ucp, 0))) {
2334 UFS_VFREE(tvp, ip->i_number, mode);
2340 #else /* !SUIDDIR */
2341 ip->i_uid = cnp->cn_cred->cr_uid;
2342 DIP_SET(ip, i_uid, ip->i_uid);
2344 if ((error = getinoquota(ip)) ||
2345 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2346 UFS_VFREE(tvp, ip->i_number, mode);
2351 #endif /* !SUIDDIR */
2352 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2355 if ((dvp->v_mount->mnt_flag & MNT_ACLS) != 0) {
2356 acl = acl_alloc(M_WAITOK);
2359 * Retrieve default ACL for parent, if any.
2361 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cnp->cn_cred,
2366 * Retrieved a default ACL, so merge mode and ACL if
2369 if (acl->acl_cnt != 0) {
2371 * Two possible ways for default ACL to not
2372 * be present. First, the EA can be
2373 * undefined, or second, the default ACL can
2374 * be blank. If it's blank, fall through to
2375 * the it's not defined case.
2377 mode = acl_posix1e_newfilemode(mode, acl);
2379 DIP_SET(ip, i_mode, mode);
2380 ufs_sync_acl_from_inode(ip, acl);
2387 * Just use the mode as-is.
2390 DIP_SET(ip, i_mode, mode);
2396 UFS_VFREE(tvp, ip->i_number, mode);
2405 DIP_SET(ip, i_mode, mode);
2409 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2412 DIP_SET(ip, i_nlink, 1);
2413 if (DOINGSOFTDEP(tvp))
2414 softdep_change_linkcnt(ip);
2415 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2416 priv_check_cred(cnp->cn_cred, PRIV_VFS_SETGID, 0)) {
2417 ip->i_mode &= ~ISGID;
2418 DIP_SET(ip, i_mode, ip->i_mode);
2421 if (cnp->cn_flags & ISWHITEOUT) {
2422 ip->i_flags |= UF_OPAQUE;
2423 DIP_SET(ip, i_flags, ip->i_flags);
2427 * Make sure inode goes to disk before directory entry.
2429 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2433 if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
2434 error = mac_vnode_create_extattr(cnp->cn_cred, dvp->v_mount,
2443 * XXX: If we abort now, will Soft Updates notify the extattr
2444 * code that the EAs for the file need to be released?
2446 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cnp->cn_cred,
2454 * XXX: This should not happen, as EOPNOTSUPP above was
2455 * supposed to free acl.
2457 printf("ufs_makeinode: VOP_GETACL() but no "
2459 /* panic("ufs_makeinode: VOP_GETACL() but no "
2469 #endif /* !UFS_ACL */
2470 ufs_makedirentry(ip, cnp, &newdir);
2471 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2479 * Write error occurred trying to update the inode
2480 * or the directory so must deallocate the inode.
2484 DIP_SET(ip, i_nlink, 0);
2485 ip->i_flag |= IN_CHANGE;
2486 if (DOINGSOFTDEP(tvp))
2487 softdep_change_linkcnt(ip);
2492 /* Global vfs data structures for ufs. */
2493 struct vop_vector ufs_vnodeops = {
2494 .vop_default = &default_vnodeops,
2495 .vop_fsync = VOP_PANIC,
2496 .vop_read = VOP_PANIC,
2497 .vop_reallocblks = VOP_PANIC,
2498 .vop_write = VOP_PANIC,
2499 .vop_access = ufs_access,
2500 .vop_bmap = ufs_bmap,
2501 .vop_cachedlookup = ufs_lookup,
2502 .vop_close = ufs_close,
2503 .vop_create = ufs_create,
2504 .vop_getattr = ufs_getattr,
2505 .vop_inactive = ufs_inactive,
2506 .vop_link = ufs_link,
2507 .vop_lookup = vfs_cache_lookup,
2508 .vop_markatime = ufs_markatime,
2509 .vop_mkdir = ufs_mkdir,
2510 .vop_mknod = ufs_mknod,
2511 .vop_open = ufs_open,
2512 .vop_pathconf = ufs_pathconf,
2513 .vop_poll = vop_stdpoll,
2514 .vop_print = ufs_print,
2515 .vop_readdir = ufs_readdir,
2516 .vop_readlink = ufs_readlink,
2517 .vop_reclaim = ufs_reclaim,
2518 .vop_remove = ufs_remove,
2519 .vop_rename = ufs_rename,
2520 .vop_rmdir = ufs_rmdir,
2521 .vop_setattr = ufs_setattr,
2523 .vop_setlabel = vop_stdsetlabel_ea,
2525 .vop_strategy = ufs_strategy,
2526 .vop_symlink = ufs_symlink,
2527 .vop_whiteout = ufs_whiteout,
2529 .vop_getextattr = ufs_getextattr,
2530 .vop_deleteextattr = ufs_deleteextattr,
2531 .vop_setextattr = ufs_setextattr,
2534 .vop_getacl = ufs_getacl,
2535 .vop_setacl = ufs_setacl,
2536 .vop_aclcheck = ufs_aclcheck,
2540 struct vop_vector ufs_fifoops = {
2541 .vop_default = &fifo_specops,
2542 .vop_fsync = VOP_PANIC,
2543 .vop_access = ufs_access,
2544 .vop_close = ufsfifo_close,
2545 .vop_getattr = ufs_getattr,
2546 .vop_inactive = ufs_inactive,
2547 .vop_kqfilter = ufsfifo_kqfilter,
2548 .vop_markatime = ufs_markatime,
2549 .vop_pathconf = ufsfifo_pathconf,
2550 .vop_print = ufs_print,
2551 .vop_read = VOP_PANIC,
2552 .vop_reclaim = ufs_reclaim,
2553 .vop_setattr = ufs_setattr,
2555 .vop_setlabel = vop_stdsetlabel_ea,
2557 .vop_write = VOP_PANIC,
2559 .vop_getextattr = ufs_getextattr,
2560 .vop_deleteextattr = ufs_deleteextattr,
2561 .vop_setextattr = ufs_setextattr,
2564 .vop_getacl = ufs_getacl,
2565 .vop_setacl = ufs_setacl,
2566 .vop_aclcheck = ufs_aclcheck,