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.11 2004/03/01 06:33:23 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;
298 lwkt_gettoken(&vlock, vp->v_interlock);
299 if (vp->v_usecount > 1)
301 lwkt_reltoken(&vlock);
307 struct vop_access_args /* {
310 struct ucred *a_cred;
314 struct vnode *vp = ap->a_vp;
315 struct inode *ip = VTOI(vp);
316 struct ucred *cred = ap->a_cred;
317 mode_t mask, mode = ap->a_mode;
325 * Disallow write attempts on read-only file systems;
326 * unless the file is a socket, fifo, or a block or
327 * character device resident on the file system.
330 switch (vp->v_type) {
334 if (vp->v_mount->mnt_flag & MNT_RDONLY)
337 if ((error = getinoquota(ip)) != 0)
346 /* If immutable bit set, nobody gets to write it. */
347 if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE))
350 /* Otherwise, user id 0 always gets access. */
351 if (cred->cr_uid == 0)
356 /* Otherwise, check the owner. */
357 if (cred->cr_uid == ip->i_uid) {
364 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
367 /* Otherwise, check the groups. */
368 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
369 if (ip->i_gid == *gp) {
376 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
379 /* Otherwise, check everyone else. */
386 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
392 struct vop_getattr_args /* {
398 struct vnode *vp = ap->a_vp;
399 struct inode *ip = VTOI(vp);
400 struct vattr *vap = ap->a_vap;
404 * Copy from inode table
406 vap->va_fsid = dev2udev(ip->i_dev);
407 vap->va_fileid = ip->i_number;
408 vap->va_mode = ip->i_mode & ~IFMT;
409 vap->va_nlink = VFSTOUFS(vp->v_mount)->um_i_effnlink_valid ?
410 ip->i_effnlink : ip->i_nlink;
411 vap->va_uid = ip->i_uid;
412 vap->va_gid = ip->i_gid;
413 vap->va_rdev = ip->i_rdev;
414 vap->va_size = ip->i_din.di_size;
415 vap->va_atime.tv_sec = ip->i_atime;
416 vap->va_atime.tv_nsec = ip->i_atimensec;
417 vap->va_mtime.tv_sec = ip->i_mtime;
418 vap->va_mtime.tv_nsec = ip->i_mtimensec;
419 vap->va_ctime.tv_sec = ip->i_ctime;
420 vap->va_ctime.tv_nsec = ip->i_ctimensec;
421 vap->va_flags = ip->i_flags;
422 vap->va_gen = ip->i_gen;
423 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
424 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
425 vap->va_type = IFTOVT(ip->i_mode);
426 vap->va_filerev = ip->i_modrev;
431 * Set attribute vnode op. called from several syscalls
435 struct vop_setattr_args /* {
438 struct ucred *a_cred;
442 struct vattr *vap = ap->a_vap;
443 struct vnode *vp = ap->a_vp;
444 struct inode *ip = VTOI(vp);
445 struct ucred *cred = ap->a_cred;
449 * Check for unsettable attributes.
451 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
452 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
453 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
454 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
457 if (vap->va_flags != VNOVAL) {
458 if (vp->v_mount->mnt_flag & MNT_RDONLY)
460 if (cred->cr_uid != ip->i_uid &&
461 (error = suser_cred(cred, PRISON_ROOT)))
463 if ((cred->cr_uid == 0) && (cred->cr_prison == NULL)) {
465 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) &&
468 ip->i_flags = vap->va_flags;
471 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
472 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
474 ip->i_flags &= SF_SETTABLE;
475 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
477 ip->i_flag |= IN_CHANGE;
478 if (vap->va_flags & (IMMUTABLE | APPEND))
481 if (ip->i_flags & (IMMUTABLE | APPEND))
484 * Go through the fields and update iff not VNOVAL.
486 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
487 if (vp->v_mount->mnt_flag & MNT_RDONLY)
489 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred, ap->a_td)) != 0)
492 if (vap->va_size != VNOVAL) {
494 * Disallow write attempts on read-only file systems;
495 * unless the file is a socket, fifo, or a block or
496 * character device resident on the file system.
498 switch (vp->v_type) {
503 if (vp->v_mount->mnt_flag & MNT_RDONLY)
509 if ((error = UFS_TRUNCATE(vp, vap->va_size, 0, cred, ap->a_td)) != 0)
513 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
514 if (vp->v_mount->mnt_flag & MNT_RDONLY)
516 if (cred->cr_uid != ip->i_uid &&
517 (error = suser_cred(cred, PRISON_ROOT)) &&
518 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
519 (error = VOP_ACCESS(vp, VWRITE, cred, ap->a_td))))
521 if (vap->va_atime.tv_sec != VNOVAL)
522 ip->i_flag |= IN_ACCESS;
523 if (vap->va_mtime.tv_sec != VNOVAL)
524 ip->i_flag |= IN_CHANGE | IN_UPDATE;
526 if (vap->va_atime.tv_sec != VNOVAL) {
527 ip->i_atime = vap->va_atime.tv_sec;
528 ip->i_atimensec = vap->va_atime.tv_nsec;
530 if (vap->va_mtime.tv_sec != VNOVAL) {
531 ip->i_mtime = vap->va_mtime.tv_sec;
532 ip->i_mtimensec = vap->va_mtime.tv_nsec;
534 error = UFS_UPDATE(vp, 0);
539 if (vap->va_mode != (mode_t)VNOVAL) {
540 if (vp->v_mount->mnt_flag & MNT_RDONLY)
542 error = ufs_chmod(vp, (int)vap->va_mode, cred, ap->a_td);
544 VN_KNOTE(vp, NOTE_ATTRIB);
549 * Change the mode on a file.
550 * Inode must be locked before calling.
553 ufs_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td)
555 struct inode *ip = VTOI(vp);
558 if (cred->cr_uid != ip->i_uid) {
559 error = suser_cred(cred, PRISON_ROOT);
564 if (vp->v_type != VDIR && (mode & S_ISTXT))
566 if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
569 ip->i_mode &= ~ALLPERMS;
570 ip->i_mode |= (mode & ALLPERMS);
571 ip->i_flag |= IN_CHANGE;
576 * Perform chown operation on inode ip;
577 * inode must be locked prior to call.
580 ufs_chown(vp, uid, gid, cred, td)
587 struct inode *ip = VTOI(vp);
596 if (uid == (uid_t)VNOVAL)
598 if (gid == (gid_t)VNOVAL)
601 * If we don't own the file, are trying to change the owner
602 * of the file, or are not a member of the target group,
603 * the caller must be superuser or the call fails.
605 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
606 (gid != ip->i_gid && !groupmember((gid_t)gid, cred))) &&
607 (error = suser_cred(cred, PRISON_ROOT)))
612 if ((error = getinoquota(ip)) != 0)
615 dqrele(vp, ip->i_dquot[USRQUOTA]);
616 ip->i_dquot[USRQUOTA] = NODQUOT;
619 dqrele(vp, ip->i_dquot[GRPQUOTA]);
620 ip->i_dquot[GRPQUOTA] = NODQUOT;
622 change = ip->i_blocks;
623 (void) chkdq(ip, -change, cred, CHOWN);
624 (void) chkiq(ip, -1, cred, CHOWN);
625 for (i = 0; i < MAXQUOTAS; i++) {
626 dqrele(vp, ip->i_dquot[i]);
627 ip->i_dquot[i] = NODQUOT;
633 if ((error = getinoquota(ip)) == 0) {
635 dqrele(vp, ip->i_dquot[USRQUOTA]);
636 ip->i_dquot[USRQUOTA] = NODQUOT;
639 dqrele(vp, ip->i_dquot[GRPQUOTA]);
640 ip->i_dquot[GRPQUOTA] = NODQUOT;
642 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
643 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
646 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
648 for (i = 0; i < MAXQUOTAS; i++) {
649 dqrele(vp, ip->i_dquot[i]);
650 ip->i_dquot[i] = NODQUOT;
655 if (getinoquota(ip) == 0) {
657 dqrele(vp, ip->i_dquot[USRQUOTA]);
658 ip->i_dquot[USRQUOTA] = NODQUOT;
661 dqrele(vp, ip->i_dquot[GRPQUOTA]);
662 ip->i_dquot[GRPQUOTA] = NODQUOT;
664 (void) chkdq(ip, change, cred, FORCE|CHOWN);
665 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
666 (void) getinoquota(ip);
671 panic("ufs_chown: lost quota");
673 ip->i_flag |= IN_CHANGE;
674 if (cred->cr_uid != 0 && (ouid != uid || ogid != gid))
675 ip->i_mode &= ~(ISUID | ISGID);
682 * NB Currently unsupported.
687 struct vop_mmap_args /* {
690 struct ucred *a_cred;
700 struct vop_remove_args /* {
703 struct componentname *a_cnp;
707 struct vnode *vp = ap->a_vp;
708 struct vnode *dvp = ap->a_dvp;
712 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
713 (VTOI(dvp)->i_flags & APPEND)) {
717 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
718 VN_KNOTE(vp, NOTE_DELETE);
719 VN_KNOTE(dvp, NOTE_WRITE);
729 struct vop_link_args /* {
730 struct vnode *a_tdvp;
732 struct componentname *a_cnp;
735 struct vnode *vp = ap->a_vp;
736 struct vnode *tdvp = ap->a_tdvp;
737 struct componentname *cnp = ap->a_cnp;
738 struct thread *td = cnp->cn_td;
740 struct direct newdir;
744 if ((cnp->cn_flags & CNP_HASBUF) == 0)
745 panic("ufs_link: no name");
747 if (tdvp->v_mount != vp->v_mount) {
751 if (tdvp != vp && (error = vn_lock(vp, NULL, LK_EXCLUSIVE, td))) {
755 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
759 if (ip->i_flags & (IMMUTABLE | APPEND)) {
765 ip->i_flag |= IN_CHANGE;
766 if (DOINGSOFTDEP(vp))
767 softdep_change_linkcnt(ip);
768 error = UFS_UPDATE(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
770 ufs_makedirentry(ip, cnp, &newdir);
771 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
777 ip->i_flag |= IN_CHANGE;
778 if (DOINGSOFTDEP(vp))
779 softdep_change_linkcnt(ip);
783 VOP_UNLOCK(vp, NULL, 0, td);
785 VN_KNOTE(vp, NOTE_LINK);
786 VN_KNOTE(tdvp, NOTE_WRITE);
791 * whiteout vnode call
795 struct vop_whiteout_args /* {
797 struct componentname *a_cnp;
801 struct vnode *dvp = ap->a_dvp;
802 struct componentname *cnp = ap->a_cnp;
803 struct direct newdir;
806 switch (ap->a_flags) {
808 /* 4.4 format directories support whiteout operations */
809 if (dvp->v_mount->mnt_maxsymlinklen > 0)
814 /* create a new directory whiteout */
816 if ((cnp->cn_flags & CNP_SAVENAME) == 0)
817 panic("ufs_whiteout: missing name");
818 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
819 panic("ufs_whiteout: old format filesystem");
823 newdir.d_namlen = cnp->cn_namelen;
824 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
825 newdir.d_type = DT_WHT;
826 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
830 /* remove an existing directory whiteout */
832 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
833 panic("ufs_whiteout: old format filesystem");
836 cnp->cn_flags &= ~CNP_DOWHITEOUT;
837 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
840 panic("ufs_whiteout: unknown op");
846 * Rename system call.
847 * rename("foo", "bar");
850 * link("foo", "bar");
852 * but ``atomically''. Can't do full commit without saving state in the
853 * inode on disk which isn't feasible at this time. Best we can do is
854 * always guarantee the target exists.
856 * Basic algorithm is:
858 * 1) Bump link count on source while we're linking it to the
859 * target. This also ensure the inode won't be deleted out
860 * from underneath us while we work (it may be truncated by
861 * a concurrent `trunc' or `open' for creation).
862 * 2) Link source to destination. If destination already exists,
864 * 3) Unlink source reference to inode if still around. If a
865 * directory was moved and the parent of the destination
866 * is different from the source, patch the ".." entry in the
871 struct vop_rename_args /* {
872 struct vnode *a_fdvp;
874 struct componentname *a_fcnp;
875 struct vnode *a_tdvp;
877 struct componentname *a_tcnp;
880 struct vnode *tvp = ap->a_tvp;
881 struct vnode *tdvp = ap->a_tdvp;
882 struct vnode *fvp = ap->a_fvp;
883 struct vnode *fdvp = ap->a_fdvp;
884 struct componentname *tcnp = ap->a_tcnp;
885 struct componentname *fcnp = ap->a_fcnp;
886 struct thread *td = fcnp->cn_td;
887 struct inode *ip, *xp, *dp;
888 struct direct newdir;
889 int doingdirectory = 0, oldparent = 0, newparent = 0;
890 int error = 0, ioflag;
893 if ((tcnp->cn_flags & CNP_HASBUF) == 0 ||
894 (fcnp->cn_flags & CNP_HASBUF) == 0)
895 panic("ufs_rename: no name");
898 * Check for cross-device rename.
900 if ((fvp->v_mount != tdvp->v_mount) ||
901 (tvp && (fvp->v_mount != tvp->v_mount))) {
915 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
916 (VTOI(tdvp)->i_flags & APPEND))) {
922 * Renaming a file to itself has no effect. The upper layers should
923 * not call us in that case. Temporarily just warn if they do.
926 printf("ufs_rename: fvp == tvp (can't happen)\n");
931 if ((error = vn_lock(fvp, NULL, LK_EXCLUSIVE, td)) != 0)
935 if (ip->i_nlink >= LINK_MAX) {
936 VOP_UNLOCK(fvp, NULL, 0, td);
940 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
941 || (dp->i_flags & APPEND)) {
942 VOP_UNLOCK(fvp, NULL, 0, td);
946 if ((ip->i_mode & IFMT) == IFDIR) {
948 * Avoid ".", "..", and aliases of "." for obvious reasons.
950 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
951 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & CNP_ISDOTDOT ||
952 (ip->i_flag & IN_RENAME)) {
953 VOP_UNLOCK(fvp, NULL, 0, td);
957 ip->i_flag |= IN_RENAME;
958 oldparent = dp->i_number;
961 VN_KNOTE(fdvp, NOTE_WRITE); /* XXX right place? */
965 * When the target exists, both the directory
966 * and target vnodes are returned locked.
974 * 1) Bump link count while we're moving stuff
975 * around. If we crash somewhere before
976 * completing our work, the link count
977 * may be wrong, but correctable.
981 ip->i_flag |= IN_CHANGE;
982 if (DOINGSOFTDEP(fvp))
983 softdep_change_linkcnt(ip);
984 if ((error = UFS_UPDATE(fvp, !(DOINGSOFTDEP(fvp) |
985 DOINGASYNC(fvp)))) != 0) {
986 VOP_UNLOCK(fvp, NULL, 0, td);
991 * If ".." must be changed (ie the directory gets a new
992 * parent) then the source directory must not be in the
993 * directory heirarchy above the target, as this would
994 * orphan everything below the source directory. Also
995 * the user must have write permission in the source so
996 * as to be able to change "..". We must repeat the call
997 * to namei, as the parent directory is unlocked by the
998 * call to checkpath().
1000 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_td);
1001 VOP_UNLOCK(fvp, NULL, 0, td);
1002 if (oldparent != dp->i_number)
1003 newparent = dp->i_number;
1004 if (doingdirectory && newparent) {
1005 if (error) /* write access check above */
1009 error = ufs_checkpath(ip, dp, tcnp->cn_cred);
1012 if ((tcnp->cn_flags & CNP_SAVESTART) == 0)
1013 panic("ufs_rename: lost to startdir");
1015 error = relookup(tdvp, &tvp, tcnp);
1025 * 2) If target doesn't exist, link the target
1026 * to the source and unlink the source.
1027 * Otherwise, rewrite the target directory
1028 * entry to reference the source inode and
1029 * expunge the original entry's existence.
1032 if (dp->i_dev != ip->i_dev)
1033 panic("ufs_rename: EXDEV");
1035 * Account for ".." in new directory.
1036 * When source and destination have the same
1037 * parent we don't fool with the link count.
1039 if (doingdirectory && newparent) {
1040 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1046 dp->i_flag |= IN_CHANGE;
1047 if (DOINGSOFTDEP(tdvp))
1048 softdep_change_linkcnt(dp);
1049 error = UFS_UPDATE(tdvp, !(DOINGSOFTDEP(tdvp) |
1054 ufs_makedirentry(ip, tcnp, &newdir);
1055 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1057 if (doingdirectory && newparent) {
1060 dp->i_flag |= IN_CHANGE;
1061 if (DOINGSOFTDEP(tdvp))
1062 softdep_change_linkcnt(dp);
1063 (void)UFS_UPDATE(tdvp, 1);
1067 VN_KNOTE(tdvp, NOTE_WRITE);
1070 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1071 panic("ufs_rename: EXDEV");
1073 * Short circuit rename(foo, foo).
1075 if (xp->i_number == ip->i_number)
1076 panic("ufs_rename: same file");
1078 * If the parent directory is "sticky", then the user must
1079 * own the parent directory, or the destination of the rename,
1080 * otherwise the destination may not be changed (except by
1081 * root). This implements append-only directories.
1083 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
1084 tcnp->cn_cred->cr_uid != dp->i_uid &&
1085 xp->i_uid != tcnp->cn_cred->cr_uid) {
1090 * Target must be empty if a directory and have no links
1091 * to it. Also, ensure source and target are compatible
1092 * (both directories, or both not directories).
1094 if ((xp->i_mode&IFMT) == IFDIR) {
1095 if ((xp->i_effnlink > 2) ||
1096 !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1100 if (!doingdirectory) {
1105 } else if (doingdirectory) {
1109 error = ufs_dirrewrite(dp, xp, ip->i_number,
1111 (doingdirectory && newparent) ? newparent : doingdirectory);
1114 if (doingdirectory) {
1117 if (DOINGSOFTDEP(tdvp))
1118 softdep_change_linkcnt(dp);
1121 if (DOINGSOFTDEP(tvp))
1122 softdep_change_linkcnt(xp);
1124 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1126 * Truncate inode. The only stuff left in the directory
1127 * is "." and "..". The "." reference is inconsequential
1128 * since we are quashing it. We have removed the "."
1129 * reference and the reference in the parent directory,
1130 * but there may be other hard links. The soft
1131 * dependency code will arrange to do these operations
1132 * after the parent directory entry has been deleted on
1133 * disk, so when running with that code we avoid doing
1138 dp->i_flag |= IN_CHANGE;
1141 xp->i_flag |= IN_CHANGE;
1142 ioflag = DOINGASYNC(tvp) ? 0 : IO_SYNC;
1143 if ((error = UFS_TRUNCATE(tvp, (off_t)0, ioflag,
1144 tcnp->cn_cred, tcnp->cn_td)) != 0)
1147 VN_KNOTE(tdvp, NOTE_WRITE);
1149 VN_KNOTE(tvp, NOTE_DELETE);
1155 * 3) Unlink the source.
1157 fcnp->cn_flags &= ~CNP_MODMASK;
1158 fcnp->cn_flags |= CNP_LOCKPARENT | CNP_LOCKLEAF;
1159 if ((fcnp->cn_flags & CNP_SAVESTART) == 0)
1160 panic("ufs_rename: lost from startdir");
1162 error = relookup(fdvp, &fvp, fcnp);
1170 * From name has disappeared.
1173 panic("ufs_rename: lost dir entry");
1178 * Ensure that the directory entry still exists and has not
1179 * changed while the new name has been entered. If the source is
1180 * a file then the entry may have been unlinked or renamed. In
1181 * either case there is no further work to be done. If the source
1182 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1183 * flag ensures that it cannot be moved by another rename or removed
1188 panic("ufs_rename: lost dir entry");
1191 * If the source is a directory with a
1192 * new parent, the link count of the old
1193 * parent directory must be decremented
1194 * and ".." set to point to the new parent.
1196 if (doingdirectory && newparent) {
1197 xp->i_offset = mastertemplate.dot_reclen;
1198 ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1201 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1202 xp->i_flag &= ~IN_RENAME;
1204 VN_KNOTE(fvp, NOTE_RENAME);
1218 ip->i_flag &= ~IN_RENAME;
1219 if (vn_lock(fvp, NULL, LK_EXCLUSIVE, td) == 0) {
1222 ip->i_flag |= IN_CHANGE;
1223 ip->i_flag &= ~IN_RENAME;
1224 if (DOINGSOFTDEP(fvp))
1225 softdep_change_linkcnt(ip);
1237 struct vop_mkdir_args /* {
1238 struct vnode *a_dvp;
1239 struct vnode **a_vpp;
1240 struct componentname *a_cnp;
1241 struct vattr *a_vap;
1244 struct vnode *dvp = ap->a_dvp;
1245 struct vattr *vap = ap->a_vap;
1246 struct componentname *cnp = ap->a_cnp;
1247 struct inode *ip, *dp;
1250 struct dirtemplate dirtemplate, *dtp;
1251 struct direct newdir;
1256 if ((cnp->cn_flags & CNP_HASBUF) == 0)
1257 panic("ufs_mkdir: no name");
1260 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1264 dmode = vap->va_mode & 0777;
1267 * Must simulate part of ufs_makeinode here to acquire the inode,
1268 * but not have it entered in the parent directory. The entry is
1269 * made later after writing "." and ".." entries.
1271 error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
1275 ip->i_gid = dp->i_gid;
1279 struct ucred ucred, *ucp;
1283 * If we are hacking owners here, (only do this where told to)
1284 * and we are not giving it TO root, (would subvert quotas)
1285 * then go ahead and give it to the other user.
1286 * The new directory also inherits the SUID bit.
1287 * If user's UID and dir UID are the same,
1288 * 'give it away' so that the SUID is still forced on.
1290 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1291 (dp->i_mode & ISUID) && dp->i_uid) {
1293 ip->i_uid = dp->i_uid;
1295 if (dp->i_uid != cnp->cn_cred->cr_uid) {
1297 * Make sure the correct user gets charged
1299 * Make a dummy credential for the victim.
1300 * XXX This seems to never be accessed out of
1301 * our context so a stack variable is ok.
1304 ucred.cr_uid = ip->i_uid;
1305 ucred.cr_ngroups = 1;
1306 ucred.cr_groups[0] = dp->i_gid;
1311 ip->i_uid = cnp->cn_cred->cr_uid;
1313 if ((error = getinoquota(ip)) ||
1314 (error = chkiq(ip, 1, ucp, 0))) {
1315 UFS_VFREE(tvp, ip->i_number, dmode);
1321 #else /* !SUIDDIR */
1322 ip->i_uid = cnp->cn_cred->cr_uid;
1324 if ((error = getinoquota(ip)) ||
1325 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1326 UFS_VFREE(tvp, ip->i_number, dmode);
1331 #endif /* !SUIDDIR */
1332 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1334 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1337 if (DOINGSOFTDEP(tvp))
1338 softdep_change_linkcnt(ip);
1339 if (cnp->cn_flags & CNP_ISWHITEOUT)
1340 ip->i_flags |= UF_OPAQUE;
1343 * Bump link count in parent directory to reflect work done below.
1344 * Should be done before reference is created so cleanup is
1345 * possible if we crash.
1349 dp->i_flag |= IN_CHANGE;
1350 if (DOINGSOFTDEP(dvp))
1351 softdep_change_linkcnt(dp);
1352 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1357 * Initialize directory with "." and ".." from static template.
1359 if (dvp->v_mount->mnt_maxsymlinklen > 0
1361 dtp = &mastertemplate;
1363 dtp = (struct dirtemplate *)&omastertemplate;
1365 dirtemplate.dot_ino = ip->i_number;
1366 dirtemplate.dotdot_ino = dp->i_number;
1367 if ((error = VOP_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred,
1368 B_CLRBUF, &bp)) != 0)
1370 ip->i_size = DIRBLKSIZ;
1371 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1372 vnode_pager_setsize(tvp, (u_long)ip->i_size);
1373 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1374 if (DOINGSOFTDEP(tvp)) {
1376 * Ensure that the entire newly allocated block is a
1377 * valid directory so that future growth within the
1378 * block does not have to ensure that the block is
1379 * written before the inode.
1382 while (blkoff < bp->b_bcount) {
1384 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1385 blkoff += DIRBLKSIZ;
1388 if ((error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) |
1389 DOINGASYNC(tvp)))) != 0) {
1390 (void)VOP_BWRITE(bp->b_vp, bp);
1394 * Directory set up, now install its entry in the parent directory.
1396 * If we are not doing soft dependencies, then we must write out the
1397 * buffer containing the new directory body before entering the new
1398 * name in the parent. If we are doing soft dependencies, then the
1399 * buffer containing the new directory body will be passed to and
1400 * released in the soft dependency code after the code has attached
1401 * an appropriate ordering dependency to the buffer which ensures that
1402 * the buffer is written before the new name is written in the parent.
1404 if (DOINGASYNC(dvp))
1406 else if (!DOINGSOFTDEP(dvp) && ((error = VOP_BWRITE(bp->b_vp, bp))))
1408 ufs_makedirentry(ip, cnp, &newdir);
1409 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1413 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1418 dp->i_flag |= IN_CHANGE;
1419 if (DOINGSOFTDEP(dvp))
1420 softdep_change_linkcnt(dp);
1422 * No need to do an explicit VOP_TRUNCATE here, vrele will
1423 * do this for us because we set the link count to 0.
1427 ip->i_flag |= IN_CHANGE;
1428 if (DOINGSOFTDEP(tvp))
1429 softdep_change_linkcnt(ip);
1437 * Rmdir system call.
1441 struct vop_rmdir_args /* {
1442 struct vnode *a_dvp;
1444 struct componentname *a_cnp;
1447 struct vnode *vp = ap->a_vp;
1448 struct vnode *dvp = ap->a_dvp;
1449 struct componentname *cnp = ap->a_cnp;
1450 struct inode *ip, *dp;
1457 * Do not remove a directory that is in the process of being renamed.
1458 * Verify the directory is empty (and valid). Rmdir ".." will not be
1459 * valid since ".." will contain a reference to the current directory
1460 * and thus be non-empty. Do not allow the removal of mounted on
1461 * directories (this can happen when an NFS exported filesystem
1462 * tries to remove a locally mounted on directory).
1465 if (ip->i_flag & IN_RENAME) {
1469 if (ip->i_effnlink != 2 ||
1470 !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1474 if ((dp->i_flags & APPEND)
1475 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1479 if (vp->v_mountedhere != 0) {
1484 * Delete reference to directory before purging
1485 * inode. If we crash in between, the directory
1486 * will be reattached to lost+found,
1490 if (DOINGSOFTDEP(vp)) {
1491 softdep_change_linkcnt(dp);
1492 softdep_change_linkcnt(ip);
1494 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1498 if (DOINGSOFTDEP(vp)) {
1499 softdep_change_linkcnt(dp);
1500 softdep_change_linkcnt(ip);
1504 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1507 * Truncate inode. The only stuff left in the directory is "." and
1508 * "..". The "." reference is inconsequential since we are quashing
1509 * it. The soft dependency code will arrange to do these operations
1510 * after the parent directory entry has been deleted on disk, so
1511 * when running with that code we avoid doing them now.
1513 if (!DOINGSOFTDEP(vp)) {
1515 dp->i_flag |= IN_CHANGE;
1517 ip->i_flag |= IN_CHANGE;
1518 ioflag = DOINGASYNC(vp) ? 0 : IO_SYNC;
1519 error = UFS_TRUNCATE(vp, (off_t)0, ioflag, cnp->cn_cred,
1524 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1525 if (ip->i_dirhash != NULL)
1526 ufsdirhash_free(ip);
1529 VN_KNOTE(vp, NOTE_DELETE);
1534 * symlink -- make a symbolic link
1538 struct vop_symlink_args /* {
1539 struct vnode *a_dvp;
1540 struct vnode **a_vpp;
1541 struct componentname *a_cnp;
1542 struct vattr *a_vap;
1546 struct vnode *vp, **vpp = ap->a_vpp;
1550 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1554 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1556 len = strlen(ap->a_target);
1557 if (len < vp->v_mount->mnt_maxsymlinklen) {
1559 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1561 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1563 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1564 UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred,
1572 * Vnode op for reading directories.
1574 * The routine below assumes that the on-disk format of a directory
1575 * is the same as that defined by <sys/dirent.h>. If the on-disk
1576 * format changes, then it will be necessary to do a conversion
1577 * from the on-disk format that read returns to the format defined
1578 * by <sys/dirent.h>.
1582 struct vop_readdir_args /* {
1585 struct ucred *a_cred;
1591 struct uio *uio = ap->a_uio;
1596 if (ap->a_ncookies != NULL)
1598 * Ensure that the block is aligned. The caller can use
1599 * the cookies to determine where in the block to start.
1601 uio->uio_offset &= ~(DIRBLKSIZ - 1);
1602 off = uio->uio_offset;
1603 count = uio->uio_resid;
1604 /* Make sure we don't return partial entries. */
1605 if (count <= ((uio->uio_offset + count) & (DIRBLKSIZ -1)))
1607 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
1608 lost = uio->uio_resid - count;
1609 uio->uio_resid = count;
1610 uio->uio_iov->iov_len = count;
1611 # if (BYTE_ORDER == LITTLE_ENDIAN)
1612 if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) {
1613 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1615 struct dirent *dp, *edp;
1623 auio.uio_iov = &aiov;
1624 auio.uio_iovcnt = 1;
1625 auio.uio_segflg = UIO_SYSSPACE;
1626 aiov.iov_len = count;
1627 MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
1628 aiov.iov_base = dirbuf;
1629 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
1631 readcnt = count - auio.uio_resid;
1632 edp = (struct dirent *)&dirbuf[readcnt];
1633 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
1635 dp->d_namlen = dp->d_type;
1637 if (dp->d_reclen > 0) {
1638 dp = (struct dirent *)
1639 ((char *)dp + dp->d_reclen);
1646 error = uiomove(dirbuf, readcnt, uio);
1648 FREE(dirbuf, M_TEMP);
1651 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1653 if (!error && ap->a_ncookies != NULL) {
1654 struct dirent* dpStart;
1655 struct dirent* dpEnd;
1661 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1662 panic("ufs_readdir: unexpected uio from NFS server");
1663 dpStart = (struct dirent *)
1664 (uio->uio_iov->iov_base - (uio->uio_offset - off));
1665 dpEnd = (struct dirent *) uio->uio_iov->iov_base;
1666 for (dp = dpStart, ncookies = 0;
1668 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen))
1670 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1672 for (dp = dpStart, cookiep = cookies;
1674 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
1675 off += dp->d_reclen;
1676 *cookiep++ = (u_long) off;
1678 *ap->a_ncookies = ncookies;
1679 *ap->a_cookies = cookies;
1681 uio->uio_resid += lost;
1683 *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
1688 * Return target name of a symbolic link
1692 struct vop_readlink_args /* {
1695 struct ucred *a_cred;
1698 struct vnode *vp = ap->a_vp;
1699 struct inode *ip = VTOI(vp);
1703 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1704 (ip->i_din.di_blocks == 0)) { /* XXX - for old fastlink support */
1705 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1708 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1712 * Calculate the logical to physical mapping if not done already,
1713 * then call the device strategy routine.
1715 * In order to be able to swap to a file, the VOP_BMAP operation may not
1716 * deadlock on memory. See ufs_bmap() for details.
1720 struct vop_strategy_args /* {
1725 struct buf *bp = ap->a_bp;
1726 struct vnode *vp = ap->a_vp;
1731 if (vp->v_type == VBLK || vp->v_type == VCHR)
1732 panic("ufs_strategy: spec");
1733 if (bp->b_blkno == bp->b_lblkno) {
1734 error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1736 bp->b_error = error;
1737 bp->b_flags |= B_ERROR;
1741 if ((long)bp->b_blkno == -1)
1744 if ((long)bp->b_blkno == -1) {
1749 bp->b_dev = vp->v_rdev;
1750 VOP_STRATEGY(vp, bp);
1755 * Print out the contents of an inode.
1759 struct vop_print_args /* {
1763 struct vnode *vp = ap->a_vp;
1764 struct inode *ip = VTOI(vp);
1766 printf("tag VT_UFS, ino %lu, on dev %s (%d, %d)",
1767 (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev),
1769 if (vp->v_type == VFIFO)
1771 lockmgr_printinfo(&ip->i_lock);
1777 * Read wrapper for special devices.
1781 struct vop_read_args /* {
1785 struct ucred *a_cred;
1793 resid = uio->uio_resid;
1794 error = VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap);
1796 * The inode may have been revoked during the call, so it must not
1797 * be accessed blindly here or in the other wrapper functions.
1799 ip = VTOI(ap->a_vp);
1800 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1801 ip->i_flag |= IN_ACCESS;
1806 * Write wrapper for special devices.
1810 struct vop_write_args /* {
1814 struct ucred *a_cred;
1822 resid = uio->uio_resid;
1823 error = VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap);
1824 ip = VTOI(ap->a_vp);
1825 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1826 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1831 * Close wrapper for special devices.
1833 * Update the times on the inode then do device close.
1837 struct vop_close_args /* {
1840 struct ucred *a_cred;
1841 struct thread *a_td;
1844 struct vnode *vp = ap->a_vp;
1847 lwkt_gettoken(&vlock, vp->v_interlock);
1848 if (vp->v_usecount > 1)
1850 lwkt_reltoken(&vlock);
1851 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
1855 * Read wrapper for fifos.
1859 struct vop_read_args /* {
1863 struct ucred *a_cred;
1871 resid = uio->uio_resid;
1872 error = VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap);
1873 ip = VTOI(ap->a_vp);
1874 if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL &&
1875 (uio->uio_resid != resid || (error == 0 && resid != 0)))
1876 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1881 * Write wrapper for fifos.
1885 struct vop_write_args /* {
1889 struct ucred *a_cred;
1897 resid = uio->uio_resid;
1898 error = VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap);
1899 ip = VTOI(ap->a_vp);
1900 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1901 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1906 * Close wrapper for fifos.
1908 * Update the times on the inode then do device close.
1912 struct vop_close_args /* {
1915 struct ucred *a_cred;
1916 struct thread *a_td;
1919 struct vnode *vp = ap->a_vp;
1922 lwkt_gettoken(&vlock, vp->v_interlock);
1923 if (vp->v_usecount > 1)
1925 lwkt_reltoken(&vlock);
1926 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
1930 * Kqfilter wrapper for fifos.
1932 * Fall through to ufs kqfilter routines if needed
1935 ufsfifo_kqfilter(ap)
1936 struct vop_kqfilter_args *ap;
1940 error = VOCALL(fifo_vnodeop_p, VOFFSET(vop_kqfilter), ap);
1942 error = ufs_kqfilter(ap);
1947 * Return POSIX pathconf information applicable to ufs filesystems.
1951 struct vop_pathconf_args /* {
1958 switch (ap->a_name) {
1960 *ap->a_retval = LINK_MAX;
1963 *ap->a_retval = NAME_MAX;
1966 *ap->a_retval = PATH_MAX;
1969 *ap->a_retval = PIPE_BUF;
1971 case _PC_CHOWN_RESTRICTED:
1984 * Advisory record locking support
1988 struct vop_advlock_args /* {
1996 struct inode *ip = VTOI(ap->a_vp);
1998 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
2002 * Initialize the vnode associated with a new inode, handle aliased
2006 ufs_vinit(mntp, specops, fifoops, vpp)
2018 switch(vp->v_type = IFTOVT(ip->i_mode)) {
2022 addaliasu(vp, ip->i_rdev);
2031 if (ip->i_number == ROOTINO)
2032 vp->v_flag |= VROOT;
2034 * Initialize modrev times
2036 getmicrouptime(&tv);
2037 SETHIGH(ip->i_modrev, tv.tv_sec);
2038 SETLOW(ip->i_modrev, tv.tv_usec * 4294);
2044 * Allocate a new inode.
2047 ufs_makeinode(mode, dvp, vpp, cnp)
2051 struct componentname *cnp;
2053 struct inode *ip, *pdir;
2054 struct direct newdir;
2060 if ((cnp->cn_flags & CNP_HASBUF) == 0)
2061 panic("ufs_makeinode: no name");
2064 if ((mode & IFMT) == 0)
2067 error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2071 ip->i_gid = pdir->i_gid;
2075 struct ucred ucred, *ucp;
2079 * If we are not the owner of the directory,
2080 * and we are hacking owners here, (only do this where told to)
2081 * and we are not giving it TO root, (would subvert quotas)
2082 * then go ahead and give it to the other user.
2083 * Note that this drops off the execute bits for security.
2085 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2086 (pdir->i_mode & ISUID) &&
2087 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2088 ip->i_uid = pdir->i_uid;
2092 * Make sure the correct user gets charged
2094 * Quickly knock up a dummy credential for the victim.
2095 * XXX This seems to never be accessed out of our
2096 * context so a stack variable is ok.
2099 ucred.cr_uid = ip->i_uid;
2100 ucred.cr_ngroups = 1;
2101 ucred.cr_groups[0] = pdir->i_gid;
2105 ip->i_uid = cnp->cn_cred->cr_uid;
2108 if ((error = getinoquota(ip)) ||
2109 (error = chkiq(ip, 1, ucp, 0))) {
2110 UFS_VFREE(tvp, ip->i_number, mode);
2116 #else /* !SUIDDIR */
2117 ip->i_uid = cnp->cn_cred->cr_uid;
2119 if ((error = getinoquota(ip)) ||
2120 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2121 UFS_VFREE(tvp, ip->i_number, mode);
2126 #endif /* !SUIDDIR */
2127 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2129 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2132 if (DOINGSOFTDEP(tvp))
2133 softdep_change_linkcnt(ip);
2134 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2135 suser_cred(cnp->cn_cred, 0)) {
2136 ip->i_mode &= ~ISGID;
2139 if (cnp->cn_flags & CNP_ISWHITEOUT)
2140 ip->i_flags |= UF_OPAQUE;
2143 * Make sure inode goes to disk before directory entry.
2145 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2148 ufs_makedirentry(ip, cnp, &newdir);
2149 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2157 * Write error occurred trying to update the inode
2158 * or the directory so must deallocate the inode.
2162 ip->i_flag |= IN_CHANGE;
2163 if (DOINGSOFTDEP(tvp))
2164 softdep_change_linkcnt(ip);
2171 struct vop_generic_args *ap;
2174 panic("no vop function for %s in ufs child", ap->a_desc->vdesc_name);
2175 return (EOPNOTSUPP);
2178 static struct filterops ufsread_filtops =
2179 { 1, NULL, filt_ufsdetach, filt_ufsread };
2180 static struct filterops ufswrite_filtops =
2181 { 1, NULL, filt_ufsdetach, filt_ufswrite };
2182 static struct filterops ufsvnode_filtops =
2183 { 1, NULL, filt_ufsdetach, filt_ufsvnode };
2187 struct vop_kqfilter_args /* {
2192 struct vnode *vp = ap->a_vp;
2193 struct knote *kn = ap->a_kn;
2196 switch (kn->kn_filter) {
2198 kn->kn_fop = &ufsread_filtops;
2201 kn->kn_fop = &ufswrite_filtops;
2204 kn->kn_fop = &ufsvnode_filtops;
2210 kn->kn_hook = (caddr_t)vp;
2212 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2213 SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
2214 lwkt_reltoken(&ilock);
2220 filt_ufsdetach(struct knote *kn)
2222 struct vnode *vp = (struct vnode *)kn->kn_hook;
2225 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2226 SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
2227 kn, knote, kn_selnext);
2228 lwkt_reltoken(&ilock);
2233 filt_ufsread(struct knote *kn, long hint)
2235 struct vnode *vp = (struct vnode *)kn->kn_hook;
2236 struct inode *ip = VTOI(vp);
2239 * filesystem is gone, so set the EOF flag and schedule
2240 * the knote for deletion.
2242 if (hint == NOTE_REVOKE) {
2243 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2247 kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
2248 return (kn->kn_data != 0);
2253 filt_ufswrite(struct knote *kn, long hint)
2257 * filesystem is gone, so set the EOF flag and schedule
2258 * the knote for deletion.
2260 if (hint == NOTE_REVOKE)
2261 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2268 filt_ufsvnode(struct knote *kn, long hint)
2271 if (kn->kn_sfflags & hint)
2272 kn->kn_fflags |= hint;
2273 if (hint == NOTE_REVOKE) {
2274 kn->kn_flags |= EV_EOF;
2277 return (kn->kn_fflags != 0);
2280 /* Global vfs data structures for ufs. */
2281 static vop_t **ufs_vnodeop_p;
2282 static struct vnodeopv_entry_desc ufs_vnodeop_entries[] = {
2283 { &vop_default_desc, (vop_t *) vop_defaultop },
2284 { &vop_fsync_desc, (vop_t *) ufs_missingop },
2285 { &vop_read_desc, (vop_t *) ufs_missingop },
2286 { &vop_reallocblks_desc, (vop_t *) ufs_missingop },
2287 { &vop_write_desc, (vop_t *) ufs_missingop },
2288 { &vop_access_desc, (vop_t *) ufs_access },
2289 { &vop_advlock_desc, (vop_t *) ufs_advlock },
2290 { &vop_bmap_desc, (vop_t *) ufs_bmap },
2291 { &vop_cachedlookup_desc, (vop_t *) ufs_lookup },
2292 { &vop_close_desc, (vop_t *) ufs_close },
2293 { &vop_create_desc, (vop_t *) ufs_create },
2294 { &vop_getattr_desc, (vop_t *) ufs_getattr },
2295 { &vop_inactive_desc, (vop_t *) ufs_inactive },
2296 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
2297 { &vop_link_desc, (vop_t *) ufs_link },
2298 { &vop_lock_desc, (vop_t *) vop_stdlock },
2299 { &vop_lookup_desc, (vop_t *) vfs_cache_lookup },
2300 { &vop_mkdir_desc, (vop_t *) ufs_mkdir },
2301 { &vop_mknod_desc, (vop_t *) ufs_mknod },
2302 { &vop_mmap_desc, (vop_t *) ufs_mmap },
2303 { &vop_open_desc, (vop_t *) ufs_open },
2304 { &vop_pathconf_desc, (vop_t *) ufs_pathconf },
2305 { &vop_poll_desc, (vop_t *) vop_stdpoll },
2306 { &vop_kqfilter_desc, (vop_t *) ufs_kqfilter },
2307 { &vop_print_desc, (vop_t *) ufs_print },
2308 { &vop_readdir_desc, (vop_t *) ufs_readdir },
2309 { &vop_readlink_desc, (vop_t *) ufs_readlink },
2310 { &vop_reclaim_desc, (vop_t *) ufs_reclaim },
2311 { &vop_remove_desc, (vop_t *) ufs_remove },
2312 { &vop_rename_desc, (vop_t *) ufs_rename },
2313 { &vop_rmdir_desc, (vop_t *) ufs_rmdir },
2314 { &vop_setattr_desc, (vop_t *) ufs_setattr },
2315 { &vop_strategy_desc, (vop_t *) ufs_strategy },
2316 { &vop_symlink_desc, (vop_t *) ufs_symlink },
2317 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
2318 { &vop_whiteout_desc, (vop_t *) ufs_whiteout },
2321 static struct vnodeopv_desc ufs_vnodeop_opv_desc =
2322 { &ufs_vnodeop_p, ufs_vnodeop_entries };
2324 static vop_t **ufs_specop_p;
2325 static struct vnodeopv_entry_desc ufs_specop_entries[] = {
2326 { &vop_default_desc, (vop_t *) spec_vnoperate },
2327 { &vop_fsync_desc, (vop_t *) ufs_missingop },
2328 { &vop_access_desc, (vop_t *) ufs_access },
2329 { &vop_close_desc, (vop_t *) ufsspec_close },
2330 { &vop_getattr_desc, (vop_t *) ufs_getattr },
2331 { &vop_inactive_desc, (vop_t *) ufs_inactive },
2332 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
2333 { &vop_lock_desc, (vop_t *) vop_stdlock },
2334 { &vop_print_desc, (vop_t *) ufs_print },
2335 { &vop_read_desc, (vop_t *) ufsspec_read },
2336 { &vop_reclaim_desc, (vop_t *) ufs_reclaim },
2337 { &vop_setattr_desc, (vop_t *) ufs_setattr },
2338 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
2339 { &vop_write_desc, (vop_t *) ufsspec_write },
2342 static struct vnodeopv_desc ufs_specop_opv_desc =
2343 { &ufs_specop_p, ufs_specop_entries };
2345 static vop_t **ufs_fifoop_p;
2346 static struct vnodeopv_entry_desc ufs_fifoop_entries[] = {
2347 { &vop_default_desc, (vop_t *) fifo_vnoperate },
2348 { &vop_fsync_desc, (vop_t *) ufs_missingop },
2349 { &vop_access_desc, (vop_t *) ufs_access },
2350 { &vop_close_desc, (vop_t *) ufsfifo_close },
2351 { &vop_getattr_desc, (vop_t *) ufs_getattr },
2352 { &vop_inactive_desc, (vop_t *) ufs_inactive },
2353 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
2354 { &vop_kqfilter_desc, (vop_t *) ufsfifo_kqfilter },
2355 { &vop_lock_desc, (vop_t *) vop_stdlock },
2356 { &vop_print_desc, (vop_t *) ufs_print },
2357 { &vop_read_desc, (vop_t *) ufsfifo_read },
2358 { &vop_reclaim_desc, (vop_t *) ufs_reclaim },
2359 { &vop_setattr_desc, (vop_t *) ufs_setattr },
2360 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
2361 { &vop_write_desc, (vop_t *) ufsfifo_write },
2364 static struct vnodeopv_desc ufs_fifoop_opv_desc =
2365 { &ufs_fifoop_p, ufs_fifoop_entries };
2367 VNODEOP_SET(ufs_vnodeop_opv_desc);
2368 VNODEOP_SET(ufs_specop_opv_desc);
2369 VNODEOP_SET(ufs_fifoop_opv_desc);
2373 struct vop_generic_args /* {
2374 struct vnodeop_desc *a_desc;
2377 return (VOCALL(ufs_vnodeop_p, ap->a_desc->vdesc_offset, ap));
2381 ufs_vnoperatefifo(ap)
2382 struct vop_generic_args /* {
2383 struct vnodeop_desc *a_desc;
2386 return (VOCALL(ufs_fifoop_p, ap->a_desc->vdesc_offset, ap));
2390 ufs_vnoperatespec(ap)
2391 struct vop_generic_args /* {
2392 struct vnodeop_desc *a_desc;
2395 return (VOCALL(ufs_specop_p, ap->a_desc->vdesc_offset, ap));