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