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