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