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