xref: /freebsd/sys/ufs/ffs/ffs_vfsops.c (revision dbe6d91e5c0cea70d7260efa41cea9e8398aa17f)
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 	return (ffs_vgetf(mp, ino, flags, vpp, 0));
1300 }
1301 
1302 int
1303 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1304 	struct mount *mp;
1305 	ino_t ino;
1306 	int flags;
1307 	struct vnode **vpp;
1308 	int ffs_flags;
1309 {
1310 	struct fs *fs;
1311 	struct inode *ip;
1312 	struct ufsmount *ump;
1313 	struct buf *bp;
1314 	struct vnode *vp;
1315 	struct cdev *dev;
1316 	int error;
1317 
1318 	error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1319 	if (error || *vpp != NULL)
1320 		return (error);
1321 
1322 	/*
1323 	 * We must promote to an exclusive lock for vnode creation.  This
1324 	 * can happen if lookup is passed LOCKSHARED.
1325  	 */
1326 	if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1327 		flags &= ~LK_TYPE_MASK;
1328 		flags |= LK_EXCLUSIVE;
1329 	}
1330 
1331 	/*
1332 	 * We do not lock vnode creation as it is believed to be too
1333 	 * expensive for such rare case as simultaneous creation of vnode
1334 	 * for same ino by different processes. We just allow them to race
1335 	 * and check later to decide who wins. Let the race begin!
1336 	 */
1337 
1338 	ump = VFSTOUFS(mp);
1339 	dev = ump->um_dev;
1340 	fs = ump->um_fs;
1341 
1342 	/*
1343 	 * If this MALLOC() is performed after the getnewvnode()
1344 	 * it might block, leaving a vnode with a NULL v_data to be
1345 	 * found by ffs_sync() if a sync happens to fire right then,
1346 	 * which will cause a panic because ffs_sync() blindly
1347 	 * dereferences vp->v_data (as well it should).
1348 	 */
1349 	ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1350 
1351 	/* Allocate a new vnode/inode. */
1352 	if (fs->fs_magic == FS_UFS1_MAGIC)
1353 		error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp);
1354 	else
1355 		error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp);
1356 	if (error) {
1357 		*vpp = NULL;
1358 		uma_zfree(uma_inode, ip);
1359 		return (error);
1360 	}
1361 	/*
1362 	 * FFS supports recursive and shared locking.
1363 	 */
1364 	VN_LOCK_AREC(vp);
1365 	VN_LOCK_ASHARE(vp);
1366 	vp->v_data = ip;
1367 	vp->v_bufobj.bo_bsize = fs->fs_bsize;
1368 	ip->i_vnode = vp;
1369 	ip->i_ump = ump;
1370 	ip->i_fs = fs;
1371 	ip->i_dev = dev;
1372 	ip->i_number = ino;
1373 #ifdef QUOTA
1374 	{
1375 		int i;
1376 		for (i = 0; i < MAXQUOTAS; i++)
1377 			ip->i_dquot[i] = NODQUOT;
1378 	}
1379 #endif
1380 
1381 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1382 	if (ffs_flags & FFSV_FORCEINSMQ)
1383 		vp->v_vflag |= VV_FORCEINSMQ;
1384 	error = insmntque(vp, mp);
1385 	if (error != 0) {
1386 		uma_zfree(uma_inode, ip);
1387 		*vpp = NULL;
1388 		return (error);
1389 	}
1390 	vp->v_vflag &= ~VV_FORCEINSMQ;
1391 	error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1392 	if (error || *vpp != NULL)
1393 		return (error);
1394 
1395 	/* Read in the disk contents for the inode, copy into the inode. */
1396 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1397 	    (int)fs->fs_bsize, NOCRED, &bp);
1398 	if (error) {
1399 		/*
1400 		 * The inode does not contain anything useful, so it would
1401 		 * be misleading to leave it on its hash chain. With mode
1402 		 * still zero, it will be unlinked and returned to the free
1403 		 * list by vput().
1404 		 */
1405 		brelse(bp);
1406 		vput(vp);
1407 		*vpp = NULL;
1408 		return (error);
1409 	}
1410 	if (ip->i_ump->um_fstype == UFS1)
1411 		ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1412 	else
1413 		ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1414 	ffs_load_inode(bp, ip, fs, ino);
1415 	if (DOINGSOFTDEP(vp))
1416 		softdep_load_inodeblock(ip);
1417 	else
1418 		ip->i_effnlink = ip->i_nlink;
1419 	bqrelse(bp);
1420 
1421 	/*
1422 	 * Initialize the vnode from the inode, check for aliases.
1423 	 * Note that the underlying vnode may have changed.
1424 	 */
1425 	if (ip->i_ump->um_fstype == UFS1)
1426 		error = ufs_vinit(mp, &ffs_fifoops1, &vp);
1427 	else
1428 		error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1429 	if (error) {
1430 		vput(vp);
1431 		*vpp = NULL;
1432 		return (error);
1433 	}
1434 
1435 	/*
1436 	 * Finish inode initialization.
1437 	 */
1438 
1439 	/*
1440 	 * Set up a generation number for this inode if it does not
1441 	 * already have one. This should only happen on old filesystems.
1442 	 */
1443 	if (ip->i_gen == 0) {
1444 		ip->i_gen = arc4random() / 2 + 1;
1445 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1446 			ip->i_flag |= IN_MODIFIED;
1447 			DIP_SET(ip, i_gen, ip->i_gen);
1448 		}
1449 	}
1450 	/*
1451 	 * Ensure that uid and gid are correct. This is a temporary
1452 	 * fix until fsck has been changed to do the update.
1453 	 */
1454 	if (fs->fs_magic == FS_UFS1_MAGIC &&		/* XXX */
1455 	    fs->fs_old_inodefmt < FS_44INODEFMT) {	/* XXX */
1456 		ip->i_uid = ip->i_din1->di_ouid;	/* XXX */
1457 		ip->i_gid = ip->i_din1->di_ogid;	/* XXX */
1458 	}						/* XXX */
1459 
1460 #ifdef MAC
1461 	if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1462 		/*
1463 		 * If this vnode is already allocated, and we're running
1464 		 * multi-label, attempt to perform a label association
1465 		 * from the extended attributes on the inode.
1466 		 */
1467 		error = mac_vnode_associate_extattr(mp, vp);
1468 		if (error) {
1469 			/* ufs_inactive will release ip->i_devvp ref. */
1470 			vput(vp);
1471 			*vpp = NULL;
1472 			return (error);
1473 		}
1474 	}
1475 #endif
1476 
1477 	*vpp = vp;
1478 	return (0);
1479 }
1480 
1481 /*
1482  * File handle to vnode
1483  *
1484  * Have to be really careful about stale file handles:
1485  * - check that the inode number is valid
1486  * - call ffs_vget() to get the locked inode
1487  * - check for an unallocated inode (i_mode == 0)
1488  * - check that the given client host has export rights and return
1489  *   those rights via. exflagsp and credanonp
1490  */
1491 static int
1492 ffs_fhtovp(mp, fhp, vpp)
1493 	struct mount *mp;
1494 	struct fid *fhp;
1495 	struct vnode **vpp;
1496 {
1497 	struct ufid *ufhp;
1498 	struct fs *fs;
1499 
1500 	ufhp = (struct ufid *)fhp;
1501 	fs = VFSTOUFS(mp)->um_fs;
1502 	if (ufhp->ufid_ino < ROOTINO ||
1503 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1504 		return (ESTALE);
1505 	return (ufs_fhtovp(mp, ufhp, vpp));
1506 }
1507 
1508 /*
1509  * Initialize the filesystem.
1510  */
1511 static int
1512 ffs_init(vfsp)
1513 	struct vfsconf *vfsp;
1514 {
1515 
1516 	softdep_initialize();
1517 	return (ufs_init(vfsp));
1518 }
1519 
1520 /*
1521  * Undo the work of ffs_init().
1522  */
1523 static int
1524 ffs_uninit(vfsp)
1525 	struct vfsconf *vfsp;
1526 {
1527 	int ret;
1528 
1529 	ret = ufs_uninit(vfsp);
1530 	softdep_uninitialize();
1531 	return (ret);
1532 }
1533 
1534 /*
1535  * Write a superblock and associated information back to disk.
1536  */
1537 int
1538 ffs_sbupdate(mp, waitfor, suspended)
1539 	struct ufsmount *mp;
1540 	int waitfor;
1541 	int suspended;
1542 {
1543 	struct fs *fs = mp->um_fs;
1544 	struct buf *sbbp;
1545 	struct buf *bp;
1546 	int blks;
1547 	void *space;
1548 	int i, size, error, allerror = 0;
1549 
1550 	if (fs->fs_ronly == 1 &&
1551 	    (mp->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1552 	    (MNT_RDONLY | MNT_UPDATE))
1553 		panic("ffs_sbupdate: write read-only filesystem");
1554 	/*
1555 	 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1556 	 */
1557 	sbbp = getblk(mp->um_devvp, btodb(fs->fs_sblockloc), (int)fs->fs_sbsize,
1558 	    0, 0, 0);
1559 	/*
1560 	 * First write back the summary information.
1561 	 */
1562 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
1563 	space = fs->fs_csp;
1564 	for (i = 0; i < blks; i += fs->fs_frag) {
1565 		size = fs->fs_bsize;
1566 		if (i + fs->fs_frag > blks)
1567 			size = (blks - i) * fs->fs_fsize;
1568 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1569 		    size, 0, 0, 0);
1570 		bcopy(space, bp->b_data, (u_int)size);
1571 		space = (char *)space + size;
1572 		if (suspended)
1573 			bp->b_flags |= B_VALIDSUSPWRT;
1574 		if (waitfor != MNT_WAIT)
1575 			bawrite(bp);
1576 		else if ((error = bwrite(bp)) != 0)
1577 			allerror = error;
1578 	}
1579 	/*
1580 	 * Now write back the superblock itself. If any errors occurred
1581 	 * up to this point, then fail so that the superblock avoids
1582 	 * being written out as clean.
1583 	 */
1584 	if (allerror) {
1585 		brelse(sbbp);
1586 		return (allerror);
1587 	}
1588 	bp = sbbp;
1589 	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1590 	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1591 		printf("%s: correcting fs_sblockloc from %jd to %d\n",
1592 		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1593 		fs->fs_sblockloc = SBLOCK_UFS1;
1594 	}
1595 	if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1596 	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1597 		printf("%s: correcting fs_sblockloc from %jd to %d\n",
1598 		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1599 		fs->fs_sblockloc = SBLOCK_UFS2;
1600 	}
1601 	fs->fs_fmod = 0;
1602 	fs->fs_time = time_second;
1603 	bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1604 	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1605 	if (suspended)
1606 		bp->b_flags |= B_VALIDSUSPWRT;
1607 	if (waitfor != MNT_WAIT)
1608 		bawrite(bp);
1609 	else if ((error = bwrite(bp)) != 0)
1610 		allerror = error;
1611 	return (allerror);
1612 }
1613 
1614 static int
1615 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1616 	int attrnamespace, const char *attrname, struct thread *td)
1617 {
1618 
1619 #ifdef UFS_EXTATTR
1620 	return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1621 	    attrname, td));
1622 #else
1623 	return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1624 	    attrname, td));
1625 #endif
1626 }
1627 
1628 static void
1629 ffs_ifree(struct ufsmount *ump, struct inode *ip)
1630 {
1631 
1632 	if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1633 		uma_zfree(uma_ufs1, ip->i_din1);
1634 	else if (ip->i_din2 != NULL)
1635 		uma_zfree(uma_ufs2, ip->i_din2);
1636 	uma_zfree(uma_inode, ip);
1637 }
1638 
1639 static int dobkgrdwrite = 1;
1640 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1641     "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1642 
1643 /*
1644  * Complete a background write started from bwrite.
1645  */
1646 static void
1647 ffs_backgroundwritedone(struct buf *bp)
1648 {
1649 	struct bufobj *bufobj;
1650 	struct buf *origbp;
1651 
1652 	/*
1653 	 * Find the original buffer that we are writing.
1654 	 */
1655 	bufobj = bp->b_bufobj;
1656 	BO_LOCK(bufobj);
1657 	if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
1658 		panic("backgroundwritedone: lost buffer");
1659 	/* Grab an extra reference to be dropped by the bufdone() below. */
1660 	bufobj_wrefl(bufobj);
1661 	BO_UNLOCK(bufobj);
1662 	/*
1663 	 * Process dependencies then return any unfinished ones.
1664 	 */
1665 	if (!LIST_EMPTY(&bp->b_dep))
1666 		buf_complete(bp);
1667 #ifdef SOFTUPDATES
1668 	if (!LIST_EMPTY(&bp->b_dep))
1669 		softdep_move_dependencies(bp, origbp);
1670 #endif
1671 	/*
1672 	 * This buffer is marked B_NOCACHE so when it is released
1673 	 * by biodone it will be tossed.
1674 	 */
1675 	bp->b_flags |= B_NOCACHE;
1676 	bp->b_flags &= ~B_CACHE;
1677 	bufdone(bp);
1678 	BO_LOCK(bufobj);
1679 	/*
1680 	 * Clear the BV_BKGRDINPROG flag in the original buffer
1681 	 * and awaken it if it is waiting for the write to complete.
1682 	 * If BV_BKGRDINPROG is not set in the original buffer it must
1683 	 * have been released and re-instantiated - which is not legal.
1684 	 */
1685 	KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
1686 	    ("backgroundwritedone: lost buffer2"));
1687 	origbp->b_vflags &= ~BV_BKGRDINPROG;
1688 	if (origbp->b_vflags & BV_BKGRDWAIT) {
1689 		origbp->b_vflags &= ~BV_BKGRDWAIT;
1690 		wakeup(&origbp->b_xflags);
1691 	}
1692 	BO_UNLOCK(bufobj);
1693 }
1694 
1695 
1696 /*
1697  * Write, release buffer on completion.  (Done by iodone
1698  * if async).  Do not bother writing anything if the buffer
1699  * is invalid.
1700  *
1701  * Note that we set B_CACHE here, indicating that buffer is
1702  * fully valid and thus cacheable.  This is true even of NFS
1703  * now so we set it generally.  This could be set either here
1704  * or in biodone() since the I/O is synchronous.  We put it
1705  * here.
1706  */
1707 static int
1708 ffs_bufwrite(struct buf *bp)
1709 {
1710 	int oldflags, s;
1711 	struct buf *newbp;
1712 
1713 	CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1714 	if (bp->b_flags & B_INVAL) {
1715 		brelse(bp);
1716 		return (0);
1717 	}
1718 
1719 	oldflags = bp->b_flags;
1720 
1721 	if (!BUF_ISLOCKED(bp))
1722 		panic("bufwrite: buffer is not busy???");
1723 	s = splbio();
1724 	/*
1725 	 * If a background write is already in progress, delay
1726 	 * writing this block if it is asynchronous. Otherwise
1727 	 * wait for the background write to complete.
1728 	 */
1729 	BO_LOCK(bp->b_bufobj);
1730 	if (bp->b_vflags & BV_BKGRDINPROG) {
1731 		if (bp->b_flags & B_ASYNC) {
1732 			BO_UNLOCK(bp->b_bufobj);
1733 			splx(s);
1734 			bdwrite(bp);
1735 			return (0);
1736 		}
1737 		bp->b_vflags |= BV_BKGRDWAIT;
1738 		msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0);
1739 		if (bp->b_vflags & BV_BKGRDINPROG)
1740 			panic("bufwrite: still writing");
1741 	}
1742 	BO_UNLOCK(bp->b_bufobj);
1743 
1744 	/* Mark the buffer clean */
1745 	bundirty(bp);
1746 
1747 	/*
1748 	 * If this buffer is marked for background writing and we
1749 	 * do not have to wait for it, make a copy and write the
1750 	 * copy so as to leave this buffer ready for further use.
1751 	 *
1752 	 * This optimization eats a lot of memory.  If we have a page
1753 	 * or buffer shortfall we can't do it.
1754 	 */
1755 	if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
1756 	    (bp->b_flags & B_ASYNC) &&
1757 	    !vm_page_count_severe() &&
1758 	    !buf_dirty_count_severe()) {
1759 		KASSERT(bp->b_iodone == NULL,
1760 		    ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
1761 
1762 		/* get a new block */
1763 		newbp = geteblk(bp->b_bufsize);
1764 
1765 		/*
1766 		 * set it to be identical to the old block.  We have to
1767 		 * set b_lblkno and BKGRDMARKER before calling bgetvp()
1768 		 * to avoid confusing the splay tree and gbincore().
1769 		 */
1770 		memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
1771 		newbp->b_lblkno = bp->b_lblkno;
1772 		newbp->b_xflags |= BX_BKGRDMARKER;
1773 		BO_LOCK(bp->b_bufobj);
1774 		bp->b_vflags |= BV_BKGRDINPROG;
1775 		bgetvp(bp->b_vp, newbp);
1776 		BO_UNLOCK(bp->b_bufobj);
1777 		newbp->b_bufobj = &bp->b_vp->v_bufobj;
1778 		newbp->b_blkno = bp->b_blkno;
1779 		newbp->b_offset = bp->b_offset;
1780 		newbp->b_iodone = ffs_backgroundwritedone;
1781 		newbp->b_flags |= B_ASYNC;
1782 		newbp->b_flags &= ~B_INVAL;
1783 
1784 #ifdef SOFTUPDATES
1785 		/* move over the dependencies */
1786 		if (!LIST_EMPTY(&bp->b_dep))
1787 			softdep_move_dependencies(bp, newbp);
1788 #endif
1789 
1790 		/*
1791 		 * Initiate write on the copy, release the original to
1792 		 * the B_LOCKED queue so that it cannot go away until
1793 		 * the background write completes. If not locked it could go
1794 		 * away and then be reconstituted while it was being written.
1795 		 * If the reconstituted buffer were written, we could end up
1796 		 * with two background copies being written at the same time.
1797 		 */
1798 		bqrelse(bp);
1799 		bp = newbp;
1800 	}
1801 
1802 	/* Let the normal bufwrite do the rest for us */
1803 	return (bufwrite(bp));
1804 }
1805 
1806 
1807 static void
1808 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
1809 {
1810 	struct vnode *vp;
1811 	int error;
1812 	struct buf *tbp;
1813 
1814 	vp = bo->__bo_vnode;
1815 	if (bp->b_iocmd == BIO_WRITE) {
1816 		if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
1817 		    bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
1818 		    (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
1819 			panic("ffs_geom_strategy: bad I/O");
1820 		bp->b_flags &= ~B_VALIDSUSPWRT;
1821 		if ((vp->v_vflag & VV_COPYONWRITE) &&
1822 		    vp->v_rdev->si_snapdata != NULL) {
1823 			if ((bp->b_flags & B_CLUSTER) != 0) {
1824 				runningbufwakeup(bp);
1825 				TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
1826 					      b_cluster.cluster_entry) {
1827 					error = ffs_copyonwrite(vp, tbp);
1828 					if (error != 0 &&
1829 					    error != EOPNOTSUPP) {
1830 						bp->b_error = error;
1831 						bp->b_ioflags |= BIO_ERROR;
1832 						bufdone(bp);
1833 						return;
1834 					}
1835 				}
1836 				bp->b_runningbufspace = bp->b_bufsize;
1837 				atomic_add_int(&runningbufspace,
1838 					       bp->b_runningbufspace);
1839 			} else {
1840 				error = ffs_copyonwrite(vp, bp);
1841 				if (error != 0 && error != EOPNOTSUPP) {
1842 					bp->b_error = error;
1843 					bp->b_ioflags |= BIO_ERROR;
1844 					bufdone(bp);
1845 					return;
1846 				}
1847 			}
1848 		}
1849 #ifdef SOFTUPDATES
1850 		if ((bp->b_flags & B_CLUSTER) != 0) {
1851 			TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
1852 				      b_cluster.cluster_entry) {
1853 				if (!LIST_EMPTY(&tbp->b_dep))
1854 					buf_start(tbp);
1855 			}
1856 		} else {
1857 			if (!LIST_EMPTY(&bp->b_dep))
1858 				buf_start(bp);
1859 		}
1860 
1861 #endif
1862 	}
1863 	g_vfs_strategy(bo, bp);
1864 }
1865