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