xref: /freebsd/sys/ufs/ffs/ffs_vfsops.c (revision 2726a7014867ad7224d09b66836c5d385f0350f4)
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 	int error;
600 
601 	if (data == NULL)
602 		return (EINVAL);
603 	error = copyin(data, &args, sizeof args);
604 	if (error)
605 		return (error);
606 
607 	ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
608 	ma = mount_arg(ma, "export", &args.export, sizeof(args.export));
609 	error = kernel_mount(ma, flags);
610 
611 	return (error);
612 }
613 
614 /*
615  * Reload all incore data for a filesystem (used after running fsck on
616  * the root filesystem and finding things to fix). If the 'force' flag
617  * is 0, the filesystem must be mounted read-only.
618  *
619  * Things to do to update the mount:
620  *	1) invalidate all cached meta-data.
621  *	2) re-read superblock from disk.
622  *	3) re-read summary information from disk.
623  *	4) invalidate all inactive vnodes.
624  *	5) clear MNTK_SUSPEND2 and MNTK_SUSPENDED flags, allowing secondary
625  *	   writers, if requested.
626  *	6) invalidate all cached file data.
627  *	7) re-read inode data for all active vnodes.
628  */
629 int
630 ffs_reload(struct mount *mp, struct thread *td, int flags)
631 {
632 	struct vnode *vp, *mvp, *devvp;
633 	struct inode *ip;
634 	void *space;
635 	struct buf *bp;
636 	struct fs *fs, *newfs;
637 	struct ufsmount *ump;
638 	ufs2_daddr_t sblockloc;
639 	int i, blks, error;
640 	u_long size;
641 	int32_t *lp;
642 
643 	ump = VFSTOUFS(mp);
644 
645 	MNT_ILOCK(mp);
646 	if ((mp->mnt_flag & MNT_RDONLY) == 0 && (flags & FFSR_FORCE) == 0) {
647 		MNT_IUNLOCK(mp);
648 		return (EINVAL);
649 	}
650 	MNT_IUNLOCK(mp);
651 
652 	/*
653 	 * Step 1: invalidate all cached meta-data.
654 	 */
655 	devvp = VFSTOUFS(mp)->um_devvp;
656 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
657 	if (vinvalbuf(devvp, 0, 0, 0) != 0)
658 		panic("ffs_reload: dirty1");
659 	VOP_UNLOCK(devvp);
660 
661 	/*
662 	 * Step 2: re-read superblock from disk.
663 	 */
664 	fs = VFSTOUFS(mp)->um_fs;
665 	if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
666 	    NOCRED, &bp)) != 0)
667 		return (error);
668 	newfs = (struct fs *)bp->b_data;
669 	if ((newfs->fs_magic != FS_UFS1_MAGIC &&
670 	     newfs->fs_magic != FS_UFS2_MAGIC) ||
671 	    newfs->fs_bsize > MAXBSIZE ||
672 	    newfs->fs_bsize < sizeof(struct fs)) {
673 			brelse(bp);
674 			return (EIO);		/* XXX needs translation */
675 	}
676 	/*
677 	 * Copy pointer fields back into superblock before copying in	XXX
678 	 * new superblock. These should really be in the ufsmount.	XXX
679 	 * Note that important parameters (eg fs_ncg) are unchanged.
680 	 */
681 	newfs->fs_csp = fs->fs_csp;
682 	newfs->fs_maxcluster = fs->fs_maxcluster;
683 	newfs->fs_contigdirs = fs->fs_contigdirs;
684 	newfs->fs_active = fs->fs_active;
685 	newfs->fs_ronly = fs->fs_ronly;
686 	sblockloc = fs->fs_sblockloc;
687 	bcopy(newfs, fs, (u_int)fs->fs_sbsize);
688 	brelse(bp);
689 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
690 	ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
691 	UFS_LOCK(ump);
692 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
693 		printf("WARNING: %s: reload pending error: blocks %jd "
694 		    "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
695 		    fs->fs_pendinginodes);
696 		fs->fs_pendingblocks = 0;
697 		fs->fs_pendinginodes = 0;
698 	}
699 	UFS_UNLOCK(ump);
700 
701 	/*
702 	 * Step 3: re-read summary information from disk.
703 	 */
704 	size = fs->fs_cssize;
705 	blks = howmany(size, fs->fs_fsize);
706 	if (fs->fs_contigsumsize > 0)
707 		size += fs->fs_ncg * sizeof(int32_t);
708 	size += fs->fs_ncg * sizeof(u_int8_t);
709 	free(fs->fs_csp, M_UFSMNT);
710 	space = malloc(size, M_UFSMNT, M_WAITOK);
711 	fs->fs_csp = space;
712 	for (i = 0; i < blks; i += fs->fs_frag) {
713 		size = fs->fs_bsize;
714 		if (i + fs->fs_frag > blks)
715 			size = (blks - i) * fs->fs_fsize;
716 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
717 		    NOCRED, &bp);
718 		if (error)
719 			return (error);
720 		bcopy(bp->b_data, space, (u_int)size);
721 		space = (char *)space + size;
722 		brelse(bp);
723 	}
724 	/*
725 	 * We no longer know anything about clusters per cylinder group.
726 	 */
727 	if (fs->fs_contigsumsize > 0) {
728 		fs->fs_maxcluster = lp = space;
729 		for (i = 0; i < fs->fs_ncg; i++)
730 			*lp++ = fs->fs_contigsumsize;
731 		space = lp;
732 	}
733 	size = fs->fs_ncg * sizeof(u_int8_t);
734 	fs->fs_contigdirs = (u_int8_t *)space;
735 	bzero(fs->fs_contigdirs, size);
736 	if ((flags & FFSR_UNSUSPEND) != 0) {
737 		MNT_ILOCK(mp);
738 		mp->mnt_kern_flag &= ~(MNTK_SUSPENDED | MNTK_SUSPEND2);
739 		wakeup(&mp->mnt_flag);
740 		MNT_IUNLOCK(mp);
741 	}
742 
743 loop:
744 	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
745 		/*
746 		 * Skip syncer vnode.
747 		 */
748 		if (vp->v_type == VNON) {
749 			VI_UNLOCK(vp);
750 			continue;
751 		}
752 		/*
753 		 * Step 4: invalidate all cached file data.
754 		 */
755 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
756 			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
757 			goto loop;
758 		}
759 		if (vinvalbuf(vp, 0, 0, 0))
760 			panic("ffs_reload: dirty2");
761 		/*
762 		 * Step 5: re-read inode data for all active vnodes.
763 		 */
764 		ip = VTOI(vp);
765 		error =
766 		    bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
767 		    (int)fs->fs_bsize, NOCRED, &bp);
768 		if (error) {
769 			vput(vp);
770 			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
771 			return (error);
772 		}
773 		if ((error = ffs_load_inode(bp, ip, fs, ip->i_number)) != 0) {
774 			brelse(bp);
775 			vput(vp);
776 			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
777 			return (error);
778 		}
779 		ip->i_effnlink = ip->i_nlink;
780 		brelse(bp);
781 		vput(vp);
782 	}
783 	return (0);
784 }
785 
786 /*
787  * Common code for mount and mountroot
788  */
789 static int
790 ffs_mountfs(odevvp, mp, td)
791 	struct vnode *odevvp;
792 	struct mount *mp;
793 	struct thread *td;
794 {
795 	struct ufsmount *ump;
796 	struct fs *fs;
797 	struct cdev *dev;
798 	int error, i, len, ronly;
799 	struct ucred *cred;
800 	struct g_consumer *cp;
801 	struct mount *nmp;
802 	struct vnode *devvp;
803 	struct fsfail_task *etp;
804 	int candelete, canspeedup;
805 	off_t loc;
806 
807 	fs = NULL;
808 	ump = NULL;
809 	cred = td ? td->td_ucred : NOCRED;
810 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
811 
812 	devvp = mntfs_allocvp(mp, odevvp);
813 	VOP_UNLOCK(odevvp);
814 	KASSERT(devvp->v_type == VCHR, ("reclaimed devvp"));
815 	dev = devvp->v_rdev;
816 	if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
817 	    (uintptr_t)mp) == 0) {
818 		mntfs_freevp(devvp);
819 		return (EBUSY);
820 	}
821 	g_topology_lock();
822 	error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
823 	g_topology_unlock();
824 	if (error != 0) {
825 		atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
826 		mntfs_freevp(devvp);
827 		return (error);
828 	}
829 	dev_ref(dev);
830 	devvp->v_bufobj.bo_ops = &ffs_ops;
831 	BO_LOCK(&odevvp->v_bufobj);
832 	odevvp->v_bufobj.bo_flag |= BO_NOBUFS;
833 	BO_UNLOCK(&odevvp->v_bufobj);
834 	if (dev->si_iosize_max != 0)
835 		mp->mnt_iosize_max = dev->si_iosize_max;
836 	if (mp->mnt_iosize_max > MAXPHYS)
837 		mp->mnt_iosize_max = MAXPHYS;
838 	if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
839 		error = EINVAL;
840 		vfs_mount_error(mp,
841 		    "Invalid sectorsize %d for superblock size %d",
842 		    cp->provider->sectorsize, SBLOCKSIZE);
843 		goto out;
844 	}
845 	/* fetch the superblock and summary information */
846 	loc = STDSB;
847 	if ((mp->mnt_flag & MNT_ROOTFS) != 0)
848 		loc = STDSB_NOHASHFAIL;
849 	if ((error = ffs_sbget(devvp, &fs, loc, M_UFSMNT, ffs_use_bread)) != 0)
850 		goto out;
851 	/* none of these types of check-hashes are maintained by this kernel */
852 	fs->fs_metackhash &= ~(CK_INDIR | CK_DIR);
853 	/* no support for any undefined flags */
854 	fs->fs_flags &= FS_SUPPORTED;
855 	fs->fs_flags &= ~FS_UNCLEAN;
856 	if (fs->fs_clean == 0) {
857 		fs->fs_flags |= FS_UNCLEAN;
858 		if (ronly || (mp->mnt_flag & MNT_FORCE) ||
859 		    ((fs->fs_flags & (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
860 		     (fs->fs_flags & FS_DOSOFTDEP))) {
861 			printf("WARNING: %s was not properly dismounted\n",
862 			    fs->fs_fsmnt);
863 		} else {
864 			vfs_mount_error(mp, "R/W mount of %s denied. %s%s",
865 			    fs->fs_fsmnt, "Filesystem is not clean - run fsck.",
866 			    (fs->fs_flags & FS_SUJ) == 0 ? "" :
867 			    " Forced mount will invalidate journal contents");
868 			error = EPERM;
869 			goto out;
870 		}
871 		if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
872 		    (mp->mnt_flag & MNT_FORCE)) {
873 			printf("WARNING: %s: lost blocks %jd files %d\n",
874 			    fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
875 			    fs->fs_pendinginodes);
876 			fs->fs_pendingblocks = 0;
877 			fs->fs_pendinginodes = 0;
878 		}
879 	}
880 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
881 		printf("WARNING: %s: mount pending error: blocks %jd "
882 		    "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
883 		    fs->fs_pendinginodes);
884 		fs->fs_pendingblocks = 0;
885 		fs->fs_pendinginodes = 0;
886 	}
887 	if ((fs->fs_flags & FS_GJOURNAL) != 0) {
888 #ifdef UFS_GJOURNAL
889 		/*
890 		 * Get journal provider name.
891 		 */
892 		len = 1024;
893 		mp->mnt_gjprovider = malloc((u_long)len, M_UFSMNT, M_WAITOK);
894 		if (g_io_getattr("GJOURNAL::provider", cp, &len,
895 		    mp->mnt_gjprovider) == 0) {
896 			mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, len,
897 			    M_UFSMNT, M_WAITOK);
898 			MNT_ILOCK(mp);
899 			mp->mnt_flag |= MNT_GJOURNAL;
900 			MNT_IUNLOCK(mp);
901 		} else {
902 			printf("WARNING: %s: GJOURNAL flag on fs "
903 			    "but no gjournal provider below\n",
904 			    mp->mnt_stat.f_mntonname);
905 			free(mp->mnt_gjprovider, M_UFSMNT);
906 			mp->mnt_gjprovider = NULL;
907 		}
908 #else
909 		printf("WARNING: %s: GJOURNAL flag on fs but no "
910 		    "UFS_GJOURNAL support\n", mp->mnt_stat.f_mntonname);
911 #endif
912 	} else {
913 		mp->mnt_gjprovider = NULL;
914 	}
915 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
916 	ump->um_cp = cp;
917 	ump->um_bo = &devvp->v_bufobj;
918 	ump->um_fs = fs;
919 	if (fs->fs_magic == FS_UFS1_MAGIC) {
920 		ump->um_fstype = UFS1;
921 		ump->um_balloc = ffs_balloc_ufs1;
922 	} else {
923 		ump->um_fstype = UFS2;
924 		ump->um_balloc = ffs_balloc_ufs2;
925 	}
926 	ump->um_blkatoff = ffs_blkatoff;
927 	ump->um_truncate = ffs_truncate;
928 	ump->um_update = ffs_update;
929 	ump->um_valloc = ffs_valloc;
930 	ump->um_vfree = ffs_vfree;
931 	ump->um_ifree = ffs_ifree;
932 	ump->um_rdonly = ffs_rdonly;
933 	ump->um_snapgone = ffs_snapgone;
934 	if ((mp->mnt_flag & MNT_UNTRUSTED) != 0)
935 		ump->um_check_blkno = ffs_check_blkno;
936 	else
937 		ump->um_check_blkno = NULL;
938 	mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
939 	ffs_oldfscompat_read(fs, ump, fs->fs_sblockloc);
940 	fs->fs_ronly = ronly;
941 	fs->fs_active = NULL;
942 	mp->mnt_data = ump;
943 	mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
944 	mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
945 	nmp = NULL;
946 	if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
947 	    (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
948 		if (nmp)
949 			vfs_rel(nmp);
950 		vfs_getnewfsid(mp);
951 	}
952 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
953 	MNT_ILOCK(mp);
954 	mp->mnt_flag |= MNT_LOCAL;
955 	MNT_IUNLOCK(mp);
956 	if ((fs->fs_flags & FS_MULTILABEL) != 0) {
957 #ifdef MAC
958 		MNT_ILOCK(mp);
959 		mp->mnt_flag |= MNT_MULTILABEL;
960 		MNT_IUNLOCK(mp);
961 #else
962 		printf("WARNING: %s: multilabel flag on fs but "
963 		    "no MAC support\n", mp->mnt_stat.f_mntonname);
964 #endif
965 	}
966 	if ((fs->fs_flags & FS_ACLS) != 0) {
967 #ifdef UFS_ACL
968 		MNT_ILOCK(mp);
969 
970 		if (mp->mnt_flag & MNT_NFS4ACLS)
971 			printf("WARNING: %s: ACLs flag on fs conflicts with "
972 			    "\"nfsv4acls\" mount option; option ignored\n",
973 			    mp->mnt_stat.f_mntonname);
974 		mp->mnt_flag &= ~MNT_NFS4ACLS;
975 		mp->mnt_flag |= MNT_ACLS;
976 
977 		MNT_IUNLOCK(mp);
978 #else
979 		printf("WARNING: %s: ACLs flag on fs but no ACLs support\n",
980 		    mp->mnt_stat.f_mntonname);
981 #endif
982 	}
983 	if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
984 #ifdef UFS_ACL
985 		MNT_ILOCK(mp);
986 
987 		if (mp->mnt_flag & MNT_ACLS)
988 			printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts "
989 			    "with \"acls\" mount option; option ignored\n",
990 			    mp->mnt_stat.f_mntonname);
991 		mp->mnt_flag &= ~MNT_ACLS;
992 		mp->mnt_flag |= MNT_NFS4ACLS;
993 
994 		MNT_IUNLOCK(mp);
995 #else
996 		printf("WARNING: %s: NFSv4 ACLs flag on fs but no "
997 		    "ACLs support\n", mp->mnt_stat.f_mntonname);
998 #endif
999 	}
1000 	if ((fs->fs_flags & FS_TRIM) != 0) {
1001 		len = sizeof(int);
1002 		if (g_io_getattr("GEOM::candelete", cp, &len,
1003 		    &candelete) == 0) {
1004 			if (candelete)
1005 				ump->um_flags |= UM_CANDELETE;
1006 			else
1007 				printf("WARNING: %s: TRIM flag on fs but disk "
1008 				    "does not support TRIM\n",
1009 				    mp->mnt_stat.f_mntonname);
1010 		} else {
1011 			printf("WARNING: %s: TRIM flag on fs but disk does "
1012 			    "not confirm that it supports TRIM\n",
1013 			    mp->mnt_stat.f_mntonname);
1014 		}
1015 		if (((ump->um_flags) & UM_CANDELETE) != 0) {
1016 			ump->um_trim_tq = taskqueue_create("trim", M_WAITOK,
1017 			    taskqueue_thread_enqueue, &ump->um_trim_tq);
1018 			taskqueue_start_threads(&ump->um_trim_tq, 1, PVFS,
1019 			    "%s trim", mp->mnt_stat.f_mntonname);
1020 			ump->um_trimhash = hashinit(MAXTRIMIO, M_TRIM,
1021 			    &ump->um_trimlisthashsize);
1022 		}
1023 	}
1024 
1025 	len = sizeof(int);
1026 	if (g_io_getattr("GEOM::canspeedup", cp, &len, &canspeedup) == 0) {
1027 		if (canspeedup)
1028 			ump->um_flags |= UM_CANSPEEDUP;
1029 	}
1030 
1031 	ump->um_mountp = mp;
1032 	ump->um_dev = dev;
1033 	ump->um_devvp = devvp;
1034 	ump->um_odevvp = odevvp;
1035 	ump->um_nindir = fs->fs_nindir;
1036 	ump->um_bptrtodb = fs->fs_fsbtodb;
1037 	ump->um_seqinc = fs->fs_frag;
1038 	for (i = 0; i < MAXQUOTAS; i++)
1039 		ump->um_quotas[i] = NULLVP;
1040 #ifdef UFS_EXTATTR
1041 	ufs_extattr_uepm_init(&ump->um_extattr);
1042 #endif
1043 	/*
1044 	 * Set FS local "last mounted on" information (NULL pad)
1045 	 */
1046 	bzero(fs->fs_fsmnt, MAXMNTLEN);
1047 	strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
1048 	mp->mnt_stat.f_iosize = fs->fs_bsize;
1049 
1050 	if (mp->mnt_flag & MNT_ROOTFS) {
1051 		/*
1052 		 * Root mount; update timestamp in mount structure.
1053 		 * this will be used by the common root mount code
1054 		 * to update the system clock.
1055 		 */
1056 		mp->mnt_time = fs->fs_time;
1057 	}
1058 
1059 	if (ronly == 0) {
1060 		fs->fs_mtime = time_second;
1061 		if ((fs->fs_flags & FS_DOSOFTDEP) &&
1062 		    (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
1063 			ffs_flushfiles(mp, FORCECLOSE, td);
1064 			goto out;
1065 		}
1066 		if (fs->fs_snapinum[0] != 0)
1067 			ffs_snapshot_mount(mp);
1068 		fs->fs_fmod = 1;
1069 		fs->fs_clean = 0;
1070 		(void) ffs_sbupdate(ump, MNT_WAIT, 0);
1071 	}
1072 	/*
1073 	 * Initialize filesystem state information in mount struct.
1074 	 */
1075 	MNT_ILOCK(mp);
1076 	mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
1077 	    MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS | MNTK_USES_BCACHE;
1078 	MNT_IUNLOCK(mp);
1079 #ifdef UFS_EXTATTR
1080 #ifdef UFS_EXTATTR_AUTOSTART
1081 	/*
1082 	 *
1083 	 * Auto-starting does the following:
1084 	 *	- check for /.attribute in the fs, and extattr_start if so
1085 	 *	- for each file in .attribute, enable that file with
1086 	 * 	  an attribute of the same name.
1087 	 * Not clear how to report errors -- probably eat them.
1088 	 * This would all happen while the filesystem was busy/not
1089 	 * available, so would effectively be "atomic".
1090 	 */
1091 	(void) ufs_extattr_autostart(mp, td);
1092 #endif /* !UFS_EXTATTR_AUTOSTART */
1093 #endif /* !UFS_EXTATTR */
1094 	etp = malloc(sizeof *ump->um_fsfail_task, M_UFSMNT, M_WAITOK | M_ZERO);
1095 	etp->fsid = mp->mnt_stat.f_fsid;
1096 	ump->um_fsfail_task = etp;
1097 	return (0);
1098 out:
1099 	if (fs != NULL) {
1100 		free(fs->fs_csp, M_UFSMNT);
1101 		free(fs, M_UFSMNT);
1102 	}
1103 	if (cp != NULL) {
1104 		g_topology_lock();
1105 		g_vfs_close(cp);
1106 		g_topology_unlock();
1107 	}
1108 	if (ump) {
1109 		mtx_destroy(UFS_MTX(ump));
1110 		if (mp->mnt_gjprovider != NULL) {
1111 			free(mp->mnt_gjprovider, M_UFSMNT);
1112 			mp->mnt_gjprovider = NULL;
1113 		}
1114 		free(ump, M_UFSMNT);
1115 		mp->mnt_data = NULL;
1116 	}
1117 	BO_LOCK(&odevvp->v_bufobj);
1118 	odevvp->v_bufobj.bo_flag &= ~BO_NOBUFS;
1119 	BO_UNLOCK(&odevvp->v_bufobj);
1120 	atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
1121 	mntfs_freevp(devvp);
1122 	dev_rel(dev);
1123 	return (error);
1124 }
1125 
1126 /*
1127  * A read function for use by filesystem-layer routines.
1128  */
1129 static int
1130 ffs_use_bread(void *devfd, off_t loc, void **bufp, int size)
1131 {
1132 	struct buf *bp;
1133 	int error;
1134 
1135 	KASSERT(*bufp == NULL, ("ffs_use_bread: non-NULL *bufp %p\n", *bufp));
1136 	*bufp = malloc(size, M_UFSMNT, M_WAITOK);
1137 	if ((error = bread((struct vnode *)devfd, btodb(loc), size, NOCRED,
1138 	    &bp)) != 0)
1139 		return (error);
1140 	bcopy(bp->b_data, *bufp, size);
1141 	bp->b_flags |= B_INVAL | B_NOCACHE;
1142 	brelse(bp);
1143 	return (0);
1144 }
1145 
1146 static int bigcgs = 0;
1147 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1148 
1149 /*
1150  * Sanity checks for loading old filesystem superblocks.
1151  * See ffs_oldfscompat_write below for unwound actions.
1152  *
1153  * XXX - Parts get retired eventually.
1154  * Unfortunately new bits get added.
1155  */
1156 static void
1157 ffs_oldfscompat_read(fs, ump, sblockloc)
1158 	struct fs *fs;
1159 	struct ufsmount *ump;
1160 	ufs2_daddr_t sblockloc;
1161 {
1162 	off_t maxfilesize;
1163 
1164 	/*
1165 	 * If not yet done, update fs_flags location and value of fs_sblockloc.
1166 	 */
1167 	if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1168 		fs->fs_flags = fs->fs_old_flags;
1169 		fs->fs_old_flags |= FS_FLAGS_UPDATED;
1170 		fs->fs_sblockloc = sblockloc;
1171 	}
1172 	/*
1173 	 * If not yet done, update UFS1 superblock with new wider fields.
1174 	 */
1175 	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1176 		fs->fs_maxbsize = fs->fs_bsize;
1177 		fs->fs_time = fs->fs_old_time;
1178 		fs->fs_size = fs->fs_old_size;
1179 		fs->fs_dsize = fs->fs_old_dsize;
1180 		fs->fs_csaddr = fs->fs_old_csaddr;
1181 		fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1182 		fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1183 		fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1184 		fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1185 	}
1186 	if (fs->fs_magic == FS_UFS1_MAGIC &&
1187 	    fs->fs_old_inodefmt < FS_44INODEFMT) {
1188 		fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1189 		fs->fs_qbmask = ~fs->fs_bmask;
1190 		fs->fs_qfmask = ~fs->fs_fmask;
1191 	}
1192 	if (fs->fs_magic == FS_UFS1_MAGIC) {
1193 		ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1194 		maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1195 		if (fs->fs_maxfilesize > maxfilesize)
1196 			fs->fs_maxfilesize = maxfilesize;
1197 	}
1198 	/* Compatibility for old filesystems */
1199 	if (fs->fs_avgfilesize <= 0)
1200 		fs->fs_avgfilesize = AVFILESIZ;
1201 	if (fs->fs_avgfpdir <= 0)
1202 		fs->fs_avgfpdir = AFPDIR;
1203 	if (bigcgs) {
1204 		fs->fs_save_cgsize = fs->fs_cgsize;
1205 		fs->fs_cgsize = fs->fs_bsize;
1206 	}
1207 }
1208 
1209 /*
1210  * Unwinding superblock updates for old filesystems.
1211  * See ffs_oldfscompat_read above for details.
1212  *
1213  * XXX - Parts get retired eventually.
1214  * Unfortunately new bits get added.
1215  */
1216 void
1217 ffs_oldfscompat_write(fs, ump)
1218 	struct fs *fs;
1219 	struct ufsmount *ump;
1220 {
1221 
1222 	/*
1223 	 * Copy back UFS2 updated fields that UFS1 inspects.
1224 	 */
1225 	if (fs->fs_magic == FS_UFS1_MAGIC) {
1226 		fs->fs_old_time = fs->fs_time;
1227 		fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1228 		fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1229 		fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1230 		fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1231 		fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1232 	}
1233 	if (bigcgs) {
1234 		fs->fs_cgsize = fs->fs_save_cgsize;
1235 		fs->fs_save_cgsize = 0;
1236 	}
1237 }
1238 
1239 /*
1240  * unmount system call
1241  */
1242 static int
1243 ffs_unmount(mp, mntflags)
1244 	struct mount *mp;
1245 	int mntflags;
1246 {
1247 	struct thread *td;
1248 	struct ufsmount *ump = VFSTOUFS(mp);
1249 	struct fs *fs;
1250 	int error, flags, susp;
1251 #ifdef UFS_EXTATTR
1252 	int e_restart;
1253 #endif
1254 
1255 	flags = 0;
1256 	td = curthread;
1257 	fs = ump->um_fs;
1258 	if (mntflags & MNT_FORCE)
1259 		flags |= FORCECLOSE;
1260 	susp = fs->fs_ronly == 0;
1261 #ifdef UFS_EXTATTR
1262 	if ((error = ufs_extattr_stop(mp, td))) {
1263 		if (error != EOPNOTSUPP)
1264 			printf("WARNING: unmount %s: ufs_extattr_stop "
1265 			    "returned errno %d\n", mp->mnt_stat.f_mntonname,
1266 			    error);
1267 		e_restart = 0;
1268 	} else {
1269 		ufs_extattr_uepm_destroy(&ump->um_extattr);
1270 		e_restart = 1;
1271 	}
1272 #endif
1273 	if (susp) {
1274 		error = vfs_write_suspend_umnt(mp);
1275 		if (error != 0)
1276 			goto fail1;
1277 	}
1278 	if (MOUNTEDSOFTDEP(mp))
1279 		error = softdep_flushfiles(mp, flags, td);
1280 	else
1281 		error = ffs_flushfiles(mp, flags, td);
1282 	if (error != 0 && !ffs_fsfail_cleanup(ump, error))
1283 		goto fail;
1284 
1285 	UFS_LOCK(ump);
1286 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1287 		printf("WARNING: unmount %s: pending error: blocks %jd "
1288 		    "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1289 		    fs->fs_pendinginodes);
1290 		fs->fs_pendingblocks = 0;
1291 		fs->fs_pendinginodes = 0;
1292 	}
1293 	UFS_UNLOCK(ump);
1294 	if (MOUNTEDSOFTDEP(mp))
1295 		softdep_unmount(mp);
1296 	if (fs->fs_ronly == 0 || ump->um_fsckpid > 0) {
1297 		fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1298 		error = ffs_sbupdate(ump, MNT_WAIT, 0);
1299 		if (ffs_fsfail_cleanup(ump, error))
1300 			error = 0;
1301 		if (error != 0 && !ffs_fsfail_cleanup(ump, error)) {
1302 			fs->fs_clean = 0;
1303 			goto fail;
1304 		}
1305 	}
1306 	if (susp)
1307 		vfs_write_resume(mp, VR_START_WRITE);
1308 	if (ump->um_trim_tq != NULL) {
1309 		while (ump->um_trim_inflight != 0)
1310 			pause("ufsutr", hz);
1311 		taskqueue_drain_all(ump->um_trim_tq);
1312 		taskqueue_free(ump->um_trim_tq);
1313 		free (ump->um_trimhash, M_TRIM);
1314 	}
1315 	g_topology_lock();
1316 	if (ump->um_fsckpid > 0) {
1317 		/*
1318 		 * Return to normal read-only mode.
1319 		 */
1320 		error = g_access(ump->um_cp, 0, -1, 0);
1321 		ump->um_fsckpid = 0;
1322 	}
1323 	g_vfs_close(ump->um_cp);
1324 	g_topology_unlock();
1325 	BO_LOCK(&ump->um_odevvp->v_bufobj);
1326 	ump->um_odevvp->v_bufobj.bo_flag &= ~BO_NOBUFS;
1327 	BO_UNLOCK(&ump->um_odevvp->v_bufobj);
1328 	atomic_store_rel_ptr((uintptr_t *)&ump->um_dev->si_mountpt, 0);
1329 	mntfs_freevp(ump->um_devvp);
1330 	vrele(ump->um_odevvp);
1331 	dev_rel(ump->um_dev);
1332 	mtx_destroy(UFS_MTX(ump));
1333 	if (mp->mnt_gjprovider != NULL) {
1334 		free(mp->mnt_gjprovider, M_UFSMNT);
1335 		mp->mnt_gjprovider = NULL;
1336 	}
1337 	free(fs->fs_csp, M_UFSMNT);
1338 	free(fs, M_UFSMNT);
1339 	if (ump->um_fsfail_task != NULL)
1340 		free(ump->um_fsfail_task, M_UFSMNT);
1341 	free(ump, M_UFSMNT);
1342 	mp->mnt_data = NULL;
1343 	MNT_ILOCK(mp);
1344 	mp->mnt_flag &= ~MNT_LOCAL;
1345 	MNT_IUNLOCK(mp);
1346 	if (td->td_su == mp) {
1347 		td->td_su = NULL;
1348 		vfs_rel(mp);
1349 	}
1350 	return (error);
1351 
1352 fail:
1353 	if (susp)
1354 		vfs_write_resume(mp, VR_START_WRITE);
1355 fail1:
1356 #ifdef UFS_EXTATTR
1357 	if (e_restart) {
1358 		ufs_extattr_uepm_init(&ump->um_extattr);
1359 #ifdef UFS_EXTATTR_AUTOSTART
1360 		(void) ufs_extattr_autostart(mp, td);
1361 #endif
1362 	}
1363 #endif
1364 
1365 	return (error);
1366 }
1367 
1368 /*
1369  * Flush out all the files in a filesystem.
1370  */
1371 int
1372 ffs_flushfiles(mp, flags, td)
1373 	struct mount *mp;
1374 	int flags;
1375 	struct thread *td;
1376 {
1377 	struct ufsmount *ump;
1378 	int qerror, error;
1379 
1380 	ump = VFSTOUFS(mp);
1381 	qerror = 0;
1382 #ifdef QUOTA
1383 	if (mp->mnt_flag & MNT_QUOTA) {
1384 		int i;
1385 		error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1386 		if (error)
1387 			return (error);
1388 		for (i = 0; i < MAXQUOTAS; i++) {
1389 			error = quotaoff(td, mp, i);
1390 			if (error != 0) {
1391 				if ((flags & EARLYFLUSH) == 0)
1392 					return (error);
1393 				else
1394 					qerror = error;
1395 			}
1396 		}
1397 
1398 		/*
1399 		 * Here we fall through to vflush again to ensure that
1400 		 * we have gotten rid of all the system vnodes, unless
1401 		 * quotas must not be closed.
1402 		 */
1403 	}
1404 #endif
1405 	ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1406 	if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1407 		if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1408 			return (error);
1409 		ffs_snapshot_unmount(mp);
1410 		flags |= FORCECLOSE;
1411 		/*
1412 		 * Here we fall through to vflush again to ensure
1413 		 * that we have gotten rid of all the system vnodes.
1414 		 */
1415 	}
1416 
1417 	/*
1418 	 * Do not close system files if quotas were not closed, to be
1419 	 * able to sync the remaining dquots.  The freeblks softupdate
1420 	 * workitems might hold a reference on a dquot, preventing
1421 	 * quotaoff() from completing.  Next round of
1422 	 * softdep_flushworklist() iteration should process the
1423 	 * blockers, allowing the next run of quotaoff() to finally
1424 	 * flush held dquots.
1425 	 *
1426 	 * Otherwise, flush all the files.
1427 	 */
1428 	if (qerror == 0 && (error = vflush(mp, 0, flags, td)) != 0)
1429 		return (error);
1430 
1431 	/*
1432 	 * Flush filesystem metadata.
1433 	 */
1434 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1435 	error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1436 	VOP_UNLOCK(ump->um_devvp);
1437 	return (error);
1438 }
1439 
1440 /*
1441  * Get filesystem statistics.
1442  */
1443 static int
1444 ffs_statfs(mp, sbp)
1445 	struct mount *mp;
1446 	struct statfs *sbp;
1447 {
1448 	struct ufsmount *ump;
1449 	struct fs *fs;
1450 
1451 	ump = VFSTOUFS(mp);
1452 	fs = ump->um_fs;
1453 	if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1454 		panic("ffs_statfs");
1455 	sbp->f_version = STATFS_VERSION;
1456 	sbp->f_bsize = fs->fs_fsize;
1457 	sbp->f_iosize = fs->fs_bsize;
1458 	sbp->f_blocks = fs->fs_dsize;
1459 	UFS_LOCK(ump);
1460 	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1461 	    fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1462 	sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1463 	    dbtofsb(fs, fs->fs_pendingblocks);
1464 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - UFS_ROOTINO;
1465 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1466 	UFS_UNLOCK(ump);
1467 	sbp->f_namemax = UFS_MAXNAMLEN;
1468 	return (0);
1469 }
1470 
1471 static bool
1472 sync_doupdate(struct inode *ip)
1473 {
1474 
1475 	return ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
1476 	    IN_UPDATE)) != 0);
1477 }
1478 
1479 static int
1480 ffs_sync_lazy_filter(struct vnode *vp, void *arg __unused)
1481 {
1482 	struct inode *ip;
1483 
1484 	/*
1485 	 * Flags are safe to access because ->v_data invalidation
1486 	 * is held off by listmtx.
1487 	 */
1488 	if (vp->v_type == VNON)
1489 		return (false);
1490 	ip = VTOI(vp);
1491 	if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0)
1492 		return (false);
1493 	return (true);
1494 }
1495 
1496 /*
1497  * For a lazy sync, we only care about access times, quotas and the
1498  * superblock.  Other filesystem changes are already converted to
1499  * cylinder group blocks or inode blocks updates and are written to
1500  * disk by syncer.
1501  */
1502 static int
1503 ffs_sync_lazy(mp)
1504      struct mount *mp;
1505 {
1506 	struct vnode *mvp, *vp;
1507 	struct inode *ip;
1508 	struct thread *td;
1509 	int allerror, error;
1510 
1511 	allerror = 0;
1512 	td = curthread;
1513 	if ((mp->mnt_flag & MNT_NOATIME) != 0) {
1514 #ifdef QUOTA
1515 		qsync(mp);
1516 #endif
1517 		goto sbupdate;
1518 	}
1519 	MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, ffs_sync_lazy_filter, NULL) {
1520 		if (vp->v_type == VNON) {
1521 			VI_UNLOCK(vp);
1522 			continue;
1523 		}
1524 		ip = VTOI(vp);
1525 
1526 		/*
1527 		 * The IN_ACCESS flag is converted to IN_MODIFIED by
1528 		 * ufs_close() and ufs_getattr() by the calls to
1529 		 * ufs_itimes_locked(), without subsequent UFS_UPDATE().
1530 		 * Test also all the other timestamp flags too, to pick up
1531 		 * any other cases that could be missed.
1532 		 */
1533 		if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0) {
1534 			VI_UNLOCK(vp);
1535 			continue;
1536 		}
1537 		if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
1538 		    td)) != 0)
1539 			continue;
1540 #ifdef QUOTA
1541 		qsyncvp(vp);
1542 #endif
1543 		if (sync_doupdate(ip))
1544 			error = ffs_update(vp, 0);
1545 		if (error != 0)
1546 			allerror = error;
1547 		vput(vp);
1548 	}
1549 sbupdate:
1550 	if (VFSTOUFS(mp)->um_fs->fs_fmod != 0 &&
1551 	    (error = ffs_sbupdate(VFSTOUFS(mp), MNT_LAZY, 0)) != 0)
1552 		allerror = error;
1553 	return (allerror);
1554 }
1555 
1556 /*
1557  * Go through the disk queues to initiate sandbagged IO;
1558  * go through the inodes to write those that have been modified;
1559  * initiate the writing of the super block if it has been modified.
1560  *
1561  * Note: we are always called with the filesystem marked busy using
1562  * vfs_busy().
1563  */
1564 static int
1565 ffs_sync(mp, waitfor)
1566 	struct mount *mp;
1567 	int waitfor;
1568 {
1569 	struct vnode *mvp, *vp, *devvp;
1570 	struct thread *td;
1571 	struct inode *ip;
1572 	struct ufsmount *ump = VFSTOUFS(mp);
1573 	struct fs *fs;
1574 	int error, count, lockreq, allerror = 0;
1575 	int suspend;
1576 	int suspended;
1577 	int secondary_writes;
1578 	int secondary_accwrites;
1579 	int softdep_deps;
1580 	int softdep_accdeps;
1581 	struct bufobj *bo;
1582 
1583 	suspend = 0;
1584 	suspended = 0;
1585 	td = curthread;
1586 	fs = ump->um_fs;
1587 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0 && ump->um_fsckpid == 0)
1588 		panic("%s: ffs_sync: modification on read-only filesystem",
1589 		    fs->fs_fsmnt);
1590 	if (waitfor == MNT_LAZY) {
1591 		if (!rebooting)
1592 			return (ffs_sync_lazy(mp));
1593 		waitfor = MNT_NOWAIT;
1594 	}
1595 
1596 	/*
1597 	 * Write back each (modified) inode.
1598 	 */
1599 	lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1600 	if (waitfor == MNT_SUSPEND) {
1601 		suspend = 1;
1602 		waitfor = MNT_WAIT;
1603 	}
1604 	if (waitfor == MNT_WAIT)
1605 		lockreq = LK_EXCLUSIVE;
1606 	lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1607 loop:
1608 	/* Grab snapshot of secondary write counts */
1609 	MNT_ILOCK(mp);
1610 	secondary_writes = mp->mnt_secondary_writes;
1611 	secondary_accwrites = mp->mnt_secondary_accwrites;
1612 	MNT_IUNLOCK(mp);
1613 
1614 	/* Grab snapshot of softdep dependency counts */
1615 	softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1616 
1617 	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1618 		/*
1619 		 * Depend on the vnode interlock to keep things stable enough
1620 		 * for a quick test.  Since there might be hundreds of
1621 		 * thousands of vnodes, we cannot afford even a subroutine
1622 		 * call unless there's a good chance that we have work to do.
1623 		 */
1624 		if (vp->v_type == VNON) {
1625 			VI_UNLOCK(vp);
1626 			continue;
1627 		}
1628 		ip = VTOI(vp);
1629 		if ((ip->i_flag &
1630 		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1631 		    vp->v_bufobj.bo_dirty.bv_cnt == 0) {
1632 			VI_UNLOCK(vp);
1633 			continue;
1634 		}
1635 		if ((error = vget(vp, lockreq, td)) != 0) {
1636 			if (error == ENOENT || error == ENOLCK) {
1637 				MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1638 				goto loop;
1639 			}
1640 			continue;
1641 		}
1642 #ifdef QUOTA
1643 		qsyncvp(vp);
1644 #endif
1645 		if ((error = ffs_syncvnode(vp, waitfor, 0)) != 0)
1646 			allerror = error;
1647 		vput(vp);
1648 	}
1649 	/*
1650 	 * Force stale filesystem control information to be flushed.
1651 	 */
1652 	if (waitfor == MNT_WAIT || rebooting) {
1653 		if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1654 			allerror = error;
1655 		if (ffs_fsfail_cleanup(ump, allerror))
1656 			allerror = 0;
1657 		/* Flushed work items may create new vnodes to clean */
1658 		if (allerror == 0 && count)
1659 			goto loop;
1660 	}
1661 
1662 	devvp = ump->um_devvp;
1663 	bo = &devvp->v_bufobj;
1664 	BO_LOCK(bo);
1665 	if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) {
1666 		BO_UNLOCK(bo);
1667 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1668 		error = VOP_FSYNC(devvp, waitfor, td);
1669 		VOP_UNLOCK(devvp);
1670 		if (MOUNTEDSOFTDEP(mp) && (error == 0 || error == EAGAIN))
1671 			error = ffs_sbupdate(ump, waitfor, 0);
1672 		if (error != 0)
1673 			allerror = error;
1674 		if (ffs_fsfail_cleanup(ump, allerror))
1675 			allerror = 0;
1676 		if (allerror == 0 && waitfor == MNT_WAIT)
1677 			goto loop;
1678 	} else if (suspend != 0) {
1679 		if (softdep_check_suspend(mp,
1680 					  devvp,
1681 					  softdep_deps,
1682 					  softdep_accdeps,
1683 					  secondary_writes,
1684 					  secondary_accwrites) != 0) {
1685 			MNT_IUNLOCK(mp);
1686 			goto loop;	/* More work needed */
1687 		}
1688 		mtx_assert(MNT_MTX(mp), MA_OWNED);
1689 		mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1690 		MNT_IUNLOCK(mp);
1691 		suspended = 1;
1692 	} else
1693 		BO_UNLOCK(bo);
1694 	/*
1695 	 * Write back modified superblock.
1696 	 */
1697 	if (fs->fs_fmod != 0 &&
1698 	    (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1699 		allerror = error;
1700 	if (ffs_fsfail_cleanup(ump, allerror))
1701 		allerror = 0;
1702 	return (allerror);
1703 }
1704 
1705 int
1706 ffs_vget(mp, ino, flags, vpp)
1707 	struct mount *mp;
1708 	ino_t ino;
1709 	int flags;
1710 	struct vnode **vpp;
1711 {
1712 	return (ffs_vgetf(mp, ino, flags, vpp, 0));
1713 }
1714 
1715 int
1716 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1717 	struct mount *mp;
1718 	ino_t ino;
1719 	int flags;
1720 	struct vnode **vpp;
1721 	int ffs_flags;
1722 {
1723 	struct fs *fs;
1724 	struct inode *ip;
1725 	struct ufsmount *ump;
1726 	struct buf *bp;
1727 	struct vnode *vp;
1728 	daddr_t dbn;
1729 	int error;
1730 
1731 	MPASS((ffs_flags & FFSV_REPLACE) == 0 || (flags & LK_EXCLUSIVE) != 0);
1732 
1733 	error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1734 	if (error != 0)
1735 		return (error);
1736 	if (*vpp != NULL) {
1737 		if ((ffs_flags & FFSV_REPLACE) == 0)
1738 			return (0);
1739 		vgone(*vpp);
1740 		vput(*vpp);
1741 	}
1742 
1743 	/*
1744 	 * We must promote to an exclusive lock for vnode creation.  This
1745 	 * can happen if lookup is passed LOCKSHARED.
1746 	 */
1747 	if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1748 		flags &= ~LK_TYPE_MASK;
1749 		flags |= LK_EXCLUSIVE;
1750 	}
1751 
1752 	/*
1753 	 * We do not lock vnode creation as it is believed to be too
1754 	 * expensive for such rare case as simultaneous creation of vnode
1755 	 * for same ino by different processes. We just allow them to race
1756 	 * and check later to decide who wins. Let the race begin!
1757 	 */
1758 
1759 	ump = VFSTOUFS(mp);
1760 	fs = ump->um_fs;
1761 	ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1762 
1763 	/* Allocate a new vnode/inode. */
1764 	error = getnewvnode("ufs", mp, fs->fs_magic == FS_UFS1_MAGIC ?
1765 	    &ffs_vnodeops1 : &ffs_vnodeops2, &vp);
1766 	if (error) {
1767 		*vpp = NULL;
1768 		uma_zfree(uma_inode, ip);
1769 		return (error);
1770 	}
1771 	/*
1772 	 * FFS supports recursive locking.
1773 	 */
1774 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1775 	VN_LOCK_AREC(vp);
1776 	vp->v_data = ip;
1777 	vp->v_bufobj.bo_bsize = fs->fs_bsize;
1778 	ip->i_vnode = vp;
1779 	ip->i_ump = ump;
1780 	ip->i_number = ino;
1781 	ip->i_ea_refs = 0;
1782 	ip->i_nextclustercg = -1;
1783 	ip->i_flag = fs->fs_magic == FS_UFS1_MAGIC ? 0 : IN_UFS2;
1784 	ip->i_mode = 0; /* ensure error cases below throw away vnode */
1785 #ifdef QUOTA
1786 	{
1787 		int i;
1788 		for (i = 0; i < MAXQUOTAS; i++)
1789 			ip->i_dquot[i] = NODQUOT;
1790 	}
1791 #endif
1792 
1793 	if (ffs_flags & FFSV_FORCEINSMQ)
1794 		vp->v_vflag |= VV_FORCEINSMQ;
1795 	error = insmntque(vp, mp);
1796 	if (error != 0) {
1797 		uma_zfree(uma_inode, ip);
1798 		*vpp = NULL;
1799 		return (error);
1800 	}
1801 	vp->v_vflag &= ~VV_FORCEINSMQ;
1802 	error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1803 	if (error != 0)
1804 		return (error);
1805 	if (*vpp != NULL) {
1806 		/*
1807 		 * Calls from ffs_valloc() (i.e. FFSV_REPLACE set)
1808 		 * operate on empty inode, which must not be found by
1809 		 * other threads until fully filled.  Vnode for empty
1810 		 * inode must be not re-inserted on the hash by other
1811 		 * thread, after removal by us at the beginning.
1812 		 */
1813 		MPASS((ffs_flags & FFSV_REPLACE) == 0);
1814 		return (0);
1815 	}
1816 
1817 	/* Read in the disk contents for the inode, copy into the inode. */
1818 	dbn = fsbtodb(fs, ino_to_fsba(fs, ino));
1819 	error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, (int)fs->fs_bsize,
1820 	    NULL, NULL, 0, NOCRED, 0, NULL, &bp);
1821 	if (error != 0) {
1822 		/*
1823 		 * The inode does not contain anything useful, so it would
1824 		 * be misleading to leave it on its hash chain. With mode
1825 		 * still zero, it will be unlinked and returned to the free
1826 		 * list by vput().
1827 		 */
1828 		vgone(vp);
1829 		vput(vp);
1830 		*vpp = NULL;
1831 		return (error);
1832 	}
1833 	if (I_IS_UFS1(ip))
1834 		ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1835 	else
1836 		ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1837 	if ((error = ffs_load_inode(bp, ip, fs, ino)) != 0) {
1838 		bqrelse(bp);
1839 		vgone(vp);
1840 		vput(vp);
1841 		*vpp = NULL;
1842 		return (error);
1843 	}
1844 	if (DOINGSOFTDEP(vp))
1845 		softdep_load_inodeblock(ip);
1846 	else
1847 		ip->i_effnlink = ip->i_nlink;
1848 	bqrelse(bp);
1849 
1850 	/*
1851 	 * Initialize the vnode from the inode, check for aliases.
1852 	 * Note that the underlying vnode may have changed.
1853 	 */
1854 	error = ufs_vinit(mp, I_IS_UFS1(ip) ? &ffs_fifoops1 : &ffs_fifoops2,
1855 	    &vp);
1856 	if (error) {
1857 		vgone(vp);
1858 		vput(vp);
1859 		*vpp = NULL;
1860 		return (error);
1861 	}
1862 
1863 	/*
1864 	 * Finish inode initialization.
1865 	 */
1866 	if (vp->v_type != VFIFO) {
1867 		/* FFS supports shared locking for all files except fifos. */
1868 		VN_LOCK_ASHARE(vp);
1869 	}
1870 
1871 	/*
1872 	 * Set up a generation number for this inode if it does not
1873 	 * already have one. This should only happen on old filesystems.
1874 	 */
1875 	if (ip->i_gen == 0) {
1876 		while (ip->i_gen == 0)
1877 			ip->i_gen = arc4random();
1878 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1879 			UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
1880 			DIP_SET(ip, i_gen, ip->i_gen);
1881 		}
1882 	}
1883 #ifdef MAC
1884 	if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1885 		/*
1886 		 * If this vnode is already allocated, and we're running
1887 		 * multi-label, attempt to perform a label association
1888 		 * from the extended attributes on the inode.
1889 		 */
1890 		error = mac_vnode_associate_extattr(mp, vp);
1891 		if (error) {
1892 			/* ufs_inactive will release ip->i_devvp ref. */
1893 			vgone(vp);
1894 			vput(vp);
1895 			*vpp = NULL;
1896 			return (error);
1897 		}
1898 	}
1899 #endif
1900 
1901 	*vpp = vp;
1902 	return (0);
1903 }
1904 
1905 /*
1906  * File handle to vnode
1907  *
1908  * Have to be really careful about stale file handles:
1909  * - check that the inode number is valid
1910  * - for UFS2 check that the inode number is initialized
1911  * - call ffs_vget() to get the locked inode
1912  * - check for an unallocated inode (i_mode == 0)
1913  * - check that the given client host has export rights and return
1914  *   those rights via. exflagsp and credanonp
1915  */
1916 static int
1917 ffs_fhtovp(mp, fhp, flags, vpp)
1918 	struct mount *mp;
1919 	struct fid *fhp;
1920 	int flags;
1921 	struct vnode **vpp;
1922 {
1923 	struct ufid *ufhp;
1924 	struct ufsmount *ump;
1925 	struct fs *fs;
1926 	struct cg *cgp;
1927 	struct buf *bp;
1928 	ino_t ino;
1929 	u_int cg;
1930 	int error;
1931 
1932 	ufhp = (struct ufid *)fhp;
1933 	ino = ufhp->ufid_ino;
1934 	ump = VFSTOUFS(mp);
1935 	fs = ump->um_fs;
1936 	if (ino < UFS_ROOTINO || ino >= fs->fs_ncg * fs->fs_ipg)
1937 		return (ESTALE);
1938 	/*
1939 	 * Need to check if inode is initialized because UFS2 does lazy
1940 	 * initialization and nfs_fhtovp can offer arbitrary inode numbers.
1941 	 */
1942 	if (fs->fs_magic != FS_UFS2_MAGIC)
1943 		return (ufs_fhtovp(mp, ufhp, flags, vpp));
1944 	cg = ino_to_cg(fs, ino);
1945 	if ((error = ffs_getcg(fs, ump->um_devvp, cg, 0, &bp, &cgp)) != 0)
1946 		return (error);
1947 	if (ino >= cg * fs->fs_ipg + cgp->cg_initediblk) {
1948 		brelse(bp);
1949 		return (ESTALE);
1950 	}
1951 	brelse(bp);
1952 	return (ufs_fhtovp(mp, ufhp, flags, vpp));
1953 }
1954 
1955 /*
1956  * Initialize the filesystem.
1957  */
1958 static int
1959 ffs_init(vfsp)
1960 	struct vfsconf *vfsp;
1961 {
1962 
1963 	ffs_susp_initialize();
1964 	softdep_initialize();
1965 	return (ufs_init(vfsp));
1966 }
1967 
1968 /*
1969  * Undo the work of ffs_init().
1970  */
1971 static int
1972 ffs_uninit(vfsp)
1973 	struct vfsconf *vfsp;
1974 {
1975 	int ret;
1976 
1977 	ret = ufs_uninit(vfsp);
1978 	softdep_uninitialize();
1979 	ffs_susp_uninitialize();
1980 	taskqueue_drain_all(taskqueue_thread);
1981 	return (ret);
1982 }
1983 
1984 /*
1985  * Structure used to pass information from ffs_sbupdate to its
1986  * helper routine ffs_use_bwrite.
1987  */
1988 struct devfd {
1989 	struct ufsmount	*ump;
1990 	struct buf	*sbbp;
1991 	int		 waitfor;
1992 	int		 suspended;
1993 	int		 error;
1994 };
1995 
1996 /*
1997  * Write a superblock and associated information back to disk.
1998  */
1999 int
2000 ffs_sbupdate(ump, waitfor, suspended)
2001 	struct ufsmount *ump;
2002 	int waitfor;
2003 	int suspended;
2004 {
2005 	struct fs *fs;
2006 	struct buf *sbbp;
2007 	struct devfd devfd;
2008 
2009 	fs = ump->um_fs;
2010 	if (fs->fs_ronly == 1 &&
2011 	    (ump->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
2012 	    (MNT_RDONLY | MNT_UPDATE) && ump->um_fsckpid == 0)
2013 		panic("ffs_sbupdate: write read-only filesystem");
2014 	/*
2015 	 * We use the superblock's buf to serialize calls to ffs_sbupdate().
2016 	 */
2017 	sbbp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
2018 	    (int)fs->fs_sbsize, 0, 0, 0);
2019 	/*
2020 	 * Initialize info needed for write function.
2021 	 */
2022 	devfd.ump = ump;
2023 	devfd.sbbp = sbbp;
2024 	devfd.waitfor = waitfor;
2025 	devfd.suspended = suspended;
2026 	devfd.error = 0;
2027 	return (ffs_sbput(&devfd, fs, fs->fs_sblockloc, ffs_use_bwrite));
2028 }
2029 
2030 /*
2031  * Write function for use by filesystem-layer routines.
2032  */
2033 static int
2034 ffs_use_bwrite(void *devfd, off_t loc, void *buf, int size)
2035 {
2036 	struct devfd *devfdp;
2037 	struct ufsmount *ump;
2038 	struct buf *bp;
2039 	struct fs *fs;
2040 	int error;
2041 
2042 	devfdp = devfd;
2043 	ump = devfdp->ump;
2044 	fs = ump->um_fs;
2045 	/*
2046 	 * Writing the superblock summary information.
2047 	 */
2048 	if (loc != fs->fs_sblockloc) {
2049 		bp = getblk(ump->um_devvp, btodb(loc), size, 0, 0, 0);
2050 		bcopy(buf, bp->b_data, (u_int)size);
2051 		if (devfdp->suspended)
2052 			bp->b_flags |= B_VALIDSUSPWRT;
2053 		if (devfdp->waitfor != MNT_WAIT)
2054 			bawrite(bp);
2055 		else if ((error = bwrite(bp)) != 0)
2056 			devfdp->error = error;
2057 		return (0);
2058 	}
2059 	/*
2060 	 * Writing the superblock itself. We need to do special checks for it.
2061 	 */
2062 	bp = devfdp->sbbp;
2063 	if (ffs_fsfail_cleanup(ump, devfdp->error))
2064 		devfdp->error = 0;
2065 	if (devfdp->error != 0) {
2066 		brelse(bp);
2067 		return (devfdp->error);
2068 	}
2069 	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
2070 	    (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
2071 		printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
2072 		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
2073 		fs->fs_sblockloc = SBLOCK_UFS1;
2074 	}
2075 	if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
2076 	    (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
2077 		printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
2078 		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
2079 		fs->fs_sblockloc = SBLOCK_UFS2;
2080 	}
2081 	if (MOUNTEDSOFTDEP(ump->um_mountp))
2082 		softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, bp);
2083 	bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
2084 	fs = (struct fs *)bp->b_data;
2085 	ffs_oldfscompat_write(fs, ump);
2086 	/*
2087 	 * Because we may have made changes to the superblock, we need to
2088 	 * recompute its check-hash.
2089 	 */
2090 	fs->fs_ckhash = ffs_calc_sbhash(fs);
2091 	if (devfdp->suspended)
2092 		bp->b_flags |= B_VALIDSUSPWRT;
2093 	if (devfdp->waitfor != MNT_WAIT)
2094 		bawrite(bp);
2095 	else if ((error = bwrite(bp)) != 0)
2096 		devfdp->error = error;
2097 	return (devfdp->error);
2098 }
2099 
2100 static int
2101 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
2102 	int attrnamespace, const char *attrname)
2103 {
2104 
2105 #ifdef UFS_EXTATTR
2106 	return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
2107 	    attrname));
2108 #else
2109 	return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
2110 	    attrname));
2111 #endif
2112 }
2113 
2114 static void
2115 ffs_ifree(struct ufsmount *ump, struct inode *ip)
2116 {
2117 
2118 	if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
2119 		uma_zfree(uma_ufs1, ip->i_din1);
2120 	else if (ip->i_din2 != NULL)
2121 		uma_zfree(uma_ufs2, ip->i_din2);
2122 	uma_zfree(uma_inode, ip);
2123 }
2124 
2125 static int dobkgrdwrite = 1;
2126 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
2127     "Do background writes (honoring the BV_BKGRDWRITE flag)?");
2128 
2129 /*
2130  * Complete a background write started from bwrite.
2131  */
2132 static void
2133 ffs_backgroundwritedone(struct buf *bp)
2134 {
2135 	struct bufobj *bufobj;
2136 	struct buf *origbp;
2137 
2138 #ifdef SOFTUPDATES
2139 	if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) != 0)
2140 		softdep_handle_error(bp);
2141 #endif
2142 
2143 	/*
2144 	 * Find the original buffer that we are writing.
2145 	 */
2146 	bufobj = bp->b_bufobj;
2147 	BO_LOCK(bufobj);
2148 	if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
2149 		panic("backgroundwritedone: lost buffer");
2150 
2151 	/*
2152 	 * We should mark the cylinder group buffer origbp as
2153 	 * dirty, to not lose the failed write.
2154 	 */
2155 	if ((bp->b_ioflags & BIO_ERROR) != 0)
2156 		origbp->b_vflags |= BV_BKGRDERR;
2157 	BO_UNLOCK(bufobj);
2158 	/*
2159 	 * Process dependencies then return any unfinished ones.
2160 	 */
2161 	if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) == 0)
2162 		buf_complete(bp);
2163 #ifdef SOFTUPDATES
2164 	if (!LIST_EMPTY(&bp->b_dep))
2165 		softdep_move_dependencies(bp, origbp);
2166 #endif
2167 	/*
2168 	 * This buffer is marked B_NOCACHE so when it is released
2169 	 * by biodone it will be tossed.
2170 	 */
2171 	bp->b_flags |= B_NOCACHE;
2172 	bp->b_flags &= ~B_CACHE;
2173 	pbrelvp(bp);
2174 
2175 	/*
2176 	 * Prevent brelse() from trying to keep and re-dirtying bp on
2177 	 * errors. It causes b_bufobj dereference in
2178 	 * bdirty()/reassignbuf(), and b_bufobj was cleared in
2179 	 * pbrelvp() above.
2180 	 */
2181 	if ((bp->b_ioflags & BIO_ERROR) != 0)
2182 		bp->b_flags |= B_INVAL;
2183 	bufdone(bp);
2184 	BO_LOCK(bufobj);
2185 	/*
2186 	 * Clear the BV_BKGRDINPROG flag in the original buffer
2187 	 * and awaken it if it is waiting for the write to complete.
2188 	 * If BV_BKGRDINPROG is not set in the original buffer it must
2189 	 * have been released and re-instantiated - which is not legal.
2190 	 */
2191 	KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
2192 	    ("backgroundwritedone: lost buffer2"));
2193 	origbp->b_vflags &= ~BV_BKGRDINPROG;
2194 	if (origbp->b_vflags & BV_BKGRDWAIT) {
2195 		origbp->b_vflags &= ~BV_BKGRDWAIT;
2196 		wakeup(&origbp->b_xflags);
2197 	}
2198 	BO_UNLOCK(bufobj);
2199 }
2200 
2201 
2202 /*
2203  * Write, release buffer on completion.  (Done by iodone
2204  * if async).  Do not bother writing anything if the buffer
2205  * is invalid.
2206  *
2207  * Note that we set B_CACHE here, indicating that buffer is
2208  * fully valid and thus cacheable.  This is true even of NFS
2209  * now so we set it generally.  This could be set either here
2210  * or in biodone() since the I/O is synchronous.  We put it
2211  * here.
2212  */
2213 static int
2214 ffs_bufwrite(struct buf *bp)
2215 {
2216 	struct buf *newbp;
2217 	struct cg *cgp;
2218 
2219 	CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2220 	if (bp->b_flags & B_INVAL) {
2221 		brelse(bp);
2222 		return (0);
2223 	}
2224 
2225 	if (!BUF_ISLOCKED(bp))
2226 		panic("bufwrite: buffer is not busy???");
2227 	/*
2228 	 * If a background write is already in progress, delay
2229 	 * writing this block if it is asynchronous. Otherwise
2230 	 * wait for the background write to complete.
2231 	 */
2232 	BO_LOCK(bp->b_bufobj);
2233 	if (bp->b_vflags & BV_BKGRDINPROG) {
2234 		if (bp->b_flags & B_ASYNC) {
2235 			BO_UNLOCK(bp->b_bufobj);
2236 			bdwrite(bp);
2237 			return (0);
2238 		}
2239 		bp->b_vflags |= BV_BKGRDWAIT;
2240 		msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), PRIBIO,
2241 		    "bwrbg", 0);
2242 		if (bp->b_vflags & BV_BKGRDINPROG)
2243 			panic("bufwrite: still writing");
2244 	}
2245 	bp->b_vflags &= ~BV_BKGRDERR;
2246 	BO_UNLOCK(bp->b_bufobj);
2247 
2248 	/*
2249 	 * If this buffer is marked for background writing and we
2250 	 * do not have to wait for it, make a copy and write the
2251 	 * copy so as to leave this buffer ready for further use.
2252 	 *
2253 	 * This optimization eats a lot of memory.  If we have a page
2254 	 * or buffer shortfall we can't do it.
2255 	 */
2256 	if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
2257 	    (bp->b_flags & B_ASYNC) &&
2258 	    !vm_page_count_severe() &&
2259 	    !buf_dirty_count_severe()) {
2260 		KASSERT(bp->b_iodone == NULL,
2261 		    ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
2262 
2263 		/* get a new block */
2264 		newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
2265 		if (newbp == NULL)
2266 			goto normal_write;
2267 
2268 		KASSERT(buf_mapped(bp), ("Unmapped cg"));
2269 		memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
2270 		BO_LOCK(bp->b_bufobj);
2271 		bp->b_vflags |= BV_BKGRDINPROG;
2272 		BO_UNLOCK(bp->b_bufobj);
2273 		newbp->b_xflags |=
2274 		    (bp->b_xflags & BX_FSPRIV) | BX_BKGRDMARKER;
2275 		newbp->b_lblkno = bp->b_lblkno;
2276 		newbp->b_blkno = bp->b_blkno;
2277 		newbp->b_offset = bp->b_offset;
2278 		newbp->b_iodone = ffs_backgroundwritedone;
2279 		newbp->b_flags |= B_ASYNC;
2280 		newbp->b_flags &= ~B_INVAL;
2281 		pbgetvp(bp->b_vp, newbp);
2282 
2283 #ifdef SOFTUPDATES
2284 		/*
2285 		 * Move over the dependencies.  If there are rollbacks,
2286 		 * leave the parent buffer dirtied as it will need to
2287 		 * be written again.
2288 		 */
2289 		if (LIST_EMPTY(&bp->b_dep) ||
2290 		    softdep_move_dependencies(bp, newbp) == 0)
2291 			bundirty(bp);
2292 #else
2293 		bundirty(bp);
2294 #endif
2295 
2296 		/*
2297 		 * Initiate write on the copy, release the original.  The
2298 		 * BKGRDINPROG flag prevents it from going away until
2299 		 * the background write completes. We have to recalculate
2300 		 * its check hash in case the buffer gets freed and then
2301 		 * reconstituted from the buffer cache during a later read.
2302 		 */
2303 		if ((bp->b_xflags & BX_CYLGRP) != 0) {
2304 			cgp = (struct cg *)bp->b_data;
2305 			cgp->cg_ckhash = 0;
2306 			cgp->cg_ckhash =
2307 			    calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2308 		}
2309 		bqrelse(bp);
2310 		bp = newbp;
2311 	} else
2312 		/* Mark the buffer clean */
2313 		bundirty(bp);
2314 
2315 
2316 	/* Let the normal bufwrite do the rest for us */
2317 normal_write:
2318 	/*
2319 	 * If we are writing a cylinder group, update its time.
2320 	 */
2321 	if ((bp->b_xflags & BX_CYLGRP) != 0) {
2322 		cgp = (struct cg *)bp->b_data;
2323 		cgp->cg_old_time = cgp->cg_time = time_second;
2324 	}
2325 	return (bufwrite(bp));
2326 }
2327 
2328 
2329 static void
2330 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
2331 {
2332 	struct vnode *vp;
2333 	struct buf *tbp;
2334 	int error, nocopy;
2335 
2336 	/*
2337 	 * This is the bufobj strategy for the private VCHR vnodes
2338 	 * used by FFS to access the underlying storage device.
2339 	 * We override the default bufobj strategy and thus bypass
2340 	 * VOP_STRATEGY() for these vnodes.
2341 	 */
2342 	vp = bo2vnode(bo);
2343 	KASSERT(bp->b_vp == NULL || bp->b_vp->v_type != VCHR ||
2344 	    bp->b_vp->v_rdev == NULL ||
2345 	    bp->b_vp->v_rdev->si_mountpt == NULL ||
2346 	    VFSTOUFS(bp->b_vp->v_rdev->si_mountpt) == NULL ||
2347 	    vp == VFSTOUFS(bp->b_vp->v_rdev->si_mountpt)->um_devvp,
2348 	    ("ffs_geom_strategy() with wrong vp"));
2349 	if (bp->b_iocmd == BIO_WRITE) {
2350 		if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
2351 		    bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
2352 		    (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
2353 			panic("ffs_geom_strategy: bad I/O");
2354 		nocopy = bp->b_flags & B_NOCOPY;
2355 		bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY);
2356 		if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 &&
2357 		    vp->v_rdev->si_snapdata != NULL) {
2358 			if ((bp->b_flags & B_CLUSTER) != 0) {
2359 				runningbufwakeup(bp);
2360 				TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2361 					      b_cluster.cluster_entry) {
2362 					error = ffs_copyonwrite(vp, tbp);
2363 					if (error != 0 &&
2364 					    error != EOPNOTSUPP) {
2365 						bp->b_error = error;
2366 						bp->b_ioflags |= BIO_ERROR;
2367 						bufdone(bp);
2368 						return;
2369 					}
2370 				}
2371 				bp->b_runningbufspace = bp->b_bufsize;
2372 				atomic_add_long(&runningbufspace,
2373 					       bp->b_runningbufspace);
2374 			} else {
2375 				error = ffs_copyonwrite(vp, bp);
2376 				if (error != 0 && error != EOPNOTSUPP) {
2377 					bp->b_error = error;
2378 					bp->b_ioflags |= BIO_ERROR;
2379 					bufdone(bp);
2380 					return;
2381 				}
2382 			}
2383 		}
2384 #ifdef SOFTUPDATES
2385 		if ((bp->b_flags & B_CLUSTER) != 0) {
2386 			TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2387 				      b_cluster.cluster_entry) {
2388 				if (!LIST_EMPTY(&tbp->b_dep))
2389 					buf_start(tbp);
2390 			}
2391 		} else {
2392 			if (!LIST_EMPTY(&bp->b_dep))
2393 				buf_start(bp);
2394 		}
2395 
2396 #endif
2397 		/*
2398 		 * Check for metadata that needs check-hashes and update them.
2399 		 */
2400 		switch (bp->b_xflags & BX_FSPRIV) {
2401 		case BX_CYLGRP:
2402 			((struct cg *)bp->b_data)->cg_ckhash = 0;
2403 			((struct cg *)bp->b_data)->cg_ckhash =
2404 			    calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2405 			break;
2406 
2407 		case BX_SUPERBLOCK:
2408 		case BX_INODE:
2409 		case BX_INDIR:
2410 		case BX_DIR:
2411 			printf("Check-hash write is unimplemented!!!\n");
2412 			break;
2413 
2414 		case 0:
2415 			break;
2416 
2417 		default:
2418 			printf("multiple buffer types 0x%b\n",
2419 			    (u_int)(bp->b_xflags & BX_FSPRIV),
2420 			    PRINT_UFS_BUF_XFLAGS);
2421 			break;
2422 		}
2423 	}
2424 	if (bp->b_iocmd != BIO_READ && ffs_enxio_enable)
2425 		bp->b_xflags |= BX_CVTENXIO;
2426 	g_vfs_strategy(bo, bp);
2427 }
2428 
2429 int
2430 ffs_own_mount(const struct mount *mp)
2431 {
2432 
2433 	if (mp->mnt_op == &ufs_vfsops)
2434 		return (1);
2435 	return (0);
2436 }
2437 
2438 #ifdef	DDB
2439 #ifdef SOFTUPDATES
2440 
2441 /* defined in ffs_softdep.c */
2442 extern void db_print_ffs(struct ufsmount *ump);
2443 
2444 DB_SHOW_COMMAND(ffs, db_show_ffs)
2445 {
2446 	struct mount *mp;
2447 	struct ufsmount *ump;
2448 
2449 	if (have_addr) {
2450 		ump = VFSTOUFS((struct mount *)addr);
2451 		db_print_ffs(ump);
2452 		return;
2453 	}
2454 
2455 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2456 		if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2457 			db_print_ffs(VFSTOUFS(mp));
2458 	}
2459 }
2460 
2461 #endif	/* SOFTUPDATES */
2462 #endif	/* DDB */
2463