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