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 vop_advlock_t ufs_advlock;
95 static int ufs_chmod(struct vnode *, int, struct ucred *, struct thread *);
96 static int ufs_chown(struct vnode *, uid_t, gid_t, struct ucred *, struct thread *);
97 static vop_close_t ufs_close;
98 static vop_create_t ufs_create;
99 static vop_getattr_t ufs_getattr;
100 static vop_link_t ufs_link;
101 static int ufs_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
102 static vop_mkdir_t ufs_mkdir;
103 static vop_mknod_t ufs_mknod;
104 static vop_open_t ufs_open;
105 static vop_pathconf_t ufs_pathconf;
106 static vop_print_t ufs_print;
107 static vop_readlink_t ufs_readlink;
108 static vop_remove_t ufs_remove;
109 static vop_rename_t ufs_rename;
110 static vop_rmdir_t ufs_rmdir;
111 static vop_setattr_t ufs_setattr;
112 static vop_strategy_t ufs_strategy;
113 static vop_symlink_t ufs_symlink;
114 static vop_whiteout_t ufs_whiteout;
115 static vop_close_t ufsfifo_close;
116 static vop_kqfilter_t ufsfifo_kqfilter;
119 * A virgin directory (no blushing please).
121 static struct dirtemplate mastertemplate = {
122 0, 12, DT_DIR, 1, ".",
123 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
125 static struct odirtemplate omastertemplate = {
127 0, DIRBLKSIZ - 12, 2, ".."
131 ufs_itimes_locked(struct vnode *vp)
136 ASSERT_VI_LOCKED(vp, __func__);
139 if ((vp->v_mount->mnt_flag & MNT_RDONLY) != 0)
141 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
144 if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
145 ip->i_flag |= IN_LAZYMOD;
146 else if (((vp->v_mount->mnt_kern_flag &
147 (MNTK_SUSPENDED | MNTK_SUSPEND)) == 0) ||
148 (ip->i_flag & (IN_CHANGE | IN_UPDATE)))
149 ip->i_flag |= IN_MODIFIED;
150 else if (ip->i_flag & IN_ACCESS)
151 ip->i_flag |= IN_LAZYACCESS;
153 if (ip->i_flag & IN_ACCESS) {
154 DIP_SET(ip, i_atime, ts.tv_sec);
155 DIP_SET(ip, i_atimensec, ts.tv_nsec);
157 if (ip->i_flag & IN_UPDATE) {
158 DIP_SET(ip, i_mtime, ts.tv_sec);
159 DIP_SET(ip, i_mtimensec, ts.tv_nsec);
162 if (ip->i_flag & IN_CHANGE) {
163 DIP_SET(ip, i_ctime, ts.tv_sec);
164 DIP_SET(ip, i_ctimensec, ts.tv_nsec);
168 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
172 ufs_itimes(struct vnode *vp)
176 ufs_itimes_locked(vp);
181 * Create a regular file
185 struct vop_create_args /* {
187 struct vnode **a_vpp;
188 struct componentname *a_cnp;
195 ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
196 ap->a_dvp, ap->a_vpp, ap->a_cnp);
208 struct vop_mknod_args /* {
210 struct vnode **a_vpp;
211 struct componentname *a_cnp;
215 struct vattr *vap = ap->a_vap;
216 struct vnode **vpp = ap->a_vpp;
221 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
222 ap->a_dvp, vpp, ap->a_cnp);
226 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
227 if (vap->va_rdev != VNOVAL) {
229 * Want to be able to use this to make badblock
230 * inodes, so don't truncate the dev number.
232 DIP_SET(ip, i_rdev, vap->va_rdev);
235 * Remove inode, then reload it through VFS_VGET so it is
236 * checked to see if it is an alias of an existing entry in
237 * the inode cache. XXX I don't believe this is necessary now.
239 (*vpp)->v_type = VNON;
240 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
243 error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
256 ufs_open(struct vop_open_args *ap)
258 struct vnode *vp = ap->a_vp;
261 if (vp->v_type == VCHR || vp->v_type == VBLK)
266 * Files marked append-only must be opened for appending.
268 if ((ip->i_flags & APPEND) &&
269 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
271 vnode_create_vobject(vp, DIP(ip, i_size), ap->a_td);
278 * Update the times on the inode.
283 struct vop_close_args /* {
286 struct ucred *a_cred;
290 struct vnode *vp = ap->a_vp;
294 usecount = vp->v_usecount;
296 ufs_itimes_locked(vp);
303 struct vop_access_args /* {
306 struct ucred *a_cred;
310 struct vnode *vp = ap->a_vp;
311 struct inode *ip = VTOI(vp);
312 mode_t mode = ap->a_mode;
319 * Disallow write attempts on read-only filesystems;
320 * unless the file is a socket, fifo, or a block or
321 * character device resident on the filesystem.
324 switch (vp->v_type) {
328 if (vp->v_mount->mnt_flag & MNT_RDONLY)
336 /* If immutable bit set, nobody gets to write it. */
337 if ((mode & VWRITE) && (ip->i_flags & (IMMUTABLE | SF_SNAPSHOT)))
341 if ((vp->v_mount->mnt_flag & MNT_ACLS) != 0) {
342 acl = uma_zalloc(acl_zone, M_WAITOK);
343 error = VOP_GETACL(vp, ACL_TYPE_ACCESS, acl, ap->a_cred,
347 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid,
348 ip->i_gid, ap->a_mode, ap->a_cred, NULL);
351 error = vaccess_acl_posix1e(vp->v_type, ip->i_uid,
352 ip->i_gid, acl, ap->a_mode, ap->a_cred, NULL);
356 "ufs_access(): Error retrieving ACL on object (%d).\n",
359 * XXX: Fall back until debugged. Should
360 * eventually possibly log an error, and return
363 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid,
364 ip->i_gid, ap->a_mode, ap->a_cred, NULL);
366 uma_zfree(acl_zone, acl);
368 #endif /* !UFS_ACL */
369 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
370 ap->a_mode, ap->a_cred, NULL);
377 struct vop_getattr_args /* {
380 struct ucred *a_cred;
384 struct vnode *vp = ap->a_vp;
385 struct inode *ip = VTOI(vp);
386 struct vattr *vap = ap->a_vap;
389 ufs_itimes_locked(vp);
390 if (ip->i_ump->um_fstype == UFS1) {
391 vap->va_atime.tv_sec = ip->i_din1->di_atime;
392 vap->va_atime.tv_nsec = ip->i_din1->di_atimensec;
394 vap->va_atime.tv_sec = ip->i_din2->di_atime;
395 vap->va_atime.tv_nsec = ip->i_din2->di_atimensec;
399 * Copy from inode table
401 vap->va_fsid = dev2udev(ip->i_dev);
402 vap->va_fileid = ip->i_number;
403 vap->va_mode = ip->i_mode & ~IFMT;
404 vap->va_nlink = ip->i_effnlink;
405 vap->va_uid = ip->i_uid;
406 vap->va_gid = ip->i_gid;
407 if (ip->i_ump->um_fstype == UFS1) {
408 vap->va_rdev = ip->i_din1->di_rdev;
409 vap->va_size = ip->i_din1->di_size;
410 vap->va_mtime.tv_sec = ip->i_din1->di_mtime;
411 vap->va_mtime.tv_nsec = ip->i_din1->di_mtimensec;
412 vap->va_ctime.tv_sec = ip->i_din1->di_ctime;
413 vap->va_ctime.tv_nsec = ip->i_din1->di_ctimensec;
414 vap->va_birthtime.tv_sec = 0;
415 vap->va_birthtime.tv_nsec = 0;
416 vap->va_bytes = dbtob((u_quad_t)ip->i_din1->di_blocks);
418 vap->va_rdev = ip->i_din2->di_rdev;
419 vap->va_size = ip->i_din2->di_size;
420 vap->va_mtime.tv_sec = ip->i_din2->di_mtime;
421 vap->va_mtime.tv_nsec = ip->i_din2->di_mtimensec;
422 vap->va_ctime.tv_sec = ip->i_din2->di_ctime;
423 vap->va_ctime.tv_nsec = ip->i_din2->di_ctimensec;
424 vap->va_birthtime.tv_sec = ip->i_din2->di_birthtime;
425 vap->va_birthtime.tv_nsec = ip->i_din2->di_birthnsec;
426 vap->va_bytes = dbtob((u_quad_t)ip->i_din2->di_blocks);
428 vap->va_flags = ip->i_flags;
429 vap->va_gen = ip->i_gen;
430 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
431 vap->va_type = IFTOVT(ip->i_mode);
432 vap->va_filerev = ip->i_modrev;
437 * Set attribute vnode op. called from several syscalls
441 struct vop_setattr_args /* {
444 struct ucred *a_cred;
448 struct vattr *vap = ap->a_vap;
449 struct vnode *vp = ap->a_vp;
450 struct inode *ip = VTOI(vp);
451 struct ucred *cred = ap->a_cred;
452 struct thread *td = ap->a_td;
456 * Check for unsettable attributes.
458 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
459 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
460 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
461 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
465 * Mark for update the file's access time for vfs_mark_atime().
466 * We are doing this here to avoid some of the checks done
467 * below -- this operation is done by request of the kernel and
468 * should bypass some security checks. Things like read-only
469 * checks get handled by other levels (e.g., ffs_update()).
471 if (vap->va_vaflags & VA_MARK_ATIME) {
472 ip->i_flag |= IN_ACCESS;
475 if (vap->va_flags != VNOVAL) {
476 if (vp->v_mount->mnt_flag & MNT_RDONLY)
479 * Callers may only modify the file flags on objects they
480 * have VADMIN rights for.
482 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
485 * Unprivileged processes are not permitted to unset system
486 * flags, or modify flags if any system flags are set.
487 * Privileged non-jail processes may not modify system flags
488 * if securelevel > 0 and any existing system flags are set.
489 * Privileged jail processes behave like privileged non-jail
490 * processes if the security.jail.chflags_allowed sysctl is
491 * is non-zero; otherwise, they behave like unprivileged
494 * XXXRW: Move implementation of jail_chflags_allowed to
497 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS,
498 jail_chflags_allowed ? SUSER_ALLOWJAIL : 0)) {
500 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
501 error = securelevel_gt(cred, 0);
505 /* Snapshot flag cannot be set or cleared */
506 if (((vap->va_flags & SF_SNAPSHOT) != 0 &&
507 (ip->i_flags & SF_SNAPSHOT) == 0) ||
508 ((vap->va_flags & SF_SNAPSHOT) == 0 &&
509 (ip->i_flags & SF_SNAPSHOT) != 0))
511 ip->i_flags = vap->va_flags;
512 DIP_SET(ip, i_flags, vap->va_flags);
515 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
516 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
518 ip->i_flags &= SF_SETTABLE;
519 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
520 DIP_SET(ip, i_flags, ip->i_flags);
522 ip->i_flag |= IN_CHANGE;
523 if (vap->va_flags & (IMMUTABLE | APPEND))
526 if (ip->i_flags & (IMMUTABLE | APPEND))
529 * Go through the fields and update iff not VNOVAL.
531 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
532 if (vp->v_mount->mnt_flag & MNT_RDONLY)
534 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred,
538 if (vap->va_size != VNOVAL) {
540 * XXX most of the following special cases should be in
541 * callers instead of in N filesystems. The VDIR check
544 switch (vp->v_type) {
550 * Truncation should have an effect in these cases.
551 * Disallow it if the filesystem is read-only or
552 * the file is being snapshotted.
554 if (vp->v_mount->mnt_flag & MNT_RDONLY)
556 if ((ip->i_flags & SF_SNAPSHOT) != 0)
561 * According to POSIX, the result is unspecified
562 * for file types other than regular files,
563 * directories and shared memory objects. We
564 * don't support shared memory objects in the file
565 * system, and have dubious support for truncating
566 * symlinks. Just ignore the request in other cases.
570 if ((error = UFS_TRUNCATE(vp, vap->va_size, IO_NORMAL,
574 if (vap->va_atime.tv_sec != VNOVAL ||
575 vap->va_mtime.tv_sec != VNOVAL ||
576 vap->va_birthtime.tv_sec != VNOVAL) {
577 if (vp->v_mount->mnt_flag & MNT_RDONLY)
579 if ((ip->i_flags & SF_SNAPSHOT) != 0)
583 * If times is NULL, ... The caller must be the owner of
584 * the file, have permission to write the file, or be the
586 * If times is non-NULL, ... The caller must be the owner of
587 * the file or be the super-user.
589 * Possibly for historical reasons, try to use VADMIN in
590 * preference to VWRITE for a NULL timestamp. This means we
591 * will return EACCES in preference to EPERM if neither
594 if (vap->va_vaflags & VA_UTIMES_NULL) {
595 error = VOP_ACCESS(vp, VADMIN, cred, td);
597 error = VOP_ACCESS(vp, VWRITE, cred, td);
599 error = VOP_ACCESS(vp, VADMIN, cred, td);
602 if (vap->va_atime.tv_sec != VNOVAL)
603 ip->i_flag |= IN_ACCESS;
604 if (vap->va_mtime.tv_sec != VNOVAL)
605 ip->i_flag |= IN_CHANGE | IN_UPDATE;
606 if (vap->va_birthtime.tv_sec != VNOVAL &&
607 ip->i_ump->um_fstype == UFS2)
608 ip->i_flag |= IN_MODIFIED;
610 if (vap->va_atime.tv_sec != VNOVAL) {
611 DIP_SET(ip, i_atime, vap->va_atime.tv_sec);
612 DIP_SET(ip, i_atimensec, vap->va_atime.tv_nsec);
614 if (vap->va_mtime.tv_sec != VNOVAL) {
615 DIP_SET(ip, i_mtime, vap->va_mtime.tv_sec);
616 DIP_SET(ip, i_mtimensec, vap->va_mtime.tv_nsec);
618 if (vap->va_birthtime.tv_sec != VNOVAL &&
619 ip->i_ump->um_fstype == UFS2) {
620 ip->i_din2->di_birthtime = vap->va_birthtime.tv_sec;
621 ip->i_din2->di_birthnsec = vap->va_birthtime.tv_nsec;
623 error = UFS_UPDATE(vp, 0);
628 if (vap->va_mode != (mode_t)VNOVAL) {
629 if (vp->v_mount->mnt_flag & MNT_RDONLY)
631 if ((ip->i_flags & SF_SNAPSHOT) != 0 && (vap->va_mode &
632 (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP | S_IXOTH | S_IWOTH)))
634 error = ufs_chmod(vp, (int)vap->va_mode, cred, td);
640 * Change the mode on a file.
641 * Inode must be locked before calling.
644 ufs_chmod(vp, mode, cred, td)
650 struct inode *ip = VTOI(vp);
654 * To modify the permissions on a file, must possess VADMIN
657 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
660 * Privileged processes may set the sticky bit on non-directories,
661 * as well as set the setgid bit on a file with a group that the
662 * process is not a member of. Both of these are allowed in
665 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
666 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE,
670 if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
671 error = priv_check_cred(cred, PRIV_VFS_SETGID,
676 ip->i_mode &= ~ALLPERMS;
677 ip->i_mode |= (mode & ALLPERMS);
678 DIP_SET(ip, i_mode, ip->i_mode);
679 ip->i_flag |= IN_CHANGE;
684 * Perform chown operation on inode ip;
685 * inode must be locked prior to call.
688 ufs_chown(vp, uid, gid, cred, td)
695 struct inode *ip = VTOI(vp);
704 if (uid == (uid_t)VNOVAL)
706 if (gid == (gid_t)VNOVAL)
709 * To modify the ownership of a file, must possess VADMIN for that
712 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
715 * To change the owner of a file, or change the group of a file to a
716 * group of which we are not a member, the caller must have
719 if ((uid != ip->i_uid ||
720 (gid != ip->i_gid && !groupmember(gid, cred))) &&
721 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, SUSER_ALLOWJAIL)))
726 if ((error = getinoquota(ip)) != 0)
729 dqrele(vp, ip->i_dquot[USRQUOTA]);
730 ip->i_dquot[USRQUOTA] = NODQUOT;
733 dqrele(vp, ip->i_dquot[GRPQUOTA]);
734 ip->i_dquot[GRPQUOTA] = NODQUOT;
736 change = DIP(ip, i_blocks);
737 (void) chkdq(ip, -change, cred, CHOWN);
738 (void) chkiq(ip, -1, cred, CHOWN);
739 for (i = 0; i < MAXQUOTAS; i++) {
740 dqrele(vp, ip->i_dquot[i]);
741 ip->i_dquot[i] = NODQUOT;
745 DIP_SET(ip, i_gid, gid);
747 DIP_SET(ip, i_uid, uid);
749 if ((error = getinoquota(ip)) == 0) {
751 dqrele(vp, ip->i_dquot[USRQUOTA]);
752 ip->i_dquot[USRQUOTA] = NODQUOT;
755 dqrele(vp, ip->i_dquot[GRPQUOTA]);
756 ip->i_dquot[GRPQUOTA] = NODQUOT;
758 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
759 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
762 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
764 for (i = 0; i < MAXQUOTAS; i++) {
765 dqrele(vp, ip->i_dquot[i]);
766 ip->i_dquot[i] = NODQUOT;
770 DIP_SET(ip, i_gid, ogid);
772 DIP_SET(ip, i_uid, ouid);
773 if (getinoquota(ip) == 0) {
775 dqrele(vp, ip->i_dquot[USRQUOTA]);
776 ip->i_dquot[USRQUOTA] = NODQUOT;
779 dqrele(vp, ip->i_dquot[GRPQUOTA]);
780 ip->i_dquot[GRPQUOTA] = NODQUOT;
782 (void) chkdq(ip, change, cred, FORCE|CHOWN);
783 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
784 (void) getinoquota(ip);
789 panic("ufs_chown: lost quota");
791 ip->i_flag |= IN_CHANGE;
792 if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
793 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID,
795 ip->i_mode &= ~(ISUID | ISGID);
796 DIP_SET(ip, i_mode, ip->i_mode);
804 struct vop_remove_args /* {
807 struct componentname *a_cnp;
811 struct vnode *vp = ap->a_vp;
812 struct vnode *dvp = ap->a_dvp;
818 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
819 (VTOI(dvp)->i_flags & APPEND)) {
824 ufs_gjournal_orphan(vp);
826 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
827 if (ip->i_nlink <= 0)
828 vp->v_vflag |= VV_NOSYNC;
829 if ((ip->i_flags & SF_SNAPSHOT) != 0) {
831 * Avoid deadlock where another thread is trying to
832 * update the inodeblock for dvp and is waiting on
833 * snaplk. Temporary unlock the vnode lock for the
834 * unlinked file and sync the directory. This should
835 * allow vput() of the directory to not block later on
836 * while holding the snapshot vnode locked, assuming
837 * that the directory hasn't been unlinked too.
839 VOP_UNLOCK(vp, 0, td);
840 (void) VOP_FSYNC(dvp, MNT_WAIT, td);
841 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
852 struct vop_link_args /* {
853 struct vnode *a_tdvp;
855 struct componentname *a_cnp;
858 struct vnode *vp = ap->a_vp;
859 struct vnode *tdvp = ap->a_tdvp;
860 struct componentname *cnp = ap->a_cnp;
862 struct direct newdir;
866 if ((cnp->cn_flags & HASBUF) == 0)
867 panic("ufs_link: no name");
869 if (tdvp->v_mount != vp->v_mount) {
874 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
878 if (ip->i_flags & (IMMUTABLE | APPEND)) {
884 DIP_SET(ip, i_nlink, ip->i_nlink);
885 ip->i_flag |= IN_CHANGE;
886 if (DOINGSOFTDEP(vp))
887 softdep_change_linkcnt(ip);
888 error = UFS_UPDATE(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
890 ufs_makedirentry(ip, cnp, &newdir);
891 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
897 DIP_SET(ip, i_nlink, ip->i_nlink);
898 ip->i_flag |= IN_CHANGE;
899 if (DOINGSOFTDEP(vp))
900 softdep_change_linkcnt(ip);
907 * whiteout vnode call
911 struct vop_whiteout_args /* {
913 struct componentname *a_cnp;
917 struct vnode *dvp = ap->a_dvp;
918 struct componentname *cnp = ap->a_cnp;
919 struct direct newdir;
922 switch (ap->a_flags) {
924 /* 4.4 format directories support whiteout operations */
925 if (dvp->v_mount->mnt_maxsymlinklen > 0)
930 /* create a new directory whiteout */
932 if ((cnp->cn_flags & SAVENAME) == 0)
933 panic("ufs_whiteout: missing name");
934 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
935 panic("ufs_whiteout: old format filesystem");
939 newdir.d_namlen = cnp->cn_namelen;
940 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
941 newdir.d_type = DT_WHT;
942 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
946 /* remove an existing directory whiteout */
948 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
949 panic("ufs_whiteout: old format filesystem");
952 cnp->cn_flags &= ~DOWHITEOUT;
953 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
956 panic("ufs_whiteout: unknown op");
962 * Rename system call.
963 * rename("foo", "bar");
966 * link("foo", "bar");
968 * but ``atomically''. Can't do full commit without saving state in the
969 * inode on disk which isn't feasible at this time. Best we can do is
970 * always guarantee the target exists.
972 * Basic algorithm is:
974 * 1) Bump link count on source while we're linking it to the
975 * target. This also ensure the inode won't be deleted out
976 * from underneath us while we work (it may be truncated by
977 * a concurrent `trunc' or `open' for creation).
978 * 2) Link source to destination. If destination already exists,
980 * 3) Unlink source reference to inode if still around. If a
981 * directory was moved and the parent of the destination
982 * is different from the source, patch the ".." entry in the
987 struct vop_rename_args /* {
988 struct vnode *a_fdvp;
990 struct componentname *a_fcnp;
991 struct vnode *a_tdvp;
993 struct componentname *a_tcnp;
996 struct vnode *tvp = ap->a_tvp;
997 struct vnode *tdvp = ap->a_tdvp;
998 struct vnode *fvp = ap->a_fvp;
999 struct vnode *fdvp = ap->a_fdvp;
1000 struct componentname *tcnp = ap->a_tcnp;
1001 struct componentname *fcnp = ap->a_fcnp;
1002 struct thread *td = fcnp->cn_thread;
1003 struct inode *ip, *xp, *dp;
1004 struct direct newdir;
1005 int doingdirectory = 0, oldparent = 0, newparent = 0;
1006 int error = 0, ioflag;
1009 if ((tcnp->cn_flags & HASBUF) == 0 ||
1010 (fcnp->cn_flags & HASBUF) == 0)
1011 panic("ufs_rename: no name");
1014 * Check for cross-device rename.
1016 if ((fvp->v_mount != tdvp->v_mount) ||
1017 (tvp && (fvp->v_mount != tvp->v_mount))) {
1031 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1032 (VTOI(tdvp)->i_flags & APPEND))) {
1038 * Renaming a file to itself has no effect. The upper layers should
1039 * not call us in that case. Temporarily just warn if they do.
1042 printf("ufs_rename: fvp == tvp (can't happen)\n");
1047 if ((error = vn_lock(fvp, LK_EXCLUSIVE, td)) != 0)
1051 if (ip->i_nlink >= LINK_MAX) {
1052 VOP_UNLOCK(fvp, 0, td);
1056 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
1057 || (dp->i_flags & APPEND)) {
1058 VOP_UNLOCK(fvp, 0, td);
1062 if ((ip->i_mode & IFMT) == IFDIR) {
1064 * Avoid ".", "..", and aliases of "." for obvious reasons.
1066 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1067 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
1068 (ip->i_flag & IN_RENAME)) {
1069 VOP_UNLOCK(fvp, 0, td);
1073 ip->i_flag |= IN_RENAME;
1074 oldparent = dp->i_number;
1080 * When the target exists, both the directory
1081 * and target vnodes are returned locked.
1089 * 1) Bump link count while we're moving stuff
1090 * around. If we crash somewhere before
1091 * completing our work, the link count
1092 * may be wrong, but correctable.
1096 DIP_SET(ip, i_nlink, ip->i_nlink);
1097 ip->i_flag |= IN_CHANGE;
1098 if (DOINGSOFTDEP(fvp))
1099 softdep_change_linkcnt(ip);
1100 if ((error = UFS_UPDATE(fvp, !(DOINGSOFTDEP(fvp) |
1101 DOINGASYNC(fvp)))) != 0) {
1102 VOP_UNLOCK(fvp, 0, td);
1107 * If ".." must be changed (ie the directory gets a new
1108 * parent) then the source directory must not be in the
1109 * directory hierarchy above the target, as this would
1110 * orphan everything below the source directory. Also
1111 * the user must have write permission in the source so
1112 * as to be able to change "..". We must repeat the call
1113 * to namei, as the parent directory is unlocked by the
1114 * call to checkpath().
1116 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
1117 VOP_UNLOCK(fvp, 0, td);
1118 if (oldparent != dp->i_number)
1119 newparent = dp->i_number;
1120 if (doingdirectory && newparent) {
1121 if (error) /* write access check above */
1125 error = ufs_checkpath(ip, dp, tcnp->cn_cred);
1128 if ((tcnp->cn_flags & SAVESTART) == 0)
1129 panic("ufs_rename: lost to startdir");
1131 error = relookup(tdvp, &tvp, tcnp);
1141 * 2) If target doesn't exist, link the target
1142 * to the source and unlink the source.
1143 * Otherwise, rewrite the target directory
1144 * entry to reference the source inode and
1145 * expunge the original entry's existence.
1148 if (dp->i_dev != ip->i_dev)
1149 panic("ufs_rename: EXDEV");
1151 * Account for ".." in new directory.
1152 * When source and destination have the same
1153 * parent we don't fool with the link count.
1155 if (doingdirectory && newparent) {
1156 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1162 DIP_SET(dp, i_nlink, dp->i_nlink);
1163 dp->i_flag |= IN_CHANGE;
1164 if (DOINGSOFTDEP(tdvp))
1165 softdep_change_linkcnt(dp);
1166 error = UFS_UPDATE(tdvp, !(DOINGSOFTDEP(tdvp) |
1171 ufs_makedirentry(ip, tcnp, &newdir);
1172 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1174 if (doingdirectory && newparent) {
1177 DIP_SET(dp, i_nlink, dp->i_nlink);
1178 dp->i_flag |= IN_CHANGE;
1179 if (DOINGSOFTDEP(tdvp))
1180 softdep_change_linkcnt(dp);
1181 (void)UFS_UPDATE(tdvp, 1);
1187 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1188 panic("ufs_rename: EXDEV");
1190 * Short circuit rename(foo, foo).
1192 if (xp->i_number == ip->i_number)
1193 panic("ufs_rename: same file");
1195 * If the parent directory is "sticky", then the caller
1196 * must possess VADMIN for the parent directory, or the
1197 * destination of the rename. This implements append-only
1200 if ((dp->i_mode & S_ISTXT) &&
1201 VOP_ACCESS(tdvp, VADMIN, tcnp->cn_cred, td) &&
1202 VOP_ACCESS(tvp, VADMIN, tcnp->cn_cred, td)) {
1207 * Target must be empty if a directory and have no links
1208 * to it. Also, ensure source and target are compatible
1209 * (both directories, or both not directories).
1211 if ((xp->i_mode&IFMT) == IFDIR) {
1212 if ((xp->i_effnlink > 2) ||
1213 !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1217 if (!doingdirectory) {
1222 } else if (doingdirectory) {
1226 error = ufs_dirrewrite(dp, xp, ip->i_number,
1228 (doingdirectory && newparent) ? newparent : doingdirectory);
1231 if (doingdirectory) {
1234 if (DOINGSOFTDEP(tdvp))
1235 softdep_change_linkcnt(dp);
1238 if (DOINGSOFTDEP(tvp))
1239 softdep_change_linkcnt(xp);
1241 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1243 * Truncate inode. The only stuff left in the directory
1244 * is "." and "..". The "." reference is inconsequential
1245 * since we are quashing it. We have removed the "."
1246 * reference and the reference in the parent directory,
1247 * but there may be other hard links. The soft
1248 * dependency code will arrange to do these operations
1249 * after the parent directory entry has been deleted on
1250 * disk, so when running with that code we avoid doing
1255 DIP_SET(dp, i_nlink, dp->i_nlink);
1256 dp->i_flag |= IN_CHANGE;
1259 DIP_SET(xp, i_nlink, xp->i_nlink);
1260 xp->i_flag |= IN_CHANGE;
1262 if (!DOINGASYNC(tvp))
1264 if ((error = UFS_TRUNCATE(tvp, (off_t)0, ioflag,
1265 tcnp->cn_cred, tcnp->cn_thread)) != 0)
1274 * 3) Unlink the source.
1276 fcnp->cn_flags &= ~MODMASK;
1277 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1278 if ((fcnp->cn_flags & SAVESTART) == 0)
1279 panic("ufs_rename: lost from startdir");
1281 error = relookup(fdvp, &fvp, fcnp);
1289 * From name has disappeared. IN_RENAME is not sufficient
1290 * to protect against directory races due to timing windows,
1291 * so we have to remove the panic. XXX the only real way
1292 * to solve this issue is at a much higher level. By the
1293 * time we hit ufs_rename() it's too late.
1297 panic("ufs_rename: lost dir entry");
1303 * Ensure that the directory entry still exists and has not
1304 * changed while the new name has been entered. If the source is
1305 * a file then the entry may have been unlinked or renamed. In
1306 * either case there is no further work to be done. If the source
1307 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1308 * flag ensures that it cannot be moved by another rename or removed
1313 * From name resolves to a different inode. IN_RENAME is
1314 * not sufficient protection against timing window races
1315 * so we can't panic here. XXX the only real way
1316 * to solve this issue is at a much higher level. By the
1317 * time we hit ufs_rename() it's too late.
1321 panic("ufs_rename: lost dir entry");
1325 * If the source is a directory with a
1326 * new parent, the link count of the old
1327 * parent directory must be decremented
1328 * and ".." set to point to the new parent.
1330 if (doingdirectory && newparent) {
1331 xp->i_offset = mastertemplate.dot_reclen;
1332 ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1335 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1336 xp->i_flag &= ~IN_RENAME;
1351 ip->i_flag &= ~IN_RENAME;
1352 if (vn_lock(fvp, LK_EXCLUSIVE, td) == 0) {
1355 DIP_SET(ip, i_nlink, ip->i_nlink);
1356 ip->i_flag |= IN_CHANGE;
1357 ip->i_flag &= ~IN_RENAME;
1358 if (DOINGSOFTDEP(fvp))
1359 softdep_change_linkcnt(ip);
1371 struct vop_mkdir_args /* {
1372 struct vnode *a_dvp;
1373 struct vnode **a_vpp;
1374 struct componentname *a_cnp;
1375 struct vattr *a_vap;
1378 struct vnode *dvp = ap->a_dvp;
1379 struct vattr *vap = ap->a_vap;
1380 struct componentname *cnp = ap->a_cnp;
1381 struct inode *ip, *dp;
1384 struct dirtemplate dirtemplate, *dtp;
1385 struct direct newdir;
1387 struct acl *acl, *dacl;
1393 if ((cnp->cn_flags & HASBUF) == 0)
1394 panic("ufs_mkdir: no name");
1397 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1401 dmode = vap->va_mode & 0777;
1404 * Must simulate part of ufs_makeinode here to acquire the inode,
1405 * but not have it entered in the parent directory. The entry is
1406 * made later after writing "." and ".." entries.
1408 error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
1412 ip->i_gid = dp->i_gid;
1413 DIP_SET(ip, i_gid, dp->i_gid);
1417 struct ucred ucred, *ucp;
1421 * If we are hacking owners here, (only do this where told to)
1422 * and we are not giving it TO root, (would subvert quotas)
1423 * then go ahead and give it to the other user.
1424 * The new directory also inherits the SUID bit.
1425 * If user's UID and dir UID are the same,
1426 * 'give it away' so that the SUID is still forced on.
1428 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1429 (dp->i_mode & ISUID) && dp->i_uid) {
1431 ip->i_uid = dp->i_uid;
1432 DIP_SET(ip, i_uid, dp->i_uid);
1434 if (dp->i_uid != cnp->cn_cred->cr_uid) {
1436 * Make sure the correct user gets charged
1438 * Make a dummy credential for the victim.
1439 * XXX This seems to never be accessed out of
1440 * our context so a stack variable is ok.
1442 refcount_init(&ucred.cr_ref, 1);
1443 ucred.cr_uid = ip->i_uid;
1444 ucred.cr_ngroups = 1;
1445 ucred.cr_groups[0] = dp->i_gid;
1450 ip->i_uid = cnp->cn_cred->cr_uid;
1451 DIP_SET(ip, i_uid, ip->i_uid);
1454 if ((error = getinoquota(ip)) ||
1455 (error = chkiq(ip, 1, ucp, 0))) {
1456 UFS_VFREE(tvp, ip->i_number, dmode);
1462 #else /* !SUIDDIR */
1463 ip->i_uid = cnp->cn_cred->cr_uid;
1464 DIP_SET(ip, i_uid, ip->i_uid);
1466 if ((error = getinoquota(ip)) ||
1467 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1468 UFS_VFREE(tvp, ip->i_number, dmode);
1473 #endif /* !SUIDDIR */
1474 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1477 if ((dvp->v_mount->mnt_flag & MNT_ACLS) != 0) {
1478 acl = uma_zalloc(acl_zone, M_WAITOK);
1479 dacl = uma_zalloc(acl_zone, M_WAITOK);
1482 * Retrieve default ACL from parent, if any.
1484 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cnp->cn_cred,
1489 * Retrieved a default ACL, so merge mode and ACL if
1490 * necessary. If the ACL is empty, fall through to
1491 * the "not defined or available" case.
1493 if (acl->acl_cnt != 0) {
1494 dmode = acl_posix1e_newfilemode(dmode, acl);
1496 DIP_SET(ip, i_mode, dmode);
1498 ufs_sync_acl_from_inode(ip, acl);
1505 * Just use the mode as-is.
1508 DIP_SET(ip, i_mode, dmode);
1509 uma_zfree(acl_zone, acl);
1510 uma_zfree(acl_zone, dacl);
1515 UFS_VFREE(tvp, ip->i_number, dmode);
1517 uma_zfree(acl_zone, acl);
1518 uma_zfree(acl_zone, dacl);
1522 #endif /* !UFS_ACL */
1524 DIP_SET(ip, i_mode, dmode);
1528 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1531 DIP_SET(ip, i_nlink, 2);
1532 if (DOINGSOFTDEP(tvp))
1533 softdep_change_linkcnt(ip);
1534 if (cnp->cn_flags & ISWHITEOUT) {
1535 ip->i_flags |= UF_OPAQUE;
1536 DIP_SET(ip, i_flags, ip->i_flags);
1540 * Bump link count in parent directory to reflect work done below.
1541 * Should be done before reference is created so cleanup is
1542 * possible if we crash.
1546 DIP_SET(dp, i_nlink, dp->i_nlink);
1547 dp->i_flag |= IN_CHANGE;
1548 if (DOINGSOFTDEP(dvp))
1549 softdep_change_linkcnt(dp);
1550 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1554 if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
1555 error = mac_create_vnode_extattr(cnp->cn_cred, dvp->v_mount,
1564 * XXX: If we abort now, will Soft Updates notify the extattr
1565 * code that the EAs for the file need to be released?
1567 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cnp->cn_cred,
1570 error = VOP_SETACL(tvp, ACL_TYPE_DEFAULT, dacl,
1571 cnp->cn_cred, cnp->cn_thread);
1578 * XXX: This should not happen, as EOPNOTSUPP above
1579 * was supposed to free acl.
1581 printf("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()\n");
1583 panic("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()");
1588 uma_zfree(acl_zone, acl);
1589 uma_zfree(acl_zone, dacl);
1593 uma_zfree(acl_zone, acl);
1594 uma_zfree(acl_zone, dacl);
1597 #endif /* !UFS_ACL */
1600 * Initialize directory with "." and ".." from static template.
1602 if (dvp->v_mount->mnt_maxsymlinklen > 0)
1603 dtp = &mastertemplate;
1605 dtp = (struct dirtemplate *)&omastertemplate;
1607 dirtemplate.dot_ino = ip->i_number;
1608 dirtemplate.dotdot_ino = dp->i_number;
1609 if ((error = UFS_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred,
1610 BA_CLRBUF, &bp)) != 0)
1612 ip->i_size = DIRBLKSIZ;
1613 DIP_SET(ip, i_size, DIRBLKSIZ);
1614 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1615 vnode_pager_setsize(tvp, (u_long)ip->i_size);
1616 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1617 if (DOINGSOFTDEP(tvp)) {
1619 * Ensure that the entire newly allocated block is a
1620 * valid directory so that future growth within the
1621 * block does not have to ensure that the block is
1622 * written before the inode.
1625 while (blkoff < bp->b_bcount) {
1627 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1628 blkoff += DIRBLKSIZ;
1631 if ((error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) |
1632 DOINGASYNC(tvp)))) != 0) {
1637 * Directory set up, now install its entry in the parent directory.
1639 * If we are not doing soft dependencies, then we must write out the
1640 * buffer containing the new directory body before entering the new
1641 * name in the parent. If we are doing soft dependencies, then the
1642 * buffer containing the new directory body will be passed to and
1643 * released in the soft dependency code after the code has attached
1644 * an appropriate ordering dependency to the buffer which ensures that
1645 * the buffer is written before the new name is written in the parent.
1647 if (DOINGASYNC(dvp))
1649 else if (!DOINGSOFTDEP(dvp) && ((error = bwrite(bp))))
1651 ufs_makedirentry(ip, cnp, &newdir);
1652 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1660 uma_zfree(acl_zone, acl);
1662 uma_zfree(acl_zone, dacl);
1666 DIP_SET(dp, i_nlink, dp->i_nlink);
1667 dp->i_flag |= IN_CHANGE;
1668 if (DOINGSOFTDEP(dvp))
1669 softdep_change_linkcnt(dp);
1671 * No need to do an explicit VOP_TRUNCATE here, vrele will
1672 * do this for us because we set the link count to 0.
1676 DIP_SET(ip, i_nlink, 0);
1677 ip->i_flag |= IN_CHANGE;
1678 if (DOINGSOFTDEP(tvp))
1679 softdep_change_linkcnt(ip);
1687 * Rmdir system call.
1691 struct vop_rmdir_args /* {
1692 struct vnode *a_dvp;
1694 struct componentname *a_cnp;
1697 struct vnode *vp = ap->a_vp;
1698 struct vnode *dvp = ap->a_dvp;
1699 struct componentname *cnp = ap->a_cnp;
1700 struct inode *ip, *dp;
1707 * Do not remove a directory that is in the process of being renamed.
1708 * Verify the directory is empty (and valid). Rmdir ".." will not be
1709 * valid since ".." will contain a reference to the current directory
1710 * and thus be non-empty. Do not allow the removal of mounted on
1711 * directories (this can happen when an NFS exported filesystem
1712 * tries to remove a locally mounted on directory).
1715 if ((ip->i_flag & IN_RENAME) || ip->i_effnlink < 2) {
1719 if (!ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1723 if ((dp->i_flags & APPEND)
1724 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1728 if (vp->v_mountedhere != 0) {
1733 ufs_gjournal_orphan(vp);
1736 * Delete reference to directory before purging
1737 * inode. If we crash in between, the directory
1738 * will be reattached to lost+found,
1742 if (DOINGSOFTDEP(vp)) {
1743 softdep_change_linkcnt(dp);
1744 softdep_change_linkcnt(ip);
1746 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1750 if (DOINGSOFTDEP(vp)) {
1751 softdep_change_linkcnt(dp);
1752 softdep_change_linkcnt(ip);
1758 * Truncate inode. The only stuff left in the directory is "." and
1759 * "..". The "." reference is inconsequential since we are quashing
1760 * it. The soft dependency code will arrange to do these operations
1761 * after the parent directory entry has been deleted on disk, so
1762 * when running with that code we avoid doing them now.
1764 if (!DOINGSOFTDEP(vp)) {
1766 DIP_SET(dp, i_nlink, dp->i_nlink);
1767 dp->i_flag |= IN_CHANGE;
1769 DIP_SET(ip, i_nlink, ip->i_nlink);
1770 ip->i_flag |= IN_CHANGE;
1772 if (!DOINGASYNC(vp))
1774 error = UFS_TRUNCATE(vp, (off_t)0, ioflag, cnp->cn_cred,
1779 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1780 if (ip->i_dirhash != NULL)
1781 ufsdirhash_free(ip);
1788 * symlink -- make a symbolic link
1792 struct vop_symlink_args /* {
1793 struct vnode *a_dvp;
1794 struct vnode **a_vpp;
1795 struct componentname *a_cnp;
1796 struct vattr *a_vap;
1800 struct vnode *vp, **vpp = ap->a_vpp;
1804 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1809 len = strlen(ap->a_target);
1810 if (len < vp->v_mount->mnt_maxsymlinklen) {
1812 bcopy(ap->a_target, SHORTLINK(ip), len);
1814 DIP_SET(ip, i_size, len);
1815 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1817 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1818 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1819 ap->a_cnp->cn_cred, NOCRED, (int *)0, (struct thread *)0);
1826 * Vnode op for reading directories.
1828 * The routine below assumes that the on-disk format of a directory
1829 * is the same as that defined by <sys/dirent.h>. If the on-disk
1830 * format changes, then it will be necessary to do a conversion
1831 * from the on-disk format that read returns to the format defined
1832 * by <sys/dirent.h>.
1836 struct vop_readdir_args /* {
1839 struct ucred *a_cred;
1845 struct uio *uio = ap->a_uio;
1850 if (ap->a_ncookies != NULL)
1852 * Ensure that the block is aligned. The caller can use
1853 * the cookies to determine where in the block to start.
1855 uio->uio_offset &= ~(DIRBLKSIZ - 1);
1856 off = uio->uio_offset;
1857 count = uio->uio_resid;
1858 /* Make sure we don't return partial entries. */
1859 if (count <= ((uio->uio_offset + count) & (DIRBLKSIZ -1)))
1861 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
1862 lost = uio->uio_resid - count;
1863 uio->uio_resid = count;
1864 uio->uio_iov->iov_len = count;
1865 # if (BYTE_ORDER == LITTLE_ENDIAN)
1866 if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) {
1867 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1869 struct dirent *dp, *edp;
1877 auio.uio_iov = &aiov;
1878 auio.uio_iovcnt = 1;
1879 auio.uio_segflg = UIO_SYSSPACE;
1880 aiov.iov_len = count;
1881 MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
1882 aiov.iov_base = dirbuf;
1883 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
1885 readcnt = count - auio.uio_resid;
1886 edp = (struct dirent *)&dirbuf[readcnt];
1887 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
1889 dp->d_namlen = dp->d_type;
1891 if (dp->d_reclen > 0) {
1892 dp = (struct dirent *)
1893 ((char *)dp + dp->d_reclen);
1900 error = uiomove(dirbuf, readcnt, uio);
1902 FREE(dirbuf, M_TEMP);
1905 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1907 if (!error && ap->a_ncookies != NULL) {
1908 struct dirent* dpStart;
1909 struct dirent* dpEnd;
1915 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1916 panic("ufs_readdir: unexpected uio from NFS server");
1917 dpStart = (struct dirent *)
1918 ((char *)uio->uio_iov->iov_base - (uio->uio_offset - off));
1919 dpEnd = (struct dirent *) uio->uio_iov->iov_base;
1920 for (dp = dpStart, ncookies = 0;
1922 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen))
1924 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1926 for (dp = dpStart, cookiep = cookies;
1928 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
1929 off += dp->d_reclen;
1930 *cookiep++ = (u_long) off;
1932 *ap->a_ncookies = ncookies;
1933 *ap->a_cookies = cookies;
1935 uio->uio_resid += lost;
1937 *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
1942 * Return target name of a symbolic link
1946 struct vop_readlink_args /* {
1949 struct ucred *a_cred;
1952 struct vnode *vp = ap->a_vp;
1953 struct inode *ip = VTOI(vp);
1957 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1958 DIP(ip, i_blocks) == 0) { /* XXX - for old fastlink support */
1959 return (uiomove(SHORTLINK(ip), isize, ap->a_uio));
1961 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1965 * Calculate the logical to physical mapping if not done already,
1966 * then call the device strategy routine.
1968 * In order to be able to swap to a file, the ufs_bmaparray() operation may not
1969 * deadlock on memory. See ufs_bmap() for details.
1973 struct vop_strategy_args /* {
1978 struct buf *bp = ap->a_bp;
1979 struct vnode *vp = ap->a_vp;
1986 if (bp->b_blkno == bp->b_lblkno) {
1987 error = ufs_bmaparray(vp, bp->b_lblkno, &blkno, bp, NULL, NULL);
1988 bp->b_blkno = blkno;
1990 bp->b_error = error;
1991 bp->b_ioflags |= BIO_ERROR;
1995 if ((long)bp->b_blkno == -1)
1998 if ((long)bp->b_blkno == -1) {
2002 bp->b_iooffset = dbtob(bp->b_blkno);
2003 bo = ip->i_umbufobj;
2004 BO_STRATEGY(bo, bp);
2009 * Print out the contents of an inode.
2013 struct vop_print_args /* {
2017 struct vnode *vp = ap->a_vp;
2018 struct inode *ip = VTOI(vp);
2020 printf("\tino %lu, on dev %s", (u_long)ip->i_number,
2021 devtoname(ip->i_dev));
2022 if (vp->v_type == VFIFO)
2029 * Close wrapper for fifos.
2031 * Update the times on the inode then do device close.
2035 struct vop_close_args /* {
2038 struct ucred *a_cred;
2039 struct thread *a_td;
2042 struct vnode *vp = ap->a_vp;
2046 usecount = vp->v_usecount;
2048 ufs_itimes_locked(vp);
2050 return (fifo_specops.vop_close(ap));
2054 * Kqfilter wrapper for fifos.
2056 * Fall through to ufs kqfilter routines if needed
2059 ufsfifo_kqfilter(ap)
2060 struct vop_kqfilter_args *ap;
2064 error = fifo_specops.vop_kqfilter(ap);
2066 error = vfs_kqfilter(ap);
2071 * Return POSIX pathconf information applicable to ufs filesystems.
2075 struct vop_pathconf_args /* {
2084 switch (ap->a_name) {
2086 *ap->a_retval = LINK_MAX;
2089 *ap->a_retval = NAME_MAX;
2092 *ap->a_retval = PATH_MAX;
2095 *ap->a_retval = PIPE_BUF;
2097 case _PC_CHOWN_RESTRICTED:
2103 case _PC_ACL_EXTENDED:
2105 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
2113 case _PC_ACL_PATH_MAX:
2115 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
2116 *ap->a_retval = ACL_MAX_ENTRIES;
2123 case _PC_MAC_PRESENT:
2125 if (ap->a_vp->v_mount->mnt_flag & MNT_MULTILABEL)
2134 /* _PC_ASYNC_IO should have been handled by upper layers. */
2135 KASSERT(0, ("_PC_ASYNC_IO should not get here"));
2144 case _PC_ALLOC_SIZE_MIN:
2145 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
2147 case _PC_FILESIZEBITS:
2150 case _PC_REC_INCR_XFER_SIZE:
2151 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2153 case _PC_REC_MAX_XFER_SIZE:
2154 *ap->a_retval = -1; /* means ``unlimited'' */
2156 case _PC_REC_MIN_XFER_SIZE:
2157 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2159 case _PC_REC_XFER_ALIGN:
2160 *ap->a_retval = PAGE_SIZE;
2162 case _PC_SYMLINK_MAX:
2163 *ap->a_retval = MAXPATHLEN;
2174 * Advisory record locking support
2178 struct vop_advlock_args /* {
2186 struct inode *ip = VTOI(ap->a_vp);
2188 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
2192 * Initialize the vnode associated with a new inode, handle aliased
2196 ufs_vinit(mntp, fifoops, vpp)
2198 struct vop_vector *fifoops;
2206 vp->v_type = IFTOVT(ip->i_mode);
2207 if (vp->v_type == VFIFO)
2209 ASSERT_VOP_LOCKED(vp, "ufs_vinit");
2210 if (ip->i_number == ROOTINO)
2211 vp->v_vflag |= VV_ROOT;
2212 ip->i_modrev = init_va_filerev();
2218 * Allocate a new inode.
2219 * Vnode dvp must be locked.
2222 ufs_makeinode(mode, dvp, vpp, cnp)
2226 struct componentname *cnp;
2228 struct inode *ip, *pdir;
2229 struct direct newdir;
2238 if ((cnp->cn_flags & HASBUF) == 0)
2239 panic("ufs_makeinode: no name");
2242 if ((mode & IFMT) == 0)
2245 error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2249 ip->i_gid = pdir->i_gid;
2250 DIP_SET(ip, i_gid, pdir->i_gid);
2254 struct ucred ucred, *ucp;
2258 * If we are not the owner of the directory,
2259 * and we are hacking owners here, (only do this where told to)
2260 * and we are not giving it TO root, (would subvert quotas)
2261 * then go ahead and give it to the other user.
2262 * Note that this drops off the execute bits for security.
2264 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2265 (pdir->i_mode & ISUID) &&
2266 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2267 ip->i_uid = pdir->i_uid;
2268 DIP_SET(ip, i_uid, ip->i_uid);
2272 * Make sure the correct user gets charged
2274 * Quickly knock up a dummy credential for the victim.
2275 * XXX This seems to never be accessed out of our
2276 * context so a stack variable is ok.
2278 refcount_init(&ucred.cr_ref, 1);
2279 ucred.cr_uid = ip->i_uid;
2280 ucred.cr_ngroups = 1;
2281 ucred.cr_groups[0] = pdir->i_gid;
2285 ip->i_uid = cnp->cn_cred->cr_uid;
2286 DIP_SET(ip, i_uid, ip->i_uid);
2290 if ((error = getinoquota(ip)) ||
2291 (error = chkiq(ip, 1, ucp, 0))) {
2292 UFS_VFREE(tvp, ip->i_number, mode);
2298 #else /* !SUIDDIR */
2299 ip->i_uid = cnp->cn_cred->cr_uid;
2300 DIP_SET(ip, i_uid, ip->i_uid);
2302 if ((error = getinoquota(ip)) ||
2303 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2304 UFS_VFREE(tvp, ip->i_number, mode);
2309 #endif /* !SUIDDIR */
2310 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2313 if ((dvp->v_mount->mnt_flag & MNT_ACLS) != 0) {
2314 acl = uma_zalloc(acl_zone, M_WAITOK);
2317 * Retrieve default ACL for parent, if any.
2319 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cnp->cn_cred,
2324 * Retrieved a default ACL, so merge mode and ACL if
2327 if (acl->acl_cnt != 0) {
2329 * Two possible ways for default ACL to not
2330 * be present. First, the EA can be
2331 * undefined, or second, the default ACL can
2332 * be blank. If it's blank, fall through to
2333 * the it's not defined case.
2335 mode = acl_posix1e_newfilemode(mode, acl);
2337 DIP_SET(ip, i_mode, mode);
2338 ufs_sync_acl_from_inode(ip, acl);
2345 * Just use the mode as-is.
2348 DIP_SET(ip, i_mode, mode);
2349 uma_zfree(acl_zone, acl);
2354 UFS_VFREE(tvp, ip->i_number, mode);
2356 uma_zfree(acl_zone, acl);
2363 DIP_SET(ip, i_mode, mode);
2367 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2370 DIP_SET(ip, i_nlink, 1);
2371 if (DOINGSOFTDEP(tvp))
2372 softdep_change_linkcnt(ip);
2373 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2374 priv_check_cred(cnp->cn_cred, PRIV_VFS_SETGID,
2376 ip->i_mode &= ~ISGID;
2377 DIP_SET(ip, i_mode, ip->i_mode);
2380 if (cnp->cn_flags & ISWHITEOUT) {
2381 ip->i_flags |= UF_OPAQUE;
2382 DIP_SET(ip, i_flags, ip->i_flags);
2386 * Make sure inode goes to disk before directory entry.
2388 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2392 if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
2393 error = mac_create_vnode_extattr(cnp->cn_cred, dvp->v_mount,
2402 * XXX: If we abort now, will Soft Updates notify the extattr
2403 * code that the EAs for the file need to be released?
2405 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cnp->cn_cred,
2413 * XXX: This should not happen, as EOPNOTSUPP above was
2414 * supposed to free acl.
2416 printf("ufs_makeinode: VOP_GETACL() but no "
2418 /* panic("ufs_makeinode: VOP_GETACL() but no "
2423 uma_zfree(acl_zone, acl);
2426 uma_zfree(acl_zone, acl);
2428 #endif /* !UFS_ACL */
2429 ufs_makedirentry(ip, cnp, &newdir);
2430 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2438 * Write error occurred trying to update the inode
2439 * or the directory so must deallocate the inode.
2443 DIP_SET(ip, i_nlink, 0);
2444 ip->i_flag |= IN_CHANGE;
2445 if (DOINGSOFTDEP(tvp))
2446 softdep_change_linkcnt(ip);
2451 /* Global vfs data structures for ufs. */
2452 struct vop_vector ufs_vnodeops = {
2453 .vop_default = &default_vnodeops,
2454 .vop_fsync = VOP_PANIC,
2455 .vop_read = VOP_PANIC,
2456 .vop_reallocblks = VOP_PANIC,
2457 .vop_write = VOP_PANIC,
2458 .vop_access = ufs_access,
2459 .vop_advlock = ufs_advlock,
2460 .vop_bmap = ufs_bmap,
2461 .vop_cachedlookup = ufs_lookup,
2462 .vop_close = ufs_close,
2463 .vop_create = ufs_create,
2464 .vop_getattr = ufs_getattr,
2465 .vop_inactive = ufs_inactive,
2466 .vop_link = ufs_link,
2467 .vop_lookup = vfs_cache_lookup,
2468 .vop_mkdir = ufs_mkdir,
2469 .vop_mknod = ufs_mknod,
2470 .vop_open = ufs_open,
2471 .vop_pathconf = ufs_pathconf,
2472 .vop_poll = vop_stdpoll,
2473 .vop_print = ufs_print,
2474 .vop_readdir = ufs_readdir,
2475 .vop_readlink = ufs_readlink,
2476 .vop_reclaim = ufs_reclaim,
2477 .vop_remove = ufs_remove,
2478 .vop_rename = ufs_rename,
2479 .vop_rmdir = ufs_rmdir,
2480 .vop_setattr = ufs_setattr,
2482 .vop_setlabel = vop_stdsetlabel_ea,
2484 .vop_strategy = ufs_strategy,
2485 .vop_symlink = ufs_symlink,
2486 .vop_whiteout = ufs_whiteout,
2488 .vop_getextattr = ufs_getextattr,
2489 .vop_deleteextattr = ufs_deleteextattr,
2490 .vop_setextattr = ufs_setextattr,
2493 .vop_getacl = ufs_getacl,
2494 .vop_setacl = ufs_setacl,
2495 .vop_aclcheck = ufs_aclcheck,
2499 struct vop_vector ufs_fifoops = {
2500 .vop_default = &fifo_specops,
2501 .vop_fsync = VOP_PANIC,
2502 .vop_access = ufs_access,
2503 .vop_close = ufsfifo_close,
2504 .vop_getattr = ufs_getattr,
2505 .vop_inactive = ufs_inactive,
2506 .vop_kqfilter = ufsfifo_kqfilter,
2507 .vop_print = ufs_print,
2508 .vop_read = VOP_PANIC,
2509 .vop_reclaim = ufs_reclaim,
2510 .vop_setattr = ufs_setattr,
2512 .vop_setlabel = vop_stdsetlabel_ea,
2514 .vop_write = VOP_PANIC,
2516 .vop_getextattr = ufs_getextattr,
2517 .vop_deleteextattr = ufs_deleteextattr,
2518 .vop_setextattr = ufs_setextattr,
2521 .vop_getacl = ufs_getacl,
2522 .vop_setacl = ufs_setacl,
2523 .vop_aclcheck = ufs_aclcheck,