xref: /freebsd/sys/ufs/ffs/ffs_vfsops.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
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 	ump->um_snapgone = ffs_snapgone;
801 	mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
802 	bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
803 	if (fs->fs_sbsize < SBLOCKSIZE)
804 		bp->b_flags |= B_INVAL | B_NOCACHE;
805 	brelse(bp);
806 	bp = NULL;
807 	fs = ump->um_fs;
808 	ffs_oldfscompat_read(fs, ump, sblockloc);
809 	fs->fs_ronly = ronly;
810 	size = fs->fs_cssize;
811 	blks = howmany(size, fs->fs_fsize);
812 	if (fs->fs_contigsumsize > 0)
813 		size += fs->fs_ncg * sizeof(int32_t);
814 	size += fs->fs_ncg * sizeof(u_int8_t);
815 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
816 	fs->fs_csp = space;
817 	for (i = 0; i < blks; i += fs->fs_frag) {
818 		size = fs->fs_bsize;
819 		if (i + fs->fs_frag > blks)
820 			size = (blks - i) * fs->fs_fsize;
821 		if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
822 		    cred, &bp)) != 0) {
823 			free(fs->fs_csp, M_UFSMNT);
824 			goto out;
825 		}
826 		bcopy(bp->b_data, space, (u_int)size);
827 		space = (char *)space + size;
828 		brelse(bp);
829 		bp = NULL;
830 	}
831 	if (fs->fs_contigsumsize > 0) {
832 		fs->fs_maxcluster = lp = space;
833 		for (i = 0; i < fs->fs_ncg; i++)
834 			*lp++ = fs->fs_contigsumsize;
835 		space = lp;
836 	}
837 	size = fs->fs_ncg * sizeof(u_int8_t);
838 	fs->fs_contigdirs = (u_int8_t *)space;
839 	bzero(fs->fs_contigdirs, size);
840 	fs->fs_active = NULL;
841 	mp->mnt_data = ump;
842 	mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
843 	mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
844 	nmp = NULL;
845 	if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
846 	    (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
847 		if (nmp)
848 			vfs_rel(nmp);
849 		vfs_getnewfsid(mp);
850 	}
851 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
852 	MNT_ILOCK(mp);
853 	mp->mnt_flag |= MNT_LOCAL;
854 	MNT_IUNLOCK(mp);
855 	if ((fs->fs_flags & FS_MULTILABEL) != 0) {
856 #ifdef MAC
857 		MNT_ILOCK(mp);
858 		mp->mnt_flag |= MNT_MULTILABEL;
859 		MNT_IUNLOCK(mp);
860 #else
861 		printf(
862 "WARNING: %s: multilabel flag on fs but no MAC support\n",
863 		    mp->mnt_stat.f_mntonname);
864 #endif
865 	}
866 	if ((fs->fs_flags & FS_ACLS) != 0) {
867 #ifdef UFS_ACL
868 		MNT_ILOCK(mp);
869 
870 		if (mp->mnt_flag & MNT_NFS4ACLS)
871 			printf("WARNING: ACLs flag on fs conflicts with "
872 			    "\"nfsv4acls\" mount option; option ignored\n");
873 		mp->mnt_flag &= ~MNT_NFS4ACLS;
874 		mp->mnt_flag |= MNT_ACLS;
875 
876 		MNT_IUNLOCK(mp);
877 #else
878 		printf("WARNING: %s: ACLs flag on fs but no ACLs support\n",
879 		    mp->mnt_stat.f_mntonname);
880 #endif
881 	}
882 	if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
883 #ifdef UFS_ACL
884 		MNT_ILOCK(mp);
885 
886 		if (mp->mnt_flag & MNT_ACLS)
887 			printf("WARNING: NFSv4 ACLs flag on fs conflicts with "
888 			    "\"acls\" mount option; option ignored\n");
889 		mp->mnt_flag &= ~MNT_ACLS;
890 		mp->mnt_flag |= MNT_NFS4ACLS;
891 
892 		MNT_IUNLOCK(mp);
893 #else
894 		printf(
895 "WARNING: %s: NFSv4 ACLs flag on fs but no ACLs support\n",
896 		    mp->mnt_stat.f_mntonname);
897 #endif
898 	}
899 	if ((fs->fs_flags & FS_TRIM) != 0) {
900 		size = sizeof(int);
901 		if (g_io_getattr("GEOM::candelete", cp, &size,
902 		    &ump->um_candelete) == 0) {
903 			if (!ump->um_candelete)
904 				printf(
905 "WARNING: %s: TRIM flag on fs but disk does not support TRIM\n",
906 				    mp->mnt_stat.f_mntonname);
907 		} else {
908 			printf(
909 "WARNING: %s: TRIM flag on fs but cannot get whether disk supports TRIM\n",
910 			    mp->mnt_stat.f_mntonname);
911 			ump->um_candelete = 0;
912 		}
913 	}
914 
915 	ump->um_mountp = mp;
916 	ump->um_dev = dev;
917 	ump->um_devvp = devvp;
918 	ump->um_nindir = fs->fs_nindir;
919 	ump->um_bptrtodb = fs->fs_fsbtodb;
920 	ump->um_seqinc = fs->fs_frag;
921 	for (i = 0; i < MAXQUOTAS; i++)
922 		ump->um_quotas[i] = NULLVP;
923 #ifdef UFS_EXTATTR
924 	ufs_extattr_uepm_init(&ump->um_extattr);
925 #endif
926 	/*
927 	 * Set FS local "last mounted on" information (NULL pad)
928 	 */
929 	bzero(fs->fs_fsmnt, MAXMNTLEN);
930 	strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
931 	mp->mnt_stat.f_iosize = fs->fs_bsize;
932 
933 	if( mp->mnt_flag & MNT_ROOTFS) {
934 		/*
935 		 * Root mount; update timestamp in mount structure.
936 		 * this will be used by the common root mount code
937 		 * to update the system clock.
938 		 */
939 		mp->mnt_time = fs->fs_time;
940 	}
941 
942 	if (ronly == 0) {
943 		fs->fs_mtime = time_second;
944 		if ((fs->fs_flags & FS_DOSOFTDEP) &&
945 		    (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
946 			free(fs->fs_csp, M_UFSMNT);
947 			ffs_flushfiles(mp, FORCECLOSE, td);
948 			goto out;
949 		}
950 		if (fs->fs_snapinum[0] != 0)
951 			ffs_snapshot_mount(mp);
952 		fs->fs_fmod = 1;
953 		fs->fs_clean = 0;
954 		(void) ffs_sbupdate(ump, MNT_WAIT, 0);
955 	}
956 	/*
957 	 * Initialize filesystem stat information in mount struct.
958 	 */
959 	MNT_ILOCK(mp);
960 	mp->mnt_kern_flag |= MNTK_MPSAFE | MNTK_LOOKUP_SHARED |
961 	    MNTK_EXTENDED_SHARED;
962 	MNT_IUNLOCK(mp);
963 #ifdef UFS_EXTATTR
964 #ifdef UFS_EXTATTR_AUTOSTART
965 	/*
966 	 *
967 	 * Auto-starting does the following:
968 	 *	- check for /.attribute in the fs, and extattr_start if so
969 	 *	- for each file in .attribute, enable that file with
970 	 * 	  an attribute of the same name.
971 	 * Not clear how to report errors -- probably eat them.
972 	 * This would all happen while the filesystem was busy/not
973 	 * available, so would effectively be "atomic".
974 	 */
975 	(void) ufs_extattr_autostart(mp, td);
976 #endif /* !UFS_EXTATTR_AUTOSTART */
977 #endif /* !UFS_EXTATTR */
978 	return (0);
979 out:
980 	if (bp)
981 		brelse(bp);
982 	if (cp != NULL) {
983 		DROP_GIANT();
984 		g_topology_lock();
985 		g_vfs_close(cp);
986 		g_topology_unlock();
987 		PICKUP_GIANT();
988 	}
989 	if (ump) {
990 		mtx_destroy(UFS_MTX(ump));
991 		if (mp->mnt_gjprovider != NULL) {
992 			free(mp->mnt_gjprovider, M_UFSMNT);
993 			mp->mnt_gjprovider = NULL;
994 		}
995 		free(ump->um_fs, M_UFSMNT);
996 		free(ump, M_UFSMNT);
997 		mp->mnt_data = NULL;
998 	}
999 	dev_rel(dev);
1000 	return (error);
1001 }
1002 
1003 #include <sys/sysctl.h>
1004 static int bigcgs = 0;
1005 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1006 
1007 /*
1008  * Sanity checks for loading old filesystem superblocks.
1009  * See ffs_oldfscompat_write below for unwound actions.
1010  *
1011  * XXX - Parts get retired eventually.
1012  * Unfortunately new bits get added.
1013  */
1014 static void
1015 ffs_oldfscompat_read(fs, ump, sblockloc)
1016 	struct fs *fs;
1017 	struct ufsmount *ump;
1018 	ufs2_daddr_t sblockloc;
1019 {
1020 	off_t maxfilesize;
1021 
1022 	/*
1023 	 * If not yet done, update fs_flags location and value of fs_sblockloc.
1024 	 */
1025 	if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1026 		fs->fs_flags = fs->fs_old_flags;
1027 		fs->fs_old_flags |= FS_FLAGS_UPDATED;
1028 		fs->fs_sblockloc = sblockloc;
1029 	}
1030 	/*
1031 	 * If not yet done, update UFS1 superblock with new wider fields.
1032 	 */
1033 	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1034 		fs->fs_maxbsize = fs->fs_bsize;
1035 		fs->fs_time = fs->fs_old_time;
1036 		fs->fs_size = fs->fs_old_size;
1037 		fs->fs_dsize = fs->fs_old_dsize;
1038 		fs->fs_csaddr = fs->fs_old_csaddr;
1039 		fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1040 		fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1041 		fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1042 		fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1043 	}
1044 	if (fs->fs_magic == FS_UFS1_MAGIC &&
1045 	    fs->fs_old_inodefmt < FS_44INODEFMT) {
1046 		fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1047 		fs->fs_qbmask = ~fs->fs_bmask;
1048 		fs->fs_qfmask = ~fs->fs_fmask;
1049 	}
1050 	if (fs->fs_magic == FS_UFS1_MAGIC) {
1051 		ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1052 		maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1053 		if (fs->fs_maxfilesize > maxfilesize)
1054 			fs->fs_maxfilesize = maxfilesize;
1055 	}
1056 	/* Compatibility for old filesystems */
1057 	if (fs->fs_avgfilesize <= 0)
1058 		fs->fs_avgfilesize = AVFILESIZ;
1059 	if (fs->fs_avgfpdir <= 0)
1060 		fs->fs_avgfpdir = AFPDIR;
1061 	if (bigcgs) {
1062 		fs->fs_save_cgsize = fs->fs_cgsize;
1063 		fs->fs_cgsize = fs->fs_bsize;
1064 	}
1065 }
1066 
1067 /*
1068  * Unwinding superblock updates for old filesystems.
1069  * See ffs_oldfscompat_read above for details.
1070  *
1071  * XXX - Parts get retired eventually.
1072  * Unfortunately new bits get added.
1073  */
1074 void
1075 ffs_oldfscompat_write(fs, ump)
1076 	struct fs *fs;
1077 	struct ufsmount *ump;
1078 {
1079 
1080 	/*
1081 	 * Copy back UFS2 updated fields that UFS1 inspects.
1082 	 */
1083 	if (fs->fs_magic == FS_UFS1_MAGIC) {
1084 		fs->fs_old_time = fs->fs_time;
1085 		fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1086 		fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1087 		fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1088 		fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1089 		fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1090 	}
1091 	if (bigcgs) {
1092 		fs->fs_cgsize = fs->fs_save_cgsize;
1093 		fs->fs_save_cgsize = 0;
1094 	}
1095 }
1096 
1097 /*
1098  * unmount system call
1099  */
1100 static int
1101 ffs_unmount(mp, mntflags)
1102 	struct mount *mp;
1103 	int mntflags;
1104 {
1105 	struct thread *td;
1106 	struct ufsmount *ump = VFSTOUFS(mp);
1107 	struct fs *fs;
1108 	int error, flags, susp;
1109 #ifdef UFS_EXTATTR
1110 	int e_restart;
1111 #endif
1112 
1113 	flags = 0;
1114 	td = curthread;
1115 	fs = ump->um_fs;
1116 	if (mntflags & MNT_FORCE) {
1117 		flags |= FORCECLOSE;
1118 		susp = fs->fs_ronly != 0;
1119 	} else
1120 		susp = 0;
1121 #ifdef UFS_EXTATTR
1122 	if ((error = ufs_extattr_stop(mp, td))) {
1123 		if (error != EOPNOTSUPP)
1124 			printf("ffs_unmount: ufs_extattr_stop returned %d\n",
1125 			    error);
1126 		e_restart = 0;
1127 	} else {
1128 		ufs_extattr_uepm_destroy(&ump->um_extattr);
1129 		e_restart = 1;
1130 	}
1131 #endif
1132 	if (susp) {
1133 		/*
1134 		 * dounmount already called vn_start_write().
1135 		 */
1136 		for (;;) {
1137 			vn_finished_write(mp);
1138 			if ((error = vfs_write_suspend(mp)) != 0)
1139 				return (error);
1140 			MNT_ILOCK(mp);
1141 			if (mp->mnt_kern_flag & MNTK_SUSPENDED) {
1142 				mp->mnt_kern_flag &= ~(MNTK_SUSPENDED |
1143 				    MNTK_SUSPEND2);
1144 				wakeup(&mp->mnt_flag);
1145 				MNT_IUNLOCK(mp);
1146 				td->td_pflags |= TDP_IGNSUSP;
1147 				break;
1148 			}
1149 			MNT_IUNLOCK(mp);
1150 			vn_start_write(NULL, &mp, V_WAIT);
1151 		}
1152 	}
1153 	if (mp->mnt_flag & MNT_SOFTDEP)
1154 		error = softdep_flushfiles(mp, flags, td);
1155 	else
1156 		error = ffs_flushfiles(mp, flags, td);
1157 	if (error != 0 && error != ENXIO)
1158 		goto fail;
1159 
1160 	UFS_LOCK(ump);
1161 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1162 		printf("%s: unmount pending error: blocks %jd files %d\n",
1163 		    fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1164 		    fs->fs_pendinginodes);
1165 		fs->fs_pendingblocks = 0;
1166 		fs->fs_pendinginodes = 0;
1167 	}
1168 	UFS_UNLOCK(ump);
1169 	softdep_unmount(mp);
1170 	if (fs->fs_ronly == 0) {
1171 		fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1172 		error = ffs_sbupdate(ump, MNT_WAIT, 0);
1173 		if (error && error != ENXIO) {
1174 			fs->fs_clean = 0;
1175 			goto fail;
1176 		}
1177 	}
1178 	if (susp) {
1179 		vfs_write_resume(mp);
1180 		vn_start_write(NULL, &mp, V_WAIT);
1181 	}
1182 	DROP_GIANT();
1183 	g_topology_lock();
1184 	g_vfs_close(ump->um_cp);
1185 	g_topology_unlock();
1186 	PICKUP_GIANT();
1187 	vrele(ump->um_devvp);
1188 	dev_rel(ump->um_dev);
1189 	mtx_destroy(UFS_MTX(ump));
1190 	if (mp->mnt_gjprovider != NULL) {
1191 		free(mp->mnt_gjprovider, M_UFSMNT);
1192 		mp->mnt_gjprovider = NULL;
1193 	}
1194 	free(fs->fs_csp, M_UFSMNT);
1195 	free(fs, M_UFSMNT);
1196 	free(ump, M_UFSMNT);
1197 	mp->mnt_data = NULL;
1198 	MNT_ILOCK(mp);
1199 	mp->mnt_flag &= ~MNT_LOCAL;
1200 	MNT_IUNLOCK(mp);
1201 	return (error);
1202 
1203 fail:
1204 	if (susp) {
1205 		vfs_write_resume(mp);
1206 		vn_start_write(NULL, &mp, V_WAIT);
1207 	}
1208 #ifdef UFS_EXTATTR
1209 	if (e_restart) {
1210 		ufs_extattr_uepm_init(&ump->um_extattr);
1211 #ifdef UFS_EXTATTR_AUTOSTART
1212 		(void) ufs_extattr_autostart(mp, td);
1213 #endif
1214 	}
1215 #endif
1216 
1217 	return (error);
1218 }
1219 
1220 /*
1221  * Flush out all the files in a filesystem.
1222  */
1223 int
1224 ffs_flushfiles(mp, flags, td)
1225 	struct mount *mp;
1226 	int flags;
1227 	struct thread *td;
1228 {
1229 	struct ufsmount *ump;
1230 	int error;
1231 
1232 	ump = VFSTOUFS(mp);
1233 #ifdef QUOTA
1234 	if (mp->mnt_flag & MNT_QUOTA) {
1235 		int i;
1236 		error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1237 		if (error)
1238 			return (error);
1239 		for (i = 0; i < MAXQUOTAS; i++) {
1240 			quotaoff(td, mp, i);
1241 		}
1242 		/*
1243 		 * Here we fall through to vflush again to ensure
1244 		 * that we have gotten rid of all the system vnodes.
1245 		 */
1246 	}
1247 #endif
1248 	ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1249 	if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1250 		if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1251 			return (error);
1252 		ffs_snapshot_unmount(mp);
1253 		flags |= FORCECLOSE;
1254 		/*
1255 		 * Here we fall through to vflush again to ensure
1256 		 * that we have gotten rid of all the system vnodes.
1257 		 */
1258 	}
1259         /*
1260 	 * Flush all the files.
1261 	 */
1262 	if ((error = vflush(mp, 0, flags, td)) != 0)
1263 		return (error);
1264 	/*
1265 	 * Flush filesystem metadata.
1266 	 */
1267 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1268 	error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1269 	VOP_UNLOCK(ump->um_devvp, 0);
1270 	return (error);
1271 }
1272 
1273 /*
1274  * Get filesystem statistics.
1275  */
1276 static int
1277 ffs_statfs(mp, sbp)
1278 	struct mount *mp;
1279 	struct statfs *sbp;
1280 {
1281 	struct ufsmount *ump;
1282 	struct fs *fs;
1283 
1284 	ump = VFSTOUFS(mp);
1285 	fs = ump->um_fs;
1286 	if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1287 		panic("ffs_statfs");
1288 	sbp->f_version = STATFS_VERSION;
1289 	sbp->f_bsize = fs->fs_fsize;
1290 	sbp->f_iosize = fs->fs_bsize;
1291 	sbp->f_blocks = fs->fs_dsize;
1292 	UFS_LOCK(ump);
1293 	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1294 	    fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1295 	sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1296 	    dbtofsb(fs, fs->fs_pendingblocks);
1297 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
1298 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1299 	UFS_UNLOCK(ump);
1300 	sbp->f_namemax = NAME_MAX;
1301 	return (0);
1302 }
1303 
1304 /*
1305  * Go through the disk queues to initiate sandbagged IO;
1306  * go through the inodes to write those that have been modified;
1307  * initiate the writing of the super block if it has been modified.
1308  *
1309  * Note: we are always called with the filesystem marked `MPBUSY'.
1310  */
1311 static int
1312 ffs_sync(mp, waitfor)
1313 	struct mount *mp;
1314 	int waitfor;
1315 {
1316 	struct vnode *mvp, *vp, *devvp;
1317 	struct thread *td;
1318 	struct inode *ip;
1319 	struct ufsmount *ump = VFSTOUFS(mp);
1320 	struct fs *fs;
1321 	int error, count, wait, lockreq, allerror = 0;
1322 	int suspend;
1323 	int suspended;
1324 	int secondary_writes;
1325 	int secondary_accwrites;
1326 	int softdep_deps;
1327 	int softdep_accdeps;
1328 	struct bufobj *bo;
1329 
1330 	td = curthread;
1331 	fs = ump->um_fs;
1332 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
1333 		printf("fs = %s\n", fs->fs_fsmnt);
1334 		panic("ffs_sync: rofs mod");
1335 	}
1336 	/*
1337 	 * Write back each (modified) inode.
1338 	 */
1339 	wait = 0;
1340 	suspend = 0;
1341 	suspended = 0;
1342 	lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1343 	if (waitfor == MNT_SUSPEND) {
1344 		suspend = 1;
1345 		waitfor = MNT_WAIT;
1346 	}
1347 	if (waitfor == MNT_WAIT) {
1348 		wait = 1;
1349 		lockreq = LK_EXCLUSIVE;
1350 	}
1351 	lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1352 	MNT_ILOCK(mp);
1353 loop:
1354 	/* Grab snapshot of secondary write counts */
1355 	secondary_writes = mp->mnt_secondary_writes;
1356 	secondary_accwrites = mp->mnt_secondary_accwrites;
1357 
1358 	/* Grab snapshot of softdep dependency counts */
1359 	MNT_IUNLOCK(mp);
1360 	softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1361 	MNT_ILOCK(mp);
1362 
1363 	MNT_VNODE_FOREACH(vp, mp, mvp) {
1364 		/*
1365 		 * Depend on the mntvnode_slock to keep things stable enough
1366 		 * for a quick test.  Since there might be hundreds of
1367 		 * thousands of vnodes, we cannot afford even a subroutine
1368 		 * call unless there's a good chance that we have work to do.
1369 		 */
1370 		VI_LOCK(vp);
1371 		if (vp->v_iflag & VI_DOOMED) {
1372 			VI_UNLOCK(vp);
1373 			continue;
1374 		}
1375 		ip = VTOI(vp);
1376 		if (vp->v_type == VNON || ((ip->i_flag &
1377 		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1378 		    vp->v_bufobj.bo_dirty.bv_cnt == 0)) {
1379 			VI_UNLOCK(vp);
1380 			continue;
1381 		}
1382 		MNT_IUNLOCK(mp);
1383 		if ((error = vget(vp, lockreq, td)) != 0) {
1384 			MNT_ILOCK(mp);
1385 			if (error == ENOENT || error == ENOLCK) {
1386 				MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
1387 				goto loop;
1388 			}
1389 			continue;
1390 		}
1391 		if ((error = ffs_syncvnode(vp, waitfor)) != 0)
1392 			allerror = error;
1393 		vput(vp);
1394 		MNT_ILOCK(mp);
1395 	}
1396 	MNT_IUNLOCK(mp);
1397 	/*
1398 	 * Force stale filesystem control information to be flushed.
1399 	 */
1400 	if (waitfor == MNT_WAIT) {
1401 		if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1402 			allerror = error;
1403 		/* Flushed work items may create new vnodes to clean */
1404 		if (allerror == 0 && count) {
1405 			MNT_ILOCK(mp);
1406 			goto loop;
1407 		}
1408 	}
1409 #ifdef QUOTA
1410 	qsync(mp);
1411 #endif
1412 	devvp = ump->um_devvp;
1413 	bo = &devvp->v_bufobj;
1414 	BO_LOCK(bo);
1415 	if (waitfor != MNT_LAZY &&
1416 	    (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
1417 		BO_UNLOCK(bo);
1418 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1419 		if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0)
1420 			allerror = error;
1421 		VOP_UNLOCK(devvp, 0);
1422 		if (allerror == 0 && waitfor == MNT_WAIT) {
1423 			MNT_ILOCK(mp);
1424 			goto loop;
1425 		}
1426 	} else if (suspend != 0) {
1427 		if (softdep_check_suspend(mp,
1428 					  devvp,
1429 					  softdep_deps,
1430 					  softdep_accdeps,
1431 					  secondary_writes,
1432 					  secondary_accwrites) != 0)
1433 			goto loop;	/* More work needed */
1434 		mtx_assert(MNT_MTX(mp), MA_OWNED);
1435 		mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1436 		MNT_IUNLOCK(mp);
1437 		suspended = 1;
1438 	} else
1439 		BO_UNLOCK(bo);
1440 	/*
1441 	 * Write back modified superblock.
1442 	 */
1443 	if (fs->fs_fmod != 0 &&
1444 	    (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1445 		allerror = error;
1446 	return (allerror);
1447 }
1448 
1449 int
1450 ffs_vget(mp, ino, flags, vpp)
1451 	struct mount *mp;
1452 	ino_t ino;
1453 	int flags;
1454 	struct vnode **vpp;
1455 {
1456 	return (ffs_vgetf(mp, ino, flags, vpp, 0));
1457 }
1458 
1459 int
1460 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1461 	struct mount *mp;
1462 	ino_t ino;
1463 	int flags;
1464 	struct vnode **vpp;
1465 	int ffs_flags;
1466 {
1467 	struct fs *fs;
1468 	struct inode *ip;
1469 	struct ufsmount *ump;
1470 	struct buf *bp;
1471 	struct vnode *vp;
1472 	struct cdev *dev;
1473 	int error;
1474 
1475 	error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1476 	if (error || *vpp != NULL)
1477 		return (error);
1478 
1479 	/*
1480 	 * We must promote to an exclusive lock for vnode creation.  This
1481 	 * can happen if lookup is passed LOCKSHARED.
1482  	 */
1483 	if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1484 		flags &= ~LK_TYPE_MASK;
1485 		flags |= LK_EXCLUSIVE;
1486 	}
1487 
1488 	/*
1489 	 * We do not lock vnode creation as it is believed to be too
1490 	 * expensive for such rare case as simultaneous creation of vnode
1491 	 * for same ino by different processes. We just allow them to race
1492 	 * and check later to decide who wins. Let the race begin!
1493 	 */
1494 
1495 	ump = VFSTOUFS(mp);
1496 	dev = ump->um_dev;
1497 	fs = ump->um_fs;
1498 
1499 	/*
1500 	 * If this malloc() is performed after the getnewvnode()
1501 	 * it might block, leaving a vnode with a NULL v_data to be
1502 	 * found by ffs_sync() if a sync happens to fire right then,
1503 	 * which will cause a panic because ffs_sync() blindly
1504 	 * dereferences vp->v_data (as well it should).
1505 	 */
1506 	ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1507 
1508 	/* Allocate a new vnode/inode. */
1509 	if (fs->fs_magic == FS_UFS1_MAGIC)
1510 		error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp);
1511 	else
1512 		error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp);
1513 	if (error) {
1514 		*vpp = NULL;
1515 		uma_zfree(uma_inode, ip);
1516 		return (error);
1517 	}
1518 	/*
1519 	 * FFS supports recursive locking.
1520 	 */
1521 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1522 	VN_LOCK_AREC(vp);
1523 	vp->v_data = ip;
1524 	vp->v_bufobj.bo_bsize = fs->fs_bsize;
1525 	ip->i_vnode = vp;
1526 	ip->i_ump = ump;
1527 	ip->i_fs = fs;
1528 	ip->i_dev = dev;
1529 	ip->i_number = ino;
1530 	ip->i_ea_refs = 0;
1531 #ifdef QUOTA
1532 	{
1533 		int i;
1534 		for (i = 0; i < MAXQUOTAS; i++)
1535 			ip->i_dquot[i] = NODQUOT;
1536 	}
1537 #endif
1538 
1539 	if (ffs_flags & FFSV_FORCEINSMQ)
1540 		vp->v_vflag |= VV_FORCEINSMQ;
1541 	error = insmntque(vp, mp);
1542 	if (error != 0) {
1543 		uma_zfree(uma_inode, ip);
1544 		*vpp = NULL;
1545 		return (error);
1546 	}
1547 	vp->v_vflag &= ~VV_FORCEINSMQ;
1548 	error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1549 	if (error || *vpp != NULL)
1550 		return (error);
1551 
1552 	/* Read in the disk contents for the inode, copy into the inode. */
1553 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1554 	    (int)fs->fs_bsize, NOCRED, &bp);
1555 	if (error) {
1556 		/*
1557 		 * The inode does not contain anything useful, so it would
1558 		 * be misleading to leave it on its hash chain. With mode
1559 		 * still zero, it will be unlinked and returned to the free
1560 		 * list by vput().
1561 		 */
1562 		brelse(bp);
1563 		vput(vp);
1564 		*vpp = NULL;
1565 		return (error);
1566 	}
1567 	if (ip->i_ump->um_fstype == UFS1)
1568 		ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1569 	else
1570 		ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1571 	ffs_load_inode(bp, ip, fs, ino);
1572 	if (DOINGSOFTDEP(vp))
1573 		softdep_load_inodeblock(ip);
1574 	else
1575 		ip->i_effnlink = ip->i_nlink;
1576 	bqrelse(bp);
1577 
1578 	/*
1579 	 * Initialize the vnode from the inode, check for aliases.
1580 	 * Note that the underlying vnode may have changed.
1581 	 */
1582 	if (ip->i_ump->um_fstype == UFS1)
1583 		error = ufs_vinit(mp, &ffs_fifoops1, &vp);
1584 	else
1585 		error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1586 	if (error) {
1587 		vput(vp);
1588 		*vpp = NULL;
1589 		return (error);
1590 	}
1591 
1592 	/*
1593 	 * Finish inode initialization.
1594 	 */
1595 	if (vp->v_type != VFIFO) {
1596 		/* FFS supports shared locking for all files except fifos. */
1597 		VN_LOCK_ASHARE(vp);
1598 	}
1599 
1600 	/*
1601 	 * Set up a generation number for this inode if it does not
1602 	 * already have one. This should only happen on old filesystems.
1603 	 */
1604 	if (ip->i_gen == 0) {
1605 		ip->i_gen = arc4random() / 2 + 1;
1606 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1607 			ip->i_flag |= IN_MODIFIED;
1608 			DIP_SET(ip, i_gen, ip->i_gen);
1609 		}
1610 	}
1611 #ifdef MAC
1612 	if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1613 		/*
1614 		 * If this vnode is already allocated, and we're running
1615 		 * multi-label, attempt to perform a label association
1616 		 * from the extended attributes on the inode.
1617 		 */
1618 		error = mac_vnode_associate_extattr(mp, vp);
1619 		if (error) {
1620 			/* ufs_inactive will release ip->i_devvp ref. */
1621 			vput(vp);
1622 			*vpp = NULL;
1623 			return (error);
1624 		}
1625 	}
1626 #endif
1627 
1628 	*vpp = vp;
1629 	return (0);
1630 }
1631 
1632 /*
1633  * File handle to vnode
1634  *
1635  * Have to be really careful about stale file handles:
1636  * - check that the inode number is valid
1637  * - call ffs_vget() to get the locked inode
1638  * - check for an unallocated inode (i_mode == 0)
1639  * - check that the given client host has export rights and return
1640  *   those rights via. exflagsp and credanonp
1641  */
1642 static int
1643 ffs_fhtovp(mp, fhp, flags, vpp)
1644 	struct mount *mp;
1645 	struct fid *fhp;
1646 	int flags;
1647 	struct vnode **vpp;
1648 {
1649 	struct ufid *ufhp;
1650 	struct fs *fs;
1651 
1652 	ufhp = (struct ufid *)fhp;
1653 	fs = VFSTOUFS(mp)->um_fs;
1654 	if (ufhp->ufid_ino < ROOTINO ||
1655 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1656 		return (ESTALE);
1657 	return (ufs_fhtovp(mp, ufhp, flags, vpp));
1658 }
1659 
1660 /*
1661  * Initialize the filesystem.
1662  */
1663 static int
1664 ffs_init(vfsp)
1665 	struct vfsconf *vfsp;
1666 {
1667 
1668 	softdep_initialize();
1669 	return (ufs_init(vfsp));
1670 }
1671 
1672 /*
1673  * Undo the work of ffs_init().
1674  */
1675 static int
1676 ffs_uninit(vfsp)
1677 	struct vfsconf *vfsp;
1678 {
1679 	int ret;
1680 
1681 	ret = ufs_uninit(vfsp);
1682 	softdep_uninitialize();
1683 	return (ret);
1684 }
1685 
1686 /*
1687  * Write a superblock and associated information back to disk.
1688  */
1689 int
1690 ffs_sbupdate(mp, waitfor, suspended)
1691 	struct ufsmount *mp;
1692 	int waitfor;
1693 	int suspended;
1694 {
1695 	struct fs *fs = mp->um_fs;
1696 	struct buf *sbbp;
1697 	struct buf *bp;
1698 	int blks;
1699 	void *space;
1700 	int i, size, error, allerror = 0;
1701 
1702 	if (fs->fs_ronly == 1 &&
1703 	    (mp->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1704 	    (MNT_RDONLY | MNT_UPDATE))
1705 		panic("ffs_sbupdate: write read-only filesystem");
1706 	/*
1707 	 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1708 	 */
1709 	sbbp = getblk(mp->um_devvp, btodb(fs->fs_sblockloc), (int)fs->fs_sbsize,
1710 	    0, 0, 0);
1711 	/*
1712 	 * First write back the summary information.
1713 	 */
1714 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
1715 	space = fs->fs_csp;
1716 	for (i = 0; i < blks; i += fs->fs_frag) {
1717 		size = fs->fs_bsize;
1718 		if (i + fs->fs_frag > blks)
1719 			size = (blks - i) * fs->fs_fsize;
1720 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1721 		    size, 0, 0, 0);
1722 		bcopy(space, bp->b_data, (u_int)size);
1723 		space = (char *)space + size;
1724 		if (suspended)
1725 			bp->b_flags |= B_VALIDSUSPWRT;
1726 		if (waitfor != MNT_WAIT)
1727 			bawrite(bp);
1728 		else if ((error = bwrite(bp)) != 0)
1729 			allerror = error;
1730 	}
1731 	/*
1732 	 * Now write back the superblock itself. If any errors occurred
1733 	 * up to this point, then fail so that the superblock avoids
1734 	 * being written out as clean.
1735 	 */
1736 	if (allerror) {
1737 		brelse(sbbp);
1738 		return (allerror);
1739 	}
1740 	bp = sbbp;
1741 	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1742 	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1743 		printf("%s: correcting fs_sblockloc from %jd to %d\n",
1744 		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1745 		fs->fs_sblockloc = SBLOCK_UFS1;
1746 	}
1747 	if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1748 	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1749 		printf("%s: correcting fs_sblockloc from %jd to %d\n",
1750 		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1751 		fs->fs_sblockloc = SBLOCK_UFS2;
1752 	}
1753 	fs->fs_fmod = 0;
1754 	fs->fs_time = time_second;
1755 	if (fs->fs_flags & FS_DOSOFTDEP)
1756 		softdep_setup_sbupdate(mp, (struct fs *)bp->b_data, bp);
1757 	bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1758 	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1759 	if (suspended)
1760 		bp->b_flags |= B_VALIDSUSPWRT;
1761 	if (waitfor != MNT_WAIT)
1762 		bawrite(bp);
1763 	else if ((error = bwrite(bp)) != 0)
1764 		allerror = error;
1765 	return (allerror);
1766 }
1767 
1768 static int
1769 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1770 	int attrnamespace, const char *attrname)
1771 {
1772 
1773 #ifdef UFS_EXTATTR
1774 	return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1775 	    attrname));
1776 #else
1777 	return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1778 	    attrname));
1779 #endif
1780 }
1781 
1782 static void
1783 ffs_ifree(struct ufsmount *ump, struct inode *ip)
1784 {
1785 
1786 	if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1787 		uma_zfree(uma_ufs1, ip->i_din1);
1788 	else if (ip->i_din2 != NULL)
1789 		uma_zfree(uma_ufs2, ip->i_din2);
1790 	uma_zfree(uma_inode, ip);
1791 }
1792 
1793 static int dobkgrdwrite = 1;
1794 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1795     "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1796 
1797 /*
1798  * Complete a background write started from bwrite.
1799  */
1800 static void
1801 ffs_backgroundwritedone(struct buf *bp)
1802 {
1803 	struct bufobj *bufobj;
1804 	struct buf *origbp;
1805 
1806 	/*
1807 	 * Find the original buffer that we are writing.
1808 	 */
1809 	bufobj = bp->b_bufobj;
1810 	BO_LOCK(bufobj);
1811 	if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
1812 		panic("backgroundwritedone: lost buffer");
1813 	/* Grab an extra reference to be dropped by the bufdone() below. */
1814 	bufobj_wrefl(bufobj);
1815 	BO_UNLOCK(bufobj);
1816 	/*
1817 	 * Process dependencies then return any unfinished ones.
1818 	 */
1819 	if (!LIST_EMPTY(&bp->b_dep))
1820 		buf_complete(bp);
1821 #ifdef SOFTUPDATES
1822 	if (!LIST_EMPTY(&bp->b_dep))
1823 		softdep_move_dependencies(bp, origbp);
1824 #endif
1825 	/*
1826 	 * This buffer is marked B_NOCACHE so when it is released
1827 	 * by biodone it will be tossed.
1828 	 */
1829 	bp->b_flags |= B_NOCACHE;
1830 	bp->b_flags &= ~B_CACHE;
1831 	bufdone(bp);
1832 	BO_LOCK(bufobj);
1833 	/*
1834 	 * Clear the BV_BKGRDINPROG flag in the original buffer
1835 	 * and awaken it if it is waiting for the write to complete.
1836 	 * If BV_BKGRDINPROG is not set in the original buffer it must
1837 	 * have been released and re-instantiated - which is not legal.
1838 	 */
1839 	KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
1840 	    ("backgroundwritedone: lost buffer2"));
1841 	origbp->b_vflags &= ~BV_BKGRDINPROG;
1842 	if (origbp->b_vflags & BV_BKGRDWAIT) {
1843 		origbp->b_vflags &= ~BV_BKGRDWAIT;
1844 		wakeup(&origbp->b_xflags);
1845 	}
1846 	BO_UNLOCK(bufobj);
1847 }
1848 
1849 
1850 /*
1851  * Write, release buffer on completion.  (Done by iodone
1852  * if async).  Do not bother writing anything if the buffer
1853  * is invalid.
1854  *
1855  * Note that we set B_CACHE here, indicating that buffer is
1856  * fully valid and thus cacheable.  This is true even of NFS
1857  * now so we set it generally.  This could be set either here
1858  * or in biodone() since the I/O is synchronous.  We put it
1859  * here.
1860  */
1861 static int
1862 ffs_bufwrite(struct buf *bp)
1863 {
1864 	int oldflags, s;
1865 	struct buf *newbp;
1866 
1867 	CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1868 	if (bp->b_flags & B_INVAL) {
1869 		brelse(bp);
1870 		return (0);
1871 	}
1872 
1873 	oldflags = bp->b_flags;
1874 
1875 	if (!BUF_ISLOCKED(bp))
1876 		panic("bufwrite: buffer is not busy???");
1877 	s = splbio();
1878 	/*
1879 	 * If a background write is already in progress, delay
1880 	 * writing this block if it is asynchronous. Otherwise
1881 	 * wait for the background write to complete.
1882 	 */
1883 	BO_LOCK(bp->b_bufobj);
1884 	if (bp->b_vflags & BV_BKGRDINPROG) {
1885 		if (bp->b_flags & B_ASYNC) {
1886 			BO_UNLOCK(bp->b_bufobj);
1887 			splx(s);
1888 			bdwrite(bp);
1889 			return (0);
1890 		}
1891 		bp->b_vflags |= BV_BKGRDWAIT;
1892 		msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0);
1893 		if (bp->b_vflags & BV_BKGRDINPROG)
1894 			panic("bufwrite: still writing");
1895 	}
1896 	BO_UNLOCK(bp->b_bufobj);
1897 
1898 	/*
1899 	 * If this buffer is marked for background writing and we
1900 	 * do not have to wait for it, make a copy and write the
1901 	 * copy so as to leave this buffer ready for further use.
1902 	 *
1903 	 * This optimization eats a lot of memory.  If we have a page
1904 	 * or buffer shortfall we can't do it.
1905 	 */
1906 	if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
1907 	    (bp->b_flags & B_ASYNC) &&
1908 	    !vm_page_count_severe() &&
1909 	    !buf_dirty_count_severe()) {
1910 		KASSERT(bp->b_iodone == NULL,
1911 		    ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
1912 
1913 		/* get a new block */
1914 		newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
1915 		if (newbp == NULL)
1916 			goto normal_write;
1917 
1918 		/*
1919 		 * set it to be identical to the old block.  We have to
1920 		 * set b_lblkno and BKGRDMARKER before calling bgetvp()
1921 		 * to avoid confusing the splay tree and gbincore().
1922 		 */
1923 		memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
1924 		newbp->b_lblkno = bp->b_lblkno;
1925 		newbp->b_xflags |= BX_BKGRDMARKER;
1926 		BO_LOCK(bp->b_bufobj);
1927 		bp->b_vflags |= BV_BKGRDINPROG;
1928 		bgetvp(bp->b_vp, newbp);
1929 		BO_UNLOCK(bp->b_bufobj);
1930 		newbp->b_bufobj = &bp->b_vp->v_bufobj;
1931 		newbp->b_blkno = bp->b_blkno;
1932 		newbp->b_offset = bp->b_offset;
1933 		newbp->b_iodone = ffs_backgroundwritedone;
1934 		newbp->b_flags |= B_ASYNC;
1935 		newbp->b_flags &= ~B_INVAL;
1936 
1937 #ifdef SOFTUPDATES
1938 		/*
1939 		 * Move over the dependencies.  If there are rollbacks,
1940 		 * leave the parent buffer dirtied as it will need to
1941 		 * be written again.
1942 		 */
1943 		if (LIST_EMPTY(&bp->b_dep) ||
1944 		    softdep_move_dependencies(bp, newbp) == 0)
1945 			bundirty(bp);
1946 #else
1947 		bundirty(bp);
1948 #endif
1949 
1950 		/*
1951 		 * Initiate write on the copy, release the original to
1952 		 * the B_LOCKED queue so that it cannot go away until
1953 		 * the background write completes. If not locked it could go
1954 		 * away and then be reconstituted while it was being written.
1955 		 * If the reconstituted buffer were written, we could end up
1956 		 * with two background copies being written at the same time.
1957 		 */
1958 		bqrelse(bp);
1959 		bp = newbp;
1960 	} else
1961 		/* Mark the buffer clean */
1962 		bundirty(bp);
1963 
1964 
1965 	/* Let the normal bufwrite do the rest for us */
1966 normal_write:
1967 	return (bufwrite(bp));
1968 }
1969 
1970 
1971 static void
1972 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
1973 {
1974 	struct vnode *vp;
1975 	int error;
1976 	struct buf *tbp;
1977 	int nocopy;
1978 
1979 	vp = bo->__bo_vnode;
1980 	if (bp->b_iocmd == BIO_WRITE) {
1981 		if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
1982 		    bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
1983 		    (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
1984 			panic("ffs_geom_strategy: bad I/O");
1985 		nocopy = bp->b_flags & B_NOCOPY;
1986 		bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY);
1987 		if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 &&
1988 		    vp->v_rdev->si_snapdata != NULL) {
1989 			if ((bp->b_flags & B_CLUSTER) != 0) {
1990 				runningbufwakeup(bp);
1991 				TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
1992 					      b_cluster.cluster_entry) {
1993 					error = ffs_copyonwrite(vp, tbp);
1994 					if (error != 0 &&
1995 					    error != EOPNOTSUPP) {
1996 						bp->b_error = error;
1997 						bp->b_ioflags |= BIO_ERROR;
1998 						bufdone(bp);
1999 						return;
2000 					}
2001 				}
2002 				bp->b_runningbufspace = bp->b_bufsize;
2003 				atomic_add_long(&runningbufspace,
2004 					       bp->b_runningbufspace);
2005 			} else {
2006 				error = ffs_copyonwrite(vp, bp);
2007 				if (error != 0 && error != EOPNOTSUPP) {
2008 					bp->b_error = error;
2009 					bp->b_ioflags |= BIO_ERROR;
2010 					bufdone(bp);
2011 					return;
2012 				}
2013 			}
2014 		}
2015 #ifdef SOFTUPDATES
2016 		if ((bp->b_flags & B_CLUSTER) != 0) {
2017 			TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2018 				      b_cluster.cluster_entry) {
2019 				if (!LIST_EMPTY(&tbp->b_dep))
2020 					buf_start(tbp);
2021 			}
2022 		} else {
2023 			if (!LIST_EMPTY(&bp->b_dep))
2024 				buf_start(bp);
2025 		}
2026 
2027 #endif
2028 	}
2029 	g_vfs_strategy(bo, bp);
2030 }
2031 
2032 #ifdef	DDB
2033 
2034 static void
2035 db_print_ffs(struct ufsmount *ump)
2036 {
2037 	db_printf("mp %p %s devvp %p fs %p su_wl %d su_wl_in %d su_deps %d "
2038 		  "su_req %d\n",
2039 	    ump->um_mountp, ump->um_mountp->mnt_stat.f_mntonname,
2040 	    ump->um_devvp, ump->um_fs, ump->softdep_on_worklist,
2041 	    ump->softdep_on_worklist_inprogress, ump->softdep_deps,
2042 	    ump->softdep_req);
2043 }
2044 
2045 DB_SHOW_COMMAND(ffs, db_show_ffs)
2046 {
2047 	struct mount *mp;
2048 	struct ufsmount *ump;
2049 
2050 	if (have_addr) {
2051 		ump = VFSTOUFS((struct mount *)addr);
2052 		db_print_ffs(ump);
2053 		return;
2054 	}
2055 
2056 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2057 		if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2058 			db_print_ffs(VFSTOUFS(mp));
2059 	}
2060 }
2061 
2062 #endif	/* DDB */
2063