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