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