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