2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1989, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include "opt_quota.h"
43 #include "opt_suiddir.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
50 #include <sys/namei.h>
51 #include <sys/kernel.h>
52 #include <sys/fcntl.h>
53 #include <sys/filio.h>
57 #include <sys/mount.h>
59 #include <sys/refcount.h>
60 #include <sys/unistd.h>
61 #include <sys/vnode.h>
62 #include <sys/dirent.h>
63 #include <sys/lockf.h>
68 #include <security/audit/audit.h>
69 #include <security/mac/mac_framework.h>
71 #include <sys/file.h> /* XXX */
74 #include <vm/vm_extern.h>
76 #include <ufs/ufs/acl.h>
77 #include <ufs/ufs/extattr.h>
78 #include <ufs/ufs/quota.h>
79 #include <ufs/ufs/inode.h>
80 #include <ufs/ufs/dir.h>
81 #include <ufs/ufs/ufsmount.h>
82 #include <ufs/ufs/ufs_extern.h>
84 #include <ufs/ufs/dirhash.h>
87 #include <ufs/ufs/gjournal.h>
88 FEATURE(ufs_gjournal, "Journaling support through GEOM for UFS");
92 FEATURE(ufs_quota, "UFS disk quotas support");
93 FEATURE(ufs_quota64, "64bit UFS disk quotas support");
98 "Give all new files in directory the same ownership as the directory");
103 #include <ufs/ffs/ffs_extern.h>
105 static vop_accessx_t ufs_accessx;
106 static vop_fplookup_vexec_t ufs_fplookup_vexec;
107 static int ufs_chmod(struct vnode *, int, struct ucred *, struct thread *);
108 static int ufs_chown(struct vnode *, uid_t, gid_t, struct ucred *,
110 static vop_close_t ufs_close;
111 static vop_create_t ufs_create;
112 static vop_stat_t ufs_stat;
113 static vop_getattr_t ufs_getattr;
114 static vop_ioctl_t ufs_ioctl;
115 static vop_link_t ufs_link;
116 static int ufs_makeinode(int mode, struct vnode *, struct vnode **,
117 struct componentname *, const char *);
118 static vop_mmapped_t ufs_mmapped;
119 static vop_mkdir_t ufs_mkdir;
120 static vop_mknod_t ufs_mknod;
121 static vop_open_t ufs_open;
122 static vop_pathconf_t ufs_pathconf;
123 static vop_print_t ufs_print;
124 static vop_readlink_t ufs_readlink;
125 static vop_remove_t ufs_remove;
126 static vop_rename_t ufs_rename;
127 static vop_rmdir_t ufs_rmdir;
128 static vop_setattr_t ufs_setattr;
129 static vop_strategy_t ufs_strategy;
130 static vop_symlink_t ufs_symlink;
131 static vop_whiteout_t ufs_whiteout;
132 static vop_close_t ufsfifo_close;
133 static vop_kqfilter_t ufsfifo_kqfilter;
135 SYSCTL_NODE(_vfs, OID_AUTO, ufs, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
139 * A virgin directory (no blushing please).
141 static struct dirtemplate mastertemplate = {
142 0, 12, DT_DIR, 1, ".",
143 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
145 static struct odirtemplate omastertemplate = {
147 0, DIRBLKSIZ - 12, 2, ".."
151 ufs_itimes_locked(struct vnode *vp)
156 ASSERT_VI_LOCKED(vp, __func__);
161 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
164 if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
165 UFS_INODE_SET_FLAG(ip, IN_LAZYMOD);
166 else if (((vp->v_mount->mnt_kern_flag &
167 (MNTK_SUSPENDED | MNTK_SUSPEND)) == 0) ||
168 (ip->i_flag & (IN_CHANGE | IN_UPDATE)))
169 UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
170 else if (ip->i_flag & IN_ACCESS)
171 UFS_INODE_SET_FLAG(ip, IN_LAZYACCESS);
173 if (ip->i_flag & IN_ACCESS) {
174 DIP_SET(ip, i_atime, ts.tv_sec);
175 DIP_SET(ip, i_atimensec, ts.tv_nsec);
177 if (ip->i_flag & IN_UPDATE) {
178 DIP_SET(ip, i_mtime, ts.tv_sec);
179 DIP_SET(ip, i_mtimensec, ts.tv_nsec);
181 if (ip->i_flag & IN_CHANGE) {
182 DIP_SET(ip, i_ctime, ts.tv_sec);
183 DIP_SET(ip, i_ctimensec, ts.tv_nsec);
184 DIP_SET(ip, i_modrev, DIP(ip, i_modrev) + 1);
188 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
192 ufs_itimes(struct vnode *vp)
196 ufs_itimes_locked(vp);
201 * Create a regular file
205 struct vop_create_args /* {
207 struct vnode **a_vpp;
208 struct componentname *a_cnp;
215 ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
216 ap->a_dvp, ap->a_vpp, ap->a_cnp, "ufs_create");
219 if ((ap->a_cnp->cn_flags & MAKEENTRY) != 0)
220 cache_enter(ap->a_dvp, *ap->a_vpp, ap->a_cnp);
230 struct vop_mknod_args /* {
232 struct vnode **a_vpp;
233 struct componentname *a_cnp;
237 struct vattr *vap = ap->a_vap;
238 struct vnode **vpp = ap->a_vpp;
243 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
244 ap->a_dvp, vpp, ap->a_cnp, "ufs_mknod");
248 UFS_INODE_SET_FLAG(ip, IN_ACCESS | IN_CHANGE | IN_UPDATE);
249 if (vap->va_rdev != VNOVAL) {
251 * Want to be able to use this to make badblock
252 * inodes, so don't truncate the dev number.
254 DIP_SET(ip, i_rdev, vap->va_rdev);
257 * Remove inode, then reload it through VFS_VGET(). This is
258 * needed to do further inode initialization, for instance
259 * fifo, which was too early for VFS_VGET() done as part of
262 (*vpp)->v_type = VNON;
263 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
266 error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
279 ufs_open(struct vop_open_args *ap)
281 struct vnode *vp = ap->a_vp;
284 if (vp->v_type == VCHR || vp->v_type == VBLK)
288 vnode_create_vobject(vp, DIP(ip, i_size), ap->a_td);
289 if (vp->v_type == VREG && (vn_irflag_read(vp) & VIRF_PGREAD) == 0) {
290 vn_irflag_set_cond(vp, VIRF_PGREAD);
294 * Files marked append-only must be opened for appending.
296 if ((ip->i_flags & APPEND) &&
297 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
306 * Update the times on the inode.
311 struct vop_close_args /* {
314 struct ucred *a_cred;
318 struct vnode *vp = ap->a_vp;
322 usecount = vp->v_usecount;
324 ufs_itimes_locked(vp);
331 struct vop_accessx_args /* {
334 struct ucred *a_cred;
338 struct vnode *vp = ap->a_vp;
339 struct inode *ip = VTOI(vp);
340 accmode_t accmode = ap->a_accmode;
348 * Disallow write attempts on read-only filesystems;
349 * unless the file is a socket, fifo, or a block or
350 * character device resident on the filesystem.
352 if (accmode & VMODIFY_PERMS) {
353 switch (vp->v_type) {
357 if (vp->v_mount->mnt_flag & MNT_RDONLY)
361 * Inode is accounted in the quotas only if struct
362 * dquot is attached to it. VOP_ACCESS() is called
363 * from vn_open_cred() and provides a convenient
364 * point to call getinoquota(). The lock mode is
365 * exclusive when the file is opening for write.
367 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
368 error = getinoquota(ip);
380 * If immutable bit set, nobody gets to write it. "& ~VADMIN_PERMS"
381 * permits the owner of the file to remove the IMMUTABLE flag.
383 if ((accmode & (VMODIFY_PERMS & ~VADMIN_PERMS)) &&
384 (ip->i_flags & (IMMUTABLE | SF_SNAPSHOT)))
388 if ((vp->v_mount->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS)) != 0) {
389 if (vp->v_mount->mnt_flag & MNT_NFS4ACLS)
390 type = ACL_TYPE_NFS4;
392 type = ACL_TYPE_ACCESS;
394 acl = acl_alloc(M_WAITOK);
395 if (type == ACL_TYPE_NFS4)
396 error = ufs_getacl_nfs4_internal(vp, acl, ap->a_td);
398 error = VOP_GETACL(vp, type, acl, ap->a_cred, ap->a_td);
401 if (type == ACL_TYPE_NFS4) {
402 error = vaccess_acl_nfs4(vp->v_type, ip->i_uid,
403 ip->i_gid, acl, accmode, ap->a_cred);
405 error = vfs_unixify_accmode(&accmode);
407 error = vaccess_acl_posix1e(vp->v_type, ip->i_uid,
408 ip->i_gid, acl, accmode, ap->a_cred);
412 if (error != EOPNOTSUPP)
414 "ufs_accessx(): Error retrieving ACL on object (%d).\n",
417 * XXX: Fall back until debugged. Should
418 * eventually possibly log an error, and return
421 error = vfs_unixify_accmode(&accmode);
423 error = vaccess(vp->v_type, ip->i_mode,
424 ip->i_uid, ip->i_gid, accmode, ap->a_cred);
430 #endif /* !UFS_ACL */
431 error = vfs_unixify_accmode(&accmode);
433 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
434 accmode, ap->a_cred);
439 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
440 * the comment above cache_fplookup for details.
443 ufs_fplookup_vexec(ap)
444 struct vop_fplookup_vexec_args /* {
446 struct ucred *a_cred;
457 if (__predict_false(ip == NULL))
463 * ACLs are not supported and UFS clears/sets this flag on mount and
464 * remount. However, we may still be racing with seeing them and there
465 * is no provision to make sure they were accounted for. This matches
466 * the behavior of the locked case, since the lookup there is also
467 * racy: mount takes no measures to block anyone from progressing.
469 all_x = S_IXUSR | S_IXGRP | S_IXOTH;
470 mode = atomic_load_short(&ip->i_mode);
471 if (__predict_true((mode & all_x) == all_x))
475 return (vaccess_vexec_smr(mode, ip->i_uid, ip->i_gid, cred));
480 ufs_stat(struct vop_stat_args *ap)
482 struct vnode *vp = ap->a_vp;
483 struct inode *ip = VTOI(vp);
484 struct stat *sb = ap->a_sb;
487 error = vop_stat_helper_pre(ap);
488 if (__predict_false(error))
492 ufs_itimes_locked(vp);
494 sb->st_atim.tv_sec = ip->i_din1->di_atime;
495 sb->st_atim.tv_nsec = ip->i_din1->di_atimensec;
497 sb->st_atim.tv_sec = ip->i_din2->di_atime;
498 sb->st_atim.tv_nsec = ip->i_din2->di_atimensec;
502 sb->st_dev = dev2udev(ITOUMP(ip)->um_dev);
503 sb->st_ino = ip->i_number;
504 sb->st_mode = (ip->i_mode & ~IFMT) | VTTOIF(vp->v_type);
505 sb->st_nlink = ip->i_effnlink;
506 sb->st_uid = ip->i_uid;
507 sb->st_gid = ip->i_gid;
509 sb->st_rdev = ip->i_din1->di_rdev;
510 sb->st_size = ip->i_din1->di_size;
511 sb->st_mtim.tv_sec = ip->i_din1->di_mtime;
512 sb->st_mtim.tv_nsec = ip->i_din1->di_mtimensec;
513 sb->st_ctim.tv_sec = ip->i_din1->di_ctime;
514 sb->st_ctim.tv_nsec = ip->i_din1->di_ctimensec;
515 sb->st_birthtim.tv_sec = -1;
516 sb->st_birthtim.tv_nsec = 0;
517 sb->st_blocks = dbtob((u_quad_t)ip->i_din1->di_blocks) / S_BLKSIZE;
519 sb->st_rdev = ip->i_din2->di_rdev;
520 sb->st_size = ip->i_din2->di_size;
521 sb->st_mtim.tv_sec = ip->i_din2->di_mtime;
522 sb->st_mtim.tv_nsec = ip->i_din2->di_mtimensec;
523 sb->st_ctim.tv_sec = ip->i_din2->di_ctime;
524 sb->st_ctim.tv_nsec = ip->i_din2->di_ctimensec;
525 sb->st_birthtim.tv_sec = ip->i_din2->di_birthtime;
526 sb->st_birthtim.tv_nsec = ip->i_din2->di_birthnsec;
527 sb->st_blocks = dbtob((u_quad_t)ip->i_din2->di_blocks) / S_BLKSIZE;
530 sb->st_blksize = max(PAGE_SIZE, vp->v_mount->mnt_stat.f_iosize);
531 sb->st_flags = ip->i_flags;
532 sb->st_gen = ip->i_gen;
534 return (vop_stat_helper_post(ap, error));
540 struct vop_getattr_args /* {
543 struct ucred *a_cred;
546 struct vnode *vp = ap->a_vp;
547 struct inode *ip = VTOI(vp);
548 struct vattr *vap = ap->a_vap;
551 ufs_itimes_locked(vp);
553 vap->va_atime.tv_sec = ip->i_din1->di_atime;
554 vap->va_atime.tv_nsec = ip->i_din1->di_atimensec;
556 vap->va_atime.tv_sec = ip->i_din2->di_atime;
557 vap->va_atime.tv_nsec = ip->i_din2->di_atimensec;
561 * Copy from inode table
563 vap->va_fsid = dev2udev(ITOUMP(ip)->um_dev);
564 vap->va_fileid = ip->i_number;
565 vap->va_mode = ip->i_mode & ~IFMT;
566 vap->va_nlink = ip->i_effnlink;
567 vap->va_uid = ip->i_uid;
568 vap->va_gid = ip->i_gid;
570 vap->va_rdev = ip->i_din1->di_rdev;
571 vap->va_size = ip->i_din1->di_size;
572 vap->va_mtime.tv_sec = ip->i_din1->di_mtime;
573 vap->va_mtime.tv_nsec = ip->i_din1->di_mtimensec;
574 vap->va_ctime.tv_sec = ip->i_din1->di_ctime;
575 vap->va_ctime.tv_nsec = ip->i_din1->di_ctimensec;
576 vap->va_bytes = dbtob((u_quad_t)ip->i_din1->di_blocks);
577 vap->va_filerev = ip->i_din1->di_modrev;
579 vap->va_rdev = ip->i_din2->di_rdev;
580 vap->va_size = ip->i_din2->di_size;
581 vap->va_mtime.tv_sec = ip->i_din2->di_mtime;
582 vap->va_mtime.tv_nsec = ip->i_din2->di_mtimensec;
583 vap->va_ctime.tv_sec = ip->i_din2->di_ctime;
584 vap->va_ctime.tv_nsec = ip->i_din2->di_ctimensec;
585 vap->va_birthtime.tv_sec = ip->i_din2->di_birthtime;
586 vap->va_birthtime.tv_nsec = ip->i_din2->di_birthnsec;
587 vap->va_bytes = dbtob((u_quad_t)ip->i_din2->di_blocks);
588 vap->va_filerev = ip->i_din2->di_modrev;
590 vap->va_flags = ip->i_flags;
591 vap->va_gen = ip->i_gen;
592 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
593 vap->va_type = IFTOVT(ip->i_mode);
598 * Set attribute vnode op. called from several syscalls
602 struct vop_setattr_args /* {
605 struct ucred *a_cred;
608 struct vattr *vap = ap->a_vap;
609 struct vnode *vp = ap->a_vp;
610 struct inode *ip = VTOI(vp);
611 struct ucred *cred = ap->a_cred;
612 struct thread *td = curthread;
616 * Check for unsettable attributes.
618 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
619 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
620 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
621 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
624 if (vap->va_flags != VNOVAL) {
625 if ((vap->va_flags & ~(SF_APPEND | SF_ARCHIVED | SF_IMMUTABLE |
626 SF_NOUNLINK | SF_SNAPSHOT | UF_APPEND | UF_ARCHIVE |
627 UF_HIDDEN | UF_IMMUTABLE | UF_NODUMP | UF_NOUNLINK |
628 UF_OFFLINE | UF_OPAQUE | UF_READONLY | UF_REPARSE |
629 UF_SPARSE | UF_SYSTEM)) != 0)
631 if (vp->v_mount->mnt_flag & MNT_RDONLY)
634 * Callers may only modify the file flags on objects they
635 * have VADMIN rights for.
637 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
640 * Unprivileged processes are not permitted to unset system
641 * flags, or modify flags if any system flags are set.
642 * Privileged non-jail processes may not modify system flags
643 * if securelevel > 0 and any existing system flags are set.
644 * Privileged jail processes behave like privileged non-jail
645 * processes if the PR_ALLOW_CHFLAGS permission bit is set;
646 * otherwise, they behave like unprivileged processes.
648 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS)) {
650 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
651 error = securelevel_gt(cred, 0);
655 /* The snapshot flag cannot be toggled. */
656 if ((vap->va_flags ^ ip->i_flags) & SF_SNAPSHOT)
660 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
661 ((vap->va_flags ^ ip->i_flags) & SF_SETTABLE))
664 ip->i_flags = vap->va_flags;
665 DIP_SET(ip, i_flags, vap->va_flags);
666 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
667 error = UFS_UPDATE(vp, 0);
668 if (ip->i_flags & (IMMUTABLE | APPEND))
672 * If immutable or append, no one can change any of its attributes
673 * except the ones already handled (in some cases, file flags
674 * including the immutability flags themselves for the superuser).
676 if (ip->i_flags & (IMMUTABLE | APPEND))
679 * Go through the fields and update iff not VNOVAL.
681 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
682 if (vp->v_mount->mnt_flag & MNT_RDONLY)
684 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred,
688 if (vap->va_size != VNOVAL) {
690 * XXX most of the following special cases should be in
691 * callers instead of in N filesystems. The VDIR check
694 switch (vp->v_type) {
700 * Truncation should have an effect in these cases.
701 * Disallow it if the filesystem is read-only or
702 * the file is being snapshotted.
704 if (vp->v_mount->mnt_flag & MNT_RDONLY)
706 if ((ip->i_flags & SF_SNAPSHOT) != 0)
711 * According to POSIX, the result is unspecified
712 * for file types other than regular files,
713 * directories and shared memory objects. We
714 * don't support shared memory objects in the file
715 * system, and have dubious support for truncating
716 * symlinks. Just ignore the request in other cases.
720 if ((error = UFS_TRUNCATE(vp, vap->va_size, IO_NORMAL |
721 ((vap->va_vaflags & VA_SYNC) != 0 ? IO_SYNC : 0),
725 if (vap->va_atime.tv_sec != VNOVAL ||
726 vap->va_mtime.tv_sec != VNOVAL ||
727 vap->va_birthtime.tv_sec != VNOVAL) {
728 if (vp->v_mount->mnt_flag & MNT_RDONLY)
730 if ((ip->i_flags & SF_SNAPSHOT) != 0)
732 error = vn_utimes_perm(vp, vap, cred, td);
735 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_MODIFIED);
736 if (vap->va_atime.tv_sec != VNOVAL) {
737 ip->i_flag &= ~IN_ACCESS;
738 DIP_SET(ip, i_atime, vap->va_atime.tv_sec);
739 DIP_SET(ip, i_atimensec, vap->va_atime.tv_nsec);
741 if (vap->va_mtime.tv_sec != VNOVAL) {
742 ip->i_flag &= ~IN_UPDATE;
743 DIP_SET(ip, i_mtime, vap->va_mtime.tv_sec);
744 DIP_SET(ip, i_mtimensec, vap->va_mtime.tv_nsec);
746 if (vap->va_birthtime.tv_sec != VNOVAL && I_IS_UFS2(ip)) {
747 ip->i_din2->di_birthtime = vap->va_birthtime.tv_sec;
748 ip->i_din2->di_birthnsec = vap->va_birthtime.tv_nsec;
750 error = UFS_UPDATE(vp, 0);
755 if (vap->va_mode != (mode_t)VNOVAL) {
756 if (vp->v_mount->mnt_flag & MNT_RDONLY)
758 if ((ip->i_flags & SF_SNAPSHOT) != 0 && (vap->va_mode &
759 (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP | S_IXOTH | S_IWOTH)))
761 error = ufs_chmod(vp, (int)vap->va_mode, cred, td);
768 ufs_update_nfs4_acl_after_mode_change(struct vnode *vp, int mode,
769 int file_owner_id, struct ucred *cred, struct thread *td)
774 aclp = acl_alloc(M_WAITOK);
775 error = ufs_getacl_nfs4_internal(vp, aclp, td);
777 * We don't have to handle EOPNOTSUPP here, as the filesystem claims
783 acl_nfs4_sync_acl_from_mode(aclp, mode, file_owner_id);
784 error = ufs_setacl_nfs4_internal(vp, aclp, td);
794 struct vop_mmapped_args /* {
806 if ((mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
807 UFS_INODE_SET_FLAG_SHARED(ip, IN_ACCESS);
809 * XXXKIB No UFS_UPDATE(ap->a_vp, 0) there.
815 * Change the mode on a file.
816 * Inode must be locked before calling.
819 ufs_chmod(vp, mode, cred, td)
825 struct inode *ip = VTOI(vp);
829 * To modify the permissions on a file, must possess VADMIN
832 if ((error = VOP_ACCESSX(vp, VWRITE_ACL, cred, td)))
835 * Privileged processes may set the sticky bit on non-directories,
836 * as well as set the setgid bit on a file with a group that the
837 * process is not a member of. Both of these are allowed in
840 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
841 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE))
844 if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
845 error = priv_check_cred(cred, PRIV_VFS_SETGID);
851 * Deny setting setuid if we are not the file owner.
853 if ((mode & ISUID) && ip->i_uid != cred->cr_uid) {
854 error = priv_check_cred(cred, PRIV_VFS_ADMIN);
859 newmode = ip->i_mode & ~ALLPERMS;
860 newmode |= (mode & ALLPERMS);
861 UFS_INODE_SET_MODE(ip, newmode);
862 DIP_SET(ip, i_mode, ip->i_mode);
863 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
865 if ((vp->v_mount->mnt_flag & MNT_NFS4ACLS) != 0)
866 error = ufs_update_nfs4_acl_after_mode_change(vp, mode, ip->i_uid, cred, td);
868 if (error == 0 && (ip->i_flag & IN_CHANGE) != 0)
869 error = UFS_UPDATE(vp, 0);
875 * Perform chown operation on inode ip;
876 * inode must be locked prior to call.
879 ufs_chown(vp, uid, gid, cred, td)
886 struct inode *ip = VTOI(vp);
895 if (uid == (uid_t)VNOVAL)
897 if (gid == (gid_t)VNOVAL)
900 * To modify the ownership of a file, must possess VADMIN for that
903 if ((error = VOP_ACCESSX(vp, VWRITE_OWNER, cred, td)))
906 * To change the owner of a file, or change the group of a file to a
907 * group of which we are not a member, the caller must have
910 if (((uid != ip->i_uid && uid != cred->cr_uid) ||
911 (gid != ip->i_gid && !groupmember(gid, cred))) &&
912 (error = priv_check_cred(cred, PRIV_VFS_CHOWN)))
917 if ((error = getinoquota(ip)) != 0)
920 dqrele(vp, ip->i_dquot[USRQUOTA]);
921 ip->i_dquot[USRQUOTA] = NODQUOT;
924 dqrele(vp, ip->i_dquot[GRPQUOTA]);
925 ip->i_dquot[GRPQUOTA] = NODQUOT;
927 change = DIP(ip, i_blocks);
928 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
929 (void) chkiq(ip, -1, cred, CHOWN|FORCE);
930 for (i = 0; i < MAXQUOTAS; i++) {
931 dqrele(vp, ip->i_dquot[i]);
932 ip->i_dquot[i] = NODQUOT;
936 DIP_SET(ip, i_gid, gid);
938 DIP_SET(ip, i_uid, uid);
940 if ((error = getinoquota(ip)) == 0) {
942 dqrele(vp, ip->i_dquot[USRQUOTA]);
943 ip->i_dquot[USRQUOTA] = NODQUOT;
946 dqrele(vp, ip->i_dquot[GRPQUOTA]);
947 ip->i_dquot[GRPQUOTA] = NODQUOT;
949 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
950 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
953 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
955 for (i = 0; i < MAXQUOTAS; i++) {
956 dqrele(vp, ip->i_dquot[i]);
957 ip->i_dquot[i] = NODQUOT;
961 DIP_SET(ip, i_gid, ogid);
963 DIP_SET(ip, i_uid, ouid);
964 if (getinoquota(ip) == 0) {
966 dqrele(vp, ip->i_dquot[USRQUOTA]);
967 ip->i_dquot[USRQUOTA] = NODQUOT;
970 dqrele(vp, ip->i_dquot[GRPQUOTA]);
971 ip->i_dquot[GRPQUOTA] = NODQUOT;
973 (void) chkdq(ip, change, cred, FORCE|CHOWN);
974 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
975 (void) getinoquota(ip);
980 panic("ufs_chown: lost quota");
982 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
983 if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
984 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID)) {
985 UFS_INODE_SET_MODE(ip, ip->i_mode & ~(ISUID | ISGID));
986 DIP_SET(ip, i_mode, ip->i_mode);
989 error = UFS_UPDATE(vp, 0);
995 struct vop_remove_args /* {
998 struct componentname *a_cnp;
1002 struct vnode *vp = ap->a_vp;
1003 struct vnode *dvp = ap->a_dvp;
1009 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1010 (VTOI(dvp)->i_flags & APPEND))
1012 if (DOINGSUJ(dvp)) {
1013 error = softdep_prelink(dvp, vp, ap->a_cnp);
1015 MPASS(error == ERELOOKUP);
1021 ufs_gjournal_orphan(vp);
1023 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
1024 if (ip->i_nlink <= 0)
1025 vp->v_vflag |= VV_NOSYNC;
1026 if ((ip->i_flags & SF_SNAPSHOT) != 0) {
1028 * Avoid deadlock where another thread is trying to
1029 * update the inodeblock for dvp and is waiting on
1030 * snaplk. Temporary unlock the vnode lock for the
1031 * unlinked file and sync the directory. This should
1032 * allow vput() of the directory to not block later on
1033 * while holding the snapshot vnode locked, assuming
1034 * that the directory hasn't been unlinked too.
1037 (void) VOP_FSYNC(dvp, MNT_WAIT, td);
1038 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1044 print_bad_link_count(const char *funcname, struct vnode *dvp)
1049 uprintf("%s: Bad link count %d on parent inode %jd in file system %s\n",
1050 funcname, dip->i_effnlink, (intmax_t)dip->i_number,
1051 dvp->v_mount->mnt_stat.f_mntonname);
1059 struct vop_link_args /* {
1060 struct vnode *a_tdvp;
1062 struct componentname *a_cnp;
1065 struct vnode *vp = ap->a_vp;
1066 struct vnode *tdvp = ap->a_tdvp;
1067 struct componentname *cnp = ap->a_cnp;
1069 struct direct newdir;
1073 if ((cnp->cn_flags & HASBUF) == 0)
1074 panic("ufs_link: no name");
1077 if (DOINGSUJ(tdvp)) {
1078 error = softdep_prelink(tdvp, vp, cnp);
1080 MPASS(error == ERELOOKUP);
1085 if (VTOI(tdvp)->i_effnlink < 2) {
1086 print_bad_link_count("ufs_link", tdvp);
1091 if (ip->i_nlink >= UFS_LINK_MAX) {
1096 * The file may have been removed after namei droped the original
1099 if (ip->i_effnlink == 0) {
1103 if (ip->i_flags & (IMMUTABLE | APPEND)) {
1110 DIP_SET(ip, i_nlink, ip->i_nlink);
1111 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1112 if (DOINGSOFTDEP(vp))
1113 softdep_setup_link(VTOI(tdvp), ip);
1114 error = UFS_UPDATE(vp, !DOINGSOFTDEP(vp) && !DOINGASYNC(vp));
1116 ufs_makedirentry(ip, cnp, &newdir);
1117 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
1123 DIP_SET(ip, i_nlink, ip->i_nlink);
1124 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1125 if (DOINGSOFTDEP(vp))
1126 softdep_revert_link(VTOI(tdvp), ip);
1133 * whiteout vnode call
1137 struct vop_whiteout_args /* {
1138 struct vnode *a_dvp;
1139 struct componentname *a_cnp;
1143 struct vnode *dvp = ap->a_dvp;
1144 struct componentname *cnp = ap->a_cnp;
1145 struct direct newdir;
1148 if (DOINGSUJ(dvp) && (ap->a_flags == CREATE ||
1149 ap->a_flags == DELETE)) {
1150 error = softdep_prelink(dvp, NULL, cnp);
1152 MPASS(error == ERELOOKUP);
1157 switch (ap->a_flags) {
1159 /* 4.4 format directories support whiteout operations */
1162 return (EOPNOTSUPP);
1165 /* create a new directory whiteout */
1167 if ((cnp->cn_flags & SAVENAME) == 0)
1168 panic("ufs_whiteout: missing name");
1170 panic("ufs_whiteout: old format filesystem");
1173 newdir.d_ino = UFS_WINO;
1174 newdir.d_namlen = cnp->cn_namelen;
1175 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
1176 newdir.d_type = DT_WHT;
1177 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
1181 /* remove an existing directory whiteout */
1184 panic("ufs_whiteout: old format filesystem");
1187 cnp->cn_flags &= ~DOWHITEOUT;
1188 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
1191 panic("ufs_whiteout: unknown op");
1196 static volatile int rename_restarts;
1197 SYSCTL_INT(_vfs_ufs, OID_AUTO, rename_restarts, CTLFLAG_RD,
1198 __DEVOLATILE(int *, &rename_restarts), 0,
1199 "Times rename had to restart due to lock contention");
1202 * Rename system call.
1203 * rename("foo", "bar");
1206 * link("foo", "bar");
1208 * but ``atomically''. Can't do full commit without saving state in the
1209 * inode on disk which isn't feasible at this time. Best we can do is
1210 * always guarantee the target exists.
1212 * Basic algorithm is:
1214 * 1) Bump link count on source while we're linking it to the
1215 * target. This also ensure the inode won't be deleted out
1216 * from underneath us while we work (it may be truncated by
1217 * a concurrent `trunc' or `open' for creation).
1218 * 2) Link source to destination. If destination already exists,
1220 * 3) Unlink source reference to inode if still around. If a
1221 * directory was moved and the parent of the destination
1222 * is different from the source, patch the ".." entry in the
1227 struct vop_rename_args /* {
1228 struct vnode *a_fdvp;
1229 struct vnode *a_fvp;
1230 struct componentname *a_fcnp;
1231 struct vnode *a_tdvp;
1232 struct vnode *a_tvp;
1233 struct componentname *a_tcnp;
1236 struct vnode *tvp = ap->a_tvp;
1237 struct vnode *tdvp = ap->a_tdvp;
1238 struct vnode *fvp = ap->a_fvp;
1239 struct vnode *fdvp = ap->a_fdvp;
1241 struct componentname *tcnp = ap->a_tcnp;
1242 struct componentname *fcnp = ap->a_fcnp;
1243 struct thread *td = fcnp->cn_thread;
1244 struct inode *fip, *tip, *tdp, *fdp;
1245 struct direct newdir;
1247 int doingdirectory, newparent;
1251 seqc_t fdvp_s, fvp_s, tdvp_s, tvp_s;
1254 want_seqc_end = false;
1257 if ((tcnp->cn_flags & HASBUF) == 0 ||
1258 (fcnp->cn_flags & HASBUF) == 0)
1259 panic("ufs_rename: no name");
1264 if (tvp && tvp != tdvp)
1267 * Check for cross-device rename.
1269 if ((fvp->v_mount != tdvp->v_mount) ||
1270 (tvp && (fvp->v_mount != tvp->v_mount))) {
1276 fdvp_s = fvp_s = tdvp_s = tvp_s = SEQC_MOD;
1279 * We need to acquire 2 to 4 locks depending on whether tvp is NULL
1280 * and fdvp and tdvp are the same directory. Subsequently we need
1281 * to double-check all paths and in the directory rename case we
1282 * need to verify that we are not creating a directory loop. To
1283 * handle this we acquire all but fdvp using non-blocking
1284 * acquisitions. If we fail to acquire any lock in the path we will
1285 * drop all held locks, acquire the new lock in a blocking fashion,
1286 * and then release it and restart the rename. This acquire/release
1287 * step ensures that we do not spin on a lock waiting for release.
1289 error = vn_lock(fdvp, LK_EXCLUSIVE);
1292 if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1294 error = vn_lock(tdvp, LK_EXCLUSIVE);
1298 atomic_add_int(&rename_restarts, 1);
1302 * Re-resolve fvp to be certain it still exists and fetch the
1305 error = ufs_lookup_ino(fdvp, NULL, fcnp, &ino);
1311 error = VFS_VGET(mp, ino, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
1317 error = VFS_VGET(mp, ino, LK_EXCLUSIVE, &nvp);
1323 atomic_add_int(&rename_restarts, 1);
1329 * Re-resolve tvp and acquire the vnode lock if present.
1331 error = ufs_lookup_ino(tdvp, NULL, tcnp, &ino);
1332 if (error != 0 && error != EJUSTRETURN) {
1339 * If tvp disappeared we just carry on.
1341 if (error == EJUSTRETURN && tvp != NULL) {
1346 * Get the tvp ino if the lookup succeeded. We may have to restart
1347 * if the non-blocking acquire fails.
1351 error = VFS_VGET(mp, ino, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
1361 error = VFS_VGET(mp, ino, LK_EXCLUSIVE, &nvp);
1365 atomic_add_int(&rename_restarts, 1);
1370 if (DOINGSUJ(fdvp) &&
1371 (seqc_in_modify(fdvp_s) || !vn_seqc_consistent(fdvp, fdvp_s) ||
1372 seqc_in_modify(fvp_s) || !vn_seqc_consistent(fvp, fvp_s) ||
1373 seqc_in_modify(tdvp_s) || !vn_seqc_consistent(tdvp, tdvp_s) ||
1374 (tvp != NULL && (seqc_in_modify(tvp_s) ||
1375 !vn_seqc_consistent(tvp, tvp_s))))) {
1376 error = softdep_prerename(fdvp, fvp, tdvp, tvp);
1378 if (error == ERELOOKUP) {
1379 fdvp_s = vn_seqc_read_any(fdvp);
1380 fvp_s = vn_seqc_read_any(fvp);
1381 tdvp_s = vn_seqc_read_any(tdvp);
1383 tvp_s = vn_seqc_read_any(tvp);
1384 atomic_add_int(&rename_restarts, 1);
1397 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1398 (VTOI(tdvp)->i_flags & APPEND))) {
1403 * Renaming a file to itself has no effect. The upper layers should
1404 * not call us in that case. However, things could change after
1405 * we drop the locks above.
1413 ino = fip->i_number;
1414 if (fip->i_nlink >= UFS_LINK_MAX) {
1418 if ((fip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
1419 || (fdp->i_flags & APPEND)) {
1423 if ((fip->i_mode & IFMT) == IFDIR) {
1425 * Avoid ".", "..", and aliases of "." for obvious reasons.
1427 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1429 (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT) {
1433 if (fdp->i_number != tdp->i_number)
1434 newparent = tdp->i_number;
1437 if ((fvp->v_type == VDIR && fvp->v_mountedhere != NULL) ||
1438 (tvp != NULL && tvp->v_type == VDIR &&
1439 tvp->v_mountedhere != NULL)) {
1445 * If ".." must be changed (ie the directory gets a new
1446 * parent) then the source directory must not be in the
1447 * directory hierarchy above the target, as this would
1448 * orphan everything below the source directory. Also
1449 * the user must have write permission in the source so
1450 * as to be able to change "..".
1452 if (doingdirectory && newparent) {
1453 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
1456 error = ufs_checkpath(ino, fdp->i_number, tdp, tcnp->cn_cred,
1459 * We encountered a lock that we have to wait for. Unlock
1460 * everything else and VGET before restarting.
1468 error = VFS_VGET(mp, ino, LK_SHARED, &nvp);
1471 atomic_add_int(&rename_restarts, 1);
1476 if ((tcnp->cn_flags & SAVESTART) == 0)
1477 panic("ufs_rename: lost to startdir");
1479 if (fip->i_effnlink == 0 || fdp->i_effnlink == 0 ||
1480 tdp->i_effnlink == 0)
1481 panic("Bad effnlink fip %p, fdp %p, tdp %p", fip, fdp, tdp);
1484 vn_seqc_write_begin(tvp);
1485 vn_seqc_write_begin(tdvp);
1486 vn_seqc_write_begin(fvp);
1487 vn_seqc_write_begin(fdvp);
1488 want_seqc_end = true;
1491 * 1) Bump link count while we're moving stuff
1492 * around. If we crash somewhere before
1493 * completing our work, the link count
1494 * may be wrong, but correctable.
1498 DIP_SET(fip, i_nlink, fip->i_nlink);
1499 UFS_INODE_SET_FLAG(fip, IN_CHANGE);
1500 if (DOINGSOFTDEP(fvp))
1501 softdep_setup_link(tdp, fip);
1502 error = UFS_UPDATE(fvp, !DOINGSOFTDEP(fvp) && !DOINGASYNC(fvp));
1507 * 2) If target doesn't exist, link the target
1508 * to the source and unlink the source.
1509 * Otherwise, rewrite the target directory
1510 * entry to reference the source inode and
1511 * expunge the original entry's existence.
1514 if (ITODEV(tdp) != ITODEV(fip))
1515 panic("ufs_rename: EXDEV");
1516 if (doingdirectory && newparent) {
1518 * Account for ".." in new directory.
1519 * When source and destination have the same
1520 * parent we don't adjust the link count. The
1521 * actual link modification is completed when
1522 * .. is rewritten below.
1524 if (tdp->i_nlink >= UFS_LINK_MAX) {
1529 ufs_makedirentry(fip, tcnp, &newdir);
1530 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1533 /* Setup tdvp for directory compaction if needed. */
1534 if (I_COUNT(tdp) != 0 && I_ENDOFF(tdp) != 0 &&
1535 I_ENDOFF(tdp) < tdp->i_size)
1536 endoff = I_ENDOFF(tdp);
1538 if (ITODEV(tip) != ITODEV(tdp) || ITODEV(tip) != ITODEV(fip))
1539 panic("ufs_rename: EXDEV");
1541 * Short circuit rename(foo, foo).
1543 if (tip->i_number == fip->i_number)
1544 panic("ufs_rename: same file");
1546 * If the parent directory is "sticky", then the caller
1547 * must possess VADMIN for the parent directory, or the
1548 * destination of the rename. This implements append-only
1551 if ((tdp->i_mode & S_ISTXT) &&
1552 VOP_ACCESS(tdvp, VADMIN, tcnp->cn_cred, td) &&
1553 VOP_ACCESS(tvp, VADMIN, tcnp->cn_cred, td)) {
1558 * Target must be empty if a directory and have no links
1559 * to it. Also, ensure source and target are compatible
1560 * (both directories, or both not directories).
1562 if ((tip->i_mode & IFMT) == IFDIR) {
1563 if ((tip->i_effnlink > 2) ||
1564 !ufs_dirempty(tip, tdp->i_number, tcnp->cn_cred)) {
1568 if (!doingdirectory) {
1573 } else if (doingdirectory) {
1577 if (doingdirectory) {
1580 if (DOINGSOFTDEP(tdvp))
1581 softdep_change_linkcnt(tdp);
1584 if (DOINGSOFTDEP(tvp))
1585 softdep_change_linkcnt(tip);
1587 error = ufs_dirrewrite(tdp, tip, fip->i_number,
1588 IFTODT(fip->i_mode),
1589 (doingdirectory && newparent) ? newparent : doingdirectory);
1591 if (doingdirectory) {
1594 if (DOINGSOFTDEP(tdvp))
1595 softdep_change_linkcnt(tdp);
1598 if (DOINGSOFTDEP(tvp))
1599 softdep_change_linkcnt(tip);
1603 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1605 * The only stuff left in the directory is "."
1606 * and "..". The "." reference is inconsequential
1607 * since we are quashing it. We have removed the "."
1608 * reference and the reference in the parent directory,
1609 * but there may be other hard links. The soft
1610 * dependency code will arrange to do these operations
1611 * after the parent directory entry has been deleted on
1612 * disk, so when running with that code we avoid doing
1617 DIP_SET(tdp, i_nlink, tdp->i_nlink);
1618 UFS_INODE_SET_FLAG(tdp, IN_CHANGE);
1621 DIP_SET(tip, i_nlink, tip->i_nlink);
1622 UFS_INODE_SET_FLAG(tip, IN_CHANGE);
1627 * 3) Unlink the source. We have to resolve the path again to
1628 * fixup the directory offset and count for ufs_dirremove.
1631 error = ufs_lookup_ino(fdvp, NULL, fcnp, &ino);
1633 panic("ufs_rename: from entry went away!");
1634 if (ino != fip->i_number)
1635 panic("ufs_rename: ino mismatch %ju != %ju\n",
1636 (uintmax_t)ino, (uintmax_t)fip->i_number);
1639 * If the source is a directory with a
1640 * new parent, the link count of the old
1641 * parent directory must be decremented
1642 * and ".." set to point to the new parent.
1644 if (doingdirectory && newparent) {
1646 * If tip exists we simply use its link, otherwise we must
1652 DIP_SET(tdp, i_nlink, tdp->i_nlink);
1653 UFS_INODE_SET_FLAG(tdp, IN_CHANGE);
1654 if (DOINGSOFTDEP(tdvp))
1655 softdep_setup_dotdot_link(tdp, fip);
1656 error = UFS_UPDATE(tdvp, !DOINGSOFTDEP(tdvp) &&
1658 /* Don't go to bad here as the new link exists. */
1661 } else if (DOINGSUJ(tdvp))
1662 /* Journal must account for each new link. */
1663 softdep_setup_dotdot_link(tdp, fip);
1664 SET_I_OFFSET(fip, mastertemplate.dot_reclen);
1665 ufs_dirrewrite(fip, fdp, newparent, DT_DIR, 0);
1668 error = ufs_dirremove(fdvp, fip, fcnp->cn_flags, 0);
1670 * The kern_renameat() looks up the fvp using the DELETE flag, which
1671 * causes the removal of the name cache entry for fvp.
1672 * As the relookup of the fvp is done in two steps:
1673 * ufs_lookup_ino() and then VFS_VGET(), another thread might do a
1674 * normal lookup of the from name just before the VFS_VGET() call,
1675 * causing the cache entry to be re-instantiated.
1677 * The same issue also applies to tvp if it exists as
1678 * otherwise we may have a stale name cache entry for the new
1679 * name that references the old i-node if it has other links
1680 * or open file descriptors.
1682 cache_vop_rename(fdvp, fvp, tdvp, tvp, fcnp, tcnp);
1685 if (want_seqc_end) {
1687 vn_seqc_write_end(tvp);
1688 vn_seqc_write_end(tdvp);
1689 vn_seqc_write_end(fvp);
1690 vn_seqc_write_end(fdvp);
1697 * If compaction or fsync was requested do it in
1698 * ffs_vput_pair() now that other locks are no longer needed.
1700 if (error == 0 && endoff != 0) {
1701 UFS_INODE_SET_FLAG(tdp, IN_ENDOFF);
1702 SET_I_ENDOFF(tdp, endoff);
1704 VOP_VPUT_PAIR(tdvp, &tvp, true);
1710 DIP_SET(fip, i_nlink, fip->i_nlink);
1711 UFS_INODE_SET_FLAG(fip, IN_CHANGE);
1712 if (DOINGSOFTDEP(fvp))
1713 softdep_revert_link(tdp, fip);
1717 if (want_seqc_end) {
1719 vn_seqc_write_end(tvp);
1720 vn_seqc_write_end(tdvp);
1721 vn_seqc_write_end(fvp);
1722 vn_seqc_write_end(fdvp);
1736 ufs_do_posix1e_acl_inheritance_dir(struct vnode *dvp, struct vnode *tvp,
1737 mode_t dmode, struct ucred *cred, struct thread *td)
1740 struct inode *ip = VTOI(tvp);
1741 struct acl *dacl, *acl;
1743 acl = acl_alloc(M_WAITOK);
1744 dacl = acl_alloc(M_WAITOK);
1747 * Retrieve default ACL from parent, if any.
1749 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cred, td);
1753 * Retrieved a default ACL, so merge mode and ACL if
1754 * necessary. If the ACL is empty, fall through to
1755 * the "not defined or available" case.
1757 if (acl->acl_cnt != 0) {
1758 dmode = acl_posix1e_newfilemode(dmode, acl);
1759 UFS_INODE_SET_MODE(ip, dmode);
1760 DIP_SET(ip, i_mode, dmode);
1762 ufs_sync_acl_from_inode(ip, acl);
1769 * Just use the mode as-is.
1771 UFS_INODE_SET_MODE(ip, dmode);
1772 DIP_SET(ip, i_mode, dmode);
1781 * XXX: If we abort now, will Soft Updates notify the extattr
1782 * code that the EAs for the file need to be released?
1784 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cred, td);
1786 error = VOP_SETACL(tvp, ACL_TYPE_DEFAULT, dacl, cred, td);
1793 * XXX: This should not happen, as EOPNOTSUPP above
1794 * was supposed to free acl.
1796 printf("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()\n");
1798 panic("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()");
1814 ufs_do_posix1e_acl_inheritance_file(struct vnode *dvp, struct vnode *tvp,
1815 mode_t mode, struct ucred *cred, struct thread *td)
1818 struct inode *ip = VTOI(tvp);
1821 acl = acl_alloc(M_WAITOK);
1824 * Retrieve default ACL for parent, if any.
1826 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cred, td);
1830 * Retrieved a default ACL, so merge mode and ACL if
1833 if (acl->acl_cnt != 0) {
1835 * Two possible ways for default ACL to not
1836 * be present. First, the EA can be
1837 * undefined, or second, the default ACL can
1838 * be blank. If it's blank, fall through to
1839 * the it's not defined case.
1841 mode = acl_posix1e_newfilemode(mode, acl);
1842 UFS_INODE_SET_MODE(ip, mode);
1843 DIP_SET(ip, i_mode, mode);
1844 ufs_sync_acl_from_inode(ip, acl);
1851 * Just use the mode as-is.
1853 UFS_INODE_SET_MODE(ip, mode);
1854 DIP_SET(ip, i_mode, mode);
1863 * XXX: If we abort now, will Soft Updates notify the extattr
1864 * code that the EAs for the file need to be released?
1866 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cred, td);
1873 * XXX: This should not happen, as EOPNOTSUPP above was
1874 * supposed to free acl.
1876 printf("ufs_do_posix1e_acl_inheritance_file: VOP_GETACL() "
1877 "but no VOP_SETACL()\n");
1878 /* panic("ufs_do_posix1e_acl_inheritance_file: VOP_GETACL() "
1879 "but no VOP_SETACL()"); */
1893 ufs_do_nfs4_acl_inheritance(struct vnode *dvp, struct vnode *tvp,
1894 mode_t child_mode, struct ucred *cred, struct thread *td)
1897 struct acl *parent_aclp, *child_aclp;
1899 parent_aclp = acl_alloc(M_WAITOK);
1900 child_aclp = acl_alloc(M_WAITOK | M_ZERO);
1902 error = ufs_getacl_nfs4_internal(dvp, parent_aclp, td);
1905 acl_nfs4_compute_inherited_acl(parent_aclp, child_aclp,
1906 child_mode, VTOI(tvp)->i_uid, tvp->v_type == VDIR);
1907 error = ufs_setacl_nfs4_internal(tvp, child_aclp, td);
1911 acl_free(parent_aclp);
1912 acl_free(child_aclp);
1923 struct vop_mkdir_args /* {
1924 struct vnode *a_dvp;
1925 struct vnode **a_vpp;
1926 struct componentname *a_cnp;
1927 struct vattr *a_vap;
1930 struct vnode *dvp = ap->a_dvp;
1931 struct vattr *vap = ap->a_vap;
1932 struct componentname *cnp = ap->a_cnp;
1933 struct inode *ip, *dp;
1936 struct dirtemplate dirtemplate, *dtp;
1937 struct direct newdir;
1942 if ((cnp->cn_flags & HASBUF) == 0)
1943 panic("ufs_mkdir: no name");
1946 if (dp->i_nlink >= UFS_LINK_MAX) {
1950 dmode = vap->va_mode & 0777;
1954 * Must simulate part of ufs_makeinode here to acquire the inode,
1955 * but not have it entered in the parent directory. The entry is
1956 * made later after writing "." and ".." entries.
1958 if (dp->i_effnlink < 2) {
1959 print_bad_link_count("ufs_mkdir", dvp);
1964 if (DOINGSUJ(dvp)) {
1965 error = softdep_prelink(dvp, NULL, cnp);
1967 MPASS(error == ERELOOKUP);
1972 error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
1975 vn_seqc_write_begin(tvp);
1977 ip->i_gid = dp->i_gid;
1978 DIP_SET(ip, i_gid, dp->i_gid);
1982 struct ucred ucred, *ucp;
1987 * If we are hacking owners here, (only do this where told to)
1988 * and we are not giving it TO root, (would subvert quotas)
1989 * then go ahead and give it to the other user.
1990 * The new directory also inherits the SUID bit.
1991 * If user's UID and dir UID are the same,
1992 * 'give it away' so that the SUID is still forced on.
1994 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1995 (dp->i_mode & ISUID) && dp->i_uid) {
1997 ip->i_uid = dp->i_uid;
1998 DIP_SET(ip, i_uid, dp->i_uid);
2000 if (dp->i_uid != cnp->cn_cred->cr_uid) {
2002 * Make sure the correct user gets charged
2004 * Make a dummy credential for the victim.
2005 * XXX This seems to never be accessed out of
2006 * our context so a stack variable is ok.
2008 refcount_init(&ucred.cr_ref, 1);
2009 ucred.cr_uid = ip->i_uid;
2010 ucred.cr_ngroups = 1;
2011 ucred.cr_groups = &ucred_group;
2012 ucred.cr_groups[0] = dp->i_gid;
2017 ip->i_uid = cnp->cn_cred->cr_uid;
2018 DIP_SET(ip, i_uid, ip->i_uid);
2021 if ((error = getinoquota(ip)) ||
2022 (error = chkiq(ip, 1, ucp, 0))) {
2023 if (DOINGSOFTDEP(tvp))
2024 softdep_revert_link(dp, ip);
2025 UFS_VFREE(tvp, ip->i_number, dmode);
2026 vn_seqc_write_end(tvp);
2033 #else /* !SUIDDIR */
2034 ip->i_uid = cnp->cn_cred->cr_uid;
2035 DIP_SET(ip, i_uid, ip->i_uid);
2037 if ((error = getinoquota(ip)) ||
2038 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2039 if (DOINGSOFTDEP(tvp))
2040 softdep_revert_link(dp, ip);
2041 UFS_VFREE(tvp, ip->i_number, dmode);
2042 vn_seqc_write_end(tvp);
2048 #endif /* !SUIDDIR */
2049 UFS_INODE_SET_FLAG(ip, IN_ACCESS | IN_CHANGE | IN_UPDATE);
2050 UFS_INODE_SET_MODE(ip, dmode);
2051 DIP_SET(ip, i_mode, dmode);
2052 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
2055 DIP_SET(ip, i_nlink, 2);
2057 if (cnp->cn_flags & ISWHITEOUT) {
2058 ip->i_flags |= UF_OPAQUE;
2059 DIP_SET(ip, i_flags, ip->i_flags);
2063 * Bump link count in parent directory to reflect work done below.
2064 * Should be done before reference is created so cleanup is
2065 * possible if we crash.
2069 DIP_SET(dp, i_nlink, dp->i_nlink);
2070 UFS_INODE_SET_FLAG(dp, IN_CHANGE);
2071 if (DOINGSOFTDEP(dvp))
2072 softdep_setup_mkdir(dp, ip);
2073 error = UFS_UPDATE(dvp, !DOINGSOFTDEP(dvp) && !DOINGASYNC(dvp));
2077 if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
2078 error = mac_vnode_create_extattr(cnp->cn_cred, dvp->v_mount,
2085 if (dvp->v_mount->mnt_flag & MNT_ACLS) {
2086 error = ufs_do_posix1e_acl_inheritance_dir(dvp, tvp, dmode,
2087 cnp->cn_cred, cnp->cn_thread);
2090 } else if (dvp->v_mount->mnt_flag & MNT_NFS4ACLS) {
2091 error = ufs_do_nfs4_acl_inheritance(dvp, tvp, dmode,
2092 cnp->cn_cred, cnp->cn_thread);
2096 #endif /* !UFS_ACL */
2099 * Initialize directory with "." and ".." from static template.
2102 dtp = &mastertemplate;
2104 dtp = (struct dirtemplate *)&omastertemplate;
2106 dirtemplate.dot_ino = ip->i_number;
2107 dirtemplate.dotdot_ino = dp->i_number;
2108 vnode_pager_setsize(tvp, DIRBLKSIZ);
2109 if ((error = UFS_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred,
2110 BA_CLRBUF, &bp)) != 0)
2112 ip->i_size = DIRBLKSIZ;
2113 DIP_SET(ip, i_size, DIRBLKSIZ);
2114 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
2115 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
2116 if (DOINGSOFTDEP(tvp)) {
2118 * Ensure that the entire newly allocated block is a
2119 * valid directory so that future growth within the
2120 * block does not have to ensure that the block is
2121 * written before the inode.
2124 while (blkoff < bp->b_bcount) {
2126 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
2127 blkoff += DIRBLKSIZ;
2130 if ((error = UFS_UPDATE(tvp, !DOINGSOFTDEP(tvp) &&
2131 !DOINGASYNC(tvp))) != 0) {
2136 * Directory set up, now install its entry in the parent directory.
2138 * If we are not doing soft dependencies, then we must write out the
2139 * buffer containing the new directory body before entering the new
2140 * name in the parent. If we are doing soft dependencies, then the
2141 * buffer containing the new directory body will be passed to and
2142 * released in the soft dependency code after the code has attached
2143 * an appropriate ordering dependency to the buffer which ensures that
2144 * the buffer is written before the new name is written in the parent.
2146 if (DOINGASYNC(dvp))
2148 else if (!DOINGSOFTDEP(dvp) && ((error = bwrite(bp))))
2150 ufs_makedirentry(ip, cnp, &newdir);
2151 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
2156 vn_seqc_write_end(tvp);
2160 DIP_SET(dp, i_nlink, dp->i_nlink);
2161 UFS_INODE_SET_FLAG(dp, IN_CHANGE);
2163 * No need to do an explicit VOP_TRUNCATE here, vrele will
2164 * do this for us because we set the link count to 0.
2168 DIP_SET(ip, i_nlink, 0);
2169 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
2170 if (DOINGSOFTDEP(tvp))
2171 softdep_revert_mkdir(dp, ip);
2172 vn_seqc_write_end(tvp);
2181 * Rmdir system call.
2185 struct vop_rmdir_args /* {
2186 struct vnode *a_dvp;
2188 struct componentname *a_cnp;
2191 struct vnode *vp = ap->a_vp;
2192 struct vnode *dvp = ap->a_dvp;
2193 struct componentname *cnp = ap->a_cnp;
2194 struct inode *ip, *dp;
2201 * Do not remove a directory that is in the process of being renamed.
2202 * Verify the directory is empty (and valid). Rmdir ".." will not be
2203 * valid since ".." will contain a reference to the current directory
2204 * and thus be non-empty. Do not allow the removal of mounted on
2205 * directories (this can happen when an NFS exported filesystem
2206 * tries to remove a locally mounted on directory).
2209 if (dp->i_effnlink <= 2) {
2210 if (dp->i_effnlink == 2)
2211 print_bad_link_count("ufs_rmdir", dvp);
2215 if (!ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
2219 if ((dp->i_flags & APPEND)
2220 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
2224 if (vp->v_mountedhere != 0) {
2228 if (DOINGSUJ(dvp)) {
2229 error = softdep_prelink(dvp, vp, cnp);
2231 MPASS(error == ERELOOKUP);
2237 ufs_gjournal_orphan(vp);
2240 * Delete reference to directory before purging
2241 * inode. If we crash in between, the directory
2242 * will be reattached to lost+found,
2246 if (DOINGSOFTDEP(vp))
2247 softdep_setup_rmdir(dp, ip);
2248 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
2252 if (DOINGSOFTDEP(vp))
2253 softdep_revert_rmdir(dp, ip);
2257 * The only stuff left in the directory is "." and "..". The "."
2258 * reference is inconsequential since we are quashing it. The soft
2259 * dependency code will arrange to do these operations after
2260 * the parent directory entry has been deleted on disk, so
2261 * when running with that code we avoid doing them now.
2263 if (!DOINGSOFTDEP(vp)) {
2265 DIP_SET(dp, i_nlink, dp->i_nlink);
2266 UFS_INODE_SET_FLAG(dp, IN_CHANGE);
2267 error = UFS_UPDATE(dvp, 0);
2269 DIP_SET(ip, i_nlink, ip->i_nlink);
2270 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
2272 cache_vop_rmdir(dvp, vp);
2274 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
2275 if (ip->i_dirhash != NULL)
2276 ufsdirhash_free(ip);
2283 * symlink -- make a symbolic link
2287 struct vop_symlink_args /* {
2288 struct vnode *a_dvp;
2289 struct vnode **a_vpp;
2290 struct componentname *a_cnp;
2291 struct vattr *a_vap;
2292 const char *a_target;
2295 struct vnode *vp, **vpp = ap->a_vpp;
2299 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
2300 vpp, ap->a_cnp, "ufs_symlink");
2304 len = strlen(ap->a_target);
2305 if (len < VFSTOUFS(vp->v_mount)->um_maxsymlinklen) {
2307 bcopy(ap->a_target, SHORTLINK(ip), len);
2309 DIP_SET(ip, i_size, len);
2310 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
2311 error = UFS_UPDATE(vp, 0);
2313 error = vn_rdwr(UIO_WRITE, vp, __DECONST(void *, ap->a_target),
2314 len, (off_t)0, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
2315 ap->a_cnp->cn_cred, NOCRED, NULL, NULL);
2322 * Vnode op for reading directories.
2326 struct vop_readdir_args /* {
2329 struct ucred *a_cred;
2335 struct vnode *vp = ap->a_vp;
2336 struct uio *uio = ap->a_uio;
2339 struct direct *dp, *edp;
2341 struct dirent dstdp;
2342 off_t offset, startoffset;
2343 size_t readcnt, skipcnt;
2348 if (uio->uio_offset < 0)
2351 if (ip->i_effnlink == 0)
2353 if (ap->a_ncookies != NULL) {
2354 if (uio->uio_resid < 0)
2357 ncookies = uio->uio_resid;
2358 if (uio->uio_offset >= ip->i_size)
2360 else if (ip->i_size - uio->uio_offset < ncookies)
2361 ncookies = ip->i_size - uio->uio_offset;
2362 ncookies = ncookies / (offsetof(struct direct, d_name) + 4) + 1;
2363 cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
2364 *ap->a_ncookies = ncookies;
2365 *ap->a_cookies = cookies;
2370 offset = startoffset = uio->uio_offset;
2371 startresid = uio->uio_resid;
2373 while (error == 0 && uio->uio_resid > 0 &&
2374 uio->uio_offset < ip->i_size) {
2375 error = UFS_BLKATOFF(vp, uio->uio_offset, NULL, &bp);
2378 if (bp->b_offset + bp->b_bcount > ip->i_size)
2379 readcnt = ip->i_size - bp->b_offset;
2381 readcnt = bp->b_bcount;
2382 skipcnt = (size_t)(uio->uio_offset - bp->b_offset) &
2383 ~(size_t)(DIRBLKSIZ - 1);
2384 offset = bp->b_offset + skipcnt;
2385 dp = (struct direct *)&bp->b_data[skipcnt];
2386 edp = (struct direct *)&bp->b_data[readcnt];
2387 while (error == 0 && uio->uio_resid > 0 && dp < edp) {
2388 if (dp->d_reclen <= offsetof(struct direct, d_name) ||
2389 (caddr_t)dp + dp->d_reclen > (caddr_t)edp) {
2393 #if BYTE_ORDER == LITTLE_ENDIAN
2394 /* Old filesystem format. */
2396 dstdp.d_namlen = dp->d_type;
2397 dstdp.d_type = dp->d_namlen;
2401 dstdp.d_namlen = dp->d_namlen;
2402 dstdp.d_type = dp->d_type;
2404 if (offsetof(struct direct, d_name) + dstdp.d_namlen >
2409 if (offset < startoffset || dp->d_ino == 0)
2411 dstdp.d_fileno = dp->d_ino;
2412 dstdp.d_reclen = GENERIC_DIRSIZ(&dstdp);
2413 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
2414 /* NOTE: d_off is the offset of the *next* entry. */
2415 dstdp.d_off = offset + dp->d_reclen;
2416 dirent_terminate(&dstdp);
2417 if (dstdp.d_reclen > uio->uio_resid) {
2418 if (uio->uio_resid == startresid)
2421 error = EJUSTRETURN;
2425 error = uiomove((caddr_t)&dstdp, dstdp.d_reclen, uio);
2428 if (cookies != NULL) {
2429 KASSERT(ncookies > 0,
2430 ("ufs_readdir: cookies buffer too small"));
2431 *cookies = offset + dp->d_reclen;
2436 offset += dp->d_reclen;
2437 dp = (struct direct *)((caddr_t)dp + dp->d_reclen);
2440 uio->uio_offset = offset;
2442 /* We need to correct uio_offset. */
2443 uio->uio_offset = offset;
2444 if (error == EJUSTRETURN)
2446 if (ap->a_ncookies != NULL) {
2448 ap->a_ncookies -= ncookies;
2450 free(*ap->a_cookies, M_TEMP);
2451 *ap->a_ncookies = 0;
2452 *ap->a_cookies = NULL;
2455 if (error == 0 && ap->a_eofflag)
2456 *ap->a_eofflag = ip->i_size <= uio->uio_offset;
2461 * Return target name of a symbolic link
2465 struct vop_readlink_args /* {
2468 struct ucred *a_cred;
2471 struct vnode *vp = ap->a_vp;
2472 struct inode *ip = VTOI(vp);
2476 if (isize < VFSTOUFS(vp->v_mount)->um_maxsymlinklen)
2477 return (uiomove(SHORTLINK(ip), isize, ap->a_uio));
2478 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
2482 * Calculate the logical to physical mapping if not done already,
2483 * then call the device strategy routine.
2485 * In order to be able to swap to a file, the ufs_bmaparray() operation may not
2486 * deadlock on memory. See ufs_bmap() for details.
2490 struct vop_strategy_args /* {
2495 struct buf *bp = ap->a_bp;
2496 struct vnode *vp = ap->a_vp;
2500 if (bp->b_blkno == bp->b_lblkno) {
2501 error = ufs_bmaparray(vp, bp->b_lblkno, &blkno, bp, NULL, NULL);
2502 bp->b_blkno = blkno;
2504 bp->b_error = error;
2505 bp->b_ioflags |= BIO_ERROR;
2509 if ((long)bp->b_blkno == -1)
2512 if ((long)bp->b_blkno == -1) {
2516 bp->b_iooffset = dbtob(bp->b_blkno);
2517 BO_STRATEGY(VFSTOUFS(vp->v_mount)->um_bo, bp);
2522 * Print out the contents of an inode.
2526 struct vop_print_args /* {
2530 struct vnode *vp = ap->a_vp;
2531 struct inode *ip = VTOI(vp);
2533 printf("\tnlink=%d, effnlink=%d, size=%jd", ip->i_nlink,
2534 ip->i_effnlink, (intmax_t)ip->i_size);
2536 printf(", extsize %d", ip->i_din2->di_extsize);
2537 printf("\n\tgeneration=%jx, uid=%d, gid=%d, flags=0x%b\n",
2538 (uintmax_t)ip->i_gen, ip->i_uid, ip->i_gid,
2539 (u_int)ip->i_flags, PRINT_INODE_FLAGS);
2540 printf("\tino %lu, on dev %s", (u_long)ip->i_number,
2541 devtoname(ITODEV(ip)));
2542 if (vp->v_type == VFIFO)
2549 * Close wrapper for fifos.
2551 * Update the times on the inode then do device close.
2555 struct vop_close_args /* {
2558 struct ucred *a_cred;
2559 struct thread *a_td;
2562 struct vnode *vp = ap->a_vp;
2566 usecount = vp->v_usecount;
2568 ufs_itimes_locked(vp);
2570 return (fifo_specops.vop_close(ap));
2574 * Kqfilter wrapper for fifos.
2576 * Fall through to ufs kqfilter routines if needed
2579 ufsfifo_kqfilter(ap)
2580 struct vop_kqfilter_args *ap;
2584 error = fifo_specops.vop_kqfilter(ap);
2586 error = vfs_kqfilter(ap);
2591 * Return POSIX pathconf information applicable to ufs filesystems.
2595 struct vop_pathconf_args /* {
2604 switch (ap->a_name) {
2606 *ap->a_retval = UFS_LINK_MAX;
2609 *ap->a_retval = UFS_MAXNAMLEN;
2612 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
2613 *ap->a_retval = PIPE_BUF;
2617 case _PC_CHOWN_RESTRICTED:
2624 case _PC_ACL_EXTENDED:
2625 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
2631 if (ap->a_vp->v_mount->mnt_flag & MNT_NFS4ACLS)
2637 case _PC_ACL_PATH_MAX:
2639 if (ap->a_vp->v_mount->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS))
2640 *ap->a_retval = ACL_MAX_ENTRIES;
2648 case _PC_MAC_PRESENT:
2649 if (ap->a_vp->v_mount->mnt_flag & MNT_MULTILABEL)
2655 case _PC_MIN_HOLE_SIZE:
2656 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2664 case _PC_ALLOC_SIZE_MIN:
2665 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
2667 case _PC_FILESIZEBITS:
2670 case _PC_REC_INCR_XFER_SIZE:
2671 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2673 case _PC_REC_MAX_XFER_SIZE:
2674 *ap->a_retval = -1; /* means ``unlimited'' */
2676 case _PC_REC_MIN_XFER_SIZE:
2677 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2679 case _PC_REC_XFER_ALIGN:
2680 *ap->a_retval = PAGE_SIZE;
2682 case _PC_SYMLINK_MAX:
2683 *ap->a_retval = MAXPATHLEN;
2687 error = vop_stdpathconf(ap);
2694 * Initialize the vnode associated with a new inode, handle aliased
2698 ufs_vinit(mntp, fifoops, vpp)
2700 struct vop_vector *fifoops;
2707 ASSERT_VOP_LOCKED(vp, "ufs_vinit");
2709 vp->v_type = IFTOVT(ip->i_mode);
2711 * Only unallocated inodes should be of type VNON.
2713 if (ip->i_mode != 0 && vp->v_type == VNON)
2715 if (vp->v_type == VFIFO)
2717 if (ip->i_number == UFS_ROOTINO)
2718 vp->v_vflag |= VV_ROOT;
2724 * Allocate a new inode.
2725 * Vnode dvp must be locked.
2728 ufs_makeinode(mode, dvp, vpp, cnp, callfunc)
2732 struct componentname *cnp;
2733 const char *callfunc;
2735 struct inode *ip, *pdir;
2736 struct direct newdir;
2742 if ((cnp->cn_flags & HASBUF) == 0)
2743 panic("%s: no name", callfunc);
2746 if ((mode & IFMT) == 0)
2749 if (pdir->i_effnlink < 2) {
2750 print_bad_link_count(callfunc, dvp);
2753 if (DOINGSUJ(dvp)) {
2754 error = softdep_prelink(dvp, NULL, cnp);
2756 MPASS(error == ERELOOKUP);
2760 error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2764 ip->i_gid = pdir->i_gid;
2765 DIP_SET(ip, i_gid, pdir->i_gid);
2769 struct ucred ucred, *ucp;
2774 * If we are not the owner of the directory,
2775 * and we are hacking owners here, (only do this where told to)
2776 * and we are not giving it TO root, (would subvert quotas)
2777 * then go ahead and give it to the other user.
2778 * Note that this drops off the execute bits for security.
2780 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2781 (pdir->i_mode & ISUID) &&
2782 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2783 ip->i_uid = pdir->i_uid;
2784 DIP_SET(ip, i_uid, ip->i_uid);
2788 * Make sure the correct user gets charged
2790 * Quickly knock up a dummy credential for the victim.
2791 * XXX This seems to never be accessed out of our
2792 * context so a stack variable is ok.
2794 refcount_init(&ucred.cr_ref, 1);
2795 ucred.cr_uid = ip->i_uid;
2796 ucred.cr_ngroups = 1;
2797 ucred.cr_groups = &ucred_group;
2798 ucred.cr_groups[0] = pdir->i_gid;
2802 ip->i_uid = cnp->cn_cred->cr_uid;
2803 DIP_SET(ip, i_uid, ip->i_uid);
2807 if ((error = getinoquota(ip)) ||
2808 (error = chkiq(ip, 1, ucp, 0))) {
2809 if (DOINGSOFTDEP(tvp))
2810 softdep_revert_link(pdir, ip);
2811 UFS_VFREE(tvp, ip->i_number, mode);
2818 #else /* !SUIDDIR */
2819 ip->i_uid = cnp->cn_cred->cr_uid;
2820 DIP_SET(ip, i_uid, ip->i_uid);
2822 if ((error = getinoquota(ip)) ||
2823 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2824 if (DOINGSOFTDEP(tvp))
2825 softdep_revert_link(pdir, ip);
2826 UFS_VFREE(tvp, ip->i_number, mode);
2832 #endif /* !SUIDDIR */
2833 vn_seqc_write_begin(tvp); /* Mostly to cover asserts */
2834 UFS_INODE_SET_FLAG(ip, IN_ACCESS | IN_CHANGE | IN_UPDATE);
2835 UFS_INODE_SET_MODE(ip, mode);
2836 DIP_SET(ip, i_mode, mode);
2837 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2840 DIP_SET(ip, i_nlink, 1);
2841 if (DOINGSOFTDEP(tvp))
2842 softdep_setup_create(VTOI(dvp), ip);
2843 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2844 priv_check_cred(cnp->cn_cred, PRIV_VFS_SETGID)) {
2845 UFS_INODE_SET_MODE(ip, ip->i_mode & ~ISGID);
2846 DIP_SET(ip, i_mode, ip->i_mode);
2849 if (cnp->cn_flags & ISWHITEOUT) {
2850 ip->i_flags |= UF_OPAQUE;
2851 DIP_SET(ip, i_flags, ip->i_flags);
2855 * Make sure inode goes to disk before directory entry.
2857 error = UFS_UPDATE(tvp, !DOINGSOFTDEP(tvp) && !DOINGASYNC(tvp));
2861 if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
2862 error = mac_vnode_create_extattr(cnp->cn_cred, dvp->v_mount,
2869 if (dvp->v_mount->mnt_flag & MNT_ACLS) {
2870 error = ufs_do_posix1e_acl_inheritance_file(dvp, tvp, mode,
2871 cnp->cn_cred, cnp->cn_thread);
2874 } else if (dvp->v_mount->mnt_flag & MNT_NFS4ACLS) {
2875 error = ufs_do_nfs4_acl_inheritance(dvp, tvp, mode,
2876 cnp->cn_cred, cnp->cn_thread);
2880 #endif /* !UFS_ACL */
2881 ufs_makedirentry(ip, cnp, &newdir);
2882 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2885 vn_seqc_write_end(tvp);
2891 * Write error occurred trying to update the inode
2892 * or the directory so must deallocate the inode.
2896 DIP_SET(ip, i_nlink, 0);
2897 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
2898 if (DOINGSOFTDEP(tvp))
2899 softdep_revert_create(VTOI(dvp), ip);
2900 vn_seqc_write_end(tvp);
2907 ufs_ioctl(struct vop_ioctl_args *ap)
2913 switch (ap->a_command) {
2915 error = vn_lock(vp, LK_SHARED);
2917 error = ufs_bmap_seekdata(vp, (off_t *)ap->a_data);
2923 return (vn_bmap_seekhole(vp, ap->a_command, (off_t *)ap->a_data,
2931 ufs_read_pgcache(struct vop_read_pgcache_args *ap)
2938 VNPASS((vn_irflag_read(vp) & VIRF_PGREAD) != 0, vp);
2940 if (uio->uio_resid > ptoa(io_hold_cnt) || uio->uio_offset < 0 ||
2941 (ap->a_ioflag & IO_DIRECT) != 0)
2942 return (EJUSTRETURN);
2943 return (vn_read_from_obj(vp, uio));
2946 /* Global vfs data structures for ufs. */
2947 struct vop_vector ufs_vnodeops = {
2948 .vop_default = &default_vnodeops,
2949 .vop_fsync = VOP_PANIC,
2950 .vop_read = VOP_PANIC,
2951 .vop_reallocblks = VOP_PANIC,
2952 .vop_write = VOP_PANIC,
2953 .vop_accessx = ufs_accessx,
2954 .vop_bmap = ufs_bmap,
2955 .vop_fplookup_vexec = ufs_fplookup_vexec,
2956 .vop_fplookup_symlink = VOP_EAGAIN,
2957 .vop_cachedlookup = ufs_lookup,
2958 .vop_close = ufs_close,
2959 .vop_create = ufs_create,
2960 .vop_stat = ufs_stat,
2961 .vop_getattr = ufs_getattr,
2962 .vop_inactive = ufs_inactive,
2963 .vop_ioctl = ufs_ioctl,
2964 .vop_link = ufs_link,
2965 .vop_lookup = vfs_cache_lookup,
2966 .vop_mmapped = ufs_mmapped,
2967 .vop_mkdir = ufs_mkdir,
2968 .vop_mknod = ufs_mknod,
2969 .vop_need_inactive = ufs_need_inactive,
2970 .vop_open = ufs_open,
2971 .vop_pathconf = ufs_pathconf,
2972 .vop_poll = vop_stdpoll,
2973 .vop_print = ufs_print,
2974 .vop_read_pgcache = ufs_read_pgcache,
2975 .vop_readdir = ufs_readdir,
2976 .vop_readlink = ufs_readlink,
2977 .vop_reclaim = ufs_reclaim,
2978 .vop_remove = ufs_remove,
2979 .vop_rename = ufs_rename,
2980 .vop_rmdir = ufs_rmdir,
2981 .vop_setattr = ufs_setattr,
2983 .vop_setlabel = vop_stdsetlabel_ea,
2985 .vop_strategy = ufs_strategy,
2986 .vop_symlink = ufs_symlink,
2987 .vop_whiteout = ufs_whiteout,
2989 .vop_getextattr = ufs_getextattr,
2990 .vop_deleteextattr = ufs_deleteextattr,
2991 .vop_setextattr = ufs_setextattr,
2994 .vop_getacl = ufs_getacl,
2995 .vop_setacl = ufs_setacl,
2996 .vop_aclcheck = ufs_aclcheck,
2999 VFS_VOP_VECTOR_REGISTER(ufs_vnodeops);
3001 struct vop_vector ufs_fifoops = {
3002 .vop_default = &fifo_specops,
3003 .vop_fsync = VOP_PANIC,
3004 .vop_accessx = ufs_accessx,
3005 .vop_close = ufsfifo_close,
3006 .vop_getattr = ufs_getattr,
3007 .vop_inactive = ufs_inactive,
3008 .vop_kqfilter = ufsfifo_kqfilter,
3009 .vop_pathconf = ufs_pathconf,
3010 .vop_print = ufs_print,
3011 .vop_read = VOP_PANIC,
3012 .vop_reclaim = ufs_reclaim,
3013 .vop_setattr = ufs_setattr,
3015 .vop_setlabel = vop_stdsetlabel_ea,
3017 .vop_write = VOP_PANIC,
3019 .vop_getextattr = ufs_getextattr,
3020 .vop_deleteextattr = ufs_deleteextattr,
3021 .vop_setextattr = ufs_setextattr,
3024 .vop_getacl = ufs_getacl,
3025 .vop_setacl = ufs_setacl,
3026 .vop_aclcheck = ufs_aclcheck,
3029 VFS_VOP_VECTOR_REGISTER(ufs_fifoops);