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.25 2004/12/17 00:18:44 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, ".."
150 ufs_itimes(struct vnode *vp)
156 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
158 if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
159 ip->i_flag |= IN_LAZYMOD;
161 ip->i_flag |= IN_MODIFIED;
162 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
164 if (ip->i_flag & IN_ACCESS) {
165 ip->i_atime = ts.tv_sec;
166 ip->i_atimensec = ts.tv_nsec;
168 if (ip->i_flag & IN_UPDATE) {
169 ip->i_mtime = ts.tv_sec;
170 ip->i_mtimensec = ts.tv_nsec;
173 if (ip->i_flag & IN_CHANGE) {
174 ip->i_ctime = ts.tv_sec;
175 ip->i_ctimensec = ts.tv_nsec;
178 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
182 * Create a regular file
184 * ufs_create(struct vnode *a_dvp, struct vnode **a_vpp,
185 * struct componentname *a_cnp, struct vattr *a_vap)
189 ufs_create(struct vop_create_args *ap)
194 ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
195 ap->a_dvp, ap->a_vpp, ap->a_cnp);
198 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
205 * ufs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
206 * struct componentname *a_cnp, struct vattr *a_vap)
211 ufs_mknod(struct vop_mknod_args *ap)
213 struct vattr *vap = ap->a_vap;
214 struct vnode **vpp = ap->a_vpp;
219 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
220 ap->a_dvp, vpp, ap->a_cnp);
223 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
225 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
226 if (vap->va_rdev != VNOVAL) {
228 * Want to be able to use this to make badblock
229 * inodes, so don't truncate the dev number.
231 ip->i_rdev = vap->va_rdev;
234 * Remove inode, then reload it through VFS_VGET so it is
235 * checked to see if it is an alias of an existing entry in
238 (*vpp)->v_type = VNON;
239 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
242 error = VFS_VGET(ap->a_dvp->v_mount, ino, vpp);
255 * ufs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
256 * struct thread *a_td)
261 ufs_open(struct vop_open_args *ap)
264 * Files marked append-only must be opened for appending.
266 if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
267 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
275 * Update the times on the inode.
277 * ufs_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
278 * struct thread *a_td)
283 ufs_close(struct vop_close_args *ap)
285 struct vnode *vp = ap->a_vp;
287 if (vp->v_usecount > 1)
293 * ufs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
294 * struct thread *a_td)
298 ufs_access(struct vop_access_args *ap)
300 struct vnode *vp = ap->a_vp;
301 struct inode *ip = VTOI(vp);
302 struct ucred *cred = ap->a_cred;
303 mode_t mask, mode = ap->a_mode;
311 * Disallow write attempts on read-only filesystems;
312 * unless the file is a socket, fifo, or a block or
313 * character device resident on the filesystem.
316 switch (vp->v_type) {
320 if (vp->v_mount->mnt_flag & MNT_RDONLY)
323 if ((error = getinoquota(ip)) != 0)
332 /* If immutable bit set, nobody gets to write it. */
333 if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE))
336 /* Otherwise, user id 0 always gets access. */
337 if (cred->cr_uid == 0)
342 /* Otherwise, check the owner. */
343 if (cred->cr_uid == ip->i_uid) {
350 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
353 /* Otherwise, check the groups. */
354 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
355 if (ip->i_gid == *gp) {
362 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
365 /* Otherwise, check everyone else. */
372 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
376 * ufs_getattr(struct vnode *a_vp, struct vattr *a_vap,
377 * struct thread *a_td)
382 ufs_getattr(struct vop_getattr_args *ap)
384 struct vnode *vp = ap->a_vp;
385 struct inode *ip = VTOI(vp);
386 struct vattr *vap = ap->a_vap;
390 * Copy from inode table
392 vap->va_fsid = dev2udev(ip->i_dev);
393 vap->va_fileid = ip->i_number;
394 vap->va_mode = ip->i_mode & ~IFMT;
395 vap->va_nlink = VFSTOUFS(vp->v_mount)->um_i_effnlink_valid ?
396 ip->i_effnlink : ip->i_nlink;
397 vap->va_uid = ip->i_uid;
398 vap->va_gid = ip->i_gid;
399 vap->va_rdev = ip->i_rdev;
400 vap->va_size = ip->i_din.di_size;
401 vap->va_atime.tv_sec = ip->i_atime;
402 vap->va_atime.tv_nsec = ip->i_atimensec;
403 vap->va_mtime.tv_sec = ip->i_mtime;
404 vap->va_mtime.tv_nsec = ip->i_mtimensec;
405 vap->va_ctime.tv_sec = ip->i_ctime;
406 vap->va_ctime.tv_nsec = ip->i_ctimensec;
407 vap->va_flags = ip->i_flags;
408 vap->va_gen = ip->i_gen;
409 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
410 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
411 vap->va_type = IFTOVT(ip->i_mode);
412 vap->va_filerev = ip->i_modrev;
417 * Set attribute vnode op. called from several syscalls
419 * ufs_setattr(struct vnode *a_vp, struct vattr *a_vap,
420 * struct ucred *a_cred, struct thread *a_td)
424 ufs_setattr(struct vop_setattr_args *ap)
426 struct vattr *vap = ap->a_vap;
427 struct vnode *vp = ap->a_vp;
428 struct inode *ip = VTOI(vp);
429 struct ucred *cred = ap->a_cred;
433 * Check for unsettable attributes.
435 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
436 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
437 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
438 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
441 if (vap->va_flags != VNOVAL) {
442 if (vp->v_mount->mnt_flag & MNT_RDONLY)
444 if (cred->cr_uid != ip->i_uid &&
445 (error = suser_cred(cred, PRISON_ROOT)))
447 if ((cred->cr_uid == 0) && (cred->cr_prison == NULL)) {
449 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) &&
452 ip->i_flags = vap->va_flags;
455 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
456 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
458 ip->i_flags &= SF_SETTABLE;
459 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
461 ip->i_flag |= IN_CHANGE;
462 if (vap->va_flags & (IMMUTABLE | APPEND))
465 if (ip->i_flags & (IMMUTABLE | APPEND))
468 * Go through the fields and update iff not VNOVAL.
470 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
471 if (vp->v_mount->mnt_flag & MNT_RDONLY)
473 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred, ap->a_td)) != 0)
476 if (vap->va_size != VNOVAL) {
478 * Disallow write attempts on read-only filesystems;
479 * unless the file is a socket, fifo, or a block or
480 * character device resident on the filesystem.
482 switch (vp->v_type) {
487 if (vp->v_mount->mnt_flag & MNT_RDONLY)
493 if ((error = UFS_TRUNCATE(vp, vap->va_size, 0, cred, ap->a_td)) != 0)
497 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
498 if (vp->v_mount->mnt_flag & MNT_RDONLY)
500 if (cred->cr_uid != ip->i_uid &&
501 (error = suser_cred(cred, PRISON_ROOT)) &&
502 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
503 (error = VOP_ACCESS(vp, VWRITE, cred, ap->a_td))))
505 if (vap->va_atime.tv_sec != VNOVAL)
506 ip->i_flag |= IN_ACCESS;
507 if (vap->va_mtime.tv_sec != VNOVAL)
508 ip->i_flag |= IN_CHANGE | IN_UPDATE;
510 if (vap->va_atime.tv_sec != VNOVAL) {
511 ip->i_atime = vap->va_atime.tv_sec;
512 ip->i_atimensec = vap->va_atime.tv_nsec;
514 if (vap->va_mtime.tv_sec != VNOVAL) {
515 ip->i_mtime = vap->va_mtime.tv_sec;
516 ip->i_mtimensec = vap->va_mtime.tv_nsec;
518 error = UFS_UPDATE(vp, 0);
523 if (vap->va_mode != (mode_t)VNOVAL) {
524 if (vp->v_mount->mnt_flag & MNT_RDONLY)
526 error = ufs_chmod(vp, (int)vap->va_mode, cred, ap->a_td);
528 VN_KNOTE(vp, NOTE_ATTRIB);
533 * Change the mode on a file.
534 * Inode must be locked before calling.
537 ufs_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td)
539 struct inode *ip = VTOI(vp);
542 if (cred->cr_uid != ip->i_uid) {
543 error = suser_cred(cred, PRISON_ROOT);
548 if (vp->v_type != VDIR && (mode & S_ISTXT))
550 if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
553 ip->i_mode &= ~ALLPERMS;
554 ip->i_mode |= (mode & ALLPERMS);
555 ip->i_flag |= IN_CHANGE;
560 * Perform chown operation on inode ip;
561 * inode must be locked prior to call.
564 ufs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
567 struct inode *ip = VTOI(vp);
576 if (uid == (uid_t)VNOVAL)
578 if (gid == (gid_t)VNOVAL)
581 * If we don't own the file, are trying to change the owner
582 * of the file, or are not a member of the target group,
583 * the caller must be superuser or the call fails.
585 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
586 (gid != ip->i_gid && !(cred->cr_gid == gid ||
587 groupmember((gid_t)gid, cred)))) &&
588 (error = suser_cred(cred, PRISON_ROOT)))
593 if ((error = getinoquota(ip)) != 0)
596 dqrele(vp, ip->i_dquot[USRQUOTA]);
597 ip->i_dquot[USRQUOTA] = NODQUOT;
600 dqrele(vp, ip->i_dquot[GRPQUOTA]);
601 ip->i_dquot[GRPQUOTA] = NODQUOT;
603 change = ip->i_blocks;
604 (void) chkdq(ip, -change, cred, CHOWN);
605 (void) chkiq(ip, -1, cred, CHOWN);
606 for (i = 0; i < MAXQUOTAS; i++) {
607 dqrele(vp, ip->i_dquot[i]);
608 ip->i_dquot[i] = NODQUOT;
614 if ((error = getinoquota(ip)) == 0) {
616 dqrele(vp, ip->i_dquot[USRQUOTA]);
617 ip->i_dquot[USRQUOTA] = NODQUOT;
620 dqrele(vp, ip->i_dquot[GRPQUOTA]);
621 ip->i_dquot[GRPQUOTA] = NODQUOT;
623 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
624 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
627 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
629 for (i = 0; i < MAXQUOTAS; i++) {
630 dqrele(vp, ip->i_dquot[i]);
631 ip->i_dquot[i] = NODQUOT;
636 if (getinoquota(ip) == 0) {
638 dqrele(vp, ip->i_dquot[USRQUOTA]);
639 ip->i_dquot[USRQUOTA] = NODQUOT;
642 dqrele(vp, ip->i_dquot[GRPQUOTA]);
643 ip->i_dquot[GRPQUOTA] = NODQUOT;
645 (void) chkdq(ip, change, cred, FORCE|CHOWN);
646 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
647 (void) getinoquota(ip);
652 panic("ufs_chown: lost quota");
654 ip->i_flag |= IN_CHANGE;
655 if (cred->cr_uid != 0 && (ouid != uid || ogid != gid))
656 ip->i_mode &= ~(ISUID | ISGID);
663 * NB Currently unsupported.
665 * ufs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
666 * struct thread *a_td)
671 ufs_mmap(struct vop_mmap_args *ap)
677 * ufs_remove(struct vnode *a_dvp, struct vnode *a_vp,
678 * struct componentname *a_cnp)
682 ufs_remove(struct vop_remove_args *ap)
685 struct vnode *vp = ap->a_vp;
686 struct vnode *dvp = ap->a_dvp;
690 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
691 (VTOI(dvp)->i_flags & APPEND)) {
695 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
696 VN_KNOTE(vp, NOTE_DELETE);
697 VN_KNOTE(dvp, NOTE_WRITE);
705 * ufs_link(struct vnode *a_tdvp, struct vnode *a_vp,
706 * struct componentname *a_cnp)
710 ufs_link(struct vop_link_args *ap)
712 struct vnode *vp = ap->a_vp;
713 struct vnode *tdvp = ap->a_tdvp;
714 struct componentname *cnp = ap->a_cnp;
715 struct thread *td = cnp->cn_td;
717 struct direct newdir;
720 if (tdvp->v_mount != vp->v_mount) {
724 if (tdvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE, td))) {
728 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
732 if (ip->i_flags & (IMMUTABLE | APPEND)) {
738 ip->i_flag |= IN_CHANGE;
739 if (DOINGSOFTDEP(vp))
740 softdep_change_linkcnt(ip);
741 error = UFS_UPDATE(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
743 ufs_makedirentry(ip, cnp, &newdir);
744 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
750 ip->i_flag |= IN_CHANGE;
751 if (DOINGSOFTDEP(vp))
752 softdep_change_linkcnt(ip);
756 VOP_UNLOCK(vp, 0, td);
758 VN_KNOTE(vp, NOTE_LINK);
759 VN_KNOTE(tdvp, NOTE_WRITE);
764 * whiteout vnode call
766 * ufs_whiteout(struct vnode *a_dvp, struct componentname *a_cnp, int a_flags)
770 ufs_whiteout(struct vop_whiteout_args *ap)
772 struct vnode *dvp = ap->a_dvp;
773 struct componentname *cnp = ap->a_cnp;
774 struct direct newdir;
777 switch (ap->a_flags) {
779 /* 4.4 format directories support whiteout operations */
780 if (dvp->v_mount->mnt_maxsymlinklen > 0)
785 /* create a new directory whiteout */
787 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
788 panic("ufs_whiteout: old format filesystem");
792 newdir.d_namlen = cnp->cn_namelen;
793 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
794 newdir.d_type = DT_WHT;
795 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
799 /* remove an existing directory whiteout */
801 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
802 panic("ufs_whiteout: old format filesystem");
805 cnp->cn_flags &= ~CNP_DOWHITEOUT;
806 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
809 panic("ufs_whiteout: unknown op");
815 * Rename system call.
816 * rename("foo", "bar");
819 * link("foo", "bar");
821 * but ``atomically''. Can't do full commit without saving state in the
822 * inode on disk which isn't feasible at this time. Best we can do is
823 * always guarantee the target exists.
825 * Basic algorithm is:
827 * 1) Bump link count on source while we're linking it to the
828 * target. This also ensure the inode won't be deleted out
829 * from underneath us while we work (it may be truncated by
830 * a concurrent `trunc' or `open' for creation).
831 * 2) Link source to destination. If destination already exists,
833 * 3) Unlink source reference to inode if still around. If a
834 * directory was moved and the parent of the destination
835 * is different from the source, patch the ".." entry in the
838 * ufs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
839 * struct componentname *a_fcnp, struct vnode *a_tdvp,
840 * struct vnode *a_tvp, struct componentname *a_tcnp)
844 ufs_rename(struct vop_rename_args *ap)
846 struct vnode *tvp = ap->a_tvp;
847 struct vnode *tdvp = ap->a_tdvp;
848 struct vnode *fvp = ap->a_fvp;
849 struct vnode *fdvp = ap->a_fdvp;
850 struct componentname *tcnp = ap->a_tcnp;
851 struct componentname *fcnp = ap->a_fcnp;
852 struct thread *td = fcnp->cn_td;
853 struct inode *ip, *xp, *dp;
854 struct direct newdir;
855 ino_t oldparent = 0, newparent = 0;
856 int doingdirectory = 0;
857 int error = 0, ioflag;
860 * Check for cross-device rename.
862 if ((fvp->v_mount != tdvp->v_mount) ||
863 (tvp && (fvp->v_mount != tvp->v_mount))) {
877 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
878 (VTOI(tdvp)->i_flags & APPEND))) {
884 * Renaming a file to itself has no effect. The upper layers should
885 * not call us in that case. Temporarily just warn if they do.
888 printf("ufs_rename: fvp == tvp (can't happen)\n");
893 if ((error = vn_lock(fvp, LK_EXCLUSIVE, td)) != 0)
897 * Note: now that fvp is locked we have to be sure to unlock it before
898 * using the 'abortit' target.
902 if (ip->i_nlink >= LINK_MAX) {
903 VOP_UNLOCK(fvp, 0, td);
907 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
908 || (dp->i_flags & APPEND)) {
909 VOP_UNLOCK(fvp, 0, td);
913 if ((ip->i_mode & IFMT) == IFDIR) {
915 * Avoid ".", "..", and aliases of "." for obvious reasons.
917 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
918 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & CNP_ISDOTDOT ||
919 (ip->i_flag & IN_RENAME)) {
920 VOP_UNLOCK(fvp, 0, td);
924 ip->i_flag |= IN_RENAME;
925 oldparent = dp->i_number;
928 VN_KNOTE(fdvp, NOTE_WRITE); /* XXX right place? */
931 * fvp still locked. ip->i_flag has IN_RENAME set if doingdirectory.
932 * Cleanup fvp requirements so we can unlock it.
934 * tvp and tdvp are locked. tvp may be NULL. Now that dp and xp
935 * is setup we can use the 'bad' target if we unlock fvp. We cannot
936 * use the abortit target anymore because of IN_RENAME.
945 * 1) Bump link count while we're moving stuff
946 * around. If we crash somewhere before
947 * completing our work, the link count
948 * may be wrong, but correctable.
952 ip->i_flag |= IN_CHANGE;
953 if (DOINGSOFTDEP(fvp))
954 softdep_change_linkcnt(ip);
955 if ((error = UFS_UPDATE(fvp, !(DOINGSOFTDEP(fvp) |
956 DOINGASYNC(fvp)))) != 0) {
957 VOP_UNLOCK(fvp, 0, td);
962 * If ".." must be changed (ie the directory gets a new
963 * parent) then the source directory must not be in the
964 * directory heirarchy above the target, as this would
965 * orphan everything below the source directory. Also
966 * the user must have write permission in the source so
967 * as to be able to change "..". We must repeat the call
968 * to namei, as the parent directory is unlocked by the
969 * call to checkpath().
971 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_td);
972 VOP_UNLOCK(fvp, 0, td);
975 * We are now back to where we were in that fvp, fdvp are unlocked
976 * and tvp, tdvp are locked. tvp may be NULL. IN_RENAME may be
977 * set. Only the bad target or, if we clean up tvp and tdvp, the
978 * out target, may be used.
980 if (oldparent != dp->i_number)
981 newparent = dp->i_number;
982 if (doingdirectory && newparent) {
983 if (error) /* write access check above */
987 * Once we start messing with tvp and tdvp we cannot use the
988 * 'bad' target, only finish cleaning tdvp and tvp up and
989 * use the 'out' target.
991 * This cleans up tvp.
999 * This is a real mess. ufs_checkpath vput's the target
1000 * directory so retain an extra ref and note that tdvp will
1001 * lose its lock on return. This leaves us with one good
1002 * ref after ufs_checkpath returns.
1005 error = ufs_checkpath(ip, dp, tcnp->cn_cred);
1006 tcnp->cn_flags |= CNP_PDIRUNLOCK;
1013 * relookup no longer messes with tdvp's refs. tdvp must be
1014 * unlocked on entry and will be locked on a successful
1017 error = relookup(tdvp, &tvp, tcnp);
1019 if (tcnp->cn_flags & CNP_PDIRUNLOCK)
1025 KKASSERT((tcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
1032 * We are back to fvp, fdvp unlocked, tvp, tdvp locked. tvp may
1033 * be NULL (xp will also be NULL in that case), and IN_RENAME will
1034 * be set if doingdirectory. This means we can use the 'bad' target
1039 * 2) If target doesn't exist, link the target
1040 * to the source and unlink the source.
1041 * Otherwise, rewrite the target directory
1042 * entry to reference the source inode and
1043 * expunge the original entry's existence.
1046 if (dp->i_dev != ip->i_dev)
1047 panic("ufs_rename: EXDEV");
1049 * Account for ".." in new directory.
1050 * When source and destination have the same
1051 * parent we don't fool with the link count.
1053 if (doingdirectory && newparent) {
1054 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1060 dp->i_flag |= IN_CHANGE;
1061 if (DOINGSOFTDEP(tdvp))
1062 softdep_change_linkcnt(dp);
1063 error = UFS_UPDATE(tdvp, !(DOINGSOFTDEP(tdvp) |
1068 ufs_makedirentry(ip, tcnp, &newdir);
1069 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1071 if (doingdirectory && newparent) {
1074 dp->i_flag |= IN_CHANGE;
1075 if (DOINGSOFTDEP(tdvp))
1076 softdep_change_linkcnt(dp);
1077 (void)UFS_UPDATE(tdvp, 1);
1081 VN_KNOTE(tdvp, NOTE_WRITE);
1084 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1085 panic("ufs_rename: EXDEV");
1087 * Short circuit rename(foo, foo).
1089 if (xp->i_number == ip->i_number)
1090 panic("ufs_rename: same file");
1092 * If the parent directory is "sticky", then the user must
1093 * own the parent directory, or the destination of the rename,
1094 * otherwise the destination may not be changed (except by
1095 * root). This implements append-only directories.
1097 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
1098 tcnp->cn_cred->cr_uid != dp->i_uid &&
1099 xp->i_uid != tcnp->cn_cred->cr_uid) {
1104 * Target must be empty if a directory and have no links
1105 * to it. Also, ensure source and target are compatible
1106 * (both directories, or both not directories).
1108 * Purge the file or directory being replaced from the
1111 if ((xp->i_mode&IFMT) == IFDIR) {
1112 if ((xp->i_effnlink > 2) ||
1113 !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1117 if (!doingdirectory) {
1121 /* cache_purge removed - handled by VFS compat layer */
1122 } else if (doingdirectory == 0) {
1123 /* cache_purge removed - handled by VFS compat layer */
1129 * note: inode passed to ufs_dirrewrite() is 0 for a
1130 * non-directory file rename, 1 for a directory rename
1131 * in the same directory, and > 1 for an inode representing
1132 * the new directory.
1134 error = ufs_dirrewrite(dp, xp, ip->i_number,
1136 (doingdirectory && newparent) ?
1137 newparent : (ino_t)doingdirectory);
1140 if (doingdirectory) {
1143 if (DOINGSOFTDEP(tdvp))
1144 softdep_change_linkcnt(dp);
1147 if (DOINGSOFTDEP(tvp))
1148 softdep_change_linkcnt(xp);
1150 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1152 * Truncate inode. The only stuff left in the directory
1153 * is "." and "..". The "." reference is inconsequential
1154 * since we are quashing it. We have removed the "."
1155 * reference and the reference in the parent directory,
1156 * but there may be other hard links. The soft
1157 * dependency code will arrange to do these operations
1158 * after the parent directory entry has been deleted on
1159 * disk, so when running with that code we avoid doing
1164 dp->i_flag |= IN_CHANGE;
1167 xp->i_flag |= IN_CHANGE;
1168 ioflag = DOINGASYNC(tvp) ? 0 : IO_SYNC;
1169 if ((error = UFS_TRUNCATE(tvp, (off_t)0, ioflag,
1170 tcnp->cn_cred, tcnp->cn_td)) != 0)
1173 VN_KNOTE(tdvp, NOTE_WRITE);
1175 VN_KNOTE(tvp, NOTE_DELETE);
1181 * tvp and tdvp have been cleaned up. only fvp and fdvp (both
1182 * unlocked) remain. We are about to overwrite fvp but we have to
1183 * keep 'ip' intact so we cannot release the old fvp, which is still
1184 * refd and accessible via ap->a_fvp.
1186 * This means we cannot use either 'bad' or 'out' to cleanup any
1191 * 3) Unlink the source.
1193 fcnp->cn_flags &= ~CNP_MODMASK;
1194 fcnp->cn_flags |= CNP_LOCKPARENT;
1195 error = relookup(fdvp, &fvp, fcnp);
1196 if (error || fvp == NULL) {
1198 * From name has disappeared. IN_RENAME will not be set if
1199 * we get past the panic so we don't have to clean it up.
1202 panic("ufs_rename: lost dir entry");
1204 if (fcnp->cn_flags & CNP_PDIRUNLOCK)
1210 KKASSERT((fcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
1213 * fdvp and fvp are locked.
1219 * Ensure that the directory entry still exists and has not
1220 * changed while the new name has been entered. If the source is
1221 * a file then the entry may have been unlinked or renamed. In
1222 * either case there is no further work to be done. If the source
1223 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1224 * flag ensures that it cannot be moved by another rename or removed
1225 * by a rmdir. Cleanup IN_RENAME.
1229 panic("ufs_rename: lost dir entry");
1232 * If the source is a directory with a
1233 * new parent, the link count of the old
1234 * parent directory must be decremented
1235 * and ".." set to point to the new parent.
1237 if (doingdirectory && newparent) {
1238 xp->i_offset = mastertemplate.dot_reclen;
1239 ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1240 /* cache_purge removed - handled by VFS compat layer */
1242 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1243 xp->i_flag &= ~IN_RENAME;
1246 VN_KNOTE(fvp, NOTE_RENAME);
1258 ip->i_flag &= ~IN_RENAME;
1259 if (vn_lock(fvp, LK_EXCLUSIVE, td) == 0) {
1262 ip->i_flag |= IN_CHANGE;
1263 ip->i_flag &= ~IN_RENAME;
1264 if (DOINGSOFTDEP(fvp))
1265 softdep_change_linkcnt(ip);
1276 * ufs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1277 * struct componentname *a_cnp, struct vattr *a_vap)
1281 ufs_mkdir(struct vop_mkdir_args *ap)
1283 struct vnode *dvp = ap->a_dvp;
1284 struct vattr *vap = ap->a_vap;
1285 struct componentname *cnp = ap->a_cnp;
1286 struct inode *ip, *dp;
1289 struct dirtemplate dirtemplate, *dtp;
1290 struct direct newdir;
1295 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1299 dmode = vap->va_mode & 0777;
1302 * Must simulate part of ufs_makeinode here to acquire the inode,
1303 * but not have it entered in the parent directory. The entry is
1304 * made later after writing "." and ".." entries.
1306 error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
1310 ip->i_gid = dp->i_gid;
1314 struct ucred ucred, *ucp;
1318 * If we are hacking owners here, (only do this where told to)
1319 * and we are not giving it TO root, (would subvert quotas)
1320 * then go ahead and give it to the other user.
1321 * The new directory also inherits the SUID bit.
1322 * If user's UID and dir UID are the same,
1323 * 'give it away' so that the SUID is still forced on.
1325 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1326 (dp->i_mode & ISUID) && dp->i_uid) {
1328 ip->i_uid = dp->i_uid;
1330 if (dp->i_uid != cnp->cn_cred->cr_uid) {
1332 * Make sure the correct user gets charged
1334 * Make a dummy credential for the victim.
1335 * XXX This seems to never be accessed out of
1336 * our context so a stack variable is ok.
1339 ucred.cr_uid = ip->i_uid;
1340 ucred.cr_ngroups = 1;
1341 ucred.cr_groups[0] = dp->i_gid;
1346 ip->i_uid = cnp->cn_cred->cr_uid;
1348 if ((error = getinoquota(ip)) ||
1349 (error = chkiq(ip, 1, ucp, 0))) {
1350 UFS_VFREE(tvp, ip->i_number, dmode);
1356 #else /* !SUIDDIR */
1357 ip->i_uid = cnp->cn_cred->cr_uid;
1359 if ((error = getinoquota(ip)) ||
1360 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1361 UFS_VFREE(tvp, ip->i_number, dmode);
1366 #endif /* !SUIDDIR */
1367 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1369 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1372 if (DOINGSOFTDEP(tvp))
1373 softdep_change_linkcnt(ip);
1374 if (cnp->cn_flags & CNP_ISWHITEOUT)
1375 ip->i_flags |= UF_OPAQUE;
1378 * Bump link count in parent directory to reflect work done below.
1379 * Should be done before reference is created so cleanup is
1380 * possible if we crash.
1384 dp->i_flag |= IN_CHANGE;
1385 if (DOINGSOFTDEP(dvp))
1386 softdep_change_linkcnt(dp);
1387 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1392 * Initialize directory with "." and ".." from static template.
1394 if (dvp->v_mount->mnt_maxsymlinklen > 0
1396 dtp = &mastertemplate;
1398 dtp = (struct dirtemplate *)&omastertemplate;
1400 dirtemplate.dot_ino = ip->i_number;
1401 dirtemplate.dotdot_ino = dp->i_number;
1402 if ((error = VOP_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred,
1403 B_CLRBUF, &bp)) != 0)
1405 ip->i_size = DIRBLKSIZ;
1406 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1407 vnode_pager_setsize(tvp, (u_long)ip->i_size);
1408 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1409 if (DOINGSOFTDEP(tvp)) {
1411 * Ensure that the entire newly allocated block is a
1412 * valid directory so that future growth within the
1413 * block does not have to ensure that the block is
1414 * written before the inode.
1417 while (blkoff < bp->b_bcount) {
1419 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1420 blkoff += DIRBLKSIZ;
1423 if ((error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) |
1424 DOINGASYNC(tvp)))) != 0) {
1425 (void)VOP_BWRITE(bp->b_vp, bp);
1429 * Directory set up, now install its entry in the parent directory.
1431 * If we are not doing soft dependencies, then we must write out the
1432 * buffer containing the new directory body before entering the new
1433 * name in the parent. If we are doing soft dependencies, then the
1434 * buffer containing the new directory body will be passed to and
1435 * released in the soft dependency code after the code has attached
1436 * an appropriate ordering dependency to the buffer which ensures that
1437 * the buffer is written before the new name is written in the parent.
1439 if (DOINGASYNC(dvp))
1441 else if (!DOINGSOFTDEP(dvp) && ((error = VOP_BWRITE(bp->b_vp, bp))))
1443 ufs_makedirentry(ip, cnp, &newdir);
1444 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1448 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1453 dp->i_flag |= IN_CHANGE;
1454 if (DOINGSOFTDEP(dvp))
1455 softdep_change_linkcnt(dp);
1457 * No need to do an explicit VOP_TRUNCATE here, vrele will
1458 * do this for us because we set the link count to 0.
1462 ip->i_flag |= IN_CHANGE;
1463 if (DOINGSOFTDEP(tvp))
1464 softdep_change_linkcnt(ip);
1472 * Rmdir system call.
1474 * ufs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1475 * struct componentname *a_cnp)
1479 ufs_rmdir(struct vop_rmdir_args *ap)
1481 struct vnode *vp = ap->a_vp;
1482 struct vnode *dvp = ap->a_dvp;
1483 struct componentname *cnp = ap->a_cnp;
1484 struct inode *ip, *dp;
1491 * Do not remove a directory that is in the process of being renamed.
1492 * Verify the directory is empty (and valid). Rmdir ".." will not be
1493 * valid since ".." will contain a reference to the current directory
1494 * and thus be non-empty. Do not allow the removal of mounted on
1495 * directories (this can happen when an NFS exported filesystem
1496 * tries to remove a locally mounted on directory).
1499 if (ip->i_flag & IN_RENAME) {
1503 if (ip->i_effnlink != 2 ||
1504 !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1508 if ((dp->i_flags & APPEND)
1509 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1513 if (vp->v_mountedhere != 0) {
1518 * Delete reference to directory before purging
1519 * inode. If we crash in between, the directory
1520 * will be reattached to lost+found,
1524 if (DOINGSOFTDEP(vp)) {
1525 softdep_change_linkcnt(dp);
1526 softdep_change_linkcnt(ip);
1528 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1532 if (DOINGSOFTDEP(vp)) {
1533 softdep_change_linkcnt(dp);
1534 softdep_change_linkcnt(ip);
1538 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1540 * Truncate inode. The only stuff left in the directory is "." and
1541 * "..". The "." reference is inconsequential since we are quashing
1542 * it. The soft dependency code will arrange to do these operations
1543 * after the parent directory entry has been deleted on disk, so
1544 * when running with that code we avoid doing them now.
1546 if (!DOINGSOFTDEP(vp)) {
1548 dp->i_flag |= IN_CHANGE;
1550 ip->i_flag |= IN_CHANGE;
1551 ioflag = DOINGASYNC(vp) ? 0 : IO_SYNC;
1552 error = UFS_TRUNCATE(vp, (off_t)0, ioflag, cnp->cn_cred,
1555 /* cache_purge removed - handled by VFS compat layer */
1557 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1558 if (ip->i_dirhash != NULL)
1559 ufsdirhash_free(ip);
1562 VN_KNOTE(vp, NOTE_DELETE);
1567 * symlink -- make a symbolic link
1569 * ufs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1570 * struct componentname *a_cnp, struct vattr *a_vap,
1575 ufs_symlink(struct vop_symlink_args *ap)
1577 struct vnode *vp, **vpp = ap->a_vpp;
1581 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1585 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1587 len = strlen(ap->a_target);
1588 if (len < vp->v_mount->mnt_maxsymlinklen) {
1590 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1592 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1594 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1595 UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred,
1603 * Vnode op for reading directories.
1605 * The routine below assumes that the on-disk format of a directory
1606 * is the same as that defined by <sys/dirent.h>. If the on-disk
1607 * format changes, then it will be necessary to do a conversion
1608 * from the on-disk format that read returns to the format defined
1609 * by <sys/dirent.h>.
1611 * ufs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
1612 * int *a_eofflag, int *ncookies, u_long **a_cookies)
1616 ufs_readdir(struct vop_readdir_args *ap)
1618 struct uio *uio = ap->a_uio;
1623 if (ap->a_ncookies != NULL)
1625 * Ensure that the block is aligned. The caller can use
1626 * the cookies to determine where in the block to start.
1628 uio->uio_offset &= ~(DIRBLKSIZ - 1);
1629 off = uio->uio_offset;
1630 count = uio->uio_resid;
1631 /* Make sure we don't return partial entries. */
1632 if (count <= ((uio->uio_offset + count) & (DIRBLKSIZ -1)))
1634 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
1635 lost = uio->uio_resid - count;
1636 uio->uio_resid = count;
1637 uio->uio_iov->iov_len = count;
1638 # if (BYTE_ORDER == LITTLE_ENDIAN)
1639 if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) {
1640 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1642 struct dirent *dp, *edp;
1650 auio.uio_iov = &aiov;
1651 auio.uio_iovcnt = 1;
1652 auio.uio_segflg = UIO_SYSSPACE;
1653 aiov.iov_len = count;
1654 MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
1655 aiov.iov_base = dirbuf;
1656 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
1658 readcnt = count - auio.uio_resid;
1659 edp = (struct dirent *)&dirbuf[readcnt];
1660 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
1662 dp->d_namlen = dp->d_type;
1664 if (dp->d_reclen > 0) {
1665 dp = (struct dirent *)
1666 ((char *)dp + dp->d_reclen);
1673 error = uiomove(dirbuf, readcnt, uio);
1675 FREE(dirbuf, M_TEMP);
1678 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1680 if (!error && ap->a_ncookies != NULL) {
1681 struct dirent* dpStart;
1682 struct dirent* dpEnd;
1688 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1689 panic("ufs_readdir: unexpected uio from NFS server");
1690 dpStart = (struct dirent *)
1691 (uio->uio_iov->iov_base - (uio->uio_offset - off));
1692 dpEnd = (struct dirent *) uio->uio_iov->iov_base;
1693 for (dp = dpStart, ncookies = 0;
1695 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen))
1697 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1699 for (dp = dpStart, cookiep = cookies;
1701 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
1702 off += dp->d_reclen;
1703 *cookiep++ = (u_long) off;
1705 *ap->a_ncookies = ncookies;
1706 *ap->a_cookies = cookies;
1708 uio->uio_resid += lost;
1710 *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
1715 * Return target name of a symbolic link
1717 * ufs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1721 ufs_readlink(struct vop_readlink_args *ap)
1723 struct vnode *vp = ap->a_vp;
1724 struct inode *ip = VTOI(vp);
1728 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1729 (ip->i_din.di_blocks == 0)) { /* XXX - for old fastlink support */
1730 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1733 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1737 * Calculate the logical to physical mapping if not done already,
1738 * then call the device strategy routine.
1740 * In order to be able to swap to a file, the VOP_BMAP operation may not
1741 * deadlock on memory. See ufs_bmap() for details.
1743 * ufs_strategy(struct vnode *a_vp, struct buf *a_bp)
1747 ufs_strategy(struct vop_strategy_args *ap)
1749 struct buf *bp = ap->a_bp;
1750 struct vnode *vp = ap->a_vp;
1755 if (vp->v_type == VBLK || vp->v_type == VCHR)
1756 panic("ufs_strategy: spec");
1757 if (bp->b_blkno == bp->b_lblkno) {
1758 error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1760 bp->b_error = error;
1761 bp->b_flags |= B_ERROR;
1765 if (bp->b_blkno == (daddr_t)-1)
1768 if (bp->b_blkno == (daddr_t)-1) {
1773 bp->b_dev = vp->v_rdev;
1774 VOP_STRATEGY(vp, bp);
1779 * Print out the contents of an inode.
1781 * ufs_print(struct vnode *a_vp)
1785 ufs_print(struct vop_print_args *ap)
1787 struct vnode *vp = ap->a_vp;
1788 struct inode *ip = VTOI(vp);
1790 printf("tag VT_UFS, ino %lu, on dev %s (%d, %d)",
1791 (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev),
1793 if (vp->v_type == VFIFO)
1795 lockmgr_printinfo(&vp->v_lock);
1801 * Read wrapper for special devices.
1803 * ufsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1804 * struct ucred *a_cred)
1808 ufsspec_read(struct vop_read_args *ap)
1815 resid = uio->uio_resid;
1816 error = VOCALL(spec_vnode_vops, &ap->a_head);
1818 * The inode may have been revoked during the call, so it must not
1819 * be accessed blindly here or in the other wrapper functions.
1821 ip = VTOI(ap->a_vp);
1822 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1823 ip->i_flag |= IN_ACCESS;
1828 * Write wrapper for special devices.
1830 * ufsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1831 * struct ucred *a_cred)
1835 ufsspec_write(struct vop_write_args *ap)
1842 resid = uio->uio_resid;
1843 error = VOCALL(spec_vnode_vops, &ap->a_head);
1844 ip = VTOI(ap->a_vp);
1845 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1846 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1851 * Close wrapper for special devices.
1853 * Update the times on the inode then do device close.
1855 * ufsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
1856 * struct thread *a_td)
1860 ufsspec_close(struct vop_close_args *ap)
1862 struct vnode *vp = ap->a_vp;
1864 if (vp->v_usecount > 1)
1866 return (VOCALL(spec_vnode_vops, &ap->a_head));
1870 * Read wrapper for fifos.
1872 * ufsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1873 * struct ucred *a_cred)
1877 ufsfifo_read(struct vop_read_args *ap)
1884 resid = uio->uio_resid;
1885 error = VOCALL(fifo_vnode_vops, &ap->a_head);
1886 ip = VTOI(ap->a_vp);
1887 if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL &&
1888 (uio->uio_resid != resid || (error == 0 && resid != 0)))
1889 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1894 * Write wrapper for fifos.
1896 * ufsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1897 * struct ucred *a_cred)
1901 ufsfifo_write(struct vop_write_args *ap)
1908 resid = uio->uio_resid;
1909 error = VOCALL(fifo_vnode_vops, &ap->a_head);
1910 ip = VTOI(ap->a_vp);
1911 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1912 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1917 * Close wrapper for fifos.
1919 * Update the times on the inode then do device close.
1921 * ufsfifo_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
1922 * struct thread *a_td)
1926 ufsfifo_close(struct vop_close_args *ap)
1928 struct vnode *vp = ap->a_vp;
1930 if (vp->v_usecount > 1)
1932 return (VOCALL(fifo_vnode_vops, &ap->a_head));
1936 * Kqfilter wrapper for fifos.
1938 * Fall through to ufs kqfilter routines if needed
1942 ufsfifo_kqfilter(struct vop_kqfilter_args *ap)
1946 error = VOCALL(fifo_vnode_vops, &ap->a_head);
1948 error = ufs_kqfilter(ap);
1953 * Return POSIX pathconf information applicable to ufs filesystems.
1955 * ufs_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
1959 ufs_pathconf(struct vop_pathconf_args *ap)
1961 switch (ap->a_name) {
1963 *ap->a_retval = LINK_MAX;
1966 *ap->a_retval = NAME_MAX;
1969 *ap->a_retval = PATH_MAX;
1972 *ap->a_retval = PIPE_BUF;
1974 case _PC_CHOWN_RESTRICTED:
1987 * Advisory record locking support
1989 * ufs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
1994 ufs_advlock(struct vop_advlock_args *ap)
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(struct mount *mntp, struct vnode **vpp)
2014 switch(vp->v_type = IFTOVT(ip->i_mode)) {
2017 vp->v_ops = &mntp->mnt_vn_spec_ops;
2018 addaliasu(vp, ip->i_rdev);
2021 vp->v_ops = &mntp->mnt_vn_fifo_ops;
2027 if (ip->i_number == ROOTINO)
2028 vp->v_flag |= VROOT;
2030 * Initialize modrev times
2032 getmicrouptime(&tv);
2033 SETHIGH(ip->i_modrev, tv.tv_sec);
2034 SETLOW(ip->i_modrev, tv.tv_usec * 4294);
2040 * Allocate a new inode.
2044 ufs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
2045 struct componentname *cnp)
2047 struct inode *ip, *pdir;
2048 struct direct newdir;
2054 if ((mode & IFMT) == 0)
2057 error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2061 ip->i_gid = pdir->i_gid;
2065 struct ucred ucred, *ucp;
2069 * If we are not the owner of the directory,
2070 * and we are hacking owners here, (only do this where told to)
2071 * and we are not giving it TO root, (would subvert quotas)
2072 * then go ahead and give it to the other user.
2073 * Note that this drops off the execute bits for security.
2075 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2076 (pdir->i_mode & ISUID) &&
2077 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2078 ip->i_uid = pdir->i_uid;
2082 * Make sure the correct user gets charged
2084 * Quickly knock up a dummy credential for the victim.
2085 * XXX This seems to never be accessed out of our
2086 * context so a stack variable is ok.
2089 ucred.cr_uid = ip->i_uid;
2090 ucred.cr_ngroups = 1;
2091 ucred.cr_groups[0] = pdir->i_gid;
2095 ip->i_uid = cnp->cn_cred->cr_uid;
2098 if ((error = getinoquota(ip)) ||
2099 (error = chkiq(ip, 1, ucp, 0))) {
2100 UFS_VFREE(tvp, ip->i_number, mode);
2106 #else /* !SUIDDIR */
2107 ip->i_uid = cnp->cn_cred->cr_uid;
2109 if ((error = getinoquota(ip)) ||
2110 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2111 UFS_VFREE(tvp, ip->i_number, mode);
2116 #endif /* !SUIDDIR */
2117 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2119 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2122 if (DOINGSOFTDEP(tvp))
2123 softdep_change_linkcnt(ip);
2124 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2125 suser_cred(cnp->cn_cred, 0)) {
2126 ip->i_mode &= ~ISGID;
2129 if (cnp->cn_flags & CNP_ISWHITEOUT)
2130 ip->i_flags |= UF_OPAQUE;
2133 * Make sure inode goes to disk before directory entry.
2135 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2138 ufs_makedirentry(ip, cnp, &newdir);
2139 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2147 * Write error occurred trying to update the inode
2148 * or the directory so must deallocate the inode.
2152 ip->i_flag |= IN_CHANGE;
2153 if (DOINGSOFTDEP(tvp))
2154 softdep_change_linkcnt(ip);
2160 ufs_missingop(struct vop_generic_args *ap)
2162 panic("no vop function for %s in ufs child", ap->a_desc->vdesc_name);
2163 return (EOPNOTSUPP);
2166 static struct filterops ufsread_filtops =
2167 { 1, NULL, filt_ufsdetach, filt_ufsread };
2168 static struct filterops ufswrite_filtops =
2169 { 1, NULL, filt_ufsdetach, filt_ufswrite };
2170 static struct filterops ufsvnode_filtops =
2171 { 1, NULL, filt_ufsdetach, filt_ufsvnode };
2174 * ufs_kqfilter(struct vnode *a_vp, struct knote *a_kn)
2177 ufs_kqfilter(struct vop_kqfilter_args *ap)
2179 struct vnode *vp = ap->a_vp;
2180 struct knote *kn = ap->a_kn;
2183 switch (kn->kn_filter) {
2185 kn->kn_fop = &ufsread_filtops;
2188 kn->kn_fop = &ufswrite_filtops;
2191 kn->kn_fop = &ufsvnode_filtops;
2197 kn->kn_hook = (caddr_t)vp;
2199 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2200 SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
2201 lwkt_reltoken(&ilock);
2207 filt_ufsdetach(struct knote *kn)
2209 struct vnode *vp = (struct vnode *)kn->kn_hook;
2212 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2213 SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
2214 kn, knote, kn_selnext);
2215 lwkt_reltoken(&ilock);
2220 filt_ufsread(struct knote *kn, long hint)
2222 struct vnode *vp = (struct vnode *)kn->kn_hook;
2223 struct inode *ip = VTOI(vp);
2226 * filesystem is gone, so set the EOF flag and schedule
2227 * the knote for deletion.
2229 if (hint == NOTE_REVOKE) {
2230 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2234 kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
2235 return (kn->kn_data != 0);
2240 filt_ufswrite(struct knote *kn, long hint)
2243 * filesystem is gone, so set the EOF flag and schedule
2244 * the knote for deletion.
2246 if (hint == NOTE_REVOKE)
2247 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2254 filt_ufsvnode(struct knote *kn, long hint)
2256 if (kn->kn_sfflags & hint)
2257 kn->kn_fflags |= hint;
2258 if (hint == NOTE_REVOKE) {
2259 kn->kn_flags |= EV_EOF;
2262 return (kn->kn_fflags != 0);
2265 /* Global vfs data structures for ufs. */
2266 static struct vop_ops *ufs_vnode_vops;
2267 static struct vnodeopv_entry_desc ufs_vnodeop_entries[] = {
2268 { &vop_default_desc, vop_defaultop },
2269 { &vop_fsync_desc, (void *) ufs_missingop },
2270 { &vop_read_desc, (void *) ufs_missingop },
2271 { &vop_reallocblks_desc, (void *) ufs_missingop },
2272 { &vop_write_desc, (void *) ufs_missingop },
2273 { &vop_access_desc, (void *) ufs_access },
2274 { &vop_advlock_desc, (void *) ufs_advlock },
2275 { &vop_bmap_desc, (void *) ufs_bmap },
2276 { &vop_lookup_desc, (void *) ufs_lookup },
2277 { &vop_close_desc, (void *) ufs_close },
2278 { &vop_create_desc, (void *) ufs_create },
2279 { &vop_getattr_desc, (void *) ufs_getattr },
2280 { &vop_inactive_desc, (void *) ufs_inactive },
2281 { &vop_islocked_desc, (void *) vop_stdislocked },
2282 { &vop_link_desc, (void *) ufs_link },
2283 { &vop_lock_desc, (void *) vop_stdlock },
2284 { &vop_mkdir_desc, (void *) ufs_mkdir },
2285 { &vop_mknod_desc, (void *) ufs_mknod },
2286 { &vop_mmap_desc, (void *) ufs_mmap },
2287 { &vop_open_desc, (void *) ufs_open },
2288 { &vop_pathconf_desc, (void *) ufs_pathconf },
2289 { &vop_poll_desc, (void *) vop_stdpoll },
2290 { &vop_kqfilter_desc, (void *) ufs_kqfilter },
2291 { &vop_print_desc, (void *) ufs_print },
2292 { &vop_readdir_desc, (void *) ufs_readdir },
2293 { &vop_readlink_desc, (void *) ufs_readlink },
2294 { &vop_reclaim_desc, (void *) ufs_reclaim },
2295 { &vop_remove_desc, (void *) ufs_remove },
2296 { &vop_rename_desc, (void *) ufs_rename },
2297 { &vop_rmdir_desc, (void *) ufs_rmdir },
2298 { &vop_setattr_desc, (void *) ufs_setattr },
2299 { &vop_strategy_desc, (void *) ufs_strategy },
2300 { &vop_symlink_desc, (void *) ufs_symlink },
2301 { &vop_unlock_desc, (void *) vop_stdunlock },
2302 { &vop_whiteout_desc, (void *) ufs_whiteout },
2305 static struct vnodeopv_desc ufs_vnodeop_opv_desc =
2306 { &ufs_vnode_vops, ufs_vnodeop_entries };
2308 static struct vop_ops *ufs_spec_vops;
2309 static struct vnodeopv_entry_desc ufs_specop_entries[] = {
2310 { &vop_default_desc, (void *) spec_vnoperate },
2311 { &vop_fsync_desc, (void *) ufs_missingop },
2312 { &vop_access_desc, (void *) ufs_access },
2313 { &vop_close_desc, (void *) ufsspec_close },
2314 { &vop_getattr_desc, (void *) ufs_getattr },
2315 { &vop_inactive_desc, (void *) ufs_inactive },
2316 { &vop_islocked_desc, (void *) vop_stdislocked },
2317 { &vop_lock_desc, (void *) vop_stdlock },
2318 { &vop_print_desc, (void *) ufs_print },
2319 { &vop_read_desc, (void *) ufsspec_read },
2320 { &vop_reclaim_desc, (void *) ufs_reclaim },
2321 { &vop_setattr_desc, (void *) ufs_setattr },
2322 { &vop_unlock_desc, (void *) vop_stdunlock },
2323 { &vop_write_desc, (void *) ufsspec_write },
2326 static struct vnodeopv_desc ufs_specop_opv_desc =
2327 { &ufs_spec_vops, ufs_specop_entries };
2329 static struct vop_ops *ufs_fifo_vops;
2330 static struct vnodeopv_entry_desc ufs_fifoop_entries[] = {
2331 { &vop_default_desc, (void *) fifo_vnoperate },
2332 { &vop_fsync_desc, (void *) ufs_missingop },
2333 { &vop_access_desc, (void *) ufs_access },
2334 { &vop_close_desc, (void *) ufsfifo_close },
2335 { &vop_getattr_desc, (void *) ufs_getattr },
2336 { &vop_inactive_desc, (void *) ufs_inactive },
2337 { &vop_islocked_desc, (void *) vop_stdislocked },
2338 { &vop_kqfilter_desc, (void *) ufsfifo_kqfilter },
2339 { &vop_lock_desc, (void *) vop_stdlock },
2340 { &vop_print_desc, (void *) ufs_print },
2341 { &vop_read_desc, (void *) ufsfifo_read },
2342 { &vop_reclaim_desc, (void *) ufs_reclaim },
2343 { &vop_setattr_desc, (void *) ufs_setattr },
2344 { &vop_unlock_desc, (void *) vop_stdunlock },
2345 { &vop_write_desc, (void *) ufsfifo_write },
2348 static struct vnodeopv_desc ufs_fifoop_opv_desc =
2349 { &ufs_fifo_vops, ufs_fifoop_entries };
2351 VNODEOP_SET(ufs_vnodeop_opv_desc);
2352 VNODEOP_SET(ufs_specop_opv_desc);
2353 VNODEOP_SET(ufs_fifoop_opv_desc);
2356 * ufs_vnoperate(struct vnodeop_desc *a_desc)
2359 ufs_vnoperate(struct vop_generic_args *ap)
2361 return (VOCALL(ufs_vnode_vops, ap));
2365 * ufs_vnoperatefifo(struct vnodeop_desc *a_desc)
2368 ufs_vnoperatefifo(struct vop_generic_args *ap)
2370 return (VOCALL(ufs_fifo_vops, ap));
2374 * ufs_vnoperatespec(struct vnodeop_desc *a_desc)
2377 ufs_vnoperatespec(struct vop_generic_args *ap)
2379 return (VOCALL(ufs_spec_vops, ap));