xref: /titanic_54/usr/src/uts/common/fs/vfs.c (revision f2ce0b1ac6ef0ef361ceb94c9ccdae476abf2953)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55a59a8b3Srsb  * Common Development and Distribution License (the "License").
65a59a8b3Srsb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
220fbb751dSJohn Levon  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
2376ca3cb0SRobert Mustacchi  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
24bd93c05dSAlexander Eremin  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
25*f2ce0b1aSAndrew Stormont  * Copyright 2017 RackTop Systems.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
337c478bd9Sstevel@tonic-gate  * The Regents of the University of California
347c478bd9Sstevel@tonic-gate  * All Rights Reserved
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
377c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
387c478bd9Sstevel@tonic-gate  * contributors.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
437c478bd9Sstevel@tonic-gate #include <sys/param.h>
447c478bd9Sstevel@tonic-gate #include <sys/errno.h>
457c478bd9Sstevel@tonic-gate #include <sys/user.h>
467c478bd9Sstevel@tonic-gate #include <sys/fstyp.h>
477c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
487c478bd9Sstevel@tonic-gate #include <sys/systm.h>
497c478bd9Sstevel@tonic-gate #include <sys/proc.h>
507c478bd9Sstevel@tonic-gate #include <sys/mount.h>
517c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
52aa59c4cbSrsb #include <sys/vfs_opreg.h>
537c478bd9Sstevel@tonic-gate #include <sys/fem.h>
547c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
557c478bd9Sstevel@tonic-gate #include <sys/stat.h>
567c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
577c478bd9Sstevel@tonic-gate #include <sys/statfs.h>
587c478bd9Sstevel@tonic-gate #include <sys/cred.h>
597c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
607c478bd9Sstevel@tonic-gate #include <sys/rwstlock.h>
617c478bd9Sstevel@tonic-gate #include <sys/dnlc.h>
627c478bd9Sstevel@tonic-gate #include <sys/file.h>
637c478bd9Sstevel@tonic-gate #include <sys/time.h>
647c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
657c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
667c478bd9Sstevel@tonic-gate #include <sys/buf.h>
677c478bd9Sstevel@tonic-gate #include <sys/swap.h>
687c478bd9Sstevel@tonic-gate #include <sys/debug.h>
697c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
707c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
717c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
727c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
737c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
747c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h>
757c478bd9Sstevel@tonic-gate #include <sys/dc_ki.h>
767c478bd9Sstevel@tonic-gate #include <sys/poll.h>
777c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
787c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
797c478bd9Sstevel@tonic-gate #include <sys/zone.h>
807c478bd9Sstevel@tonic-gate #include <sys/policy.h>
817c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
827c478bd9Sstevel@tonic-gate #include <sys/objfs.h>
837c478bd9Sstevel@tonic-gate #include <sys/console.h>
847c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
85da6c28aaSamw #include <sys/attr.h>
86b24ab676SJeff Bonwick #include <sys/zio.h>
87e7cbe64fSgw25295 #include <sys/spa.h>
8893239addSjohnlev #include <sys/lofi.h>
896cefaae1SJack Meng #include <sys/bootprops.h>
90147c1346SArne Jansen #include <sys/avl.h>
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #include <vm/page.h>
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
9582c7f3c4Srsb /* Private interfaces to create vopstats-related data structures */
9682c7f3c4Srsb extern void		initialize_vopstats(vopstats_t *);
9782c7f3c4Srsb extern vopstats_t	*get_fstype_vopstats(struct vfs *, struct vfssw *);
9882c7f3c4Srsb extern vsk_anchor_t	*get_vskstat_anchor(struct vfs *);
9982c7f3c4Srsb 
1007c478bd9Sstevel@tonic-gate static void vfs_clearmntopt_nolock(mntopts_t *, const char *, int);
1017c478bd9Sstevel@tonic-gate static void vfs_setmntopt_nolock(mntopts_t *, const char *,
1027c478bd9Sstevel@tonic-gate     const char *, int, int);
1037c478bd9Sstevel@tonic-gate static int  vfs_optionisset_nolock(const mntopts_t *, const char *, char **);
1047c478bd9Sstevel@tonic-gate static void vfs_freemnttab(struct vfs *);
1057c478bd9Sstevel@tonic-gate static void vfs_freeopt(mntopt_t *);
1067c478bd9Sstevel@tonic-gate static void vfs_swapopttbl_nolock(mntopts_t *, mntopts_t *);
1077c478bd9Sstevel@tonic-gate static void vfs_swapopttbl(mntopts_t *, mntopts_t *);
1087c478bd9Sstevel@tonic-gate static void vfs_copyopttbl_extend(const mntopts_t *, mntopts_t *, int);
1097c478bd9Sstevel@tonic-gate static void vfs_createopttbl_extend(mntopts_t *, const char *,
1107c478bd9Sstevel@tonic-gate     const mntopts_t *);
1117c478bd9Sstevel@tonic-gate static char **vfs_copycancelopt_extend(char **const, int);
1127c478bd9Sstevel@tonic-gate static void vfs_freecancelopt(char **);
113843e1988Sjohnlev static void getrootfs(char **, char **);
1147c478bd9Sstevel@tonic-gate static int getmacpath(dev_info_t *, void *);
115df2381bfSpraks static void vfs_mnttabvp_setup(void);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate struct ipmnt {
1187c478bd9Sstevel@tonic-gate 	struct ipmnt	*mip_next;
1197c478bd9Sstevel@tonic-gate 	dev_t		mip_dev;
1207c478bd9Sstevel@tonic-gate 	struct vfs	*mip_vfsp;
1217c478bd9Sstevel@tonic-gate };
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static kmutex_t		vfs_miplist_mutex;
1247c478bd9Sstevel@tonic-gate static struct ipmnt	*vfs_miplist = NULL;
1257c478bd9Sstevel@tonic-gate static struct ipmnt	*vfs_miplist_end = NULL;
1267c478bd9Sstevel@tonic-gate 
127da6c28aaSamw static kmem_cache_t *vfs_cache;	/* Pointer to VFS kmem cache */
128da6c28aaSamw 
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate  * VFS global data.
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate vnode_t *rootdir;		/* pointer to root inode vnode. */
1337c478bd9Sstevel@tonic-gate vnode_t *devicesdir;		/* pointer to inode of devices root */
134facf4a8dSllai1 vnode_t	*devdir;		/* pointer to inode of dev root */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate char *server_rootpath;		/* root path for diskless clients */
1377c478bd9Sstevel@tonic-gate char *server_hostname;		/* hostname of diskless server */
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static struct vfs root;
1407c478bd9Sstevel@tonic-gate static struct vfs devices;
141facf4a8dSllai1 static struct vfs dev;
1427c478bd9Sstevel@tonic-gate struct vfs *rootvfs = &root;	/* pointer to root vfs; head of VFS list. */
143147c1346SArne Jansen avl_tree_t vfs_by_dev;		/* avl tree to index mounted VFSs by dev */
144147c1346SArne Jansen avl_tree_t vfs_by_mntpnt;	/* avl tree to index mounted VFSs by mntpnt */
145147c1346SArne Jansen uint64_t vfs_curr_mntix;	/* counter to provide a unique mntix for
146147c1346SArne Jansen 				 * entries in the above avl trees.
147147c1346SArne Jansen 				 * protected by vfslist lock */
1487c478bd9Sstevel@tonic-gate rvfs_t *rvfs_list;		/* array of vfs ptrs for vfs hash list */
1497c478bd9Sstevel@tonic-gate int vfshsz = 512;		/* # of heads/locks in vfs hash arrays */
1507c478bd9Sstevel@tonic-gate 				/* must be power of 2!	*/
1517c478bd9Sstevel@tonic-gate timespec_t vfs_mnttab_ctime;	/* mnttab created time */
1527c478bd9Sstevel@tonic-gate timespec_t vfs_mnttab_mtime;	/* mnttab last modified time */
153ffc349abSdm120769 char *vfs_dummyfstype = "\0";
1547c478bd9Sstevel@tonic-gate struct pollhead vfs_pollhd;	/* for mnttab pollers */
155df2381bfSpraks struct vnode *vfs_mntdummyvp;	/* to fake mnttab read/write for file events */
156df2381bfSpraks int	mntfstype;		/* will be set once mnt fs is mounted */
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate  * Table for generic options recognized in the VFS layer and acted
1607c478bd9Sstevel@tonic-gate  * on at this level before parsing file system specific options.
1617c478bd9Sstevel@tonic-gate  * The nosuid option is stronger than any of the devices and setuid
1627c478bd9Sstevel@tonic-gate  * options, so those are canceled when nosuid is seen.
1637c478bd9Sstevel@tonic-gate  *
1647c478bd9Sstevel@tonic-gate  * All options which are added here need to be added to the
1657c478bd9Sstevel@tonic-gate  * list of standard options in usr/src/cmd/fs.d/fslib.c as well.
1667c478bd9Sstevel@tonic-gate  */
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * VFS Mount options table
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate static char *ro_cancel[] = { MNTOPT_RW, NULL };
1717c478bd9Sstevel@tonic-gate static char *rw_cancel[] = { MNTOPT_RO, NULL };
1727c478bd9Sstevel@tonic-gate static char *suid_cancel[] = { MNTOPT_NOSUID, NULL };
1737c478bd9Sstevel@tonic-gate static char *nosuid_cancel[] = { MNTOPT_SUID, MNTOPT_DEVICES, MNTOPT_NODEVICES,
1747c478bd9Sstevel@tonic-gate     MNTOPT_NOSETUID, MNTOPT_SETUID, NULL };
1757c478bd9Sstevel@tonic-gate static char *devices_cancel[] = { MNTOPT_NODEVICES, NULL };
1767c478bd9Sstevel@tonic-gate static char *nodevices_cancel[] = { MNTOPT_DEVICES, NULL };
1777c478bd9Sstevel@tonic-gate static char *setuid_cancel[] = { MNTOPT_NOSETUID, NULL };
1787c478bd9Sstevel@tonic-gate static char *nosetuid_cancel[] = { MNTOPT_SETUID, NULL };
1797c478bd9Sstevel@tonic-gate static char *nbmand_cancel[] = { MNTOPT_NONBMAND, NULL };
1807c478bd9Sstevel@tonic-gate static char *nonbmand_cancel[] = { MNTOPT_NBMAND, NULL };
1817c478bd9Sstevel@tonic-gate static char *exec_cancel[] = { MNTOPT_NOEXEC, NULL };
1827c478bd9Sstevel@tonic-gate static char *noexec_cancel[] = { MNTOPT_EXEC, NULL };
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate static const mntopt_t mntopts[] = {
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate  *	option name		cancel options		default arg	flags
1877c478bd9Sstevel@tonic-gate  */
1887c478bd9Sstevel@tonic-gate 	{ MNTOPT_REMOUNT,	NULL,			NULL,
1897c478bd9Sstevel@tonic-gate 		MO_NODISPLAY, (void *)0 },
1907c478bd9Sstevel@tonic-gate 	{ MNTOPT_RO,		ro_cancel,		NULL,		0,
1917c478bd9Sstevel@tonic-gate 		(void *)0 },
1927c478bd9Sstevel@tonic-gate 	{ MNTOPT_RW,		rw_cancel,		NULL,		0,
1937c478bd9Sstevel@tonic-gate 		(void *)0 },
1947c478bd9Sstevel@tonic-gate 	{ MNTOPT_SUID,		suid_cancel,		NULL,		0,
1957c478bd9Sstevel@tonic-gate 		(void *)0 },
1967c478bd9Sstevel@tonic-gate 	{ MNTOPT_NOSUID,	nosuid_cancel,		NULL,		0,
1977c478bd9Sstevel@tonic-gate 		(void *)0 },
1987c478bd9Sstevel@tonic-gate 	{ MNTOPT_DEVICES,	devices_cancel,		NULL,		0,
1997c478bd9Sstevel@tonic-gate 		(void *)0 },
2007c478bd9Sstevel@tonic-gate 	{ MNTOPT_NODEVICES,	nodevices_cancel,	NULL,		0,
2017c478bd9Sstevel@tonic-gate 		(void *)0 },
2027c478bd9Sstevel@tonic-gate 	{ MNTOPT_SETUID,	setuid_cancel,		NULL,		0,
2037c478bd9Sstevel@tonic-gate 		(void *)0 },
2047c478bd9Sstevel@tonic-gate 	{ MNTOPT_NOSETUID,	nosetuid_cancel,	NULL,		0,
2057c478bd9Sstevel@tonic-gate 		(void *)0 },
2067c478bd9Sstevel@tonic-gate 	{ MNTOPT_NBMAND,	nbmand_cancel,		NULL,		0,
2077c478bd9Sstevel@tonic-gate 		(void *)0 },
2087c478bd9Sstevel@tonic-gate 	{ MNTOPT_NONBMAND,	nonbmand_cancel,	NULL,		0,
2097c478bd9Sstevel@tonic-gate 		(void *)0 },
2107c478bd9Sstevel@tonic-gate 	{ MNTOPT_EXEC,		exec_cancel,		NULL,		0,
2117c478bd9Sstevel@tonic-gate 		(void *)0 },
2127c478bd9Sstevel@tonic-gate 	{ MNTOPT_NOEXEC,	noexec_cancel,		NULL,		0,
2137c478bd9Sstevel@tonic-gate 		(void *)0 },
2147c478bd9Sstevel@tonic-gate };
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate const mntopts_t vfs_mntopts = {
2177c478bd9Sstevel@tonic-gate 	sizeof (mntopts) / sizeof (mntopt_t),
2187c478bd9Sstevel@tonic-gate 	(mntopt_t *)&mntopts[0]
2197c478bd9Sstevel@tonic-gate };
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate /*
2227c478bd9Sstevel@tonic-gate  * File system operation dispatch functions.
2237c478bd9Sstevel@tonic-gate  */
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate int
2267c478bd9Sstevel@tonic-gate fsop_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	return (*(vfsp)->vfs_op->vfs_mount)(vfsp, mvp, uap, cr);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate int
2327c478bd9Sstevel@tonic-gate fsop_unmount(vfs_t *vfsp, int flag, cred_t *cr)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	return (*(vfsp)->vfs_op->vfs_unmount)(vfsp, flag, cr);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate int
2387c478bd9Sstevel@tonic-gate fsop_root(vfs_t *vfsp, vnode_t **vpp)
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate 	refstr_t *mntpt;
2417c478bd9Sstevel@tonic-gate 	int ret = (*(vfsp)->vfs_op->vfs_root)(vfsp, vpp);
2427c478bd9Sstevel@tonic-gate 	/*
2437c478bd9Sstevel@tonic-gate 	 * Make sure this root has a path.  With lofs, it is possible to have
2447c478bd9Sstevel@tonic-gate 	 * a NULL mountpoint.
2457c478bd9Sstevel@tonic-gate 	 */
246ca2c3138Seschrock 	if (ret == 0 && vfsp->vfs_mntpt != NULL && (*vpp)->v_path == NULL) {
2477c478bd9Sstevel@tonic-gate 		mntpt = vfs_getmntpoint(vfsp);
2487c478bd9Sstevel@tonic-gate 		vn_setpath_str(*vpp, refstr_value(mntpt),
2497c478bd9Sstevel@tonic-gate 		    strlen(refstr_value(mntpt)));
2507c478bd9Sstevel@tonic-gate 		refstr_rele(mntpt);
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	return (ret);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate int
2577c478bd9Sstevel@tonic-gate fsop_statfs(vfs_t *vfsp, statvfs64_t *sp)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	return (*(vfsp)->vfs_op->vfs_statvfs)(vfsp, sp);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate int
2637c478bd9Sstevel@tonic-gate fsop_sync(vfs_t *vfsp, short flag, cred_t *cr)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	return (*(vfsp)->vfs_op->vfs_sync)(vfsp, flag, cr);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate int
2697c478bd9Sstevel@tonic-gate fsop_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
2707c478bd9Sstevel@tonic-gate {
271da6c28aaSamw 	/*
272da6c28aaSamw 	 * In order to handle system attribute fids in a manner
273da6c28aaSamw 	 * transparent to the underlying fs, we embed the fid for
274da6c28aaSamw 	 * the sysattr parent object in the sysattr fid and tack on
275da6c28aaSamw 	 * some extra bytes that only the sysattr layer knows about.
276da6c28aaSamw 	 *
277da6c28aaSamw 	 * This guarantees that sysattr fids are larger than other fids
2789660e5cbSJanice Chang 	 * for this vfs. If the vfs supports the sysattr view interface
2799660e5cbSJanice Chang 	 * (as indicated by VFSFT_SYSATTR_VIEWS), we cannot have a size
2809660e5cbSJanice Chang 	 * collision with XATTR_FIDSZ.
281da6c28aaSamw 	 */
2829660e5cbSJanice Chang 	if (vfs_has_feature(vfsp, VFSFT_SYSATTR_VIEWS) &&
283da6c28aaSamw 	    fidp->fid_len == XATTR_FIDSZ)
284da6c28aaSamw 		return (xattr_dir_vget(vfsp, vpp, fidp));
285da6c28aaSamw 
2867c478bd9Sstevel@tonic-gate 	return (*(vfsp)->vfs_op->vfs_vget)(vfsp, vpp, fidp);
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate int
2907c478bd9Sstevel@tonic-gate fsop_mountroot(vfs_t *vfsp, enum whymountroot reason)
2917c478bd9Sstevel@tonic-gate {
2927c478bd9Sstevel@tonic-gate 	return (*(vfsp)->vfs_op->vfs_mountroot)(vfsp, reason);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate void
2967c478bd9Sstevel@tonic-gate fsop_freefs(vfs_t *vfsp)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	(*(vfsp)->vfs_op->vfs_freevfs)(vfsp);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate int
3027c478bd9Sstevel@tonic-gate fsop_vnstate(vfs_t *vfsp, vnode_t *vp, vntrans_t nstate)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	return ((*(vfsp)->vfs_op->vfs_vnstate)(vfsp, vp, nstate));
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate int
3087c478bd9Sstevel@tonic-gate fsop_sync_by_kind(int fstype, short flag, cred_t *cr)
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	ASSERT((fstype >= 0) && (fstype < nfstype));
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	if (ALLOCATED_VFSSW(&vfssw[fstype]) && VFS_INSTALLED(&vfssw[fstype]))
3137c478bd9Sstevel@tonic-gate 		return (*vfssw[fstype].vsw_vfsops.vfs_sync) (NULL, flag, cr);
3147c478bd9Sstevel@tonic-gate 	else
3157c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate /*
3197c478bd9Sstevel@tonic-gate  * File system initialization.  vfs_setfsops() must be called from a file
3207c478bd9Sstevel@tonic-gate  * system's init routine.
3217c478bd9Sstevel@tonic-gate  */
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate static int
3247c478bd9Sstevel@tonic-gate fs_copyfsops(const fs_operation_def_t *template, vfsops_t *actual,
3257c478bd9Sstevel@tonic-gate     int *unused_ops)
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate 	static const fs_operation_trans_def_t vfs_ops_table[] = {
3287c478bd9Sstevel@tonic-gate 		VFSNAME_MOUNT, offsetof(vfsops_t, vfs_mount),
3297c478bd9Sstevel@tonic-gate 			fs_nosys, fs_nosys,
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 		VFSNAME_UNMOUNT, offsetof(vfsops_t, vfs_unmount),
3327c478bd9Sstevel@tonic-gate 			fs_nosys, fs_nosys,
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		VFSNAME_ROOT, offsetof(vfsops_t, vfs_root),
3357c478bd9Sstevel@tonic-gate 			fs_nosys, fs_nosys,
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		VFSNAME_STATVFS, offsetof(vfsops_t, vfs_statvfs),
3387c478bd9Sstevel@tonic-gate 			fs_nosys, fs_nosys,
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 		VFSNAME_SYNC, offsetof(vfsops_t, vfs_sync),
3417c478bd9Sstevel@tonic-gate 			(fs_generic_func_p) fs_sync,
3427c478bd9Sstevel@tonic-gate 			(fs_generic_func_p) fs_sync,	/* No errors allowed */
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 		VFSNAME_VGET, offsetof(vfsops_t, vfs_vget),
3457c478bd9Sstevel@tonic-gate 			fs_nosys, fs_nosys,
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 		VFSNAME_MOUNTROOT, offsetof(vfsops_t, vfs_mountroot),
3487c478bd9Sstevel@tonic-gate 			fs_nosys, fs_nosys,
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 		VFSNAME_FREEVFS, offsetof(vfsops_t, vfs_freevfs),
3517c478bd9Sstevel@tonic-gate 			(fs_generic_func_p)fs_freevfs,
3527c478bd9Sstevel@tonic-gate 			(fs_generic_func_p)fs_freevfs,	/* Shouldn't fail */
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		VFSNAME_VNSTATE, offsetof(vfsops_t, vfs_vnstate),
3557c478bd9Sstevel@tonic-gate 			(fs_generic_func_p)fs_nosys,
3567c478bd9Sstevel@tonic-gate 			(fs_generic_func_p)fs_nosys,
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 		NULL, 0, NULL, NULL
3597c478bd9Sstevel@tonic-gate 	};
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	return (fs_build_vector(actual, unused_ops, vfs_ops_table, template));
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
364e7cbe64fSgw25295 void
365e7cbe64fSgw25295 zfs_boot_init() {
366e7cbe64fSgw25295 
367e7cbe64fSgw25295 	if (strcmp(rootfs.bo_fstype, MNTTYPE_ZFS) == 0)
368e7cbe64fSgw25295 		spa_boot_init();
369e7cbe64fSgw25295 }
370e7cbe64fSgw25295 
3717c478bd9Sstevel@tonic-gate int
3727c478bd9Sstevel@tonic-gate vfs_setfsops(int fstype, const fs_operation_def_t *template, vfsops_t **actual)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	int error;
3757c478bd9Sstevel@tonic-gate 	int unused_ops;
3767c478bd9Sstevel@tonic-gate 
377d320ee2dSrsb 	/*
378d320ee2dSrsb 	 * Verify that fstype refers to a valid fs.  Note that
379d320ee2dSrsb 	 * 0 is valid since it's used to set "stray" ops.
380d320ee2dSrsb 	 */
381d320ee2dSrsb 	if ((fstype < 0) || (fstype >= nfstype))
3827c478bd9Sstevel@tonic-gate 		return (EINVAL);
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if (!ALLOCATED_VFSSW(&vfssw[fstype]))
3857c478bd9Sstevel@tonic-gate 		return (EINVAL);
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/* Set up the operations vector. */
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	error = fs_copyfsops(template, &vfssw[fstype].vsw_vfsops, &unused_ops);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	if (error != 0)
3927c478bd9Sstevel@tonic-gate 		return (error);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	vfssw[fstype].vsw_flag |= VSW_INSTALLED;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	if (actual != NULL)
3977c478bd9Sstevel@tonic-gate 		*actual = &vfssw[fstype].vsw_vfsops;
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate #if DEBUG
4007c478bd9Sstevel@tonic-gate 	if (unused_ops != 0)
4017c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "vfs_setfsops: %s: %d operations supplied "
4027c478bd9Sstevel@tonic-gate 		    "but not used", vfssw[fstype].vsw_name, unused_ops);
4037c478bd9Sstevel@tonic-gate #endif
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	return (0);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate int
4097c478bd9Sstevel@tonic-gate vfs_makefsops(const fs_operation_def_t *template, vfsops_t **actual)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate 	int error;
4127c478bd9Sstevel@tonic-gate 	int unused_ops;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	*actual = (vfsops_t *)kmem_alloc(sizeof (vfsops_t), KM_SLEEP);
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	error = fs_copyfsops(template, *actual, &unused_ops);
4177c478bd9Sstevel@tonic-gate 	if (error != 0) {
4187c478bd9Sstevel@tonic-gate 		kmem_free(*actual, sizeof (vfsops_t));
4197c478bd9Sstevel@tonic-gate 		*actual = NULL;
4207c478bd9Sstevel@tonic-gate 		return (error);
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	return (0);
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate /*
4277c478bd9Sstevel@tonic-gate  * Free a vfsops structure created as a result of vfs_makefsops().
4287c478bd9Sstevel@tonic-gate  * NOTE: For a vfsops structure initialized by vfs_setfsops(), use
4297c478bd9Sstevel@tonic-gate  * vfs_freevfsops_by_type().
4307c478bd9Sstevel@tonic-gate  */
4317c478bd9Sstevel@tonic-gate void
4327c478bd9Sstevel@tonic-gate vfs_freevfsops(vfsops_t *vfsops)
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate 	kmem_free(vfsops, sizeof (vfsops_t));
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate /*
4387c478bd9Sstevel@tonic-gate  * Since the vfsops structure is part of the vfssw table and wasn't
4397c478bd9Sstevel@tonic-gate  * really allocated, we're not really freeing anything.  We keep
4407c478bd9Sstevel@tonic-gate  * the name for consistency with vfs_freevfsops().  We do, however,
4417c478bd9Sstevel@tonic-gate  * need to take care of a little bookkeeping.
4427c478bd9Sstevel@tonic-gate  * NOTE: For a vfsops structure created by vfs_setfsops(), use
4437c478bd9Sstevel@tonic-gate  * vfs_freevfsops_by_type().
4447c478bd9Sstevel@tonic-gate  */
4457c478bd9Sstevel@tonic-gate int
4467c478bd9Sstevel@tonic-gate vfs_freevfsops_by_type(int fstype)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	/* Verify that fstype refers to a loaded fs (and not fsid 0). */
4507c478bd9Sstevel@tonic-gate 	if ((fstype <= 0) || (fstype >= nfstype))
4517c478bd9Sstevel@tonic-gate 		return (EINVAL);
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	WLOCK_VFSSW();
4547c478bd9Sstevel@tonic-gate 	if ((vfssw[fstype].vsw_flag & VSW_INSTALLED) == 0) {
4557c478bd9Sstevel@tonic-gate 		WUNLOCK_VFSSW();
4567c478bd9Sstevel@tonic-gate 		return (EINVAL);
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	vfssw[fstype].vsw_flag &= ~VSW_INSTALLED;
4607c478bd9Sstevel@tonic-gate 	WUNLOCK_VFSSW();
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	return (0);
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate /* Support routines used to reference vfs_op */
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate /* Set the operations vector for a vfs */
4687c478bd9Sstevel@tonic-gate void
4697c478bd9Sstevel@tonic-gate vfs_setops(vfs_t *vfsp, vfsops_t *vfsops)
4707c478bd9Sstevel@tonic-gate {
4717c478bd9Sstevel@tonic-gate 	vfsops_t	*op;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	ASSERT(vfsp != NULL);
4747c478bd9Sstevel@tonic-gate 	ASSERT(vfsops != NULL);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	op = vfsp->vfs_op;
4777c478bd9Sstevel@tonic-gate 	membar_consumer();
478da6c28aaSamw 	if (vfsp->vfs_femhead == NULL &&
47975d94465SJosef 'Jeff' Sipek 	    atomic_cas_ptr(&vfsp->vfs_op, op, vfsops) == op) {
4807c478bd9Sstevel@tonic-gate 		return;
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 	fsem_setvfsops(vfsp, vfsops);
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate /* Retrieve the operations vector for a vfs */
4867c478bd9Sstevel@tonic-gate vfsops_t *
4877c478bd9Sstevel@tonic-gate vfs_getops(vfs_t *vfsp)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	vfsops_t	*op;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	ASSERT(vfsp != NULL);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	op = vfsp->vfs_op;
4947c478bd9Sstevel@tonic-gate 	membar_consumer();
495da6c28aaSamw 	if (vfsp->vfs_femhead == NULL && op == vfsp->vfs_op) {
4967c478bd9Sstevel@tonic-gate 		return (op);
4977c478bd9Sstevel@tonic-gate 	} else {
4987c478bd9Sstevel@tonic-gate 		return (fsem_getvfsops(vfsp));
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate /*
5037c478bd9Sstevel@tonic-gate  * Returns non-zero (1) if the vfsops matches that of the vfs.
5047c478bd9Sstevel@tonic-gate  * Returns zero (0) if not.
5057c478bd9Sstevel@tonic-gate  */
5067c478bd9Sstevel@tonic-gate int
5077c478bd9Sstevel@tonic-gate vfs_matchops(vfs_t *vfsp, vfsops_t *vfsops)
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	return (vfs_getops(vfsp) == vfsops);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate /*
5137c478bd9Sstevel@tonic-gate  * Returns non-zero (1) if the file system has installed a non-default,
5147c478bd9Sstevel@tonic-gate  * non-error vfs_sync routine.  Returns zero (0) otherwise.
5157c478bd9Sstevel@tonic-gate  */
5167c478bd9Sstevel@tonic-gate int
5177c478bd9Sstevel@tonic-gate vfs_can_sync(vfs_t *vfsp)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	/* vfs_sync() routine is not the default/error function */
5207c478bd9Sstevel@tonic-gate 	return (vfs_getops(vfsp)->vfs_sync != fs_sync);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate /*
5247c478bd9Sstevel@tonic-gate  * Initialize a vfs structure.
5257c478bd9Sstevel@tonic-gate  */
5267c478bd9Sstevel@tonic-gate void
5277c478bd9Sstevel@tonic-gate vfs_init(vfs_t *vfsp, vfsops_t *op, void *data)
5287c478bd9Sstevel@tonic-gate {
529da6c28aaSamw 	/* Other initialization has been moved to vfs_alloc() */
5307c478bd9Sstevel@tonic-gate 	vfsp->vfs_count = 0;
5317c478bd9Sstevel@tonic-gate 	vfsp->vfs_next = vfsp;
5327c478bd9Sstevel@tonic-gate 	vfsp->vfs_prev = vfsp;
5337c478bd9Sstevel@tonic-gate 	vfsp->vfs_zone_next = vfsp;
5347c478bd9Sstevel@tonic-gate 	vfsp->vfs_zone_prev = vfsp;
53593239addSjohnlev 	vfsp->vfs_lofi_minor = 0;
5367c478bd9Sstevel@tonic-gate 	sema_init(&vfsp->vfs_reflock, 1, NULL, SEMA_DEFAULT, NULL);
537da6c28aaSamw 	vfsimpl_setup(vfsp);
538da6c28aaSamw 	vfsp->vfs_data = (data);
539da6c28aaSamw 	vfs_setops((vfsp), (op));
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate 
542ddfcde86Srsb /*
543ddfcde86Srsb  * Allocate and initialize the vfs implementation private data
544ddfcde86Srsb  * structure, vfs_impl_t.
545ddfcde86Srsb  */
546ddfcde86Srsb void
547ddfcde86Srsb vfsimpl_setup(vfs_t *vfsp)
548ddfcde86Srsb {
549da6c28aaSamw 	int i;
550da6c28aaSamw 
551da6c28aaSamw 	if (vfsp->vfs_implp != NULL) {
552da6c28aaSamw 		return;
553da6c28aaSamw 	}
554da6c28aaSamw 
555ddfcde86Srsb 	vfsp->vfs_implp = kmem_alloc(sizeof (vfs_impl_t), KM_SLEEP);
556da6c28aaSamw 	/* Note that these are #define'd in vfs.h */
557ddfcde86Srsb 	vfsp->vfs_vskap = NULL;
558ddfcde86Srsb 	vfsp->vfs_fstypevsp = NULL;
559da6c28aaSamw 
560da6c28aaSamw 	/* Set size of counted array, then zero the array */
561da6c28aaSamw 	vfsp->vfs_featureset[0] = VFS_FEATURE_MAXSZ - 1;
562da6c28aaSamw 	for (i = 1; i <  VFS_FEATURE_MAXSZ; i++) {
563da6c28aaSamw 		vfsp->vfs_featureset[i] = 0;
564da6c28aaSamw 	}
565ddfcde86Srsb }
566ddfcde86Srsb 
567ddfcde86Srsb /*
568ddfcde86Srsb  * Release the vfs_impl_t structure, if it exists. Some unbundled
569ddfcde86Srsb  * filesystems may not use the newer version of vfs and thus
570ddfcde86Srsb  * would not contain this implementation private data structure.
571ddfcde86Srsb  */
572ddfcde86Srsb void
573ddfcde86Srsb vfsimpl_teardown(vfs_t *vfsp)
574ddfcde86Srsb {
575ddfcde86Srsb 	vfs_impl_t	*vip = vfsp->vfs_implp;
576ddfcde86Srsb 
577ddfcde86Srsb 	if (vip == NULL)
578ddfcde86Srsb 		return;
579ddfcde86Srsb 
580ddfcde86Srsb 	kmem_free(vfsp->vfs_implp, sizeof (vfs_impl_t));
581ddfcde86Srsb 	vfsp->vfs_implp = NULL;
582ddfcde86Srsb }
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate /*
5857c478bd9Sstevel@tonic-gate  * VFS system calls: mount, umount, syssync, statfs, fstatfs, statvfs,
5867c478bd9Sstevel@tonic-gate  * fstatvfs, and sysfs moved to common/syscall.
5877c478bd9Sstevel@tonic-gate  */
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate /*
5907c478bd9Sstevel@tonic-gate  * Update every mounted file system.  We call the vfs_sync operation of
5917c478bd9Sstevel@tonic-gate  * each file system type, passing it a NULL vfsp to indicate that all
5927c478bd9Sstevel@tonic-gate  * mounted file systems of that type should be updated.
5937c478bd9Sstevel@tonic-gate  */
5947c478bd9Sstevel@tonic-gate void
5957c478bd9Sstevel@tonic-gate vfs_sync(int flag)
5967c478bd9Sstevel@tonic-gate {
5977c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
5987c478bd9Sstevel@tonic-gate 	RLOCK_VFSSW();
5997c478bd9Sstevel@tonic-gate 	for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
6007c478bd9Sstevel@tonic-gate 		if (ALLOCATED_VFSSW(vswp) && VFS_INSTALLED(vswp)) {
6017c478bd9Sstevel@tonic-gate 			vfs_refvfssw(vswp);
6027c478bd9Sstevel@tonic-gate 			RUNLOCK_VFSSW();
6037c478bd9Sstevel@tonic-gate 			(void) (*vswp->vsw_vfsops.vfs_sync)(NULL, flag,
6047c478bd9Sstevel@tonic-gate 			    CRED());
6057c478bd9Sstevel@tonic-gate 			vfs_unrefvfssw(vswp);
6067c478bd9Sstevel@tonic-gate 			RLOCK_VFSSW();
6077c478bd9Sstevel@tonic-gate 		}
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 	RUNLOCK_VFSSW();
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate void
6137c478bd9Sstevel@tonic-gate sync(void)
6147c478bd9Sstevel@tonic-gate {
6157c478bd9Sstevel@tonic-gate 	vfs_sync(0);
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate /*
619147c1346SArne Jansen  * compare function for vfs_by_dev avl tree. compare dev first, then mntix
620147c1346SArne Jansen  */
621147c1346SArne Jansen static int
622147c1346SArne Jansen vfs_cmp_dev(const void *aa, const void *bb)
623147c1346SArne Jansen {
624147c1346SArne Jansen 	const vfs_t *a = aa;
625147c1346SArne Jansen 	const vfs_t *b = bb;
626147c1346SArne Jansen 
627147c1346SArne Jansen 	if (a->vfs_dev < b->vfs_dev)
628147c1346SArne Jansen 		return (-1);
629147c1346SArne Jansen 	if (a->vfs_dev > b->vfs_dev)
630147c1346SArne Jansen 		return (1);
631147c1346SArne Jansen 	if (a->vfs_mntix < b->vfs_mntix)
632147c1346SArne Jansen 		return (-1);
633147c1346SArne Jansen 	if (a->vfs_mntix > b->vfs_mntix)
634147c1346SArne Jansen 		return (1);
635147c1346SArne Jansen 	return (0);
636147c1346SArne Jansen }
637147c1346SArne Jansen 
638147c1346SArne Jansen /*
639147c1346SArne Jansen  * compare function for vfs_by_mntpnt avl tree. compare mntpnt first, then mntix
640147c1346SArne Jansen  */
641147c1346SArne Jansen static int
642147c1346SArne Jansen vfs_cmp_mntpnt(const void *aa, const void *bb)
643147c1346SArne Jansen {
644147c1346SArne Jansen 	const vfs_t *a = aa;
645147c1346SArne Jansen 	const vfs_t *b = bb;
646147c1346SArne Jansen 	int ret;
647147c1346SArne Jansen 
648147c1346SArne Jansen 	ret = strcmp(refstr_value(a->vfs_mntpt), refstr_value(b->vfs_mntpt));
649147c1346SArne Jansen 	if (ret < 0)
650147c1346SArne Jansen 		return (-1);
651147c1346SArne Jansen 	if (ret > 0)
652147c1346SArne Jansen 		return (1);
653147c1346SArne Jansen 	if (a->vfs_mntix < b->vfs_mntix)
654147c1346SArne Jansen 		return (-1);
655147c1346SArne Jansen 	if (a->vfs_mntix > b->vfs_mntix)
656147c1346SArne Jansen 		return (1);
657147c1346SArne Jansen 	return (0);
658147c1346SArne Jansen }
659147c1346SArne Jansen 
660147c1346SArne Jansen /*
6617c478bd9Sstevel@tonic-gate  * External routines.
6627c478bd9Sstevel@tonic-gate  */
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate krwlock_t vfssw_lock;	/* lock accesses to vfssw */
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate /*
6677c478bd9Sstevel@tonic-gate  * Lock for accessing the vfs linked list.  Initialized in vfs_mountroot(),
6687c478bd9Sstevel@tonic-gate  * but otherwise should be accessed only via vfs_list_lock() and
6697c478bd9Sstevel@tonic-gate  * vfs_list_unlock().  Also used to protect the timestamp for mods to the list.
6707c478bd9Sstevel@tonic-gate  */
6717c478bd9Sstevel@tonic-gate static krwlock_t vfslist;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate /*
6747c478bd9Sstevel@tonic-gate  * Mount devfs on /devices. This is done right after root is mounted
6757c478bd9Sstevel@tonic-gate  * to provide device access support for the system
6767c478bd9Sstevel@tonic-gate  */
6777c478bd9Sstevel@tonic-gate static void
6787c478bd9Sstevel@tonic-gate vfs_mountdevices(void)
6797c478bd9Sstevel@tonic-gate {
6807c478bd9Sstevel@tonic-gate 	struct vfssw *vsw;
6817c478bd9Sstevel@tonic-gate 	struct vnode *mvp;
6827c478bd9Sstevel@tonic-gate 	struct mounta mounta = {	/* fake mounta for devfs_mount() */
6837c478bd9Sstevel@tonic-gate 		NULL,
6847c478bd9Sstevel@tonic-gate 		NULL,
6857c478bd9Sstevel@tonic-gate 		MS_SYSSPACE,
6867c478bd9Sstevel@tonic-gate 		NULL,
6877c478bd9Sstevel@tonic-gate 		NULL,
6887c478bd9Sstevel@tonic-gate 		0,
6897c478bd9Sstevel@tonic-gate 		NULL,
6907c478bd9Sstevel@tonic-gate 		0
6917c478bd9Sstevel@tonic-gate 	};
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	/*
6947c478bd9Sstevel@tonic-gate 	 * _init devfs module to fill in the vfssw
6957c478bd9Sstevel@tonic-gate 	 */
6967c478bd9Sstevel@tonic-gate 	if (modload("fs", "devfs") == -1)
697ae115bc7Smrj 		panic("Cannot _init devfs module");
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	/*
7007c478bd9Sstevel@tonic-gate 	 * Hold vfs
7017c478bd9Sstevel@tonic-gate 	 */
7027c478bd9Sstevel@tonic-gate 	RLOCK_VFSSW();
7037c478bd9Sstevel@tonic-gate 	vsw = vfs_getvfsswbyname("devfs");
7047c478bd9Sstevel@tonic-gate 	VFS_INIT(&devices, &vsw->vsw_vfsops, NULL);
7057c478bd9Sstevel@tonic-gate 	VFS_HOLD(&devices);
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	/*
7087c478bd9Sstevel@tonic-gate 	 * Locate mount point
7097c478bd9Sstevel@tonic-gate 	 */
7107c478bd9Sstevel@tonic-gate 	if (lookupname("/devices", UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp))
711ae115bc7Smrj 		panic("Cannot find /devices");
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	/*
7147c478bd9Sstevel@tonic-gate 	 * Perform the mount of /devices
7157c478bd9Sstevel@tonic-gate 	 */
7167c478bd9Sstevel@tonic-gate 	if (VFS_MOUNT(&devices, mvp, &mounta, CRED()))
717ae115bc7Smrj 		panic("Cannot mount /devices");
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	RUNLOCK_VFSSW();
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	/*
7227c478bd9Sstevel@tonic-gate 	 * Set appropriate members and add to vfs list for mnttab display
7237c478bd9Sstevel@tonic-gate 	 */
724d7de0ceaSRobert Harris 	vfs_setresource(&devices, "/devices", 0);
725d7de0ceaSRobert Harris 	vfs_setmntpoint(&devices, "/devices", 0);
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	/*
7287c478bd9Sstevel@tonic-gate 	 * Hold the root of /devices so it won't go away
7297c478bd9Sstevel@tonic-gate 	 */
7307c478bd9Sstevel@tonic-gate 	if (VFS_ROOT(&devices, &devicesdir))
731ae115bc7Smrj 		panic("vfs_mountdevices: not devices root");
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	if (vfs_lock(&devices) != 0) {
734facf4a8dSllai1 		VN_RELE(devicesdir);
7357c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Cannot acquire vfs_lock of /devices");
7367c478bd9Sstevel@tonic-gate 		return;
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	if (vn_vfswlock(mvp) != 0) {
7407c478bd9Sstevel@tonic-gate 		vfs_unlock(&devices);
741facf4a8dSllai1 		VN_RELE(devicesdir);
7427c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Cannot acquire vfswlock of /devices");
7437c478bd9Sstevel@tonic-gate 		return;
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	vfs_add(mvp, &devices, 0);
7477c478bd9Sstevel@tonic-gate 	vn_vfsunlock(mvp);
7487c478bd9Sstevel@tonic-gate 	vfs_unlock(&devices);
749facf4a8dSllai1 	VN_RELE(devicesdir);
750facf4a8dSllai1 }
751facf4a8dSllai1 
752facf4a8dSllai1 /*
753facf4a8dSllai1  * mount the first instance of /dev  to root and remain mounted
754facf4a8dSllai1  */
755facf4a8dSllai1 static void
756facf4a8dSllai1 vfs_mountdev1(void)
757facf4a8dSllai1 {
758facf4a8dSllai1 	struct vfssw *vsw;
759facf4a8dSllai1 	struct vnode *mvp;
760facf4a8dSllai1 	struct mounta mounta = {	/* fake mounta for sdev_mount() */
761facf4a8dSllai1 		NULL,
762facf4a8dSllai1 		NULL,
763facf4a8dSllai1 		MS_SYSSPACE | MS_OVERLAY,
764facf4a8dSllai1 		NULL,
765facf4a8dSllai1 		NULL,
766facf4a8dSllai1 		0,
767facf4a8dSllai1 		NULL,
768facf4a8dSllai1 		0
769facf4a8dSllai1 	};
770facf4a8dSllai1 
771facf4a8dSllai1 	/*
772facf4a8dSllai1 	 * _init dev module to fill in the vfssw
773facf4a8dSllai1 	 */
774facf4a8dSllai1 	if (modload("fs", "dev") == -1)
775facf4a8dSllai1 		cmn_err(CE_PANIC, "Cannot _init dev module\n");
776facf4a8dSllai1 
777facf4a8dSllai1 	/*
778facf4a8dSllai1 	 * Hold vfs
779facf4a8dSllai1 	 */
780facf4a8dSllai1 	RLOCK_VFSSW();
781facf4a8dSllai1 	vsw = vfs_getvfsswbyname("dev");
782facf4a8dSllai1 	VFS_INIT(&dev, &vsw->vsw_vfsops, NULL);
783facf4a8dSllai1 	VFS_HOLD(&dev);
784facf4a8dSllai1 
785facf4a8dSllai1 	/*
786facf4a8dSllai1 	 * Locate mount point
787facf4a8dSllai1 	 */
788facf4a8dSllai1 	if (lookupname("/dev", UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp))
789facf4a8dSllai1 		cmn_err(CE_PANIC, "Cannot find /dev\n");
790facf4a8dSllai1 
791facf4a8dSllai1 	/*
792facf4a8dSllai1 	 * Perform the mount of /dev
793facf4a8dSllai1 	 */
794facf4a8dSllai1 	if (VFS_MOUNT(&dev, mvp, &mounta, CRED()))
795facf4a8dSllai1 		cmn_err(CE_PANIC, "Cannot mount /dev 1\n");
796facf4a8dSllai1 
797facf4a8dSllai1 	RUNLOCK_VFSSW();
798facf4a8dSllai1 
799facf4a8dSllai1 	/*
800facf4a8dSllai1 	 * Set appropriate members and add to vfs list for mnttab display
801facf4a8dSllai1 	 */
802d7de0ceaSRobert Harris 	vfs_setresource(&dev, "/dev", 0);
803d7de0ceaSRobert Harris 	vfs_setmntpoint(&dev, "/dev", 0);
804facf4a8dSllai1 
805facf4a8dSllai1 	/*
806facf4a8dSllai1 	 * Hold the root of /dev so it won't go away
807facf4a8dSllai1 	 */
808facf4a8dSllai1 	if (VFS_ROOT(&dev, &devdir))
809facf4a8dSllai1 		cmn_err(CE_PANIC, "vfs_mountdev1: not dev root");
810facf4a8dSllai1 
811facf4a8dSllai1 	if (vfs_lock(&dev) != 0) {
812facf4a8dSllai1 		VN_RELE(devdir);
813facf4a8dSllai1 		cmn_err(CE_NOTE, "Cannot acquire vfs_lock of /dev");
814facf4a8dSllai1 		return;
815facf4a8dSllai1 	}
816facf4a8dSllai1 
817facf4a8dSllai1 	if (vn_vfswlock(mvp) != 0) {
818facf4a8dSllai1 		vfs_unlock(&dev);
819facf4a8dSllai1 		VN_RELE(devdir);
820facf4a8dSllai1 		cmn_err(CE_NOTE, "Cannot acquire vfswlock of /dev");
821facf4a8dSllai1 		return;
822facf4a8dSllai1 	}
823facf4a8dSllai1 
824facf4a8dSllai1 	vfs_add(mvp, &dev, 0);
825facf4a8dSllai1 	vn_vfsunlock(mvp);
826facf4a8dSllai1 	vfs_unlock(&dev);
827facf4a8dSllai1 	VN_RELE(devdir);
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate /*
8317c478bd9Sstevel@tonic-gate  * Mount required filesystem. This is done right after root is mounted.
8327c478bd9Sstevel@tonic-gate  */
8337c478bd9Sstevel@tonic-gate static void
8347c478bd9Sstevel@tonic-gate vfs_mountfs(char *module, char *spec, char *path)
8357c478bd9Sstevel@tonic-gate {
8367c478bd9Sstevel@tonic-gate 	struct vnode *mvp;
8377c478bd9Sstevel@tonic-gate 	struct mounta mounta;
8387c478bd9Sstevel@tonic-gate 	vfs_t *vfsp;
8397c478bd9Sstevel@tonic-gate 
840*f2ce0b1aSAndrew Stormont 	bzero(&mounta, sizeof (mounta));
8417c478bd9Sstevel@tonic-gate 	mounta.flags = MS_SYSSPACE | MS_DATA;
8427c478bd9Sstevel@tonic-gate 	mounta.fstype = module;
8437c478bd9Sstevel@tonic-gate 	mounta.spec = spec;
8447c478bd9Sstevel@tonic-gate 	mounta.dir = path;
8457c478bd9Sstevel@tonic-gate 	if (lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp)) {
846ae115bc7Smrj 		cmn_err(CE_WARN, "Cannot find %s", path);
8477c478bd9Sstevel@tonic-gate 		return;
8487c478bd9Sstevel@tonic-gate 	}
8497c478bd9Sstevel@tonic-gate 	if (domount(NULL, &mounta, mvp, CRED(), &vfsp))
850ae115bc7Smrj 		cmn_err(CE_WARN, "Cannot mount %s", path);
8517c478bd9Sstevel@tonic-gate 	else
8527c478bd9Sstevel@tonic-gate 		VFS_RELE(vfsp);
8537c478bd9Sstevel@tonic-gate 	VN_RELE(mvp);
8547c478bd9Sstevel@tonic-gate }
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate /*
8577c478bd9Sstevel@tonic-gate  * vfs_mountroot is called by main() to mount the root filesystem.
8587c478bd9Sstevel@tonic-gate  */
8597c478bd9Sstevel@tonic-gate void
8607c478bd9Sstevel@tonic-gate vfs_mountroot(void)
8617c478bd9Sstevel@tonic-gate {
8627c478bd9Sstevel@tonic-gate 	struct vnode	*rvp = NULL;
8637c478bd9Sstevel@tonic-gate 	char		*path;
8647c478bd9Sstevel@tonic-gate 	size_t		plen;
8655a59a8b3Srsb 	struct vfssw	*vswp;
86635a5a358SJonathan Adams 	proc_t		*p;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	rw_init(&vfssw_lock, NULL, RW_DEFAULT, NULL);
8697c478bd9Sstevel@tonic-gate 	rw_init(&vfslist, NULL, RW_DEFAULT, NULL);
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	/*
872147c1346SArne Jansen 	 * Alloc the avl trees for quick indexing via dev and mountpoint
873147c1346SArne Jansen 	 */
874147c1346SArne Jansen 	avl_create(&vfs_by_dev, vfs_cmp_dev, sizeof(vfs_t),
875147c1346SArne Jansen 	    offsetof(vfs_t, vfs_avldev));
876147c1346SArne Jansen 	avl_create(&vfs_by_mntpnt, vfs_cmp_mntpnt, sizeof(vfs_t),
877147c1346SArne Jansen 	    offsetof(vfs_t, vfs_avlmntpnt));
878147c1346SArne Jansen 
879147c1346SArne Jansen 	/*
8807c478bd9Sstevel@tonic-gate 	 * Alloc the vfs hash bucket array and locks
8817c478bd9Sstevel@tonic-gate 	 */
8827c478bd9Sstevel@tonic-gate 	rvfs_list = kmem_zalloc(vfshsz * sizeof (rvfs_t), KM_SLEEP);
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	/*
8857c478bd9Sstevel@tonic-gate 	 * Call machine-dependent routine "rootconf" to choose a root
8867c478bd9Sstevel@tonic-gate 	 * file system type.
8877c478bd9Sstevel@tonic-gate 	 */
8887c478bd9Sstevel@tonic-gate 	if (rootconf())
889ae115bc7Smrj 		panic("vfs_mountroot: cannot mount root");
8907c478bd9Sstevel@tonic-gate 	/*
8917c478bd9Sstevel@tonic-gate 	 * Get vnode for '/'.  Set up rootdir, u.u_rdir and u.u_cdir
8927c478bd9Sstevel@tonic-gate 	 * to point to it.  These are used by lookuppn() so that it
8937c478bd9Sstevel@tonic-gate 	 * knows where to start from ('/' or '.').
8947c478bd9Sstevel@tonic-gate 	 */
895d7de0ceaSRobert Harris 	vfs_setmntpoint(rootvfs, "/", 0);
8967c478bd9Sstevel@tonic-gate 	if (VFS_ROOT(rootvfs, &rootdir))
897ae115bc7Smrj 		panic("vfs_mountroot: no root vnode");
89835a5a358SJonathan Adams 
89935a5a358SJonathan Adams 	/*
90035a5a358SJonathan Adams 	 * At this point, the process tree consists of p0 and possibly some
90135a5a358SJonathan Adams 	 * direct children of p0.  (i.e. there are no grandchildren)
90235a5a358SJonathan Adams 	 *
90335a5a358SJonathan Adams 	 * Walk through them all, setting their current directory.
90435a5a358SJonathan Adams 	 */
90535a5a358SJonathan Adams 	mutex_enter(&pidlock);
90635a5a358SJonathan Adams 	for (p = practive; p != NULL; p = p->p_next) {
90735a5a358SJonathan Adams 		ASSERT(p == &p0 || p->p_parent == &p0);
90835a5a358SJonathan Adams 
90935a5a358SJonathan Adams 		PTOU(p)->u_cdir = rootdir;
91035a5a358SJonathan Adams 		VN_HOLD(PTOU(p)->u_cdir);
91135a5a358SJonathan Adams 		PTOU(p)->u_rdir = NULL;
91235a5a358SJonathan Adams 	}
91335a5a358SJonathan Adams 	mutex_exit(&pidlock);
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	/*
9167c478bd9Sstevel@tonic-gate 	 * Setup the global zone's rootvp, now that it exists.
9177c478bd9Sstevel@tonic-gate 	 */
9187c478bd9Sstevel@tonic-gate 	global_zone->zone_rootvp = rootdir;
9197c478bd9Sstevel@tonic-gate 	VN_HOLD(global_zone->zone_rootvp);
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	/*
9227c478bd9Sstevel@tonic-gate 	 * Notify the module code that it can begin using the
9237c478bd9Sstevel@tonic-gate 	 * root filesystem instead of the boot program's services.
9247c478bd9Sstevel@tonic-gate 	 */
9257c478bd9Sstevel@tonic-gate 	modrootloaded = 1;
926e7cbe64fSgw25295 
927e7cbe64fSgw25295 	/*
928e7cbe64fSgw25295 	 * Special handling for a ZFS root file system.
929e7cbe64fSgw25295 	 */
930e7cbe64fSgw25295 	zfs_boot_init();
931e7cbe64fSgw25295 
9327c478bd9Sstevel@tonic-gate 	/*
9337c478bd9Sstevel@tonic-gate 	 * Set up mnttab information for root
9347c478bd9Sstevel@tonic-gate 	 */
935d7de0ceaSRobert Harris 	vfs_setresource(rootvfs, rootfs.bo_name, 0);
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	/*
9387c478bd9Sstevel@tonic-gate 	 * Notify cluster software that the root filesystem is available.
9397c478bd9Sstevel@tonic-gate 	 */
9407c478bd9Sstevel@tonic-gate 	clboot_mountroot();
9417c478bd9Sstevel@tonic-gate 
9425a59a8b3Srsb 	/* Now that we're all done with the root FS, set up its vopstats */
9435a59a8b3Srsb 	if ((vswp = vfs_getvfsswbyvfsops(vfs_getops(rootvfs))) != NULL) {
9445a59a8b3Srsb 		/* Set flag for statistics collection */
9455a59a8b3Srsb 		if (vswp->vsw_flag & VSW_STATS) {
94682c7f3c4Srsb 			initialize_vopstats(&rootvfs->vfs_vopstats);
9475a59a8b3Srsb 			rootvfs->vfs_flag |= VFS_STATS;
94882c7f3c4Srsb 			rootvfs->vfs_fstypevsp =
94982c7f3c4Srsb 			    get_fstype_vopstats(rootvfs, vswp);
95082c7f3c4Srsb 			rootvfs->vfs_vskap = get_vskstat_anchor(rootvfs);
9515a59a8b3Srsb 		}
9525a59a8b3Srsb 		vfs_unrefvfssw(vswp);
9535a59a8b3Srsb 	}
9545a59a8b3Srsb 
9557c478bd9Sstevel@tonic-gate 	/*
956facf4a8dSllai1 	 * Mount /devices, /dev instance 1, /system/contract, /etc/mnttab,
957a237e38eSth199096 	 * /etc/svc/volatile, /etc/dfs/sharetab, /system/object, and /proc.
9587c478bd9Sstevel@tonic-gate 	 */
9597c478bd9Sstevel@tonic-gate 	vfs_mountdevices();
960facf4a8dSllai1 	vfs_mountdev1();
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	vfs_mountfs("ctfs", "ctfs", CTFS_ROOT);
9637c478bd9Sstevel@tonic-gate 	vfs_mountfs("proc", "/proc", "/proc");
9647c478bd9Sstevel@tonic-gate 	vfs_mountfs("mntfs", "/etc/mnttab", "/etc/mnttab");
9657c478bd9Sstevel@tonic-gate 	vfs_mountfs("tmpfs", "/etc/svc/volatile", "/etc/svc/volatile");
9667c478bd9Sstevel@tonic-gate 	vfs_mountfs("objfs", "objfs", OBJFS_ROOT);
96776ca3cb0SRobert Mustacchi 	vfs_mountfs("bootfs", "bootfs", "/system/boot");
9687c478bd9Sstevel@tonic-gate 
969a237e38eSth199096 	if (getzoneid() == GLOBAL_ZONEID) {
970a237e38eSth199096 		vfs_mountfs("sharefs", "sharefs", "/etc/dfs/sharetab");
971a237e38eSth199096 	}
972a237e38eSth199096 
9737c478bd9Sstevel@tonic-gate #ifdef __sparc
9747c478bd9Sstevel@tonic-gate 	/*
9757c478bd9Sstevel@tonic-gate 	 * This bit of magic can go away when we convert sparc to
9767c478bd9Sstevel@tonic-gate 	 * the new boot architecture based on ramdisk.
9777c478bd9Sstevel@tonic-gate 	 *
9787c478bd9Sstevel@tonic-gate 	 * Booting off a mirrored root volume:
9797c478bd9Sstevel@tonic-gate 	 * At this point, we have booted and mounted root on a
9807c478bd9Sstevel@tonic-gate 	 * single component of the mirror.  Complete the boot
9817c478bd9Sstevel@tonic-gate 	 * by configuring SVM and converting the root to the
9827c478bd9Sstevel@tonic-gate 	 * dev_t of the mirrored root device.  This dev_t conversion
9837c478bd9Sstevel@tonic-gate 	 * only works because the underlying device doesn't change.
9847c478bd9Sstevel@tonic-gate 	 */
9857c478bd9Sstevel@tonic-gate 	if (root_is_svm) {
9867c478bd9Sstevel@tonic-gate 		if (svm_rootconf()) {
987ae115bc7Smrj 			panic("vfs_mountroot: cannot remount root");
9887c478bd9Sstevel@tonic-gate 		}
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 		/*
9917c478bd9Sstevel@tonic-gate 		 * mnttab should reflect the new root device
9927c478bd9Sstevel@tonic-gate 		 */
9937c478bd9Sstevel@tonic-gate 		vfs_lock_wait(rootvfs);
994d7de0ceaSRobert Harris 		vfs_setresource(rootvfs, rootfs.bo_name, 0);
9957c478bd9Sstevel@tonic-gate 		vfs_unlock(rootvfs);
9967c478bd9Sstevel@tonic-gate 	}
9977c478bd9Sstevel@tonic-gate #endif /* __sparc */
9987c478bd9Sstevel@tonic-gate 
999bde2df36SYuri Pankov 	if (strcmp(rootfs.bo_fstype, "zfs") != 0) {
10007c478bd9Sstevel@tonic-gate 		/*
10017c478bd9Sstevel@tonic-gate 		 * Look up the root device via devfs so that a dv_node is
10027c478bd9Sstevel@tonic-gate 		 * created for it. The vnode is never VN_RELE()ed.
10037c478bd9Sstevel@tonic-gate 		 * We allocate more than MAXPATHLEN so that the
10047c478bd9Sstevel@tonic-gate 		 * buffer passed to i_ddi_prompath_to_devfspath() is
10057c478bd9Sstevel@tonic-gate 		 * exactly MAXPATHLEN (the function expects a buffer
10067c478bd9Sstevel@tonic-gate 		 * of that length).
10077c478bd9Sstevel@tonic-gate 		 */
10087c478bd9Sstevel@tonic-gate 		plen = strlen("/devices");
10097c478bd9Sstevel@tonic-gate 		path = kmem_alloc(plen + MAXPATHLEN, KM_SLEEP);
10107c478bd9Sstevel@tonic-gate 		(void) strcpy(path, "/devices");
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 		if (i_ddi_prompath_to_devfspath(rootfs.bo_name, path + plen)
10137c478bd9Sstevel@tonic-gate 		    != DDI_SUCCESS ||
10147c478bd9Sstevel@tonic-gate 		    lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &rvp)) {
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 			/* NUL terminate in case "path" has garbage */
10177c478bd9Sstevel@tonic-gate 			path[plen + MAXPATHLEN - 1] = '\0';
10187c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1019bde2df36SYuri Pankov 			cmn_err(CE_WARN, "!Cannot lookup root device: %s",
1020bde2df36SYuri Pankov 			    path);
10217c478bd9Sstevel@tonic-gate #endif
10227c478bd9Sstevel@tonic-gate 		}
10237c478bd9Sstevel@tonic-gate 		kmem_free(path, plen + MAXPATHLEN);
1024bde2df36SYuri Pankov 	}
1025bde2df36SYuri Pankov 
1026df2381bfSpraks 	vfs_mnttabvp_setup();
10277c478bd9Sstevel@tonic-gate }
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate /*
103093239addSjohnlev  * Check to see if our "block device" is actually a file.  If so,
103193239addSjohnlev  * automatically add a lofi device, and keep track of this fact.
103293239addSjohnlev  */
103393239addSjohnlev static int
103493239addSjohnlev lofi_add(const char *fsname, struct vfs *vfsp,
103593239addSjohnlev     mntopts_t *mntopts, struct mounta *uap)
103693239addSjohnlev {
103793239addSjohnlev 	int fromspace = (uap->flags & MS_SYSSPACE) ?
103893239addSjohnlev 	    UIO_SYSSPACE : UIO_USERSPACE;
103993239addSjohnlev 	struct lofi_ioctl *li = NULL;
104093239addSjohnlev 	struct vnode *vp = NULL;
104193239addSjohnlev 	struct pathname	pn = { NULL };
104293239addSjohnlev 	ldi_ident_t ldi_id;
104393239addSjohnlev 	ldi_handle_t ldi_hdl;
104443d5cd3dSjohnlev 	vfssw_t *vfssw;
104593239addSjohnlev 	int minor;
104693239addSjohnlev 	int err = 0;
104793239addSjohnlev 
10480fbb751dSJohn Levon 	if ((vfssw = vfs_getvfssw(fsname)) == NULL)
104993239addSjohnlev 		return (0);
105043d5cd3dSjohnlev 
105143d5cd3dSjohnlev 	if (!(vfssw->vsw_flag & VSW_CANLOFI)) {
105243d5cd3dSjohnlev 		vfs_unrefvfssw(vfssw);
105393239addSjohnlev 		return (0);
105443d5cd3dSjohnlev 	}
105543d5cd3dSjohnlev 
105643d5cd3dSjohnlev 	vfs_unrefvfssw(vfssw);
105743d5cd3dSjohnlev 	vfssw = NULL;
105893239addSjohnlev 
105993239addSjohnlev 	if (pn_get(uap->spec, fromspace, &pn) != 0)
106093239addSjohnlev 		return (0);
106193239addSjohnlev 
106293239addSjohnlev 	if (lookupname(uap->spec, fromspace, FOLLOW, NULL, &vp) != 0)
106393239addSjohnlev 		goto out;
106493239addSjohnlev 
106593239addSjohnlev 	if (vp->v_type != VREG)
106693239addSjohnlev 		goto out;
106793239addSjohnlev 
106893239addSjohnlev 	/* OK, this is a lofi mount. */
106993239addSjohnlev 
107093239addSjohnlev 	if ((uap->flags & (MS_REMOUNT|MS_GLOBAL)) ||
107193239addSjohnlev 	    vfs_optionisset_nolock(mntopts, MNTOPT_SUID, NULL) ||
107293239addSjohnlev 	    vfs_optionisset_nolock(mntopts, MNTOPT_SETUID, NULL) ||
107393239addSjohnlev 	    vfs_optionisset_nolock(mntopts, MNTOPT_DEVICES, NULL)) {
107493239addSjohnlev 		err = EINVAL;
107593239addSjohnlev 		goto out;
107693239addSjohnlev 	}
107793239addSjohnlev 
107893239addSjohnlev 	ldi_id = ldi_ident_from_anon();
107993239addSjohnlev 	li = kmem_zalloc(sizeof (*li), KM_SLEEP);
10806f02aa44SDina K Nimeh 	(void) strlcpy(li->li_filename, pn.pn_path, MAXPATHLEN);
108193239addSjohnlev 
10820fbb751dSJohn Levon 	err = ldi_open_by_name("/dev/lofictl", FREAD | FWRITE, kcred,
10830fbb751dSJohn Levon 	    &ldi_hdl, ldi_id);
108493239addSjohnlev 
108593239addSjohnlev 	if (err)
108693239addSjohnlev 		goto out2;
108793239addSjohnlev 
108893239addSjohnlev 	err = ldi_ioctl(ldi_hdl, LOFI_MAP_FILE, (intptr_t)li,
10890fbb751dSJohn Levon 	    FREAD | FWRITE | FKIOCTL, kcred, &minor);
109093239addSjohnlev 
10910fbb751dSJohn Levon 	(void) ldi_close(ldi_hdl, FREAD | FWRITE, kcred);
109293239addSjohnlev 
109393239addSjohnlev 	if (!err)
109493239addSjohnlev 		vfsp->vfs_lofi_minor = minor;
109593239addSjohnlev 
109693239addSjohnlev out2:
109793239addSjohnlev 	ldi_ident_release(ldi_id);
109893239addSjohnlev out:
109993239addSjohnlev 	if (li != NULL)
110093239addSjohnlev 		kmem_free(li, sizeof (*li));
110193239addSjohnlev 	if (vp != NULL)
110293239addSjohnlev 		VN_RELE(vp);
110393239addSjohnlev 	pn_free(&pn);
110493239addSjohnlev 	return (err);
110593239addSjohnlev }
110693239addSjohnlev 
110793239addSjohnlev static void
110893239addSjohnlev lofi_remove(struct vfs *vfsp)
110993239addSjohnlev {
111093239addSjohnlev 	struct lofi_ioctl *li = NULL;
111193239addSjohnlev 	ldi_ident_t ldi_id;
111293239addSjohnlev 	ldi_handle_t ldi_hdl;
111393239addSjohnlev 	int err;
111493239addSjohnlev 
111593239addSjohnlev 	if (vfsp->vfs_lofi_minor == 0)
111693239addSjohnlev 		return;
111793239addSjohnlev 
111893239addSjohnlev 	ldi_id = ldi_ident_from_anon();
111993239addSjohnlev 
112093239addSjohnlev 	li = kmem_zalloc(sizeof (*li), KM_SLEEP);
112193239addSjohnlev 	li->li_minor = vfsp->vfs_lofi_minor;
112293239addSjohnlev 	li->li_cleanup = B_TRUE;
112393239addSjohnlev 
11240fbb751dSJohn Levon 	err = ldi_open_by_name("/dev/lofictl", FREAD | FWRITE, kcred,
11250fbb751dSJohn Levon 	    &ldi_hdl, ldi_id);
112693239addSjohnlev 
112793239addSjohnlev 	if (err)
112893239addSjohnlev 		goto out;
112993239addSjohnlev 
113093239addSjohnlev 	err = ldi_ioctl(ldi_hdl, LOFI_UNMAP_FILE_MINOR, (intptr_t)li,
11310fbb751dSJohn Levon 	    FREAD | FWRITE | FKIOCTL, kcred, NULL);
113293239addSjohnlev 
11330fbb751dSJohn Levon 	(void) ldi_close(ldi_hdl, FREAD | FWRITE, kcred);
113493239addSjohnlev 
113593239addSjohnlev 	if (!err)
113693239addSjohnlev 		vfsp->vfs_lofi_minor = 0;
113793239addSjohnlev 
113893239addSjohnlev out:
113993239addSjohnlev 	ldi_ident_release(ldi_id);
114093239addSjohnlev 	if (li != NULL)
114193239addSjohnlev 		kmem_free(li, sizeof (*li));
114293239addSjohnlev }
114393239addSjohnlev 
114493239addSjohnlev /*
11457c478bd9Sstevel@tonic-gate  * Common mount code.  Called from the system call entry point, from autofs,
1146b9238976Sth199096  * nfsv4 trigger mounts, and from pxfs.
11477c478bd9Sstevel@tonic-gate  *
11487c478bd9Sstevel@tonic-gate  * Takes the effective file system type, mount arguments, the mount point
11497c478bd9Sstevel@tonic-gate  * vnode, flags specifying whether the mount is a remount and whether it
11507c478bd9Sstevel@tonic-gate  * should be entered into the vfs list, and credentials.  Fills in its vfspp
11517c478bd9Sstevel@tonic-gate  * parameter with the mounted file system instance's vfs.
11527c478bd9Sstevel@tonic-gate  *
11537c478bd9Sstevel@tonic-gate  * Note that the effective file system type is specified as a string.  It may
11547c478bd9Sstevel@tonic-gate  * be null, in which case it's determined from the mount arguments, and may
11557c478bd9Sstevel@tonic-gate  * differ from the type specified in the mount arguments; this is a hook to
11567c478bd9Sstevel@tonic-gate  * allow interposition when instantiating file system instances.
11577c478bd9Sstevel@tonic-gate  *
11587c478bd9Sstevel@tonic-gate  * The caller is responsible for releasing its own hold on the mount point
11597c478bd9Sstevel@tonic-gate  * vp (this routine does its own hold when necessary).
11607c478bd9Sstevel@tonic-gate  * Also note that for remounts, the mount point vp should be the vnode for
11617c478bd9Sstevel@tonic-gate  * the root of the file system rather than the vnode that the file system
11627c478bd9Sstevel@tonic-gate  * is mounted on top of.
11637c478bd9Sstevel@tonic-gate  */
11647c478bd9Sstevel@tonic-gate int
11657c478bd9Sstevel@tonic-gate domount(char *fsname, struct mounta *uap, vnode_t *vp, struct cred *credp,
11667c478bd9Sstevel@tonic-gate 	struct vfs **vfspp)
11677c478bd9Sstevel@tonic-gate {
11687c478bd9Sstevel@tonic-gate 	struct vfssw	*vswp;
11697c478bd9Sstevel@tonic-gate 	vfsops_t	*vfsops;
11707c478bd9Sstevel@tonic-gate 	struct vfs	*vfsp;
11717c478bd9Sstevel@tonic-gate 	struct vnode	*bvp;
11727c478bd9Sstevel@tonic-gate 	dev_t		bdev = 0;
11737c478bd9Sstevel@tonic-gate 	mntopts_t	mnt_mntopts;
11747c478bd9Sstevel@tonic-gate 	int		error = 0;
11757c478bd9Sstevel@tonic-gate 	int		copyout_error = 0;
11767c478bd9Sstevel@tonic-gate 	int		ovflags;
11777c478bd9Sstevel@tonic-gate 	char		*opts = uap->optptr;
11787c478bd9Sstevel@tonic-gate 	char		*inargs = opts;
11797c478bd9Sstevel@tonic-gate 	int		optlen = uap->optlen;
11807c478bd9Sstevel@tonic-gate 	int		remount;
11817c478bd9Sstevel@tonic-gate 	int		rdonly;
11827c478bd9Sstevel@tonic-gate 	int		nbmand = 0;
11837c478bd9Sstevel@tonic-gate 	int		delmip = 0;
11847c478bd9Sstevel@tonic-gate 	int		addmip = 0;
11857c478bd9Sstevel@tonic-gate 	int		splice = ((uap->flags & MS_NOSPLICE) == 0);
11867c478bd9Sstevel@tonic-gate 	int		fromspace = (uap->flags & MS_SYSSPACE) ?
11877c478bd9Sstevel@tonic-gate 	    UIO_SYSSPACE : UIO_USERSPACE;
11887c478bd9Sstevel@tonic-gate 	char		*resource = NULL, *mountpt = NULL;
11897c478bd9Sstevel@tonic-gate 	refstr_t	*oldresource, *oldmntpt;
11907c478bd9Sstevel@tonic-gate 	struct pathname	pn, rpn;
119182c7f3c4Srsb 	vsk_anchor_t	*vskap;
119293239addSjohnlev 	char fstname[FSTYPSZ];
11935fd5c689SJerry Jelinek 	zone_t		*zone;
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	/*
11967c478bd9Sstevel@tonic-gate 	 * The v_flag value for the mount point vp is permanently set
11977c478bd9Sstevel@tonic-gate 	 * to VVFSLOCK so that no one bypasses the vn_vfs*locks routine
11987c478bd9Sstevel@tonic-gate 	 * for mount point locking.
11997c478bd9Sstevel@tonic-gate 	 */
12007c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
12017c478bd9Sstevel@tonic-gate 	vp->v_flag |= VVFSLOCK;
12027c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	mnt_mntopts.mo_count = 0;
12057c478bd9Sstevel@tonic-gate 	/*
12067c478bd9Sstevel@tonic-gate 	 * Find the ops vector to use to invoke the file system-specific mount
12077c478bd9Sstevel@tonic-gate 	 * method.  If the fsname argument is non-NULL, use it directly.
12087c478bd9Sstevel@tonic-gate 	 * Otherwise, dig the file system type information out of the mount
12097c478bd9Sstevel@tonic-gate 	 * arguments.
12107c478bd9Sstevel@tonic-gate 	 *
12117c478bd9Sstevel@tonic-gate 	 * A side effect is to hold the vfssw entry.
12127c478bd9Sstevel@tonic-gate 	 *
12137c478bd9Sstevel@tonic-gate 	 * Mount arguments can be specified in several ways, which are
12147c478bd9Sstevel@tonic-gate 	 * distinguished by flag bit settings.  The preferred way is to set
12157c478bd9Sstevel@tonic-gate 	 * MS_OPTIONSTR, indicating an 8 argument mount with the file system
12167c478bd9Sstevel@tonic-gate 	 * type supplied as a character string and the last two arguments
12177c478bd9Sstevel@tonic-gate 	 * being a pointer to a character buffer and the size of the buffer.
12187c478bd9Sstevel@tonic-gate 	 * On entry, the buffer holds a null terminated list of options; on
12197c478bd9Sstevel@tonic-gate 	 * return, the string is the list of options the file system
12207c478bd9Sstevel@tonic-gate 	 * recognized. If MS_DATA is set arguments five and six point to a
12217c478bd9Sstevel@tonic-gate 	 * block of binary data which the file system interprets.
12227c478bd9Sstevel@tonic-gate 	 * A further wrinkle is that some callers don't set MS_FSS and MS_DATA
12237c478bd9Sstevel@tonic-gate 	 * consistently with these conventions.  To handle them, we check to
12247c478bd9Sstevel@tonic-gate 	 * see whether the pointer to the file system name has a numeric value
12257c478bd9Sstevel@tonic-gate 	 * less than 256.  If so, we treat it as an index.
12267c478bd9Sstevel@tonic-gate 	 */
12277c478bd9Sstevel@tonic-gate 	if (fsname != NULL) {
12287c478bd9Sstevel@tonic-gate 		if ((vswp = vfs_getvfssw(fsname)) == NULL) {
12297c478bd9Sstevel@tonic-gate 			return (EINVAL);
12307c478bd9Sstevel@tonic-gate 		}
12317c478bd9Sstevel@tonic-gate 	} else if (uap->flags & (MS_OPTIONSTR | MS_DATA | MS_FSS)) {
12327c478bd9Sstevel@tonic-gate 		size_t n;
12337c478bd9Sstevel@tonic-gate 		uint_t fstype;
123493239addSjohnlev 
123593239addSjohnlev 		fsname = fstname;
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 		if ((fstype = (uintptr_t)uap->fstype) < 256) {
12387c478bd9Sstevel@tonic-gate 			RLOCK_VFSSW();
12397c478bd9Sstevel@tonic-gate 			if (fstype == 0 || fstype >= nfstype ||
12407c478bd9Sstevel@tonic-gate 			    !ALLOCATED_VFSSW(&vfssw[fstype])) {
12417c478bd9Sstevel@tonic-gate 				RUNLOCK_VFSSW();
12427c478bd9Sstevel@tonic-gate 				return (EINVAL);
12437c478bd9Sstevel@tonic-gate 			}
124493239addSjohnlev 			(void) strcpy(fsname, vfssw[fstype].vsw_name);
12457c478bd9Sstevel@tonic-gate 			RUNLOCK_VFSSW();
124693239addSjohnlev 			if ((vswp = vfs_getvfssw(fsname)) == NULL)
12477c478bd9Sstevel@tonic-gate 				return (EINVAL);
12487c478bd9Sstevel@tonic-gate 		} else {
12497c478bd9Sstevel@tonic-gate 			/*
12507c478bd9Sstevel@tonic-gate 			 * Handle either kernel or user address space.
12517c478bd9Sstevel@tonic-gate 			 */
12527c478bd9Sstevel@tonic-gate 			if (uap->flags & MS_SYSSPACE) {
125393239addSjohnlev 				error = copystr(uap->fstype, fsname,
12547c478bd9Sstevel@tonic-gate 				    FSTYPSZ, &n);
12557c478bd9Sstevel@tonic-gate 			} else {
125693239addSjohnlev 				error = copyinstr(uap->fstype, fsname,
12577c478bd9Sstevel@tonic-gate 				    FSTYPSZ, &n);
12587c478bd9Sstevel@tonic-gate 			}
12597c478bd9Sstevel@tonic-gate 			if (error) {
12607c478bd9Sstevel@tonic-gate 				if (error == ENAMETOOLONG)
12617c478bd9Sstevel@tonic-gate 					return (EINVAL);
12627c478bd9Sstevel@tonic-gate 				return (error);
12637c478bd9Sstevel@tonic-gate 			}
126493239addSjohnlev 			if ((vswp = vfs_getvfssw(fsname)) == NULL)
12657c478bd9Sstevel@tonic-gate 				return (EINVAL);
12667c478bd9Sstevel@tonic-gate 		}
12677c478bd9Sstevel@tonic-gate 	} else {
12687c478bd9Sstevel@tonic-gate 		if ((vswp = vfs_getvfsswbyvfsops(vfs_getops(rootvfs))) == NULL)
12697c478bd9Sstevel@tonic-gate 			return (EINVAL);
12700fbb751dSJohn Levon 		fsname = vswp->vsw_name;
12717c478bd9Sstevel@tonic-gate 	}
12727c478bd9Sstevel@tonic-gate 	if (!VFS_INSTALLED(vswp))
12737c478bd9Sstevel@tonic-gate 		return (EINVAL);
12740fbb751dSJohn Levon 
12750fbb751dSJohn Levon 	if ((error = secpolicy_fs_allowed_mount(fsname)) != 0)  {
12760fbb751dSJohn Levon 		vfs_unrefvfssw(vswp);
12770fbb751dSJohn Levon 		return (error);
12780fbb751dSJohn Levon 	}
12790fbb751dSJohn Levon 
12807c478bd9Sstevel@tonic-gate 	vfsops = &vswp->vsw_vfsops;
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	vfs_copyopttbl(&vswp->vsw_optproto, &mnt_mntopts);
12837c478bd9Sstevel@tonic-gate 	/*
12847c478bd9Sstevel@tonic-gate 	 * Fetch mount options and parse them for generic vfs options
12857c478bd9Sstevel@tonic-gate 	 */
12867c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_OPTIONSTR) {
12877c478bd9Sstevel@tonic-gate 		/*
12887c478bd9Sstevel@tonic-gate 		 * Limit the buffer size
12897c478bd9Sstevel@tonic-gate 		 */
12907c478bd9Sstevel@tonic-gate 		if (optlen < 0 || optlen > MAX_MNTOPT_STR) {
12917c478bd9Sstevel@tonic-gate 			error = EINVAL;
12927c478bd9Sstevel@tonic-gate 			goto errout;
12937c478bd9Sstevel@tonic-gate 		}
12947c478bd9Sstevel@tonic-gate 		if ((uap->flags & MS_SYSSPACE) == 0) {
12957c478bd9Sstevel@tonic-gate 			inargs = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
12967c478bd9Sstevel@tonic-gate 			inargs[0] = '\0';
12977c478bd9Sstevel@tonic-gate 			if (optlen) {
12987c478bd9Sstevel@tonic-gate 				error = copyinstr(opts, inargs, (size_t)optlen,
12997c478bd9Sstevel@tonic-gate 				    NULL);
13007c478bd9Sstevel@tonic-gate 				if (error) {
13017c478bd9Sstevel@tonic-gate 					goto errout;
13027c478bd9Sstevel@tonic-gate 				}
13037c478bd9Sstevel@tonic-gate 			}
13047c478bd9Sstevel@tonic-gate 		}
13057c478bd9Sstevel@tonic-gate 		vfs_parsemntopts(&mnt_mntopts, inargs, 0);
13067c478bd9Sstevel@tonic-gate 	}
13077c478bd9Sstevel@tonic-gate 	/*
13087c478bd9Sstevel@tonic-gate 	 * Flag bits override the options string.
13097c478bd9Sstevel@tonic-gate 	 */
13107c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_REMOUNT)
13117c478bd9Sstevel@tonic-gate 		vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_REMOUNT, NULL, 0, 0);
13127c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_RDONLY)
13137c478bd9Sstevel@tonic-gate 		vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_RO, NULL, 0, 0);
13147c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_NOSUID)
13157c478bd9Sstevel@tonic-gate 		vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL, 0, 0);
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	/*
13187c478bd9Sstevel@tonic-gate 	 * Check if this is a remount; must be set in the option string and
13197c478bd9Sstevel@tonic-gate 	 * the file system must support a remount option.
13207c478bd9Sstevel@tonic-gate 	 */
13217c478bd9Sstevel@tonic-gate 	if (remount = vfs_optionisset_nolock(&mnt_mntopts,
13227c478bd9Sstevel@tonic-gate 	    MNTOPT_REMOUNT, NULL)) {
13237c478bd9Sstevel@tonic-gate 		if (!(vswp->vsw_flag & VSW_CANREMOUNT)) {
13247c478bd9Sstevel@tonic-gate 			error = ENOTSUP;
13257c478bd9Sstevel@tonic-gate 			goto errout;
13267c478bd9Sstevel@tonic-gate 		}
13277c478bd9Sstevel@tonic-gate 		uap->flags |= MS_REMOUNT;
13287c478bd9Sstevel@tonic-gate 	}
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	/*
13317c478bd9Sstevel@tonic-gate 	 * uap->flags and vfs_optionisset() should agree.
13327c478bd9Sstevel@tonic-gate 	 */
13337c478bd9Sstevel@tonic-gate 	if (rdonly = vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_RO, NULL)) {
13347c478bd9Sstevel@tonic-gate 		uap->flags |= MS_RDONLY;
13357c478bd9Sstevel@tonic-gate 	}
13367c478bd9Sstevel@tonic-gate 	if (vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL)) {
13377c478bd9Sstevel@tonic-gate 		uap->flags |= MS_NOSUID;
13387c478bd9Sstevel@tonic-gate 	}
13397c478bd9Sstevel@tonic-gate 	nbmand = vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_NBMAND, NULL);
13407c478bd9Sstevel@tonic-gate 	ASSERT(splice || !remount);
13417c478bd9Sstevel@tonic-gate 	/*
13427c478bd9Sstevel@tonic-gate 	 * If we are splicing the fs into the namespace,
13437c478bd9Sstevel@tonic-gate 	 * perform mount point checks.
13447c478bd9Sstevel@tonic-gate 	 *
13457c478bd9Sstevel@tonic-gate 	 * We want to resolve the path for the mount point to eliminate
13467c478bd9Sstevel@tonic-gate 	 * '.' and ".." and symlinks in mount points; we can't do the
13477c478bd9Sstevel@tonic-gate 	 * same for the resource string, since it would turn
13487c478bd9Sstevel@tonic-gate 	 * "/dev/dsk/c0t0d0s0" into "/devices/pci@...".  We need to do
13497c478bd9Sstevel@tonic-gate 	 * this before grabbing vn_vfswlock(), because otherwise we
13507c478bd9Sstevel@tonic-gate 	 * would deadlock with lookuppn().
13517c478bd9Sstevel@tonic-gate 	 */
13527c478bd9Sstevel@tonic-gate 	if (splice) {
13537c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_count > 0);
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 		/*
13567c478bd9Sstevel@tonic-gate 		 * Pick up mount point and device from appropriate space.
13577c478bd9Sstevel@tonic-gate 		 */
13587c478bd9Sstevel@tonic-gate 		if (pn_get(uap->spec, fromspace, &pn) == 0) {
13597c478bd9Sstevel@tonic-gate 			resource = kmem_alloc(pn.pn_pathlen + 1,
13607c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
13617c478bd9Sstevel@tonic-gate 			(void) strcpy(resource, pn.pn_path);
13627c478bd9Sstevel@tonic-gate 			pn_free(&pn);
13637c478bd9Sstevel@tonic-gate 		}
13647c478bd9Sstevel@tonic-gate 		/*
13657c478bd9Sstevel@tonic-gate 		 * Do a lookupname prior to taking the
13667c478bd9Sstevel@tonic-gate 		 * writelock. Mark this as completed if
13677c478bd9Sstevel@tonic-gate 		 * successful for later cleanup and addition to
13687c478bd9Sstevel@tonic-gate 		 * the mount in progress table.
13697c478bd9Sstevel@tonic-gate 		 */
13707c478bd9Sstevel@tonic-gate 		if ((uap->flags & MS_GLOBAL) == 0 &&
13717c478bd9Sstevel@tonic-gate 		    lookupname(uap->spec, fromspace,
13727c478bd9Sstevel@tonic-gate 		    FOLLOW, NULL, &bvp) == 0) {
13737c478bd9Sstevel@tonic-gate 			addmip = 1;
13747c478bd9Sstevel@tonic-gate 		}
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 		if ((error = pn_get(uap->dir, fromspace, &pn)) == 0) {
13777c478bd9Sstevel@tonic-gate 			pathname_t *pnp;
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 			if (*pn.pn_path != '/') {
13807c478bd9Sstevel@tonic-gate 				error = EINVAL;
13817c478bd9Sstevel@tonic-gate 				pn_free(&pn);
13827c478bd9Sstevel@tonic-gate 				goto errout;
13837c478bd9Sstevel@tonic-gate 			}
13847c478bd9Sstevel@tonic-gate 			pn_alloc(&rpn);
13857c478bd9Sstevel@tonic-gate 			/*
13867c478bd9Sstevel@tonic-gate 			 * Kludge to prevent autofs from deadlocking with
13877c478bd9Sstevel@tonic-gate 			 * itself when it calls domount().
13887c478bd9Sstevel@tonic-gate 			 *
13897c478bd9Sstevel@tonic-gate 			 * If autofs is calling, it is because it is doing
13907c478bd9Sstevel@tonic-gate 			 * (autofs) mounts in the process of an NFS mount.  A
13917c478bd9Sstevel@tonic-gate 			 * lookuppn() here would cause us to block waiting for
13927c478bd9Sstevel@tonic-gate 			 * said NFS mount to complete, which can't since this
13937c478bd9Sstevel@tonic-gate 			 * is the thread that was supposed to doing it.
13947c478bd9Sstevel@tonic-gate 			 */
13957c478bd9Sstevel@tonic-gate 			if (fromspace == UIO_USERSPACE) {
13967c478bd9Sstevel@tonic-gate 				if ((error = lookuppn(&pn, &rpn, FOLLOW, NULL,
13977c478bd9Sstevel@tonic-gate 				    NULL)) == 0) {
13987c478bd9Sstevel@tonic-gate 					pnp = &rpn;
13997c478bd9Sstevel@tonic-gate 				} else {
14007c478bd9Sstevel@tonic-gate 					/*
14017c478bd9Sstevel@tonic-gate 					 * The file disappeared or otherwise
14027c478bd9Sstevel@tonic-gate 					 * became inaccessible since we opened
14037c478bd9Sstevel@tonic-gate 					 * it; might as well fail the mount
14047c478bd9Sstevel@tonic-gate 					 * since the mount point is no longer
14057c478bd9Sstevel@tonic-gate 					 * accessible.
14067c478bd9Sstevel@tonic-gate 					 */
14077c478bd9Sstevel@tonic-gate 					pn_free(&rpn);
14087c478bd9Sstevel@tonic-gate 					pn_free(&pn);
14097c478bd9Sstevel@tonic-gate 					goto errout;
14107c478bd9Sstevel@tonic-gate 				}
14117c478bd9Sstevel@tonic-gate 			} else {
14127c478bd9Sstevel@tonic-gate 				pnp = &pn;
14137c478bd9Sstevel@tonic-gate 			}
14147c478bd9Sstevel@tonic-gate 			mountpt = kmem_alloc(pnp->pn_pathlen + 1, KM_SLEEP);
14157c478bd9Sstevel@tonic-gate 			(void) strcpy(mountpt, pnp->pn_path);
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 			/*
14187c478bd9Sstevel@tonic-gate 			 * If the addition of the zone's rootpath
14197c478bd9Sstevel@tonic-gate 			 * would push us over a total path length
14207c478bd9Sstevel@tonic-gate 			 * of MAXPATHLEN, we fail the mount with
14217c478bd9Sstevel@tonic-gate 			 * ENAMETOOLONG, which is what we would have
14227c478bd9Sstevel@tonic-gate 			 * gotten if we were trying to perform the same
14237c478bd9Sstevel@tonic-gate 			 * mount in the global zone.
14247c478bd9Sstevel@tonic-gate 			 *
14257c478bd9Sstevel@tonic-gate 			 * strlen() doesn't count the trailing
14267c478bd9Sstevel@tonic-gate 			 * '\0', but zone_rootpathlen counts both a
14277c478bd9Sstevel@tonic-gate 			 * trailing '/' and the terminating '\0'.
14287c478bd9Sstevel@tonic-gate 			 */
14297c478bd9Sstevel@tonic-gate 			if ((curproc->p_zone->zone_rootpathlen - 1 +
14307c478bd9Sstevel@tonic-gate 			    strlen(mountpt)) > MAXPATHLEN ||
14317c478bd9Sstevel@tonic-gate 			    (resource != NULL &&
14327c478bd9Sstevel@tonic-gate 			    (curproc->p_zone->zone_rootpathlen - 1 +
14337c478bd9Sstevel@tonic-gate 			    strlen(resource)) > MAXPATHLEN)) {
14347c478bd9Sstevel@tonic-gate 				error = ENAMETOOLONG;
14357c478bd9Sstevel@tonic-gate 			}
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 			pn_free(&rpn);
14387c478bd9Sstevel@tonic-gate 			pn_free(&pn);
14397c478bd9Sstevel@tonic-gate 		}
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 		if (error)
14427c478bd9Sstevel@tonic-gate 			goto errout;
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 		/*
14457c478bd9Sstevel@tonic-gate 		 * Prevent path name resolution from proceeding past
14467c478bd9Sstevel@tonic-gate 		 * the mount point.
14477c478bd9Sstevel@tonic-gate 		 */
14487c478bd9Sstevel@tonic-gate 		if (vn_vfswlock(vp) != 0) {
14497c478bd9Sstevel@tonic-gate 			error = EBUSY;
14507c478bd9Sstevel@tonic-gate 			goto errout;
14517c478bd9Sstevel@tonic-gate 		}
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 		/*
14547c478bd9Sstevel@tonic-gate 		 * Verify that it's legitimate to establish a mount on
14557c478bd9Sstevel@tonic-gate 		 * the prospective mount point.
14567c478bd9Sstevel@tonic-gate 		 */
14577c478bd9Sstevel@tonic-gate 		if (vn_mountedvfs(vp) != NULL) {
14587c478bd9Sstevel@tonic-gate 			/*
14597c478bd9Sstevel@tonic-gate 			 * The mount point lock was obtained after some
14607c478bd9Sstevel@tonic-gate 			 * other thread raced through and established a mount.
14617c478bd9Sstevel@tonic-gate 			 */
14627c478bd9Sstevel@tonic-gate 			vn_vfsunlock(vp);
14637c478bd9Sstevel@tonic-gate 			error = EBUSY;
14647c478bd9Sstevel@tonic-gate 			goto errout;
14657c478bd9Sstevel@tonic-gate 		}
14667c478bd9Sstevel@tonic-gate 		if (vp->v_flag & VNOMOUNT) {
14677c478bd9Sstevel@tonic-gate 			vn_vfsunlock(vp);
14687c478bd9Sstevel@tonic-gate 			error = EINVAL;
14697c478bd9Sstevel@tonic-gate 			goto errout;
14707c478bd9Sstevel@tonic-gate 		}
14717c478bd9Sstevel@tonic-gate 	}
14727c478bd9Sstevel@tonic-gate 	if ((uap->flags & (MS_DATA | MS_OPTIONSTR)) == 0) {
14737c478bd9Sstevel@tonic-gate 		uap->dataptr = NULL;
14747c478bd9Sstevel@tonic-gate 		uap->datalen = 0;
14757c478bd9Sstevel@tonic-gate 	}
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 	/*
14787c478bd9Sstevel@tonic-gate 	 * If this is a remount, we don't want to create a new VFS.
14797c478bd9Sstevel@tonic-gate 	 * Instead, we pass the existing one with a remount flag.
14807c478bd9Sstevel@tonic-gate 	 */
14817c478bd9Sstevel@tonic-gate 	if (remount) {
14827c478bd9Sstevel@tonic-gate 		/*
14837c478bd9Sstevel@tonic-gate 		 * Confirm that the mount point is the root vnode of the
14847c478bd9Sstevel@tonic-gate 		 * file system that is being remounted.
14857c478bd9Sstevel@tonic-gate 		 * This can happen if the user specifies a different
14867c478bd9Sstevel@tonic-gate 		 * mount point directory pathname in the (re)mount command.
14877c478bd9Sstevel@tonic-gate 		 *
14887c478bd9Sstevel@tonic-gate 		 * Code below can only be reached if splice is true, so it's
14897c478bd9Sstevel@tonic-gate 		 * safe to do vn_vfsunlock() here.
14907c478bd9Sstevel@tonic-gate 		 */
14917c478bd9Sstevel@tonic-gate 		if ((vp->v_flag & VROOT) == 0) {
14927c478bd9Sstevel@tonic-gate 			vn_vfsunlock(vp);
14937c478bd9Sstevel@tonic-gate 			error = ENOENT;
14947c478bd9Sstevel@tonic-gate 			goto errout;
14957c478bd9Sstevel@tonic-gate 		}
14967c478bd9Sstevel@tonic-gate 		/*
14977c478bd9Sstevel@tonic-gate 		 * Disallow making file systems read-only unless file system
14987c478bd9Sstevel@tonic-gate 		 * explicitly allows it in its vfssw.  Ignore other flags.
14997c478bd9Sstevel@tonic-gate 		 */
15007c478bd9Sstevel@tonic-gate 		if (rdonly && vn_is_readonly(vp) == 0 &&
15017c478bd9Sstevel@tonic-gate 		    (vswp->vsw_flag & VSW_CANRWRO) == 0) {
15027c478bd9Sstevel@tonic-gate 			vn_vfsunlock(vp);
15037c478bd9Sstevel@tonic-gate 			error = EINVAL;
15047c478bd9Sstevel@tonic-gate 			goto errout;
15057c478bd9Sstevel@tonic-gate 		}
15067c478bd9Sstevel@tonic-gate 		/*
1507da6c28aaSamw 		 * Disallow changing the NBMAND disposition of the file
1508da6c28aaSamw 		 * system on remounts.
15097c478bd9Sstevel@tonic-gate 		 */
15107c478bd9Sstevel@tonic-gate 		if ((nbmand && ((vp->v_vfsp->vfs_flag & VFS_NBMAND) == 0)) ||
15117c478bd9Sstevel@tonic-gate 		    (!nbmand && (vp->v_vfsp->vfs_flag & VFS_NBMAND))) {
1512da6c28aaSamw 			vn_vfsunlock(vp);
1513da6c28aaSamw 			error = EINVAL;
1514da6c28aaSamw 			goto errout;
15157c478bd9Sstevel@tonic-gate 		}
15167c478bd9Sstevel@tonic-gate 		vfsp = vp->v_vfsp;
15177c478bd9Sstevel@tonic-gate 		ovflags = vfsp->vfs_flag;
15187c478bd9Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_REMOUNT;
15197c478bd9Sstevel@tonic-gate 		vfsp->vfs_flag &= ~VFS_RDONLY;
15207c478bd9Sstevel@tonic-gate 	} else {
1521da6c28aaSamw 		vfsp = vfs_alloc(KM_SLEEP);
15227c478bd9Sstevel@tonic-gate 		VFS_INIT(vfsp, vfsops, NULL);
15237c478bd9Sstevel@tonic-gate 	}
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	VFS_HOLD(vfsp);
15267c478bd9Sstevel@tonic-gate 
152793239addSjohnlev 	if ((error = lofi_add(fsname, vfsp, &mnt_mntopts, uap)) != 0) {
152893239addSjohnlev 		if (!remount) {
152993239addSjohnlev 			if (splice)
153093239addSjohnlev 				vn_vfsunlock(vp);
153193239addSjohnlev 			vfs_free(vfsp);
153293239addSjohnlev 		} else {
153393239addSjohnlev 			vn_vfsunlock(vp);
153493239addSjohnlev 			VFS_RELE(vfsp);
153593239addSjohnlev 		}
153693239addSjohnlev 		goto errout;
153793239addSjohnlev 	}
153893239addSjohnlev 
153993239addSjohnlev 	/*
154093239addSjohnlev 	 * PRIV_SYS_MOUNT doesn't mean you can become root.
154193239addSjohnlev 	 */
154293239addSjohnlev 	if (vfsp->vfs_lofi_minor != 0) {
154393239addSjohnlev 		uap->flags |= MS_NOSUID;
154493239addSjohnlev 		vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL, 0, 0);
154593239addSjohnlev 	}
154693239addSjohnlev 
15477c478bd9Sstevel@tonic-gate 	/*
15487c478bd9Sstevel@tonic-gate 	 * The vfs_reflock is not used anymore the code below explicitly
15497c478bd9Sstevel@tonic-gate 	 * holds it preventing others accesing it directly.
15507c478bd9Sstevel@tonic-gate 	 */
15517c478bd9Sstevel@tonic-gate 	if ((sema_tryp(&vfsp->vfs_reflock) == 0) &&
15527c478bd9Sstevel@tonic-gate 	    !(vfsp->vfs_flag & VFS_REMOUNT))
15537c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
1554ae115bc7Smrj 		    "mount type %s couldn't get vfs_reflock", vswp->vsw_name);
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 	/*
15577c478bd9Sstevel@tonic-gate 	 * Lock the vfs. If this is a remount we want to avoid spurious umount
15587c478bd9Sstevel@tonic-gate 	 * failures that happen as a side-effect of fsflush() and other mount
15597c478bd9Sstevel@tonic-gate 	 * and unmount operations that might be going on simultaneously and
15607c478bd9Sstevel@tonic-gate 	 * may have locked the vfs currently. To not return EBUSY immediately
15617c478bd9Sstevel@tonic-gate 	 * here we use vfs_lock_wait() instead vfs_lock() for the remount case.
15627c478bd9Sstevel@tonic-gate 	 */
15637c478bd9Sstevel@tonic-gate 	if (!remount) {
15647c478bd9Sstevel@tonic-gate 		if (error = vfs_lock(vfsp)) {
15657c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag = ovflags;
156693239addSjohnlev 
156793239addSjohnlev 			lofi_remove(vfsp);
156893239addSjohnlev 
15697c478bd9Sstevel@tonic-gate 			if (splice)
15707c478bd9Sstevel@tonic-gate 				vn_vfsunlock(vp);
1571da6c28aaSamw 			vfs_free(vfsp);
15727c478bd9Sstevel@tonic-gate 			goto errout;
15737c478bd9Sstevel@tonic-gate 		}
15747c478bd9Sstevel@tonic-gate 	} else {
15757c478bd9Sstevel@tonic-gate 		vfs_lock_wait(vfsp);
15767c478bd9Sstevel@tonic-gate 	}
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	/*
15797c478bd9Sstevel@tonic-gate 	 * Add device to mount in progress table, global mounts require special
15807c478bd9Sstevel@tonic-gate 	 * handling. It is possible that we have already done the lookupname
15817c478bd9Sstevel@tonic-gate 	 * on a spliced, non-global fs. If so, we don't want to do it again
15827c478bd9Sstevel@tonic-gate 	 * since we cannot do a lookupname after taking the
15837c478bd9Sstevel@tonic-gate 	 * wlock above. This case is for a non-spliced, non-global filesystem.
15847c478bd9Sstevel@tonic-gate 	 */
15857c478bd9Sstevel@tonic-gate 	if (!addmip) {
15867c478bd9Sstevel@tonic-gate 		if ((uap->flags & MS_GLOBAL) == 0 &&
15877c478bd9Sstevel@tonic-gate 		    lookupname(uap->spec, fromspace, FOLLOW, NULL, &bvp) == 0) {
15887c478bd9Sstevel@tonic-gate 			addmip = 1;
15897c478bd9Sstevel@tonic-gate 		}
15907c478bd9Sstevel@tonic-gate 	}
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	if (addmip) {
159393239addSjohnlev 		vnode_t *lvp = NULL;
159493239addSjohnlev 
159593239addSjohnlev 		error = vfs_get_lofi(vfsp, &lvp);
159693239addSjohnlev 		if (error > 0) {
159793239addSjohnlev 			lofi_remove(vfsp);
159893239addSjohnlev 
159993239addSjohnlev 			if (splice)
160093239addSjohnlev 				vn_vfsunlock(vp);
160193239addSjohnlev 			vfs_unlock(vfsp);
160293239addSjohnlev 
160393239addSjohnlev 			if (remount) {
160493239addSjohnlev 				VFS_RELE(vfsp);
160593239addSjohnlev 			} else {
160693239addSjohnlev 				vfs_free(vfsp);
160793239addSjohnlev 			}
160893239addSjohnlev 
160993239addSjohnlev 			goto errout;
161093239addSjohnlev 		} else if (error == -1) {
16117c478bd9Sstevel@tonic-gate 			bdev = bvp->v_rdev;
16127c478bd9Sstevel@tonic-gate 			VN_RELE(bvp);
161393239addSjohnlev 		} else {
161493239addSjohnlev 			bdev = lvp->v_rdev;
161593239addSjohnlev 			VN_RELE(lvp);
161693239addSjohnlev 			VN_RELE(bvp);
161793239addSjohnlev 		}
161893239addSjohnlev 
16197c478bd9Sstevel@tonic-gate 		vfs_addmip(bdev, vfsp);
16207c478bd9Sstevel@tonic-gate 		addmip = 0;
16217c478bd9Sstevel@tonic-gate 		delmip = 1;
16227c478bd9Sstevel@tonic-gate 	}
16237c478bd9Sstevel@tonic-gate 	/*
16247c478bd9Sstevel@tonic-gate 	 * Invalidate cached entry for the mount point.
16257c478bd9Sstevel@tonic-gate 	 */
16267c478bd9Sstevel@tonic-gate 	if (splice)
16277c478bd9Sstevel@tonic-gate 		dnlc_purge_vp(vp);
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 	/*
16307c478bd9Sstevel@tonic-gate 	 * If have an option string but the filesystem doesn't supply a
16317c478bd9Sstevel@tonic-gate 	 * prototype options table, create a table with the global
16327c478bd9Sstevel@tonic-gate 	 * options and sufficient room to accept all the options in the
16337c478bd9Sstevel@tonic-gate 	 * string.  Then parse the passed in option string
16347c478bd9Sstevel@tonic-gate 	 * accepting all the options in the string.  This gives us an
16357c478bd9Sstevel@tonic-gate 	 * option table with all the proper cancel properties for the
16367c478bd9Sstevel@tonic-gate 	 * global options.
16377c478bd9Sstevel@tonic-gate 	 *
16387c478bd9Sstevel@tonic-gate 	 * Filesystems that supply a prototype options table are handled
16397c478bd9Sstevel@tonic-gate 	 * earlier in this function.
16407c478bd9Sstevel@tonic-gate 	 */
16417c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_OPTIONSTR) {
16427c478bd9Sstevel@tonic-gate 		if (!(vswp->vsw_flag & VSW_HASPROTO)) {
16437c478bd9Sstevel@tonic-gate 			mntopts_t tmp_mntopts;
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 			tmp_mntopts.mo_count = 0;
16467c478bd9Sstevel@tonic-gate 			vfs_createopttbl_extend(&tmp_mntopts, inargs,
16477c478bd9Sstevel@tonic-gate 			    &mnt_mntopts);
16487c478bd9Sstevel@tonic-gate 			vfs_parsemntopts(&tmp_mntopts, inargs, 1);
16497c478bd9Sstevel@tonic-gate 			vfs_swapopttbl_nolock(&mnt_mntopts, &tmp_mntopts);
16507c478bd9Sstevel@tonic-gate 			vfs_freeopttbl(&tmp_mntopts);
16517c478bd9Sstevel@tonic-gate 		}
16527c478bd9Sstevel@tonic-gate 	}
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 	/*
16555fd5c689SJerry Jelinek 	 * Serialize with zone state transitions.
16565fd5c689SJerry Jelinek 	 * See vfs_list_add; zone mounted into is:
16575fd5c689SJerry Jelinek 	 * 	zone_find_by_path(refstr_value(vfsp->vfs_mntpt))
16585fd5c689SJerry Jelinek 	 * not the zone doing the mount (curproc->p_zone), but if we're already
16595fd5c689SJerry Jelinek 	 * inside a NGZ, then we know what zone we are.
16607c478bd9Sstevel@tonic-gate 	 */
16615fd5c689SJerry Jelinek 	if (INGLOBALZONE(curproc)) {
16625fd5c689SJerry Jelinek 		zone = zone_find_by_path(mountpt);
16635fd5c689SJerry Jelinek 		ASSERT(zone != NULL);
16645fd5c689SJerry Jelinek 	} else {
16655fd5c689SJerry Jelinek 		zone = curproc->p_zone;
16665fd5c689SJerry Jelinek 		/*
16675fd5c689SJerry Jelinek 		 * zone_find_by_path does a hold, so do one here too so that
16685fd5c689SJerry Jelinek 		 * we can do a zone_rele after mount_completed.
16695fd5c689SJerry Jelinek 		 */
16705fd5c689SJerry Jelinek 		zone_hold(zone);
16715fd5c689SJerry Jelinek 	}
16725fd5c689SJerry Jelinek 	mount_in_progress(zone);
16737c478bd9Sstevel@tonic-gate 	/*
16747c478bd9Sstevel@tonic-gate 	 * Instantiate (or reinstantiate) the file system.  If appropriate,
16757c478bd9Sstevel@tonic-gate 	 * splice it into the file system name space.
16767c478bd9Sstevel@tonic-gate 	 *
16777c478bd9Sstevel@tonic-gate 	 * We want VFS_MOUNT() to be able to override the vfs_resource
16787c478bd9Sstevel@tonic-gate 	 * string if necessary (ie, mntfs), and also for a remount to
16797c478bd9Sstevel@tonic-gate 	 * change the same (necessary when remounting '/' during boot).
16807c478bd9Sstevel@tonic-gate 	 * So we set up vfs_mntpt and vfs_resource to what we think they
16817c478bd9Sstevel@tonic-gate 	 * should be, then hand off control to VFS_MOUNT() which can
16827c478bd9Sstevel@tonic-gate 	 * override this.
16837c478bd9Sstevel@tonic-gate 	 *
16847c478bd9Sstevel@tonic-gate 	 * For safety's sake, when changing vfs_resource or vfs_mntpt of
16857c478bd9Sstevel@tonic-gate 	 * a vfs which is on the vfs list (i.e. during a remount), we must
16867c478bd9Sstevel@tonic-gate 	 * never set those fields to NULL. Several bits of code make
16877c478bd9Sstevel@tonic-gate 	 * assumptions that the fields are always valid.
16887c478bd9Sstevel@tonic-gate 	 */
16897c478bd9Sstevel@tonic-gate 	vfs_swapopttbl(&mnt_mntopts, &vfsp->vfs_mntopts);
16907c478bd9Sstevel@tonic-gate 	if (remount) {
16917c478bd9Sstevel@tonic-gate 		if ((oldresource = vfsp->vfs_resource) != NULL)
16927c478bd9Sstevel@tonic-gate 			refstr_hold(oldresource);
16937c478bd9Sstevel@tonic-gate 		if ((oldmntpt = vfsp->vfs_mntpt) != NULL)
16947c478bd9Sstevel@tonic-gate 			refstr_hold(oldmntpt);
16957c478bd9Sstevel@tonic-gate 	}
1696d7de0ceaSRobert Harris 	vfs_setresource(vfsp, resource, 0);
1697d7de0ceaSRobert Harris 	vfs_setmntpoint(vfsp, mountpt, 0);
16987c478bd9Sstevel@tonic-gate 
1699df2381bfSpraks 	/*
1700df2381bfSpraks 	 * going to mount on this vnode, so notify.
1701df2381bfSpraks 	 */
1702da6c28aaSamw 	vnevent_mountedover(vp, NULL);
17037c478bd9Sstevel@tonic-gate 	error = VFS_MOUNT(vfsp, vp, uap, credp);
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_RDONLY)
17067c478bd9Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
17077c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_NOSUID)
17087c478bd9Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_NOSUID, NULL, 0);
17097c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_GLOBAL)
17107c478bd9Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_GLOBAL, NULL, 0);
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	if (error) {
171393239addSjohnlev 		lofi_remove(vfsp);
171493239addSjohnlev 
17157c478bd9Sstevel@tonic-gate 		if (remount) {
17167c478bd9Sstevel@tonic-gate 			/* put back pre-remount options */
17177c478bd9Sstevel@tonic-gate 			vfs_swapopttbl(&mnt_mntopts, &vfsp->vfs_mntopts);
1718d7de0ceaSRobert Harris 			vfs_setmntpoint(vfsp, refstr_value(oldmntpt),
1719d7de0ceaSRobert Harris 			    VFSSP_VERBATIM);
17207c478bd9Sstevel@tonic-gate 			if (oldmntpt)
17217c478bd9Sstevel@tonic-gate 				refstr_rele(oldmntpt);
1722d7de0ceaSRobert Harris 			vfs_setresource(vfsp, refstr_value(oldresource),
1723d7de0ceaSRobert Harris 			    VFSSP_VERBATIM);
17247c478bd9Sstevel@tonic-gate 			if (oldresource)
17257c478bd9Sstevel@tonic-gate 				refstr_rele(oldresource);
17267c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag = ovflags;
17277c478bd9Sstevel@tonic-gate 			vfs_unlock(vfsp);
17287c478bd9Sstevel@tonic-gate 			VFS_RELE(vfsp);
17297c478bd9Sstevel@tonic-gate 		} else {
17307c478bd9Sstevel@tonic-gate 			vfs_unlock(vfsp);
17317c478bd9Sstevel@tonic-gate 			vfs_freemnttab(vfsp);
1732da6c28aaSamw 			vfs_free(vfsp);
17337c478bd9Sstevel@tonic-gate 		}
17347c478bd9Sstevel@tonic-gate 	} else {
17357c478bd9Sstevel@tonic-gate 		/*
17367c478bd9Sstevel@tonic-gate 		 * Set the mount time to now
17377c478bd9Sstevel@tonic-gate 		 */
17387c478bd9Sstevel@tonic-gate 		vfsp->vfs_mtime = ddi_get_time();
17397c478bd9Sstevel@tonic-gate 		if (remount) {
17407c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag &= ~VFS_REMOUNT;
17417c478bd9Sstevel@tonic-gate 			if (oldresource)
17427c478bd9Sstevel@tonic-gate 				refstr_rele(oldresource);
17437c478bd9Sstevel@tonic-gate 			if (oldmntpt)
17447c478bd9Sstevel@tonic-gate 				refstr_rele(oldmntpt);
17457c478bd9Sstevel@tonic-gate 		} else if (splice) {
17467c478bd9Sstevel@tonic-gate 			/*
17477c478bd9Sstevel@tonic-gate 			 * Link vfsp into the name space at the mount
17487c478bd9Sstevel@tonic-gate 			 * point. Vfs_add() is responsible for
17497c478bd9Sstevel@tonic-gate 			 * holding the mount point which will be
17507c478bd9Sstevel@tonic-gate 			 * released when vfs_remove() is called.
17517c478bd9Sstevel@tonic-gate 			 */
17527c478bd9Sstevel@tonic-gate 			vfs_add(vp, vfsp, uap->flags);
17537c478bd9Sstevel@tonic-gate 		} else {
17547c478bd9Sstevel@tonic-gate 			/*
17557c478bd9Sstevel@tonic-gate 			 * Hold the reference to file system which is
17567c478bd9Sstevel@tonic-gate 			 * not linked into the name space.
17577c478bd9Sstevel@tonic-gate 			 */
17587c478bd9Sstevel@tonic-gate 			vfsp->vfs_zone = NULL;
17597c478bd9Sstevel@tonic-gate 			VFS_HOLD(vfsp);
17607c478bd9Sstevel@tonic-gate 			vfsp->vfs_vnodecovered = NULL;
17617c478bd9Sstevel@tonic-gate 		}
17627c478bd9Sstevel@tonic-gate 		/*
17637c478bd9Sstevel@tonic-gate 		 * Set flags for global options encountered
17647c478bd9Sstevel@tonic-gate 		 */
17657c478bd9Sstevel@tonic-gate 		if (vfs_optionisset(vfsp, MNTOPT_RO, NULL))
17667c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag |= VFS_RDONLY;
17677c478bd9Sstevel@tonic-gate 		else
17687c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag &= ~VFS_RDONLY;
17697c478bd9Sstevel@tonic-gate 		if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
17707c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag |= (VFS_NOSETUID|VFS_NODEVICES);
17717c478bd9Sstevel@tonic-gate 		} else {
17727c478bd9Sstevel@tonic-gate 			if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
17737c478bd9Sstevel@tonic-gate 				vfsp->vfs_flag |= VFS_NODEVICES;
17747c478bd9Sstevel@tonic-gate 			else
17757c478bd9Sstevel@tonic-gate 				vfsp->vfs_flag &= ~VFS_NODEVICES;
17767c478bd9Sstevel@tonic-gate 			if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))
17777c478bd9Sstevel@tonic-gate 				vfsp->vfs_flag |= VFS_NOSETUID;
17787c478bd9Sstevel@tonic-gate 			else
17797c478bd9Sstevel@tonic-gate 				vfsp->vfs_flag &= ~VFS_NOSETUID;
17807c478bd9Sstevel@tonic-gate 		}
17817c478bd9Sstevel@tonic-gate 		if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL))
17827c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag |= VFS_NBMAND;
17837c478bd9Sstevel@tonic-gate 		else
17847c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag &= ~VFS_NBMAND;
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 		if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL))
17877c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag |= VFS_XATTR;
17887c478bd9Sstevel@tonic-gate 		else
17897c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag &= ~VFS_XATTR;
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 		if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL))
17927c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag |= VFS_NOEXEC;
17937c478bd9Sstevel@tonic-gate 		else
17947c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag &= ~VFS_NOEXEC;
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 		/*
17977c478bd9Sstevel@tonic-gate 		 * Now construct the output option string of options
17987c478bd9Sstevel@tonic-gate 		 * we recognized.
17997c478bd9Sstevel@tonic-gate 		 */
18007c478bd9Sstevel@tonic-gate 		if (uap->flags & MS_OPTIONSTR) {
18017c478bd9Sstevel@tonic-gate 			vfs_list_read_lock();
18027c478bd9Sstevel@tonic-gate 			copyout_error = vfs_buildoptionstr(
18037c478bd9Sstevel@tonic-gate 			    &vfsp->vfs_mntopts, inargs, optlen);
18047c478bd9Sstevel@tonic-gate 			vfs_list_unlock();
18057c478bd9Sstevel@tonic-gate 			if (copyout_error == 0 &&
18067c478bd9Sstevel@tonic-gate 			    (uap->flags & MS_SYSSPACE) == 0) {
18077c478bd9Sstevel@tonic-gate 				copyout_error = copyoutstr(inargs, opts,
18087c478bd9Sstevel@tonic-gate 				    optlen, NULL);
18097c478bd9Sstevel@tonic-gate 			}
18107c478bd9Sstevel@tonic-gate 		}
18115a59a8b3Srsb 
181282c7f3c4Srsb 		/*
181382c7f3c4Srsb 		 * If this isn't a remount, set up the vopstats before
1814ec64bf9dSrsb 		 * anyone can touch this. We only allow spliced file
1815ec64bf9dSrsb 		 * systems (file systems which are in the namespace) to
1816ec64bf9dSrsb 		 * have the VFS_STATS flag set.
1817ec64bf9dSrsb 		 * NOTE: PxFS mounts the underlying file system with
1818ec64bf9dSrsb 		 * MS_NOSPLICE set and copies those vfs_flags to its private
1819ec64bf9dSrsb 		 * vfs structure. As a result, PxFS should never have
1820ec64bf9dSrsb 		 * the VFS_STATS flag or else we might access the vfs
1821ec64bf9dSrsb 		 * statistics-related fields prior to them being
1822ec64bf9dSrsb 		 * properly initialized.
182382c7f3c4Srsb 		 */
1824ec64bf9dSrsb 		if (!remount && (vswp->vsw_flag & VSW_STATS) && splice) {
182582c7f3c4Srsb 			initialize_vopstats(&vfsp->vfs_vopstats);
182682c7f3c4Srsb 			/*
182782c7f3c4Srsb 			 * We need to set vfs_vskap to NULL because there's
182882c7f3c4Srsb 			 * a chance it won't be set below.  This is checked
182982c7f3c4Srsb 			 * in teardown_vopstats() so we can't have garbage.
183082c7f3c4Srsb 			 */
183182c7f3c4Srsb 			vfsp->vfs_vskap = NULL;
18325a59a8b3Srsb 			vfsp->vfs_flag |= VFS_STATS;
183382c7f3c4Srsb 			vfsp->vfs_fstypevsp = get_fstype_vopstats(vfsp, vswp);
18345a59a8b3Srsb 		}
18355a59a8b3Srsb 
1836f48205beScasper 		if (vswp->vsw_flag & VSW_XID)
1837f48205beScasper 			vfsp->vfs_flag |= VFS_XID;
1838f48205beScasper 
18397c478bd9Sstevel@tonic-gate 		vfs_unlock(vfsp);
18407c478bd9Sstevel@tonic-gate 	}
18415fd5c689SJerry Jelinek 	mount_completed(zone);
18425fd5c689SJerry Jelinek 	zone_rele(zone);
18437c478bd9Sstevel@tonic-gate 	if (splice)
18447c478bd9Sstevel@tonic-gate 		vn_vfsunlock(vp);
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate 	if ((error == 0) && (copyout_error == 0)) {
184782c7f3c4Srsb 		if (!remount) {
18485a59a8b3Srsb 			/*
184982c7f3c4Srsb 			 * Don't call get_vskstat_anchor() while holding
185082c7f3c4Srsb 			 * locks since it allocates memory and calls
185182c7f3c4Srsb 			 * VFS_STATVFS().  For NFS, the latter can generate
185282c7f3c4Srsb 			 * an over-the-wire call.
18535a59a8b3Srsb 			 */
185482c7f3c4Srsb 			vskap = get_vskstat_anchor(vfsp);
185582c7f3c4Srsb 			/* Only take the lock if we have something to do */
185682c7f3c4Srsb 			if (vskap != NULL) {
185782c7f3c4Srsb 				vfs_lock_wait(vfsp);
185882c7f3c4Srsb 				if (vfsp->vfs_flag & VFS_STATS) {
185982c7f3c4Srsb 					vfsp->vfs_vskap = vskap;
186082c7f3c4Srsb 				}
186182c7f3c4Srsb 				vfs_unlock(vfsp);
186282c7f3c4Srsb 			}
186382c7f3c4Srsb 		}
18645a59a8b3Srsb 		/* Return vfsp to caller. */
18657c478bd9Sstevel@tonic-gate 		*vfspp = vfsp;
18667c478bd9Sstevel@tonic-gate 	}
18677c478bd9Sstevel@tonic-gate errout:
18687c478bd9Sstevel@tonic-gate 	vfs_freeopttbl(&mnt_mntopts);
18697c478bd9Sstevel@tonic-gate 	if (resource != NULL)
18707c478bd9Sstevel@tonic-gate 		kmem_free(resource, strlen(resource) + 1);
18717c478bd9Sstevel@tonic-gate 	if (mountpt != NULL)
18727c478bd9Sstevel@tonic-gate 		kmem_free(mountpt, strlen(mountpt) + 1);
18737c478bd9Sstevel@tonic-gate 	/*
18747c478bd9Sstevel@tonic-gate 	 * It is possible we errored prior to adding to mount in progress
18757c478bd9Sstevel@tonic-gate 	 * table. Must free vnode we acquired with successful lookupname.
18767c478bd9Sstevel@tonic-gate 	 */
18777c478bd9Sstevel@tonic-gate 	if (addmip)
18787c478bd9Sstevel@tonic-gate 		VN_RELE(bvp);
18797c478bd9Sstevel@tonic-gate 	if (delmip)
18807c478bd9Sstevel@tonic-gate 		vfs_delmip(vfsp);
18817c478bd9Sstevel@tonic-gate 	ASSERT(vswp != NULL);
18827c478bd9Sstevel@tonic-gate 	vfs_unrefvfssw(vswp);
18837c478bd9Sstevel@tonic-gate 	if (inargs != opts)
18847c478bd9Sstevel@tonic-gate 		kmem_free(inargs, MAX_MNTOPT_STR);
18857c478bd9Sstevel@tonic-gate 	if (copyout_error) {
188693239addSjohnlev 		lofi_remove(vfsp);
18877c478bd9Sstevel@tonic-gate 		VFS_RELE(vfsp);
18887c478bd9Sstevel@tonic-gate 		error = copyout_error;
18897c478bd9Sstevel@tonic-gate 	}
18907c478bd9Sstevel@tonic-gate 	return (error);
18917c478bd9Sstevel@tonic-gate }
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate static void
1894d7de0ceaSRobert Harris vfs_setpath(
1895d7de0ceaSRobert Harris     struct vfs *vfsp,		/* vfs being updated */
1896d7de0ceaSRobert Harris     refstr_t **refp,		/* Ref-count string to contain the new path */
1897d7de0ceaSRobert Harris     const char *newpath,	/* Path to add to refp (above) */
1898d7de0ceaSRobert Harris     uint32_t flag)		/* flag */
18997c478bd9Sstevel@tonic-gate {
19007c478bd9Sstevel@tonic-gate 	size_t len;
19017c478bd9Sstevel@tonic-gate 	refstr_t *ref;
19027c478bd9Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
19037c478bd9Sstevel@tonic-gate 	char *sp;
19047c478bd9Sstevel@tonic-gate 	int have_list_lock = 0;
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	ASSERT(!VFS_ON_LIST(vfsp) || vfs_lock_held(vfsp));
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate 	/*
19097c478bd9Sstevel@tonic-gate 	 * New path must be less than MAXPATHLEN because mntfs
19107c478bd9Sstevel@tonic-gate 	 * will only display up to MAXPATHLEN bytes. This is currently
19117c478bd9Sstevel@tonic-gate 	 * safe, because domount() uses pn_get(), and other callers
19127c478bd9Sstevel@tonic-gate 	 * similarly cap the size to fewer than MAXPATHLEN bytes.
19137c478bd9Sstevel@tonic-gate 	 */
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate 	ASSERT(strlen(newpath) < MAXPATHLEN);
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	/* mntfs requires consistency while vfs list lock is held */
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 	if (VFS_ON_LIST(vfsp)) {
19207c478bd9Sstevel@tonic-gate 		have_list_lock = 1;
19217c478bd9Sstevel@tonic-gate 		vfs_list_lock();
19227c478bd9Sstevel@tonic-gate 	}
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 	if (*refp != NULL)
19257c478bd9Sstevel@tonic-gate 		refstr_rele(*refp);
19267c478bd9Sstevel@tonic-gate 
1927d7de0ceaSRobert Harris 	/*
1928d7de0ceaSRobert Harris 	 * If we are in a non-global zone then we prefix the supplied path,
1929d7de0ceaSRobert Harris 	 * newpath, with the zone's root path, with two exceptions. The first
1930d7de0ceaSRobert Harris 	 * is where we have been explicitly directed to avoid doing so; this
1931d7de0ceaSRobert Harris 	 * will be the case following a failed remount, where the path supplied
1932d7de0ceaSRobert Harris 	 * will be a saved version which must now be restored. The second
1933d7de0ceaSRobert Harris 	 * exception is where newpath is not a pathname but a descriptive name,
1934d7de0ceaSRobert Harris 	 * e.g. "procfs".
1935d7de0ceaSRobert Harris 	 */
1936d7de0ceaSRobert Harris 	if (zone == global_zone || (flag & VFSSP_VERBATIM) || *newpath != '/') {
19377c478bd9Sstevel@tonic-gate 		ref = refstr_alloc(newpath);
19387c478bd9Sstevel@tonic-gate 		goto out;
19397c478bd9Sstevel@tonic-gate 	}
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	/*
19427c478bd9Sstevel@tonic-gate 	 * Truncate the trailing '/' in the zoneroot, and merge
19437c478bd9Sstevel@tonic-gate 	 * in the zone's rootpath with the "newpath" (resource
19447c478bd9Sstevel@tonic-gate 	 * or mountpoint) passed in.
19457c478bd9Sstevel@tonic-gate 	 *
19467c478bd9Sstevel@tonic-gate 	 * The size of the required buffer is thus the size of
19477c478bd9Sstevel@tonic-gate 	 * the buffer required for the passed-in newpath
19487c478bd9Sstevel@tonic-gate 	 * (strlen(newpath) + 1), plus the size of the buffer
19497c478bd9Sstevel@tonic-gate 	 * required to hold zone_rootpath (zone_rootpathlen)
19507c478bd9Sstevel@tonic-gate 	 * minus one for one of the now-superfluous NUL
19517c478bd9Sstevel@tonic-gate 	 * terminations, minus one for the trailing '/'.
19527c478bd9Sstevel@tonic-gate 	 *
19537c478bd9Sstevel@tonic-gate 	 * That gives us:
19547c478bd9Sstevel@tonic-gate 	 *
19557c478bd9Sstevel@tonic-gate 	 * (strlen(newpath) + 1) + zone_rootpathlen - 1 - 1
19567c478bd9Sstevel@tonic-gate 	 *
19577c478bd9Sstevel@tonic-gate 	 * Which is what we have below.
19587c478bd9Sstevel@tonic-gate 	 */
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 	len = strlen(newpath) + zone->zone_rootpathlen - 1;
19617c478bd9Sstevel@tonic-gate 	sp = kmem_alloc(len, KM_SLEEP);
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 	/*
19647c478bd9Sstevel@tonic-gate 	 * Copy everything including the trailing slash, which
19657c478bd9Sstevel@tonic-gate 	 * we then overwrite with the NUL character.
19667c478bd9Sstevel@tonic-gate 	 */
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 	(void) strcpy(sp, zone->zone_rootpath);
19697c478bd9Sstevel@tonic-gate 	sp[zone->zone_rootpathlen - 2] = '\0';
19707c478bd9Sstevel@tonic-gate 	(void) strcat(sp, newpath);
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate 	ref = refstr_alloc(sp);
19737c478bd9Sstevel@tonic-gate 	kmem_free(sp, len);
19747c478bd9Sstevel@tonic-gate out:
19757c478bd9Sstevel@tonic-gate 	*refp = ref;
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 	if (have_list_lock) {
19787c478bd9Sstevel@tonic-gate 		vfs_mnttab_modtimeupd();
19797c478bd9Sstevel@tonic-gate 		vfs_list_unlock();
19807c478bd9Sstevel@tonic-gate 	}
19817c478bd9Sstevel@tonic-gate }
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate /*
19847c478bd9Sstevel@tonic-gate  * Record a mounted resource name in a vfs structure.
19857c478bd9Sstevel@tonic-gate  * If vfsp is already mounted, caller must hold the vfs lock.
19867c478bd9Sstevel@tonic-gate  */
19877c478bd9Sstevel@tonic-gate void
1988d7de0ceaSRobert Harris vfs_setresource(struct vfs *vfsp, const char *resource, uint32_t flag)
19897c478bd9Sstevel@tonic-gate {
19907c478bd9Sstevel@tonic-gate 	if (resource == NULL || resource[0] == '\0')
19917c478bd9Sstevel@tonic-gate 		resource = VFS_NORESOURCE;
1992d7de0ceaSRobert Harris 	vfs_setpath(vfsp, &vfsp->vfs_resource, resource, flag);
19937c478bd9Sstevel@tonic-gate }
19947c478bd9Sstevel@tonic-gate 
19957c478bd9Sstevel@tonic-gate /*
19967c478bd9Sstevel@tonic-gate  * Record a mount point name in a vfs structure.
19977c478bd9Sstevel@tonic-gate  * If vfsp is already mounted, caller must hold the vfs lock.
19987c478bd9Sstevel@tonic-gate  */
19997c478bd9Sstevel@tonic-gate void
2000d7de0ceaSRobert Harris vfs_setmntpoint(struct vfs *vfsp, const char *mntpt, uint32_t flag)
20017c478bd9Sstevel@tonic-gate {
20027c478bd9Sstevel@tonic-gate 	if (mntpt == NULL || mntpt[0] == '\0')
20037c478bd9Sstevel@tonic-gate 		mntpt = VFS_NOMNTPT;
2004d7de0ceaSRobert Harris 	vfs_setpath(vfsp, &vfsp->vfs_mntpt, mntpt, flag);
20057c478bd9Sstevel@tonic-gate }
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate /* Returns the vfs_resource. Caller must call refstr_rele() when finished. */
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate refstr_t *
20107c478bd9Sstevel@tonic-gate vfs_getresource(const struct vfs *vfsp)
20117c478bd9Sstevel@tonic-gate {
20127c478bd9Sstevel@tonic-gate 	refstr_t *resource;
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate 	vfs_list_read_lock();
20157c478bd9Sstevel@tonic-gate 	resource = vfsp->vfs_resource;
20167c478bd9Sstevel@tonic-gate 	refstr_hold(resource);
20177c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 	return (resource);
20207c478bd9Sstevel@tonic-gate }
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate /* Returns the vfs_mntpt. Caller must call refstr_rele() when finished. */
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate refstr_t *
20257c478bd9Sstevel@tonic-gate vfs_getmntpoint(const struct vfs *vfsp)
20267c478bd9Sstevel@tonic-gate {
20277c478bd9Sstevel@tonic-gate 	refstr_t *mntpt;
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 	vfs_list_read_lock();
20307c478bd9Sstevel@tonic-gate 	mntpt = vfsp->vfs_mntpt;
20317c478bd9Sstevel@tonic-gate 	refstr_hold(mntpt);
20327c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 	return (mntpt);
20357c478bd9Sstevel@tonic-gate }
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate /*
20387c478bd9Sstevel@tonic-gate  * Create an empty options table with enough empty slots to hold all
20397c478bd9Sstevel@tonic-gate  * The options in the options string passed as an argument.
20407c478bd9Sstevel@tonic-gate  * Potentially prepend another options table.
20417c478bd9Sstevel@tonic-gate  *
20427c478bd9Sstevel@tonic-gate  * Note: caller is responsible for locking the vfs list, if needed,
20437c478bd9Sstevel@tonic-gate  *       to protect mops.
20447c478bd9Sstevel@tonic-gate  */
20457c478bd9Sstevel@tonic-gate static void
20467c478bd9Sstevel@tonic-gate vfs_createopttbl_extend(mntopts_t *mops, const char *opts,
20477c478bd9Sstevel@tonic-gate     const mntopts_t *mtmpl)
20487c478bd9Sstevel@tonic-gate {
20497c478bd9Sstevel@tonic-gate 	const char *s = opts;
20507c478bd9Sstevel@tonic-gate 	uint_t count;
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 	if (opts == NULL || *opts == '\0') {
20537c478bd9Sstevel@tonic-gate 		count = 0;
20547c478bd9Sstevel@tonic-gate 	} else {
20557c478bd9Sstevel@tonic-gate 		count = 1;
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 		/*
20587c478bd9Sstevel@tonic-gate 		 * Count number of options in the string
20597c478bd9Sstevel@tonic-gate 		 */
20607c478bd9Sstevel@tonic-gate 		for (s = strchr(s, ','); s != NULL; s = strchr(s, ',')) {
20617c478bd9Sstevel@tonic-gate 			count++;
20627c478bd9Sstevel@tonic-gate 			s++;
20637c478bd9Sstevel@tonic-gate 		}
20647c478bd9Sstevel@tonic-gate 	}
20657c478bd9Sstevel@tonic-gate 	vfs_copyopttbl_extend(mtmpl, mops, count);
20667c478bd9Sstevel@tonic-gate }
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate /*
20697c478bd9Sstevel@tonic-gate  * Create an empty options table with enough empty slots to hold all
20707c478bd9Sstevel@tonic-gate  * The options in the options string passed as an argument.
20717c478bd9Sstevel@tonic-gate  *
20727c478bd9Sstevel@tonic-gate  * This function is *not* for general use by filesystems.
20737c478bd9Sstevel@tonic-gate  *
20747c478bd9Sstevel@tonic-gate  * Note: caller is responsible for locking the vfs list, if needed,
20757c478bd9Sstevel@tonic-gate  *       to protect mops.
20767c478bd9Sstevel@tonic-gate  */
20777c478bd9Sstevel@tonic-gate void
20787c478bd9Sstevel@tonic-gate vfs_createopttbl(mntopts_t *mops, const char *opts)
20797c478bd9Sstevel@tonic-gate {
20807c478bd9Sstevel@tonic-gate 	vfs_createopttbl_extend(mops, opts, NULL);
20817c478bd9Sstevel@tonic-gate }
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate /*
20857c478bd9Sstevel@tonic-gate  * Swap two mount options tables
20867c478bd9Sstevel@tonic-gate  */
20877c478bd9Sstevel@tonic-gate static void
20887c478bd9Sstevel@tonic-gate vfs_swapopttbl_nolock(mntopts_t *optbl1, mntopts_t *optbl2)
20897c478bd9Sstevel@tonic-gate {
20907c478bd9Sstevel@tonic-gate 	uint_t tmpcnt;
20917c478bd9Sstevel@tonic-gate 	mntopt_t *tmplist;
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	tmpcnt = optbl2->mo_count;
20947c478bd9Sstevel@tonic-gate 	tmplist = optbl2->mo_list;
20957c478bd9Sstevel@tonic-gate 	optbl2->mo_count = optbl1->mo_count;
20967c478bd9Sstevel@tonic-gate 	optbl2->mo_list = optbl1->mo_list;
20977c478bd9Sstevel@tonic-gate 	optbl1->mo_count = tmpcnt;
20987c478bd9Sstevel@tonic-gate 	optbl1->mo_list = tmplist;
20997c478bd9Sstevel@tonic-gate }
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate static void
21027c478bd9Sstevel@tonic-gate vfs_swapopttbl(mntopts_t *optbl1, mntopts_t *optbl2)
21037c478bd9Sstevel@tonic-gate {
21047c478bd9Sstevel@tonic-gate 	vfs_list_lock();
21057c478bd9Sstevel@tonic-gate 	vfs_swapopttbl_nolock(optbl1, optbl2);
21067c478bd9Sstevel@tonic-gate 	vfs_mnttab_modtimeupd();
21077c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
21087c478bd9Sstevel@tonic-gate }
21097c478bd9Sstevel@tonic-gate 
21107c478bd9Sstevel@tonic-gate static char **
21117c478bd9Sstevel@tonic-gate vfs_copycancelopt_extend(char **const moc, int extend)
21127c478bd9Sstevel@tonic-gate {
21137c478bd9Sstevel@tonic-gate 	int i = 0;
21147c478bd9Sstevel@tonic-gate 	int j;
21157c478bd9Sstevel@tonic-gate 	char **result;
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 	if (moc != NULL) {
21187c478bd9Sstevel@tonic-gate 		for (; moc[i] != NULL; i++)
21197c478bd9Sstevel@tonic-gate 			/* count number of options to cancel */;
21207c478bd9Sstevel@tonic-gate 	}
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 	if (i + extend == 0)
21237c478bd9Sstevel@tonic-gate 		return (NULL);
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate 	result = kmem_alloc((i + extend + 1) * sizeof (char *), KM_SLEEP);
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 	for (j = 0; j < i; j++) {
21287c478bd9Sstevel@tonic-gate 		result[j] = kmem_alloc(strlen(moc[j]) + 1, KM_SLEEP);
21297c478bd9Sstevel@tonic-gate 		(void) strcpy(result[j], moc[j]);
21307c478bd9Sstevel@tonic-gate 	}
21317c478bd9Sstevel@tonic-gate 	for (; j <= i + extend; j++)
21327c478bd9Sstevel@tonic-gate 		result[j] = NULL;
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate 	return (result);
21357c478bd9Sstevel@tonic-gate }
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate static void
21387c478bd9Sstevel@tonic-gate vfs_copyopt(const mntopt_t *s, mntopt_t *d)
21397c478bd9Sstevel@tonic-gate {
21407c478bd9Sstevel@tonic-gate 	char *sp, *dp;
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate 	d->mo_flags = s->mo_flags;
21437c478bd9Sstevel@tonic-gate 	d->mo_data = s->mo_data;
21447c478bd9Sstevel@tonic-gate 	sp = s->mo_name;
21457c478bd9Sstevel@tonic-gate 	if (sp != NULL) {
21467c478bd9Sstevel@tonic-gate 		dp = kmem_alloc(strlen(sp) + 1, KM_SLEEP);
21477c478bd9Sstevel@tonic-gate 		(void) strcpy(dp, sp);
21487c478bd9Sstevel@tonic-gate 		d->mo_name = dp;
21497c478bd9Sstevel@tonic-gate 	} else {
21507c478bd9Sstevel@tonic-gate 		d->mo_name = NULL; /* should never happen */
21517c478bd9Sstevel@tonic-gate 	}
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate 	d->mo_cancel = vfs_copycancelopt_extend(s->mo_cancel, 0);
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate 	sp = s->mo_arg;
21567c478bd9Sstevel@tonic-gate 	if (sp != NULL) {
21577c478bd9Sstevel@tonic-gate 		dp = kmem_alloc(strlen(sp) + 1, KM_SLEEP);
21587c478bd9Sstevel@tonic-gate 		(void) strcpy(dp, sp);
21597c478bd9Sstevel@tonic-gate 		d->mo_arg = dp;
21607c478bd9Sstevel@tonic-gate 	} else {
21617c478bd9Sstevel@tonic-gate 		d->mo_arg = NULL;
21627c478bd9Sstevel@tonic-gate 	}
21637c478bd9Sstevel@tonic-gate }
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate /*
21667c478bd9Sstevel@tonic-gate  * Copy a mount options table, possibly allocating some spare
21677c478bd9Sstevel@tonic-gate  * slots at the end.  It is permissible to copy_extend the NULL table.
21687c478bd9Sstevel@tonic-gate  */
21697c478bd9Sstevel@tonic-gate static void
21707c478bd9Sstevel@tonic-gate vfs_copyopttbl_extend(const mntopts_t *smo, mntopts_t *dmo, int extra)
21717c478bd9Sstevel@tonic-gate {
21727c478bd9Sstevel@tonic-gate 	uint_t i, count;
21737c478bd9Sstevel@tonic-gate 	mntopt_t *motbl;
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	/*
21767c478bd9Sstevel@tonic-gate 	 * Clear out any existing stuff in the options table being initialized
21777c478bd9Sstevel@tonic-gate 	 */
21787c478bd9Sstevel@tonic-gate 	vfs_freeopttbl(dmo);
21797c478bd9Sstevel@tonic-gate 	count = (smo == NULL) ? 0 : smo->mo_count;
21807c478bd9Sstevel@tonic-gate 	if ((count + extra) == 0)	/* nothing to do */
21817c478bd9Sstevel@tonic-gate 		return;
21827c478bd9Sstevel@tonic-gate 	dmo->mo_count = count + extra;
21837c478bd9Sstevel@tonic-gate 	motbl = kmem_zalloc((count + extra) * sizeof (mntopt_t), KM_SLEEP);
21847c478bd9Sstevel@tonic-gate 	dmo->mo_list = motbl;
21857c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
21867c478bd9Sstevel@tonic-gate 		vfs_copyopt(&smo->mo_list[i], &motbl[i]);
21877c478bd9Sstevel@tonic-gate 	}
21887c478bd9Sstevel@tonic-gate 	for (i = count; i < count + extra; i++) {
21897c478bd9Sstevel@tonic-gate 		motbl[i].mo_flags = MO_EMPTY;
21907c478bd9Sstevel@tonic-gate 	}
21917c478bd9Sstevel@tonic-gate }
21927c478bd9Sstevel@tonic-gate 
21937c478bd9Sstevel@tonic-gate /*
21947c478bd9Sstevel@tonic-gate  * Copy a mount options table.
21957c478bd9Sstevel@tonic-gate  *
21967c478bd9Sstevel@tonic-gate  * This function is *not* for general use by filesystems.
21977c478bd9Sstevel@tonic-gate  *
21987c478bd9Sstevel@tonic-gate  * Note: caller is responsible for locking the vfs list, if needed,
21997c478bd9Sstevel@tonic-gate  *       to protect smo and dmo.
22007c478bd9Sstevel@tonic-gate  */
22017c478bd9Sstevel@tonic-gate void
22027c478bd9Sstevel@tonic-gate vfs_copyopttbl(const mntopts_t *smo, mntopts_t *dmo)
22037c478bd9Sstevel@tonic-gate {
22047c478bd9Sstevel@tonic-gate 	vfs_copyopttbl_extend(smo, dmo, 0);
22057c478bd9Sstevel@tonic-gate }
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate static char **
22087c478bd9Sstevel@tonic-gate vfs_mergecancelopts(const mntopt_t *mop1, const mntopt_t *mop2)
22097c478bd9Sstevel@tonic-gate {
22107c478bd9Sstevel@tonic-gate 	int c1 = 0;
22117c478bd9Sstevel@tonic-gate 	int c2 = 0;
22127c478bd9Sstevel@tonic-gate 	char **result;
22137c478bd9Sstevel@tonic-gate 	char **sp1, **sp2, **dp;
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate 	/*
22167c478bd9Sstevel@tonic-gate 	 * First we count both lists of cancel options.
22177c478bd9Sstevel@tonic-gate 	 * If either is NULL or has no elements, we return a copy of
22187c478bd9Sstevel@tonic-gate 	 * the other.
22197c478bd9Sstevel@tonic-gate 	 */
22207c478bd9Sstevel@tonic-gate 	if (mop1->mo_cancel != NULL) {
22217c478bd9Sstevel@tonic-gate 		for (; mop1->mo_cancel[c1] != NULL; c1++)
22227c478bd9Sstevel@tonic-gate 			/* count cancel options in mop1 */;
22237c478bd9Sstevel@tonic-gate 	}
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate 	if (c1 == 0)
22267c478bd9Sstevel@tonic-gate 		return (vfs_copycancelopt_extend(mop2->mo_cancel, 0));
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate 	if (mop2->mo_cancel != NULL) {
22297c478bd9Sstevel@tonic-gate 		for (; mop2->mo_cancel[c2] != NULL; c2++)
22307c478bd9Sstevel@tonic-gate 			/* count cancel options in mop2 */;
22317c478bd9Sstevel@tonic-gate 	}
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate 	result = vfs_copycancelopt_extend(mop1->mo_cancel, c2);
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate 	if (c2 == 0)
22367c478bd9Sstevel@tonic-gate 		return (result);
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate 	/*
22397c478bd9Sstevel@tonic-gate 	 * When we get here, we've got two sets of cancel options;
22407c478bd9Sstevel@tonic-gate 	 * we need to merge the two sets.  We know that the result
22417c478bd9Sstevel@tonic-gate 	 * array has "c1+c2+1" entries and in the end we might shrink
22427c478bd9Sstevel@tonic-gate 	 * it.
22437c478bd9Sstevel@tonic-gate 	 * Result now has a copy of the c1 entries from mop1; we'll
22447c478bd9Sstevel@tonic-gate 	 * now lookup all the entries of mop2 in mop1 and copy it if
22457c478bd9Sstevel@tonic-gate 	 * it is unique.
22467c478bd9Sstevel@tonic-gate 	 * This operation is O(n^2) but it's only called once per
22477c478bd9Sstevel@tonic-gate 	 * filesystem per duplicate option.  This is a situation
22487c478bd9Sstevel@tonic-gate 	 * which doesn't arise with the filesystems in ON and
22497c478bd9Sstevel@tonic-gate 	 * n is generally 1.
22507c478bd9Sstevel@tonic-gate 	 */
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 	dp = &result[c1];
22537c478bd9Sstevel@tonic-gate 	for (sp2 = mop2->mo_cancel; *sp2 != NULL; sp2++) {
22547c478bd9Sstevel@tonic-gate 		for (sp1 = mop1->mo_cancel; *sp1 != NULL; sp1++) {
22557c478bd9Sstevel@tonic-gate 			if (strcmp(*sp1, *sp2) == 0)
22567c478bd9Sstevel@tonic-gate 				break;
22577c478bd9Sstevel@tonic-gate 		}
22587c478bd9Sstevel@tonic-gate 		if (*sp1 == NULL) {
22597c478bd9Sstevel@tonic-gate 			/*
22607c478bd9Sstevel@tonic-gate 			 * Option *sp2 not found in mop1, so copy it.
22617c478bd9Sstevel@tonic-gate 			 * The calls to vfs_copycancelopt_extend()
22627c478bd9Sstevel@tonic-gate 			 * guarantee that there's enough room.
22637c478bd9Sstevel@tonic-gate 			 */
22647c478bd9Sstevel@tonic-gate 			*dp = kmem_alloc(strlen(*sp2) + 1, KM_SLEEP);
22657c478bd9Sstevel@tonic-gate 			(void) strcpy(*dp++, *sp2);
22667c478bd9Sstevel@tonic-gate 		}
22677c478bd9Sstevel@tonic-gate 	}
22687c478bd9Sstevel@tonic-gate 	if (dp != &result[c1+c2]) {
22697c478bd9Sstevel@tonic-gate 		size_t bytes = (dp - result + 1) * sizeof (char *);
22707c478bd9Sstevel@tonic-gate 		char **nres = kmem_alloc(bytes, KM_SLEEP);
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 		bcopy(result, nres, bytes);
22737c478bd9Sstevel@tonic-gate 		kmem_free(result, (c1 + c2 + 1) * sizeof (char *));
22747c478bd9Sstevel@tonic-gate 		result = nres;
22757c478bd9Sstevel@tonic-gate 	}
22767c478bd9Sstevel@tonic-gate 	return (result);
22777c478bd9Sstevel@tonic-gate }
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate /*
22807c478bd9Sstevel@tonic-gate  * Merge two mount option tables (outer and inner) into one.  This is very
22817c478bd9Sstevel@tonic-gate  * similar to "merging" global variables and automatic variables in C.
22827c478bd9Sstevel@tonic-gate  *
22837c478bd9Sstevel@tonic-gate  * This isn't (and doesn't have to be) fast.
22847c478bd9Sstevel@tonic-gate  *
22857c478bd9Sstevel@tonic-gate  * This function is *not* for general use by filesystems.
22867c478bd9Sstevel@tonic-gate  *
22877c478bd9Sstevel@tonic-gate  * Note: caller is responsible for locking the vfs list, if needed,
22887c478bd9Sstevel@tonic-gate  *       to protect omo, imo & dmo.
22897c478bd9Sstevel@tonic-gate  */
22907c478bd9Sstevel@tonic-gate void
22917c478bd9Sstevel@tonic-gate vfs_mergeopttbl(const mntopts_t *omo, const mntopts_t *imo, mntopts_t *dmo)
22927c478bd9Sstevel@tonic-gate {
22937c478bd9Sstevel@tonic-gate 	uint_t i, count;
22947c478bd9Sstevel@tonic-gate 	mntopt_t *mop, *motbl;
22957c478bd9Sstevel@tonic-gate 	uint_t freeidx;
22967c478bd9Sstevel@tonic-gate 
22977c478bd9Sstevel@tonic-gate 	/*
22987c478bd9Sstevel@tonic-gate 	 * First determine how much space we need to allocate.
22997c478bd9Sstevel@tonic-gate 	 */
23007c478bd9Sstevel@tonic-gate 	count = omo->mo_count;
23017c478bd9Sstevel@tonic-gate 	for (i = 0; i < imo->mo_count; i++) {
23027c478bd9Sstevel@tonic-gate 		if (imo->mo_list[i].mo_flags & MO_EMPTY)
23037c478bd9Sstevel@tonic-gate 			continue;
23047c478bd9Sstevel@tonic-gate 		if (vfs_hasopt(omo, imo->mo_list[i].mo_name) == NULL)
23057c478bd9Sstevel@tonic-gate 			count++;
23067c478bd9Sstevel@tonic-gate 	}
23077c478bd9Sstevel@tonic-gate 	ASSERT(count >= omo->mo_count &&
23087c478bd9Sstevel@tonic-gate 	    count <= omo->mo_count + imo->mo_count);
23097c478bd9Sstevel@tonic-gate 	motbl = kmem_alloc(count * sizeof (mntopt_t), KM_SLEEP);
23107c478bd9Sstevel@tonic-gate 	for (i = 0; i < omo->mo_count; i++)
23117c478bd9Sstevel@tonic-gate 		vfs_copyopt(&omo->mo_list[i], &motbl[i]);
23127c478bd9Sstevel@tonic-gate 	freeidx = omo->mo_count;
23137c478bd9Sstevel@tonic-gate 	for (i = 0; i < imo->mo_count; i++) {
23147c478bd9Sstevel@tonic-gate 		if (imo->mo_list[i].mo_flags & MO_EMPTY)
23157c478bd9Sstevel@tonic-gate 			continue;
23167c478bd9Sstevel@tonic-gate 		if ((mop = vfs_hasopt(omo, imo->mo_list[i].mo_name)) != NULL) {
23177c478bd9Sstevel@tonic-gate 			char **newcanp;
23187c478bd9Sstevel@tonic-gate 			uint_t index = mop - omo->mo_list;
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate 			newcanp = vfs_mergecancelopts(mop, &motbl[index]);
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate 			vfs_freeopt(&motbl[index]);
23237c478bd9Sstevel@tonic-gate 			vfs_copyopt(&imo->mo_list[i], &motbl[index]);
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 			vfs_freecancelopt(motbl[index].mo_cancel);
23267c478bd9Sstevel@tonic-gate 			motbl[index].mo_cancel = newcanp;
23277c478bd9Sstevel@tonic-gate 		} else {
23287c478bd9Sstevel@tonic-gate 			/*
23297c478bd9Sstevel@tonic-gate 			 * If it's a new option, just copy it over to the first
23307c478bd9Sstevel@tonic-gate 			 * free location.
23317c478bd9Sstevel@tonic-gate 			 */
23327c478bd9Sstevel@tonic-gate 			vfs_copyopt(&imo->mo_list[i], &motbl[freeidx++]);
23337c478bd9Sstevel@tonic-gate 		}
23347c478bd9Sstevel@tonic-gate 	}
23357c478bd9Sstevel@tonic-gate 	dmo->mo_count = count;
23367c478bd9Sstevel@tonic-gate 	dmo->mo_list = motbl;
23377c478bd9Sstevel@tonic-gate }
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate /*
23407c478bd9Sstevel@tonic-gate  * Functions to set and clear mount options in a mount options table.
23417c478bd9Sstevel@tonic-gate  */
23427c478bd9Sstevel@tonic-gate 
23437c478bd9Sstevel@tonic-gate /*
23447c478bd9Sstevel@tonic-gate  * Clear a mount option, if it exists.
23457c478bd9Sstevel@tonic-gate  *
23467c478bd9Sstevel@tonic-gate  * The update_mnttab arg indicates whether mops is part of a vfs that is on
23477c478bd9Sstevel@tonic-gate  * the vfs list.
23487c478bd9Sstevel@tonic-gate  */
23497c478bd9Sstevel@tonic-gate static void
23507c478bd9Sstevel@tonic-gate vfs_clearmntopt_nolock(mntopts_t *mops, const char *opt, int update_mnttab)
23517c478bd9Sstevel@tonic-gate {
23527c478bd9Sstevel@tonic-gate 	struct mntopt *mop;
23537c478bd9Sstevel@tonic-gate 	uint_t i, count;
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 	ASSERT(!update_mnttab || RW_WRITE_HELD(&vfslist));
23567c478bd9Sstevel@tonic-gate 
23577c478bd9Sstevel@tonic-gate 	count = mops->mo_count;
23587c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
23597c478bd9Sstevel@tonic-gate 		mop = &mops->mo_list[i];
23607c478bd9Sstevel@tonic-gate 
23617c478bd9Sstevel@tonic-gate 		if (mop->mo_flags & MO_EMPTY)
23627c478bd9Sstevel@tonic-gate 			continue;
23637c478bd9Sstevel@tonic-gate 		if (strcmp(opt, mop->mo_name))
23647c478bd9Sstevel@tonic-gate 			continue;
23657c478bd9Sstevel@tonic-gate 		mop->mo_flags &= ~MO_SET;
23667c478bd9Sstevel@tonic-gate 		if (mop->mo_arg != NULL) {
23677c478bd9Sstevel@tonic-gate 			kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
23687c478bd9Sstevel@tonic-gate 		}
23697c478bd9Sstevel@tonic-gate 		mop->mo_arg = NULL;
23707c478bd9Sstevel@tonic-gate 		if (update_mnttab)
23717c478bd9Sstevel@tonic-gate 			vfs_mnttab_modtimeupd();
23727c478bd9Sstevel@tonic-gate 		break;
23737c478bd9Sstevel@tonic-gate 	}
23747c478bd9Sstevel@tonic-gate }
23757c478bd9Sstevel@tonic-gate 
23767c478bd9Sstevel@tonic-gate void
23777c478bd9Sstevel@tonic-gate vfs_clearmntopt(struct vfs *vfsp, const char *opt)
23787c478bd9Sstevel@tonic-gate {
23797c478bd9Sstevel@tonic-gate 	int gotlock = 0;
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate 	if (VFS_ON_LIST(vfsp)) {
23827c478bd9Sstevel@tonic-gate 		gotlock = 1;
23837c478bd9Sstevel@tonic-gate 		vfs_list_lock();
23847c478bd9Sstevel@tonic-gate 	}
23857c478bd9Sstevel@tonic-gate 	vfs_clearmntopt_nolock(&vfsp->vfs_mntopts, opt, gotlock);
23867c478bd9Sstevel@tonic-gate 	if (gotlock)
23877c478bd9Sstevel@tonic-gate 		vfs_list_unlock();
23887c478bd9Sstevel@tonic-gate }
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate /*
23927c478bd9Sstevel@tonic-gate  * Set a mount option on.  If it's not found in the table, it's silently
23937c478bd9Sstevel@tonic-gate  * ignored.  If the option has MO_IGNORE set, it is still set unless the
23947c478bd9Sstevel@tonic-gate  * VFS_NOFORCEOPT bit is set in the flags.  Also, VFS_DISPLAY/VFS_NODISPLAY flag
23957c478bd9Sstevel@tonic-gate  * bits can be used to toggle the MO_NODISPLAY bit for the option.
23967c478bd9Sstevel@tonic-gate  * If the VFS_CREATEOPT flag bit is set then the first option slot with
23977c478bd9Sstevel@tonic-gate  * MO_EMPTY set is created as the option passed in.
23987c478bd9Sstevel@tonic-gate  *
23997c478bd9Sstevel@tonic-gate  * The update_mnttab arg indicates whether mops is part of a vfs that is on
24007c478bd9Sstevel@tonic-gate  * the vfs list.
24017c478bd9Sstevel@tonic-gate  */
24027c478bd9Sstevel@tonic-gate static void
24037c478bd9Sstevel@tonic-gate vfs_setmntopt_nolock(mntopts_t *mops, const char *opt,
24047c478bd9Sstevel@tonic-gate     const char *arg, int flags, int update_mnttab)
24057c478bd9Sstevel@tonic-gate {
24067c478bd9Sstevel@tonic-gate 	mntopt_t *mop;
24077c478bd9Sstevel@tonic-gate 	uint_t i, count;
24087c478bd9Sstevel@tonic-gate 	char *sp;
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate 	ASSERT(!update_mnttab || RW_WRITE_HELD(&vfslist));
24117c478bd9Sstevel@tonic-gate 
24127c478bd9Sstevel@tonic-gate 	if (flags & VFS_CREATEOPT) {
24137c478bd9Sstevel@tonic-gate 		if (vfs_hasopt(mops, opt) != NULL) {
24147c478bd9Sstevel@tonic-gate 			flags &= ~VFS_CREATEOPT;
24157c478bd9Sstevel@tonic-gate 		}
24167c478bd9Sstevel@tonic-gate 	}
24177c478bd9Sstevel@tonic-gate 	count = mops->mo_count;
24187c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
24197c478bd9Sstevel@tonic-gate 		mop = &mops->mo_list[i];
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate 		if (mop->mo_flags & MO_EMPTY) {
24227c478bd9Sstevel@tonic-gate 			if ((flags & VFS_CREATEOPT) == 0)
24237c478bd9Sstevel@tonic-gate 				continue;
24247c478bd9Sstevel@tonic-gate 			sp = kmem_alloc(strlen(opt) + 1, KM_SLEEP);
24257c478bd9Sstevel@tonic-gate 			(void) strcpy(sp, opt);
24267c478bd9Sstevel@tonic-gate 			mop->mo_name = sp;
24277c478bd9Sstevel@tonic-gate 			if (arg != NULL)
24287c478bd9Sstevel@tonic-gate 				mop->mo_flags = MO_HASVALUE;
24297c478bd9Sstevel@tonic-gate 			else
24307c478bd9Sstevel@tonic-gate 				mop->mo_flags = 0;
24317c478bd9Sstevel@tonic-gate 		} else if (strcmp(opt, mop->mo_name)) {
24327c478bd9Sstevel@tonic-gate 			continue;
24337c478bd9Sstevel@tonic-gate 		}
24347c478bd9Sstevel@tonic-gate 		if ((mop->mo_flags & MO_IGNORE) && (flags & VFS_NOFORCEOPT))
24357c478bd9Sstevel@tonic-gate 			break;
24367c478bd9Sstevel@tonic-gate 		if (arg != NULL && (mop->mo_flags & MO_HASVALUE) != 0) {
24377c478bd9Sstevel@tonic-gate 			sp = kmem_alloc(strlen(arg) + 1, KM_SLEEP);
24387c478bd9Sstevel@tonic-gate 			(void) strcpy(sp, arg);
24397c478bd9Sstevel@tonic-gate 		} else {
24407c478bd9Sstevel@tonic-gate 			sp = NULL;
24417c478bd9Sstevel@tonic-gate 		}
24427c478bd9Sstevel@tonic-gate 		if (mop->mo_arg != NULL)
24437c478bd9Sstevel@tonic-gate 			kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
24447c478bd9Sstevel@tonic-gate 		mop->mo_arg = sp;
24457c478bd9Sstevel@tonic-gate 		if (flags & VFS_DISPLAY)
24467c478bd9Sstevel@tonic-gate 			mop->mo_flags &= ~MO_NODISPLAY;
24477c478bd9Sstevel@tonic-gate 		if (flags & VFS_NODISPLAY)
24487c478bd9Sstevel@tonic-gate 			mop->mo_flags |= MO_NODISPLAY;
24497c478bd9Sstevel@tonic-gate 		mop->mo_flags |= MO_SET;
24507c478bd9Sstevel@tonic-gate 		if (mop->mo_cancel != NULL) {
24517c478bd9Sstevel@tonic-gate 			char **cp;
24527c478bd9Sstevel@tonic-gate 
24537c478bd9Sstevel@tonic-gate 			for (cp = mop->mo_cancel; *cp != NULL; cp++)
24547c478bd9Sstevel@tonic-gate 				vfs_clearmntopt_nolock(mops, *cp, 0);
24557c478bd9Sstevel@tonic-gate 		}
24567c478bd9Sstevel@tonic-gate 		if (update_mnttab)
24577c478bd9Sstevel@tonic-gate 			vfs_mnttab_modtimeupd();
24587c478bd9Sstevel@tonic-gate 		break;
24597c478bd9Sstevel@tonic-gate 	}
24607c478bd9Sstevel@tonic-gate }
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate void
24637c478bd9Sstevel@tonic-gate vfs_setmntopt(struct vfs *vfsp, const char *opt, const char *arg, int flags)
24647c478bd9Sstevel@tonic-gate {
24657c478bd9Sstevel@tonic-gate 	int gotlock = 0;
24667c478bd9Sstevel@tonic-gate 
24677c478bd9Sstevel@tonic-gate 	if (VFS_ON_LIST(vfsp)) {
24687c478bd9Sstevel@tonic-gate 		gotlock = 1;
24697c478bd9Sstevel@tonic-gate 		vfs_list_lock();
24707c478bd9Sstevel@tonic-gate 	}
24717c478bd9Sstevel@tonic-gate 	vfs_setmntopt_nolock(&vfsp->vfs_mntopts, opt, arg, flags, gotlock);
24727c478bd9Sstevel@tonic-gate 	if (gotlock)
24737c478bd9Sstevel@tonic-gate 		vfs_list_unlock();
24747c478bd9Sstevel@tonic-gate }
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate /*
24787c478bd9Sstevel@tonic-gate  * Add a "tag" option to a mounted file system's options list.
24797c478bd9Sstevel@tonic-gate  *
24807c478bd9Sstevel@tonic-gate  * Note: caller is responsible for locking the vfs list, if needed,
24817c478bd9Sstevel@tonic-gate  *       to protect mops.
24827c478bd9Sstevel@tonic-gate  */
24837c478bd9Sstevel@tonic-gate static mntopt_t *
24847c478bd9Sstevel@tonic-gate vfs_addtag(mntopts_t *mops, const char *tag)
24857c478bd9Sstevel@tonic-gate {
24867c478bd9Sstevel@tonic-gate 	uint_t count;
24877c478bd9Sstevel@tonic-gate 	mntopt_t *mop, *motbl;
24887c478bd9Sstevel@tonic-gate 
24897c478bd9Sstevel@tonic-gate 	count = mops->mo_count + 1;
24907c478bd9Sstevel@tonic-gate 	motbl = kmem_zalloc(count * sizeof (mntopt_t), KM_SLEEP);
24917c478bd9Sstevel@tonic-gate 	if (mops->mo_count) {
24927c478bd9Sstevel@tonic-gate 		size_t len = (count - 1) * sizeof (mntopt_t);
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate 		bcopy(mops->mo_list, motbl, len);
24957c478bd9Sstevel@tonic-gate 		kmem_free(mops->mo_list, len);
24967c478bd9Sstevel@tonic-gate 	}
24977c478bd9Sstevel@tonic-gate 	mops->mo_count = count;
24987c478bd9Sstevel@tonic-gate 	mops->mo_list = motbl;
24997c478bd9Sstevel@tonic-gate 	mop = &motbl[count - 1];
25007c478bd9Sstevel@tonic-gate 	mop->mo_flags = MO_TAG;
25017c478bd9Sstevel@tonic-gate 	mop->mo_name = kmem_alloc(strlen(tag) + 1, KM_SLEEP);
25027c478bd9Sstevel@tonic-gate 	(void) strcpy(mop->mo_name, tag);
25037c478bd9Sstevel@tonic-gate 	return (mop);
25047c478bd9Sstevel@tonic-gate }
25057c478bd9Sstevel@tonic-gate 
25067c478bd9Sstevel@tonic-gate /*
25077c478bd9Sstevel@tonic-gate  * Allow users to set arbitrary "tags" in a vfs's mount options.
25087c478bd9Sstevel@tonic-gate  * Broader use within the kernel is discouraged.
25097c478bd9Sstevel@tonic-gate  */
25107c478bd9Sstevel@tonic-gate int
25117c478bd9Sstevel@tonic-gate vfs_settag(uint_t major, uint_t minor, const char *mntpt, const char *tag,
25127c478bd9Sstevel@tonic-gate     cred_t *cr)
25137c478bd9Sstevel@tonic-gate {
25147c478bd9Sstevel@tonic-gate 	vfs_t *vfsp;
25157c478bd9Sstevel@tonic-gate 	mntopts_t *mops;
25167c478bd9Sstevel@tonic-gate 	mntopt_t *mop;
25177c478bd9Sstevel@tonic-gate 	int found = 0;
25187c478bd9Sstevel@tonic-gate 	dev_t dev = makedevice(major, minor);
25197c478bd9Sstevel@tonic-gate 	int err = 0;
25207c478bd9Sstevel@tonic-gate 	char *buf = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate 	/*
25237c478bd9Sstevel@tonic-gate 	 * Find the desired mounted file system
25247c478bd9Sstevel@tonic-gate 	 */
25257c478bd9Sstevel@tonic-gate 	vfs_list_lock();
25267c478bd9Sstevel@tonic-gate 	vfsp = rootvfs;
25277c478bd9Sstevel@tonic-gate 	do {
25287c478bd9Sstevel@tonic-gate 		if (vfsp->vfs_dev == dev &&
25297c478bd9Sstevel@tonic-gate 		    strcmp(mntpt, refstr_value(vfsp->vfs_mntpt)) == 0) {
25307c478bd9Sstevel@tonic-gate 			found = 1;
25317c478bd9Sstevel@tonic-gate 			break;
25327c478bd9Sstevel@tonic-gate 		}
25337c478bd9Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
25347c478bd9Sstevel@tonic-gate 	} while (vfsp != rootvfs);
25357c478bd9Sstevel@tonic-gate 
25367c478bd9Sstevel@tonic-gate 	if (!found) {
25377c478bd9Sstevel@tonic-gate 		err = EINVAL;
25387c478bd9Sstevel@tonic-gate 		goto out;
25397c478bd9Sstevel@tonic-gate 	}
25407c478bd9Sstevel@tonic-gate 	err = secpolicy_fs_config(cr, vfsp);
25417c478bd9Sstevel@tonic-gate 	if (err != 0)
25427c478bd9Sstevel@tonic-gate 		goto out;
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate 	mops = &vfsp->vfs_mntopts;
25457c478bd9Sstevel@tonic-gate 	/*
25467c478bd9Sstevel@tonic-gate 	 * Add tag if it doesn't already exist
25477c478bd9Sstevel@tonic-gate 	 */
25487c478bd9Sstevel@tonic-gate 	if ((mop = vfs_hasopt(mops, tag)) == NULL) {
25497c478bd9Sstevel@tonic-gate 		int len;
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 		(void) vfs_buildoptionstr(mops, buf, MAX_MNTOPT_STR);
25527c478bd9Sstevel@tonic-gate 		len = strlen(buf);
25537c478bd9Sstevel@tonic-gate 		if (len + strlen(tag) + 2 > MAX_MNTOPT_STR) {
25547c478bd9Sstevel@tonic-gate 			err = ENAMETOOLONG;
25557c478bd9Sstevel@tonic-gate 			goto out;
25567c478bd9Sstevel@tonic-gate 		}
25577c478bd9Sstevel@tonic-gate 		mop = vfs_addtag(mops, tag);
25587c478bd9Sstevel@tonic-gate 	}
25597c478bd9Sstevel@tonic-gate 	if ((mop->mo_flags & MO_TAG) == 0) {
25607c478bd9Sstevel@tonic-gate 		err = EINVAL;
25617c478bd9Sstevel@tonic-gate 		goto out;
25627c478bd9Sstevel@tonic-gate 	}
25637c478bd9Sstevel@tonic-gate 	vfs_setmntopt_nolock(mops, tag, NULL, 0, 1);
25647c478bd9Sstevel@tonic-gate out:
25657c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
25667c478bd9Sstevel@tonic-gate 	kmem_free(buf, MAX_MNTOPT_STR);
25677c478bd9Sstevel@tonic-gate 	return (err);
25687c478bd9Sstevel@tonic-gate }
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate /*
25717c478bd9Sstevel@tonic-gate  * Allow users to remove arbitrary "tags" in a vfs's mount options.
25727c478bd9Sstevel@tonic-gate  * Broader use within the kernel is discouraged.
25737c478bd9Sstevel@tonic-gate  */
25747c478bd9Sstevel@tonic-gate int
25757c478bd9Sstevel@tonic-gate vfs_clrtag(uint_t major, uint_t minor, const char *mntpt, const char *tag,
25767c478bd9Sstevel@tonic-gate     cred_t *cr)
25777c478bd9Sstevel@tonic-gate {
25787c478bd9Sstevel@tonic-gate 	vfs_t *vfsp;
25797c478bd9Sstevel@tonic-gate 	mntopt_t *mop;
25807c478bd9Sstevel@tonic-gate 	int found = 0;
25817c478bd9Sstevel@tonic-gate 	dev_t dev = makedevice(major, minor);
25827c478bd9Sstevel@tonic-gate 	int err = 0;
25837c478bd9Sstevel@tonic-gate 
25847c478bd9Sstevel@tonic-gate 	/*
25857c478bd9Sstevel@tonic-gate 	 * Find the desired mounted file system
25867c478bd9Sstevel@tonic-gate 	 */
25877c478bd9Sstevel@tonic-gate 	vfs_list_lock();
25887c478bd9Sstevel@tonic-gate 	vfsp = rootvfs;
25897c478bd9Sstevel@tonic-gate 	do {
25907c478bd9Sstevel@tonic-gate 		if (vfsp->vfs_dev == dev &&
25917c478bd9Sstevel@tonic-gate 		    strcmp(mntpt, refstr_value(vfsp->vfs_mntpt)) == 0) {
25927c478bd9Sstevel@tonic-gate 			found = 1;
25937c478bd9Sstevel@tonic-gate 			break;
25947c478bd9Sstevel@tonic-gate 		}
25957c478bd9Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
25967c478bd9Sstevel@tonic-gate 	} while (vfsp != rootvfs);
25977c478bd9Sstevel@tonic-gate 
25987c478bd9Sstevel@tonic-gate 	if (!found) {
25997c478bd9Sstevel@tonic-gate 		err = EINVAL;
26007c478bd9Sstevel@tonic-gate 		goto out;
26017c478bd9Sstevel@tonic-gate 	}
26027c478bd9Sstevel@tonic-gate 	err = secpolicy_fs_config(cr, vfsp);
26037c478bd9Sstevel@tonic-gate 	if (err != 0)
26047c478bd9Sstevel@tonic-gate 		goto out;
26057c478bd9Sstevel@tonic-gate 
26067c478bd9Sstevel@tonic-gate 	if ((mop = vfs_hasopt(&vfsp->vfs_mntopts, tag)) == NULL) {
26077c478bd9Sstevel@tonic-gate 		err = EINVAL;
26087c478bd9Sstevel@tonic-gate 		goto out;
26097c478bd9Sstevel@tonic-gate 	}
26107c478bd9Sstevel@tonic-gate 	if ((mop->mo_flags & MO_TAG) == 0) {
26117c478bd9Sstevel@tonic-gate 		err = EINVAL;
26127c478bd9Sstevel@tonic-gate 		goto out;
26137c478bd9Sstevel@tonic-gate 	}
26147c478bd9Sstevel@tonic-gate 	vfs_clearmntopt_nolock(&vfsp->vfs_mntopts, tag, 1);
26157c478bd9Sstevel@tonic-gate out:
26167c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
26177c478bd9Sstevel@tonic-gate 	return (err);
26187c478bd9Sstevel@tonic-gate }
26197c478bd9Sstevel@tonic-gate 
26207c478bd9Sstevel@tonic-gate /*
26217c478bd9Sstevel@tonic-gate  * Function to parse an option string and fill in a mount options table.
26227c478bd9Sstevel@tonic-gate  * Unknown options are silently ignored.  The input option string is modified
26237c478bd9Sstevel@tonic-gate  * by replacing separators with nulls.  If the create flag is set, options
26247c478bd9Sstevel@tonic-gate  * not found in the table are just added on the fly.  The table must have
26257c478bd9Sstevel@tonic-gate  * an option slot marked MO_EMPTY to add an option on the fly.
26267c478bd9Sstevel@tonic-gate  *
26277c478bd9Sstevel@tonic-gate  * This function is *not* for general use by filesystems.
26287c478bd9Sstevel@tonic-gate  *
26297c478bd9Sstevel@tonic-gate  * Note: caller is responsible for locking the vfs list, if needed,
26307c478bd9Sstevel@tonic-gate  *       to protect mops..
26317c478bd9Sstevel@tonic-gate  */
26327c478bd9Sstevel@tonic-gate void
26337c478bd9Sstevel@tonic-gate vfs_parsemntopts(mntopts_t *mops, char *osp, int create)
26347c478bd9Sstevel@tonic-gate {
26357c478bd9Sstevel@tonic-gate 	char *s = osp, *p, *nextop, *valp, *cp, *ep;
26367c478bd9Sstevel@tonic-gate 	int setflg = VFS_NOFORCEOPT;
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate 	if (osp == NULL)
26397c478bd9Sstevel@tonic-gate 		return;
26407c478bd9Sstevel@tonic-gate 	while (*s != '\0') {
26417c478bd9Sstevel@tonic-gate 		p = strchr(s, ',');	/* find next option */
26427c478bd9Sstevel@tonic-gate 		if (p == NULL) {
26437c478bd9Sstevel@tonic-gate 			cp = NULL;
26447c478bd9Sstevel@tonic-gate 			p = s + strlen(s);
26457c478bd9Sstevel@tonic-gate 		} else {
26467c478bd9Sstevel@tonic-gate 			cp = p;		/* save location of comma */
26477c478bd9Sstevel@tonic-gate 			*p++ = '\0';	/* mark end and point to next option */
26487c478bd9Sstevel@tonic-gate 		}
26497c478bd9Sstevel@tonic-gate 		nextop = p;
26507c478bd9Sstevel@tonic-gate 		p = strchr(s, '=');	/* look for value */
26517c478bd9Sstevel@tonic-gate 		if (p == NULL) {
26527c478bd9Sstevel@tonic-gate 			valp = NULL;	/* no value supplied */
26537c478bd9Sstevel@tonic-gate 		} else {
26547c478bd9Sstevel@tonic-gate 			ep = p;		/* save location of equals */
26557c478bd9Sstevel@tonic-gate 			*p++ = '\0';	/* end option and point to value */
26567c478bd9Sstevel@tonic-gate 			valp = p;
26577c478bd9Sstevel@tonic-gate 		}
26587c478bd9Sstevel@tonic-gate 		/*
26597c478bd9Sstevel@tonic-gate 		 * set option into options table
26607c478bd9Sstevel@tonic-gate 		 */
26617c478bd9Sstevel@tonic-gate 		if (create)
26627c478bd9Sstevel@tonic-gate 			setflg |= VFS_CREATEOPT;
26637c478bd9Sstevel@tonic-gate 		vfs_setmntopt_nolock(mops, s, valp, setflg, 0);
26647c478bd9Sstevel@tonic-gate 		if (cp != NULL)
26657c478bd9Sstevel@tonic-gate 			*cp = ',';	/* restore the comma */
26667c478bd9Sstevel@tonic-gate 		if (valp != NULL)
26677c478bd9Sstevel@tonic-gate 			*ep = '=';	/* restore the equals */
26687c478bd9Sstevel@tonic-gate 		s = nextop;
26697c478bd9Sstevel@tonic-gate 	}
26707c478bd9Sstevel@tonic-gate }
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate /*
26737c478bd9Sstevel@tonic-gate  * Function to inquire if an option exists in a mount options table.
26747c478bd9Sstevel@tonic-gate  * Returns a pointer to the option if it exists, else NULL.
26757c478bd9Sstevel@tonic-gate  *
26767c478bd9Sstevel@tonic-gate  * This function is *not* for general use by filesystems.
26777c478bd9Sstevel@tonic-gate  *
26787c478bd9Sstevel@tonic-gate  * Note: caller is responsible for locking the vfs list, if needed,
26797c478bd9Sstevel@tonic-gate  *       to protect mops.
26807c478bd9Sstevel@tonic-gate  */
26817c478bd9Sstevel@tonic-gate struct mntopt *
26827c478bd9Sstevel@tonic-gate vfs_hasopt(const mntopts_t *mops, const char *opt)
26837c478bd9Sstevel@tonic-gate {
26847c478bd9Sstevel@tonic-gate 	struct mntopt *mop;
26857c478bd9Sstevel@tonic-gate 	uint_t i, count;
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate 	count = mops->mo_count;
26887c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
26897c478bd9Sstevel@tonic-gate 		mop = &mops->mo_list[i];
26907c478bd9Sstevel@tonic-gate 
26917c478bd9Sstevel@tonic-gate 		if (mop->mo_flags & MO_EMPTY)
26927c478bd9Sstevel@tonic-gate 			continue;
26937c478bd9Sstevel@tonic-gate 		if (strcmp(opt, mop->mo_name) == 0)
26947c478bd9Sstevel@tonic-gate 			return (mop);
26957c478bd9Sstevel@tonic-gate 	}
26967c478bd9Sstevel@tonic-gate 	return (NULL);
26977c478bd9Sstevel@tonic-gate }
26987c478bd9Sstevel@tonic-gate 
26997c478bd9Sstevel@tonic-gate /*
27007c478bd9Sstevel@tonic-gate  * Function to inquire if an option is set in a mount options table.
27017c478bd9Sstevel@tonic-gate  * Returns non-zero if set and fills in the arg pointer with a pointer to
27027c478bd9Sstevel@tonic-gate  * the argument string or NULL if there is no argument string.
27037c478bd9Sstevel@tonic-gate  */
27047c478bd9Sstevel@tonic-gate static int
27057c478bd9Sstevel@tonic-gate vfs_optionisset_nolock(const mntopts_t *mops, const char *opt, char **argp)
27067c478bd9Sstevel@tonic-gate {
27077c478bd9Sstevel@tonic-gate 	struct mntopt *mop;
27087c478bd9Sstevel@tonic-gate 	uint_t i, count;
27097c478bd9Sstevel@tonic-gate 
27107c478bd9Sstevel@tonic-gate 	count = mops->mo_count;
27117c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
27127c478bd9Sstevel@tonic-gate 		mop = &mops->mo_list[i];
27137c478bd9Sstevel@tonic-gate 
27147c478bd9Sstevel@tonic-gate 		if (mop->mo_flags & MO_EMPTY)
27157c478bd9Sstevel@tonic-gate 			continue;
27167c478bd9Sstevel@tonic-gate 		if (strcmp(opt, mop->mo_name))
27177c478bd9Sstevel@tonic-gate 			continue;
27187c478bd9Sstevel@tonic-gate 		if ((mop->mo_flags & MO_SET) == 0)
27197c478bd9Sstevel@tonic-gate 			return (0);
27207c478bd9Sstevel@tonic-gate 		if (argp != NULL && (mop->mo_flags & MO_HASVALUE) != 0)
27217c478bd9Sstevel@tonic-gate 			*argp = mop->mo_arg;
27227c478bd9Sstevel@tonic-gate 		return (1);
27237c478bd9Sstevel@tonic-gate 	}
27247c478bd9Sstevel@tonic-gate 	return (0);
27257c478bd9Sstevel@tonic-gate }
27267c478bd9Sstevel@tonic-gate 
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate int
27297c478bd9Sstevel@tonic-gate vfs_optionisset(const struct vfs *vfsp, const char *opt, char **argp)
27307c478bd9Sstevel@tonic-gate {
27317c478bd9Sstevel@tonic-gate 	int ret;
27327c478bd9Sstevel@tonic-gate 
27337c478bd9Sstevel@tonic-gate 	vfs_list_read_lock();
27347c478bd9Sstevel@tonic-gate 	ret = vfs_optionisset_nolock(&vfsp->vfs_mntopts, opt, argp);
27357c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
27367c478bd9Sstevel@tonic-gate 	return (ret);
27377c478bd9Sstevel@tonic-gate }
27387c478bd9Sstevel@tonic-gate 
27397c478bd9Sstevel@tonic-gate 
27407c478bd9Sstevel@tonic-gate /*
27417c478bd9Sstevel@tonic-gate  * Construct a comma separated string of the options set in the given
27427c478bd9Sstevel@tonic-gate  * mount table, return the string in the given buffer.  Return non-zero if
27437c478bd9Sstevel@tonic-gate  * the buffer would overflow.
27447c478bd9Sstevel@tonic-gate  *
27457c478bd9Sstevel@tonic-gate  * This function is *not* for general use by filesystems.
27467c478bd9Sstevel@tonic-gate  *
27477c478bd9Sstevel@tonic-gate  * Note: caller is responsible for locking the vfs list, if needed,
27487c478bd9Sstevel@tonic-gate  *       to protect mp.
27497c478bd9Sstevel@tonic-gate  */
27507c478bd9Sstevel@tonic-gate int
27517c478bd9Sstevel@tonic-gate vfs_buildoptionstr(const mntopts_t *mp, char *buf, int len)
27527c478bd9Sstevel@tonic-gate {
27537c478bd9Sstevel@tonic-gate 	char *cp;
27547c478bd9Sstevel@tonic-gate 	uint_t i;
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate 	buf[0] = '\0';
27577c478bd9Sstevel@tonic-gate 	cp = buf;
27587c478bd9Sstevel@tonic-gate 	for (i = 0; i < mp->mo_count; i++) {
27597c478bd9Sstevel@tonic-gate 		struct mntopt *mop;
27607c478bd9Sstevel@tonic-gate 
27617c478bd9Sstevel@tonic-gate 		mop = &mp->mo_list[i];
27627c478bd9Sstevel@tonic-gate 		if (mop->mo_flags & MO_SET) {
27637c478bd9Sstevel@tonic-gate 			int optlen, comma = 0;
27647c478bd9Sstevel@tonic-gate 
27657c478bd9Sstevel@tonic-gate 			if (buf[0] != '\0')
27667c478bd9Sstevel@tonic-gate 				comma = 1;
27677c478bd9Sstevel@tonic-gate 			optlen = strlen(mop->mo_name);
27687c478bd9Sstevel@tonic-gate 			if (strlen(buf) + comma + optlen + 1 > len)
27697c478bd9Sstevel@tonic-gate 				goto err;
27707c478bd9Sstevel@tonic-gate 			if (comma)
27717c478bd9Sstevel@tonic-gate 				*cp++ = ',';
27727c478bd9Sstevel@tonic-gate 			(void) strcpy(cp, mop->mo_name);
27737c478bd9Sstevel@tonic-gate 			cp += optlen;
27747c478bd9Sstevel@tonic-gate 			/*
27757c478bd9Sstevel@tonic-gate 			 * Append option value if there is one
27767c478bd9Sstevel@tonic-gate 			 */
27777c478bd9Sstevel@tonic-gate 			if (mop->mo_arg != NULL) {
27787c478bd9Sstevel@tonic-gate 				int arglen;
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 				arglen = strlen(mop->mo_arg);
27817c478bd9Sstevel@tonic-gate 				if (strlen(buf) + arglen + 2 > len)
27827c478bd9Sstevel@tonic-gate 					goto err;
27837c478bd9Sstevel@tonic-gate 				*cp++ = '=';
27847c478bd9Sstevel@tonic-gate 				(void) strcpy(cp, mop->mo_arg);
27857c478bd9Sstevel@tonic-gate 				cp += arglen;
27867c478bd9Sstevel@tonic-gate 			}
27877c478bd9Sstevel@tonic-gate 		}
27887c478bd9Sstevel@tonic-gate 	}
27897c478bd9Sstevel@tonic-gate 	return (0);
27907c478bd9Sstevel@tonic-gate err:
27917c478bd9Sstevel@tonic-gate 	return (EOVERFLOW);
27927c478bd9Sstevel@tonic-gate }
27937c478bd9Sstevel@tonic-gate 
27947c478bd9Sstevel@tonic-gate static void
27957c478bd9Sstevel@tonic-gate vfs_freecancelopt(char **moc)
27967c478bd9Sstevel@tonic-gate {
27977c478bd9Sstevel@tonic-gate 	if (moc != NULL) {
27987c478bd9Sstevel@tonic-gate 		int ccnt = 0;
27997c478bd9Sstevel@tonic-gate 		char **cp;
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate 		for (cp = moc; *cp != NULL; cp++) {
28027c478bd9Sstevel@tonic-gate 			kmem_free(*cp, strlen(*cp) + 1);
28037c478bd9Sstevel@tonic-gate 			ccnt++;
28047c478bd9Sstevel@tonic-gate 		}
28057c478bd9Sstevel@tonic-gate 		kmem_free(moc, (ccnt + 1) * sizeof (char *));
28067c478bd9Sstevel@tonic-gate 	}
28077c478bd9Sstevel@tonic-gate }
28087c478bd9Sstevel@tonic-gate 
28097c478bd9Sstevel@tonic-gate static void
28107c478bd9Sstevel@tonic-gate vfs_freeopt(mntopt_t *mop)
28117c478bd9Sstevel@tonic-gate {
28127c478bd9Sstevel@tonic-gate 	if (mop->mo_name != NULL)
28137c478bd9Sstevel@tonic-gate 		kmem_free(mop->mo_name, strlen(mop->mo_name) + 1);
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate 	vfs_freecancelopt(mop->mo_cancel);
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate 	if (mop->mo_arg != NULL)
28187c478bd9Sstevel@tonic-gate 		kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
28197c478bd9Sstevel@tonic-gate }
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate /*
28227c478bd9Sstevel@tonic-gate  * Free a mount options table
28237c478bd9Sstevel@tonic-gate  *
28247c478bd9Sstevel@tonic-gate  * This function is *not* for general use by filesystems.
28257c478bd9Sstevel@tonic-gate  *
28267c478bd9Sstevel@tonic-gate  * Note: caller is responsible for locking the vfs list, if needed,
28277c478bd9Sstevel@tonic-gate  *       to protect mp.
28287c478bd9Sstevel@tonic-gate  */
28297c478bd9Sstevel@tonic-gate void
28307c478bd9Sstevel@tonic-gate vfs_freeopttbl(mntopts_t *mp)
28317c478bd9Sstevel@tonic-gate {
28327c478bd9Sstevel@tonic-gate 	uint_t i, count;
28337c478bd9Sstevel@tonic-gate 
28347c478bd9Sstevel@tonic-gate 	count = mp->mo_count;
28357c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
28367c478bd9Sstevel@tonic-gate 		vfs_freeopt(&mp->mo_list[i]);
28377c478bd9Sstevel@tonic-gate 	}
28387c478bd9Sstevel@tonic-gate 	if (count) {
28397c478bd9Sstevel@tonic-gate 		kmem_free(mp->mo_list, sizeof (mntopt_t) * count);
28407c478bd9Sstevel@tonic-gate 		mp->mo_count = 0;
28417c478bd9Sstevel@tonic-gate 		mp->mo_list = NULL;
28427c478bd9Sstevel@tonic-gate 	}
28437c478bd9Sstevel@tonic-gate }
28447c478bd9Sstevel@tonic-gate 
2845df2381bfSpraks 
2846df2381bfSpraks /* ARGSUSED */
2847df2381bfSpraks static int
2848df2381bfSpraks vfs_mntdummyread(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cred,
2849df2381bfSpraks 	caller_context_t *ct)
2850df2381bfSpraks {
2851df2381bfSpraks 	return (0);
2852df2381bfSpraks }
2853df2381bfSpraks 
2854df2381bfSpraks /* ARGSUSED */
2855df2381bfSpraks static int
2856df2381bfSpraks vfs_mntdummywrite(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cred,
2857df2381bfSpraks 	caller_context_t *ct)
2858df2381bfSpraks {
2859df2381bfSpraks 	return (0);
2860df2381bfSpraks }
2861df2381bfSpraks 
2862df2381bfSpraks /*
2863df2381bfSpraks  * The dummy vnode is currently used only by file events notification
2864df2381bfSpraks  * module which is just interested in the timestamps.
2865df2381bfSpraks  */
2866df2381bfSpraks /* ARGSUSED */
2867df2381bfSpraks static int
2868da6c28aaSamw vfs_mntdummygetattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2869da6c28aaSamw     caller_context_t *ct)
2870df2381bfSpraks {
2871df2381bfSpraks 	bzero(vap, sizeof (vattr_t));
2872df2381bfSpraks 	vap->va_type = VREG;
2873df2381bfSpraks 	vap->va_nlink = 1;
2874df2381bfSpraks 	vap->va_ctime = vfs_mnttab_ctime;
2875df2381bfSpraks 	/*
2876df2381bfSpraks 	 * it is ok to just copy mtime as the time will be monotonically
2877df2381bfSpraks 	 * increasing.
2878df2381bfSpraks 	 */
2879df2381bfSpraks 	vap->va_mtime = vfs_mnttab_mtime;
2880df2381bfSpraks 	vap->va_atime = vap->va_mtime;
2881df2381bfSpraks 	return (0);
2882df2381bfSpraks }
2883df2381bfSpraks 
2884df2381bfSpraks static void
2885df2381bfSpraks vfs_mnttabvp_setup(void)
2886df2381bfSpraks {
2887df2381bfSpraks 	vnode_t *tvp;
2888df2381bfSpraks 	vnodeops_t *vfs_mntdummyvnops;
2889df2381bfSpraks 	const fs_operation_def_t mnt_dummyvnodeops_template[] = {
2890df2381bfSpraks 		VOPNAME_READ, 		{ .vop_read = vfs_mntdummyread },
2891df2381bfSpraks 		VOPNAME_WRITE, 		{ .vop_write = vfs_mntdummywrite },
2892df2381bfSpraks 		VOPNAME_GETATTR,	{ .vop_getattr = vfs_mntdummygetattr },
2893df2381bfSpraks 		VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
2894df2381bfSpraks 		NULL,			NULL
2895df2381bfSpraks 	};
2896df2381bfSpraks 
2897df2381bfSpraks 	if (vn_make_ops("mnttab", mnt_dummyvnodeops_template,
2898df2381bfSpraks 	    &vfs_mntdummyvnops) != 0) {
2899df2381bfSpraks 		cmn_err(CE_WARN, "vfs_mnttabvp_setup: vn_make_ops failed");
2900df2381bfSpraks 		/* Shouldn't happen, but not bad enough to panic */
2901df2381bfSpraks 		return;
2902df2381bfSpraks 	}
2903df2381bfSpraks 
2904df2381bfSpraks 	/*
2905df2381bfSpraks 	 * A global dummy vnode is allocated to represent mntfs files.
2906df2381bfSpraks 	 * The mntfs file (/etc/mnttab) can be monitored for file events
2907df2381bfSpraks 	 * and receive an event when mnttab changes. Dummy VOP calls
2908df2381bfSpraks 	 * will be made on this vnode. The file events notification module
2909df2381bfSpraks 	 * intercepts this vnode and delivers relevant events.
2910df2381bfSpraks 	 */
2911df2381bfSpraks 	tvp = vn_alloc(KM_SLEEP);
2912df2381bfSpraks 	tvp->v_flag = VNOMOUNT|VNOMAP|VNOSWAP|VNOCACHE;
2913df2381bfSpraks 	vn_setops(tvp, vfs_mntdummyvnops);
2914df2381bfSpraks 	tvp->v_type = VREG;
2915df2381bfSpraks 	/*
2916df2381bfSpraks 	 * The mnt dummy ops do not reference v_data.
2917df2381bfSpraks 	 * No other module intercepting this vnode should either.
2918df2381bfSpraks 	 * Just set it to point to itself.
2919df2381bfSpraks 	 */
2920df2381bfSpraks 	tvp->v_data = (caddr_t)tvp;
2921df2381bfSpraks 	tvp->v_vfsp = rootvfs;
2922df2381bfSpraks 	vfs_mntdummyvp = tvp;
2923df2381bfSpraks }
2924df2381bfSpraks 
2925df2381bfSpraks /*
2926df2381bfSpraks  * performs fake read/write ops
2927df2381bfSpraks  */
2928df2381bfSpraks static void
2929df2381bfSpraks vfs_mnttab_rwop(int rw)
2930df2381bfSpraks {
2931df2381bfSpraks 	struct uio	uio;
2932df2381bfSpraks 	struct iovec	iov;
2933df2381bfSpraks 	char	buf[1];
2934df2381bfSpraks 
2935df2381bfSpraks 	if (vfs_mntdummyvp == NULL)
2936df2381bfSpraks 		return;
2937df2381bfSpraks 
2938df2381bfSpraks 	bzero(&uio, sizeof (uio));
2939df2381bfSpraks 	bzero(&iov, sizeof (iov));
2940df2381bfSpraks 	iov.iov_base = buf;
2941df2381bfSpraks 	iov.iov_len = 0;
2942df2381bfSpraks 	uio.uio_iov = &iov;
2943df2381bfSpraks 	uio.uio_iovcnt = 1;
2944df2381bfSpraks 	uio.uio_loffset = 0;
2945df2381bfSpraks 	uio.uio_segflg = UIO_SYSSPACE;
2946df2381bfSpraks 	uio.uio_resid = 0;
2947df2381bfSpraks 	if (rw) {
2948df2381bfSpraks 		(void) VOP_WRITE(vfs_mntdummyvp, &uio, 0, kcred, NULL);
2949df2381bfSpraks 	} else {
2950df2381bfSpraks 		(void) VOP_READ(vfs_mntdummyvp, &uio, 0, kcred, NULL);
2951df2381bfSpraks 	}
2952df2381bfSpraks }
2953df2381bfSpraks 
2954df2381bfSpraks /*
2955df2381bfSpraks  * Generate a write operation.
2956df2381bfSpraks  */
2957df2381bfSpraks void
2958df2381bfSpraks vfs_mnttab_writeop(void)
2959df2381bfSpraks {
2960df2381bfSpraks 	vfs_mnttab_rwop(1);
2961df2381bfSpraks }
2962df2381bfSpraks 
2963df2381bfSpraks /*
2964df2381bfSpraks  * Generate a read operation.
2965df2381bfSpraks  */
2966df2381bfSpraks void
2967df2381bfSpraks vfs_mnttab_readop(void)
2968df2381bfSpraks {
2969df2381bfSpraks 	vfs_mnttab_rwop(0);
2970df2381bfSpraks }
2971df2381bfSpraks 
29727c478bd9Sstevel@tonic-gate /*
29737c478bd9Sstevel@tonic-gate  * Free any mnttab information recorded in the vfs struct.
29747c478bd9Sstevel@tonic-gate  * The vfs must not be on the vfs list.
29757c478bd9Sstevel@tonic-gate  */
29767c478bd9Sstevel@tonic-gate static void
29777c478bd9Sstevel@tonic-gate vfs_freemnttab(struct vfs *vfsp)
29787c478bd9Sstevel@tonic-gate {
29797c478bd9Sstevel@tonic-gate 	ASSERT(!VFS_ON_LIST(vfsp));
29807c478bd9Sstevel@tonic-gate 
29817c478bd9Sstevel@tonic-gate 	/*
29827c478bd9Sstevel@tonic-gate 	 * Free device and mount point information
29837c478bd9Sstevel@tonic-gate 	 */
29847c478bd9Sstevel@tonic-gate 	if (vfsp->vfs_mntpt != NULL) {
29857c478bd9Sstevel@tonic-gate 		refstr_rele(vfsp->vfs_mntpt);
29867c478bd9Sstevel@tonic-gate 		vfsp->vfs_mntpt = NULL;
29877c478bd9Sstevel@tonic-gate 	}
29887c478bd9Sstevel@tonic-gate 	if (vfsp->vfs_resource != NULL) {
29897c478bd9Sstevel@tonic-gate 		refstr_rele(vfsp->vfs_resource);
29907c478bd9Sstevel@tonic-gate 		vfsp->vfs_resource = NULL;
29917c478bd9Sstevel@tonic-gate 	}
29927c478bd9Sstevel@tonic-gate 	/*
29937c478bd9Sstevel@tonic-gate 	 * Now free mount options information
29947c478bd9Sstevel@tonic-gate 	 */
29957c478bd9Sstevel@tonic-gate 	vfs_freeopttbl(&vfsp->vfs_mntopts);
29967c478bd9Sstevel@tonic-gate }
29977c478bd9Sstevel@tonic-gate 
29987c478bd9Sstevel@tonic-gate /*
29997c478bd9Sstevel@tonic-gate  * Return the last mnttab modification time
30007c478bd9Sstevel@tonic-gate  */
30017c478bd9Sstevel@tonic-gate void
30027c478bd9Sstevel@tonic-gate vfs_mnttab_modtime(timespec_t *ts)
30037c478bd9Sstevel@tonic-gate {
30047c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&vfslist));
30057c478bd9Sstevel@tonic-gate 	*ts = vfs_mnttab_mtime;
30067c478bd9Sstevel@tonic-gate }
30077c478bd9Sstevel@tonic-gate 
30087c478bd9Sstevel@tonic-gate /*
30097c478bd9Sstevel@tonic-gate  * See if mnttab is changed
30107c478bd9Sstevel@tonic-gate  */
30117c478bd9Sstevel@tonic-gate void
30127c478bd9Sstevel@tonic-gate vfs_mnttab_poll(timespec_t *old, struct pollhead **phpp)
30137c478bd9Sstevel@tonic-gate {
30147c478bd9Sstevel@tonic-gate 	int changed;
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate 	*phpp = (struct pollhead *)NULL;
30177c478bd9Sstevel@tonic-gate 
30187c478bd9Sstevel@tonic-gate 	/*
30197c478bd9Sstevel@tonic-gate 	 * Note: don't grab vfs list lock before accessing vfs_mnttab_mtime.
30207c478bd9Sstevel@tonic-gate 	 * Can lead to deadlock against vfs_mnttab_modtimeupd(). It is safe
30217c478bd9Sstevel@tonic-gate 	 * to not grab the vfs list lock because tv_sec is monotonically
30227c478bd9Sstevel@tonic-gate 	 * increasing.
30237c478bd9Sstevel@tonic-gate 	 */
30247c478bd9Sstevel@tonic-gate 
30257c478bd9Sstevel@tonic-gate 	changed = (old->tv_nsec != vfs_mnttab_mtime.tv_nsec) ||
30267c478bd9Sstevel@tonic-gate 	    (old->tv_sec != vfs_mnttab_mtime.tv_sec);
30277c478bd9Sstevel@tonic-gate 	if (!changed) {
30287c478bd9Sstevel@tonic-gate 		*phpp = &vfs_pollhd;
30297c478bd9Sstevel@tonic-gate 	}
30307c478bd9Sstevel@tonic-gate }
30317c478bd9Sstevel@tonic-gate 
3032835ee219SRobert Harris /* Provide a unique and monotonically-increasing timestamp. */
3033835ee219SRobert Harris void
3034835ee219SRobert Harris vfs_mono_time(timespec_t *ts)
3035835ee219SRobert Harris {
3036835ee219SRobert Harris 	static volatile hrtime_t hrt;		/* The saved time. */
3037835ee219SRobert Harris 	hrtime_t	newhrt, oldhrt;		/* For effecting the CAS. */
3038835ee219SRobert Harris 	timespec_t	newts;
3039835ee219SRobert Harris 
30404ba8a214SRobert Harris 	/*
30414ba8a214SRobert Harris 	 * Try gethrestime() first, but be prepared to fabricate a sensible
30424ba8a214SRobert Harris 	 * answer at the first sign of any trouble.
30434ba8a214SRobert Harris 	 */
3044835ee219SRobert Harris 	gethrestime(&newts);
3045835ee219SRobert Harris 	newhrt = ts2hrt(&newts);
30464ba8a214SRobert Harris 	for (;;) {
3047835ee219SRobert Harris 		oldhrt = hrt;
30484ba8a214SRobert Harris 		if (newhrt <= hrt)
30494ba8a214SRobert Harris 			newhrt = hrt + 1;
305075d94465SJosef 'Jeff' Sipek 		if (atomic_cas_64((uint64_t *)&hrt, oldhrt, newhrt) == oldhrt)
30514ba8a214SRobert Harris 			break;
3052835ee219SRobert Harris 	}
3053835ee219SRobert Harris 	hrt2ts(newhrt, ts);
3054835ee219SRobert Harris }
3055835ee219SRobert Harris 
30567c478bd9Sstevel@tonic-gate /*
30577c478bd9Sstevel@tonic-gate  * Update the mnttab modification time and wake up any waiters for
30587c478bd9Sstevel@tonic-gate  * mnttab changes
30597c478bd9Sstevel@tonic-gate  */
30607c478bd9Sstevel@tonic-gate void
30617c478bd9Sstevel@tonic-gate vfs_mnttab_modtimeupd()
30627c478bd9Sstevel@tonic-gate {
30634ba8a214SRobert Harris 	hrtime_t oldhrt, newhrt;
30644ba8a214SRobert Harris 
30657c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&vfslist));
30664ba8a214SRobert Harris 	oldhrt = ts2hrt(&vfs_mnttab_mtime);
30674ba8a214SRobert Harris 	gethrestime(&vfs_mnttab_mtime);
30684ba8a214SRobert Harris 	newhrt = ts2hrt(&vfs_mnttab_mtime);
30694ba8a214SRobert Harris 	if (oldhrt == (hrtime_t)0)
30707c478bd9Sstevel@tonic-gate 		vfs_mnttab_ctime = vfs_mnttab_mtime;
30714ba8a214SRobert Harris 	/*
30724ba8a214SRobert Harris 	 * Attempt to provide unique mtime (like uniqtime but not).
30734ba8a214SRobert Harris 	 */
30744ba8a214SRobert Harris 	if (newhrt == oldhrt) {
30754ba8a214SRobert Harris 		newhrt++;
30764ba8a214SRobert Harris 		hrt2ts(newhrt, &vfs_mnttab_mtime);
30774ba8a214SRobert Harris 	}
30787c478bd9Sstevel@tonic-gate 	pollwakeup(&vfs_pollhd, (short)POLLRDBAND);
3079df2381bfSpraks 	vfs_mnttab_writeop();
30807c478bd9Sstevel@tonic-gate }
30817c478bd9Sstevel@tonic-gate 
30827c478bd9Sstevel@tonic-gate int
30837c478bd9Sstevel@tonic-gate dounmount(struct vfs *vfsp, int flag, cred_t *cr)
30847c478bd9Sstevel@tonic-gate {
30857c478bd9Sstevel@tonic-gate 	vnode_t *coveredvp;
30867c478bd9Sstevel@tonic-gate 	int error;
30875a59a8b3Srsb 	extern void teardown_vopstats(vfs_t *);
30887c478bd9Sstevel@tonic-gate 
30897c478bd9Sstevel@tonic-gate 	/*
30907c478bd9Sstevel@tonic-gate 	 * Get covered vnode. This will be NULL if the vfs is not linked
30917c478bd9Sstevel@tonic-gate 	 * into the file system name space (i.e., domount() with MNT_NOSPICE).
30927c478bd9Sstevel@tonic-gate 	 */
30937c478bd9Sstevel@tonic-gate 	coveredvp = vfsp->vfs_vnodecovered;
30947c478bd9Sstevel@tonic-gate 	ASSERT(coveredvp == NULL || vn_vfswlock_held(coveredvp));
30957c478bd9Sstevel@tonic-gate 
30967c478bd9Sstevel@tonic-gate 	/*
30977c478bd9Sstevel@tonic-gate 	 * Purge all dnlc entries for this vfs.
30987c478bd9Sstevel@tonic-gate 	 */
30997c478bd9Sstevel@tonic-gate 	(void) dnlc_purge_vfsp(vfsp, 0);
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 	/* For forcible umount, skip VFS_SYNC() since it may hang */
31027c478bd9Sstevel@tonic-gate 	if ((flag & MS_FORCE) == 0)
31037c478bd9Sstevel@tonic-gate 		(void) VFS_SYNC(vfsp, 0, cr);
31047c478bd9Sstevel@tonic-gate 
31057c478bd9Sstevel@tonic-gate 	/*
31067c478bd9Sstevel@tonic-gate 	 * Lock the vfs to maintain fs status quo during unmount.  This
31077c478bd9Sstevel@tonic-gate 	 * has to be done after the sync because ufs_update tries to acquire
31087c478bd9Sstevel@tonic-gate 	 * the vfs_reflock.
31097c478bd9Sstevel@tonic-gate 	 */
31107c478bd9Sstevel@tonic-gate 	vfs_lock_wait(vfsp);
31117c478bd9Sstevel@tonic-gate 
31127c478bd9Sstevel@tonic-gate 	if (error = VFS_UNMOUNT(vfsp, flag, cr)) {
31137c478bd9Sstevel@tonic-gate 		vfs_unlock(vfsp);
31147c478bd9Sstevel@tonic-gate 		if (coveredvp != NULL)
31157c478bd9Sstevel@tonic-gate 			vn_vfsunlock(coveredvp);
31167c478bd9Sstevel@tonic-gate 	} else if (coveredvp != NULL) {
31175a59a8b3Srsb 		teardown_vopstats(vfsp);
31187c478bd9Sstevel@tonic-gate 		/*
31197c478bd9Sstevel@tonic-gate 		 * vfs_remove() will do a VN_RELE(vfsp->vfs_vnodecovered)
31207c478bd9Sstevel@tonic-gate 		 * when it frees vfsp so we do a VN_HOLD() so we can
31217c478bd9Sstevel@tonic-gate 		 * continue to use coveredvp afterwards.
31227c478bd9Sstevel@tonic-gate 		 */
31237c478bd9Sstevel@tonic-gate 		VN_HOLD(coveredvp);
31247c478bd9Sstevel@tonic-gate 		vfs_remove(vfsp);
31257c478bd9Sstevel@tonic-gate 		vn_vfsunlock(coveredvp);
31267c478bd9Sstevel@tonic-gate 		VN_RELE(coveredvp);
31277c478bd9Sstevel@tonic-gate 	} else {
31285a59a8b3Srsb 		teardown_vopstats(vfsp);
31297c478bd9Sstevel@tonic-gate 		/*
31307c478bd9Sstevel@tonic-gate 		 * Release the reference to vfs that is not linked
31317c478bd9Sstevel@tonic-gate 		 * into the name space.
31327c478bd9Sstevel@tonic-gate 		 */
31337c478bd9Sstevel@tonic-gate 		vfs_unlock(vfsp);
31347c478bd9Sstevel@tonic-gate 		VFS_RELE(vfsp);
31357c478bd9Sstevel@tonic-gate 	}
31367c478bd9Sstevel@tonic-gate 	return (error);
31377c478bd9Sstevel@tonic-gate }
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate 
31407c478bd9Sstevel@tonic-gate /*
31417c478bd9Sstevel@tonic-gate  * Vfs_unmountall() is called by uadmin() to unmount all
31427c478bd9Sstevel@tonic-gate  * mounted file systems (except the root file system) during shutdown.
31437c478bd9Sstevel@tonic-gate  * It follows the existing locking protocol when traversing the vfs list
31447c478bd9Sstevel@tonic-gate  * to sync and unmount vfses. Even though there should be no
31457c478bd9Sstevel@tonic-gate  * other thread running while the system is shutting down, it is prudent
31467c478bd9Sstevel@tonic-gate  * to still follow the locking protocol.
31477c478bd9Sstevel@tonic-gate  */
31487c478bd9Sstevel@tonic-gate void
31497c478bd9Sstevel@tonic-gate vfs_unmountall(void)
31507c478bd9Sstevel@tonic-gate {
31517c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
31527c478bd9Sstevel@tonic-gate 	struct vfs *prev_vfsp = NULL;
31537c478bd9Sstevel@tonic-gate 	int error;
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate 	/*
31567c478bd9Sstevel@tonic-gate 	 * Toss all dnlc entries now so that the per-vfs sync
31577c478bd9Sstevel@tonic-gate 	 * and unmount operations don't have to slog through
31587c478bd9Sstevel@tonic-gate 	 * a bunch of uninteresting vnodes over and over again.
31597c478bd9Sstevel@tonic-gate 	 */
31607c478bd9Sstevel@tonic-gate 	dnlc_purge();
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 	vfs_list_lock();
31637c478bd9Sstevel@tonic-gate 	for (vfsp = rootvfs->vfs_prev; vfsp != rootvfs; vfsp = prev_vfsp) {
31647c478bd9Sstevel@tonic-gate 		prev_vfsp = vfsp->vfs_prev;
31657c478bd9Sstevel@tonic-gate 
31667c478bd9Sstevel@tonic-gate 		if (vfs_lock(vfsp) != 0)
31677c478bd9Sstevel@tonic-gate 			continue;
31687c478bd9Sstevel@tonic-gate 		error = vn_vfswlock(vfsp->vfs_vnodecovered);
31697c478bd9Sstevel@tonic-gate 		vfs_unlock(vfsp);
31707c478bd9Sstevel@tonic-gate 		if (error)
31717c478bd9Sstevel@tonic-gate 			continue;
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate 		vfs_list_unlock();
31747c478bd9Sstevel@tonic-gate 
31757c478bd9Sstevel@tonic-gate 		(void) VFS_SYNC(vfsp, SYNC_CLOSE, CRED());
31767c478bd9Sstevel@tonic-gate 		(void) dounmount(vfsp, 0, CRED());
31777c478bd9Sstevel@tonic-gate 
31787c478bd9Sstevel@tonic-gate 		/*
31797c478bd9Sstevel@tonic-gate 		 * Since we dropped the vfslist lock above we must
31807c478bd9Sstevel@tonic-gate 		 * verify that next_vfsp still exists, else start over.
31817c478bd9Sstevel@tonic-gate 		 */
31827c478bd9Sstevel@tonic-gate 		vfs_list_lock();
31837c478bd9Sstevel@tonic-gate 		for (vfsp = rootvfs->vfs_prev;
31847c478bd9Sstevel@tonic-gate 		    vfsp != rootvfs; vfsp = vfsp->vfs_prev)
31857c478bd9Sstevel@tonic-gate 			if (vfsp == prev_vfsp)
31867c478bd9Sstevel@tonic-gate 				break;
31877c478bd9Sstevel@tonic-gate 		if (vfsp == rootvfs && prev_vfsp != rootvfs)
31887c478bd9Sstevel@tonic-gate 			prev_vfsp = rootvfs->vfs_prev;
31897c478bd9Sstevel@tonic-gate 	}
31907c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
31917c478bd9Sstevel@tonic-gate }
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate /*
31947c478bd9Sstevel@tonic-gate  * Called to add an entry to the end of the vfs mount in progress list
31957c478bd9Sstevel@tonic-gate  */
31967c478bd9Sstevel@tonic-gate void
31977c478bd9Sstevel@tonic-gate vfs_addmip(dev_t dev, struct vfs *vfsp)
31987c478bd9Sstevel@tonic-gate {
31997c478bd9Sstevel@tonic-gate 	struct ipmnt *mipp;
32007c478bd9Sstevel@tonic-gate 
32017c478bd9Sstevel@tonic-gate 	mipp = (struct ipmnt *)kmem_alloc(sizeof (struct ipmnt), KM_SLEEP);
32027c478bd9Sstevel@tonic-gate 	mipp->mip_next = NULL;
32037c478bd9Sstevel@tonic-gate 	mipp->mip_dev = dev;
32047c478bd9Sstevel@tonic-gate 	mipp->mip_vfsp = vfsp;
32057c478bd9Sstevel@tonic-gate 	mutex_enter(&vfs_miplist_mutex);
32067c478bd9Sstevel@tonic-gate 	if (vfs_miplist_end != NULL)
32077c478bd9Sstevel@tonic-gate 		vfs_miplist_end->mip_next = mipp;
32087c478bd9Sstevel@tonic-gate 	else
32097c478bd9Sstevel@tonic-gate 		vfs_miplist = mipp;
32107c478bd9Sstevel@tonic-gate 	vfs_miplist_end = mipp;
32117c478bd9Sstevel@tonic-gate 	mutex_exit(&vfs_miplist_mutex);
32127c478bd9Sstevel@tonic-gate }
32137c478bd9Sstevel@tonic-gate 
32147c478bd9Sstevel@tonic-gate /*
32157c478bd9Sstevel@tonic-gate  * Called to remove an entry from the mount in progress list
32167c478bd9Sstevel@tonic-gate  * Either because the mount completed or it failed.
32177c478bd9Sstevel@tonic-gate  */
32187c478bd9Sstevel@tonic-gate void
32197c478bd9Sstevel@tonic-gate vfs_delmip(struct vfs *vfsp)
32207c478bd9Sstevel@tonic-gate {
32217c478bd9Sstevel@tonic-gate 	struct ipmnt *mipp, *mipprev;
32227c478bd9Sstevel@tonic-gate 
32237c478bd9Sstevel@tonic-gate 	mutex_enter(&vfs_miplist_mutex);
32247c478bd9Sstevel@tonic-gate 	mipprev = NULL;
32257c478bd9Sstevel@tonic-gate 	for (mipp = vfs_miplist;
32267c478bd9Sstevel@tonic-gate 	    mipp && mipp->mip_vfsp != vfsp; mipp = mipp->mip_next) {
32277c478bd9Sstevel@tonic-gate 		mipprev = mipp;
32287c478bd9Sstevel@tonic-gate 	}
32297c478bd9Sstevel@tonic-gate 	if (mipp == NULL)
32307c478bd9Sstevel@tonic-gate 		return; /* shouldn't happen */
32317c478bd9Sstevel@tonic-gate 	if (mipp == vfs_miplist_end)
32327c478bd9Sstevel@tonic-gate 		vfs_miplist_end = mipprev;
32337c478bd9Sstevel@tonic-gate 	if (mipprev == NULL)
32347c478bd9Sstevel@tonic-gate 		vfs_miplist = mipp->mip_next;
32357c478bd9Sstevel@tonic-gate 	else
32367c478bd9Sstevel@tonic-gate 		mipprev->mip_next = mipp->mip_next;
32377c478bd9Sstevel@tonic-gate 	mutex_exit(&vfs_miplist_mutex);
32387c478bd9Sstevel@tonic-gate 	kmem_free(mipp, sizeof (struct ipmnt));
32397c478bd9Sstevel@tonic-gate }
32407c478bd9Sstevel@tonic-gate 
32417c478bd9Sstevel@tonic-gate /*
32427c478bd9Sstevel@tonic-gate  * vfs_add is called by a specific filesystem's mount routine to add
32437c478bd9Sstevel@tonic-gate  * the new vfs into the vfs list/hash and to cover the mounted-on vnode.
32447c478bd9Sstevel@tonic-gate  * The vfs should already have been locked by the caller.
32457c478bd9Sstevel@tonic-gate  *
32467c478bd9Sstevel@tonic-gate  * coveredvp is NULL if this is the root.
32477c478bd9Sstevel@tonic-gate  */
32487c478bd9Sstevel@tonic-gate void
32497c478bd9Sstevel@tonic-gate vfs_add(vnode_t *coveredvp, struct vfs *vfsp, int mflag)
32507c478bd9Sstevel@tonic-gate {
32517c478bd9Sstevel@tonic-gate 	int newflag;
32527c478bd9Sstevel@tonic-gate 
32537c478bd9Sstevel@tonic-gate 	ASSERT(vfs_lock_held(vfsp));
32547c478bd9Sstevel@tonic-gate 	VFS_HOLD(vfsp);
32557c478bd9Sstevel@tonic-gate 	newflag = vfsp->vfs_flag;
32567c478bd9Sstevel@tonic-gate 	if (mflag & MS_RDONLY)
32577c478bd9Sstevel@tonic-gate 		newflag |= VFS_RDONLY;
32587c478bd9Sstevel@tonic-gate 	else
32597c478bd9Sstevel@tonic-gate 		newflag &= ~VFS_RDONLY;
32607c478bd9Sstevel@tonic-gate 	if (mflag & MS_NOSUID)
32617c478bd9Sstevel@tonic-gate 		newflag |= (VFS_NOSETUID|VFS_NODEVICES);
32627c478bd9Sstevel@tonic-gate 	else
32637c478bd9Sstevel@tonic-gate 		newflag &= ~(VFS_NOSETUID|VFS_NODEVICES);
32647c478bd9Sstevel@tonic-gate 	if (mflag & MS_NOMNTTAB)
32657c478bd9Sstevel@tonic-gate 		newflag |= VFS_NOMNTTAB;
32667c478bd9Sstevel@tonic-gate 	else
32677c478bd9Sstevel@tonic-gate 		newflag &= ~VFS_NOMNTTAB;
32687c478bd9Sstevel@tonic-gate 
32697c478bd9Sstevel@tonic-gate 	if (coveredvp != NULL) {
32707c478bd9Sstevel@tonic-gate 		ASSERT(vn_vfswlock_held(coveredvp));
32717c478bd9Sstevel@tonic-gate 		coveredvp->v_vfsmountedhere = vfsp;
32727c478bd9Sstevel@tonic-gate 		VN_HOLD(coveredvp);
32737c478bd9Sstevel@tonic-gate 	}
32747c478bd9Sstevel@tonic-gate 	vfsp->vfs_vnodecovered = coveredvp;
32757c478bd9Sstevel@tonic-gate 	vfsp->vfs_flag = newflag;
32767c478bd9Sstevel@tonic-gate 
32777c478bd9Sstevel@tonic-gate 	vfs_list_add(vfsp);
32787c478bd9Sstevel@tonic-gate }
32797c478bd9Sstevel@tonic-gate 
32807c478bd9Sstevel@tonic-gate /*
32817c478bd9Sstevel@tonic-gate  * Remove a vfs from the vfs list, null out the pointer from the
32827c478bd9Sstevel@tonic-gate  * covered vnode to the vfs (v_vfsmountedhere), and null out the pointer
32837c478bd9Sstevel@tonic-gate  * from the vfs to the covered vnode (vfs_vnodecovered). Release the
32847c478bd9Sstevel@tonic-gate  * reference to the vfs and to the covered vnode.
32857c478bd9Sstevel@tonic-gate  *
32867c478bd9Sstevel@tonic-gate  * Called from dounmount after it's confirmed with the file system
32877c478bd9Sstevel@tonic-gate  * that the unmount is legal.
32887c478bd9Sstevel@tonic-gate  */
32897c478bd9Sstevel@tonic-gate void
32907c478bd9Sstevel@tonic-gate vfs_remove(struct vfs *vfsp)
32917c478bd9Sstevel@tonic-gate {
32927c478bd9Sstevel@tonic-gate 	vnode_t *vp;
32937c478bd9Sstevel@tonic-gate 
32947c478bd9Sstevel@tonic-gate 	ASSERT(vfs_lock_held(vfsp));
32957c478bd9Sstevel@tonic-gate 
32967c478bd9Sstevel@tonic-gate 	/*
32977c478bd9Sstevel@tonic-gate 	 * Can't unmount root.  Should never happen because fs will
32987c478bd9Sstevel@tonic-gate 	 * be busy.
32997c478bd9Sstevel@tonic-gate 	 */
33007c478bd9Sstevel@tonic-gate 	if (vfsp == rootvfs)
3301ae115bc7Smrj 		panic("vfs_remove: unmounting root");
33027c478bd9Sstevel@tonic-gate 
33037c478bd9Sstevel@tonic-gate 	vfs_list_remove(vfsp);
33047c478bd9Sstevel@tonic-gate 
33057c478bd9Sstevel@tonic-gate 	/*
33067c478bd9Sstevel@tonic-gate 	 * Unhook from the file system name space.
33077c478bd9Sstevel@tonic-gate 	 */
33087c478bd9Sstevel@tonic-gate 	vp = vfsp->vfs_vnodecovered;
33097c478bd9Sstevel@tonic-gate 	ASSERT(vn_vfswlock_held(vp));
33107c478bd9Sstevel@tonic-gate 	vp->v_vfsmountedhere = NULL;
33117c478bd9Sstevel@tonic-gate 	vfsp->vfs_vnodecovered = NULL;
33127c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
33137c478bd9Sstevel@tonic-gate 
33147c478bd9Sstevel@tonic-gate 	/*
33157c478bd9Sstevel@tonic-gate 	 * Release lock and wakeup anybody waiting.
33167c478bd9Sstevel@tonic-gate 	 */
33177c478bd9Sstevel@tonic-gate 	vfs_unlock(vfsp);
33187c478bd9Sstevel@tonic-gate 	VFS_RELE(vfsp);
33197c478bd9Sstevel@tonic-gate }
33207c478bd9Sstevel@tonic-gate 
33217c478bd9Sstevel@tonic-gate /*
33227c478bd9Sstevel@tonic-gate  * Lock a filesystem to prevent access to it while mounting,
33237c478bd9Sstevel@tonic-gate  * unmounting and syncing.  Return EBUSY immediately if lock
33247c478bd9Sstevel@tonic-gate  * can't be acquired.
33257c478bd9Sstevel@tonic-gate  */
33267c478bd9Sstevel@tonic-gate int
33277c478bd9Sstevel@tonic-gate vfs_lock(vfs_t *vfsp)
33287c478bd9Sstevel@tonic-gate {
33297c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
33307c478bd9Sstevel@tonic-gate 
33317c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vfsp);
33327c478bd9Sstevel@tonic-gate 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER))
33337c478bd9Sstevel@tonic-gate 		return (0);
33347c478bd9Sstevel@tonic-gate 
33357c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
33367c478bd9Sstevel@tonic-gate 	return (EBUSY);
33377c478bd9Sstevel@tonic-gate }
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate int
33407c478bd9Sstevel@tonic-gate vfs_rlock(vfs_t *vfsp)
33417c478bd9Sstevel@tonic-gate {
33427c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
33437c478bd9Sstevel@tonic-gate 
33447c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vfsp);
33457c478bd9Sstevel@tonic-gate 
33467c478bd9Sstevel@tonic-gate 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER))
33477c478bd9Sstevel@tonic-gate 		return (0);
33487c478bd9Sstevel@tonic-gate 
33497c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
33507c478bd9Sstevel@tonic-gate 	return (EBUSY);
33517c478bd9Sstevel@tonic-gate }
33527c478bd9Sstevel@tonic-gate 
33537c478bd9Sstevel@tonic-gate void
33547c478bd9Sstevel@tonic-gate vfs_lock_wait(vfs_t *vfsp)
33557c478bd9Sstevel@tonic-gate {
33567c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
33577c478bd9Sstevel@tonic-gate 
33587c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vfsp);
33597c478bd9Sstevel@tonic-gate 	rwst_enter(&vpvfsentry->ve_lock, RW_WRITER);
33607c478bd9Sstevel@tonic-gate }
33617c478bd9Sstevel@tonic-gate 
33627c478bd9Sstevel@tonic-gate void
33637c478bd9Sstevel@tonic-gate vfs_rlock_wait(vfs_t *vfsp)
33647c478bd9Sstevel@tonic-gate {
33657c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
33667c478bd9Sstevel@tonic-gate 
33677c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vfsp);
33687c478bd9Sstevel@tonic-gate 	rwst_enter(&vpvfsentry->ve_lock, RW_READER);
33697c478bd9Sstevel@tonic-gate }
33707c478bd9Sstevel@tonic-gate 
33717c478bd9Sstevel@tonic-gate /*
33727c478bd9Sstevel@tonic-gate  * Unlock a locked filesystem.
33737c478bd9Sstevel@tonic-gate  */
33747c478bd9Sstevel@tonic-gate void
33757c478bd9Sstevel@tonic-gate vfs_unlock(vfs_t *vfsp)
33767c478bd9Sstevel@tonic-gate {
33777c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
33787c478bd9Sstevel@tonic-gate 
33797c478bd9Sstevel@tonic-gate 	/*
33807c478bd9Sstevel@tonic-gate 	 * vfs_unlock will mimic sema_v behaviour to fix 4748018.
33817c478bd9Sstevel@tonic-gate 	 * And these changes should remain for the patch changes as it is.
33827c478bd9Sstevel@tonic-gate 	 */
33837c478bd9Sstevel@tonic-gate 	if (panicstr)
33847c478bd9Sstevel@tonic-gate 		return;
33857c478bd9Sstevel@tonic-gate 
33867c478bd9Sstevel@tonic-gate 	/*
33877c478bd9Sstevel@tonic-gate 	 * ve_refcount needs to be dropped twice here.
33887c478bd9Sstevel@tonic-gate 	 * 1. To release refernce after a call to vfs_locks_getlock()
33897c478bd9Sstevel@tonic-gate 	 * 2. To release the reference from the locking routines like
33907c478bd9Sstevel@tonic-gate 	 *    vfs_rlock_wait/vfs_wlock_wait/vfs_wlock etc,.
33917c478bd9Sstevel@tonic-gate 	 */
33927c478bd9Sstevel@tonic-gate 
33937c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vfsp);
33947c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
33957c478bd9Sstevel@tonic-gate 
33967c478bd9Sstevel@tonic-gate 	rwst_exit(&vpvfsentry->ve_lock);
33977c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
33987c478bd9Sstevel@tonic-gate }
33997c478bd9Sstevel@tonic-gate 
34007c478bd9Sstevel@tonic-gate /*
34017c478bd9Sstevel@tonic-gate  * Utility routine that allows a filesystem to construct its
34027c478bd9Sstevel@tonic-gate  * fsid in "the usual way" - by munging some underlying dev_t and
34037c478bd9Sstevel@tonic-gate  * the filesystem type number into the 64-bit fsid.  Note that
34047c478bd9Sstevel@tonic-gate  * this implicitly relies on dev_t persistence to make filesystem
34057c478bd9Sstevel@tonic-gate  * id's persistent.
34067c478bd9Sstevel@tonic-gate  *
34077c478bd9Sstevel@tonic-gate  * There's nothing to prevent an individual fs from constructing its
34087c478bd9Sstevel@tonic-gate  * fsid in a different way, and indeed they should.
34097c478bd9Sstevel@tonic-gate  *
34107c478bd9Sstevel@tonic-gate  * Since we want fsids to be 32-bit quantities (so that they can be
34117c478bd9Sstevel@tonic-gate  * exported identically by either 32-bit or 64-bit APIs, as well as
34127c478bd9Sstevel@tonic-gate  * the fact that fsid's are "known" to NFS), we compress the device
34137c478bd9Sstevel@tonic-gate  * number given down to 32-bits, and panic if that isn't possible.
34147c478bd9Sstevel@tonic-gate  */
34157c478bd9Sstevel@tonic-gate void
34167c478bd9Sstevel@tonic-gate vfs_make_fsid(fsid_t *fsi, dev_t dev, int val)
34177c478bd9Sstevel@tonic-gate {
34187c478bd9Sstevel@tonic-gate 	if (!cmpldev((dev32_t *)&fsi->val[0], dev))
34197c478bd9Sstevel@tonic-gate 		panic("device number too big for fsid!");
34207c478bd9Sstevel@tonic-gate 	fsi->val[1] = val;
34217c478bd9Sstevel@tonic-gate }
34227c478bd9Sstevel@tonic-gate 
34237c478bd9Sstevel@tonic-gate int
34247c478bd9Sstevel@tonic-gate vfs_lock_held(vfs_t *vfsp)
34257c478bd9Sstevel@tonic-gate {
34267c478bd9Sstevel@tonic-gate 	int held;
34277c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
34287c478bd9Sstevel@tonic-gate 
34297c478bd9Sstevel@tonic-gate 	/*
34307c478bd9Sstevel@tonic-gate 	 * vfs_lock_held will mimic sema_held behaviour
34317c478bd9Sstevel@tonic-gate 	 * if panicstr is set. And these changes should remain
34327c478bd9Sstevel@tonic-gate 	 * for the patch changes as it is.
34337c478bd9Sstevel@tonic-gate 	 */
34347c478bd9Sstevel@tonic-gate 	if (panicstr)
34357c478bd9Sstevel@tonic-gate 		return (1);
34367c478bd9Sstevel@tonic-gate 
34377c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vfsp);
34387c478bd9Sstevel@tonic-gate 	held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER);
34397c478bd9Sstevel@tonic-gate 
34407c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
34417c478bd9Sstevel@tonic-gate 	return (held);
34427c478bd9Sstevel@tonic-gate }
34437c478bd9Sstevel@tonic-gate 
34447c478bd9Sstevel@tonic-gate struct _kthread *
34457c478bd9Sstevel@tonic-gate vfs_lock_owner(vfs_t *vfsp)
34467c478bd9Sstevel@tonic-gate {
34477c478bd9Sstevel@tonic-gate 	struct _kthread *owner;
34487c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
34497c478bd9Sstevel@tonic-gate 
34507c478bd9Sstevel@tonic-gate 	/*
34517c478bd9Sstevel@tonic-gate 	 * vfs_wlock_held will mimic sema_held behaviour
34527c478bd9Sstevel@tonic-gate 	 * if panicstr is set. And these changes should remain
34537c478bd9Sstevel@tonic-gate 	 * for the patch changes as it is.
34547c478bd9Sstevel@tonic-gate 	 */
34557c478bd9Sstevel@tonic-gate 	if (panicstr)
34567c478bd9Sstevel@tonic-gate 		return (NULL);
34577c478bd9Sstevel@tonic-gate 
34587c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vfsp);
34597c478bd9Sstevel@tonic-gate 	owner = rwst_owner(&vpvfsentry->ve_lock);
34607c478bd9Sstevel@tonic-gate 
34617c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
34627c478bd9Sstevel@tonic-gate 	return (owner);
34637c478bd9Sstevel@tonic-gate }
34647c478bd9Sstevel@tonic-gate 
34657c478bd9Sstevel@tonic-gate /*
34667c478bd9Sstevel@tonic-gate  * vfs list locking.
34677c478bd9Sstevel@tonic-gate  *
34687c478bd9Sstevel@tonic-gate  * Rather than manipulate the vfslist lock directly, we abstract into lock
34697c478bd9Sstevel@tonic-gate  * and unlock routines to allow the locking implementation to be changed for
34707c478bd9Sstevel@tonic-gate  * clustering.
34717c478bd9Sstevel@tonic-gate  *
34727c478bd9Sstevel@tonic-gate  * Whenever the vfs list is modified through its hash links, the overall list
34737c478bd9Sstevel@tonic-gate  * lock must be obtained before locking the relevant hash bucket.  But to see
34747c478bd9Sstevel@tonic-gate  * whether a given vfs is on the list, it suffices to obtain the lock for the
34757c478bd9Sstevel@tonic-gate  * hash bucket without getting the overall list lock.  (See getvfs() below.)
34767c478bd9Sstevel@tonic-gate  */
34777c478bd9Sstevel@tonic-gate 
34787c478bd9Sstevel@tonic-gate void
34797c478bd9Sstevel@tonic-gate vfs_list_lock()
34807c478bd9Sstevel@tonic-gate {
34817c478bd9Sstevel@tonic-gate 	rw_enter(&vfslist, RW_WRITER);
34827c478bd9Sstevel@tonic-gate }
34837c478bd9Sstevel@tonic-gate 
34847c478bd9Sstevel@tonic-gate void
34857c478bd9Sstevel@tonic-gate vfs_list_read_lock()
34867c478bd9Sstevel@tonic-gate {
34877c478bd9Sstevel@tonic-gate 	rw_enter(&vfslist, RW_READER);
34887c478bd9Sstevel@tonic-gate }
34897c478bd9Sstevel@tonic-gate 
34907c478bd9Sstevel@tonic-gate void
34917c478bd9Sstevel@tonic-gate vfs_list_unlock()
34927c478bd9Sstevel@tonic-gate {
34937c478bd9Sstevel@tonic-gate 	rw_exit(&vfslist);
34947c478bd9Sstevel@tonic-gate }
34957c478bd9Sstevel@tonic-gate 
34967c478bd9Sstevel@tonic-gate /*
34977c478bd9Sstevel@tonic-gate  * Low level worker routines for adding entries to and removing entries from
34987c478bd9Sstevel@tonic-gate  * the vfs list.
34997c478bd9Sstevel@tonic-gate  */
35007c478bd9Sstevel@tonic-gate 
35017c478bd9Sstevel@tonic-gate static void
35027c478bd9Sstevel@tonic-gate vfs_hash_add(struct vfs *vfsp, int insert_at_head)
35037c478bd9Sstevel@tonic-gate {
35047c478bd9Sstevel@tonic-gate 	int vhno;
35057c478bd9Sstevel@tonic-gate 	struct vfs **hp;
35067c478bd9Sstevel@tonic-gate 	dev_t dev;
35077c478bd9Sstevel@tonic-gate 
35087c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&vfslist));
35097c478bd9Sstevel@tonic-gate 
35107c478bd9Sstevel@tonic-gate 	dev = expldev(vfsp->vfs_fsid.val[0]);
35117c478bd9Sstevel@tonic-gate 	vhno = VFSHASH(getmajor(dev), getminor(dev));
35127c478bd9Sstevel@tonic-gate 
35137c478bd9Sstevel@tonic-gate 	mutex_enter(&rvfs_list[vhno].rvfs_lock);
35147c478bd9Sstevel@tonic-gate 
35157c478bd9Sstevel@tonic-gate 	/*
35167c478bd9Sstevel@tonic-gate 	 * Link into the hash table, inserting it at the end, so that LOFS
35177c478bd9Sstevel@tonic-gate 	 * with the same fsid as UFS (or other) file systems will not hide the
35187c478bd9Sstevel@tonic-gate 	 * UFS.
35197c478bd9Sstevel@tonic-gate 	 */
35207c478bd9Sstevel@tonic-gate 	if (insert_at_head) {
35217c478bd9Sstevel@tonic-gate 		vfsp->vfs_hash = rvfs_list[vhno].rvfs_head;
35227c478bd9Sstevel@tonic-gate 		rvfs_list[vhno].rvfs_head = vfsp;
35237c478bd9Sstevel@tonic-gate 	} else {
35247c478bd9Sstevel@tonic-gate 		for (hp = &rvfs_list[vhno].rvfs_head; *hp != NULL;
35257c478bd9Sstevel@tonic-gate 		    hp = &(*hp)->vfs_hash)
35267c478bd9Sstevel@tonic-gate 			continue;
35277c478bd9Sstevel@tonic-gate 		/*
35287c478bd9Sstevel@tonic-gate 		 * hp now contains the address of the pointer to update
35297c478bd9Sstevel@tonic-gate 		 * to effect the insertion.
35307c478bd9Sstevel@tonic-gate 		 */
35317c478bd9Sstevel@tonic-gate 		vfsp->vfs_hash = NULL;
35327c478bd9Sstevel@tonic-gate 		*hp = vfsp;
35337c478bd9Sstevel@tonic-gate 	}
35347c478bd9Sstevel@tonic-gate 
35357c478bd9Sstevel@tonic-gate 	rvfs_list[vhno].rvfs_len++;
35367c478bd9Sstevel@tonic-gate 	mutex_exit(&rvfs_list[vhno].rvfs_lock);
35377c478bd9Sstevel@tonic-gate }
35387c478bd9Sstevel@tonic-gate 
35397c478bd9Sstevel@tonic-gate 
35407c478bd9Sstevel@tonic-gate static void
35417c478bd9Sstevel@tonic-gate vfs_hash_remove(struct vfs *vfsp)
35427c478bd9Sstevel@tonic-gate {
35437c478bd9Sstevel@tonic-gate 	int vhno;
35447c478bd9Sstevel@tonic-gate 	struct vfs *tvfsp;
35457c478bd9Sstevel@tonic-gate 	dev_t dev;
35467c478bd9Sstevel@tonic-gate 
35477c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&vfslist));
35487c478bd9Sstevel@tonic-gate 
35497c478bd9Sstevel@tonic-gate 	dev = expldev(vfsp->vfs_fsid.val[0]);
35507c478bd9Sstevel@tonic-gate 	vhno = VFSHASH(getmajor(dev), getminor(dev));
35517c478bd9Sstevel@tonic-gate 
35527c478bd9Sstevel@tonic-gate 	mutex_enter(&rvfs_list[vhno].rvfs_lock);
35537c478bd9Sstevel@tonic-gate 
35547c478bd9Sstevel@tonic-gate 	/*
35557c478bd9Sstevel@tonic-gate 	 * Remove from hash.
35567c478bd9Sstevel@tonic-gate 	 */
35577c478bd9Sstevel@tonic-gate 	if (rvfs_list[vhno].rvfs_head == vfsp) {
35587c478bd9Sstevel@tonic-gate 		rvfs_list[vhno].rvfs_head = vfsp->vfs_hash;
35597c478bd9Sstevel@tonic-gate 		rvfs_list[vhno].rvfs_len--;
35607c478bd9Sstevel@tonic-gate 		goto foundit;
35617c478bd9Sstevel@tonic-gate 	}
35627c478bd9Sstevel@tonic-gate 	for (tvfsp = rvfs_list[vhno].rvfs_head; tvfsp != NULL;
35637c478bd9Sstevel@tonic-gate 	    tvfsp = tvfsp->vfs_hash) {
35647c478bd9Sstevel@tonic-gate 		if (tvfsp->vfs_hash == vfsp) {
35657c478bd9Sstevel@tonic-gate 			tvfsp->vfs_hash = vfsp->vfs_hash;
35667c478bd9Sstevel@tonic-gate 			rvfs_list[vhno].rvfs_len--;
35677c478bd9Sstevel@tonic-gate 			goto foundit;
35687c478bd9Sstevel@tonic-gate 		}
35697c478bd9Sstevel@tonic-gate 	}
35707c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "vfs_list_remove: vfs not found in hash");
35717c478bd9Sstevel@tonic-gate 
35727c478bd9Sstevel@tonic-gate foundit:
35737c478bd9Sstevel@tonic-gate 
35747c478bd9Sstevel@tonic-gate 	mutex_exit(&rvfs_list[vhno].rvfs_lock);
35757c478bd9Sstevel@tonic-gate }
35767c478bd9Sstevel@tonic-gate 
35777c478bd9Sstevel@tonic-gate 
35787c478bd9Sstevel@tonic-gate void
35797c478bd9Sstevel@tonic-gate vfs_list_add(struct vfs *vfsp)
35807c478bd9Sstevel@tonic-gate {
35817c478bd9Sstevel@tonic-gate 	zone_t *zone;
35827c478bd9Sstevel@tonic-gate 
35837c478bd9Sstevel@tonic-gate 	/*
3584835ee219SRobert Harris 	 * Typically, the vfs_t will have been created on behalf of the file
3585835ee219SRobert Harris 	 * system in vfs_init, where it will have been provided with a
3586835ee219SRobert Harris 	 * vfs_impl_t. This, however, might be lacking if the vfs_t was created
3587835ee219SRobert Harris 	 * by an unbundled file system. We therefore check for such an example
3588835ee219SRobert Harris 	 * before stamping the vfs_t with its creation time for the benefit of
3589835ee219SRobert Harris 	 * mntfs.
3590835ee219SRobert Harris 	 */
3591835ee219SRobert Harris 	if (vfsp->vfs_implp == NULL)
3592835ee219SRobert Harris 		vfsimpl_setup(vfsp);
3593835ee219SRobert Harris 	vfs_mono_time(&vfsp->vfs_hrctime);
3594835ee219SRobert Harris 
3595835ee219SRobert Harris 	/*
35967c478bd9Sstevel@tonic-gate 	 * The zone that owns the mount is the one that performed the mount.
35977c478bd9Sstevel@tonic-gate 	 * Note that this isn't necessarily the same as the zone mounted into.
3598a19609f8Sjv227347 	 * The corresponding zone_rele_ref() will be done when the vfs_t
3599a19609f8Sjv227347 	 * is being free'd.
36007c478bd9Sstevel@tonic-gate 	 */
36017c478bd9Sstevel@tonic-gate 	vfsp->vfs_zone = curproc->p_zone;
3602a19609f8Sjv227347 	zone_init_ref(&vfsp->vfs_implp->vi_zone_ref);
3603a19609f8Sjv227347 	zone_hold_ref(vfsp->vfs_zone, &vfsp->vfs_implp->vi_zone_ref,
3604a19609f8Sjv227347 	    ZONE_REF_VFS);
36057c478bd9Sstevel@tonic-gate 
36067c478bd9Sstevel@tonic-gate 	/*
36077c478bd9Sstevel@tonic-gate 	 * Find the zone mounted into, and put this mount on its vfs list.
36087c478bd9Sstevel@tonic-gate 	 */
36097c478bd9Sstevel@tonic-gate 	zone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
36107c478bd9Sstevel@tonic-gate 	ASSERT(zone != NULL);
36117c478bd9Sstevel@tonic-gate 	/*
36127c478bd9Sstevel@tonic-gate 	 * Special casing for the root vfs.  This structure is allocated
36137c478bd9Sstevel@tonic-gate 	 * statically and hooked onto rootvfs at link time.  During the
36147c478bd9Sstevel@tonic-gate 	 * vfs_mountroot call at system startup time, the root file system's
36157c478bd9Sstevel@tonic-gate 	 * VFS_MOUNTROOT routine will call vfs_add with this root vfs struct
36167c478bd9Sstevel@tonic-gate 	 * as argument.  The code below must detect and handle this special
36177c478bd9Sstevel@tonic-gate 	 * case.  The only apparent justification for this special casing is
36187c478bd9Sstevel@tonic-gate 	 * to ensure that the root file system appears at the head of the
36197c478bd9Sstevel@tonic-gate 	 * list.
36207c478bd9Sstevel@tonic-gate 	 *
36217c478bd9Sstevel@tonic-gate 	 * XXX:	I'm assuming that it's ok to do normal list locking when
36227c478bd9Sstevel@tonic-gate 	 *	adding the entry for the root file system (this used to be
36237c478bd9Sstevel@tonic-gate 	 *	done with no locks held).
36247c478bd9Sstevel@tonic-gate 	 */
36257c478bd9Sstevel@tonic-gate 	vfs_list_lock();
36267c478bd9Sstevel@tonic-gate 	/*
36277c478bd9Sstevel@tonic-gate 	 * Link into the vfs list proper.
36287c478bd9Sstevel@tonic-gate 	 */
36297c478bd9Sstevel@tonic-gate 	if (vfsp == &root) {
36307c478bd9Sstevel@tonic-gate 		/*
36317c478bd9Sstevel@tonic-gate 		 * Assert: This vfs is already on the list as its first entry.
36327c478bd9Sstevel@tonic-gate 		 * Thus, there's nothing to do.
36337c478bd9Sstevel@tonic-gate 		 */
36347c478bd9Sstevel@tonic-gate 		ASSERT(rootvfs == vfsp);
36357c478bd9Sstevel@tonic-gate 		/*
36367c478bd9Sstevel@tonic-gate 		 * Add it to the head of the global zone's vfslist.
36377c478bd9Sstevel@tonic-gate 		 */
36387c478bd9Sstevel@tonic-gate 		ASSERT(zone == global_zone);
36397c478bd9Sstevel@tonic-gate 		ASSERT(zone->zone_vfslist == NULL);
36407c478bd9Sstevel@tonic-gate 		zone->zone_vfslist = vfsp;
36417c478bd9Sstevel@tonic-gate 	} else {
36427c478bd9Sstevel@tonic-gate 		/*
36437c478bd9Sstevel@tonic-gate 		 * Link to end of list using vfs_prev (as rootvfs is now a
36447c478bd9Sstevel@tonic-gate 		 * doubly linked circular list) so list is in mount order for
36457c478bd9Sstevel@tonic-gate 		 * mnttab use.
36467c478bd9Sstevel@tonic-gate 		 */
36477c478bd9Sstevel@tonic-gate 		rootvfs->vfs_prev->vfs_next = vfsp;
36487c478bd9Sstevel@tonic-gate 		vfsp->vfs_prev = rootvfs->vfs_prev;
36497c478bd9Sstevel@tonic-gate 		rootvfs->vfs_prev = vfsp;
36507c478bd9Sstevel@tonic-gate 		vfsp->vfs_next = rootvfs;
36517c478bd9Sstevel@tonic-gate 
36527c478bd9Sstevel@tonic-gate 		/*
36537c478bd9Sstevel@tonic-gate 		 * Do it again for the zone-private list (which may be NULL).
36547c478bd9Sstevel@tonic-gate 		 */
36557c478bd9Sstevel@tonic-gate 		if (zone->zone_vfslist == NULL) {
36567c478bd9Sstevel@tonic-gate 			ASSERT(zone != global_zone);
36577c478bd9Sstevel@tonic-gate 			zone->zone_vfslist = vfsp;
36587c478bd9Sstevel@tonic-gate 		} else {
36597c478bd9Sstevel@tonic-gate 			zone->zone_vfslist->vfs_zone_prev->vfs_zone_next = vfsp;
36607c478bd9Sstevel@tonic-gate 			vfsp->vfs_zone_prev = zone->zone_vfslist->vfs_zone_prev;
36617c478bd9Sstevel@tonic-gate 			zone->zone_vfslist->vfs_zone_prev = vfsp;
36627c478bd9Sstevel@tonic-gate 			vfsp->vfs_zone_next = zone->zone_vfslist;
36637c478bd9Sstevel@tonic-gate 		}
36647c478bd9Sstevel@tonic-gate 	}
36657c478bd9Sstevel@tonic-gate 
36667c478bd9Sstevel@tonic-gate 	/*
36677c478bd9Sstevel@tonic-gate 	 * Link into the hash table, inserting it at the end, so that LOFS
36687c478bd9Sstevel@tonic-gate 	 * with the same fsid as UFS (or other) file systems will not hide
36697c478bd9Sstevel@tonic-gate 	 * the UFS.
36707c478bd9Sstevel@tonic-gate 	 */
36717c478bd9Sstevel@tonic-gate 	vfs_hash_add(vfsp, 0);
36727c478bd9Sstevel@tonic-gate 
36737c478bd9Sstevel@tonic-gate 	/*
3674147c1346SArne Jansen 	 * Link into tree indexed by mntpoint, for vfs_mntpoint2vfsp
3675147c1346SArne Jansen 	 * mntix discerns entries with the same key
3676147c1346SArne Jansen 	 */
3677147c1346SArne Jansen 	vfsp->vfs_mntix = ++vfs_curr_mntix;
3678147c1346SArne Jansen 	avl_add(&vfs_by_dev, vfsp);
3679147c1346SArne Jansen 
3680147c1346SArne Jansen 	/*
3681147c1346SArne Jansen 	 * Link into tree indexed by dev, for vfs_devismounted
3682147c1346SArne Jansen 	 */
3683147c1346SArne Jansen 	avl_add(&vfs_by_mntpnt, vfsp);
3684147c1346SArne Jansen 
3685147c1346SArne Jansen 	/*
36867c478bd9Sstevel@tonic-gate 	 * update the mnttab modification time
36877c478bd9Sstevel@tonic-gate 	 */
36887c478bd9Sstevel@tonic-gate 	vfs_mnttab_modtimeupd();
36897c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
36907c478bd9Sstevel@tonic-gate 	zone_rele(zone);
36917c478bd9Sstevel@tonic-gate }
36927c478bd9Sstevel@tonic-gate 
36937c478bd9Sstevel@tonic-gate void
36947c478bd9Sstevel@tonic-gate vfs_list_remove(struct vfs *vfsp)
36957c478bd9Sstevel@tonic-gate {
36967c478bd9Sstevel@tonic-gate 	zone_t *zone;
36977c478bd9Sstevel@tonic-gate 
36987c478bd9Sstevel@tonic-gate 	zone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
36997c478bd9Sstevel@tonic-gate 	ASSERT(zone != NULL);
37007c478bd9Sstevel@tonic-gate 	/*
37017c478bd9Sstevel@tonic-gate 	 * Callers are responsible for preventing attempts to unmount the
37027c478bd9Sstevel@tonic-gate 	 * root.
37037c478bd9Sstevel@tonic-gate 	 */
37047c478bd9Sstevel@tonic-gate 	ASSERT(vfsp != rootvfs);
37057c478bd9Sstevel@tonic-gate 
37067c478bd9Sstevel@tonic-gate 	vfs_list_lock();
37077c478bd9Sstevel@tonic-gate 
37087c478bd9Sstevel@tonic-gate 	/*
3709147c1346SArne Jansen 	 * Remove from avl trees
3710147c1346SArne Jansen 	 */
3711147c1346SArne Jansen 	avl_remove(&vfs_by_mntpnt, vfsp);
3712147c1346SArne Jansen 	avl_remove(&vfs_by_dev, vfsp);
3713147c1346SArne Jansen 
3714147c1346SArne Jansen 	/*
37157c478bd9Sstevel@tonic-gate 	 * Remove from hash.
37167c478bd9Sstevel@tonic-gate 	 */
37177c478bd9Sstevel@tonic-gate 	vfs_hash_remove(vfsp);
37187c478bd9Sstevel@tonic-gate 
37197c478bd9Sstevel@tonic-gate 	/*
37207c478bd9Sstevel@tonic-gate 	 * Remove from vfs list.
37217c478bd9Sstevel@tonic-gate 	 */
37227c478bd9Sstevel@tonic-gate 	vfsp->vfs_prev->vfs_next = vfsp->vfs_next;
37237c478bd9Sstevel@tonic-gate 	vfsp->vfs_next->vfs_prev = vfsp->vfs_prev;
37247c478bd9Sstevel@tonic-gate 	vfsp->vfs_next = vfsp->vfs_prev = NULL;
37257c478bd9Sstevel@tonic-gate 
37267c478bd9Sstevel@tonic-gate 	/*
37277c478bd9Sstevel@tonic-gate 	 * Remove from zone-specific vfs list.
37287c478bd9Sstevel@tonic-gate 	 */
37297c478bd9Sstevel@tonic-gate 	if (zone->zone_vfslist == vfsp)
37307c478bd9Sstevel@tonic-gate 		zone->zone_vfslist = vfsp->vfs_zone_next;
37317c478bd9Sstevel@tonic-gate 
37327c478bd9Sstevel@tonic-gate 	if (vfsp->vfs_zone_next == vfsp) {
37337c478bd9Sstevel@tonic-gate 		ASSERT(vfsp->vfs_zone_prev == vfsp);
37347c478bd9Sstevel@tonic-gate 		ASSERT(zone->zone_vfslist == vfsp);
37357c478bd9Sstevel@tonic-gate 		zone->zone_vfslist = NULL;
37367c478bd9Sstevel@tonic-gate 	}
37377c478bd9Sstevel@tonic-gate 
37387c478bd9Sstevel@tonic-gate 	vfsp->vfs_zone_prev->vfs_zone_next = vfsp->vfs_zone_next;
37397c478bd9Sstevel@tonic-gate 	vfsp->vfs_zone_next->vfs_zone_prev = vfsp->vfs_zone_prev;
37407c478bd9Sstevel@tonic-gate 	vfsp->vfs_zone_next = vfsp->vfs_zone_prev = NULL;
37417c478bd9Sstevel@tonic-gate 
37427c478bd9Sstevel@tonic-gate 	/*
37437c478bd9Sstevel@tonic-gate 	 * update the mnttab modification time
37447c478bd9Sstevel@tonic-gate 	 */
37457c478bd9Sstevel@tonic-gate 	vfs_mnttab_modtimeupd();
37467c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
37477c478bd9Sstevel@tonic-gate 	zone_rele(zone);
37487c478bd9Sstevel@tonic-gate }
37497c478bd9Sstevel@tonic-gate 
37507c478bd9Sstevel@tonic-gate struct vfs *
37517c478bd9Sstevel@tonic-gate getvfs(fsid_t *fsid)
37527c478bd9Sstevel@tonic-gate {
37537c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
37547c478bd9Sstevel@tonic-gate 	int val0 = fsid->val[0];
37557c478bd9Sstevel@tonic-gate 	int val1 = fsid->val[1];
37567c478bd9Sstevel@tonic-gate 	dev_t dev = expldev(val0);
37577c478bd9Sstevel@tonic-gate 	int vhno = VFSHASH(getmajor(dev), getminor(dev));
37587c478bd9Sstevel@tonic-gate 	kmutex_t *hmp = &rvfs_list[vhno].rvfs_lock;
37597c478bd9Sstevel@tonic-gate 
37607c478bd9Sstevel@tonic-gate 	mutex_enter(hmp);
37617c478bd9Sstevel@tonic-gate 	for (vfsp = rvfs_list[vhno].rvfs_head; vfsp; vfsp = vfsp->vfs_hash) {
37627c478bd9Sstevel@tonic-gate 		if (vfsp->vfs_fsid.val[0] == val0 &&
37637c478bd9Sstevel@tonic-gate 		    vfsp->vfs_fsid.val[1] == val1) {
37647c478bd9Sstevel@tonic-gate 			VFS_HOLD(vfsp);
37657c478bd9Sstevel@tonic-gate 			mutex_exit(hmp);
37667c478bd9Sstevel@tonic-gate 			return (vfsp);
37677c478bd9Sstevel@tonic-gate 		}
37687c478bd9Sstevel@tonic-gate 	}
37697c478bd9Sstevel@tonic-gate 	mutex_exit(hmp);
37707c478bd9Sstevel@tonic-gate 	return (NULL);
37717c478bd9Sstevel@tonic-gate }
37727c478bd9Sstevel@tonic-gate 
37737c478bd9Sstevel@tonic-gate /*
37747c478bd9Sstevel@tonic-gate  * Search the vfs mount in progress list for a specified device/vfs entry.
37757c478bd9Sstevel@tonic-gate  * Returns 0 if the first entry in the list that the device matches has the
37767c478bd9Sstevel@tonic-gate  * given vfs pointer as well.  If the device matches but a different vfs
37777c478bd9Sstevel@tonic-gate  * pointer is encountered in the list before the given vfs pointer then
37787c478bd9Sstevel@tonic-gate  * a 1 is returned.
37797c478bd9Sstevel@tonic-gate  */
37807c478bd9Sstevel@tonic-gate 
37817c478bd9Sstevel@tonic-gate int
37827c478bd9Sstevel@tonic-gate vfs_devmounting(dev_t dev, struct vfs *vfsp)
37837c478bd9Sstevel@tonic-gate {
37847c478bd9Sstevel@tonic-gate 	int retval = 0;
37857c478bd9Sstevel@tonic-gate 	struct ipmnt *mipp;
37867c478bd9Sstevel@tonic-gate 
37877c478bd9Sstevel@tonic-gate 	mutex_enter(&vfs_miplist_mutex);
37887c478bd9Sstevel@tonic-gate 	for (mipp = vfs_miplist; mipp != NULL; mipp = mipp->mip_next) {
37897c478bd9Sstevel@tonic-gate 		if (mipp->mip_dev == dev) {
37907c478bd9Sstevel@tonic-gate 			if (mipp->mip_vfsp != vfsp)
37917c478bd9Sstevel@tonic-gate 				retval = 1;
37927c478bd9Sstevel@tonic-gate 			break;
37937c478bd9Sstevel@tonic-gate 		}
37947c478bd9Sstevel@tonic-gate 	}
37957c478bd9Sstevel@tonic-gate 	mutex_exit(&vfs_miplist_mutex);
37967c478bd9Sstevel@tonic-gate 	return (retval);
37977c478bd9Sstevel@tonic-gate }
37987c478bd9Sstevel@tonic-gate 
37997c478bd9Sstevel@tonic-gate /*
38007c478bd9Sstevel@tonic-gate  * Search the vfs list for a specified device.  Returns 1, if entry is found
38017c478bd9Sstevel@tonic-gate  * or 0 if no suitable entry is found.
38027c478bd9Sstevel@tonic-gate  */
38037c478bd9Sstevel@tonic-gate 
38047c478bd9Sstevel@tonic-gate int
38057c478bd9Sstevel@tonic-gate vfs_devismounted(dev_t dev)
38067c478bd9Sstevel@tonic-gate {
38077c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
3808147c1346SArne Jansen 	int found = 0;
3809147c1346SArne Jansen 	struct vfs search;
3810147c1346SArne Jansen 	avl_index_t index;
3811147c1346SArne Jansen 
3812147c1346SArne Jansen 	search.vfs_dev = dev;
3813147c1346SArne Jansen 	search.vfs_mntix = 0;
38147c478bd9Sstevel@tonic-gate 
38157c478bd9Sstevel@tonic-gate 	vfs_list_read_lock();
3816147c1346SArne Jansen 
3817147c1346SArne Jansen 	/*
3818147c1346SArne Jansen 	 * there might be several entries with the same dev in the tree,
3819147c1346SArne Jansen 	 * only discerned by mntix. To find the first, we start with a mntix
3820147c1346SArne Jansen 	 * of 0. The search will fail. The following avl_nearest will give
3821147c1346SArne Jansen 	 * us the actual first entry.
3822147c1346SArne Jansen 	 */
3823147c1346SArne Jansen 	VERIFY(avl_find(&vfs_by_dev, &search, &index) == NULL);
3824147c1346SArne Jansen 	vfsp = avl_nearest(&vfs_by_dev, index, AVL_AFTER);
3825147c1346SArne Jansen 
3826147c1346SArne Jansen 	if (vfsp != NULL && vfsp->vfs_dev == dev)
38277c478bd9Sstevel@tonic-gate 		found = 1;
38287c478bd9Sstevel@tonic-gate 
38297c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
38307c478bd9Sstevel@tonic-gate 	return (found);
38317c478bd9Sstevel@tonic-gate }
38327c478bd9Sstevel@tonic-gate 
38337c478bd9Sstevel@tonic-gate /*
38347c478bd9Sstevel@tonic-gate  * Search the vfs list for a specified device.  Returns a pointer to it
38357c478bd9Sstevel@tonic-gate  * or NULL if no suitable entry is found. The caller of this routine
38367c478bd9Sstevel@tonic-gate  * is responsible for releasing the returned vfs pointer.
38377c478bd9Sstevel@tonic-gate  */
38387c478bd9Sstevel@tonic-gate struct vfs *
38397c478bd9Sstevel@tonic-gate vfs_dev2vfsp(dev_t dev)
38407c478bd9Sstevel@tonic-gate {
38417c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
38427c478bd9Sstevel@tonic-gate 	int found;
3843147c1346SArne Jansen 	struct vfs search;
3844147c1346SArne Jansen 	avl_index_t index;
3845147c1346SArne Jansen 
3846147c1346SArne Jansen 	search.vfs_dev = dev;
3847147c1346SArne Jansen 	search.vfs_mntix = 0;
38487c478bd9Sstevel@tonic-gate 
38497c478bd9Sstevel@tonic-gate 	vfs_list_read_lock();
3850147c1346SArne Jansen 
3851147c1346SArne Jansen 	/*
3852147c1346SArne Jansen 	 * there might be several entries with the same dev in the tree,
3853147c1346SArne Jansen 	 * only discerned by mntix. To find the first, we start with a mntix
3854147c1346SArne Jansen 	 * of 0. The search will fail. The following avl_nearest will give
3855147c1346SArne Jansen 	 * us the actual first entry.
3856147c1346SArne Jansen 	 */
3857147c1346SArne Jansen 	VERIFY(avl_find(&vfs_by_dev, &search, &index) == NULL);
3858147c1346SArne Jansen 	vfsp = avl_nearest(&vfs_by_dev, index, AVL_AFTER);
3859147c1346SArne Jansen 
38607c478bd9Sstevel@tonic-gate 	found = 0;
3861147c1346SArne Jansen 	while (vfsp != NULL && vfsp->vfs_dev == dev) {
38627c478bd9Sstevel@tonic-gate 		/*
38637c478bd9Sstevel@tonic-gate 		 * The following could be made more efficient by making
38647c478bd9Sstevel@tonic-gate 		 * the entire loop use vfs_zone_next if the call is from
38657c478bd9Sstevel@tonic-gate 		 * a zone.  The only callers, however, ustat(2) and
38667c478bd9Sstevel@tonic-gate 		 * umount2(2), don't seem to justify the added
38677c478bd9Sstevel@tonic-gate 		 * complexity at present.
38687c478bd9Sstevel@tonic-gate 		 */
3869147c1346SArne Jansen 		if (ZONE_PATH_VISIBLE(refstr_value(vfsp->vfs_mntpt),
38707c478bd9Sstevel@tonic-gate 		    curproc->p_zone)) {
38717c478bd9Sstevel@tonic-gate 			VFS_HOLD(vfsp);
38727c478bd9Sstevel@tonic-gate 			found = 1;
38737c478bd9Sstevel@tonic-gate 			break;
38747c478bd9Sstevel@tonic-gate 		}
3875147c1346SArne Jansen 		vfsp = AVL_NEXT(&vfs_by_dev, vfsp);
3876147c1346SArne Jansen 	}
38777c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
38787c478bd9Sstevel@tonic-gate 	return (found ? vfsp : NULL);
38797c478bd9Sstevel@tonic-gate }
38807c478bd9Sstevel@tonic-gate 
38817c478bd9Sstevel@tonic-gate /*
38827c478bd9Sstevel@tonic-gate  * Search the vfs list for a specified mntpoint.  Returns a pointer to it
38837c478bd9Sstevel@tonic-gate  * or NULL if no suitable entry is found. The caller of this routine
38847c478bd9Sstevel@tonic-gate  * is responsible for releasing the returned vfs pointer.
38857c478bd9Sstevel@tonic-gate  *
38867c478bd9Sstevel@tonic-gate  * Note that if multiple mntpoints match, the last one matching is
38877c478bd9Sstevel@tonic-gate  * returned in an attempt to return the "top" mount when overlay
38887c478bd9Sstevel@tonic-gate  * mounts are covering the same mount point.  This is accomplished by starting
38897c478bd9Sstevel@tonic-gate  * at the end of the list and working our way backwards, stopping at the first
38907c478bd9Sstevel@tonic-gate  * matching mount.
38917c478bd9Sstevel@tonic-gate  */
38927c478bd9Sstevel@tonic-gate struct vfs *
38937c478bd9Sstevel@tonic-gate vfs_mntpoint2vfsp(const char *mp)
38947c478bd9Sstevel@tonic-gate {
38957c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
38967c478bd9Sstevel@tonic-gate 	struct vfs *retvfsp = NULL;
38977c478bd9Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
38987c478bd9Sstevel@tonic-gate 	struct vfs *list;
38997c478bd9Sstevel@tonic-gate 
39007c478bd9Sstevel@tonic-gate 	vfs_list_read_lock();
39017c478bd9Sstevel@tonic-gate 	if (getzoneid() == GLOBAL_ZONEID) {
39027c478bd9Sstevel@tonic-gate 		/*
39037c478bd9Sstevel@tonic-gate 		 * The global zone may see filesystems in any zone.
39047c478bd9Sstevel@tonic-gate 		 */
3905147c1346SArne Jansen 		struct vfs search;
3906147c1346SArne Jansen 		search.vfs_mntpt = refstr_alloc(mp);
3907147c1346SArne Jansen 		search.vfs_mntix = UINT64_MAX;
3908147c1346SArne Jansen 		avl_index_t index;
3909147c1346SArne Jansen 
3910147c1346SArne Jansen 		/*
3911147c1346SArne Jansen 		 * there might be several entries with the same mntpnt in the
3912147c1346SArne Jansen 		 * tree, only discerned by mntix. To find the last, we start
3913147c1346SArne Jansen 		 * with a mntix of UINT64_MAX. The search will fail. The
3914147c1346SArne Jansen 		 * following avl_nearest will give  us the actual last entry
3915147c1346SArne Jansen 		 * matching the mntpnt.
3916147c1346SArne Jansen 		 */
3917147c1346SArne Jansen 		VERIFY(avl_find(&vfs_by_mntpnt, &search, &index) == 0);
3918147c1346SArne Jansen 		vfsp = avl_nearest(&vfs_by_mntpnt, index, AVL_BEFORE);
3919147c1346SArne Jansen 
3920147c1346SArne Jansen 		refstr_rele(search.vfs_mntpt);
3921147c1346SArne Jansen 
3922147c1346SArne Jansen 		if (vfsp != NULL &&
3923147c1346SArne Jansen 		    strcmp(refstr_value(vfsp->vfs_mntpt), mp) == 0)
39247c478bd9Sstevel@tonic-gate 			retvfsp = vfsp;
39257c478bd9Sstevel@tonic-gate 	} else if ((list = zone->zone_vfslist) != NULL) {
39267c478bd9Sstevel@tonic-gate 		const char *mntpt;
39277c478bd9Sstevel@tonic-gate 
39287c478bd9Sstevel@tonic-gate 		vfsp = list->vfs_zone_prev;
39297c478bd9Sstevel@tonic-gate 		do {
39307c478bd9Sstevel@tonic-gate 			mntpt = refstr_value(vfsp->vfs_mntpt);
39317c478bd9Sstevel@tonic-gate 			mntpt = ZONE_PATH_TRANSLATE(mntpt, zone);
39327c478bd9Sstevel@tonic-gate 			if (strcmp(mntpt, mp) == 0) {
39337c478bd9Sstevel@tonic-gate 				retvfsp = vfsp;
39347c478bd9Sstevel@tonic-gate 				break;
39357c478bd9Sstevel@tonic-gate 			}
39367c478bd9Sstevel@tonic-gate 			vfsp = vfsp->vfs_zone_prev;
39377c478bd9Sstevel@tonic-gate 		} while (vfsp != list->vfs_zone_prev);
39387c478bd9Sstevel@tonic-gate 	}
39397c478bd9Sstevel@tonic-gate 	if (retvfsp)
39407c478bd9Sstevel@tonic-gate 		VFS_HOLD(retvfsp);
39417c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
39427c478bd9Sstevel@tonic-gate 	return (retvfsp);
39437c478bd9Sstevel@tonic-gate }
39447c478bd9Sstevel@tonic-gate 
39457c478bd9Sstevel@tonic-gate /*
39467c478bd9Sstevel@tonic-gate  * Search the vfs list for a specified vfsops.
39477c478bd9Sstevel@tonic-gate  * if vfs entry is found then return 1, else 0.
39487c478bd9Sstevel@tonic-gate  */
39497c478bd9Sstevel@tonic-gate int
39507c478bd9Sstevel@tonic-gate vfs_opsinuse(vfsops_t *ops)
39517c478bd9Sstevel@tonic-gate {
39527c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
39537c478bd9Sstevel@tonic-gate 	int found;
39547c478bd9Sstevel@tonic-gate 
39557c478bd9Sstevel@tonic-gate 	vfs_list_read_lock();
39567c478bd9Sstevel@tonic-gate 	vfsp = rootvfs;
39577c478bd9Sstevel@tonic-gate 	found = 0;
39587c478bd9Sstevel@tonic-gate 	do {
39597c478bd9Sstevel@tonic-gate 		if (vfs_getops(vfsp) == ops) {
39607c478bd9Sstevel@tonic-gate 			found = 1;
39617c478bd9Sstevel@tonic-gate 			break;
39627c478bd9Sstevel@tonic-gate 		}
39637c478bd9Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
39647c478bd9Sstevel@tonic-gate 	} while (vfsp != rootvfs);
39657c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
39667c478bd9Sstevel@tonic-gate 	return (found);
39677c478bd9Sstevel@tonic-gate }
39687c478bd9Sstevel@tonic-gate 
39697c478bd9Sstevel@tonic-gate /*
39707c478bd9Sstevel@tonic-gate  * Allocate an entry in vfssw for a file system type
39717c478bd9Sstevel@tonic-gate  */
39727c478bd9Sstevel@tonic-gate struct vfssw *
397343d5cd3dSjohnlev allocate_vfssw(const char *type)
39747c478bd9Sstevel@tonic-gate {
39757c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
39767c478bd9Sstevel@tonic-gate 
39777c478bd9Sstevel@tonic-gate 	if (type[0] == '\0' || strlen(type) + 1 > _ST_FSTYPSZ) {
39787c478bd9Sstevel@tonic-gate 		/*
39797c478bd9Sstevel@tonic-gate 		 * The vfssw table uses the empty string to identify an
39807c478bd9Sstevel@tonic-gate 		 * available entry; we cannot add any type which has
39817c478bd9Sstevel@tonic-gate 		 * a leading NUL. The string length is limited to
39827c478bd9Sstevel@tonic-gate 		 * the size of the st_fstype array in struct stat.
39837c478bd9Sstevel@tonic-gate 		 */
39847c478bd9Sstevel@tonic-gate 		return (NULL);
39857c478bd9Sstevel@tonic-gate 	}
39867c478bd9Sstevel@tonic-gate 
39877c478bd9Sstevel@tonic-gate 	ASSERT(VFSSW_WRITE_LOCKED());
39887c478bd9Sstevel@tonic-gate 	for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++)
39897c478bd9Sstevel@tonic-gate 		if (!ALLOCATED_VFSSW(vswp)) {
39907c478bd9Sstevel@tonic-gate 			vswp->vsw_name = kmem_alloc(strlen(type) + 1, KM_SLEEP);
39917c478bd9Sstevel@tonic-gate 			(void) strcpy(vswp->vsw_name, type);
39927c478bd9Sstevel@tonic-gate 			ASSERT(vswp->vsw_count == 0);
39937c478bd9Sstevel@tonic-gate 			vswp->vsw_count = 1;
39947c478bd9Sstevel@tonic-gate 			mutex_init(&vswp->vsw_lock, NULL, MUTEX_DEFAULT, NULL);
39957c478bd9Sstevel@tonic-gate 			return (vswp);
39967c478bd9Sstevel@tonic-gate 		}
39977c478bd9Sstevel@tonic-gate 	return (NULL);
39987c478bd9Sstevel@tonic-gate }
39997c478bd9Sstevel@tonic-gate 
40007c478bd9Sstevel@tonic-gate /*
40017c478bd9Sstevel@tonic-gate  * Impose additional layer of translation between vfstype names
40027c478bd9Sstevel@tonic-gate  * and module names in the filesystem.
40037c478bd9Sstevel@tonic-gate  */
400443d5cd3dSjohnlev static const char *
400543d5cd3dSjohnlev vfs_to_modname(const char *vfstype)
40067c478bd9Sstevel@tonic-gate {
40077c478bd9Sstevel@tonic-gate 	if (strcmp(vfstype, "proc") == 0) {
40087c478bd9Sstevel@tonic-gate 		vfstype = "procfs";
40097c478bd9Sstevel@tonic-gate 	} else if (strcmp(vfstype, "fd") == 0) {
40107c478bd9Sstevel@tonic-gate 		vfstype = "fdfs";
40117c478bd9Sstevel@tonic-gate 	} else if (strncmp(vfstype, "nfs", 3) == 0) {
40127c478bd9Sstevel@tonic-gate 		vfstype = "nfs";
40137c478bd9Sstevel@tonic-gate 	}
40147c478bd9Sstevel@tonic-gate 
40157c478bd9Sstevel@tonic-gate 	return (vfstype);
40167c478bd9Sstevel@tonic-gate }
40177c478bd9Sstevel@tonic-gate 
40187c478bd9Sstevel@tonic-gate /*
40197c478bd9Sstevel@tonic-gate  * Find a vfssw entry given a file system type name.
40207c478bd9Sstevel@tonic-gate  * Try to autoload the filesystem if it's not found.
40217c478bd9Sstevel@tonic-gate  * If it's installed, return the vfssw locked to prevent unloading.
40227c478bd9Sstevel@tonic-gate  */
40237c478bd9Sstevel@tonic-gate struct vfssw *
402443d5cd3dSjohnlev vfs_getvfssw(const char *type)
40257c478bd9Sstevel@tonic-gate {
40267c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
402743d5cd3dSjohnlev 	const char *modname;
40287c478bd9Sstevel@tonic-gate 
40297c478bd9Sstevel@tonic-gate 	RLOCK_VFSSW();
40307c478bd9Sstevel@tonic-gate 	vswp = vfs_getvfsswbyname(type);
40317c478bd9Sstevel@tonic-gate 	modname = vfs_to_modname(type);
40327c478bd9Sstevel@tonic-gate 
40337c478bd9Sstevel@tonic-gate 	if (rootdir == NULL) {
40347c478bd9Sstevel@tonic-gate 		/*
40357c478bd9Sstevel@tonic-gate 		 * If we haven't yet loaded the root file system, then our
40367c478bd9Sstevel@tonic-gate 		 * _init won't be called until later. Allocate vfssw entry,
40377c478bd9Sstevel@tonic-gate 		 * because mod_installfs won't be called.
40387c478bd9Sstevel@tonic-gate 		 */
40397c478bd9Sstevel@tonic-gate 		if (vswp == NULL) {
40407c478bd9Sstevel@tonic-gate 			RUNLOCK_VFSSW();
40417c478bd9Sstevel@tonic-gate 			WLOCK_VFSSW();
40427c478bd9Sstevel@tonic-gate 			if ((vswp = vfs_getvfsswbyname(type)) == NULL) {
40437c478bd9Sstevel@tonic-gate 				if ((vswp = allocate_vfssw(type)) == NULL) {
40447c478bd9Sstevel@tonic-gate 					WUNLOCK_VFSSW();
40457c478bd9Sstevel@tonic-gate 					return (NULL);
40467c478bd9Sstevel@tonic-gate 				}
40477c478bd9Sstevel@tonic-gate 			}
40487c478bd9Sstevel@tonic-gate 			WUNLOCK_VFSSW();
40497c478bd9Sstevel@tonic-gate 			RLOCK_VFSSW();
40507c478bd9Sstevel@tonic-gate 		}
40517c478bd9Sstevel@tonic-gate 		if (!VFS_INSTALLED(vswp)) {
40527c478bd9Sstevel@tonic-gate 			RUNLOCK_VFSSW();
40537c478bd9Sstevel@tonic-gate 			(void) modloadonly("fs", modname);
40547c478bd9Sstevel@tonic-gate 		} else
40557c478bd9Sstevel@tonic-gate 			RUNLOCK_VFSSW();
40567c478bd9Sstevel@tonic-gate 		return (vswp);
40577c478bd9Sstevel@tonic-gate 	}
40587c478bd9Sstevel@tonic-gate 
40597c478bd9Sstevel@tonic-gate 	/*
40607c478bd9Sstevel@tonic-gate 	 * Try to load the filesystem.  Before calling modload(), we drop
40617c478bd9Sstevel@tonic-gate 	 * our lock on the VFS switch table, and pick it up after the
40627c478bd9Sstevel@tonic-gate 	 * module is loaded.  However, there is a potential race:  the
40637c478bd9Sstevel@tonic-gate 	 * module could be unloaded after the call to modload() completes
40647c478bd9Sstevel@tonic-gate 	 * but before we pick up the lock and drive on.  Therefore,
40657c478bd9Sstevel@tonic-gate 	 * we keep reloading the module until we've loaded the module
40667c478bd9Sstevel@tonic-gate 	 * _and_ we have the lock on the VFS switch table.
40677c478bd9Sstevel@tonic-gate 	 */
40687c478bd9Sstevel@tonic-gate 	while (vswp == NULL || !VFS_INSTALLED(vswp)) {
40697c478bd9Sstevel@tonic-gate 		RUNLOCK_VFSSW();
40707c478bd9Sstevel@tonic-gate 		if (modload("fs", modname) == -1)
40717c478bd9Sstevel@tonic-gate 			return (NULL);
40727c478bd9Sstevel@tonic-gate 		RLOCK_VFSSW();
40737c478bd9Sstevel@tonic-gate 		if (vswp == NULL)
40747c478bd9Sstevel@tonic-gate 			if ((vswp = vfs_getvfsswbyname(type)) == NULL)
40757c478bd9Sstevel@tonic-gate 				break;
40767c478bd9Sstevel@tonic-gate 	}
40777c478bd9Sstevel@tonic-gate 	RUNLOCK_VFSSW();
40787c478bd9Sstevel@tonic-gate 
40797c478bd9Sstevel@tonic-gate 	return (vswp);
40807c478bd9Sstevel@tonic-gate }
40817c478bd9Sstevel@tonic-gate 
40827c478bd9Sstevel@tonic-gate /*
40837c478bd9Sstevel@tonic-gate  * Find a vfssw entry given a file system type name.
40847c478bd9Sstevel@tonic-gate  */
40857c478bd9Sstevel@tonic-gate struct vfssw *
408643d5cd3dSjohnlev vfs_getvfsswbyname(const char *type)
40877c478bd9Sstevel@tonic-gate {
40887c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
40897c478bd9Sstevel@tonic-gate 
40907c478bd9Sstevel@tonic-gate 	ASSERT(VFSSW_LOCKED());
40917c478bd9Sstevel@tonic-gate 	if (type == NULL || *type == '\0')
40927c478bd9Sstevel@tonic-gate 		return (NULL);
40937c478bd9Sstevel@tonic-gate 
40947c478bd9Sstevel@tonic-gate 	for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
40957c478bd9Sstevel@tonic-gate 		if (strcmp(type, vswp->vsw_name) == 0) {
40967c478bd9Sstevel@tonic-gate 			vfs_refvfssw(vswp);
40977c478bd9Sstevel@tonic-gate 			return (vswp);
40987c478bd9Sstevel@tonic-gate 		}
40997c478bd9Sstevel@tonic-gate 	}
41007c478bd9Sstevel@tonic-gate 
41017c478bd9Sstevel@tonic-gate 	return (NULL);
41027c478bd9Sstevel@tonic-gate }
41037c478bd9Sstevel@tonic-gate 
41047c478bd9Sstevel@tonic-gate /*
41057c478bd9Sstevel@tonic-gate  * Find a vfssw entry given a set of vfsops.
41067c478bd9Sstevel@tonic-gate  */
41077c478bd9Sstevel@tonic-gate struct vfssw *
41087c478bd9Sstevel@tonic-gate vfs_getvfsswbyvfsops(vfsops_t *vfsops)
41097c478bd9Sstevel@tonic-gate {
41107c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
41117c478bd9Sstevel@tonic-gate 
41127c478bd9Sstevel@tonic-gate 	RLOCK_VFSSW();
41137c478bd9Sstevel@tonic-gate 	for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
41147c478bd9Sstevel@tonic-gate 		if (ALLOCATED_VFSSW(vswp) && &vswp->vsw_vfsops == vfsops) {
41157c478bd9Sstevel@tonic-gate 			vfs_refvfssw(vswp);
41167c478bd9Sstevel@tonic-gate 			RUNLOCK_VFSSW();
41177c478bd9Sstevel@tonic-gate 			return (vswp);
41187c478bd9Sstevel@tonic-gate 		}
41197c478bd9Sstevel@tonic-gate 	}
41207c478bd9Sstevel@tonic-gate 	RUNLOCK_VFSSW();
41217c478bd9Sstevel@tonic-gate 
41227c478bd9Sstevel@tonic-gate 	return (NULL);
41237c478bd9Sstevel@tonic-gate }
41247c478bd9Sstevel@tonic-gate 
41257c478bd9Sstevel@tonic-gate /*
41267c478bd9Sstevel@tonic-gate  * Reference a vfssw entry.
41277c478bd9Sstevel@tonic-gate  */
41287c478bd9Sstevel@tonic-gate void
41297c478bd9Sstevel@tonic-gate vfs_refvfssw(struct vfssw *vswp)
41307c478bd9Sstevel@tonic-gate {
41317c478bd9Sstevel@tonic-gate 
41327c478bd9Sstevel@tonic-gate 	mutex_enter(&vswp->vsw_lock);
41337c478bd9Sstevel@tonic-gate 	vswp->vsw_count++;
41347c478bd9Sstevel@tonic-gate 	mutex_exit(&vswp->vsw_lock);
41357c478bd9Sstevel@tonic-gate }
41367c478bd9Sstevel@tonic-gate 
41377c478bd9Sstevel@tonic-gate /*
41387c478bd9Sstevel@tonic-gate  * Unreference a vfssw entry.
41397c478bd9Sstevel@tonic-gate  */
41407c478bd9Sstevel@tonic-gate void
41417c478bd9Sstevel@tonic-gate vfs_unrefvfssw(struct vfssw *vswp)
41427c478bd9Sstevel@tonic-gate {
41437c478bd9Sstevel@tonic-gate 
41447c478bd9Sstevel@tonic-gate 	mutex_enter(&vswp->vsw_lock);
41457c478bd9Sstevel@tonic-gate 	vswp->vsw_count--;
41467c478bd9Sstevel@tonic-gate 	mutex_exit(&vswp->vsw_lock);
41477c478bd9Sstevel@tonic-gate }
41487c478bd9Sstevel@tonic-gate 
41497c478bd9Sstevel@tonic-gate int sync_timeout = 30;		/* timeout for syncing a page during panic */
41507c478bd9Sstevel@tonic-gate int sync_timeleft;		/* portion of sync_timeout remaining */
41517c478bd9Sstevel@tonic-gate 
41527c478bd9Sstevel@tonic-gate static int sync_retries = 20;	/* number of retries when not making progress */
41537c478bd9Sstevel@tonic-gate static int sync_triesleft;	/* portion of sync_retries remaining */
41547c478bd9Sstevel@tonic-gate 
41557c478bd9Sstevel@tonic-gate static pgcnt_t old_pgcnt, new_pgcnt;
41567c478bd9Sstevel@tonic-gate static int new_bufcnt, old_bufcnt;
41577c478bd9Sstevel@tonic-gate 
41587c478bd9Sstevel@tonic-gate /*
41597c478bd9Sstevel@tonic-gate  * Sync all of the mounted filesystems, and then wait for the actual i/o to
41607c478bd9Sstevel@tonic-gate  * complete.  We wait by counting the number of dirty pages and buffers,
41617c478bd9Sstevel@tonic-gate  * pushing them out using bio_busy() and page_busy(), and then counting again.
41627c478bd9Sstevel@tonic-gate  * This routine is used during both the uadmin A_SHUTDOWN code as well as
41637c478bd9Sstevel@tonic-gate  * the SYNC phase of the panic code (see comments in panic.c).  It should only
41647c478bd9Sstevel@tonic-gate  * be used after some higher-level mechanism has quiesced the system so that
41657c478bd9Sstevel@tonic-gate  * new writes are not being initiated while we are waiting for completion.
41667c478bd9Sstevel@tonic-gate  *
41677c478bd9Sstevel@tonic-gate  * To ensure finite running time, our algorithm uses two timeout mechanisms:
41687c478bd9Sstevel@tonic-gate  * sync_timeleft (a timer implemented by the omnipresent deadman() cyclic), and
41697c478bd9Sstevel@tonic-gate  * sync_triesleft (a progress counter used by the vfs_syncall() loop below).
41707c478bd9Sstevel@tonic-gate  * Together these ensure that syncing completes if our i/o paths are stuck.
41717c478bd9Sstevel@tonic-gate  * The counters are declared above so they can be found easily in the debugger.
41727c478bd9Sstevel@tonic-gate  *
41737c478bd9Sstevel@tonic-gate  * The sync_timeleft counter is reset by bio_busy() and page_busy() using the
41747c478bd9Sstevel@tonic-gate  * vfs_syncprogress() subroutine whenever we make progress through the lists of
41757c478bd9Sstevel@tonic-gate  * pages and buffers.  It is decremented and expired by the deadman() cyclic.
41767c478bd9Sstevel@tonic-gate  * When vfs_syncall() decides it is done, we disable the deadman() counter by
41777c478bd9Sstevel@tonic-gate  * setting sync_timeleft to zero.  This timer guards against vfs_syncall()
41787c478bd9Sstevel@tonic-gate  * deadlocking or hanging inside of a broken filesystem or driver routine.
41797c478bd9Sstevel@tonic-gate  *
41807c478bd9Sstevel@tonic-gate  * The sync_triesleft counter is updated by vfs_syncall() itself.  If we make
41817c478bd9Sstevel@tonic-gate  * sync_retries consecutive calls to bio_busy() and page_busy() without
41827c478bd9Sstevel@tonic-gate  * decreasing either the number of dirty buffers or dirty pages below the
41837c478bd9Sstevel@tonic-gate  * lowest count we have seen so far, we give up and return from vfs_syncall().
41847c478bd9Sstevel@tonic-gate  *
41857c478bd9Sstevel@tonic-gate  * Each loop iteration ends with a call to delay() one second to allow time for
41867c478bd9Sstevel@tonic-gate  * i/o completion and to permit the user time to read our progress messages.
41877c478bd9Sstevel@tonic-gate  */
41887c478bd9Sstevel@tonic-gate void
41897c478bd9Sstevel@tonic-gate vfs_syncall(void)
41907c478bd9Sstevel@tonic-gate {
41917c478bd9Sstevel@tonic-gate 	if (rootdir == NULL && !modrootloaded)
41927c478bd9Sstevel@tonic-gate 		return; /* panic during boot - no filesystems yet */
41937c478bd9Sstevel@tonic-gate 
41947c478bd9Sstevel@tonic-gate 	printf("syncing file systems...");
41957c478bd9Sstevel@tonic-gate 	vfs_syncprogress();
41967c478bd9Sstevel@tonic-gate 	sync();
41977c478bd9Sstevel@tonic-gate 
41987c478bd9Sstevel@tonic-gate 	vfs_syncprogress();
41997c478bd9Sstevel@tonic-gate 	sync_triesleft = sync_retries;
42007c478bd9Sstevel@tonic-gate 
42017c478bd9Sstevel@tonic-gate 	old_bufcnt = new_bufcnt = INT_MAX;
42027c478bd9Sstevel@tonic-gate 	old_pgcnt = new_pgcnt = ULONG_MAX;
42037c478bd9Sstevel@tonic-gate 
42047c478bd9Sstevel@tonic-gate 	while (sync_triesleft > 0) {
42057c478bd9Sstevel@tonic-gate 		old_bufcnt = MIN(old_bufcnt, new_bufcnt);
42067c478bd9Sstevel@tonic-gate 		old_pgcnt = MIN(old_pgcnt, new_pgcnt);
42077c478bd9Sstevel@tonic-gate 
42087c478bd9Sstevel@tonic-gate 		new_bufcnt = bio_busy(B_TRUE);
42097c478bd9Sstevel@tonic-gate 		new_pgcnt = page_busy(B_TRUE);
42107c478bd9Sstevel@tonic-gate 		vfs_syncprogress();
42117c478bd9Sstevel@tonic-gate 
42127c478bd9Sstevel@tonic-gate 		if (new_bufcnt == 0 && new_pgcnt == 0)
42137c478bd9Sstevel@tonic-gate 			break;
42147c478bd9Sstevel@tonic-gate 
42157c478bd9Sstevel@tonic-gate 		if (new_bufcnt < old_bufcnt || new_pgcnt < old_pgcnt)
42167c478bd9Sstevel@tonic-gate 			sync_triesleft = sync_retries;
42177c478bd9Sstevel@tonic-gate 		else
42187c478bd9Sstevel@tonic-gate 			sync_triesleft--;
42197c478bd9Sstevel@tonic-gate 
42207c478bd9Sstevel@tonic-gate 		if (new_bufcnt)
42217c478bd9Sstevel@tonic-gate 			printf(" [%d]", new_bufcnt);
42227c478bd9Sstevel@tonic-gate 		if (new_pgcnt)
42237c478bd9Sstevel@tonic-gate 			printf(" %lu", new_pgcnt);
42247c478bd9Sstevel@tonic-gate 
42257c478bd9Sstevel@tonic-gate 		delay(hz);
42267c478bd9Sstevel@tonic-gate 	}
42277c478bd9Sstevel@tonic-gate 
42287c478bd9Sstevel@tonic-gate 	if (new_bufcnt != 0 || new_pgcnt != 0)
42297c478bd9Sstevel@tonic-gate 		printf(" done (not all i/o completed)\n");
42307c478bd9Sstevel@tonic-gate 	else
42317c478bd9Sstevel@tonic-gate 		printf(" done\n");
42327c478bd9Sstevel@tonic-gate 
42337c478bd9Sstevel@tonic-gate 	sync_timeleft = 0;
42347c478bd9Sstevel@tonic-gate 	delay(hz);
42357c478bd9Sstevel@tonic-gate }
42367c478bd9Sstevel@tonic-gate 
42377c478bd9Sstevel@tonic-gate /*
42387c478bd9Sstevel@tonic-gate  * If we are in the middle of the sync phase of panic, reset sync_timeleft to
42397c478bd9Sstevel@tonic-gate  * sync_timeout to indicate that we are making progress and the deadman()
42407c478bd9Sstevel@tonic-gate  * omnipresent cyclic should not yet time us out.  Note that it is safe to
42417c478bd9Sstevel@tonic-gate  * store to sync_timeleft here since the deadman() is firing at high-level
42427c478bd9Sstevel@tonic-gate  * on top of us.  If we are racing with the deadman(), either the deadman()
42437c478bd9Sstevel@tonic-gate  * will decrement the old value and then we will reset it, or we will
42447c478bd9Sstevel@tonic-gate  * reset it and then the deadman() will immediately decrement it.  In either
42457c478bd9Sstevel@tonic-gate  * case, correct behavior results.
42467c478bd9Sstevel@tonic-gate  */
42477c478bd9Sstevel@tonic-gate void
42487c478bd9Sstevel@tonic-gate vfs_syncprogress(void)
42497c478bd9Sstevel@tonic-gate {
42507c478bd9Sstevel@tonic-gate 	if (panicstr)
42517c478bd9Sstevel@tonic-gate 		sync_timeleft = sync_timeout;
42527c478bd9Sstevel@tonic-gate }
42537c478bd9Sstevel@tonic-gate 
42547c478bd9Sstevel@tonic-gate /*
42557c478bd9Sstevel@tonic-gate  * Map VFS flags to statvfs flags.  These shouldn't really be separate
42567c478bd9Sstevel@tonic-gate  * flags at all.
42577c478bd9Sstevel@tonic-gate  */
42587c478bd9Sstevel@tonic-gate uint_t
42597c478bd9Sstevel@tonic-gate vf_to_stf(uint_t vf)
42607c478bd9Sstevel@tonic-gate {
42617c478bd9Sstevel@tonic-gate 	uint_t stf = 0;
42627c478bd9Sstevel@tonic-gate 
42637c478bd9Sstevel@tonic-gate 	if (vf & VFS_RDONLY)
42647c478bd9Sstevel@tonic-gate 		stf |= ST_RDONLY;
42657c478bd9Sstevel@tonic-gate 	if (vf & VFS_NOSETUID)
42667c478bd9Sstevel@tonic-gate 		stf |= ST_NOSUID;
42677c478bd9Sstevel@tonic-gate 	if (vf & VFS_NOTRUNC)
42687c478bd9Sstevel@tonic-gate 		stf |= ST_NOTRUNC;
42697c478bd9Sstevel@tonic-gate 
42707c478bd9Sstevel@tonic-gate 	return (stf);
42717c478bd9Sstevel@tonic-gate }
42727c478bd9Sstevel@tonic-gate 
42737c478bd9Sstevel@tonic-gate /*
42747c478bd9Sstevel@tonic-gate  * Entries for (illegal) fstype 0.
42757c478bd9Sstevel@tonic-gate  */
42767c478bd9Sstevel@tonic-gate /* ARGSUSED */
42777c478bd9Sstevel@tonic-gate int
42787c478bd9Sstevel@tonic-gate vfsstray_sync(struct vfs *vfsp, short arg, struct cred *cr)
42797c478bd9Sstevel@tonic-gate {
42807c478bd9Sstevel@tonic-gate 	cmn_err(CE_PANIC, "stray vfs operation");
42817c478bd9Sstevel@tonic-gate 	return (0);
42827c478bd9Sstevel@tonic-gate }
42837c478bd9Sstevel@tonic-gate 
42847c478bd9Sstevel@tonic-gate /*
42857c478bd9Sstevel@tonic-gate  * Entries for (illegal) fstype 0.
42867c478bd9Sstevel@tonic-gate  */
42877c478bd9Sstevel@tonic-gate int
42887c478bd9Sstevel@tonic-gate vfsstray(void)
42897c478bd9Sstevel@tonic-gate {
42907c478bd9Sstevel@tonic-gate 	cmn_err(CE_PANIC, "stray vfs operation");
42917c478bd9Sstevel@tonic-gate 	return (0);
42927c478bd9Sstevel@tonic-gate }
42937c478bd9Sstevel@tonic-gate 
42947c478bd9Sstevel@tonic-gate /*
42957c478bd9Sstevel@tonic-gate  * Support for dealing with forced UFS unmount and its interaction with
42967c478bd9Sstevel@tonic-gate  * LOFS. Could be used by any filesystem.
42977c478bd9Sstevel@tonic-gate  * See bug 1203132.
42987c478bd9Sstevel@tonic-gate  */
42997c478bd9Sstevel@tonic-gate int
43007c478bd9Sstevel@tonic-gate vfs_EIO(void)
43017c478bd9Sstevel@tonic-gate {
43027c478bd9Sstevel@tonic-gate 	return (EIO);
43037c478bd9Sstevel@tonic-gate }
43047c478bd9Sstevel@tonic-gate 
43057c478bd9Sstevel@tonic-gate /*
43067c478bd9Sstevel@tonic-gate  * We've gotta define the op for sync separately, since the compiler gets
43077c478bd9Sstevel@tonic-gate  * confused if we mix and match ANSI and normal style prototypes when
43087c478bd9Sstevel@tonic-gate  * a "short" argument is present and spits out a warning.
43097c478bd9Sstevel@tonic-gate  */
43107c478bd9Sstevel@tonic-gate /*ARGSUSED*/
43117c478bd9Sstevel@tonic-gate int
43127c478bd9Sstevel@tonic-gate vfs_EIO_sync(struct vfs *vfsp, short arg, struct cred *cr)
43137c478bd9Sstevel@tonic-gate {
43147c478bd9Sstevel@tonic-gate 	return (EIO);
43157c478bd9Sstevel@tonic-gate }
43167c478bd9Sstevel@tonic-gate 
43177c478bd9Sstevel@tonic-gate vfs_t EIO_vfs;
43187c478bd9Sstevel@tonic-gate vfsops_t *EIO_vfsops;
43197c478bd9Sstevel@tonic-gate 
43207c478bd9Sstevel@tonic-gate /*
43217c478bd9Sstevel@tonic-gate  * Called from startup() to initialize all loaded vfs's
43227c478bd9Sstevel@tonic-gate  */
43237c478bd9Sstevel@tonic-gate void
43247c478bd9Sstevel@tonic-gate vfsinit(void)
43257c478bd9Sstevel@tonic-gate {
43267c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
43277c478bd9Sstevel@tonic-gate 	int error;
432882c7f3c4Srsb 	extern int vopstats_enabled;
43295a59a8b3Srsb 	extern void vopstats_startup();
43307c478bd9Sstevel@tonic-gate 
43317c478bd9Sstevel@tonic-gate 	static const fs_operation_def_t EIO_vfsops_template[] = {
4332aa59c4cbSrsb 		VFSNAME_MOUNT,		{ .error = vfs_EIO },
4333aa59c4cbSrsb 		VFSNAME_UNMOUNT,	{ .error = vfs_EIO },
4334aa59c4cbSrsb 		VFSNAME_ROOT,		{ .error = vfs_EIO },
4335aa59c4cbSrsb 		VFSNAME_STATVFS,	{ .error = vfs_EIO },
4336aa59c4cbSrsb 		VFSNAME_SYNC, 		{ .vfs_sync = vfs_EIO_sync },
4337aa59c4cbSrsb 		VFSNAME_VGET,		{ .error = vfs_EIO },
4338aa59c4cbSrsb 		VFSNAME_MOUNTROOT,	{ .error = vfs_EIO },
4339aa59c4cbSrsb 		VFSNAME_FREEVFS,	{ .error = vfs_EIO },
4340aa59c4cbSrsb 		VFSNAME_VNSTATE,	{ .error = vfs_EIO },
43417c478bd9Sstevel@tonic-gate 		NULL, NULL
43427c478bd9Sstevel@tonic-gate 	};
43437c478bd9Sstevel@tonic-gate 
4344aa59c4cbSrsb 	static const fs_operation_def_t stray_vfsops_template[] = {
4345aa59c4cbSrsb 		VFSNAME_MOUNT,		{ .error = vfsstray },
4346aa59c4cbSrsb 		VFSNAME_UNMOUNT,	{ .error = vfsstray },
4347aa59c4cbSrsb 		VFSNAME_ROOT,		{ .error = vfsstray },
4348aa59c4cbSrsb 		VFSNAME_STATVFS,	{ .error = vfsstray },
4349aa59c4cbSrsb 		VFSNAME_SYNC, 		{ .vfs_sync = vfsstray_sync },
4350aa59c4cbSrsb 		VFSNAME_VGET,		{ .error = vfsstray },
4351aa59c4cbSrsb 		VFSNAME_MOUNTROOT,	{ .error = vfsstray },
4352aa59c4cbSrsb 		VFSNAME_FREEVFS,	{ .error = vfsstray },
4353aa59c4cbSrsb 		VFSNAME_VNSTATE,	{ .error = vfsstray },
4354aa59c4cbSrsb 		NULL, NULL
4355aa59c4cbSrsb 	};
43567c478bd9Sstevel@tonic-gate 
4357da6c28aaSamw 	/* Create vfs cache */
4358da6c28aaSamw 	vfs_cache = kmem_cache_create("vfs_cache", sizeof (struct vfs),
4359da6c28aaSamw 	    sizeof (uintptr_t), NULL, NULL, NULL, NULL, NULL, 0);
43607c478bd9Sstevel@tonic-gate 
4361da6c28aaSamw 	/* Initialize the vnode cache (file systems may use it during init). */
43627c478bd9Sstevel@tonic-gate 	vn_create_cache();
43637c478bd9Sstevel@tonic-gate 
43647c478bd9Sstevel@tonic-gate 	/* Setup event monitor framework */
43657c478bd9Sstevel@tonic-gate 	fem_init();
43667c478bd9Sstevel@tonic-gate 
43677c478bd9Sstevel@tonic-gate 	/* Initialize the dummy stray file system type. */
4368d320ee2dSrsb 	error = vfs_setfsops(0, stray_vfsops_template, NULL);
43697c478bd9Sstevel@tonic-gate 
43707c478bd9Sstevel@tonic-gate 	/* Initialize the dummy EIO file system. */
43717c478bd9Sstevel@tonic-gate 	error = vfs_makefsops(EIO_vfsops_template, &EIO_vfsops);
43727c478bd9Sstevel@tonic-gate 	if (error != 0) {
43737c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "vfsinit: bad EIO vfs ops template");
43747c478bd9Sstevel@tonic-gate 		/* Shouldn't happen, but not bad enough to panic */
43757c478bd9Sstevel@tonic-gate 	}
43767c478bd9Sstevel@tonic-gate 
43777c478bd9Sstevel@tonic-gate 	VFS_INIT(&EIO_vfs, EIO_vfsops, (caddr_t)NULL);
43787c478bd9Sstevel@tonic-gate 
43797c478bd9Sstevel@tonic-gate 	/*
43807c478bd9Sstevel@tonic-gate 	 * Default EIO_vfs.vfs_flag to VFS_UNMOUNTED so a lookup
43817c478bd9Sstevel@tonic-gate 	 * on this vfs can immediately notice it's invalid.
43827c478bd9Sstevel@tonic-gate 	 */
43837c478bd9Sstevel@tonic-gate 	EIO_vfs.vfs_flag |= VFS_UNMOUNTED;
43847c478bd9Sstevel@tonic-gate 
43857c478bd9Sstevel@tonic-gate 	/*
43867c478bd9Sstevel@tonic-gate 	 * Call the init routines of non-loadable filesystems only.
43877c478bd9Sstevel@tonic-gate 	 * Filesystems which are loaded as separate modules will be
43887c478bd9Sstevel@tonic-gate 	 * initialized by the module loading code instead.
43897c478bd9Sstevel@tonic-gate 	 */
43907c478bd9Sstevel@tonic-gate 
43917c478bd9Sstevel@tonic-gate 	for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
43927c478bd9Sstevel@tonic-gate 		RLOCK_VFSSW();
43937c478bd9Sstevel@tonic-gate 		if (vswp->vsw_init != NULL)
43947c478bd9Sstevel@tonic-gate 			(*vswp->vsw_init)(vswp - vfssw, vswp->vsw_name);
43957c478bd9Sstevel@tonic-gate 		RUNLOCK_VFSSW();
43967c478bd9Sstevel@tonic-gate 	}
43975a59a8b3Srsb 
43985a59a8b3Srsb 	vopstats_startup();
439982c7f3c4Srsb 
440082c7f3c4Srsb 	if (vopstats_enabled) {
440182c7f3c4Srsb 		/* EIO_vfs can collect stats, but we don't retrieve them */
440282c7f3c4Srsb 		initialize_vopstats(&EIO_vfs.vfs_vopstats);
440382c7f3c4Srsb 		EIO_vfs.vfs_fstypevsp = NULL;
440482c7f3c4Srsb 		EIO_vfs.vfs_vskap = NULL;
440582c7f3c4Srsb 		EIO_vfs.vfs_flag |= VFS_STATS;
440682c7f3c4Srsb 	}
4407da6c28aaSamw 
4408da6c28aaSamw 	xattr_init();
44097a286c47SDai Ngo 
44107a286c47SDai Ngo 	reparse_point_init();
4411da6c28aaSamw }
4412da6c28aaSamw 
4413da6c28aaSamw vfs_t *
4414da6c28aaSamw vfs_alloc(int kmflag)
4415da6c28aaSamw {
4416da6c28aaSamw 	vfs_t *vfsp;
4417da6c28aaSamw 
4418da6c28aaSamw 	vfsp = kmem_cache_alloc(vfs_cache, kmflag);
4419da6c28aaSamw 
4420da6c28aaSamw 	/*
4421da6c28aaSamw 	 * Do the simplest initialization here.
4422da6c28aaSamw 	 * Everything else gets done in vfs_init()
4423da6c28aaSamw 	 */
4424da6c28aaSamw 	bzero(vfsp, sizeof (vfs_t));
4425da6c28aaSamw 	return (vfsp);
4426da6c28aaSamw }
4427da6c28aaSamw 
4428da6c28aaSamw void
4429da6c28aaSamw vfs_free(vfs_t *vfsp)
4430da6c28aaSamw {
4431da6c28aaSamw 	/*
4432da6c28aaSamw 	 * One would be tempted to assert that "vfsp->vfs_count == 0".
4433da6c28aaSamw 	 * The problem is that this gets called out of domount() with
4434da6c28aaSamw 	 * a partially initialized vfs and a vfs_count of 1.  This is
4435da6c28aaSamw 	 * also called from vfs_rele() with a vfs_count of 0.  We can't
4436da6c28aaSamw 	 * call VFS_RELE() from domount() if VFS_MOUNT() hasn't successfully
4437da6c28aaSamw 	 * returned.  This is because VFS_MOUNT() fully initializes the
4438da6c28aaSamw 	 * vfs structure and its associated data.  VFS_RELE() will call
4439da6c28aaSamw 	 * VFS_FREEVFS() which may panic the system if the data structures
4440da6c28aaSamw 	 * aren't fully initialized from a successful VFS_MOUNT()).
4441da6c28aaSamw 	 */
4442da6c28aaSamw 
4443da6c28aaSamw 	/* If FEM was in use, make sure everything gets cleaned up */
4444da6c28aaSamw 	if (vfsp->vfs_femhead) {
4445da6c28aaSamw 		ASSERT(vfsp->vfs_femhead->femh_list == NULL);
4446da6c28aaSamw 		mutex_destroy(&vfsp->vfs_femhead->femh_lock);
4447da6c28aaSamw 		kmem_free(vfsp->vfs_femhead, sizeof (*(vfsp->vfs_femhead)));
4448da6c28aaSamw 		vfsp->vfs_femhead = NULL;
4449da6c28aaSamw 	}
4450da6c28aaSamw 
4451da6c28aaSamw 	if (vfsp->vfs_implp)
4452da6c28aaSamw 		vfsimpl_teardown(vfsp);
4453da6c28aaSamw 	sema_destroy(&vfsp->vfs_reflock);
4454da6c28aaSamw 	kmem_cache_free(vfs_cache, vfsp);
44557c478bd9Sstevel@tonic-gate }
44567c478bd9Sstevel@tonic-gate 
44577c478bd9Sstevel@tonic-gate /*
44587c478bd9Sstevel@tonic-gate  * Increments the vfs reference count by one atomically.
44597c478bd9Sstevel@tonic-gate  */
44607c478bd9Sstevel@tonic-gate void
44617c478bd9Sstevel@tonic-gate vfs_hold(vfs_t *vfsp)
44627c478bd9Sstevel@tonic-gate {
44631a5e258fSJosef 'Jeff' Sipek 	atomic_inc_32(&vfsp->vfs_count);
44647c478bd9Sstevel@tonic-gate 	ASSERT(vfsp->vfs_count != 0);
44657c478bd9Sstevel@tonic-gate }
44667c478bd9Sstevel@tonic-gate 
44677c478bd9Sstevel@tonic-gate /*
44687c478bd9Sstevel@tonic-gate  * Decrements the vfs reference count by one atomically. When
44697c478bd9Sstevel@tonic-gate  * vfs reference count becomes zero, it calls the file system
44707c478bd9Sstevel@tonic-gate  * specific vfs_freevfs() to free up the resources.
44717c478bd9Sstevel@tonic-gate  */
44727c478bd9Sstevel@tonic-gate void
44737c478bd9Sstevel@tonic-gate vfs_rele(vfs_t *vfsp)
44747c478bd9Sstevel@tonic-gate {
44757c478bd9Sstevel@tonic-gate 	ASSERT(vfsp->vfs_count != 0);
44761a5e258fSJosef 'Jeff' Sipek 	if (atomic_dec_32_nv(&vfsp->vfs_count) == 0) {
44777c478bd9Sstevel@tonic-gate 		VFS_FREEVFS(vfsp);
447893239addSjohnlev 		lofi_remove(vfsp);
44797c478bd9Sstevel@tonic-gate 		if (vfsp->vfs_zone)
4480a19609f8Sjv227347 			zone_rele_ref(&vfsp->vfs_implp->vi_zone_ref,
4481a19609f8Sjv227347 			    ZONE_REF_VFS);
44827c478bd9Sstevel@tonic-gate 		vfs_freemnttab(vfsp);
4483da6c28aaSamw 		vfs_free(vfsp);
44847c478bd9Sstevel@tonic-gate 	}
44857c478bd9Sstevel@tonic-gate }
44867c478bd9Sstevel@tonic-gate 
44877c478bd9Sstevel@tonic-gate /*
44887c478bd9Sstevel@tonic-gate  * Generic operations vector support.
44897c478bd9Sstevel@tonic-gate  *
44907c478bd9Sstevel@tonic-gate  * This is used to build operations vectors for both the vfs and vnode.
44917c478bd9Sstevel@tonic-gate  * It's normally called only when a file system is loaded.
44927c478bd9Sstevel@tonic-gate  *
44937c478bd9Sstevel@tonic-gate  * There are many possible algorithms for this, including the following:
44947c478bd9Sstevel@tonic-gate  *
44957c478bd9Sstevel@tonic-gate  *   (1) scan the list of known operations; for each, see if the file system
44967c478bd9Sstevel@tonic-gate  *       includes an entry for it, and fill it in as appropriate.
44977c478bd9Sstevel@tonic-gate  *
44987c478bd9Sstevel@tonic-gate  *   (2) set up defaults for all known operations.  scan the list of ops
44997c478bd9Sstevel@tonic-gate  *       supplied by the file system; for each which is both supplied and
45007c478bd9Sstevel@tonic-gate  *       known, fill it in.
45017c478bd9Sstevel@tonic-gate  *
45027c478bd9Sstevel@tonic-gate  *   (3) sort the lists of known ops & supplied ops; scan the list, filling
45037c478bd9Sstevel@tonic-gate  *       in entries as we go.
45047c478bd9Sstevel@tonic-gate  *
45057c478bd9Sstevel@tonic-gate  * we choose (1) for simplicity, and because performance isn't critical here.
45067c478bd9Sstevel@tonic-gate  * note that (2) could be sped up using a precomputed hash table on known ops.
45077c478bd9Sstevel@tonic-gate  * (3) could be faster than either, but only if the lists were very large or
45087c478bd9Sstevel@tonic-gate  * supplied in sorted order.
45097c478bd9Sstevel@tonic-gate  *
45107c478bd9Sstevel@tonic-gate  */
45117c478bd9Sstevel@tonic-gate 
45127c478bd9Sstevel@tonic-gate int
45137c478bd9Sstevel@tonic-gate fs_build_vector(void *vector, int *unused_ops,
45147c478bd9Sstevel@tonic-gate     const fs_operation_trans_def_t *translation,
45157c478bd9Sstevel@tonic-gate     const fs_operation_def_t *operations)
45167c478bd9Sstevel@tonic-gate {
45177c478bd9Sstevel@tonic-gate 	int i, num_trans, num_ops, used;
45187c478bd9Sstevel@tonic-gate 
4519aa59c4cbSrsb 	/*
4520aa59c4cbSrsb 	 * Count the number of translations and the number of supplied
4521aa59c4cbSrsb 	 * operations.
4522aa59c4cbSrsb 	 */
45237c478bd9Sstevel@tonic-gate 
45247c478bd9Sstevel@tonic-gate 	{
45257c478bd9Sstevel@tonic-gate 		const fs_operation_trans_def_t *p;
45267c478bd9Sstevel@tonic-gate 
45277c478bd9Sstevel@tonic-gate 		for (num_trans = 0, p = translation;
45287c478bd9Sstevel@tonic-gate 		    p->name != NULL;
45297c478bd9Sstevel@tonic-gate 		    num_trans++, p++)
45307c478bd9Sstevel@tonic-gate 			;
45317c478bd9Sstevel@tonic-gate 	}
45327c478bd9Sstevel@tonic-gate 
45337c478bd9Sstevel@tonic-gate 	{
45347c478bd9Sstevel@tonic-gate 		const fs_operation_def_t *p;
45357c478bd9Sstevel@tonic-gate 
45367c478bd9Sstevel@tonic-gate 		for (num_ops = 0, p = operations;
45377c478bd9Sstevel@tonic-gate 		    p->name != NULL;
45387c478bd9Sstevel@tonic-gate 		    num_ops++, p++)
45397c478bd9Sstevel@tonic-gate 			;
45407c478bd9Sstevel@tonic-gate 	}
45417c478bd9Sstevel@tonic-gate 
45427c478bd9Sstevel@tonic-gate 	/* Walk through each operation known to our caller.  There will be */
45437c478bd9Sstevel@tonic-gate 	/* one entry in the supplied "translation table" for each. */
45447c478bd9Sstevel@tonic-gate 
45457c478bd9Sstevel@tonic-gate 	used = 0;
45467c478bd9Sstevel@tonic-gate 
45477c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_trans; i++) {
45487c478bd9Sstevel@tonic-gate 		int j, found;
45497c478bd9Sstevel@tonic-gate 		char *curname;
45507c478bd9Sstevel@tonic-gate 		fs_generic_func_p result;
45517c478bd9Sstevel@tonic-gate 		fs_generic_func_p *location;
45527c478bd9Sstevel@tonic-gate 
45537c478bd9Sstevel@tonic-gate 		curname = translation[i].name;
45547c478bd9Sstevel@tonic-gate 
45557c478bd9Sstevel@tonic-gate 		/* Look for a matching operation in the list supplied by the */
45567c478bd9Sstevel@tonic-gate 		/* file system. */
45577c478bd9Sstevel@tonic-gate 
45587c478bd9Sstevel@tonic-gate 		found = 0;
45597c478bd9Sstevel@tonic-gate 
45607c478bd9Sstevel@tonic-gate 		for (j = 0; j < num_ops; j++) {
45617c478bd9Sstevel@tonic-gate 			if (strcmp(operations[j].name, curname) == 0) {
45627c478bd9Sstevel@tonic-gate 				used++;
45637c478bd9Sstevel@tonic-gate 				found = 1;
45647c478bd9Sstevel@tonic-gate 				break;
45657c478bd9Sstevel@tonic-gate 			}
45667c478bd9Sstevel@tonic-gate 		}
45677c478bd9Sstevel@tonic-gate 
4568aa59c4cbSrsb 		/*
4569aa59c4cbSrsb 		 * If the file system is using a "placeholder" for default
4570aa59c4cbSrsb 		 * or error functions, grab the appropriate function out of
4571aa59c4cbSrsb 		 * the translation table.  If the file system didn't supply
4572aa59c4cbSrsb 		 * this operation at all, use the default function.
4573aa59c4cbSrsb 		 */
45747c478bd9Sstevel@tonic-gate 
45757c478bd9Sstevel@tonic-gate 		if (found) {
4576aa59c4cbSrsb 			result = operations[j].func.fs_generic;
45777c478bd9Sstevel@tonic-gate 			if (result == fs_default) {
45787c478bd9Sstevel@tonic-gate 				result = translation[i].defaultFunc;
45797c478bd9Sstevel@tonic-gate 			} else if (result == fs_error) {
45807c478bd9Sstevel@tonic-gate 				result = translation[i].errorFunc;
45817c478bd9Sstevel@tonic-gate 			} else if (result == NULL) {
45827c478bd9Sstevel@tonic-gate 				/* Null values are PROHIBITED */
45837c478bd9Sstevel@tonic-gate 				return (EINVAL);
45847c478bd9Sstevel@tonic-gate 			}
45857c478bd9Sstevel@tonic-gate 		} else {
45867c478bd9Sstevel@tonic-gate 			result = translation[i].defaultFunc;
45877c478bd9Sstevel@tonic-gate 		}
45887c478bd9Sstevel@tonic-gate 
45897c478bd9Sstevel@tonic-gate 		/* Now store the function into the operations vector. */
45907c478bd9Sstevel@tonic-gate 
45917c478bd9Sstevel@tonic-gate 		location = (fs_generic_func_p *)
45927c478bd9Sstevel@tonic-gate 		    (((char *)vector) + translation[i].offset);
45937c478bd9Sstevel@tonic-gate 
45947c478bd9Sstevel@tonic-gate 		*location = result;
45957c478bd9Sstevel@tonic-gate 	}
45967c478bd9Sstevel@tonic-gate 
45977c478bd9Sstevel@tonic-gate 	*unused_ops = num_ops - used;
45987c478bd9Sstevel@tonic-gate 
45997c478bd9Sstevel@tonic-gate 	return (0);
46007c478bd9Sstevel@tonic-gate }
46017c478bd9Sstevel@tonic-gate 
46027c478bd9Sstevel@tonic-gate /* Placeholder functions, should never be called. */
46037c478bd9Sstevel@tonic-gate 
46047c478bd9Sstevel@tonic-gate int
46057c478bd9Sstevel@tonic-gate fs_error(void)
46067c478bd9Sstevel@tonic-gate {
46077c478bd9Sstevel@tonic-gate 	cmn_err(CE_PANIC, "fs_error called");
46087c478bd9Sstevel@tonic-gate 	return (0);
46097c478bd9Sstevel@tonic-gate }
46107c478bd9Sstevel@tonic-gate 
46117c478bd9Sstevel@tonic-gate int
46127c478bd9Sstevel@tonic-gate fs_default(void)
46137c478bd9Sstevel@tonic-gate {
46147c478bd9Sstevel@tonic-gate 	cmn_err(CE_PANIC, "fs_default called");
46157c478bd9Sstevel@tonic-gate 	return (0);
46167c478bd9Sstevel@tonic-gate }
46177c478bd9Sstevel@tonic-gate 
46187c478bd9Sstevel@tonic-gate #ifdef __sparc
46197c478bd9Sstevel@tonic-gate 
46207c478bd9Sstevel@tonic-gate /*
46217c478bd9Sstevel@tonic-gate  * Part of the implementation of booting off a mirrored root
46227c478bd9Sstevel@tonic-gate  * involves a change of dev_t for the root device.  To
46237c478bd9Sstevel@tonic-gate  * accomplish this, first remove the existing hash table
46247c478bd9Sstevel@tonic-gate  * entry for the root device, convert to the new dev_t,
46257c478bd9Sstevel@tonic-gate  * then re-insert in the hash table at the head of the list.
46267c478bd9Sstevel@tonic-gate  */
46277c478bd9Sstevel@tonic-gate void
46287c478bd9Sstevel@tonic-gate vfs_root_redev(vfs_t *vfsp, dev_t ndev, int fstype)
46297c478bd9Sstevel@tonic-gate {
46307c478bd9Sstevel@tonic-gate 	vfs_list_lock();
46317c478bd9Sstevel@tonic-gate 
46327c478bd9Sstevel@tonic-gate 	vfs_hash_remove(vfsp);
46337c478bd9Sstevel@tonic-gate 
46347c478bd9Sstevel@tonic-gate 	vfsp->vfs_dev = ndev;
46357c478bd9Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, ndev, fstype);
46367c478bd9Sstevel@tonic-gate 
46377c478bd9Sstevel@tonic-gate 	vfs_hash_add(vfsp, 1);
46387c478bd9Sstevel@tonic-gate 
46397c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
46407c478bd9Sstevel@tonic-gate }
46417c478bd9Sstevel@tonic-gate 
46427c478bd9Sstevel@tonic-gate #else /* x86 NEWBOOT */
46437c478bd9Sstevel@tonic-gate 
464406bbe1e0Sedp #if defined(__x86)
464506bbe1e0Sedp extern int hvmboot_rootconf();
464606bbe1e0Sedp #endif /* __x86 */
464706bbe1e0Sedp 
46486cefaae1SJack Meng extern ib_boot_prop_t *iscsiboot_prop;
46496cefaae1SJack Meng 
46507c478bd9Sstevel@tonic-gate int
46517c478bd9Sstevel@tonic-gate rootconf()
46527c478bd9Sstevel@tonic-gate {
46537c478bd9Sstevel@tonic-gate 	int error;
46547c478bd9Sstevel@tonic-gate 	struct vfssw *vsw;
46557c478bd9Sstevel@tonic-gate 	extern void pm_init();
4656843e1988Sjohnlev 	char *fstyp, *fsmod;
46576cefaae1SJack Meng 	int ret = -1;
46587c478bd9Sstevel@tonic-gate 
4659843e1988Sjohnlev 	getrootfs(&fstyp, &fsmod);
46607c478bd9Sstevel@tonic-gate 
466106bbe1e0Sedp #if defined(__x86)
466206bbe1e0Sedp 	/*
466306bbe1e0Sedp 	 * hvmboot_rootconf() is defined in the hvm_bootstrap misc module,
466406bbe1e0Sedp 	 * which lives in /platform/i86hvm, and hence is only available when
466506bbe1e0Sedp 	 * booted in an x86 hvm environment.  If the hvm_bootstrap misc module
466606bbe1e0Sedp 	 * is not available then the modstub for this function will return 0.
466706bbe1e0Sedp 	 * If the hvm_bootstrap misc module is available it will be loaded
466806bbe1e0Sedp 	 * and hvmboot_rootconf() will be invoked.
466906bbe1e0Sedp 	 */
467006bbe1e0Sedp 	if (error = hvmboot_rootconf())
467106bbe1e0Sedp 		return (error);
467206bbe1e0Sedp #endif /* __x86 */
467306bbe1e0Sedp 
46747c478bd9Sstevel@tonic-gate 	if (error = clboot_rootconf())
46757c478bd9Sstevel@tonic-gate 		return (error);
46767c478bd9Sstevel@tonic-gate 
4677843e1988Sjohnlev 	if (modload("fs", fsmod) == -1)
4678843e1988Sjohnlev 		panic("Cannot _init %s module", fsmod);
46797c478bd9Sstevel@tonic-gate 
46807c478bd9Sstevel@tonic-gate 	RLOCK_VFSSW();
46817c478bd9Sstevel@tonic-gate 	vsw = vfs_getvfsswbyname(fstyp);
46827c478bd9Sstevel@tonic-gate 	RUNLOCK_VFSSW();
468356c0b1daSPavel Filipensky 	if (vsw == NULL) {
468456c0b1daSPavel Filipensky 		cmn_err(CE_CONT, "Cannot find %s filesystem\n", fstyp);
468556c0b1daSPavel Filipensky 		return (ENXIO);
468656c0b1daSPavel Filipensky 	}
46877c478bd9Sstevel@tonic-gate 	VFS_INIT(rootvfs, &vsw->vsw_vfsops, 0);
46887c478bd9Sstevel@tonic-gate 	VFS_HOLD(rootvfs);
46897c478bd9Sstevel@tonic-gate 
46907c478bd9Sstevel@tonic-gate 	/* always mount readonly first */
46917c478bd9Sstevel@tonic-gate 	rootvfs->vfs_flag |= VFS_RDONLY;
46927c478bd9Sstevel@tonic-gate 
46937c478bd9Sstevel@tonic-gate 	pm_init();
46947c478bd9Sstevel@tonic-gate 
46956cefaae1SJack Meng 	if (netboot && iscsiboot_prop) {
46966cefaae1SJack Meng 		cmn_err(CE_WARN, "NFS boot and iSCSI boot"
46976cefaae1SJack Meng 		    " shouldn't happen in the same time");
46986cefaae1SJack Meng 		return (EINVAL);
46996cefaae1SJack Meng 	}
47006cefaae1SJack Meng 
4701dedec472SJack Meng 	if (netboot || iscsiboot_prop) {
47026cefaae1SJack Meng 		ret = strplumb();
4703dedec472SJack Meng 		if (ret != 0) {
4704dedec472SJack Meng 			cmn_err(CE_WARN, "Cannot plumb network device %d", ret);
4705dedec472SJack Meng 			return (EFAULT);
4706dedec472SJack Meng 		}
4707dedec472SJack Meng 	}
47086cefaae1SJack Meng 
47096cefaae1SJack Meng 	if ((ret == 0) && iscsiboot_prop) {
47106cefaae1SJack Meng 		ret = modload("drv", "iscsi");
47116cefaae1SJack Meng 		/* -1 indicates fail */
47126cefaae1SJack Meng 		if (ret == -1) {
47136cefaae1SJack Meng 			cmn_err(CE_WARN, "Failed to load iscsi module");
47146cefaae1SJack Meng 			iscsi_boot_prop_free();
47156cefaae1SJack Meng 			return (EINVAL);
47166cefaae1SJack Meng 		} else {
47176cefaae1SJack Meng 			if (!i_ddi_attach_pseudo_node("iscsi")) {
47186cefaae1SJack Meng 				cmn_err(CE_WARN,
47196cefaae1SJack Meng 				    "Failed to attach iscsi driver");
47206cefaae1SJack Meng 				iscsi_boot_prop_free();
47216cefaae1SJack Meng 				return (ENODEV);
47226cefaae1SJack Meng 			}
47236cefaae1SJack Meng 		}
47246cefaae1SJack Meng 	}
47257c478bd9Sstevel@tonic-gate 
47267c478bd9Sstevel@tonic-gate 	error = VFS_MOUNTROOT(rootvfs, ROOT_INIT);
47277c478bd9Sstevel@tonic-gate 	vfs_unrefvfssw(vsw);
47287c478bd9Sstevel@tonic-gate 	rootdev = rootvfs->vfs_dev;
47297c478bd9Sstevel@tonic-gate 
47307c478bd9Sstevel@tonic-gate 	if (error)
473156c0b1daSPavel Filipensky 		cmn_err(CE_CONT, "Cannot mount root on %s fstype %s\n",
473256c0b1daSPavel Filipensky 		    rootfs.bo_name, fstyp);
473356c0b1daSPavel Filipensky 	else
473456c0b1daSPavel Filipensky 		cmn_err(CE_CONT, "?root on %s fstype %s\n",
473556c0b1daSPavel Filipensky 		    rootfs.bo_name, fstyp);
47367c478bd9Sstevel@tonic-gate 	return (error);
47377c478bd9Sstevel@tonic-gate }
47387c478bd9Sstevel@tonic-gate 
47397c478bd9Sstevel@tonic-gate /*
47407c478bd9Sstevel@tonic-gate  * XXX this is called by nfs only and should probably be removed
47417c478bd9Sstevel@tonic-gate  * If booted with ASKNAME, prompt on the console for a filesystem
47427c478bd9Sstevel@tonic-gate  * name and return it.
47437c478bd9Sstevel@tonic-gate  */
47447c478bd9Sstevel@tonic-gate void
47457c478bd9Sstevel@tonic-gate getfsname(char *askfor, char *name, size_t namelen)
47467c478bd9Sstevel@tonic-gate {
47477c478bd9Sstevel@tonic-gate 	if (boothowto & RB_ASKNAME) {
47487c478bd9Sstevel@tonic-gate 		printf("%s name: ", askfor);
47497c478bd9Sstevel@tonic-gate 		console_gets(name, namelen);
47507c478bd9Sstevel@tonic-gate 	}
47517c478bd9Sstevel@tonic-gate }
47527c478bd9Sstevel@tonic-gate 
47537c478bd9Sstevel@tonic-gate /*
475456c0b1daSPavel Filipensky  * Init the root filesystem type (rootfs.bo_fstype) from the "fstype"
475556c0b1daSPavel Filipensky  * property.
475656c0b1daSPavel Filipensky  *
475756c0b1daSPavel Filipensky  * Filesystem types starting with the prefix "nfs" are diskless clients;
475856c0b1daSPavel Filipensky  * init the root filename name (rootfs.bo_name), too.
475956c0b1daSPavel Filipensky  *
476056c0b1daSPavel Filipensky  * If we are booting via NFS we currently have these options:
476156c0b1daSPavel Filipensky  *	nfs -	dynamically choose NFS V2, V3, or V4 (default)
476256c0b1daSPavel Filipensky  *	nfs2 -	force NFS V2
476356c0b1daSPavel Filipensky  *	nfs3 -	force NFS V3
476456c0b1daSPavel Filipensky  *	nfs4 -	force NFS V4
476556c0b1daSPavel Filipensky  * Because we need to maintain backward compatibility with the naming
476656c0b1daSPavel Filipensky  * convention that the NFS V2 filesystem name is "nfs" (see vfs_conf.c)
476756c0b1daSPavel Filipensky  * we need to map "nfs" => "nfsdyn" and "nfs2" => "nfs".  The dynamic
476856c0b1daSPavel Filipensky  * nfs module will map the type back to either "nfs", "nfs3", or "nfs4".
4769bd93c05dSAlexander Eremin  * This is only for root filesystems, all other uses will expect
4770bd93c05dSAlexander Eremin  * that "nfs" == NFS V2.
47717c478bd9Sstevel@tonic-gate  */
4772843e1988Sjohnlev static void
4773843e1988Sjohnlev getrootfs(char **fstypp, char **fsmodp)
47747c478bd9Sstevel@tonic-gate {
47757c478bd9Sstevel@tonic-gate 	extern char *strplumb_get_netdev_path(void);
47767c478bd9Sstevel@tonic-gate 	char *propstr = NULL;
47777c478bd9Sstevel@tonic-gate 
477856c0b1daSPavel Filipensky 	/*
477956c0b1daSPavel Filipensky 	 * Check fstype property; for diskless it should be one of "nfs",
478056c0b1daSPavel Filipensky 	 * "nfs2", "nfs3" or "nfs4".
478156c0b1daSPavel Filipensky 	 */
47827c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
47837c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "fstype", &propstr)
47847c478bd9Sstevel@tonic-gate 	    == DDI_SUCCESS) {
47857c478bd9Sstevel@tonic-gate 		(void) strncpy(rootfs.bo_fstype, propstr, BO_MAXFSNAME);
4786b1b8ab34Slling 		ddi_prop_free(propstr);
4787b1b8ab34Slling 
4788b1b8ab34Slling 	/*
4789b1b8ab34Slling 	 * if the boot property 'fstype' is not set, but 'zfs-bootfs' is set,
4790b1b8ab34Slling 	 * assume the type of this root filesystem is 'zfs'.
4791b1b8ab34Slling 	 */
4792b1b8ab34Slling 	} else if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4793b1b8ab34Slling 	    DDI_PROP_DONTPASS, "zfs-bootfs", &propstr)
4794b1b8ab34Slling 	    == DDI_SUCCESS) {
4795b1b8ab34Slling 		(void) strncpy(rootfs.bo_fstype, "zfs", BO_MAXFSNAME);
47967c478bd9Sstevel@tonic-gate 		ddi_prop_free(propstr);
47977c478bd9Sstevel@tonic-gate 	}
47987c478bd9Sstevel@tonic-gate 
4799843e1988Sjohnlev 	if (strncmp(rootfs.bo_fstype, "nfs", 3) != 0) {
4800843e1988Sjohnlev 		*fstypp = *fsmodp = rootfs.bo_fstype;
4801843e1988Sjohnlev 		return;
4802843e1988Sjohnlev 	}
48037c478bd9Sstevel@tonic-gate 
48047c478bd9Sstevel@tonic-gate 	++netboot;
480556c0b1daSPavel Filipensky 
480656c0b1daSPavel Filipensky 	if (strcmp(rootfs.bo_fstype, "nfs2") == 0)
480756c0b1daSPavel Filipensky 		(void) strcpy(rootfs.bo_fstype, "nfs");
480856c0b1daSPavel Filipensky 	else if (strcmp(rootfs.bo_fstype, "nfs") == 0)
480956c0b1daSPavel Filipensky 		(void) strcpy(rootfs.bo_fstype, "nfsdyn");
481056c0b1daSPavel Filipensky 
4811843e1988Sjohnlev 	/*
4812843e1988Sjohnlev 	 * check if path to network interface is specified in bootpath
4813843e1988Sjohnlev 	 * or by a hypervisor domain configuration file.
4814843e1988Sjohnlev 	 * XXPV - enable strlumb_get_netdev_path()
4815843e1988Sjohnlev 	 */
4816843e1988Sjohnlev 	if (ddi_prop_exists(DDI_DEV_T_ANY, ddi_root_node(), DDI_PROP_DONTPASS,
4817843e1988Sjohnlev 	    "xpv-nfsroot")) {
4818843e1988Sjohnlev 		(void) strcpy(rootfs.bo_name, "/xpvd/xnf@0");
4819843e1988Sjohnlev 	} else if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
48207c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "bootpath", &propstr)
48217c478bd9Sstevel@tonic-gate 	    == DDI_SUCCESS) {
48227c478bd9Sstevel@tonic-gate 		(void) strncpy(rootfs.bo_name, propstr, BO_MAXOBJNAME);
48237c478bd9Sstevel@tonic-gate 		ddi_prop_free(propstr);
48247c478bd9Sstevel@tonic-gate 	} else {
48257c478bd9Sstevel@tonic-gate 		/* attempt to determine netdev_path via boot_mac address */
48267c478bd9Sstevel@tonic-gate 		netdev_path = strplumb_get_netdev_path();
48277c478bd9Sstevel@tonic-gate 		if (netdev_path == NULL)
4828ae115bc7Smrj 			panic("cannot find boot network interface");
48297c478bd9Sstevel@tonic-gate 		(void) strncpy(rootfs.bo_name, netdev_path, BO_MAXOBJNAME);
48307c478bd9Sstevel@tonic-gate 	}
4831843e1988Sjohnlev 	*fstypp = rootfs.bo_fstype;
4832843e1988Sjohnlev 	*fsmodp = "nfs";
48337c478bd9Sstevel@tonic-gate }
48347c478bd9Sstevel@tonic-gate #endif
4835da6c28aaSamw 
4836da6c28aaSamw /*
4837da6c28aaSamw  * VFS feature routines
4838da6c28aaSamw  */
4839da6c28aaSamw 
4840da6c28aaSamw #define	VFTINDEX(feature)	(((feature) >> 32) & 0xFFFFFFFF)
4841da6c28aaSamw #define	VFTBITS(feature)	((feature) & 0xFFFFFFFFLL)
4842da6c28aaSamw 
4843da6c28aaSamw /* Register a feature in the vfs */
4844da6c28aaSamw void
4845da6c28aaSamw vfs_set_feature(vfs_t *vfsp, vfs_feature_t feature)
4846da6c28aaSamw {
4847da6c28aaSamw 	/* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4848da6c28aaSamw 	if (vfsp->vfs_implp == NULL)
4849da6c28aaSamw 		return;
4850da6c28aaSamw 
4851da6c28aaSamw 	vfsp->vfs_featureset[VFTINDEX(feature)] |= VFTBITS(feature);
4852da6c28aaSamw }
4853da6c28aaSamw 
485444bffe01SMark Shellenbaum void
485544bffe01SMark Shellenbaum vfs_clear_feature(vfs_t *vfsp, vfs_feature_t feature)
485644bffe01SMark Shellenbaum {
485744bffe01SMark Shellenbaum 	/* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
485844bffe01SMark Shellenbaum 	if (vfsp->vfs_implp == NULL)
485944bffe01SMark Shellenbaum 		return;
486044bffe01SMark Shellenbaum 	vfsp->vfs_featureset[VFTINDEX(feature)] &= VFTBITS(~feature);
486144bffe01SMark Shellenbaum }
486244bffe01SMark Shellenbaum 
4863da6c28aaSamw /*
4864da6c28aaSamw  * Query a vfs for a feature.
4865da6c28aaSamw  * Returns 1 if feature is present, 0 if not
4866da6c28aaSamw  */
4867da6c28aaSamw int
4868da6c28aaSamw vfs_has_feature(vfs_t *vfsp, vfs_feature_t feature)
4869da6c28aaSamw {
4870da6c28aaSamw 	int	ret = 0;
4871da6c28aaSamw 
4872da6c28aaSamw 	/* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4873da6c28aaSamw 	if (vfsp->vfs_implp == NULL)
4874da6c28aaSamw 		return (ret);
4875da6c28aaSamw 
4876da6c28aaSamw 	if (vfsp->vfs_featureset[VFTINDEX(feature)] & VFTBITS(feature))
4877da6c28aaSamw 		ret = 1;
4878da6c28aaSamw 
4879da6c28aaSamw 	return (ret);
4880da6c28aaSamw }
488179a28c7aSmarks 
488279a28c7aSmarks /*
488379a28c7aSmarks  * Propagate feature set from one vfs to another
488479a28c7aSmarks  */
488579a28c7aSmarks void
488679a28c7aSmarks vfs_propagate_features(vfs_t *from, vfs_t *to)
488779a28c7aSmarks {
488879a28c7aSmarks 	int i;
488979a28c7aSmarks 
489079a28c7aSmarks 	if (to->vfs_implp == NULL || from->vfs_implp == NULL)
489179a28c7aSmarks 		return;
489279a28c7aSmarks 
489379a28c7aSmarks 	for (i = 1; i <= to->vfs_featureset[0]; i++) {
489479a28c7aSmarks 		to->vfs_featureset[i] = from->vfs_featureset[i];
489579a28c7aSmarks 	}
489679a28c7aSmarks }
489793239addSjohnlev 
48980fbb751dSJohn Levon #define	LOFINODE_PATH "/dev/lofi/%d"
489993239addSjohnlev 
490093239addSjohnlev /*
490193239addSjohnlev  * Return the vnode for the lofi node if there's a lofi mount in place.
490293239addSjohnlev  * Returns -1 when there's no lofi node, 0 on success, and > 0 on
490393239addSjohnlev  * failure.
490493239addSjohnlev  */
490593239addSjohnlev int
490693239addSjohnlev vfs_get_lofi(vfs_t *vfsp, vnode_t **vpp)
490793239addSjohnlev {
490893239addSjohnlev 	char *path = NULL;
490993239addSjohnlev 	int strsize;
491093239addSjohnlev 	int err;
491193239addSjohnlev 
491293239addSjohnlev 	if (vfsp->vfs_lofi_minor == 0) {
491393239addSjohnlev 		*vpp = NULL;
491493239addSjohnlev 		return (-1);
491593239addSjohnlev 	}
491693239addSjohnlev 
49170fbb751dSJohn Levon 	strsize = snprintf(NULL, 0, LOFINODE_PATH, vfsp->vfs_lofi_minor);
491893239addSjohnlev 	path = kmem_alloc(strsize + 1, KM_SLEEP);
49190fbb751dSJohn Levon 	(void) snprintf(path, strsize + 1, LOFINODE_PATH, vfsp->vfs_lofi_minor);
492093239addSjohnlev 
49210fbb751dSJohn Levon 	/*
49220fbb751dSJohn Levon 	 * We may be inside a zone, so we need to use the /dev path, but
49230fbb751dSJohn Levon 	 * it's created asynchronously, so we wait here.
49240fbb751dSJohn Levon 	 */
49250fbb751dSJohn Levon 	for (;;) {
492693239addSjohnlev 		err = lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, vpp);
492793239addSjohnlev 
49280fbb751dSJohn Levon 		if (err != ENOENT)
49290fbb751dSJohn Levon 			break;
49300fbb751dSJohn Levon 
49310fbb751dSJohn Levon 		if ((err = delay_sig(hz / 8)) == EINTR)
49320fbb751dSJohn Levon 			break;
49330fbb751dSJohn Levon 	}
49340fbb751dSJohn Levon 
493593239addSjohnlev 	if (err)
493693239addSjohnlev 		*vpp = NULL;
493793239addSjohnlev 
493893239addSjohnlev 	kmem_free(path, strsize + 1);
493993239addSjohnlev 	return (err);
494093239addSjohnlev }
4941