xref: /netbsd-src/sys/ufs/ffs/ffs_vfsops.c (revision d25ffa98a4bfca1fe272f3c182496ec9934faac7)
1 /*	$NetBSD: ffs_vfsops.c,v 1.268 2011/06/17 14:23:52 manu Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Wasabi Systems, Inc, and by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1989, 1991, 1993, 1994
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)ffs_vfsops.c	8.31 (Berkeley) 5/20/95
61  */
62 
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: ffs_vfsops.c,v 1.268 2011/06/17 14:23:52 manu Exp $");
65 
66 #if defined(_KERNEL_OPT)
67 #include "opt_ffs.h"
68 #include "opt_quota.h"
69 #include "opt_wapbl.h"
70 #endif
71 
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/namei.h>
75 #include <sys/proc.h>
76 #include <sys/kernel.h>
77 #include <sys/vnode.h>
78 #include <sys/socket.h>
79 #include <sys/mount.h>
80 #include <sys/buf.h>
81 #include <sys/device.h>
82 #include <sys/mbuf.h>
83 #include <sys/file.h>
84 #include <sys/disklabel.h>
85 #include <sys/ioctl.h>
86 #include <sys/errno.h>
87 #include <sys/malloc.h>
88 #include <sys/pool.h>
89 #include <sys/lock.h>
90 #include <sys/sysctl.h>
91 #include <sys/conf.h>
92 #include <sys/kauth.h>
93 #include <sys/wapbl.h>
94 #include <sys/fstrans.h>
95 #include <sys/module.h>
96 
97 #include <miscfs/genfs/genfs.h>
98 #include <miscfs/specfs/specdev.h>
99 
100 #include <ufs/ufs/quota.h>
101 #include <ufs/ufs/ufsmount.h>
102 #include <ufs/ufs/inode.h>
103 #include <ufs/ufs/dir.h>
104 #include <ufs/ufs/ufs_extern.h>
105 #include <ufs/ufs/ufs_bswap.h>
106 #include <ufs/ufs/ufs_wapbl.h>
107 
108 #include <ufs/ffs/fs.h>
109 #include <ufs/ffs/ffs_extern.h>
110 
111 MODULE(MODULE_CLASS_VFS, ffs, NULL);
112 
113 static int	ffs_vfs_fsync(vnode_t *, int);
114 
115 static struct sysctllog *ffs_sysctl_log;
116 
117 /* how many times ffs_init() was called */
118 int ffs_initcount = 0;
119 
120 extern const struct vnodeopv_desc ffs_vnodeop_opv_desc;
121 extern const struct vnodeopv_desc ffs_specop_opv_desc;
122 extern const struct vnodeopv_desc ffs_fifoop_opv_desc;
123 
124 const struct vnodeopv_desc * const ffs_vnodeopv_descs[] = {
125 	&ffs_vnodeop_opv_desc,
126 	&ffs_specop_opv_desc,
127 	&ffs_fifoop_opv_desc,
128 	NULL,
129 };
130 
131 struct vfsops ffs_vfsops = {
132 	MOUNT_FFS,
133 	sizeof (struct ufs_args),
134 	ffs_mount,
135 	ufs_start,
136 	ffs_unmount,
137 	ufs_root,
138 	ufs_quotactl,
139 	ffs_statvfs,
140 	ffs_sync,
141 	ffs_vget,
142 	ffs_fhtovp,
143 	ffs_vptofh,
144 	ffs_init,
145 	ffs_reinit,
146 	ffs_done,
147 	ffs_mountroot,
148 	ffs_snapshot,
149 	ffs_extattrctl,
150 	ffs_suspendctl,
151 	genfs_renamelock_enter,
152 	genfs_renamelock_exit,
153 	ffs_vfs_fsync,
154 	ffs_vnodeopv_descs,
155 	0,
156 	{ NULL, NULL },
157 };
158 
159 static const struct genfs_ops ffs_genfsops = {
160 	.gop_size = ffs_gop_size,
161 	.gop_alloc = ufs_gop_alloc,
162 	.gop_write = genfs_gop_write,
163 	.gop_markupdate = ufs_gop_markupdate,
164 };
165 
166 static const struct ufs_ops ffs_ufsops = {
167 	.uo_itimes = ffs_itimes,
168 	.uo_update = ffs_update,
169 	.uo_truncate = ffs_truncate,
170 	.uo_valloc = ffs_valloc,
171 	.uo_vfree = ffs_vfree,
172 	.uo_balloc = ffs_balloc,
173 	.uo_unmark_vnode = (void (*)(vnode_t *))nullop,
174 };
175 
176 static int
177 ffs_modcmd(modcmd_t cmd, void *arg)
178 {
179 	int error;
180 
181 #if 0
182 	extern int doasyncfree;
183 #endif
184 #ifdef UFS_EXTATTR
185 	extern int ufs_extattr_autocreate;
186 #endif
187 	extern int ffs_log_changeopt;
188 
189 	switch (cmd) {
190 	case MODULE_CMD_INIT:
191 		error = vfs_attach(&ffs_vfsops);
192 		if (error != 0)
193 			break;
194 
195 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
196 			       CTLFLAG_PERMANENT,
197 			       CTLTYPE_NODE, "vfs", NULL,
198 			       NULL, 0, NULL, 0,
199 			       CTL_VFS, CTL_EOL);
200 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
201 			       CTLFLAG_PERMANENT,
202 			       CTLTYPE_NODE, "ffs",
203 			       SYSCTL_DESCR("Berkeley Fast File System"),
204 			       NULL, 0, NULL, 0,
205 			       CTL_VFS, 1, CTL_EOL);
206 		/*
207 		 * @@@ should we even bother with these first three?
208 		 */
209 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
210 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
211 			       CTLTYPE_INT, "doclusterread", NULL,
212 			       sysctl_notavail, 0, NULL, 0,
213 			       CTL_VFS, 1, FFS_CLUSTERREAD, CTL_EOL);
214 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
215 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
216 			       CTLTYPE_INT, "doclusterwrite", NULL,
217 			       sysctl_notavail, 0, NULL, 0,
218 			       CTL_VFS, 1, FFS_CLUSTERWRITE, CTL_EOL);
219 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
220 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
221 			       CTLTYPE_INT, "doreallocblks", NULL,
222 			       sysctl_notavail, 0, NULL, 0,
223 			       CTL_VFS, 1, FFS_REALLOCBLKS, CTL_EOL);
224 #if 0
225 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
226 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
227 			       CTLTYPE_INT, "doasyncfree",
228 			       SYSCTL_DESCR("Release dirty blocks asynchronously"),
229 			       NULL, 0, &doasyncfree, 0,
230 			       CTL_VFS, 1, FFS_ASYNCFREE, CTL_EOL);
231 #endif
232 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
233 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
234 			       CTLTYPE_INT, "log_changeopt",
235 			       SYSCTL_DESCR("Log changes in optimization strategy"),
236 			       NULL, 0, &ffs_log_changeopt, 0,
237 			       CTL_VFS, 1, FFS_LOG_CHANGEOPT, CTL_EOL);
238 #ifdef UFS_EXTATTR
239 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
240 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
241 			       CTLTYPE_INT, "extattr_autocreate",
242 			       SYSCTL_DESCR("Size of attribute for "
243 					    "backing file autocreation"),
244 			       NULL, 0, &ufs_extattr_autocreate, 0,
245 			       CTL_VFS, 1, FFS_EXTATTR_AUTOCREATE, CTL_EOL);
246 
247 #endif /* UFS_EXTATTR */
248 
249 		break;
250 	case MODULE_CMD_FINI:
251 		error = vfs_detach(&ffs_vfsops);
252 		if (error != 0)
253 			break;
254 		sysctl_teardown(&ffs_sysctl_log);
255 		break;
256 	default:
257 		error = ENOTTY;
258 		break;
259 	}
260 
261 	return (error);
262 }
263 
264 pool_cache_t ffs_inode_cache;
265 pool_cache_t ffs_dinode1_cache;
266 pool_cache_t ffs_dinode2_cache;
267 
268 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *, daddr_t);
269 static void ffs_oldfscompat_write(struct fs *, struct ufsmount *);
270 
271 /*
272  * Called by main() when ffs is going to be mounted as root.
273  */
274 
275 int
276 ffs_mountroot(void)
277 {
278 	struct fs *fs;
279 	struct mount *mp;
280 	struct lwp *l = curlwp;			/* XXX */
281 	struct ufsmount *ump;
282 	int error;
283 
284 	if (device_class(root_device) != DV_DISK)
285 		return (ENODEV);
286 
287 	if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp))) {
288 		vrele(rootvp);
289 		return (error);
290 	}
291 
292 	/*
293 	 * We always need to be able to mount the root file system.
294 	 */
295 	mp->mnt_flag |= MNT_FORCE;
296 	if ((error = ffs_mountfs(rootvp, mp, l)) != 0) {
297 		vfs_unbusy(mp, false, NULL);
298 		vfs_destroy(mp);
299 		return (error);
300 	}
301 	mp->mnt_flag &= ~MNT_FORCE;
302 	mutex_enter(&mountlist_lock);
303 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
304 	mutex_exit(&mountlist_lock);
305 	ump = VFSTOUFS(mp);
306 	fs = ump->um_fs;
307 	memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
308 	(void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
309 	(void)ffs_statvfs(mp, &mp->mnt_stat);
310 	vfs_unbusy(mp, false, NULL);
311 	setrootfstime((time_t)fs->fs_time);
312 	return (0);
313 }
314 
315 /*
316  * VFS Operations.
317  *
318  * mount system call
319  */
320 int
321 ffs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
322 {
323 	struct lwp *l = curlwp;
324 	struct vnode *devvp = NULL;
325 	struct ufs_args *args = data;
326 	struct ufsmount *ump = NULL;
327 	struct fs *fs;
328 	int error = 0, flags, update;
329 	mode_t accessmode;
330 
331 	if (*data_len < sizeof *args)
332 		return EINVAL;
333 
334 	if (mp->mnt_flag & MNT_GETARGS) {
335 		ump = VFSTOUFS(mp);
336 		if (ump == NULL)
337 			return EIO;
338 		args->fspec = NULL;
339 		*data_len = sizeof *args;
340 		return 0;
341 	}
342 
343 	update = mp->mnt_flag & MNT_UPDATE;
344 
345 	/* Check arguments */
346 	if (args->fspec != NULL) {
347 		/*
348 		 * Look up the name and verify that it's sane.
349 		 */
350 		error = namei_simple_user(args->fspec,
351 					NSM_FOLLOW_NOEMULROOT, &devvp);
352 		if (error != 0)
353 			return (error);
354 
355 		if (!update) {
356 			/*
357 			 * Be sure this is a valid block device
358 			 */
359 			if (devvp->v_type != VBLK)
360 				error = ENOTBLK;
361 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
362 				error = ENXIO;
363 		} else {
364 			/*
365 			 * Be sure we're still naming the same device
366 			 * used for our initial mount
367 			 */
368 			ump = VFSTOUFS(mp);
369 			if (devvp != ump->um_devvp) {
370 				if (devvp->v_rdev != ump->um_devvp->v_rdev)
371 					error = EINVAL;
372 				else {
373 					vrele(devvp);
374 					devvp = ump->um_devvp;
375 					vref(devvp);
376 				}
377 			}
378 		}
379 	} else {
380 		if (!update) {
381 			/* New mounts must have a filename for the device */
382 			return (EINVAL);
383 		} else {
384 			/* Use the extant mount */
385 			ump = VFSTOUFS(mp);
386 			devvp = ump->um_devvp;
387 			vref(devvp);
388 		}
389 	}
390 
391 	/*
392 	 * If mount by non-root, then verify that user has necessary
393 	 * permissions on the device.
394 	 *
395 	 * Permission to update a mount is checked higher, so here we presume
396 	 * updating the mount is okay (for example, as far as securelevel goes)
397 	 * which leaves us with the normal check.
398 	 */
399 	if (error == 0) {
400 		accessmode = VREAD;
401 		if (update ?
402 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
403 		    (mp->mnt_flag & MNT_RDONLY) == 0)
404 			accessmode |= VWRITE;
405 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
406 		error = genfs_can_mount(devvp, accessmode, l->l_cred);
407 		VOP_UNLOCK(devvp);
408 	}
409 
410 	if (error) {
411 		vrele(devvp);
412 		return (error);
413 	}
414 
415 #ifdef WAPBL
416 	/* WAPBL can only be enabled on a r/w mount. */
417 	if ((mp->mnt_flag & MNT_RDONLY) && !(mp->mnt_iflag & IMNT_WANTRDWR)) {
418 		mp->mnt_flag &= ~MNT_LOG;
419 	}
420 #else /* !WAPBL */
421 	mp->mnt_flag &= ~MNT_LOG;
422 #endif /* !WAPBL */
423 
424 	if (!update) {
425 		int xflags;
426 
427 		if (mp->mnt_flag & MNT_RDONLY)
428 			xflags = FREAD;
429 		else
430 			xflags = FREAD | FWRITE;
431 		error = VOP_OPEN(devvp, xflags, FSCRED);
432 		if (error)
433 			goto fail;
434 		error = ffs_mountfs(devvp, mp, l);
435 		if (error) {
436 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
437 			(void)VOP_CLOSE(devvp, xflags, NOCRED);
438 			VOP_UNLOCK(devvp);
439 			goto fail;
440 		}
441 
442 		ump = VFSTOUFS(mp);
443 		fs = ump->um_fs;
444 	} else {
445 		/*
446 		 * Update the mount.
447 		 */
448 
449 		/*
450 		 * The initial mount got a reference on this
451 		 * device, so drop the one obtained via
452 		 * namei(), above.
453 		 */
454 		vrele(devvp);
455 
456 		ump = VFSTOUFS(mp);
457 		fs = ump->um_fs;
458 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
459 			/*
460 			 * Changing from r/w to r/o
461 			 */
462 			flags = WRITECLOSE;
463 			if (mp->mnt_flag & MNT_FORCE)
464 				flags |= FORCECLOSE;
465 			error = ffs_flushfiles(mp, flags, l);
466 			if (error == 0)
467 				error = UFS_WAPBL_BEGIN(mp);
468 			if (error == 0 &&
469 			    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
470 			    fs->fs_clean & FS_WASCLEAN) {
471 				if (mp->mnt_flag & MNT_SOFTDEP)
472 					fs->fs_flags &= ~FS_DOSOFTDEP;
473 				fs->fs_clean = FS_ISCLEAN;
474 				(void) ffs_sbupdate(ump, MNT_WAIT);
475 			}
476 			if (error == 0)
477 				UFS_WAPBL_END(mp);
478 			if (error)
479 				return (error);
480 		}
481 
482 #ifdef WAPBL
483 		if ((mp->mnt_flag & MNT_LOG) == 0) {
484 			error = ffs_wapbl_stop(mp, mp->mnt_flag & MNT_FORCE);
485 			if (error)
486 				return error;
487 		}
488 #endif /* WAPBL */
489 
490 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
491 			/*
492 			 * Finish change from r/w to r/o
493 			 */
494 			fs->fs_ronly = 1;
495 			fs->fs_fmod = 0;
496 		}
497 
498 		if (mp->mnt_flag & MNT_RELOAD) {
499 			error = ffs_reload(mp, l->l_cred, l);
500 			if (error)
501 				return (error);
502 		}
503 
504 		if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
505 			/*
506 			 * Changing from read-only to read/write
507 			 */
508 #ifndef QUOTA2
509 			if (fs->fs_flags & FS_DOQUOTA2) {
510 				ump->um_flags |= UFS_QUOTA2;
511 				uprintf("%s: options QUOTA2 not enabled%s\n",
512 				    mp->mnt_stat.f_mntonname,
513 				    (mp->mnt_flag & MNT_FORCE) ? "" :
514 				    ", not mounting");
515 				return EINVAL;
516 			}
517 #endif
518 			fs->fs_ronly = 0;
519 			fs->fs_clean <<= 1;
520 			fs->fs_fmod = 1;
521 #ifdef WAPBL
522 			if (fs->fs_flags & FS_DOWAPBL) {
523 				printf("%s: replaying log to disk\n",
524 				    fs->fs_fsmnt);
525 				KDASSERT(mp->mnt_wapbl_replay);
526 				error = wapbl_replay_write(mp->mnt_wapbl_replay,
527 							   devvp);
528 				if (error) {
529 					return error;
530 				}
531 				wapbl_replay_stop(mp->mnt_wapbl_replay);
532 				fs->fs_clean = FS_WASCLEAN;
533 			}
534 #endif /* WAPBL */
535 			if (fs->fs_snapinum[0] != 0)
536 				ffs_snapshot_mount(mp);
537 		}
538 
539 #ifdef WAPBL
540 		error = ffs_wapbl_start(mp);
541 		if (error)
542 			return error;
543 #endif /* WAPBL */
544 
545 #ifdef QUOTA2
546 		if (!fs->fs_ronly) {
547 			error = ffs_quota2_mount(mp);
548 			if (error) {
549 				return error;
550 			}
551 		}
552 #endif
553 		if (args->fspec == NULL)
554 			return 0;
555 	}
556 
557 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
558 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
559 	if (error == 0)
560 		(void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
561 		    sizeof(fs->fs_fsmnt));
562 	fs->fs_flags &= ~FS_DOSOFTDEP;
563 	if (fs->fs_fmod != 0) {	/* XXX */
564 		int err;
565 
566 		fs->fs_fmod = 0;
567 		if (fs->fs_clean & FS_WASCLEAN)
568 			fs->fs_time = time_second;
569 		else {
570 			printf("%s: file system not clean (fs_clean=%#x); "
571 			    "please fsck(8)\n", mp->mnt_stat.f_mntfromname,
572 			    fs->fs_clean);
573 			printf("%s: lost blocks %" PRId64 " files %d\n",
574 			    mp->mnt_stat.f_mntfromname, fs->fs_pendingblocks,
575 			    fs->fs_pendinginodes);
576 		}
577 		err = UFS_WAPBL_BEGIN(mp);
578 		if (err == 0) {
579 			(void) ffs_cgupdate(ump, MNT_WAIT);
580 			UFS_WAPBL_END(mp);
581 		}
582 	}
583 	if ((mp->mnt_flag & MNT_SOFTDEP) != 0) {
584 		printf("%s: `-o softdep' is no longer supported, "
585 		    "consider `-o log'\n", mp->mnt_stat.f_mntfromname);
586 		mp->mnt_flag &= ~MNT_SOFTDEP;
587 	}
588 
589 	return (error);
590 
591 fail:
592 	vrele(devvp);
593 	return (error);
594 }
595 
596 /*
597  * Reload all incore data for a filesystem (used after running fsck on
598  * the root filesystem and finding things to fix). The filesystem must
599  * be mounted read-only.
600  *
601  * Things to do to update the mount:
602  *	1) invalidate all cached meta-data.
603  *	2) re-read superblock from disk.
604  *	3) re-read summary information from disk.
605  *	4) invalidate all inactive vnodes.
606  *	5) invalidate all cached file data.
607  *	6) re-read inode data for all active vnodes.
608  */
609 int
610 ffs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
611 {
612 	struct vnode *vp, *mvp, *devvp;
613 	struct inode *ip;
614 	void *space;
615 	struct buf *bp;
616 	struct fs *fs, *newfs;
617 	struct partinfo dpart;
618 	int i, bsize, blks, error;
619 	int32_t *lp;
620 	struct ufsmount *ump;
621 	daddr_t sblockloc;
622 
623 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
624 		return (EINVAL);
625 
626 	ump = VFSTOUFS(mp);
627 	/*
628 	 * Step 1: invalidate all cached meta-data.
629 	 */
630 	devvp = ump->um_devvp;
631 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
632 	error = vinvalbuf(devvp, 0, cred, l, 0, 0);
633 	VOP_UNLOCK(devvp);
634 	if (error)
635 		panic("ffs_reload: dirty1");
636 	/*
637 	 * Step 2: re-read superblock from disk.
638 	 */
639 	fs = ump->um_fs;
640 
641 	/* XXX we don't handle possibility that superblock moved. */
642 	error = bread(devvp, fs->fs_sblockloc / DEV_BSIZE, fs->fs_sbsize,
643 		      NOCRED, 0, &bp);
644 	if (error) {
645 		brelse(bp, 0);
646 		return (error);
647 	}
648 	newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
649 	memcpy(newfs, bp->b_data, fs->fs_sbsize);
650 #ifdef FFS_EI
651 	if (ump->um_flags & UFS_NEEDSWAP) {
652 		ffs_sb_swap((struct fs*)bp->b_data, newfs);
653 		fs->fs_flags |= FS_SWAPPED;
654 	} else
655 #endif
656 		fs->fs_flags &= ~FS_SWAPPED;
657 	if ((newfs->fs_magic != FS_UFS1_MAGIC &&
658 	     newfs->fs_magic != FS_UFS2_MAGIC)||
659 	     newfs->fs_bsize > MAXBSIZE ||
660 	     newfs->fs_bsize < sizeof(struct fs)) {
661 		brelse(bp, 0);
662 		free(newfs, M_UFSMNT);
663 		return (EIO);		/* XXX needs translation */
664 	}
665 	/* Store off old fs_sblockloc for fs_oldfscompat_read. */
666 	sblockloc = fs->fs_sblockloc;
667 	/*
668 	 * Copy pointer fields back into superblock before copying in	XXX
669 	 * new superblock. These should really be in the ufsmount.	XXX
670 	 * Note that important parameters (eg fs_ncg) are unchanged.
671 	 */
672 	newfs->fs_csp = fs->fs_csp;
673 	newfs->fs_maxcluster = fs->fs_maxcluster;
674 	newfs->fs_contigdirs = fs->fs_contigdirs;
675 	newfs->fs_ronly = fs->fs_ronly;
676 	newfs->fs_active = fs->fs_active;
677 	memcpy(fs, newfs, (u_int)fs->fs_sbsize);
678 	brelse(bp, 0);
679 	free(newfs, M_UFSMNT);
680 
681 	/* Recheck for apple UFS filesystem */
682 	ump->um_flags &= ~UFS_ISAPPLEUFS;
683 	/* First check to see if this is tagged as an Apple UFS filesystem
684 	 * in the disklabel
685 	 */
686 	if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred) == 0) &&
687 		(dpart.part->p_fstype == FS_APPLEUFS)) {
688 		ump->um_flags |= UFS_ISAPPLEUFS;
689 	}
690 #ifdef APPLE_UFS
691 	else {
692 		/* Manually look for an apple ufs label, and if a valid one
693 		 * is found, then treat it like an Apple UFS filesystem anyway
694 		 *
695 		 * EINVAL is most probably a blocksize or alignment problem,
696 		 * it is unlikely that this is an Apple UFS filesystem then.
697 		 */
698 		error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / DEV_BSIZE),
699 			APPLEUFS_LABEL_SIZE, cred, 0, &bp);
700 		if (error && error != EINVAL) {
701 			brelse(bp, 0);
702 			return (error);
703 		}
704 		if (error == 0) {
705 			error = ffs_appleufs_validate(fs->fs_fsmnt,
706 				(struct appleufslabel *)bp->b_data, NULL);
707 			if (error == 0)
708 				ump->um_flags |= UFS_ISAPPLEUFS;
709 		}
710 		brelse(bp, 0);
711 		bp = NULL;
712 	}
713 #else
714 	if (ump->um_flags & UFS_ISAPPLEUFS)
715 		return (EIO);
716 #endif
717 
718 	if (UFS_MPISAPPLEUFS(ump)) {
719 		/* see comment about NeXT below */
720 		ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
721 		ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
722 		mp->mnt_iflag |= IMNT_DTYPE;
723 	} else {
724 		ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
725 		ump->um_dirblksiz = DIRBLKSIZ;
726 		if (ump->um_maxsymlinklen > 0)
727 			mp->mnt_iflag |= IMNT_DTYPE;
728 		else
729 			mp->mnt_iflag &= ~IMNT_DTYPE;
730 	}
731 	ffs_oldfscompat_read(fs, ump, sblockloc);
732 
733 	mutex_enter(&ump->um_lock);
734 	ump->um_maxfilesize = fs->fs_maxfilesize;
735 	if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
736 		uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
737 		    mp->mnt_stat.f_mntonname, fs->fs_flags,
738 		    (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
739 		if ((mp->mnt_flag & MNT_FORCE) == 0) {
740 			mutex_exit(&ump->um_lock);
741 			return (EINVAL);
742 		}
743 	}
744 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
745 		fs->fs_pendingblocks = 0;
746 		fs->fs_pendinginodes = 0;
747 	}
748 	mutex_exit(&ump->um_lock);
749 
750 	ffs_statvfs(mp, &mp->mnt_stat);
751 	/*
752 	 * Step 3: re-read summary information from disk.
753 	 */
754 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
755 	space = fs->fs_csp;
756 	for (i = 0; i < blks; i += fs->fs_frag) {
757 		bsize = fs->fs_bsize;
758 		if (i + fs->fs_frag > blks)
759 			bsize = (blks - i) * fs->fs_fsize;
760 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), bsize,
761 			      NOCRED, 0, &bp);
762 		if (error) {
763 			brelse(bp, 0);
764 			return (error);
765 		}
766 #ifdef FFS_EI
767 		if (UFS_FSNEEDSWAP(fs))
768 			ffs_csum_swap((struct csum *)bp->b_data,
769 			    (struct csum *)space, bsize);
770 		else
771 #endif
772 			memcpy(space, bp->b_data, (size_t)bsize);
773 		space = (char *)space + bsize;
774 		brelse(bp, 0);
775 	}
776 	if (fs->fs_snapinum[0] != 0)
777 		ffs_snapshot_mount(mp);
778 	/*
779 	 * We no longer know anything about clusters per cylinder group.
780 	 */
781 	if (fs->fs_contigsumsize > 0) {
782 		lp = fs->fs_maxcluster;
783 		for (i = 0; i < fs->fs_ncg; i++)
784 			*lp++ = fs->fs_contigsumsize;
785 	}
786 
787 	/* Allocate a marker vnode. */
788 	if ((mvp = vnalloc(mp)) == NULL)
789 		return ENOMEM;
790 	/*
791 	 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
792 	 * and vclean() can be called indirectly
793 	 */
794 	mutex_enter(&mntvnode_lock);
795  loop:
796 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
797 		vmark(mvp, vp);
798 		if (vp->v_mount != mp || vismarker(vp))
799 			continue;
800 		/*
801 		 * Step 4: invalidate all inactive vnodes.
802 		 */
803 		if (vrecycle(vp, &mntvnode_lock, l)) {
804 			mutex_enter(&mntvnode_lock);
805 			(void)vunmark(mvp);
806 			goto loop;
807 		}
808 		/*
809 		 * Step 5: invalidate all cached file data.
810 		 */
811 		mutex_enter(vp->v_interlock);
812 		mutex_exit(&mntvnode_lock);
813 		if (vget(vp, LK_EXCLUSIVE)) {
814 			(void)vunmark(mvp);
815 			goto loop;
816 		}
817 		if (vinvalbuf(vp, 0, cred, l, 0, 0))
818 			panic("ffs_reload: dirty2");
819 		/*
820 		 * Step 6: re-read inode data for all active vnodes.
821 		 */
822 		ip = VTOI(vp);
823 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
824 			      (int)fs->fs_bsize, NOCRED, 0, &bp);
825 		if (error) {
826 			brelse(bp, 0);
827 			vput(vp);
828 			(void)vunmark(mvp);
829 			break;
830 		}
831 		ffs_load_inode(bp, ip, fs, ip->i_number);
832 		brelse(bp, 0);
833 		vput(vp);
834 		mutex_enter(&mntvnode_lock);
835 	}
836 	mutex_exit(&mntvnode_lock);
837 	vnfree(mvp);
838 	return (error);
839 }
840 
841 /*
842  * Possible superblock locations ordered from most to least likely.
843  */
844 static const int sblock_try[] = SBLOCKSEARCH;
845 
846 /*
847  * Common code for mount and mountroot
848  */
849 int
850 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
851 {
852 	struct ufsmount *ump;
853 	struct buf *bp;
854 	struct fs *fs;
855 	dev_t dev;
856 	struct partinfo dpart;
857 	void *space;
858 	daddr_t sblockloc, fsblockloc;
859 	int blks, fstype;
860 	int error, i, bsize, ronly, bset = 0;
861 #ifdef FFS_EI
862 	int needswap = 0;		/* keep gcc happy */
863 #endif
864 	int32_t *lp;
865 	kauth_cred_t cred;
866 	u_int32_t sbsize = 8192;	/* keep gcc happy*/
867 	int32_t fsbsize;
868 
869 	dev = devvp->v_rdev;
870 	cred = l ? l->l_cred : NOCRED;
871 
872 	/* Flush out any old buffers remaining from a previous use. */
873 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
874 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
875 	VOP_UNLOCK(devvp);
876 	if (error)
877 		return (error);
878 
879 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
880 
881 	bp = NULL;
882 	ump = NULL;
883 	fs = NULL;
884 	sblockloc = 0;
885 	fstype = 0;
886 
887 	error = fstrans_mount(mp);
888 	if (error)
889 		return error;
890 
891 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
892 	memset(ump, 0, sizeof *ump);
893 	mutex_init(&ump->um_lock, MUTEX_DEFAULT, IPL_NONE);
894 	error = ffs_snapshot_init(ump);
895 	if (error)
896 		goto out;
897 	ump->um_ops = &ffs_ufsops;
898 
899 #ifdef WAPBL
900  sbagain:
901 #endif
902 	/*
903 	 * Try reading the superblock in each of its possible locations.
904 	 */
905 	for (i = 0; ; i++) {
906 		if (bp != NULL) {
907 			brelse(bp, BC_NOCACHE);
908 			bp = NULL;
909 		}
910 		if (sblock_try[i] == -1) {
911 			error = EINVAL;
912 			fs = NULL;
913 			goto out;
914 		}
915 		error = bread(devvp, sblock_try[i] / DEV_BSIZE, SBLOCKSIZE, cred,
916 			      0, &bp);
917 		if (error) {
918 			fs = NULL;
919 			goto out;
920 		}
921 		fs = (struct fs*)bp->b_data;
922 		fsblockloc = sblockloc = sblock_try[i];
923 		if (fs->fs_magic == FS_UFS1_MAGIC) {
924 			sbsize = fs->fs_sbsize;
925 			fstype = UFS1;
926 			fsbsize = fs->fs_bsize;
927 #ifdef FFS_EI
928 			needswap = 0;
929 		} else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC)) {
930 			sbsize = bswap32(fs->fs_sbsize);
931 			fstype = UFS1;
932 			fsbsize = bswap32(fs->fs_bsize);
933 			needswap = 1;
934 #endif
935 		} else if (fs->fs_magic == FS_UFS2_MAGIC) {
936 			sbsize = fs->fs_sbsize;
937 			fstype = UFS2;
938 			fsbsize = fs->fs_bsize;
939 #ifdef FFS_EI
940 			needswap = 0;
941 		} else if (fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
942 			sbsize = bswap32(fs->fs_sbsize);
943 			fstype = UFS2;
944 			fsbsize = bswap32(fs->fs_bsize);
945 			needswap = 1;
946 #endif
947 		} else
948 			continue;
949 
950 
951 		/* fs->fs_sblockloc isn't defined for old filesystems */
952 		if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) {
953 			if (sblockloc == SBLOCK_UFS2)
954 				/*
955 				 * This is likely to be the first alternate
956 				 * in a filesystem with 64k blocks.
957 				 * Don't use it.
958 				 */
959 				continue;
960 			fsblockloc = sblockloc;
961 		} else {
962 			fsblockloc = fs->fs_sblockloc;
963 #ifdef FFS_EI
964 			if (needswap)
965 				fsblockloc = bswap64(fsblockloc);
966 #endif
967 		}
968 
969 		/* Check we haven't found an alternate superblock */
970 		if (fsblockloc != sblockloc)
971 			continue;
972 
973 		/* Validate size of superblock */
974 		if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs))
975 			continue;
976 
977 		/* Check that we can handle the file system blocksize */
978 		if (fsbsize > MAXBSIZE) {
979 			printf("ffs_mountfs: block size (%d) > MAXBSIZE (%d)\n",
980 			    fsbsize, MAXBSIZE);
981 			continue;
982 		}
983 
984 		/* Ok seems to be a good superblock */
985 		break;
986 	}
987 
988 	fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
989 	memcpy(fs, bp->b_data, sbsize);
990 	ump->um_fs = fs;
991 
992 #ifdef FFS_EI
993 	if (needswap) {
994 		ffs_sb_swap((struct fs*)bp->b_data, fs);
995 		fs->fs_flags |= FS_SWAPPED;
996 	} else
997 #endif
998 		fs->fs_flags &= ~FS_SWAPPED;
999 
1000 #ifdef WAPBL
1001 	if ((mp->mnt_wapbl_replay == 0) && (fs->fs_flags & FS_DOWAPBL)) {
1002 		error = ffs_wapbl_replay_start(mp, fs, devvp);
1003 		if (error && (mp->mnt_flag & MNT_FORCE) == 0)
1004 			goto out;
1005 		if (!error) {
1006 			if (!ronly) {
1007 				/* XXX fsmnt may be stale. */
1008 				printf("%s: replaying log to disk\n",
1009 				    fs->fs_fsmnt);
1010 				error = wapbl_replay_write(mp->mnt_wapbl_replay,
1011 				    devvp);
1012 				if (error)
1013 					goto out;
1014 				wapbl_replay_stop(mp->mnt_wapbl_replay);
1015 				fs->fs_clean = FS_WASCLEAN;
1016 			} else {
1017 				/* XXX fsmnt may be stale */
1018 				printf("%s: replaying log to memory\n",
1019 				    fs->fs_fsmnt);
1020 			}
1021 
1022 			/* Force a re-read of the superblock */
1023 			brelse(bp, BC_INVAL);
1024 			bp = NULL;
1025 			free(fs, M_UFSMNT);
1026 			fs = NULL;
1027 			goto sbagain;
1028 		}
1029 	}
1030 #else /* !WAPBL */
1031 	if ((fs->fs_flags & FS_DOWAPBL) && (mp->mnt_flag & MNT_FORCE) == 0) {
1032 		error = EPERM;
1033 		goto out;
1034 	}
1035 #endif /* !WAPBL */
1036 
1037 	ffs_oldfscompat_read(fs, ump, sblockloc);
1038 	ump->um_maxfilesize = fs->fs_maxfilesize;
1039 
1040 	if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
1041 		uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
1042 		    mp->mnt_stat.f_mntonname, fs->fs_flags,
1043 		    (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
1044 		if ((mp->mnt_flag & MNT_FORCE) == 0) {
1045 			error = EINVAL;
1046 			goto out;
1047 		}
1048 	}
1049 
1050 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1051 		fs->fs_pendingblocks = 0;
1052 		fs->fs_pendinginodes = 0;
1053 	}
1054 
1055 	ump->um_fstype = fstype;
1056 	if (fs->fs_sbsize < SBLOCKSIZE)
1057 		brelse(bp, BC_INVAL);
1058 	else
1059 		brelse(bp, 0);
1060 	bp = NULL;
1061 
1062 	/* First check to see if this is tagged as an Apple UFS filesystem
1063 	 * in the disklabel
1064 	 */
1065 	if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred) == 0) &&
1066 		(dpart.part->p_fstype == FS_APPLEUFS)) {
1067 		ump->um_flags |= UFS_ISAPPLEUFS;
1068 	}
1069 #ifdef APPLE_UFS
1070 	else {
1071 		/* Manually look for an apple ufs label, and if a valid one
1072 		 * is found, then treat it like an Apple UFS filesystem anyway
1073 		 */
1074 		error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / DEV_BSIZE),
1075 			APPLEUFS_LABEL_SIZE, cred, 0, &bp);
1076 		if (error)
1077 			goto out;
1078 		error = ffs_appleufs_validate(fs->fs_fsmnt,
1079 			(struct appleufslabel *)bp->b_data, NULL);
1080 		if (error == 0) {
1081 			ump->um_flags |= UFS_ISAPPLEUFS;
1082 		}
1083 		brelse(bp, 0);
1084 		bp = NULL;
1085 	}
1086 #else
1087 	if (ump->um_flags & UFS_ISAPPLEUFS) {
1088 		error = EINVAL;
1089 		goto out;
1090 	}
1091 #endif
1092 
1093 #if 0
1094 /*
1095  * XXX This code changes the behaviour of mounting dirty filesystems, to
1096  * XXX require "mount -f ..." to mount them.  This doesn't match what
1097  * XXX mount(8) describes and is disabled for now.
1098  */
1099 	/*
1100 	 * If the file system is not clean, don't allow it to be mounted
1101 	 * unless MNT_FORCE is specified.  (Note: MNT_FORCE is always set
1102 	 * for the root file system.)
1103 	 */
1104 	if (fs->fs_flags & FS_DOWAPBL) {
1105 		/*
1106 		 * wapbl normally expects to be FS_WASCLEAN when the FS_DOWAPBL
1107 		 * bit is set, although there's a window in unmount where it
1108 		 * could be FS_ISCLEAN
1109 		 */
1110 		if ((mp->mnt_flag & MNT_FORCE) == 0 &&
1111 		    (fs->fs_clean & (FS_WASCLEAN | FS_ISCLEAN)) == 0) {
1112 			error = EPERM;
1113 			goto out;
1114 		}
1115 	} else
1116 		if ((fs->fs_clean & FS_ISCLEAN) == 0 &&
1117 		    (mp->mnt_flag & MNT_FORCE) == 0) {
1118 			error = EPERM;
1119 			goto out;
1120 		}
1121 #endif
1122 
1123 	/*
1124 	 * verify that we can access the last block in the fs
1125 	 * if we're mounting read/write.
1126 	 */
1127 
1128 	if (!ronly) {
1129 		error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
1130 		    cred, 0, &bp);
1131 		if (bp->b_bcount != fs->fs_fsize)
1132 			error = EINVAL;
1133 		if (error) {
1134 			bset = BC_INVAL;
1135 			goto out;
1136 		}
1137 		brelse(bp, BC_INVAL);
1138 		bp = NULL;
1139 	}
1140 
1141 	fs->fs_ronly = ronly;
1142 	/* Don't bump fs_clean if we're replaying journal */
1143 	if (!((fs->fs_flags & FS_DOWAPBL) && (fs->fs_clean & FS_WASCLEAN)))
1144 		if (ronly == 0) {
1145 			fs->fs_clean <<= 1;
1146 			fs->fs_fmod = 1;
1147 		}
1148 	bsize = fs->fs_cssize;
1149 	blks = howmany(bsize, fs->fs_fsize);
1150 	if (fs->fs_contigsumsize > 0)
1151 		bsize += fs->fs_ncg * sizeof(int32_t);
1152 	bsize += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
1153 	space = malloc((u_long)bsize, M_UFSMNT, M_WAITOK);
1154 	fs->fs_csp = space;
1155 	for (i = 0; i < blks; i += fs->fs_frag) {
1156 		bsize = fs->fs_bsize;
1157 		if (i + fs->fs_frag > blks)
1158 			bsize = (blks - i) * fs->fs_fsize;
1159 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), bsize,
1160 			      cred, 0, &bp);
1161 		if (error) {
1162 			free(fs->fs_csp, M_UFSMNT);
1163 			goto out;
1164 		}
1165 #ifdef FFS_EI
1166 		if (needswap)
1167 			ffs_csum_swap((struct csum *)bp->b_data,
1168 				(struct csum *)space, bsize);
1169 		else
1170 #endif
1171 			memcpy(space, bp->b_data, (u_int)bsize);
1172 
1173 		space = (char *)space + bsize;
1174 		brelse(bp, 0);
1175 		bp = NULL;
1176 	}
1177 	if (fs->fs_contigsumsize > 0) {
1178 		fs->fs_maxcluster = lp = space;
1179 		for (i = 0; i < fs->fs_ncg; i++)
1180 			*lp++ = fs->fs_contigsumsize;
1181 		space = lp;
1182 	}
1183 	bsize = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
1184 	fs->fs_contigdirs = space;
1185 	space = (char *)space + bsize;
1186 	memset(fs->fs_contigdirs, 0, bsize);
1187 		/* Compatibility for old filesystems - XXX */
1188 	if (fs->fs_avgfilesize <= 0)
1189 		fs->fs_avgfilesize = AVFILESIZ;
1190 	if (fs->fs_avgfpdir <= 0)
1191 		fs->fs_avgfpdir = AFPDIR;
1192 	fs->fs_active = NULL;
1193 	mp->mnt_data = ump;
1194 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
1195 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
1196 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
1197 	mp->mnt_stat.f_namemax = FFS_MAXNAMLEN;
1198 	if (UFS_MPISAPPLEUFS(ump)) {
1199 		/* NeXT used to keep short symlinks in the inode even
1200 		 * when using FS_42INODEFMT.  In that case fs->fs_maxsymlinklen
1201 		 * is probably -1, but we still need to be able to identify
1202 		 * short symlinks.
1203 		 */
1204 		ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
1205 		ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
1206 		mp->mnt_iflag |= IMNT_DTYPE;
1207 	} else {
1208 		ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
1209 		ump->um_dirblksiz = DIRBLKSIZ;
1210 		if (ump->um_maxsymlinklen > 0)
1211 			mp->mnt_iflag |= IMNT_DTYPE;
1212 		else
1213 			mp->mnt_iflag &= ~IMNT_DTYPE;
1214 	}
1215 	mp->mnt_fs_bshift = fs->fs_bshift;
1216 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
1217 	mp->mnt_flag |= MNT_LOCAL;
1218 	mp->mnt_iflag |= IMNT_MPSAFE;
1219 #ifdef FFS_EI
1220 	if (needswap)
1221 		ump->um_flags |= UFS_NEEDSWAP;
1222 #endif
1223 	ump->um_mountp = mp;
1224 	ump->um_dev = dev;
1225 	ump->um_devvp = devvp;
1226 	ump->um_nindir = fs->fs_nindir;
1227 	ump->um_lognindir = ffs(fs->fs_nindir) - 1;
1228 	ump->um_bptrtodb = fs->fs_fshift - DEV_BSHIFT;
1229 	ump->um_seqinc = fs->fs_frag;
1230 	for (i = 0; i < MAXQUOTAS; i++)
1231 		ump->um_quotas[i] = NULLVP;
1232 	devvp->v_specmountpoint = mp;
1233 	if (ronly == 0 && fs->fs_snapinum[0] != 0)
1234 		ffs_snapshot_mount(mp);
1235 #ifdef WAPBL
1236 	if (!ronly) {
1237 		KDASSERT(fs->fs_ronly == 0);
1238 		/*
1239 		 * ffs_wapbl_start() needs mp->mnt_stat initialised if it
1240 		 * needs to create a new log file in-filesystem.
1241 		 */
1242 		ffs_statvfs(mp, &mp->mnt_stat);
1243 
1244 		error = ffs_wapbl_start(mp);
1245 		if (error) {
1246 			free(fs->fs_csp, M_UFSMNT);
1247 			goto out;
1248 		}
1249 	}
1250 #endif /* WAPBL */
1251 	if (ronly == 0) {
1252 #ifdef QUOTA2
1253 		error = ffs_quota2_mount(mp);
1254 		if (error) {
1255 			free(fs->fs_csp, M_UFSMNT);
1256 			goto out;
1257 		}
1258 #else
1259 		if (fs->fs_flags & FS_DOQUOTA2) {
1260 			ump->um_flags |= UFS_QUOTA2;
1261 			uprintf("%s: options QUOTA2 not enabled%s\n",
1262 			    mp->mnt_stat.f_mntonname,
1263 			    (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
1264 			if ((mp->mnt_flag & MNT_FORCE) == 0) {
1265 				error = EINVAL;
1266 				free(fs->fs_csp, M_UFSMNT);
1267 				goto out;
1268 			}
1269 		}
1270 #endif
1271 	 }
1272 #ifdef UFS_EXTATTR
1273 	/*
1274 	 * Initialize file-backed extended attributes on UFS1 file
1275 	 * systems.
1276 	 */
1277 	if (ump->um_fstype == UFS1)
1278 		ufs_extattr_uepm_init(&ump->um_extattr);
1279 #endif /* UFS_EXTATTR */
1280 
1281 	return (0);
1282 out:
1283 #ifdef WAPBL
1284 	if (mp->mnt_wapbl_replay) {
1285 		wapbl_replay_stop(mp->mnt_wapbl_replay);
1286 		wapbl_replay_free(mp->mnt_wapbl_replay);
1287 		mp->mnt_wapbl_replay = 0;
1288 	}
1289 #endif
1290 
1291 	fstrans_unmount(mp);
1292 	if (fs)
1293 		free(fs, M_UFSMNT);
1294 	devvp->v_specmountpoint = NULL;
1295 	if (bp)
1296 		brelse(bp, bset);
1297 	if (ump) {
1298 		if (ump->um_oldfscompat)
1299 			free(ump->um_oldfscompat, M_UFSMNT);
1300 		mutex_destroy(&ump->um_lock);
1301 		free(ump, M_UFSMNT);
1302 		mp->mnt_data = NULL;
1303 	}
1304 	return (error);
1305 }
1306 
1307 /*
1308  * Sanity checks for loading old filesystem superblocks.
1309  * See ffs_oldfscompat_write below for unwound actions.
1310  *
1311  * XXX - Parts get retired eventually.
1312  * Unfortunately new bits get added.
1313  */
1314 static void
1315 ffs_oldfscompat_read(struct fs *fs, struct ufsmount *ump, daddr_t sblockloc)
1316 {
1317 	off_t maxfilesize;
1318 	int32_t *extrasave;
1319 
1320 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1321 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
1322 		return;
1323 
1324 	if (!ump->um_oldfscompat)
1325 		ump->um_oldfscompat = malloc(512 + 3*sizeof(int32_t),
1326 		    M_UFSMNT, M_WAITOK);
1327 
1328 	memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
1329 	extrasave = ump->um_oldfscompat;
1330 	extrasave += 512/sizeof(int32_t);
1331 	extrasave[0] = fs->fs_old_npsect;
1332 	extrasave[1] = fs->fs_old_interleave;
1333 	extrasave[2] = fs->fs_old_trackskew;
1334 
1335 	/* These fields will be overwritten by their
1336 	 * original values in fs_oldfscompat_write, so it is harmless
1337 	 * to modify them here.
1338 	 */
1339 	fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1340 	fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1341 	fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1342 	fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1343 
1344 	fs->fs_maxbsize = fs->fs_bsize;
1345 	fs->fs_time = fs->fs_old_time;
1346 	fs->fs_size = fs->fs_old_size;
1347 	fs->fs_dsize = fs->fs_old_dsize;
1348 	fs->fs_csaddr = fs->fs_old_csaddr;
1349 	fs->fs_sblockloc = sblockloc;
1350 
1351 	fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL);
1352 
1353 	if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
1354 		fs->fs_old_nrpos = 8;
1355 		fs->fs_old_npsect = fs->fs_old_nsect;
1356 		fs->fs_old_interleave = 1;
1357 		fs->fs_old_trackskew = 0;
1358 	}
1359 
1360 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {
1361 		fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
1362 		fs->fs_qbmask = ~fs->fs_bmask;
1363 		fs->fs_qfmask = ~fs->fs_fmask;
1364 	}
1365 
1366 	maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
1367 	if (fs->fs_maxfilesize > maxfilesize)
1368 		fs->fs_maxfilesize = maxfilesize;
1369 
1370 	/* Compatibility for old filesystems */
1371 	if (fs->fs_avgfilesize <= 0)
1372 		fs->fs_avgfilesize = AVFILESIZ;
1373 	if (fs->fs_avgfpdir <= 0)
1374 		fs->fs_avgfpdir = AFPDIR;
1375 
1376 #if 0
1377 	if (bigcgs) {
1378 		fs->fs_save_cgsize = fs->fs_cgsize;
1379 		fs->fs_cgsize = fs->fs_bsize;
1380 	}
1381 #endif
1382 }
1383 
1384 /*
1385  * Unwinding superblock updates for old filesystems.
1386  * See ffs_oldfscompat_read above for details.
1387  *
1388  * XXX - Parts get retired eventually.
1389  * Unfortunately new bits get added.
1390  */
1391 static void
1392 ffs_oldfscompat_write(struct fs *fs, struct ufsmount *ump)
1393 {
1394 	int32_t *extrasave;
1395 
1396 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1397 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
1398 		return;
1399 
1400 	fs->fs_old_time = fs->fs_time;
1401 	fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1402 	fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1403 	fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1404 	fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1405 	fs->fs_old_flags = fs->fs_flags;
1406 
1407 #if 0
1408 	if (bigcgs) {
1409 		fs->fs_cgsize = fs->fs_save_cgsize;
1410 	}
1411 #endif
1412 
1413 	memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
1414 	extrasave = ump->um_oldfscompat;
1415 	extrasave += 512/sizeof(int32_t);
1416 	fs->fs_old_npsect = extrasave[0];
1417 	fs->fs_old_interleave = extrasave[1];
1418 	fs->fs_old_trackskew = extrasave[2];
1419 
1420 }
1421 
1422 /*
1423  * unmount vfs operation
1424  */
1425 int
1426 ffs_unmount(struct mount *mp, int mntflags)
1427 {
1428 	struct lwp *l = curlwp;
1429 	struct ufsmount *ump = VFSTOUFS(mp);
1430 	struct fs *fs = ump->um_fs;
1431 	int error, flags;
1432 #ifdef WAPBL
1433 	extern int doforce;
1434 #endif
1435 
1436 	flags = 0;
1437 	if (mntflags & MNT_FORCE)
1438 		flags |= FORCECLOSE;
1439 	if ((error = ffs_flushfiles(mp, flags, l)) != 0)
1440 		return (error);
1441 	error = UFS_WAPBL_BEGIN(mp);
1442 	if (error == 0)
1443 		if (fs->fs_ronly == 0 &&
1444 		    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
1445 		    fs->fs_clean & FS_WASCLEAN) {
1446 			fs->fs_clean = FS_ISCLEAN;
1447 			fs->fs_fmod = 0;
1448 			(void) ffs_sbupdate(ump, MNT_WAIT);
1449 		}
1450 	if (error == 0)
1451 		UFS_WAPBL_END(mp);
1452 #ifdef WAPBL
1453 	KASSERT(!(mp->mnt_wapbl_replay && mp->mnt_wapbl));
1454 	if (mp->mnt_wapbl_replay) {
1455 		KDASSERT(fs->fs_ronly);
1456 		wapbl_replay_stop(mp->mnt_wapbl_replay);
1457 		wapbl_replay_free(mp->mnt_wapbl_replay);
1458 		mp->mnt_wapbl_replay = 0;
1459 	}
1460 	error = ffs_wapbl_stop(mp, doforce && (mntflags & MNT_FORCE));
1461 	if (error) {
1462 		return error;
1463 	}
1464 #endif /* WAPBL */
1465 #ifdef UFS_EXTATTR
1466 	if (ump->um_fstype == UFS1) {
1467 		ufs_extattr_stop(mp, l);
1468 		ufs_extattr_uepm_destroy(&ump->um_extattr);
1469 	}
1470 #endif /* UFS_EXTATTR */
1471 
1472 	if (ump->um_devvp->v_type != VBAD)
1473 		ump->um_devvp->v_specmountpoint = NULL;
1474 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1475 	(void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD | FWRITE,
1476 		NOCRED);
1477 	vput(ump->um_devvp);
1478 	free(fs->fs_csp, M_UFSMNT);
1479 	free(fs, M_UFSMNT);
1480 	if (ump->um_oldfscompat != NULL)
1481 		free(ump->um_oldfscompat, M_UFSMNT);
1482 	mutex_destroy(&ump->um_lock);
1483 	ffs_snapshot_fini(ump);
1484 	free(ump, M_UFSMNT);
1485 	mp->mnt_data = NULL;
1486 	mp->mnt_flag &= ~MNT_LOCAL;
1487 	fstrans_unmount(mp);
1488 	return (0);
1489 }
1490 
1491 /*
1492  * Flush out all the files in a filesystem.
1493  */
1494 int
1495 ffs_flushfiles(struct mount *mp, int flags, struct lwp *l)
1496 {
1497 	extern int doforce;
1498 	struct ufsmount *ump;
1499 	int error;
1500 
1501 	if (!doforce)
1502 		flags &= ~FORCECLOSE;
1503 	ump = VFSTOUFS(mp);
1504 #ifdef QUOTA
1505 	if ((error = quota1_umount(mp, flags)) != 0)
1506 		return (error);
1507 #endif
1508 #ifdef QUOTA2
1509 	if ((error = quota2_umount(mp, flags)) != 0)
1510 		return (error);
1511 #endif
1512 	if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
1513 		return (error);
1514 	ffs_snapshot_unmount(mp);
1515 	/*
1516 	 * Flush all the files.
1517 	 */
1518 	error = vflush(mp, NULLVP, flags);
1519 	if (error)
1520 		return (error);
1521 	/*
1522 	 * Flush filesystem metadata.
1523 	 */
1524 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1525 	error = VOP_FSYNC(ump->um_devvp, l->l_cred, FSYNC_WAIT, 0, 0);
1526 	VOP_UNLOCK(ump->um_devvp);
1527 	if (flags & FORCECLOSE) /* XXXDBJ */
1528 		error = 0;
1529 
1530 #ifdef WAPBL
1531 	if (error)
1532 		return error;
1533 	if (mp->mnt_wapbl) {
1534 		error = wapbl_flush(mp->mnt_wapbl, 1);
1535 		if (flags & FORCECLOSE)
1536 			error = 0;
1537 	}
1538 #endif
1539 
1540 	return (error);
1541 }
1542 
1543 /*
1544  * Get file system statistics.
1545  */
1546 int
1547 ffs_statvfs(struct mount *mp, struct statvfs *sbp)
1548 {
1549 	struct ufsmount *ump;
1550 	struct fs *fs;
1551 
1552 	ump = VFSTOUFS(mp);
1553 	fs = ump->um_fs;
1554 	mutex_enter(&ump->um_lock);
1555 	sbp->f_bsize = fs->fs_bsize;
1556 	sbp->f_frsize = fs->fs_fsize;
1557 	sbp->f_iosize = fs->fs_bsize;
1558 	sbp->f_blocks = fs->fs_dsize;
1559 	sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
1560 	    fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1561 	sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
1562 	    fs->fs_minfree) / (u_int64_t) 100;
1563 	if (sbp->f_bfree > sbp->f_bresvd)
1564 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
1565 	else
1566 		sbp->f_bavail = 0;
1567 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
1568 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1569 	sbp->f_favail = sbp->f_ffree;
1570 	sbp->f_fresvd = 0;
1571 	mutex_exit(&ump->um_lock);
1572 	copy_statvfs_info(sbp, mp);
1573 
1574 	return (0);
1575 }
1576 
1577 /*
1578  * Go through the disk queues to initiate sandbagged IO;
1579  * go through the inodes to write those that have been modified;
1580  * initiate the writing of the super block if it has been modified.
1581  *
1582  * Note: we are always called with the filesystem marked `MPBUSY'.
1583  */
1584 int
1585 ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
1586 {
1587 	struct vnode *vp, *mvp, *nvp;
1588 	struct inode *ip;
1589 	struct ufsmount *ump = VFSTOUFS(mp);
1590 	struct fs *fs;
1591 	int error, allerror = 0;
1592 	bool is_suspending;
1593 
1594 	fs = ump->um_fs;
1595 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
1596 		printf("fs = %s\n", fs->fs_fsmnt);
1597 		panic("update: rofs mod");
1598 	}
1599 
1600 	/* Allocate a marker vnode. */
1601 	if ((mvp = vnalloc(mp)) == NULL)
1602 		return (ENOMEM);
1603 
1604 	fstrans_start(mp, FSTRANS_SHARED);
1605 	is_suspending = (fstrans_getstate(mp) == FSTRANS_SUSPENDING);
1606 	/*
1607 	 * Write back each (modified) inode.
1608 	 */
1609 	mutex_enter(&mntvnode_lock);
1610 loop:
1611 	/*
1612 	 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
1613 	 * and vclean() can be called indirectly
1614 	 */
1615 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
1616 		nvp = TAILQ_NEXT(vp, v_mntvnodes);
1617 		/*
1618 		 * If the vnode that we are about to sync is no longer
1619 		 * associated with this mount point, start over.
1620 		 */
1621 		if (vp->v_mount != mp)
1622 			goto loop;
1623 		/*
1624 		 * Don't interfere with concurrent scans of this FS.
1625 		 */
1626 		if (vismarker(vp))
1627 			continue;
1628 		mutex_enter(vp->v_interlock);
1629 		ip = VTOI(vp);
1630 
1631 		/*
1632 		 * Skip the vnode/inode if inaccessible.
1633 		 */
1634 		if (ip == NULL || (vp->v_iflag & (VI_XLOCK | VI_CLEAN)) != 0 ||
1635 		    vp->v_type == VNON) {
1636 			mutex_exit(vp->v_interlock);
1637 			continue;
1638 		}
1639 
1640 		/*
1641 		 * We deliberately update inode times here.  This will
1642 		 * prevent a massive queue of updates accumulating, only
1643 		 * to be handled by a call to unmount.
1644 		 *
1645 		 * XXX It would be better to have the syncer trickle these
1646 		 * out.  Adjustment needed to allow registering vnodes for
1647 		 * sync when the vnode is clean, but the inode dirty.  Or
1648 		 * have ufs itself trickle out inode updates.
1649 		 *
1650 		 * If doing a lazy sync, we don't care about metadata or
1651 		 * data updates, because they are handled by each vnode's
1652 		 * synclist entry.  In this case we are only interested in
1653 		 * writing back modified inodes.
1654 		 */
1655 		if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE |
1656 		    IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) == 0 &&
1657 		    (waitfor == MNT_LAZY || (LIST_EMPTY(&vp->v_dirtyblkhd) &&
1658 		    UVM_OBJ_IS_CLEAN(&vp->v_uobj)))) {
1659 			mutex_exit(vp->v_interlock);
1660 			continue;
1661 		}
1662 		if (vp->v_type == VBLK && is_suspending) {
1663 			mutex_exit(vp->v_interlock);
1664 			continue;
1665 		}
1666 		vmark(mvp, vp);
1667 		mutex_exit(&mntvnode_lock);
1668 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT);
1669 		if (error) {
1670 			mutex_enter(&mntvnode_lock);
1671 			nvp = vunmark(mvp);
1672 			if (error == ENOENT) {
1673 				goto loop;
1674 			}
1675 			continue;
1676 		}
1677 		if (waitfor == MNT_LAZY) {
1678 			error = UFS_WAPBL_BEGIN(vp->v_mount);
1679 			if (!error) {
1680 				error = ffs_update(vp, NULL, NULL,
1681 				    UPDATE_CLOSE);
1682 				UFS_WAPBL_END(vp->v_mount);
1683 			}
1684 		} else {
1685 			error = VOP_FSYNC(vp, cred, FSYNC_NOLOG |
1686 			    (waitfor == MNT_WAIT ? FSYNC_WAIT : 0), 0, 0);
1687 		}
1688 		if (error)
1689 			allerror = error;
1690 		vput(vp);
1691 		mutex_enter(&mntvnode_lock);
1692 		nvp = vunmark(mvp);
1693 	}
1694 	mutex_exit(&mntvnode_lock);
1695 	/*
1696 	 * Force stale file system control information to be flushed.
1697 	 */
1698 	if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
1699 	    !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
1700 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1701 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
1702 		    (waitfor == MNT_WAIT ? FSYNC_WAIT : 0) | FSYNC_NOLOG,
1703 		    0, 0)) != 0)
1704 			allerror = error;
1705 		VOP_UNLOCK(ump->um_devvp);
1706 		if (allerror == 0 && waitfor == MNT_WAIT && !mp->mnt_wapbl) {
1707 			mutex_enter(&mntvnode_lock);
1708 			goto loop;
1709 		}
1710 	}
1711 #if defined(QUOTA) || defined(QUOTA2)
1712 	qsync(mp);
1713 #endif
1714 	/*
1715 	 * Write back modified superblock.
1716 	 */
1717 	if (fs->fs_fmod != 0) {
1718 		fs->fs_fmod = 0;
1719 		fs->fs_time = time_second;
1720 		error = UFS_WAPBL_BEGIN(mp);
1721 		if (error)
1722 			allerror = error;
1723 		else {
1724 			if ((error = ffs_cgupdate(ump, waitfor)))
1725 				allerror = error;
1726 			UFS_WAPBL_END(mp);
1727 		}
1728 	}
1729 
1730 #ifdef WAPBL
1731 	if (mp->mnt_wapbl) {
1732 		error = wapbl_flush(mp->mnt_wapbl, 0);
1733 		if (error)
1734 			allerror = error;
1735 	}
1736 #endif
1737 
1738 	fstrans_done(mp);
1739 	vnfree(mvp);
1740 	return (allerror);
1741 }
1742 
1743 /*
1744  * Look up a FFS dinode number to find its incore vnode, otherwise read it
1745  * in from disk.  If it is in core, wait for the lock bit to clear, then
1746  * return the inode locked.  Detection and handling of mount points must be
1747  * done by the calling routine.
1748  */
1749 int
1750 ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1751 {
1752 	struct fs *fs;
1753 	struct inode *ip;
1754 	struct ufsmount *ump;
1755 	struct buf *bp;
1756 	struct vnode *vp;
1757 	dev_t dev;
1758 	int error;
1759 
1760 	ump = VFSTOUFS(mp);
1761 	dev = ump->um_dev;
1762 
1763  retry:
1764 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1765 		return (0);
1766 
1767 	/* Allocate a new vnode/inode. */
1768 	error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, NULL, &vp);
1769 	if (error) {
1770 		*vpp = NULL;
1771 		return (error);
1772 	}
1773 	ip = pool_cache_get(ffs_inode_cache, PR_WAITOK);
1774 
1775 	/*
1776 	 * If someone beat us to it, put back the freshly allocated
1777 	 * vnode/inode pair and retry.
1778 	 */
1779 	mutex_enter(&ufs_hashlock);
1780 	if (ufs_ihashget(dev, ino, 0) != NULL) {
1781 		mutex_exit(&ufs_hashlock);
1782 		ungetnewvnode(vp);
1783 		pool_cache_put(ffs_inode_cache, ip);
1784 		goto retry;
1785 	}
1786 
1787 	vp->v_vflag |= VV_LOCKSWORK;
1788 
1789 	/*
1790 	 * XXX MFS ends up here, too, to allocate an inode.  Should we
1791 	 * XXX create another pool for MFS inodes?
1792 	 */
1793 
1794 	memset(ip, 0, sizeof(struct inode));
1795 	vp->v_data = ip;
1796 	ip->i_vnode = vp;
1797 	ip->i_ump = ump;
1798 	ip->i_fs = fs = ump->um_fs;
1799 	ip->i_dev = dev;
1800 	ip->i_number = ino;
1801 #if defined(QUOTA) || defined(QUOTA2)
1802 	ufsquota_init(ip);
1803 #endif
1804 
1805 	/*
1806 	 * Initialize genfs node, we might proceed to destroy it in
1807 	 * error branches.
1808 	 */
1809 	genfs_node_init(vp, &ffs_genfsops);
1810 
1811 	/*
1812 	 * Put it onto its hash chain and lock it so that other requests for
1813 	 * this inode will block if they arrive while we are sleeping waiting
1814 	 * for old data structures to be purged or for the contents of the
1815 	 * disk portion of this inode to be read.
1816 	 */
1817 
1818 	ufs_ihashins(ip);
1819 	mutex_exit(&ufs_hashlock);
1820 
1821 	/* Read in the disk contents for the inode, copy into the inode. */
1822 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1823 		      (int)fs->fs_bsize, NOCRED, 0, &bp);
1824 	if (error) {
1825 
1826 		/*
1827 		 * The inode does not contain anything useful, so it would
1828 		 * be misleading to leave it on its hash chain. With mode
1829 		 * still zero, it will be unlinked and returned to the free
1830 		 * list by vput().
1831 		 */
1832 
1833 		vput(vp);
1834 		brelse(bp, 0);
1835 		*vpp = NULL;
1836 		return (error);
1837 	}
1838 	if (ip->i_ump->um_fstype == UFS1)
1839 		ip->i_din.ffs1_din = pool_cache_get(ffs_dinode1_cache,
1840 		    PR_WAITOK);
1841 	else
1842 		ip->i_din.ffs2_din = pool_cache_get(ffs_dinode2_cache,
1843 		    PR_WAITOK);
1844 	ffs_load_inode(bp, ip, fs, ino);
1845 	brelse(bp, 0);
1846 
1847 	/*
1848 	 * Initialize the vnode from the inode, check for aliases.
1849 	 * Note that the underlying vnode may have changed.
1850 	 */
1851 
1852 	ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1853 
1854 	/*
1855 	 * Finish inode initialization now that aliasing has been resolved.
1856 	 */
1857 
1858 	ip->i_devvp = ump->um_devvp;
1859 	vref(ip->i_devvp);
1860 
1861 	/*
1862 	 * Ensure that uid and gid are correct. This is a temporary
1863 	 * fix until fsck has been changed to do the update.
1864 	 */
1865 
1866 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {		/* XXX */
1867 		ip->i_uid = ip->i_ffs1_ouid;			/* XXX */
1868 		ip->i_gid = ip->i_ffs1_ogid;			/* XXX */
1869 	}							/* XXX */
1870 	uvm_vnp_setsize(vp, ip->i_size);
1871 	*vpp = vp;
1872 	return (0);
1873 }
1874 
1875 /*
1876  * File handle to vnode
1877  *
1878  * Have to be really careful about stale file handles:
1879  * - check that the inode number is valid
1880  * - call ffs_vget() to get the locked inode
1881  * - check for an unallocated inode (i_mode == 0)
1882  * - check that the given client host has export rights and return
1883  *   those rights via. exflagsp and credanonp
1884  */
1885 int
1886 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1887 {
1888 	struct ufid ufh;
1889 	struct fs *fs;
1890 
1891 	if (fhp->fid_len != sizeof(struct ufid))
1892 		return EINVAL;
1893 
1894 	memcpy(&ufh, fhp, sizeof(ufh));
1895 	fs = VFSTOUFS(mp)->um_fs;
1896 	if (ufh.ufid_ino < ROOTINO ||
1897 	    ufh.ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1898 		return (ESTALE);
1899 	return (ufs_fhtovp(mp, &ufh, vpp));
1900 }
1901 
1902 /*
1903  * Vnode pointer to File handle
1904  */
1905 /* ARGSUSED */
1906 int
1907 ffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1908 {
1909 	struct inode *ip;
1910 	struct ufid ufh;
1911 
1912 	if (*fh_size < sizeof(struct ufid)) {
1913 		*fh_size = sizeof(struct ufid);
1914 		return E2BIG;
1915 	}
1916 	ip = VTOI(vp);
1917 	*fh_size = sizeof(struct ufid);
1918 	memset(&ufh, 0, sizeof(ufh));
1919 	ufh.ufid_len = sizeof(struct ufid);
1920 	ufh.ufid_ino = ip->i_number;
1921 	ufh.ufid_gen = ip->i_gen;
1922 	memcpy(fhp, &ufh, sizeof(ufh));
1923 	return (0);
1924 }
1925 
1926 void
1927 ffs_init(void)
1928 {
1929 	if (ffs_initcount++ > 0)
1930 		return;
1931 
1932 	ffs_inode_cache = pool_cache_init(sizeof(struct inode), 0, 0, 0,
1933 	    "ffsino", NULL, IPL_NONE, NULL, NULL, NULL);
1934 	ffs_dinode1_cache = pool_cache_init(sizeof(struct ufs1_dinode), 0, 0, 0,
1935 	    "ffsdino1", NULL, IPL_NONE, NULL, NULL, NULL);
1936 	ffs_dinode2_cache = pool_cache_init(sizeof(struct ufs2_dinode), 0, 0, 0,
1937 	    "ffsdino2", NULL, IPL_NONE, NULL, NULL, NULL);
1938 	ufs_init();
1939 }
1940 
1941 void
1942 ffs_reinit(void)
1943 {
1944 
1945 	ufs_reinit();
1946 }
1947 
1948 void
1949 ffs_done(void)
1950 {
1951 	if (--ffs_initcount > 0)
1952 		return;
1953 
1954 	ufs_done();
1955 	pool_cache_destroy(ffs_dinode2_cache);
1956 	pool_cache_destroy(ffs_dinode1_cache);
1957 	pool_cache_destroy(ffs_inode_cache);
1958 }
1959 
1960 /*
1961  * Write a superblock and associated information back to disk.
1962  */
1963 int
1964 ffs_sbupdate(struct ufsmount *mp, int waitfor)
1965 {
1966 	struct fs *fs = mp->um_fs;
1967 	struct buf *bp;
1968 	int error = 0;
1969 	u_int32_t saveflag;
1970 
1971 	error = ffs_getblk(mp->um_devvp,
1972 	    fs->fs_sblockloc / DEV_BSIZE, FFS_NOBLK,
1973 	    fs->fs_sbsize, false, &bp);
1974 	if (error)
1975 		return error;
1976 	saveflag = fs->fs_flags & FS_INTERNAL;
1977 	fs->fs_flags &= ~FS_INTERNAL;
1978 
1979 	memcpy(bp->b_data, fs, fs->fs_sbsize);
1980 
1981 	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1982 #ifdef FFS_EI
1983 	if (mp->um_flags & UFS_NEEDSWAP)
1984 		ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
1985 #endif
1986 	fs->fs_flags |= saveflag;
1987 
1988 	if (waitfor == MNT_WAIT)
1989 		error = bwrite(bp);
1990 	else
1991 		bawrite(bp);
1992 	return (error);
1993 }
1994 
1995 int
1996 ffs_cgupdate(struct ufsmount *mp, int waitfor)
1997 {
1998 	struct fs *fs = mp->um_fs;
1999 	struct buf *bp;
2000 	int blks;
2001 	void *space;
2002 	int i, size, error = 0, allerror = 0;
2003 
2004 	allerror = ffs_sbupdate(mp, waitfor);
2005 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
2006 	space = fs->fs_csp;
2007 	for (i = 0; i < blks; i += fs->fs_frag) {
2008 		size = fs->fs_bsize;
2009 		if (i + fs->fs_frag > blks)
2010 			size = (blks - i) * fs->fs_fsize;
2011 		error = ffs_getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
2012 		    FFS_NOBLK, size, false, &bp);
2013 		if (error)
2014 			break;
2015 #ifdef FFS_EI
2016 		if (mp->um_flags & UFS_NEEDSWAP)
2017 			ffs_csum_swap((struct csum*)space,
2018 			    (struct csum*)bp->b_data, size);
2019 		else
2020 #endif
2021 			memcpy(bp->b_data, space, (u_int)size);
2022 		space = (char *)space + size;
2023 		if (waitfor == MNT_WAIT)
2024 			error = bwrite(bp);
2025 		else
2026 			bawrite(bp);
2027 	}
2028 	if (!allerror && error)
2029 		allerror = error;
2030 	return (allerror);
2031 }
2032 
2033 int
2034 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
2035     int attrnamespace, const char *attrname)
2036 {
2037 #ifdef UFS_EXTATTR
2038 	/*
2039 	 * File-backed extended attributes are only supported on UFS1.
2040 	 * UFS2 has native extended attributes.
2041 	 */
2042 	if (VFSTOUFS(mp)->um_fstype == UFS1)
2043 		return (ufs_extattrctl(mp, cmd, vp, attrnamespace, attrname));
2044 #endif
2045 	return (vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname));
2046 }
2047 
2048 int
2049 ffs_suspendctl(struct mount *mp, int cmd)
2050 {
2051 	int error;
2052 	struct lwp *l = curlwp;
2053 
2054 	switch (cmd) {
2055 	case SUSPEND_SUSPEND:
2056 		if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
2057 			return error;
2058 		error = ffs_sync(mp, MNT_WAIT, l->l_proc->p_cred);
2059 		if (error == 0)
2060 			error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
2061 #ifdef WAPBL
2062 		if (error == 0 && mp->mnt_wapbl)
2063 			error = wapbl_flush(mp->mnt_wapbl, 1);
2064 #endif
2065 		if (error != 0) {
2066 			(void) fstrans_setstate(mp, FSTRANS_NORMAL);
2067 			return error;
2068 		}
2069 		return 0;
2070 
2071 	case SUSPEND_RESUME:
2072 		return fstrans_setstate(mp, FSTRANS_NORMAL);
2073 
2074 	default:
2075 		return EINVAL;
2076 	}
2077 }
2078 
2079 /*
2080  * Synch vnode for a mounted file system.
2081  */
2082 static int
2083 ffs_vfs_fsync(vnode_t *vp, int flags)
2084 {
2085 	int error, i, pflags;
2086 #ifdef WAPBL
2087 	struct mount *mp;
2088 #endif
2089 
2090 	KASSERT(vp->v_type == VBLK);
2091 	KASSERT(vp->v_specmountpoint != NULL);
2092 
2093 	/*
2094 	 * Flush all dirty data associated with the vnode.
2095 	 */
2096 	pflags = PGO_ALLPAGES | PGO_CLEANIT;
2097 	if ((flags & FSYNC_WAIT) != 0)
2098 		pflags |= PGO_SYNCIO;
2099 	mutex_enter(vp->v_interlock);
2100 	error = VOP_PUTPAGES(vp, 0, 0, pflags);
2101 	if (error)
2102 		return error;
2103 
2104 #ifdef WAPBL
2105 	mp = vp->v_specmountpoint;
2106 	if (mp && mp->mnt_wapbl) {
2107 		/*
2108 		 * Don't bother writing out metadata if the syncer is
2109 		 * making the request.  We will let the sync vnode
2110 		 * write it out in a single burst through a call to
2111 		 * VFS_SYNC().
2112 		 */
2113 		if ((flags & (FSYNC_DATAONLY | FSYNC_LAZY | FSYNC_NOLOG)) != 0)
2114 			return 0;
2115 
2116 		/*
2117 		 * Don't flush the log if the vnode being flushed
2118 		 * contains no dirty buffers that could be in the log.
2119 		 */
2120 		if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
2121 			error = wapbl_flush(mp->mnt_wapbl, 0);
2122 			if (error)
2123 				return error;
2124 		}
2125 
2126 		if ((flags & FSYNC_WAIT) != 0) {
2127 			mutex_enter(vp->v_interlock);
2128 			while (vp->v_numoutput)
2129 				cv_wait(&vp->v_cv, vp->v_interlock);
2130 			mutex_exit(vp->v_interlock);
2131 		}
2132 
2133 		return 0;
2134 	}
2135 #endif /* WAPBL */
2136 
2137 	error = vflushbuf(vp, (flags & FSYNC_WAIT) != 0);
2138 	if (error == 0 && (flags & FSYNC_CACHE) != 0) {
2139 		i = 1;
2140 		(void)VOP_IOCTL(vp, DIOCCACHESYNC, &i, FWRITE,
2141 		    kauth_cred_get());
2142 	}
2143 
2144 	return error;
2145 }
2146