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