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