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