2 * Copyright (c) 1982, 1986, 1989, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 #include "opt_quota.h"
42 #include "opt_suiddir.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #include <sys/namei.h>
50 #include <sys/kernel.h>
51 #include <sys/fcntl.h>
55 #include <sys/mount.h>
57 #include <sys/refcount.h>
58 #include <sys/unistd.h>
59 #include <sys/vnode.h>
60 #include <sys/dirent.h>
61 #include <sys/lockf.h>
66 #include <machine/mutex.h>
68 #include <security/mac/mac_framework.h>
70 #include <sys/file.h> /* XXX */
73 #include <vm/vm_extern.h>
75 #include <fs/fifofs/fifo.h>
77 #include <ufs/ufs/acl.h>
78 #include <ufs/ufs/extattr.h>
79 #include <ufs/ufs/quota.h>
80 #include <ufs/ufs/inode.h>
81 #include <ufs/ufs/dir.h>
82 #include <ufs/ufs/ufsmount.h>
83 #include <ufs/ufs/ufs_extern.h>
85 #include <ufs/ufs/dirhash.h>
88 #include <ufs/ufs/gjournal.h>
91 #include <ufs/ffs/ffs_extern.h>
93 static vop_access_t ufs_access;
94 static int ufs_chmod(struct vnode *, int, struct ucred *, struct thread *);
95 static int ufs_chown(struct vnode *, uid_t, gid_t, struct ucred *, struct thread *);
96 static vop_close_t ufs_close;
97 static vop_create_t ufs_create;
98 static vop_getattr_t ufs_getattr;
99 static vop_link_t ufs_link;
100 static int ufs_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
101 static vop_mkdir_t ufs_mkdir;
102 static vop_mknod_t ufs_mknod;
103 static vop_open_t ufs_open;
104 static vop_pathconf_t ufs_pathconf;
105 static vop_print_t ufs_print;
106 static vop_readlink_t ufs_readlink;
107 static vop_remove_t ufs_remove;
108 static vop_rename_t ufs_rename;
109 static vop_rmdir_t ufs_rmdir;
110 static vop_setattr_t ufs_setattr;
111 static vop_strategy_t ufs_strategy;
112 static vop_symlink_t ufs_symlink;
113 static vop_whiteout_t ufs_whiteout;
114 static vop_close_t ufsfifo_close;
115 static vop_kqfilter_t ufsfifo_kqfilter;
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);
161 if (ip->i_flag & IN_CHANGE) {
162 DIP_SET(ip, i_ctime, ts.tv_sec);
163 DIP_SET(ip, i_ctimensec, ts.tv_nsec);
167 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
171 ufs_itimes(struct vnode *vp)
175 ufs_itimes_locked(vp);
180 * Create a regular file
184 struct vop_create_args /* {
186 struct vnode **a_vpp;
187 struct componentname *a_cnp;
194 ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
195 ap->a_dvp, ap->a_vpp, ap->a_cnp);
207 struct vop_mknod_args /* {
209 struct vnode **a_vpp;
210 struct componentname *a_cnp;
214 struct vattr *vap = ap->a_vap;
215 struct vnode **vpp = ap->a_vpp;
220 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
221 ap->a_dvp, vpp, ap->a_cnp);
225 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
226 if (vap->va_rdev != VNOVAL) {
228 * Want to be able to use this to make badblock
229 * inodes, so don't truncate the dev number.
231 DIP_SET(ip, i_rdev, vap->va_rdev);
234 * Remove inode, then reload it through VFS_VGET so it is
235 * checked to see if it is an alias of an existing entry in
236 * the inode cache. XXX I don't believe this is necessary now.
238 (*vpp)->v_type = VNON;
239 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
242 error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
255 ufs_open(struct vop_open_args *ap)
257 struct vnode *vp = ap->a_vp;
260 if (vp->v_type == VCHR || vp->v_type == VBLK)
265 * Files marked append-only must be opened for appending.
267 if ((ip->i_flags & APPEND) &&
268 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
270 vnode_create_vobject(vp, DIP(ip, i_size), ap->a_td);
277 * Update the times on the inode.
282 struct vop_close_args /* {
285 struct ucred *a_cred;
289 struct vnode *vp = ap->a_vp;
293 usecount = vp->v_usecount;
295 ufs_itimes_locked(vp);
302 struct vop_access_args /* {
305 struct ucred *a_cred;
309 struct vnode *vp = ap->a_vp;
310 struct inode *ip = VTOI(vp);
311 mode_t mode = ap->a_mode;
321 * Disallow write attempts on read-only filesystems;
322 * unless the file is a socket, fifo, or a block or
323 * character device resident on the filesystem.
326 switch (vp->v_type) {
330 if (vp->v_mount->mnt_flag & MNT_RDONLY)
334 * Inode is accounted in the quotas only if struct
335 * dquot is attached to it. VOP_ACCESS() is called
336 * from vn_open_cred() and provides a convenient
337 * point to call getinoquota().
339 if (VOP_ISLOCKED(vp, ap->a_td) != LK_EXCLUSIVE) {
342 * Upgrade vnode lock, since getinoquota()
343 * requires exclusive lock to modify inode.
347 vn_lock(vp, LK_UPGRADE | LK_RETRY, ap->a_td);
349 if (vp->v_iflag & VI_DOOMED) {
357 error = getinoquota(ip);
360 vn_lock(vp, LK_DOWNGRADE | LK_RETRY, ap->a_td);
370 /* If immutable bit set, nobody gets to write it. */
371 if ((mode & VWRITE) && (ip->i_flags & (IMMUTABLE | SF_SNAPSHOT)))
375 if ((vp->v_mount->mnt_flag & MNT_ACLS) != 0) {
376 acl = uma_zalloc(acl_zone, M_WAITOK);
377 error = VOP_GETACL(vp, ACL_TYPE_ACCESS, acl, ap->a_cred,
381 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid,
382 ip->i_gid, ap->a_mode, ap->a_cred, NULL);
385 error = vaccess_acl_posix1e(vp->v_type, ip->i_uid,
386 ip->i_gid, acl, ap->a_mode, ap->a_cred, NULL);
390 "ufs_access(): Error retrieving ACL on object (%d).\n",
393 * XXX: Fall back until debugged. Should
394 * eventually possibly log an error, and return
397 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid,
398 ip->i_gid, ap->a_mode, ap->a_cred, NULL);
400 uma_zfree(acl_zone, acl);
402 #endif /* !UFS_ACL */
403 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
404 ap->a_mode, ap->a_cred, NULL);
411 struct vop_getattr_args /* {
414 struct ucred *a_cred;
418 struct vnode *vp = ap->a_vp;
419 struct inode *ip = VTOI(vp);
420 struct vattr *vap = ap->a_vap;
423 ufs_itimes_locked(vp);
424 if (ip->i_ump->um_fstype == UFS1) {
425 vap->va_atime.tv_sec = ip->i_din1->di_atime;
426 vap->va_atime.tv_nsec = ip->i_din1->di_atimensec;
428 vap->va_atime.tv_sec = ip->i_din2->di_atime;
429 vap->va_atime.tv_nsec = ip->i_din2->di_atimensec;
433 * Copy from inode table
435 vap->va_fsid = dev2udev(ip->i_dev);
436 vap->va_fileid = ip->i_number;
437 vap->va_mode = ip->i_mode & ~IFMT;
438 vap->va_nlink = ip->i_effnlink;
439 vap->va_uid = ip->i_uid;
440 vap->va_gid = ip->i_gid;
441 if (ip->i_ump->um_fstype == UFS1) {
442 vap->va_rdev = ip->i_din1->di_rdev;
443 vap->va_size = ip->i_din1->di_size;
444 vap->va_mtime.tv_sec = ip->i_din1->di_mtime;
445 vap->va_mtime.tv_nsec = ip->i_din1->di_mtimensec;
446 vap->va_ctime.tv_sec = ip->i_din1->di_ctime;
447 vap->va_ctime.tv_nsec = ip->i_din1->di_ctimensec;
448 vap->va_bytes = dbtob((u_quad_t)ip->i_din1->di_blocks);
450 vap->va_rdev = ip->i_din2->di_rdev;
451 vap->va_size = ip->i_din2->di_size;
452 vap->va_mtime.tv_sec = ip->i_din2->di_mtime;
453 vap->va_mtime.tv_nsec = ip->i_din2->di_mtimensec;
454 vap->va_ctime.tv_sec = ip->i_din2->di_ctime;
455 vap->va_ctime.tv_nsec = ip->i_din2->di_ctimensec;
456 vap->va_birthtime.tv_sec = ip->i_din2->di_birthtime;
457 vap->va_birthtime.tv_nsec = ip->i_din2->di_birthnsec;
458 vap->va_bytes = dbtob((u_quad_t)ip->i_din2->di_blocks);
460 vap->va_flags = ip->i_flags;
461 vap->va_gen = ip->i_gen;
462 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
463 vap->va_type = IFTOVT(ip->i_mode);
464 vap->va_filerev = ip->i_modrev;
469 * Set attribute vnode op. called from several syscalls
473 struct vop_setattr_args /* {
476 struct ucred *a_cred;
480 struct vattr *vap = ap->a_vap;
481 struct vnode *vp = ap->a_vp;
482 struct inode *ip = VTOI(vp);
483 struct ucred *cred = ap->a_cred;
484 struct thread *td = ap->a_td;
488 * Check for unsettable attributes.
490 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
491 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
492 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
493 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
497 * Mark for update the file's access time for vfs_mark_atime().
498 * We are doing this here to avoid some of the checks done
499 * below -- this operation is done by request of the kernel and
500 * should bypass some security checks. Things like read-only
501 * checks get handled by other levels (e.g., ffs_update()).
503 if (vap->va_vaflags & VA_MARK_ATIME) {
504 ip->i_flag |= IN_ACCESS;
507 if (vap->va_flags != VNOVAL) {
508 if (vp->v_mount->mnt_flag & MNT_RDONLY)
511 * Callers may only modify the file flags on objects they
512 * have VADMIN rights for.
514 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
517 * Unprivileged processes are not permitted to unset system
518 * flags, or modify flags if any system flags are set.
519 * Privileged non-jail processes may not modify system flags
520 * if securelevel > 0 and any existing system flags are set.
521 * Privileged jail processes behave like privileged non-jail
522 * processes if the security.jail.chflags_allowed sysctl is
523 * is non-zero; otherwise, they behave like unprivileged
526 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
528 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
529 error = securelevel_gt(cred, 0);
533 /* Snapshot flag cannot be set or cleared */
534 if (((vap->va_flags & SF_SNAPSHOT) != 0 &&
535 (ip->i_flags & SF_SNAPSHOT) == 0) ||
536 ((vap->va_flags & SF_SNAPSHOT) == 0 &&
537 (ip->i_flags & SF_SNAPSHOT) != 0))
539 ip->i_flags = vap->va_flags;
540 DIP_SET(ip, i_flags, vap->va_flags);
543 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
544 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
546 ip->i_flags &= SF_SETTABLE;
547 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
548 DIP_SET(ip, i_flags, ip->i_flags);
550 ip->i_flag |= IN_CHANGE;
551 if (vap->va_flags & (IMMUTABLE | APPEND))
554 if (ip->i_flags & (IMMUTABLE | APPEND))
557 * Go through the fields and update iff not VNOVAL.
559 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
560 if (vp->v_mount->mnt_flag & MNT_RDONLY)
562 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred,
566 if (vap->va_size != VNOVAL) {
568 * XXX most of the following special cases should be in
569 * callers instead of in N filesystems. The VDIR check
572 switch (vp->v_type) {
578 * Truncation should have an effect in these cases.
579 * Disallow it if the filesystem is read-only or
580 * the file is being snapshotted.
582 if (vp->v_mount->mnt_flag & MNT_RDONLY)
584 if ((ip->i_flags & SF_SNAPSHOT) != 0)
589 * According to POSIX, the result is unspecified
590 * for file types other than regular files,
591 * directories and shared memory objects. We
592 * don't support shared memory objects in the file
593 * system, and have dubious support for truncating
594 * symlinks. Just ignore the request in other cases.
598 if ((error = UFS_TRUNCATE(vp, vap->va_size, IO_NORMAL,
602 if (vap->va_atime.tv_sec != VNOVAL ||
603 vap->va_mtime.tv_sec != VNOVAL ||
604 vap->va_birthtime.tv_sec != VNOVAL) {
605 if (vp->v_mount->mnt_flag & MNT_RDONLY)
607 if ((ip->i_flags & SF_SNAPSHOT) != 0)
611 * If times is NULL, ... The caller must be the owner of
612 * the file, have permission to write the file, or be the
614 * If times is non-NULL, ... The caller must be the owner of
615 * the file or be the super-user.
617 * Possibly for historical reasons, try to use VADMIN in
618 * preference to VWRITE for a NULL timestamp. This means we
619 * will return EACCES in preference to EPERM if neither
622 if (vap->va_vaflags & VA_UTIMES_NULL) {
623 error = VOP_ACCESS(vp, VADMIN, cred, td);
625 error = VOP_ACCESS(vp, VWRITE, cred, td);
627 error = VOP_ACCESS(vp, VADMIN, cred, td);
630 if (vap->va_atime.tv_sec != VNOVAL)
631 ip->i_flag |= IN_ACCESS;
632 if (vap->va_mtime.tv_sec != VNOVAL)
633 ip->i_flag |= IN_CHANGE | IN_UPDATE;
634 if (vap->va_birthtime.tv_sec != VNOVAL &&
635 ip->i_ump->um_fstype == UFS2)
636 ip->i_flag |= IN_MODIFIED;
638 if (vap->va_atime.tv_sec != VNOVAL) {
639 DIP_SET(ip, i_atime, vap->va_atime.tv_sec);
640 DIP_SET(ip, i_atimensec, vap->va_atime.tv_nsec);
642 if (vap->va_mtime.tv_sec != VNOVAL) {
643 DIP_SET(ip, i_mtime, vap->va_mtime.tv_sec);
644 DIP_SET(ip, i_mtimensec, vap->va_mtime.tv_nsec);
646 if (vap->va_birthtime.tv_sec != VNOVAL &&
647 ip->i_ump->um_fstype == UFS2) {
648 ip->i_din2->di_birthtime = vap->va_birthtime.tv_sec;
649 ip->i_din2->di_birthnsec = vap->va_birthtime.tv_nsec;
651 error = UFS_UPDATE(vp, 0);
656 if (vap->va_mode != (mode_t)VNOVAL) {
657 if (vp->v_mount->mnt_flag & MNT_RDONLY)
659 if ((ip->i_flags & SF_SNAPSHOT) != 0 && (vap->va_mode &
660 (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP | S_IXOTH | S_IWOTH)))
662 error = ufs_chmod(vp, (int)vap->va_mode, cred, td);
668 * Change the mode on a file.
669 * Inode must be locked before calling.
672 ufs_chmod(vp, mode, cred, td)
678 struct inode *ip = VTOI(vp);
682 * To modify the permissions on a file, must possess VADMIN
685 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
688 * Privileged processes may set the sticky bit on non-directories,
689 * as well as set the setgid bit on a file with a group that the
690 * process is not a member of. Both of these are allowed in
693 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
694 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0))
697 if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
698 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
702 ip->i_mode &= ~ALLPERMS;
703 ip->i_mode |= (mode & ALLPERMS);
704 DIP_SET(ip, i_mode, ip->i_mode);
705 ip->i_flag |= IN_CHANGE;
710 * Perform chown operation on inode ip;
711 * inode must be locked prior to call.
714 ufs_chown(vp, uid, gid, cred, td)
721 struct inode *ip = VTOI(vp);
730 if (uid == (uid_t)VNOVAL)
732 if (gid == (gid_t)VNOVAL)
735 * To modify the ownership of a file, must possess VADMIN for that
738 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
741 * To change the owner of a file, or change the group of a file to a
742 * group of which we are not a member, the caller must have
745 if ((uid != ip->i_uid ||
746 (gid != ip->i_gid && !groupmember(gid, cred))) &&
747 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
752 if ((error = getinoquota(ip)) != 0)
755 dqrele(vp, ip->i_dquot[USRQUOTA]);
756 ip->i_dquot[USRQUOTA] = NODQUOT;
759 dqrele(vp, ip->i_dquot[GRPQUOTA]);
760 ip->i_dquot[GRPQUOTA] = NODQUOT;
762 change = DIP(ip, i_blocks);
763 (void) chkdq(ip, -change, cred, CHOWN);
764 (void) chkiq(ip, -1, cred, CHOWN);
765 for (i = 0; i < MAXQUOTAS; i++) {
766 dqrele(vp, ip->i_dquot[i]);
767 ip->i_dquot[i] = NODQUOT;
771 DIP_SET(ip, i_gid, gid);
773 DIP_SET(ip, i_uid, uid);
775 if ((error = getinoquota(ip)) == 0) {
777 dqrele(vp, ip->i_dquot[USRQUOTA]);
778 ip->i_dquot[USRQUOTA] = NODQUOT;
781 dqrele(vp, ip->i_dquot[GRPQUOTA]);
782 ip->i_dquot[GRPQUOTA] = NODQUOT;
784 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
785 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
788 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
790 for (i = 0; i < MAXQUOTAS; i++) {
791 dqrele(vp, ip->i_dquot[i]);
792 ip->i_dquot[i] = NODQUOT;
796 DIP_SET(ip, i_gid, ogid);
798 DIP_SET(ip, i_uid, ouid);
799 if (getinoquota(ip) == 0) {
801 dqrele(vp, ip->i_dquot[USRQUOTA]);
802 ip->i_dquot[USRQUOTA] = NODQUOT;
805 dqrele(vp, ip->i_dquot[GRPQUOTA]);
806 ip->i_dquot[GRPQUOTA] = NODQUOT;
808 (void) chkdq(ip, change, cred, FORCE|CHOWN);
809 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
810 (void) getinoquota(ip);
815 panic("ufs_chown: lost quota");
817 ip->i_flag |= IN_CHANGE;
818 if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
819 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0)) {
820 ip->i_mode &= ~(ISUID | ISGID);
821 DIP_SET(ip, i_mode, ip->i_mode);
829 struct vop_remove_args /* {
832 struct componentname *a_cnp;
836 struct vnode *vp = ap->a_vp;
837 struct vnode *dvp = ap->a_dvp;
843 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
844 (VTOI(dvp)->i_flags & APPEND)) {
849 ufs_gjournal_orphan(vp);
851 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
852 if (ip->i_nlink <= 0)
853 vp->v_vflag |= VV_NOSYNC;
854 if ((ip->i_flags & SF_SNAPSHOT) != 0) {
856 * Avoid deadlock where another thread is trying to
857 * update the inodeblock for dvp and is waiting on
858 * snaplk. Temporary unlock the vnode lock for the
859 * unlinked file and sync the directory. This should
860 * allow vput() of the directory to not block later on
861 * while holding the snapshot vnode locked, assuming
862 * that the directory hasn't been unlinked too.
864 VOP_UNLOCK(vp, 0, td);
865 (void) VOP_FSYNC(dvp, MNT_WAIT, td);
866 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
877 struct vop_link_args /* {
878 struct vnode *a_tdvp;
880 struct componentname *a_cnp;
883 struct vnode *vp = ap->a_vp;
884 struct vnode *tdvp = ap->a_tdvp;
885 struct componentname *cnp = ap->a_cnp;
887 struct direct newdir;
891 if ((cnp->cn_flags & HASBUF) == 0)
892 panic("ufs_link: no name");
894 if (tdvp->v_mount != vp->v_mount) {
899 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
903 if (ip->i_flags & (IMMUTABLE | APPEND)) {
909 DIP_SET(ip, i_nlink, ip->i_nlink);
910 ip->i_flag |= IN_CHANGE;
911 if (DOINGSOFTDEP(vp))
912 softdep_change_linkcnt(ip);
913 error = UFS_UPDATE(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
915 ufs_makedirentry(ip, cnp, &newdir);
916 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
922 DIP_SET(ip, i_nlink, ip->i_nlink);
923 ip->i_flag |= IN_CHANGE;
924 if (DOINGSOFTDEP(vp))
925 softdep_change_linkcnt(ip);
932 * whiteout vnode call
936 struct vop_whiteout_args /* {
938 struct componentname *a_cnp;
942 struct vnode *dvp = ap->a_dvp;
943 struct componentname *cnp = ap->a_cnp;
944 struct direct newdir;
947 switch (ap->a_flags) {
949 /* 4.4 format directories support whiteout operations */
950 if (dvp->v_mount->mnt_maxsymlinklen > 0)
955 /* create a new directory whiteout */
957 if ((cnp->cn_flags & SAVENAME) == 0)
958 panic("ufs_whiteout: missing name");
959 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
960 panic("ufs_whiteout: old format filesystem");
964 newdir.d_namlen = cnp->cn_namelen;
965 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
966 newdir.d_type = DT_WHT;
967 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
971 /* remove an existing directory whiteout */
973 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
974 panic("ufs_whiteout: old format filesystem");
977 cnp->cn_flags &= ~DOWHITEOUT;
978 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
981 panic("ufs_whiteout: unknown op");
987 * Rename system call.
988 * rename("foo", "bar");
991 * link("foo", "bar");
993 * but ``atomically''. Can't do full commit without saving state in the
994 * inode on disk which isn't feasible at this time. Best we can do is
995 * always guarantee the target exists.
997 * Basic algorithm is:
999 * 1) Bump link count on source while we're linking it to the
1000 * target. This also ensure the inode won't be deleted out
1001 * from underneath us while we work (it may be truncated by
1002 * a concurrent `trunc' or `open' for creation).
1003 * 2) Link source to destination. If destination already exists,
1005 * 3) Unlink source reference to inode if still around. If a
1006 * directory was moved and the parent of the destination
1007 * is different from the source, patch the ".." entry in the
1012 struct vop_rename_args /* {
1013 struct vnode *a_fdvp;
1014 struct vnode *a_fvp;
1015 struct componentname *a_fcnp;
1016 struct vnode *a_tdvp;
1017 struct vnode *a_tvp;
1018 struct componentname *a_tcnp;
1021 struct vnode *tvp = ap->a_tvp;
1022 struct vnode *tdvp = ap->a_tdvp;
1023 struct vnode *fvp = ap->a_fvp;
1024 struct vnode *fdvp = ap->a_fdvp;
1025 struct componentname *tcnp = ap->a_tcnp;
1026 struct componentname *fcnp = ap->a_fcnp;
1027 struct thread *td = fcnp->cn_thread;
1028 struct inode *ip, *xp, *dp;
1029 struct direct newdir;
1030 int doingdirectory = 0, oldparent = 0, newparent = 0;
1031 int error = 0, ioflag;
1034 if ((tcnp->cn_flags & HASBUF) == 0 ||
1035 (fcnp->cn_flags & HASBUF) == 0)
1036 panic("ufs_rename: no name");
1039 * Check for cross-device rename.
1041 if ((fvp->v_mount != tdvp->v_mount) ||
1042 (tvp && (fvp->v_mount != tvp->v_mount))) {
1056 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1057 (VTOI(tdvp)->i_flags & APPEND))) {
1063 * Renaming a file to itself has no effect. The upper layers should
1064 * not call us in that case. Temporarily just warn if they do.
1067 printf("ufs_rename: fvp == tvp (can't happen)\n");
1072 if ((error = vn_lock(fvp, LK_EXCLUSIVE, td)) != 0)
1076 if (ip->i_nlink >= LINK_MAX) {
1077 VOP_UNLOCK(fvp, 0, td);
1081 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
1082 || (dp->i_flags & APPEND)) {
1083 VOP_UNLOCK(fvp, 0, td);
1087 if ((ip->i_mode & IFMT) == IFDIR) {
1089 * Avoid ".", "..", and aliases of "." for obvious reasons.
1091 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1092 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
1093 (ip->i_flag & IN_RENAME)) {
1094 VOP_UNLOCK(fvp, 0, td);
1098 ip->i_flag |= IN_RENAME;
1099 oldparent = dp->i_number;
1105 * When the target exists, both the directory
1106 * and target vnodes are returned locked.
1114 * 1) Bump link count while we're moving stuff
1115 * around. If we crash somewhere before
1116 * completing our work, the link count
1117 * may be wrong, but correctable.
1121 DIP_SET(ip, i_nlink, ip->i_nlink);
1122 ip->i_flag |= IN_CHANGE;
1123 if (DOINGSOFTDEP(fvp))
1124 softdep_change_linkcnt(ip);
1125 if ((error = UFS_UPDATE(fvp, !(DOINGSOFTDEP(fvp) |
1126 DOINGASYNC(fvp)))) != 0) {
1127 VOP_UNLOCK(fvp, 0, td);
1132 * If ".." must be changed (ie the directory gets a new
1133 * parent) then the source directory must not be in the
1134 * directory hierarchy above the target, as this would
1135 * orphan everything below the source directory. Also
1136 * the user must have write permission in the source so
1137 * as to be able to change "..". We must repeat the call
1138 * to namei, as the parent directory is unlocked by the
1139 * call to checkpath().
1141 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
1142 VOP_UNLOCK(fvp, 0, td);
1143 if (oldparent != dp->i_number)
1144 newparent = dp->i_number;
1145 if (doingdirectory && newparent) {
1146 if (error) /* write access check above */
1150 error = ufs_checkpath(ip, dp, tcnp->cn_cred);
1153 if ((tcnp->cn_flags & SAVESTART) == 0)
1154 panic("ufs_rename: lost to startdir");
1156 error = relookup(tdvp, &tvp, tcnp);
1166 * 2) If target doesn't exist, link the target
1167 * to the source and unlink the source.
1168 * Otherwise, rewrite the target directory
1169 * entry to reference the source inode and
1170 * expunge the original entry's existence.
1173 if (dp->i_dev != ip->i_dev)
1174 panic("ufs_rename: EXDEV");
1176 * Account for ".." in new directory.
1177 * When source and destination have the same
1178 * parent we don't fool with the link count.
1180 if (doingdirectory && newparent) {
1181 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1187 DIP_SET(dp, i_nlink, dp->i_nlink);
1188 dp->i_flag |= IN_CHANGE;
1189 if (DOINGSOFTDEP(tdvp))
1190 softdep_change_linkcnt(dp);
1191 error = UFS_UPDATE(tdvp, !(DOINGSOFTDEP(tdvp) |
1196 ufs_makedirentry(ip, tcnp, &newdir);
1197 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1199 if (doingdirectory && newparent) {
1202 DIP_SET(dp, i_nlink, dp->i_nlink);
1203 dp->i_flag |= IN_CHANGE;
1204 if (DOINGSOFTDEP(tdvp))
1205 softdep_change_linkcnt(dp);
1206 (void)UFS_UPDATE(tdvp, 1);
1212 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1213 panic("ufs_rename: EXDEV");
1215 * Short circuit rename(foo, foo).
1217 if (xp->i_number == ip->i_number)
1218 panic("ufs_rename: same file");
1220 * If the parent directory is "sticky", then the caller
1221 * must possess VADMIN for the parent directory, or the
1222 * destination of the rename. This implements append-only
1225 if ((dp->i_mode & S_ISTXT) &&
1226 VOP_ACCESS(tdvp, VADMIN, tcnp->cn_cred, td) &&
1227 VOP_ACCESS(tvp, VADMIN, tcnp->cn_cred, td)) {
1232 * Target must be empty if a directory and have no links
1233 * to it. Also, ensure source and target are compatible
1234 * (both directories, or both not directories).
1236 if ((xp->i_mode&IFMT) == IFDIR) {
1237 if ((xp->i_effnlink > 2) ||
1238 !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1242 if (!doingdirectory) {
1247 } else if (doingdirectory) {
1251 error = ufs_dirrewrite(dp, xp, ip->i_number,
1253 (doingdirectory && newparent) ? newparent : doingdirectory);
1256 if (doingdirectory) {
1259 if (DOINGSOFTDEP(tdvp))
1260 softdep_change_linkcnt(dp);
1263 if (DOINGSOFTDEP(tvp))
1264 softdep_change_linkcnt(xp);
1266 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1268 * Truncate inode. The only stuff left in the directory
1269 * is "." and "..". The "." reference is inconsequential
1270 * since we are quashing it. We have removed the "."
1271 * reference and the reference in the parent directory,
1272 * but there may be other hard links. The soft
1273 * dependency code will arrange to do these operations
1274 * after the parent directory entry has been deleted on
1275 * disk, so when running with that code we avoid doing
1280 DIP_SET(dp, i_nlink, dp->i_nlink);
1281 dp->i_flag |= IN_CHANGE;
1284 DIP_SET(xp, i_nlink, xp->i_nlink);
1285 xp->i_flag |= IN_CHANGE;
1287 if (!DOINGASYNC(tvp))
1289 if ((error = UFS_TRUNCATE(tvp, (off_t)0, ioflag,
1290 tcnp->cn_cred, tcnp->cn_thread)) != 0)
1299 * 3) Unlink the source.
1301 fcnp->cn_flags &= ~MODMASK;
1302 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1303 if ((fcnp->cn_flags & SAVESTART) == 0)
1304 panic("ufs_rename: lost from startdir");
1306 error = relookup(fdvp, &fvp, fcnp);
1314 * From name has disappeared. IN_RENAME is not sufficient
1315 * to protect against directory races due to timing windows,
1316 * so we have to remove the panic. XXX the only real way
1317 * to solve this issue is at a much higher level. By the
1318 * time we hit ufs_rename() it's too late.
1322 panic("ufs_rename: lost dir entry");
1328 * Ensure that the directory entry still exists and has not
1329 * changed while the new name has been entered. If the source is
1330 * a file then the entry may have been unlinked or renamed. In
1331 * either case there is no further work to be done. If the source
1332 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1333 * flag ensures that it cannot be moved by another rename or removed
1338 * From name resolves to a different inode. IN_RENAME is
1339 * not sufficient protection against timing window races
1340 * so we can't panic here. XXX the only real way
1341 * to solve this issue is at a much higher level. By the
1342 * time we hit ufs_rename() it's too late.
1346 panic("ufs_rename: lost dir entry");
1350 * If the source is a directory with a
1351 * new parent, the link count of the old
1352 * parent directory must be decremented
1353 * and ".." set to point to the new parent.
1355 if (doingdirectory && newparent) {
1356 xp->i_offset = mastertemplate.dot_reclen;
1357 ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1360 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1361 xp->i_flag &= ~IN_RENAME;
1376 ip->i_flag &= ~IN_RENAME;
1377 if (vn_lock(fvp, LK_EXCLUSIVE, td) == 0) {
1380 DIP_SET(ip, i_nlink, ip->i_nlink);
1381 ip->i_flag |= IN_CHANGE;
1382 ip->i_flag &= ~IN_RENAME;
1383 if (DOINGSOFTDEP(fvp))
1384 softdep_change_linkcnt(ip);
1396 struct vop_mkdir_args /* {
1397 struct vnode *a_dvp;
1398 struct vnode **a_vpp;
1399 struct componentname *a_cnp;
1400 struct vattr *a_vap;
1403 struct vnode *dvp = ap->a_dvp;
1404 struct vattr *vap = ap->a_vap;
1405 struct componentname *cnp = ap->a_cnp;
1406 struct inode *ip, *dp;
1409 struct dirtemplate dirtemplate, *dtp;
1410 struct direct newdir;
1412 struct acl *acl, *dacl;
1418 if ((cnp->cn_flags & HASBUF) == 0)
1419 panic("ufs_mkdir: no name");
1422 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1426 dmode = vap->va_mode & 0777;
1429 * Must simulate part of ufs_makeinode here to acquire the inode,
1430 * but not have it entered in the parent directory. The entry is
1431 * made later after writing "." and ".." entries.
1433 error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
1437 ip->i_gid = dp->i_gid;
1438 DIP_SET(ip, i_gid, dp->i_gid);
1442 struct ucred ucred, *ucp;
1446 * If we are hacking owners here, (only do this where told to)
1447 * and we are not giving it TO root, (would subvert quotas)
1448 * then go ahead and give it to the other user.
1449 * The new directory also inherits the SUID bit.
1450 * If user's UID and dir UID are the same,
1451 * 'give it away' so that the SUID is still forced on.
1453 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1454 (dp->i_mode & ISUID) && dp->i_uid) {
1456 ip->i_uid = dp->i_uid;
1457 DIP_SET(ip, i_uid, dp->i_uid);
1459 if (dp->i_uid != cnp->cn_cred->cr_uid) {
1461 * Make sure the correct user gets charged
1463 * Make a dummy credential for the victim.
1464 * XXX This seems to never be accessed out of
1465 * our context so a stack variable is ok.
1467 refcount_init(&ucred.cr_ref, 1);
1468 ucred.cr_uid = ip->i_uid;
1469 ucred.cr_ngroups = 1;
1470 ucred.cr_groups[0] = dp->i_gid;
1475 ip->i_uid = cnp->cn_cred->cr_uid;
1476 DIP_SET(ip, i_uid, ip->i_uid);
1479 if ((error = getinoquota(ip)) ||
1480 (error = chkiq(ip, 1, ucp, 0))) {
1481 UFS_VFREE(tvp, ip->i_number, dmode);
1487 #else /* !SUIDDIR */
1488 ip->i_uid = cnp->cn_cred->cr_uid;
1489 DIP_SET(ip, i_uid, ip->i_uid);
1491 if ((error = getinoquota(ip)) ||
1492 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1493 UFS_VFREE(tvp, ip->i_number, dmode);
1498 #endif /* !SUIDDIR */
1499 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1502 if ((dvp->v_mount->mnt_flag & MNT_ACLS) != 0) {
1503 acl = uma_zalloc(acl_zone, M_WAITOK);
1504 dacl = uma_zalloc(acl_zone, M_WAITOK);
1507 * Retrieve default ACL from parent, if any.
1509 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cnp->cn_cred,
1514 * Retrieved a default ACL, so merge mode and ACL if
1515 * necessary. If the ACL is empty, fall through to
1516 * the "not defined or available" case.
1518 if (acl->acl_cnt != 0) {
1519 dmode = acl_posix1e_newfilemode(dmode, acl);
1521 DIP_SET(ip, i_mode, dmode);
1523 ufs_sync_acl_from_inode(ip, acl);
1530 * Just use the mode as-is.
1533 DIP_SET(ip, i_mode, dmode);
1534 uma_zfree(acl_zone, acl);
1535 uma_zfree(acl_zone, dacl);
1540 UFS_VFREE(tvp, ip->i_number, dmode);
1542 uma_zfree(acl_zone, acl);
1543 uma_zfree(acl_zone, dacl);
1547 #endif /* !UFS_ACL */
1549 DIP_SET(ip, i_mode, dmode);
1553 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1556 DIP_SET(ip, i_nlink, 2);
1557 if (DOINGSOFTDEP(tvp))
1558 softdep_change_linkcnt(ip);
1559 if (cnp->cn_flags & ISWHITEOUT) {
1560 ip->i_flags |= UF_OPAQUE;
1561 DIP_SET(ip, i_flags, ip->i_flags);
1565 * Bump link count in parent directory to reflect work done below.
1566 * Should be done before reference is created so cleanup is
1567 * possible if we crash.
1571 DIP_SET(dp, i_nlink, dp->i_nlink);
1572 dp->i_flag |= IN_CHANGE;
1573 if (DOINGSOFTDEP(dvp))
1574 softdep_change_linkcnt(dp);
1575 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1579 if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
1580 error = mac_create_vnode_extattr(cnp->cn_cred, dvp->v_mount,
1589 * XXX: If we abort now, will Soft Updates notify the extattr
1590 * code that the EAs for the file need to be released?
1592 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cnp->cn_cred,
1595 error = VOP_SETACL(tvp, ACL_TYPE_DEFAULT, dacl,
1596 cnp->cn_cred, cnp->cn_thread);
1603 * XXX: This should not happen, as EOPNOTSUPP above
1604 * was supposed to free acl.
1606 printf("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()\n");
1608 panic("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()");
1613 uma_zfree(acl_zone, acl);
1614 uma_zfree(acl_zone, dacl);
1618 uma_zfree(acl_zone, acl);
1619 uma_zfree(acl_zone, dacl);
1622 #endif /* !UFS_ACL */
1625 * Initialize directory with "." and ".." from static template.
1627 if (dvp->v_mount->mnt_maxsymlinklen > 0)
1628 dtp = &mastertemplate;
1630 dtp = (struct dirtemplate *)&omastertemplate;
1632 dirtemplate.dot_ino = ip->i_number;
1633 dirtemplate.dotdot_ino = dp->i_number;
1634 if ((error = UFS_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred,
1635 BA_CLRBUF, &bp)) != 0)
1637 ip->i_size = DIRBLKSIZ;
1638 DIP_SET(ip, i_size, DIRBLKSIZ);
1639 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1640 vnode_pager_setsize(tvp, (u_long)ip->i_size);
1641 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1642 if (DOINGSOFTDEP(tvp)) {
1644 * Ensure that the entire newly allocated block is a
1645 * valid directory so that future growth within the
1646 * block does not have to ensure that the block is
1647 * written before the inode.
1650 while (blkoff < bp->b_bcount) {
1652 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1653 blkoff += DIRBLKSIZ;
1656 if ((error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) |
1657 DOINGASYNC(tvp)))) != 0) {
1662 * Directory set up, now install its entry in the parent directory.
1664 * If we are not doing soft dependencies, then we must write out the
1665 * buffer containing the new directory body before entering the new
1666 * name in the parent. If we are doing soft dependencies, then the
1667 * buffer containing the new directory body will be passed to and
1668 * released in the soft dependency code after the code has attached
1669 * an appropriate ordering dependency to the buffer which ensures that
1670 * the buffer is written before the new name is written in the parent.
1672 if (DOINGASYNC(dvp))
1674 else if (!DOINGSOFTDEP(dvp) && ((error = bwrite(bp))))
1676 ufs_makedirentry(ip, cnp, &newdir);
1677 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1685 uma_zfree(acl_zone, acl);
1687 uma_zfree(acl_zone, dacl);
1691 DIP_SET(dp, i_nlink, dp->i_nlink);
1692 dp->i_flag |= IN_CHANGE;
1693 if (DOINGSOFTDEP(dvp))
1694 softdep_change_linkcnt(dp);
1696 * No need to do an explicit VOP_TRUNCATE here, vrele will
1697 * do this for us because we set the link count to 0.
1701 DIP_SET(ip, i_nlink, 0);
1702 ip->i_flag |= IN_CHANGE;
1703 if (DOINGSOFTDEP(tvp))
1704 softdep_change_linkcnt(ip);
1712 * Rmdir system call.
1716 struct vop_rmdir_args /* {
1717 struct vnode *a_dvp;
1719 struct componentname *a_cnp;
1722 struct vnode *vp = ap->a_vp;
1723 struct vnode *dvp = ap->a_dvp;
1724 struct componentname *cnp = ap->a_cnp;
1725 struct inode *ip, *dp;
1732 * Do not remove a directory that is in the process of being renamed.
1733 * Verify the directory is empty (and valid). Rmdir ".." will not be
1734 * valid since ".." will contain a reference to the current directory
1735 * and thus be non-empty. Do not allow the removal of mounted on
1736 * directories (this can happen when an NFS exported filesystem
1737 * tries to remove a locally mounted on directory).
1740 if ((ip->i_flag & IN_RENAME) || ip->i_effnlink < 2) {
1744 if (!ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1748 if ((dp->i_flags & APPEND)
1749 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1753 if (vp->v_mountedhere != 0) {
1758 ufs_gjournal_orphan(vp);
1761 * Delete reference to directory before purging
1762 * inode. If we crash in between, the directory
1763 * will be reattached to lost+found,
1767 if (DOINGSOFTDEP(vp)) {
1768 softdep_change_linkcnt(dp);
1769 softdep_change_linkcnt(ip);
1771 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1775 if (DOINGSOFTDEP(vp)) {
1776 softdep_change_linkcnt(dp);
1777 softdep_change_linkcnt(ip);
1783 * Truncate inode. The only stuff left in the directory is "." and
1784 * "..". The "." reference is inconsequential since we are quashing
1785 * it. The soft dependency code will arrange to do these operations
1786 * after the parent directory entry has been deleted on disk, so
1787 * when running with that code we avoid doing them now.
1789 if (!DOINGSOFTDEP(vp)) {
1791 DIP_SET(dp, i_nlink, dp->i_nlink);
1792 dp->i_flag |= IN_CHANGE;
1794 DIP_SET(ip, i_nlink, ip->i_nlink);
1795 ip->i_flag |= IN_CHANGE;
1797 if (!DOINGASYNC(vp))
1799 error = UFS_TRUNCATE(vp, (off_t)0, ioflag, cnp->cn_cred,
1804 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1805 if (ip->i_dirhash != NULL)
1806 ufsdirhash_free(ip);
1813 * symlink -- make a symbolic link
1817 struct vop_symlink_args /* {
1818 struct vnode *a_dvp;
1819 struct vnode **a_vpp;
1820 struct componentname *a_cnp;
1821 struct vattr *a_vap;
1825 struct vnode *vp, **vpp = ap->a_vpp;
1829 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1834 len = strlen(ap->a_target);
1835 if (len < vp->v_mount->mnt_maxsymlinklen) {
1837 bcopy(ap->a_target, SHORTLINK(ip), len);
1839 DIP_SET(ip, i_size, len);
1840 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1842 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1843 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1844 ap->a_cnp->cn_cred, NOCRED, (int *)0, (struct thread *)0);
1851 * Vnode op for reading directories.
1853 * The routine below assumes that the on-disk format of a directory
1854 * is the same as that defined by <sys/dirent.h>. If the on-disk
1855 * format changes, then it will be necessary to do a conversion
1856 * from the on-disk format that read returns to the format defined
1857 * by <sys/dirent.h>.
1861 struct vop_readdir_args /* {
1864 struct ucred *a_cred;
1870 struct uio *uio = ap->a_uio;
1875 if (ap->a_ncookies != NULL)
1877 * Ensure that the block is aligned. The caller can use
1878 * the cookies to determine where in the block to start.
1880 uio->uio_offset &= ~(DIRBLKSIZ - 1);
1881 off = uio->uio_offset;
1882 count = uio->uio_resid;
1883 /* Make sure we don't return partial entries. */
1884 if (count <= ((uio->uio_offset + count) & (DIRBLKSIZ -1)))
1886 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
1887 lost = uio->uio_resid - count;
1888 uio->uio_resid = count;
1889 uio->uio_iov->iov_len = count;
1890 # if (BYTE_ORDER == LITTLE_ENDIAN)
1891 if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) {
1892 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1894 struct dirent *dp, *edp;
1902 auio.uio_iov = &aiov;
1903 auio.uio_iovcnt = 1;
1904 auio.uio_segflg = UIO_SYSSPACE;
1905 aiov.iov_len = count;
1906 MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
1907 aiov.iov_base = dirbuf;
1908 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
1910 readcnt = count - auio.uio_resid;
1911 edp = (struct dirent *)&dirbuf[readcnt];
1912 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
1914 dp->d_namlen = dp->d_type;
1916 if (dp->d_reclen > 0) {
1917 dp = (struct dirent *)
1918 ((char *)dp + dp->d_reclen);
1925 error = uiomove(dirbuf, readcnt, uio);
1927 FREE(dirbuf, M_TEMP);
1930 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1932 if (!error && ap->a_ncookies != NULL) {
1933 struct dirent* dpStart;
1934 struct dirent* dpEnd;
1940 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1941 panic("ufs_readdir: unexpected uio from NFS server");
1942 dpStart = (struct dirent *)
1943 ((char *)uio->uio_iov->iov_base - (uio->uio_offset - off));
1944 dpEnd = (struct dirent *) uio->uio_iov->iov_base;
1945 for (dp = dpStart, ncookies = 0;
1947 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen))
1949 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1951 for (dp = dpStart, cookiep = cookies;
1953 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
1954 off += dp->d_reclen;
1955 *cookiep++ = (u_long) off;
1957 *ap->a_ncookies = ncookies;
1958 *ap->a_cookies = cookies;
1960 uio->uio_resid += lost;
1962 *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
1967 * Return target name of a symbolic link
1971 struct vop_readlink_args /* {
1974 struct ucred *a_cred;
1977 struct vnode *vp = ap->a_vp;
1978 struct inode *ip = VTOI(vp);
1982 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1983 DIP(ip, i_blocks) == 0) { /* XXX - for old fastlink support */
1984 return (uiomove(SHORTLINK(ip), isize, ap->a_uio));
1986 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1990 * Calculate the logical to physical mapping if not done already,
1991 * then call the device strategy routine.
1993 * In order to be able to swap to a file, the ufs_bmaparray() operation may not
1994 * deadlock on memory. See ufs_bmap() for details.
1998 struct vop_strategy_args /* {
2003 struct buf *bp = ap->a_bp;
2004 struct vnode *vp = ap->a_vp;
2011 if (bp->b_blkno == bp->b_lblkno) {
2012 error = ufs_bmaparray(vp, bp->b_lblkno, &blkno, bp, NULL, NULL);
2013 bp->b_blkno = blkno;
2015 bp->b_error = error;
2016 bp->b_ioflags |= BIO_ERROR;
2020 if ((long)bp->b_blkno == -1)
2023 if ((long)bp->b_blkno == -1) {
2027 bp->b_iooffset = dbtob(bp->b_blkno);
2028 bo = ip->i_umbufobj;
2029 BO_STRATEGY(bo, bp);
2034 * Print out the contents of an inode.
2038 struct vop_print_args /* {
2042 struct vnode *vp = ap->a_vp;
2043 struct inode *ip = VTOI(vp);
2045 printf("\tino %lu, on dev %s", (u_long)ip->i_number,
2046 devtoname(ip->i_dev));
2047 if (vp->v_type == VFIFO)
2054 * Close wrapper for fifos.
2056 * Update the times on the inode then do device close.
2060 struct vop_close_args /* {
2063 struct ucred *a_cred;
2064 struct thread *a_td;
2067 struct vnode *vp = ap->a_vp;
2071 usecount = vp->v_usecount;
2073 ufs_itimes_locked(vp);
2075 return (fifo_specops.vop_close(ap));
2079 * Kqfilter wrapper for fifos.
2081 * Fall through to ufs kqfilter routines if needed
2084 ufsfifo_kqfilter(ap)
2085 struct vop_kqfilter_args *ap;
2089 error = fifo_specops.vop_kqfilter(ap);
2091 error = vfs_kqfilter(ap);
2096 * Return POSIX pathconf information applicable to ufs filesystems.
2100 struct vop_pathconf_args /* {
2109 switch (ap->a_name) {
2111 *ap->a_retval = LINK_MAX;
2114 *ap->a_retval = NAME_MAX;
2117 *ap->a_retval = PATH_MAX;
2120 *ap->a_retval = PIPE_BUF;
2122 case _PC_CHOWN_RESTRICTED:
2128 case _PC_ACL_EXTENDED:
2130 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
2138 case _PC_ACL_PATH_MAX:
2140 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
2141 *ap->a_retval = ACL_MAX_ENTRIES;
2148 case _PC_MAC_PRESENT:
2150 if (ap->a_vp->v_mount->mnt_flag & MNT_MULTILABEL)
2159 /* _PC_ASYNC_IO should have been handled by upper layers. */
2160 KASSERT(0, ("_PC_ASYNC_IO should not get here"));
2169 case _PC_ALLOC_SIZE_MIN:
2170 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
2172 case _PC_FILESIZEBITS:
2175 case _PC_REC_INCR_XFER_SIZE:
2176 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2178 case _PC_REC_MAX_XFER_SIZE:
2179 *ap->a_retval = -1; /* means ``unlimited'' */
2181 case _PC_REC_MIN_XFER_SIZE:
2182 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2184 case _PC_REC_XFER_ALIGN:
2185 *ap->a_retval = PAGE_SIZE;
2187 case _PC_SYMLINK_MAX:
2188 *ap->a_retval = MAXPATHLEN;
2199 * Initialize the vnode associated with a new inode, handle aliased
2203 ufs_vinit(mntp, fifoops, vpp)
2205 struct vop_vector *fifoops;
2213 vp->v_type = IFTOVT(ip->i_mode);
2214 if (vp->v_type == VFIFO)
2216 ASSERT_VOP_LOCKED(vp, "ufs_vinit");
2217 if (ip->i_number == ROOTINO)
2218 vp->v_vflag |= VV_ROOT;
2219 ip->i_modrev = init_va_filerev();
2225 * Allocate a new inode.
2226 * Vnode dvp must be locked.
2229 ufs_makeinode(mode, dvp, vpp, cnp)
2233 struct componentname *cnp;
2235 struct inode *ip, *pdir;
2236 struct direct newdir;
2245 if ((cnp->cn_flags & HASBUF) == 0)
2246 panic("ufs_makeinode: no name");
2249 if ((mode & IFMT) == 0)
2252 error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2256 ip->i_gid = pdir->i_gid;
2257 DIP_SET(ip, i_gid, pdir->i_gid);
2261 struct ucred ucred, *ucp;
2265 * If we are not the owner of the directory,
2266 * and we are hacking owners here, (only do this where told to)
2267 * and we are not giving it TO root, (would subvert quotas)
2268 * then go ahead and give it to the other user.
2269 * Note that this drops off the execute bits for security.
2271 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2272 (pdir->i_mode & ISUID) &&
2273 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2274 ip->i_uid = pdir->i_uid;
2275 DIP_SET(ip, i_uid, ip->i_uid);
2279 * Make sure the correct user gets charged
2281 * Quickly knock up a dummy credential for the victim.
2282 * XXX This seems to never be accessed out of our
2283 * context so a stack variable is ok.
2285 refcount_init(&ucred.cr_ref, 1);
2286 ucred.cr_uid = ip->i_uid;
2287 ucred.cr_ngroups = 1;
2288 ucred.cr_groups[0] = pdir->i_gid;
2292 ip->i_uid = cnp->cn_cred->cr_uid;
2293 DIP_SET(ip, i_uid, ip->i_uid);
2297 if ((error = getinoquota(ip)) ||
2298 (error = chkiq(ip, 1, ucp, 0))) {
2299 UFS_VFREE(tvp, ip->i_number, mode);
2305 #else /* !SUIDDIR */
2306 ip->i_uid = cnp->cn_cred->cr_uid;
2307 DIP_SET(ip, i_uid, ip->i_uid);
2309 if ((error = getinoquota(ip)) ||
2310 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2311 UFS_VFREE(tvp, ip->i_number, mode);
2316 #endif /* !SUIDDIR */
2317 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2320 if ((dvp->v_mount->mnt_flag & MNT_ACLS) != 0) {
2321 acl = uma_zalloc(acl_zone, M_WAITOK);
2324 * Retrieve default ACL for parent, if any.
2326 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cnp->cn_cred,
2331 * Retrieved a default ACL, so merge mode and ACL if
2334 if (acl->acl_cnt != 0) {
2336 * Two possible ways for default ACL to not
2337 * be present. First, the EA can be
2338 * undefined, or second, the default ACL can
2339 * be blank. If it's blank, fall through to
2340 * the it's not defined case.
2342 mode = acl_posix1e_newfilemode(mode, acl);
2344 DIP_SET(ip, i_mode, mode);
2345 ufs_sync_acl_from_inode(ip, acl);
2352 * Just use the mode as-is.
2355 DIP_SET(ip, i_mode, mode);
2356 uma_zfree(acl_zone, acl);
2361 UFS_VFREE(tvp, ip->i_number, mode);
2363 uma_zfree(acl_zone, acl);
2370 DIP_SET(ip, i_mode, mode);
2374 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2377 DIP_SET(ip, i_nlink, 1);
2378 if (DOINGSOFTDEP(tvp))
2379 softdep_change_linkcnt(ip);
2380 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2381 priv_check_cred(cnp->cn_cred, PRIV_VFS_SETGID, 0)) {
2382 ip->i_mode &= ~ISGID;
2383 DIP_SET(ip, i_mode, ip->i_mode);
2386 if (cnp->cn_flags & ISWHITEOUT) {
2387 ip->i_flags |= UF_OPAQUE;
2388 DIP_SET(ip, i_flags, ip->i_flags);
2392 * Make sure inode goes to disk before directory entry.
2394 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2398 if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
2399 error = mac_create_vnode_extattr(cnp->cn_cred, dvp->v_mount,
2408 * XXX: If we abort now, will Soft Updates notify the extattr
2409 * code that the EAs for the file need to be released?
2411 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cnp->cn_cred,
2419 * XXX: This should not happen, as EOPNOTSUPP above was
2420 * supposed to free acl.
2422 printf("ufs_makeinode: VOP_GETACL() but no "
2424 /* panic("ufs_makeinode: VOP_GETACL() but no "
2429 uma_zfree(acl_zone, acl);
2432 uma_zfree(acl_zone, acl);
2434 #endif /* !UFS_ACL */
2435 ufs_makedirentry(ip, cnp, &newdir);
2436 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2444 * Write error occurred trying to update the inode
2445 * or the directory so must deallocate the inode.
2449 DIP_SET(ip, i_nlink, 0);
2450 ip->i_flag |= IN_CHANGE;
2451 if (DOINGSOFTDEP(tvp))
2452 softdep_change_linkcnt(ip);
2457 /* Global vfs data structures for ufs. */
2458 struct vop_vector ufs_vnodeops = {
2459 .vop_default = &default_vnodeops,
2460 .vop_fsync = VOP_PANIC,
2461 .vop_read = VOP_PANIC,
2462 .vop_reallocblks = VOP_PANIC,
2463 .vop_write = VOP_PANIC,
2464 .vop_access = ufs_access,
2465 .vop_bmap = ufs_bmap,
2466 .vop_cachedlookup = ufs_lookup,
2467 .vop_close = ufs_close,
2468 .vop_create = ufs_create,
2469 .vop_getattr = ufs_getattr,
2470 .vop_inactive = ufs_inactive,
2471 .vop_link = ufs_link,
2472 .vop_lookup = vfs_cache_lookup,
2473 .vop_mkdir = ufs_mkdir,
2474 .vop_mknod = ufs_mknod,
2475 .vop_open = ufs_open,
2476 .vop_pathconf = ufs_pathconf,
2477 .vop_poll = vop_stdpoll,
2478 .vop_print = ufs_print,
2479 .vop_readdir = ufs_readdir,
2480 .vop_readlink = ufs_readlink,
2481 .vop_reclaim = ufs_reclaim,
2482 .vop_remove = ufs_remove,
2483 .vop_rename = ufs_rename,
2484 .vop_rmdir = ufs_rmdir,
2485 .vop_setattr = ufs_setattr,
2487 .vop_setlabel = vop_stdsetlabel_ea,
2489 .vop_strategy = ufs_strategy,
2490 .vop_symlink = ufs_symlink,
2491 .vop_whiteout = ufs_whiteout,
2493 .vop_getextattr = ufs_getextattr,
2494 .vop_deleteextattr = ufs_deleteextattr,
2495 .vop_setextattr = ufs_setextattr,
2498 .vop_getacl = ufs_getacl,
2499 .vop_setacl = ufs_setacl,
2500 .vop_aclcheck = ufs_aclcheck,
2504 struct vop_vector ufs_fifoops = {
2505 .vop_default = &fifo_specops,
2506 .vop_fsync = VOP_PANIC,
2507 .vop_access = ufs_access,
2508 .vop_close = ufsfifo_close,
2509 .vop_getattr = ufs_getattr,
2510 .vop_inactive = ufs_inactive,
2511 .vop_kqfilter = ufsfifo_kqfilter,
2512 .vop_print = ufs_print,
2513 .vop_read = VOP_PANIC,
2514 .vop_reclaim = ufs_reclaim,
2515 .vop_setattr = ufs_setattr,
2517 .vop_setlabel = vop_stdsetlabel_ea,
2519 .vop_write = VOP_PANIC,
2521 .vop_getextattr = ufs_getextattr,
2522 .vop_deleteextattr = ufs_deleteextattr,
2523 .vop_setextattr = ufs_setextattr,
2526 .vop_getacl = ufs_getacl,
2527 .vop_setacl = ufs_setacl,
2528 .vop_aclcheck = ufs_aclcheck,