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.47 2006/05/05 21:15:10 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 */
68 #include <vm/vm_extern.h>
70 #include <vfs/fifofs/fifo.h>
76 #include "ufs_extern.h"
81 static int ufs_access (struct vop_access_args *);
82 static int ufs_advlock (struct vop_advlock_args *);
83 static int ufs_chmod (struct vnode *, int, struct ucred *, struct thread *);
84 static int ufs_chown (struct vnode *, uid_t, gid_t, struct ucred *, struct thread *);
85 static int ufs_close (struct vop_close_args *);
86 static int ufs_create (struct vop_old_create_args *);
87 static int ufs_getattr (struct vop_getattr_args *);
88 static int ufs_link (struct vop_old_link_args *);
89 static int ufs_makeinode (int mode, struct vnode *, struct vnode **, struct componentname *);
90 static int ufs_missingop (struct vop_generic_args *ap);
91 static int ufs_mkdir (struct vop_old_mkdir_args *);
92 static int ufs_mknod (struct vop_old_mknod_args *);
93 static int ufs_mmap (struct vop_mmap_args *);
94 static int ufs_open (struct vop_open_args *);
95 static int ufs_pathconf (struct vop_pathconf_args *);
96 static int ufs_print (struct vop_print_args *);
97 static int ufs_readdir (struct vop_readdir_args *);
98 static int ufs_readlink (struct vop_readlink_args *);
99 static int ufs_remove (struct vop_old_remove_args *);
100 static int ufs_rename (struct vop_old_rename_args *);
101 static int ufs_rmdir (struct vop_old_rmdir_args *);
102 static int ufs_setattr (struct vop_setattr_args *);
103 static int ufs_strategy (struct vop_strategy_args *);
104 static int ufs_symlink (struct vop_old_symlink_args *);
105 static int ufs_whiteout (struct vop_old_whiteout_args *);
106 static int ufsfifo_close (struct vop_close_args *);
107 static int ufsfifo_kqfilter (struct vop_kqfilter_args *);
108 static int ufsfifo_read (struct vop_read_args *);
109 static int ufsfifo_write (struct vop_write_args *);
110 static int ufsspec_close (struct vop_close_args *);
111 static int ufsspec_read (struct vop_read_args *);
112 static int ufsspec_write (struct vop_write_args *);
113 static int filt_ufsread (struct knote *kn, long hint);
114 static int filt_ufswrite (struct knote *kn, long hint);
115 static int filt_ufsvnode (struct knote *kn, long hint);
116 static void filt_ufsdetach (struct knote *kn);
117 static int ufs_kqfilter (struct vop_kqfilter_args *ap);
123 #define SETHIGH(q, h) { \
126 tmp.val[_QUAD_HIGHWORD] = (h); \
129 #define SETLOW(q, l) { \
132 tmp.val[_QUAD_LOWWORD] = (l); \
135 #define VN_KNOTE(vp, b) \
136 KNOTE(&vp->v_pollinfo.vpi_selinfo.si_note, (b))
138 #define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0)
141 * A virgin directory (no blushing please).
143 static struct dirtemplate mastertemplate = {
144 0, 12, DT_DIR, 1, ".",
145 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
147 static struct odirtemplate omastertemplate = {
149 0, DIRBLKSIZ - 12, 2, ".."
153 ufs_itimes(struct vnode *vp)
159 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
161 if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
162 ip->i_flag |= IN_LAZYMOD;
164 ip->i_flag |= IN_MODIFIED;
165 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
167 if (ip->i_flag & IN_ACCESS) {
168 ip->i_atime = ts.tv_sec;
169 ip->i_atimensec = ts.tv_nsec;
171 if (ip->i_flag & IN_UPDATE) {
172 ip->i_mtime = ts.tv_sec;
173 ip->i_mtimensec = ts.tv_nsec;
176 if (ip->i_flag & IN_CHANGE) {
177 ip->i_ctime = ts.tv_sec;
178 ip->i_ctimensec = ts.tv_nsec;
181 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
185 * Create a regular file
187 * ufs_create(struct vnode *a_dvp, struct vnode **a_vpp,
188 * struct componentname *a_cnp, struct vattr *a_vap)
192 ufs_create(struct vop_old_create_args *ap)
197 ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
198 ap->a_dvp, ap->a_vpp, ap->a_cnp);
201 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
208 * ufs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
209 * struct componentname *a_cnp, struct vattr *a_vap)
214 ufs_mknod(struct vop_old_mknod_args *ap)
216 struct vattr *vap = ap->a_vap;
217 struct vnode **vpp = ap->a_vpp;
222 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
223 ap->a_dvp, vpp, ap->a_cnp);
226 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
228 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
229 if (vap->va_rdev != VNOVAL) {
231 * Want to be able to use this to make badblock
232 * inodes, so don't truncate the dev number.
234 ip->i_rdev = vap->va_rdev;
237 * Remove inode, then reload it through VFS_VGET so it is
238 * checked to see if it is an alias of an existing entry in
241 (*vpp)->v_type = VNON;
242 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
245 error = VFS_VGET(ap->a_dvp->v_mount, ino, vpp);
258 * ufs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
259 * struct thread *a_td)
264 ufs_open(struct vop_open_args *ap)
266 struct vnode *vp = ap->a_vp;
269 * Files marked append-only must be opened for appending.
271 if ((VTOI(vp)->i_flags & APPEND) &&
272 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE) {
276 return (vop_stdopen(ap));
282 * Update the times on the inode.
284 * ufs_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
285 * struct thread *a_td)
290 ufs_close(struct vop_close_args *ap)
292 struct vnode *vp = ap->a_vp;
294 if (vp->v_usecount > 1)
296 return (vop_stdclose(ap));
300 * ufs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
301 * struct thread *a_td)
305 ufs_access(struct vop_access_args *ap)
307 struct vnode *vp = ap->a_vp;
308 struct inode *ip = VTOI(vp);
309 struct ucred *cred = ap->a_cred;
310 mode_t mask, mode = ap->a_mode;
318 * Disallow write attempts on read-only filesystems;
319 * unless the file is a socket, fifo, or a block or
320 * character device resident on the filesystem.
323 switch (vp->v_type) {
327 if (vp->v_mount->mnt_flag & MNT_RDONLY)
330 if ((error = ufs_getinoquota(ip)) != 0)
339 /* If immutable bit set, nobody gets to write it. */
340 if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE))
343 /* Otherwise, user id 0 always gets access. */
344 if (cred->cr_uid == 0)
349 /* Otherwise, check the owner. */
350 if (cred->cr_uid == ip->i_uid) {
357 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
360 /* Otherwise, check the groups. */
361 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
362 if (ip->i_gid == *gp) {
369 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
372 /* Otherwise, check everyone else. */
379 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
383 * ufs_getattr(struct vnode *a_vp, struct vattr *a_vap,
384 * struct thread *a_td)
389 ufs_getattr(struct vop_getattr_args *ap)
391 struct vnode *vp = ap->a_vp;
392 struct inode *ip = VTOI(vp);
393 struct vattr *vap = ap->a_vap;
396 * This may cause i_fsmid to be updated even if no change (0)
397 * is returned, but we should only write out the inode if non-zero
398 * is returned and if the mount is read-write.
400 if (cache_check_fsmid_vp(vp, &ip->i_fsmid) &&
401 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0
403 ip->i_flag |= IN_LAZYMOD;
408 * Copy from inode table
410 vap->va_fsid = dev2udev(ip->i_dev);
411 vap->va_fileid = ip->i_number;
412 vap->va_mode = ip->i_mode & ~IFMT;
413 vap->va_nlink = VFSTOUFS(vp->v_mount)->um_i_effnlink_valid ?
414 ip->i_effnlink : ip->i_nlink;
415 vap->va_uid = ip->i_uid;
416 vap->va_gid = ip->i_gid;
417 vap->va_rdev = ip->i_rdev;
418 vap->va_size = ip->i_din.di_size;
419 vap->va_atime.tv_sec = ip->i_atime;
420 vap->va_atime.tv_nsec = ip->i_atimensec;
421 vap->va_mtime.tv_sec = ip->i_mtime;
422 vap->va_mtime.tv_nsec = ip->i_mtimensec;
423 vap->va_ctime.tv_sec = ip->i_ctime;
424 vap->va_ctime.tv_nsec = ip->i_ctimensec;
425 vap->va_flags = ip->i_flags;
426 vap->va_gen = ip->i_gen;
427 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
428 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
429 vap->va_type = IFTOVT(ip->i_mode);
430 vap->va_filerev = ip->i_modrev;
431 vap->va_fsmid = ip->i_fsmid;
436 * Set attribute vnode op. called from several syscalls
438 * ufs_setattr(struct vnode *a_vp, struct vattr *a_vap,
439 * struct ucred *a_cred, struct thread *a_td)
443 ufs_setattr(struct vop_setattr_args *ap)
445 struct vattr *vap = ap->a_vap;
446 struct vnode *vp = ap->a_vp;
447 struct inode *ip = VTOI(vp);
448 struct ucred *cred = ap->a_cred;
452 * Check for unsettable attributes.
454 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
455 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
456 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
457 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
460 if (vap->va_flags != VNOVAL) {
461 if (vp->v_mount->mnt_flag & MNT_RDONLY)
463 if (cred->cr_uid != ip->i_uid &&
464 (error = suser_cred(cred, PRISON_ROOT)))
467 * Note that a root chflags becomes a user chflags when
468 * we are jailed, unless the jail.chflags_allowed sysctl
471 if (cred->cr_uid == 0 &&
472 (!jailed(cred) || jail_chflags_allowed)) {
474 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) &&
477 ip->i_flags = vap->va_flags;
480 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
481 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
483 ip->i_flags &= SF_SETTABLE;
484 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
486 ip->i_flag |= IN_CHANGE;
487 if (vap->va_flags & (IMMUTABLE | APPEND))
490 if (ip->i_flags & (IMMUTABLE | APPEND))
493 * Go through the fields and update iff not VNOVAL.
495 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
496 if (vp->v_mount->mnt_flag & MNT_RDONLY)
498 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred, ap->a_td)) != 0)
501 if (vap->va_size != VNOVAL) {
503 * Disallow write attempts on read-only filesystems;
504 * unless the file is a socket, fifo, or a block or
505 * character device resident on the filesystem.
507 switch (vp->v_type) {
512 if (vp->v_mount->mnt_flag & MNT_RDONLY)
518 if ((error = UFS_TRUNCATE(vp, vap->va_size, 0, cred, ap->a_td)) != 0)
522 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
523 if (vp->v_mount->mnt_flag & MNT_RDONLY)
525 if (cred->cr_uid != ip->i_uid &&
526 (error = suser_cred(cred, PRISON_ROOT)) &&
527 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
528 (error = VOP_ACCESS(vp, VWRITE, cred, ap->a_td))))
530 if (vap->va_atime.tv_sec != VNOVAL)
531 ip->i_flag |= IN_ACCESS;
532 if (vap->va_mtime.tv_sec != VNOVAL)
533 ip->i_flag |= IN_CHANGE | IN_UPDATE;
535 if (vap->va_atime.tv_sec != VNOVAL) {
536 ip->i_atime = vap->va_atime.tv_sec;
537 ip->i_atimensec = vap->va_atime.tv_nsec;
539 if (vap->va_mtime.tv_sec != VNOVAL) {
540 ip->i_mtime = vap->va_mtime.tv_sec;
541 ip->i_mtimensec = vap->va_mtime.tv_nsec;
543 error = UFS_UPDATE(vp, 0);
548 if (vap->va_mode != (mode_t)VNOVAL) {
549 if (vp->v_mount->mnt_flag & MNT_RDONLY)
551 error = ufs_chmod(vp, (int)vap->va_mode, cred, ap->a_td);
553 VN_KNOTE(vp, NOTE_ATTRIB);
558 * Change the mode on a file.
559 * Inode must be locked before calling.
562 ufs_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td)
564 struct inode *ip = VTOI(vp);
567 if (cred->cr_uid != ip->i_uid) {
568 error = suser_cred(cred, PRISON_ROOT);
573 if (vp->v_type != VDIR && (mode & S_ISTXT))
575 if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
578 ip->i_mode &= ~ALLPERMS;
579 ip->i_mode |= (mode & ALLPERMS);
580 ip->i_flag |= IN_CHANGE;
585 * Perform chown operation on inode ip;
586 * inode must be locked prior to call.
589 ufs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
592 struct inode *ip = VTOI(vp);
601 if (uid == (uid_t)VNOVAL)
603 if (gid == (gid_t)VNOVAL)
606 * If we don't own the file, are trying to change the owner
607 * of the file, or are not a member of the target group,
608 * the caller must be superuser or the call fails.
610 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
611 (gid != ip->i_gid && !(cred->cr_gid == gid ||
612 groupmember((gid_t)gid, cred)))) &&
613 (error = suser_cred(cred, PRISON_ROOT)))
618 if ((error = ufs_getinoquota(ip)) != 0)
621 ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
622 ip->i_dquot[USRQUOTA] = NODQUOT;
625 ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
626 ip->i_dquot[GRPQUOTA] = NODQUOT;
628 change = ip->i_blocks;
629 (void) ufs_chkdq(ip, -change, cred, CHOWN);
630 (void) ufs_chkiq(ip, -1, cred, CHOWN);
631 for (i = 0; i < MAXQUOTAS; i++) {
632 ufs_dqrele(vp, ip->i_dquot[i]);
633 ip->i_dquot[i] = NODQUOT;
639 if ((error = ufs_getinoquota(ip)) == 0) {
641 ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
642 ip->i_dquot[USRQUOTA] = NODQUOT;
645 ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
646 ip->i_dquot[GRPQUOTA] = NODQUOT;
648 if ((error = ufs_chkdq(ip, change, cred, CHOWN)) == 0) {
649 if ((error = ufs_chkiq(ip, 1, cred, CHOWN)) == 0)
652 (void)ufs_chkdq(ip, -change, cred, CHOWN|FORCE);
654 for (i = 0; i < MAXQUOTAS; i++) {
655 ufs_dqrele(vp, ip->i_dquot[i]);
656 ip->i_dquot[i] = NODQUOT;
661 if (ufs_getinoquota(ip) == 0) {
663 ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
664 ip->i_dquot[USRQUOTA] = NODQUOT;
667 ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
668 ip->i_dquot[GRPQUOTA] = NODQUOT;
670 (void) ufs_chkdq(ip, change, cred, FORCE|CHOWN);
671 (void) ufs_chkiq(ip, 1, cred, FORCE|CHOWN);
672 (void) ufs_getinoquota(ip);
676 if (ufs_getinoquota(ip))
677 panic("ufs_chown: lost quota");
679 ip->i_flag |= IN_CHANGE;
680 if (cred->cr_uid != 0 && (ouid != uid || ogid != gid))
681 ip->i_mode &= ~(ISUID | ISGID);
688 * NB Currently unsupported.
690 * ufs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
691 * struct thread *a_td)
696 ufs_mmap(struct vop_mmap_args *ap)
702 * ufs_remove(struct vnode *a_dvp, struct vnode *a_vp,
703 * struct componentname *a_cnp)
707 ufs_remove(struct vop_old_remove_args *ap)
710 struct vnode *vp = ap->a_vp;
711 struct vnode *dvp = ap->a_dvp;
715 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
716 (VTOI(dvp)->i_flags & APPEND)) {
720 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
721 VN_KNOTE(vp, NOTE_DELETE);
722 VN_KNOTE(dvp, NOTE_WRITE);
730 * ufs_link(struct vnode *a_tdvp, struct vnode *a_vp,
731 * struct componentname *a_cnp)
735 ufs_link(struct vop_old_link_args *ap)
737 struct vnode *vp = ap->a_vp;
738 struct vnode *tdvp = ap->a_tdvp;
739 struct componentname *cnp = ap->a_cnp;
741 struct direct newdir;
744 if (tdvp->v_mount != vp->v_mount) {
748 if (tdvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE))) {
752 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
756 if (ip->i_flags & (IMMUTABLE | APPEND)) {
762 ip->i_flag |= IN_CHANGE;
763 if (DOINGSOFTDEP(vp))
764 softdep_change_linkcnt(ip);
765 error = UFS_UPDATE(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
767 ufs_makedirentry(ip, cnp, &newdir);
768 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
774 ip->i_flag |= IN_CHANGE;
775 if (DOINGSOFTDEP(vp))
776 softdep_change_linkcnt(ip);
782 VN_KNOTE(vp, NOTE_LINK);
783 VN_KNOTE(tdvp, NOTE_WRITE);
788 * whiteout vnode call
790 * ufs_whiteout(struct vnode *a_dvp, struct componentname *a_cnp, int a_flags)
794 ufs_whiteout(struct vop_old_whiteout_args *ap)
796 struct vnode *dvp = ap->a_dvp;
797 struct componentname *cnp = ap->a_cnp;
798 struct direct newdir;
801 switch (ap->a_flags) {
803 /* 4.4 format directories support whiteout operations */
804 if (dvp->v_mount->mnt_maxsymlinklen > 0)
809 /* create a new directory whiteout */
811 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
812 panic("ufs_whiteout: old format filesystem");
816 newdir.d_namlen = cnp->cn_namelen;
817 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
818 newdir.d_type = DT_WHT;
819 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
823 /* remove an existing directory whiteout */
825 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
826 panic("ufs_whiteout: old format filesystem");
829 cnp->cn_flags &= ~CNP_DOWHITEOUT;
830 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
833 panic("ufs_whiteout: unknown op");
839 * Rename system call.
840 * rename("foo", "bar");
843 * link("foo", "bar");
845 * but ``atomically''. Can't do full commit without saving state in the
846 * inode on disk which isn't feasible at this time. Best we can do is
847 * always guarantee the target exists.
849 * Basic algorithm is:
851 * 1) Bump link count on source while we're linking it to the
852 * target. This also ensure the inode won't be deleted out
853 * from underneath us while we work (it may be truncated by
854 * a concurrent `trunc' or `open' for creation).
855 * 2) Link source to destination. If destination already exists,
857 * 3) Unlink source reference to inode if still around. If a
858 * directory was moved and the parent of the destination
859 * is different from the source, patch the ".." entry in the
862 * ufs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
863 * struct componentname *a_fcnp, struct vnode *a_tdvp,
864 * struct vnode *a_tvp, struct componentname *a_tcnp)
868 ufs_rename(struct vop_old_rename_args *ap)
870 struct vnode *tvp = ap->a_tvp;
871 struct vnode *tdvp = ap->a_tdvp;
872 struct vnode *fvp = ap->a_fvp;
873 struct vnode *fdvp = ap->a_fdvp;
874 struct componentname *tcnp = ap->a_tcnp;
875 struct componentname *fcnp = ap->a_fcnp;
876 struct inode *ip, *xp, *dp;
877 struct direct newdir;
878 ino_t oldparent = 0, newparent = 0;
879 int doingdirectory = 0;
880 int error = 0, ioflag;
883 * Check for cross-device rename.
885 if ((fvp->v_mount != tdvp->v_mount) ||
886 (tvp && (fvp->v_mount != tvp->v_mount))) {
900 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
901 (VTOI(tdvp)->i_flags & APPEND))) {
907 * Renaming a file to itself has no effect. The upper layers should
908 * not call us in that case. Temporarily just warn if they do.
911 printf("ufs_rename: fvp == tvp (can't happen)\n");
916 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
920 * Note: now that fvp is locked we have to be sure to unlock it before
921 * using the 'abortit' target.
925 if (ip->i_nlink >= LINK_MAX) {
930 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
931 || (dp->i_flags & APPEND)) {
936 if ((ip->i_mode & IFMT) == IFDIR) {
938 * Avoid ".", "..", and aliases of "." for obvious reasons.
940 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
941 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & CNP_ISDOTDOT ||
942 (ip->i_flag & IN_RENAME)) {
947 ip->i_flag |= IN_RENAME;
948 oldparent = dp->i_number;
951 VN_KNOTE(fdvp, NOTE_WRITE); /* XXX right place? */
954 * fvp still locked. ip->i_flag has IN_RENAME set if doingdirectory.
955 * Cleanup fvp requirements so we can unlock it.
957 * tvp and tdvp are locked. tvp may be NULL. Now that dp and xp
958 * is setup we can use the 'bad' target if we unlock fvp. We cannot
959 * use the abortit target anymore because of IN_RENAME.
968 * 1) Bump link count while we're moving stuff
969 * around. If we crash somewhere before
970 * completing our work, the link count
971 * may be wrong, but correctable.
975 ip->i_flag |= IN_CHANGE;
976 if (DOINGSOFTDEP(fvp))
977 softdep_change_linkcnt(ip);
978 if ((error = UFS_UPDATE(fvp, !(DOINGSOFTDEP(fvp) |
979 DOINGASYNC(fvp)))) != 0) {
985 * If ".." must be changed (ie the directory gets a new
986 * parent) then the source directory must not be in the
987 * directory heirarchy above the target, as this would
988 * orphan everything below the source directory. Also
989 * the user must have write permission in the source so
990 * as to be able to change "..". We must repeat the call
991 * to namei, as the parent directory is unlocked by the
992 * call to checkpath().
994 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_td);
998 * We are now back to where we were in that fvp, fdvp are unlocked
999 * and tvp, tdvp are locked. tvp may be NULL. IN_RENAME may be
1000 * set. Only the bad target or, if we clean up tvp and tdvp, the
1001 * out target, may be used.
1003 if (oldparent != dp->i_number)
1004 newparent = dp->i_number;
1005 if (doingdirectory && newparent) {
1006 if (error) /* write access check above */
1010 * Once we start messing with tvp and tdvp we cannot use the
1011 * 'bad' target, only finish cleaning tdvp and tvp up and
1012 * use the 'out' target.
1014 * This cleans up tvp.
1022 * This is a real mess. ufs_checkpath vput's the target
1023 * directory so retain an extra ref and note that tdvp will
1024 * lose its lock on return. This leaves us with one good
1025 * ref after ufs_checkpath returns.
1028 error = ufs_checkpath(ip, dp, tcnp->cn_cred);
1029 tcnp->cn_flags |= CNP_PDIRUNLOCK;
1036 * relookup no longer messes with tdvp's refs. tdvp must be
1037 * unlocked on entry and will be locked on a successful
1040 error = relookup(tdvp, &tvp, tcnp);
1042 if (tcnp->cn_flags & CNP_PDIRUNLOCK)
1048 KKASSERT((tcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
1055 * We are back to fvp, fdvp unlocked, tvp, tdvp locked. tvp may
1056 * be NULL (xp will also be NULL in that case), and IN_RENAME will
1057 * be set if doingdirectory. This means we can use the 'bad' target
1062 * 2) If target doesn't exist, link the target
1063 * to the source and unlink the source.
1064 * Otherwise, rewrite the target directory
1065 * entry to reference the source inode and
1066 * expunge the original entry's existence.
1069 if (dp->i_dev != ip->i_dev)
1070 panic("ufs_rename: EXDEV");
1072 * Account for ".." in new directory.
1073 * When source and destination have the same
1074 * parent we don't fool with the link count.
1076 if (doingdirectory && newparent) {
1077 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1083 dp->i_flag |= IN_CHANGE;
1084 if (DOINGSOFTDEP(tdvp))
1085 softdep_change_linkcnt(dp);
1086 error = UFS_UPDATE(tdvp, !(DOINGSOFTDEP(tdvp) |
1091 ufs_makedirentry(ip, tcnp, &newdir);
1092 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1094 if (doingdirectory && newparent) {
1097 dp->i_flag |= IN_CHANGE;
1098 if (DOINGSOFTDEP(tdvp))
1099 softdep_change_linkcnt(dp);
1100 (void)UFS_UPDATE(tdvp, 1);
1104 VN_KNOTE(tdvp, NOTE_WRITE);
1107 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1108 panic("ufs_rename: EXDEV");
1110 * Short circuit rename(foo, foo).
1112 if (xp->i_number == ip->i_number)
1113 panic("ufs_rename: same file");
1115 * If the parent directory is "sticky", then the user must
1116 * own the parent directory, or the destination of the rename,
1117 * otherwise the destination may not be changed (except by
1118 * root). This implements append-only directories.
1120 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
1121 tcnp->cn_cred->cr_uid != dp->i_uid &&
1122 xp->i_uid != tcnp->cn_cred->cr_uid) {
1127 * Target must be empty if a directory and have no links
1128 * to it. Also, ensure source and target are compatible
1129 * (both directories, or both not directories).
1131 * Purge the file or directory being replaced from the
1134 if ((xp->i_mode&IFMT) == IFDIR) {
1135 if ((xp->i_effnlink > 2) ||
1136 !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1140 if (!doingdirectory) {
1144 /* cache_purge removed - handled by VFS compat layer */
1145 } else if (doingdirectory == 0) {
1146 /* cache_purge removed - handled by VFS compat layer */
1152 * note: inode passed to ufs_dirrewrite() is 0 for a
1153 * non-directory file rename, 1 for a directory rename
1154 * in the same directory, and > 1 for an inode representing
1155 * the new directory.
1157 error = ufs_dirrewrite(dp, xp, ip->i_number,
1159 (doingdirectory && newparent) ?
1160 newparent : (ino_t)doingdirectory);
1163 if (doingdirectory) {
1166 if (DOINGSOFTDEP(tdvp))
1167 softdep_change_linkcnt(dp);
1170 if (DOINGSOFTDEP(tvp))
1171 softdep_change_linkcnt(xp);
1173 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1175 * Truncate inode. The only stuff left in the directory
1176 * is "." and "..". The "." reference is inconsequential
1177 * since we are quashing it. We have removed the "."
1178 * reference and the reference in the parent directory,
1179 * but there may be other hard links. The soft
1180 * dependency code will arrange to do these operations
1181 * after the parent directory entry has been deleted on
1182 * disk, so when running with that code we avoid doing
1187 dp->i_flag |= IN_CHANGE;
1190 xp->i_flag |= IN_CHANGE;
1191 ioflag = DOINGASYNC(tvp) ? 0 : IO_SYNC;
1192 if ((error = UFS_TRUNCATE(tvp, (off_t)0, ioflag,
1193 tcnp->cn_cred, tcnp->cn_td)) != 0)
1196 VN_KNOTE(tdvp, NOTE_WRITE);
1198 VN_KNOTE(tvp, NOTE_DELETE);
1204 * tvp and tdvp have been cleaned up. only fvp and fdvp (both
1205 * unlocked) remain. We are about to overwrite fvp but we have to
1206 * keep 'ip' intact so we cannot release the old fvp, which is still
1207 * refd and accessible via ap->a_fvp.
1209 * This means we cannot use either 'bad' or 'out' to cleanup any
1214 * 3) Unlink the source.
1216 fcnp->cn_flags &= ~CNP_MODMASK;
1217 fcnp->cn_flags |= CNP_LOCKPARENT;
1218 error = relookup(fdvp, &fvp, fcnp);
1219 if (error || fvp == NULL) {
1221 * From name has disappeared. IN_RENAME will not be set if
1222 * we get past the panic so we don't have to clean it up.
1225 panic("ufs_rename: lost dir entry");
1227 if (fcnp->cn_flags & CNP_PDIRUNLOCK)
1233 KKASSERT((fcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
1236 * fdvp and fvp are locked.
1242 * Ensure that the directory entry still exists and has not
1243 * changed while the new name has been entered. If the source is
1244 * a file then the entry may have been unlinked or renamed. In
1245 * either case there is no further work to be done. If the source
1246 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1247 * flag ensures that it cannot be moved by another rename or removed
1248 * by a rmdir. Cleanup IN_RENAME.
1252 panic("ufs_rename: lost dir entry");
1255 * If the source is a directory with a
1256 * new parent, the link count of the old
1257 * parent directory must be decremented
1258 * and ".." set to point to the new parent.
1260 if (doingdirectory && newparent) {
1261 xp->i_offset = mastertemplate.dot_reclen;
1262 ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1263 /* cache_purge removed - handled by VFS compat layer */
1265 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1266 xp->i_flag &= ~IN_RENAME;
1269 VN_KNOTE(fvp, NOTE_RENAME);
1281 ip->i_flag &= ~IN_RENAME;
1282 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1285 ip->i_flag |= IN_CHANGE;
1286 ip->i_flag &= ~IN_RENAME;
1287 if (DOINGSOFTDEP(fvp))
1288 softdep_change_linkcnt(ip);
1299 * ufs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1300 * struct componentname *a_cnp, struct vattr *a_vap)
1304 ufs_mkdir(struct vop_old_mkdir_args *ap)
1306 struct vnode *dvp = ap->a_dvp;
1307 struct vattr *vap = ap->a_vap;
1308 struct componentname *cnp = ap->a_cnp;
1309 struct inode *ip, *dp;
1312 struct dirtemplate dirtemplate, *dtp;
1313 struct direct newdir;
1318 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1322 dmode = vap->va_mode & 0777;
1325 * Must simulate part of ufs_makeinode here to acquire the inode,
1326 * but not have it entered in the parent directory. The entry is
1327 * made later after writing "." and ".." entries.
1329 error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
1333 ip->i_gid = dp->i_gid;
1337 struct ucred ucred, *ucp;
1341 * If we are hacking owners here, (only do this where told to)
1342 * and we are not giving it TO root, (would subvert quotas)
1343 * then go ahead and give it to the other user.
1344 * The new directory also inherits the SUID bit.
1345 * If user's UID and dir UID are the same,
1346 * 'give it away' so that the SUID is still forced on.
1348 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1349 (dp->i_mode & ISUID) && dp->i_uid) {
1351 ip->i_uid = dp->i_uid;
1353 if (dp->i_uid != cnp->cn_cred->cr_uid) {
1355 * Make sure the correct user gets charged
1357 * Make a dummy credential for the victim.
1358 * XXX This seems to never be accessed out of
1359 * our context so a stack variable is ok.
1362 ucred.cr_uid = ip->i_uid;
1363 ucred.cr_ngroups = 1;
1364 ucred.cr_groups[0] = dp->i_gid;
1369 ip->i_uid = cnp->cn_cred->cr_uid;
1371 if ((error = ufs_getinoquota(ip)) ||
1372 (error = ufs_chkiq(ip, 1, ucp, 0))) {
1373 UFS_VFREE(tvp, ip->i_number, dmode);
1379 #else /* !SUIDDIR */
1380 ip->i_uid = cnp->cn_cred->cr_uid;
1382 if ((error = ufs_getinoquota(ip)) ||
1383 (error = ufs_chkiq(ip, 1, cnp->cn_cred, 0))) {
1384 UFS_VFREE(tvp, ip->i_number, dmode);
1389 #endif /* !SUIDDIR */
1390 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1392 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1395 if (DOINGSOFTDEP(tvp))
1396 softdep_change_linkcnt(ip);
1397 if (cnp->cn_flags & CNP_ISWHITEOUT)
1398 ip->i_flags |= UF_OPAQUE;
1401 * Bump link count in parent directory to reflect work done below.
1402 * Should be done before reference is created so cleanup is
1403 * possible if we crash.
1407 dp->i_flag |= IN_CHANGE;
1408 if (DOINGSOFTDEP(dvp))
1409 softdep_change_linkcnt(dp);
1410 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1415 * The vnode must have a VM object in order to issue buffer cache
1418 vinitvmio(tvp, DIRBLKSIZ);
1421 * Initialize directory with "." and ".." from static template.
1423 if (dvp->v_mount->mnt_maxsymlinklen > 0)
1424 dtp = &mastertemplate;
1426 dtp = (struct dirtemplate *)&omastertemplate;
1428 dirtemplate.dot_ino = ip->i_number;
1429 dirtemplate.dotdot_ino = dp->i_number;
1430 vnode_pager_setsize(tvp, DIRBLKSIZ);
1431 error = VOP_BALLOC(tvp, 0LL, DIRBLKSIZ, cnp->cn_cred, B_CLRBUF, &bp);
1434 ip->i_size = DIRBLKSIZ;
1435 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1436 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1437 if (DOINGSOFTDEP(tvp)) {
1439 * Ensure that the entire newly allocated block is a
1440 * valid directory so that future growth within the
1441 * block does not have to ensure that the block is
1442 * written before the inode.
1445 while (blkoff < bp->b_bcount) {
1447 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1448 blkoff += DIRBLKSIZ;
1451 if ((error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) |
1452 DOINGASYNC(tvp)))) != 0) {
1457 * Directory set up, now install its entry in the parent directory.
1459 * If we are not doing soft dependencies, then we must write out the
1460 * buffer containing the new directory body before entering the new
1461 * name in the parent. If we are doing soft dependencies, then the
1462 * buffer containing the new directory body will be passed to and
1463 * released in the soft dependency code after the code has attached
1464 * an appropriate ordering dependency to the buffer which ensures that
1465 * the buffer is written before the new name is written in the parent.
1467 if (DOINGASYNC(dvp))
1469 else if (!DOINGSOFTDEP(dvp) && (error = bwrite(bp)) != 0)
1471 ufs_makedirentry(ip, cnp, &newdir);
1472 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1476 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1481 dp->i_flag |= IN_CHANGE;
1482 if (DOINGSOFTDEP(dvp))
1483 softdep_change_linkcnt(dp);
1485 * No need to do an explicit VOP_TRUNCATE here, vrele will
1486 * do this for us because we set the link count to 0.
1490 ip->i_flag |= IN_CHANGE;
1491 if (DOINGSOFTDEP(tvp))
1492 softdep_change_linkcnt(ip);
1500 * Rmdir system call.
1502 * ufs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1503 * struct componentname *a_cnp)
1507 ufs_rmdir(struct vop_old_rmdir_args *ap)
1509 struct vnode *vp = ap->a_vp;
1510 struct vnode *dvp = ap->a_dvp;
1511 struct componentname *cnp = ap->a_cnp;
1512 struct inode *ip, *dp;
1519 * Do not remove a directory that is in the process of being renamed.
1520 * Verify the directory is empty (and valid). Rmdir ".." will not be
1521 * valid since ".." will contain a reference to the current directory
1522 * and thus be non-empty. Do not allow the removal of mounted on
1523 * directories (this can happen when an NFS exported filesystem
1524 * tries to remove a locally mounted on directory).
1527 if (ip->i_flag & IN_RENAME) {
1531 if (ip->i_effnlink != 2 ||
1532 !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1536 if ((dp->i_flags & APPEND)
1537 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1541 if (vp->v_mountedhere != 0) {
1546 * Delete reference to directory before purging
1547 * inode. If we crash in between, the directory
1548 * will be reattached to lost+found,
1552 if (DOINGSOFTDEP(vp)) {
1553 softdep_change_linkcnt(dp);
1554 softdep_change_linkcnt(ip);
1556 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1560 if (DOINGSOFTDEP(vp)) {
1561 softdep_change_linkcnt(dp);
1562 softdep_change_linkcnt(ip);
1566 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1568 * Truncate inode. The only stuff left in the directory is "." and
1569 * "..". The "." reference is inconsequential since we are quashing
1570 * it. The soft dependency code will arrange to do these operations
1571 * after the parent directory entry has been deleted on disk, so
1572 * when running with that code we avoid doing them now.
1574 if (!DOINGSOFTDEP(vp)) {
1576 dp->i_flag |= IN_CHANGE;
1578 ip->i_flag |= IN_CHANGE;
1579 ioflag = DOINGASYNC(vp) ? 0 : IO_SYNC;
1580 error = UFS_TRUNCATE(vp, (off_t)0, ioflag, cnp->cn_cred,
1583 /* cache_purge removed - handled by VFS compat layer */
1585 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1586 if (ip->i_dirhash != NULL)
1587 ufsdirhash_free(ip);
1590 VN_KNOTE(vp, NOTE_DELETE);
1595 * symlink -- make a symbolic link
1597 * ufs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1598 * struct componentname *a_cnp, struct vattr *a_vap,
1603 ufs_symlink(struct vop_old_symlink_args *ap)
1605 struct vnode *vp, **vpp = ap->a_vpp;
1609 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1613 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1615 len = strlen(ap->a_target);
1616 if (len < vp->v_mount->mnt_maxsymlinklen) {
1618 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1620 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1623 * Make sure we have a VM object in order to use
1626 if (vp->v_object == NULL)
1628 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1629 UIO_SYSSPACE, IO_NODELOCKED,
1630 ap->a_cnp->cn_cred, NULL, NULL);
1638 * Vnode op for reading directories.
1640 * The routine below assumes that the on-disk format of a directory
1641 * is the same as that defined by <sys/dirent.h>. If the on-disk
1642 * format changes, then it will be necessary to do a conversion
1643 * from the on-disk format that read returns to the format defined
1644 * by <sys/dirent.h>.
1646 * ufs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
1647 * int *a_eofflag, int *ncookies, u_long **a_cookies)
1651 ufs_readdir(struct vop_readdir_args *ap)
1653 struct uio *uio = ap->a_uio;
1654 struct vnode *vp = ap->a_vp;
1655 struct direct *edp, *dp;
1659 int readcnt, retval;
1662 off_t startoffset = uio->uio_offset;
1664 count = uio->uio_resid;
1666 * Avoid complications for partial directory entries by adjusting
1667 * the i/o to end at a block boundary. Don't give up (like the old ufs
1668 * does) if the initial adjustment gives a negative count, since
1669 * many callers don't supply a large enough buffer. The correct
1670 * size is a little larger than DIRBLKSIZ to allow for expansion
1671 * of directory entries, but some callers just use 512.
1673 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
1678 auio.uio_iov = &aiov;
1679 auio.uio_iovcnt = 1;
1680 auio.uio_resid = count;
1681 auio.uio_segflg = UIO_SYSSPACE;
1682 aiov.iov_len = count;
1683 MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
1684 aiov.iov_base = dirbuf;
1685 error = VOP_READ(vp, &auio, 0, ap->a_cred);
1687 readcnt = count - auio.uio_resid;
1688 edp = (struct direct *)&dirbuf[readcnt];
1690 for (dp = (struct direct *)dirbuf;
1691 !error && uio->uio_resid > 0 && dp < edp; ) {
1692 if (dp->d_reclen <= 0) {
1696 #if BYTE_ORDER == LITTLE_ENDIAN
1698 retval = vop_write_dirent(&error, uio,
1699 dp->d_ino, dp->d_namlen, dp->d_type,
1704 retval = vop_write_dirent(&error, uio,
1705 dp->d_ino, dp->d_type, dp->d_namlen,
1712 dp = (struct direct *)((char *)dp + dp->d_reclen);
1716 /* we need to correct uio_offset */
1717 uio->uio_offset = startoffset + (caddr_t)dp - dirbuf;
1719 if (!error && ap->a_ncookies != NULL) {
1720 u_long *cookiep, *cookies, *ecookies;
1723 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1724 panic("ufs_readdir: unexpected uio from NFS server");
1725 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1728 for (dp = (struct direct *)dirbuf,
1729 cookiep = cookies, ecookies = cookies + ncookies;
1731 dp = (struct direct *)((caddr_t) dp + dp->d_reclen)) {
1732 off += dp->d_reclen;
1733 *cookiep++ = (u_long) off;
1735 *ap->a_ncookies = ncookies;
1736 *ap->a_cookies = cookies;
1739 FREE(dirbuf, M_TEMP);
1741 *ap->a_eofflag = VTOI(vp)->i_size <= uio->uio_offset;
1746 * Return target name of a symbolic link
1748 * ufs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1752 ufs_readlink(struct vop_readlink_args *ap)
1754 struct vnode *vp = ap->a_vp;
1755 struct inode *ip = VTOI(vp);
1759 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1760 (ip->i_din.di_blocks == 0)) { /* XXX - for old fastlink support */
1761 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1766 * Perform the equivalent of an OPEN on vp so we can issue a
1769 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1773 * Calculate the logical to physical mapping if not done already,
1774 * then call the device strategy routine.
1776 * In order to be able to swap to a file, the VOP_BMAP operation may not
1777 * deadlock on memory. See ufs_bmap() for details.
1779 * ufs_strategy(struct vnode *a_vp, struct bio *a_bio)
1783 ufs_strategy(struct vop_strategy_args *ap)
1785 struct bio *bio = ap->a_bio;
1787 struct buf *bp = bio->bio_buf;
1788 struct vnode *vp = ap->a_vp;
1793 if (vp->v_type == VBLK || vp->v_type == VCHR)
1794 panic("ufs_strategy: spec");
1795 nbio = push_bio(bio);
1796 if (nbio->bio_offset == NOOFFSET) {
1797 error = VOP_BMAP(vp, bio->bio_offset, NULL, &nbio->bio_offset,
1800 bp->b_error = error;
1801 bp->b_flags |= B_ERROR;
1802 /* I/O was never started on nbio, must biodone(bio) */
1806 if (nbio->bio_offset == NOOFFSET)
1809 if (nbio->bio_offset == NOOFFSET) {
1811 * We hit a hole in the file. The buffer has been zero-filled
1812 * so just biodone() it.
1816 vn_strategy(ip->i_devvp, nbio);
1822 * Print out the contents of an inode.
1824 * ufs_print(struct vnode *a_vp)
1828 ufs_print(struct vop_print_args *ap)
1830 struct vnode *vp = ap->a_vp;
1831 struct inode *ip = VTOI(vp);
1833 printf("tag VT_UFS, ino %lu, on dev %s (%d, %d)",
1834 (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev),
1836 if (vp->v_type == VFIFO)
1838 lockmgr_printinfo(&vp->v_lock);
1844 * Read wrapper for special devices.
1846 * ufsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1847 * struct ucred *a_cred)
1851 ufsspec_read(struct vop_read_args *ap)
1858 resid = uio->uio_resid;
1859 error = VOCALL(spec_vnode_vops, &ap->a_head);
1861 * The inode may have been revoked during the call, so it must not
1862 * be accessed blindly here or in the other wrapper functions.
1864 ip = VTOI(ap->a_vp);
1865 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1866 ip->i_flag |= IN_ACCESS;
1871 * Write wrapper for special devices.
1873 * ufsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1874 * struct ucred *a_cred)
1878 ufsspec_write(struct vop_write_args *ap)
1885 resid = uio->uio_resid;
1886 error = VOCALL(spec_vnode_vops, &ap->a_head);
1887 ip = VTOI(ap->a_vp);
1888 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1889 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1894 * Close wrapper for special devices.
1896 * Update the times on the inode then do device close.
1898 * ufsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
1899 * struct thread *a_td)
1903 ufsspec_close(struct vop_close_args *ap)
1905 struct vnode *vp = ap->a_vp;
1907 if (vp->v_usecount > 1)
1909 return (VOCALL(spec_vnode_vops, &ap->a_head));
1913 * Read wrapper for fifos.
1915 * ufsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1916 * struct ucred *a_cred)
1920 ufsfifo_read(struct vop_read_args *ap)
1927 resid = uio->uio_resid;
1928 error = VOCALL(fifo_vnode_vops, &ap->a_head);
1929 ip = VTOI(ap->a_vp);
1930 if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL &&
1931 (uio->uio_resid != resid || (error == 0 && resid != 0)))
1932 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1937 * Write wrapper for fifos.
1939 * ufsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1940 * struct ucred *a_cred)
1944 ufsfifo_write(struct vop_write_args *ap)
1951 resid = uio->uio_resid;
1952 error = VOCALL(fifo_vnode_vops, &ap->a_head);
1953 ip = VTOI(ap->a_vp);
1954 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1955 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1960 * Close wrapper for fifos.
1962 * Update the times on the inode then do device close.
1964 * ufsfifo_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
1965 * struct thread *a_td)
1969 ufsfifo_close(struct vop_close_args *ap)
1971 struct vnode *vp = ap->a_vp;
1973 if (vp->v_usecount > 1)
1975 return (VOCALL(fifo_vnode_vops, &ap->a_head));
1979 * Kqfilter wrapper for fifos.
1981 * Fall through to ufs kqfilter routines if needed
1985 ufsfifo_kqfilter(struct vop_kqfilter_args *ap)
1989 error = VOCALL(fifo_vnode_vops, &ap->a_head);
1991 error = ufs_kqfilter(ap);
1996 * Return POSIX pathconf information applicable to ufs filesystems.
1998 * ufs_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
2002 ufs_pathconf(struct vop_pathconf_args *ap)
2004 switch (ap->a_name) {
2006 *ap->a_retval = LINK_MAX;
2009 *ap->a_retval = NAME_MAX;
2012 *ap->a_retval = PATH_MAX;
2015 *ap->a_retval = PIPE_BUF;
2017 case _PC_CHOWN_RESTRICTED:
2030 * Advisory record locking support
2032 * ufs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
2037 ufs_advlock(struct vop_advlock_args *ap)
2039 struct inode *ip = VTOI(ap->a_vp);
2041 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
2045 * Initialize the vnode associated with a new inode, handle aliased
2048 * Make sure directories have their VM object now rather then later,
2049 * saving us from having to check on all the myrid directory VOPs
2050 * that might be executed without a VOP_OPEN being performed.
2053 ufs_vinit(struct mount *mntp, struct vnode **vpp)
2062 vp->v_type = IFTOVT(ip->i_mode);
2064 switch(vp->v_type) {
2067 vp->v_ops = &mntp->mnt_vn_spec_ops;
2068 addaliasu(vp, ip->i_rdev);
2071 vp->v_ops = &mntp->mnt_vn_fifo_ops;
2075 vinitvmio(vp, ip->i_size);
2078 if (ip->i_size >= vp->v_mount->mnt_maxsymlinklen)
2079 vinitvmio(vp, ip->i_size);
2086 if (ip->i_number == ROOTINO)
2087 vp->v_flag |= VROOT;
2089 * Initialize modrev times
2091 getmicrouptime(&tv);
2092 SETHIGH(ip->i_modrev, tv.tv_sec);
2093 SETLOW(ip->i_modrev, tv.tv_usec * 4294);
2099 * Allocate a new inode.
2103 ufs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
2104 struct componentname *cnp)
2106 struct inode *ip, *pdir;
2107 struct direct newdir;
2113 if ((mode & IFMT) == 0)
2116 error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2120 ip->i_gid = pdir->i_gid;
2124 struct ucred ucred, *ucp;
2128 * If we are not the owner of the directory,
2129 * and we are hacking owners here, (only do this where told to)
2130 * and we are not giving it TO root, (would subvert quotas)
2131 * then go ahead and give it to the other user.
2132 * Note that this drops off the execute bits for security.
2134 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2135 (pdir->i_mode & ISUID) &&
2136 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2137 ip->i_uid = pdir->i_uid;
2141 * Make sure the correct user gets charged
2143 * Quickly knock up a dummy credential for the victim.
2144 * XXX This seems to never be accessed out of our
2145 * context so a stack variable is ok.
2148 ucred.cr_uid = ip->i_uid;
2149 ucred.cr_ngroups = 1;
2150 ucred.cr_groups[0] = pdir->i_gid;
2154 ip->i_uid = cnp->cn_cred->cr_uid;
2157 if ((error = ufs_getinoquota(ip)) ||
2158 (error = ufs_chkiq(ip, 1, ucp, 0))) {
2159 UFS_VFREE(tvp, ip->i_number, mode);
2165 #else /* !SUIDDIR */
2166 ip->i_uid = cnp->cn_cred->cr_uid;
2168 if ((error = ufs_getinoquota(ip)) ||
2169 (error = ufs_chkiq(ip, 1, cnp->cn_cred, 0))) {
2170 UFS_VFREE(tvp, ip->i_number, mode);
2175 #endif /* !SUIDDIR */
2176 ip->i_fsmid = cache_getnewfsmid();
2177 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2179 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2182 if (DOINGSOFTDEP(tvp))
2183 softdep_change_linkcnt(ip);
2184 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2185 suser_cred(cnp->cn_cred, 0)) {
2186 ip->i_mode &= ~ISGID;
2189 if (cnp->cn_flags & CNP_ISWHITEOUT)
2190 ip->i_flags |= UF_OPAQUE;
2193 * Regular files and directories need VM objects. Softlinks do
2194 * not (not immediately anyway).
2196 if (tvp->v_type == VREG || tvp->v_type == VDIR)
2200 * Make sure inode goes to disk before directory entry.
2202 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2205 ufs_makedirentry(ip, cnp, &newdir);
2206 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2214 * Write error occurred trying to update the inode
2215 * or the directory so must deallocate the inode.
2219 ip->i_flag |= IN_CHANGE;
2220 if (DOINGSOFTDEP(tvp))
2221 softdep_change_linkcnt(ip);
2227 ufs_missingop(struct vop_generic_args *ap)
2229 panic("no vop function for %s in ufs child", ap->a_desc->vdesc_name);
2230 return (EOPNOTSUPP);
2233 static struct filterops ufsread_filtops =
2234 { 1, NULL, filt_ufsdetach, filt_ufsread };
2235 static struct filterops ufswrite_filtops =
2236 { 1, NULL, filt_ufsdetach, filt_ufswrite };
2237 static struct filterops ufsvnode_filtops =
2238 { 1, NULL, filt_ufsdetach, filt_ufsvnode };
2241 * ufs_kqfilter(struct vnode *a_vp, struct knote *a_kn)
2244 ufs_kqfilter(struct vop_kqfilter_args *ap)
2246 struct vnode *vp = ap->a_vp;
2247 struct knote *kn = ap->a_kn;
2250 switch (kn->kn_filter) {
2252 kn->kn_fop = &ufsread_filtops;
2255 kn->kn_fop = &ufswrite_filtops;
2258 kn->kn_fop = &ufsvnode_filtops;
2264 kn->kn_hook = (caddr_t)vp;
2266 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2267 SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
2268 lwkt_reltoken(&ilock);
2274 filt_ufsdetach(struct knote *kn)
2276 struct vnode *vp = (struct vnode *)kn->kn_hook;
2279 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2280 SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
2281 kn, knote, kn_selnext);
2282 lwkt_reltoken(&ilock);
2287 filt_ufsread(struct knote *kn, long hint)
2289 struct vnode *vp = (struct vnode *)kn->kn_hook;
2290 struct inode *ip = VTOI(vp);
2293 * filesystem is gone, so set the EOF flag and schedule
2294 * the knote for deletion.
2296 if (hint == NOTE_REVOKE) {
2297 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2301 kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
2302 return (kn->kn_data != 0);
2307 filt_ufswrite(struct knote *kn, long hint)
2310 * filesystem is gone, so set the EOF flag and schedule
2311 * the knote for deletion.
2313 if (hint == NOTE_REVOKE)
2314 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2321 filt_ufsvnode(struct knote *kn, long hint)
2323 if (kn->kn_sfflags & hint)
2324 kn->kn_fflags |= hint;
2325 if (hint == NOTE_REVOKE) {
2326 kn->kn_flags |= EV_EOF;
2329 return (kn->kn_fflags != 0);
2332 /* Global vfs data structures for ufs. */
2333 static struct vop_ops *ufs_vnode_vops;
2334 static struct vnodeopv_entry_desc ufs_vnodeop_entries[] = {
2335 { &vop_default_desc, vop_defaultop },
2336 { &vop_fsync_desc, (vnodeopv_entry_t) ufs_missingop },
2337 { &vop_read_desc, (vnodeopv_entry_t) ufs_missingop },
2338 { &vop_reallocblks_desc, (vnodeopv_entry_t) ufs_missingop },
2339 { &vop_write_desc, (vnodeopv_entry_t) ufs_missingop },
2340 { &vop_access_desc, (vnodeopv_entry_t) ufs_access },
2341 { &vop_advlock_desc, (vnodeopv_entry_t) ufs_advlock },
2342 { &vop_bmap_desc, (vnodeopv_entry_t) ufs_bmap },
2343 { &vop_old_lookup_desc, (vnodeopv_entry_t) ufs_lookup },
2344 { &vop_close_desc, (vnodeopv_entry_t) ufs_close },
2345 { &vop_old_create_desc, (vnodeopv_entry_t) ufs_create },
2346 { &vop_getattr_desc, (vnodeopv_entry_t) ufs_getattr },
2347 { &vop_inactive_desc, (vnodeopv_entry_t) ufs_inactive },
2348 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
2349 { &vop_old_link_desc, (vnodeopv_entry_t) ufs_link },
2350 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
2351 { &vop_old_mkdir_desc, (vnodeopv_entry_t) ufs_mkdir },
2352 { &vop_old_mknod_desc, (vnodeopv_entry_t) ufs_mknod },
2353 { &vop_mmap_desc, (vnodeopv_entry_t) ufs_mmap },
2354 { &vop_open_desc, (vnodeopv_entry_t) ufs_open },
2355 { &vop_pathconf_desc, (vnodeopv_entry_t) ufs_pathconf },
2356 { &vop_poll_desc, (vnodeopv_entry_t) vop_stdpoll },
2357 { &vop_kqfilter_desc, (vnodeopv_entry_t) ufs_kqfilter },
2358 { &vop_print_desc, (vnodeopv_entry_t) ufs_print },
2359 { &vop_readdir_desc, (vnodeopv_entry_t) ufs_readdir },
2360 { &vop_readlink_desc, (vnodeopv_entry_t) ufs_readlink },
2361 { &vop_reclaim_desc, (vnodeopv_entry_t) ufs_reclaim },
2362 { &vop_old_remove_desc, (vnodeopv_entry_t) ufs_remove },
2363 { &vop_old_rename_desc, (vnodeopv_entry_t) ufs_rename },
2364 { &vop_old_rmdir_desc, (vnodeopv_entry_t) ufs_rmdir },
2365 { &vop_setattr_desc, (vnodeopv_entry_t) ufs_setattr },
2366 { &vop_strategy_desc, (vnodeopv_entry_t) ufs_strategy },
2367 { &vop_old_symlink_desc, (vnodeopv_entry_t) ufs_symlink },
2368 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
2369 { &vop_old_whiteout_desc, (vnodeopv_entry_t) ufs_whiteout },
2372 static struct vnodeopv_desc ufs_vnodeop_opv_desc =
2373 { &ufs_vnode_vops, ufs_vnodeop_entries, VVF_SUPPORTS_FSMID };
2375 static struct vop_ops *ufs_spec_vops;
2376 static struct vnodeopv_entry_desc ufs_specop_entries[] = {
2377 { &vop_default_desc, (vnodeopv_entry_t) spec_vnoperate },
2378 { &vop_fsync_desc, (vnodeopv_entry_t) ufs_missingop },
2379 { &vop_access_desc, (vnodeopv_entry_t) ufs_access },
2380 { &vop_close_desc, (vnodeopv_entry_t) ufsspec_close },
2381 { &vop_getattr_desc, (vnodeopv_entry_t) ufs_getattr },
2382 { &vop_inactive_desc, (vnodeopv_entry_t) ufs_inactive },
2383 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
2384 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
2385 { &vop_print_desc, (vnodeopv_entry_t) ufs_print },
2386 { &vop_read_desc, (vnodeopv_entry_t) ufsspec_read },
2387 { &vop_reclaim_desc, (vnodeopv_entry_t) ufs_reclaim },
2388 { &vop_setattr_desc, (vnodeopv_entry_t) ufs_setattr },
2389 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
2390 { &vop_write_desc, (vnodeopv_entry_t) ufsspec_write },
2393 static struct vnodeopv_desc ufs_specop_opv_desc =
2394 { &ufs_spec_vops, ufs_specop_entries, VVF_SUPPORTS_FSMID };
2396 static struct vop_ops *ufs_fifo_vops;
2397 static struct vnodeopv_entry_desc ufs_fifoop_entries[] = {
2398 { &vop_default_desc, (vnodeopv_entry_t) fifo_vnoperate },
2399 { &vop_fsync_desc, (vnodeopv_entry_t) ufs_missingop },
2400 { &vop_access_desc, (vnodeopv_entry_t) ufs_access },
2401 { &vop_close_desc, (vnodeopv_entry_t) ufsfifo_close },
2402 { &vop_getattr_desc, (vnodeopv_entry_t) ufs_getattr },
2403 { &vop_inactive_desc, (vnodeopv_entry_t) ufs_inactive },
2404 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
2405 { &vop_kqfilter_desc, (vnodeopv_entry_t) ufsfifo_kqfilter },
2406 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
2407 { &vop_print_desc, (vnodeopv_entry_t) ufs_print },
2408 { &vop_read_desc, (vnodeopv_entry_t) ufsfifo_read },
2409 { &vop_reclaim_desc, (vnodeopv_entry_t) ufs_reclaim },
2410 { &vop_setattr_desc, (vnodeopv_entry_t) ufs_setattr },
2411 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
2412 { &vop_write_desc, (vnodeopv_entry_t) ufsfifo_write },
2415 static struct vnodeopv_desc ufs_fifoop_opv_desc =
2416 { &ufs_fifo_vops, ufs_fifoop_entries, VVF_SUPPORTS_FSMID };
2418 VNODEOP_SET(ufs_vnodeop_opv_desc);
2419 VNODEOP_SET(ufs_specop_opv_desc);
2420 VNODEOP_SET(ufs_fifoop_opv_desc);
2423 * ufs_vnoperate(struct vnodeop_desc *a_desc)
2426 ufs_vnoperate(struct vop_generic_args *ap)
2428 return (VOCALL(ufs_vnode_vops, ap));
2432 * ufs_vnoperatefifo(struct vnodeop_desc *a_desc)
2435 ufs_vnoperatefifo(struct vop_generic_args *ap)
2437 return (VOCALL(ufs_fifo_vops, ap));
2441 * ufs_vnoperatespec(struct vnodeop_desc *a_desc)
2444 ufs_vnoperatespec(struct vop_generic_args *ap)
2446 return (VOCALL(ufs_spec_vops, ap));