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