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