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