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.67 2008/09/28 05:04:22 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>
55 #include <sys/namei.h>
56 #include <sys/mount.h>
57 #include <sys/unistd.h>
58 #include <sys/vnode.h>
59 #include <sys/malloc.h>
60 #include <sys/dirent.h>
61 #include <sys/lockf.h>
62 #include <sys/event.h>
65 #include <sys/file.h> /* XXX */
69 #include <vm/vm_extern.h>
71 #include <vfs/fifofs/fifo.h>
77 #include "ufs_extern.h"
78 #include "ffs_extern.h"
83 static int ufs_access (struct vop_access_args *);
84 static int ufs_advlock (struct vop_advlock_args *);
85 static int ufs_chmod (struct vnode *, int, struct ucred *);
86 static int ufs_chown (struct vnode *, uid_t, gid_t, struct ucred *);
87 static int ufs_close (struct vop_close_args *);
88 static int ufs_create (struct vop_old_create_args *);
89 static int ufs_getattr (struct vop_getattr_args *);
90 static int ufs_link (struct vop_old_link_args *);
91 static int ufs_makeinode (int mode, struct vnode *, struct vnode **, struct componentname *);
92 static int ufs_markatime (struct vop_markatime_args *);
93 static int ufs_missingop (struct vop_generic_args *ap);
94 static int ufs_mkdir (struct vop_old_mkdir_args *);
95 static int ufs_mknod (struct vop_old_mknod_args *);
96 static int ufs_mmap (struct vop_mmap_args *);
97 static int ufs_open (struct vop_open_args *);
98 static int ufs_print (struct vop_print_args *);
99 static int ufs_readdir (struct vop_readdir_args *);
100 static int ufs_readlink (struct vop_readlink_args *);
101 static int ufs_remove (struct vop_old_remove_args *);
102 static int ufs_rename (struct vop_old_rename_args *);
103 static int ufs_rmdir (struct vop_old_rmdir_args *);
104 static int ufs_setattr (struct vop_setattr_args *);
105 static int ufs_strategy (struct vop_strategy_args *);
106 static int ufs_symlink (struct vop_old_symlink_args *);
107 static int ufs_whiteout (struct vop_old_whiteout_args *);
108 static int ufsfifo_close (struct vop_close_args *);
109 static int ufsfifo_kqfilter (struct vop_kqfilter_args *);
110 static int ufsfifo_read (struct vop_read_args *);
111 static int ufsfifo_write (struct vop_write_args *);
112 static int ufsspec_close (struct vop_close_args *);
113 static int ufsspec_read (struct vop_read_args *);
114 static int ufsspec_write (struct vop_write_args *);
115 static int filt_ufsread (struct knote *kn, long hint);
116 static int filt_ufswrite (struct knote *kn, long hint);
117 static int filt_ufsvnode (struct knote *kn, long hint);
118 static void filt_ufsdetach (struct knote *kn);
119 static int ufs_kqfilter (struct vop_kqfilter_args *ap);
125 #define SETHIGH(q, h) { \
128 tmp.val[_QUAD_HIGHWORD] = (h); \
131 #define SETLOW(q, l) { \
134 tmp.val[_QUAD_LOWWORD] = (l); \
137 #define VN_KNOTE(vp, b) \
138 KNOTE(&vp->v_pollinfo.vpi_selinfo.si_note, (b))
140 #define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0)
143 * A virgin directory (no blushing please).
145 static struct dirtemplate mastertemplate = {
146 0, 12, DT_DIR, 1, ".",
147 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
149 static struct odirtemplate omastertemplate = {
151 0, DIRBLKSIZ - 12, 2, ".."
155 ufs_itimes(struct vnode *vp)
161 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
163 if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
164 ip->i_flag |= IN_LAZYMOD;
166 ip->i_flag |= IN_MODIFIED;
167 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
169 if (ip->i_flag & IN_ACCESS) {
170 ip->i_atime = ts.tv_sec;
171 ip->i_atimensec = ts.tv_nsec;
173 if (ip->i_flag & IN_UPDATE) {
174 ip->i_mtime = ts.tv_sec;
175 ip->i_mtimensec = ts.tv_nsec;
178 if (ip->i_flag & IN_CHANGE) {
179 ip->i_ctime = ts.tv_sec;
180 ip->i_ctimensec = ts.tv_nsec;
183 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
187 * Create a regular file
189 * ufs_create(struct vnode *a_dvp, struct vnode **a_vpp,
190 * struct componentname *a_cnp, struct vattr *a_vap)
194 ufs_create(struct vop_old_create_args *ap)
199 ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
200 ap->a_dvp, ap->a_vpp, ap->a_cnp);
203 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
210 * ufs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
211 * struct componentname *a_cnp, struct vattr *a_vap)
216 ufs_mknod(struct vop_old_mknod_args *ap)
218 struct vattr *vap = ap->a_vap;
219 struct vnode **vpp = ap->a_vpp;
225 * UFS cannot represent the entire major/minor range supported by
228 if (vap->va_rmajor != VNOVAL &&
229 makeudev(vap->va_rmajor, vap->va_rminor) == NOUDEV) {
233 /* no special directory support */
234 if (vap->va_type == VDIR)
237 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
238 ap->a_dvp, vpp, ap->a_cnp);
241 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
243 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
244 if (vap->va_rmajor != VNOVAL) {
246 * Want to be able to use this to make badblock
247 * inodes, so don't truncate the dev number.
249 ip->i_rdev = makeudev(vap->va_rmajor, vap->va_rminor);
252 * Remove inode, then reload it through VFS_VGET so it is
253 * checked to see if it is an alias of an existing entry in
256 (*vpp)->v_type = VNON;
257 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
258 vgone_vxlocked(*vpp);
260 error = VFS_VGET(ap->a_dvp->v_mount, ino, vpp);
273 * ufs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
279 ufs_open(struct vop_open_args *ap)
281 struct vnode *vp = ap->a_vp;
284 * Files marked append-only must be opened for appending.
286 if ((VTOI(vp)->i_flags & APPEND) &&
287 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE) {
291 return (vop_stdopen(ap));
297 * Update the times on the inode.
299 * ufs_close(struct vnode *a_vp, int a_fflag)
304 ufs_close(struct vop_close_args *ap)
306 struct vnode *vp = ap->a_vp;
308 if (vp->v_sysref.refcnt > 1)
310 return (vop_stdclose(ap));
314 * ufs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
318 ufs_access(struct vop_access_args *ap)
320 struct vnode *vp = ap->a_vp;
321 struct inode *ip = VTOI(vp);
322 struct ucred *cred = ap->a_cred;
323 mode_t mask, mode = ap->a_mode;
331 * Disallow write attempts on read-only filesystems;
332 * unless the file is a socket, fifo, or a block or
333 * character device resident on the filesystem.
336 switch (vp->v_type) {
340 if (vp->v_mount->mnt_flag & MNT_RDONLY)
343 if ((error = ufs_getinoquota(ip)) != 0)
352 /* If immutable bit set, nobody gets to write it. */
353 if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE))
356 /* Otherwise, user id 0 always gets access. */
357 if (cred->cr_uid == 0)
362 /* Otherwise, check the owner. */
363 if (cred->cr_uid == ip->i_uid) {
372 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
375 /* Otherwise, check the groups. */
376 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
377 if (ip->i_gid == *gp) {
384 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
387 /* Otherwise, check everyone else. */
394 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
398 * ufs_getattr(struct vnode *a_vp, struct vattr *a_vap)
403 ufs_getattr(struct vop_getattr_args *ap)
405 struct vnode *vp = ap->a_vp;
406 struct inode *ip = VTOI(vp);
407 struct vattr *vap = ap->a_vap;
410 * This may cause i_fsmid to be updated even if no change (0)
411 * is returned, but we should only write out the inode if non-zero
412 * is returned and if the mount is read-write.
414 if (cache_check_fsmid_vp(vp, &ip->i_fsmid) &&
415 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0
417 ip->i_flag |= IN_LAZYMOD;
422 * Copy from inode table
424 vap->va_fsid = dev2udev(ip->i_dev);
425 vap->va_fileid = ip->i_number;
426 vap->va_mode = ip->i_mode & ~IFMT;
427 vap->va_nlink = VFSTOUFS(vp->v_mount)->um_i_effnlink_valid ?
428 ip->i_effnlink : ip->i_nlink;
429 vap->va_uid = ip->i_uid;
430 vap->va_gid = ip->i_gid;
431 vap->va_rmajor = umajor(ip->i_rdev);
432 vap->va_rminor = uminor(ip->i_rdev);
433 vap->va_size = ip->i_din.di_size;
434 vap->va_atime.tv_sec = ip->i_atime;
435 vap->va_atime.tv_nsec = ip->i_atimensec;
436 vap->va_mtime.tv_sec = ip->i_mtime;
437 vap->va_mtime.tv_nsec = ip->i_mtimensec;
438 vap->va_ctime.tv_sec = ip->i_ctime;
439 vap->va_ctime.tv_nsec = ip->i_ctimensec;
440 vap->va_flags = ip->i_flags;
441 vap->va_gen = ip->i_gen;
442 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
443 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
444 vap->va_type = IFTOVT(ip->i_mode);
445 vap->va_filerev = ip->i_modrev;
446 vap->va_fsmid = ip->i_fsmid;
452 ufs_markatime(struct vop_markatime_args *ap)
454 struct vnode *vp = ap->a_vp;
455 struct inode *ip = VTOI(vp);
457 if (vp->v_mount->mnt_flag & MNT_RDONLY)
459 if (vp->v_mount->mnt_flag & MNT_NOATIME)
461 ip->i_flag |= IN_ACCESS;
462 VN_KNOTE(vp, NOTE_ATTRIB);
467 * Set attribute vnode op. called from several syscalls
469 * ufs_setattr(struct vnode *a_vp, struct vattr *a_vap,
470 * struct ucred *a_cred)
474 ufs_setattr(struct vop_setattr_args *ap)
476 struct vattr *vap = ap->a_vap;
477 struct vnode *vp = ap->a_vp;
478 struct inode *ip = VTOI(vp);
479 struct ucred *cred = ap->a_cred;
483 * Check for unsettable attributes.
485 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
486 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
487 (vap->va_blocksize != VNOVAL) || (vap->va_rmajor != VNOVAL) ||
488 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
491 if (vap->va_flags != VNOVAL) {
492 if (vp->v_mount->mnt_flag & MNT_RDONLY)
494 if (cred->cr_uid != ip->i_uid &&
495 (error = priv_check_cred(cred, PRIV_ROOT, PRISON_ROOT)))
498 * Note that a root chflags becomes a user chflags when
499 * we are jailed, unless the jail.chflags_allowed sysctl
502 if (cred->cr_uid == 0 &&
503 (!jailed(cred) || jail_chflags_allowed)) {
505 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) &&
508 ip->i_flags = vap->va_flags;
511 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
512 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
514 ip->i_flags &= SF_SETTABLE;
515 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
517 ip->i_flag |= IN_CHANGE;
518 if (vap->va_flags & (IMMUTABLE | APPEND))
521 if (ip->i_flags & (IMMUTABLE | APPEND))
524 * Go through the fields and update iff not VNOVAL.
526 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
527 if (vp->v_mount->mnt_flag & MNT_RDONLY)
529 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred)) != 0)
532 if (vap->va_size != VNOVAL) {
534 * Disallow write attempts on read-only filesystems;
535 * unless the file is a socket, fifo, or a block or
536 * character device resident on the filesystem.
538 switch (vp->v_type) {
543 if (vp->v_mount->mnt_flag & MNT_RDONLY)
549 if ((error = ffs_truncate(vp, vap->va_size, 0, cred)) != 0)
553 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
554 if (vp->v_mount->mnt_flag & MNT_RDONLY)
556 if (cred->cr_uid != ip->i_uid &&
557 (error = priv_check_cred(cred, PRIV_ROOT, PRISON_ROOT)) &&
558 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
559 (error = VOP_ACCESS(vp, VWRITE, cred))))
561 if (vap->va_atime.tv_sec != VNOVAL)
562 ip->i_flag |= IN_ACCESS;
563 if (vap->va_mtime.tv_sec != VNOVAL)
564 ip->i_flag |= IN_CHANGE | IN_UPDATE;
566 if (vap->va_atime.tv_sec != VNOVAL) {
567 ip->i_atime = vap->va_atime.tv_sec;
568 ip->i_atimensec = vap->va_atime.tv_nsec;
570 if (vap->va_mtime.tv_sec != VNOVAL) {
571 ip->i_mtime = vap->va_mtime.tv_sec;
572 ip->i_mtimensec = vap->va_mtime.tv_nsec;
574 error = ffs_update(vp, 0);
579 if (vap->va_mode != (mode_t)VNOVAL) {
580 if (vp->v_mount->mnt_flag & MNT_RDONLY)
582 error = ufs_chmod(vp, (int)vap->va_mode, cred);
584 VN_KNOTE(vp, NOTE_ATTRIB);
589 * Change the mode on a file.
590 * Inode must be locked before calling.
593 ufs_chmod(struct vnode *vp, int mode, struct ucred *cred)
595 struct inode *ip = VTOI(vp);
598 if (cred->cr_uid != ip->i_uid) {
599 error = priv_check_cred(cred, PRIV_ROOT, PRISON_ROOT);
604 if (vp->v_type != VDIR && (mode & S_ISTXT))
606 if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
609 ip->i_mode &= ~ALLPERMS;
610 ip->i_mode |= (mode & ALLPERMS);
611 ip->i_flag |= IN_CHANGE;
616 * Perform chown operation on inode ip;
617 * inode must be locked prior to call.
620 ufs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred)
622 struct inode *ip = VTOI(vp);
631 if (uid == (uid_t)VNOVAL)
633 if (gid == (gid_t)VNOVAL)
636 * If we don't own the file, are trying to change the owner
637 * of the file, or are not a member of the target group,
638 * the caller must be superuser or the call fails.
640 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
641 (gid != ip->i_gid && !(cred->cr_gid == gid ||
642 groupmember((gid_t)gid, cred)))) &&
643 (error = priv_check_cred(cred, PRIV_ROOT, PRISON_ROOT)))
648 if ((error = ufs_getinoquota(ip)) != 0)
651 ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
652 ip->i_dquot[USRQUOTA] = NODQUOT;
655 ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
656 ip->i_dquot[GRPQUOTA] = NODQUOT;
658 change = ip->i_blocks;
659 (void) ufs_chkdq(ip, -change, cred, CHOWN);
660 (void) ufs_chkiq(ip, -1, cred, CHOWN);
661 for (i = 0; i < MAXQUOTAS; i++) {
662 ufs_dqrele(vp, ip->i_dquot[i]);
663 ip->i_dquot[i] = NODQUOT;
669 if ((error = ufs_getinoquota(ip)) == 0) {
671 ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
672 ip->i_dquot[USRQUOTA] = NODQUOT;
675 ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
676 ip->i_dquot[GRPQUOTA] = NODQUOT;
678 if ((error = ufs_chkdq(ip, change, cred, CHOWN)) == 0) {
679 if ((error = ufs_chkiq(ip, 1, cred, CHOWN)) == 0)
682 (void)ufs_chkdq(ip, -change, cred, CHOWN|FORCE);
684 for (i = 0; i < MAXQUOTAS; i++) {
685 ufs_dqrele(vp, ip->i_dquot[i]);
686 ip->i_dquot[i] = NODQUOT;
691 if (ufs_getinoquota(ip) == 0) {
693 ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
694 ip->i_dquot[USRQUOTA] = NODQUOT;
697 ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
698 ip->i_dquot[GRPQUOTA] = NODQUOT;
700 (void) ufs_chkdq(ip, change, cred, FORCE|CHOWN);
701 (void) ufs_chkiq(ip, 1, cred, FORCE|CHOWN);
702 (void) ufs_getinoquota(ip);
706 if (ufs_getinoquota(ip))
707 panic("ufs_chown: lost quota");
709 ip->i_flag |= IN_CHANGE;
710 if (cred->cr_uid != 0 && (ouid != uid || ogid != gid))
711 ip->i_mode &= ~(ISUID | ISGID);
718 * NB Currently unsupported.
720 * ufs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
725 ufs_mmap(struct vop_mmap_args *ap)
731 * ufs_remove(struct vnode *a_dvp, struct vnode *a_vp,
732 * struct componentname *a_cnp)
736 ufs_remove(struct vop_old_remove_args *ap)
739 struct vnode *vp = ap->a_vp;
740 struct vnode *dvp = ap->a_dvp;
744 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
745 (VTOI(dvp)->i_flags & APPEND)) {
749 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
750 VN_KNOTE(vp, NOTE_DELETE);
751 VN_KNOTE(dvp, NOTE_WRITE);
759 * ufs_link(struct vnode *a_tdvp, struct vnode *a_vp,
760 * struct componentname *a_cnp)
764 ufs_link(struct vop_old_link_args *ap)
766 struct vnode *vp = ap->a_vp;
767 struct vnode *tdvp = ap->a_tdvp;
768 struct componentname *cnp = ap->a_cnp;
770 struct direct newdir;
773 if (tdvp->v_mount != vp->v_mount) {
777 if (tdvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE))) {
781 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
785 if (ip->i_flags & (IMMUTABLE | APPEND)) {
791 ip->i_flag |= IN_CHANGE;
792 if (DOINGSOFTDEP(vp))
793 softdep_change_linkcnt(ip);
794 error = ffs_update(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
796 ufs_makedirentry(ip, cnp, &newdir);
797 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
803 ip->i_flag |= IN_CHANGE;
804 if (DOINGSOFTDEP(vp))
805 softdep_change_linkcnt(ip);
811 VN_KNOTE(vp, NOTE_LINK);
812 VN_KNOTE(tdvp, NOTE_WRITE);
817 * whiteout vnode call
819 * ufs_whiteout(struct vnode *a_dvp, struct componentname *a_cnp, int a_flags)
823 ufs_whiteout(struct vop_old_whiteout_args *ap)
825 struct vnode *dvp = ap->a_dvp;
826 struct componentname *cnp = ap->a_cnp;
827 struct direct newdir;
830 switch (ap->a_flags) {
832 /* 4.4 format directories support whiteout operations */
833 if (dvp->v_mount->mnt_maxsymlinklen > 0)
838 /* create a new directory whiteout */
840 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
841 panic("ufs_whiteout: old format filesystem");
845 newdir.d_namlen = cnp->cn_namelen;
846 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
847 newdir.d_type = DT_WHT;
848 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
852 /* remove an existing directory whiteout */
854 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
855 panic("ufs_whiteout: old format filesystem");
858 cnp->cn_flags &= ~CNP_DOWHITEOUT;
859 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
862 panic("ufs_whiteout: unknown op");
868 * Rename system call.
869 * rename("foo", "bar");
872 * link("foo", "bar");
874 * but ``atomically''. Can't do full commit without saving state in the
875 * inode on disk which isn't feasible at this time. Best we can do is
876 * always guarantee the target exists.
878 * Basic algorithm is:
880 * 1) Bump link count on source while we're linking it to the
881 * target. This also ensure the inode won't be deleted out
882 * from underneath us while we work (it may be truncated by
883 * a concurrent `trunc' or `open' for creation).
884 * 2) Link source to destination. If destination already exists,
886 * 3) Unlink source reference to inode if still around. If a
887 * directory was moved and the parent of the destination
888 * is different from the source, patch the ".." entry in the
891 * ufs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
892 * struct componentname *a_fcnp, struct vnode *a_tdvp,
893 * struct vnode *a_tvp, struct componentname *a_tcnp)
897 ufs_rename(struct vop_old_rename_args *ap)
899 struct vnode *tvp = ap->a_tvp;
900 struct vnode *tdvp = ap->a_tdvp;
901 struct vnode *fvp = ap->a_fvp;
902 struct vnode *fdvp = ap->a_fdvp;
903 struct componentname *tcnp = ap->a_tcnp;
904 struct componentname *fcnp = ap->a_fcnp;
905 struct inode *ip, *xp, *dp;
906 struct direct newdir;
907 ino_t oldparent = 0, newparent = 0;
908 int doingdirectory = 0;
909 int error = 0, ioflag;
912 * Check for cross-device rename.
914 if ((fvp->v_mount != tdvp->v_mount) ||
915 (tvp && (fvp->v_mount != tvp->v_mount))) {
929 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
930 (VTOI(tdvp)->i_flags & APPEND))) {
936 * Renaming a file to itself has no effect. The upper layers should
937 * not call us in that case. Temporarily just warn if they do.
940 kprintf("ufs_rename: fvp == tvp (can't happen)\n");
945 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
949 * Note: now that fvp is locked we have to be sure to unlock it before
950 * using the 'abortit' target.
954 if (ip->i_nlink >= LINK_MAX) {
959 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
960 || (dp->i_flags & APPEND)) {
965 if ((ip->i_mode & IFMT) == IFDIR) {
967 * Avoid ".", "..", and aliases of "." for obvious reasons.
969 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
970 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & CNP_ISDOTDOT ||
971 (ip->i_flag & IN_RENAME)) {
976 ip->i_flag |= IN_RENAME;
977 oldparent = dp->i_number;
980 VN_KNOTE(fdvp, NOTE_WRITE); /* XXX right place? */
983 * fvp still locked. ip->i_flag has IN_RENAME set if doingdirectory.
984 * Cleanup fvp requirements so we can unlock it.
986 * tvp and tdvp are locked. tvp may be NULL. Now that dp and xp
987 * is setup we can use the 'bad' target if we unlock fvp. We cannot
988 * use the abortit target anymore because of IN_RENAME.
997 * 1) Bump link count while we're moving stuff
998 * around. If we crash somewhere before
999 * completing our work, the link count
1000 * may be wrong, but correctable.
1004 ip->i_flag |= IN_CHANGE;
1005 if (DOINGSOFTDEP(fvp))
1006 softdep_change_linkcnt(ip);
1007 if ((error = ffs_update(fvp, !(DOINGSOFTDEP(fvp) |
1008 DOINGASYNC(fvp)))) != 0) {
1014 * If ".." must be changed (ie the directory gets a new
1015 * parent) then the source directory must not be in the
1016 * directory heirarchy above the target, as this would
1017 * orphan everything below the source directory. Also
1018 * the user must have write permission in the source so
1019 * as to be able to change "..". We must repeat the call
1020 * to namei, as the parent directory is unlocked by the
1021 * call to checkpath().
1023 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred);
1027 * We are now back to where we were in that fvp, fdvp are unlocked
1028 * and tvp, tdvp are locked. tvp may be NULL. IN_RENAME may be
1029 * set. Only the bad target or, if we clean up tvp and tdvp, the
1030 * out target, may be used.
1032 if (oldparent != dp->i_number)
1033 newparent = dp->i_number;
1034 if (doingdirectory && newparent) {
1035 if (error) /* write access check above */
1039 * Once we start messing with tvp and tdvp we cannot use the
1040 * 'bad' target, only finish cleaning tdvp and tvp up and
1041 * use the 'out' target.
1043 * This cleans up tvp.
1051 * This is a real mess. ufs_checkpath vput's the target
1052 * directory so retain an extra ref and note that tdvp will
1053 * lose its lock on return. This leaves us with one good
1054 * ref after ufs_checkpath returns.
1057 error = ufs_checkpath(ip, dp, tcnp->cn_cred);
1058 tcnp->cn_flags |= CNP_PDIRUNLOCK;
1065 * relookup no longer messes with tdvp's refs. tdvp must be
1066 * unlocked on entry and will be locked on a successful
1069 error = relookup(tdvp, &tvp, tcnp);
1071 if (tcnp->cn_flags & CNP_PDIRUNLOCK)
1077 KKASSERT((tcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
1084 * We are back to fvp, fdvp unlocked, tvp, tdvp locked. tvp may
1085 * be NULL (xp will also be NULL in that case), and IN_RENAME will
1086 * be set if doingdirectory. This means we can use the 'bad' target
1091 * 2) If target doesn't exist, link the target
1092 * to the source and unlink the source.
1093 * Otherwise, rewrite the target directory
1094 * entry to reference the source inode and
1095 * expunge the original entry's existence.
1098 if (dp->i_dev != ip->i_dev)
1099 panic("ufs_rename: EXDEV");
1101 * Account for ".." in new directory.
1102 * When source and destination have the same
1103 * parent we don't fool with the link count.
1105 if (doingdirectory && newparent) {
1106 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1112 dp->i_flag |= IN_CHANGE;
1113 if (DOINGSOFTDEP(tdvp))
1114 softdep_change_linkcnt(dp);
1115 error = ffs_update(tdvp, !(DOINGSOFTDEP(tdvp) |
1120 ufs_makedirentry(ip, tcnp, &newdir);
1121 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1123 if (doingdirectory && newparent) {
1126 dp->i_flag |= IN_CHANGE;
1127 if (DOINGSOFTDEP(tdvp))
1128 softdep_change_linkcnt(dp);
1129 (void)ffs_update(tdvp, 1);
1133 VN_KNOTE(tdvp, NOTE_WRITE);
1136 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1137 panic("ufs_rename: EXDEV");
1139 * Short circuit rename(foo, foo).
1141 if (xp->i_number == ip->i_number)
1142 panic("ufs_rename: same file");
1144 * If the parent directory is "sticky", then the user must
1145 * own the parent directory, or the destination of the rename,
1146 * otherwise the destination may not be changed (except by
1147 * root). This implements append-only directories.
1149 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
1150 tcnp->cn_cred->cr_uid != dp->i_uid &&
1151 xp->i_uid != tcnp->cn_cred->cr_uid) {
1156 * Target must be empty if a directory and have no links
1157 * to it. Also, ensure source and target are compatible
1158 * (both directories, or both not directories).
1160 * Purge the file or directory being replaced from the
1163 if ((xp->i_mode&IFMT) == IFDIR) {
1164 if ((xp->i_effnlink > 2) ||
1165 !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1169 if (!doingdirectory) {
1173 /* cache_purge removed - handled by VFS compat layer */
1174 } else if (doingdirectory == 0) {
1175 /* cache_purge removed - handled by VFS compat layer */
1181 * note: inode passed to ufs_dirrewrite() is 0 for a
1182 * non-directory file rename, 1 for a directory rename
1183 * in the same directory, and > 1 for an inode representing
1184 * the new directory.
1186 error = ufs_dirrewrite(dp, xp, ip->i_number,
1188 (doingdirectory && newparent) ?
1189 newparent : (ino_t)doingdirectory);
1192 if (doingdirectory) {
1195 if (DOINGSOFTDEP(tdvp))
1196 softdep_change_linkcnt(dp);
1199 if (DOINGSOFTDEP(tvp))
1200 softdep_change_linkcnt(xp);
1202 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1204 * Truncate inode. The only stuff left in the directory
1205 * is "." and "..". The "." reference is inconsequential
1206 * since we are quashing it. We have removed the "."
1207 * reference and the reference in the parent directory,
1208 * but there may be other hard links. The soft
1209 * dependency code will arrange to do these operations
1210 * after the parent directory entry has been deleted on
1211 * disk, so when running with that code we avoid doing
1216 dp->i_flag |= IN_CHANGE;
1219 xp->i_flag |= IN_CHANGE;
1220 ioflag = DOINGASYNC(tvp) ? 0 : IO_SYNC;
1221 error = ffs_truncate(tvp, (off_t)0, ioflag,
1226 VN_KNOTE(tdvp, NOTE_WRITE);
1228 VN_KNOTE(tvp, NOTE_DELETE);
1234 * tvp and tdvp have been cleaned up. only fvp and fdvp (both
1235 * unlocked) remain. We are about to overwrite fvp but we have to
1236 * keep 'ip' intact so we cannot release the old fvp, which is still
1237 * refd and accessible via ap->a_fvp.
1239 * This means we cannot use either 'bad' or 'out' to cleanup any
1244 * 3) Unlink the source.
1246 fcnp->cn_flags &= ~CNP_MODMASK;
1247 fcnp->cn_flags |= CNP_LOCKPARENT;
1248 error = relookup(fdvp, &fvp, fcnp);
1249 if (error || fvp == NULL) {
1251 * From name has disappeared. IN_RENAME will not be set if
1252 * we get past the panic so we don't have to clean it up.
1255 panic("ufs_rename: lost dir entry");
1257 if (fcnp->cn_flags & CNP_PDIRUNLOCK)
1263 KKASSERT((fcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
1266 * fdvp and fvp are locked.
1272 * Ensure that the directory entry still exists and has not
1273 * changed while the new name has been entered. If the source is
1274 * a file then the entry may have been unlinked or renamed. In
1275 * either case there is no further work to be done. If the source
1276 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1277 * flag ensures that it cannot be moved by another rename or removed
1278 * by a rmdir. Cleanup IN_RENAME.
1282 panic("ufs_rename: lost dir entry");
1285 * If the source is a directory with a
1286 * new parent, the link count of the old
1287 * parent directory must be decremented
1288 * and ".." set to point to the new parent.
1290 if (doingdirectory && newparent) {
1291 xp->i_offset = mastertemplate.dot_reclen;
1292 ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1293 /* cache_purge removed - handled by VFS compat layer */
1295 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1296 xp->i_flag &= ~IN_RENAME;
1299 VN_KNOTE(fvp, NOTE_RENAME);
1311 ip->i_flag &= ~IN_RENAME;
1312 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1315 ip->i_flag |= IN_CHANGE;
1316 ip->i_flag &= ~IN_RENAME;
1317 if (DOINGSOFTDEP(fvp))
1318 softdep_change_linkcnt(ip);
1329 * ufs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1330 * struct componentname *a_cnp, struct vattr *a_vap)
1334 ufs_mkdir(struct vop_old_mkdir_args *ap)
1336 struct vnode *dvp = ap->a_dvp;
1337 struct vattr *vap = ap->a_vap;
1338 struct componentname *cnp = ap->a_cnp;
1339 struct inode *ip, *dp;
1342 struct dirtemplate dirtemplate, *dtp;
1343 struct direct newdir;
1348 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1352 dmode = vap->va_mode & 0777;
1355 * Must simulate part of ufs_makeinode here to acquire the inode,
1356 * but not have it entered in the parent directory. The entry is
1357 * made later after writing "." and ".." entries.
1359 error = ffs_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1363 ip->i_gid = dp->i_gid;
1367 struct ucred ucred, *ucp;
1371 * If we are hacking owners here, (only do this where told to)
1372 * and we are not giving it TO root, (would subvert quotas)
1373 * then go ahead and give it to the other user.
1374 * The new directory also inherits the SUID bit.
1375 * If user's UID and dir UID are the same,
1376 * 'give it away' so that the SUID is still forced on.
1378 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1379 (dp->i_mode & ISUID) && dp->i_uid) {
1381 ip->i_uid = dp->i_uid;
1383 if (dp->i_uid != cnp->cn_cred->cr_uid) {
1385 * Make sure the correct user gets charged
1387 * Make a dummy credential for the victim.
1388 * XXX This seems to never be accessed out of
1389 * our context so a stack variable is ok.
1392 ucred.cr_uid = ip->i_uid;
1393 ucred.cr_ngroups = 1;
1394 ucred.cr_groups[0] = dp->i_gid;
1399 ip->i_uid = cnp->cn_cred->cr_uid;
1401 if ((error = ufs_getinoquota(ip)) ||
1402 (error = ufs_chkiq(ip, 1, ucp, 0))) {
1403 ffs_vfree(tvp, ip->i_number, dmode);
1409 #else /* !SUIDDIR */
1410 ip->i_uid = cnp->cn_cred->cr_uid;
1412 if ((error = ufs_getinoquota(ip)) ||
1413 (error = ufs_chkiq(ip, 1, cnp->cn_cred, 0))) {
1414 ffs_vfree(tvp, ip->i_number, dmode);
1419 #endif /* !SUIDDIR */
1420 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1422 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1425 if (DOINGSOFTDEP(tvp))
1426 softdep_change_linkcnt(ip);
1427 if (cnp->cn_flags & CNP_ISWHITEOUT)
1428 ip->i_flags |= UF_OPAQUE;
1431 * Bump link count in parent directory to reflect work done below.
1432 * Should be done before reference is created so cleanup is
1433 * possible if we crash.
1437 dp->i_flag |= IN_CHANGE;
1438 if (DOINGSOFTDEP(dvp))
1439 softdep_change_linkcnt(dp);
1440 error = ffs_update(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1445 * The vnode must have a VM object in order to issue buffer cache
1448 vinitvmio(tvp, DIRBLKSIZ);
1451 * Initialize directory with "." and ".." from static template.
1453 if (dvp->v_mount->mnt_maxsymlinklen > 0)
1454 dtp = &mastertemplate;
1456 dtp = (struct dirtemplate *)&omastertemplate;
1458 dirtemplate.dot_ino = ip->i_number;
1459 dirtemplate.dotdot_ino = dp->i_number;
1460 vnode_pager_setsize(tvp, DIRBLKSIZ);
1461 error = VOP_BALLOC(tvp, 0LL, DIRBLKSIZ, cnp->cn_cred, B_CLRBUF, &bp);
1464 ip->i_size = DIRBLKSIZ;
1465 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1466 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1467 if (DOINGSOFTDEP(tvp)) {
1469 * Ensure that the entire newly allocated block is a
1470 * valid directory so that future growth within the
1471 * block does not have to ensure that the block is
1472 * written before the inode.
1475 while (blkoff < bp->b_bcount) {
1477 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1478 blkoff += DIRBLKSIZ;
1481 if ((error = ffs_update(tvp, !(DOINGSOFTDEP(tvp) |
1482 DOINGASYNC(tvp)))) != 0) {
1487 * Directory set up, now install its entry in the parent directory.
1489 * If we are not doing soft dependencies, then we must write out the
1490 * buffer containing the new directory body before entering the new
1491 * name in the parent. If we are doing soft dependencies, then the
1492 * buffer containing the new directory body will be passed to and
1493 * released in the soft dependency code after the code has attached
1494 * an appropriate ordering dependency to the buffer which ensures that
1495 * the buffer is written before the new name is written in the parent.
1497 if (DOINGASYNC(dvp))
1499 else if (!DOINGSOFTDEP(dvp) && (error = bwrite(bp)) != 0)
1501 ufs_makedirentry(ip, cnp, &newdir);
1502 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1506 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1511 dp->i_flag |= IN_CHANGE;
1512 if (DOINGSOFTDEP(dvp))
1513 softdep_change_linkcnt(dp);
1515 * No need to do an explicit VOP_TRUNCATE here, vrele will
1516 * do this for us because we set the link count to 0.
1520 ip->i_flag |= IN_CHANGE;
1521 if (DOINGSOFTDEP(tvp))
1522 softdep_change_linkcnt(ip);
1530 * Rmdir system call.
1532 * ufs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1533 * struct componentname *a_cnp)
1537 ufs_rmdir(struct vop_old_rmdir_args *ap)
1539 struct vnode *vp = ap->a_vp;
1540 struct vnode *dvp = ap->a_dvp;
1541 struct componentname *cnp = ap->a_cnp;
1542 struct inode *ip, *dp;
1549 * Do not remove a directory that is in the process of being renamed.
1550 * Verify the directory is empty (and valid). Rmdir ".." will not be
1551 * valid since ".." will contain a reference to the current directory
1552 * and thus be non-empty. Do not allow the removal of mounted on
1553 * directories (this can happen when an NFS exported filesystem
1554 * tries to remove a locally mounted on directory).
1557 if (ip->i_flag & IN_RENAME) {
1561 if (ip->i_effnlink != 2 ||
1562 !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1566 if ((dp->i_flags & APPEND)
1567 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1572 * Delete reference to directory before purging
1573 * inode. If we crash in between, the directory
1574 * will be reattached to lost+found,
1578 if (DOINGSOFTDEP(vp)) {
1579 softdep_change_linkcnt(dp);
1580 softdep_change_linkcnt(ip);
1582 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1586 if (DOINGSOFTDEP(vp)) {
1587 softdep_change_linkcnt(dp);
1588 softdep_change_linkcnt(ip);
1592 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1594 * Truncate inode. The only stuff left in the directory is "." and
1595 * "..". The "." reference is inconsequential since we are quashing
1596 * it. The soft dependency code will arrange to do these operations
1597 * after the parent directory entry has been deleted on disk, so
1598 * when running with that code we avoid doing them now.
1600 if (!DOINGSOFTDEP(vp)) {
1602 dp->i_flag |= IN_CHANGE;
1604 ip->i_flag |= IN_CHANGE;
1605 ioflag = DOINGASYNC(vp) ? 0 : IO_SYNC;
1606 error = ffs_truncate(vp, (off_t)0, ioflag, cnp->cn_cred);
1608 /* cache_purge removed - handled by VFS compat layer */
1610 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1611 if (ip->i_dirhash != NULL)
1612 ufsdirhash_free(ip);
1615 VN_KNOTE(vp, NOTE_DELETE);
1620 * symlink -- make a symbolic link
1622 * ufs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1623 * struct componentname *a_cnp, struct vattr *a_vap,
1628 ufs_symlink(struct vop_old_symlink_args *ap)
1630 struct vnode *vp, **vpp = ap->a_vpp;
1634 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1638 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1640 len = strlen(ap->a_target);
1641 if (len < vp->v_mount->mnt_maxsymlinklen) {
1643 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1645 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1648 * Make sure we have a VM object in order to use
1651 if (vp->v_object == NULL)
1653 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1654 UIO_SYSSPACE, IO_NODELOCKED,
1655 ap->a_cnp->cn_cred, NULL);
1663 * Vnode op for reading directories.
1665 * ufs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
1666 * int *a_eofflag, int *ncookies, off_t **a_cookies)
1670 ufs_readdir(struct vop_readdir_args *ap)
1672 struct uio *uio = ap->a_uio;
1673 struct vnode *vp = ap->a_vp;
1678 int offset; /* offset into buffer cache buffer */
1679 int eoffset; /* end of buffer clipped to file EOF */
1680 int pickup; /* pickup point */
1685 if (uio->uio_offset < 0)
1688 * Guess the number of cookies needed. Make sure we compute at
1689 * least 1, and no more then a reasonable limit.
1691 if (ap->a_ncookies) {
1692 ncookies = uio->uio_resid / 16 + 1;
1693 if (ncookies > 1024)
1695 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1697 ncookies = -1; /* force conditionals below */
1702 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
1708 if (uio->uio_offset >= VTOI(vp)->i_size) {
1711 if (ap->a_ncookies) {
1712 *ap->a_ncookies = cookie_index;
1713 *ap->a_cookies = cookies;
1719 * Loop until we run out of cookies, we run out of user buffer,
1720 * or we hit the directory EOF.
1722 * Always start scans at the beginning of the buffer, don't trust
1723 * the offset supplied by userland.
1725 while ((error = ffs_blkatoff_ra(vp, uio->uio_offset, NULL, &bp, 2)) == 0) {
1726 pickup = (int)(uio->uio_offset - bp->b_loffset);
1729 if (bp->b_loffset + bp->b_bcount > VTOI(vp)->i_size)
1730 eoffset = (int)(VTOI(vp)->i_size - bp->b_loffset);
1732 eoffset = bp->b_bcount;
1734 while (offset < eoffset) {
1735 dp = (struct direct *)(bp->b_data + offset);
1736 if (dp->d_reclen <= 0 || (dp->d_reclen & 3) ||
1737 offset + dp->d_reclen > bp->b_bcount) {
1741 if (offsetof(struct direct, d_name[dp->d_namlen]) > dp->d_reclen) {
1745 if (offset < pickup) {
1746 offset += dp->d_reclen;
1749 #if BYTE_ORDER == LITTLE_ENDIAN
1751 retval = vop_write_dirent(&error, uio,
1752 dp->d_ino, dp->d_namlen, dp->d_type,
1757 retval = vop_write_dirent(&error, uio,
1758 dp->d_ino, dp->d_type, dp->d_namlen,
1764 cookies[cookie_index] = bp->b_loffset + offset;
1766 offset += dp->d_reclen;
1767 if (cookie_index == ncookies)
1772 * This will align the next loop to the beginning of the
1773 * next block, and pickup will calculate to 0.
1775 uio->uio_offset = bp->b_loffset + offset;
1778 if (retval || error || cookie_index == ncookies ||
1779 uio->uio_offset >= VTOI(vp)->i_size) {
1784 *ap->a_eofflag = VTOI(vp)->i_size <= uio->uio_offset;
1787 * Report errors only if we didn't manage to read anything
1789 if (error && cookie_index == 0) {
1791 kfree(cookies, M_TEMP);
1792 *ap->a_ncookies = 0;
1793 *ap->a_cookies = NULL;
1798 *ap->a_ncookies = cookie_index;
1799 *ap->a_cookies = cookies;
1808 * Return target name of a symbolic link
1810 * ufs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1814 ufs_readlink(struct vop_readlink_args *ap)
1816 struct vnode *vp = ap->a_vp;
1817 struct inode *ip = VTOI(vp);
1821 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1822 (ip->i_din.di_blocks == 0)) { /* XXX - for old fastlink support */
1823 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1828 * Perform the equivalent of an OPEN on vp so we can issue a
1831 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1835 * Calculate the logical to physical mapping if not done already,
1836 * then call the device strategy routine.
1838 * In order to be able to swap to a file, the VOP_BMAP operation may not
1839 * deadlock on memory. See ufs_bmap() for details.
1841 * ufs_strategy(struct vnode *a_vp, struct bio *a_bio)
1845 ufs_strategy(struct vop_strategy_args *ap)
1847 struct bio *bio = ap->a_bio;
1849 struct buf *bp = bio->bio_buf;
1850 struct vnode *vp = ap->a_vp;
1855 if (vp->v_type == VBLK || vp->v_type == VCHR)
1856 panic("ufs_strategy: spec");
1857 nbio = push_bio(bio);
1858 if (nbio->bio_offset == NOOFFSET) {
1859 error = VOP_BMAP(vp, bio->bio_offset, &nbio->bio_offset,
1860 NULL, NULL, bp->b_cmd);
1862 bp->b_error = error;
1863 bp->b_flags |= B_ERROR;
1864 /* I/O was never started on nbio, must biodone(bio) */
1868 if (nbio->bio_offset == NOOFFSET)
1871 if (nbio->bio_offset == NOOFFSET) {
1873 * We hit a hole in the file. The buffer has been zero-filled
1874 * so just biodone() it.
1878 vn_strategy(ip->i_devvp, nbio);
1884 * Print out the contents of an inode.
1886 * ufs_print(struct vnode *a_vp)
1890 ufs_print(struct vop_print_args *ap)
1892 struct vnode *vp = ap->a_vp;
1893 struct inode *ip = VTOI(vp);
1895 kprintf("tag VT_UFS, ino %lu, on dev %s (%d, %d)",
1896 (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev),
1898 if (vp->v_type == VFIFO)
1900 lockmgr_printinfo(&vp->v_lock);
1906 * Read wrapper for special devices.
1908 * ufsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1909 * struct ucred *a_cred)
1913 ufsspec_read(struct vop_read_args *ap)
1920 resid = uio->uio_resid;
1921 error = VOCALL(&spec_vnode_vops, &ap->a_head);
1923 * The inode may have been revoked during the call, so it must not
1924 * be accessed blindly here or in the other wrapper functions.
1926 ip = VTOI(ap->a_vp);
1927 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1928 ip->i_flag |= IN_ACCESS;
1933 * Write wrapper for special devices.
1935 * ufsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1936 * struct ucred *a_cred)
1940 ufsspec_write(struct vop_write_args *ap)
1947 resid = uio->uio_resid;
1948 error = VOCALL(&spec_vnode_vops, &ap->a_head);
1949 ip = VTOI(ap->a_vp);
1950 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1951 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1956 * Close wrapper for special devices.
1958 * Update the times on the inode then do device close.
1960 * ufsspec_close(struct vnode *a_vp, int a_fflag)
1964 ufsspec_close(struct vop_close_args *ap)
1966 struct vnode *vp = ap->a_vp;
1968 if (vp->v_sysref.refcnt > 1)
1970 return (VOCALL(&spec_vnode_vops, &ap->a_head));
1974 * Read wrapper for fifos.
1976 * ufsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1977 * struct ucred *a_cred)
1981 ufsfifo_read(struct vop_read_args *ap)
1988 resid = uio->uio_resid;
1989 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1990 ip = VTOI(ap->a_vp);
1991 if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL &&
1992 (uio->uio_resid != resid || (error == 0 && resid != 0)))
1993 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1998 * Write wrapper for fifos.
2000 * ufsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
2001 * struct ucred *a_cred)
2005 ufsfifo_write(struct vop_write_args *ap)
2012 resid = uio->uio_resid;
2013 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2014 ip = VTOI(ap->a_vp);
2015 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
2016 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
2021 * Close wrapper for fifos.
2023 * Update the times on the inode then do device close.
2025 * ufsfifo_close(struct vnode *a_vp, int a_fflag)
2029 ufsfifo_close(struct vop_close_args *ap)
2031 struct vnode *vp = ap->a_vp;
2033 if (vp->v_sysref.refcnt > 1)
2035 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
2039 * Kqfilter wrapper for fifos.
2041 * Fall through to ufs kqfilter routines if needed
2045 ufsfifo_kqfilter(struct vop_kqfilter_args *ap)
2049 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2051 error = ufs_kqfilter(ap);
2056 * Advisory record locking support
2058 * ufs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
2063 ufs_advlock(struct vop_advlock_args *ap)
2065 struct inode *ip = VTOI(ap->a_vp);
2067 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
2071 * Initialize the vnode associated with a new inode, handle aliased
2074 * Make sure directories have their VM object now rather then later,
2075 * saving us from having to check on all the myrid directory VOPs
2076 * that might be executed without a VOP_OPEN being performed.
2079 ufs_vinit(struct mount *mntp, struct vnode **vpp)
2088 vp->v_type = IFTOVT(ip->i_mode);
2090 switch(vp->v_type) {
2093 vp->v_ops = &mntp->mnt_vn_spec_ops;
2094 addaliasu(vp, umajor(ip->i_rdev), uminor(ip->i_rdev));
2097 vp->v_ops = &mntp->mnt_vn_fifo_ops;
2101 vinitvmio(vp, ip->i_size);
2104 if (ip->i_size >= vp->v_mount->mnt_maxsymlinklen)
2105 vinitvmio(vp, ip->i_size);
2112 if (ip->i_number == ROOTINO)
2113 vp->v_flag |= VROOT;
2115 * Initialize modrev times
2117 getmicrouptime(&tv);
2118 SETHIGH(ip->i_modrev, tv.tv_sec);
2119 SETLOW(ip->i_modrev, tv.tv_usec * 4294);
2125 * Allocate a new inode.
2129 ufs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
2130 struct componentname *cnp)
2132 struct inode *ip, *pdir;
2133 struct direct newdir;
2139 if ((mode & IFMT) == 0)
2142 error = ffs_valloc(dvp, mode, cnp->cn_cred, &tvp);
2146 ip->i_flags = pdir->i_flags & (SF_NOHISTORY|UF_NOHISTORY|UF_NODUMP);
2147 ip->i_gid = pdir->i_gid;
2151 struct ucred ucred, *ucp;
2155 * If we are not the owner of the directory,
2156 * and we are hacking owners here, (only do this where told to)
2157 * and we are not giving it TO root, (would subvert quotas)
2158 * then go ahead and give it to the other user.
2159 * Note that this drops off the execute bits for security.
2161 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2162 (pdir->i_mode & ISUID) &&
2163 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2164 ip->i_uid = pdir->i_uid;
2168 * Make sure the correct user gets charged
2170 * Quickly knock up a dummy credential for the victim.
2171 * XXX This seems to never be accessed out of our
2172 * context so a stack variable is ok.
2175 ucred.cr_uid = ip->i_uid;
2176 ucred.cr_ngroups = 1;
2177 ucred.cr_groups[0] = pdir->i_gid;
2181 ip->i_uid = cnp->cn_cred->cr_uid;
2184 if ((error = ufs_getinoquota(ip)) ||
2185 (error = ufs_chkiq(ip, 1, ucp, 0))) {
2186 ffs_vfree(tvp, ip->i_number, mode);
2192 #else /* !SUIDDIR */
2193 ip->i_uid = cnp->cn_cred->cr_uid;
2195 if ((error = ufs_getinoquota(ip)) ||
2196 (error = ufs_chkiq(ip, 1, cnp->cn_cred, 0))) {
2197 ffs_vfree(tvp, ip->i_number, mode);
2202 #endif /* !SUIDDIR */
2203 ip->i_fsmid = cache_getnewfsmid();
2204 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2206 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2209 if (DOINGSOFTDEP(tvp))
2210 softdep_change_linkcnt(ip);
2211 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2212 priv_check_cred(cnp->cn_cred, PRIV_ROOT, 0)) {
2213 ip->i_mode &= ~ISGID;
2216 if (cnp->cn_flags & CNP_ISWHITEOUT)
2217 ip->i_flags |= UF_OPAQUE;
2220 * Regular files and directories need VM objects. Softlinks do
2221 * not (not immediately anyway).
2223 if (tvp->v_type == VREG || tvp->v_type == VDIR)
2227 * Make sure inode goes to disk before directory entry.
2229 error = ffs_update(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2232 ufs_makedirentry(ip, cnp, &newdir);
2233 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2241 * Write error occurred trying to update the inode
2242 * or the directory so must deallocate the inode.
2246 ip->i_flag |= IN_CHANGE;
2247 if (DOINGSOFTDEP(tvp))
2248 softdep_change_linkcnt(ip);
2254 ufs_missingop(struct vop_generic_args *ap)
2256 panic("no vop function for %s in ufs child", ap->a_desc->sd_name);
2257 return (EOPNOTSUPP);
2260 static struct filterops ufsread_filtops =
2261 { 1, NULL, filt_ufsdetach, filt_ufsread };
2262 static struct filterops ufswrite_filtops =
2263 { 1, NULL, filt_ufsdetach, filt_ufswrite };
2264 static struct filterops ufsvnode_filtops =
2265 { 1, NULL, filt_ufsdetach, filt_ufsvnode };
2268 * ufs_kqfilter(struct vnode *a_vp, struct knote *a_kn)
2271 ufs_kqfilter(struct vop_kqfilter_args *ap)
2273 struct vnode *vp = ap->a_vp;
2274 struct knote *kn = ap->a_kn;
2277 switch (kn->kn_filter) {
2279 kn->kn_fop = &ufsread_filtops;
2282 kn->kn_fop = &ufswrite_filtops;
2285 kn->kn_fop = &ufsvnode_filtops;
2291 kn->kn_hook = (caddr_t)vp;
2293 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2294 SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
2295 lwkt_reltoken(&ilock);
2301 filt_ufsdetach(struct knote *kn)
2303 struct vnode *vp = (struct vnode *)kn->kn_hook;
2306 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2307 SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
2308 kn, knote, kn_selnext);
2309 lwkt_reltoken(&ilock);
2314 filt_ufsread(struct knote *kn, long hint)
2316 struct vnode *vp = (struct vnode *)kn->kn_hook;
2317 struct inode *ip = VTOI(vp);
2320 * filesystem is gone, so set the EOF flag and schedule
2321 * the knote for deletion.
2323 if (hint == NOTE_REVOKE) {
2324 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2328 kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
2329 return (kn->kn_data != 0);
2334 filt_ufswrite(struct knote *kn, long hint)
2337 * filesystem is gone, so set the EOF flag and schedule
2338 * the knote for deletion.
2340 if (hint == NOTE_REVOKE)
2341 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2348 filt_ufsvnode(struct knote *kn, long hint)
2350 if (kn->kn_sfflags & hint)
2351 kn->kn_fflags |= hint;
2352 if (hint == NOTE_REVOKE) {
2353 kn->kn_flags |= EV_EOF;
2356 return (kn->kn_fflags != 0);
2359 /* Global vfs data structures for ufs. */
2360 static struct vop_ops ufs_vnode_vops = {
2361 .vop_default = vop_defaultop,
2362 .vop_fsync = (void *)ufs_missingop,
2363 .vop_read = (void *)ufs_missingop,
2364 .vop_reallocblks = (void *)ufs_missingop,
2365 .vop_write = (void *)ufs_missingop,
2366 .vop_access = ufs_access,
2367 .vop_advlock = ufs_advlock,
2368 .vop_bmap = ufs_bmap,
2369 .vop_old_lookup = ufs_lookup,
2370 .vop_close = ufs_close,
2371 .vop_old_create = ufs_create,
2372 .vop_getattr = ufs_getattr,
2373 .vop_inactive = ufs_inactive,
2374 .vop_old_link = ufs_link,
2375 .vop_old_mkdir = ufs_mkdir,
2376 .vop_old_mknod = ufs_mknod,
2377 .vop_mmap = ufs_mmap,
2378 .vop_open = ufs_open,
2379 .vop_pathconf = vop_stdpathconf,
2380 .vop_poll = vop_stdpoll,
2381 .vop_kqfilter = ufs_kqfilter,
2382 .vop_print = ufs_print,
2383 .vop_readdir = ufs_readdir,
2384 .vop_readlink = ufs_readlink,
2385 .vop_reclaim = ufs_reclaim,
2386 .vop_old_remove = ufs_remove,
2387 .vop_old_rename = ufs_rename,
2388 .vop_old_rmdir = ufs_rmdir,
2389 .vop_setattr = ufs_setattr,
2390 .vop_markatime = ufs_markatime,
2391 .vop_strategy = ufs_strategy,
2392 .vop_old_symlink = ufs_symlink,
2393 .vop_old_whiteout = ufs_whiteout
2396 static struct vop_ops ufs_spec_vops = {
2397 .vop_default = spec_vnoperate,
2398 .vop_fsync = (void *)ufs_missingop,
2399 .vop_access = ufs_access,
2400 .vop_close = ufsspec_close,
2401 .vop_getattr = ufs_getattr,
2402 .vop_inactive = ufs_inactive,
2403 .vop_print = ufs_print,
2404 .vop_read = ufsspec_read,
2405 .vop_reclaim = ufs_reclaim,
2406 .vop_setattr = ufs_setattr,
2407 .vop_markatime = ufs_markatime,
2408 .vop_write = ufsspec_write
2411 static struct vop_ops ufs_fifo_vops = {
2412 .vop_default = fifo_vnoperate,
2413 .vop_fsync = (void *)ufs_missingop,
2414 .vop_access = ufs_access,
2415 .vop_close = ufsfifo_close,
2416 .vop_getattr = ufs_getattr,
2417 .vop_inactive = ufs_inactive,
2418 .vop_kqfilter = ufsfifo_kqfilter,
2419 .vop_print = ufs_print,
2420 .vop_read = ufsfifo_read,
2421 .vop_reclaim = ufs_reclaim,
2422 .vop_setattr = ufs_setattr,
2423 .vop_markatime = ufs_markatime,
2424 .vop_write = ufsfifo_write
2427 VNODEOP_SET(ufs_vnode_vops);
2428 VNODEOP_SET(ufs_spec_vops);
2429 VNODEOP_SET(ufs_fifo_vops);
2435 ufs_vnoperate(struct vop_generic_args *ap)
2437 return (VOCALL(&ufs_vnode_vops, ap));
2441 * ufs_vnoperatefifo()
2444 ufs_vnoperatefifo(struct vop_generic_args *ap)
2446 return (VOCALL(&ufs_fifo_vops, ap));
2450 * ufs_vnoperatespec()
2453 ufs_vnoperatespec(struct vop_generic_args *ap)
2455 return (VOCALL(&ufs_spec_vops, ap));