Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_vfsops.c revision 1.187
      1 /*	$NetBSD: ext2fs_vfsops.c,v 1.187 2015/02/19 21:31:44 maxv 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.187 2015/02/19 21:31:44 maxv 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/pool.h>
     86 #include <sys/lock.h>
     87 #include <sys/conf.h>
     88 #include <sys/kauth.h>
     89 #include <sys/module.h>
     90 
     91 #include <miscfs/genfs/genfs.h>
     92 #include <miscfs/specfs/specdev.h>
     93 
     94 #include <ufs/ufs/quota.h>
     95 #include <ufs/ufs/ufsmount.h>
     96 #include <ufs/ufs/inode.h>
     97 #include <ufs/ufs/dir.h>
     98 #include <ufs/ufs/ufs_extern.h>
     99 
    100 #include <ufs/ext2fs/ext2fs.h>
    101 #include <ufs/ext2fs/ext2fs_dir.h>
    102 #include <ufs/ext2fs/ext2fs_extern.h>
    103 
    104 MODULE(MODULE_CLASS_VFS, ext2fs, "ffs");
    105 
    106 int ext2fs_sbupdate(struct ufsmount *, int);
    107 static int ext2fs_checksb(struct ext2fs *, int);
    108 
    109 static struct sysctllog *ext2fs_sysctl_log;
    110 
    111 extern const struct vnodeopv_desc ext2fs_vnodeop_opv_desc;
    112 extern const struct vnodeopv_desc ext2fs_specop_opv_desc;
    113 extern const struct vnodeopv_desc ext2fs_fifoop_opv_desc;
    114 
    115 const struct vnodeopv_desc * const ext2fs_vnodeopv_descs[] = {
    116 	&ext2fs_vnodeop_opv_desc,
    117 	&ext2fs_specop_opv_desc,
    118 	&ext2fs_fifoop_opv_desc,
    119 	NULL,
    120 };
    121 
    122 struct vfsops ext2fs_vfsops = {
    123 	.vfs_name = MOUNT_EXT2FS,
    124 	.vfs_min_mount_data = sizeof (struct ufs_args),
    125 	.vfs_mount = ext2fs_mount,
    126 	.vfs_start = ufs_start,
    127 	.vfs_unmount = ext2fs_unmount,
    128 	.vfs_root = ufs_root,
    129 	.vfs_quotactl = ufs_quotactl,
    130 	.vfs_statvfs = ext2fs_statvfs,
    131 	.vfs_sync = ext2fs_sync,
    132 	.vfs_vget = ufs_vget,
    133 	.vfs_loadvnode = ext2fs_loadvnode,
    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 
    251 int
    252 ext2fs_mountroot(void)
    253 {
    254 	extern struct vnode *rootvp;
    255 	struct m_ext2fs *fs;
    256 	struct mount *mp;
    257 	struct ufsmount *ump;
    258 	int error;
    259 
    260 	if (device_class(root_device) != DV_DISK)
    261 		return (ENODEV);
    262 
    263 	if ((error = vfs_rootmountalloc(MOUNT_EXT2FS, "root_device", &mp))) {
    264 		vrele(rootvp);
    265 		return (error);
    266 	}
    267 
    268 	if ((error = ext2fs_mountfs(rootvp, mp)) != 0) {
    269 		vfs_unbusy(mp, false, NULL);
    270 		vfs_destroy(mp);
    271 		return (error);
    272 	}
    273 	mountlist_append(mp);
    274 	ump = VFSTOUFS(mp);
    275 	fs = ump->um_e2fs;
    276 	memset(fs->e2fs_fsmnt, 0, sizeof(fs->e2fs_fsmnt));
    277 	(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
    278 	    sizeof(fs->e2fs_fsmnt) - 1, 0);
    279 	if (fs->e2fs.e2fs_rev > E2FS_REV0) {
    280 		memset(fs->e2fs.e2fs_fsmnt, 0, sizeof(fs->e2fs.e2fs_fsmnt));
    281 		(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
    282 		    sizeof(fs->e2fs.e2fs_fsmnt) - 1, 0);
    283 	}
    284 	(void)ext2fs_statvfs(mp, &mp->mnt_stat);
    285 	vfs_unbusy(mp, false, NULL);
    286 	setrootfstime((time_t)fs->e2fs.e2fs_wtime);
    287 	return (0);
    288 }
    289 
    290 /*
    291  * VFS Operations.
    292  *
    293  * mount system call
    294  */
    295 int
    296 ext2fs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    297 {
    298 	struct lwp *l = curlwp;
    299 	struct vnode *devvp;
    300 	struct ufs_args *args = data;
    301 	struct ufsmount *ump = NULL;
    302 	struct m_ext2fs *fs;
    303 	size_t size;
    304 	int error = 0, flags, update;
    305 	mode_t accessmode;
    306 
    307 	if (args == NULL)
    308 		return EINVAL;
    309 	if (*data_len < sizeof *args)
    310 		return EINVAL;
    311 
    312 	if (mp->mnt_flag & MNT_GETARGS) {
    313 		ump = VFSTOUFS(mp);
    314 		if (ump == NULL)
    315 			return EIO;
    316 		memset(args, 0, sizeof *args);
    317 		args->fspec = NULL;
    318 		*data_len = sizeof *args;
    319 		return 0;
    320 	}
    321 
    322 	update = mp->mnt_flag & MNT_UPDATE;
    323 
    324 	/* Check arguments */
    325 	if (args->fspec != NULL) {
    326 		/*
    327 		 * Look up the name and verify that it's sane.
    328 		 */
    329 		error = namei_simple_user(args->fspec,
    330 					NSM_FOLLOW_NOEMULROOT, &devvp);
    331 		if (error != 0)
    332 			return (error);
    333 
    334 		if (!update) {
    335 			/*
    336 			 * Be sure this is a valid block device
    337 			 */
    338 			if (devvp->v_type != VBLK)
    339 				error = ENOTBLK;
    340 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
    341 				error = ENXIO;
    342 		} else {
    343 		        /*
    344 			 * Be sure we're still naming the same device
    345 			 * used for our initial mount
    346 			 */
    347 			ump = VFSTOUFS(mp);
    348 			if (devvp != ump->um_devvp) {
    349 				if (devvp->v_rdev != ump->um_devvp->v_rdev)
    350 					error = EINVAL;
    351 				else {
    352 					vrele(devvp);
    353 					devvp = ump->um_devvp;
    354 					vref(devvp);
    355 				}
    356 			}
    357 		}
    358 	} else {
    359 		if (!update) {
    360 			/* New mounts must have a filename for the device */
    361 			return (EINVAL);
    362 		} else {
    363 			ump = VFSTOUFS(mp);
    364 			devvp = ump->um_devvp;
    365 			vref(devvp);
    366 		}
    367 	}
    368 
    369 	/*
    370 	 * If mount by non-root, then verify that user has necessary
    371 	 * permissions on the device.
    372 	 *
    373 	 * Permission to update a mount is checked higher, so here we presume
    374 	 * updating the mount is okay (for example, as far as securelevel goes)
    375 	 * which leaves us with the normal check.
    376 	 */
    377 	if (error == 0) {
    378 		accessmode = VREAD;
    379 		if (update ?
    380 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
    381 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    382 			accessmode |= VWRITE;
    383 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    384 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    385 		    KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
    386 		    KAUTH_ARG(accessmode));
    387 		VOP_UNLOCK(devvp);
    388 	}
    389 
    390 	if (error) {
    391 		vrele(devvp);
    392 		return (error);
    393 	}
    394 
    395 	if (!update) {
    396 		int xflags;
    397 
    398 		if (mp->mnt_flag & MNT_RDONLY)
    399 			xflags = FREAD;
    400 		else
    401 			xflags = FREAD|FWRITE;
    402 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    403 		error = VOP_OPEN(devvp, xflags, FSCRED);
    404 		VOP_UNLOCK(devvp);
    405 		if (error)
    406 			goto fail;
    407 		error = ext2fs_mountfs(devvp, mp);
    408 		if (error) {
    409 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    410 			(void)VOP_CLOSE(devvp, xflags, NOCRED);
    411 			VOP_UNLOCK(devvp);
    412 			goto fail;
    413 		}
    414 
    415 		ump = VFSTOUFS(mp);
    416 		fs = ump->um_e2fs;
    417 	} else {
    418 		/*
    419 		 * Update the mount.
    420 		 */
    421 
    422 		/*
    423 		 * The initial mount got a reference on this
    424 		 * device, so drop the one obtained via
    425 		 * namei(), above.
    426 		 */
    427 		vrele(devvp);
    428 
    429 		ump = VFSTOUFS(mp);
    430 		fs = ump->um_e2fs;
    431 		if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    432 			/*
    433 			 * Changing from r/w to r/o
    434 			 */
    435 			flags = WRITECLOSE;
    436 			if (mp->mnt_flag & MNT_FORCE)
    437 				flags |= FORCECLOSE;
    438 			error = ext2fs_flushfiles(mp, flags);
    439 			if (error == 0 &&
    440 			    ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
    441 			    (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
    442 				fs->e2fs.e2fs_state = E2FS_ISCLEAN;
    443 				(void) ext2fs_sbupdate(ump, MNT_WAIT);
    444 			}
    445 			if (error)
    446 				return (error);
    447 			fs->e2fs_ronly = 1;
    448 		}
    449 
    450 		if (mp->mnt_flag & MNT_RELOAD) {
    451 			error = ext2fs_reload(mp, l->l_cred, l);
    452 			if (error)
    453 				return (error);
    454 		}
    455 
    456 		if (fs->e2fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
    457 			/*
    458 			 * Changing from read-only to read/write
    459 			 */
    460 			fs->e2fs_ronly = 0;
    461 			if (fs->e2fs.e2fs_state == E2FS_ISCLEAN)
    462 				fs->e2fs.e2fs_state = 0;
    463 			else
    464 				fs->e2fs.e2fs_state = E2FS_ERRORS;
    465 			fs->e2fs_fmod = 1;
    466 		}
    467 		if (args->fspec == NULL)
    468 			return 0;
    469 	}
    470 
    471 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
    472 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    473 	(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
    474 	    sizeof(fs->e2fs_fsmnt) - 1, &size);
    475 	memset(fs->e2fs_fsmnt + size, 0, sizeof(fs->e2fs_fsmnt) - size);
    476 	if (fs->e2fs.e2fs_rev > E2FS_REV0) {
    477 		(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
    478 		    sizeof(fs->e2fs.e2fs_fsmnt) - 1, &size);
    479 		memset(fs->e2fs.e2fs_fsmnt, 0,
    480 		    sizeof(fs->e2fs.e2fs_fsmnt) - size);
    481 	}
    482 	if (fs->e2fs_fmod != 0) {	/* XXX */
    483 		fs->e2fs_fmod = 0;
    484 		if (fs->e2fs.e2fs_state == 0)
    485 			fs->e2fs.e2fs_wtime = time_second;
    486 		else
    487 			printf("%s: file system not clean; please fsck(8)\n",
    488 				mp->mnt_stat.f_mntfromname);
    489 		(void) ext2fs_cgupdate(ump, MNT_WAIT);
    490 	}
    491 	return (error);
    492 
    493 fail:
    494 	vrele(devvp);
    495 	return (error);
    496 }
    497 
    498 /*
    499  * Reload all incore data for a filesystem (used after running fsck on
    500  * the root filesystem and finding things to fix). The filesystem must
    501  * be mounted read-only.
    502  *
    503  * Things to do to update the mount:
    504  *	1) invalidate all cached meta-data.
    505  *	2) re-read superblock from disk.
    506  *	3) re-read summary information from disk.
    507  *	4) invalidate all inactive vnodes.
    508  *	5) invalidate all cached file data.
    509  *	6) re-read inode data for all active vnodes.
    510  */
    511 int
    512 ext2fs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
    513 {
    514 	struct vnode *vp, *devvp;
    515 	struct inode *ip;
    516 	struct buf *bp;
    517 	struct m_ext2fs *fs;
    518 	struct ext2fs *newfs;
    519 	int i, error;
    520 	void *cp;
    521 	struct ufsmount *ump;
    522 	struct vnode_iterator *marker;
    523 
    524 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
    525 		return (EINVAL);
    526 
    527 	ump = VFSTOUFS(mp);
    528 	/*
    529 	 * Step 1: invalidate all cached meta-data.
    530 	 */
    531 	devvp = ump->um_devvp;
    532 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    533 	error = vinvalbuf(devvp, 0, cred, l, 0, 0);
    534 	VOP_UNLOCK(devvp);
    535 	if (error)
    536 		panic("ext2fs_reload: dirty1");
    537 	/*
    538 	 * Step 2: re-read superblock from disk.
    539 	 */
    540 	error = bread(devvp, SBLOCK, SBSIZE, NOCRED, 0, &bp);
    541 	if (error) {
    542 		return (error);
    543 	}
    544 	newfs = (struct ext2fs *)bp->b_data;
    545 	error = ext2fs_checksb(newfs, (mp->mnt_flag & MNT_RDONLY) != 0);
    546 	if (error) {
    547 		brelse(bp, 0);
    548 		return (error);
    549 	}
    550 
    551 	fs = ump->um_e2fs;
    552 	/*
    553 	 * copy in new superblock, and compute in-memory values
    554 	 */
    555 	e2fs_sbload(newfs, &fs->e2fs);
    556 	fs->e2fs_ncg =
    557 	    howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
    558 	    fs->e2fs.e2fs_bpg);
    559 	fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + LOG_MINBSIZE - DEV_BSHIFT;
    560 	fs->e2fs_bsize = MINBSIZE << fs->e2fs.e2fs_log_bsize;
    561 	fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
    562 	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
    563 	fs->e2fs_bmask = ~fs->e2fs_qbmask;
    564 	fs->e2fs_ngdb =
    565 	    howmany(fs->e2fs_ncg, fs->e2fs_bsize / sizeof(struct ext2_gd));
    566 	fs->e2fs_ipb = fs->e2fs_bsize / EXT2_DINODE_SIZE(fs);
    567 	fs->e2fs_itpg = fs->e2fs.e2fs_ipg / fs->e2fs_ipb;
    568 	brelse(bp, 0);
    569 
    570 	/*
    571 	 * Step 3: re-read summary information from disk.
    572 	 */
    573 
    574 	for (i = 0; i < fs->e2fs_ngdb; i++) {
    575 		error = bread(devvp ,
    576 		    EXT2_FSBTODB(fs, fs->e2fs.e2fs_first_dblock +
    577 		    1 /* superblock */ + i),
    578 		    fs->e2fs_bsize, NOCRED, 0, &bp);
    579 		if (error) {
    580 			return (error);
    581 		}
    582 		e2fs_cgload((struct ext2_gd *)bp->b_data,
    583 		    &fs->e2fs_gd[i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
    584 		    fs->e2fs_bsize);
    585 		brelse(bp, 0);
    586 	}
    587 
    588 	vfs_vnode_iterator_init(mp, &marker);
    589 	while ((vp = vfs_vnode_iterator_next(marker, NULL, NULL))) {
    590 		/*
    591 		 * Step 4: invalidate all inactive vnodes.
    592 		 */
    593 		if (vrecycle(vp))
    594 			continue;
    595 		/*
    596 		 * Step 5: invalidate all cached file data.
    597 		 */
    598 		if (vn_lock(vp, LK_EXCLUSIVE)) {
    599 			vrele(vp);
    600 			continue;
    601 		}
    602 		if (vinvalbuf(vp, 0, cred, l, 0, 0))
    603 			panic("ext2fs_reload: dirty2");
    604 		/*
    605 		 * Step 6: re-read inode data for all active vnodes.
    606 		 */
    607 		ip = VTOI(vp);
    608 		error = bread(devvp, EXT2_FSBTODB(fs, ino_to_fsba(fs, ip->i_number)),
    609 		    (int)fs->e2fs_bsize, NOCRED, 0, &bp);
    610 		if (error) {
    611 			vput(vp);
    612 			break;
    613 		}
    614 		cp = (char *)bp->b_data +
    615 		    (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE(fs));
    616 		e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
    617 		ext2fs_set_inode_guid(ip);
    618 		brelse(bp, 0);
    619 		vput(vp);
    620 	}
    621 	vfs_vnode_iterator_destroy(marker);
    622 	return (error);
    623 }
    624 
    625 /*
    626  * Common code for mount and mountroot
    627  */
    628 int
    629 ext2fs_mountfs(struct vnode *devvp, struct mount *mp)
    630 {
    631 	struct lwp *l = curlwp;
    632 	struct ufsmount *ump;
    633 	struct buf *bp;
    634 	struct ext2fs *fs;
    635 	struct m_ext2fs *m_fs;
    636 	dev_t dev;
    637 	int error, i, ronly;
    638 	kauth_cred_t cred;
    639 
    640 	dev = devvp->v_rdev;
    641 	cred = l->l_cred;
    642 
    643 	/* Flush out any old buffers remaining from a previous use. */
    644 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    645 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
    646 	VOP_UNLOCK(devvp);
    647 	if (error)
    648 		return (error);
    649 
    650 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    651 
    652 	bp = NULL;
    653 	ump = NULL;
    654 
    655 #ifdef DEBUG_EXT2
    656 	printf("ext2 sb size: %zu\n", sizeof(struct ext2fs));
    657 #endif
    658 	error = bread(devvp, SBLOCK, SBSIZE, cred, 0, &bp);
    659 	if (error)
    660 		goto out;
    661 	fs = (struct ext2fs *)bp->b_data;
    662 	error = ext2fs_checksb(fs, ronly);
    663 	if (error)
    664 		goto out;
    665 	ump = kmem_zalloc(sizeof(*ump), KM_SLEEP);
    666 	ump->um_fstype = UFS1;
    667 	ump->um_ops = &ext2fs_ufsops;
    668 	ump->um_e2fs = kmem_zalloc(sizeof(struct m_ext2fs), KM_SLEEP);
    669 	e2fs_sbload((struct ext2fs *)bp->b_data, &ump->um_e2fs->e2fs);
    670 	brelse(bp, 0);
    671 	bp = NULL;
    672 	m_fs = ump->um_e2fs;
    673 	m_fs->e2fs_ronly = ronly;
    674 
    675 #ifdef DEBUG_EXT2
    676 	printf("ext2 ino size %zu\n", EXT2_DINODE_SIZE(m_fs));
    677 #endif
    678 	if (ronly == 0) {
    679 		if (m_fs->e2fs.e2fs_state == E2FS_ISCLEAN)
    680 			m_fs->e2fs.e2fs_state = 0;
    681 		else
    682 			m_fs->e2fs.e2fs_state = E2FS_ERRORS;
    683 		m_fs->e2fs_fmod = 1;
    684 	}
    685 
    686 	/* compute dynamic sb infos */
    687 	m_fs->e2fs_ncg =
    688 	    howmany(m_fs->e2fs.e2fs_bcount - m_fs->e2fs.e2fs_first_dblock,
    689 	    m_fs->e2fs.e2fs_bpg);
    690 	m_fs->e2fs_fsbtodb = m_fs->e2fs.e2fs_log_bsize + LOG_MINBSIZE - DEV_BSHIFT;
    691 	m_fs->e2fs_bsize = MINBSIZE << m_fs->e2fs.e2fs_log_bsize;
    692 	m_fs->e2fs_bshift = LOG_MINBSIZE + m_fs->e2fs.e2fs_log_bsize;
    693 	m_fs->e2fs_qbmask = m_fs->e2fs_bsize - 1;
    694 	m_fs->e2fs_bmask = ~m_fs->e2fs_qbmask;
    695 	m_fs->e2fs_ngdb =
    696 	    howmany(m_fs->e2fs_ncg, m_fs->e2fs_bsize / sizeof(struct ext2_gd));
    697 	m_fs->e2fs_ipb = m_fs->e2fs_bsize / EXT2_DINODE_SIZE(m_fs);
    698 	m_fs->e2fs_itpg = m_fs->e2fs.e2fs_ipg / m_fs->e2fs_ipb;
    699 
    700 	m_fs->e2fs_gd = kmem_alloc(m_fs->e2fs_ngdb * m_fs->e2fs_bsize, KM_SLEEP);
    701 	for (i = 0; i < m_fs->e2fs_ngdb; i++) {
    702 		error = bread(devvp ,
    703 		    EXT2_FSBTODB(m_fs, m_fs->e2fs.e2fs_first_dblock +
    704 		    1 /* superblock */ + i),
    705 		    m_fs->e2fs_bsize, NOCRED, 0, &bp);
    706 		if (error) {
    707 			kmem_free(m_fs->e2fs_gd,
    708 			    m_fs->e2fs_ngdb * m_fs->e2fs_bsize);
    709 			goto out;
    710 		}
    711 		e2fs_cgload((struct ext2_gd *)bp->b_data,
    712 		    &m_fs->e2fs_gd[
    713 			i * m_fs->e2fs_bsize / sizeof(struct ext2_gd)],
    714 		    m_fs->e2fs_bsize);
    715 		brelse(bp, 0);
    716 		bp = NULL;
    717 	}
    718 
    719 	mp->mnt_data = ump;
    720 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
    721 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_EXT2FS);
    722 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    723 	mp->mnt_stat.f_namemax = EXT2FS_MAXNAMLEN;
    724 	mp->mnt_flag |= MNT_LOCAL;
    725 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
    726 	mp->mnt_fs_bshift = m_fs->e2fs_bshift;
    727 	mp->mnt_iflag |= IMNT_DTYPE;
    728 	ump->um_flags = 0;
    729 	ump->um_mountp = mp;
    730 	ump->um_dev = dev;
    731 	ump->um_devvp = devvp;
    732 	ump->um_nindir = EXT2_NINDIR(m_fs);
    733 	ump->um_lognindir = ffs(EXT2_NINDIR(m_fs)) - 1;
    734 	ump->um_bptrtodb = m_fs->e2fs_fsbtodb;
    735 	ump->um_seqinc = 1; /* no frags */
    736 	ump->um_maxsymlinklen = EXT2_MAXSYMLINKLEN;
    737 	ump->um_dirblksiz = m_fs->e2fs_bsize;
    738 	ump->um_maxfilesize = ((uint64_t)0x80000000 * m_fs->e2fs_bsize - 1);
    739 	spec_node_setmountedfs(devvp, mp);
    740 	return (0);
    741 
    742 out:
    743 	if (bp != NULL)
    744 		brelse(bp, 0);
    745 	if (ump) {
    746 		kmem_free(ump->um_e2fs, sizeof(struct m_ext2fs));
    747 		kmem_free(ump, sizeof(*ump));
    748 		mp->mnt_data = NULL;
    749 	}
    750 	return (error);
    751 }
    752 
    753 /*
    754  * unmount system call
    755  */
    756 int
    757 ext2fs_unmount(struct mount *mp, int mntflags)
    758 {
    759 	struct ufsmount *ump;
    760 	struct m_ext2fs *fs;
    761 	int error, flags;
    762 
    763 	flags = 0;
    764 	if (mntflags & MNT_FORCE)
    765 		flags |= FORCECLOSE;
    766 	if ((error = ext2fs_flushfiles(mp, flags)) != 0)
    767 		return (error);
    768 	ump = VFSTOUFS(mp);
    769 	fs = ump->um_e2fs;
    770 	if (fs->e2fs_ronly == 0 &&
    771 		ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
    772 		(fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
    773 		fs->e2fs.e2fs_state = E2FS_ISCLEAN;
    774 		(void) ext2fs_sbupdate(ump, MNT_WAIT);
    775 	}
    776 	if (ump->um_devvp->v_type != VBAD)
    777 		spec_node_setmountedfs(ump->um_devvp, NULL);
    778 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    779 	error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
    780 	    NOCRED);
    781 	vput(ump->um_devvp);
    782 	kmem_free(fs->e2fs_gd, fs->e2fs_ngdb * fs->e2fs_bsize);
    783 	kmem_free(fs, sizeof(*fs));
    784 	kmem_free(ump, sizeof(*ump));
    785 	mp->mnt_data = NULL;
    786 	mp->mnt_flag &= ~MNT_LOCAL;
    787 	return (error);
    788 }
    789 
    790 /*
    791  * Flush out all the files in a filesystem.
    792  */
    793 int
    794 ext2fs_flushfiles(struct mount *mp, int flags)
    795 {
    796 	extern int doforce;
    797 	int error;
    798 
    799 	if (!doforce)
    800 		flags &= ~FORCECLOSE;
    801 	error = vflush(mp, NULLVP, flags);
    802 	return (error);
    803 }
    804 
    805 /*
    806  * Get file system statistics.
    807  */
    808 int
    809 ext2fs_statvfs(struct mount *mp, struct statvfs *sbp)
    810 {
    811 	struct ufsmount *ump;
    812 	struct m_ext2fs *fs;
    813 	uint32_t overhead, overhead_per_group, ngdb;
    814 	int i, ngroups;
    815 
    816 	ump = VFSTOUFS(mp);
    817 	fs = ump->um_e2fs;
    818 	if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
    819 		panic("ext2fs_statvfs");
    820 
    821 	/*
    822 	 * Compute the overhead (FS structures)
    823 	 */
    824 	overhead_per_group =
    825 	    1 /* block bitmap */ +
    826 	    1 /* inode bitmap */ +
    827 	    fs->e2fs_itpg;
    828 	overhead = fs->e2fs.e2fs_first_dblock +
    829 	    fs->e2fs_ncg * overhead_per_group;
    830 	if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
    831 	    fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
    832 		for (i = 0, ngroups = 0; i < fs->e2fs_ncg; i++) {
    833 			if (cg_has_sb(i))
    834 				ngroups++;
    835 		}
    836 	} else {
    837 		ngroups = fs->e2fs_ncg;
    838 	}
    839 	ngdb = fs->e2fs_ngdb;
    840 	if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
    841 	    fs->e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE)
    842 		ngdb += fs->e2fs.e2fs_reserved_ngdb;
    843 	overhead += ngroups * (1 /* superblock */ + ngdb);
    844 
    845 	sbp->f_bsize = fs->e2fs_bsize;
    846 	sbp->f_frsize = MINBSIZE << fs->e2fs.e2fs_fsize;
    847 	sbp->f_iosize = fs->e2fs_bsize;
    848 	sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
    849 	sbp->f_bfree = fs->e2fs.e2fs_fbcount;
    850 	sbp->f_bresvd = fs->e2fs.e2fs_rbcount;
    851 	if (sbp->f_bfree > sbp->f_bresvd)
    852 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
    853 	else
    854 		sbp->f_bavail = 0;
    855 	sbp->f_files =  fs->e2fs.e2fs_icount;
    856 	sbp->f_ffree = fs->e2fs.e2fs_ficount;
    857 	sbp->f_favail = fs->e2fs.e2fs_ficount;
    858 	sbp->f_fresvd = 0;
    859 	copy_statvfs_info(sbp, mp);
    860 	return (0);
    861 }
    862 
    863 static bool
    864 ext2fs_sync_selector(void *cl, struct vnode *vp)
    865 {
    866 	struct inode *ip;
    867 
    868 	ip = VTOI(vp);
    869 	/*
    870 	 * Skip the vnode/inode if inaccessible.
    871 	 */
    872 	if (ip == NULL || vp->v_type == VNON)
    873 		return false;
    874 
    875 	if (((ip->i_flag &
    876 	      (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
    877 	     LIST_EMPTY(&vp->v_dirtyblkhd) &&
    878 	     UVM_OBJ_IS_CLEAN(&vp->v_uobj)))
    879 		return false;
    880 	return true;
    881 }
    882 
    883 /*
    884  * Go through the disk queues to initiate sandbagged IO;
    885  * go through the inodes to write those that have been modified;
    886  * initiate the writing of the super block if it has been modified.
    887  *
    888  * Note: we are always called with the filesystem marked `MPBUSY'.
    889  */
    890 int
    891 ext2fs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
    892 {
    893 	struct vnode *vp;
    894 	struct ufsmount *ump = VFSTOUFS(mp);
    895 	struct m_ext2fs *fs;
    896 	struct vnode_iterator *marker;
    897 	int error, allerror = 0;
    898 
    899 	fs = ump->um_e2fs;
    900 	if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) {	/* XXX */
    901 		printf("fs = %s\n", fs->e2fs_fsmnt);
    902 		panic("update: rofs mod");
    903 	}
    904 
    905 	/*
    906 	 * Write back each (modified) inode.
    907 	 */
    908 	vfs_vnode_iterator_init(mp, &marker);
    909 	while ((vp = vfs_vnode_iterator_next(marker, ext2fs_sync_selector,
    910 	    NULL)))
    911 	{
    912 		error = vn_lock(vp, LK_EXCLUSIVE);
    913 		if (error) {
    914 			vrele(vp);
    915 			continue;
    916 		}
    917 		if (vp->v_type == VREG && waitfor == MNT_LAZY)
    918 			error = ext2fs_update(vp, NULL, NULL, 0);
    919 		else
    920 			error = VOP_FSYNC(vp, cred,
    921 			    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0);
    922 		if (error)
    923 			allerror = error;
    924 		vput(vp);
    925 	}
    926 	vfs_vnode_iterator_destroy(marker);
    927 	/*
    928 	 * Force stale file system control information to be flushed.
    929 	 */
    930 	if (waitfor != MNT_LAZY) {
    931 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    932 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
    933 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
    934 			allerror = error;
    935 		VOP_UNLOCK(ump->um_devvp);
    936 	}
    937 	/*
    938 	 * Write back modified superblock.
    939 	 */
    940 	if (fs->e2fs_fmod != 0) {
    941 		fs->e2fs_fmod = 0;
    942 		fs->e2fs.e2fs_wtime = time_second;
    943 		if ((error = ext2fs_cgupdate(ump, waitfor)))
    944 			allerror = error;
    945 	}
    946 	return (allerror);
    947 }
    948 
    949 /*
    950  * Read an inode from disk and initialize this vnode / inode pair.
    951  * Caller assures no other thread will try to load this inode.
    952  */
    953 int
    954 ext2fs_loadvnode(struct mount *mp, struct vnode *vp,
    955     const void *key, size_t key_len, const void **new_key)
    956 {
    957 	ino_t ino;
    958 	struct m_ext2fs *fs;
    959 	struct inode *ip;
    960 	struct ufsmount *ump;
    961 	struct buf *bp;
    962 	dev_t dev;
    963 	int error;
    964 	void *cp;
    965 
    966 	KASSERT(key_len == sizeof(ino));
    967 	memcpy(&ino, key, key_len);
    968 	ump = VFSTOUFS(mp);
    969 	dev = ump->um_dev;
    970 	fs = ump->um_e2fs;
    971 
    972 	/* Read in the disk contents for the inode, copy into the inode. */
    973 	error = bread(ump->um_devvp, EXT2_FSBTODB(fs, ino_to_fsba(fs, ino)),
    974 	    (int)fs->e2fs_bsize, NOCRED, 0, &bp);
    975 	if (error)
    976 		return error;
    977 
    978 	/* Allocate and initialize inode. */
    979 	ip = pool_get(&ext2fs_inode_pool, PR_WAITOK);
    980 	memset(ip, 0, sizeof(struct inode));
    981 	vp->v_tag = VT_EXT2FS;
    982 	vp->v_op = ext2fs_vnodeop_p;
    983 	vp->v_vflag |= VV_LOCKSWORK;
    984 	vp->v_data = ip;
    985 	ip->i_vnode = vp;
    986 	ip->i_ump = ump;
    987 	ip->i_e2fs = fs;
    988 	ip->i_dev = dev;
    989 	ip->i_number = ino;
    990 	ip->i_e2fs_last_lblk = 0;
    991 	ip->i_e2fs_last_blk = 0;
    992 
    993 	/* Initialize genfs node. */
    994 	genfs_node_init(vp, &ext2fs_genfsops);
    995 
    996 	cp = (char *)bp->b_data + (ino_to_fsbo(fs, ino) * EXT2_DINODE_SIZE(fs));
    997 	ip->i_din.e2fs_din = pool_get(&ext2fs_dinode_pool, PR_WAITOK);
    998 	e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
    999 	ext2fs_set_inode_guid(ip);
   1000 	brelse(bp, 0);
   1001 
   1002 	/* If the inode was deleted, reset all fields */
   1003 	if (ip->i_e2fs_dtime != 0) {
   1004 		ip->i_e2fs_mode = 0;
   1005 		(void)ext2fs_setsize(ip, 0);
   1006 		(void)ext2fs_setnblock(ip, 0);
   1007 		memset(ip->i_e2fs_blocks, 0, sizeof(ip->i_e2fs_blocks));
   1008 	}
   1009 
   1010 	/* Initialize the vnode from the inode. */
   1011 	ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
   1012 
   1013 	/* Finish inode initialization. */
   1014 	ip->i_devvp = ump->um_devvp;
   1015 	vref(ip->i_devvp);
   1016 
   1017 	/*
   1018 	 * Set up a generation number for this inode if it does not
   1019 	 * already have one. This should only happen on old filesystems.
   1020 	 */
   1021 
   1022 	if (ip->i_e2fs_gen == 0) {
   1023 		if (++ext2gennumber < (u_long)time_second)
   1024 			ext2gennumber = time_second;
   1025 		ip->i_e2fs_gen = ext2gennumber;
   1026 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
   1027 			ip->i_flag |= IN_MODIFIED;
   1028 	}
   1029 	uvm_vnp_setsize(vp, ext2fs_size(ip));
   1030 	*new_key = &ip->i_number;
   1031 	return 0;
   1032 }
   1033 
   1034 /*
   1035  * File handle to vnode
   1036  *
   1037  * Have to be really careful about stale file handles:
   1038  * - check that the inode number is valid
   1039  * - call ext2fs_vget() to get the locked inode
   1040  * - check for an unallocated inode (i_mode == 0)
   1041  */
   1042 int
   1043 ext2fs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
   1044 {
   1045 	struct inode *ip;
   1046 	struct vnode *nvp;
   1047 	int error;
   1048 	struct ufid ufh;
   1049 	struct m_ext2fs *fs;
   1050 
   1051 	if (fhp->fid_len != sizeof(struct ufid))
   1052 		return EINVAL;
   1053 
   1054 	memcpy(&ufh, fhp, sizeof(struct ufid));
   1055 	fs = VFSTOUFS(mp)->um_e2fs;
   1056 	if ((ufh.ufid_ino < EXT2_FIRSTINO && ufh.ufid_ino != EXT2_ROOTINO) ||
   1057 		ufh.ufid_ino >= fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
   1058 		return (ESTALE);
   1059 
   1060 	if ((error = VFS_VGET(mp, ufh.ufid_ino, &nvp)) != 0) {
   1061 		*vpp = NULLVP;
   1062 		return (error);
   1063 	}
   1064 	ip = VTOI(nvp);
   1065 	if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0 ||
   1066 		ip->i_e2fs_gen != ufh.ufid_gen) {
   1067 		vput(nvp);
   1068 		*vpp = NULLVP;
   1069 		return (ESTALE);
   1070 	}
   1071 	*vpp = nvp;
   1072 	return (0);
   1073 }
   1074 
   1075 /*
   1076  * Vnode pointer to File handle
   1077  */
   1078 /* ARGSUSED */
   1079 int
   1080 ext2fs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
   1081 {
   1082 	struct inode *ip;
   1083 	struct ufid ufh;
   1084 
   1085 	if (*fh_size < sizeof(struct ufid)) {
   1086 		*fh_size = sizeof(struct ufid);
   1087 		return E2BIG;
   1088 	}
   1089 	*fh_size = sizeof(struct ufid);
   1090 
   1091 	ip = VTOI(vp);
   1092 	memset(&ufh, 0, sizeof(ufh));
   1093 	ufh.ufid_len = sizeof(struct ufid);
   1094 	ufh.ufid_ino = ip->i_number;
   1095 	ufh.ufid_gen = ip->i_e2fs_gen;
   1096 	memcpy(fhp, &ufh, sizeof(ufh));
   1097 	return (0);
   1098 }
   1099 
   1100 /*
   1101  * Write a superblock and associated information back to disk.
   1102  */
   1103 int
   1104 ext2fs_sbupdate(struct ufsmount *mp, int waitfor)
   1105 {
   1106 	struct m_ext2fs *fs = mp->um_e2fs;
   1107 	struct buf *bp;
   1108 	int error = 0;
   1109 
   1110 	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
   1111 	e2fs_sbsave(&fs->e2fs, (struct ext2fs*)bp->b_data);
   1112 	if (waitfor == MNT_WAIT)
   1113 		error = bwrite(bp);
   1114 	else
   1115 		bawrite(bp);
   1116 	return (error);
   1117 }
   1118 
   1119 int
   1120 ext2fs_cgupdate(struct ufsmount *mp, int waitfor)
   1121 {
   1122 	struct m_ext2fs *fs = mp->um_e2fs;
   1123 	struct buf *bp;
   1124 	int i, error = 0, allerror = 0;
   1125 
   1126 	allerror = ext2fs_sbupdate(mp, waitfor);
   1127 	for (i = 0; i < fs->e2fs_ngdb; i++) {
   1128 		bp = getblk(mp->um_devvp, EXT2_FSBTODB(fs,
   1129 		    fs->e2fs.e2fs_first_dblock +
   1130 		    1 /* superblock */ + i), fs->e2fs_bsize, 0, 0);
   1131 		e2fs_cgsave(&fs->e2fs_gd[
   1132 		    i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
   1133 		    (struct ext2_gd *)bp->b_data, fs->e2fs_bsize);
   1134 		if (waitfor == MNT_WAIT)
   1135 			error = bwrite(bp);
   1136 		else
   1137 			bawrite(bp);
   1138 	}
   1139 
   1140 	if (!allerror && error)
   1141 		allerror = error;
   1142 	return (allerror);
   1143 }
   1144 
   1145 static int
   1146 ext2fs_checksb(struct ext2fs *fs, int ronly)
   1147 {
   1148 	uint32_t u32;
   1149 
   1150 	if (fs2h16(fs->e2fs_magic) != E2FS_MAGIC) {
   1151 		return (EINVAL);		/* XXX needs translation */
   1152 	}
   1153 	if (fs2h32(fs->e2fs_rev) > E2FS_REV1) {
   1154 		printf("ext2fs: unsupported revision number: %x\n",
   1155 		    fs2h32(fs->e2fs_rev));
   1156 		return (EINVAL);		/* XXX needs translation */
   1157 	}
   1158 	if (fs2h32(fs->e2fs_log_bsize) > 2) { /* block size = 1024|2048|4096 */
   1159 		printf("ext2fs: bad block size: %d "
   1160 		    "(expected <= 2 for ext2 fs)\n",
   1161 		    fs2h32(fs->e2fs_log_bsize));
   1162 		return (EINVAL);	   /* XXX needs translation */
   1163 	}
   1164 	if (fs->e2fs_bpg == 0) {
   1165 		printf("ext2fs: zero blocks per group\n");
   1166 		return EINVAL;
   1167 	}
   1168 
   1169 	if (fs2h32(fs->e2fs_rev) > E2FS_REV0) {
   1170 		char buf[256];
   1171 		if (fs2h32(fs->e2fs_first_ino) != EXT2_FIRSTINO) {
   1172 			printf("ext2fs: unsupported first inode position\n");
   1173 			return (EINVAL);      /* XXX needs translation */
   1174 		}
   1175 		u32 = fs2h32(fs->e2fs_features_incompat) & ~EXT2F_INCOMPAT_SUPP;
   1176 		if (u32) {
   1177 			snprintb(buf, sizeof(buf), EXT2F_INCOMPAT_BITS, u32);
   1178 			printf("ext2fs: unsupported incompat features: %s\n",
   1179 			    buf);
   1180 			return EINVAL;	/* XXX needs translation */
   1181 		}
   1182 		u32 = fs2h32(fs->e2fs_features_rocompat) & ~EXT2F_ROCOMPAT_SUPP;
   1183 		if (!ronly && u32) {
   1184 			snprintb(buf, sizeof(buf), EXT2F_ROCOMPAT_BITS, u32);
   1185 			printf("ext2fs: unsupported ro-incompat features: %s\n",
   1186 			    buf);
   1187 			return EROFS;	/* XXX needs translation */
   1188 		}
   1189 	}
   1190 	return (0);
   1191 }
   1192