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