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