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