xref: /freebsd/sys/ufs/ffs/ffs_vfsops.c (revision d184218c)
1 /*-
2  * Copyright (c) 1989, 1991, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)ffs_vfsops.c	8.31 (Berkeley) 5/20/95
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_quota.h"
36 #include "opt_ufs.h"
37 #include "opt_ffs.h"
38 #include "opt_ddb.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/namei.h>
43 #include <sys/priv.h>
44 #include <sys/proc.h>
45 #include <sys/kernel.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/bio.h>
49 #include <sys/buf.h>
50 #include <sys/conf.h>
51 #include <sys/fcntl.h>
52 #include <sys/ioccom.h>
53 #include <sys/malloc.h>
54 #include <sys/mutex.h>
55 
56 #include <security/mac/mac_framework.h>
57 
58 #include <ufs/ufs/extattr.h>
59 #include <ufs/ufs/gjournal.h>
60 #include <ufs/ufs/quota.h>
61 #include <ufs/ufs/ufsmount.h>
62 #include <ufs/ufs/inode.h>
63 #include <ufs/ufs/ufs_extern.h>
64 
65 #include <ufs/ffs/fs.h>
66 #include <ufs/ffs/ffs_extern.h>
67 
68 #include <vm/vm.h>
69 #include <vm/uma.h>
70 #include <vm/vm_page.h>
71 
72 #include <geom/geom.h>
73 #include <geom/geom_vfs.h>
74 
75 #include <ddb/ddb.h>
76 
77 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
78 
79 static int	ffs_mountfs(struct vnode *, struct mount *, struct thread *);
80 static void	ffs_oldfscompat_read(struct fs *, struct ufsmount *,
81 		    ufs2_daddr_t);
82 static void	ffs_ifree(struct ufsmount *ump, struct inode *ip);
83 static int	ffs_sync_lazy(struct mount *mp);
84 
85 static vfs_init_t ffs_init;
86 static vfs_uninit_t ffs_uninit;
87 static vfs_extattrctl_t ffs_extattrctl;
88 static vfs_cmount_t ffs_cmount;
89 static vfs_unmount_t ffs_unmount;
90 static vfs_mount_t ffs_mount;
91 static vfs_statfs_t ffs_statfs;
92 static vfs_fhtovp_t ffs_fhtovp;
93 static vfs_sync_t ffs_sync;
94 
95 static struct vfsops ufs_vfsops = {
96 	.vfs_extattrctl =	ffs_extattrctl,
97 	.vfs_fhtovp =		ffs_fhtovp,
98 	.vfs_init =		ffs_init,
99 	.vfs_mount =		ffs_mount,
100 	.vfs_cmount =		ffs_cmount,
101 	.vfs_quotactl =		ufs_quotactl,
102 	.vfs_root =		ufs_root,
103 	.vfs_statfs =		ffs_statfs,
104 	.vfs_sync =		ffs_sync,
105 	.vfs_uninit =		ffs_uninit,
106 	.vfs_unmount =		ffs_unmount,
107 	.vfs_vget =		ffs_vget,
108 	.vfs_susp_clean =	process_deferred_inactive,
109 };
110 
111 VFS_SET(ufs_vfsops, ufs, 0);
112 MODULE_VERSION(ufs, 1);
113 
114 static b_strategy_t ffs_geom_strategy;
115 static b_write_t ffs_bufwrite;
116 
117 static struct buf_ops ffs_ops = {
118 	.bop_name =	"FFS",
119 	.bop_write =	ffs_bufwrite,
120 	.bop_strategy =	ffs_geom_strategy,
121 	.bop_sync =	bufsync,
122 #ifdef NO_FFS_SNAPSHOT
123 	.bop_bdflush =	bufbdflush,
124 #else
125 	.bop_bdflush =	ffs_bdflush,
126 #endif
127 };
128 
129 /*
130  * Note that userquota and groupquota options are not currently used
131  * by UFS/FFS code and generally mount(8) does not pass those options
132  * from userland, but they can be passed by loader(8) via
133  * vfs.root.mountfrom.options.
134  */
135 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
136     "noclusterw", "noexec", "export", "force", "from", "groupquota",
137     "multilabel", "nfsv4acls", "fsckpid", "snapshot", "nosuid", "suiddir",
138     "nosymfollow", "sync", "union", "userquota", NULL };
139 
140 static int
141 ffs_mount(struct mount *mp)
142 {
143 	struct vnode *devvp;
144 	struct thread *td;
145 	struct ufsmount *ump = NULL;
146 	struct fs *fs;
147 	pid_t fsckpid = 0;
148 	int error, flags;
149 	uint64_t mntorflags;
150 	accmode_t accmode;
151 	struct nameidata ndp;
152 	char *fspec;
153 
154 	td = curthread;
155 	if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
156 		return (EINVAL);
157 	if (uma_inode == NULL) {
158 		uma_inode = uma_zcreate("FFS inode",
159 		    sizeof(struct inode), NULL, NULL, NULL, NULL,
160 		    UMA_ALIGN_PTR, 0);
161 		uma_ufs1 = uma_zcreate("FFS1 dinode",
162 		    sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
163 		    UMA_ALIGN_PTR, 0);
164 		uma_ufs2 = uma_zcreate("FFS2 dinode",
165 		    sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
166 		    UMA_ALIGN_PTR, 0);
167 	}
168 
169 	vfs_deleteopt(mp->mnt_optnew, "groupquota");
170 	vfs_deleteopt(mp->mnt_optnew, "userquota");
171 
172 	fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
173 	if (error)
174 		return (error);
175 
176 	mntorflags = 0;
177 	if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
178 		mntorflags |= MNT_ACLS;
179 
180 	if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
181 		mntorflags |= MNT_SNAPSHOT;
182 		/*
183 		 * Once we have set the MNT_SNAPSHOT flag, do not
184 		 * persist "snapshot" in the options list.
185 		 */
186 		vfs_deleteopt(mp->mnt_optnew, "snapshot");
187 		vfs_deleteopt(mp->mnt_opt, "snapshot");
188 	}
189 
190 	if (vfs_getopt(mp->mnt_optnew, "fsckpid", NULL, NULL) == 0 &&
191 	    vfs_scanopt(mp->mnt_optnew, "fsckpid", "%d", &fsckpid) == 1) {
192 		/*
193 		 * Once we have set the restricted PID, do not
194 		 * persist "fsckpid" in the options list.
195 		 */
196 		vfs_deleteopt(mp->mnt_optnew, "fsckpid");
197 		vfs_deleteopt(mp->mnt_opt, "fsckpid");
198 		if (mp->mnt_flag & MNT_UPDATE) {
199 			if (VFSTOUFS(mp)->um_fs->fs_ronly == 0 &&
200 			     vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
201 				vfs_mount_error(mp,
202 				    "Checker enable: Must be read-only");
203 				return (EINVAL);
204 			}
205 		} else if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
206 			vfs_mount_error(mp,
207 			    "Checker enable: Must be read-only");
208 			return (EINVAL);
209 		}
210 		/* Set to -1 if we are done */
211 		if (fsckpid == 0)
212 			fsckpid = -1;
213 	}
214 
215 	if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) {
216 		if (mntorflags & MNT_ACLS) {
217 			vfs_mount_error(mp,
218 			    "\"acls\" and \"nfsv4acls\" options "
219 			    "are mutually exclusive");
220 			return (EINVAL);
221 		}
222 		mntorflags |= MNT_NFS4ACLS;
223 	}
224 
225 	MNT_ILOCK(mp);
226 	mp->mnt_flag |= mntorflags;
227 	MNT_IUNLOCK(mp);
228 	/*
229 	 * If updating, check whether changing from read-only to
230 	 * read/write; if there is no device name, that's all we do.
231 	 */
232 	if (mp->mnt_flag & MNT_UPDATE) {
233 		ump = VFSTOUFS(mp);
234 		fs = ump->um_fs;
235 		devvp = ump->um_devvp;
236 		if (fsckpid == -1 && ump->um_fsckpid > 0) {
237 			if ((error = ffs_flushfiles(mp, WRITECLOSE, td)) != 0 ||
238 			    (error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0)
239 				return (error);
240 			DROP_GIANT();
241 			g_topology_lock();
242 			/*
243 			 * Return to normal read-only mode.
244 			 */
245 			error = g_access(ump->um_cp, 0, -1, 0);
246 			g_topology_unlock();
247 			PICKUP_GIANT();
248 			ump->um_fsckpid = 0;
249 		}
250 		if (fs->fs_ronly == 0 &&
251 		    vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
252 			/*
253 			 * Flush any dirty data and suspend filesystem.
254 			 */
255 			if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
256 				return (error);
257 			for (;;) {
258 				vn_finished_write(mp);
259 				if ((error = vfs_write_suspend(mp)) != 0)
260 					return (error);
261 				MNT_ILOCK(mp);
262 				if (mp->mnt_kern_flag & MNTK_SUSPENDED) {
263 					/*
264 					 * Allow the secondary writes
265 					 * to proceed.
266 					 */
267 					mp->mnt_kern_flag &= ~(MNTK_SUSPENDED |
268 					    MNTK_SUSPEND2);
269 					wakeup(&mp->mnt_flag);
270 					MNT_IUNLOCK(mp);
271 					/*
272 					 * Allow the curthread to
273 					 * ignore the suspension to
274 					 * synchronize on-disk state.
275 					 */
276 					td->td_pflags |= TDP_IGNSUSP;
277 					break;
278 				}
279 				MNT_IUNLOCK(mp);
280 				vn_start_write(NULL, &mp, V_WAIT);
281 			}
282 			/*
283 			 * Check for and optionally get rid of files open
284 			 * for writing.
285 			 */
286 			flags = WRITECLOSE;
287 			if (mp->mnt_flag & MNT_FORCE)
288 				flags |= FORCECLOSE;
289 			if (MOUNTEDSOFTDEP(mp)) {
290 				error = softdep_flushfiles(mp, flags, td);
291 			} else {
292 				error = ffs_flushfiles(mp, flags, td);
293 			}
294 			if (error) {
295 				vfs_write_resume(mp, 0);
296 				return (error);
297 			}
298 			if (fs->fs_pendingblocks != 0 ||
299 			    fs->fs_pendinginodes != 0) {
300 				printf("WARNING: %s Update error: blocks %jd "
301 				    "files %d\n", fs->fs_fsmnt,
302 				    (intmax_t)fs->fs_pendingblocks,
303 				    fs->fs_pendinginodes);
304 				fs->fs_pendingblocks = 0;
305 				fs->fs_pendinginodes = 0;
306 			}
307 			if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
308 				fs->fs_clean = 1;
309 			if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
310 				fs->fs_ronly = 0;
311 				fs->fs_clean = 0;
312 				vfs_write_resume(mp, 0);
313 				return (error);
314 			}
315 			if (MOUNTEDSOFTDEP(mp))
316 				softdep_unmount(mp);
317 			DROP_GIANT();
318 			g_topology_lock();
319 			/*
320 			 * Drop our write and exclusive access.
321 			 */
322 			g_access(ump->um_cp, 0, -1, -1);
323 			g_topology_unlock();
324 			PICKUP_GIANT();
325 			fs->fs_ronly = 1;
326 			MNT_ILOCK(mp);
327 			mp->mnt_flag |= MNT_RDONLY;
328 			MNT_IUNLOCK(mp);
329 			/*
330 			 * Allow the writers to note that filesystem
331 			 * is ro now.
332 			 */
333 			vfs_write_resume(mp, 0);
334 		}
335 		if ((mp->mnt_flag & MNT_RELOAD) &&
336 		    (error = ffs_reload(mp, td, 0)) != 0)
337 			return (error);
338 		if (fs->fs_ronly &&
339 		    !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
340 			/*
341 			 * If we are running a checker, do not allow upgrade.
342 			 */
343 			if (ump->um_fsckpid > 0) {
344 				vfs_mount_error(mp,
345 				    "Active checker, cannot upgrade to write");
346 				return (EINVAL);
347 			}
348 			/*
349 			 * If upgrade to read-write by non-root, then verify
350 			 * that user has necessary permissions on the device.
351 			 */
352 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
353 			error = VOP_ACCESS(devvp, VREAD | VWRITE,
354 			    td->td_ucred, td);
355 			if (error)
356 				error = priv_check(td, PRIV_VFS_MOUNT_PERM);
357 			if (error) {
358 				VOP_UNLOCK(devvp, 0);
359 				return (error);
360 			}
361 			VOP_UNLOCK(devvp, 0);
362 			fs->fs_flags &= ~FS_UNCLEAN;
363 			if (fs->fs_clean == 0) {
364 				fs->fs_flags |= FS_UNCLEAN;
365 				if ((mp->mnt_flag & MNT_FORCE) ||
366 				    ((fs->fs_flags &
367 				     (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
368 				     (fs->fs_flags & FS_DOSOFTDEP))) {
369 					printf("WARNING: %s was not properly "
370 					   "dismounted\n", fs->fs_fsmnt);
371 				} else {
372 					vfs_mount_error(mp,
373 					   "R/W mount of %s denied. %s.%s",
374 					   fs->fs_fsmnt,
375 					   "Filesystem is not clean - run fsck",
376 					   (fs->fs_flags & FS_SUJ) == 0 ? "" :
377 					   " Forced mount will invalidate"
378 					   " journal contents");
379 					return (EPERM);
380 				}
381 			}
382 			DROP_GIANT();
383 			g_topology_lock();
384 			/*
385 			 * Request exclusive write access.
386 			 */
387 			error = g_access(ump->um_cp, 0, 1, 1);
388 			g_topology_unlock();
389 			PICKUP_GIANT();
390 			if (error)
391 				return (error);
392 			if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
393 				return (error);
394 			fs->fs_ronly = 0;
395 			MNT_ILOCK(mp);
396 			mp->mnt_flag &= ~MNT_RDONLY;
397 			MNT_IUNLOCK(mp);
398 			fs->fs_mtime = time_second;
399 			/* check to see if we need to start softdep */
400 			if ((fs->fs_flags & FS_DOSOFTDEP) &&
401 			    (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
402 				vn_finished_write(mp);
403 				return (error);
404 			}
405 			fs->fs_clean = 0;
406 			if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
407 				vn_finished_write(mp);
408 				return (error);
409 			}
410 			if (fs->fs_snapinum[0] != 0)
411 				ffs_snapshot_mount(mp);
412 			vn_finished_write(mp);
413 		}
414 		/*
415 		 * Soft updates is incompatible with "async",
416 		 * so if we are doing softupdates stop the user
417 		 * from setting the async flag in an update.
418 		 * Softdep_mount() clears it in an initial mount
419 		 * or ro->rw remount.
420 		 */
421 		if (MOUNTEDSOFTDEP(mp)) {
422 			/* XXX: Reset too late ? */
423 			MNT_ILOCK(mp);
424 			mp->mnt_flag &= ~MNT_ASYNC;
425 			MNT_IUNLOCK(mp);
426 		}
427 		/*
428 		 * Keep MNT_ACLS flag if it is stored in superblock.
429 		 */
430 		if ((fs->fs_flags & FS_ACLS) != 0) {
431 			/* XXX: Set too late ? */
432 			MNT_ILOCK(mp);
433 			mp->mnt_flag |= MNT_ACLS;
434 			MNT_IUNLOCK(mp);
435 		}
436 
437 		if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
438 			/* XXX: Set too late ? */
439 			MNT_ILOCK(mp);
440 			mp->mnt_flag |= MNT_NFS4ACLS;
441 			MNT_IUNLOCK(mp);
442 		}
443 		/*
444 		 * If this is a request from fsck to clean up the filesystem,
445 		 * then allow the specified pid to proceed.
446 		 */
447 		if (fsckpid > 0) {
448 			if (ump->um_fsckpid != 0) {
449 				vfs_mount_error(mp,
450 				    "Active checker already running on %s",
451 				    fs->fs_fsmnt);
452 				return (EINVAL);
453 			}
454 			KASSERT(MOUNTEDSOFTDEP(mp) == 0,
455 			    ("soft updates enabled on read-only file system"));
456 			DROP_GIANT();
457 			g_topology_lock();
458 			/*
459 			 * Request write access.
460 			 */
461 			error = g_access(ump->um_cp, 0, 1, 0);
462 			g_topology_unlock();
463 			PICKUP_GIANT();
464 			if (error) {
465 				vfs_mount_error(mp,
466 				    "Checker activation failed on %s",
467 				    fs->fs_fsmnt);
468 				return (error);
469 			}
470 			ump->um_fsckpid = fsckpid;
471 			if (fs->fs_snapinum[0] != 0)
472 				ffs_snapshot_mount(mp);
473 			fs->fs_mtime = time_second;
474 			fs->fs_fmod = 1;
475 			fs->fs_clean = 0;
476 			(void) ffs_sbupdate(ump, MNT_WAIT, 0);
477 		}
478 
479 		/*
480 		 * If this is a snapshot request, take the snapshot.
481 		 */
482 		if (mp->mnt_flag & MNT_SNAPSHOT)
483 			return (ffs_snapshot(mp, fspec));
484 	}
485 
486 	/*
487 	 * Not an update, or updating the name: look up the name
488 	 * and verify that it refers to a sensible disk device.
489 	 */
490 	NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
491 	if ((error = namei(&ndp)) != 0)
492 		return (error);
493 	NDFREE(&ndp, NDF_ONLY_PNBUF);
494 	devvp = ndp.ni_vp;
495 	if (!vn_isdisk(devvp, &error)) {
496 		vput(devvp);
497 		return (error);
498 	}
499 
500 	/*
501 	 * If mount by non-root, then verify that user has necessary
502 	 * permissions on the device.
503 	 */
504 	accmode = VREAD;
505 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
506 		accmode |= VWRITE;
507 	error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
508 	if (error)
509 		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
510 	if (error) {
511 		vput(devvp);
512 		return (error);
513 	}
514 
515 	if (mp->mnt_flag & MNT_UPDATE) {
516 		/*
517 		 * Update only
518 		 *
519 		 * If it's not the same vnode, or at least the same device
520 		 * then it's not correct.
521 		 */
522 
523 		if (devvp->v_rdev != ump->um_devvp->v_rdev)
524 			error = EINVAL;	/* needs translation */
525 		vput(devvp);
526 		if (error)
527 			return (error);
528 	} else {
529 		/*
530 		 * New mount
531 		 *
532 		 * We need the name for the mount point (also used for
533 		 * "last mounted on") copied in. If an error occurs,
534 		 * the mount point is discarded by the upper level code.
535 		 * Note that vfs_mount() populates f_mntonname for us.
536 		 */
537 		if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
538 			vrele(devvp);
539 			return (error);
540 		}
541 		if (fsckpid > 0) {
542 			KASSERT(MOUNTEDSOFTDEP(mp) == 0,
543 			    ("soft updates enabled on read-only file system"));
544 			ump = VFSTOUFS(mp);
545 			fs = ump->um_fs;
546 			DROP_GIANT();
547 			g_topology_lock();
548 			/*
549 			 * Request write access.
550 			 */
551 			error = g_access(ump->um_cp, 0, 1, 0);
552 			g_topology_unlock();
553 			PICKUP_GIANT();
554 			if (error) {
555 				printf("WARNING: %s: Checker activation "
556 				    "failed\n", fs->fs_fsmnt);
557 			} else {
558 				ump->um_fsckpid = fsckpid;
559 				if (fs->fs_snapinum[0] != 0)
560 					ffs_snapshot_mount(mp);
561 				fs->fs_mtime = time_second;
562 				fs->fs_clean = 0;
563 				(void) ffs_sbupdate(ump, MNT_WAIT, 0);
564 			}
565 		}
566 	}
567 	vfs_mountedfrom(mp, fspec);
568 	return (0);
569 }
570 
571 /*
572  * Compatibility with old mount system call.
573  */
574 
575 static int
576 ffs_cmount(struct mntarg *ma, void *data, uint64_t flags)
577 {
578 	struct ufs_args args;
579 	struct export_args exp;
580 	int error;
581 
582 	if (data == NULL)
583 		return (EINVAL);
584 	error = copyin(data, &args, sizeof args);
585 	if (error)
586 		return (error);
587 	vfs_oexport_conv(&args.export, &exp);
588 
589 	ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
590 	ma = mount_arg(ma, "export", &exp, sizeof(exp));
591 	error = kernel_mount(ma, flags);
592 
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). If the 'force' flag
599  * is 0, the filesystem must 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, struct thread *td, int force)
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 ufsmount *ump;
618 	ufs2_daddr_t sblockloc;
619 	int i, blks, size, error;
620 	int32_t *lp;
621 
622 	ump = VFSTOUFS(mp);
623 
624 	MNT_ILOCK(mp);
625 	if ((mp->mnt_flag & MNT_RDONLY) == 0 && force == 0) {
626 		MNT_IUNLOCK(mp);
627 		return (EINVAL);
628 	}
629 	MNT_IUNLOCK(mp);
630 
631 	/*
632 	 * Step 1: invalidate all cached meta-data.
633 	 */
634 	devvp = VFSTOUFS(mp)->um_devvp;
635 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
636 	if (vinvalbuf(devvp, 0, 0, 0) != 0)
637 		panic("ffs_reload: dirty1");
638 	VOP_UNLOCK(devvp, 0);
639 
640 	/*
641 	 * Step 2: re-read superblock from disk.
642 	 */
643 	fs = VFSTOUFS(mp)->um_fs;
644 	if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
645 	    NOCRED, &bp)) != 0)
646 		return (error);
647 	newfs = (struct fs *)bp->b_data;
648 	if ((newfs->fs_magic != FS_UFS1_MAGIC &&
649 	     newfs->fs_magic != FS_UFS2_MAGIC) ||
650 	    newfs->fs_bsize > MAXBSIZE ||
651 	    newfs->fs_bsize < sizeof(struct fs)) {
652 			brelse(bp);
653 			return (EIO);		/* XXX needs translation */
654 	}
655 	/*
656 	 * Copy pointer fields back into superblock before copying in	XXX
657 	 * new superblock. These should really be in the ufsmount.	XXX
658 	 * Note that important parameters (eg fs_ncg) are unchanged.
659 	 */
660 	newfs->fs_csp = fs->fs_csp;
661 	newfs->fs_maxcluster = fs->fs_maxcluster;
662 	newfs->fs_contigdirs = fs->fs_contigdirs;
663 	newfs->fs_active = fs->fs_active;
664 	newfs->fs_ronly = fs->fs_ronly;
665 	sblockloc = fs->fs_sblockloc;
666 	bcopy(newfs, fs, (u_int)fs->fs_sbsize);
667 	brelse(bp);
668 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
669 	ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
670 	UFS_LOCK(ump);
671 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
672 		printf("WARNING: %s: reload pending error: blocks %jd "
673 		    "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
674 		    fs->fs_pendinginodes);
675 		fs->fs_pendingblocks = 0;
676 		fs->fs_pendinginodes = 0;
677 	}
678 	UFS_UNLOCK(ump);
679 
680 	/*
681 	 * Step 3: re-read summary information from disk.
682 	 */
683 	size = fs->fs_cssize;
684 	blks = howmany(size, fs->fs_fsize);
685 	if (fs->fs_contigsumsize > 0)
686 		size += fs->fs_ncg * sizeof(int32_t);
687 	size += fs->fs_ncg * sizeof(u_int8_t);
688 	free(fs->fs_csp, M_UFSMNT);
689 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
690 	fs->fs_csp = space;
691 	for (i = 0; i < blks; i += fs->fs_frag) {
692 		size = fs->fs_bsize;
693 		if (i + fs->fs_frag > blks)
694 			size = (blks - i) * fs->fs_fsize;
695 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
696 		    NOCRED, &bp);
697 		if (error)
698 			return (error);
699 		bcopy(bp->b_data, space, (u_int)size);
700 		space = (char *)space + size;
701 		brelse(bp);
702 	}
703 	/*
704 	 * We no longer know anything about clusters per cylinder group.
705 	 */
706 	if (fs->fs_contigsumsize > 0) {
707 		fs->fs_maxcluster = lp = space;
708 		for (i = 0; i < fs->fs_ncg; i++)
709 			*lp++ = fs->fs_contigsumsize;
710 		space = lp;
711 	}
712 	size = fs->fs_ncg * sizeof(u_int8_t);
713 	fs->fs_contigdirs = (u_int8_t *)space;
714 	bzero(fs->fs_contigdirs, size);
715 
716 loop:
717 	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
718 		/*
719 		 * Skip syncer vnode.
720 		 */
721 		if (vp->v_type == VNON) {
722 			VI_UNLOCK(vp);
723 			continue;
724 		}
725 		/*
726 		 * Step 4: invalidate all cached file data.
727 		 */
728 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
729 			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
730 			goto loop;
731 		}
732 		if (vinvalbuf(vp, 0, 0, 0))
733 			panic("ffs_reload: dirty2");
734 		/*
735 		 * Step 5: re-read inode data for all active vnodes.
736 		 */
737 		ip = VTOI(vp);
738 		error =
739 		    bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
740 		    (int)fs->fs_bsize, NOCRED, &bp);
741 		if (error) {
742 			VOP_UNLOCK(vp, 0);
743 			vrele(vp);
744 			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
745 			return (error);
746 		}
747 		ffs_load_inode(bp, ip, fs, ip->i_number);
748 		ip->i_effnlink = ip->i_nlink;
749 		brelse(bp);
750 		VOP_UNLOCK(vp, 0);
751 		vrele(vp);
752 	}
753 	return (0);
754 }
755 
756 /*
757  * Possible superblock locations ordered from most to least likely.
758  */
759 static int sblock_try[] = SBLOCKSEARCH;
760 
761 /*
762  * Common code for mount and mountroot
763  */
764 static int
765 ffs_mountfs(devvp, mp, td)
766 	struct vnode *devvp;
767 	struct mount *mp;
768 	struct thread *td;
769 {
770 	struct ufsmount *ump;
771 	struct buf *bp;
772 	struct fs *fs;
773 	struct cdev *dev;
774 	void *space;
775 	ufs2_daddr_t sblockloc;
776 	int error, i, blks, size, ronly;
777 	int32_t *lp;
778 	struct ucred *cred;
779 	struct g_consumer *cp;
780 	struct mount *nmp;
781 
782 	bp = NULL;
783 	ump = NULL;
784 	cred = td ? td->td_ucred : NOCRED;
785 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
786 
787 	dev = devvp->v_rdev;
788 	dev_ref(dev);
789 	DROP_GIANT();
790 	g_topology_lock();
791 	error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
792 	g_topology_unlock();
793 	PICKUP_GIANT();
794 	VOP_UNLOCK(devvp, 0);
795 	if (error)
796 		goto out;
797 	if (devvp->v_rdev->si_iosize_max != 0)
798 		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
799 	if (mp->mnt_iosize_max > MAXPHYS)
800 		mp->mnt_iosize_max = MAXPHYS;
801 
802 	devvp->v_bufobj.bo_ops = &ffs_ops;
803 
804 	fs = NULL;
805 	sblockloc = 0;
806 	/*
807 	 * Try reading the superblock in each of its possible locations.
808 	 */
809 	for (i = 0; sblock_try[i] != -1; i++) {
810 		if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
811 			error = EINVAL;
812 			vfs_mount_error(mp,
813 			    "Invalid sectorsize %d for superblock size %d",
814 			    cp->provider->sectorsize, SBLOCKSIZE);
815 			goto out;
816 		}
817 		if ((error = bread(devvp, btodb(sblock_try[i]), SBLOCKSIZE,
818 		    cred, &bp)) != 0)
819 			goto out;
820 		fs = (struct fs *)bp->b_data;
821 		sblockloc = sblock_try[i];
822 		if ((fs->fs_magic == FS_UFS1_MAGIC ||
823 		     (fs->fs_magic == FS_UFS2_MAGIC &&
824 		      (fs->fs_sblockloc == sblockloc ||
825 		       (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) &&
826 		    fs->fs_bsize <= MAXBSIZE &&
827 		    fs->fs_bsize >= sizeof(struct fs))
828 			break;
829 		brelse(bp);
830 		bp = NULL;
831 	}
832 	if (sblock_try[i] == -1) {
833 		error = EINVAL;		/* XXX needs translation */
834 		goto out;
835 	}
836 	fs->fs_fmod = 0;
837 	fs->fs_flags &= ~FS_INDEXDIRS;	/* no support for directory indicies */
838 	fs->fs_flags &= ~FS_UNCLEAN;
839 	if (fs->fs_clean == 0) {
840 		fs->fs_flags |= FS_UNCLEAN;
841 		if (ronly || (mp->mnt_flag & MNT_FORCE) ||
842 		    ((fs->fs_flags & (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
843 		     (fs->fs_flags & FS_DOSOFTDEP))) {
844 			printf("WARNING: %s was not properly dismounted\n",
845 			    fs->fs_fsmnt);
846 		} else {
847 			vfs_mount_error(mp, "R/W mount of %s denied. %s%s",
848 			    fs->fs_fsmnt, "Filesystem is not clean - run fsck.",
849 			    (fs->fs_flags & FS_SUJ) == 0 ? "" :
850 			    " Forced mount will invalidate journal contents");
851 			error = EPERM;
852 			goto out;
853 		}
854 		if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
855 		    (mp->mnt_flag & MNT_FORCE)) {
856 			printf("WARNING: %s: lost blocks %jd files %d\n",
857 			    fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
858 			    fs->fs_pendinginodes);
859 			fs->fs_pendingblocks = 0;
860 			fs->fs_pendinginodes = 0;
861 		}
862 	}
863 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
864 		printf("WARNING: %s: mount pending error: blocks %jd "
865 		    "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
866 		    fs->fs_pendinginodes);
867 		fs->fs_pendingblocks = 0;
868 		fs->fs_pendinginodes = 0;
869 	}
870 	if ((fs->fs_flags & FS_GJOURNAL) != 0) {
871 #ifdef UFS_GJOURNAL
872 		/*
873 		 * Get journal provider name.
874 		 */
875 		size = 1024;
876 		mp->mnt_gjprovider = malloc(size, M_UFSMNT, M_WAITOK);
877 		if (g_io_getattr("GJOURNAL::provider", cp, &size,
878 		    mp->mnt_gjprovider) == 0) {
879 			mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, size,
880 			    M_UFSMNT, M_WAITOK);
881 			MNT_ILOCK(mp);
882 			mp->mnt_flag |= MNT_GJOURNAL;
883 			MNT_IUNLOCK(mp);
884 		} else {
885 			printf("WARNING: %s: GJOURNAL flag on fs "
886 			    "but no gjournal provider below\n",
887 			    mp->mnt_stat.f_mntonname);
888 			free(mp->mnt_gjprovider, M_UFSMNT);
889 			mp->mnt_gjprovider = NULL;
890 		}
891 #else
892 		printf("WARNING: %s: GJOURNAL flag on fs but no "
893 		    "UFS_GJOURNAL support\n", mp->mnt_stat.f_mntonname);
894 #endif
895 	} else {
896 		mp->mnt_gjprovider = NULL;
897 	}
898 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
899 	ump->um_cp = cp;
900 	ump->um_bo = &devvp->v_bufobj;
901 	ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK);
902 	if (fs->fs_magic == FS_UFS1_MAGIC) {
903 		ump->um_fstype = UFS1;
904 		ump->um_balloc = ffs_balloc_ufs1;
905 	} else {
906 		ump->um_fstype = UFS2;
907 		ump->um_balloc = ffs_balloc_ufs2;
908 	}
909 	ump->um_blkatoff = ffs_blkatoff;
910 	ump->um_truncate = ffs_truncate;
911 	ump->um_update = ffs_update;
912 	ump->um_valloc = ffs_valloc;
913 	ump->um_vfree = ffs_vfree;
914 	ump->um_ifree = ffs_ifree;
915 	ump->um_rdonly = ffs_rdonly;
916 	ump->um_snapgone = ffs_snapgone;
917 	mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
918 	bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
919 	if (fs->fs_sbsize < SBLOCKSIZE)
920 		bp->b_flags |= B_INVAL | B_NOCACHE;
921 	brelse(bp);
922 	bp = NULL;
923 	fs = ump->um_fs;
924 	ffs_oldfscompat_read(fs, ump, sblockloc);
925 	fs->fs_ronly = ronly;
926 	size = fs->fs_cssize;
927 	blks = howmany(size, fs->fs_fsize);
928 	if (fs->fs_contigsumsize > 0)
929 		size += fs->fs_ncg * sizeof(int32_t);
930 	size += fs->fs_ncg * sizeof(u_int8_t);
931 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
932 	fs->fs_csp = space;
933 	for (i = 0; i < blks; i += fs->fs_frag) {
934 		size = fs->fs_bsize;
935 		if (i + fs->fs_frag > blks)
936 			size = (blks - i) * fs->fs_fsize;
937 		if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
938 		    cred, &bp)) != 0) {
939 			free(fs->fs_csp, M_UFSMNT);
940 			goto out;
941 		}
942 		bcopy(bp->b_data, space, (u_int)size);
943 		space = (char *)space + size;
944 		brelse(bp);
945 		bp = NULL;
946 	}
947 	if (fs->fs_contigsumsize > 0) {
948 		fs->fs_maxcluster = lp = space;
949 		for (i = 0; i < fs->fs_ncg; i++)
950 			*lp++ = fs->fs_contigsumsize;
951 		space = lp;
952 	}
953 	size = fs->fs_ncg * sizeof(u_int8_t);
954 	fs->fs_contigdirs = (u_int8_t *)space;
955 	bzero(fs->fs_contigdirs, size);
956 	fs->fs_active = NULL;
957 	mp->mnt_data = ump;
958 	mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
959 	mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
960 	nmp = NULL;
961 	if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
962 	    (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
963 		if (nmp)
964 			vfs_rel(nmp);
965 		vfs_getnewfsid(mp);
966 	}
967 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
968 	MNT_ILOCK(mp);
969 	mp->mnt_flag |= MNT_LOCAL;
970 	MNT_IUNLOCK(mp);
971 	if ((fs->fs_flags & FS_MULTILABEL) != 0) {
972 #ifdef MAC
973 		MNT_ILOCK(mp);
974 		mp->mnt_flag |= MNT_MULTILABEL;
975 		MNT_IUNLOCK(mp);
976 #else
977 		printf("WARNING: %s: multilabel flag on fs but "
978 		    "no MAC support\n", mp->mnt_stat.f_mntonname);
979 #endif
980 	}
981 	if ((fs->fs_flags & FS_ACLS) != 0) {
982 #ifdef UFS_ACL
983 		MNT_ILOCK(mp);
984 
985 		if (mp->mnt_flag & MNT_NFS4ACLS)
986 			printf("WARNING: %s: ACLs flag on fs conflicts with "
987 			    "\"nfsv4acls\" mount option; option ignored\n",
988 			    mp->mnt_stat.f_mntonname);
989 		mp->mnt_flag &= ~MNT_NFS4ACLS;
990 		mp->mnt_flag |= MNT_ACLS;
991 
992 		MNT_IUNLOCK(mp);
993 #else
994 		printf("WARNING: %s: ACLs flag on fs but no ACLs support\n",
995 		    mp->mnt_stat.f_mntonname);
996 #endif
997 	}
998 	if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
999 #ifdef UFS_ACL
1000 		MNT_ILOCK(mp);
1001 
1002 		if (mp->mnt_flag & MNT_ACLS)
1003 			printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts "
1004 			    "with \"acls\" mount option; option ignored\n",
1005 			    mp->mnt_stat.f_mntonname);
1006 		mp->mnt_flag &= ~MNT_ACLS;
1007 		mp->mnt_flag |= MNT_NFS4ACLS;
1008 
1009 		MNT_IUNLOCK(mp);
1010 #else
1011 		printf("WARNING: %s: NFSv4 ACLs flag on fs but no "
1012 		    "ACLs support\n", mp->mnt_stat.f_mntonname);
1013 #endif
1014 	}
1015 	if ((fs->fs_flags & FS_TRIM) != 0) {
1016 		size = sizeof(int);
1017 		if (g_io_getattr("GEOM::candelete", cp, &size,
1018 		    &ump->um_candelete) == 0) {
1019 			if (!ump->um_candelete)
1020 				printf("WARNING: %s: TRIM flag on fs but disk "
1021 				    "does not support TRIM\n",
1022 				    mp->mnt_stat.f_mntonname);
1023 		} else {
1024 			printf("WARNING: %s: TRIM flag on fs but disk does "
1025 			    "not confirm that it supports TRIM\n",
1026 			    mp->mnt_stat.f_mntonname);
1027 			ump->um_candelete = 0;
1028 		}
1029 	}
1030 
1031 	ump->um_mountp = mp;
1032 	ump->um_dev = dev;
1033 	ump->um_devvp = devvp;
1034 	ump->um_nindir = fs->fs_nindir;
1035 	ump->um_bptrtodb = fs->fs_fsbtodb;
1036 	ump->um_seqinc = fs->fs_frag;
1037 	for (i = 0; i < MAXQUOTAS; i++)
1038 		ump->um_quotas[i] = NULLVP;
1039 #ifdef UFS_EXTATTR
1040 	ufs_extattr_uepm_init(&ump->um_extattr);
1041 #endif
1042 	/*
1043 	 * Set FS local "last mounted on" information (NULL pad)
1044 	 */
1045 	bzero(fs->fs_fsmnt, MAXMNTLEN);
1046 	strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
1047 	mp->mnt_stat.f_iosize = fs->fs_bsize;
1048 
1049 	if (mp->mnt_flag & MNT_ROOTFS) {
1050 		/*
1051 		 * Root mount; update timestamp in mount structure.
1052 		 * this will be used by the common root mount code
1053 		 * to update the system clock.
1054 		 */
1055 		mp->mnt_time = fs->fs_time;
1056 	}
1057 
1058 	if (ronly == 0) {
1059 		fs->fs_mtime = time_second;
1060 		if ((fs->fs_flags & FS_DOSOFTDEP) &&
1061 		    (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
1062 			free(fs->fs_csp, M_UFSMNT);
1063 			ffs_flushfiles(mp, FORCECLOSE, td);
1064 			goto out;
1065 		}
1066 		if (devvp->v_type == VCHR && devvp->v_rdev != NULL)
1067 			devvp->v_rdev->si_mountpt = mp;
1068 		if (fs->fs_snapinum[0] != 0)
1069 			ffs_snapshot_mount(mp);
1070 		fs->fs_fmod = 1;
1071 		fs->fs_clean = 0;
1072 		(void) ffs_sbupdate(ump, MNT_WAIT, 0);
1073 	}
1074 	/*
1075 	 * Initialize filesystem stat information in mount struct.
1076 	 */
1077 	MNT_ILOCK(mp);
1078 	mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
1079 	    MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS;
1080 	MNT_IUNLOCK(mp);
1081 #ifdef UFS_EXTATTR
1082 #ifdef UFS_EXTATTR_AUTOSTART
1083 	/*
1084 	 *
1085 	 * Auto-starting does the following:
1086 	 *	- check for /.attribute in the fs, and extattr_start if so
1087 	 *	- for each file in .attribute, enable that file with
1088 	 * 	  an attribute of the same name.
1089 	 * Not clear how to report errors -- probably eat them.
1090 	 * This would all happen while the filesystem was busy/not
1091 	 * available, so would effectively be "atomic".
1092 	 */
1093 	(void) ufs_extattr_autostart(mp, td);
1094 #endif /* !UFS_EXTATTR_AUTOSTART */
1095 #endif /* !UFS_EXTATTR */
1096 	return (0);
1097 out:
1098 	if (bp)
1099 		brelse(bp);
1100 	if (cp != NULL) {
1101 		DROP_GIANT();
1102 		g_topology_lock();
1103 		g_vfs_close(cp);
1104 		g_topology_unlock();
1105 		PICKUP_GIANT();
1106 	}
1107 	if (ump) {
1108 		mtx_destroy(UFS_MTX(ump));
1109 		if (mp->mnt_gjprovider != NULL) {
1110 			free(mp->mnt_gjprovider, M_UFSMNT);
1111 			mp->mnt_gjprovider = NULL;
1112 		}
1113 		free(ump->um_fs, M_UFSMNT);
1114 		free(ump, M_UFSMNT);
1115 		mp->mnt_data = NULL;
1116 	}
1117 	dev_rel(dev);
1118 	return (error);
1119 }
1120 
1121 #include <sys/sysctl.h>
1122 static int bigcgs = 0;
1123 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1124 
1125 /*
1126  * Sanity checks for loading old filesystem superblocks.
1127  * See ffs_oldfscompat_write below for unwound actions.
1128  *
1129  * XXX - Parts get retired eventually.
1130  * Unfortunately new bits get added.
1131  */
1132 static void
1133 ffs_oldfscompat_read(fs, ump, sblockloc)
1134 	struct fs *fs;
1135 	struct ufsmount *ump;
1136 	ufs2_daddr_t sblockloc;
1137 {
1138 	off_t maxfilesize;
1139 
1140 	/*
1141 	 * If not yet done, update fs_flags location and value of fs_sblockloc.
1142 	 */
1143 	if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1144 		fs->fs_flags = fs->fs_old_flags;
1145 		fs->fs_old_flags |= FS_FLAGS_UPDATED;
1146 		fs->fs_sblockloc = sblockloc;
1147 	}
1148 	/*
1149 	 * If not yet done, update UFS1 superblock with new wider fields.
1150 	 */
1151 	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1152 		fs->fs_maxbsize = fs->fs_bsize;
1153 		fs->fs_time = fs->fs_old_time;
1154 		fs->fs_size = fs->fs_old_size;
1155 		fs->fs_dsize = fs->fs_old_dsize;
1156 		fs->fs_csaddr = fs->fs_old_csaddr;
1157 		fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1158 		fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1159 		fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1160 		fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1161 	}
1162 	if (fs->fs_magic == FS_UFS1_MAGIC &&
1163 	    fs->fs_old_inodefmt < FS_44INODEFMT) {
1164 		fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1165 		fs->fs_qbmask = ~fs->fs_bmask;
1166 		fs->fs_qfmask = ~fs->fs_fmask;
1167 	}
1168 	if (fs->fs_magic == FS_UFS1_MAGIC) {
1169 		ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1170 		maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1171 		if (fs->fs_maxfilesize > maxfilesize)
1172 			fs->fs_maxfilesize = maxfilesize;
1173 	}
1174 	/* Compatibility for old filesystems */
1175 	if (fs->fs_avgfilesize <= 0)
1176 		fs->fs_avgfilesize = AVFILESIZ;
1177 	if (fs->fs_avgfpdir <= 0)
1178 		fs->fs_avgfpdir = AFPDIR;
1179 	if (bigcgs) {
1180 		fs->fs_save_cgsize = fs->fs_cgsize;
1181 		fs->fs_cgsize = fs->fs_bsize;
1182 	}
1183 }
1184 
1185 /*
1186  * Unwinding superblock updates for old filesystems.
1187  * See ffs_oldfscompat_read above for details.
1188  *
1189  * XXX - Parts get retired eventually.
1190  * Unfortunately new bits get added.
1191  */
1192 void
1193 ffs_oldfscompat_write(fs, ump)
1194 	struct fs *fs;
1195 	struct ufsmount *ump;
1196 {
1197 
1198 	/*
1199 	 * Copy back UFS2 updated fields that UFS1 inspects.
1200 	 */
1201 	if (fs->fs_magic == FS_UFS1_MAGIC) {
1202 		fs->fs_old_time = fs->fs_time;
1203 		fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1204 		fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1205 		fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1206 		fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1207 		fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1208 	}
1209 	if (bigcgs) {
1210 		fs->fs_cgsize = fs->fs_save_cgsize;
1211 		fs->fs_save_cgsize = 0;
1212 	}
1213 }
1214 
1215 /*
1216  * unmount system call
1217  */
1218 static int
1219 ffs_unmount(mp, mntflags)
1220 	struct mount *mp;
1221 	int mntflags;
1222 {
1223 	struct thread *td;
1224 	struct ufsmount *ump = VFSTOUFS(mp);
1225 	struct fs *fs;
1226 	int error, flags, susp;
1227 #ifdef UFS_EXTATTR
1228 	int e_restart;
1229 #endif
1230 
1231 	flags = 0;
1232 	td = curthread;
1233 	fs = ump->um_fs;
1234 	susp = 0;
1235 	if (mntflags & MNT_FORCE) {
1236 		flags |= FORCECLOSE;
1237 		susp = fs->fs_ronly != 0;
1238 	}
1239 #ifdef UFS_EXTATTR
1240 	if ((error = ufs_extattr_stop(mp, td))) {
1241 		if (error != EOPNOTSUPP)
1242 			printf("WARNING: unmount %s: ufs_extattr_stop "
1243 			    "returned errno %d\n", mp->mnt_stat.f_mntonname,
1244 			    error);
1245 		e_restart = 0;
1246 	} else {
1247 		ufs_extattr_uepm_destroy(&ump->um_extattr);
1248 		e_restart = 1;
1249 	}
1250 #endif
1251 	if (susp) {
1252 		/*
1253 		 * dounmount already called vn_start_write().
1254 		 */
1255 		for (;;) {
1256 			vn_finished_write(mp);
1257 			if ((error = vfs_write_suspend(mp)) != 0)
1258 				return (error);
1259 			MNT_ILOCK(mp);
1260 			if (mp->mnt_kern_flag & MNTK_SUSPENDED) {
1261 				mp->mnt_kern_flag &= ~(MNTK_SUSPENDED |
1262 				    MNTK_SUSPEND2);
1263 				wakeup(&mp->mnt_flag);
1264 				MNT_IUNLOCK(mp);
1265 				td->td_pflags |= TDP_IGNSUSP;
1266 				break;
1267 			}
1268 			MNT_IUNLOCK(mp);
1269 			vn_start_write(NULL, &mp, V_WAIT);
1270 		}
1271 	}
1272 	if (MOUNTEDSOFTDEP(mp))
1273 		error = softdep_flushfiles(mp, flags, td);
1274 	else
1275 		error = ffs_flushfiles(mp, flags, td);
1276 	if (error != 0 && error != ENXIO)
1277 		goto fail;
1278 
1279 	UFS_LOCK(ump);
1280 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1281 		printf("WARNING: unmount %s: pending error: blocks %jd "
1282 		    "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1283 		    fs->fs_pendinginodes);
1284 		fs->fs_pendingblocks = 0;
1285 		fs->fs_pendinginodes = 0;
1286 	}
1287 	UFS_UNLOCK(ump);
1288 	softdep_unmount(mp);
1289 	if (fs->fs_ronly == 0 || ump->um_fsckpid > 0) {
1290 		fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1291 		error = ffs_sbupdate(ump, MNT_WAIT, 0);
1292 		if (error && error != ENXIO) {
1293 			fs->fs_clean = 0;
1294 			goto fail;
1295 		}
1296 	}
1297 	if (susp)
1298 		vfs_write_resume(mp, VR_START_WRITE);
1299 	DROP_GIANT();
1300 	g_topology_lock();
1301 	if (ump->um_fsckpid > 0) {
1302 		/*
1303 		 * Return to normal read-only mode.
1304 		 */
1305 		error = g_access(ump->um_cp, 0, -1, 0);
1306 		ump->um_fsckpid = 0;
1307 	}
1308 	g_vfs_close(ump->um_cp);
1309 	g_topology_unlock();
1310 	PICKUP_GIANT();
1311 	if (ump->um_devvp->v_type == VCHR && ump->um_devvp->v_rdev != NULL)
1312 		ump->um_devvp->v_rdev->si_mountpt = NULL;
1313 	vrele(ump->um_devvp);
1314 	dev_rel(ump->um_dev);
1315 	mtx_destroy(UFS_MTX(ump));
1316 	if (mp->mnt_gjprovider != NULL) {
1317 		free(mp->mnt_gjprovider, M_UFSMNT);
1318 		mp->mnt_gjprovider = NULL;
1319 	}
1320 	free(fs->fs_csp, M_UFSMNT);
1321 	free(fs, M_UFSMNT);
1322 	free(ump, M_UFSMNT);
1323 	mp->mnt_data = NULL;
1324 	MNT_ILOCK(mp);
1325 	mp->mnt_flag &= ~MNT_LOCAL;
1326 	MNT_IUNLOCK(mp);
1327 	return (error);
1328 
1329 fail:
1330 	if (susp)
1331 		vfs_write_resume(mp, VR_START_WRITE);
1332 #ifdef UFS_EXTATTR
1333 	if (e_restart) {
1334 		ufs_extattr_uepm_init(&ump->um_extattr);
1335 #ifdef UFS_EXTATTR_AUTOSTART
1336 		(void) ufs_extattr_autostart(mp, td);
1337 #endif
1338 	}
1339 #endif
1340 
1341 	return (error);
1342 }
1343 
1344 /*
1345  * Flush out all the files in a filesystem.
1346  */
1347 int
1348 ffs_flushfiles(mp, flags, td)
1349 	struct mount *mp;
1350 	int flags;
1351 	struct thread *td;
1352 {
1353 	struct ufsmount *ump;
1354 	int qerror, error;
1355 
1356 	ump = VFSTOUFS(mp);
1357 	qerror = 0;
1358 #ifdef QUOTA
1359 	if (mp->mnt_flag & MNT_QUOTA) {
1360 		int i;
1361 		error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1362 		if (error)
1363 			return (error);
1364 		for (i = 0; i < MAXQUOTAS; i++) {
1365 			error = quotaoff(td, mp, i);
1366 			if (error != 0) {
1367 				if ((flags & EARLYFLUSH) == 0)
1368 					return (error);
1369 				else
1370 					qerror = error;
1371 			}
1372 		}
1373 
1374 		/*
1375 		 * Here we fall through to vflush again to ensure that
1376 		 * we have gotten rid of all the system vnodes, unless
1377 		 * quotas must not be closed.
1378 		 */
1379 	}
1380 #endif
1381 	ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1382 	if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1383 		if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1384 			return (error);
1385 		ffs_snapshot_unmount(mp);
1386 		flags |= FORCECLOSE;
1387 		/*
1388 		 * Here we fall through to vflush again to ensure
1389 		 * that we have gotten rid of all the system vnodes.
1390 		 */
1391 	}
1392 
1393 	/*
1394 	 * Do not close system files if quotas were not closed, to be
1395 	 * able to sync the remaining dquots.  The freeblks softupdate
1396 	 * workitems might hold a reference on a dquot, preventing
1397 	 * quotaoff() from completing.  Next round of
1398 	 * softdep_flushworklist() iteration should process the
1399 	 * blockers, allowing the next run of quotaoff() to finally
1400 	 * flush held dquots.
1401 	 *
1402 	 * Otherwise, flush all the files.
1403 	 */
1404 	if (qerror == 0 && (error = vflush(mp, 0, flags, td)) != 0)
1405 		return (error);
1406 
1407 	/*
1408 	 * Flush filesystem metadata.
1409 	 */
1410 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1411 	error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1412 	VOP_UNLOCK(ump->um_devvp, 0);
1413 	return (error);
1414 }
1415 
1416 /*
1417  * Get filesystem statistics.
1418  */
1419 static int
1420 ffs_statfs(mp, sbp)
1421 	struct mount *mp;
1422 	struct statfs *sbp;
1423 {
1424 	struct ufsmount *ump;
1425 	struct fs *fs;
1426 
1427 	ump = VFSTOUFS(mp);
1428 	fs = ump->um_fs;
1429 	if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1430 		panic("ffs_statfs");
1431 	sbp->f_version = STATFS_VERSION;
1432 	sbp->f_bsize = fs->fs_fsize;
1433 	sbp->f_iosize = fs->fs_bsize;
1434 	sbp->f_blocks = fs->fs_dsize;
1435 	UFS_LOCK(ump);
1436 	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1437 	    fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1438 	sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1439 	    dbtofsb(fs, fs->fs_pendingblocks);
1440 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
1441 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1442 	UFS_UNLOCK(ump);
1443 	sbp->f_namemax = NAME_MAX;
1444 	return (0);
1445 }
1446 
1447 /*
1448  * For a lazy sync, we only care about access times, quotas and the
1449  * superblock.  Other filesystem changes are already converted to
1450  * cylinder group blocks or inode blocks updates and are written to
1451  * disk by syncer.
1452  */
1453 static int
1454 ffs_sync_lazy(mp)
1455      struct mount *mp;
1456 {
1457 	struct vnode *mvp, *vp;
1458 	struct inode *ip;
1459 	struct thread *td;
1460 	int allerror, error;
1461 
1462 	allerror = 0;
1463 	td = curthread;
1464 	if ((mp->mnt_flag & MNT_NOATIME) != 0)
1465 		goto qupdate;
1466 	MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
1467 		if (vp->v_type == VNON) {
1468 			VI_UNLOCK(vp);
1469 			continue;
1470 		}
1471 		ip = VTOI(vp);
1472 
1473 		/*
1474 		 * The IN_ACCESS flag is converted to IN_MODIFIED by
1475 		 * ufs_close() and ufs_getattr() by the calls to
1476 		 * ufs_itimes_locked(), without subsequent UFS_UPDATE().
1477 		 * Test also all the other timestamp flags too, to pick up
1478 		 * any other cases that could be missed.
1479 		 */
1480 		if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
1481 		    IN_UPDATE)) == 0) {
1482 			VI_UNLOCK(vp);
1483 			continue;
1484 		}
1485 		if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
1486 		    td)) != 0)
1487 			continue;
1488 		error = ffs_update(vp, 0);
1489 		if (error != 0)
1490 			allerror = error;
1491 		vput(vp);
1492 	}
1493 
1494 qupdate:
1495 #ifdef QUOTA
1496 	qsync(mp);
1497 #endif
1498 
1499 	if (VFSTOUFS(mp)->um_fs->fs_fmod != 0 &&
1500 	    (error = ffs_sbupdate(VFSTOUFS(mp), MNT_LAZY, 0)) != 0)
1501 		allerror = error;
1502 	return (allerror);
1503 }
1504 
1505 /*
1506  * Go through the disk queues to initiate sandbagged IO;
1507  * go through the inodes to write those that have been modified;
1508  * initiate the writing of the super block if it has been modified.
1509  *
1510  * Note: we are always called with the filesystem marked busy using
1511  * vfs_busy().
1512  */
1513 static int
1514 ffs_sync(mp, waitfor)
1515 	struct mount *mp;
1516 	int waitfor;
1517 {
1518 	struct vnode *mvp, *vp, *devvp;
1519 	struct thread *td;
1520 	struct inode *ip;
1521 	struct ufsmount *ump = VFSTOUFS(mp);
1522 	struct fs *fs;
1523 	int error, count, wait, lockreq, allerror = 0;
1524 	int suspend;
1525 	int suspended;
1526 	int secondary_writes;
1527 	int secondary_accwrites;
1528 	int softdep_deps;
1529 	int softdep_accdeps;
1530 	struct bufobj *bo;
1531 
1532 	wait = 0;
1533 	suspend = 0;
1534 	suspended = 0;
1535 	td = curthread;
1536 	fs = ump->um_fs;
1537 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0 && ump->um_fsckpid == 0)
1538 		panic("%s: ffs_sync: modification on read-only filesystem",
1539 		    fs->fs_fsmnt);
1540 	if (waitfor == MNT_LAZY)
1541 		return (ffs_sync_lazy(mp));
1542 
1543 	/*
1544 	 * Write back each (modified) inode.
1545 	 */
1546 	lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1547 	if (waitfor == MNT_SUSPEND) {
1548 		suspend = 1;
1549 		waitfor = MNT_WAIT;
1550 	}
1551 	if (waitfor == MNT_WAIT) {
1552 		wait = 1;
1553 		lockreq = LK_EXCLUSIVE;
1554 	}
1555 	lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1556 loop:
1557 	/* Grab snapshot of secondary write counts */
1558 	MNT_ILOCK(mp);
1559 	secondary_writes = mp->mnt_secondary_writes;
1560 	secondary_accwrites = mp->mnt_secondary_accwrites;
1561 	MNT_IUNLOCK(mp);
1562 
1563 	/* Grab snapshot of softdep dependency counts */
1564 	softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1565 
1566 	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1567 		/*
1568 		 * Depend on the vnode interlock to keep things stable enough
1569 		 * for a quick test.  Since there might be hundreds of
1570 		 * thousands of vnodes, we cannot afford even a subroutine
1571 		 * call unless there's a good chance that we have work to do.
1572 		 */
1573 		if (vp->v_type == VNON) {
1574 			VI_UNLOCK(vp);
1575 			continue;
1576 		}
1577 		ip = VTOI(vp);
1578 		if ((ip->i_flag &
1579 		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1580 		    vp->v_bufobj.bo_dirty.bv_cnt == 0) {
1581 			VI_UNLOCK(vp);
1582 			continue;
1583 		}
1584 		if ((error = vget(vp, lockreq, td)) != 0) {
1585 			if (error == ENOENT || error == ENOLCK) {
1586 				MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1587 				goto loop;
1588 			}
1589 			continue;
1590 		}
1591 		if ((error = ffs_syncvnode(vp, waitfor, 0)) != 0)
1592 			allerror = error;
1593 		vput(vp);
1594 	}
1595 	/*
1596 	 * Force stale filesystem control information to be flushed.
1597 	 */
1598 	if (waitfor == MNT_WAIT) {
1599 		if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1600 			allerror = error;
1601 		/* Flushed work items may create new vnodes to clean */
1602 		if (allerror == 0 && count)
1603 			goto loop;
1604 	}
1605 #ifdef QUOTA
1606 	qsync(mp);
1607 #endif
1608 
1609 	devvp = ump->um_devvp;
1610 	bo = &devvp->v_bufobj;
1611 	BO_LOCK(bo);
1612 	if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) {
1613 		BO_UNLOCK(bo);
1614 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1615 		if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0)
1616 			allerror = error;
1617 		VOP_UNLOCK(devvp, 0);
1618 		if (allerror == 0 && waitfor == MNT_WAIT)
1619 			goto loop;
1620 	} else if (suspend != 0) {
1621 		if (softdep_check_suspend(mp,
1622 					  devvp,
1623 					  softdep_deps,
1624 					  softdep_accdeps,
1625 					  secondary_writes,
1626 					  secondary_accwrites) != 0) {
1627 			MNT_IUNLOCK(mp);
1628 			goto loop;	/* More work needed */
1629 		}
1630 		mtx_assert(MNT_MTX(mp), MA_OWNED);
1631 		mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1632 		MNT_IUNLOCK(mp);
1633 		suspended = 1;
1634 	} else
1635 		BO_UNLOCK(bo);
1636 	/*
1637 	 * Write back modified superblock.
1638 	 */
1639 	if (fs->fs_fmod != 0 &&
1640 	    (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1641 		allerror = error;
1642 	return (allerror);
1643 }
1644 
1645 int
1646 ffs_vget(mp, ino, flags, vpp)
1647 	struct mount *mp;
1648 	ino_t ino;
1649 	int flags;
1650 	struct vnode **vpp;
1651 {
1652 	return (ffs_vgetf(mp, ino, flags, vpp, 0));
1653 }
1654 
1655 int
1656 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1657 	struct mount *mp;
1658 	ino_t ino;
1659 	int flags;
1660 	struct vnode **vpp;
1661 	int ffs_flags;
1662 {
1663 	struct fs *fs;
1664 	struct inode *ip;
1665 	struct ufsmount *ump;
1666 	struct buf *bp;
1667 	struct vnode *vp;
1668 	struct cdev *dev;
1669 	int error;
1670 
1671 	error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1672 	if (error || *vpp != NULL)
1673 		return (error);
1674 
1675 	/*
1676 	 * We must promote to an exclusive lock for vnode creation.  This
1677 	 * can happen if lookup is passed LOCKSHARED.
1678  	 */
1679 	if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1680 		flags &= ~LK_TYPE_MASK;
1681 		flags |= LK_EXCLUSIVE;
1682 	}
1683 
1684 	/*
1685 	 * We do not lock vnode creation as it is believed to be too
1686 	 * expensive for such rare case as simultaneous creation of vnode
1687 	 * for same ino by different processes. We just allow them to race
1688 	 * and check later to decide who wins. Let the race begin!
1689 	 */
1690 
1691 	ump = VFSTOUFS(mp);
1692 	dev = ump->um_dev;
1693 	fs = ump->um_fs;
1694 	ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1695 
1696 	/* Allocate a new vnode/inode. */
1697 	if (fs->fs_magic == FS_UFS1_MAGIC)
1698 		error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp);
1699 	else
1700 		error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp);
1701 	if (error) {
1702 		*vpp = NULL;
1703 		uma_zfree(uma_inode, ip);
1704 		return (error);
1705 	}
1706 	/*
1707 	 * FFS supports recursive locking.
1708 	 */
1709 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1710 	VN_LOCK_AREC(vp);
1711 	vp->v_data = ip;
1712 	vp->v_bufobj.bo_bsize = fs->fs_bsize;
1713 	ip->i_vnode = vp;
1714 	ip->i_ump = ump;
1715 	ip->i_fs = fs;
1716 	ip->i_dev = dev;
1717 	ip->i_number = ino;
1718 	ip->i_ea_refs = 0;
1719 #ifdef QUOTA
1720 	{
1721 		int i;
1722 		for (i = 0; i < MAXQUOTAS; i++)
1723 			ip->i_dquot[i] = NODQUOT;
1724 	}
1725 #endif
1726 
1727 	if (ffs_flags & FFSV_FORCEINSMQ)
1728 		vp->v_vflag |= VV_FORCEINSMQ;
1729 	error = insmntque(vp, mp);
1730 	if (error != 0) {
1731 		uma_zfree(uma_inode, ip);
1732 		*vpp = NULL;
1733 		return (error);
1734 	}
1735 	vp->v_vflag &= ~VV_FORCEINSMQ;
1736 	error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1737 	if (error || *vpp != NULL)
1738 		return (error);
1739 
1740 	/* Read in the disk contents for the inode, copy into the inode. */
1741 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1742 	    (int)fs->fs_bsize, NOCRED, &bp);
1743 	if (error) {
1744 		/*
1745 		 * The inode does not contain anything useful, so it would
1746 		 * be misleading to leave it on its hash chain. With mode
1747 		 * still zero, it will be unlinked and returned to the free
1748 		 * list by vput().
1749 		 */
1750 		brelse(bp);
1751 		vput(vp);
1752 		*vpp = NULL;
1753 		return (error);
1754 	}
1755 	if (ip->i_ump->um_fstype == UFS1)
1756 		ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1757 	else
1758 		ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1759 	ffs_load_inode(bp, ip, fs, ino);
1760 	if (DOINGSOFTDEP(vp))
1761 		softdep_load_inodeblock(ip);
1762 	else
1763 		ip->i_effnlink = ip->i_nlink;
1764 	bqrelse(bp);
1765 
1766 	/*
1767 	 * Initialize the vnode from the inode, check for aliases.
1768 	 * Note that the underlying vnode may have changed.
1769 	 */
1770 	if (ip->i_ump->um_fstype == UFS1)
1771 		error = ufs_vinit(mp, &ffs_fifoops1, &vp);
1772 	else
1773 		error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1774 	if (error) {
1775 		vput(vp);
1776 		*vpp = NULL;
1777 		return (error);
1778 	}
1779 
1780 	/*
1781 	 * Finish inode initialization.
1782 	 */
1783 	if (vp->v_type != VFIFO) {
1784 		/* FFS supports shared locking for all files except fifos. */
1785 		VN_LOCK_ASHARE(vp);
1786 	}
1787 
1788 	/*
1789 	 * Set up a generation number for this inode if it does not
1790 	 * already have one. This should only happen on old filesystems.
1791 	 */
1792 	if (ip->i_gen == 0) {
1793 		ip->i_gen = arc4random() / 2 + 1;
1794 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1795 			ip->i_flag |= IN_MODIFIED;
1796 			DIP_SET(ip, i_gen, ip->i_gen);
1797 		}
1798 	}
1799 #ifdef MAC
1800 	if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1801 		/*
1802 		 * If this vnode is already allocated, and we're running
1803 		 * multi-label, attempt to perform a label association
1804 		 * from the extended attributes on the inode.
1805 		 */
1806 		error = mac_vnode_associate_extattr(mp, vp);
1807 		if (error) {
1808 			/* ufs_inactive will release ip->i_devvp ref. */
1809 			vput(vp);
1810 			*vpp = NULL;
1811 			return (error);
1812 		}
1813 	}
1814 #endif
1815 
1816 	*vpp = vp;
1817 	return (0);
1818 }
1819 
1820 /*
1821  * File handle to vnode
1822  *
1823  * Have to be really careful about stale file handles:
1824  * - check that the inode number is valid
1825  * - call ffs_vget() to get the locked inode
1826  * - check for an unallocated inode (i_mode == 0)
1827  * - check that the given client host has export rights and return
1828  *   those rights via. exflagsp and credanonp
1829  */
1830 static int
1831 ffs_fhtovp(mp, fhp, flags, vpp)
1832 	struct mount *mp;
1833 	struct fid *fhp;
1834 	int flags;
1835 	struct vnode **vpp;
1836 {
1837 	struct ufid *ufhp;
1838 	struct fs *fs;
1839 
1840 	ufhp = (struct ufid *)fhp;
1841 	fs = VFSTOUFS(mp)->um_fs;
1842 	if (ufhp->ufid_ino < ROOTINO ||
1843 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1844 		return (ESTALE);
1845 	return (ufs_fhtovp(mp, ufhp, flags, vpp));
1846 }
1847 
1848 /*
1849  * Initialize the filesystem.
1850  */
1851 static int
1852 ffs_init(vfsp)
1853 	struct vfsconf *vfsp;
1854 {
1855 
1856 	ffs_susp_initialize();
1857 	softdep_initialize();
1858 	return (ufs_init(vfsp));
1859 }
1860 
1861 /*
1862  * Undo the work of ffs_init().
1863  */
1864 static int
1865 ffs_uninit(vfsp)
1866 	struct vfsconf *vfsp;
1867 {
1868 	int ret;
1869 
1870 	ret = ufs_uninit(vfsp);
1871 	softdep_uninitialize();
1872 	ffs_susp_uninitialize();
1873 	return (ret);
1874 }
1875 
1876 /*
1877  * Write a superblock and associated information back to disk.
1878  */
1879 int
1880 ffs_sbupdate(ump, waitfor, suspended)
1881 	struct ufsmount *ump;
1882 	int waitfor;
1883 	int suspended;
1884 {
1885 	struct fs *fs = ump->um_fs;
1886 	struct buf *sbbp;
1887 	struct buf *bp;
1888 	int blks;
1889 	void *space;
1890 	int i, size, error, allerror = 0;
1891 
1892 	if (fs->fs_ronly == 1 &&
1893 	    (ump->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1894 	    (MNT_RDONLY | MNT_UPDATE) && ump->um_fsckpid == 0)
1895 		panic("ffs_sbupdate: write read-only filesystem");
1896 	/*
1897 	 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1898 	 */
1899 	sbbp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
1900 	    (int)fs->fs_sbsize, 0, 0, 0);
1901 	/*
1902 	 * First write back the summary information.
1903 	 */
1904 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
1905 	space = fs->fs_csp;
1906 	for (i = 0; i < blks; i += fs->fs_frag) {
1907 		size = fs->fs_bsize;
1908 		if (i + fs->fs_frag > blks)
1909 			size = (blks - i) * fs->fs_fsize;
1910 		bp = getblk(ump->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1911 		    size, 0, 0, 0);
1912 		bcopy(space, bp->b_data, (u_int)size);
1913 		space = (char *)space + size;
1914 		if (suspended)
1915 			bp->b_flags |= B_VALIDSUSPWRT;
1916 		if (waitfor != MNT_WAIT)
1917 			bawrite(bp);
1918 		else if ((error = bwrite(bp)) != 0)
1919 			allerror = error;
1920 	}
1921 	/*
1922 	 * Now write back the superblock itself. If any errors occurred
1923 	 * up to this point, then fail so that the superblock avoids
1924 	 * being written out as clean.
1925 	 */
1926 	if (allerror) {
1927 		brelse(sbbp);
1928 		return (allerror);
1929 	}
1930 	bp = sbbp;
1931 	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1932 	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1933 		printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1934 		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1935 		fs->fs_sblockloc = SBLOCK_UFS1;
1936 	}
1937 	if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1938 	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1939 		printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1940 		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1941 		fs->fs_sblockloc = SBLOCK_UFS2;
1942 	}
1943 	fs->fs_fmod = 0;
1944 	fs->fs_time = time_second;
1945 	if (fs->fs_flags & FS_DOSOFTDEP)
1946 		softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, bp);
1947 	bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1948 	ffs_oldfscompat_write((struct fs *)bp->b_data, ump);
1949 	if (suspended)
1950 		bp->b_flags |= B_VALIDSUSPWRT;
1951 	if (waitfor != MNT_WAIT)
1952 		bawrite(bp);
1953 	else if ((error = bwrite(bp)) != 0)
1954 		allerror = error;
1955 	return (allerror);
1956 }
1957 
1958 static int
1959 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1960 	int attrnamespace, const char *attrname)
1961 {
1962 
1963 #ifdef UFS_EXTATTR
1964 	return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1965 	    attrname));
1966 #else
1967 	return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1968 	    attrname));
1969 #endif
1970 }
1971 
1972 static void
1973 ffs_ifree(struct ufsmount *ump, struct inode *ip)
1974 {
1975 
1976 	if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1977 		uma_zfree(uma_ufs1, ip->i_din1);
1978 	else if (ip->i_din2 != NULL)
1979 		uma_zfree(uma_ufs2, ip->i_din2);
1980 	uma_zfree(uma_inode, ip);
1981 }
1982 
1983 static int dobkgrdwrite = 1;
1984 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1985     "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1986 
1987 /*
1988  * Complete a background write started from bwrite.
1989  */
1990 static void
1991 ffs_backgroundwritedone(struct buf *bp)
1992 {
1993 	struct bufobj *bufobj;
1994 	struct buf *origbp;
1995 
1996 	/*
1997 	 * Find the original buffer that we are writing.
1998 	 */
1999 	bufobj = bp->b_bufobj;
2000 	BO_LOCK(bufobj);
2001 	if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
2002 		panic("backgroundwritedone: lost buffer");
2003 	BO_UNLOCK(bufobj);
2004 	/*
2005 	 * Process dependencies then return any unfinished ones.
2006 	 */
2007 	pbrelvp(bp);
2008 	if (!LIST_EMPTY(&bp->b_dep))
2009 		buf_complete(bp);
2010 #ifdef SOFTUPDATES
2011 	if (!LIST_EMPTY(&bp->b_dep))
2012 		softdep_move_dependencies(bp, origbp);
2013 #endif
2014 	/*
2015 	 * This buffer is marked B_NOCACHE so when it is released
2016 	 * by biodone it will be tossed.
2017 	 */
2018 	bp->b_flags |= B_NOCACHE;
2019 	bp->b_flags &= ~B_CACHE;
2020 	bufdone(bp);
2021 	BO_LOCK(bufobj);
2022 	/*
2023 	 * Clear the BV_BKGRDINPROG flag in the original buffer
2024 	 * and awaken it if it is waiting for the write to complete.
2025 	 * If BV_BKGRDINPROG is not set in the original buffer it must
2026 	 * have been released and re-instantiated - which is not legal.
2027 	 */
2028 	KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
2029 	    ("backgroundwritedone: lost buffer2"));
2030 	origbp->b_vflags &= ~BV_BKGRDINPROG;
2031 	if (origbp->b_vflags & BV_BKGRDWAIT) {
2032 		origbp->b_vflags &= ~BV_BKGRDWAIT;
2033 		wakeup(&origbp->b_xflags);
2034 	}
2035 	BO_UNLOCK(bufobj);
2036 }
2037 
2038 
2039 /*
2040  * Write, release buffer on completion.  (Done by iodone
2041  * if async).  Do not bother writing anything if the buffer
2042  * is invalid.
2043  *
2044  * Note that we set B_CACHE here, indicating that buffer is
2045  * fully valid and thus cacheable.  This is true even of NFS
2046  * now so we set it generally.  This could be set either here
2047  * or in biodone() since the I/O is synchronous.  We put it
2048  * here.
2049  */
2050 static int
2051 ffs_bufwrite(struct buf *bp)
2052 {
2053 	struct buf *newbp;
2054 	int oldflags;
2055 
2056 	CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2057 	if (bp->b_flags & B_INVAL) {
2058 		brelse(bp);
2059 		return (0);
2060 	}
2061 
2062 	oldflags = bp->b_flags;
2063 
2064 	if (!BUF_ISLOCKED(bp))
2065 		panic("bufwrite: buffer is not busy???");
2066 	/*
2067 	 * If a background write is already in progress, delay
2068 	 * writing this block if it is asynchronous. Otherwise
2069 	 * wait for the background write to complete.
2070 	 */
2071 	BO_LOCK(bp->b_bufobj);
2072 	if (bp->b_vflags & BV_BKGRDINPROG) {
2073 		if (bp->b_flags & B_ASYNC) {
2074 			BO_UNLOCK(bp->b_bufobj);
2075 			bdwrite(bp);
2076 			return (0);
2077 		}
2078 		bp->b_vflags |= BV_BKGRDWAIT;
2079 		msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0);
2080 		if (bp->b_vflags & BV_BKGRDINPROG)
2081 			panic("bufwrite: still writing");
2082 	}
2083 	BO_UNLOCK(bp->b_bufobj);
2084 
2085 	/*
2086 	 * If this buffer is marked for background writing and we
2087 	 * do not have to wait for it, make a copy and write the
2088 	 * copy so as to leave this buffer ready for further use.
2089 	 *
2090 	 * This optimization eats a lot of memory.  If we have a page
2091 	 * or buffer shortfall we can't do it.
2092 	 */
2093 	if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
2094 	    (bp->b_flags & B_ASYNC) &&
2095 	    !vm_page_count_severe() &&
2096 	    !buf_dirty_count_severe()) {
2097 		KASSERT(bp->b_iodone == NULL,
2098 		    ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
2099 
2100 		/* get a new block */
2101 		newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
2102 		if (newbp == NULL)
2103 			goto normal_write;
2104 
2105 		KASSERT((bp->b_flags & B_UNMAPPED) == 0, ("Unmapped cg"));
2106 		memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
2107 		BO_LOCK(bp->b_bufobj);
2108 		bp->b_vflags |= BV_BKGRDINPROG;
2109 		BO_UNLOCK(bp->b_bufobj);
2110 		newbp->b_xflags |= BX_BKGRDMARKER;
2111 		newbp->b_lblkno = bp->b_lblkno;
2112 		newbp->b_blkno = bp->b_blkno;
2113 		newbp->b_offset = bp->b_offset;
2114 		newbp->b_iodone = ffs_backgroundwritedone;
2115 		newbp->b_flags |= B_ASYNC;
2116 		newbp->b_flags &= ~B_INVAL;
2117 		pbgetvp(bp->b_vp, newbp);
2118 
2119 #ifdef SOFTUPDATES
2120 		/*
2121 		 * Move over the dependencies.  If there are rollbacks,
2122 		 * leave the parent buffer dirtied as it will need to
2123 		 * be written again.
2124 		 */
2125 		if (LIST_EMPTY(&bp->b_dep) ||
2126 		    softdep_move_dependencies(bp, newbp) == 0)
2127 			bundirty(bp);
2128 #else
2129 		bundirty(bp);
2130 #endif
2131 
2132 		/*
2133 		 * Initiate write on the copy, release the original.  The
2134 		 * BKGRDINPROG flag prevents it from going away until
2135 		 * the background write completes.
2136 		 */
2137 		bqrelse(bp);
2138 		bp = newbp;
2139 	} else
2140 		/* Mark the buffer clean */
2141 		bundirty(bp);
2142 
2143 
2144 	/* Let the normal bufwrite do the rest for us */
2145 normal_write:
2146 	return (bufwrite(bp));
2147 }
2148 
2149 
2150 static void
2151 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
2152 {
2153 	struct vnode *vp;
2154 	int error;
2155 	struct buf *tbp;
2156 	int nocopy;
2157 
2158 	vp = bo->__bo_vnode;
2159 	if (bp->b_iocmd == BIO_WRITE) {
2160 		if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
2161 		    bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
2162 		    (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
2163 			panic("ffs_geom_strategy: bad I/O");
2164 		nocopy = bp->b_flags & B_NOCOPY;
2165 		bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY);
2166 		if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 &&
2167 		    vp->v_rdev->si_snapdata != NULL) {
2168 			if ((bp->b_flags & B_CLUSTER) != 0) {
2169 				runningbufwakeup(bp);
2170 				TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2171 					      b_cluster.cluster_entry) {
2172 					error = ffs_copyonwrite(vp, tbp);
2173 					if (error != 0 &&
2174 					    error != EOPNOTSUPP) {
2175 						bp->b_error = error;
2176 						bp->b_ioflags |= BIO_ERROR;
2177 						bufdone(bp);
2178 						return;
2179 					}
2180 				}
2181 				bp->b_runningbufspace = bp->b_bufsize;
2182 				atomic_add_long(&runningbufspace,
2183 					       bp->b_runningbufspace);
2184 			} else {
2185 				error = ffs_copyonwrite(vp, bp);
2186 				if (error != 0 && error != EOPNOTSUPP) {
2187 					bp->b_error = error;
2188 					bp->b_ioflags |= BIO_ERROR;
2189 					bufdone(bp);
2190 					return;
2191 				}
2192 			}
2193 		}
2194 #ifdef SOFTUPDATES
2195 		if ((bp->b_flags & B_CLUSTER) != 0) {
2196 			TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2197 				      b_cluster.cluster_entry) {
2198 				if (!LIST_EMPTY(&tbp->b_dep))
2199 					buf_start(tbp);
2200 			}
2201 		} else {
2202 			if (!LIST_EMPTY(&bp->b_dep))
2203 				buf_start(bp);
2204 		}
2205 
2206 #endif
2207 	}
2208 	g_vfs_strategy(bo, bp);
2209 }
2210 
2211 int
2212 ffs_own_mount(const struct mount *mp)
2213 {
2214 
2215 	if (mp->mnt_op == &ufs_vfsops)
2216 		return (1);
2217 	return (0);
2218 }
2219 
2220 #ifdef	DDB
2221 
2222 static void
2223 db_print_ffs(struct ufsmount *ump)
2224 {
2225 	db_printf("mp %p %s devvp %p fs %p su_wl %d su_deps %d su_req %d\n",
2226 	    ump->um_mountp, ump->um_mountp->mnt_stat.f_mntonname,
2227 	    ump->um_devvp, ump->um_fs, ump->softdep_on_worklist,
2228 	    ump->softdep_deps, ump->softdep_req);
2229 }
2230 
2231 DB_SHOW_COMMAND(ffs, db_show_ffs)
2232 {
2233 	struct mount *mp;
2234 	struct ufsmount *ump;
2235 
2236 	if (have_addr) {
2237 		ump = VFSTOUFS((struct mount *)addr);
2238 		db_print_ffs(ump);
2239 		return;
2240 	}
2241 
2242 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2243 		if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2244 			db_print_ffs(VFSTOUFS(mp));
2245 	}
2246 }
2247 
2248 #endif	/* DDB */
2249