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>
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"
77 #include "ffs_extern.h"
82 static int ufs_access (struct vop_access_args *);
83 static int ufs_advlock (struct vop_advlock_args *);
84 static int ufs_chmod (struct vnode *, int, struct ucred *);
85 static int ufs_chown (struct vnode *, uid_t, gid_t, struct ucred *);
86 static int ufs_close (struct vop_close_args *);
87 static int ufs_create (struct vop_old_create_args *);
88 static int ufs_getattr (struct vop_getattr_args *);
89 static int ufs_link (struct vop_old_link_args *);
90 static int ufs_makeinode (int mode, struct vnode *, struct vnode **, struct componentname *);
91 static int ufs_missingop (struct vop_generic_args *ap);
92 static int ufs_mkdir (struct vop_old_mkdir_args *);
93 static int ufs_mknod (struct vop_old_mknod_args *);
94 static int ufs_mmap (struct vop_mmap_args *);
95 static int ufs_open (struct vop_open_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;
223 * UFS cannot represent the entire major/minor range supported by
226 if (vap->va_rmajor != VNOVAL &&
227 makeudev(vap->va_rmajor, vap->va_rminor) == NOUDEV) {
231 /* no special directory support */
232 if (vap->va_type == VDIR)
235 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
236 ap->a_dvp, vpp, ap->a_cnp);
239 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
241 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
242 if (vap->va_rmajor != VNOVAL) {
244 * Want to be able to use this to make badblock
245 * inodes, so don't truncate the dev number.
247 ip->i_rdev = makeudev(vap->va_rmajor, vap->va_rminor);
250 * Remove inode, then reload it through VFS_VGET so it is
251 * checked to see if it is an alias of an existing entry in
254 (*vpp)->v_type = VNON;
255 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
256 vgone_vxlocked(*vpp);
258 error = VFS_VGET(ap->a_dvp->v_mount, ino, vpp);
271 * ufs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
277 ufs_open(struct vop_open_args *ap)
279 struct vnode *vp = ap->a_vp;
282 * Files marked append-only must be opened for appending.
284 if ((VTOI(vp)->i_flags & APPEND) &&
285 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE) {
289 return (vop_stdopen(ap));
295 * Update the times on the inode.
297 * ufs_close(struct vnode *a_vp, int a_fflag)
302 ufs_close(struct vop_close_args *ap)
304 struct vnode *vp = ap->a_vp;
306 if (vp->v_sysref.refcnt > 1)
308 return (vop_stdclose(ap));
312 * ufs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
316 ufs_access(struct vop_access_args *ap)
318 struct vnode *vp = ap->a_vp;
319 struct inode *ip = VTOI(vp);
320 struct ucred *cred = ap->a_cred;
321 mode_t mask, mode = ap->a_mode;
329 * Disallow write attempts on read-only filesystems;
330 * unless the file is a socket, fifo, or a block or
331 * character device resident on the filesystem.
334 switch (vp->v_type) {
338 if (vp->v_mount->mnt_flag & MNT_RDONLY)
341 if ((error = ufs_getinoquota(ip)) != 0)
350 /* If immutable bit set, nobody gets to write it. */
351 if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE))
354 /* Otherwise, user id 0 always gets access. */
355 if (cred->cr_uid == 0)
360 /* Otherwise, check the owner. */
361 if (cred->cr_uid == ip->i_uid) {
368 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
371 /* Otherwise, check the groups. */
372 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
373 if (ip->i_gid == *gp) {
380 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
383 /* Otherwise, check everyone else. */
390 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
394 * ufs_getattr(struct vnode *a_vp, struct vattr *a_vap)
399 ufs_getattr(struct vop_getattr_args *ap)
401 struct vnode *vp = ap->a_vp;
402 struct inode *ip = VTOI(vp);
403 struct vattr *vap = ap->a_vap;
406 * This may cause i_fsmid to be updated even if no change (0)
407 * is returned, but we should only write out the inode if non-zero
408 * is returned and if the mount is read-write.
410 if (cache_check_fsmid_vp(vp, &ip->i_fsmid) &&
411 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0
413 ip->i_flag |= IN_LAZYMOD;
418 * Copy from inode table
420 vap->va_fsid = dev2udev(ip->i_dev);
421 vap->va_fileid = ip->i_number;
422 vap->va_mode = ip->i_mode & ~IFMT;
423 vap->va_nlink = VFSTOUFS(vp->v_mount)->um_i_effnlink_valid ?
424 ip->i_effnlink : ip->i_nlink;
425 vap->va_uid = ip->i_uid;
426 vap->va_gid = ip->i_gid;
427 vap->va_rmajor = umajor(ip->i_rdev);
428 vap->va_rminor = uminor(ip->i_rdev);
429 vap->va_size = ip->i_din.di_size;
430 vap->va_atime.tv_sec = ip->i_atime;
431 vap->va_atime.tv_nsec = ip->i_atimensec;
432 vap->va_mtime.tv_sec = ip->i_mtime;
433 vap->va_mtime.tv_nsec = ip->i_mtimensec;
434 vap->va_ctime.tv_sec = ip->i_ctime;
435 vap->va_ctime.tv_nsec = ip->i_ctimensec;
436 vap->va_flags = ip->i_flags;
437 vap->va_gen = ip->i_gen;
438 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
439 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
440 vap->va_type = IFTOVT(ip->i_mode);
441 vap->va_filerev = ip->i_modrev;
442 vap->va_fsmid = ip->i_fsmid;
447 * Set attribute vnode op. called from several syscalls
449 * ufs_setattr(struct vnode *a_vp, struct vattr *a_vap,
450 * struct ucred *a_cred)
454 ufs_setattr(struct vop_setattr_args *ap)
456 struct vattr *vap = ap->a_vap;
457 struct vnode *vp = ap->a_vp;
458 struct inode *ip = VTOI(vp);
459 struct ucred *cred = ap->a_cred;
463 * Check for unsettable attributes.
465 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
466 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
467 (vap->va_blocksize != VNOVAL) || (vap->va_rmajor != VNOVAL) ||
468 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
471 if (vap->va_flags != VNOVAL) {
472 if (vp->v_mount->mnt_flag & MNT_RDONLY)
474 if (cred->cr_uid != ip->i_uid &&
475 (error = suser_cred(cred, PRISON_ROOT)))
478 * Note that a root chflags becomes a user chflags when
479 * we are jailed, unless the jail.chflags_allowed sysctl
482 if (cred->cr_uid == 0 &&
483 (!jailed(cred) || jail_chflags_allowed)) {
485 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) &&
488 ip->i_flags = vap->va_flags;
491 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
492 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
494 ip->i_flags &= SF_SETTABLE;
495 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
497 ip->i_flag |= IN_CHANGE;
498 if (vap->va_flags & (IMMUTABLE | APPEND))
501 if (ip->i_flags & (IMMUTABLE | APPEND))
504 * Go through the fields and update iff not VNOVAL.
506 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
507 if (vp->v_mount->mnt_flag & MNT_RDONLY)
509 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred)) != 0)
512 if (vap->va_size != VNOVAL) {
514 * Disallow write attempts on read-only filesystems;
515 * unless the file is a socket, fifo, or a block or
516 * character device resident on the filesystem.
518 switch (vp->v_type) {
523 if (vp->v_mount->mnt_flag & MNT_RDONLY)
529 if ((error = ffs_truncate(vp, vap->va_size, 0, cred)) != 0)
533 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
534 if (vp->v_mount->mnt_flag & MNT_RDONLY)
536 if (cred->cr_uid != ip->i_uid &&
537 (error = suser_cred(cred, PRISON_ROOT)) &&
538 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
539 (error = VOP_ACCESS(vp, VWRITE, cred))))
541 if (vap->va_atime.tv_sec != VNOVAL)
542 ip->i_flag |= IN_ACCESS;
543 if (vap->va_mtime.tv_sec != VNOVAL)
544 ip->i_flag |= IN_CHANGE | IN_UPDATE;
546 if (vap->va_atime.tv_sec != VNOVAL) {
547 ip->i_atime = vap->va_atime.tv_sec;
548 ip->i_atimensec = vap->va_atime.tv_nsec;
550 if (vap->va_mtime.tv_sec != VNOVAL) {
551 ip->i_mtime = vap->va_mtime.tv_sec;
552 ip->i_mtimensec = vap->va_mtime.tv_nsec;
554 error = ffs_update(vp, 0);
559 if (vap->va_mode != (mode_t)VNOVAL) {
560 if (vp->v_mount->mnt_flag & MNT_RDONLY)
562 error = ufs_chmod(vp, (int)vap->va_mode, cred);
564 VN_KNOTE(vp, NOTE_ATTRIB);
569 * Change the mode on a file.
570 * Inode must be locked before calling.
573 ufs_chmod(struct vnode *vp, int mode, struct ucred *cred)
575 struct inode *ip = VTOI(vp);
578 if (cred->cr_uid != ip->i_uid) {
579 error = suser_cred(cred, PRISON_ROOT);
584 if (vp->v_type != VDIR && (mode & S_ISTXT))
586 if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
589 ip->i_mode &= ~ALLPERMS;
590 ip->i_mode |= (mode & ALLPERMS);
591 ip->i_flag |= IN_CHANGE;
596 * Perform chown operation on inode ip;
597 * inode must be locked prior to call.
600 ufs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred)
602 struct inode *ip = VTOI(vp);
611 if (uid == (uid_t)VNOVAL)
613 if (gid == (gid_t)VNOVAL)
616 * If we don't own the file, are trying to change the owner
617 * of the file, or are not a member of the target group,
618 * the caller must be superuser or the call fails.
620 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
621 (gid != ip->i_gid && !(cred->cr_gid == gid ||
622 groupmember((gid_t)gid, cred)))) &&
623 (error = suser_cred(cred, PRISON_ROOT)))
628 if ((error = ufs_getinoquota(ip)) != 0)
631 ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
632 ip->i_dquot[USRQUOTA] = NODQUOT;
635 ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
636 ip->i_dquot[GRPQUOTA] = NODQUOT;
638 change = ip->i_blocks;
639 (void) ufs_chkdq(ip, -change, cred, CHOWN);
640 (void) ufs_chkiq(ip, -1, cred, CHOWN);
641 for (i = 0; i < MAXQUOTAS; i++) {
642 ufs_dqrele(vp, ip->i_dquot[i]);
643 ip->i_dquot[i] = NODQUOT;
649 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 if ((error = ufs_chkdq(ip, change, cred, CHOWN)) == 0) {
659 if ((error = ufs_chkiq(ip, 1, cred, CHOWN)) == 0)
662 (void)ufs_chkdq(ip, -change, cred, CHOWN|FORCE);
664 for (i = 0; i < MAXQUOTAS; i++) {
665 ufs_dqrele(vp, ip->i_dquot[i]);
666 ip->i_dquot[i] = NODQUOT;
671 if (ufs_getinoquota(ip) == 0) {
673 ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
674 ip->i_dquot[USRQUOTA] = NODQUOT;
677 ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
678 ip->i_dquot[GRPQUOTA] = NODQUOT;
680 (void) ufs_chkdq(ip, change, cred, FORCE|CHOWN);
681 (void) ufs_chkiq(ip, 1, cred, FORCE|CHOWN);
682 (void) ufs_getinoquota(ip);
686 if (ufs_getinoquota(ip))
687 panic("ufs_chown: lost quota");
689 ip->i_flag |= IN_CHANGE;
690 if (cred->cr_uid != 0 && (ouid != uid || ogid != gid))
691 ip->i_mode &= ~(ISUID | ISGID);
698 * NB Currently unsupported.
700 * ufs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
705 ufs_mmap(struct vop_mmap_args *ap)
711 * ufs_remove(struct vnode *a_dvp, struct vnode *a_vp,
712 * struct componentname *a_cnp)
716 ufs_remove(struct vop_old_remove_args *ap)
719 struct vnode *vp = ap->a_vp;
720 struct vnode *dvp = ap->a_dvp;
724 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
725 (VTOI(dvp)->i_flags & APPEND)) {
729 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
730 VN_KNOTE(vp, NOTE_DELETE);
731 VN_KNOTE(dvp, NOTE_WRITE);
739 * ufs_link(struct vnode *a_tdvp, struct vnode *a_vp,
740 * struct componentname *a_cnp)
744 ufs_link(struct vop_old_link_args *ap)
746 struct vnode *vp = ap->a_vp;
747 struct vnode *tdvp = ap->a_tdvp;
748 struct componentname *cnp = ap->a_cnp;
750 struct direct newdir;
753 if (tdvp->v_mount != vp->v_mount) {
757 if (tdvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE))) {
761 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
765 if (ip->i_flags & (IMMUTABLE | APPEND)) {
771 ip->i_flag |= IN_CHANGE;
772 if (DOINGSOFTDEP(vp))
773 softdep_change_linkcnt(ip);
774 error = ffs_update(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
776 ufs_makedirentry(ip, cnp, &newdir);
777 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
783 ip->i_flag |= IN_CHANGE;
784 if (DOINGSOFTDEP(vp))
785 softdep_change_linkcnt(ip);
791 VN_KNOTE(vp, NOTE_LINK);
792 VN_KNOTE(tdvp, NOTE_WRITE);
797 * whiteout vnode call
799 * ufs_whiteout(struct vnode *a_dvp, struct componentname *a_cnp, int a_flags)
803 ufs_whiteout(struct vop_old_whiteout_args *ap)
805 struct vnode *dvp = ap->a_dvp;
806 struct componentname *cnp = ap->a_cnp;
807 struct direct newdir;
810 switch (ap->a_flags) {
812 /* 4.4 format directories support whiteout operations */
813 if (dvp->v_mount->mnt_maxsymlinklen > 0)
818 /* create a new directory whiteout */
820 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
821 panic("ufs_whiteout: old format filesystem");
825 newdir.d_namlen = cnp->cn_namelen;
826 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
827 newdir.d_type = DT_WHT;
828 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
832 /* remove an existing directory whiteout */
834 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
835 panic("ufs_whiteout: old format filesystem");
838 cnp->cn_flags &= ~CNP_DOWHITEOUT;
839 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
842 panic("ufs_whiteout: unknown op");
848 * Rename system call.
849 * rename("foo", "bar");
852 * link("foo", "bar");
854 * but ``atomically''. Can't do full commit without saving state in the
855 * inode on disk which isn't feasible at this time. Best we can do is
856 * always guarantee the target exists.
858 * Basic algorithm is:
860 * 1) Bump link count on source while we're linking it to the
861 * target. This also ensure the inode won't be deleted out
862 * from underneath us while we work (it may be truncated by
863 * a concurrent `trunc' or `open' for creation).
864 * 2) Link source to destination. If destination already exists,
866 * 3) Unlink source reference to inode if still around. If a
867 * directory was moved and the parent of the destination
868 * is different from the source, patch the ".." entry in the
871 * ufs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
872 * struct componentname *a_fcnp, struct vnode *a_tdvp,
873 * struct vnode *a_tvp, struct componentname *a_tcnp)
877 ufs_rename(struct vop_old_rename_args *ap)
879 struct vnode *tvp = ap->a_tvp;
880 struct vnode *tdvp = ap->a_tdvp;
881 struct vnode *fvp = ap->a_fvp;
882 struct vnode *fdvp = ap->a_fdvp;
883 struct componentname *tcnp = ap->a_tcnp;
884 struct componentname *fcnp = ap->a_fcnp;
885 struct inode *ip, *xp, *dp;
886 struct direct newdir;
887 ino_t oldparent = 0, newparent = 0;
888 int doingdirectory = 0;
889 int error = 0, ioflag;
892 * Check for cross-device rename.
894 if ((fvp->v_mount != tdvp->v_mount) ||
895 (tvp && (fvp->v_mount != tvp->v_mount))) {
909 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
910 (VTOI(tdvp)->i_flags & APPEND))) {
916 * Renaming a file to itself has no effect. The upper layers should
917 * not call us in that case. Temporarily just warn if they do.
920 kprintf("ufs_rename: fvp == tvp (can't happen)\n");
925 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
929 * Note: now that fvp is locked we have to be sure to unlock it before
930 * using the 'abortit' target.
934 if (ip->i_nlink >= LINK_MAX) {
939 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
940 || (dp->i_flags & APPEND)) {
945 if ((ip->i_mode & IFMT) == IFDIR) {
947 * Avoid ".", "..", and aliases of "." for obvious reasons.
949 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
950 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & CNP_ISDOTDOT ||
951 (ip->i_flag & IN_RENAME)) {
956 ip->i_flag |= IN_RENAME;
957 oldparent = dp->i_number;
960 VN_KNOTE(fdvp, NOTE_WRITE); /* XXX right place? */
963 * fvp still locked. ip->i_flag has IN_RENAME set if doingdirectory.
964 * Cleanup fvp requirements so we can unlock it.
966 * tvp and tdvp are locked. tvp may be NULL. Now that dp and xp
967 * is setup we can use the 'bad' target if we unlock fvp. We cannot
968 * use the abortit target anymore because of IN_RENAME.
977 * 1) Bump link count while we're moving stuff
978 * around. If we crash somewhere before
979 * completing our work, the link count
980 * may be wrong, but correctable.
984 ip->i_flag |= IN_CHANGE;
985 if (DOINGSOFTDEP(fvp))
986 softdep_change_linkcnt(ip);
987 if ((error = ffs_update(fvp, !(DOINGSOFTDEP(fvp) |
988 DOINGASYNC(fvp)))) != 0) {
994 * If ".." must be changed (ie the directory gets a new
995 * parent) then the source directory must not be in the
996 * directory heirarchy above the target, as this would
997 * orphan everything below the source directory. Also
998 * the user must have write permission in the source so
999 * as to be able to change "..". We must repeat the call
1000 * to namei, as the parent directory is unlocked by the
1001 * call to checkpath().
1003 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred);
1007 * We are now back to where we were in that fvp, fdvp are unlocked
1008 * and tvp, tdvp are locked. tvp may be NULL. IN_RENAME may be
1009 * set. Only the bad target or, if we clean up tvp and tdvp, the
1010 * out target, may be used.
1012 if (oldparent != dp->i_number)
1013 newparent = dp->i_number;
1014 if (doingdirectory && newparent) {
1015 if (error) /* write access check above */
1019 * Once we start messing with tvp and tdvp we cannot use the
1020 * 'bad' target, only finish cleaning tdvp and tvp up and
1021 * use the 'out' target.
1023 * This cleans up tvp.
1031 * This is a real mess. ufs_checkpath vput's the target
1032 * directory so retain an extra ref and note that tdvp will
1033 * lose its lock on return. This leaves us with one good
1034 * ref after ufs_checkpath returns.
1037 error = ufs_checkpath(ip, dp, tcnp->cn_cred);
1038 tcnp->cn_flags |= CNP_PDIRUNLOCK;
1045 * relookup no longer messes with tdvp's refs. tdvp must be
1046 * unlocked on entry and will be locked on a successful
1049 error = relookup(tdvp, &tvp, tcnp);
1051 if (tcnp->cn_flags & CNP_PDIRUNLOCK)
1057 KKASSERT((tcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
1064 * We are back to fvp, fdvp unlocked, tvp, tdvp locked. tvp may
1065 * be NULL (xp will also be NULL in that case), and IN_RENAME will
1066 * be set if doingdirectory. This means we can use the 'bad' target
1071 * 2) If target doesn't exist, link the target
1072 * to the source and unlink the source.
1073 * Otherwise, rewrite the target directory
1074 * entry to reference the source inode and
1075 * expunge the original entry's existence.
1078 if (dp->i_dev != ip->i_dev)
1079 panic("ufs_rename: EXDEV");
1081 * Account for ".." in new directory.
1082 * When source and destination have the same
1083 * parent we don't fool with the link count.
1085 if (doingdirectory && newparent) {
1086 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1092 dp->i_flag |= IN_CHANGE;
1093 if (DOINGSOFTDEP(tdvp))
1094 softdep_change_linkcnt(dp);
1095 error = ffs_update(tdvp, !(DOINGSOFTDEP(tdvp) |
1100 ufs_makedirentry(ip, tcnp, &newdir);
1101 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1103 if (doingdirectory && newparent) {
1106 dp->i_flag |= IN_CHANGE;
1107 if (DOINGSOFTDEP(tdvp))
1108 softdep_change_linkcnt(dp);
1109 (void)ffs_update(tdvp, 1);
1113 VN_KNOTE(tdvp, NOTE_WRITE);
1116 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1117 panic("ufs_rename: EXDEV");
1119 * Short circuit rename(foo, foo).
1121 if (xp->i_number == ip->i_number)
1122 panic("ufs_rename: same file");
1124 * If the parent directory is "sticky", then the user must
1125 * own the parent directory, or the destination of the rename,
1126 * otherwise the destination may not be changed (except by
1127 * root). This implements append-only directories.
1129 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
1130 tcnp->cn_cred->cr_uid != dp->i_uid &&
1131 xp->i_uid != tcnp->cn_cred->cr_uid) {
1136 * Target must be empty if a directory and have no links
1137 * to it. Also, ensure source and target are compatible
1138 * (both directories, or both not directories).
1140 * Purge the file or directory being replaced from the
1143 if ((xp->i_mode&IFMT) == IFDIR) {
1144 if ((xp->i_effnlink > 2) ||
1145 !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1149 if (!doingdirectory) {
1153 /* cache_purge removed - handled by VFS compat layer */
1154 } else if (doingdirectory == 0) {
1155 /* cache_purge removed - handled by VFS compat layer */
1161 * note: inode passed to ufs_dirrewrite() is 0 for a
1162 * non-directory file rename, 1 for a directory rename
1163 * in the same directory, and > 1 for an inode representing
1164 * the new directory.
1166 error = ufs_dirrewrite(dp, xp, ip->i_number,
1168 (doingdirectory && newparent) ?
1169 newparent : (ino_t)doingdirectory);
1172 if (doingdirectory) {
1175 if (DOINGSOFTDEP(tdvp))
1176 softdep_change_linkcnt(dp);
1179 if (DOINGSOFTDEP(tvp))
1180 softdep_change_linkcnt(xp);
1182 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1184 * Truncate inode. The only stuff left in the directory
1185 * is "." and "..". The "." reference is inconsequential
1186 * since we are quashing it. We have removed the "."
1187 * reference and the reference in the parent directory,
1188 * but there may be other hard links. The soft
1189 * dependency code will arrange to do these operations
1190 * after the parent directory entry has been deleted on
1191 * disk, so when running with that code we avoid doing
1196 dp->i_flag |= IN_CHANGE;
1199 xp->i_flag |= IN_CHANGE;
1200 ioflag = DOINGASYNC(tvp) ? 0 : IO_SYNC;
1201 error = ffs_truncate(tvp, (off_t)0, ioflag,
1206 VN_KNOTE(tdvp, NOTE_WRITE);
1208 VN_KNOTE(tvp, NOTE_DELETE);
1214 * tvp and tdvp have been cleaned up. only fvp and fdvp (both
1215 * unlocked) remain. We are about to overwrite fvp but we have to
1216 * keep 'ip' intact so we cannot release the old fvp, which is still
1217 * refd and accessible via ap->a_fvp.
1219 * This means we cannot use either 'bad' or 'out' to cleanup any
1224 * 3) Unlink the source.
1226 fcnp->cn_flags &= ~CNP_MODMASK;
1227 fcnp->cn_flags |= CNP_LOCKPARENT;
1228 error = relookup(fdvp, &fvp, fcnp);
1229 if (error || fvp == NULL) {
1231 * From name has disappeared. IN_RENAME will not be set if
1232 * we get past the panic so we don't have to clean it up.
1235 panic("ufs_rename: lost dir entry");
1237 if (fcnp->cn_flags & CNP_PDIRUNLOCK)
1243 KKASSERT((fcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
1246 * fdvp and fvp are locked.
1252 * Ensure that the directory entry still exists and has not
1253 * changed while the new name has been entered. If the source is
1254 * a file then the entry may have been unlinked or renamed. In
1255 * either case there is no further work to be done. If the source
1256 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1257 * flag ensures that it cannot be moved by another rename or removed
1258 * by a rmdir. Cleanup IN_RENAME.
1262 panic("ufs_rename: lost dir entry");
1265 * If the source is a directory with a
1266 * new parent, the link count of the old
1267 * parent directory must be decremented
1268 * and ".." set to point to the new parent.
1270 if (doingdirectory && newparent) {
1271 xp->i_offset = mastertemplate.dot_reclen;
1272 ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1273 /* cache_purge removed - handled by VFS compat layer */
1275 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1276 xp->i_flag &= ~IN_RENAME;
1279 VN_KNOTE(fvp, NOTE_RENAME);
1291 ip->i_flag &= ~IN_RENAME;
1292 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1295 ip->i_flag |= IN_CHANGE;
1296 ip->i_flag &= ~IN_RENAME;
1297 if (DOINGSOFTDEP(fvp))
1298 softdep_change_linkcnt(ip);
1309 * ufs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1310 * struct componentname *a_cnp, struct vattr *a_vap)
1314 ufs_mkdir(struct vop_old_mkdir_args *ap)
1316 struct vnode *dvp = ap->a_dvp;
1317 struct vattr *vap = ap->a_vap;
1318 struct componentname *cnp = ap->a_cnp;
1319 struct inode *ip, *dp;
1322 struct dirtemplate dirtemplate, *dtp;
1323 struct direct newdir;
1328 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1332 dmode = vap->va_mode & 0777;
1335 * Must simulate part of ufs_makeinode here to acquire the inode,
1336 * but not have it entered in the parent directory. The entry is
1337 * made later after writing "." and ".." entries.
1339 error = ffs_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1343 ip->i_gid = dp->i_gid;
1347 struct ucred ucred, *ucp;
1351 * If we are hacking owners here, (only do this where told to)
1352 * and we are not giving it TO root, (would subvert quotas)
1353 * then go ahead and give it to the other user.
1354 * The new directory also inherits the SUID bit.
1355 * If user's UID and dir UID are the same,
1356 * 'give it away' so that the SUID is still forced on.
1358 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1359 (dp->i_mode & ISUID) && dp->i_uid) {
1361 ip->i_uid = dp->i_uid;
1363 if (dp->i_uid != cnp->cn_cred->cr_uid) {
1365 * Make sure the correct user gets charged
1367 * Make a dummy credential for the victim.
1368 * XXX This seems to never be accessed out of
1369 * our context so a stack variable is ok.
1372 ucred.cr_uid = ip->i_uid;
1373 ucred.cr_ngroups = 1;
1374 ucred.cr_groups[0] = dp->i_gid;
1379 ip->i_uid = cnp->cn_cred->cr_uid;
1381 if ((error = ufs_getinoquota(ip)) ||
1382 (error = ufs_chkiq(ip, 1, ucp, 0))) {
1383 ffs_vfree(tvp, ip->i_number, dmode);
1389 #else /* !SUIDDIR */
1390 ip->i_uid = cnp->cn_cred->cr_uid;
1392 if ((error = ufs_getinoquota(ip)) ||
1393 (error = ufs_chkiq(ip, 1, cnp->cn_cred, 0))) {
1394 ffs_vfree(tvp, ip->i_number, dmode);
1399 #endif /* !SUIDDIR */
1400 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1402 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1405 if (DOINGSOFTDEP(tvp))
1406 softdep_change_linkcnt(ip);
1407 if (cnp->cn_flags & CNP_ISWHITEOUT)
1408 ip->i_flags |= UF_OPAQUE;
1411 * Bump link count in parent directory to reflect work done below.
1412 * Should be done before reference is created so cleanup is
1413 * possible if we crash.
1417 dp->i_flag |= IN_CHANGE;
1418 if (DOINGSOFTDEP(dvp))
1419 softdep_change_linkcnt(dp);
1420 error = ffs_update(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1425 * The vnode must have a VM object in order to issue buffer cache
1428 vinitvmio(tvp, DIRBLKSIZ);
1431 * Initialize directory with "." and ".." from static template.
1433 if (dvp->v_mount->mnt_maxsymlinklen > 0)
1434 dtp = &mastertemplate;
1436 dtp = (struct dirtemplate *)&omastertemplate;
1438 dirtemplate.dot_ino = ip->i_number;
1439 dirtemplate.dotdot_ino = dp->i_number;
1440 vnode_pager_setsize(tvp, DIRBLKSIZ);
1441 error = VOP_BALLOC(tvp, 0LL, DIRBLKSIZ, cnp->cn_cred, B_CLRBUF, &bp);
1444 ip->i_size = DIRBLKSIZ;
1445 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1446 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1447 if (DOINGSOFTDEP(tvp)) {
1449 * Ensure that the entire newly allocated block is a
1450 * valid directory so that future growth within the
1451 * block does not have to ensure that the block is
1452 * written before the inode.
1455 while (blkoff < bp->b_bcount) {
1457 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1458 blkoff += DIRBLKSIZ;
1461 if ((error = ffs_update(tvp, !(DOINGSOFTDEP(tvp) |
1462 DOINGASYNC(tvp)))) != 0) {
1467 * Directory set up, now install its entry in the parent directory.
1469 * If we are not doing soft dependencies, then we must write out the
1470 * buffer containing the new directory body before entering the new
1471 * name in the parent. If we are doing soft dependencies, then the
1472 * buffer containing the new directory body will be passed to and
1473 * released in the soft dependency code after the code has attached
1474 * an appropriate ordering dependency to the buffer which ensures that
1475 * the buffer is written before the new name is written in the parent.
1477 if (DOINGASYNC(dvp))
1479 else if (!DOINGSOFTDEP(dvp) && (error = bwrite(bp)) != 0)
1481 ufs_makedirentry(ip, cnp, &newdir);
1482 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1486 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1491 dp->i_flag |= IN_CHANGE;
1492 if (DOINGSOFTDEP(dvp))
1493 softdep_change_linkcnt(dp);
1495 * No need to do an explicit VOP_TRUNCATE here, vrele will
1496 * do this for us because we set the link count to 0.
1500 ip->i_flag |= IN_CHANGE;
1501 if (DOINGSOFTDEP(tvp))
1502 softdep_change_linkcnt(ip);
1510 * Rmdir system call.
1512 * ufs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1513 * struct componentname *a_cnp)
1517 ufs_rmdir(struct vop_old_rmdir_args *ap)
1519 struct vnode *vp = ap->a_vp;
1520 struct vnode *dvp = ap->a_dvp;
1521 struct componentname *cnp = ap->a_cnp;
1522 struct inode *ip, *dp;
1529 * Do not remove a directory that is in the process of being renamed.
1530 * Verify the directory is empty (and valid). Rmdir ".." will not be
1531 * valid since ".." will contain a reference to the current directory
1532 * and thus be non-empty. Do not allow the removal of mounted on
1533 * directories (this can happen when an NFS exported filesystem
1534 * tries to remove a locally mounted on directory).
1537 if (ip->i_flag & IN_RENAME) {
1541 if (ip->i_effnlink != 2 ||
1542 !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1546 if ((dp->i_flags & APPEND)
1547 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1552 * Delete reference to directory before purging
1553 * inode. If we crash in between, the directory
1554 * will be reattached to lost+found,
1558 if (DOINGSOFTDEP(vp)) {
1559 softdep_change_linkcnt(dp);
1560 softdep_change_linkcnt(ip);
1562 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1566 if (DOINGSOFTDEP(vp)) {
1567 softdep_change_linkcnt(dp);
1568 softdep_change_linkcnt(ip);
1572 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1574 * Truncate inode. The only stuff left in the directory is "." and
1575 * "..". The "." reference is inconsequential since we are quashing
1576 * it. The soft dependency code will arrange to do these operations
1577 * after the parent directory entry has been deleted on disk, so
1578 * when running with that code we avoid doing them now.
1580 if (!DOINGSOFTDEP(vp)) {
1582 dp->i_flag |= IN_CHANGE;
1584 ip->i_flag |= IN_CHANGE;
1585 ioflag = DOINGASYNC(vp) ? 0 : IO_SYNC;
1586 error = ffs_truncate(vp, (off_t)0, ioflag, cnp->cn_cred);
1588 /* cache_purge removed - handled by VFS compat layer */
1590 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1591 if (ip->i_dirhash != NULL)
1592 ufsdirhash_free(ip);
1595 VN_KNOTE(vp, NOTE_DELETE);
1600 * symlink -- make a symbolic link
1602 * ufs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1603 * struct componentname *a_cnp, struct vattr *a_vap,
1608 ufs_symlink(struct vop_old_symlink_args *ap)
1610 struct vnode *vp, **vpp = ap->a_vpp;
1614 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1618 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1620 len = strlen(ap->a_target);
1621 if (len < vp->v_mount->mnt_maxsymlinklen) {
1623 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1625 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1628 * Make sure we have a VM object in order to use
1631 if (vp->v_object == NULL)
1633 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1634 UIO_SYSSPACE, IO_NODELOCKED,
1635 ap->a_cnp->cn_cred, NULL);
1643 * Vnode op for reading directories.
1645 * ufs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
1646 * int *a_eofflag, int *ncookies, off_t **a_cookies)
1650 ufs_readdir(struct vop_readdir_args *ap)
1652 struct uio *uio = ap->a_uio;
1653 struct vnode *vp = ap->a_vp;
1658 int offset; /* offset into buffer cache buffer */
1659 int eoffset; /* end of buffer clipped to file EOF */
1660 int pickup; /* pickup point */
1665 if (uio->uio_offset < 0)
1668 * Guess the number of cookies needed. Make sure we compute at
1669 * least 1, and no more then a reasonable limit.
1671 if (ap->a_ncookies) {
1672 ncookies = uio->uio_resid / 16 + 1;
1673 if (ncookies > 1024)
1675 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1677 ncookies = -1; /* force conditionals below */
1682 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
1688 if (uio->uio_offset >= VTOI(vp)->i_size) {
1691 if (ap->a_ncookies) {
1692 *ap->a_ncookies = cookie_index;
1693 *ap->a_cookies = cookies;
1699 * Loop until we run out of cookies, we run out of user buffer,
1700 * or we hit the directory EOF.
1702 * Always start scans at the beginning of the buffer, don't trust
1703 * the offset supplied by userland.
1705 while ((error = ffs_blkatoff_ra(vp, uio->uio_offset, NULL, &bp, 2)) == 0) {
1706 pickup = (int)(uio->uio_offset - bp->b_loffset);
1709 if (bp->b_loffset + bp->b_bcount > VTOI(vp)->i_size)
1710 eoffset = (int)(VTOI(vp)->i_size - bp->b_loffset);
1712 eoffset = bp->b_bcount;
1714 while (offset < eoffset) {
1715 dp = (struct direct *)(bp->b_data + offset);
1716 if (dp->d_reclen <= 0 || (dp->d_reclen & 3) ||
1717 offset + dp->d_reclen > bp->b_bcount) {
1721 if (offsetof(struct direct, d_name[dp->d_namlen]) > dp->d_reclen) {
1725 if (offset < pickup) {
1726 offset += dp->d_reclen;
1729 #if BYTE_ORDER == LITTLE_ENDIAN
1731 retval = vop_write_dirent(&error, uio,
1732 dp->d_ino, dp->d_namlen, dp->d_type,
1737 retval = vop_write_dirent(&error, uio,
1738 dp->d_ino, dp->d_type, dp->d_namlen,
1744 cookies[cookie_index] = bp->b_loffset + offset;
1746 offset += dp->d_reclen;
1747 if (cookie_index == ncookies)
1752 * This will align the next loop to the beginning of the
1753 * next block, and pickup will calculate to 0.
1755 uio->uio_offset = bp->b_loffset + offset;
1758 if (retval || error || cookie_index == ncookies ||
1759 uio->uio_offset >= VTOI(vp)->i_size) {
1764 *ap->a_eofflag = VTOI(vp)->i_size <= uio->uio_offset;
1767 * Report errors only if we didn't manage to read anything
1769 if (error && cookie_index == 0) {
1771 kfree(cookies, M_TEMP);
1772 *ap->a_ncookies = 0;
1773 *ap->a_cookies = NULL;
1778 *ap->a_ncookies = cookie_index;
1779 *ap->a_cookies = cookies;
1788 * Return target name of a symbolic link
1790 * ufs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1794 ufs_readlink(struct vop_readlink_args *ap)
1796 struct vnode *vp = ap->a_vp;
1797 struct inode *ip = VTOI(vp);
1801 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1802 (ip->i_din.di_blocks == 0)) { /* XXX - for old fastlink support */
1803 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1808 * Perform the equivalent of an OPEN on vp so we can issue a
1811 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1815 * Calculate the logical to physical mapping if not done already,
1816 * then call the device strategy routine.
1818 * In order to be able to swap to a file, the VOP_BMAP operation may not
1819 * deadlock on memory. See ufs_bmap() for details.
1821 * ufs_strategy(struct vnode *a_vp, struct bio *a_bio)
1825 ufs_strategy(struct vop_strategy_args *ap)
1827 struct bio *bio = ap->a_bio;
1829 struct buf *bp = bio->bio_buf;
1830 struct vnode *vp = ap->a_vp;
1835 if (vp->v_type == VBLK || vp->v_type == VCHR)
1836 panic("ufs_strategy: spec");
1837 nbio = push_bio(bio);
1838 if (nbio->bio_offset == NOOFFSET) {
1839 error = VOP_BMAP(vp, bio->bio_offset, &nbio->bio_offset,
1840 NULL, NULL, bp->b_cmd);
1842 bp->b_error = error;
1843 bp->b_flags |= B_ERROR;
1844 /* I/O was never started on nbio, must biodone(bio) */
1848 if (nbio->bio_offset == NOOFFSET)
1851 if (nbio->bio_offset == NOOFFSET) {
1853 * We hit a hole in the file. The buffer has been zero-filled
1854 * so just biodone() it.
1858 vn_strategy(ip->i_devvp, nbio);
1864 * Print out the contents of an inode.
1866 * ufs_print(struct vnode *a_vp)
1870 ufs_print(struct vop_print_args *ap)
1872 struct vnode *vp = ap->a_vp;
1873 struct inode *ip = VTOI(vp);
1875 kprintf("tag VT_UFS, ino %lu, on dev %s (%d, %d)",
1876 (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev),
1878 if (vp->v_type == VFIFO)
1880 lockmgr_printinfo(&vp->v_lock);
1886 * Read wrapper for special devices.
1888 * ufsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1889 * struct ucred *a_cred)
1893 ufsspec_read(struct vop_read_args *ap)
1900 resid = uio->uio_resid;
1901 error = VOCALL(&spec_vnode_vops, &ap->a_head);
1903 * The inode may have been revoked during the call, so it must not
1904 * be accessed blindly here or in the other wrapper functions.
1906 ip = VTOI(ap->a_vp);
1907 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1908 ip->i_flag |= IN_ACCESS;
1913 * Write wrapper for special devices.
1915 * ufsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1916 * struct ucred *a_cred)
1920 ufsspec_write(struct vop_write_args *ap)
1927 resid = uio->uio_resid;
1928 error = VOCALL(&spec_vnode_vops, &ap->a_head);
1929 ip = VTOI(ap->a_vp);
1930 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1931 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1936 * Close wrapper for special devices.
1938 * Update the times on the inode then do device close.
1940 * ufsspec_close(struct vnode *a_vp, int a_fflag)
1944 ufsspec_close(struct vop_close_args *ap)
1946 struct vnode *vp = ap->a_vp;
1948 if (vp->v_sysref.refcnt > 1)
1950 return (VOCALL(&spec_vnode_vops, &ap->a_head));
1954 * Read wrapper for fifos.
1956 * ufsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1957 * struct ucred *a_cred)
1961 ufsfifo_read(struct vop_read_args *ap)
1968 resid = uio->uio_resid;
1969 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1970 ip = VTOI(ap->a_vp);
1971 if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL &&
1972 (uio->uio_resid != resid || (error == 0 && resid != 0)))
1973 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1978 * Write wrapper for fifos.
1980 * ufsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1981 * struct ucred *a_cred)
1985 ufsfifo_write(struct vop_write_args *ap)
1992 resid = uio->uio_resid;
1993 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1994 ip = VTOI(ap->a_vp);
1995 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1996 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
2001 * Close wrapper for fifos.
2003 * Update the times on the inode then do device close.
2005 * ufsfifo_close(struct vnode *a_vp, int a_fflag)
2009 ufsfifo_close(struct vop_close_args *ap)
2011 struct vnode *vp = ap->a_vp;
2013 if (vp->v_sysref.refcnt > 1)
2015 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
2019 * Kqfilter wrapper for fifos.
2021 * Fall through to ufs kqfilter routines if needed
2025 ufsfifo_kqfilter(struct vop_kqfilter_args *ap)
2029 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2031 error = ufs_kqfilter(ap);
2036 * Advisory record locking support
2038 * ufs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
2043 ufs_advlock(struct vop_advlock_args *ap)
2045 struct inode *ip = VTOI(ap->a_vp);
2047 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
2051 * Initialize the vnode associated with a new inode, handle aliased
2054 * Make sure directories have their VM object now rather then later,
2055 * saving us from having to check on all the myrid directory VOPs
2056 * that might be executed without a VOP_OPEN being performed.
2059 ufs_vinit(struct mount *mntp, struct vnode **vpp)
2068 vp->v_type = IFTOVT(ip->i_mode);
2070 switch(vp->v_type) {
2073 vp->v_ops = &mntp->mnt_vn_spec_ops;
2074 addaliasu(vp, umajor(ip->i_rdev), uminor(ip->i_rdev));
2077 vp->v_ops = &mntp->mnt_vn_fifo_ops;
2081 vinitvmio(vp, ip->i_size);
2084 if (ip->i_size >= vp->v_mount->mnt_maxsymlinklen)
2085 vinitvmio(vp, ip->i_size);
2092 if (ip->i_number == ROOTINO)
2093 vp->v_flag |= VROOT;
2095 * Initialize modrev times
2097 getmicrouptime(&tv);
2098 SETHIGH(ip->i_modrev, tv.tv_sec);
2099 SETLOW(ip->i_modrev, tv.tv_usec * 4294);
2105 * Allocate a new inode.
2109 ufs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
2110 struct componentname *cnp)
2112 struct inode *ip, *pdir;
2113 struct direct newdir;
2119 if ((mode & IFMT) == 0)
2122 error = ffs_valloc(dvp, mode, cnp->cn_cred, &tvp);
2126 ip->i_flags = pdir->i_flags & (SF_NOHISTORY|UF_NOHISTORY|UF_NODUMP);
2127 ip->i_gid = pdir->i_gid;
2131 struct ucred ucred, *ucp;
2135 * If we are not the owner of the directory,
2136 * and we are hacking owners here, (only do this where told to)
2137 * and we are not giving it TO root, (would subvert quotas)
2138 * then go ahead and give it to the other user.
2139 * Note that this drops off the execute bits for security.
2141 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2142 (pdir->i_mode & ISUID) &&
2143 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2144 ip->i_uid = pdir->i_uid;
2148 * Make sure the correct user gets charged
2150 * Quickly knock up a dummy credential for the victim.
2151 * XXX This seems to never be accessed out of our
2152 * context so a stack variable is ok.
2155 ucred.cr_uid = ip->i_uid;
2156 ucred.cr_ngroups = 1;
2157 ucred.cr_groups[0] = pdir->i_gid;
2161 ip->i_uid = cnp->cn_cred->cr_uid;
2164 if ((error = ufs_getinoquota(ip)) ||
2165 (error = ufs_chkiq(ip, 1, ucp, 0))) {
2166 ffs_vfree(tvp, ip->i_number, mode);
2172 #else /* !SUIDDIR */
2173 ip->i_uid = cnp->cn_cred->cr_uid;
2175 if ((error = ufs_getinoquota(ip)) ||
2176 (error = ufs_chkiq(ip, 1, cnp->cn_cred, 0))) {
2177 ffs_vfree(tvp, ip->i_number, mode);
2182 #endif /* !SUIDDIR */
2183 ip->i_fsmid = cache_getnewfsmid();
2184 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2186 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2189 if (DOINGSOFTDEP(tvp))
2190 softdep_change_linkcnt(ip);
2191 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2192 suser_cred(cnp->cn_cred, 0)) {
2193 ip->i_mode &= ~ISGID;
2196 if (cnp->cn_flags & CNP_ISWHITEOUT)
2197 ip->i_flags |= UF_OPAQUE;
2200 * Regular files and directories need VM objects. Softlinks do
2201 * not (not immediately anyway).
2203 if (tvp->v_type == VREG || tvp->v_type == VDIR)
2207 * Make sure inode goes to disk before directory entry.
2209 error = ffs_update(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2212 ufs_makedirentry(ip, cnp, &newdir);
2213 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2221 * Write error occurred trying to update the inode
2222 * or the directory so must deallocate the inode.
2226 ip->i_flag |= IN_CHANGE;
2227 if (DOINGSOFTDEP(tvp))
2228 softdep_change_linkcnt(ip);
2234 ufs_missingop(struct vop_generic_args *ap)
2236 panic("no vop function for %s in ufs child", ap->a_desc->sd_name);
2237 return (EOPNOTSUPP);
2240 static struct filterops ufsread_filtops =
2241 { 1, NULL, filt_ufsdetach, filt_ufsread };
2242 static struct filterops ufswrite_filtops =
2243 { 1, NULL, filt_ufsdetach, filt_ufswrite };
2244 static struct filterops ufsvnode_filtops =
2245 { 1, NULL, filt_ufsdetach, filt_ufsvnode };
2248 * ufs_kqfilter(struct vnode *a_vp, struct knote *a_kn)
2251 ufs_kqfilter(struct vop_kqfilter_args *ap)
2253 struct vnode *vp = ap->a_vp;
2254 struct knote *kn = ap->a_kn;
2257 switch (kn->kn_filter) {
2259 kn->kn_fop = &ufsread_filtops;
2262 kn->kn_fop = &ufswrite_filtops;
2265 kn->kn_fop = &ufsvnode_filtops;
2271 kn->kn_hook = (caddr_t)vp;
2273 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2274 SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
2275 lwkt_reltoken(&ilock);
2281 filt_ufsdetach(struct knote *kn)
2283 struct vnode *vp = (struct vnode *)kn->kn_hook;
2286 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2287 SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
2288 kn, knote, kn_selnext);
2289 lwkt_reltoken(&ilock);
2294 filt_ufsread(struct knote *kn, long hint)
2296 struct vnode *vp = (struct vnode *)kn->kn_hook;
2297 struct inode *ip = VTOI(vp);
2300 * filesystem is gone, so set the EOF flag and schedule
2301 * the knote for deletion.
2303 if (hint == NOTE_REVOKE) {
2304 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2308 kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
2309 return (kn->kn_data != 0);
2314 filt_ufswrite(struct knote *kn, long hint)
2317 * filesystem is gone, so set the EOF flag and schedule
2318 * the knote for deletion.
2320 if (hint == NOTE_REVOKE)
2321 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2328 filt_ufsvnode(struct knote *kn, long hint)
2330 if (kn->kn_sfflags & hint)
2331 kn->kn_fflags |= hint;
2332 if (hint == NOTE_REVOKE) {
2333 kn->kn_flags |= EV_EOF;
2336 return (kn->kn_fflags != 0);
2339 /* Global vfs data structures for ufs. */
2340 static struct vop_ops ufs_vnode_vops = {
2341 .vop_default = vop_defaultop,
2342 .vop_fsync = (void *)ufs_missingop,
2343 .vop_read = (void *)ufs_missingop,
2344 .vop_reallocblks = (void *)ufs_missingop,
2345 .vop_write = (void *)ufs_missingop,
2346 .vop_access = ufs_access,
2347 .vop_advlock = ufs_advlock,
2348 .vop_bmap = ufs_bmap,
2349 .vop_old_lookup = ufs_lookup,
2350 .vop_close = ufs_close,
2351 .vop_old_create = ufs_create,
2352 .vop_getattr = ufs_getattr,
2353 .vop_inactive = ufs_inactive,
2354 .vop_old_link = ufs_link,
2355 .vop_old_mkdir = ufs_mkdir,
2356 .vop_old_mknod = ufs_mknod,
2357 .vop_mmap = ufs_mmap,
2358 .vop_open = ufs_open,
2359 .vop_pathconf = vop_stdpathconf,
2360 .vop_poll = vop_stdpoll,
2361 .vop_kqfilter = ufs_kqfilter,
2362 .vop_print = ufs_print,
2363 .vop_readdir = ufs_readdir,
2364 .vop_readlink = ufs_readlink,
2365 .vop_reclaim = ufs_reclaim,
2366 .vop_old_remove = ufs_remove,
2367 .vop_old_rename = ufs_rename,
2368 .vop_old_rmdir = ufs_rmdir,
2369 .vop_setattr = ufs_setattr,
2370 .vop_strategy = ufs_strategy,
2371 .vop_old_symlink = ufs_symlink,
2372 .vop_old_whiteout = ufs_whiteout
2375 static struct vop_ops ufs_spec_vops = {
2376 .vop_default = spec_vnoperate,
2377 .vop_fsync = (void *)ufs_missingop,
2378 .vop_access = ufs_access,
2379 .vop_close = ufsspec_close,
2380 .vop_getattr = ufs_getattr,
2381 .vop_inactive = ufs_inactive,
2382 .vop_print = ufs_print,
2383 .vop_read = ufsspec_read,
2384 .vop_reclaim = ufs_reclaim,
2385 .vop_setattr = ufs_setattr,
2386 .vop_write = ufsspec_write
2389 static struct vop_ops ufs_fifo_vops = {
2390 .vop_default = fifo_vnoperate,
2391 .vop_fsync = (void *)ufs_missingop,
2392 .vop_access = ufs_access,
2393 .vop_close = ufsfifo_close,
2394 .vop_getattr = ufs_getattr,
2395 .vop_inactive = ufs_inactive,
2396 .vop_kqfilter = ufsfifo_kqfilter,
2397 .vop_print = ufs_print,
2398 .vop_read = ufsfifo_read,
2399 .vop_reclaim = ufs_reclaim,
2400 .vop_setattr = ufs_setattr,
2401 .vop_write = ufsfifo_write
2404 VNODEOP_SET(ufs_vnode_vops);
2405 VNODEOP_SET(ufs_spec_vops);
2406 VNODEOP_SET(ufs_fifo_vops);
2412 ufs_vnoperate(struct vop_generic_args *ap)
2414 return (VOCALL(&ufs_vnode_vops, ap));
2418 * ufs_vnoperatefifo()
2421 ufs_vnoperatefifo(struct vop_generic_args *ap)
2423 return (VOCALL(&ufs_fifo_vops, ap));
2427 * ufs_vnoperatespec()
2430 ufs_vnoperatespec(struct vop_generic_args *ap)
2432 return (VOCALL(&ufs_spec_vops, ap));