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