xref: /freebsd/sys/fs/ext2fs/ext2_vfsops.c (revision 0957b409)
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	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 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 /*
354  * This computes the fields of the m_ext2fs structure from the
355  * data in the ext2fs structure read in.
356  */
357 static int
358 compute_sb_data(struct vnode *devvp, struct ext2fs *es,
359     struct m_ext2fs *fs)
360 {
361 	int g_count = 0, error;
362 	int i, j;
363 	struct buf *bp;
364 	uint32_t e2fs_descpb, e2fs_gdbcount_alloc;
365 
366 	fs->e2fs_bcount = es->e2fs_bcount;
367 	fs->e2fs_rbcount = es->e2fs_rbcount;
368 	fs->e2fs_fbcount = es->e2fs_fbcount;
369 	if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
370 		fs->e2fs_bcount |= (uint64_t)(es->e4fs_bcount_hi) << 32;
371 		fs->e2fs_rbcount |= (uint64_t)(es->e4fs_rbcount_hi) << 32;
372 		fs->e2fs_fbcount |= (uint64_t)(es->e4fs_fbcount_hi) << 32;
373 	}
374 	fs->e2fs_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->e2fs_log_bsize;
375 	fs->e2fs_bsize = 1U << fs->e2fs_bshift;
376 	fs->e2fs_fsbtodb = es->e2fs_log_bsize + 1;
377 	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
378 	fs->e2fs_fsize = EXT2_MIN_FRAG_SIZE << es->e2fs_log_fsize;
379 	if (fs->e2fs_fsize)
380 		fs->e2fs_fpb = fs->e2fs_bsize / fs->e2fs_fsize;
381 	fs->e2fs_bpg = es->e2fs_bpg;
382 	fs->e2fs_fpg = es->e2fs_fpg;
383 	fs->e2fs_ipg = es->e2fs_ipg;
384 	if (es->e2fs_rev == E2FS_REV0) {
385 		fs->e2fs_isize = E2FS_REV0_INODE_SIZE;
386 	} else {
387 		fs->e2fs_isize = es->e2fs_inode_size;
388 
389 		/*
390 		 * Simple sanity check for superblock inode size value.
391 		 */
392 		if (EXT2_INODE_SIZE(fs) < E2FS_REV0_INODE_SIZE ||
393 		    EXT2_INODE_SIZE(fs) > fs->e2fs_bsize ||
394 		    (fs->e2fs_isize & (fs->e2fs_isize - 1)) != 0) {
395 			printf("ext2fs: invalid inode size %d\n",
396 			    fs->e2fs_isize);
397 			return (EIO);
398 		}
399 	}
400 	/* Check for extra isize in big inodes. */
401 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_EXTRA_ISIZE) &&
402 	    EXT2_INODE_SIZE(fs) < sizeof(struct ext2fs_dinode)) {
403 		printf("ext2fs: no space for extra inode timestamps\n");
404 		return (EINVAL);
405 	}
406 	/* Check checksum features */
407 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) &&
408 	    EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
409 		printf("ext2fs: incorrect checksum features combination\n");
410 		return (EINVAL);
411 	}
412 	/* Check for group descriptor size */
413 	if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT) &&
414 	    (es->e3fs_desc_size != sizeof(struct ext2_gd))) {
415 		printf("ext2fs: group descriptor size unsupported %d\n",
416 		    es->e3fs_desc_size);
417 		return (EINVAL);
418 	}
419 	/* Check for block size = 1K|2K|4K */
420 	if (es->e2fs_log_bsize > 2) {
421 		printf("ext2fs: bad block size: %d\n", es->e2fs_log_bsize);
422 		return (EINVAL);
423 	}
424 	/* Check for group size */
425 	if (fs->e2fs_bpg == 0) {
426 		printf("ext2fs: zero blocks per group\n");
427 		return (EINVAL);
428 	}
429 	if (fs->e2fs_bpg != fs->e2fs_bsize * 8) {
430 		printf("ext2fs: non-standard group size unsupported %d\n",
431 		    fs->e2fs_bpg);
432 		return (EINVAL);
433 	}
434 
435 	fs->e2fs_ipb = fs->e2fs_bsize / EXT2_INODE_SIZE(fs);
436 	if (fs->e2fs_ipg == 0) {
437 		printf("ext2fs: zero inodes per group\n");
438 		return (EINVAL);
439 	}
440 	fs->e2fs_itpg = fs->e2fs_ipg / fs->e2fs_ipb;
441 	/* Check for block consistency */
442 	if (es->e2fs_first_dblock >= fs->e2fs_bcount) {
443 		printf("ext2fs: invalid first data block\n");
444 		return (EINVAL);
445 	}
446 	if (fs->e2fs_rbcount > fs->e2fs_bcount ||
447 	    fs->e2fs_fbcount > fs->e2fs_bcount) {
448 		printf("ext2fs: invalid block count\n");
449 		return (EINVAL);
450 	}
451 	/* s_resuid / s_resgid ? */
452 	fs->e2fs_gcount = howmany(fs->e2fs_bcount - es->e2fs_first_dblock,
453 	    EXT2_BLOCKS_PER_GROUP(fs));
454 	if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
455 		e2fs_descpb = fs->e2fs_bsize / sizeof(struct ext2_gd);
456 		e2fs_gdbcount_alloc = howmany(fs->e2fs_gcount, e2fs_descpb);
457 	} else {
458 		e2fs_descpb = fs->e2fs_bsize / E2FS_REV0_GD_SIZE;
459 		e2fs_gdbcount_alloc = howmany(fs->e2fs_gcount,
460 		    fs->e2fs_bsize / sizeof(struct ext2_gd));
461 	}
462 	fs->e2fs_gdbcount = howmany(fs->e2fs_gcount, e2fs_descpb);
463 	fs->e2fs_gd = malloc(e2fs_gdbcount_alloc * fs->e2fs_bsize,
464 	    M_EXT2MNT, M_WAITOK | M_ZERO);
465 	fs->e2fs_contigdirs = malloc(fs->e2fs_gcount *
466 	    sizeof(*fs->e2fs_contigdirs), M_EXT2MNT, M_WAITOK | M_ZERO);
467 
468 	for (i = 0; i < fs->e2fs_gdbcount; i++) {
469 		error = bread(devvp,
470 		    fsbtodb(fs, cg_location(fs, i)),
471 		    fs->e2fs_bsize, NOCRED, &bp);
472 		if (error) {
473 			free(fs->e2fs_contigdirs, M_EXT2MNT);
474 			free(fs->e2fs_gd, M_EXT2MNT);
475 			brelse(bp);
476 			return (error);
477 		}
478 		if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
479 			memcpy(&fs->e2fs_gd[
480 			    i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
481 			    bp->b_data, fs->e2fs_bsize);
482 		} else {
483 			for (j = 0; j < e2fs_descpb &&
484 			    g_count < fs->e2fs_gcount; j++, g_count++)
485 				memcpy(&fs->e2fs_gd[g_count],
486 				    bp->b_data + j * E2FS_REV0_GD_SIZE,
487 				    E2FS_REV0_GD_SIZE);
488 		}
489 		brelse(bp);
490 		bp = NULL;
491 	}
492 	/* Precompute checksum seed for all metadata */
493 	ext2_sb_csum_set_seed(fs);
494 	/* Verfy cg csum */
495 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) ||
496 	    EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
497 		error = ext2_gd_csum_verify(fs, devvp->v_rdev);
498 		if (error)
499 			return (error);
500 	}
501 	/* Initialization for the ext2 Orlov allocator variant. */
502 	fs->e2fs_total_dir = 0;
503 	for (i = 0; i < fs->e2fs_gcount; i++)
504 		fs->e2fs_total_dir += e2fs_gd_get_ndirs(&fs->e2fs_gd[i]);
505 
506 	if (es->e2fs_rev == E2FS_REV0 ||
507 	    !EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_LARGEFILE))
508 		fs->e2fs_maxfilesize = 0x7fffffff;
509 	else {
510 		fs->e2fs_maxfilesize = 0xffffffffffff;
511 		if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_HUGE_FILE))
512 			fs->e2fs_maxfilesize = 0x7fffffffffffffff;
513 	}
514 	if (es->e4fs_flags & E2FS_UNSIGNED_HASH) {
515 		fs->e2fs_uhash = 3;
516 	} else if ((es->e4fs_flags & E2FS_SIGNED_HASH) == 0) {
517 #ifdef __CHAR_UNSIGNED__
518 		es->e4fs_flags |= E2FS_UNSIGNED_HASH;
519 		fs->e2fs_uhash = 3;
520 #else
521 		es->e4fs_flags |= E2FS_SIGNED_HASH;
522 #endif
523 	}
524 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
525 		error = ext2_sb_csum_verify(fs);
526 
527 	return (error);
528 }
529 
530 /*
531  * Reload all incore data for a filesystem (used after running fsck on
532  * the root filesystem and finding things to fix). The filesystem must
533  * be mounted read-only.
534  *
535  * Things to do to update the mount:
536  *	1) invalidate all cached meta-data.
537  *	2) re-read superblock from disk.
538  *	3) invalidate all cluster summary information.
539  *	4) invalidate all inactive vnodes.
540  *	5) invalidate all cached file data.
541  *	6) re-read inode data for all active vnodes.
542  * XXX we are missing some steps, in particular # 3, this has to be reviewed.
543  */
544 static int
545 ext2_reload(struct mount *mp, struct thread *td)
546 {
547 	struct vnode *vp, *mvp, *devvp;
548 	struct inode *ip;
549 	struct buf *bp;
550 	struct ext2fs *es;
551 	struct m_ext2fs *fs;
552 	struct csum *sump;
553 	int error, i;
554 	int32_t *lp;
555 
556 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
557 		return (EINVAL);
558 	/*
559 	 * Step 1: invalidate all cached meta-data.
560 	 */
561 	devvp = VFSTOEXT2(mp)->um_devvp;
562 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
563 	if (vinvalbuf(devvp, 0, 0, 0) != 0)
564 		panic("ext2_reload: dirty1");
565 	VOP_UNLOCK(devvp, 0);
566 
567 	/*
568 	 * Step 2: re-read superblock from disk.
569 	 * constants have been adjusted for ext2
570 	 */
571 	if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
572 		return (error);
573 	es = (struct ext2fs *)bp->b_data;
574 	if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) {
575 		brelse(bp);
576 		return (EIO);		/* XXX needs translation */
577 	}
578 	fs = VFSTOEXT2(mp)->um_e2fs;
579 	bcopy(bp->b_data, fs->e2fs, sizeof(struct ext2fs));
580 
581 	if ((error = compute_sb_data(devvp, es, fs)) != 0) {
582 		brelse(bp);
583 		return (error);
584 	}
585 #ifdef UNKLAR
586 	if (fs->fs_sbsize < SBSIZE)
587 		bp->b_flags |= B_INVAL;
588 #endif
589 	brelse(bp);
590 
591 	/*
592 	 * Step 3: invalidate all cluster summary information.
593 	 */
594 	if (fs->e2fs_contigsumsize > 0) {
595 		lp = fs->e2fs_maxcluster;
596 		sump = fs->e2fs_clustersum;
597 		for (i = 0; i < fs->e2fs_gcount; i++, sump++) {
598 			*lp++ = fs->e2fs_contigsumsize;
599 			sump->cs_init = 0;
600 			bzero(sump->cs_sum, fs->e2fs_contigsumsize + 1);
601 		}
602 	}
603 
604 loop:
605 	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
606 		/*
607 		 * Step 4: invalidate all cached file data.
608 		 */
609 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
610 			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
611 			goto loop;
612 		}
613 		if (vinvalbuf(vp, 0, 0, 0))
614 			panic("ext2_reload: dirty2");
615 
616 		/*
617 		 * Step 5: re-read inode data for all active vnodes.
618 		 */
619 		ip = VTOI(vp);
620 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
621 		    (int)fs->e2fs_bsize, NOCRED, &bp);
622 		if (error) {
623 			VOP_UNLOCK(vp, 0);
624 			vrele(vp);
625 			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
626 			return (error);
627 		}
628 		ext2_ei2i((struct ext2fs_dinode *)((char *)bp->b_data +
629 		    EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ip->i_number)), ip);
630 		brelse(bp);
631 		VOP_UNLOCK(vp, 0);
632 		vrele(vp);
633 	}
634 	return (0);
635 }
636 
637 /*
638  * Common code for mount and mountroot.
639  */
640 static int
641 ext2_mountfs(struct vnode *devvp, struct mount *mp)
642 {
643 	struct ext2mount *ump;
644 	struct buf *bp;
645 	struct m_ext2fs *fs;
646 	struct ext2fs *es;
647 	struct cdev *dev = devvp->v_rdev;
648 	struct g_consumer *cp;
649 	struct bufobj *bo;
650 	struct csum *sump;
651 	int error;
652 	int ronly;
653 	int i;
654 	u_long size;
655 	int32_t *lp;
656 	int32_t e2fs_maxcontig;
657 
658 	ronly = vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0);
659 	/* XXX: use VOP_ACESS to check FS perms */
660 	g_topology_lock();
661 	error = g_vfs_open(devvp, &cp, "ext2fs", ronly ? 0 : 1);
662 	g_topology_unlock();
663 	VOP_UNLOCK(devvp, 0);
664 	if (error)
665 		return (error);
666 
667 	/* XXX: should we check for some sectorsize or 512 instead? */
668 	if (((SBSIZE % cp->provider->sectorsize) != 0) ||
669 	    (SBSIZE < cp->provider->sectorsize)) {
670 		g_topology_lock();
671 		g_vfs_close(cp);
672 		g_topology_unlock();
673 		return (EINVAL);
674 	}
675 
676 	bo = &devvp->v_bufobj;
677 	bo->bo_private = cp;
678 	bo->bo_ops = g_vfs_bufops;
679 	if (devvp->v_rdev->si_iosize_max != 0)
680 		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
681 	if (mp->mnt_iosize_max > MAXPHYS)
682 		mp->mnt_iosize_max = MAXPHYS;
683 
684 	bp = NULL;
685 	ump = NULL;
686 	if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
687 		goto out;
688 	es = (struct ext2fs *)bp->b_data;
689 	if (ext2_check_sb_compat(es, dev, ronly) != 0) {
690 		error = EINVAL;		/* XXX needs translation */
691 		goto out;
692 	}
693 	if ((es->e2fs_state & E2FS_ISCLEAN) == 0 ||
694 	    (es->e2fs_state & E2FS_ERRORS)) {
695 		if (ronly || (mp->mnt_flag & MNT_FORCE)) {
696 			printf(
697 "WARNING: Filesystem was not properly dismounted\n");
698 		} else {
699 			printf(
700 "WARNING: R/W mount denied.  Filesystem is not clean - run fsck\n");
701 			error = EPERM;
702 			goto out;
703 		}
704 	}
705 	ump = malloc(sizeof(*ump), M_EXT2MNT, M_WAITOK | M_ZERO);
706 
707 	/*
708 	 * I don't know whether this is the right strategy. Note that
709 	 * we dynamically allocate both an m_ext2fs and an ext2fs
710 	 * while Linux keeps the super block in a locked buffer.
711 	 */
712 	ump->um_e2fs = malloc(sizeof(struct m_ext2fs),
713 	    M_EXT2MNT, M_WAITOK | M_ZERO);
714 	ump->um_e2fs->e2fs = malloc(sizeof(struct ext2fs),
715 	    M_EXT2MNT, M_WAITOK);
716 	mtx_init(EXT2_MTX(ump), "EXT2FS", "EXT2FS Lock", MTX_DEF);
717 	bcopy(es, ump->um_e2fs->e2fs, (u_int)sizeof(struct ext2fs));
718 	if ((error = compute_sb_data(devvp, ump->um_e2fs->e2fs, ump->um_e2fs)))
719 		goto out;
720 
721 	/*
722 	 * Calculate the maximum contiguous blocks and size of cluster summary
723 	 * array.  In FFS this is done by newfs; however, the superblock
724 	 * in ext2fs doesn't have these variables, so we can calculate
725 	 * them here.
726 	 */
727 	e2fs_maxcontig = MAX(1, MAXPHYS / ump->um_e2fs->e2fs_bsize);
728 	ump->um_e2fs->e2fs_contigsumsize = MIN(e2fs_maxcontig, EXT2_MAXCONTIG);
729 	if (ump->um_e2fs->e2fs_contigsumsize > 0) {
730 		size = ump->um_e2fs->e2fs_gcount * sizeof(int32_t);
731 		ump->um_e2fs->e2fs_maxcluster = malloc(size, M_EXT2MNT, M_WAITOK);
732 		size = ump->um_e2fs->e2fs_gcount * sizeof(struct csum);
733 		ump->um_e2fs->e2fs_clustersum = malloc(size, M_EXT2MNT, M_WAITOK);
734 		lp = ump->um_e2fs->e2fs_maxcluster;
735 		sump = ump->um_e2fs->e2fs_clustersum;
736 		for (i = 0; i < ump->um_e2fs->e2fs_gcount; i++, sump++) {
737 			*lp++ = ump->um_e2fs->e2fs_contigsumsize;
738 			sump->cs_init = 0;
739 			sump->cs_sum = malloc((ump->um_e2fs->e2fs_contigsumsize + 1) *
740 			    sizeof(int32_t), M_EXT2MNT, M_WAITOK | M_ZERO);
741 		}
742 	}
743 
744 	brelse(bp);
745 	bp = NULL;
746 	fs = ump->um_e2fs;
747 	fs->e2fs_ronly = ronly;	/* ronly is set according to mnt_flags */
748 
749 	/*
750 	 * If the fs is not mounted read-only, make sure the super block is
751 	 * always written back on a sync().
752 	 */
753 	fs->e2fs_wasvalid = fs->e2fs->e2fs_state & E2FS_ISCLEAN ? 1 : 0;
754 	if (ronly == 0) {
755 		fs->e2fs_fmod = 1;	/* mark it modified */
756 		fs->e2fs->e2fs_state &= ~E2FS_ISCLEAN;	/* set fs invalid */
757 	}
758 	mp->mnt_data = ump;
759 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
760 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
761 	mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
762 	MNT_ILOCK(mp);
763 	mp->mnt_flag |= MNT_LOCAL;
764 	MNT_IUNLOCK(mp);
765 	ump->um_mountp = mp;
766 	ump->um_dev = dev;
767 	ump->um_devvp = devvp;
768 	ump->um_bo = &devvp->v_bufobj;
769 	ump->um_cp = cp;
770 
771 	/*
772 	 * Setting those two parameters allowed us to use
773 	 * ufs_bmap w/o changse!
774 	 */
775 	ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
776 	ump->um_bptrtodb = fs->e2fs->e2fs_log_bsize + 1;
777 	ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
778 	if (ronly == 0)
779 		ext2_sbupdate(ump, MNT_WAIT);
780 	/*
781 	 * Initialize filesystem stat information in mount struct.
782 	 */
783 	MNT_ILOCK(mp);
784 	mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
785 	    MNTK_USES_BCACHE;
786 	MNT_IUNLOCK(mp);
787 	return (0);
788 out:
789 	if (bp)
790 		brelse(bp);
791 	if (cp != NULL) {
792 		g_topology_lock();
793 		g_vfs_close(cp);
794 		g_topology_unlock();
795 	}
796 	if (ump) {
797 		mtx_destroy(EXT2_MTX(ump));
798 		free(ump->um_e2fs->e2fs_gd, M_EXT2MNT);
799 		free(ump->um_e2fs->e2fs_contigdirs, M_EXT2MNT);
800 		free(ump->um_e2fs->e2fs, M_EXT2MNT);
801 		free(ump->um_e2fs, M_EXT2MNT);
802 		free(ump, M_EXT2MNT);
803 		mp->mnt_data = NULL;
804 	}
805 	return (error);
806 }
807 
808 /*
809  * Unmount system call.
810  */
811 static int
812 ext2_unmount(struct mount *mp, int mntflags)
813 {
814 	struct ext2mount *ump;
815 	struct m_ext2fs *fs;
816 	struct csum *sump;
817 	int error, flags, i, ronly;
818 
819 	flags = 0;
820 	if (mntflags & MNT_FORCE) {
821 		if (mp->mnt_flag & MNT_ROOTFS)
822 			return (EINVAL);
823 		flags |= FORCECLOSE;
824 	}
825 	if ((error = ext2_flushfiles(mp, flags, curthread)) != 0)
826 		return (error);
827 	ump = VFSTOEXT2(mp);
828 	fs = ump->um_e2fs;
829 	ronly = fs->e2fs_ronly;
830 	if (ronly == 0 && ext2_cgupdate(ump, MNT_WAIT) == 0) {
831 		if (fs->e2fs_wasvalid)
832 			fs->e2fs->e2fs_state |= E2FS_ISCLEAN;
833 		ext2_sbupdate(ump, MNT_WAIT);
834 	}
835 
836 	g_topology_lock();
837 	g_vfs_close(ump->um_cp);
838 	g_topology_unlock();
839 	vrele(ump->um_devvp);
840 	sump = fs->e2fs_clustersum;
841 	for (i = 0; i < fs->e2fs_gcount; i++, sump++)
842 		free(sump->cs_sum, M_EXT2MNT);
843 	free(fs->e2fs_clustersum, M_EXT2MNT);
844 	free(fs->e2fs_maxcluster, M_EXT2MNT);
845 	free(fs->e2fs_gd, M_EXT2MNT);
846 	free(fs->e2fs_contigdirs, M_EXT2MNT);
847 	free(fs->e2fs, M_EXT2MNT);
848 	free(fs, M_EXT2MNT);
849 	free(ump, M_EXT2MNT);
850 	mp->mnt_data = NULL;
851 	MNT_ILOCK(mp);
852 	mp->mnt_flag &= ~MNT_LOCAL;
853 	MNT_IUNLOCK(mp);
854 	return (error);
855 }
856 
857 /*
858  * Flush out all the files in a filesystem.
859  */
860 static int
861 ext2_flushfiles(struct mount *mp, int flags, struct thread *td)
862 {
863 	int error;
864 
865 	error = vflush(mp, 0, flags, td);
866 	return (error);
867 }
868 
869 /*
870  * Get filesystem statistics.
871  */
872 int
873 ext2_statfs(struct mount *mp, struct statfs *sbp)
874 {
875 	struct ext2mount *ump;
876 	struct m_ext2fs *fs;
877 	uint32_t overhead, overhead_per_group, ngdb;
878 	int i, ngroups;
879 
880 	ump = VFSTOEXT2(mp);
881 	fs = ump->um_e2fs;
882 	if (fs->e2fs->e2fs_magic != E2FS_MAGIC)
883 		panic("ext2_statfs");
884 
885 	/*
886 	 * Compute the overhead (FS structures)
887 	 */
888 	overhead_per_group =
889 	    1 /* block bitmap */ +
890 	    1 /* inode bitmap */ +
891 	    fs->e2fs_itpg;
892 	overhead = fs->e2fs->e2fs_first_dblock +
893 	    fs->e2fs_gcount * overhead_per_group;
894 	if (fs->e2fs->e2fs_rev > E2FS_REV0 &&
895 	    fs->e2fs->e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
896 		for (i = 0, ngroups = 0; i < fs->e2fs_gcount; i++) {
897 			if (ext2_cg_has_sb(fs, i))
898 				ngroups++;
899 		}
900 	} else {
901 		ngroups = fs->e2fs_gcount;
902 	}
903 	ngdb = fs->e2fs_gdbcount;
904 	if (fs->e2fs->e2fs_rev > E2FS_REV0 &&
905 	    fs->e2fs->e2fs_features_compat & EXT2F_COMPAT_RESIZE)
906 		ngdb += fs->e2fs->e2fs_reserved_ngdb;
907 	overhead += ngroups * (1 /* superblock */ + ngdb);
908 
909 	sbp->f_bsize = EXT2_FRAG_SIZE(fs);
910 	sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
911 	sbp->f_blocks = fs->e2fs_bcount - overhead;
912 	sbp->f_bfree = fs->e2fs_fbcount;
913 	sbp->f_bavail = sbp->f_bfree - fs->e2fs_rbcount;
914 	sbp->f_files = fs->e2fs->e2fs_icount;
915 	sbp->f_ffree = fs->e2fs->e2fs_ficount;
916 	return (0);
917 }
918 
919 /*
920  * Go through the disk queues to initiate sandbagged IO;
921  * go through the inodes to write those that have been modified;
922  * initiate the writing of the super block if it has been modified.
923  *
924  * Note: we are always called with the filesystem marked `MPBUSY'.
925  */
926 static int
927 ext2_sync(struct mount *mp, int waitfor)
928 {
929 	struct vnode *mvp, *vp;
930 	struct thread *td;
931 	struct inode *ip;
932 	struct ext2mount *ump = VFSTOEXT2(mp);
933 	struct m_ext2fs *fs;
934 	int error, allerror = 0;
935 
936 	td = curthread;
937 	fs = ump->um_e2fs;
938 	if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) {		/* XXX */
939 		printf("fs = %s\n", fs->e2fs_fsmnt);
940 		panic("ext2_sync: rofs mod");
941 	}
942 
943 	/*
944 	 * Write back each (modified) inode.
945 	 */
946 loop:
947 	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
948 		if (vp->v_type == VNON) {
949 			VI_UNLOCK(vp);
950 			continue;
951 		}
952 		ip = VTOI(vp);
953 		if ((ip->i_flag &
954 		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
955 		    (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
956 		    waitfor == MNT_LAZY)) {
957 			VI_UNLOCK(vp);
958 			continue;
959 		}
960 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
961 		if (error) {
962 			if (error == ENOENT) {
963 				MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
964 				goto loop;
965 			}
966 			continue;
967 		}
968 		if ((error = VOP_FSYNC(vp, waitfor, td)) != 0)
969 			allerror = error;
970 		VOP_UNLOCK(vp, 0);
971 		vrele(vp);
972 	}
973 
974 	/*
975 	 * Force stale filesystem control information to be flushed.
976 	 */
977 	if (waitfor != MNT_LAZY) {
978 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
979 		if ((error = VOP_FSYNC(ump->um_devvp, waitfor, td)) != 0)
980 			allerror = error;
981 		VOP_UNLOCK(ump->um_devvp, 0);
982 	}
983 
984 	/*
985 	 * Write back modified superblock.
986 	 */
987 	if (fs->e2fs_fmod != 0) {
988 		fs->e2fs_fmod = 0;
989 		fs->e2fs->e2fs_wtime = time_second;
990 		if ((error = ext2_cgupdate(ump, waitfor)) != 0)
991 			allerror = error;
992 	}
993 	return (allerror);
994 }
995 
996 /*
997  * Look up an EXT2FS dinode number to find its incore vnode, otherwise read it
998  * in from disk.  If it is in core, wait for the lock bit to clear, then
999  * return the inode locked.  Detection and handling of mount points must be
1000  * done by the calling routine.
1001  */
1002 static int
1003 ext2_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
1004 {
1005 	struct m_ext2fs *fs;
1006 	struct inode *ip;
1007 	struct ext2mount *ump;
1008 	struct buf *bp;
1009 	struct vnode *vp;
1010 	struct thread *td;
1011 	int i, error;
1012 	int used_blocks;
1013 
1014 	td = curthread;
1015 	error = vfs_hash_get(mp, ino, flags, td, vpp, NULL, NULL);
1016 	if (error || *vpp != NULL)
1017 		return (error);
1018 
1019 	ump = VFSTOEXT2(mp);
1020 	ip = malloc(sizeof(struct inode), M_EXT2NODE, M_WAITOK | M_ZERO);
1021 
1022 	/* Allocate a new vnode/inode. */
1023 	if ((error = getnewvnode("ext2fs", mp, &ext2_vnodeops, &vp)) != 0) {
1024 		*vpp = NULL;
1025 		free(ip, M_EXT2NODE);
1026 		return (error);
1027 	}
1028 	vp->v_data = ip;
1029 	ip->i_vnode = vp;
1030 	ip->i_e2fs = fs = ump->um_e2fs;
1031 	ip->i_ump = ump;
1032 	ip->i_number = ino;
1033 
1034 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1035 	error = insmntque(vp, mp);
1036 	if (error != 0) {
1037 		free(ip, M_EXT2NODE);
1038 		*vpp = NULL;
1039 		return (error);
1040 	}
1041 	error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
1042 	if (error || *vpp != NULL)
1043 		return (error);
1044 
1045 	/* Read in the disk contents for the inode, copy into the inode. */
1046 	if ((error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1047 	    (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) {
1048 		/*
1049 		 * The inode does not contain anything useful, so it would
1050 		 * be misleading to leave it on its hash chain. With mode
1051 		 * still zero, it will be unlinked and returned to the free
1052 		 * list by vput().
1053 		 */
1054 		brelse(bp);
1055 		vput(vp);
1056 		*vpp = NULL;
1057 		return (error);
1058 	}
1059 	/* convert ext2 inode to dinode */
1060 	error = ext2_ei2i((struct ext2fs_dinode *)((char *)bp->b_data +
1061 	    EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ino)), ip);
1062 	if (error) {
1063 		printf("ext2fs: Bad inode %lu csum - run fsck\n",
1064 		    (unsigned long)ino);
1065 		brelse(bp);
1066 		vput(vp);
1067 		*vpp = NULL;
1068 		return (error);
1069 	}
1070 	ip->i_block_group = ino_to_cg(fs, ino);
1071 	ip->i_next_alloc_block = 0;
1072 	ip->i_next_alloc_goal = 0;
1073 
1074 	/*
1075 	 * Now we want to make sure that block pointers for unused
1076 	 * blocks are zeroed out - ext2_balloc depends on this
1077 	 * although for regular files and directories only
1078 	 *
1079 	 * If IN_E4EXTENTS is enabled, unused blocks are not zeroed
1080 	 * out because we could corrupt the extent tree.
1081 	 */
1082 	if (!(ip->i_flag & IN_E4EXTENTS) &&
1083 	    (S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode))) {
1084 		used_blocks = howmany(ip->i_size, fs->e2fs_bsize);
1085 		for (i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
1086 			ip->i_db[i] = 0;
1087 	}
1088 #ifdef EXT2FS_DEBUG
1089 	ext2_print_inode(ip);
1090 	ext4_ext_print_extent_tree_status(ip);
1091 #endif
1092 	bqrelse(bp);
1093 
1094 	/*
1095 	 * Initialize the vnode from the inode, check for aliases.
1096 	 * Note that the underlying vnode may have changed.
1097 	 */
1098 	if ((error = ext2_vinit(mp, &ext2_fifoops, &vp)) != 0) {
1099 		vput(vp);
1100 		*vpp = NULL;
1101 		return (error);
1102 	}
1103 
1104 	/*
1105 	 * Finish inode initialization.
1106 	 */
1107 
1108 	*vpp = vp;
1109 	return (0);
1110 }
1111 
1112 /*
1113  * File handle to vnode
1114  *
1115  * Have to be really careful about stale file handles:
1116  * - check that the inode number is valid
1117  * - call ext2_vget() to get the locked inode
1118  * - check for an unallocated inode (i_mode == 0)
1119  * - check that the given client host has export rights and return
1120  *   those rights via. exflagsp and credanonp
1121  */
1122 static int
1123 ext2_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
1124 {
1125 	struct inode *ip;
1126 	struct ufid *ufhp;
1127 	struct vnode *nvp;
1128 	struct m_ext2fs *fs;
1129 	int error;
1130 
1131 	ufhp = (struct ufid *)fhp;
1132 	fs = VFSTOEXT2(mp)->um_e2fs;
1133 	if (ufhp->ufid_ino < EXT2_ROOTINO ||
1134 	    ufhp->ufid_ino > fs->e2fs_gcount * fs->e2fs->e2fs_ipg)
1135 		return (ESTALE);
1136 
1137 	error = VFS_VGET(mp, ufhp->ufid_ino, LK_EXCLUSIVE, &nvp);
1138 	if (error) {
1139 		*vpp = NULLVP;
1140 		return (error);
1141 	}
1142 	ip = VTOI(nvp);
1143 	if (ip->i_mode == 0 ||
1144 	    ip->i_gen != ufhp->ufid_gen || ip->i_nlink <= 0) {
1145 		vput(nvp);
1146 		*vpp = NULLVP;
1147 		return (ESTALE);
1148 	}
1149 	*vpp = nvp;
1150 	vnode_create_vobject(*vpp, 0, curthread);
1151 	return (0);
1152 }
1153 
1154 /*
1155  * Write a superblock and associated information back to disk.
1156  */
1157 static int
1158 ext2_sbupdate(struct ext2mount *mp, int waitfor)
1159 {
1160 	struct m_ext2fs *fs = mp->um_e2fs;
1161 	struct ext2fs *es = fs->e2fs;
1162 	struct buf *bp;
1163 	int error = 0;
1164 
1165 	es->e2fs_bcount = fs->e2fs_bcount & 0xffffffff;
1166 	es->e2fs_rbcount = fs->e2fs_rbcount & 0xffffffff;
1167 	es->e2fs_fbcount = fs->e2fs_fbcount & 0xffffffff;
1168 	if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
1169 		es->e4fs_bcount_hi = fs->e2fs_bcount >> 32;
1170 		es->e4fs_rbcount_hi = fs->e2fs_rbcount >> 32;
1171 		es->e4fs_fbcount_hi = fs->e2fs_fbcount >> 32;
1172 	}
1173 
1174 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
1175 		ext2_sb_csum_set(fs);
1176 
1177 	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0, 0);
1178 	bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2fs));
1179 	if (waitfor == MNT_WAIT)
1180 		error = bwrite(bp);
1181 	else
1182 		bawrite(bp);
1183 
1184 	/*
1185 	 * The buffers for group descriptors, inode bitmaps and block bitmaps
1186 	 * are not busy at this point and are (hopefully) written by the
1187 	 * usual sync mechanism. No need to write them here.
1188 	 */
1189 	return (error);
1190 }
1191 int
1192 ext2_cgupdate(struct ext2mount *mp, int waitfor)
1193 {
1194 	struct m_ext2fs *fs = mp->um_e2fs;
1195 	struct buf *bp;
1196 	int i, j, g_count = 0, error = 0, allerror = 0;
1197 
1198 	allerror = ext2_sbupdate(mp, waitfor);
1199 
1200 	/* Update gd csums */
1201 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) ||
1202 	    EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
1203 		ext2_gd_csum_set(fs);
1204 
1205 	for (i = 0; i < fs->e2fs_gdbcount; i++) {
1206 		bp = getblk(mp->um_devvp, fsbtodb(fs,
1207 		    cg_location(fs, i)),
1208 		    fs->e2fs_bsize, 0, 0, 0);
1209 		if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
1210 			memcpy(bp->b_data, &fs->e2fs_gd[
1211 			    i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
1212 			    fs->e2fs_bsize);
1213 		} else {
1214 			for (j = 0; j < fs->e2fs_bsize / E2FS_REV0_GD_SIZE &&
1215 			    g_count < fs->e2fs_gcount; j++, g_count++)
1216 				memcpy(bp->b_data + j * E2FS_REV0_GD_SIZE,
1217 				    &fs->e2fs_gd[g_count], E2FS_REV0_GD_SIZE);
1218 		}
1219 		if (waitfor == MNT_WAIT)
1220 			error = bwrite(bp);
1221 		else
1222 			bawrite(bp);
1223 	}
1224 
1225 	if (!allerror && error)
1226 		allerror = error;
1227 	return (allerror);
1228 }
1229 
1230 /*
1231  * Return the root of a filesystem.
1232  */
1233 static int
1234 ext2_root(struct mount *mp, int flags, struct vnode **vpp)
1235 {
1236 	struct vnode *nvp;
1237 	int error;
1238 
1239 	error = VFS_VGET(mp, EXT2_ROOTINO, LK_EXCLUSIVE, &nvp);
1240 	if (error)
1241 		return (error);
1242 	*vpp = nvp;
1243 	return (0);
1244 }
1245