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