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