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