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.20 2004/08/28 19:02:30 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
239 (*vpp)->v_type = VNON;
240 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;
288 lwkt_gettoken(&vlock, vp->v_interlock);
289 if (vp->v_usecount > 1)
291 lwkt_reltoken(&vlock);
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)))
450 if ((cred->cr_uid == 0) && (cred->cr_prison == NULL)) {
452 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) &&
455 ip->i_flags = vap->va_flags;
458 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
459 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
461 ip->i_flags &= SF_SETTABLE;
462 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
464 ip->i_flag |= IN_CHANGE;
465 if (vap->va_flags & (IMMUTABLE | APPEND))
468 if (ip->i_flags & (IMMUTABLE | APPEND))
471 * Go through the fields and update iff not VNOVAL.
473 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
474 if (vp->v_mount->mnt_flag & MNT_RDONLY)
476 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred, ap->a_td)) != 0)
479 if (vap->va_size != VNOVAL) {
481 * Disallow write attempts on read-only filesystems;
482 * unless the file is a socket, fifo, or a block or
483 * character device resident on the filesystem.
485 switch (vp->v_type) {
490 if (vp->v_mount->mnt_flag & MNT_RDONLY)
496 if ((error = UFS_TRUNCATE(vp, vap->va_size, 0, cred, ap->a_td)) != 0)
500 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
501 if (vp->v_mount->mnt_flag & MNT_RDONLY)
503 if (cred->cr_uid != ip->i_uid &&
504 (error = suser_cred(cred, PRISON_ROOT)) &&
505 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
506 (error = VOP_ACCESS(vp, VWRITE, cred, ap->a_td))))
508 if (vap->va_atime.tv_sec != VNOVAL)
509 ip->i_flag |= IN_ACCESS;
510 if (vap->va_mtime.tv_sec != VNOVAL)
511 ip->i_flag |= IN_CHANGE | IN_UPDATE;
513 if (vap->va_atime.tv_sec != VNOVAL) {
514 ip->i_atime = vap->va_atime.tv_sec;
515 ip->i_atimensec = vap->va_atime.tv_nsec;
517 if (vap->va_mtime.tv_sec != VNOVAL) {
518 ip->i_mtime = vap->va_mtime.tv_sec;
519 ip->i_mtimensec = vap->va_mtime.tv_nsec;
521 error = UFS_UPDATE(vp, 0);
526 if (vap->va_mode != (mode_t)VNOVAL) {
527 if (vp->v_mount->mnt_flag & MNT_RDONLY)
529 error = ufs_chmod(vp, (int)vap->va_mode, cred, ap->a_td);
531 VN_KNOTE(vp, NOTE_ATTRIB);
536 * Change the mode on a file.
537 * Inode must be locked before calling.
540 ufs_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td)
542 struct inode *ip = VTOI(vp);
545 if (cred->cr_uid != ip->i_uid) {
546 error = suser_cred(cred, PRISON_ROOT);
551 if (vp->v_type != VDIR && (mode & S_ISTXT))
553 if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
556 ip->i_mode &= ~ALLPERMS;
557 ip->i_mode |= (mode & ALLPERMS);
558 ip->i_flag |= IN_CHANGE;
563 * Perform chown operation on inode ip;
564 * inode must be locked prior to call.
567 ufs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
570 struct inode *ip = VTOI(vp);
579 if (uid == (uid_t)VNOVAL)
581 if (gid == (gid_t)VNOVAL)
584 * If we don't own the file, are trying to change the owner
585 * of the file, or are not a member of the target group,
586 * the caller must be superuser or the call fails.
588 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
589 (gid != ip->i_gid && !(cred->cr_gid == gid ||
590 groupmember((gid_t)gid, cred)))) &&
591 (error = suser_cred(cred, PRISON_ROOT)))
596 if ((error = getinoquota(ip)) != 0)
599 dqrele(vp, ip->i_dquot[USRQUOTA]);
600 ip->i_dquot[USRQUOTA] = NODQUOT;
603 dqrele(vp, ip->i_dquot[GRPQUOTA]);
604 ip->i_dquot[GRPQUOTA] = NODQUOT;
606 change = ip->i_blocks;
607 (void) chkdq(ip, -change, cred, CHOWN);
608 (void) chkiq(ip, -1, cred, CHOWN);
609 for (i = 0; i < MAXQUOTAS; i++) {
610 dqrele(vp, ip->i_dquot[i]);
611 ip->i_dquot[i] = NODQUOT;
617 if ((error = getinoquota(ip)) == 0) {
619 dqrele(vp, ip->i_dquot[USRQUOTA]);
620 ip->i_dquot[USRQUOTA] = NODQUOT;
623 dqrele(vp, ip->i_dquot[GRPQUOTA]);
624 ip->i_dquot[GRPQUOTA] = NODQUOT;
626 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
627 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
630 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
632 for (i = 0; i < MAXQUOTAS; i++) {
633 dqrele(vp, ip->i_dquot[i]);
634 ip->i_dquot[i] = NODQUOT;
639 if (getinoquota(ip) == 0) {
641 dqrele(vp, ip->i_dquot[USRQUOTA]);
642 ip->i_dquot[USRQUOTA] = NODQUOT;
645 dqrele(vp, ip->i_dquot[GRPQUOTA]);
646 ip->i_dquot[GRPQUOTA] = NODQUOT;
648 (void) chkdq(ip, change, cred, FORCE|CHOWN);
649 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
650 (void) getinoquota(ip);
655 panic("ufs_chown: lost quota");
657 ip->i_flag |= IN_CHANGE;
658 if (cred->cr_uid != 0 && (ouid != uid || ogid != gid))
659 ip->i_mode &= ~(ISUID | ISGID);
666 * NB Currently unsupported.
668 * ufs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
669 * struct thread *a_td)
674 ufs_mmap(struct vop_mmap_args *ap)
680 * ufs_remove(struct vnode *a_dvp, struct vnode *a_vp,
681 * struct componentname *a_cnp)
685 ufs_remove(struct vop_remove_args *ap)
688 struct vnode *vp = ap->a_vp;
689 struct vnode *dvp = ap->a_dvp;
693 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
694 (VTOI(dvp)->i_flags & APPEND)) {
698 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
699 VN_KNOTE(vp, NOTE_DELETE);
700 VN_KNOTE(dvp, NOTE_WRITE);
708 * ufs_link(struct vnode *a_tdvp, struct vnode *a_vp,
709 * struct componentname *a_cnp)
713 ufs_link(struct vop_link_args *ap)
715 struct vnode *vp = ap->a_vp;
716 struct vnode *tdvp = ap->a_tdvp;
717 struct componentname *cnp = ap->a_cnp;
718 struct thread *td = cnp->cn_td;
720 struct direct newdir;
724 if ((cnp->cn_flags & CNP_HASBUF) == 0)
725 panic("ufs_link: no name");
727 if (tdvp->v_mount != vp->v_mount) {
731 if (tdvp != vp && (error = vn_lock(vp, NULL, LK_EXCLUSIVE, td))) {
735 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
739 if (ip->i_flags & (IMMUTABLE | APPEND)) {
745 ip->i_flag |= IN_CHANGE;
746 if (DOINGSOFTDEP(vp))
747 softdep_change_linkcnt(ip);
748 error = UFS_UPDATE(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
750 ufs_makedirentry(ip, cnp, &newdir);
751 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
757 ip->i_flag |= IN_CHANGE;
758 if (DOINGSOFTDEP(vp))
759 softdep_change_linkcnt(ip);
763 VOP_UNLOCK(vp, NULL, 0, td);
765 VN_KNOTE(vp, NOTE_LINK);
766 VN_KNOTE(tdvp, NOTE_WRITE);
771 * whiteout vnode call
773 * ufs_whiteout(struct vnode *a_dvp, struct componentname *a_cnp, int a_flags)
777 ufs_whiteout(struct vop_whiteout_args *ap)
779 struct vnode *dvp = ap->a_dvp;
780 struct componentname *cnp = ap->a_cnp;
781 struct direct newdir;
784 switch (ap->a_flags) {
786 /* 4.4 format directories support whiteout operations */
787 if (dvp->v_mount->mnt_maxsymlinklen > 0)
792 /* create a new directory whiteout */
794 if ((cnp->cn_flags & CNP_SAVENAME) == 0)
795 panic("ufs_whiteout: missing name");
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 if ((tcnp->cn_flags & CNP_HASBUF) == 0 ||
870 (fcnp->cn_flags & CNP_HASBUF) == 0)
871 panic("ufs_rename: no name");
872 #endif /* DIAGNOSTIC */
874 * Check for cross-device rename.
876 if ((fvp->v_mount != tdvp->v_mount) ||
877 (tvp && (fvp->v_mount != tvp->v_mount))) {
891 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
892 (VTOI(tdvp)->i_flags & APPEND))) {
898 * Renaming a file to itself has no effect. The upper layers should
899 * not call us in that case. Temporarily just warn if they do.
902 printf("ufs_rename: fvp == tvp (can't happen)\n");
907 if ((error = vn_lock(fvp, NULL, LK_EXCLUSIVE, td)) != 0)
911 if (ip->i_nlink >= LINK_MAX) {
912 VOP_UNLOCK(fvp, NULL, 0, td);
916 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
917 || (dp->i_flags & APPEND)) {
918 VOP_UNLOCK(fvp, NULL, 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, NULL, 0, td);
933 ip->i_flag |= IN_RENAME;
934 oldparent = dp->i_number;
937 VN_KNOTE(fdvp, NOTE_WRITE); /* XXX right place? */
941 * When the target exists, both the directory
942 * and target vnodes are returned locked.
950 * 1) Bump link count while we're moving stuff
951 * around. If we crash somewhere before
952 * completing our work, the link count
953 * may be wrong, but correctable.
957 ip->i_flag |= IN_CHANGE;
958 if (DOINGSOFTDEP(fvp))
959 softdep_change_linkcnt(ip);
960 if ((error = UFS_UPDATE(fvp, !(DOINGSOFTDEP(fvp) |
961 DOINGASYNC(fvp)))) != 0) {
962 VOP_UNLOCK(fvp, NULL, 0, td);
967 * If ".." must be changed (ie the directory gets a new
968 * parent) then the source directory must not be in the
969 * directory heirarchy above the target, as this would
970 * orphan everything below the source directory. Also
971 * the user must have write permission in the source so
972 * as to be able to change "..". We must repeat the call
973 * to namei, as the parent directory is unlocked by the
974 * call to checkpath().
976 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_td);
977 VOP_UNLOCK(fvp, NULL, 0, td);
978 if (oldparent != dp->i_number)
979 newparent = dp->i_number;
980 if (doingdirectory && newparent) {
981 if (error) /* write access check above */
985 error = ufs_checkpath(ip, dp, tcnp->cn_cred);
988 if ((tcnp->cn_flags & CNP_SAVESTART) == 0)
989 panic("ufs_rename: lost to startdir");
991 error = relookup(tdvp, &tvp, tcnp);
1001 * 2) If target doesn't exist, link the target
1002 * to the source and unlink the source.
1003 * Otherwise, rewrite the target directory
1004 * entry to reference the source inode and
1005 * expunge the original entry's existence.
1008 if (dp->i_dev != ip->i_dev)
1009 panic("ufs_rename: EXDEV");
1011 * Account for ".." in new directory.
1012 * When source and destination have the same
1013 * parent we don't fool with the link count.
1015 if (doingdirectory && newparent) {
1016 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1022 dp->i_flag |= IN_CHANGE;
1023 if (DOINGSOFTDEP(tdvp))
1024 softdep_change_linkcnt(dp);
1025 error = UFS_UPDATE(tdvp, !(DOINGSOFTDEP(tdvp) |
1030 ufs_makedirentry(ip, tcnp, &newdir);
1031 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1033 if (doingdirectory && newparent) {
1036 dp->i_flag |= IN_CHANGE;
1037 if (DOINGSOFTDEP(tdvp))
1038 softdep_change_linkcnt(dp);
1039 (void)UFS_UPDATE(tdvp, 1);
1043 VN_KNOTE(tdvp, NOTE_WRITE);
1046 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1047 panic("ufs_rename: EXDEV");
1049 * Short circuit rename(foo, foo).
1051 if (xp->i_number == ip->i_number)
1052 panic("ufs_rename: same file");
1054 * If the parent directory is "sticky", then the user must
1055 * own the parent directory, or the destination of the rename,
1056 * otherwise the destination may not be changed (except by
1057 * root). This implements append-only directories.
1059 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
1060 tcnp->cn_cred->cr_uid != dp->i_uid &&
1061 xp->i_uid != tcnp->cn_cred->cr_uid) {
1066 * Target must be empty if a directory and have no links
1067 * to it. Also, ensure source and target are compatible
1068 * (both directories, or both not directories).
1070 if ((xp->i_mode&IFMT) == IFDIR) {
1071 if ((xp->i_effnlink > 2) ||
1072 !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1076 if (!doingdirectory) {
1081 } else if (doingdirectory) {
1086 * note: inode passed to ufs_dirrewrite() is 0 for a
1087 * non-directory file rename, 1 for a directory rename
1088 * in the same directory, and > 1 for an inode representing
1089 * the new directory.
1091 error = ufs_dirrewrite(dp, xp, ip->i_number,
1093 (doingdirectory && newparent) ?
1094 newparent : (ino_t)doingdirectory);
1097 if (doingdirectory) {
1100 if (DOINGSOFTDEP(tdvp))
1101 softdep_change_linkcnt(dp);
1104 if (DOINGSOFTDEP(tvp))
1105 softdep_change_linkcnt(xp);
1107 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1109 * Truncate inode. The only stuff left in the directory
1110 * is "." and "..". The "." reference is inconsequential
1111 * since we are quashing it. We have removed the "."
1112 * reference and the reference in the parent directory,
1113 * but there may be other hard links. The soft
1114 * dependency code will arrange to do these operations
1115 * after the parent directory entry has been deleted on
1116 * disk, so when running with that code we avoid doing
1121 dp->i_flag |= IN_CHANGE;
1124 xp->i_flag |= IN_CHANGE;
1125 ioflag = DOINGASYNC(tvp) ? 0 : IO_SYNC;
1126 if ((error = UFS_TRUNCATE(tvp, (off_t)0, ioflag,
1127 tcnp->cn_cred, tcnp->cn_td)) != 0)
1130 VN_KNOTE(tdvp, NOTE_WRITE);
1132 VN_KNOTE(tvp, NOTE_DELETE);
1138 * 3) Unlink the source.
1140 fcnp->cn_flags &= ~CNP_MODMASK;
1141 fcnp->cn_flags |= CNP_LOCKPARENT | CNP_LOCKLEAF;
1142 if ((fcnp->cn_flags & CNP_SAVESTART) == 0)
1143 panic("ufs_rename: lost from startdir");
1145 error = relookup(fdvp, &fvp, fcnp);
1153 * From name has disappeared.
1156 panic("ufs_rename: lost dir entry");
1161 * Ensure that the directory entry still exists and has not
1162 * changed while the new name has been entered. If the source is
1163 * a file then the entry may have been unlinked or renamed. In
1164 * either case there is no further work to be done. If the source
1165 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1166 * flag ensures that it cannot be moved by another rename or removed
1171 panic("ufs_rename: lost dir entry");
1174 * If the source is a directory with a
1175 * new parent, the link count of the old
1176 * parent directory must be decremented
1177 * and ".." set to point to the new parent.
1179 if (doingdirectory && newparent) {
1180 xp->i_offset = mastertemplate.dot_reclen;
1181 ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1184 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1185 xp->i_flag &= ~IN_RENAME;
1187 VN_KNOTE(fvp, NOTE_RENAME);
1201 ip->i_flag &= ~IN_RENAME;
1202 if (vn_lock(fvp, NULL, LK_EXCLUSIVE, td) == 0) {
1205 ip->i_flag |= IN_CHANGE;
1206 ip->i_flag &= ~IN_RENAME;
1207 if (DOINGSOFTDEP(fvp))
1208 softdep_change_linkcnt(ip);
1218 * ufs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1219 * struct componentname *a_cnp, struct vattr *a_vap)
1223 ufs_mkdir(struct vop_mkdir_args *ap)
1225 struct vnode *dvp = ap->a_dvp;
1226 struct vattr *vap = ap->a_vap;
1227 struct componentname *cnp = ap->a_cnp;
1228 struct inode *ip, *dp;
1231 struct dirtemplate dirtemplate, *dtp;
1232 struct direct newdir;
1237 if ((cnp->cn_flags & CNP_HASBUF) == 0)
1238 panic("ufs_mkdir: no name");
1241 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1245 dmode = vap->va_mode & 0777;
1248 * Must simulate part of ufs_makeinode here to acquire the inode,
1249 * but not have it entered in the parent directory. The entry is
1250 * made later after writing "." and ".." entries.
1252 error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
1256 ip->i_gid = dp->i_gid;
1260 struct ucred ucred, *ucp;
1264 * If we are hacking owners here, (only do this where told to)
1265 * and we are not giving it TO root, (would subvert quotas)
1266 * then go ahead and give it to the other user.
1267 * The new directory also inherits the SUID bit.
1268 * If user's UID and dir UID are the same,
1269 * 'give it away' so that the SUID is still forced on.
1271 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1272 (dp->i_mode & ISUID) && dp->i_uid) {
1274 ip->i_uid = dp->i_uid;
1276 if (dp->i_uid != cnp->cn_cred->cr_uid) {
1278 * Make sure the correct user gets charged
1280 * Make a dummy credential for the victim.
1281 * XXX This seems to never be accessed out of
1282 * our context so a stack variable is ok.
1285 ucred.cr_uid = ip->i_uid;
1286 ucred.cr_ngroups = 1;
1287 ucred.cr_groups[0] = dp->i_gid;
1292 ip->i_uid = cnp->cn_cred->cr_uid;
1294 if ((error = getinoquota(ip)) ||
1295 (error = chkiq(ip, 1, ucp, 0))) {
1296 UFS_VFREE(tvp, ip->i_number, dmode);
1302 #else /* !SUIDDIR */
1303 ip->i_uid = cnp->cn_cred->cr_uid;
1305 if ((error = getinoquota(ip)) ||
1306 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1307 UFS_VFREE(tvp, ip->i_number, dmode);
1312 #endif /* !SUIDDIR */
1313 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1315 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1318 if (DOINGSOFTDEP(tvp))
1319 softdep_change_linkcnt(ip);
1320 if (cnp->cn_flags & CNP_ISWHITEOUT)
1321 ip->i_flags |= UF_OPAQUE;
1324 * Bump link count in parent directory to reflect work done below.
1325 * Should be done before reference is created so cleanup is
1326 * possible if we crash.
1330 dp->i_flag |= IN_CHANGE;
1331 if (DOINGSOFTDEP(dvp))
1332 softdep_change_linkcnt(dp);
1333 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1338 * Initialize directory with "." and ".." from static template.
1340 if (dvp->v_mount->mnt_maxsymlinklen > 0
1342 dtp = &mastertemplate;
1344 dtp = (struct dirtemplate *)&omastertemplate;
1346 dirtemplate.dot_ino = ip->i_number;
1347 dirtemplate.dotdot_ino = dp->i_number;
1348 if ((error = VOP_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred,
1349 B_CLRBUF, &bp)) != 0)
1351 ip->i_size = DIRBLKSIZ;
1352 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1353 vnode_pager_setsize(tvp, (u_long)ip->i_size);
1354 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1355 if (DOINGSOFTDEP(tvp)) {
1357 * Ensure that the entire newly allocated block is a
1358 * valid directory so that future growth within the
1359 * block does not have to ensure that the block is
1360 * written before the inode.
1363 while (blkoff < bp->b_bcount) {
1365 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1366 blkoff += DIRBLKSIZ;
1369 if ((error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) |
1370 DOINGASYNC(tvp)))) != 0) {
1371 (void)VOP_BWRITE(bp->b_vp, bp);
1375 * Directory set up, now install its entry in the parent directory.
1377 * If we are not doing soft dependencies, then we must write out the
1378 * buffer containing the new directory body before entering the new
1379 * name in the parent. If we are doing soft dependencies, then the
1380 * buffer containing the new directory body will be passed to and
1381 * released in the soft dependency code after the code has attached
1382 * an appropriate ordering dependency to the buffer which ensures that
1383 * the buffer is written before the new name is written in the parent.
1385 if (DOINGASYNC(dvp))
1387 else if (!DOINGSOFTDEP(dvp) && ((error = VOP_BWRITE(bp->b_vp, bp))))
1389 ufs_makedirentry(ip, cnp, &newdir);
1390 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1394 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1399 dp->i_flag |= IN_CHANGE;
1400 if (DOINGSOFTDEP(dvp))
1401 softdep_change_linkcnt(dp);
1403 * No need to do an explicit VOP_TRUNCATE here, vrele will
1404 * do this for us because we set the link count to 0.
1408 ip->i_flag |= IN_CHANGE;
1409 if (DOINGSOFTDEP(tvp))
1410 softdep_change_linkcnt(ip);
1418 * Rmdir system call.
1420 * ufs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1421 * struct componentname *a_cnp)
1425 ufs_rmdir(struct vop_rmdir_args *ap)
1427 struct vnode *vp = ap->a_vp;
1428 struct vnode *dvp = ap->a_dvp;
1429 struct componentname *cnp = ap->a_cnp;
1430 struct inode *ip, *dp;
1437 * Do not remove a directory that is in the process of being renamed.
1438 * Verify the directory is empty (and valid). Rmdir ".." will not be
1439 * valid since ".." will contain a reference to the current directory
1440 * and thus be non-empty. Do not allow the removal of mounted on
1441 * directories (this can happen when an NFS exported filesystem
1442 * tries to remove a locally mounted on directory).
1445 if (ip->i_flag & IN_RENAME) {
1449 if (ip->i_effnlink != 2 ||
1450 !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1454 if ((dp->i_flags & APPEND)
1455 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1459 if (vp->v_mountedhere != 0) {
1464 * Delete reference to directory before purging
1465 * inode. If we crash in between, the directory
1466 * will be reattached to lost+found,
1470 if (DOINGSOFTDEP(vp)) {
1471 softdep_change_linkcnt(dp);
1472 softdep_change_linkcnt(ip);
1474 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1478 if (DOINGSOFTDEP(vp)) {
1479 softdep_change_linkcnt(dp);
1480 softdep_change_linkcnt(ip);
1484 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1487 * Truncate inode. The only stuff left in the directory is "." and
1488 * "..". The "." reference is inconsequential since we are quashing
1489 * it. The soft dependency code will arrange to do these operations
1490 * after the parent directory entry has been deleted on disk, so
1491 * when running with that code we avoid doing them now.
1493 if (!DOINGSOFTDEP(vp)) {
1495 dp->i_flag |= IN_CHANGE;
1497 ip->i_flag |= IN_CHANGE;
1498 ioflag = DOINGASYNC(vp) ? 0 : IO_SYNC;
1499 error = UFS_TRUNCATE(vp, (off_t)0, ioflag, cnp->cn_cred,
1504 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1505 if (ip->i_dirhash != NULL)
1506 ufsdirhash_free(ip);
1509 VN_KNOTE(vp, NOTE_DELETE);
1514 * symlink -- make a symbolic link
1516 * ufs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1517 * struct componentname *a_cnp, struct vattr *a_vap,
1522 ufs_symlink(struct vop_symlink_args *ap)
1524 struct vnode *vp, **vpp = ap->a_vpp;
1528 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1532 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1534 len = strlen(ap->a_target);
1535 if (len < vp->v_mount->mnt_maxsymlinklen) {
1537 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1539 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1541 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1542 UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred,
1550 * Vnode op for reading directories.
1552 * The routine below assumes that the on-disk format of a directory
1553 * is the same as that defined by <sys/dirent.h>. If the on-disk
1554 * format changes, then it will be necessary to do a conversion
1555 * from the on-disk format that read returns to the format defined
1556 * by <sys/dirent.h>.
1558 * ufs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
1559 * int *a_eofflag, int *ncookies, u_long **a_cookies)
1563 ufs_readdir(struct vop_readdir_args *ap)
1565 struct uio *uio = ap->a_uio;
1570 if (ap->a_ncookies != NULL)
1572 * Ensure that the block is aligned. The caller can use
1573 * the cookies to determine where in the block to start.
1575 uio->uio_offset &= ~(DIRBLKSIZ - 1);
1576 off = uio->uio_offset;
1577 count = uio->uio_resid;
1578 /* Make sure we don't return partial entries. */
1579 if (count <= ((uio->uio_offset + count) & (DIRBLKSIZ -1)))
1581 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
1582 lost = uio->uio_resid - count;
1583 uio->uio_resid = count;
1584 uio->uio_iov->iov_len = count;
1585 # if (BYTE_ORDER == LITTLE_ENDIAN)
1586 if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) {
1587 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1589 struct dirent *dp, *edp;
1597 auio.uio_iov = &aiov;
1598 auio.uio_iovcnt = 1;
1599 auio.uio_segflg = UIO_SYSSPACE;
1600 aiov.iov_len = count;
1601 MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
1602 aiov.iov_base = dirbuf;
1603 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
1605 readcnt = count - auio.uio_resid;
1606 edp = (struct dirent *)&dirbuf[readcnt];
1607 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
1609 dp->d_namlen = dp->d_type;
1611 if (dp->d_reclen > 0) {
1612 dp = (struct dirent *)
1613 ((char *)dp + dp->d_reclen);
1620 error = uiomove(dirbuf, readcnt, uio);
1622 FREE(dirbuf, M_TEMP);
1625 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1627 if (!error && ap->a_ncookies != NULL) {
1628 struct dirent* dpStart;
1629 struct dirent* dpEnd;
1635 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1636 panic("ufs_readdir: unexpected uio from NFS server");
1637 dpStart = (struct dirent *)
1638 (uio->uio_iov->iov_base - (uio->uio_offset - off));
1639 dpEnd = (struct dirent *) uio->uio_iov->iov_base;
1640 for (dp = dpStart, ncookies = 0;
1642 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen))
1644 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1646 for (dp = dpStart, cookiep = cookies;
1648 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
1649 off += dp->d_reclen;
1650 *cookiep++ = (u_long) off;
1652 *ap->a_ncookies = ncookies;
1653 *ap->a_cookies = cookies;
1655 uio->uio_resid += lost;
1657 *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
1662 * Return target name of a symbolic link
1664 * ufs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1668 ufs_readlink(struct vop_readlink_args *ap)
1670 struct vnode *vp = ap->a_vp;
1671 struct inode *ip = VTOI(vp);
1675 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1676 (ip->i_din.di_blocks == 0)) { /* XXX - for old fastlink support */
1677 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1680 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1684 * Calculate the logical to physical mapping if not done already,
1685 * then call the device strategy routine.
1687 * In order to be able to swap to a file, the VOP_BMAP operation may not
1688 * deadlock on memory. See ufs_bmap() for details.
1690 * ufs_strategy(struct vnode *a_vp, struct buf *a_bp)
1694 ufs_strategy(struct vop_strategy_args *ap)
1696 struct buf *bp = ap->a_bp;
1697 struct vnode *vp = ap->a_vp;
1702 if (vp->v_type == VBLK || vp->v_type == VCHR)
1703 panic("ufs_strategy: spec");
1704 if (bp->b_blkno == bp->b_lblkno) {
1705 error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1707 bp->b_error = error;
1708 bp->b_flags |= B_ERROR;
1712 if (bp->b_blkno == (daddr_t)-1)
1715 if (bp->b_blkno == (daddr_t)-1) {
1720 bp->b_dev = vp->v_rdev;
1721 VOP_STRATEGY(vp, bp);
1726 * Print out the contents of an inode.
1728 * ufs_print(struct vnode *a_vp)
1732 ufs_print(struct vop_print_args *ap)
1734 struct vnode *vp = ap->a_vp;
1735 struct inode *ip = VTOI(vp);
1737 printf("tag VT_UFS, ino %lu, on dev %s (%d, %d)",
1738 (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev),
1740 if (vp->v_type == VFIFO)
1742 lockmgr_printinfo(&vp->v_lock);
1748 * Read wrapper for special devices.
1750 * ufsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1751 * struct ucred *a_cred)
1755 ufsspec_read(struct vop_read_args *ap)
1762 resid = uio->uio_resid;
1763 error = VOCALL(spec_vnode_vops, &ap->a_head);
1765 * The inode may have been revoked during the call, so it must not
1766 * be accessed blindly here or in the other wrapper functions.
1768 ip = VTOI(ap->a_vp);
1769 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1770 ip->i_flag |= IN_ACCESS;
1775 * Write wrapper for special devices.
1777 * ufsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1778 * struct ucred *a_cred)
1782 ufsspec_write(struct vop_write_args *ap)
1789 resid = uio->uio_resid;
1790 error = VOCALL(spec_vnode_vops, &ap->a_head);
1791 ip = VTOI(ap->a_vp);
1792 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1793 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1798 * Close wrapper for special devices.
1800 * Update the times on the inode then do device close.
1802 * ufsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
1803 * struct thread *a_td)
1807 ufsspec_close(struct vop_close_args *ap)
1809 struct vnode *vp = ap->a_vp;
1812 lwkt_gettoken(&vlock, vp->v_interlock);
1813 if (vp->v_usecount > 1)
1815 lwkt_reltoken(&vlock);
1816 return (VOCALL(spec_vnode_vops, &ap->a_head));
1820 * Read wrapper for fifos.
1822 * ufsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1823 * struct ucred *a_cred)
1827 ufsfifo_read(struct vop_read_args *ap)
1834 resid = uio->uio_resid;
1835 error = VOCALL(fifo_vnode_vops, &ap->a_head);
1836 ip = VTOI(ap->a_vp);
1837 if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL &&
1838 (uio->uio_resid != resid || (error == 0 && resid != 0)))
1839 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1844 * Write wrapper for fifos.
1846 * ufsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1847 * struct ucred *a_cred)
1851 ufsfifo_write(struct vop_write_args *ap)
1858 resid = uio->uio_resid;
1859 error = VOCALL(fifo_vnode_vops, &ap->a_head);
1860 ip = VTOI(ap->a_vp);
1861 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1862 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1867 * Close wrapper for fifos.
1869 * Update the times on the inode then do device close.
1871 * ufsfifo_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
1872 * struct thread *a_td)
1876 ufsfifo_close(struct vop_close_args *ap)
1878 struct vnode *vp = ap->a_vp;
1881 lwkt_gettoken(&vlock, vp->v_interlock);
1882 if (vp->v_usecount > 1)
1884 lwkt_reltoken(&vlock);
1885 return (VOCALL(fifo_vnode_vops, &ap->a_head));
1889 * Kqfilter wrapper for fifos.
1891 * Fall through to ufs kqfilter routines if needed
1895 ufsfifo_kqfilter(struct vop_kqfilter_args *ap)
1899 error = VOCALL(fifo_vnode_vops, &ap->a_head);
1901 error = ufs_kqfilter(ap);
1906 * Return POSIX pathconf information applicable to ufs filesystems.
1908 * ufs_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
1912 ufs_pathconf(struct vop_pathconf_args *ap)
1914 switch (ap->a_name) {
1916 *ap->a_retval = LINK_MAX;
1919 *ap->a_retval = NAME_MAX;
1922 *ap->a_retval = PATH_MAX;
1925 *ap->a_retval = PIPE_BUF;
1927 case _PC_CHOWN_RESTRICTED:
1940 * Advisory record locking support
1942 * ufs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
1947 ufs_advlock(struct vop_advlock_args *ap)
1949 struct inode *ip = VTOI(ap->a_vp);
1951 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
1955 * Initialize the vnode associated with a new inode, handle aliased
1959 ufs_vinit(struct mount *mntp, struct vnode **vpp)
1967 switch(vp->v_type = IFTOVT(ip->i_mode)) {
1970 vp->v_ops = mntp->mnt_vn_spec_ops;
1971 addaliasu(vp, ip->i_rdev);
1974 vp->v_ops = mntp->mnt_vn_fifo_ops;
1980 if (ip->i_number == ROOTINO)
1981 vp->v_flag |= VROOT;
1983 * Initialize modrev times
1985 getmicrouptime(&tv);
1986 SETHIGH(ip->i_modrev, tv.tv_sec);
1987 SETLOW(ip->i_modrev, tv.tv_usec * 4294);
1993 * Allocate a new inode.
1997 ufs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
1998 struct componentname *cnp)
2000 struct inode *ip, *pdir;
2001 struct direct newdir;
2007 if ((cnp->cn_flags & CNP_HASBUF) == 0)
2008 panic("ufs_makeinode: no name");
2011 if ((mode & IFMT) == 0)
2014 error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2018 ip->i_gid = pdir->i_gid;
2022 struct ucred ucred, *ucp;
2026 * If we are not the owner of the directory,
2027 * and we are hacking owners here, (only do this where told to)
2028 * and we are not giving it TO root, (would subvert quotas)
2029 * then go ahead and give it to the other user.
2030 * Note that this drops off the execute bits for security.
2032 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2033 (pdir->i_mode & ISUID) &&
2034 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2035 ip->i_uid = pdir->i_uid;
2039 * Make sure the correct user gets charged
2041 * Quickly knock up a dummy credential for the victim.
2042 * XXX This seems to never be accessed out of our
2043 * context so a stack variable is ok.
2046 ucred.cr_uid = ip->i_uid;
2047 ucred.cr_ngroups = 1;
2048 ucred.cr_groups[0] = pdir->i_gid;
2052 ip->i_uid = cnp->cn_cred->cr_uid;
2055 if ((error = getinoquota(ip)) ||
2056 (error = chkiq(ip, 1, ucp, 0))) {
2057 UFS_VFREE(tvp, ip->i_number, mode);
2063 #else /* !SUIDDIR */
2064 ip->i_uid = cnp->cn_cred->cr_uid;
2066 if ((error = getinoquota(ip)) ||
2067 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2068 UFS_VFREE(tvp, ip->i_number, mode);
2073 #endif /* !SUIDDIR */
2074 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2076 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2079 if (DOINGSOFTDEP(tvp))
2080 softdep_change_linkcnt(ip);
2081 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2082 suser_cred(cnp->cn_cred, 0)) {
2083 ip->i_mode &= ~ISGID;
2086 if (cnp->cn_flags & CNP_ISWHITEOUT)
2087 ip->i_flags |= UF_OPAQUE;
2090 * Make sure inode goes to disk before directory entry.
2092 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2095 ufs_makedirentry(ip, cnp, &newdir);
2096 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2104 * Write error occurred trying to update the inode
2105 * or the directory so must deallocate the inode.
2109 ip->i_flag |= IN_CHANGE;
2110 if (DOINGSOFTDEP(tvp))
2111 softdep_change_linkcnt(ip);
2117 ufs_missingop(struct vop_generic_args *ap)
2119 panic("no vop function for %s in ufs child", ap->a_desc->vdesc_name);
2120 return (EOPNOTSUPP);
2123 static struct filterops ufsread_filtops =
2124 { 1, NULL, filt_ufsdetach, filt_ufsread };
2125 static struct filterops ufswrite_filtops =
2126 { 1, NULL, filt_ufsdetach, filt_ufswrite };
2127 static struct filterops ufsvnode_filtops =
2128 { 1, NULL, filt_ufsdetach, filt_ufsvnode };
2131 * ufs_kqfilter(struct vnode *a_vp, struct knote *a_kn)
2134 ufs_kqfilter(struct vop_kqfilter_args *ap)
2136 struct vnode *vp = ap->a_vp;
2137 struct knote *kn = ap->a_kn;
2140 switch (kn->kn_filter) {
2142 kn->kn_fop = &ufsread_filtops;
2145 kn->kn_fop = &ufswrite_filtops;
2148 kn->kn_fop = &ufsvnode_filtops;
2154 kn->kn_hook = (caddr_t)vp;
2156 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2157 SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
2158 lwkt_reltoken(&ilock);
2164 filt_ufsdetach(struct knote *kn)
2166 struct vnode *vp = (struct vnode *)kn->kn_hook;
2169 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2170 SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
2171 kn, knote, kn_selnext);
2172 lwkt_reltoken(&ilock);
2177 filt_ufsread(struct knote *kn, long hint)
2179 struct vnode *vp = (struct vnode *)kn->kn_hook;
2180 struct inode *ip = VTOI(vp);
2183 * filesystem is gone, so set the EOF flag and schedule
2184 * the knote for deletion.
2186 if (hint == NOTE_REVOKE) {
2187 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2191 kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
2192 return (kn->kn_data != 0);
2197 filt_ufswrite(struct knote *kn, long hint)
2200 * filesystem is gone, so set the EOF flag and schedule
2201 * the knote for deletion.
2203 if (hint == NOTE_REVOKE)
2204 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2211 filt_ufsvnode(struct knote *kn, long hint)
2213 if (kn->kn_sfflags & hint)
2214 kn->kn_fflags |= hint;
2215 if (hint == NOTE_REVOKE) {
2216 kn->kn_flags |= EV_EOF;
2219 return (kn->kn_fflags != 0);
2222 /* Global vfs data structures for ufs. */
2223 static struct vop_ops *ufs_vnode_vops;
2224 static struct vnodeopv_entry_desc ufs_vnodeop_entries[] = {
2225 { &vop_default_desc, vop_defaultop },
2226 { &vop_fsync_desc, (void *) ufs_missingop },
2227 { &vop_read_desc, (void *) ufs_missingop },
2228 { &vop_reallocblks_desc, (void *) ufs_missingop },
2229 { &vop_write_desc, (void *) ufs_missingop },
2230 { &vop_access_desc, (void *) ufs_access },
2231 { &vop_advlock_desc, (void *) ufs_advlock },
2232 { &vop_bmap_desc, (void *) ufs_bmap },
2233 { &vop_cachedlookup_desc, (void *) ufs_lookup },
2234 { &vop_close_desc, (void *) ufs_close },
2235 { &vop_create_desc, (void *) ufs_create },
2236 { &vop_getattr_desc, (void *) ufs_getattr },
2237 { &vop_inactive_desc, (void *) ufs_inactive },
2238 { &vop_islocked_desc, (void *) vop_stdislocked },
2239 { &vop_link_desc, (void *) ufs_link },
2240 { &vop_lock_desc, (void *) vop_stdlock },
2241 { &vop_lookup_desc, (void *) vfs_cache_lookup },
2242 { &vop_mkdir_desc, (void *) ufs_mkdir },
2243 { &vop_mknod_desc, (void *) ufs_mknod },
2244 { &vop_mmap_desc, (void *) ufs_mmap },
2245 { &vop_open_desc, (void *) ufs_open },
2246 { &vop_pathconf_desc, (void *) ufs_pathconf },
2247 { &vop_poll_desc, (void *) vop_stdpoll },
2248 { &vop_kqfilter_desc, (void *) ufs_kqfilter },
2249 { &vop_print_desc, (void *) ufs_print },
2250 { &vop_readdir_desc, (void *) ufs_readdir },
2251 { &vop_readlink_desc, (void *) ufs_readlink },
2252 { &vop_reclaim_desc, (void *) ufs_reclaim },
2253 { &vop_remove_desc, (void *) ufs_remove },
2254 { &vop_rename_desc, (void *) ufs_rename },
2255 { &vop_rmdir_desc, (void *) ufs_rmdir },
2256 { &vop_setattr_desc, (void *) ufs_setattr },
2257 { &vop_strategy_desc, (void *) ufs_strategy },
2258 { &vop_symlink_desc, (void *) ufs_symlink },
2259 { &vop_unlock_desc, (void *) vop_stdunlock },
2260 { &vop_whiteout_desc, (void *) ufs_whiteout },
2263 static struct vnodeopv_desc ufs_vnodeop_opv_desc =
2264 { &ufs_vnode_vops, ufs_vnodeop_entries };
2266 static struct vop_ops *ufs_spec_vops;
2267 static struct vnodeopv_entry_desc ufs_specop_entries[] = {
2268 { &vop_default_desc, (void *) spec_vnoperate },
2269 { &vop_fsync_desc, (void *) ufs_missingop },
2270 { &vop_access_desc, (void *) ufs_access },
2271 { &vop_close_desc, (void *) ufsspec_close },
2272 { &vop_getattr_desc, (void *) ufs_getattr },
2273 { &vop_inactive_desc, (void *) ufs_inactive },
2274 { &vop_islocked_desc, (void *) vop_stdislocked },
2275 { &vop_lock_desc, (void *) vop_stdlock },
2276 { &vop_print_desc, (void *) ufs_print },
2277 { &vop_read_desc, (void *) ufsspec_read },
2278 { &vop_reclaim_desc, (void *) ufs_reclaim },
2279 { &vop_setattr_desc, (void *) ufs_setattr },
2280 { &vop_unlock_desc, (void *) vop_stdunlock },
2281 { &vop_write_desc, (void *) ufsspec_write },
2284 static struct vnodeopv_desc ufs_specop_opv_desc =
2285 { &ufs_spec_vops, ufs_specop_entries };
2287 static struct vop_ops *ufs_fifo_vops;
2288 static struct vnodeopv_entry_desc ufs_fifoop_entries[] = {
2289 { &vop_default_desc, (void *) fifo_vnoperate },
2290 { &vop_fsync_desc, (void *) ufs_missingop },
2291 { &vop_access_desc, (void *) ufs_access },
2292 { &vop_close_desc, (void *) ufsfifo_close },
2293 { &vop_getattr_desc, (void *) ufs_getattr },
2294 { &vop_inactive_desc, (void *) ufs_inactive },
2295 { &vop_islocked_desc, (void *) vop_stdislocked },
2296 { &vop_kqfilter_desc, (void *) ufsfifo_kqfilter },
2297 { &vop_lock_desc, (void *) vop_stdlock },
2298 { &vop_print_desc, (void *) ufs_print },
2299 { &vop_read_desc, (void *) ufsfifo_read },
2300 { &vop_reclaim_desc, (void *) ufs_reclaim },
2301 { &vop_setattr_desc, (void *) ufs_setattr },
2302 { &vop_unlock_desc, (void *) vop_stdunlock },
2303 { &vop_write_desc, (void *) ufsfifo_write },
2306 static struct vnodeopv_desc ufs_fifoop_opv_desc =
2307 { &ufs_fifo_vops, ufs_fifoop_entries };
2309 VNODEOP_SET(ufs_vnodeop_opv_desc);
2310 VNODEOP_SET(ufs_specop_opv_desc);
2311 VNODEOP_SET(ufs_fifoop_opv_desc);
2314 * ufs_vnoperate(struct vnodeop_desc *a_desc)
2317 ufs_vnoperate(struct vop_generic_args *ap)
2319 return (VOCALL(ufs_vnode_vops, ap));
2323 * ufs_vnoperatefifo(struct vnodeop_desc *a_desc)
2326 ufs_vnoperatefifo(struct vop_generic_args *ap)
2328 return (VOCALL(ufs_fifo_vops, ap));
2332 * ufs_vnoperatespec(struct vnodeop_desc *a_desc)
2335 ufs_vnoperatespec(struct vop_generic_args *ap)
2337 return (VOCALL(ufs_spec_vops, ap));