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