Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_vfsops.c revision 1.146
      1 /*	$NetBSD: ext2fs_vfsops.c,v 1.146 2009/09/12 01:43:52 tsutsui Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1991, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)ffs_vfsops.c	8.14 (Berkeley) 11/28/94
     32  * Modified for ext2fs by Manuel Bouyer.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1997 Manuel Bouyer.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. All advertising materials mentioning features or use of this software
     47  *    must display the following acknowledgement:
     48  *	This product includes software developed by Manuel Bouyer.
     49  * 4. The name of the author may not be used to endorse or promote products
     50  *    derived from this software without specific prior written permission.
     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  *	@(#)ffs_vfsops.c	8.14 (Berkeley) 11/28/94
     64  * Modified for ext2fs by Manuel Bouyer.
     65  */
     66 
     67 #include <sys/cdefs.h>
     68 __KERNEL_RCSID(0, "$NetBSD: ext2fs_vfsops.c,v 1.146 2009/09/12 01:43:52 tsutsui Exp $");
     69 
     70 #if defined(_KERNEL_OPT)
     71 #include "opt_compat_netbsd.h"
     72 #endif
     73 
     74 #include <sys/param.h>
     75 #include <sys/systm.h>
     76 #include <sys/sysctl.h>
     77 #include <sys/namei.h>
     78 #include <sys/proc.h>
     79 #include <sys/kernel.h>
     80 #include <sys/vnode.h>
     81 #include <sys/socket.h>
     82 #include <sys/mount.h>
     83 #include <sys/buf.h>
     84 #include <sys/device.h>
     85 #include <sys/mbuf.h>
     86 #include <sys/file.h>
     87 #include <sys/disklabel.h>
     88 #include <sys/ioctl.h>
     89 #include <sys/errno.h>
     90 #include <sys/malloc.h>
     91 #include <sys/pool.h>
     92 #include <sys/lock.h>
     93 #include <sys/conf.h>
     94 #include <sys/kauth.h>
     95 #include <sys/module.h>
     96 
     97 #include <miscfs/genfs/genfs.h>
     98 #include <miscfs/specfs/specdev.h>
     99 
    100 #include <ufs/ufs/quota.h>
    101 #include <ufs/ufs/ufsmount.h>
    102 #include <ufs/ufs/inode.h>
    103 #include <ufs/ufs/dir.h>
    104 #include <ufs/ufs/ufs_extern.h>
    105 
    106 #include <ufs/ext2fs/ext2fs.h>
    107 #include <ufs/ext2fs/ext2fs_dir.h>
    108 #include <ufs/ext2fs/ext2fs_extern.h>
    109 
    110 MODULE(MODULE_CLASS_VFS, ext2fs, "ffs");
    111 
    112 extern kmutex_t ufs_hashlock;
    113 
    114 int ext2fs_sbupdate(struct ufsmount *, int);
    115 static int ext2fs_checksb(struct ext2fs *, int);
    116 static void ext2fs_set_inode_guid(struct inode *);
    117 
    118 static struct sysctllog *ext2fs_sysctl_log;
    119 
    120 extern const struct vnodeopv_desc ext2fs_vnodeop_opv_desc;
    121 extern const struct vnodeopv_desc ext2fs_specop_opv_desc;
    122 extern const struct vnodeopv_desc ext2fs_fifoop_opv_desc;
    123 
    124 const struct vnodeopv_desc * const ext2fs_vnodeopv_descs[] = {
    125 	&ext2fs_vnodeop_opv_desc,
    126 	&ext2fs_specop_opv_desc,
    127 	&ext2fs_fifoop_opv_desc,
    128 	NULL,
    129 };
    130 
    131 struct vfsops ext2fs_vfsops = {
    132 	MOUNT_EXT2FS,
    133 	sizeof (struct ufs_args),
    134 	ext2fs_mount,
    135 	ufs_start,
    136 	ext2fs_unmount,
    137 	ufs_root,
    138 	ufs_quotactl,
    139 	ext2fs_statvfs,
    140 	ext2fs_sync,
    141 	ext2fs_vget,
    142 	ext2fs_fhtovp,
    143 	ext2fs_vptofh,
    144 	ext2fs_init,
    145 	ext2fs_reinit,
    146 	ext2fs_done,
    147 	ext2fs_mountroot,
    148 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    149 	vfs_stdextattrctl,
    150 	(void *)eopnotsupp,	/* vfs_suspendctl */
    151 	genfs_renamelock_enter,
    152 	genfs_renamelock_exit,
    153 	(void *)eopnotsupp,
    154 	ext2fs_vnodeopv_descs,
    155 	0,
    156 	{ NULL, NULL },
    157 };
    158 
    159 static const struct genfs_ops ext2fs_genfsops = {
    160 	.gop_size = genfs_size,
    161 	.gop_alloc = ext2fs_gop_alloc,
    162 	.gop_write = genfs_gop_write,
    163 	.gop_markupdate = ufs_gop_markupdate,
    164 };
    165 
    166 static const struct ufs_ops ext2fs_ufsops = {
    167 	.uo_itimes = ext2fs_itimes,
    168 	.uo_update = ext2fs_update,
    169 	.uo_vfree = ext2fs_vfree,
    170 	.uo_unmark_vnode = (void (*)(vnode_t *))nullop,
    171 };
    172 
    173 /* Fill in the inode uid/gid from ext2 halves.  */
    174 static void
    175 ext2fs_set_inode_guid(struct inode *ip)
    176 {
    177 
    178 	ip->i_gid = ip->i_e2fs_gid;
    179 	ip->i_uid = ip->i_e2fs_uid;
    180 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
    181 		ip->i_gid |= ip->i_e2fs_gid_high << 16;
    182 		ip->i_uid |= ip->i_e2fs_uid_high << 16;
    183 	}
    184 }
    185 
    186 static int
    187 ext2fs_modcmd(modcmd_t cmd, void *arg)
    188 {
    189 	int error;
    190 
    191 	switch (cmd) {
    192 	case MODULE_CMD_INIT:
    193 		error = vfs_attach(&ext2fs_vfsops);
    194 		if (error != 0)
    195 			break;
    196 		sysctl_createv(&ext2fs_sysctl_log, 0, NULL, NULL,
    197 			       CTLFLAG_PERMANENT,
    198 			       CTLTYPE_NODE, "vfs", NULL,
    199 			       NULL, 0, NULL, 0,
    200 			       CTL_VFS, CTL_EOL);
    201 		sysctl_createv(&ext2fs_sysctl_log, 0, NULL, NULL,
    202 			       CTLFLAG_PERMANENT,
    203 			       CTLTYPE_NODE, "ext2fs",
    204 			       SYSCTL_DESCR("Linux EXT2FS file system"),
    205 			       NULL, 0, NULL, 0,
    206 			       CTL_VFS, 17, CTL_EOL);
    207 		/*
    208 		 * XXX the "17" above could be dynamic, thereby eliminating
    209 		 * one more instance of the "number to vfs" mapping problem,
    210 		 * but "17" is the order as taken from sys/mount.h
    211 		 */
    212 		break;
    213 	case MODULE_CMD_FINI:
    214 		error = vfs_detach(&ext2fs_vfsops);
    215 		if (error != 0)
    216 			break;
    217 		sysctl_teardown(&ext2fs_sysctl_log);
    218 		break;
    219 	default:
    220 		error = ENOTTY;
    221 		break;
    222 	}
    223 
    224 	return (error);
    225 }
    226 
    227 /*
    228  * XXX Same structure as FFS inodes?  Should we share a common pool?
    229  */
    230 struct pool ext2fs_inode_pool;
    231 struct pool ext2fs_dinode_pool;
    232 
    233 extern u_long ext2gennumber;
    234 
    235 void
    236 ext2fs_init(void)
    237 {
    238 
    239 	pool_init(&ext2fs_inode_pool, sizeof(struct inode), 0, 0, 0,
    240 	    "ext2fsinopl", &pool_allocator_nointr, IPL_NONE);
    241 	pool_init(&ext2fs_dinode_pool, sizeof(struct ext2fs_dinode), 0, 0, 0,
    242 	    "ext2dinopl", &pool_allocator_nointr, IPL_NONE);
    243 	ufs_init();
    244 }
    245 
    246 void
    247 ext2fs_reinit(void)
    248 {
    249 	ufs_reinit();
    250 }
    251 
    252 void
    253 ext2fs_done(void)
    254 {
    255 
    256 	ufs_done();
    257 	pool_destroy(&ext2fs_inode_pool);
    258 	pool_destroy(&ext2fs_dinode_pool);
    259 }
    260 
    261 /*
    262  * Called by main() when ext2fs is going to be mounted as root.
    263  *
    264  * Name is updated by mount(8) after booting.
    265  */
    266 #define ROOTNAME	"root_device"
    267 
    268 int
    269 ext2fs_mountroot(void)
    270 {
    271 	extern struct vnode *rootvp;
    272 	struct m_ext2fs *fs;
    273 	struct mount *mp;
    274 	struct ufsmount *ump;
    275 	int error;
    276 
    277 	if (device_class(root_device) != DV_DISK)
    278 		return (ENODEV);
    279 
    280 	if ((error = vfs_rootmountalloc(MOUNT_EXT2FS, "root_device", &mp))) {
    281 		vrele(rootvp);
    282 		return (error);
    283 	}
    284 
    285 	if ((error = ext2fs_mountfs(rootvp, mp)) != 0) {
    286 		vfs_unbusy(mp, false, NULL);
    287 		vfs_destroy(mp);
    288 		return (error);
    289 	}
    290 	mutex_enter(&mountlist_lock);
    291 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    292 	mutex_exit(&mountlist_lock);
    293 	ump = VFSTOUFS(mp);
    294 	fs = ump->um_e2fs;
    295 	memset(fs->e2fs_fsmnt, 0, sizeof(fs->e2fs_fsmnt));
    296 	(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
    297 	    sizeof(fs->e2fs_fsmnt) - 1, 0);
    298 	if (fs->e2fs.e2fs_rev > E2FS_REV0) {
    299 		memset(fs->e2fs.e2fs_fsmnt, 0, sizeof(fs->e2fs.e2fs_fsmnt));
    300 		(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
    301 		    sizeof(fs->e2fs.e2fs_fsmnt) - 1, 0);
    302 	}
    303 	(void)ext2fs_statvfs(mp, &mp->mnt_stat);
    304 	vfs_unbusy(mp, false, NULL);
    305 	setrootfstime((time_t)fs->e2fs.e2fs_wtime);
    306 	return (0);
    307 }
    308 
    309 /*
    310  * VFS Operations.
    311  *
    312  * mount system call
    313  */
    314 int
    315 ext2fs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    316 {
    317 	struct lwp *l = curlwp;
    318 	struct vnode *devvp;
    319 	struct ufs_args *args = data;
    320 	struct ufsmount *ump = NULL;
    321 	struct m_ext2fs *fs;
    322 	size_t size;
    323 	int error = 0, flags, update;
    324 	mode_t accessmode;
    325 
    326 	if (*data_len < sizeof *args)
    327 		return EINVAL;
    328 
    329 	if (mp->mnt_flag & MNT_GETARGS) {
    330 		ump = VFSTOUFS(mp);
    331 		if (ump == NULL)
    332 			return EIO;
    333 		memset(args, 0, sizeof *args);
    334 		args->fspec = NULL;
    335 		*data_len = sizeof *args;
    336 		return 0;
    337 	}
    338 
    339 	update = mp->mnt_flag & MNT_UPDATE;
    340 
    341 	/* Check arguments */
    342 	if (args->fspec != NULL) {
    343 		/*
    344 		 * Look up the name and verify that it's sane.
    345 		 */
    346 		error = namei_simple_user(args->fspec,
    347 					NSM_FOLLOW_NOEMULROOT, &devvp);
    348 		if (error != 0)
    349 			return (error);
    350 
    351 		if (!update) {
    352 			/*
    353 			 * Be sure this is a valid block device
    354 			 */
    355 			if (devvp->v_type != VBLK)
    356 				error = ENOTBLK;
    357 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
    358 				error = ENXIO;
    359 		} else {
    360 		        /*
    361 			 * Be sure we're still naming the same device
    362 			 * used for our initial mount
    363 			 */
    364 			ump = VFSTOUFS(mp);
    365 			if (devvp != ump->um_devvp) {
    366 				if (devvp->v_rdev != ump->um_devvp->v_rdev)
    367 					error = EINVAL;
    368 				else {
    369 					vrele(devvp);
    370 					devvp = ump->um_devvp;
    371 					vref(devvp);
    372 				}
    373 			}
    374 		}
    375 	} else {
    376 		if (!update) {
    377 			/* New mounts must have a filename for the device */
    378 			return (EINVAL);
    379 		} else {
    380 			ump = VFSTOUFS(mp);
    381 			devvp = ump->um_devvp;
    382 			vref(devvp);
    383 		}
    384 	}
    385 
    386 	/*
    387 	 * If mount by non-root, then verify that user has necessary
    388 	 * permissions on the device.
    389 	 *
    390 	 * Permission to update a mount is checked higher, so here we presume
    391 	 * updating the mount is okay (for example, as far as securelevel goes)
    392 	 * which leaves us with the normal check.
    393 	 */
    394 	if (error == 0) {
    395 		accessmode = VREAD;
    396 		if (update ?
    397 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
    398 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    399 			accessmode |= VWRITE;
    400 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    401 		error = genfs_can_mount(devvp, accessmode, l->l_cred);
    402 		VOP_UNLOCK(devvp, 0);
    403 	}
    404 
    405 	if (error) {
    406 		vrele(devvp);
    407 		return (error);
    408 	}
    409 
    410 	if (!update) {
    411 		int xflags;
    412 
    413 		if (mp->mnt_flag & MNT_RDONLY)
    414 			xflags = FREAD;
    415 		else
    416 			xflags = FREAD|FWRITE;
    417 		error = VOP_OPEN(devvp, xflags, FSCRED);
    418 		if (error)
    419 			goto fail;
    420 		error = ext2fs_mountfs(devvp, mp);
    421 		if (error) {
    422 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    423 			(void)VOP_CLOSE(devvp, xflags, NOCRED);
    424 			VOP_UNLOCK(devvp, 0);
    425 			goto fail;
    426 		}
    427 
    428 		ump = VFSTOUFS(mp);
    429 		fs = ump->um_e2fs;
    430 	} else {
    431 		/*
    432 		 * Update the mount.
    433 		 */
    434 
    435 		/*
    436 		 * The initial mount got a reference on this
    437 		 * device, so drop the one obtained via
    438 		 * namei(), above.
    439 		 */
    440 		vrele(devvp);
    441 
    442 		ump = VFSTOUFS(mp);
    443 		fs = ump->um_e2fs;
    444 		if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    445 			/*
    446 			 * Changing from r/w to r/o
    447 			 */
    448 			flags = WRITECLOSE;
    449 			if (mp->mnt_flag & MNT_FORCE)
    450 				flags |= FORCECLOSE;
    451 			error = ext2fs_flushfiles(mp, flags);
    452 			if (error == 0 &&
    453 			    ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
    454 			    (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
    455 				fs->e2fs.e2fs_state = E2FS_ISCLEAN;
    456 				(void) ext2fs_sbupdate(ump, MNT_WAIT);
    457 			}
    458 			if (error)
    459 				return (error);
    460 			fs->e2fs_ronly = 1;
    461 		}
    462 
    463 		if (mp->mnt_flag & MNT_RELOAD) {
    464 			error = ext2fs_reload(mp, l->l_cred);
    465 			if (error)
    466 				return (error);
    467 		}
    468 
    469 		if (fs->e2fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
    470 			/*
    471 			 * Changing from read-only to read/write
    472 			 */
    473 			fs->e2fs_ronly = 0;
    474 			if (fs->e2fs.e2fs_state == E2FS_ISCLEAN)
    475 				fs->e2fs.e2fs_state = 0;
    476 			else
    477 				fs->e2fs.e2fs_state = E2FS_ERRORS;
    478 			fs->e2fs_fmod = 1;
    479 		}
    480 		if (args->fspec == NULL)
    481 			return EINVAL;
    482 	}
    483 
    484 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
    485 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    486 	(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
    487 	    sizeof(fs->e2fs_fsmnt) - 1, &size);
    488 	memset(fs->e2fs_fsmnt + size, 0, sizeof(fs->e2fs_fsmnt) - size);
    489 	if (fs->e2fs.e2fs_rev > E2FS_REV0) {
    490 		(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
    491 		    sizeof(fs->e2fs.e2fs_fsmnt) - 1, &size);
    492 		memset(fs->e2fs.e2fs_fsmnt, 0,
    493 		    sizeof(fs->e2fs.e2fs_fsmnt) - size);
    494 	}
    495 	if (fs->e2fs_fmod != 0) {	/* XXX */
    496 		fs->e2fs_fmod = 0;
    497 		if (fs->e2fs.e2fs_state == 0)
    498 			fs->e2fs.e2fs_wtime = time_second;
    499 		else
    500 			printf("%s: file system not clean; please fsck(8)\n",
    501 				mp->mnt_stat.f_mntfromname);
    502 		(void) ext2fs_cgupdate(ump, MNT_WAIT);
    503 	}
    504 	return (error);
    505 
    506 fail:
    507 	vrele(devvp);
    508 	return (error);
    509 }
    510 
    511 /*
    512  * Reload all incore data for a filesystem (used after running fsck on
    513  * the root filesystem and finding things to fix). The filesystem must
    514  * be mounted read-only.
    515  *
    516  * Things to do to update the mount:
    517  *	1) invalidate all cached meta-data.
    518  *	2) re-read superblock from disk.
    519  *	3) re-read summary information from disk.
    520  *	4) invalidate all inactive vnodes.
    521  *	5) invalidate all cached file data.
    522  *	6) re-read inode data for all active vnodes.
    523  */
    524 int
    525 ext2fs_reload(struct mount *mountp, kauth_cred_t cred)
    526 {
    527 	struct lwp *l = curlwp;
    528 	struct vnode *vp, *mvp, *devvp;
    529 	struct inode *ip;
    530 	struct buf *bp;
    531 	struct m_ext2fs *fs;
    532 	struct ext2fs *newfs;
    533 	struct partinfo dpart;
    534 	int i, size, error;
    535 	void *cp;
    536 
    537 	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
    538 		return (EINVAL);
    539 
    540 	/*
    541 	 * Step 1: invalidate all cached meta-data.
    542 	 */
    543 	devvp = VFSTOUFS(mountp)->um_devvp;
    544 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    545 	error = vinvalbuf(devvp, 0, cred, l, 0, 0);
    546 	VOP_UNLOCK(devvp, 0);
    547 	if (error)
    548 		panic("ext2fs_reload: dirty1");
    549 	/*
    550 	 * Step 2: re-read superblock from disk.
    551 	 */
    552 	if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED) != 0)
    553 		size = DEV_BSIZE;
    554 	else
    555 		size = dpart.disklab->d_secsize;
    556 	error = bread(devvp, (daddr_t)(SBOFF / size), SBSIZE, NOCRED, 0, &bp);
    557 	if (error) {
    558 		brelse(bp, 0);
    559 		return (error);
    560 	}
    561 	newfs = (struct ext2fs *)bp->b_data;
    562 	error = ext2fs_checksb(newfs, (mountp->mnt_flag & MNT_RDONLY) != 0);
    563 	if (error) {
    564 		brelse(bp, 0);
    565 		return (error);
    566 	}
    567 
    568 	fs = VFSTOUFS(mountp)->um_e2fs;
    569 	/*
    570 	 * copy in new superblock, and compute in-memory values
    571 	 */
    572 	e2fs_sbload(newfs, &fs->e2fs);
    573 	fs->e2fs_ncg =
    574 	    howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
    575 	    fs->e2fs.e2fs_bpg);
    576 	/* XXX assume hw bsize = 512 */
    577 	fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
    578 	fs->e2fs_bsize = MINBSIZE << fs->e2fs.e2fs_log_bsize;
    579 	fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
    580 	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
    581 	fs->e2fs_bmask = ~fs->e2fs_qbmask;
    582 	fs->e2fs_ngdb =
    583 	    howmany(fs->e2fs_ncg, fs->e2fs_bsize / sizeof(struct ext2_gd));
    584 	fs->e2fs_ipb = fs->e2fs_bsize / EXT2_DINODE_SIZE(fs);
    585 	fs->e2fs_itpg = fs->e2fs.e2fs_ipg / fs->e2fs_ipb;
    586 
    587 	/*
    588 	 * Step 3: re-read summary information from disk.
    589 	 */
    590 
    591 	for (i = 0; i < fs->e2fs_ngdb; i++) {
    592 		error = bread(devvp ,
    593 		    fsbtodb(fs, fs->e2fs.e2fs_first_dblock +
    594 		    1 /* superblock */ + i),
    595 		    fs->e2fs_bsize, NOCRED, 0, &bp);
    596 		if (error) {
    597 			brelse(bp, 0);
    598 			return (error);
    599 		}
    600 		e2fs_cgload((struct ext2_gd *)bp->b_data,
    601 		    &fs->e2fs_gd[i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
    602 		    fs->e2fs_bsize);
    603 		brelse(bp, 0);
    604 	}
    605 
    606 	/* Allocate a marker vnode. */
    607 	if ((mvp = vnalloc(mountp)) == NULL)
    608 		return (ENOMEM);
    609 	/*
    610 	 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
    611 	 * and vclean() can be called indirectly
    612 	 */
    613 	mutex_enter(&mntvnode_lock);
    614 loop:
    615 	for (vp = TAILQ_FIRST(&mountp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
    616 		vmark(mvp, vp);
    617 		if (vp->v_mount != mountp || vismarker(vp))
    618 			continue;
    619 		/*
    620 		 * Step 4: invalidate all inactive vnodes.
    621 		 */
    622 		if (vrecycle(vp, &mntvnode_lock, l)) {
    623 			mutex_enter(&mntvnode_lock);
    624 			(void)vunmark(mvp);
    625 			goto loop;
    626 		}
    627 		/*
    628 		 * Step 5: invalidate all cached file data.
    629 		 */
    630 		mutex_enter(&vp->v_interlock);
    631 		mutex_exit(&mntvnode_lock);
    632 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
    633 			mutex_enter(&mntvnode_lock);
    634 			(void)vunmark(mvp);
    635 			goto loop;
    636 		}
    637 		if (vinvalbuf(vp, 0, cred, l, 0, 0))
    638 			panic("ext2fs_reload: dirty2");
    639 		/*
    640 		 * Step 6: re-read inode data for all active vnodes.
    641 		 */
    642 		ip = VTOI(vp);
    643 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    644 		    (int)fs->e2fs_bsize, NOCRED, 0, &bp);
    645 		if (error) {
    646 			vput(vp);
    647 			mutex_enter(&mntvnode_lock);
    648 			(void)vunmark(mvp);
    649 			break;
    650 		}
    651 		cp = (char *)bp->b_data +
    652 		    (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE(fs));
    653 		e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
    654 		ext2fs_set_inode_guid(ip);
    655 		brelse(bp, 0);
    656 		vput(vp);
    657 		mutex_enter(&mntvnode_lock);
    658 	}
    659 	mutex_exit(&mntvnode_lock);
    660 	vnfree(mvp);
    661 	return (error);
    662 }
    663 
    664 /*
    665  * Common code for mount and mountroot
    666  */
    667 int
    668 ext2fs_mountfs(struct vnode *devvp, struct mount *mp)
    669 {
    670 	struct lwp *l = curlwp;
    671 	struct ufsmount *ump;
    672 	struct buf *bp;
    673 	struct ext2fs *fs;
    674 	struct m_ext2fs *m_fs;
    675 	dev_t dev;
    676 	struct partinfo dpart;
    677 	int error, i, size, ronly;
    678 	kauth_cred_t cred;
    679 	struct proc *p;
    680 
    681 	dev = devvp->v_rdev;
    682 	p = l ? l->l_proc : NULL;
    683 	cred = l ? l->l_cred : NOCRED;
    684 
    685 	/* Flush out any old buffers remaining from a previous use. */
    686 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    687 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
    688 	VOP_UNLOCK(devvp, 0);
    689 	if (error)
    690 		return (error);
    691 
    692 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    693 	if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred) != 0)
    694 		size = DEV_BSIZE;
    695 	else
    696 		size = dpart.disklab->d_secsize;
    697 
    698 	bp = NULL;
    699 	ump = NULL;
    700 
    701 #ifdef DEBUG_EXT2
    702 	printf("ext2 sb size: %d\n", sizeof(struct ext2fs));
    703 #endif
    704 	error = bread(devvp, (SBOFF / size), SBSIZE, cred, 0, &bp);
    705 	if (error)
    706 		goto out;
    707 	fs = (struct ext2fs *)bp->b_data;
    708 	error = ext2fs_checksb(fs, ronly);
    709 	if (error)
    710 		goto out;
    711 	ump = malloc(sizeof(*ump), M_UFSMNT, M_WAITOK);
    712 	memset(ump, 0, sizeof(*ump));
    713 	ump->um_fstype = UFS1;
    714 	ump->um_ops = &ext2fs_ufsops;
    715 	ump->um_e2fs = malloc(sizeof(struct m_ext2fs), M_UFSMNT, M_WAITOK);
    716 	memset(ump->um_e2fs, 0, sizeof(struct m_ext2fs));
    717 	e2fs_sbload((struct ext2fs *)bp->b_data, &ump->um_e2fs->e2fs);
    718 	brelse(bp, 0);
    719 	bp = NULL;
    720 	m_fs = ump->um_e2fs;
    721 	m_fs->e2fs_ronly = ronly;
    722 
    723 #ifdef DEBUG_EXT2
    724 	printf("ext2 ino size %d\n", EXT2_DINODE_SIZE(m_fs));
    725 #endif
    726 	if (ronly == 0) {
    727 		if (m_fs->e2fs.e2fs_state == E2FS_ISCLEAN)
    728 			m_fs->e2fs.e2fs_state = 0;
    729 		else
    730 			m_fs->e2fs.e2fs_state = E2FS_ERRORS;
    731 		m_fs->e2fs_fmod = 1;
    732 	}
    733 
    734 	/* compute dynamic sb infos */
    735 	m_fs->e2fs_ncg =
    736 	    howmany(m_fs->e2fs.e2fs_bcount - m_fs->e2fs.e2fs_first_dblock,
    737 	    m_fs->e2fs.e2fs_bpg);
    738 	/* XXX assume hw bsize = 512 */
    739 	m_fs->e2fs_fsbtodb = m_fs->e2fs.e2fs_log_bsize + 1;
    740 	m_fs->e2fs_bsize = MINBSIZE << m_fs->e2fs.e2fs_log_bsize;
    741 	m_fs->e2fs_bshift = LOG_MINBSIZE + m_fs->e2fs.e2fs_log_bsize;
    742 	m_fs->e2fs_qbmask = m_fs->e2fs_bsize - 1;
    743 	m_fs->e2fs_bmask = ~m_fs->e2fs_qbmask;
    744 	m_fs->e2fs_ngdb =
    745 	    howmany(m_fs->e2fs_ncg, m_fs->e2fs_bsize / sizeof(struct ext2_gd));
    746 	m_fs->e2fs_ipb = m_fs->e2fs_bsize / EXT2_DINODE_SIZE(m_fs);
    747 	m_fs->e2fs_itpg = m_fs->e2fs.e2fs_ipg / m_fs->e2fs_ipb;
    748 
    749 	m_fs->e2fs_gd = malloc(m_fs->e2fs_ngdb * m_fs->e2fs_bsize,
    750 	    M_UFSMNT, M_WAITOK);
    751 	for (i = 0; i < m_fs->e2fs_ngdb; i++) {
    752 		error = bread(devvp ,
    753 		    fsbtodb(m_fs, m_fs->e2fs.e2fs_first_dblock +
    754 		    1 /* superblock */ + i),
    755 		    m_fs->e2fs_bsize, NOCRED, 0, &bp);
    756 		if (error) {
    757 			free(m_fs->e2fs_gd, M_UFSMNT);
    758 			goto out;
    759 		}
    760 		e2fs_cgload((struct ext2_gd *)bp->b_data,
    761 		    &m_fs->e2fs_gd[
    762 			i * m_fs->e2fs_bsize / sizeof(struct ext2_gd)],
    763 		    m_fs->e2fs_bsize);
    764 		brelse(bp, 0);
    765 		bp = NULL;
    766 	}
    767 
    768 	mp->mnt_data = ump;
    769 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
    770 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_EXT2FS);
    771 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    772 	mp->mnt_stat.f_namemax = EXT2FS_MAXNAMLEN;
    773 	mp->mnt_flag |= MNT_LOCAL;
    774 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
    775 	mp->mnt_fs_bshift = m_fs->e2fs_bshift;
    776 	mp->mnt_iflag |= IMNT_DTYPE;
    777 	ump->um_flags = 0;
    778 	ump->um_mountp = mp;
    779 	ump->um_dev = dev;
    780 	ump->um_devvp = devvp;
    781 	ump->um_nindir = NINDIR(m_fs);
    782 	ump->um_lognindir = ffs(NINDIR(m_fs)) - 1;
    783 	ump->um_bptrtodb = m_fs->e2fs_fsbtodb;
    784 	ump->um_seqinc = 1; /* no frags */
    785 	ump->um_maxsymlinklen = EXT2_MAXSYMLINKLEN;
    786 	ump->um_dirblksiz = m_fs->e2fs_bsize;
    787 	ump->um_maxfilesize = ((uint64_t)0x80000000 * m_fs->e2fs_bsize - 1);
    788 	devvp->v_specmountpoint = mp;
    789 	return (0);
    790 
    791 out:
    792 	KASSERT(bp != NULL);
    793 	brelse(bp, 0);
    794 	if (ump) {
    795 		free(ump->um_e2fs, M_UFSMNT);
    796 		free(ump, M_UFSMNT);
    797 		mp->mnt_data = NULL;
    798 	}
    799 	return (error);
    800 }
    801 
    802 /*
    803  * unmount system call
    804  */
    805 int
    806 ext2fs_unmount(struct mount *mp, int mntflags)
    807 {
    808 	struct ufsmount *ump;
    809 	struct m_ext2fs *fs;
    810 	int error, flags;
    811 
    812 	flags = 0;
    813 	if (mntflags & MNT_FORCE)
    814 		flags |= FORCECLOSE;
    815 	if ((error = ext2fs_flushfiles(mp, flags)) != 0)
    816 		return (error);
    817 	ump = VFSTOUFS(mp);
    818 	fs = ump->um_e2fs;
    819 	if (fs->e2fs_ronly == 0 &&
    820 		ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
    821 		(fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
    822 		fs->e2fs.e2fs_state = E2FS_ISCLEAN;
    823 		(void) ext2fs_sbupdate(ump, MNT_WAIT);
    824 	}
    825 	if (ump->um_devvp->v_type != VBAD)
    826 		ump->um_devvp->v_specmountpoint = NULL;
    827 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    828 	error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
    829 	    NOCRED);
    830 	vput(ump->um_devvp);
    831 	free(fs->e2fs_gd, M_UFSMNT);
    832 	free(fs, M_UFSMNT);
    833 	free(ump, M_UFSMNT);
    834 	mp->mnt_data = NULL;
    835 	mp->mnt_flag &= ~MNT_LOCAL;
    836 	return (error);
    837 }
    838 
    839 /*
    840  * Flush out all the files in a filesystem.
    841  */
    842 int
    843 ext2fs_flushfiles(struct mount *mp, int flags)
    844 {
    845 	extern int doforce;
    846 	int error;
    847 
    848 	if (!doforce)
    849 		flags &= ~FORCECLOSE;
    850 	error = vflush(mp, NULLVP, flags);
    851 	return (error);
    852 }
    853 
    854 /*
    855  * Get file system statistics.
    856  */
    857 int
    858 ext2fs_statvfs(struct mount *mp, struct statvfs *sbp)
    859 {
    860 	struct ufsmount *ump;
    861 	struct m_ext2fs *fs;
    862 	uint32_t overhead, overhead_per_group, ngdb;
    863 	int i, ngroups;
    864 
    865 	ump = VFSTOUFS(mp);
    866 	fs = ump->um_e2fs;
    867 	if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
    868 		panic("ext2fs_statvfs");
    869 
    870 	/*
    871 	 * Compute the overhead (FS structures)
    872 	 */
    873 	overhead_per_group =
    874 	    1 /* block bitmap */ +
    875 	    1 /* inode bitmap */ +
    876 	    fs->e2fs_itpg;
    877 	overhead = fs->e2fs.e2fs_first_dblock +
    878 	    fs->e2fs_ncg * overhead_per_group;
    879 	if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
    880 	    fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
    881 		for (i = 0, ngroups = 0; i < fs->e2fs_ncg; i++) {
    882 			if (cg_has_sb(i))
    883 				ngroups++;
    884 		}
    885 	} else {
    886 		ngroups = fs->e2fs_ncg;
    887 	}
    888 	ngdb = fs->e2fs_ngdb;
    889 	if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
    890 	    fs->e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE)
    891 		ngdb += fs->e2fs.e2fs_reserved_ngdb;
    892 	overhead += ngroups * (1 /* superblock */ + ngdb);
    893 
    894 	sbp->f_bsize = fs->e2fs_bsize;
    895 	sbp->f_frsize = MINBSIZE << fs->e2fs.e2fs_fsize;
    896 	sbp->f_iosize = fs->e2fs_bsize;
    897 	sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
    898 	sbp->f_bfree = fs->e2fs.e2fs_fbcount;
    899 	sbp->f_bresvd = fs->e2fs.e2fs_rbcount;
    900 	if (sbp->f_bfree > sbp->f_bresvd)
    901 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
    902 	else
    903 		sbp->f_bavail = 0;
    904 	sbp->f_files =  fs->e2fs.e2fs_icount;
    905 	sbp->f_ffree = fs->e2fs.e2fs_ficount;
    906 	sbp->f_favail = fs->e2fs.e2fs_ficount;
    907 	sbp->f_fresvd = 0;
    908 	copy_statvfs_info(sbp, mp);
    909 	return (0);
    910 }
    911 
    912 /*
    913  * Go through the disk queues to initiate sandbagged IO;
    914  * go through the inodes to write those that have been modified;
    915  * initiate the writing of the super block if it has been modified.
    916  *
    917  * Note: we are always called with the filesystem marked `MPBUSY'.
    918  */
    919 int
    920 ext2fs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
    921 {
    922 	struct vnode *vp, *mvp;
    923 	struct inode *ip;
    924 	struct ufsmount *ump = VFSTOUFS(mp);
    925 	struct m_ext2fs *fs;
    926 	int error, allerror = 0;
    927 
    928 	fs = ump->um_e2fs;
    929 	if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) {	/* XXX */
    930 		printf("fs = %s\n", fs->e2fs_fsmnt);
    931 		panic("update: rofs mod");
    932 	}
    933 
    934 	/* Allocate a marker vnode. */
    935 	if ((mvp = vnalloc(mp)) == NULL)
    936 		return (ENOMEM);
    937 
    938 	/*
    939 	 * Write back each (modified) inode.
    940 	 */
    941 	mutex_enter(&mntvnode_lock);
    942 loop:
    943 	/*
    944 	 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
    945 	 * and vclean() can be called indirectly
    946 	 */
    947 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
    948 		vmark(mvp, vp);
    949 		if (vp->v_mount != mp || vismarker(vp))
    950 			continue;
    951 		mutex_enter(&vp->v_interlock);
    952 		ip = VTOI(vp);
    953 		if (ip == NULL || (vp->v_iflag & (VI_XLOCK|VI_CLEAN)) != 0 ||
    954 		    vp->v_type == VNON ||
    955 		    ((ip->i_flag &
    956 		      (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
    957 		     LIST_EMPTY(&vp->v_dirtyblkhd) &&
    958 		     UVM_OBJ_IS_CLEAN(&vp->v_uobj)))
    959 		{
    960 			mutex_exit(&vp->v_interlock);
    961 			continue;
    962 		}
    963 		mutex_exit(&mntvnode_lock);
    964 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    965 		if (error) {
    966 			mutex_enter(&mntvnode_lock);
    967 			if (error == ENOENT) {
    968 				mutex_enter(&mntvnode_lock);
    969 				(void)vunmark(mvp);
    970 				goto loop;
    971 			}
    972 			continue;
    973 		}
    974 		if (vp->v_type == VREG && waitfor == MNT_LAZY)
    975 			error = ext2fs_update(vp, NULL, NULL, 0);
    976 		else
    977 			error = VOP_FSYNC(vp, cred,
    978 			    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0);
    979 		if (error)
    980 			allerror = error;
    981 		vput(vp);
    982 		mutex_enter(&mntvnode_lock);
    983 	}
    984 	mutex_exit(&mntvnode_lock);
    985 	vnfree(mvp);
    986 	/*
    987 	 * Force stale file system control information to be flushed.
    988 	 */
    989 	if (waitfor != MNT_LAZY) {
    990 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    991 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
    992 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
    993 			allerror = error;
    994 		VOP_UNLOCK(ump->um_devvp, 0);
    995 	}
    996 	/*
    997 	 * Write back modified superblock.
    998 	 */
    999 	if (fs->e2fs_fmod != 0) {
   1000 		fs->e2fs_fmod = 0;
   1001 		fs->e2fs.e2fs_wtime = time_second;
   1002 		if ((error = ext2fs_cgupdate(ump, waitfor)))
   1003 			allerror = error;
   1004 	}
   1005 	return (allerror);
   1006 }
   1007 
   1008 /*
   1009  * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
   1010  * in from disk.  If it is in core, wait for the lock bit to clear, then
   1011  * return the inode locked.  Detection and handling of mount points must be
   1012  * done by the calling routine.
   1013  */
   1014 int
   1015 ext2fs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
   1016 {
   1017 	struct m_ext2fs *fs;
   1018 	struct inode *ip;
   1019 	struct ufsmount *ump;
   1020 	struct buf *bp;
   1021 	struct vnode *vp;
   1022 	dev_t dev;
   1023 	int error;
   1024 	void *cp;
   1025 
   1026 	ump = VFSTOUFS(mp);
   1027 	dev = ump->um_dev;
   1028 retry:
   1029 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1030 		return (0);
   1031 
   1032 	/* Allocate a new vnode/inode. */
   1033 	if ((error = getnewvnode(VT_EXT2FS, mp, ext2fs_vnodeop_p, &vp)) != 0) {
   1034 		*vpp = NULL;
   1035 		return (error);
   1036 	}
   1037 	ip = pool_get(&ext2fs_inode_pool, PR_WAITOK);
   1038 
   1039 	mutex_enter(&ufs_hashlock);
   1040 	if ((*vpp = ufs_ihashget(dev, ino, 0)) != NULL) {
   1041 		mutex_exit(&ufs_hashlock);
   1042 		ungetnewvnode(vp);
   1043 		pool_put(&ext2fs_inode_pool, ip);
   1044 		goto retry;
   1045 	}
   1046 
   1047 	vp->v_vflag |= VV_LOCKSWORK;
   1048 
   1049 	memset(ip, 0, sizeof(struct inode));
   1050 	vp->v_data = ip;
   1051 	ip->i_vnode = vp;
   1052 	ip->i_ump = ump;
   1053 	ip->i_e2fs = fs = ump->um_e2fs;
   1054 	ip->i_dev = dev;
   1055 	ip->i_number = ino;
   1056 	ip->i_e2fs_last_lblk = 0;
   1057 	ip->i_e2fs_last_blk = 0;
   1058 	genfs_node_init(vp, &ext2fs_genfsops);
   1059 
   1060 	/*
   1061 	 * Put it onto its hash chain and lock it so that other requests for
   1062 	 * this inode will block if they arrive while we are sleeping waiting
   1063 	 * for old data structures to be purged or for the contents of the
   1064 	 * disk portion of this inode to be read.
   1065 	 */
   1066 
   1067 	ufs_ihashins(ip);
   1068 	mutex_exit(&ufs_hashlock);
   1069 
   1070 	/* Read in the disk contents for the inode, copy into the inode. */
   1071 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
   1072 	    (int)fs->e2fs_bsize, NOCRED, 0, &bp);
   1073 	if (error) {
   1074 
   1075 		/*
   1076 		 * The inode does not contain anything useful, so it would
   1077 		 * be misleading to leave it on its hash chain. With mode
   1078 		 * still zero, it will be unlinked and returned to the free
   1079 		 * list by vput().
   1080 		 */
   1081 
   1082 		vput(vp);
   1083 		brelse(bp, 0);
   1084 		*vpp = NULL;
   1085 		return (error);
   1086 	}
   1087 	cp = (char *)bp->b_data + (ino_to_fsbo(fs, ino) * EXT2_DINODE_SIZE(fs));
   1088 	ip->i_din.e2fs_din = pool_get(&ext2fs_dinode_pool, PR_WAITOK);
   1089 	e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
   1090 	ext2fs_set_inode_guid(ip);
   1091 	brelse(bp, 0);
   1092 
   1093 	/* If the inode was deleted, reset all fields */
   1094 	if (ip->i_e2fs_dtime != 0) {
   1095 		ip->i_e2fs_mode = ip->i_e2fs_nblock = 0;
   1096 		(void)ext2fs_setsize(ip, 0);
   1097 		memset(ip->i_e2fs_blocks, 0, sizeof(ip->i_e2fs_blocks));
   1098 	}
   1099 
   1100 	/*
   1101 	 * Initialize the vnode from the inode, check for aliases.
   1102 	 */
   1103 
   1104 	error = ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
   1105 	if (error) {
   1106 		vput(vp);
   1107 		*vpp = NULL;
   1108 		return (error);
   1109 	}
   1110 	/*
   1111 	 * Finish inode initialization now that aliasing has been resolved.
   1112 	 */
   1113 
   1114 	ip->i_devvp = ump->um_devvp;
   1115 	VREF(ip->i_devvp);
   1116 
   1117 	/*
   1118 	 * Set up a generation number for this inode if it does not
   1119 	 * already have one. This should only happen on old filesystems.
   1120 	 */
   1121 
   1122 	if (ip->i_e2fs_gen == 0) {
   1123 		if (++ext2gennumber < (u_long)time_second)
   1124 			ext2gennumber = time_second;
   1125 		ip->i_e2fs_gen = ext2gennumber;
   1126 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
   1127 			ip->i_flag |= IN_MODIFIED;
   1128 	}
   1129 	uvm_vnp_setsize(vp, ext2fs_size(ip));
   1130 	*vpp = vp;
   1131 	return (0);
   1132 }
   1133 
   1134 /*
   1135  * File handle to vnode
   1136  *
   1137  * Have to be really careful about stale file handles:
   1138  * - check that the inode number is valid
   1139  * - call ext2fs_vget() to get the locked inode
   1140  * - check for an unallocated inode (i_mode == 0)
   1141  */
   1142 int
   1143 ext2fs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
   1144 {
   1145 	struct inode *ip;
   1146 	struct vnode *nvp;
   1147 	int error;
   1148 	struct ufid ufh;
   1149 	struct m_ext2fs *fs;
   1150 
   1151 	if (fhp->fid_len != sizeof(struct ufid))
   1152 		return EINVAL;
   1153 
   1154 	memcpy(&ufh, fhp, sizeof(struct ufid));
   1155 	fs = VFSTOUFS(mp)->um_e2fs;
   1156 	if ((ufh.ufid_ino < EXT2_FIRSTINO && ufh.ufid_ino != EXT2_ROOTINO) ||
   1157 		ufh.ufid_ino >= fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
   1158 		return (ESTALE);
   1159 
   1160 	if ((error = VFS_VGET(mp, ufh.ufid_ino, &nvp)) != 0) {
   1161 		*vpp = NULLVP;
   1162 		return (error);
   1163 	}
   1164 	ip = VTOI(nvp);
   1165 	if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0 ||
   1166 		ip->i_e2fs_gen != ufh.ufid_gen) {
   1167 		vput(nvp);
   1168 		*vpp = NULLVP;
   1169 		return (ESTALE);
   1170 	}
   1171 	*vpp = nvp;
   1172 	return (0);
   1173 }
   1174 
   1175 /*
   1176  * Vnode pointer to File handle
   1177  */
   1178 /* ARGSUSED */
   1179 int
   1180 ext2fs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
   1181 {
   1182 	struct inode *ip;
   1183 	struct ufid ufh;
   1184 
   1185 	if (*fh_size < sizeof(struct ufid)) {
   1186 		*fh_size = sizeof(struct ufid);
   1187 		return E2BIG;
   1188 	}
   1189 	*fh_size = sizeof(struct ufid);
   1190 
   1191 	ip = VTOI(vp);
   1192 	memset(&ufh, 0, sizeof(ufh));
   1193 	ufh.ufid_len = sizeof(struct ufid);
   1194 	ufh.ufid_ino = ip->i_number;
   1195 	ufh.ufid_gen = ip->i_e2fs_gen;
   1196 	memcpy(fhp, &ufh, sizeof(ufh));
   1197 	return (0);
   1198 }
   1199 
   1200 /*
   1201  * Write a superblock and associated information back to disk.
   1202  */
   1203 int
   1204 ext2fs_sbupdate(struct ufsmount *mp, int waitfor)
   1205 {
   1206 	struct m_ext2fs *fs = mp->um_e2fs;
   1207 	struct buf *bp;
   1208 	int error = 0;
   1209 
   1210 	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
   1211 	e2fs_sbsave(&fs->e2fs, (struct ext2fs*)bp->b_data);
   1212 	if (waitfor == MNT_WAIT)
   1213 		error = bwrite(bp);
   1214 	else
   1215 		bawrite(bp);
   1216 	return (error);
   1217 }
   1218 
   1219 int
   1220 ext2fs_cgupdate(struct ufsmount *mp, int waitfor)
   1221 {
   1222 	struct m_ext2fs *fs = mp->um_e2fs;
   1223 	struct buf *bp;
   1224 	int i, error = 0, allerror = 0;
   1225 
   1226 	allerror = ext2fs_sbupdate(mp, waitfor);
   1227 	for (i = 0; i < fs->e2fs_ngdb; i++) {
   1228 		bp = getblk(mp->um_devvp, fsbtodb(fs,
   1229 		    fs->e2fs.e2fs_first_dblock +
   1230 		    1 /* superblock */ + i), fs->e2fs_bsize, 0, 0);
   1231 		e2fs_cgsave(&fs->e2fs_gd[
   1232 		    i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
   1233 		    (struct ext2_gd *)bp->b_data, fs->e2fs_bsize);
   1234 		if (waitfor == MNT_WAIT)
   1235 			error = bwrite(bp);
   1236 		else
   1237 			bawrite(bp);
   1238 	}
   1239 
   1240 	if (!allerror && error)
   1241 		allerror = error;
   1242 	return (allerror);
   1243 }
   1244 
   1245 static int
   1246 ext2fs_checksb(struct ext2fs *fs, int ronly)
   1247 {
   1248 
   1249 	if (fs2h16(fs->e2fs_magic) != E2FS_MAGIC) {
   1250 		return (EINVAL);		/* XXX needs translation */
   1251 	}
   1252 	if (fs2h32(fs->e2fs_rev) > E2FS_REV1) {
   1253 #ifdef DIAGNOSTIC
   1254 		printf("Ext2 fs: unsupported revision number: %x\n",
   1255 		    fs2h32(fs->e2fs_rev));
   1256 #endif
   1257 		return (EINVAL);		/* XXX needs translation */
   1258 	}
   1259 	if (fs2h32(fs->e2fs_log_bsize) > 2) { /* block size = 1024|2048|4096 */
   1260 #ifdef DIAGNOSTIC
   1261 		printf("Ext2 fs: bad block size: %d "
   1262 		    "(expected <= 2 for ext2 fs)\n",
   1263 		    fs2h32(fs->e2fs_log_bsize));
   1264 #endif
   1265 		return (EINVAL);	   /* XXX needs translation */
   1266 	}
   1267 	if (fs2h32(fs->e2fs_rev) > E2FS_REV0) {
   1268 		if (fs2h32(fs->e2fs_first_ino) != EXT2_FIRSTINO) {
   1269 			printf("Ext2 fs: unsupported first inode position\n");
   1270 			return (EINVAL);      /* XXX needs translation */
   1271 		}
   1272 		if (fs2h32(fs->e2fs_features_incompat) &
   1273 		    ~EXT2F_INCOMPAT_SUPP) {
   1274 			printf("Ext2 fs: unsupported optional feature\n");
   1275 			return (EINVAL);      /* XXX needs translation */
   1276 		}
   1277 		if (!ronly && fs2h32(fs->e2fs_features_rocompat) &
   1278 		    ~EXT2F_ROCOMPAT_SUPP) {
   1279 			return (EROFS);      /* XXX needs translation */
   1280 		}
   1281 	}
   1282 	return (0);
   1283 }
   1284