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.29 2005/08/07 19:16:15 joerg 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 */
68 #include <vm/vm_extern.h>
70 #include <vfs/fifofs/fifo.h>
76 #include "ufs_extern.h"
81 static int ufs_access (struct vop_access_args *);
82 static int ufs_advlock (struct vop_advlock_args *);
83 static int ufs_chmod (struct vnode *, int, struct ucred *, struct thread *);
84 static int ufs_chown (struct vnode *, uid_t, gid_t, struct ucred *, struct thread *);
85 static int ufs_close (struct vop_close_args *);
86 static int ufs_create (struct vop_create_args *);
87 static int ufs_getattr (struct vop_getattr_args *);
88 static int ufs_link (struct vop_link_args *);
89 static int ufs_makeinode (int mode, struct vnode *, struct vnode **, struct componentname *);
90 static int ufs_missingop (struct vop_generic_args *ap);
91 static int ufs_mkdir (struct vop_mkdir_args *);
92 static int ufs_mknod (struct vop_mknod_args *);
93 static int ufs_mmap (struct vop_mmap_args *);
94 static int ufs_open (struct vop_open_args *);
95 static int ufs_pathconf (struct vop_pathconf_args *);
96 static int ufs_print (struct vop_print_args *);
97 static int ufs_readdir (struct vop_readdir_args *);
98 static int ufs_readlink (struct vop_readlink_args *);
99 static int ufs_remove (struct vop_remove_args *);
100 static int ufs_rename (struct vop_rename_args *);
101 static int ufs_rmdir (struct vop_rmdir_args *);
102 static int ufs_setattr (struct vop_setattr_args *);
103 static int ufs_strategy (struct vop_strategy_args *);
104 static int ufs_symlink (struct vop_symlink_args *);
105 static int ufs_whiteout (struct vop_whiteout_args *);
106 static int ufsfifo_close (struct vop_close_args *);
107 static int ufsfifo_kqfilter (struct vop_kqfilter_args *);
108 static int ufsfifo_read (struct vop_read_args *);
109 static int ufsfifo_write (struct vop_write_args *);
110 static int ufsspec_close (struct vop_close_args *);
111 static int ufsspec_read (struct vop_read_args *);
112 static int ufsspec_write (struct vop_write_args *);
113 static int filt_ufsread (struct knote *kn, long hint);
114 static int filt_ufswrite (struct knote *kn, long hint);
115 static int filt_ufsvnode (struct knote *kn, long hint);
116 static void filt_ufsdetach (struct knote *kn);
117 static int ufs_kqfilter (struct vop_kqfilter_args *ap);
123 #define SETHIGH(q, h) { \
126 tmp.val[_QUAD_HIGHWORD] = (h); \
129 #define SETLOW(q, l) { \
132 tmp.val[_QUAD_LOWWORD] = (l); \
135 #define VN_KNOTE(vp, b) \
136 KNOTE(&vp->v_pollinfo.vpi_selinfo.si_note, (b))
138 #define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0)
141 * A virgin directory (no blushing please).
143 static struct dirtemplate mastertemplate = {
144 0, 12, DT_DIR, 1, ".",
145 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
147 static struct odirtemplate omastertemplate = {
149 0, DIRBLKSIZ - 12, 2, ".."
153 ufs_itimes(struct vnode *vp)
159 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
161 if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
162 ip->i_flag |= IN_LAZYMOD;
164 ip->i_flag |= IN_MODIFIED;
165 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
167 if (ip->i_flag & IN_ACCESS) {
168 ip->i_atime = ts.tv_sec;
169 ip->i_atimensec = ts.tv_nsec;
171 if (ip->i_flag & IN_UPDATE) {
172 ip->i_mtime = ts.tv_sec;
173 ip->i_mtimensec = ts.tv_nsec;
176 if (ip->i_flag & IN_CHANGE) {
177 ip->i_ctime = ts.tv_sec;
178 ip->i_ctimensec = ts.tv_nsec;
181 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
185 * Create a regular file
187 * ufs_create(struct vnode *a_dvp, struct vnode **a_vpp,
188 * struct componentname *a_cnp, struct vattr *a_vap)
192 ufs_create(struct vop_create_args *ap)
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);
208 * ufs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
209 * struct componentname *a_cnp, struct vattr *a_vap)
214 ufs_mknod(struct vop_mknod_args *ap)
216 struct vattr *vap = ap->a_vap;
217 struct vnode **vpp = ap->a_vpp;
222 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
223 ap->a_dvp, vpp, ap->a_cnp);
226 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
228 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
229 if (vap->va_rdev != VNOVAL) {
231 * Want to be able to use this to make badblock
232 * inodes, so don't truncate the dev number.
234 ip->i_rdev = vap->va_rdev;
237 * Remove inode, then reload it through VFS_VGET so it is
238 * checked to see if it is an alias of an existing entry in
241 (*vpp)->v_type = VNON;
242 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
245 error = VFS_VGET(ap->a_dvp->v_mount, ino, vpp);
258 * ufs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
259 * struct thread *a_td)
264 ufs_open(struct vop_open_args *ap)
267 * Files marked append-only must be opened for appending.
269 if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
270 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
278 * Update the times on the inode.
280 * ufs_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
281 * struct thread *a_td)
286 ufs_close(struct vop_close_args *ap)
288 struct vnode *vp = ap->a_vp;
290 if (vp->v_usecount > 1)
296 * ufs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
297 * struct thread *a_td)
301 ufs_access(struct vop_access_args *ap)
303 struct vnode *vp = ap->a_vp;
304 struct inode *ip = VTOI(vp);
305 struct ucred *cred = ap->a_cred;
306 mode_t mask, mode = ap->a_mode;
314 * Disallow write attempts on read-only filesystems;
315 * unless the file is a socket, fifo, or a block or
316 * character device resident on the filesystem.
319 switch (vp->v_type) {
323 if (vp->v_mount->mnt_flag & MNT_RDONLY)
326 if ((error = getinoquota(ip)) != 0)
335 /* If immutable bit set, nobody gets to write it. */
336 if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE))
339 /* Otherwise, user id 0 always gets access. */
340 if (cred->cr_uid == 0)
345 /* Otherwise, check the owner. */
346 if (cred->cr_uid == ip->i_uid) {
353 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
356 /* Otherwise, check the groups. */
357 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
358 if (ip->i_gid == *gp) {
365 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
368 /* Otherwise, check everyone else. */
375 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
379 * ufs_getattr(struct vnode *a_vp, struct vattr *a_vap,
380 * struct thread *a_td)
385 ufs_getattr(struct vop_getattr_args *ap)
387 struct vnode *vp = ap->a_vp;
388 struct inode *ip = VTOI(vp);
389 struct vattr *vap = ap->a_vap;
393 * Copy from inode table
395 vap->va_fsid = dev2udev(ip->i_dev);
396 vap->va_fileid = ip->i_number;
397 vap->va_mode = ip->i_mode & ~IFMT;
398 vap->va_nlink = VFSTOUFS(vp->v_mount)->um_i_effnlink_valid ?
399 ip->i_effnlink : ip->i_nlink;
400 vap->va_uid = ip->i_uid;
401 vap->va_gid = ip->i_gid;
402 vap->va_rdev = ip->i_rdev;
403 vap->va_size = ip->i_din.di_size;
404 vap->va_atime.tv_sec = ip->i_atime;
405 vap->va_atime.tv_nsec = ip->i_atimensec;
406 vap->va_mtime.tv_sec = ip->i_mtime;
407 vap->va_mtime.tv_nsec = ip->i_mtimensec;
408 vap->va_ctime.tv_sec = ip->i_ctime;
409 vap->va_ctime.tv_nsec = ip->i_ctimensec;
410 vap->va_flags = ip->i_flags;
411 vap->va_gen = ip->i_gen;
412 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
413 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
414 vap->va_type = IFTOVT(ip->i_mode);
415 vap->va_filerev = ip->i_modrev;
420 * Set attribute vnode op. called from several syscalls
422 * ufs_setattr(struct vnode *a_vp, struct vattr *a_vap,
423 * struct ucred *a_cred, struct thread *a_td)
427 ufs_setattr(struct vop_setattr_args *ap)
429 struct vattr *vap = ap->a_vap;
430 struct vnode *vp = ap->a_vp;
431 struct inode *ip = VTOI(vp);
432 struct ucred *cred = ap->a_cred;
436 * Check for unsettable attributes.
438 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
439 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
440 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
441 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
444 if (vap->va_flags != VNOVAL) {
445 if (vp->v_mount->mnt_flag & MNT_RDONLY)
447 if (cred->cr_uid != ip->i_uid &&
448 (error = suser_cred(cred, PRISON_ROOT)))
451 * Note that a root chflags becomes a user chflags when
452 * we are jailed, unless the jail.chflags_allowed sysctl
455 if (cred->cr_uid == 0 &&
456 (!jailed(cred) || jail_chflags_allowed)) {
458 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) &&
461 ip->i_flags = vap->va_flags;
464 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
465 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
467 ip->i_flags &= SF_SETTABLE;
468 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
470 ip->i_flag |= IN_CHANGE;
471 if (vap->va_flags & (IMMUTABLE | APPEND))
474 if (ip->i_flags & (IMMUTABLE | APPEND))
477 * Go through the fields and update iff not VNOVAL.
479 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
480 if (vp->v_mount->mnt_flag & MNT_RDONLY)
482 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred, ap->a_td)) != 0)
485 if (vap->va_size != VNOVAL) {
487 * Disallow write attempts on read-only filesystems;
488 * unless the file is a socket, fifo, or a block or
489 * character device resident on the filesystem.
491 switch (vp->v_type) {
496 if (vp->v_mount->mnt_flag & MNT_RDONLY)
502 if ((error = UFS_TRUNCATE(vp, vap->va_size, 0, cred, ap->a_td)) != 0)
506 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
507 if (vp->v_mount->mnt_flag & MNT_RDONLY)
509 if (cred->cr_uid != ip->i_uid &&
510 (error = suser_cred(cred, PRISON_ROOT)) &&
511 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
512 (error = VOP_ACCESS(vp, VWRITE, cred, ap->a_td))))
514 if (vap->va_atime.tv_sec != VNOVAL)
515 ip->i_flag |= IN_ACCESS;
516 if (vap->va_mtime.tv_sec != VNOVAL)
517 ip->i_flag |= IN_CHANGE | IN_UPDATE;
519 if (vap->va_atime.tv_sec != VNOVAL) {
520 ip->i_atime = vap->va_atime.tv_sec;
521 ip->i_atimensec = vap->va_atime.tv_nsec;
523 if (vap->va_mtime.tv_sec != VNOVAL) {
524 ip->i_mtime = vap->va_mtime.tv_sec;
525 ip->i_mtimensec = vap->va_mtime.tv_nsec;
527 error = UFS_UPDATE(vp, 0);
532 if (vap->va_mode != (mode_t)VNOVAL) {
533 if (vp->v_mount->mnt_flag & MNT_RDONLY)
535 error = ufs_chmod(vp, (int)vap->va_mode, cred, ap->a_td);
537 VN_KNOTE(vp, NOTE_ATTRIB);
542 * Change the mode on a file.
543 * Inode must be locked before calling.
546 ufs_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td)
548 struct inode *ip = VTOI(vp);
551 if (cred->cr_uid != ip->i_uid) {
552 error = suser_cred(cred, PRISON_ROOT);
557 if (vp->v_type != VDIR && (mode & S_ISTXT))
559 if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
562 ip->i_mode &= ~ALLPERMS;
563 ip->i_mode |= (mode & ALLPERMS);
564 ip->i_flag |= IN_CHANGE;
569 * Perform chown operation on inode ip;
570 * inode must be locked prior to call.
573 ufs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
576 struct inode *ip = VTOI(vp);
585 if (uid == (uid_t)VNOVAL)
587 if (gid == (gid_t)VNOVAL)
590 * If we don't own the file, are trying to change the owner
591 * of the file, or are not a member of the target group,
592 * the caller must be superuser or the call fails.
594 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
595 (gid != ip->i_gid && !(cred->cr_gid == gid ||
596 groupmember((gid_t)gid, cred)))) &&
597 (error = suser_cred(cred, PRISON_ROOT)))
602 if ((error = getinoquota(ip)) != 0)
605 dqrele(vp, ip->i_dquot[USRQUOTA]);
606 ip->i_dquot[USRQUOTA] = NODQUOT;
609 dqrele(vp, ip->i_dquot[GRPQUOTA]);
610 ip->i_dquot[GRPQUOTA] = NODQUOT;
612 change = ip->i_blocks;
613 (void) chkdq(ip, -change, cred, CHOWN);
614 (void) chkiq(ip, -1, cred, CHOWN);
615 for (i = 0; i < MAXQUOTAS; i++) {
616 dqrele(vp, ip->i_dquot[i]);
617 ip->i_dquot[i] = NODQUOT;
623 if ((error = getinoquota(ip)) == 0) {
625 dqrele(vp, ip->i_dquot[USRQUOTA]);
626 ip->i_dquot[USRQUOTA] = NODQUOT;
629 dqrele(vp, ip->i_dquot[GRPQUOTA]);
630 ip->i_dquot[GRPQUOTA] = NODQUOT;
632 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
633 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
636 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
638 for (i = 0; i < MAXQUOTAS; i++) {
639 dqrele(vp, ip->i_dquot[i]);
640 ip->i_dquot[i] = NODQUOT;
645 if (getinoquota(ip) == 0) {
647 dqrele(vp, ip->i_dquot[USRQUOTA]);
648 ip->i_dquot[USRQUOTA] = NODQUOT;
651 dqrele(vp, ip->i_dquot[GRPQUOTA]);
652 ip->i_dquot[GRPQUOTA] = NODQUOT;
654 (void) chkdq(ip, change, cred, FORCE|CHOWN);
655 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
656 (void) getinoquota(ip);
661 panic("ufs_chown: lost quota");
663 ip->i_flag |= IN_CHANGE;
664 if (cred->cr_uid != 0 && (ouid != uid || ogid != gid))
665 ip->i_mode &= ~(ISUID | ISGID);
672 * NB Currently unsupported.
674 * ufs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
675 * struct thread *a_td)
680 ufs_mmap(struct vop_mmap_args *ap)
686 * ufs_remove(struct vnode *a_dvp, struct vnode *a_vp,
687 * struct componentname *a_cnp)
691 ufs_remove(struct vop_remove_args *ap)
694 struct vnode *vp = ap->a_vp;
695 struct vnode *dvp = ap->a_dvp;
699 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
700 (VTOI(dvp)->i_flags & APPEND)) {
704 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
705 VN_KNOTE(vp, NOTE_DELETE);
706 VN_KNOTE(dvp, NOTE_WRITE);
714 * ufs_link(struct vnode *a_tdvp, struct vnode *a_vp,
715 * struct componentname *a_cnp)
719 ufs_link(struct vop_link_args *ap)
721 struct vnode *vp = ap->a_vp;
722 struct vnode *tdvp = ap->a_tdvp;
723 struct componentname *cnp = ap->a_cnp;
724 struct thread *td = cnp->cn_td;
726 struct direct newdir;
729 if (tdvp->v_mount != vp->v_mount) {
733 if (tdvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE, td))) {
737 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
741 if (ip->i_flags & (IMMUTABLE | APPEND)) {
747 ip->i_flag |= IN_CHANGE;
748 if (DOINGSOFTDEP(vp))
749 softdep_change_linkcnt(ip);
750 error = UFS_UPDATE(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
752 ufs_makedirentry(ip, cnp, &newdir);
753 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
759 ip->i_flag |= IN_CHANGE;
760 if (DOINGSOFTDEP(vp))
761 softdep_change_linkcnt(ip);
765 VOP_UNLOCK(vp, 0, td);
767 VN_KNOTE(vp, NOTE_LINK);
768 VN_KNOTE(tdvp, NOTE_WRITE);
773 * whiteout vnode call
775 * ufs_whiteout(struct vnode *a_dvp, struct componentname *a_cnp, int a_flags)
779 ufs_whiteout(struct vop_whiteout_args *ap)
781 struct vnode *dvp = ap->a_dvp;
782 struct componentname *cnp = ap->a_cnp;
783 struct direct newdir;
786 switch (ap->a_flags) {
788 /* 4.4 format directories support whiteout operations */
789 if (dvp->v_mount->mnt_maxsymlinklen > 0)
794 /* create a new directory whiteout */
796 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
797 panic("ufs_whiteout: old format filesystem");
801 newdir.d_namlen = cnp->cn_namelen;
802 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
803 newdir.d_type = DT_WHT;
804 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
808 /* remove an existing directory whiteout */
810 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
811 panic("ufs_whiteout: old format filesystem");
814 cnp->cn_flags &= ~CNP_DOWHITEOUT;
815 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
818 panic("ufs_whiteout: unknown op");
824 * Rename system call.
825 * rename("foo", "bar");
828 * link("foo", "bar");
830 * but ``atomically''. Can't do full commit without saving state in the
831 * inode on disk which isn't feasible at this time. Best we can do is
832 * always guarantee the target exists.
834 * Basic algorithm is:
836 * 1) Bump link count on source while we're linking it to the
837 * target. This also ensure the inode won't be deleted out
838 * from underneath us while we work (it may be truncated by
839 * a concurrent `trunc' or `open' for creation).
840 * 2) Link source to destination. If destination already exists,
842 * 3) Unlink source reference to inode if still around. If a
843 * directory was moved and the parent of the destination
844 * is different from the source, patch the ".." entry in the
847 * ufs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
848 * struct componentname *a_fcnp, struct vnode *a_tdvp,
849 * struct vnode *a_tvp, struct componentname *a_tcnp)
853 ufs_rename(struct vop_rename_args *ap)
855 struct vnode *tvp = ap->a_tvp;
856 struct vnode *tdvp = ap->a_tdvp;
857 struct vnode *fvp = ap->a_fvp;
858 struct vnode *fdvp = ap->a_fdvp;
859 struct componentname *tcnp = ap->a_tcnp;
860 struct componentname *fcnp = ap->a_fcnp;
861 struct thread *td = fcnp->cn_td;
862 struct inode *ip, *xp, *dp;
863 struct direct newdir;
864 ino_t oldparent = 0, newparent = 0;
865 int doingdirectory = 0;
866 int error = 0, ioflag;
869 * Check for cross-device rename.
871 if ((fvp->v_mount != tdvp->v_mount) ||
872 (tvp && (fvp->v_mount != tvp->v_mount))) {
886 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
887 (VTOI(tdvp)->i_flags & APPEND))) {
893 * Renaming a file to itself has no effect. The upper layers should
894 * not call us in that case. Temporarily just warn if they do.
897 printf("ufs_rename: fvp == tvp (can't happen)\n");
902 if ((error = vn_lock(fvp, LK_EXCLUSIVE, td)) != 0)
906 * Note: now that fvp is locked we have to be sure to unlock it before
907 * using the 'abortit' target.
911 if (ip->i_nlink >= LINK_MAX) {
912 VOP_UNLOCK(fvp, 0, td);
916 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
917 || (dp->i_flags & APPEND)) {
918 VOP_UNLOCK(fvp, 0, td);
922 if ((ip->i_mode & IFMT) == IFDIR) {
924 * Avoid ".", "..", and aliases of "." for obvious reasons.
926 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
927 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & CNP_ISDOTDOT ||
928 (ip->i_flag & IN_RENAME)) {
929 VOP_UNLOCK(fvp, 0, td);
933 ip->i_flag |= IN_RENAME;
934 oldparent = dp->i_number;
937 VN_KNOTE(fdvp, NOTE_WRITE); /* XXX right place? */
940 * fvp still locked. ip->i_flag has IN_RENAME set if doingdirectory.
941 * Cleanup fvp requirements so we can unlock it.
943 * tvp and tdvp are locked. tvp may be NULL. Now that dp and xp
944 * is setup we can use the 'bad' target if we unlock fvp. We cannot
945 * use the abortit target anymore because of IN_RENAME.
954 * 1) Bump link count while we're moving stuff
955 * around. If we crash somewhere before
956 * completing our work, the link count
957 * may be wrong, but correctable.
961 ip->i_flag |= IN_CHANGE;
962 if (DOINGSOFTDEP(fvp))
963 softdep_change_linkcnt(ip);
964 if ((error = UFS_UPDATE(fvp, !(DOINGSOFTDEP(fvp) |
965 DOINGASYNC(fvp)))) != 0) {
966 VOP_UNLOCK(fvp, 0, td);
971 * If ".." must be changed (ie the directory gets a new
972 * parent) then the source directory must not be in the
973 * directory heirarchy above the target, as this would
974 * orphan everything below the source directory. Also
975 * the user must have write permission in the source so
976 * as to be able to change "..". We must repeat the call
977 * to namei, as the parent directory is unlocked by the
978 * call to checkpath().
980 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_td);
981 VOP_UNLOCK(fvp, 0, td);
984 * We are now back to where we were in that fvp, fdvp are unlocked
985 * and tvp, tdvp are locked. tvp may be NULL. IN_RENAME may be
986 * set. Only the bad target or, if we clean up tvp and tdvp, the
987 * out target, may be used.
989 if (oldparent != dp->i_number)
990 newparent = dp->i_number;
991 if (doingdirectory && newparent) {
992 if (error) /* write access check above */
996 * Once we start messing with tvp and tdvp we cannot use the
997 * 'bad' target, only finish cleaning tdvp and tvp up and
998 * use the 'out' target.
1000 * This cleans up tvp.
1008 * This is a real mess. ufs_checkpath vput's the target
1009 * directory so retain an extra ref and note that tdvp will
1010 * lose its lock on return. This leaves us with one good
1011 * ref after ufs_checkpath returns.
1014 error = ufs_checkpath(ip, dp, tcnp->cn_cred);
1015 tcnp->cn_flags |= CNP_PDIRUNLOCK;
1022 * relookup no longer messes with tdvp's refs. tdvp must be
1023 * unlocked on entry and will be locked on a successful
1026 error = relookup(tdvp, &tvp, tcnp);
1028 if (tcnp->cn_flags & CNP_PDIRUNLOCK)
1034 KKASSERT((tcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
1041 * We are back to fvp, fdvp unlocked, tvp, tdvp locked. tvp may
1042 * be NULL (xp will also be NULL in that case), and IN_RENAME will
1043 * be set if doingdirectory. This means we can use the 'bad' target
1048 * 2) If target doesn't exist, link the target
1049 * to the source and unlink the source.
1050 * Otherwise, rewrite the target directory
1051 * entry to reference the source inode and
1052 * expunge the original entry's existence.
1055 if (dp->i_dev != ip->i_dev)
1056 panic("ufs_rename: EXDEV");
1058 * Account for ".." in new directory.
1059 * When source and destination have the same
1060 * parent we don't fool with the link count.
1062 if (doingdirectory && newparent) {
1063 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1069 dp->i_flag |= IN_CHANGE;
1070 if (DOINGSOFTDEP(tdvp))
1071 softdep_change_linkcnt(dp);
1072 error = UFS_UPDATE(tdvp, !(DOINGSOFTDEP(tdvp) |
1077 ufs_makedirentry(ip, tcnp, &newdir);
1078 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1080 if (doingdirectory && newparent) {
1083 dp->i_flag |= IN_CHANGE;
1084 if (DOINGSOFTDEP(tdvp))
1085 softdep_change_linkcnt(dp);
1086 (void)UFS_UPDATE(tdvp, 1);
1090 VN_KNOTE(tdvp, NOTE_WRITE);
1093 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1094 panic("ufs_rename: EXDEV");
1096 * Short circuit rename(foo, foo).
1098 if (xp->i_number == ip->i_number)
1099 panic("ufs_rename: same file");
1101 * If the parent directory is "sticky", then the user must
1102 * own the parent directory, or the destination of the rename,
1103 * otherwise the destination may not be changed (except by
1104 * root). This implements append-only directories.
1106 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
1107 tcnp->cn_cred->cr_uid != dp->i_uid &&
1108 xp->i_uid != tcnp->cn_cred->cr_uid) {
1113 * Target must be empty if a directory and have no links
1114 * to it. Also, ensure source and target are compatible
1115 * (both directories, or both not directories).
1117 * Purge the file or directory being replaced from the
1120 if ((xp->i_mode&IFMT) == IFDIR) {
1121 if ((xp->i_effnlink > 2) ||
1122 !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1126 if (!doingdirectory) {
1130 /* cache_purge removed - handled by VFS compat layer */
1131 } else if (doingdirectory == 0) {
1132 /* cache_purge removed - handled by VFS compat layer */
1138 * note: inode passed to ufs_dirrewrite() is 0 for a
1139 * non-directory file rename, 1 for a directory rename
1140 * in the same directory, and > 1 for an inode representing
1141 * the new directory.
1143 error = ufs_dirrewrite(dp, xp, ip->i_number,
1145 (doingdirectory && newparent) ?
1146 newparent : (ino_t)doingdirectory);
1149 if (doingdirectory) {
1152 if (DOINGSOFTDEP(tdvp))
1153 softdep_change_linkcnt(dp);
1156 if (DOINGSOFTDEP(tvp))
1157 softdep_change_linkcnt(xp);
1159 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1161 * Truncate inode. The only stuff left in the directory
1162 * is "." and "..". The "." reference is inconsequential
1163 * since we are quashing it. We have removed the "."
1164 * reference and the reference in the parent directory,
1165 * but there may be other hard links. The soft
1166 * dependency code will arrange to do these operations
1167 * after the parent directory entry has been deleted on
1168 * disk, so when running with that code we avoid doing
1173 dp->i_flag |= IN_CHANGE;
1176 xp->i_flag |= IN_CHANGE;
1177 ioflag = DOINGASYNC(tvp) ? 0 : IO_SYNC;
1178 if ((error = UFS_TRUNCATE(tvp, (off_t)0, ioflag,
1179 tcnp->cn_cred, tcnp->cn_td)) != 0)
1182 VN_KNOTE(tdvp, NOTE_WRITE);
1184 VN_KNOTE(tvp, NOTE_DELETE);
1190 * tvp and tdvp have been cleaned up. only fvp and fdvp (both
1191 * unlocked) remain. We are about to overwrite fvp but we have to
1192 * keep 'ip' intact so we cannot release the old fvp, which is still
1193 * refd and accessible via ap->a_fvp.
1195 * This means we cannot use either 'bad' or 'out' to cleanup any
1200 * 3) Unlink the source.
1202 fcnp->cn_flags &= ~CNP_MODMASK;
1203 fcnp->cn_flags |= CNP_LOCKPARENT;
1204 error = relookup(fdvp, &fvp, fcnp);
1205 if (error || fvp == NULL) {
1207 * From name has disappeared. IN_RENAME will not be set if
1208 * we get past the panic so we don't have to clean it up.
1211 panic("ufs_rename: lost dir entry");
1213 if (fcnp->cn_flags & CNP_PDIRUNLOCK)
1219 KKASSERT((fcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
1222 * fdvp and fvp are locked.
1228 * Ensure that the directory entry still exists and has not
1229 * changed while the new name has been entered. If the source is
1230 * a file then the entry may have been unlinked or renamed. In
1231 * either case there is no further work to be done. If the source
1232 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1233 * flag ensures that it cannot be moved by another rename or removed
1234 * by a rmdir. Cleanup IN_RENAME.
1238 panic("ufs_rename: lost dir entry");
1241 * If the source is a directory with a
1242 * new parent, the link count of the old
1243 * parent directory must be decremented
1244 * and ".." set to point to the new parent.
1246 if (doingdirectory && newparent) {
1247 xp->i_offset = mastertemplate.dot_reclen;
1248 ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1249 /* cache_purge removed - handled by VFS compat layer */
1251 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1252 xp->i_flag &= ~IN_RENAME;
1255 VN_KNOTE(fvp, NOTE_RENAME);
1267 ip->i_flag &= ~IN_RENAME;
1268 if (vn_lock(fvp, LK_EXCLUSIVE, td) == 0) {
1271 ip->i_flag |= IN_CHANGE;
1272 ip->i_flag &= ~IN_RENAME;
1273 if (DOINGSOFTDEP(fvp))
1274 softdep_change_linkcnt(ip);
1285 * ufs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1286 * struct componentname *a_cnp, struct vattr *a_vap)
1290 ufs_mkdir(struct vop_mkdir_args *ap)
1292 struct vnode *dvp = ap->a_dvp;
1293 struct vattr *vap = ap->a_vap;
1294 struct componentname *cnp = ap->a_cnp;
1295 struct inode *ip, *dp;
1298 struct dirtemplate dirtemplate, *dtp;
1299 struct direct newdir;
1304 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1308 dmode = vap->va_mode & 0777;
1311 * Must simulate part of ufs_makeinode here to acquire the inode,
1312 * but not have it entered in the parent directory. The entry is
1313 * made later after writing "." and ".." entries.
1315 error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
1319 ip->i_gid = dp->i_gid;
1323 struct ucred ucred, *ucp;
1327 * If we are hacking owners here, (only do this where told to)
1328 * and we are not giving it TO root, (would subvert quotas)
1329 * then go ahead and give it to the other user.
1330 * The new directory also inherits the SUID bit.
1331 * If user's UID and dir UID are the same,
1332 * 'give it away' so that the SUID is still forced on.
1334 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1335 (dp->i_mode & ISUID) && dp->i_uid) {
1337 ip->i_uid = dp->i_uid;
1339 if (dp->i_uid != cnp->cn_cred->cr_uid) {
1341 * Make sure the correct user gets charged
1343 * Make a dummy credential for the victim.
1344 * XXX This seems to never be accessed out of
1345 * our context so a stack variable is ok.
1348 ucred.cr_uid = ip->i_uid;
1349 ucred.cr_ngroups = 1;
1350 ucred.cr_groups[0] = dp->i_gid;
1355 ip->i_uid = cnp->cn_cred->cr_uid;
1357 if ((error = getinoquota(ip)) ||
1358 (error = chkiq(ip, 1, ucp, 0))) {
1359 UFS_VFREE(tvp, ip->i_number, dmode);
1365 #else /* !SUIDDIR */
1366 ip->i_uid = cnp->cn_cred->cr_uid;
1368 if ((error = getinoquota(ip)) ||
1369 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1370 UFS_VFREE(tvp, ip->i_number, dmode);
1375 #endif /* !SUIDDIR */
1376 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1378 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1381 if (DOINGSOFTDEP(tvp))
1382 softdep_change_linkcnt(ip);
1383 if (cnp->cn_flags & CNP_ISWHITEOUT)
1384 ip->i_flags |= UF_OPAQUE;
1387 * Bump link count in parent directory to reflect work done below.
1388 * Should be done before reference is created so cleanup is
1389 * possible if we crash.
1393 dp->i_flag |= IN_CHANGE;
1394 if (DOINGSOFTDEP(dvp))
1395 softdep_change_linkcnt(dp);
1396 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1401 * Initialize directory with "." and ".." from static template.
1403 if (dvp->v_mount->mnt_maxsymlinklen > 0
1405 dtp = &mastertemplate;
1407 dtp = (struct dirtemplate *)&omastertemplate;
1409 dirtemplate.dot_ino = ip->i_number;
1410 dirtemplate.dotdot_ino = dp->i_number;
1411 if ((error = VOP_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred,
1412 B_CLRBUF, &bp)) != 0)
1414 ip->i_size = DIRBLKSIZ;
1415 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1416 vnode_pager_setsize(tvp, (u_long)ip->i_size);
1417 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1418 if (DOINGSOFTDEP(tvp)) {
1420 * Ensure that the entire newly allocated block is a
1421 * valid directory so that future growth within the
1422 * block does not have to ensure that the block is
1423 * written before the inode.
1426 while (blkoff < bp->b_bcount) {
1428 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1429 blkoff += DIRBLKSIZ;
1432 if ((error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) |
1433 DOINGASYNC(tvp)))) != 0) {
1434 (void)VOP_BWRITE(bp->b_vp, bp);
1438 * Directory set up, now install its entry in the parent directory.
1440 * If we are not doing soft dependencies, then we must write out the
1441 * buffer containing the new directory body before entering the new
1442 * name in the parent. If we are doing soft dependencies, then the
1443 * buffer containing the new directory body will be passed to and
1444 * released in the soft dependency code after the code has attached
1445 * an appropriate ordering dependency to the buffer which ensures that
1446 * the buffer is written before the new name is written in the parent.
1448 if (DOINGASYNC(dvp))
1450 else if (!DOINGSOFTDEP(dvp) && ((error = VOP_BWRITE(bp->b_vp, bp))))
1452 ufs_makedirentry(ip, cnp, &newdir);
1453 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1457 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1462 dp->i_flag |= IN_CHANGE;
1463 if (DOINGSOFTDEP(dvp))
1464 softdep_change_linkcnt(dp);
1466 * No need to do an explicit VOP_TRUNCATE here, vrele will
1467 * do this for us because we set the link count to 0.
1471 ip->i_flag |= IN_CHANGE;
1472 if (DOINGSOFTDEP(tvp))
1473 softdep_change_linkcnt(ip);
1481 * Rmdir system call.
1483 * ufs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1484 * struct componentname *a_cnp)
1488 ufs_rmdir(struct vop_rmdir_args *ap)
1490 struct vnode *vp = ap->a_vp;
1491 struct vnode *dvp = ap->a_dvp;
1492 struct componentname *cnp = ap->a_cnp;
1493 struct inode *ip, *dp;
1500 * Do not remove a directory that is in the process of being renamed.
1501 * Verify the directory is empty (and valid). Rmdir ".." will not be
1502 * valid since ".." will contain a reference to the current directory
1503 * and thus be non-empty. Do not allow the removal of mounted on
1504 * directories (this can happen when an NFS exported filesystem
1505 * tries to remove a locally mounted on directory).
1508 if (ip->i_flag & IN_RENAME) {
1512 if (ip->i_effnlink != 2 ||
1513 !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1517 if ((dp->i_flags & APPEND)
1518 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1522 if (vp->v_mountedhere != 0) {
1527 * Delete reference to directory before purging
1528 * inode. If we crash in between, the directory
1529 * will be reattached to lost+found,
1533 if (DOINGSOFTDEP(vp)) {
1534 softdep_change_linkcnt(dp);
1535 softdep_change_linkcnt(ip);
1537 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1541 if (DOINGSOFTDEP(vp)) {
1542 softdep_change_linkcnt(dp);
1543 softdep_change_linkcnt(ip);
1547 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1549 * Truncate inode. The only stuff left in the directory is "." and
1550 * "..". The "." reference is inconsequential since we are quashing
1551 * it. The soft dependency code will arrange to do these operations
1552 * after the parent directory entry has been deleted on disk, so
1553 * when running with that code we avoid doing them now.
1555 if (!DOINGSOFTDEP(vp)) {
1557 dp->i_flag |= IN_CHANGE;
1559 ip->i_flag |= IN_CHANGE;
1560 ioflag = DOINGASYNC(vp) ? 0 : IO_SYNC;
1561 error = UFS_TRUNCATE(vp, (off_t)0, ioflag, cnp->cn_cred,
1564 /* cache_purge removed - handled by VFS compat layer */
1566 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1567 if (ip->i_dirhash != NULL)
1568 ufsdirhash_free(ip);
1571 VN_KNOTE(vp, NOTE_DELETE);
1576 * symlink -- make a symbolic link
1578 * ufs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1579 * struct componentname *a_cnp, struct vattr *a_vap,
1584 ufs_symlink(struct vop_symlink_args *ap)
1586 struct vnode *vp, **vpp = ap->a_vpp;
1590 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1594 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1596 len = strlen(ap->a_target);
1597 if (len < vp->v_mount->mnt_maxsymlinklen) {
1599 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1601 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1603 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1604 UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred,
1612 * Vnode op for reading directories.
1614 * The routine below assumes that the on-disk format of a directory
1615 * is the same as that defined by <sys/dirent.h>. If the on-disk
1616 * format changes, then it will be necessary to do a conversion
1617 * from the on-disk format that read returns to the format defined
1618 * by <sys/dirent.h>.
1620 * ufs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
1621 * int *a_eofflag, int *ncookies, u_long **a_cookies)
1625 ufs_readdir(struct vop_readdir_args *ap)
1627 struct uio *uio = ap->a_uio;
1630 struct direct *edp, *dp;
1632 struct dirent dstdp;
1637 off_t startoffset = uio->uio_offset;
1639 count = uio->uio_resid;
1641 * Avoid complications for partial directory entries by adjusting
1642 * the i/o to end at a block boundary. Don't give up (like the old ufs
1643 * does) if the initial adjustment gives a negative count, since
1644 * many callers don't supply a large enough buffer. The correct
1645 * size is a little larger than DIRBLKSIZ to allow for expansion
1646 * of directory entries, but some callers just use 512.
1648 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
1653 auio.uio_iov = &aiov;
1654 auio.uio_iovcnt = 1;
1655 auio.uio_resid = count;
1656 auio.uio_segflg = UIO_SYSSPACE;
1657 aiov.iov_len = count;
1658 MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
1659 aiov.iov_base = dirbuf;
1660 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
1662 readcnt = count - auio.uio_resid;
1663 edp = (struct direct *)&dirbuf[readcnt];
1665 bzero(&dstdp, offsetof(struct dirent, d_name));
1666 for (dp = (struct direct *)dirbuf;
1667 !error && uio->uio_resid > 0 && dp < edp; ) {
1668 dstdp.d_fileno = dp->d_ino;
1669 #if BYTE_ORDER == LITTLE_ENDIAN
1670 if (OFSFMT(ap->a_vp)) {
1671 dstdp.d_namlen = dp->d_type;
1672 dstdp.d_type = dp->d_namlen;
1676 dstdp.d_namlen = dp->d_namlen;
1677 dstdp.d_type = dp->d_type;
1679 dstdp.d_reclen = GENERIC_DIRSIZ(&dstdp);
1680 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
1681 bzero(dstdp.d_name + dstdp.d_namlen,
1682 dstdp.d_reclen - offsetof(struct dirent, d_name) -
1685 if (dp->d_reclen > 0) {
1686 if(dstdp.d_reclen <= uio->uio_resid) {
1688 dp = (struct direct *)
1689 ((char *)dp + dp->d_reclen);
1691 uiomove((caddr_t)&dstdp,
1692 dstdp.d_reclen, uio);
1702 /* we need to correct uio_offset */
1703 uio->uio_offset = startoffset + (caddr_t)dp - dirbuf;
1705 if (!error && ap->a_ncookies != NULL) {
1706 u_long *cookiep, *cookies, *ecookies;
1709 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1710 panic("ufs_readdir: unexpected uio from NFS server");
1711 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1714 for (dp = (struct direct *)dirbuf,
1715 cookiep = cookies, ecookies = cookies + ncookies;
1717 dp = (struct direct *)((caddr_t) dp + dp->d_reclen)) {
1718 off += dp->d_reclen;
1719 *cookiep++ = (u_long) off;
1721 *ap->a_ncookies = ncookies;
1722 *ap->a_cookies = cookies;
1725 FREE(dirbuf, M_TEMP);
1727 *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
1732 * Return target name of a symbolic link
1734 * ufs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1738 ufs_readlink(struct vop_readlink_args *ap)
1740 struct vnode *vp = ap->a_vp;
1741 struct inode *ip = VTOI(vp);
1745 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1746 (ip->i_din.di_blocks == 0)) { /* XXX - for old fastlink support */
1747 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1750 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1754 * Calculate the logical to physical mapping if not done already,
1755 * then call the device strategy routine.
1757 * In order to be able to swap to a file, the VOP_BMAP operation may not
1758 * deadlock on memory. See ufs_bmap() for details.
1760 * ufs_strategy(struct vnode *a_vp, struct buf *a_bp)
1764 ufs_strategy(struct vop_strategy_args *ap)
1766 struct buf *bp = ap->a_bp;
1767 struct vnode *vp = ap->a_vp;
1772 if (vp->v_type == VBLK || vp->v_type == VCHR)
1773 panic("ufs_strategy: spec");
1774 if (bp->b_blkno == bp->b_lblkno) {
1775 error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1777 bp->b_error = error;
1778 bp->b_flags |= B_ERROR;
1782 if (bp->b_blkno == (daddr_t)-1)
1785 if (bp->b_blkno == (daddr_t)-1) {
1790 bp->b_dev = vp->v_rdev;
1791 VOP_STRATEGY(vp, bp);
1796 * Print out the contents of an inode.
1798 * ufs_print(struct vnode *a_vp)
1802 ufs_print(struct vop_print_args *ap)
1804 struct vnode *vp = ap->a_vp;
1805 struct inode *ip = VTOI(vp);
1807 printf("tag VT_UFS, ino %lu, on dev %s (%d, %d)",
1808 (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev),
1810 if (vp->v_type == VFIFO)
1812 lockmgr_printinfo(&vp->v_lock);
1818 * Read wrapper for special devices.
1820 * ufsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1821 * struct ucred *a_cred)
1825 ufsspec_read(struct vop_read_args *ap)
1832 resid = uio->uio_resid;
1833 error = VOCALL(spec_vnode_vops, &ap->a_head);
1835 * The inode may have been revoked during the call, so it must not
1836 * be accessed blindly here or in the other wrapper functions.
1838 ip = VTOI(ap->a_vp);
1839 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1840 ip->i_flag |= IN_ACCESS;
1845 * Write wrapper for special devices.
1847 * ufsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1848 * struct ucred *a_cred)
1852 ufsspec_write(struct vop_write_args *ap)
1859 resid = uio->uio_resid;
1860 error = VOCALL(spec_vnode_vops, &ap->a_head);
1861 ip = VTOI(ap->a_vp);
1862 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1863 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1868 * Close wrapper for special devices.
1870 * Update the times on the inode then do device close.
1872 * ufsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
1873 * struct thread *a_td)
1877 ufsspec_close(struct vop_close_args *ap)
1879 struct vnode *vp = ap->a_vp;
1881 if (vp->v_usecount > 1)
1883 return (VOCALL(spec_vnode_vops, &ap->a_head));
1887 * Read wrapper for fifos.
1889 * ufsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1890 * struct ucred *a_cred)
1894 ufsfifo_read(struct vop_read_args *ap)
1901 resid = uio->uio_resid;
1902 error = VOCALL(fifo_vnode_vops, &ap->a_head);
1903 ip = VTOI(ap->a_vp);
1904 if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL &&
1905 (uio->uio_resid != resid || (error == 0 && resid != 0)))
1906 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1911 * Write wrapper for fifos.
1913 * ufsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1914 * struct ucred *a_cred)
1918 ufsfifo_write(struct vop_write_args *ap)
1925 resid = uio->uio_resid;
1926 error = VOCALL(fifo_vnode_vops, &ap->a_head);
1927 ip = VTOI(ap->a_vp);
1928 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1929 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1934 * Close wrapper for fifos.
1936 * Update the times on the inode then do device close.
1938 * ufsfifo_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
1939 * struct thread *a_td)
1943 ufsfifo_close(struct vop_close_args *ap)
1945 struct vnode *vp = ap->a_vp;
1947 if (vp->v_usecount > 1)
1949 return (VOCALL(fifo_vnode_vops, &ap->a_head));
1953 * Kqfilter wrapper for fifos.
1955 * Fall through to ufs kqfilter routines if needed
1959 ufsfifo_kqfilter(struct vop_kqfilter_args *ap)
1963 error = VOCALL(fifo_vnode_vops, &ap->a_head);
1965 error = ufs_kqfilter(ap);
1970 * Return POSIX pathconf information applicable to ufs filesystems.
1972 * ufs_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
1976 ufs_pathconf(struct vop_pathconf_args *ap)
1978 switch (ap->a_name) {
1980 *ap->a_retval = LINK_MAX;
1983 *ap->a_retval = NAME_MAX;
1986 *ap->a_retval = PATH_MAX;
1989 *ap->a_retval = PIPE_BUF;
1991 case _PC_CHOWN_RESTRICTED:
2004 * Advisory record locking support
2006 * ufs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
2011 ufs_advlock(struct vop_advlock_args *ap)
2013 struct inode *ip = VTOI(ap->a_vp);
2015 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
2019 * Initialize the vnode associated with a new inode, handle aliased
2023 ufs_vinit(struct mount *mntp, struct vnode **vpp)
2031 switch(vp->v_type = IFTOVT(ip->i_mode)) {
2034 vp->v_ops = &mntp->mnt_vn_spec_ops;
2035 addaliasu(vp, ip->i_rdev);
2038 vp->v_ops = &mntp->mnt_vn_fifo_ops;
2044 if (ip->i_number == ROOTINO)
2045 vp->v_flag |= VROOT;
2047 * Initialize modrev times
2049 getmicrouptime(&tv);
2050 SETHIGH(ip->i_modrev, tv.tv_sec);
2051 SETLOW(ip->i_modrev, tv.tv_usec * 4294);
2057 * Allocate a new inode.
2061 ufs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
2062 struct componentname *cnp)
2064 struct inode *ip, *pdir;
2065 struct direct newdir;
2071 if ((mode & IFMT) == 0)
2074 error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2078 ip->i_gid = pdir->i_gid;
2082 struct ucred ucred, *ucp;
2086 * If we are not the owner of the directory,
2087 * and we are hacking owners here, (only do this where told to)
2088 * and we are not giving it TO root, (would subvert quotas)
2089 * then go ahead and give it to the other user.
2090 * Note that this drops off the execute bits for security.
2092 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2093 (pdir->i_mode & ISUID) &&
2094 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2095 ip->i_uid = pdir->i_uid;
2099 * Make sure the correct user gets charged
2101 * Quickly knock up a dummy credential for the victim.
2102 * XXX This seems to never be accessed out of our
2103 * context so a stack variable is ok.
2106 ucred.cr_uid = ip->i_uid;
2107 ucred.cr_ngroups = 1;
2108 ucred.cr_groups[0] = pdir->i_gid;
2112 ip->i_uid = cnp->cn_cred->cr_uid;
2115 if ((error = getinoquota(ip)) ||
2116 (error = chkiq(ip, 1, ucp, 0))) {
2117 UFS_VFREE(tvp, ip->i_number, mode);
2123 #else /* !SUIDDIR */
2124 ip->i_uid = cnp->cn_cred->cr_uid;
2126 if ((error = getinoquota(ip)) ||
2127 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2128 UFS_VFREE(tvp, ip->i_number, mode);
2133 #endif /* !SUIDDIR */
2134 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2136 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2139 if (DOINGSOFTDEP(tvp))
2140 softdep_change_linkcnt(ip);
2141 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2142 suser_cred(cnp->cn_cred, 0)) {
2143 ip->i_mode &= ~ISGID;
2146 if (cnp->cn_flags & CNP_ISWHITEOUT)
2147 ip->i_flags |= UF_OPAQUE;
2150 * Make sure inode goes to disk before directory entry.
2152 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2155 ufs_makedirentry(ip, cnp, &newdir);
2156 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2164 * Write error occurred trying to update the inode
2165 * or the directory so must deallocate the inode.
2169 ip->i_flag |= IN_CHANGE;
2170 if (DOINGSOFTDEP(tvp))
2171 softdep_change_linkcnt(ip);
2177 ufs_missingop(struct vop_generic_args *ap)
2179 panic("no vop function for %s in ufs child", ap->a_desc->vdesc_name);
2180 return (EOPNOTSUPP);
2183 static struct filterops ufsread_filtops =
2184 { 1, NULL, filt_ufsdetach, filt_ufsread };
2185 static struct filterops ufswrite_filtops =
2186 { 1, NULL, filt_ufsdetach, filt_ufswrite };
2187 static struct filterops ufsvnode_filtops =
2188 { 1, NULL, filt_ufsdetach, filt_ufsvnode };
2191 * ufs_kqfilter(struct vnode *a_vp, struct knote *a_kn)
2194 ufs_kqfilter(struct vop_kqfilter_args *ap)
2196 struct vnode *vp = ap->a_vp;
2197 struct knote *kn = ap->a_kn;
2200 switch (kn->kn_filter) {
2202 kn->kn_fop = &ufsread_filtops;
2205 kn->kn_fop = &ufswrite_filtops;
2208 kn->kn_fop = &ufsvnode_filtops;
2214 kn->kn_hook = (caddr_t)vp;
2216 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2217 SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
2218 lwkt_reltoken(&ilock);
2224 filt_ufsdetach(struct knote *kn)
2226 struct vnode *vp = (struct vnode *)kn->kn_hook;
2229 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2230 SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
2231 kn, knote, kn_selnext);
2232 lwkt_reltoken(&ilock);
2237 filt_ufsread(struct knote *kn, long hint)
2239 struct vnode *vp = (struct vnode *)kn->kn_hook;
2240 struct inode *ip = VTOI(vp);
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);
2251 kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
2252 return (kn->kn_data != 0);
2257 filt_ufswrite(struct knote *kn, long hint)
2260 * filesystem is gone, so set the EOF flag and schedule
2261 * the knote for deletion.
2263 if (hint == NOTE_REVOKE)
2264 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2271 filt_ufsvnode(struct knote *kn, long hint)
2273 if (kn->kn_sfflags & hint)
2274 kn->kn_fflags |= hint;
2275 if (hint == NOTE_REVOKE) {
2276 kn->kn_flags |= EV_EOF;
2279 return (kn->kn_fflags != 0);
2282 /* Global vfs data structures for ufs. */
2283 static struct vop_ops *ufs_vnode_vops;
2284 static struct vnodeopv_entry_desc ufs_vnodeop_entries[] = {
2285 { &vop_default_desc, vop_defaultop },
2286 { &vop_fsync_desc, (vnodeopv_entry_t) ufs_missingop },
2287 { &vop_read_desc, (vnodeopv_entry_t) ufs_missingop },
2288 { &vop_reallocblks_desc, (vnodeopv_entry_t) ufs_missingop },
2289 { &vop_write_desc, (vnodeopv_entry_t) ufs_missingop },
2290 { &vop_access_desc, (vnodeopv_entry_t) ufs_access },
2291 { &vop_advlock_desc, (vnodeopv_entry_t) ufs_advlock },
2292 { &vop_bmap_desc, (vnodeopv_entry_t) ufs_bmap },
2293 { &vop_lookup_desc, (vnodeopv_entry_t) ufs_lookup },
2294 { &vop_close_desc, (vnodeopv_entry_t) ufs_close },
2295 { &vop_create_desc, (vnodeopv_entry_t) ufs_create },
2296 { &vop_getattr_desc, (vnodeopv_entry_t) ufs_getattr },
2297 { &vop_inactive_desc, (vnodeopv_entry_t) ufs_inactive },
2298 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
2299 { &vop_link_desc, (vnodeopv_entry_t) ufs_link },
2300 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
2301 { &vop_mkdir_desc, (vnodeopv_entry_t) ufs_mkdir },
2302 { &vop_mknod_desc, (vnodeopv_entry_t) ufs_mknod },
2303 { &vop_mmap_desc, (vnodeopv_entry_t) ufs_mmap },
2304 { &vop_open_desc, (vnodeopv_entry_t) ufs_open },
2305 { &vop_pathconf_desc, (vnodeopv_entry_t) ufs_pathconf },
2306 { &vop_poll_desc, (vnodeopv_entry_t) vop_stdpoll },
2307 { &vop_kqfilter_desc, (vnodeopv_entry_t) ufs_kqfilter },
2308 { &vop_print_desc, (vnodeopv_entry_t) ufs_print },
2309 { &vop_readdir_desc, (vnodeopv_entry_t) ufs_readdir },
2310 { &vop_readlink_desc, (vnodeopv_entry_t) ufs_readlink },
2311 { &vop_reclaim_desc, (vnodeopv_entry_t) ufs_reclaim },
2312 { &vop_remove_desc, (vnodeopv_entry_t) ufs_remove },
2313 { &vop_rename_desc, (vnodeopv_entry_t) ufs_rename },
2314 { &vop_rmdir_desc, (vnodeopv_entry_t) ufs_rmdir },
2315 { &vop_setattr_desc, (vnodeopv_entry_t) ufs_setattr },
2316 { &vop_strategy_desc, (vnodeopv_entry_t) ufs_strategy },
2317 { &vop_symlink_desc, (vnodeopv_entry_t) ufs_symlink },
2318 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
2319 { &vop_whiteout_desc, (vnodeopv_entry_t) ufs_whiteout },
2322 static struct vnodeopv_desc ufs_vnodeop_opv_desc =
2323 { &ufs_vnode_vops, ufs_vnodeop_entries };
2325 static struct vop_ops *ufs_spec_vops;
2326 static struct vnodeopv_entry_desc ufs_specop_entries[] = {
2327 { &vop_default_desc, (vnodeopv_entry_t) spec_vnoperate },
2328 { &vop_fsync_desc, (vnodeopv_entry_t) ufs_missingop },
2329 { &vop_access_desc, (vnodeopv_entry_t) ufs_access },
2330 { &vop_close_desc, (vnodeopv_entry_t) ufsspec_close },
2331 { &vop_getattr_desc, (vnodeopv_entry_t) ufs_getattr },
2332 { &vop_inactive_desc, (vnodeopv_entry_t) ufs_inactive },
2333 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
2334 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
2335 { &vop_print_desc, (vnodeopv_entry_t) ufs_print },
2336 { &vop_read_desc, (vnodeopv_entry_t) ufsspec_read },
2337 { &vop_reclaim_desc, (vnodeopv_entry_t) ufs_reclaim },
2338 { &vop_setattr_desc, (vnodeopv_entry_t) ufs_setattr },
2339 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
2340 { &vop_write_desc, (vnodeopv_entry_t) ufsspec_write },
2343 static struct vnodeopv_desc ufs_specop_opv_desc =
2344 { &ufs_spec_vops, ufs_specop_entries };
2346 static struct vop_ops *ufs_fifo_vops;
2347 static struct vnodeopv_entry_desc ufs_fifoop_entries[] = {
2348 { &vop_default_desc, (vnodeopv_entry_t) fifo_vnoperate },
2349 { &vop_fsync_desc, (vnodeopv_entry_t) ufs_missingop },
2350 { &vop_access_desc, (vnodeopv_entry_t) ufs_access },
2351 { &vop_close_desc, (vnodeopv_entry_t) ufsfifo_close },
2352 { &vop_getattr_desc, (vnodeopv_entry_t) ufs_getattr },
2353 { &vop_inactive_desc, (vnodeopv_entry_t) ufs_inactive },
2354 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
2355 { &vop_kqfilter_desc, (vnodeopv_entry_t) ufsfifo_kqfilter },
2356 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
2357 { &vop_print_desc, (vnodeopv_entry_t) ufs_print },
2358 { &vop_read_desc, (vnodeopv_entry_t) ufsfifo_read },
2359 { &vop_reclaim_desc, (vnodeopv_entry_t) ufs_reclaim },
2360 { &vop_setattr_desc, (vnodeopv_entry_t) ufs_setattr },
2361 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
2362 { &vop_write_desc, (vnodeopv_entry_t) ufsfifo_write },
2365 static struct vnodeopv_desc ufs_fifoop_opv_desc =
2366 { &ufs_fifo_vops, ufs_fifoop_entries };
2368 VNODEOP_SET(ufs_vnodeop_opv_desc);
2369 VNODEOP_SET(ufs_specop_opv_desc);
2370 VNODEOP_SET(ufs_fifoop_opv_desc);
2373 * ufs_vnoperate(struct vnodeop_desc *a_desc)
2376 ufs_vnoperate(struct vop_generic_args *ap)
2378 return (VOCALL(ufs_vnode_vops, ap));
2382 * ufs_vnoperatefifo(struct vnodeop_desc *a_desc)
2385 ufs_vnoperatefifo(struct vop_generic_args *ap)
2387 return (VOCALL(ufs_fifo_vops, ap));
2391 * ufs_vnoperatespec(struct vnodeop_desc *a_desc)
2394 ufs_vnoperatespec(struct vop_generic_args *ap)
2396 return (VOCALL(ufs_spec_vops, ap));