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