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