xref: /dragonfly/sys/vfs/ext2fs/ext2_vfsops.c (revision d50f9ae3)
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/buf2.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/mutex2.h>
57 #include <sys/nlookup.h>
58 
59 #include <vfs/ext2fs/fs.h>
60 #include <vfs/ext2fs/ext2_mount.h>
61 #include <vfs/ext2fs/inode.h>
62 
63 #include <vfs/ext2fs/ext2fs.h>
64 #include <vfs/ext2fs/ext2_dinode.h>
65 #include <vfs/ext2fs/ext2_extern.h>
66 #include <vfs/ext2fs/ext2_extents.h>
67 
68 SDT_PROVIDER_DECLARE(ext2fs);
69 /*
70  * ext2fs trace probe:
71  * arg0: verbosity. Higher numbers give more verbose messages
72  * arg1: Textual message
73  */
74 SDT_PROBE_DEFINE2(ext2fs, , vfsops, trace, "int", "char*");
75 SDT_PROBE_DEFINE2(ext2fs, , vfsops, ext2_cg_validate_error, "char*", "int");
76 SDT_PROBE_DEFINE1(ext2fs, , vfsops, ext2_compute_sb_data_error, "char*");
77 
78 static int	ext2_flushfiles(struct mount *mp, int flags);
79 static int	ext2_mountfs(struct vnode *, struct mount *);
80 static int	ext2_reload(struct mount *mp);
81 static int	ext2_sbupdate(struct ext2mount *, int);
82 static int	ext2_cgupdate(struct ext2mount *, int);
83 static int	ext2_init(struct vfsconf *);
84 static int	ext2_uninit(struct vfsconf *);
85 static vfs_unmount_t		ext2_unmount;
86 static vfs_root_t		ext2_root;
87 static vfs_statfs_t		ext2_statfs;
88 static vfs_statvfs_t		ext2_statvfs;
89 static vfs_sync_t		ext2_sync;
90 static vfs_vget_t		ext2_vget;
91 static vfs_fhtovp_t		ext2_fhtovp;
92 static vfs_vptofh_t		ext2_vptofh;
93 static vfs_checkexp_t		ext2_check_export;
94 static vfs_mount_t		ext2_mount;
95 
96 MALLOC_DEFINE(M_EXT2NODE, "ext2_node", "EXT2 vnode private part");
97 static MALLOC_DEFINE(M_EXT2MNT, "ext2_mount", "EXT2 mount structure");
98 
99 static struct vfsops ext2fs_vfsops = {
100 	.vfs_flags =		0,
101 	.vfs_mount =		ext2_mount,
102 	.vfs_unmount =		ext2_unmount,
103 	.vfs_root =		ext2_root,	/* root inode via vget */
104 	.vfs_statfs =		ext2_statfs,
105 	.vfs_statvfs =		ext2_statvfs,
106 	.vfs_sync =		ext2_sync,
107 	.vfs_vget =		ext2_vget,
108 	.vfs_fhtovp =		ext2_fhtovp,
109 	.vfs_vptofh =		ext2_vptofh,
110 	.vfs_checkexp =		ext2_check_export,
111 	.vfs_init =		ext2_init,
112 	.vfs_uninit =		ext2_uninit
113 };
114 
115 VFS_SET(ext2fs_vfsops, ext2fs, 0);
116 MODULE_VERSION(ext2fs, 1);
117 
118 static int	ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev,
119 		    int ronly);
120 static int	ext2_compute_sb_data(struct vnode * devvp,
121 		    struct ext2fs * es, struct m_ext2fs * fs);
122 
123 static int ext2fs_inode_hash_lock;
124 
125 /*
126  * VFS Operations.
127  *
128  * mount system call
129  */
130 static int
131 ext2_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
132 {
133 	struct ext2_args args;
134 	struct vnode *devvp;
135 	struct ext2mount *ump = NULL;
136 	struct m_ext2fs *fs;
137 	struct nlookupdata nd;
138 	mode_t accmode;
139 	int error, flags;
140 	size_t size;
141 
142 	if ((error = copyin(data, (caddr_t)&args, sizeof (struct ext2_args))) != 0)
143 		return (error);
144 
145 	/*
146 	 * If updating, check whether changing from read-only to
147 	 * read/write; if there is no device name, that's all we do.
148 	 */
149 	if (mp->mnt_flag & MNT_UPDATE) {
150 		ump = VFSTOEXT2(mp);
151 		fs = ump->um_e2fs;
152 		devvp = ump->um_devvp;
153 		error = 0;
154 		if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
155 			error = VFS_SYNC(mp, MNT_WAIT);
156 			if (error)
157 				return (error);
158 			flags = WRITECLOSE;
159 			if (mp->mnt_flag & MNT_FORCE)
160 				flags |= FORCECLOSE;
161 			if (vfs_busy(mp, LK_NOWAIT))
162 				return (EBUSY);
163 			error = ext2_flushfiles(mp, flags);
164 			vfs_unbusy(mp);
165 			if (error == 0 && fs->e2fs_wasvalid &&
166 			    ext2_cgupdate(ump, MNT_WAIT) == 0) {
167 				fs->e2fs->e2fs_state =
168 				    htole16((le16toh(fs->e2fs->e2fs_state) |
169 				    E2FS_ISCLEAN));
170 				ext2_sbupdate(ump, MNT_WAIT);
171 			}
172 			fs->e2fs_ronly = 1;
173 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
174 			VOP_OPEN(devvp, FREAD, FSCRED, NULL);
175 			VOP_CLOSE(devvp, FREAD | FWRITE, NULL);
176 			vn_unlock(devvp);
177 		}
178 		if (!error && (mp->mnt_flag & MNT_RELOAD))
179 			error = ext2_reload(mp);
180 		if (error)
181 			return (error);
182 		devvp = ump->um_devvp;
183 		if (fs->e2fs_ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
184 			if (ext2_check_sb_compat(fs->e2fs, devvp->v_rdev, 0))
185 				return (EPERM);
186 
187 			/*
188 			 * If upgrade to read-write by non-root, then verify
189 			 * that user has necessary permissions on the device.
190 			 */
191 			if (cred->cr_uid != 0) {
192 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
193 				error = VOP_EACCESS(devvp, VREAD | VWRITE, cred);
194 				if (error) {
195 					vn_unlock(devvp);
196 					return (error);
197 				}
198 				vn_unlock(devvp);
199 			}
200 
201 			if ((le16toh(fs->e2fs->e2fs_state) & E2FS_ISCLEAN) == 0 ||
202 			    (le16toh(fs->e2fs->e2fs_state) & E2FS_ERRORS)) {
203 				if (mp->mnt_flag & MNT_FORCE) {
204 					printf(
205 "WARNING: %s was not properly dismounted\n", fs->e2fs_fsmnt);
206 				} else {
207 					printf(
208 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
209 					    fs->e2fs_fsmnt);
210 					return (EPERM);
211 				}
212 			}
213 			fs->e2fs->e2fs_state =
214 			    htole16(le16toh(fs->e2fs->e2fs_state) & ~E2FS_ISCLEAN);
215 			(void)ext2_cgupdate(ump, MNT_WAIT);
216 			fs->e2fs_ronly = 0;
217 			mp->mnt_flag &= ~MNT_RDONLY;
218 
219 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
220 			VOP_OPEN(devvp, FREAD | FWRITE, FSCRED, NULL);
221 			VOP_CLOSE(devvp, FREAD, NULL);
222 			vn_unlock(devvp);
223 		}
224 		if (args.fspec == NULL) {
225 			/*
226 			 * Process export requests.
227 			 */
228 			return (vfs_export(mp, &ump->um_export, &args.export));
229 		}
230 	}
231 
232 	/*
233 	 * Not an update, or updating the name: look up the name
234 	 * and verify that it refers to a sensible disk device.
235 	 */
236 	devvp = NULL;
237 	error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
238 	if (error == 0)
239 		error = nlookup(&nd);
240 	if (error == 0)
241 		error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
242 	nlookup_done(&nd);
243 	if (error)
244 		return (error);
245 
246 	if (!vn_isdisk(devvp, &error)) {
247 		vrele(devvp);
248 		return (error);
249 	}
250 
251 	/*
252 	 * If mount by non-root, then verify that user has necessary
253 	 * permissions on the device.
254 	 *
255 	 * XXXRW: VOP_ACCESS() enough?
256 	 */
257 	if (cred->cr_uid != 0) {
258 		accmode = VREAD;
259 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
260 			accmode |= VWRITE;
261 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
262 		if ((error = VOP_EACCESS(devvp, accmode, cred)) != 0) {
263 			vput(devvp);
264 			return (error);
265 		}
266 		vn_unlock(devvp);
267 	}
268 
269 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
270 		error = ext2_mountfs(devvp, mp);
271 	} else {
272 		if (devvp != ump->um_devvp)
273 			error = EINVAL;	/* needs translation */
274 		else
275 			vrele(devvp);
276 	}
277 	if (error) {
278 		vrele(devvp);
279 		return (error);
280 	}
281 	ump = VFSTOEXT2(mp);
282 	fs = ump->um_e2fs;
283 
284 	/*
285 	 * Note that this strncpy() is ok because of a check at the start
286 	 * of ext2_mount().
287 	 */
288 	copyinstr(path, fs->e2fs_fsmnt, sizeof(fs->e2fs_fsmnt) - 1, &size);
289 	bzero(fs->e2fs_fsmnt + size, sizeof(fs->e2fs_fsmnt) - size);
290 	copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
291 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
292 	ext2_statfs(mp, &mp->mnt_stat, cred);
293 	return (0);
294 }
295 
296 static int
297 ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev, int ronly)
298 {
299 	uint32_t i, mask;
300 
301 	if (le16toh(es->e2fs_magic) != E2FS_MAGIC) {
302 		printf("ext2fs: %s: wrong magic number %#x (expected %#x)\n",
303 		    devtoname(dev), le16toh(es->e2fs_magic), E2FS_MAGIC);
304 		return (1);
305 	}
306 	if (le32toh(es->e2fs_rev) > E2FS_REV0) {
307 		mask = le32toh(es->e2fs_features_incompat) & ~(EXT2F_INCOMPAT_SUPP);
308 		if (mask) {
309 			printf("WARNING: mount of %s denied due to "
310 			    "unsupported optional features:\n", devtoname(dev));
311 			for (i = 0;
312 			    i < sizeof(incompat)/sizeof(struct ext2_feature);
313 			    i++)
314 				if (mask & incompat[i].mask)
315 					printf("%s ", incompat[i].name);
316 			printf("\n");
317 			return (1);
318 		}
319 		mask = le32toh(es->e2fs_features_rocompat) & ~EXT2F_ROCOMPAT_SUPP;
320 		if (!ronly && mask) {
321 			printf("WARNING: R/W mount of %s denied due to "
322 			    "unsupported optional features:\n", devtoname(dev));
323 			for (i = 0;
324 			    i < sizeof(ro_compat)/sizeof(struct ext2_feature);
325 			    i++)
326 				if (mask & ro_compat[i].mask)
327 					printf("%s ", ro_compat[i].name);
328 			printf("\n");
329 			return (1);
330 		}
331 	}
332 	return (0);
333 }
334 
335 static e4fs_daddr_t
336 ext2_cg_location(struct m_ext2fs *fs, int number)
337 {
338 	int cg, descpb, logical_sb, has_super = 0;
339 
340 	/*
341 	 * Adjust logical superblock block number.
342 	 * Godmar thinks: if the blocksize is greater than 1024, then
343 	 * the superblock is logically part of block zero.
344 	 */
345 	logical_sb = fs->e2fs_bsize > SBSIZE ? 0 : 1;
346 
347 	if (!EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_META_BG) ||
348 	    number < le32toh(fs->e2fs->e3fs_first_meta_bg))
349 		return (logical_sb + number + 1);
350 
351 	if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT))
352 		descpb = fs->e2fs_bsize / sizeof(struct ext2_gd);
353 	else
354 		descpb = fs->e2fs_bsize / E2FS_REV0_GD_SIZE;
355 
356 	cg = descpb * number;
357 
358 	if (ext2_cg_has_sb(fs, cg))
359 		has_super = 1;
360 
361 	return (has_super + cg * (e4fs_daddr_t)EXT2_BLOCKS_PER_GROUP(fs) +
362 	    le32toh(fs->e2fs->e2fs_first_dblock));
363 }
364 
365 static int
366 ext2_cg_validate(struct m_ext2fs *fs)
367 {
368 	uint64_t b_bitmap;
369 	uint64_t i_bitmap;
370 	uint64_t i_tables;
371 	uint64_t first_block, last_block, last_cg_block;
372 	struct ext2_gd *gd;
373 	unsigned int i, cg_count;
374 
375 	first_block = le32toh(fs->e2fs->e2fs_first_dblock);
376 	last_cg_block = ext2_cg_number_gdb(fs, 0);
377 	cg_count = fs->e2fs_gcount;
378 
379 	for (i = 0; i < fs->e2fs_gcount; i++) {
380 		gd = &fs->e2fs_gd[i];
381 
382 		if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_FLEX_BG) ||
383 		    i == fs->e2fs_gcount - 1) {
384 			last_block = fs->e2fs_bcount - 1;
385 		} else {
386 			last_block = first_block +
387 			    (EXT2_BLOCKS_PER_GROUP(fs) - 1);
388 		}
389 
390 		if ((cg_count == fs->e2fs_gcount) &&
391 		    !(le16toh(gd->ext4bgd_flags) & EXT2_BG_INODE_ZEROED))
392 			cg_count = i;
393 
394 		b_bitmap = e2fs_gd_get_b_bitmap(gd);
395 		if (b_bitmap == 0) {
396 			SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
397 			    "block bitmap is zero", i);
398 			return (EINVAL);
399 		}
400 		if (b_bitmap <= last_cg_block) {
401 			SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
402 			    "block bitmap overlaps gds", i);
403 			return (EINVAL);
404 		}
405 		if (b_bitmap < first_block || b_bitmap > last_block) {
406 			SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
407 			    "block bitmap not in group", i);
408 			return (EINVAL);
409 		}
410 
411 		i_bitmap = e2fs_gd_get_i_bitmap(gd);
412 		if (i_bitmap == 0) {
413 			SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
414 			    "inode bitmap is zero", i);
415 			return (EINVAL);
416 		}
417 		if (i_bitmap <= last_cg_block) {
418 			SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
419 			    "inode bitmap overlaps gds", i);
420 			return (EINVAL);
421 		}
422 		if (i_bitmap < first_block || i_bitmap > last_block) {
423 			SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
424 			    "inode bitmap not in group blk", i);
425 			return (EINVAL);
426 		}
427 
428 		i_tables = e2fs_gd_get_i_tables(gd);
429 		if (i_tables == 0) {
430 			SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
431 			    "inode table is zero", i);
432 			return (EINVAL);
433 		}
434 		if (i_tables <= last_cg_block) {
435 			SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
436 			    "inode talbes overlaps gds", i);
437 			return (EINVAL);
438 		}
439 		if (i_tables < first_block ||
440 		    i_tables + fs->e2fs_itpg - 1 > last_block) {
441 			SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
442 			    "inode tables not in group blk", i);
443 			return (EINVAL);
444 		}
445 
446 		if (!EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_FLEX_BG))
447 			first_block += EXT2_BLOCKS_PER_GROUP(fs);
448 	}
449 
450 	return (0);
451 }
452 
453 /*
454  * This computes the fields of the m_ext2fs structure from the
455  * data in the ext2fs structure read in.
456  */
457 static int
458 ext2_compute_sb_data(struct vnode *devvp, struct ext2fs *es,
459     struct m_ext2fs *fs)
460 {
461 	struct buf *bp;
462 	uint32_t e2fs_descpb, e2fs_gdbcount_alloc;
463 	int i, j;
464 	int g_count = 0;
465 	int error;
466 
467 	/* Check checksum features */
468 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) &&
469 	    EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
470 		SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
471 		    "incorrect checksum features combination");
472 		return (EINVAL);
473 	}
474 
475 	/* Precompute checksum seed for all metadata */
476 	ext2_sb_csum_set_seed(fs);
477 
478 	/* Verify sb csum if possible */
479 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
480 		error = ext2_sb_csum_verify(fs);
481 		if (error) {
482 			return (error);
483 		}
484 	}
485 
486 	/* Check for block size = 1K|2K|4K */
487 	if (le32toh(es->e2fs_log_bsize) > 2) {
488 		SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
489 		    "bad block size");
490 		return (EINVAL);
491 	}
492 
493 	fs->e2fs_bshift = EXT2_MIN_BLOCK_LOG_SIZE + le32toh(es->e2fs_log_bsize);
494 	fs->e2fs_bsize = 1U << fs->e2fs_bshift;
495 	fs->e2fs_fsbtodb = le32toh(es->e2fs_log_bsize) + 1;
496 	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
497 
498 	/* Check for fragment size */
499 	if (le32toh(es->e2fs_log_fsize) >
500 	    (EXT2_MAX_FRAG_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE)) {
501 		SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
502 		    "invalid log cluster size");
503 		return (EINVAL);
504 	}
505 
506 	fs->e2fs_fsize = EXT2_MIN_FRAG_SIZE << le32toh(es->e2fs_log_fsize);
507 	if (fs->e2fs_fsize != fs->e2fs_bsize) {
508 		SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
509 		    "fragment size != block size");
510 		return (EINVAL);
511 	}
512 
513 	fs->e2fs_fpb = fs->e2fs_bsize / fs->e2fs_fsize;
514 
515 	/* Check reserved gdt blocks for future filesystem expansion */
516 	if (le16toh(es->e2fs_reserved_ngdb) > (fs->e2fs_bsize / 4)) {
517 		SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
518 		    "number of reserved GDT blocks too large");
519 		return (EINVAL);
520 	}
521 
522 	if (le32toh(es->e2fs_rev) == E2FS_REV0) {
523 		fs->e2fs_isize = E2FS_REV0_INODE_SIZE;
524 	} else {
525 		fs->e2fs_isize = le16toh(es->e2fs_inode_size);
526 
527 		/*
528 		 * Check first ino.
529 		 */
530 		if (le32toh(es->e2fs_first_ino) < EXT2_FIRSTINO) {
531 			SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
532 			    "invalid first ino");
533 			return (EINVAL);
534 		}
535 
536 		/*
537 		 * Simple sanity check for superblock inode size value.
538 		 */
539 		if (EXT2_INODE_SIZE(fs) < E2FS_REV0_INODE_SIZE ||
540 		    EXT2_INODE_SIZE(fs) > fs->e2fs_bsize ||
541 		    (fs->e2fs_isize & (fs->e2fs_isize - 1)) != 0) {
542 			SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
543 			    "invalid inode size");
544 			return (EINVAL);
545 		}
546 	}
547 
548 	/* Check group descriptors */
549 	if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT) &&
550 	    le16toh(es->e3fs_desc_size) != E2FS_64BIT_GD_SIZE) {
551 		SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
552 		    "unsupported 64bit descriptor size");
553 		return (EINVAL);
554 	}
555 
556 	fs->e2fs_bpg = le32toh(es->e2fs_bpg);
557 	fs->e2fs_fpg = le32toh(es->e2fs_fpg);
558 	if (fs->e2fs_bpg == 0 || fs->e2fs_fpg == 0) {
559 		SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
560 		    "zero blocks/fragments per group");
561 		return (EINVAL);
562 	} else if (fs->e2fs_bpg != fs->e2fs_fpg) {
563 		SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
564 		    "blocks per group not equal fragments per group");
565 		return (EINVAL);
566 	}
567 
568 	if (fs->e2fs_bpg != fs->e2fs_bsize * 8) {
569 		SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
570 		    "non-standard group size unsupported");
571 		return (EINVAL);
572 	}
573 
574 	fs->e2fs_ipb = fs->e2fs_bsize / EXT2_INODE_SIZE(fs);
575 	if (fs->e2fs_ipb == 0 ||
576 	    fs->e2fs_ipb > fs->e2fs_bsize / E2FS_REV0_INODE_SIZE) {
577 		SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
578 		    "bad inodes per block size");
579 		return (EINVAL);
580 	}
581 
582 	fs->e2fs_ipg = le32toh(es->e2fs_ipg);
583 	if (fs->e2fs_ipg < fs->e2fs_ipb || fs->e2fs_ipg >  fs->e2fs_bsize * 8) {
584 		SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
585 		    "invalid inodes per group");
586 		return (EINVAL);
587 	}
588 
589 	fs->e2fs_itpg = fs->e2fs_ipg / fs->e2fs_ipb;
590 
591 	fs->e2fs_bcount = le32toh(es->e2fs_bcount);
592 	fs->e2fs_rbcount = le32toh(es->e2fs_rbcount);
593 	fs->e2fs_fbcount = le32toh(es->e2fs_fbcount);
594 	if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
595 		fs->e2fs_bcount |= (uint64_t)(le32toh(es->e4fs_bcount_hi)) << 32;
596 		fs->e2fs_rbcount |= (uint64_t)(le32toh(es->e4fs_rbcount_hi)) << 32;
597 		fs->e2fs_fbcount |= (uint64_t)(le32toh(es->e4fs_fbcount_hi)) << 32;
598 	}
599 	if (fs->e2fs_rbcount > fs->e2fs_bcount ||
600 	    fs->e2fs_fbcount > fs->e2fs_bcount) {
601 		SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
602 		    "invalid block count");
603 		return (EINVAL);
604 	}
605 
606 	fs->e2fs_ficount = le32toh(es->e2fs_ficount);
607 	if (fs->e2fs_ficount > le32toh(es->e2fs_icount)) {
608 		SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
609 		    "invalid number of free inodes");
610 		return (EINVAL);
611 	}
612 
613 	if (le32toh(es->e2fs_first_dblock) >= fs->e2fs_bcount) {
614 		SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
615 		    "first data block out of range");
616 		return (EINVAL);
617 	}
618 
619 	fs->e2fs_gcount = howmany(fs->e2fs_bcount -
620 	    le32toh(es->e2fs_first_dblock), EXT2_BLOCKS_PER_GROUP(fs));
621 	if (fs->e2fs_gcount > ((uint64_t)1 << 32) - EXT2_DESCS_PER_BLOCK(fs)) {
622 		SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
623 		    "groups count too large");
624 		return (EINVAL);
625 	}
626 
627 	/* Check for extra isize in big inodes. */
628 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_EXTRA_ISIZE) &&
629 	    EXT2_INODE_SIZE(fs) < sizeof(struct ext2fs_dinode)) {
630 		SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
631 		    "no space for extra inode timestamps");
632 		return (EINVAL);
633 	}
634 
635 	/* s_resuid / s_resgid ? */
636 
637 	if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
638 		e2fs_descpb = fs->e2fs_bsize / E2FS_64BIT_GD_SIZE;
639 		e2fs_gdbcount_alloc = howmany(fs->e2fs_gcount, e2fs_descpb);
640 	} else {
641 		e2fs_descpb = fs->e2fs_bsize / E2FS_REV0_GD_SIZE;
642 		e2fs_gdbcount_alloc = howmany(fs->e2fs_gcount,
643 		    fs->e2fs_bsize / sizeof(struct ext2_gd));
644 	}
645 	fs->e2fs_gdbcount = howmany(fs->e2fs_gcount, e2fs_descpb);
646 	fs->e2fs_gd = malloc(e2fs_gdbcount_alloc * fs->e2fs_bsize,
647 	    M_EXT2MNT, M_WAITOK | M_ZERO);
648 	fs->e2fs_contigdirs = malloc(fs->e2fs_gcount *
649 	    sizeof(*fs->e2fs_contigdirs), M_EXT2MNT, M_WAITOK | M_ZERO);
650 
651 	for (i = 0; i < fs->e2fs_gdbcount; i++) {
652 		error = bread(devvp, fsbtodoff(fs, ext2_cg_location(fs, i)),
653 		    fs->e2fs_bsize, &bp);
654 		if (error) {
655 			/*
656 			 * fs->e2fs_gd and fs->e2fs_contigdirs
657 			 * will be freed later by the caller,
658 			 * because this function could be called from
659 			 * MNT_UPDATE path.
660 			 */
661 			brelse(bp);
662 			return (error);
663 		}
664 		if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
665 			memcpy(&fs->e2fs_gd[
666 			    i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
667 			    bp->b_data, fs->e2fs_bsize);
668 		} else {
669 			for (j = 0; j < e2fs_descpb &&
670 			    g_count < fs->e2fs_gcount; j++, g_count++)
671 				memcpy(&fs->e2fs_gd[g_count],
672 				    bp->b_data + j * E2FS_REV0_GD_SIZE,
673 				    E2FS_REV0_GD_SIZE);
674 		}
675 		brelse(bp);
676 		bp = NULL;
677 	}
678 
679 	/* Validate cgs consistency */
680 	error = ext2_cg_validate(fs);
681 	if (error)
682 		return (error);
683 
684 	/* Verfy cgs csum */
685 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) ||
686 	    EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
687 		error = ext2_gd_csum_verify(fs, devvp->v_rdev);
688 		if (error)
689 			return (error);
690 	}
691 	/* Initialization for the ext2 Orlov allocator variant. */
692 	fs->e2fs_total_dir = 0;
693 	for (i = 0; i < fs->e2fs_gcount; i++)
694 		fs->e2fs_total_dir += e2fs_gd_get_ndirs(&fs->e2fs_gd[i]);
695 
696 	if (le32toh(es->e2fs_rev) == E2FS_REV0 ||
697 	    !EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_LARGEFILE))
698 		fs->e2fs_maxfilesize = 0x7fffffff;
699 	else {
700 		fs->e2fs_maxfilesize = 0xffffffffffff;
701 		if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_HUGE_FILE))
702 			fs->e2fs_maxfilesize = 0x7fffffffffffffff;
703 	}
704 	if (le32toh(es->e4fs_flags) & E2FS_UNSIGNED_HASH) {
705 		fs->e2fs_uhash = 3;
706 	} else if ((le32toh(es->e4fs_flags) & E2FS_SIGNED_HASH) == 0) {
707 #ifdef __CHAR_UNSIGNED__
708 		es->e4fs_flags = htole32(le32toh(es->e4fs_flags) | E2FS_UNSIGNED_HASH);
709 		fs->e2fs_uhash = 3;
710 #else
711 		es->e4fs_flags = htole32(le32toh(es->e4fs_flags) | E2FS_SIGNED_HASH);
712 #endif
713 	}
714 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
715 		error = ext2_sb_csum_verify(fs);
716 
717 	return (error);
718 }
719 
720 struct scaninfo {
721 	int rescan;
722 	int allerror;
723 	int waitfor;
724 	struct vnode *devvp;
725 	struct m_ext2fs *fs;
726 };
727 
728 static int
729 ext2_reload_scan(struct mount *mp, struct vnode *vp, void *data)
730 {
731 	struct scaninfo *info = data;
732 	struct inode *ip;
733 	struct buf *bp;
734 	int error;
735 
736 	/*
737 	 * Try to recycle
738 	 */
739 	if (vrecycle(vp))
740 		return (0);
741 
742 	/*
743 	 * Step 1: invalidate all cached file data.
744 	 */
745 	if (vinvalbuf(vp, 0, 0, 0))
746 		panic("ext2_reload: dirty2");
747 	/*
748 	 * Step 2: re-read inode data for all active vnodes.
749 	 */
750 	ip = VTOI(vp);
751 	error = bread(info->devvp,
752 	    fsbtodoff(info->fs, ino_to_fsba(info->fs, ip->i_number)),
753 	    (int)info->fs->e2fs_bsize, &bp);
754 	if (error) {
755 		brelse(bp);
756 		return (error);
757 	}
758 
759 	error = ext2_ei2i((struct ext2fs_dinode *)((char *)bp->b_data +
760 	    EXT2_INODE_SIZE(info->fs) * ino_to_fsbo(info->fs, ip->i_number)),
761 	    ip);
762 
763 	brelse(bp);
764 	return (error);
765 }
766 
767 /*
768  * Reload all incore data for a filesystem (used after running fsck on
769  * the root filesystem and finding things to fix). The filesystem must
770  * be mounted read-only.
771  *
772  * Things to do to update the mount:
773  *	1) invalidate all cached meta-data.
774  *	2) re-read superblock from disk.
775  *	3) invalidate all cluster summary information.
776  *	4) invalidate all inactive vnodes.
777  *	5) invalidate all cached file data.
778  *	6) re-read inode data for all active vnodes.
779  * XXX we are missing some steps, in particular # 3, this has to be reviewed.
780  */
781 static int
782 ext2_reload(struct mount *mp)
783 {
784 	struct vnode *devvp;
785 	struct buf *bp;
786 	struct ext2fs *es;
787 	struct m_ext2fs *fs;
788 	struct csum *sump;
789 	struct scaninfo scaninfo;
790 	int error, i;
791 	int32_t *lp;
792 
793 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
794 		return (EINVAL);
795 	/*
796 	 * Step 1: invalidate all cached meta-data.
797 	 */
798 	devvp = VFSTOEXT2(mp)->um_devvp;
799 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
800 	if (vinvalbuf(devvp, 0, 0, 0) != 0)
801 		panic("ext2_reload: dirty1");
802 	vn_unlock(devvp);
803 
804 	/*
805 	 * Step 2: re-read superblock from disk.
806 	 * constants have been adjusted for ext2
807 	 */
808 	if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0) {
809 		brelse(bp);
810 		return (error);
811 	}
812 	es = (struct ext2fs *)bp->b_data;
813 	if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) {
814 		brelse(bp);
815 		return (EIO);		/* XXX needs translation */
816 	}
817 	fs = VFSTOEXT2(mp)->um_e2fs;
818 	bcopy(bp->b_data, fs->e2fs, sizeof(struct ext2fs));
819 
820 	if ((error = ext2_compute_sb_data(devvp, es, fs)) != 0) {
821 		brelse(bp);
822 		return (error);
823 	}
824 #ifdef UNKLAR
825 	if (fs->fs_sbsize < SBSIZE)
826 		bp->b_flags |= B_INVAL;
827 #endif
828 	brelse(bp);
829 
830 	/*
831 	 * Step 3: invalidate all cluster summary information.
832 	 */
833 	if (fs->e2fs_contigsumsize > 0) {
834 		lp = fs->e2fs_maxcluster;
835 		sump = fs->e2fs_clustersum;
836 		for (i = 0; i < fs->e2fs_gcount; i++, sump++) {
837 			*lp++ = fs->e2fs_contigsumsize;
838 			sump->cs_init = 0;
839 			bzero(sump->cs_sum, fs->e2fs_contigsumsize + 1);
840 		}
841 	}
842 
843 	scaninfo.rescan = 1;
844 	scaninfo.devvp = devvp;
845 	scaninfo.fs = fs;
846 	while (error == 0 && scaninfo.rescan) {
847 		scaninfo.rescan = 0;
848 		error = vmntvnodescan(mp, VMSC_GETVX, NULL, ext2_reload_scan,
849 		    &scaninfo);
850 	}
851 	return (error);
852 }
853 
854 /*
855  * Common code for mount and mountroot.
856  */
857 static int
858 ext2_mountfs(struct vnode *devvp, struct mount *mp)
859 {
860 	struct ext2mount *ump;
861 	struct buf *bp;
862 	struct m_ext2fs *fs;
863 	struct ext2fs *es;
864 	struct cdev *dev = devvp->v_rdev;
865 	struct csum *sump;
866 	int error;
867 	int ronly;
868 	int i;
869 	u_long size;
870 	int32_t *lp;
871 	int32_t e2fs_maxcontig;
872 
873 	/*
874 	 * Disallow multiple mounts of the same device.
875 	 * Disallow mounting of a device that is currently in use
876 	 * (except for root, which might share swap device for miniroot).
877 	 * Flush out any old buffers remaining from a previous use.
878 	 */
879 	if ((error = vfs_mountedon(devvp)) != 0)
880 		return (error);
881 	if (vcount(devvp) > 0)
882 		return (EBUSY);
883 	if ((error = vinvalbuf(devvp, V_SAVE, 0, 0)) != 0)
884 		return (error);
885 #ifdef READONLY
886 	/* Turn on this to force it to be read-only. */
887 	mp->mnt_flag |= MNT_RDONLY;
888 #endif
889 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
890 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
891 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD | FWRITE, FSCRED, NULL);
892 	vn_unlock(devvp);
893 	if (error)
894 		return (error);
895 
896 	if (devvp->v_rdev->si_iosize_max != 0)
897 		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
898 	if (mp->mnt_iosize_max > MAXPHYS)
899 		mp->mnt_iosize_max = MAXPHYS;
900 
901 	bp = NULL;
902 	ump = NULL;
903 	if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0)
904 		goto out;
905 	es = (struct ext2fs *)bp->b_data;
906 	if (ext2_check_sb_compat(es, dev, ronly) != 0) {
907 		error = EINVAL;		/* XXX needs translation */
908 		goto out;
909 	}
910 	if ((le16toh(es->e2fs_state) & E2FS_ISCLEAN) == 0 ||
911 	    (le16toh(es->e2fs_state) & E2FS_ERRORS)) {
912 		if (ronly || (mp->mnt_flag & MNT_FORCE)) {
913 			printf(
914 "WARNING: Filesystem was not properly dismounted\n");
915 		} else {
916 			printf(
917 "WARNING: R/W mount denied.  Filesystem is not clean - run fsck\n");
918 			error = EPERM;
919 			goto out;
920 		}
921 	}
922 	ump = malloc(sizeof(*ump), M_EXT2MNT, M_WAITOK | M_ZERO);
923 
924 	/*
925 	 * I don't know whether this is the right strategy. Note that
926 	 * we dynamically allocate both an m_ext2fs and an ext2fs
927 	 * while Linux keeps the super block in a locked buffer.
928 	 */
929 	ump->um_e2fs = malloc(sizeof(struct m_ext2fs),
930 	    M_EXT2MNT, M_WAITOK | M_ZERO);
931 	ump->um_e2fs->e2fs = malloc(sizeof(struct ext2fs),
932 	    M_EXT2MNT, M_WAITOK);
933 	mtx_init(EXT2_MTX(ump), "EXT2FS Lock");
934 	bcopy(es, ump->um_e2fs->e2fs, (u_int)sizeof(struct ext2fs));
935 	if ((error = ext2_compute_sb_data(devvp, ump->um_e2fs->e2fs, ump->um_e2fs)))
936 		goto out;
937 
938 	/*
939 	 * Calculate the maximum contiguous blocks and size of cluster summary
940 	 * array.  In FFS this is done by newfs; however, the superblock
941 	 * in ext2fs doesn't have these variables, so we can calculate
942 	 * them here.
943 	 */
944 	e2fs_maxcontig = MAX(1, MAXPHYS / ump->um_e2fs->e2fs_bsize);
945 	ump->um_e2fs->e2fs_contigsumsize = MIN(e2fs_maxcontig, EXT2_MAXCONTIG);
946 	if (ump->um_e2fs->e2fs_contigsumsize > 0) {
947 		size = ump->um_e2fs->e2fs_gcount * sizeof(int32_t);
948 		ump->um_e2fs->e2fs_maxcluster = malloc(size, M_EXT2MNT, M_WAITOK);
949 		size = ump->um_e2fs->e2fs_gcount * sizeof(struct csum);
950 		ump->um_e2fs->e2fs_clustersum = malloc(size, M_EXT2MNT, M_WAITOK);
951 		lp = ump->um_e2fs->e2fs_maxcluster;
952 		sump = ump->um_e2fs->e2fs_clustersum;
953 		for (i = 0; i < ump->um_e2fs->e2fs_gcount; i++, sump++) {
954 			*lp++ = ump->um_e2fs->e2fs_contigsumsize;
955 			sump->cs_init = 0;
956 			sump->cs_sum = malloc((ump->um_e2fs->e2fs_contigsumsize + 1) *
957 			    sizeof(int32_t), M_EXT2MNT, M_WAITOK | M_ZERO);
958 		}
959 	}
960 
961 	brelse(bp);
962 	bp = NULL;
963 	fs = ump->um_e2fs;
964 	fs->e2fs_ronly = ronly;	/* ronly is set according to mnt_flags */
965 
966 	/*
967 	 * If the fs is not mounted read-only, make sure the super block is
968 	 * always written back on a sync().
969 	 */
970 	fs->e2fs_wasvalid = le16toh(fs->e2fs->e2fs_state) & E2FS_ISCLEAN ? 1 : 0;
971 	if (ronly == 0) {
972 		fs->e2fs_fmod = 1;	/* mark it modified and set fs invalid */
973 		fs->e2fs->e2fs_state =
974 		    htole16(le16toh(fs->e2fs->e2fs_state) & ~E2FS_ISCLEAN);
975 	}
976 	mp->mnt_data = (qaddr_t)ump;
977 	mp->mnt_stat.f_fsid.val[0] = devid_from_dev(dev);
978 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
979 	mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
980 	mp->mnt_flag |= MNT_LOCAL;
981 	ump->um_mountp = mp;
982 	ump->um_dev = dev;
983 	ump->um_devvp = devvp;
984 
985 	/*
986 	 * Setting those two parameters allowed us to use
987 	 * ufs_bmap w/o changse!
988 	 */
989 	ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
990 	ump->um_bptrtodb = le32toh(fs->e2fs->e2fs_log_bsize) + 1;
991 	ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
992 	dev->si_mountpoint = mp;
993 
994 	vfs_add_vnodeops(mp, &ext2_vnodeops, &mp->mnt_vn_norm_ops);
995 	vfs_add_vnodeops(mp, &ext2_specops, &mp->mnt_vn_spec_ops);
996 	vfs_add_vnodeops(mp, &ext2_fifoops, &mp->mnt_vn_fifo_ops);
997 
998 	if (ronly == 0)
999 		ext2_sbupdate(ump, MNT_WAIT);
1000 	return (0);
1001 out:
1002 	if (bp)
1003 		brelse(bp);
1004 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1005 	VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NULL);
1006 	vn_unlock(devvp);
1007 	if (ump) {
1008 		mtx_uninit(EXT2_MTX(ump));
1009 		free(ump->um_e2fs->e2fs_gd, M_EXT2MNT);
1010 		free(ump->um_e2fs->e2fs_contigdirs, M_EXT2MNT);
1011 		free(ump->um_e2fs->e2fs, M_EXT2MNT);
1012 		free(ump->um_e2fs, M_EXT2MNT);
1013 		free(ump, M_EXT2MNT);
1014 		mp->mnt_data = NULL;
1015 	}
1016 	return (error);
1017 }
1018 
1019 /*
1020  * Unmount system call.
1021  */
1022 static int
1023 ext2_unmount(struct mount *mp, int mntflags)
1024 {
1025 	struct ext2mount *ump;
1026 	struct m_ext2fs *fs;
1027 	struct csum *sump;
1028 	int error, flags, i, ronly;
1029 
1030 	flags = 0;
1031 	if (mntflags & MNT_FORCE) {
1032 		if (mp->mnt_flag & MNT_ROOTFS)
1033 			return (EINVAL);
1034 		flags |= FORCECLOSE;
1035 	}
1036 	if ((error = ext2_flushfiles(mp, flags)) != 0)
1037 		return (error);
1038 	ump = VFSTOEXT2(mp);
1039 	fs = ump->um_e2fs;
1040 	ronly = fs->e2fs_ronly;
1041 	if (ronly == 0 && ext2_cgupdate(ump, MNT_WAIT) == 0) {
1042 		if (fs->e2fs_wasvalid)
1043 			fs->e2fs->e2fs_state =
1044 			    htole16(le16toh(fs->e2fs->e2fs_state) | E2FS_ISCLEAN);
1045 		ext2_sbupdate(ump, MNT_WAIT);
1046 	}
1047 
1048 	ump->um_devvp->v_rdev->si_mountpoint = NULL;
1049 
1050 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1051 	error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD | FWRITE, NULL);
1052 	vn_unlock(ump->um_devvp);
1053 
1054 	vrele(ump->um_devvp);
1055 	sump = fs->e2fs_clustersum;
1056 	for (i = 0; i < fs->e2fs_gcount; i++, sump++)
1057 		free(sump->cs_sum, M_EXT2MNT);
1058 	free(fs->e2fs_clustersum, M_EXT2MNT);
1059 	free(fs->e2fs_maxcluster, M_EXT2MNT);
1060 	free(fs->e2fs_gd, M_EXT2MNT);
1061 	free(fs->e2fs_contigdirs, M_EXT2MNT);
1062 	free(fs->e2fs, M_EXT2MNT);
1063 	free(fs, M_EXT2MNT);
1064 	free(ump, M_EXT2MNT);
1065 	mp->mnt_data = NULL;
1066 	mp->mnt_flag &= ~MNT_LOCAL;
1067 	return (error);
1068 }
1069 
1070 /*
1071  * Flush out all the files in a filesystem.
1072  */
1073 static int
1074 ext2_flushfiles(struct mount *mp, int flags)
1075 {
1076 	int error;
1077 
1078 	error = vflush(mp, 0, flags);
1079 	return (error);
1080 }
1081 
1082 /*
1083  * Get filesystem statistics.
1084  */
1085 static int
1086 ext2_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
1087 {
1088 	struct ext2mount *ump;
1089 	struct m_ext2fs *fs;
1090 	uint32_t overhead, overhead_per_group, ngdb;
1091 	int i, ngroups;
1092 
1093 	ump = VFSTOEXT2(mp);
1094 	fs = ump->um_e2fs;
1095 	if (le16toh(fs->e2fs->e2fs_magic) != E2FS_MAGIC)
1096 		panic("ext2_statfs");
1097 
1098 	/*
1099 	 * Compute the overhead (FS structures)
1100 	 */
1101 	overhead_per_group =
1102 	    1 /* block bitmap */ +
1103 	    1 /* inode bitmap */ +
1104 	    fs->e2fs_itpg;
1105 	overhead = le32toh(fs->e2fs->e2fs_first_dblock) +
1106 	    fs->e2fs_gcount * overhead_per_group;
1107 	if (le32toh(fs->e2fs->e2fs_rev) > E2FS_REV0 &&
1108 	    le32toh(fs->e2fs->e2fs_features_rocompat) & EXT2F_ROCOMPAT_SPARSESUPER) {
1109 		for (i = 0, ngroups = 0; i < fs->e2fs_gcount; i++) {
1110 			if (ext2_cg_has_sb(fs, i))
1111 				ngroups++;
1112 		}
1113 	} else {
1114 		ngroups = fs->e2fs_gcount;
1115 	}
1116 	ngdb = fs->e2fs_gdbcount;
1117 	if (le32toh(fs->e2fs->e2fs_rev) > E2FS_REV0 &&
1118 	    le32toh(fs->e2fs->e2fs_features_compat) & EXT2F_COMPAT_RESIZE)
1119 		ngdb += le16toh(fs->e2fs->e2fs_reserved_ngdb);
1120 	overhead += ngroups * (1 /* superblock */ + ngdb);
1121 
1122 	sbp->f_type = mp->mnt_vfc->vfc_typenum;
1123 	sbp->f_bsize = EXT2_FRAG_SIZE(fs);
1124 	sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
1125 	sbp->f_blocks = fs->e2fs_bcount - overhead;
1126 	sbp->f_bfree = fs->e2fs_fbcount;
1127 	sbp->f_bavail = sbp->f_bfree - fs->e2fs_rbcount;
1128 	sbp->f_files = le32toh(fs->e2fs->e2fs_icount);
1129 	sbp->f_ffree = fs->e2fs_ficount;
1130 	if (sbp != &mp->mnt_stat) {
1131 		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
1132 		    (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
1133 	}
1134 	return (0);
1135 }
1136 
1137 static int
1138 ext2_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
1139 {
1140 	struct ext2mount *ump;
1141 	struct m_ext2fs *fs;
1142 	uint32_t overhead, overhead_per_group, ngdb;
1143 	int i, ngroups;
1144 
1145 	ump = VFSTOEXT2(mp);
1146 	fs = ump->um_e2fs;
1147 	if (le16toh(fs->e2fs->e2fs_magic) != E2FS_MAGIC)
1148 		panic("ext2_statfs");
1149 
1150 	/*
1151 	 * Compute the overhead (FS structures)
1152 	 */
1153 	overhead_per_group =
1154 	    1 /* block bitmap */ +
1155 	    1 /* inode bitmap */ +
1156 	    fs->e2fs_itpg;
1157 	overhead = le32toh(fs->e2fs->e2fs_first_dblock) +
1158 	    fs->e2fs_gcount * overhead_per_group;
1159 	if (le32toh(fs->e2fs->e2fs_rev) > E2FS_REV0 &&
1160 	    le32toh(fs->e2fs->e2fs_features_rocompat) & EXT2F_ROCOMPAT_SPARSESUPER) {
1161 		for (i = 0, ngroups = 0; i < fs->e2fs_gcount; i++) {
1162 			if (ext2_cg_has_sb(fs, i))
1163 				ngroups++;
1164 		}
1165 	} else {
1166 		ngroups = fs->e2fs_gcount;
1167 	}
1168 	ngdb = fs->e2fs_gdbcount;
1169 	if (le32toh(fs->e2fs->e2fs_rev) > E2FS_REV0 &&
1170 	    le32toh(fs->e2fs->e2fs_features_compat) & EXT2F_COMPAT_RESIZE)
1171 		ngdb += le16toh(fs->e2fs->e2fs_reserved_ngdb);
1172 	overhead += ngroups * (1 /* superblock */ + ngdb);
1173 
1174 	sbp->f_type = mp->mnt_vfc->vfc_typenum;
1175 	sbp->f_bsize = EXT2_FRAG_SIZE(fs);
1176 	sbp->f_frsize = EXT2_BLOCK_SIZE(fs);
1177 	sbp->f_blocks = fs->e2fs_bcount - overhead;
1178 	sbp->f_bfree = fs->e2fs_fbcount;
1179 	sbp->f_bavail = sbp->f_bfree - fs->e2fs_rbcount;
1180 	sbp->f_files = le32toh(fs->e2fs->e2fs_icount);
1181 	sbp->f_ffree = fs->e2fs_ficount;
1182 	return (0);
1183 }
1184 
1185 static int
1186 ext2_sync_scan(struct mount *mp, struct vnode *vp, void *data)
1187 {
1188 	struct scaninfo *info = data;
1189 	struct inode *ip;
1190 	int error;
1191 
1192 	ip = VTOI(vp);
1193 	if (vp->v_type == VNON ||
1194 	    ((ip->i_flag &
1195 	    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1196 	    (RB_EMPTY(&vp->v_rbdirty_tree) || (info->waitfor & MNT_LAZY)))) {
1197 		return (0);
1198 	}
1199 	if ((error = VOP_FSYNC(vp, info->waitfor, 0)) != 0)
1200 		info->allerror = error;
1201 	return (0);
1202 }
1203 
1204 /*
1205  * Go through the disk queues to initiate sandbagged IO;
1206  * go through the inodes to write those that have been modified;
1207  * initiate the writing of the super block if it has been modified.
1208  *
1209  * Note: we are always called with the filesystem marked `MPBUSY'.
1210  */
1211 static int
1212 ext2_sync(struct mount *mp, int waitfor)
1213 {
1214 	struct ext2mount *ump = VFSTOEXT2(mp);
1215 	struct m_ext2fs *fs;
1216 	struct scaninfo scaninfo;
1217 	int error;
1218 
1219 	fs = ump->um_e2fs;
1220 	if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) {		/* XXX */
1221 		panic("ext2_sync: rofs mod fs=%s", fs->e2fs_fsmnt);
1222 	}
1223 
1224 	/*
1225 	 * Write back each (modified) inode.
1226 	 */
1227 	scaninfo.allerror = 0;
1228 	scaninfo.rescan = 1;
1229 	scaninfo.waitfor = waitfor;
1230 	while (scaninfo.rescan) {
1231 		scaninfo.rescan = 0;
1232 		vmntvnodescan(mp, VMSC_GETVP | VMSC_NOWAIT,
1233 			      NULL, ext2_sync_scan, &scaninfo);
1234 	}
1235 
1236 	/*
1237 	 * Force stale filesystem control information to be flushed.
1238 	 */
1239 	if ((waitfor & MNT_LAZY) == 0) {
1240 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1241 		if ((error = VOP_FSYNC(ump->um_devvp, waitfor, 0)) != 0)
1242 			scaninfo.allerror = error;
1243 		vn_unlock(ump->um_devvp);
1244 	}
1245 
1246 	/*
1247 	 * Write back modified superblock.
1248 	 */
1249 	if (fs->e2fs_fmod != 0) {
1250 		fs->e2fs_fmod = 0;
1251 		fs->e2fs->e2fs_wtime = htole32(time_second);
1252 		if ((error = ext2_cgupdate(ump, waitfor)) != 0)
1253 			scaninfo.allerror = error;
1254 	}
1255 	return (scaninfo.allerror);
1256 }
1257 
1258 int
1259 ext2_alloc_vnode(struct mount *mp, ino_t ino, struct vnode **vpp)
1260 {
1261 	struct ext2mount *ump;
1262 	struct vnode *vp;
1263 	struct inode *ip;
1264 	int error;
1265 
1266 	ump = VFSTOEXT2(mp);
1267 	/*
1268 	 * Lock out the creation of new entries in the FFS hash table in
1269 	 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
1270 	 * may occur!
1271 	 */
1272 	if (ext2fs_inode_hash_lock) {
1273 		while (ext2fs_inode_hash_lock) {
1274 			ext2fs_inode_hash_lock = -1;
1275 			tsleep(&ext2fs_inode_hash_lock, 0, "e2vget", 0);
1276 		}
1277 		return (-1);
1278 	}
1279 	ext2fs_inode_hash_lock = 1;
1280 
1281 	ip = malloc(sizeof(struct inode), M_EXT2NODE, M_WAITOK | M_ZERO);
1282 
1283 	/* Allocate a new vnode/inode. */
1284 	if ((error = getnewvnode(VT_EXT2FS, mp, &vp, VLKTIMEOUT,
1285 	    LK_CANRECURSE)) != 0) {
1286 		if (ext2fs_inode_hash_lock < 0)
1287 			wakeup(&ext2fs_inode_hash_lock);
1288 		ext2fs_inode_hash_lock = 0;
1289 		*vpp = NULL;
1290 		free(ip, M_EXT2NODE);
1291 		return (error);
1292 	}
1293 	//lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1294 	vp->v_data = ip;
1295 	ip->i_vnode = vp;
1296 	ip->i_e2fs = ump->um_e2fs;
1297 	ip->i_dev = ump->um_dev;
1298 	ip->i_ump = ump;
1299 	ip->i_number = ino;
1300 	ip->i_block_group = ino_to_cg(ip->i_e2fs, ino);
1301 	ip->i_next_alloc_block = 0;
1302 	ip->i_next_alloc_goal = 0;
1303 
1304 	/*
1305 	 * Put it onto its hash chain.  Since our vnode is locked, other
1306 	 * requests for this inode will block if they arrive while we are
1307 	 * sleeping waiting for old data structures to be purged or for the
1308 	 * contents of the disk portion of this inode to be read.
1309 	 */
1310 	if (ext2_ihashins(ip)) {
1311 		printf("ext2_alloc_vnode: ihashins collision, retrying inode %ld\n",
1312 		    (long)ip->i_number);
1313 		*vpp = NULL;
1314 		vp->v_type = VBAD;
1315 		vx_put(vp);
1316 		free(ip, M_EXT2NODE);
1317 		return (-1);
1318 	}
1319 
1320 	if (ext2fs_inode_hash_lock < 0)
1321 		wakeup(&ext2fs_inode_hash_lock);
1322 	ext2fs_inode_hash_lock = 0;
1323 	*vpp = vp;
1324 
1325 	return (0);
1326 }
1327 
1328 /*
1329  * Look up an EXT2FS dinode number to find its incore vnode, otherwise read it
1330  * in from disk.  If it is in core, wait for the lock bit to clear, then
1331  * return the inode locked.  Detection and handling of mount points must be
1332  * done by the calling routine.
1333  */
1334 static int
1335 ext2_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
1336 {
1337 	struct m_ext2fs *fs;
1338 	struct inode *ip;
1339 	struct ext2mount *ump;
1340 	struct buf *bp;
1341 	struct vnode *vp;
1342 	unsigned int i, used_blocks;
1343 	int error;
1344 
1345 	ump = VFSTOEXT2(mp);
1346 restart:
1347 	if ((*vpp = ext2_ihashget(ump->um_dev, ino)) != NULL)
1348 		return (0);
1349 	if (ext2_alloc_vnode(mp, ino, &vp) == -1)
1350 		goto restart;
1351 	ip = VTOI(vp);
1352 	fs = ip->i_e2fs;
1353 
1354 	/* Read in the disk contents for the inode, copy into the inode. */
1355 	if ((error = bread(ump->um_devvp, fsbtodoff(fs, ino_to_fsba(fs, ino)),
1356 	    (int)fs->e2fs_bsize, &bp)) != 0) {
1357 		/*
1358 		 * The inode does not contain anything useful, so it would
1359 		 * be misleading to leave it on its hash chain. With mode
1360 		 * still zero, it will be unlinked and returned to the free
1361 		 * list by vput().
1362 		 */
1363 		vp->v_type = VBAD;
1364 		brelse(bp);
1365 		vx_put(vp);
1366 		*vpp = NULL;
1367 		return (error);
1368 	}
1369 	/* convert ext2 inode to dinode */
1370 	error = ext2_ei2i((struct ext2fs_dinode *)((char *)bp->b_data +
1371 	    EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ino)), ip);
1372 	if (error) {
1373 		brelse(bp);
1374 		vx_put(vp);
1375 		*vpp = NULL;
1376 		return (error);
1377 	}
1378 
1379 	/*
1380 	 * Now we want to make sure that block pointers for unused
1381 	 * blocks are zeroed out - ext2_balloc depends on this
1382 	 * although for regular files and directories only
1383 	 *
1384 	 * If IN_E4EXTENTS is enabled, unused blocks are not zeroed
1385 	 * out because we could corrupt the extent tree.
1386 	 */
1387 	if (!(ip->i_flag & IN_E4EXTENTS) &&
1388 	    (S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode))) {
1389 		used_blocks = howmany(ip->i_size, fs->e2fs_bsize);
1390 		for (i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
1391 			ip->i_db[i] = 0;
1392 	}
1393 #ifdef EXT2FS_PRINT_EXTENTS
1394 	ext2_print_inode(ip);
1395 	ext4_ext_print_extent_tree_status(ip);
1396 #endif
1397 	bqrelse(bp);
1398 
1399 	/*
1400 	 * Initialize the vnode from the inode, check for aliases.
1401 	 * Note that the underlying vnode may have changed.
1402 	 */
1403 	if ((error = ext2_vinit(mp, &vp)) != 0) {
1404 		vx_put(vp);
1405 		*vpp = NULL;
1406 		return (error);
1407 	}
1408 
1409 	/*
1410 	 * Finish inode initialization now that aliasing has been resolved.
1411 	 */
1412 	vref(ip->i_devvp);
1413 	/*
1414 	 * Set up a generation number for this inode if it does not
1415 	 * already have one. This should only happen on old filesystems.
1416 	 */
1417 	if (ip->i_gen == 0) {
1418 		ip->i_gen = krandom() / 2 + 1;
1419 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1420 			ip->i_flag |= IN_MODIFIED;
1421 	}
1422 	/*
1423 	 * Return the locked and refd vnode.
1424 	 */
1425 	vx_downgrade(vp);	/* downgrade VX lock to VN lock */
1426 	*vpp = vp;
1427 
1428 	return (0);
1429 }
1430 
1431 /*
1432  * File handle to vnode
1433  *
1434  * Have to be really careful about stale file handles:
1435  * - check that the inode number is valid
1436  * - call ext2_vget() to get the locked inode
1437  * - check for an unallocated inode (i_mode == 0)
1438  * - check that the given client host has export rights and return
1439  *   those rights via. exflagsp and credanonp
1440  */
1441 static int
1442 ext2_fhtovp(struct mount *mp, struct vnode *rootvp, struct fid *fhp,
1443     struct vnode **vpp)
1444 {
1445 	struct inode *ip;
1446 	struct ufid *ufhp;
1447 	struct vnode *nvp;
1448 	struct m_ext2fs *fs;
1449 	int error;
1450 
1451 	ufhp = (struct ufid *)fhp;
1452 	fs = VFSTOEXT2(mp)->um_e2fs;
1453 	if (ufhp->ufid_ino < EXT2_ROOTINO ||
1454 	    ufhp->ufid_ino > fs->e2fs_gcount * fs->e2fs_ipg)
1455 		return (ESTALE);
1456 
1457 	error = VFS_VGET(mp, NULL, LK_EXCLUSIVE, &nvp);
1458 	if (error) {
1459 		*vpp = NULLVP;
1460 		return (error);
1461 	}
1462 	ip = VTOI(nvp);
1463 	if (ip->i_mode == 0 ||
1464 	    ip->i_gen != ufhp->ufid_gen || ip->i_nlink <= 0) {
1465 		vput(nvp);
1466 		*vpp = NULLVP;
1467 		return (ESTALE);
1468 	}
1469 	*vpp = nvp;
1470 	return (0);
1471 }
1472 
1473 /*
1474  * Vnode pointer to File handle
1475  */
1476 /* ARGSUSED */
1477 static int
1478 ext2_vptofh(struct vnode *vp, struct fid *fhp)
1479 {
1480 	struct inode *ip;
1481 	struct ufid *ufhp;
1482 
1483 	ip = VTOI(vp);
1484 	ufhp = (struct ufid *)fhp;
1485 	ufhp->ufid_len = sizeof(struct ufid);
1486 	ufhp->ufid_ino = ip->i_number;
1487 	ufhp->ufid_gen = ip->i_gen;
1488 	return (0);
1489 }
1490 
1491 /*
1492  * This is the generic part of fhtovp called after the underlying
1493  * filesystem has validated the file handle.
1494  *
1495  * Verify that a host should have access to a filesystem.
1496  */
1497 static int
1498 ext2_check_export(struct mount *mp, struct sockaddr *nam, int *exflagsp,
1499                  struct ucred **credanonp)
1500 {
1501 	struct netcred *np;
1502 	struct ext2mount *ump;
1503 
1504 	ump = VFSTOEXT2(mp);
1505 	/*
1506 	 * Get the export permission structure for this <mp, client> tuple.
1507 	 */
1508 	np = vfs_export_lookup(mp, &ump->um_export, nam);
1509 	if (np == NULL)
1510 		return (EACCES);
1511 
1512 	*exflagsp = np->netc_exflags;
1513 	*credanonp = &np->netc_anon;
1514 	return (0);
1515 }
1516 
1517 /*
1518  * Write a superblock and associated information back to disk.
1519  */
1520 static int
1521 ext2_sbupdate(struct ext2mount *mp, int waitfor)
1522 {
1523 	struct m_ext2fs *fs = mp->um_e2fs;
1524 	struct ext2fs *es = fs->e2fs;
1525 	struct buf *bp;
1526 	int error = 0;
1527 
1528 	es->e2fs_bcount = htole32(fs->e2fs_bcount & 0xffffffff);
1529 	es->e2fs_rbcount = htole32(fs->e2fs_rbcount & 0xffffffff);
1530 	es->e2fs_fbcount = htole32(fs->e2fs_fbcount & 0xffffffff);
1531 	if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
1532 		es->e4fs_bcount_hi = htole32(fs->e2fs_bcount >> 32);
1533 		es->e4fs_rbcount_hi = htole32(fs->e2fs_rbcount >> 32);
1534 		es->e4fs_fbcount_hi = htole32(fs->e2fs_fbcount >> 32);
1535 	}
1536 
1537 	es->e2fs_ficount = htole32(fs->e2fs_ficount);
1538 
1539 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
1540 		ext2_sb_csum_set(fs);
1541 
1542 	bp = getblk(mp->um_devvp, SBOFF, SBSIZE, 0, 0);
1543 	bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2fs));
1544 	if (waitfor == MNT_WAIT)
1545 		error = bwrite(bp);
1546 	else
1547 		bawrite(bp);
1548 
1549 	/*
1550 	 * The buffers for group descriptors, inode bitmaps and block bitmaps
1551 	 * are not busy at this point and are (hopefully) written by the
1552 	 * usual sync mechanism. No need to write them here.
1553 	 */
1554 	return (error);
1555 }
1556 
1557 static int
1558 ext2_cgupdate(struct ext2mount *mp, int waitfor)
1559 {
1560 	struct m_ext2fs *fs = mp->um_e2fs;
1561 	struct buf *bp;
1562 	int i, j, g_count = 0, error = 0, allerror = 0;
1563 
1564 	allerror = ext2_sbupdate(mp, waitfor);
1565 
1566 	/* Update gd csums */
1567 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) ||
1568 	    EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
1569 		ext2_gd_csum_set(fs);
1570 
1571 	for (i = 0; i < fs->e2fs_gdbcount; i++) {
1572 		bp = getblk(mp->um_devvp, fsbtodoff(fs,
1573 		    ext2_cg_location(fs, i)),
1574 		    fs->e2fs_bsize, 0, 0);
1575 		if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
1576 			memcpy(bp->b_data, &fs->e2fs_gd[
1577 			    i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
1578 			    fs->e2fs_bsize);
1579 		} else {
1580 			for (j = 0; j < fs->e2fs_bsize / E2FS_REV0_GD_SIZE &&
1581 			    g_count < fs->e2fs_gcount; j++, g_count++)
1582 				memcpy(bp->b_data + j * E2FS_REV0_GD_SIZE,
1583 				    &fs->e2fs_gd[g_count], E2FS_REV0_GD_SIZE);
1584 		}
1585 		if (waitfor == MNT_WAIT)
1586 			error = bwrite(bp);
1587 		else
1588 			bawrite(bp);
1589 	}
1590 
1591 	if (!allerror && error)
1592 		allerror = error;
1593 	return (allerror);
1594 }
1595 
1596 /*
1597  * Return the root of a filesystem.
1598  */
1599 static int
1600 ext2_root(struct mount *mp, struct vnode **vpp)
1601 {
1602 	struct vnode *nvp;
1603 	int error;
1604 
1605 	error = VFS_VGET(mp, NULL, (ino_t)EXT2_ROOTINO, &nvp);
1606 	if (error)
1607 		return (error);
1608 	*vpp = nvp;
1609 	return (0);
1610 }
1611 
1612 /*
1613  * Initialize ext2 filesystems, done only once.
1614  */
1615 static int
1616 ext2_init(struct vfsconf *vfsp)
1617 {
1618 	static int done;
1619 
1620 	if (done)
1621 		return (0);
1622 	done = 1;
1623 	ext2_ihashinit();
1624 
1625 	return (0);
1626 }
1627 
1628 static int
1629 ext2_uninit(struct vfsconf *vfsp)
1630 {
1631 
1632 	ext2_ihashuninit();
1633 
1634 	return (0);
1635 }
1636