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