Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_vnops.c revision 1.112
      1 /*	$NetBSD: ext2fs_vnops.c,v 1.112 2014/05/25 13:47:22 hannken Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)ufs_vnops.c	8.14 (Berkeley) 10/26/94
     37  * Modified for ext2fs by Manuel Bouyer.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1997 Manuel Bouyer.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     53  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     54  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     55  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     56  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     57  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     61  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     62  *
     63  *	@(#)ufs_vnops.c	8.14 (Berkeley) 10/26/94
     64  * Modified for ext2fs by Manuel Bouyer.
     65  */
     66 
     67 #include <sys/cdefs.h>
     68 __KERNEL_RCSID(0, "$NetBSD: ext2fs_vnops.c,v 1.112 2014/05/25 13:47:22 hannken Exp $");
     69 
     70 #include <sys/param.h>
     71 #include <sys/systm.h>
     72 #include <sys/resourcevar.h>
     73 #include <sys/kernel.h>
     74 #include <sys/file.h>
     75 #include <sys/stat.h>
     76 #include <sys/buf.h>
     77 #include <sys/proc.h>
     78 #include <sys/mount.h>
     79 #include <sys/namei.h>
     80 #include <sys/vnode.h>
     81 #include <sys/lockf.h>
     82 #include <sys/malloc.h>
     83 #include <sys/pool.h>
     84 #include <sys/signalvar.h>
     85 #include <sys/kauth.h>
     86 
     87 #include <miscfs/fifofs/fifo.h>
     88 #include <miscfs/genfs/genfs.h>
     89 #include <miscfs/specfs/specdev.h>
     90 
     91 #include <ufs/ufs/inode.h>
     92 #include <ufs/ufs/ufs_extern.h>
     93 #include <ufs/ufs/ufsmount.h>
     94 
     95 #include <ufs/ext2fs/ext2fs.h>
     96 #include <ufs/ext2fs/ext2fs_extern.h>
     97 #include <ufs/ext2fs/ext2fs_dir.h>
     98 
     99 extern int prtactive;
    100 
    101 static int ext2fs_chmod(struct vnode *, int, kauth_cred_t, struct lwp *);
    102 static int ext2fs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t,
    103 				struct lwp *);
    104 
    105 union _qcvt {
    106 	int64_t	qcvt;
    107 	int32_t val[2];
    108 };
    109 
    110 #define SETHIGH(q, h) { \
    111 	union _qcvt tmp; \
    112 	tmp.qcvt = (q); \
    113 	tmp.val[_QUAD_HIGHWORD] = (h); \
    114 	(q) = tmp.qcvt; \
    115 }
    116 #define SETLOW(q, l) { \
    117 	union _qcvt tmp; \
    118 	tmp.qcvt = (q); \
    119 	tmp.val[_QUAD_LOWWORD] = (l); \
    120 	(q) = tmp.qcvt; \
    121 }
    122 
    123 /*
    124  * Create a regular file
    125  */
    126 int
    127 ext2fs_create(void *v)
    128 {
    129 	struct vop_create_v3_args /* {
    130 		struct vnode *a_dvp;
    131 		struct vnode **a_vpp;
    132 		struct componentname *a_cnp;
    133 		struct vattr *a_vap;
    134 	} */ *ap = v;
    135 	int	error;
    136 
    137 	error =
    138 	    ext2fs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
    139 			     ap->a_dvp, ap->a_vpp, ap->a_cnp);
    140 
    141 	if (error)
    142 		return (error);
    143 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
    144 	VOP_UNLOCK(*ap->a_vpp);
    145 	return (0);
    146 }
    147 
    148 /*
    149  * Mknod vnode call
    150  */
    151 /* ARGSUSED */
    152 int
    153 ext2fs_mknod(void *v)
    154 {
    155 	struct vop_mknod_v3_args /* {
    156 		struct vnode *a_dvp;
    157 		struct vnode **a_vpp;
    158 		struct componentname *a_cnp;
    159 		struct vattr *a_vap;
    160 	} */ *ap = v;
    161 	struct vattr *vap = ap->a_vap;
    162 	struct vnode **vpp = ap->a_vpp;
    163 	struct inode *ip;
    164 	int error;
    165 	struct mount	*mp;
    166 	ino_t		ino;
    167 
    168 	if ((error = ext2fs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
    169 		    ap->a_dvp, vpp, ap->a_cnp)) != 0)
    170 		return (error);
    171 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
    172 	ip = VTOI(*vpp);
    173 	mp  = (*vpp)->v_mount;
    174 	ino = ip->i_number;
    175 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
    176 	if (vap->va_rdev != VNOVAL) {
    177 		/*
    178 		 * Want to be able to use this to make badblock
    179 		 * inodes, so don't truncate the dev number.
    180 		 */
    181 		ip->i_din.e2fs_din->e2di_rdev = h2fs32(vap->va_rdev);
    182 	}
    183 	/*
    184 	 * Remove inode so that it will be reloaded by vcache_get and
    185 	 * checked to see if it is an alias of an existing entry in
    186 	 * the inode cache.
    187 	 */
    188 	(*vpp)->v_type = VNON;
    189 	VOP_UNLOCK(*vpp);
    190 	vgone(*vpp);
    191 	error = vcache_get(mp, &ino, sizeof(ino), vpp);
    192 	if (error != 0) {
    193 		*vpp = NULL;
    194 		return (error);
    195 	}
    196 	return (0);
    197 }
    198 
    199 /*
    200  * Open called.
    201  *
    202  * Just check the APPEND flag.
    203  */
    204 /* ARGSUSED */
    205 int
    206 ext2fs_open(void *v)
    207 {
    208 	struct vop_open_args /* {
    209 		struct vnode *a_vp;
    210 		int  a_mode;
    211 		kauth_cred_t a_cred;
    212 	} */ *ap = v;
    213 
    214 	/*
    215 	 * Files marked append-only must be opened for appending.
    216 	 */
    217 	if ((VTOI(ap->a_vp)->i_e2fs_flags & EXT2_APPEND) &&
    218 		(ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
    219 		return (EPERM);
    220 	return (0);
    221 }
    222 
    223 static int
    224 ext2fs_check_possible(struct vnode *vp, struct inode *ip, mode_t mode)
    225 {
    226 
    227 	/*
    228 	 * Disallow write attempts on read-only file systems;
    229 	 * unless the file is a socket, fifo, or a block or
    230 	 * character device resident on the file system.
    231 	 */
    232 	if (mode & VWRITE) {
    233 		switch (vp->v_type) {
    234 		case VDIR:
    235 		case VLNK:
    236 		case VREG:
    237 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
    238 				return (EROFS);
    239 			break;
    240 		default:
    241 			break;
    242 		}
    243 	}
    244 
    245 	/* If immutable bit set, nobody gets to write it. */
    246 	if ((mode & VWRITE) && (ip->i_e2fs_flags & EXT2_IMMUTABLE))
    247 		return (EPERM);
    248 
    249 	return 0;
    250 }
    251 
    252 static int
    253 ext2fs_check_permitted(struct vnode *vp, struct inode *ip, mode_t mode,
    254     kauth_cred_t cred)
    255 {
    256 
    257 	return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode, vp->v_type,
    258 	    ip->i_e2fs_mode & ALLPERMS), vp, NULL, genfs_can_access(vp->v_type,
    259 	    ip->i_e2fs_mode & ALLPERMS, ip->i_uid, ip->i_gid, mode, cred));
    260 }
    261 
    262 int
    263 ext2fs_access(void *v)
    264 {
    265 	struct vop_access_args /* {
    266 		struct vnode *a_vp;
    267 		int  a_mode;
    268 		kauth_cred_t a_cred;
    269 	} */ *ap = v;
    270 	struct vnode *vp = ap->a_vp;
    271 	struct inode *ip = VTOI(vp);
    272 	mode_t mode = ap->a_mode;
    273 	int error;
    274 
    275 	error = ext2fs_check_possible(vp, ip, mode);
    276 	if (error)
    277 		return error;
    278 
    279 	error = ext2fs_check_permitted(vp, ip, mode, ap->a_cred);
    280 
    281 	return error;
    282 }
    283 
    284 /* ARGSUSED */
    285 int
    286 ext2fs_getattr(void *v)
    287 {
    288 	struct vop_getattr_args /* {
    289 		struct vnode *a_vp;
    290 		struct vattr *a_vap;
    291 		kauth_cred_t a_cred;
    292 	} */ *ap = v;
    293 	struct vnode *vp = ap->a_vp;
    294 	struct inode *ip = VTOI(vp);
    295 	struct vattr *vap = ap->a_vap;
    296 
    297 	EXT2FS_ITIMES(ip, NULL, NULL, NULL);
    298 	/*
    299 	 * Copy from inode table
    300 	 */
    301 	vap->va_fsid = ip->i_dev;
    302 	vap->va_fileid = ip->i_number;
    303 	vap->va_mode = ip->i_e2fs_mode & ALLPERMS;
    304 	vap->va_nlink = ip->i_e2fs_nlink;
    305 	vap->va_uid = ip->i_uid;
    306 	vap->va_gid = ip->i_gid;
    307 	vap->va_rdev = (dev_t)fs2h32(ip->i_din.e2fs_din->e2di_rdev);
    308 	vap->va_size = vp->v_size;
    309 	vap->va_atime.tv_sec = ip->i_e2fs_atime;
    310 	vap->va_atime.tv_nsec = 0;
    311 	vap->va_mtime.tv_sec = ip->i_e2fs_mtime;
    312 	vap->va_mtime.tv_nsec = 0;
    313 	vap->va_ctime.tv_sec = ip->i_e2fs_ctime;
    314 	vap->va_ctime.tv_nsec = 0;
    315 #ifdef EXT2FS_SYSTEM_FLAGS
    316 	vap->va_flags = (ip->i_e2fs_flags & EXT2_APPEND) ? SF_APPEND : 0;
    317 	vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? SF_IMMUTABLE : 0;
    318 #else
    319 	vap->va_flags = (ip->i_e2fs_flags & EXT2_APPEND) ? UF_APPEND : 0;
    320 	vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? UF_IMMUTABLE : 0;
    321 #endif
    322 	vap->va_gen = ip->i_e2fs_gen;
    323 	/* this doesn't belong here */
    324 	if (vp->v_type == VBLK)
    325 		vap->va_blocksize = BLKDEV_IOSIZE;
    326 	else if (vp->v_type == VCHR)
    327 		vap->va_blocksize = MAXBSIZE;
    328 	else
    329 		vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
    330 	vap->va_bytes = dbtob(ext2fs_nblock(ip));
    331 	vap->va_type = vp->v_type;
    332 	vap->va_filerev = ip->i_modrev;
    333 	return (0);
    334 }
    335 
    336 /*
    337  * Set attribute vnode op. called from several syscalls
    338  */
    339 int
    340 ext2fs_setattr(void *v)
    341 {
    342 	struct vop_setattr_args /* {
    343 		struct vnode *a_vp;
    344 		struct vattr *a_vap;
    345 		kauth_cred_t a_cred;
    346 	} */ *ap = v;
    347 	struct vattr *vap = ap->a_vap;
    348 	struct vnode *vp = ap->a_vp;
    349 	struct inode *ip = VTOI(vp);
    350 	kauth_cred_t cred = ap->a_cred;
    351 	struct lwp *l = curlwp;
    352 	int error;
    353 	kauth_action_t action = KAUTH_VNODE_WRITE_FLAGS;
    354 	bool changing_sysflags = false;
    355 
    356 	/*
    357 	 * Check for unsettable attributes.
    358 	 */
    359 	if ((vap->va_type != VNON) || (vap->va_nlink != (nlink_t)VNOVAL) ||
    360 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
    361 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
    362 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
    363 		return (EINVAL);
    364 	}
    365 	if (vap->va_flags != VNOVAL) {
    366 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
    367 			return (EROFS);
    368 
    369 		/*
    370 		 * Check if we're allowed to change the flags.
    371 		 * If EXT2FS_SYSTEM_FLAGS is set, then the flags are treated
    372 		 * as system flags, otherwise they're considered to be user
    373 		 * flags.
    374 		 */
    375 #ifdef EXT2FS_SYSTEM_FLAGS
    376 		/* Indicate we're changing system flags if we are. */
    377 		if ((vap->va_flags & SF_APPEND) ||
    378 		     (vap->va_flags & SF_IMMUTABLE)) {
    379 			action |= KAUTH_VNODE_WRITE_SYSFLAGS;
    380 			changing_sysflags = true;
    381 		}
    382 
    383 		/* Indicate the node has system flags if it does. */
    384 		if (ip->i_e2fs_flags & (EXT2_APPEND | EXT2_IMMUTABLE)) {
    385 			action |= KAUTH_VNODE_HAS_SYSFLAGS;
    386 		}
    387 #endif /* EXT2FS_SYSTEM_FLAGS */
    388 
    389 		error = kauth_authorize_vnode(cred, action, vp, NULL,
    390 		    genfs_can_chflags(cred, vp->v_type, ip->i_uid,
    391 		    changing_sysflags));
    392 		if (error)
    393 			return (error);
    394 
    395 #ifdef EXT2FS_SYSTEM_FLAGS
    396 		ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE);
    397 		ip->i_e2fs_flags |=
    398 		    (vap->va_flags & SF_APPEND) ?  EXT2_APPEND : 0 |
    399 		    (vap->va_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE : 0;
    400 #else
    401 		ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE);
    402 		ip->i_e2fs_flags |=
    403 		    (vap->va_flags & UF_APPEND) ? EXT2_APPEND : 0 |
    404 		    (vap->va_flags & UF_IMMUTABLE) ? EXT2_IMMUTABLE : 0;
    405 #endif
    406 		ip->i_flag |= IN_CHANGE;
    407 		if (vap->va_flags & (IMMUTABLE | APPEND))
    408 			return (0);
    409 	}
    410 	if (ip->i_e2fs_flags & (EXT2_APPEND | EXT2_IMMUTABLE))
    411 		return (EPERM);
    412 	/*
    413 	 * Go through the fields and update iff not VNOVAL.
    414 	 */
    415 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
    416 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
    417 			return (EROFS);
    418 		error = ext2fs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
    419 		if (error)
    420 			return (error);
    421 	}
    422 	if (vap->va_size != VNOVAL) {
    423 		/*
    424 		 * Disallow write attempts on read-only file systems;
    425 		 * unless the file is a socket, fifo, or a block or
    426 		 * character device resident on the file system.
    427 		 */
    428 		switch (vp->v_type) {
    429 		case VDIR:
    430 			return (EISDIR);
    431 		case VLNK:
    432 		case VREG:
    433 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
    434 				return (EROFS);
    435 		default:
    436 			break;
    437 		}
    438 		error = ext2fs_truncate(vp, vap->va_size, 0, cred);
    439 		if (error)
    440 			return (error);
    441 	}
    442 	ip = VTOI(vp);
    443 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
    444 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
    445 			return (EROFS);
    446 		error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
    447 		    NULL, genfs_can_chtimes(vp, vap->va_vaflags, ip->i_uid,
    448 		    cred));
    449 		if (error)
    450 			return (error);
    451 		if (vap->va_atime.tv_sec != VNOVAL)
    452 			if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
    453 				ip->i_flag |= IN_ACCESS;
    454 		if (vap->va_mtime.tv_sec != VNOVAL) {
    455 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    456 			if (vp->v_mount->mnt_flag & MNT_RELATIME)
    457 				ip->i_flag |= IN_ACCESS;
    458 		}
    459 		error = ext2fs_update(vp, &vap->va_atime, &vap->va_mtime,
    460 			UPDATE_WAIT);
    461 		if (error)
    462 			return (error);
    463 	}
    464 	error = 0;
    465 	if (vap->va_mode != (mode_t)VNOVAL) {
    466 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
    467 			return (EROFS);
    468 		error = ext2fs_chmod(vp, (int)vap->va_mode, cred, l);
    469 	}
    470 	VN_KNOTE(vp, NOTE_ATTRIB);
    471 	return (error);
    472 }
    473 
    474 /*
    475  * Change the mode on a file.
    476  * Inode must be locked before calling.
    477  */
    478 static int
    479 ext2fs_chmod(struct vnode *vp, int mode, kauth_cred_t cred, struct lwp *l)
    480 {
    481 	struct inode *ip = VTOI(vp);
    482 	int error;
    483 
    484 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
    485 	    NULL, genfs_can_chmod(vp->v_type, cred, ip->i_uid, ip->i_gid,
    486 	    mode));
    487 	if (error)
    488 		return (error);
    489 
    490 	ip->i_e2fs_mode &= ~ALLPERMS;
    491 	ip->i_e2fs_mode |= (mode & ALLPERMS);
    492 	ip->i_flag |= IN_CHANGE;
    493 	return (0);
    494 }
    495 
    496 /*
    497  * Perform chown operation on inode ip;
    498  * inode must be locked prior to call.
    499  */
    500 static int
    501 ext2fs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
    502 		struct lwp *l)
    503 {
    504 	struct inode *ip = VTOI(vp);
    505 	uid_t ouid;
    506 	gid_t ogid;
    507 	int error;
    508 
    509 	if (uid == (uid_t)VNOVAL)
    510 		uid = ip->i_uid;
    511 	if (gid == (gid_t)VNOVAL)
    512 		gid = ip->i_gid;
    513 
    514 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
    515 	    NULL, genfs_can_chown(cred, ip->i_uid, ip->i_gid, uid, gid));
    516 	if (error)
    517 		return (error);
    518 
    519 	ogid = ip->i_gid;
    520 	ouid = ip->i_uid;
    521 
    522 	ip->i_e2fs_gid = gid & 0xffff;
    523 	ip->i_e2fs_uid = uid & 0xffff;
    524 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
    525 		ip->i_e2fs_gid_high = (gid >> 16) & 0xffff;
    526 		ip->i_e2fs_uid_high = (uid >> 16) & 0xffff;
    527 	} else {
    528 		ip->i_e2fs_gid_high = 0;
    529 		ip->i_e2fs_uid_high = 0;
    530 	}
    531 	if (ouid != uid || ogid != gid) {
    532 		ext2fs_set_inode_guid(ip);
    533 		ip->i_flag |= IN_CHANGE;
    534 	}
    535 	if (ouid != uid && (ip->i_e2fs_mode & ISUID) &&
    536 	    kauth_authorize_vnode(cred, KAUTH_VNODE_RETAIN_SUID,
    537 	    vp, NULL, EPERM) != 0)
    538 		ip->i_e2fs_mode &= ~ISUID;
    539 	if (ogid != gid && (ip->i_e2fs_mode & ISGID) &&
    540 	    kauth_authorize_vnode(cred, KAUTH_VNODE_RETAIN_SGID,
    541 	    vp, NULL, EPERM) != 0)
    542 		ip->i_e2fs_mode &= ~ISGID;
    543 	return (0);
    544 }
    545 
    546 int
    547 ext2fs_remove(void *v)
    548 {
    549 	struct vop_remove_args /* {
    550 		struct vnode *a_dvp;
    551 		struct vnode *a_vp;
    552 		struct componentname *a_cnp;
    553 	} */ *ap = v;
    554 	struct inode *ip;
    555 	struct vnode *vp = ap->a_vp;
    556 	struct vnode *dvp = ap->a_dvp;
    557 	struct ufs_lookup_results *ulr;
    558 	int error;
    559 
    560 	/* XXX should handle this material another way */
    561 	ulr = &VTOI(dvp)->i_crap;
    562 	UFS_CHECK_CRAPCOUNTER(VTOI(dvp));
    563 
    564 	ip = VTOI(vp);
    565 	if (vp->v_type == VDIR ||
    566 		(ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
    567 		(VTOI(dvp)->i_e2fs_flags & EXT2_APPEND)) {
    568 		error = EPERM;
    569 	} else {
    570 		error = ext2fs_dirremove(dvp, ulr, ap->a_cnp);
    571 		if (error == 0) {
    572 			ip->i_e2fs_nlink--;
    573 			ip->i_flag |= IN_CHANGE;
    574 		}
    575 	}
    576 
    577 	VN_KNOTE(vp, NOTE_DELETE);
    578 	VN_KNOTE(dvp, NOTE_WRITE);
    579 	if (dvp == vp)
    580 		vrele(vp);
    581 	else
    582 		vput(vp);
    583 	vput(dvp);
    584 	return (error);
    585 }
    586 
    587 /*
    588  * ext2fs_link: create hard link.
    589  */
    590 int
    591 ext2fs_link(void *v)
    592 {
    593 	struct vop_link_args /* {
    594 		struct vnode *a_dvp;
    595 		struct vnode *a_vp;
    596 		struct componentname *a_cnp;
    597 	} */ *ap = v;
    598 	struct vnode *dvp = ap->a_dvp;
    599 	struct vnode *vp = ap->a_vp;
    600 	struct componentname *cnp = ap->a_cnp;
    601 	struct inode *ip;
    602 	int error;
    603 	struct ufs_lookup_results *ulr;
    604 
    605 	KASSERT(dvp != vp);
    606 	KASSERT(vp->v_type != VDIR);
    607 	KASSERT(dvp->v_mount == vp->v_mount);
    608 
    609 	/* XXX should handle this material another way */
    610 	ulr = &VTOI(dvp)->i_crap;
    611 	UFS_CHECK_CRAPCOUNTER(VTOI(dvp));
    612 
    613 	error = vn_lock(vp, LK_EXCLUSIVE);
    614 	if (error) {
    615 		VOP_ABORTOP(dvp, cnp);
    616 		goto out2;
    617 	}
    618 	ip = VTOI(vp);
    619 	if ((nlink_t)ip->i_e2fs_nlink >= LINK_MAX) {
    620 		VOP_ABORTOP(dvp, cnp);
    621 		error = EMLINK;
    622 		goto out1;
    623 	}
    624 	if (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) {
    625 		VOP_ABORTOP(dvp, cnp);
    626 		error = EPERM;
    627 		goto out1;
    628 	}
    629 	ip->i_e2fs_nlink++;
    630 	ip->i_flag |= IN_CHANGE;
    631 	error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
    632 	if (!error)
    633 		error = ext2fs_direnter(ip, dvp, ulr, cnp);
    634 	if (error) {
    635 		ip->i_e2fs_nlink--;
    636 		ip->i_flag |= IN_CHANGE;
    637 	}
    638 out1:
    639 	VOP_UNLOCK(vp);
    640 out2:
    641 	VN_KNOTE(vp, NOTE_LINK);
    642 	VN_KNOTE(dvp, NOTE_WRITE);
    643 	vput(dvp);
    644 	return (error);
    645 }
    646 
    647 /*
    648  * Mkdir system call
    649  */
    650 int
    651 ext2fs_mkdir(void *v)
    652 {
    653 	struct vop_mkdir_v3_args /* {
    654 		struct vnode *a_dvp;
    655 		struct vnode **a_vpp;
    656 		struct componentname *a_cnp;
    657 		struct vattr *a_vap;
    658 	} */ *ap = v;
    659 	struct vnode		*dvp = ap->a_dvp;
    660 	struct vattr		*vap = ap->a_vap;
    661 	struct componentname	*cnp = ap->a_cnp;
    662 	struct inode		*ip, *dp = VTOI(dvp);
    663 	struct vnode		*tvp;
    664 	struct ext2fs_dirtemplate dirtemplate;
    665 	int			error, dmode;
    666 	struct ufs_lookup_results *ulr;
    667 
    668 	/* XXX should handle this material another way */
    669 	ulr = &VTOI(dvp)->i_crap;
    670 	UFS_CHECK_CRAPCOUNTER(VTOI(dvp));
    671 
    672 	if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) {
    673 		error = EMLINK;
    674 		goto out;
    675 	}
    676 	dmode = vap->va_mode & ACCESSPERMS;
    677 	dmode |= IFDIR;
    678 	/*
    679 	 * Must simulate part of ext2fs_makeinode here to acquire the inode,
    680 	 * but not have it entered in the parent directory. The entry is
    681 	 * made later after writing "." and ".." entries.
    682 	 */
    683 	if ((error = ext2fs_valloc(dvp, dmode, cnp->cn_cred, &tvp)) != 0)
    684 		goto out;
    685 	ip = VTOI(tvp);
    686 	ip->i_uid = kauth_cred_geteuid(cnp->cn_cred);
    687 	ip->i_e2fs_uid = ip->i_uid & 0xffff;
    688 	ip->i_e2fs_gid = dp->i_e2fs_gid;
    689 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
    690 		ip->i_e2fs_uid_high = (ip->i_uid >> 16) & 0xffff;
    691 		ip->i_e2fs_gid_high = dp->i_e2fs_gid_high;
    692 	} else {
    693 		ip->i_e2fs_uid_high = 0;
    694 		ip->i_e2fs_gid_high = 0;
    695 	}
    696 	ip->i_gid = ip->i_e2fs_gid | (ip->i_e2fs_gid_high << 16);
    697 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
    698 	ip->i_e2fs_mode = dmode;
    699 	tvp->v_type = VDIR;	/* Rest init'd in getnewvnode(). */
    700 	ip->i_e2fs_nlink = 2;
    701 
    702 	/*
    703 	 * Bump link count in parent directory
    704 	 * to reflect work done below.  Should
    705 	 * be done before reference is created
    706 	 * so reparation is possible if we crash.
    707 	 */
    708 	dp->i_e2fs_nlink++;
    709 	dp->i_flag |= IN_CHANGE;
    710 	if ((error = ext2fs_update(dvp, NULL, NULL, UPDATE_DIROP)) != 0)
    711 		goto bad;
    712 
    713 	/* Initialize directory with "." and ".." from static template. */
    714 	memset(&dirtemplate, 0, sizeof(dirtemplate));
    715 	dirtemplate.dot_ino = h2fs32(ip->i_number);
    716 	dirtemplate.dot_reclen = h2fs16(12);
    717 	dirtemplate.dot_namlen = 1;
    718 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
    719 	    (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
    720 		dirtemplate.dot_type = EXT2_FT_DIR;
    721 	}
    722 	dirtemplate.dot_name[0] = '.';
    723 	dirtemplate.dotdot_ino = h2fs32(dp->i_number);
    724     dirtemplate.dotdot_reclen = h2fs16(VTOI(dvp)->i_e2fs->e2fs_bsize - 12);
    725 	dirtemplate.dotdot_namlen = 2;
    726 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
    727 	    (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
    728 		dirtemplate.dotdot_type = EXT2_FT_DIR;
    729 	}
    730 	dirtemplate.dotdot_name[0] = dirtemplate.dotdot_name[1] = '.';
    731 	error = vn_rdwr(UIO_WRITE, tvp, (void *)&dirtemplate,
    732 	    sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
    733 	    IO_NODELOCKED|IO_SYNC, cnp->cn_cred, (size_t *)0, NULL);
    734 	if (error) {
    735 		dp->i_e2fs_nlink--;
    736 		dp->i_flag |= IN_CHANGE;
    737 		goto bad;
    738 	}
    739 	if (VTOI(dvp)->i_e2fs->e2fs_bsize > dvp->v_mount->mnt_stat.f_bsize)
    740 		panic("ext2fs_mkdir: blksize"); /* XXX should grow with balloc() */
    741 	else {
    742 		error = ext2fs_setsize(ip, VTOI(dvp)->i_e2fs->e2fs_bsize);
    743 		if (error) {
    744 			dp->i_e2fs_nlink--;
    745 			dp->i_flag |= IN_CHANGE;
    746 			goto bad;
    747 		}
    748 		ip->i_flag |= IN_CHANGE;
    749 		uvm_vnp_setsize(tvp, ext2fs_size(ip));
    750 	}
    751 
    752 	/* Directory set up, now install it's entry in the parent directory. */
    753 	error = ext2fs_direnter(ip, dvp, ulr, cnp);
    754 	if (error != 0) {
    755 		dp->i_e2fs_nlink--;
    756 		dp->i_flag |= IN_CHANGE;
    757 	}
    758 bad:
    759 	/*
    760 	 * No need to do an explicit ext2fs_truncate here, vrele will do this
    761 	 * for us because we set the link count to 0.
    762 	 */
    763 	if (error) {
    764 		ip->i_e2fs_nlink = 0;
    765 		ip->i_flag |= IN_CHANGE;
    766 		vput(tvp);
    767 	} else {
    768 		VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
    769 		VOP_UNLOCK(tvp);
    770 		*ap->a_vpp = tvp;
    771 	}
    772 out:
    773 	return (error);
    774 }
    775 
    776 /*
    777  * Rmdir system call.
    778  */
    779 int
    780 ext2fs_rmdir(void *v)
    781 {
    782 	struct vop_rmdir_args /* {
    783 		struct vnode *a_dvp;
    784 		struct vnode *a_vp;
    785 		struct componentname *a_cnp;
    786 	} */ *ap = v;
    787 	struct vnode *vp = ap->a_vp;
    788 	struct vnode *dvp = ap->a_dvp;
    789 	struct componentname *cnp = ap->a_cnp;
    790 	struct inode *ip, *dp;
    791 	int error;
    792 	struct ufs_lookup_results *ulr;
    793 
    794 	ip = VTOI(vp);
    795 	dp = VTOI(dvp);
    796 
    797 	/* XXX should handle this material another way */
    798 	ulr = &dp->i_crap;
    799 	UFS_CHECK_CRAPCOUNTER(dp);
    800 
    801 	/*
    802 	 * No rmdir "." please.
    803 	 */
    804 	if (dp == ip) {
    805 		vrele(dvp);
    806 		vput(vp);
    807 		return (EINVAL);
    808 	}
    809 	/*
    810 	 * Verify the directory is empty (and valid).
    811 	 * (Rmdir ".." won't be valid since
    812 	 *  ".." will contain a reference to
    813 	 *  the current directory and thus be
    814 	 *  non-empty.)
    815 	 */
    816 	error = 0;
    817 	if (ip->i_e2fs_nlink != 2 ||
    818 	    !ext2fs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
    819 		error = ENOTEMPTY;
    820 		goto out;
    821 	}
    822 	if ((dp->i_e2fs_flags & EXT2_APPEND) ||
    823 				 (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND))) {
    824 		error = EPERM;
    825 		goto out;
    826 	}
    827 	/*
    828 	 * Delete reference to directory before purging
    829 	 * inode.  If we crash in between, the directory
    830 	 * will be reattached to lost+found,
    831 	 */
    832 	error = ext2fs_dirremove(dvp, ulr, cnp);
    833 	if (error != 0)
    834 		goto out;
    835 	dp->i_e2fs_nlink--;
    836 	dp->i_flag |= IN_CHANGE;
    837 	VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
    838 	cache_purge(dvp);
    839 	vput(dvp);
    840 	dvp = NULL;
    841 	/*
    842 	 * Truncate inode.  The only stuff left
    843 	 * in the directory is "." and "..".  The
    844 	 * "." reference is inconsequential since
    845 	 * we're quashing it.  The ".." reference
    846 	 * has already been adjusted above.  We've
    847 	 * removed the "." reference and the reference
    848 	 * in the parent directory, but there may be
    849 	 * other hard links so decrement by 2 and
    850 	 * worry about them later.
    851 	 */
    852 	ip->i_e2fs_nlink -= 2;
    853 	error = ext2fs_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred);
    854 	cache_purge(ITOV(ip));
    855 out:
    856 	VN_KNOTE(vp, NOTE_DELETE);
    857 	if (dvp)
    858 		vput(dvp);
    859 	vput(vp);
    860 	return (error);
    861 }
    862 
    863 /*
    864  * symlink -- make a symbolic link
    865  */
    866 int
    867 ext2fs_symlink(void *v)
    868 {
    869 	struct vop_symlink_v3_args /* {
    870 		struct vnode *a_dvp;
    871 		struct vnode **a_vpp;
    872 		struct componentname *a_cnp;
    873 		struct vattr *a_vap;
    874 		char *a_target;
    875 	} */ *ap = v;
    876 	struct vnode	*vp, **vpp;
    877 	struct inode	*ip;
    878 	int		len, error;
    879 
    880 	vpp = ap->a_vpp;
    881 	error = ext2fs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
    882 			      vpp, ap->a_cnp);
    883 	if (error)
    884 		return (error);
    885 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
    886 	vp = *vpp;
    887 	len = strlen(ap->a_target);
    888 	ip = VTOI(vp);
    889 	if (len < ip->i_ump->um_maxsymlinklen) {
    890 		memcpy((char *)ip->i_din.e2fs_din->e2di_shortlink, ap->a_target, len);
    891 		error = ext2fs_setsize(ip, len);
    892 		if (error)
    893 			goto bad;
    894 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    895 		if (vp->v_mount->mnt_flag & MNT_RELATIME)
    896 			ip->i_flag |= IN_ACCESS;
    897 		uvm_vnp_setsize(vp, len);
    898 	} else
    899 		error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
    900 		    UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred,
    901 		    (size_t *)0, NULL);
    902 bad:
    903 	VOP_UNLOCK(vp);
    904 	if (error)
    905 		vrele(vp);
    906 	return (error);
    907 }
    908 
    909 /*
    910  * Return target name of a symbolic link
    911  */
    912 int
    913 ext2fs_readlink(void *v)
    914 {
    915 	struct vop_readlink_args /* {
    916 		struct vnode *a_vp;
    917 		struct uio *a_uio;
    918 		kauth_cred_t a_cred;
    919 	} */ *ap = v;
    920 	struct vnode	*vp = ap->a_vp;
    921 	struct inode	*ip = VTOI(vp);
    922 	struct ufsmount	*ump = ip->i_ump;
    923 	int		isize;
    924 
    925 	isize = ext2fs_size(ip);
    926 	if (isize < ump->um_maxsymlinklen ||
    927 	    (ump->um_maxsymlinklen == 0 && ext2fs_nblock(ip) == 0)) {
    928 		uiomove((char *)ip->i_din.e2fs_din->e2di_shortlink, isize, ap->a_uio);
    929 		return (0);
    930 	}
    931 	return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
    932 }
    933 
    934 /*
    935  * Advisory record locking support
    936  */
    937 int
    938 ext2fs_advlock(void *v)
    939 {
    940 	struct vop_advlock_args /* {
    941 		struct vnode *a_vp;
    942 		void * a_id;
    943 		int  a_op;
    944 		struct flock *a_fl;
    945 		int  a_flags;
    946 	} */ *ap = v;
    947 	struct inode *ip = VTOI(ap->a_vp);
    948 
    949 	return lf_advlock(ap, &ip->i_lockf, ext2fs_size(ip));
    950 }
    951 
    952 int
    953 ext2fs_fsync(void *v)
    954 {
    955 	struct vop_fsync_args /* {
    956 		struct vnode *a_vp;
    957 		kauth_cred_t a_cred;
    958 		int a_flags;
    959 		off_t offlo;
    960 		off_t offhi;
    961 		struct proc *a_p;
    962 	} */ *ap = v;
    963 	struct vnode *vp = ap->a_vp;
    964 	int wait;
    965 	int error;
    966 
    967 	wait = (ap->a_flags & FSYNC_WAIT) != 0;
    968 
    969 	if (vp->v_type == VBLK)
    970 		error = spec_fsync(v);
    971 	else
    972 		error = vflushbuf(vp, ap->a_flags);
    973 	if (error == 0 && (ap->a_flags & FSYNC_DATAONLY) == 0)
    974 		error = ext2fs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
    975 
    976 	if (error == 0 && ap->a_flags & FSYNC_CACHE) {
    977 		int l = 0;
    978 		error = VOP_IOCTL(VTOI(vp)->i_devvp, DIOCCACHESYNC, &l, FWRITE,
    979 		    curlwp->l_cred);
    980 	}
    981 
    982 	return error;
    983 }
    984 
    985 /*
    986  * Initialize the vnode associated with a new inode, handle aliased
    987  * vnodes.
    988  */
    989 int
    990 ext2fs_vinit(struct mount *mntp, int (**specops)(void *),
    991 	int (**fifoops)(void *), struct vnode **vpp)
    992 {
    993 	struct timeval tv;
    994 	struct inode *ip;
    995 	struct vnode *vp;
    996 
    997 	vp = *vpp;
    998 	ip = VTOI(vp);
    999 	switch(vp->v_type = IFTOVT(ip->i_e2fs_mode)) {
   1000 	case VCHR:
   1001 	case VBLK:
   1002 		vp->v_op = specops;
   1003 		spec_node_init(vp, fs2h32(ip->i_din.e2fs_din->e2di_rdev));
   1004 		break;
   1005 	case VFIFO:
   1006 		vp->v_op = fifoops;
   1007 		break;
   1008 	case VNON:
   1009 	case VBAD:
   1010 	case VSOCK:
   1011 	case VLNK:
   1012 	case VDIR:
   1013 	case VREG:
   1014 		break;
   1015 	}
   1016 	if (ip->i_number == UFS_ROOTINO)
   1017                 vp->v_vflag |= VV_ROOT;
   1018 	/*
   1019 	 * Initialize modrev times
   1020 	 */
   1021 	getmicrouptime(&tv);
   1022 	SETHIGH(ip->i_modrev, tv.tv_sec);
   1023 	SETLOW(ip->i_modrev, tv.tv_usec * 4294);
   1024 	*vpp = vp;
   1025 	return (0);
   1026 }
   1027 
   1028 /*
   1029  * Allocate a new inode.
   1030  */
   1031 int
   1032 ext2fs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
   1033 		struct componentname *cnp)
   1034 {
   1035 	struct inode *ip, *pdir;
   1036 	struct vnode *tvp;
   1037 	int error;
   1038 	struct ufs_lookup_results *ulr;
   1039 
   1040 	pdir = VTOI(dvp);
   1041 
   1042 	/* XXX should handle this material another way */
   1043 	ulr = &pdir->i_crap;
   1044 	UFS_CHECK_CRAPCOUNTER(pdir);
   1045 
   1046 	*vpp = NULL;
   1047 	if ((mode & IFMT) == 0)
   1048 		mode |= IFREG;
   1049 
   1050 	if ((error = ext2fs_valloc(dvp, mode, cnp->cn_cred, &tvp)) != 0) {
   1051 		return (error);
   1052 	}
   1053 	ip = VTOI(tvp);
   1054 	ip->i_uid = kauth_cred_geteuid(cnp->cn_cred);
   1055 	ip->i_e2fs_uid = ip->i_uid & 0xffff;
   1056 	ip->i_e2fs_gid = pdir->i_e2fs_gid;
   1057 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
   1058 		ip->i_e2fs_uid_high = (ip->i_uid >> 16) & 0xffff;
   1059 		ip->i_e2fs_gid_high = pdir->i_e2fs_gid_high;
   1060 	} else {
   1061 		ip->i_e2fs_uid_high = 0;
   1062 		ip->i_e2fs_gid_high = 0;
   1063 	}
   1064 	ip->i_gid = ip->i_e2fs_gid | (ip->i_e2fs_gid_high << 16);
   1065 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
   1066 	ip->i_e2fs_mode = mode;
   1067 	tvp->v_type = IFTOVT(mode);	/* Rest init'd in getnewvnode(). */
   1068 	ip->i_e2fs_nlink = 1;
   1069 
   1070 	/* Authorize setting SGID if needed. */
   1071 	if (ip->i_e2fs_mode & ISGID) {
   1072 		error = kauth_authorize_vnode(cnp->cn_cred, KAUTH_VNODE_WRITE_SECURITY,
   1073 		    tvp, NULL, genfs_can_chmod(tvp->v_type, cnp->cn_cred, ip->i_uid,
   1074 		    ip->i_gid, mode));
   1075 		if (error)
   1076 			ip->i_e2fs_mode &= ~ISGID;
   1077 	}
   1078 
   1079 	/*
   1080 	 * Make sure inode goes to disk before directory entry.
   1081 	 */
   1082 	if ((error = ext2fs_update(tvp, NULL, NULL, UPDATE_WAIT)) != 0)
   1083 		goto bad;
   1084 	error = ext2fs_direnter(ip, dvp, ulr, cnp);
   1085 	if (error != 0)
   1086 		goto bad;
   1087 	*vpp = tvp;
   1088 	return (0);
   1089 
   1090 bad:
   1091 	/*
   1092 	 * Write error occurred trying to update the inode
   1093 	 * or the directory so must deallocate the inode.
   1094 	 */
   1095 	tvp->v_type = VNON;	/* Stop explosion if VBLK */
   1096 	ip->i_e2fs_nlink = 0;
   1097 	ip->i_flag |= IN_CHANGE;
   1098 	vput(tvp);
   1099 	return (error);
   1100 }
   1101 
   1102 /*
   1103  * Reclaim an inode so that it can be used for other purposes.
   1104  */
   1105 int
   1106 ext2fs_reclaim(void *v)
   1107 {
   1108 	struct vop_reclaim_args /* {
   1109 		struct vnode *a_vp;
   1110 	} */ *ap = v;
   1111 	struct vnode *vp = ap->a_vp;
   1112 	struct inode *ip = VTOI(vp);
   1113 	int error;
   1114 
   1115 	/*
   1116 	 * The inode must be freed and updated before being removed
   1117 	 * from its hash chain.  Other threads trying to gain a hold
   1118 	 * or lock on the inode will be stalled.
   1119 	 */
   1120 	if (ip->i_omode == 1 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
   1121 		ext2fs_vfree(vp, ip->i_number, ip->i_e2fs_mode);
   1122 	if ((error = ufs_reclaim(vp)) != 0)
   1123 		return (error);
   1124 	if (ip->i_din.e2fs_din != NULL)
   1125 		pool_put(&ext2fs_dinode_pool, ip->i_din.e2fs_din);
   1126 	genfs_node_destroy(vp);
   1127 	pool_put(&ext2fs_inode_pool, vp->v_data);
   1128 	vp->v_data = NULL;
   1129 	return (0);
   1130 }
   1131 
   1132 /* Global vfs data structures for ext2fs. */
   1133 int (**ext2fs_vnodeop_p)(void *);
   1134 const struct vnodeopv_entry_desc ext2fs_vnodeop_entries[] = {
   1135 	{ &vop_default_desc, vn_default_error },
   1136 	{ &vop_lookup_desc, ext2fs_lookup },		/* lookup */
   1137 	{ &vop_create_desc, ext2fs_create },		/* create */
   1138 	{ &vop_mknod_desc, ext2fs_mknod },		/* mknod */
   1139 	{ &vop_open_desc, ext2fs_open },		/* open */
   1140 	{ &vop_close_desc, ufs_close },			/* close */
   1141 	{ &vop_access_desc, ext2fs_access },		/* access */
   1142 	{ &vop_getattr_desc, ext2fs_getattr },		/* getattr */
   1143 	{ &vop_setattr_desc, ext2fs_setattr },		/* setattr */
   1144 	{ &vop_read_desc, ext2fs_read },		/* read */
   1145 	{ &vop_write_desc, ext2fs_write },		/* write */
   1146 	{ &vop_ioctl_desc, ufs_ioctl },			/* ioctl */
   1147 	{ &vop_fcntl_desc, ufs_fcntl },			/* fcntl */
   1148 	{ &vop_poll_desc, ufs_poll },			/* poll */
   1149 	{ &vop_kqfilter_desc, genfs_kqfilter },		/* kqfilter */
   1150 	{ &vop_revoke_desc, ufs_revoke },		/* revoke */
   1151 	{ &vop_mmap_desc, ufs_mmap },			/* mmap */
   1152 	{ &vop_fsync_desc, ext2fs_fsync },		/* fsync */
   1153 	{ &vop_seek_desc, ufs_seek },			/* seek */
   1154 	{ &vop_remove_desc, ext2fs_remove },		/* remove */
   1155 	{ &vop_link_desc, ext2fs_link },		/* link */
   1156 	{ &vop_rename_desc, ext2fs_rename },		/* rename */
   1157 	{ &vop_mkdir_desc, ext2fs_mkdir },		/* mkdir */
   1158 	{ &vop_rmdir_desc, ext2fs_rmdir },		/* rmdir */
   1159 	{ &vop_symlink_desc, ext2fs_symlink },		/* symlink */
   1160 	{ &vop_readdir_desc, ext2fs_readdir },		/* readdir */
   1161 	{ &vop_readlink_desc, ext2fs_readlink },	/* readlink */
   1162 	{ &vop_abortop_desc, ufs_abortop },		/* abortop */
   1163 	{ &vop_inactive_desc, ext2fs_inactive },	/* inactive */
   1164 	{ &vop_reclaim_desc, ext2fs_reclaim },		/* reclaim */
   1165 	{ &vop_lock_desc, ufs_lock },			/* lock */
   1166 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
   1167 	{ &vop_bmap_desc, ext2fs_bmap },		/* bmap */
   1168 	{ &vop_strategy_desc, ufs_strategy },		/* strategy */
   1169 	{ &vop_print_desc, ufs_print },			/* print */
   1170 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
   1171 	{ &vop_pathconf_desc, ufs_pathconf },		/* pathconf */
   1172 	{ &vop_advlock_desc, ext2fs_advlock },		/* advlock */
   1173 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
   1174 	{ &vop_getpages_desc, genfs_getpages },		/* getpages */
   1175 	{ &vop_putpages_desc, genfs_putpages },		/* putpages */
   1176 	{ NULL, NULL }
   1177 };
   1178 const struct vnodeopv_desc ext2fs_vnodeop_opv_desc =
   1179 	{ &ext2fs_vnodeop_p, ext2fs_vnodeop_entries };
   1180 
   1181 int (**ext2fs_specop_p)(void *);
   1182 const struct vnodeopv_entry_desc ext2fs_specop_entries[] = {
   1183 	{ &vop_default_desc, vn_default_error },
   1184 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
   1185 	{ &vop_create_desc, spec_create },		/* create */
   1186 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
   1187 	{ &vop_open_desc, spec_open },			/* open */
   1188 	{ &vop_close_desc, ufsspec_close },		/* close */
   1189 	{ &vop_access_desc, ext2fs_access },		/* access */
   1190 	{ &vop_getattr_desc, ext2fs_getattr },		/* getattr */
   1191 	{ &vop_setattr_desc, ext2fs_setattr },		/* setattr */
   1192 	{ &vop_read_desc, ufsspec_read },		/* read */
   1193 	{ &vop_write_desc, ufsspec_write },		/* write */
   1194 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
   1195 	{ &vop_fcntl_desc, ufs_fcntl },			/* fcntl */
   1196 	{ &vop_poll_desc, spec_poll },			/* poll */
   1197 	{ &vop_kqfilter_desc, spec_kqfilter },		/* kqfilter */
   1198 	{ &vop_revoke_desc, spec_revoke },		/* revoke */
   1199 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
   1200 	{ &vop_fsync_desc, ext2fs_fsync },		/* fsync */
   1201 	{ &vop_seek_desc, spec_seek },			/* seek */
   1202 	{ &vop_remove_desc, spec_remove },		/* remove */
   1203 	{ &vop_link_desc, spec_link },			/* link */
   1204 	{ &vop_rename_desc, spec_rename },		/* rename */
   1205 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
   1206 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
   1207 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
   1208 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
   1209 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
   1210 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
   1211 	{ &vop_inactive_desc, ext2fs_inactive },	/* inactive */
   1212 	{ &vop_reclaim_desc, ext2fs_reclaim },		/* reclaim */
   1213 	{ &vop_lock_desc, ufs_lock },			/* lock */
   1214 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
   1215 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
   1216 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
   1217 	{ &vop_print_desc, ufs_print },			/* print */
   1218 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
   1219 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
   1220 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
   1221 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
   1222 	{ &vop_getpages_desc, spec_getpages },		/* getpages */
   1223 	{ &vop_putpages_desc, spec_putpages },		/* putpages */
   1224 	{ NULL, NULL }
   1225 };
   1226 const struct vnodeopv_desc ext2fs_specop_opv_desc =
   1227 	{ &ext2fs_specop_p, ext2fs_specop_entries };
   1228 
   1229 int (**ext2fs_fifoop_p)(void *);
   1230 const struct vnodeopv_entry_desc ext2fs_fifoop_entries[] = {
   1231 	{ &vop_default_desc, vn_default_error },
   1232 	{ &vop_lookup_desc, vn_fifo_bypass },		/* lookup */
   1233 	{ &vop_create_desc, vn_fifo_bypass },		/* create */
   1234 	{ &vop_mknod_desc, vn_fifo_bypass },		/* mknod */
   1235 	{ &vop_open_desc, vn_fifo_bypass },		/* open */
   1236 	{ &vop_close_desc, ufsfifo_close },		/* close */
   1237 	{ &vop_access_desc, ext2fs_access },		/* access */
   1238 	{ &vop_getattr_desc, ext2fs_getattr },		/* getattr */
   1239 	{ &vop_setattr_desc, ext2fs_setattr },		/* setattr */
   1240 	{ &vop_read_desc, ufsfifo_read },		/* read */
   1241 	{ &vop_write_desc, ufsfifo_write },		/* write */
   1242 	{ &vop_ioctl_desc, vn_fifo_bypass },		/* ioctl */
   1243 	{ &vop_fcntl_desc, ufs_fcntl },			/* fcntl */
   1244 	{ &vop_poll_desc, vn_fifo_bypass },		/* poll */
   1245 	{ &vop_kqfilter_desc, vn_fifo_bypass },		/* kqfilter */
   1246 	{ &vop_revoke_desc, vn_fifo_bypass },		/* revoke */
   1247 	{ &vop_mmap_desc, vn_fifo_bypass },		/* mmap */
   1248 	{ &vop_fsync_desc, ext2fs_fsync },		/* fsync */
   1249 	{ &vop_seek_desc, vn_fifo_bypass },		/* seek */
   1250 	{ &vop_remove_desc, vn_fifo_bypass },		/* remove */
   1251 	{ &vop_link_desc, vn_fifo_bypass },		/* link */
   1252 	{ &vop_rename_desc, vn_fifo_bypass },		/* rename */
   1253 	{ &vop_mkdir_desc, vn_fifo_bypass },		/* mkdir */
   1254 	{ &vop_rmdir_desc, vn_fifo_bypass },		/* rmdir */
   1255 	{ &vop_symlink_desc, vn_fifo_bypass },		/* symlink */
   1256 	{ &vop_readdir_desc, vn_fifo_bypass },		/* readdir */
   1257 	{ &vop_readlink_desc, vn_fifo_bypass },		/* readlink */
   1258 	{ &vop_abortop_desc, vn_fifo_bypass },		/* abortop */
   1259 	{ &vop_inactive_desc, ext2fs_inactive },	/* inactive */
   1260 	{ &vop_reclaim_desc, ext2fs_reclaim },		/* reclaim */
   1261 	{ &vop_lock_desc, ufs_lock },			/* lock */
   1262 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
   1263 	{ &vop_bmap_desc, vn_fifo_bypass },		/* bmap */
   1264 	{ &vop_strategy_desc, vn_fifo_bypass },		/* strategy */
   1265 	{ &vop_print_desc, ufs_print },			/* print */
   1266 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
   1267 	{ &vop_pathconf_desc, vn_fifo_bypass },		/* pathconf */
   1268 	{ &vop_advlock_desc, vn_fifo_bypass },		/* advlock */
   1269 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
   1270 	{ &vop_putpages_desc, vn_fifo_bypass },		/* putpages */
   1271 	{ NULL, NULL }
   1272 };
   1273 const struct vnodeopv_desc ext2fs_fifoop_opv_desc =
   1274 	{ &ext2fs_fifoop_p, ext2fs_fifoop_entries };
   1275