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