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