xref: /onnv-gate/usr/src/uts/common/fs/ufs/ufs_vfsops.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #include <sys/types.h>
44*0Sstevel@tonic-gate #include <sys/t_lock.h>
45*0Sstevel@tonic-gate #include <sys/param.h>
46*0Sstevel@tonic-gate #include <sys/systm.h>
47*0Sstevel@tonic-gate #include <sys/bitmap.h>
48*0Sstevel@tonic-gate #include <sys/sysmacros.h>
49*0Sstevel@tonic-gate #include <sys/kmem.h>
50*0Sstevel@tonic-gate #include <sys/signal.h>
51*0Sstevel@tonic-gate #include <sys/user.h>
52*0Sstevel@tonic-gate #include <sys/proc.h>
53*0Sstevel@tonic-gate #include <sys/disp.h>
54*0Sstevel@tonic-gate #include <sys/buf.h>
55*0Sstevel@tonic-gate #include <sys/pathname.h>
56*0Sstevel@tonic-gate #include <sys/vfs.h>
57*0Sstevel@tonic-gate #include <sys/vnode.h>
58*0Sstevel@tonic-gate #include <sys/file.h>
59*0Sstevel@tonic-gate #include <sys/uio.h>
60*0Sstevel@tonic-gate #include <sys/dkio.h>
61*0Sstevel@tonic-gate #include <sys/cred.h>
62*0Sstevel@tonic-gate #include <sys/conf.h>
63*0Sstevel@tonic-gate #include <sys/dnlc.h>
64*0Sstevel@tonic-gate #include <sys/kstat.h>
65*0Sstevel@tonic-gate #include <sys/acl.h>
66*0Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
67*0Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
68*0Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
69*0Sstevel@tonic-gate #include <sys/fs/ufs_mount.h>
70*0Sstevel@tonic-gate #include <sys/fs/ufs_acl.h>
71*0Sstevel@tonic-gate #include <sys/fs/ufs_panic.h>
72*0Sstevel@tonic-gate #include <sys/fs/ufs_bio.h>
73*0Sstevel@tonic-gate #include <sys/fs/ufs_quota.h>
74*0Sstevel@tonic-gate #include <sys/fs/ufs_log.h>
75*0Sstevel@tonic-gate #undef NFS
76*0Sstevel@tonic-gate #include <sys/statvfs.h>
77*0Sstevel@tonic-gate #include <sys/mount.h>
78*0Sstevel@tonic-gate #include <sys/mntent.h>
79*0Sstevel@tonic-gate #include <sys/swap.h>
80*0Sstevel@tonic-gate #include <sys/errno.h>
81*0Sstevel@tonic-gate #include <sys/debug.h>
82*0Sstevel@tonic-gate #include "fs/fs_subr.h"
83*0Sstevel@tonic-gate #include <sys/cmn_err.h>
84*0Sstevel@tonic-gate #include <sys/dnlc.h>
85*0Sstevel@tonic-gate #include <sys/fssnap_if.h>
86*0Sstevel@tonic-gate #include <sys/sunddi.h>
87*0Sstevel@tonic-gate #include <sys/bootconf.h>
88*0Sstevel@tonic-gate #include <sys/policy.h>
89*0Sstevel@tonic-gate #include <sys/zone.h>
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate /*
92*0Sstevel@tonic-gate  * This is the loadable module wrapper.
93*0Sstevel@tonic-gate  */
94*0Sstevel@tonic-gate #include <sys/modctl.h>
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate int			ufsfstype;
97*0Sstevel@tonic-gate vfsops_t		*ufs_vfsops;
98*0Sstevel@tonic-gate static int		ufsinit(int, char *);
99*0Sstevel@tonic-gate static int		mountfs();
100*0Sstevel@tonic-gate extern int		highbit();
101*0Sstevel@tonic-gate extern struct instats	ins;
102*0Sstevel@tonic-gate extern struct vnode *common_specvp(struct vnode *vp);
103*0Sstevel@tonic-gate extern vfs_t		EIO_vfs;
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate struct  dquot *dquot, *dquotNDQUOT;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate /*
108*0Sstevel@tonic-gate  * Cylinder group summary information handling tunable.
109*0Sstevel@tonic-gate  * This defines when these deltas get logged.
110*0Sstevel@tonic-gate  * If the number of cylinders in the file system is over the
111*0Sstevel@tonic-gate  * tunable then we log csum updates. Otherwise the updates are only
112*0Sstevel@tonic-gate  * done for performance on unmount. After a panic they can be
113*0Sstevel@tonic-gate  * quickly constructed during mounting. See ufs_construct_si()
114*0Sstevel@tonic-gate  * called from ufs_getsummaryinfo().
115*0Sstevel@tonic-gate  *
116*0Sstevel@tonic-gate  * This performance feature can of course be disabled by setting
117*0Sstevel@tonic-gate  * ufs_ncg_log to 0, and fully enabled by setting it to 0xffffffff.
118*0Sstevel@tonic-gate  */
119*0Sstevel@tonic-gate #define	UFS_LOG_NCG_DEFAULT 10000
120*0Sstevel@tonic-gate uint32_t ufs_ncg_log = UFS_LOG_NCG_DEFAULT;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate /*
123*0Sstevel@tonic-gate  * ufs_clean_root indicates whether the root fs went down cleanly
124*0Sstevel@tonic-gate  */
125*0Sstevel@tonic-gate static int ufs_clean_root = 0;
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate /*
128*0Sstevel@tonic-gate  * UFS Mount options table
129*0Sstevel@tonic-gate  */
130*0Sstevel@tonic-gate static char *intr_cancel[] = { MNTOPT_NOINTR, NULL };
131*0Sstevel@tonic-gate static char *nointr_cancel[] = { MNTOPT_INTR, NULL };
132*0Sstevel@tonic-gate static char *forcedirectio_cancel[] = { MNTOPT_NOFORCEDIRECTIO, NULL };
133*0Sstevel@tonic-gate static char *noforcedirectio_cancel[] = { MNTOPT_FORCEDIRECTIO, NULL };
134*0Sstevel@tonic-gate static char *largefiles_cancel[] = { MNTOPT_NOLARGEFILES, NULL };
135*0Sstevel@tonic-gate static char *nolargefiles_cancel[] = { MNTOPT_LARGEFILES, NULL };
136*0Sstevel@tonic-gate static char *logging_cancel[] = { MNTOPT_NOLOGGING, NULL };
137*0Sstevel@tonic-gate static char *nologging_cancel[] = { MNTOPT_LOGGING, NULL };
138*0Sstevel@tonic-gate static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
139*0Sstevel@tonic-gate static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
140*0Sstevel@tonic-gate static char *quota_cancel[] = { MNTOPT_NOQUOTA, NULL };
141*0Sstevel@tonic-gate static char *noquota_cancel[] = { MNTOPT_QUOTA, NULL };
142*0Sstevel@tonic-gate static char *dfratime_cancel[] = { MNTOPT_NODFRATIME, NULL };
143*0Sstevel@tonic-gate static char *nodfratime_cancel[] = { MNTOPT_DFRATIME, NULL };
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate static mntopt_t mntopts[] = {
146*0Sstevel@tonic-gate /*
147*0Sstevel@tonic-gate  *	option name		cancel option	default arg	flags
148*0Sstevel@tonic-gate  *		ufs arg flag
149*0Sstevel@tonic-gate  */
150*0Sstevel@tonic-gate 	{ MNTOPT_INTR,		intr_cancel,	NULL,		MO_DEFAULT,
151*0Sstevel@tonic-gate 		(void *)0 },
152*0Sstevel@tonic-gate 	{ MNTOPT_NOINTR,	nointr_cancel,	NULL,		0,
153*0Sstevel@tonic-gate 		(void *)UFSMNT_NOINTR },
154*0Sstevel@tonic-gate 	{ MNTOPT_SYNCDIR,	NULL,		NULL,		0,
155*0Sstevel@tonic-gate 		(void *)UFSMNT_SYNCDIR },
156*0Sstevel@tonic-gate 	{ MNTOPT_FORCEDIRECTIO,	forcedirectio_cancel, NULL,	0,
157*0Sstevel@tonic-gate 		(void *)UFSMNT_FORCEDIRECTIO },
158*0Sstevel@tonic-gate 	{ MNTOPT_NOFORCEDIRECTIO, noforcedirectio_cancel, NULL, 0,
159*0Sstevel@tonic-gate 		(void *)UFSMNT_NOFORCEDIRECTIO },
160*0Sstevel@tonic-gate 	{ MNTOPT_NOSETSEC,	NULL,		NULL,		0,
161*0Sstevel@tonic-gate 		(void *)UFSMNT_NOSETSEC },
162*0Sstevel@tonic-gate 	{ MNTOPT_LARGEFILES,	largefiles_cancel, NULL,	MO_DEFAULT,
163*0Sstevel@tonic-gate 		(void *)UFSMNT_LARGEFILES },
164*0Sstevel@tonic-gate 	{ MNTOPT_NOLARGEFILES,	nolargefiles_cancel, NULL,	0,
165*0Sstevel@tonic-gate 		(void *)0 },
166*0Sstevel@tonic-gate 	{ MNTOPT_LOGGING,	logging_cancel, NULL,		MO_TAG,
167*0Sstevel@tonic-gate 		(void *)UFSMNT_LOGGING },
168*0Sstevel@tonic-gate 	{ MNTOPT_NOLOGGING,	nologging_cancel, NULL,
169*0Sstevel@tonic-gate 		MO_NODISPLAY|MO_DEFAULT|MO_TAG, (void *)0 },
170*0Sstevel@tonic-gate 	{ MNTOPT_QUOTA,		quota_cancel, NULL,		MO_IGNORE,
171*0Sstevel@tonic-gate 		(void *)0 },
172*0Sstevel@tonic-gate 	{ MNTOPT_NOQUOTA,	noquota_cancel,	NULL,
173*0Sstevel@tonic-gate 		MO_NODISPLAY|MO_DEFAULT, (void *)0 },
174*0Sstevel@tonic-gate 	{ MNTOPT_GLOBAL,	NULL,		NULL,		0,
175*0Sstevel@tonic-gate 		(void *)0 },
176*0Sstevel@tonic-gate 	{ MNTOPT_XATTR,	xattr_cancel,		NULL,		MO_DEFAULT,
177*0Sstevel@tonic-gate 		(void *)0 },
178*0Sstevel@tonic-gate 	{ MNTOPT_NOXATTR,	noxattr_cancel,		NULL,		0,
179*0Sstevel@tonic-gate 		(void *)0 },
180*0Sstevel@tonic-gate 	{ MNTOPT_NOATIME,	NULL,		NULL,		0,
181*0Sstevel@tonic-gate 		(void *)UFSMNT_NOATIME },
182*0Sstevel@tonic-gate 	{ MNTOPT_DFRATIME,	dfratime_cancel, NULL,		0,
183*0Sstevel@tonic-gate 		(void *)0 },
184*0Sstevel@tonic-gate 	{ MNTOPT_NODFRATIME,	nodfratime_cancel, NULL,
185*0Sstevel@tonic-gate 		MO_NODISPLAY|MO_DEFAULT, (void *)UFSMNT_NODFRATIME },
186*0Sstevel@tonic-gate 	{ MNTOPT_ONERROR,	NULL,		UFSMNT_ONERROR_PANIC_STR,
187*0Sstevel@tonic-gate 		MO_DEFAULT|MO_HASVALUE,	(void *)0 },
188*0Sstevel@tonic-gate };
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate static mntopts_t ufs_mntopts = {
191*0Sstevel@tonic-gate 	sizeof (mntopts) / sizeof (mntopt_t),
192*0Sstevel@tonic-gate 	mntopts
193*0Sstevel@tonic-gate };
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate static vfsdef_t vfw = {
196*0Sstevel@tonic-gate 	VFSDEF_VERSION,
197*0Sstevel@tonic-gate 	"ufs",
198*0Sstevel@tonic-gate 	ufsinit,
199*0Sstevel@tonic-gate 	VSW_HASPROTO|VSW_CANREMOUNT,
200*0Sstevel@tonic-gate 	&ufs_mntopts
201*0Sstevel@tonic-gate };
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate /*
204*0Sstevel@tonic-gate  * Module linkage information for the kernel.
205*0Sstevel@tonic-gate  */
206*0Sstevel@tonic-gate extern struct mod_ops mod_fsops;
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate static struct modlfs modlfs = {
209*0Sstevel@tonic-gate 	&mod_fsops, "filesystem for ufs", &vfw
210*0Sstevel@tonic-gate };
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
213*0Sstevel@tonic-gate 	MODREV_1, (void *)&modlfs, NULL
214*0Sstevel@tonic-gate };
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate /*
217*0Sstevel@tonic-gate  * An attempt has been made to make this module unloadable.  In order to
218*0Sstevel@tonic-gate  * test it, we need a system in which the root fs is NOT ufs.  THIS HAS NOT
219*0Sstevel@tonic-gate  * BEEN DONE
220*0Sstevel@tonic-gate  */
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate extern kstat_t *ufs_inode_kstat;
223*0Sstevel@tonic-gate extern uint_t ufs_lockfs_key;
224*0Sstevel@tonic-gate extern void ufs_lockfs_tsd_destructor(void *);
225*0Sstevel@tonic-gate extern uint_t bypass_snapshot_throttle_key;
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate int
228*0Sstevel@tonic-gate _init(void)
229*0Sstevel@tonic-gate {
230*0Sstevel@tonic-gate 	/*
231*0Sstevel@tonic-gate 	 * Create an index into the per thread array so that any thread doing
232*0Sstevel@tonic-gate 	 * VOP will have a lockfs mark on it.
233*0Sstevel@tonic-gate 	 */
234*0Sstevel@tonic-gate 	tsd_create(&ufs_lockfs_key, ufs_lockfs_tsd_destructor);
235*0Sstevel@tonic-gate 	tsd_create(&bypass_snapshot_throttle_key, NULL);
236*0Sstevel@tonic-gate 	return (mod_install(&modlinkage));
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate int
240*0Sstevel@tonic-gate _fini(void)
241*0Sstevel@tonic-gate {
242*0Sstevel@tonic-gate 	return (EBUSY);
243*0Sstevel@tonic-gate }
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate int
246*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
247*0Sstevel@tonic-gate {
248*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
249*0Sstevel@tonic-gate }
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate extern struct vnode *makespecvp(dev_t dev, vtype_t type);
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate extern kmutex_t	ufs_scan_lock;
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate static int mountfs(struct vfs *, enum whymountroot, struct vnode *, char *,
256*0Sstevel@tonic-gate 		struct cred *, int, void *, int);
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate static int
260*0Sstevel@tonic-gate ufs_mount(struct vfs *vfsp, struct vnode *mvp, struct mounta *uap,
261*0Sstevel@tonic-gate 	struct cred *cr)
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate {
264*0Sstevel@tonic-gate 	char *data = uap->dataptr;
265*0Sstevel@tonic-gate 	int datalen = uap->datalen;
266*0Sstevel@tonic-gate 	dev_t dev;
267*0Sstevel@tonic-gate 	struct vnode *bvp;
268*0Sstevel@tonic-gate 	struct pathname dpn;
269*0Sstevel@tonic-gate 	int error;
270*0Sstevel@tonic-gate 	enum whymountroot why = ROOT_INIT;
271*0Sstevel@tonic-gate 	struct ufs_args args;
272*0Sstevel@tonic-gate 	int oflag, aflag;
273*0Sstevel@tonic-gate 	int fromspace = (uap->flags & MS_SYSSPACE) ?
274*0Sstevel@tonic-gate 	    UIO_SYSSPACE : UIO_USERSPACE;
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
277*0Sstevel@tonic-gate 		return (error);
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	if (mvp->v_type != VDIR)
280*0Sstevel@tonic-gate 		return (ENOTDIR);
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	mutex_enter(&mvp->v_lock);
283*0Sstevel@tonic-gate 	if ((uap->flags & MS_REMOUNT) == 0 &&
284*0Sstevel@tonic-gate 	    (uap->flags & MS_OVERLAY) == 0 &&
285*0Sstevel@tonic-gate 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
286*0Sstevel@tonic-gate 		mutex_exit(&mvp->v_lock);
287*0Sstevel@tonic-gate 		return (EBUSY);
288*0Sstevel@tonic-gate 	}
289*0Sstevel@tonic-gate 	mutex_exit(&mvp->v_lock);
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	/*
292*0Sstevel@tonic-gate 	 * Get arguments
293*0Sstevel@tonic-gate 	 */
294*0Sstevel@tonic-gate 	bzero(&args, sizeof (args));
295*0Sstevel@tonic-gate 	if ((uap->flags & MS_DATA) && data != NULL && datalen != 0) {
296*0Sstevel@tonic-gate 		int copy_result = 0;
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate 		if (datalen > sizeof (args))
299*0Sstevel@tonic-gate 			return (EINVAL);
300*0Sstevel@tonic-gate 		if (uap->flags & MS_SYSSPACE)
301*0Sstevel@tonic-gate 			bcopy(data, &args, datalen);
302*0Sstevel@tonic-gate 		else
303*0Sstevel@tonic-gate 			copy_result = copyin(data, &args, datalen);
304*0Sstevel@tonic-gate 		if (copy_result)
305*0Sstevel@tonic-gate 			return (EFAULT);
306*0Sstevel@tonic-gate 		datalen = sizeof (struct ufs_args);
307*0Sstevel@tonic-gate 	} else {
308*0Sstevel@tonic-gate 		datalen = 0;
309*0Sstevel@tonic-gate 	}
310*0Sstevel@tonic-gate 	/*
311*0Sstevel@tonic-gate 	 * Read in the mount point pathname
312*0Sstevel@tonic-gate 	 * (so we can record the directory the file system was last mounted on).
313*0Sstevel@tonic-gate 	 */
314*0Sstevel@tonic-gate 	if (error = pn_get(uap->dir, fromspace, &dpn))
315*0Sstevel@tonic-gate 		return (error);
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 	/*
318*0Sstevel@tonic-gate 	 * Resolve path name of special file being mounted.
319*0Sstevel@tonic-gate 	 */
320*0Sstevel@tonic-gate 	if (error = lookupname(uap->spec, fromspace, FOLLOW, NULL, &bvp)) {
321*0Sstevel@tonic-gate 		pn_free(&dpn);
322*0Sstevel@tonic-gate 		return (error);
323*0Sstevel@tonic-gate 	}
324*0Sstevel@tonic-gate 	if (bvp->v_type != VBLK) {
325*0Sstevel@tonic-gate 		VN_RELE(bvp);
326*0Sstevel@tonic-gate 		pn_free(&dpn);
327*0Sstevel@tonic-gate 		return (ENOTBLK);
328*0Sstevel@tonic-gate 	}
329*0Sstevel@tonic-gate 	dev = bvp->v_rdev;
330*0Sstevel@tonic-gate 	if (getmajor(dev) >= devcnt) {
331*0Sstevel@tonic-gate 		pn_free(&dpn);
332*0Sstevel@tonic-gate 		VN_RELE(bvp);
333*0Sstevel@tonic-gate 		return (ENXIO);
334*0Sstevel@tonic-gate 	}
335*0Sstevel@tonic-gate 	if (uap->flags & MS_REMOUNT)
336*0Sstevel@tonic-gate 		why = ROOT_REMOUNT;
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 	/*
339*0Sstevel@tonic-gate 	 * In SunCluster, requests to a global device are satisfied by
340*0Sstevel@tonic-gate 	 * a local device. We substitute the global pxfs node with a
341*0Sstevel@tonic-gate 	 * local spec node here.
342*0Sstevel@tonic-gate 	 */
343*0Sstevel@tonic-gate 	if (IS_PXFSVP(bvp)) {
344*0Sstevel@tonic-gate 		VN_RELE(bvp);
345*0Sstevel@tonic-gate 		bvp = makespecvp(dev, VBLK);
346*0Sstevel@tonic-gate 	}
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 	/*
349*0Sstevel@tonic-gate 	 * Open block device mounted on.  We need this to
350*0Sstevel@tonic-gate 	 * check whether the caller has sufficient rights to
351*0Sstevel@tonic-gate 	 * access the device in question.
352*0Sstevel@tonic-gate 	 * When bio is fixed for vnodes this can all be vnode
353*0Sstevel@tonic-gate 	 * operations.
354*0Sstevel@tonic-gate 	 */
355*0Sstevel@tonic-gate 	if ((vfsp->vfs_flag & VFS_RDONLY) != 0 ||
356*0Sstevel@tonic-gate 	    (uap->flags & MS_RDONLY) != 0) {
357*0Sstevel@tonic-gate 		oflag = FREAD;
358*0Sstevel@tonic-gate 		aflag = VREAD;
359*0Sstevel@tonic-gate 	} else {
360*0Sstevel@tonic-gate 		oflag = FREAD | FWRITE;
361*0Sstevel@tonic-gate 		aflag = VREAD | VWRITE;
362*0Sstevel@tonic-gate 	}
363*0Sstevel@tonic-gate 	if ((error = VOP_ACCESS(bvp, aflag, 0, cr)) != 0 ||
364*0Sstevel@tonic-gate 	    (error = secpolicy_spec_open(cr, bvp, oflag)) != 0) {
365*0Sstevel@tonic-gate 		pn_free(&dpn);
366*0Sstevel@tonic-gate 		VN_RELE(bvp);
367*0Sstevel@tonic-gate 		return (error);
368*0Sstevel@tonic-gate 	}
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate 	/*
371*0Sstevel@tonic-gate 	 * Ensure that this device isn't already mounted or in progress on a
372*0Sstevel@tonic-gate 	 * mount unless this is a REMOUNT request or we are told to suppress
373*0Sstevel@tonic-gate 	 * mount checks. Global mounts require special handling.
374*0Sstevel@tonic-gate 	 */
375*0Sstevel@tonic-gate 	if ((uap->flags & MS_NOCHECK) == 0) {
376*0Sstevel@tonic-gate 		if ((uap->flags & MS_GLOBAL) == 0 &&
377*0Sstevel@tonic-gate 		    vfs_devmounting(dev, vfsp)) {
378*0Sstevel@tonic-gate 			pn_free(&dpn);
379*0Sstevel@tonic-gate 			VN_RELE(bvp);
380*0Sstevel@tonic-gate 			return (EBUSY);
381*0Sstevel@tonic-gate 		}
382*0Sstevel@tonic-gate 		if (vfs_devismounted(dev)) {
383*0Sstevel@tonic-gate 			if ((uap->flags & MS_REMOUNT) == 0) {
384*0Sstevel@tonic-gate 				pn_free(&dpn);
385*0Sstevel@tonic-gate 				VN_RELE(bvp);
386*0Sstevel@tonic-gate 				return (EBUSY);
387*0Sstevel@tonic-gate 			}
388*0Sstevel@tonic-gate 		}
389*0Sstevel@tonic-gate 	}
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 	/*
392*0Sstevel@tonic-gate 	 * If the device is a tape, mount it read only
393*0Sstevel@tonic-gate 	 */
394*0Sstevel@tonic-gate 	if (devopsp[getmajor(dev)]->devo_cb_ops->cb_flag & D_TAPE) {
395*0Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_RDONLY;
396*0Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
397*0Sstevel@tonic-gate 	}
398*0Sstevel@tonic-gate 	if (uap->flags & MS_RDONLY)
399*0Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_RDONLY;
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	/*
402*0Sstevel@tonic-gate 	 * Mount the filesystem, free the device vnode on error.
403*0Sstevel@tonic-gate 	 */
404*0Sstevel@tonic-gate 	error = mountfs(vfsp, why, bvp, dpn.pn_path, cr, 0, &args, datalen);
405*0Sstevel@tonic-gate 	pn_free(&dpn);
406*0Sstevel@tonic-gate 	if (error) {
407*0Sstevel@tonic-gate 		VN_RELE(bvp);
408*0Sstevel@tonic-gate 	}
409*0Sstevel@tonic-gate 	return (error);
410*0Sstevel@tonic-gate }
411*0Sstevel@tonic-gate /*
412*0Sstevel@tonic-gate  * Mount root file system.
413*0Sstevel@tonic-gate  * "why" is ROOT_INIT on initial call ROOT_REMOUNT if called to
414*0Sstevel@tonic-gate  * remount the root file system, and ROOT_UNMOUNT if called to
415*0Sstevel@tonic-gate  * unmount the root (e.g., as part of a system shutdown).
416*0Sstevel@tonic-gate  *
417*0Sstevel@tonic-gate  * XXX - this may be partially machine-dependent; it, along with the VFS_SWAPVP
418*0Sstevel@tonic-gate  * operation, goes along with auto-configuration.  A mechanism should be
419*0Sstevel@tonic-gate  * provided by which machine-INdependent code in the kernel can say "get me the
420*0Sstevel@tonic-gate  * right root file system" and "get me the right initial swap area", and have
421*0Sstevel@tonic-gate  * that done in what may well be a machine-dependent fashion.
422*0Sstevel@tonic-gate  * Unfortunately, it is also file-system-type dependent (NFS gets it via
423*0Sstevel@tonic-gate  * bootparams calls, UFS gets it from various and sundry machine-dependent
424*0Sstevel@tonic-gate  * mechanisms, as SPECFS does for swap).
425*0Sstevel@tonic-gate  */
426*0Sstevel@tonic-gate static int
427*0Sstevel@tonic-gate ufs_mountroot(struct vfs *vfsp, enum whymountroot why)
428*0Sstevel@tonic-gate {
429*0Sstevel@tonic-gate 	struct fs *fsp;
430*0Sstevel@tonic-gate 	int error;
431*0Sstevel@tonic-gate 	static int ufsrootdone = 0;
432*0Sstevel@tonic-gate 	dev_t rootdev;
433*0Sstevel@tonic-gate 	struct vnode *vp;
434*0Sstevel@tonic-gate 	struct vnode *devvp = 0;
435*0Sstevel@tonic-gate 	int ovflags;
436*0Sstevel@tonic-gate 	int doclkset;
437*0Sstevel@tonic-gate 	ufsvfs_t *ufsvfsp;
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate 	if (why == ROOT_INIT) {
440*0Sstevel@tonic-gate 		if (ufsrootdone++)
441*0Sstevel@tonic-gate 			return (EBUSY);
442*0Sstevel@tonic-gate 		rootdev = getrootdev();
443*0Sstevel@tonic-gate 		if (rootdev == (dev_t)NODEV)
444*0Sstevel@tonic-gate 			return (ENODEV);
445*0Sstevel@tonic-gate 		vfsp->vfs_dev = rootdev;
446*0Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_RDONLY;
447*0Sstevel@tonic-gate 	} else if (why == ROOT_REMOUNT) {
448*0Sstevel@tonic-gate 		vp = ((struct ufsvfs *)vfsp->vfs_data)->vfs_devvp;
449*0Sstevel@tonic-gate 		(void) dnlc_purge_vfsp(vfsp, 0);
450*0Sstevel@tonic-gate 		vp = common_specvp(vp);
451*0Sstevel@tonic-gate 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_INVAL, CRED());
452*0Sstevel@tonic-gate 		(void) bfinval(vfsp->vfs_dev, 0);
453*0Sstevel@tonic-gate 		fsp = getfs(vfsp);
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate 		ovflags = vfsp->vfs_flag;
456*0Sstevel@tonic-gate 		vfsp->vfs_flag &= ~VFS_RDONLY;
457*0Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_REMOUNT;
458*0Sstevel@tonic-gate 		rootdev = vfsp->vfs_dev;
459*0Sstevel@tonic-gate 	} else if (why == ROOT_UNMOUNT) {
460*0Sstevel@tonic-gate 		if (vfs_lock(vfsp) == 0) {
461*0Sstevel@tonic-gate 			(void) ufs_flush(vfsp);
462*0Sstevel@tonic-gate 			/*
463*0Sstevel@tonic-gate 			 * Mark the log as fully rolled
464*0Sstevel@tonic-gate 			 */
465*0Sstevel@tonic-gate 			ufsvfsp = (ufsvfs_t *)vfsp->vfs_data;
466*0Sstevel@tonic-gate 			fsp = ufsvfsp->vfs_fs;
467*0Sstevel@tonic-gate 			if (TRANS_ISTRANS(ufsvfsp) &&
468*0Sstevel@tonic-gate 			    !TRANS_ISERROR(ufsvfsp) &&
469*0Sstevel@tonic-gate 			    (fsp->fs_rolled == FS_NEED_ROLL)) {
470*0Sstevel@tonic-gate 				ml_unit_t *ul = ufsvfsp->vfs_log;
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate 				error = ufs_putsummaryinfo(ul->un_dev,
473*0Sstevel@tonic-gate 				    ufsvfsp, fsp);
474*0Sstevel@tonic-gate 				if (error == 0) {
475*0Sstevel@tonic-gate 					fsp->fs_rolled = FS_ALL_ROLLED;
476*0Sstevel@tonic-gate 					UFS_BWRITE2(NULL, ufsvfsp->vfs_bufp);
477*0Sstevel@tonic-gate 				}
478*0Sstevel@tonic-gate 			}
479*0Sstevel@tonic-gate 			vfs_unlock(vfsp);
480*0Sstevel@tonic-gate 		} else {
481*0Sstevel@tonic-gate 			ufs_update(0);
482*0Sstevel@tonic-gate 		}
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate 		vp = ((struct ufsvfs *)vfsp->vfs_data)->vfs_devvp;
485*0Sstevel@tonic-gate 		(void) VOP_CLOSE(vp, FREAD|FWRITE, 1,
486*0Sstevel@tonic-gate 			(offset_t)0, CRED());
487*0Sstevel@tonic-gate 		return (0);
488*0Sstevel@tonic-gate 	}
489*0Sstevel@tonic-gate 	error = vfs_lock(vfsp);
490*0Sstevel@tonic-gate 	if (error)
491*0Sstevel@tonic-gate 		return (error);
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 	devvp = makespecvp(rootdev, VBLK);
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate 	/* If RO media, don't call clkset() (see below) */
496*0Sstevel@tonic-gate 	doclkset = 1;
497*0Sstevel@tonic-gate 	if (why == ROOT_INIT) {
498*0Sstevel@tonic-gate 		error = VOP_OPEN(&devvp, FREAD|FWRITE, CRED());
499*0Sstevel@tonic-gate 		if (error == 0) {
500*0Sstevel@tonic-gate 			(void) VOP_CLOSE(devvp, FREAD|FWRITE, 1,
501*0Sstevel@tonic-gate 				(offset_t)0, CRED());
502*0Sstevel@tonic-gate 		} else {
503*0Sstevel@tonic-gate 			doclkset = 0;
504*0Sstevel@tonic-gate 		}
505*0Sstevel@tonic-gate 	}
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 	error = mountfs(vfsp, why, devvp, "/", CRED(), 1, NULL, 0);
508*0Sstevel@tonic-gate 	/*
509*0Sstevel@tonic-gate 	 * XXX - assumes root device is not indirect, because we don't set
510*0Sstevel@tonic-gate 	 * rootvp.  Is rootvp used for anything?  If so, make another arg
511*0Sstevel@tonic-gate 	 * to mountfs.
512*0Sstevel@tonic-gate 	 */
513*0Sstevel@tonic-gate 	if (error) {
514*0Sstevel@tonic-gate 		vfs_unlock(vfsp);
515*0Sstevel@tonic-gate 		if (why == ROOT_REMOUNT)
516*0Sstevel@tonic-gate 			vfsp->vfs_flag = ovflags;
517*0Sstevel@tonic-gate 		if (rootvp) {
518*0Sstevel@tonic-gate 			VN_RELE(rootvp);
519*0Sstevel@tonic-gate 			rootvp = (struct vnode *)0;
520*0Sstevel@tonic-gate 		}
521*0Sstevel@tonic-gate 		VN_RELE(devvp);
522*0Sstevel@tonic-gate 		return (error);
523*0Sstevel@tonic-gate 	}
524*0Sstevel@tonic-gate 	if (why == ROOT_INIT)
525*0Sstevel@tonic-gate 		vfs_add((struct vnode *)0, vfsp,
526*0Sstevel@tonic-gate 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
527*0Sstevel@tonic-gate 	vfs_unlock(vfsp);
528*0Sstevel@tonic-gate 	fsp = getfs(vfsp);
529*0Sstevel@tonic-gate 	clkset(doclkset ? fsp->fs_time : -1);
530*0Sstevel@tonic-gate 	ufsvfsp = (ufsvfs_t *)vfsp->vfs_data;
531*0Sstevel@tonic-gate 	if (ufsvfsp->vfs_log) {
532*0Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_LOGGING, NULL, 0);
533*0Sstevel@tonic-gate 	}
534*0Sstevel@tonic-gate 	return (0);
535*0Sstevel@tonic-gate }
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate static int
538*0Sstevel@tonic-gate remountfs(struct vfs *vfsp, dev_t dev, void *raw_argsp, int args_len)
539*0Sstevel@tonic-gate {
540*0Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
541*0Sstevel@tonic-gate 	struct ulockfs *ulp = &ufsvfsp->vfs_ulockfs;
542*0Sstevel@tonic-gate 	struct buf *bp = ufsvfsp->vfs_bufp;
543*0Sstevel@tonic-gate 	struct fs *fsp = (struct fs *)bp->b_un.b_addr;
544*0Sstevel@tonic-gate 	struct fs *fspt;
545*0Sstevel@tonic-gate 	struct buf *tpt = 0;
546*0Sstevel@tonic-gate 	int error = 0;
547*0Sstevel@tonic-gate 	int flags = 0;
548*0Sstevel@tonic-gate 
549*0Sstevel@tonic-gate 	if (args_len == sizeof (struct ufs_args) && raw_argsp)
550*0Sstevel@tonic-gate 		flags = ((struct ufs_args *)raw_argsp)->flags;
551*0Sstevel@tonic-gate 
552*0Sstevel@tonic-gate 	/* cannot remount to RDONLY */
553*0Sstevel@tonic-gate 	if (vfsp->vfs_flag & VFS_RDONLY)
554*0Sstevel@tonic-gate 		return (EINVAL);
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate 	/* whoops, wrong dev */
557*0Sstevel@tonic-gate 	if (vfsp->vfs_dev != dev)
558*0Sstevel@tonic-gate 		return (EINVAL);
559*0Sstevel@tonic-gate 
560*0Sstevel@tonic-gate 	/*
561*0Sstevel@tonic-gate 	 * synchronize w/ufs ioctls
562*0Sstevel@tonic-gate 	 */
563*0Sstevel@tonic-gate 	mutex_enter(&ulp->ul_lock);
564*0Sstevel@tonic-gate 
565*0Sstevel@tonic-gate 	/*
566*0Sstevel@tonic-gate 	 * reset options
567*0Sstevel@tonic-gate 	 */
568*0Sstevel@tonic-gate 	ufsvfsp->vfs_nointr  = flags & UFSMNT_NOINTR;
569*0Sstevel@tonic-gate 	ufsvfsp->vfs_syncdir = flags & UFSMNT_SYNCDIR;
570*0Sstevel@tonic-gate 	ufsvfsp->vfs_nosetsec = flags & UFSMNT_NOSETSEC;
571*0Sstevel@tonic-gate 	ufsvfsp->vfs_noatime = flags & UFSMNT_NOATIME;
572*0Sstevel@tonic-gate 	if ((flags & UFSMNT_NODFRATIME) || ufsvfsp->vfs_noatime)
573*0Sstevel@tonic-gate 		ufsvfsp->vfs_dfritime &= ~UFS_DFRATIME;
574*0Sstevel@tonic-gate 	else	/* dfratime, default behavior */
575*0Sstevel@tonic-gate 		ufsvfsp->vfs_dfritime |= UFS_DFRATIME;
576*0Sstevel@tonic-gate 	if (flags & UFSMNT_FORCEDIRECTIO)
577*0Sstevel@tonic-gate 		ufsvfsp->vfs_forcedirectio = 1;
578*0Sstevel@tonic-gate 	else	/* default is no direct I/O */
579*0Sstevel@tonic-gate 		ufsvfsp->vfs_forcedirectio = 0;
580*0Sstevel@tonic-gate 	ufsvfsp->vfs_iotstamp = lbolt;
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 	/*
583*0Sstevel@tonic-gate 	 * set largefiles flag in ufsvfs equal to the
584*0Sstevel@tonic-gate 	 * value passed in by the mount command. If
585*0Sstevel@tonic-gate 	 * it is "nolargefiles", and the flag is set
586*0Sstevel@tonic-gate 	 * in the superblock, the mount fails.
587*0Sstevel@tonic-gate 	 */
588*0Sstevel@tonic-gate 	if (!(flags & UFSMNT_LARGEFILES)) {  /* "nolargefiles" */
589*0Sstevel@tonic-gate 		if (fsp->fs_flags & FSLARGEFILES) {
590*0Sstevel@tonic-gate 			error = EFBIG;
591*0Sstevel@tonic-gate 			goto remounterr;
592*0Sstevel@tonic-gate 		}
593*0Sstevel@tonic-gate 		ufsvfsp->vfs_lfflags &= ~UFS_LARGEFILES;
594*0Sstevel@tonic-gate 	} else	/* "largefiles" */
595*0Sstevel@tonic-gate 		ufsvfsp->vfs_lfflags |= UFS_LARGEFILES;
596*0Sstevel@tonic-gate 	/*
597*0Sstevel@tonic-gate 	 * read/write to read/write; all done
598*0Sstevel@tonic-gate 	 */
599*0Sstevel@tonic-gate 	if (fsp->fs_ronly == 0)
600*0Sstevel@tonic-gate 		goto remounterr;
601*0Sstevel@tonic-gate 
602*0Sstevel@tonic-gate 	/*
603*0Sstevel@tonic-gate 	 * fix-on-panic assumes RO->RW remount implies system-critical fs
604*0Sstevel@tonic-gate 	 * if it is shortly after boot; so, don't attempt to lock and fix
605*0Sstevel@tonic-gate 	 * (unless the user explicitly asked for another action on error)
606*0Sstevel@tonic-gate 	 * XXX UFSMNT_ONERROR_RDONLY rather than UFSMNT_ONERROR_PANIC
607*0Sstevel@tonic-gate 	 */
608*0Sstevel@tonic-gate #define	BOOT_TIME_LIMIT	(180*hz)
609*0Sstevel@tonic-gate 	if (!(flags & UFSMNT_ONERROR_FLGMASK) && lbolt < BOOT_TIME_LIMIT) {
610*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s is required to be mounted onerror=%s",
611*0Sstevel@tonic-gate 			ufsvfsp->vfs_fs->fs_fsmnt, UFSMNT_ONERROR_PANIC_STR);
612*0Sstevel@tonic-gate 		flags |= UFSMNT_ONERROR_PANIC;
613*0Sstevel@tonic-gate 	}
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 	if ((error = ufsfx_mount(ufsvfsp, flags)) != 0)
616*0Sstevel@tonic-gate 		goto remounterr;
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 	/*
619*0Sstevel@tonic-gate 	 * Lock the file system and flush stuff from memory
620*0Sstevel@tonic-gate 	 */
621*0Sstevel@tonic-gate 	error = ufs_quiesce(ulp);
622*0Sstevel@tonic-gate 	if (error)
623*0Sstevel@tonic-gate 		goto remounterr;
624*0Sstevel@tonic-gate 
625*0Sstevel@tonic-gate 	/*
626*0Sstevel@tonic-gate 	 * We don't need to call the expensive ufs_flush when going from
627*0Sstevel@tonic-gate 	 * read only to read/write, except if the root fs didn't come
628*0Sstevel@tonic-gate 	 * down cleanly.
629*0Sstevel@tonic-gate 	 */
630*0Sstevel@tonic-gate 	if ((ufsvfsp->vfs_devvp == rootvp) && !ufs_clean_root) {
631*0Sstevel@tonic-gate 		error = ufs_flush(vfsp);
632*0Sstevel@tonic-gate 		if (error) {
633*0Sstevel@tonic-gate 			goto remounterr;
634*0Sstevel@tonic-gate 		}
635*0Sstevel@tonic-gate 	}
636*0Sstevel@tonic-gate 
637*0Sstevel@tonic-gate 	tpt = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, SBLOCK, SBSIZE);
638*0Sstevel@tonic-gate 	if (tpt->b_flags & B_ERROR) {
639*0Sstevel@tonic-gate 		error = EIO;
640*0Sstevel@tonic-gate 		goto remounterr;
641*0Sstevel@tonic-gate 	}
642*0Sstevel@tonic-gate 	fspt = (struct fs *)tpt->b_un.b_addr;
643*0Sstevel@tonic-gate 	if (((fspt->fs_magic != FS_MAGIC) &&
644*0Sstevel@tonic-gate 	    (fspt->fs_magic != MTB_UFS_MAGIC)) ||
645*0Sstevel@tonic-gate 	    (fspt->fs_magic == MTB_UFS_MAGIC &&
646*0Sstevel@tonic-gate 		(fspt->fs_version > MTB_UFS_VERSION_1 ||
647*0Sstevel@tonic-gate 		fspt->fs_version < MTB_UFS_VERSION_MIN)) ||
648*0Sstevel@tonic-gate 	    fspt->fs_bsize > MAXBSIZE || fspt->fs_frag > MAXFRAG ||
649*0Sstevel@tonic-gate 	    fspt->fs_bsize < sizeof (struct fs) || fspt->fs_bsize < PAGESIZE) {
650*0Sstevel@tonic-gate 		tpt->b_flags |= B_STALE | B_AGE;
651*0Sstevel@tonic-gate 		error = EINVAL;
652*0Sstevel@tonic-gate 		goto remounterr;
653*0Sstevel@tonic-gate 	}
654*0Sstevel@tonic-gate 
655*0Sstevel@tonic-gate 	if (ufsvfsp->vfs_log && (ufsvfsp->vfs_log->un_flags & LDL_NOROLL)) {
656*0Sstevel@tonic-gate 		ufsvfsp->vfs_log->un_flags &= ~LDL_NOROLL;
657*0Sstevel@tonic-gate 		logmap_start_roll(ufsvfsp->vfs_log);
658*0Sstevel@tonic-gate 	}
659*0Sstevel@tonic-gate 
660*0Sstevel@tonic-gate 	if (TRANS_ISERROR(ufsvfsp))
661*0Sstevel@tonic-gate 		goto remounterr;
662*0Sstevel@tonic-gate 	TRANS_DOMATAMAP(ufsvfsp);
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate 	if ((fspt->fs_state + fspt->fs_time == FSOKAY) &&
665*0Sstevel@tonic-gate 	    fspt->fs_clean == FSLOG && !TRANS_ISTRANS(ufsvfsp)) {
666*0Sstevel@tonic-gate 		ufsvfsp->vfs_log = NULL;
667*0Sstevel@tonic-gate 		ufsvfsp->vfs_domatamap = 0;
668*0Sstevel@tonic-gate 		error = ENOSPC;
669*0Sstevel@tonic-gate 		goto remounterr;
670*0Sstevel@tonic-gate 	}
671*0Sstevel@tonic-gate 
672*0Sstevel@tonic-gate 	if (fspt->fs_state + fspt->fs_time == FSOKAY &&
673*0Sstevel@tonic-gate 	    (fspt->fs_clean == FSCLEAN ||
674*0Sstevel@tonic-gate 	    fspt->fs_clean == FSSTABLE ||
675*0Sstevel@tonic-gate 	    fspt->fs_clean == FSLOG)) {
676*0Sstevel@tonic-gate 
677*0Sstevel@tonic-gate 		/*
678*0Sstevel@tonic-gate 		 * Ensure that ufs_getsummaryinfo doesn't reconstruct
679*0Sstevel@tonic-gate 		 * the summary info.
680*0Sstevel@tonic-gate 		 */
681*0Sstevel@tonic-gate 		error = ufs_getsummaryinfo(vfsp->vfs_dev, ufsvfsp, fspt);
682*0Sstevel@tonic-gate 		if (error)
683*0Sstevel@tonic-gate 			goto remounterr;
684*0Sstevel@tonic-gate 
685*0Sstevel@tonic-gate 		/* preserve mount name */
686*0Sstevel@tonic-gate 		(void) strncpy(fspt->fs_fsmnt, fsp->fs_fsmnt, MAXMNTLEN);
687*0Sstevel@tonic-gate 		/* free the old cg space */
688*0Sstevel@tonic-gate 		kmem_free(fsp->fs_u.fs_csp, fsp->fs_cssize);
689*0Sstevel@tonic-gate 		/* switch in the new superblock */
690*0Sstevel@tonic-gate 		fspt->fs_rolled = FS_NEED_ROLL;
691*0Sstevel@tonic-gate 		bcopy(tpt->b_un.b_addr, bp->b_un.b_addr, fspt->fs_sbsize);
692*0Sstevel@tonic-gate 
693*0Sstevel@tonic-gate 		fsp->fs_clean = FSSTABLE;
694*0Sstevel@tonic-gate 	} /* superblock updated in memory */
695*0Sstevel@tonic-gate 	tpt->b_flags |= B_STALE | B_AGE;
696*0Sstevel@tonic-gate 	brelse(tpt);
697*0Sstevel@tonic-gate 	tpt = 0;
698*0Sstevel@tonic-gate 
699*0Sstevel@tonic-gate 	if (fsp->fs_clean != FSSTABLE) {
700*0Sstevel@tonic-gate 		error = ENOSPC;
701*0Sstevel@tonic-gate 		goto remounterr;
702*0Sstevel@tonic-gate 	}
703*0Sstevel@tonic-gate 
704*0Sstevel@tonic-gate 
705*0Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp)) {
706*0Sstevel@tonic-gate 		fsp->fs_clean = FSLOG;
707*0Sstevel@tonic-gate 		ufsvfsp->vfs_dio = 0;
708*0Sstevel@tonic-gate 	} else
709*0Sstevel@tonic-gate 		if (ufsvfsp->vfs_dio)
710*0Sstevel@tonic-gate 			fsp->fs_clean = FSSUSPEND;
711*0Sstevel@tonic-gate 
712*0Sstevel@tonic-gate 	TRANS_MATA_MOUNT(ufsvfsp);
713*0Sstevel@tonic-gate 
714*0Sstevel@tonic-gate 	fsp->fs_fmod = 0;
715*0Sstevel@tonic-gate 	fsp->fs_ronly = 0;
716*0Sstevel@tonic-gate 
717*0Sstevel@tonic-gate 	cv_broadcast(&ulp->ul_cv);
718*0Sstevel@tonic-gate 	mutex_exit(&ulp->ul_lock);
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp)) {
721*0Sstevel@tonic-gate 
722*0Sstevel@tonic-gate 		/*
723*0Sstevel@tonic-gate 		 * start the delete thread
724*0Sstevel@tonic-gate 		 */
725*0Sstevel@tonic-gate 		ufs_thread_start(&ufsvfsp->vfs_delete, ufs_thread_delete, vfsp);
726*0Sstevel@tonic-gate 
727*0Sstevel@tonic-gate 		/*
728*0Sstevel@tonic-gate 		 * start the reclaim thread
729*0Sstevel@tonic-gate 		 */
730*0Sstevel@tonic-gate 		if (fsp->fs_reclaim & (FS_RECLAIM|FS_RECLAIMING)) {
731*0Sstevel@tonic-gate 			fsp->fs_reclaim &= ~FS_RECLAIM;
732*0Sstevel@tonic-gate 			fsp->fs_reclaim |=  FS_RECLAIMING;
733*0Sstevel@tonic-gate 			ufs_thread_start(&ufsvfsp->vfs_reclaim,
734*0Sstevel@tonic-gate 				ufs_thread_reclaim, vfsp);
735*0Sstevel@tonic-gate 		}
736*0Sstevel@tonic-gate 	}
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate 	TRANS_SBWRITE(ufsvfsp, TOP_MOUNT);
739*0Sstevel@tonic-gate 
740*0Sstevel@tonic-gate 	return (0);
741*0Sstevel@tonic-gate 
742*0Sstevel@tonic-gate remounterr:
743*0Sstevel@tonic-gate 	if (tpt)
744*0Sstevel@tonic-gate 		brelse(tpt);
745*0Sstevel@tonic-gate 	cv_broadcast(&ulp->ul_cv);
746*0Sstevel@tonic-gate 	mutex_exit(&ulp->ul_lock);
747*0Sstevel@tonic-gate 	return (error);
748*0Sstevel@tonic-gate }
749*0Sstevel@tonic-gate 
750*0Sstevel@tonic-gate /*
751*0Sstevel@tonic-gate  * If the device maxtransfer size is not available, we use ufs_maxmaxphys
752*0Sstevel@tonic-gate  * along with the system value for maxphys to determine the value for
753*0Sstevel@tonic-gate  * maxtransfer.
754*0Sstevel@tonic-gate  */
755*0Sstevel@tonic-gate int ufs_maxmaxphys = (1024 * 1024);
756*0Sstevel@tonic-gate 
757*0Sstevel@tonic-gate #include <sys/ddi.h>		/* for delay(9f) */
758*0Sstevel@tonic-gate 
759*0Sstevel@tonic-gate int ufs_mount_error_delay = 20;	/* default to 20ms */
760*0Sstevel@tonic-gate int ufs_mount_timeout = 60;	/* default to 1 minute */
761*0Sstevel@tonic-gate 
762*0Sstevel@tonic-gate static int
763*0Sstevel@tonic-gate mountfs(struct vfs *vfsp, enum whymountroot why, struct vnode *devvp,
764*0Sstevel@tonic-gate 	char *path, cred_t *cr, int isroot, void *raw_argsp, int args_len)
765*0Sstevel@tonic-gate {
766*0Sstevel@tonic-gate 	dev_t dev = devvp->v_rdev;
767*0Sstevel@tonic-gate 	struct fs *fsp;
768*0Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = 0;
769*0Sstevel@tonic-gate 	struct buf *bp = 0;
770*0Sstevel@tonic-gate 	struct buf *tp = 0;
771*0Sstevel@tonic-gate 	struct dk_cinfo ci;
772*0Sstevel@tonic-gate 	int error = 0;
773*0Sstevel@tonic-gate 	size_t len;
774*0Sstevel@tonic-gate 	int needclose = 0;
775*0Sstevel@tonic-gate 	int needtrans = 0;
776*0Sstevel@tonic-gate 	struct inode *rip;
777*0Sstevel@tonic-gate 	struct vnode *rvp = NULL;
778*0Sstevel@tonic-gate 	int flags = 0;
779*0Sstevel@tonic-gate 	kmutex_t *ihm;
780*0Sstevel@tonic-gate 	int elapsed;
781*0Sstevel@tonic-gate 	int status;
782*0Sstevel@tonic-gate 	extern	int	maxphys;
783*0Sstevel@tonic-gate 
784*0Sstevel@tonic-gate 	if (args_len == sizeof (struct ufs_args) && raw_argsp)
785*0Sstevel@tonic-gate 		flags = ((struct ufs_args *)raw_argsp)->flags;
786*0Sstevel@tonic-gate 
787*0Sstevel@tonic-gate 	ASSERT(vfs_lock_held(vfsp));
788*0Sstevel@tonic-gate 
789*0Sstevel@tonic-gate 	if (why == ROOT_INIT) {
790*0Sstevel@tonic-gate 		/*
791*0Sstevel@tonic-gate 		 * Open block device mounted on.
792*0Sstevel@tonic-gate 		 * When bio is fixed for vnodes this can all be vnode
793*0Sstevel@tonic-gate 		 * operations.
794*0Sstevel@tonic-gate 		 */
795*0Sstevel@tonic-gate 		error = VOP_OPEN(&devvp,
796*0Sstevel@tonic-gate 		    (vfsp->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE, cr);
797*0Sstevel@tonic-gate 		if (error)
798*0Sstevel@tonic-gate 			goto out;
799*0Sstevel@tonic-gate 		needclose = 1;
800*0Sstevel@tonic-gate 
801*0Sstevel@tonic-gate 		/*
802*0Sstevel@tonic-gate 		 * Refuse to go any further if this
803*0Sstevel@tonic-gate 		 * device is being used for swapping.
804*0Sstevel@tonic-gate 		 */
805*0Sstevel@tonic-gate 		if (IS_SWAPVP(devvp)) {
806*0Sstevel@tonic-gate 			error = EBUSY;
807*0Sstevel@tonic-gate 			goto out;
808*0Sstevel@tonic-gate 		}
809*0Sstevel@tonic-gate 	}
810*0Sstevel@tonic-gate 
811*0Sstevel@tonic-gate 	/*
812*0Sstevel@tonic-gate 	 * check for dev already mounted on
813*0Sstevel@tonic-gate 	 */
814*0Sstevel@tonic-gate 	if (vfsp->vfs_flag & VFS_REMOUNT) {
815*0Sstevel@tonic-gate 		error = remountfs(vfsp, dev, raw_argsp, args_len);
816*0Sstevel@tonic-gate 		if (error == 0)
817*0Sstevel@tonic-gate 			VN_RELE(devvp);
818*0Sstevel@tonic-gate 		return (error);
819*0Sstevel@tonic-gate 	}
820*0Sstevel@tonic-gate 
821*0Sstevel@tonic-gate 	ASSERT(devvp != 0);
822*0Sstevel@tonic-gate 
823*0Sstevel@tonic-gate 	/*
824*0Sstevel@tonic-gate 	 * Flush back any dirty pages on the block device to
825*0Sstevel@tonic-gate 	 * try and keep the buffer cache in sync with the page
826*0Sstevel@tonic-gate 	 * cache if someone is trying to use block devices when
827*0Sstevel@tonic-gate 	 * they really should be using the raw device.
828*0Sstevel@tonic-gate 	 */
829*0Sstevel@tonic-gate 	(void) VOP_PUTPAGE(common_specvp(devvp), (offset_t)0,
830*0Sstevel@tonic-gate 	    (size_t)0, B_INVAL, cr);
831*0Sstevel@tonic-gate 
832*0Sstevel@tonic-gate 	/*
833*0Sstevel@tonic-gate 	 * read in superblock
834*0Sstevel@tonic-gate 	 */
835*0Sstevel@tonic-gate 	ufsvfsp = kmem_zalloc(sizeof (struct ufsvfs), KM_SLEEP);
836*0Sstevel@tonic-gate 	tp = UFS_BREAD(ufsvfsp, dev, SBLOCK, SBSIZE);
837*0Sstevel@tonic-gate 	if (tp->b_flags & B_ERROR)
838*0Sstevel@tonic-gate 		goto out;
839*0Sstevel@tonic-gate 	fsp = (struct fs *)tp->b_un.b_addr;
840*0Sstevel@tonic-gate #ifdef _LP64
841*0Sstevel@tonic-gate 	if ((fsp->fs_magic != FS_MAGIC) && (fsp->fs_magic != MTB_UFS_MAGIC)) {
842*0Sstevel@tonic-gate 		if (why == ROOT_INIT) {
843*0Sstevel@tonic-gate 			cmn_err(CE_NOTE,
844*0Sstevel@tonic-gate 			    "mount: not a UFS magic number (0x%x)",
845*0Sstevel@tonic-gate 			    fsp->fs_magic);
846*0Sstevel@tonic-gate 		}
847*0Sstevel@tonic-gate 		error = EINVAL;
848*0Sstevel@tonic-gate 		goto out;
849*0Sstevel@tonic-gate 	}
850*0Sstevel@tonic-gate 
851*0Sstevel@tonic-gate 	if ((fsp->fs_magic == MTB_UFS_MAGIC) &&
852*0Sstevel@tonic-gate 	    (fsp->fs_version > MTB_UFS_VERSION_1 ||
853*0Sstevel@tonic-gate 	    fsp->fs_version < MTB_UFS_VERSION_MIN)) {
854*0Sstevel@tonic-gate 		cmn_err(CE_NOTE,
855*0Sstevel@tonic-gate 		    "mount: unrecognized version of UFS on-disk format: %d",
856*0Sstevel@tonic-gate 		    fsp->fs_version);
857*0Sstevel@tonic-gate 		error = EINVAL;
858*0Sstevel@tonic-gate 		goto out;
859*0Sstevel@tonic-gate 	}
860*0Sstevel@tonic-gate 
861*0Sstevel@tonic-gate #else
862*0Sstevel@tonic-gate 	if (fsp->fs_magic == MTB_UFS_MAGIC) {
863*0Sstevel@tonic-gate 		if (fsp->fs_version > MTB_UFS_VERSION_1 ||
864*0Sstevel@tonic-gate 		    fsp->fs_version < MTB_UFS_VERSION_MIN) {
865*0Sstevel@tonic-gate 			cmn_err(CE_NOTE,
866*0Sstevel@tonic-gate 			    "mount: unrecognized version of UFS"
867*0Sstevel@tonic-gate 			    " on-disk format: %d", fsp->fs_version);
868*0Sstevel@tonic-gate 			error = EINVAL;
869*0Sstevel@tonic-gate 			goto out;
870*0Sstevel@tonic-gate 		}
871*0Sstevel@tonic-gate 
872*0Sstevel@tonic-gate 		/*
873*0Sstevel@tonic-gate 		 * Find the size of the device in sectors.  If the
874*0Sstevel@tonic-gate 		 * the size in sectors is greater than INT_MAX, it's
875*0Sstevel@tonic-gate 		 * a multi-terabyte file system, which can't be
876*0Sstevel@tonic-gate 		 * mounted by a 32-bit kernel.  We can't use the
877*0Sstevel@tonic-gate 		 * fsbtodb() macro in the next line because the macro
878*0Sstevel@tonic-gate 		 * casts the intermediate values to daddr_t, which is
879*0Sstevel@tonic-gate 		 * a 32-bit quantity in a 32-bit kernel.  Here we
880*0Sstevel@tonic-gate 		 * really do need the intermediate values to be held
881*0Sstevel@tonic-gate 		 * in 64-bit quantities because we're checking for
882*0Sstevel@tonic-gate 		 * overflow of a 32-bit field.
883*0Sstevel@tonic-gate 		 */
884*0Sstevel@tonic-gate 		if ((((diskaddr_t)(fsp->fs_size)) << fsp->fs_fsbtodb)
885*0Sstevel@tonic-gate 		    > INT_MAX) {
886*0Sstevel@tonic-gate 			cmn_err(CE_NOTE,
887*0Sstevel@tonic-gate 			    "mount: multi-terabyte UFS cannot be"
888*0Sstevel@tonic-gate 			    " mounted by a 32-bit kernel");
889*0Sstevel@tonic-gate 			error = EINVAL;
890*0Sstevel@tonic-gate 			goto out;
891*0Sstevel@tonic-gate 		}
892*0Sstevel@tonic-gate 
893*0Sstevel@tonic-gate 	} else if (fsp->fs_magic != FS_MAGIC) {
894*0Sstevel@tonic-gate 		cmn_err(CE_NOTE,
895*0Sstevel@tonic-gate 		    "mount: not a UFS magic number (0x%x)", fsp->fs_magic);
896*0Sstevel@tonic-gate 		error = EINVAL;
897*0Sstevel@tonic-gate 		goto out;
898*0Sstevel@tonic-gate 	}
899*0Sstevel@tonic-gate #endif /* _LP64 */
900*0Sstevel@tonic-gate 
901*0Sstevel@tonic-gate 	if (fsp->fs_bsize > MAXBSIZE || fsp->fs_frag > MAXFRAG ||
902*0Sstevel@tonic-gate 	    fsp->fs_bsize < sizeof (struct fs) || fsp->fs_bsize < PAGESIZE) {
903*0Sstevel@tonic-gate 		error = EINVAL;	/* also needs translation */
904*0Sstevel@tonic-gate 		goto out;
905*0Sstevel@tonic-gate 	}
906*0Sstevel@tonic-gate 
907*0Sstevel@tonic-gate 	/*
908*0Sstevel@tonic-gate 	 * Allocate VFS private data.
909*0Sstevel@tonic-gate 	 */
910*0Sstevel@tonic-gate 	vfsp->vfs_bcount = 0;
911*0Sstevel@tonic-gate 	vfsp->vfs_data = (caddr_t)ufsvfsp;
912*0Sstevel@tonic-gate 	vfsp->vfs_fstype = ufsfstype;
913*0Sstevel@tonic-gate 	vfsp->vfs_dev = dev;
914*0Sstevel@tonic-gate 	vfsp->vfs_flag |= VFS_NOTRUNC;
915*0Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, dev, ufsfstype);
916*0Sstevel@tonic-gate 	ufsvfsp->vfs_devvp = devvp;
917*0Sstevel@tonic-gate 
918*0Sstevel@tonic-gate 	/*
919*0Sstevel@tonic-gate 	 * Cross-link with vfs and add to instance list.
920*0Sstevel@tonic-gate 	 */
921*0Sstevel@tonic-gate 	ufsvfsp->vfs_vfs = vfsp;
922*0Sstevel@tonic-gate 	ufs_vfs_add(ufsvfsp);
923*0Sstevel@tonic-gate 
924*0Sstevel@tonic-gate 	ufsvfsp->vfs_dev = dev;
925*0Sstevel@tonic-gate 	ufsvfsp->vfs_bufp = tp;
926*0Sstevel@tonic-gate 
927*0Sstevel@tonic-gate 	ufsvfsp->vfs_dirsize = INODESIZE + (4 * ALLOCSIZE) + fsp->fs_fsize;
928*0Sstevel@tonic-gate 	ufsvfsp->vfs_minfrags = (int)((int64_t)fsp->fs_dsize *
929*0Sstevel@tonic-gate 							fsp->fs_minfree / 100);
930*0Sstevel@tonic-gate 	/*
931*0Sstevel@tonic-gate 	 * if mount allows largefiles, indicate so in ufsvfs
932*0Sstevel@tonic-gate 	 */
933*0Sstevel@tonic-gate 	if (flags & UFSMNT_LARGEFILES)
934*0Sstevel@tonic-gate 		ufsvfsp->vfs_lfflags |= UFS_LARGEFILES;
935*0Sstevel@tonic-gate 	/*
936*0Sstevel@tonic-gate 	 * Initialize threads
937*0Sstevel@tonic-gate 	 */
938*0Sstevel@tonic-gate 	ufs_thread_init(&ufsvfsp->vfs_delete, 1);
939*0Sstevel@tonic-gate 	ufs_thread_init(&ufsvfsp->vfs_reclaim, 0);
940*0Sstevel@tonic-gate 
941*0Sstevel@tonic-gate 	/*
942*0Sstevel@tonic-gate 	 * Chicken and egg problem. The superblock may have deltas
943*0Sstevel@tonic-gate 	 * in the log.  So after the log is scanned we reread the
944*0Sstevel@tonic-gate 	 * superblock. We guarantee that the fields needed to
945*0Sstevel@tonic-gate 	 * scan the log will not be in the log.
946*0Sstevel@tonic-gate 	 */
947*0Sstevel@tonic-gate 	if (fsp->fs_logbno && fsp->fs_clean == FSLOG &&
948*0Sstevel@tonic-gate 	    (fsp->fs_state + fsp->fs_time == FSOKAY)) {
949*0Sstevel@tonic-gate 		error = lufs_snarf(ufsvfsp, fsp, (vfsp->vfs_flag & VFS_RDONLY));
950*0Sstevel@tonic-gate 		if (error) {
951*0Sstevel@tonic-gate 			/*
952*0Sstevel@tonic-gate 			 * Allow a ro mount to continue even if the
953*0Sstevel@tonic-gate 			 * log cannot be processed - yet.
954*0Sstevel@tonic-gate 			 */
955*0Sstevel@tonic-gate 			if (!(vfsp->vfs_flag & VFS_RDONLY)) {
956*0Sstevel@tonic-gate 				cmn_err(CE_WARN, "Error accessing ufs "
957*0Sstevel@tonic-gate 					"log for %s; Please run fsck(1M)",
958*0Sstevel@tonic-gate 					path);
959*0Sstevel@tonic-gate 				goto out;
960*0Sstevel@tonic-gate 			}
961*0Sstevel@tonic-gate 		}
962*0Sstevel@tonic-gate 		tp->b_flags |= (B_AGE | B_STALE);
963*0Sstevel@tonic-gate 		brelse(tp);
964*0Sstevel@tonic-gate 		tp = UFS_BREAD(ufsvfsp, dev, SBLOCK, SBSIZE);
965*0Sstevel@tonic-gate 		fsp = (struct fs *)tp->b_un.b_addr;
966*0Sstevel@tonic-gate 		ufsvfsp->vfs_bufp = tp;
967*0Sstevel@tonic-gate 		if (tp->b_flags & B_ERROR)
968*0Sstevel@tonic-gate 			goto out;
969*0Sstevel@tonic-gate 	}
970*0Sstevel@tonic-gate 
971*0Sstevel@tonic-gate 	/*
972*0Sstevel@tonic-gate 	 * Set logging mounted flag used by lockfs
973*0Sstevel@tonic-gate 	 */
974*0Sstevel@tonic-gate 	ufsvfsp->vfs_validfs = UT_MOUNTED;
975*0Sstevel@tonic-gate 
976*0Sstevel@tonic-gate 	/*
977*0Sstevel@tonic-gate 	 * Copy the super block into a buffer in its native size.
978*0Sstevel@tonic-gate 	 * Use ngeteblk to allocate the buffer
979*0Sstevel@tonic-gate 	 */
980*0Sstevel@tonic-gate 	bp = ngeteblk(fsp->fs_bsize);
981*0Sstevel@tonic-gate 	ufsvfsp->vfs_bufp = bp;
982*0Sstevel@tonic-gate 	bp->b_edev = dev;
983*0Sstevel@tonic-gate 	bp->b_dev = cmpdev(dev);
984*0Sstevel@tonic-gate 	bp->b_blkno = SBLOCK;
985*0Sstevel@tonic-gate 	bp->b_bcount = fsp->fs_sbsize;
986*0Sstevel@tonic-gate 	bcopy(tp->b_un.b_addr, bp->b_un.b_addr, fsp->fs_sbsize);
987*0Sstevel@tonic-gate 	tp->b_flags |= B_STALE | B_AGE;
988*0Sstevel@tonic-gate 	brelse(tp);
989*0Sstevel@tonic-gate 	tp = 0;
990*0Sstevel@tonic-gate 
991*0Sstevel@tonic-gate 	fsp = (struct fs *)bp->b_un.b_addr;
992*0Sstevel@tonic-gate 	/*
993*0Sstevel@tonic-gate 	 * Mount fails if superblock flag indicates presence of large
994*0Sstevel@tonic-gate 	 * files and filesystem is attempted to be mounted 'nolargefiles'.
995*0Sstevel@tonic-gate 	 * The exception is for a read only mount of root, which we
996*0Sstevel@tonic-gate 	 * always want to succeed, so fsck can fix potential problems.
997*0Sstevel@tonic-gate 	 * The assumption is that we will remount root at some point,
998*0Sstevel@tonic-gate 	 * and the remount will enforce the mount option.
999*0Sstevel@tonic-gate 	 */
1000*0Sstevel@tonic-gate 	if (!(isroot & (vfsp->vfs_flag & VFS_RDONLY)) &&
1001*0Sstevel@tonic-gate 	    (fsp->fs_flags & FSLARGEFILES) &&
1002*0Sstevel@tonic-gate 	    !(flags & UFSMNT_LARGEFILES)) {
1003*0Sstevel@tonic-gate 		error = EFBIG;
1004*0Sstevel@tonic-gate 		goto out;
1005*0Sstevel@tonic-gate 	}
1006*0Sstevel@tonic-gate 
1007*0Sstevel@tonic-gate 	if (vfsp->vfs_flag & VFS_RDONLY) {
1008*0Sstevel@tonic-gate 		fsp->fs_ronly = 1;
1009*0Sstevel@tonic-gate 		fsp->fs_fmod = 0;
1010*0Sstevel@tonic-gate 		if (((fsp->fs_state + fsp->fs_time) == FSOKAY) &&
1011*0Sstevel@tonic-gate 		    ((fsp->fs_clean == FSCLEAN) ||
1012*0Sstevel@tonic-gate 		    (fsp->fs_clean == FSSTABLE) ||
1013*0Sstevel@tonic-gate 		    (fsp->fs_clean == FSLOG))) {
1014*0Sstevel@tonic-gate 			if (isroot) {
1015*0Sstevel@tonic-gate 				if (fsp->fs_clean == FSLOG) {
1016*0Sstevel@tonic-gate 					if (fsp->fs_rolled == FS_ALL_ROLLED) {
1017*0Sstevel@tonic-gate 						ufs_clean_root = 1;
1018*0Sstevel@tonic-gate 					}
1019*0Sstevel@tonic-gate 				} else {
1020*0Sstevel@tonic-gate 					ufs_clean_root = 1;
1021*0Sstevel@tonic-gate 				}
1022*0Sstevel@tonic-gate 			}
1023*0Sstevel@tonic-gate 			fsp->fs_clean = FSSTABLE;
1024*0Sstevel@tonic-gate 		} else {
1025*0Sstevel@tonic-gate 			fsp->fs_clean = FSBAD;
1026*0Sstevel@tonic-gate 		}
1027*0Sstevel@tonic-gate 	} else {
1028*0Sstevel@tonic-gate 
1029*0Sstevel@tonic-gate 		fsp->fs_fmod = 0;
1030*0Sstevel@tonic-gate 		fsp->fs_ronly = 0;
1031*0Sstevel@tonic-gate 
1032*0Sstevel@tonic-gate 		TRANS_DOMATAMAP(ufsvfsp);
1033*0Sstevel@tonic-gate 
1034*0Sstevel@tonic-gate 		if ((TRANS_ISERROR(ufsvfsp)) ||
1035*0Sstevel@tonic-gate 		    (((fsp->fs_state + fsp->fs_time) == FSOKAY) &&
1036*0Sstevel@tonic-gate 			fsp->fs_clean == FSLOG && !TRANS_ISTRANS(ufsvfsp))) {
1037*0Sstevel@tonic-gate 			ufsvfsp->vfs_log = NULL;
1038*0Sstevel@tonic-gate 			ufsvfsp->vfs_domatamap = 0;
1039*0Sstevel@tonic-gate 			error = ENOSPC;
1040*0Sstevel@tonic-gate 			goto out;
1041*0Sstevel@tonic-gate 		}
1042*0Sstevel@tonic-gate 
1043*0Sstevel@tonic-gate 		if (((fsp->fs_state + fsp->fs_time) == FSOKAY) &&
1044*0Sstevel@tonic-gate 		    (fsp->fs_clean == FSCLEAN ||
1045*0Sstevel@tonic-gate 			fsp->fs_clean == FSSTABLE ||
1046*0Sstevel@tonic-gate 			fsp->fs_clean == FSLOG))
1047*0Sstevel@tonic-gate 			fsp->fs_clean = FSSTABLE;
1048*0Sstevel@tonic-gate 		else {
1049*0Sstevel@tonic-gate 			if (isroot) {
1050*0Sstevel@tonic-gate 				/*
1051*0Sstevel@tonic-gate 				 * allow root partition to be mounted even
1052*0Sstevel@tonic-gate 				 * when fs_state is not ok
1053*0Sstevel@tonic-gate 				 * will be fixed later by a remount root
1054*0Sstevel@tonic-gate 				 */
1055*0Sstevel@tonic-gate 				fsp->fs_clean = FSBAD;
1056*0Sstevel@tonic-gate 				ufsvfsp->vfs_log = NULL;
1057*0Sstevel@tonic-gate 				ufsvfsp->vfs_domatamap = 0;
1058*0Sstevel@tonic-gate 			} else {
1059*0Sstevel@tonic-gate 				error = ENOSPC;
1060*0Sstevel@tonic-gate 				goto out;
1061*0Sstevel@tonic-gate 			}
1062*0Sstevel@tonic-gate 		}
1063*0Sstevel@tonic-gate 
1064*0Sstevel@tonic-gate 		if (fsp->fs_clean == FSSTABLE && TRANS_ISTRANS(ufsvfsp))
1065*0Sstevel@tonic-gate 			fsp->fs_clean = FSLOG;
1066*0Sstevel@tonic-gate 	}
1067*0Sstevel@tonic-gate 	TRANS_MATA_MOUNT(ufsvfsp);
1068*0Sstevel@tonic-gate 	needtrans = 1;
1069*0Sstevel@tonic-gate 
1070*0Sstevel@tonic-gate 	vfsp->vfs_bsize = fsp->fs_bsize;
1071*0Sstevel@tonic-gate 
1072*0Sstevel@tonic-gate 	/*
1073*0Sstevel@tonic-gate 	 * Read in summary info
1074*0Sstevel@tonic-gate 	 */
1075*0Sstevel@tonic-gate 	if (error = ufs_getsummaryinfo(dev, ufsvfsp, fsp))
1076*0Sstevel@tonic-gate 		goto out;
1077*0Sstevel@tonic-gate 
1078*0Sstevel@tonic-gate 	/*
1079*0Sstevel@tonic-gate 	 * lastwhinetime is set to zero rather than lbolt, so that after
1080*0Sstevel@tonic-gate 	 * mounting if the filesystem is found to be full, then immediately the
1081*0Sstevel@tonic-gate 	 * "file system message" will be logged.
1082*0Sstevel@tonic-gate 	 */
1083*0Sstevel@tonic-gate 	ufsvfsp->vfs_lastwhinetime = 0L;
1084*0Sstevel@tonic-gate 
1085*0Sstevel@tonic-gate 
1086*0Sstevel@tonic-gate 	mutex_init(&ufsvfsp->vfs_lock, NULL, MUTEX_DEFAULT, NULL);
1087*0Sstevel@tonic-gate 	(void) copystr(path, fsp->fs_fsmnt, sizeof (fsp->fs_fsmnt) - 1, &len);
1088*0Sstevel@tonic-gate 	bzero(fsp->fs_fsmnt + len, sizeof (fsp->fs_fsmnt) - len);
1089*0Sstevel@tonic-gate 
1090*0Sstevel@tonic-gate 	/*
1091*0Sstevel@tonic-gate 	 * Sanity checks for old file systems
1092*0Sstevel@tonic-gate 	 */
1093*0Sstevel@tonic-gate 	if (fsp->fs_postblformat == FS_42POSTBLFMT)
1094*0Sstevel@tonic-gate 		ufsvfsp->vfs_nrpos = 8;
1095*0Sstevel@tonic-gate 	else
1096*0Sstevel@tonic-gate 		ufsvfsp->vfs_nrpos = fsp->fs_nrpos;
1097*0Sstevel@tonic-gate 
1098*0Sstevel@tonic-gate 	/*
1099*0Sstevel@tonic-gate 	 * Initialize lockfs structure to support file system locking
1100*0Sstevel@tonic-gate 	 */
1101*0Sstevel@tonic-gate 	bzero(&ufsvfsp->vfs_ulockfs.ul_lockfs,
1102*0Sstevel@tonic-gate 	    sizeof (struct lockfs));
1103*0Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_fs_lock = ULOCKFS_ULOCK;
1104*0Sstevel@tonic-gate 	mutex_init(&ufsvfsp->vfs_ulockfs.ul_lock, NULL,
1105*0Sstevel@tonic-gate 	    MUTEX_DEFAULT, NULL);
1106*0Sstevel@tonic-gate 	cv_init(&ufsvfsp->vfs_ulockfs.ul_cv, NULL, CV_DEFAULT, NULL);
1107*0Sstevel@tonic-gate 
1108*0Sstevel@tonic-gate 	/*
1109*0Sstevel@tonic-gate 	 * We don't need to grab vfs_dqrwlock for this ufs_iget() call.
1110*0Sstevel@tonic-gate 	 * We are in the process of mounting the file system so there
1111*0Sstevel@tonic-gate 	 * is no need to grab the quota lock. If a quota applies to the
1112*0Sstevel@tonic-gate 	 * root inode, then it will be updated when quotas are enabled.
1113*0Sstevel@tonic-gate 	 *
1114*0Sstevel@tonic-gate 	 * However, we have an ASSERT(RW_LOCK_HELD(&ufsvfsp->vfs_dqrwlock))
1115*0Sstevel@tonic-gate 	 * in getinoquota() that we want to keep so grab it anyway.
1116*0Sstevel@tonic-gate 	 */
1117*0Sstevel@tonic-gate 	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
1118*0Sstevel@tonic-gate 
1119*0Sstevel@tonic-gate 	error = ufs_iget_alloced(vfsp, UFSROOTINO, &rip, cr);
1120*0Sstevel@tonic-gate 
1121*0Sstevel@tonic-gate 	rw_exit(&ufsvfsp->vfs_dqrwlock);
1122*0Sstevel@tonic-gate 
1123*0Sstevel@tonic-gate 	if (error)
1124*0Sstevel@tonic-gate 		goto out;
1125*0Sstevel@tonic-gate 
1126*0Sstevel@tonic-gate 	/*
1127*0Sstevel@tonic-gate 	 * make sure root inode is a directory.  Returning ENOTDIR might
1128*0Sstevel@tonic-gate 	 * be confused with the mount point not being a directory, so
1129*0Sstevel@tonic-gate 	 * we use EIO instead.
1130*0Sstevel@tonic-gate 	 */
1131*0Sstevel@tonic-gate 	if ((rip->i_mode & IFMT) != IFDIR) {
1132*0Sstevel@tonic-gate 		/*
1133*0Sstevel@tonic-gate 		 * Mark this inode as subject for cleanup
1134*0Sstevel@tonic-gate 		 * to avoid stray inodes in the cache.
1135*0Sstevel@tonic-gate 		 */
1136*0Sstevel@tonic-gate 		rvp = ITOV(rip);
1137*0Sstevel@tonic-gate 		error = EIO;
1138*0Sstevel@tonic-gate 		goto out;
1139*0Sstevel@tonic-gate 	}
1140*0Sstevel@tonic-gate 
1141*0Sstevel@tonic-gate 	rvp = ITOV(rip);
1142*0Sstevel@tonic-gate 	mutex_enter(&rvp->v_lock);
1143*0Sstevel@tonic-gate 	rvp->v_flag |= VROOT;
1144*0Sstevel@tonic-gate 	mutex_exit(&rvp->v_lock);
1145*0Sstevel@tonic-gate 	ufsvfsp->vfs_root = rvp;
1146*0Sstevel@tonic-gate 	/* The buffer for the root inode does not contain a valid b_vp */
1147*0Sstevel@tonic-gate 	(void) bfinval(dev, 0);
1148*0Sstevel@tonic-gate 
1149*0Sstevel@tonic-gate 	/* options */
1150*0Sstevel@tonic-gate 	ufsvfsp->vfs_nosetsec = flags & UFSMNT_NOSETSEC;
1151*0Sstevel@tonic-gate 	ufsvfsp->vfs_nointr  = flags & UFSMNT_NOINTR;
1152*0Sstevel@tonic-gate 	ufsvfsp->vfs_syncdir = flags & UFSMNT_SYNCDIR;
1153*0Sstevel@tonic-gate 	ufsvfsp->vfs_noatime = flags & UFSMNT_NOATIME;
1154*0Sstevel@tonic-gate 	if ((flags & UFSMNT_NODFRATIME) || ufsvfsp->vfs_noatime)
1155*0Sstevel@tonic-gate 		ufsvfsp->vfs_dfritime &= ~UFS_DFRATIME;
1156*0Sstevel@tonic-gate 	else	/* dfratime, default behavior */
1157*0Sstevel@tonic-gate 		ufsvfsp->vfs_dfritime |= UFS_DFRATIME;
1158*0Sstevel@tonic-gate 	if (flags & UFSMNT_FORCEDIRECTIO)
1159*0Sstevel@tonic-gate 		ufsvfsp->vfs_forcedirectio = 1;
1160*0Sstevel@tonic-gate 	else if (flags & UFSMNT_NOFORCEDIRECTIO)
1161*0Sstevel@tonic-gate 		ufsvfsp->vfs_forcedirectio = 0;
1162*0Sstevel@tonic-gate 	ufsvfsp->vfs_iotstamp = lbolt;
1163*0Sstevel@tonic-gate 
1164*0Sstevel@tonic-gate 	ufsvfsp->vfs_nindiroffset = fsp->fs_nindir - 1;
1165*0Sstevel@tonic-gate 	ufsvfsp->vfs_nindirshift = highbit(ufsvfsp->vfs_nindiroffset);
1166*0Sstevel@tonic-gate 	ufsvfsp->vfs_ioclustsz = fsp->fs_bsize * fsp->fs_maxcontig;
1167*0Sstevel@tonic-gate 
1168*0Sstevel@tonic-gate 	if (cdev_ioctl(dev, DKIOCINFO, (intptr_t)&ci,
1169*0Sstevel@tonic-gate 	    FKIOCTL|FNATIVE|FREAD, CRED(), &status) == 0) {
1170*0Sstevel@tonic-gate 		ufsvfsp->vfs_iotransz = ci.dki_maxtransfer * DEV_BSIZE;
1171*0Sstevel@tonic-gate 	} else {
1172*0Sstevel@tonic-gate 		ufsvfsp->vfs_iotransz = MIN(maxphys, ufs_maxmaxphys);
1173*0Sstevel@tonic-gate 	}
1174*0Sstevel@tonic-gate 
1175*0Sstevel@tonic-gate 	if (ufsvfsp->vfs_iotransz <= 0) {
1176*0Sstevel@tonic-gate 		ufsvfsp->vfs_iotransz = MIN(maxphys, ufs_maxmaxphys);
1177*0Sstevel@tonic-gate 	}
1178*0Sstevel@tonic-gate 
1179*0Sstevel@tonic-gate 	/*
1180*0Sstevel@tonic-gate 	 * When logging, used to reserve log space for writes and truncs
1181*0Sstevel@tonic-gate 	 */
1182*0Sstevel@tonic-gate 	ufsvfsp->vfs_avgbfree = fsp->fs_cstotal.cs_nbfree / fsp->fs_ncg;
1183*0Sstevel@tonic-gate 
1184*0Sstevel@tonic-gate 	/*
1185*0Sstevel@tonic-gate 	 * Determine whether to log cylinder group summary info.
1186*0Sstevel@tonic-gate 	 */
1187*0Sstevel@tonic-gate 	ufsvfsp->vfs_nolog_si = (fsp->fs_ncg < ufs_ncg_log);
1188*0Sstevel@tonic-gate 
1189*0Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp)) {
1190*0Sstevel@tonic-gate 		/*
1191*0Sstevel@tonic-gate 		 * start the delete thread
1192*0Sstevel@tonic-gate 		 */
1193*0Sstevel@tonic-gate 		ufs_thread_start(&ufsvfsp->vfs_delete, ufs_thread_delete, vfsp);
1194*0Sstevel@tonic-gate 
1195*0Sstevel@tonic-gate 		/*
1196*0Sstevel@tonic-gate 		 * start reclaim thread if the filesystem was not mounted
1197*0Sstevel@tonic-gate 		 * read only.
1198*0Sstevel@tonic-gate 		 */
1199*0Sstevel@tonic-gate 		if (!fsp->fs_ronly && (fsp->fs_reclaim &
1200*0Sstevel@tonic-gate 			(FS_RECLAIM|FS_RECLAIMING))) {
1201*0Sstevel@tonic-gate 			fsp->fs_reclaim &= ~FS_RECLAIM;
1202*0Sstevel@tonic-gate 			fsp->fs_reclaim |=  FS_RECLAIMING;
1203*0Sstevel@tonic-gate 			ufs_thread_start(&ufsvfsp->vfs_reclaim,
1204*0Sstevel@tonic-gate 			    ufs_thread_reclaim, vfsp);
1205*0Sstevel@tonic-gate 		}
1206*0Sstevel@tonic-gate 
1207*0Sstevel@tonic-gate 		/* Mark the fs as unrolled */
1208*0Sstevel@tonic-gate 		fsp->fs_rolled = FS_NEED_ROLL;
1209*0Sstevel@tonic-gate 	} else if (!fsp->fs_ronly && (fsp->fs_reclaim &
1210*0Sstevel@tonic-gate 	    (FS_RECLAIM|FS_RECLAIMING))) {
1211*0Sstevel@tonic-gate 		/*
1212*0Sstevel@tonic-gate 		 * If a file system that is mounted nologging, after
1213*0Sstevel@tonic-gate 		 * having previously been mounted logging, becomes
1214*0Sstevel@tonic-gate 		 * unmounted whilst the reclaim thread is in the throes
1215*0Sstevel@tonic-gate 		 * of reclaiming open/deleted inodes, a subsequent mount
1216*0Sstevel@tonic-gate 		 * of such a file system with logging disabled could lead
1217*0Sstevel@tonic-gate 		 * to inodes becoming lost.  So, start reclaim now, even
1218*0Sstevel@tonic-gate 		 * though logging was disabled for the previous mount, to
1219*0Sstevel@tonic-gate 		 * tidy things up.
1220*0Sstevel@tonic-gate 		 */
1221*0Sstevel@tonic-gate 		fsp->fs_reclaim &= ~FS_RECLAIM;
1222*0Sstevel@tonic-gate 		fsp->fs_reclaim |=  FS_RECLAIMING;
1223*0Sstevel@tonic-gate 		ufs_thread_start(&ufsvfsp->vfs_reclaim,
1224*0Sstevel@tonic-gate 		    ufs_thread_reclaim, vfsp);
1225*0Sstevel@tonic-gate 	}
1226*0Sstevel@tonic-gate 
1227*0Sstevel@tonic-gate 	if (!fsp->fs_ronly) {
1228*0Sstevel@tonic-gate 		TRANS_SBWRITE(ufsvfsp, TOP_MOUNT);
1229*0Sstevel@tonic-gate 		if (error = geterror(ufsvfsp->vfs_bufp))
1230*0Sstevel@tonic-gate 			goto out;
1231*0Sstevel@tonic-gate 	}
1232*0Sstevel@tonic-gate 
1233*0Sstevel@tonic-gate 	/* fix-on-panic initialization */
1234*0Sstevel@tonic-gate 	if (isroot && !(flags & UFSMNT_ONERROR_FLGMASK))
1235*0Sstevel@tonic-gate 		flags |= UFSMNT_ONERROR_PANIC;	/* XXX ..._RDONLY */
1236*0Sstevel@tonic-gate 
1237*0Sstevel@tonic-gate 	if ((error = ufsfx_mount(ufsvfsp, flags)) != 0)
1238*0Sstevel@tonic-gate 		goto out;
1239*0Sstevel@tonic-gate 
1240*0Sstevel@tonic-gate 	if (why == ROOT_INIT && isroot)
1241*0Sstevel@tonic-gate 		rootvp = devvp;
1242*0Sstevel@tonic-gate 
1243*0Sstevel@tonic-gate 	return (0);
1244*0Sstevel@tonic-gate out:
1245*0Sstevel@tonic-gate 	if (error == 0)
1246*0Sstevel@tonic-gate 		error = EIO;
1247*0Sstevel@tonic-gate 	if (rvp) {
1248*0Sstevel@tonic-gate 		/* the following sequence is similar to ufs_unmount() */
1249*0Sstevel@tonic-gate 
1250*0Sstevel@tonic-gate 		/*
1251*0Sstevel@tonic-gate 		 * There's a problem that ufs_iget() puts inodes into
1252*0Sstevel@tonic-gate 		 * the inode cache before it returns them.  If someone
1253*0Sstevel@tonic-gate 		 * traverses that cache and gets a reference to our
1254*0Sstevel@tonic-gate 		 * inode, there's a chance they'll still be using it
1255*0Sstevel@tonic-gate 		 * after we've destroyed it.  This is a hard race to
1256*0Sstevel@tonic-gate 		 * hit, but it's happened (putting in a medium delay
1257*0Sstevel@tonic-gate 		 * here, and a large delay in ufs_scan_inodes() for
1258*0Sstevel@tonic-gate 		 * inodes on the device we're bailing out on, makes
1259*0Sstevel@tonic-gate 		 * the race easy to demonstrate).  The symptom is some
1260*0Sstevel@tonic-gate 		 * other part of UFS faulting on bad inode contents,
1261*0Sstevel@tonic-gate 		 * or when grabbing one of the locks inside the inode,
1262*0Sstevel@tonic-gate 		 * etc.  The usual victim is ufs_scan_inodes() or
1263*0Sstevel@tonic-gate 		 * someone called by it.
1264*0Sstevel@tonic-gate 		 */
1265*0Sstevel@tonic-gate 
1266*0Sstevel@tonic-gate 		/*
1267*0Sstevel@tonic-gate 		 * First, isolate it so that no new references can be
1268*0Sstevel@tonic-gate 		 * gotten via the inode cache.
1269*0Sstevel@tonic-gate 		 */
1270*0Sstevel@tonic-gate 		ihm = &ih_lock[INOHASH(UFSROOTINO)];
1271*0Sstevel@tonic-gate 		mutex_enter(ihm);
1272*0Sstevel@tonic-gate 		remque(rip);
1273*0Sstevel@tonic-gate 		mutex_exit(ihm);
1274*0Sstevel@tonic-gate 
1275*0Sstevel@tonic-gate 		/*
1276*0Sstevel@tonic-gate 		 * Now wait for all outstanding references except our
1277*0Sstevel@tonic-gate 		 * own to drain.  This could, in theory, take forever,
1278*0Sstevel@tonic-gate 		 * so don't wait *too* long.  If we time out, mark
1279*0Sstevel@tonic-gate 		 * it stale and leak it, so we don't hit the problem
1280*0Sstevel@tonic-gate 		 * described above.
1281*0Sstevel@tonic-gate 		 *
1282*0Sstevel@tonic-gate 		 * Note that v_count is an int, which means we can read
1283*0Sstevel@tonic-gate 		 * it in one operation.  Thus, there's no need to lock
1284*0Sstevel@tonic-gate 		 * around our tests.
1285*0Sstevel@tonic-gate 		 */
1286*0Sstevel@tonic-gate 		elapsed = 0;
1287*0Sstevel@tonic-gate 		while ((rvp->v_count > 1) && (elapsed < ufs_mount_timeout)) {
1288*0Sstevel@tonic-gate 			delay(ufs_mount_error_delay * drv_usectohz(1000));
1289*0Sstevel@tonic-gate 			elapsed += ufs_mount_error_delay;
1290*0Sstevel@tonic-gate 		}
1291*0Sstevel@tonic-gate 
1292*0Sstevel@tonic-gate 		if (rvp->v_count > 1) {
1293*0Sstevel@tonic-gate 			mutex_enter(&rip->i_tlock);
1294*0Sstevel@tonic-gate 			rip->i_flag |= ISTALE;
1295*0Sstevel@tonic-gate 			mutex_exit(&rip->i_tlock);
1296*0Sstevel@tonic-gate 			cmn_err(CE_WARN,
1297*0Sstevel@tonic-gate 		"Timed out while cleaning up after failed mount of %s",
1298*0Sstevel@tonic-gate 				path);
1299*0Sstevel@tonic-gate 		} else {
1300*0Sstevel@tonic-gate 
1301*0Sstevel@tonic-gate 			/*
1302*0Sstevel@tonic-gate 			 * Now we're the only one with a handle left, so tear
1303*0Sstevel@tonic-gate 			 * it down the rest of the way.
1304*0Sstevel@tonic-gate 			 */
1305*0Sstevel@tonic-gate 			if (ufs_rmidle(rip))
1306*0Sstevel@tonic-gate 				VN_RELE(rvp);
1307*0Sstevel@tonic-gate 			ufs_si_del(rip);
1308*0Sstevel@tonic-gate 			rip->i_ufsvfs = NULL;
1309*0Sstevel@tonic-gate 			rvp->v_vfsp = NULL;
1310*0Sstevel@tonic-gate 			rvp->v_type = VBAD;
1311*0Sstevel@tonic-gate 			VN_RELE(rvp);
1312*0Sstevel@tonic-gate 		}
1313*0Sstevel@tonic-gate 	}
1314*0Sstevel@tonic-gate 	if (needtrans) {
1315*0Sstevel@tonic-gate 		TRANS_MATA_UMOUNT(ufsvfsp);
1316*0Sstevel@tonic-gate 	}
1317*0Sstevel@tonic-gate 	if (ufsvfsp) {
1318*0Sstevel@tonic-gate 		ufs_vfs_remove(ufsvfsp);
1319*0Sstevel@tonic-gate 		ufs_thread_exit(&ufsvfsp->vfs_delete);
1320*0Sstevel@tonic-gate 		ufs_thread_exit(&ufsvfsp->vfs_reclaim);
1321*0Sstevel@tonic-gate 		mutex_destroy(&ufsvfsp->vfs_lock);
1322*0Sstevel@tonic-gate 		if (ufsvfsp->vfs_log) {
1323*0Sstevel@tonic-gate 			lufs_unsnarf(ufsvfsp);
1324*0Sstevel@tonic-gate 		}
1325*0Sstevel@tonic-gate 		kmem_free(ufsvfsp, sizeof (struct ufsvfs));
1326*0Sstevel@tonic-gate 	}
1327*0Sstevel@tonic-gate 	if (bp) {
1328*0Sstevel@tonic-gate 		bp->b_flags |= (B_STALE|B_AGE);
1329*0Sstevel@tonic-gate 		brelse(bp);
1330*0Sstevel@tonic-gate 	}
1331*0Sstevel@tonic-gate 	if (tp) {
1332*0Sstevel@tonic-gate 		tp->b_flags |= (B_STALE|B_AGE);
1333*0Sstevel@tonic-gate 		brelse(tp);
1334*0Sstevel@tonic-gate 	}
1335*0Sstevel@tonic-gate 	if (needclose) {
1336*0Sstevel@tonic-gate 		(void) VOP_CLOSE(devvp, (vfsp->vfs_flag & VFS_RDONLY) ?
1337*0Sstevel@tonic-gate 		    FREAD : FREAD|FWRITE, 1, (offset_t)0, cr);
1338*0Sstevel@tonic-gate 		bflush(dev);
1339*0Sstevel@tonic-gate 		(void) bfinval(dev, 1);
1340*0Sstevel@tonic-gate 	}
1341*0Sstevel@tonic-gate 	return (error);
1342*0Sstevel@tonic-gate }
1343*0Sstevel@tonic-gate 
1344*0Sstevel@tonic-gate /*
1345*0Sstevel@tonic-gate  * vfs operations
1346*0Sstevel@tonic-gate  */
1347*0Sstevel@tonic-gate static int
1348*0Sstevel@tonic-gate ufs_unmount(struct vfs *vfsp, int fflag, struct cred *cr)
1349*0Sstevel@tonic-gate {
1350*0Sstevel@tonic-gate 	dev_t 		dev		= vfsp->vfs_dev;
1351*0Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp	= (struct ufsvfs *)vfsp->vfs_data;
1352*0Sstevel@tonic-gate 	struct fs	*fs		= ufsvfsp->vfs_fs;
1353*0Sstevel@tonic-gate 	struct ulockfs	*ulp		= &ufsvfsp->vfs_ulockfs;
1354*0Sstevel@tonic-gate 	struct vnode 	*bvp, *vp;
1355*0Sstevel@tonic-gate 	struct buf	*bp;
1356*0Sstevel@tonic-gate 	struct inode	*ip, *inext, *rip;
1357*0Sstevel@tonic-gate 	union ihead	*ih;
1358*0Sstevel@tonic-gate 	int 		error, flag, i;
1359*0Sstevel@tonic-gate 	struct lockfs	lockfs;
1360*0Sstevel@tonic-gate 	int		poll_events = POLLPRI;
1361*0Sstevel@tonic-gate 	extern struct pollhead ufs_pollhd;
1362*0Sstevel@tonic-gate 	refstr_t	*mountpoint;
1363*0Sstevel@tonic-gate 
1364*0Sstevel@tonic-gate 	ASSERT(vfs_lock_held(vfsp));
1365*0Sstevel@tonic-gate 
1366*0Sstevel@tonic-gate 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
1367*0Sstevel@tonic-gate 		return (EPERM);
1368*0Sstevel@tonic-gate 	/*
1369*0Sstevel@tonic-gate 	 * Forced unmount is now supported through the
1370*0Sstevel@tonic-gate 	 * lockfs protocol.
1371*0Sstevel@tonic-gate 	 */
1372*0Sstevel@tonic-gate 	if (fflag & MS_FORCE) {
1373*0Sstevel@tonic-gate 		/*
1374*0Sstevel@tonic-gate 		 * Mark the filesystem as being unmounted now in
1375*0Sstevel@tonic-gate 		 * case of a forcible umount before we take any
1376*0Sstevel@tonic-gate 		 * locks inside UFS to prevent racing with a VFS_VGET()
1377*0Sstevel@tonic-gate 		 * request. Throw these VFS_VGET() requests away for
1378*0Sstevel@tonic-gate 		 * the duration of the forcible umount so they won't
1379*0Sstevel@tonic-gate 		 * use stale or even freed data later on when we're done.
1380*0Sstevel@tonic-gate 		 * It may happen that the VFS has had a additional hold
1381*0Sstevel@tonic-gate 		 * placed on it by someone other than UFS and thus will
1382*0Sstevel@tonic-gate 		 * not get freed immediately once we're done with the
1383*0Sstevel@tonic-gate 		 * umount by dounmount() - use VFS_UNMOUNTED to inform
1384*0Sstevel@tonic-gate 		 * users of this still-alive VFS that its corresponding
1385*0Sstevel@tonic-gate 		 * filesystem being gone so they can detect that and error
1386*0Sstevel@tonic-gate 		 * out.
1387*0Sstevel@tonic-gate 		 */
1388*0Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_UNMOUNTED;
1389*0Sstevel@tonic-gate 
1390*0Sstevel@tonic-gate 		ufs_thread_suspend(&ufsvfsp->vfs_delete);
1391*0Sstevel@tonic-gate 		mutex_enter(&ulp->ul_lock);
1392*0Sstevel@tonic-gate 		/*
1393*0Sstevel@tonic-gate 		 * If file system is already hard locked,
1394*0Sstevel@tonic-gate 		 * unmount the file system, otherwise
1395*0Sstevel@tonic-gate 		 * hard lock it before unmounting.
1396*0Sstevel@tonic-gate 		 */
1397*0Sstevel@tonic-gate 		if (!ULOCKFS_IS_HLOCK(ulp)) {
1398*0Sstevel@tonic-gate 			lockfs.lf_lock = LOCKFS_HLOCK;
1399*0Sstevel@tonic-gate 			lockfs.lf_flags = 0;
1400*0Sstevel@tonic-gate 			lockfs.lf_key = ulp->ul_lockfs.lf_key + 1;
1401*0Sstevel@tonic-gate 			lockfs.lf_comlen = 0;
1402*0Sstevel@tonic-gate 			lockfs.lf_comment = NULL;
1403*0Sstevel@tonic-gate 			ufs_freeze(ulp, &lockfs);
1404*0Sstevel@tonic-gate 			ULOCKFS_SET_BUSY(ulp);
1405*0Sstevel@tonic-gate 			LOCKFS_SET_BUSY(&ulp->ul_lockfs);
1406*0Sstevel@tonic-gate 			(void) ufs_quiesce(ulp);
1407*0Sstevel@tonic-gate 			(void) ufs_flush(vfsp);
1408*0Sstevel@tonic-gate 			(void) ufs_thaw(vfsp, ufsvfsp, ulp);
1409*0Sstevel@tonic-gate 			ULOCKFS_CLR_BUSY(ulp);
1410*0Sstevel@tonic-gate 			LOCKFS_CLR_BUSY(&ulp->ul_lockfs);
1411*0Sstevel@tonic-gate 			poll_events |= POLLERR;
1412*0Sstevel@tonic-gate 			pollwakeup(&ufs_pollhd, poll_events);
1413*0Sstevel@tonic-gate 		}
1414*0Sstevel@tonic-gate 		ufs_thread_continue(&ufsvfsp->vfs_delete);
1415*0Sstevel@tonic-gate 		mutex_exit(&ulp->ul_lock);
1416*0Sstevel@tonic-gate 	}
1417*0Sstevel@tonic-gate 
1418*0Sstevel@tonic-gate 	/* let all types of writes go through */
1419*0Sstevel@tonic-gate 	ufsvfsp->vfs_iotstamp = lbolt;
1420*0Sstevel@tonic-gate 
1421*0Sstevel@tonic-gate 	/* coordinate with global hlock thread */
1422*0Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp) && (ufsvfsp->vfs_validfs == UT_HLOCKING)) {
1423*0Sstevel@tonic-gate 		/*
1424*0Sstevel@tonic-gate 		 * last possibility for a forced umount to fail hence clear
1425*0Sstevel@tonic-gate 		 * VFS_UNMOUNTED if appropriate.
1426*0Sstevel@tonic-gate 		 */
1427*0Sstevel@tonic-gate 		if (fflag & MS_FORCE)
1428*0Sstevel@tonic-gate 			vfsp->vfs_flag &= ~VFS_UNMOUNTED;
1429*0Sstevel@tonic-gate 		return (EAGAIN);
1430*0Sstevel@tonic-gate 	}
1431*0Sstevel@tonic-gate 
1432*0Sstevel@tonic-gate 	ufsvfsp->vfs_validfs = UT_UNMOUNTED;
1433*0Sstevel@tonic-gate 
1434*0Sstevel@tonic-gate 	/* kill the reclaim thread */
1435*0Sstevel@tonic-gate 	ufs_thread_exit(&ufsvfsp->vfs_reclaim);
1436*0Sstevel@tonic-gate 
1437*0Sstevel@tonic-gate 	/* suspend the delete thread */
1438*0Sstevel@tonic-gate 	ufs_thread_suspend(&ufsvfsp->vfs_delete);
1439*0Sstevel@tonic-gate 
1440*0Sstevel@tonic-gate 	/*
1441*0Sstevel@tonic-gate 	 * drain the delete and idle queues
1442*0Sstevel@tonic-gate 	 */
1443*0Sstevel@tonic-gate 	ufs_delete_drain(vfsp, -1, 1);
1444*0Sstevel@tonic-gate 	ufs_idle_drain(vfsp);
1445*0Sstevel@tonic-gate 
1446*0Sstevel@tonic-gate 	/*
1447*0Sstevel@tonic-gate 	 * use the lockfs protocol to prevent new ops from starting
1448*0Sstevel@tonic-gate 	 * a forcible umount can not fail beyond this point as
1449*0Sstevel@tonic-gate 	 * we hard-locked the filesystem and drained all current consumers
1450*0Sstevel@tonic-gate 	 * before.
1451*0Sstevel@tonic-gate 	 */
1452*0Sstevel@tonic-gate 	mutex_enter(&ulp->ul_lock);
1453*0Sstevel@tonic-gate 
1454*0Sstevel@tonic-gate 	/*
1455*0Sstevel@tonic-gate 	 * if the file system is busy; return EBUSY
1456*0Sstevel@tonic-gate 	 */
1457*0Sstevel@tonic-gate 	if (ulp->ul_vnops_cnt || ULOCKFS_IS_SLOCK(ulp)) {
1458*0Sstevel@tonic-gate 		error = EBUSY;
1459*0Sstevel@tonic-gate 		goto out;
1460*0Sstevel@tonic-gate 	}
1461*0Sstevel@tonic-gate 
1462*0Sstevel@tonic-gate 	/*
1463*0Sstevel@tonic-gate 	 * if this is not a forced unmount (!hard/error locked), then
1464*0Sstevel@tonic-gate 	 * get rid of every inode except the root and quota inodes
1465*0Sstevel@tonic-gate 	 * also, commit any outstanding transactions
1466*0Sstevel@tonic-gate 	 */
1467*0Sstevel@tonic-gate 	if (!ULOCKFS_IS_HLOCK(ulp) && !ULOCKFS_IS_ELOCK(ulp))
1468*0Sstevel@tonic-gate 		if (error = ufs_flush(vfsp))
1469*0Sstevel@tonic-gate 			goto out;
1470*0Sstevel@tonic-gate 
1471*0Sstevel@tonic-gate 	/*
1472*0Sstevel@tonic-gate 	 * ignore inodes in the cache if fs is hard locked or error locked
1473*0Sstevel@tonic-gate 	 */
1474*0Sstevel@tonic-gate 	rip = VTOI(ufsvfsp->vfs_root);
1475*0Sstevel@tonic-gate 	if (!ULOCKFS_IS_HLOCK(ulp) && !ULOCKFS_IS_ELOCK(ulp)) {
1476*0Sstevel@tonic-gate 		/*
1477*0Sstevel@tonic-gate 		 * Otherwise, only the quota and root inodes are in the cache.
1478*0Sstevel@tonic-gate 		 *
1479*0Sstevel@tonic-gate 		 * Avoid racing with ufs_update() and ufs_sync().
1480*0Sstevel@tonic-gate 		 */
1481*0Sstevel@tonic-gate 		mutex_enter(&ufs_scan_lock);
1482*0Sstevel@tonic-gate 
1483*0Sstevel@tonic-gate 		for (i = 0, ih = ihead; i < inohsz; i++, ih++) {
1484*0Sstevel@tonic-gate 			mutex_enter(&ih_lock[i]);
1485*0Sstevel@tonic-gate 			for (ip = ih->ih_chain[0];
1486*0Sstevel@tonic-gate 					ip != (struct inode *)ih;
1487*0Sstevel@tonic-gate 					ip = ip->i_forw) {
1488*0Sstevel@tonic-gate 				if (ip->i_ufsvfs != ufsvfsp)
1489*0Sstevel@tonic-gate 					continue;
1490*0Sstevel@tonic-gate 				if (ip == ufsvfsp->vfs_qinod)
1491*0Sstevel@tonic-gate 					continue;
1492*0Sstevel@tonic-gate 				if (ip == rip && ITOV(ip)->v_count == 1)
1493*0Sstevel@tonic-gate 					continue;
1494*0Sstevel@tonic-gate 				mutex_exit(&ih_lock[i]);
1495*0Sstevel@tonic-gate 				mutex_exit(&ufs_scan_lock);
1496*0Sstevel@tonic-gate 				error = EBUSY;
1497*0Sstevel@tonic-gate 				goto out;
1498*0Sstevel@tonic-gate 			}
1499*0Sstevel@tonic-gate 			mutex_exit(&ih_lock[i]);
1500*0Sstevel@tonic-gate 		}
1501*0Sstevel@tonic-gate 		mutex_exit(&ufs_scan_lock);
1502*0Sstevel@tonic-gate 	}
1503*0Sstevel@tonic-gate 
1504*0Sstevel@tonic-gate 	/*
1505*0Sstevel@tonic-gate 	 * if a snapshot exists and this is a forced unmount, then delete
1506*0Sstevel@tonic-gate 	 * the snapshot.  Otherwise return EBUSY.  This will insure the
1507*0Sstevel@tonic-gate 	 * snapshot always belongs to a valid file system.
1508*0Sstevel@tonic-gate 	 */
1509*0Sstevel@tonic-gate 	if (ufsvfsp->vfs_snapshot) {
1510*0Sstevel@tonic-gate 		if (ULOCKFS_IS_HLOCK(ulp) || ULOCKFS_IS_ELOCK(ulp)) {
1511*0Sstevel@tonic-gate 			(void) fssnap_delete(&ufsvfsp->vfs_snapshot);
1512*0Sstevel@tonic-gate 		} else {
1513*0Sstevel@tonic-gate 			error = EBUSY;
1514*0Sstevel@tonic-gate 			goto out;
1515*0Sstevel@tonic-gate 		}
1516*0Sstevel@tonic-gate 	}
1517*0Sstevel@tonic-gate 
1518*0Sstevel@tonic-gate 	/*
1519*0Sstevel@tonic-gate 	 * Close the quota file and invalidate anything left in the quota
1520*0Sstevel@tonic-gate 	 * cache for this file system.  Pass kcred to allow all quota
1521*0Sstevel@tonic-gate 	 * manipulations.
1522*0Sstevel@tonic-gate 	 */
1523*0Sstevel@tonic-gate 	(void) closedq(ufsvfsp, kcred);
1524*0Sstevel@tonic-gate 	invalidatedq(ufsvfsp);
1525*0Sstevel@tonic-gate 	/*
1526*0Sstevel@tonic-gate 	 * drain the delete and idle queues
1527*0Sstevel@tonic-gate 	 */
1528*0Sstevel@tonic-gate 	ufs_delete_drain(vfsp, -1, 0);
1529*0Sstevel@tonic-gate 	ufs_idle_drain(vfsp);
1530*0Sstevel@tonic-gate 
1531*0Sstevel@tonic-gate 	/*
1532*0Sstevel@tonic-gate 	 * discard the inodes for this fs (including root, shadow, and quota)
1533*0Sstevel@tonic-gate 	 */
1534*0Sstevel@tonic-gate 	for (i = 0, ih = ihead; i < inohsz; i++, ih++) {
1535*0Sstevel@tonic-gate 		mutex_enter(&ih_lock[i]);
1536*0Sstevel@tonic-gate 		for (inext = 0, ip = ih->ih_chain[0];
1537*0Sstevel@tonic-gate 				ip != (struct inode *)ih;
1538*0Sstevel@tonic-gate 				ip = inext) {
1539*0Sstevel@tonic-gate 			inext = ip->i_forw;
1540*0Sstevel@tonic-gate 			if (ip->i_ufsvfs != ufsvfsp)
1541*0Sstevel@tonic-gate 				continue;
1542*0Sstevel@tonic-gate 			vp = ITOV(ip);
1543*0Sstevel@tonic-gate 			VN_HOLD(vp)
1544*0Sstevel@tonic-gate 			remque(ip);
1545*0Sstevel@tonic-gate 			if (ufs_rmidle(ip))
1546*0Sstevel@tonic-gate 				VN_RELE(vp);
1547*0Sstevel@tonic-gate 			ufs_si_del(ip);
1548*0Sstevel@tonic-gate 			/*
1549*0Sstevel@tonic-gate 			 * rip->i_ufsvfsp is needed by bflush()
1550*0Sstevel@tonic-gate 			 */
1551*0Sstevel@tonic-gate 			if (ip != rip)
1552*0Sstevel@tonic-gate 				ip->i_ufsvfs = NULL;
1553*0Sstevel@tonic-gate 			/*
1554*0Sstevel@tonic-gate 			 * Set vnode's vfsops to dummy ops, which return
1555*0Sstevel@tonic-gate 			 * EIO. This is needed to forced unmounts to work
1556*0Sstevel@tonic-gate 			 * with lofs/nfs properly.
1557*0Sstevel@tonic-gate 			 */
1558*0Sstevel@tonic-gate 			if (ULOCKFS_IS_HLOCK(ulp) || ULOCKFS_IS_ELOCK(ulp))
1559*0Sstevel@tonic-gate 				vp->v_vfsp = &EIO_vfs;
1560*0Sstevel@tonic-gate 			else
1561*0Sstevel@tonic-gate 				vp->v_vfsp = NULL;
1562*0Sstevel@tonic-gate 			vp->v_type = VBAD;
1563*0Sstevel@tonic-gate 			VN_RELE(vp);
1564*0Sstevel@tonic-gate 		}
1565*0Sstevel@tonic-gate 		mutex_exit(&ih_lock[i]);
1566*0Sstevel@tonic-gate 	}
1567*0Sstevel@tonic-gate 	ufs_si_cache_flush(dev);
1568*0Sstevel@tonic-gate 
1569*0Sstevel@tonic-gate 	/*
1570*0Sstevel@tonic-gate 	 * kill the delete thread and drain the idle queue
1571*0Sstevel@tonic-gate 	 */
1572*0Sstevel@tonic-gate 	ufs_thread_exit(&ufsvfsp->vfs_delete);
1573*0Sstevel@tonic-gate 	ufs_idle_drain(vfsp);
1574*0Sstevel@tonic-gate 
1575*0Sstevel@tonic-gate 	bp = ufsvfsp->vfs_bufp;
1576*0Sstevel@tonic-gate 	bvp = ufsvfsp->vfs_devvp;
1577*0Sstevel@tonic-gate 	flag = !fs->fs_ronly;
1578*0Sstevel@tonic-gate 	if (flag) {
1579*0Sstevel@tonic-gate 		bflush(dev);
1580*0Sstevel@tonic-gate 		if (fs->fs_clean != FSBAD) {
1581*0Sstevel@tonic-gate 			if (fs->fs_clean == FSSTABLE)
1582*0Sstevel@tonic-gate 				fs->fs_clean = FSCLEAN;
1583*0Sstevel@tonic-gate 			fs->fs_reclaim &= ~FS_RECLAIM;
1584*0Sstevel@tonic-gate 		}
1585*0Sstevel@tonic-gate 		if (TRANS_ISTRANS(ufsvfsp) &&
1586*0Sstevel@tonic-gate 		    !TRANS_ISERROR(ufsvfsp) &&
1587*0Sstevel@tonic-gate 		    !ULOCKFS_IS_HLOCK(ulp) &&
1588*0Sstevel@tonic-gate 		    (fs->fs_rolled == FS_NEED_ROLL)) {
1589*0Sstevel@tonic-gate 			/*
1590*0Sstevel@tonic-gate 			 * ufs_flush() above has flushed the last Moby.
1591*0Sstevel@tonic-gate 			 * This is needed to ensure the following superblock
1592*0Sstevel@tonic-gate 			 * update really is the last metadata update
1593*0Sstevel@tonic-gate 			 */
1594*0Sstevel@tonic-gate 			error = ufs_putsummaryinfo(dev, ufsvfsp, fs);
1595*0Sstevel@tonic-gate 			if (error == 0) {
1596*0Sstevel@tonic-gate 				fs->fs_rolled = FS_ALL_ROLLED;
1597*0Sstevel@tonic-gate 			}
1598*0Sstevel@tonic-gate 		}
1599*0Sstevel@tonic-gate 		TRANS_SBUPDATE(ufsvfsp, vfsp, TOP_SBUPDATE_UNMOUNT);
1600*0Sstevel@tonic-gate 		/*
1601*0Sstevel@tonic-gate 		 * push this last transaction
1602*0Sstevel@tonic-gate 		 */
1603*0Sstevel@tonic-gate 		curthread->t_flag |= T_DONTBLOCK;
1604*0Sstevel@tonic-gate 		TRANS_BEGIN_SYNC(ufsvfsp, TOP_COMMIT_UNMOUNT, TOP_COMMIT_SIZE,
1605*0Sstevel@tonic-gate 		    error);
1606*0Sstevel@tonic-gate 		if (!error)
1607*0Sstevel@tonic-gate 			TRANS_END_SYNC(ufsvfsp, error, TOP_COMMIT_UNMOUNT,
1608*0Sstevel@tonic-gate 			    TOP_COMMIT_SIZE);
1609*0Sstevel@tonic-gate 		curthread->t_flag &= ~T_DONTBLOCK;
1610*0Sstevel@tonic-gate 	}
1611*0Sstevel@tonic-gate 
1612*0Sstevel@tonic-gate 	TRANS_MATA_UMOUNT(ufsvfsp);
1613*0Sstevel@tonic-gate 	lufs_unsnarf(ufsvfsp);		/* Release the in-memory structs */
1614*0Sstevel@tonic-gate 	ufsfx_unmount(ufsvfsp);		/* fix-on-panic bookkeeping */
1615*0Sstevel@tonic-gate 	kmem_free(fs->fs_u.fs_csp, fs->fs_cssize);
1616*0Sstevel@tonic-gate 
1617*0Sstevel@tonic-gate 	bp->b_flags |= B_STALE|B_AGE;
1618*0Sstevel@tonic-gate 	ufsvfsp->vfs_bufp = NULL;	/* don't point at freed buf */
1619*0Sstevel@tonic-gate 	brelse(bp);			/* free the superblock buf */
1620*0Sstevel@tonic-gate 
1621*0Sstevel@tonic-gate 	(void) VOP_PUTPAGE(common_specvp(bvp), (offset_t)0, (size_t)0,
1622*0Sstevel@tonic-gate 	    B_INVAL, cr);
1623*0Sstevel@tonic-gate 	(void) VOP_CLOSE(bvp, flag, 1, (offset_t)0, cr);
1624*0Sstevel@tonic-gate 	bflush(dev);
1625*0Sstevel@tonic-gate 	(void) bfinval(dev, 1);
1626*0Sstevel@tonic-gate 	VN_RELE(bvp);
1627*0Sstevel@tonic-gate 
1628*0Sstevel@tonic-gate 	/*
1629*0Sstevel@tonic-gate 	 * It is now safe to NULL out the ufsvfs pointer and discard
1630*0Sstevel@tonic-gate 	 * the root inode.
1631*0Sstevel@tonic-gate 	 */
1632*0Sstevel@tonic-gate 	rip->i_ufsvfs = NULL;
1633*0Sstevel@tonic-gate 	VN_RELE(ITOV(rip));
1634*0Sstevel@tonic-gate 
1635*0Sstevel@tonic-gate 	/* free up lockfs comment structure, if any */
1636*0Sstevel@tonic-gate 	if (ulp->ul_lockfs.lf_comlen && ulp->ul_lockfs.lf_comment)
1637*0Sstevel@tonic-gate 		kmem_free(ulp->ul_lockfs.lf_comment, ulp->ul_lockfs.lf_comlen);
1638*0Sstevel@tonic-gate 
1639*0Sstevel@tonic-gate 	/*
1640*0Sstevel@tonic-gate 	 * Remove from instance list.
1641*0Sstevel@tonic-gate 	 */
1642*0Sstevel@tonic-gate 	ufs_vfs_remove(ufsvfsp);
1643*0Sstevel@tonic-gate 
1644*0Sstevel@tonic-gate 	/*
1645*0Sstevel@tonic-gate 	 * For a forcible unmount, threads may be asleep in
1646*0Sstevel@tonic-gate 	 * ufs_lockfs_begin/ufs_check_lockfs.  These threads will need
1647*0Sstevel@tonic-gate 	 * the ufsvfs structure so we don't free it, yet.  ufs_update
1648*0Sstevel@tonic-gate 	 * will free it up after awhile.
1649*0Sstevel@tonic-gate 	 */
1650*0Sstevel@tonic-gate 	if (ULOCKFS_IS_HLOCK(ulp) || ULOCKFS_IS_ELOCK(ulp)) {
1651*0Sstevel@tonic-gate 		extern kmutex_t		ufsvfs_mutex;
1652*0Sstevel@tonic-gate 		extern struct ufsvfs	*ufsvfslist;
1653*0Sstevel@tonic-gate 
1654*0Sstevel@tonic-gate 		mutex_enter(&ufsvfs_mutex);
1655*0Sstevel@tonic-gate 		ufsvfsp->vfs_dontblock = 1;
1656*0Sstevel@tonic-gate 		ufsvfsp->vfs_next = ufsvfslist;
1657*0Sstevel@tonic-gate 		ufsvfslist = ufsvfsp;
1658*0Sstevel@tonic-gate 		mutex_exit(&ufsvfs_mutex);
1659*0Sstevel@tonic-gate 		/* wakeup any suspended threads */
1660*0Sstevel@tonic-gate 		cv_broadcast(&ulp->ul_cv);
1661*0Sstevel@tonic-gate 		mutex_exit(&ulp->ul_lock);
1662*0Sstevel@tonic-gate 	} else {
1663*0Sstevel@tonic-gate 		mutex_destroy(&ufsvfsp->vfs_lock);
1664*0Sstevel@tonic-gate 		kmem_free(ufsvfsp, sizeof (struct ufsvfs));
1665*0Sstevel@tonic-gate 	}
1666*0Sstevel@tonic-gate 
1667*0Sstevel@tonic-gate 	/*
1668*0Sstevel@tonic-gate 	 * Now mark the filesystem as unmounted since we're done with it.
1669*0Sstevel@tonic-gate 	 */
1670*0Sstevel@tonic-gate 	vfsp->vfs_flag |= VFS_UNMOUNTED;
1671*0Sstevel@tonic-gate 
1672*0Sstevel@tonic-gate 	return (0);
1673*0Sstevel@tonic-gate out:
1674*0Sstevel@tonic-gate 	/* open the fs to new ops */
1675*0Sstevel@tonic-gate 	cv_broadcast(&ulp->ul_cv);
1676*0Sstevel@tonic-gate 	mutex_exit(&ulp->ul_lock);
1677*0Sstevel@tonic-gate 
1678*0Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp)) {
1679*0Sstevel@tonic-gate 		/* allow the delete thread to continue */
1680*0Sstevel@tonic-gate 		ufs_thread_continue(&ufsvfsp->vfs_delete);
1681*0Sstevel@tonic-gate 		/* restart the reclaim thread */
1682*0Sstevel@tonic-gate 		ufs_thread_start(&ufsvfsp->vfs_reclaim, ufs_thread_reclaim,
1683*0Sstevel@tonic-gate 				vfsp);
1684*0Sstevel@tonic-gate 		/* coordinate with global hlock thread */
1685*0Sstevel@tonic-gate 		ufsvfsp->vfs_validfs = UT_MOUNTED;
1686*0Sstevel@tonic-gate 		/* check for trans errors during umount */
1687*0Sstevel@tonic-gate 		ufs_trans_onerror();
1688*0Sstevel@tonic-gate 
1689*0Sstevel@tonic-gate 		/*
1690*0Sstevel@tonic-gate 		 * if we have a seperate /usr it will never unmount
1691*0Sstevel@tonic-gate 		 * when halting. In order to not re-read all the
1692*0Sstevel@tonic-gate 		 * cylinder group summary info on mounting after
1693*0Sstevel@tonic-gate 		 * reboot the logging of summary info is re-enabled
1694*0Sstevel@tonic-gate 		 * and the super block written out.
1695*0Sstevel@tonic-gate 		 */
1696*0Sstevel@tonic-gate 		mountpoint = vfs_getmntpoint(vfsp);
1697*0Sstevel@tonic-gate 		if ((fs->fs_si == FS_SI_OK) &&
1698*0Sstevel@tonic-gate 		    (strcmp("/usr", refstr_value(mountpoint)) == 0)) {
1699*0Sstevel@tonic-gate 			ufsvfsp->vfs_nolog_si = 0;
1700*0Sstevel@tonic-gate 			UFS_BWRITE2(NULL, ufsvfsp->vfs_bufp);
1701*0Sstevel@tonic-gate 		}
1702*0Sstevel@tonic-gate 		refstr_rele(mountpoint);
1703*0Sstevel@tonic-gate 	}
1704*0Sstevel@tonic-gate 
1705*0Sstevel@tonic-gate 	return (error);
1706*0Sstevel@tonic-gate }
1707*0Sstevel@tonic-gate 
1708*0Sstevel@tonic-gate static int
1709*0Sstevel@tonic-gate ufs_root(struct vfs *vfsp, struct vnode **vpp)
1710*0Sstevel@tonic-gate {
1711*0Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
1712*0Sstevel@tonic-gate 	struct vnode *vp;
1713*0Sstevel@tonic-gate 
1714*0Sstevel@tonic-gate 	if (!vfsp)
1715*0Sstevel@tonic-gate 		return (EIO);
1716*0Sstevel@tonic-gate 
1717*0Sstevel@tonic-gate 	ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
1718*0Sstevel@tonic-gate 	if (!ufsvfsp || !ufsvfsp->vfs_root)
1719*0Sstevel@tonic-gate 		return (EIO);	/* forced unmount */
1720*0Sstevel@tonic-gate 
1721*0Sstevel@tonic-gate 	vp = ufsvfsp->vfs_root;
1722*0Sstevel@tonic-gate 	VN_HOLD(vp);
1723*0Sstevel@tonic-gate 	*vpp = vp;
1724*0Sstevel@tonic-gate 	return (0);
1725*0Sstevel@tonic-gate }
1726*0Sstevel@tonic-gate 
1727*0Sstevel@tonic-gate /*
1728*0Sstevel@tonic-gate  * Get file system statistics.
1729*0Sstevel@tonic-gate  */
1730*0Sstevel@tonic-gate static int
1731*0Sstevel@tonic-gate ufs_statvfs(struct vfs *vfsp, struct statvfs64 *sp)
1732*0Sstevel@tonic-gate {
1733*0Sstevel@tonic-gate 	struct fs *fsp;
1734*0Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
1735*0Sstevel@tonic-gate 	int blk, i;
1736*0Sstevel@tonic-gate 	long max_avail, used;
1737*0Sstevel@tonic-gate 	dev32_t d32;
1738*0Sstevel@tonic-gate 	struct ufs_q *delq;
1739*0Sstevel@tonic-gate 
1740*0Sstevel@tonic-gate 	if (vfsp->vfs_flag & VFS_UNMOUNTED)
1741*0Sstevel@tonic-gate 		return (EIO);
1742*0Sstevel@tonic-gate 
1743*0Sstevel@tonic-gate 	ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
1744*0Sstevel@tonic-gate 	fsp = ufsvfsp->vfs_fs;
1745*0Sstevel@tonic-gate 	if ((fsp->fs_magic != FS_MAGIC) && (fsp->fs_magic != MTB_UFS_MAGIC))
1746*0Sstevel@tonic-gate 		return (EINVAL);
1747*0Sstevel@tonic-gate 	if (fsp->fs_magic == MTB_UFS_MAGIC &&
1748*0Sstevel@tonic-gate 	    (fsp->fs_version > MTB_UFS_VERSION_1 ||
1749*0Sstevel@tonic-gate 	    fsp->fs_version < MTB_UFS_VERSION_MIN))
1750*0Sstevel@tonic-gate 		return (EINVAL);
1751*0Sstevel@tonic-gate 
1752*0Sstevel@tonic-gate 	/*
1753*0Sstevel@tonic-gate 	 * Can't get a self-consistent result with the delete thread running
1754*0Sstevel@tonic-gate 	 * or if others come in and jumble the queue up for us.  This is
1755*0Sstevel@tonic-gate 	 * a no-op if there is no delete thread.
1756*0Sstevel@tonic-gate 	 */
1757*0Sstevel@tonic-gate 	delq = &ufsvfsp->vfs_delete;
1758*0Sstevel@tonic-gate 	ufs_thread_suspend(delq);
1759*0Sstevel@tonic-gate 
1760*0Sstevel@tonic-gate 	/*
1761*0Sstevel@tonic-gate 	 * get the basic numbers
1762*0Sstevel@tonic-gate 	 */
1763*0Sstevel@tonic-gate 	(void) bzero(sp, sizeof (*sp));
1764*0Sstevel@tonic-gate 
1765*0Sstevel@tonic-gate 	sp->f_bsize = fsp->fs_bsize;
1766*0Sstevel@tonic-gate 	sp->f_frsize = fsp->fs_fsize;
1767*0Sstevel@tonic-gate 	sp->f_blocks = (fsblkcnt64_t)fsp->fs_dsize;
1768*0Sstevel@tonic-gate 	sp->f_bfree = (fsblkcnt64_t)fsp->fs_cstotal.cs_nbfree * fsp->fs_frag +
1769*0Sstevel@tonic-gate 	    fsp->fs_cstotal.cs_nffree;
1770*0Sstevel@tonic-gate 
1771*0Sstevel@tonic-gate 	sp->f_files = (fsfilcnt64_t)fsp->fs_ncg * fsp->fs_ipg;
1772*0Sstevel@tonic-gate 	sp->f_ffree = (fsfilcnt64_t)fsp->fs_cstotal.cs_nifree;
1773*0Sstevel@tonic-gate 
1774*0Sstevel@tonic-gate 	/*
1775*0Sstevel@tonic-gate 	 * Adjust the numbers based on things waiting to be deleted.
1776*0Sstevel@tonic-gate 	 * modifies f_bfree and f_ffree.  Afterwards, everything we
1777*0Sstevel@tonic-gate 	 * come up with will be self-consistent.  By definition, this
1778*0Sstevel@tonic-gate 	 * is a point-in-time snapshot, so the fact that the delete
1779*0Sstevel@tonic-gate 	 * thread's probably already invalidated the results is not a
1780*0Sstevel@tonic-gate 	 * problem.
1781*0Sstevel@tonic-gate 	 */
1782*0Sstevel@tonic-gate 	ufs_delete_adjust_stats(ufsvfsp, sp);
1783*0Sstevel@tonic-gate 	ufs_thread_continue(delq);
1784*0Sstevel@tonic-gate 
1785*0Sstevel@tonic-gate 	/*
1786*0Sstevel@tonic-gate 	 * avail = MAX(max_avail - used, 0)
1787*0Sstevel@tonic-gate 	 */
1788*0Sstevel@tonic-gate 	max_avail = fsp->fs_dsize - ufsvfsp->vfs_minfrags;
1789*0Sstevel@tonic-gate 
1790*0Sstevel@tonic-gate 	used = (fsp->fs_dsize - sp->f_bfree);
1791*0Sstevel@tonic-gate 
1792*0Sstevel@tonic-gate 	if (max_avail > used)
1793*0Sstevel@tonic-gate 		sp->f_bavail = (fsblkcnt64_t)max_avail - used;
1794*0Sstevel@tonic-gate 	else
1795*0Sstevel@tonic-gate 		sp->f_bavail = (fsblkcnt64_t)0;
1796*0Sstevel@tonic-gate 
1797*0Sstevel@tonic-gate 	sp->f_favail = sp->f_ffree;
1798*0Sstevel@tonic-gate 	(void) cmpldev(&d32, vfsp->vfs_dev);
1799*0Sstevel@tonic-gate 	sp->f_fsid = d32;
1800*0Sstevel@tonic-gate 	(void) strcpy(sp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1801*0Sstevel@tonic-gate 	sp->f_flag = vf_to_stf(vfsp->vfs_flag);
1802*0Sstevel@tonic-gate 
1803*0Sstevel@tonic-gate 	/* keep coordinated with ufs_l_pathconf() */
1804*0Sstevel@tonic-gate 	sp->f_namemax = MAXNAMLEN;
1805*0Sstevel@tonic-gate 
1806*0Sstevel@tonic-gate 	if (fsp->fs_cpc == 0) {
1807*0Sstevel@tonic-gate 		bzero(sp->f_fstr, 14);
1808*0Sstevel@tonic-gate 		return (0);
1809*0Sstevel@tonic-gate 	}
1810*0Sstevel@tonic-gate 	blk = fsp->fs_spc * fsp->fs_cpc / NSPF(fsp);
1811*0Sstevel@tonic-gate 	for (i = 0; i < blk; i += fsp->fs_frag) /* CSTYLED */
1812*0Sstevel@tonic-gate 		/* void */;
1813*0Sstevel@tonic-gate 	i -= fsp->fs_frag;
1814*0Sstevel@tonic-gate 	blk = i / fsp->fs_frag;
1815*0Sstevel@tonic-gate 	bcopy(&(fs_rotbl(fsp)[blk]), sp->f_fstr, 14);
1816*0Sstevel@tonic-gate 	return (0);
1817*0Sstevel@tonic-gate }
1818*0Sstevel@tonic-gate 
1819*0Sstevel@tonic-gate /*
1820*0Sstevel@tonic-gate  * Flush any pending I/O to file system vfsp.
1821*0Sstevel@tonic-gate  * The ufs_update() routine will only flush *all* ufs files.
1822*0Sstevel@tonic-gate  * If vfsp is non-NULL, only sync this ufs (in preparation
1823*0Sstevel@tonic-gate  * for a umount).
1824*0Sstevel@tonic-gate  */
1825*0Sstevel@tonic-gate /*ARGSUSED*/
1826*0Sstevel@tonic-gate static int
1827*0Sstevel@tonic-gate ufs_sync(struct vfs *vfsp, short flag, struct cred *cr)
1828*0Sstevel@tonic-gate {
1829*0Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
1830*0Sstevel@tonic-gate 	struct fs *fs;
1831*0Sstevel@tonic-gate 	int cheap = flag & SYNC_ATTR;
1832*0Sstevel@tonic-gate 	int error;
1833*0Sstevel@tonic-gate 
1834*0Sstevel@tonic-gate 	/*
1835*0Sstevel@tonic-gate 	 * SYNC_CLOSE means we're rebooting.  Toss everything
1836*0Sstevel@tonic-gate 	 * on the idle queue so we don't have to slog through
1837*0Sstevel@tonic-gate 	 * a bunch of uninteresting inodes over and over again.
1838*0Sstevel@tonic-gate 	 */
1839*0Sstevel@tonic-gate 	if (flag & SYNC_CLOSE)
1840*0Sstevel@tonic-gate 		ufs_idle_drain(NULL);
1841*0Sstevel@tonic-gate 
1842*0Sstevel@tonic-gate 	if (vfsp == NULL) {
1843*0Sstevel@tonic-gate 		ufs_update(flag);
1844*0Sstevel@tonic-gate 		return (0);
1845*0Sstevel@tonic-gate 	}
1846*0Sstevel@tonic-gate 
1847*0Sstevel@tonic-gate 	/* Flush a single ufs */
1848*0Sstevel@tonic-gate 	if (!vfs_matchops(vfsp, ufs_vfsops) || vfs_lock(vfsp) != 0)
1849*0Sstevel@tonic-gate 		return (0);
1850*0Sstevel@tonic-gate 
1851*0Sstevel@tonic-gate 	ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
1852*0Sstevel@tonic-gate 	if (!ufsvfsp)
1853*0Sstevel@tonic-gate 		return (EIO);
1854*0Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
1855*0Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
1856*0Sstevel@tonic-gate 
1857*0Sstevel@tonic-gate 	if (ufsvfsp->vfs_dio &&
1858*0Sstevel@tonic-gate 	    fs->fs_ronly == 0 &&
1859*0Sstevel@tonic-gate 	    fs->fs_clean != FSBAD &&
1860*0Sstevel@tonic-gate 	    fs->fs_clean != FSLOG) {
1861*0Sstevel@tonic-gate 		/* turn off fast-io on unmount, so no fsck needed (4029401) */
1862*0Sstevel@tonic-gate 		ufsvfsp->vfs_dio = 0;
1863*0Sstevel@tonic-gate 		fs->fs_clean = FSACTIVE;
1864*0Sstevel@tonic-gate 		fs->fs_fmod = 1;
1865*0Sstevel@tonic-gate 	}
1866*0Sstevel@tonic-gate 
1867*0Sstevel@tonic-gate 	/* Write back modified superblock */
1868*0Sstevel@tonic-gate 	if (fs->fs_fmod == 0) {
1869*0Sstevel@tonic-gate 		mutex_exit(&ufsvfsp->vfs_lock);
1870*0Sstevel@tonic-gate 	} else {
1871*0Sstevel@tonic-gate 		if (fs->fs_ronly != 0) {
1872*0Sstevel@tonic-gate 			mutex_exit(&ufsvfsp->vfs_lock);
1873*0Sstevel@tonic-gate 			vfs_unlock(vfsp);
1874*0Sstevel@tonic-gate 			return (ufs_fault(ufsvfsp->vfs_root,
1875*0Sstevel@tonic-gate 					    "fs = %s update: ro fs mod\n",
1876*0Sstevel@tonic-gate 					    fs->fs_fsmnt));
1877*0Sstevel@tonic-gate 		}
1878*0Sstevel@tonic-gate 		fs->fs_fmod = 0;
1879*0Sstevel@tonic-gate 		mutex_exit(&ufsvfsp->vfs_lock);
1880*0Sstevel@tonic-gate 
1881*0Sstevel@tonic-gate 		TRANS_SBUPDATE(ufsvfsp, vfsp, TOP_SBUPDATE_UPDATE);
1882*0Sstevel@tonic-gate 	}
1883*0Sstevel@tonic-gate 	vfs_unlock(vfsp);
1884*0Sstevel@tonic-gate 
1885*0Sstevel@tonic-gate 	/*
1886*0Sstevel@tonic-gate 	 * Avoid racing with ufs_update() and ufs_unmount().
1887*0Sstevel@tonic-gate 	 *
1888*0Sstevel@tonic-gate 	 */
1889*0Sstevel@tonic-gate 	mutex_enter(&ufs_scan_lock);
1890*0Sstevel@tonic-gate 
1891*0Sstevel@tonic-gate 	(void) ufs_scan_inodes(1, ufs_sync_inode,
1892*0Sstevel@tonic-gate 	    (void *)(uintptr_t)cheap, ufsvfsp);
1893*0Sstevel@tonic-gate 
1894*0Sstevel@tonic-gate 	mutex_exit(&ufs_scan_lock);
1895*0Sstevel@tonic-gate 
1896*0Sstevel@tonic-gate 	bflush((dev_t)vfsp->vfs_dev);
1897*0Sstevel@tonic-gate 
1898*0Sstevel@tonic-gate 	/*
1899*0Sstevel@tonic-gate 	 * commit any outstanding async transactions
1900*0Sstevel@tonic-gate 	 */
1901*0Sstevel@tonic-gate 	curthread->t_flag |= T_DONTBLOCK;
1902*0Sstevel@tonic-gate 	TRANS_BEGIN_SYNC(ufsvfsp, TOP_COMMIT_UPDATE, TOP_COMMIT_SIZE, error);
1903*0Sstevel@tonic-gate 	if (!error) {
1904*0Sstevel@tonic-gate 		TRANS_END_SYNC(ufsvfsp, error, TOP_COMMIT_UPDATE,
1905*0Sstevel@tonic-gate 		    TOP_COMMIT_SIZE);
1906*0Sstevel@tonic-gate 	}
1907*0Sstevel@tonic-gate 	curthread->t_flag &= ~T_DONTBLOCK;
1908*0Sstevel@tonic-gate 
1909*0Sstevel@tonic-gate 	return (0);
1910*0Sstevel@tonic-gate }
1911*0Sstevel@tonic-gate 
1912*0Sstevel@tonic-gate 
1913*0Sstevel@tonic-gate void
1914*0Sstevel@tonic-gate sbupdate(struct vfs *vfsp)
1915*0Sstevel@tonic-gate {
1916*0Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
1917*0Sstevel@tonic-gate 	struct fs *fs = ufsvfsp->vfs_fs;
1918*0Sstevel@tonic-gate 	struct buf *bp;
1919*0Sstevel@tonic-gate 	int blks;
1920*0Sstevel@tonic-gate 	caddr_t space;
1921*0Sstevel@tonic-gate 	int i;
1922*0Sstevel@tonic-gate 	size_t size;
1923*0Sstevel@tonic-gate 
1924*0Sstevel@tonic-gate 	/*
1925*0Sstevel@tonic-gate 	 * for ulockfs processing, limit the superblock writes
1926*0Sstevel@tonic-gate 	 */
1927*0Sstevel@tonic-gate 	if ((ufsvfsp->vfs_ulockfs.ul_sbowner) &&
1928*0Sstevel@tonic-gate 	    (curthread != ufsvfsp->vfs_ulockfs.ul_sbowner)) {
1929*0Sstevel@tonic-gate 		/* process later */
1930*0Sstevel@tonic-gate 		fs->fs_fmod = 1;
1931*0Sstevel@tonic-gate 		return;
1932*0Sstevel@tonic-gate 	}
1933*0Sstevel@tonic-gate 	ULOCKFS_SET_MOD((&ufsvfsp->vfs_ulockfs));
1934*0Sstevel@tonic-gate 
1935*0Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp)) {
1936*0Sstevel@tonic-gate 		mutex_enter(&ufsvfsp->vfs_lock);
1937*0Sstevel@tonic-gate 		ufs_sbwrite(ufsvfsp);
1938*0Sstevel@tonic-gate 		mutex_exit(&ufsvfsp->vfs_lock);
1939*0Sstevel@tonic-gate 		return;
1940*0Sstevel@tonic-gate 	}
1941*0Sstevel@tonic-gate 
1942*0Sstevel@tonic-gate 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
1943*0Sstevel@tonic-gate 	space = (caddr_t)fs->fs_u.fs_csp;
1944*0Sstevel@tonic-gate 	for (i = 0; i < blks; i += fs->fs_frag) {
1945*0Sstevel@tonic-gate 		size = fs->fs_bsize;
1946*0Sstevel@tonic-gate 		if (i + fs->fs_frag > blks)
1947*0Sstevel@tonic-gate 			size = (blks - i) * fs->fs_fsize;
1948*0Sstevel@tonic-gate 		bp = UFS_GETBLK(ufsvfsp, ufsvfsp->vfs_dev,
1949*0Sstevel@tonic-gate 			(daddr_t)(fsbtodb(fs, fs->fs_csaddr + i)),
1950*0Sstevel@tonic-gate 			fs->fs_bsize);
1951*0Sstevel@tonic-gate 		bcopy(space, bp->b_un.b_addr, size);
1952*0Sstevel@tonic-gate 		space += size;
1953*0Sstevel@tonic-gate 		bp->b_bcount = size;
1954*0Sstevel@tonic-gate 		UFS_BRWRITE(ufsvfsp, bp);
1955*0Sstevel@tonic-gate 	}
1956*0Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
1957*0Sstevel@tonic-gate 	ufs_sbwrite(ufsvfsp);
1958*0Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
1959*0Sstevel@tonic-gate }
1960*0Sstevel@tonic-gate 
1961*0Sstevel@tonic-gate int ufs_vget_idle_count = 2;	/* Number of inodes to idle each time */
1962*0Sstevel@tonic-gate static int
1963*0Sstevel@tonic-gate ufs_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp)
1964*0Sstevel@tonic-gate {
1965*0Sstevel@tonic-gate 	int error = 0;
1966*0Sstevel@tonic-gate 	struct ufid *ufid;
1967*0Sstevel@tonic-gate 	struct inode *ip;
1968*0Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
1969*0Sstevel@tonic-gate 	struct ulockfs *ulp;
1970*0Sstevel@tonic-gate 
1971*0Sstevel@tonic-gate 	/*
1972*0Sstevel@tonic-gate 	 * Check for unmounted filesystem.
1973*0Sstevel@tonic-gate 	 */
1974*0Sstevel@tonic-gate 	if (vfsp->vfs_flag & VFS_UNMOUNTED) {
1975*0Sstevel@tonic-gate 		error = EIO;
1976*0Sstevel@tonic-gate 		goto errout;
1977*0Sstevel@tonic-gate 	}
1978*0Sstevel@tonic-gate 
1979*0Sstevel@tonic-gate 	/*
1980*0Sstevel@tonic-gate 	 * Keep the idle queue from getting too long by
1981*0Sstevel@tonic-gate 	 * idling an inode before attempting to allocate another.
1982*0Sstevel@tonic-gate 	 *    This operation must be performed before entering
1983*0Sstevel@tonic-gate 	 *    lockfs or a transaction.
1984*0Sstevel@tonic-gate 	 */
1985*0Sstevel@tonic-gate 	if (ufs_idle_q.uq_ne > ufs_idle_q.uq_hiwat)
1986*0Sstevel@tonic-gate 		if ((curthread->t_flag & T_DONTBLOCK) == 0) {
1987*0Sstevel@tonic-gate 			ins.in_vidles.value.ul += ufs_vget_idle_count;
1988*0Sstevel@tonic-gate 			ufs_idle_some(ufs_vget_idle_count);
1989*0Sstevel@tonic-gate 		}
1990*0Sstevel@tonic-gate 
1991*0Sstevel@tonic-gate 	ufid = (struct ufid *)fidp;
1992*0Sstevel@tonic-gate 
1993*0Sstevel@tonic-gate 	if (error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_VGET_MASK))
1994*0Sstevel@tonic-gate 		goto errout;
1995*0Sstevel@tonic-gate 
1996*0Sstevel@tonic-gate 	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
1997*0Sstevel@tonic-gate 
1998*0Sstevel@tonic-gate 	error = ufs_iget(vfsp, ufid->ufid_ino, &ip, CRED());
1999*0Sstevel@tonic-gate 
2000*0Sstevel@tonic-gate 	rw_exit(&ufsvfsp->vfs_dqrwlock);
2001*0Sstevel@tonic-gate 
2002*0Sstevel@tonic-gate 	ufs_lockfs_end(ulp);
2003*0Sstevel@tonic-gate 
2004*0Sstevel@tonic-gate 	if (error)
2005*0Sstevel@tonic-gate 		goto errout;
2006*0Sstevel@tonic-gate 
2007*0Sstevel@tonic-gate 	/*
2008*0Sstevel@tonic-gate 	 * Check if the inode has been deleted or freed or is in transient state
2009*0Sstevel@tonic-gate 	 * since the last VFS_VGET() request for it, release it and don't return
2010*0Sstevel@tonic-gate 	 * it to the caller, presumably NFS, as it's no longer valid.
2011*0Sstevel@tonic-gate 	 */
2012*0Sstevel@tonic-gate 	if (ip->i_gen != ufid->ufid_gen || ip->i_mode == 0 ||
2013*0Sstevel@tonic-gate 	    (ip->i_flag & IDEL)) {
2014*0Sstevel@tonic-gate 		VN_RELE(ITOV(ip));
2015*0Sstevel@tonic-gate 		error = EINVAL;
2016*0Sstevel@tonic-gate 		goto errout;
2017*0Sstevel@tonic-gate 	}
2018*0Sstevel@tonic-gate 
2019*0Sstevel@tonic-gate 	*vpp = ITOV(ip);
2020*0Sstevel@tonic-gate 	return (0);
2021*0Sstevel@tonic-gate 
2022*0Sstevel@tonic-gate errout:
2023*0Sstevel@tonic-gate 	*vpp = NULL;
2024*0Sstevel@tonic-gate 	return (error);
2025*0Sstevel@tonic-gate }
2026*0Sstevel@tonic-gate 
2027*0Sstevel@tonic-gate static int
2028*0Sstevel@tonic-gate ufsinit(int fstype, char *name)
2029*0Sstevel@tonic-gate {
2030*0Sstevel@tonic-gate 	static const fs_operation_def_t ufs_vfsops_template[] = {
2031*0Sstevel@tonic-gate 		VFSNAME_MOUNT, ufs_mount,
2032*0Sstevel@tonic-gate 		VFSNAME_UNMOUNT, ufs_unmount,
2033*0Sstevel@tonic-gate 		VFSNAME_ROOT, ufs_root,
2034*0Sstevel@tonic-gate 		VFSNAME_STATVFS, ufs_statvfs,
2035*0Sstevel@tonic-gate 		VFSNAME_SYNC, (fs_generic_func_p) ufs_sync,
2036*0Sstevel@tonic-gate 		VFSNAME_VGET, ufs_vget,
2037*0Sstevel@tonic-gate 		VFSNAME_MOUNTROOT, ufs_mountroot,
2038*0Sstevel@tonic-gate 		NULL, NULL
2039*0Sstevel@tonic-gate 	};
2040*0Sstevel@tonic-gate 	int error;
2041*0Sstevel@tonic-gate 
2042*0Sstevel@tonic-gate 	ufsfstype = fstype;
2043*0Sstevel@tonic-gate 
2044*0Sstevel@tonic-gate 	error = vfs_setfsops(fstype, ufs_vfsops_template, &ufs_vfsops);
2045*0Sstevel@tonic-gate 	if (error != 0) {
2046*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "ufsinit: bad vfs ops template");
2047*0Sstevel@tonic-gate 		return (error);
2048*0Sstevel@tonic-gate 	}
2049*0Sstevel@tonic-gate 
2050*0Sstevel@tonic-gate 	error = vn_make_ops(name, ufs_vnodeops_template, &ufs_vnodeops);
2051*0Sstevel@tonic-gate 	if (error != 0) {
2052*0Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
2053*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "ufsinit: bad vnode ops template");
2054*0Sstevel@tonic-gate 		return (error);
2055*0Sstevel@tonic-gate 	}
2056*0Sstevel@tonic-gate 
2057*0Sstevel@tonic-gate 	ufs_iinit();
2058*0Sstevel@tonic-gate 	return (0);
2059*0Sstevel@tonic-gate }
2060*0Sstevel@tonic-gate 
2061*0Sstevel@tonic-gate #ifdef __sparc
2062*0Sstevel@tonic-gate 
2063*0Sstevel@tonic-gate /*
2064*0Sstevel@tonic-gate  * Mounting a mirrored SVM volume is only supported on ufs,
2065*0Sstevel@tonic-gate  * this is special-case boot code to support that configuration.
2066*0Sstevel@tonic-gate  * At this point, we have booted and mounted root on a
2067*0Sstevel@tonic-gate  * single component of the mirror.  Complete the boot
2068*0Sstevel@tonic-gate  * by configuring SVM and converting the root to the
2069*0Sstevel@tonic-gate  * dev_t of the mirrored root device.  This dev_t conversion
2070*0Sstevel@tonic-gate  * only works because the underlying device doesn't change.
2071*0Sstevel@tonic-gate  */
2072*0Sstevel@tonic-gate int
2073*0Sstevel@tonic-gate ufs_remountroot(struct vfs *vfsp)
2074*0Sstevel@tonic-gate {
2075*0Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
2076*0Sstevel@tonic-gate 	struct ulockfs *ulp;
2077*0Sstevel@tonic-gate 	dev_t new_rootdev;
2078*0Sstevel@tonic-gate 	dev_t old_rootdev;
2079*0Sstevel@tonic-gate 	struct vnode *old_rootvp;
2080*0Sstevel@tonic-gate 	struct vnode *new_rootvp;
2081*0Sstevel@tonic-gate 	int error, sberror = 0;
2082*0Sstevel@tonic-gate 	struct inode	*ip;
2083*0Sstevel@tonic-gate 	union ihead	*ih;
2084*0Sstevel@tonic-gate 	struct buf	*bp;
2085*0Sstevel@tonic-gate 	int i;
2086*0Sstevel@tonic-gate 
2087*0Sstevel@tonic-gate 	old_rootdev = rootdev;
2088*0Sstevel@tonic-gate 	old_rootvp = rootvp;
2089*0Sstevel@tonic-gate 
2090*0Sstevel@tonic-gate 	new_rootdev = getrootdev();
2091*0Sstevel@tonic-gate 	if (new_rootdev == (dev_t)NODEV) {
2092*0Sstevel@tonic-gate 		return (ENODEV);
2093*0Sstevel@tonic-gate 	}
2094*0Sstevel@tonic-gate 
2095*0Sstevel@tonic-gate 	new_rootvp = makespecvp(new_rootdev, VBLK);
2096*0Sstevel@tonic-gate 
2097*0Sstevel@tonic-gate 	error = VOP_OPEN(&new_rootvp,
2098*0Sstevel@tonic-gate 	    (vfsp->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE, CRED());
2099*0Sstevel@tonic-gate 	if (error) {
2100*0Sstevel@tonic-gate 		cmn_err(CE_CONT,
2101*0Sstevel@tonic-gate 		    "Cannot open mirrored root device, error %d\n", error);
2102*0Sstevel@tonic-gate 		return (error);
2103*0Sstevel@tonic-gate 	}
2104*0Sstevel@tonic-gate 
2105*0Sstevel@tonic-gate 	if (vfs_lock(vfsp) != 0) {
2106*0Sstevel@tonic-gate 		return (EBUSY);
2107*0Sstevel@tonic-gate 	}
2108*0Sstevel@tonic-gate 
2109*0Sstevel@tonic-gate 	ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
2110*0Sstevel@tonic-gate 	ulp = &ufsvfsp->vfs_ulockfs;
2111*0Sstevel@tonic-gate 
2112*0Sstevel@tonic-gate 	mutex_enter(&ulp->ul_lock);
2113*0Sstevel@tonic-gate 
2114*0Sstevel@tonic-gate 	(void) ufs_quiesce(ulp);
2115*0Sstevel@tonic-gate 	(void) ufs_flush(vfsp);
2116*0Sstevel@tonic-gate 
2117*0Sstevel@tonic-gate 	/*
2118*0Sstevel@tonic-gate 	 * Convert root vfs to new dev_t, including vfs hash
2119*0Sstevel@tonic-gate 	 * table and fs id.
2120*0Sstevel@tonic-gate 	 */
2121*0Sstevel@tonic-gate 	vfs_root_redev(vfsp, new_rootdev, ufsfstype);
2122*0Sstevel@tonic-gate 
2123*0Sstevel@tonic-gate 	ufsvfsp->vfs_devvp = new_rootvp;
2124*0Sstevel@tonic-gate 	ufsvfsp->vfs_dev = new_rootdev;
2125*0Sstevel@tonic-gate 
2126*0Sstevel@tonic-gate 	bp = ufsvfsp->vfs_bufp;
2127*0Sstevel@tonic-gate 	bp->b_edev = new_rootdev;
2128*0Sstevel@tonic-gate 	bp->b_dev = cmpdev(new_rootdev);
2129*0Sstevel@tonic-gate 
2130*0Sstevel@tonic-gate 	/*
2131*0Sstevel@tonic-gate 	 * The buffer for the root inode does not contain a valid b_vp
2132*0Sstevel@tonic-gate 	 */
2133*0Sstevel@tonic-gate 	(void) bfinval(new_rootdev, 0);
2134*0Sstevel@tonic-gate 
2135*0Sstevel@tonic-gate 	/*
2136*0Sstevel@tonic-gate 	 * Here we hand-craft inodes with old root device
2137*0Sstevel@tonic-gate 	 * references to refer to the new device instead.
2138*0Sstevel@tonic-gate 	 */
2139*0Sstevel@tonic-gate 	mutex_enter(&ufs_scan_lock);
2140*0Sstevel@tonic-gate 
2141*0Sstevel@tonic-gate 	for (i = 0, ih = ihead; i < inohsz; i++, ih++) {
2142*0Sstevel@tonic-gate 		mutex_enter(&ih_lock[i]);
2143*0Sstevel@tonic-gate 		for (ip = ih->ih_chain[0];
2144*0Sstevel@tonic-gate 				ip != (struct inode *)ih;
2145*0Sstevel@tonic-gate 				ip = ip->i_forw) {
2146*0Sstevel@tonic-gate 			if (ip->i_ufsvfs != ufsvfsp)
2147*0Sstevel@tonic-gate 				continue;
2148*0Sstevel@tonic-gate 			if (ip == ufsvfsp->vfs_qinod)
2149*0Sstevel@tonic-gate 				continue;
2150*0Sstevel@tonic-gate 			if (ip->i_dev == old_rootdev) {
2151*0Sstevel@tonic-gate 				ip->i_dev = new_rootdev;
2152*0Sstevel@tonic-gate 			}
2153*0Sstevel@tonic-gate 
2154*0Sstevel@tonic-gate 			if (ip->i_devvp == old_rootvp) {
2155*0Sstevel@tonic-gate 				ip->i_devvp = new_rootvp;
2156*0Sstevel@tonic-gate 			}
2157*0Sstevel@tonic-gate 		}
2158*0Sstevel@tonic-gate 		mutex_exit(&ih_lock[i]);
2159*0Sstevel@tonic-gate 	}
2160*0Sstevel@tonic-gate 
2161*0Sstevel@tonic-gate 	mutex_exit(&ufs_scan_lock);
2162*0Sstevel@tonic-gate 
2163*0Sstevel@tonic-gate 	/*
2164*0Sstevel@tonic-gate 	 * Make Sure logging structures are using the new device
2165*0Sstevel@tonic-gate 	 * if logging is enabled.  Also start any logging thread that
2166*0Sstevel@tonic-gate 	 * needs to write to the device and couldn't earlier.
2167*0Sstevel@tonic-gate 	 */
2168*0Sstevel@tonic-gate 	if (ufsvfsp->vfs_log) {
2169*0Sstevel@tonic-gate 		buf_t		*bp, *tbp;
2170*0Sstevel@tonic-gate 		ml_unit_t	*ul = ufsvfsp->vfs_log;
2171*0Sstevel@tonic-gate 		struct fs	*fsp = ufsvfsp->vfs_fs;
2172*0Sstevel@tonic-gate 
2173*0Sstevel@tonic-gate 		/*
2174*0Sstevel@tonic-gate 		 * Update the main logging structure.
2175*0Sstevel@tonic-gate 		 */
2176*0Sstevel@tonic-gate 		ul->un_dev = new_rootdev;
2177*0Sstevel@tonic-gate 
2178*0Sstevel@tonic-gate 		/*
2179*0Sstevel@tonic-gate 		 * Get a new bp for the on disk structures.
2180*0Sstevel@tonic-gate 		 */
2181*0Sstevel@tonic-gate 		bp = ul->un_bp;
2182*0Sstevel@tonic-gate 		tbp = ngeteblk(dbtob(LS_SECTORS));
2183*0Sstevel@tonic-gate 		tbp->b_edev = new_rootdev;
2184*0Sstevel@tonic-gate 		tbp->b_dev = cmpdev(new_rootdev);
2185*0Sstevel@tonic-gate 		tbp->b_blkno = bp->b_blkno;
2186*0Sstevel@tonic-gate 		bcopy(bp->b_un.b_addr, tbp->b_un.b_addr, DEV_BSIZE);
2187*0Sstevel@tonic-gate 		bcopy(bp->b_un.b_addr, tbp->b_un.b_addr + DEV_BSIZE, DEV_BSIZE);
2188*0Sstevel@tonic-gate 		bp->b_flags |= (B_STALE | B_AGE);
2189*0Sstevel@tonic-gate 		brelse(bp);
2190*0Sstevel@tonic-gate 		ul->un_bp = tbp;
2191*0Sstevel@tonic-gate 
2192*0Sstevel@tonic-gate 		/*
2193*0Sstevel@tonic-gate 		 * Allocate new circular buffers.
2194*0Sstevel@tonic-gate 		 */
2195*0Sstevel@tonic-gate 		alloc_rdbuf(&ul->un_rdbuf, MAPBLOCKSIZE, MAPBLOCKSIZE);
2196*0Sstevel@tonic-gate 		alloc_wrbuf(&ul->un_wrbuf, ldl_bufsize(ul));
2197*0Sstevel@tonic-gate 
2198*0Sstevel@tonic-gate 		/*
2199*0Sstevel@tonic-gate 		 * Clear the noroll bit which indicates that logging
2200*0Sstevel@tonic-gate 		 * can't roll the log yet.
2201*0Sstevel@tonic-gate 		 */
2202*0Sstevel@tonic-gate 		ASSERT(ul->un_flags & LDL_NOROLL);
2203*0Sstevel@tonic-gate 		ul->un_flags &= ~LDL_NOROLL;
2204*0Sstevel@tonic-gate 
2205*0Sstevel@tonic-gate 		/*
2206*0Sstevel@tonic-gate 		 * Start the logmap roll thread.
2207*0Sstevel@tonic-gate 		 */
2208*0Sstevel@tonic-gate 		logmap_start_roll(ul);
2209*0Sstevel@tonic-gate 
2210*0Sstevel@tonic-gate 		/*
2211*0Sstevel@tonic-gate 		 * Start the reclaim thread if needed.
2212*0Sstevel@tonic-gate 		 */
2213*0Sstevel@tonic-gate 		if (!fsp->fs_ronly && (fsp->fs_reclaim &
2214*0Sstevel@tonic-gate 			(FS_RECLAIM|FS_RECLAIMING))) {
2215*0Sstevel@tonic-gate 			fsp->fs_reclaim &= ~FS_RECLAIM;
2216*0Sstevel@tonic-gate 			fsp->fs_reclaim |= FS_RECLAIMING;
2217*0Sstevel@tonic-gate 			ufs_thread_start(&ufsvfsp->vfs_reclaim,
2218*0Sstevel@tonic-gate 				ufs_thread_reclaim, vfsp);
2219*0Sstevel@tonic-gate 			TRANS_SBWRITE(ufsvfsp, TOP_SBUPDATE_UPDATE);
2220*0Sstevel@tonic-gate 			if (sberror = geterror(ufsvfsp->vfs_bufp)) {
2221*0Sstevel@tonic-gate 				refstr_t	*mntpt;
2222*0Sstevel@tonic-gate 				mntpt = vfs_getmntpoint(vfsp);
2223*0Sstevel@tonic-gate 				cmn_err(CE_WARN,
2224*0Sstevel@tonic-gate 					"Remountroot failed to update Reclaim"
2225*0Sstevel@tonic-gate 					"state for filesystem %s "
2226*0Sstevel@tonic-gate 					"Error writing SuperBlock %d",
2227*0Sstevel@tonic-gate 					refstr_value(mntpt), error);
2228*0Sstevel@tonic-gate 				refstr_rele(mntpt);
2229*0Sstevel@tonic-gate 			}
2230*0Sstevel@tonic-gate 		}
2231*0Sstevel@tonic-gate 	}
2232*0Sstevel@tonic-gate 
2233*0Sstevel@tonic-gate 	rootdev = new_rootdev;
2234*0Sstevel@tonic-gate 	rootvp = new_rootvp;
2235*0Sstevel@tonic-gate 
2236*0Sstevel@tonic-gate 	cv_broadcast(&ulp->ul_cv);
2237*0Sstevel@tonic-gate 	mutex_exit(&ulp->ul_lock);
2238*0Sstevel@tonic-gate 
2239*0Sstevel@tonic-gate 	vfs_unlock(vfsp);
2240*0Sstevel@tonic-gate 
2241*0Sstevel@tonic-gate 	error = VOP_CLOSE(old_rootvp, FREAD, 1, (offset_t)0, CRED());
2242*0Sstevel@tonic-gate 	if (error) {
2243*0Sstevel@tonic-gate 		cmn_err(CE_CONT,
2244*0Sstevel@tonic-gate 		    "close of root device component failed, error %d\n",
2245*0Sstevel@tonic-gate 		    error);
2246*0Sstevel@tonic-gate 	}
2247*0Sstevel@tonic-gate 	VN_RELE(old_rootvp);
2248*0Sstevel@tonic-gate 
2249*0Sstevel@tonic-gate 	return (sberror ? sberror : error);
2250*0Sstevel@tonic-gate }
2251*0Sstevel@tonic-gate 
2252*0Sstevel@tonic-gate #endif	/* __sparc */
2253