Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_vfsops.c revision 1.62
      1 /*	$NetBSD: ext2fs_vfsops.c,v 1.62 2003/10/05 17:48:49 bouyer 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 REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  *
     64  *	@(#)ffs_vfsops.c	8.14 (Berkeley) 11/28/94
     65  * Modified for ext2fs by Manuel Bouyer.
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: ext2fs_vfsops.c,v 1.62 2003/10/05 17:48:49 bouyer Exp $");
     70 
     71 #if defined(_KERNEL_OPT)
     72 #include "opt_compat_netbsd.h"
     73 #endif
     74 
     75 #include <sys/param.h>
     76 #include <sys/systm.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_statfs,
    130 	ext2fs_sync,
    131 	ext2fs_vget,
    132 	ext2fs_fhtovp,
    133 	ext2fs_vptofh,
    134 	ext2fs_init,
    135 	ext2fs_reinit,
    136 	ext2fs_done,
    137 	ext2fs_sysctl,
    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 struct pool ext2fs_inode_pool;
    150 struct pool ext2fs_dinode_pool;
    151 
    152 extern u_long ext2gennumber;
    153 
    154 void
    155 ext2fs_init()
    156 {
    157 	ufs_init();
    158 
    159 	/*
    160 	 * XXX Same structure as FFS inodes?  Should we share a common pool?
    161 	 */
    162 	pool_init(&ext2fs_inode_pool, sizeof(struct inode), 0, 0, 0,
    163 	    "ext2fsinopl", &pool_allocator_nointr);
    164 	pool_init(&ext2fs_dinode_pool, sizeof(struct ext2fs_dinode), 0, 0, 0,
    165 	    "ext2dinopl", &pool_allocator_nointr);
    166 }
    167 
    168 void
    169 ext2fs_reinit()
    170 {
    171 	ufs_reinit();
    172 }
    173 
    174 void
    175 ext2fs_done()
    176 {
    177 	ufs_done();
    178 	pool_destroy(&ext2fs_inode_pool);
    179 }
    180 
    181 /*
    182  * Called by main() when ext2fs is going to be mounted as root.
    183  *
    184  * Name is updated by mount(8) after booting.
    185  */
    186 #define ROOTNAME	"root_device"
    187 
    188 int
    189 ext2fs_mountroot()
    190 {
    191 	extern struct vnode *rootvp;
    192 	struct m_ext2fs *fs;
    193 	struct mount *mp;
    194 	struct proc *p = curproc;	/* XXX */
    195 	struct ufsmount *ump;
    196 	int error;
    197 
    198 	if (root_device->dv_class != DV_DISK)
    199 		return (ENODEV);
    200 
    201 	/*
    202 	 * Get vnodes for rootdev.
    203 	 */
    204 	if (bdevvp(rootdev, &rootvp))
    205 		panic("ext2fs_mountroot: can't setup bdevvp's");
    206 
    207 	if ((error = vfs_rootmountalloc(MOUNT_EXT2FS, "root_device", &mp))) {
    208 		vrele(rootvp);
    209 		return (error);
    210 	}
    211 
    212 	if ((error = ext2fs_mountfs(rootvp, mp, p)) != 0) {
    213 		mp->mnt_op->vfs_refcount--;
    214 		vfs_unbusy(mp);
    215 		free(mp, M_MOUNT);
    216 		vrele(rootvp);
    217 		return (error);
    218 	}
    219 	simple_lock(&mountlist_slock);
    220 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    221 	simple_unlock(&mountlist_slock);
    222 	ump = VFSTOUFS(mp);
    223 	fs = ump->um_e2fs;
    224 	memset(fs->e2fs_fsmnt, 0, sizeof(fs->e2fs_fsmnt));
    225 	(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
    226 	    sizeof(fs->e2fs_fsmnt) - 1, 0);
    227 	if (fs->e2fs.e2fs_rev > E2FS_REV0) {
    228 		memset(fs->e2fs.e2fs_fsmnt, 0, sizeof(fs->e2fs.e2fs_fsmnt));
    229 		(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
    230 		    sizeof(fs->e2fs.e2fs_fsmnt) - 1, 0);
    231 	}
    232 	(void)ext2fs_statfs(mp, &mp->mnt_stat, p);
    233 	vfs_unbusy(mp);
    234 	inittodr(fs->e2fs.e2fs_wtime);
    235 	return (0);
    236 }
    237 
    238 /*
    239  * VFS Operations.
    240  *
    241  * mount system call
    242  */
    243 int
    244 ext2fs_mount(mp, path, data, ndp, p)
    245 	struct mount *mp;
    246 	const char *path;
    247 	void * data;
    248 	struct nameidata *ndp;
    249 	struct proc *p;
    250 {
    251 	struct vnode *devvp;
    252 	struct ufs_args args;
    253 	struct ufsmount *ump = NULL;
    254 	struct m_ext2fs *fs;
    255 	size_t size;
    256 	int error, flags;
    257 	mode_t accessmode;
    258 
    259 	if (mp->mnt_flag & MNT_GETARGS) {
    260 		ump = VFSTOUFS(mp);
    261 		if (ump == NULL)
    262 			return EIO;
    263 		args.fspec = NULL;
    264 		vfs_showexport(mp, &args.export, &ump->um_export);
    265 		return copyout(&args, data, sizeof(args));
    266 	}
    267 
    268 	error = copyin(data, &args, sizeof (struct ufs_args));
    269 	if (error)
    270 		return (error);
    271 	/*
    272 	 * If updating, check whether changing from read-only to
    273 	 * read/write; if there is no device name, that's all we do.
    274 	 */
    275 	if (mp->mnt_flag & MNT_UPDATE) {
    276 		ump = VFSTOUFS(mp);
    277 		fs = ump->um_e2fs;
    278 		if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    279 			flags = WRITECLOSE;
    280 			if (mp->mnt_flag & MNT_FORCE)
    281 				flags |= FORCECLOSE;
    282 			error = ext2fs_flushfiles(mp, flags, p);
    283 			if (error == 0 &&
    284 				ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
    285 				(fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
    286 				fs->e2fs.e2fs_state = E2FS_ISCLEAN;
    287 				(void) ext2fs_sbupdate(ump, MNT_WAIT);
    288 			}
    289 			if (error)
    290 				return (error);
    291 			fs->e2fs_ronly = 1;
    292 		}
    293 		if (mp->mnt_flag & MNT_RELOAD) {
    294 			error = ext2fs_reload(mp, ndp->ni_cnd.cn_cred, p);
    295 			if (error)
    296 				return (error);
    297 		}
    298 		if (fs->e2fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
    299 			/*
    300 			 * If upgrade to read-write by non-root, then verify
    301 			 * that user has necessary permissions on the device.
    302 			 */
    303 			if (p->p_ucred->cr_uid != 0) {
    304 				devvp = ump->um_devvp;
    305 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    306 				error = VOP_ACCESS(devvp, VREAD | VWRITE,
    307 						   p->p_ucred, p);
    308 				VOP_UNLOCK(devvp, 0);
    309 				if (error)
    310 					return (error);
    311 			}
    312 			fs->e2fs_ronly = 0;
    313 			if (fs->e2fs.e2fs_state == E2FS_ISCLEAN)
    314 				fs->e2fs.e2fs_state = 0;
    315 			else
    316 				fs->e2fs.e2fs_state = E2FS_ERRORS;
    317 			fs->e2fs_fmod = 1;
    318 		}
    319 		if (args.fspec == 0) {
    320 			/*
    321 			 * Process export requests.
    322 			 */
    323 			return (vfs_export(mp, &ump->um_export, &args.export));
    324 		}
    325 	}
    326 	/*
    327 	 * Not an update, or updating the name: look up the name
    328 	 * and verify that it refers to a sensible block device.
    329 	 */
    330 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
    331 	if ((error = namei(ndp)) != 0)
    332 		return (error);
    333 	devvp = ndp->ni_vp;
    334 
    335 	if (devvp->v_type != VBLK) {
    336 		vrele(devvp);
    337 		return (ENOTBLK);
    338 	}
    339 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
    340 		vrele(devvp);
    341 		return (ENXIO);
    342 	}
    343 	/*
    344 	 * If mount by non-root, then verify that user has necessary
    345 	 * permissions on the device.
    346 	 */
    347 	if (p->p_ucred->cr_uid != 0) {
    348 		accessmode = VREAD;
    349 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
    350 			accessmode |= VWRITE;
    351 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    352 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
    353 		VOP_UNLOCK(devvp, 0);
    354 		if (error) {
    355 			vrele(devvp);
    356 			return (error);
    357 		}
    358 	}
    359 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
    360 		error = ext2fs_mountfs(devvp, mp, p);
    361 	else {
    362 		if (devvp != ump->um_devvp)
    363 			error = EINVAL;	/* needs translation */
    364 		else
    365 			vrele(devvp);
    366 	}
    367 	if (error) {
    368 		vrele(devvp);
    369 		return (error);
    370 	}
    371 	ump = VFSTOUFS(mp);
    372 	fs = ump->um_e2fs;
    373 	error = set_statfs_info(path, UIO_USERSPACE, args.fspec,
    374 	    UIO_USERSPACE, mp, p);
    375 	(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
    376 	    sizeof(fs->e2fs_fsmnt) - 1, &size);
    377 	memset(fs->e2fs_fsmnt + size, 0, sizeof(fs->e2fs_fsmnt) - size);
    378 	if (fs->e2fs.e2fs_rev > E2FS_REV0) {
    379 		(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
    380 		    sizeof(fs->e2fs.e2fs_fsmnt) - 1, &size);
    381 		memset(fs->e2fs.e2fs_fsmnt, 0,
    382 		    sizeof(fs->e2fs.e2fs_fsmnt) - size);
    383 	}
    384 	if (fs->e2fs_fmod != 0) {	/* XXX */
    385 		fs->e2fs_fmod = 0;
    386 		if (fs->e2fs.e2fs_state == 0)
    387 			fs->e2fs.e2fs_wtime = time.tv_sec;
    388 		else
    389 			printf("%s: file system not clean; please fsck(8)\n",
    390 				mp->mnt_stat.f_mntfromname);
    391 		(void) ext2fs_cgupdate(ump, MNT_WAIT);
    392 	}
    393 	return error;
    394 }
    395 
    396 /*
    397  * Reload all incore data for a filesystem (used after running fsck on
    398  * the root filesystem and finding things to fix). The filesystem must
    399  * be mounted read-only.
    400  *
    401  * Things to do to update the mount:
    402  *	1) invalidate all cached meta-data.
    403  *	2) re-read superblock from disk.
    404  *	3) re-read summary information from disk.
    405  *	4) invalidate all inactive vnodes.
    406  *	5) invalidate all cached file data.
    407  *	6) re-read inode data for all active vnodes.
    408  */
    409 int
    410 ext2fs_reload(mountp, cred, p)
    411 	struct mount *mountp;
    412 	struct ucred *cred;
    413 	struct proc *p;
    414 {
    415 	struct vnode *vp, *nvp, *devvp;
    416 	struct inode *ip;
    417 	struct buf *bp;
    418 	struct m_ext2fs *fs;
    419 	struct ext2fs *newfs;
    420 	struct partinfo dpart;
    421 	int i, size, error;
    422 	caddr_t cp;
    423 
    424 	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
    425 		return (EINVAL);
    426 	/*
    427 	 * Step 1: invalidate all cached meta-data.
    428 	 */
    429 	devvp = VFSTOUFS(mountp)->um_devvp;
    430 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    431 	error = vinvalbuf(devvp, 0, cred, p, 0, 0);
    432 	VOP_UNLOCK(devvp, 0);
    433 	if (error)
    434 		panic("ext2fs_reload: dirty1");
    435 	/*
    436 	 * Step 2: re-read superblock from disk.
    437 	 */
    438 	if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED, p) != 0)
    439 		size = DEV_BSIZE;
    440 	else
    441 		size = dpart.disklab->d_secsize;
    442 	error = bread(devvp, (daddr_t)(SBOFF / size), SBSIZE, NOCRED, &bp);
    443 	if (error) {
    444 		brelse(bp);
    445 		return (error);
    446 	}
    447 	newfs = (struct ext2fs *)bp->b_data;
    448 	error = ext2fs_checksb(newfs, (mountp->mnt_flag & MNT_RDONLY) != 0);
    449 	if (error) {
    450 		brelse(bp);
    451 		return (error);
    452 	}
    453 
    454 	fs = VFSTOUFS(mountp)->um_e2fs;
    455 	/*
    456 	 * copy in new superblock, and compute in-memory values
    457 	 */
    458 	e2fs_sbload(newfs, &fs->e2fs);
    459 	fs->e2fs_ncg =
    460 	    howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
    461 	    fs->e2fs.e2fs_bpg);
    462 	/* XXX assume hw bsize = 512 */
    463 	fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
    464 	fs->e2fs_bsize = 1024 << fs->e2fs.e2fs_log_bsize;
    465 	fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
    466 	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
    467 	fs->e2fs_bmask = ~fs->e2fs_qbmask;
    468 	fs->e2fs_ngdb = howmany(fs->e2fs_ncg,
    469 			fs->e2fs_bsize / sizeof(struct ext2_gd));
    470 	fs->e2fs_ipb = fs->e2fs_bsize / EXT2_DINODE_SIZE;
    471 	fs->e2fs_itpg = fs->e2fs.e2fs_ipg/fs->e2fs_ipb;
    472 
    473 	/*
    474 	 * Step 3: re-read summary information from disk.
    475 	 */
    476 
    477 	for (i=0; i < fs->e2fs_ngdb; i++) {
    478 		error = bread(devvp ,
    479 		    fsbtodb(fs, ((fs->e2fs_bsize>1024)? 0 : 1) + i + 1),
    480 		    fs->e2fs_bsize, NOCRED, &bp);
    481 		if (error) {
    482 			brelse(bp);
    483 			return (error);
    484 		}
    485 		e2fs_cgload((struct ext2_gd*)bp->b_data,
    486 		    &fs->e2fs_gd[i* fs->e2fs_bsize / sizeof(struct ext2_gd)],
    487 		    fs->e2fs_bsize);
    488 		brelse(bp);
    489 	}
    490 
    491 loop:
    492 	simple_lock(&mntvnode_slock);
    493 	for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
    494 		if (vp->v_mount != mountp) {
    495 			simple_unlock(&mntvnode_slock);
    496 			goto loop;
    497 		}
    498 		nvp = vp->v_mntvnodes.le_next;
    499 		/*
    500 		 * Step 4: invalidate all inactive vnodes.
    501 		 */
    502 		if (vrecycle(vp, &mntvnode_slock, p))
    503 			goto loop;
    504 		/*
    505 		 * Step 5: invalidate all cached file data.
    506 		 */
    507 		simple_lock(&vp->v_interlock);
    508 		simple_unlock(&mntvnode_slock);
    509 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    510 			goto loop;
    511 		if (vinvalbuf(vp, 0, cred, p, 0, 0))
    512 			panic("ext2fs_reload: dirty2");
    513 		/*
    514 		 * Step 6: re-read inode data for all active vnodes.
    515 		 */
    516 		ip = VTOI(vp);
    517 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    518 				  (int)fs->e2fs_bsize, NOCRED, &bp);
    519 		if (error) {
    520 			vput(vp);
    521 			return (error);
    522 		}
    523 		cp = (caddr_t)bp->b_data +
    524 		    (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE);
    525 		e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
    526 		brelse(bp);
    527 		vput(vp);
    528 		simple_lock(&mntvnode_slock);
    529 	}
    530 	simple_unlock(&mntvnode_slock);
    531 	return (0);
    532 }
    533 
    534 /*
    535  * Common code for mount and mountroot
    536  */
    537 int
    538 ext2fs_mountfs(devvp, mp, p)
    539 	struct vnode *devvp;
    540 	struct mount *mp;
    541 	struct proc *p;
    542 {
    543 	struct ufsmount *ump;
    544 	struct buf *bp;
    545 	struct ext2fs *fs;
    546 	struct m_ext2fs *m_fs;
    547 	dev_t dev;
    548 	struct partinfo dpart;
    549 	int error, i, size, ronly;
    550 	struct ucred *cred;
    551 	extern struct vnode *rootvp;
    552 
    553 	dev = devvp->v_rdev;
    554 	cred = p ? p->p_ucred : NOCRED;
    555 	/*
    556 	 * Disallow multiple mounts of the same device.
    557 	 * Disallow mounting of a device that is currently in use
    558 	 * (except for root, which might share swap device for miniroot).
    559 	 * Flush out any old buffers remaining from a previous use.
    560 	 */
    561 	if ((error = vfs_mountedon(devvp)) != 0)
    562 		return (error);
    563 	if (vcount(devvp) > 1 && devvp != rootvp)
    564 		return (EBUSY);
    565 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    566 	error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0);
    567 	VOP_UNLOCK(devvp, 0);
    568 	if (error)
    569 		return (error);
    570 
    571 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    572 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
    573 	if (error)
    574 		return (error);
    575 	if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) != 0)
    576 		size = DEV_BSIZE;
    577 	else
    578 		size = dpart.disklab->d_secsize;
    579 
    580 	bp = NULL;
    581 	ump = NULL;
    582 
    583 #ifdef DEBUG_EXT2
    584 	printf("sb size: %d ino size %d\n", sizeof(struct ext2fs),
    585 	    EXT2_DINODE_SIZE);
    586 #endif
    587 	error = bread(devvp, (SBOFF / size), SBSIZE, cred, &bp);
    588 	if (error)
    589 		goto out;
    590 	fs = (struct ext2fs *)bp->b_data;
    591 	error = ext2fs_checksb(fs, ronly);
    592 	if (error)
    593 		goto out;
    594 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
    595 	memset(ump, 0, sizeof *ump);
    596 	ump->um_fstype = UFS1;
    597 	ump->um_e2fs = malloc(sizeof(struct m_ext2fs), M_UFSMNT, M_WAITOK);
    598 	memset(ump->um_e2fs, 0, sizeof(struct m_ext2fs));
    599 	e2fs_sbload((struct ext2fs*)bp->b_data, &ump->um_e2fs->e2fs);
    600 	brelse(bp);
    601 	bp = NULL;
    602 	m_fs = ump->um_e2fs;
    603 	m_fs->e2fs_ronly = ronly;
    604 	if (ronly == 0) {
    605 		if (m_fs->e2fs.e2fs_state == E2FS_ISCLEAN)
    606 			m_fs->e2fs.e2fs_state = 0;
    607 		else
    608 			m_fs->e2fs.e2fs_state = E2FS_ERRORS;
    609 		m_fs->e2fs_fmod = 1;
    610 	}
    611 
    612 	/* compute dynamic sb infos */
    613 	m_fs->e2fs_ncg =
    614 		howmany(m_fs->e2fs.e2fs_bcount - m_fs->e2fs.e2fs_first_dblock,
    615 		m_fs->e2fs.e2fs_bpg);
    616 	/* XXX assume hw bsize = 512 */
    617 	m_fs->e2fs_fsbtodb = m_fs->e2fs.e2fs_log_bsize + 1;
    618 	m_fs->e2fs_bsize = 1024 << m_fs->e2fs.e2fs_log_bsize;
    619 	m_fs->e2fs_bshift = LOG_MINBSIZE + m_fs->e2fs.e2fs_log_bsize;
    620 	m_fs->e2fs_qbmask = m_fs->e2fs_bsize - 1;
    621 	m_fs->e2fs_bmask = ~m_fs->e2fs_qbmask;
    622 	m_fs->e2fs_ngdb = howmany(m_fs->e2fs_ncg,
    623 		m_fs->e2fs_bsize / sizeof(struct ext2_gd));
    624 	m_fs->e2fs_ipb = m_fs->e2fs_bsize / EXT2_DINODE_SIZE;
    625 	m_fs->e2fs_itpg = m_fs->e2fs.e2fs_ipg/m_fs->e2fs_ipb;
    626 
    627 	m_fs->e2fs_gd = malloc(m_fs->e2fs_ngdb * m_fs->e2fs_bsize,
    628 		M_UFSMNT, M_WAITOK);
    629 	for (i=0; i < m_fs->e2fs_ngdb; i++) {
    630 		error = bread(devvp ,
    631 		    fsbtodb(m_fs, ((m_fs->e2fs_bsize>1024)? 0 : 1) + i + 1),
    632 		    m_fs->e2fs_bsize, NOCRED, &bp);
    633 		if (error) {
    634 			free(m_fs->e2fs_gd, M_UFSMNT);
    635 			goto out;
    636 		}
    637 		e2fs_cgload((struct ext2_gd*)bp->b_data,
    638 		    &m_fs->e2fs_gd[
    639 			i * m_fs->e2fs_bsize / sizeof(struct ext2_gd)],
    640 		    m_fs->e2fs_bsize);
    641 		brelse(bp);
    642 		bp = NULL;
    643 	}
    644 
    645 	mp->mnt_data = ump;
    646 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
    647 	mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_EXT2FS);
    648 	mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
    649 	mp->mnt_flag |= MNT_LOCAL;
    650 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
    651 	mp->mnt_fs_bshift = m_fs->e2fs_bshift;
    652 	ump->um_flags = 0;
    653 	ump->um_mountp = mp;
    654 	ump->um_dev = dev;
    655 	ump->um_devvp = devvp;
    656 	ump->um_nindir = NINDIR(m_fs);
    657 	ump->um_lognindir = ffs(NINDIR(m_fs)) - 1;
    658 	ump->um_bptrtodb = m_fs->e2fs_fsbtodb;
    659 	ump->um_seqinc = 1; /* no frags */
    660 	devvp->v_specmountpoint = mp;
    661 	return (0);
    662 
    663 out:
    664 	if (bp)
    665 		brelse(bp);
    666 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    667 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
    668 	VOP_UNLOCK(devvp, 0);
    669 	if (ump) {
    670 		free(ump->um_e2fs, M_UFSMNT);
    671 		free(ump, M_UFSMNT);
    672 		mp->mnt_data = NULL;
    673 	}
    674 	return (error);
    675 }
    676 
    677 /*
    678  * unmount system call
    679  */
    680 int
    681 ext2fs_unmount(mp, mntflags, p)
    682 	struct mount *mp;
    683 	int mntflags;
    684 	struct proc *p;
    685 {
    686 	struct ufsmount *ump;
    687 	struct m_ext2fs *fs;
    688 	int error, flags;
    689 
    690 	flags = 0;
    691 	if (mntflags & MNT_FORCE)
    692 		flags |= FORCECLOSE;
    693 	if ((error = ext2fs_flushfiles(mp, flags, p)) != 0)
    694 		return (error);
    695 	ump = VFSTOUFS(mp);
    696 	fs = ump->um_e2fs;
    697 	if (fs->e2fs_ronly == 0 &&
    698 		ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
    699 		(fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
    700 		fs->e2fs.e2fs_state = E2FS_ISCLEAN;
    701 		(void) ext2fs_sbupdate(ump, MNT_WAIT);
    702 	}
    703 	if (ump->um_devvp->v_type != VBAD)
    704 		ump->um_devvp->v_specmountpoint = NULL;
    705 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    706 	error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
    707 		NOCRED, p);
    708 	vput(ump->um_devvp);
    709 	free(fs->e2fs_gd, M_UFSMNT);
    710 	free(fs, M_UFSMNT);
    711 	free(ump, M_UFSMNT);
    712 	mp->mnt_data = NULL;
    713 	mp->mnt_flag &= ~MNT_LOCAL;
    714 	return (error);
    715 }
    716 
    717 /*
    718  * Flush out all the files in a filesystem.
    719  */
    720 int
    721 ext2fs_flushfiles(mp, flags, p)
    722 	struct mount *mp;
    723 	int flags;
    724 	struct proc *p;
    725 {
    726 	extern int doforce;
    727 	int error;
    728 
    729 	if (!doforce)
    730 		flags &= ~FORCECLOSE;
    731 	error = vflush(mp, NULLVP, flags);
    732 	return (error);
    733 }
    734 
    735 /*
    736  * Get file system statistics.
    737  */
    738 int
    739 ext2fs_statfs(mp, sbp, p)
    740 	struct mount *mp;
    741 	struct statfs *sbp;
    742 	struct proc *p;
    743 {
    744 	struct ufsmount *ump;
    745 	struct m_ext2fs *fs;
    746 	u_int32_t overhead, overhead_per_group;
    747 	int i, ngroups;
    748 
    749 	ump = VFSTOUFS(mp);
    750 	fs = ump->um_e2fs;
    751 	if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
    752 		panic("ext2fs_statfs");
    753 
    754 #ifdef COMPAT_09
    755 	sbp->f_type = 1;
    756 #else
    757 	sbp->f_type = 0;
    758 #endif
    759 
    760 	/*
    761 	 * Compute the overhead (FS structures)
    762 	 */
    763 	overhead_per_group = 1 /* block bitmap */ +
    764 				 1 /* inode bitmap */ +
    765 				 fs->e2fs_itpg;
    766 	overhead = fs->e2fs.e2fs_first_dblock +
    767 		   fs->e2fs_ncg * overhead_per_group;
    768 	if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
    769 	    fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
    770 		for (i = 0, ngroups = 0; i < fs->e2fs_ncg; i++) {
    771 			if (cg_has_sb(i))
    772 				ngroups++;
    773 		}
    774 	} else {
    775 		ngroups = fs->e2fs_ncg;
    776 	}
    777 	overhead += ngroups * (1 + fs->e2fs_ngdb);
    778 
    779 	sbp->f_bsize = fs->e2fs_bsize;
    780 	sbp->f_iosize = fs->e2fs_bsize;
    781 	sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
    782 	sbp->f_bfree = fs->e2fs.e2fs_fbcount;
    783 	sbp->f_bavail = sbp->f_bfree - fs->e2fs.e2fs_rbcount;
    784 	sbp->f_files =  fs->e2fs.e2fs_icount;
    785 	sbp->f_ffree = fs->e2fs.e2fs_ficount;
    786 	copy_statfs_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 int
   1064 ext2fs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1065 	int *name;
   1066 	u_int namelen;
   1067 	void *oldp;
   1068 	size_t *oldlenp;
   1069 	void *newp;
   1070 	size_t newlen;
   1071 	struct proc *p;
   1072 {
   1073 	return (EOPNOTSUPP);
   1074 }
   1075 
   1076 /*
   1077  * Write a superblock and associated information back to disk.
   1078  */
   1079 int
   1080 ext2fs_sbupdate(mp, waitfor)
   1081 	struct ufsmount *mp;
   1082 	int waitfor;
   1083 {
   1084 	struct m_ext2fs *fs = mp->um_e2fs;
   1085 	struct buf *bp;
   1086 	int error = 0;
   1087 
   1088 	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
   1089 	e2fs_sbsave(&fs->e2fs, (struct ext2fs*)bp->b_data);
   1090 	if (waitfor == MNT_WAIT)
   1091 		error = bwrite(bp);
   1092 	else
   1093 		bawrite(bp);
   1094 	return (error);
   1095 }
   1096 
   1097 int
   1098 ext2fs_cgupdate(mp, waitfor)
   1099 	struct ufsmount *mp;
   1100 	int waitfor;
   1101 {
   1102 	struct m_ext2fs *fs = mp->um_e2fs;
   1103 	struct buf *bp;
   1104 	int i, error = 0, allerror = 0;
   1105 
   1106 	allerror = ext2fs_sbupdate(mp, waitfor);
   1107 	for (i = 0; i < fs->e2fs_ngdb; i++) {
   1108 		bp = getblk(mp->um_devvp, fsbtodb(fs, ((fs->e2fs_bsize>1024)?0:1)+i+1),
   1109 			fs->e2fs_bsize, 0, 0);
   1110 		e2fs_cgsave(&fs->e2fs_gd[i* fs->e2fs_bsize / sizeof(struct ext2_gd)],
   1111 				(struct ext2_gd*)bp->b_data, fs->e2fs_bsize);
   1112 		if (waitfor == MNT_WAIT)
   1113 			error = bwrite(bp);
   1114 		else
   1115 			bawrite(bp);
   1116 	}
   1117 
   1118 	if (!allerror && error)
   1119 		allerror = error;
   1120 	return (allerror);
   1121 }
   1122 
   1123 static int
   1124 ext2fs_checksb(fs, ronly)
   1125 	struct ext2fs *fs;
   1126 	int ronly;
   1127 {
   1128 	if (fs2h16(fs->e2fs_magic) != E2FS_MAGIC) {
   1129 		return (EIO);		/* XXX needs translation */
   1130 	}
   1131 	if (fs2h32(fs->e2fs_rev) > E2FS_REV1) {
   1132 #ifdef DIAGNOSTIC
   1133 		printf("Ext2 fs: unsupported revision number: %x\n",
   1134 					fs2h32(fs->e2fs_rev));
   1135 #endif
   1136 		return (EIO);		/* XXX needs translation */
   1137 	}
   1138 	if (fs2h32(fs->e2fs_log_bsize) > 2) { /* block size = 1024|2048|4096 */
   1139 #ifdef DIAGNOSTIC
   1140 		printf("Ext2 fs: bad block size: %d (expected <=2 for ext2 fs)\n",
   1141 			fs2h32(fs->e2fs_log_bsize));
   1142 #endif
   1143 		return (EIO);	   /* XXX needs translation */
   1144 	}
   1145 	if (fs2h32(fs->e2fs_rev) > E2FS_REV0) {
   1146 		if (fs2h32(fs->e2fs_first_ino) != EXT2_FIRSTINO ||
   1147 		    fs2h16(fs->e2fs_inode_size) != EXT2_DINODE_SIZE) {
   1148 			printf("Ext2 fs: unsupported inode size\n");
   1149 			return (EINVAL);      /* XXX needs translation */
   1150 		}
   1151 		if (fs2h32(fs->e2fs_features_incompat) &
   1152 		    ~EXT2F_INCOMPAT_SUPP) {
   1153 			printf("Ext2 fs: unsupported optionnal feature\n");
   1154 			return (EINVAL);      /* XXX needs translation */
   1155 		}
   1156 		if (!ronly && fs2h32(fs->e2fs_features_rocompat) &
   1157 		    ~EXT2F_ROCOMPAT_SUPP) {
   1158 			return (EROFS);      /* XXX needs translation */
   1159 		}
   1160 	}
   1161 	return (0);
   1162 }
   1163