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