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