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