xref: /freebsd/sys/fs/ext2fs/ext2_vfsops.c (revision 2e43efd0bb1e9cd780f02fa5b888f9264e66e37b)
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/fs.h>
62 #include <fs/ext2fs/ext2_mount.h>
63 #include <fs/ext2fs/inode.h>
64 
65 #include <fs/ext2fs/ext2fs.h>
66 #include <fs/ext2fs/ext2_dinode.h>
67 #include <fs/ext2fs/ext2_extern.h>
68 #include <fs/ext2fs/ext2_extents.h>
69 
70 
71 static int	ext2_flushfiles(struct mount *mp, int flags, struct thread *td);
72 static int	ext2_mountfs(struct vnode *, struct mount *);
73 static int	ext2_reload(struct mount *mp, struct thread *td);
74 static int	ext2_sbupdate(struct ext2mount *, int);
75 static int	ext2_cgupdate(struct ext2mount *, int);
76 static vfs_unmount_t		ext2_unmount;
77 static vfs_root_t		ext2_root;
78 static vfs_statfs_t		ext2_statfs;
79 static vfs_sync_t		ext2_sync;
80 static vfs_vget_t		ext2_vget;
81 static vfs_fhtovp_t		ext2_fhtovp;
82 static vfs_mount_t		ext2_mount;
83 
84 MALLOC_DEFINE(M_EXT2NODE, "ext2_node", "EXT2 vnode private part");
85 static MALLOC_DEFINE(M_EXT2MNT, "ext2_mount", "EXT2 mount structure");
86 
87 static struct vfsops ext2fs_vfsops = {
88 	.vfs_fhtovp =		ext2_fhtovp,
89 	.vfs_mount =		ext2_mount,
90 	.vfs_root =		ext2_root,	/* root inode via vget */
91 	.vfs_statfs =		ext2_statfs,
92 	.vfs_sync =		ext2_sync,
93 	.vfs_unmount =		ext2_unmount,
94 	.vfs_vget =		ext2_vget,
95 };
96 
97 VFS_SET(ext2fs_vfsops, ext2fs, 0);
98 
99 static int	ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev,
100 		    int ronly);
101 static int	ext2_compute_sb_data(struct vnode * devvp,
102 		    struct ext2fs * es, struct m_ext2fs * fs);
103 
104 static const char *ext2_opts[] = { "acls", "async", "noatime", "noclusterr",
105     "noclusterw", "noexec", "export", "force", "from", "multilabel",
106     "suiddir", "nosymfollow", "sync", "union", NULL };
107 
108 /*
109  * VFS Operations.
110  *
111  * mount system call
112  */
113 static int
114 ext2_mount(struct mount *mp)
115 {
116 	struct vfsoptlist *opts;
117 	struct vnode *devvp;
118 	struct thread *td;
119 	struct ext2mount *ump = NULL;
120 	struct m_ext2fs *fs;
121 	struct nameidata nd, *ndp = &nd;
122 	accmode_t accmode;
123 	char *path, *fspec;
124 	int error, flags, len;
125 
126 	td = curthread;
127 	opts = mp->mnt_optnew;
128 
129 	if (vfs_filteropt(opts, ext2_opts))
130 		return (EINVAL);
131 
132 	vfs_getopt(opts, "fspath", (void **)&path, NULL);
133 	/* Double-check the length of path.. */
134 	if (strlen(path) >= MAXMNTLEN)
135 		return (ENAMETOOLONG);
136 
137 	fspec = NULL;
138 	error = vfs_getopt(opts, "from", (void **)&fspec, &len);
139 	if (!error && fspec[len - 1] != '\0')
140 		return (EINVAL);
141 
142 	/*
143 	 * If updating, check whether changing from read-only to
144 	 * read/write; if there is no device name, that's all we do.
145 	 */
146 	if (mp->mnt_flag & MNT_UPDATE) {
147 		ump = VFSTOEXT2(mp);
148 		fs = ump->um_e2fs;
149 		error = 0;
150 		if (fs->e2fs_ronly == 0 &&
151 		    vfs_flagopt(opts, "ro", NULL, 0)) {
152 			error = VFS_SYNC(mp, MNT_WAIT);
153 			if (error)
154 				return (error);
155 			flags = WRITECLOSE;
156 			if (mp->mnt_flag & MNT_FORCE)
157 				flags |= FORCECLOSE;
158 			error = ext2_flushfiles(mp, flags, td);
159 			if (error == 0 && fs->e2fs_wasvalid &&
160 			    ext2_cgupdate(ump, MNT_WAIT) == 0) {
161 				fs->e2fs->e2fs_state |= E2FS_ISCLEAN;
162 				ext2_sbupdate(ump, MNT_WAIT);
163 			}
164 			fs->e2fs_ronly = 1;
165 			vfs_flagopt(opts, "ro", &mp->mnt_flag, MNT_RDONLY);
166 			g_topology_lock();
167 			g_access(ump->um_cp, 0, -1, 0);
168 			g_topology_unlock();
169 		}
170 		if (!error && (mp->mnt_flag & MNT_RELOAD))
171 			error = ext2_reload(mp, td);
172 		if (error)
173 			return (error);
174 		devvp = ump->um_devvp;
175 		if (fs->e2fs_ronly && !vfs_flagopt(opts, "ro", NULL, 0)) {
176 			if (ext2_check_sb_compat(fs->e2fs, devvp->v_rdev, 0))
177 				return (EPERM);
178 
179 			/*
180 			 * If upgrade to read-write by non-root, then verify
181 			 * that user has necessary permissions on the device.
182 			 */
183 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
184 			error = VOP_ACCESS(devvp, VREAD | VWRITE,
185 			    td->td_ucred, td);
186 			if (error)
187 				error = priv_check(td, PRIV_VFS_MOUNT_PERM);
188 			if (error) {
189 				VOP_UNLOCK(devvp, 0);
190 				return (error);
191 			}
192 			VOP_UNLOCK(devvp, 0);
193 			g_topology_lock();
194 			error = g_access(ump->um_cp, 0, 1, 0);
195 			g_topology_unlock();
196 			if (error)
197 				return (error);
198 
199 			if ((fs->e2fs->e2fs_state & E2FS_ISCLEAN) == 0 ||
200 			    (fs->e2fs->e2fs_state & E2FS_ERRORS)) {
201 				if (mp->mnt_flag & MNT_FORCE) {
202 					printf(
203 "WARNING: %s was not properly dismounted\n", fs->e2fs_fsmnt);
204 				} else {
205 					printf(
206 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
207 					    fs->e2fs_fsmnt);
208 					return (EPERM);
209 				}
210 			}
211 			fs->e2fs->e2fs_state &= ~E2FS_ISCLEAN;
212 			(void)ext2_cgupdate(ump, MNT_WAIT);
213 			fs->e2fs_ronly = 0;
214 			MNT_ILOCK(mp);
215 			mp->mnt_flag &= ~MNT_RDONLY;
216 			MNT_IUNLOCK(mp);
217 		}
218 		if (vfs_flagopt(opts, "export", NULL, 0)) {
219 			/* Process export requests in vfs_mount.c. */
220 			return (error);
221 		}
222 	}
223 
224 	/*
225 	 * Not an update, or updating the name: look up the name
226 	 * and verify that it refers to a sensible disk device.
227 	 */
228 	if (fspec == NULL)
229 		return (EINVAL);
230 	NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
231 	if ((error = namei(ndp)) != 0)
232 		return (error);
233 	NDFREE(ndp, NDF_ONLY_PNBUF);
234 	devvp = ndp->ni_vp;
235 
236 	if (!vn_isdisk(devvp, &error)) {
237 		vput(devvp);
238 		return (error);
239 	}
240 
241 	/*
242 	 * If mount by non-root, then verify that user has necessary
243 	 * permissions on the device.
244 	 *
245 	 * XXXRW: VOP_ACCESS() enough?
246 	 */
247 	accmode = VREAD;
248 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
249 		accmode |= VWRITE;
250 	error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
251 	if (error)
252 		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
253 	if (error) {
254 		vput(devvp);
255 		return (error);
256 	}
257 
258 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
259 		error = ext2_mountfs(devvp, mp);
260 	} else {
261 		if (devvp != ump->um_devvp) {
262 			vput(devvp);
263 			return (EINVAL);	/* needs translation */
264 		} else
265 			vput(devvp);
266 	}
267 	if (error) {
268 		vrele(devvp);
269 		return (error);
270 	}
271 	ump = VFSTOEXT2(mp);
272 	fs = ump->um_e2fs;
273 
274 	/*
275 	 * Note that this strncpy() is ok because of a check at the start
276 	 * of ext2_mount().
277 	 */
278 	strncpy(fs->e2fs_fsmnt, path, MAXMNTLEN);
279 	fs->e2fs_fsmnt[MAXMNTLEN - 1] = '\0';
280 	vfs_mountedfrom(mp, fspec);
281 	return (0);
282 }
283 
284 static int
285 ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev, int ronly)
286 {
287 	uint32_t i, mask;
288 
289 	if (es->e2fs_magic != E2FS_MAGIC) {
290 		printf("ext2fs: %s: wrong magic number %#x (expected %#x)\n",
291 		    devtoname(dev), es->e2fs_magic, E2FS_MAGIC);
292 		return (1);
293 	}
294 	if (es->e2fs_rev > E2FS_REV0) {
295 		mask = es->e2fs_features_incompat & ~(EXT2F_INCOMPAT_SUPP);
296 		if (mask) {
297 			printf("WARNING: mount of %s denied due to "
298 			    "unsupported optional features:\n", devtoname(dev));
299 			for (i = 0;
300 			    i < sizeof(incompat)/sizeof(struct ext2_feature);
301 			    i++)
302 				if (mask & incompat[i].mask)
303 					printf("%s ", incompat[i].name);
304 			printf("\n");
305 			return (1);
306 		}
307 		mask = es->e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP;
308 		if (!ronly && mask) {
309 			printf("WARNING: R/W mount of %s denied due to "
310 			    "unsupported optional features:\n", devtoname(dev));
311 			for (i = 0;
312 			    i < sizeof(ro_compat)/sizeof(struct ext2_feature);
313 			    i++)
314 				if (mask & ro_compat[i].mask)
315 					printf("%s ", ro_compat[i].name);
316 			printf("\n");
317 			return (1);
318 		}
319 	}
320 	return (0);
321 }
322 
323 static e4fs_daddr_t
324 ext2_cg_location(struct m_ext2fs *fs, int number)
325 {
326 	int cg, descpb, logical_sb, has_super = 0;
327 
328 	/*
329 	 * Adjust logical superblock block number.
330 	 * Godmar thinks: if the blocksize is greater than 1024, then
331 	 * the superblock is logically part of block zero.
332 	 */
333 	logical_sb = fs->e2fs_bsize > SBSIZE ? 0 : 1;
334 
335 	if (!EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_META_BG) ||
336 	    number < fs->e2fs->e3fs_first_meta_bg)
337 		return (logical_sb + number + 1);
338 
339 	if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT))
340 		descpb = fs->e2fs_bsize / sizeof(struct ext2_gd);
341 	else
342 		descpb = fs->e2fs_bsize / E2FS_REV0_GD_SIZE;
343 
344 	cg = descpb * number;
345 
346 	if (ext2_cg_has_sb(fs, cg))
347 		has_super = 1;
348 
349 	return (has_super + cg * (e4fs_daddr_t)EXT2_BLOCKS_PER_GROUP(fs) +
350 	    fs->e2fs->e2fs_first_dblock);
351 }
352 
353 static int
354 ext2_cg_validate(struct m_ext2fs *fs)
355 {
356 	uint64_t b_bitmap;
357 	uint64_t i_bitmap;
358 	uint64_t i_tables;
359 	uint64_t first_block, last_block, last_cg_block;
360 	struct ext2_gd *gd;
361 	unsigned int i, cg_count;
362 
363 	first_block = fs->e2fs->e2fs_first_dblock;
364 	last_cg_block = ext2_cg_number_gdb(fs, 0);
365 	cg_count = fs->e2fs_gcount;
366 
367 	for (i = 0; i < fs->e2fs_gcount; i++) {
368 		gd = &fs->e2fs_gd[i];
369 
370 		if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_FLEX_BG) ||
371 		    i == fs->e2fs_gcount - 1) {
372 			last_block = fs->e2fs_bcount - 1;
373 		} else {
374 			last_block = first_block +
375 			    (EXT2_BLOCKS_PER_GROUP(fs) - 1);
376 		}
377 
378 		if ((cg_count == fs->e2fs_gcount) &&
379 		    !(gd->ext4bgd_flags & EXT2_BG_INODE_ZEROED))
380 			cg_count = i;
381 
382 		b_bitmap = e2fs_gd_get_b_bitmap(gd);
383 		if (b_bitmap == 0) {
384 			printf("ext2fs: cg %u: block bitmap is zero\n", i);
385 			return (EINVAL);
386 
387 		}
388 		if (b_bitmap <= last_cg_block) {
389 			printf("ext2fs: cg %u: block bitmap overlaps gds\n", i);
390 			return (EINVAL);
391 		}
392 		if (b_bitmap < first_block || b_bitmap > last_block) {
393 			printf("ext2fs: cg %u: block bitmap not in group, blk=%ju\n",
394 			    i, b_bitmap);
395 			return (EINVAL);
396 		}
397 
398 		i_bitmap = e2fs_gd_get_i_bitmap(gd);
399 		if (i_bitmap == 0) {
400 			printf("ext2fs: cg %u: inode bitmap is zero\n", i);
401 			return (EINVAL);
402 		}
403 		if (i_bitmap <= last_cg_block) {
404 			printf("ext2fs: cg %u: inode bitmap overlaps gds\n", i);
405 			return (EINVAL);
406 		}
407 		if (i_bitmap < first_block || i_bitmap > last_block) {
408 			printf("ext2fs: cg %u: inode bitmap not in group blk=%ju\n",
409 			    i, i_bitmap);
410 			return (EINVAL);
411 		}
412 
413 		i_tables = e2fs_gd_get_i_tables(gd);
414 		if (i_tables == 0) {
415 			printf("ext2fs: cg %u: inode table is zero\n", i);
416 			return (EINVAL);
417 		}
418 		if (i_tables <= last_cg_block) {
419 			printf("ext2fs: cg %u: inode talbes overlaps gds\n", i);
420 			return (EINVAL);
421 		}
422 		if (i_tables < first_block ||
423 		    i_tables + fs->e2fs_itpg - 1 > last_block) {
424 			printf("ext2fs: cg %u: inode tables not in group blk=%ju\n",
425 			    i, i_tables);
426 			return (EINVAL);
427 		}
428 
429 		if (!EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_FLEX_BG))
430 			first_block += EXT2_BLOCKS_PER_GROUP(fs);
431 	}
432 
433 	return (0);
434 }
435 
436 /*
437  * This computes the fields of the m_ext2fs structure from the
438  * data in the ext2fs structure read in.
439  */
440 static int
441 ext2_compute_sb_data(struct vnode *devvp, struct ext2fs *es,
442     struct m_ext2fs *fs)
443 {
444 	struct buf *bp;
445 	uint32_t e2fs_descpb, e2fs_gdbcount_alloc;
446 	int i, j;
447 	int g_count = 0;
448 	int error;
449 
450 	/* Check checksum features */
451 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) &&
452 	    EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
453 		printf("ext2fs: incorrect checksum features combination\n");
454 		return (EINVAL);
455 	}
456 
457 	/* Precompute checksum seed for all metadata */
458 	ext2_sb_csum_set_seed(fs);
459 
460 	/* Verify sb csum if possible */
461 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
462 		error = ext2_sb_csum_verify(fs);
463 		if (error) {
464 			return (error);
465 		}
466 	}
467 
468 	/* Check for block size = 1K|2K|4K */
469 	if (es->e2fs_log_bsize > 2) {
470 		printf("ext2fs: bad block size: %d\n", es->e2fs_log_bsize);
471 		return (EINVAL);
472 	}
473 
474 	fs->e2fs_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->e2fs_log_bsize;
475 	fs->e2fs_bsize = 1U << fs->e2fs_bshift;
476 	fs->e2fs_fsbtodb = es->e2fs_log_bsize + 1;
477 	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
478 
479 	/* Check for fragment size */
480 	if (es->e2fs_log_fsize >
481 	    (EXT2_MAX_FRAG_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE)) {
482 		printf("ext2fs: invalid log cluster size: %u\n",
483 		    es->e2fs_log_fsize);
484 		return (EINVAL);
485 	}
486 
487 	fs->e2fs_fsize = EXT2_MIN_FRAG_SIZE << es->e2fs_log_fsize;
488 	if (fs->e2fs_fsize != fs->e2fs_bsize) {
489 		printf("ext2fs: fragment size (%u) != block size %u\n",
490 		    fs->e2fs_fsize, fs->e2fs_bsize);
491 		return (EINVAL);
492 	}
493 
494 	fs->e2fs_fpb = fs->e2fs_bsize / fs->e2fs_fsize;
495 
496 	/* Check reserved gdt blocks for future filesystem expansion */
497 	if (es->e2fs_reserved_ngdb > (fs->e2fs_bsize / 4)) {
498 		printf("ext2fs: number of reserved GDT blocks too large: %u\n",
499 		    es->e2fs_reserved_ngdb);
500 		return (EINVAL);
501 	}
502 
503 	if (es->e2fs_rev == E2FS_REV0) {
504 		fs->e2fs_isize = E2FS_REV0_INODE_SIZE;
505 	} else {
506 		fs->e2fs_isize = es->e2fs_inode_size;
507 
508 		/*
509 		 * Check first ino.
510 		 */
511 		if (es->e2fs_first_ino < EXT2_FIRSTINO) {
512 			printf("ext2fs: invalid first ino: %u\n",
513 			    es->e2fs_first_ino);
514 			return (EINVAL);
515 		}
516 
517 		/*
518 		 * Simple sanity check for superblock inode size value.
519 		 */
520 		if (EXT2_INODE_SIZE(fs) < E2FS_REV0_INODE_SIZE ||
521 		    EXT2_INODE_SIZE(fs) > fs->e2fs_bsize ||
522 		    (fs->e2fs_isize & (fs->e2fs_isize - 1)) != 0) {
523 			printf("ext2fs: invalid inode size %u\n",
524 			    fs->e2fs_isize);
525 			return (EINVAL);
526 		}
527 	}
528 
529 	/* Check group descriptors */
530 	if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT) &&
531 	    es->e3fs_desc_size != E2FS_64BIT_GD_SIZE) {
532 			printf("ext2fs: unsupported 64bit descriptor size %u\n",
533 			    es->e3fs_desc_size);
534 			return (EINVAL);
535 	}
536 
537 	fs->e2fs_bpg = es->e2fs_bpg;
538 	fs->e2fs_fpg = es->e2fs_fpg;
539 	if (fs->e2fs_bpg == 0 || fs->e2fs_fpg == 0) {
540 		printf("ext2fs: zero blocks/fragments per group\n");
541 		return (EINVAL);
542 	}
543 	if (fs->e2fs_bpg != fs->e2fs_bsize * 8) {
544 		printf("ext2fs: non-standard group size unsupported %d\n",
545 		    fs->e2fs_bpg);
546 		return (EINVAL);
547 	}
548 
549 	fs->e2fs_ipb = fs->e2fs_bsize / EXT2_INODE_SIZE(fs);
550 	if (fs->e2fs_ipb == 0 ||
551 	    fs->e2fs_ipb > fs->e2fs_bsize / E2FS_REV0_INODE_SIZE) {
552 		printf("ext2fs: bad inodes per block size\n");
553 		return (EINVAL);
554 	}
555 
556 	fs->e2fs_ipg = es->e2fs_ipg;
557 	if (fs->e2fs_ipg < fs->e2fs_ipb || fs->e2fs_ipg >  fs->e2fs_bsize * 8) {
558 		printf("ext2fs: invalid inodes per group: %u\n", fs->e2fs_ipb);
559 		return (EINVAL);
560 	}
561 
562 	fs->e2fs_itpg = fs->e2fs_ipg / fs->e2fs_ipb;
563 
564 	fs->e2fs_bcount = es->e2fs_bcount;
565 	fs->e2fs_rbcount = es->e2fs_rbcount;
566 	fs->e2fs_fbcount = es->e2fs_fbcount;
567 	if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
568 		fs->e2fs_bcount |= (uint64_t)(es->e4fs_bcount_hi) << 32;
569 		fs->e2fs_rbcount |= (uint64_t)(es->e4fs_rbcount_hi) << 32;
570 		fs->e2fs_fbcount |= (uint64_t)(es->e4fs_fbcount_hi) << 32;
571 	}
572 	if (fs->e2fs_rbcount > fs->e2fs_bcount ||
573 	    fs->e2fs_fbcount > fs->e2fs_bcount) {
574 		printf("ext2fs: invalid block count\n");
575 		return (EINVAL);
576 	}
577 	if (es->e2fs_first_dblock >= fs->e2fs_bcount) {
578 		printf("ext2fs: first data block out of range\n");
579 		return (EINVAL);
580 	}
581 
582 	fs->e2fs_gcount = howmany(fs->e2fs_bcount - es->e2fs_first_dblock,
583 	    EXT2_BLOCKS_PER_GROUP(fs));
584 	if (fs->e2fs_gcount > ((uint64_t)1 << 32) - EXT2_DESCS_PER_BLOCK(fs)) {
585 		printf("ext2fs: groups count too large: %u\n", fs->e2fs_gcount);
586 		return (EINVAL);
587 	}
588 
589 	/* Check for extra isize in big inodes. */
590 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_EXTRA_ISIZE) &&
591 	    EXT2_INODE_SIZE(fs) < sizeof(struct ext2fs_dinode)) {
592 		printf("ext2fs: no space for extra inode timestamps\n");
593 		return (EINVAL);
594 	}
595 
596 	/* s_resuid / s_resgid ? */
597 
598 	if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
599 		e2fs_descpb = fs->e2fs_bsize / E2FS_64BIT_GD_SIZE;
600 		e2fs_gdbcount_alloc = howmany(fs->e2fs_gcount, e2fs_descpb);
601 	} else {
602 		e2fs_descpb = fs->e2fs_bsize / E2FS_REV0_GD_SIZE;
603 		e2fs_gdbcount_alloc = howmany(fs->e2fs_gcount,
604 		    fs->e2fs_bsize / sizeof(struct ext2_gd));
605 	}
606 	fs->e2fs_gdbcount = howmany(fs->e2fs_gcount, e2fs_descpb);
607 	fs->e2fs_gd = malloc(e2fs_gdbcount_alloc * fs->e2fs_bsize,
608 	    M_EXT2MNT, M_WAITOK | M_ZERO);
609 	fs->e2fs_contigdirs = malloc(fs->e2fs_gcount *
610 	    sizeof(*fs->e2fs_contigdirs), M_EXT2MNT, M_WAITOK | M_ZERO);
611 
612 	for (i = 0; i < fs->e2fs_gdbcount; i++) {
613 		error = bread(devvp,
614 		    fsbtodb(fs, ext2_cg_location(fs, i)),
615 		    fs->e2fs_bsize, NOCRED, &bp);
616 		if (error) {
617 			/*
618 			 * fs->e2fs_gd and fs->e2fs_contigdirs
619 			 * will be freed later by the caller,
620 			 * because this function could be called from
621 			 * MNT_UPDATE path.
622 			 */
623 			brelse(bp);
624 			return (error);
625 		}
626 		if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
627 			memcpy(&fs->e2fs_gd[
628 			    i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
629 			    bp->b_data, fs->e2fs_bsize);
630 		} else {
631 			for (j = 0; j < e2fs_descpb &&
632 			    g_count < fs->e2fs_gcount; j++, g_count++)
633 				memcpy(&fs->e2fs_gd[g_count],
634 				    bp->b_data + j * E2FS_REV0_GD_SIZE,
635 				    E2FS_REV0_GD_SIZE);
636 		}
637 		brelse(bp);
638 		bp = NULL;
639 	}
640 
641 	/* Validate cgs consistency */
642 	error = ext2_cg_validate(fs);
643 	if (error)
644 		return (error);
645 
646 	/* Verfy cgs csum */
647 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) ||
648 	    EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
649 		error = ext2_gd_csum_verify(fs, devvp->v_rdev);
650 		if (error)
651 			return (error);
652 	}
653 	/* Initialization for the ext2 Orlov allocator variant. */
654 	fs->e2fs_total_dir = 0;
655 	for (i = 0; i < fs->e2fs_gcount; i++)
656 		fs->e2fs_total_dir += e2fs_gd_get_ndirs(&fs->e2fs_gd[i]);
657 
658 	if (es->e2fs_rev == E2FS_REV0 ||
659 	    !EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_LARGEFILE))
660 		fs->e2fs_maxfilesize = 0x7fffffff;
661 	else {
662 		fs->e2fs_maxfilesize = 0xffffffffffff;
663 		if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_HUGE_FILE))
664 			fs->e2fs_maxfilesize = 0x7fffffffffffffff;
665 	}
666 	if (es->e4fs_flags & E2FS_UNSIGNED_HASH) {
667 		fs->e2fs_uhash = 3;
668 	} else if ((es->e4fs_flags & E2FS_SIGNED_HASH) == 0) {
669 #ifdef __CHAR_UNSIGNED__
670 		es->e4fs_flags |= E2FS_UNSIGNED_HASH;
671 		fs->e2fs_uhash = 3;
672 #else
673 		es->e4fs_flags |= E2FS_SIGNED_HASH;
674 #endif
675 	}
676 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
677 		error = ext2_sb_csum_verify(fs);
678 
679 	return (error);
680 }
681 
682 /*
683  * Reload all incore data for a filesystem (used after running fsck on
684  * the root filesystem and finding things to fix). The filesystem must
685  * be mounted read-only.
686  *
687  * Things to do to update the mount:
688  *	1) invalidate all cached meta-data.
689  *	2) re-read superblock from disk.
690  *	3) invalidate all cluster summary information.
691  *	4) invalidate all inactive vnodes.
692  *	5) invalidate all cached file data.
693  *	6) re-read inode data for all active vnodes.
694  * XXX we are missing some steps, in particular # 3, this has to be reviewed.
695  */
696 static int
697 ext2_reload(struct mount *mp, struct thread *td)
698 {
699 	struct vnode *vp, *mvp, *devvp;
700 	struct inode *ip;
701 	struct buf *bp;
702 	struct ext2fs *es;
703 	struct m_ext2fs *fs;
704 	struct csum *sump;
705 	int error, i;
706 	int32_t *lp;
707 
708 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
709 		return (EINVAL);
710 	/*
711 	 * Step 1: invalidate all cached meta-data.
712 	 */
713 	devvp = VFSTOEXT2(mp)->um_devvp;
714 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
715 	if (vinvalbuf(devvp, 0, 0, 0) != 0)
716 		panic("ext2_reload: dirty1");
717 	VOP_UNLOCK(devvp, 0);
718 
719 	/*
720 	 * Step 2: re-read superblock from disk.
721 	 * constants have been adjusted for ext2
722 	 */
723 	if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
724 		return (error);
725 	es = (struct ext2fs *)bp->b_data;
726 	if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) {
727 		brelse(bp);
728 		return (EIO);		/* XXX needs translation */
729 	}
730 	fs = VFSTOEXT2(mp)->um_e2fs;
731 	bcopy(bp->b_data, fs->e2fs, sizeof(struct ext2fs));
732 
733 	if ((error = ext2_compute_sb_data(devvp, es, fs)) != 0) {
734 		brelse(bp);
735 		return (error);
736 	}
737 #ifdef UNKLAR
738 	if (fs->fs_sbsize < SBSIZE)
739 		bp->b_flags |= B_INVAL;
740 #endif
741 	brelse(bp);
742 
743 	/*
744 	 * Step 3: invalidate all cluster summary information.
745 	 */
746 	if (fs->e2fs_contigsumsize > 0) {
747 		lp = fs->e2fs_maxcluster;
748 		sump = fs->e2fs_clustersum;
749 		for (i = 0; i < fs->e2fs_gcount; i++, sump++) {
750 			*lp++ = fs->e2fs_contigsumsize;
751 			sump->cs_init = 0;
752 			bzero(sump->cs_sum, fs->e2fs_contigsumsize + 1);
753 		}
754 	}
755 
756 loop:
757 	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
758 		/*
759 		 * Step 4: invalidate all cached file data.
760 		 */
761 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
762 			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
763 			goto loop;
764 		}
765 		if (vinvalbuf(vp, 0, 0, 0))
766 			panic("ext2_reload: dirty2");
767 
768 		/*
769 		 * Step 5: re-read inode data for all active vnodes.
770 		 */
771 		ip = VTOI(vp);
772 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
773 		    (int)fs->e2fs_bsize, NOCRED, &bp);
774 		if (error) {
775 			VOP_UNLOCK(vp, 0);
776 			vrele(vp);
777 			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
778 			return (error);
779 		}
780 
781 		error = ext2_ei2i((struct ext2fs_dinode *)((char *)bp->b_data +
782 		    EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ip->i_number)), ip);
783 
784 		brelse(bp);
785 		VOP_UNLOCK(vp, 0);
786 		vrele(vp);
787 
788 		if (error) {
789 			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
790 			return (error);
791 		}
792 	}
793 	return (0);
794 }
795 
796 /*
797  * Common code for mount and mountroot.
798  */
799 static int
800 ext2_mountfs(struct vnode *devvp, struct mount *mp)
801 {
802 	struct ext2mount *ump;
803 	struct buf *bp;
804 	struct m_ext2fs *fs;
805 	struct ext2fs *es;
806 	struct cdev *dev = devvp->v_rdev;
807 	struct g_consumer *cp;
808 	struct bufobj *bo;
809 	struct csum *sump;
810 	int error;
811 	int ronly;
812 	int i;
813 	u_long size;
814 	int32_t *lp;
815 	int32_t e2fs_maxcontig;
816 
817 	ronly = vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0);
818 	/* XXX: use VOP_ACESS to check FS perms */
819 	g_topology_lock();
820 	error = g_vfs_open(devvp, &cp, "ext2fs", ronly ? 0 : 1);
821 	g_topology_unlock();
822 	VOP_UNLOCK(devvp, 0);
823 	if (error)
824 		return (error);
825 
826 	/* XXX: should we check for some sectorsize or 512 instead? */
827 	if (((SBSIZE % cp->provider->sectorsize) != 0) ||
828 	    (SBSIZE < cp->provider->sectorsize)) {
829 		g_topology_lock();
830 		g_vfs_close(cp);
831 		g_topology_unlock();
832 		return (EINVAL);
833 	}
834 
835 	bo = &devvp->v_bufobj;
836 	bo->bo_private = cp;
837 	bo->bo_ops = g_vfs_bufops;
838 	if (devvp->v_rdev->si_iosize_max != 0)
839 		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
840 	if (mp->mnt_iosize_max > MAXPHYS)
841 		mp->mnt_iosize_max = MAXPHYS;
842 
843 	bp = NULL;
844 	ump = NULL;
845 	if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
846 		goto out;
847 	es = (struct ext2fs *)bp->b_data;
848 	if (ext2_check_sb_compat(es, dev, ronly) != 0) {
849 		error = EINVAL;		/* XXX needs translation */
850 		goto out;
851 	}
852 	if ((es->e2fs_state & E2FS_ISCLEAN) == 0 ||
853 	    (es->e2fs_state & E2FS_ERRORS)) {
854 		if (ronly || (mp->mnt_flag & MNT_FORCE)) {
855 			printf(
856 "WARNING: Filesystem was not properly dismounted\n");
857 		} else {
858 			printf(
859 "WARNING: R/W mount denied.  Filesystem is not clean - run fsck\n");
860 			error = EPERM;
861 			goto out;
862 		}
863 	}
864 	ump = malloc(sizeof(*ump), M_EXT2MNT, M_WAITOK | M_ZERO);
865 
866 	/*
867 	 * I don't know whether this is the right strategy. Note that
868 	 * we dynamically allocate both an m_ext2fs and an ext2fs
869 	 * while Linux keeps the super block in a locked buffer.
870 	 */
871 	ump->um_e2fs = malloc(sizeof(struct m_ext2fs),
872 	    M_EXT2MNT, M_WAITOK | M_ZERO);
873 	ump->um_e2fs->e2fs = malloc(sizeof(struct ext2fs),
874 	    M_EXT2MNT, M_WAITOK);
875 	mtx_init(EXT2_MTX(ump), "EXT2FS", "EXT2FS Lock", MTX_DEF);
876 	bcopy(es, ump->um_e2fs->e2fs, (u_int)sizeof(struct ext2fs));
877 	if ((error = ext2_compute_sb_data(devvp, ump->um_e2fs->e2fs, ump->um_e2fs)))
878 		goto out;
879 
880 	/*
881 	 * Calculate the maximum contiguous blocks and size of cluster summary
882 	 * array.  In FFS this is done by newfs; however, the superblock
883 	 * in ext2fs doesn't have these variables, so we can calculate
884 	 * them here.
885 	 */
886 	e2fs_maxcontig = MAX(1, MAXPHYS / ump->um_e2fs->e2fs_bsize);
887 	ump->um_e2fs->e2fs_contigsumsize = MIN(e2fs_maxcontig, EXT2_MAXCONTIG);
888 	if (ump->um_e2fs->e2fs_contigsumsize > 0) {
889 		size = ump->um_e2fs->e2fs_gcount * sizeof(int32_t);
890 		ump->um_e2fs->e2fs_maxcluster = malloc(size, M_EXT2MNT, M_WAITOK);
891 		size = ump->um_e2fs->e2fs_gcount * sizeof(struct csum);
892 		ump->um_e2fs->e2fs_clustersum = malloc(size, M_EXT2MNT, M_WAITOK);
893 		lp = ump->um_e2fs->e2fs_maxcluster;
894 		sump = ump->um_e2fs->e2fs_clustersum;
895 		for (i = 0; i < ump->um_e2fs->e2fs_gcount; i++, sump++) {
896 			*lp++ = ump->um_e2fs->e2fs_contigsumsize;
897 			sump->cs_init = 0;
898 			sump->cs_sum = malloc((ump->um_e2fs->e2fs_contigsumsize + 1) *
899 			    sizeof(int32_t), M_EXT2MNT, M_WAITOK | M_ZERO);
900 		}
901 	}
902 
903 	brelse(bp);
904 	bp = NULL;
905 	fs = ump->um_e2fs;
906 	fs->e2fs_ronly = ronly;	/* ronly is set according to mnt_flags */
907 
908 	/*
909 	 * If the fs is not mounted read-only, make sure the super block is
910 	 * always written back on a sync().
911 	 */
912 	fs->e2fs_wasvalid = fs->e2fs->e2fs_state & E2FS_ISCLEAN ? 1 : 0;
913 	if (ronly == 0) {
914 		fs->e2fs_fmod = 1;	/* mark it modified */
915 		fs->e2fs->e2fs_state &= ~E2FS_ISCLEAN;	/* set fs invalid */
916 	}
917 	mp->mnt_data = ump;
918 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
919 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
920 	mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
921 	MNT_ILOCK(mp);
922 	mp->mnt_flag |= MNT_LOCAL;
923 	MNT_IUNLOCK(mp);
924 	ump->um_mountp = mp;
925 	ump->um_dev = dev;
926 	ump->um_devvp = devvp;
927 	ump->um_bo = &devvp->v_bufobj;
928 	ump->um_cp = cp;
929 
930 	/*
931 	 * Setting those two parameters allowed us to use
932 	 * ufs_bmap w/o changse!
933 	 */
934 	ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
935 	ump->um_bptrtodb = fs->e2fs->e2fs_log_bsize + 1;
936 	ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
937 	if (ronly == 0)
938 		ext2_sbupdate(ump, MNT_WAIT);
939 	/*
940 	 * Initialize filesystem stat information in mount struct.
941 	 */
942 	MNT_ILOCK(mp);
943 	mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
944 	    MNTK_USES_BCACHE;
945 	MNT_IUNLOCK(mp);
946 	return (0);
947 out:
948 	if (bp)
949 		brelse(bp);
950 	if (cp != NULL) {
951 		g_topology_lock();
952 		g_vfs_close(cp);
953 		g_topology_unlock();
954 	}
955 	if (ump) {
956 		mtx_destroy(EXT2_MTX(ump));
957 		free(ump->um_e2fs->e2fs_gd, M_EXT2MNT);
958 		free(ump->um_e2fs->e2fs_contigdirs, M_EXT2MNT);
959 		free(ump->um_e2fs->e2fs, M_EXT2MNT);
960 		free(ump->um_e2fs, M_EXT2MNT);
961 		free(ump, M_EXT2MNT);
962 		mp->mnt_data = NULL;
963 	}
964 	return (error);
965 }
966 
967 /*
968  * Unmount system call.
969  */
970 static int
971 ext2_unmount(struct mount *mp, int mntflags)
972 {
973 	struct ext2mount *ump;
974 	struct m_ext2fs *fs;
975 	struct csum *sump;
976 	int error, flags, i, ronly;
977 
978 	flags = 0;
979 	if (mntflags & MNT_FORCE) {
980 		if (mp->mnt_flag & MNT_ROOTFS)
981 			return (EINVAL);
982 		flags |= FORCECLOSE;
983 	}
984 	if ((error = ext2_flushfiles(mp, flags, curthread)) != 0)
985 		return (error);
986 	ump = VFSTOEXT2(mp);
987 	fs = ump->um_e2fs;
988 	ronly = fs->e2fs_ronly;
989 	if (ronly == 0 && ext2_cgupdate(ump, MNT_WAIT) == 0) {
990 		if (fs->e2fs_wasvalid)
991 			fs->e2fs->e2fs_state |= E2FS_ISCLEAN;
992 		ext2_sbupdate(ump, MNT_WAIT);
993 	}
994 
995 	g_topology_lock();
996 	g_vfs_close(ump->um_cp);
997 	g_topology_unlock();
998 	vrele(ump->um_devvp);
999 	sump = fs->e2fs_clustersum;
1000 	for (i = 0; i < fs->e2fs_gcount; i++, sump++)
1001 		free(sump->cs_sum, M_EXT2MNT);
1002 	free(fs->e2fs_clustersum, M_EXT2MNT);
1003 	free(fs->e2fs_maxcluster, M_EXT2MNT);
1004 	free(fs->e2fs_gd, M_EXT2MNT);
1005 	free(fs->e2fs_contigdirs, M_EXT2MNT);
1006 	free(fs->e2fs, M_EXT2MNT);
1007 	free(fs, M_EXT2MNT);
1008 	free(ump, M_EXT2MNT);
1009 	mp->mnt_data = NULL;
1010 	MNT_ILOCK(mp);
1011 	mp->mnt_flag &= ~MNT_LOCAL;
1012 	MNT_IUNLOCK(mp);
1013 	return (error);
1014 }
1015 
1016 /*
1017  * Flush out all the files in a filesystem.
1018  */
1019 static int
1020 ext2_flushfiles(struct mount *mp, int flags, struct thread *td)
1021 {
1022 	int error;
1023 
1024 	error = vflush(mp, 0, flags, td);
1025 	return (error);
1026 }
1027 
1028 /*
1029  * Get filesystem statistics.
1030  */
1031 int
1032 ext2_statfs(struct mount *mp, struct statfs *sbp)
1033 {
1034 	struct ext2mount *ump;
1035 	struct m_ext2fs *fs;
1036 	uint32_t overhead, overhead_per_group, ngdb;
1037 	int i, ngroups;
1038 
1039 	ump = VFSTOEXT2(mp);
1040 	fs = ump->um_e2fs;
1041 	if (fs->e2fs->e2fs_magic != E2FS_MAGIC)
1042 		panic("ext2_statfs");
1043 
1044 	/*
1045 	 * Compute the overhead (FS structures)
1046 	 */
1047 	overhead_per_group =
1048 	    1 /* block bitmap */ +
1049 	    1 /* inode bitmap */ +
1050 	    fs->e2fs_itpg;
1051 	overhead = fs->e2fs->e2fs_first_dblock +
1052 	    fs->e2fs_gcount * overhead_per_group;
1053 	if (fs->e2fs->e2fs_rev > E2FS_REV0 &&
1054 	    fs->e2fs->e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
1055 		for (i = 0, ngroups = 0; i < fs->e2fs_gcount; i++) {
1056 			if (ext2_cg_has_sb(fs, i))
1057 				ngroups++;
1058 		}
1059 	} else {
1060 		ngroups = fs->e2fs_gcount;
1061 	}
1062 	ngdb = fs->e2fs_gdbcount;
1063 	if (fs->e2fs->e2fs_rev > E2FS_REV0 &&
1064 	    fs->e2fs->e2fs_features_compat & EXT2F_COMPAT_RESIZE)
1065 		ngdb += fs->e2fs->e2fs_reserved_ngdb;
1066 	overhead += ngroups * (1 /* superblock */ + ngdb);
1067 
1068 	sbp->f_bsize = EXT2_FRAG_SIZE(fs);
1069 	sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
1070 	sbp->f_blocks = fs->e2fs_bcount - overhead;
1071 	sbp->f_bfree = fs->e2fs_fbcount;
1072 	sbp->f_bavail = sbp->f_bfree - fs->e2fs_rbcount;
1073 	sbp->f_files = fs->e2fs->e2fs_icount;
1074 	sbp->f_ffree = fs->e2fs->e2fs_ficount;
1075 	return (0);
1076 }
1077 
1078 /*
1079  * Go through the disk queues to initiate sandbagged IO;
1080  * go through the inodes to write those that have been modified;
1081  * initiate the writing of the super block if it has been modified.
1082  *
1083  * Note: we are always called with the filesystem marked `MPBUSY'.
1084  */
1085 static int
1086 ext2_sync(struct mount *mp, int waitfor)
1087 {
1088 	struct vnode *mvp, *vp;
1089 	struct thread *td;
1090 	struct inode *ip;
1091 	struct ext2mount *ump = VFSTOEXT2(mp);
1092 	struct m_ext2fs *fs;
1093 	int error, allerror = 0;
1094 
1095 	td = curthread;
1096 	fs = ump->um_e2fs;
1097 	if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) {		/* XXX */
1098 		printf("fs = %s\n", fs->e2fs_fsmnt);
1099 		panic("ext2_sync: rofs mod");
1100 	}
1101 
1102 	/*
1103 	 * Write back each (modified) inode.
1104 	 */
1105 loop:
1106 	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1107 		if (vp->v_type == VNON) {
1108 			VI_UNLOCK(vp);
1109 			continue;
1110 		}
1111 		ip = VTOI(vp);
1112 		if ((ip->i_flag &
1113 		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1114 		    (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
1115 		    waitfor == MNT_LAZY)) {
1116 			VI_UNLOCK(vp);
1117 			continue;
1118 		}
1119 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
1120 		if (error) {
1121 			if (error == ENOENT) {
1122 				MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1123 				goto loop;
1124 			}
1125 			continue;
1126 		}
1127 		if ((error = VOP_FSYNC(vp, waitfor, td)) != 0)
1128 			allerror = error;
1129 		VOP_UNLOCK(vp, 0);
1130 		vrele(vp);
1131 	}
1132 
1133 	/*
1134 	 * Force stale filesystem control information to be flushed.
1135 	 */
1136 	if (waitfor != MNT_LAZY) {
1137 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1138 		if ((error = VOP_FSYNC(ump->um_devvp, waitfor, td)) != 0)
1139 			allerror = error;
1140 		VOP_UNLOCK(ump->um_devvp, 0);
1141 	}
1142 
1143 	/*
1144 	 * Write back modified superblock.
1145 	 */
1146 	if (fs->e2fs_fmod != 0) {
1147 		fs->e2fs_fmod = 0;
1148 		fs->e2fs->e2fs_wtime = time_second;
1149 		if ((error = ext2_cgupdate(ump, waitfor)) != 0)
1150 			allerror = error;
1151 	}
1152 	return (allerror);
1153 }
1154 
1155 /*
1156  * Look up an EXT2FS dinode number to find its incore vnode, otherwise read it
1157  * in from disk.  If it is in core, wait for the lock bit to clear, then
1158  * return the inode locked.  Detection and handling of mount points must be
1159  * done by the calling routine.
1160  */
1161 static int
1162 ext2_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
1163 {
1164 	struct m_ext2fs *fs;
1165 	struct inode *ip;
1166 	struct ext2mount *ump;
1167 	struct buf *bp;
1168 	struct vnode *vp;
1169 	struct thread *td;
1170 	unsigned int i, used_blocks;
1171 	int error;
1172 
1173 	td = curthread;
1174 	error = vfs_hash_get(mp, ino, flags, td, vpp, NULL, NULL);
1175 	if (error || *vpp != NULL)
1176 		return (error);
1177 
1178 	ump = VFSTOEXT2(mp);
1179 	ip = malloc(sizeof(struct inode), M_EXT2NODE, M_WAITOK | M_ZERO);
1180 
1181 	/* Allocate a new vnode/inode. */
1182 	if ((error = getnewvnode("ext2fs", mp, &ext2_vnodeops, &vp)) != 0) {
1183 		*vpp = NULL;
1184 		free(ip, M_EXT2NODE);
1185 		return (error);
1186 	}
1187 	vp->v_data = ip;
1188 	ip->i_vnode = vp;
1189 	ip->i_e2fs = fs = ump->um_e2fs;
1190 	ip->i_ump = ump;
1191 	ip->i_number = ino;
1192 
1193 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1194 	error = insmntque(vp, mp);
1195 	if (error != 0) {
1196 		free(ip, M_EXT2NODE);
1197 		*vpp = NULL;
1198 		return (error);
1199 	}
1200 	error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
1201 	if (error || *vpp != NULL)
1202 		return (error);
1203 
1204 	/* Read in the disk contents for the inode, copy into the inode. */
1205 	if ((error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1206 	    (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) {
1207 		/*
1208 		 * The inode does not contain anything useful, so it would
1209 		 * be misleading to leave it on its hash chain. With mode
1210 		 * still zero, it will be unlinked and returned to the free
1211 		 * list by vput().
1212 		 */
1213 		brelse(bp);
1214 		vput(vp);
1215 		*vpp = NULL;
1216 		return (error);
1217 	}
1218 	/* convert ext2 inode to dinode */
1219 	error = ext2_ei2i((struct ext2fs_dinode *)((char *)bp->b_data +
1220 	    EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ino)), ip);
1221 	if (error) {
1222 		brelse(bp);
1223 		vput(vp);
1224 		*vpp = NULL;
1225 		return (error);
1226 	}
1227 	ip->i_block_group = ino_to_cg(fs, ino);
1228 	ip->i_next_alloc_block = 0;
1229 	ip->i_next_alloc_goal = 0;
1230 
1231 	/*
1232 	 * Now we want to make sure that block pointers for unused
1233 	 * blocks are zeroed out - ext2_balloc depends on this
1234 	 * although for regular files and directories only
1235 	 *
1236 	 * If IN_E4EXTENTS is enabled, unused blocks are not zeroed
1237 	 * out because we could corrupt the extent tree.
1238 	 */
1239 	if (!(ip->i_flag & IN_E4EXTENTS) &&
1240 	    (S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode))) {
1241 		used_blocks = howmany(ip->i_size, fs->e2fs_bsize);
1242 		for (i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
1243 			ip->i_db[i] = 0;
1244 	}
1245 #ifdef EXT2FS_DEBUG
1246 	ext2_print_inode(ip);
1247 	ext4_ext_print_extent_tree_status(ip);
1248 #endif
1249 	bqrelse(bp);
1250 
1251 	/*
1252 	 * Initialize the vnode from the inode, check for aliases.
1253 	 * Note that the underlying vnode may have changed.
1254 	 */
1255 	if ((error = ext2_vinit(mp, &ext2_fifoops, &vp)) != 0) {
1256 		vput(vp);
1257 		*vpp = NULL;
1258 		return (error);
1259 	}
1260 
1261 	/*
1262 	 * Finish inode initialization.
1263 	 */
1264 
1265 	*vpp = vp;
1266 	return (0);
1267 }
1268 
1269 /*
1270  * File handle to vnode
1271  *
1272  * Have to be really careful about stale file handles:
1273  * - check that the inode number is valid
1274  * - call ext2_vget() to get the locked inode
1275  * - check for an unallocated inode (i_mode == 0)
1276  * - check that the given client host has export rights and return
1277  *   those rights via. exflagsp and credanonp
1278  */
1279 static int
1280 ext2_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
1281 {
1282 	struct inode *ip;
1283 	struct ufid *ufhp;
1284 	struct vnode *nvp;
1285 	struct m_ext2fs *fs;
1286 	int error;
1287 
1288 	ufhp = (struct ufid *)fhp;
1289 	fs = VFSTOEXT2(mp)->um_e2fs;
1290 	if (ufhp->ufid_ino < EXT2_ROOTINO ||
1291 	    ufhp->ufid_ino > fs->e2fs_gcount * fs->e2fs->e2fs_ipg)
1292 		return (ESTALE);
1293 
1294 	error = VFS_VGET(mp, ufhp->ufid_ino, LK_EXCLUSIVE, &nvp);
1295 	if (error) {
1296 		*vpp = NULLVP;
1297 		return (error);
1298 	}
1299 	ip = VTOI(nvp);
1300 	if (ip->i_mode == 0 ||
1301 	    ip->i_gen != ufhp->ufid_gen || ip->i_nlink <= 0) {
1302 		vput(nvp);
1303 		*vpp = NULLVP;
1304 		return (ESTALE);
1305 	}
1306 	*vpp = nvp;
1307 	vnode_create_vobject(*vpp, 0, curthread);
1308 	return (0);
1309 }
1310 
1311 /*
1312  * Write a superblock and associated information back to disk.
1313  */
1314 static int
1315 ext2_sbupdate(struct ext2mount *mp, int waitfor)
1316 {
1317 	struct m_ext2fs *fs = mp->um_e2fs;
1318 	struct ext2fs *es = fs->e2fs;
1319 	struct buf *bp;
1320 	int error = 0;
1321 
1322 	es->e2fs_bcount = fs->e2fs_bcount & 0xffffffff;
1323 	es->e2fs_rbcount = fs->e2fs_rbcount & 0xffffffff;
1324 	es->e2fs_fbcount = fs->e2fs_fbcount & 0xffffffff;
1325 	if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
1326 		es->e4fs_bcount_hi = fs->e2fs_bcount >> 32;
1327 		es->e4fs_rbcount_hi = fs->e2fs_rbcount >> 32;
1328 		es->e4fs_fbcount_hi = fs->e2fs_fbcount >> 32;
1329 	}
1330 
1331 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
1332 		ext2_sb_csum_set(fs);
1333 
1334 	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0, 0);
1335 	bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2fs));
1336 	if (waitfor == MNT_WAIT)
1337 		error = bwrite(bp);
1338 	else
1339 		bawrite(bp);
1340 
1341 	/*
1342 	 * The buffers for group descriptors, inode bitmaps and block bitmaps
1343 	 * are not busy at this point and are (hopefully) written by the
1344 	 * usual sync mechanism. No need to write them here.
1345 	 */
1346 	return (error);
1347 }
1348 int
1349 ext2_cgupdate(struct ext2mount *mp, int waitfor)
1350 {
1351 	struct m_ext2fs *fs = mp->um_e2fs;
1352 	struct buf *bp;
1353 	int i, j, g_count = 0, error = 0, allerror = 0;
1354 
1355 	allerror = ext2_sbupdate(mp, waitfor);
1356 
1357 	/* Update gd csums */
1358 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) ||
1359 	    EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
1360 		ext2_gd_csum_set(fs);
1361 
1362 	for (i = 0; i < fs->e2fs_gdbcount; i++) {
1363 		bp = getblk(mp->um_devvp, fsbtodb(fs,
1364 		    ext2_cg_location(fs, i)),
1365 		    fs->e2fs_bsize, 0, 0, 0);
1366 		if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
1367 			memcpy(bp->b_data, &fs->e2fs_gd[
1368 			    i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
1369 			    fs->e2fs_bsize);
1370 		} else {
1371 			for (j = 0; j < fs->e2fs_bsize / E2FS_REV0_GD_SIZE &&
1372 			    g_count < fs->e2fs_gcount; j++, g_count++)
1373 				memcpy(bp->b_data + j * E2FS_REV0_GD_SIZE,
1374 				    &fs->e2fs_gd[g_count], E2FS_REV0_GD_SIZE);
1375 		}
1376 		if (waitfor == MNT_WAIT)
1377 			error = bwrite(bp);
1378 		else
1379 			bawrite(bp);
1380 	}
1381 
1382 	if (!allerror && error)
1383 		allerror = error;
1384 	return (allerror);
1385 }
1386 
1387 /*
1388  * Return the root of a filesystem.
1389  */
1390 static int
1391 ext2_root(struct mount *mp, int flags, struct vnode **vpp)
1392 {
1393 	struct vnode *nvp;
1394 	int error;
1395 
1396 	error = VFS_VGET(mp, EXT2_ROOTINO, LK_EXCLUSIVE, &nvp);
1397 	if (error)
1398 		return (error);
1399 	*vpp = nvp;
1400 	return (0);
1401 }
1402