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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
39 * $FreeBSD: src/sys/ufs/ufs/ufs_vnops.c,v 1.131.2.8 2003/01/02 17:26:19 bde Exp $
40 * $DragonFly: src/sys/vfs/ufs/ufs_vnops.c,v 1.10 2003/09/23 05:03:53 dillon Exp $
43 #include "opt_quota.h"
44 #include "opt_suiddir.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/fcntl.h>
54 #include <sys/namei.h>
55 #include <sys/mount.h>
56 #include <sys/unistd.h>
57 #include <sys/vnode.h>
58 #include <sys/malloc.h>
59 #include <sys/dirent.h>
60 #include <sys/lockf.h>
61 #include <sys/event.h>
64 #include <sys/file.h> /* XXX */
67 #include <vm/vm_extern.h>
69 #include <vfs/fifofs/fifo.h>
75 #include "ufs_extern.h"
80 static int ufs_access (struct vop_access_args *);
81 static int ufs_advlock (struct vop_advlock_args *);
82 static int ufs_chmod (struct vnode *, int, struct ucred *, struct thread *);
83 static int ufs_chown (struct vnode *, uid_t, gid_t, struct ucred *, struct thread *);
84 static int ufs_close (struct vop_close_args *);
85 static int ufs_create (struct vop_create_args *);
86 static int ufs_getattr (struct vop_getattr_args *);
87 static int ufs_link (struct vop_link_args *);
88 static int ufs_makeinode (int mode, struct vnode *, struct vnode **, struct componentname *);
89 static int ufs_missingop (struct vop_generic_args *ap);
90 static int ufs_mkdir (struct vop_mkdir_args *);
91 static int ufs_mknod (struct vop_mknod_args *);
92 static int ufs_mmap (struct vop_mmap_args *);
93 static int ufs_open (struct vop_open_args *);
94 static int ufs_pathconf (struct vop_pathconf_args *);
95 static int ufs_print (struct vop_print_args *);
96 static int ufs_readdir (struct vop_readdir_args *);
97 static int ufs_readlink (struct vop_readlink_args *);
98 static int ufs_remove (struct vop_remove_args *);
99 static int ufs_rename (struct vop_rename_args *);
100 static int ufs_rmdir (struct vop_rmdir_args *);
101 static int ufs_setattr (struct vop_setattr_args *);
102 static int ufs_strategy (struct vop_strategy_args *);
103 static int ufs_symlink (struct vop_symlink_args *);
104 static int ufs_whiteout (struct vop_whiteout_args *);
105 static int ufsfifo_close (struct vop_close_args *);
106 static int ufsfifo_kqfilter (struct vop_kqfilter_args *);
107 static int ufsfifo_read (struct vop_read_args *);
108 static int ufsfifo_write (struct vop_write_args *);
109 static int ufsspec_close (struct vop_close_args *);
110 static int ufsspec_read (struct vop_read_args *);
111 static int ufsspec_write (struct vop_write_args *);
112 static int filt_ufsread (struct knote *kn, long hint);
113 static int filt_ufswrite (struct knote *kn, long hint);
114 static int filt_ufsvnode (struct knote *kn, long hint);
115 static void filt_ufsdetach (struct knote *kn);
116 static int ufs_kqfilter (struct vop_kqfilter_args *ap);
122 #define SETHIGH(q, h) { \
125 tmp.val[_QUAD_HIGHWORD] = (h); \
128 #define SETLOW(q, l) { \
131 tmp.val[_QUAD_LOWWORD] = (l); \
134 #define VN_KNOTE(vp, b) \
135 KNOTE(&vp->v_pollinfo.vpi_selinfo.si_note, (b))
138 * A virgin directory (no blushing please).
140 static struct dirtemplate mastertemplate = {
141 0, 12, DT_DIR, 1, ".",
142 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
144 static struct odirtemplate omastertemplate = {
146 0, DIRBLKSIZ - 12, 2, ".."
157 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
159 if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
160 ip->i_flag |= IN_LAZYMOD;
162 ip->i_flag |= IN_MODIFIED;
163 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
165 if (ip->i_flag & IN_ACCESS) {
166 ip->i_atime = ts.tv_sec;
167 ip->i_atimensec = ts.tv_nsec;
169 if (ip->i_flag & IN_UPDATE) {
170 ip->i_mtime = ts.tv_sec;
171 ip->i_mtimensec = ts.tv_nsec;
174 if (ip->i_flag & IN_CHANGE) {
175 ip->i_ctime = ts.tv_sec;
176 ip->i_ctimensec = ts.tv_nsec;
179 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
183 * Create a regular file
187 struct vop_create_args /* {
189 struct vnode **a_vpp;
190 struct componentname *a_cnp;
197 ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
198 ap->a_dvp, ap->a_vpp, ap->a_cnp);
201 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
211 struct vop_mknod_args /* {
213 struct vnode **a_vpp;
214 struct componentname *a_cnp;
218 struct vattr *vap = ap->a_vap;
219 struct vnode **vpp = ap->a_vpp;
224 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
225 ap->a_dvp, vpp, ap->a_cnp);
228 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
230 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
231 if (vap->va_rdev != VNOVAL) {
233 * Want to be able to use this to make badblock
234 * inodes, so don't truncate the dev number.
236 ip->i_rdev = vap->va_rdev;
239 * Remove inode, then reload it through VFS_VGET so it is
240 * checked to see if it is an alias of an existing entry in
244 (*vpp)->v_type = VNON;
245 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
247 error = VFS_VGET(ap->a_dvp->v_mount, ino, vpp);
263 struct vop_open_args /* {
266 struct ucred *a_cred;
272 * Files marked append-only must be opened for appending.
274 if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
275 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
283 * Update the times on the inode.
288 struct vop_close_args /* {
291 struct ucred *a_cred;
295 struct vnode *vp = ap->a_vp;
297 lwkt_gettoken(&vp->v_interlock);
298 if (vp->v_usecount > 1)
300 lwkt_reltoken(&vp->v_interlock);
306 struct vop_access_args /* {
309 struct ucred *a_cred;
313 struct vnode *vp = ap->a_vp;
314 struct inode *ip = VTOI(vp);
315 struct ucred *cred = ap->a_cred;
316 mode_t mask, mode = ap->a_mode;
324 * Disallow write attempts on read-only file systems;
325 * unless the file is a socket, fifo, or a block or
326 * character device resident on the file system.
329 switch (vp->v_type) {
333 if (vp->v_mount->mnt_flag & MNT_RDONLY)
336 if ((error = getinoquota(ip)) != 0)
345 /* If immutable bit set, nobody gets to write it. */
346 if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE))
349 /* Otherwise, user id 0 always gets access. */
350 if (cred->cr_uid == 0)
355 /* Otherwise, check the owner. */
356 if (cred->cr_uid == ip->i_uid) {
363 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
366 /* Otherwise, check the groups. */
367 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
368 if (ip->i_gid == *gp) {
375 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
378 /* Otherwise, check everyone else. */
385 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
391 struct vop_getattr_args /* {
397 struct vnode *vp = ap->a_vp;
398 struct inode *ip = VTOI(vp);
399 struct vattr *vap = ap->a_vap;
403 * Copy from inode table
405 vap->va_fsid = dev2udev(ip->i_dev);
406 vap->va_fileid = ip->i_number;
407 vap->va_mode = ip->i_mode & ~IFMT;
408 vap->va_nlink = VFSTOUFS(vp->v_mount)->um_i_effnlink_valid ?
409 ip->i_effnlink : ip->i_nlink;
410 vap->va_uid = ip->i_uid;
411 vap->va_gid = ip->i_gid;
412 vap->va_rdev = ip->i_rdev;
413 vap->va_size = ip->i_din.di_size;
414 vap->va_atime.tv_sec = ip->i_atime;
415 vap->va_atime.tv_nsec = ip->i_atimensec;
416 vap->va_mtime.tv_sec = ip->i_mtime;
417 vap->va_mtime.tv_nsec = ip->i_mtimensec;
418 vap->va_ctime.tv_sec = ip->i_ctime;
419 vap->va_ctime.tv_nsec = ip->i_ctimensec;
420 vap->va_flags = ip->i_flags;
421 vap->va_gen = ip->i_gen;
422 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
423 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
424 vap->va_type = IFTOVT(ip->i_mode);
425 vap->va_filerev = ip->i_modrev;
430 * Set attribute vnode op. called from several syscalls
434 struct vop_setattr_args /* {
437 struct ucred *a_cred;
441 struct vattr *vap = ap->a_vap;
442 struct vnode *vp = ap->a_vp;
443 struct inode *ip = VTOI(vp);
444 struct ucred *cred = ap->a_cred;
448 * Check for unsettable attributes.
450 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
451 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
452 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
453 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
456 if (vap->va_flags != VNOVAL) {
457 if (vp->v_mount->mnt_flag & MNT_RDONLY)
459 if (cred->cr_uid != ip->i_uid &&
460 (error = suser_cred(cred, PRISON_ROOT)))
462 if ((cred->cr_uid == 0) && (cred->cr_prison == NULL)) {
464 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) &&
467 ip->i_flags = vap->va_flags;
470 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
471 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
473 ip->i_flags &= SF_SETTABLE;
474 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
476 ip->i_flag |= IN_CHANGE;
477 if (vap->va_flags & (IMMUTABLE | APPEND))
480 if (ip->i_flags & (IMMUTABLE | APPEND))
483 * Go through the fields and update iff not VNOVAL.
485 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
486 if (vp->v_mount->mnt_flag & MNT_RDONLY)
488 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred, ap->a_td)) != 0)
491 if (vap->va_size != VNOVAL) {
493 * Disallow write attempts on read-only file systems;
494 * unless the file is a socket, fifo, or a block or
495 * character device resident on the file system.
497 switch (vp->v_type) {
502 if (vp->v_mount->mnt_flag & MNT_RDONLY)
508 if ((error = UFS_TRUNCATE(vp, vap->va_size, 0, cred, ap->a_td)) != 0)
512 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
513 if (vp->v_mount->mnt_flag & MNT_RDONLY)
515 if (cred->cr_uid != ip->i_uid &&
516 (error = suser_cred(cred, PRISON_ROOT)) &&
517 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
518 (error = VOP_ACCESS(vp, VWRITE, cred, ap->a_td))))
520 if (vap->va_atime.tv_sec != VNOVAL)
521 ip->i_flag |= IN_ACCESS;
522 if (vap->va_mtime.tv_sec != VNOVAL)
523 ip->i_flag |= IN_CHANGE | IN_UPDATE;
525 if (vap->va_atime.tv_sec != VNOVAL) {
526 ip->i_atime = vap->va_atime.tv_sec;
527 ip->i_atimensec = vap->va_atime.tv_nsec;
529 if (vap->va_mtime.tv_sec != VNOVAL) {
530 ip->i_mtime = vap->va_mtime.tv_sec;
531 ip->i_mtimensec = vap->va_mtime.tv_nsec;
533 error = UFS_UPDATE(vp, 0);
538 if (vap->va_mode != (mode_t)VNOVAL) {
539 if (vp->v_mount->mnt_flag & MNT_RDONLY)
541 error = ufs_chmod(vp, (int)vap->va_mode, cred, ap->a_td);
543 VN_KNOTE(vp, NOTE_ATTRIB);
548 * Change the mode on a file.
549 * Inode must be locked before calling.
552 ufs_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td)
554 struct inode *ip = VTOI(vp);
557 if (cred->cr_uid != ip->i_uid) {
558 error = suser_cred(cred, PRISON_ROOT);
563 if (vp->v_type != VDIR && (mode & S_ISTXT))
565 if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
568 ip->i_mode &= ~ALLPERMS;
569 ip->i_mode |= (mode & ALLPERMS);
570 ip->i_flag |= IN_CHANGE;
575 * Perform chown operation on inode ip;
576 * inode must be locked prior to call.
579 ufs_chown(vp, uid, gid, cred, td)
586 struct inode *ip = VTOI(vp);
595 if (uid == (uid_t)VNOVAL)
597 if (gid == (gid_t)VNOVAL)
600 * If we don't own the file, are trying to change the owner
601 * of the file, or are not a member of the target group,
602 * the caller must be superuser or the call fails.
604 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
605 (gid != ip->i_gid && !groupmember((gid_t)gid, cred))) &&
606 (error = suser_cred(cred, PRISON_ROOT)))
611 if ((error = getinoquota(ip)) != 0)
614 dqrele(vp, ip->i_dquot[USRQUOTA]);
615 ip->i_dquot[USRQUOTA] = NODQUOT;
618 dqrele(vp, ip->i_dquot[GRPQUOTA]);
619 ip->i_dquot[GRPQUOTA] = NODQUOT;
621 change = ip->i_blocks;
622 (void) chkdq(ip, -change, cred, CHOWN);
623 (void) chkiq(ip, -1, cred, CHOWN);
624 for (i = 0; i < MAXQUOTAS; i++) {
625 dqrele(vp, ip->i_dquot[i]);
626 ip->i_dquot[i] = NODQUOT;
632 if ((error = getinoquota(ip)) == 0) {
634 dqrele(vp, ip->i_dquot[USRQUOTA]);
635 ip->i_dquot[USRQUOTA] = NODQUOT;
638 dqrele(vp, ip->i_dquot[GRPQUOTA]);
639 ip->i_dquot[GRPQUOTA] = NODQUOT;
641 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
642 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
645 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
647 for (i = 0; i < MAXQUOTAS; i++) {
648 dqrele(vp, ip->i_dquot[i]);
649 ip->i_dquot[i] = NODQUOT;
654 if (getinoquota(ip) == 0) {
656 dqrele(vp, ip->i_dquot[USRQUOTA]);
657 ip->i_dquot[USRQUOTA] = NODQUOT;
660 dqrele(vp, ip->i_dquot[GRPQUOTA]);
661 ip->i_dquot[GRPQUOTA] = NODQUOT;
663 (void) chkdq(ip, change, cred, FORCE|CHOWN);
664 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
665 (void) getinoquota(ip);
670 panic("ufs_chown: lost quota");
672 ip->i_flag |= IN_CHANGE;
673 if (cred->cr_uid != 0 && (ouid != uid || ogid != gid))
674 ip->i_mode &= ~(ISUID | ISGID);
681 * NB Currently unsupported.
686 struct vop_mmap_args /* {
689 struct ucred *a_cred;
699 struct vop_remove_args /* {
702 struct componentname *a_cnp;
706 struct vnode *vp = ap->a_vp;
707 struct vnode *dvp = ap->a_dvp;
711 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
712 (VTOI(dvp)->i_flags & APPEND)) {
716 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
717 VN_KNOTE(vp, NOTE_DELETE);
718 VN_KNOTE(dvp, NOTE_WRITE);
728 struct vop_link_args /* {
729 struct vnode *a_tdvp;
731 struct componentname *a_cnp;
734 struct vnode *vp = ap->a_vp;
735 struct vnode *tdvp = ap->a_tdvp;
736 struct componentname *cnp = ap->a_cnp;
737 struct thread *td = cnp->cn_td;
739 struct direct newdir;
743 if ((cnp->cn_flags & CNP_HASBUF) == 0)
744 panic("ufs_link: no name");
746 if (tdvp->v_mount != vp->v_mount) {
750 if (tdvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE, td))) {
754 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
758 if (ip->i_flags & (IMMUTABLE | APPEND)) {
764 ip->i_flag |= IN_CHANGE;
765 if (DOINGSOFTDEP(vp))
766 softdep_change_linkcnt(ip);
767 error = UFS_UPDATE(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
769 ufs_makedirentry(ip, cnp, &newdir);
770 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
776 ip->i_flag |= IN_CHANGE;
777 if (DOINGSOFTDEP(vp))
778 softdep_change_linkcnt(ip);
782 VOP_UNLOCK(vp, 0, td);
784 VN_KNOTE(vp, NOTE_LINK);
785 VN_KNOTE(tdvp, NOTE_WRITE);
790 * whiteout vnode call
794 struct vop_whiteout_args /* {
796 struct componentname *a_cnp;
800 struct vnode *dvp = ap->a_dvp;
801 struct componentname *cnp = ap->a_cnp;
802 struct direct newdir;
805 switch (ap->a_flags) {
807 /* 4.4 format directories support whiteout operations */
808 if (dvp->v_mount->mnt_maxsymlinklen > 0)
813 /* create a new directory whiteout */
815 if ((cnp->cn_flags & CNP_SAVENAME) == 0)
816 panic("ufs_whiteout: missing name");
817 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
818 panic("ufs_whiteout: old format filesystem");
822 newdir.d_namlen = cnp->cn_namelen;
823 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
824 newdir.d_type = DT_WHT;
825 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
829 /* remove an existing directory whiteout */
831 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
832 panic("ufs_whiteout: old format filesystem");
835 cnp->cn_flags &= ~CNP_DOWHITEOUT;
836 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
839 panic("ufs_whiteout: unknown op");
845 * Rename system call.
846 * rename("foo", "bar");
849 * link("foo", "bar");
851 * but ``atomically''. Can't do full commit without saving state in the
852 * inode on disk which isn't feasible at this time. Best we can do is
853 * always guarantee the target exists.
855 * Basic algorithm is:
857 * 1) Bump link count on source while we're linking it to the
858 * target. This also ensure the inode won't be deleted out
859 * from underneath us while we work (it may be truncated by
860 * a concurrent `trunc' or `open' for creation).
861 * 2) Link source to destination. If destination already exists,
863 * 3) Unlink source reference to inode if still around. If a
864 * directory was moved and the parent of the destination
865 * is different from the source, patch the ".." entry in the
870 struct vop_rename_args /* {
871 struct vnode *a_fdvp;
873 struct componentname *a_fcnp;
874 struct vnode *a_tdvp;
876 struct componentname *a_tcnp;
879 struct vnode *tvp = ap->a_tvp;
880 struct vnode *tdvp = ap->a_tdvp;
881 struct vnode *fvp = ap->a_fvp;
882 struct vnode *fdvp = ap->a_fdvp;
883 struct componentname *tcnp = ap->a_tcnp;
884 struct componentname *fcnp = ap->a_fcnp;
885 struct thread *td = fcnp->cn_td;
886 struct inode *ip, *xp, *dp;
887 struct direct newdir;
888 int doingdirectory = 0, oldparent = 0, newparent = 0;
889 int error = 0, ioflag;
892 if ((tcnp->cn_flags & CNP_HASBUF) == 0 ||
893 (fcnp->cn_flags & CNP_HASBUF) == 0)
894 panic("ufs_rename: no name");
897 * Check for cross-device rename.
899 if ((fvp->v_mount != tdvp->v_mount) ||
900 (tvp && (fvp->v_mount != tvp->v_mount))) {
914 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
915 (VTOI(tdvp)->i_flags & APPEND))) {
921 * Renaming a file to itself has no effect. The upper layers should
922 * not call us in that case. Temporarily just warn if they do.
925 printf("ufs_rename: fvp == tvp (can't happen)\n");
930 if ((error = vn_lock(fvp, LK_EXCLUSIVE, td)) != 0)
934 if (ip->i_nlink >= LINK_MAX) {
935 VOP_UNLOCK(fvp, 0, td);
939 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
940 || (dp->i_flags & APPEND)) {
941 VOP_UNLOCK(fvp, 0, td);
945 if ((ip->i_mode & IFMT) == IFDIR) {
947 * Avoid ".", "..", and aliases of "." for obvious reasons.
949 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
950 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & CNP_ISDOTDOT ||
951 (ip->i_flag & IN_RENAME)) {
952 VOP_UNLOCK(fvp, 0, td);
956 ip->i_flag |= IN_RENAME;
957 oldparent = dp->i_number;
960 VN_KNOTE(fdvp, NOTE_WRITE); /* XXX right place? */
964 * When the target exists, both the directory
965 * and target vnodes are returned locked.
973 * 1) Bump link count while we're moving stuff
974 * around. If we crash somewhere before
975 * completing our work, the link count
976 * may be wrong, but correctable.
980 ip->i_flag |= IN_CHANGE;
981 if (DOINGSOFTDEP(fvp))
982 softdep_change_linkcnt(ip);
983 if ((error = UFS_UPDATE(fvp, !(DOINGSOFTDEP(fvp) |
984 DOINGASYNC(fvp)))) != 0) {
985 VOP_UNLOCK(fvp, 0, td);
990 * If ".." must be changed (ie the directory gets a new
991 * parent) then the source directory must not be in the
992 * directory heirarchy above the target, as this would
993 * orphan everything below the source directory. Also
994 * the user must have write permission in the source so
995 * as to be able to change "..". We must repeat the call
996 * to namei, as the parent directory is unlocked by the
997 * call to checkpath().
999 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_td);
1000 VOP_UNLOCK(fvp, 0, td);
1001 if (oldparent != dp->i_number)
1002 newparent = dp->i_number;
1003 if (doingdirectory && newparent) {
1004 if (error) /* write access check above */
1008 error = ufs_checkpath(ip, dp, tcnp->cn_cred);
1011 if ((tcnp->cn_flags & CNP_SAVESTART) == 0)
1012 panic("ufs_rename: lost to startdir");
1014 error = relookup(tdvp, &tvp, tcnp);
1024 * 2) If target doesn't exist, link the target
1025 * to the source and unlink the source.
1026 * Otherwise, rewrite the target directory
1027 * entry to reference the source inode and
1028 * expunge the original entry's existence.
1031 if (dp->i_dev != ip->i_dev)
1032 panic("ufs_rename: EXDEV");
1034 * Account for ".." in new directory.
1035 * When source and destination have the same
1036 * parent we don't fool with the link count.
1038 if (doingdirectory && newparent) {
1039 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1045 dp->i_flag |= IN_CHANGE;
1046 if (DOINGSOFTDEP(tdvp))
1047 softdep_change_linkcnt(dp);
1048 error = UFS_UPDATE(tdvp, !(DOINGSOFTDEP(tdvp) |
1053 ufs_makedirentry(ip, tcnp, &newdir);
1054 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1056 if (doingdirectory && newparent) {
1059 dp->i_flag |= IN_CHANGE;
1060 if (DOINGSOFTDEP(tdvp))
1061 softdep_change_linkcnt(dp);
1062 (void)UFS_UPDATE(tdvp, 1);
1066 VN_KNOTE(tdvp, NOTE_WRITE);
1069 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1070 panic("ufs_rename: EXDEV");
1072 * Short circuit rename(foo, foo).
1074 if (xp->i_number == ip->i_number)
1075 panic("ufs_rename: same file");
1077 * If the parent directory is "sticky", then the user must
1078 * own the parent directory, or the destination of the rename,
1079 * otherwise the destination may not be changed (except by
1080 * root). This implements append-only directories.
1082 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
1083 tcnp->cn_cred->cr_uid != dp->i_uid &&
1084 xp->i_uid != tcnp->cn_cred->cr_uid) {
1089 * Target must be empty if a directory and have no links
1090 * to it. Also, ensure source and target are compatible
1091 * (both directories, or both not directories).
1093 if ((xp->i_mode&IFMT) == IFDIR) {
1094 if ((xp->i_effnlink > 2) ||
1095 !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1099 if (!doingdirectory) {
1104 } else if (doingdirectory) {
1108 error = ufs_dirrewrite(dp, xp, ip->i_number,
1110 (doingdirectory && newparent) ? newparent : doingdirectory);
1113 if (doingdirectory) {
1116 if (DOINGSOFTDEP(tdvp))
1117 softdep_change_linkcnt(dp);
1120 if (DOINGSOFTDEP(tvp))
1121 softdep_change_linkcnt(xp);
1123 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1125 * Truncate inode. The only stuff left in the directory
1126 * is "." and "..". The "." reference is inconsequential
1127 * since we are quashing it. We have removed the "."
1128 * reference and the reference in the parent directory,
1129 * but there may be other hard links. The soft
1130 * dependency code will arrange to do these operations
1131 * after the parent directory entry has been deleted on
1132 * disk, so when running with that code we avoid doing
1137 dp->i_flag |= IN_CHANGE;
1140 xp->i_flag |= IN_CHANGE;
1141 ioflag = DOINGASYNC(tvp) ? 0 : IO_SYNC;
1142 if ((error = UFS_TRUNCATE(tvp, (off_t)0, ioflag,
1143 tcnp->cn_cred, tcnp->cn_td)) != 0)
1146 VN_KNOTE(tdvp, NOTE_WRITE);
1148 VN_KNOTE(tvp, NOTE_DELETE);
1154 * 3) Unlink the source.
1156 fcnp->cn_flags &= ~CNP_MODMASK;
1157 fcnp->cn_flags |= CNP_LOCKPARENT | CNP_LOCKLEAF;
1158 if ((fcnp->cn_flags & CNP_SAVESTART) == 0)
1159 panic("ufs_rename: lost from startdir");
1161 error = relookup(fdvp, &fvp, fcnp);
1169 * From name has disappeared.
1172 panic("ufs_rename: lost dir entry");
1177 * Ensure that the directory entry still exists and has not
1178 * changed while the new name has been entered. If the source is
1179 * a file then the entry may have been unlinked or renamed. In
1180 * either case there is no further work to be done. If the source
1181 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1182 * flag ensures that it cannot be moved by another rename or removed
1187 panic("ufs_rename: lost dir entry");
1190 * If the source is a directory with a
1191 * new parent, the link count of the old
1192 * parent directory must be decremented
1193 * and ".." set to point to the new parent.
1195 if (doingdirectory && newparent) {
1196 xp->i_offset = mastertemplate.dot_reclen;
1197 ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1200 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1201 xp->i_flag &= ~IN_RENAME;
1203 VN_KNOTE(fvp, NOTE_RENAME);
1217 ip->i_flag &= ~IN_RENAME;
1218 if (vn_lock(fvp, LK_EXCLUSIVE, td) == 0) {
1221 ip->i_flag |= IN_CHANGE;
1222 ip->i_flag &= ~IN_RENAME;
1223 if (DOINGSOFTDEP(fvp))
1224 softdep_change_linkcnt(ip);
1236 struct vop_mkdir_args /* {
1237 struct vnode *a_dvp;
1238 struct vnode **a_vpp;
1239 struct componentname *a_cnp;
1240 struct vattr *a_vap;
1243 struct vnode *dvp = ap->a_dvp;
1244 struct vattr *vap = ap->a_vap;
1245 struct componentname *cnp = ap->a_cnp;
1246 struct inode *ip, *dp;
1249 struct dirtemplate dirtemplate, *dtp;
1250 struct direct newdir;
1255 if ((cnp->cn_flags & CNP_HASBUF) == 0)
1256 panic("ufs_mkdir: no name");
1259 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1263 dmode = vap->va_mode & 0777;
1266 * Must simulate part of ufs_makeinode here to acquire the inode,
1267 * but not have it entered in the parent directory. The entry is
1268 * made later after writing "." and ".." entries.
1270 error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
1274 ip->i_gid = dp->i_gid;
1278 struct ucred ucred, *ucp;
1282 * If we are hacking owners here, (only do this where told to)
1283 * and we are not giving it TO root, (would subvert quotas)
1284 * then go ahead and give it to the other user.
1285 * The new directory also inherits the SUID bit.
1286 * If user's UID and dir UID are the same,
1287 * 'give it away' so that the SUID is still forced on.
1289 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1290 (dp->i_mode & ISUID) && dp->i_uid) {
1292 ip->i_uid = dp->i_uid;
1294 if (dp->i_uid != cnp->cn_cred->cr_uid) {
1296 * Make sure the correct user gets charged
1298 * Make a dummy credential for the victim.
1299 * XXX This seems to never be accessed out of
1300 * our context so a stack variable is ok.
1303 ucred.cr_uid = ip->i_uid;
1304 ucred.cr_ngroups = 1;
1305 ucred.cr_groups[0] = dp->i_gid;
1310 ip->i_uid = cnp->cn_cred->cr_uid;
1312 if ((error = getinoquota(ip)) ||
1313 (error = chkiq(ip, 1, ucp, 0))) {
1314 UFS_VFREE(tvp, ip->i_number, dmode);
1320 #else /* !SUIDDIR */
1321 ip->i_uid = cnp->cn_cred->cr_uid;
1323 if ((error = getinoquota(ip)) ||
1324 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1325 UFS_VFREE(tvp, ip->i_number, dmode);
1330 #endif /* !SUIDDIR */
1331 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1333 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1336 if (DOINGSOFTDEP(tvp))
1337 softdep_change_linkcnt(ip);
1338 if (cnp->cn_flags & CNP_ISWHITEOUT)
1339 ip->i_flags |= UF_OPAQUE;
1342 * Bump link count in parent directory to reflect work done below.
1343 * Should be done before reference is created so cleanup is
1344 * possible if we crash.
1348 dp->i_flag |= IN_CHANGE;
1349 if (DOINGSOFTDEP(dvp))
1350 softdep_change_linkcnt(dp);
1351 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1356 * Initialize directory with "." and ".." from static template.
1358 if (dvp->v_mount->mnt_maxsymlinklen > 0
1360 dtp = &mastertemplate;
1362 dtp = (struct dirtemplate *)&omastertemplate;
1364 dirtemplate.dot_ino = ip->i_number;
1365 dirtemplate.dotdot_ino = dp->i_number;
1366 if ((error = VOP_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred,
1367 B_CLRBUF, &bp)) != 0)
1369 ip->i_size = DIRBLKSIZ;
1370 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1371 vnode_pager_setsize(tvp, (u_long)ip->i_size);
1372 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1373 if (DOINGSOFTDEP(tvp)) {
1375 * Ensure that the entire newly allocated block is a
1376 * valid directory so that future growth within the
1377 * block does not have to ensure that the block is
1378 * written before the inode.
1381 while (blkoff < bp->b_bcount) {
1383 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1384 blkoff += DIRBLKSIZ;
1387 if ((error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) |
1388 DOINGASYNC(tvp)))) != 0) {
1389 (void)VOP_BWRITE(bp->b_vp, bp);
1393 * Directory set up, now install its entry in the parent directory.
1395 * If we are not doing soft dependencies, then we must write out the
1396 * buffer containing the new directory body before entering the new
1397 * name in the parent. If we are doing soft dependencies, then the
1398 * buffer containing the new directory body will be passed to and
1399 * released in the soft dependency code after the code has attached
1400 * an appropriate ordering dependency to the buffer which ensures that
1401 * the buffer is written before the new name is written in the parent.
1403 if (DOINGASYNC(dvp))
1405 else if (!DOINGSOFTDEP(dvp) && ((error = VOP_BWRITE(bp->b_vp, bp))))
1407 ufs_makedirentry(ip, cnp, &newdir);
1408 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1412 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1417 dp->i_flag |= IN_CHANGE;
1418 if (DOINGSOFTDEP(dvp))
1419 softdep_change_linkcnt(dp);
1421 * No need to do an explicit VOP_TRUNCATE here, vrele will
1422 * do this for us because we set the link count to 0.
1426 ip->i_flag |= IN_CHANGE;
1427 if (DOINGSOFTDEP(tvp))
1428 softdep_change_linkcnt(ip);
1436 * Rmdir system call.
1440 struct vop_rmdir_args /* {
1441 struct vnode *a_dvp;
1443 struct componentname *a_cnp;
1446 struct vnode *vp = ap->a_vp;
1447 struct vnode *dvp = ap->a_dvp;
1448 struct componentname *cnp = ap->a_cnp;
1449 struct inode *ip, *dp;
1456 * Do not remove a directory that is in the process of being renamed.
1457 * Verify the directory is empty (and valid). Rmdir ".." will not be
1458 * valid since ".." will contain a reference to the current directory
1459 * and thus be non-empty. Do not allow the removal of mounted on
1460 * directories (this can happen when an NFS exported filesystem
1461 * tries to remove a locally mounted on directory).
1464 if (ip->i_flag & IN_RENAME) {
1468 if (ip->i_effnlink != 2 ||
1469 !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1473 if ((dp->i_flags & APPEND)
1474 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1478 if (vp->v_mountedhere != 0) {
1483 * Delete reference to directory before purging
1484 * inode. If we crash in between, the directory
1485 * will be reattached to lost+found,
1489 if (DOINGSOFTDEP(vp)) {
1490 softdep_change_linkcnt(dp);
1491 softdep_change_linkcnt(ip);
1493 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1497 if (DOINGSOFTDEP(vp)) {
1498 softdep_change_linkcnt(dp);
1499 softdep_change_linkcnt(ip);
1503 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1506 * Truncate inode. The only stuff left in the directory is "." and
1507 * "..". The "." reference is inconsequential since we are quashing
1508 * it. The soft dependency code will arrange to do these operations
1509 * after the parent directory entry has been deleted on disk, so
1510 * when running with that code we avoid doing them now.
1512 if (!DOINGSOFTDEP(vp)) {
1514 dp->i_flag |= IN_CHANGE;
1516 ip->i_flag |= IN_CHANGE;
1517 ioflag = DOINGASYNC(vp) ? 0 : IO_SYNC;
1518 error = UFS_TRUNCATE(vp, (off_t)0, ioflag, cnp->cn_cred,
1523 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1524 if (ip->i_dirhash != NULL)
1525 ufsdirhash_free(ip);
1528 VN_KNOTE(vp, NOTE_DELETE);
1533 * symlink -- make a symbolic link
1537 struct vop_symlink_args /* {
1538 struct vnode *a_dvp;
1539 struct vnode **a_vpp;
1540 struct componentname *a_cnp;
1541 struct vattr *a_vap;
1545 struct vnode *vp, **vpp = ap->a_vpp;
1549 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1553 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1555 len = strlen(ap->a_target);
1556 if (len < vp->v_mount->mnt_maxsymlinklen) {
1558 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1560 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1562 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1563 UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred,
1571 * Vnode op for reading directories.
1573 * The routine below assumes that the on-disk format of a directory
1574 * is the same as that defined by <sys/dirent.h>. If the on-disk
1575 * format changes, then it will be necessary to do a conversion
1576 * from the on-disk format that read returns to the format defined
1577 * by <sys/dirent.h>.
1581 struct vop_readdir_args /* {
1584 struct ucred *a_cred;
1590 struct uio *uio = ap->a_uio;
1595 if (ap->a_ncookies != NULL)
1597 * Ensure that the block is aligned. The caller can use
1598 * the cookies to determine where in the block to start.
1600 uio->uio_offset &= ~(DIRBLKSIZ - 1);
1601 off = uio->uio_offset;
1602 count = uio->uio_resid;
1603 /* Make sure we don't return partial entries. */
1604 if (count <= ((uio->uio_offset + count) & (DIRBLKSIZ -1)))
1606 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
1607 lost = uio->uio_resid - count;
1608 uio->uio_resid = count;
1609 uio->uio_iov->iov_len = count;
1610 # if (BYTE_ORDER == LITTLE_ENDIAN)
1611 if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) {
1612 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1614 struct dirent *dp, *edp;
1622 auio.uio_iov = &aiov;
1623 auio.uio_iovcnt = 1;
1624 auio.uio_segflg = UIO_SYSSPACE;
1625 aiov.iov_len = count;
1626 MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
1627 aiov.iov_base = dirbuf;
1628 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
1630 readcnt = count - auio.uio_resid;
1631 edp = (struct dirent *)&dirbuf[readcnt];
1632 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
1634 dp->d_namlen = dp->d_type;
1636 if (dp->d_reclen > 0) {
1637 dp = (struct dirent *)
1638 ((char *)dp + dp->d_reclen);
1645 error = uiomove(dirbuf, readcnt, uio);
1647 FREE(dirbuf, M_TEMP);
1650 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1652 if (!error && ap->a_ncookies != NULL) {
1653 struct dirent* dpStart;
1654 struct dirent* dpEnd;
1660 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1661 panic("ufs_readdir: unexpected uio from NFS server");
1662 dpStart = (struct dirent *)
1663 (uio->uio_iov->iov_base - (uio->uio_offset - off));
1664 dpEnd = (struct dirent *) uio->uio_iov->iov_base;
1665 for (dp = dpStart, ncookies = 0;
1667 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen))
1669 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1671 for (dp = dpStart, cookiep = cookies;
1673 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
1674 off += dp->d_reclen;
1675 *cookiep++ = (u_long) off;
1677 *ap->a_ncookies = ncookies;
1678 *ap->a_cookies = cookies;
1680 uio->uio_resid += lost;
1682 *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
1687 * Return target name of a symbolic link
1691 struct vop_readlink_args /* {
1694 struct ucred *a_cred;
1697 struct vnode *vp = ap->a_vp;
1698 struct inode *ip = VTOI(vp);
1702 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1703 (ip->i_din.di_blocks == 0)) { /* XXX - for old fastlink support */
1704 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1707 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1711 * Calculate the logical to physical mapping if not done already,
1712 * then call the device strategy routine.
1714 * In order to be able to swap to a file, the VOP_BMAP operation may not
1715 * deadlock on memory. See ufs_bmap() for details.
1719 struct vop_strategy_args /* {
1724 struct buf *bp = ap->a_bp;
1725 struct vnode *vp = ap->a_vp;
1730 if (vp->v_type == VBLK || vp->v_type == VCHR)
1731 panic("ufs_strategy: spec");
1732 if (bp->b_blkno == bp->b_lblkno) {
1733 error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1735 bp->b_error = error;
1736 bp->b_flags |= B_ERROR;
1740 if ((long)bp->b_blkno == -1)
1743 if ((long)bp->b_blkno == -1) {
1748 bp->b_dev = vp->v_rdev;
1749 VOP_STRATEGY(vp, bp);
1754 * Print out the contents of an inode.
1758 struct vop_print_args /* {
1762 struct vnode *vp = ap->a_vp;
1763 struct inode *ip = VTOI(vp);
1765 printf("tag VT_UFS, ino %lu, on dev %s (%d, %d)",
1766 (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev),
1768 if (vp->v_type == VFIFO)
1770 lockmgr_printinfo(&ip->i_lock);
1776 * Read wrapper for special devices.
1780 struct vop_read_args /* {
1784 struct ucred *a_cred;
1792 resid = uio->uio_resid;
1793 error = VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap);
1795 * The inode may have been revoked during the call, so it must not
1796 * be accessed blindly here or in the other wrapper functions.
1798 ip = VTOI(ap->a_vp);
1799 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1800 ip->i_flag |= IN_ACCESS;
1805 * Write wrapper for special devices.
1809 struct vop_write_args /* {
1813 struct ucred *a_cred;
1821 resid = uio->uio_resid;
1822 error = VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap);
1823 ip = VTOI(ap->a_vp);
1824 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1825 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1830 * Close wrapper for special devices.
1832 * Update the times on the inode then do device close.
1836 struct vop_close_args /* {
1839 struct ucred *a_cred;
1840 struct thread *a_td;
1843 struct vnode *vp = ap->a_vp;
1845 lwkt_gettoken(&vp->v_interlock);
1846 if (vp->v_usecount > 1)
1848 lwkt_reltoken(&vp->v_interlock);
1849 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
1853 * Read wrapper for fifos.
1857 struct vop_read_args /* {
1861 struct ucred *a_cred;
1869 resid = uio->uio_resid;
1870 error = VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap);
1871 ip = VTOI(ap->a_vp);
1872 if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL &&
1873 (uio->uio_resid != resid || (error == 0 && resid != 0)))
1874 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1879 * Write wrapper for fifos.
1883 struct vop_write_args /* {
1887 struct ucred *a_cred;
1895 resid = uio->uio_resid;
1896 error = VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap);
1897 ip = VTOI(ap->a_vp);
1898 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1899 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1904 * Close wrapper for fifos.
1906 * Update the times on the inode then do device close.
1910 struct vop_close_args /* {
1913 struct ucred *a_cred;
1914 struct thread *a_td;
1917 struct vnode *vp = ap->a_vp;
1919 lwkt_gettoken(&vp->v_interlock);
1920 if (vp->v_usecount > 1)
1922 lwkt_reltoken(&vp->v_interlock);
1923 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
1927 * Kqfilter wrapper for fifos.
1929 * Fall through to ufs kqfilter routines if needed
1932 ufsfifo_kqfilter(ap)
1933 struct vop_kqfilter_args *ap;
1937 error = VOCALL(fifo_vnodeop_p, VOFFSET(vop_kqfilter), ap);
1939 error = ufs_kqfilter(ap);
1944 * Return POSIX pathconf information applicable to ufs filesystems.
1948 struct vop_pathconf_args /* {
1955 switch (ap->a_name) {
1957 *ap->a_retval = LINK_MAX;
1960 *ap->a_retval = NAME_MAX;
1963 *ap->a_retval = PATH_MAX;
1966 *ap->a_retval = PIPE_BUF;
1968 case _PC_CHOWN_RESTRICTED:
1981 * Advisory record locking support
1985 struct vop_advlock_args /* {
1993 struct inode *ip = VTOI(ap->a_vp);
1995 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
1999 * Initialize the vnode associated with a new inode, handle aliased
2003 ufs_vinit(mntp, specops, fifoops, vpp)
2015 switch(vp->v_type = IFTOVT(ip->i_mode)) {
2019 addaliasu(vp, ip->i_rdev);
2028 if (ip->i_number == ROOTINO)
2029 vp->v_flag |= VROOT;
2031 * Initialize modrev times
2033 getmicrouptime(&tv);
2034 SETHIGH(ip->i_modrev, tv.tv_sec);
2035 SETLOW(ip->i_modrev, tv.tv_usec * 4294);
2041 * Allocate a new inode.
2044 ufs_makeinode(mode, dvp, vpp, cnp)
2048 struct componentname *cnp;
2050 struct inode *ip, *pdir;
2051 struct direct newdir;
2057 if ((cnp->cn_flags & CNP_HASBUF) == 0)
2058 panic("ufs_makeinode: no name");
2061 if ((mode & IFMT) == 0)
2064 error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2068 ip->i_gid = pdir->i_gid;
2072 struct ucred ucred, *ucp;
2076 * If we are not the owner of the directory,
2077 * and we are hacking owners here, (only do this where told to)
2078 * and we are not giving it TO root, (would subvert quotas)
2079 * then go ahead and give it to the other user.
2080 * Note that this drops off the execute bits for security.
2082 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2083 (pdir->i_mode & ISUID) &&
2084 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2085 ip->i_uid = pdir->i_uid;
2089 * Make sure the correct user gets charged
2091 * Quickly knock up a dummy credential for the victim.
2092 * XXX This seems to never be accessed out of our
2093 * context so a stack variable is ok.
2096 ucred.cr_uid = ip->i_uid;
2097 ucred.cr_ngroups = 1;
2098 ucred.cr_groups[0] = pdir->i_gid;
2102 ip->i_uid = cnp->cn_cred->cr_uid;
2105 if ((error = getinoquota(ip)) ||
2106 (error = chkiq(ip, 1, ucp, 0))) {
2107 UFS_VFREE(tvp, ip->i_number, mode);
2113 #else /* !SUIDDIR */
2114 ip->i_uid = cnp->cn_cred->cr_uid;
2116 if ((error = getinoquota(ip)) ||
2117 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2118 UFS_VFREE(tvp, ip->i_number, mode);
2123 #endif /* !SUIDDIR */
2124 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2126 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2129 if (DOINGSOFTDEP(tvp))
2130 softdep_change_linkcnt(ip);
2131 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2132 suser_cred(cnp->cn_cred, 0)) {
2133 ip->i_mode &= ~ISGID;
2136 if (cnp->cn_flags & CNP_ISWHITEOUT)
2137 ip->i_flags |= UF_OPAQUE;
2140 * Make sure inode goes to disk before directory entry.
2142 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2145 ufs_makedirentry(ip, cnp, &newdir);
2146 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2154 * Write error occurred trying to update the inode
2155 * or the directory so must deallocate the inode.
2159 ip->i_flag |= IN_CHANGE;
2160 if (DOINGSOFTDEP(tvp))
2161 softdep_change_linkcnt(ip);
2168 struct vop_generic_args *ap;
2171 panic("no vop function for %s in ufs child", ap->a_desc->vdesc_name);
2172 return (EOPNOTSUPP);
2175 static struct filterops ufsread_filtops =
2176 { 1, NULL, filt_ufsdetach, filt_ufsread };
2177 static struct filterops ufswrite_filtops =
2178 { 1, NULL, filt_ufsdetach, filt_ufswrite };
2179 static struct filterops ufsvnode_filtops =
2180 { 1, NULL, filt_ufsdetach, filt_ufsvnode };
2184 struct vop_kqfilter_args /* {
2189 struct vnode *vp = ap->a_vp;
2190 struct knote *kn = ap->a_kn;
2192 switch (kn->kn_filter) {
2194 kn->kn_fop = &ufsread_filtops;
2197 kn->kn_fop = &ufswrite_filtops;
2200 kn->kn_fop = &ufsvnode_filtops;
2206 kn->kn_hook = (caddr_t)vp;
2208 lwkt_gettoken(&vp->v_pollinfo.vpi_token);
2209 SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
2210 lwkt_reltoken(&vp->v_pollinfo.vpi_token);
2216 filt_ufsdetach(struct knote *kn)
2218 struct vnode *vp = (struct vnode *)kn->kn_hook;
2220 lwkt_gettoken(&vp->v_pollinfo.vpi_token);
2221 SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
2222 kn, knote, kn_selnext);
2223 lwkt_reltoken(&vp->v_pollinfo.vpi_token);
2228 filt_ufsread(struct knote *kn, long hint)
2230 struct vnode *vp = (struct vnode *)kn->kn_hook;
2231 struct inode *ip = VTOI(vp);
2234 * filesystem is gone, so set the EOF flag and schedule
2235 * the knote for deletion.
2237 if (hint == NOTE_REVOKE) {
2238 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2242 kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
2243 return (kn->kn_data != 0);
2248 filt_ufswrite(struct knote *kn, long hint)
2252 * filesystem is gone, so set the EOF flag and schedule
2253 * the knote for deletion.
2255 if (hint == NOTE_REVOKE)
2256 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2263 filt_ufsvnode(struct knote *kn, long hint)
2266 if (kn->kn_sfflags & hint)
2267 kn->kn_fflags |= hint;
2268 if (hint == NOTE_REVOKE) {
2269 kn->kn_flags |= EV_EOF;
2272 return (kn->kn_fflags != 0);
2275 /* Global vfs data structures for ufs. */
2276 static vop_t **ufs_vnodeop_p;
2277 static struct vnodeopv_entry_desc ufs_vnodeop_entries[] = {
2278 { &vop_default_desc, (vop_t *) vop_defaultop },
2279 { &vop_fsync_desc, (vop_t *) ufs_missingop },
2280 { &vop_read_desc, (vop_t *) ufs_missingop },
2281 { &vop_reallocblks_desc, (vop_t *) ufs_missingop },
2282 { &vop_write_desc, (vop_t *) ufs_missingop },
2283 { &vop_access_desc, (vop_t *) ufs_access },
2284 { &vop_advlock_desc, (vop_t *) ufs_advlock },
2285 { &vop_bmap_desc, (vop_t *) ufs_bmap },
2286 { &vop_cachedlookup_desc, (vop_t *) ufs_lookup },
2287 { &vop_close_desc, (vop_t *) ufs_close },
2288 { &vop_create_desc, (vop_t *) ufs_create },
2289 { &vop_getattr_desc, (vop_t *) ufs_getattr },
2290 { &vop_inactive_desc, (vop_t *) ufs_inactive },
2291 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
2292 { &vop_link_desc, (vop_t *) ufs_link },
2293 { &vop_lock_desc, (vop_t *) vop_stdlock },
2294 { &vop_lookup_desc, (vop_t *) vfs_cache_lookup },
2295 { &vop_mkdir_desc, (vop_t *) ufs_mkdir },
2296 { &vop_mknod_desc, (vop_t *) ufs_mknod },
2297 { &vop_mmap_desc, (vop_t *) ufs_mmap },
2298 { &vop_open_desc, (vop_t *) ufs_open },
2299 { &vop_pathconf_desc, (vop_t *) ufs_pathconf },
2300 { &vop_poll_desc, (vop_t *) vop_stdpoll },
2301 { &vop_kqfilter_desc, (vop_t *) ufs_kqfilter },
2302 { &vop_print_desc, (vop_t *) ufs_print },
2303 { &vop_readdir_desc, (vop_t *) ufs_readdir },
2304 { &vop_readlink_desc, (vop_t *) ufs_readlink },
2305 { &vop_reclaim_desc, (vop_t *) ufs_reclaim },
2306 { &vop_remove_desc, (vop_t *) ufs_remove },
2307 { &vop_rename_desc, (vop_t *) ufs_rename },
2308 { &vop_rmdir_desc, (vop_t *) ufs_rmdir },
2309 { &vop_setattr_desc, (vop_t *) ufs_setattr },
2310 { &vop_strategy_desc, (vop_t *) ufs_strategy },
2311 { &vop_symlink_desc, (vop_t *) ufs_symlink },
2312 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
2313 { &vop_whiteout_desc, (vop_t *) ufs_whiteout },
2316 static struct vnodeopv_desc ufs_vnodeop_opv_desc =
2317 { &ufs_vnodeop_p, ufs_vnodeop_entries };
2319 static vop_t **ufs_specop_p;
2320 static struct vnodeopv_entry_desc ufs_specop_entries[] = {
2321 { &vop_default_desc, (vop_t *) spec_vnoperate },
2322 { &vop_fsync_desc, (vop_t *) ufs_missingop },
2323 { &vop_access_desc, (vop_t *) ufs_access },
2324 { &vop_close_desc, (vop_t *) ufsspec_close },
2325 { &vop_getattr_desc, (vop_t *) ufs_getattr },
2326 { &vop_inactive_desc, (vop_t *) ufs_inactive },
2327 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
2328 { &vop_lock_desc, (vop_t *) vop_stdlock },
2329 { &vop_print_desc, (vop_t *) ufs_print },
2330 { &vop_read_desc, (vop_t *) ufsspec_read },
2331 { &vop_reclaim_desc, (vop_t *) ufs_reclaim },
2332 { &vop_setattr_desc, (vop_t *) ufs_setattr },
2333 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
2334 { &vop_write_desc, (vop_t *) ufsspec_write },
2337 static struct vnodeopv_desc ufs_specop_opv_desc =
2338 { &ufs_specop_p, ufs_specop_entries };
2340 static vop_t **ufs_fifoop_p;
2341 static struct vnodeopv_entry_desc ufs_fifoop_entries[] = {
2342 { &vop_default_desc, (vop_t *) fifo_vnoperate },
2343 { &vop_fsync_desc, (vop_t *) ufs_missingop },
2344 { &vop_access_desc, (vop_t *) ufs_access },
2345 { &vop_close_desc, (vop_t *) ufsfifo_close },
2346 { &vop_getattr_desc, (vop_t *) ufs_getattr },
2347 { &vop_inactive_desc, (vop_t *) ufs_inactive },
2348 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
2349 { &vop_kqfilter_desc, (vop_t *) ufsfifo_kqfilter },
2350 { &vop_lock_desc, (vop_t *) vop_stdlock },
2351 { &vop_print_desc, (vop_t *) ufs_print },
2352 { &vop_read_desc, (vop_t *) ufsfifo_read },
2353 { &vop_reclaim_desc, (vop_t *) ufs_reclaim },
2354 { &vop_setattr_desc, (vop_t *) ufs_setattr },
2355 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
2356 { &vop_write_desc, (vop_t *) ufsfifo_write },
2359 static struct vnodeopv_desc ufs_fifoop_opv_desc =
2360 { &ufs_fifoop_p, ufs_fifoop_entries };
2362 VNODEOP_SET(ufs_vnodeop_opv_desc);
2363 VNODEOP_SET(ufs_specop_opv_desc);
2364 VNODEOP_SET(ufs_fifoop_opv_desc);
2368 struct vop_generic_args /* {
2369 struct vnodeop_desc *a_desc;
2372 return (VOCALL(ufs_vnodeop_p, ap->a_desc->vdesc_offset, ap));
2376 ufs_vnoperatefifo(ap)
2377 struct vop_generic_args /* {
2378 struct vnodeop_desc *a_desc;
2381 return (VOCALL(ufs_fifoop_p, ap->a_desc->vdesc_offset, ap));
2385 ufs_vnoperatespec(ap)
2386 struct vop_generic_args /* {
2387 struct vnodeop_desc *a_desc;
2390 return (VOCALL(ufs_specop_p, ap->a_desc->vdesc_offset, ap));