xref: /illumos-gate/usr/src/uts/common/fs/vfs.c (revision c6f039c7)
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  */
215f10ef69SYuri Pankov 
227c478bd9Sstevel@tonic-gate /*
230fbb751dSJohn Levon  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24d6294894SJohn Levon  * Copyright (c) 2019, Joyent, Inc.
25406fc510SToomas Soome  * Copyright 2016 Toomas Soome <tsoome@me.com>
26d9a54dd1SSebastien Roy  * Copyright (c) 2016, 2017 by Delphix. All rights reserved.
275f10ef69SYuri Pankov  * Copyright 2016 Nexenta Systems, Inc.
282fc78efeSAndrew Stormont  * Copyright 2017 RackTop Systems.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
32b6d49c84SToomas Soome /*	  All Rights Reserved	*/
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
367c478bd9Sstevel@tonic-gate  * The Regents of the University of California
377c478bd9Sstevel@tonic-gate  * All Rights Reserved
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
407c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
417c478bd9Sstevel@tonic-gate  * contributors.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
467c478bd9Sstevel@tonic-gate #include <sys/param.h>
477c478bd9Sstevel@tonic-gate #include <sys/errno.h>
487c478bd9Sstevel@tonic-gate #include <sys/user.h>
497c478bd9Sstevel@tonic-gate #include <sys/fstyp.h>
507c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
517c478bd9Sstevel@tonic-gate #include <sys/systm.h>
527c478bd9Sstevel@tonic-gate #include <sys/proc.h>
537c478bd9Sstevel@tonic-gate #include <sys/mount.h>
547c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
55aa59c4cbSrsb #include <sys/vfs_opreg.h>
567c478bd9Sstevel@tonic-gate #include <sys/fem.h>
577c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
587c478bd9Sstevel@tonic-gate #include <sys/stat.h>
597c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
607c478bd9Sstevel@tonic-gate #include <sys/statfs.h>
617c478bd9Sstevel@tonic-gate #include <sys/cred.h>
627c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
637c478bd9Sstevel@tonic-gate #include <sys/rwstlock.h>
647c478bd9Sstevel@tonic-gate #include <sys/dnlc.h>
657c478bd9Sstevel@tonic-gate #include <sys/file.h>
667c478bd9Sstevel@tonic-gate #include <sys/time.h>
677c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
687c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
697c478bd9Sstevel@tonic-gate #include <sys/buf.h>
707c478bd9Sstevel@tonic-gate #include <sys/swap.h>
717c478bd9Sstevel@tonic-gate #include <sys/debug.h>
727c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
737c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
747c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
757c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
767c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
777c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h>
787c478bd9Sstevel@tonic-gate #include <sys/dc_ki.h>
797c478bd9Sstevel@tonic-gate #include <sys/poll.h>
807c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
817c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
827c478bd9Sstevel@tonic-gate #include <sys/zone.h>
837c478bd9Sstevel@tonic-gate #include <sys/policy.h>
847c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
857c478bd9Sstevel@tonic-gate #include <sys/objfs.h>
867c478bd9Sstevel@tonic-gate #include <sys/console.h>
877c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
88da6c28aaSamw #include <sys/attr.h>
89b24ab676SJeff Bonwick #include <sys/zio.h>
90e7cbe64fSgw #include <sys/spa.h>
9193239addSjohnlev #include <sys/lofi.h>
926cefaae1SJack Meng #include <sys/bootprops.h>
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #include <vm/page.h>
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
9782c7f3c4Srsb /* Private interfaces to create vopstats-related data structures */
9882c7f3c4Srsb extern void		initialize_vopstats(vopstats_t *);
9982c7f3c4Srsb extern vopstats_t	*get_fstype_vopstats(struct vfs *, struct vfssw *);
10082c7f3c4Srsb extern vsk_anchor_t	*get_vskstat_anchor(struct vfs *);
10182c7f3c4Srsb 
1027c478bd9Sstevel@tonic-gate static void vfs_clearmntopt_nolock(mntopts_t *, const char *, int);
1037c478bd9Sstevel@tonic-gate static void vfs_setmntopt_nolock(mntopts_t *, const char *,
1047c478bd9Sstevel@tonic-gate     const char *, int, int);
1057c478bd9Sstevel@tonic-gate static int  vfs_optionisset_nolock(const mntopts_t *, const char *, char **);
1067c478bd9Sstevel@tonic-gate static void vfs_freemnttab(struct vfs *);
1077c478bd9Sstevel@tonic-gate static void vfs_freeopt(mntopt_t *);
1087c478bd9Sstevel@tonic-gate static void vfs_swapopttbl_nolock(mntopts_t *, mntopts_t *);
1097c478bd9Sstevel@tonic-gate static void vfs_swapopttbl(mntopts_t *, mntopts_t *);
1107c478bd9Sstevel@tonic-gate static void vfs_copyopttbl_extend(const mntopts_t *, mntopts_t *, int);
1117c478bd9Sstevel@tonic-gate static void vfs_createopttbl_extend(mntopts_t *, const char *,
1127c478bd9Sstevel@tonic-gate     const mntopts_t *);
1137c478bd9Sstevel@tonic-gate static char **vfs_copycancelopt_extend(char **const, int);
1147c478bd9Sstevel@tonic-gate static void vfs_freecancelopt(char **);
115843e1988Sjohnlev static void getrootfs(char **, char **);
1167c478bd9Sstevel@tonic-gate static int getmacpath(dev_info_t *, void *);
117df2381bfSpraks static void vfs_mnttabvp_setup(void);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate struct ipmnt {
1207c478bd9Sstevel@tonic-gate 	struct ipmnt	*mip_next;
1217c478bd9Sstevel@tonic-gate 	dev_t		mip_dev;
1227c478bd9Sstevel@tonic-gate 	struct vfs	*mip_vfsp;
1237c478bd9Sstevel@tonic-gate };
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate static kmutex_t		vfs_miplist_mutex;
1267c478bd9Sstevel@tonic-gate static struct ipmnt	*vfs_miplist = NULL;
1277c478bd9Sstevel@tonic-gate static struct ipmnt	*vfs_miplist_end = NULL;
1287c478bd9Sstevel@tonic-gate 
129da6c28aaSamw static kmem_cache_t *vfs_cache;	/* Pointer to VFS kmem cache */
130da6c28aaSamw 
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate  * VFS global data.
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate vnode_t *rootdir;		/* pointer to root inode vnode. */
1357c478bd9Sstevel@tonic-gate vnode_t *devicesdir;		/* pointer to inode of devices root */
136facf4a8dSllai vnode_t	*devdir;		/* pointer to inode of dev root */
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate char *server_rootpath;		/* root path for diskless clients */
1397c478bd9Sstevel@tonic-gate char *server_hostname;		/* hostname of diskless server */
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate static struct vfs root;
1427c478bd9Sstevel@tonic-gate static struct vfs devices;
143facf4a8dSllai static struct vfs dev;
1447c478bd9Sstevel@tonic-gate struct vfs *rootvfs = &root;	/* pointer to root vfs; head of VFS list. */
1457c478bd9Sstevel@tonic-gate rvfs_t *rvfs_list;		/* array of vfs ptrs for vfs hash list */
1467c478bd9Sstevel@tonic-gate int vfshsz = 512;		/* # of heads/locks in vfs hash arrays */
1477c478bd9Sstevel@tonic-gate 				/* must be power of 2!	*/
1487c478bd9Sstevel@tonic-gate timespec_t vfs_mnttab_ctime;	/* mnttab created time */
1497c478bd9Sstevel@tonic-gate timespec_t vfs_mnttab_mtime;	/* mnttab last modified time */
150ffc349abSdm char *vfs_dummyfstype = "\0";
1517c478bd9Sstevel@tonic-gate struct pollhead vfs_pollhd;	/* for mnttab pollers */
152df2381bfSpraks struct vnode *vfs_mntdummyvp;	/* to fake mnttab read/write for file events */
153df2381bfSpraks int	mntfstype;		/* will be set once mnt fs is mounted */
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * Table for generic options recognized in the VFS layer and acted
1577c478bd9Sstevel@tonic-gate  * on at this level before parsing file system specific options.
1587c478bd9Sstevel@tonic-gate  * The nosuid option is stronger than any of the devices and setuid
1597c478bd9Sstevel@tonic-gate  * options, so those are canceled when nosuid is seen.
1607c478bd9Sstevel@tonic-gate  *
1617c478bd9Sstevel@tonic-gate  * All options which are added here need to be added to the
1627c478bd9Sstevel@tonic-gate  * list of standard options in usr/src/cmd/fs.d/fslib.c as well.
1637c478bd9Sstevel@tonic-gate  */
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate  * VFS Mount options table
1667c478bd9Sstevel@tonic-gate  */
1677c478bd9Sstevel@tonic-gate static char *ro_cancel[] = { MNTOPT_RW, NULL };
1687c478bd9Sstevel@tonic-gate static char *rw_cancel[] = { MNTOPT_RO, NULL };
1697c478bd9Sstevel@tonic-gate static char *suid_cancel[] = { MNTOPT_NOSUID, NULL };
1707c478bd9Sstevel@tonic-gate static char *nosuid_cancel[] = { MNTOPT_SUID, MNTOPT_DEVICES, MNTOPT_NODEVICES,
1717c478bd9Sstevel@tonic-gate     MNTOPT_NOSETUID, MNTOPT_SETUID, NULL };
1727c478bd9Sstevel@tonic-gate static char *devices_cancel[] = { MNTOPT_NODEVICES, NULL };
1737c478bd9Sstevel@tonic-gate static char *nodevices_cancel[] = { MNTOPT_DEVICES, NULL };
1747c478bd9Sstevel@tonic-gate static char *setuid_cancel[] = { MNTOPT_NOSETUID, NULL };
1757c478bd9Sstevel@tonic-gate static char *nosetuid_cancel[] = { MNTOPT_SETUID, NULL };
1767c478bd9Sstevel@tonic-gate static char *nbmand_cancel[] = { MNTOPT_NONBMAND, NULL };
1777c478bd9Sstevel@tonic-gate static char *nonbmand_cancel[] = { MNTOPT_NBMAND, NULL };
1787c478bd9Sstevel@tonic-gate static char *exec_cancel[] = { MNTOPT_NOEXEC, NULL };
1797c478bd9Sstevel@tonic-gate static char *noexec_cancel[] = { MNTOPT_EXEC, NULL };
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate static const mntopt_t mntopts[] = {
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate  *	option name		cancel options		default arg	flags
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate 	{ MNTOPT_REMOUNT,	NULL,			NULL,
1867c478bd9Sstevel@tonic-gate 		MO_NODISPLAY, (void *)0 },
1877c478bd9Sstevel@tonic-gate 	{ MNTOPT_RO,		ro_cancel,		NULL,		0,
1887c478bd9Sstevel@tonic-gate 		(void *)0 },
1897c478bd9Sstevel@tonic-gate 	{ MNTOPT_RW,		rw_cancel,		NULL,		0,
1907c478bd9Sstevel@tonic-gate 		(void *)0 },
1917c478bd9Sstevel@tonic-gate 	{ MNTOPT_SUID,		suid_cancel,		NULL,		0,
1927c478bd9Sstevel@tonic-gate 		(void *)0 },
1937c478bd9Sstevel@tonic-gate 	{ MNTOPT_NOSUID,	nosuid_cancel,		NULL,		0,
1947c478bd9Sstevel@tonic-gate 		(void *)0 },
1957c478bd9Sstevel@tonic-gate 	{ MNTOPT_DEVICES,	devices_cancel,		NULL,		0,
1967c478bd9Sstevel@tonic-gate 		(void *)0 },
1977c478bd9Sstevel@tonic-gate 	{ MNTOPT_NODEVICES,	nodevices_cancel,	NULL,		0,
1987c478bd9Sstevel@tonic-gate 		(void *)0 },
1997c478bd9Sstevel@tonic-gate 	{ MNTOPT_SETUID,	setuid_cancel,		NULL,		0,
2007c478bd9Sstevel@tonic-gate 		(void *)0 },
2017c478bd9Sstevel@tonic-gate 	{ MNTOPT_NOSETUID,	nosetuid_cancel,	NULL,		0,
2027c478bd9Sstevel@tonic-gate 		(void *)0 },
2037c478bd9Sstevel@tonic-gate 	{ MNTOPT_NBMAND,	nbmand_cancel,		NULL,		0,
2047c478bd9Sstevel@tonic-gate 		(void *)0 },
2057c478bd9Sstevel@tonic-gate 	{ MNTOPT_NONBMAND,	nonbmand_cancel,	NULL,		0,
2067c478bd9Sstevel@tonic-gate 		(void *)0 },
2077c478bd9Sstevel@tonic-gate 	{ MNTOPT_EXEC,		exec_cancel,		NULL,		0,
2087c478bd9Sstevel@tonic-gate 		(void *)0 },
2097c478bd9Sstevel@tonic-gate 	{ MNTOPT_NOEXEC,	noexec_cancel,		NULL,		0,
2107c478bd9Sstevel@tonic-gate 		(void *)0 },
2117c478bd9Sstevel@tonic-gate };
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate const mntopts_t vfs_mntopts = {
2147c478bd9Sstevel@tonic-gate 	sizeof (mntopts) / sizeof (mntopt_t),
2157c478bd9Sstevel@tonic-gate 	(mntopt_t *)&mntopts[0]
2167c478bd9Sstevel@tonic-gate };
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate  * File system operation dispatch functions.
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate int
fsop_mount(vfs_t * vfsp,vnode_t * mvp,struct mounta * uap,cred_t * cr)2237c478bd9Sstevel@tonic-gate fsop_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	return (*(vfsp)->vfs_op->vfs_mount)(vfsp, mvp, uap, cr);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate int
fsop_unmount(vfs_t * vfsp,int flag,cred_t * cr)2297c478bd9Sstevel@tonic-gate fsop_unmount(vfs_t *vfsp, int flag, cred_t *cr)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	return (*(vfsp)->vfs_op->vfs_unmount)(vfsp, flag, cr);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate int
fsop_root(vfs_t * vfsp,vnode_t ** vpp)2357c478bd9Sstevel@tonic-gate fsop_root(vfs_t *vfsp, vnode_t **vpp)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	refstr_t *mntpt;
2387c478bd9Sstevel@tonic-gate 	int ret = (*(vfsp)->vfs_op->vfs_root)(vfsp, vpp);
2397c478bd9Sstevel@tonic-gate 	/*
2407c478bd9Sstevel@tonic-gate 	 * Make sure this root has a path.  With lofs, it is possible to have
2417c478bd9Sstevel@tonic-gate 	 * a NULL mountpoint.
2427c478bd9Sstevel@tonic-gate 	 */
243e2fc3408SPatrick Mooney 	if (ret == 0 && vfsp->vfs_mntpt != NULL &&
244e2fc3408SPatrick Mooney 	    (*vpp)->v_path == vn_vpath_empty) {
245e2fc3408SPatrick Mooney 		const char *path;
246e2fc3408SPatrick Mooney 
2477c478bd9Sstevel@tonic-gate 		mntpt = vfs_getmntpoint(vfsp);
248e2fc3408SPatrick Mooney 		path = refstr_value(mntpt);
249e2fc3408SPatrick Mooney 		vn_setpath_str(*vpp, path, strlen(path));
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
fsop_statfs(vfs_t * vfsp,statvfs64_t * sp)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
fsop_sync(vfs_t * vfsp,short flag,cred_t * cr)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
fsop_vget(vfs_t * vfsp,vnode_t ** vpp,fid_t * fidp)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
fsop_mountroot(vfs_t * vfsp,enum whymountroot reason)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
fsop_freefs(vfs_t * vfsp)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
fsop_vnstate(vfs_t * vfsp,vnode_t * vp,vntrans_t nstate)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
fsop_sync_by_kind(int fstype,short flag,cred_t * cr)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
fs_copyfsops(const fs_operation_def_t * template,vfsops_t * actual,int * unused_ops)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),
351b6d49c84SToomas Soome 			(fs_generic_func_p)(uintptr_t)fs_freevfs,
352b6d49c84SToomas Soome 			/* Shouldn't fail */
353b6d49c84SToomas Soome 			(fs_generic_func_p)(uintptr_t)fs_freevfs,
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 		VFSNAME_VNSTATE, offsetof(vfsops_t, vfs_vnstate),
3567c478bd9Sstevel@tonic-gate 			(fs_generic_func_p)fs_nosys,
3577c478bd9Sstevel@tonic-gate 			(fs_generic_func_p)fs_nosys,
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 		NULL, 0, NULL, NULL
3607c478bd9Sstevel@tonic-gate 	};
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	return (fs_build_vector(actual, unused_ops, vfs_ops_table, template));
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
365e7cbe64fSgw void
zfs_boot_init(void)366406fc510SToomas Soome zfs_boot_init(void)
367406fc510SToomas Soome {
368e7cbe64fSgw 	if (strcmp(rootfs.bo_fstype, MNTTYPE_ZFS) == 0)
369e7cbe64fSgw 		spa_boot_init();
370e7cbe64fSgw }
371e7cbe64fSgw 
3727c478bd9Sstevel@tonic-gate int
vfs_setfsops(int fstype,const fs_operation_def_t * template,vfsops_t ** actual)3737c478bd9Sstevel@tonic-gate vfs_setfsops(int fstype, const fs_operation_def_t *template, vfsops_t **actual)
3747c478bd9Sstevel@tonic-gate {
3757c478bd9Sstevel@tonic-gate 	int error;
3767c478bd9Sstevel@tonic-gate 	int unused_ops;
3777c478bd9Sstevel@tonic-gate 
378d320ee2dSrsb 	/*
379d320ee2dSrsb 	 * Verify that fstype refers to a valid fs.  Note that
380d320ee2dSrsb 	 * 0 is valid since it's used to set "stray" ops.
381d320ee2dSrsb 	 */
382d320ee2dSrsb 	if ((fstype < 0) || (fstype >= nfstype))
3837c478bd9Sstevel@tonic-gate 		return (EINVAL);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if (!ALLOCATED_VFSSW(&vfssw[fstype]))
3867c478bd9Sstevel@tonic-gate 		return (EINVAL);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	/* Set up the operations vector. */
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	error = fs_copyfsops(template, &vfssw[fstype].vsw_vfsops, &unused_ops);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	if (error != 0)
3937c478bd9Sstevel@tonic-gate 		return (error);
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	vfssw[fstype].vsw_flag |= VSW_INSTALLED;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	if (actual != NULL)
3987c478bd9Sstevel@tonic-gate 		*actual = &vfssw[fstype].vsw_vfsops;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate #if DEBUG
4017c478bd9Sstevel@tonic-gate 	if (unused_ops != 0)
4027c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "vfs_setfsops: %s: %d operations supplied "
4037c478bd9Sstevel@tonic-gate 		    "but not used", vfssw[fstype].vsw_name, unused_ops);
4047c478bd9Sstevel@tonic-gate #endif
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	return (0);
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate int
vfs_makefsops(const fs_operation_def_t * template,vfsops_t ** actual)4107c478bd9Sstevel@tonic-gate vfs_makefsops(const fs_operation_def_t *template, vfsops_t **actual)
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate 	int error;
4137c478bd9Sstevel@tonic-gate 	int unused_ops;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	*actual = (vfsops_t *)kmem_alloc(sizeof (vfsops_t), KM_SLEEP);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	error = fs_copyfsops(template, *actual, &unused_ops);
4187c478bd9Sstevel@tonic-gate 	if (error != 0) {
4197c478bd9Sstevel@tonic-gate 		kmem_free(*actual, sizeof (vfsops_t));
4207c478bd9Sstevel@tonic-gate 		*actual = NULL;
4217c478bd9Sstevel@tonic-gate 		return (error);
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	return (0);
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate /*
4287c478bd9Sstevel@tonic-gate  * Free a vfsops structure created as a result of vfs_makefsops().
4297c478bd9Sstevel@tonic-gate  * NOTE: For a vfsops structure initialized by vfs_setfsops(), use
4307c478bd9Sstevel@tonic-gate  * vfs_freevfsops_by_type().
4317c478bd9Sstevel@tonic-gate  */
4327c478bd9Sstevel@tonic-gate void
vfs_freevfsops(vfsops_t * vfsops)4337c478bd9Sstevel@tonic-gate vfs_freevfsops(vfsops_t *vfsops)
4347c478bd9Sstevel@tonic-gate {
4357c478bd9Sstevel@tonic-gate 	kmem_free(vfsops, sizeof (vfsops_t));
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate /*
4397c478bd9Sstevel@tonic-gate  * Since the vfsops structure is part of the vfssw table and wasn't
4407c478bd9Sstevel@tonic-gate  * really allocated, we're not really freeing anything.  We keep
4417c478bd9Sstevel@tonic-gate  * the name for consistency with vfs_freevfsops().  We do, however,
4427c478bd9Sstevel@tonic-gate  * need to take care of a little bookkeeping.
4437c478bd9Sstevel@tonic-gate  * NOTE: For a vfsops structure created by vfs_setfsops(), use
4447c478bd9Sstevel@tonic-gate  * vfs_freevfsops_by_type().
4457c478bd9Sstevel@tonic-gate  */
4467c478bd9Sstevel@tonic-gate int
vfs_freevfsops_by_type(int fstype)4477c478bd9Sstevel@tonic-gate vfs_freevfsops_by_type(int fstype)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/* Verify that fstype refers to a loaded fs (and not fsid 0). */
4517c478bd9Sstevel@tonic-gate 	if ((fstype <= 0) || (fstype >= nfstype))
4527c478bd9Sstevel@tonic-gate 		return (EINVAL);
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	WLOCK_VFSSW();
4557c478bd9Sstevel@tonic-gate 	if ((vfssw[fstype].vsw_flag & VSW_INSTALLED) == 0) {
4567c478bd9Sstevel@tonic-gate 		WUNLOCK_VFSSW();
4577c478bd9Sstevel@tonic-gate 		return (EINVAL);
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	vfssw[fstype].vsw_flag &= ~VSW_INSTALLED;
4617c478bd9Sstevel@tonic-gate 	WUNLOCK_VFSSW();
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	return (0);
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate /* Support routines used to reference vfs_op */
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate /* Set the operations vector for a vfs */
4697c478bd9Sstevel@tonic-gate void
vfs_setops(vfs_t * vfsp,vfsops_t * vfsops)4707c478bd9Sstevel@tonic-gate vfs_setops(vfs_t *vfsp, vfsops_t *vfsops)
4717c478bd9Sstevel@tonic-gate {
4727c478bd9Sstevel@tonic-gate 	vfsops_t	*op;
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	ASSERT(vfsp != NULL);
4757c478bd9Sstevel@tonic-gate 	ASSERT(vfsops != NULL);
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	op = vfsp->vfs_op;
4787c478bd9Sstevel@tonic-gate 	membar_consumer();
479da6c28aaSamw 	if (vfsp->vfs_femhead == NULL &&
48075d94465SJosef 'Jeff' Sipek 	    atomic_cas_ptr(&vfsp->vfs_op, op, vfsops) == op) {
4817c478bd9Sstevel@tonic-gate 		return;
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 	fsem_setvfsops(vfsp, vfsops);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /* Retrieve the operations vector for a vfs */
4877c478bd9Sstevel@tonic-gate vfsops_t *
vfs_getops(vfs_t * vfsp)4887c478bd9Sstevel@tonic-gate vfs_getops(vfs_t *vfsp)
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate 	vfsops_t	*op;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	ASSERT(vfsp != NULL);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	op = vfsp->vfs_op;
4957c478bd9Sstevel@tonic-gate 	membar_consumer();
496da6c28aaSamw 	if (vfsp->vfs_femhead == NULL && op == vfsp->vfs_op) {
4977c478bd9Sstevel@tonic-gate 		return (op);
4987c478bd9Sstevel@tonic-gate 	} else {
4997c478bd9Sstevel@tonic-gate 		return (fsem_getvfsops(vfsp));
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate /*
5047c478bd9Sstevel@tonic-gate  * Returns non-zero (1) if the vfsops matches that of the vfs.
5057c478bd9Sstevel@tonic-gate  * Returns zero (0) if not.
5067c478bd9Sstevel@tonic-gate  */
5077c478bd9Sstevel@tonic-gate int
vfs_matchops(vfs_t * vfsp,vfsops_t * vfsops)5087c478bd9Sstevel@tonic-gate vfs_matchops(vfs_t *vfsp, vfsops_t *vfsops)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	return (vfs_getops(vfsp) == vfsops);
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate /*
5147c478bd9Sstevel@tonic-gate  * Returns non-zero (1) if the file system has installed a non-default,
5157c478bd9Sstevel@tonic-gate  * non-error vfs_sync routine.  Returns zero (0) otherwise.
5167c478bd9Sstevel@tonic-gate  */
5177c478bd9Sstevel@tonic-gate int
vfs_can_sync(vfs_t * vfsp)5187c478bd9Sstevel@tonic-gate vfs_can_sync(vfs_t *vfsp)
5197c478bd9Sstevel@tonic-gate {
5207c478bd9Sstevel@tonic-gate 	/* vfs_sync() routine is not the default/error function */
5217c478bd9Sstevel@tonic-gate 	return (vfs_getops(vfsp)->vfs_sync != fs_sync);
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate /*
5257c478bd9Sstevel@tonic-gate  * Initialize a vfs structure.
5267c478bd9Sstevel@tonic-gate  */
5277c478bd9Sstevel@tonic-gate void
vfs_init(vfs_t * vfsp,vfsops_t * op,void * data)5287c478bd9Sstevel@tonic-gate vfs_init(vfs_t *vfsp, vfsops_t *op, void *data)
5297c478bd9Sstevel@tonic-gate {
530da6c28aaSamw 	/* Other initialization has been moved to vfs_alloc() */
5317c478bd9Sstevel@tonic-gate 	vfsp->vfs_count = 0;
5327c478bd9Sstevel@tonic-gate 	vfsp->vfs_next = vfsp;
5337c478bd9Sstevel@tonic-gate 	vfsp->vfs_prev = vfsp;
5347c478bd9Sstevel@tonic-gate 	vfsp->vfs_zone_next = vfsp;
5357c478bd9Sstevel@tonic-gate 	vfsp->vfs_zone_prev = vfsp;
536406fc510SToomas Soome 	vfsp->vfs_lofi_id = 0;
537da6c28aaSamw 	sema_init(&vfsp->vfs_reflock, 1, NULL, SEMA_DEFAULT, NULL);
538da6c28aaSamw 	vfsimpl_setup(vfsp);
5397c478bd9Sstevel@tonic-gate 	vfsp->vfs_data = (data);
5407c478bd9Sstevel@tonic-gate 	vfs_setops((vfsp), (op));
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate 
543ddfcde86Srsb /*
544ddfcde86Srsb  * Allocate and initialize the vfs implementation private data
545ddfcde86Srsb  * structure, vfs_impl_t.
546ddfcde86Srsb  */
547ddfcde86Srsb void
vfsimpl_setup(vfs_t * vfsp)548ddfcde86Srsb vfsimpl_setup(vfs_t *vfsp)
549ddfcde86Srsb {
550da6c28aaSamw 	int i;
551da6c28aaSamw 
552da6c28aaSamw 	if (vfsp->vfs_implp != NULL) {
553da6c28aaSamw 		return;
554da6c28aaSamw 	}
555da6c28aaSamw 
556ddfcde86Srsb 	vfsp->vfs_implp = kmem_alloc(sizeof (vfs_impl_t), KM_SLEEP);
557da6c28aaSamw 	/* Note that these are #define'd in vfs.h */
558ddfcde86Srsb 	vfsp->vfs_vskap = NULL;
559ddfcde86Srsb 	vfsp->vfs_fstypevsp = NULL;
560da6c28aaSamw 
561da6c28aaSamw 	/* Set size of counted array, then zero the array */
562da6c28aaSamw 	vfsp->vfs_featureset[0] = VFS_FEATURE_MAXSZ - 1;
563da6c28aaSamw 	for (i = 1; i <  VFS_FEATURE_MAXSZ; i++) {
564da6c28aaSamw 		vfsp->vfs_featureset[i] = 0;
565da6c28aaSamw 	}
566ddfcde86Srsb }
567ddfcde86Srsb 
568ddfcde86Srsb /*
569ddfcde86Srsb  * Release the vfs_impl_t structure, if it exists. Some unbundled
570ddfcde86Srsb  * filesystems may not use the newer version of vfs and thus
571ddfcde86Srsb  * would not contain this implementation private data structure.
572ddfcde86Srsb  */
573ddfcde86Srsb void
vfsimpl_teardown(vfs_t * vfsp)574ddfcde86Srsb vfsimpl_teardown(vfs_t *vfsp)
575ddfcde86Srsb {
576ddfcde86Srsb 	vfs_impl_t	*vip = vfsp->vfs_implp;
577ddfcde86Srsb 
578ddfcde86Srsb 	if (vip == NULL)
579ddfcde86Srsb 		return;
580ddfcde86Srsb 
581ddfcde86Srsb 	kmem_free(vfsp->vfs_implp, sizeof (vfs_impl_t));
582ddfcde86Srsb 	vfsp->vfs_implp = NULL;
583ddfcde86Srsb }
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate /*
5867c478bd9Sstevel@tonic-gate  * VFS system calls: mount, umount, syssync, statfs, fstatfs, statvfs,
5877c478bd9Sstevel@tonic-gate  * fstatvfs, and sysfs moved to common/syscall.
5887c478bd9Sstevel@tonic-gate  */
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate /*
5917c478bd9Sstevel@tonic-gate  * Update every mounted file system.  We call the vfs_sync operation of
5927c478bd9Sstevel@tonic-gate  * each file system type, passing it a NULL vfsp to indicate that all
5937c478bd9Sstevel@tonic-gate  * mounted file systems of that type should be updated.
5947c478bd9Sstevel@tonic-gate  */
5957c478bd9Sstevel@tonic-gate void
vfs_sync(int flag)5967c478bd9Sstevel@tonic-gate vfs_sync(int flag)
5977c478bd9Sstevel@tonic-gate {
5987c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
5997c478bd9Sstevel@tonic-gate 	RLOCK_VFSSW();
6007c478bd9Sstevel@tonic-gate 	for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
6017c478bd9Sstevel@tonic-gate 		if (ALLOCATED_VFSSW(vswp) && VFS_INSTALLED(vswp)) {
6027c478bd9Sstevel@tonic-gate 			vfs_refvfssw(vswp);
6037c478bd9Sstevel@tonic-gate 			RUNLOCK_VFSSW();
6047c478bd9Sstevel@tonic-gate 			(void) (*vswp->vsw_vfsops.vfs_sync)(NULL, flag,
6057c478bd9Sstevel@tonic-gate 			    CRED());
6067c478bd9Sstevel@tonic-gate 			vfs_unrefvfssw(vswp);
6077c478bd9Sstevel@tonic-gate 			RLOCK_VFSSW();
6087c478bd9Sstevel@tonic-gate 		}
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 	RUNLOCK_VFSSW();
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate void
sync(void)6147c478bd9Sstevel@tonic-gate sync(void)
6157c478bd9Sstevel@tonic-gate {
6167c478bd9Sstevel@tonic-gate 	vfs_sync(0);
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate /*
6207c478bd9Sstevel@tonic-gate  * External routines.
6217c478bd9Sstevel@tonic-gate  */
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate krwlock_t vfssw_lock;	/* lock accesses to vfssw */
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate /*
6267c478bd9Sstevel@tonic-gate  * Lock for accessing the vfs linked list.  Initialized in vfs_mountroot(),
6277c478bd9Sstevel@tonic-gate  * but otherwise should be accessed only via vfs_list_lock() and
6287c478bd9Sstevel@tonic-gate  * vfs_list_unlock().  Also used to protect the timestamp for mods to the list.
6297c478bd9Sstevel@tonic-gate  */
6307c478bd9Sstevel@tonic-gate static krwlock_t vfslist;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate /*
6337c478bd9Sstevel@tonic-gate  * Mount devfs on /devices. This is done right after root is mounted
6347c478bd9Sstevel@tonic-gate  * to provide device access support for the system
6357c478bd9Sstevel@tonic-gate  */
6367c478bd9Sstevel@tonic-gate static void
vfs_mountdevices(void)6377c478bd9Sstevel@tonic-gate vfs_mountdevices(void)
6387c478bd9Sstevel@tonic-gate {
6397c478bd9Sstevel@tonic-gate 	struct vfssw *vsw;
6407c478bd9Sstevel@tonic-gate 	struct vnode *mvp;
6417c478bd9Sstevel@tonic-gate 	struct mounta mounta = {	/* fake mounta for devfs_mount() */
6427c478bd9Sstevel@tonic-gate 		NULL,
6437c478bd9Sstevel@tonic-gate 		NULL,
6447c478bd9Sstevel@tonic-gate 		MS_SYSSPACE,
6457c478bd9Sstevel@tonic-gate 		NULL,
6467c478bd9Sstevel@tonic-gate 		NULL,
6477c478bd9Sstevel@tonic-gate 		0,
6487c478bd9Sstevel@tonic-gate 		NULL,
6497c478bd9Sstevel@tonic-gate 		0
6507c478bd9Sstevel@tonic-gate 	};
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	/*
6537c478bd9Sstevel@tonic-gate 	 * _init devfs module to fill in the vfssw
6547c478bd9Sstevel@tonic-gate 	 */
6557c478bd9Sstevel@tonic-gate 	if (modload("fs", "devfs") == -1)
656ae115bc7Smrj 		panic("Cannot _init devfs module");
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	/*
6597c478bd9Sstevel@tonic-gate 	 * Hold vfs
6607c478bd9Sstevel@tonic-gate 	 */
6617c478bd9Sstevel@tonic-gate 	RLOCK_VFSSW();
6627c478bd9Sstevel@tonic-gate 	vsw = vfs_getvfsswbyname("devfs");
6637c478bd9Sstevel@tonic-gate 	VFS_INIT(&devices, &vsw->vsw_vfsops, NULL);
6647c478bd9Sstevel@tonic-gate 	VFS_HOLD(&devices);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	/*
6677c478bd9Sstevel@tonic-gate 	 * Locate mount point
6687c478bd9Sstevel@tonic-gate 	 */
6697c478bd9Sstevel@tonic-gate 	if (lookupname("/devices", UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp))
670ae115bc7Smrj 		panic("Cannot find /devices");
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	/*
6737c478bd9Sstevel@tonic-gate 	 * Perform the mount of /devices
6747c478bd9Sstevel@tonic-gate 	 */
6757c478bd9Sstevel@tonic-gate 	if (VFS_MOUNT(&devices, mvp, &mounta, CRED()))
676ae115bc7Smrj 		panic("Cannot mount /devices");
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	RUNLOCK_VFSSW();
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	/*
6817c478bd9Sstevel@tonic-gate 	 * Set appropriate members and add to vfs list for mnttab display
6827c478bd9Sstevel@tonic-gate 	 */
683d7de0ceaSRobert Harris 	vfs_setresource(&devices, "/devices", 0);
684d7de0ceaSRobert Harris 	vfs_setmntpoint(&devices, "/devices", 0);
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/*
6877c478bd9Sstevel@tonic-gate 	 * Hold the root of /devices so it won't go away
6887c478bd9Sstevel@tonic-gate 	 */
6897c478bd9Sstevel@tonic-gate 	if (VFS_ROOT(&devices, &devicesdir))
690ae115bc7Smrj 		panic("vfs_mountdevices: not devices root");
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	if (vfs_lock(&devices) != 0) {
693facf4a8dSllai 		VN_RELE(devicesdir);
6947c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Cannot acquire vfs_lock of /devices");
6957c478bd9Sstevel@tonic-gate 		return;
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	if (vn_vfswlock(mvp) != 0) {
6997c478bd9Sstevel@tonic-gate 		vfs_unlock(&devices);
700facf4a8dSllai 		VN_RELE(devicesdir);
7017c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Cannot acquire vfswlock of /devices");
7027c478bd9Sstevel@tonic-gate 		return;
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	vfs_add(mvp, &devices, 0);
7067c478bd9Sstevel@tonic-gate 	vn_vfsunlock(mvp);
7077c478bd9Sstevel@tonic-gate 	vfs_unlock(&devices);
708facf4a8dSllai 	VN_RELE(devicesdir);
709facf4a8dSllai }
710facf4a8dSllai 
711facf4a8dSllai /*
712facf4a8dSllai  * mount the first instance of /dev  to root and remain mounted
713facf4a8dSllai  */
714facf4a8dSllai static void
vfs_mountdev1(void)715facf4a8dSllai vfs_mountdev1(void)
716facf4a8dSllai {
717facf4a8dSllai 	struct vfssw *vsw;
718facf4a8dSllai 	struct vnode *mvp;
719facf4a8dSllai 	struct mounta mounta = {	/* fake mounta for sdev_mount() */
720facf4a8dSllai 		NULL,
721facf4a8dSllai 		NULL,
722facf4a8dSllai 		MS_SYSSPACE | MS_OVERLAY,
723facf4a8dSllai 		NULL,
724facf4a8dSllai 		NULL,
725facf4a8dSllai 		0,
726facf4a8dSllai 		NULL,
727facf4a8dSllai 		0
728facf4a8dSllai 	};
729facf4a8dSllai 
730facf4a8dSllai 	/*
731facf4a8dSllai 	 * _init dev module to fill in the vfssw
732facf4a8dSllai 	 */
733facf4a8dSllai 	if (modload("fs", "dev") == -1)
734facf4a8dSllai 		cmn_err(CE_PANIC, "Cannot _init dev module\n");
735facf4a8dSllai 
736facf4a8dSllai 	/*
737facf4a8dSllai 	 * Hold vfs
738facf4a8dSllai 	 */
739facf4a8dSllai 	RLOCK_VFSSW();
740facf4a8dSllai 	vsw = vfs_getvfsswbyname("dev");
741facf4a8dSllai 	VFS_INIT(&dev, &vsw->vsw_vfsops, NULL);
742facf4a8dSllai 	VFS_HOLD(&dev);
743facf4a8dSllai 
744facf4a8dSllai 	/*
745facf4a8dSllai 	 * Locate mount point
746facf4a8dSllai 	 */
747facf4a8dSllai 	if (lookupname("/dev", UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp))
748facf4a8dSllai 		cmn_err(CE_PANIC, "Cannot find /dev\n");
749facf4a8dSllai 
750facf4a8dSllai 	/*
751facf4a8dSllai 	 * Perform the mount of /dev
752facf4a8dSllai 	 */
753facf4a8dSllai 	if (VFS_MOUNT(&dev, mvp, &mounta, CRED()))
754facf4a8dSllai 		cmn_err(CE_PANIC, "Cannot mount /dev 1\n");
755facf4a8dSllai 
756facf4a8dSllai 	RUNLOCK_VFSSW();
757facf4a8dSllai 
758facf4a8dSllai 	/*
759facf4a8dSllai 	 * Set appropriate members and add to vfs list for mnttab display
760facf4a8dSllai 	 */
761d7de0ceaSRobert Harris 	vfs_setresource(&dev, "/dev", 0);
762d7de0ceaSRobert Harris 	vfs_setmntpoint(&dev, "/dev", 0);
763facf4a8dSllai 
764facf4a8dSllai 	/*
765facf4a8dSllai 	 * Hold the root of /dev so it won't go away
766facf4a8dSllai 	 */
767facf4a8dSllai 	if (VFS_ROOT(&dev, &devdir))
768facf4a8dSllai 		cmn_err(CE_PANIC, "vfs_mountdev1: not dev root");
769facf4a8dSllai 
770facf4a8dSllai 	if (vfs_lock(&dev) != 0) {
771facf4a8dSllai 		VN_RELE(devdir);
772facf4a8dSllai 		cmn_err(CE_NOTE, "Cannot acquire vfs_lock of /dev");
773facf4a8dSllai 		return;
774facf4a8dSllai 	}
775facf4a8dSllai 
776facf4a8dSllai 	if (vn_vfswlock(mvp) != 0) {
777facf4a8dSllai 		vfs_unlock(&dev);
778facf4a8dSllai 		VN_RELE(devdir);
779facf4a8dSllai 		cmn_err(CE_NOTE, "Cannot acquire vfswlock of /dev");
780facf4a8dSllai 		return;
781facf4a8dSllai 	}
782facf4a8dSllai 
783facf4a8dSllai 	vfs_add(mvp, &dev, 0);
784facf4a8dSllai 	vn_vfsunlock(mvp);
785facf4a8dSllai 	vfs_unlock(&dev);
786facf4a8dSllai 	VN_RELE(devdir);
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate /*
7907c478bd9Sstevel@tonic-gate  * Mount required filesystem. This is done right after root is mounted.
7917c478bd9Sstevel@tonic-gate  */
7927c478bd9Sstevel@tonic-gate static void
vfs_mountfs(char * module,char * spec,char * path)7937c478bd9Sstevel@tonic-gate vfs_mountfs(char *module, char *spec, char *path)
7947c478bd9Sstevel@tonic-gate {
7957c478bd9Sstevel@tonic-gate 	struct vnode *mvp;
7967c478bd9Sstevel@tonic-gate 	struct mounta mounta;
7977c478bd9Sstevel@tonic-gate 	vfs_t *vfsp;
7987c478bd9Sstevel@tonic-gate 
7992fc78efeSAndrew Stormont 	bzero(&mounta, sizeof (mounta));
8007c478bd9Sstevel@tonic-gate 	mounta.flags = MS_SYSSPACE | MS_DATA;
8017c478bd9Sstevel@tonic-gate 	mounta.fstype = module;
8027c478bd9Sstevel@tonic-gate 	mounta.spec = spec;
8037c478bd9Sstevel@tonic-gate 	mounta.dir = path;
8047c478bd9Sstevel@tonic-gate 	if (lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp)) {
805ae115bc7Smrj 		cmn_err(CE_WARN, "Cannot find %s", path);
8067c478bd9Sstevel@tonic-gate 		return;
8077c478bd9Sstevel@tonic-gate 	}
8087c478bd9Sstevel@tonic-gate 	if (domount(NULL, &mounta, mvp, CRED(), &vfsp))
809ae115bc7Smrj 		cmn_err(CE_WARN, "Cannot mount %s", path);
8107c478bd9Sstevel@tonic-gate 	else
8117c478bd9Sstevel@tonic-gate 		VFS_RELE(vfsp);
8127c478bd9Sstevel@tonic-gate 	VN_RELE(mvp);
8137c478bd9Sstevel@tonic-gate }
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate /*
8167c478bd9Sstevel@tonic-gate  * vfs_mountroot is called by main() to mount the root filesystem.
8177c478bd9Sstevel@tonic-gate  */
8187c478bd9Sstevel@tonic-gate void
vfs_mountroot(void)8197c478bd9Sstevel@tonic-gate vfs_mountroot(void)
8207c478bd9Sstevel@tonic-gate {
8217c478bd9Sstevel@tonic-gate 	struct vnode	*rvp = NULL;
8227c478bd9Sstevel@tonic-gate 	char		*path;
8237c478bd9Sstevel@tonic-gate 	size_t		plen;
8245a59a8b3Srsb 	struct vfssw	*vswp;
82535a5a358SJonathan Adams 	proc_t		*p;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	rw_init(&vfssw_lock, NULL, RW_DEFAULT, NULL);
8287c478bd9Sstevel@tonic-gate 	rw_init(&vfslist, NULL, RW_DEFAULT, NULL);
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	/*
8317c478bd9Sstevel@tonic-gate 	 * Alloc the vfs hash bucket array and locks
8327c478bd9Sstevel@tonic-gate 	 */
8337c478bd9Sstevel@tonic-gate 	rvfs_list = kmem_zalloc(vfshsz * sizeof (rvfs_t), KM_SLEEP);
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	/*
8367c478bd9Sstevel@tonic-gate 	 * Call machine-dependent routine "rootconf" to choose a root
8377c478bd9Sstevel@tonic-gate 	 * file system type.
8387c478bd9Sstevel@tonic-gate 	 */
8397c478bd9Sstevel@tonic-gate 	if (rootconf())
840ae115bc7Smrj 		panic("vfs_mountroot: cannot mount root");
8417c478bd9Sstevel@tonic-gate 	/*
8427c478bd9Sstevel@tonic-gate 	 * Get vnode for '/'.  Set up rootdir, u.u_rdir and u.u_cdir
8437c478bd9Sstevel@tonic-gate 	 * to point to it.  These are used by lookuppn() so that it
8447c478bd9Sstevel@tonic-gate 	 * knows where to start from ('/' or '.').
8457c478bd9Sstevel@tonic-gate 	 */
846d7de0ceaSRobert Harris 	vfs_setmntpoint(rootvfs, "/", 0);
8477c478bd9Sstevel@tonic-gate 	if (VFS_ROOT(rootvfs, &rootdir))
848ae115bc7Smrj 		panic("vfs_mountroot: no root vnode");
84935a5a358SJonathan Adams 
85035a5a358SJonathan Adams 	/*
85135a5a358SJonathan Adams 	 * At this point, the process tree consists of p0 and possibly some
85235a5a358SJonathan Adams 	 * direct children of p0.  (i.e. there are no grandchildren)
85335a5a358SJonathan Adams 	 *
85435a5a358SJonathan Adams 	 * Walk through them all, setting their current directory.
85535a5a358SJonathan Adams 	 */
85635a5a358SJonathan Adams 	mutex_enter(&pidlock);
85735a5a358SJonathan Adams 	for (p = practive; p != NULL; p = p->p_next) {
85835a5a358SJonathan Adams 		ASSERT(p == &p0 || p->p_parent == &p0);
85935a5a358SJonathan Adams 
86035a5a358SJonathan Adams 		PTOU(p)->u_cdir = rootdir;
86135a5a358SJonathan Adams 		VN_HOLD(PTOU(p)->u_cdir);
86235a5a358SJonathan Adams 		PTOU(p)->u_rdir = NULL;
86335a5a358SJonathan Adams 	}
86435a5a358SJonathan Adams 	mutex_exit(&pidlock);
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	/*
8677c478bd9Sstevel@tonic-gate 	 * Setup the global zone's rootvp, now that it exists.
8687c478bd9Sstevel@tonic-gate 	 */
8697c478bd9Sstevel@tonic-gate 	global_zone->zone_rootvp = rootdir;
8707c478bd9Sstevel@tonic-gate 	VN_HOLD(global_zone->zone_rootvp);
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	/*
8737c478bd9Sstevel@tonic-gate 	 * Notify the module code that it can begin using the
8747c478bd9Sstevel@tonic-gate 	 * root filesystem instead of the boot program's services.
8757c478bd9Sstevel@tonic-gate 	 */
8767c478bd9Sstevel@tonic-gate 	modrootloaded = 1;
877e7cbe64fSgw 
878e7cbe64fSgw 	/*
879e7cbe64fSgw 	 * Special handling for a ZFS root file system.
880e7cbe64fSgw 	 */
881e7cbe64fSgw 	zfs_boot_init();
882e7cbe64fSgw 
8837c478bd9Sstevel@tonic-gate 	/*
8847c478bd9Sstevel@tonic-gate 	 * Set up mnttab information for root
8857c478bd9Sstevel@tonic-gate 	 */
886d7de0ceaSRobert Harris 	vfs_setresource(rootvfs, rootfs.bo_name, 0);
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	/*
8897c478bd9Sstevel@tonic-gate 	 * Notify cluster software that the root filesystem is available.
8907c478bd9Sstevel@tonic-gate 	 */
8917c478bd9Sstevel@tonic-gate 	clboot_mountroot();
8927c478bd9Sstevel@tonic-gate 
8935a59a8b3Srsb 	/* Now that we're all done with the root FS, set up its vopstats */
8945a59a8b3Srsb 	if ((vswp = vfs_getvfsswbyvfsops(vfs_getops(rootvfs))) != NULL) {
8955a59a8b3Srsb 		/* Set flag for statistics collection */
8965a59a8b3Srsb 		if (vswp->vsw_flag & VSW_STATS) {
89782c7f3c4Srsb 			initialize_vopstats(&rootvfs->vfs_vopstats);
8985a59a8b3Srsb 			rootvfs->vfs_flag |= VFS_STATS;
89982c7f3c4Srsb 			rootvfs->vfs_fstypevsp =
90082c7f3c4Srsb 			    get_fstype_vopstats(rootvfs, vswp);
90182c7f3c4Srsb 			rootvfs->vfs_vskap = get_vskstat_anchor(rootvfs);
9025a59a8b3Srsb 		}
9035a59a8b3Srsb 		vfs_unrefvfssw(vswp);
9045a59a8b3Srsb 	}
9055a59a8b3Srsb 
9067c478bd9Sstevel@tonic-gate 	/*
907facf4a8dSllai 	 * Mount /devices, /dev instance 1, /system/contract, /etc/mnttab,
908a237e38eSth 	 * /etc/svc/volatile, /etc/dfs/sharetab, /system/object, and /proc.
9097c478bd9Sstevel@tonic-gate 	 */
9107c478bd9Sstevel@tonic-gate 	vfs_mountdevices();
911facf4a8dSllai 	vfs_mountdev1();
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	vfs_mountfs("ctfs", "ctfs", CTFS_ROOT);
9147c478bd9Sstevel@tonic-gate 	vfs_mountfs("proc", "/proc", "/proc");
9157c478bd9Sstevel@tonic-gate 	vfs_mountfs("mntfs", "/etc/mnttab", "/etc/mnttab");
9167c478bd9Sstevel@tonic-gate 	vfs_mountfs("tmpfs", "/etc/svc/volatile", "/etc/svc/volatile");
9177c478bd9Sstevel@tonic-gate 	vfs_mountfs("objfs", "objfs", OBJFS_ROOT);
91876ca3cb0SRobert Mustacchi 	vfs_mountfs("bootfs", "bootfs", "/system/boot");
9197c478bd9Sstevel@tonic-gate 
920a237e38eSth 	if (getzoneid() == GLOBAL_ZONEID) {
921a237e38eSth 		vfs_mountfs("sharefs", "sharefs", "/etc/dfs/sharetab");
922a237e38eSth 	}
923a237e38eSth 
924bde2df36SYuri Pankov 	if (strcmp(rootfs.bo_fstype, "zfs") != 0) {
925bde2df36SYuri Pankov 		/*
926bde2df36SYuri Pankov 		 * Look up the root device via devfs so that a dv_node is
927bde2df36SYuri Pankov 		 * created for it. The vnode is never VN_RELE()ed.
928bde2df36SYuri Pankov 		 * We allocate more than MAXPATHLEN so that the
929bde2df36SYuri Pankov 		 * buffer passed to i_ddi_prompath_to_devfspath() is
930bde2df36SYuri Pankov 		 * exactly MAXPATHLEN (the function expects a buffer
931bde2df36SYuri Pankov 		 * of that length).
932bde2df36SYuri Pankov 		 */
933bde2df36SYuri Pankov 		plen = strlen("/devices");
934bde2df36SYuri Pankov 		path = kmem_alloc(plen + MAXPATHLEN, KM_SLEEP);
935bde2df36SYuri Pankov 		(void) strcpy(path, "/devices");
9367c478bd9Sstevel@tonic-gate 
937bde2df36SYuri Pankov 		if (i_ddi_prompath_to_devfspath(rootfs.bo_name, path + plen)
938bde2df36SYuri Pankov 		    != DDI_SUCCESS ||
939bde2df36SYuri Pankov 		    lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &rvp)) {
9407c478bd9Sstevel@tonic-gate 
941bde2df36SYuri Pankov 			/* NUL terminate in case "path" has garbage */
942bde2df36SYuri Pankov 			path[plen + MAXPATHLEN - 1] = '\0';
9437c478bd9Sstevel@tonic-gate #ifdef	DEBUG
944bde2df36SYuri Pankov 			cmn_err(CE_WARN, "!Cannot lookup root device: %s",
945bde2df36SYuri Pankov 			    path);
9467c478bd9Sstevel@tonic-gate #endif
947bde2df36SYuri Pankov 		}
948bde2df36SYuri Pankov 		kmem_free(path, plen + MAXPATHLEN);
9497c478bd9Sstevel@tonic-gate 	}
950bde2df36SYuri Pankov 
951df2381bfSpraks 	vfs_mnttabvp_setup();
9527c478bd9Sstevel@tonic-gate }
9537c478bd9Sstevel@tonic-gate 
95493239addSjohnlev /*
95593239addSjohnlev  * Check to see if our "block device" is actually a file.  If so,
95693239addSjohnlev  * automatically add a lofi device, and keep track of this fact.
95793239addSjohnlev  */
95893239addSjohnlev static int
lofi_add(const char * fsname,struct vfs * vfsp,mntopts_t * mntopts,struct mounta * uap)95993239addSjohnlev lofi_add(const char *fsname, struct vfs *vfsp,
96093239addSjohnlev     mntopts_t *mntopts, struct mounta *uap)
96193239addSjohnlev {
96293239addSjohnlev 	int fromspace = (uap->flags & MS_SYSSPACE) ?
96393239addSjohnlev 	    UIO_SYSSPACE : UIO_USERSPACE;
96493239addSjohnlev 	struct lofi_ioctl *li = NULL;
96593239addSjohnlev 	struct vnode *vp = NULL;
96693239addSjohnlev 	struct pathname	pn = { NULL };
96793239addSjohnlev 	ldi_ident_t ldi_id;
96893239addSjohnlev 	ldi_handle_t ldi_hdl;
96943d5cd3dSjohnlev 	vfssw_t *vfssw;
970406fc510SToomas Soome 	int id;
97193239addSjohnlev 	int err = 0;
97293239addSjohnlev 
9730fbb751dSJohn Levon 	if ((vfssw = vfs_getvfssw(fsname)) == NULL)
97493239addSjohnlev 		return (0);
97543d5cd3dSjohnlev 
97643d5cd3dSjohnlev 	if (!(vfssw->vsw_flag & VSW_CANLOFI)) {
97743d5cd3dSjohnlev 		vfs_unrefvfssw(vfssw);
97893239addSjohnlev 		return (0);
97943d5cd3dSjohnlev 	}
98043d5cd3dSjohnlev 
98143d5cd3dSjohnlev 	vfs_unrefvfssw(vfssw);
98243d5cd3dSjohnlev 	vfssw = NULL;
98393239addSjohnlev 
98493239addSjohnlev 	if (pn_get(uap->spec, fromspace, &pn) != 0)
98593239addSjohnlev 		return (0);
98693239addSjohnlev 
98793239addSjohnlev 	if (lookupname(uap->spec, fromspace, FOLLOW, NULL, &vp) != 0)
98893239addSjohnlev 		goto out;
98993239addSjohnlev 
99093239addSjohnlev 	if (vp->v_type != VREG)
99193239addSjohnlev 		goto out;
99293239addSjohnlev 
99393239addSjohnlev 	/* OK, this is a lofi mount. */
99493239addSjohnlev 
99593239addSjohnlev 	if ((uap->flags & (MS_REMOUNT|MS_GLOBAL)) ||
99693239addSjohnlev 	    vfs_optionisset_nolock(mntopts, MNTOPT_SUID, NULL) ||
99793239addSjohnlev 	    vfs_optionisset_nolock(mntopts, MNTOPT_SETUID, NULL) ||
99893239addSjohnlev 	    vfs_optionisset_nolock(mntopts, MNTOPT_DEVICES, NULL)) {
99993239addSjohnlev 		err = EINVAL;
100093239addSjohnlev 		goto out;
100193239addSjohnlev 	}
100293239addSjohnlev 
100393239addSjohnlev 	ldi_id = ldi_ident_from_anon();
100493239addSjohnlev 	li = kmem_zalloc(sizeof (*li), KM_SLEEP);
10056f02aa44SDina K Nimeh 	(void) strlcpy(li->li_filename, pn.pn_path, MAXPATHLEN);
100693239addSjohnlev 
10070fbb751dSJohn Levon 	err = ldi_open_by_name("/dev/lofictl", FREAD | FWRITE, kcred,
10080fbb751dSJohn Levon 	    &ldi_hdl, ldi_id);
100993239addSjohnlev 
101093239addSjohnlev 	if (err)
101193239addSjohnlev 		goto out2;
101293239addSjohnlev 
101393239addSjohnlev 	err = ldi_ioctl(ldi_hdl, LOFI_MAP_FILE, (intptr_t)li,
1014406fc510SToomas Soome 	    FREAD | FWRITE | FKIOCTL, kcred, &id);
101593239addSjohnlev 
10160fbb751dSJohn Levon 	(void) ldi_close(ldi_hdl, FREAD | FWRITE, kcred);
101793239addSjohnlev 
101893239addSjohnlev 	if (!err)
1019406fc510SToomas Soome 		vfsp->vfs_lofi_id = id;
102093239addSjohnlev 
102193239addSjohnlev out2:
102293239addSjohnlev 	ldi_ident_release(ldi_id);
102393239addSjohnlev out:
102493239addSjohnlev 	if (li != NULL)
102593239addSjohnlev 		kmem_free(li, sizeof (*li));
102693239addSjohnlev 	if (vp != NULL)
102793239addSjohnlev 		VN_RELE(vp);
102893239addSjohnlev 	pn_free(&pn);
102993239addSjohnlev 	return (err);
103093239addSjohnlev }
103193239addSjohnlev 
103293239addSjohnlev static void
lofi_remove(struct vfs * vfsp)103393239addSjohnlev lofi_remove(struct vfs *vfsp)
103493239addSjohnlev {
1035d6294894SJohn Levon 	struct lofi_ioctl *li;
103693239addSjohnlev 	ldi_ident_t ldi_id;
103793239addSjohnlev 	ldi_handle_t ldi_hdl;
103893239addSjohnlev 	int err;
103993239addSjohnlev 
1040406fc510SToomas Soome 	if (vfsp->vfs_lofi_id == 0)
104193239addSjohnlev 		return;
104293239addSjohnlev 
104393239addSjohnlev 	ldi_id = ldi_ident_from_anon();
104493239addSjohnlev 
104593239addSjohnlev 	li = kmem_zalloc(sizeof (*li), KM_SLEEP);
1046406fc510SToomas Soome 	li->li_id = vfsp->vfs_lofi_id;
104793239addSjohnlev 	li->li_cleanup = B_TRUE;
104893239addSjohnlev 
10490fbb751dSJohn Levon 	err = ldi_open_by_name("/dev/lofictl", FREAD | FWRITE, kcred,
10500fbb751dSJohn Levon 	    &ldi_hdl, ldi_id);
105193239addSjohnlev 
105293239addSjohnlev 	if (err)
105393239addSjohnlev 		goto out;
105493239addSjohnlev 
105593239addSjohnlev 	err = ldi_ioctl(ldi_hdl, LOFI_UNMAP_FILE_MINOR, (intptr_t)li,
10560fbb751dSJohn Levon 	    FREAD | FWRITE | FKIOCTL, kcred, NULL);
105793239addSjohnlev 
10580fbb751dSJohn Levon 	(void) ldi_close(ldi_hdl, FREAD | FWRITE, kcred);
105993239addSjohnlev 
106093239addSjohnlev 	if (!err)
1061406fc510SToomas Soome 		vfsp->vfs_lofi_id = 0;
106293239addSjohnlev 
106393239addSjohnlev out:
106493239addSjohnlev 	ldi_ident_release(ldi_id);
1065d6294894SJohn Levon 	kmem_free(li, sizeof (*li));
106693239addSjohnlev }
106793239addSjohnlev 
10687c478bd9Sstevel@tonic-gate /*
10697c478bd9Sstevel@tonic-gate  * Common mount code.  Called from the system call entry point, from autofs,
1070b9238976Sth  * nfsv4 trigger mounts, and from pxfs.
10717c478bd9Sstevel@tonic-gate  *
10727c478bd9Sstevel@tonic-gate  * Takes the effective file system type, mount arguments, the mount point
10737c478bd9Sstevel@tonic-gate  * vnode, flags specifying whether the mount is a remount and whether it
10747c478bd9Sstevel@tonic-gate  * should be entered into the vfs list, and credentials.  Fills in its vfspp
10757c478bd9Sstevel@tonic-gate  * parameter with the mounted file system instance's vfs.
10767c478bd9Sstevel@tonic-gate  *
10777c478bd9Sstevel@tonic-gate  * Note that the effective file system type is specified as a string.  It may
10787c478bd9Sstevel@tonic-gate  * be null, in which case it's determined from the mount arguments, and may
10797c478bd9Sstevel@tonic-gate  * differ from the type specified in the mount arguments; this is a hook to
10807c478bd9Sstevel@tonic-gate  * allow interposition when instantiating file system instances.
10817c478bd9Sstevel@tonic-gate  *
10827c478bd9Sstevel@tonic-gate  * The caller is responsible for releasing its own hold on the mount point
10837c478bd9Sstevel@tonic-gate  * vp (this routine does its own hold when necessary).
10847c478bd9Sstevel@tonic-gate  * Also note that for remounts, the mount point vp should be the vnode for
10857c478bd9Sstevel@tonic-gate  * the root of the file system rather than the vnode that the file system
10867c478bd9Sstevel@tonic-gate  * is mounted on top of.
10877c478bd9Sstevel@tonic-gate  */
10887c478bd9Sstevel@tonic-gate int
domount(char * fsname,struct mounta * uap,vnode_t * vp,struct cred * credp,struct vfs ** vfspp)10897c478bd9Sstevel@tonic-gate domount(char *fsname, struct mounta *uap, vnode_t *vp, struct cred *credp,
1090406fc510SToomas Soome     struct vfs **vfspp)
10917c478bd9Sstevel@tonic-gate {
10927c478bd9Sstevel@tonic-gate 	struct vfssw	*vswp;
10937c478bd9Sstevel@tonic-gate 	vfsops_t	*vfsops;
10947c478bd9Sstevel@tonic-gate 	struct vfs	*vfsp;
10957c478bd9Sstevel@tonic-gate 	struct vnode	*bvp;
10967c478bd9Sstevel@tonic-gate 	dev_t		bdev = 0;
10977c478bd9Sstevel@tonic-gate 	mntopts_t	mnt_mntopts;
10987c478bd9Sstevel@tonic-gate 	int		error = 0;
10997c478bd9Sstevel@tonic-gate 	int		copyout_error = 0;
1100*c6f039c7SToomas Soome 	int		ovflags = 0;
11017c478bd9Sstevel@tonic-gate 	char		*opts = uap->optptr;
11027c478bd9Sstevel@tonic-gate 	char		*inargs = opts;
11037c478bd9Sstevel@tonic-gate 	int		optlen = uap->optlen;
11047c478bd9Sstevel@tonic-gate 	int		remount;
11057c478bd9Sstevel@tonic-gate 	int		rdonly;
11067c478bd9Sstevel@tonic-gate 	int		nbmand = 0;
11077c478bd9Sstevel@tonic-gate 	int		delmip = 0;
11087c478bd9Sstevel@tonic-gate 	int		addmip = 0;
11097c478bd9Sstevel@tonic-gate 	int		splice = ((uap->flags & MS_NOSPLICE) == 0);
11107c478bd9Sstevel@tonic-gate 	int		fromspace = (uap->flags & MS_SYSSPACE) ?
1111b1b8ab34Slling 	    UIO_SYSSPACE : UIO_USERSPACE;
11127c478bd9Sstevel@tonic-gate 	char		*resource = NULL, *mountpt = NULL;
11137c478bd9Sstevel@tonic-gate 	refstr_t	*oldresource, *oldmntpt;
11147c478bd9Sstevel@tonic-gate 	struct pathname	pn, rpn;
111582c7f3c4Srsb 	vsk_anchor_t	*vskap;
111693239addSjohnlev 	char fstname[FSTYPSZ];
11175fd5c689SJerry Jelinek 	zone_t		*zone;
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	/*
11207c478bd9Sstevel@tonic-gate 	 * The v_flag value for the mount point vp is permanently set
11217c478bd9Sstevel@tonic-gate 	 * to VVFSLOCK so that no one bypasses the vn_vfs*locks routine
11227c478bd9Sstevel@tonic-gate 	 * for mount point locking.
11237c478bd9Sstevel@tonic-gate 	 */
11247c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
11257c478bd9Sstevel@tonic-gate 	vp->v_flag |= VVFSLOCK;
11267c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	mnt_mntopts.mo_count = 0;
11297c478bd9Sstevel@tonic-gate 	/*
11307c478bd9Sstevel@tonic-gate 	 * Find the ops vector to use to invoke the file system-specific mount
11317c478bd9Sstevel@tonic-gate 	 * method.  If the fsname argument is non-NULL, use it directly.
11327c478bd9Sstevel@tonic-gate 	 * Otherwise, dig the file system type information out of the mount
11337c478bd9Sstevel@tonic-gate 	 * arguments.
11347c478bd9Sstevel@tonic-gate 	 *
11357c478bd9Sstevel@tonic-gate 	 * A side effect is to hold the vfssw entry.
11367c478bd9Sstevel@tonic-gate 	 *
11377c478bd9Sstevel@tonic-gate 	 * Mount arguments can be specified in several ways, which are
11387c478bd9Sstevel@tonic-gate 	 * distinguished by flag bit settings.  The preferred way is to set
11397c478bd9Sstevel@tonic-gate 	 * MS_OPTIONSTR, indicating an 8 argument mount with the file system
11407c478bd9Sstevel@tonic-gate 	 * type supplied as a character string and the last two arguments
11417c478bd9Sstevel@tonic-gate 	 * being a pointer to a character buffer and the size of the buffer.
11427c478bd9Sstevel@tonic-gate 	 * On entry, the buffer holds a null terminated list of options; on
11437c478bd9Sstevel@tonic-gate 	 * return, the string is the list of options the file system
11447c478bd9Sstevel@tonic-gate 	 * recognized. If MS_DATA is set arguments five and six point to a
11457c478bd9Sstevel@tonic-gate 	 * block of binary data which the file system interprets.
11467c478bd9Sstevel@tonic-gate 	 * A further wrinkle is that some callers don't set MS_FSS and MS_DATA
11477c478bd9Sstevel@tonic-gate 	 * consistently with these conventions.  To handle them, we check to
11487c478bd9Sstevel@tonic-gate 	 * see whether the pointer to the file system name has a numeric value
11497c478bd9Sstevel@tonic-gate 	 * less than 256.  If so, we treat it as an index.
11507c478bd9Sstevel@tonic-gate 	 */
11517c478bd9Sstevel@tonic-gate 	if (fsname != NULL) {
11527c478bd9Sstevel@tonic-gate 		if ((vswp = vfs_getvfssw(fsname)) == NULL) {
11537c478bd9Sstevel@tonic-gate 			return (EINVAL);
11547c478bd9Sstevel@tonic-gate 		}
11557c478bd9Sstevel@tonic-gate 	} else if (uap->flags & (MS_OPTIONSTR | MS_DATA | MS_FSS)) {
11567c478bd9Sstevel@tonic-gate 		size_t n;
11577c478bd9Sstevel@tonic-gate 		uint_t fstype;
115893239addSjohnlev 
115993239addSjohnlev 		fsname = fstname;
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 		if ((fstype = (uintptr_t)uap->fstype) < 256) {
11627c478bd9Sstevel@tonic-gate 			RLOCK_VFSSW();
11637c478bd9Sstevel@tonic-gate 			if (fstype == 0 || fstype >= nfstype ||
11647c478bd9Sstevel@tonic-gate 			    !ALLOCATED_VFSSW(&vfssw[fstype])) {
11657c478bd9Sstevel@tonic-gate 				RUNLOCK_VFSSW();
11667c478bd9Sstevel@tonic-gate 				return (EINVAL);
11677c478bd9Sstevel@tonic-gate 			}
116893239addSjohnlev 			(void) strcpy(fsname, vfssw[fstype].vsw_name);
11697c478bd9Sstevel@tonic-gate 			RUNLOCK_VFSSW();
117093239addSjohnlev 			if ((vswp = vfs_getvfssw(fsname)) == NULL)
11717c478bd9Sstevel@tonic-gate 				return (EINVAL);
11727c478bd9Sstevel@tonic-gate 		} else {
11737c478bd9Sstevel@tonic-gate 			/*
11747c478bd9Sstevel@tonic-gate 			 * Handle either kernel or user address space.
11757c478bd9Sstevel@tonic-gate 			 */
11767c478bd9Sstevel@tonic-gate 			if (uap->flags & MS_SYSSPACE) {
117793239addSjohnlev 				error = copystr(uap->fstype, fsname,
11787c478bd9Sstevel@tonic-gate 				    FSTYPSZ, &n);
11797c478bd9Sstevel@tonic-gate 			} else {
118093239addSjohnlev 				error = copyinstr(uap->fstype, fsname,
11817c478bd9Sstevel@tonic-gate 				    FSTYPSZ, &n);
11827c478bd9Sstevel@tonic-gate 			}
11837c478bd9Sstevel@tonic-gate 			if (error) {
11847c478bd9Sstevel@tonic-gate 				if (error == ENAMETOOLONG)
11857c478bd9Sstevel@tonic-gate 					return (EINVAL);
11867c478bd9Sstevel@tonic-gate 				return (error);
11877c478bd9Sstevel@tonic-gate 			}
118893239addSjohnlev 			if ((vswp = vfs_getvfssw(fsname)) == NULL)
11897c478bd9Sstevel@tonic-gate 				return (EINVAL);
11907c478bd9Sstevel@tonic-gate 		}
11917c478bd9Sstevel@tonic-gate 	} else {
11927c478bd9Sstevel@tonic-gate 		if ((vswp = vfs_getvfsswbyvfsops(vfs_getops(rootvfs))) == NULL)
11937c478bd9Sstevel@tonic-gate 			return (EINVAL);
11940fbb751dSJohn Levon 		fsname = vswp->vsw_name;
11957c478bd9Sstevel@tonic-gate 	}
11967c478bd9Sstevel@tonic-gate 	if (!VFS_INSTALLED(vswp))
11977c478bd9Sstevel@tonic-gate 		return (EINVAL);
11980fbb751dSJohn Levon 
11990fbb751dSJohn Levon 	if ((error = secpolicy_fs_allowed_mount(fsname)) != 0)  {
12000fbb751dSJohn Levon 		vfs_unrefvfssw(vswp);
12010fbb751dSJohn Levon 		return (error);
12020fbb751dSJohn Levon 	}
12030fbb751dSJohn Levon 
12047c478bd9Sstevel@tonic-gate 	vfsops = &vswp->vsw_vfsops;
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	vfs_copyopttbl(&vswp->vsw_optproto, &mnt_mntopts);
12077c478bd9Sstevel@tonic-gate 	/*
12087c478bd9Sstevel@tonic-gate 	 * Fetch mount options and parse them for generic vfs options
12097c478bd9Sstevel@tonic-gate 	 */
12107c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_OPTIONSTR) {
12117c478bd9Sstevel@tonic-gate 		/*
12127c478bd9Sstevel@tonic-gate 		 * Limit the buffer size
12137c478bd9Sstevel@tonic-gate 		 */
12147c478bd9Sstevel@tonic-gate 		if (optlen < 0 || optlen > MAX_MNTOPT_STR) {
12157c478bd9Sstevel@tonic-gate 			error = EINVAL;
12167c478bd9Sstevel@tonic-gate 			goto errout;
12177c478bd9Sstevel@tonic-gate 		}
12187c478bd9Sstevel@tonic-gate 		if ((uap->flags & MS_SYSSPACE) == 0) {
12197c478bd9Sstevel@tonic-gate 			inargs = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
12207c478bd9Sstevel@tonic-gate 			inargs[0] = '\0';
12217c478bd9Sstevel@tonic-gate 			if (optlen) {
12227c478bd9Sstevel@tonic-gate 				error = copyinstr(opts, inargs, (size_t)optlen,
1223b1b8ab34Slling 				    NULL);
12247c478bd9Sstevel@tonic-gate 				if (error) {
12257c478bd9Sstevel@tonic-gate 					goto errout;
12267c478bd9Sstevel@tonic-gate 				}
12277c478bd9Sstevel@tonic-gate 			}
12287c478bd9Sstevel@tonic-gate 		}
12297c478bd9Sstevel@tonic-gate 		vfs_parsemntopts(&mnt_mntopts, inargs, 0);
12307c478bd9Sstevel@tonic-gate 	}
12317c478bd9Sstevel@tonic-gate 	/*
12327c478bd9Sstevel@tonic-gate 	 * Flag bits override the options string.
12337c478bd9Sstevel@tonic-gate 	 */
12347c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_REMOUNT)
12357c478bd9Sstevel@tonic-gate 		vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_REMOUNT, NULL, 0, 0);
12367c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_RDONLY)
12377c478bd9Sstevel@tonic-gate 		vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_RO, NULL, 0, 0);
12387c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_NOSUID)
12397c478bd9Sstevel@tonic-gate 		vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL, 0, 0);
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	/*
12427c478bd9Sstevel@tonic-gate 	 * Check if this is a remount; must be set in the option string and
12437c478bd9Sstevel@tonic-gate 	 * the file system must support a remount option.
12447c478bd9Sstevel@tonic-gate 	 */
12457c478bd9Sstevel@tonic-gate 	if (remount = vfs_optionisset_nolock(&mnt_mntopts,
12467c478bd9Sstevel@tonic-gate 	    MNTOPT_REMOUNT, NULL)) {
12477c478bd9Sstevel@tonic-gate 		if (!(vswp->vsw_flag & VSW_CANREMOUNT)) {
12487c478bd9Sstevel@tonic-gate 			error = ENOTSUP;
12497c478bd9Sstevel@tonic-gate 			goto errout;
12507c478bd9Sstevel@tonic-gate 		}
12517c478bd9Sstevel@tonic-gate 		uap->flags |= MS_REMOUNT;
12527c478bd9Sstevel@tonic-gate 	}
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	/*
12557c478bd9Sstevel@tonic-gate 	 * uap->flags and vfs_optionisset() should agree.
12567c478bd9Sstevel@tonic-gate 	 */
12577c478bd9Sstevel@tonic-gate 	if (rdonly = vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_RO, NULL)) {
12587c478bd9Sstevel@tonic-gate 		uap->flags |= MS_RDONLY;
12597c478bd9Sstevel@tonic-gate 	}
12607c478bd9Sstevel@tonic-gate 	if (vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL)) {
12617c478bd9Sstevel@tonic-gate 		uap->flags |= MS_NOSUID;
12627c478bd9Sstevel@tonic-gate 	}
12637c478bd9Sstevel@tonic-gate 	nbmand = vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_NBMAND, NULL);
12647c478bd9Sstevel@tonic-gate 	ASSERT(splice || !remount);
12657c478bd9Sstevel@tonic-gate 	/*
12667c478bd9Sstevel@tonic-gate 	 * If we are splicing the fs into the namespace,
12677c478bd9Sstevel@tonic-gate 	 * perform mount point checks.
12687c478bd9Sstevel@tonic-gate 	 *
12697c478bd9Sstevel@tonic-gate 	 * We want to resolve the path for the mount point to eliminate
12707c478bd9Sstevel@tonic-gate 	 * '.' and ".." and symlinks in mount points; we can't do the
12717c478bd9Sstevel@tonic-gate 	 * same for the resource string, since it would turn
12727c478bd9Sstevel@tonic-gate 	 * "/dev/dsk/c0t0d0s0" into "/devices/pci@...".  We need to do
12737c478bd9Sstevel@tonic-gate 	 * this before grabbing vn_vfswlock(), because otherwise we
12747c478bd9Sstevel@tonic-gate 	 * would deadlock with lookuppn().
12757c478bd9Sstevel@tonic-gate 	 */
12767c478bd9Sstevel@tonic-gate 	if (splice) {
12777c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_count > 0);
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 		/*
12807c478bd9Sstevel@tonic-gate 		 * Pick up mount point and device from appropriate space.
12817c478bd9Sstevel@tonic-gate 		 */
12827c478bd9Sstevel@tonic-gate 		if (pn_get(uap->spec, fromspace, &pn) == 0) {
12837c478bd9Sstevel@tonic-gate 			resource = kmem_alloc(pn.pn_pathlen + 1,
12847c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
12857c478bd9Sstevel@tonic-gate 			(void) strcpy(resource, pn.pn_path);
12867c478bd9Sstevel@tonic-gate 			pn_free(&pn);
12877c478bd9Sstevel@tonic-gate 		}
12887c478bd9Sstevel@tonic-gate 		/*
12897c478bd9Sstevel@tonic-gate 		 * Do a lookupname prior to taking the
12907c478bd9Sstevel@tonic-gate 		 * writelock. Mark this as completed if
12917c478bd9Sstevel@tonic-gate 		 * successful for later cleanup and addition to
12927c478bd9Sstevel@tonic-gate 		 * the mount in progress table.
12937c478bd9Sstevel@tonic-gate 		 */
1294d9a54dd1SSebastien Roy 		if ((vswp->vsw_flag & VSW_MOUNTDEV) &&
1295d9a54dd1SSebastien Roy 		    (uap->flags & MS_GLOBAL) == 0 &&
12967c478bd9Sstevel@tonic-gate 		    lookupname(uap->spec, fromspace,
1297b1b8ab34Slling 		    FOLLOW, NULL, &bvp) == 0) {
12987c478bd9Sstevel@tonic-gate 			addmip = 1;
12997c478bd9Sstevel@tonic-gate 		}
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 		if ((error = pn_get(uap->dir, fromspace, &pn)) == 0) {
13027c478bd9Sstevel@tonic-gate 			pathname_t *pnp;
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 			if (*pn.pn_path != '/') {
13057c478bd9Sstevel@tonic-gate 				error = EINVAL;
13067c478bd9Sstevel@tonic-gate 				pn_free(&pn);
13077c478bd9Sstevel@tonic-gate 				goto errout;
13087c478bd9Sstevel@tonic-gate 			}
13097c478bd9Sstevel@tonic-gate 			pn_alloc(&rpn);
13107c478bd9Sstevel@tonic-gate 			/*
13117c478bd9Sstevel@tonic-gate 			 * Kludge to prevent autofs from deadlocking with
13127c478bd9Sstevel@tonic-gate 			 * itself when it calls domount().
13137c478bd9Sstevel@tonic-gate 			 *
13147c478bd9Sstevel@tonic-gate 			 * If autofs is calling, it is because it is doing
13157c478bd9Sstevel@tonic-gate 			 * (autofs) mounts in the process of an NFS mount.  A
13167c478bd9Sstevel@tonic-gate 			 * lookuppn() here would cause us to block waiting for
13177c478bd9Sstevel@tonic-gate 			 * said NFS mount to complete, which can't since this
13187c478bd9Sstevel@tonic-gate 			 * is the thread that was supposed to doing it.
13197c478bd9Sstevel@tonic-gate 			 */
13207c478bd9Sstevel@tonic-gate 			if (fromspace == UIO_USERSPACE) {
13217c478bd9Sstevel@tonic-gate 				if ((error = lookuppn(&pn, &rpn, FOLLOW, NULL,
13227c478bd9Sstevel@tonic-gate 				    NULL)) == 0) {
13237c478bd9Sstevel@tonic-gate 					pnp = &rpn;
13247c478bd9Sstevel@tonic-gate 				} else {
13257c478bd9Sstevel@tonic-gate 					/*
13267c478bd9Sstevel@tonic-gate 					 * The file disappeared or otherwise
13277c478bd9Sstevel@tonic-gate 					 * became inaccessible since we opened
13287c478bd9Sstevel@tonic-gate 					 * it; might as well fail the mount
13297c478bd9Sstevel@tonic-gate 					 * since the mount point is no longer
13307c478bd9Sstevel@tonic-gate 					 * accessible.
13317c478bd9Sstevel@tonic-gate 					 */
13327c478bd9Sstevel@tonic-gate 					pn_free(&rpn);
13337c478bd9Sstevel@tonic-gate 					pn_free(&pn);
13347c478bd9Sstevel@tonic-gate 					goto errout;
13357c478bd9Sstevel@tonic-gate 				}
13367c478bd9Sstevel@tonic-gate 			} else {
13377c478bd9Sstevel@tonic-gate 				pnp = &pn;
13387c478bd9Sstevel@tonic-gate 			}
13397c478bd9Sstevel@tonic-gate 			mountpt = kmem_alloc(pnp->pn_pathlen + 1, KM_SLEEP);
13407c478bd9Sstevel@tonic-gate 			(void) strcpy(mountpt, pnp->pn_path);
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 			/*
13437c478bd9Sstevel@tonic-gate 			 * If the addition of the zone's rootpath
13447c478bd9Sstevel@tonic-gate 			 * would push us over a total path length
13457c478bd9Sstevel@tonic-gate 			 * of MAXPATHLEN, we fail the mount with
13467c478bd9Sstevel@tonic-gate 			 * ENAMETOOLONG, which is what we would have
13477c478bd9Sstevel@tonic-gate 			 * gotten if we were trying to perform the same
13487c478bd9Sstevel@tonic-gate 			 * mount in the global zone.
13497c478bd9Sstevel@tonic-gate 			 *
13507c478bd9Sstevel@tonic-gate 			 * strlen() doesn't count the trailing
13517c478bd9Sstevel@tonic-gate 			 * '\0', but zone_rootpathlen counts both a
13527c478bd9Sstevel@tonic-gate 			 * trailing '/' and the terminating '\0'.
13537c478bd9Sstevel@tonic-gate 			 */
13547c478bd9Sstevel@tonic-gate 			if ((curproc->p_zone->zone_rootpathlen - 1 +
13557c478bd9Sstevel@tonic-gate 			    strlen(mountpt)) > MAXPATHLEN ||
13567c478bd9Sstevel@tonic-gate 			    (resource != NULL &&
13577c478bd9Sstevel@tonic-gate 			    (curproc->p_zone->zone_rootpathlen - 1 +
13587c478bd9Sstevel@tonic-gate 			    strlen(resource)) > MAXPATHLEN)) {
13597c478bd9Sstevel@tonic-gate 				error = ENAMETOOLONG;
13607c478bd9Sstevel@tonic-gate 			}
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 			pn_free(&rpn);
13637c478bd9Sstevel@tonic-gate 			pn_free(&pn);
13647c478bd9Sstevel@tonic-gate 		}
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 		if (error)
13677c478bd9Sstevel@tonic-gate 			goto errout;
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 		/*
13707c478bd9Sstevel@tonic-gate 		 * Prevent path name resolution from proceeding past
13717c478bd9Sstevel@tonic-gate 		 * the mount point.
13727c478bd9Sstevel@tonic-gate 		 */
13737c478bd9Sstevel@tonic-gate 		if (vn_vfswlock(vp) != 0) {
13747c478bd9Sstevel@tonic-gate 			error = EBUSY;
13757c478bd9Sstevel@tonic-gate 			goto errout;
13767c478bd9Sstevel@tonic-gate 		}
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 		/*
13797c478bd9Sstevel@tonic-gate 		 * Verify that it's legitimate to establish a mount on
13807c478bd9Sstevel@tonic-gate 		 * the prospective mount point.
13817c478bd9Sstevel@tonic-gate 		 */
13827c478bd9Sstevel@tonic-gate 		if (vn_mountedvfs(vp) != NULL) {
13837c478bd9Sstevel@tonic-gate 			/*
13847c478bd9Sstevel@tonic-gate 			 * The mount point lock was obtained after some
13857c478bd9Sstevel@tonic-gate 			 * other thread raced through and established a mount.
13867c478bd9Sstevel@tonic-gate 			 */
13877c478bd9Sstevel@tonic-gate 			vn_vfsunlock(vp);
13887c478bd9Sstevel@tonic-gate 			error = EBUSY;
13897c478bd9Sstevel@tonic-gate 			goto errout;
13907c478bd9Sstevel@tonic-gate 		}
13917c478bd9Sstevel@tonic-gate 		if (vp->v_flag & VNOMOUNT) {
13927c478bd9Sstevel@tonic-gate 			vn_vfsunlock(vp);
13937c478bd9Sstevel@tonic-gate 			error = EINVAL;
13947c478bd9Sstevel@tonic-gate 			goto errout;
13957c478bd9Sstevel@tonic-gate 		}
13967c478bd9Sstevel@tonic-gate 	}
13977c478bd9Sstevel@tonic-gate 	if ((uap->flags & (MS_DATA | MS_OPTIONSTR)) == 0) {
13987c478bd9Sstevel@tonic-gate 		uap->dataptr = NULL;
13997c478bd9Sstevel@tonic-gate 		uap->datalen = 0;
14007c478bd9Sstevel@tonic-gate 	}
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	/*
14037c478bd9Sstevel@tonic-gate 	 * If this is a remount, we don't want to create a new VFS.
14047c478bd9Sstevel@tonic-gate 	 * Instead, we pass the existing one with a remount flag.
14057c478bd9Sstevel@tonic-gate 	 */
14067c478bd9Sstevel@tonic-gate 	if (remount) {
14077c478bd9Sstevel@tonic-gate 		/*
14087c478bd9Sstevel@tonic-gate 		 * Confirm that the mount point is the root vnode of the
14097c478bd9Sstevel@tonic-gate 		 * file system that is being remounted.
14107c478bd9Sstevel@tonic-gate 		 * This can happen if the user specifies a different
14117c478bd9Sstevel@tonic-gate 		 * mount point directory pathname in the (re)mount command.
14127c478bd9Sstevel@tonic-gate 		 *
14137c478bd9Sstevel@tonic-gate 		 * Code below can only be reached if splice is true, so it's
14147c478bd9Sstevel@tonic-gate 		 * safe to do vn_vfsunlock() here.
14157c478bd9Sstevel@tonic-gate 		 */
14167c478bd9Sstevel@tonic-gate 		if ((vp->v_flag & VROOT) == 0) {
14177c478bd9Sstevel@tonic-gate 			vn_vfsunlock(vp);
14187c478bd9Sstevel@tonic-gate 			error = ENOENT;
14197c478bd9Sstevel@tonic-gate 			goto errout;
14207c478bd9Sstevel@tonic-gate 		}
14217c478bd9Sstevel@tonic-gate 		/*
14227c478bd9Sstevel@tonic-gate 		 * Disallow making file systems read-only unless file system
14237c478bd9Sstevel@tonic-gate 		 * explicitly allows it in its vfssw.  Ignore other flags.
14247c478bd9Sstevel@tonic-gate 		 */
14257c478bd9Sstevel@tonic-gate 		if (rdonly && vn_is_readonly(vp) == 0 &&
14267c478bd9Sstevel@tonic-gate 		    (vswp->vsw_flag & VSW_CANRWRO) == 0) {
14277c478bd9Sstevel@tonic-gate 			vn_vfsunlock(vp);
14287c478bd9Sstevel@tonic-gate 			error = EINVAL;
14297c478bd9Sstevel@tonic-gate 			goto errout;
14307c478bd9Sstevel@tonic-gate 		}
14317c478bd9Sstevel@tonic-gate 		/*
1432da6c28aaSamw 		 * Disallow changing the NBMAND disposition of the file
1433da6c28aaSamw 		 * system on remounts.
14347c478bd9Sstevel@tonic-gate 		 */
14357c478bd9Sstevel@tonic-gate 		if ((nbmand && ((vp->v_vfsp->vfs_flag & VFS_NBMAND) == 0)) ||
14367c478bd9Sstevel@tonic-gate 		    (!nbmand && (vp->v_vfsp->vfs_flag & VFS_NBMAND))) {
1437da6c28aaSamw 			vn_vfsunlock(vp);
1438da6c28aaSamw 			error = EINVAL;
1439da6c28aaSamw 			goto errout;
14407c478bd9Sstevel@tonic-gate 		}
14417c478bd9Sstevel@tonic-gate 		vfsp = vp->v_vfsp;
14427c478bd9Sstevel@tonic-gate 		ovflags = vfsp->vfs_flag;
14437c478bd9Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_REMOUNT;
14447c478bd9Sstevel@tonic-gate 		vfsp->vfs_flag &= ~VFS_RDONLY;
14457c478bd9Sstevel@tonic-gate 	} else {
1446da6c28aaSamw 		vfsp = vfs_alloc(KM_SLEEP);
14477c478bd9Sstevel@tonic-gate 		VFS_INIT(vfsp, vfsops, NULL);
14487c478bd9Sstevel@tonic-gate 	}
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 	VFS_HOLD(vfsp);
14517c478bd9Sstevel@tonic-gate 
145293239addSjohnlev 	if ((error = lofi_add(fsname, vfsp, &mnt_mntopts, uap)) != 0) {
145393239addSjohnlev 		if (!remount) {
145493239addSjohnlev 			if (splice)
145593239addSjohnlev 				vn_vfsunlock(vp);
145693239addSjohnlev 			vfs_free(vfsp);
145793239addSjohnlev 		} else {
145893239addSjohnlev 			vn_vfsunlock(vp);
145993239addSjohnlev 			VFS_RELE(vfsp);
146093239addSjohnlev 		}
146193239addSjohnlev 		goto errout;
146293239addSjohnlev 	}
146393239addSjohnlev 
146493239addSjohnlev 	/*
146593239addSjohnlev 	 * PRIV_SYS_MOUNT doesn't mean you can become root.
146693239addSjohnlev 	 */
1467406fc510SToomas Soome 	if (vfsp->vfs_lofi_id != 0) {
146893239addSjohnlev 		uap->flags |= MS_NOSUID;
146993239addSjohnlev 		vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL, 0, 0);
147093239addSjohnlev 	}
147193239addSjohnlev 
14727c478bd9Sstevel@tonic-gate 	/*
14737c478bd9Sstevel@tonic-gate 	 * The vfs_reflock is not used anymore the code below explicitly
14747c478bd9Sstevel@tonic-gate 	 * holds it preventing others accesing it directly.
14757c478bd9Sstevel@tonic-gate 	 */
14767c478bd9Sstevel@tonic-gate 	if ((sema_tryp(&vfsp->vfs_reflock) == 0) &&
14777c478bd9Sstevel@tonic-gate 	    !(vfsp->vfs_flag & VFS_REMOUNT))
14787c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
1479ae115bc7Smrj 		    "mount type %s couldn't get vfs_reflock", vswp->vsw_name);
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 	/*
14827c478bd9Sstevel@tonic-gate 	 * Lock the vfs. If this is a remount we want to avoid spurious umount
14837c478bd9Sstevel@tonic-gate 	 * failures that happen as a side-effect of fsflush() and other mount
14847c478bd9Sstevel@tonic-gate 	 * and unmount operations that might be going on simultaneously and
14857c478bd9Sstevel@tonic-gate 	 * may have locked the vfs currently. To not return EBUSY immediately
14867c478bd9Sstevel@tonic-gate 	 * here we use vfs_lock_wait() instead vfs_lock() for the remount case.
14877c478bd9Sstevel@tonic-gate 	 */
14887c478bd9Sstevel@tonic-gate 	if (!remount) {
14897c478bd9Sstevel@tonic-gate 		if (error = vfs_lock(vfsp)) {
149093239addSjohnlev 			lofi_remove(vfsp);
149193239addSjohnlev 
14927c478bd9Sstevel@tonic-gate 			if (splice)
14937c478bd9Sstevel@tonic-gate 				vn_vfsunlock(vp);
1494da6c28aaSamw 			vfs_free(vfsp);
14957c478bd9Sstevel@tonic-gate 			goto errout;
14967c478bd9Sstevel@tonic-gate 		}
14977c478bd9Sstevel@tonic-gate 	} else {
14987c478bd9Sstevel@tonic-gate 		vfs_lock_wait(vfsp);
14997c478bd9Sstevel@tonic-gate 	}
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	/*
15027c478bd9Sstevel@tonic-gate 	 * Add device to mount in progress table, global mounts require special
15037c478bd9Sstevel@tonic-gate 	 * handling. It is possible that we have already done the lookupname
15047c478bd9Sstevel@tonic-gate 	 * on a spliced, non-global fs. If so, we don't want to do it again
15057c478bd9Sstevel@tonic-gate 	 * since we cannot do a lookupname after taking the
15067c478bd9Sstevel@tonic-gate 	 * wlock above. This case is for a non-spliced, non-global filesystem.
15077c478bd9Sstevel@tonic-gate 	 */
15087c478bd9Sstevel@tonic-gate 	if (!addmip) {
1509d9a54dd1SSebastien Roy 		if ((vswp->vsw_flag & VSW_MOUNTDEV) &&
1510d9a54dd1SSebastien Roy 		    (uap->flags & MS_GLOBAL) == 0 &&
1511b1b8ab34Slling 		    lookupname(uap->spec, fromspace, FOLLOW, NULL, &bvp) == 0) {
15127c478bd9Sstevel@tonic-gate 			addmip = 1;
15137c478bd9Sstevel@tonic-gate 		}
15147c478bd9Sstevel@tonic-gate 	}
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	if (addmip) {
151793239addSjohnlev 		vnode_t *lvp = NULL;
151893239addSjohnlev 
151993239addSjohnlev 		error = vfs_get_lofi(vfsp, &lvp);
152093239addSjohnlev 		if (error > 0) {
152193239addSjohnlev 			lofi_remove(vfsp);
152293239addSjohnlev 
152393239addSjohnlev 			if (splice)
152493239addSjohnlev 				vn_vfsunlock(vp);
152593239addSjohnlev 			vfs_unlock(vfsp);
152693239addSjohnlev 
152793239addSjohnlev 			if (remount) {
152893239addSjohnlev 				VFS_RELE(vfsp);
152993239addSjohnlev 			} else {
153093239addSjohnlev 				vfs_free(vfsp);
153193239addSjohnlev 			}
153293239addSjohnlev 
153393239addSjohnlev 			goto errout;
153493239addSjohnlev 		} else if (error == -1) {
153593239addSjohnlev 			bdev = bvp->v_rdev;
153693239addSjohnlev 			VN_RELE(bvp);
153793239addSjohnlev 		} else {
153893239addSjohnlev 			bdev = lvp->v_rdev;
153993239addSjohnlev 			VN_RELE(lvp);
154093239addSjohnlev 			VN_RELE(bvp);
154193239addSjohnlev 		}
154293239addSjohnlev 
15437c478bd9Sstevel@tonic-gate 		vfs_addmip(bdev, vfsp);
15447c478bd9Sstevel@tonic-gate 		addmip = 0;
15457c478bd9Sstevel@tonic-gate 		delmip = 1;
15467c478bd9Sstevel@tonic-gate 	}
15477c478bd9Sstevel@tonic-gate 	/*
15487c478bd9Sstevel@tonic-gate 	 * Invalidate cached entry for the mount point.
15497c478bd9Sstevel@tonic-gate 	 */
15507c478bd9Sstevel@tonic-gate 	if (splice)
15517c478bd9Sstevel@tonic-gate 		dnlc_purge_vp(vp);
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 	/*
15547c478bd9Sstevel@tonic-gate 	 * If have an option string but the filesystem doesn't supply a
15557c478bd9Sstevel@tonic-gate 	 * prototype options table, create a table with the global
15567c478bd9Sstevel@tonic-gate 	 * options and sufficient room to accept all the options in the
15577c478bd9Sstevel@tonic-gate 	 * string.  Then parse the passed in option string
15587c478bd9Sstevel@tonic-gate 	 * accepting all the options in the string.  This gives us an
15597c478bd9Sstevel@tonic-gate 	 * option table with all the proper cancel properties for the
15607c478bd9Sstevel@tonic-gate 	 * global options.
15617c478bd9Sstevel@tonic-gate 	 *
15627c478bd9Sstevel@tonic-gate 	 * Filesystems that supply a prototype options table are handled
15637c478bd9Sstevel@tonic-gate 	 * earlier in this function.
15647c478bd9Sstevel@tonic-gate 	 */
15657c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_OPTIONSTR) {
15667c478bd9Sstevel@tonic-gate 		if (!(vswp->vsw_flag & VSW_HASPROTO)) {
15677c478bd9Sstevel@tonic-gate 			mntopts_t tmp_mntopts;
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 			tmp_mntopts.mo_count = 0;
15707c478bd9Sstevel@tonic-gate 			vfs_createopttbl_extend(&tmp_mntopts, inargs,
15717c478bd9Sstevel@tonic-gate 			    &mnt_mntopts);
15727c478bd9Sstevel@tonic-gate 			vfs_parsemntopts(&tmp_mntopts, inargs, 1);
15737c478bd9Sstevel@tonic-gate 			vfs_swapopttbl_nolock(&mnt_mntopts, &tmp_mntopts);
15747c478bd9Sstevel@tonic-gate 			vfs_freeopttbl(&tmp_mntopts);
15757c478bd9Sstevel@tonic-gate 		}
15767c478bd9Sstevel@tonic-gate 	}
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	/*
15795fd5c689SJerry Jelinek 	 * Serialize with zone state transitions.
15805fd5c689SJerry Jelinek 	 * See vfs_list_add; zone mounted into is:
1581b6d49c84SToomas Soome 	 *	zone_find_by_path(refstr_value(vfsp->vfs_mntpt))
15825fd5c689SJerry Jelinek 	 * not the zone doing the mount (curproc->p_zone), but if we're already
15835fd5c689SJerry Jelinek 	 * inside a NGZ, then we know what zone we are.
15847c478bd9Sstevel@tonic-gate 	 */
15855fd5c689SJerry Jelinek 	if (INGLOBALZONE(curproc)) {
15865fd5c689SJerry Jelinek 		zone = zone_find_by_path(mountpt);
15875fd5c689SJerry Jelinek 		ASSERT(zone != NULL);
15885fd5c689SJerry Jelinek 	} else {
15895fd5c689SJerry Jelinek 		zone = curproc->p_zone;
15905fd5c689SJerry Jelinek 		/*
15915fd5c689SJerry Jelinek 		 * zone_find_by_path does a hold, so do one here too so that
15925fd5c689SJerry Jelinek 		 * we can do a zone_rele after mount_completed.
15935fd5c689SJerry Jelinek 		 */
15945fd5c689SJerry Jelinek 		zone_hold(zone);
15955fd5c689SJerry Jelinek 	}
15965fd5c689SJerry Jelinek 	mount_in_progress(zone);
15977c478bd9Sstevel@tonic-gate 	/*
15987c478bd9Sstevel@tonic-gate 	 * Instantiate (or reinstantiate) the file system.  If appropriate,
15997c478bd9Sstevel@tonic-gate 	 * splice it into the file system name space.
16007c478bd9Sstevel@tonic-gate 	 *
16017c478bd9Sstevel@tonic-gate 	 * We want VFS_MOUNT() to be able to override the vfs_resource
16027c478bd9Sstevel@tonic-gate 	 * string if necessary (ie, mntfs), and also for a remount to
16037c478bd9Sstevel@tonic-gate 	 * change the same (necessary when remounting '/' during boot).
16047c478bd9Sstevel@tonic-gate 	 * So we set up vfs_mntpt and vfs_resource to what we think they
16057c478bd9Sstevel@tonic-gate 	 * should be, then hand off control to VFS_MOUNT() which can
16067c478bd9Sstevel@tonic-gate 	 * override this.
16077c478bd9Sstevel@tonic-gate 	 *
16087c478bd9Sstevel@tonic-gate 	 * For safety's sake, when changing vfs_resource or vfs_mntpt of
16097c478bd9Sstevel@tonic-gate 	 * a vfs which is on the vfs list (i.e. during a remount), we must
16107c478bd9Sstevel@tonic-gate 	 * never set those fields to NULL. Several bits of code make
16117c478bd9Sstevel@tonic-gate 	 * assumptions that the fields are always valid.
16127c478bd9Sstevel@tonic-gate 	 */
16137c478bd9Sstevel@tonic-gate 	vfs_swapopttbl(&mnt_mntopts, &vfsp->vfs_mntopts);
16147c478bd9Sstevel@tonic-gate 	if (remount) {
16157c478bd9Sstevel@tonic-gate 		if ((oldresource = vfsp->vfs_resource) != NULL)
16167c478bd9Sstevel@tonic-gate 			refstr_hold(oldresource);
16177c478bd9Sstevel@tonic-gate 		if ((oldmntpt = vfsp->vfs_mntpt) != NULL)
16187c478bd9Sstevel@tonic-gate 			refstr_hold(oldmntpt);
16197c478bd9Sstevel@tonic-gate 	}
1620d7de0ceaSRobert Harris 	vfs_setresource(vfsp, resource, 0);
1621d7de0ceaSRobert Harris 	vfs_setmntpoint(vfsp, mountpt, 0);
16227c478bd9Sstevel@tonic-gate 
1623df2381bfSpraks 	/*
1624df2381bfSpraks 	 * going to mount on this vnode, so notify.
1625df2381bfSpraks 	 */
1626da6c28aaSamw 	vnevent_mountedover(vp, NULL);
16277c478bd9Sstevel@tonic-gate 	error = VFS_MOUNT(vfsp, vp, uap, credp);
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_RDONLY)
16307c478bd9Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
16317c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_NOSUID)
16327c478bd9Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_NOSUID, NULL, 0);
16337c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_GLOBAL)
16347c478bd9Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_GLOBAL, NULL, 0);
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 	if (error) {
163793239addSjohnlev 		lofi_remove(vfsp);
163893239addSjohnlev 
16397c478bd9Sstevel@tonic-gate 		if (remount) {
16407c478bd9Sstevel@tonic-gate 			/* put back pre-remount options */
16417c478bd9Sstevel@tonic-gate 			vfs_swapopttbl(&mnt_mntopts, &vfsp->vfs_mntopts);
1642d7de0ceaSRobert Harris 			vfs_setmntpoint(vfsp, refstr_value(oldmntpt),
1643d7de0ceaSRobert Harris 			    VFSSP_VERBATIM);
16447c478bd9Sstevel@tonic-gate 			if (oldmntpt)
16457c478bd9Sstevel@tonic-gate 				refstr_rele(oldmntpt);
1646d7de0ceaSRobert Harris 			vfs_setresource(vfsp, refstr_value(oldresource),
1647d7de0ceaSRobert Harris 			    VFSSP_VERBATIM);
16487c478bd9Sstevel@tonic-gate 			if (oldresource)
16497c478bd9Sstevel@tonic-gate 				refstr_rele(oldresource);
16507c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag = ovflags;
16517c478bd9Sstevel@tonic-gate 			vfs_unlock(vfsp);
16527c478bd9Sstevel@tonic-gate 			VFS_RELE(vfsp);
16537c478bd9Sstevel@tonic-gate 		} else {
16547c478bd9Sstevel@tonic-gate 			vfs_unlock(vfsp);
16557c478bd9Sstevel@tonic-gate 			vfs_freemnttab(vfsp);
1656da6c28aaSamw 			vfs_free(vfsp);
16577c478bd9Sstevel@tonic-gate 		}
16587c478bd9Sstevel@tonic-gate 	} else {
16597c478bd9Sstevel@tonic-gate 		/*
16607c478bd9Sstevel@tonic-gate 		 * Set the mount time to now
16617c478bd9Sstevel@tonic-gate 		 */
16627c478bd9Sstevel@tonic-gate 		vfsp->vfs_mtime = ddi_get_time();
16637c478bd9Sstevel@tonic-gate 		if (remount) {
16647c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag &= ~VFS_REMOUNT;
16657c478bd9Sstevel@tonic-gate 			if (oldresource)
16667c478bd9Sstevel@tonic-gate 				refstr_rele(oldresource);
16677c478bd9Sstevel@tonic-gate 			if (oldmntpt)
16687c478bd9Sstevel@tonic-gate 				refstr_rele(oldmntpt);
16697c478bd9Sstevel@tonic-gate 		} else if (splice) {
16707c478bd9Sstevel@tonic-gate 			/*
16717c478bd9Sstevel@tonic-gate 			 * Link vfsp into the name space at the mount
16727c478bd9Sstevel@tonic-gate 			 * point. Vfs_add() is responsible for
16737c478bd9Sstevel@tonic-gate 			 * holding the mount point which will be
16747c478bd9Sstevel@tonic-gate 			 * released when vfs_remove() is called.
16757c478bd9Sstevel@tonic-gate 			 */
16767c478bd9Sstevel@tonic-gate 			vfs_add(vp, vfsp, uap->flags);
16777c478bd9Sstevel@tonic-gate 		} else {
16787c478bd9Sstevel@tonic-gate 			/*
16797c478bd9Sstevel@tonic-gate 			 * Hold the reference to file system which is
16807c478bd9Sstevel@tonic-gate 			 * not linked into the name space.
16817c478bd9Sstevel@tonic-gate 			 */
16827c478bd9Sstevel@tonic-gate 			vfsp->vfs_zone = NULL;
16837c478bd9Sstevel@tonic-gate 			VFS_HOLD(vfsp);
16847c478bd9Sstevel@tonic-gate 			vfsp->vfs_vnodecovered = NULL;
16857c478bd9Sstevel@tonic-gate 		}
16867c478bd9Sstevel@tonic-gate 		/*
16877c478bd9Sstevel@tonic-gate 		 * Set flags for global options encountered
16887c478bd9Sstevel@tonic-gate 		 */
16897c478bd9Sstevel@tonic-gate 		if (vfs_optionisset(vfsp, MNTOPT_RO, NULL))
16907c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag |= VFS_RDONLY;
16917c478bd9Sstevel@tonic-gate 		else
16927c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag &= ~VFS_RDONLY;
16937c478bd9Sstevel@tonic-gate 		if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
16947c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag |= (VFS_NOSETUID|VFS_NODEVICES);
16957c478bd9Sstevel@tonic-gate 		} else {
16967c478bd9Sstevel@tonic-gate 			if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
16977c478bd9Sstevel@tonic-gate 				vfsp->vfs_flag |= VFS_NODEVICES;
16987c478bd9Sstevel@tonic-gate 			else
16997c478bd9Sstevel@tonic-gate 				vfsp->vfs_flag &= ~VFS_NODEVICES;
17007c478bd9Sstevel@tonic-gate 			if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))
17017c478bd9Sstevel@tonic-gate 				vfsp->vfs_flag |= VFS_NOSETUID;
17027c478bd9Sstevel@tonic-gate 			else
17037c478bd9Sstevel@tonic-gate 				vfsp->vfs_flag &= ~VFS_NOSETUID;
17047c478bd9Sstevel@tonic-gate 		}
17057c478bd9Sstevel@tonic-gate 		if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL))
17067c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag |= VFS_NBMAND;
17077c478bd9Sstevel@tonic-gate 		else
17087c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag &= ~VFS_NBMAND;
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 		if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL))
17117c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag |= VFS_XATTR;
17127c478bd9Sstevel@tonic-gate 		else
17137c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag &= ~VFS_XATTR;
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 		if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL))
17167c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag |= VFS_NOEXEC;
17177c478bd9Sstevel@tonic-gate 		else
17187c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag &= ~VFS_NOEXEC;
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 		/*
17217c478bd9Sstevel@tonic-gate 		 * Now construct the output option string of options
17227c478bd9Sstevel@tonic-gate 		 * we recognized.
17237c478bd9Sstevel@tonic-gate 		 */
17247c478bd9Sstevel@tonic-gate 		if (uap->flags & MS_OPTIONSTR) {
17257c478bd9Sstevel@tonic-gate 			vfs_list_read_lock();
17267c478bd9Sstevel@tonic-gate 			copyout_error = vfs_buildoptionstr(
1727b1b8ab34Slling 			    &vfsp->vfs_mntopts, inargs, optlen);
17287c478bd9Sstevel@tonic-gate 			vfs_list_unlock();
17297c478bd9Sstevel@tonic-gate 			if (copyout_error == 0 &&
17307c478bd9Sstevel@tonic-gate 			    (uap->flags & MS_SYSSPACE) == 0) {
17317c478bd9Sstevel@tonic-gate 				copyout_error = copyoutstr(inargs, opts,
17327c478bd9Sstevel@tonic-gate 				    optlen, NULL);
17337c478bd9Sstevel@tonic-gate 			}
17347c478bd9Sstevel@tonic-gate 		}
17355a59a8b3Srsb 
173682c7f3c4Srsb 		/*
173782c7f3c4Srsb 		 * If this isn't a remount, set up the vopstats before
1738ec64bf9dSrsb 		 * anyone can touch this. We only allow spliced file
1739ec64bf9dSrsb 		 * systems (file systems which are in the namespace) to
1740ec64bf9dSrsb 		 * have the VFS_STATS flag set.
1741ec64bf9dSrsb 		 * NOTE: PxFS mounts the underlying file system with
1742ec64bf9dSrsb 		 * MS_NOSPLICE set and copies those vfs_flags to its private
1743ec64bf9dSrsb 		 * vfs structure. As a result, PxFS should never have
1744ec64bf9dSrsb 		 * the VFS_STATS flag or else we might access the vfs
1745ec64bf9dSrsb 		 * statistics-related fields prior to them being
1746ec64bf9dSrsb 		 * properly initialized.
174782c7f3c4Srsb 		 */
1748ec64bf9dSrsb 		if (!remount && (vswp->vsw_flag & VSW_STATS) && splice) {
174982c7f3c4Srsb 			initialize_vopstats(&vfsp->vfs_vopstats);
175082c7f3c4Srsb 			/*
175182c7f3c4Srsb 			 * We need to set vfs_vskap to NULL because there's
175282c7f3c4Srsb 			 * a chance it won't be set below.  This is checked
175382c7f3c4Srsb 			 * in teardown_vopstats() so we can't have garbage.
175482c7f3c4Srsb 			 */
175582c7f3c4Srsb 			vfsp->vfs_vskap = NULL;
17565a59a8b3Srsb 			vfsp->vfs_flag |= VFS_STATS;
175782c7f3c4Srsb 			vfsp->vfs_fstypevsp = get_fstype_vopstats(vfsp, vswp);
17585a59a8b3Srsb 		}
17595a59a8b3Srsb 
1760f48205beScasper 		if (vswp->vsw_flag & VSW_XID)
1761f48205beScasper 			vfsp->vfs_flag |= VFS_XID;
1762f48205beScasper 
17637c478bd9Sstevel@tonic-gate 		vfs_unlock(vfsp);
17647c478bd9Sstevel@tonic-gate 	}
17655fd5c689SJerry Jelinek 	mount_completed(zone);
17665fd5c689SJerry Jelinek 	zone_rele(zone);
17677c478bd9Sstevel@tonic-gate 	if (splice)
17687c478bd9Sstevel@tonic-gate 		vn_vfsunlock(vp);
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	if ((error == 0) && (copyout_error == 0)) {
177182c7f3c4Srsb 		if (!remount) {
177282c7f3c4Srsb 			/*
177382c7f3c4Srsb 			 * Don't call get_vskstat_anchor() while holding
177482c7f3c4Srsb 			 * locks since it allocates memory and calls
177582c7f3c4Srsb 			 * VFS_STATVFS().  For NFS, the latter can generate
177682c7f3c4Srsb 			 * an over-the-wire call.
177782c7f3c4Srsb 			 */
177882c7f3c4Srsb 			vskap = get_vskstat_anchor(vfsp);
177982c7f3c4Srsb 			/* Only take the lock if we have something to do */
178082c7f3c4Srsb 			if (vskap != NULL) {
178182c7f3c4Srsb 				vfs_lock_wait(vfsp);
178282c7f3c4Srsb 				if (vfsp->vfs_flag & VFS_STATS) {
178382c7f3c4Srsb 					vfsp->vfs_vskap = vskap;
178482c7f3c4Srsb 				}
178582c7f3c4Srsb 				vfs_unlock(vfsp);
178682c7f3c4Srsb 			}
178782c7f3c4Srsb 		}
17885a59a8b3Srsb 		/* Return vfsp to caller. */
17897c478bd9Sstevel@tonic-gate 		*vfspp = vfsp;
17907c478bd9Sstevel@tonic-gate 	}
17917c478bd9Sstevel@tonic-gate errout:
17927c478bd9Sstevel@tonic-gate 	vfs_freeopttbl(&mnt_mntopts);
17937c478bd9Sstevel@tonic-gate 	if (resource != NULL)
17947c478bd9Sstevel@tonic-gate 		kmem_free(resource, strlen(resource) + 1);
17957c478bd9Sstevel@tonic-gate 	if (mountpt != NULL)
17967c478bd9Sstevel@tonic-gate 		kmem_free(mountpt, strlen(mountpt) + 1);
17977c478bd9Sstevel@tonic-gate 	/*
17987c478bd9Sstevel@tonic-gate 	 * It is possible we errored prior to adding to mount in progress
17997c478bd9Sstevel@tonic-gate 	 * table. Must free vnode we acquired with successful lookupname.
18007c478bd9Sstevel@tonic-gate 	 */
18017c478bd9Sstevel@tonic-gate 	if (addmip)
18027c478bd9Sstevel@tonic-gate 		VN_RELE(bvp);
18037c478bd9Sstevel@tonic-gate 	if (delmip)
18047c478bd9Sstevel@tonic-gate 		vfs_delmip(vfsp);
18057c478bd9Sstevel@tonic-gate 	ASSERT(vswp != NULL);
18067c478bd9Sstevel@tonic-gate 	vfs_unrefvfssw(vswp);
18077c478bd9Sstevel@tonic-gate 	if (inargs != opts)
18087c478bd9Sstevel@tonic-gate 		kmem_free(inargs, MAX_MNTOPT_STR);
18097c478bd9Sstevel@tonic-gate 	if (copyout_error) {
181093239addSjohnlev 		lofi_remove(vfsp);
18117c478bd9Sstevel@tonic-gate 		VFS_RELE(vfsp);
18127c478bd9Sstevel@tonic-gate 		error = copyout_error;
18137c478bd9Sstevel@tonic-gate 	}
18147c478bd9Sstevel@tonic-gate 	return (error);
18157c478bd9Sstevel@tonic-gate }
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate static void
vfs_setpath(struct vfs * vfsp,refstr_t ** refp,const char * newpath,uint32_t flag)1818d7de0ceaSRobert Harris vfs_setpath(
1819d7de0ceaSRobert Harris     struct vfs *vfsp,		/* vfs being updated */
1820d7de0ceaSRobert Harris     refstr_t **refp,		/* Ref-count string to contain the new path */
1821d7de0ceaSRobert Harris     const char *newpath,	/* Path to add to refp (above) */
1822d7de0ceaSRobert Harris     uint32_t flag)		/* flag */
18237c478bd9Sstevel@tonic-gate {
18247c478bd9Sstevel@tonic-gate 	size_t len;
18257c478bd9Sstevel@tonic-gate 	refstr_t *ref;
18267c478bd9Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
18277c478bd9Sstevel@tonic-gate 	char *sp;
18287c478bd9Sstevel@tonic-gate 	int have_list_lock = 0;
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 	ASSERT(!VFS_ON_LIST(vfsp) || vfs_lock_held(vfsp));
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 	/*
18337c478bd9Sstevel@tonic-gate 	 * New path must be less than MAXPATHLEN because mntfs
18347c478bd9Sstevel@tonic-gate 	 * will only display up to MAXPATHLEN bytes. This is currently
18357c478bd9Sstevel@tonic-gate 	 * safe, because domount() uses pn_get(), and other callers
18367c478bd9Sstevel@tonic-gate 	 * similarly cap the size to fewer than MAXPATHLEN bytes.
18377c478bd9Sstevel@tonic-gate 	 */
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	ASSERT(strlen(newpath) < MAXPATHLEN);
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	/* mntfs requires consistency while vfs list lock is held */
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 	if (VFS_ON_LIST(vfsp)) {
18447c478bd9Sstevel@tonic-gate 		have_list_lock = 1;
18457c478bd9Sstevel@tonic-gate 		vfs_list_lock();
18467c478bd9Sstevel@tonic-gate 	}
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 	if (*refp != NULL)
18497c478bd9Sstevel@tonic-gate 		refstr_rele(*refp);
18507c478bd9Sstevel@tonic-gate 
1851d7de0ceaSRobert Harris 	/*
1852d7de0ceaSRobert Harris 	 * If we are in a non-global zone then we prefix the supplied path,
1853d7de0ceaSRobert Harris 	 * newpath, with the zone's root path, with two exceptions. The first
1854d7de0ceaSRobert Harris 	 * is where we have been explicitly directed to avoid doing so; this
1855d7de0ceaSRobert Harris 	 * will be the case following a failed remount, where the path supplied
1856d7de0ceaSRobert Harris 	 * will be a saved version which must now be restored. The second
1857d7de0ceaSRobert Harris 	 * exception is where newpath is not a pathname but a descriptive name,
1858d7de0ceaSRobert Harris 	 * e.g. "procfs".
1859d7de0ceaSRobert Harris 	 */
1860d7de0ceaSRobert Harris 	if (zone == global_zone || (flag & VFSSP_VERBATIM) || *newpath != '/') {
18617c478bd9Sstevel@tonic-gate 		ref = refstr_alloc(newpath);
18627c478bd9Sstevel@tonic-gate 		goto out;
18637c478bd9Sstevel@tonic-gate 	}
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	/*
18667c478bd9Sstevel@tonic-gate 	 * Truncate the trailing '/' in the zoneroot, and merge
18677c478bd9Sstevel@tonic-gate 	 * in the zone's rootpath with the "newpath" (resource
18687c478bd9Sstevel@tonic-gate 	 * or mountpoint) passed in.
18697c478bd9Sstevel@tonic-gate 	 *
18707c478bd9Sstevel@tonic-gate 	 * The size of the required buffer is thus the size of
18717c478bd9Sstevel@tonic-gate 	 * the buffer required for the passed-in newpath
18727c478bd9Sstevel@tonic-gate 	 * (strlen(newpath) + 1), plus the size of the buffer
18737c478bd9Sstevel@tonic-gate 	 * required to hold zone_rootpath (zone_rootpathlen)
18747c478bd9Sstevel@tonic-gate 	 * minus one for one of the now-superfluous NUL
18757c478bd9Sstevel@tonic-gate 	 * terminations, minus one for the trailing '/'.
18767c478bd9Sstevel@tonic-gate 	 *
18777c478bd9Sstevel@tonic-gate 	 * That gives us:
18787c478bd9Sstevel@tonic-gate 	 *
18797c478bd9Sstevel@tonic-gate 	 * (strlen(newpath) + 1) + zone_rootpathlen - 1 - 1
18807c478bd9Sstevel@tonic-gate 	 *
18817c478bd9Sstevel@tonic-gate 	 * Which is what we have below.
18827c478bd9Sstevel@tonic-gate 	 */
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 	len = strlen(newpath) + zone->zone_rootpathlen - 1;
18857c478bd9Sstevel@tonic-gate 	sp = kmem_alloc(len, KM_SLEEP);
18867c478bd9Sstevel@tonic-gate 
18877c478bd9Sstevel@tonic-gate 	/*
18887c478bd9Sstevel@tonic-gate 	 * Copy everything including the trailing slash, which
18897c478bd9Sstevel@tonic-gate 	 * we then overwrite with the NUL character.
18907c478bd9Sstevel@tonic-gate 	 */
18917c478bd9Sstevel@tonic-gate 
18927c478bd9Sstevel@tonic-gate 	(void) strcpy(sp, zone->zone_rootpath);
18937c478bd9Sstevel@tonic-gate 	sp[zone->zone_rootpathlen - 2] = '\0';
18947c478bd9Sstevel@tonic-gate 	(void) strcat(sp, newpath);
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 	ref = refstr_alloc(sp);
18977c478bd9Sstevel@tonic-gate 	kmem_free(sp, len);
18987c478bd9Sstevel@tonic-gate out:
18997c478bd9Sstevel@tonic-gate 	*refp = ref;
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 	if (have_list_lock) {
19027c478bd9Sstevel@tonic-gate 		vfs_mnttab_modtimeupd();
19037c478bd9Sstevel@tonic-gate 		vfs_list_unlock();
19047c478bd9Sstevel@tonic-gate 	}
19057c478bd9Sstevel@tonic-gate }
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate /*
19087c478bd9Sstevel@tonic-gate  * Record a mounted resource name in a vfs structure.
19097c478bd9Sstevel@tonic-gate  * If vfsp is already mounted, caller must hold the vfs lock.
19107c478bd9Sstevel@tonic-gate  */
19117c478bd9Sstevel@tonic-gate void
vfs_setresource(struct vfs * vfsp,const char * resource,uint32_t flag)1912d7de0ceaSRobert Harris vfs_setresource(struct vfs *vfsp, const char *resource, uint32_t flag)
19137c478bd9Sstevel@tonic-gate {
19147c478bd9Sstevel@tonic-gate 	if (resource == NULL || resource[0] == '\0')
19157c478bd9Sstevel@tonic-gate 		resource = VFS_NORESOURCE;
1916d7de0ceaSRobert Harris 	vfs_setpath(vfsp, &vfsp->vfs_resource, resource, flag);
19177c478bd9Sstevel@tonic-gate }
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate /*
19207c478bd9Sstevel@tonic-gate  * Record a mount point name in a vfs structure.
19217c478bd9Sstevel@tonic-gate  * If vfsp is already mounted, caller must hold the vfs lock.
19227c478bd9Sstevel@tonic-gate  */
19237c478bd9Sstevel@tonic-gate void
vfs_setmntpoint(struct vfs * vfsp,const char * mntpt,uint32_t flag)1924d7de0ceaSRobert Harris vfs_setmntpoint(struct vfs *vfsp, const char *mntpt, uint32_t flag)
19257c478bd9Sstevel@tonic-gate {
19267c478bd9Sstevel@tonic-gate 	if (mntpt == NULL || mntpt[0] == '\0')
19277c478bd9Sstevel@tonic-gate 		mntpt = VFS_NOMNTPT;
1928d7de0ceaSRobert Harris 	vfs_setpath(vfsp, &vfsp->vfs_mntpt, mntpt, flag);
19297c478bd9Sstevel@tonic-gate }
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate /* Returns the vfs_resource. Caller must call refstr_rele() when finished. */
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate refstr_t *
vfs_getresource(const struct vfs * vfsp)19347c478bd9Sstevel@tonic-gate vfs_getresource(const struct vfs *vfsp)
19357c478bd9Sstevel@tonic-gate {
19367c478bd9Sstevel@tonic-gate 	refstr_t *resource;
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 	vfs_list_read_lock();
19397c478bd9Sstevel@tonic-gate 	resource = vfsp->vfs_resource;
19407c478bd9Sstevel@tonic-gate 	refstr_hold(resource);
19417c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 	return (resource);
19447c478bd9Sstevel@tonic-gate }
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate /* Returns the vfs_mntpt. Caller must call refstr_rele() when finished. */
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate refstr_t *
vfs_getmntpoint(const struct vfs * vfsp)19497c478bd9Sstevel@tonic-gate vfs_getmntpoint(const struct vfs *vfsp)
19507c478bd9Sstevel@tonic-gate {
19517c478bd9Sstevel@tonic-gate 	refstr_t *mntpt;
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	vfs_list_read_lock();
19547c478bd9Sstevel@tonic-gate 	mntpt = vfsp->vfs_mntpt;
19557c478bd9Sstevel@tonic-gate 	refstr_hold(mntpt);
19567c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 	return (mntpt);
19597c478bd9Sstevel@tonic-gate }
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate /*
19627c478bd9Sstevel@tonic-gate  * Create an empty options table with enough empty slots to hold all
19637c478bd9Sstevel@tonic-gate  * The options in the options string passed as an argument.
19647c478bd9Sstevel@tonic-gate  * Potentially prepend another options table.
19657c478bd9Sstevel@tonic-gate  *
19667c478bd9Sstevel@tonic-gate  * Note: caller is responsible for locking the vfs list, if needed,
19677c478bd9Sstevel@tonic-gate  *       to protect mops.
19687c478bd9Sstevel@tonic-gate  */
19697c478bd9Sstevel@tonic-gate static void
vfs_createopttbl_extend(mntopts_t * mops,const char * opts,const mntopts_t * mtmpl)19707c478bd9Sstevel@tonic-gate vfs_createopttbl_extend(mntopts_t *mops, const char *opts,
19717c478bd9Sstevel@tonic-gate     const mntopts_t *mtmpl)
19727c478bd9Sstevel@tonic-gate {
19737c478bd9Sstevel@tonic-gate 	const char *s = opts;
19747c478bd9Sstevel@tonic-gate 	uint_t count;
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate 	if (opts == NULL || *opts == '\0') {
19777c478bd9Sstevel@tonic-gate 		count = 0;
19787c478bd9Sstevel@tonic-gate 	} else {
19797c478bd9Sstevel@tonic-gate 		count = 1;
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 		/*
19827c478bd9Sstevel@tonic-gate 		 * Count number of options in the string
19837c478bd9Sstevel@tonic-gate 		 */
19847c478bd9Sstevel@tonic-gate 		for (s = strchr(s, ','); s != NULL; s = strchr(s, ',')) {
19857c478bd9Sstevel@tonic-gate 			count++;
19867c478bd9Sstevel@tonic-gate 			s++;
19877c478bd9Sstevel@tonic-gate 		}
19887c478bd9Sstevel@tonic-gate 	}
19897c478bd9Sstevel@tonic-gate 	vfs_copyopttbl_extend(mtmpl, mops, count);
19907c478bd9Sstevel@tonic-gate }
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate /*
19937c478bd9Sstevel@tonic-gate  * Create an empty options table with enough empty slots to hold all
19947c478bd9Sstevel@tonic-gate  * The options in the options string passed as an argument.
19957c478bd9Sstevel@tonic-gate  *
19967c478bd9Sstevel@tonic-gate  * This function is *not* for general use by filesystems.
19977c478bd9Sstevel@tonic-gate  *
19987c478bd9Sstevel@tonic-gate  * Note: caller is responsible for locking the vfs list, if needed,
19997c478bd9Sstevel@tonic-gate  *       to protect mops.
20007c478bd9Sstevel@tonic-gate  */
20017c478bd9Sstevel@tonic-gate void
vfs_createopttbl(mntopts_t * mops,const char * opts)20027c478bd9Sstevel@tonic-gate vfs_createopttbl(mntopts_t *mops, const char *opts)
20037c478bd9Sstevel@tonic-gate {
20047c478bd9Sstevel@tonic-gate 	vfs_createopttbl_extend(mops, opts, NULL);
20057c478bd9Sstevel@tonic-gate }
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate /*
20097c478bd9Sstevel@tonic-gate  * Swap two mount options tables
20107c478bd9Sstevel@tonic-gate  */
20117c478bd9Sstevel@tonic-gate static void
vfs_swapopttbl_nolock(mntopts_t * optbl1,mntopts_t * optbl2)20127c478bd9Sstevel@tonic-gate vfs_swapopttbl_nolock(mntopts_t *optbl1, mntopts_t *optbl2)
20137c478bd9Sstevel@tonic-gate {
20147c478bd9Sstevel@tonic-gate 	uint_t tmpcnt;
20157c478bd9Sstevel@tonic-gate 	mntopt_t *tmplist;
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 	tmpcnt = optbl2->mo_count;
20187c478bd9Sstevel@tonic-gate 	tmplist = optbl2->mo_list;
20197c478bd9Sstevel@tonic-gate 	optbl2->mo_count = optbl1->mo_count;
20207c478bd9Sstevel@tonic-gate 	optbl2->mo_list = optbl1->mo_list;
20217c478bd9Sstevel@tonic-gate 	optbl1->mo_count = tmpcnt;
20227c478bd9Sstevel@tonic-gate 	optbl1->mo_list = tmplist;
20237c478bd9Sstevel@tonic-gate }
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate static void
vfs_swapopttbl(mntopts_t * optbl1,mntopts_t * optbl2)20267c478bd9Sstevel@tonic-gate vfs_swapopttbl(mntopts_t *optbl1, mntopts_t *optbl2)
20277c478bd9Sstevel@tonic-gate {
20287c478bd9Sstevel@tonic-gate 	vfs_list_lock();
20297c478bd9Sstevel@tonic-gate 	vfs_swapopttbl_nolock(optbl1, optbl2);
20307c478bd9Sstevel@tonic-gate 	vfs_mnttab_modtimeupd();
20317c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
20327c478bd9Sstevel@tonic-gate }
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate static char **
vfs_copycancelopt_extend(char ** const moc,int extend)20357c478bd9Sstevel@tonic-gate vfs_copycancelopt_extend(char **const moc, int extend)
20367c478bd9Sstevel@tonic-gate {
20377c478bd9Sstevel@tonic-gate 	int i = 0;
20387c478bd9Sstevel@tonic-gate 	int j;
20397c478bd9Sstevel@tonic-gate 	char **result;
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 	if (moc != NULL) {
20427c478bd9Sstevel@tonic-gate 		for (; moc[i] != NULL; i++)
20437c478bd9Sstevel@tonic-gate 			/* count number of options to cancel */;
20447c478bd9Sstevel@tonic-gate 	}
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 	if (i + extend == 0)
20477c478bd9Sstevel@tonic-gate 		return (NULL);
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 	result = kmem_alloc((i + extend + 1) * sizeof (char *), KM_SLEEP);
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 	for (j = 0; j < i; j++) {
20527c478bd9Sstevel@tonic-gate 		result[j] = kmem_alloc(strlen(moc[j]) + 1, KM_SLEEP);
20537c478bd9Sstevel@tonic-gate 		(void) strcpy(result[j], moc[j]);
20547c478bd9Sstevel@tonic-gate 	}
20557c478bd9Sstevel@tonic-gate 	for (; j <= i + extend; j++)
20567c478bd9Sstevel@tonic-gate 		result[j] = NULL;
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	return (result);
20597c478bd9Sstevel@tonic-gate }
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate static void
vfs_copyopt(const mntopt_t * s,mntopt_t * d)20627c478bd9Sstevel@tonic-gate vfs_copyopt(const mntopt_t *s, mntopt_t *d)
20637c478bd9Sstevel@tonic-gate {
20647c478bd9Sstevel@tonic-gate 	char *sp, *dp;
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 	d->mo_flags = s->mo_flags;
20677c478bd9Sstevel@tonic-gate 	d->mo_data = s->mo_data;
20687c478bd9Sstevel@tonic-gate 	sp = s->mo_name;
20697c478bd9Sstevel@tonic-gate 	if (sp != NULL) {
20707c478bd9Sstevel@tonic-gate 		dp = kmem_alloc(strlen(sp) + 1, KM_SLEEP);
20717c478bd9Sstevel@tonic-gate 		(void) strcpy(dp, sp);
20727c478bd9Sstevel@tonic-gate 		d->mo_name = dp;
20737c478bd9Sstevel@tonic-gate 	} else {
20747c478bd9Sstevel@tonic-gate 		d->mo_name = NULL; /* should never happen */
20757c478bd9Sstevel@tonic-gate 	}
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 	d->mo_cancel = vfs_copycancelopt_extend(s->mo_cancel, 0);
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 	sp = s->mo_arg;
20807c478bd9Sstevel@tonic-gate 	if (sp != NULL) {
20817c478bd9Sstevel@tonic-gate 		dp = kmem_alloc(strlen(sp) + 1, KM_SLEEP);
20827c478bd9Sstevel@tonic-gate 		(void) strcpy(dp, sp);
20837c478bd9Sstevel@tonic-gate 		d->mo_arg = dp;
20847c478bd9Sstevel@tonic-gate 	} else {
20857c478bd9Sstevel@tonic-gate 		d->mo_arg = NULL;
20867c478bd9Sstevel@tonic-gate 	}
20877c478bd9Sstevel@tonic-gate }
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate /*
20907c478bd9Sstevel@tonic-gate  * Copy a mount options table, possibly allocating some spare
20917c478bd9Sstevel@tonic-gate  * slots at the end.  It is permissible to copy_extend the NULL table.
20927c478bd9Sstevel@tonic-gate  */
20937c478bd9Sstevel@tonic-gate static void
vfs_copyopttbl_extend(const mntopts_t * smo,mntopts_t * dmo,int extra)20947c478bd9Sstevel@tonic-gate vfs_copyopttbl_extend(const mntopts_t *smo, mntopts_t *dmo, int extra)
20957c478bd9Sstevel@tonic-gate {
20967c478bd9Sstevel@tonic-gate 	uint_t i, count;
20977c478bd9Sstevel@tonic-gate 	mntopt_t *motbl;
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate 	/*
21007c478bd9Sstevel@tonic-gate 	 * Clear out any existing stuff in the options table being initialized
21017c478bd9Sstevel@tonic-gate 	 */
21027c478bd9Sstevel@tonic-gate 	vfs_freeopttbl(dmo);
21037c478bd9Sstevel@tonic-gate 	count = (smo == NULL) ? 0 : smo->mo_count;
21047c478bd9Sstevel@tonic-gate 	if ((count + extra) == 0)	/* nothing to do */
21057c478bd9Sstevel@tonic-gate 		return;
21067c478bd9Sstevel@tonic-gate 	dmo->mo_count = count + extra;
21077c478bd9Sstevel@tonic-gate 	motbl = kmem_zalloc((count + extra) * sizeof (mntopt_t), KM_SLEEP);
21087c478bd9Sstevel@tonic-gate 	dmo->mo_list = motbl;
21097c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
21107c478bd9Sstevel@tonic-gate 		vfs_copyopt(&smo->mo_list[i], &motbl[i]);
21117c478bd9Sstevel@tonic-gate 	}
21127c478bd9Sstevel@tonic-gate 	for (i = count; i < count + extra; i++) {
21137c478bd9Sstevel@tonic-gate 		motbl[i].mo_flags = MO_EMPTY;
21147c478bd9Sstevel@tonic-gate 	}
21157c478bd9Sstevel@tonic-gate }
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate /*
21187c478bd9Sstevel@tonic-gate  * Copy a mount options table.
21197c478bd9Sstevel@tonic-gate  *
21207c478bd9Sstevel@tonic-gate  * This function is *not* for general use by filesystems.
21217c478bd9Sstevel@tonic-gate  *
21227c478bd9Sstevel@tonic-gate  * Note: caller is responsible for locking the vfs list, if needed,
21237c478bd9Sstevel@tonic-gate  *       to protect smo and dmo.
21247c478bd9Sstevel@tonic-gate  */
21257c478bd9Sstevel@tonic-gate void
vfs_copyopttbl(const mntopts_t * smo,mntopts_t * dmo)21267c478bd9Sstevel@tonic-gate vfs_copyopttbl(const mntopts_t *smo, mntopts_t *dmo)
21277c478bd9Sstevel@tonic-gate {
21287c478bd9Sstevel@tonic-gate 	vfs_copyopttbl_extend(smo, dmo, 0);
21297c478bd9Sstevel@tonic-gate }
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate static char **
vfs_mergecancelopts(const mntopt_t * mop1,const mntopt_t * mop2)21327c478bd9Sstevel@tonic-gate vfs_mergecancelopts(const mntopt_t *mop1, const mntopt_t *mop2)
21337c478bd9Sstevel@tonic-gate {
21347c478bd9Sstevel@tonic-gate 	int c1 = 0;
21357c478bd9Sstevel@tonic-gate 	int c2 = 0;
21367c478bd9Sstevel@tonic-gate 	char **result;
21377c478bd9Sstevel@tonic-gate 	char **sp1, **sp2, **dp;
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 	/*
21407c478bd9Sstevel@tonic-gate 	 * First we count both lists of cancel options.
21417c478bd9Sstevel@tonic-gate 	 * If either is NULL or has no elements, we return a copy of
21427c478bd9Sstevel@tonic-gate 	 * the other.
21437c478bd9Sstevel@tonic-gate 	 */
21447c478bd9Sstevel@tonic-gate 	if (mop1->mo_cancel != NULL) {
21457c478bd9Sstevel@tonic-gate 		for (; mop1->mo_cancel[c1] != NULL; c1++)
21467c478bd9Sstevel@tonic-gate 			/* count cancel options in mop1 */;
21477c478bd9Sstevel@tonic-gate 	}
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 	if (c1 == 0)
21507c478bd9Sstevel@tonic-gate 		return (vfs_copycancelopt_extend(mop2->mo_cancel, 0));
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 	if (mop2->mo_cancel != NULL) {
21537c478bd9Sstevel@tonic-gate 		for (; mop2->mo_cancel[c2] != NULL; c2++)
21547c478bd9Sstevel@tonic-gate 			/* count cancel options in mop2 */;
21557c478bd9Sstevel@tonic-gate 	}
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate 	result = vfs_copycancelopt_extend(mop1->mo_cancel, c2);
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 	if (c2 == 0)
21607c478bd9Sstevel@tonic-gate 		return (result);
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate 	/*
21637c478bd9Sstevel@tonic-gate 	 * When we get here, we've got two sets of cancel options;
21647c478bd9Sstevel@tonic-gate 	 * we need to merge the two sets.  We know that the result
21657c478bd9Sstevel@tonic-gate 	 * array has "c1+c2+1" entries and in the end we might shrink
21667c478bd9Sstevel@tonic-gate 	 * it.
21677c478bd9Sstevel@tonic-gate 	 * Result now has a copy of the c1 entries from mop1; we'll
21687c478bd9Sstevel@tonic-gate 	 * now lookup all the entries of mop2 in mop1 and copy it if
21697c478bd9Sstevel@tonic-gate 	 * it is unique.
21707c478bd9Sstevel@tonic-gate 	 * This operation is O(n^2) but it's only called once per
21717c478bd9Sstevel@tonic-gate 	 * filesystem per duplicate option.  This is a situation
21727c478bd9Sstevel@tonic-gate 	 * which doesn't arise with the filesystems in ON and
21737c478bd9Sstevel@tonic-gate 	 * n is generally 1.
21747c478bd9Sstevel@tonic-gate 	 */
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate 	dp = &result[c1];
21777c478bd9Sstevel@tonic-gate 	for (sp2 = mop2->mo_cancel; *sp2 != NULL; sp2++) {
21787c478bd9Sstevel@tonic-gate 		for (sp1 = mop1->mo_cancel; *sp1 != NULL; sp1++) {
21797c478bd9Sstevel@tonic-gate 			if (strcmp(*sp1, *sp2) == 0)
21807c478bd9Sstevel@tonic-gate 				break;
21817c478bd9Sstevel@tonic-gate 		}
21827c478bd9Sstevel@tonic-gate 		if (*sp1 == NULL) {
21837c478bd9Sstevel@tonic-gate 			/*
21847c478bd9Sstevel@tonic-gate 			 * Option *sp2 not found in mop1, so copy it.
21857c478bd9Sstevel@tonic-gate 			 * The calls to vfs_copycancelopt_extend()
21867c478bd9Sstevel@tonic-gate 			 * guarantee that there's enough room.
21877c478bd9Sstevel@tonic-gate 			 */
21887c478bd9Sstevel@tonic-gate 			*dp = kmem_alloc(strlen(*sp2) + 1, KM_SLEEP);
21897c478bd9Sstevel@tonic-gate 			(void) strcpy(*dp++, *sp2);
21907c478bd9Sstevel@tonic-gate 		}
21917c478bd9Sstevel@tonic-gate 	}
21927c478bd9Sstevel@tonic-gate 	if (dp != &result[c1+c2]) {
21937c478bd9Sstevel@tonic-gate 		size_t bytes = (dp - result + 1) * sizeof (char *);
21947c478bd9Sstevel@tonic-gate 		char **nres = kmem_alloc(bytes, KM_SLEEP);
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate 		bcopy(result, nres, bytes);
21977c478bd9Sstevel@tonic-gate 		kmem_free(result, (c1 + c2 + 1) * sizeof (char *));
21987c478bd9Sstevel@tonic-gate 		result = nres;
21997c478bd9Sstevel@tonic-gate 	}
22007c478bd9Sstevel@tonic-gate 	return (result);
22017c478bd9Sstevel@tonic-gate }
22027c478bd9Sstevel@tonic-gate 
22037c478bd9Sstevel@tonic-gate /*
22047c478bd9Sstevel@tonic-gate  * Merge two mount option tables (outer and inner) into one.  This is very
22057c478bd9Sstevel@tonic-gate  * similar to "merging" global variables and automatic variables in C.
22067c478bd9Sstevel@tonic-gate  *
22077c478bd9Sstevel@tonic-gate  * This isn't (and doesn't have to be) fast.
22087c478bd9Sstevel@tonic-gate  *
22097c478bd9Sstevel@tonic-gate  * This function is *not* for general use by filesystems.
22107c478bd9Sstevel@tonic-gate  *
22117c478bd9Sstevel@tonic-gate  * Note: caller is responsible for locking the vfs list, if needed,
22127c478bd9Sstevel@tonic-gate  *       to protect omo, imo & dmo.
22137c478bd9Sstevel@tonic-gate  */
22147c478bd9Sstevel@tonic-gate void
vfs_mergeopttbl(const mntopts_t * omo,const mntopts_t * imo,mntopts_t * dmo)22157c478bd9Sstevel@tonic-gate vfs_mergeopttbl(const mntopts_t *omo, const mntopts_t *imo, mntopts_t *dmo)
22167c478bd9Sstevel@tonic-gate {
22177c478bd9Sstevel@tonic-gate 	uint_t i, count;
22187c478bd9Sstevel@tonic-gate 	mntopt_t *mop, *motbl;
22197c478bd9Sstevel@tonic-gate 	uint_t freeidx;
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate 	/*
22227c478bd9Sstevel@tonic-gate 	 * First determine how much space we need to allocate.
22237c478bd9Sstevel@tonic-gate 	 */
22247c478bd9Sstevel@tonic-gate 	count = omo->mo_count;
22257c478bd9Sstevel@tonic-gate 	for (i = 0; i < imo->mo_count; i++) {
22267c478bd9Sstevel@tonic-gate 		if (imo->mo_list[i].mo_flags & MO_EMPTY)
22277c478bd9Sstevel@tonic-gate 			continue;
22287c478bd9Sstevel@tonic-gate 		if (vfs_hasopt(omo, imo->mo_list[i].mo_name) == NULL)
22297c478bd9Sstevel@tonic-gate 			count++;
22307c478bd9Sstevel@tonic-gate 	}
22317c478bd9Sstevel@tonic-gate 	ASSERT(count >= omo->mo_count &&
22327c478bd9Sstevel@tonic-gate 	    count <= omo->mo_count + imo->mo_count);
22337c478bd9Sstevel@tonic-gate 	motbl = kmem_alloc(count * sizeof (mntopt_t), KM_SLEEP);
22347c478bd9Sstevel@tonic-gate 	for (i = 0; i < omo->mo_count; i++)
22357c478bd9Sstevel@tonic-gate 		vfs_copyopt(&omo->mo_list[i], &motbl[i]);
22367c478bd9Sstevel@tonic-gate 	freeidx = omo->mo_count;
22377c478bd9Sstevel@tonic-gate 	for (i = 0; i < imo->mo_count; i++) {
22387c478bd9Sstevel@tonic-gate 		if (imo->mo_list[i].mo_flags & MO_EMPTY)
22397c478bd9Sstevel@tonic-gate 			continue;
22407c478bd9Sstevel@tonic-gate 		if ((mop = vfs_hasopt(omo, imo->mo_list[i].mo_name)) != NULL) {
22417c478bd9Sstevel@tonic-gate 			char **newcanp;
22427c478bd9Sstevel@tonic-gate 			uint_t index = mop - omo->mo_list;
22437c478bd9Sstevel@tonic-gate 
22447c478bd9Sstevel@tonic-gate 			newcanp = vfs_mergecancelopts(mop, &motbl[index]);
22457c478bd9Sstevel@tonic-gate 
22467c478bd9Sstevel@tonic-gate 			vfs_freeopt(&motbl[index]);
22477c478bd9Sstevel@tonic-gate 			vfs_copyopt(&imo->mo_list[i], &motbl[index]);
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate 			vfs_freecancelopt(motbl[index].mo_cancel);
22507c478bd9Sstevel@tonic-gate 			motbl[index].mo_cancel = newcanp;
22517c478bd9Sstevel@tonic-gate 		} else {
22527c478bd9Sstevel@tonic-gate 			/*
22537c478bd9Sstevel@tonic-gate 			 * If it's a new option, just copy it over to the first
22547c478bd9Sstevel@tonic-gate 			 * free location.
22557c478bd9Sstevel@tonic-gate 			 */
22567c478bd9Sstevel@tonic-gate 			vfs_copyopt(&imo->mo_list[i], &motbl[freeidx++]);
22577c478bd9Sstevel@tonic-gate 		}
22587c478bd9Sstevel@tonic-gate 	}
22597c478bd9Sstevel@tonic-gate 	dmo->mo_count = count;
22607c478bd9Sstevel@tonic-gate 	dmo->mo_list = motbl;
22617c478bd9Sstevel@tonic-gate }
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate /*
22647c478bd9Sstevel@tonic-gate  * Functions to set and clear mount options in a mount options table.
22657c478bd9Sstevel@tonic-gate  */
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate /*
22687c478bd9Sstevel@tonic-gate  * Clear a mount option, if it exists.
22697c478bd9Sstevel@tonic-gate  *
22707c478bd9Sstevel@tonic-gate  * The update_mnttab arg indicates whether mops is part of a vfs that is on
22717c478bd9Sstevel@tonic-gate  * the vfs list.
22727c478bd9Sstevel@tonic-gate  */
22737c478bd9Sstevel@tonic-gate static void
vfs_clearmntopt_nolock(mntopts_t * mops,const char * opt,int update_mnttab)22747c478bd9Sstevel@tonic-gate vfs_clearmntopt_nolock(mntopts_t *mops, const char *opt, int update_mnttab)
22757c478bd9Sstevel@tonic-gate {
22767c478bd9Sstevel@tonic-gate 	struct mntopt *mop;
22777c478bd9Sstevel@tonic-gate 	uint_t i, count;
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 	ASSERT(!update_mnttab || RW_WRITE_HELD(&vfslist));
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate 	count = mops->mo_count;
22827c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
22837c478bd9Sstevel@tonic-gate 		mop = &mops->mo_list[i];
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 		if (mop->mo_flags & MO_EMPTY)
22867c478bd9Sstevel@tonic-gate 			continue;
22877c478bd9Sstevel@tonic-gate 		if (strcmp(opt, mop->mo_name))
22887c478bd9Sstevel@tonic-gate 			continue;
22897c478bd9Sstevel@tonic-gate 		mop->mo_flags &= ~MO_SET;
22907c478bd9Sstevel@tonic-gate 		if (mop->mo_arg != NULL) {
22917c478bd9Sstevel@tonic-gate 			kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
22927c478bd9Sstevel@tonic-gate 		}
22937c478bd9Sstevel@tonic-gate 		mop->mo_arg = NULL;
22947c478bd9Sstevel@tonic-gate 		if (update_mnttab)
22957c478bd9Sstevel@tonic-gate 			vfs_mnttab_modtimeupd();
22967c478bd9Sstevel@tonic-gate 		break;
22977c478bd9Sstevel@tonic-gate 	}
22987c478bd9Sstevel@tonic-gate }
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate void
vfs_clearmntopt(struct vfs * vfsp,const char * opt)23017c478bd9Sstevel@tonic-gate vfs_clearmntopt(struct vfs *vfsp, const char *opt)
23027c478bd9Sstevel@tonic-gate {
23037c478bd9Sstevel@tonic-gate 	int gotlock = 0;
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate 	if (VFS_ON_LIST(vfsp)) {
23067c478bd9Sstevel@tonic-gate 		gotlock = 1;
23077c478bd9Sstevel@tonic-gate 		vfs_list_lock();
23087c478bd9Sstevel@tonic-gate 	}
23097c478bd9Sstevel@tonic-gate 	vfs_clearmntopt_nolock(&vfsp->vfs_mntopts, opt, gotlock);
23107c478bd9Sstevel@tonic-gate 	if (gotlock)
23117c478bd9Sstevel@tonic-gate 		vfs_list_unlock();
23127c478bd9Sstevel@tonic-gate }
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate /*
23167c478bd9Sstevel@tonic-gate  * Set a mount option on.  If it's not found in the table, it's silently
23177c478bd9Sstevel@tonic-gate  * ignored.  If the option has MO_IGNORE set, it is still set unless the
23187c478bd9Sstevel@tonic-gate  * VFS_NOFORCEOPT bit is set in the flags.  Also, VFS_DISPLAY/VFS_NODISPLAY flag
23197c478bd9Sstevel@tonic-gate  * bits can be used to toggle the MO_NODISPLAY bit for the option.
23207c478bd9Sstevel@tonic-gate  * If the VFS_CREATEOPT flag bit is set then the first option slot with
23217c478bd9Sstevel@tonic-gate  * MO_EMPTY set is created as the option passed in.
23227c478bd9Sstevel@tonic-gate  *
23237c478bd9Sstevel@tonic-gate  * The update_mnttab arg indicates whether mops is part of a vfs that is on
23247c478bd9Sstevel@tonic-gate  * the vfs list.
23257c478bd9Sstevel@tonic-gate  */
23267c478bd9Sstevel@tonic-gate static void
vfs_setmntopt_nolock(mntopts_t * mops,const char * opt,const char * arg,int flags,int update_mnttab)23277c478bd9Sstevel@tonic-gate vfs_setmntopt_nolock(mntopts_t *mops, const char *opt,
23287c478bd9Sstevel@tonic-gate     const char *arg, int flags, int update_mnttab)
23297c478bd9Sstevel@tonic-gate {
23307c478bd9Sstevel@tonic-gate 	mntopt_t *mop;
23317c478bd9Sstevel@tonic-gate 	uint_t i, count;
23327c478bd9Sstevel@tonic-gate 	char *sp;
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 	ASSERT(!update_mnttab || RW_WRITE_HELD(&vfslist));
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate 	if (flags & VFS_CREATEOPT) {
23377c478bd9Sstevel@tonic-gate 		if (vfs_hasopt(mops, opt) != NULL) {
23387c478bd9Sstevel@tonic-gate 			flags &= ~VFS_CREATEOPT;
23397c478bd9Sstevel@tonic-gate 		}
23407c478bd9Sstevel@tonic-gate 	}
23417c478bd9Sstevel@tonic-gate 	count = mops->mo_count;
23427c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
23437c478bd9Sstevel@tonic-gate 		mop = &mops->mo_list[i];
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate 		if (mop->mo_flags & MO_EMPTY) {
23467c478bd9Sstevel@tonic-gate 			if ((flags & VFS_CREATEOPT) == 0)
23477c478bd9Sstevel@tonic-gate 				continue;
23487c478bd9Sstevel@tonic-gate 			sp = kmem_alloc(strlen(opt) + 1, KM_SLEEP);
23497c478bd9Sstevel@tonic-gate 			(void) strcpy(sp, opt);
23507c478bd9Sstevel@tonic-gate 			mop->mo_name = sp;
23517c478bd9Sstevel@tonic-gate 			if (arg != NULL)
23527c478bd9Sstevel@tonic-gate 				mop->mo_flags = MO_HASVALUE;
23537c478bd9Sstevel@tonic-gate 			else
23547c478bd9Sstevel@tonic-gate 				mop->mo_flags = 0;
23557c478bd9Sstevel@tonic-gate 		} else if (strcmp(opt, mop->mo_name)) {
23567c478bd9Sstevel@tonic-gate 			continue;
23577c478bd9Sstevel@tonic-gate 		}
23587c478bd9Sstevel@tonic-gate 		if ((mop->mo_flags & MO_IGNORE) && (flags & VFS_NOFORCEOPT))
23597c478bd9Sstevel@tonic-gate 			break;
23607c478bd9Sstevel@tonic-gate 		if (arg != NULL && (mop->mo_flags & MO_HASVALUE) != 0) {
23617c478bd9Sstevel@tonic-gate 			sp = kmem_alloc(strlen(arg) + 1, KM_SLEEP);
23627c478bd9Sstevel@tonic-gate 			(void) strcpy(sp, arg);
23637c478bd9Sstevel@tonic-gate 		} else {
23647c478bd9Sstevel@tonic-gate 			sp = NULL;
23657c478bd9Sstevel@tonic-gate 		}
23667c478bd9Sstevel@tonic-gate 		if (mop->mo_arg != NULL)
23677c478bd9Sstevel@tonic-gate 			kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
23687c478bd9Sstevel@tonic-gate 		mop->mo_arg = sp;
23697c478bd9Sstevel@tonic-gate 		if (flags & VFS_DISPLAY)
23707c478bd9Sstevel@tonic-gate 			mop->mo_flags &= ~MO_NODISPLAY;
23717c478bd9Sstevel@tonic-gate 		if (flags & VFS_NODISPLAY)
23727c478bd9Sstevel@tonic-gate 			mop->mo_flags |= MO_NODISPLAY;
23737c478bd9Sstevel@tonic-gate 		mop->mo_flags |= MO_SET;
23747c478bd9Sstevel@tonic-gate 		if (mop->mo_cancel != NULL) {
23757c478bd9Sstevel@tonic-gate 			char **cp;
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate 			for (cp = mop->mo_cancel; *cp != NULL; cp++)
23787c478bd9Sstevel@tonic-gate 				vfs_clearmntopt_nolock(mops, *cp, 0);
23797c478bd9Sstevel@tonic-gate 		}
23807c478bd9Sstevel@tonic-gate 		if (update_mnttab)
23817c478bd9Sstevel@tonic-gate 			vfs_mnttab_modtimeupd();
23827c478bd9Sstevel@tonic-gate 		break;
23837c478bd9Sstevel@tonic-gate 	}
23847c478bd9Sstevel@tonic-gate }
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate void
vfs_setmntopt(struct vfs * vfsp,const char * opt,const char * arg,int flags)23877c478bd9Sstevel@tonic-gate vfs_setmntopt(struct vfs *vfsp, const char *opt, const char *arg, int flags)
23887c478bd9Sstevel@tonic-gate {
23897c478bd9Sstevel@tonic-gate 	int gotlock = 0;
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate 	if (VFS_ON_LIST(vfsp)) {
23927c478bd9Sstevel@tonic-gate 		gotlock = 1;
23937c478bd9Sstevel@tonic-gate 		vfs_list_lock();
23947c478bd9Sstevel@tonic-gate 	}
23957c478bd9Sstevel@tonic-gate 	vfs_setmntopt_nolock(&vfsp->vfs_mntopts, opt, arg, flags, gotlock);
23967c478bd9Sstevel@tonic-gate 	if (gotlock)
23977c478bd9Sstevel@tonic-gate 		vfs_list_unlock();
23987c478bd9Sstevel@tonic-gate }
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate /*
24027c478bd9Sstevel@tonic-gate  * Add a "tag" option to a mounted file system's options list.
24037c478bd9Sstevel@tonic-gate  *
24047c478bd9Sstevel@tonic-gate  * Note: caller is responsible for locking the vfs list, if needed,
24057c478bd9Sstevel@tonic-gate  *       to protect mops.
24067c478bd9Sstevel@tonic-gate  */
24077c478bd9Sstevel@tonic-gate static mntopt_t *
vfs_addtag(mntopts_t * mops,const char * tag)24087c478bd9Sstevel@tonic-gate vfs_addtag(mntopts_t *mops, const char *tag)
24097c478bd9Sstevel@tonic-gate {
24107c478bd9Sstevel@tonic-gate 	uint_t count;
24117c478bd9Sstevel@tonic-gate 	mntopt_t *mop, *motbl;
24127c478bd9Sstevel@tonic-gate 
24137c478bd9Sstevel@tonic-gate 	count = mops->mo_count + 1;
24147c478bd9Sstevel@tonic-gate 	motbl = kmem_zalloc(count * sizeof (mntopt_t), KM_SLEEP);
24157c478bd9Sstevel@tonic-gate 	if (mops->mo_count) {
24167c478bd9Sstevel@tonic-gate 		size_t len = (count - 1) * sizeof (mntopt_t);
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate 		bcopy(mops->mo_list, motbl, len);
24197c478bd9Sstevel@tonic-gate 		kmem_free(mops->mo_list, len);
24207c478bd9Sstevel@tonic-gate 	}
24217c478bd9Sstevel@tonic-gate 	mops->mo_count = count;
24227c478bd9Sstevel@tonic-gate 	mops->mo_list = motbl;
24237c478bd9Sstevel@tonic-gate 	mop = &motbl[count - 1];
24247c478bd9Sstevel@tonic-gate 	mop->mo_flags = MO_TAG;
24257c478bd9Sstevel@tonic-gate 	mop->mo_name = kmem_alloc(strlen(tag) + 1, KM_SLEEP);
24267c478bd9Sstevel@tonic-gate 	(void) strcpy(mop->mo_name, tag);
24277c478bd9Sstevel@tonic-gate 	return (mop);
24287c478bd9Sstevel@tonic-gate }
24297c478bd9Sstevel@tonic-gate 
24307c478bd9Sstevel@tonic-gate /*
24317c478bd9Sstevel@tonic-gate  * Allow users to set arbitrary "tags" in a vfs's mount options.
24327c478bd9Sstevel@tonic-gate  * Broader use within the kernel is discouraged.
24337c478bd9Sstevel@tonic-gate  */
24347c478bd9Sstevel@tonic-gate int
vfs_settag(uint_t major,uint_t minor,const char * mntpt,const char * tag,cred_t * cr)24357c478bd9Sstevel@tonic-gate vfs_settag(uint_t major, uint_t minor, const char *mntpt, const char *tag,
24367c478bd9Sstevel@tonic-gate     cred_t *cr)
24377c478bd9Sstevel@tonic-gate {
24387c478bd9Sstevel@tonic-gate 	vfs_t *vfsp;
24397c478bd9Sstevel@tonic-gate 	mntopts_t *mops;
24407c478bd9Sstevel@tonic-gate 	mntopt_t *mop;
24417c478bd9Sstevel@tonic-gate 	int found = 0;
24427c478bd9Sstevel@tonic-gate 	dev_t dev = makedevice(major, minor);
24437c478bd9Sstevel@tonic-gate 	int err = 0;
24447c478bd9Sstevel@tonic-gate 	char *buf = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
24457c478bd9Sstevel@tonic-gate 
24467c478bd9Sstevel@tonic-gate 	/*
24477c478bd9Sstevel@tonic-gate 	 * Find the desired mounted file system
24487c478bd9Sstevel@tonic-gate 	 */
24497c478bd9Sstevel@tonic-gate 	vfs_list_lock();
24507c478bd9Sstevel@tonic-gate 	vfsp = rootvfs;
24517c478bd9Sstevel@tonic-gate 	do {
24527c478bd9Sstevel@tonic-gate 		if (vfsp->vfs_dev == dev &&
24537c478bd9Sstevel@tonic-gate 		    strcmp(mntpt, refstr_value(vfsp->vfs_mntpt)) == 0) {
24547c478bd9Sstevel@tonic-gate 			found = 1;
24557c478bd9Sstevel@tonic-gate 			break;
24567c478bd9Sstevel@tonic-gate 		}
24577c478bd9Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
24587c478bd9Sstevel@tonic-gate 	} while (vfsp != rootvfs);
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 	if (!found) {
24617c478bd9Sstevel@tonic-gate 		err = EINVAL;
24627c478bd9Sstevel@tonic-gate 		goto out;
24637c478bd9Sstevel@tonic-gate 	}
24647c478bd9Sstevel@tonic-gate 	err = secpolicy_fs_config(cr, vfsp);
24657c478bd9Sstevel@tonic-gate 	if (err != 0)
24667c478bd9Sstevel@tonic-gate 		goto out;
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 	mops = &vfsp->vfs_mntopts;
24697c478bd9Sstevel@tonic-gate 	/*
24707c478bd9Sstevel@tonic-gate 	 * Add tag if it doesn't already exist
24717c478bd9Sstevel@tonic-gate 	 */
24727c478bd9Sstevel@tonic-gate 	if ((mop = vfs_hasopt(mops, tag)) == NULL) {
24737c478bd9Sstevel@tonic-gate 		int len;
24747c478bd9Sstevel@tonic-gate 
24757c478bd9Sstevel@tonic-gate 		(void) vfs_buildoptionstr(mops, buf, MAX_MNTOPT_STR);
24767c478bd9Sstevel@tonic-gate 		len = strlen(buf);
24777c478bd9Sstevel@tonic-gate 		if (len + strlen(tag) + 2 > MAX_MNTOPT_STR) {
24787c478bd9Sstevel@tonic-gate 			err = ENAMETOOLONG;
24797c478bd9Sstevel@tonic-gate 			goto out;
24807c478bd9Sstevel@tonic-gate 		}
24817c478bd9Sstevel@tonic-gate 		mop = vfs_addtag(mops, tag);
24827c478bd9Sstevel@tonic-gate 	}
24837c478bd9Sstevel@tonic-gate 	if ((mop->mo_flags & MO_TAG) == 0) {
24847c478bd9Sstevel@tonic-gate 		err = EINVAL;
24857c478bd9Sstevel@tonic-gate 		goto out;
24867c478bd9Sstevel@tonic-gate 	}
24877c478bd9Sstevel@tonic-gate 	vfs_setmntopt_nolock(mops, tag, NULL, 0, 1);
24887c478bd9Sstevel@tonic-gate out:
24897c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
24907c478bd9Sstevel@tonic-gate 	kmem_free(buf, MAX_MNTOPT_STR);
24917c478bd9Sstevel@tonic-gate 	return (err);
24927c478bd9Sstevel@tonic-gate }
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate /*
24957c478bd9Sstevel@tonic-gate  * Allow users to remove arbitrary "tags" in a vfs's mount options.
24967c478bd9Sstevel@tonic-gate  * Broader use within the kernel is discouraged.
24977c478bd9Sstevel@tonic-gate  */
24987c478bd9Sstevel@tonic-gate int
vfs_clrtag(uint_t major,uint_t minor,const char * mntpt,const char * tag,cred_t * cr)24997c478bd9Sstevel@tonic-gate vfs_clrtag(uint_t major, uint_t minor, const char *mntpt, const char *tag,
25007c478bd9Sstevel@tonic-gate     cred_t *cr)
25017c478bd9Sstevel@tonic-gate {
25027c478bd9Sstevel@tonic-gate 	vfs_t *vfsp;
25037c478bd9Sstevel@tonic-gate 	mntopt_t *mop;
25047c478bd9Sstevel@tonic-gate 	int found = 0;
25057c478bd9Sstevel@tonic-gate 	dev_t dev = makedevice(major, minor);
25067c478bd9Sstevel@tonic-gate 	int err = 0;
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate 	/*
25097c478bd9Sstevel@tonic-gate 	 * Find the desired mounted file system
25107c478bd9Sstevel@tonic-gate 	 */
25117c478bd9Sstevel@tonic-gate 	vfs_list_lock();
25127c478bd9Sstevel@tonic-gate 	vfsp = rootvfs;
25137c478bd9Sstevel@tonic-gate 	do {
25147c478bd9Sstevel@tonic-gate 		if (vfsp->vfs_dev == dev &&
25157c478bd9Sstevel@tonic-gate 		    strcmp(mntpt, refstr_value(vfsp->vfs_mntpt)) == 0) {
25167c478bd9Sstevel@tonic-gate 			found = 1;
25177c478bd9Sstevel@tonic-gate 			break;
25187c478bd9Sstevel@tonic-gate 		}
25197c478bd9Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
25207c478bd9Sstevel@tonic-gate 	} while (vfsp != rootvfs);
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate 	if (!found) {
25237c478bd9Sstevel@tonic-gate 		err = EINVAL;
25247c478bd9Sstevel@tonic-gate 		goto out;
25257c478bd9Sstevel@tonic-gate 	}
25267c478bd9Sstevel@tonic-gate 	err = secpolicy_fs_config(cr, vfsp);
25277c478bd9Sstevel@tonic-gate 	if (err != 0)
25287c478bd9Sstevel@tonic-gate 		goto out;
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate 	if ((mop = vfs_hasopt(&vfsp->vfs_mntopts, tag)) == NULL) {
25317c478bd9Sstevel@tonic-gate 		err = EINVAL;
25327c478bd9Sstevel@tonic-gate 		goto out;
25337c478bd9Sstevel@tonic-gate 	}
25347c478bd9Sstevel@tonic-gate 	if ((mop->mo_flags & MO_TAG) == 0) {
25357c478bd9Sstevel@tonic-gate 		err = EINVAL;
25367c478bd9Sstevel@tonic-gate 		goto out;
25377c478bd9Sstevel@tonic-gate 	}
25387c478bd9Sstevel@tonic-gate 	vfs_clearmntopt_nolock(&vfsp->vfs_mntopts, tag, 1);
25397c478bd9Sstevel@tonic-gate out:
25407c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
25417c478bd9Sstevel@tonic-gate 	return (err);
25427c478bd9Sstevel@tonic-gate }
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate /*
25457c478bd9Sstevel@tonic-gate  * Function to parse an option string and fill in a mount options table.
25467c478bd9Sstevel@tonic-gate  * Unknown options are silently ignored.  The input option string is modified
25477c478bd9Sstevel@tonic-gate  * by replacing separators with nulls.  If the create flag is set, options
25487c478bd9Sstevel@tonic-gate  * not found in the table are just added on the fly.  The table must have
25497c478bd9Sstevel@tonic-gate  * an option slot marked MO_EMPTY to add an option on the fly.
25507c478bd9Sstevel@tonic-gate  *
25517c478bd9Sstevel@tonic-gate  * This function is *not* for general use by filesystems.
25527c478bd9Sstevel@tonic-gate  *
25537c478bd9Sstevel@tonic-gate  * Note: caller is responsible for locking the vfs list, if needed,
25547c478bd9Sstevel@tonic-gate  *       to protect mops..
25557c478bd9Sstevel@tonic-gate  */
25567c478bd9Sstevel@tonic-gate void
vfs_parsemntopts(mntopts_t * mops,char * osp,int create)25577c478bd9Sstevel@tonic-gate vfs_parsemntopts(mntopts_t *mops, char *osp, int create)
25587c478bd9Sstevel@tonic-gate {
25597c478bd9Sstevel@tonic-gate 	char *s = osp, *p, *nextop, *valp, *cp, *ep;
25607c478bd9Sstevel@tonic-gate 	int setflg = VFS_NOFORCEOPT;
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 	if (osp == NULL)
25637c478bd9Sstevel@tonic-gate 		return;
25647c478bd9Sstevel@tonic-gate 	while (*s != '\0') {
25657c478bd9Sstevel@tonic-gate 		p = strchr(s, ',');	/* find next option */
25667c478bd9Sstevel@tonic-gate 		if (p == NULL) {
25677c478bd9Sstevel@tonic-gate 			cp = NULL;
25687c478bd9Sstevel@tonic-gate 			p = s + strlen(s);
25697c478bd9Sstevel@tonic-gate 		} else {
25707c478bd9Sstevel@tonic-gate 			cp = p;		/* save location of comma */
25717c478bd9Sstevel@tonic-gate 			*p++ = '\0';	/* mark end and point to next option */
25727c478bd9Sstevel@tonic-gate 		}
25737c478bd9Sstevel@tonic-gate 		nextop = p;
25747c478bd9Sstevel@tonic-gate 		p = strchr(s, '=');	/* look for value */
25757c478bd9Sstevel@tonic-gate 		if (p == NULL) {
25767c478bd9Sstevel@tonic-gate 			valp = NULL;	/* no value supplied */
25777c478bd9Sstevel@tonic-gate 		} else {
25787c478bd9Sstevel@tonic-gate 			ep = p;		/* save location of equals */
25797c478bd9Sstevel@tonic-gate 			*p++ = '\0';	/* end option and point to value */
25807c478bd9Sstevel@tonic-gate 			valp = p;
25817c478bd9Sstevel@tonic-gate 		}
25827c478bd9Sstevel@tonic-gate 		/*
25837c478bd9Sstevel@tonic-gate 		 * set option into options table
25847c478bd9Sstevel@tonic-gate 		 */
25857c478bd9Sstevel@tonic-gate 		if (create)
25867c478bd9Sstevel@tonic-gate 			setflg |= VFS_CREATEOPT;
25877c478bd9Sstevel@tonic-gate 		vfs_setmntopt_nolock(mops, s, valp, setflg, 0);
25887c478bd9Sstevel@tonic-gate 		if (cp != NULL)
25897c478bd9Sstevel@tonic-gate 			*cp = ',';	/* restore the comma */
25907c478bd9Sstevel@tonic-gate 		if (valp != NULL)
25917c478bd9Sstevel@tonic-gate 			*ep = '=';	/* restore the equals */
25927c478bd9Sstevel@tonic-gate 		s = nextop;
25937c478bd9Sstevel@tonic-gate 	}
25947c478bd9Sstevel@tonic-gate }
25957c478bd9Sstevel@tonic-gate 
25967c478bd9Sstevel@tonic-gate /*
25977c478bd9Sstevel@tonic-gate  * Function to inquire if an option exists in a mount options table.
25987c478bd9Sstevel@tonic-gate  * Returns a pointer to the option if it exists, else NULL.
25997c478bd9Sstevel@tonic-gate  *
26007c478bd9Sstevel@tonic-gate  * This function is *not* for general use by filesystems.
26017c478bd9Sstevel@tonic-gate  *
26027c478bd9Sstevel@tonic-gate  * Note: caller is responsible for locking the vfs list, if needed,
26037c478bd9Sstevel@tonic-gate  *       to protect mops.
26047c478bd9Sstevel@tonic-gate  */
26057c478bd9Sstevel@tonic-gate struct mntopt *
vfs_hasopt(const mntopts_t * mops,const char * opt)26067c478bd9Sstevel@tonic-gate vfs_hasopt(const mntopts_t *mops, const char *opt)
26077c478bd9Sstevel@tonic-gate {
26087c478bd9Sstevel@tonic-gate 	struct mntopt *mop;
26097c478bd9Sstevel@tonic-gate 	uint_t i, count;
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 	count = mops->mo_count;
26127c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
26137c478bd9Sstevel@tonic-gate 		mop = &mops->mo_list[i];
26147c478bd9Sstevel@tonic-gate 
26157c478bd9Sstevel@tonic-gate 		if (mop->mo_flags & MO_EMPTY)
26167c478bd9Sstevel@tonic-gate 			continue;
26177c478bd9Sstevel@tonic-gate 		if (strcmp(opt, mop->mo_name) == 0)
26187c478bd9Sstevel@tonic-gate 			return (mop);
26197c478bd9Sstevel@tonic-gate 	}
26207c478bd9Sstevel@tonic-gate 	return (NULL);
26217c478bd9Sstevel@tonic-gate }
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate /*
26247c478bd9Sstevel@tonic-gate  * Function to inquire if an option is set in a mount options table.
26257c478bd9Sstevel@tonic-gate  * Returns non-zero if set and fills in the arg pointer with a pointer to
26267c478bd9Sstevel@tonic-gate  * the argument string or NULL if there is no argument string.
26277c478bd9Sstevel@tonic-gate  */
26287c478bd9Sstevel@tonic-gate static int
vfs_optionisset_nolock(const mntopts_t * mops,const char * opt,char ** argp)26297c478bd9Sstevel@tonic-gate vfs_optionisset_nolock(const mntopts_t *mops, const char *opt, char **argp)
26307c478bd9Sstevel@tonic-gate {
26317c478bd9Sstevel@tonic-gate 	struct mntopt *mop;
26327c478bd9Sstevel@tonic-gate 	uint_t i, count;
26337c478bd9Sstevel@tonic-gate 
26347c478bd9Sstevel@tonic-gate 	count = mops->mo_count;
26357c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
26367c478bd9Sstevel@tonic-gate 		mop = &mops->mo_list[i];
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate 		if (mop->mo_flags & MO_EMPTY)
26397c478bd9Sstevel@tonic-gate 			continue;
26407c478bd9Sstevel@tonic-gate 		if (strcmp(opt, mop->mo_name))
26417c478bd9Sstevel@tonic-gate 			continue;
26427c478bd9Sstevel@tonic-gate 		if ((mop->mo_flags & MO_SET) == 0)
26437c478bd9Sstevel@tonic-gate 			return (0);
26447c478bd9Sstevel@tonic-gate 		if (argp != NULL && (mop->mo_flags & MO_HASVALUE) != 0)
26457c478bd9Sstevel@tonic-gate 			*argp = mop->mo_arg;
26467c478bd9Sstevel@tonic-gate 		return (1);
26477c478bd9Sstevel@tonic-gate 	}
26487c478bd9Sstevel@tonic-gate 	return (0);
26497c478bd9Sstevel@tonic-gate }
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate 
26527c478bd9Sstevel@tonic-gate int
vfs_optionisset(const struct vfs * vfsp,const char * opt,char ** argp)26537c478bd9Sstevel@tonic-gate vfs_optionisset(const struct vfs *vfsp, const char *opt, char **argp)
26547c478bd9Sstevel@tonic-gate {
26557c478bd9Sstevel@tonic-gate 	int ret;
26567c478bd9Sstevel@tonic-gate 
26577c478bd9Sstevel@tonic-gate 	vfs_list_read_lock();
26587c478bd9Sstevel@tonic-gate 	ret = vfs_optionisset_nolock(&vfsp->vfs_mntopts, opt, argp);
26597c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
26607c478bd9Sstevel@tonic-gate 	return (ret);
26617c478bd9Sstevel@tonic-gate }
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate 
26647c478bd9Sstevel@tonic-gate /*
26657c478bd9Sstevel@tonic-gate  * Construct a comma separated string of the options set in the given
26667c478bd9Sstevel@tonic-gate  * mount table, return the string in the given buffer.  Return non-zero if
26677c478bd9Sstevel@tonic-gate  * the buffer would overflow.
26687c478bd9Sstevel@tonic-gate  *
26697c478bd9Sstevel@tonic-gate  * This function is *not* for general use by filesystems.
26707c478bd9Sstevel@tonic-gate  *
26717c478bd9Sstevel@tonic-gate  * Note: caller is responsible for locking the vfs list, if needed,
26727c478bd9Sstevel@tonic-gate  *       to protect mp.
26737c478bd9Sstevel@tonic-gate  */
26747c478bd9Sstevel@tonic-gate int
vfs_buildoptionstr(const mntopts_t * mp,char * buf,int len)26757c478bd9Sstevel@tonic-gate vfs_buildoptionstr(const mntopts_t *mp, char *buf, int len)
26767c478bd9Sstevel@tonic-gate {
26777c478bd9Sstevel@tonic-gate 	char *cp;
26787c478bd9Sstevel@tonic-gate 	uint_t i;
26797c478bd9Sstevel@tonic-gate 
26807c478bd9Sstevel@tonic-gate 	buf[0] = '\0';
26817c478bd9Sstevel@tonic-gate 	cp = buf;
26827c478bd9Sstevel@tonic-gate 	for (i = 0; i < mp->mo_count; i++) {
26837c478bd9Sstevel@tonic-gate 		struct mntopt *mop;
26847c478bd9Sstevel@tonic-gate 
26857c478bd9Sstevel@tonic-gate 		mop = &mp->mo_list[i];
26867c478bd9Sstevel@tonic-gate 		if (mop->mo_flags & MO_SET) {
26877c478bd9Sstevel@tonic-gate 			int optlen, comma = 0;
26887c478bd9Sstevel@tonic-gate 
26897c478bd9Sstevel@tonic-gate 			if (buf[0] != '\0')
26907c478bd9Sstevel@tonic-gate 				comma = 1;
26917c478bd9Sstevel@tonic-gate 			optlen = strlen(mop->mo_name);
26927c478bd9Sstevel@tonic-gate 			if (strlen(buf) + comma + optlen + 1 > len)
26937c478bd9Sstevel@tonic-gate 				goto err;
26947c478bd9Sstevel@tonic-gate 			if (comma)
26957c478bd9Sstevel@tonic-gate 				*cp++ = ',';
26967c478bd9Sstevel@tonic-gate 			(void) strcpy(cp, mop->mo_name);
26977c478bd9Sstevel@tonic-gate 			cp += optlen;
26987c478bd9Sstevel@tonic-gate 			/*
26997c478bd9Sstevel@tonic-gate 			 * Append option value if there is one
27007c478bd9Sstevel@tonic-gate 			 */
27017c478bd9Sstevel@tonic-gate 			if (mop->mo_arg != NULL) {
27027c478bd9Sstevel@tonic-gate 				int arglen;
27037c478bd9Sstevel@tonic-gate 
27047c478bd9Sstevel@tonic-gate 				arglen = strlen(mop->mo_arg);
27057c478bd9Sstevel@tonic-gate 				if (strlen(buf) + arglen + 2 > len)
27067c478bd9Sstevel@tonic-gate 					goto err;
27077c478bd9Sstevel@tonic-gate 				*cp++ = '=';
27087c478bd9Sstevel@tonic-gate 				(void) strcpy(cp, mop->mo_arg);
27097c478bd9Sstevel@tonic-gate 				cp += arglen;
27107c478bd9Sstevel@tonic-gate 			}
27117c478bd9Sstevel@tonic-gate 		}
27127c478bd9Sstevel@tonic-gate 	}
27137c478bd9Sstevel@tonic-gate 	return (0);
27147c478bd9Sstevel@tonic-gate err:
27157c478bd9Sstevel@tonic-gate 	return (EOVERFLOW);
27167c478bd9Sstevel@tonic-gate }
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate static void
vfs_freecancelopt(char ** moc)27197c478bd9Sstevel@tonic-gate vfs_freecancelopt(char **moc)
27207c478bd9Sstevel@tonic-gate {
27217c478bd9Sstevel@tonic-gate 	if (moc != NULL) {
27227c478bd9Sstevel@tonic-gate 		int ccnt = 0;
27237c478bd9Sstevel@tonic-gate 		char **cp;
27247c478bd9Sstevel@tonic-gate 
27257c478bd9Sstevel@tonic-gate 		for (cp = moc; *cp != NULL; cp++) {
27267c478bd9Sstevel@tonic-gate 			kmem_free(*cp, strlen(*cp) + 1);
27277c478bd9Sstevel@tonic-gate 			ccnt++;
27287c478bd9Sstevel@tonic-gate 		}
27297c478bd9Sstevel@tonic-gate 		kmem_free(moc, (ccnt + 1) * sizeof (char *));
27307c478bd9Sstevel@tonic-gate 	}
27317c478bd9Sstevel@tonic-gate }
27327c478bd9Sstevel@tonic-gate 
27337c478bd9Sstevel@tonic-gate static void
vfs_freeopt(mntopt_t * mop)27347c478bd9Sstevel@tonic-gate vfs_freeopt(mntopt_t *mop)
27357c478bd9Sstevel@tonic-gate {
27367c478bd9Sstevel@tonic-gate 	if (mop->mo_name != NULL)
27377c478bd9Sstevel@tonic-gate 		kmem_free(mop->mo_name, strlen(mop->mo_name) + 1);
27387c478bd9Sstevel@tonic-gate 
27397c478bd9Sstevel@tonic-gate 	vfs_freecancelopt(mop->mo_cancel);
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 	if (mop->mo_arg != NULL)
27427c478bd9Sstevel@tonic-gate 		kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
27437c478bd9Sstevel@tonic-gate }
27447c478bd9Sstevel@tonic-gate 
27457c478bd9Sstevel@tonic-gate /*
27467c478bd9Sstevel@tonic-gate  * Free a mount options table
27477c478bd9Sstevel@tonic-gate  *
27487c478bd9Sstevel@tonic-gate  * This function is *not* for general use by filesystems.
27497c478bd9Sstevel@tonic-gate  *
27507c478bd9Sstevel@tonic-gate  * Note: caller is responsible for locking the vfs list, if needed,
27517c478bd9Sstevel@tonic-gate  *       to protect mp.
27527c478bd9Sstevel@tonic-gate  */
27537c478bd9Sstevel@tonic-gate void
vfs_freeopttbl(mntopts_t * mp)27547c478bd9Sstevel@tonic-gate vfs_freeopttbl(mntopts_t *mp)
27557c478bd9Sstevel@tonic-gate {
27567c478bd9Sstevel@tonic-gate 	uint_t i, count;
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate 	count = mp->mo_count;
27597c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
27607c478bd9Sstevel@tonic-gate 		vfs_freeopt(&mp->mo_list[i]);
27617c478bd9Sstevel@tonic-gate 	}
27627c478bd9Sstevel@tonic-gate 	if (count) {
27637c478bd9Sstevel@tonic-gate 		kmem_free(mp->mo_list, sizeof (mntopt_t) * count);
27647c478bd9Sstevel@tonic-gate 		mp->mo_count = 0;
27657c478bd9Sstevel@tonic-gate 		mp->mo_list = NULL;
27667c478bd9Sstevel@tonic-gate 	}
27677c478bd9Sstevel@tonic-gate }
27687c478bd9Sstevel@tonic-gate 
2769df2381bfSpraks 
2770df2381bfSpraks /* ARGSUSED */
2771df2381bfSpraks static int
vfs_mntdummyread(vnode_t * vp,uio_t * uio,int ioflag,cred_t * cred,caller_context_t * ct)2772df2381bfSpraks vfs_mntdummyread(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cred,
2773406fc510SToomas Soome     caller_context_t *ct)
2774df2381bfSpraks {
2775df2381bfSpraks 	return (0);
2776df2381bfSpraks }
2777df2381bfSpraks 
2778df2381bfSpraks /* ARGSUSED */
2779df2381bfSpraks static int
vfs_mntdummywrite(vnode_t * vp,uio_t * uio,int ioflag,cred_t * cred,caller_context_t * ct)2780df2381bfSpraks vfs_mntdummywrite(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cred,
2781406fc510SToomas Soome     caller_context_t *ct)
2782df2381bfSpraks {
2783df2381bfSpraks 	return (0);
2784df2381bfSpraks }
2785df2381bfSpraks 
2786df2381bfSpraks /*
2787df2381bfSpraks  * The dummy vnode is currently used only by file events notification
2788df2381bfSpraks  * module which is just interested in the timestamps.
2789df2381bfSpraks  */
2790df2381bfSpraks /* ARGSUSED */
2791df2381bfSpraks static int
vfs_mntdummygetattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)2792da6c28aaSamw vfs_mntdummygetattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2793da6c28aaSamw     caller_context_t *ct)
2794df2381bfSpraks {
2795df2381bfSpraks 	bzero(vap, sizeof (vattr_t));
2796df2381bfSpraks 	vap->va_type = VREG;
2797df2381bfSpraks 	vap->va_nlink = 1;
2798df2381bfSpraks 	vap->va_ctime = vfs_mnttab_ctime;
2799df2381bfSpraks 	/*
2800df2381bfSpraks 	 * it is ok to just copy mtime as the time will be monotonically
2801df2381bfSpraks 	 * increasing.
2802df2381bfSpraks 	 */
2803df2381bfSpraks 	vap->va_mtime = vfs_mnttab_mtime;
2804df2381bfSpraks 	vap->va_atime = vap->va_mtime;
2805df2381bfSpraks 	return (0);
2806df2381bfSpraks }
2807df2381bfSpraks 
2808df2381bfSpraks static void
vfs_mnttabvp_setup(void)2809df2381bfSpraks vfs_mnttabvp_setup(void)
2810df2381bfSpraks {
2811df2381bfSpraks 	vnode_t *tvp;
2812df2381bfSpraks 	vnodeops_t *vfs_mntdummyvnops;
2813df2381bfSpraks 	const fs_operation_def_t mnt_dummyvnodeops_template[] = {
2814b6d49c84SToomas Soome 		VOPNAME_READ,		{ .vop_read = vfs_mntdummyread },
2815b6d49c84SToomas Soome 		VOPNAME_WRITE,		{ .vop_write = vfs_mntdummywrite },
2816df2381bfSpraks 		VOPNAME_GETATTR,	{ .vop_getattr = vfs_mntdummygetattr },
2817df2381bfSpraks 		VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
2818df2381bfSpraks 		NULL,			NULL
2819df2381bfSpraks 	};
2820df2381bfSpraks 
2821df2381bfSpraks 	if (vn_make_ops("mnttab", mnt_dummyvnodeops_template,
2822df2381bfSpraks 	    &vfs_mntdummyvnops) != 0) {
2823df2381bfSpraks 		cmn_err(CE_WARN, "vfs_mnttabvp_setup: vn_make_ops failed");
2824df2381bfSpraks 		/* Shouldn't happen, but not bad enough to panic */
2825df2381bfSpraks 		return;
2826df2381bfSpraks 	}
2827df2381bfSpraks 
2828df2381bfSpraks 	/*
2829df2381bfSpraks 	 * A global dummy vnode is allocated to represent mntfs files.
2830df2381bfSpraks 	 * The mntfs file (/etc/mnttab) can be monitored for file events
2831df2381bfSpraks 	 * and receive an event when mnttab changes. Dummy VOP calls
2832df2381bfSpraks 	 * will be made on this vnode. The file events notification module
2833df2381bfSpraks 	 * intercepts this vnode and delivers relevant events.
2834df2381bfSpraks 	 */
2835df2381bfSpraks 	tvp = vn_alloc(KM_SLEEP);
2836df2381bfSpraks 	tvp->v_flag = VNOMOUNT|VNOMAP|VNOSWAP|VNOCACHE;
2837df2381bfSpraks 	vn_setops(tvp, vfs_mntdummyvnops);
2838df2381bfSpraks 	tvp->v_type = VREG;
2839df2381bfSpraks 	/*
2840df2381bfSpraks 	 * The mnt dummy ops do not reference v_data.
2841df2381bfSpraks 	 * No other module intercepting this vnode should either.
2842df2381bfSpraks 	 * Just set it to point to itself.
2843df2381bfSpraks 	 */
2844df2381bfSpraks 	tvp->v_data = (caddr_t)tvp;
2845df2381bfSpraks 	tvp->v_vfsp = rootvfs;
2846df2381bfSpraks 	vfs_mntdummyvp = tvp;
2847df2381bfSpraks }
2848df2381bfSpraks 
2849df2381bfSpraks /*
2850df2381bfSpraks  * performs fake read/write ops
2851df2381bfSpraks  */
2852df2381bfSpraks static void
vfs_mnttab_rwop(int rw)2853df2381bfSpraks vfs_mnttab_rwop(int rw)
2854df2381bfSpraks {
2855df2381bfSpraks 	struct uio	uio;
2856df2381bfSpraks 	struct iovec	iov;
2857df2381bfSpraks 	char	buf[1];
2858df2381bfSpraks 
2859df2381bfSpraks 	if (vfs_mntdummyvp == NULL)
2860df2381bfSpraks 		return;
2861df2381bfSpraks 
2862df2381bfSpraks 	bzero(&uio, sizeof (uio));
2863df2381bfSpraks 	bzero(&iov, sizeof (iov));
2864df2381bfSpraks 	iov.iov_base = buf;
2865df2381bfSpraks 	iov.iov_len = 0;
2866df2381bfSpraks 	uio.uio_iov = &iov;
2867df2381bfSpraks 	uio.uio_iovcnt = 1;
2868df2381bfSpraks 	uio.uio_loffset = 0;
2869df2381bfSpraks 	uio.uio_segflg = UIO_SYSSPACE;
2870df2381bfSpraks 	uio.uio_resid = 0;
2871df2381bfSpraks 	if (rw) {
2872df2381bfSpraks 		(void) VOP_WRITE(vfs_mntdummyvp, &uio, 0, kcred, NULL);
2873df2381bfSpraks 	} else {
2874df2381bfSpraks 		(void) VOP_READ(vfs_mntdummyvp, &uio, 0, kcred, NULL);
2875df2381bfSpraks 	}
2876df2381bfSpraks }
2877df2381bfSpraks 
2878df2381bfSpraks /*
2879df2381bfSpraks  * Generate a write operation.
2880df2381bfSpraks  */
2881df2381bfSpraks void
vfs_mnttab_writeop(void)2882df2381bfSpraks vfs_mnttab_writeop(void)
2883df2381bfSpraks {
2884df2381bfSpraks 	vfs_mnttab_rwop(1);
2885df2381bfSpraks }
2886df2381bfSpraks 
2887df2381bfSpraks /*
2888df2381bfSpraks  * Generate a read operation.
2889df2381bfSpraks  */
2890df2381bfSpraks void
vfs_mnttab_readop(void)2891df2381bfSpraks vfs_mnttab_readop(void)
2892df2381bfSpraks {
2893df2381bfSpraks 	vfs_mnttab_rwop(0);
2894df2381bfSpraks }
2895df2381bfSpraks 
28967c478bd9Sstevel@tonic-gate /*
28977c478bd9Sstevel@tonic-gate  * Free any mnttab information recorded in the vfs struct.
28987c478bd9Sstevel@tonic-gate  * The vfs must not be on the vfs list.
28997c478bd9Sstevel@tonic-gate  */
29007c478bd9Sstevel@tonic-gate static void
vfs_freemnttab(struct vfs * vfsp)29017c478bd9Sstevel@tonic-gate vfs_freemnttab(struct vfs *vfsp)
29027c478bd9Sstevel@tonic-gate {
29037c478bd9Sstevel@tonic-gate 	ASSERT(!VFS_ON_LIST(vfsp));
29047c478bd9Sstevel@tonic-gate 
29057c478bd9Sstevel@tonic-gate 	/*
29067c478bd9Sstevel@tonic-gate 	 * Free device and mount point information
29077c478bd9Sstevel@tonic-gate 	 */
29087c478bd9Sstevel@tonic-gate 	if (vfsp->vfs_mntpt != NULL) {
29097c478bd9Sstevel@tonic-gate 		refstr_rele(vfsp->vfs_mntpt);
29107c478bd9Sstevel@tonic-gate 		vfsp->vfs_mntpt = NULL;
29117c478bd9Sstevel@tonic-gate 	}
29127c478bd9Sstevel@tonic-gate 	if (vfsp->vfs_resource != NULL) {
29137c478bd9Sstevel@tonic-gate 		refstr_rele(vfsp->vfs_resource);
29147c478bd9Sstevel@tonic-gate 		vfsp->vfs_resource = NULL;
29157c478bd9Sstevel@tonic-gate 	}
29167c478bd9Sstevel@tonic-gate 	/*
29177c478bd9Sstevel@tonic-gate 	 * Now free mount options information
29187c478bd9Sstevel@tonic-gate 	 */
29197c478bd9Sstevel@tonic-gate 	vfs_freeopttbl(&vfsp->vfs_mntopts);
29207c478bd9Sstevel@tonic-gate }
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate /*
29237c478bd9Sstevel@tonic-gate  * Return the last mnttab modification time
29247c478bd9Sstevel@tonic-gate  */
29257c478bd9Sstevel@tonic-gate void
vfs_mnttab_modtime(timespec_t * ts)29267c478bd9Sstevel@tonic-gate vfs_mnttab_modtime(timespec_t *ts)
29277c478bd9Sstevel@tonic-gate {
29287c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&vfslist));
29297c478bd9Sstevel@tonic-gate 	*ts = vfs_mnttab_mtime;
29307c478bd9Sstevel@tonic-gate }
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate /*
29337c478bd9Sstevel@tonic-gate  * See if mnttab is changed
29347c478bd9Sstevel@tonic-gate  */
29357c478bd9Sstevel@tonic-gate void
vfs_mnttab_poll(timespec_t * old,struct pollhead ** phpp)29367c478bd9Sstevel@tonic-gate vfs_mnttab_poll(timespec_t *old, struct pollhead **phpp)
29377c478bd9Sstevel@tonic-gate {
29387c478bd9Sstevel@tonic-gate 	int changed;
29397c478bd9Sstevel@tonic-gate 
29407c478bd9Sstevel@tonic-gate 	*phpp = (struct pollhead *)NULL;
29417c478bd9Sstevel@tonic-gate 
29427c478bd9Sstevel@tonic-gate 	/*
29437c478bd9Sstevel@tonic-gate 	 * Note: don't grab vfs list lock before accessing vfs_mnttab_mtime.
29447c478bd9Sstevel@tonic-gate 	 * Can lead to deadlock against vfs_mnttab_modtimeupd(). It is safe
29457c478bd9Sstevel@tonic-gate 	 * to not grab the vfs list lock because tv_sec is monotonically
29467c478bd9Sstevel@tonic-gate 	 * increasing.
29477c478bd9Sstevel@tonic-gate 	 */
29487c478bd9Sstevel@tonic-gate 
29497c478bd9Sstevel@tonic-gate 	changed = (old->tv_nsec != vfs_mnttab_mtime.tv_nsec) ||
29507c478bd9Sstevel@tonic-gate 	    (old->tv_sec != vfs_mnttab_mtime.tv_sec);
29517c478bd9Sstevel@tonic-gate 	if (!changed) {
29527c478bd9Sstevel@tonic-gate 		*phpp = &vfs_pollhd;
29537c478bd9Sstevel@tonic-gate 	}
29547c478bd9Sstevel@tonic-gate }
29557c478bd9Sstevel@tonic-gate 
2956835ee219SRobert Harris /* Provide a unique and monotonically-increasing timestamp. */
2957835ee219SRobert Harris void
vfs_mono_time(timespec_t * ts)2958835ee219SRobert Harris vfs_mono_time(timespec_t *ts)
2959835ee219SRobert Harris {
2960835ee219SRobert Harris 	static volatile hrtime_t hrt;		/* The saved time. */
2961835ee219SRobert Harris 	hrtime_t	newhrt, oldhrt;		/* For effecting the CAS. */
2962835ee219SRobert Harris 	timespec_t	newts;
2963835ee219SRobert Harris 
29644ba8a214SRobert Harris 	/*
29654ba8a214SRobert Harris 	 * Try gethrestime() first, but be prepared to fabricate a sensible
29664ba8a214SRobert Harris 	 * answer at the first sign of any trouble.
29674ba8a214SRobert Harris 	 */
2968835ee219SRobert Harris 	gethrestime(&newts);
2969835ee219SRobert Harris 	newhrt = ts2hrt(&newts);
29704ba8a214SRobert Harris 	for (;;) {
2971835ee219SRobert Harris 		oldhrt = hrt;
29724ba8a214SRobert Harris 		if (newhrt <= hrt)
29734ba8a214SRobert Harris 			newhrt = hrt + 1;
297475d94465SJosef 'Jeff' Sipek 		if (atomic_cas_64((uint64_t *)&hrt, oldhrt, newhrt) == oldhrt)
29754ba8a214SRobert Harris 			break;
29764ba8a214SRobert Harris 	}
2977835ee219SRobert Harris 	hrt2ts(newhrt, ts);
2978835ee219SRobert Harris }
2979835ee219SRobert Harris 
29807c478bd9Sstevel@tonic-gate /*
29817c478bd9Sstevel@tonic-gate  * Update the mnttab modification time and wake up any waiters for
29827c478bd9Sstevel@tonic-gate  * mnttab changes
29837c478bd9Sstevel@tonic-gate  */
29847c478bd9Sstevel@tonic-gate void
vfs_mnttab_modtimeupd()29857c478bd9Sstevel@tonic-gate vfs_mnttab_modtimeupd()
29867c478bd9Sstevel@tonic-gate {
29874ba8a214SRobert Harris 	hrtime_t oldhrt, newhrt;
29884ba8a214SRobert Harris 
29897c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&vfslist));
29904ba8a214SRobert Harris 	oldhrt = ts2hrt(&vfs_mnttab_mtime);
29914ba8a214SRobert Harris 	gethrestime(&vfs_mnttab_mtime);
29924ba8a214SRobert Harris 	newhrt = ts2hrt(&vfs_mnttab_mtime);
29934ba8a214SRobert Harris 	if (oldhrt == (hrtime_t)0)
29947c478bd9Sstevel@tonic-gate 		vfs_mnttab_ctime = vfs_mnttab_mtime;
29954ba8a214SRobert Harris 	/*
29964ba8a214SRobert Harris 	 * Attempt to provide unique mtime (like uniqtime but not).
29974ba8a214SRobert Harris 	 */
29984ba8a214SRobert Harris 	if (newhrt == oldhrt) {
29994ba8a214SRobert Harris 		newhrt++;
30004ba8a214SRobert Harris 		hrt2ts(newhrt, &vfs_mnttab_mtime);
30014ba8a214SRobert Harris 	}
30027c478bd9Sstevel@tonic-gate 	pollwakeup(&vfs_pollhd, (short)POLLRDBAND);
3003df2381bfSpraks 	vfs_mnttab_writeop();
30047c478bd9Sstevel@tonic-gate }
30057c478bd9Sstevel@tonic-gate 
30067c478bd9Sstevel@tonic-gate int
dounmount(struct vfs * vfsp,int flag,cred_t * cr)30077c478bd9Sstevel@tonic-gate dounmount(struct vfs *vfsp, int flag, cred_t *cr)
30087c478bd9Sstevel@tonic-gate {
30097c478bd9Sstevel@tonic-gate 	vnode_t *coveredvp;
30107c478bd9Sstevel@tonic-gate 	int error;
30115a59a8b3Srsb 	extern void teardown_vopstats(vfs_t *);
30127c478bd9Sstevel@tonic-gate 
30137c478bd9Sstevel@tonic-gate 	/*
30147c478bd9Sstevel@tonic-gate 	 * Get covered vnode. This will be NULL if the vfs is not linked
30157c478bd9Sstevel@tonic-gate 	 * into the file system name space (i.e., domount() with MNT_NOSPICE).
30167c478bd9Sstevel@tonic-gate 	 */
30177c478bd9Sstevel@tonic-gate 	coveredvp = vfsp->vfs_vnodecovered;
30187c478bd9Sstevel@tonic-gate 	ASSERT(coveredvp == NULL || vn_vfswlock_held(coveredvp));
30197c478bd9Sstevel@tonic-gate 
30207c478bd9Sstevel@tonic-gate 	/*
30217c478bd9Sstevel@tonic-gate 	 * Purge all dnlc entries for this vfs.
30227c478bd9Sstevel@tonic-gate 	 */
30237c478bd9Sstevel@tonic-gate 	(void) dnlc_purge_vfsp(vfsp, 0);
30247c478bd9Sstevel@tonic-gate 
30257c478bd9Sstevel@tonic-gate 	/* For forcible umount, skip VFS_SYNC() since it may hang */
30267c478bd9Sstevel@tonic-gate 	if ((flag & MS_FORCE) == 0)
30277c478bd9Sstevel@tonic-gate 		(void) VFS_SYNC(vfsp, 0, cr);
30287c478bd9Sstevel@tonic-gate 
30297c478bd9Sstevel@tonic-gate 	/*
30307c478bd9Sstevel@tonic-gate 	 * Lock the vfs to maintain fs status quo during unmount.  This
30317c478bd9Sstevel@tonic-gate 	 * has to be done after the sync because ufs_update tries to acquire
30327c478bd9Sstevel@tonic-gate 	 * the vfs_reflock.
30337c478bd9Sstevel@tonic-gate 	 */
30347c478bd9Sstevel@tonic-gate 	vfs_lock_wait(vfsp);
30357c478bd9Sstevel@tonic-gate 
30367c478bd9Sstevel@tonic-gate 	if (error = VFS_UNMOUNT(vfsp, flag, cr)) {
30377c478bd9Sstevel@tonic-gate 		vfs_unlock(vfsp);
30387c478bd9Sstevel@tonic-gate 		if (coveredvp != NULL)
30397c478bd9Sstevel@tonic-gate 			vn_vfsunlock(coveredvp);
30407c478bd9Sstevel@tonic-gate 	} else if (coveredvp != NULL) {
30415a59a8b3Srsb 		teardown_vopstats(vfsp);
30427c478bd9Sstevel@tonic-gate 		/*
30437c478bd9Sstevel@tonic-gate 		 * vfs_remove() will do a VN_RELE(vfsp->vfs_vnodecovered)
30447c478bd9Sstevel@tonic-gate 		 * when it frees vfsp so we do a VN_HOLD() so we can
30457c478bd9Sstevel@tonic-gate 		 * continue to use coveredvp afterwards.
30467c478bd9Sstevel@tonic-gate 		 */
30477c478bd9Sstevel@tonic-gate 		VN_HOLD(coveredvp);
30487c478bd9Sstevel@tonic-gate 		vfs_remove(vfsp);
30497c478bd9Sstevel@tonic-gate 		vn_vfsunlock(coveredvp);
30507c478bd9Sstevel@tonic-gate 		VN_RELE(coveredvp);
30517c478bd9Sstevel@tonic-gate 	} else {
30525a59a8b3Srsb 		teardown_vopstats(vfsp);
30537c478bd9Sstevel@tonic-gate 		/*
30547c478bd9Sstevel@tonic-gate 		 * Release the reference to vfs that is not linked
30557c478bd9Sstevel@tonic-gate 		 * into the name space.
30567c478bd9Sstevel@tonic-gate 		 */
30577c478bd9Sstevel@tonic-gate 		vfs_unlock(vfsp);
30587c478bd9Sstevel@tonic-gate 		VFS_RELE(vfsp);
30597c478bd9Sstevel@tonic-gate 	}
30607c478bd9Sstevel@tonic-gate 	return (error);
30617c478bd9Sstevel@tonic-gate }
30627c478bd9Sstevel@tonic-gate 
30637c478bd9Sstevel@tonic-gate 
30647c478bd9Sstevel@tonic-gate /*
30657c478bd9Sstevel@tonic-gate  * Vfs_unmountall() is called by uadmin() to unmount all
30667c478bd9Sstevel@tonic-gate  * mounted file systems (except the root file system) during shutdown.
30677c478bd9Sstevel@tonic-gate  * It follows the existing locking protocol when traversing the vfs list
30687c478bd9Sstevel@tonic-gate  * to sync and unmount vfses. Even though there should be no
30697c478bd9Sstevel@tonic-gate  * other thread running while the system is shutting down, it is prudent
30707c478bd9Sstevel@tonic-gate  * to still follow the locking protocol.
30717c478bd9Sstevel@tonic-gate  */
30727c478bd9Sstevel@tonic-gate void
vfs_unmountall(void)30737c478bd9Sstevel@tonic-gate vfs_unmountall(void)
30747c478bd9Sstevel@tonic-gate {
30757c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
30767c478bd9Sstevel@tonic-gate 	struct vfs *prev_vfsp = NULL;
30777c478bd9Sstevel@tonic-gate 	int error;
30787c478bd9Sstevel@tonic-gate 
30797c478bd9Sstevel@tonic-gate 	/*
30807c478bd9Sstevel@tonic-gate 	 * Toss all dnlc entries now so that the per-vfs sync
30817c478bd9Sstevel@tonic-gate 	 * and unmount operations don't have to slog through
30827c478bd9Sstevel@tonic-gate 	 * a bunch of uninteresting vnodes over and over again.
30837c478bd9Sstevel@tonic-gate 	 */
30847c478bd9Sstevel@tonic-gate 	dnlc_purge();
30857c478bd9Sstevel@tonic-gate 
30867c478bd9Sstevel@tonic-gate 	vfs_list_lock();
30877c478bd9Sstevel@tonic-gate 	for (vfsp = rootvfs->vfs_prev; vfsp != rootvfs; vfsp = prev_vfsp) {
30887c478bd9Sstevel@tonic-gate 		prev_vfsp = vfsp->vfs_prev;
30897c478bd9Sstevel@tonic-gate 
30907c478bd9Sstevel@tonic-gate 		if (vfs_lock(vfsp) != 0)
30917c478bd9Sstevel@tonic-gate 			continue;
30927c478bd9Sstevel@tonic-gate 		error = vn_vfswlock(vfsp->vfs_vnodecovered);
30937c478bd9Sstevel@tonic-gate 		vfs_unlock(vfsp);
30947c478bd9Sstevel@tonic-gate 		if (error)
30957c478bd9Sstevel@tonic-gate 			continue;
30967c478bd9Sstevel@tonic-gate 
30977c478bd9Sstevel@tonic-gate 		vfs_list_unlock();
30987c478bd9Sstevel@tonic-gate 
30997c478bd9Sstevel@tonic-gate 		(void) VFS_SYNC(vfsp, SYNC_CLOSE, CRED());
31007c478bd9Sstevel@tonic-gate 		(void) dounmount(vfsp, 0, CRED());
31017c478bd9Sstevel@tonic-gate 
31027c478bd9Sstevel@tonic-gate 		/*
31037c478bd9Sstevel@tonic-gate 		 * Since we dropped the vfslist lock above we must
31047c478bd9Sstevel@tonic-gate 		 * verify that next_vfsp still exists, else start over.
31057c478bd9Sstevel@tonic-gate 		 */
31067c478bd9Sstevel@tonic-gate 		vfs_list_lock();
31077c478bd9Sstevel@tonic-gate 		for (vfsp = rootvfs->vfs_prev;
3108b1b8ab34Slling 		    vfsp != rootvfs; vfsp = vfsp->vfs_prev)
31097c478bd9Sstevel@tonic-gate 			if (vfsp == prev_vfsp)
31107c478bd9Sstevel@tonic-gate 				break;
31117c478bd9Sstevel@tonic-gate 		if (vfsp == rootvfs && prev_vfsp != rootvfs)
31127c478bd9Sstevel@tonic-gate 			prev_vfsp = rootvfs->vfs_prev;
31137c478bd9Sstevel@tonic-gate 	}
31147c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
31157c478bd9Sstevel@tonic-gate }
31167c478bd9Sstevel@tonic-gate 
31177c478bd9Sstevel@tonic-gate /*
31187c478bd9Sstevel@tonic-gate  * Called to add an entry to the end of the vfs mount in progress list
31197c478bd9Sstevel@tonic-gate  */
31207c478bd9Sstevel@tonic-gate void
vfs_addmip(dev_t dev,struct vfs * vfsp)31217c478bd9Sstevel@tonic-gate vfs_addmip(dev_t dev, struct vfs *vfsp)
31227c478bd9Sstevel@tonic-gate {
31237c478bd9Sstevel@tonic-gate 	struct ipmnt *mipp;
31247c478bd9Sstevel@tonic-gate 
31257c478bd9Sstevel@tonic-gate 	mipp = (struct ipmnt *)kmem_alloc(sizeof (struct ipmnt), KM_SLEEP);
31267c478bd9Sstevel@tonic-gate 	mipp->mip_next = NULL;
31277c478bd9Sstevel@tonic-gate 	mipp->mip_dev = dev;
31287c478bd9Sstevel@tonic-gate 	mipp->mip_vfsp = vfsp;
31297c478bd9Sstevel@tonic-gate 	mutex_enter(&vfs_miplist_mutex);
31307c478bd9Sstevel@tonic-gate 	if (vfs_miplist_end != NULL)
31317c478bd9Sstevel@tonic-gate 		vfs_miplist_end->mip_next = mipp;
31327c478bd9Sstevel@tonic-gate 	else
31337c478bd9Sstevel@tonic-gate 		vfs_miplist = mipp;
31347c478bd9Sstevel@tonic-gate 	vfs_miplist_end = mipp;
31357c478bd9Sstevel@tonic-gate 	mutex_exit(&vfs_miplist_mutex);
31367c478bd9Sstevel@tonic-gate }
31377c478bd9Sstevel@tonic-gate 
31387c478bd9Sstevel@tonic-gate /*
31397c478bd9Sstevel@tonic-gate  * Called to remove an entry from the mount in progress list
31407c478bd9Sstevel@tonic-gate  * Either because the mount completed or it failed.
31417c478bd9Sstevel@tonic-gate  */
31427c478bd9Sstevel@tonic-gate void
vfs_delmip(struct vfs * vfsp)31437c478bd9Sstevel@tonic-gate vfs_delmip(struct vfs *vfsp)
31447c478bd9Sstevel@tonic-gate {
31457c478bd9Sstevel@tonic-gate 	struct ipmnt *mipp, *mipprev;
31467c478bd9Sstevel@tonic-gate 
31477c478bd9Sstevel@tonic-gate 	mutex_enter(&vfs_miplist_mutex);
31487c478bd9Sstevel@tonic-gate 	mipprev = NULL;
31497c478bd9Sstevel@tonic-gate 	for (mipp = vfs_miplist;
3150b1b8ab34Slling 	    mipp && mipp->mip_vfsp != vfsp; mipp = mipp->mip_next) {
31517c478bd9Sstevel@tonic-gate 		mipprev = mipp;
31527c478bd9Sstevel@tonic-gate 	}
31537c478bd9Sstevel@tonic-gate 	if (mipp == NULL)
31547c478bd9Sstevel@tonic-gate 		return; /* shouldn't happen */
31557c478bd9Sstevel@tonic-gate 	if (mipp == vfs_miplist_end)
31567c478bd9Sstevel@tonic-gate 		vfs_miplist_end = mipprev;
31577c478bd9Sstevel@tonic-gate 	if (mipprev == NULL)
31587c478bd9Sstevel@tonic-gate 		vfs_miplist = mipp->mip_next;
31597c478bd9Sstevel@tonic-gate 	else
31607c478bd9Sstevel@tonic-gate 		mipprev->mip_next = mipp->mip_next;
31617c478bd9Sstevel@tonic-gate 	mutex_exit(&vfs_miplist_mutex);
31627c478bd9Sstevel@tonic-gate 	kmem_free(mipp, sizeof (struct ipmnt));
31637c478bd9Sstevel@tonic-gate }
31647c478bd9Sstevel@tonic-gate 
31657c478bd9Sstevel@tonic-gate /*
31667c478bd9Sstevel@tonic-gate  * vfs_add is called by a specific filesystem's mount routine to add
31677c478bd9Sstevel@tonic-gate  * the new vfs into the vfs list/hash and to cover the mounted-on vnode.
31687c478bd9Sstevel@tonic-gate  * The vfs should already have been locked by the caller.
31697c478bd9Sstevel@tonic-gate  *
31707c478bd9Sstevel@tonic-gate  * coveredvp is NULL if this is the root.
31717c478bd9Sstevel@tonic-gate  */
31727c478bd9Sstevel@tonic-gate void
vfs_add(vnode_t * coveredvp,struct vfs * vfsp,int mflag)31737c478bd9Sstevel@tonic-gate vfs_add(vnode_t *coveredvp, struct vfs *vfsp, int mflag)
31747c478bd9Sstevel@tonic-gate {
31757c478bd9Sstevel@tonic-gate 	int newflag;
31767c478bd9Sstevel@tonic-gate 
31777c478bd9Sstevel@tonic-gate 	ASSERT(vfs_lock_held(vfsp));
31787c478bd9Sstevel@tonic-gate 	VFS_HOLD(vfsp);
31797c478bd9Sstevel@tonic-gate 	newflag = vfsp->vfs_flag;
31807c478bd9Sstevel@tonic-gate 	if (mflag & MS_RDONLY)
31817c478bd9Sstevel@tonic-gate 		newflag |= VFS_RDONLY;
31827c478bd9Sstevel@tonic-gate 	else
31837c478bd9Sstevel@tonic-gate 		newflag &= ~VFS_RDONLY;
31847c478bd9Sstevel@tonic-gate 	if (mflag & MS_NOSUID)
31857c478bd9Sstevel@tonic-gate 		newflag |= (VFS_NOSETUID|VFS_NODEVICES);
31867c478bd9Sstevel@tonic-gate 	else
31877c478bd9Sstevel@tonic-gate 		newflag &= ~(VFS_NOSETUID|VFS_NODEVICES);
31887c478bd9Sstevel@tonic-gate 	if (mflag & MS_NOMNTTAB)
31897c478bd9Sstevel@tonic-gate 		newflag |= VFS_NOMNTTAB;
31907c478bd9Sstevel@tonic-gate 	else
31917c478bd9Sstevel@tonic-gate 		newflag &= ~VFS_NOMNTTAB;
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate 	if (coveredvp != NULL) {
31947c478bd9Sstevel@tonic-gate 		ASSERT(vn_vfswlock_held(coveredvp));
31957c478bd9Sstevel@tonic-gate 		coveredvp->v_vfsmountedhere = vfsp;
31967c478bd9Sstevel@tonic-gate 		VN_HOLD(coveredvp);
31977c478bd9Sstevel@tonic-gate 	}
31987c478bd9Sstevel@tonic-gate 	vfsp->vfs_vnodecovered = coveredvp;
31997c478bd9Sstevel@tonic-gate 	vfsp->vfs_flag = newflag;
32007c478bd9Sstevel@tonic-gate 
32017c478bd9Sstevel@tonic-gate 	vfs_list_add(vfsp);
32027c478bd9Sstevel@tonic-gate }
32037c478bd9Sstevel@tonic-gate 
32047c478bd9Sstevel@tonic-gate /*
32057c478bd9Sstevel@tonic-gate  * Remove a vfs from the vfs list, null out the pointer from the
32067c478bd9Sstevel@tonic-gate  * covered vnode to the vfs (v_vfsmountedhere), and null out the pointer
32077c478bd9Sstevel@tonic-gate  * from the vfs to the covered vnode (vfs_vnodecovered). Release the
32087c478bd9Sstevel@tonic-gate  * reference to the vfs and to the covered vnode.
32097c478bd9Sstevel@tonic-gate  *
32107c478bd9Sstevel@tonic-gate  * Called from dounmount after it's confirmed with the file system
32117c478bd9Sstevel@tonic-gate  * that the unmount is legal.
32127c478bd9Sstevel@tonic-gate  */
32137c478bd9Sstevel@tonic-gate void
vfs_remove(struct vfs * vfsp)32147c478bd9Sstevel@tonic-gate vfs_remove(struct vfs *vfsp)
32157c478bd9Sstevel@tonic-gate {
32167c478bd9Sstevel@tonic-gate 	vnode_t *vp;
32177c478bd9Sstevel@tonic-gate 
32187c478bd9Sstevel@tonic-gate 	ASSERT(vfs_lock_held(vfsp));
32197c478bd9Sstevel@tonic-gate 
32207c478bd9Sstevel@tonic-gate 	/*
32217c478bd9Sstevel@tonic-gate 	 * Can't unmount root.  Should never happen because fs will
32227c478bd9Sstevel@tonic-gate 	 * be busy.
32237c478bd9Sstevel@tonic-gate 	 */
32247c478bd9Sstevel@tonic-gate 	if (vfsp == rootvfs)
3225ae115bc7Smrj 		panic("vfs_remove: unmounting root");
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate 	vfs_list_remove(vfsp);
32287c478bd9Sstevel@tonic-gate 
32297c478bd9Sstevel@tonic-gate 	/*
32307c478bd9Sstevel@tonic-gate 	 * Unhook from the file system name space.
32317c478bd9Sstevel@tonic-gate 	 */
32327c478bd9Sstevel@tonic-gate 	vp = vfsp->vfs_vnodecovered;
32337c478bd9Sstevel@tonic-gate 	ASSERT(vn_vfswlock_held(vp));
32347c478bd9Sstevel@tonic-gate 	vp->v_vfsmountedhere = NULL;
32357c478bd9Sstevel@tonic-gate 	vfsp->vfs_vnodecovered = NULL;
32367c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
32377c478bd9Sstevel@tonic-gate 
32387c478bd9Sstevel@tonic-gate 	/*
32397c478bd9Sstevel@tonic-gate 	 * Release lock and wakeup anybody waiting.
32407c478bd9Sstevel@tonic-gate 	 */
32417c478bd9Sstevel@tonic-gate 	vfs_unlock(vfsp);
32427c478bd9Sstevel@tonic-gate 	VFS_RELE(vfsp);
32437c478bd9Sstevel@tonic-gate }
32447c478bd9Sstevel@tonic-gate 
32457c478bd9Sstevel@tonic-gate /*
32467c478bd9Sstevel@tonic-gate  * Lock a filesystem to prevent access to it while mounting,
32477c478bd9Sstevel@tonic-gate  * unmounting and syncing.  Return EBUSY immediately if lock
32487c478bd9Sstevel@tonic-gate  * can't be acquired.
32497c478bd9Sstevel@tonic-gate  */
32507c478bd9Sstevel@tonic-gate int
vfs_lock(vfs_t * vfsp)32517c478bd9Sstevel@tonic-gate vfs_lock(vfs_t *vfsp)
32527c478bd9Sstevel@tonic-gate {
32537c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
32547c478bd9Sstevel@tonic-gate 
32557c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vfsp);
32567c478bd9Sstevel@tonic-gate 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER))
32577c478bd9Sstevel@tonic-gate 		return (0);
32587c478bd9Sstevel@tonic-gate 
32597c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
32607c478bd9Sstevel@tonic-gate 	return (EBUSY);
32617c478bd9Sstevel@tonic-gate }
32627c478bd9Sstevel@tonic-gate 
32637c478bd9Sstevel@tonic-gate int
vfs_rlock(vfs_t * vfsp)32647c478bd9Sstevel@tonic-gate vfs_rlock(vfs_t *vfsp)
32657c478bd9Sstevel@tonic-gate {
32667c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
32677c478bd9Sstevel@tonic-gate 
32687c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vfsp);
32697c478bd9Sstevel@tonic-gate 
32707c478bd9Sstevel@tonic-gate 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER))
32717c478bd9Sstevel@tonic-gate 		return (0);
32727c478bd9Sstevel@tonic-gate 
32737c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
32747c478bd9Sstevel@tonic-gate 	return (EBUSY);
32757c478bd9Sstevel@tonic-gate }
32767c478bd9Sstevel@tonic-gate 
32777c478bd9Sstevel@tonic-gate void
vfs_lock_wait(vfs_t * vfsp)32787c478bd9Sstevel@tonic-gate vfs_lock_wait(vfs_t *vfsp)
32797c478bd9Sstevel@tonic-gate {
32807c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
32817c478bd9Sstevel@tonic-gate 
32827c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vfsp);
32837c478bd9Sstevel@tonic-gate 	rwst_enter(&vpvfsentry->ve_lock, RW_WRITER);
32847c478bd9Sstevel@tonic-gate }
32857c478bd9Sstevel@tonic-gate 
32867c478bd9Sstevel@tonic-gate void
vfs_rlock_wait(vfs_t * vfsp)32877c478bd9Sstevel@tonic-gate vfs_rlock_wait(vfs_t *vfsp)
32887c478bd9Sstevel@tonic-gate {
32897c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
32907c478bd9Sstevel@tonic-gate 
32917c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vfsp);
32927c478bd9Sstevel@tonic-gate 	rwst_enter(&vpvfsentry->ve_lock, RW_READER);
32937c478bd9Sstevel@tonic-gate }
32947c478bd9Sstevel@tonic-gate 
32957c478bd9Sstevel@tonic-gate /*
32967c478bd9Sstevel@tonic-gate  * Unlock a locked filesystem.
32977c478bd9Sstevel@tonic-gate  */
32987c478bd9Sstevel@tonic-gate void
vfs_unlock(vfs_t * vfsp)32997c478bd9Sstevel@tonic-gate vfs_unlock(vfs_t *vfsp)
33007c478bd9Sstevel@tonic-gate {
33017c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
33027c478bd9Sstevel@tonic-gate 
33037c478bd9Sstevel@tonic-gate 	/*
33047c478bd9Sstevel@tonic-gate 	 * vfs_unlock will mimic sema_v behaviour to fix 4748018.
33057c478bd9Sstevel@tonic-gate 	 * And these changes should remain for the patch changes as it is.
33067c478bd9Sstevel@tonic-gate 	 */
33077c478bd9Sstevel@tonic-gate 	if (panicstr)
33087c478bd9Sstevel@tonic-gate 		return;
33097c478bd9Sstevel@tonic-gate 
33107c478bd9Sstevel@tonic-gate 	/*
33117c478bd9Sstevel@tonic-gate 	 * ve_refcount needs to be dropped twice here.
33127c478bd9Sstevel@tonic-gate 	 * 1. To release refernce after a call to vfs_locks_getlock()
33137c478bd9Sstevel@tonic-gate 	 * 2. To release the reference from the locking routines like
33147c478bd9Sstevel@tonic-gate 	 *    vfs_rlock_wait/vfs_wlock_wait/vfs_wlock etc,.
33157c478bd9Sstevel@tonic-gate 	 */
33167c478bd9Sstevel@tonic-gate 
33177c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vfsp);
33187c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
33197c478bd9Sstevel@tonic-gate 
33207c478bd9Sstevel@tonic-gate 	rwst_exit(&vpvfsentry->ve_lock);
33217c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
33227c478bd9Sstevel@tonic-gate }
33237c478bd9Sstevel@tonic-gate 
33247c478bd9Sstevel@tonic-gate /*
33257c478bd9Sstevel@tonic-gate  * Utility routine that allows a filesystem to construct its
33267c478bd9Sstevel@tonic-gate  * fsid in "the usual way" - by munging some underlying dev_t and
33277c478bd9Sstevel@tonic-gate  * the filesystem type number into the 64-bit fsid.  Note that
33287c478bd9Sstevel@tonic-gate  * this implicitly relies on dev_t persistence to make filesystem
33297c478bd9Sstevel@tonic-gate  * id's persistent.
33307c478bd9Sstevel@tonic-gate  *
33317c478bd9Sstevel@tonic-gate  * There's nothing to prevent an individual fs from constructing its
33327c478bd9Sstevel@tonic-gate  * fsid in a different way, and indeed they should.
33337c478bd9Sstevel@tonic-gate  *
33347c478bd9Sstevel@tonic-gate  * Since we want fsids to be 32-bit quantities (so that they can be
33357c478bd9Sstevel@tonic-gate  * exported identically by either 32-bit or 64-bit APIs, as well as
33367c478bd9Sstevel@tonic-gate  * the fact that fsid's are "known" to NFS), we compress the device
33377c478bd9Sstevel@tonic-gate  * number given down to 32-bits, and panic if that isn't possible.
33387c478bd9Sstevel@tonic-gate  */
33397c478bd9Sstevel@tonic-gate void
vfs_make_fsid(fsid_t * fsi,dev_t dev,int val)33407c478bd9Sstevel@tonic-gate vfs_make_fsid(fsid_t *fsi, dev_t dev, int val)
33417c478bd9Sstevel@tonic-gate {
33427c478bd9Sstevel@tonic-gate 	if (!cmpldev((dev32_t *)&fsi->val[0], dev))
33437c478bd9Sstevel@tonic-gate 		panic("device number too big for fsid!");
33447c478bd9Sstevel@tonic-gate 	fsi->val[1] = val;
33457c478bd9Sstevel@tonic-gate }
33467c478bd9Sstevel@tonic-gate 
33477c478bd9Sstevel@tonic-gate int
vfs_lock_held(vfs_t * vfsp)33487c478bd9Sstevel@tonic-gate vfs_lock_held(vfs_t *vfsp)
33497c478bd9Sstevel@tonic-gate {
33507c478bd9Sstevel@tonic-gate 	int held;
33517c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
33527c478bd9Sstevel@tonic-gate 
33537c478bd9Sstevel@tonic-gate 	/*
33547c478bd9Sstevel@tonic-gate 	 * vfs_lock_held will mimic sema_held behaviour
33557c478bd9Sstevel@tonic-gate 	 * if panicstr is set. And these changes should remain
33567c478bd9Sstevel@tonic-gate 	 * for the patch changes as it is.
33577c478bd9Sstevel@tonic-gate 	 */
33587c478bd9Sstevel@tonic-gate 	if (panicstr)
33597c478bd9Sstevel@tonic-gate 		return (1);
33607c478bd9Sstevel@tonic-gate 
33617c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vfsp);
33627c478bd9Sstevel@tonic-gate 	held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER);
33637c478bd9Sstevel@tonic-gate 
33647c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
33657c478bd9Sstevel@tonic-gate 	return (held);
33667c478bd9Sstevel@tonic-gate }
33677c478bd9Sstevel@tonic-gate 
33687c478bd9Sstevel@tonic-gate struct _kthread *
vfs_lock_owner(vfs_t * vfsp)33697c478bd9Sstevel@tonic-gate vfs_lock_owner(vfs_t *vfsp)
33707c478bd9Sstevel@tonic-gate {
33717c478bd9Sstevel@tonic-gate 	struct _kthread *owner;
33727c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
33737c478bd9Sstevel@tonic-gate 
33747c478bd9Sstevel@tonic-gate 	/*
33757c478bd9Sstevel@tonic-gate 	 * vfs_wlock_held will mimic sema_held behaviour
33767c478bd9Sstevel@tonic-gate 	 * if panicstr is set. And these changes should remain
33777c478bd9Sstevel@tonic-gate 	 * for the patch changes as it is.
33787c478bd9Sstevel@tonic-gate 	 */
33797c478bd9Sstevel@tonic-gate 	if (panicstr)
33807c478bd9Sstevel@tonic-gate 		return (NULL);
33817c478bd9Sstevel@tonic-gate 
33827c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vfsp);
33837c478bd9Sstevel@tonic-gate 	owner = rwst_owner(&vpvfsentry->ve_lock);
33847c478bd9Sstevel@tonic-gate 
33857c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
33867c478bd9Sstevel@tonic-gate 	return (owner);
33877c478bd9Sstevel@tonic-gate }
33887c478bd9Sstevel@tonic-gate 
33897c478bd9Sstevel@tonic-gate /*
33907c478bd9Sstevel@tonic-gate  * vfs list locking.
33917c478bd9Sstevel@tonic-gate  *
33927c478bd9Sstevel@tonic-gate  * Rather than manipulate the vfslist lock directly, we abstract into lock
33937c478bd9Sstevel@tonic-gate  * and unlock routines to allow the locking implementation to be changed for
33947c478bd9Sstevel@tonic-gate  * clustering.
33957c478bd9Sstevel@tonic-gate  *
33967c478bd9Sstevel@tonic-gate  * Whenever the vfs list is modified through its hash links, the overall list
33977c478bd9Sstevel@tonic-gate  * lock must be obtained before locking the relevant hash bucket.  But to see
33987c478bd9Sstevel@tonic-gate  * whether a given vfs is on the list, it suffices to obtain the lock for the
33997c478bd9Sstevel@tonic-gate  * hash bucket without getting the overall list lock.  (See getvfs() below.)
34007c478bd9Sstevel@tonic-gate  */
34017c478bd9Sstevel@tonic-gate 
34027c478bd9Sstevel@tonic-gate void
vfs_list_lock()34037c478bd9Sstevel@tonic-gate vfs_list_lock()
34047c478bd9Sstevel@tonic-gate {
34057c478bd9Sstevel@tonic-gate 	rw_enter(&vfslist, RW_WRITER);
34067c478bd9Sstevel@tonic-gate }
34077c478bd9Sstevel@tonic-gate 
34087c478bd9Sstevel@tonic-gate void
vfs_list_read_lock()34097c478bd9Sstevel@tonic-gate vfs_list_read_lock()
34107c478bd9Sstevel@tonic-gate {
34117c478bd9Sstevel@tonic-gate 	rw_enter(&vfslist, RW_READER);
34127c478bd9Sstevel@tonic-gate }
34137c478bd9Sstevel@tonic-gate 
34147c478bd9Sstevel@tonic-gate void
vfs_list_unlock()34157c478bd9Sstevel@tonic-gate vfs_list_unlock()
34167c478bd9Sstevel@tonic-gate {
34177c478bd9Sstevel@tonic-gate 	rw_exit(&vfslist);
34187c478bd9Sstevel@tonic-gate }
34197c478bd9Sstevel@tonic-gate 
34207c478bd9Sstevel@tonic-gate /*
34217c478bd9Sstevel@tonic-gate  * Low level worker routines for adding entries to and removing entries from
34227c478bd9Sstevel@tonic-gate  * the vfs list.
34237c478bd9Sstevel@tonic-gate  */
34247c478bd9Sstevel@tonic-gate 
34257c478bd9Sstevel@tonic-gate static void
vfs_hash_add(struct vfs * vfsp,int insert_at_head)34267c478bd9Sstevel@tonic-gate vfs_hash_add(struct vfs *vfsp, int insert_at_head)
34277c478bd9Sstevel@tonic-gate {
34287c478bd9Sstevel@tonic-gate 	int vhno;
34297c478bd9Sstevel@tonic-gate 	struct vfs **hp;
34307c478bd9Sstevel@tonic-gate 	dev_t dev;
34317c478bd9Sstevel@tonic-gate 
34327c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&vfslist));
34337c478bd9Sstevel@tonic-gate 
34347c478bd9Sstevel@tonic-gate 	dev = expldev(vfsp->vfs_fsid.val[0]);
34357c478bd9Sstevel@tonic-gate 	vhno = VFSHASH(getmajor(dev), getminor(dev));
34367c478bd9Sstevel@tonic-gate 
34377c478bd9Sstevel@tonic-gate 	mutex_enter(&rvfs_list[vhno].rvfs_lock);
34387c478bd9Sstevel@tonic-gate 
34397c478bd9Sstevel@tonic-gate 	/*
34407c478bd9Sstevel@tonic-gate 	 * Link into the hash table, inserting it at the end, so that LOFS
34417c478bd9Sstevel@tonic-gate 	 * with the same fsid as UFS (or other) file systems will not hide the
34427c478bd9Sstevel@tonic-gate 	 * UFS.
34437c478bd9Sstevel@tonic-gate 	 */
34447c478bd9Sstevel@tonic-gate 	if (insert_at_head) {
34457c478bd9Sstevel@tonic-gate 		vfsp->vfs_hash = rvfs_list[vhno].rvfs_head;
34467c478bd9Sstevel@tonic-gate 		rvfs_list[vhno].rvfs_head = vfsp;
34477c478bd9Sstevel@tonic-gate 	} else {
34487c478bd9Sstevel@tonic-gate 		for (hp = &rvfs_list[vhno].rvfs_head; *hp != NULL;
34497c478bd9Sstevel@tonic-gate 		    hp = &(*hp)->vfs_hash)
34507c478bd9Sstevel@tonic-gate 			continue;
34517c478bd9Sstevel@tonic-gate 		/*
34527c478bd9Sstevel@tonic-gate 		 * hp now contains the address of the pointer to update
34537c478bd9Sstevel@tonic-gate 		 * to effect the insertion.
34547c478bd9Sstevel@tonic-gate 		 */
34557c478bd9Sstevel@tonic-gate 		vfsp->vfs_hash = NULL;
34567c478bd9Sstevel@tonic-gate 		*hp = vfsp;
34577c478bd9Sstevel@tonic-gate 	}
34587c478bd9Sstevel@tonic-gate 
34597c478bd9Sstevel@tonic-gate 	rvfs_list[vhno].rvfs_len++;
34607c478bd9Sstevel@tonic-gate 	mutex_exit(&rvfs_list[vhno].rvfs_lock);
34617c478bd9Sstevel@tonic-gate }
34627c478bd9Sstevel@tonic-gate 
34637c478bd9Sstevel@tonic-gate 
34647c478bd9Sstevel@tonic-gate static void
vfs_hash_remove(struct vfs * vfsp)34657c478bd9Sstevel@tonic-gate vfs_hash_remove(struct vfs *vfsp)
34667c478bd9Sstevel@tonic-gate {
34677c478bd9Sstevel@tonic-gate 	int vhno;
34687c478bd9Sstevel@tonic-gate 	struct vfs *tvfsp;
34697c478bd9Sstevel@tonic-gate 	dev_t dev;
34707c478bd9Sstevel@tonic-gate 
34717c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&vfslist));
34727c478bd9Sstevel@tonic-gate 
34737c478bd9Sstevel@tonic-gate 	dev = expldev(vfsp->vfs_fsid.val[0]);
34747c478bd9Sstevel@tonic-gate 	vhno = VFSHASH(getmajor(dev), getminor(dev));
34757c478bd9Sstevel@tonic-gate 
34767c478bd9Sstevel@tonic-gate 	mutex_enter(&rvfs_list[vhno].rvfs_lock);
34777c478bd9Sstevel@tonic-gate 
34787c478bd9Sstevel@tonic-gate 	/*
34797c478bd9Sstevel@tonic-gate 	 * Remove from hash.
34807c478bd9Sstevel@tonic-gate 	 */
34817c478bd9Sstevel@tonic-gate 	if (rvfs_list[vhno].rvfs_head == vfsp) {
34827c478bd9Sstevel@tonic-gate 		rvfs_list[vhno].rvfs_head = vfsp->vfs_hash;
34837c478bd9Sstevel@tonic-gate 		rvfs_list[vhno].rvfs_len--;
34847c478bd9Sstevel@tonic-gate 		goto foundit;
34857c478bd9Sstevel@tonic-gate 	}
34867c478bd9Sstevel@tonic-gate 	for (tvfsp = rvfs_list[vhno].rvfs_head; tvfsp != NULL;
34877c478bd9Sstevel@tonic-gate 	    tvfsp = tvfsp->vfs_hash) {
34887c478bd9Sstevel@tonic-gate 		if (tvfsp->vfs_hash == vfsp) {
34897c478bd9Sstevel@tonic-gate 			tvfsp->vfs_hash = vfsp->vfs_hash;
34907c478bd9Sstevel@tonic-gate 			rvfs_list[vhno].rvfs_len--;
34917c478bd9Sstevel@tonic-gate 			goto foundit;
34927c478bd9Sstevel@tonic-gate 		}
34937c478bd9Sstevel@tonic-gate 	}
34947c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "vfs_list_remove: vfs not found in hash");
34957c478bd9Sstevel@tonic-gate 
34967c478bd9Sstevel@tonic-gate foundit:
34977c478bd9Sstevel@tonic-gate 
34987c478bd9Sstevel@tonic-gate 	mutex_exit(&rvfs_list[vhno].rvfs_lock);
34997c478bd9Sstevel@tonic-gate }
35007c478bd9Sstevel@tonic-gate 
35017c478bd9Sstevel@tonic-gate 
35027c478bd9Sstevel@tonic-gate void
vfs_list_add(struct vfs * vfsp)35037c478bd9Sstevel@tonic-gate vfs_list_add(struct vfs *vfsp)
35047c478bd9Sstevel@tonic-gate {
35057c478bd9Sstevel@tonic-gate 	zone_t *zone;
35067c478bd9Sstevel@tonic-gate 
3507835ee219SRobert Harris 	/*
3508835ee219SRobert Harris 	 * Typically, the vfs_t will have been created on behalf of the file
3509835ee219SRobert Harris 	 * system in vfs_init, where it will have been provided with a
3510835ee219SRobert Harris 	 * vfs_impl_t. This, however, might be lacking if the vfs_t was created
3511835ee219SRobert Harris 	 * by an unbundled file system. We therefore check for such an example
3512835ee219SRobert Harris 	 * before stamping the vfs_t with its creation time for the benefit of
3513835ee219SRobert Harris 	 * mntfs.
3514835ee219SRobert Harris 	 */
3515835ee219SRobert Harris 	if (vfsp->vfs_implp == NULL)
3516835ee219SRobert Harris 		vfsimpl_setup(vfsp);
3517835ee219SRobert Harris 	vfs_mono_time(&vfsp->vfs_hrctime);
3518835ee219SRobert Harris 
35197c478bd9Sstevel@tonic-gate 	/*
35207c478bd9Sstevel@tonic-gate 	 * The zone that owns the mount is the one that performed the mount.
35217c478bd9Sstevel@tonic-gate 	 * Note that this isn't necessarily the same as the zone mounted into.
3522a19609f8Sjv 	 * The corresponding zone_rele_ref() will be done when the vfs_t
3523a19609f8Sjv 	 * is being free'd.
35247c478bd9Sstevel@tonic-gate 	 */
35257c478bd9Sstevel@tonic-gate 	vfsp->vfs_zone = curproc->p_zone;
3526a19609f8Sjv 	zone_init_ref(&vfsp->vfs_implp->vi_zone_ref);
3527a19609f8Sjv 	zone_hold_ref(vfsp->vfs_zone, &vfsp->vfs_implp->vi_zone_ref,
3528a19609f8Sjv 	    ZONE_REF_VFS);
35297c478bd9Sstevel@tonic-gate 
35307c478bd9Sstevel@tonic-gate 	/*
35317c478bd9Sstevel@tonic-gate 	 * Find the zone mounted into, and put this mount on its vfs list.
35327c478bd9Sstevel@tonic-gate 	 */
35337c478bd9Sstevel@tonic-gate 	zone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
35347c478bd9Sstevel@tonic-gate 	ASSERT(zone != NULL);
35357c478bd9Sstevel@tonic-gate 	/*
35367c478bd9Sstevel@tonic-gate 	 * Special casing for the root vfs.  This structure is allocated
35377c478bd9Sstevel@tonic-gate 	 * statically and hooked onto rootvfs at link time.  During the
35387c478bd9Sstevel@tonic-gate 	 * vfs_mountroot call at system startup time, the root file system's
35397c478bd9Sstevel@tonic-gate 	 * VFS_MOUNTROOT routine will call vfs_add with this root vfs struct
35407c478bd9Sstevel@tonic-gate 	 * as argument.  The code below must detect and handle this special
35417c478bd9Sstevel@tonic-gate 	 * case.  The only apparent justification for this special casing is
35427c478bd9Sstevel@tonic-gate 	 * to ensure that the root file system appears at the head of the
35437c478bd9Sstevel@tonic-gate 	 * list.
35447c478bd9Sstevel@tonic-gate 	 *
35457c478bd9Sstevel@tonic-gate 	 * XXX:	I'm assuming that it's ok to do normal list locking when
35467c478bd9Sstevel@tonic-gate 	 *	adding the entry for the root file system (this used to be
35477c478bd9Sstevel@tonic-gate 	 *	done with no locks held).
35487c478bd9Sstevel@tonic-gate 	 */
35497c478bd9Sstevel@tonic-gate 	vfs_list_lock();
35507c478bd9Sstevel@tonic-gate 	/*
35517c478bd9Sstevel@tonic-gate 	 * Link into the vfs list proper.
35527c478bd9Sstevel@tonic-gate 	 */
35537c478bd9Sstevel@tonic-gate 	if (vfsp == &root) {
35547c478bd9Sstevel@tonic-gate 		/*
35557c478bd9Sstevel@tonic-gate 		 * Assert: This vfs is already on the list as its first entry.
35567c478bd9Sstevel@tonic-gate 		 * Thus, there's nothing to do.
35577c478bd9Sstevel@tonic-gate 		 */
35587c478bd9Sstevel@tonic-gate 		ASSERT(rootvfs == vfsp);
35597c478bd9Sstevel@tonic-gate 		/*
35607c478bd9Sstevel@tonic-gate 		 * Add it to the head of the global zone's vfslist.
35617c478bd9Sstevel@tonic-gate 		 */
35627c478bd9Sstevel@tonic-gate 		ASSERT(zone == global_zone);
35637c478bd9Sstevel@tonic-gate 		ASSERT(zone->zone_vfslist == NULL);
35647c478bd9Sstevel@tonic-gate 		zone->zone_vfslist = vfsp;
35657c478bd9Sstevel@tonic-gate 	} else {
35667c478bd9Sstevel@tonic-gate 		/*
35677c478bd9Sstevel@tonic-gate 		 * Link to end of list using vfs_prev (as rootvfs is now a
35687c478bd9Sstevel@tonic-gate 		 * doubly linked circular list) so list is in mount order for
35697c478bd9Sstevel@tonic-gate 		 * mnttab use.
35707c478bd9Sstevel@tonic-gate 		 */
35717c478bd9Sstevel@tonic-gate 		rootvfs->vfs_prev->vfs_next = vfsp;
35727c478bd9Sstevel@tonic-gate 		vfsp->vfs_prev = rootvfs->vfs_prev;
35737c478bd9Sstevel@tonic-gate 		rootvfs->vfs_prev = vfsp;
35747c478bd9Sstevel@tonic-gate 		vfsp->vfs_next = rootvfs;
35757c478bd9Sstevel@tonic-gate 
35767c478bd9Sstevel@tonic-gate 		/*
35777c478bd9Sstevel@tonic-gate 		 * Do it again for the zone-private list (which may be NULL).
35787c478bd9Sstevel@tonic-gate 		 */
35797c478bd9Sstevel@tonic-gate 		if (zone->zone_vfslist == NULL) {
35807c478bd9Sstevel@tonic-gate 			ASSERT(zone != global_zone);
35817c478bd9Sstevel@tonic-gate 			zone->zone_vfslist = vfsp;
35827c478bd9Sstevel@tonic-gate 		} else {
35837c478bd9Sstevel@tonic-gate 			zone->zone_vfslist->vfs_zone_prev->vfs_zone_next = vfsp;
35847c478bd9Sstevel@tonic-gate 			vfsp->vfs_zone_prev = zone->zone_vfslist->vfs_zone_prev;
35857c478bd9Sstevel@tonic-gate 			zone->zone_vfslist->vfs_zone_prev = vfsp;
35867c478bd9Sstevel@tonic-gate 			vfsp->vfs_zone_next = zone->zone_vfslist;
35877c478bd9Sstevel@tonic-gate 		}
35887c478bd9Sstevel@tonic-gate 	}
35897c478bd9Sstevel@tonic-gate 
35907c478bd9Sstevel@tonic-gate 	/*
35917c478bd9Sstevel@tonic-gate 	 * Link into the hash table, inserting it at the end, so that LOFS
35927c478bd9Sstevel@tonic-gate 	 * with the same fsid as UFS (or other) file systems will not hide
35937c478bd9Sstevel@tonic-gate 	 * the UFS.
35947c478bd9Sstevel@tonic-gate 	 */
35957c478bd9Sstevel@tonic-gate 	vfs_hash_add(vfsp, 0);
35967c478bd9Sstevel@tonic-gate 
35977c478bd9Sstevel@tonic-gate 	/*
35987c478bd9Sstevel@tonic-gate 	 * update the mnttab modification time
35997c478bd9Sstevel@tonic-gate 	 */
36007c478bd9Sstevel@tonic-gate 	vfs_mnttab_modtimeupd();
36017c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
36027c478bd9Sstevel@tonic-gate 	zone_rele(zone);
36037c478bd9Sstevel@tonic-gate }
36047c478bd9Sstevel@tonic-gate 
36057c478bd9Sstevel@tonic-gate void
vfs_list_remove(struct vfs * vfsp)36067c478bd9Sstevel@tonic-gate vfs_list_remove(struct vfs *vfsp)
36077c478bd9Sstevel@tonic-gate {
36087c478bd9Sstevel@tonic-gate 	zone_t *zone;
36097c478bd9Sstevel@tonic-gate 
36107c478bd9Sstevel@tonic-gate 	zone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
36117c478bd9Sstevel@tonic-gate 	ASSERT(zone != NULL);
36127c478bd9Sstevel@tonic-gate 	/*
36137c478bd9Sstevel@tonic-gate 	 * Callers are responsible for preventing attempts to unmount the
36147c478bd9Sstevel@tonic-gate 	 * root.
36157c478bd9Sstevel@tonic-gate 	 */
36167c478bd9Sstevel@tonic-gate 	ASSERT(vfsp != rootvfs);
36177c478bd9Sstevel@tonic-gate 
36187c478bd9Sstevel@tonic-gate 	vfs_list_lock();
36197c478bd9Sstevel@tonic-gate 
36207c478bd9Sstevel@tonic-gate 	/*
36217c478bd9Sstevel@tonic-gate 	 * Remove from hash.
36227c478bd9Sstevel@tonic-gate 	 */
36237c478bd9Sstevel@tonic-gate 	vfs_hash_remove(vfsp);
36247c478bd9Sstevel@tonic-gate 
36257c478bd9Sstevel@tonic-gate 	/*
36267c478bd9Sstevel@tonic-gate 	 * Remove from vfs list.
36277c478bd9Sstevel@tonic-gate 	 */
36287c478bd9Sstevel@tonic-gate 	vfsp->vfs_prev->vfs_next = vfsp->vfs_next;
36297c478bd9Sstevel@tonic-gate 	vfsp->vfs_next->vfs_prev = vfsp->vfs_prev;
36307c478bd9Sstevel@tonic-gate 	vfsp->vfs_next = vfsp->vfs_prev = NULL;
36317c478bd9Sstevel@tonic-gate 
36327c478bd9Sstevel@tonic-gate 	/*
36337c478bd9Sstevel@tonic-gate 	 * Remove from zone-specific vfs list.
36347c478bd9Sstevel@tonic-gate 	 */
36357c478bd9Sstevel@tonic-gate 	if (zone->zone_vfslist == vfsp)
36367c478bd9Sstevel@tonic-gate 		zone->zone_vfslist = vfsp->vfs_zone_next;
36377c478bd9Sstevel@tonic-gate 
36387c478bd9Sstevel@tonic-gate 	if (vfsp->vfs_zone_next == vfsp) {
36397c478bd9Sstevel@tonic-gate 		ASSERT(vfsp->vfs_zone_prev == vfsp);
36407c478bd9Sstevel@tonic-gate 		ASSERT(zone->zone_vfslist == vfsp);
36417c478bd9Sstevel@tonic-gate 		zone->zone_vfslist = NULL;
36427c478bd9Sstevel@tonic-gate 	}
36437c478bd9Sstevel@tonic-gate 
36447c478bd9Sstevel@tonic-gate 	vfsp->vfs_zone_prev->vfs_zone_next = vfsp->vfs_zone_next;
36457c478bd9Sstevel@tonic-gate 	vfsp->vfs_zone_next->vfs_zone_prev = vfsp->vfs_zone_prev;
36467c478bd9Sstevel@tonic-gate 	vfsp->vfs_zone_next = vfsp->vfs_zone_prev = NULL;
36477c478bd9Sstevel@tonic-gate 
36487c478bd9Sstevel@tonic-gate 	/*
36497c478bd9Sstevel@tonic-gate 	 * update the mnttab modification time
36507c478bd9Sstevel@tonic-gate 	 */
36517c478bd9Sstevel@tonic-gate 	vfs_mnttab_modtimeupd();
36527c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
36537c478bd9Sstevel@tonic-gate 	zone_rele(zone);
36547c478bd9Sstevel@tonic-gate }
36557c478bd9Sstevel@tonic-gate 
36567c478bd9Sstevel@tonic-gate struct vfs *
getvfs(fsid_t * fsid)36577c478bd9Sstevel@tonic-gate getvfs(fsid_t *fsid)
36587c478bd9Sstevel@tonic-gate {
36597c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
36607c478bd9Sstevel@tonic-gate 	int val0 = fsid->val[0];
36617c478bd9Sstevel@tonic-gate 	int val1 = fsid->val[1];
36627c478bd9Sstevel@tonic-gate 	dev_t dev = expldev(val0);
36637c478bd9Sstevel@tonic-gate 	int vhno = VFSHASH(getmajor(dev), getminor(dev));
36647c478bd9Sstevel@tonic-gate 	kmutex_t *hmp = &rvfs_list[vhno].rvfs_lock;
36657c478bd9Sstevel@tonic-gate 
36667c478bd9Sstevel@tonic-gate 	mutex_enter(hmp);
36677c478bd9Sstevel@tonic-gate 	for (vfsp = rvfs_list[vhno].rvfs_head; vfsp; vfsp = vfsp->vfs_hash) {
36687c478bd9Sstevel@tonic-gate 		if (vfsp->vfs_fsid.val[0] == val0 &&
36697c478bd9Sstevel@tonic-gate 		    vfsp->vfs_fsid.val[1] == val1) {
36707c478bd9Sstevel@tonic-gate 			VFS_HOLD(vfsp);
36717c478bd9Sstevel@tonic-gate 			mutex_exit(hmp);
36727c478bd9Sstevel@tonic-gate 			return (vfsp);
36737c478bd9Sstevel@tonic-gate 		}
36747c478bd9Sstevel@tonic-gate 	}
36757c478bd9Sstevel@tonic-gate 	mutex_exit(hmp);
36767c478bd9Sstevel@tonic-gate 	return (NULL);
36777c478bd9Sstevel@tonic-gate }
36787c478bd9Sstevel@tonic-gate 
36797c478bd9Sstevel@tonic-gate /*
36807c478bd9Sstevel@tonic-gate  * Search the vfs mount in progress list for a specified device/vfs entry.
36817c478bd9Sstevel@tonic-gate  * Returns 0 if the first entry in the list that the device matches has the
36827c478bd9Sstevel@tonic-gate  * given vfs pointer as well.  If the device matches but a different vfs
36837c478bd9Sstevel@tonic-gate  * pointer is encountered in the list before the given vfs pointer then
36847c478bd9Sstevel@tonic-gate  * a 1 is returned.
36857c478bd9Sstevel@tonic-gate  */
36867c478bd9Sstevel@tonic-gate 
36877c478bd9Sstevel@tonic-gate int
vfs_devmounting(dev_t dev,struct vfs * vfsp)36887c478bd9Sstevel@tonic-gate vfs_devmounting(dev_t dev, struct vfs *vfsp)
36897c478bd9Sstevel@tonic-gate {
36907c478bd9Sstevel@tonic-gate 	int retval = 0;
36917c478bd9Sstevel@tonic-gate 	struct ipmnt *mipp;
36927c478bd9Sstevel@tonic-gate 
36937c478bd9Sstevel@tonic-gate 	mutex_enter(&vfs_miplist_mutex);
36947c478bd9Sstevel@tonic-gate 	for (mipp = vfs_miplist; mipp != NULL; mipp = mipp->mip_next) {
36957c478bd9Sstevel@tonic-gate 		if (mipp->mip_dev == dev) {
36967c478bd9Sstevel@tonic-gate 			if (mipp->mip_vfsp != vfsp)
36977c478bd9Sstevel@tonic-gate 				retval = 1;
36987c478bd9Sstevel@tonic-gate 			break;
36997c478bd9Sstevel@tonic-gate 		}
37007c478bd9Sstevel@tonic-gate 	}
37017c478bd9Sstevel@tonic-gate 	mutex_exit(&vfs_miplist_mutex);
37027c478bd9Sstevel@tonic-gate 	return (retval);
37037c478bd9Sstevel@tonic-gate }
37047c478bd9Sstevel@tonic-gate 
37057c478bd9Sstevel@tonic-gate /*
37067c478bd9Sstevel@tonic-gate  * Search the vfs list for a specified device.  Returns 1, if entry is found
37077c478bd9Sstevel@tonic-gate  * or 0 if no suitable entry is found.
37087c478bd9Sstevel@tonic-gate  */
37097c478bd9Sstevel@tonic-gate 
37107c478bd9Sstevel@tonic-gate int
vfs_devismounted(dev_t dev)37117c478bd9Sstevel@tonic-gate vfs_devismounted(dev_t dev)
37127c478bd9Sstevel@tonic-gate {
37137c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
37147c478bd9Sstevel@tonic-gate 	int found;
37157c478bd9Sstevel@tonic-gate 
37167c478bd9Sstevel@tonic-gate 	vfs_list_read_lock();
37177c478bd9Sstevel@tonic-gate 	vfsp = rootvfs;
37187c478bd9Sstevel@tonic-gate 	found = 0;
37197c478bd9Sstevel@tonic-gate 	do {
37207c478bd9Sstevel@tonic-gate 		if (vfsp->vfs_dev == dev) {
37217c478bd9Sstevel@tonic-gate 			found = 1;
37227c478bd9Sstevel@tonic-gate 			break;
37237c478bd9Sstevel@tonic-gate 		}
37247c478bd9Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
37257c478bd9Sstevel@tonic-gate 	} while (vfsp != rootvfs);
37267c478bd9Sstevel@tonic-gate 
37277c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
37287c478bd9Sstevel@tonic-gate 	return (found);
37297c478bd9Sstevel@tonic-gate }
37307c478bd9Sstevel@tonic-gate 
37317c478bd9Sstevel@tonic-gate /*
37327c478bd9Sstevel@tonic-gate  * Search the vfs list for a specified device.  Returns a pointer to it
37337c478bd9Sstevel@tonic-gate  * or NULL if no suitable entry is found. The caller of this routine
37347c478bd9Sstevel@tonic-gate  * is responsible for releasing the returned vfs pointer.
37357c478bd9Sstevel@tonic-gate  */
37367c478bd9Sstevel@tonic-gate struct vfs *
vfs_dev2vfsp(dev_t dev)37377c478bd9Sstevel@tonic-gate vfs_dev2vfsp(dev_t dev)
37387c478bd9Sstevel@tonic-gate {
37397c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
37407c478bd9Sstevel@tonic-gate 	int found;
37417c478bd9Sstevel@tonic-gate 
37427c478bd9Sstevel@tonic-gate 	vfs_list_read_lock();
37437c478bd9Sstevel@tonic-gate 	vfsp = rootvfs;
37447c478bd9Sstevel@tonic-gate 	found = 0;
37457c478bd9Sstevel@tonic-gate 	do {
37467c478bd9Sstevel@tonic-gate 		/*
37477c478bd9Sstevel@tonic-gate 		 * The following could be made more efficient by making
37487c478bd9Sstevel@tonic-gate 		 * the entire loop use vfs_zone_next if the call is from
37497c478bd9Sstevel@tonic-gate 		 * a zone.  The only callers, however, ustat(2) and
37507c478bd9Sstevel@tonic-gate 		 * umount2(2), don't seem to justify the added
37517c478bd9Sstevel@tonic-gate 		 * complexity at present.
37527c478bd9Sstevel@tonic-gate 		 */
37537c478bd9Sstevel@tonic-gate 		if (vfsp->vfs_dev == dev &&
37547c478bd9Sstevel@tonic-gate 		    ZONE_PATH_VISIBLE(refstr_value(vfsp->vfs_mntpt),
37557c478bd9Sstevel@tonic-gate 		    curproc->p_zone)) {
37567c478bd9Sstevel@tonic-gate 			VFS_HOLD(vfsp);
37577c478bd9Sstevel@tonic-gate 			found = 1;
37587c478bd9Sstevel@tonic-gate 			break;
37597c478bd9Sstevel@tonic-gate 		}
37607c478bd9Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
37617c478bd9Sstevel@tonic-gate 	} while (vfsp != rootvfs);
37627c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
37637c478bd9Sstevel@tonic-gate 	return (found ? vfsp: NULL);
37647c478bd9Sstevel@tonic-gate }
37657c478bd9Sstevel@tonic-gate 
37667c478bd9Sstevel@tonic-gate /*
37677c478bd9Sstevel@tonic-gate  * Search the vfs list for a specified mntpoint.  Returns a pointer to it
37687c478bd9Sstevel@tonic-gate  * or NULL if no suitable entry is found. The caller of this routine
37697c478bd9Sstevel@tonic-gate  * is responsible for releasing the returned vfs pointer.
37707c478bd9Sstevel@tonic-gate  *
37717c478bd9Sstevel@tonic-gate  * Note that if multiple mntpoints match, the last one matching is
37727c478bd9Sstevel@tonic-gate  * returned in an attempt to return the "top" mount when overlay
37737c478bd9Sstevel@tonic-gate  * mounts are covering the same mount point.  This is accomplished by starting
37747c478bd9Sstevel@tonic-gate  * at the end of the list and working our way backwards, stopping at the first
37757c478bd9Sstevel@tonic-gate  * matching mount.
37767c478bd9Sstevel@tonic-gate  */
37777c478bd9Sstevel@tonic-gate struct vfs *
vfs_mntpoint2vfsp(const char * mp)37787c478bd9Sstevel@tonic-gate vfs_mntpoint2vfsp(const char *mp)
37797c478bd9Sstevel@tonic-gate {
37807c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
37817c478bd9Sstevel@tonic-gate 	struct vfs *retvfsp = NULL;
37827c478bd9Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
37837c478bd9Sstevel@tonic-gate 	struct vfs *list;
37847c478bd9Sstevel@tonic-gate 
37857c478bd9Sstevel@tonic-gate 	vfs_list_read_lock();
37867c478bd9Sstevel@tonic-gate 	if (getzoneid() == GLOBAL_ZONEID) {
37877c478bd9Sstevel@tonic-gate 		/*
37887c478bd9Sstevel@tonic-gate 		 * The global zone may see filesystems in any zone.
37897c478bd9Sstevel@tonic-gate 		 */
37907c478bd9Sstevel@tonic-gate 		vfsp = rootvfs->vfs_prev;
37917c478bd9Sstevel@tonic-gate 		do {
37927c478bd9Sstevel@tonic-gate 			if (strcmp(refstr_value(vfsp->vfs_mntpt), mp) == 0) {
37937c478bd9Sstevel@tonic-gate 				retvfsp = vfsp;
37947c478bd9Sstevel@tonic-gate 				break;
37957c478bd9Sstevel@tonic-gate 			}
37967c478bd9Sstevel@tonic-gate 			vfsp = vfsp->vfs_prev;
37977c478bd9Sstevel@tonic-gate 		} while (vfsp != rootvfs->vfs_prev);
37987c478bd9Sstevel@tonic-gate 	} else if ((list = zone->zone_vfslist) != NULL) {
37997c478bd9Sstevel@tonic-gate 		const char *mntpt;
38007c478bd9Sstevel@tonic-gate 
38017c478bd9Sstevel@tonic-gate 		vfsp = list->vfs_zone_prev;
38027c478bd9Sstevel@tonic-gate 		do {
38037c478bd9Sstevel@tonic-gate 			mntpt = refstr_value(vfsp->vfs_mntpt);
38047c478bd9Sstevel@tonic-gate 			mntpt = ZONE_PATH_TRANSLATE(mntpt, zone);
38057c478bd9Sstevel@tonic-gate 			if (strcmp(mntpt, mp) == 0) {
38067c478bd9Sstevel@tonic-gate 				retvfsp = vfsp;
38077c478bd9Sstevel@tonic-gate 				break;
38087c478bd9Sstevel@tonic-gate 			}
38097c478bd9Sstevel@tonic-gate 			vfsp = vfsp->vfs_zone_prev;
38107c478bd9Sstevel@tonic-gate 		} while (vfsp != list->vfs_zone_prev);
38117c478bd9Sstevel@tonic-gate 	}
38127c478bd9Sstevel@tonic-gate 	if (retvfsp)
38137c478bd9Sstevel@tonic-gate 		VFS_HOLD(retvfsp);
38147c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
38157c478bd9Sstevel@tonic-gate 	return (retvfsp);
38167c478bd9Sstevel@tonic-gate }
38177c478bd9Sstevel@tonic-gate 
38187c478bd9Sstevel@tonic-gate /*
38197c478bd9Sstevel@tonic-gate  * Search the vfs list for a specified vfsops.
38207c478bd9Sstevel@tonic-gate  * if vfs entry is found then return 1, else 0.
38217c478bd9Sstevel@tonic-gate  */
38227c478bd9Sstevel@tonic-gate int
vfs_opsinuse(vfsops_t * ops)38237c478bd9Sstevel@tonic-gate vfs_opsinuse(vfsops_t *ops)
38247c478bd9Sstevel@tonic-gate {
38257c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
38267c478bd9Sstevel@tonic-gate 	int found;
38277c478bd9Sstevel@tonic-gate 
38287c478bd9Sstevel@tonic-gate 	vfs_list_read_lock();
38297c478bd9Sstevel@tonic-gate 	vfsp = rootvfs;
38307c478bd9Sstevel@tonic-gate 	found = 0;
38317c478bd9Sstevel@tonic-gate 	do {
38327c478bd9Sstevel@tonic-gate 		if (vfs_getops(vfsp) == ops) {
38337c478bd9Sstevel@tonic-gate 			found = 1;
38347c478bd9Sstevel@tonic-gate 			break;
38357c478bd9Sstevel@tonic-gate 		}
38367c478bd9Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
38377c478bd9Sstevel@tonic-gate 	} while (vfsp != rootvfs);
38387c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
38397c478bd9Sstevel@tonic-gate 	return (found);
38407c478bd9Sstevel@tonic-gate }
38417c478bd9Sstevel@tonic-gate 
38427c478bd9Sstevel@tonic-gate /*
38437c478bd9Sstevel@tonic-gate  * Allocate an entry in vfssw for a file system type
38447c478bd9Sstevel@tonic-gate  */
38457c478bd9Sstevel@tonic-gate struct vfssw *
allocate_vfssw(const char * type)384643d5cd3dSjohnlev allocate_vfssw(const char *type)
38477c478bd9Sstevel@tonic-gate {
38487c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
38497c478bd9Sstevel@tonic-gate 
38507c478bd9Sstevel@tonic-gate 	if (type[0] == '\0' || strlen(type) + 1 > _ST_FSTYPSZ) {
38517c478bd9Sstevel@tonic-gate 		/*
38527c478bd9Sstevel@tonic-gate 		 * The vfssw table uses the empty string to identify an
38537c478bd9Sstevel@tonic-gate 		 * available entry; we cannot add any type which has
38547c478bd9Sstevel@tonic-gate 		 * a leading NUL. The string length is limited to
38557c478bd9Sstevel@tonic-gate 		 * the size of the st_fstype array in struct stat.
38567c478bd9Sstevel@tonic-gate 		 */
38577c478bd9Sstevel@tonic-gate 		return (NULL);
38587c478bd9Sstevel@tonic-gate 	}
38597c478bd9Sstevel@tonic-gate 
38607c478bd9Sstevel@tonic-gate 	ASSERT(VFSSW_WRITE_LOCKED());
38617c478bd9Sstevel@tonic-gate 	for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++)
38627c478bd9Sstevel@tonic-gate 		if (!ALLOCATED_VFSSW(vswp)) {
38637c478bd9Sstevel@tonic-gate 			vswp->vsw_name = kmem_alloc(strlen(type) + 1, KM_SLEEP);
38647c478bd9Sstevel@tonic-gate 			(void) strcpy(vswp->vsw_name, type);
38657c478bd9Sstevel@tonic-gate 			ASSERT(vswp->vsw_count == 0);
38667c478bd9Sstevel@tonic-gate 			vswp->vsw_count = 1;
38677c478bd9Sstevel@tonic-gate 			mutex_init(&vswp->vsw_lock, NULL, MUTEX_DEFAULT, NULL);
38687c478bd9Sstevel@tonic-gate 			return (vswp);
38697c478bd9Sstevel@tonic-gate 		}
38707c478bd9Sstevel@tonic-gate 	return (NULL);
38717c478bd9Sstevel@tonic-gate }
38727c478bd9Sstevel@tonic-gate 
38737c478bd9Sstevel@tonic-gate /*
38747c478bd9Sstevel@tonic-gate  * Impose additional layer of translation between vfstype names
38757c478bd9Sstevel@tonic-gate  * and module names in the filesystem.
38767c478bd9Sstevel@tonic-gate  */
387743d5cd3dSjohnlev static const char *
vfs_to_modname(const char * vfstype)387843d5cd3dSjohnlev vfs_to_modname(const char *vfstype)
38797c478bd9Sstevel@tonic-gate {
38807c478bd9Sstevel@tonic-gate 	if (strcmp(vfstype, "proc") == 0) {
38817c478bd9Sstevel@tonic-gate 		vfstype = "procfs";
38827c478bd9Sstevel@tonic-gate 	} else if (strcmp(vfstype, "fd") == 0) {
38837c478bd9Sstevel@tonic-gate 		vfstype = "fdfs";
38847c478bd9Sstevel@tonic-gate 	} else if (strncmp(vfstype, "nfs", 3) == 0) {
38857c478bd9Sstevel@tonic-gate 		vfstype = "nfs";
38867c478bd9Sstevel@tonic-gate 	}
38877c478bd9Sstevel@tonic-gate 
38887c478bd9Sstevel@tonic-gate 	return (vfstype);
38897c478bd9Sstevel@tonic-gate }
38907c478bd9Sstevel@tonic-gate 
38917c478bd9Sstevel@tonic-gate /*
38927c478bd9Sstevel@tonic-gate  * Find a vfssw entry given a file system type name.
38937c478bd9Sstevel@tonic-gate  * Try to autoload the filesystem if it's not found.
38947c478bd9Sstevel@tonic-gate  * If it's installed, return the vfssw locked to prevent unloading.
38957c478bd9Sstevel@tonic-gate  */
38967c478bd9Sstevel@tonic-gate struct vfssw *
vfs_getvfssw(const char * type)389743d5cd3dSjohnlev vfs_getvfssw(const char *type)
38987c478bd9Sstevel@tonic-gate {
38997c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
390043d5cd3dSjohnlev 	const char *modname;
39017c478bd9Sstevel@tonic-gate 
39027c478bd9Sstevel@tonic-gate 	RLOCK_VFSSW();
39037c478bd9Sstevel@tonic-gate 	vswp = vfs_getvfsswbyname(type);
39047c478bd9Sstevel@tonic-gate 	modname = vfs_to_modname(type);
39057c478bd9Sstevel@tonic-gate 
39067c478bd9Sstevel@tonic-gate 	if (rootdir == NULL) {
39077c478bd9Sstevel@tonic-gate 		/*
39087c478bd9Sstevel@tonic-gate 		 * If we haven't yet loaded the root file system, then our
39097c478bd9Sstevel@tonic-gate 		 * _init won't be called until later. Allocate vfssw entry,
39107c478bd9Sstevel@tonic-gate 		 * because mod_installfs won't be called.
39117c478bd9Sstevel@tonic-gate 		 */
39127c478bd9Sstevel@tonic-gate 		if (vswp == NULL) {
39137c478bd9Sstevel@tonic-gate 			RUNLOCK_VFSSW();
39147c478bd9Sstevel@tonic-gate 			WLOCK_VFSSW();
39157c478bd9Sstevel@tonic-gate 			if ((vswp = vfs_getvfsswbyname(type)) == NULL) {
39167c478bd9Sstevel@tonic-gate 				if ((vswp = allocate_vfssw(type)) == NULL) {
39177c478bd9Sstevel@tonic-gate 					WUNLOCK_VFSSW();
39187c478bd9Sstevel@tonic-gate 					return (NULL);
39197c478bd9Sstevel@tonic-gate 				}
39207c478bd9Sstevel@tonic-gate 			}
39217c478bd9Sstevel@tonic-gate 			WUNLOCK_VFSSW();
39227c478bd9Sstevel@tonic-gate 			RLOCK_VFSSW();
39237c478bd9Sstevel@tonic-gate 		}
39247c478bd9Sstevel@tonic-gate 		if (!VFS_INSTALLED(vswp)) {
39257c478bd9Sstevel@tonic-gate 			RUNLOCK_VFSSW();
39267c478bd9Sstevel@tonic-gate 			(void) modloadonly("fs", modname);
39277c478bd9Sstevel@tonic-gate 		} else
39287c478bd9Sstevel@tonic-gate 			RUNLOCK_VFSSW();
39297c478bd9Sstevel@tonic-gate 		return (vswp);
39307c478bd9Sstevel@tonic-gate 	}
39317c478bd9Sstevel@tonic-gate 
39327c478bd9Sstevel@tonic-gate 	/*
39337c478bd9Sstevel@tonic-gate 	 * Try to load the filesystem.  Before calling modload(), we drop
39347c478bd9Sstevel@tonic-gate 	 * our lock on the VFS switch table, and pick it up after the
39357c478bd9Sstevel@tonic-gate 	 * module is loaded.  However, there is a potential race:  the
39367c478bd9Sstevel@tonic-gate 	 * module could be unloaded after the call to modload() completes
39377c478bd9Sstevel@tonic-gate 	 * but before we pick up the lock and drive on.  Therefore,
39387c478bd9Sstevel@tonic-gate 	 * we keep reloading the module until we've loaded the module
39397c478bd9Sstevel@tonic-gate 	 * _and_ we have the lock on the VFS switch table.
39407c478bd9Sstevel@tonic-gate 	 */
39417c478bd9Sstevel@tonic-gate 	while (vswp == NULL || !VFS_INSTALLED(vswp)) {
39427c478bd9Sstevel@tonic-gate 		RUNLOCK_VFSSW();
39437c478bd9Sstevel@tonic-gate 		if (modload("fs", modname) == -1)
39447c478bd9Sstevel@tonic-gate 			return (NULL);
39457c478bd9Sstevel@tonic-gate 		RLOCK_VFSSW();
39467c478bd9Sstevel@tonic-gate 		if (vswp == NULL)
39477c478bd9Sstevel@tonic-gate 			if ((vswp = vfs_getvfsswbyname(type)) == NULL)
39487c478bd9Sstevel@tonic-gate 				break;
39497c478bd9Sstevel@tonic-gate 	}
39507c478bd9Sstevel@tonic-gate 	RUNLOCK_VFSSW();
39517c478bd9Sstevel@tonic-gate 
39527c478bd9Sstevel@tonic-gate 	return (vswp);
39537c478bd9Sstevel@tonic-gate }
39547c478bd9Sstevel@tonic-gate 
39557c478bd9Sstevel@tonic-gate /*
39567c478bd9Sstevel@tonic-gate  * Find a vfssw entry given a file system type name.
39577c478bd9Sstevel@tonic-gate  */
39587c478bd9Sstevel@tonic-gate struct vfssw *
vfs_getvfsswbyname(const char * type)395943d5cd3dSjohnlev vfs_getvfsswbyname(const char *type)
39607c478bd9Sstevel@tonic-gate {
39617c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
39627c478bd9Sstevel@tonic-gate 
39637c478bd9Sstevel@tonic-gate 	ASSERT(VFSSW_LOCKED());
39647c478bd9Sstevel@tonic-gate 	if (type == NULL || *type == '\0')
39657c478bd9Sstevel@tonic-gate 		return (NULL);
39667c478bd9Sstevel@tonic-gate 
39677c478bd9Sstevel@tonic-gate 	for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
39687c478bd9Sstevel@tonic-gate 		if (strcmp(type, vswp->vsw_name) == 0) {
39697c478bd9Sstevel@tonic-gate 			vfs_refvfssw(vswp);
39707c478bd9Sstevel@tonic-gate 			return (vswp);
39717c478bd9Sstevel@tonic-gate 		}
39727c478bd9Sstevel@tonic-gate 	}
39737c478bd9Sstevel@tonic-gate 
39747c478bd9Sstevel@tonic-gate 	return (NULL);
39757c478bd9Sstevel@tonic-gate }
39767c478bd9Sstevel@tonic-gate 
39777c478bd9Sstevel@tonic-gate /*
39787c478bd9Sstevel@tonic-gate  * Find a vfssw entry given a set of vfsops.
39797c478bd9Sstevel@tonic-gate  */
39807c478bd9Sstevel@tonic-gate struct vfssw *
vfs_getvfsswbyvfsops(vfsops_t * vfsops)39817c478bd9Sstevel@tonic-gate vfs_getvfsswbyvfsops(vfsops_t *vfsops)
39827c478bd9Sstevel@tonic-gate {
39837c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
39847c478bd9Sstevel@tonic-gate 
39857c478bd9Sstevel@tonic-gate 	RLOCK_VFSSW();
39867c478bd9Sstevel@tonic-gate 	for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
39877c478bd9Sstevel@tonic-gate 		if (ALLOCATED_VFSSW(vswp) && &vswp->vsw_vfsops == vfsops) {
39887c478bd9Sstevel@tonic-gate 			vfs_refvfssw(vswp);
39897c478bd9Sstevel@tonic-gate 			RUNLOCK_VFSSW();
39907c478bd9Sstevel@tonic-gate 			return (vswp);
39917c478bd9Sstevel@tonic-gate 		}
39927c478bd9Sstevel@tonic-gate 	}
39937c478bd9Sstevel@tonic-gate 	RUNLOCK_VFSSW();
39947c478bd9Sstevel@tonic-gate 
39957c478bd9Sstevel@tonic-gate 	return (NULL);
39967c478bd9Sstevel@tonic-gate }
39977c478bd9Sstevel@tonic-gate 
39987c478bd9Sstevel@tonic-gate /*
39997c478bd9Sstevel@tonic-gate  * Reference a vfssw entry.
40007c478bd9Sstevel@tonic-gate  */
40017c478bd9Sstevel@tonic-gate void
vfs_refvfssw(struct vfssw * vswp)40027c478bd9Sstevel@tonic-gate vfs_refvfssw(struct vfssw *vswp)
40037c478bd9Sstevel@tonic-gate {
40047c478bd9Sstevel@tonic-gate 
40057c478bd9Sstevel@tonic-gate 	mutex_enter(&vswp->vsw_lock);
40067c478bd9Sstevel@tonic-gate 	vswp->vsw_count++;
40077c478bd9Sstevel@tonic-gate 	mutex_exit(&vswp->vsw_lock);
40087c478bd9Sstevel@tonic-gate }
40097c478bd9Sstevel@tonic-gate 
40107c478bd9Sstevel@tonic-gate /*
40117c478bd9Sstevel@tonic-gate  * Unreference a vfssw entry.
40127c478bd9Sstevel@tonic-gate  */
40137c478bd9Sstevel@tonic-gate void
vfs_unrefvfssw(struct vfssw * vswp)40147c478bd9Sstevel@tonic-gate vfs_unrefvfssw(struct vfssw *vswp)
40157c478bd9Sstevel@tonic-gate {
40167c478bd9Sstevel@tonic-gate 
40177c478bd9Sstevel@tonic-gate 	mutex_enter(&vswp->vsw_lock);
40187c478bd9Sstevel@tonic-gate 	vswp->vsw_count--;
40197c478bd9Sstevel@tonic-gate 	mutex_exit(&vswp->vsw_lock);
40207c478bd9Sstevel@tonic-gate }
40217c478bd9Sstevel@tonic-gate 
40227c478bd9Sstevel@tonic-gate static int sync_retries = 20;	/* number of retries when not making progress */
40237c478bd9Sstevel@tonic-gate static int sync_triesleft;	/* portion of sync_retries remaining */
40247c478bd9Sstevel@tonic-gate 
40257c478bd9Sstevel@tonic-gate static pgcnt_t old_pgcnt, new_pgcnt;
40267c478bd9Sstevel@tonic-gate static int new_bufcnt, old_bufcnt;
40277c478bd9Sstevel@tonic-gate 
40287c478bd9Sstevel@tonic-gate /*
40297c478bd9Sstevel@tonic-gate  * Sync all of the mounted filesystems, and then wait for the actual i/o to
40307c478bd9Sstevel@tonic-gate  * complete.  We wait by counting the number of dirty pages and buffers,
40317c478bd9Sstevel@tonic-gate  * pushing them out using bio_busy() and page_busy(), and then counting again.
40323f11de9dSSara Hartse  * This routine is used during the uadmin A_SHUTDOWN code.  It should only
40337c478bd9Sstevel@tonic-gate  * be used after some higher-level mechanism has quiesced the system so that
40347c478bd9Sstevel@tonic-gate  * new writes are not being initiated while we are waiting for completion.
40357c478bd9Sstevel@tonic-gate  *
40363f11de9dSSara Hartse  * To ensure finite running time, our algorithm uses sync_triesleft (a progress
40373f11de9dSSara Hartse  * counter used by the vfs_syncall() loop below). It is declared above so
40383f11de9dSSara Hartse  * it can be found easily in the debugger.
40397c478bd9Sstevel@tonic-gate  *
40407c478bd9Sstevel@tonic-gate  * The sync_triesleft counter is updated by vfs_syncall() itself.  If we make
40417c478bd9Sstevel@tonic-gate  * sync_retries consecutive calls to bio_busy() and page_busy() without
40427c478bd9Sstevel@tonic-gate  * decreasing either the number of dirty buffers or dirty pages below the
40437c478bd9Sstevel@tonic-gate  * lowest count we have seen so far, we give up and return from vfs_syncall().
40447c478bd9Sstevel@tonic-gate  *
40457c478bd9Sstevel@tonic-gate  * Each loop iteration ends with a call to delay() one second to allow time for
40467c478bd9Sstevel@tonic-gate  * i/o completion and to permit the user time to read our progress messages.
40477c478bd9Sstevel@tonic-gate  */
40487c478bd9Sstevel@tonic-gate void
vfs_syncall(void)40497c478bd9Sstevel@tonic-gate vfs_syncall(void)
40507c478bd9Sstevel@tonic-gate {
40517c478bd9Sstevel@tonic-gate 	if (rootdir == NULL && !modrootloaded)
40523f11de9dSSara Hartse 		return; /* no filesystems have been loaded yet */
40537c478bd9Sstevel@tonic-gate 
40547c478bd9Sstevel@tonic-gate 	printf("syncing file systems...");
40557c478bd9Sstevel@tonic-gate 	sync();
40567c478bd9Sstevel@tonic-gate 
40577c478bd9Sstevel@tonic-gate 	sync_triesleft = sync_retries;
40587c478bd9Sstevel@tonic-gate 
40597c478bd9Sstevel@tonic-gate 	old_bufcnt = new_bufcnt = INT_MAX;
40607c478bd9Sstevel@tonic-gate 	old_pgcnt = new_pgcnt = ULONG_MAX;
40617c478bd9Sstevel@tonic-gate 
40627c478bd9Sstevel@tonic-gate 	while (sync_triesleft > 0) {
40637c478bd9Sstevel@tonic-gate 		old_bufcnt = MIN(old_bufcnt, new_bufcnt);
40647c478bd9Sstevel@tonic-gate 		old_pgcnt = MIN(old_pgcnt, new_pgcnt);
40657c478bd9Sstevel@tonic-gate 
40667c478bd9Sstevel@tonic-gate 		new_bufcnt = bio_busy(B_TRUE);
40677c478bd9Sstevel@tonic-gate 		new_pgcnt = page_busy(B_TRUE);
40687c478bd9Sstevel@tonic-gate 
40697c478bd9Sstevel@tonic-gate 		if (new_bufcnt == 0 && new_pgcnt == 0)
40707c478bd9Sstevel@tonic-gate 			break;
40717c478bd9Sstevel@tonic-gate 
40727c478bd9Sstevel@tonic-gate 		if (new_bufcnt < old_bufcnt || new_pgcnt < old_pgcnt)
40737c478bd9Sstevel@tonic-gate 			sync_triesleft = sync_retries;
40747c478bd9Sstevel@tonic-gate 		else
40757c478bd9Sstevel@tonic-gate 			sync_triesleft--;
40767c478bd9Sstevel@tonic-gate 
40777c478bd9Sstevel@tonic-gate 		if (new_bufcnt)
40787c478bd9Sstevel@tonic-gate 			printf(" [%d]", new_bufcnt);
40797c478bd9Sstevel@tonic-gate 		if (new_pgcnt)
40807c478bd9Sstevel@tonic-gate 			printf(" %lu", new_pgcnt);
40817c478bd9Sstevel@tonic-gate 
40827c478bd9Sstevel@tonic-gate 		delay(hz);
40837c478bd9Sstevel@tonic-gate 	}
40847c478bd9Sstevel@tonic-gate 
40857c478bd9Sstevel@tonic-gate 	if (new_bufcnt != 0 || new_pgcnt != 0)
40867c478bd9Sstevel@tonic-gate 		printf(" done (not all i/o completed)\n");
40877c478bd9Sstevel@tonic-gate 	else
40887c478bd9Sstevel@tonic-gate 		printf(" done\n");
40897c478bd9Sstevel@tonic-gate 
40907c478bd9Sstevel@tonic-gate 	delay(hz);
40917c478bd9Sstevel@tonic-gate }
40927c478bd9Sstevel@tonic-gate 
40937c478bd9Sstevel@tonic-gate /*
40947c478bd9Sstevel@tonic-gate  * Map VFS flags to statvfs flags.  These shouldn't really be separate
40957c478bd9Sstevel@tonic-gate  * flags at all.
40967c478bd9Sstevel@tonic-gate  */
40977c478bd9Sstevel@tonic-gate uint_t
vf_to_stf(uint_t vf)40987c478bd9Sstevel@tonic-gate vf_to_stf(uint_t vf)
40997c478bd9Sstevel@tonic-gate {
41007c478bd9Sstevel@tonic-gate 	uint_t stf = 0;
41017c478bd9Sstevel@tonic-gate 
41027c478bd9Sstevel@tonic-gate 	if (vf & VFS_RDONLY)
41037c478bd9Sstevel@tonic-gate 		stf |= ST_RDONLY;
41047c478bd9Sstevel@tonic-gate 	if (vf & VFS_NOSETUID)
41057c478bd9Sstevel@tonic-gate 		stf |= ST_NOSUID;
41067c478bd9Sstevel@tonic-gate 	if (vf & VFS_NOTRUNC)
41077c478bd9Sstevel@tonic-gate 		stf |= ST_NOTRUNC;
41087c478bd9Sstevel@tonic-gate 
41097c478bd9Sstevel@tonic-gate 	return (stf);
41107c478bd9Sstevel@tonic-gate }
41117c478bd9Sstevel@tonic-gate 
41127c478bd9Sstevel@tonic-gate /*
41137c478bd9Sstevel@tonic-gate  * Entries for (illegal) fstype 0.
41147c478bd9Sstevel@tonic-gate  */
41157c478bd9Sstevel@tonic-gate /* ARGSUSED */
41167c478bd9Sstevel@tonic-gate int
vfsstray_sync(struct vfs * vfsp,short arg,struct cred * cr)41177c478bd9Sstevel@tonic-gate vfsstray_sync(struct vfs *vfsp, short arg, struct cred *cr)
41187c478bd9Sstevel@tonic-gate {
41197c478bd9Sstevel@tonic-gate 	cmn_err(CE_PANIC, "stray vfs operation");
41207c478bd9Sstevel@tonic-gate 	return (0);
41217c478bd9Sstevel@tonic-gate }
41227c478bd9Sstevel@tonic-gate 
41237c478bd9Sstevel@tonic-gate /*
41247c478bd9Sstevel@tonic-gate  * Entries for (illegal) fstype 0.
41257c478bd9Sstevel@tonic-gate  */
41267c478bd9Sstevel@tonic-gate int
vfsstray(void)41277c478bd9Sstevel@tonic-gate vfsstray(void)
41287c478bd9Sstevel@tonic-gate {
41297c478bd9Sstevel@tonic-gate 	cmn_err(CE_PANIC, "stray vfs operation");
41307c478bd9Sstevel@tonic-gate 	return (0);
41317c478bd9Sstevel@tonic-gate }
41327c478bd9Sstevel@tonic-gate 
41337c478bd9Sstevel@tonic-gate /*
41347c478bd9Sstevel@tonic-gate  * Support for dealing with forced UFS unmount and its interaction with
41357c478bd9Sstevel@tonic-gate  * LOFS. Could be used by any filesystem.
41367c478bd9Sstevel@tonic-gate  * See bug 1203132.
41377c478bd9Sstevel@tonic-gate  */
41387c478bd9Sstevel@tonic-gate int
vfs_EIO(void)41397c478bd9Sstevel@tonic-gate vfs_EIO(void)
41407c478bd9Sstevel@tonic-gate {
41417c478bd9Sstevel@tonic-gate 	return (EIO);
41427c478bd9Sstevel@tonic-gate }
41437c478bd9Sstevel@tonic-gate 
41447c478bd9Sstevel@tonic-gate /*
41457c478bd9Sstevel@tonic-gate  * We've gotta define the op for sync separately, since the compiler gets
41467c478bd9Sstevel@tonic-gate  * confused if we mix and match ANSI and normal style prototypes when
41477c478bd9Sstevel@tonic-gate  * a "short" argument is present and spits out a warning.
41487c478bd9Sstevel@tonic-gate  */
41497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
41507c478bd9Sstevel@tonic-gate int
vfs_EIO_sync(struct vfs * vfsp,short arg,struct cred * cr)41517c478bd9Sstevel@tonic-gate vfs_EIO_sync(struct vfs *vfsp, short arg, struct cred *cr)
41527c478bd9Sstevel@tonic-gate {
41537c478bd9Sstevel@tonic-gate 	return (EIO);
41547c478bd9Sstevel@tonic-gate }
41557c478bd9Sstevel@tonic-gate 
41567c478bd9Sstevel@tonic-gate vfs_t EIO_vfs;
41577c478bd9Sstevel@tonic-gate vfsops_t *EIO_vfsops;
41587c478bd9Sstevel@tonic-gate 
41597c478bd9Sstevel@tonic-gate /*
41607c478bd9Sstevel@tonic-gate  * Called from startup() to initialize all loaded vfs's
41617c478bd9Sstevel@tonic-gate  */
41627c478bd9Sstevel@tonic-gate void
vfsinit(void)41637c478bd9Sstevel@tonic-gate vfsinit(void)
41647c478bd9Sstevel@tonic-gate {
41657c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
41667c478bd9Sstevel@tonic-gate 	int error;
416782c7f3c4Srsb 	extern int vopstats_enabled;
41685a59a8b3Srsb 	extern void vopstats_startup();
41697c478bd9Sstevel@tonic-gate 
41707c478bd9Sstevel@tonic-gate 	static const fs_operation_def_t EIO_vfsops_template[] = {
4171aa59c4cbSrsb 		VFSNAME_MOUNT,		{ .error = vfs_EIO },
4172aa59c4cbSrsb 		VFSNAME_UNMOUNT,	{ .error = vfs_EIO },
4173aa59c4cbSrsb 		VFSNAME_ROOT,		{ .error = vfs_EIO },
4174aa59c4cbSrsb 		VFSNAME_STATVFS,	{ .error = vfs_EIO },
4175b6d49c84SToomas Soome 		VFSNAME_SYNC,		{ .vfs_sync = vfs_EIO_sync },
4176aa59c4cbSrsb 		VFSNAME_VGET,		{ .error = vfs_EIO },
4177aa59c4cbSrsb 		VFSNAME_MOUNTROOT,	{ .error = vfs_EIO },
4178aa59c4cbSrsb 		VFSNAME_FREEVFS,	{ .error = vfs_EIO },
4179aa59c4cbSrsb 		VFSNAME_VNSTATE,	{ .error = vfs_EIO },
41807c478bd9Sstevel@tonic-gate 		NULL, NULL
41817c478bd9Sstevel@tonic-gate 	};
41827c478bd9Sstevel@tonic-gate 
4183aa59c4cbSrsb 	static const fs_operation_def_t stray_vfsops_template[] = {
4184aa59c4cbSrsb 		VFSNAME_MOUNT,		{ .error = vfsstray },
4185aa59c4cbSrsb 		VFSNAME_UNMOUNT,	{ .error = vfsstray },
4186aa59c4cbSrsb 		VFSNAME_ROOT,		{ .error = vfsstray },
4187aa59c4cbSrsb 		VFSNAME_STATVFS,	{ .error = vfsstray },
4188b6d49c84SToomas Soome 		VFSNAME_SYNC,		{ .vfs_sync = vfsstray_sync },
4189aa59c4cbSrsb 		VFSNAME_VGET,		{ .error = vfsstray },
4190aa59c4cbSrsb 		VFSNAME_MOUNTROOT,	{ .error = vfsstray },
4191aa59c4cbSrsb 		VFSNAME_FREEVFS,	{ .error = vfsstray },
4192aa59c4cbSrsb 		VFSNAME_VNSTATE,	{ .error = vfsstray },
4193aa59c4cbSrsb 		NULL, NULL
4194aa59c4cbSrsb 	};
41957c478bd9Sstevel@tonic-gate 
4196da6c28aaSamw 	/* Create vfs cache */
4197da6c28aaSamw 	vfs_cache = kmem_cache_create("vfs_cache", sizeof (struct vfs),
4198da6c28aaSamw 	    sizeof (uintptr_t), NULL, NULL, NULL, NULL, NULL, 0);
41997c478bd9Sstevel@tonic-gate 
4200da6c28aaSamw 	/* Initialize the vnode cache (file systems may use it during init). */
42017c478bd9Sstevel@tonic-gate 	vn_create_cache();
42027c478bd9Sstevel@tonic-gate 
42037c478bd9Sstevel@tonic-gate 	/* Setup event monitor framework */
42047c478bd9Sstevel@tonic-gate 	fem_init();
42057c478bd9Sstevel@tonic-gate 
42067c478bd9Sstevel@tonic-gate 	/* Initialize the dummy stray file system type. */
4207d320ee2dSrsb 	error = vfs_setfsops(0, stray_vfsops_template, NULL);
42087c478bd9Sstevel@tonic-gate 
42097c478bd9Sstevel@tonic-gate 	/* Initialize the dummy EIO file system. */
42107c478bd9Sstevel@tonic-gate 	error = vfs_makefsops(EIO_vfsops_template, &EIO_vfsops);
42117c478bd9Sstevel@tonic-gate 	if (error != 0) {
42127c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "vfsinit: bad EIO vfs ops template");
42137c478bd9Sstevel@tonic-gate 		/* Shouldn't happen, but not bad enough to panic */
42147c478bd9Sstevel@tonic-gate 	}
42157c478bd9Sstevel@tonic-gate 
42167c478bd9Sstevel@tonic-gate 	VFS_INIT(&EIO_vfs, EIO_vfsops, (caddr_t)NULL);
42177c478bd9Sstevel@tonic-gate 
42187c478bd9Sstevel@tonic-gate 	/*
42197c478bd9Sstevel@tonic-gate 	 * Default EIO_vfs.vfs_flag to VFS_UNMOUNTED so a lookup
42207c478bd9Sstevel@tonic-gate 	 * on this vfs can immediately notice it's invalid.
42217c478bd9Sstevel@tonic-gate 	 */
42227c478bd9Sstevel@tonic-gate 	EIO_vfs.vfs_flag |= VFS_UNMOUNTED;
42237c478bd9Sstevel@tonic-gate 
42247c478bd9Sstevel@tonic-gate 	/*
42257c478bd9Sstevel@tonic-gate 	 * Call the init routines of non-loadable filesystems only.
42267c478bd9Sstevel@tonic-gate 	 * Filesystems which are loaded as separate modules will be
42277c478bd9Sstevel@tonic-gate 	 * initialized by the module loading code instead.
42287c478bd9Sstevel@tonic-gate 	 */
42297c478bd9Sstevel@tonic-gate 
42307c478bd9Sstevel@tonic-gate 	for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
42317c478bd9Sstevel@tonic-gate 		RLOCK_VFSSW();
42327c478bd9Sstevel@tonic-gate 		if (vswp->vsw_init != NULL)
4233d6294894SJohn Levon 			(void) (*vswp->vsw_init)(vswp - vfssw, vswp->vsw_name);
42347c478bd9Sstevel@tonic-gate 		RUNLOCK_VFSSW();
42357c478bd9Sstevel@tonic-gate 	}
42365a59a8b3Srsb 
42375a59a8b3Srsb 	vopstats_startup();
423882c7f3c4Srsb 
423982c7f3c4Srsb 	if (vopstats_enabled) {
424082c7f3c4Srsb 		/* EIO_vfs can collect stats, but we don't retrieve them */
424182c7f3c4Srsb 		initialize_vopstats(&EIO_vfs.vfs_vopstats);
424282c7f3c4Srsb 		EIO_vfs.vfs_fstypevsp = NULL;
424382c7f3c4Srsb 		EIO_vfs.vfs_vskap = NULL;
424482c7f3c4Srsb 		EIO_vfs.vfs_flag |= VFS_STATS;
424582c7f3c4Srsb 	}
4246da6c28aaSamw 
4247da6c28aaSamw 	xattr_init();
42487a286c47SDai Ngo 
42497a286c47SDai Ngo 	reparse_point_init();
4250da6c28aaSamw }
4251da6c28aaSamw 
4252da6c28aaSamw vfs_t *
vfs_alloc(int kmflag)4253da6c28aaSamw vfs_alloc(int kmflag)
4254da6c28aaSamw {
4255da6c28aaSamw 	vfs_t *vfsp;
4256da6c28aaSamw 
4257da6c28aaSamw 	vfsp = kmem_cache_alloc(vfs_cache, kmflag);
4258da6c28aaSamw 
4259da6c28aaSamw 	/*
4260da6c28aaSamw 	 * Do the simplest initialization here.
4261da6c28aaSamw 	 * Everything else gets done in vfs_init()
4262da6c28aaSamw 	 */
4263da6c28aaSamw 	bzero(vfsp, sizeof (vfs_t));
4264da6c28aaSamw 	return (vfsp);
4265da6c28aaSamw }
4266da6c28aaSamw 
4267da6c28aaSamw void
vfs_free(vfs_t * vfsp)4268da6c28aaSamw vfs_free(vfs_t *vfsp)
4269da6c28aaSamw {
4270da6c28aaSamw 	/*
4271da6c28aaSamw 	 * One would be tempted to assert that "vfsp->vfs_count == 0".
4272da6c28aaSamw 	 * The problem is that this gets called out of domount() with
4273da6c28aaSamw 	 * a partially initialized vfs and a vfs_count of 1.  This is
4274da6c28aaSamw 	 * also called from vfs_rele() with a vfs_count of 0.  We can't
4275da6c28aaSamw 	 * call VFS_RELE() from domount() if VFS_MOUNT() hasn't successfully
4276da6c28aaSamw 	 * returned.  This is because VFS_MOUNT() fully initializes the
4277da6c28aaSamw 	 * vfs structure and its associated data.  VFS_RELE() will call
4278da6c28aaSamw 	 * VFS_FREEVFS() which may panic the system if the data structures
4279da6c28aaSamw 	 * aren't fully initialized from a successful VFS_MOUNT()).
4280da6c28aaSamw 	 */
4281da6c28aaSamw 
4282da6c28aaSamw 	/* If FEM was in use, make sure everything gets cleaned up */
4283da6c28aaSamw 	if (vfsp->vfs_femhead) {
4284da6c28aaSamw 		ASSERT(vfsp->vfs_femhead->femh_list == NULL);
4285da6c28aaSamw 		mutex_destroy(&vfsp->vfs_femhead->femh_lock);
4286da6c28aaSamw 		kmem_free(vfsp->vfs_femhead, sizeof (*(vfsp->vfs_femhead)));
4287da6c28aaSamw 		vfsp->vfs_femhead = NULL;
4288da6c28aaSamw 	}
4289da6c28aaSamw 
4290da6c28aaSamw 	if (vfsp->vfs_implp)
4291da6c28aaSamw 		vfsimpl_teardown(vfsp);
4292da6c28aaSamw 	sema_destroy(&vfsp->vfs_reflock);
4293da6c28aaSamw 	kmem_cache_free(vfs_cache, vfsp);
42947c478bd9Sstevel@tonic-gate }
42957c478bd9Sstevel@tonic-gate 
42967c478bd9Sstevel@tonic-gate /*
42977c478bd9Sstevel@tonic-gate  * Increments the vfs reference count by one atomically.
42987c478bd9Sstevel@tonic-gate  */
42997c478bd9Sstevel@tonic-gate void
vfs_hold(vfs_t * vfsp)43007c478bd9Sstevel@tonic-gate vfs_hold(vfs_t *vfsp)
43017c478bd9Sstevel@tonic-gate {
43021a5e258fSJosef 'Jeff' Sipek 	atomic_inc_32(&vfsp->vfs_count);
43037c478bd9Sstevel@tonic-gate 	ASSERT(vfsp->vfs_count != 0);
43047c478bd9Sstevel@tonic-gate }
43057c478bd9Sstevel@tonic-gate 
43067c478bd9Sstevel@tonic-gate /*
43077c478bd9Sstevel@tonic-gate  * Decrements the vfs reference count by one atomically. When
43087c478bd9Sstevel@tonic-gate  * vfs reference count becomes zero, it calls the file system
43097c478bd9Sstevel@tonic-gate  * specific vfs_freevfs() to free up the resources.
43107c478bd9Sstevel@tonic-gate  */
43117c478bd9Sstevel@tonic-gate void
vfs_rele(vfs_t * vfsp)43127c478bd9Sstevel@tonic-gate vfs_rele(vfs_t *vfsp)
43137c478bd9Sstevel@tonic-gate {
43147c478bd9Sstevel@tonic-gate 	ASSERT(vfsp->vfs_count != 0);
43151a5e258fSJosef 'Jeff' Sipek 	if (atomic_dec_32_nv(&vfsp->vfs_count) == 0) {
43167c478bd9Sstevel@tonic-gate 		VFS_FREEVFS(vfsp);
431793239addSjohnlev 		lofi_remove(vfsp);
43187c478bd9Sstevel@tonic-gate 		if (vfsp->vfs_zone)
4319a19609f8Sjv 			zone_rele_ref(&vfsp->vfs_implp->vi_zone_ref,
4320a19609f8Sjv 			    ZONE_REF_VFS);
43217c478bd9Sstevel@tonic-gate 		vfs_freemnttab(vfsp);
4322da6c28aaSamw 		vfs_free(vfsp);
43237c478bd9Sstevel@tonic-gate 	}
43247c478bd9Sstevel@tonic-gate }
43257c478bd9Sstevel@tonic-gate 
43267c478bd9Sstevel@tonic-gate /*
43277c478bd9Sstevel@tonic-gate  * Generic operations vector support.
43287c478bd9Sstevel@tonic-gate  *
43297c478bd9Sstevel@tonic-gate  * This is used to build operations vectors for both the vfs and vnode.
43307c478bd9Sstevel@tonic-gate  * It's normally called only when a file system is loaded.
43317c478bd9Sstevel@tonic-gate  *
43327c478bd9Sstevel@tonic-gate  * There are many possible algorithms for this, including the following:
43337c478bd9Sstevel@tonic-gate  *
43347c478bd9Sstevel@tonic-gate  *   (1) scan the list of known operations; for each, see if the file system
43357c478bd9Sstevel@tonic-gate  *       includes an entry for it, and fill it in as appropriate.
43367c478bd9Sstevel@tonic-gate  *
43377c478bd9Sstevel@tonic-gate  *   (2) set up defaults for all known operations.  scan the list of ops
43387c478bd9Sstevel@tonic-gate  *       supplied by the file system; for each which is both supplied and
43397c478bd9Sstevel@tonic-gate  *       known, fill it in.
43407c478bd9Sstevel@tonic-gate  *
43417c478bd9Sstevel@tonic-gate  *   (3) sort the lists of known ops & supplied ops; scan the list, filling
43427c478bd9Sstevel@tonic-gate  *       in entries as we go.
43437c478bd9Sstevel@tonic-gate  *
43447c478bd9Sstevel@tonic-gate  * we choose (1) for simplicity, and because performance isn't critical here.
43457c478bd9Sstevel@tonic-gate  * note that (2) could be sped up using a precomputed hash table on known ops.
43467c478bd9Sstevel@tonic-gate  * (3) could be faster than either, but only if the lists were very large or
43477c478bd9Sstevel@tonic-gate  * supplied in sorted order.
43487c478bd9Sstevel@tonic-gate  *
43497c478bd9Sstevel@tonic-gate  */
43507c478bd9Sstevel@tonic-gate 
43517c478bd9Sstevel@tonic-gate int
fs_build_vector(void * vector,int * unused_ops,const fs_operation_trans_def_t * translation,const fs_operation_def_t * operations)43527c478bd9Sstevel@tonic-gate fs_build_vector(void *vector, int *unused_ops,
43537c478bd9Sstevel@tonic-gate     const fs_operation_trans_def_t *translation,
43547c478bd9Sstevel@tonic-gate     const fs_operation_def_t *operations)
43557c478bd9Sstevel@tonic-gate {
43567c478bd9Sstevel@tonic-gate 	int i, num_trans, num_ops, used;
43577c478bd9Sstevel@tonic-gate 
4358aa59c4cbSrsb 	/*
4359aa59c4cbSrsb 	 * Count the number of translations and the number of supplied
4360aa59c4cbSrsb 	 * operations.
4361aa59c4cbSrsb 	 */
43627c478bd9Sstevel@tonic-gate 
43637c478bd9Sstevel@tonic-gate 	{
43647c478bd9Sstevel@tonic-gate 		const fs_operation_trans_def_t *p;
43657c478bd9Sstevel@tonic-gate 
43667c478bd9Sstevel@tonic-gate 		for (num_trans = 0, p = translation;
43677c478bd9Sstevel@tonic-gate 		    p->name != NULL;
43687c478bd9Sstevel@tonic-gate 		    num_trans++, p++)
43697c478bd9Sstevel@tonic-gate 			;
43707c478bd9Sstevel@tonic-gate 	}
43717c478bd9Sstevel@tonic-gate 
43727c478bd9Sstevel@tonic-gate 	{
43737c478bd9Sstevel@tonic-gate 		const fs_operation_def_t *p;
43747c478bd9Sstevel@tonic-gate 
43757c478bd9Sstevel@tonic-gate 		for (num_ops = 0, p = operations;
43767c478bd9Sstevel@tonic-gate 		    p->name != NULL;
43777c478bd9Sstevel@tonic-gate 		    num_ops++, p++)
43787c478bd9Sstevel@tonic-gate 			;
43797c478bd9Sstevel@tonic-gate 	}
43807c478bd9Sstevel@tonic-gate 
43817c478bd9Sstevel@tonic-gate 	/* Walk through each operation known to our caller.  There will be */
43827c478bd9Sstevel@tonic-gate 	/* one entry in the supplied "translation table" for each. */
43837c478bd9Sstevel@tonic-gate 
43847c478bd9Sstevel@tonic-gate 	used = 0;
43857c478bd9Sstevel@tonic-gate 
43867c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_trans; i++) {
43877c478bd9Sstevel@tonic-gate 		int j, found;
43887c478bd9Sstevel@tonic-gate 		char *curname;
43897c478bd9Sstevel@tonic-gate 		fs_generic_func_p result;
43907c478bd9Sstevel@tonic-gate 		fs_generic_func_p *location;
43917c478bd9Sstevel@tonic-gate 
43927c478bd9Sstevel@tonic-gate 		curname = translation[i].name;
43937c478bd9Sstevel@tonic-gate 
43947c478bd9Sstevel@tonic-gate 		/* Look for a matching operation in the list supplied by the */
43957c478bd9Sstevel@tonic-gate 		/* file system. */
43967c478bd9Sstevel@tonic-gate 
43977c478bd9Sstevel@tonic-gate 		found = 0;
43987c478bd9Sstevel@tonic-gate 
43997c478bd9Sstevel@tonic-gate 		for (j = 0; j < num_ops; j++) {
44007c478bd9Sstevel@tonic-gate 			if (strcmp(operations[j].name, curname) == 0) {
44017c478bd9Sstevel@tonic-gate 				used++;
44027c478bd9Sstevel@tonic-gate 				found = 1;
44037c478bd9Sstevel@tonic-gate 				break;
44047c478bd9Sstevel@tonic-gate 			}
44057c478bd9Sstevel@tonic-gate 		}
44067c478bd9Sstevel@tonic-gate 
4407aa59c4cbSrsb 		/*
4408aa59c4cbSrsb 		 * If the file system is using a "placeholder" for default
4409aa59c4cbSrsb 		 * or error functions, grab the appropriate function out of
4410aa59c4cbSrsb 		 * the translation table.  If the file system didn't supply
4411aa59c4cbSrsb 		 * this operation at all, use the default function.
4412aa59c4cbSrsb 		 */
44137c478bd9Sstevel@tonic-gate 
44147c478bd9Sstevel@tonic-gate 		if (found) {
4415aa59c4cbSrsb 			result = operations[j].func.fs_generic;
44167c478bd9Sstevel@tonic-gate 			if (result == fs_default) {
44177c478bd9Sstevel@tonic-gate 				result = translation[i].defaultFunc;
44187c478bd9Sstevel@tonic-gate 			} else if (result == fs_error) {
44197c478bd9Sstevel@tonic-gate 				result = translation[i].errorFunc;
44207c478bd9Sstevel@tonic-gate 			} else if (result == NULL) {
44217c478bd9Sstevel@tonic-gate 				/* Null values are PROHIBITED */
44227c478bd9Sstevel@tonic-gate 				return (EINVAL);
44237c478bd9Sstevel@tonic-gate 			}
44247c478bd9Sstevel@tonic-gate 		} else {
44257c478bd9Sstevel@tonic-gate 			result = translation[i].defaultFunc;
44267c478bd9Sstevel@tonic-gate 		}
44277c478bd9Sstevel@tonic-gate 
44287c478bd9Sstevel@tonic-gate 		/* Now store the function into the operations vector. */
44297c478bd9Sstevel@tonic-gate 
44307c478bd9Sstevel@tonic-gate 		location = (fs_generic_func_p *)
44317c478bd9Sstevel@tonic-gate 		    (((char *)vector) + translation[i].offset);
44327c478bd9Sstevel@tonic-gate 
44337c478bd9Sstevel@tonic-gate 		*location = result;
44347c478bd9Sstevel@tonic-gate 	}
44357c478bd9Sstevel@tonic-gate 
44367c478bd9Sstevel@tonic-gate 	*unused_ops = num_ops - used;
44377c478bd9Sstevel@tonic-gate 
44387c478bd9Sstevel@tonic-gate 	return (0);
44397c478bd9Sstevel@tonic-gate }
44407c478bd9Sstevel@tonic-gate 
44417c478bd9Sstevel@tonic-gate /* Placeholder functions, should never be called. */
44427c478bd9Sstevel@tonic-gate 
44437c478bd9Sstevel@tonic-gate int
fs_error(void)44447c478bd9Sstevel@tonic-gate fs_error(void)
44457c478bd9Sstevel@tonic-gate {
44467c478bd9Sstevel@tonic-gate 	cmn_err(CE_PANIC, "fs_error called");
44477c478bd9Sstevel@tonic-gate 	return (0);
44487c478bd9Sstevel@tonic-gate }
44497c478bd9Sstevel@tonic-gate 
44507c478bd9Sstevel@tonic-gate int
fs_default(void)44517c478bd9Sstevel@tonic-gate fs_default(void)
44527c478bd9Sstevel@tonic-gate {
44537c478bd9Sstevel@tonic-gate 	cmn_err(CE_PANIC, "fs_default called");
44547c478bd9Sstevel@tonic-gate 	return (0);
44557c478bd9Sstevel@tonic-gate }
44567c478bd9Sstevel@tonic-gate 
44577c478bd9Sstevel@tonic-gate #ifdef __sparc
44587c478bd9Sstevel@tonic-gate 
44597c478bd9Sstevel@tonic-gate /*
44607c478bd9Sstevel@tonic-gate  * Part of the implementation of booting off a mirrored root
44617c478bd9Sstevel@tonic-gate  * involves a change of dev_t for the root device.  To
44627c478bd9Sstevel@tonic-gate  * accomplish this, first remove the existing hash table
44637c478bd9Sstevel@tonic-gate  * entry for the root device, convert to the new dev_t,
44647c478bd9Sstevel@tonic-gate  * then re-insert in the hash table at the head of the list.
44657c478bd9Sstevel@tonic-gate  */
44667c478bd9Sstevel@tonic-gate void
vfs_root_redev(vfs_t * vfsp,dev_t ndev,int fstype)44677c478bd9Sstevel@tonic-gate vfs_root_redev(vfs_t *vfsp, dev_t ndev, int fstype)
44687c478bd9Sstevel@tonic-gate {
44697c478bd9Sstevel@tonic-gate 	vfs_list_lock();
44707c478bd9Sstevel@tonic-gate 
44717c478bd9Sstevel@tonic-gate 	vfs_hash_remove(vfsp);
44727c478bd9Sstevel@tonic-gate 
44737c478bd9Sstevel@tonic-gate 	vfsp->vfs_dev = ndev;
44747c478bd9Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, ndev, fstype);
44757c478bd9Sstevel@tonic-gate 
44767c478bd9Sstevel@tonic-gate 	vfs_hash_add(vfsp, 1);
44777c478bd9Sstevel@tonic-gate 
44787c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
44797c478bd9Sstevel@tonic-gate }
44807c478bd9Sstevel@tonic-gate 
44817c478bd9Sstevel@tonic-gate #else /* x86 NEWBOOT */
44827c478bd9Sstevel@tonic-gate 
448306bbe1e0Sedp #if defined(__x86)
448406bbe1e0Sedp extern int hvmboot_rootconf();
448506bbe1e0Sedp #endif /* __x86 */
448606bbe1e0Sedp 
44876cefaae1SJack Meng extern ib_boot_prop_t *iscsiboot_prop;
44886cefaae1SJack Meng 
44897c478bd9Sstevel@tonic-gate int
rootconf()44907c478bd9Sstevel@tonic-gate rootconf()
44917c478bd9Sstevel@tonic-gate {
44927c478bd9Sstevel@tonic-gate 	int error;
44937c478bd9Sstevel@tonic-gate 	struct vfssw *vsw;
44947c478bd9Sstevel@tonic-gate 	extern void pm_init();
4495843e1988Sjohnlev 	char *fstyp, *fsmod;
44966cefaae1SJack Meng 	int ret = -1;
44977c478bd9Sstevel@tonic-gate 
4498843e1988Sjohnlev 	getrootfs(&fstyp, &fsmod);
44997c478bd9Sstevel@tonic-gate 
450006bbe1e0Sedp #if defined(__x86)
450106bbe1e0Sedp 	/*
450206bbe1e0Sedp 	 * hvmboot_rootconf() is defined in the hvm_bootstrap misc module,
450306bbe1e0Sedp 	 * which lives in /platform/i86hvm, and hence is only available when
450406bbe1e0Sedp 	 * booted in an x86 hvm environment.  If the hvm_bootstrap misc module
450506bbe1e0Sedp 	 * is not available then the modstub for this function will return 0.
450606bbe1e0Sedp 	 * If the hvm_bootstrap misc module is available it will be loaded
450706bbe1e0Sedp 	 * and hvmboot_rootconf() will be invoked.
450806bbe1e0Sedp 	 */
450906bbe1e0Sedp 	if (error = hvmboot_rootconf())
451006bbe1e0Sedp 		return (error);
451106bbe1e0Sedp #endif /* __x86 */
451206bbe1e0Sedp 
45137c478bd9Sstevel@tonic-gate 	if (error = clboot_rootconf())
45147c478bd9Sstevel@tonic-gate 		return (error);
45157c478bd9Sstevel@tonic-gate 
4516843e1988Sjohnlev 	if (modload("fs", fsmod) == -1)
4517843e1988Sjohnlev 		panic("Cannot _init %s module", fsmod);
45187c478bd9Sstevel@tonic-gate 
45197c478bd9Sstevel@tonic-gate 	RLOCK_VFSSW();
45207c478bd9Sstevel@tonic-gate 	vsw = vfs_getvfsswbyname(fstyp);
45217c478bd9Sstevel@tonic-gate 	RUNLOCK_VFSSW();
452256c0b1daSPavel Filipensky 	if (vsw == NULL) {
452356c0b1daSPavel Filipensky 		cmn_err(CE_CONT, "Cannot find %s filesystem\n", fstyp);
452456c0b1daSPavel Filipensky 		return (ENXIO);
452556c0b1daSPavel Filipensky 	}
45267c478bd9Sstevel@tonic-gate 	VFS_INIT(rootvfs, &vsw->vsw_vfsops, 0);
45277c478bd9Sstevel@tonic-gate 	VFS_HOLD(rootvfs);
45287c478bd9Sstevel@tonic-gate 
45297c478bd9Sstevel@tonic-gate 	/* always mount readonly first */
45307c478bd9Sstevel@tonic-gate 	rootvfs->vfs_flag |= VFS_RDONLY;
45317c478bd9Sstevel@tonic-gate 
45327c478bd9Sstevel@tonic-gate 	pm_init();
45337c478bd9Sstevel@tonic-gate 
45346cefaae1SJack Meng 	if (netboot && iscsiboot_prop) {
45356cefaae1SJack Meng 		cmn_err(CE_WARN, "NFS boot and iSCSI boot"
45366cefaae1SJack Meng 		    " shouldn't happen in the same time");
45376cefaae1SJack Meng 		return (EINVAL);
45386cefaae1SJack Meng 	}
45396cefaae1SJack Meng 
4540dedec472SJack Meng 	if (netboot || iscsiboot_prop) {
45416cefaae1SJack Meng 		ret = strplumb();
4542dedec472SJack Meng 		if (ret != 0) {
4543dedec472SJack Meng 			cmn_err(CE_WARN, "Cannot plumb network device %d", ret);
4544dedec472SJack Meng 			return (EFAULT);
4545dedec472SJack Meng 		}
4546dedec472SJack Meng 	}
45476cefaae1SJack Meng 
45486cefaae1SJack Meng 	if ((ret == 0) && iscsiboot_prop) {
45496cefaae1SJack Meng 		ret = modload("drv", "iscsi");
45506cefaae1SJack Meng 		/* -1 indicates fail */
45516cefaae1SJack Meng 		if (ret == -1) {
45526cefaae1SJack Meng 			cmn_err(CE_WARN, "Failed to load iscsi module");
45536cefaae1SJack Meng 			iscsi_boot_prop_free();
45546cefaae1SJack Meng 			return (EINVAL);
45556cefaae1SJack Meng 		} else {
45566cefaae1SJack Meng 			if (!i_ddi_attach_pseudo_node("iscsi")) {
45576cefaae1SJack Meng 				cmn_err(CE_WARN,
45586cefaae1SJack Meng 				    "Failed to attach iscsi driver");
45596cefaae1SJack Meng 				iscsi_boot_prop_free();
45606cefaae1SJack Meng 				return (ENODEV);
45616cefaae1SJack Meng 			}
45626cefaae1SJack Meng 		}
45636cefaae1SJack Meng 	}
45647c478bd9Sstevel@tonic-gate 
45657c478bd9Sstevel@tonic-gate 	error = VFS_MOUNTROOT(rootvfs, ROOT_INIT);
45667c478bd9Sstevel@tonic-gate 	vfs_unrefvfssw(vsw);
45677c478bd9Sstevel@tonic-gate 	rootdev = rootvfs->vfs_dev;
45687c478bd9Sstevel@tonic-gate 
45697c478bd9Sstevel@tonic-gate 	if (error)
457056c0b1daSPavel Filipensky 		cmn_err(CE_CONT, "Cannot mount root on %s fstype %s\n",
457156c0b1daSPavel Filipensky 		    rootfs.bo_name, fstyp);
457256c0b1daSPavel Filipensky 	else
457356c0b1daSPavel Filipensky 		cmn_err(CE_CONT, "?root on %s fstype %s\n",
457456c0b1daSPavel Filipensky 		    rootfs.bo_name, fstyp);
45757c478bd9Sstevel@tonic-gate 	return (error);
45767c478bd9Sstevel@tonic-gate }
45777c478bd9Sstevel@tonic-gate 
45787c478bd9Sstevel@tonic-gate /*
45797c478bd9Sstevel@tonic-gate  * XXX this is called by nfs only and should probably be removed
45807c478bd9Sstevel@tonic-gate  * If booted with ASKNAME, prompt on the console for a filesystem
45817c478bd9Sstevel@tonic-gate  * name and return it.
45827c478bd9Sstevel@tonic-gate  */
45837c478bd9Sstevel@tonic-gate void
getfsname(char * askfor,char * name,size_t namelen)45847c478bd9Sstevel@tonic-gate getfsname(char *askfor, char *name, size_t namelen)
45857c478bd9Sstevel@tonic-gate {
45867c478bd9Sstevel@tonic-gate 	if (boothowto & RB_ASKNAME) {
45877c478bd9Sstevel@tonic-gate 		printf("%s name: ", askfor);
45887c478bd9Sstevel@tonic-gate 		console_gets(name, namelen);
45897c478bd9Sstevel@tonic-gate 	}
45907c478bd9Sstevel@tonic-gate }
45917c478bd9Sstevel@tonic-gate 
45927c478bd9Sstevel@tonic-gate /*
459356c0b1daSPavel Filipensky  * Init the root filesystem type (rootfs.bo_fstype) from the "fstype"
459456c0b1daSPavel Filipensky  * property.
459556c0b1daSPavel Filipensky  *
459656c0b1daSPavel Filipensky  * Filesystem types starting with the prefix "nfs" are diskless clients;
459756c0b1daSPavel Filipensky  * init the root filename name (rootfs.bo_name), too.
459856c0b1daSPavel Filipensky  *
459956c0b1daSPavel Filipensky  * If we are booting via NFS we currently have these options:
460056c0b1daSPavel Filipensky  *	nfs -	dynamically choose NFS V2, V3, or V4 (default)
460156c0b1daSPavel Filipensky  *	nfs2 -	force NFS V2
460256c0b1daSPavel Filipensky  *	nfs3 -	force NFS V3
460356c0b1daSPavel Filipensky  *	nfs4 -	force NFS V4
460456c0b1daSPavel Filipensky  * Because we need to maintain backward compatibility with the naming
460556c0b1daSPavel Filipensky  * convention that the NFS V2 filesystem name is "nfs" (see vfs_conf.c)
460656c0b1daSPavel Filipensky  * we need to map "nfs" => "nfsdyn" and "nfs2" => "nfs".  The dynamic
460756c0b1daSPavel Filipensky  * nfs module will map the type back to either "nfs", "nfs3", or "nfs4".
4608bd93c05dSAlexander Eremin  * This is only for root filesystems, all other uses will expect
4609bd93c05dSAlexander Eremin  * that "nfs" == NFS V2.
46107c478bd9Sstevel@tonic-gate  */
4611843e1988Sjohnlev static void
getrootfs(char ** fstypp,char ** fsmodp)4612843e1988Sjohnlev getrootfs(char **fstypp, char **fsmodp)
46137c478bd9Sstevel@tonic-gate {
46147c478bd9Sstevel@tonic-gate 	char *propstr = NULL;
46157c478bd9Sstevel@tonic-gate 
461656c0b1daSPavel Filipensky 	/*
461756c0b1daSPavel Filipensky 	 * Check fstype property; for diskless it should be one of "nfs",
461856c0b1daSPavel Filipensky 	 * "nfs2", "nfs3" or "nfs4".
461956c0b1daSPavel Filipensky 	 */
46207c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
46217c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "fstype", &propstr)
46227c478bd9Sstevel@tonic-gate 	    == DDI_SUCCESS) {
46237c478bd9Sstevel@tonic-gate 		(void) strncpy(rootfs.bo_fstype, propstr, BO_MAXFSNAME);
46247c478bd9Sstevel@tonic-gate 		ddi_prop_free(propstr);
4625b1b8ab34Slling 
4626b1b8ab34Slling 	/*
4627b1b8ab34Slling 	 * if the boot property 'fstype' is not set, but 'zfs-bootfs' is set,
4628b1b8ab34Slling 	 * assume the type of this root filesystem is 'zfs'.
4629b1b8ab34Slling 	 */
4630b1b8ab34Slling 	} else if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4631b1b8ab34Slling 	    DDI_PROP_DONTPASS, "zfs-bootfs", &propstr)
4632b1b8ab34Slling 	    == DDI_SUCCESS) {
4633b1b8ab34Slling 		(void) strncpy(rootfs.bo_fstype, "zfs", BO_MAXFSNAME);
4634b1b8ab34Slling 		ddi_prop_free(propstr);
46357c478bd9Sstevel@tonic-gate 	}
46367c478bd9Sstevel@tonic-gate 
4637843e1988Sjohnlev 	if (strncmp(rootfs.bo_fstype, "nfs", 3) != 0) {
4638843e1988Sjohnlev 		*fstypp = *fsmodp = rootfs.bo_fstype;
4639843e1988Sjohnlev 		return;
4640843e1988Sjohnlev 	}
46417c478bd9Sstevel@tonic-gate 
46427c478bd9Sstevel@tonic-gate 	++netboot;
464356c0b1daSPavel Filipensky 
464456c0b1daSPavel Filipensky 	if (strcmp(rootfs.bo_fstype, "nfs2") == 0)
464556c0b1daSPavel Filipensky 		(void) strcpy(rootfs.bo_fstype, "nfs");
464656c0b1daSPavel Filipensky 	else if (strcmp(rootfs.bo_fstype, "nfs") == 0)
464756c0b1daSPavel Filipensky 		(void) strcpy(rootfs.bo_fstype, "nfsdyn");
464856c0b1daSPavel Filipensky 
4649843e1988Sjohnlev 	/*
4650843e1988Sjohnlev 	 * check if path to network interface is specified in bootpath
4651843e1988Sjohnlev 	 * or by a hypervisor domain configuration file.
4652843e1988Sjohnlev 	 * XXPV - enable strlumb_get_netdev_path()
4653843e1988Sjohnlev 	 */
4654843e1988Sjohnlev 	if (ddi_prop_exists(DDI_DEV_T_ANY, ddi_root_node(), DDI_PROP_DONTPASS,
4655843e1988Sjohnlev 	    "xpv-nfsroot")) {
4656843e1988Sjohnlev 		(void) strcpy(rootfs.bo_name, "/xpvd/xnf@0");
4657843e1988Sjohnlev 	} else if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
46587c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "bootpath", &propstr)
46597c478bd9Sstevel@tonic-gate 	    == DDI_SUCCESS) {
46607c478bd9Sstevel@tonic-gate 		(void) strncpy(rootfs.bo_name, propstr, BO_MAXOBJNAME);
46617c478bd9Sstevel@tonic-gate 		ddi_prop_free(propstr);
46627c478bd9Sstevel@tonic-gate 	} else {
4663c2c0835aSToomas Soome 		rootfs.bo_name[0] = '\0';
46647c478bd9Sstevel@tonic-gate 	}
4665843e1988Sjohnlev 	*fstypp = rootfs.bo_fstype;
4666843e1988Sjohnlev 	*fsmodp = "nfs";
46677c478bd9Sstevel@tonic-gate }
46687c478bd9Sstevel@tonic-gate #endif
4669da6c28aaSamw 
4670da6c28aaSamw /*
4671da6c28aaSamw  * VFS feature routines
4672da6c28aaSamw  */
4673da6c28aaSamw 
4674da6c28aaSamw #define	VFTINDEX(feature)	(((feature) >> 32) & 0xFFFFFFFF)
4675da6c28aaSamw #define	VFTBITS(feature)	((feature) & 0xFFFFFFFFLL)
4676da6c28aaSamw 
4677da6c28aaSamw /* Register a feature in the vfs */
4678da6c28aaSamw void
vfs_set_feature(vfs_t * vfsp,vfs_feature_t feature)4679da6c28aaSamw vfs_set_feature(vfs_t *vfsp, vfs_feature_t feature)
4680da6c28aaSamw {
4681da6c28aaSamw 	/* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4682da6c28aaSamw 	if (vfsp->vfs_implp == NULL)
4683da6c28aaSamw 		return;
4684da6c28aaSamw 
4685da6c28aaSamw 	vfsp->vfs_featureset[VFTINDEX(feature)] |= VFTBITS(feature);
4686da6c28aaSamw }
4687da6c28aaSamw 
468844bffe01SMark Shellenbaum void
vfs_clear_feature(vfs_t * vfsp,vfs_feature_t feature)468944bffe01SMark Shellenbaum vfs_clear_feature(vfs_t *vfsp, vfs_feature_t feature)
469044bffe01SMark Shellenbaum {
469144bffe01SMark Shellenbaum 	/* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
469244bffe01SMark Shellenbaum 	if (vfsp->vfs_implp == NULL)
469344bffe01SMark Shellenbaum 		return;
469444bffe01SMark Shellenbaum 	vfsp->vfs_featureset[VFTINDEX(feature)] &= VFTBITS(~feature);
469544bffe01SMark Shellenbaum }
469644bffe01SMark Shellenbaum 
4697da6c28aaSamw /*
4698da6c28aaSamw  * Query a vfs for a feature.
4699da6c28aaSamw  * Returns 1 if feature is present, 0 if not
4700da6c28aaSamw  */
4701da6c28aaSamw int
vfs_has_feature(vfs_t * vfsp,vfs_feature_t feature)4702da6c28aaSamw vfs_has_feature(vfs_t *vfsp, vfs_feature_t feature)
4703da6c28aaSamw {
4704da6c28aaSamw 	int	ret = 0;
4705da6c28aaSamw 
4706da6c28aaSamw 	/* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4707da6c28aaSamw 	if (vfsp->vfs_implp == NULL)
4708da6c28aaSamw 		return (ret);
4709da6c28aaSamw 
4710da6c28aaSamw 	if (vfsp->vfs_featureset[VFTINDEX(feature)] & VFTBITS(feature))
4711da6c28aaSamw 		ret = 1;
4712da6c28aaSamw 
4713da6c28aaSamw 	return (ret);
4714da6c28aaSamw }
471579a28c7aSmarks 
471679a28c7aSmarks /*
471779a28c7aSmarks  * Propagate feature set from one vfs to another
471879a28c7aSmarks  */
471979a28c7aSmarks void
vfs_propagate_features(vfs_t * from,vfs_t * to)472079a28c7aSmarks vfs_propagate_features(vfs_t *from, vfs_t *to)
472179a28c7aSmarks {
472279a28c7aSmarks 	int i;
472379a28c7aSmarks 
472479a28c7aSmarks 	if (to->vfs_implp == NULL || from->vfs_implp == NULL)
472579a28c7aSmarks 		return;
472679a28c7aSmarks 
472779a28c7aSmarks 	for (i = 1; i <= to->vfs_featureset[0]; i++) {
472879a28c7aSmarks 		to->vfs_featureset[i] = from->vfs_featureset[i];
472979a28c7aSmarks 	}
473079a28c7aSmarks }
473193239addSjohnlev 
47320fbb751dSJohn Levon #define	LOFINODE_PATH "/dev/lofi/%d"
473393239addSjohnlev 
473493239addSjohnlev /*
473593239addSjohnlev  * Return the vnode for the lofi node if there's a lofi mount in place.
473693239addSjohnlev  * Returns -1 when there's no lofi node, 0 on success, and > 0 on
473793239addSjohnlev  * failure.
473893239addSjohnlev  */
473993239addSjohnlev int
vfs_get_lofi(vfs_t * vfsp,vnode_t ** vpp)474093239addSjohnlev vfs_get_lofi(vfs_t *vfsp, vnode_t **vpp)
474193239addSjohnlev {
474293239addSjohnlev 	char *path = NULL;
474393239addSjohnlev 	int strsize;
474493239addSjohnlev 	int err;
474593239addSjohnlev 
4746406fc510SToomas Soome 	if (vfsp->vfs_lofi_id == 0) {
474793239addSjohnlev 		*vpp = NULL;
474893239addSjohnlev 		return (-1);
474993239addSjohnlev 	}
475093239addSjohnlev 
4751406fc510SToomas Soome 	strsize = snprintf(NULL, 0, LOFINODE_PATH, vfsp->vfs_lofi_id);
475293239addSjohnlev 	path = kmem_alloc(strsize + 1, KM_SLEEP);
4753406fc510SToomas Soome 	(void) snprintf(path, strsize + 1, LOFINODE_PATH, vfsp->vfs_lofi_id);
47540fbb751dSJohn Levon 
47550fbb751dSJohn Levon 	/*
47560fbb751dSJohn Levon 	 * We may be inside a zone, so we need to use the /dev path, but
47570fbb751dSJohn Levon 	 * it's created asynchronously, so we wait here.
47580fbb751dSJohn Levon 	 */
47590fbb751dSJohn Levon 	for (;;) {
47600fbb751dSJohn Levon 		err = lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, vpp);
47610fbb751dSJohn Levon 
47620fbb751dSJohn Levon 		if (err != ENOENT)
47630fbb751dSJohn Levon 			break;
476493239addSjohnlev 
47650fbb751dSJohn Levon 		if ((err = delay_sig(hz / 8)) == EINTR)
47660fbb751dSJohn Levon 			break;
47670fbb751dSJohn Levon 	}
476893239addSjohnlev 
476993239addSjohnlev 	if (err)
477093239addSjohnlev 		*vpp = NULL;
477193239addSjohnlev 
477293239addSjohnlev 	kmem_free(path, strsize + 1);
477393239addSjohnlev 	return (err);
477493239addSjohnlev }
4775