xref: /netbsd-src/sys/ufs/ext2fs/ext2fs_vfsops.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: ext2fs_vfsops.c,v 1.182 2014/05/24 16:34:04 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  *
47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57  *
58  *	@(#)ffs_vfsops.c	8.14 (Berkeley) 11/28/94
59  * Modified for ext2fs by Manuel Bouyer.
60  */
61 
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: ext2fs_vfsops.c,v 1.182 2014/05/24 16:34:04 christos Exp $");
64 
65 #if defined(_KERNEL_OPT)
66 #include "opt_compat_netbsd.h"
67 #endif
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/sysctl.h>
72 #include <sys/namei.h>
73 #include <sys/proc.h>
74 #include <sys/kernel.h>
75 #include <sys/vnode.h>
76 #include <sys/socket.h>
77 #include <sys/mount.h>
78 #include <sys/buf.h>
79 #include <sys/device.h>
80 #include <sys/mbuf.h>
81 #include <sys/file.h>
82 #include <sys/disklabel.h>
83 #include <sys/ioctl.h>
84 #include <sys/errno.h>
85 #include <sys/malloc.h>
86 #include <sys/pool.h>
87 #include <sys/lock.h>
88 #include <sys/conf.h>
89 #include <sys/kauth.h>
90 #include <sys/module.h>
91 
92 #include <miscfs/genfs/genfs.h>
93 #include <miscfs/specfs/specdev.h>
94 
95 #include <ufs/ufs/quota.h>
96 #include <ufs/ufs/ufsmount.h>
97 #include <ufs/ufs/inode.h>
98 #include <ufs/ufs/dir.h>
99 #include <ufs/ufs/ufs_extern.h>
100 
101 #include <ufs/ext2fs/ext2fs.h>
102 #include <ufs/ext2fs/ext2fs_dir.h>
103 #include <ufs/ext2fs/ext2fs_extern.h>
104 
105 MODULE(MODULE_CLASS_VFS, ext2fs, "ffs");
106 
107 int ext2fs_sbupdate(struct ufsmount *, int);
108 static int ext2fs_checksb(struct ext2fs *, int);
109 
110 static struct sysctllog *ext2fs_sysctl_log;
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 	.vfs_name = MOUNT_EXT2FS,
125 	.vfs_min_mount_data = sizeof (struct ufs_args),
126 	.vfs_mount = ext2fs_mount,
127 	.vfs_start = ufs_start,
128 	.vfs_unmount = ext2fs_unmount,
129 	.vfs_root = ufs_root,
130 	.vfs_quotactl = ufs_quotactl,
131 	.vfs_statvfs = ext2fs_statvfs,
132 	.vfs_sync = ext2fs_sync,
133 	.vfs_vget = ufs_vget,
134 	.vfs_loadvnode = ext2fs_loadvnode,
135 	.vfs_fhtovp = ext2fs_fhtovp,
136 	.vfs_vptofh = ext2fs_vptofh,
137 	.vfs_init = ext2fs_init,
138 	.vfs_reinit = ext2fs_reinit,
139 	.vfs_done = ext2fs_done,
140 	.vfs_mountroot = ext2fs_mountroot,
141 	.vfs_snapshot = (void *)eopnotsupp,
142 	.vfs_extattrctl = vfs_stdextattrctl,
143 	.vfs_suspendctl = (void *)eopnotsupp,
144 	.vfs_renamelock_enter = genfs_renamelock_enter,
145 	.vfs_renamelock_exit = genfs_renamelock_exit,
146 	.vfs_fsync = (void *)eopnotsupp,
147 	.vfs_opv_descs = ext2fs_vnodeopv_descs
148 };
149 
150 static const struct genfs_ops ext2fs_genfsops = {
151 	.gop_size = genfs_size,
152 	.gop_alloc = ext2fs_gop_alloc,
153 	.gop_write = genfs_gop_write,
154 	.gop_markupdate = ufs_gop_markupdate,
155 };
156 
157 static const struct ufs_ops ext2fs_ufsops = {
158 	.uo_itimes = ext2fs_itimes,
159 	.uo_update = ext2fs_update,
160 	.uo_vfree = ext2fs_vfree,
161 };
162 
163 /* Fill in the inode uid/gid from ext2 halves.  */
164 void
165 ext2fs_set_inode_guid(struct inode *ip)
166 {
167 
168 	ip->i_gid = ip->i_e2fs_gid;
169 	ip->i_uid = ip->i_e2fs_uid;
170 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
171 		ip->i_gid |= ip->i_e2fs_gid_high << 16;
172 		ip->i_uid |= ip->i_e2fs_uid_high << 16;
173 	}
174 }
175 
176 static int
177 ext2fs_modcmd(modcmd_t cmd, void *arg)
178 {
179 	int error;
180 
181 	switch (cmd) {
182 	case MODULE_CMD_INIT:
183 		error = vfs_attach(&ext2fs_vfsops);
184 		if (error != 0)
185 			break;
186 		sysctl_createv(&ext2fs_sysctl_log, 0, NULL, NULL,
187 			       CTLFLAG_PERMANENT,
188 			       CTLTYPE_NODE, "ext2fs",
189 			       SYSCTL_DESCR("Linux EXT2FS file system"),
190 			       NULL, 0, NULL, 0,
191 			       CTL_VFS, 17, CTL_EOL);
192 		/*
193 		 * XXX the "17" above could be dynamic, thereby eliminating
194 		 * one more instance of the "number to vfs" mapping problem,
195 		 * but "17" is the order as taken from sys/mount.h
196 		 */
197 		break;
198 	case MODULE_CMD_FINI:
199 		error = vfs_detach(&ext2fs_vfsops);
200 		if (error != 0)
201 			break;
202 		sysctl_teardown(&ext2fs_sysctl_log);
203 		break;
204 	default:
205 		error = ENOTTY;
206 		break;
207 	}
208 
209 	return (error);
210 }
211 
212 /*
213  * XXX Same structure as FFS inodes?  Should we share a common pool?
214  */
215 struct pool ext2fs_inode_pool;
216 struct pool ext2fs_dinode_pool;
217 
218 extern u_long ext2gennumber;
219 
220 void
221 ext2fs_init(void)
222 {
223 
224 	pool_init(&ext2fs_inode_pool, sizeof(struct inode), 0, 0, 0,
225 	    "ext2fsinopl", &pool_allocator_nointr, IPL_NONE);
226 	pool_init(&ext2fs_dinode_pool, sizeof(struct ext2fs_dinode), 0, 0, 0,
227 	    "ext2dinopl", &pool_allocator_nointr, IPL_NONE);
228 	ufs_init();
229 }
230 
231 void
232 ext2fs_reinit(void)
233 {
234 	ufs_reinit();
235 }
236 
237 void
238 ext2fs_done(void)
239 {
240 
241 	ufs_done();
242 	pool_destroy(&ext2fs_inode_pool);
243 	pool_destroy(&ext2fs_dinode_pool);
244 }
245 
246 /*
247  * Called by main() when ext2fs is going to be mounted as root.
248  *
249  * Name is updated by mount(8) after booting.
250  */
251 #define ROOTNAME	"root_device"
252 
253 int
254 ext2fs_mountroot(void)
255 {
256 	extern struct vnode *rootvp;
257 	struct m_ext2fs *fs;
258 	struct mount *mp;
259 	struct ufsmount *ump;
260 	int error;
261 
262 	if (device_class(root_device) != DV_DISK)
263 		return (ENODEV);
264 
265 	if ((error = vfs_rootmountalloc(MOUNT_EXT2FS, "root_device", &mp))) {
266 		vrele(rootvp);
267 		return (error);
268 	}
269 
270 	if ((error = ext2fs_mountfs(rootvp, mp)) != 0) {
271 		vfs_unbusy(mp, false, NULL);
272 		vfs_destroy(mp);
273 		return (error);
274 	}
275 	mountlist_append(mp);
276 	ump = VFSTOUFS(mp);
277 	fs = ump->um_e2fs;
278 	memset(fs->e2fs_fsmnt, 0, sizeof(fs->e2fs_fsmnt));
279 	(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
280 	    sizeof(fs->e2fs_fsmnt) - 1, 0);
281 	if (fs->e2fs.e2fs_rev > E2FS_REV0) {
282 		memset(fs->e2fs.e2fs_fsmnt, 0, sizeof(fs->e2fs.e2fs_fsmnt));
283 		(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
284 		    sizeof(fs->e2fs.e2fs_fsmnt) - 1, 0);
285 	}
286 	(void)ext2fs_statvfs(mp, &mp->mnt_stat);
287 	vfs_unbusy(mp, false, NULL);
288 	setrootfstime((time_t)fs->e2fs.e2fs_wtime);
289 	return (0);
290 }
291 
292 /*
293  * VFS Operations.
294  *
295  * mount system call
296  */
297 int
298 ext2fs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
299 {
300 	struct lwp *l = curlwp;
301 	struct vnode *devvp;
302 	struct ufs_args *args = data;
303 	struct ufsmount *ump = NULL;
304 	struct m_ext2fs *fs;
305 	size_t size;
306 	int error = 0, flags, update;
307 	mode_t accessmode;
308 
309 	if (args == NULL)
310 		return EINVAL;
311 	if (*data_len < sizeof *args)
312 		return EINVAL;
313 
314 	if (mp->mnt_flag & MNT_GETARGS) {
315 		ump = VFSTOUFS(mp);
316 		if (ump == NULL)
317 			return EIO;
318 		memset(args, 0, sizeof *args);
319 		args->fspec = NULL;
320 		*data_len = sizeof *args;
321 		return 0;
322 	}
323 
324 	update = mp->mnt_flag & MNT_UPDATE;
325 
326 	/* Check arguments */
327 	if (args->fspec != NULL) {
328 		/*
329 		 * Look up the name and verify that it's sane.
330 		 */
331 		error = namei_simple_user(args->fspec,
332 					NSM_FOLLOW_NOEMULROOT, &devvp);
333 		if (error != 0)
334 			return (error);
335 
336 		if (!update) {
337 			/*
338 			 * Be sure this is a valid block device
339 			 */
340 			if (devvp->v_type != VBLK)
341 				error = ENOTBLK;
342 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
343 				error = ENXIO;
344 		} else {
345 		        /*
346 			 * Be sure we're still naming the same device
347 			 * used for our initial mount
348 			 */
349 			ump = VFSTOUFS(mp);
350 			if (devvp != ump->um_devvp) {
351 				if (devvp->v_rdev != ump->um_devvp->v_rdev)
352 					error = EINVAL;
353 				else {
354 					vrele(devvp);
355 					devvp = ump->um_devvp;
356 					vref(devvp);
357 				}
358 			}
359 		}
360 	} else {
361 		if (!update) {
362 			/* New mounts must have a filename for the device */
363 			return (EINVAL);
364 		} else {
365 			ump = VFSTOUFS(mp);
366 			devvp = ump->um_devvp;
367 			vref(devvp);
368 		}
369 	}
370 
371 	/*
372 	 * If mount by non-root, then verify that user has necessary
373 	 * permissions on the device.
374 	 *
375 	 * Permission to update a mount is checked higher, so here we presume
376 	 * updating the mount is okay (for example, as far as securelevel goes)
377 	 * which leaves us with the normal check.
378 	 */
379 	if (error == 0) {
380 		accessmode = VREAD;
381 		if (update ?
382 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
383 		    (mp->mnt_flag & MNT_RDONLY) == 0)
384 			accessmode |= VWRITE;
385 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
386 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
387 		    KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
388 		    KAUTH_ARG(accessmode));
389 		VOP_UNLOCK(devvp);
390 	}
391 
392 	if (error) {
393 		vrele(devvp);
394 		return (error);
395 	}
396 
397 	if (!update) {
398 		int xflags;
399 
400 		if (mp->mnt_flag & MNT_RDONLY)
401 			xflags = FREAD;
402 		else
403 			xflags = FREAD|FWRITE;
404 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
405 		error = VOP_OPEN(devvp, xflags, FSCRED);
406 		VOP_UNLOCK(devvp);
407 		if (error)
408 			goto fail;
409 		error = ext2fs_mountfs(devvp, mp);
410 		if (error) {
411 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
412 			(void)VOP_CLOSE(devvp, xflags, NOCRED);
413 			VOP_UNLOCK(devvp);
414 			goto fail;
415 		}
416 
417 		ump = VFSTOUFS(mp);
418 		fs = ump->um_e2fs;
419 	} else {
420 		/*
421 		 * Update the mount.
422 		 */
423 
424 		/*
425 		 * The initial mount got a reference on this
426 		 * device, so drop the one obtained via
427 		 * namei(), above.
428 		 */
429 		vrele(devvp);
430 
431 		ump = VFSTOUFS(mp);
432 		fs = ump->um_e2fs;
433 		if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
434 			/*
435 			 * Changing from r/w to r/o
436 			 */
437 			flags = WRITECLOSE;
438 			if (mp->mnt_flag & MNT_FORCE)
439 				flags |= FORCECLOSE;
440 			error = ext2fs_flushfiles(mp, flags);
441 			if (error == 0 &&
442 			    ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
443 			    (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
444 				fs->e2fs.e2fs_state = E2FS_ISCLEAN;
445 				(void) ext2fs_sbupdate(ump, MNT_WAIT);
446 			}
447 			if (error)
448 				return (error);
449 			fs->e2fs_ronly = 1;
450 		}
451 
452 		if (mp->mnt_flag & MNT_RELOAD) {
453 			error = ext2fs_reload(mp, l->l_cred, l);
454 			if (error)
455 				return (error);
456 		}
457 
458 		if (fs->e2fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
459 			/*
460 			 * Changing from read-only to read/write
461 			 */
462 			fs->e2fs_ronly = 0;
463 			if (fs->e2fs.e2fs_state == E2FS_ISCLEAN)
464 				fs->e2fs.e2fs_state = 0;
465 			else
466 				fs->e2fs.e2fs_state = E2FS_ERRORS;
467 			fs->e2fs_fmod = 1;
468 		}
469 		if (args->fspec == NULL)
470 			return 0;
471 	}
472 
473 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
474 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
475 	(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
476 	    sizeof(fs->e2fs_fsmnt) - 1, &size);
477 	memset(fs->e2fs_fsmnt + size, 0, sizeof(fs->e2fs_fsmnt) - size);
478 	if (fs->e2fs.e2fs_rev > E2FS_REV0) {
479 		(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
480 		    sizeof(fs->e2fs.e2fs_fsmnt) - 1, &size);
481 		memset(fs->e2fs.e2fs_fsmnt, 0,
482 		    sizeof(fs->e2fs.e2fs_fsmnt) - size);
483 	}
484 	if (fs->e2fs_fmod != 0) {	/* XXX */
485 		fs->e2fs_fmod = 0;
486 		if (fs->e2fs.e2fs_state == 0)
487 			fs->e2fs.e2fs_wtime = time_second;
488 		else
489 			printf("%s: file system not clean; please fsck(8)\n",
490 				mp->mnt_stat.f_mntfromname);
491 		(void) ext2fs_cgupdate(ump, MNT_WAIT);
492 	}
493 	return (error);
494 
495 fail:
496 	vrele(devvp);
497 	return (error);
498 }
499 
500 /*
501  * Reload all incore data for a filesystem (used after running fsck on
502  * the root filesystem and finding things to fix). The filesystem must
503  * be mounted read-only.
504  *
505  * Things to do to update the mount:
506  *	1) invalidate all cached meta-data.
507  *	2) re-read superblock from disk.
508  *	3) re-read summary information from disk.
509  *	4) invalidate all inactive vnodes.
510  *	5) invalidate all cached file data.
511  *	6) re-read inode data for all active vnodes.
512  */
513 int
514 ext2fs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
515 {
516 	struct vnode *vp, *devvp;
517 	struct inode *ip;
518 	struct buf *bp;
519 	struct m_ext2fs *fs;
520 	struct ext2fs *newfs;
521 	int i, error;
522 	void *cp;
523 	struct ufsmount *ump;
524 	struct vnode_iterator *marker;
525 
526 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
527 		return (EINVAL);
528 
529 	ump = VFSTOUFS(mp);
530 	/*
531 	 * Step 1: invalidate all cached meta-data.
532 	 */
533 	devvp = ump->um_devvp;
534 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
535 	error = vinvalbuf(devvp, 0, cred, l, 0, 0);
536 	VOP_UNLOCK(devvp);
537 	if (error)
538 		panic("ext2fs_reload: dirty1");
539 	/*
540 	 * Step 2: re-read superblock from disk.
541 	 */
542 	error = bread(devvp, SBLOCK, SBSIZE, NOCRED, 0, &bp);
543 	if (error) {
544 		return (error);
545 	}
546 	newfs = (struct ext2fs *)bp->b_data;
547 	error = ext2fs_checksb(newfs, (mp->mnt_flag & MNT_RDONLY) != 0);
548 	if (error) {
549 		brelse(bp, 0);
550 		return (error);
551 	}
552 
553 	fs = ump->um_e2fs;
554 	/*
555 	 * copy in new superblock, and compute in-memory values
556 	 */
557 	e2fs_sbload(newfs, &fs->e2fs);
558 	fs->e2fs_ncg =
559 	    howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
560 	    fs->e2fs.e2fs_bpg);
561 	fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + LOG_MINBSIZE - DEV_BSHIFT;
562 	fs->e2fs_bsize = MINBSIZE << fs->e2fs.e2fs_log_bsize;
563 	fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
564 	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
565 	fs->e2fs_bmask = ~fs->e2fs_qbmask;
566 	fs->e2fs_ngdb =
567 	    howmany(fs->e2fs_ncg, fs->e2fs_bsize / sizeof(struct ext2_gd));
568 	fs->e2fs_ipb = fs->e2fs_bsize / EXT2_DINODE_SIZE(fs);
569 	fs->e2fs_itpg = fs->e2fs.e2fs_ipg / fs->e2fs_ipb;
570 	brelse(bp, 0);
571 
572 	/*
573 	 * Step 3: re-read summary information from disk.
574 	 */
575 
576 	for (i = 0; i < fs->e2fs_ngdb; i++) {
577 		error = bread(devvp ,
578 		    EXT2_FSBTODB(fs, fs->e2fs.e2fs_first_dblock +
579 		    1 /* superblock */ + i),
580 		    fs->e2fs_bsize, NOCRED, 0, &bp);
581 		if (error) {
582 			return (error);
583 		}
584 		e2fs_cgload((struct ext2_gd *)bp->b_data,
585 		    &fs->e2fs_gd[i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
586 		    fs->e2fs_bsize);
587 		brelse(bp, 0);
588 	}
589 
590 	vfs_vnode_iterator_init(mp, &marker);
591 	while ((vp = vfs_vnode_iterator_next(marker, NULL, NULL))) {
592 		/*
593 		 * Step 4: invalidate all inactive vnodes.
594 		 */
595 		if (vrecycle(vp))
596 			continue;
597 		/*
598 		 * Step 5: invalidate all cached file data.
599 		 */
600 		if (vn_lock(vp, LK_EXCLUSIVE)) {
601 			vrele(vp);
602 			continue;
603 		}
604 		if (vinvalbuf(vp, 0, cred, l, 0, 0))
605 			panic("ext2fs_reload: dirty2");
606 		/*
607 		 * Step 6: re-read inode data for all active vnodes.
608 		 */
609 		ip = VTOI(vp);
610 		error = bread(devvp, EXT2_FSBTODB(fs, ino_to_fsba(fs, ip->i_number)),
611 		    (int)fs->e2fs_bsize, NOCRED, 0, &bp);
612 		if (error) {
613 			vput(vp);
614 			break;
615 		}
616 		cp = (char *)bp->b_data +
617 		    (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE(fs));
618 		e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
619 		ext2fs_set_inode_guid(ip);
620 		brelse(bp, 0);
621 		vput(vp);
622 	}
623 	vfs_vnode_iterator_destroy(marker);
624 	return (error);
625 }
626 
627 /*
628  * Common code for mount and mountroot
629  */
630 int
631 ext2fs_mountfs(struct vnode *devvp, struct mount *mp)
632 {
633 	struct lwp *l = curlwp;
634 	struct ufsmount *ump;
635 	struct buf *bp;
636 	struct ext2fs *fs;
637 	struct m_ext2fs *m_fs;
638 	dev_t dev;
639 	int error, i, ronly;
640 	kauth_cred_t cred;
641 
642 	dev = devvp->v_rdev;
643 	cred = l ? l->l_cred : NOCRED;
644 
645 	/* Flush out any old buffers remaining from a previous use. */
646 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
647 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
648 	VOP_UNLOCK(devvp);
649 	if (error)
650 		return (error);
651 
652 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
653 
654 	bp = NULL;
655 	ump = NULL;
656 
657 #ifdef DEBUG_EXT2
658 	printf("ext2 sb size: %zu\n", sizeof(struct ext2fs));
659 #endif
660 	error = bread(devvp, SBLOCK, SBSIZE, cred, 0, &bp);
661 	if (error)
662 		goto out;
663 	fs = (struct ext2fs *)bp->b_data;
664 	error = ext2fs_checksb(fs, ronly);
665 	if (error)
666 		goto out;
667 	ump = kmem_zalloc(sizeof(*ump), KM_SLEEP);
668 	ump->um_fstype = UFS1;
669 	ump->um_ops = &ext2fs_ufsops;
670 	ump->um_e2fs = kmem_zalloc(sizeof(struct m_ext2fs), KM_SLEEP);
671 	e2fs_sbload((struct ext2fs *)bp->b_data, &ump->um_e2fs->e2fs);
672 	brelse(bp, 0);
673 	bp = NULL;
674 	m_fs = ump->um_e2fs;
675 	m_fs->e2fs_ronly = ronly;
676 
677 #ifdef DEBUG_EXT2
678 	printf("ext2 ino size %zu\n", EXT2_DINODE_SIZE(m_fs));
679 #endif
680 	if (ronly == 0) {
681 		if (m_fs->e2fs.e2fs_state == E2FS_ISCLEAN)
682 			m_fs->e2fs.e2fs_state = 0;
683 		else
684 			m_fs->e2fs.e2fs_state = E2FS_ERRORS;
685 		m_fs->e2fs_fmod = 1;
686 	}
687 
688 	/* compute dynamic sb infos */
689 	m_fs->e2fs_ncg =
690 	    howmany(m_fs->e2fs.e2fs_bcount - m_fs->e2fs.e2fs_first_dblock,
691 	    m_fs->e2fs.e2fs_bpg);
692 	m_fs->e2fs_fsbtodb = m_fs->e2fs.e2fs_log_bsize + LOG_MINBSIZE - DEV_BSHIFT;
693 	m_fs->e2fs_bsize = MINBSIZE << m_fs->e2fs.e2fs_log_bsize;
694 	m_fs->e2fs_bshift = LOG_MINBSIZE + m_fs->e2fs.e2fs_log_bsize;
695 	m_fs->e2fs_qbmask = m_fs->e2fs_bsize - 1;
696 	m_fs->e2fs_bmask = ~m_fs->e2fs_qbmask;
697 	m_fs->e2fs_ngdb =
698 	    howmany(m_fs->e2fs_ncg, m_fs->e2fs_bsize / sizeof(struct ext2_gd));
699 	m_fs->e2fs_ipb = m_fs->e2fs_bsize / EXT2_DINODE_SIZE(m_fs);
700 	m_fs->e2fs_itpg = m_fs->e2fs.e2fs_ipg / m_fs->e2fs_ipb;
701 
702 	m_fs->e2fs_gd = kmem_alloc(m_fs->e2fs_ngdb * m_fs->e2fs_bsize, KM_SLEEP);
703 	for (i = 0; i < m_fs->e2fs_ngdb; i++) {
704 		error = bread(devvp ,
705 		    EXT2_FSBTODB(m_fs, m_fs->e2fs.e2fs_first_dblock +
706 		    1 /* superblock */ + i),
707 		    m_fs->e2fs_bsize, NOCRED, 0, &bp);
708 		if (error) {
709 			kmem_free(m_fs->e2fs_gd,
710 			    m_fs->e2fs_ngdb * m_fs->e2fs_bsize);
711 			goto out;
712 		}
713 		e2fs_cgload((struct ext2_gd *)bp->b_data,
714 		    &m_fs->e2fs_gd[
715 			i * m_fs->e2fs_bsize / sizeof(struct ext2_gd)],
716 		    m_fs->e2fs_bsize);
717 		brelse(bp, 0);
718 		bp = NULL;
719 	}
720 
721 	mp->mnt_data = ump;
722 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
723 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_EXT2FS);
724 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
725 	mp->mnt_stat.f_namemax = EXT2FS_MAXNAMLEN;
726 	mp->mnt_flag |= MNT_LOCAL;
727 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
728 	mp->mnt_fs_bshift = m_fs->e2fs_bshift;
729 	mp->mnt_iflag |= IMNT_DTYPE;
730 	ump->um_flags = 0;
731 	ump->um_mountp = mp;
732 	ump->um_dev = dev;
733 	ump->um_devvp = devvp;
734 	ump->um_nindir = EXT2_NINDIR(m_fs);
735 	ump->um_lognindir = ffs(EXT2_NINDIR(m_fs)) - 1;
736 	ump->um_bptrtodb = m_fs->e2fs_fsbtodb;
737 	ump->um_seqinc = 1; /* no frags */
738 	ump->um_maxsymlinklen = EXT2_MAXSYMLINKLEN;
739 	ump->um_dirblksiz = m_fs->e2fs_bsize;
740 	ump->um_maxfilesize = ((uint64_t)0x80000000 * m_fs->e2fs_bsize - 1);
741 	spec_node_setmountedfs(devvp, mp);
742 	return (0);
743 
744 out:
745 	if (bp != NULL)
746 		brelse(bp, 0);
747 	if (ump) {
748 		kmem_free(ump->um_e2fs, sizeof(struct m_ext2fs));
749 		kmem_free(ump, sizeof(*ump));
750 		mp->mnt_data = NULL;
751 	}
752 	return (error);
753 }
754 
755 /*
756  * unmount system call
757  */
758 int
759 ext2fs_unmount(struct mount *mp, int mntflags)
760 {
761 	struct ufsmount *ump;
762 	struct m_ext2fs *fs;
763 	int error, flags;
764 
765 	flags = 0;
766 	if (mntflags & MNT_FORCE)
767 		flags |= FORCECLOSE;
768 	if ((error = ext2fs_flushfiles(mp, flags)) != 0)
769 		return (error);
770 	ump = VFSTOUFS(mp);
771 	fs = ump->um_e2fs;
772 	if (fs->e2fs_ronly == 0 &&
773 		ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
774 		(fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
775 		fs->e2fs.e2fs_state = E2FS_ISCLEAN;
776 		(void) ext2fs_sbupdate(ump, MNT_WAIT);
777 	}
778 	if (ump->um_devvp->v_type != VBAD)
779 		spec_node_setmountedfs(ump->um_devvp, NULL);
780 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
781 	error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
782 	    NOCRED);
783 	vput(ump->um_devvp);
784 	kmem_free(fs->e2fs_gd, fs->e2fs_ngdb * fs->e2fs_bsize);
785 	kmem_free(fs, sizeof(*fs));
786 	kmem_free(ump, sizeof(*ump));
787 	mp->mnt_data = NULL;
788 	mp->mnt_flag &= ~MNT_LOCAL;
789 	return (error);
790 }
791 
792 /*
793  * Flush out all the files in a filesystem.
794  */
795 int
796 ext2fs_flushfiles(struct mount *mp, int flags)
797 {
798 	extern int doforce;
799 	int error;
800 
801 	if (!doforce)
802 		flags &= ~FORCECLOSE;
803 	error = vflush(mp, NULLVP, flags);
804 	return (error);
805 }
806 
807 /*
808  * Get file system statistics.
809  */
810 int
811 ext2fs_statvfs(struct mount *mp, struct statvfs *sbp)
812 {
813 	struct ufsmount *ump;
814 	struct m_ext2fs *fs;
815 	uint32_t overhead, overhead_per_group, ngdb;
816 	int i, ngroups;
817 
818 	ump = VFSTOUFS(mp);
819 	fs = ump->um_e2fs;
820 	if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
821 		panic("ext2fs_statvfs");
822 
823 	/*
824 	 * Compute the overhead (FS structures)
825 	 */
826 	overhead_per_group =
827 	    1 /* block bitmap */ +
828 	    1 /* inode bitmap */ +
829 	    fs->e2fs_itpg;
830 	overhead = fs->e2fs.e2fs_first_dblock +
831 	    fs->e2fs_ncg * overhead_per_group;
832 	if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
833 	    fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
834 		for (i = 0, ngroups = 0; i < fs->e2fs_ncg; i++) {
835 			if (cg_has_sb(i))
836 				ngroups++;
837 		}
838 	} else {
839 		ngroups = fs->e2fs_ncg;
840 	}
841 	ngdb = fs->e2fs_ngdb;
842 	if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
843 	    fs->e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE)
844 		ngdb += fs->e2fs.e2fs_reserved_ngdb;
845 	overhead += ngroups * (1 /* superblock */ + ngdb);
846 
847 	sbp->f_bsize = fs->e2fs_bsize;
848 	sbp->f_frsize = MINBSIZE << fs->e2fs.e2fs_fsize;
849 	sbp->f_iosize = fs->e2fs_bsize;
850 	sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
851 	sbp->f_bfree = fs->e2fs.e2fs_fbcount;
852 	sbp->f_bresvd = fs->e2fs.e2fs_rbcount;
853 	if (sbp->f_bfree > sbp->f_bresvd)
854 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
855 	else
856 		sbp->f_bavail = 0;
857 	sbp->f_files =  fs->e2fs.e2fs_icount;
858 	sbp->f_ffree = fs->e2fs.e2fs_ficount;
859 	sbp->f_favail = fs->e2fs.e2fs_ficount;
860 	sbp->f_fresvd = 0;
861 	copy_statvfs_info(sbp, mp);
862 	return (0);
863 }
864 
865 static bool
866 ext2fs_sync_selector(void *cl, struct vnode *vp)
867 {
868 	struct inode *ip;
869 
870 	ip = VTOI(vp);
871 	/*
872 	 * Skip the vnode/inode if inaccessible.
873 	 */
874 	if (ip == NULL || vp->v_type == VNON)
875 		return false;
876 
877 	if (((ip->i_flag &
878 	      (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
879 	     LIST_EMPTY(&vp->v_dirtyblkhd) &&
880 	     UVM_OBJ_IS_CLEAN(&vp->v_uobj)))
881 		return false;
882 	return true;
883 }
884 
885 /*
886  * Go through the disk queues to initiate sandbagged IO;
887  * go through the inodes to write those that have been modified;
888  * initiate the writing of the super block if it has been modified.
889  *
890  * Note: we are always called with the filesystem marked `MPBUSY'.
891  */
892 int
893 ext2fs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
894 {
895 	struct vnode *vp;
896 	struct ufsmount *ump = VFSTOUFS(mp);
897 	struct m_ext2fs *fs;
898 	struct vnode_iterator *marker;
899 	int error, allerror = 0;
900 
901 	fs = ump->um_e2fs;
902 	if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) {	/* XXX */
903 		printf("fs = %s\n", fs->e2fs_fsmnt);
904 		panic("update: rofs mod");
905 	}
906 
907 	/*
908 	 * Write back each (modified) inode.
909 	 */
910 	vfs_vnode_iterator_init(mp, &marker);
911 	while ((vp = vfs_vnode_iterator_next(marker, ext2fs_sync_selector,
912 	    NULL)))
913 	{
914 		error = vn_lock(vp, LK_EXCLUSIVE);
915 		if (error) {
916 			vrele(vp);
917 			continue;
918 		}
919 		if (vp->v_type == VREG && waitfor == MNT_LAZY)
920 			error = ext2fs_update(vp, NULL, NULL, 0);
921 		else
922 			error = VOP_FSYNC(vp, cred,
923 			    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0);
924 		if (error)
925 			allerror = error;
926 		vput(vp);
927 	}
928 	vfs_vnode_iterator_destroy(marker);
929 	/*
930 	 * Force stale file system control information to be flushed.
931 	 */
932 	if (waitfor != MNT_LAZY) {
933 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
934 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
935 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
936 			allerror = error;
937 		VOP_UNLOCK(ump->um_devvp);
938 	}
939 	/*
940 	 * Write back modified superblock.
941 	 */
942 	if (fs->e2fs_fmod != 0) {
943 		fs->e2fs_fmod = 0;
944 		fs->e2fs.e2fs_wtime = time_second;
945 		if ((error = ext2fs_cgupdate(ump, waitfor)))
946 			allerror = error;
947 	}
948 	return (allerror);
949 }
950 
951 /*
952  * Read an inode from disk and initialize this vnode / inode pair.
953  * Caller assures no other thread will try to load this inode.
954  */
955 int
956 ext2fs_loadvnode(struct mount *mp, struct vnode *vp,
957     const void *key, size_t key_len, const void **new_key)
958 {
959 	ino_t ino;
960 	struct m_ext2fs *fs;
961 	struct inode *ip;
962 	struct ufsmount *ump;
963 	struct buf *bp;
964 	dev_t dev;
965 	int error;
966 	void *cp;
967 
968 	KASSERT(key_len == sizeof(ino));
969 	memcpy(&ino, key, key_len);
970 	ump = VFSTOUFS(mp);
971 	dev = ump->um_dev;
972 	fs = ump->um_e2fs;
973 
974 	/* Read in the disk contents for the inode, copy into the inode. */
975 	error = bread(ump->um_devvp, EXT2_FSBTODB(fs, ino_to_fsba(fs, ino)),
976 	    (int)fs->e2fs_bsize, NOCRED, 0, &bp);
977 	if (error)
978 		return error;
979 
980 	/* Allocate and initialize inode. */
981 	ip = pool_get(&ext2fs_inode_pool, PR_WAITOK);
982 	memset(ip, 0, sizeof(struct inode));
983 	vp->v_tag = VT_EXT2FS;
984 	vp->v_op = ext2fs_vnodeop_p;
985 	vp->v_vflag |= VV_LOCKSWORK;
986 	vp->v_data = ip;
987 	ip->i_vnode = vp;
988 	ip->i_ump = ump;
989 	ip->i_e2fs = fs;
990 	ip->i_dev = dev;
991 	ip->i_number = ino;
992 	ip->i_e2fs_last_lblk = 0;
993 	ip->i_e2fs_last_blk = 0;
994 
995 	/* Initialize genfs node. */
996 	genfs_node_init(vp, &ext2fs_genfsops);
997 
998 	cp = (char *)bp->b_data + (ino_to_fsbo(fs, ino) * EXT2_DINODE_SIZE(fs));
999 	ip->i_din.e2fs_din = pool_get(&ext2fs_dinode_pool, PR_WAITOK);
1000 	e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
1001 	ext2fs_set_inode_guid(ip);
1002 	brelse(bp, 0);
1003 
1004 	/* If the inode was deleted, reset all fields */
1005 	if (ip->i_e2fs_dtime != 0) {
1006 		ip->i_e2fs_mode = 0;
1007 		(void)ext2fs_setsize(ip, 0);
1008 		(void)ext2fs_setnblock(ip, 0);
1009 		memset(ip->i_e2fs_blocks, 0, sizeof(ip->i_e2fs_blocks));
1010 	}
1011 
1012 	/* Initialize the vnode from the inode. */
1013 	ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
1014 
1015 	/* Finish inode initialization. */
1016 	ip->i_devvp = ump->um_devvp;
1017 	vref(ip->i_devvp);
1018 
1019 	/*
1020 	 * Set up a generation number for this inode if it does not
1021 	 * already have one. This should only happen on old filesystems.
1022 	 */
1023 
1024 	if (ip->i_e2fs_gen == 0) {
1025 		if (++ext2gennumber < (u_long)time_second)
1026 			ext2gennumber = time_second;
1027 		ip->i_e2fs_gen = ext2gennumber;
1028 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1029 			ip->i_flag |= IN_MODIFIED;
1030 	}
1031 	uvm_vnp_setsize(vp, ext2fs_size(ip));
1032 	*new_key = &ip->i_number;
1033 	return 0;
1034 }
1035 
1036 /*
1037  * File handle to vnode
1038  *
1039  * Have to be really careful about stale file handles:
1040  * - check that the inode number is valid
1041  * - call ext2fs_vget() to get the locked inode
1042  * - check for an unallocated inode (i_mode == 0)
1043  */
1044 int
1045 ext2fs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1046 {
1047 	struct inode *ip;
1048 	struct vnode *nvp;
1049 	int error;
1050 	struct ufid ufh;
1051 	struct m_ext2fs *fs;
1052 
1053 	if (fhp->fid_len != sizeof(struct ufid))
1054 		return EINVAL;
1055 
1056 	memcpy(&ufh, fhp, sizeof(struct ufid));
1057 	fs = VFSTOUFS(mp)->um_e2fs;
1058 	if ((ufh.ufid_ino < EXT2_FIRSTINO && ufh.ufid_ino != EXT2_ROOTINO) ||
1059 		ufh.ufid_ino >= fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
1060 		return (ESTALE);
1061 
1062 	if ((error = VFS_VGET(mp, ufh.ufid_ino, &nvp)) != 0) {
1063 		*vpp = NULLVP;
1064 		return (error);
1065 	}
1066 	ip = VTOI(nvp);
1067 	if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0 ||
1068 		ip->i_e2fs_gen != ufh.ufid_gen) {
1069 		vput(nvp);
1070 		*vpp = NULLVP;
1071 		return (ESTALE);
1072 	}
1073 	*vpp = nvp;
1074 	return (0);
1075 }
1076 
1077 /*
1078  * Vnode pointer to File handle
1079  */
1080 /* ARGSUSED */
1081 int
1082 ext2fs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1083 {
1084 	struct inode *ip;
1085 	struct ufid ufh;
1086 
1087 	if (*fh_size < sizeof(struct ufid)) {
1088 		*fh_size = sizeof(struct ufid);
1089 		return E2BIG;
1090 	}
1091 	*fh_size = sizeof(struct ufid);
1092 
1093 	ip = VTOI(vp);
1094 	memset(&ufh, 0, sizeof(ufh));
1095 	ufh.ufid_len = sizeof(struct ufid);
1096 	ufh.ufid_ino = ip->i_number;
1097 	ufh.ufid_gen = ip->i_e2fs_gen;
1098 	memcpy(fhp, &ufh, sizeof(ufh));
1099 	return (0);
1100 }
1101 
1102 /*
1103  * Write a superblock and associated information back to disk.
1104  */
1105 int
1106 ext2fs_sbupdate(struct ufsmount *mp, int waitfor)
1107 {
1108 	struct m_ext2fs *fs = mp->um_e2fs;
1109 	struct buf *bp;
1110 	int error = 0;
1111 
1112 	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
1113 	e2fs_sbsave(&fs->e2fs, (struct ext2fs*)bp->b_data);
1114 	if (waitfor == MNT_WAIT)
1115 		error = bwrite(bp);
1116 	else
1117 		bawrite(bp);
1118 	return (error);
1119 }
1120 
1121 int
1122 ext2fs_cgupdate(struct ufsmount *mp, int waitfor)
1123 {
1124 	struct m_ext2fs *fs = mp->um_e2fs;
1125 	struct buf *bp;
1126 	int i, error = 0, allerror = 0;
1127 
1128 	allerror = ext2fs_sbupdate(mp, waitfor);
1129 	for (i = 0; i < fs->e2fs_ngdb; i++) {
1130 		bp = getblk(mp->um_devvp, EXT2_FSBTODB(fs,
1131 		    fs->e2fs.e2fs_first_dblock +
1132 		    1 /* superblock */ + i), fs->e2fs_bsize, 0, 0);
1133 		e2fs_cgsave(&fs->e2fs_gd[
1134 		    i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
1135 		    (struct ext2_gd *)bp->b_data, fs->e2fs_bsize);
1136 		if (waitfor == MNT_WAIT)
1137 			error = bwrite(bp);
1138 		else
1139 			bawrite(bp);
1140 	}
1141 
1142 	if (!allerror && error)
1143 		allerror = error;
1144 	return (allerror);
1145 }
1146 
1147 static int
1148 ext2fs_checksb(struct ext2fs *fs, int ronly)
1149 {
1150 	uint32_t u32;
1151 
1152 	if (fs2h16(fs->e2fs_magic) != E2FS_MAGIC) {
1153 		return (EINVAL);		/* XXX needs translation */
1154 	}
1155 	if (fs2h32(fs->e2fs_rev) > E2FS_REV1) {
1156 #ifdef DIAGNOSTIC
1157 		printf("ext2fs: unsupported revision number: %x\n",
1158 		    fs2h32(fs->e2fs_rev));
1159 #endif
1160 		return (EINVAL);		/* XXX needs translation */
1161 	}
1162 	if (fs2h32(fs->e2fs_log_bsize) > 2) { /* block size = 1024|2048|4096 */
1163 #ifdef DIAGNOSTIC
1164 		printf("ext2fs: bad block size: %d "
1165 		    "(expected <= 2 for ext2 fs)\n",
1166 		    fs2h32(fs->e2fs_log_bsize));
1167 #endif
1168 		return (EINVAL);	   /* XXX needs translation */
1169 	}
1170 	if (fs2h32(fs->e2fs_rev) > E2FS_REV0) {
1171 		char buf[256];
1172 		if (fs2h32(fs->e2fs_first_ino) != EXT2_FIRSTINO) {
1173 			printf("ext2fs: unsupported first inode position\n");
1174 			return (EINVAL);      /* XXX needs translation */
1175 		}
1176 		u32 = fs2h32(fs->e2fs_features_incompat) & ~EXT2F_INCOMPAT_SUPP;
1177 		if (u32) {
1178 			snprintb(buf, sizeof(buf), EXT2F_INCOMPAT_BITS, u32);
1179 			printf("ext2fs: unsupported incompat features: %s\n",
1180 			    buf);
1181 			return EINVAL;	/* XXX needs translation */
1182 		}
1183 		u32 = fs2h32(fs->e2fs_features_rocompat) & ~EXT2F_ROCOMPAT_SUPP;
1184 		if (!ronly && u32) {
1185 			snprintb(buf, sizeof(buf), EXT2F_ROCOMPAT_BITS, u32);
1186 			printf("ext2fs: unsupported ro-incompat features: %s\n",
1187 			    buf);
1188 			return EROFS;	/* XXX needs translation */
1189 		}
1190 	}
1191 	return (0);
1192 }
1193