Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_vfsops.c revision 1.73
      1 /*	$NetBSD: ext2fs_vfsops.c,v 1.73 2004/07/05 07:28:45 pk 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.73 2004/07/05 07:28:45 pk 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_maxsymlinklen = EXT2_MAXSYMLINKLEN;
    658 	mp->mnt_flag |= MNT_LOCAL;
    659 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
    660 	mp->mnt_fs_bshift = m_fs->e2fs_bshift;
    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 	devvp->v_specmountpoint = mp;
    670 	return (0);
    671 
    672 out:
    673 	if (bp)
    674 		brelse(bp);
    675 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    676 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
    677 	VOP_UNLOCK(devvp, 0);
    678 	if (ump) {
    679 		free(ump->um_e2fs, M_UFSMNT);
    680 		free(ump, M_UFSMNT);
    681 		mp->mnt_data = NULL;
    682 	}
    683 	return (error);
    684 }
    685 
    686 /*
    687  * unmount system call
    688  */
    689 int
    690 ext2fs_unmount(mp, mntflags, p)
    691 	struct mount *mp;
    692 	int mntflags;
    693 	struct proc *p;
    694 {
    695 	struct ufsmount *ump;
    696 	struct m_ext2fs *fs;
    697 	int error, flags;
    698 
    699 	flags = 0;
    700 	if (mntflags & MNT_FORCE)
    701 		flags |= FORCECLOSE;
    702 	if ((error = ext2fs_flushfiles(mp, flags, p)) != 0)
    703 		return (error);
    704 	ump = VFSTOUFS(mp);
    705 	fs = ump->um_e2fs;
    706 	if (fs->e2fs_ronly == 0 &&
    707 		ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
    708 		(fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
    709 		fs->e2fs.e2fs_state = E2FS_ISCLEAN;
    710 		(void) ext2fs_sbupdate(ump, MNT_WAIT);
    711 	}
    712 	if (ump->um_devvp->v_type != VBAD)
    713 		ump->um_devvp->v_specmountpoint = NULL;
    714 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    715 	error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
    716 		NOCRED, p);
    717 	vput(ump->um_devvp);
    718 	free(fs->e2fs_gd, M_UFSMNT);
    719 	free(fs, M_UFSMNT);
    720 	free(ump, M_UFSMNT);
    721 	mp->mnt_data = NULL;
    722 	mp->mnt_flag &= ~MNT_LOCAL;
    723 	return (error);
    724 }
    725 
    726 /*
    727  * Flush out all the files in a filesystem.
    728  */
    729 int
    730 ext2fs_flushfiles(mp, flags, p)
    731 	struct mount *mp;
    732 	int flags;
    733 	struct proc *p;
    734 {
    735 	extern int doforce;
    736 	int error;
    737 
    738 	if (!doforce)
    739 		flags &= ~FORCECLOSE;
    740 	error = vflush(mp, NULLVP, flags);
    741 	return (error);
    742 }
    743 
    744 /*
    745  * Get file system statistics.
    746  */
    747 int
    748 ext2fs_statvfs(mp, sbp, p)
    749 	struct mount *mp;
    750 	struct statvfs *sbp;
    751 	struct proc *p;
    752 {
    753 	struct ufsmount *ump;
    754 	struct m_ext2fs *fs;
    755 	u_int32_t overhead, overhead_per_group;
    756 	int i, ngroups;
    757 
    758 	ump = VFSTOUFS(mp);
    759 	fs = ump->um_e2fs;
    760 	if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
    761 		panic("ext2fs_statvfs");
    762 
    763 	/*
    764 	 * Compute the overhead (FS structures)
    765 	 */
    766 	overhead_per_group = 1 /* block bitmap */ +
    767 				 1 /* inode bitmap */ +
    768 				 fs->e2fs_itpg;
    769 	overhead = fs->e2fs.e2fs_first_dblock +
    770 		   fs->e2fs_ncg * overhead_per_group;
    771 	if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
    772 	    fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
    773 		for (i = 0, ngroups = 0; i < fs->e2fs_ncg; i++) {
    774 			if (cg_has_sb(i))
    775 				ngroups++;
    776 		}
    777 	} else {
    778 		ngroups = fs->e2fs_ncg;
    779 	}
    780 	overhead += ngroups * (1 + fs->e2fs_ngdb);
    781 
    782 	sbp->f_bsize = fs->e2fs_bsize;
    783 	sbp->f_frsize = fs->e2fs.e2fs_fsize;
    784 	sbp->f_iosize = fs->e2fs_bsize;
    785 	sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
    786 	sbp->f_bfree = fs->e2fs.e2fs_fbcount;
    787 	sbp->f_bresvd = fs->e2fs.e2fs_rbcount;
    788 	if (sbp->f_bfree > sbp->f_bresvd)
    789 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
    790 	else
    791 		sbp->f_bavail = 0;
    792 	sbp->f_files =  fs->e2fs.e2fs_icount;
    793 	sbp->f_ffree = fs->e2fs.e2fs_ficount;
    794 	sbp->f_favail = fs->e2fs.e2fs_ficount;
    795 	sbp->f_fresvd = 0;
    796 	copy_statvfs_info(sbp, mp);
    797 	return (0);
    798 }
    799 
    800 /*
    801  * Go through the disk queues to initiate sandbagged IO;
    802  * go through the inodes to write those that have been modified;
    803  * initiate the writing of the super block if it has been modified.
    804  *
    805  * Note: we are always called with the filesystem marked `MPBUSY'.
    806  */
    807 int
    808 ext2fs_sync(mp, waitfor, cred, p)
    809 	struct mount *mp;
    810 	int waitfor;
    811 	struct ucred *cred;
    812 	struct proc *p;
    813 {
    814 	struct vnode *vp, *nvp;
    815 	struct inode *ip;
    816 	struct ufsmount *ump = VFSTOUFS(mp);
    817 	struct m_ext2fs *fs;
    818 	int error, allerror = 0;
    819 
    820 	fs = ump->um_e2fs;
    821 	if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) {	/* XXX */
    822 		printf("fs = %s\n", fs->e2fs_fsmnt);
    823 		panic("update: rofs mod");
    824 	}
    825 	/*
    826 	 * Write back each (modified) inode.
    827 	 */
    828 	simple_lock(&mntvnode_slock);
    829 loop:
    830 	for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
    831 		/*
    832 		 * If the vnode that we are about to sync is no longer
    833 		 * associated with this mount point, start over.
    834 		 */
    835 		if (vp->v_mount != mp)
    836 			goto loop;
    837 		simple_lock(&vp->v_interlock);
    838 		nvp = LIST_NEXT(vp, v_mntvnodes);
    839 		ip = VTOI(vp);
    840 		if (waitfor == MNT_LAZY || vp->v_type == VNON ||
    841 		    ((ip->i_flag &
    842 		      (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFIED | IN_ACCESSED)) == 0 &&
    843 		     LIST_EMPTY(&vp->v_dirtyblkhd) &&
    844 		     vp->v_uobj.uo_npages == 0))
    845 		{
    846 			simple_unlock(&vp->v_interlock);
    847 			continue;
    848 		}
    849 		simple_unlock(&mntvnode_slock);
    850 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    851 		if (error) {
    852 			simple_lock(&mntvnode_slock);
    853 			if (error == ENOENT)
    854 				goto loop;
    855 			continue;
    856 		}
    857 		if ((error = VOP_FSYNC(vp, cred,
    858 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
    859 			allerror = error;
    860 		vput(vp);
    861 		simple_lock(&mntvnode_slock);
    862 	}
    863 	simple_unlock(&mntvnode_slock);
    864 	/*
    865 	 * Force stale file system control information to be flushed.
    866 	 */
    867 	if (waitfor != MNT_LAZY) {
    868 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    869 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
    870 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
    871 			allerror = error;
    872 		VOP_UNLOCK(ump->um_devvp, 0);
    873 	}
    874 	/*
    875 	 * Write back modified superblock.
    876 	 */
    877 	if (fs->e2fs_fmod != 0) {
    878 		fs->e2fs_fmod = 0;
    879 		fs->e2fs.e2fs_wtime = time.tv_sec;
    880 		if ((error = ext2fs_cgupdate(ump, waitfor)))
    881 			allerror = error;
    882 	}
    883 	return (allerror);
    884 }
    885 
    886 /*
    887  * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
    888  * in from disk.  If it is in core, wait for the lock bit to clear, then
    889  * return the inode locked.  Detection and handling of mount points must be
    890  * done by the calling routine.
    891  */
    892 int
    893 ext2fs_vget(mp, ino, vpp)
    894 	struct mount *mp;
    895 	ino_t ino;
    896 	struct vnode **vpp;
    897 {
    898 	struct m_ext2fs *fs;
    899 	struct inode *ip;
    900 	struct ufsmount *ump;
    901 	struct buf *bp;
    902 	struct vnode *vp;
    903 	dev_t dev;
    904 	int error;
    905 	caddr_t cp;
    906 
    907 	ump = VFSTOUFS(mp);
    908 	dev = ump->um_dev;
    909 
    910 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
    911 		return (0);
    912 
    913 	/* Allocate a new vnode/inode. */
    914 	if ((error = getnewvnode(VT_EXT2FS, mp, ext2fs_vnodeop_p, &vp)) != 0) {
    915 		*vpp = NULL;
    916 		return (error);
    917 	}
    918 
    919 	do {
    920 		if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
    921 			ungetnewvnode(vp);
    922 			return (0);
    923 		}
    924 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
    925 
    926 	ip = pool_get(&ext2fs_inode_pool, PR_WAITOK);
    927 	memset(ip, 0, sizeof(struct inode));
    928 	vp->v_data = ip;
    929 	ip->i_vnode = vp;
    930 	ip->i_ump = ump;
    931 	ip->i_e2fs = fs = ump->um_e2fs;
    932 	ip->i_dev = dev;
    933 	ip->i_number = ino;
    934 	ip->i_e2fs_last_lblk = 0;
    935 	ip->i_e2fs_last_blk = 0;
    936 
    937 	/*
    938 	 * Put it onto its hash chain and lock it so that other requests for
    939 	 * this inode will block if they arrive while we are sleeping waiting
    940 	 * for old data structures to be purged or for the contents of the
    941 	 * disk portion of this inode to be read.
    942 	 */
    943 
    944 	ufs_ihashins(ip);
    945 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    946 
    947 	/* Read in the disk contents for the inode, copy into the inode. */
    948 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
    949 			  (int)fs->e2fs_bsize, NOCRED, &bp);
    950 	if (error) {
    951 
    952 		/*
    953 		 * The inode does not contain anything useful, so it would
    954 		 * be misleading to leave it on its hash chain. With mode
    955 		 * still zero, it will be unlinked and returned to the free
    956 		 * list by vput().
    957 		 */
    958 
    959 		vput(vp);
    960 		brelse(bp);
    961 		*vpp = NULL;
    962 		return (error);
    963 	}
    964 	cp = (caddr_t)bp->b_data +
    965 	    (ino_to_fsbo(fs, ino) * EXT2_DINODE_SIZE);
    966 	ip->i_din.e2fs_din = pool_get(&ext2fs_dinode_pool, PR_WAITOK);
    967 	e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
    968 	brelse(bp);
    969 
    970 	/* If the inode was deleted, reset all fields */
    971 	if (ip->i_e2fs_dtime != 0) {
    972 		ip->i_e2fs_mode = ip->i_e2fs_size = ip->i_e2fs_nblock = 0;
    973 		memset(ip->i_e2fs_blocks, 0, sizeof(ip->i_e2fs_blocks));
    974 	}
    975 
    976 	/*
    977 	 * Initialize the vnode from the inode, check for aliases.
    978 	 * Note that the underlying vnode may have changed.
    979 	 */
    980 
    981 	error = ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
    982 	if (error) {
    983 		vput(vp);
    984 		*vpp = NULL;
    985 		return (error);
    986 	}
    987 	/*
    988 	 * Finish inode initialization now that aliasing has been resolved.
    989 	 */
    990 
    991 	genfs_node_init(vp, &ext2fs_genfsops);
    992 	ip->i_devvp = ump->um_devvp;
    993 	VREF(ip->i_devvp);
    994 
    995 	/*
    996 	 * Set up a generation number for this inode if it does not
    997 	 * already have one. This should only happen on old filesystems.
    998 	 */
    999 
   1000 	if (ip->i_e2fs_gen == 0) {
   1001 		if (++ext2gennumber < (u_long)time.tv_sec)
   1002 			ext2gennumber = time.tv_sec;
   1003 		ip->i_e2fs_gen = ext2gennumber;
   1004 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
   1005 			ip->i_flag |= IN_MODIFIED;
   1006 	}
   1007 	vp->v_size = ip->i_e2fs_size;
   1008 	*vpp = vp;
   1009 	return (0);
   1010 }
   1011 
   1012 /*
   1013  * File handle to vnode
   1014  *
   1015  * Have to be really careful about stale file handles:
   1016  * - check that the inode number is valid
   1017  * - call ext2fs_vget() to get the locked inode
   1018  * - check for an unallocated inode (i_mode == 0)
   1019  */
   1020 int
   1021 ext2fs_fhtovp(mp, fhp, vpp)
   1022 	struct mount *mp;
   1023 	struct fid *fhp;
   1024 	struct vnode **vpp;
   1025 {
   1026 	struct inode *ip;
   1027 	struct vnode *nvp;
   1028 	int error;
   1029 	struct ufid *ufhp;
   1030 	struct m_ext2fs *fs;
   1031 
   1032 	ufhp = (struct ufid *)fhp;
   1033 	fs = VFSTOUFS(mp)->um_e2fs;
   1034 	if ((ufhp->ufid_ino < EXT2_FIRSTINO && ufhp->ufid_ino != EXT2_ROOTINO) ||
   1035 		ufhp->ufid_ino >= fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
   1036 		return (ESTALE);
   1037 
   1038 	if ((error = VFS_VGET(mp, ufhp->ufid_ino, &nvp)) != 0) {
   1039 		*vpp = NULLVP;
   1040 		return (error);
   1041 	}
   1042 	ip = VTOI(nvp);
   1043 	if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0 ||
   1044 		ip->i_e2fs_gen != ufhp->ufid_gen) {
   1045 		vput(nvp);
   1046 		*vpp = NULLVP;
   1047 		return (ESTALE);
   1048 	}
   1049 	*vpp = nvp;
   1050 	return (0);
   1051 }
   1052 
   1053 /*
   1054  * Vnode pointer to File handle
   1055  */
   1056 /* ARGSUSED */
   1057 int
   1058 ext2fs_vptofh(vp, fhp)
   1059 	struct vnode *vp;
   1060 	struct fid *fhp;
   1061 {
   1062 	struct inode *ip;
   1063 	struct ufid *ufhp;
   1064 
   1065 	ip = VTOI(vp);
   1066 	ufhp = (struct ufid *)fhp;
   1067 	ufhp->ufid_len = sizeof(struct ufid);
   1068 	ufhp->ufid_ino = ip->i_number;
   1069 	ufhp->ufid_gen = ip->i_e2fs_gen;
   1070 	return (0);
   1071 }
   1072 
   1073 SYSCTL_SETUP(sysctl_vfs_ext2fs_setup, "sysctl vfs.ext2fs subtree setup")
   1074 {
   1075 
   1076 	sysctl_createv(clog, 0, NULL, NULL,
   1077 		       CTLFLAG_PERMANENT,
   1078 		       CTLTYPE_NODE, "vfs", NULL,
   1079 		       NULL, 0, NULL, 0,
   1080 		       CTL_VFS, CTL_EOL);
   1081 	sysctl_createv(clog, 0, NULL, NULL,
   1082 		       CTLFLAG_PERMANENT,
   1083 		       CTLTYPE_NODE, "ext2fs",
   1084 		       SYSCTL_DESCR("Linux EXT2FS file system"),
   1085 		       NULL, 0, NULL, 0,
   1086 		       CTL_VFS, 17, CTL_EOL);
   1087 	/*
   1088 	 * XXX the "17" above could be dynamic, thereby eliminating
   1089 	 * one more instance of the "number to vfs" mapping problem,
   1090 	 * but "17" is the order as taken from sys/mount.h
   1091 	 */
   1092 }
   1093 
   1094 /*
   1095  * Write a superblock and associated information back to disk.
   1096  */
   1097 int
   1098 ext2fs_sbupdate(mp, waitfor)
   1099 	struct ufsmount *mp;
   1100 	int waitfor;
   1101 {
   1102 	struct m_ext2fs *fs = mp->um_e2fs;
   1103 	struct buf *bp;
   1104 	int error = 0;
   1105 
   1106 	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
   1107 	e2fs_sbsave(&fs->e2fs, (struct ext2fs*)bp->b_data);
   1108 	if (waitfor == MNT_WAIT)
   1109 		error = bwrite(bp);
   1110 	else
   1111 		bawrite(bp);
   1112 	return (error);
   1113 }
   1114 
   1115 int
   1116 ext2fs_cgupdate(mp, waitfor)
   1117 	struct ufsmount *mp;
   1118 	int waitfor;
   1119 {
   1120 	struct m_ext2fs *fs = mp->um_e2fs;
   1121 	struct buf *bp;
   1122 	int i, error = 0, allerror = 0;
   1123 
   1124 	allerror = ext2fs_sbupdate(mp, waitfor);
   1125 	for (i = 0; i < fs->e2fs_ngdb; i++) {
   1126 		bp = getblk(mp->um_devvp, fsbtodb(fs, ((fs->e2fs_bsize>1024)?0:1)+i+1),
   1127 			fs->e2fs_bsize, 0, 0);
   1128 		e2fs_cgsave(&fs->e2fs_gd[i* fs->e2fs_bsize / sizeof(struct ext2_gd)],
   1129 				(struct ext2_gd*)bp->b_data, fs->e2fs_bsize);
   1130 		if (waitfor == MNT_WAIT)
   1131 			error = bwrite(bp);
   1132 		else
   1133 			bawrite(bp);
   1134 	}
   1135 
   1136 	if (!allerror && error)
   1137 		allerror = error;
   1138 	return (allerror);
   1139 }
   1140 
   1141 static int
   1142 ext2fs_checksb(fs, ronly)
   1143 	struct ext2fs *fs;
   1144 	int ronly;
   1145 {
   1146 	if (fs2h16(fs->e2fs_magic) != E2FS_MAGIC) {
   1147 		return (EIO);		/* XXX needs translation */
   1148 	}
   1149 	if (fs2h32(fs->e2fs_rev) > E2FS_REV1) {
   1150 #ifdef DIAGNOSTIC
   1151 		printf("Ext2 fs: unsupported revision number: %x\n",
   1152 					fs2h32(fs->e2fs_rev));
   1153 #endif
   1154 		return (EIO);		/* XXX needs translation */
   1155 	}
   1156 	if (fs2h32(fs->e2fs_log_bsize) > 2) { /* block size = 1024|2048|4096 */
   1157 #ifdef DIAGNOSTIC
   1158 		printf("Ext2 fs: bad block size: %d (expected <=2 for ext2 fs)\n",
   1159 			fs2h32(fs->e2fs_log_bsize));
   1160 #endif
   1161 		return (EIO);	   /* XXX needs translation */
   1162 	}
   1163 	if (fs2h32(fs->e2fs_rev) > E2FS_REV0) {
   1164 		if (fs2h32(fs->e2fs_first_ino) != EXT2_FIRSTINO ||
   1165 		    fs2h16(fs->e2fs_inode_size) != EXT2_DINODE_SIZE) {
   1166 			printf("Ext2 fs: unsupported inode size\n");
   1167 			return (EINVAL);      /* XXX needs translation */
   1168 		}
   1169 		if (fs2h32(fs->e2fs_features_incompat) &
   1170 		    ~EXT2F_INCOMPAT_SUPP) {
   1171 			printf("Ext2 fs: unsupported optional feature\n");
   1172 			return (EINVAL);      /* XXX needs translation */
   1173 		}
   1174 		if (!ronly && fs2h32(fs->e2fs_features_rocompat) &
   1175 		    ~EXT2F_ROCOMPAT_SUPP) {
   1176 			return (EROFS);      /* XXX needs translation */
   1177 		}
   1178 	}
   1179 	return (0);
   1180 }
   1181