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_accessx_t ufs_accessx;
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_accessx_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;
322 * Disallow write attempts on read-only filesystems;
323 * unless the file is a socket, fifo, or a block or
324 * character device resident on the filesystem.
326 if (accmode & VMODIFY_PERMS) {
327 switch (vp->v_type) {
331 if (vp->v_mount->mnt_flag & MNT_RDONLY)
335 * Inode is accounted in the quotas only if struct
336 * dquot is attached to it. VOP_ACCESS() is called
337 * from vn_open_cred() and provides a convenient
338 * point to call getinoquota().
340 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
343 * Upgrade vnode lock, since getinoquota()
344 * requires exclusive lock to modify inode.
348 vn_lock(vp, LK_UPGRADE | LK_RETRY);
350 if (vp->v_iflag & VI_DOOMED) {
358 error = getinoquota(ip);
361 vn_lock(vp, LK_DOWNGRADE | LK_RETRY);
372 * If immutable bit set, nobody gets to write it. "& ~VADMIN_PERMS"
373 * is here, because without it, * it would be impossible for the owner
374 * to remove the IMMUTABLE flag.
376 if ((accmode & (VMODIFY_PERMS & ~VADMIN_PERMS)) &&
377 (ip->i_flags & (IMMUTABLE | SF_SNAPSHOT)))
381 if ((vp->v_mount->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS)) != 0) {
382 if (vp->v_mount->mnt_flag & MNT_NFS4ACLS)
383 type = ACL_TYPE_NFS4;
385 type = ACL_TYPE_ACCESS;
387 acl = acl_alloc(M_WAITOK);
388 if (type == ACL_TYPE_NFS4)
389 error = ufs_getacl_nfs4_internal(vp, acl, ap->a_td);
391 error = VOP_GETACL(vp, type, acl, ap->a_cred, ap->a_td);
394 if (type == ACL_TYPE_NFS4) {
395 error = vaccess_acl_nfs4(vp->v_type, ip->i_uid,
396 ip->i_gid, acl, accmode, ap->a_cred, NULL);
398 error = vfs_unixify_accmode(&accmode);
400 error = vaccess_acl_posix1e(vp->v_type, ip->i_uid,
401 ip->i_gid, acl, accmode, ap->a_cred, NULL);
405 if (error != EOPNOTSUPP)
407 "ufs_accessx(): Error retrieving ACL on object (%d).\n",
410 * XXX: Fall back until debugged. Should
411 * eventually possibly log an error, and return
414 error = vfs_unixify_accmode(&accmode);
416 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid,
417 ip->i_gid, accmode, ap->a_cred, NULL);
423 #endif /* !UFS_ACL */
424 error = vfs_unixify_accmode(&accmode);
426 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
427 accmode, ap->a_cred, NULL);
434 struct vop_getattr_args /* {
437 struct ucred *a_cred;
440 struct vnode *vp = ap->a_vp;
441 struct inode *ip = VTOI(vp);
442 struct vattr *vap = ap->a_vap;
445 ufs_itimes_locked(vp);
446 if (ip->i_ump->um_fstype == UFS1) {
447 vap->va_atime.tv_sec = ip->i_din1->di_atime;
448 vap->va_atime.tv_nsec = ip->i_din1->di_atimensec;
450 vap->va_atime.tv_sec = ip->i_din2->di_atime;
451 vap->va_atime.tv_nsec = ip->i_din2->di_atimensec;
455 * Copy from inode table
457 vap->va_fsid = dev2udev(ip->i_dev);
458 vap->va_fileid = ip->i_number;
459 vap->va_mode = ip->i_mode & ~IFMT;
460 vap->va_nlink = ip->i_effnlink;
461 vap->va_uid = ip->i_uid;
462 vap->va_gid = ip->i_gid;
463 if (ip->i_ump->um_fstype == UFS1) {
464 vap->va_rdev = ip->i_din1->di_rdev;
465 vap->va_size = ip->i_din1->di_size;
466 vap->va_mtime.tv_sec = ip->i_din1->di_mtime;
467 vap->va_mtime.tv_nsec = ip->i_din1->di_mtimensec;
468 vap->va_ctime.tv_sec = ip->i_din1->di_ctime;
469 vap->va_ctime.tv_nsec = ip->i_din1->di_ctimensec;
470 vap->va_bytes = dbtob((u_quad_t)ip->i_din1->di_blocks);
471 vap->va_filerev = ip->i_din1->di_modrev;
473 vap->va_rdev = ip->i_din2->di_rdev;
474 vap->va_size = ip->i_din2->di_size;
475 vap->va_mtime.tv_sec = ip->i_din2->di_mtime;
476 vap->va_mtime.tv_nsec = ip->i_din2->di_mtimensec;
477 vap->va_ctime.tv_sec = ip->i_din2->di_ctime;
478 vap->va_ctime.tv_nsec = ip->i_din2->di_ctimensec;
479 vap->va_birthtime.tv_sec = ip->i_din2->di_birthtime;
480 vap->va_birthtime.tv_nsec = ip->i_din2->di_birthnsec;
481 vap->va_bytes = dbtob((u_quad_t)ip->i_din2->di_blocks);
482 vap->va_filerev = ip->i_din2->di_modrev;
484 vap->va_flags = ip->i_flags;
485 vap->va_gen = ip->i_gen;
486 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
487 vap->va_type = IFTOVT(ip->i_mode);
492 * Set attribute vnode op. called from several syscalls
496 struct vop_setattr_args /* {
499 struct ucred *a_cred;
502 struct vattr *vap = ap->a_vap;
503 struct vnode *vp = ap->a_vp;
504 struct inode *ip = VTOI(vp);
505 struct ucred *cred = ap->a_cred;
506 struct thread *td = curthread;
510 * Check for unsettable attributes.
512 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
513 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
514 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
515 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
518 if (vap->va_flags != VNOVAL) {
519 if (vp->v_mount->mnt_flag & MNT_RDONLY)
522 * Callers may only modify the file flags on objects they
523 * have VADMIN rights for.
525 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
528 * Unprivileged processes are not permitted to unset system
529 * flags, or modify flags if any system flags are set.
530 * Privileged non-jail processes may not modify system flags
531 * if securelevel > 0 and any existing system flags are set.
532 * Privileged jail processes behave like privileged non-jail
533 * processes if the security.jail.chflags_allowed sysctl is
534 * is non-zero; otherwise, they behave like unprivileged
537 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
539 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
540 error = securelevel_gt(cred, 0);
544 /* Snapshot flag cannot be set or cleared */
545 if (((vap->va_flags & SF_SNAPSHOT) != 0 &&
546 (ip->i_flags & SF_SNAPSHOT) == 0) ||
547 ((vap->va_flags & SF_SNAPSHOT) == 0 &&
548 (ip->i_flags & SF_SNAPSHOT) != 0))
550 ip->i_flags = vap->va_flags;
551 DIP_SET(ip, i_flags, vap->va_flags);
554 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
555 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
557 ip->i_flags &= SF_SETTABLE;
558 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
559 DIP_SET(ip, i_flags, ip->i_flags);
561 ip->i_flag |= IN_CHANGE;
562 if (vap->va_flags & (IMMUTABLE | APPEND))
565 if (ip->i_flags & (IMMUTABLE | APPEND))
568 * Go through the fields and update iff not VNOVAL.
570 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
571 if (vp->v_mount->mnt_flag & MNT_RDONLY)
573 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred,
577 if (vap->va_size != VNOVAL) {
579 * XXX most of the following special cases should be in
580 * callers instead of in N filesystems. The VDIR check
583 switch (vp->v_type) {
589 * Truncation should have an effect in these cases.
590 * Disallow it if the filesystem is read-only or
591 * the file is being snapshotted.
593 if (vp->v_mount->mnt_flag & MNT_RDONLY)
595 if ((ip->i_flags & SF_SNAPSHOT) != 0)
600 * According to POSIX, the result is unspecified
601 * for file types other than regular files,
602 * directories and shared memory objects. We
603 * don't support shared memory objects in the file
604 * system, and have dubious support for truncating
605 * symlinks. Just ignore the request in other cases.
609 if ((error = UFS_TRUNCATE(vp, vap->va_size, IO_NORMAL,
613 if (vap->va_atime.tv_sec != VNOVAL ||
614 vap->va_mtime.tv_sec != VNOVAL ||
615 vap->va_birthtime.tv_sec != VNOVAL) {
616 if (vp->v_mount->mnt_flag & MNT_RDONLY)
618 if ((ip->i_flags & SF_SNAPSHOT) != 0)
622 * If times is NULL, ... The caller must be the owner of
623 * the file, have permission to write the file, or be the
625 * If times is non-NULL, ... The caller must be the owner of
626 * the file or be the super-user.
628 * Possibly for historical reasons, try to use VADMIN in
629 * preference to VWRITE for a NULL timestamp. This means we
630 * will return EACCES in preference to EPERM if neither
633 if (vap->va_vaflags & VA_UTIMES_NULL) {
635 * NFSv4.1, draft 21, 6.2.1.3.1, Discussion of Mask Attributes
637 * "A user having ACL_WRITE_DATA or ACL_WRITE_ATTRIBUTES
638 * will be allowed to set the times [..] to the current
641 * XXX: Calling it four times seems a little excessive.
643 error = VOP_ACCESSX(vp, VWRITE_ATTRIBUTES, cred, td);
645 error = VOP_ACCESS(vp, VWRITE, cred, td);
647 error = VOP_ACCESSX(vp, VWRITE_ATTRIBUTES, cred, td);
650 if (vap->va_atime.tv_sec != VNOVAL)
651 ip->i_flag |= IN_ACCESS;
652 if (vap->va_mtime.tv_sec != VNOVAL)
653 ip->i_flag |= IN_CHANGE | IN_UPDATE;
654 if (vap->va_birthtime.tv_sec != VNOVAL &&
655 ip->i_ump->um_fstype == UFS2)
656 ip->i_flag |= IN_MODIFIED;
658 if (vap->va_atime.tv_sec != VNOVAL) {
659 DIP_SET(ip, i_atime, vap->va_atime.tv_sec);
660 DIP_SET(ip, i_atimensec, vap->va_atime.tv_nsec);
662 if (vap->va_mtime.tv_sec != VNOVAL) {
663 DIP_SET(ip, i_mtime, vap->va_mtime.tv_sec);
664 DIP_SET(ip, i_mtimensec, vap->va_mtime.tv_nsec);
666 if (vap->va_birthtime.tv_sec != VNOVAL &&
667 ip->i_ump->um_fstype == UFS2) {
668 ip->i_din2->di_birthtime = vap->va_birthtime.tv_sec;
669 ip->i_din2->di_birthnsec = vap->va_birthtime.tv_nsec;
671 error = UFS_UPDATE(vp, 0);
676 if (vap->va_mode != (mode_t)VNOVAL) {
677 if (vp->v_mount->mnt_flag & MNT_RDONLY)
679 if ((ip->i_flags & SF_SNAPSHOT) != 0 && (vap->va_mode &
680 (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP | S_IXOTH | S_IWOTH)))
682 error = ufs_chmod(vp, (int)vap->va_mode, cred, td);
689 ufs_update_nfs4_acl_after_mode_change(struct vnode *vp, int mode,
690 int file_owner_id, struct ucred *cred, struct thread *td)
695 aclp = acl_alloc(M_WAITOK);
696 error = ufs_getacl_nfs4_internal(vp, aclp, td);
698 * We don't have to handle EOPNOTSUPP here, as the filesystem claims
704 acl_nfs4_sync_acl_from_mode(aclp, mode, file_owner_id);
705 error = ufs_setacl_nfs4_internal(vp, aclp, td);
714 * Mark this file's access time for update for vfs_mark_atime(). This
715 * is called from execve() and mmap().
719 struct vop_markatime_args /* {
723 struct vnode *vp = ap->a_vp;
724 struct inode *ip = VTOI(vp);
727 ip->i_flag |= IN_ACCESS;
733 * Change the mode on a file.
734 * Inode must be locked before calling.
737 ufs_chmod(vp, mode, cred, td)
743 struct inode *ip = VTOI(vp);
747 * To modify the permissions on a file, must possess VADMIN
750 if ((error = VOP_ACCESSX(vp, VWRITE_ACL, cred, td)))
753 * Privileged processes may set the sticky bit on non-directories,
754 * as well as set the setgid bit on a file with a group that the
755 * process is not a member of. Both of these are allowed in
758 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
759 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0))
762 if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
763 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
769 * Deny setting setuid if we are not the file owner.
771 if ((mode & ISUID) && ip->i_uid != cred->cr_uid) {
772 error = priv_check_cred(cred, PRIV_VFS_ADMIN, 0);
777 ip->i_mode &= ~ALLPERMS;
778 ip->i_mode |= (mode & ALLPERMS);
779 DIP_SET(ip, i_mode, ip->i_mode);
780 ip->i_flag |= IN_CHANGE;
782 if ((vp->v_mount->mnt_flag & MNT_NFS4ACLS) != 0)
783 error = ufs_update_nfs4_acl_after_mode_change(vp, mode, ip->i_uid, cred, td);
789 * Perform chown operation on inode ip;
790 * inode must be locked prior to call.
793 ufs_chown(vp, uid, gid, cred, td)
800 struct inode *ip = VTOI(vp);
809 if (uid == (uid_t)VNOVAL)
811 if (gid == (gid_t)VNOVAL)
814 * To modify the ownership of a file, must possess VADMIN for that
817 if ((error = VOP_ACCESSX(vp, VWRITE_OWNER, cred, td)))
820 * To change the owner of a file, or change the group of a file to a
821 * group of which we are not a member, the caller must have
824 if (((uid != ip->i_uid && uid != cred->cr_uid) ||
825 (gid != ip->i_gid && !groupmember(gid, cred))) &&
826 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
831 if ((error = getinoquota(ip)) != 0)
834 dqrele(vp, ip->i_dquot[USRQUOTA]);
835 ip->i_dquot[USRQUOTA] = NODQUOT;
838 dqrele(vp, ip->i_dquot[GRPQUOTA]);
839 ip->i_dquot[GRPQUOTA] = NODQUOT;
841 change = DIP(ip, i_blocks);
842 (void) chkdq(ip, -change, cred, CHOWN);
843 (void) chkiq(ip, -1, cred, CHOWN);
844 for (i = 0; i < MAXQUOTAS; i++) {
845 dqrele(vp, ip->i_dquot[i]);
846 ip->i_dquot[i] = NODQUOT;
850 DIP_SET(ip, i_gid, gid);
852 DIP_SET(ip, i_uid, uid);
854 if ((error = getinoquota(ip)) == 0) {
856 dqrele(vp, ip->i_dquot[USRQUOTA]);
857 ip->i_dquot[USRQUOTA] = NODQUOT;
860 dqrele(vp, ip->i_dquot[GRPQUOTA]);
861 ip->i_dquot[GRPQUOTA] = NODQUOT;
863 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
864 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
867 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
869 for (i = 0; i < MAXQUOTAS; i++) {
870 dqrele(vp, ip->i_dquot[i]);
871 ip->i_dquot[i] = NODQUOT;
875 DIP_SET(ip, i_gid, ogid);
877 DIP_SET(ip, i_uid, ouid);
878 if (getinoquota(ip) == 0) {
880 dqrele(vp, ip->i_dquot[USRQUOTA]);
881 ip->i_dquot[USRQUOTA] = NODQUOT;
884 dqrele(vp, ip->i_dquot[GRPQUOTA]);
885 ip->i_dquot[GRPQUOTA] = NODQUOT;
887 (void) chkdq(ip, change, cred, FORCE|CHOWN);
888 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
889 (void) getinoquota(ip);
894 panic("ufs_chown: lost quota");
896 ip->i_flag |= IN_CHANGE;
897 if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
898 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0)) {
899 ip->i_mode &= ~(ISUID | ISGID);
900 DIP_SET(ip, i_mode, ip->i_mode);
908 struct vop_remove_args /* {
911 struct componentname *a_cnp;
915 struct vnode *vp = ap->a_vp;
916 struct vnode *dvp = ap->a_dvp;
922 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
923 (VTOI(dvp)->i_flags & APPEND)) {
928 ufs_gjournal_orphan(vp);
930 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
931 if (ip->i_nlink <= 0)
932 vp->v_vflag |= VV_NOSYNC;
933 if ((ip->i_flags & SF_SNAPSHOT) != 0) {
935 * Avoid deadlock where another thread is trying to
936 * update the inodeblock for dvp and is waiting on
937 * snaplk. Temporary unlock the vnode lock for the
938 * unlinked file and sync the directory. This should
939 * allow vput() of the directory to not block later on
940 * while holding the snapshot vnode locked, assuming
941 * that the directory hasn't been unlinked too.
944 (void) VOP_FSYNC(dvp, MNT_WAIT, td);
945 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
956 struct vop_link_args /* {
957 struct vnode *a_tdvp;
959 struct componentname *a_cnp;
962 struct vnode *vp = ap->a_vp;
963 struct vnode *tdvp = ap->a_tdvp;
964 struct componentname *cnp = ap->a_cnp;
966 struct direct newdir;
970 if ((cnp->cn_flags & HASBUF) == 0)
971 panic("ufs_link: no name");
973 if (tdvp->v_mount != vp->v_mount) {
978 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
982 if (ip->i_flags & (IMMUTABLE | APPEND)) {
988 DIP_SET(ip, i_nlink, ip->i_nlink);
989 ip->i_flag |= IN_CHANGE;
990 if (DOINGSOFTDEP(vp))
991 softdep_change_linkcnt(ip);
992 error = UFS_UPDATE(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
994 ufs_makedirentry(ip, cnp, &newdir);
995 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
1001 DIP_SET(ip, i_nlink, ip->i_nlink);
1002 ip->i_flag |= IN_CHANGE;
1003 if (DOINGSOFTDEP(vp))
1004 softdep_change_linkcnt(ip);
1011 * whiteout vnode call
1015 struct vop_whiteout_args /* {
1016 struct vnode *a_dvp;
1017 struct componentname *a_cnp;
1021 struct vnode *dvp = ap->a_dvp;
1022 struct componentname *cnp = ap->a_cnp;
1023 struct direct newdir;
1026 switch (ap->a_flags) {
1028 /* 4.4 format directories support whiteout operations */
1029 if (dvp->v_mount->mnt_maxsymlinklen > 0)
1031 return (EOPNOTSUPP);
1034 /* create a new directory whiteout */
1036 if ((cnp->cn_flags & SAVENAME) == 0)
1037 panic("ufs_whiteout: missing name");
1038 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
1039 panic("ufs_whiteout: old format filesystem");
1042 newdir.d_ino = WINO;
1043 newdir.d_namlen = cnp->cn_namelen;
1044 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
1045 newdir.d_type = DT_WHT;
1046 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
1050 /* remove an existing directory whiteout */
1052 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
1053 panic("ufs_whiteout: old format filesystem");
1056 cnp->cn_flags &= ~DOWHITEOUT;
1057 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
1060 panic("ufs_whiteout: unknown op");
1066 * Rename system call.
1067 * rename("foo", "bar");
1070 * link("foo", "bar");
1072 * but ``atomically''. Can't do full commit without saving state in the
1073 * inode on disk which isn't feasible at this time. Best we can do is
1074 * always guarantee the target exists.
1076 * Basic algorithm is:
1078 * 1) Bump link count on source while we're linking it to the
1079 * target. This also ensure the inode won't be deleted out
1080 * from underneath us while we work (it may be truncated by
1081 * a concurrent `trunc' or `open' for creation).
1082 * 2) Link source to destination. If destination already exists,
1084 * 3) Unlink source reference to inode if still around. If a
1085 * directory was moved and the parent of the destination
1086 * is different from the source, patch the ".." entry in the
1091 struct vop_rename_args /* {
1092 struct vnode *a_fdvp;
1093 struct vnode *a_fvp;
1094 struct componentname *a_fcnp;
1095 struct vnode *a_tdvp;
1096 struct vnode *a_tvp;
1097 struct componentname *a_tcnp;
1100 struct vnode *tvp = ap->a_tvp;
1101 struct vnode *tdvp = ap->a_tdvp;
1102 struct vnode *fvp = ap->a_fvp;
1103 struct vnode *fdvp = ap->a_fdvp;
1104 struct componentname *tcnp = ap->a_tcnp;
1105 struct componentname *fcnp = ap->a_fcnp;
1106 struct thread *td = fcnp->cn_thread;
1107 struct inode *ip, *xp, *dp;
1108 struct direct newdir;
1109 int doingdirectory = 0, oldparent = 0, newparent = 0;
1110 int error = 0, ioflag;
1114 if ((tcnp->cn_flags & HASBUF) == 0 ||
1115 (fcnp->cn_flags & HASBUF) == 0)
1116 panic("ufs_rename: no name");
1119 * Check for cross-device rename.
1121 if ((fvp->v_mount != tdvp->v_mount) ||
1122 (tvp && (fvp->v_mount != tvp->v_mount))) {
1136 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1137 (VTOI(tdvp)->i_flags & APPEND))) {
1143 * Renaming a file to itself has no effect. The upper layers should
1144 * not call us in that case. Temporarily just warn if they do.
1147 printf("ufs_rename: fvp == tvp (can't happen)\n");
1152 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
1156 if (ip->i_nlink >= LINK_MAX) {
1161 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
1162 || (dp->i_flags & APPEND)) {
1167 if ((ip->i_mode & IFMT) == IFDIR) {
1169 * Avoid ".", "..", and aliases of "." for obvious reasons.
1171 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1172 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
1173 (ip->i_flag & IN_RENAME)) {
1178 ip->i_flag |= IN_RENAME;
1179 oldparent = dp->i_number;
1185 * When the target exists, both the directory
1186 * and target vnodes are returned locked.
1194 * 1) Bump link count while we're moving stuff
1195 * around. If we crash somewhere before
1196 * completing our work, the link count
1197 * may be wrong, but correctable.
1201 DIP_SET(ip, i_nlink, ip->i_nlink);
1202 ip->i_flag |= IN_CHANGE;
1203 if (DOINGSOFTDEP(fvp))
1204 softdep_change_linkcnt(ip);
1205 if ((error = UFS_UPDATE(fvp, !(DOINGSOFTDEP(fvp) |
1206 DOINGASYNC(fvp)))) != 0) {
1212 * If ".." must be changed (ie the directory gets a new
1213 * parent) then the source directory must not be in the
1214 * directory hierarchy above the target, as this would
1215 * orphan everything below the source directory. Also
1216 * the user must have write permission in the source so
1217 * as to be able to change "..". We must repeat the call
1218 * to namei, as the parent directory is unlocked by the
1219 * call to checkpath().
1221 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
1222 fvp_ino = ip->i_number;
1224 if (oldparent != dp->i_number)
1225 newparent = dp->i_number;
1226 if (doingdirectory && newparent) {
1227 if (error) /* write access check above */
1231 error = ufs_checkpath(fvp_ino, dp, tcnp->cn_cred);
1234 if ((tcnp->cn_flags & SAVESTART) == 0)
1235 panic("ufs_rename: lost to startdir");
1237 error = relookup(tdvp, &tvp, tcnp);
1247 * 2) If target doesn't exist, link the target
1248 * to the source and unlink the source.
1249 * Otherwise, rewrite the target directory
1250 * entry to reference the source inode and
1251 * expunge the original entry's existence.
1254 if (dp->i_dev != ip->i_dev)
1255 panic("ufs_rename: EXDEV");
1257 * Account for ".." in new directory.
1258 * When source and destination have the same
1259 * parent we don't fool with the link count.
1261 if (doingdirectory && newparent) {
1262 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1268 DIP_SET(dp, i_nlink, dp->i_nlink);
1269 dp->i_flag |= IN_CHANGE;
1270 if (DOINGSOFTDEP(tdvp))
1271 softdep_change_linkcnt(dp);
1272 error = UFS_UPDATE(tdvp, !(DOINGSOFTDEP(tdvp) |
1277 ufs_makedirentry(ip, tcnp, &newdir);
1278 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1280 if (doingdirectory && newparent) {
1283 DIP_SET(dp, i_nlink, dp->i_nlink);
1284 dp->i_flag |= IN_CHANGE;
1285 if (DOINGSOFTDEP(tdvp))
1286 softdep_change_linkcnt(dp);
1287 (void)UFS_UPDATE(tdvp, 1);
1293 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1294 panic("ufs_rename: EXDEV");
1296 * Short circuit rename(foo, foo).
1298 if (xp->i_number == ip->i_number)
1299 panic("ufs_rename: same file");
1301 * If the parent directory is "sticky", then the caller
1302 * must possess VADMIN for the parent directory, or the
1303 * destination of the rename. This implements append-only
1306 if ((dp->i_mode & S_ISTXT) &&
1307 VOP_ACCESS(tdvp, VADMIN, tcnp->cn_cred, td) &&
1308 VOP_ACCESS(tvp, VADMIN, tcnp->cn_cred, td)) {
1313 * Target must be empty if a directory and have no links
1314 * to it. Also, ensure source and target are compatible
1315 * (both directories, or both not directories).
1317 if ((xp->i_mode&IFMT) == IFDIR) {
1318 if ((xp->i_effnlink > 2) ||
1319 !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1323 if (!doingdirectory) {
1328 } else if (doingdirectory) {
1332 error = ufs_dirrewrite(dp, xp, ip->i_number,
1334 (doingdirectory && newparent) ? newparent : doingdirectory);
1337 if (doingdirectory) {
1340 if (DOINGSOFTDEP(tdvp))
1341 softdep_change_linkcnt(dp);
1344 if (DOINGSOFTDEP(tvp))
1345 softdep_change_linkcnt(xp);
1347 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1349 * Truncate inode. The only stuff left in the directory
1350 * is "." and "..". The "." reference is inconsequential
1351 * since we are quashing it. We have removed the "."
1352 * reference and the reference in the parent directory,
1353 * but there may be other hard links. The soft
1354 * dependency code will arrange to do these operations
1355 * after the parent directory entry has been deleted on
1356 * disk, so when running with that code we avoid doing
1361 DIP_SET(dp, i_nlink, dp->i_nlink);
1362 dp->i_flag |= IN_CHANGE;
1365 DIP_SET(xp, i_nlink, xp->i_nlink);
1366 xp->i_flag |= IN_CHANGE;
1368 if (!DOINGASYNC(tvp))
1370 if ((error = UFS_TRUNCATE(tvp, (off_t)0, ioflag,
1371 tcnp->cn_cred, tcnp->cn_thread)) != 0)
1380 * 3) Unlink the source.
1382 fcnp->cn_flags &= ~MODMASK;
1383 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1384 if ((fcnp->cn_flags & SAVESTART) == 0)
1385 panic("ufs_rename: lost from startdir");
1387 error = relookup(fdvp, &fvp, fcnp);
1395 * From name has disappeared. IN_RENAME is not sufficient
1396 * to protect against directory races due to timing windows,
1397 * so we have to remove the panic. XXX the only real way
1398 * to solve this issue is at a much higher level. By the
1399 * time we hit ufs_rename() it's too late.
1403 panic("ufs_rename: lost dir entry");
1409 * Ensure that the directory entry still exists and has not
1410 * changed while the new name has been entered. If the source is
1411 * a file then the entry may have been unlinked or renamed. In
1412 * either case there is no further work to be done. If the source
1413 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1414 * flag ensures that it cannot be moved by another rename or removed
1419 * From name resolves to a different inode. IN_RENAME is
1420 * not sufficient protection against timing window races
1421 * so we can't panic here. XXX the only real way
1422 * to solve this issue is at a much higher level. By the
1423 * time we hit ufs_rename() it's too late.
1427 panic("ufs_rename: lost dir entry");
1431 * If the source is a directory with a
1432 * new parent, the link count of the old
1433 * parent directory must be decremented
1434 * and ".." set to point to the new parent.
1436 if (doingdirectory && newparent) {
1437 xp->i_offset = mastertemplate.dot_reclen;
1438 ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1441 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1442 xp->i_flag &= ~IN_RENAME;
1457 ip->i_flag &= ~IN_RENAME;
1458 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1461 DIP_SET(ip, i_nlink, ip->i_nlink);
1462 ip->i_flag |= IN_CHANGE;
1463 ip->i_flag &= ~IN_RENAME;
1464 if (DOINGSOFTDEP(fvp))
1465 softdep_change_linkcnt(ip);
1474 ufs_do_posix1e_acl_inheritance_dir(struct vnode *dvp, struct vnode *tvp,
1475 mode_t dmode, struct ucred *cred, struct thread *td)
1478 struct inode *ip = VTOI(tvp);
1479 struct acl *dacl, *acl;
1481 acl = acl_alloc(M_WAITOK);
1482 dacl = acl_alloc(M_WAITOK);
1485 * Retrieve default ACL from parent, if any.
1487 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cred, td);
1491 * Retrieved a default ACL, so merge mode and ACL if
1492 * necessary. If the ACL is empty, fall through to
1493 * the "not defined or available" case.
1495 if (acl->acl_cnt != 0) {
1496 dmode = acl_posix1e_newfilemode(dmode, acl);
1498 DIP_SET(ip, i_mode, dmode);
1500 ufs_sync_acl_from_inode(ip, acl);
1507 * Just use the mode as-is.
1510 DIP_SET(ip, i_mode, dmode);
1519 * XXX: If we abort now, will Soft Updates notify the extattr
1520 * code that the EAs for the file need to be released?
1522 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cred, td);
1524 error = VOP_SETACL(tvp, ACL_TYPE_DEFAULT, dacl, cred, td);
1531 * XXX: This should not happen, as EOPNOTSUPP above
1532 * was supposed to free acl.
1534 printf("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()\n");
1536 panic("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()");
1552 ufs_do_posix1e_acl_inheritance_file(struct vnode *dvp, struct vnode *tvp,
1553 mode_t mode, struct ucred *cred, struct thread *td)
1556 struct inode *ip = VTOI(tvp);
1559 acl = acl_alloc(M_WAITOK);
1562 * Retrieve default ACL for parent, if any.
1564 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cred, td);
1568 * Retrieved a default ACL, so merge mode and ACL if
1571 if (acl->acl_cnt != 0) {
1573 * Two possible ways for default ACL to not
1574 * be present. First, the EA can be
1575 * undefined, or second, the default ACL can
1576 * be blank. If it's blank, fall through to
1577 * the it's not defined case.
1579 mode = acl_posix1e_newfilemode(mode, acl);
1581 DIP_SET(ip, i_mode, mode);
1582 ufs_sync_acl_from_inode(ip, acl);
1589 * Just use the mode as-is.
1592 DIP_SET(ip, i_mode, mode);
1601 * XXX: If we abort now, will Soft Updates notify the extattr
1602 * code that the EAs for the file need to be released?
1604 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cred, td);
1611 * XXX: This should not happen, as EOPNOTSUPP above was
1612 * supposed to free acl.
1614 printf("ufs_makeinode: VOP_GETACL() but no "
1616 /* panic("ufs_makeinode: VOP_GETACL() but no "
1631 ufs_do_nfs4_acl_inheritance(struct vnode *dvp, struct vnode *tvp,
1632 mode_t child_mode, struct ucred *cred, struct thread *td)
1635 struct acl *parent_aclp, *child_aclp;
1637 parent_aclp = acl_alloc(M_WAITOK);
1638 child_aclp = acl_alloc(M_WAITOK | M_ZERO);
1640 error = ufs_getacl_nfs4_internal(dvp, parent_aclp, td);
1643 acl_nfs4_compute_inherited_acl(parent_aclp, child_aclp,
1644 child_mode, VTOI(tvp)->i_uid, tvp->v_type == VDIR);
1645 error = ufs_setacl_nfs4_internal(tvp, child_aclp, td);
1649 acl_free(parent_aclp);
1650 acl_free(child_aclp);
1661 struct vop_mkdir_args /* {
1662 struct vnode *a_dvp;
1663 struct vnode **a_vpp;
1664 struct componentname *a_cnp;
1665 struct vattr *a_vap;
1668 struct vnode *dvp = ap->a_dvp;
1669 struct vattr *vap = ap->a_vap;
1670 struct componentname *cnp = ap->a_cnp;
1671 struct inode *ip, *dp;
1674 struct dirtemplate dirtemplate, *dtp;
1675 struct direct newdir;
1680 if ((cnp->cn_flags & HASBUF) == 0)
1681 panic("ufs_mkdir: no name");
1684 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1688 dmode = vap->va_mode & 0777;
1691 * Must simulate part of ufs_makeinode here to acquire the inode,
1692 * but not have it entered in the parent directory. The entry is
1693 * made later after writing "." and ".." entries.
1695 error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
1699 ip->i_gid = dp->i_gid;
1700 DIP_SET(ip, i_gid, dp->i_gid);
1704 struct ucred ucred, *ucp;
1709 * If we are hacking owners here, (only do this where told to)
1710 * and we are not giving it TO root, (would subvert quotas)
1711 * then go ahead and give it to the other user.
1712 * The new directory also inherits the SUID bit.
1713 * If user's UID and dir UID are the same,
1714 * 'give it away' so that the SUID is still forced on.
1716 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1717 (dp->i_mode & ISUID) && dp->i_uid) {
1719 ip->i_uid = dp->i_uid;
1720 DIP_SET(ip, i_uid, dp->i_uid);
1722 if (dp->i_uid != cnp->cn_cred->cr_uid) {
1724 * Make sure the correct user gets charged
1726 * Make a dummy credential for the victim.
1727 * XXX This seems to never be accessed out of
1728 * our context so a stack variable is ok.
1730 refcount_init(&ucred.cr_ref, 1);
1731 ucred.cr_uid = ip->i_uid;
1732 ucred.cr_ngroups = 1;
1733 ucred.cr_groups = &ucred_group;
1734 ucred.cr_groups[0] = dp->i_gid;
1739 ip->i_uid = cnp->cn_cred->cr_uid;
1740 DIP_SET(ip, i_uid, ip->i_uid);
1743 if ((error = getinoquota(ip)) ||
1744 (error = chkiq(ip, 1, ucp, 0))) {
1745 UFS_VFREE(tvp, ip->i_number, dmode);
1751 #else /* !SUIDDIR */
1752 ip->i_uid = cnp->cn_cred->cr_uid;
1753 DIP_SET(ip, i_uid, ip->i_uid);
1755 if ((error = getinoquota(ip)) ||
1756 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1757 UFS_VFREE(tvp, ip->i_number, dmode);
1762 #endif /* !SUIDDIR */
1763 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1765 DIP_SET(ip, i_mode, dmode);
1766 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1769 DIP_SET(ip, i_nlink, 2);
1770 if (DOINGSOFTDEP(tvp))
1771 softdep_change_linkcnt(ip);
1772 if (cnp->cn_flags & ISWHITEOUT) {
1773 ip->i_flags |= UF_OPAQUE;
1774 DIP_SET(ip, i_flags, ip->i_flags);
1778 * Bump link count in parent directory to reflect work done below.
1779 * Should be done before reference is created so cleanup is
1780 * possible if we crash.
1784 DIP_SET(dp, i_nlink, dp->i_nlink);
1785 dp->i_flag |= IN_CHANGE;
1786 if (DOINGSOFTDEP(dvp))
1787 softdep_change_linkcnt(dp);
1788 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1792 if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
1793 error = mac_vnode_create_extattr(cnp->cn_cred, dvp->v_mount,
1800 if (dvp->v_mount->mnt_flag & MNT_ACLS) {
1801 error = ufs_do_posix1e_acl_inheritance_dir(dvp, tvp, dmode,
1802 cnp->cn_cred, cnp->cn_thread);
1805 } else if (dvp->v_mount->mnt_flag & MNT_NFS4ACLS) {
1806 error = ufs_do_nfs4_acl_inheritance(dvp, tvp, dmode,
1807 cnp->cn_cred, cnp->cn_thread);
1811 #endif /* !UFS_ACL */
1814 * Initialize directory with "." and ".." from static template.
1816 if (dvp->v_mount->mnt_maxsymlinklen > 0)
1817 dtp = &mastertemplate;
1819 dtp = (struct dirtemplate *)&omastertemplate;
1821 dirtemplate.dot_ino = ip->i_number;
1822 dirtemplate.dotdot_ino = dp->i_number;
1823 if ((error = UFS_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred,
1824 BA_CLRBUF, &bp)) != 0)
1826 ip->i_size = DIRBLKSIZ;
1827 DIP_SET(ip, i_size, DIRBLKSIZ);
1828 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1829 vnode_pager_setsize(tvp, (u_long)ip->i_size);
1830 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1831 if (DOINGSOFTDEP(tvp)) {
1833 * Ensure that the entire newly allocated block is a
1834 * valid directory so that future growth within the
1835 * block does not have to ensure that the block is
1836 * written before the inode.
1839 while (blkoff < bp->b_bcount) {
1841 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1842 blkoff += DIRBLKSIZ;
1845 if ((error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) |
1846 DOINGASYNC(tvp)))) != 0) {
1851 * Directory set up, now install its entry in the parent directory.
1853 * If we are not doing soft dependencies, then we must write out the
1854 * buffer containing the new directory body before entering the new
1855 * name in the parent. If we are doing soft dependencies, then the
1856 * buffer containing the new directory body will be passed to and
1857 * released in the soft dependency code after the code has attached
1858 * an appropriate ordering dependency to the buffer which ensures that
1859 * the buffer is written before the new name is written in the parent.
1861 if (DOINGASYNC(dvp))
1863 else if (!DOINGSOFTDEP(dvp) && ((error = bwrite(bp))))
1865 ufs_makedirentry(ip, cnp, &newdir);
1866 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1874 DIP_SET(dp, i_nlink, dp->i_nlink);
1875 dp->i_flag |= IN_CHANGE;
1876 if (DOINGSOFTDEP(dvp))
1877 softdep_change_linkcnt(dp);
1879 * No need to do an explicit VOP_TRUNCATE here, vrele will
1880 * do this for us because we set the link count to 0.
1884 DIP_SET(ip, i_nlink, 0);
1885 ip->i_flag |= IN_CHANGE;
1886 if (DOINGSOFTDEP(tvp))
1887 softdep_change_linkcnt(ip);
1895 * Rmdir system call.
1899 struct vop_rmdir_args /* {
1900 struct vnode *a_dvp;
1902 struct componentname *a_cnp;
1905 struct vnode *vp = ap->a_vp;
1906 struct vnode *dvp = ap->a_dvp;
1907 struct componentname *cnp = ap->a_cnp;
1908 struct inode *ip, *dp;
1915 * Do not remove a directory that is in the process of being renamed.
1916 * Verify the directory is empty (and valid). Rmdir ".." will not be
1917 * valid since ".." will contain a reference to the current directory
1918 * and thus be non-empty. Do not allow the removal of mounted on
1919 * directories (this can happen when an NFS exported filesystem
1920 * tries to remove a locally mounted on directory).
1923 if ((ip->i_flag & IN_RENAME) || ip->i_effnlink < 2) {
1927 if (!ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1931 if ((dp->i_flags & APPEND)
1932 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1936 if (vp->v_mountedhere != 0) {
1941 ufs_gjournal_orphan(vp);
1944 * Delete reference to directory before purging
1945 * inode. If we crash in between, the directory
1946 * will be reattached to lost+found,
1950 if (DOINGSOFTDEP(vp)) {
1951 softdep_change_linkcnt(dp);
1952 softdep_change_linkcnt(ip);
1954 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1958 if (DOINGSOFTDEP(vp)) {
1959 softdep_change_linkcnt(dp);
1960 softdep_change_linkcnt(ip);
1966 * Truncate inode. The only stuff left in the directory is "." and
1967 * "..". The "." reference is inconsequential since we are quashing
1968 * it. The soft dependency code will arrange to do these operations
1969 * after the parent directory entry has been deleted on disk, so
1970 * when running with that code we avoid doing them now.
1972 if (!DOINGSOFTDEP(vp)) {
1974 DIP_SET(dp, i_nlink, dp->i_nlink);
1975 dp->i_flag |= IN_CHANGE;
1977 DIP_SET(ip, i_nlink, ip->i_nlink);
1978 ip->i_flag |= IN_CHANGE;
1980 if (!DOINGASYNC(vp))
1982 error = UFS_TRUNCATE(vp, (off_t)0, ioflag, cnp->cn_cred,
1987 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1988 if (ip->i_dirhash != NULL)
1989 ufsdirhash_free(ip);
1996 * symlink -- make a symbolic link
2000 struct vop_symlink_args /* {
2001 struct vnode *a_dvp;
2002 struct vnode **a_vpp;
2003 struct componentname *a_cnp;
2004 struct vattr *a_vap;
2008 struct vnode *vp, **vpp = ap->a_vpp;
2012 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
2017 len = strlen(ap->a_target);
2018 if (len < vp->v_mount->mnt_maxsymlinklen) {
2020 bcopy(ap->a_target, SHORTLINK(ip), len);
2022 DIP_SET(ip, i_size, len);
2023 ip->i_flag |= IN_CHANGE | IN_UPDATE;
2025 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
2026 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
2027 ap->a_cnp->cn_cred, NOCRED, NULL, NULL);
2034 * Vnode op for reading directories.
2036 * The routine below assumes that the on-disk format of a directory
2037 * is the same as that defined by <sys/dirent.h>. If the on-disk
2038 * format changes, then it will be necessary to do a conversion
2039 * from the on-disk format that read returns to the format defined
2040 * by <sys/dirent.h>.
2044 struct vop_readdir_args /* {
2047 struct ucred *a_cred;
2053 struct uio *uio = ap->a_uio;
2058 if (ap->a_ncookies != NULL)
2060 * Ensure that the block is aligned. The caller can use
2061 * the cookies to determine where in the block to start.
2063 uio->uio_offset &= ~(DIRBLKSIZ - 1);
2064 off = uio->uio_offset;
2065 count = uio->uio_resid;
2066 /* Make sure we don't return partial entries. */
2067 if (count <= ((uio->uio_offset + count) & (DIRBLKSIZ -1)))
2069 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
2070 lost = uio->uio_resid - count;
2071 uio->uio_resid = count;
2072 uio->uio_iov->iov_len = count;
2073 # if (BYTE_ORDER == LITTLE_ENDIAN)
2074 if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) {
2075 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
2077 struct dirent *dp, *edp;
2085 auio.uio_iov = &aiov;
2086 auio.uio_iovcnt = 1;
2087 auio.uio_segflg = UIO_SYSSPACE;
2088 aiov.iov_len = count;
2089 dirbuf = malloc(count, M_TEMP, M_WAITOK);
2090 aiov.iov_base = dirbuf;
2091 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
2093 readcnt = count - auio.uio_resid;
2094 edp = (struct dirent *)&dirbuf[readcnt];
2095 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
2097 dp->d_namlen = dp->d_type;
2099 if (dp->d_reclen > 0) {
2100 dp = (struct dirent *)
2101 ((char *)dp + dp->d_reclen);
2108 error = uiomove(dirbuf, readcnt, uio);
2110 free(dirbuf, M_TEMP);
2113 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
2115 if (!error && ap->a_ncookies != NULL) {
2116 struct dirent* dpStart;
2117 struct dirent* dpEnd;
2123 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2124 panic("ufs_readdir: unexpected uio from NFS server");
2125 dpStart = (struct dirent *)
2126 ((char *)uio->uio_iov->iov_base - (uio->uio_offset - off));
2127 dpEnd = (struct dirent *) uio->uio_iov->iov_base;
2128 for (dp = dpStart, ncookies = 0;
2130 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen))
2132 cookies = malloc(ncookies * sizeof(u_long), M_TEMP,
2134 for (dp = dpStart, cookiep = cookies;
2136 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
2137 off += dp->d_reclen;
2138 *cookiep++ = (u_long) off;
2140 *ap->a_ncookies = ncookies;
2141 *ap->a_cookies = cookies;
2143 uio->uio_resid += lost;
2145 *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
2150 * Return target name of a symbolic link
2154 struct vop_readlink_args /* {
2157 struct ucred *a_cred;
2160 struct vnode *vp = ap->a_vp;
2161 struct inode *ip = VTOI(vp);
2165 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
2166 DIP(ip, i_blocks) == 0) { /* XXX - for old fastlink support */
2167 return (uiomove(SHORTLINK(ip), isize, ap->a_uio));
2169 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
2173 * Calculate the logical to physical mapping if not done already,
2174 * then call the device strategy routine.
2176 * In order to be able to swap to a file, the ufs_bmaparray() operation may not
2177 * deadlock on memory. See ufs_bmap() for details.
2181 struct vop_strategy_args /* {
2186 struct buf *bp = ap->a_bp;
2187 struct vnode *vp = ap->a_vp;
2194 if (bp->b_blkno == bp->b_lblkno) {
2195 error = ufs_bmaparray(vp, bp->b_lblkno, &blkno, bp, NULL, NULL);
2196 bp->b_blkno = blkno;
2198 bp->b_error = error;
2199 bp->b_ioflags |= BIO_ERROR;
2203 if ((long)bp->b_blkno == -1)
2206 if ((long)bp->b_blkno == -1) {
2210 bp->b_iooffset = dbtob(bp->b_blkno);
2211 bo = ip->i_umbufobj;
2212 BO_STRATEGY(bo, bp);
2217 * Print out the contents of an inode.
2221 struct vop_print_args /* {
2225 struct vnode *vp = ap->a_vp;
2226 struct inode *ip = VTOI(vp);
2228 printf("\tino %lu, on dev %s", (u_long)ip->i_number,
2229 devtoname(ip->i_dev));
2230 if (vp->v_type == VFIFO)
2237 * Close wrapper for fifos.
2239 * Update the times on the inode then do device close.
2243 struct vop_close_args /* {
2246 struct ucred *a_cred;
2247 struct thread *a_td;
2250 struct vnode *vp = ap->a_vp;
2254 usecount = vp->v_usecount;
2256 ufs_itimes_locked(vp);
2258 return (fifo_specops.vop_close(ap));
2262 * Kqfilter wrapper for fifos.
2264 * Fall through to ufs kqfilter routines if needed
2267 ufsfifo_kqfilter(ap)
2268 struct vop_kqfilter_args *ap;
2272 error = fifo_specops.vop_kqfilter(ap);
2274 error = vfs_kqfilter(ap);
2279 * Return POSIX pathconf information applicable to fifos.
2282 ufsfifo_pathconf(ap)
2283 struct vop_pathconf_args /* {
2290 switch (ap->a_name) {
2291 case _PC_ACL_EXTENDED:
2293 case _PC_ACL_PATH_MAX:
2294 case _PC_MAC_PRESENT:
2295 return (ufs_pathconf(ap));
2297 return (fifo_specops.vop_pathconf(ap));
2303 * Return POSIX pathconf information applicable to ufs filesystems.
2307 struct vop_pathconf_args /* {
2316 switch (ap->a_name) {
2318 *ap->a_retval = LINK_MAX;
2321 *ap->a_retval = NAME_MAX;
2324 *ap->a_retval = PATH_MAX;
2327 *ap->a_retval = PIPE_BUF;
2329 case _PC_CHOWN_RESTRICTED:
2335 case _PC_ACL_EXTENDED:
2337 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
2348 if (ap->a_vp->v_mount->mnt_flag & MNT_NFS4ACLS)
2357 case _PC_ACL_PATH_MAX:
2359 if (ap->a_vp->v_mount->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS))
2360 *ap->a_retval = ACL_MAX_ENTRIES;
2367 case _PC_MAC_PRESENT:
2369 if (ap->a_vp->v_mount->mnt_flag & MNT_MULTILABEL)
2378 /* _PC_ASYNC_IO should have been handled by upper layers. */
2379 KASSERT(0, ("_PC_ASYNC_IO should not get here"));
2388 case _PC_ALLOC_SIZE_MIN:
2389 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
2391 case _PC_FILESIZEBITS:
2394 case _PC_REC_INCR_XFER_SIZE:
2395 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2397 case _PC_REC_MAX_XFER_SIZE:
2398 *ap->a_retval = -1; /* means ``unlimited'' */
2400 case _PC_REC_MIN_XFER_SIZE:
2401 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2403 case _PC_REC_XFER_ALIGN:
2404 *ap->a_retval = PAGE_SIZE;
2406 case _PC_SYMLINK_MAX:
2407 *ap->a_retval = MAXPATHLEN;
2418 * Initialize the vnode associated with a new inode, handle aliased
2422 ufs_vinit(mntp, fifoops, vpp)
2424 struct vop_vector *fifoops;
2432 vp->v_type = IFTOVT(ip->i_mode);
2433 if (vp->v_type == VFIFO)
2435 ASSERT_VOP_LOCKED(vp, "ufs_vinit");
2436 if (ip->i_number == ROOTINO)
2437 vp->v_vflag |= VV_ROOT;
2443 * Allocate a new inode.
2444 * Vnode dvp must be locked.
2447 ufs_makeinode(mode, dvp, vpp, cnp)
2451 struct componentname *cnp;
2453 struct inode *ip, *pdir;
2454 struct direct newdir;
2460 if ((cnp->cn_flags & HASBUF) == 0)
2461 panic("ufs_makeinode: no name");
2464 if ((mode & IFMT) == 0)
2467 error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2471 ip->i_gid = pdir->i_gid;
2472 DIP_SET(ip, i_gid, pdir->i_gid);
2476 struct ucred ucred, *ucp;
2481 * If we are not the owner of the directory,
2482 * and we are hacking owners here, (only do this where told to)
2483 * and we are not giving it TO root, (would subvert quotas)
2484 * then go ahead and give it to the other user.
2485 * Note that this drops off the execute bits for security.
2487 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2488 (pdir->i_mode & ISUID) &&
2489 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2490 ip->i_uid = pdir->i_uid;
2491 DIP_SET(ip, i_uid, ip->i_uid);
2495 * Make sure the correct user gets charged
2497 * Quickly knock up a dummy credential for the victim.
2498 * XXX This seems to never be accessed out of our
2499 * context so a stack variable is ok.
2501 refcount_init(&ucred.cr_ref, 1);
2502 ucred.cr_uid = ip->i_uid;
2503 ucred.cr_ngroups = 1;
2504 ucred.cr_groups = &ucred_group;
2505 ucred.cr_groups[0] = pdir->i_gid;
2509 ip->i_uid = cnp->cn_cred->cr_uid;
2510 DIP_SET(ip, i_uid, ip->i_uid);
2514 if ((error = getinoquota(ip)) ||
2515 (error = chkiq(ip, 1, ucp, 0))) {
2516 UFS_VFREE(tvp, ip->i_number, mode);
2522 #else /* !SUIDDIR */
2523 ip->i_uid = cnp->cn_cred->cr_uid;
2524 DIP_SET(ip, i_uid, ip->i_uid);
2526 if ((error = getinoquota(ip)) ||
2527 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2528 UFS_VFREE(tvp, ip->i_number, mode);
2533 #endif /* !SUIDDIR */
2534 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2536 DIP_SET(ip, i_mode, mode);
2537 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2540 DIP_SET(ip, i_nlink, 1);
2541 if (DOINGSOFTDEP(tvp))
2542 softdep_change_linkcnt(ip);
2543 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2544 priv_check_cred(cnp->cn_cred, PRIV_VFS_SETGID, 0)) {
2545 ip->i_mode &= ~ISGID;
2546 DIP_SET(ip, i_mode, ip->i_mode);
2549 if (cnp->cn_flags & ISWHITEOUT) {
2550 ip->i_flags |= UF_OPAQUE;
2551 DIP_SET(ip, i_flags, ip->i_flags);
2555 * Make sure inode goes to disk before directory entry.
2557 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2561 if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
2562 error = mac_vnode_create_extattr(cnp->cn_cred, dvp->v_mount,
2569 if (dvp->v_mount->mnt_flag & MNT_ACLS) {
2570 error = ufs_do_posix1e_acl_inheritance_file(dvp, tvp, mode,
2571 cnp->cn_cred, cnp->cn_thread);
2574 } else if (dvp->v_mount->mnt_flag & MNT_NFS4ACLS) {
2575 error = ufs_do_nfs4_acl_inheritance(dvp, tvp, mode,
2576 cnp->cn_cred, cnp->cn_thread);
2580 #endif /* !UFS_ACL */
2581 ufs_makedirentry(ip, cnp, &newdir);
2582 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2590 * Write error occurred trying to update the inode
2591 * or the directory so must deallocate the inode.
2595 DIP_SET(ip, i_nlink, 0);
2596 ip->i_flag |= IN_CHANGE;
2597 if (DOINGSOFTDEP(tvp))
2598 softdep_change_linkcnt(ip);
2603 /* Global vfs data structures for ufs. */
2604 struct vop_vector ufs_vnodeops = {
2605 .vop_default = &default_vnodeops,
2606 .vop_fsync = VOP_PANIC,
2607 .vop_read = VOP_PANIC,
2608 .vop_reallocblks = VOP_PANIC,
2609 .vop_write = VOP_PANIC,
2610 .vop_accessx = ufs_accessx,
2611 .vop_bmap = ufs_bmap,
2612 .vop_cachedlookup = ufs_lookup,
2613 .vop_close = ufs_close,
2614 .vop_create = ufs_create,
2615 .vop_getattr = ufs_getattr,
2616 .vop_inactive = ufs_inactive,
2617 .vop_link = ufs_link,
2618 .vop_lookup = vfs_cache_lookup,
2619 .vop_markatime = ufs_markatime,
2620 .vop_mkdir = ufs_mkdir,
2621 .vop_mknod = ufs_mknod,
2622 .vop_open = ufs_open,
2623 .vop_pathconf = ufs_pathconf,
2624 .vop_poll = vop_stdpoll,
2625 .vop_print = ufs_print,
2626 .vop_readdir = ufs_readdir,
2627 .vop_readlink = ufs_readlink,
2628 .vop_reclaim = ufs_reclaim,
2629 .vop_remove = ufs_remove,
2630 .vop_rename = ufs_rename,
2631 .vop_rmdir = ufs_rmdir,
2632 .vop_setattr = ufs_setattr,
2634 .vop_setlabel = vop_stdsetlabel_ea,
2636 .vop_strategy = ufs_strategy,
2637 .vop_symlink = ufs_symlink,
2638 .vop_whiteout = ufs_whiteout,
2640 .vop_getextattr = ufs_getextattr,
2641 .vop_deleteextattr = ufs_deleteextattr,
2642 .vop_setextattr = ufs_setextattr,
2645 .vop_getacl = ufs_getacl,
2646 .vop_setacl = ufs_setacl,
2647 .vop_aclcheck = ufs_aclcheck,
2651 struct vop_vector ufs_fifoops = {
2652 .vop_default = &fifo_specops,
2653 .vop_fsync = VOP_PANIC,
2654 .vop_accessx = ufs_accessx,
2655 .vop_close = ufsfifo_close,
2656 .vop_getattr = ufs_getattr,
2657 .vop_inactive = ufs_inactive,
2658 .vop_kqfilter = ufsfifo_kqfilter,
2659 .vop_markatime = ufs_markatime,
2660 .vop_pathconf = ufsfifo_pathconf,
2661 .vop_print = ufs_print,
2662 .vop_read = VOP_PANIC,
2663 .vop_reclaim = ufs_reclaim,
2664 .vop_setattr = ufs_setattr,
2666 .vop_setlabel = vop_stdsetlabel_ea,
2668 .vop_write = VOP_PANIC,
2670 .vop_getextattr = ufs_getextattr,
2671 .vop_deleteextattr = ufs_deleteextattr,
2672 .vop_setextattr = ufs_setextattr,
2675 .vop_getacl = ufs_getacl,
2676 .vop_setacl = ufs_setacl,
2677 .vop_aclcheck = ufs_aclcheck,