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