2 * modified for EXT2FS support in Lites 1.1
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
8 * Copyright (c) 1982, 1986, 1989, 1993
9 * The Regents of the University of California. All rights reserved.
10 * (c) UNIX System Laboratories, Inc.
11 * All or some portions of this file are derived from material licensed
12 * to the University of California by American Telephone and Telegraph
13 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
14 * the permission of UNIX System Laboratories, Inc.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
45 * @(#)ext2_vnops.c 8.7 (Berkeley) 2/3/94
46 * $FreeBSD: src/sys/gnu/ext2fs/ext2_vnops.c,v 1.51.2.2 2003/01/02 17:26:18 bde Exp $
47 * $DragonFly: src/sys/vfs/gnu/ext2fs/ext2_vnops.c,v 1.43 2008/06/19 23:27:39 dillon Exp $
50 #include "opt_quota.h"
51 #include "opt_suiddir.h"
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/resourcevar.h>
56 #include <sys/kernel.h>
58 #include <sys/fcntl.h>
63 #include <sys/mount.h>
65 #include <sys/vnode.h>
66 #include <sys/dirent.h>
67 #include <sys/lockf.h>
68 #include <sys/event.h>
72 #include <sys/namei.h>
73 #include <sys/signalvar.h>
74 #include <sys/unistd.h>
77 #include <vm/vm_extern.h>
78 #include <vm/vm_zone.h>
79 #include <vm/vnode_pager.h>
82 #include <sys/thread2.h>
84 #include <vfs/fifofs/fifo.h>
89 #include "ext2mount.h"
90 #include "ext2_fs_sb.h"
92 #include "ext2_extern.h"
95 static int ext2_access (struct vop_access_args *);
96 static int ext2_advlock (struct vop_advlock_args *);
97 static int ext2_chmod (struct vnode *, int, struct ucred *);
98 static int ext2_chown (struct vnode *, uid_t, gid_t, struct ucred *);
99 static int ext2_close (struct vop_close_args *);
100 static int ext2_getattr (struct vop_getattr_args *);
101 static int ext2_makeinode (int mode, struct vnode *, struct vnode **, struct componentname *);
102 static int ext2_mmap (struct vop_mmap_args *);
103 static int ext2_open (struct vop_open_args *);
104 static int ext2_pathconf (struct vop_pathconf_args *);
105 static int ext2_print (struct vop_print_args *);
106 static int ext2_readlink (struct vop_readlink_args *);
107 static int ext2_setattr (struct vop_setattr_args *);
108 static int ext2_strategy (struct vop_strategy_args *);
109 static int ext2_whiteout (struct vop_old_whiteout_args *);
110 static int filt_ext2read (struct knote *kn, long hint);
111 static int filt_ext2write (struct knote *kn, long hint);
112 static int filt_ext2vnode (struct knote *kn, long hint);
113 static void filt_ext2detach (struct knote *kn);
114 static int ext2_kqfilter (struct vop_kqfilter_args *ap);
115 static int ext2spec_close (struct vop_close_args *);
116 static int ext2spec_read (struct vop_read_args *);
117 static int ext2spec_write (struct vop_write_args *);
118 static int ext2fifo_close (struct vop_close_args *);
119 static int ext2fifo_kqfilter (struct vop_kqfilter_args *);
120 static int ext2fifo_read (struct vop_read_args *);
121 static int ext2fifo_write (struct vop_write_args *);
123 static int ext2_fsync (struct vop_fsync_args *);
124 static int ext2_read (struct vop_read_args *);
125 static int ext2_write (struct vop_write_args *);
126 static int ext2_remove (struct vop_old_remove_args *);
127 static int ext2_link (struct vop_old_link_args *);
128 static int ext2_rename (struct vop_old_rename_args *);
129 static int ext2_mkdir (struct vop_old_mkdir_args *);
130 static int ext2_rmdir (struct vop_old_rmdir_args *);
131 static int ext2_create (struct vop_old_create_args *);
132 static int ext2_mknod (struct vop_old_mknod_args *);
133 static int ext2_symlink (struct vop_old_symlink_args *);
134 static int ext2_getpages (struct vop_getpages_args *);
136 #include "ext2_readwrite.c"
142 #define SETHIGH(q, h) { \
145 tmp.val[_QUAD_HIGHWORD] = (h); \
148 #define SETLOW(q, l) { \
151 tmp.val[_QUAD_LOWWORD] = (l); \
154 #define VN_KNOTE(vp, b) \
155 KNOTE(&vp->v_pollinfo.vpi_selinfo.si_note, (b))
157 #define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0)
160 * A virgin directory (no blushing please).
161 * Note that the type and namlen fields are reversed relative to ufs.
162 * Also, we don't use `struct odirtemplate', since it would just cause
163 * endianness problems.
165 static struct dirtemplate ext2_mastertemplate = {
166 0, 12, 1, EXT2_FT_DIR, ".",
167 0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
169 static struct dirtemplate ext2_omastertemplate = {
170 0, 12, 1, EXT2_FT_UNKNOWN, ".",
171 0, DIRBLKSIZ - 12, 2, EXT2_FT_UNKNOWN, ".."
175 * Create a regular file
177 * ext2_create(struct vnode *a_dvp, struct vnode **a_vpp,
178 * struct componentname *a_cnp, struct vattr *a_vap)
181 ext2_create(struct vop_old_create_args *ap)
186 ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
187 ap->a_dvp, ap->a_vpp, ap->a_cnp);
194 * Synch an open file.
196 * ext2_fsync(struct vnode *a_vp, struct ucred *a_cred, int a_waitfor,
201 static int ext2_fsync_bp(struct buf *bp, void *data);
203 struct ext2_fsync_bp_info {
209 ext2_fsync(struct vop_fsync_args *ap)
211 struct ext2_fsync_bp_info info;
212 struct vnode *vp = ap->a_vp;
216 * XXX why is all this fs specific?
220 * Flush all dirty buffers associated with a vnode.
222 ext2_discard_prealloc(VTOI(vp));
227 info.waitfor = ap->a_waitfor;
228 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
229 ext2_fsync_bp, &info);
233 if (ap->a_waitfor == MNT_WAIT) {
234 while (vp->v_track_write.bk_active) {
235 vp->v_track_write.bk_waitflag = 1;
236 tsleep(&vp->v_track_write, 0, "e2fsyn", 0);
239 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
240 vprint("ext2_fsync: dirty", vp);
246 return (EXT2_UPDATE(ap->a_vp, ap->a_waitfor == MNT_WAIT));
250 ext2_fsync_bp(struct buf *bp, void *data)
252 struct ext2_fsync_bp_info *info = data;
254 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
256 if ((bp->b_flags & B_DELWRI) == 0)
257 panic("ext2_fsync: not dirty");
262 * Wait for I/O associated with indirect blocks to complete,
263 * since there is no way to quickly wait for them below.
265 if (bp->b_vp == info->vp || info->waitfor == MNT_NOWAIT)
276 * ext2_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
277 * struct componentname *a_cnp, struct vattr *a_vap)
281 ext2_mknod(struct vop_old_mknod_args *ap)
283 struct vattr *vap = ap->a_vap;
284 struct vnode **vpp = ap->a_vpp;
289 if (vap->va_rmajor != VNOVAL &&
290 makeudev(vap->va_rmajor, vap->va_rminor) == NOUDEV) {
294 error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
295 ap->a_dvp, vpp, ap->a_cnp);
299 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
300 if (vap->va_rmajor != VNOVAL) {
302 * Want to be able to use this to make badblock
303 * inodes, so don't truncate the dev number.
305 ip->i_rdev = makeudev(vap->va_rmajor, vap->va_rminor);
308 * Remove inode, then reload it through VFS_VGET so it is
309 * checked to see if it is an alias of an existing entry in
312 (*vpp)->v_type = VNON;
313 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
314 vgone_vxlocked(*vpp);
316 error = VFS_VGET(ap->a_dvp->v_mount, ino, vpp);
325 * ext2_remove(struct vnode *a_dvp, struct vnode *a_vp,
326 * struct componentname *a_cnp)
329 ext2_remove(struct vop_old_remove_args *ap)
332 struct vnode *vp = ap->a_vp;
333 struct vnode *dvp = ap->a_dvp;
337 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
338 (VTOI(dvp)->i_flags & APPEND)) {
342 error = ext2_dirremove(dvp, ap->a_cnp);
345 ip->i_flag |= IN_CHANGE;
354 * ext2_link(struct vnode *a_tdvp, struct vnode *a_vp,
355 * struct componentname *a_cnp)
358 ext2_link(struct vop_old_link_args *ap)
360 struct vnode *vp = ap->a_vp;
361 struct vnode *tdvp = ap->a_tdvp;
362 struct componentname *cnp = ap->a_cnp;
366 if (tdvp->v_mount != vp->v_mount) {
370 if (tdvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE))) {
374 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
378 if (ip->i_flags & (IMMUTABLE | APPEND)) {
383 ip->i_flag |= IN_CHANGE;
384 error = EXT2_UPDATE(vp, 1);
386 error = ext2_direnter(ip, tdvp, cnp);
389 ip->i_flag |= IN_CHANGE;
399 * Rename system call. fdvp, fvp are ref'd. tvp, tdvp are ref'd and locked.
400 * all vp's are released and must be in an unlocked state on return.
402 * ext2_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
403 * struct componentname *a_fcnp, struct vnode *a_tdvp,
404 * struct vnode *a_tvp, struct componentname *a_tcnp)
407 ext2_rename(struct vop_old_rename_args *ap)
409 struct vnode *tvp = ap->a_tvp;
410 struct vnode *tdvp = ap->a_tdvp;
411 struct vnode *fvp = ap->a_fvp;
412 struct vnode *fdvp = ap->a_fdvp;
413 struct componentname *tcnp = ap->a_tcnp;
414 struct componentname *fcnp = ap->a_fcnp;
415 struct inode *ip, *xp, *dp;
416 struct dirtemplate dirbuf;
417 int doingdirectory = 0, oldparent = 0, newparent = 0;
422 * Check for cross-device rename.
424 if ((fvp->v_mount != tdvp->v_mount) ||
425 (tvp && (fvp->v_mount != tvp->v_mount)) ||
440 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
441 (VTOI(tdvp)->i_flags & APPEND))) {
447 * Renaming a file to itself has no effect. The upper layers should
448 * not call us in that case. Temporarily just warn if they do.
455 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
459 * fvp, tvp, tdvp locked. fdvp not locked but note that fdvp may
464 if (ip->i_nlink >= LINK_MAX) {
469 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
470 || (dp->i_flags & APPEND)) {
475 if ((ip->i_mode & IFMT) == IFDIR) {
477 * Avoid ".", "..", and aliases of "." for obvious reasons.
479 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
480 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & CNP_ISDOTDOT ||
481 (ip->i_flag & IN_RENAME)) {
486 ip->i_flag |= IN_RENAME;
487 oldparent = dp->i_number;
492 * tvp is non-NULL if the target exists. fvp is still locked but
493 * we will unlock it soon. The 'bad' goto target requires dp and
494 * xp to be correctly assigned.
503 * 1) Bump link count while we're moving stuff
504 * around. If we crash somewhere before
505 * completing our work, the link count
506 * may be wrong, but correctable.
509 ip->i_flag |= IN_CHANGE;
510 if ((error = EXT2_UPDATE(fvp, 1)) != 0) {
516 * If ".." must be changed (ie the directory gets a new
517 * parent) then the source directory must not be in the
518 * directory heirarchy above the target, as this would
519 * orphan everything below the source directory. Also
520 * the user must have write permission in the source so
521 * as to be able to change "..". We must repeat the call
522 * to namei, as the parent directory is unlocked by the
523 * call to checkpath().
525 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred);
529 * tvp (if not NULL) and tdvp are locked. fvp and fdvp are not.
530 * dp and xp are set according to tdvp and tvp.
532 if (oldparent != dp->i_number)
533 newparent = dp->i_number;
534 if (doingdirectory && newparent) {
535 if (error) /* write access check above */
539 * Prepare for relookup, get rid of xp
547 * checkpath vput()'s tdvp (VTOI(dp)) on return no matter what,
548 * get an extra ref so we wind up with just an unlocked, ref'd
549 * tdvp. The 'out' target skips xp and tdvp cleanups. Our
550 * tdvp is now unlocked so we have to clean it up ourselves.
553 error = ext2_checkpath(ip, dp, tcnp->cn_cred);
554 tcnp->cn_flags |= CNP_PDIRUNLOCK;
560 * relookup no longer messes with the ref count. An unlocked
561 * tdvp must be passed and if no error occurs a locked tdvp
562 * will be returned. We have to use the out target again.
564 error = relookup(tdvp, &tvp, tcnp);
566 if (tcnp->cn_flags & CNP_PDIRUNLOCK)
574 * tdvp is locked at this point. in the RENAME case tvp may
575 * be NULL without an error, assign xp accordingly. The
576 * 'bad' target can be used again after this.
583 * 2) If target doesn't exist, link the target
584 * to the source and unlink the source.
585 * Otherwise, rewrite the target directory
586 * entry to reference the source inode and
587 * expunge the original entry's existence.
589 * tdvp and tvp are cleaned up by this code. tvp is only good if
593 if (dp->i_dev != ip->i_dev)
594 panic("ext2_rename: EXDEV");
596 * Account for ".." in new directory.
597 * When source and destination have the same
598 * parent we don't fool with the link count.
600 if (doingdirectory && newparent) {
601 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
606 dp->i_flag |= IN_CHANGE;
607 error = EXT2_UPDATE(tdvp, 1);
611 error = ext2_direnter(ip, tdvp, tcnp);
613 if (doingdirectory && newparent) {
615 dp->i_flag |= IN_CHANGE;
616 EXT2_UPDATE(tdvp, 1);
622 * manual cleanup, we can't use the bad or out target after
627 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
628 panic("ext2_rename: EXDEV");
630 * Short circuit rename(foo, foo).
632 if (xp->i_number == ip->i_number)
633 panic("ext2_rename: same file");
635 * If the parent directory is "sticky", then the user must
636 * own the parent directory, or the destination of the rename,
637 * otherwise the destination may not be changed (except by
638 * root). This implements append-only directories.
640 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
641 tcnp->cn_cred->cr_uid != dp->i_uid &&
642 xp->i_uid != tcnp->cn_cred->cr_uid) {
647 * Target must be empty if a directory and have no links
648 * to it. Also, ensure source and target are compatible
649 * (both directories, or both not directories).
651 if ((xp->i_mode&IFMT) == IFDIR) {
652 if (! ext2_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
657 if (!doingdirectory) {
661 } else if (doingdirectory) {
665 error = ext2_dirrewrite(dp, ip, tcnp);
669 * If the target directory is in the same
670 * directory as the source directory,
671 * decrement the link count on the parent
672 * of the target directory.
674 if (doingdirectory && !newparent) {
676 dp->i_flag |= IN_CHANGE;
680 * manual cleanup, we can't use the bad or out target after
686 * Adjust the link count of the target to
687 * reflect the dirrewrite above. If this is
688 * a directory it is empty and there are
689 * no links to it, so we can squash the inode and
690 * any space associated with it. We disallowed
691 * renaming over top of a directory with links to
692 * it above, as the remaining link would point to
693 * a directory without "." or ".." entries.
696 if (doingdirectory) {
697 if (--xp->i_nlink != 0)
698 panic("ext2_rename: linked directory");
699 error = EXT2_TRUNCATE(tvp, (off_t)0, IO_SYNC,
702 xp->i_flag |= IN_CHANGE;
708 * tvp and tdvp have been cleaned up. The bad and out targets may
709 * not be used. fvp and fdvp are ref'd but not locked. ip
710 * still represents the old fvp and ip->i_flag may still have IN_RENAME
711 * set (if doingdirectory).
715 * 3) Unlink the source.
717 * fdvp is locked and ref'd. ap->a_fvp holds the old lookup unlocked
718 * and ref'd, fvp will hold the new lookup locked and ref'd.
720 * After the relookup ap->a_fvp must be released as part of our
721 * cleanup, not just fdvp and fvp. And, on success, fdvp and
722 * fvp will be locked so the bad and out targets cannot be used.
724 fcnp->cn_flags &= ~CNP_MODMASK;
725 fcnp->cn_flags |= CNP_LOCKPARENT;
726 KKASSERT(fcnp->cn_flags & CNP_PDIRUNLOCK);
727 error = relookup(fdvp, &fvp, fcnp);
730 * From name has disappeared.
733 panic("ext2_rename: lost dir entry");
734 /* ip->i_flag only sets IN_RENAME if doingdirectory */
736 if (fcnp->cn_flags & CNP_PDIRUNLOCK)
742 KKASSERT((fcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
745 * This case shouldn't occur
749 * From name has disappeared.
752 panic("ext2_rename: lost dir entry");
753 /* ip->i_flag only sets IN_RENAME if doingdirectory */
761 * fvp and fdvp are both ref'd and locked.
767 * Ensure that the directory entry still exists and has not
768 * changed while the new name has been entered. If the source is
769 * a file then the entry may have been unlinked or renamed. In
770 * either case there is no further work to be done. If the source
771 * is a directory then it cannot have been rmdir'ed; its link
772 * count of three would cause a rmdir to fail with ENOTEMPTY.
773 * The IN_RENAME flag ensures that it cannot be moved by another
778 panic("ext2_rename: lost dir entry");
779 /* ip->i_flag only sets IN_RENAME if doingdirectory */
782 * If the source is a directory with a
783 * new parent, the link count of the old
784 * parent directory must be decremented
785 * and ".." set to point to the new parent.
787 if (doingdirectory && newparent) {
789 dp->i_flag |= IN_CHANGE;
790 error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
791 sizeof (struct dirtemplate), (off_t)0,
792 UIO_SYSSPACE, IO_NODELOCKED,
793 tcnp->cn_cred, NULL);
795 /* Like ext2 little-endian: */
796 namlen = dirbuf.dotdot_type;
798 dirbuf.dotdot_name[0] != '.' ||
799 dirbuf.dotdot_name[1] != '.') {
800 ext2_dirbad(xp, (doff_t)12,
801 "rename: mangled dir");
803 dirbuf.dotdot_ino = newparent;
804 vn_rdwr(UIO_WRITE, fvp,
806 sizeof (struct dirtemplate),
807 (off_t)0, UIO_SYSSPACE,
808 IO_NODELOCKED|IO_SYNC,
809 tcnp->cn_cred, NULL);
813 error = ext2_dirremove(fdvp, fcnp);
816 xp->i_flag |= IN_CHANGE;
818 xp->i_flag &= ~IN_RENAME;
832 ip->i_flag &= ~IN_RENAME;
833 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
835 ip->i_flag |= IN_CHANGE;
836 ip->i_flag &= ~IN_RENAME;
847 * ext2_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
848 * struct componentname *a_cnp, struct vattr *a_vap)
851 ext2_mkdir(struct vop_old_mkdir_args *ap)
853 struct vnode *dvp = ap->a_dvp;
854 struct vattr *vap = ap->a_vap;
855 struct componentname *cnp = ap->a_cnp;
856 struct inode *ip, *dp;
858 struct dirtemplate dirtemplate, *dtp;
862 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
866 dmode = vap->va_mode & 0777;
869 * Must simulate part of ext2_makeinode here to acquire the inode,
870 * but not have it entered in the parent directory. The entry is
871 * made later after writing "." and ".." entries.
873 error = EXT2_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
877 ip->i_gid = dp->i_gid;
881 struct ucred ucred, *ucp;
885 * if we are hacking owners here, (only do this where told to)
886 * and we are not giving it TOO root, (would subvert quotas)
887 * then go ahead and give it to the other user.
888 * The new directory also inherits the SUID bit.
889 * If user's UID and dir UID are the same,
890 * 'give it away' so that the SUID is still forced on.
892 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
893 (dp->i_mode & ISUID) && dp->i_uid) {
895 ip->i_uid = dp->i_uid;
897 if (dp->i_uid != cnp->cn_cred->cr_uid) {
899 * make sure the correct user gets charged
901 * Make a dummy credential for the victim.
902 * XXX This seems to never be accessed out of
903 * our context so a stack variable is ok.
906 ucred.cr_uid = ip->i_uid;
907 ucred.cr_ngroups = 1;
908 ucred.cr_groups[0] = dp->i_gid;
913 ip->i_uid = cnp->cn_cred->cr_uid;
916 if ((error = ext2_getinoquota(ip)) ||
917 (error = ext2_chkiq(ip, 1, ucp, 0))) {
918 EXT2_VFREE(tvp, ip->i_number, dmode);
925 ip->i_uid = cnp->cn_cred->cr_uid;
927 if ((error = ext2_getinoquota(ip)) ||
928 (error = ext2_chkiq(ip, 1, cnp->cn_cred, 0))) {
929 EXT2_VFREE(tvp, ip->i_number, dmode);
935 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
937 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
939 if (cnp->cn_flags & CNP_ISWHITEOUT)
940 ip->i_flags |= UF_OPAQUE;
941 error = EXT2_UPDATE(tvp, 1);
944 * The vnode must have a VM object in order to issue buffer cache
950 * Bump link count in parent directory
951 * to reflect work done below. Should
952 * be done before reference is created
953 * so reparation is possible if we crash.
956 dp->i_flag |= IN_CHANGE;
957 error = EXT2_UPDATE(dvp, 1);
961 /* Initialize directory with "." and ".." from static template. */
962 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs->s_es,
963 EXT2_FEATURE_INCOMPAT_FILETYPE))
964 dtp = &ext2_mastertemplate;
966 dtp = &ext2_omastertemplate;
968 dirtemplate.dot_ino = ip->i_number;
969 dirtemplate.dotdot_ino = dp->i_number;
970 /* note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE
971 * so let's just redefine it - for this function only
974 #define DIRBLKSIZ VTOI(dvp)->i_e2fs->s_blocksize
975 dirtemplate.dotdot_reclen = DIRBLKSIZ - 12;
976 error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
977 sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
978 IO_NODELOCKED|IO_SYNC, cnp->cn_cred, NULL);
981 dp->i_flag |= IN_CHANGE;
984 if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
985 panic("ext2_mkdir: blksize"); /* XXX should grow with balloc() */
987 ip->i_size = DIRBLKSIZ;
988 ip->i_flag |= IN_CHANGE;
991 /* Directory set up, now install its entry in the parent directory. */
992 error = ext2_direnter(ip, dvp, cnp);
995 dp->i_flag |= IN_CHANGE;
999 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1000 * for us because we set the link count to 0.
1004 ip->i_flag |= IN_CHANGE;
1011 #define DIRBLKSIZ DEV_BSIZE
1015 * Rmdir system call.
1017 * ext2_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1018 * struct componentname *a_cnp)
1021 ext2_rmdir(struct vop_old_rmdir_args *ap)
1023 struct vnode *vp = ap->a_vp;
1024 struct vnode *dvp = ap->a_dvp;
1025 struct componentname *cnp = ap->a_cnp;
1026 struct inode *ip, *dp;
1033 * Verify the directory is empty (and valid).
1034 * (Rmdir ".." won't be valid since
1035 * ".." will contain a reference to
1036 * the current directory and thus be
1040 if (ip->i_nlink != 2 || !ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1044 if ((dp->i_flags & APPEND)
1045 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1050 * Delete reference to directory before purging
1051 * inode. If we crash in between, the directory
1052 * will be reattached to lost+found,
1054 error = ext2_dirremove(dvp, cnp);
1058 dp->i_flag |= IN_CHANGE;
1061 * Truncate inode. The only stuff left
1062 * in the directory is "." and "..". The
1063 * "." reference is inconsequential since
1064 * we're quashing it. The ".." reference
1065 * has already been adjusted above. We've
1066 * removed the "." reference and the reference
1067 * in the parent directory, but there may be
1068 * other hard links so decrement by 2 and
1069 * worry about them later.
1072 error = EXT2_TRUNCATE(vp, (off_t)0, IO_SYNC, cnp->cn_cred);
1073 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1079 * symlink -- make a symbolic link
1081 * ext2_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1082 * struct componentname *a_cnp, struct vattr *a_vap,
1086 ext2_symlink(struct vop_old_symlink_args *ap)
1088 struct vnode *vp, **vpp = ap->a_vpp;
1092 error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1097 len = strlen(ap->a_target);
1098 if (len < vp->v_mount->mnt_maxsymlinklen) {
1100 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1102 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1105 * Make sure we have a VM object in order to use
1108 if (vp->v_object == NULL)
1111 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1112 UIO_SYSSPACE, IO_NODELOCKED,
1113 ap->a_cnp->cn_cred, NULL);
1123 * Allocate a new inode.
1126 ext2_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
1127 struct componentname *cnp)
1129 struct inode *ip, *pdir;
1135 if ((mode & IFMT) == 0)
1138 error = EXT2_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
1143 ip->i_gid = pdir->i_gid;
1147 struct ucred ucred, *ucp;
1152 * not the owner of the directory,
1153 * and we are hacking owners here, (only do this where told to)
1154 * and we are not giving it TOO root, (would subvert quotas)
1155 * then go ahead and give it to the other user.
1156 * Note that this drops off the execute bits for security.
1158 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1159 (pdir->i_mode & ISUID) &&
1160 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1161 ip->i_uid = pdir->i_uid;
1165 * make sure the correct user gets charged
1167 * Quickly knock up a dummy credential for the victim.
1168 * XXX This seems to never be accessed out of our
1169 * context so a stack variable is ok.
1172 ucred.cr_uid = ip->i_uid;
1173 ucred.cr_ngroups = 1;
1174 ucred.cr_groups[0] = pdir->i_gid;
1178 ip->i_uid = cnp->cn_cred->cr_uid;
1182 if ((error = ext2_getinoquota(ip)) ||
1183 (error = ext2_chkiq(ip, 1, ucp, 0))) {
1184 EXT2_VFREE(tvp, ip->i_number, mode);
1191 ip->i_uid = cnp->cn_cred->cr_uid;
1193 if ((error = ext2_getinoquota(ip)) ||
1194 (error = ext2_chkiq(ip, 1, cnp->cn_cred, 0))) {
1195 EXT2_VFREE(tvp, ip->i_number, mode);
1201 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1203 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
1205 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
1206 priv_check_cred(cnp->cn_cred, PRIV_ROOT, PRISON_ROOT))
1207 ip->i_mode &= ~ISGID;
1209 if (cnp->cn_flags & CNP_ISWHITEOUT)
1210 ip->i_flags |= UF_OPAQUE;
1213 * Regular files and directories need VM objects. Softlinks do
1214 * not (not immediately anyway).
1216 if (tvp->v_type == VREG || tvp->v_type == VDIR)
1220 * Make sure inode goes to disk before directory entry.
1222 error = EXT2_UPDATE(tvp, 1);
1225 error = ext2_direnter(ip, dvp, cnp);
1234 * Write error occurred trying to update the inode
1235 * or the directory so must deallocate the inode.
1238 ip->i_flag |= IN_CHANGE;
1246 * XXX By default, wimp out... note that a_offset is ignored (and always
1250 ext2_getpages(struct vop_getpages_args *ap)
1252 return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
1257 ext2_itimes(struct vnode *vp)
1263 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
1265 if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
1266 ip->i_flag |= IN_LAZYMOD;
1268 ip->i_flag |= IN_MODIFIED;
1269 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1271 if (ip->i_flag & IN_ACCESS) {
1272 ip->i_atime = ts.tv_sec;
1273 ip->i_atimensec = ts.tv_nsec;
1275 if (ip->i_flag & IN_UPDATE) {
1276 ip->i_mtime = ts.tv_sec;
1277 ip->i_mtimensec = ts.tv_nsec;
1280 if (ip->i_flag & IN_CHANGE) {
1281 ip->i_ctime = ts.tv_sec;
1282 ip->i_ctimensec = ts.tv_nsec;
1285 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
1293 * ext2_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
1294 * struct file *a_fp)
1299 ext2_open(struct vop_open_args *ap)
1301 struct vnode *vp = ap->a_vp;
1304 * Files marked append-only must be opened for appending.
1306 if ((VTOI(vp)->i_flags & APPEND) &&
1307 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE) {
1310 return (vop_stdopen(ap));
1316 * Update the times on the inode.
1318 * ext2_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred)
1323 ext2_close(struct vop_close_args *ap)
1325 struct vnode *vp = ap->a_vp;
1327 if (vp->v_sysref.refcnt > 1)
1329 return (vop_stdclose(ap));
1333 * ext2_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
1337 ext2_access(struct vop_access_args *ap)
1339 struct vnode *vp = ap->a_vp;
1340 struct inode *ip = VTOI(vp);
1341 struct ucred *cred = ap->a_cred;
1342 mode_t mask, mode = ap->a_mode;
1350 * Disallow write attempts on read-only filesystems;
1351 * unless the file is a socket, fifo, or a block or
1352 * character device resident on the filesystem.
1354 if (mode & VWRITE) {
1355 switch (vp->v_type) {
1359 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1362 if ((error = ext2_getinoquota(ip)) != 0)
1371 /* If immutable bit set, nobody gets to write it. */
1372 if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE))
1375 /* Otherwise, user id 0 always gets access. */
1376 if (cred->cr_uid == 0)
1381 /* Otherwise, check the owner. */
1382 if (cred->cr_uid == ip->i_uid) {
1389 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
1392 /* Otherwise, check the groups. */
1393 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
1394 if (ip->i_gid == *gp) {
1401 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
1404 /* Otherwise, check everyone else. */
1411 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
1415 * ext2_getattr(struct vnode *a_vp, struct vattr *a_vap)
1420 ext2_getattr(struct vop_getattr_args *ap)
1422 struct vnode *vp = ap->a_vp;
1423 struct inode *ip = VTOI(vp);
1424 struct vattr *vap = ap->a_vap;
1428 * Copy from inode table
1430 vap->va_fsid = dev2udev(ip->i_dev);
1431 vap->va_fileid = ip->i_number;
1432 vap->va_mode = ip->i_mode & ~IFMT;
1433 vap->va_nlink = VFSTOEXT2(vp->v_mount)->um_i_effnlink_valid ?
1434 ip->i_effnlink : ip->i_nlink;
1435 vap->va_uid = ip->i_uid;
1436 vap->va_gid = ip->i_gid;
1437 vap->va_rmajor = umajor(ip->i_rdev);
1438 vap->va_rminor = uminor(ip->i_rdev);
1439 vap->va_size = ip->i_din.di_size;
1440 vap->va_atime.tv_sec = ip->i_atime;
1441 vap->va_atime.tv_nsec = ip->i_atimensec;
1442 vap->va_mtime.tv_sec = ip->i_mtime;
1443 vap->va_mtime.tv_nsec = ip->i_mtimensec;
1444 vap->va_ctime.tv_sec = ip->i_ctime;
1445 vap->va_ctime.tv_nsec = ip->i_ctimensec;
1446 vap->va_flags = ip->i_flags;
1447 vap->va_gen = ip->i_gen;
1448 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
1449 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
1450 vap->va_type = IFTOVT(ip->i_mode);
1451 vap->va_filerev = ip->i_modrev;
1456 * Set attribute vnode op. called from several syscalls
1458 * ext2_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
1462 ext2_setattr(struct vop_setattr_args *ap)
1464 struct vattr *vap = ap->a_vap;
1465 struct vnode *vp = ap->a_vp;
1466 struct inode *ip = VTOI(vp);
1467 struct ucred *cred = ap->a_cred;
1471 * Check for unsettable attributes.
1473 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
1474 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
1475 (vap->va_blocksize != VNOVAL) || (vap->va_rmajor != VNOVAL) ||
1476 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
1479 if (vap->va_flags != VNOVAL) {
1480 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1482 if (cred->cr_uid != ip->i_uid &&
1483 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)))
1486 * Note that a root chflags becomes a user chflags when
1487 * we are jailed, unless the jail.chflags_allowed sysctl
1490 if (cred->cr_uid == 0 &&
1491 (!jailed(cred) || jail_chflags_allowed)) {
1493 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) &&
1496 ip->i_flags = vap->va_flags;
1499 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
1500 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
1502 ip->i_flags &= SF_SETTABLE;
1503 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
1505 ip->i_flag |= IN_CHANGE;
1506 if (vap->va_flags & (IMMUTABLE | APPEND))
1509 if (ip->i_flags & (IMMUTABLE | APPEND))
1512 * Go through the fields and update iff not VNOVAL.
1514 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
1515 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1517 if ((error = ext2_chown(vp, vap->va_uid, vap->va_gid, cred)) != 0)
1520 if (vap->va_size != VNOVAL) {
1522 * Disallow write attempts on read-only filesystems;
1523 * unless the file is a socket, fifo, or a block or
1524 * character device resident on the filesystem.
1526 switch (vp->v_type) {
1531 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1537 if ((error = EXT2_TRUNCATE(vp, vap->va_size, 0, cred)) != 0)
1541 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1542 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1544 if (cred->cr_uid != ip->i_uid &&
1545 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)) &&
1546 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
1547 (error = VOP_ACCESS(vp, VWRITE, cred))))
1549 if (vap->va_atime.tv_sec != VNOVAL)
1550 ip->i_flag |= IN_ACCESS;
1551 if (vap->va_mtime.tv_sec != VNOVAL)
1552 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1554 if (vap->va_atime.tv_sec != VNOVAL) {
1555 ip->i_atime = vap->va_atime.tv_sec;
1556 ip->i_atimensec = vap->va_atime.tv_nsec;
1558 if (vap->va_mtime.tv_sec != VNOVAL) {
1559 ip->i_mtime = vap->va_mtime.tv_sec;
1560 ip->i_mtimensec = vap->va_mtime.tv_nsec;
1562 error = EXT2_UPDATE(vp, 0);
1567 if (vap->va_mode != (mode_t)VNOVAL) {
1568 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1570 error = ext2_chmod(vp, (int)vap->va_mode, cred);
1572 VN_KNOTE(vp, NOTE_ATTRIB);
1577 * Change the mode on a file.
1578 * Inode must be locked before calling.
1581 ext2_chmod(struct vnode *vp, int mode, struct ucred *cred)
1583 struct inode *ip = VTOI(vp);
1586 if (cred->cr_uid != ip->i_uid) {
1587 error = priv_check_cred(cred, PRIV_ROOT, PRISON_ROOT);
1592 if (vp->v_type != VDIR && (mode & S_ISTXT))
1594 if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
1597 ip->i_mode &= ~ALLPERMS;
1598 ip->i_mode |= (mode & ALLPERMS);
1599 ip->i_flag |= IN_CHANGE;
1604 * Perform chown operation on inode ip;
1605 * inode must be locked prior to call.
1608 ext2_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred)
1610 struct inode *ip = VTOI(vp);
1619 if (uid == (uid_t)VNOVAL)
1621 if (gid == (gid_t)VNOVAL)
1624 * If we don't own the file, are trying to change the owner
1625 * of the file, or are not a member of the target group,
1626 * the caller must be superuser or the call fails.
1628 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
1629 (gid != ip->i_gid && !(cred->cr_gid == gid ||
1630 groupmember((gid_t)gid, cred)))) &&
1631 (error = priv_check_cred(cred, PRIV_ROOT, PRISON_ROOT)))
1636 if ((error = ext2_getinoquota(ip)) != 0)
1639 ext2_dqrele(vp, ip->i_dquot[USRQUOTA]);
1640 ip->i_dquot[USRQUOTA] = NODQUOT;
1643 ext2_dqrele(vp, ip->i_dquot[GRPQUOTA]);
1644 ip->i_dquot[GRPQUOTA] = NODQUOT;
1646 change = ip->i_blocks;
1647 (void) ext2_chkdq(ip, -change, cred, CHOWN);
1648 (void) ext2_chkiq(ip, -1, cred, CHOWN);
1649 for (i = 0; i < MAXQUOTAS; i++) {
1650 ext2_dqrele(vp, ip->i_dquot[i]);
1651 ip->i_dquot[i] = NODQUOT;
1657 if ((error = ext2_getinoquota(ip)) == 0) {
1659 ext2_dqrele(vp, ip->i_dquot[USRQUOTA]);
1660 ip->i_dquot[USRQUOTA] = NODQUOT;
1663 ext2_dqrele(vp, ip->i_dquot[GRPQUOTA]);
1664 ip->i_dquot[GRPQUOTA] = NODQUOT;
1666 if ((error = ext2_chkdq(ip, change, cred, CHOWN)) == 0) {
1667 if ((error = ext2_chkiq(ip, 1, cred, CHOWN)) == 0)
1670 (void)ext2_chkdq(ip, -change, cred, CHOWN|FORCE);
1672 for (i = 0; i < MAXQUOTAS; i++) {
1673 ext2_dqrele(vp, ip->i_dquot[i]);
1674 ip->i_dquot[i] = NODQUOT;
1679 if (ext2_getinoquota(ip) == 0) {
1681 ext2_dqrele(vp, ip->i_dquot[USRQUOTA]);
1682 ip->i_dquot[USRQUOTA] = NODQUOT;
1685 ext2_dqrele(vp, ip->i_dquot[GRPQUOTA]);
1686 ip->i_dquot[GRPQUOTA] = NODQUOT;
1688 (void) ext2_chkdq(ip, change, cred, FORCE|CHOWN);
1689 (void) ext2_chkiq(ip, 1, cred, FORCE|CHOWN);
1690 (void) ext2_getinoquota(ip);
1694 if (ext2_getinoquota(ip))
1695 panic("ext2_chown: lost quota");
1697 ip->i_flag |= IN_CHANGE;
1698 if (cred->cr_uid != 0 && (ouid != uid || ogid != gid))
1699 ip->i_mode &= ~(ISUID | ISGID);
1706 * NB Currently unsupported.
1708 * ext2_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
1713 ext2_mmap(struct vop_mmap_args *ap)
1719 * whiteout vnode call
1721 * ext2_whiteout(struct vnode *a_dvp, struct componentname *a_cnp, int a_flags)
1725 ext2_whiteout(struct vop_old_whiteout_args *ap)
1727 return (EOPNOTSUPP);
1731 * Return target name of a symbolic link
1733 * ext2_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1737 ext2_readlink(struct vop_readlink_args *ap)
1739 struct vnode *vp = ap->a_vp;
1740 struct inode *ip = VTOI(vp);
1744 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1745 (ip->i_din.di_blocks == 0)) { /* XXX - for old fastlink support */
1746 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1749 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1753 * Calculate the logical to physical mapping if not done already,
1754 * then call the device strategy routine.
1756 * In order to be able to swap to a file, the VOP_BMAP operation may not
1757 * deadlock on memory. See ext2_bmap() for details.
1759 * ext2_strategy(struct vnode *a_vp, struct bio *a_bio)
1763 ext2_strategy(struct vop_strategy_args *ap)
1765 struct bio *bio = ap->a_bio;
1767 struct buf *bp = bio->bio_buf;
1768 struct vnode *vp = ap->a_vp;
1773 if (vp->v_type == VBLK || vp->v_type == VCHR)
1774 panic("ext2_strategy: spec");
1775 nbio = push_bio(bio);
1776 if (nbio->bio_offset == NOOFFSET) {
1777 error = VOP_BMAP(vp, bio->bio_offset, &nbio->bio_offset,
1778 NULL, NULL, bp->b_cmd);
1780 bp->b_error = error;
1781 bp->b_flags |= B_ERROR;
1782 /* I/O was never started on nbio, must biodone(bio) */
1786 if (nbio->bio_offset == NOOFFSET)
1789 if (nbio->bio_offset == NOOFFSET) {
1790 /* I/O was never started on nbio, must biodone(bio) */
1794 vn_strategy(ip->i_devvp, nbio);
1799 * Print out the contents of an inode.
1801 * ext2_print(struct vnode *a_vp)
1805 ext2_print(struct vop_print_args *ap)
1807 struct vnode *vp = ap->a_vp;
1808 struct inode *ip = VTOI(vp);
1810 kprintf("tag VT_EXT2FS, ino %lu, on dev %s (%d, %d)",
1811 (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev),
1813 if (vp->v_type == VFIFO)
1815 lockmgr_printinfo(&vp->v_lock);
1821 * Read wrapper for special devices.
1823 * ext2spec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1824 * struct ucred *a_cred)
1828 ext2spec_read(struct vop_read_args *ap)
1835 resid = uio->uio_resid;
1836 error = VOCALL(&spec_vnode_vops, &ap->a_head);
1838 * The inode may have been revoked during the call, so it must not
1839 * be accessed blindly here or in the other wrapper functions.
1841 ip = VTOI(ap->a_vp);
1842 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1843 ip->i_flag |= IN_ACCESS;
1848 * Write wrapper for special devices.
1850 * ext2spec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1851 * struct ucred *a_cred)
1855 ext2spec_write(struct vop_write_args *ap)
1862 resid = uio->uio_resid;
1863 error = VOCALL(&spec_vnode_vops, &ap->a_head);
1864 ip = VTOI(ap->a_vp);
1865 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1866 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1871 * Close wrapper for special devices.
1873 * Update the times on the inode then do device close.
1875 * ext2spec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred)
1879 ext2spec_close(struct vop_close_args *ap)
1881 struct vnode *vp = ap->a_vp;
1883 if (vp->v_sysref.refcnt > 1)
1885 return (VOCALL(&spec_vnode_vops, &ap->a_head));
1889 * Read wrapper for fifos.
1891 * ext2fifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1892 * struct ucred *a_cred)
1896 ext2fifo_read(struct vop_read_args *ap)
1903 resid = uio->uio_resid;
1904 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1905 ip = VTOI(ap->a_vp);
1906 if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL &&
1907 (uio->uio_resid != resid || (error == 0 && resid != 0)))
1908 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1913 * Write wrapper for fifos.
1915 * ext2fifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1916 * struct ucred *a_cred)
1920 ext2fifo_write(struct vop_write_args *ap)
1927 resid = uio->uio_resid;
1928 error = VOCALL(&fifo_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 fifos.
1938 * Update the times on the inode then do device close.
1940 * ext2fifo_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred)
1944 ext2fifo_close(struct vop_close_args *ap)
1946 struct vnode *vp = ap->a_vp;
1948 if (vp->v_sysref.refcnt > 1)
1950 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
1954 * Kqfilter wrapper for fifos.
1956 * Fall through to ext2 kqfilter routines if needed
1960 ext2fifo_kqfilter(struct vop_kqfilter_args *ap)
1964 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1966 error = ext2_kqfilter(ap);
1971 * Return POSIX pathconf information applicable to ext2 filesystems.
1973 * ext2_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
1977 ext2_pathconf(struct vop_pathconf_args *ap)
1979 switch (ap->a_name) {
1981 *ap->a_retval = LINK_MAX;
1984 *ap->a_retval = NAME_MAX;
1987 *ap->a_retval = PATH_MAX;
1990 *ap->a_retval = PIPE_BUF;
1992 case _PC_CHOWN_RESTRICTED:
2005 * Advisory record locking support
2007 * ext2_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
2012 ext2_advlock(struct vop_advlock_args *ap)
2014 struct inode *ip = VTOI(ap->a_vp);
2016 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
2020 * Initialize the vnode associated with a new inode, handle aliased
2024 ext2_vinit(struct mount *mntp, struct vnode **vpp)
2033 switch(vp->v_type = IFTOVT(ip->i_mode)) {
2036 vp->v_ops = &mntp->mnt_vn_spec_ops;
2037 addaliasu(vp, umajor(ip->i_rdev), uminor(ip->i_rdev));
2040 vp->v_ops = &mntp->mnt_vn_fifo_ops;
2044 vinitvmio(vp, ip->i_size);
2047 if ((ip->i_size >= vp->v_mount->mnt_maxsymlinklen) &&
2048 ip->i_din.di_blocks != 0
2050 vinitvmio(vp, ip->i_size);
2058 if (ip->i_number == ROOTINO)
2059 vp->v_flag |= VROOT;
2061 * Initialize modrev times
2063 getmicrouptime(&tv);
2064 SETHIGH(ip->i_modrev, tv.tv_sec);
2065 SETLOW(ip->i_modrev, tv.tv_usec * 4294);
2070 static struct filterops ext2read_filtops =
2071 { 1, NULL, filt_ext2detach, filt_ext2read };
2072 static struct filterops ext2write_filtops =
2073 { 1, NULL, filt_ext2detach, filt_ext2write };
2074 static struct filterops ext2vnode_filtops =
2075 { 1, NULL, filt_ext2detach, filt_ext2vnode };
2078 * ext2_kqfilter(struct vnode *a_vp, struct knote *a_kn)
2081 ext2_kqfilter(struct vop_kqfilter_args *ap)
2083 struct vnode *vp = ap->a_vp;
2084 struct knote *kn = ap->a_kn;
2087 switch (kn->kn_filter) {
2089 kn->kn_fop = &ext2read_filtops;
2092 kn->kn_fop = &ext2write_filtops;
2095 kn->kn_fop = &ext2vnode_filtops;
2101 kn->kn_hook = (caddr_t)vp;
2103 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2104 SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
2105 lwkt_reltoken(&ilock);
2111 filt_ext2detach(struct knote *kn)
2113 struct vnode *vp = (struct vnode *)kn->kn_hook;
2116 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2117 SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
2118 kn, knote, kn_selnext);
2119 lwkt_reltoken(&ilock);
2124 filt_ext2read(struct knote *kn, long hint)
2126 struct vnode *vp = (struct vnode *)kn->kn_hook;
2127 struct inode *ip = VTOI(vp);
2130 * filesystem is gone, so set the EOF flag and schedule
2131 * the knote for deletion.
2133 if (hint == NOTE_REVOKE) {
2134 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2138 kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
2139 return (kn->kn_data != 0);
2144 filt_ext2write(struct knote *kn, long hint)
2147 * filesystem is gone, so set the EOF flag and schedule
2148 * the knote for deletion.
2150 if (hint == NOTE_REVOKE)
2151 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2158 filt_ext2vnode(struct knote *kn, long hint)
2160 if (kn->kn_sfflags & hint)
2161 kn->kn_fflags |= hint;
2162 if (hint == NOTE_REVOKE) {
2163 kn->kn_flags |= EV_EOF;
2166 return (kn->kn_fflags != 0);
2169 struct vop_ops ext2_vnode_vops = {
2170 .vop_default = vop_defaultop,
2171 .vop_fsync = ext2_fsync,
2172 .vop_read = ext2_read,
2173 .vop_reallocblks = ext2_reallocblks,
2174 .vop_write = ext2_write,
2175 .vop_access = ext2_access,
2176 .vop_advlock = ext2_advlock,
2177 .vop_bmap = ext2_bmap,
2178 .vop_old_lookup = ext2_lookup,
2179 .vop_close = ext2_close,
2180 .vop_old_create = ext2_create,
2181 .vop_getattr = ext2_getattr,
2182 .vop_inactive = ext2_inactive,
2183 .vop_old_link = ext2_link,
2184 .vop_old_mkdir = ext2_mkdir,
2185 .vop_old_mknod = ext2_mknod,
2186 .vop_mmap = ext2_mmap,
2187 .vop_open = ext2_open,
2188 .vop_pathconf = ext2_pathconf,
2189 .vop_poll = vop_stdpoll,
2190 .vop_kqfilter = ext2_kqfilter,
2191 .vop_print = ext2_print,
2192 .vop_readdir = ext2_readdir,
2193 .vop_readlink = ext2_readlink,
2194 .vop_reclaim = ext2_reclaim,
2195 .vop_old_remove = ext2_remove,
2196 .vop_old_rename = ext2_rename,
2197 .vop_old_rmdir = ext2_rmdir,
2198 .vop_setattr = ext2_setattr,
2199 .vop_strategy = ext2_strategy,
2200 .vop_old_symlink = ext2_symlink,
2201 .vop_old_whiteout = ext2_whiteout,
2202 .vop_getpages = ext2_getpages,
2203 .vop_putpages = vop_stdputpages
2206 struct vop_ops ext2_spec_vops = {
2207 .vop_default = ext2_vnoperatespec,
2208 .vop_fsync = ext2_fsync,
2209 .vop_access = ext2_access,
2210 .vop_close = ext2spec_close,
2211 .vop_getattr = ext2_getattr,
2212 .vop_inactive = ext2_inactive,
2213 .vop_print = ext2_print,
2214 .vop_read = ext2spec_read,
2215 .vop_reclaim = ext2_reclaim,
2216 .vop_setattr = ext2_setattr,
2217 .vop_write = ext2spec_write
2220 struct vop_ops ext2_fifo_vops = {
2221 .vop_default = ext2_vnoperatefifo,
2222 .vop_fsync = ext2_fsync,
2223 .vop_access = ext2_access,
2224 .vop_close = ext2fifo_close,
2225 .vop_getattr = ext2_getattr,
2226 .vop_inactive = ext2_inactive,
2227 .vop_kqfilter = ext2fifo_kqfilter,
2228 .vop_print = ext2_print,
2229 .vop_read = ext2fifo_read,
2230 .vop_reclaim = ext2_reclaim,
2231 .vop_setattr = ext2_setattr,
2232 .vop_write = ext2fifo_write
2235 VNODEOP_SET(ext2_vnode_vops);
2236 VNODEOP_SET(ext2_spec_vops);
2237 VNODEOP_SET(ext2_fifo_vops);
2243 ext2_vnoperate(struct vop_generic_args *ap)
2245 return (VOCALL(&ext2_vnode_vops, ap));
2249 * ext2_vnoperatefifo()
2252 ext2_vnoperatefifo(struct vop_generic_args *ap)
2254 return (VOCALL(&ext2_fifo_vops, ap));
2258 * ext2_vnoperatespec()
2261 ext2_vnoperatespec(struct vop_generic_args *ap)
2263 return (VOCALL(&ext2_spec_vops, ap));