Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_vfsops.c revision 1.215
      1 /*	$NetBSD: ext2fs_vfsops.c,v 1.215 2020/01/17 20:08:10 ad 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.215 2020/01/17 20:08:10 ad 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/file.h>
     81 #include <sys/disklabel.h>
     82 #include <sys/ioctl.h>
     83 #include <sys/errno.h>
     84 #include <sys/pool.h>
     85 #include <sys/lock.h>
     86 #include <sys/conf.h>
     87 #include <sys/kauth.h>
     88 #include <sys/module.h>
     89 
     90 #include <miscfs/genfs/genfs.h>
     91 #include <miscfs/specfs/specdev.h>
     92 
     93 #include <ufs/ufs/quota.h>
     94 #include <ufs/ufs/ufsmount.h>
     95 #include <ufs/ufs/inode.h>
     96 #include <ufs/ufs/dir.h>
     97 #include <ufs/ufs/ufs_extern.h>
     98 
     99 #include <ufs/ext2fs/ext2fs.h>
    100 #include <ufs/ext2fs/ext2fs_dir.h>
    101 #include <ufs/ext2fs/ext2fs_extern.h>
    102 
    103 MODULE(MODULE_CLASS_VFS, ext2fs, "ufs");
    104 
    105 int ext2fs_sbupdate(struct ufsmount *, int);
    106 static int ext2fs_sbfill(struct m_ext2fs *, int);
    107 
    108 static struct sysctllog *ext2fs_sysctl_log;
    109 
    110 extern const struct vnodeopv_desc ext2fs_vnodeop_opv_desc;
    111 extern const struct vnodeopv_desc ext2fs_specop_opv_desc;
    112 extern const struct vnodeopv_desc ext2fs_fifoop_opv_desc;
    113 
    114 const struct vnodeopv_desc * const ext2fs_vnodeopv_descs[] = {
    115 	&ext2fs_vnodeop_opv_desc,
    116 	&ext2fs_specop_opv_desc,
    117 	&ext2fs_fifoop_opv_desc,
    118 	NULL,
    119 };
    120 
    121 struct vfsops ext2fs_vfsops = {
    122 	.vfs_name = MOUNT_EXT2FS,
    123 	.vfs_min_mount_data = sizeof (struct ufs_args),
    124 	.vfs_mount = ext2fs_mount,
    125 	.vfs_start = ufs_start,
    126 	.vfs_unmount = ext2fs_unmount,
    127 	.vfs_root = ufs_root,
    128 	.vfs_quotactl = ufs_quotactl,
    129 	.vfs_statvfs = ext2fs_statvfs,
    130 	.vfs_sync = ext2fs_sync,
    131 	.vfs_vget = ufs_vget,
    132 	.vfs_loadvnode = ext2fs_loadvnode,
    133 	.vfs_newvnode = ext2fs_newvnode,
    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 	.gop_putrange = genfs_gop_putrange,
    155 };
    156 
    157 static const struct ufs_ops ext2fs_ufsops = {
    158 	.uo_itimes = ext2fs_itimes,
    159 	.uo_update = ext2fs_update,
    160 	.uo_bufrd = ext2fs_bufrd,
    161 	.uo_bufwr = ext2fs_bufwr,
    162 };
    163 
    164 /* Fill in the inode uid/gid from ext2 halves.  */
    165 void
    166 ext2fs_set_inode_guid(struct inode *ip)
    167 {
    168 
    169 	ip->i_gid = ip->i_e2fs_gid;
    170 	ip->i_uid = ip->i_e2fs_uid;
    171 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
    172 		ip->i_gid |= ip->i_e2fs_gid_high << 16;
    173 		ip->i_uid |= ip->i_e2fs_uid_high << 16;
    174 	}
    175 }
    176 
    177 static int
    178 ext2fs_modcmd(modcmd_t cmd, void *arg)
    179 {
    180 	int error;
    181 
    182 	switch (cmd) {
    183 	case MODULE_CMD_INIT:
    184 		error = vfs_attach(&ext2fs_vfsops);
    185 		if (error != 0)
    186 			break;
    187 		sysctl_createv(&ext2fs_sysctl_log, 0, NULL, NULL,
    188 			       CTLFLAG_PERMANENT,
    189 			       CTLTYPE_NODE, "ext2fs",
    190 			       SYSCTL_DESCR("Linux EXT2FS file system"),
    191 			       NULL, 0, NULL, 0,
    192 			       CTL_VFS, 17, CTL_EOL);
    193 		/*
    194 		 * XXX the "17" above could be dynamic, thereby eliminating
    195 		 * one more instance of the "number to vfs" mapping problem,
    196 		 * but "17" is the order as taken from sys/mount.h
    197 		 */
    198 		break;
    199 	case MODULE_CMD_FINI:
    200 		error = vfs_detach(&ext2fs_vfsops);
    201 		if (error != 0)
    202 			break;
    203 		sysctl_teardown(&ext2fs_sysctl_log);
    204 		break;
    205 	default:
    206 		error = ENOTTY;
    207 		break;
    208 	}
    209 
    210 	return error;
    211 }
    212 
    213 /*
    214  * XXX Same structure as FFS inodes?  Should we share a common pool?
    215  */
    216 struct pool ext2fs_inode_pool;
    217 
    218 extern u_long ext2gennumber;
    219 
    220 void
    221 ext2fs_init(void)
    222 {
    223 
    224 	pool_init(&ext2fs_inode_pool, sizeof(struct inode), 0, 0, 0,
    225 	    "ext2fsinopl", &pool_allocator_nointr, IPL_NONE);
    226 	ufs_init();
    227 }
    228 
    229 void
    230 ext2fs_reinit(void)
    231 {
    232 	ufs_reinit();
    233 }
    234 
    235 void
    236 ext2fs_done(void)
    237 {
    238 
    239 	ufs_done();
    240 	pool_destroy(&ext2fs_inode_pool);
    241 }
    242 
    243 static void
    244 ext2fs_sb_setmountinfo(struct m_ext2fs *fs, struct mount *mp)
    245 {
    246 	(void)strlcpy(fs->e2fs_fsmnt, mp->mnt_stat.f_mntonname,
    247             sizeof(fs->e2fs_fsmnt));
    248 	if (fs->e2fs_ronly == 0 && fs->e2fs.e2fs_rev > E2FS_REV0) {
    249 		(void)strlcpy(fs->e2fs.e2fs_fsmnt, mp->mnt_stat.f_mntonname,
    250 		    sizeof(fs->e2fs.e2fs_fsmnt));
    251 
    252 		fs->e2fs.e2fs_mtime = time_second;
    253 		fs->e2fs.e2fs_mnt_count++;
    254 
    255 		fs->e2fs_fmod = 1;
    256 	}
    257 }
    258 
    259 /*
    260  * Called by main() when ext2fs is going to be mounted as root.
    261  *
    262  * Name is updated by mount(8) after booting.
    263  */
    264 
    265 int
    266 ext2fs_mountroot(void)
    267 {
    268 	extern struct vnode *rootvp;
    269 	struct m_ext2fs *fs;
    270 	struct mount *mp;
    271 	struct ufsmount *ump;
    272 	int error;
    273 
    274 	if (device_class(root_device) != DV_DISK)
    275 		return ENODEV;
    276 
    277 	if ((error = vfs_rootmountalloc(MOUNT_EXT2FS, "root_device", &mp))) {
    278 		vrele(rootvp);
    279 		return error;
    280 	}
    281 
    282 	if ((error = ext2fs_mountfs(rootvp, mp)) != 0) {
    283 		vfs_unbusy(mp);
    284 		vfs_rele(mp);
    285 		return error;
    286 	}
    287 	mountlist_append(mp);
    288 	ump = VFSTOUFS(mp);
    289 	fs = ump->um_e2fs;
    290 	ext2fs_sb_setmountinfo(fs, mp);
    291 	(void)ext2fs_statvfs(mp, &mp->mnt_stat);
    292 	vfs_unbusy(mp);
    293 	setrootfstime((time_t)fs->e2fs.e2fs_wtime);
    294 	return 0;
    295 }
    296 
    297 /*
    298  * VFS Operations.
    299  *
    300  * mount system call
    301  */
    302 int
    303 ext2fs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    304 {
    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 if (bdevsw_lookup(devvp->v_rdev) == NULL)
    347 				error = ENXIO;
    348 		} else {
    349 		        /*
    350 			 * Be sure we're still naming the same device
    351 			 * used for our initial mount
    352 			 */
    353 			ump = VFSTOUFS(mp);
    354 			if (devvp != ump->um_devvp) {
    355 				if (devvp->v_rdev != ump->um_devvp->v_rdev)
    356 					error = EINVAL;
    357 				else {
    358 					vrele(devvp);
    359 					devvp = ump->um_devvp;
    360 					vref(devvp);
    361 				}
    362 			}
    363 		}
    364 	} else {
    365 		if (!update) {
    366 			/* New mounts must have a filename for the device */
    367 			return EINVAL;
    368 		} else {
    369 			ump = VFSTOUFS(mp);
    370 			devvp = ump->um_devvp;
    371 			vref(devvp);
    372 		}
    373 	}
    374 
    375 	/*
    376 	 * If mount by non-root, then verify that user has necessary
    377 	 * permissions on the device.
    378 	 *
    379 	 * Permission to update a mount is checked higher, so here we presume
    380 	 * updating the mount is okay (for example, as far as securelevel goes)
    381 	 * which leaves us with the normal check.
    382 	 */
    383 	if (error == 0) {
    384 		accessmode = VREAD;
    385 		if (update ?
    386 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
    387 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    388 			accessmode |= VWRITE;
    389 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    390 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    391 		    KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
    392 		    KAUTH_ARG(accessmode));
    393 		VOP_UNLOCK(devvp);
    394 	}
    395 
    396 	if (error) {
    397 		vrele(devvp);
    398 		return error;
    399 	}
    400 
    401 	if (!update) {
    402 		int xflags;
    403 
    404 		if (mp->mnt_flag & MNT_RDONLY)
    405 			xflags = FREAD;
    406 		else
    407 			xflags = FREAD|FWRITE;
    408 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    409 		error = VOP_OPEN(devvp, xflags, FSCRED);
    410 		VOP_UNLOCK(devvp);
    411 		if (error)
    412 			goto fail;
    413 		error = ext2fs_mountfs(devvp, mp);
    414 		if (error) {
    415 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    416 			(void)VOP_CLOSE(devvp, xflags, NOCRED);
    417 			VOP_UNLOCK(devvp);
    418 			goto fail;
    419 		}
    420 
    421 		ump = VFSTOUFS(mp);
    422 		fs = ump->um_e2fs;
    423 	} else {
    424 		/*
    425 		 * Update the mount.
    426 		 */
    427 
    428 		/*
    429 		 * The initial mount got a reference on this
    430 		 * device, so drop the one obtained via
    431 		 * namei(), above.
    432 		 */
    433 		vrele(devvp);
    434 
    435 		ump = VFSTOUFS(mp);
    436 		fs = ump->um_e2fs;
    437 		if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    438 			/*
    439 			 * Changing from r/w to r/o
    440 			 */
    441 			flags = WRITECLOSE;
    442 			if (mp->mnt_flag & MNT_FORCE)
    443 				flags |= FORCECLOSE;
    444 			error = ext2fs_flushfiles(mp, flags);
    445 			if (error == 0 &&
    446 			    ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
    447 			    (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
    448 				fs->e2fs.e2fs_state = E2FS_ISCLEAN;
    449 				(void) ext2fs_sbupdate(ump, MNT_WAIT);
    450 			}
    451 			if (error)
    452 				return error;
    453 			fs->e2fs_ronly = 1;
    454 		}
    455 
    456 		if (mp->mnt_flag & MNT_RELOAD) {
    457 			error = ext2fs_reload(mp, l->l_cred, l);
    458 			if (error)
    459 				return error;
    460 		}
    461 
    462 		if (fs->e2fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
    463 			/*
    464 			 * Changing from read-only to read/write
    465 			 */
    466 			fs->e2fs_ronly = 0;
    467 			if (fs->e2fs.e2fs_state == E2FS_ISCLEAN)
    468 				fs->e2fs.e2fs_state = 0;
    469 			else
    470 				fs->e2fs.e2fs_state = E2FS_ERRORS;
    471 			fs->e2fs_fmod = 1;
    472 		}
    473 		if (args->fspec == NULL)
    474 			return 0;
    475 	}
    476 
    477 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
    478 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    479 	if (error == 0)
    480 		ext2fs_sb_setmountinfo(fs, mp);
    481 
    482 	if (fs->e2fs_fmod != 0) {	/* XXX */
    483 		fs->e2fs_fmod = 0;
    484 		if (fs->e2fs.e2fs_state == 0)
    485 			fs->e2fs.e2fs_wtime = time_second;
    486 		else
    487 			printf("%s: file system not clean; please fsck(8)\n",
    488 				mp->mnt_stat.f_mntfromname);
    489 		(void) ext2fs_cgupdate(ump, MNT_WAIT);
    490 	}
    491 	return error;
    492 
    493 fail:
    494 	vrele(devvp);
    495 	return error;
    496 }
    497 
    498 /*
    499  * Sanity check the disk vnode content, and copy it over to inode structure.
    500  */
    501 static int
    502 ext2fs_loadvnode_content(struct m_ext2fs *fs, ino_t ino, struct buf *bp, struct inode *ip)
    503 {
    504 	struct ext2fs_dinode *din;
    505 	int error = 0;
    506 
    507 	din = (struct ext2fs_dinode *)((char *)bp->b_data + (ino_to_fsbo(fs, ino) * EXT2_DINODE_SIZE(fs)));
    508 
    509 	/* sanity checks - inode data NOT byteswapped at this point */
    510 	if (EXT2_DINODE_FITS(din, e2di_extra_isize, EXT2_DINODE_SIZE(fs))
    511 	    && (EXT2_DINODE_SIZE(fs) - EXT2_REV0_DINODE_SIZE) < fs2h16(din->e2di_extra_isize))
    512 	{
    513 		printf("ext2fs: inode %"PRIu64" bad extra_isize %u",
    514 			ino, din->e2di_extra_isize);
    515 		error = EINVAL;
    516 		goto bad;
    517 	}
    518 
    519 	/* everything allright, proceed with copy */
    520 	if (ip->i_din.e2fs_din == NULL)
    521 		ip->i_din.e2fs_din = kmem_alloc(EXT2_DINODE_SIZE(fs), KM_SLEEP);
    522 
    523 	e2fs_iload(din, ip->i_din.e2fs_din, EXT2_DINODE_SIZE(fs));
    524 
    525 	ext2fs_set_inode_guid(ip);
    526 
    527     bad:
    528 	return error;
    529 }
    530 
    531 /*
    532  * Reload all incore data for a filesystem (used after running fsck on
    533  * the root filesystem and finding things to fix). The filesystem must
    534  * be mounted read-only.
    535  *
    536  * Things to do to update the mount:
    537  *	1) invalidate all cached meta-data.
    538  *	2) re-read superblock from disk.
    539  *	3) re-read summary information from disk.
    540  *	4) invalidate all inactive vnodes.
    541  *	5) invalidate all cached file data.
    542  *	6) re-read inode data for all active vnodes.
    543  */
    544 int
    545 ext2fs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
    546 {
    547 	struct vnode *vp, *devvp;
    548 	struct inode *ip;
    549 	struct buf *bp;
    550 	struct m_ext2fs *fs;
    551 	struct ext2fs *newfs;
    552 	int i, error;
    553 	struct ufsmount *ump;
    554 	struct vnode_iterator *marker;
    555 
    556 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
    557 		return EINVAL;
    558 
    559 	ump = VFSTOUFS(mp);
    560 	/*
    561 	 * Step 1: invalidate all cached meta-data.
    562 	 */
    563 	devvp = ump->um_devvp;
    564 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    565 	error = vinvalbuf(devvp, 0, cred, l, 0, 0);
    566 	VOP_UNLOCK(devvp);
    567 	if (error)
    568 		panic("ext2fs_reload: dirty1");
    569 
    570 	fs = ump->um_e2fs;
    571 	/*
    572 	 * Step 2: re-read superblock from disk. Copy in new superblock, and compute
    573 	 * in-memory values.
    574 	 */
    575 	error = bread(devvp, SBLOCK, SBSIZE, 0, &bp);
    576 	if (error)
    577 		return error;
    578 	newfs = (struct ext2fs *)bp->b_data;
    579 	e2fs_sbload(newfs, &fs->e2fs);
    580 
    581 	brelse(bp, 0);
    582 
    583 	error = ext2fs_sbfill(fs, (mp->mnt_flag & MNT_RDONLY) != 0);
    584 	if (error)
    585 		return error;
    586 
    587 	/*
    588 	 * Step 3: re-read summary information from disk.
    589 	 */
    590 	for (i = 0; i < fs->e2fs_ngdb; i++) {
    591 		error = bread(devvp ,
    592 		    EXT2_FSBTODB(fs, fs->e2fs.e2fs_first_dblock +
    593 		    1 /* superblock */ + i),
    594 		    fs->e2fs_bsize, 0, &bp);
    595 		if (error) {
    596 			return error;
    597 		}
    598 		e2fs_cgload((struct ext2_gd *)bp->b_data,
    599 		    &fs->e2fs_gd[i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
    600 		    fs->e2fs_bsize);
    601 		brelse(bp, 0);
    602 	}
    603 
    604 	vfs_vnode_iterator_init(mp, &marker);
    605 	while ((vp = vfs_vnode_iterator_next(marker, NULL, NULL))) {
    606 		/*
    607 		 * Step 4: invalidate all inactive vnodes.
    608 		 */
    609 		if (vrecycle(vp))
    610 			continue;
    611 		/*
    612 		 * Step 5: invalidate all cached file data.
    613 		 */
    614 		if (vn_lock(vp, LK_EXCLUSIVE)) {
    615 			vrele(vp);
    616 			continue;
    617 		}
    618 		if (vinvalbuf(vp, 0, cred, l, 0, 0))
    619 			panic("ext2fs_reload: dirty2");
    620 		/*
    621 		 * Step 6: re-read inode data for all active vnodes.
    622 		 */
    623 		ip = VTOI(vp);
    624 		error = bread(devvp, EXT2_FSBTODB(fs, ino_to_fsba(fs, ip->i_number)),
    625 		    (int)fs->e2fs_bsize, 0, &bp);
    626 		if (error) {
    627 			vput(vp);
    628 			break;
    629 		}
    630 		error = ext2fs_loadvnode_content(fs, ip->i_number, bp, ip);
    631 		brelse(bp, 0);
    632 		if (error) {
    633 			vput(vp);
    634 			break;
    635 		}
    636 
    637 		vput(vp);
    638 	}
    639 	vfs_vnode_iterator_destroy(marker);
    640 	return error;
    641 }
    642 
    643 /*
    644  * Common code for mount and mountroot
    645  */
    646 int
    647 ext2fs_mountfs(struct vnode *devvp, struct mount *mp)
    648 {
    649 	struct lwp *l = curlwp;
    650 	struct ufsmount *ump;
    651 	struct buf *bp;
    652 	struct ext2fs *fs;
    653 	struct m_ext2fs *m_fs;
    654 	dev_t dev;
    655 	int error, i, ronly;
    656 	kauth_cred_t cred;
    657 
    658 	dev = devvp->v_rdev;
    659 	cred = l->l_cred;
    660 
    661 	/* Flush out any old buffers remaining from a previous use. */
    662 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    663 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
    664 	VOP_UNLOCK(devvp);
    665 	if (error)
    666 		return error;
    667 
    668 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    669 
    670 	bp = NULL;
    671 	ump = NULL;
    672 
    673 	/* Read the superblock from disk, and swap it directly. */
    674 	error = bread(devvp, SBLOCK, SBSIZE, 0, &bp);
    675 	if (error)
    676 		goto out;
    677 	fs = (struct ext2fs *)bp->b_data;
    678 	m_fs = kmem_zalloc(sizeof(*m_fs), KM_SLEEP);
    679 	e2fs_sbload(fs, &m_fs->e2fs);
    680 
    681 	brelse(bp, 0);
    682 	bp = NULL;
    683 
    684 	/* Once swapped, validate and fill in the superblock. */
    685 	error = ext2fs_sbfill(m_fs, ronly);
    686 	if (error) {
    687 		kmem_free(m_fs, sizeof(*m_fs));
    688 		goto out;
    689 	}
    690 	m_fs->e2fs_ronly = ronly;
    691 
    692 	ump = kmem_zalloc(sizeof(*ump), KM_SLEEP);
    693 	ump->um_fstype = UFS1;
    694 	ump->um_ops = &ext2fs_ufsops;
    695 	ump->um_e2fs = m_fs;
    696 
    697 	if (ronly == 0) {
    698 		if (m_fs->e2fs.e2fs_state == E2FS_ISCLEAN)
    699 			m_fs->e2fs.e2fs_state = 0;
    700 		else
    701 			m_fs->e2fs.e2fs_state = E2FS_ERRORS;
    702 		m_fs->e2fs_fmod = 1;
    703 	}
    704 
    705 	/* XXX: should be added in ext2fs_sbfill()? */
    706 	m_fs->e2fs_gd = kmem_alloc(m_fs->e2fs_ngdb * m_fs->e2fs_bsize, KM_SLEEP);
    707 	for (i = 0; i < m_fs->e2fs_ngdb; i++) {
    708 		error = bread(devvp,
    709 		    EXT2_FSBTODB(m_fs, m_fs->e2fs.e2fs_first_dblock +
    710 		    1 /* superblock */ + i),
    711 		    m_fs->e2fs_bsize, 0, &bp);
    712 		if (error) {
    713 			kmem_free(m_fs->e2fs_gd,
    714 			    m_fs->e2fs_ngdb * m_fs->e2fs_bsize);
    715 			goto out;
    716 		}
    717 		e2fs_cgload((struct ext2_gd *)bp->b_data,
    718 		    &m_fs->e2fs_gd[
    719 			i * m_fs->e2fs_bsize / sizeof(struct ext2_gd)],
    720 		    m_fs->e2fs_bsize);
    721 		brelse(bp, 0);
    722 		bp = NULL;
    723 	}
    724 
    725 	error = ext2fs_cg_verify_and_initialize(devvp, m_fs, ronly);
    726 	if (error) {
    727 		kmem_free(m_fs->e2fs_gd, m_fs->e2fs_ngdb * m_fs->e2fs_bsize);
    728 		goto out;
    729 	}
    730 
    731 	mp->mnt_data = ump;
    732 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
    733 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_EXT2FS);
    734 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    735 	mp->mnt_stat.f_namemax = EXT2FS_MAXNAMLEN;
    736 	mp->mnt_flag |= MNT_LOCAL;
    737 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
    738 	mp->mnt_fs_bshift = m_fs->e2fs_bshift;
    739 	mp->mnt_iflag |= IMNT_DTYPE;
    740 	ump->um_flags = 0;
    741 	ump->um_mountp = mp;
    742 	ump->um_dev = dev;
    743 	ump->um_devvp = devvp;
    744 	ump->um_nindir = EXT2_NINDIR(m_fs);
    745 	ump->um_lognindir = ffs(EXT2_NINDIR(m_fs)) - 1;
    746 	ump->um_bptrtodb = m_fs->e2fs_fsbtodb;
    747 	ump->um_seqinc = 1; /* no frags */
    748 	ump->um_maxsymlinklen = EXT2_MAXSYMLINKLEN;
    749 	ump->um_dirblksiz = m_fs->e2fs_bsize;
    750 	ump->um_maxfilesize = ((uint64_t)0x80000000 * m_fs->e2fs_bsize - 1);
    751 	spec_node_setmountedfs(devvp, mp);
    752 	return 0;
    753 
    754 out:
    755 	if (bp != NULL)
    756 		brelse(bp, 0);
    757 	if (ump) {
    758 		kmem_free(ump->um_e2fs, sizeof(*m_fs));
    759 		kmem_free(ump, sizeof(*ump));
    760 		mp->mnt_data = NULL;
    761 	}
    762 	return error;
    763 }
    764 
    765 /*
    766  * unmount system call
    767  */
    768 int
    769 ext2fs_unmount(struct mount *mp, int mntflags)
    770 {
    771 	struct ufsmount *ump;
    772 	struct m_ext2fs *fs;
    773 	int error, flags;
    774 
    775 	flags = 0;
    776 	if (mntflags & MNT_FORCE)
    777 		flags |= FORCECLOSE;
    778 	if ((error = ext2fs_flushfiles(mp, flags)) != 0)
    779 		return error;
    780 	ump = VFSTOUFS(mp);
    781 	fs = ump->um_e2fs;
    782 	if (fs->e2fs_ronly == 0 &&
    783 		ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
    784 		(fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
    785 		fs->e2fs.e2fs_state = E2FS_ISCLEAN;
    786 		(void) ext2fs_sbupdate(ump, MNT_WAIT);
    787 	}
    788 	if (ump->um_devvp->v_type != VBAD)
    789 		spec_node_setmountedfs(ump->um_devvp, NULL);
    790 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    791 	error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
    792 	    NOCRED);
    793 	vput(ump->um_devvp);
    794 	kmem_free(fs->e2fs_gd, fs->e2fs_ngdb * fs->e2fs_bsize);
    795 	kmem_free(fs, sizeof(*fs));
    796 	kmem_free(ump, sizeof(*ump));
    797 	mp->mnt_data = NULL;
    798 	mp->mnt_flag &= ~MNT_LOCAL;
    799 	return error;
    800 }
    801 
    802 /*
    803  * Flush out all the files in a filesystem.
    804  */
    805 int
    806 ext2fs_flushfiles(struct mount *mp, int flags)
    807 {
    808 	extern int doforce;
    809 	int error;
    810 
    811 	if (!doforce)
    812 		flags &= ~FORCECLOSE;
    813 	error = vflush(mp, NULLVP, flags);
    814 	return error;
    815 }
    816 
    817 /*
    818  * Get file system statistics.
    819  */
    820 int
    821 ext2fs_statvfs(struct mount *mp, struct statvfs *sbp)
    822 {
    823 	struct ufsmount *ump;
    824 	struct m_ext2fs *fs;
    825 	uint32_t overhead, overhead_per_group, ngdb;
    826 	int i, ngroups;
    827 
    828 	ump = VFSTOUFS(mp);
    829 	fs = ump->um_e2fs;
    830 	if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
    831 		panic("ext2fs_statvfs");
    832 
    833 	/*
    834 	 * Compute the overhead (FS structures)
    835 	 */
    836 	overhead_per_group =
    837 	    1 /* block bitmap */ +
    838 	    1 /* inode bitmap */ +
    839 	    fs->e2fs_itpg;
    840 	overhead = fs->e2fs.e2fs_first_dblock +
    841 	    fs->e2fs_ncg * overhead_per_group;
    842 	if (EXT2F_HAS_COMPAT_FEATURE(fs, EXT2F_COMPAT_SPARSESUPER2)) {
    843 		/*
    844 		 * Superblock and group descriptions is in group zero,
    845 		 * then optionally 0, 1 or 2 extra copies.
    846 		 */
    847 		ngroups = 1
    848 			+ (fs->e2fs.e4fs_backup_bgs[0] ? 1 : 0)
    849 			+ (fs->e2fs.e4fs_backup_bgs[1] ? 1 : 0);
    850 	} else if (EXT2F_HAS_ROCOMPAT_FEATURE(fs, EXT2F_ROCOMPAT_SPARSESUPER)) {
    851 		for (i = 0, ngroups = 0; i < fs->e2fs_ncg; i++) {
    852 			if (cg_has_sb(i))
    853 				ngroups++;
    854 		}
    855 	} else {
    856 		ngroups = fs->e2fs_ncg;
    857 	}
    858 	ngdb = fs->e2fs_ngdb;
    859 	if (EXT2F_HAS_COMPAT_FEATURE(fs, EXT2F_COMPAT_RESIZE))
    860 		ngdb += fs->e2fs.e2fs_reserved_ngdb;
    861 	overhead += ngroups * (1 /* superblock */ + ngdb);
    862 
    863 	sbp->f_bsize = fs->e2fs_bsize;
    864 	sbp->f_frsize = MINBSIZE << fs->e2fs.e2fs_fsize;
    865 	sbp->f_iosize = fs->e2fs_bsize;
    866 	sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
    867 	sbp->f_bfree = fs->e2fs.e2fs_fbcount;
    868 	sbp->f_bresvd = fs->e2fs.e2fs_rbcount;
    869 	if (sbp->f_bfree > sbp->f_bresvd)
    870 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
    871 	else
    872 		sbp->f_bavail = 0;
    873 	sbp->f_files =  fs->e2fs.e2fs_icount;
    874 	sbp->f_ffree = fs->e2fs.e2fs_ficount;
    875 	sbp->f_favail = fs->e2fs.e2fs_ficount;
    876 	sbp->f_fresvd = 0;
    877 	copy_statvfs_info(sbp, mp);
    878 	return 0;
    879 }
    880 
    881 static bool
    882 ext2fs_sync_selector(void *cl, struct vnode *vp)
    883 {
    884 	struct inode *ip;
    885 
    886 	KASSERT(mutex_owned(vp->v_interlock));
    887 
    888 	ip = VTOI(vp);
    889 	/*
    890 	 * Skip the vnode/inode if inaccessible.
    891 	 */
    892 	if (ip == NULL || vp->v_type == VNON)
    893 		return false;
    894 
    895 	if (((ip->i_flag &
    896 	      (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
    897 	     LIST_EMPTY(&vp->v_dirtyblkhd) &&
    898 	     UVM_OBJ_IS_CLEAN(&vp->v_uobj)))
    899 		return false;
    900 	return true;
    901 }
    902 
    903 /*
    904  * Go through the disk queues to initiate sandbagged IO;
    905  * go through the inodes to write those that have been modified;
    906  * initiate the writing of the super block if it has been modified.
    907  *
    908  * Note: we are always called with the filesystem marked `MPBUSY'.
    909  */
    910 int
    911 ext2fs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
    912 {
    913 	struct vnode *vp;
    914 	struct ufsmount *ump = VFSTOUFS(mp);
    915 	struct m_ext2fs *fs;
    916 	struct vnode_iterator *marker;
    917 	int error, allerror = 0;
    918 
    919 	fs = ump->um_e2fs;
    920 	if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) {	/* XXX */
    921 		printf("fs = %s\n", fs->e2fs_fsmnt);
    922 		panic("update: rofs mod");
    923 	}
    924 
    925 	/*
    926 	 * Write back each (modified) inode.
    927 	 */
    928 	vfs_vnode_iterator_init(mp, &marker);
    929 	while ((vp = vfs_vnode_iterator_next(marker, ext2fs_sync_selector,
    930 	    NULL)))
    931 	{
    932 		error = vn_lock(vp, LK_EXCLUSIVE);
    933 		if (error) {
    934 			vrele(vp);
    935 			continue;
    936 		}
    937 		if (vp->v_type == VREG && waitfor == MNT_LAZY)
    938 			error = ext2fs_update(vp, NULL, NULL, 0);
    939 		else
    940 			error = VOP_FSYNC(vp, cred,
    941 			    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0);
    942 		if (error)
    943 			allerror = error;
    944 		vput(vp);
    945 	}
    946 	vfs_vnode_iterator_destroy(marker);
    947 	/*
    948 	 * Force stale file system control information to be flushed.
    949 	 */
    950 	if (waitfor != MNT_LAZY) {
    951 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    952 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
    953 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
    954 			allerror = error;
    955 		VOP_UNLOCK(ump->um_devvp);
    956 	}
    957 	/*
    958 	 * Write back modified superblock.
    959 	 */
    960 	if (fs->e2fs_fmod != 0) {
    961 		fs->e2fs_fmod = 0;
    962 		fs->e2fs.e2fs_wtime = time_second;
    963 		if ((error = ext2fs_cgupdate(ump, waitfor)))
    964 			allerror = error;
    965 	}
    966 	return allerror;
    967 }
    968 
    969 /*
    970  * Load inode from disk and initialize vnode.
    971  */
    972 static int
    973 ext2fs_init_vnode(struct ufsmount *ump, struct vnode *vp, ino_t ino)
    974 {
    975 	struct m_ext2fs *fs;
    976 	struct inode *ip;
    977 	struct buf *bp;
    978 	int error;
    979 
    980 	fs = ump->um_e2fs;
    981 
    982 	/* Read in the disk contents for the inode, copy into the inode. */
    983 	error = bread(ump->um_devvp, EXT2_FSBTODB(fs, ino_to_fsba(fs, ino)),
    984 	    (int)fs->e2fs_bsize, 0, &bp);
    985 	if (error)
    986 		return error;
    987 
    988 	/* Allocate and initialize inode. */
    989 	ip = pool_get(&ext2fs_inode_pool, PR_WAITOK);
    990 	memset(ip, 0, sizeof(struct inode));
    991 	ip->i_vnode = vp;
    992 	ip->i_ump = ump;
    993 	ip->i_e2fs = fs;
    994 	ip->i_dev = ump->um_dev;
    995 	ip->i_number = ino;
    996 	ip->i_e2fs_last_lblk = 0;
    997 	ip->i_e2fs_last_blk = 0;
    998 
    999 	error = ext2fs_loadvnode_content(fs, ino, bp, ip);
   1000 	brelse(bp, 0);
   1001 	if (error) {
   1002 		pool_put(&ext2fs_inode_pool, ip);
   1003 		return error;
   1004 	}
   1005 
   1006 	/* If the inode was deleted, reset all fields */
   1007 	if (ip->i_e2fs_dtime != 0) {
   1008 		ip->i_e2fs_mode = 0;
   1009 		(void)ext2fs_setsize(ip, 0);
   1010 		(void)ext2fs_setnblock(ip, 0);
   1011 		memset(ip->i_e2fs_blocks, 0, sizeof(ip->i_e2fs_blocks));
   1012 	}
   1013 
   1014 	/* Initialise vnode with this inode. */
   1015 	vp->v_tag = VT_EXT2FS;
   1016 	vp->v_op = ext2fs_vnodeop_p;
   1017 	vp->v_vflag |= VV_LOCKSWORK;
   1018 	vp->v_data = ip;
   1019 
   1020 	/* Initialize genfs node. */
   1021 	genfs_node_init(vp, &ext2fs_genfsops);
   1022 
   1023 	return 0;
   1024 }
   1025 
   1026 /*
   1027  * Read an inode from disk and initialize this vnode / inode pair.
   1028  * Caller assures no other thread will try to load this inode.
   1029  */
   1030 int
   1031 ext2fs_loadvnode(struct mount *mp, struct vnode *vp,
   1032     const void *key, size_t key_len, const void **new_key)
   1033 {
   1034 	ino_t ino;
   1035 	struct inode *ip;
   1036 	struct ufsmount *ump;
   1037 	int error;
   1038 
   1039 	KASSERT(key_len == sizeof(ino));
   1040 	memcpy(&ino, key, key_len);
   1041 	ump = VFSTOUFS(mp);
   1042 
   1043 	error = ext2fs_init_vnode(ump, vp, ino);
   1044 	if (error)
   1045 		return error;
   1046 
   1047 	ip = VTOI(vp);
   1048 
   1049 	/* Initialize the vnode from the inode. */
   1050 	ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
   1051 
   1052 	/* Finish inode initialization. */
   1053 	ip->i_devvp = ump->um_devvp;
   1054 	vref(ip->i_devvp);
   1055 
   1056 	/*
   1057 	 * Set up a generation number for this inode if it does not
   1058 	 * already have one. This should only happen on old filesystems.
   1059 	 */
   1060 
   1061 	if (ip->i_e2fs_gen == 0) {
   1062 		if (++ext2gennumber < (u_long)time_second)
   1063 			ext2gennumber = time_second;
   1064 		ip->i_e2fs_gen = ext2gennumber;
   1065 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
   1066 			ip->i_flag |= IN_MODIFIED;
   1067 	}
   1068 	uvm_vnp_setsize(vp, ext2fs_size(ip));
   1069 	*new_key = &ip->i_number;
   1070 	return 0;
   1071 }
   1072 
   1073 /*
   1074  * Create a new inode on disk and initialize this vnode / inode pair.
   1075  */
   1076 int
   1077 ext2fs_newvnode(struct mount *mp, struct vnode *dvp, struct vnode *vp,
   1078     struct vattr *vap, kauth_cred_t cred, void *extra,
   1079     size_t *key_len, const void **new_key)
   1080 {
   1081 	ino_t ino;
   1082 	struct inode *ip, *pdir;
   1083 	struct m_ext2fs *fs;
   1084 	struct ufsmount *ump;
   1085 	int error, mode;
   1086 
   1087 	KASSERT(dvp->v_mount == mp);
   1088 	KASSERT(vap->va_type != VNON);
   1089 
   1090 	*key_len = sizeof(ino);
   1091 
   1092 	pdir = VTOI(dvp);
   1093 	fs = pdir->i_e2fs;
   1094 	ump = VFSTOUFS(mp);
   1095 	mode = MAKEIMODE(vap->va_type, vap->va_mode);
   1096 
   1097 	/* Allocate fresh inode. */
   1098 	error = ext2fs_valloc(dvp, mode, cred, &ino);
   1099 	if (error)
   1100 		return error;
   1101 
   1102 	/* Attach inode to vnode. */
   1103 	error = ext2fs_init_vnode(ump, vp, ino);
   1104 	if (error) {
   1105 		ext2fs_vfree(dvp, ino, mode);
   1106 		return error;
   1107 	}
   1108 
   1109 	ip = VTOI(vp);
   1110 
   1111 	KASSERT(!E2FS_HAS_GD_CSUM(fs) || (fs->e2fs_gd[ino_to_cg(fs, ino)].ext2bgd_flags & h2fs16(E2FS_BG_INODE_ZEROED)) != 0);
   1112 
   1113 	/* check for already used inode; makes sense only for ZEROED itable */
   1114 	if (__predict_false(ip->i_e2fs_mode && ip->i_e2fs_nlink != 0)) {
   1115 		printf("mode = 0%o, nlinks %d, inum = %llu, fs = %s\n",
   1116 		    ip->i_e2fs_mode, ip->i_e2fs_nlink,
   1117 		    (unsigned long long)ip->i_number, fs->e2fs_fsmnt);
   1118 		panic("ext2fs_valloc: dup alloc");
   1119 	}
   1120 
   1121 	memset(ip->i_din.e2fs_din, 0, EXT2_DINODE_SIZE(fs));
   1122 
   1123 	/*
   1124 	 * Set up a new generation number for this inode.
   1125 	 */
   1126 	if (++ext2gennumber < time_second)
   1127 		ext2gennumber = time_second;
   1128 	ip->i_e2fs_gen = ext2gennumber;
   1129 
   1130 	ip->i_uid = kauth_cred_geteuid(cred);
   1131 	ip->i_e2fs_uid = ip->i_uid & 0xffff;
   1132 	ip->i_e2fs_gid = pdir->i_e2fs_gid;
   1133 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
   1134 		ip->i_e2fs_uid_high = (ip->i_uid >> 16) & 0xffff;
   1135 		ip->i_e2fs_gid_high = pdir->i_e2fs_gid_high;
   1136 	} else {
   1137 		ip->i_e2fs_uid_high = 0;
   1138 		ip->i_e2fs_gid_high = 0;
   1139 	}
   1140 	ip->i_gid = ip->i_e2fs_gid | (ip->i_e2fs_gid_high << 16);
   1141 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
   1142 	ip->i_e2fs_mode = mode;
   1143 	vp->v_type = IFTOVT(mode);
   1144 	ip->i_e2fs_nlink = 1;
   1145 
   1146 	/* Authorize setting SGID if needed. */
   1147 	if (ip->i_e2fs_mode & ISGID) {
   1148 		error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY,
   1149 		    vp, NULL, genfs_can_chmod(vp->v_type, cred, ip->i_uid,
   1150 		    ip->i_gid, mode));
   1151 		if (error)
   1152 			ip->i_e2fs_mode &= ~ISGID;
   1153 	}
   1154 
   1155 	/* Initialize extra_isize according to what is set in superblock */
   1156 	if (EXT2F_HAS_ROCOMPAT_FEATURE(ip->i_e2fs, EXT2F_ROCOMPAT_EXTRA_ISIZE)
   1157 	    && EXT2_DINODE_SIZE(ip->i_e2fs) > EXT2_REV0_DINODE_SIZE) {
   1158 		ip->i_din.e2fs_din->e2di_extra_isize = ip->i_e2fs->e2fs.e4fs_want_extra_isize;
   1159 	}
   1160 
   1161 	/* Set create time if possible */
   1162 	if (EXT2_DINODE_FITS(ip->i_din.e2fs_din, e2di_crtime, EXT2_DINODE_SIZE(ip->i_e2fs))) {
   1163 		struct timespec now;
   1164 		vfs_timestamp(&now);
   1165 		EXT2_DINODE_TIME_SET(&now, ip->i_din.e2fs_din, e2di_crtime, EXT2_DINODE_SIZE(ip->i_e2fs));
   1166 	}
   1167 
   1168 	/* Initialize the vnode from the inode. */
   1169 	ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
   1170 
   1171 	/* Finish inode initialization. */
   1172 	ip->i_devvp = ump->um_devvp;
   1173 	vref(ip->i_devvp);
   1174 
   1175 	uvm_vnp_setsize(vp, ext2fs_size(ip));
   1176 	*new_key = &ip->i_number;
   1177 	return 0;
   1178 }
   1179 
   1180 /*
   1181  * File handle to vnode
   1182  *
   1183  * Have to be really careful about stale file handles:
   1184  * - check that the inode number is valid
   1185  * - call ext2fs_vget() to get the locked inode
   1186  * - check for an unallocated inode (i_mode == 0)
   1187  */
   1188 int
   1189 ext2fs_fhtovp(struct mount *mp, struct fid *fhp, int lktype, struct vnode **vpp)
   1190 {
   1191 	struct inode *ip;
   1192 	struct vnode *nvp;
   1193 	int error;
   1194 	struct ufid ufh;
   1195 	struct m_ext2fs *fs;
   1196 
   1197 	if (fhp->fid_len != sizeof(struct ufid))
   1198 		return EINVAL;
   1199 
   1200 	memcpy(&ufh, fhp, sizeof(struct ufid));
   1201 	fs = VFSTOUFS(mp)->um_e2fs;
   1202 	if ((ufh.ufid_ino < EXT2_FIRSTINO && ufh.ufid_ino != EXT2_ROOTINO) ||
   1203 		ufh.ufid_ino >= fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
   1204 		return ESTALE;
   1205 
   1206 	if ((error = VFS_VGET(mp, ufh.ufid_ino, lktype, &nvp)) != 0) {
   1207 		*vpp = NULLVP;
   1208 		return error;
   1209 	}
   1210 	ip = VTOI(nvp);
   1211 	if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0 ||
   1212 		ip->i_e2fs_gen != ufh.ufid_gen) {
   1213 		vput(nvp);
   1214 		*vpp = NULLVP;
   1215 		return ESTALE;
   1216 	}
   1217 	*vpp = nvp;
   1218 	return 0;
   1219 }
   1220 
   1221 /*
   1222  * Vnode pointer to File handle
   1223  */
   1224 /* ARGSUSED */
   1225 int
   1226 ext2fs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
   1227 {
   1228 	struct inode *ip;
   1229 	struct ufid ufh;
   1230 
   1231 	if (*fh_size < sizeof(struct ufid)) {
   1232 		*fh_size = sizeof(struct ufid);
   1233 		return E2BIG;
   1234 	}
   1235 	*fh_size = sizeof(struct ufid);
   1236 
   1237 	ip = VTOI(vp);
   1238 	memset(&ufh, 0, sizeof(ufh));
   1239 	ufh.ufid_len = sizeof(struct ufid);
   1240 	ufh.ufid_ino = ip->i_number;
   1241 	ufh.ufid_gen = ip->i_e2fs_gen;
   1242 	memcpy(fhp, &ufh, sizeof(ufh));
   1243 	return 0;
   1244 }
   1245 
   1246 /*
   1247  * Write a superblock and associated information back to disk.
   1248  */
   1249 int
   1250 ext2fs_sbupdate(struct ufsmount *mp, int waitfor)
   1251 {
   1252 	struct m_ext2fs *fs = mp->um_e2fs;
   1253 	struct buf *bp;
   1254 	int error = 0;
   1255 
   1256 	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
   1257 	e2fs_sbsave(&fs->e2fs, (struct ext2fs*)bp->b_data);
   1258 	if (waitfor == MNT_WAIT)
   1259 		error = bwrite(bp);
   1260 	else
   1261 		bawrite(bp);
   1262 	return error;
   1263 }
   1264 
   1265 int
   1266 ext2fs_cgupdate(struct ufsmount *mp, int waitfor)
   1267 {
   1268 	struct m_ext2fs *fs = mp->um_e2fs;
   1269 	struct buf *bp;
   1270 	int i, error = 0, allerror = 0;
   1271 
   1272 	allerror = ext2fs_sbupdate(mp, waitfor);
   1273 	for (i = 0; i < fs->e2fs_ngdb; i++) {
   1274 		bp = getblk(mp->um_devvp, EXT2_FSBTODB(fs,
   1275 		    fs->e2fs.e2fs_first_dblock +
   1276 		    1 /* superblock */ + i), fs->e2fs_bsize, 0, 0);
   1277 		e2fs_cgsave(&fs->e2fs_gd[
   1278 		    i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
   1279 		    (struct ext2_gd *)bp->b_data, fs->e2fs_bsize);
   1280 		if (waitfor == MNT_WAIT)
   1281 			error = bwrite(bp);
   1282 		else
   1283 			bawrite(bp);
   1284 	}
   1285 
   1286 	if (!allerror && error)
   1287 		allerror = error;
   1288 	return allerror;
   1289 }
   1290 
   1291 /*
   1292  * Fill in the m_fs structure, and validate the fields of the superblock.
   1293  * NOTE: here, the superblock is already swapped.
   1294  */
   1295 static int
   1296 ext2fs_sbfill(struct m_ext2fs *m_fs, int ronly)
   1297 {
   1298 	uint32_t u32;
   1299 	struct ext2fs *fs = &m_fs->e2fs;
   1300 
   1301 	/*
   1302 	 * General sanity checks
   1303 	 */
   1304 	if (fs->e2fs_magic != E2FS_MAGIC)
   1305 		return EINVAL;
   1306 	if (fs->e2fs_rev > E2FS_REV1) {
   1307 		printf("ext2fs: unsupported revision number: %x\n", fs->e2fs_rev);
   1308 		return EINVAL;
   1309 	}
   1310 	if (fs->e2fs_log_bsize > 2) {
   1311 		/* block size = 1024|2048|4096 */
   1312 		printf("ext2fs: bad block size: %d\n", fs->e2fs_log_bsize);
   1313 		return EINVAL;
   1314 	}
   1315 	if (fs->e2fs_bpg == 0) {
   1316 		printf("ext2fs: zero blocks per group\n");
   1317 		return EINVAL;
   1318 	}
   1319 	if (fs->e2fs_ipg == 0) {
   1320 		printf("ext2fs: zero inodes per group\n");
   1321 		return EINVAL;
   1322 	}
   1323 
   1324 	if (fs->e2fs_first_dblock >= fs->e2fs_bcount) {
   1325 		printf("ext2fs: invalid first data block\n");
   1326 		return EINVAL;
   1327 	}
   1328 	if (fs->e2fs_rbcount > fs->e2fs_bcount ||
   1329 	    fs->e2fs_fbcount > fs->e2fs_bcount) {
   1330 		printf("ext2fs: invalid block count\n");
   1331 		return EINVAL;
   1332 	}
   1333 
   1334 	/*
   1335 	 * Compute the fields of the superblock
   1336 	 */
   1337 	u32 = fs->e2fs_bcount - fs->e2fs_first_dblock; /* > 0 */
   1338 	m_fs->e2fs_ncg = howmany(u32, fs->e2fs_bpg);
   1339 	if (m_fs->e2fs_ncg == 0) {
   1340 		printf("ext2fs: invalid number of cylinder groups\n");
   1341 		return EINVAL;
   1342 	}
   1343 
   1344 	m_fs->e2fs_fsbtodb = fs->e2fs_log_bsize + LOG_MINBSIZE - DEV_BSHIFT;
   1345 	m_fs->e2fs_bsize = MINBSIZE << fs->e2fs_log_bsize;
   1346 	m_fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs_log_bsize;
   1347 	m_fs->e2fs_qbmask = m_fs->e2fs_bsize - 1;
   1348 	m_fs->e2fs_bmask = ~m_fs->e2fs_qbmask;
   1349 
   1350 	if ((u32 = m_fs->e2fs_bsize / sizeof(struct ext2_gd)) == 0) {
   1351 		/* Unlikely to happen */
   1352 		printf("ext2fs: invalid block size\n");
   1353 		return EINVAL;
   1354 	}
   1355 	m_fs->e2fs_ngdb = howmany(m_fs->e2fs_ncg, u32);
   1356 	if (m_fs->e2fs_ngdb == 0) {
   1357 		printf("ext2fs: invalid number of group descriptor blocks\n");
   1358 		return EINVAL;
   1359 	}
   1360 
   1361 	if (m_fs->e2fs_bsize < EXT2_DINODE_SIZE(m_fs)) {
   1362 		printf("ext2fs: invalid inode size\n");
   1363 		return EINVAL;
   1364 	}
   1365 	m_fs->e2fs_ipb = m_fs->e2fs_bsize / EXT2_DINODE_SIZE(m_fs);
   1366 
   1367 	m_fs->e2fs_itpg = fs->e2fs_ipg / m_fs->e2fs_ipb;
   1368 
   1369 	/*
   1370 	 * Revision-specific checks
   1371 	 */
   1372 	if (fs->e2fs_rev > E2FS_REV0) {
   1373 		char buf[256];
   1374 		if (fs->e2fs_first_ino != EXT2_FIRSTINO) {
   1375 			printf("ext2fs: unsupported first inode position\n");
   1376 			return EINVAL;
   1377 		}
   1378 		u32 = fs->e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP;
   1379 		if (u32) {
   1380 			snprintb(buf, sizeof(buf), EXT2F_INCOMPAT_BITS, u32);
   1381 			printf("ext2fs: unsupported incompat features: %s\n", buf);
   1382 #ifndef EXT2_IGNORE_INCOMPAT_FEATURES
   1383 			return EINVAL;
   1384 #endif
   1385 		}
   1386 		u32 = fs->e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP;
   1387 		if (!ronly && u32) {
   1388 			snprintb(buf, sizeof(buf), EXT2F_ROCOMPAT_BITS, u32);
   1389 			printf("ext2fs: unsupported ro-incompat features: %s\n",
   1390 			    buf);
   1391 #ifndef EXT2_IGNORE_ROCOMPAT_FEATURES
   1392 			return EROFS;
   1393 #endif
   1394 		}
   1395 		if (fs->e2fs_inode_size == 0 || !powerof2(fs->e2fs_inode_size) || fs->e2fs_inode_size > m_fs->e2fs_bsize) {
   1396 			printf("ext2fs: bad inode size\n");
   1397 			return EINVAL;
   1398 		}
   1399 	}
   1400 
   1401 	return 0;
   1402 }
   1403