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