xref: /freebsd/sys/fs/ext2fs/ext2_vfsops.c (revision 60eddb209b5ad13a549ca74a41b7cb38a31da5ef)
1 /*-
2  *  modified for EXT2FS support in Lites 1.1
3  *
4  *  Aug 1995, Godmar Back (gback@cs.utah.edu)
5  *  University of Utah, Department of Computer Science
6  */
7 /*-
8  * SPDX-License-Identifier: BSD-3-Clause
9  *
10  * Copyright (c) 1989, 1991, 1993, 1994
11  *	The Regents of the University of California.  All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)ffs_vfsops.c	8.8 (Berkeley) 4/18/94
38  * $FreeBSD$
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/namei.h>
44 #include <sys/priv.h>
45 #include <sys/proc.h>
46 #include <sys/kernel.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
49 #include <sys/bio.h>
50 #include <sys/buf.h>
51 #include <sys/conf.h>
52 #include <sys/endian.h>
53 #include <sys/fcntl.h>
54 #include <sys/malloc.h>
55 #include <sys/stat.h>
56 #include <sys/mutex.h>
57 
58 #include <geom/geom.h>
59 #include <geom/geom_vfs.h>
60 
61 #include <fs/ext2fs/ext2_mount.h>
62 #include <fs/ext2fs/inode.h>
63 
64 #include <fs/ext2fs/fs.h>
65 #include <fs/ext2fs/ext2fs.h>
66 #include <fs/ext2fs/ext2_dinode.h>
67 #include <fs/ext2fs/ext2_extern.h>
68 
69 static int	ext2_flushfiles(struct mount *mp, int flags, struct thread *td);
70 static int	ext2_mountfs(struct vnode *, struct mount *);
71 static int	ext2_reload(struct mount *mp, struct thread *td);
72 static int	ext2_sbupdate(struct ext2mount *, int);
73 static int	ext2_cgupdate(struct ext2mount *, int);
74 static vfs_unmount_t		ext2_unmount;
75 static vfs_root_t		ext2_root;
76 static vfs_statfs_t		ext2_statfs;
77 static vfs_sync_t		ext2_sync;
78 static vfs_vget_t		ext2_vget;
79 static vfs_fhtovp_t		ext2_fhtovp;
80 static vfs_mount_t		ext2_mount;
81 
82 MALLOC_DEFINE(M_EXT2NODE, "ext2_node", "EXT2 vnode private part");
83 static MALLOC_DEFINE(M_EXT2MNT, "ext2_mount", "EXT2 mount structure");
84 
85 static struct vfsops ext2fs_vfsops = {
86 	.vfs_fhtovp =		ext2_fhtovp,
87 	.vfs_mount =		ext2_mount,
88 	.vfs_root =		ext2_root,	/* root inode via vget */
89 	.vfs_statfs =		ext2_statfs,
90 	.vfs_sync =		ext2_sync,
91 	.vfs_unmount =		ext2_unmount,
92 	.vfs_vget =		ext2_vget,
93 };
94 
95 VFS_SET(ext2fs_vfsops, ext2fs, 0);
96 
97 static int	ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev,
98 		    int ronly);
99 static int	compute_sb_data(struct vnode * devvp,
100 		    struct ext2fs * es, struct m_ext2fs * fs);
101 
102 static const char *ext2_opts[] = { "acls", "async", "noatime", "noclusterr",
103     "noclusterw", "noexec", "export", "force", "from", "multilabel",
104     "suiddir", "nosymfollow", "sync", "union", NULL };
105 
106 /*
107  * VFS Operations.
108  *
109  * mount system call
110  */
111 static int
112 ext2_mount(struct mount *mp)
113 {
114 	struct vfsoptlist *opts;
115 	struct vnode *devvp;
116 	struct thread *td;
117 	struct ext2mount *ump = NULL;
118 	struct m_ext2fs *fs;
119 	struct nameidata nd, *ndp = &nd;
120 	accmode_t accmode;
121 	char *path, *fspec;
122 	int error, flags, len;
123 
124 	td = curthread;
125 	opts = mp->mnt_optnew;
126 
127 	if (vfs_filteropt(opts, ext2_opts))
128 		return (EINVAL);
129 
130 	vfs_getopt(opts, "fspath", (void **)&path, NULL);
131 	/* Double-check the length of path.. */
132 	if (strlen(path) >= MAXMNTLEN)
133 		return (ENAMETOOLONG);
134 
135 	fspec = NULL;
136 	error = vfs_getopt(opts, "from", (void **)&fspec, &len);
137 	if (!error && fspec[len - 1] != '\0')
138 		return (EINVAL);
139 
140 	/*
141 	 * If updating, check whether changing from read-only to
142 	 * read/write; if there is no device name, that's all we do.
143 	 */
144 	if (mp->mnt_flag & MNT_UPDATE) {
145 		ump = VFSTOEXT2(mp);
146 		fs = ump->um_e2fs;
147 		error = 0;
148 		if (fs->e2fs_ronly == 0 &&
149 		    vfs_flagopt(opts, "ro", NULL, 0)) {
150 			error = VFS_SYNC(mp, MNT_WAIT);
151 			if (error)
152 				return (error);
153 			flags = WRITECLOSE;
154 			if (mp->mnt_flag & MNT_FORCE)
155 				flags |= FORCECLOSE;
156 			error = ext2_flushfiles(mp, flags, td);
157 			if (error == 0 && fs->e2fs_wasvalid && ext2_cgupdate(ump, MNT_WAIT) == 0) {
158 				fs->e2fs->e2fs_state |= E2FS_ISCLEAN;
159 				ext2_sbupdate(ump, MNT_WAIT);
160 			}
161 			fs->e2fs_ronly = 1;
162 			vfs_flagopt(opts, "ro", &mp->mnt_flag, MNT_RDONLY);
163 			g_topology_lock();
164 			g_access(ump->um_cp, 0, -1, 0);
165 			g_topology_unlock();
166 		}
167 		if (!error && (mp->mnt_flag & MNT_RELOAD))
168 			error = ext2_reload(mp, td);
169 		if (error)
170 			return (error);
171 		devvp = ump->um_devvp;
172 		if (fs->e2fs_ronly && !vfs_flagopt(opts, "ro", NULL, 0)) {
173 			if (ext2_check_sb_compat(fs->e2fs, devvp->v_rdev, 0))
174 				return (EPERM);
175 
176 			/*
177 			 * If upgrade to read-write by non-root, then verify
178 			 * that user has necessary permissions on the device.
179 			 */
180 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
181 			error = VOP_ACCESS(devvp, VREAD | VWRITE,
182 			    td->td_ucred, td);
183 			if (error)
184 				error = priv_check(td, PRIV_VFS_MOUNT_PERM);
185 			if (error) {
186 				VOP_UNLOCK(devvp, 0);
187 				return (error);
188 			}
189 			VOP_UNLOCK(devvp, 0);
190 			g_topology_lock();
191 			error = g_access(ump->um_cp, 0, 1, 0);
192 			g_topology_unlock();
193 			if (error)
194 				return (error);
195 
196 			if ((fs->e2fs->e2fs_state & E2FS_ISCLEAN) == 0 ||
197 			    (fs->e2fs->e2fs_state & E2FS_ERRORS)) {
198 				if (mp->mnt_flag & MNT_FORCE) {
199 					printf(
200 "WARNING: %s was not properly dismounted\n", fs->e2fs_fsmnt);
201 				} else {
202 					printf(
203 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
204 					    fs->e2fs_fsmnt);
205 					return (EPERM);
206 				}
207 			}
208 			fs->e2fs->e2fs_state &= ~E2FS_ISCLEAN;
209 			(void)ext2_cgupdate(ump, MNT_WAIT);
210 			fs->e2fs_ronly = 0;
211 			MNT_ILOCK(mp);
212 			mp->mnt_flag &= ~MNT_RDONLY;
213 			MNT_IUNLOCK(mp);
214 		}
215 		if (vfs_flagopt(opts, "export", NULL, 0)) {
216 			/* Process export requests in vfs_mount.c. */
217 			return (error);
218 		}
219 	}
220 
221 	/*
222 	 * Not an update, or updating the name: look up the name
223 	 * and verify that it refers to a sensible disk device.
224 	 */
225 	if (fspec == NULL)
226 		return (EINVAL);
227 	NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
228 	if ((error = namei(ndp)) != 0)
229 		return (error);
230 	NDFREE(ndp, NDF_ONLY_PNBUF);
231 	devvp = ndp->ni_vp;
232 
233 	if (!vn_isdisk(devvp, &error)) {
234 		vput(devvp);
235 		return (error);
236 	}
237 
238 	/*
239 	 * If mount by non-root, then verify that user has necessary
240 	 * permissions on the device.
241 	 *
242 	 * XXXRW: VOP_ACCESS() enough?
243 	 */
244 	accmode = VREAD;
245 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
246 		accmode |= VWRITE;
247 	error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
248 	if (error)
249 		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
250 	if (error) {
251 		vput(devvp);
252 		return (error);
253 	}
254 
255 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
256 		error = ext2_mountfs(devvp, mp);
257 	} else {
258 		if (devvp != ump->um_devvp) {
259 			vput(devvp);
260 			return (EINVAL);	/* needs translation */
261 		} else
262 			vput(devvp);
263 	}
264 	if (error) {
265 		vrele(devvp);
266 		return (error);
267 	}
268 	ump = VFSTOEXT2(mp);
269 	fs = ump->um_e2fs;
270 
271 	/*
272 	 * Note that this strncpy() is ok because of a check at the start
273 	 * of ext2_mount().
274 	 */
275 	strncpy(fs->e2fs_fsmnt, path, MAXMNTLEN);
276 	fs->e2fs_fsmnt[MAXMNTLEN - 1] = '\0';
277 	vfs_mountedfrom(mp, fspec);
278 	return (0);
279 }
280 
281 static int
282 ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev, int ronly)
283 {
284 	uint32_t i, mask;
285 
286 	if (es->e2fs_magic != E2FS_MAGIC) {
287 		printf("ext2fs: %s: wrong magic number %#x (expected %#x)\n",
288 		    devtoname(dev), es->e2fs_magic, E2FS_MAGIC);
289 		return (1);
290 	}
291 	if (es->e2fs_rev > E2FS_REV0) {
292 		mask = es->e2fs_features_incompat & ~(EXT2F_INCOMPAT_SUPP |
293 		    EXT4F_RO_INCOMPAT_SUPP);
294 		if (mask) {
295 			printf("WARNING: mount of %s denied due to "
296 			    "unsupported optional features:\n", devtoname(dev));
297 			for (i = 0;
298 			    i < sizeof(incompat)/sizeof(struct ext2_feature);
299 			    i++)
300 				if (mask & incompat[i].mask)
301 					printf("%s ", incompat[i].name);
302 			printf("\n");
303 			return (1);
304 		}
305 		mask = es->e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP;
306 		if (!ronly && mask) {
307 			printf("WARNING: R/W mount of %s denied due to "
308 			    "unsupported optional features:\n", devtoname(dev));
309 			for (i = 0;
310 			    i < sizeof(ro_compat)/sizeof(struct ext2_feature);
311 			    i++)
312 				if (mask & ro_compat[i].mask)
313 					printf("%s ", ro_compat[i].name);
314 			printf("\n");
315 			return (1);
316 		}
317 	}
318 	return (0);
319 }
320 
321 /*
322  * This computes the fields of the m_ext2fs structure from the
323  * data in the ext2fs structure read in.
324  */
325 static int
326 compute_sb_data(struct vnode *devvp, struct ext2fs *es,
327     struct m_ext2fs *fs)
328 {
329 	int g_count = 0, error;
330 	int i, j;
331 	int logic_sb_block = 1;	/* XXX for now */
332 	struct buf *bp;
333 	uint32_t e2fs_descpb, e2fs_gdbcount_alloc;
334 
335 	fs->e2fs_bcount = es->e2fs_bcount;
336 	fs->e2fs_rbcount = es->e2fs_rbcount;
337 	fs->e2fs_fbcount = es->e2fs_fbcount;
338 	if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
339 		fs->e2fs_bcount |= (uint64_t)(es->e4fs_bcount_hi) << 32;
340 		fs->e2fs_rbcount |= (uint64_t)(es->e4fs_rbcount_hi) << 32;
341 		fs->e2fs_fbcount |= (uint64_t)(es->e4fs_fbcount_hi) << 32;
342 	}
343 	fs->e2fs_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->e2fs_log_bsize;
344 	fs->e2fs_bsize = 1U << fs->e2fs_bshift;
345 	fs->e2fs_fsbtodb = es->e2fs_log_bsize + 1;
346 	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
347 	fs->e2fs_fsize = EXT2_MIN_FRAG_SIZE << es->e2fs_log_fsize;
348 	if (fs->e2fs_fsize)
349 		fs->e2fs_fpb = fs->e2fs_bsize / fs->e2fs_fsize;
350 	fs->e2fs_bpg = es->e2fs_bpg;
351 	fs->e2fs_fpg = es->e2fs_fpg;
352 	fs->e2fs_ipg = es->e2fs_ipg;
353 	if (es->e2fs_rev == E2FS_REV0) {
354 		fs->e2fs_isize = E2FS_REV0_INODE_SIZE;
355 	} else {
356 		fs->e2fs_isize = es->e2fs_inode_size;
357 
358 		/*
359 		 * Simple sanity check for superblock inode size value.
360 		 */
361 		if (EXT2_INODE_SIZE(fs) < E2FS_REV0_INODE_SIZE ||
362 		    EXT2_INODE_SIZE(fs) > fs->e2fs_bsize ||
363 		    (fs->e2fs_isize & (fs->e2fs_isize - 1)) != 0) {
364 			printf("ext2fs: invalid inode size %d\n",
365 			    fs->e2fs_isize);
366 			return (EIO);
367 		}
368 	}
369 	/* Check for extra isize in big inodes. */
370 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_EXTRA_ISIZE) &&
371 	    EXT2_INODE_SIZE(fs) < sizeof(struct ext2fs_dinode)) {
372 		printf("ext2fs: no space for extra inode timestamps\n");
373 		return (EINVAL);
374 	}
375 	/* Check for group descriptor size */
376 	if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT) &&
377 	    (es->e3fs_desc_size != sizeof(struct ext2_gd))) {
378 		printf("ext2fs: group descriptor size unsupported %d\n",
379 		    es->e3fs_desc_size);
380 		return (EINVAL);
381 	}
382 
383 	fs->e2fs_ipb = fs->e2fs_bsize / EXT2_INODE_SIZE(fs);
384 	fs->e2fs_itpg = fs->e2fs_ipg / fs->e2fs_ipb;
385 	/* s_resuid / s_resgid ? */
386 	fs->e2fs_gcount = howmany(fs->e2fs_bcount - es->e2fs_first_dblock,
387 	    EXT2_BLOCKS_PER_GROUP(fs));
388 	if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
389 		e2fs_descpb = fs->e2fs_bsize / sizeof(struct ext2_gd);
390 		e2fs_gdbcount_alloc = howmany(fs->e2fs_gcount, e2fs_descpb);
391 	} else {
392 		e2fs_descpb = fs->e2fs_bsize / E2FS_REV0_GD_SIZE;
393 		e2fs_gdbcount_alloc = howmany(fs->e2fs_gcount,
394 		    fs->e2fs_bsize / sizeof(struct ext2_gd));
395 	}
396 	fs->e2fs_gdbcount = howmany(fs->e2fs_gcount, e2fs_descpb);
397 	fs->e2fs_gd = malloc(e2fs_gdbcount_alloc * fs->e2fs_bsize,
398 	    M_EXT2MNT, M_WAITOK | M_ZERO);
399 	fs->e2fs_contigdirs = malloc(fs->e2fs_gcount *
400 	    sizeof(*fs->e2fs_contigdirs), M_EXT2MNT, M_WAITOK | M_ZERO);
401 
402 	/*
403 	 * Adjust logic_sb_block.
404 	 * Godmar thinks: if the blocksize is greater than 1024, then
405 	 * the superblock is logically part of block zero.
406 	 */
407 	if (fs->e2fs_bsize > SBSIZE)
408 		logic_sb_block = 0;
409 	for (i = 0; i < fs->e2fs_gdbcount; i++) {
410 		error = bread(devvp,
411 		    fsbtodb(fs, logic_sb_block + i + 1),
412 		    fs->e2fs_bsize, NOCRED, &bp);
413 		if (error) {
414 			free(fs->e2fs_contigdirs, M_EXT2MNT);
415 			free(fs->e2fs_gd, M_EXT2MNT);
416 			brelse(bp);
417 			return (error);
418 		}
419 		if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
420 			memcpy(&fs->e2fs_gd[
421 			    i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
422 			    bp->b_data, fs->e2fs_bsize);
423 		} else {
424 			for (j = 0; j < e2fs_descpb &&
425 			    g_count < fs->e2fs_gcount; j++, g_count++)
426 				memcpy(&fs->e2fs_gd[g_count],
427 				    bp->b_data + j * E2FS_REV0_GD_SIZE,
428 				    E2FS_REV0_GD_SIZE);
429 		}
430 		brelse(bp);
431 		bp = NULL;
432 	}
433 	/* Verify cg csum */
434 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM)) {
435 		error = ext2_gd_csum_verify(fs, devvp->v_rdev);
436 		if (error)
437 			return (error);
438 	}
439 	/* Initialization for the ext2 Orlov allocator variant. */
440 	fs->e2fs_total_dir = 0;
441 	for (i = 0; i < fs->e2fs_gcount; i++)
442 		fs->e2fs_total_dir += fs->e2fs_gd[i].ext2bgd_ndirs;
443 
444 	if (es->e2fs_rev == E2FS_REV0 ||
445 	    !EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_LARGEFILE))
446 		fs->e2fs_maxfilesize = 0x7fffffff;
447 	else {
448 		fs->e2fs_maxfilesize = 0xffffffffffff;
449 		if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_HUGE_FILE))
450 			fs->e2fs_maxfilesize = 0x7fffffffffffffff;
451 	}
452 	if (es->e4fs_flags & E2FS_UNSIGNED_HASH) {
453 		fs->e2fs_uhash = 3;
454 	} else if ((es->e4fs_flags & E2FS_SIGNED_HASH) == 0) {
455 #ifdef __CHAR_UNSIGNED__
456 		es->e4fs_flags |= E2FS_UNSIGNED_HASH;
457 		fs->e2fs_uhash = 3;
458 #else
459 		es->e4fs_flags |= E2FS_SIGNED_HASH;
460 #endif
461 	}
462 
463 	return (0);
464 }
465 
466 /*
467  * Reload all incore data for a filesystem (used after running fsck on
468  * the root filesystem and finding things to fix). The filesystem must
469  * be mounted read-only.
470  *
471  * Things to do to update the mount:
472  *	1) invalidate all cached meta-data.
473  *	2) re-read superblock from disk.
474  *	3) invalidate all cluster summary information.
475  *	4) invalidate all inactive vnodes.
476  *	5) invalidate all cached file data.
477  *	6) re-read inode data for all active vnodes.
478  * XXX we are missing some steps, in particular # 3, this has to be reviewed.
479  */
480 static int
481 ext2_reload(struct mount *mp, struct thread *td)
482 {
483 	struct vnode *vp, *mvp, *devvp;
484 	struct inode *ip;
485 	struct buf *bp;
486 	struct ext2fs *es;
487 	struct m_ext2fs *fs;
488 	struct csum *sump;
489 	int error, i;
490 	int32_t *lp;
491 
492 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
493 		return (EINVAL);
494 	/*
495 	 * Step 1: invalidate all cached meta-data.
496 	 */
497 	devvp = VFSTOEXT2(mp)->um_devvp;
498 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
499 	if (vinvalbuf(devvp, 0, 0, 0) != 0)
500 		panic("ext2_reload: dirty1");
501 	VOP_UNLOCK(devvp, 0);
502 
503 	/*
504 	 * Step 2: re-read superblock from disk.
505 	 * constants have been adjusted for ext2
506 	 */
507 	if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
508 		return (error);
509 	es = (struct ext2fs *)bp->b_data;
510 	if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) {
511 		brelse(bp);
512 		return (EIO);		/* XXX needs translation */
513 	}
514 	fs = VFSTOEXT2(mp)->um_e2fs;
515 	bcopy(bp->b_data, fs->e2fs, sizeof(struct ext2fs));
516 
517 	if ((error = compute_sb_data(devvp, es, fs)) != 0) {
518 		brelse(bp);
519 		return (error);
520 	}
521 #ifdef UNKLAR
522 	if (fs->fs_sbsize < SBSIZE)
523 		bp->b_flags |= B_INVAL;
524 #endif
525 	brelse(bp);
526 
527 	/*
528 	 * Step 3: invalidate all cluster summary information.
529 	 */
530 	if (fs->e2fs_contigsumsize > 0) {
531 		lp = fs->e2fs_maxcluster;
532 		sump = fs->e2fs_clustersum;
533 		for (i = 0; i < fs->e2fs_gcount; i++, sump++) {
534 			*lp++ = fs->e2fs_contigsumsize;
535 			sump->cs_init = 0;
536 			bzero(sump->cs_sum, fs->e2fs_contigsumsize + 1);
537 		}
538 	}
539 
540 loop:
541 	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
542 		/*
543 		 * Step 4: invalidate all cached file data.
544 		 */
545 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
546 			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
547 			goto loop;
548 		}
549 		if (vinvalbuf(vp, 0, 0, 0))
550 			panic("ext2_reload: dirty2");
551 
552 		/*
553 		 * Step 5: re-read inode data for all active vnodes.
554 		 */
555 		ip = VTOI(vp);
556 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
557 		    (int)fs->e2fs_bsize, NOCRED, &bp);
558 		if (error) {
559 			VOP_UNLOCK(vp, 0);
560 			vrele(vp);
561 			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
562 			return (error);
563 		}
564 		ext2_ei2i((struct ext2fs_dinode *)((char *)bp->b_data +
565 		    EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ip->i_number)), ip);
566 		brelse(bp);
567 		VOP_UNLOCK(vp, 0);
568 		vrele(vp);
569 	}
570 	return (0);
571 }
572 
573 /*
574  * Common code for mount and mountroot.
575  */
576 static int
577 ext2_mountfs(struct vnode *devvp, struct mount *mp)
578 {
579 	struct ext2mount *ump;
580 	struct buf *bp;
581 	struct m_ext2fs *fs;
582 	struct ext2fs *es;
583 	struct cdev *dev = devvp->v_rdev;
584 	struct g_consumer *cp;
585 	struct bufobj *bo;
586 	struct csum *sump;
587 	int error;
588 	int ronly;
589 	int i;
590 	u_long size;
591 	int32_t *lp;
592 	int32_t e2fs_maxcontig;
593 
594 	ronly = vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0);
595 	/* XXX: use VOP_ACESS to check FS perms */
596 	g_topology_lock();
597 	error = g_vfs_open(devvp, &cp, "ext2fs", ronly ? 0 : 1);
598 	g_topology_unlock();
599 	VOP_UNLOCK(devvp, 0);
600 	if (error)
601 		return (error);
602 
603 	/* XXX: should we check for some sectorsize or 512 instead? */
604 	if (((SBSIZE % cp->provider->sectorsize) != 0) ||
605 	    (SBSIZE < cp->provider->sectorsize)) {
606 		g_topology_lock();
607 		g_vfs_close(cp);
608 		g_topology_unlock();
609 		return (EINVAL);
610 	}
611 
612 	bo = &devvp->v_bufobj;
613 	bo->bo_private = cp;
614 	bo->bo_ops = g_vfs_bufops;
615 	if (devvp->v_rdev->si_iosize_max != 0)
616 		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
617 	if (mp->mnt_iosize_max > MAXPHYS)
618 		mp->mnt_iosize_max = MAXPHYS;
619 
620 	bp = NULL;
621 	ump = NULL;
622 	if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
623 		goto out;
624 	es = (struct ext2fs *)bp->b_data;
625 	if (ext2_check_sb_compat(es, dev, ronly) != 0) {
626 		error = EINVAL;		/* XXX needs translation */
627 		goto out;
628 	}
629 	if ((es->e2fs_state & E2FS_ISCLEAN) == 0 ||
630 	    (es->e2fs_state & E2FS_ERRORS)) {
631 		if (ronly || (mp->mnt_flag & MNT_FORCE)) {
632 			printf(
633 "WARNING: Filesystem was not properly dismounted\n");
634 		} else {
635 			printf(
636 "WARNING: R/W mount denied.  Filesystem is not clean - run fsck\n");
637 			error = EPERM;
638 			goto out;
639 		}
640 	}
641 	ump = malloc(sizeof(*ump), M_EXT2MNT, M_WAITOK | M_ZERO);
642 
643 	/*
644 	 * I don't know whether this is the right strategy. Note that
645 	 * we dynamically allocate both an m_ext2fs and an ext2fs
646 	 * while Linux keeps the super block in a locked buffer.
647 	 */
648 	ump->um_e2fs = malloc(sizeof(struct m_ext2fs),
649 	    M_EXT2MNT, M_WAITOK | M_ZERO);
650 	ump->um_e2fs->e2fs = malloc(sizeof(struct ext2fs),
651 	    M_EXT2MNT, M_WAITOK);
652 	mtx_init(EXT2_MTX(ump), "EXT2FS", "EXT2FS Lock", MTX_DEF);
653 	bcopy(es, ump->um_e2fs->e2fs, (u_int)sizeof(struct ext2fs));
654 	if ((error = compute_sb_data(devvp, ump->um_e2fs->e2fs, ump->um_e2fs)))
655 		goto out;
656 
657 	/*
658 	 * Calculate the maximum contiguous blocks and size of cluster summary
659 	 * array.  In FFS this is done by newfs; however, the superblock
660 	 * in ext2fs doesn't have these variables, so we can calculate
661 	 * them here.
662 	 */
663 	e2fs_maxcontig = MAX(1, MAXPHYS / ump->um_e2fs->e2fs_bsize);
664 	ump->um_e2fs->e2fs_contigsumsize = MIN(e2fs_maxcontig, EXT2_MAXCONTIG);
665 	if (ump->um_e2fs->e2fs_contigsumsize > 0) {
666 		size = ump->um_e2fs->e2fs_gcount * sizeof(int32_t);
667 		ump->um_e2fs->e2fs_maxcluster = malloc(size, M_EXT2MNT, M_WAITOK);
668 		size = ump->um_e2fs->e2fs_gcount * sizeof(struct csum);
669 		ump->um_e2fs->e2fs_clustersum = malloc(size, M_EXT2MNT, M_WAITOK);
670 		lp = ump->um_e2fs->e2fs_maxcluster;
671 		sump = ump->um_e2fs->e2fs_clustersum;
672 		for (i = 0; i < ump->um_e2fs->e2fs_gcount; i++, sump++) {
673 			*lp++ = ump->um_e2fs->e2fs_contigsumsize;
674 			sump->cs_init = 0;
675 			sump->cs_sum = malloc((ump->um_e2fs->e2fs_contigsumsize + 1) *
676 			    sizeof(int32_t), M_EXT2MNT, M_WAITOK | M_ZERO);
677 		}
678 	}
679 
680 	brelse(bp);
681 	bp = NULL;
682 	fs = ump->um_e2fs;
683 	fs->e2fs_ronly = ronly;	/* ronly is set according to mnt_flags */
684 
685 	/*
686 	 * If the fs is not mounted read-only, make sure the super block is
687 	 * always written back on a sync().
688 	 */
689 	fs->e2fs_wasvalid = fs->e2fs->e2fs_state & E2FS_ISCLEAN ? 1 : 0;
690 	if (ronly == 0) {
691 		fs->e2fs_fmod = 1;	/* mark it modified */
692 		fs->e2fs->e2fs_state &= ~E2FS_ISCLEAN;	/* set fs invalid */
693 	}
694 	mp->mnt_data = ump;
695 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
696 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
697 	mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
698 	MNT_ILOCK(mp);
699 	mp->mnt_flag |= MNT_LOCAL;
700 	MNT_IUNLOCK(mp);
701 	ump->um_mountp = mp;
702 	ump->um_dev = dev;
703 	ump->um_devvp = devvp;
704 	ump->um_bo = &devvp->v_bufobj;
705 	ump->um_cp = cp;
706 
707 	/*
708 	 * Setting those two parameters allowed us to use
709 	 * ufs_bmap w/o changse!
710 	 */
711 	ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
712 	ump->um_bptrtodb = fs->e2fs->e2fs_log_bsize + 1;
713 	ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
714 	if (ronly == 0)
715 		ext2_sbupdate(ump, MNT_WAIT);
716 	/*
717 	 * Initialize filesystem stat information in mount struct.
718 	 */
719 	MNT_ILOCK(mp);
720 	mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
721 	    MNTK_USES_BCACHE;
722 	MNT_IUNLOCK(mp);
723 	return (0);
724 out:
725 	if (bp)
726 		brelse(bp);
727 	if (cp != NULL) {
728 		g_topology_lock();
729 		g_vfs_close(cp);
730 		g_topology_unlock();
731 	}
732 	if (ump) {
733 		mtx_destroy(EXT2_MTX(ump));
734 		free(ump->um_e2fs->e2fs_gd, M_EXT2MNT);
735 		free(ump->um_e2fs->e2fs_contigdirs, M_EXT2MNT);
736 		free(ump->um_e2fs->e2fs, M_EXT2MNT);
737 		free(ump->um_e2fs, M_EXT2MNT);
738 		free(ump, M_EXT2MNT);
739 		mp->mnt_data = NULL;
740 	}
741 	return (error);
742 }
743 
744 /*
745  * Unmount system call.
746  */
747 static int
748 ext2_unmount(struct mount *mp, int mntflags)
749 {
750 	struct ext2mount *ump;
751 	struct m_ext2fs *fs;
752 	struct csum *sump;
753 	int error, flags, i, ronly;
754 
755 	flags = 0;
756 	if (mntflags & MNT_FORCE) {
757 		if (mp->mnt_flag & MNT_ROOTFS)
758 			return (EINVAL);
759 		flags |= FORCECLOSE;
760 	}
761 	if ((error = ext2_flushfiles(mp, flags, curthread)) != 0)
762 		return (error);
763 	ump = VFSTOEXT2(mp);
764 	fs = ump->um_e2fs;
765 	ronly = fs->e2fs_ronly;
766 	if (ronly == 0 && ext2_cgupdate(ump, MNT_WAIT) == 0) {
767 		if (fs->e2fs_wasvalid)
768 			fs->e2fs->e2fs_state |= E2FS_ISCLEAN;
769 		ext2_sbupdate(ump, MNT_WAIT);
770 	}
771 
772 	g_topology_lock();
773 	g_vfs_close(ump->um_cp);
774 	g_topology_unlock();
775 	vrele(ump->um_devvp);
776 	sump = fs->e2fs_clustersum;
777 	for (i = 0; i < fs->e2fs_gcount; i++, sump++)
778 		free(sump->cs_sum, M_EXT2MNT);
779 	free(fs->e2fs_clustersum, M_EXT2MNT);
780 	free(fs->e2fs_maxcluster, M_EXT2MNT);
781 	free(fs->e2fs_gd, M_EXT2MNT);
782 	free(fs->e2fs_contigdirs, M_EXT2MNT);
783 	free(fs->e2fs, M_EXT2MNT);
784 	free(fs, M_EXT2MNT);
785 	free(ump, M_EXT2MNT);
786 	mp->mnt_data = NULL;
787 	MNT_ILOCK(mp);
788 	mp->mnt_flag &= ~MNT_LOCAL;
789 	MNT_IUNLOCK(mp);
790 	return (error);
791 }
792 
793 /*
794  * Flush out all the files in a filesystem.
795  */
796 static int
797 ext2_flushfiles(struct mount *mp, int flags, struct thread *td)
798 {
799 	int error;
800 
801 	error = vflush(mp, 0, flags, td);
802 	return (error);
803 }
804 
805 /*
806  * Get filesystem statistics.
807  */
808 int
809 ext2_statfs(struct mount *mp, struct statfs *sbp)
810 {
811 	struct ext2mount *ump;
812 	struct m_ext2fs *fs;
813 	uint32_t overhead, overhead_per_group, ngdb;
814 	int i, ngroups;
815 
816 	ump = VFSTOEXT2(mp);
817 	fs = ump->um_e2fs;
818 	if (fs->e2fs->e2fs_magic != E2FS_MAGIC)
819 		panic("ext2_statfs");
820 
821 	/*
822 	 * Compute the overhead (FS structures)
823 	 */
824 	overhead_per_group =
825 	    1 /* block bitmap */ +
826 	    1 /* inode bitmap */ +
827 	    fs->e2fs_itpg;
828 	overhead = fs->e2fs->e2fs_first_dblock +
829 	    fs->e2fs_gcount * overhead_per_group;
830 	if (fs->e2fs->e2fs_rev > E2FS_REV0 &&
831 	    fs->e2fs->e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
832 		for (i = 0, ngroups = 0; i < fs->e2fs_gcount; i++) {
833 			if (ext2_cg_has_sb(fs, i))
834 				ngroups++;
835 		}
836 	} else {
837 		ngroups = fs->e2fs_gcount;
838 	}
839 	ngdb = fs->e2fs_gdbcount;
840 	if (fs->e2fs->e2fs_rev > E2FS_REV0 &&
841 	    fs->e2fs->e2fs_features_compat & EXT2F_COMPAT_RESIZE)
842 		ngdb += fs->e2fs->e2fs_reserved_ngdb;
843 	overhead += ngroups * (1 /* superblock */ + ngdb);
844 
845 	sbp->f_bsize = EXT2_FRAG_SIZE(fs);
846 	sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
847 	sbp->f_blocks = fs->e2fs_bcount - overhead;
848 	sbp->f_bfree = fs->e2fs_fbcount;
849 	sbp->f_bavail = sbp->f_bfree - fs->e2fs_rbcount;
850 	sbp->f_files = fs->e2fs->e2fs_icount;
851 	sbp->f_ffree = fs->e2fs->e2fs_ficount;
852 	return (0);
853 }
854 
855 /*
856  * Go through the disk queues to initiate sandbagged IO;
857  * go through the inodes to write those that have been modified;
858  * initiate the writing of the super block if it has been modified.
859  *
860  * Note: we are always called with the filesystem marked `MPBUSY'.
861  */
862 static int
863 ext2_sync(struct mount *mp, int waitfor)
864 {
865 	struct vnode *mvp, *vp;
866 	struct thread *td;
867 	struct inode *ip;
868 	struct ext2mount *ump = VFSTOEXT2(mp);
869 	struct m_ext2fs *fs;
870 	int error, allerror = 0;
871 
872 	td = curthread;
873 	fs = ump->um_e2fs;
874 	if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) {		/* XXX */
875 		printf("fs = %s\n", fs->e2fs_fsmnt);
876 		panic("ext2_sync: rofs mod");
877 	}
878 
879 	/*
880 	 * Write back each (modified) inode.
881 	 */
882 loop:
883 	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
884 		if (vp->v_type == VNON) {
885 			VI_UNLOCK(vp);
886 			continue;
887 		}
888 		ip = VTOI(vp);
889 		if ((ip->i_flag &
890 		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
891 		    (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
892 		    waitfor == MNT_LAZY)) {
893 			VI_UNLOCK(vp);
894 			continue;
895 		}
896 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
897 		if (error) {
898 			if (error == ENOENT) {
899 				MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
900 				goto loop;
901 			}
902 			continue;
903 		}
904 		if ((error = VOP_FSYNC(vp, waitfor, td)) != 0)
905 			allerror = error;
906 		VOP_UNLOCK(vp, 0);
907 		vrele(vp);
908 	}
909 
910 	/*
911 	 * Force stale filesystem control information to be flushed.
912 	 */
913 	if (waitfor != MNT_LAZY) {
914 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
915 		if ((error = VOP_FSYNC(ump->um_devvp, waitfor, td)) != 0)
916 			allerror = error;
917 		VOP_UNLOCK(ump->um_devvp, 0);
918 	}
919 
920 	/*
921 	 * Write back modified superblock.
922 	 */
923 	if (fs->e2fs_fmod != 0) {
924 		fs->e2fs_fmod = 0;
925 		fs->e2fs->e2fs_wtime = time_second;
926 		if ((error = ext2_cgupdate(ump, waitfor)) != 0)
927 			allerror = error;
928 	}
929 	return (allerror);
930 }
931 
932 /*
933  * Look up an EXT2FS dinode number to find its incore vnode, otherwise read it
934  * in from disk.  If it is in core, wait for the lock bit to clear, then
935  * return the inode locked.  Detection and handling of mount points must be
936  * done by the calling routine.
937  */
938 static int
939 ext2_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
940 {
941 	struct m_ext2fs *fs;
942 	struct inode *ip;
943 	struct ext2mount *ump;
944 	struct buf *bp;
945 	struct vnode *vp;
946 	struct thread *td;
947 	int i, error;
948 	int used_blocks;
949 
950 	td = curthread;
951 	error = vfs_hash_get(mp, ino, flags, td, vpp, NULL, NULL);
952 	if (error || *vpp != NULL)
953 		return (error);
954 
955 	ump = VFSTOEXT2(mp);
956 	ip = malloc(sizeof(struct inode), M_EXT2NODE, M_WAITOK | M_ZERO);
957 
958 	/* Allocate a new vnode/inode. */
959 	if ((error = getnewvnode("ext2fs", mp, &ext2_vnodeops, &vp)) != 0) {
960 		*vpp = NULL;
961 		free(ip, M_EXT2NODE);
962 		return (error);
963 	}
964 	vp->v_data = ip;
965 	ip->i_vnode = vp;
966 	ip->i_e2fs = fs = ump->um_e2fs;
967 	ip->i_ump = ump;
968 	ip->i_number = ino;
969 
970 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
971 	error = insmntque(vp, mp);
972 	if (error != 0) {
973 		free(ip, M_EXT2NODE);
974 		*vpp = NULL;
975 		return (error);
976 	}
977 	error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
978 	if (error || *vpp != NULL)
979 		return (error);
980 
981 	/* Read in the disk contents for the inode, copy into the inode. */
982 	if ((error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
983 	    (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) {
984 		/*
985 		 * The inode does not contain anything useful, so it would
986 		 * be misleading to leave it on its hash chain. With mode
987 		 * still zero, it will be unlinked and returned to the free
988 		 * list by vput().
989 		 */
990 		brelse(bp);
991 		vput(vp);
992 		*vpp = NULL;
993 		return (error);
994 	}
995 	/* convert ext2 inode to dinode */
996 	ext2_ei2i((struct ext2fs_dinode *)((char *)bp->b_data + EXT2_INODE_SIZE(fs) *
997 	    ino_to_fsbo(fs, ino)), ip);
998 	ip->i_block_group = ino_to_cg(fs, ino);
999 	ip->i_next_alloc_block = 0;
1000 	ip->i_next_alloc_goal = 0;
1001 
1002 	/*
1003 	 * Now we want to make sure that block pointers for unused
1004 	 * blocks are zeroed out - ext2_balloc depends on this
1005 	 * although for regular files and directories only
1006 	 *
1007 	 * If IN_E4EXTENTS is enabled, unused blocks are not zeroed
1008 	 * out because we could corrupt the extent tree.
1009 	 */
1010 	if (!(ip->i_flag & IN_E4EXTENTS) &&
1011 	    (S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode))) {
1012 		used_blocks = howmany(ip->i_size, fs->e2fs_bsize);
1013 		for (i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
1014 			ip->i_db[i] = 0;
1015 	}
1016 #ifdef EXT2FS_DEBUG
1017 	ext2_print_inode(ip);
1018 	ext4_ext_print_extent_tree_status(ip);
1019 #endif
1020 	bqrelse(bp);
1021 
1022 	/*
1023 	 * Initialize the vnode from the inode, check for aliases.
1024 	 * Note that the underlying vnode may have changed.
1025 	 */
1026 	if ((error = ext2_vinit(mp, &ext2_fifoops, &vp)) != 0) {
1027 		vput(vp);
1028 		*vpp = NULL;
1029 		return (error);
1030 	}
1031 
1032 	/*
1033 	 * Finish inode initialization.
1034 	 */
1035 
1036 	*vpp = vp;
1037 	return (0);
1038 }
1039 
1040 /*
1041  * File handle to vnode
1042  *
1043  * Have to be really careful about stale file handles:
1044  * - check that the inode number is valid
1045  * - call ext2_vget() to get the locked inode
1046  * - check for an unallocated inode (i_mode == 0)
1047  * - check that the given client host has export rights and return
1048  *   those rights via. exflagsp and credanonp
1049  */
1050 static int
1051 ext2_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
1052 {
1053 	struct inode *ip;
1054 	struct ufid *ufhp;
1055 	struct vnode *nvp;
1056 	struct m_ext2fs *fs;
1057 	int error;
1058 
1059 	ufhp = (struct ufid *)fhp;
1060 	fs = VFSTOEXT2(mp)->um_e2fs;
1061 	if (ufhp->ufid_ino < EXT2_ROOTINO ||
1062 	    ufhp->ufid_ino > fs->e2fs_gcount * fs->e2fs->e2fs_ipg)
1063 		return (ESTALE);
1064 
1065 	error = VFS_VGET(mp, ufhp->ufid_ino, LK_EXCLUSIVE, &nvp);
1066 	if (error) {
1067 		*vpp = NULLVP;
1068 		return (error);
1069 	}
1070 	ip = VTOI(nvp);
1071 	if (ip->i_mode == 0 ||
1072 	    ip->i_gen != ufhp->ufid_gen || ip->i_nlink <= 0) {
1073 		vput(nvp);
1074 		*vpp = NULLVP;
1075 		return (ESTALE);
1076 	}
1077 	*vpp = nvp;
1078 	vnode_create_vobject(*vpp, 0, curthread);
1079 	return (0);
1080 }
1081 
1082 /*
1083  * Write a superblock and associated information back to disk.
1084  */
1085 static int
1086 ext2_sbupdate(struct ext2mount *mp, int waitfor)
1087 {
1088 	struct m_ext2fs *fs = mp->um_e2fs;
1089 	struct ext2fs *es = fs->e2fs;
1090 	struct buf *bp;
1091 	int error = 0;
1092 
1093 	es->e2fs_bcount = fs->e2fs_bcount & 0xffffffff;
1094 	es->e2fs_rbcount = fs->e2fs_rbcount & 0xffffffff;
1095 	es->e2fs_fbcount = fs->e2fs_fbcount & 0xffffffff;
1096 	if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
1097 		es->e4fs_bcount_hi = fs->e2fs_bcount >> 32;
1098 		es->e4fs_rbcount_hi = fs->e2fs_rbcount >> 32;
1099 		es->e4fs_fbcount_hi = fs->e2fs_fbcount >> 32;
1100 	}
1101 
1102 	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0, 0);
1103 	bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2fs));
1104 	if (waitfor == MNT_WAIT)
1105 		error = bwrite(bp);
1106 	else
1107 		bawrite(bp);
1108 
1109 	/*
1110 	 * The buffers for group descriptors, inode bitmaps and block bitmaps
1111 	 * are not busy at this point and are (hopefully) written by the
1112 	 * usual sync mechanism. No need to write them here.
1113 	 */
1114 	return (error);
1115 }
1116 int
1117 ext2_cgupdate(struct ext2mount *mp, int waitfor)
1118 {
1119 	struct m_ext2fs *fs = mp->um_e2fs;
1120 	struct buf *bp;
1121 	int i, j, g_count = 0, error = 0, allerror = 0;
1122 
1123 	allerror = ext2_sbupdate(mp, waitfor);
1124 
1125 	/* Update gd csums */
1126 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM))
1127 		ext2_gd_csum_set(fs);
1128 
1129 	for (i = 0; i < fs->e2fs_gdbcount; i++) {
1130 		bp = getblk(mp->um_devvp, fsbtodb(fs,
1131 		    fs->e2fs->e2fs_first_dblock +
1132 		    1 /* superblock */ + i), fs->e2fs_bsize, 0, 0, 0);
1133 		if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
1134 			memcpy(bp->b_data, &fs->e2fs_gd[
1135 			    i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
1136 			    fs->e2fs_bsize);
1137 		} else {
1138 			for (j = 0; j < fs->e2fs_bsize / E2FS_REV0_GD_SIZE &&
1139 			    g_count < fs->e2fs_gcount; j++, g_count++)
1140 				memcpy(bp->b_data + j * E2FS_REV0_GD_SIZE,
1141 				    &fs->e2fs_gd[g_count], E2FS_REV0_GD_SIZE);
1142 		}
1143 		if (waitfor == MNT_WAIT)
1144 			error = bwrite(bp);
1145 		else
1146 			bawrite(bp);
1147 	}
1148 
1149 	if (!allerror && error)
1150 		allerror = error;
1151 	return (allerror);
1152 }
1153 
1154 /*
1155  * Return the root of a filesystem.
1156  */
1157 static int
1158 ext2_root(struct mount *mp, int flags, struct vnode **vpp)
1159 {
1160 	struct vnode *nvp;
1161 	int error;
1162 
1163 	error = VFS_VGET(mp, EXT2_ROOTINO, LK_EXCLUSIVE, &nvp);
1164 	if (error)
1165 		return (error);
1166 	*vpp = nvp;
1167 	return (0);
1168 }
1169