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