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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
35 * $FreeBSD: src/sys/ufs/ufs/ufs_vnops.c,v 1.131.2.8 2003/01/02 17:26:19 bde Exp $
38 #include "opt_quota.h"
39 #include "opt_suiddir.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/fcntl.h>
50 #include <sys/namei.h>
51 #include <sys/mount.h>
52 #include <sys/unistd.h>
53 #include <sys/vnode.h>
54 #include <sys/malloc.h>
55 #include <sys/dirent.h>
56 #include <sys/lockf.h>
57 #include <sys/event.h>
60 #include <sys/file.h> /* XXX */
64 #include <vm/vm_extern.h>
66 #include <vfs/fifofs/fifo.h>
72 #include "ufs_extern.h"
73 #include "ffs_extern.h"
79 static int ufs_access (struct vop_access_args *);
80 static int ufs_advlock (struct vop_advlock_args *);
81 static int ufs_chmod (struct vnode *, int, struct ucred *);
82 static int ufs_chown (struct vnode *, uid_t, gid_t, struct ucred *);
83 static int ufs_close (struct vop_close_args *);
84 static int ufs_create (struct vop_old_create_args *);
85 static int ufs_getattr (struct vop_getattr_args *);
86 static int ufs_link (struct vop_old_link_args *);
87 static int ufs_makeinode (int mode, struct vnode *, struct vnode **, struct componentname *);
88 static int ufs_markatime (struct vop_markatime_args *);
89 static int ufs_missingop (struct vop_generic_args *ap);
90 static int ufs_mkdir (struct vop_old_mkdir_args *);
91 static int ufs_mknod (struct vop_old_mknod_args *);
92 static int ufs_mmap (struct vop_mmap_args *);
93 static int ufs_print (struct vop_print_args *);
94 static int ufs_readdir (struct vop_readdir_args *);
95 static int ufs_readlink (struct vop_readlink_args *);
96 static int ufs_remove (struct vop_old_remove_args *);
97 static int ufs_rename (struct vop_old_rename_args *);
98 static int ufs_rmdir (struct vop_old_rmdir_args *);
99 static int ufs_setattr (struct vop_setattr_args *);
100 static int ufs_strategy (struct vop_strategy_args *);
101 static int ufs_symlink (struct vop_old_symlink_args *);
102 static int ufs_whiteout (struct vop_old_whiteout_args *);
103 static int ufsfifo_close (struct vop_close_args *);
104 static int ufsfifo_kqfilter (struct vop_kqfilter_args *);
105 static int ufsfifo_read (struct vop_read_args *);
106 static int ufsfifo_write (struct vop_write_args *);
107 static int filt_ufsread (struct knote *kn, long hint);
108 static int filt_ufswrite (struct knote *kn, long hint);
109 static int filt_ufsvnode (struct knote *kn, long hint);
110 static void filt_ufsdetach (struct knote *kn);
111 static int ufs_kqfilter (struct vop_kqfilter_args *ap);
117 #define SETHIGH(q, h) { \
120 tmp.val[_QUAD_HIGHWORD] = (h); \
123 #define SETLOW(q, l) { \
126 tmp.val[_QUAD_LOWWORD] = (l); \
129 #define VN_KNOTE(vp, b) \
130 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, (b))
132 #define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0)
135 * A virgin directory (no blushing please).
137 static struct dirtemplate mastertemplate = {
138 0, 12, DT_DIR, 1, ".",
139 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
141 static struct odirtemplate omastertemplate = {
143 0, DIRBLKSIZ - 12, 2, ".."
147 ufs_itimes(struct vnode *vp)
153 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
155 if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
156 ip->i_flag |= IN_LAZYMOD;
158 ip->i_flag |= IN_MODIFIED;
159 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
161 if (ip->i_flag & IN_ACCESS) {
162 ip->i_atime = ts.tv_sec;
163 ip->i_atimensec = ts.tv_nsec;
165 if (ip->i_flag & IN_UPDATE) {
166 ip->i_mtime = ts.tv_sec;
167 ip->i_mtimensec = ts.tv_nsec;
170 if (ip->i_flag & IN_CHANGE) {
171 ip->i_ctime = ts.tv_sec;
172 ip->i_ctimensec = ts.tv_nsec;
175 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
179 * Create a regular file
181 * ufs_create(struct vnode *a_dvp, struct vnode **a_vpp,
182 * struct componentname *a_cnp, struct vattr *a_vap)
186 ufs_create(struct vop_old_create_args *ap)
191 ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
192 ap->a_dvp, ap->a_vpp, ap->a_cnp);
195 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
202 * ufs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
203 * struct componentname *a_cnp, struct vattr *a_vap)
208 ufs_mknod(struct vop_old_mknod_args *ap)
210 struct vattr *vap = ap->a_vap;
211 struct vnode **vpp = ap->a_vpp;
217 * UFS cannot represent the entire major/minor range supported by
220 if (vap->va_rmajor != VNOVAL &&
221 makeudev(vap->va_rmajor, vap->va_rminor) == NOUDEV) {
225 /* no special directory support */
226 if (vap->va_type == VDIR)
229 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
230 ap->a_dvp, vpp, ap->a_cnp);
233 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
235 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
236 if (vap->va_rmajor != VNOVAL) {
238 * Want to be able to use this to make badblock
239 * inodes, so don't truncate the dev number.
241 ip->i_rdev = makeudev(vap->va_rmajor, vap->va_rminor);
244 * Remove inode, then reload it through VFS_VGET so it is
245 * checked to see if it is an alias of an existing entry in
248 (*vpp)->v_type = VNON;
249 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
250 vgone_vxlocked(*vpp);
252 error = VFS_VGET(ap->a_dvp->v_mount, NULL, ino, vpp);
263 * Update the times on the inode.
265 * ufs_close(struct vnode *a_vp, int a_fflag)
270 ufs_close(struct vop_close_args *ap)
272 struct vnode *vp = ap->a_vp;
276 return (vop_stdclose(ap));
280 * ufs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
284 ufs_access(struct vop_access_args *ap)
286 struct vnode *vp = ap->a_vp;
287 struct inode *ip = VTOI(vp);
291 if (ap->a_mode & VWRITE) {
292 switch (vp->v_type) {
296 if ((error = ufs_getinoquota(ip)) != 0)
305 error = vop_helper_access(ap, ip->i_uid, ip->i_gid, ip->i_mode, 0);
310 * ufs_getattr(struct vnode *a_vp, struct vattr *a_vap)
315 ufs_getattr(struct vop_getattr_args *ap)
317 struct vnode *vp = ap->a_vp;
318 struct inode *ip = VTOI(vp);
319 struct vattr *vap = ap->a_vap;
323 * Copy from inode table
325 vap->va_fsid = dev2udev(ip->i_dev);
326 vap->va_fileid = ip->i_number;
327 vap->va_mode = ip->i_mode & ~IFMT;
328 vap->va_nlink = VFSTOUFS(vp->v_mount)->um_i_effnlink_valid ?
329 ip->i_effnlink : ip->i_nlink;
330 vap->va_uid = ip->i_uid;
331 vap->va_gid = ip->i_gid;
332 vap->va_rmajor = umajor(ip->i_rdev);
333 vap->va_rminor = uminor(ip->i_rdev);
334 vap->va_size = ip->i_din.di_size;
335 vap->va_atime.tv_sec = ip->i_atime;
336 vap->va_atime.tv_nsec = ip->i_atimensec;
337 vap->va_mtime.tv_sec = ip->i_mtime;
338 vap->va_mtime.tv_nsec = ip->i_mtimensec;
339 vap->va_ctime.tv_sec = ip->i_ctime;
340 vap->va_ctime.tv_nsec = ip->i_ctimensec;
341 vap->va_flags = ip->i_flags;
342 vap->va_gen = ip->i_gen;
343 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
344 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
345 vap->va_type = IFTOVT(ip->i_mode);
346 vap->va_filerev = ip->i_modrev;
352 ufs_markatime(struct vop_markatime_args *ap)
354 struct vnode *vp = ap->a_vp;
355 struct inode *ip = VTOI(vp);
357 if (vp->v_mount->mnt_flag & MNT_RDONLY)
359 if (vp->v_mount->mnt_flag & MNT_NOATIME)
361 ip->i_flag |= IN_ACCESS;
362 VN_KNOTE(vp, NOTE_ATTRIB);
367 * Set attribute vnode op. called from several syscalls
369 * ufs_setattr(struct vnode *a_vp, struct vattr *a_vap,
370 * struct ucred *a_cred)
374 ufs_setattr(struct vop_setattr_args *ap)
376 struct vattr *vap = ap->a_vap;
377 struct vnode *vp = ap->a_vp;
378 struct inode *ip = VTOI(vp);
379 struct ucred *cred = ap->a_cred;
383 * Check for unsettable attributes.
385 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
386 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
387 (vap->va_blocksize != VNOVAL) || (vap->va_rmajor != VNOVAL) ||
388 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
391 if (vap->va_flags != VNOVAL) {
392 if (vp->v_mount->mnt_flag & MNT_RDONLY)
394 if (cred->cr_uid != ip->i_uid &&
395 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)))
398 * Note that a root chflags becomes a user chflags when
399 * we are jailed, unless the jail.chflags_allowed sysctl
402 if (cred->cr_uid == 0 &&
403 (!jailed(cred) || jail_chflags_allowed)) {
405 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) &&
408 ip->i_flags = vap->va_flags;
411 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
412 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
414 ip->i_flags &= SF_SETTABLE;
415 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
417 ip->i_flag |= IN_CHANGE;
418 if (vap->va_flags & (IMMUTABLE | APPEND))
421 if (ip->i_flags & (IMMUTABLE | APPEND))
424 * Go through the fields and update iff not VNOVAL.
426 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
427 if (vp->v_mount->mnt_flag & MNT_RDONLY)
429 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred)) != 0)
432 if (vap->va_size != VNOVAL) {
434 * Disallow write attempts on read-only filesystems;
435 * unless the file is a socket, fifo, or a block or
436 * character device resident on the filesystem.
438 switch (vp->v_type) {
443 if (vp->v_mount->mnt_flag & MNT_RDONLY)
449 if ((error = ffs_truncate(vp, vap->va_size, 0, cred)) != 0)
453 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
454 if (vp->v_mount->mnt_flag & MNT_RDONLY)
456 if (cred->cr_uid != ip->i_uid &&
457 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)) &&
458 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
459 (error = VOP_EACCESS(vp, VWRITE, cred))))
461 if (vap->va_atime.tv_sec != VNOVAL)
462 ip->i_flag |= IN_ACCESS;
463 if (vap->va_mtime.tv_sec != VNOVAL)
464 ip->i_flag |= IN_CHANGE | IN_UPDATE;
466 if (vap->va_atime.tv_sec != VNOVAL) {
467 ip->i_atime = vap->va_atime.tv_sec;
468 ip->i_atimensec = vap->va_atime.tv_nsec;
470 if (vap->va_mtime.tv_sec != VNOVAL) {
471 ip->i_mtime = vap->va_mtime.tv_sec;
472 ip->i_mtimensec = vap->va_mtime.tv_nsec;
474 error = ffs_update(vp, 0);
479 if (vap->va_mode != (mode_t)VNOVAL) {
480 if (vp->v_mount->mnt_flag & MNT_RDONLY)
482 error = ufs_chmod(vp, (int)vap->va_mode, cred);
484 VN_KNOTE(vp, NOTE_ATTRIB);
489 * Change the mode on a file.
490 * Inode must be locked before calling.
493 ufs_chmod(struct vnode *vp, int mode, struct ucred *cred)
495 struct inode *ip = VTOI(vp);
497 mode_t cur_mode = ip->i_mode;
499 error = vop_helper_chmod(vp, mode, cred, ip->i_uid, ip->i_gid,
504 if (cred->cr_uid != ip->i_uid) {
505 error = priv_check_cred(cred, PRIV_VFS_CHMOD, 0);
510 if (vp->v_type != VDIR && (mode & S_ISTXT))
512 if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
516 ip->i_mode = cur_mode;
517 ip->i_flag |= IN_CHANGE;
522 * Perform chown operation on inode ip;
523 * inode must be locked prior to call.
526 ufs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred)
528 struct inode *ip = VTOI(vp);
537 if (uid == (uid_t)VNOVAL)
539 if (gid == (gid_t)VNOVAL)
542 * If we don't own the file, are trying to change the owner
543 * of the file, or are not a member of the target group,
544 * the caller must be superuser or the call fails.
546 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
547 (gid != ip->i_gid && !(cred->cr_gid == gid ||
548 groupmember(gid, cred)))) &&
549 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
554 if ((error = ufs_getinoquota(ip)) != 0)
557 ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
558 ip->i_dquot[USRQUOTA] = NODQUOT;
561 ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
562 ip->i_dquot[GRPQUOTA] = NODQUOT;
564 change = ip->i_blocks;
565 (void) ufs_chkdq(ip, -change, cred, CHOWN);
566 (void) ufs_chkiq(ip, -1, cred, CHOWN);
567 for (i = 0; i < MAXQUOTAS; i++) {
568 ufs_dqrele(vp, ip->i_dquot[i]);
569 ip->i_dquot[i] = NODQUOT;
575 if ((error = ufs_getinoquota(ip)) == 0) {
577 ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
578 ip->i_dquot[USRQUOTA] = NODQUOT;
581 ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
582 ip->i_dquot[GRPQUOTA] = NODQUOT;
584 if ((error = ufs_chkdq(ip, change, cred, CHOWN)) == 0) {
585 if ((error = ufs_chkiq(ip, 1, cred, CHOWN)) == 0)
588 (void)ufs_chkdq(ip, -change, cred, CHOWN|FORCE);
590 for (i = 0; i < MAXQUOTAS; i++) {
591 ufs_dqrele(vp, ip->i_dquot[i]);
592 ip->i_dquot[i] = NODQUOT;
597 if (ufs_getinoquota(ip) == 0) {
599 ufs_dqrele(vp, ip->i_dquot[USRQUOTA]);
600 ip->i_dquot[USRQUOTA] = NODQUOT;
603 ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]);
604 ip->i_dquot[GRPQUOTA] = NODQUOT;
606 (void) ufs_chkdq(ip, change, cred, FORCE|CHOWN);
607 (void) ufs_chkiq(ip, 1, cred, FORCE|CHOWN);
608 (void) ufs_getinoquota(ip);
612 if (ufs_getinoquota(ip))
613 panic("ufs_chown: lost quota");
615 ip->i_flag |= IN_CHANGE;
616 if (cred->cr_uid != 0 && (ouid != uid || ogid != gid))
617 ip->i_mode &= ~(ISUID | ISGID);
624 * NB Currently unsupported.
626 * ufs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
631 ufs_mmap(struct vop_mmap_args *ap)
637 * ufs_remove(struct vnode *a_dvp, struct vnode *a_vp,
638 * struct componentname *a_cnp)
642 ufs_remove(struct vop_old_remove_args *ap)
645 struct vnode *vp = ap->a_vp;
646 struct vnode *dvp = ap->a_dvp;
650 #if 0 /* handled by kernel now */
651 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
652 (VTOI(dvp)->i_flags & APPEND)) {
657 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
658 VN_KNOTE(vp, NOTE_DELETE);
659 VN_KNOTE(dvp, NOTE_WRITE);
669 * ufs_link(struct vnode *a_tdvp, struct vnode *a_vp,
670 * struct componentname *a_cnp)
674 ufs_link(struct vop_old_link_args *ap)
676 struct vnode *vp = ap->a_vp;
677 struct vnode *tdvp = ap->a_tdvp;
678 struct componentname *cnp = ap->a_cnp;
680 struct direct newdir;
683 if (tdvp->v_mount != vp->v_mount) {
688 error = vn_lock(vp, LK_EXCLUSIVE | LK_FAILRECLAIM);
693 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
697 #if 0 /* handled by kernel now, also DragonFly allows this */
698 if (ip->i_flags & (IMMUTABLE | APPEND)) {
705 ip->i_flag |= IN_CHANGE;
706 if (DOINGSOFTDEP(vp))
707 softdep_change_linkcnt(ip);
708 error = ffs_update(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
710 ufs_makedirentry(ip, cnp, &newdir);
711 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
717 ip->i_flag |= IN_CHANGE;
718 if (DOINGSOFTDEP(vp))
719 softdep_change_linkcnt(ip);
725 VN_KNOTE(vp, NOTE_LINK);
726 VN_KNOTE(tdvp, NOTE_WRITE);
731 * whiteout vnode call
733 * ufs_whiteout(struct vnode *a_dvp, struct componentname *a_cnp, int a_flags)
737 ufs_whiteout(struct vop_old_whiteout_args *ap)
739 struct vnode *dvp = ap->a_dvp;
740 struct componentname *cnp = ap->a_cnp;
741 struct direct newdir;
744 switch (ap->a_flags) {
746 /* 4.4 format directories support whiteout operations */
747 if (dvp->v_mount->mnt_maxsymlinklen > 0)
752 /* create a new directory whiteout */
754 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
755 panic("ufs_whiteout: old format filesystem");
759 newdir.d_namlen = cnp->cn_namelen;
760 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
761 newdir.d_type = DT_WHT;
762 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
766 /* remove an existing directory whiteout */
768 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
769 panic("ufs_whiteout: old format filesystem");
772 cnp->cn_flags &= ~CNP_DOWHITEOUT;
773 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
776 panic("ufs_whiteout: unknown op");
782 * Rename system call.
783 * rename("foo", "bar");
786 * link("foo", "bar");
788 * but ``atomically''. Can't do full commit without saving state in the
789 * inode on disk which isn't feasible at this time. Best we can do is
790 * always guarantee the target exists.
792 * Basic algorithm is:
794 * 1) Bump link count on source while we're linking it to the
795 * target. This also ensure the inode won't be deleted out
796 * from underneath us while we work (it may be truncated by
797 * a concurrent `trunc' or `open' for creation).
798 * 2) Link source to destination. If destination already exists,
800 * 3) Unlink source reference to inode if still around. If a
801 * directory was moved and the parent of the destination
802 * is different from the source, patch the ".." entry in the
805 * ufs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
806 * struct componentname *a_fcnp, struct vnode *a_tdvp,
807 * struct vnode *a_tvp, struct componentname *a_tcnp)
811 ufs_rename(struct vop_old_rename_args *ap)
813 struct vnode *tvp = ap->a_tvp;
814 struct vnode *tdvp = ap->a_tdvp;
815 struct vnode *fvp = ap->a_fvp;
816 struct vnode *fdvp = ap->a_fdvp;
817 struct componentname *tcnp = ap->a_tcnp;
818 struct componentname *fcnp = ap->a_fcnp;
819 struct inode *ip, *xp, *dp;
820 struct direct newdir;
821 ino_t oldparent = 0, newparent = 0;
822 int doingdirectory = 0;
823 int error = 0, ioflag;
826 * Check for cross-device rename.
828 if ((fvp->v_mount != tdvp->v_mount) ||
829 (tvp && (fvp->v_mount != tvp->v_mount))) {
843 #if 0 /* handled by kernel now */
844 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
845 (VTOI(tdvp)->i_flags & APPEND))) {
852 * Renaming a file to itself has no effect. The upper layers should
853 * not call us in that case. Temporarily just warn if they do.
856 kprintf("ufs_rename: fvp == tvp (can't happen)\n");
861 error = vn_lock(fvp, LK_EXCLUSIVE | LK_FAILRECLAIM);
866 * Note: now that fvp is locked we have to be sure to unlock it before
867 * using the 'abortit' target.
871 if (ip->i_nlink >= LINK_MAX) {
876 #if 0 /* handled by kernel now */
877 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
878 || (dp->i_flags & APPEND)) {
884 if ((ip->i_mode & IFMT) == IFDIR) {
886 * Avoid ".", "..", and aliases of "." for obvious reasons.
888 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
889 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & CNP_ISDOTDOT ||
890 (ip->i_flag & IN_RENAME)) {
895 ip->i_flag |= IN_RENAME;
896 oldparent = dp->i_number;
899 VN_KNOTE(fdvp, NOTE_WRITE); /* XXX right place? */
902 * fvp still locked. ip->i_flag has IN_RENAME set if doingdirectory.
903 * Cleanup fvp requirements so we can unlock it.
905 * tvp and tdvp are locked. tvp may be NULL. Now that dp and xp
906 * is setup we can use the 'bad' target if we unlock fvp. We cannot
907 * use the abortit target anymore because of IN_RENAME.
916 * 1) Bump link count while we're moving stuff
917 * around. If we crash somewhere before
918 * completing our work, the link count
919 * may be wrong, but correctable.
923 ip->i_flag |= IN_CHANGE;
924 if (DOINGSOFTDEP(fvp))
925 softdep_change_linkcnt(ip);
926 if ((error = ffs_update(fvp, !(DOINGSOFTDEP(fvp) |
927 DOINGASYNC(fvp)))) != 0) {
933 * If ".." must be changed (ie the directory gets a new
934 * parent) then the source directory must not be in the
935 * directory heirarchy above the target, as this would
936 * orphan everything below the source directory. Also
937 * the user must have write permission in the source so
938 * as to be able to change "..". We must repeat the call
939 * to namei, as the parent directory is unlocked by the
940 * call to checkpath().
942 error = VOP_EACCESS(fvp, VWRITE, tcnp->cn_cred);
946 * We are now back to where we were in that fvp, fdvp are unlocked
947 * and tvp, tdvp are locked. tvp may be NULL. IN_RENAME may be
948 * set. Only the bad target or, if we clean up tvp and tdvp, the
949 * out target, may be used.
951 if (oldparent != dp->i_number)
952 newparent = dp->i_number;
953 if (doingdirectory && newparent) {
954 if (error) /* write access check above */
958 * Once we start messing with tvp and tdvp we cannot use the
959 * 'bad' target, only finish cleaning tdvp and tvp up and
960 * use the 'out' target.
962 * This cleans up tvp.
970 * This is a real mess. ufs_checkpath vput's the target
971 * directory so retain an extra ref and note that tdvp will
972 * lose its lock on return. This leaves us with one good
973 * ref after ufs_checkpath returns.
976 error = ufs_checkpath(ip, dp, tcnp->cn_cred);
977 tcnp->cn_flags |= CNP_PDIRUNLOCK;
984 * relookup no longer messes with tdvp's refs. tdvp must be
985 * unlocked on entry and will be locked on a successful
988 error = relookup(tdvp, &tvp, tcnp);
990 if (tcnp->cn_flags & CNP_PDIRUNLOCK)
996 KKASSERT((tcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
1003 * We are back to fvp, fdvp unlocked, tvp, tdvp locked. tvp may
1004 * be NULL (xp will also be NULL in that case), and IN_RENAME will
1005 * be set if doingdirectory. This means we can use the 'bad' target
1010 * 2) If target doesn't exist, link the target
1011 * to the source and unlink the source.
1012 * Otherwise, rewrite the target directory
1013 * entry to reference the source inode and
1014 * expunge the original entry's existence.
1017 if (dp->i_dev != ip->i_dev)
1018 panic("ufs_rename: EXDEV");
1020 * Account for ".." in new directory.
1021 * When source and destination have the same
1022 * parent we don't fool with the link count.
1024 if (doingdirectory && newparent) {
1025 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1031 dp->i_flag |= IN_CHANGE;
1032 if (DOINGSOFTDEP(tdvp))
1033 softdep_change_linkcnt(dp);
1034 error = ffs_update(tdvp, !(DOINGSOFTDEP(tdvp) |
1039 ufs_makedirentry(ip, tcnp, &newdir);
1040 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1042 if (doingdirectory && newparent) {
1045 dp->i_flag |= IN_CHANGE;
1046 if (DOINGSOFTDEP(tdvp))
1047 softdep_change_linkcnt(dp);
1048 (void)ffs_update(tdvp, 1);
1052 VN_KNOTE(tdvp, NOTE_WRITE);
1055 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1056 panic("ufs_rename: EXDEV");
1058 * Short circuit rename(foo, foo).
1060 if (xp->i_number == ip->i_number)
1061 panic("ufs_rename: same file");
1063 * If the parent directory is "sticky", then the user must
1064 * own the parent directory, or the destination of the rename,
1065 * otherwise the destination may not be changed (except by
1066 * root). This implements append-only directories.
1068 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
1069 tcnp->cn_cred->cr_uid != dp->i_uid &&
1070 xp->i_uid != tcnp->cn_cred->cr_uid) {
1075 * Target must be empty if a directory and have no links
1076 * to it. Also, ensure source and target are compatible
1077 * (both directories, or both not directories).
1079 * Purge the file or directory being replaced from the
1082 if ((xp->i_mode&IFMT) == IFDIR) {
1083 if ((xp->i_effnlink > 2) ||
1084 !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1088 if (!doingdirectory) {
1092 /* cache_purge removed - handled by VFS compat layer */
1093 } else if (doingdirectory == 0) {
1094 /* cache_purge removed - handled by VFS compat layer */
1100 * note: inode passed to ufs_dirrewrite() is 0 for a
1101 * non-directory file rename, 1 for a directory rename
1102 * in the same directory, and > 1 for an inode representing
1103 * the new directory.
1105 error = ufs_dirrewrite(dp, xp, ip->i_number,
1107 (doingdirectory && newparent) ?
1108 newparent : (ino_t)doingdirectory);
1111 if (doingdirectory) {
1114 if (DOINGSOFTDEP(tdvp))
1115 softdep_change_linkcnt(dp);
1118 if (DOINGSOFTDEP(tvp))
1119 softdep_change_linkcnt(xp);
1121 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1123 * Truncate inode. The only stuff left in the directory
1124 * is "." and "..". The "." reference is inconsequential
1125 * since we are quashing it. We have removed the "."
1126 * reference and the reference in the parent directory,
1127 * but there may be other hard links. The soft
1128 * dependency code will arrange to do these operations
1129 * after the parent directory entry has been deleted on
1130 * disk, so when running with that code we avoid doing
1135 dp->i_flag |= IN_CHANGE;
1138 xp->i_flag |= IN_CHANGE;
1139 ioflag = DOINGASYNC(tvp) ? 0 : IO_SYNC;
1140 error = ffs_truncate(tvp, (off_t)0, ioflag,
1145 VN_KNOTE(tdvp, NOTE_WRITE);
1147 VN_KNOTE(tvp, NOTE_DELETE);
1153 * tvp and tdvp have been cleaned up. only fvp and fdvp (both
1154 * unlocked) remain. We are about to overwrite fvp but we have to
1155 * keep 'ip' intact so we cannot release the old fvp, which is still
1156 * refd and accessible via ap->a_fvp.
1158 * This means we cannot use either 'bad' or 'out' to cleanup any
1163 * 3) Unlink the source.
1165 fcnp->cn_flags &= ~CNP_MODMASK;
1166 fcnp->cn_flags |= CNP_LOCKPARENT;
1167 error = relookup(fdvp, &fvp, fcnp);
1168 if (error || fvp == NULL) {
1170 * From name has disappeared. IN_RENAME will not be set if
1171 * we get past the panic so we don't have to clean it up.
1174 panic("ufs_rename: lost dir entry");
1176 if (fcnp->cn_flags & CNP_PDIRUNLOCK)
1182 KKASSERT((fcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
1185 * fdvp and fvp are locked.
1191 * Ensure that the directory entry still exists and has not
1192 * changed while the new name has been entered. If the source is
1193 * a file then the entry may have been unlinked or renamed. In
1194 * either case there is no further work to be done. If the source
1195 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1196 * flag ensures that it cannot be moved by another rename or removed
1197 * by a rmdir. Cleanup IN_RENAME.
1201 panic("ufs_rename: lost dir entry");
1204 * If the source is a directory with a
1205 * new parent, the link count of the old
1206 * parent directory must be decremented
1207 * and ".." set to point to the new parent.
1209 if (doingdirectory && newparent) {
1210 xp->i_offset = mastertemplate.dot_reclen;
1211 ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1212 /* cache_purge removed - handled by VFS compat layer */
1214 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1215 xp->i_flag &= ~IN_RENAME;
1218 VN_KNOTE(fvp, NOTE_RENAME);
1230 ip->i_flag &= ~IN_RENAME;
1231 if (vn_lock(fvp, LK_EXCLUSIVE | LK_FAILRECLAIM) == 0) {
1234 ip->i_flag |= IN_CHANGE;
1235 ip->i_flag &= ~IN_RENAME;
1236 if (DOINGSOFTDEP(fvp))
1237 softdep_change_linkcnt(ip);
1248 * ufs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1249 * struct componentname *a_cnp, struct vattr *a_vap)
1253 ufs_mkdir(struct vop_old_mkdir_args *ap)
1255 struct vnode *dvp = ap->a_dvp;
1256 struct vattr *vap = ap->a_vap;
1257 struct componentname *cnp = ap->a_cnp;
1258 struct inode *ip, *dp;
1261 struct dirtemplate dirtemplate, *dtp;
1262 struct direct newdir;
1267 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1271 dmode = vap->va_mode & 0777;
1274 * Must simulate part of ufs_makeinode here to acquire the inode,
1275 * but not have it entered in the parent directory. The entry is
1276 * made later after writing "." and ".." entries.
1278 error = ffs_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1282 ip->i_gid = dp->i_gid;
1286 struct ucred ucred, *ucp;
1290 * If we are hacking owners here, (only do this where told to)
1291 * and we are not giving it TO root, (would subvert quotas)
1292 * then go ahead and give it to the other user.
1293 * The new directory also inherits the SUID bit.
1294 * If user's UID and dir UID are the same,
1295 * 'give it away' so that the SUID is still forced on.
1297 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1298 (dp->i_mode & ISUID) && dp->i_uid) {
1300 ip->i_uid = dp->i_uid;
1302 if (dp->i_uid != cnp->cn_cred->cr_uid) {
1304 * Make sure the correct user gets charged
1306 * Make a dummy credential for the victim.
1307 * XXX This seems to never be accessed out of
1308 * our context so a stack variable is ok.
1311 ucred.cr_uid = ip->i_uid;
1312 ucred.cr_ngroups = 1;
1313 ucred.cr_groups[0] = dp->i_gid;
1318 ip->i_uid = cnp->cn_cred->cr_uid;
1320 if ((error = ufs_getinoquota(ip)) ||
1321 (error = ufs_chkiq(ip, 1, ucp, 0))) {
1322 ffs_vfree(tvp, ip->i_number, dmode);
1328 #else /* !SUIDDIR */
1329 ip->i_uid = cnp->cn_cred->cr_uid;
1331 if ((error = ufs_getinoquota(ip)) ||
1332 (error = ufs_chkiq(ip, 1, cnp->cn_cred, 0))) {
1333 ffs_vfree(tvp, ip->i_number, dmode);
1338 #endif /* !SUIDDIR */
1339 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1341 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1344 if (DOINGSOFTDEP(tvp))
1345 softdep_change_linkcnt(ip);
1346 if (cnp->cn_flags & CNP_ISWHITEOUT)
1347 ip->i_flags |= UF_OPAQUE;
1350 * Bump link count in parent directory to reflect work done below.
1351 * Should be done before reference is created so cleanup is
1352 * possible if we crash.
1356 dp->i_flag |= IN_CHANGE;
1357 if (DOINGSOFTDEP(dvp))
1358 softdep_change_linkcnt(dp);
1359 error = ffs_update(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1364 * The vnode must have a VM object in order to issue buffer cache
1367 vinitvmio(tvp, DIRBLKSIZ, DIRBLKSIZ, -1);
1370 * Initialize directory with "." and ".." from static template.
1372 if (dvp->v_mount->mnt_maxsymlinklen > 0)
1373 dtp = &mastertemplate;
1375 dtp = (struct dirtemplate *)&omastertemplate;
1377 dirtemplate.dot_ino = ip->i_number;
1378 dirtemplate.dotdot_ino = dp->i_number;
1379 nvnode_pager_setsize(tvp, DIRBLKSIZ, DIRBLKSIZ, -1);
1380 error = VOP_BALLOC(tvp, 0LL, DIRBLKSIZ, cnp->cn_cred, B_CLRBUF, &bp);
1383 ip->i_size = DIRBLKSIZ;
1384 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1385 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1386 if (DOINGSOFTDEP(tvp)) {
1388 * Ensure that the entire newly allocated block is a
1389 * valid directory so that future growth within the
1390 * block does not have to ensure that the block is
1391 * written before the inode.
1394 while (blkoff < bp->b_bcount) {
1396 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1397 blkoff += DIRBLKSIZ;
1400 if ((error = ffs_update(tvp, !(DOINGSOFTDEP(tvp) |
1401 DOINGASYNC(tvp)))) != 0) {
1406 * Directory set up, now install its entry in the parent directory.
1408 * If we are not doing soft dependencies, then we must write out the
1409 * buffer containing the new directory body before entering the new
1410 * name in the parent. If we are doing soft dependencies, then the
1411 * buffer containing the new directory body will be passed to and
1412 * released in the soft dependency code after the code has attached
1413 * an appropriate ordering dependency to the buffer which ensures that
1414 * the buffer is written before the new name is written in the parent.
1416 if (DOINGASYNC(dvp))
1418 else if (!DOINGSOFTDEP(dvp) && (error = bwrite(bp)) != 0)
1420 ufs_makedirentry(ip, cnp, &newdir);
1421 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1425 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1430 dp->i_flag |= IN_CHANGE;
1431 if (DOINGSOFTDEP(dvp))
1432 softdep_change_linkcnt(dp);
1434 * No need to do an explicit VOP_TRUNCATE here, vrele will
1435 * do this for us because we set the link count to 0.
1439 ip->i_flag |= IN_CHANGE;
1440 if (DOINGSOFTDEP(tvp))
1441 softdep_change_linkcnt(ip);
1449 * Rmdir system call.
1451 * ufs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1452 * struct componentname *a_cnp)
1456 ufs_rmdir(struct vop_old_rmdir_args *ap)
1458 struct vnode *vp = ap->a_vp;
1459 struct vnode *dvp = ap->a_dvp;
1460 struct componentname *cnp = ap->a_cnp;
1461 struct inode *ip, *dp;
1468 * Do not remove a directory that is in the process of being renamed.
1469 * Verify the directory is empty (and valid). Rmdir ".." will not be
1470 * valid since ".." will contain a reference to the current directory
1471 * and thus be non-empty. Do not allow the removal of mounted on
1472 * directories (this can happen when an NFS exported filesystem
1473 * tries to remove a locally mounted on directory).
1476 if (ip->i_flag & IN_RENAME) {
1480 if (ip->i_effnlink != 2 ||
1481 !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1485 #if 0 /* handled by kernel now */
1486 if ((dp->i_flags & APPEND)
1487 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1493 * Delete reference to directory before purging
1494 * inode. If we crash in between, the directory
1495 * will be reattached to lost+found,
1499 if (DOINGSOFTDEP(vp)) {
1500 softdep_change_linkcnt(dp);
1501 softdep_change_linkcnt(ip);
1503 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1507 if (DOINGSOFTDEP(vp)) {
1508 softdep_change_linkcnt(dp);
1509 softdep_change_linkcnt(ip);
1513 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1515 * Truncate inode. The only stuff left in the directory is "." and
1516 * "..". The "." reference is inconsequential since we are quashing
1517 * it. The soft dependency code will arrange to do these operations
1518 * after the parent directory entry has been deleted on disk, so
1519 * when running with that code we avoid doing them now.
1521 if (!DOINGSOFTDEP(vp)) {
1523 dp->i_flag |= IN_CHANGE;
1525 ip->i_flag |= IN_CHANGE;
1526 ioflag = DOINGASYNC(vp) ? 0 : IO_SYNC;
1527 error = ffs_truncate(vp, (off_t)0, ioflag, cnp->cn_cred);
1529 /* cache_purge removed - handled by VFS compat layer */
1531 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1532 if (ip->i_dirhash != NULL)
1533 ufsdirhash_free(ip);
1536 VN_KNOTE(vp, NOTE_DELETE);
1541 * symlink -- make a symbolic link
1543 * ufs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1544 * struct componentname *a_cnp, struct vattr *a_vap,
1549 ufs_symlink(struct vop_old_symlink_args *ap)
1551 struct vnode *vp, **vpp = ap->a_vpp;
1555 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1559 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1561 len = strlen(ap->a_target);
1562 if (len < vp->v_mount->mnt_maxsymlinklen) {
1564 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1566 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1569 * Make sure we have a VM object in order to use
1572 if (vp->v_object == NULL)
1573 vinitvmio(vp, 0, PAGE_SIZE, -1);
1574 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1575 UIO_SYSSPACE, IO_NODELOCKED,
1576 ap->a_cnp->cn_cred, NULL);
1584 * Vnode op for reading directories.
1586 * ufs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
1587 * int *a_eofflag, int *ncookies, off_t **a_cookies)
1591 ufs_readdir(struct vop_readdir_args *ap)
1593 struct uio *uio = ap->a_uio;
1594 struct vnode *vp = ap->a_vp;
1599 int offset; /* offset into buffer cache buffer */
1600 int eoffset; /* end of buffer clipped to file EOF */
1601 int pickup; /* pickup point */
1606 if (uio->uio_offset < 0)
1609 * Guess the number of cookies needed. Make sure we compute at
1610 * least 1, and no more then a reasonable limit.
1612 if (ap->a_ncookies) {
1613 ncookies = uio->uio_resid / 16 + 1;
1614 if (ncookies > 1024)
1616 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1618 ncookies = -1; /* force conditionals below */
1623 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
1630 if (uio->uio_offset >= VTOI(vp)->i_size) {
1633 if (ap->a_ncookies) {
1634 *ap->a_ncookies = cookie_index;
1635 *ap->a_cookies = cookies;
1641 * Loop until we run out of cookies, we run out of user buffer,
1642 * or we hit the directory EOF.
1644 * Always start scans at the beginning of the buffer, don't trust
1645 * the offset supplied by userland.
1647 while ((error = ffs_blkatoff_ra(vp, uio->uio_offset, NULL, &bp, 2)) == 0) {
1648 pickup = (int)(uio->uio_offset - bp->b_loffset);
1651 if (bp->b_loffset + bp->b_bcount > VTOI(vp)->i_size)
1652 eoffset = (int)(VTOI(vp)->i_size - bp->b_loffset);
1654 eoffset = bp->b_bcount;
1656 while (offset < eoffset) {
1657 dp = (struct direct *)(bp->b_data + offset);
1658 if (dp->d_reclen <= 0 || (dp->d_reclen & 3) ||
1659 offset + dp->d_reclen > bp->b_bcount) {
1663 if (offsetof(struct direct, d_name[dp->d_namlen]) > dp->d_reclen) {
1667 if (offset < pickup) {
1668 offset += dp->d_reclen;
1671 #if BYTE_ORDER == LITTLE_ENDIAN
1673 retval = vop_write_dirent(&error, uio,
1674 dp->d_ino, dp->d_namlen, dp->d_type,
1679 retval = vop_write_dirent(&error, uio,
1680 dp->d_ino, dp->d_type, dp->d_namlen,
1686 cookies[cookie_index] = bp->b_loffset + offset;
1688 offset += dp->d_reclen;
1689 if (cookie_index == ncookies)
1694 * This will align the next loop to the beginning of the
1695 * next block, and pickup will calculate to 0.
1697 uio->uio_offset = bp->b_loffset + offset;
1700 if (retval || error || cookie_index == ncookies ||
1701 uio->uio_offset >= VTOI(vp)->i_size) {
1706 *ap->a_eofflag = VTOI(vp)->i_size <= uio->uio_offset;
1709 * Report errors only if we didn't manage to read anything
1711 if (error && cookie_index == 0) {
1713 kfree(cookies, M_TEMP);
1714 *ap->a_ncookies = 0;
1715 *ap->a_cookies = NULL;
1720 *ap->a_ncookies = cookie_index;
1721 *ap->a_cookies = cookies;
1730 * Return target name of a symbolic link
1732 * ufs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1736 ufs_readlink(struct vop_readlink_args *ap)
1738 struct vnode *vp = ap->a_vp;
1739 struct inode *ip = VTOI(vp);
1743 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1744 (ip->i_din.di_blocks == 0)) { /* XXX - for old fastlink support */
1745 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1750 * Perform the equivalent of an OPEN on vp so we can issue a
1753 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1757 * Calculate the logical to physical mapping if not done already,
1758 * then call the device strategy routine.
1760 * In order to be able to swap to a file, the VOP_BMAP operation may not
1761 * deadlock on memory. See ufs_bmap() for details.
1763 * ufs_strategy(struct vnode *a_vp, struct bio *a_bio)
1767 ufs_strategy(struct vop_strategy_args *ap)
1769 struct bio *bio = ap->a_bio;
1771 struct buf *bp = bio->bio_buf;
1772 struct vnode *vp = ap->a_vp;
1777 if (vp->v_type == VBLK || vp->v_type == VCHR)
1778 panic("ufs_strategy: spec");
1779 nbio = push_bio(bio);
1780 if (nbio->bio_offset == NOOFFSET) {
1781 error = VOP_BMAP(vp, bio->bio_offset, &nbio->bio_offset,
1782 NULL, NULL, bp->b_cmd);
1784 bp->b_error = error;
1785 bp->b_flags |= B_ERROR;
1786 /* I/O was never started on nbio, must biodone(bio) */
1790 if (nbio->bio_offset == NOOFFSET)
1793 if (nbio->bio_offset == NOOFFSET) {
1795 * We hit a hole in the file. The buffer has been zero-filled
1796 * so just biodone() it.
1800 vn_strategy(ip->i_devvp, nbio);
1806 * Print out the contents of an inode.
1808 * ufs_print(struct vnode *a_vp)
1812 ufs_print(struct vop_print_args *ap)
1814 struct vnode *vp = ap->a_vp;
1815 struct inode *ip = VTOI(vp);
1817 kprintf("tag VT_UFS, ino %lu, on dev %s (%d, %d)",
1818 (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev),
1820 if (vp->v_type == VFIFO)
1822 lockmgr_printinfo(&vp->v_lock);
1828 * Read wrapper for fifos.
1830 * ufsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1831 * struct ucred *a_cred)
1835 ufsfifo_read(struct vop_read_args *ap)
1842 resid = uio->uio_resid;
1843 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1844 ip = VTOI(ap->a_vp);
1845 if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL &&
1846 (uio->uio_resid != resid || (error == 0 && resid != 0)))
1847 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1852 * Write wrapper for fifos.
1854 * ufsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1855 * struct ucred *a_cred)
1859 ufsfifo_write(struct vop_write_args *ap)
1866 resid = uio->uio_resid;
1867 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1868 ip = VTOI(ap->a_vp);
1869 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1870 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1875 * Close wrapper for fifos.
1877 * Update the times on the inode then do device close.
1879 * ufsfifo_close(struct vnode *a_vp, int a_fflag)
1883 ufsfifo_close(struct vop_close_args *ap)
1885 struct vnode *vp = ap->a_vp;
1887 if (VREFCNT(vp) > 1)
1889 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
1893 * Kqfilter wrapper for fifos.
1895 * Fall through to ufs kqfilter routines if needed
1899 ufsfifo_kqfilter(struct vop_kqfilter_args *ap)
1903 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1905 error = ufs_kqfilter(ap);
1910 * Advisory record locking support
1912 * ufs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
1917 ufs_advlock(struct vop_advlock_args *ap)
1919 struct inode *ip = VTOI(ap->a_vp);
1921 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
1925 * Initialize the vnode associated with a new inode, handle aliased
1928 * Make sure directories have their VM object now rather then later,
1929 * saving us from having to check on all the myrid directory VOPs
1930 * that might be executed without a VOP_OPEN being performed.
1933 ufs_vinit(struct mount *mntp, struct vnode **vpp)
1942 vp->v_type = IFTOVT(ip->i_mode);
1944 switch(vp->v_type) {
1947 vp->v_ops = &mntp->mnt_vn_spec_ops;
1948 addaliasu(vp, umajor(ip->i_rdev), uminor(ip->i_rdev));
1951 vp->v_ops = &mntp->mnt_vn_fifo_ops;
1955 vinitvmio(vp, ip->i_size,
1956 blkoffsize(ip->i_fs, ip, ip->i_size),
1957 blkoff(ip->i_fs, ip->i_size));
1960 if (ip->i_size >= vp->v_mount->mnt_maxsymlinklen) {
1961 vinitvmio(vp, ip->i_size,
1962 blkoffsize(ip->i_fs, ip, ip->i_size),
1963 blkoff(ip->i_fs, ip->i_size));
1971 if (ip->i_number == ROOTINO)
1972 vsetflags(vp, VROOT);
1974 * Initialize modrev times
1976 getmicrouptime(&tv);
1977 SETHIGH(ip->i_modrev, tv.tv_sec);
1978 SETLOW(ip->i_modrev, tv.tv_usec * 4294);
1984 * Allocate a new inode.
1988 ufs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
1989 struct componentname *cnp)
1991 struct inode *ip, *pdir;
1992 struct direct newdir;
1998 if ((mode & IFMT) == 0)
2001 error = ffs_valloc(dvp, mode, cnp->cn_cred, &tvp);
2005 ip->i_flags = pdir->i_flags & (SF_NOHISTORY|UF_NOHISTORY|UF_NODUMP);
2006 ip->i_gid = pdir->i_gid;
2010 struct ucred ucred, *ucp;
2014 * If we are not the owner of the directory,
2015 * and we are hacking owners here, (only do this where told to)
2016 * and we are not giving it TO root, (would subvert quotas)
2017 * then go ahead and give it to the other user.
2018 * Note that this drops off the execute bits for security.
2020 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2021 (pdir->i_mode & ISUID) &&
2022 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2023 ip->i_uid = pdir->i_uid;
2027 * Make sure the correct user gets charged
2029 * Quickly knock up a dummy credential for the victim.
2030 * XXX This seems to never be accessed out of our
2031 * context so a stack variable is ok.
2034 ucred.cr_uid = ip->i_uid;
2035 ucred.cr_ngroups = 1;
2036 ucred.cr_groups[0] = pdir->i_gid;
2040 ip->i_uid = cnp->cn_cred->cr_uid;
2043 if ((error = ufs_getinoquota(ip)) ||
2044 (error = ufs_chkiq(ip, 1, ucp, 0))) {
2045 ffs_vfree(tvp, ip->i_number, mode);
2051 #else /* !SUIDDIR */
2052 ip->i_uid = cnp->cn_cred->cr_uid;
2054 if ((error = ufs_getinoquota(ip)) ||
2055 (error = ufs_chkiq(ip, 1, cnp->cn_cred, 0))) {
2056 ffs_vfree(tvp, ip->i_number, mode);
2061 #endif /* !SUIDDIR */
2062 ip->i_din.di_spare[0] = 0;
2063 ip->i_din.di_spare[1] = 0;
2064 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2066 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2069 if (DOINGSOFTDEP(tvp))
2070 softdep_change_linkcnt(ip);
2071 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2072 priv_check_cred(cnp->cn_cred, PRIV_VFS_SETGID, 0)) {
2073 ip->i_mode &= ~ISGID;
2076 if (cnp->cn_flags & CNP_ISWHITEOUT)
2077 ip->i_flags |= UF_OPAQUE;
2080 * Regular files and directories need VM objects. Softlinks do
2081 * not (not immediately anyway).
2083 if (tvp->v_type == VREG || tvp->v_type == VDIR)
2084 vinitvmio(tvp, 0, PAGE_SIZE, -1);
2087 * Make sure inode goes to disk before directory entry.
2089 error = ffs_update(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2092 ufs_makedirentry(ip, cnp, &newdir);
2093 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2101 * Write error occurred trying to update the inode
2102 * or the directory so must deallocate the inode.
2106 ip->i_flag |= IN_CHANGE;
2107 if (DOINGSOFTDEP(tvp))
2108 softdep_change_linkcnt(ip);
2114 ufs_missingop(struct vop_generic_args *ap)
2116 panic("no vop function for %s in ufs child", ap->a_desc->sd_name);
2117 return (EOPNOTSUPP);
2120 static struct filterops ufsread_filtops =
2121 { FILTEROP_ISFD, NULL, filt_ufsdetach, filt_ufsread };
2122 static struct filterops ufswrite_filtops =
2123 { FILTEROP_ISFD, NULL, filt_ufsdetach, filt_ufswrite };
2124 static struct filterops ufsvnode_filtops =
2125 { FILTEROP_ISFD, NULL, filt_ufsdetach, filt_ufsvnode };
2128 * ufs_kqfilter(struct vnode *a_vp, struct knote *a_kn)
2131 ufs_kqfilter(struct vop_kqfilter_args *ap)
2133 struct vnode *vp = ap->a_vp;
2134 struct knote *kn = ap->a_kn;
2136 switch (kn->kn_filter) {
2138 kn->kn_fop = &ufsread_filtops;
2141 kn->kn_fop = &ufswrite_filtops;
2144 kn->kn_fop = &ufsvnode_filtops;
2147 return (EOPNOTSUPP);
2150 kn->kn_hook = (caddr_t)vp;
2152 /* XXX: kq token actually protects the list */
2153 lwkt_gettoken(&vp->v_token);
2154 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2155 lwkt_reltoken(&vp->v_token);
2161 filt_ufsdetach(struct knote *kn)
2163 struct vnode *vp = (struct vnode *)kn->kn_hook;
2165 lwkt_gettoken(&vp->v_token);
2166 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2167 lwkt_reltoken(&vp->v_token);
2172 filt_ufsread(struct knote *kn, long hint)
2174 struct vnode *vp = (struct vnode *)kn->kn_hook;
2175 struct inode *ip = VTOI(vp);
2179 * filesystem is gone, so set the EOF flag and schedule
2180 * the knote for deletion.
2182 if (hint == NOTE_REVOKE) {
2183 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2187 off = ip->i_size - kn->kn_fp->f_offset;
2188 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2189 if (kn->kn_sfflags & NOTE_OLDAPI)
2191 return (kn->kn_data != 0);
2196 filt_ufswrite(struct knote *kn, long hint)
2199 * filesystem is gone, so set the EOF flag and schedule
2200 * the knote for deletion.
2202 if (hint == NOTE_REVOKE)
2203 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2210 filt_ufsvnode(struct knote *kn, long hint)
2212 if (kn->kn_sfflags & hint)
2213 kn->kn_fflags |= hint;
2214 if (hint == NOTE_REVOKE) {
2215 kn->kn_flags |= (EV_EOF | EV_NODATA);
2218 return (kn->kn_fflags != 0);
2221 /* Global vfs data structures for ufs. */
2222 static struct vop_ops ufs_vnode_vops = {
2223 .vop_default = vop_defaultop,
2224 .vop_fsync = (void *)ufs_missingop,
2225 .vop_read = (void *)ufs_missingop,
2226 .vop_reallocblks = (void *)ufs_missingop,
2227 .vop_write = (void *)ufs_missingop,
2228 .vop_access = ufs_access,
2229 .vop_advlock = ufs_advlock,
2230 .vop_bmap = ufs_bmap,
2231 .vop_old_lookup = ufs_lookup,
2232 .vop_close = ufs_close,
2233 .vop_old_create = ufs_create,
2234 .vop_getattr = ufs_getattr,
2235 .vop_inactive = ufs_inactive,
2236 .vop_old_link = ufs_link,
2237 .vop_old_mkdir = ufs_mkdir,
2238 .vop_old_mknod = ufs_mknod,
2239 .vop_mmap = ufs_mmap,
2240 .vop_open = vop_stdopen,
2241 .vop_pathconf = vop_stdpathconf,
2242 .vop_kqfilter = ufs_kqfilter,
2243 .vop_print = ufs_print,
2244 .vop_readdir = ufs_readdir,
2245 .vop_readlink = ufs_readlink,
2246 .vop_reclaim = ufs_reclaim,
2247 .vop_old_remove = ufs_remove,
2248 .vop_old_rename = ufs_rename,
2249 .vop_old_rmdir = ufs_rmdir,
2250 .vop_setattr = ufs_setattr,
2251 .vop_markatime = ufs_markatime,
2252 .vop_strategy = ufs_strategy,
2253 .vop_old_symlink = ufs_symlink,
2254 .vop_old_whiteout = ufs_whiteout
2257 static struct vop_ops ufs_spec_vops = {
2258 .vop_default = vop_defaultop,
2259 .vop_fsync = (void *)ufs_missingop,
2260 .vop_access = ufs_access,
2261 .vop_close = ufs_close,
2262 .vop_getattr = ufs_getattr,
2263 .vop_inactive = ufs_inactive,
2264 .vop_print = ufs_print,
2265 .vop_read = vop_stdnoread,
2266 .vop_reclaim = ufs_reclaim,
2267 .vop_setattr = ufs_setattr,
2268 .vop_markatime = ufs_markatime,
2269 .vop_write = vop_stdnowrite
2272 static struct vop_ops ufs_fifo_vops = {
2273 .vop_default = fifo_vnoperate,
2274 .vop_fsync = (void *)ufs_missingop,
2275 .vop_access = ufs_access,
2276 .vop_close = ufsfifo_close,
2277 .vop_getattr = ufs_getattr,
2278 .vop_inactive = ufs_inactive,
2279 .vop_kqfilter = ufsfifo_kqfilter,
2280 .vop_print = ufs_print,
2281 .vop_read = ufsfifo_read,
2282 .vop_reclaim = ufs_reclaim,
2283 .vop_setattr = ufs_setattr,
2284 .vop_markatime = ufs_markatime,
2285 .vop_write = ufsfifo_write
2288 VNODEOP_SET(ufs_vnode_vops);
2289 VNODEOP_SET(ufs_spec_vops);
2290 VNODEOP_SET(ufs_fifo_vops);
2296 ufs_vnoperate(struct vop_generic_args *ap)
2298 return (VOCALL(&ufs_vnode_vops, ap));
2302 * ufs_vnoperatefifo()
2305 ufs_vnoperatefifo(struct vop_generic_args *ap)
2307 return (VOCALL(&ufs_fifo_vops, ap));
2311 * ufs_vnoperatespec()
2314 ufs_vnoperatespec(struct vop_generic_args *ap)
2316 return (VOCALL(&ufs_spec_vops, ap));