xref: /onnv-gate/usr/src/uts/common/fs/ufs/ufs_vfsops.c (revision 11066:cebb50cbe4f9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51488Srsb  * Common Development and Distribution License (the "License").
61488Srsb  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
2210655SFrank.Batschulat@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  * The Regents of the University of California
320Sstevel@tonic-gate  * All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  * contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <sys/t_lock.h>
410Sstevel@tonic-gate #include <sys/param.h>
420Sstevel@tonic-gate #include <sys/systm.h>
430Sstevel@tonic-gate #include <sys/bitmap.h>
440Sstevel@tonic-gate #include <sys/sysmacros.h>
450Sstevel@tonic-gate #include <sys/kmem.h>
460Sstevel@tonic-gate #include <sys/signal.h>
470Sstevel@tonic-gate #include <sys/user.h>
480Sstevel@tonic-gate #include <sys/proc.h>
490Sstevel@tonic-gate #include <sys/disp.h>
500Sstevel@tonic-gate #include <sys/buf.h>
510Sstevel@tonic-gate #include <sys/pathname.h>
520Sstevel@tonic-gate #include <sys/vfs.h>
533898Srsb #include <sys/vfs_opreg.h>
540Sstevel@tonic-gate #include <sys/vnode.h>
550Sstevel@tonic-gate #include <sys/file.h>
56329Saguzovsk #include <sys/atomic.h>
570Sstevel@tonic-gate #include <sys/uio.h>
580Sstevel@tonic-gate #include <sys/dkio.h>
590Sstevel@tonic-gate #include <sys/cred.h>
600Sstevel@tonic-gate #include <sys/conf.h>
610Sstevel@tonic-gate #include <sys/dnlc.h>
620Sstevel@tonic-gate #include <sys/kstat.h>
630Sstevel@tonic-gate #include <sys/acl.h>
640Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
650Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
660Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
670Sstevel@tonic-gate #include <sys/fs/ufs_mount.h>
680Sstevel@tonic-gate #include <sys/fs/ufs_acl.h>
690Sstevel@tonic-gate #include <sys/fs/ufs_panic.h>
700Sstevel@tonic-gate #include <sys/fs/ufs_bio.h>
710Sstevel@tonic-gate #include <sys/fs/ufs_quota.h>
720Sstevel@tonic-gate #include <sys/fs/ufs_log.h>
730Sstevel@tonic-gate #undef NFS
740Sstevel@tonic-gate #include <sys/statvfs.h>
750Sstevel@tonic-gate #include <sys/mount.h>
760Sstevel@tonic-gate #include <sys/mntent.h>
770Sstevel@tonic-gate #include <sys/swap.h>
780Sstevel@tonic-gate #include <sys/errno.h>
790Sstevel@tonic-gate #include <sys/debug.h>
800Sstevel@tonic-gate #include "fs/fs_subr.h"
810Sstevel@tonic-gate #include <sys/cmn_err.h>
820Sstevel@tonic-gate #include <sys/dnlc.h>
830Sstevel@tonic-gate #include <sys/fssnap_if.h>
840Sstevel@tonic-gate #include <sys/sunddi.h>
850Sstevel@tonic-gate #include <sys/bootconf.h>
860Sstevel@tonic-gate #include <sys/policy.h>
870Sstevel@tonic-gate #include <sys/zone.h>
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * This is the loadable module wrapper.
910Sstevel@tonic-gate  */
920Sstevel@tonic-gate #include <sys/modctl.h>
930Sstevel@tonic-gate 
940Sstevel@tonic-gate int			ufsfstype;
950Sstevel@tonic-gate vfsops_t		*ufs_vfsops;
960Sstevel@tonic-gate static int		ufsinit(int, char *);
970Sstevel@tonic-gate static int		mountfs();
980Sstevel@tonic-gate extern int		highbit();
990Sstevel@tonic-gate extern struct instats	ins;
1000Sstevel@tonic-gate extern struct vnode *common_specvp(struct vnode *vp);
1010Sstevel@tonic-gate extern vfs_t		EIO_vfs;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate struct  dquot *dquot, *dquotNDQUOT;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  * Cylinder group summary information handling tunable.
1070Sstevel@tonic-gate  * This defines when these deltas get logged.
1080Sstevel@tonic-gate  * If the number of cylinders in the file system is over the
1090Sstevel@tonic-gate  * tunable then we log csum updates. Otherwise the updates are only
1100Sstevel@tonic-gate  * done for performance on unmount. After a panic they can be
1110Sstevel@tonic-gate  * quickly constructed during mounting. See ufs_construct_si()
1120Sstevel@tonic-gate  * called from ufs_getsummaryinfo().
1130Sstevel@tonic-gate  *
1140Sstevel@tonic-gate  * This performance feature can of course be disabled by setting
1150Sstevel@tonic-gate  * ufs_ncg_log to 0, and fully enabled by setting it to 0xffffffff.
1160Sstevel@tonic-gate  */
1170Sstevel@tonic-gate #define	UFS_LOG_NCG_DEFAULT 10000
1180Sstevel@tonic-gate uint32_t ufs_ncg_log = UFS_LOG_NCG_DEFAULT;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate  * ufs_clean_root indicates whether the root fs went down cleanly
1220Sstevel@tonic-gate  */
1230Sstevel@tonic-gate static int ufs_clean_root = 0;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate  * UFS Mount options table
1270Sstevel@tonic-gate  */
1280Sstevel@tonic-gate static char *intr_cancel[] = { MNTOPT_NOINTR, NULL };
1290Sstevel@tonic-gate static char *nointr_cancel[] = { MNTOPT_INTR, NULL };
1300Sstevel@tonic-gate static char *forcedirectio_cancel[] = { MNTOPT_NOFORCEDIRECTIO, NULL };
1310Sstevel@tonic-gate static char *noforcedirectio_cancel[] = { MNTOPT_FORCEDIRECTIO, NULL };
1320Sstevel@tonic-gate static char *largefiles_cancel[] = { MNTOPT_NOLARGEFILES, NULL };
1330Sstevel@tonic-gate static char *nolargefiles_cancel[] = { MNTOPT_LARGEFILES, NULL };
1340Sstevel@tonic-gate static char *logging_cancel[] = { MNTOPT_NOLOGGING, NULL };
1350Sstevel@tonic-gate static char *nologging_cancel[] = { MNTOPT_LOGGING, NULL };
1360Sstevel@tonic-gate static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
1370Sstevel@tonic-gate static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
1380Sstevel@tonic-gate static char *quota_cancel[] = { MNTOPT_NOQUOTA, NULL };
1390Sstevel@tonic-gate static char *noquota_cancel[] = { MNTOPT_QUOTA, NULL };
1400Sstevel@tonic-gate static char *dfratime_cancel[] = { MNTOPT_NODFRATIME, NULL };
1410Sstevel@tonic-gate static char *nodfratime_cancel[] = { MNTOPT_DFRATIME, NULL };
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate static mntopt_t mntopts[] = {
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate  *	option name		cancel option	default arg	flags
1460Sstevel@tonic-gate  *		ufs arg flag
1470Sstevel@tonic-gate  */
1480Sstevel@tonic-gate 	{ MNTOPT_INTR,		intr_cancel,	NULL,		MO_DEFAULT,
1490Sstevel@tonic-gate 		(void *)0 },
1500Sstevel@tonic-gate 	{ MNTOPT_NOINTR,	nointr_cancel,	NULL,		0,
1510Sstevel@tonic-gate 		(void *)UFSMNT_NOINTR },
1520Sstevel@tonic-gate 	{ MNTOPT_SYNCDIR,	NULL,		NULL,		0,
1530Sstevel@tonic-gate 		(void *)UFSMNT_SYNCDIR },
1540Sstevel@tonic-gate 	{ MNTOPT_FORCEDIRECTIO,	forcedirectio_cancel, NULL,	0,
1550Sstevel@tonic-gate 		(void *)UFSMNT_FORCEDIRECTIO },
1560Sstevel@tonic-gate 	{ MNTOPT_NOFORCEDIRECTIO, noforcedirectio_cancel, NULL, 0,
1570Sstevel@tonic-gate 		(void *)UFSMNT_NOFORCEDIRECTIO },
1580Sstevel@tonic-gate 	{ MNTOPT_NOSETSEC,	NULL,		NULL,		0,
1590Sstevel@tonic-gate 		(void *)UFSMNT_NOSETSEC },
1600Sstevel@tonic-gate 	{ MNTOPT_LARGEFILES,	largefiles_cancel, NULL,	MO_DEFAULT,
1610Sstevel@tonic-gate 		(void *)UFSMNT_LARGEFILES },
1620Sstevel@tonic-gate 	{ MNTOPT_NOLARGEFILES,	nolargefiles_cancel, NULL,	0,
1630Sstevel@tonic-gate 		(void *)0 },
1640Sstevel@tonic-gate 	{ MNTOPT_LOGGING,	logging_cancel, NULL,		MO_TAG,
1650Sstevel@tonic-gate 		(void *)UFSMNT_LOGGING },
1660Sstevel@tonic-gate 	{ MNTOPT_NOLOGGING,	nologging_cancel, NULL,
1670Sstevel@tonic-gate 		MO_NODISPLAY|MO_DEFAULT|MO_TAG, (void *)0 },
1680Sstevel@tonic-gate 	{ MNTOPT_QUOTA,		quota_cancel, NULL,		MO_IGNORE,
1690Sstevel@tonic-gate 		(void *)0 },
1700Sstevel@tonic-gate 	{ MNTOPT_NOQUOTA,	noquota_cancel,	NULL,
1710Sstevel@tonic-gate 		MO_NODISPLAY|MO_DEFAULT, (void *)0 },
1720Sstevel@tonic-gate 	{ MNTOPT_GLOBAL,	NULL,		NULL,		0,
1730Sstevel@tonic-gate 		(void *)0 },
1740Sstevel@tonic-gate 	{ MNTOPT_XATTR,	xattr_cancel,		NULL,		MO_DEFAULT,
1750Sstevel@tonic-gate 		(void *)0 },
1760Sstevel@tonic-gate 	{ MNTOPT_NOXATTR,	noxattr_cancel,		NULL,		0,
1770Sstevel@tonic-gate 		(void *)0 },
1780Sstevel@tonic-gate 	{ MNTOPT_NOATIME,	NULL,		NULL,		0,
1790Sstevel@tonic-gate 		(void *)UFSMNT_NOATIME },
1800Sstevel@tonic-gate 	{ MNTOPT_DFRATIME,	dfratime_cancel, NULL,		0,
1810Sstevel@tonic-gate 		(void *)0 },
1820Sstevel@tonic-gate 	{ MNTOPT_NODFRATIME,	nodfratime_cancel, NULL,
1830Sstevel@tonic-gate 		MO_NODISPLAY|MO_DEFAULT, (void *)UFSMNT_NODFRATIME },
1840Sstevel@tonic-gate 	{ MNTOPT_ONERROR,	NULL,		UFSMNT_ONERROR_PANIC_STR,
1850Sstevel@tonic-gate 		MO_DEFAULT|MO_HASVALUE,	(void *)0 },
1860Sstevel@tonic-gate };
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate static mntopts_t ufs_mntopts = {
1890Sstevel@tonic-gate 	sizeof (mntopts) / sizeof (mntopt_t),
1900Sstevel@tonic-gate 	mntopts
1910Sstevel@tonic-gate };
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate static vfsdef_t vfw = {
1940Sstevel@tonic-gate 	VFSDEF_VERSION,
1950Sstevel@tonic-gate 	"ufs",
1960Sstevel@tonic-gate 	ufsinit,
1976855Sjohnlev 	VSW_HASPROTO|VSW_CANREMOUNT|VSW_STATS|VSW_CANLOFI,
1980Sstevel@tonic-gate 	&ufs_mntopts
1990Sstevel@tonic-gate };
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate  * Module linkage information for the kernel.
2030Sstevel@tonic-gate  */
2040Sstevel@tonic-gate extern struct mod_ops mod_fsops;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate static struct modlfs modlfs = {
2070Sstevel@tonic-gate 	&mod_fsops, "filesystem for ufs", &vfw
2080Sstevel@tonic-gate };
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate static struct modlinkage modlinkage = {
2110Sstevel@tonic-gate 	MODREV_1, (void *)&modlfs, NULL
2120Sstevel@tonic-gate };
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate  * An attempt has been made to make this module unloadable.  In order to
2160Sstevel@tonic-gate  * test it, we need a system in which the root fs is NOT ufs.  THIS HAS NOT
2170Sstevel@tonic-gate  * BEEN DONE
2180Sstevel@tonic-gate  */
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate extern kstat_t *ufs_inode_kstat;
2210Sstevel@tonic-gate extern uint_t ufs_lockfs_key;
2220Sstevel@tonic-gate extern void ufs_lockfs_tsd_destructor(void *);
2230Sstevel@tonic-gate extern uint_t bypass_snapshot_throttle_key;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate int
_init(void)2260Sstevel@tonic-gate _init(void)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate 	/*
2290Sstevel@tonic-gate 	 * Create an index into the per thread array so that any thread doing
2300Sstevel@tonic-gate 	 * VOP will have a lockfs mark on it.
2310Sstevel@tonic-gate 	 */
2320Sstevel@tonic-gate 	tsd_create(&ufs_lockfs_key, ufs_lockfs_tsd_destructor);
2330Sstevel@tonic-gate 	tsd_create(&bypass_snapshot_throttle_key, NULL);
2340Sstevel@tonic-gate 	return (mod_install(&modlinkage));
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate int
_fini(void)2380Sstevel@tonic-gate _fini(void)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate 	return (EBUSY);
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2440Sstevel@tonic-gate _info(struct modinfo *modinfop)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate extern struct vnode *makespecvp(dev_t dev, vtype_t type);
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate extern kmutex_t	ufs_scan_lock;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate static int mountfs(struct vfs *, enum whymountroot, struct vnode *, char *,
2540Sstevel@tonic-gate 		struct cred *, int, void *, int);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate static int
ufs_mount(struct vfs * vfsp,struct vnode * mvp,struct mounta * uap,struct cred * cr)2580Sstevel@tonic-gate ufs_mount(struct vfs *vfsp, struct vnode *mvp, struct mounta *uap,
2590Sstevel@tonic-gate 	struct cred *cr)
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate {
2620Sstevel@tonic-gate 	char *data = uap->dataptr;
2630Sstevel@tonic-gate 	int datalen = uap->datalen;
2640Sstevel@tonic-gate 	dev_t dev;
2656734Sjohnlev 	struct vnode *lvp = NULL;
2666734Sjohnlev 	struct vnode *svp = NULL;
2670Sstevel@tonic-gate 	struct pathname dpn;
2680Sstevel@tonic-gate 	int error;
2690Sstevel@tonic-gate 	enum whymountroot why = ROOT_INIT;
2700Sstevel@tonic-gate 	struct ufs_args args;
2710Sstevel@tonic-gate 	int oflag, aflag;
2720Sstevel@tonic-gate 	int fromspace = (uap->flags & MS_SYSSPACE) ?
2730Sstevel@tonic-gate 	    UIO_SYSSPACE : UIO_USERSPACE;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
2760Sstevel@tonic-gate 		return (error);
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	if (mvp->v_type != VDIR)
2790Sstevel@tonic-gate 		return (ENOTDIR);
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	mutex_enter(&mvp->v_lock);
2820Sstevel@tonic-gate 	if ((uap->flags & MS_REMOUNT) == 0 &&
2830Sstevel@tonic-gate 	    (uap->flags & MS_OVERLAY) == 0 &&
2840Sstevel@tonic-gate 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
2850Sstevel@tonic-gate 		mutex_exit(&mvp->v_lock);
2860Sstevel@tonic-gate 		return (EBUSY);
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 	mutex_exit(&mvp->v_lock);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	/*
2910Sstevel@tonic-gate 	 * Get arguments
2920Sstevel@tonic-gate 	 */
2930Sstevel@tonic-gate 	bzero(&args, sizeof (args));
2940Sstevel@tonic-gate 	if ((uap->flags & MS_DATA) && data != NULL && datalen != 0) {
2950Sstevel@tonic-gate 		int copy_result = 0;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 		if (datalen > sizeof (args))
2980Sstevel@tonic-gate 			return (EINVAL);
2990Sstevel@tonic-gate 		if (uap->flags & MS_SYSSPACE)
3000Sstevel@tonic-gate 			bcopy(data, &args, datalen);
3010Sstevel@tonic-gate 		else
3020Sstevel@tonic-gate 			copy_result = copyin(data, &args, datalen);
3030Sstevel@tonic-gate 		if (copy_result)
3040Sstevel@tonic-gate 			return (EFAULT);
3050Sstevel@tonic-gate 		datalen = sizeof (struct ufs_args);
3060Sstevel@tonic-gate 	} else {
3070Sstevel@tonic-gate 		datalen = 0;
3080Sstevel@tonic-gate 	}
3096734Sjohnlev 
3106734Sjohnlev 	if ((vfsp->vfs_flag & VFS_RDONLY) != 0 ||
3116734Sjohnlev 	    (uap->flags & MS_RDONLY) != 0) {
3126734Sjohnlev 		oflag = FREAD;
3136734Sjohnlev 		aflag = VREAD;
3146734Sjohnlev 	} else {
3156734Sjohnlev 		oflag = FREAD | FWRITE;
3166734Sjohnlev 		aflag = VREAD | VWRITE;
3176734Sjohnlev 	}
3186734Sjohnlev 
3190Sstevel@tonic-gate 	/*
3200Sstevel@tonic-gate 	 * Read in the mount point pathname
3210Sstevel@tonic-gate 	 * (so we can record the directory the file system was last mounted on).
3220Sstevel@tonic-gate 	 */
3230Sstevel@tonic-gate 	if (error = pn_get(uap->dir, fromspace, &dpn))
3240Sstevel@tonic-gate 		return (error);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	/*
3270Sstevel@tonic-gate 	 * Resolve path name of special file being mounted.
3280Sstevel@tonic-gate 	 */
3296734Sjohnlev 	if (error = lookupname(uap->spec, fromspace, FOLLOW, NULL, &svp)) {
3300Sstevel@tonic-gate 		pn_free(&dpn);
3310Sstevel@tonic-gate 		return (error);
3320Sstevel@tonic-gate 	}
3336734Sjohnlev 
3346734Sjohnlev 	error = vfs_get_lofi(vfsp, &lvp);
3356734Sjohnlev 
3366734Sjohnlev 	if (error > 0) {
3376734Sjohnlev 		VN_RELE(svp);
3380Sstevel@tonic-gate 		pn_free(&dpn);
3396734Sjohnlev 		return (error);
3406734Sjohnlev 	} else if (error == 0) {
3416734Sjohnlev 		dev = lvp->v_rdev;
3426734Sjohnlev 
3436734Sjohnlev 		if (getmajor(dev) >= devcnt) {
3446734Sjohnlev 			error = ENXIO;
3456734Sjohnlev 			goto out;
3466734Sjohnlev 		}
3476734Sjohnlev 	} else {
3486734Sjohnlev 		dev = svp->v_rdev;
3496734Sjohnlev 
3506734Sjohnlev 		if (svp->v_type != VBLK) {
3516734Sjohnlev 			VN_RELE(svp);
3526734Sjohnlev 			pn_free(&dpn);
3536734Sjohnlev 			return (ENOTBLK);
3546734Sjohnlev 		}
3556734Sjohnlev 
3566734Sjohnlev 		if (getmajor(dev) >= devcnt) {
3576734Sjohnlev 			error = ENXIO;
3586734Sjohnlev 			goto out;
3596734Sjohnlev 		}
3606734Sjohnlev 
3616734Sjohnlev 		/*
3626734Sjohnlev 		 * In SunCluster, requests to a global device are
3636734Sjohnlev 		 * satisfied by a local device. We substitute the global
3646734Sjohnlev 		 * pxfs node with a local spec node here.
3656734Sjohnlev 		 */
3666734Sjohnlev 		if (IS_PXFSVP(svp)) {
3676734Sjohnlev 			ASSERT(lvp == NULL);
3686734Sjohnlev 			VN_RELE(svp);
3696734Sjohnlev 			svp = makespecvp(dev, VBLK);
3706734Sjohnlev 		}
3716734Sjohnlev 
3726734Sjohnlev 		if ((error = secpolicy_spec_open(cr, svp, oflag)) != 0) {
3736734Sjohnlev 			VN_RELE(svp);
3746734Sjohnlev 			pn_free(&dpn);
3756734Sjohnlev 			return (error);
3766734Sjohnlev 		}
3770Sstevel@tonic-gate 	}
3786734Sjohnlev 
3790Sstevel@tonic-gate 	if (uap->flags & MS_REMOUNT)
3800Sstevel@tonic-gate 		why = ROOT_REMOUNT;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	/*
3836734Sjohnlev 	 * Open device/file mounted on.  We need this to check whether
3846734Sjohnlev 	 * the caller has sufficient rights to access the resource in
3856734Sjohnlev 	 * question.  When bio is fixed for vnodes this can all be vnode
3860Sstevel@tonic-gate 	 * operations.
3870Sstevel@tonic-gate 	 */
3886734Sjohnlev 	if ((error = VOP_ACCESS(svp, aflag, 0, cr, NULL)) != 0)
3896734Sjohnlev 		goto out;
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	/*
3920Sstevel@tonic-gate 	 * Ensure that this device isn't already mounted or in progress on a
3930Sstevel@tonic-gate 	 * mount unless this is a REMOUNT request or we are told to suppress
3940Sstevel@tonic-gate 	 * mount checks. Global mounts require special handling.
3950Sstevel@tonic-gate 	 */
3960Sstevel@tonic-gate 	if ((uap->flags & MS_NOCHECK) == 0) {
3970Sstevel@tonic-gate 		if ((uap->flags & MS_GLOBAL) == 0 &&
3980Sstevel@tonic-gate 		    vfs_devmounting(dev, vfsp)) {
3996734Sjohnlev 			error = EBUSY;
4006734Sjohnlev 			goto out;
4010Sstevel@tonic-gate 		}
4020Sstevel@tonic-gate 		if (vfs_devismounted(dev)) {
4030Sstevel@tonic-gate 			if ((uap->flags & MS_REMOUNT) == 0) {
4046734Sjohnlev 				error = EBUSY;
4056734Sjohnlev 				goto out;
4060Sstevel@tonic-gate 			}
4070Sstevel@tonic-gate 		}
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	/*
4110Sstevel@tonic-gate 	 * If the device is a tape, mount it read only
4120Sstevel@tonic-gate 	 */
4130Sstevel@tonic-gate 	if (devopsp[getmajor(dev)]->devo_cb_ops->cb_flag & D_TAPE) {
4140Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_RDONLY;
4150Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 	if (uap->flags & MS_RDONLY)
4180Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_RDONLY;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	/*
4210Sstevel@tonic-gate 	 * Mount the filesystem, free the device vnode on error.
4220Sstevel@tonic-gate 	 */
4236734Sjohnlev 	error = mountfs(vfsp, why, lvp != NULL ? lvp : svp,
4246734Sjohnlev 	    dpn.pn_path, cr, 0, &args, datalen);
4256734Sjohnlev 
4266734Sjohnlev 	if (error == 0) {
4277757SJanice.Chang@Sun.COM 		vfs_set_feature(vfsp, VFSFT_SYSATTR_VIEWS);
4286734Sjohnlev 
4296734Sjohnlev 		/*
4306734Sjohnlev 		 * If lofi, drop our reference to the original file.
4316734Sjohnlev 		 */
4326734Sjohnlev 		if (lvp != NULL)
4336734Sjohnlev 			VN_RELE(svp);
4346734Sjohnlev 	}
4356734Sjohnlev 
4366734Sjohnlev out:
4370Sstevel@tonic-gate 	pn_free(&dpn);
4386734Sjohnlev 
4390Sstevel@tonic-gate 	if (error) {
4406734Sjohnlev 		if (lvp != NULL)
4416734Sjohnlev 			VN_RELE(lvp);
4426734Sjohnlev 		if (svp != NULL)
4436734Sjohnlev 			VN_RELE(svp);
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 	return (error);
4460Sstevel@tonic-gate }
4476734Sjohnlev 
4480Sstevel@tonic-gate /*
4490Sstevel@tonic-gate  * Mount root file system.
4500Sstevel@tonic-gate  * "why" is ROOT_INIT on initial call ROOT_REMOUNT if called to
4510Sstevel@tonic-gate  * remount the root file system, and ROOT_UNMOUNT if called to
4520Sstevel@tonic-gate  * unmount the root (e.g., as part of a system shutdown).
4530Sstevel@tonic-gate  *
4540Sstevel@tonic-gate  * XXX - this may be partially machine-dependent; it, along with the VFS_SWAPVP
4550Sstevel@tonic-gate  * operation, goes along with auto-configuration.  A mechanism should be
4560Sstevel@tonic-gate  * provided by which machine-INdependent code in the kernel can say "get me the
4570Sstevel@tonic-gate  * right root file system" and "get me the right initial swap area", and have
4580Sstevel@tonic-gate  * that done in what may well be a machine-dependent fashion.
4590Sstevel@tonic-gate  * Unfortunately, it is also file-system-type dependent (NFS gets it via
4600Sstevel@tonic-gate  * bootparams calls, UFS gets it from various and sundry machine-dependent
4610Sstevel@tonic-gate  * mechanisms, as SPECFS does for swap).
4620Sstevel@tonic-gate  */
4630Sstevel@tonic-gate static int
ufs_mountroot(struct vfs * vfsp,enum whymountroot why)4640Sstevel@tonic-gate ufs_mountroot(struct vfs *vfsp, enum whymountroot why)
4650Sstevel@tonic-gate {
4660Sstevel@tonic-gate 	struct fs *fsp;
4670Sstevel@tonic-gate 	int error;
4680Sstevel@tonic-gate 	static int ufsrootdone = 0;
4690Sstevel@tonic-gate 	dev_t rootdev;
4700Sstevel@tonic-gate 	struct vnode *vp;
4710Sstevel@tonic-gate 	struct vnode *devvp = 0;
4720Sstevel@tonic-gate 	int ovflags;
4730Sstevel@tonic-gate 	int doclkset;
4740Sstevel@tonic-gate 	ufsvfs_t *ufsvfsp;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	if (why == ROOT_INIT) {
4770Sstevel@tonic-gate 		if (ufsrootdone++)
4780Sstevel@tonic-gate 			return (EBUSY);
4790Sstevel@tonic-gate 		rootdev = getrootdev();
4800Sstevel@tonic-gate 		if (rootdev == (dev_t)NODEV)
4810Sstevel@tonic-gate 			return (ENODEV);
4820Sstevel@tonic-gate 		vfsp->vfs_dev = rootdev;
4830Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_RDONLY;
4840Sstevel@tonic-gate 	} else if (why == ROOT_REMOUNT) {
4850Sstevel@tonic-gate 		vp = ((struct ufsvfs *)vfsp->vfs_data)->vfs_devvp;
4860Sstevel@tonic-gate 		(void) dnlc_purge_vfsp(vfsp, 0);
4870Sstevel@tonic-gate 		vp = common_specvp(vp);
4885331Samw 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_INVAL,
4895331Samw 		    CRED(), NULL);
4900Sstevel@tonic-gate 		(void) bfinval(vfsp->vfs_dev, 0);
4910Sstevel@tonic-gate 		fsp = getfs(vfsp);
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 		ovflags = vfsp->vfs_flag;
4940Sstevel@tonic-gate 		vfsp->vfs_flag &= ~VFS_RDONLY;
4950Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_REMOUNT;
4960Sstevel@tonic-gate 		rootdev = vfsp->vfs_dev;
4970Sstevel@tonic-gate 	} else if (why == ROOT_UNMOUNT) {
4980Sstevel@tonic-gate 		if (vfs_lock(vfsp) == 0) {
4990Sstevel@tonic-gate 			(void) ufs_flush(vfsp);
5000Sstevel@tonic-gate 			/*
5010Sstevel@tonic-gate 			 * Mark the log as fully rolled
5020Sstevel@tonic-gate 			 */
5030Sstevel@tonic-gate 			ufsvfsp = (ufsvfs_t *)vfsp->vfs_data;
5040Sstevel@tonic-gate 			fsp = ufsvfsp->vfs_fs;
5050Sstevel@tonic-gate 			if (TRANS_ISTRANS(ufsvfsp) &&
5060Sstevel@tonic-gate 			    !TRANS_ISERROR(ufsvfsp) &&
5070Sstevel@tonic-gate 			    (fsp->fs_rolled == FS_NEED_ROLL)) {
5080Sstevel@tonic-gate 				ml_unit_t *ul = ufsvfsp->vfs_log;
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 				error = ufs_putsummaryinfo(ul->un_dev,
5110Sstevel@tonic-gate 				    ufsvfsp, fsp);
5120Sstevel@tonic-gate 				if (error == 0) {
5130Sstevel@tonic-gate 					fsp->fs_rolled = FS_ALL_ROLLED;
5140Sstevel@tonic-gate 					UFS_BWRITE2(NULL, ufsvfsp->vfs_bufp);
5150Sstevel@tonic-gate 				}
5160Sstevel@tonic-gate 			}
5170Sstevel@tonic-gate 			vfs_unlock(vfsp);
5180Sstevel@tonic-gate 		} else {
5190Sstevel@tonic-gate 			ufs_update(0);
5200Sstevel@tonic-gate 		}
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 		vp = ((struct ufsvfs *)vfsp->vfs_data)->vfs_devvp;
5230Sstevel@tonic-gate 		(void) VOP_CLOSE(vp, FREAD|FWRITE, 1,
5245331Samw 		    (offset_t)0, CRED(), NULL);
5250Sstevel@tonic-gate 		return (0);
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate 	error = vfs_lock(vfsp);
5280Sstevel@tonic-gate 	if (error)
5290Sstevel@tonic-gate 		return (error);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	devvp = makespecvp(rootdev, VBLK);
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	/* If RO media, don't call clkset() (see below) */
5340Sstevel@tonic-gate 	doclkset = 1;
5350Sstevel@tonic-gate 	if (why == ROOT_INIT) {
5365331Samw 		error = VOP_OPEN(&devvp, FREAD|FWRITE, CRED(), NULL);
5370Sstevel@tonic-gate 		if (error == 0) {
5380Sstevel@tonic-gate 			(void) VOP_CLOSE(devvp, FREAD|FWRITE, 1,
5395331Samw 			    (offset_t)0, CRED(), NULL);
5400Sstevel@tonic-gate 		} else {
5410Sstevel@tonic-gate 			doclkset = 0;
5420Sstevel@tonic-gate 		}
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	error = mountfs(vfsp, why, devvp, "/", CRED(), 1, NULL, 0);
5460Sstevel@tonic-gate 	/*
5470Sstevel@tonic-gate 	 * XXX - assumes root device is not indirect, because we don't set
5480Sstevel@tonic-gate 	 * rootvp.  Is rootvp used for anything?  If so, make another arg
5490Sstevel@tonic-gate 	 * to mountfs.
5500Sstevel@tonic-gate 	 */
5510Sstevel@tonic-gate 	if (error) {
5520Sstevel@tonic-gate 		vfs_unlock(vfsp);
5530Sstevel@tonic-gate 		if (why == ROOT_REMOUNT)
5540Sstevel@tonic-gate 			vfsp->vfs_flag = ovflags;
5550Sstevel@tonic-gate 		if (rootvp) {
5560Sstevel@tonic-gate 			VN_RELE(rootvp);
5570Sstevel@tonic-gate 			rootvp = (struct vnode *)0;
5580Sstevel@tonic-gate 		}
5590Sstevel@tonic-gate 		VN_RELE(devvp);
5600Sstevel@tonic-gate 		return (error);
5610Sstevel@tonic-gate 	}
5620Sstevel@tonic-gate 	if (why == ROOT_INIT)
5630Sstevel@tonic-gate 		vfs_add((struct vnode *)0, vfsp,
5640Sstevel@tonic-gate 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
5650Sstevel@tonic-gate 	vfs_unlock(vfsp);
5660Sstevel@tonic-gate 	fsp = getfs(vfsp);
5670Sstevel@tonic-gate 	clkset(doclkset ? fsp->fs_time : -1);
5680Sstevel@tonic-gate 	ufsvfsp = (ufsvfs_t *)vfsp->vfs_data;
5690Sstevel@tonic-gate 	if (ufsvfsp->vfs_log) {
5700Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_LOGGING, NULL, 0);
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate 	return (0);
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate static int
remountfs(struct vfs * vfsp,dev_t dev,void * raw_argsp,int args_len)5760Sstevel@tonic-gate remountfs(struct vfs *vfsp, dev_t dev, void *raw_argsp, int args_len)
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
5790Sstevel@tonic-gate 	struct ulockfs *ulp = &ufsvfsp->vfs_ulockfs;
5800Sstevel@tonic-gate 	struct buf *bp = ufsvfsp->vfs_bufp;
5810Sstevel@tonic-gate 	struct fs *fsp = (struct fs *)bp->b_un.b_addr;
5820Sstevel@tonic-gate 	struct fs *fspt;
5830Sstevel@tonic-gate 	struct buf *tpt = 0;
5840Sstevel@tonic-gate 	int error = 0;
5850Sstevel@tonic-gate 	int flags = 0;
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	if (args_len == sizeof (struct ufs_args) && raw_argsp)
5880Sstevel@tonic-gate 		flags = ((struct ufs_args *)raw_argsp)->flags;
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	/* cannot remount to RDONLY */
5910Sstevel@tonic-gate 	if (vfsp->vfs_flag & VFS_RDONLY)
5921107Ssjelinek 		return (ENOTSUP);
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	/* whoops, wrong dev */
5950Sstevel@tonic-gate 	if (vfsp->vfs_dev != dev)
5960Sstevel@tonic-gate 		return (EINVAL);
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	/*
5990Sstevel@tonic-gate 	 * synchronize w/ufs ioctls
6000Sstevel@tonic-gate 	 */
6010Sstevel@tonic-gate 	mutex_enter(&ulp->ul_lock);
602329Saguzovsk 	atomic_add_long(&ufs_quiesce_pend, 1);
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	/*
6050Sstevel@tonic-gate 	 * reset options
6060Sstevel@tonic-gate 	 */
6070Sstevel@tonic-gate 	ufsvfsp->vfs_nointr  = flags & UFSMNT_NOINTR;
6080Sstevel@tonic-gate 	ufsvfsp->vfs_syncdir = flags & UFSMNT_SYNCDIR;
6090Sstevel@tonic-gate 	ufsvfsp->vfs_nosetsec = flags & UFSMNT_NOSETSEC;
6100Sstevel@tonic-gate 	ufsvfsp->vfs_noatime = flags & UFSMNT_NOATIME;
6110Sstevel@tonic-gate 	if ((flags & UFSMNT_NODFRATIME) || ufsvfsp->vfs_noatime)
6120Sstevel@tonic-gate 		ufsvfsp->vfs_dfritime &= ~UFS_DFRATIME;
6130Sstevel@tonic-gate 	else	/* dfratime, default behavior */
6140Sstevel@tonic-gate 		ufsvfsp->vfs_dfritime |= UFS_DFRATIME;
6150Sstevel@tonic-gate 	if (flags & UFSMNT_FORCEDIRECTIO)
6160Sstevel@tonic-gate 		ufsvfsp->vfs_forcedirectio = 1;
6170Sstevel@tonic-gate 	else	/* default is no direct I/O */
6180Sstevel@tonic-gate 		ufsvfsp->vfs_forcedirectio = 0;
619*11066Srafael.vanoni@sun.com 	ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	/*
6220Sstevel@tonic-gate 	 * set largefiles flag in ufsvfs equal to the
6230Sstevel@tonic-gate 	 * value passed in by the mount command. If
6240Sstevel@tonic-gate 	 * it is "nolargefiles", and the flag is set
6250Sstevel@tonic-gate 	 * in the superblock, the mount fails.
6260Sstevel@tonic-gate 	 */
6270Sstevel@tonic-gate 	if (!(flags & UFSMNT_LARGEFILES)) {  /* "nolargefiles" */
6280Sstevel@tonic-gate 		if (fsp->fs_flags & FSLARGEFILES) {
6290Sstevel@tonic-gate 			error = EFBIG;
6300Sstevel@tonic-gate 			goto remounterr;
6310Sstevel@tonic-gate 		}
6320Sstevel@tonic-gate 		ufsvfsp->vfs_lfflags &= ~UFS_LARGEFILES;
6330Sstevel@tonic-gate 	} else	/* "largefiles" */
6340Sstevel@tonic-gate 		ufsvfsp->vfs_lfflags |= UFS_LARGEFILES;
6350Sstevel@tonic-gate 	/*
6360Sstevel@tonic-gate 	 * read/write to read/write; all done
6370Sstevel@tonic-gate 	 */
6380Sstevel@tonic-gate 	if (fsp->fs_ronly == 0)
6390Sstevel@tonic-gate 		goto remounterr;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	/*
6420Sstevel@tonic-gate 	 * fix-on-panic assumes RO->RW remount implies system-critical fs
6430Sstevel@tonic-gate 	 * if it is shortly after boot; so, don't attempt to lock and fix
6440Sstevel@tonic-gate 	 * (unless the user explicitly asked for another action on error)
6450Sstevel@tonic-gate 	 * XXX UFSMNT_ONERROR_RDONLY rather than UFSMNT_ONERROR_PANIC
6460Sstevel@tonic-gate 	 */
6470Sstevel@tonic-gate #define	BOOT_TIME_LIMIT	(180*hz)
648*11066Srafael.vanoni@sun.com 	if (!(flags & UFSMNT_ONERROR_FLGMASK) &&
649*11066Srafael.vanoni@sun.com 	    ddi_get_lbolt() < BOOT_TIME_LIMIT) {
6500Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s is required to be mounted onerror=%s",
6514662Sfrankho 		    ufsvfsp->vfs_fs->fs_fsmnt, UFSMNT_ONERROR_PANIC_STR);
6520Sstevel@tonic-gate 		flags |= UFSMNT_ONERROR_PANIC;
6530Sstevel@tonic-gate 	}
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	if ((error = ufsfx_mount(ufsvfsp, flags)) != 0)
6560Sstevel@tonic-gate 		goto remounterr;
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	/*
659921Sbatschul 	 * quiesce the file system
6600Sstevel@tonic-gate 	 */
6610Sstevel@tonic-gate 	error = ufs_quiesce(ulp);
6620Sstevel@tonic-gate 	if (error)
6630Sstevel@tonic-gate 		goto remounterr;
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	tpt = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, SBLOCK, SBSIZE);
6660Sstevel@tonic-gate 	if (tpt->b_flags & B_ERROR) {
6670Sstevel@tonic-gate 		error = EIO;
6680Sstevel@tonic-gate 		goto remounterr;
6690Sstevel@tonic-gate 	}
6700Sstevel@tonic-gate 	fspt = (struct fs *)tpt->b_un.b_addr;
6710Sstevel@tonic-gate 	if (((fspt->fs_magic != FS_MAGIC) &&
6720Sstevel@tonic-gate 	    (fspt->fs_magic != MTB_UFS_MAGIC)) ||
673757Svsakar 	    (fspt->fs_magic == FS_MAGIC &&
6744662Sfrankho 	    (fspt->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
6754662Sfrankho 	    fspt->fs_version != UFS_VERSION_MIN)) ||
6760Sstevel@tonic-gate 	    (fspt->fs_magic == MTB_UFS_MAGIC &&
6774662Sfrankho 	    (fspt->fs_version > MTB_UFS_VERSION_1 ||
6784662Sfrankho 	    fspt->fs_version < MTB_UFS_VERSION_MIN)) ||
6790Sstevel@tonic-gate 	    fspt->fs_bsize > MAXBSIZE || fspt->fs_frag > MAXFRAG ||
6800Sstevel@tonic-gate 	    fspt->fs_bsize < sizeof (struct fs) || fspt->fs_bsize < PAGESIZE) {
6810Sstevel@tonic-gate 		tpt->b_flags |= B_STALE | B_AGE;
6820Sstevel@tonic-gate 		error = EINVAL;
6830Sstevel@tonic-gate 		goto remounterr;
6840Sstevel@tonic-gate 	}
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	if (ufsvfsp->vfs_log && (ufsvfsp->vfs_log->un_flags & LDL_NOROLL)) {
6870Sstevel@tonic-gate 		ufsvfsp->vfs_log->un_flags &= ~LDL_NOROLL;
6880Sstevel@tonic-gate 		logmap_start_roll(ufsvfsp->vfs_log);
6890Sstevel@tonic-gate 	}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	if (TRANS_ISERROR(ufsvfsp))
6920Sstevel@tonic-gate 		goto remounterr;
6930Sstevel@tonic-gate 	TRANS_DOMATAMAP(ufsvfsp);
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	if ((fspt->fs_state + fspt->fs_time == FSOKAY) &&
6960Sstevel@tonic-gate 	    fspt->fs_clean == FSLOG && !TRANS_ISTRANS(ufsvfsp)) {
6970Sstevel@tonic-gate 		ufsvfsp->vfs_log = NULL;
6980Sstevel@tonic-gate 		ufsvfsp->vfs_domatamap = 0;
6990Sstevel@tonic-gate 		error = ENOSPC;
7000Sstevel@tonic-gate 		goto remounterr;
7010Sstevel@tonic-gate 	}
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	if (fspt->fs_state + fspt->fs_time == FSOKAY &&
7040Sstevel@tonic-gate 	    (fspt->fs_clean == FSCLEAN ||
7050Sstevel@tonic-gate 	    fspt->fs_clean == FSSTABLE ||
7060Sstevel@tonic-gate 	    fspt->fs_clean == FSLOG)) {
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 		/*
7090Sstevel@tonic-gate 		 * Ensure that ufs_getsummaryinfo doesn't reconstruct
7100Sstevel@tonic-gate 		 * the summary info.
7110Sstevel@tonic-gate 		 */
7120Sstevel@tonic-gate 		error = ufs_getsummaryinfo(vfsp->vfs_dev, ufsvfsp, fspt);
7130Sstevel@tonic-gate 		if (error)
7140Sstevel@tonic-gate 			goto remounterr;
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 		/* preserve mount name */
7170Sstevel@tonic-gate 		(void) strncpy(fspt->fs_fsmnt, fsp->fs_fsmnt, MAXMNTLEN);
7180Sstevel@tonic-gate 		/* free the old cg space */
7190Sstevel@tonic-gate 		kmem_free(fsp->fs_u.fs_csp, fsp->fs_cssize);
7200Sstevel@tonic-gate 		/* switch in the new superblock */
7210Sstevel@tonic-gate 		fspt->fs_rolled = FS_NEED_ROLL;
7220Sstevel@tonic-gate 		bcopy(tpt->b_un.b_addr, bp->b_un.b_addr, fspt->fs_sbsize);
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 		fsp->fs_clean = FSSTABLE;
7250Sstevel@tonic-gate 	} /* superblock updated in memory */
7260Sstevel@tonic-gate 	tpt->b_flags |= B_STALE | B_AGE;
7270Sstevel@tonic-gate 	brelse(tpt);
7280Sstevel@tonic-gate 	tpt = 0;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	if (fsp->fs_clean != FSSTABLE) {
7310Sstevel@tonic-gate 		error = ENOSPC;
7320Sstevel@tonic-gate 		goto remounterr;
7330Sstevel@tonic-gate 	}
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp)) {
7370Sstevel@tonic-gate 		fsp->fs_clean = FSLOG;
7380Sstevel@tonic-gate 		ufsvfsp->vfs_dio = 0;
7390Sstevel@tonic-gate 	} else
7400Sstevel@tonic-gate 		if (ufsvfsp->vfs_dio)
7410Sstevel@tonic-gate 			fsp->fs_clean = FSSUSPEND;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	TRANS_MATA_MOUNT(ufsvfsp);
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	fsp->fs_fmod = 0;
7460Sstevel@tonic-gate 	fsp->fs_ronly = 0;
7470Sstevel@tonic-gate 
748329Saguzovsk 	atomic_add_long(&ufs_quiesce_pend, -1);
7490Sstevel@tonic-gate 	cv_broadcast(&ulp->ul_cv);
7500Sstevel@tonic-gate 	mutex_exit(&ulp->ul_lock);
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp)) {
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 		/*
7550Sstevel@tonic-gate 		 * start the delete thread
7560Sstevel@tonic-gate 		 */
7570Sstevel@tonic-gate 		ufs_thread_start(&ufsvfsp->vfs_delete, ufs_thread_delete, vfsp);
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 		/*
7600Sstevel@tonic-gate 		 * start the reclaim thread
7610Sstevel@tonic-gate 		 */
7620Sstevel@tonic-gate 		if (fsp->fs_reclaim & (FS_RECLAIM|FS_RECLAIMING)) {
7630Sstevel@tonic-gate 			fsp->fs_reclaim &= ~FS_RECLAIM;
7640Sstevel@tonic-gate 			fsp->fs_reclaim |=  FS_RECLAIMING;
7650Sstevel@tonic-gate 			ufs_thread_start(&ufsvfsp->vfs_reclaim,
7664662Sfrankho 			    ufs_thread_reclaim, vfsp);
7670Sstevel@tonic-gate 		}
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	TRANS_SBWRITE(ufsvfsp, TOP_MOUNT);
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	return (0);
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate remounterr:
7750Sstevel@tonic-gate 	if (tpt)
7760Sstevel@tonic-gate 		brelse(tpt);
777329Saguzovsk 	atomic_add_long(&ufs_quiesce_pend, -1);
7780Sstevel@tonic-gate 	cv_broadcast(&ulp->ul_cv);
7790Sstevel@tonic-gate 	mutex_exit(&ulp->ul_lock);
7800Sstevel@tonic-gate 	return (error);
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate /*
7840Sstevel@tonic-gate  * If the device maxtransfer size is not available, we use ufs_maxmaxphys
7850Sstevel@tonic-gate  * along with the system value for maxphys to determine the value for
7860Sstevel@tonic-gate  * maxtransfer.
7870Sstevel@tonic-gate  */
7880Sstevel@tonic-gate int ufs_maxmaxphys = (1024 * 1024);
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate #include <sys/ddi.h>		/* for delay(9f) */
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate int ufs_mount_error_delay = 20;	/* default to 20ms */
7931107Ssjelinek int ufs_mount_timeout = 60000;	/* default to 1 minute */
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate static int
mountfs(struct vfs * vfsp,enum whymountroot why,struct vnode * devvp,char * path,cred_t * cr,int isroot,void * raw_argsp,int args_len)7960Sstevel@tonic-gate mountfs(struct vfs *vfsp, enum whymountroot why, struct vnode *devvp,
7970Sstevel@tonic-gate 	char *path, cred_t *cr, int isroot, void *raw_argsp, int args_len)
7980Sstevel@tonic-gate {
7990Sstevel@tonic-gate 	dev_t dev = devvp->v_rdev;
8000Sstevel@tonic-gate 	struct fs *fsp;
8010Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = 0;
8020Sstevel@tonic-gate 	struct buf *bp = 0;
8030Sstevel@tonic-gate 	struct buf *tp = 0;
8040Sstevel@tonic-gate 	struct dk_cinfo ci;
8050Sstevel@tonic-gate 	int error = 0;
8060Sstevel@tonic-gate 	size_t len;
8070Sstevel@tonic-gate 	int needclose = 0;
8080Sstevel@tonic-gate 	int needtrans = 0;
8090Sstevel@tonic-gate 	struct inode *rip;
8100Sstevel@tonic-gate 	struct vnode *rvp = NULL;
8110Sstevel@tonic-gate 	int flags = 0;
8120Sstevel@tonic-gate 	kmutex_t *ihm;
8130Sstevel@tonic-gate 	int elapsed;
8140Sstevel@tonic-gate 	int status;
8150Sstevel@tonic-gate 	extern	int	maxphys;
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	if (args_len == sizeof (struct ufs_args) && raw_argsp)
8180Sstevel@tonic-gate 		flags = ((struct ufs_args *)raw_argsp)->flags;
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	ASSERT(vfs_lock_held(vfsp));
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	if (why == ROOT_INIT) {
8230Sstevel@tonic-gate 		/*
8240Sstevel@tonic-gate 		 * Open block device mounted on.
8250Sstevel@tonic-gate 		 * When bio is fixed for vnodes this can all be vnode
8260Sstevel@tonic-gate 		 * operations.
8270Sstevel@tonic-gate 		 */
8280Sstevel@tonic-gate 		error = VOP_OPEN(&devvp,
8295331Samw 		    (vfsp->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE,
8305331Samw 		    cr, NULL);
8310Sstevel@tonic-gate 		if (error)
8320Sstevel@tonic-gate 			goto out;
8330Sstevel@tonic-gate 		needclose = 1;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 		/*
8360Sstevel@tonic-gate 		 * Refuse to go any further if this
8370Sstevel@tonic-gate 		 * device is being used for swapping.
8380Sstevel@tonic-gate 		 */
8390Sstevel@tonic-gate 		if (IS_SWAPVP(devvp)) {
8400Sstevel@tonic-gate 			error = EBUSY;
8410Sstevel@tonic-gate 			goto out;
8420Sstevel@tonic-gate 		}
8430Sstevel@tonic-gate 	}
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	/*
8460Sstevel@tonic-gate 	 * check for dev already mounted on
8470Sstevel@tonic-gate 	 */
8480Sstevel@tonic-gate 	if (vfsp->vfs_flag & VFS_REMOUNT) {
8490Sstevel@tonic-gate 		error = remountfs(vfsp, dev, raw_argsp, args_len);
8500Sstevel@tonic-gate 		if (error == 0)
8510Sstevel@tonic-gate 			VN_RELE(devvp);
8520Sstevel@tonic-gate 		return (error);
8530Sstevel@tonic-gate 	}
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	ASSERT(devvp != 0);
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	/*
8580Sstevel@tonic-gate 	 * Flush back any dirty pages on the block device to
8590Sstevel@tonic-gate 	 * try and keep the buffer cache in sync with the page
8600Sstevel@tonic-gate 	 * cache if someone is trying to use block devices when
8610Sstevel@tonic-gate 	 * they really should be using the raw device.
8620Sstevel@tonic-gate 	 */
8630Sstevel@tonic-gate 	(void) VOP_PUTPAGE(common_specvp(devvp), (offset_t)0,
8645331Samw 	    (size_t)0, B_INVAL, cr, NULL);
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	/*
8670Sstevel@tonic-gate 	 * read in superblock
8680Sstevel@tonic-gate 	 */
8690Sstevel@tonic-gate 	ufsvfsp = kmem_zalloc(sizeof (struct ufsvfs), KM_SLEEP);
8700Sstevel@tonic-gate 	tp = UFS_BREAD(ufsvfsp, dev, SBLOCK, SBSIZE);
8710Sstevel@tonic-gate 	if (tp->b_flags & B_ERROR)
8720Sstevel@tonic-gate 		goto out;
8730Sstevel@tonic-gate 	fsp = (struct fs *)tp->b_un.b_addr;
874757Svsakar 
8750Sstevel@tonic-gate 	if ((fsp->fs_magic != FS_MAGIC) && (fsp->fs_magic != MTB_UFS_MAGIC)) {
876757Svsakar 		cmn_err(CE_NOTE,
877757Svsakar 		    "mount: not a UFS magic number (0x%x)", fsp->fs_magic);
878757Svsakar 		error = EINVAL;
879757Svsakar 		goto out;
880757Svsakar 	}
881757Svsakar 
882757Svsakar 	if ((fsp->fs_magic == FS_MAGIC) &&
883757Svsakar 	    (fsp->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
884757Svsakar 	    fsp->fs_version != UFS_VERSION_MIN)) {
885757Svsakar 		cmn_err(CE_NOTE,
886757Svsakar 		    "mount: unrecognized version of UFS on-disk format: %d",
887757Svsakar 		    fsp->fs_version);
8880Sstevel@tonic-gate 		error = EINVAL;
8890Sstevel@tonic-gate 		goto out;
8900Sstevel@tonic-gate 	}
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	if ((fsp->fs_magic == MTB_UFS_MAGIC) &&
8930Sstevel@tonic-gate 	    (fsp->fs_version > MTB_UFS_VERSION_1 ||
8940Sstevel@tonic-gate 	    fsp->fs_version < MTB_UFS_VERSION_MIN)) {
8950Sstevel@tonic-gate 		cmn_err(CE_NOTE,
8960Sstevel@tonic-gate 		    "mount: unrecognized version of UFS on-disk format: %d",
8970Sstevel@tonic-gate 		    fsp->fs_version);
8980Sstevel@tonic-gate 		error = EINVAL;
8990Sstevel@tonic-gate 		goto out;
9000Sstevel@tonic-gate 	}
9010Sstevel@tonic-gate 
902757Svsakar #ifndef _LP64
9030Sstevel@tonic-gate 	if (fsp->fs_magic == MTB_UFS_MAGIC) {
9040Sstevel@tonic-gate 		/*
9050Sstevel@tonic-gate 		 * Find the size of the device in sectors.  If the
9060Sstevel@tonic-gate 		 * the size in sectors is greater than INT_MAX, it's
9070Sstevel@tonic-gate 		 * a multi-terabyte file system, which can't be
9080Sstevel@tonic-gate 		 * mounted by a 32-bit kernel.  We can't use the
9090Sstevel@tonic-gate 		 * fsbtodb() macro in the next line because the macro
9100Sstevel@tonic-gate 		 * casts the intermediate values to daddr_t, which is
9110Sstevel@tonic-gate 		 * a 32-bit quantity in a 32-bit kernel.  Here we
9120Sstevel@tonic-gate 		 * really do need the intermediate values to be held
9130Sstevel@tonic-gate 		 * in 64-bit quantities because we're checking for
9140Sstevel@tonic-gate 		 * overflow of a 32-bit field.
9150Sstevel@tonic-gate 		 */
9160Sstevel@tonic-gate 		if ((((diskaddr_t)(fsp->fs_size)) << fsp->fs_fsbtodb)
9170Sstevel@tonic-gate 		    > INT_MAX) {
9180Sstevel@tonic-gate 			cmn_err(CE_NOTE,
9190Sstevel@tonic-gate 			    "mount: multi-terabyte UFS cannot be"
9200Sstevel@tonic-gate 			    " mounted by a 32-bit kernel");
9210Sstevel@tonic-gate 			error = EINVAL;
9220Sstevel@tonic-gate 			goto out;
9230Sstevel@tonic-gate 		}
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	}
926757Svsakar #endif
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	if (fsp->fs_bsize > MAXBSIZE || fsp->fs_frag > MAXFRAG ||
9290Sstevel@tonic-gate 	    fsp->fs_bsize < sizeof (struct fs) || fsp->fs_bsize < PAGESIZE) {
9300Sstevel@tonic-gate 		error = EINVAL;	/* also needs translation */
9310Sstevel@tonic-gate 		goto out;
9320Sstevel@tonic-gate 	}
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	/*
9350Sstevel@tonic-gate 	 * Allocate VFS private data.
9360Sstevel@tonic-gate 	 */
9370Sstevel@tonic-gate 	vfsp->vfs_bcount = 0;
9380Sstevel@tonic-gate 	vfsp->vfs_data = (caddr_t)ufsvfsp;
9390Sstevel@tonic-gate 	vfsp->vfs_fstype = ufsfstype;
9400Sstevel@tonic-gate 	vfsp->vfs_dev = dev;
9410Sstevel@tonic-gate 	vfsp->vfs_flag |= VFS_NOTRUNC;
9420Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, dev, ufsfstype);
9430Sstevel@tonic-gate 	ufsvfsp->vfs_devvp = devvp;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	/*
9460Sstevel@tonic-gate 	 * Cross-link with vfs and add to instance list.
9470Sstevel@tonic-gate 	 */
9480Sstevel@tonic-gate 	ufsvfsp->vfs_vfs = vfsp;
9490Sstevel@tonic-gate 	ufs_vfs_add(ufsvfsp);
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	ufsvfsp->vfs_dev = dev;
9520Sstevel@tonic-gate 	ufsvfsp->vfs_bufp = tp;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	ufsvfsp->vfs_dirsize = INODESIZE + (4 * ALLOCSIZE) + fsp->fs_fsize;
9554662Sfrankho 	ufsvfsp->vfs_minfrags =
9564662Sfrankho 	    (int)((int64_t)fsp->fs_dsize * fsp->fs_minfree / 100);
9570Sstevel@tonic-gate 	/*
9580Sstevel@tonic-gate 	 * if mount allows largefiles, indicate so in ufsvfs
9590Sstevel@tonic-gate 	 */
9600Sstevel@tonic-gate 	if (flags & UFSMNT_LARGEFILES)
9610Sstevel@tonic-gate 		ufsvfsp->vfs_lfflags |= UFS_LARGEFILES;
9620Sstevel@tonic-gate 	/*
9630Sstevel@tonic-gate 	 * Initialize threads
9640Sstevel@tonic-gate 	 */
965512Sjkennedy 	ufs_delete_init(ufsvfsp, 1);
9660Sstevel@tonic-gate 	ufs_thread_init(&ufsvfsp->vfs_reclaim, 0);
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	/*
9690Sstevel@tonic-gate 	 * Chicken and egg problem. The superblock may have deltas
9700Sstevel@tonic-gate 	 * in the log.  So after the log is scanned we reread the
9710Sstevel@tonic-gate 	 * superblock. We guarantee that the fields needed to
9720Sstevel@tonic-gate 	 * scan the log will not be in the log.
9730Sstevel@tonic-gate 	 */
9740Sstevel@tonic-gate 	if (fsp->fs_logbno && fsp->fs_clean == FSLOG &&
9750Sstevel@tonic-gate 	    (fsp->fs_state + fsp->fs_time == FSOKAY)) {
9760Sstevel@tonic-gate 		error = lufs_snarf(ufsvfsp, fsp, (vfsp->vfs_flag & VFS_RDONLY));
9770Sstevel@tonic-gate 		if (error) {
9780Sstevel@tonic-gate 			/*
9790Sstevel@tonic-gate 			 * Allow a ro mount to continue even if the
9800Sstevel@tonic-gate 			 * log cannot be processed - yet.
9810Sstevel@tonic-gate 			 */
9820Sstevel@tonic-gate 			if (!(vfsp->vfs_flag & VFS_RDONLY)) {
9830Sstevel@tonic-gate 				cmn_err(CE_WARN, "Error accessing ufs "
9844662Sfrankho 				    "log for %s; Please run fsck(1M)", path);
9850Sstevel@tonic-gate 				goto out;
9860Sstevel@tonic-gate 			}
9870Sstevel@tonic-gate 		}
9880Sstevel@tonic-gate 		tp->b_flags |= (B_AGE | B_STALE);
9890Sstevel@tonic-gate 		brelse(tp);
9900Sstevel@tonic-gate 		tp = UFS_BREAD(ufsvfsp, dev, SBLOCK, SBSIZE);
9910Sstevel@tonic-gate 		fsp = (struct fs *)tp->b_un.b_addr;
9920Sstevel@tonic-gate 		ufsvfsp->vfs_bufp = tp;
9930Sstevel@tonic-gate 		if (tp->b_flags & B_ERROR)
9940Sstevel@tonic-gate 			goto out;
9950Sstevel@tonic-gate 	}
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	/*
9980Sstevel@tonic-gate 	 * Set logging mounted flag used by lockfs
9990Sstevel@tonic-gate 	 */
10000Sstevel@tonic-gate 	ufsvfsp->vfs_validfs = UT_MOUNTED;
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	/*
10030Sstevel@tonic-gate 	 * Copy the super block into a buffer in its native size.
10040Sstevel@tonic-gate 	 * Use ngeteblk to allocate the buffer
10050Sstevel@tonic-gate 	 */
10060Sstevel@tonic-gate 	bp = ngeteblk(fsp->fs_bsize);
10070Sstevel@tonic-gate 	ufsvfsp->vfs_bufp = bp;
10080Sstevel@tonic-gate 	bp->b_edev = dev;
10090Sstevel@tonic-gate 	bp->b_dev = cmpdev(dev);
10100Sstevel@tonic-gate 	bp->b_blkno = SBLOCK;
10110Sstevel@tonic-gate 	bp->b_bcount = fsp->fs_sbsize;
10120Sstevel@tonic-gate 	bcopy(tp->b_un.b_addr, bp->b_un.b_addr, fsp->fs_sbsize);
10130Sstevel@tonic-gate 	tp->b_flags |= B_STALE | B_AGE;
10140Sstevel@tonic-gate 	brelse(tp);
10150Sstevel@tonic-gate 	tp = 0;
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	fsp = (struct fs *)bp->b_un.b_addr;
10180Sstevel@tonic-gate 	/*
10190Sstevel@tonic-gate 	 * Mount fails if superblock flag indicates presence of large
10200Sstevel@tonic-gate 	 * files and filesystem is attempted to be mounted 'nolargefiles'.
10210Sstevel@tonic-gate 	 * The exception is for a read only mount of root, which we
10220Sstevel@tonic-gate 	 * always want to succeed, so fsck can fix potential problems.
10230Sstevel@tonic-gate 	 * The assumption is that we will remount root at some point,
10240Sstevel@tonic-gate 	 * and the remount will enforce the mount option.
10250Sstevel@tonic-gate 	 */
10260Sstevel@tonic-gate 	if (!(isroot & (vfsp->vfs_flag & VFS_RDONLY)) &&
10270Sstevel@tonic-gate 	    (fsp->fs_flags & FSLARGEFILES) &&
10280Sstevel@tonic-gate 	    !(flags & UFSMNT_LARGEFILES)) {
10290Sstevel@tonic-gate 		error = EFBIG;
10300Sstevel@tonic-gate 		goto out;
10310Sstevel@tonic-gate 	}
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	if (vfsp->vfs_flag & VFS_RDONLY) {
10340Sstevel@tonic-gate 		fsp->fs_ronly = 1;
10350Sstevel@tonic-gate 		fsp->fs_fmod = 0;
10360Sstevel@tonic-gate 		if (((fsp->fs_state + fsp->fs_time) == FSOKAY) &&
10370Sstevel@tonic-gate 		    ((fsp->fs_clean == FSCLEAN) ||
10380Sstevel@tonic-gate 		    (fsp->fs_clean == FSSTABLE) ||
10390Sstevel@tonic-gate 		    (fsp->fs_clean == FSLOG))) {
10400Sstevel@tonic-gate 			if (isroot) {
10410Sstevel@tonic-gate 				if (fsp->fs_clean == FSLOG) {
10420Sstevel@tonic-gate 					if (fsp->fs_rolled == FS_ALL_ROLLED) {
10430Sstevel@tonic-gate 						ufs_clean_root = 1;
10440Sstevel@tonic-gate 					}
10450Sstevel@tonic-gate 				} else {
10460Sstevel@tonic-gate 					ufs_clean_root = 1;
10470Sstevel@tonic-gate 				}
10480Sstevel@tonic-gate 			}
10490Sstevel@tonic-gate 			fsp->fs_clean = FSSTABLE;
10500Sstevel@tonic-gate 		} else {
10510Sstevel@tonic-gate 			fsp->fs_clean = FSBAD;
10520Sstevel@tonic-gate 		}
10530Sstevel@tonic-gate 	} else {
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 		fsp->fs_fmod = 0;
10560Sstevel@tonic-gate 		fsp->fs_ronly = 0;
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 		TRANS_DOMATAMAP(ufsvfsp);
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 		if ((TRANS_ISERROR(ufsvfsp)) ||
10610Sstevel@tonic-gate 		    (((fsp->fs_state + fsp->fs_time) == FSOKAY) &&
10624662Sfrankho 		    fsp->fs_clean == FSLOG && !TRANS_ISTRANS(ufsvfsp))) {
10630Sstevel@tonic-gate 			ufsvfsp->vfs_log = NULL;
10640Sstevel@tonic-gate 			ufsvfsp->vfs_domatamap = 0;
10650Sstevel@tonic-gate 			error = ENOSPC;
10660Sstevel@tonic-gate 			goto out;
10670Sstevel@tonic-gate 		}
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 		if (((fsp->fs_state + fsp->fs_time) == FSOKAY) &&
10700Sstevel@tonic-gate 		    (fsp->fs_clean == FSCLEAN ||
10714662Sfrankho 		    fsp->fs_clean == FSSTABLE ||
10724662Sfrankho 		    fsp->fs_clean == FSLOG))
10730Sstevel@tonic-gate 			fsp->fs_clean = FSSTABLE;
10740Sstevel@tonic-gate 		else {
10750Sstevel@tonic-gate 			if (isroot) {
10760Sstevel@tonic-gate 				/*
10770Sstevel@tonic-gate 				 * allow root partition to be mounted even
10780Sstevel@tonic-gate 				 * when fs_state is not ok
10790Sstevel@tonic-gate 				 * will be fixed later by a remount root
10800Sstevel@tonic-gate 				 */
10810Sstevel@tonic-gate 				fsp->fs_clean = FSBAD;
10820Sstevel@tonic-gate 				ufsvfsp->vfs_log = NULL;
10830Sstevel@tonic-gate 				ufsvfsp->vfs_domatamap = 0;
10840Sstevel@tonic-gate 			} else {
10850Sstevel@tonic-gate 				error = ENOSPC;
10860Sstevel@tonic-gate 				goto out;
10870Sstevel@tonic-gate 			}
10880Sstevel@tonic-gate 		}
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 		if (fsp->fs_clean == FSSTABLE && TRANS_ISTRANS(ufsvfsp))
10910Sstevel@tonic-gate 			fsp->fs_clean = FSLOG;
10920Sstevel@tonic-gate 	}
10930Sstevel@tonic-gate 	TRANS_MATA_MOUNT(ufsvfsp);
10940Sstevel@tonic-gate 	needtrans = 1;
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	vfsp->vfs_bsize = fsp->fs_bsize;
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	/*
10990Sstevel@tonic-gate 	 * Read in summary info
11000Sstevel@tonic-gate 	 */
11010Sstevel@tonic-gate 	if (error = ufs_getsummaryinfo(dev, ufsvfsp, fsp))
11020Sstevel@tonic-gate 		goto out;
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 	/*
11050Sstevel@tonic-gate 	 * lastwhinetime is set to zero rather than lbolt, so that after
11060Sstevel@tonic-gate 	 * mounting if the filesystem is found to be full, then immediately the
11070Sstevel@tonic-gate 	 * "file system message" will be logged.
11080Sstevel@tonic-gate 	 */
11090Sstevel@tonic-gate 	ufsvfsp->vfs_lastwhinetime = 0L;
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	mutex_init(&ufsvfsp->vfs_lock, NULL, MUTEX_DEFAULT, NULL);
11130Sstevel@tonic-gate 	(void) copystr(path, fsp->fs_fsmnt, sizeof (fsp->fs_fsmnt) - 1, &len);
11140Sstevel@tonic-gate 	bzero(fsp->fs_fsmnt + len, sizeof (fsp->fs_fsmnt) - len);
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	/*
11170Sstevel@tonic-gate 	 * Sanity checks for old file systems
11180Sstevel@tonic-gate 	 */
11190Sstevel@tonic-gate 	if (fsp->fs_postblformat == FS_42POSTBLFMT)
11200Sstevel@tonic-gate 		ufsvfsp->vfs_nrpos = 8;
11210Sstevel@tonic-gate 	else
11220Sstevel@tonic-gate 		ufsvfsp->vfs_nrpos = fsp->fs_nrpos;
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	/*
11250Sstevel@tonic-gate 	 * Initialize lockfs structure to support file system locking
11260Sstevel@tonic-gate 	 */
11270Sstevel@tonic-gate 	bzero(&ufsvfsp->vfs_ulockfs.ul_lockfs,
11280Sstevel@tonic-gate 	    sizeof (struct lockfs));
11290Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_fs_lock = ULOCKFS_ULOCK;
11300Sstevel@tonic-gate 	mutex_init(&ufsvfsp->vfs_ulockfs.ul_lock, NULL,
11310Sstevel@tonic-gate 	    MUTEX_DEFAULT, NULL);
11320Sstevel@tonic-gate 	cv_init(&ufsvfsp->vfs_ulockfs.ul_cv, NULL, CV_DEFAULT, NULL);
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	/*
11350Sstevel@tonic-gate 	 * We don't need to grab vfs_dqrwlock for this ufs_iget() call.
11360Sstevel@tonic-gate 	 * We are in the process of mounting the file system so there
11370Sstevel@tonic-gate 	 * is no need to grab the quota lock. If a quota applies to the
11380Sstevel@tonic-gate 	 * root inode, then it will be updated when quotas are enabled.
11390Sstevel@tonic-gate 	 *
11400Sstevel@tonic-gate 	 * However, we have an ASSERT(RW_LOCK_HELD(&ufsvfsp->vfs_dqrwlock))
11410Sstevel@tonic-gate 	 * in getinoquota() that we want to keep so grab it anyway.
11420Sstevel@tonic-gate 	 */
11430Sstevel@tonic-gate 	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	error = ufs_iget_alloced(vfsp, UFSROOTINO, &rip, cr);
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 	rw_exit(&ufsvfsp->vfs_dqrwlock);
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	if (error)
11500Sstevel@tonic-gate 		goto out;
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	/*
11530Sstevel@tonic-gate 	 * make sure root inode is a directory.  Returning ENOTDIR might
11540Sstevel@tonic-gate 	 * be confused with the mount point not being a directory, so
11550Sstevel@tonic-gate 	 * we use EIO instead.
11560Sstevel@tonic-gate 	 */
11570Sstevel@tonic-gate 	if ((rip->i_mode & IFMT) != IFDIR) {
11580Sstevel@tonic-gate 		/*
11590Sstevel@tonic-gate 		 * Mark this inode as subject for cleanup
11600Sstevel@tonic-gate 		 * to avoid stray inodes in the cache.
11610Sstevel@tonic-gate 		 */
11620Sstevel@tonic-gate 		rvp = ITOV(rip);
11630Sstevel@tonic-gate 		error = EIO;
11640Sstevel@tonic-gate 		goto out;
11650Sstevel@tonic-gate 	}
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 	rvp = ITOV(rip);
11680Sstevel@tonic-gate 	mutex_enter(&rvp->v_lock);
11690Sstevel@tonic-gate 	rvp->v_flag |= VROOT;
11700Sstevel@tonic-gate 	mutex_exit(&rvp->v_lock);
11710Sstevel@tonic-gate 	ufsvfsp->vfs_root = rvp;
11720Sstevel@tonic-gate 	/* The buffer for the root inode does not contain a valid b_vp */
11730Sstevel@tonic-gate 	(void) bfinval(dev, 0);
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	/* options */
11760Sstevel@tonic-gate 	ufsvfsp->vfs_nosetsec = flags & UFSMNT_NOSETSEC;
11770Sstevel@tonic-gate 	ufsvfsp->vfs_nointr  = flags & UFSMNT_NOINTR;
11780Sstevel@tonic-gate 	ufsvfsp->vfs_syncdir = flags & UFSMNT_SYNCDIR;
11790Sstevel@tonic-gate 	ufsvfsp->vfs_noatime = flags & UFSMNT_NOATIME;
11800Sstevel@tonic-gate 	if ((flags & UFSMNT_NODFRATIME) || ufsvfsp->vfs_noatime)
11810Sstevel@tonic-gate 		ufsvfsp->vfs_dfritime &= ~UFS_DFRATIME;
11820Sstevel@tonic-gate 	else	/* dfratime, default behavior */
11830Sstevel@tonic-gate 		ufsvfsp->vfs_dfritime |= UFS_DFRATIME;
11840Sstevel@tonic-gate 	if (flags & UFSMNT_FORCEDIRECTIO)
11850Sstevel@tonic-gate 		ufsvfsp->vfs_forcedirectio = 1;
11860Sstevel@tonic-gate 	else if (flags & UFSMNT_NOFORCEDIRECTIO)
11870Sstevel@tonic-gate 		ufsvfsp->vfs_forcedirectio = 0;
1188*11066Srafael.vanoni@sun.com 	ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	ufsvfsp->vfs_nindiroffset = fsp->fs_nindir - 1;
11910Sstevel@tonic-gate 	ufsvfsp->vfs_nindirshift = highbit(ufsvfsp->vfs_nindiroffset);
11920Sstevel@tonic-gate 	ufsvfsp->vfs_ioclustsz = fsp->fs_bsize * fsp->fs_maxcontig;
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	if (cdev_ioctl(dev, DKIOCINFO, (intptr_t)&ci,
11950Sstevel@tonic-gate 	    FKIOCTL|FNATIVE|FREAD, CRED(), &status) == 0) {
11960Sstevel@tonic-gate 		ufsvfsp->vfs_iotransz = ci.dki_maxtransfer * DEV_BSIZE;
11970Sstevel@tonic-gate 	} else {
11980Sstevel@tonic-gate 		ufsvfsp->vfs_iotransz = MIN(maxphys, ufs_maxmaxphys);
11990Sstevel@tonic-gate 	}
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	if (ufsvfsp->vfs_iotransz <= 0) {
12020Sstevel@tonic-gate 		ufsvfsp->vfs_iotransz = MIN(maxphys, ufs_maxmaxphys);
12030Sstevel@tonic-gate 	}
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 	/*
12060Sstevel@tonic-gate 	 * When logging, used to reserve log space for writes and truncs
12070Sstevel@tonic-gate 	 */
12080Sstevel@tonic-gate 	ufsvfsp->vfs_avgbfree = fsp->fs_cstotal.cs_nbfree / fsp->fs_ncg;
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	/*
12110Sstevel@tonic-gate 	 * Determine whether to log cylinder group summary info.
12120Sstevel@tonic-gate 	 */
12130Sstevel@tonic-gate 	ufsvfsp->vfs_nolog_si = (fsp->fs_ncg < ufs_ncg_log);
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp)) {
12160Sstevel@tonic-gate 		/*
12170Sstevel@tonic-gate 		 * start the delete thread
12180Sstevel@tonic-gate 		 */
12190Sstevel@tonic-gate 		ufs_thread_start(&ufsvfsp->vfs_delete, ufs_thread_delete, vfsp);
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 		/*
12220Sstevel@tonic-gate 		 * start reclaim thread if the filesystem was not mounted
12230Sstevel@tonic-gate 		 * read only.
12240Sstevel@tonic-gate 		 */
12250Sstevel@tonic-gate 		if (!fsp->fs_ronly && (fsp->fs_reclaim &
12264662Sfrankho 		    (FS_RECLAIM|FS_RECLAIMING))) {
12270Sstevel@tonic-gate 			fsp->fs_reclaim &= ~FS_RECLAIM;
12280Sstevel@tonic-gate 			fsp->fs_reclaim |=  FS_RECLAIMING;
12290Sstevel@tonic-gate 			ufs_thread_start(&ufsvfsp->vfs_reclaim,
12300Sstevel@tonic-gate 			    ufs_thread_reclaim, vfsp);
12310Sstevel@tonic-gate 		}
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 		/* Mark the fs as unrolled */
12340Sstevel@tonic-gate 		fsp->fs_rolled = FS_NEED_ROLL;
12350Sstevel@tonic-gate 	} else if (!fsp->fs_ronly && (fsp->fs_reclaim &
12360Sstevel@tonic-gate 	    (FS_RECLAIM|FS_RECLAIMING))) {
12370Sstevel@tonic-gate 		/*
12380Sstevel@tonic-gate 		 * If a file system that is mounted nologging, after
12390Sstevel@tonic-gate 		 * having previously been mounted logging, becomes
12400Sstevel@tonic-gate 		 * unmounted whilst the reclaim thread is in the throes
12410Sstevel@tonic-gate 		 * of reclaiming open/deleted inodes, a subsequent mount
12420Sstevel@tonic-gate 		 * of such a file system with logging disabled could lead
12430Sstevel@tonic-gate 		 * to inodes becoming lost.  So, start reclaim now, even
12440Sstevel@tonic-gate 		 * though logging was disabled for the previous mount, to
12450Sstevel@tonic-gate 		 * tidy things up.
12460Sstevel@tonic-gate 		 */
12470Sstevel@tonic-gate 		fsp->fs_reclaim &= ~FS_RECLAIM;
12480Sstevel@tonic-gate 		fsp->fs_reclaim |=  FS_RECLAIMING;
12490Sstevel@tonic-gate 		ufs_thread_start(&ufsvfsp->vfs_reclaim,
12500Sstevel@tonic-gate 		    ufs_thread_reclaim, vfsp);
12510Sstevel@tonic-gate 	}
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	if (!fsp->fs_ronly) {
12540Sstevel@tonic-gate 		TRANS_SBWRITE(ufsvfsp, TOP_MOUNT);
12550Sstevel@tonic-gate 		if (error = geterror(ufsvfsp->vfs_bufp))
12560Sstevel@tonic-gate 			goto out;
12570Sstevel@tonic-gate 	}
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	/* fix-on-panic initialization */
12600Sstevel@tonic-gate 	if (isroot && !(flags & UFSMNT_ONERROR_FLGMASK))
12610Sstevel@tonic-gate 		flags |= UFSMNT_ONERROR_PANIC;	/* XXX ..._RDONLY */
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 	if ((error = ufsfx_mount(ufsvfsp, flags)) != 0)
12640Sstevel@tonic-gate 		goto out;
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	if (why == ROOT_INIT && isroot)
12670Sstevel@tonic-gate 		rootvp = devvp;
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 	return (0);
12700Sstevel@tonic-gate out:
12710Sstevel@tonic-gate 	if (error == 0)
12720Sstevel@tonic-gate 		error = EIO;
12730Sstevel@tonic-gate 	if (rvp) {
12740Sstevel@tonic-gate 		/* the following sequence is similar to ufs_unmount() */
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 		/*
12770Sstevel@tonic-gate 		 * There's a problem that ufs_iget() puts inodes into
12780Sstevel@tonic-gate 		 * the inode cache before it returns them.  If someone
12790Sstevel@tonic-gate 		 * traverses that cache and gets a reference to our
12800Sstevel@tonic-gate 		 * inode, there's a chance they'll still be using it
12810Sstevel@tonic-gate 		 * after we've destroyed it.  This is a hard race to
12820Sstevel@tonic-gate 		 * hit, but it's happened (putting in a medium delay
12830Sstevel@tonic-gate 		 * here, and a large delay in ufs_scan_inodes() for
12840Sstevel@tonic-gate 		 * inodes on the device we're bailing out on, makes
12850Sstevel@tonic-gate 		 * the race easy to demonstrate).  The symptom is some
12860Sstevel@tonic-gate 		 * other part of UFS faulting on bad inode contents,
12870Sstevel@tonic-gate 		 * or when grabbing one of the locks inside the inode,
12880Sstevel@tonic-gate 		 * etc.  The usual victim is ufs_scan_inodes() or
12890Sstevel@tonic-gate 		 * someone called by it.
12900Sstevel@tonic-gate 		 */
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 		/*
12930Sstevel@tonic-gate 		 * First, isolate it so that no new references can be
12940Sstevel@tonic-gate 		 * gotten via the inode cache.
12950Sstevel@tonic-gate 		 */
12960Sstevel@tonic-gate 		ihm = &ih_lock[INOHASH(UFSROOTINO)];
12970Sstevel@tonic-gate 		mutex_enter(ihm);
12980Sstevel@tonic-gate 		remque(rip);
12990Sstevel@tonic-gate 		mutex_exit(ihm);
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 		/*
13020Sstevel@tonic-gate 		 * Now wait for all outstanding references except our
13030Sstevel@tonic-gate 		 * own to drain.  This could, in theory, take forever,
13040Sstevel@tonic-gate 		 * so don't wait *too* long.  If we time out, mark
13050Sstevel@tonic-gate 		 * it stale and leak it, so we don't hit the problem
13060Sstevel@tonic-gate 		 * described above.
13070Sstevel@tonic-gate 		 *
13080Sstevel@tonic-gate 		 * Note that v_count is an int, which means we can read
13090Sstevel@tonic-gate 		 * it in one operation.  Thus, there's no need to lock
13100Sstevel@tonic-gate 		 * around our tests.
13110Sstevel@tonic-gate 		 */
13120Sstevel@tonic-gate 		elapsed = 0;
13130Sstevel@tonic-gate 		while ((rvp->v_count > 1) && (elapsed < ufs_mount_timeout)) {
13140Sstevel@tonic-gate 			delay(ufs_mount_error_delay * drv_usectohz(1000));
13150Sstevel@tonic-gate 			elapsed += ufs_mount_error_delay;
13160Sstevel@tonic-gate 		}
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 		if (rvp->v_count > 1) {
13190Sstevel@tonic-gate 			mutex_enter(&rip->i_tlock);
13200Sstevel@tonic-gate 			rip->i_flag |= ISTALE;
13210Sstevel@tonic-gate 			mutex_exit(&rip->i_tlock);
13220Sstevel@tonic-gate 			cmn_err(CE_WARN,
13234662Sfrankho 			    "Timed out while cleaning up after "
13244662Sfrankho 			    "failed mount of %s", path);
13250Sstevel@tonic-gate 		} else {
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 			/*
13280Sstevel@tonic-gate 			 * Now we're the only one with a handle left, so tear
13290Sstevel@tonic-gate 			 * it down the rest of the way.
13300Sstevel@tonic-gate 			 */
13310Sstevel@tonic-gate 			if (ufs_rmidle(rip))
13320Sstevel@tonic-gate 				VN_RELE(rvp);
13330Sstevel@tonic-gate 			ufs_si_del(rip);
13340Sstevel@tonic-gate 			rip->i_ufsvfs = NULL;
13350Sstevel@tonic-gate 			rvp->v_vfsp = NULL;
13360Sstevel@tonic-gate 			rvp->v_type = VBAD;
13370Sstevel@tonic-gate 			VN_RELE(rvp);
13380Sstevel@tonic-gate 		}
13390Sstevel@tonic-gate 	}
13400Sstevel@tonic-gate 	if (needtrans) {
13410Sstevel@tonic-gate 		TRANS_MATA_UMOUNT(ufsvfsp);
13420Sstevel@tonic-gate 	}
13430Sstevel@tonic-gate 	if (ufsvfsp) {
13440Sstevel@tonic-gate 		ufs_vfs_remove(ufsvfsp);
13450Sstevel@tonic-gate 		ufs_thread_exit(&ufsvfsp->vfs_delete);
13460Sstevel@tonic-gate 		ufs_thread_exit(&ufsvfsp->vfs_reclaim);
13470Sstevel@tonic-gate 		mutex_destroy(&ufsvfsp->vfs_lock);
13480Sstevel@tonic-gate 		if (ufsvfsp->vfs_log) {
13490Sstevel@tonic-gate 			lufs_unsnarf(ufsvfsp);
13500Sstevel@tonic-gate 		}
13510Sstevel@tonic-gate 		kmem_free(ufsvfsp, sizeof (struct ufsvfs));
13520Sstevel@tonic-gate 	}
13530Sstevel@tonic-gate 	if (bp) {
13540Sstevel@tonic-gate 		bp->b_flags |= (B_STALE|B_AGE);
13550Sstevel@tonic-gate 		brelse(bp);
13560Sstevel@tonic-gate 	}
13570Sstevel@tonic-gate 	if (tp) {
13580Sstevel@tonic-gate 		tp->b_flags |= (B_STALE|B_AGE);
13590Sstevel@tonic-gate 		brelse(tp);
13600Sstevel@tonic-gate 	}
13610Sstevel@tonic-gate 	if (needclose) {
13620Sstevel@tonic-gate 		(void) VOP_CLOSE(devvp, (vfsp->vfs_flag & VFS_RDONLY) ?
13635331Samw 		    FREAD : FREAD|FWRITE, 1, (offset_t)0, cr, NULL);
13640Sstevel@tonic-gate 		bflush(dev);
13650Sstevel@tonic-gate 		(void) bfinval(dev, 1);
13660Sstevel@tonic-gate 	}
13670Sstevel@tonic-gate 	return (error);
13680Sstevel@tonic-gate }
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate /*
13710Sstevel@tonic-gate  * vfs operations
13720Sstevel@tonic-gate  */
13730Sstevel@tonic-gate static int
ufs_unmount(struct vfs * vfsp,int fflag,struct cred * cr)13740Sstevel@tonic-gate ufs_unmount(struct vfs *vfsp, int fflag, struct cred *cr)
13750Sstevel@tonic-gate {
13760Sstevel@tonic-gate 	dev_t 		dev		= vfsp->vfs_dev;
13770Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp	= (struct ufsvfs *)vfsp->vfs_data;
13780Sstevel@tonic-gate 	struct fs	*fs		= ufsvfsp->vfs_fs;
13790Sstevel@tonic-gate 	struct ulockfs	*ulp		= &ufsvfsp->vfs_ulockfs;
13800Sstevel@tonic-gate 	struct vnode 	*bvp, *vp;
13810Sstevel@tonic-gate 	struct buf	*bp;
13820Sstevel@tonic-gate 	struct inode	*ip, *inext, *rip;
13830Sstevel@tonic-gate 	union ihead	*ih;
13840Sstevel@tonic-gate 	int 		error, flag, i;
13850Sstevel@tonic-gate 	struct lockfs	lockfs;
13860Sstevel@tonic-gate 	int		poll_events = POLLPRI;
13870Sstevel@tonic-gate 	extern struct pollhead ufs_pollhd;
13880Sstevel@tonic-gate 	refstr_t	*mountpoint;
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 	ASSERT(vfs_lock_held(vfsp));
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
13930Sstevel@tonic-gate 		return (EPERM);
13940Sstevel@tonic-gate 	/*
13950Sstevel@tonic-gate 	 * Forced unmount is now supported through the
13960Sstevel@tonic-gate 	 * lockfs protocol.
13970Sstevel@tonic-gate 	 */
13980Sstevel@tonic-gate 	if (fflag & MS_FORCE) {
13990Sstevel@tonic-gate 		/*
14000Sstevel@tonic-gate 		 * Mark the filesystem as being unmounted now in
14010Sstevel@tonic-gate 		 * case of a forcible umount before we take any
14020Sstevel@tonic-gate 		 * locks inside UFS to prevent racing with a VFS_VGET()
14030Sstevel@tonic-gate 		 * request. Throw these VFS_VGET() requests away for
14040Sstevel@tonic-gate 		 * the duration of the forcible umount so they won't
14050Sstevel@tonic-gate 		 * use stale or even freed data later on when we're done.
14060Sstevel@tonic-gate 		 * It may happen that the VFS has had a additional hold
14070Sstevel@tonic-gate 		 * placed on it by someone other than UFS and thus will
14080Sstevel@tonic-gate 		 * not get freed immediately once we're done with the
14090Sstevel@tonic-gate 		 * umount by dounmount() - use VFS_UNMOUNTED to inform
14100Sstevel@tonic-gate 		 * users of this still-alive VFS that its corresponding
14110Sstevel@tonic-gate 		 * filesystem being gone so they can detect that and error
14120Sstevel@tonic-gate 		 * out.
14130Sstevel@tonic-gate 		 */
14140Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_UNMOUNTED;
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 		ufs_thread_suspend(&ufsvfsp->vfs_delete);
14170Sstevel@tonic-gate 		mutex_enter(&ulp->ul_lock);
14180Sstevel@tonic-gate 		/*
14190Sstevel@tonic-gate 		 * If file system is already hard locked,
14200Sstevel@tonic-gate 		 * unmount the file system, otherwise
14210Sstevel@tonic-gate 		 * hard lock it before unmounting.
14220Sstevel@tonic-gate 		 */
14230Sstevel@tonic-gate 		if (!ULOCKFS_IS_HLOCK(ulp)) {
1424329Saguzovsk 			atomic_add_long(&ufs_quiesce_pend, 1);
14250Sstevel@tonic-gate 			lockfs.lf_lock = LOCKFS_HLOCK;
14260Sstevel@tonic-gate 			lockfs.lf_flags = 0;
14270Sstevel@tonic-gate 			lockfs.lf_key = ulp->ul_lockfs.lf_key + 1;
14280Sstevel@tonic-gate 			lockfs.lf_comlen = 0;
14290Sstevel@tonic-gate 			lockfs.lf_comment = NULL;
14300Sstevel@tonic-gate 			ufs_freeze(ulp, &lockfs);
14310Sstevel@tonic-gate 			ULOCKFS_SET_BUSY(ulp);
14320Sstevel@tonic-gate 			LOCKFS_SET_BUSY(&ulp->ul_lockfs);
14330Sstevel@tonic-gate 			(void) ufs_quiesce(ulp);
14340Sstevel@tonic-gate 			(void) ufs_flush(vfsp);
14350Sstevel@tonic-gate 			(void) ufs_thaw(vfsp, ufsvfsp, ulp);
1436329Saguzovsk 			atomic_add_long(&ufs_quiesce_pend, -1);
14370Sstevel@tonic-gate 			ULOCKFS_CLR_BUSY(ulp);
14380Sstevel@tonic-gate 			LOCKFS_CLR_BUSY(&ulp->ul_lockfs);
14390Sstevel@tonic-gate 			poll_events |= POLLERR;
14400Sstevel@tonic-gate 			pollwakeup(&ufs_pollhd, poll_events);
14410Sstevel@tonic-gate 		}
14420Sstevel@tonic-gate 		ufs_thread_continue(&ufsvfsp->vfs_delete);
14430Sstevel@tonic-gate 		mutex_exit(&ulp->ul_lock);
14440Sstevel@tonic-gate 	}
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 	/* let all types of writes go through */
1447*11066Srafael.vanoni@sun.com 	ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	/* coordinate with global hlock thread */
14500Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp) && (ufsvfsp->vfs_validfs == UT_HLOCKING)) {
14510Sstevel@tonic-gate 		/*
14520Sstevel@tonic-gate 		 * last possibility for a forced umount to fail hence clear
14530Sstevel@tonic-gate 		 * VFS_UNMOUNTED if appropriate.
14540Sstevel@tonic-gate 		 */
14550Sstevel@tonic-gate 		if (fflag & MS_FORCE)
14560Sstevel@tonic-gate 			vfsp->vfs_flag &= ~VFS_UNMOUNTED;
14570Sstevel@tonic-gate 		return (EAGAIN);
14580Sstevel@tonic-gate 	}
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	ufsvfsp->vfs_validfs = UT_UNMOUNTED;
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 	/* kill the reclaim thread */
14630Sstevel@tonic-gate 	ufs_thread_exit(&ufsvfsp->vfs_reclaim);
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 	/* suspend the delete thread */
14660Sstevel@tonic-gate 	ufs_thread_suspend(&ufsvfsp->vfs_delete);
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 	/*
14690Sstevel@tonic-gate 	 * drain the delete and idle queues
14700Sstevel@tonic-gate 	 */
14710Sstevel@tonic-gate 	ufs_delete_drain(vfsp, -1, 1);
14720Sstevel@tonic-gate 	ufs_idle_drain(vfsp);
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	/*
14750Sstevel@tonic-gate 	 * use the lockfs protocol to prevent new ops from starting
14760Sstevel@tonic-gate 	 * a forcible umount can not fail beyond this point as
14770Sstevel@tonic-gate 	 * we hard-locked the filesystem and drained all current consumers
14780Sstevel@tonic-gate 	 * before.
14790Sstevel@tonic-gate 	 */
14800Sstevel@tonic-gate 	mutex_enter(&ulp->ul_lock);
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate 	/*
14830Sstevel@tonic-gate 	 * if the file system is busy; return EBUSY
14840Sstevel@tonic-gate 	 */
1485923Ssdebnath 	if (ulp->ul_vnops_cnt || ulp->ul_falloc_cnt || ULOCKFS_IS_SLOCK(ulp)) {
14860Sstevel@tonic-gate 		error = EBUSY;
14870Sstevel@tonic-gate 		goto out;
14880Sstevel@tonic-gate 	}
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 	/*
14910Sstevel@tonic-gate 	 * if this is not a forced unmount (!hard/error locked), then
14920Sstevel@tonic-gate 	 * get rid of every inode except the root and quota inodes
14930Sstevel@tonic-gate 	 * also, commit any outstanding transactions
14940Sstevel@tonic-gate 	 */
14950Sstevel@tonic-gate 	if (!ULOCKFS_IS_HLOCK(ulp) && !ULOCKFS_IS_ELOCK(ulp))
14960Sstevel@tonic-gate 		if (error = ufs_flush(vfsp))
14970Sstevel@tonic-gate 			goto out;
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 	/*
15000Sstevel@tonic-gate 	 * ignore inodes in the cache if fs is hard locked or error locked
15010Sstevel@tonic-gate 	 */
15020Sstevel@tonic-gate 	rip = VTOI(ufsvfsp->vfs_root);
15030Sstevel@tonic-gate 	if (!ULOCKFS_IS_HLOCK(ulp) && !ULOCKFS_IS_ELOCK(ulp)) {
15040Sstevel@tonic-gate 		/*
15050Sstevel@tonic-gate 		 * Otherwise, only the quota and root inodes are in the cache.
15060Sstevel@tonic-gate 		 *
15070Sstevel@tonic-gate 		 * Avoid racing with ufs_update() and ufs_sync().
15080Sstevel@tonic-gate 		 */
15090Sstevel@tonic-gate 		mutex_enter(&ufs_scan_lock);
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 		for (i = 0, ih = ihead; i < inohsz; i++, ih++) {
15120Sstevel@tonic-gate 			mutex_enter(&ih_lock[i]);
15130Sstevel@tonic-gate 			for (ip = ih->ih_chain[0];
15144662Sfrankho 			    ip != (struct inode *)ih;
15154662Sfrankho 			    ip = ip->i_forw) {
15160Sstevel@tonic-gate 				if (ip->i_ufsvfs != ufsvfsp)
15170Sstevel@tonic-gate 					continue;
15180Sstevel@tonic-gate 				if (ip == ufsvfsp->vfs_qinod)
15190Sstevel@tonic-gate 					continue;
15200Sstevel@tonic-gate 				if (ip == rip && ITOV(ip)->v_count == 1)
15210Sstevel@tonic-gate 					continue;
15220Sstevel@tonic-gate 				mutex_exit(&ih_lock[i]);
15230Sstevel@tonic-gate 				mutex_exit(&ufs_scan_lock);
15240Sstevel@tonic-gate 				error = EBUSY;
15250Sstevel@tonic-gate 				goto out;
15260Sstevel@tonic-gate 			}
15270Sstevel@tonic-gate 			mutex_exit(&ih_lock[i]);
15280Sstevel@tonic-gate 		}
15290Sstevel@tonic-gate 		mutex_exit(&ufs_scan_lock);
15300Sstevel@tonic-gate 	}
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	/*
15330Sstevel@tonic-gate 	 * if a snapshot exists and this is a forced unmount, then delete
15340Sstevel@tonic-gate 	 * the snapshot.  Otherwise return EBUSY.  This will insure the
15350Sstevel@tonic-gate 	 * snapshot always belongs to a valid file system.
15360Sstevel@tonic-gate 	 */
15370Sstevel@tonic-gate 	if (ufsvfsp->vfs_snapshot) {
15380Sstevel@tonic-gate 		if (ULOCKFS_IS_HLOCK(ulp) || ULOCKFS_IS_ELOCK(ulp)) {
15390Sstevel@tonic-gate 			(void) fssnap_delete(&ufsvfsp->vfs_snapshot);
15400Sstevel@tonic-gate 		} else {
15410Sstevel@tonic-gate 			error = EBUSY;
15420Sstevel@tonic-gate 			goto out;
15430Sstevel@tonic-gate 		}
15440Sstevel@tonic-gate 	}
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	/*
15470Sstevel@tonic-gate 	 * Close the quota file and invalidate anything left in the quota
15480Sstevel@tonic-gate 	 * cache for this file system.  Pass kcred to allow all quota
15490Sstevel@tonic-gate 	 * manipulations.
15500Sstevel@tonic-gate 	 */
15510Sstevel@tonic-gate 	(void) closedq(ufsvfsp, kcred);
15520Sstevel@tonic-gate 	invalidatedq(ufsvfsp);
15530Sstevel@tonic-gate 	/*
15540Sstevel@tonic-gate 	 * drain the delete and idle queues
15550Sstevel@tonic-gate 	 */
15560Sstevel@tonic-gate 	ufs_delete_drain(vfsp, -1, 0);
15570Sstevel@tonic-gate 	ufs_idle_drain(vfsp);
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 	/*
15600Sstevel@tonic-gate 	 * discard the inodes for this fs (including root, shadow, and quota)
15610Sstevel@tonic-gate 	 */
15620Sstevel@tonic-gate 	for (i = 0, ih = ihead; i < inohsz; i++, ih++) {
15630Sstevel@tonic-gate 		mutex_enter(&ih_lock[i]);
15640Sstevel@tonic-gate 		for (inext = 0, ip = ih->ih_chain[0];
15654662Sfrankho 		    ip != (struct inode *)ih;
15664662Sfrankho 		    ip = inext) {
15670Sstevel@tonic-gate 			inext = ip->i_forw;
15680Sstevel@tonic-gate 			if (ip->i_ufsvfs != ufsvfsp)
15690Sstevel@tonic-gate 				continue;
15702322Sbatschul 
15712322Sbatschul 			/*
15722322Sbatschul 			 * We've found the inode in the cache and as we
15732322Sbatschul 			 * hold the hash mutex the inode can not
15742322Sbatschul 			 * disappear from underneath us.
15752322Sbatschul 			 * We also know it must have at least a vnode
15762322Sbatschul 			 * reference count of 1.
15772322Sbatschul 			 * We perform an additional VN_HOLD so the VN_RELE
15782322Sbatschul 			 * in case we take the inode off the idle queue
15792322Sbatschul 			 * can not be the last one.
15802322Sbatschul 			 * It is safe to grab the writer contents lock here
15812322Sbatschul 			 * to prevent a race with ufs_iinactive() putting
15822322Sbatschul 			 * inodes into the idle queue while we operate on
15832322Sbatschul 			 * this inode.
15842322Sbatschul 			 */
15852322Sbatschul 			rw_enter(&ip->i_contents, RW_WRITER);
15862322Sbatschul 
15870Sstevel@tonic-gate 			vp = ITOV(ip);
15880Sstevel@tonic-gate 			VN_HOLD(vp)
15890Sstevel@tonic-gate 			remque(ip);
15900Sstevel@tonic-gate 			if (ufs_rmidle(ip))
15910Sstevel@tonic-gate 				VN_RELE(vp);
15920Sstevel@tonic-gate 			ufs_si_del(ip);
15930Sstevel@tonic-gate 			/*
15940Sstevel@tonic-gate 			 * rip->i_ufsvfsp is needed by bflush()
15950Sstevel@tonic-gate 			 */
15960Sstevel@tonic-gate 			if (ip != rip)
15970Sstevel@tonic-gate 				ip->i_ufsvfs = NULL;
15980Sstevel@tonic-gate 			/*
15990Sstevel@tonic-gate 			 * Set vnode's vfsops to dummy ops, which return
16000Sstevel@tonic-gate 			 * EIO. This is needed to forced unmounts to work
16010Sstevel@tonic-gate 			 * with lofs/nfs properly.
16020Sstevel@tonic-gate 			 */
16030Sstevel@tonic-gate 			if (ULOCKFS_IS_HLOCK(ulp) || ULOCKFS_IS_ELOCK(ulp))
16040Sstevel@tonic-gate 				vp->v_vfsp = &EIO_vfs;
16050Sstevel@tonic-gate 			else
16060Sstevel@tonic-gate 				vp->v_vfsp = NULL;
16070Sstevel@tonic-gate 			vp->v_type = VBAD;
16082322Sbatschul 
16092322Sbatschul 			rw_exit(&ip->i_contents);
16102322Sbatschul 
16110Sstevel@tonic-gate 			VN_RELE(vp);
16120Sstevel@tonic-gate 		}
16130Sstevel@tonic-gate 		mutex_exit(&ih_lock[i]);
16140Sstevel@tonic-gate 	}
16150Sstevel@tonic-gate 	ufs_si_cache_flush(dev);
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	/*
16180Sstevel@tonic-gate 	 * kill the delete thread and drain the idle queue
16190Sstevel@tonic-gate 	 */
16200Sstevel@tonic-gate 	ufs_thread_exit(&ufsvfsp->vfs_delete);
16210Sstevel@tonic-gate 	ufs_idle_drain(vfsp);
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 	bp = ufsvfsp->vfs_bufp;
16240Sstevel@tonic-gate 	bvp = ufsvfsp->vfs_devvp;
16250Sstevel@tonic-gate 	flag = !fs->fs_ronly;
16260Sstevel@tonic-gate 	if (flag) {
16270Sstevel@tonic-gate 		bflush(dev);
16280Sstevel@tonic-gate 		if (fs->fs_clean != FSBAD) {
16290Sstevel@tonic-gate 			if (fs->fs_clean == FSSTABLE)
16300Sstevel@tonic-gate 				fs->fs_clean = FSCLEAN;
16310Sstevel@tonic-gate 			fs->fs_reclaim &= ~FS_RECLAIM;
16320Sstevel@tonic-gate 		}
16330Sstevel@tonic-gate 		if (TRANS_ISTRANS(ufsvfsp) &&
16340Sstevel@tonic-gate 		    !TRANS_ISERROR(ufsvfsp) &&
16350Sstevel@tonic-gate 		    !ULOCKFS_IS_HLOCK(ulp) &&
16360Sstevel@tonic-gate 		    (fs->fs_rolled == FS_NEED_ROLL)) {
16370Sstevel@tonic-gate 			/*
16380Sstevel@tonic-gate 			 * ufs_flush() above has flushed the last Moby.
16390Sstevel@tonic-gate 			 * This is needed to ensure the following superblock
16400Sstevel@tonic-gate 			 * update really is the last metadata update
16410Sstevel@tonic-gate 			 */
16420Sstevel@tonic-gate 			error = ufs_putsummaryinfo(dev, ufsvfsp, fs);
16430Sstevel@tonic-gate 			if (error == 0) {
16440Sstevel@tonic-gate 				fs->fs_rolled = FS_ALL_ROLLED;
16450Sstevel@tonic-gate 			}
16460Sstevel@tonic-gate 		}
16470Sstevel@tonic-gate 		TRANS_SBUPDATE(ufsvfsp, vfsp, TOP_SBUPDATE_UNMOUNT);
16480Sstevel@tonic-gate 		/*
16490Sstevel@tonic-gate 		 * push this last transaction
16500Sstevel@tonic-gate 		 */
16510Sstevel@tonic-gate 		curthread->t_flag |= T_DONTBLOCK;
16520Sstevel@tonic-gate 		TRANS_BEGIN_SYNC(ufsvfsp, TOP_COMMIT_UNMOUNT, TOP_COMMIT_SIZE,
16530Sstevel@tonic-gate 		    error);
16540Sstevel@tonic-gate 		if (!error)
16550Sstevel@tonic-gate 			TRANS_END_SYNC(ufsvfsp, error, TOP_COMMIT_UNMOUNT,
16560Sstevel@tonic-gate 			    TOP_COMMIT_SIZE);
16570Sstevel@tonic-gate 		curthread->t_flag &= ~T_DONTBLOCK;
16580Sstevel@tonic-gate 	}
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 	TRANS_MATA_UMOUNT(ufsvfsp);
16610Sstevel@tonic-gate 	lufs_unsnarf(ufsvfsp);		/* Release the in-memory structs */
16620Sstevel@tonic-gate 	ufsfx_unmount(ufsvfsp);		/* fix-on-panic bookkeeping */
16630Sstevel@tonic-gate 	kmem_free(fs->fs_u.fs_csp, fs->fs_cssize);
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 	bp->b_flags |= B_STALE|B_AGE;
16660Sstevel@tonic-gate 	ufsvfsp->vfs_bufp = NULL;	/* don't point at freed buf */
16670Sstevel@tonic-gate 	brelse(bp);			/* free the superblock buf */
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate 	(void) VOP_PUTPAGE(common_specvp(bvp), (offset_t)0, (size_t)0,
16705331Samw 	    B_INVAL, cr, NULL);
16715331Samw 	(void) VOP_CLOSE(bvp, flag, 1, (offset_t)0, cr, NULL);
16720Sstevel@tonic-gate 	bflush(dev);
16730Sstevel@tonic-gate 	(void) bfinval(dev, 1);
16740Sstevel@tonic-gate 	VN_RELE(bvp);
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 	/*
16770Sstevel@tonic-gate 	 * It is now safe to NULL out the ufsvfs pointer and discard
16780Sstevel@tonic-gate 	 * the root inode.
16790Sstevel@tonic-gate 	 */
16800Sstevel@tonic-gate 	rip->i_ufsvfs = NULL;
16810Sstevel@tonic-gate 	VN_RELE(ITOV(rip));
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 	/* free up lockfs comment structure, if any */
16840Sstevel@tonic-gate 	if (ulp->ul_lockfs.lf_comlen && ulp->ul_lockfs.lf_comment)
16850Sstevel@tonic-gate 		kmem_free(ulp->ul_lockfs.lf_comment, ulp->ul_lockfs.lf_comlen);
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	/*
16880Sstevel@tonic-gate 	 * Remove from instance list.
16890Sstevel@tonic-gate 	 */
16900Sstevel@tonic-gate 	ufs_vfs_remove(ufsvfsp);
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 	/*
16930Sstevel@tonic-gate 	 * For a forcible unmount, threads may be asleep in
16940Sstevel@tonic-gate 	 * ufs_lockfs_begin/ufs_check_lockfs.  These threads will need
16950Sstevel@tonic-gate 	 * the ufsvfs structure so we don't free it, yet.  ufs_update
16960Sstevel@tonic-gate 	 * will free it up after awhile.
16970Sstevel@tonic-gate 	 */
16980Sstevel@tonic-gate 	if (ULOCKFS_IS_HLOCK(ulp) || ULOCKFS_IS_ELOCK(ulp)) {
16990Sstevel@tonic-gate 		extern kmutex_t		ufsvfs_mutex;
17000Sstevel@tonic-gate 		extern struct ufsvfs	*ufsvfslist;
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 		mutex_enter(&ufsvfs_mutex);
17030Sstevel@tonic-gate 		ufsvfsp->vfs_dontblock = 1;
17040Sstevel@tonic-gate 		ufsvfsp->vfs_next = ufsvfslist;
17050Sstevel@tonic-gate 		ufsvfslist = ufsvfsp;
17060Sstevel@tonic-gate 		mutex_exit(&ufsvfs_mutex);
17070Sstevel@tonic-gate 		/* wakeup any suspended threads */
17080Sstevel@tonic-gate 		cv_broadcast(&ulp->ul_cv);
17090Sstevel@tonic-gate 		mutex_exit(&ulp->ul_lock);
17100Sstevel@tonic-gate 	} else {
17110Sstevel@tonic-gate 		mutex_destroy(&ufsvfsp->vfs_lock);
17120Sstevel@tonic-gate 		kmem_free(ufsvfsp, sizeof (struct ufsvfs));
17130Sstevel@tonic-gate 	}
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate 	/*
17160Sstevel@tonic-gate 	 * Now mark the filesystem as unmounted since we're done with it.
17170Sstevel@tonic-gate 	 */
17180Sstevel@tonic-gate 	vfsp->vfs_flag |= VFS_UNMOUNTED;
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 	return (0);
17210Sstevel@tonic-gate out:
17220Sstevel@tonic-gate 	/* open the fs to new ops */
17230Sstevel@tonic-gate 	cv_broadcast(&ulp->ul_cv);
17240Sstevel@tonic-gate 	mutex_exit(&ulp->ul_lock);
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp)) {
17270Sstevel@tonic-gate 		/* allow the delete thread to continue */
17280Sstevel@tonic-gate 		ufs_thread_continue(&ufsvfsp->vfs_delete);
17290Sstevel@tonic-gate 		/* restart the reclaim thread */
17300Sstevel@tonic-gate 		ufs_thread_start(&ufsvfsp->vfs_reclaim, ufs_thread_reclaim,
17314662Sfrankho 		    vfsp);
17320Sstevel@tonic-gate 		/* coordinate with global hlock thread */
17330Sstevel@tonic-gate 		ufsvfsp->vfs_validfs = UT_MOUNTED;
17340Sstevel@tonic-gate 		/* check for trans errors during umount */
17350Sstevel@tonic-gate 		ufs_trans_onerror();
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 		/*
17385331Samw 		 * if we have a separate /usr it will never unmount
17390Sstevel@tonic-gate 		 * when halting. In order to not re-read all the
17400Sstevel@tonic-gate 		 * cylinder group summary info on mounting after
17410Sstevel@tonic-gate 		 * reboot the logging of summary info is re-enabled
17420Sstevel@tonic-gate 		 * and the super block written out.
17430Sstevel@tonic-gate 		 */
17440Sstevel@tonic-gate 		mountpoint = vfs_getmntpoint(vfsp);
17450Sstevel@tonic-gate 		if ((fs->fs_si == FS_SI_OK) &&
17460Sstevel@tonic-gate 		    (strcmp("/usr", refstr_value(mountpoint)) == 0)) {
17470Sstevel@tonic-gate 			ufsvfsp->vfs_nolog_si = 0;
17480Sstevel@tonic-gate 			UFS_BWRITE2(NULL, ufsvfsp->vfs_bufp);
17490Sstevel@tonic-gate 		}
17500Sstevel@tonic-gate 		refstr_rele(mountpoint);
17510Sstevel@tonic-gate 	}
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate 	return (error);
17540Sstevel@tonic-gate }
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate static int
ufs_root(struct vfs * vfsp,struct vnode ** vpp)17570Sstevel@tonic-gate ufs_root(struct vfs *vfsp, struct vnode **vpp)
17580Sstevel@tonic-gate {
17590Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
17600Sstevel@tonic-gate 	struct vnode *vp;
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	if (!vfsp)
17630Sstevel@tonic-gate 		return (EIO);
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 	ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
17660Sstevel@tonic-gate 	if (!ufsvfsp || !ufsvfsp->vfs_root)
17670Sstevel@tonic-gate 		return (EIO);	/* forced unmount */
17680Sstevel@tonic-gate 
17690Sstevel@tonic-gate 	vp = ufsvfsp->vfs_root;
17700Sstevel@tonic-gate 	VN_HOLD(vp);
17710Sstevel@tonic-gate 	*vpp = vp;
17720Sstevel@tonic-gate 	return (0);
17730Sstevel@tonic-gate }
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate /*
17760Sstevel@tonic-gate  * Get file system statistics.
17770Sstevel@tonic-gate  */
17780Sstevel@tonic-gate static int
ufs_statvfs(struct vfs * vfsp,struct statvfs64 * sp)17790Sstevel@tonic-gate ufs_statvfs(struct vfs *vfsp, struct statvfs64 *sp)
17800Sstevel@tonic-gate {
17810Sstevel@tonic-gate 	struct fs *fsp;
17820Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
17830Sstevel@tonic-gate 	int blk, i;
17840Sstevel@tonic-gate 	long max_avail, used;
17850Sstevel@tonic-gate 	dev32_t d32;
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 	if (vfsp->vfs_flag & VFS_UNMOUNTED)
17880Sstevel@tonic-gate 		return (EIO);
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 	ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
17910Sstevel@tonic-gate 	fsp = ufsvfsp->vfs_fs;
17920Sstevel@tonic-gate 	if ((fsp->fs_magic != FS_MAGIC) && (fsp->fs_magic != MTB_UFS_MAGIC))
17930Sstevel@tonic-gate 		return (EINVAL);
1794757Svsakar 	if (fsp->fs_magic == FS_MAGIC &&
1795757Svsakar 	    (fsp->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
1796757Svsakar 	    fsp->fs_version != UFS_VERSION_MIN))
1797757Svsakar 		return (EINVAL);
17980Sstevel@tonic-gate 	if (fsp->fs_magic == MTB_UFS_MAGIC &&
17990Sstevel@tonic-gate 	    (fsp->fs_version > MTB_UFS_VERSION_1 ||
18000Sstevel@tonic-gate 	    fsp->fs_version < MTB_UFS_VERSION_MIN))
18010Sstevel@tonic-gate 		return (EINVAL);
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate 	/*
18040Sstevel@tonic-gate 	 * get the basic numbers
18050Sstevel@tonic-gate 	 */
18060Sstevel@tonic-gate 	(void) bzero(sp, sizeof (*sp));
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate 	sp->f_bsize = fsp->fs_bsize;
18090Sstevel@tonic-gate 	sp->f_frsize = fsp->fs_fsize;
18100Sstevel@tonic-gate 	sp->f_blocks = (fsblkcnt64_t)fsp->fs_dsize;
18110Sstevel@tonic-gate 	sp->f_bfree = (fsblkcnt64_t)fsp->fs_cstotal.cs_nbfree * fsp->fs_frag +
18120Sstevel@tonic-gate 	    fsp->fs_cstotal.cs_nffree;
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate 	sp->f_files = (fsfilcnt64_t)fsp->fs_ncg * fsp->fs_ipg;
18150Sstevel@tonic-gate 	sp->f_ffree = (fsfilcnt64_t)fsp->fs_cstotal.cs_nifree;
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate 	/*
18180Sstevel@tonic-gate 	 * Adjust the numbers based on things waiting to be deleted.
18190Sstevel@tonic-gate 	 * modifies f_bfree and f_ffree.  Afterwards, everything we
18200Sstevel@tonic-gate 	 * come up with will be self-consistent.  By definition, this
18210Sstevel@tonic-gate 	 * is a point-in-time snapshot, so the fact that the delete
18220Sstevel@tonic-gate 	 * thread's probably already invalidated the results is not a
1823512Sjkennedy 	 * problem.  Note that if the delete thread is ever extended to
1824512Sjkennedy 	 * non-logging ufs, this adjustment must always be made.
18250Sstevel@tonic-gate 	 */
1826512Sjkennedy 	if (TRANS_ISTRANS(ufsvfsp))
1827512Sjkennedy 		ufs_delete_adjust_stats(ufsvfsp, sp);
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate 	/*
18300Sstevel@tonic-gate 	 * avail = MAX(max_avail - used, 0)
18310Sstevel@tonic-gate 	 */
18320Sstevel@tonic-gate 	max_avail = fsp->fs_dsize - ufsvfsp->vfs_minfrags;
18330Sstevel@tonic-gate 
18340Sstevel@tonic-gate 	used = (fsp->fs_dsize - sp->f_bfree);
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate 	if (max_avail > used)
18370Sstevel@tonic-gate 		sp->f_bavail = (fsblkcnt64_t)max_avail - used;
18380Sstevel@tonic-gate 	else
18390Sstevel@tonic-gate 		sp->f_bavail = (fsblkcnt64_t)0;
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate 	sp->f_favail = sp->f_ffree;
18420Sstevel@tonic-gate 	(void) cmpldev(&d32, vfsp->vfs_dev);
18430Sstevel@tonic-gate 	sp->f_fsid = d32;
18440Sstevel@tonic-gate 	(void) strcpy(sp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
18450Sstevel@tonic-gate 	sp->f_flag = vf_to_stf(vfsp->vfs_flag);
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate 	/* keep coordinated with ufs_l_pathconf() */
18480Sstevel@tonic-gate 	sp->f_namemax = MAXNAMLEN;
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 	if (fsp->fs_cpc == 0) {
18510Sstevel@tonic-gate 		bzero(sp->f_fstr, 14);
18520Sstevel@tonic-gate 		return (0);
18530Sstevel@tonic-gate 	}
18540Sstevel@tonic-gate 	blk = fsp->fs_spc * fsp->fs_cpc / NSPF(fsp);
18550Sstevel@tonic-gate 	for (i = 0; i < blk; i += fsp->fs_frag) /* CSTYLED */
18560Sstevel@tonic-gate 		/* void */;
18570Sstevel@tonic-gate 	i -= fsp->fs_frag;
18580Sstevel@tonic-gate 	blk = i / fsp->fs_frag;
18590Sstevel@tonic-gate 	bcopy(&(fs_rotbl(fsp)[blk]), sp->f_fstr, 14);
18600Sstevel@tonic-gate 	return (0);
18610Sstevel@tonic-gate }
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate /*
18640Sstevel@tonic-gate  * Flush any pending I/O to file system vfsp.
18650Sstevel@tonic-gate  * The ufs_update() routine will only flush *all* ufs files.
18660Sstevel@tonic-gate  * If vfsp is non-NULL, only sync this ufs (in preparation
18670Sstevel@tonic-gate  * for a umount).
18680Sstevel@tonic-gate  */
18690Sstevel@tonic-gate /*ARGSUSED*/
18700Sstevel@tonic-gate static int
ufs_sync(struct vfs * vfsp,short flag,struct cred * cr)18710Sstevel@tonic-gate ufs_sync(struct vfs *vfsp, short flag, struct cred *cr)
18720Sstevel@tonic-gate {
18730Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
18740Sstevel@tonic-gate 	struct fs *fs;
18750Sstevel@tonic-gate 	int cheap = flag & SYNC_ATTR;
18760Sstevel@tonic-gate 	int error;
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 	/*
18790Sstevel@tonic-gate 	 * SYNC_CLOSE means we're rebooting.  Toss everything
18800Sstevel@tonic-gate 	 * on the idle queue so we don't have to slog through
18810Sstevel@tonic-gate 	 * a bunch of uninteresting inodes over and over again.
18820Sstevel@tonic-gate 	 */
18830Sstevel@tonic-gate 	if (flag & SYNC_CLOSE)
18840Sstevel@tonic-gate 		ufs_idle_drain(NULL);
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 	if (vfsp == NULL) {
18870Sstevel@tonic-gate 		ufs_update(flag);
18880Sstevel@tonic-gate 		return (0);
18890Sstevel@tonic-gate 	}
18900Sstevel@tonic-gate 
18910Sstevel@tonic-gate 	/* Flush a single ufs */
18920Sstevel@tonic-gate 	if (!vfs_matchops(vfsp, ufs_vfsops) || vfs_lock(vfsp) != 0)
18930Sstevel@tonic-gate 		return (0);
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate 	ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
18960Sstevel@tonic-gate 	if (!ufsvfsp)
18970Sstevel@tonic-gate 		return (EIO);
18980Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
18990Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
19000Sstevel@tonic-gate 
19010Sstevel@tonic-gate 	if (ufsvfsp->vfs_dio &&
19020Sstevel@tonic-gate 	    fs->fs_ronly == 0 &&
19030Sstevel@tonic-gate 	    fs->fs_clean != FSBAD &&
19040Sstevel@tonic-gate 	    fs->fs_clean != FSLOG) {
19050Sstevel@tonic-gate 		/* turn off fast-io on unmount, so no fsck needed (4029401) */
19060Sstevel@tonic-gate 		ufsvfsp->vfs_dio = 0;
19070Sstevel@tonic-gate 		fs->fs_clean = FSACTIVE;
19080Sstevel@tonic-gate 		fs->fs_fmod = 1;
19090Sstevel@tonic-gate 	}
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate 	/* Write back modified superblock */
19120Sstevel@tonic-gate 	if (fs->fs_fmod == 0) {
19130Sstevel@tonic-gate 		mutex_exit(&ufsvfsp->vfs_lock);
19140Sstevel@tonic-gate 	} else {
19150Sstevel@tonic-gate 		if (fs->fs_ronly != 0) {
19160Sstevel@tonic-gate 			mutex_exit(&ufsvfsp->vfs_lock);
19170Sstevel@tonic-gate 			vfs_unlock(vfsp);
19180Sstevel@tonic-gate 			return (ufs_fault(ufsvfsp->vfs_root,
19194662Sfrankho 			    "fs = %s update: ro fs mod\n", fs->fs_fsmnt));
19200Sstevel@tonic-gate 		}
19210Sstevel@tonic-gate 		fs->fs_fmod = 0;
19220Sstevel@tonic-gate 		mutex_exit(&ufsvfsp->vfs_lock);
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 		TRANS_SBUPDATE(ufsvfsp, vfsp, TOP_SBUPDATE_UPDATE);
19250Sstevel@tonic-gate 	}
19260Sstevel@tonic-gate 	vfs_unlock(vfsp);
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate 	/*
19290Sstevel@tonic-gate 	 * Avoid racing with ufs_update() and ufs_unmount().
19300Sstevel@tonic-gate 	 *
19310Sstevel@tonic-gate 	 */
19320Sstevel@tonic-gate 	mutex_enter(&ufs_scan_lock);
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 	(void) ufs_scan_inodes(1, ufs_sync_inode,
19350Sstevel@tonic-gate 	    (void *)(uintptr_t)cheap, ufsvfsp);
19360Sstevel@tonic-gate 
19370Sstevel@tonic-gate 	mutex_exit(&ufs_scan_lock);
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 	bflush((dev_t)vfsp->vfs_dev);
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate 	/*
19420Sstevel@tonic-gate 	 * commit any outstanding async transactions
19430Sstevel@tonic-gate 	 */
19440Sstevel@tonic-gate 	curthread->t_flag |= T_DONTBLOCK;
19450Sstevel@tonic-gate 	TRANS_BEGIN_SYNC(ufsvfsp, TOP_COMMIT_UPDATE, TOP_COMMIT_SIZE, error);
19460Sstevel@tonic-gate 	if (!error) {
19470Sstevel@tonic-gate 		TRANS_END_SYNC(ufsvfsp, error, TOP_COMMIT_UPDATE,
19480Sstevel@tonic-gate 		    TOP_COMMIT_SIZE);
19490Sstevel@tonic-gate 	}
19500Sstevel@tonic-gate 	curthread->t_flag &= ~T_DONTBLOCK;
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 	return (0);
19530Sstevel@tonic-gate }
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate void
sbupdate(struct vfs * vfsp)19570Sstevel@tonic-gate sbupdate(struct vfs *vfsp)
19580Sstevel@tonic-gate {
19590Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
19600Sstevel@tonic-gate 	struct fs *fs = ufsvfsp->vfs_fs;
19610Sstevel@tonic-gate 	struct buf *bp;
19620Sstevel@tonic-gate 	int blks;
19630Sstevel@tonic-gate 	caddr_t space;
19640Sstevel@tonic-gate 	int i;
19650Sstevel@tonic-gate 	size_t size;
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate 	/*
19680Sstevel@tonic-gate 	 * for ulockfs processing, limit the superblock writes
19690Sstevel@tonic-gate 	 */
19700Sstevel@tonic-gate 	if ((ufsvfsp->vfs_ulockfs.ul_sbowner) &&
19710Sstevel@tonic-gate 	    (curthread != ufsvfsp->vfs_ulockfs.ul_sbowner)) {
19720Sstevel@tonic-gate 		/* process later */
19730Sstevel@tonic-gate 		fs->fs_fmod = 1;
19740Sstevel@tonic-gate 		return;
19750Sstevel@tonic-gate 	}
19760Sstevel@tonic-gate 	ULOCKFS_SET_MOD((&ufsvfsp->vfs_ulockfs));
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp)) {
19790Sstevel@tonic-gate 		mutex_enter(&ufsvfsp->vfs_lock);
19800Sstevel@tonic-gate 		ufs_sbwrite(ufsvfsp);
19810Sstevel@tonic-gate 		mutex_exit(&ufsvfsp->vfs_lock);
19820Sstevel@tonic-gate 		return;
19830Sstevel@tonic-gate 	}
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
19860Sstevel@tonic-gate 	space = (caddr_t)fs->fs_u.fs_csp;
19870Sstevel@tonic-gate 	for (i = 0; i < blks; i += fs->fs_frag) {
19880Sstevel@tonic-gate 		size = fs->fs_bsize;
19890Sstevel@tonic-gate 		if (i + fs->fs_frag > blks)
19900Sstevel@tonic-gate 			size = (blks - i) * fs->fs_fsize;
19910Sstevel@tonic-gate 		bp = UFS_GETBLK(ufsvfsp, ufsvfsp->vfs_dev,
19924662Sfrankho 		    (daddr_t)(fsbtodb(fs, fs->fs_csaddr + i)),
19934662Sfrankho 		    fs->fs_bsize);
19940Sstevel@tonic-gate 		bcopy(space, bp->b_un.b_addr, size);
19950Sstevel@tonic-gate 		space += size;
19960Sstevel@tonic-gate 		bp->b_bcount = size;
19970Sstevel@tonic-gate 		UFS_BRWRITE(ufsvfsp, bp);
19980Sstevel@tonic-gate 	}
19990Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
20000Sstevel@tonic-gate 	ufs_sbwrite(ufsvfsp);
20010Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
20020Sstevel@tonic-gate }
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate int ufs_vget_idle_count = 2;	/* Number of inodes to idle each time */
20050Sstevel@tonic-gate static int
ufs_vget(struct vfs * vfsp,struct vnode ** vpp,struct fid * fidp)20060Sstevel@tonic-gate ufs_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp)
20070Sstevel@tonic-gate {
20080Sstevel@tonic-gate 	int error = 0;
20090Sstevel@tonic-gate 	struct ufid *ufid;
20100Sstevel@tonic-gate 	struct inode *ip;
20110Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
20120Sstevel@tonic-gate 	struct ulockfs *ulp;
20130Sstevel@tonic-gate 
20140Sstevel@tonic-gate 	/*
20150Sstevel@tonic-gate 	 * Check for unmounted filesystem.
20160Sstevel@tonic-gate 	 */
20170Sstevel@tonic-gate 	if (vfsp->vfs_flag & VFS_UNMOUNTED) {
20180Sstevel@tonic-gate 		error = EIO;
20190Sstevel@tonic-gate 		goto errout;
20200Sstevel@tonic-gate 	}
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate 	/*
20230Sstevel@tonic-gate 	 * Keep the idle queue from getting too long by
20240Sstevel@tonic-gate 	 * idling an inode before attempting to allocate another.
20250Sstevel@tonic-gate 	 *    This operation must be performed before entering
20260Sstevel@tonic-gate 	 *    lockfs or a transaction.
20270Sstevel@tonic-gate 	 */
20280Sstevel@tonic-gate 	if (ufs_idle_q.uq_ne > ufs_idle_q.uq_hiwat)
20290Sstevel@tonic-gate 		if ((curthread->t_flag & T_DONTBLOCK) == 0) {
20300Sstevel@tonic-gate 			ins.in_vidles.value.ul += ufs_vget_idle_count;
20310Sstevel@tonic-gate 			ufs_idle_some(ufs_vget_idle_count);
20320Sstevel@tonic-gate 		}
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate 	ufid = (struct ufid *)fidp;
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 	if (error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_VGET_MASK))
20370Sstevel@tonic-gate 		goto errout;
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate 	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
20400Sstevel@tonic-gate 
20410Sstevel@tonic-gate 	error = ufs_iget(vfsp, ufid->ufid_ino, &ip, CRED());
20420Sstevel@tonic-gate 
20430Sstevel@tonic-gate 	rw_exit(&ufsvfsp->vfs_dqrwlock);
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate 	ufs_lockfs_end(ulp);
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate 	if (error)
20480Sstevel@tonic-gate 		goto errout;
20490Sstevel@tonic-gate 
20500Sstevel@tonic-gate 	/*
20510Sstevel@tonic-gate 	 * Check if the inode has been deleted or freed or is in transient state
20520Sstevel@tonic-gate 	 * since the last VFS_VGET() request for it, release it and don't return
20530Sstevel@tonic-gate 	 * it to the caller, presumably NFS, as it's no longer valid.
20540Sstevel@tonic-gate 	 */
20550Sstevel@tonic-gate 	if (ip->i_gen != ufid->ufid_gen || ip->i_mode == 0 ||
205610655SFrank.Batschulat@Sun.COM 	    (ip->i_nlink <= 0)) {
20570Sstevel@tonic-gate 		VN_RELE(ITOV(ip));
20580Sstevel@tonic-gate 		error = EINVAL;
20590Sstevel@tonic-gate 		goto errout;
20600Sstevel@tonic-gate 	}
20610Sstevel@tonic-gate 
20620Sstevel@tonic-gate 	*vpp = ITOV(ip);
20630Sstevel@tonic-gate 	return (0);
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate errout:
20660Sstevel@tonic-gate 	*vpp = NULL;
20670Sstevel@tonic-gate 	return (error);
20680Sstevel@tonic-gate }
20690Sstevel@tonic-gate 
20700Sstevel@tonic-gate static int
ufsinit(int fstype,char * name)20710Sstevel@tonic-gate ufsinit(int fstype, char *name)
20720Sstevel@tonic-gate {
20730Sstevel@tonic-gate 	static const fs_operation_def_t ufs_vfsops_template[] = {
20743898Srsb 		VFSNAME_MOUNT,		{ .vfs_mount = ufs_mount },
20753898Srsb 		VFSNAME_UNMOUNT,	{ .vfs_unmount = ufs_unmount },
20763898Srsb 		VFSNAME_ROOT,		{ .vfs_root = ufs_root },
20773898Srsb 		VFSNAME_STATVFS,	{ .vfs_statvfs = ufs_statvfs },
20783898Srsb 		VFSNAME_SYNC,		{ .vfs_sync = ufs_sync },
20793898Srsb 		VFSNAME_VGET,		{ .vfs_vget = ufs_vget },
20803898Srsb 		VFSNAME_MOUNTROOT,	{ .vfs_mountroot = ufs_mountroot },
20813898Srsb 		NULL,			NULL
20820Sstevel@tonic-gate 	};
20830Sstevel@tonic-gate 	int error;
20840Sstevel@tonic-gate 
20850Sstevel@tonic-gate 	ufsfstype = fstype;
20860Sstevel@tonic-gate 
20870Sstevel@tonic-gate 	error = vfs_setfsops(fstype, ufs_vfsops_template, &ufs_vfsops);
20880Sstevel@tonic-gate 	if (error != 0) {
20890Sstevel@tonic-gate 		cmn_err(CE_WARN, "ufsinit: bad vfs ops template");
20900Sstevel@tonic-gate 		return (error);
20910Sstevel@tonic-gate 	}
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate 	error = vn_make_ops(name, ufs_vnodeops_template, &ufs_vnodeops);
20940Sstevel@tonic-gate 	if (error != 0) {
20950Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
20960Sstevel@tonic-gate 		cmn_err(CE_WARN, "ufsinit: bad vnode ops template");
20970Sstevel@tonic-gate 		return (error);
20980Sstevel@tonic-gate 	}
20990Sstevel@tonic-gate 
21000Sstevel@tonic-gate 	ufs_iinit();
21010Sstevel@tonic-gate 	return (0);
21020Sstevel@tonic-gate }
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate #ifdef __sparc
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate /*
21070Sstevel@tonic-gate  * Mounting a mirrored SVM volume is only supported on ufs,
21080Sstevel@tonic-gate  * this is special-case boot code to support that configuration.
21090Sstevel@tonic-gate  * At this point, we have booted and mounted root on a
21100Sstevel@tonic-gate  * single component of the mirror.  Complete the boot
21110Sstevel@tonic-gate  * by configuring SVM and converting the root to the
21120Sstevel@tonic-gate  * dev_t of the mirrored root device.  This dev_t conversion
21130Sstevel@tonic-gate  * only works because the underlying device doesn't change.
21140Sstevel@tonic-gate  */
21150Sstevel@tonic-gate int
ufs_remountroot(struct vfs * vfsp)21160Sstevel@tonic-gate ufs_remountroot(struct vfs *vfsp)
21170Sstevel@tonic-gate {
21180Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
21190Sstevel@tonic-gate 	struct ulockfs *ulp;
21200Sstevel@tonic-gate 	dev_t new_rootdev;
21210Sstevel@tonic-gate 	dev_t old_rootdev;
21220Sstevel@tonic-gate 	struct vnode *old_rootvp;
21230Sstevel@tonic-gate 	struct vnode *new_rootvp;
21240Sstevel@tonic-gate 	int error, sberror = 0;
21250Sstevel@tonic-gate 	struct inode	*ip;
21260Sstevel@tonic-gate 	union ihead	*ih;
21270Sstevel@tonic-gate 	struct buf	*bp;
21280Sstevel@tonic-gate 	int i;
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 	old_rootdev = rootdev;
21310Sstevel@tonic-gate 	old_rootvp = rootvp;
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate 	new_rootdev = getrootdev();
21340Sstevel@tonic-gate 	if (new_rootdev == (dev_t)NODEV) {
21350Sstevel@tonic-gate 		return (ENODEV);
21360Sstevel@tonic-gate 	}
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate 	new_rootvp = makespecvp(new_rootdev, VBLK);
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate 	error = VOP_OPEN(&new_rootvp,
21415331Samw 	    (vfsp->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE, CRED(), NULL);
21420Sstevel@tonic-gate 	if (error) {
21430Sstevel@tonic-gate 		cmn_err(CE_CONT,
21440Sstevel@tonic-gate 		    "Cannot open mirrored root device, error %d\n", error);
21450Sstevel@tonic-gate 		return (error);
21460Sstevel@tonic-gate 	}
21470Sstevel@tonic-gate 
21480Sstevel@tonic-gate 	if (vfs_lock(vfsp) != 0) {
21490Sstevel@tonic-gate 		return (EBUSY);
21500Sstevel@tonic-gate 	}
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 	ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
21530Sstevel@tonic-gate 	ulp = &ufsvfsp->vfs_ulockfs;
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 	mutex_enter(&ulp->ul_lock);
2156329Saguzovsk 	atomic_add_long(&ufs_quiesce_pend, 1);
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate 	(void) ufs_quiesce(ulp);
21590Sstevel@tonic-gate 	(void) ufs_flush(vfsp);
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 	/*
21620Sstevel@tonic-gate 	 * Convert root vfs to new dev_t, including vfs hash
21630Sstevel@tonic-gate 	 * table and fs id.
21640Sstevel@tonic-gate 	 */
21650Sstevel@tonic-gate 	vfs_root_redev(vfsp, new_rootdev, ufsfstype);
21660Sstevel@tonic-gate 
21670Sstevel@tonic-gate 	ufsvfsp->vfs_devvp = new_rootvp;
21680Sstevel@tonic-gate 	ufsvfsp->vfs_dev = new_rootdev;
21690Sstevel@tonic-gate 
21700Sstevel@tonic-gate 	bp = ufsvfsp->vfs_bufp;
21710Sstevel@tonic-gate 	bp->b_edev = new_rootdev;
21720Sstevel@tonic-gate 	bp->b_dev = cmpdev(new_rootdev);
21730Sstevel@tonic-gate 
21740Sstevel@tonic-gate 	/*
21750Sstevel@tonic-gate 	 * The buffer for the root inode does not contain a valid b_vp
21760Sstevel@tonic-gate 	 */
21770Sstevel@tonic-gate 	(void) bfinval(new_rootdev, 0);
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate 	/*
21800Sstevel@tonic-gate 	 * Here we hand-craft inodes with old root device
21810Sstevel@tonic-gate 	 * references to refer to the new device instead.
21820Sstevel@tonic-gate 	 */
21830Sstevel@tonic-gate 	mutex_enter(&ufs_scan_lock);
21840Sstevel@tonic-gate 
21850Sstevel@tonic-gate 	for (i = 0, ih = ihead; i < inohsz; i++, ih++) {
21860Sstevel@tonic-gate 		mutex_enter(&ih_lock[i]);
21870Sstevel@tonic-gate 		for (ip = ih->ih_chain[0];
21884662Sfrankho 		    ip != (struct inode *)ih;
21894662Sfrankho 		    ip = ip->i_forw) {
21900Sstevel@tonic-gate 			if (ip->i_ufsvfs != ufsvfsp)
21910Sstevel@tonic-gate 				continue;
21920Sstevel@tonic-gate 			if (ip == ufsvfsp->vfs_qinod)
21930Sstevel@tonic-gate 				continue;
21940Sstevel@tonic-gate 			if (ip->i_dev == old_rootdev) {
21950Sstevel@tonic-gate 				ip->i_dev = new_rootdev;
21960Sstevel@tonic-gate 			}
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate 			if (ip->i_devvp == old_rootvp) {
21990Sstevel@tonic-gate 				ip->i_devvp = new_rootvp;
22000Sstevel@tonic-gate 			}
22010Sstevel@tonic-gate 		}
22020Sstevel@tonic-gate 		mutex_exit(&ih_lock[i]);
22030Sstevel@tonic-gate 	}
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 	mutex_exit(&ufs_scan_lock);
22060Sstevel@tonic-gate 
22070Sstevel@tonic-gate 	/*
22080Sstevel@tonic-gate 	 * Make Sure logging structures are using the new device
22090Sstevel@tonic-gate 	 * if logging is enabled.  Also start any logging thread that
22100Sstevel@tonic-gate 	 * needs to write to the device and couldn't earlier.
22110Sstevel@tonic-gate 	 */
22120Sstevel@tonic-gate 	if (ufsvfsp->vfs_log) {
22130Sstevel@tonic-gate 		buf_t		*bp, *tbp;
22140Sstevel@tonic-gate 		ml_unit_t	*ul = ufsvfsp->vfs_log;
22150Sstevel@tonic-gate 		struct fs	*fsp = ufsvfsp->vfs_fs;
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate 		/*
22180Sstevel@tonic-gate 		 * Update the main logging structure.
22190Sstevel@tonic-gate 		 */
22200Sstevel@tonic-gate 		ul->un_dev = new_rootdev;
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 		/*
22230Sstevel@tonic-gate 		 * Get a new bp for the on disk structures.
22240Sstevel@tonic-gate 		 */
22250Sstevel@tonic-gate 		bp = ul->un_bp;
22260Sstevel@tonic-gate 		tbp = ngeteblk(dbtob(LS_SECTORS));
22270Sstevel@tonic-gate 		tbp->b_edev = new_rootdev;
22280Sstevel@tonic-gate 		tbp->b_dev = cmpdev(new_rootdev);
22290Sstevel@tonic-gate 		tbp->b_blkno = bp->b_blkno;
22300Sstevel@tonic-gate 		bcopy(bp->b_un.b_addr, tbp->b_un.b_addr, DEV_BSIZE);
22310Sstevel@tonic-gate 		bcopy(bp->b_un.b_addr, tbp->b_un.b_addr + DEV_BSIZE, DEV_BSIZE);
22320Sstevel@tonic-gate 		bp->b_flags |= (B_STALE | B_AGE);
22330Sstevel@tonic-gate 		brelse(bp);
22340Sstevel@tonic-gate 		ul->un_bp = tbp;
22350Sstevel@tonic-gate 
22360Sstevel@tonic-gate 		/*
22370Sstevel@tonic-gate 		 * Allocate new circular buffers.
22380Sstevel@tonic-gate 		 */
22390Sstevel@tonic-gate 		alloc_rdbuf(&ul->un_rdbuf, MAPBLOCKSIZE, MAPBLOCKSIZE);
22400Sstevel@tonic-gate 		alloc_wrbuf(&ul->un_wrbuf, ldl_bufsize(ul));
22410Sstevel@tonic-gate 
22420Sstevel@tonic-gate 		/*
22430Sstevel@tonic-gate 		 * Clear the noroll bit which indicates that logging
2244921Sbatschul 		 * can't roll the log yet and start the logmap roll thread
2245921Sbatschul 		 * unless the filesystem is still read-only in which case
2246921Sbatschul 		 * remountfs() will do it when going to read-write.
22470Sstevel@tonic-gate 		 */
22480Sstevel@tonic-gate 		ASSERT(ul->un_flags & LDL_NOROLL);
22490Sstevel@tonic-gate 
2250921Sbatschul 		if (!fsp->fs_ronly) {
2251921Sbatschul 			ul->un_flags &= ~LDL_NOROLL;
2252921Sbatschul 			logmap_start_roll(ul);
2253921Sbatschul 		}
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate 		/*
22560Sstevel@tonic-gate 		 * Start the reclaim thread if needed.
22570Sstevel@tonic-gate 		 */
22580Sstevel@tonic-gate 		if (!fsp->fs_ronly && (fsp->fs_reclaim &
22594662Sfrankho 		    (FS_RECLAIM|FS_RECLAIMING))) {
22600Sstevel@tonic-gate 			fsp->fs_reclaim &= ~FS_RECLAIM;
22610Sstevel@tonic-gate 			fsp->fs_reclaim |= FS_RECLAIMING;
22620Sstevel@tonic-gate 			ufs_thread_start(&ufsvfsp->vfs_reclaim,
22634662Sfrankho 			    ufs_thread_reclaim, vfsp);
22640Sstevel@tonic-gate 			TRANS_SBWRITE(ufsvfsp, TOP_SBUPDATE_UPDATE);
22650Sstevel@tonic-gate 			if (sberror = geterror(ufsvfsp->vfs_bufp)) {
22660Sstevel@tonic-gate 				refstr_t	*mntpt;
22670Sstevel@tonic-gate 				mntpt = vfs_getmntpoint(vfsp);
22680Sstevel@tonic-gate 				cmn_err(CE_WARN,
22694662Sfrankho 				    "Remountroot failed to update Reclaim"
22704662Sfrankho 				    "state for filesystem %s "
22714662Sfrankho 				    "Error writing SuperBlock %d",
22724662Sfrankho 				    refstr_value(mntpt), error);
22730Sstevel@tonic-gate 				refstr_rele(mntpt);
22740Sstevel@tonic-gate 			}
22750Sstevel@tonic-gate 		}
22760Sstevel@tonic-gate 	}
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 	rootdev = new_rootdev;
22790Sstevel@tonic-gate 	rootvp = new_rootvp;
22800Sstevel@tonic-gate 
2281329Saguzovsk 	atomic_add_long(&ufs_quiesce_pend, -1);
22820Sstevel@tonic-gate 	cv_broadcast(&ulp->ul_cv);
22830Sstevel@tonic-gate 	mutex_exit(&ulp->ul_lock);
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 	vfs_unlock(vfsp);
22860Sstevel@tonic-gate 
22875331Samw 	error = VOP_CLOSE(old_rootvp, FREAD, 1, (offset_t)0, CRED(), NULL);
22880Sstevel@tonic-gate 	if (error) {
22890Sstevel@tonic-gate 		cmn_err(CE_CONT,
22900Sstevel@tonic-gate 		    "close of root device component failed, error %d\n",
22910Sstevel@tonic-gate 		    error);
22920Sstevel@tonic-gate 	}
22930Sstevel@tonic-gate 	VN_RELE(old_rootvp);
22940Sstevel@tonic-gate 
22950Sstevel@tonic-gate 	return (sberror ? sberror : error);
22960Sstevel@tonic-gate }
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate #endif	/* __sparc */
2299