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