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