xref: /netbsd-src/sys/ufs/ext2fs/ext2fs_vfsops.c (revision c42dbd0ed2e61fe6eda8590caa852ccf34719964)
1 /*	$NetBSD: ext2fs_vfsops.c,v 1.225 2023/08/27 16:35:51 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1991, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)ffs_vfsops.c	8.14 (Berkeley) 11/28/94
32  * Modified for ext2fs by Manuel Bouyer.
33  */
34 
35 /*
36  * Copyright (c) 1997 Manuel Bouyer.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57  *
58  *	@(#)ffs_vfsops.c	8.14 (Berkeley) 11/28/94
59  * Modified for ext2fs by Manuel Bouyer.
60  */
61 
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: ext2fs_vfsops.c,v 1.225 2023/08/27 16:35:51 christos Exp $");
64 
65 #if defined(_KERNEL_OPT)
66 #include "opt_compat_netbsd.h"
67 #endif
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/sysctl.h>
72 #include <sys/namei.h>
73 #include <sys/proc.h>
74 #include <sys/kernel.h>
75 #include <sys/vnode.h>
76 #include <sys/socket.h>
77 #include <sys/mount.h>
78 #include <sys/buf.h>
79 #include <sys/device.h>
80 #include <sys/file.h>
81 #include <sys/disklabel.h>
82 #include <sys/ioctl.h>
83 #include <sys/errno.h>
84 #include <sys/pool.h>
85 #include <sys/lock.h>
86 #include <sys/conf.h>
87 #include <sys/kauth.h>
88 #include <sys/module.h>
89 
90 #include <miscfs/genfs/genfs.h>
91 #include <miscfs/specfs/specdev.h>
92 
93 #include <ufs/ufs/quota.h>
94 #include <ufs/ufs/ufsmount.h>
95 #include <ufs/ufs/inode.h>
96 #include <ufs/ufs/dir.h>
97 #include <ufs/ufs/ufs_extern.h>
98 
99 #include <ufs/ext2fs/ext2fs.h>
100 #include <ufs/ext2fs/ext2fs_dir.h>
101 #include <ufs/ext2fs/ext2fs_extern.h>
102 
103 MODULE(MODULE_CLASS_VFS, ext2fs, "ufs");
104 
105 int ext2fs_sbupdate(struct ufsmount *, int);
106 static int ext2fs_sbfill(struct m_ext2fs *, int);
107 
108 extern const struct vnodeopv_desc ext2fs_vnodeop_opv_desc;
109 extern const struct vnodeopv_desc ext2fs_specop_opv_desc;
110 extern const struct vnodeopv_desc ext2fs_fifoop_opv_desc;
111 
112 const struct vnodeopv_desc * const ext2fs_vnodeopv_descs[] = {
113 	&ext2fs_vnodeop_opv_desc,
114 	&ext2fs_specop_opv_desc,
115 	&ext2fs_fifoop_opv_desc,
116 	NULL,
117 };
118 
119 struct vfsops ext2fs_vfsops = {
120 	.vfs_name = MOUNT_EXT2FS,
121 	.vfs_min_mount_data = sizeof (struct ufs_args),
122 	.vfs_mount = ext2fs_mount,
123 	.vfs_start = ufs_start,
124 	.vfs_unmount = ext2fs_unmount,
125 	.vfs_root = ufs_root,
126 	.vfs_quotactl = ufs_quotactl,
127 	.vfs_statvfs = ext2fs_statvfs,
128 	.vfs_sync = ext2fs_sync,
129 	.vfs_vget = ufs_vget,
130 	.vfs_loadvnode = ext2fs_loadvnode,
131 	.vfs_newvnode = ext2fs_newvnode,
132 	.vfs_fhtovp = ext2fs_fhtovp,
133 	.vfs_vptofh = ext2fs_vptofh,
134 	.vfs_init = ext2fs_init,
135 	.vfs_reinit = ext2fs_reinit,
136 	.vfs_done = ext2fs_done,
137 	.vfs_mountroot = ext2fs_mountroot,
138 	.vfs_snapshot = (void *)eopnotsupp,
139 	.vfs_extattrctl = vfs_stdextattrctl,
140 	.vfs_suspendctl = genfs_suspendctl,
141 	.vfs_renamelock_enter = genfs_renamelock_enter,
142 	.vfs_renamelock_exit = genfs_renamelock_exit,
143 	.vfs_fsync = (void *)eopnotsupp,
144 	.vfs_opv_descs = ext2fs_vnodeopv_descs
145 };
146 
147 static const struct genfs_ops ext2fs_genfsops = {
148 	.gop_size = genfs_size,
149 	.gop_alloc = ext2fs_gop_alloc,
150 	.gop_write = genfs_gop_write,
151 	.gop_markupdate = ufs_gop_markupdate,
152 	.gop_putrange = genfs_gop_putrange,
153 };
154 
155 static const struct ufs_ops ext2fs_ufsops = {
156 	.uo_itimes = ext2fs_itimes,
157 	.uo_update = ext2fs_update,
158 	.uo_bufrd = ext2fs_bufrd,
159 	.uo_bufwr = ext2fs_bufwr,
160 };
161 
162 static void
163 e2fs_cgload(const char *ondisk, struct ext2_gd *inmemory, int cg_size,
164     int shift_cg_entry_size)
165 {
166 
167 	if (shift_cg_entry_size == 6) {
168 		memcpy(inmemory, ondisk, cg_size);
169 		return;
170 	}
171 
172 	const char *iptr = ondisk;
173 	struct ext2_gd *optr = inmemory;
174 	int sh = 1 << shift_cg_entry_size;
175 	int lim = cg_size >> shift_cg_entry_size;
176 	if (shift_cg_entry_size > 6) {
177 		for (int i = 0; i < lim; i++, optr++, iptr += sh) {
178 			memcpy(optr, iptr, sizeof(*optr));
179 		}
180 	} else {
181 		for (int i = 0; i < lim; i++, optr++, iptr += sh) {
182 			memcpy(optr, iptr, E2FS_REV0_GD_SIZE);
183 			memset((char *)optr + E2FS_REV0_GD_SIZE, 0,
184 			    sizeof(*optr) - E2FS_REV0_GD_SIZE);
185 		}
186 	}
187 }
188 
189 static void
190 e2fs_cgsave(const struct ext2_gd *inmemory, char *ondisk, int cg_size,
191     int shift_cg_entry_size)
192 {
193 
194 	if (shift_cg_entry_size == 6) {
195 		memcpy(ondisk, inmemory, cg_size);
196 		return;
197 	}
198 
199 	const struct ext2_gd *iptr = inmemory;
200 	char *optr = ondisk;
201 	int sh = 1 << shift_cg_entry_size;
202 	int lim = cg_size >> shift_cg_entry_size;
203 	if (shift_cg_entry_size > 6) {
204 		for (int i = 0; i < lim; i++, iptr++, optr += sh) {
205 			memcpy(optr, iptr, sizeof(*iptr));
206 			memset(optr + sizeof(*iptr), 0, sh - sizeof(*iptr));
207 		}
208 	} else {
209 		for (int i = 0; i < lim; i++, iptr++, optr += sh) {
210 			memcpy(optr, iptr, E2FS_REV0_GD_SIZE);
211 		}
212 	}
213 }
214 
215 /* Fill in the inode uid/gid from ext2 halves.  */
216 void
217 ext2fs_set_inode_guid(struct inode *ip)
218 {
219 
220 	ip->i_gid = ip->i_e2fs_gid;
221 	ip->i_uid = ip->i_e2fs_uid;
222 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
223 		ip->i_gid |= ip->i_e2fs_gid_high << 16;
224 		ip->i_uid |= ip->i_e2fs_uid_high << 16;
225 	}
226 }
227 
228 SYSCTL_SETUP(ext2fs_sysctl_setup, "ext2fs sysctl")
229 {
230 
231 		sysctl_createv(clog, 0, NULL, NULL,
232 			       CTLFLAG_PERMANENT,
233 			       CTLTYPE_NODE, "ext2fs",
234 			       SYSCTL_DESCR("Linux EXT2FS file system"),
235 			       NULL, 0, NULL, 0,
236 			       CTL_VFS, 17, CTL_EOL);
237 		/*
238 		 * XXX the "17" above could be dynamic, thereby eliminating
239 		 * one more instance of the "number to vfs" mapping problem,
240 		 * but "17" is the order as taken from sys/mount.h
241 		 */
242 }
243 
244 static int
245 ext2fs_modcmd(modcmd_t cmd, void *arg)
246 {
247 	int error;
248 
249 	switch (cmd) {
250 	case MODULE_CMD_INIT:
251 		error = vfs_attach(&ext2fs_vfsops);
252 		if (error != 0)
253 			break;
254 		break;
255 	case MODULE_CMD_FINI:
256 		error = vfs_detach(&ext2fs_vfsops);
257 		if (error != 0)
258 			break;
259 		break;
260 	default:
261 		error = ENOTTY;
262 		break;
263 	}
264 
265 	return error;
266 }
267 
268 /*
269  * XXX Same structure as FFS inodes?  Should we share a common pool?
270  */
271 struct pool ext2fs_inode_pool;
272 
273 extern u_long ext2gennumber;
274 
275 void
276 ext2fs_init(void)
277 {
278 
279 	pool_init(&ext2fs_inode_pool, sizeof(struct inode), 0, 0, 0,
280 	    "ext2fsinopl", &pool_allocator_nointr, IPL_NONE);
281 	ufs_init();
282 }
283 
284 void
285 ext2fs_reinit(void)
286 {
287 	ufs_reinit();
288 }
289 
290 void
291 ext2fs_done(void)
292 {
293 
294 	ufs_done();
295 	pool_destroy(&ext2fs_inode_pool);
296 }
297 
298 static void
299 ext2fs_sb_setmountinfo(struct m_ext2fs *fs, struct mount *mp)
300 {
301 	(void)strlcpy(fs->e2fs_fsmnt, mp->mnt_stat.f_mntonname,
302             sizeof(fs->e2fs_fsmnt));
303 	if (fs->e2fs_ronly == 0 && fs->e2fs.e2fs_rev > E2FS_REV0) {
304 		(void)strlcpy(fs->e2fs.e2fs_fsmnt, mp->mnt_stat.f_mntonname,
305 		    sizeof(fs->e2fs.e2fs_fsmnt));
306 
307 		fs->e2fs.e2fs_mtime = time_second;
308 		fs->e2fs.e2fs_mnt_count++;
309 
310 		fs->e2fs_fmod = 1;
311 	}
312 }
313 
314 /*
315  * Called by main() when ext2fs is going to be mounted as root.
316  *
317  * Name is updated by mount(8) after booting.
318  */
319 
320 int
321 ext2fs_mountroot(void)
322 {
323 	extern struct vnode *rootvp;
324 	struct m_ext2fs *fs;
325 	struct mount *mp;
326 	struct ufsmount *ump;
327 	int error;
328 
329 	if (device_class(root_device) != DV_DISK)
330 		return ENODEV;
331 
332 	if ((error = vfs_rootmountalloc(MOUNT_EXT2FS, "root_device", &mp))) {
333 		vrele(rootvp);
334 		return error;
335 	}
336 
337 	if ((error = ext2fs_mountfs(rootvp, mp)) != 0) {
338 		vfs_unbusy(mp);
339 		vfs_rele(mp);
340 		return error;
341 	}
342 	mountlist_append(mp);
343 	ump = VFSTOUFS(mp);
344 	fs = ump->um_e2fs;
345 	ext2fs_sb_setmountinfo(fs, mp);
346 	(void)ext2fs_statvfs(mp, &mp->mnt_stat);
347 	vfs_unbusy(mp);
348 	setrootfstime((time_t)fs->e2fs.e2fs_wtime);
349 	return 0;
350 }
351 
352 /*
353  * VFS Operations.
354  *
355  * mount system call
356  */
357 int
358 ext2fs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
359 {
360 	struct lwp *l = curlwp;
361 	struct vnode *devvp;
362 	struct ufs_args *args = data;
363 	struct ufsmount *ump = NULL;
364 	struct m_ext2fs *fs;
365 	int error = 0, flags, update;
366 	mode_t accessmode;
367 
368 	if (args == NULL)
369 		return EINVAL;
370 	if (*data_len < sizeof *args)
371 		return EINVAL;
372 
373 	if (mp->mnt_flag & MNT_GETARGS) {
374 		ump = VFSTOUFS(mp);
375 		if (ump == NULL)
376 			return EIO;
377 		memset(args, 0, sizeof *args);
378 		args->fspec = NULL;
379 		*data_len = sizeof *args;
380 		return 0;
381 	}
382 
383 	update = mp->mnt_flag & MNT_UPDATE;
384 
385 	/* Check arguments */
386 	if (args->fspec != NULL) {
387 		/*
388 		 * Look up the name and verify that it's sane.
389 		 */
390 		error = namei_simple_user(args->fspec,
391 					NSM_FOLLOW_NOEMULROOT, &devvp);
392 		if (error != 0)
393 			return error;
394 
395 		if (!update) {
396 			/*
397 			 * Be sure this is a valid block device
398 			 */
399 			if (devvp->v_type != VBLK)
400 				error = ENOTBLK;
401 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
402 				error = ENXIO;
403 		} else {
404 		        /*
405 			 * Be sure we're still naming the same device
406 			 * used for our initial mount
407 			 */
408 			ump = VFSTOUFS(mp);
409 			if (devvp != ump->um_devvp) {
410 				if (devvp->v_rdev != ump->um_devvp->v_rdev)
411 					error = EINVAL;
412 				else {
413 					vrele(devvp);
414 					devvp = ump->um_devvp;
415 					vref(devvp);
416 				}
417 			}
418 		}
419 	} else {
420 		if (!update) {
421 			/* New mounts must have a filename for the device */
422 			return EINVAL;
423 		} else {
424 			ump = VFSTOUFS(mp);
425 			devvp = ump->um_devvp;
426 			vref(devvp);
427 		}
428 	}
429 
430 	/*
431 	 * If mount by non-root, then verify that user has necessary
432 	 * permissions on the device.
433 	 *
434 	 * Permission to update a mount is checked higher, so here we presume
435 	 * updating the mount is okay (for example, as far as securelevel goes)
436 	 * which leaves us with the normal check.
437 	 */
438 	if (error == 0) {
439 		accessmode = VREAD;
440 		if (update ?
441 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
442 		    (mp->mnt_flag & MNT_RDONLY) == 0)
443 			accessmode |= VWRITE;
444 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
445 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
446 		    KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
447 		    KAUTH_ARG(accessmode));
448 		VOP_UNLOCK(devvp);
449 	}
450 
451 	if (error) {
452 		vrele(devvp);
453 		return error;
454 	}
455 
456 	if (!update) {
457 		int xflags;
458 
459 		if (mp->mnt_flag & MNT_RDONLY)
460 			xflags = FREAD;
461 		else
462 			xflags = FREAD|FWRITE;
463 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
464 		error = VOP_OPEN(devvp, xflags, FSCRED);
465 		VOP_UNLOCK(devvp);
466 		if (error)
467 			goto fail;
468 		error = ext2fs_mountfs(devvp, mp);
469 		if (error) {
470 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
471 			(void)VOP_CLOSE(devvp, xflags, NOCRED);
472 			VOP_UNLOCK(devvp);
473 			goto fail;
474 		}
475 
476 		ump = VFSTOUFS(mp);
477 		fs = ump->um_e2fs;
478 	} else {
479 		/*
480 		 * Update the mount.
481 		 */
482 
483 		/*
484 		 * The initial mount got a reference on this
485 		 * device, so drop the one obtained via
486 		 * namei(), above.
487 		 */
488 		vrele(devvp);
489 
490 		ump = VFSTOUFS(mp);
491 		fs = ump->um_e2fs;
492 		if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
493 			/*
494 			 * Changing from r/w to r/o
495 			 */
496 			flags = WRITECLOSE;
497 			if (mp->mnt_flag & MNT_FORCE)
498 				flags |= FORCECLOSE;
499 			error = ext2fs_flushfiles(mp, flags);
500 			if (error == 0 &&
501 			    ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
502 			    (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
503 				fs->e2fs.e2fs_state = E2FS_ISCLEAN;
504 				(void) ext2fs_sbupdate(ump, MNT_WAIT);
505 			}
506 			if (error)
507 				return error;
508 			fs->e2fs_ronly = 1;
509 		}
510 
511 		if (mp->mnt_flag & MNT_RELOAD) {
512 			error = ext2fs_reload(mp, l->l_cred, l);
513 			if (error)
514 				return error;
515 		}
516 
517 		if (fs->e2fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
518 			/*
519 			 * Changing from read-only to read/write
520 			 */
521 			fs->e2fs_ronly = 0;
522 			if (fs->e2fs.e2fs_state == E2FS_ISCLEAN)
523 				fs->e2fs.e2fs_state = 0;
524 			else
525 				fs->e2fs.e2fs_state = E2FS_ERRORS;
526 			fs->e2fs_fmod = 1;
527 		}
528 		if (args->fspec == NULL)
529 			return 0;
530 	}
531 
532 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
533 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
534 	if (error == 0)
535 		ext2fs_sb_setmountinfo(fs, mp);
536 
537 	if (fs->e2fs_fmod != 0) {	/* XXX */
538 		fs->e2fs_fmod = 0;
539 		if (fs->e2fs.e2fs_state == 0)
540 			fs->e2fs.e2fs_wtime = time_second;
541 		else
542 			printf("%s: file system not clean; please fsck(8)\n",
543 				mp->mnt_stat.f_mntfromname);
544 		(void) ext2fs_cgupdate(ump, MNT_WAIT);
545 	}
546 	return error;
547 
548 fail:
549 	vrele(devvp);
550 	return error;
551 }
552 
553 /*
554  * Sanity check the disk vnode content, and copy it over to inode structure.
555  */
556 static int
557 ext2fs_loadvnode_content(struct m_ext2fs *fs, ino_t ino, struct buf *bp, struct inode *ip)
558 {
559 	struct ext2fs_dinode *din;
560 	int error = 0;
561 
562 	din = (struct ext2fs_dinode *)((char *)bp->b_data +
563 	    (ino_to_fsbo(fs, ino) * EXT2_DINODE_SIZE(fs)));
564 
565 	/* sanity checks - inode data NOT byteswapped at this point */
566 	if (EXT2_DINODE_FITS(din, e2di_extra_isize, EXT2_DINODE_SIZE(fs))
567 	    && (EXT2_DINODE_SIZE(fs) - EXT2_REV0_DINODE_SIZE)
568 	    < fs2h16(din->e2di_extra_isize))
569 	{
570 		printf("ext2fs: inode %"PRIu64" bad extra_isize %u",
571 			ino, din->e2di_extra_isize);
572 		error = EINVAL;
573 		goto bad;
574 	}
575 
576 	/* everything alright, proceed with copy */
577 	if (ip->i_din.e2fs_din == NULL)
578 		ip->i_din.e2fs_din = kmem_alloc(EXT2_DINODE_SIZE(fs), KM_SLEEP);
579 
580 	e2fs_iload(din, ip->i_din.e2fs_din, EXT2_DINODE_SIZE(fs));
581 
582 	ext2fs_set_inode_guid(ip);
583 
584     bad:
585 	return error;
586 }
587 
588 /*
589  * Reload all incore data for a filesystem (used after running fsck on
590  * the root filesystem and finding things to fix). The filesystem must
591  * be mounted read-only.
592  *
593  * Things to do to update the mount:
594  *	1) invalidate all cached meta-data.
595  *	2) re-read superblock from disk.
596  *	3) re-read summary information from disk.
597  *	4) invalidate all inactive vnodes.
598  *	5) invalidate all cached file data.
599  *	6) re-read inode data for all active vnodes.
600  */
601 int
602 ext2fs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
603 {
604 	struct vnode *vp, *devvp;
605 	struct inode *ip;
606 	struct buf *bp;
607 	struct m_ext2fs *fs;
608 	struct ext2fs *newfs;
609 	int i, error;
610 	struct ufsmount *ump;
611 	struct vnode_iterator *marker;
612 
613 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
614 		return EINVAL;
615 
616 	ump = VFSTOUFS(mp);
617 	/*
618 	 * Step 1: invalidate all cached meta-data.
619 	 */
620 	devvp = ump->um_devvp;
621 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
622 	error = vinvalbuf(devvp, 0, cred, l, 0, 0);
623 	VOP_UNLOCK(devvp);
624 	if (error)
625 		panic("ext2fs_reload: dirty1");
626 
627 	fs = ump->um_e2fs;
628 	/*
629 	 * Step 2: re-read superblock from disk. Copy in new superblock, and
630 	 * compute in-memory values.
631 	 */
632 	error = bread(devvp, SBLOCK, SBSIZE, 0, &bp);
633 	if (error)
634 		return error;
635 	newfs = (struct ext2fs *)bp->b_data;
636 	e2fs_sbload(newfs, &fs->e2fs);
637 
638 	brelse(bp, 0);
639 
640 	error = ext2fs_sbfill(fs, (mp->mnt_flag & MNT_RDONLY) != 0);
641 	if (error)
642 		return error;
643 
644 	/*
645 	 * Step 3: re-read summary information from disk.
646 	 */
647 	for (i = 0; i < fs->e2fs_ngdb; i++) {
648 		error = bread(devvp ,
649 		    EXT2_FSBTODB(fs, fs->e2fs.e2fs_first_dblock +
650 		    1 /* superblock */ + i),
651 		    fs->e2fs_bsize, 0, &bp);
652 		if (error) {
653 			return error;
654 		}
655 		e2fs_cgload(bp->b_data,
656 		    &fs->e2fs_gd[i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
657 		    fs->e2fs_bsize, fs->e2fs_group_desc_shift);
658 		brelse(bp, 0);
659 	}
660 
661 	vfs_vnode_iterator_init(mp, &marker);
662 	while ((vp = vfs_vnode_iterator_next(marker, NULL, NULL))) {
663 		/*
664 		 * Step 4: invalidate all inactive vnodes.
665 		 */
666 		if (vrecycle(vp))
667 			continue;
668 		/*
669 		 * Step 5: invalidate all cached file data.
670 		 */
671 		if (vn_lock(vp, LK_EXCLUSIVE)) {
672 			vrele(vp);
673 			continue;
674 		}
675 		if (vinvalbuf(vp, 0, cred, l, 0, 0))
676 			panic("ext2fs_reload: dirty2");
677 		/*
678 		 * Step 6: re-read inode data for all active vnodes.
679 		 */
680 		ip = VTOI(vp);
681 		error = bread(devvp, EXT2_FSBTODB(fs, ino_to_fsba(fs, ip->i_number)),
682 		    (int)fs->e2fs_bsize, 0, &bp);
683 		if (error) {
684 			vput(vp);
685 			break;
686 		}
687 		error = ext2fs_loadvnode_content(fs, ip->i_number, bp, ip);
688 		brelse(bp, 0);
689 		if (error) {
690 			vput(vp);
691 			break;
692 		}
693 
694 		vput(vp);
695 	}
696 	vfs_vnode_iterator_destroy(marker);
697 	return error;
698 }
699 
700 /*
701  * Common code for mount and mountroot
702  */
703 int
704 ext2fs_mountfs(struct vnode *devvp, struct mount *mp)
705 {
706 	struct lwp *l = curlwp;
707 	struct ufsmount *ump;
708 	struct buf *bp;
709 	struct ext2fs *fs;
710 	struct m_ext2fs *m_fs;
711 	dev_t dev;
712 	int error, i, ronly;
713 	kauth_cred_t cred;
714 
715 	dev = devvp->v_rdev;
716 	cred = l->l_cred;
717 
718 	/* Flush out any old buffers remaining from a previous use. */
719 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
720 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
721 	VOP_UNLOCK(devvp);
722 	if (error)
723 		return error;
724 
725 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
726 
727 	bp = NULL;
728 	ump = NULL;
729 
730 	/* Read the superblock from disk, and swap it directly. */
731 	error = bread(devvp, SBLOCK, SBSIZE, 0, &bp);
732 	if (error)
733 		goto out;
734 	fs = (struct ext2fs *)bp->b_data;
735 	m_fs = kmem_zalloc(sizeof(*m_fs), KM_SLEEP);
736 	e2fs_sbload(fs, &m_fs->e2fs);
737 
738 	brelse(bp, 0);
739 	bp = NULL;
740 
741 	/* Once swapped, validate and fill in the superblock. */
742 	error = ext2fs_sbfill(m_fs, ronly);
743 	if (error) {
744 		kmem_free(m_fs, sizeof(*m_fs));
745 		goto out;
746 	}
747 	m_fs->e2fs_ronly = ronly;
748 
749 	ump = kmem_zalloc(sizeof(*ump), KM_SLEEP);
750 	ump->um_fstype = UFS1;
751 	ump->um_ops = &ext2fs_ufsops;
752 	ump->um_e2fs = m_fs;
753 
754 	if (ronly == 0) {
755 		if (m_fs->e2fs.e2fs_state == E2FS_ISCLEAN)
756 			m_fs->e2fs.e2fs_state = 0;
757 		else
758 			m_fs->e2fs.e2fs_state = E2FS_ERRORS;
759 		m_fs->e2fs_fmod = 1;
760 	}
761 
762 	int32_t sh = m_fs->e2fs_bsize >> m_fs->e2fs_group_desc_shift;
763 	/* XXX: should be added in ext2fs_sbfill()? */
764 	m_fs->e2fs_gd = kmem_alloc(m_fs->e2fs_ngdb * sh
765 	    * sizeof(struct ext2_gd), KM_SLEEP);
766 	for (i = 0; i < m_fs->e2fs_ngdb; i++) {
767 		error = bread(devvp,
768 		    EXT2_FSBTODB(m_fs, m_fs->e2fs.e2fs_first_dblock +
769 		    1 /* superblock */ + i),
770 		    m_fs->e2fs_bsize, 0, &bp);
771 		if (error)
772 			goto out1;
773 		e2fs_cgload(bp->b_data, &m_fs->e2fs_gd[i * m_fs->e2fs_bsize
774 		    / sizeof(struct ext2_gd)],
775 		    m_fs->e2fs_bsize, m_fs->e2fs_group_desc_shift);
776 		brelse(bp, 0);
777 		bp = NULL;
778 	}
779 
780 	error = ext2fs_cg_verify_and_initialize(devvp, m_fs, ronly);
781 	if (error)
782 		goto out1;
783 
784 	mp->mnt_data = ump;
785 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
786 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_EXT2FS);
787 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
788 	mp->mnt_stat.f_namemax = EXT2FS_MAXNAMLEN;
789 	mp->mnt_flag |= MNT_LOCAL;
790 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
791 	mp->mnt_fs_bshift = m_fs->e2fs_bshift;
792 	mp->mnt_iflag |= IMNT_DTYPE | IMNT_SHRLOOKUP;
793 	ump->um_flags = 0;
794 	ump->um_mountp = mp;
795 	ump->um_dev = dev;
796 	ump->um_devvp = devvp;
797 	ump->um_nindir = EXT2_NINDIR(m_fs);
798 	ump->um_lognindir = ffs(EXT2_NINDIR(m_fs)) - 1;
799 	ump->um_bptrtodb = m_fs->e2fs_fsbtodb;
800 	ump->um_seqinc = 1; /* no frags */
801 	ump->um_maxsymlinklen = EXT2_MAXSYMLINKLEN;
802 	ump->um_dirblksiz = m_fs->e2fs_bsize;
803 	ump->um_maxfilesize = ((uint64_t)0x80000000 * m_fs->e2fs_bsize - 1);
804 	spec_node_setmountedfs(devvp, mp);
805 	return 0;
806 
807 out1:
808 	kmem_free(m_fs->e2fs_gd, m_fs->e2fs_ngdb * sh * sizeof(struct ext2_gd));
809 out:
810 	if (bp != NULL)
811 		brelse(bp, 0);
812 	if (ump) {
813 		kmem_free(ump->um_e2fs, sizeof(*m_fs));
814 		kmem_free(ump, sizeof(*ump));
815 		mp->mnt_data = NULL;
816 	}
817 	return error;
818 }
819 
820 /*
821  * unmount system call
822  */
823 int
824 ext2fs_unmount(struct mount *mp, int mntflags)
825 {
826 	struct ufsmount *ump;
827 	struct m_ext2fs *fs;
828 	int error, flags;
829 
830 	flags = 0;
831 	if (mntflags & MNT_FORCE)
832 		flags |= FORCECLOSE;
833 	if ((error = ext2fs_flushfiles(mp, flags)) != 0)
834 		return error;
835 	ump = VFSTOUFS(mp);
836 	fs = ump->um_e2fs;
837 	if (fs->e2fs_ronly == 0 &&
838 		ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
839 		(fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
840 		fs->e2fs.e2fs_state = E2FS_ISCLEAN;
841 		(void) ext2fs_sbupdate(ump, MNT_WAIT);
842 	}
843 	if (ump->um_devvp->v_type != VBAD)
844 		spec_node_setmountedfs(ump->um_devvp, NULL);
845 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
846 	error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
847 	    NOCRED);
848 	vput(ump->um_devvp);
849 	int32_t sh = fs->e2fs_bsize >> fs->e2fs_group_desc_shift;
850 	kmem_free(fs->e2fs_gd, fs->e2fs_ngdb * sh * sizeof(struct ext2_gd));
851 	kmem_free(fs, sizeof(*fs));
852 	kmem_free(ump, sizeof(*ump));
853 	mp->mnt_data = NULL;
854 	mp->mnt_flag &= ~MNT_LOCAL;
855 	return error;
856 }
857 
858 /*
859  * Flush out all the files in a filesystem.
860  */
861 int
862 ext2fs_flushfiles(struct mount *mp, int flags)
863 {
864 	extern int doforce;
865 	int error;
866 
867 	if (!doforce)
868 		flags &= ~FORCECLOSE;
869 	error = vflush(mp, NULLVP, flags);
870 	return error;
871 }
872 
873 /*
874  * Get file system statistics.
875  */
876 int
877 ext2fs_statvfs(struct mount *mp, struct statvfs *sbp)
878 {
879 	struct ufsmount *ump;
880 	struct m_ext2fs *fs;
881 	uint32_t overhead, overhead_per_group, ngdb;
882 	int i, ngroups;
883 
884 	ump = VFSTOUFS(mp);
885 	fs = ump->um_e2fs;
886 	if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
887 		panic("ext2fs_statvfs");
888 
889 	/*
890 	 * Compute the overhead (FS structures)
891 	 */
892 	overhead_per_group =
893 	    1 /* block bitmap */ +
894 	    1 /* inode bitmap */ +
895 	    fs->e2fs_itpg;
896 	overhead = fs->e2fs.e2fs_first_dblock +
897 	    fs->e2fs_ncg * overhead_per_group;
898 	if (EXT2F_HAS_COMPAT_FEATURE(fs, EXT2F_COMPAT_SPARSESUPER2)) {
899 		/*
900 		 * Superblock and group descriptions is in group zero,
901 		 * then optionally 0, 1 or 2 extra copies.
902 		 */
903 		ngroups = 1
904 			+ (fs->e2fs.e4fs_backup_bgs[0] ? 1 : 0)
905 			+ (fs->e2fs.e4fs_backup_bgs[1] ? 1 : 0);
906 	} else if (EXT2F_HAS_ROCOMPAT_FEATURE(fs, EXT2F_ROCOMPAT_SPARSESUPER)) {
907 		for (i = 0, ngroups = 0; i < fs->e2fs_ncg; i++) {
908 			if (cg_has_sb(i))
909 				ngroups++;
910 		}
911 	} else {
912 		ngroups = fs->e2fs_ncg;
913 	}
914 	ngdb = fs->e2fs_ngdb;
915 	if (EXT2F_HAS_COMPAT_FEATURE(fs, EXT2F_COMPAT_RESIZE))
916 		ngdb += fs->e2fs.e2fs_reserved_ngdb;
917 	overhead += ngroups * (1 /* superblock */ + ngdb);
918 
919 	sbp->f_bsize = fs->e2fs_bsize;
920 	sbp->f_frsize = MINBSIZE << fs->e2fs.e2fs_fsize;
921 	sbp->f_iosize = fs->e2fs_bsize;
922 	sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
923 	sbp->f_bfree = fs->e2fs.e2fs_fbcount;
924 	sbp->f_bresvd = fs->e2fs.e2fs_rbcount;
925 	if (sbp->f_bfree > sbp->f_bresvd)
926 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
927 	else
928 		sbp->f_bavail = 0;
929 	sbp->f_files =  fs->e2fs.e2fs_icount;
930 	sbp->f_ffree = fs->e2fs.e2fs_ficount;
931 	sbp->f_favail = fs->e2fs.e2fs_ficount;
932 	sbp->f_fresvd = 0;
933 	copy_statvfs_info(sbp, mp);
934 	return 0;
935 }
936 
937 static bool
938 ext2fs_sync_selector(void *cl, struct vnode *vp)
939 {
940 	struct inode *ip;
941 
942 	KASSERT(mutex_owned(vp->v_interlock));
943 
944 	ip = VTOI(vp);
945 	/*
946 	 * Skip the vnode/inode if inaccessible.
947 	 */
948 	if (ip == NULL || vp->v_type == VNON)
949 		return false;
950 
951 	if (((ip->i_flag &
952 	      (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
953 	     LIST_EMPTY(&vp->v_dirtyblkhd) &&
954 	     (vp->v_iflag & VI_ONWORKLST) == 0))
955 		return false;
956 	return true;
957 }
958 
959 /*
960  * Go through the disk queues to initiate sandbagged IO;
961  * go through the inodes to write those that have been modified;
962  * initiate the writing of the super block if it has been modified.
963  *
964  * Note: we are always called with the filesystem marked `MPBUSY'.
965  */
966 int
967 ext2fs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
968 {
969 	struct vnode *vp;
970 	struct ufsmount *ump = VFSTOUFS(mp);
971 	struct m_ext2fs *fs;
972 	struct vnode_iterator *marker;
973 	int error, allerror = 0;
974 
975 	fs = ump->um_e2fs;
976 	if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) {	/* XXX */
977 		printf("fs = %s\n", fs->e2fs_fsmnt);
978 		panic("update: rofs mod");
979 	}
980 
981 	/*
982 	 * Write back each (modified) inode.
983 	 */
984 	vfs_vnode_iterator_init(mp, &marker);
985 	while ((vp = vfs_vnode_iterator_next(marker, ext2fs_sync_selector,
986 	    NULL)))
987 	{
988 		error = vn_lock(vp, LK_EXCLUSIVE);
989 		if (error) {
990 			vrele(vp);
991 			continue;
992 		}
993 		if (vp->v_type == VREG && waitfor == MNT_LAZY)
994 			error = ext2fs_update(vp, NULL, NULL, 0);
995 		else
996 			error = VOP_FSYNC(vp, cred,
997 			    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0);
998 		if (error)
999 			allerror = error;
1000 		vput(vp);
1001 	}
1002 	vfs_vnode_iterator_destroy(marker);
1003 	/*
1004 	 * Force stale file system control information to be flushed.
1005 	 */
1006 	if (waitfor != MNT_LAZY) {
1007 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1008 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
1009 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
1010 			allerror = error;
1011 		VOP_UNLOCK(ump->um_devvp);
1012 	}
1013 	/*
1014 	 * Write back modified superblock.
1015 	 */
1016 	if (fs->e2fs_fmod != 0) {
1017 		fs->e2fs_fmod = 0;
1018 		fs->e2fs.e2fs_wtime = time_second;
1019 		if ((error = ext2fs_cgupdate(ump, waitfor)))
1020 			allerror = error;
1021 	}
1022 	return allerror;
1023 }
1024 
1025 /*
1026  * Load inode from disk and initialize vnode.
1027  */
1028 static int
1029 ext2fs_init_vnode(struct ufsmount *ump, struct vnode *vp, ino_t ino)
1030 {
1031 	struct m_ext2fs *fs;
1032 	struct inode *ip;
1033 	struct buf *bp;
1034 	int error;
1035 
1036 	fs = ump->um_e2fs;
1037 
1038 	/* Read in the disk contents for the inode, copy into the inode. */
1039 	error = bread(ump->um_devvp, EXT2_FSBTODB(fs, ino_to_fsba(fs, ino)),
1040 	    (int)fs->e2fs_bsize, 0, &bp);
1041 	if (error)
1042 		return error;
1043 
1044 	/* Allocate and initialize inode. */
1045 	ip = pool_get(&ext2fs_inode_pool, PR_WAITOK);
1046 	memset(ip, 0, sizeof(struct inode));
1047 	ip->i_vnode = vp;
1048 	ip->i_ump = ump;
1049 	ip->i_e2fs = fs;
1050 	ip->i_dev = ump->um_dev;
1051 	ip->i_number = ino;
1052 	ip->i_e2fs_last_lblk = 0;
1053 	ip->i_e2fs_last_blk = 0;
1054 
1055 	error = ext2fs_loadvnode_content(fs, ino, bp, ip);
1056 	brelse(bp, 0);
1057 	if (error) {
1058 		pool_put(&ext2fs_inode_pool, ip);
1059 		return error;
1060 	}
1061 
1062 	/* If the inode was deleted, reset all fields */
1063 	if (ip->i_e2fs_dtime != 0) {
1064 		ip->i_e2fs_mode = 0;
1065 		(void)ext2fs_setsize(ip, 0);
1066 		(void)ext2fs_setnblock(ip, 0);
1067 		memset(ip->i_e2fs_blocks, 0, sizeof(ip->i_e2fs_blocks));
1068 	}
1069 
1070 	/* Initialise vnode with this inode. */
1071 	vp->v_tag = VT_EXT2FS;
1072 	vp->v_op = ext2fs_vnodeop_p;
1073 	vp->v_data = ip;
1074 
1075 	/* Initialize genfs node. */
1076 	genfs_node_init(vp, &ext2fs_genfsops);
1077 
1078 	return 0;
1079 }
1080 
1081 /*
1082  * Read an inode from disk and initialize this vnode / inode pair.
1083  * Caller assures no other thread will try to load this inode.
1084  */
1085 int
1086 ext2fs_loadvnode(struct mount *mp, struct vnode *vp,
1087     const void *key, size_t key_len, const void **new_key)
1088 {
1089 	ino_t ino;
1090 	struct inode *ip;
1091 	struct ufsmount *ump;
1092 	int error;
1093 
1094 	KASSERT(key_len == sizeof(ino));
1095 	memcpy(&ino, key, key_len);
1096 	ump = VFSTOUFS(mp);
1097 
1098 	error = ext2fs_init_vnode(ump, vp, ino);
1099 	if (error)
1100 		return error;
1101 
1102 	ip = VTOI(vp);
1103 
1104 	/* Initialize the vnode from the inode. */
1105 	ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
1106 
1107 	/* Finish inode initialization. */
1108 	ip->i_devvp = ump->um_devvp;
1109 	vref(ip->i_devvp);
1110 
1111 	/*
1112 	 * Set up a generation number for this inode if it does not
1113 	 * already have one. This should only happen on old filesystems.
1114 	 */
1115 
1116 	if (ip->i_e2fs_gen == 0) {
1117 		if (++ext2gennumber < (u_long)time_second)
1118 			ext2gennumber = time_second;
1119 		ip->i_e2fs_gen = ext2gennumber;
1120 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
1121 			ip->i_flag |= IN_MODIFIED;
1122 	}
1123 	uvm_vnp_setsize(vp, ext2fs_size(ip));
1124 	*new_key = &ip->i_number;
1125 	return 0;
1126 }
1127 
1128 /*
1129  * Create a new inode on disk and initialize this vnode / inode pair.
1130  */
1131 int
1132 ext2fs_newvnode(struct mount *mp, struct vnode *dvp, struct vnode *vp,
1133     struct vattr *vap, kauth_cred_t cred, void *extra,
1134     size_t *key_len, const void **new_key)
1135 {
1136 	ino_t ino;
1137 	struct inode *ip, *pdir;
1138 	struct m_ext2fs *fs;
1139 	struct ufsmount *ump;
1140 	int error, mode;
1141 
1142 	KASSERT(dvp->v_mount == mp);
1143 	KASSERT(vap->va_type != VNON);
1144 
1145 	*key_len = sizeof(ino);
1146 
1147 	pdir = VTOI(dvp);
1148 	fs = pdir->i_e2fs;
1149 	ump = VFSTOUFS(mp);
1150 	mode = MAKEIMODE(vap->va_type, vap->va_mode);
1151 
1152 	/* Allocate fresh inode. */
1153 	error = ext2fs_valloc(dvp, mode, cred, &ino);
1154 	if (error)
1155 		return error;
1156 
1157 	/* Attach inode to vnode. */
1158 	error = ext2fs_init_vnode(ump, vp, ino);
1159 	if (error) {
1160 		ext2fs_vfree(dvp, ino, mode);
1161 		return error;
1162 	}
1163 
1164 	ip = VTOI(vp);
1165 
1166 	KASSERT(!E2FS_HAS_GD_CSUM(fs) ||
1167 	    (fs->e2fs_gd[ino_to_cg(fs, ino)].ext2bgd_flags &
1168 	    h2fs16(E2FS_BG_INODE_ZEROED)) != 0);
1169 
1170 	/* check for already used inode; makes sense only for ZEROED itable */
1171 	if (__predict_false(ip->i_e2fs_mode && ip->i_e2fs_nlink != 0)) {
1172 		printf("mode = 0%o, nlinks %d, inum = %llu, fs = %s\n",
1173 		    ip->i_e2fs_mode, ip->i_e2fs_nlink,
1174 		    (unsigned long long)ip->i_number, fs->e2fs_fsmnt);
1175 		panic("ext2fs_valloc: dup alloc");
1176 	}
1177 
1178 	memset(ip->i_din.e2fs_din, 0, EXT2_DINODE_SIZE(fs));
1179 
1180 	/*
1181 	 * Set up a new generation number for this inode.
1182 	 */
1183 	if (++ext2gennumber < time_second)
1184 		ext2gennumber = time_second;
1185 	ip->i_e2fs_gen = ext2gennumber;
1186 
1187 	ip->i_uid = kauth_cred_geteuid(cred);
1188 	ip->i_e2fs_uid = ip->i_uid & 0xffff;
1189 	ip->i_e2fs_gid = pdir->i_e2fs_gid;
1190 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
1191 		ip->i_e2fs_uid_high = (ip->i_uid >> 16) & 0xffff;
1192 		ip->i_e2fs_gid_high = pdir->i_e2fs_gid_high;
1193 	} else {
1194 		ip->i_e2fs_uid_high = 0;
1195 		ip->i_e2fs_gid_high = 0;
1196 	}
1197 	ip->i_gid = ip->i_e2fs_gid | (ip->i_e2fs_gid_high << 16);
1198 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1199 	ip->i_e2fs_mode = mode;
1200 	vp->v_type = IFTOVT(mode);
1201 	ip->i_e2fs_nlink = 1;
1202 
1203 	/* Authorize setting SGID if needed. */
1204 	if (ip->i_e2fs_mode & ISGID) {
1205 		error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY,
1206 		    vp, NULL, genfs_can_chmod(vp, cred, ip->i_uid, ip->i_gid,
1207 		    mode));
1208 		if (error)
1209 			ip->i_e2fs_mode &= ~ISGID;
1210 	}
1211 
1212 	/* Initialize extra_isize according to what is set in superblock */
1213 	if (EXT2F_HAS_ROCOMPAT_FEATURE(ip->i_e2fs, EXT2F_ROCOMPAT_EXTRA_ISIZE)
1214 	    && EXT2_DINODE_SIZE(ip->i_e2fs) > EXT2_REV0_DINODE_SIZE) {
1215 		ip->i_din.e2fs_din->e2di_extra_isize =
1216 		    ip->i_e2fs->e2fs.e4fs_want_extra_isize;
1217 	}
1218 
1219 	/* Set create time if possible */
1220 	if (EXT2_DINODE_FITS(ip->i_din.e2fs_din, e2di_crtime,
1221 	    EXT2_DINODE_SIZE(ip->i_e2fs))) {
1222 		struct timespec now;
1223 		vfs_timestamp(&now);
1224 		EXT2_DINODE_TIME_SET(&now, ip->i_din.e2fs_din, e2di_crtime,
1225 		    EXT2_DINODE_SIZE(ip->i_e2fs));
1226 	}
1227 
1228 	/* Initialize the vnode from the inode. */
1229 	ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
1230 
1231 	/* Finish inode initialization. */
1232 	ip->i_devvp = ump->um_devvp;
1233 	vref(ip->i_devvp);
1234 
1235 	uvm_vnp_setsize(vp, ext2fs_size(ip));
1236 	*new_key = &ip->i_number;
1237 	return 0;
1238 }
1239 
1240 /*
1241  * File handle to vnode
1242  *
1243  * Have to be really careful about stale file handles:
1244  * - check that the inode number is valid
1245  * - call ext2fs_vget() to get the locked inode
1246  * - check for an unallocated inode (i_mode == 0)
1247  */
1248 int
1249 ext2fs_fhtovp(struct mount *mp, struct fid *fhp, int lktype, struct vnode **vpp)
1250 {
1251 	struct inode *ip;
1252 	struct vnode *nvp;
1253 	int error;
1254 	struct ufid ufh;
1255 	struct m_ext2fs *fs;
1256 
1257 	if (fhp->fid_len != sizeof(struct ufid))
1258 		return EINVAL;
1259 
1260 	memcpy(&ufh, fhp, sizeof(struct ufid));
1261 	fs = VFSTOUFS(mp)->um_e2fs;
1262 	if ((ufh.ufid_ino < EXT2_FIRSTINO && ufh.ufid_ino != EXT2_ROOTINO) ||
1263 		ufh.ufid_ino >= fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
1264 		return ESTALE;
1265 
1266 	if ((error = VFS_VGET(mp, ufh.ufid_ino, lktype, &nvp)) != 0) {
1267 		*vpp = NULLVP;
1268 		return error;
1269 	}
1270 	ip = VTOI(nvp);
1271 	if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0 ||
1272 		ip->i_e2fs_gen != ufh.ufid_gen) {
1273 		vput(nvp);
1274 		*vpp = NULLVP;
1275 		return ESTALE;
1276 	}
1277 	*vpp = nvp;
1278 	return 0;
1279 }
1280 
1281 /*
1282  * Vnode pointer to File handle
1283  */
1284 /* ARGSUSED */
1285 int
1286 ext2fs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1287 {
1288 	struct inode *ip;
1289 	struct ufid ufh;
1290 
1291 	if (*fh_size < sizeof(struct ufid)) {
1292 		*fh_size = sizeof(struct ufid);
1293 		return E2BIG;
1294 	}
1295 	*fh_size = sizeof(struct ufid);
1296 
1297 	ip = VTOI(vp);
1298 	memset(&ufh, 0, sizeof(ufh));
1299 	ufh.ufid_len = sizeof(struct ufid);
1300 	ufh.ufid_ino = ip->i_number;
1301 	ufh.ufid_gen = ip->i_e2fs_gen;
1302 	memcpy(fhp, &ufh, sizeof(ufh));
1303 	return 0;
1304 }
1305 
1306 /*
1307  * Write a superblock and associated information back to disk.
1308  */
1309 int
1310 ext2fs_sbupdate(struct ufsmount *mp, int waitfor)
1311 {
1312 	struct m_ext2fs *fs = mp->um_e2fs;
1313 	struct buf *bp;
1314 	int error = 0;
1315 
1316 	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
1317 	e2fs_sbsave(&fs->e2fs, (struct ext2fs*)bp->b_data);
1318 	if (waitfor == MNT_WAIT)
1319 		error = bwrite(bp);
1320 	else
1321 		bawrite(bp);
1322 	return error;
1323 }
1324 
1325 int
1326 ext2fs_cgupdate(struct ufsmount *mp, int waitfor)
1327 {
1328 	struct m_ext2fs *fs = mp->um_e2fs;
1329 	struct buf *bp;
1330 	int i, error = 0, allerror = 0;
1331 
1332 	allerror = ext2fs_sbupdate(mp, waitfor);
1333 	for (i = 0; i < fs->e2fs_ngdb; i++) {
1334 		bp = getblk(mp->um_devvp, EXT2_FSBTODB(fs,
1335 		    fs->e2fs.e2fs_first_dblock +
1336 		    1 /* superblock */ + i), fs->e2fs_bsize, 0, 0);
1337 		e2fs_cgsave(&fs->e2fs_gd[
1338 		    i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
1339 		    bp->b_data, fs->e2fs_bsize, fs->e2fs_group_desc_shift);
1340 		if (waitfor == MNT_WAIT)
1341 			error = bwrite(bp);
1342 		else
1343 			bawrite(bp);
1344 	}
1345 
1346 	if (!allerror && error)
1347 		allerror = error;
1348 	return allerror;
1349 }
1350 
1351 /*
1352  * Fill in the m_fs structure, and validate the fields of the superblock.
1353  * NOTE: here, the superblock is already swapped.
1354  */
1355 static int
1356 ext2fs_sbfill(struct m_ext2fs *m_fs, int ronly)
1357 {
1358 	uint32_t u32;
1359 	struct ext2fs *fs = &m_fs->e2fs;
1360 
1361 	/*
1362 	 * General sanity checks
1363 	 */
1364 	if (fs->e2fs_magic != E2FS_MAGIC)
1365 		return EINVAL;
1366 	if (fs->e2fs_rev > E2FS_REV1) {
1367 		printf("ext2fs: unsupported revision number: %#x\n",
1368 		    fs->e2fs_rev);
1369 		return EINVAL;
1370 	}
1371 	if (fs->e2fs_log_bsize > 2) {
1372 		/* block size = 1024|2048|4096 */
1373 		printf("ext2fs: bad block size: %d\n", fs->e2fs_log_bsize);
1374 		return EINVAL;
1375 	}
1376 	if (fs->e2fs_bpg == 0) {
1377 		printf("ext2fs: zero blocks per group\n");
1378 		return EINVAL;
1379 	}
1380 	if (fs->e2fs_ipg == 0) {
1381 		printf("ext2fs: zero inodes per group\n");
1382 		return EINVAL;
1383 	}
1384 
1385 	if (fs->e2fs_first_dblock >= fs->e2fs_bcount) {
1386 		printf("ext2fs: invalid first data block\n");
1387 		return EINVAL;
1388 	}
1389 	if (fs->e2fs_rbcount > fs->e2fs_bcount ||
1390 	    fs->e2fs_fbcount > fs->e2fs_bcount) {
1391 		printf("ext2fs: invalid block count\n");
1392 		return EINVAL;
1393 	}
1394 
1395 	/*
1396 	 * Compute the fields of the superblock
1397 	 */
1398 	u32 = fs->e2fs_bcount - fs->e2fs_first_dblock; /* > 0 */
1399 	m_fs->e2fs_ncg = howmany(u32, fs->e2fs_bpg);
1400 	if (m_fs->e2fs_ncg == 0) {
1401 		printf("ext2fs: invalid number of cylinder groups\n");
1402 		return EINVAL;
1403 	}
1404 
1405 	m_fs->e2fs_fsbtodb = fs->e2fs_log_bsize + LOG_MINBSIZE - DEV_BSHIFT;
1406 	m_fs->e2fs_bsize = MINBSIZE << fs->e2fs_log_bsize;
1407 	m_fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs_log_bsize;
1408 	m_fs->e2fs_qbmask = m_fs->e2fs_bsize - 1;
1409 	m_fs->e2fs_bmask = ~m_fs->e2fs_qbmask;
1410 
1411 	if (!(fs->e2fs_features_incompat & EXT2F_INCOMPAT_64BIT) ||
1412 	    (fs->e2fs_rev == E2FS_REV0))
1413 		m_fs->e2fs_group_desc_shift = 5;
1414 	else {
1415 		for (m_fs->e2fs_group_desc_shift = 0;
1416 		     (1 << m_fs->e2fs_group_desc_shift)
1417 		       < fs->e3fs_desc_size;
1418 		     m_fs->e2fs_group_desc_shift++);
1419 	}
1420 
1421 	if ((u32 = (m_fs->e2fs_bsize >> m_fs->e2fs_group_desc_shift)) == 0) {
1422 		/* Unlikely to happen */
1423 		printf("ext2fs: invalid block size\n");
1424 		return EINVAL;
1425 	}
1426 	m_fs->e2fs_ngdb = howmany(m_fs->e2fs_ncg, u32);
1427 	if (m_fs->e2fs_ngdb == 0) {
1428 		printf("ext2fs: invalid number of group descriptor blocks\n");
1429 		return EINVAL;
1430 	}
1431 
1432 	if (m_fs->e2fs_bsize < EXT2_DINODE_SIZE(m_fs)) {
1433 		printf("ext2fs: invalid inode size\n");
1434 		return EINVAL;
1435 	}
1436 	m_fs->e2fs_ipb = m_fs->e2fs_bsize / EXT2_DINODE_SIZE(m_fs);
1437 
1438 	m_fs->e2fs_itpg = fs->e2fs_ipg / m_fs->e2fs_ipb;
1439 
1440 	/*
1441 	 * Revision-specific checks
1442 	 */
1443 	if (fs->e2fs_rev > E2FS_REV0) {
1444 		char buf[256];
1445 		if (fs->e2fs_first_ino != EXT2_FIRSTINO) {
1446 			printf("ext2fs: unsupported first inode position\n");
1447 			return EINVAL;
1448 		}
1449 		u32 = fs->e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP;
1450 		if (u32) {
1451 			snprintb(buf, sizeof(buf), EXT2F_INCOMPAT_BITS, u32);
1452 			printf("ext2fs: unsupported incompat features: %s\n",
1453 			    buf);
1454 #ifndef EXT2_IGNORE_INCOMPAT_FEATURES
1455 			return EINVAL;
1456 #endif
1457 		}
1458 		u32 = fs->e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP;
1459 		if (!ronly && u32) {
1460 			snprintb(buf, sizeof(buf), EXT2F_ROCOMPAT_BITS, u32);
1461 			printf("ext2fs: unsupported ro-incompat features: %s\n",
1462 			    buf);
1463 #ifndef EXT2_IGNORE_ROCOMPAT_FEATURES
1464 			return EROFS;
1465 #endif
1466 		}
1467 		if (fs->e2fs_inode_size == 0 || !powerof2(fs->e2fs_inode_size) || fs->e2fs_inode_size > m_fs->e2fs_bsize) {
1468 			printf("ext2fs: bad inode size\n");
1469 			return EINVAL;
1470 		}
1471 	}
1472 
1473 	return 0;
1474 }
1475