xref: /freebsd/sys/ufs/ffs/ffs_vfsops.c (revision 6fecb4e41eb5e074ed467e7e1cc70e4c20e08d08)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1989, 1991, 1993, 1994
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29996c772fSJohn Dyson  *	@(#)ffs_vfsops.c	8.31 (Berkeley) 5/20/95
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
32f4636c59SDavid E. O'Brien #include <sys/cdefs.h>
33f4636c59SDavid E. O'Brien __FBSDID("$FreeBSD$");
34f4636c59SDavid E. O'Brien 
3580830407SRobert Watson #include "opt_mac.h"
3601733a9bSGarrett Wollman #include "opt_quota.h"
37516081f2SRobert Watson #include "opt_ufs.h"
386e77a041SPoul-Henning Kamp #include "opt_ffs.h"
3952dfc8d7SKonstantin Belousov #include "opt_ddb.h"
4001733a9bSGarrett Wollman 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
43df8bae1dSRodney W. Grimes #include <sys/namei.h>
44acd3428bSRobert Watson #include <sys/priv.h>
45df8bae1dSRodney W. Grimes #include <sys/proc.h>
46df8bae1dSRodney W. Grimes #include <sys/kernel.h>
47df8bae1dSRodney W. Grimes #include <sys/vnode.h>
48df8bae1dSRodney W. Grimes #include <sys/mount.h>
499626b608SPoul-Henning Kamp #include <sys/bio.h>
50df8bae1dSRodney W. Grimes #include <sys/buf.h>
5181bca6ddSKATO Takenori #include <sys/conf.h>
523ac4d1efSBruce Evans #include <sys/fcntl.h>
53df8bae1dSRodney W. Grimes #include <sys/malloc.h>
541b367556SJason Evans #include <sys/mutex.h>
55a18b1f1dSJason Evans 
56aed55708SRobert Watson #include <security/mac/mac_framework.h>
57aed55708SRobert Watson 
58a64ed089SRobert Watson #include <ufs/ufs/extattr.h>
591a60c7fcSPawel Jakub Dawidek #include <ufs/ufs/gjournal.h>
60df8bae1dSRodney W. Grimes #include <ufs/ufs/quota.h>
61df8bae1dSRodney W. Grimes #include <ufs/ufs/ufsmount.h>
62df8bae1dSRodney W. Grimes #include <ufs/ufs/inode.h>
63df8bae1dSRodney W. Grimes #include <ufs/ufs/ufs_extern.h>
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes #include <ufs/ffs/fs.h>
66df8bae1dSRodney W. Grimes #include <ufs/ffs/ffs_extern.h>
67df8bae1dSRodney W. Grimes 
68f6b04d2bSDavid Greenman #include <vm/vm.h>
69aa4d7a8aSPoul-Henning Kamp #include <vm/uma.h>
70f6b04d2bSDavid Greenman #include <vm/vm_page.h>
71f6b04d2bSDavid Greenman 
7243920011SPoul-Henning Kamp #include <geom/geom.h>
7343920011SPoul-Henning Kamp #include <geom/geom_vfs.h>
7443920011SPoul-Henning Kamp 
7552dfc8d7SKonstantin Belousov #include <ddb/ddb.h>
7652dfc8d7SKonstantin Belousov 
77adf41577SPoul-Henning Kamp static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
7855166637SPoul-Henning Kamp 
795e8c582aSPoul-Henning Kamp static int	ffs_reload(struct mount *, struct thread *);
80975512a9SPoul-Henning Kamp static int	ffs_mountfs(struct vnode *, struct mount *, struct thread *);
811c85e6a3SKirk McKusick static void	ffs_oldfscompat_read(struct fs *, struct ufsmount *,
821c85e6a3SKirk McKusick 		    ufs2_daddr_t);
831c85e6a3SKirk McKusick static void	ffs_oldfscompat_write(struct fs *, struct ufsmount *);
84975512a9SPoul-Henning Kamp static void	ffs_ifree(struct ufsmount *ump, struct inode *ip);
859bf1a756SPoul-Henning Kamp static vfs_init_t ffs_init;
869bf1a756SPoul-Henning Kamp static vfs_uninit_t ffs_uninit;
87d6fe88e4SPoul-Henning Kamp static vfs_extattrctl_t ffs_extattrctl;
8820a92a18SPoul-Henning Kamp static vfs_cmount_t ffs_cmount;
89adf41577SPoul-Henning Kamp static vfs_unmount_t ffs_unmount;
9020a92a18SPoul-Henning Kamp static vfs_mount_t ffs_mount;
91adf41577SPoul-Henning Kamp static vfs_statfs_t ffs_statfs;
92adf41577SPoul-Henning Kamp static vfs_fhtovp_t ffs_fhtovp;
93adf41577SPoul-Henning Kamp static vfs_sync_t ffs_sync;
94df8bae1dSRodney W. Grimes 
95303b270bSEivind Eklund static struct vfsops ufs_vfsops = {
967652131bSPoul-Henning Kamp 	.vfs_extattrctl =	ffs_extattrctl,
977652131bSPoul-Henning Kamp 	.vfs_fhtovp =		ffs_fhtovp,
987652131bSPoul-Henning Kamp 	.vfs_init =		ffs_init,
9920a92a18SPoul-Henning Kamp 	.vfs_mount =		ffs_mount,
10020a92a18SPoul-Henning Kamp 	.vfs_cmount =		ffs_cmount,
1017652131bSPoul-Henning Kamp 	.vfs_quotactl =		ufs_quotactl,
1027652131bSPoul-Henning Kamp 	.vfs_root =		ufs_root,
1037652131bSPoul-Henning Kamp 	.vfs_statfs =		ffs_statfs,
1047652131bSPoul-Henning Kamp 	.vfs_sync =		ffs_sync,
1057652131bSPoul-Henning Kamp 	.vfs_uninit =		ffs_uninit,
1067652131bSPoul-Henning Kamp 	.vfs_unmount =		ffs_unmount,
1077652131bSPoul-Henning Kamp 	.vfs_vget =		ffs_vget,
1082814d5baSKonstantin Belousov 	.vfs_susp_clean =	process_deferred_inactive,
109df8bae1dSRodney W. Grimes };
110df8bae1dSRodney W. Grimes 
1118994ca3cSBruce Evans VFS_SET(ufs_vfsops, ufs, 0);
1125fe6d2beSPawel Jakub Dawidek MODULE_VERSION(ufs, 1);
113c901836cSGarrett Wollman 
1146e77a041SPoul-Henning Kamp static b_strategy_t ffs_geom_strategy;
115dd19a799SPoul-Henning Kamp static b_write_t ffs_bufwrite;
1166e77a041SPoul-Henning Kamp 
1176e77a041SPoul-Henning Kamp static struct buf_ops ffs_ops = {
1186e77a041SPoul-Henning Kamp 	.bop_name =	"FFS",
119dd19a799SPoul-Henning Kamp 	.bop_write =	ffs_bufwrite,
1206e77a041SPoul-Henning Kamp 	.bop_strategy =	ffs_geom_strategy,
1216ef8480aSPoul-Henning Kamp 	.bop_sync =	bufsync,
1222cc7d26fSKonstantin Belousov #ifdef NO_FFS_SNAPSHOT
1232cc7d26fSKonstantin Belousov 	.bop_bdflush =	bufbdflush,
1242cc7d26fSKonstantin Belousov #else
1252cc7d26fSKonstantin Belousov 	.bop_bdflush =	ffs_bdflush,
1262cc7d26fSKonstantin Belousov #endif
1276e77a041SPoul-Henning Kamp };
1286e77a041SPoul-Henning Kamp 
129d952ba1bSJohn Baldwin static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
130d952ba1bSJohn Baldwin     "noclusterw", "noexec", "export", "force", "from", "multilabel",
131d952ba1bSJohn Baldwin     "snapshot", "nosuid", "suiddir", "nosymfollow", "sync",
132ee98eb82SCraig Rodrigues     "union", NULL };
13320a92a18SPoul-Henning Kamp 
1345e8c582aSPoul-Henning Kamp static int
13520a92a18SPoul-Henning Kamp ffs_mount(struct mount *mp, struct thread *td)
136df8bae1dSRodney W. Grimes {
13720a92a18SPoul-Henning Kamp 	struct vnode *devvp;
13826f9a767SRodney W. Grimes 	struct ufsmount *ump = 0;
13905f4ff5dSPoul-Henning Kamp 	struct fs *fs;
140f2a2857bSKirk McKusick 	int error, flags;
1415da56ddbSTor Egge 	u_int mntorflags, mntandnotflags;
142c9b99213SBruce Evans 	mode_t accessmode;
1435e8c582aSPoul-Henning Kamp 	struct nameidata ndp;
14420a92a18SPoul-Henning Kamp 	char *fspec;
145df8bae1dSRodney W. Grimes 
14620a92a18SPoul-Henning Kamp 	if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
14720a92a18SPoul-Henning Kamp 		return (EINVAL);
148aa4d7a8aSPoul-Henning Kamp 	if (uma_inode == NULL) {
149aa4d7a8aSPoul-Henning Kamp 		uma_inode = uma_zcreate("FFS inode",
150aa4d7a8aSPoul-Henning Kamp 		    sizeof(struct inode), NULL, NULL, NULL, NULL,
151aa4d7a8aSPoul-Henning Kamp 		    UMA_ALIGN_PTR, 0);
152aa4d7a8aSPoul-Henning Kamp 		uma_ufs1 = uma_zcreate("FFS1 dinode",
153aa4d7a8aSPoul-Henning Kamp 		    sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
154aa4d7a8aSPoul-Henning Kamp 		    UMA_ALIGN_PTR, 0);
155aa4d7a8aSPoul-Henning Kamp 		uma_ufs2 = uma_zcreate("FFS2 dinode",
156aa4d7a8aSPoul-Henning Kamp 		    sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
157aa4d7a8aSPoul-Henning Kamp 		    UMA_ALIGN_PTR, 0);
158aa4d7a8aSPoul-Henning Kamp 	}
1598d02a378SPawel Jakub Dawidek 
16020a92a18SPoul-Henning Kamp 	fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
16151ac12abSPoul-Henning Kamp 	if (error)
162f2a2857bSKirk McKusick 		return (error);
1632b14f991SJulian Elischer 
1645da56ddbSTor Egge 	mntorflags = 0;
1655da56ddbSTor Egge 	mntandnotflags = 0;
16626f59b64SCraig Rodrigues 	if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
1675da56ddbSTor Egge 		mntorflags |= MNT_ACLS;
16826f59b64SCraig Rodrigues 
169fb77e0afSCraig Rodrigues 	if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
1705da56ddbSTor Egge 		mntorflags |= MNT_SNAPSHOT;
171fb77e0afSCraig Rodrigues 		/*
172fb77e0afSCraig Rodrigues 		 * Once we have set the MNT_SNAPSHOT flag, do not
173fb77e0afSCraig Rodrigues 		 * persist "snapshot" in the options list.
174fb77e0afSCraig Rodrigues 		 */
175fb77e0afSCraig Rodrigues 		vfs_deleteopt(mp->mnt_optnew, "snapshot");
176fb77e0afSCraig Rodrigues 		vfs_deleteopt(mp->mnt_opt, "snapshot");
177fb77e0afSCraig Rodrigues 	}
17826f59b64SCraig Rodrigues 
1795da56ddbSTor Egge 	MNT_ILOCK(mp);
1805da56ddbSTor Egge 	mp->mnt_flag = (mp->mnt_flag | mntorflags) & ~mntandnotflags;
1815da56ddbSTor Egge 	MNT_IUNLOCK(mp);
182df8bae1dSRodney W. Grimes 	/*
183df8bae1dSRodney W. Grimes 	 * If updating, check whether changing from read-only to
184df8bae1dSRodney W. Grimes 	 * read/write; if there is no device name, that's all we do.
185df8bae1dSRodney W. Grimes 	 */
186df8bae1dSRodney W. Grimes 	if (mp->mnt_flag & MNT_UPDATE) {
187df8bae1dSRodney W. Grimes 		ump = VFSTOUFS(mp);
188df8bae1dSRodney W. Grimes 		fs = ump->um_fs;
18926cf9c3bSPeter Wemm 		devvp = ump->um_devvp;
19020a92a18SPoul-Henning Kamp 		if (fs->fs_ronly == 0 &&
19120a92a18SPoul-Henning Kamp 		    vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
1926fecb4e4SKonstantin Belousov 			/*
1936fecb4e4SKonstantin Belousov 			 * Flush any dirty data and suspend filesystem.
1946fecb4e4SKonstantin Belousov 			 */
195f2a2857bSKirk McKusick 			if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
196f2a2857bSKirk McKusick 				return (error);
1976fecb4e4SKonstantin Belousov 			for (;;) {
1989ab73fd1SKirk McKusick 				vn_finished_write(mp);
1996fecb4e4SKonstantin Belousov 				if ((error = vfs_write_suspend(mp)) != 0)
2009ab73fd1SKirk McKusick 					return (error);
2016fecb4e4SKonstantin Belousov 				MNT_ILOCK(mp);
2026fecb4e4SKonstantin Belousov 				if (mp->mnt_kern_flag & MNTK_SUSPENDED) {
2036fecb4e4SKonstantin Belousov 					/*
2046fecb4e4SKonstantin Belousov 					 * Allow the secondary writes
2056fecb4e4SKonstantin Belousov 					 * to proceed.
2066fecb4e4SKonstantin Belousov 					 */
2076fecb4e4SKonstantin Belousov 					mp->mnt_kern_flag &= ~(MNTK_SUSPENDED |
2086fecb4e4SKonstantin Belousov 					    MNTK_SUSPEND2);
2096fecb4e4SKonstantin Belousov 					wakeup(&mp->mnt_flag);
2106fecb4e4SKonstantin Belousov 					MNT_IUNLOCK(mp);
2116fecb4e4SKonstantin Belousov 					/*
2126fecb4e4SKonstantin Belousov 					 * Allow the curthread to
2136fecb4e4SKonstantin Belousov 					 * ignore the suspension to
2146fecb4e4SKonstantin Belousov 					 * synchronize on-disk state.
2156fecb4e4SKonstantin Belousov 					 */
2166fecb4e4SKonstantin Belousov 					curthread->td_pflags |= TDP_IGNSUSP;
2176fecb4e4SKonstantin Belousov 					break;
2186fecb4e4SKonstantin Belousov 				}
2196fecb4e4SKonstantin Belousov 				MNT_IUNLOCK(mp);
2206fecb4e4SKonstantin Belousov 				vn_start_write(NULL, &mp, V_WAIT);
2219ab73fd1SKirk McKusick 			}
222cd600596SKirk McKusick 			/*
223cd600596SKirk McKusick 			 * Check for and optionally get rid of files open
224cd600596SKirk McKusick 			 * for writing.
225cd600596SKirk McKusick 			 */
226df8bae1dSRodney W. Grimes 			flags = WRITECLOSE;
227df8bae1dSRodney W. Grimes 			if (mp->mnt_flag & MNT_FORCE)
228df8bae1dSRodney W. Grimes 				flags |= FORCECLOSE;
229b1897c19SJulian Elischer 			if (mp->mnt_flag & MNT_SOFTDEP) {
230b40ce416SJulian Elischer 				error = softdep_flushfiles(mp, flags, td);
231b1897c19SJulian Elischer 			} else {
232b40ce416SJulian Elischer 				error = ffs_flushfiles(mp, flags, td);
233df8bae1dSRodney W. Grimes 			}
234f2a2857bSKirk McKusick 			if (error) {
2356fecb4e4SKonstantin Belousov 				vfs_write_resume(mp);
236f2a2857bSKirk McKusick 				return (error);
237b1897c19SJulian Elischer 			}
2389ccb939eSKirk McKusick 			if (fs->fs_pendingblocks != 0 ||
2399ccb939eSKirk McKusick 			    fs->fs_pendinginodes != 0) {
240cfbf0a46SMaxime Henrion 				printf("%s: %s: blocks %jd files %d\n",
2411c85e6a3SKirk McKusick 				    fs->fs_fsmnt, "update error",
2421c85e6a3SKirk McKusick 				    (intmax_t)fs->fs_pendingblocks,
2439ccb939eSKirk McKusick 				    fs->fs_pendinginodes);
2449ccb939eSKirk McKusick 				fs->fs_pendingblocks = 0;
2459ccb939eSKirk McKusick 				fs->fs_pendinginodes = 0;
2469ccb939eSKirk McKusick 			}
2471a6a6610SKirk McKusick 			if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
248f2a2857bSKirk McKusick 				fs->fs_clean = 1;
249791dd2faSTor Egge 			if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
250f2a2857bSKirk McKusick 				fs->fs_ronly = 0;
251f2a2857bSKirk McKusick 				fs->fs_clean = 0;
2526fecb4e4SKonstantin Belousov 				vfs_write_resume(mp);
253f2a2857bSKirk McKusick 				return (error);
2542b14f991SJulian Elischer 			}
25543920011SPoul-Henning Kamp 			DROP_GIANT();
25643920011SPoul-Henning Kamp 			g_topology_lock();
25743920011SPoul-Henning Kamp 			g_access(ump->um_cp, 0, -1, 0);
25843920011SPoul-Henning Kamp 			g_topology_unlock();
25943920011SPoul-Henning Kamp 			PICKUP_GIANT();
26020a92a18SPoul-Henning Kamp 			fs->fs_ronly = 1;
2615da56ddbSTor Egge 			MNT_ILOCK(mp);
26220a92a18SPoul-Henning Kamp 			mp->mnt_flag |= MNT_RDONLY;
2635da56ddbSTor Egge 			MNT_IUNLOCK(mp);
2646fecb4e4SKonstantin Belousov 			/*
2656fecb4e4SKonstantin Belousov 			 * Allow the writers to note that filesystem
2666fecb4e4SKonstantin Belousov 			 * is ro now.
2676fecb4e4SKonstantin Belousov 			 */
2686fecb4e4SKonstantin Belousov 			vfs_write_resume(mp);
269f2a2857bSKirk McKusick 		}
270f2a2857bSKirk McKusick 		if ((mp->mnt_flag & MNT_RELOAD) &&
2715e8c582aSPoul-Henning Kamp 		    (error = ffs_reload(mp, td)) != 0)
272f2a2857bSKirk McKusick 			return (error);
27320a92a18SPoul-Henning Kamp 		if (fs->fs_ronly &&
27420a92a18SPoul-Henning Kamp 		    !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
275c9b99213SBruce Evans 			/*
276c9b99213SBruce Evans 			 * If upgrade to read-write by non-root, then verify
277c9b99213SBruce Evans 			 * that user has necessary permissions on the device.
278c9b99213SBruce Evans 			 */
279cb05b60aSAttilio Rao 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
280acd3428bSRobert Watson 			error = VOP_ACCESS(devvp, VREAD | VWRITE,
281acd3428bSRobert Watson 			    td->td_ucred, td);
282acd3428bSRobert Watson 			if (error)
283acd3428bSRobert Watson 				error = priv_check(td, PRIV_VFS_MOUNT_PERM);
284acd3428bSRobert Watson 			if (error) {
28522db15c0SAttilio Rao 				VOP_UNLOCK(devvp, 0);
286c9b99213SBruce Evans 				return (error);
287c9b99213SBruce Evans 			}
28822db15c0SAttilio Rao 			VOP_UNLOCK(devvp, 0);
2897e58bfacSBruce Evans 			fs->fs_flags &= ~FS_UNCLEAN;
2900922cce6SBruce Evans 			if (fs->fs_clean == 0) {
2917e58bfacSBruce Evans 				fs->fs_flags |= FS_UNCLEAN;
292812b1d41SKirk McKusick 				if ((mp->mnt_flag & MNT_FORCE) ||
2931a6a6610SKirk McKusick 				    ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
2941a6a6610SKirk McKusick 				     (fs->fs_flags & FS_DOSOFTDEP))) {
295f2a2857bSKirk McKusick 					printf("WARNING: %s was not %s\n",
296f2a2857bSKirk McKusick 					   fs->fs_fsmnt, "properly dismounted");
2970922cce6SBruce Evans 				} else {
2980922cce6SBruce Evans 					printf(
2990922cce6SBruce Evans "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
3000922cce6SBruce Evans 					    fs->fs_fsmnt);
301f2a2857bSKirk McKusick 					return (EPERM);
3020922cce6SBruce Evans 				}
3030922cce6SBruce Evans 			}
30440c340aaSPoul-Henning Kamp 			DROP_GIANT();
30540c340aaSPoul-Henning Kamp 			g_topology_lock();
30640c340aaSPoul-Henning Kamp 			/*
30740c340aaSPoul-Henning Kamp 			 * If we're the root device, we may not have an E count
30840c340aaSPoul-Henning Kamp 			 * yet, get it now.
30940c340aaSPoul-Henning Kamp 			 */
31040c340aaSPoul-Henning Kamp 			if (ump->um_cp->ace == 0)
31140c340aaSPoul-Henning Kamp 				error = g_access(ump->um_cp, 0, 1, 1);
31240c340aaSPoul-Henning Kamp 			else
31340c340aaSPoul-Henning Kamp 				error = g_access(ump->um_cp, 0, 1, 0);
31440c340aaSPoul-Henning Kamp 			g_topology_unlock();
31540c340aaSPoul-Henning Kamp 			PICKUP_GIANT();
31640c340aaSPoul-Henning Kamp 			if (error)
31740c340aaSPoul-Henning Kamp 				return (error);
318f2a2857bSKirk McKusick 			if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
319f2a2857bSKirk McKusick 				return (error);
320f2a2857bSKirk McKusick 			fs->fs_ronly = 0;
3215da56ddbSTor Egge 			MNT_ILOCK(mp);
32220a92a18SPoul-Henning Kamp 			mp->mnt_flag &= ~MNT_RDONLY;
3235da56ddbSTor Egge 			MNT_IUNLOCK(mp);
324f2a2857bSKirk McKusick 			fs->fs_clean = 0;
325791dd2faSTor Egge 			if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
326f2a2857bSKirk McKusick 				vn_finished_write(mp);
327f2a2857bSKirk McKusick 				return (error);
328f2a2857bSKirk McKusick 			}
32926cf9c3bSPeter Wemm 			/* check to see if we need to start softdep */
330f2a2857bSKirk McKusick 			if ((fs->fs_flags & FS_DOSOFTDEP) &&
331a854ed98SJohn Baldwin 			    (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
332f2a2857bSKirk McKusick 				vn_finished_write(mp);
333f2a2857bSKirk McKusick 				return (error);
33426cf9c3bSPeter Wemm 			}
335f2a2857bSKirk McKusick 			if (fs->fs_snapinum[0] != 0)
336f2a2857bSKirk McKusick 				ffs_snapshot_mount(mp);
337f2a2857bSKirk McKusick 			vn_finished_write(mp);
3381469eec8SDavid Greenman 		}
339c11d2981SJulian Elischer 		/*
340c11d2981SJulian Elischer 		 * Soft updates is incompatible with "async",
341c11d2981SJulian Elischer 		 * so if we are doing softupdates stop the user
342c11d2981SJulian Elischer 		 * from setting the async flag in an update.
343c11d2981SJulian Elischer 		 * Softdep_mount() clears it in an initial mount
344c11d2981SJulian Elischer 		 * or ro->rw remount.
345c11d2981SJulian Elischer 		 */
3465da56ddbSTor Egge 		if (mp->mnt_flag & MNT_SOFTDEP) {
3475da56ddbSTor Egge 			/* XXX: Reset too late ? */
3485da56ddbSTor Egge 			MNT_ILOCK(mp);
349c11d2981SJulian Elischer 			mp->mnt_flag &= ~MNT_ASYNC;
3505da56ddbSTor Egge 			MNT_IUNLOCK(mp);
3515da56ddbSTor Egge 		}
352df8bae1dSRodney W. Grimes 		/*
35339cfb239SPawel Jakub Dawidek 		 * Keep MNT_ACLS flag if it is stored in superblock.
35439cfb239SPawel Jakub Dawidek 		 */
3555da56ddbSTor Egge 		if ((fs->fs_flags & FS_ACLS) != 0) {
3565da56ddbSTor Egge 			/* XXX: Set too late ? */
3575da56ddbSTor Egge 			MNT_ILOCK(mp);
35839cfb239SPawel Jakub Dawidek 			mp->mnt_flag |= MNT_ACLS;
3595da56ddbSTor Egge 			MNT_IUNLOCK(mp);
3605da56ddbSTor Egge 		}
361cea90362SCraig Rodrigues 
362f2a2857bSKirk McKusick 		/*
363f2a2857bSKirk McKusick 		 * If this is a snapshot request, take the snapshot.
364f2a2857bSKirk McKusick 		 */
365f2a2857bSKirk McKusick 		if (mp->mnt_flag & MNT_SNAPSHOT)
36620a92a18SPoul-Henning Kamp 			return (ffs_snapshot(mp, fspec));
367df8bae1dSRodney W. Grimes 	}
3682b14f991SJulian Elischer 
369df8bae1dSRodney W. Grimes 	/*
370df8bae1dSRodney W. Grimes 	 * Not an update, or updating the name: look up the name
371e9827c6dSBruce Evans 	 * and verify that it refers to a sensible disk device.
372df8bae1dSRodney W. Grimes 	 */
373fdedad76SSuleiman Souhlal 	NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
3745e8c582aSPoul-Henning Kamp 	if ((error = namei(&ndp)) != 0)
375f2a2857bSKirk McKusick 		return (error);
3765e8c582aSPoul-Henning Kamp 	NDFREE(&ndp, NDF_ONLY_PNBUF);
3775e8c582aSPoul-Henning Kamp 	devvp = ndp.ni_vp;
378f2a2857bSKirk McKusick 	if (!vn_isdisk(devvp, &error)) {
379fdedad76SSuleiman Souhlal 		vput(devvp);
380f2a2857bSKirk McKusick 		return (error);
381f2a2857bSKirk McKusick 	}
382c9b99213SBruce Evans 
383c9b99213SBruce Evans 	/*
384c9b99213SBruce Evans 	 * If mount by non-root, then verify that user has necessary
385c9b99213SBruce Evans 	 * permissions on the device.
386c9b99213SBruce Evans 	 */
387c9b99213SBruce Evans 	accessmode = VREAD;
388c9b99213SBruce Evans 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
389c9b99213SBruce Evans 		accessmode |= VWRITE;
390acd3428bSRobert Watson 	error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
391acd3428bSRobert Watson 	if (error)
392acd3428bSRobert Watson 		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
393acd3428bSRobert Watson 	if (error) {
394c9b99213SBruce Evans 		vput(devvp);
395c9b99213SBruce Evans 		return (error);
396c9b99213SBruce Evans 	}
397c9b99213SBruce Evans 
3982b14f991SJulian Elischer 	if (mp->mnt_flag & MNT_UPDATE) {
3992b14f991SJulian Elischer 		/*
400f2a2857bSKirk McKusick 		 * Update only
401f2a2857bSKirk McKusick 		 *
4023e425b96SJulian Elischer 		 * If it's not the same vnode, or at least the same device
4033e425b96SJulian Elischer 		 * then it's not correct.
4042b14f991SJulian Elischer 		 */
4052b14f991SJulian Elischer 
40643920011SPoul-Henning Kamp 		if (devvp->v_rdev != ump->um_devvp->v_rdev)
407f2a2857bSKirk McKusick 			error = EINVAL;	/* needs translation */
408fdedad76SSuleiman Souhlal 		vput(devvp);
409f2a2857bSKirk McKusick 		if (error)
410f2a2857bSKirk McKusick 			return (error);
4112b14f991SJulian Elischer 	} else {
4122b14f991SJulian Elischer 		/*
413f2a2857bSKirk McKusick 		 * New mount
4142b14f991SJulian Elischer 		 *
415f2a2857bSKirk McKusick 		 * We need the name for the mount point (also used for
416f2a2857bSKirk McKusick 		 * "last mounted on") copied in. If an error occurs,
417f2a2857bSKirk McKusick 		 * the mount point is discarded by the upper level code.
418f3a90da9SAdrian Chadd 		 * Note that vfs_mount() populates f_mntonname for us.
419f2a2857bSKirk McKusick 		 */
420975512a9SPoul-Henning Kamp 		if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
421f2a2857bSKirk McKusick 			vrele(devvp);
422f2a2857bSKirk McKusick 			return (error);
423f2a2857bSKirk McKusick 		}
424f2a2857bSKirk McKusick 	}
42520a92a18SPoul-Henning Kamp 	vfs_mountedfrom(mp, fspec);
426f2a2857bSKirk McKusick 	return (0);
4272b14f991SJulian Elischer }
4282b14f991SJulian Elischer 
429df8bae1dSRodney W. Grimes /*
43020a92a18SPoul-Henning Kamp  * Compatibility with old mount system call.
43120a92a18SPoul-Henning Kamp  */
43220a92a18SPoul-Henning Kamp 
43320a92a18SPoul-Henning Kamp static int
43420a92a18SPoul-Henning Kamp ffs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
43520a92a18SPoul-Henning Kamp {
43620a92a18SPoul-Henning Kamp 	struct ufs_args args;
43720a92a18SPoul-Henning Kamp 	int error;
43820a92a18SPoul-Henning Kamp 
43920a92a18SPoul-Henning Kamp 	if (data == NULL)
44020a92a18SPoul-Henning Kamp 		return (EINVAL);
44120a92a18SPoul-Henning Kamp 	error = copyin(data, &args, sizeof args);
44220a92a18SPoul-Henning Kamp 	if (error)
44320a92a18SPoul-Henning Kamp 		return (error);
44420a92a18SPoul-Henning Kamp 
44520a92a18SPoul-Henning Kamp 	ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
44620a92a18SPoul-Henning Kamp 	ma = mount_arg(ma, "export", &args.export, sizeof args.export);
44720a92a18SPoul-Henning Kamp 	error = kernel_mount(ma, flags);
44820a92a18SPoul-Henning Kamp 
44920a92a18SPoul-Henning Kamp 	return (error);
45020a92a18SPoul-Henning Kamp }
45120a92a18SPoul-Henning Kamp 
45220a92a18SPoul-Henning Kamp /*
453df8bae1dSRodney W. Grimes  * Reload all incore data for a filesystem (used after running fsck on
454df8bae1dSRodney W. Grimes  * the root filesystem and finding things to fix). The filesystem must
455df8bae1dSRodney W. Grimes  * be mounted read-only.
456df8bae1dSRodney W. Grimes  *
457df8bae1dSRodney W. Grimes  * Things to do to update the mount:
458df8bae1dSRodney W. Grimes  *	1) invalidate all cached meta-data.
459df8bae1dSRodney W. Grimes  *	2) re-read superblock from disk.
460df8bae1dSRodney W. Grimes  *	3) re-read summary information from disk.
461df8bae1dSRodney W. Grimes  *	4) invalidate all inactive vnodes.
462df8bae1dSRodney W. Grimes  *	5) invalidate all cached file data.
463df8bae1dSRodney W. Grimes  *	6) re-read inode data for all active vnodes.
464df8bae1dSRodney W. Grimes  */
4655e8c582aSPoul-Henning Kamp static int
4665e8c582aSPoul-Henning Kamp ffs_reload(struct mount *mp, struct thread *td)
467df8bae1dSRodney W. Grimes {
46882be0a5aSTor Egge 	struct vnode *vp, *mvp, *devvp;
469df8bae1dSRodney W. Grimes 	struct inode *ip;
470f55ff3f3SIan Dowse 	void *space;
471df8bae1dSRodney W. Grimes 	struct buf *bp;
472996c772fSJohn Dyson 	struct fs *fs, *newfs;
4733ba649d7SJeff Roberson 	struct ufsmount *ump;
4741c85e6a3SKirk McKusick 	ufs2_daddr_t sblockloc;
475df8bae1dSRodney W. Grimes 	int i, blks, size, error;
476996c772fSJohn Dyson 	int32_t *lp;
477df8bae1dSRodney W. Grimes 
4782b14f991SJulian Elischer 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
479df8bae1dSRodney W. Grimes 		return (EINVAL);
4803ba649d7SJeff Roberson 	ump = VFSTOUFS(mp);
481df8bae1dSRodney W. Grimes 	/*
482df8bae1dSRodney W. Grimes 	 * Step 1: invalidate all cached meta-data.
483df8bae1dSRodney W. Grimes 	 */
4842b14f991SJulian Elischer 	devvp = VFSTOUFS(mp)->um_devvp;
485cb05b60aSAttilio Rao 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
4867c0745eeSPoul-Henning Kamp 	if (vinvalbuf(devvp, 0, td, 0, 0) != 0)
487df8bae1dSRodney W. Grimes 		panic("ffs_reload: dirty1");
48822db15c0SAttilio Rao 	VOP_UNLOCK(devvp, 0);
48995e5e988SJohn Dyson 
490df8bae1dSRodney W. Grimes 	/*
491df8bae1dSRodney W. Grimes 	 * Step 2: re-read superblock from disk.
492df8bae1dSRodney W. Grimes 	 */
4931c85e6a3SKirk McKusick 	fs = VFSTOUFS(mp)->um_fs;
494ada981b2SKirk McKusick 	if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
4951c85e6a3SKirk McKusick 	    NOCRED, &bp)) != 0)
496df8bae1dSRodney W. Grimes 		return (error);
497996c772fSJohn Dyson 	newfs = (struct fs *)bp->b_data;
4981c85e6a3SKirk McKusick 	if ((newfs->fs_magic != FS_UFS1_MAGIC &&
4991c85e6a3SKirk McKusick 	     newfs->fs_magic != FS_UFS2_MAGIC) ||
5001c85e6a3SKirk McKusick 	    newfs->fs_bsize > MAXBSIZE ||
501996c772fSJohn Dyson 	    newfs->fs_bsize < sizeof(struct fs)) {
502df8bae1dSRodney W. Grimes 			brelse(bp);
503df8bae1dSRodney W. Grimes 			return (EIO);		/* XXX needs translation */
504df8bae1dSRodney W. Grimes 	}
505996c772fSJohn Dyson 	/*
506996c772fSJohn Dyson 	 * Copy pointer fields back into superblock before copying in	XXX
507996c772fSJohn Dyson 	 * new superblock. These should really be in the ufsmount.	XXX
508996c772fSJohn Dyson 	 * Note that important parameters (eg fs_ncg) are unchanged.
509996c772fSJohn Dyson 	 */
510f55ff3f3SIan Dowse 	newfs->fs_csp = fs->fs_csp;
511996c772fSJohn Dyson 	newfs->fs_maxcluster = fs->fs_maxcluster;
5125d69bac4SIan Dowse 	newfs->fs_contigdirs = fs->fs_contigdirs;
513143a5346SIan Dowse 	newfs->fs_active = fs->fs_active;
51431c81e4bSDon Lewis 	/* The file system is still read-only. */
51531c81e4bSDon Lewis 	newfs->fs_ronly = 1;
5161c85e6a3SKirk McKusick 	sblockloc = fs->fs_sblockloc;
517996c772fSJohn Dyson 	bcopy(newfs, fs, (u_int)fs->fs_sbsize);
518df8bae1dSRodney W. Grimes 	brelse(bp);
519996c772fSJohn Dyson 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
5201c85e6a3SKirk McKusick 	ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
5213ba649d7SJeff Roberson 	UFS_LOCK(ump);
5229ccb939eSKirk McKusick 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
523cfbf0a46SMaxime Henrion 		printf("%s: reload pending error: blocks %jd files %d\n",
5241c85e6a3SKirk McKusick 		    fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
5251c85e6a3SKirk McKusick 		    fs->fs_pendinginodes);
5269ccb939eSKirk McKusick 		fs->fs_pendingblocks = 0;
5279ccb939eSKirk McKusick 		fs->fs_pendinginodes = 0;
5289ccb939eSKirk McKusick 	}
5293ba649d7SJeff Roberson 	UFS_UNLOCK(ump);
530996c772fSJohn Dyson 
531df8bae1dSRodney W. Grimes 	/*
532df8bae1dSRodney W. Grimes 	 * Step 3: re-read summary information from disk.
533df8bae1dSRodney W. Grimes 	 */
534df8bae1dSRodney W. Grimes 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
535f55ff3f3SIan Dowse 	space = fs->fs_csp;
536df8bae1dSRodney W. Grimes 	for (i = 0; i < blks; i += fs->fs_frag) {
537df8bae1dSRodney W. Grimes 		size = fs->fs_bsize;
538df8bae1dSRodney W. Grimes 		if (i + fs->fs_frag > blks)
539df8bae1dSRodney W. Grimes 			size = (blks - i) * fs->fs_fsize;
540c9671602SPoul-Henning Kamp 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
541c9671602SPoul-Henning Kamp 		    NOCRED, &bp);
542c9671602SPoul-Henning Kamp 		if (error)
543df8bae1dSRodney W. Grimes 			return (error);
544f55ff3f3SIan Dowse 		bcopy(bp->b_data, space, (u_int)size);
545f55ff3f3SIan Dowse 		space = (char *)space + size;
546df8bae1dSRodney W. Grimes 		brelse(bp);
547df8bae1dSRodney W. Grimes 	}
548996c772fSJohn Dyson 	/*
549996c772fSJohn Dyson 	 * We no longer know anything about clusters per cylinder group.
550996c772fSJohn Dyson 	 */
551996c772fSJohn Dyson 	if (fs->fs_contigsumsize > 0) {
552996c772fSJohn Dyson 		lp = fs->fs_maxcluster;
553996c772fSJohn Dyson 		for (i = 0; i < fs->fs_ncg; i++)
554996c772fSJohn Dyson 			*lp++ = fs->fs_contigsumsize;
555996c772fSJohn Dyson 	}
556996c772fSJohn Dyson 
557df8bae1dSRodney W. Grimes loop:
558ca430f2eSAlexander Kabaev 	MNT_ILOCK(mp);
55982be0a5aSTor Egge 	MNT_VNODE_FOREACH(vp, mp, mvp) {
56004a17687SJeff Roberson 		VI_LOCK(vp);
561fe68abe2SJeff Roberson 		if (vp->v_iflag & VI_DOOMED) {
5622f05568aSJeff Roberson 			VI_UNLOCK(vp);
5632f05568aSJeff Roberson 			continue;
5642f05568aSJeff Roberson 		}
565ca430f2eSAlexander Kabaev 		MNT_IUNLOCK(mp);
566df8bae1dSRodney W. Grimes 		/*
567fe68abe2SJeff Roberson 		 * Step 4: invalidate all cached file data.
568df8bae1dSRodney W. Grimes 		 */
569b40ce416SJulian Elischer 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
57082be0a5aSTor Egge 			MNT_VNODE_FOREACH_ABORT(mp, mvp);
571df8bae1dSRodney W. Grimes 			goto loop;
572996c772fSJohn Dyson 		}
5737c0745eeSPoul-Henning Kamp 		if (vinvalbuf(vp, 0, td, 0, 0))
574df8bae1dSRodney W. Grimes 			panic("ffs_reload: dirty2");
575df8bae1dSRodney W. Grimes 		/*
576fe68abe2SJeff Roberson 		 * Step 5: re-read inode data for all active vnodes.
577df8bae1dSRodney W. Grimes 		 */
578df8bae1dSRodney W. Grimes 		ip = VTOI(vp);
579c9671602SPoul-Henning Kamp 		error =
580df8bae1dSRodney W. Grimes 		    bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
581c9671602SPoul-Henning Kamp 		    (int)fs->fs_bsize, NOCRED, &bp);
582c9671602SPoul-Henning Kamp 		if (error) {
58322db15c0SAttilio Rao 			VOP_UNLOCK(vp, 0);
58445d45c6cSAlexander Kabaev 			vrele(vp);
58582be0a5aSTor Egge 			MNT_VNODE_FOREACH_ABORT(mp, mvp);
586df8bae1dSRodney W. Grimes 			return (error);
587df8bae1dSRodney W. Grimes 		}
588de6ba7c0SPoul-Henning Kamp 		ffs_load_inode(bp, ip, fs, ip->i_number);
589b1897c19SJulian Elischer 		ip->i_effnlink = ip->i_nlink;
590df8bae1dSRodney W. Grimes 		brelse(bp);
59122db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
592cb9ddc80SAlexander Kabaev 		vrele(vp);
593ca430f2eSAlexander Kabaev 		MNT_ILOCK(mp);
594df8bae1dSRodney W. Grimes 	}
595ca430f2eSAlexander Kabaev 	MNT_IUNLOCK(mp);
596df8bae1dSRodney W. Grimes 	return (0);
597df8bae1dSRodney W. Grimes }
598df8bae1dSRodney W. Grimes 
5991c85e6a3SKirk McKusick /*
6001c85e6a3SKirk McKusick  * Possible superblock locations ordered from most to least likely.
6011c85e6a3SKirk McKusick  */
6021c85e6a3SKirk McKusick static int sblock_try[] = SBLOCKSEARCH;
6035819ab3fSKirk McKusick 
604df8bae1dSRodney W. Grimes /*
605df8bae1dSRodney W. Grimes  * Common code for mount and mountroot
606df8bae1dSRodney W. Grimes  */
607975512a9SPoul-Henning Kamp static int
608975512a9SPoul-Henning Kamp ffs_mountfs(devvp, mp, td)
60905f4ff5dSPoul-Henning Kamp 	struct vnode *devvp;
610df8bae1dSRodney W. Grimes 	struct mount *mp;
611b40ce416SJulian Elischer 	struct thread *td;
612df8bae1dSRodney W. Grimes {
61305f4ff5dSPoul-Henning Kamp 	struct ufsmount *ump;
614df8bae1dSRodney W. Grimes 	struct buf *bp;
61505f4ff5dSPoul-Henning Kamp 	struct fs *fs;
61689c9c53dSPoul-Henning Kamp 	struct cdev *dev;
617f55ff3f3SIan Dowse 	void *space;
6181c85e6a3SKirk McKusick 	ufs2_daddr_t sblockloc;
619f5ef029eSPoul-Henning Kamp 	int error, i, blks, size, ronly;
620996c772fSJohn Dyson 	int32_t *lp;
621996c772fSJohn Dyson 	struct ucred *cred;
62243920011SPoul-Henning Kamp 	struct g_consumer *cp;
6233bbd6d8aSJeff Roberson 	struct mount *nmp;
624df8bae1dSRodney W. Grimes 
625996c772fSJohn Dyson 	dev = devvp->v_rdev;
626a854ed98SJohn Baldwin 	cred = td ? td->td_ucred : NOCRED;
62743920011SPoul-Henning Kamp 
62843920011SPoul-Henning Kamp 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
62943920011SPoul-Henning Kamp 	DROP_GIANT();
63043920011SPoul-Henning Kamp 	g_topology_lock();
63143920011SPoul-Henning Kamp 	error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
63243920011SPoul-Henning Kamp 
63343920011SPoul-Henning Kamp 	/*
63443920011SPoul-Henning Kamp 	 * If we are a root mount, drop the E flag so fsck can do its magic.
63593e0b506SPoul-Henning Kamp 	 * We will pick it up again when we remount R/W.
63643920011SPoul-Henning Kamp 	 */
63743920011SPoul-Henning Kamp 	if (error == 0 && ronly && (mp->mnt_flag & MNT_ROOTFS))
63843920011SPoul-Henning Kamp 		error = g_access(cp, 0, 0, -1);
63943920011SPoul-Henning Kamp 	g_topology_unlock();
64043920011SPoul-Henning Kamp 	PICKUP_GIANT();
64122db15c0SAttilio Rao 	VOP_UNLOCK(devvp, 0);
642c9671602SPoul-Henning Kamp 	if (error)
643df8bae1dSRodney W. Grimes 		return (error);
6440508986cSBruce Evans 	if (devvp->v_rdev->si_iosize_max != 0)
6451b5464efSPoul-Henning Kamp 		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
6461b5464efSPoul-Henning Kamp 	if (mp->mnt_iosize_max > MAXPHYS)
6471b5464efSPoul-Henning Kamp 		mp->mnt_iosize_max = MAXPHYS;
64895e5e988SJohn Dyson 
64943920011SPoul-Henning Kamp 	devvp->v_bufobj.bo_private = cp;
6506e77a041SPoul-Henning Kamp 	devvp->v_bufobj.bo_ops = &ffs_ops;
6516e77a041SPoul-Henning Kamp 
652df8bae1dSRodney W. Grimes 	bp = NULL;
653df8bae1dSRodney W. Grimes 	ump = NULL;
6541c85e6a3SKirk McKusick 	fs = NULL;
6551c85e6a3SKirk McKusick 	sblockloc = 0;
6561c85e6a3SKirk McKusick 	/*
6571c85e6a3SKirk McKusick 	 * Try reading the superblock in each of its possible locations.
6581c85e6a3SKirk McKusick 	 */
6591c85e6a3SKirk McKusick 	for (i = 0; sblock_try[i] != -1; i++) {
66071ac2d7cSCraig Rodrigues 		if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
66171ac2d7cSCraig Rodrigues 			error = EINVAL;
66271ac2d7cSCraig Rodrigues 			vfs_mount_error(mp,
66371ac2d7cSCraig Rodrigues 			    "Invalid sectorsize %d for superblock size %d",
66471ac2d7cSCraig Rodrigues 			    cp->provider->sectorsize, SBLOCKSIZE);
66571ac2d7cSCraig Rodrigues 			goto out;
66671ac2d7cSCraig Rodrigues 		}
66771ac2d7cSCraig Rodrigues 		if ((error = bread(devvp, btodb(sblock_try[i]), SBLOCKSIZE,
6681c85e6a3SKirk McKusick 		    cred, &bp)) != 0)
669df8bae1dSRodney W. Grimes 			goto out;
670df8bae1dSRodney W. Grimes 		fs = (struct fs *)bp->b_data;
671ada981b2SKirk McKusick 		sblockloc = sblock_try[i];
6721c85e6a3SKirk McKusick 		if ((fs->fs_magic == FS_UFS1_MAGIC ||
6731c85e6a3SKirk McKusick 		     (fs->fs_magic == FS_UFS2_MAGIC &&
674ada981b2SKirk McKusick 		      (fs->fs_sblockloc == sblockloc ||
675ada981b2SKirk McKusick 		       (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) &&
6761c85e6a3SKirk McKusick 		    fs->fs_bsize <= MAXBSIZE &&
6771c85e6a3SKirk McKusick 		    fs->fs_bsize >= sizeof(struct fs))
6781c85e6a3SKirk McKusick 			break;
6791c85e6a3SKirk McKusick 		brelse(bp);
6801c85e6a3SKirk McKusick 		bp = NULL;
6811c85e6a3SKirk McKusick 	}
6821c85e6a3SKirk McKusick 	if (sblock_try[i] == -1) {
683df8bae1dSRodney W. Grimes 		error = EINVAL;		/* XXX needs translation */
684df8bae1dSRodney W. Grimes 		goto out;
685df8bae1dSRodney W. Grimes 	}
6863f6f17eeSJulian Elischer 	fs->fs_fmod = 0;
6871c85e6a3SKirk McKusick 	fs->fs_flags &= ~FS_INDEXDIRS;	/* no support for directory indicies */
6880922cce6SBruce Evans 	fs->fs_flags &= ~FS_UNCLEAN;
6890922cce6SBruce Evans 	if (fs->fs_clean == 0) {
6900922cce6SBruce Evans 		fs->fs_flags |= FS_UNCLEAN;
691812b1d41SKirk McKusick 		if (ronly || (mp->mnt_flag & MNT_FORCE) ||
6921a6a6610SKirk McKusick 		    ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
6931a6a6610SKirk McKusick 		     (fs->fs_flags & FS_DOSOFTDEP))) {
6940922cce6SBruce Evans 			printf(
6950922cce6SBruce Evans "WARNING: %s was not properly dismounted\n",
6960922cce6SBruce Evans 			    fs->fs_fsmnt);
6971469eec8SDavid Greenman 		} else {
6980922cce6SBruce Evans 			printf(
6990922cce6SBruce Evans "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
7000922cce6SBruce Evans 			    fs->fs_fsmnt);
7011469eec8SDavid Greenman 			error = EPERM;
7021469eec8SDavid Greenman 			goto out;
7031469eec8SDavid Greenman 		}
7041c85e6a3SKirk McKusick 		if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
7051c85e6a3SKirk McKusick 		    (mp->mnt_flag & MNT_FORCE)) {
706cfbf0a46SMaxime Henrion 			printf("%s: lost blocks %jd files %d\n", fs->fs_fsmnt,
7071c85e6a3SKirk McKusick 			    (intmax_t)fs->fs_pendingblocks,
7081c85e6a3SKirk McKusick 			    fs->fs_pendinginodes);
7099ccb939eSKirk McKusick 			fs->fs_pendingblocks = 0;
7109ccb939eSKirk McKusick 			fs->fs_pendinginodes = 0;
7119ccb939eSKirk McKusick 		}
7129ccb939eSKirk McKusick 	}
7139ccb939eSKirk McKusick 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
714cfbf0a46SMaxime Henrion 		printf("%s: mount pending error: blocks %jd files %d\n",
7151c85e6a3SKirk McKusick 		    fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
7161c85e6a3SKirk McKusick 		    fs->fs_pendinginodes);
7179ccb939eSKirk McKusick 		fs->fs_pendingblocks = 0;
7189ccb939eSKirk McKusick 		fs->fs_pendinginodes = 0;
7191469eec8SDavid Greenman 	}
7201a60c7fcSPawel Jakub Dawidek 	if ((fs->fs_flags & FS_GJOURNAL) != 0) {
7211a60c7fcSPawel Jakub Dawidek #ifdef UFS_GJOURNAL
7221a60c7fcSPawel Jakub Dawidek 		/*
7231a60c7fcSPawel Jakub Dawidek 		 * Get journal provider name.
7241a60c7fcSPawel Jakub Dawidek 		 */
7251a60c7fcSPawel Jakub Dawidek 		size = 1024;
7261a60c7fcSPawel Jakub Dawidek 		mp->mnt_gjprovider = malloc(size, M_UFSMNT, M_WAITOK);
7271a60c7fcSPawel Jakub Dawidek 		if (g_io_getattr("GJOURNAL::provider", cp, &size,
7281a60c7fcSPawel Jakub Dawidek 		    mp->mnt_gjprovider) == 0) {
7291a60c7fcSPawel Jakub Dawidek 			mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, size,
7301a60c7fcSPawel Jakub Dawidek 			    M_UFSMNT, M_WAITOK);
7311a60c7fcSPawel Jakub Dawidek 			MNT_ILOCK(mp);
7321a60c7fcSPawel Jakub Dawidek 			mp->mnt_flag |= MNT_GJOURNAL;
7331a60c7fcSPawel Jakub Dawidek 			MNT_IUNLOCK(mp);
7341a60c7fcSPawel Jakub Dawidek 		} else {
7351a60c7fcSPawel Jakub Dawidek 			printf(
7361a60c7fcSPawel Jakub Dawidek "WARNING: %s: GJOURNAL flag on fs but no gjournal provider below\n",
7371a60c7fcSPawel Jakub Dawidek 			    mp->mnt_stat.f_mntonname);
7381a60c7fcSPawel Jakub Dawidek 			free(mp->mnt_gjprovider, M_UFSMNT);
7391a60c7fcSPawel Jakub Dawidek 			mp->mnt_gjprovider = NULL;
7401a60c7fcSPawel Jakub Dawidek 		}
7411a60c7fcSPawel Jakub Dawidek #else
7421a60c7fcSPawel Jakub Dawidek 		printf(
7431a60c7fcSPawel Jakub Dawidek "WARNING: %s: GJOURNAL flag on fs but no UFS_GJOURNAL support\n",
7441a60c7fcSPawel Jakub Dawidek 		    mp->mnt_stat.f_mntonname);
7451a60c7fcSPawel Jakub Dawidek #endif
7461a60c7fcSPawel Jakub Dawidek 	} else {
7471a60c7fcSPawel Jakub Dawidek 		mp->mnt_gjprovider = NULL;
7481a60c7fcSPawel Jakub Dawidek 	}
749a163d034SWarner Losh 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
75043920011SPoul-Henning Kamp 	ump->um_cp = cp;
75143920011SPoul-Henning Kamp 	ump->um_bo = &devvp->v_bufobj;
7528dd56505SPoul-Henning Kamp 	ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK);
7531c85e6a3SKirk McKusick 	if (fs->fs_magic == FS_UFS1_MAGIC) {
7541c85e6a3SKirk McKusick 		ump->um_fstype = UFS1;
7551c85e6a3SKirk McKusick 		ump->um_balloc = ffs_balloc_ufs1;
7561c85e6a3SKirk McKusick 	} else {
7571c85e6a3SKirk McKusick 		ump->um_fstype = UFS2;
7581c85e6a3SKirk McKusick 		ump->um_balloc = ffs_balloc_ufs2;
7591c85e6a3SKirk McKusick 	}
760cec0f20cSPoul-Henning Kamp 	ump->um_blkatoff = ffs_blkatoff;
761cec0f20cSPoul-Henning Kamp 	ump->um_truncate = ffs_truncate;
762987f5696SPoul-Henning Kamp 	ump->um_update = ffs_update;
763cec0f20cSPoul-Henning Kamp 	ump->um_valloc = ffs_valloc;
764cec0f20cSPoul-Henning Kamp 	ump->um_vfree = ffs_vfree;
765975512a9SPoul-Henning Kamp 	ump->um_ifree = ffs_ifree;
76690446e36SKonstantin Belousov 	ump->um_rdonly = ffs_rdonly;
7673ba649d7SJeff Roberson 	mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
768df8bae1dSRodney W. Grimes 	bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
7691c85e6a3SKirk McKusick 	if (fs->fs_sbsize < SBLOCKSIZE)
770f2a2857bSKirk McKusick 		bp->b_flags |= B_INVAL | B_NOCACHE;
771df8bae1dSRodney W. Grimes 	brelse(bp);
772df8bae1dSRodney W. Grimes 	bp = NULL;
773df8bae1dSRodney W. Grimes 	fs = ump->um_fs;
7741c85e6a3SKirk McKusick 	ffs_oldfscompat_read(fs, ump, sblockloc);
775df8bae1dSRodney W. Grimes 	fs->fs_ronly = ronly;
776996c772fSJohn Dyson 	size = fs->fs_cssize;
777996c772fSJohn Dyson 	blks = howmany(size, fs->fs_fsize);
778996c772fSJohn Dyson 	if (fs->fs_contigsumsize > 0)
779996c772fSJohn Dyson 		size += fs->fs_ncg * sizeof(int32_t);
780a61ab64aSKirk McKusick 	size += fs->fs_ncg * sizeof(u_int8_t);
781a163d034SWarner Losh 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
782f55ff3f3SIan Dowse 	fs->fs_csp = space;
783df8bae1dSRodney W. Grimes 	for (i = 0; i < blks; i += fs->fs_frag) {
784df8bae1dSRodney W. Grimes 		size = fs->fs_bsize;
785df8bae1dSRodney W. Grimes 		if (i + fs->fs_frag > blks)
786df8bae1dSRodney W. Grimes 			size = (blks - i) * fs->fs_fsize;
7878aef1712SMatthew Dillon 		if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
7888aef1712SMatthew Dillon 		    cred, &bp)) != 0) {
789f55ff3f3SIan Dowse 			free(fs->fs_csp, M_UFSMNT);
790df8bae1dSRodney W. Grimes 			goto out;
791df8bae1dSRodney W. Grimes 		}
792df8bae1dSRodney W. Grimes 		bcopy(bp->b_data, space, (u_int)size);
793f55ff3f3SIan Dowse 		space = (char *)space + size;
794df8bae1dSRodney W. Grimes 		brelse(bp);
795df8bae1dSRodney W. Grimes 		bp = NULL;
796df8bae1dSRodney W. Grimes 	}
797996c772fSJohn Dyson 	if (fs->fs_contigsumsize > 0) {
798f55ff3f3SIan Dowse 		fs->fs_maxcluster = lp = space;
799996c772fSJohn Dyson 		for (i = 0; i < fs->fs_ncg; i++)
800996c772fSJohn Dyson 			*lp++ = fs->fs_contigsumsize;
8014691e9eaSIan Dowse 		space = lp;
802996c772fSJohn Dyson 	}
803a61ab64aSKirk McKusick 	size = fs->fs_ncg * sizeof(u_int8_t);
804a61ab64aSKirk McKusick 	fs->fs_contigdirs = (u_int8_t *)space;
805a61ab64aSKirk McKusick 	bzero(fs->fs_contigdirs, size);
806143a5346SIan Dowse 	fs->fs_active = NULL;
80777465d93SAlfred Perlstein 	mp->mnt_data = ump;
80868de329eSPoul-Henning Kamp 	mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
8098f89943eSGuido van Rooij 	mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
8103bbd6d8aSJeff Roberson 	nmp = NULL;
81168de329eSPoul-Henning Kamp 	if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
8123bbd6d8aSJeff Roberson 	    (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
8133bbd6d8aSJeff Roberson 		if (nmp)
8143bbd6d8aSJeff Roberson 			vfs_rel(nmp);
81568de329eSPoul-Henning Kamp 		vfs_getnewfsid(mp);
8163bbd6d8aSJeff Roberson 	}
817df8bae1dSRodney W. Grimes 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
8185da56ddbSTor Egge 	MNT_ILOCK(mp);
819cc9d8990SPeter Wemm 	mp->mnt_flag |= MNT_LOCAL;
8205da56ddbSTor Egge 	MNT_IUNLOCK(mp);
8215da56ddbSTor Egge 	if ((fs->fs_flags & FS_MULTILABEL) != 0) {
8227b3f1bbdSTom Rhodes #ifdef MAC
8235da56ddbSTor Egge 		MNT_ILOCK(mp);
82480830407SRobert Watson 		mp->mnt_flag |= MNT_MULTILABEL;
8255da56ddbSTor Egge 		MNT_IUNLOCK(mp);
8267b3f1bbdSTom Rhodes #else
8277b3f1bbdSTom Rhodes 		printf(
8287b3f1bbdSTom Rhodes "WARNING: %s: multilabel flag on fs but no MAC support\n",
829946478fcSPawel Jakub Dawidek 		    mp->mnt_stat.f_mntonname);
8307b3f1bbdSTom Rhodes #endif
8315da56ddbSTor Egge 	}
8325da56ddbSTor Egge 	if ((fs->fs_flags & FS_ACLS) != 0) {
8337b3f1bbdSTom Rhodes #ifdef UFS_ACL
8345da56ddbSTor Egge 		MNT_ILOCK(mp);
83580830407SRobert Watson 		mp->mnt_flag |= MNT_ACLS;
8365da56ddbSTor Egge 		MNT_IUNLOCK(mp);
8377b3f1bbdSTom Rhodes #else
8387b3f1bbdSTom Rhodes 		printf(
8397b3f1bbdSTom Rhodes "WARNING: %s: ACLs flag on fs but no ACLs support\n",
840946478fcSPawel Jakub Dawidek 		    mp->mnt_stat.f_mntonname);
8417b3f1bbdSTom Rhodes #endif
8425da56ddbSTor Egge 	}
843df8bae1dSRodney W. Grimes 	ump->um_mountp = mp;
844df8bae1dSRodney W. Grimes 	ump->um_dev = dev;
845df8bae1dSRodney W. Grimes 	ump->um_devvp = devvp;
846df8bae1dSRodney W. Grimes 	ump->um_nindir = fs->fs_nindir;
847df8bae1dSRodney W. Grimes 	ump->um_bptrtodb = fs->fs_fsbtodb;
848df8bae1dSRodney W. Grimes 	ump->um_seqinc = fs->fs_frag;
849df8bae1dSRodney W. Grimes 	for (i = 0; i < MAXQUOTAS; i++)
850df8bae1dSRodney W. Grimes 		ump->um_quotas[i] = NULLVP;
851516081f2SRobert Watson #ifdef UFS_EXTATTR
852a64ed089SRobert Watson 	ufs_extattr_uepm_init(&ump->um_extattr);
853a64ed089SRobert Watson #endif
8542b14f991SJulian Elischer 	/*
8552b14f991SJulian Elischer 	 * Set FS local "last mounted on" information (NULL pad)
8562b14f991SJulian Elischer 	 */
85793373c42SSuleiman Souhlal 	bzero(fs->fs_fsmnt, MAXMNTLEN);
85893373c42SSuleiman Souhlal 	strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
8592b14f991SJulian Elischer 
8602b14f991SJulian Elischer 	if( mp->mnt_flag & MNT_ROOTFS) {
8612b14f991SJulian Elischer 		/*
8622b14f991SJulian Elischer 		 * Root mount; update timestamp in mount structure.
8632b14f991SJulian Elischer 		 * this will be used by the common root mount code
8642b14f991SJulian Elischer 		 * to update the system clock.
8652b14f991SJulian Elischer 		 */
8662b14f991SJulian Elischer 		mp->mnt_time = fs->fs_time;
8672b14f991SJulian Elischer 	}
868996c772fSJohn Dyson 
869996c772fSJohn Dyson 	if (ronly == 0) {
870b1897c19SJulian Elischer 		if ((fs->fs_flags & FS_DOSOFTDEP) &&
871b1897c19SJulian Elischer 		    (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
872f55ff3f3SIan Dowse 			free(fs->fs_csp, M_UFSMNT);
873b1897c19SJulian Elischer 			goto out;
874b1897c19SJulian Elischer 		}
875f2a2857bSKirk McKusick 		if (fs->fs_snapinum[0] != 0)
876f2a2857bSKirk McKusick 			ffs_snapshot_mount(mp);
877cf60e8e4SKirk McKusick 		fs->fs_fmod = 1;
878996c772fSJohn Dyson 		fs->fs_clean = 0;
879791dd2faSTor Egge 		(void) ffs_sbupdate(ump, MNT_WAIT, 0);
880996c772fSJohn Dyson 	}
881d8d3d415SPoul-Henning Kamp 	/*
882d8d3d415SPoul-Henning Kamp 	 * Initialize filesystem stat information in mount struct.
883d8d3d415SPoul-Henning Kamp 	 */
8846cf7bc60SRobert Watson 	MNT_ILOCK(mp);
8856cf7bc60SRobert Watson 	mp->mnt_kern_flag |= MNTK_MPSAFE;
8866cf7bc60SRobert Watson 	MNT_IUNLOCK(mp);
887516081f2SRobert Watson #ifdef UFS_EXTATTR
888516081f2SRobert Watson #ifdef UFS_EXTATTR_AUTOSTART
8899de54ba5SRobert Watson 	/*
8909de54ba5SRobert Watson 	 *
891f5161237SRobert Watson 	 * Auto-starting does the following:
8929de54ba5SRobert Watson 	 *	- check for /.attribute in the fs, and extattr_start if so
8939de54ba5SRobert Watson 	 *	- for each file in .attribute, enable that file with
8949de54ba5SRobert Watson 	 * 	  an attribute of the same name.
8959de54ba5SRobert Watson 	 * Not clear how to report errors -- probably eat them.
8969de54ba5SRobert Watson 	 * This would all happen while the filesystem was busy/not
8979de54ba5SRobert Watson 	 * available, so would effectively be "atomic".
8989de54ba5SRobert Watson 	 */
899e7fd8877SKonstantin Belousov 	mp->mnt_stat.f_iosize = fs->fs_bsize;
900b40ce416SJulian Elischer 	(void) ufs_extattr_autostart(mp, td);
901516081f2SRobert Watson #endif /* !UFS_EXTATTR_AUTOSTART */
902516081f2SRobert Watson #endif /* !UFS_EXTATTR */
903df8bae1dSRodney W. Grimes 	return (0);
904df8bae1dSRodney W. Grimes out:
905df8bae1dSRodney W. Grimes 	if (bp)
906df8bae1dSRodney W. Grimes 		brelse(bp);
90743920011SPoul-Henning Kamp 	if (cp != NULL) {
90843920011SPoul-Henning Kamp 		DROP_GIANT();
90943920011SPoul-Henning Kamp 		g_topology_lock();
91084a69752SPoul-Henning Kamp 		g_vfs_close(cp, td);
91143920011SPoul-Henning Kamp 		g_topology_unlock();
91243920011SPoul-Henning Kamp 		PICKUP_GIANT();
91343920011SPoul-Henning Kamp 	}
914df8bae1dSRodney W. Grimes 	if (ump) {
9153ba649d7SJeff Roberson 		mtx_destroy(UFS_MTX(ump));
9161a60c7fcSPawel Jakub Dawidek 		if (mp->mnt_gjprovider != NULL) {
9171a60c7fcSPawel Jakub Dawidek 			free(mp->mnt_gjprovider, M_UFSMNT);
9181a60c7fcSPawel Jakub Dawidek 			mp->mnt_gjprovider = NULL;
9191a60c7fcSPawel Jakub Dawidek 		}
920df8bae1dSRodney W. Grimes 		free(ump->um_fs, M_UFSMNT);
921df8bae1dSRodney W. Grimes 		free(ump, M_UFSMNT);
92277465d93SAlfred Perlstein 		mp->mnt_data = NULL;
923df8bae1dSRodney W. Grimes 	}
924df8bae1dSRodney W. Grimes 	return (error);
925df8bae1dSRodney W. Grimes }
926df8bae1dSRodney W. Grimes 
9271c85e6a3SKirk McKusick #include <sys/sysctl.h>
928adf41577SPoul-Henning Kamp static int bigcgs = 0;
9291c85e6a3SKirk McKusick SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
9301c85e6a3SKirk McKusick 
931df8bae1dSRodney W. Grimes /*
9321c85e6a3SKirk McKusick  * Sanity checks for loading old filesystem superblocks.
9331c85e6a3SKirk McKusick  * See ffs_oldfscompat_write below for unwound actions.
934df8bae1dSRodney W. Grimes  *
9351c85e6a3SKirk McKusick  * XXX - Parts get retired eventually.
9361c85e6a3SKirk McKusick  * Unfortunately new bits get added.
937df8bae1dSRodney W. Grimes  */
9381c85e6a3SKirk McKusick static void
9391c85e6a3SKirk McKusick ffs_oldfscompat_read(fs, ump, sblockloc)
940df8bae1dSRodney W. Grimes 	struct fs *fs;
9411c85e6a3SKirk McKusick 	struct ufsmount *ump;
9421c85e6a3SKirk McKusick 	ufs2_daddr_t sblockloc;
9431c85e6a3SKirk McKusick {
9441c85e6a3SKirk McKusick 	off_t maxfilesize;
9451c85e6a3SKirk McKusick 
9461c85e6a3SKirk McKusick 	/*
947ada981b2SKirk McKusick 	 * If not yet done, update fs_flags location and value of fs_sblockloc.
948ada981b2SKirk McKusick 	 */
949ada981b2SKirk McKusick 	if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
950ada981b2SKirk McKusick 		fs->fs_flags = fs->fs_old_flags;
951ada981b2SKirk McKusick 		fs->fs_old_flags |= FS_FLAGS_UPDATED;
952ada981b2SKirk McKusick 		fs->fs_sblockloc = sblockloc;
953ada981b2SKirk McKusick 	}
954ada981b2SKirk McKusick 	/*
9551c85e6a3SKirk McKusick 	 * If not yet done, update UFS1 superblock with new wider fields.
9561c85e6a3SKirk McKusick 	 */
95774f3809aSKirk McKusick 	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
9581c85e6a3SKirk McKusick 		fs->fs_maxbsize = fs->fs_bsize;
9591c85e6a3SKirk McKusick 		fs->fs_time = fs->fs_old_time;
9601c85e6a3SKirk McKusick 		fs->fs_size = fs->fs_old_size;
9611c85e6a3SKirk McKusick 		fs->fs_dsize = fs->fs_old_dsize;
9621c85e6a3SKirk McKusick 		fs->fs_csaddr = fs->fs_old_csaddr;
9631c85e6a3SKirk McKusick 		fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
9641c85e6a3SKirk McKusick 		fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
9651c85e6a3SKirk McKusick 		fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
9661c85e6a3SKirk McKusick 		fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
9671c85e6a3SKirk McKusick 	}
9681c85e6a3SKirk McKusick 	if (fs->fs_magic == FS_UFS1_MAGIC &&
9691c85e6a3SKirk McKusick 	    fs->fs_old_inodefmt < FS_44INODEFMT) {
9708680d698SNate Lawson 		fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
9711c85e6a3SKirk McKusick 		fs->fs_qbmask = ~fs->fs_bmask;
9721c85e6a3SKirk McKusick 		fs->fs_qfmask = ~fs->fs_fmask;
9731c85e6a3SKirk McKusick 	}
9748f42fb8fSIan Dowse 	if (fs->fs_magic == FS_UFS1_MAGIC) {
9751c85e6a3SKirk McKusick 		ump->um_savedmaxfilesize = fs->fs_maxfilesize;
9768680d698SNate Lawson 		maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
9771c85e6a3SKirk McKusick 		if (fs->fs_maxfilesize > maxfilesize)
9781c85e6a3SKirk McKusick 			fs->fs_maxfilesize = maxfilesize;
9798f42fb8fSIan Dowse 	}
9801c85e6a3SKirk McKusick 	/* Compatibility for old filesystems */
9811c85e6a3SKirk McKusick 	if (fs->fs_avgfilesize <= 0)
9821c85e6a3SKirk McKusick 		fs->fs_avgfilesize = AVFILESIZ;
9831c85e6a3SKirk McKusick 	if (fs->fs_avgfpdir <= 0)
9841c85e6a3SKirk McKusick 		fs->fs_avgfpdir = AFPDIR;
9851c85e6a3SKirk McKusick 	if (bigcgs) {
9861c85e6a3SKirk McKusick 		fs->fs_save_cgsize = fs->fs_cgsize;
9871c85e6a3SKirk McKusick 		fs->fs_cgsize = fs->fs_bsize;
9881c85e6a3SKirk McKusick 	}
9891c85e6a3SKirk McKusick }
9901c85e6a3SKirk McKusick 
9911c85e6a3SKirk McKusick /*
9921c85e6a3SKirk McKusick  * Unwinding superblock updates for old filesystems.
9931c85e6a3SKirk McKusick  * See ffs_oldfscompat_read above for details.
9941c85e6a3SKirk McKusick  *
9951c85e6a3SKirk McKusick  * XXX - Parts get retired eventually.
9961c85e6a3SKirk McKusick  * Unfortunately new bits get added.
9971c85e6a3SKirk McKusick  */
9981c85e6a3SKirk McKusick static void
9991c85e6a3SKirk McKusick ffs_oldfscompat_write(fs, ump)
10001c85e6a3SKirk McKusick 	struct fs *fs;
10011c85e6a3SKirk McKusick 	struct ufsmount *ump;
1002df8bae1dSRodney W. Grimes {
1003df8bae1dSRodney W. Grimes 
10041c85e6a3SKirk McKusick 	/*
10051c85e6a3SKirk McKusick 	 * Copy back UFS2 updated fields that UFS1 inspects.
10061c85e6a3SKirk McKusick 	 */
10071c85e6a3SKirk McKusick 	if (fs->fs_magic == FS_UFS1_MAGIC) {
10081c85e6a3SKirk McKusick 		fs->fs_old_time = fs->fs_time;
10091c85e6a3SKirk McKusick 		fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
10101c85e6a3SKirk McKusick 		fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
10111c85e6a3SKirk McKusick 		fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
10121c85e6a3SKirk McKusick 		fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
10131c85e6a3SKirk McKusick 		fs->fs_maxfilesize = ump->um_savedmaxfilesize;
10148f42fb8fSIan Dowse 	}
10151c85e6a3SKirk McKusick 	if (bigcgs) {
10161c85e6a3SKirk McKusick 		fs->fs_cgsize = fs->fs_save_cgsize;
10171c85e6a3SKirk McKusick 		fs->fs_save_cgsize = 0;
10181c85e6a3SKirk McKusick 	}
1019df8bae1dSRodney W. Grimes }
1020df8bae1dSRodney W. Grimes 
1021df8bae1dSRodney W. Grimes /*
1022df8bae1dSRodney W. Grimes  * unmount system call
1023df8bae1dSRodney W. Grimes  */
1024adf41577SPoul-Henning Kamp static int
1025b40ce416SJulian Elischer ffs_unmount(mp, mntflags, td)
1026df8bae1dSRodney W. Grimes 	struct mount *mp;
1027df8bae1dSRodney W. Grimes 	int mntflags;
1028b40ce416SJulian Elischer 	struct thread *td;
1029df8bae1dSRodney W. Grimes {
103005f4ff5dSPoul-Henning Kamp 	struct ufsmount *ump = VFSTOUFS(mp);
103105f4ff5dSPoul-Henning Kamp 	struct fs *fs;
10326fecb4e4SKonstantin Belousov 	int error, flags, susp;
1033df8bae1dSRodney W. Grimes 
1034df8bae1dSRodney W. Grimes 	flags = 0;
10356fecb4e4SKonstantin Belousov 	fs = ump->um_fs;
1036df8bae1dSRodney W. Grimes 	if (mntflags & MNT_FORCE) {
1037df8bae1dSRodney W. Grimes 		flags |= FORCECLOSE;
10386fecb4e4SKonstantin Belousov 		susp = fs->fs_ronly != 0;
10396fecb4e4SKonstantin Belousov 	} else
10406fecb4e4SKonstantin Belousov 		susp = 0;
1041516081f2SRobert Watson #ifdef UFS_EXTATTR
1042b40ce416SJulian Elischer 	if ((error = ufs_extattr_stop(mp, td))) {
1043b2b0497aSRobert Watson 		if (error != EOPNOTSUPP)
1044b2b0497aSRobert Watson 			printf("ffs_unmount: ufs_extattr_stop returned %d\n",
1045b2b0497aSRobert Watson 			    error);
10467df97b61SRobert Watson 	} else {
10479de54ba5SRobert Watson 		ufs_extattr_uepm_destroy(&ump->um_extattr);
10487df97b61SRobert Watson 	}
1049a64ed089SRobert Watson #endif
10506fecb4e4SKonstantin Belousov 	if (susp) {
10516fecb4e4SKonstantin Belousov 		/*
10526fecb4e4SKonstantin Belousov 		 * dounmount already called vn_start_write().
10536fecb4e4SKonstantin Belousov 		 */
10546fecb4e4SKonstantin Belousov 		for (;;) {
10556fecb4e4SKonstantin Belousov 			vn_finished_write(mp);
10566fecb4e4SKonstantin Belousov 			if ((error = vfs_write_suspend(mp)) != 0)
10576fecb4e4SKonstantin Belousov 				return (error);
10586fecb4e4SKonstantin Belousov 			MNT_ILOCK(mp);
10596fecb4e4SKonstantin Belousov 			if (mp->mnt_kern_flag & MNTK_SUSPENDED) {
10606fecb4e4SKonstantin Belousov 				mp->mnt_kern_flag &= ~(MNTK_SUSPENDED |
10616fecb4e4SKonstantin Belousov 				    MNTK_SUSPEND2);
10626fecb4e4SKonstantin Belousov 				wakeup(&mp->mnt_flag);
10636fecb4e4SKonstantin Belousov 				MNT_IUNLOCK(mp);
10646fecb4e4SKonstantin Belousov 				curthread->td_pflags |= TDP_IGNSUSP;
10656fecb4e4SKonstantin Belousov 				break;
10666fecb4e4SKonstantin Belousov 			}
10676fecb4e4SKonstantin Belousov 			MNT_IUNLOCK(mp);
10686fecb4e4SKonstantin Belousov 			vn_start_write(NULL, &mp, V_WAIT);
10696fecb4e4SKonstantin Belousov 		}
10706fecb4e4SKonstantin Belousov 	}
1071b1897c19SJulian Elischer 	if (mp->mnt_flag & MNT_SOFTDEP) {
1072b40ce416SJulian Elischer 		if ((error = softdep_flushfiles(mp, flags, td)) != 0)
10736fecb4e4SKonstantin Belousov 			goto fail;
1074b1897c19SJulian Elischer 	} else {
1075b40ce416SJulian Elischer 		if ((error = ffs_flushfiles(mp, flags, td)) != 0)
10766fecb4e4SKonstantin Belousov 			goto fail;
1077b1897c19SJulian Elischer 	}
10783ba649d7SJeff Roberson 	UFS_LOCK(ump);
10799ccb939eSKirk McKusick 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1080cfbf0a46SMaxime Henrion 		printf("%s: unmount pending error: blocks %jd files %d\n",
10811c85e6a3SKirk McKusick 		    fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
10821c85e6a3SKirk McKusick 		    fs->fs_pendinginodes);
10839ccb939eSKirk McKusick 		fs->fs_pendingblocks = 0;
10849ccb939eSKirk McKusick 		fs->fs_pendinginodes = 0;
10859ccb939eSKirk McKusick 	}
10863ba649d7SJeff Roberson 	UFS_UNLOCK(ump);
1087996c772fSJohn Dyson 	if (fs->fs_ronly == 0) {
10881a6a6610SKirk McKusick 		fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1089791dd2faSTor Egge 		error = ffs_sbupdate(ump, MNT_WAIT, 0);
1090996c772fSJohn Dyson 		if (error) {
1091996c772fSJohn Dyson 			fs->fs_clean = 0;
10926fecb4e4SKonstantin Belousov 			goto fail;
1093996c772fSJohn Dyson 		}
1094e0e9c421SDavid Greenman 	}
10956fecb4e4SKonstantin Belousov 	if (susp) {
10966fecb4e4SKonstantin Belousov 		vfs_write_resume(mp);
10976fecb4e4SKonstantin Belousov 		vn_start_write(NULL, &mp, V_WAIT);
10986fecb4e4SKonstantin Belousov 	}
109943920011SPoul-Henning Kamp 	DROP_GIANT();
110043920011SPoul-Henning Kamp 	g_topology_lock();
110184a69752SPoul-Henning Kamp 	g_vfs_close(ump->um_cp, td);
110243920011SPoul-Henning Kamp 	g_topology_unlock();
110343920011SPoul-Henning Kamp 	PICKUP_GIANT();
11046476c0d2SJohn Dyson 	vrele(ump->um_devvp);
11053ba649d7SJeff Roberson 	mtx_destroy(UFS_MTX(ump));
11061a60c7fcSPawel Jakub Dawidek 	if (mp->mnt_gjprovider != NULL) {
11071a60c7fcSPawel Jakub Dawidek 		free(mp->mnt_gjprovider, M_UFSMNT);
11081a60c7fcSPawel Jakub Dawidek 		mp->mnt_gjprovider = NULL;
11091a60c7fcSPawel Jakub Dawidek 	}
1110f55ff3f3SIan Dowse 	free(fs->fs_csp, M_UFSMNT);
1111df8bae1dSRodney W. Grimes 	free(fs, M_UFSMNT);
1112df8bae1dSRodney W. Grimes 	free(ump, M_UFSMNT);
111377465d93SAlfred Perlstein 	mp->mnt_data = NULL;
11145da56ddbSTor Egge 	MNT_ILOCK(mp);
1115cc9d8990SPeter Wemm 	mp->mnt_flag &= ~MNT_LOCAL;
11165da56ddbSTor Egge 	MNT_IUNLOCK(mp);
1117df8bae1dSRodney W. Grimes 	return (error);
11186fecb4e4SKonstantin Belousov 
11196fecb4e4SKonstantin Belousov fail:
11206fecb4e4SKonstantin Belousov 	if (susp) {
11216fecb4e4SKonstantin Belousov 		vfs_write_resume(mp);
11226fecb4e4SKonstantin Belousov 		vn_start_write(NULL, &mp, V_WAIT);
11236fecb4e4SKonstantin Belousov 	}
11246fecb4e4SKonstantin Belousov 	return (error);
1125df8bae1dSRodney W. Grimes }
1126df8bae1dSRodney W. Grimes 
1127df8bae1dSRodney W. Grimes /*
1128df8bae1dSRodney W. Grimes  * Flush out all the files in a filesystem.
1129df8bae1dSRodney W. Grimes  */
113026f9a767SRodney W. Grimes int
1131b40ce416SJulian Elischer ffs_flushfiles(mp, flags, td)
113205f4ff5dSPoul-Henning Kamp 	struct mount *mp;
1133df8bae1dSRodney W. Grimes 	int flags;
1134b40ce416SJulian Elischer 	struct thread *td;
1135df8bae1dSRodney W. Grimes {
113605f4ff5dSPoul-Henning Kamp 	struct ufsmount *ump;
1137c9671602SPoul-Henning Kamp 	int error;
1138df8bae1dSRodney W. Grimes 
1139df8bae1dSRodney W. Grimes 	ump = VFSTOUFS(mp);
1140df8bae1dSRodney W. Grimes #ifdef QUOTA
1141df8bae1dSRodney W. Grimes 	if (mp->mnt_flag & MNT_QUOTA) {
1142c1d9efcbSPoul-Henning Kamp 		int i;
1143f257b7a5SAlfred Perlstein 		error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1144c1d9efcbSPoul-Henning Kamp 		if (error)
1145df8bae1dSRodney W. Grimes 			return (error);
1146df8bae1dSRodney W. Grimes 		for (i = 0; i < MAXQUOTAS; i++) {
1147b40ce416SJulian Elischer 			quotaoff(td, mp, i);
1148df8bae1dSRodney W. Grimes 		}
1149df8bae1dSRodney W. Grimes 		/*
1150df8bae1dSRodney W. Grimes 		 * Here we fall through to vflush again to ensure
1151df8bae1dSRodney W. Grimes 		 * that we have gotten rid of all the system vnodes.
1152df8bae1dSRodney W. Grimes 		 */
1153df8bae1dSRodney W. Grimes 	}
1154df8bae1dSRodney W. Grimes #endif
1155e6e370a7SJeff Roberson 	ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1156e6e370a7SJeff Roberson 	if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1157f257b7a5SAlfred Perlstein 		if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1158f2a2857bSKirk McKusick 			return (error);
1159f2a2857bSKirk McKusick 		ffs_snapshot_unmount(mp);
116095e7a3c3STor Egge 		flags |= FORCECLOSE;
1161f2a2857bSKirk McKusick 		/*
1162f2a2857bSKirk McKusick 		 * Here we fall through to vflush again to ensure
1163f2a2857bSKirk McKusick 		 * that we have gotten rid of all the system vnodes.
1164f2a2857bSKirk McKusick 		 */
1165f2a2857bSKirk McKusick 	}
1166b1897c19SJulian Elischer         /*
1167b1897c19SJulian Elischer 	 * Flush all the files.
1168b1897c19SJulian Elischer 	 */
1169f257b7a5SAlfred Perlstein 	if ((error = vflush(mp, 0, flags, td)) != 0)
1170b1897c19SJulian Elischer 		return (error);
1171b1897c19SJulian Elischer 	/*
1172b1897c19SJulian Elischer 	 * Flush filesystem metadata.
1173b1897c19SJulian Elischer 	 */
1174cb05b60aSAttilio Rao 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
11758df6bac4SPoul-Henning Kamp 	error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
117622db15c0SAttilio Rao 	VOP_UNLOCK(ump->um_devvp, 0);
1177df8bae1dSRodney W. Grimes 	return (error);
1178df8bae1dSRodney W. Grimes }
1179df8bae1dSRodney W. Grimes 
1180df8bae1dSRodney W. Grimes /*
1181df8bae1dSRodney W. Grimes  * Get filesystem statistics.
1182df8bae1dSRodney W. Grimes  */
1183adf41577SPoul-Henning Kamp static int
1184b40ce416SJulian Elischer ffs_statfs(mp, sbp, td)
1185df8bae1dSRodney W. Grimes 	struct mount *mp;
118605f4ff5dSPoul-Henning Kamp 	struct statfs *sbp;
1187b40ce416SJulian Elischer 	struct thread *td;
1188df8bae1dSRodney W. Grimes {
118905f4ff5dSPoul-Henning Kamp 	struct ufsmount *ump;
119005f4ff5dSPoul-Henning Kamp 	struct fs *fs;
1191df8bae1dSRodney W. Grimes 
1192df8bae1dSRodney W. Grimes 	ump = VFSTOUFS(mp);
1193df8bae1dSRodney W. Grimes 	fs = ump->um_fs;
11941c85e6a3SKirk McKusick 	if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1195df8bae1dSRodney W. Grimes 		panic("ffs_statfs");
1196fde81c7dSKirk McKusick 	sbp->f_version = STATFS_VERSION;
1197df8bae1dSRodney W. Grimes 	sbp->f_bsize = fs->fs_fsize;
1198df8bae1dSRodney W. Grimes 	sbp->f_iosize = fs->fs_bsize;
1199df8bae1dSRodney W. Grimes 	sbp->f_blocks = fs->fs_dsize;
12003ba649d7SJeff Roberson 	UFS_LOCK(ump);
1201df8bae1dSRodney W. Grimes 	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
12029ccb939eSKirk McKusick 	    fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
12039ccb939eSKirk McKusick 	sbp->f_bavail = freespace(fs, fs->fs_minfree) +
12049ccb939eSKirk McKusick 	    dbtofsb(fs, fs->fs_pendingblocks);
1205df8bae1dSRodney W. Grimes 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
12069ccb939eSKirk McKusick 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
12073ba649d7SJeff Roberson 	UFS_UNLOCK(ump);
1208fde81c7dSKirk McKusick 	sbp->f_namemax = NAME_MAX;
1209df8bae1dSRodney W. Grimes 	return (0);
1210df8bae1dSRodney W. Grimes }
1211df8bae1dSRodney W. Grimes 
1212df8bae1dSRodney W. Grimes /*
1213df8bae1dSRodney W. Grimes  * Go through the disk queues to initiate sandbagged IO;
1214df8bae1dSRodney W. Grimes  * go through the inodes to write those that have been modified;
1215df8bae1dSRodney W. Grimes  * initiate the writing of the super block if it has been modified.
1216df8bae1dSRodney W. Grimes  *
1217df8bae1dSRodney W. Grimes  * Note: we are always called with the filesystem marked `MPBUSY'.
1218df8bae1dSRodney W. Grimes  */
1219adf41577SPoul-Henning Kamp static int
12208df6bac4SPoul-Henning Kamp ffs_sync(mp, waitfor, td)
1221df8bae1dSRodney W. Grimes 	struct mount *mp;
1222df8bae1dSRodney W. Grimes 	int waitfor;
1223b40ce416SJulian Elischer 	struct thread *td;
1224df8bae1dSRodney W. Grimes {
122582be0a5aSTor Egge 	struct vnode *mvp, *vp, *devvp;
1226996c772fSJohn Dyson 	struct inode *ip;
1227996c772fSJohn Dyson 	struct ufsmount *ump = VFSTOUFS(mp);
1228996c772fSJohn Dyson 	struct fs *fs;
12299b971133SKirk McKusick 	int error, count, wait, lockreq, allerror = 0;
1230791dd2faSTor Egge 	int suspend;
1231791dd2faSTor Egge 	int suspended;
1232791dd2faSTor Egge 	int secondary_writes;
1233791dd2faSTor Egge 	int secondary_accwrites;
1234791dd2faSTor Egge 	int softdep_deps;
1235791dd2faSTor Egge 	int softdep_accdeps;
1236156cb265SPoul-Henning Kamp 	struct bufobj *bo;
1237df8bae1dSRodney W. Grimes 
1238df8bae1dSRodney W. Grimes 	fs = ump->um_fs;
1239996c772fSJohn Dyson 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
1240df8bae1dSRodney W. Grimes 		printf("fs = %s\n", fs->fs_fsmnt);
12415ace3b26SMike Pritchard 		panic("ffs_sync: rofs mod");
1242df8bae1dSRodney W. Grimes 	}
1243df8bae1dSRodney W. Grimes 	/*
1244df8bae1dSRodney W. Grimes 	 * Write back each (modified) inode.
1245df8bae1dSRodney W. Grimes 	 */
12469b971133SKirk McKusick 	wait = 0;
1247791dd2faSTor Egge 	suspend = 0;
1248791dd2faSTor Egge 	suspended = 0;
1249245df27cSMatthew Dillon 	lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1250791dd2faSTor Egge 	if (waitfor == MNT_SUSPEND) {
1251791dd2faSTor Egge 		suspend = 1;
1252791dd2faSTor Egge 		waitfor = MNT_WAIT;
1253791dd2faSTor Egge 	}
12549b971133SKirk McKusick 	if (waitfor == MNT_WAIT) {
12559b971133SKirk McKusick 		wait = 1;
1256245df27cSMatthew Dillon 		lockreq = LK_EXCLUSIVE;
12579b971133SKirk McKusick 	}
125841d4783dSJeff Roberson 	lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1259ca430f2eSAlexander Kabaev 	MNT_ILOCK(mp);
1260df8bae1dSRodney W. Grimes loop:
1261791dd2faSTor Egge 	/* Grab snapshot of secondary write counts */
1262791dd2faSTor Egge 	secondary_writes = mp->mnt_secondary_writes;
1263791dd2faSTor Egge 	secondary_accwrites = mp->mnt_secondary_accwrites;
1264791dd2faSTor Egge 
1265791dd2faSTor Egge 	/* Grab snapshot of softdep dependency counts */
1266791dd2faSTor Egge 	MNT_IUNLOCK(mp);
1267791dd2faSTor Egge 	softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1268791dd2faSTor Egge 	MNT_ILOCK(mp);
1269791dd2faSTor Egge 
127082be0a5aSTor Egge 	MNT_VNODE_FOREACH(vp, mp, mvp) {
1271245df27cSMatthew Dillon 		/*
1272245df27cSMatthew Dillon 		 * Depend on the mntvnode_slock to keep things stable enough
1273245df27cSMatthew Dillon 		 * for a quick test.  Since there might be hundreds of
1274245df27cSMatthew Dillon 		 * thousands of vnodes, we cannot afford even a subroutine
1275245df27cSMatthew Dillon 		 * call unless there's a good chance that we have work to do.
1276245df27cSMatthew Dillon 		 */
12772f05568aSJeff Roberson 		VI_LOCK(vp);
1278fe68abe2SJeff Roberson 		if (vp->v_iflag & VI_DOOMED) {
12792f05568aSJeff Roberson 			VI_UNLOCK(vp);
12802f05568aSJeff Roberson 			continue;
12812f05568aSJeff Roberson 		}
1282df8bae1dSRodney W. Grimes 		ip = VTOI(vp);
1283cf60e8e4SKirk McKusick 		if (vp->v_type == VNON || ((ip->i_flag &
1284cf60e8e4SKirk McKusick 		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1285156cb265SPoul-Henning Kamp 		    vp->v_bufobj.bo_dirty.bv_cnt == 0)) {
12862f05568aSJeff Roberson 			VI_UNLOCK(vp);
1287df8bae1dSRodney W. Grimes 			continue;
1288996c772fSJohn Dyson 		}
1289ca430f2eSAlexander Kabaev 		MNT_IUNLOCK(mp);
1290b40ce416SJulian Elischer 		if ((error = vget(vp, lockreq, td)) != 0) {
1291ca430f2eSAlexander Kabaev 			MNT_ILOCK(mp);
129282be0a5aSTor Egge 			if (error == ENOENT || error == ENOLCK) {
129382be0a5aSTor Egge 				MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
1294df8bae1dSRodney W. Grimes 				goto loop;
129582be0a5aSTor Egge 			}
12962f05568aSJeff Roberson 			continue;
12972f05568aSJeff Roberson 		}
129840854ff5SPoul-Henning Kamp 		if ((error = ffs_syncvnode(vp, waitfor)) != 0)
1299df8bae1dSRodney W. Grimes 			allerror = error;
130041d4783dSJeff Roberson 		vput(vp);
1301ca430f2eSAlexander Kabaev 		MNT_ILOCK(mp);
1302245df27cSMatthew Dillon 	}
1303ca430f2eSAlexander Kabaev 	MNT_IUNLOCK(mp);
1304df8bae1dSRodney W. Grimes 	/*
1305df8bae1dSRodney W. Grimes 	 * Force stale filesystem control information to be flushed.
1306df8bae1dSRodney W. Grimes 	 */
13079b971133SKirk McKusick 	if (waitfor == MNT_WAIT) {
1308b40ce416SJulian Elischer 		if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
13099b971133SKirk McKusick 			allerror = error;
13109b971133SKirk McKusick 		/* Flushed work items may create new vnodes to clean */
13119ab73fd1SKirk McKusick 		if (allerror == 0 && count) {
1312ca430f2eSAlexander Kabaev 			MNT_ILOCK(mp);
13139b971133SKirk McKusick 			goto loop;
13149b971133SKirk McKusick 		}
13159b971133SKirk McKusick 	}
1316589c7af9SKirk McKusick #ifdef QUOTA
1317589c7af9SKirk McKusick 	qsync(mp);
1318589c7af9SKirk McKusick #endif
1319112f7372SKirk McKusick 	devvp = ump->um_devvp;
1320156cb265SPoul-Henning Kamp 	bo = &devvp->v_bufobj;
1321698b1a66SJeff Roberson 	BO_LOCK(bo);
1322112f7372SKirk McKusick 	if (waitfor != MNT_LAZY &&
1323156cb265SPoul-Henning Kamp 	    (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
1324698b1a66SJeff Roberson 		BO_UNLOCK(bo);
1325698b1a66SJeff Roberson 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
13268df6bac4SPoul-Henning Kamp 		if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0)
1327df8bae1dSRodney W. Grimes 			allerror = error;
132822db15c0SAttilio Rao 		VOP_UNLOCK(devvp, 0);
13299ab73fd1SKirk McKusick 		if (allerror == 0 && waitfor == MNT_WAIT) {
1330ca430f2eSAlexander Kabaev 			MNT_ILOCK(mp);
1331112f7372SKirk McKusick 			goto loop;
1332b1897c19SJulian Elischer 		}
1333791dd2faSTor Egge 	} else if (suspend != 0) {
1334791dd2faSTor Egge 		if (softdep_check_suspend(mp,
1335791dd2faSTor Egge 					  devvp,
1336791dd2faSTor Egge 					  softdep_deps,
1337791dd2faSTor Egge 					  softdep_accdeps,
1338791dd2faSTor Egge 					  secondary_writes,
1339791dd2faSTor Egge 					  secondary_accwrites) != 0)
1340791dd2faSTor Egge 			goto loop;	/* More work needed */
1341791dd2faSTor Egge 		mtx_assert(MNT_MTX(mp), MA_OWNED);
1342ca2fa807STor Egge 		mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1343791dd2faSTor Egge 		MNT_IUNLOCK(mp);
1344791dd2faSTor Egge 		suspended = 1;
1345112f7372SKirk McKusick 	} else
1346698b1a66SJeff Roberson 		BO_UNLOCK(bo);
1347996c772fSJohn Dyson 	/*
1348996c772fSJohn Dyson 	 * Write back modified superblock.
1349996c772fSJohn Dyson 	 */
1350791dd2faSTor Egge 	if (fs->fs_fmod != 0 &&
1351791dd2faSTor Egge 	    (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1352996c772fSJohn Dyson 		allerror = error;
1353df8bae1dSRodney W. Grimes 	return (allerror);
1354df8bae1dSRodney W. Grimes }
1355df8bae1dSRodney W. Grimes 
1356df8bae1dSRodney W. Grimes int
1357a0595d02SKirk McKusick ffs_vget(mp, ino, flags, vpp)
1358df8bae1dSRodney W. Grimes 	struct mount *mp;
1359df8bae1dSRodney W. Grimes 	ino_t ino;
1360a0595d02SKirk McKusick 	int flags;
1361df8bae1dSRodney W. Grimes 	struct vnode **vpp;
1362df8bae1dSRodney W. Grimes {
13637b7ed832SKonstantin Belousov 	return (ffs_vgetf(mp, ino, flags, vpp, 0));
13647b7ed832SKonstantin Belousov }
13657b7ed832SKonstantin Belousov 
13667b7ed832SKonstantin Belousov int
13677b7ed832SKonstantin Belousov ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
13687b7ed832SKonstantin Belousov 	struct mount *mp;
13697b7ed832SKonstantin Belousov 	ino_t ino;
13707b7ed832SKonstantin Belousov 	int flags;
13717b7ed832SKonstantin Belousov 	struct vnode **vpp;
13727b7ed832SKonstantin Belousov 	int ffs_flags;
13737b7ed832SKonstantin Belousov {
1374996c772fSJohn Dyson 	struct fs *fs;
1375996c772fSJohn Dyson 	struct inode *ip;
1376df8bae1dSRodney W. Grimes 	struct ufsmount *ump;
1377df8bae1dSRodney W. Grimes 	struct buf *bp;
1378df8bae1dSRodney W. Grimes 	struct vnode *vp;
137989c9c53dSPoul-Henning Kamp 	struct cdev *dev;
1380f576a00dSSemen Ustimenko 	int error;
1381df8bae1dSRodney W. Grimes 
138251f5ce0cSPoul-Henning Kamp 	error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1383e82ef95cSPoul-Henning Kamp 	if (error || *vpp != NULL)
138414bc0685SPoul-Henning Kamp 		return (error);
1385f576a00dSSemen Ustimenko 
1386f576a00dSSemen Ustimenko 	/*
1387d6919865SJeff Roberson 	 * We must promote to an exclusive lock for vnode creation.  This
1388d6919865SJeff Roberson 	 * can happen if lookup is passed LOCKSHARED.
1389d6919865SJeff Roberson  	 */
1390d6919865SJeff Roberson 	if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1391d6919865SJeff Roberson 		flags &= ~LK_TYPE_MASK;
1392d6919865SJeff Roberson 		flags |= LK_EXCLUSIVE;
1393d6919865SJeff Roberson 	}
1394d6919865SJeff Roberson 
1395d6919865SJeff Roberson 	/*
139613866b3fSSemen Ustimenko 	 * We do not lock vnode creation as it is believed to be too
1397f576a00dSSemen Ustimenko 	 * expensive for such rare case as simultaneous creation of vnode
1398f576a00dSSemen Ustimenko 	 * for same ino by different processes. We just allow them to race
1399f576a00dSSemen Ustimenko 	 * and check later to decide who wins. Let the race begin!
1400f576a00dSSemen Ustimenko 	 */
140114bc0685SPoul-Henning Kamp 
140214bc0685SPoul-Henning Kamp 	ump = VFSTOUFS(mp);
140314bc0685SPoul-Henning Kamp 	dev = ump->um_dev;
140414bc0685SPoul-Henning Kamp 	fs = ump->um_fs;
1405df8bae1dSRodney W. Grimes 
14062094ddb6SDavid Greenman 	/*
14072f9bae59SDavid Greenman 	 * If this MALLOC() is performed after the getnewvnode()
14082f9bae59SDavid Greenman 	 * it might block, leaving a vnode with a NULL v_data to be
14092f9bae59SDavid Greenman 	 * found by ffs_sync() if a sync happens to fire right then,
14102f9bae59SDavid Greenman 	 * which will cause a panic because ffs_sync() blindly
14112f9bae59SDavid Greenman 	 * dereferences vp->v_data (as well it should).
14122f9bae59SDavid Greenman 	 */
141314bc0685SPoul-Henning Kamp 	ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
14142f9bae59SDavid Greenman 
1415df8bae1dSRodney W. Grimes 	/* Allocate a new vnode/inode. */
141602f2c6a9SPoul-Henning Kamp 	if (fs->fs_magic == FS_UFS1_MAGIC)
141702f2c6a9SPoul-Henning Kamp 		error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp);
141802f2c6a9SPoul-Henning Kamp 	else
141902f2c6a9SPoul-Henning Kamp 		error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp);
1420c9671602SPoul-Henning Kamp 	if (error) {
1421df8bae1dSRodney W. Grimes 		*vpp = NULL;
1422aa4d7a8aSPoul-Henning Kamp 		uma_zfree(uma_inode, ip);
1423df8bae1dSRodney W. Grimes 		return (error);
1424df8bae1dSRodney W. Grimes 	}
142567e87166SBoris Popov 	/*
1426aa7ba427SJeff Roberson 	 * FFS supports recursive and shared locking.
142767e87166SBoris Popov 	 */
1428628f51d2SAttilio Rao 	VN_LOCK_AREC(vp);
1429628f51d2SAttilio Rao 	VN_LOCK_ASHARE(vp);
1430df8bae1dSRodney W. Grimes 	vp->v_data = ip;
14315d9d81e7SPoul-Henning Kamp 	vp->v_bufobj.bo_bsize = fs->fs_bsize;
1432df8bae1dSRodney W. Grimes 	ip->i_vnode = vp;
14331c85e6a3SKirk McKusick 	ip->i_ump = ump;
1434c94cd5fcSPoul-Henning Kamp 	ip->i_fs = fs;
1435df8bae1dSRodney W. Grimes 	ip->i_dev = dev;
1436df8bae1dSRodney W. Grimes 	ip->i_number = ino;
1437df8bae1dSRodney W. Grimes #ifdef QUOTA
1438c1d9efcbSPoul-Henning Kamp 	{
1439c1d9efcbSPoul-Henning Kamp 		int i;
1440df8bae1dSRodney W. Grimes 		for (i = 0; i < MAXQUOTAS; i++)
1441df8bae1dSRodney W. Grimes 			ip->i_dquot[i] = NODQUOT;
1442c1d9efcbSPoul-Henning Kamp 	}
1443df8bae1dSRodney W. Grimes #endif
1444df8bae1dSRodney W. Grimes 
14450e9eb108SAttilio Rao 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
14467b7ed832SKonstantin Belousov 	if (ffs_flags & FFSV_FORCEINSMQ)
14477b7ed832SKonstantin Belousov 		vp->v_vflag |= VV_FORCEINSMQ;
144861b9d89fSTor Egge 	error = insmntque(vp, mp);
144961b9d89fSTor Egge 	if (error != 0) {
145061b9d89fSTor Egge 		uma_zfree(uma_inode, ip);
145161b9d89fSTor Egge 		*vpp = NULL;
145261b9d89fSTor Egge 		return (error);
145361b9d89fSTor Egge 	}
14547b7ed832SKonstantin Belousov 	vp->v_vflag &= ~VV_FORCEINSMQ;
1455a80d8caaSPawel Jakub Dawidek 	error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
145645c26fa2SPoul-Henning Kamp 	if (error || *vpp != NULL)
1457f576a00dSSemen Ustimenko 		return (error);
1458f576a00dSSemen Ustimenko 
1459df8bae1dSRodney W. Grimes 	/* Read in the disk contents for the inode, copy into the inode. */
1460c9671602SPoul-Henning Kamp 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1461c9671602SPoul-Henning Kamp 	    (int)fs->fs_bsize, NOCRED, &bp);
1462c9671602SPoul-Henning Kamp 	if (error) {
1463df8bae1dSRodney W. Grimes 		/*
1464df8bae1dSRodney W. Grimes 		 * The inode does not contain anything useful, so it would
1465df8bae1dSRodney W. Grimes 		 * be misleading to leave it on its hash chain. With mode
1466df8bae1dSRodney W. Grimes 		 * still zero, it will be unlinked and returned to the free
1467df8bae1dSRodney W. Grimes 		 * list by vput().
1468df8bae1dSRodney W. Grimes 		 */
1469df8bae1dSRodney W. Grimes 		brelse(bp);
1470bd7e5f99SJohn Dyson 		vput(vp);
1471df8bae1dSRodney W. Grimes 		*vpp = NULL;
1472df8bae1dSRodney W. Grimes 		return (error);
1473df8bae1dSRodney W. Grimes 	}
1474de6ba7c0SPoul-Henning Kamp 	if (ip->i_ump->um_fstype == UFS1)
1475a163d034SWarner Losh 		ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1476de6ba7c0SPoul-Henning Kamp 	else
1477a163d034SWarner Losh 		ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1478de6ba7c0SPoul-Henning Kamp 	ffs_load_inode(bp, ip, fs, ino);
1479b1897c19SJulian Elischer 	if (DOINGSOFTDEP(vp))
1480b1897c19SJulian Elischer 		softdep_load_inodeblock(ip);
1481b1897c19SJulian Elischer 	else
1482b1897c19SJulian Elischer 		ip->i_effnlink = ip->i_nlink;
1483bd7e5f99SJohn Dyson 	bqrelse(bp);
1484df8bae1dSRodney W. Grimes 
1485df8bae1dSRodney W. Grimes 	/*
1486df8bae1dSRodney W. Grimes 	 * Initialize the vnode from the inode, check for aliases.
1487df8bae1dSRodney W. Grimes 	 * Note that the underlying vnode may have changed.
1488df8bae1dSRodney W. Grimes 	 */
148902f2c6a9SPoul-Henning Kamp 	if (ip->i_ump->um_fstype == UFS1)
149002f2c6a9SPoul-Henning Kamp 		error = ufs_vinit(mp, &ffs_fifoops1, &vp);
149102f2c6a9SPoul-Henning Kamp 	else
149202f2c6a9SPoul-Henning Kamp 		error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1493c9671602SPoul-Henning Kamp 	if (error) {
1494df8bae1dSRodney W. Grimes 		vput(vp);
1495df8bae1dSRodney W. Grimes 		*vpp = NULL;
1496df8bae1dSRodney W. Grimes 		return (error);
1497df8bae1dSRodney W. Grimes 	}
1498de68347bSPoul-Henning Kamp 
1499df8bae1dSRodney W. Grimes 	/*
15005c24d6eeSPoul-Henning Kamp 	 * Finish inode initialization.
1501df8bae1dSRodney W. Grimes 	 */
1502de68347bSPoul-Henning Kamp 
1503df8bae1dSRodney W. Grimes 	/*
1504df8bae1dSRodney W. Grimes 	 * Set up a generation number for this inode if it does not
1505df8bae1dSRodney W. Grimes 	 * already have one. This should only happen on old filesystems.
1506df8bae1dSRodney W. Grimes 	 */
1507df8bae1dSRodney W. Grimes 	if (ip->i_gen == 0) {
1508aca3e497SKirk McKusick 		ip->i_gen = arc4random() / 2 + 1;
15091c85e6a3SKirk McKusick 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1510df8bae1dSRodney W. Grimes 			ip->i_flag |= IN_MODIFIED;
1511b403319bSAlexander Kabaev 			DIP_SET(ip, i_gen, ip->i_gen);
15121c85e6a3SKirk McKusick 		}
1513df8bae1dSRodney W. Grimes 	}
1514df8bae1dSRodney W. Grimes 	/*
1515df8bae1dSRodney W. Grimes 	 * Ensure that uid and gid are correct. This is a temporary
1516df8bae1dSRodney W. Grimes 	 * fix until fsck has been changed to do the update.
1517df8bae1dSRodney W. Grimes 	 */
15181c85e6a3SKirk McKusick 	if (fs->fs_magic == FS_UFS1_MAGIC &&		/* XXX */
15191c85e6a3SKirk McKusick 	    fs->fs_old_inodefmt < FS_44INODEFMT) {	/* XXX */
15201c85e6a3SKirk McKusick 		ip->i_uid = ip->i_din1->di_ouid;	/* XXX */
15211c85e6a3SKirk McKusick 		ip->i_gid = ip->i_din1->di_ogid;	/* XXX */
1522df8bae1dSRodney W. Grimes 	}						/* XXX */
1523df8bae1dSRodney W. Grimes 
1524763bbd2fSRobert Watson #ifdef MAC
1525763bbd2fSRobert Watson 	if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1526763bbd2fSRobert Watson 		/*
1527763bbd2fSRobert Watson 		 * If this vnode is already allocated, and we're running
1528763bbd2fSRobert Watson 		 * multi-label, attempt to perform a label association
1529763bbd2fSRobert Watson 		 * from the extended attributes on the inode.
1530763bbd2fSRobert Watson 		 */
153130d239bcSRobert Watson 		error = mac_vnode_associate_extattr(mp, vp);
1532763bbd2fSRobert Watson 		if (error) {
1533763bbd2fSRobert Watson 			/* ufs_inactive will release ip->i_devvp ref. */
1534763bbd2fSRobert Watson 			vput(vp);
1535763bbd2fSRobert Watson 			*vpp = NULL;
1536763bbd2fSRobert Watson 			return (error);
1537763bbd2fSRobert Watson 		}
1538763bbd2fSRobert Watson 	}
1539763bbd2fSRobert Watson #endif
1540763bbd2fSRobert Watson 
1541df8bae1dSRodney W. Grimes 	*vpp = vp;
1542df8bae1dSRodney W. Grimes 	return (0);
1543df8bae1dSRodney W. Grimes }
1544df8bae1dSRodney W. Grimes 
1545df8bae1dSRodney W. Grimes /*
1546df8bae1dSRodney W. Grimes  * File handle to vnode
1547df8bae1dSRodney W. Grimes  *
1548df8bae1dSRodney W. Grimes  * Have to be really careful about stale file handles:
1549df8bae1dSRodney W. Grimes  * - check that the inode number is valid
1550df8bae1dSRodney W. Grimes  * - call ffs_vget() to get the locked inode
1551df8bae1dSRodney W. Grimes  * - check for an unallocated inode (i_mode == 0)
1552df8bae1dSRodney W. Grimes  * - check that the given client host has export rights and return
1553df8bae1dSRodney W. Grimes  *   those rights via. exflagsp and credanonp
1554df8bae1dSRodney W. Grimes  */
1555adf41577SPoul-Henning Kamp static int
1556c24fda81SAlfred Perlstein ffs_fhtovp(mp, fhp, vpp)
155705f4ff5dSPoul-Henning Kamp 	struct mount *mp;
1558df8bae1dSRodney W. Grimes 	struct fid *fhp;
1559df8bae1dSRodney W. Grimes 	struct vnode **vpp;
1560df8bae1dSRodney W. Grimes {
156105f4ff5dSPoul-Henning Kamp 	struct ufid *ufhp;
1562df8bae1dSRodney W. Grimes 	struct fs *fs;
1563df8bae1dSRodney W. Grimes 
1564df8bae1dSRodney W. Grimes 	ufhp = (struct ufid *)fhp;
1565df8bae1dSRodney W. Grimes 	fs = VFSTOUFS(mp)->um_fs;
1566df8bae1dSRodney W. Grimes 	if (ufhp->ufid_ino < ROOTINO ||
1567df8bae1dSRodney W. Grimes 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1568df8bae1dSRodney W. Grimes 		return (ESTALE);
1569c24fda81SAlfred Perlstein 	return (ufs_fhtovp(mp, ufhp, vpp));
1570df8bae1dSRodney W. Grimes }
1571df8bae1dSRodney W. Grimes 
1572df8bae1dSRodney W. Grimes /*
15735346934fSIan Dowse  * Initialize the filesystem.
1574996c772fSJohn Dyson  */
1575996c772fSJohn Dyson static int
1576996c772fSJohn Dyson ffs_init(vfsp)
1577996c772fSJohn Dyson 	struct vfsconf *vfsp;
1578996c772fSJohn Dyson {
1579996c772fSJohn Dyson 
1580b1897c19SJulian Elischer 	softdep_initialize();
1581996c772fSJohn Dyson 	return (ufs_init(vfsp));
1582996c772fSJohn Dyson }
1583996c772fSJohn Dyson 
1584996c772fSJohn Dyson /*
15855346934fSIan Dowse  * Undo the work of ffs_init().
15865346934fSIan Dowse  */
15875346934fSIan Dowse static int
15885346934fSIan Dowse ffs_uninit(vfsp)
15895346934fSIan Dowse 	struct vfsconf *vfsp;
15905346934fSIan Dowse {
15915346934fSIan Dowse 	int ret;
15925346934fSIan Dowse 
15935346934fSIan Dowse 	ret = ufs_uninit(vfsp);
15945346934fSIan Dowse 	softdep_uninitialize();
15955346934fSIan Dowse 	return (ret);
15965346934fSIan Dowse }
15975346934fSIan Dowse 
15985346934fSIan Dowse /*
1599df8bae1dSRodney W. Grimes  * Write a superblock and associated information back to disk.
1600df8bae1dSRodney W. Grimes  */
16011a60c7fcSPawel Jakub Dawidek int
1602791dd2faSTor Egge ffs_sbupdate(mp, waitfor, suspended)
1603df8bae1dSRodney W. Grimes 	struct ufsmount *mp;
1604df8bae1dSRodney W. Grimes 	int waitfor;
1605791dd2faSTor Egge 	int suspended;
1606df8bae1dSRodney W. Grimes {
16071c85e6a3SKirk McKusick 	struct fs *fs = mp->um_fs;
16083ba649d7SJeff Roberson 	struct buf *sbbp;
160905f4ff5dSPoul-Henning Kamp 	struct buf *bp;
1610df8bae1dSRodney W. Grimes 	int blks;
1611f55ff3f3SIan Dowse 	void *space;
1612996c772fSJohn Dyson 	int i, size, error, allerror = 0;
1613df8bae1dSRodney W. Grimes 
161474f3809aSKirk McKusick 	if (fs->fs_ronly == 1 &&
161574f3809aSKirk McKusick 	    (mp->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
161674f3809aSKirk McKusick 	    (MNT_RDONLY | MNT_UPDATE))
161774f3809aSKirk McKusick 		panic("ffs_sbupdate: write read-only filesystem");
1618996c772fSJohn Dyson 	/*
16193ba649d7SJeff Roberson 	 * We use the superblock's buf to serialize calls to ffs_sbupdate().
16203ba649d7SJeff Roberson 	 */
16213ba649d7SJeff Roberson 	sbbp = getblk(mp->um_devvp, btodb(fs->fs_sblockloc), (int)fs->fs_sbsize,
16223ba649d7SJeff Roberson 	    0, 0, 0);
16233ba649d7SJeff Roberson 	/*
1624996c772fSJohn Dyson 	 * First write back the summary information.
1625996c772fSJohn Dyson 	 */
1626df8bae1dSRodney W. Grimes 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
1627f55ff3f3SIan Dowse 	space = fs->fs_csp;
1628df8bae1dSRodney W. Grimes 	for (i = 0; i < blks; i += fs->fs_frag) {
1629df8bae1dSRodney W. Grimes 		size = fs->fs_bsize;
1630df8bae1dSRodney W. Grimes 		if (i + fs->fs_frag > blks)
1631df8bae1dSRodney W. Grimes 			size = (blks - i) * fs->fs_fsize;
1632df8bae1dSRodney W. Grimes 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
16337261f5f6SJeff Roberson 		    size, 0, 0, 0);
1634df8bae1dSRodney W. Grimes 		bcopy(space, bp->b_data, (u_int)size);
1635f55ff3f3SIan Dowse 		space = (char *)space + size;
1636791dd2faSTor Egge 		if (suspended)
1637791dd2faSTor Egge 			bp->b_flags |= B_VALIDSUSPWRT;
1638996c772fSJohn Dyson 		if (waitfor != MNT_WAIT)
1639df8bae1dSRodney W. Grimes 			bawrite(bp);
16408aef1712SMatthew Dillon 		else if ((error = bwrite(bp)) != 0)
1641996c772fSJohn Dyson 			allerror = error;
1642df8bae1dSRodney W. Grimes 	}
1643996c772fSJohn Dyson 	/*
1644996c772fSJohn Dyson 	 * Now write back the superblock itself. If any errors occurred
1645996c772fSJohn Dyson 	 * up to this point, then fail so that the superblock avoids
1646996c772fSJohn Dyson 	 * being written out as clean.
1647996c772fSJohn Dyson 	 */
16483ba649d7SJeff Roberson 	if (allerror) {
16493ba649d7SJeff Roberson 		brelse(sbbp);
1650996c772fSJohn Dyson 		return (allerror);
16513ba649d7SJeff Roberson 	}
16523ba649d7SJeff Roberson 	bp = sbbp;
165331574422SKirk McKusick 	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
165431574422SKirk McKusick 	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1655fa5d33e2SKirk McKusick 		printf("%s: correcting fs_sblockloc from %jd to %d\n",
1656fa5d33e2SKirk McKusick 		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1657fa5d33e2SKirk McKusick 		fs->fs_sblockloc = SBLOCK_UFS1;
1658fa5d33e2SKirk McKusick 	}
165931574422SKirk McKusick 	if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
166031574422SKirk McKusick 	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1661fa5d33e2SKirk McKusick 		printf("%s: correcting fs_sblockloc from %jd to %d\n",
1662fa5d33e2SKirk McKusick 		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1663fa5d33e2SKirk McKusick 		fs->fs_sblockloc = SBLOCK_UFS2;
1664fa5d33e2SKirk McKusick 	}
1665b1897c19SJulian Elischer 	fs->fs_fmod = 0;
1666227ee8a1SPoul-Henning Kamp 	fs->fs_time = time_second;
1667996c772fSJohn Dyson 	bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
16681c85e6a3SKirk McKusick 	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1669791dd2faSTor Egge 	if (suspended)
1670791dd2faSTor Egge 		bp->b_flags |= B_VALIDSUSPWRT;
1671996c772fSJohn Dyson 	if (waitfor != MNT_WAIT)
1672996c772fSJohn Dyson 		bawrite(bp);
16738aef1712SMatthew Dillon 	else if ((error = bwrite(bp)) != 0)
1674996c772fSJohn Dyson 		allerror = error;
1675996c772fSJohn Dyson 	return (allerror);
1676df8bae1dSRodney W. Grimes }
1677d6fe88e4SPoul-Henning Kamp 
1678d6fe88e4SPoul-Henning Kamp static int
1679d6fe88e4SPoul-Henning Kamp ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1680d6fe88e4SPoul-Henning Kamp 	int attrnamespace, const char *attrname, struct thread *td)
1681d6fe88e4SPoul-Henning Kamp {
1682d6fe88e4SPoul-Henning Kamp 
1683d6fe88e4SPoul-Henning Kamp #ifdef UFS_EXTATTR
1684d6fe88e4SPoul-Henning Kamp 	return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1685d6fe88e4SPoul-Henning Kamp 	    attrname, td));
1686d6fe88e4SPoul-Henning Kamp #else
1687d6fe88e4SPoul-Henning Kamp 	return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1688d6fe88e4SPoul-Henning Kamp 	    attrname, td));
1689d6fe88e4SPoul-Henning Kamp #endif
1690d6fe88e4SPoul-Henning Kamp }
1691975512a9SPoul-Henning Kamp 
1692975512a9SPoul-Henning Kamp static void
1693975512a9SPoul-Henning Kamp ffs_ifree(struct ufsmount *ump, struct inode *ip)
1694975512a9SPoul-Henning Kamp {
1695975512a9SPoul-Henning Kamp 
169636329289STim J. Robbins 	if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1697aa4d7a8aSPoul-Henning Kamp 		uma_zfree(uma_ufs1, ip->i_din1);
169836329289STim J. Robbins 	else if (ip->i_din2 != NULL)
16998d721e87STim J. Robbins 		uma_zfree(uma_ufs2, ip->i_din2);
1700aa4d7a8aSPoul-Henning Kamp 	uma_zfree(uma_inode, ip);
1701975512a9SPoul-Henning Kamp }
17026e77a041SPoul-Henning Kamp 
1703dd19a799SPoul-Henning Kamp static int dobkgrdwrite = 1;
1704dd19a799SPoul-Henning Kamp SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1705dd19a799SPoul-Henning Kamp     "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1706dd19a799SPoul-Henning Kamp 
1707dd19a799SPoul-Henning Kamp /*
1708dd19a799SPoul-Henning Kamp  * Complete a background write started from bwrite.
1709dd19a799SPoul-Henning Kamp  */
1710dd19a799SPoul-Henning Kamp static void
1711dd19a799SPoul-Henning Kamp ffs_backgroundwritedone(struct buf *bp)
1712dd19a799SPoul-Henning Kamp {
1713204ec66dSJeff Roberson 	struct bufobj *bufobj;
1714dd19a799SPoul-Henning Kamp 	struct buf *origbp;
1715dd19a799SPoul-Henning Kamp 
1716dd19a799SPoul-Henning Kamp 	/*
1717dd19a799SPoul-Henning Kamp 	 * Find the original buffer that we are writing.
1718dd19a799SPoul-Henning Kamp 	 */
1719204ec66dSJeff Roberson 	bufobj = bp->b_bufobj;
1720204ec66dSJeff Roberson 	BO_LOCK(bufobj);
1721dd19a799SPoul-Henning Kamp 	if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
1722dd19a799SPoul-Henning Kamp 		panic("backgroundwritedone: lost buffer");
1723204ec66dSJeff Roberson 	/* Grab an extra reference to be dropped by the bufdone() below. */
1724204ec66dSJeff Roberson 	bufobj_wrefl(bufobj);
1725204ec66dSJeff Roberson 	BO_UNLOCK(bufobj);
1726dd19a799SPoul-Henning Kamp 	/*
1727dd19a799SPoul-Henning Kamp 	 * Process dependencies then return any unfinished ones.
1728dd19a799SPoul-Henning Kamp 	 */
172904533fc6SXin LI 	if (!LIST_EMPTY(&bp->b_dep))
1730dd19a799SPoul-Henning Kamp 		buf_complete(bp);
1731dd19a799SPoul-Henning Kamp #ifdef SOFTUPDATES
173204533fc6SXin LI 	if (!LIST_EMPTY(&bp->b_dep))
1733dd19a799SPoul-Henning Kamp 		softdep_move_dependencies(bp, origbp);
1734dd19a799SPoul-Henning Kamp #endif
1735dd19a799SPoul-Henning Kamp 	/*
1736204ec66dSJeff Roberson 	 * This buffer is marked B_NOCACHE so when it is released
1737204ec66dSJeff Roberson 	 * by biodone it will be tossed.
1738dd19a799SPoul-Henning Kamp 	 */
1739dd19a799SPoul-Henning Kamp 	bp->b_flags |= B_NOCACHE;
1740ec9c9e73SAlan Cox 	bp->b_flags &= ~B_CACHE;
1741dd19a799SPoul-Henning Kamp 	bufdone(bp);
1742204ec66dSJeff Roberson 	BO_LOCK(bufobj);
1743dd19a799SPoul-Henning Kamp 	/*
1744dd19a799SPoul-Henning Kamp 	 * Clear the BV_BKGRDINPROG flag in the original buffer
1745dd19a799SPoul-Henning Kamp 	 * and awaken it if it is waiting for the write to complete.
1746dd19a799SPoul-Henning Kamp 	 * If BV_BKGRDINPROG is not set in the original buffer it must
1747dd19a799SPoul-Henning Kamp 	 * have been released and re-instantiated - which is not legal.
1748dd19a799SPoul-Henning Kamp 	 */
1749dd19a799SPoul-Henning Kamp 	KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
1750dd19a799SPoul-Henning Kamp 	    ("backgroundwritedone: lost buffer2"));
1751dd19a799SPoul-Henning Kamp 	origbp->b_vflags &= ~BV_BKGRDINPROG;
1752dd19a799SPoul-Henning Kamp 	if (origbp->b_vflags & BV_BKGRDWAIT) {
1753dd19a799SPoul-Henning Kamp 		origbp->b_vflags &= ~BV_BKGRDWAIT;
1754dd19a799SPoul-Henning Kamp 		wakeup(&origbp->b_xflags);
1755dd19a799SPoul-Henning Kamp 	}
1756204ec66dSJeff Roberson 	BO_UNLOCK(bufobj);
1757dd19a799SPoul-Henning Kamp }
1758dd19a799SPoul-Henning Kamp 
1759dd19a799SPoul-Henning Kamp 
1760dd19a799SPoul-Henning Kamp /*
1761dd19a799SPoul-Henning Kamp  * Write, release buffer on completion.  (Done by iodone
1762dd19a799SPoul-Henning Kamp  * if async).  Do not bother writing anything if the buffer
1763dd19a799SPoul-Henning Kamp  * is invalid.
1764dd19a799SPoul-Henning Kamp  *
1765dd19a799SPoul-Henning Kamp  * Note that we set B_CACHE here, indicating that buffer is
1766dd19a799SPoul-Henning Kamp  * fully valid and thus cacheable.  This is true even of NFS
1767dd19a799SPoul-Henning Kamp  * now so we set it generally.  This could be set either here
1768dd19a799SPoul-Henning Kamp  * or in biodone() since the I/O is synchronous.  We put it
1769dd19a799SPoul-Henning Kamp  * here.
1770dd19a799SPoul-Henning Kamp  */
1771dd19a799SPoul-Henning Kamp static int
1772dd19a799SPoul-Henning Kamp ffs_bufwrite(struct buf *bp)
1773dd19a799SPoul-Henning Kamp {
1774dd19a799SPoul-Henning Kamp 	int oldflags, s;
1775dd19a799SPoul-Henning Kamp 	struct buf *newbp;
1776dd19a799SPoul-Henning Kamp 
1777dd19a799SPoul-Henning Kamp 	CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1778dd19a799SPoul-Henning Kamp 	if (bp->b_flags & B_INVAL) {
1779dd19a799SPoul-Henning Kamp 		brelse(bp);
1780dd19a799SPoul-Henning Kamp 		return (0);
1781dd19a799SPoul-Henning Kamp 	}
1782dd19a799SPoul-Henning Kamp 
1783dd19a799SPoul-Henning Kamp 	oldflags = bp->b_flags;
1784dd19a799SPoul-Henning Kamp 
1785d638e093SAttilio Rao 	if (!BUF_ISLOCKED(bp))
1786dd19a799SPoul-Henning Kamp 		panic("bufwrite: buffer is not busy???");
1787dd19a799SPoul-Henning Kamp 	s = splbio();
1788dd19a799SPoul-Henning Kamp 	/*
1789dd19a799SPoul-Henning Kamp 	 * If a background write is already in progress, delay
1790dd19a799SPoul-Henning Kamp 	 * writing this block if it is asynchronous. Otherwise
1791dd19a799SPoul-Henning Kamp 	 * wait for the background write to complete.
1792dd19a799SPoul-Henning Kamp 	 */
1793dd19a799SPoul-Henning Kamp 	BO_LOCK(bp->b_bufobj);
1794dd19a799SPoul-Henning Kamp 	if (bp->b_vflags & BV_BKGRDINPROG) {
1795dd19a799SPoul-Henning Kamp 		if (bp->b_flags & B_ASYNC) {
1796dd19a799SPoul-Henning Kamp 			BO_UNLOCK(bp->b_bufobj);
1797dd19a799SPoul-Henning Kamp 			splx(s);
1798dd19a799SPoul-Henning Kamp 			bdwrite(bp);
1799dd19a799SPoul-Henning Kamp 			return (0);
1800dd19a799SPoul-Henning Kamp 		}
1801dd19a799SPoul-Henning Kamp 		bp->b_vflags |= BV_BKGRDWAIT;
1802dd19a799SPoul-Henning Kamp 		msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0);
1803dd19a799SPoul-Henning Kamp 		if (bp->b_vflags & BV_BKGRDINPROG)
1804dd19a799SPoul-Henning Kamp 			panic("bufwrite: still writing");
1805dd19a799SPoul-Henning Kamp 	}
1806dd19a799SPoul-Henning Kamp 	BO_UNLOCK(bp->b_bufobj);
1807dd19a799SPoul-Henning Kamp 
1808dd19a799SPoul-Henning Kamp 	/* Mark the buffer clean */
1809dd19a799SPoul-Henning Kamp 	bundirty(bp);
1810dd19a799SPoul-Henning Kamp 
1811dd19a799SPoul-Henning Kamp 	/*
1812dd19a799SPoul-Henning Kamp 	 * If this buffer is marked for background writing and we
1813dd19a799SPoul-Henning Kamp 	 * do not have to wait for it, make a copy and write the
1814dd19a799SPoul-Henning Kamp 	 * copy so as to leave this buffer ready for further use.
1815dd19a799SPoul-Henning Kamp 	 *
1816dd19a799SPoul-Henning Kamp 	 * This optimization eats a lot of memory.  If we have a page
1817dd19a799SPoul-Henning Kamp 	 * or buffer shortfall we can't do it.
1818dd19a799SPoul-Henning Kamp 	 */
1819dd19a799SPoul-Henning Kamp 	if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
1820dd19a799SPoul-Henning Kamp 	    (bp->b_flags & B_ASYNC) &&
1821dd19a799SPoul-Henning Kamp 	    !vm_page_count_severe() &&
1822dd19a799SPoul-Henning Kamp 	    !buf_dirty_count_severe()) {
1823dd19a799SPoul-Henning Kamp 		KASSERT(bp->b_iodone == NULL,
1824dd19a799SPoul-Henning Kamp 		    ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
1825dd19a799SPoul-Henning Kamp 
1826dd19a799SPoul-Henning Kamp 		/* get a new block */
1827dd19a799SPoul-Henning Kamp 		newbp = geteblk(bp->b_bufsize);
1828dd19a799SPoul-Henning Kamp 
1829dd19a799SPoul-Henning Kamp 		/*
1830dd19a799SPoul-Henning Kamp 		 * set it to be identical to the old block.  We have to
1831dd19a799SPoul-Henning Kamp 		 * set b_lblkno and BKGRDMARKER before calling bgetvp()
1832dd19a799SPoul-Henning Kamp 		 * to avoid confusing the splay tree and gbincore().
1833dd19a799SPoul-Henning Kamp 		 */
1834dd19a799SPoul-Henning Kamp 		memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
1835dd19a799SPoul-Henning Kamp 		newbp->b_lblkno = bp->b_lblkno;
1836dd19a799SPoul-Henning Kamp 		newbp->b_xflags |= BX_BKGRDMARKER;
1837dd19a799SPoul-Henning Kamp 		BO_LOCK(bp->b_bufobj);
1838dd19a799SPoul-Henning Kamp 		bp->b_vflags |= BV_BKGRDINPROG;
1839dd19a799SPoul-Henning Kamp 		bgetvp(bp->b_vp, newbp);
1840dd19a799SPoul-Henning Kamp 		BO_UNLOCK(bp->b_bufobj);
1841dd19a799SPoul-Henning Kamp 		newbp->b_bufobj = &bp->b_vp->v_bufobj;
1842dd19a799SPoul-Henning Kamp 		newbp->b_blkno = bp->b_blkno;
1843dd19a799SPoul-Henning Kamp 		newbp->b_offset = bp->b_offset;
1844dd19a799SPoul-Henning Kamp 		newbp->b_iodone = ffs_backgroundwritedone;
1845dd19a799SPoul-Henning Kamp 		newbp->b_flags |= B_ASYNC;
1846dd19a799SPoul-Henning Kamp 		newbp->b_flags &= ~B_INVAL;
1847dd19a799SPoul-Henning Kamp 
1848dd19a799SPoul-Henning Kamp #ifdef SOFTUPDATES
1849dd19a799SPoul-Henning Kamp 		/* move over the dependencies */
185004533fc6SXin LI 		if (!LIST_EMPTY(&bp->b_dep))
1851dd19a799SPoul-Henning Kamp 			softdep_move_dependencies(bp, newbp);
1852dd19a799SPoul-Henning Kamp #endif
1853dd19a799SPoul-Henning Kamp 
1854dd19a799SPoul-Henning Kamp 		/*
1855dd19a799SPoul-Henning Kamp 		 * Initiate write on the copy, release the original to
1856dd19a799SPoul-Henning Kamp 		 * the B_LOCKED queue so that it cannot go away until
1857dd19a799SPoul-Henning Kamp 		 * the background write completes. If not locked it could go
1858dd19a799SPoul-Henning Kamp 		 * away and then be reconstituted while it was being written.
1859dd19a799SPoul-Henning Kamp 		 * If the reconstituted buffer were written, we could end up
1860dd19a799SPoul-Henning Kamp 		 * with two background copies being written at the same time.
1861dd19a799SPoul-Henning Kamp 		 */
1862dd19a799SPoul-Henning Kamp 		bqrelse(bp);
1863dd19a799SPoul-Henning Kamp 		bp = newbp;
1864dd19a799SPoul-Henning Kamp 	}
1865dd19a799SPoul-Henning Kamp 
1866dd19a799SPoul-Henning Kamp 	/* Let the normal bufwrite do the rest for us */
18679248a827STor Egge 	return (bufwrite(bp));
1868dd19a799SPoul-Henning Kamp }
1869dd19a799SPoul-Henning Kamp 
1870dd19a799SPoul-Henning Kamp 
18718dd56505SPoul-Henning Kamp static void
18726e77a041SPoul-Henning Kamp ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
18736e77a041SPoul-Henning Kamp {
1874153910e0SJeff Roberson 	struct vnode *vp;
1875153910e0SJeff Roberson 	int error;
18767de3839dSTor Egge 	struct buf *tbp;
18776e77a041SPoul-Henning Kamp 
1878153910e0SJeff Roberson 	vp = bo->__bo_vnode;
1879153910e0SJeff Roberson 	if (bp->b_iocmd == BIO_WRITE) {
1880153910e0SJeff Roberson 		if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
1881153910e0SJeff Roberson 		    bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
1882153910e0SJeff Roberson 		    (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
1883153910e0SJeff Roberson 			panic("ffs_geom_strategy: bad I/O");
1884153910e0SJeff Roberson 		bp->b_flags &= ~B_VALIDSUSPWRT;
1885153910e0SJeff Roberson 		if ((vp->v_vflag & VV_COPYONWRITE) &&
18867de3839dSTor Egge 		    vp->v_rdev->si_snapdata != NULL) {
18877de3839dSTor Egge 			if ((bp->b_flags & B_CLUSTER) != 0) {
1888868bb88fSTor Egge 				runningbufwakeup(bp);
18897de3839dSTor Egge 				TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
18907de3839dSTor Egge 					      b_cluster.cluster_entry) {
18917de3839dSTor Egge 					error = ffs_copyonwrite(vp, tbp);
18927de3839dSTor Egge 					if (error != 0 &&
1893153910e0SJeff Roberson 					    error != EOPNOTSUPP) {
1894153910e0SJeff Roberson 						bp->b_error = error;
1895153910e0SJeff Roberson 						bp->b_ioflags |= BIO_ERROR;
1896153910e0SJeff Roberson 						bufdone(bp);
1897153910e0SJeff Roberson 						return;
1898153910e0SJeff Roberson 					}
1899153910e0SJeff Roberson 				}
1900868bb88fSTor Egge 				bp->b_runningbufspace = bp->b_bufsize;
1901868bb88fSTor Egge 				atomic_add_int(&runningbufspace,
1902868bb88fSTor Egge 					       bp->b_runningbufspace);
19037de3839dSTor Egge 			} else {
19047de3839dSTor Egge 				error = ffs_copyonwrite(vp, bp);
19057de3839dSTor Egge 				if (error != 0 && error != EOPNOTSUPP) {
19067de3839dSTor Egge 					bp->b_error = error;
19077de3839dSTor Egge 					bp->b_ioflags |= BIO_ERROR;
19087de3839dSTor Egge 					bufdone(bp);
19097de3839dSTor Egge 					return;
19107de3839dSTor Egge 				}
19117de3839dSTor Egge 			}
19127de3839dSTor Egge 		}
19137de3839dSTor Egge #ifdef SOFTUPDATES
19147de3839dSTor Egge 		if ((bp->b_flags & B_CLUSTER) != 0) {
19157de3839dSTor Egge 			TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
19167de3839dSTor Egge 				      b_cluster.cluster_entry) {
191704533fc6SXin LI 				if (!LIST_EMPTY(&tbp->b_dep))
19187de3839dSTor Egge 					buf_start(tbp);
19197de3839dSTor Egge 			}
19207de3839dSTor Egge 		} else {
192104533fc6SXin LI 			if (!LIST_EMPTY(&bp->b_dep))
19227de3839dSTor Egge 				buf_start(bp);
19237de3839dSTor Egge 		}
19247de3839dSTor Egge 
19257de3839dSTor Egge #endif
19267de3839dSTor Egge 	}
192743920011SPoul-Henning Kamp 	g_vfs_strategy(bo, bp);
19286e77a041SPoul-Henning Kamp }
192952dfc8d7SKonstantin Belousov 
193052dfc8d7SKonstantin Belousov #ifdef	DDB
193152dfc8d7SKonstantin Belousov 
193252dfc8d7SKonstantin Belousov static void
193352dfc8d7SKonstantin Belousov db_print_ffs(struct ufsmount *ump)
193452dfc8d7SKonstantin Belousov {
193552dfc8d7SKonstantin Belousov 	db_printf("mp %p %s devvp %p fs %p su_wl %d su_wl_in %d su_deps %d "
193652dfc8d7SKonstantin Belousov 		  "su_req %d\n",
193752dfc8d7SKonstantin Belousov 	    ump->um_mountp, ump->um_mountp->mnt_stat.f_mntonname,
193852dfc8d7SKonstantin Belousov 	    ump->um_devvp, ump->um_fs, ump->softdep_on_worklist,
193952dfc8d7SKonstantin Belousov 	    ump->softdep_on_worklist_inprogress, ump->softdep_deps,
194052dfc8d7SKonstantin Belousov 	    ump->softdep_req);
194152dfc8d7SKonstantin Belousov }
194252dfc8d7SKonstantin Belousov 
194352dfc8d7SKonstantin Belousov DB_SHOW_COMMAND(ffs, db_show_ffs)
194452dfc8d7SKonstantin Belousov {
194552dfc8d7SKonstantin Belousov 	struct mount *mp;
194652dfc8d7SKonstantin Belousov 	struct ufsmount *ump;
194752dfc8d7SKonstantin Belousov 
194852dfc8d7SKonstantin Belousov 	if (have_addr) {
194952dfc8d7SKonstantin Belousov 		ump = VFSTOUFS((struct mount *)addr);
195052dfc8d7SKonstantin Belousov 		db_print_ffs(ump);
195152dfc8d7SKonstantin Belousov 		return;
195252dfc8d7SKonstantin Belousov 	}
195352dfc8d7SKonstantin Belousov 
195452dfc8d7SKonstantin Belousov 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
195552dfc8d7SKonstantin Belousov 		if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
195652dfc8d7SKonstantin Belousov 			db_print_ffs(VFSTOUFS(mp));
195752dfc8d7SKonstantin Belousov 	}
195852dfc8d7SKonstantin Belousov }
195952dfc8d7SKonstantin Belousov 
196052dfc8d7SKonstantin Belousov #endif	/* DDB */
1961