Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_vfsops.c revision 1.177
      1 /*	$NetBSD: ext2fs_vfsops.c,v 1.177 2014/03/05 09:37:29 hannken 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.177 2014/03/05 09:37:29 hannken Exp $");
     64 
     65 #if defined(_KERNEL_OPT)
     66 #include "opt_compat_netbsd.h"
     67 #endif
     68 
     69 #include <sys/param.h>
     70 #include <sys/systm.h>
     71 #include <sys/sysctl.h>
     72 #include <sys/namei.h>
     73 #include <sys/proc.h>
     74 #include <sys/kernel.h>
     75 #include <sys/vnode.h>
     76 #include <sys/socket.h>
     77 #include <sys/mount.h>
     78 #include <sys/buf.h>
     79 #include <sys/device.h>
     80 #include <sys/mbuf.h>
     81 #include <sys/file.h>
     82 #include <sys/disklabel.h>
     83 #include <sys/ioctl.h>
     84 #include <sys/errno.h>
     85 #include <sys/malloc.h>
     86 #include <sys/pool.h>
     87 #include <sys/lock.h>
     88 #include <sys/conf.h>
     89 #include <sys/kauth.h>
     90 #include <sys/module.h>
     91 
     92 #include <miscfs/genfs/genfs.h>
     93 #include <miscfs/specfs/specdev.h>
     94 
     95 #include <ufs/ufs/quota.h>
     96 #include <ufs/ufs/ufsmount.h>
     97 #include <ufs/ufs/inode.h>
     98 #include <ufs/ufs/dir.h>
     99 #include <ufs/ufs/ufs_extern.h>
    100 
    101 #include <ufs/ext2fs/ext2fs.h>
    102 #include <ufs/ext2fs/ext2fs_dir.h>
    103 #include <ufs/ext2fs/ext2fs_extern.h>
    104 
    105 MODULE(MODULE_CLASS_VFS, ext2fs, "ffs");
    106 
    107 int ext2fs_sbupdate(struct ufsmount *, int);
    108 static int ext2fs_checksb(struct ext2fs *, int);
    109 
    110 static struct sysctllog *ext2fs_sysctl_log;
    111 
    112 extern const struct vnodeopv_desc ext2fs_vnodeop_opv_desc;
    113 extern const struct vnodeopv_desc ext2fs_specop_opv_desc;
    114 extern const struct vnodeopv_desc ext2fs_fifoop_opv_desc;
    115 
    116 const struct vnodeopv_desc * const ext2fs_vnodeopv_descs[] = {
    117 	&ext2fs_vnodeop_opv_desc,
    118 	&ext2fs_specop_opv_desc,
    119 	&ext2fs_fifoop_opv_desc,
    120 	NULL,
    121 };
    122 
    123 struct vfsops ext2fs_vfsops = {
    124 	MOUNT_EXT2FS,
    125 	sizeof (struct ufs_args),
    126 	ext2fs_mount,
    127 	ufs_start,
    128 	ext2fs_unmount,
    129 	ufs_root,
    130 	ufs_quotactl,
    131 	ext2fs_statvfs,
    132 	ext2fs_sync,
    133 	ext2fs_vget,
    134 	ext2fs_fhtovp,
    135 	ext2fs_vptofh,
    136 	ext2fs_init,
    137 	ext2fs_reinit,
    138 	ext2fs_done,
    139 	ext2fs_mountroot,
    140 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    141 	vfs_stdextattrctl,
    142 	(void *)eopnotsupp,	/* vfs_suspendctl */
    143 	genfs_renamelock_enter,
    144 	genfs_renamelock_exit,
    145 	(void *)eopnotsupp,
    146 	ext2fs_vnodeopv_descs,
    147 	0,
    148 	{ NULL, NULL },
    149 };
    150 
    151 static const struct genfs_ops ext2fs_genfsops = {
    152 	.gop_size = genfs_size,
    153 	.gop_alloc = ext2fs_gop_alloc,
    154 	.gop_write = genfs_gop_write,
    155 	.gop_markupdate = ufs_gop_markupdate,
    156 };
    157 
    158 static const struct ufs_ops ext2fs_ufsops = {
    159 	.uo_itimes = ext2fs_itimes,
    160 	.uo_update = ext2fs_update,
    161 	.uo_vfree = ext2fs_vfree,
    162 };
    163 
    164 /* Fill in the inode uid/gid from ext2 halves.  */
    165 void
    166 ext2fs_set_inode_guid(struct inode *ip)
    167 {
    168 
    169 	ip->i_gid = ip->i_e2fs_gid;
    170 	ip->i_uid = ip->i_e2fs_uid;
    171 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
    172 		ip->i_gid |= ip->i_e2fs_gid_high << 16;
    173 		ip->i_uid |= ip->i_e2fs_uid_high << 16;
    174 	}
    175 }
    176 
    177 static int
    178 ext2fs_modcmd(modcmd_t cmd, void *arg)
    179 {
    180 	int error;
    181 
    182 	switch (cmd) {
    183 	case MODULE_CMD_INIT:
    184 		error = vfs_attach(&ext2fs_vfsops);
    185 		if (error != 0)
    186 			break;
    187 		sysctl_createv(&ext2fs_sysctl_log, 0, NULL, NULL,
    188 			       CTLFLAG_PERMANENT,
    189 			       CTLTYPE_NODE, "ext2fs",
    190 			       SYSCTL_DESCR("Linux EXT2FS file system"),
    191 			       NULL, 0, NULL, 0,
    192 			       CTL_VFS, 17, CTL_EOL);
    193 		/*
    194 		 * XXX the "17" above could be dynamic, thereby eliminating
    195 		 * one more instance of the "number to vfs" mapping problem,
    196 		 * but "17" is the order as taken from sys/mount.h
    197 		 */
    198 		break;
    199 	case MODULE_CMD_FINI:
    200 		error = vfs_detach(&ext2fs_vfsops);
    201 		if (error != 0)
    202 			break;
    203 		sysctl_teardown(&ext2fs_sysctl_log);
    204 		break;
    205 	default:
    206 		error = ENOTTY;
    207 		break;
    208 	}
    209 
    210 	return (error);
    211 }
    212 
    213 /*
    214  * XXX Same structure as FFS inodes?  Should we share a common pool?
    215  */
    216 struct pool ext2fs_inode_pool;
    217 struct pool ext2fs_dinode_pool;
    218 
    219 extern u_long ext2gennumber;
    220 
    221 void
    222 ext2fs_init(void)
    223 {
    224 
    225 	pool_init(&ext2fs_inode_pool, sizeof(struct inode), 0, 0, 0,
    226 	    "ext2fsinopl", &pool_allocator_nointr, IPL_NONE);
    227 	pool_init(&ext2fs_dinode_pool, sizeof(struct ext2fs_dinode), 0, 0, 0,
    228 	    "ext2dinopl", &pool_allocator_nointr, IPL_NONE);
    229 	ufs_init();
    230 }
    231 
    232 void
    233 ext2fs_reinit(void)
    234 {
    235 	ufs_reinit();
    236 }
    237 
    238 void
    239 ext2fs_done(void)
    240 {
    241 
    242 	ufs_done();
    243 	pool_destroy(&ext2fs_inode_pool);
    244 	pool_destroy(&ext2fs_dinode_pool);
    245 }
    246 
    247 /*
    248  * Called by main() when ext2fs is going to be mounted as root.
    249  *
    250  * Name is updated by mount(8) after booting.
    251  */
    252 #define ROOTNAME	"root_device"
    253 
    254 int
    255 ext2fs_mountroot(void)
    256 {
    257 	extern struct vnode *rootvp;
    258 	struct m_ext2fs *fs;
    259 	struct mount *mp;
    260 	struct ufsmount *ump;
    261 	int error;
    262 
    263 	if (device_class(root_device) != DV_DISK)
    264 		return (ENODEV);
    265 
    266 	if ((error = vfs_rootmountalloc(MOUNT_EXT2FS, "root_device", &mp))) {
    267 		vrele(rootvp);
    268 		return (error);
    269 	}
    270 
    271 	if ((error = ext2fs_mountfs(rootvp, mp)) != 0) {
    272 		vfs_unbusy(mp, false, NULL);
    273 		vfs_destroy(mp);
    274 		return (error);
    275 	}
    276 	mountlist_append(mp);
    277 	ump = VFSTOUFS(mp);
    278 	fs = ump->um_e2fs;
    279 	memset(fs->e2fs_fsmnt, 0, sizeof(fs->e2fs_fsmnt));
    280 	(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
    281 	    sizeof(fs->e2fs_fsmnt) - 1, 0);
    282 	if (fs->e2fs.e2fs_rev > E2FS_REV0) {
    283 		memset(fs->e2fs.e2fs_fsmnt, 0, sizeof(fs->e2fs.e2fs_fsmnt));
    284 		(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
    285 		    sizeof(fs->e2fs.e2fs_fsmnt) - 1, 0);
    286 	}
    287 	(void)ext2fs_statvfs(mp, &mp->mnt_stat);
    288 	vfs_unbusy(mp, false, NULL);
    289 	setrootfstime((time_t)fs->e2fs.e2fs_wtime);
    290 	return (0);
    291 }
    292 
    293 /*
    294  * VFS Operations.
    295  *
    296  * mount system call
    297  */
    298 int
    299 ext2fs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    300 {
    301 	struct lwp *l = curlwp;
    302 	struct vnode *devvp;
    303 	struct ufs_args *args = data;
    304 	struct ufsmount *ump = NULL;
    305 	struct m_ext2fs *fs;
    306 	size_t size;
    307 	int error = 0, flags, update;
    308 	mode_t accessmode;
    309 
    310 	if (*data_len < sizeof *args)
    311 		return EINVAL;
    312 
    313 	if (mp->mnt_flag & MNT_GETARGS) {
    314 		ump = VFSTOUFS(mp);
    315 		if (ump == NULL)
    316 			return EIO;
    317 		memset(args, 0, sizeof *args);
    318 		args->fspec = NULL;
    319 		*data_len = sizeof *args;
    320 		return 0;
    321 	}
    322 
    323 	update = mp->mnt_flag & MNT_UPDATE;
    324 
    325 	/* Check arguments */
    326 	if (args->fspec != NULL) {
    327 		/*
    328 		 * Look up the name and verify that it's sane.
    329 		 */
    330 		error = namei_simple_user(args->fspec,
    331 					NSM_FOLLOW_NOEMULROOT, &devvp);
    332 		if (error != 0)
    333 			return (error);
    334 
    335 		if (!update) {
    336 			/*
    337 			 * Be sure this is a valid block device
    338 			 */
    339 			if (devvp->v_type != VBLK)
    340 				error = ENOTBLK;
    341 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
    342 				error = ENXIO;
    343 		} else {
    344 		        /*
    345 			 * Be sure we're still naming the same device
    346 			 * used for our initial mount
    347 			 */
    348 			ump = VFSTOUFS(mp);
    349 			if (devvp != ump->um_devvp) {
    350 				if (devvp->v_rdev != ump->um_devvp->v_rdev)
    351 					error = EINVAL;
    352 				else {
    353 					vrele(devvp);
    354 					devvp = ump->um_devvp;
    355 					vref(devvp);
    356 				}
    357 			}
    358 		}
    359 	} else {
    360 		if (!update) {
    361 			/* New mounts must have a filename for the device */
    362 			return (EINVAL);
    363 		} else {
    364 			ump = VFSTOUFS(mp);
    365 			devvp = ump->um_devvp;
    366 			vref(devvp);
    367 		}
    368 	}
    369 
    370 	/*
    371 	 * If mount by non-root, then verify that user has necessary
    372 	 * permissions on the device.
    373 	 *
    374 	 * Permission to update a mount is checked higher, so here we presume
    375 	 * updating the mount is okay (for example, as far as securelevel goes)
    376 	 * which leaves us with the normal check.
    377 	 */
    378 	if (error == 0) {
    379 		accessmode = VREAD;
    380 		if (update ?
    381 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
    382 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    383 			accessmode |= VWRITE;
    384 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    385 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    386 		    KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
    387 		    KAUTH_ARG(accessmode));
    388 		VOP_UNLOCK(devvp);
    389 	}
    390 
    391 	if (error) {
    392 		vrele(devvp);
    393 		return (error);
    394 	}
    395 
    396 	if (!update) {
    397 		int xflags;
    398 
    399 		if (mp->mnt_flag & MNT_RDONLY)
    400 			xflags = FREAD;
    401 		else
    402 			xflags = FREAD|FWRITE;
    403 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    404 		error = VOP_OPEN(devvp, xflags, FSCRED);
    405 		VOP_UNLOCK(devvp);
    406 		if (error)
    407 			goto fail;
    408 		error = ext2fs_mountfs(devvp, mp);
    409 		if (error) {
    410 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    411 			(void)VOP_CLOSE(devvp, xflags, NOCRED);
    412 			VOP_UNLOCK(devvp);
    413 			goto fail;
    414 		}
    415 
    416 		ump = VFSTOUFS(mp);
    417 		fs = ump->um_e2fs;
    418 	} else {
    419 		/*
    420 		 * Update the mount.
    421 		 */
    422 
    423 		/*
    424 		 * The initial mount got a reference on this
    425 		 * device, so drop the one obtained via
    426 		 * namei(), above.
    427 		 */
    428 		vrele(devvp);
    429 
    430 		ump = VFSTOUFS(mp);
    431 		fs = ump->um_e2fs;
    432 		if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    433 			/*
    434 			 * Changing from r/w to r/o
    435 			 */
    436 			flags = WRITECLOSE;
    437 			if (mp->mnt_flag & MNT_FORCE)
    438 				flags |= FORCECLOSE;
    439 			error = ext2fs_flushfiles(mp, flags);
    440 			if (error == 0 &&
    441 			    ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
    442 			    (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
    443 				fs->e2fs.e2fs_state = E2FS_ISCLEAN;
    444 				(void) ext2fs_sbupdate(ump, MNT_WAIT);
    445 			}
    446 			if (error)
    447 				return (error);
    448 			fs->e2fs_ronly = 1;
    449 		}
    450 
    451 		if (mp->mnt_flag & MNT_RELOAD) {
    452 			error = ext2fs_reload(mp, l->l_cred, l);
    453 			if (error)
    454 				return (error);
    455 		}
    456 
    457 		if (fs->e2fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
    458 			/*
    459 			 * Changing from read-only to read/write
    460 			 */
    461 			fs->e2fs_ronly = 0;
    462 			if (fs->e2fs.e2fs_state == E2FS_ISCLEAN)
    463 				fs->e2fs.e2fs_state = 0;
    464 			else
    465 				fs->e2fs.e2fs_state = E2FS_ERRORS;
    466 			fs->e2fs_fmod = 1;
    467 		}
    468 		if (args->fspec == NULL)
    469 			return 0;
    470 	}
    471 
    472 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
    473 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    474 	(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
    475 	    sizeof(fs->e2fs_fsmnt) - 1, &size);
    476 	memset(fs->e2fs_fsmnt + size, 0, sizeof(fs->e2fs_fsmnt) - size);
    477 	if (fs->e2fs.e2fs_rev > E2FS_REV0) {
    478 		(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
    479 		    sizeof(fs->e2fs.e2fs_fsmnt) - 1, &size);
    480 		memset(fs->e2fs.e2fs_fsmnt, 0,
    481 		    sizeof(fs->e2fs.e2fs_fsmnt) - size);
    482 	}
    483 	if (fs->e2fs_fmod != 0) {	/* XXX */
    484 		fs->e2fs_fmod = 0;
    485 		if (fs->e2fs.e2fs_state == 0)
    486 			fs->e2fs.e2fs_wtime = time_second;
    487 		else
    488 			printf("%s: file system not clean; please fsck(8)\n",
    489 				mp->mnt_stat.f_mntfromname);
    490 		(void) ext2fs_cgupdate(ump, MNT_WAIT);
    491 	}
    492 	return (error);
    493 
    494 fail:
    495 	vrele(devvp);
    496 	return (error);
    497 }
    498 
    499 /*
    500  * Reload all incore data for a filesystem (used after running fsck on
    501  * the root filesystem and finding things to fix). The filesystem must
    502  * be mounted read-only.
    503  *
    504  * Things to do to update the mount:
    505  *	1) invalidate all cached meta-data.
    506  *	2) re-read superblock from disk.
    507  *	3) re-read summary information from disk.
    508  *	4) invalidate all inactive vnodes.
    509  *	5) invalidate all cached file data.
    510  *	6) re-read inode data for all active vnodes.
    511  */
    512 int
    513 ext2fs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
    514 {
    515 	struct vnode *vp, *devvp;
    516 	struct inode *ip;
    517 	struct buf *bp;
    518 	struct m_ext2fs *fs;
    519 	struct ext2fs *newfs;
    520 	int i, error;
    521 	void *cp;
    522 	struct ufsmount *ump;
    523 	struct vnode_iterator *marker;
    524 
    525 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
    526 		return (EINVAL);
    527 
    528 	ump = VFSTOUFS(mp);
    529 	/*
    530 	 * Step 1: invalidate all cached meta-data.
    531 	 */
    532 	devvp = ump->um_devvp;
    533 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    534 	error = vinvalbuf(devvp, 0, cred, l, 0, 0);
    535 	VOP_UNLOCK(devvp);
    536 	if (error)
    537 		panic("ext2fs_reload: dirty1");
    538 	/*
    539 	 * Step 2: re-read superblock from disk.
    540 	 */
    541 	error = bread(devvp, SBLOCK, SBSIZE, NOCRED, 0, &bp);
    542 	if (error) {
    543 		return (error);
    544 	}
    545 	newfs = (struct ext2fs *)bp->b_data;
    546 	error = ext2fs_checksb(newfs, (mp->mnt_flag & MNT_RDONLY) != 0);
    547 	if (error) {
    548 		brelse(bp, 0);
    549 		return (error);
    550 	}
    551 
    552 	fs = ump->um_e2fs;
    553 	/*
    554 	 * copy in new superblock, and compute in-memory values
    555 	 */
    556 	e2fs_sbload(newfs, &fs->e2fs);
    557 	fs->e2fs_ncg =
    558 	    howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
    559 	    fs->e2fs.e2fs_bpg);
    560 	fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + LOG_MINBSIZE - DEV_BSHIFT;
    561 	fs->e2fs_bsize = MINBSIZE << fs->e2fs.e2fs_log_bsize;
    562 	fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
    563 	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
    564 	fs->e2fs_bmask = ~fs->e2fs_qbmask;
    565 	fs->e2fs_ngdb =
    566 	    howmany(fs->e2fs_ncg, fs->e2fs_bsize / sizeof(struct ext2_gd));
    567 	fs->e2fs_ipb = fs->e2fs_bsize / EXT2_DINODE_SIZE(fs);
    568 	fs->e2fs_itpg = fs->e2fs.e2fs_ipg / fs->e2fs_ipb;
    569 	brelse(bp, 0);
    570 
    571 	/*
    572 	 * Step 3: re-read summary information from disk.
    573 	 */
    574 
    575 	for (i = 0; i < fs->e2fs_ngdb; i++) {
    576 		error = bread(devvp ,
    577 		    EXT2_FSBTODB(fs, fs->e2fs.e2fs_first_dblock +
    578 		    1 /* superblock */ + i),
    579 		    fs->e2fs_bsize, NOCRED, 0, &bp);
    580 		if (error) {
    581 			return (error);
    582 		}
    583 		e2fs_cgload((struct ext2_gd *)bp->b_data,
    584 		    &fs->e2fs_gd[i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
    585 		    fs->e2fs_bsize);
    586 		brelse(bp, 0);
    587 	}
    588 
    589 	vfs_vnode_iterator_init(mp, &marker);
    590 	while (vfs_vnode_iterator_next(marker, &vp)) {
    591 		/*
    592 		 * Step 4: invalidate all inactive vnodes.
    593 		 */
    594 		if (vrecycle(vp))
    595 			continue;
    596 		/*
    597 		 * Step 5: invalidate all cached file data.
    598 		 */
    599 		if (vn_lock(vp, LK_EXCLUSIVE)) {
    600 			vrele(vp);
    601 			continue;
    602 		}
    603 		if (vinvalbuf(vp, 0, cred, l, 0, 0))
    604 			panic("ext2fs_reload: dirty2");
    605 		/*
    606 		 * Step 6: re-read inode data for all active vnodes.
    607 		 */
    608 		ip = VTOI(vp);
    609 		error = bread(devvp, EXT2_FSBTODB(fs, ino_to_fsba(fs, ip->i_number)),
    610 		    (int)fs->e2fs_bsize, NOCRED, 0, &bp);
    611 		if (error) {
    612 			vput(vp);
    613 			break;
    614 		}
    615 		cp = (char *)bp->b_data +
    616 		    (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE(fs));
    617 		e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
    618 		ext2fs_set_inode_guid(ip);
    619 		brelse(bp, 0);
    620 		vput(vp);
    621 	}
    622 	vfs_vnode_iterator_destroy(marker);
    623 	return (error);
    624 }
    625 
    626 /*
    627  * Common code for mount and mountroot
    628  */
    629 int
    630 ext2fs_mountfs(struct vnode *devvp, struct mount *mp)
    631 {
    632 	struct lwp *l = curlwp;
    633 	struct ufsmount *ump;
    634 	struct buf *bp;
    635 	struct ext2fs *fs;
    636 	struct m_ext2fs *m_fs;
    637 	dev_t dev;
    638 	int error, i, ronly;
    639 	kauth_cred_t cred;
    640 
    641 	dev = devvp->v_rdev;
    642 	cred = l ? l->l_cred : NOCRED;
    643 
    644 	/* Flush out any old buffers remaining from a previous use. */
    645 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    646 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
    647 	VOP_UNLOCK(devvp);
    648 	if (error)
    649 		return (error);
    650 
    651 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    652 
    653 	bp = NULL;
    654 	ump = NULL;
    655 
    656 #ifdef DEBUG_EXT2
    657 	printf("ext2 sb size: %zu\n", sizeof(struct ext2fs));
    658 #endif
    659 	error = bread(devvp, SBLOCK, SBSIZE, cred, 0, &bp);
    660 	if (error)
    661 		goto out;
    662 	fs = (struct ext2fs *)bp->b_data;
    663 	error = ext2fs_checksb(fs, ronly);
    664 	if (error)
    665 		goto out;
    666 	ump = kmem_zalloc(sizeof(*ump), KM_SLEEP);
    667 	ump->um_fstype = UFS1;
    668 	ump->um_ops = &ext2fs_ufsops;
    669 	ump->um_e2fs = kmem_zalloc(sizeof(struct m_ext2fs), KM_SLEEP);
    670 	e2fs_sbload((struct ext2fs *)bp->b_data, &ump->um_e2fs->e2fs);
    671 	brelse(bp, 0);
    672 	bp = NULL;
    673 	m_fs = ump->um_e2fs;
    674 	m_fs->e2fs_ronly = ronly;
    675 
    676 #ifdef DEBUG_EXT2
    677 	printf("ext2 ino size %zu\n", EXT2_DINODE_SIZE(m_fs));
    678 #endif
    679 	if (ronly == 0) {
    680 		if (m_fs->e2fs.e2fs_state == E2FS_ISCLEAN)
    681 			m_fs->e2fs.e2fs_state = 0;
    682 		else
    683 			m_fs->e2fs.e2fs_state = E2FS_ERRORS;
    684 		m_fs->e2fs_fmod = 1;
    685 	}
    686 
    687 	/* compute dynamic sb infos */
    688 	m_fs->e2fs_ncg =
    689 	    howmany(m_fs->e2fs.e2fs_bcount - m_fs->e2fs.e2fs_first_dblock,
    690 	    m_fs->e2fs.e2fs_bpg);
    691 	m_fs->e2fs_fsbtodb = m_fs->e2fs.e2fs_log_bsize + LOG_MINBSIZE - DEV_BSHIFT;
    692 	m_fs->e2fs_bsize = MINBSIZE << m_fs->e2fs.e2fs_log_bsize;
    693 	m_fs->e2fs_bshift = LOG_MINBSIZE + m_fs->e2fs.e2fs_log_bsize;
    694 	m_fs->e2fs_qbmask = m_fs->e2fs_bsize - 1;
    695 	m_fs->e2fs_bmask = ~m_fs->e2fs_qbmask;
    696 	m_fs->e2fs_ngdb =
    697 	    howmany(m_fs->e2fs_ncg, m_fs->e2fs_bsize / sizeof(struct ext2_gd));
    698 	m_fs->e2fs_ipb = m_fs->e2fs_bsize / EXT2_DINODE_SIZE(m_fs);
    699 	m_fs->e2fs_itpg = m_fs->e2fs.e2fs_ipg / m_fs->e2fs_ipb;
    700 
    701 	m_fs->e2fs_gd = kmem_alloc(m_fs->e2fs_ngdb * m_fs->e2fs_bsize, KM_SLEEP);
    702 	for (i = 0; i < m_fs->e2fs_ngdb; i++) {
    703 		error = bread(devvp ,
    704 		    EXT2_FSBTODB(m_fs, m_fs->e2fs.e2fs_first_dblock +
    705 		    1 /* superblock */ + i),
    706 		    m_fs->e2fs_bsize, NOCRED, 0, &bp);
    707 		if (error) {
    708 			kmem_free(m_fs->e2fs_gd,
    709 			    m_fs->e2fs_ngdb * m_fs->e2fs_bsize);
    710 			goto out;
    711 		}
    712 		e2fs_cgload((struct ext2_gd *)bp->b_data,
    713 		    &m_fs->e2fs_gd[
    714 			i * m_fs->e2fs_bsize / sizeof(struct ext2_gd)],
    715 		    m_fs->e2fs_bsize);
    716 		brelse(bp, 0);
    717 		bp = NULL;
    718 	}
    719 
    720 	mp->mnt_data = ump;
    721 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
    722 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_EXT2FS);
    723 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    724 	mp->mnt_stat.f_namemax = EXT2FS_MAXNAMLEN;
    725 	mp->mnt_flag |= MNT_LOCAL;
    726 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
    727 	mp->mnt_fs_bshift = m_fs->e2fs_bshift;
    728 	mp->mnt_iflag |= IMNT_DTYPE;
    729 	ump->um_flags = 0;
    730 	ump->um_mountp = mp;
    731 	ump->um_dev = dev;
    732 	ump->um_devvp = devvp;
    733 	ump->um_nindir = EXT2_NINDIR(m_fs);
    734 	ump->um_lognindir = ffs(EXT2_NINDIR(m_fs)) - 1;
    735 	ump->um_bptrtodb = m_fs->e2fs_fsbtodb;
    736 	ump->um_seqinc = 1; /* no frags */
    737 	ump->um_maxsymlinklen = EXT2_MAXSYMLINKLEN;
    738 	ump->um_dirblksiz = m_fs->e2fs_bsize;
    739 	ump->um_maxfilesize = ((uint64_t)0x80000000 * m_fs->e2fs_bsize - 1);
    740 	spec_node_setmountedfs(devvp, mp);
    741 	return (0);
    742 
    743 out:
    744 	if (bp != NULL)
    745 		brelse(bp, 0);
    746 	if (ump) {
    747 		kmem_free(ump->um_e2fs, sizeof(struct m_ext2fs));
    748 		kmem_free(ump, sizeof(*ump));
    749 		mp->mnt_data = NULL;
    750 	}
    751 	return (error);
    752 }
    753 
    754 /*
    755  * unmount system call
    756  */
    757 int
    758 ext2fs_unmount(struct mount *mp, int mntflags)
    759 {
    760 	struct ufsmount *ump;
    761 	struct m_ext2fs *fs;
    762 	int error, flags;
    763 
    764 	flags = 0;
    765 	if (mntflags & MNT_FORCE)
    766 		flags |= FORCECLOSE;
    767 	if ((error = ext2fs_flushfiles(mp, flags)) != 0)
    768 		return (error);
    769 	ump = VFSTOUFS(mp);
    770 	fs = ump->um_e2fs;
    771 	if (fs->e2fs_ronly == 0 &&
    772 		ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
    773 		(fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
    774 		fs->e2fs.e2fs_state = E2FS_ISCLEAN;
    775 		(void) ext2fs_sbupdate(ump, MNT_WAIT);
    776 	}
    777 	if (ump->um_devvp->v_type != VBAD)
    778 		spec_node_setmountedfs(ump->um_devvp, NULL);
    779 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    780 	error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
    781 	    NOCRED);
    782 	vput(ump->um_devvp);
    783 	kmem_free(fs->e2fs_gd, fs->e2fs_ngdb * fs->e2fs_bsize);
    784 	kmem_free(fs, sizeof(*fs));
    785 	kmem_free(ump, sizeof(*ump));
    786 	mp->mnt_data = NULL;
    787 	mp->mnt_flag &= ~MNT_LOCAL;
    788 	return (error);
    789 }
    790 
    791 /*
    792  * Flush out all the files in a filesystem.
    793  */
    794 int
    795 ext2fs_flushfiles(struct mount *mp, int flags)
    796 {
    797 	extern int doforce;
    798 	int error;
    799 
    800 	if (!doforce)
    801 		flags &= ~FORCECLOSE;
    802 	error = vflush(mp, NULLVP, flags);
    803 	return (error);
    804 }
    805 
    806 /*
    807  * Get file system statistics.
    808  */
    809 int
    810 ext2fs_statvfs(struct mount *mp, struct statvfs *sbp)
    811 {
    812 	struct ufsmount *ump;
    813 	struct m_ext2fs *fs;
    814 	uint32_t overhead, overhead_per_group, ngdb;
    815 	int i, ngroups;
    816 
    817 	ump = VFSTOUFS(mp);
    818 	fs = ump->um_e2fs;
    819 	if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
    820 		panic("ext2fs_statvfs");
    821 
    822 	/*
    823 	 * Compute the overhead (FS structures)
    824 	 */
    825 	overhead_per_group =
    826 	    1 /* block bitmap */ +
    827 	    1 /* inode bitmap */ +
    828 	    fs->e2fs_itpg;
    829 	overhead = fs->e2fs.e2fs_first_dblock +
    830 	    fs->e2fs_ncg * overhead_per_group;
    831 	if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
    832 	    fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
    833 		for (i = 0, ngroups = 0; i < fs->e2fs_ncg; i++) {
    834 			if (cg_has_sb(i))
    835 				ngroups++;
    836 		}
    837 	} else {
    838 		ngroups = fs->e2fs_ncg;
    839 	}
    840 	ngdb = fs->e2fs_ngdb;
    841 	if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
    842 	    fs->e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE)
    843 		ngdb += fs->e2fs.e2fs_reserved_ngdb;
    844 	overhead += ngroups * (1 /* superblock */ + ngdb);
    845 
    846 	sbp->f_bsize = fs->e2fs_bsize;
    847 	sbp->f_frsize = MINBSIZE << fs->e2fs.e2fs_fsize;
    848 	sbp->f_iosize = fs->e2fs_bsize;
    849 	sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
    850 	sbp->f_bfree = fs->e2fs.e2fs_fbcount;
    851 	sbp->f_bresvd = fs->e2fs.e2fs_rbcount;
    852 	if (sbp->f_bfree > sbp->f_bresvd)
    853 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
    854 	else
    855 		sbp->f_bavail = 0;
    856 	sbp->f_files =  fs->e2fs.e2fs_icount;
    857 	sbp->f_ffree = fs->e2fs.e2fs_ficount;
    858 	sbp->f_favail = fs->e2fs.e2fs_ficount;
    859 	sbp->f_fresvd = 0;
    860 	copy_statvfs_info(sbp, mp);
    861 	return (0);
    862 }
    863 
    864 /*
    865  * Go through the disk queues to initiate sandbagged IO;
    866  * go through the inodes to write those that have been modified;
    867  * initiate the writing of the super block if it has been modified.
    868  *
    869  * Note: we are always called with the filesystem marked `MPBUSY'.
    870  */
    871 int
    872 ext2fs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
    873 {
    874 	struct vnode *vp, *mvp;
    875 	struct inode *ip;
    876 	struct ufsmount *ump = VFSTOUFS(mp);
    877 	struct m_ext2fs *fs;
    878 	int error, allerror = 0;
    879 
    880 	fs = ump->um_e2fs;
    881 	if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) {	/* XXX */
    882 		printf("fs = %s\n", fs->e2fs_fsmnt);
    883 		panic("update: rofs mod");
    884 	}
    885 
    886 	/* Allocate a marker vnode. */
    887 	mvp = vnalloc(mp);
    888 
    889 	/*
    890 	 * Write back each (modified) inode.
    891 	 */
    892 	mutex_enter(&mntvnode_lock);
    893 loop:
    894 	/*
    895 	 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
    896 	 * and vclean() can be called indirectly
    897 	 */
    898 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
    899 		vmark(mvp, vp);
    900 		if (vp->v_mount != mp || vismarker(vp))
    901 			continue;
    902 		mutex_enter(vp->v_interlock);
    903 		ip = VTOI(vp);
    904 		if (ip == NULL || (vp->v_iflag & (VI_XLOCK|VI_CLEAN)) != 0 ||
    905 		    vp->v_type == VNON ||
    906 		    ((ip->i_flag &
    907 		      (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
    908 		     LIST_EMPTY(&vp->v_dirtyblkhd) &&
    909 		     UVM_OBJ_IS_CLEAN(&vp->v_uobj)))
    910 		{
    911 			mutex_exit(vp->v_interlock);
    912 			continue;
    913 		}
    914 		mutex_exit(&mntvnode_lock);
    915 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT);
    916 		if (error) {
    917 			mutex_enter(&mntvnode_lock);
    918 			if (error == ENOENT) {
    919 				mutex_enter(&mntvnode_lock);
    920 				(void)vunmark(mvp);
    921 				goto loop;
    922 			}
    923 			continue;
    924 		}
    925 		if (vp->v_type == VREG && waitfor == MNT_LAZY)
    926 			error = ext2fs_update(vp, NULL, NULL, 0);
    927 		else
    928 			error = VOP_FSYNC(vp, cred,
    929 			    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0);
    930 		if (error)
    931 			allerror = error;
    932 		vput(vp);
    933 		mutex_enter(&mntvnode_lock);
    934 	}
    935 	mutex_exit(&mntvnode_lock);
    936 	vnfree(mvp);
    937 	/*
    938 	 * Force stale file system control information to be flushed.
    939 	 */
    940 	if (waitfor != MNT_LAZY) {
    941 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    942 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
    943 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
    944 			allerror = error;
    945 		VOP_UNLOCK(ump->um_devvp);
    946 	}
    947 	/*
    948 	 * Write back modified superblock.
    949 	 */
    950 	if (fs->e2fs_fmod != 0) {
    951 		fs->e2fs_fmod = 0;
    952 		fs->e2fs.e2fs_wtime = time_second;
    953 		if ((error = ext2fs_cgupdate(ump, waitfor)))
    954 			allerror = error;
    955 	}
    956 	return (allerror);
    957 }
    958 
    959 /*
    960  * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
    961  * in from disk.  If it is in core, wait for the lock bit to clear, then
    962  * return the inode locked.  Detection and handling of mount points must be
    963  * done by the calling routine.
    964  */
    965 int
    966 ext2fs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
    967 {
    968 	struct m_ext2fs *fs;
    969 	struct inode *ip;
    970 	struct ufsmount *ump;
    971 	struct buf *bp;
    972 	struct vnode *vp;
    973 	dev_t dev;
    974 	int error;
    975 	void *cp;
    976 
    977 	ump = VFSTOUFS(mp);
    978 	dev = ump->um_dev;
    979 retry:
    980 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
    981 		return (0);
    982 
    983 	/* Allocate a new vnode/inode. */
    984 	error = getnewvnode(VT_EXT2FS, mp, ext2fs_vnodeop_p, NULL, &vp);
    985 	if (error) {
    986 		*vpp = NULL;
    987 		return (error);
    988 	}
    989 	ip = pool_get(&ext2fs_inode_pool, PR_WAITOK);
    990 
    991 	mutex_enter(&ufs_hashlock);
    992 	if ((*vpp = ufs_ihashget(dev, ino, 0)) != NULL) {
    993 		mutex_exit(&ufs_hashlock);
    994 		ungetnewvnode(vp);
    995 		pool_put(&ext2fs_inode_pool, ip);
    996 		goto retry;
    997 	}
    998 
    999 	vp->v_vflag |= VV_LOCKSWORK;
   1000 
   1001 	memset(ip, 0, sizeof(struct inode));
   1002 	vp->v_data = ip;
   1003 	ip->i_vnode = vp;
   1004 	ip->i_ump = ump;
   1005 	ip->i_e2fs = fs = ump->um_e2fs;
   1006 	ip->i_dev = dev;
   1007 	ip->i_number = ino;
   1008 	ip->i_e2fs_last_lblk = 0;
   1009 	ip->i_e2fs_last_blk = 0;
   1010 	genfs_node_init(vp, &ext2fs_genfsops);
   1011 
   1012 	/*
   1013 	 * Put it onto its hash chain and lock it so that other requests for
   1014 	 * this inode will block if they arrive while we are sleeping waiting
   1015 	 * for old data structures to be purged or for the contents of the
   1016 	 * disk portion of this inode to be read.
   1017 	 */
   1018 
   1019 	ufs_ihashins(ip);
   1020 	mutex_exit(&ufs_hashlock);
   1021 
   1022 	/* Read in the disk contents for the inode, copy into the inode. */
   1023 	error = bread(ump->um_devvp, EXT2_FSBTODB(fs, ino_to_fsba(fs, ino)),
   1024 	    (int)fs->e2fs_bsize, NOCRED, 0, &bp);
   1025 	if (error) {
   1026 
   1027 		/*
   1028 		 * The inode does not contain anything useful, so it would
   1029 		 * be misleading to leave it on its hash chain. With mode
   1030 		 * still zero, it will be unlinked and returned to the free
   1031 		 * list by vput().
   1032 		 */
   1033 
   1034 		vput(vp);
   1035 		*vpp = NULL;
   1036 		return (error);
   1037 	}
   1038 	cp = (char *)bp->b_data + (ino_to_fsbo(fs, ino) * EXT2_DINODE_SIZE(fs));
   1039 	ip->i_din.e2fs_din = pool_get(&ext2fs_dinode_pool, PR_WAITOK);
   1040 	e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
   1041 	ext2fs_set_inode_guid(ip);
   1042 	brelse(bp, 0);
   1043 
   1044 	/* If the inode was deleted, reset all fields */
   1045 	if (ip->i_e2fs_dtime != 0) {
   1046 		ip->i_e2fs_mode = 0;
   1047 		(void)ext2fs_setsize(ip, 0);
   1048 		(void)ext2fs_setnblock(ip, 0);
   1049 		memset(ip->i_e2fs_blocks, 0, sizeof(ip->i_e2fs_blocks));
   1050 	}
   1051 
   1052 	/*
   1053 	 * Initialize the vnode from the inode, check for aliases.
   1054 	 */
   1055 
   1056 	error = ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
   1057 	if (error) {
   1058 		vput(vp);
   1059 		*vpp = NULL;
   1060 		return (error);
   1061 	}
   1062 	/*
   1063 	 * Finish inode initialization now that aliasing has been resolved.
   1064 	 */
   1065 
   1066 	ip->i_devvp = ump->um_devvp;
   1067 	vref(ip->i_devvp);
   1068 
   1069 	/*
   1070 	 * Set up a generation number for this inode if it does not
   1071 	 * already have one. This should only happen on old filesystems.
   1072 	 */
   1073 
   1074 	if (ip->i_e2fs_gen == 0) {
   1075 		if (++ext2gennumber < (u_long)time_second)
   1076 			ext2gennumber = time_second;
   1077 		ip->i_e2fs_gen = ext2gennumber;
   1078 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
   1079 			ip->i_flag |= IN_MODIFIED;
   1080 	}
   1081 	uvm_vnp_setsize(vp, ext2fs_size(ip));
   1082 	*vpp = vp;
   1083 	return (0);
   1084 }
   1085 
   1086 /*
   1087  * File handle to vnode
   1088  *
   1089  * Have to be really careful about stale file handles:
   1090  * - check that the inode number is valid
   1091  * - call ext2fs_vget() to get the locked inode
   1092  * - check for an unallocated inode (i_mode == 0)
   1093  */
   1094 int
   1095 ext2fs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
   1096 {
   1097 	struct inode *ip;
   1098 	struct vnode *nvp;
   1099 	int error;
   1100 	struct ufid ufh;
   1101 	struct m_ext2fs *fs;
   1102 
   1103 	if (fhp->fid_len != sizeof(struct ufid))
   1104 		return EINVAL;
   1105 
   1106 	memcpy(&ufh, fhp, sizeof(struct ufid));
   1107 	fs = VFSTOUFS(mp)->um_e2fs;
   1108 	if ((ufh.ufid_ino < EXT2_FIRSTINO && ufh.ufid_ino != EXT2_ROOTINO) ||
   1109 		ufh.ufid_ino >= fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
   1110 		return (ESTALE);
   1111 
   1112 	if ((error = VFS_VGET(mp, ufh.ufid_ino, &nvp)) != 0) {
   1113 		*vpp = NULLVP;
   1114 		return (error);
   1115 	}
   1116 	ip = VTOI(nvp);
   1117 	if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0 ||
   1118 		ip->i_e2fs_gen != ufh.ufid_gen) {
   1119 		vput(nvp);
   1120 		*vpp = NULLVP;
   1121 		return (ESTALE);
   1122 	}
   1123 	*vpp = nvp;
   1124 	return (0);
   1125 }
   1126 
   1127 /*
   1128  * Vnode pointer to File handle
   1129  */
   1130 /* ARGSUSED */
   1131 int
   1132 ext2fs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
   1133 {
   1134 	struct inode *ip;
   1135 	struct ufid ufh;
   1136 
   1137 	if (*fh_size < sizeof(struct ufid)) {
   1138 		*fh_size = sizeof(struct ufid);
   1139 		return E2BIG;
   1140 	}
   1141 	*fh_size = sizeof(struct ufid);
   1142 
   1143 	ip = VTOI(vp);
   1144 	memset(&ufh, 0, sizeof(ufh));
   1145 	ufh.ufid_len = sizeof(struct ufid);
   1146 	ufh.ufid_ino = ip->i_number;
   1147 	ufh.ufid_gen = ip->i_e2fs_gen;
   1148 	memcpy(fhp, &ufh, sizeof(ufh));
   1149 	return (0);
   1150 }
   1151 
   1152 /*
   1153  * Write a superblock and associated information back to disk.
   1154  */
   1155 int
   1156 ext2fs_sbupdate(struct ufsmount *mp, int waitfor)
   1157 {
   1158 	struct m_ext2fs *fs = mp->um_e2fs;
   1159 	struct buf *bp;
   1160 	int error = 0;
   1161 
   1162 	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
   1163 	e2fs_sbsave(&fs->e2fs, (struct ext2fs*)bp->b_data);
   1164 	if (waitfor == MNT_WAIT)
   1165 		error = bwrite(bp);
   1166 	else
   1167 		bawrite(bp);
   1168 	return (error);
   1169 }
   1170 
   1171 int
   1172 ext2fs_cgupdate(struct ufsmount *mp, int waitfor)
   1173 {
   1174 	struct m_ext2fs *fs = mp->um_e2fs;
   1175 	struct buf *bp;
   1176 	int i, error = 0, allerror = 0;
   1177 
   1178 	allerror = ext2fs_sbupdate(mp, waitfor);
   1179 	for (i = 0; i < fs->e2fs_ngdb; i++) {
   1180 		bp = getblk(mp->um_devvp, EXT2_FSBTODB(fs,
   1181 		    fs->e2fs.e2fs_first_dblock +
   1182 		    1 /* superblock */ + i), fs->e2fs_bsize, 0, 0);
   1183 		e2fs_cgsave(&fs->e2fs_gd[
   1184 		    i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
   1185 		    (struct ext2_gd *)bp->b_data, fs->e2fs_bsize);
   1186 		if (waitfor == MNT_WAIT)
   1187 			error = bwrite(bp);
   1188 		else
   1189 			bawrite(bp);
   1190 	}
   1191 
   1192 	if (!allerror && error)
   1193 		allerror = error;
   1194 	return (allerror);
   1195 }
   1196 
   1197 static int
   1198 ext2fs_checksb(struct ext2fs *fs, int ronly)
   1199 {
   1200 	uint32_t u32;
   1201 
   1202 	if (fs2h16(fs->e2fs_magic) != E2FS_MAGIC) {
   1203 		return (EINVAL);		/* XXX needs translation */
   1204 	}
   1205 	if (fs2h32(fs->e2fs_rev) > E2FS_REV1) {
   1206 #ifdef DIAGNOSTIC
   1207 		printf("ext2fs: unsupported revision number: %x\n",
   1208 		    fs2h32(fs->e2fs_rev));
   1209 #endif
   1210 		return (EINVAL);		/* XXX needs translation */
   1211 	}
   1212 	if (fs2h32(fs->e2fs_log_bsize) > 2) { /* block size = 1024|2048|4096 */
   1213 #ifdef DIAGNOSTIC
   1214 		printf("ext2fs: bad block size: %d "
   1215 		    "(expected <= 2 for ext2 fs)\n",
   1216 		    fs2h32(fs->e2fs_log_bsize));
   1217 #endif
   1218 		return (EINVAL);	   /* XXX needs translation */
   1219 	}
   1220 	if (fs2h32(fs->e2fs_rev) > E2FS_REV0) {
   1221 		char buf[256];
   1222 		if (fs2h32(fs->e2fs_first_ino) != EXT2_FIRSTINO) {
   1223 			printf("ext2fs: unsupported first inode position\n");
   1224 			return (EINVAL);      /* XXX needs translation */
   1225 		}
   1226 		u32 = fs2h32(fs->e2fs_features_incompat) & ~EXT2F_INCOMPAT_SUPP;
   1227 		if (u32) {
   1228 			snprintb(buf, sizeof(buf), EXT2F_INCOMPAT_BITS, u32);
   1229 			printf("ext2fs: unsupported incompat features: %s\n",
   1230 			    buf);
   1231 			return EINVAL;	/* XXX needs translation */
   1232 		}
   1233 		u32 = fs2h32(fs->e2fs_features_rocompat) & ~EXT2F_ROCOMPAT_SUPP;
   1234 		if (!ronly && u32) {
   1235 			snprintb(buf, sizeof(buf), EXT2F_ROCOMPAT_BITS, u32);
   1236 			printf("ext2fs: unsupported ro-incompat features: %s\n",
   1237 			    buf);
   1238 			return EROFS;	/* XXX needs translation */
   1239 		}
   1240 	}
   1241 	return (0);
   1242 }
   1243