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