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