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