Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_vfsops.c revision 1.88
      1 /*	$NetBSD: ext2fs_vfsops.c,v 1.88 2005/08/23 08:05:13 christos 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.88 2005/08/23 08:05:13 christos 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 
     95 #include <miscfs/specfs/specdev.h>
     96 
     97 #include <ufs/ufs/quota.h>
     98 #include <ufs/ufs/ufsmount.h>
     99 #include <ufs/ufs/inode.h>
    100 #include <ufs/ufs/dir.h>
    101 #include <ufs/ufs/ufs_extern.h>
    102 
    103 #include <ufs/ext2fs/ext2fs.h>
    104 #include <ufs/ext2fs/ext2fs_dir.h>
    105 #include <ufs/ext2fs/ext2fs_extern.h>
    106 
    107 extern struct lock ufs_hashlock;
    108 
    109 int ext2fs_sbupdate __P((struct ufsmount *, int));
    110 static int ext2fs_checksb __P((struct ext2fs *, int));
    111 
    112 extern const struct vnodeopv_desc ext2fs_vnodeop_opv_desc;
    113 extern const struct vnodeopv_desc ext2fs_specop_opv_desc;
    114 extern const struct vnodeopv_desc ext2fs_fifoop_opv_desc;
    115 
    116 const struct vnodeopv_desc * const ext2fs_vnodeopv_descs[] = {
    117 	&ext2fs_vnodeop_opv_desc,
    118 	&ext2fs_specop_opv_desc,
    119 	&ext2fs_fifoop_opv_desc,
    120 	NULL,
    121 };
    122 
    123 struct vfsops ext2fs_vfsops = {
    124 	MOUNT_EXT2FS,
    125 	ext2fs_mount,
    126 	ufs_start,
    127 	ext2fs_unmount,
    128 	ufs_root,
    129 	ufs_quotactl,
    130 	ext2fs_statvfs,
    131 	ext2fs_sync,
    132 	ext2fs_vget,
    133 	ext2fs_fhtovp,
    134 	ext2fs_vptofh,
    135 	ext2fs_init,
    136 	ext2fs_reinit,
    137 	ext2fs_done,
    138 	NULL,
    139 	ext2fs_mountroot,
    140 	ufs_check_export,
    141 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    142 	vfs_stdextattrctl,
    143 	ext2fs_vnodeopv_descs,
    144 };
    145 VFS_ATTACH(ext2fs_vfsops);
    146 
    147 static const struct genfs_ops ext2fs_genfsops = {
    148 	.gop_size = genfs_size,
    149 	.gop_alloc = ext2fs_gop_alloc,
    150 	.gop_write = genfs_gop_write,
    151 	.gop_markupdate = ufs_gop_markupdate,
    152 };
    153 
    154 /*
    155  * XXX Same structure as FFS inodes?  Should we share a common pool?
    156  */
    157 POOL_INIT(ext2fs_inode_pool, sizeof(struct inode), 0, 0, 0, "ext2fsinopl",
    158     &pool_allocator_nointr);
    159 POOL_INIT(ext2fs_dinode_pool, sizeof(struct ext2fs_dinode), 0, 0, 0,
    160     "ext2dinopl", &pool_allocator_nointr);
    161 
    162 extern u_long ext2gennumber;
    163 
    164 void
    165 ext2fs_init()
    166 {
    167 #ifdef _LKM
    168 	pool_init(&ext2fs_inode_pool, sizeof(struct inode), 0, 0, 0,
    169 	    "ext2fsinopl", &pool_allocator_nointr);
    170 	pool_init(&ext2fs_dinode_pool, sizeof(struct ext2fs_dinode), 0, 0, 0,
    171 	    "ext2dinopl", &pool_allocator_nointr);
    172 #endif
    173 	ufs_init();
    174 }
    175 
    176 void
    177 ext2fs_reinit()
    178 {
    179 	ufs_reinit();
    180 }
    181 
    182 void
    183 ext2fs_done()
    184 {
    185 	ufs_done();
    186 #ifdef _LKM
    187 	pool_destroy(&ext2fs_inode_pool);
    188 	pool_destroy(&ext2fs_dinode_pool);
    189 #endif
    190 }
    191 
    192 /*
    193  * Called by main() when ext2fs is going to be mounted as root.
    194  *
    195  * Name is updated by mount(8) after booting.
    196  */
    197 #define ROOTNAME	"root_device"
    198 
    199 int
    200 ext2fs_mountroot()
    201 {
    202 	extern struct vnode *rootvp;
    203 	struct m_ext2fs *fs;
    204 	struct mount *mp;
    205 	struct proc *p = curproc;	/* XXX */
    206 	struct ufsmount *ump;
    207 	int error;
    208 
    209 	if (root_device->dv_class != DV_DISK)
    210 		return (ENODEV);
    211 
    212 	if ((error = vfs_rootmountalloc(MOUNT_EXT2FS, "root_device", &mp))) {
    213 		vrele(rootvp);
    214 		return (error);
    215 	}
    216 
    217 	if ((error = ext2fs_mountfs(rootvp, mp, p)) != 0) {
    218 		mp->mnt_op->vfs_refcount--;
    219 		vfs_unbusy(mp);
    220 		free(mp, M_MOUNT);
    221 		return (error);
    222 	}
    223 	simple_lock(&mountlist_slock);
    224 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    225 	simple_unlock(&mountlist_slock);
    226 	ump = VFSTOUFS(mp);
    227 	fs = ump->um_e2fs;
    228 	memset(fs->e2fs_fsmnt, 0, sizeof(fs->e2fs_fsmnt));
    229 	(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
    230 	    sizeof(fs->e2fs_fsmnt) - 1, 0);
    231 	if (fs->e2fs.e2fs_rev > E2FS_REV0) {
    232 		memset(fs->e2fs.e2fs_fsmnt, 0, sizeof(fs->e2fs.e2fs_fsmnt));
    233 		(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
    234 		    sizeof(fs->e2fs.e2fs_fsmnt) - 1, 0);
    235 	}
    236 	(void)ext2fs_statvfs(mp, &mp->mnt_stat, p);
    237 	vfs_unbusy(mp);
    238 	setrootfstime((time_t)fs->e2fs.e2fs_wtime);
    239 	return (0);
    240 }
    241 
    242 /*
    243  * VFS Operations.
    244  *
    245  * mount system call
    246  */
    247 int
    248 ext2fs_mount(mp, path, data, ndp, p)
    249 	struct mount *mp;
    250 	const char *path;
    251 	void * data;
    252 	struct nameidata *ndp;
    253 	struct proc *p;
    254 {
    255 	struct vnode *devvp;
    256 	struct ufs_args args;
    257 	struct ufsmount *ump = NULL;
    258 	struct m_ext2fs *fs;
    259 	size_t size;
    260 	int error, flags, update;
    261 	mode_t accessmode;
    262 
    263 	if (mp->mnt_flag & MNT_GETARGS) {
    264 		ump = VFSTOUFS(mp);
    265 		if (ump == NULL)
    266 			return EIO;
    267 		args.fspec = NULL;
    268 		vfs_showexport(mp, &args.export, &ump->um_export);
    269 		return copyout(&args, data, sizeof(args));
    270 	}
    271 	error = copyin(data, &args, sizeof (struct ufs_args));
    272 	if (error)
    273 		return (error);
    274 
    275 	update = mp->mnt_flag & MNT_UPDATE;
    276 
    277 	/* Check arguments */
    278 	if (args.fspec != NULL) {
    279 		/*
    280 		 * Look up the name and verify that it's sane.
    281 		 */
    282 		NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
    283 		if ((error = namei(ndp)) != 0)
    284 			return (error);
    285 		devvp = ndp->ni_vp;
    286 
    287 		if (!update) {
    288 			/*
    289 			 * Be sure this is a valid block device
    290 			 */
    291 			if (devvp->v_type != VBLK)
    292 				error = ENOTBLK;
    293 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
    294 				error = ENXIO;
    295 		} else {
    296 		        /*
    297 			 * Be sure we're still naming the same device
    298 			 * used for our initial mount
    299 			 */
    300 			ump = VFSTOUFS(mp);
    301 			if (devvp != ump->um_devvp)
    302 				error = EINVAL;
    303 		}
    304 	} else {
    305 		if (!update) {
    306 			/* New mounts must have a filename for the device */
    307 			return (EINVAL);
    308 		} else {
    309 			ump = VFSTOUFS(mp);
    310 			devvp = ump->um_devvp;
    311 			vref(devvp);
    312 		}
    313 	}
    314 
    315 	/*
    316 	 * If mount by non-root, then verify that user has necessary
    317 	 * permissions on the device.
    318 	 */
    319 	if (error == 0 && p->p_ucred->cr_uid != 0) {
    320 		accessmode = VREAD;
    321 		if (update ?
    322 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
    323 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    324 			accessmode |= VWRITE;
    325 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    326 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
    327 		VOP_UNLOCK(devvp, 0);
    328 	}
    329 
    330 	if (error) {
    331 		vrele(devvp);
    332 		return (error);
    333 	}
    334 
    335 	if (!update) {
    336 		int xflags;
    337 
    338 		/*
    339 		 * Disallow multiple mounts of the same device.
    340 		 * Disallow mounting of a device that is currently in use
    341 		 * (except for root, which might share swap device for
    342 		 * miniroot).
    343 		 */
    344 		error = vfs_mountedon(devvp);
    345 		if (error)
    346 			goto fail;
    347 		if (vcount(devvp) > 1 && devvp != rootvp) {
    348 			error = EBUSY;
    349 			goto fail;
    350 		}
    351 		if (mp->mnt_flag & MNT_RDONLY)
    352 			xflags = FREAD;
    353 		else
    354 			xflags = FREAD|FWRITE;
    355 		error = VOP_OPEN(devvp, xflags, FSCRED, p);
    356 		if (error)
    357 			goto fail;
    358 		error = ext2fs_mountfs(devvp, mp, p);
    359 		if (error) {
    360 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    361 			(void)VOP_CLOSE(devvp, xflags, NOCRED, p);
    362 			VOP_UNLOCK(devvp, 0);
    363 			goto fail;
    364 		}
    365 
    366 		ump = VFSTOUFS(mp);
    367 		fs = ump->um_e2fs;
    368 	} else {
    369 		/*
    370 		 * Update the mount.
    371 		 */
    372 
    373 		/*
    374 		 * The initial mount got a reference on this
    375 		 * device, so drop the one obtained via
    376 		 * namei(), above.
    377 		 */
    378 		vrele(devvp);
    379 
    380 		ump = VFSTOUFS(mp);
    381 		fs = ump->um_e2fs;
    382 		if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    383 			/*
    384 			 * Changing from r/w to r/o
    385 			 */
    386 			flags = WRITECLOSE;
    387 			if (mp->mnt_flag & MNT_FORCE)
    388 				flags |= FORCECLOSE;
    389 			error = ext2fs_flushfiles(mp, flags, p);
    390 			if (error == 0 &&
    391 			    ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
    392 			    (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
    393 				fs->e2fs.e2fs_state = E2FS_ISCLEAN;
    394 				(void) ext2fs_sbupdate(ump, MNT_WAIT);
    395 			}
    396 			if (error)
    397 				return (error);
    398 			fs->e2fs_ronly = 1;
    399 		}
    400 
    401 		if (mp->mnt_flag & MNT_RELOAD) {
    402 			error = ext2fs_reload(mp, ndp->ni_cnd.cn_cred, p);
    403 			if (error)
    404 				return (error);
    405 		}
    406 
    407 		if (fs->e2fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
    408 			/*
    409 			 * Changing from read-only to read/write
    410 			 */
    411 			fs->e2fs_ronly = 0;
    412 			if (fs->e2fs.e2fs_state == E2FS_ISCLEAN)
    413 				fs->e2fs.e2fs_state = 0;
    414 			else
    415 				fs->e2fs.e2fs_state = E2FS_ERRORS;
    416 			fs->e2fs_fmod = 1;
    417 		}
    418 		if (args.fspec == 0) {
    419 			/*
    420 			 * Process export requests.
    421 			 */
    422 			return (vfs_export(mp, &ump->um_export, &args.export));
    423 		}
    424 	}
    425 
    426 	error = set_statvfs_info(path, UIO_USERSPACE, args.fspec,
    427 	    UIO_USERSPACE, mp, p);
    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.tv_sec;
    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(mountp, cred, p)
    468 	struct mount *mountp;
    469 	struct ucred *cred;
    470 	struct proc *p;
    471 {
    472 	struct vnode *vp, *nvp, *devvp;
    473 	struct inode *ip;
    474 	struct buf *bp;
    475 	struct m_ext2fs *fs;
    476 	struct ext2fs *newfs;
    477 	struct partinfo dpart;
    478 	int i, size, error;
    479 	caddr_t cp;
    480 
    481 	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
    482 		return (EINVAL);
    483 	/*
    484 	 * Step 1: invalidate all cached meta-data.
    485 	 */
    486 	devvp = VFSTOUFS(mountp)->um_devvp;
    487 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    488 	error = vinvalbuf(devvp, 0, cred, p, 0, 0);
    489 	VOP_UNLOCK(devvp, 0);
    490 	if (error)
    491 		panic("ext2fs_reload: dirty1");
    492 	/*
    493 	 * Step 2: re-read superblock from disk.
    494 	 */
    495 	if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED, p) != 0)
    496 		size = DEV_BSIZE;
    497 	else
    498 		size = dpart.disklab->d_secsize;
    499 	error = bread(devvp, (daddr_t)(SBOFF / size), SBSIZE, NOCRED, &bp);
    500 	if (error) {
    501 		brelse(bp);
    502 		return (error);
    503 	}
    504 	newfs = (struct ext2fs *)bp->b_data;
    505 	error = ext2fs_checksb(newfs, (mountp->mnt_flag & MNT_RDONLY) != 0);
    506 	if (error) {
    507 		brelse(bp);
    508 		return (error);
    509 	}
    510 
    511 	fs = VFSTOUFS(mountp)->um_e2fs;
    512 	/*
    513 	 * copy in new superblock, and compute in-memory values
    514 	 */
    515 	e2fs_sbload(newfs, &fs->e2fs);
    516 	fs->e2fs_ncg =
    517 	    howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
    518 	    fs->e2fs.e2fs_bpg);
    519 	/* XXX assume hw bsize = 512 */
    520 	fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
    521 	fs->e2fs_bsize = 1024 << fs->e2fs.e2fs_log_bsize;
    522 	fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
    523 	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
    524 	fs->e2fs_bmask = ~fs->e2fs_qbmask;
    525 	fs->e2fs_ngdb = howmany(fs->e2fs_ncg,
    526 			fs->e2fs_bsize / sizeof(struct ext2_gd));
    527 	fs->e2fs_ipb = fs->e2fs_bsize / EXT2_DINODE_SIZE;
    528 	fs->e2fs_itpg = fs->e2fs.e2fs_ipg/fs->e2fs_ipb;
    529 
    530 	/*
    531 	 * Step 3: re-read summary information from disk.
    532 	 */
    533 
    534 	for (i=0; i < fs->e2fs_ngdb; i++) {
    535 		error = bread(devvp ,
    536 		    fsbtodb(fs, ((fs->e2fs_bsize>1024)? 0 : 1) + i + 1),
    537 		    fs->e2fs_bsize, NOCRED, &bp);
    538 		if (error) {
    539 			brelse(bp);
    540 			return (error);
    541 		}
    542 		e2fs_cgload((struct ext2_gd*)bp->b_data,
    543 		    &fs->e2fs_gd[i* fs->e2fs_bsize / sizeof(struct ext2_gd)],
    544 		    fs->e2fs_bsize);
    545 		brelse(bp);
    546 	}
    547 
    548 loop:
    549 	simple_lock(&mntvnode_slock);
    550 	for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
    551 		if (vp->v_mount != mountp) {
    552 			simple_unlock(&mntvnode_slock);
    553 			goto loop;
    554 		}
    555 		nvp = vp->v_mntvnodes.le_next;
    556 		/*
    557 		 * Step 4: invalidate all inactive vnodes.
    558 		 */
    559 		if (vrecycle(vp, &mntvnode_slock, p))
    560 			goto loop;
    561 		/*
    562 		 * Step 5: invalidate all cached file data.
    563 		 */
    564 		simple_lock(&vp->v_interlock);
    565 		simple_unlock(&mntvnode_slock);
    566 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    567 			goto loop;
    568 		if (vinvalbuf(vp, 0, cred, p, 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 = (caddr_t)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(devvp, mp, p)
    596 	struct vnode *devvp;
    597 	struct mount *mp;
    598 	struct proc *p;
    599 {
    600 	struct ufsmount *ump;
    601 	struct buf *bp;
    602 	struct ext2fs *fs;
    603 	struct m_ext2fs *m_fs;
    604 	dev_t dev;
    605 	struct partinfo dpart;
    606 	int error, i, size, ronly;
    607 	struct ucred *cred;
    608 
    609 	dev = devvp->v_rdev;
    610 	cred = p ? p->p_ucred : 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, p, 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, p) != 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_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 	if (bp)
    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(mp, mntflags, p)
    729 	struct mount *mp;
    730 	int mntflags;
    731 	struct proc *p;
    732 {
    733 	struct ufsmount *ump;
    734 	struct m_ext2fs *fs;
    735 	int error, flags;
    736 
    737 	flags = 0;
    738 	if (mntflags & MNT_FORCE)
    739 		flags |= FORCECLOSE;
    740 	if ((error = ext2fs_flushfiles(mp, flags, p)) != 0)
    741 		return (error);
    742 	ump = VFSTOUFS(mp);
    743 	fs = ump->um_e2fs;
    744 	if (fs->e2fs_ronly == 0 &&
    745 		ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
    746 		(fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
    747 		fs->e2fs.e2fs_state = E2FS_ISCLEAN;
    748 		(void) ext2fs_sbupdate(ump, MNT_WAIT);
    749 	}
    750 	if (ump->um_devvp->v_type != VBAD)
    751 		ump->um_devvp->v_specmountpoint = NULL;
    752 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    753 	error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
    754 		NOCRED, p);
    755 	vput(ump->um_devvp);
    756 	free(fs->e2fs_gd, M_UFSMNT);
    757 	free(fs, M_UFSMNT);
    758 	free(ump, M_UFSMNT);
    759 	mp->mnt_data = NULL;
    760 	mp->mnt_flag &= ~MNT_LOCAL;
    761 	return (error);
    762 }
    763 
    764 /*
    765  * Flush out all the files in a filesystem.
    766  */
    767 int
    768 ext2fs_flushfiles(mp, flags, p)
    769 	struct mount *mp;
    770 	int flags;
    771 	struct proc *p;
    772 {
    773 	extern int doforce;
    774 	int error;
    775 
    776 	if (!doforce)
    777 		flags &= ~FORCECLOSE;
    778 	error = vflush(mp, NULLVP, flags);
    779 	return (error);
    780 }
    781 
    782 /*
    783  * Get file system statistics.
    784  */
    785 int
    786 ext2fs_statvfs(mp, sbp, p)
    787 	struct mount *mp;
    788 	struct statvfs *sbp;
    789 	struct proc *p;
    790 {
    791 	struct ufsmount *ump;
    792 	struct m_ext2fs *fs;
    793 	u_int32_t overhead, overhead_per_group;
    794 	int i, ngroups;
    795 
    796 	ump = VFSTOUFS(mp);
    797 	fs = ump->um_e2fs;
    798 	if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
    799 		panic("ext2fs_statvfs");
    800 
    801 	/*
    802 	 * Compute the overhead (FS structures)
    803 	 */
    804 	overhead_per_group = 1 /* block bitmap */ +
    805 				 1 /* inode bitmap */ +
    806 				 fs->e2fs_itpg;
    807 	overhead = fs->e2fs.e2fs_first_dblock +
    808 		   fs->e2fs_ncg * overhead_per_group;
    809 	if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
    810 	    fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
    811 		for (i = 0, ngroups = 0; i < fs->e2fs_ncg; i++) {
    812 			if (cg_has_sb(i))
    813 				ngroups++;
    814 		}
    815 	} else {
    816 		ngroups = fs->e2fs_ncg;
    817 	}
    818 	overhead += ngroups * (1 + fs->e2fs_ngdb);
    819 
    820 	sbp->f_bsize = fs->e2fs_bsize;
    821 	sbp->f_frsize = 1024 << fs->e2fs.e2fs_fsize;
    822 	sbp->f_iosize = fs->e2fs_bsize;
    823 	sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
    824 	sbp->f_bfree = fs->e2fs.e2fs_fbcount;
    825 	sbp->f_bresvd = fs->e2fs.e2fs_rbcount;
    826 	if (sbp->f_bfree > sbp->f_bresvd)
    827 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
    828 	else
    829 		sbp->f_bavail = 0;
    830 	sbp->f_files =  fs->e2fs.e2fs_icount;
    831 	sbp->f_ffree = fs->e2fs.e2fs_ficount;
    832 	sbp->f_favail = fs->e2fs.e2fs_ficount;
    833 	sbp->f_fresvd = 0;
    834 	copy_statvfs_info(sbp, mp);
    835 	return (0);
    836 }
    837 
    838 /*
    839  * Go through the disk queues to initiate sandbagged IO;
    840  * go through the inodes to write those that have been modified;
    841  * initiate the writing of the super block if it has been modified.
    842  *
    843  * Note: we are always called with the filesystem marked `MPBUSY'.
    844  */
    845 int
    846 ext2fs_sync(mp, waitfor, cred, p)
    847 	struct mount *mp;
    848 	int waitfor;
    849 	struct ucred *cred;
    850 	struct proc *p;
    851 {
    852 	struct vnode *vp, *nvp;
    853 	struct inode *ip;
    854 	struct ufsmount *ump = VFSTOUFS(mp);
    855 	struct m_ext2fs *fs;
    856 	int error, allerror = 0;
    857 
    858 	fs = ump->um_e2fs;
    859 	if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) {	/* XXX */
    860 		printf("fs = %s\n", fs->e2fs_fsmnt);
    861 		panic("update: rofs mod");
    862 	}
    863 	/*
    864 	 * Write back each (modified) inode.
    865 	 */
    866 	simple_lock(&mntvnode_slock);
    867 loop:
    868 	for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
    869 		/*
    870 		 * If the vnode that we are about to sync is no longer
    871 		 * associated with this mount point, start over.
    872 		 */
    873 		if (vp->v_mount != mp)
    874 			goto loop;
    875 		simple_lock(&vp->v_interlock);
    876 		nvp = LIST_NEXT(vp, v_mntvnodes);
    877 		ip = VTOI(vp);
    878 		if (vp->v_type == VNON ||
    879 		    ((ip->i_flag &
    880 		      (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
    881 		     LIST_EMPTY(&vp->v_dirtyblkhd) &&
    882 		     vp->v_uobj.uo_npages == 0))
    883 		{
    884 			simple_unlock(&vp->v_interlock);
    885 			continue;
    886 		}
    887 		simple_unlock(&mntvnode_slock);
    888 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    889 		if (error) {
    890 			simple_lock(&mntvnode_slock);
    891 			if (error == ENOENT)
    892 				goto loop;
    893 			continue;
    894 		}
    895 		if (vp->v_type == VREG && waitfor == MNT_LAZY)
    896 			error = VOP_UPDATE(vp, NULL, NULL, 0);
    897 		else
    898 			error = VOP_FSYNC(vp, cred,
    899 			    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p);
    900 		if (error)
    901 			allerror = error;
    902 		vput(vp);
    903 		simple_lock(&mntvnode_slock);
    904 	}
    905 	simple_unlock(&mntvnode_slock);
    906 	/*
    907 	 * Force stale file system control information to be flushed.
    908 	 */
    909 	if (waitfor != MNT_LAZY) {
    910 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    911 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
    912 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
    913 			allerror = error;
    914 		VOP_UNLOCK(ump->um_devvp, 0);
    915 	}
    916 	/*
    917 	 * Write back modified superblock.
    918 	 */
    919 	if (fs->e2fs_fmod != 0) {
    920 		fs->e2fs_fmod = 0;
    921 		fs->e2fs.e2fs_wtime = time.tv_sec;
    922 		if ((error = ext2fs_cgupdate(ump, waitfor)))
    923 			allerror = error;
    924 	}
    925 	return (allerror);
    926 }
    927 
    928 /*
    929  * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
    930  * in from disk.  If it is in core, wait for the lock bit to clear, then
    931  * return the inode locked.  Detection and handling of mount points must be
    932  * done by the calling routine.
    933  */
    934 int
    935 ext2fs_vget(mp, ino, vpp)
    936 	struct mount *mp;
    937 	ino_t ino;
    938 	struct vnode **vpp;
    939 {
    940 	struct m_ext2fs *fs;
    941 	struct inode *ip;
    942 	struct ufsmount *ump;
    943 	struct buf *bp;
    944 	struct vnode *vp;
    945 	dev_t dev;
    946 	int error;
    947 	caddr_t cp;
    948 
    949 	ump = VFSTOUFS(mp);
    950 	dev = ump->um_dev;
    951 
    952 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
    953 		return (0);
    954 
    955 	/* Allocate a new vnode/inode. */
    956 	if ((error = getnewvnode(VT_EXT2FS, mp, ext2fs_vnodeop_p, &vp)) != 0) {
    957 		*vpp = NULL;
    958 		return (error);
    959 	}
    960 
    961 	do {
    962 		if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
    963 			ungetnewvnode(vp);
    964 			return (0);
    965 		}
    966 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
    967 
    968 	vp->v_flag |= VLOCKSWORK;
    969 
    970 	ip = pool_get(&ext2fs_inode_pool, PR_WAITOK);
    971 	memset(ip, 0, sizeof(struct inode));
    972 	vp->v_data = ip;
    973 	ip->i_vnode = vp;
    974 	ip->i_ump = ump;
    975 	ip->i_e2fs = fs = ump->um_e2fs;
    976 	ip->i_dev = dev;
    977 	ip->i_number = ino;
    978 	ip->i_e2fs_last_lblk = 0;
    979 	ip->i_e2fs_last_blk = 0;
    980 
    981 	/*
    982 	 * Put it onto its hash chain and lock it so that other requests for
    983 	 * this inode will block if they arrive while we are sleeping waiting
    984 	 * for old data structures to be purged or for the contents of the
    985 	 * disk portion of this inode to be read.
    986 	 */
    987 
    988 	ufs_ihashins(ip);
    989 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    990 
    991 	/* Read in the disk contents for the inode, copy into the inode. */
    992 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
    993 			  (int)fs->e2fs_bsize, NOCRED, &bp);
    994 	if (error) {
    995 
    996 		/*
    997 		 * The inode does not contain anything useful, so it would
    998 		 * be misleading to leave it on its hash chain. With mode
    999 		 * still zero, it will be unlinked and returned to the free
   1000 		 * list by vput().
   1001 		 */
   1002 
   1003 		vput(vp);
   1004 		brelse(bp);
   1005 		*vpp = NULL;
   1006 		return (error);
   1007 	}
   1008 	cp = (caddr_t)bp->b_data +
   1009 	    (ino_to_fsbo(fs, ino) * EXT2_DINODE_SIZE);
   1010 	ip->i_din.e2fs_din = pool_get(&ext2fs_dinode_pool, PR_WAITOK);
   1011 	e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
   1012 	brelse(bp);
   1013 
   1014 	/* If the inode was deleted, reset all fields */
   1015 	if (ip->i_e2fs_dtime != 0) {
   1016 		ip->i_e2fs_mode = ip->i_e2fs_nblock = 0;
   1017 		(void)ext2fs_setsize(ip, 0);
   1018 		memset(ip->i_e2fs_blocks, 0, sizeof(ip->i_e2fs_blocks));
   1019 	}
   1020 
   1021 	/*
   1022 	 * Initialize the vnode from the inode, check for aliases.
   1023 	 * Note that the underlying vnode may have changed.
   1024 	 */
   1025 
   1026 	error = ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
   1027 	if (error) {
   1028 		vput(vp);
   1029 		*vpp = NULL;
   1030 		return (error);
   1031 	}
   1032 	/*
   1033 	 * Finish inode initialization now that aliasing has been resolved.
   1034 	 */
   1035 
   1036 	genfs_node_init(vp, &ext2fs_genfsops);
   1037 	ip->i_devvp = ump->um_devvp;
   1038 	VREF(ip->i_devvp);
   1039 
   1040 	/*
   1041 	 * Set up a generation number for this inode if it does not
   1042 	 * already have one. This should only happen on old filesystems.
   1043 	 */
   1044 
   1045 	if (ip->i_e2fs_gen == 0) {
   1046 		if (++ext2gennumber < (u_long)time.tv_sec)
   1047 			ext2gennumber = time.tv_sec;
   1048 		ip->i_e2fs_gen = ext2gennumber;
   1049 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
   1050 			ip->i_flag |= IN_MODIFIED;
   1051 	}
   1052 	vp->v_size = ext2fs_size(ip);
   1053 	*vpp = vp;
   1054 	return (0);
   1055 }
   1056 
   1057 /*
   1058  * File handle to vnode
   1059  *
   1060  * Have to be really careful about stale file handles:
   1061  * - check that the inode number is valid
   1062  * - call ext2fs_vget() to get the locked inode
   1063  * - check for an unallocated inode (i_mode == 0)
   1064  */
   1065 int
   1066 ext2fs_fhtovp(mp, fhp, vpp)
   1067 	struct mount *mp;
   1068 	struct fid *fhp;
   1069 	struct vnode **vpp;
   1070 {
   1071 	struct inode *ip;
   1072 	struct vnode *nvp;
   1073 	int error;
   1074 	struct ufid *ufhp;
   1075 	struct m_ext2fs *fs;
   1076 
   1077 	ufhp = (struct ufid *)fhp;
   1078 	fs = VFSTOUFS(mp)->um_e2fs;
   1079 	if ((ufhp->ufid_ino < EXT2_FIRSTINO && ufhp->ufid_ino != EXT2_ROOTINO) ||
   1080 		ufhp->ufid_ino >= fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
   1081 		return (ESTALE);
   1082 
   1083 	if ((error = VFS_VGET(mp, ufhp->ufid_ino, &nvp)) != 0) {
   1084 		*vpp = NULLVP;
   1085 		return (error);
   1086 	}
   1087 	ip = VTOI(nvp);
   1088 	if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0 ||
   1089 		ip->i_e2fs_gen != ufhp->ufid_gen) {
   1090 		vput(nvp);
   1091 		*vpp = NULLVP;
   1092 		return (ESTALE);
   1093 	}
   1094 	*vpp = nvp;
   1095 	return (0);
   1096 }
   1097 
   1098 /*
   1099  * Vnode pointer to File handle
   1100  */
   1101 /* ARGSUSED */
   1102 int
   1103 ext2fs_vptofh(vp, fhp)
   1104 	struct vnode *vp;
   1105 	struct fid *fhp;
   1106 {
   1107 	struct inode *ip;
   1108 	struct ufid *ufhp;
   1109 
   1110 	ip = VTOI(vp);
   1111 	ufhp = (struct ufid *)fhp;
   1112 	ufhp->ufid_len = sizeof(struct ufid);
   1113 	ufhp->ufid_ino = ip->i_number;
   1114 	ufhp->ufid_gen = ip->i_e2fs_gen;
   1115 	return (0);
   1116 }
   1117 
   1118 SYSCTL_SETUP(sysctl_vfs_ext2fs_setup, "sysctl vfs.ext2fs subtree setup")
   1119 {
   1120 
   1121 	sysctl_createv(clog, 0, NULL, NULL,
   1122 		       CTLFLAG_PERMANENT,
   1123 		       CTLTYPE_NODE, "vfs", NULL,
   1124 		       NULL, 0, NULL, 0,
   1125 		       CTL_VFS, CTL_EOL);
   1126 	sysctl_createv(clog, 0, NULL, NULL,
   1127 		       CTLFLAG_PERMANENT,
   1128 		       CTLTYPE_NODE, "ext2fs",
   1129 		       SYSCTL_DESCR("Linux EXT2FS file system"),
   1130 		       NULL, 0, NULL, 0,
   1131 		       CTL_VFS, 17, CTL_EOL);
   1132 	/*
   1133 	 * XXX the "17" above could be dynamic, thereby eliminating
   1134 	 * one more instance of the "number to vfs" mapping problem,
   1135 	 * but "17" is the order as taken from sys/mount.h
   1136 	 */
   1137 }
   1138 
   1139 /*
   1140  * Write a superblock and associated information back to disk.
   1141  */
   1142 int
   1143 ext2fs_sbupdate(mp, waitfor)
   1144 	struct ufsmount *mp;
   1145 	int waitfor;
   1146 {
   1147 	struct m_ext2fs *fs = mp->um_e2fs;
   1148 	struct buf *bp;
   1149 	int error = 0;
   1150 
   1151 	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
   1152 	e2fs_sbsave(&fs->e2fs, (struct ext2fs*)bp->b_data);
   1153 	if (waitfor == MNT_WAIT)
   1154 		error = bwrite(bp);
   1155 	else
   1156 		bawrite(bp);
   1157 	return (error);
   1158 }
   1159 
   1160 int
   1161 ext2fs_cgupdate(mp, waitfor)
   1162 	struct ufsmount *mp;
   1163 	int waitfor;
   1164 {
   1165 	struct m_ext2fs *fs = mp->um_e2fs;
   1166 	struct buf *bp;
   1167 	int i, error = 0, allerror = 0;
   1168 
   1169 	allerror = ext2fs_sbupdate(mp, waitfor);
   1170 	for (i = 0; i < fs->e2fs_ngdb; i++) {
   1171 		bp = getblk(mp->um_devvp, fsbtodb(fs, ((fs->e2fs_bsize>1024)?0:1)+i+1),
   1172 			fs->e2fs_bsize, 0, 0);
   1173 		e2fs_cgsave(&fs->e2fs_gd[i* fs->e2fs_bsize / sizeof(struct ext2_gd)],
   1174 				(struct ext2_gd*)bp->b_data, fs->e2fs_bsize);
   1175 		if (waitfor == MNT_WAIT)
   1176 			error = bwrite(bp);
   1177 		else
   1178 			bawrite(bp);
   1179 	}
   1180 
   1181 	if (!allerror && error)
   1182 		allerror = error;
   1183 	return (allerror);
   1184 }
   1185 
   1186 static int
   1187 ext2fs_checksb(fs, ronly)
   1188 	struct ext2fs *fs;
   1189 	int ronly;
   1190 {
   1191 	if (fs2h16(fs->e2fs_magic) != E2FS_MAGIC) {
   1192 		return (EINVAL);		/* XXX needs translation */
   1193 	}
   1194 	if (fs2h32(fs->e2fs_rev) > E2FS_REV1) {
   1195 #ifdef DIAGNOSTIC
   1196 		printf("Ext2 fs: unsupported revision number: %x\n",
   1197 					fs2h32(fs->e2fs_rev));
   1198 #endif
   1199 		return (EINVAL);		/* XXX needs translation */
   1200 	}
   1201 	if (fs2h32(fs->e2fs_log_bsize) > 2) { /* block size = 1024|2048|4096 */
   1202 #ifdef DIAGNOSTIC
   1203 		printf("Ext2 fs: bad block size: %d (expected <=2 for ext2 fs)\n",
   1204 			fs2h32(fs->e2fs_log_bsize));
   1205 #endif
   1206 		return (EINVAL);	   /* XXX needs translation */
   1207 	}
   1208 	if (fs2h32(fs->e2fs_rev) > E2FS_REV0) {
   1209 		if (fs2h32(fs->e2fs_first_ino) != EXT2_FIRSTINO ||
   1210 		    fs2h16(fs->e2fs_inode_size) != EXT2_DINODE_SIZE) {
   1211 			printf("Ext2 fs: unsupported inode size\n");
   1212 			return (EINVAL);      /* XXX needs translation */
   1213 		}
   1214 		if (fs2h32(fs->e2fs_features_incompat) &
   1215 		    ~EXT2F_INCOMPAT_SUPP) {
   1216 			printf("Ext2 fs: unsupported optional feature\n");
   1217 			return (EINVAL);      /* XXX needs translation */
   1218 		}
   1219 		if (!ronly && fs2h32(fs->e2fs_features_rocompat) &
   1220 		    ~EXT2F_ROCOMPAT_SUPP) {
   1221 			return (EROFS);      /* XXX needs translation */
   1222 		}
   1223 	}
   1224 	return (0);
   1225 }
   1226