xref: /titanic_52/usr/src/uts/common/fs/vfs.c (revision a29e56d91db891741f1af9f6bbd3e3c3cac5f19b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
25  * Copyright 2017 RackTop Systems.
26  * Copyright 2016 Nexenta Systems, Inc.
27  * Copyright 2016 Toomas Soome <tsoome@me.com>
28  */
29 
30 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
31 /*	  All Rights Reserved  	*/
32 
33 /*
34  * University Copyright- Copyright (c) 1982, 1986, 1988
35  * The Regents of the University of California
36  * All Rights Reserved
37  *
38  * University Acknowledgment- Portions of this document are derived from
39  * software developed by the University of California, Berkeley, and its
40  * contributors.
41  */
42 
43 #include <sys/types.h>
44 #include <sys/t_lock.h>
45 #include <sys/param.h>
46 #include <sys/errno.h>
47 #include <sys/user.h>
48 #include <sys/fstyp.h>
49 #include <sys/kmem.h>
50 #include <sys/systm.h>
51 #include <sys/proc.h>
52 #include <sys/mount.h>
53 #include <sys/vfs.h>
54 #include <sys/vfs_opreg.h>
55 #include <sys/fem.h>
56 #include <sys/mntent.h>
57 #include <sys/stat.h>
58 #include <sys/statvfs.h>
59 #include <sys/statfs.h>
60 #include <sys/cred.h>
61 #include <sys/vnode.h>
62 #include <sys/rwstlock.h>
63 #include <sys/dnlc.h>
64 #include <sys/file.h>
65 #include <sys/time.h>
66 #include <sys/atomic.h>
67 #include <sys/cmn_err.h>
68 #include <sys/buf.h>
69 #include <sys/swap.h>
70 #include <sys/debug.h>
71 #include <sys/vnode.h>
72 #include <sys/modctl.h>
73 #include <sys/ddi.h>
74 #include <sys/pathname.h>
75 #include <sys/bootconf.h>
76 #include <sys/dumphdr.h>
77 #include <sys/dc_ki.h>
78 #include <sys/poll.h>
79 #include <sys/sunddi.h>
80 #include <sys/sysmacros.h>
81 #include <sys/zone.h>
82 #include <sys/policy.h>
83 #include <sys/ctfs.h>
84 #include <sys/objfs.h>
85 #include <sys/console.h>
86 #include <sys/reboot.h>
87 #include <sys/attr.h>
88 #include <sys/zio.h>
89 #include <sys/spa.h>
90 #include <sys/lofi.h>
91 #include <sys/bootprops.h>
92 #include <sys/avl.h>
93 
94 #include <vm/page.h>
95 
96 #include <fs/fs_subr.h>
97 /* Private interfaces to create vopstats-related data structures */
98 extern void		initialize_vopstats(vopstats_t *);
99 extern vopstats_t	*get_fstype_vopstats(struct vfs *, struct vfssw *);
100 extern vsk_anchor_t	*get_vskstat_anchor(struct vfs *);
101 
102 static void vfs_clearmntopt_nolock(mntopts_t *, const char *, int);
103 static void vfs_setmntopt_nolock(mntopts_t *, const char *,
104     const char *, int, int);
105 static int  vfs_optionisset_nolock(const mntopts_t *, const char *, char **);
106 static void vfs_freemnttab(struct vfs *);
107 static void vfs_freeopt(mntopt_t *);
108 static void vfs_swapopttbl_nolock(mntopts_t *, mntopts_t *);
109 static void vfs_swapopttbl(mntopts_t *, mntopts_t *);
110 static void vfs_copyopttbl_extend(const mntopts_t *, mntopts_t *, int);
111 static void vfs_createopttbl_extend(mntopts_t *, const char *,
112     const mntopts_t *);
113 static char **vfs_copycancelopt_extend(char **const, int);
114 static void vfs_freecancelopt(char **);
115 static void getrootfs(char **, char **);
116 static int getmacpath(dev_info_t *, void *);
117 static void vfs_mnttabvp_setup(void);
118 
119 struct ipmnt {
120 	struct ipmnt	*mip_next;
121 	dev_t		mip_dev;
122 	struct vfs	*mip_vfsp;
123 };
124 
125 static kmutex_t		vfs_miplist_mutex;
126 static struct ipmnt	*vfs_miplist = NULL;
127 static struct ipmnt	*vfs_miplist_end = NULL;
128 
129 static kmem_cache_t *vfs_cache;	/* Pointer to VFS kmem cache */
130 
131 /*
132  * VFS global data.
133  */
134 vnode_t *rootdir;		/* pointer to root inode vnode. */
135 vnode_t *devicesdir;		/* pointer to inode of devices root */
136 vnode_t	*devdir;		/* pointer to inode of dev root */
137 
138 char *server_rootpath;		/* root path for diskless clients */
139 char *server_hostname;		/* hostname of diskless server */
140 
141 static struct vfs root;
142 static struct vfs devices;
143 static struct vfs dev;
144 struct vfs *rootvfs = &root;	/* pointer to root vfs; head of VFS list. */
145 avl_tree_t vfs_by_dev;		/* avl tree to index mounted VFSs by dev */
146 avl_tree_t vfs_by_mntpnt;	/* avl tree to index mounted VFSs by mntpnt */
147 uint64_t vfs_curr_mntix;	/* counter to provide a unique mntix for
148 				 * entries in the above avl trees.
149 				 * protected by vfslist lock */
150 rvfs_t *rvfs_list;		/* array of vfs ptrs for vfs hash list */
151 int vfshsz = 512;		/* # of heads/locks in vfs hash arrays */
152 				/* must be power of 2!	*/
153 timespec_t vfs_mnttab_ctime;	/* mnttab created time */
154 timespec_t vfs_mnttab_mtime;	/* mnttab last modified time */
155 char *vfs_dummyfstype = "\0";
156 struct pollhead vfs_pollhd;	/* for mnttab pollers */
157 struct vnode *vfs_mntdummyvp;	/* to fake mnttab read/write for file events */
158 int	mntfstype;		/* will be set once mnt fs is mounted */
159 
160 /*
161  * Table for generic options recognized in the VFS layer and acted
162  * on at this level before parsing file system specific options.
163  * The nosuid option is stronger than any of the devices and setuid
164  * options, so those are canceled when nosuid is seen.
165  *
166  * All options which are added here need to be added to the
167  * list of standard options in usr/src/cmd/fs.d/fslib.c as well.
168  */
169 /*
170  * VFS Mount options table
171  */
172 static char *ro_cancel[] = { MNTOPT_RW, NULL };
173 static char *rw_cancel[] = { MNTOPT_RO, NULL };
174 static char *suid_cancel[] = { MNTOPT_NOSUID, NULL };
175 static char *nosuid_cancel[] = { MNTOPT_SUID, MNTOPT_DEVICES, MNTOPT_NODEVICES,
176     MNTOPT_NOSETUID, MNTOPT_SETUID, NULL };
177 static char *devices_cancel[] = { MNTOPT_NODEVICES, NULL };
178 static char *nodevices_cancel[] = { MNTOPT_DEVICES, NULL };
179 static char *setuid_cancel[] = { MNTOPT_NOSETUID, NULL };
180 static char *nosetuid_cancel[] = { MNTOPT_SETUID, NULL };
181 static char *nbmand_cancel[] = { MNTOPT_NONBMAND, NULL };
182 static char *nonbmand_cancel[] = { MNTOPT_NBMAND, NULL };
183 static char *exec_cancel[] = { MNTOPT_NOEXEC, NULL };
184 static char *noexec_cancel[] = { MNTOPT_EXEC, NULL };
185 static char *follow_cancel[] = { MNTOPT_NOFOLLOW, NULL };
186 static char *nofollow_cancel[] = { MNTOPT_FOLLOW, NULL };
187 
188 static const mntopt_t mntopts[] = {
189 /*
190  *	option name		cancel options		default arg	flags
191  */
192 	{ MNTOPT_REMOUNT,	NULL,			NULL,
193 		MO_NODISPLAY, (void *)0 },
194 	{ MNTOPT_RO,		ro_cancel,		NULL,		0,
195 		(void *)0 },
196 	{ MNTOPT_RW,		rw_cancel,		NULL,		0,
197 		(void *)0 },
198 	{ MNTOPT_SUID,		suid_cancel,		NULL,		0,
199 		(void *)0 },
200 	{ MNTOPT_NOSUID,	nosuid_cancel,		NULL,		0,
201 		(void *)0 },
202 	{ MNTOPT_DEVICES,	devices_cancel,		NULL,		0,
203 		(void *)0 },
204 	{ MNTOPT_NODEVICES,	nodevices_cancel,	NULL,		0,
205 		(void *)0 },
206 	{ MNTOPT_SETUID,	setuid_cancel,		NULL,		0,
207 		(void *)0 },
208 	{ MNTOPT_NOSETUID,	nosetuid_cancel,	NULL,		0,
209 		(void *)0 },
210 	{ MNTOPT_NBMAND,	nbmand_cancel,		NULL,		0,
211 		(void *)0 },
212 	{ MNTOPT_NONBMAND,	nonbmand_cancel,	NULL,		0,
213 		(void *)0 },
214 	{ MNTOPT_EXEC,		exec_cancel,		NULL,		0,
215 		(void *)0 },
216 	{ MNTOPT_NOEXEC,	noexec_cancel,		NULL,		0,
217 		(void *)0 },
218 	{ MNTOPT_FOLLOW,	follow_cancel,		NULL,		0,
219 		(void *)0 },
220 	{ MNTOPT_NOFOLLOW,	nofollow_cancel,	NULL,		0,
221 		(void *)0 },
222 };
223 
224 const mntopts_t vfs_mntopts = {
225 	sizeof (mntopts) / sizeof (mntopt_t),
226 	(mntopt_t *)&mntopts[0]
227 };
228 
229 /*
230  * File system operation dispatch functions.
231  */
232 
233 int
234 fsop_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
235 {
236 	return (*(vfsp)->vfs_op->vfs_mount)(vfsp, mvp, uap, cr);
237 }
238 
239 int
240 fsop_unmount(vfs_t *vfsp, int flag, cred_t *cr)
241 {
242 	return (*(vfsp)->vfs_op->vfs_unmount)(vfsp, flag, cr);
243 }
244 
245 int
246 fsop_root(vfs_t *vfsp, vnode_t **vpp)
247 {
248 	refstr_t *mntpt;
249 	int ret = (*(vfsp)->vfs_op->vfs_root)(vfsp, vpp);
250 	/*
251 	 * Make sure this root has a path.  With lofs, it is possible to have
252 	 * a NULL mountpoint.
253 	 */
254 	if (ret == 0 && vfsp->vfs_mntpt != NULL && (*vpp)->v_path == NULL) {
255 		mntpt = vfs_getmntpoint(vfsp);
256 		vn_setpath_str(*vpp, refstr_value(mntpt),
257 		    strlen(refstr_value(mntpt)));
258 		refstr_rele(mntpt);
259 	}
260 
261 	return (ret);
262 }
263 
264 int
265 fsop_statfs(vfs_t *vfsp, statvfs64_t *sp)
266 {
267 	return (*(vfsp)->vfs_op->vfs_statvfs)(vfsp, sp);
268 }
269 
270 int
271 fsop_sync(vfs_t *vfsp, short flag, cred_t *cr)
272 {
273 	return (*(vfsp)->vfs_op->vfs_sync)(vfsp, flag, cr);
274 }
275 
276 int
277 fsop_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
278 {
279 	/*
280 	 * In order to handle system attribute fids in a manner
281 	 * transparent to the underlying fs, we embed the fid for
282 	 * the sysattr parent object in the sysattr fid and tack on
283 	 * some extra bytes that only the sysattr layer knows about.
284 	 *
285 	 * This guarantees that sysattr fids are larger than other fids
286 	 * for this vfs. If the vfs supports the sysattr view interface
287 	 * (as indicated by VFSFT_SYSATTR_VIEWS), we cannot have a size
288 	 * collision with XATTR_FIDSZ.
289 	 */
290 	if (vfs_has_feature(vfsp, VFSFT_SYSATTR_VIEWS) &&
291 	    fidp->fid_len == XATTR_FIDSZ)
292 		return (xattr_dir_vget(vfsp, vpp, fidp));
293 
294 	return (*(vfsp)->vfs_op->vfs_vget)(vfsp, vpp, fidp);
295 }
296 
297 int
298 fsop_mountroot(vfs_t *vfsp, enum whymountroot reason)
299 {
300 	return (*(vfsp)->vfs_op->vfs_mountroot)(vfsp, reason);
301 }
302 
303 void
304 fsop_freefs(vfs_t *vfsp)
305 {
306 	(*(vfsp)->vfs_op->vfs_freevfs)(vfsp);
307 }
308 
309 int
310 fsop_vnstate(vfs_t *vfsp, vnode_t *vp, vntrans_t nstate)
311 {
312 	return ((*(vfsp)->vfs_op->vfs_vnstate)(vfsp, vp, nstate));
313 }
314 
315 int
316 fsop_sync_by_kind(int fstype, short flag, cred_t *cr)
317 {
318 	ASSERT((fstype >= 0) && (fstype < nfstype));
319 
320 	if (ALLOCATED_VFSSW(&vfssw[fstype]) && VFS_INSTALLED(&vfssw[fstype]))
321 		return (*vfssw[fstype].vsw_vfsops.vfs_sync) (NULL, flag, cr);
322 	else
323 		return (ENOTSUP);
324 }
325 
326 /*
327  * File system initialization.  vfs_setfsops() must be called from a file
328  * system's init routine.
329  */
330 
331 static int
332 fs_copyfsops(const fs_operation_def_t *template, vfsops_t *actual,
333     int *unused_ops)
334 {
335 	static const fs_operation_trans_def_t vfs_ops_table[] = {
336 		VFSNAME_MOUNT, offsetof(vfsops_t, vfs_mount),
337 			fs_nosys, fs_nosys,
338 
339 		VFSNAME_UNMOUNT, offsetof(vfsops_t, vfs_unmount),
340 			fs_nosys, fs_nosys,
341 
342 		VFSNAME_ROOT, offsetof(vfsops_t, vfs_root),
343 			fs_nosys, fs_nosys,
344 
345 		VFSNAME_STATVFS, offsetof(vfsops_t, vfs_statvfs),
346 			fs_nosys, fs_nosys,
347 
348 		VFSNAME_SYNC, offsetof(vfsops_t, vfs_sync),
349 			(fs_generic_func_p) fs_sync,
350 			(fs_generic_func_p) fs_sync,	/* No errors allowed */
351 
352 		VFSNAME_VGET, offsetof(vfsops_t, vfs_vget),
353 			fs_nosys, fs_nosys,
354 
355 		VFSNAME_MOUNTROOT, offsetof(vfsops_t, vfs_mountroot),
356 			fs_nosys, fs_nosys,
357 
358 		VFSNAME_FREEVFS, offsetof(vfsops_t, vfs_freevfs),
359 			(fs_generic_func_p)fs_freevfs,
360 			(fs_generic_func_p)fs_freevfs,	/* Shouldn't fail */
361 
362 		VFSNAME_VNSTATE, offsetof(vfsops_t, vfs_vnstate),
363 			(fs_generic_func_p)fs_nosys,
364 			(fs_generic_func_p)fs_nosys,
365 
366 		NULL, 0, NULL, NULL
367 	};
368 
369 	return (fs_build_vector(actual, unused_ops, vfs_ops_table, template));
370 }
371 
372 void
373 zfs_boot_init(void)
374 {
375 	if (strcmp(rootfs.bo_fstype, MNTTYPE_ZFS) == 0)
376 		spa_boot_init();
377 }
378 
379 int
380 vfs_setfsops(int fstype, const fs_operation_def_t *template, vfsops_t **actual)
381 {
382 	int error;
383 	int unused_ops;
384 
385 	/*
386 	 * Verify that fstype refers to a valid fs.  Note that
387 	 * 0 is valid since it's used to set "stray" ops.
388 	 */
389 	if ((fstype < 0) || (fstype >= nfstype))
390 		return (EINVAL);
391 
392 	if (!ALLOCATED_VFSSW(&vfssw[fstype]))
393 		return (EINVAL);
394 
395 	/* Set up the operations vector. */
396 
397 	error = fs_copyfsops(template, &vfssw[fstype].vsw_vfsops, &unused_ops);
398 
399 	if (error != 0)
400 		return (error);
401 
402 	vfssw[fstype].vsw_flag |= VSW_INSTALLED;
403 
404 	if (actual != NULL)
405 		*actual = &vfssw[fstype].vsw_vfsops;
406 
407 #if DEBUG
408 	if (unused_ops != 0)
409 		cmn_err(CE_WARN, "vfs_setfsops: %s: %d operations supplied "
410 		    "but not used", vfssw[fstype].vsw_name, unused_ops);
411 #endif
412 
413 	return (0);
414 }
415 
416 int
417 vfs_makefsops(const fs_operation_def_t *template, vfsops_t **actual)
418 {
419 	int error;
420 	int unused_ops;
421 
422 	*actual = (vfsops_t *)kmem_alloc(sizeof (vfsops_t), KM_SLEEP);
423 
424 	error = fs_copyfsops(template, *actual, &unused_ops);
425 	if (error != 0) {
426 		kmem_free(*actual, sizeof (vfsops_t));
427 		*actual = NULL;
428 		return (error);
429 	}
430 
431 	return (0);
432 }
433 
434 /*
435  * Free a vfsops structure created as a result of vfs_makefsops().
436  * NOTE: For a vfsops structure initialized by vfs_setfsops(), use
437  * vfs_freevfsops_by_type().
438  */
439 void
440 vfs_freevfsops(vfsops_t *vfsops)
441 {
442 	kmem_free(vfsops, sizeof (vfsops_t));
443 }
444 
445 /*
446  * Since the vfsops structure is part of the vfssw table and wasn't
447  * really allocated, we're not really freeing anything.  We keep
448  * the name for consistency with vfs_freevfsops().  We do, however,
449  * need to take care of a little bookkeeping.
450  * NOTE: For a vfsops structure created by vfs_setfsops(), use
451  * vfs_freevfsops_by_type().
452  */
453 int
454 vfs_freevfsops_by_type(int fstype)
455 {
456 
457 	/* Verify that fstype refers to a loaded fs (and not fsid 0). */
458 	if ((fstype <= 0) || (fstype >= nfstype))
459 		return (EINVAL);
460 
461 	WLOCK_VFSSW();
462 	if ((vfssw[fstype].vsw_flag & VSW_INSTALLED) == 0) {
463 		WUNLOCK_VFSSW();
464 		return (EINVAL);
465 	}
466 
467 	vfssw[fstype].vsw_flag &= ~VSW_INSTALLED;
468 	WUNLOCK_VFSSW();
469 
470 	return (0);
471 }
472 
473 /* Support routines used to reference vfs_op */
474 
475 /* Set the operations vector for a vfs */
476 void
477 vfs_setops(vfs_t *vfsp, vfsops_t *vfsops)
478 {
479 	vfsops_t	*op;
480 
481 	ASSERT(vfsp != NULL);
482 	ASSERT(vfsops != NULL);
483 
484 	op = vfsp->vfs_op;
485 	membar_consumer();
486 	if (vfsp->vfs_femhead == NULL &&
487 	    atomic_cas_ptr(&vfsp->vfs_op, op, vfsops) == op) {
488 		return;
489 	}
490 	fsem_setvfsops(vfsp, vfsops);
491 }
492 
493 /* Retrieve the operations vector for a vfs */
494 vfsops_t *
495 vfs_getops(vfs_t *vfsp)
496 {
497 	vfsops_t	*op;
498 
499 	ASSERT(vfsp != NULL);
500 
501 	op = vfsp->vfs_op;
502 	membar_consumer();
503 	if (vfsp->vfs_femhead == NULL && op == vfsp->vfs_op) {
504 		return (op);
505 	} else {
506 		return (fsem_getvfsops(vfsp));
507 	}
508 }
509 
510 /*
511  * Returns non-zero (1) if the vfsops matches that of the vfs.
512  * Returns zero (0) if not.
513  */
514 int
515 vfs_matchops(vfs_t *vfsp, vfsops_t *vfsops)
516 {
517 	return (vfs_getops(vfsp) == vfsops);
518 }
519 
520 /*
521  * Returns non-zero (1) if the file system has installed a non-default,
522  * non-error vfs_sync routine.  Returns zero (0) otherwise.
523  */
524 int
525 vfs_can_sync(vfs_t *vfsp)
526 {
527 	/* vfs_sync() routine is not the default/error function */
528 	return (vfs_getops(vfsp)->vfs_sync != fs_sync);
529 }
530 
531 /*
532  * Initialize a vfs structure.
533  */
534 void
535 vfs_init(vfs_t *vfsp, vfsops_t *op, void *data)
536 {
537 	/* Other initialization has been moved to vfs_alloc() */
538 	vfsp->vfs_count = 0;
539 	vfsp->vfs_next = vfsp;
540 	vfsp->vfs_prev = vfsp;
541 	vfsp->vfs_zone_next = vfsp;
542 	vfsp->vfs_zone_prev = vfsp;
543 	vfsp->vfs_lofi_id = 0;
544 	sema_init(&vfsp->vfs_reflock, 1, NULL, SEMA_DEFAULT, NULL);
545 	vfsimpl_setup(vfsp);
546 	vfsp->vfs_data = (data);
547 	vfs_setops((vfsp), (op));
548 }
549 
550 /*
551  * Allocate and initialize the vfs implementation private data
552  * structure, vfs_impl_t.
553  */
554 void
555 vfsimpl_setup(vfs_t *vfsp)
556 {
557 	int i;
558 
559 	if (vfsp->vfs_implp != NULL) {
560 		return;
561 	}
562 
563 	vfsp->vfs_implp = kmem_alloc(sizeof (vfs_impl_t), KM_SLEEP);
564 	/* Note that these are #define'd in vfs.h */
565 	vfsp->vfs_vskap = NULL;
566 	vfsp->vfs_fstypevsp = NULL;
567 
568 	/* Set size of counted array, then zero the array */
569 	vfsp->vfs_featureset[0] = VFS_FEATURE_MAXSZ - 1;
570 	for (i = 1; i <  VFS_FEATURE_MAXSZ; i++) {
571 		vfsp->vfs_featureset[i] = 0;
572 	}
573 }
574 
575 /*
576  * Release the vfs_impl_t structure, if it exists. Some unbundled
577  * filesystems may not use the newer version of vfs and thus
578  * would not contain this implementation private data structure.
579  */
580 void
581 vfsimpl_teardown(vfs_t *vfsp)
582 {
583 	vfs_impl_t	*vip = vfsp->vfs_implp;
584 
585 	if (vip == NULL)
586 		return;
587 
588 	kmem_free(vfsp->vfs_implp, sizeof (vfs_impl_t));
589 	vfsp->vfs_implp = NULL;
590 }
591 
592 /*
593  * VFS system calls: mount, umount, syssync, statfs, fstatfs, statvfs,
594  * fstatvfs, and sysfs moved to common/syscall.
595  */
596 
597 /*
598  * Update every mounted file system.  We call the vfs_sync operation of
599  * each file system type, passing it a NULL vfsp to indicate that all
600  * mounted file systems of that type should be updated.
601  */
602 void
603 vfs_sync(int flag)
604 {
605 	struct vfssw *vswp;
606 	RLOCK_VFSSW();
607 	for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
608 		if (ALLOCATED_VFSSW(vswp) && VFS_INSTALLED(vswp)) {
609 			vfs_refvfssw(vswp);
610 			RUNLOCK_VFSSW();
611 			(void) (*vswp->vsw_vfsops.vfs_sync)(NULL, flag,
612 			    CRED());
613 			vfs_unrefvfssw(vswp);
614 			RLOCK_VFSSW();
615 		}
616 	}
617 	RUNLOCK_VFSSW();
618 }
619 
620 void
621 sync(void)
622 {
623 	vfs_sync(0);
624 }
625 
626 /*
627  * compare function for vfs_by_dev avl tree. compare dev first, then mntix
628  */
629 static int
630 vfs_cmp_dev(const void *aa, const void *bb)
631 {
632 	const vfs_t *a = aa;
633 	const vfs_t *b = bb;
634 
635 	if (a->vfs_dev < b->vfs_dev)
636 		return (-1);
637 	if (a->vfs_dev > b->vfs_dev)
638 		return (1);
639 	if (a->vfs_mntix < b->vfs_mntix)
640 		return (-1);
641 	if (a->vfs_mntix > b->vfs_mntix)
642 		return (1);
643 	return (0);
644 }
645 
646 /*
647  * compare function for vfs_by_mntpnt avl tree. compare mntpnt first, then mntix
648  */
649 static int
650 vfs_cmp_mntpnt(const void *aa, const void *bb)
651 {
652 	const vfs_t *a = aa;
653 	const vfs_t *b = bb;
654 	int ret;
655 
656 	ret = strcmp(refstr_value(a->vfs_mntpt), refstr_value(b->vfs_mntpt));
657 	if (ret < 0)
658 		return (-1);
659 	if (ret > 0)
660 		return (1);
661 	if (a->vfs_mntix < b->vfs_mntix)
662 		return (-1);
663 	if (a->vfs_mntix > b->vfs_mntix)
664 		return (1);
665 	return (0);
666 }
667 
668 /*
669  * External routines.
670  */
671 
672 krwlock_t vfssw_lock;	/* lock accesses to vfssw */
673 
674 /*
675  * Lock for accessing the vfs linked list.  Initialized in vfs_mountroot(),
676  * but otherwise should be accessed only via vfs_list_lock() and
677  * vfs_list_unlock().  Also used to protect the timestamp for mods to the list.
678  */
679 static krwlock_t vfslist;
680 
681 /*
682  * Mount devfs on /devices. This is done right after root is mounted
683  * to provide device access support for the system
684  */
685 static void
686 vfs_mountdevices(void)
687 {
688 	struct vfssw *vsw;
689 	struct vnode *mvp;
690 	struct mounta mounta = {	/* fake mounta for devfs_mount() */
691 		NULL,
692 		NULL,
693 		MS_SYSSPACE,
694 		NULL,
695 		NULL,
696 		0,
697 		NULL,
698 		0
699 	};
700 
701 	/*
702 	 * _init devfs module to fill in the vfssw
703 	 */
704 	if (modload("fs", "devfs") == -1)
705 		panic("Cannot _init devfs module");
706 
707 	/*
708 	 * Hold vfs
709 	 */
710 	RLOCK_VFSSW();
711 	vsw = vfs_getvfsswbyname("devfs");
712 	VFS_INIT(&devices, &vsw->vsw_vfsops, NULL);
713 	VFS_HOLD(&devices);
714 
715 	/*
716 	 * Locate mount point
717 	 */
718 	if (lookupname("/devices", UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp))
719 		panic("Cannot find /devices");
720 
721 	/*
722 	 * Perform the mount of /devices
723 	 */
724 	if (VFS_MOUNT(&devices, mvp, &mounta, CRED()))
725 		panic("Cannot mount /devices");
726 
727 	RUNLOCK_VFSSW();
728 
729 	/*
730 	 * Set appropriate members and add to vfs list for mnttab display
731 	 */
732 	vfs_setresource(&devices, "/devices", 0);
733 	vfs_setmntpoint(&devices, "/devices", 0);
734 
735 	/*
736 	 * Hold the root of /devices so it won't go away
737 	 */
738 	if (VFS_ROOT(&devices, &devicesdir))
739 		panic("vfs_mountdevices: not devices root");
740 
741 	if (vfs_lock(&devices) != 0) {
742 		VN_RELE(devicesdir);
743 		cmn_err(CE_NOTE, "Cannot acquire vfs_lock of /devices");
744 		return;
745 	}
746 
747 	if (vn_vfswlock(mvp) != 0) {
748 		vfs_unlock(&devices);
749 		VN_RELE(devicesdir);
750 		cmn_err(CE_NOTE, "Cannot acquire vfswlock of /devices");
751 		return;
752 	}
753 
754 	vfs_add(mvp, &devices, 0);
755 	vn_vfsunlock(mvp);
756 	vfs_unlock(&devices);
757 	VN_RELE(devicesdir);
758 }
759 
760 /*
761  * mount the first instance of /dev  to root and remain mounted
762  */
763 static void
764 vfs_mountdev1(void)
765 {
766 	struct vfssw *vsw;
767 	struct vnode *mvp;
768 	struct mounta mounta = {	/* fake mounta for sdev_mount() */
769 		NULL,
770 		NULL,
771 		MS_SYSSPACE | MS_OVERLAY,
772 		NULL,
773 		NULL,
774 		0,
775 		NULL,
776 		0
777 	};
778 
779 	/*
780 	 * _init dev module to fill in the vfssw
781 	 */
782 	if (modload("fs", "dev") == -1)
783 		cmn_err(CE_PANIC, "Cannot _init dev module\n");
784 
785 	/*
786 	 * Hold vfs
787 	 */
788 	RLOCK_VFSSW();
789 	vsw = vfs_getvfsswbyname("dev");
790 	VFS_INIT(&dev, &vsw->vsw_vfsops, NULL);
791 	VFS_HOLD(&dev);
792 
793 	/*
794 	 * Locate mount point
795 	 */
796 	if (lookupname("/dev", UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp))
797 		cmn_err(CE_PANIC, "Cannot find /dev\n");
798 
799 	/*
800 	 * Perform the mount of /dev
801 	 */
802 	if (VFS_MOUNT(&dev, mvp, &mounta, CRED()))
803 		cmn_err(CE_PANIC, "Cannot mount /dev 1\n");
804 
805 	RUNLOCK_VFSSW();
806 
807 	/*
808 	 * Set appropriate members and add to vfs list for mnttab display
809 	 */
810 	vfs_setresource(&dev, "/dev", 0);
811 	vfs_setmntpoint(&dev, "/dev", 0);
812 
813 	/*
814 	 * Hold the root of /dev so it won't go away
815 	 */
816 	if (VFS_ROOT(&dev, &devdir))
817 		cmn_err(CE_PANIC, "vfs_mountdev1: not dev root");
818 
819 	if (vfs_lock(&dev) != 0) {
820 		VN_RELE(devdir);
821 		cmn_err(CE_NOTE, "Cannot acquire vfs_lock of /dev");
822 		return;
823 	}
824 
825 	if (vn_vfswlock(mvp) != 0) {
826 		vfs_unlock(&dev);
827 		VN_RELE(devdir);
828 		cmn_err(CE_NOTE, "Cannot acquire vfswlock of /dev");
829 		return;
830 	}
831 
832 	vfs_add(mvp, &dev, 0);
833 	vn_vfsunlock(mvp);
834 	vfs_unlock(&dev);
835 	VN_RELE(devdir);
836 }
837 
838 /*
839  * Mount required filesystem. This is done right after root is mounted.
840  */
841 static void
842 vfs_mountfs(char *module, char *spec, char *path)
843 {
844 	struct vnode *mvp;
845 	struct mounta mounta;
846 	vfs_t *vfsp;
847 
848 	bzero(&mounta, sizeof (mounta));
849 	mounta.flags = MS_SYSSPACE | MS_DATA;
850 	mounta.fstype = module;
851 	mounta.spec = spec;
852 	mounta.dir = path;
853 	if (lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp)) {
854 		cmn_err(CE_WARN, "Cannot find %s", path);
855 		return;
856 	}
857 	if (domount(NULL, &mounta, mvp, CRED(), &vfsp))
858 		cmn_err(CE_WARN, "Cannot mount %s", path);
859 	else
860 		VFS_RELE(vfsp);
861 	VN_RELE(mvp);
862 }
863 
864 /*
865  * vfs_mountroot is called by main() to mount the root filesystem.
866  */
867 void
868 vfs_mountroot(void)
869 {
870 	struct vnode	*rvp = NULL;
871 	char		*path;
872 	size_t		plen;
873 	struct vfssw	*vswp;
874 	proc_t		*p;
875 
876 	rw_init(&vfssw_lock, NULL, RW_DEFAULT, NULL);
877 	rw_init(&vfslist, NULL, RW_DEFAULT, NULL);
878 
879 	/*
880 	 * Alloc the avl trees for quick indexing via dev and mountpoint
881 	 */
882 	avl_create(&vfs_by_dev, vfs_cmp_dev, sizeof(vfs_t),
883 	    offsetof(vfs_t, vfs_avldev));
884 	avl_create(&vfs_by_mntpnt, vfs_cmp_mntpnt, sizeof(vfs_t),
885 	    offsetof(vfs_t, vfs_avlmntpnt));
886 
887 	/*
888 	 * Alloc the vfs hash bucket array and locks
889 	 */
890 	rvfs_list = kmem_zalloc(vfshsz * sizeof (rvfs_t), KM_SLEEP);
891 
892 	/*
893 	 * Call machine-dependent routine "rootconf" to choose a root
894 	 * file system type.
895 	 */
896 	if (rootconf())
897 		panic("vfs_mountroot: cannot mount root");
898 	/*
899 	 * Get vnode for '/'.  Set up rootdir, u.u_rdir and u.u_cdir
900 	 * to point to it.  These are used by lookuppn() so that it
901 	 * knows where to start from ('/' or '.').
902 	 */
903 	vfs_setmntpoint(rootvfs, "/", 0);
904 	if (VFS_ROOT(rootvfs, &rootdir))
905 		panic("vfs_mountroot: no root vnode");
906 
907 	/*
908 	 * At this point, the process tree consists of p0 and possibly some
909 	 * direct children of p0.  (i.e. there are no grandchildren)
910 	 *
911 	 * Walk through them all, setting their current directory.
912 	 */
913 	mutex_enter(&pidlock);
914 	for (p = practive; p != NULL; p = p->p_next) {
915 		ASSERT(p == &p0 || p->p_parent == &p0);
916 
917 		PTOU(p)->u_cdir = rootdir;
918 		VN_HOLD(PTOU(p)->u_cdir);
919 		PTOU(p)->u_rdir = NULL;
920 	}
921 	mutex_exit(&pidlock);
922 
923 	/*
924 	 * Setup the global zone's rootvp, now that it exists.
925 	 */
926 	global_zone->zone_rootvp = rootdir;
927 	VN_HOLD(global_zone->zone_rootvp);
928 
929 	/*
930 	 * Notify the module code that it can begin using the
931 	 * root filesystem instead of the boot program's services.
932 	 */
933 	modrootloaded = 1;
934 
935 	/*
936 	 * Special handling for a ZFS root file system.
937 	 */
938 	zfs_boot_init();
939 
940 	/*
941 	 * Set up mnttab information for root
942 	 */
943 	vfs_setresource(rootvfs, rootfs.bo_name, 0);
944 
945 	/*
946 	 * Notify cluster software that the root filesystem is available.
947 	 */
948 	clboot_mountroot();
949 
950 	/* Now that we're all done with the root FS, set up its vopstats */
951 	if ((vswp = vfs_getvfsswbyvfsops(vfs_getops(rootvfs))) != NULL) {
952 		/* Set flag for statistics collection */
953 		if (vswp->vsw_flag & VSW_STATS) {
954 			initialize_vopstats(&rootvfs->vfs_vopstats);
955 			rootvfs->vfs_flag |= VFS_STATS;
956 			rootvfs->vfs_fstypevsp =
957 			    get_fstype_vopstats(rootvfs, vswp);
958 			rootvfs->vfs_vskap = get_vskstat_anchor(rootvfs);
959 		}
960 		vfs_unrefvfssw(vswp);
961 	}
962 
963 	/*
964 	 * Mount /devices, /dev instance 1, /system/contract, /etc/mnttab,
965 	 * /etc/svc/volatile, /etc/dfs/sharetab, /system/object, and /proc.
966 	 */
967 	vfs_mountdevices();
968 	vfs_mountdev1();
969 
970 	vfs_mountfs("ctfs", "ctfs", CTFS_ROOT);
971 	vfs_mountfs("proc", "/proc", "/proc");
972 	vfs_mountfs("mntfs", "/etc/mnttab", "/etc/mnttab");
973 	vfs_mountfs("tmpfs", "/etc/svc/volatile", "/etc/svc/volatile");
974 	vfs_mountfs("objfs", "objfs", OBJFS_ROOT);
975 	vfs_mountfs("bootfs", "bootfs", "/system/boot");
976 
977 	if (getzoneid() == GLOBAL_ZONEID) {
978 		vfs_mountfs("sharefs", "sharefs", "/etc/dfs/sharetab");
979 	}
980 
981 	if (strcmp(rootfs.bo_fstype, "zfs") != 0) {
982 		/*
983 		 * Look up the root device via devfs so that a dv_node is
984 		 * created for it. The vnode is never VN_RELE()ed.
985 		 * We allocate more than MAXPATHLEN so that the
986 		 * buffer passed to i_ddi_prompath_to_devfspath() is
987 		 * exactly MAXPATHLEN (the function expects a buffer
988 		 * of that length).
989 		 */
990 		plen = strlen("/devices");
991 		path = kmem_alloc(plen + MAXPATHLEN, KM_SLEEP);
992 		(void) strcpy(path, "/devices");
993 
994 		if (i_ddi_prompath_to_devfspath(rootfs.bo_name, path + plen)
995 		    != DDI_SUCCESS ||
996 		    lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &rvp)) {
997 
998 			/* NUL terminate in case "path" has garbage */
999 			path[plen + MAXPATHLEN - 1] = '\0';
1000 #ifdef	DEBUG
1001 			cmn_err(CE_WARN, "!Cannot lookup root device: %s",
1002 			    path);
1003 #endif
1004 		}
1005 		kmem_free(path, plen + MAXPATHLEN);
1006 	}
1007 
1008 	vfs_mnttabvp_setup();
1009 }
1010 
1011 /*
1012  * Check to see if our "block device" is actually a file.  If so,
1013  * automatically add a lofi device, and keep track of this fact.
1014  */
1015 static int
1016 lofi_add(const char *fsname, struct vfs *vfsp,
1017     mntopts_t *mntopts, struct mounta *uap)
1018 {
1019 	int fromspace = (uap->flags & MS_SYSSPACE) ?
1020 	    UIO_SYSSPACE : UIO_USERSPACE;
1021 	struct lofi_ioctl *li = NULL;
1022 	struct vnode *vp = NULL;
1023 	struct pathname	pn = { NULL };
1024 	ldi_ident_t ldi_id;
1025 	ldi_handle_t ldi_hdl;
1026 	vfssw_t *vfssw;
1027 	int id;
1028 	int err = 0;
1029 
1030 	if ((vfssw = vfs_getvfssw(fsname)) == NULL)
1031 		return (0);
1032 
1033 	if (!(vfssw->vsw_flag & VSW_CANLOFI)) {
1034 		vfs_unrefvfssw(vfssw);
1035 		return (0);
1036 	}
1037 
1038 	vfs_unrefvfssw(vfssw);
1039 	vfssw = NULL;
1040 
1041 	if (pn_get(uap->spec, fromspace, &pn) != 0)
1042 		return (0);
1043 
1044 	if (lookupname(uap->spec, fromspace, FOLLOW, NULL, &vp) != 0)
1045 		goto out;
1046 
1047 	if (vp->v_type != VREG)
1048 		goto out;
1049 
1050 	/* OK, this is a lofi mount. */
1051 
1052 	if ((uap->flags & (MS_REMOUNT|MS_GLOBAL)) ||
1053 	    vfs_optionisset_nolock(mntopts, MNTOPT_SUID, NULL) ||
1054 	    vfs_optionisset_nolock(mntopts, MNTOPT_SETUID, NULL) ||
1055 	    vfs_optionisset_nolock(mntopts, MNTOPT_DEVICES, NULL)) {
1056 		err = EINVAL;
1057 		goto out;
1058 	}
1059 
1060 	ldi_id = ldi_ident_from_anon();
1061 	li = kmem_zalloc(sizeof (*li), KM_SLEEP);
1062 	(void) strlcpy(li->li_filename, pn.pn_path, MAXPATHLEN);
1063 
1064 	err = ldi_open_by_name("/dev/lofictl", FREAD | FWRITE, kcred,
1065 	    &ldi_hdl, ldi_id);
1066 
1067 	if (err)
1068 		goto out2;
1069 
1070 	err = ldi_ioctl(ldi_hdl, LOFI_MAP_FILE, (intptr_t)li,
1071 	    FREAD | FWRITE | FKIOCTL, kcred, &id);
1072 
1073 	(void) ldi_close(ldi_hdl, FREAD | FWRITE, kcred);
1074 
1075 	if (!err)
1076 		vfsp->vfs_lofi_id = id;
1077 
1078 out2:
1079 	ldi_ident_release(ldi_id);
1080 out:
1081 	if (li != NULL)
1082 		kmem_free(li, sizeof (*li));
1083 	if (vp != NULL)
1084 		VN_RELE(vp);
1085 	pn_free(&pn);
1086 	return (err);
1087 }
1088 
1089 static void
1090 lofi_remove(struct vfs *vfsp)
1091 {
1092 	struct lofi_ioctl *li = NULL;
1093 	ldi_ident_t ldi_id;
1094 	ldi_handle_t ldi_hdl;
1095 	int err;
1096 
1097 	if (vfsp->vfs_lofi_id == 0)
1098 		return;
1099 
1100 	ldi_id = ldi_ident_from_anon();
1101 
1102 	li = kmem_zalloc(sizeof (*li), KM_SLEEP);
1103 	li->li_id = vfsp->vfs_lofi_id;
1104 	li->li_cleanup = B_TRUE;
1105 
1106 	err = ldi_open_by_name("/dev/lofictl", FREAD | FWRITE, kcred,
1107 	    &ldi_hdl, ldi_id);
1108 
1109 	if (err)
1110 		goto out;
1111 
1112 	err = ldi_ioctl(ldi_hdl, LOFI_UNMAP_FILE_MINOR, (intptr_t)li,
1113 	    FREAD | FWRITE | FKIOCTL, kcred, NULL);
1114 
1115 	(void) ldi_close(ldi_hdl, FREAD | FWRITE, kcred);
1116 
1117 	if (!err)
1118 		vfsp->vfs_lofi_id = 0;
1119 
1120 out:
1121 	ldi_ident_release(ldi_id);
1122 	if (li != NULL)
1123 		kmem_free(li, sizeof (*li));
1124 }
1125 
1126 /*
1127  * Common mount code.  Called from the system call entry point, from autofs,
1128  * nfsv4 trigger mounts, and from pxfs.
1129  *
1130  * Takes the effective file system type, mount arguments, the mount point
1131  * vnode, flags specifying whether the mount is a remount and whether it
1132  * should be entered into the vfs list, and credentials.  Fills in its vfspp
1133  * parameter with the mounted file system instance's vfs.
1134  *
1135  * Note that the effective file system type is specified as a string.  It may
1136  * be null, in which case it's determined from the mount arguments, and may
1137  * differ from the type specified in the mount arguments; this is a hook to
1138  * allow interposition when instantiating file system instances.
1139  *
1140  * The caller is responsible for releasing its own hold on the mount point
1141  * vp (this routine does its own hold when necessary).
1142  * Also note that for remounts, the mount point vp should be the vnode for
1143  * the root of the file system rather than the vnode that the file system
1144  * is mounted on top of.
1145  */
1146 int
1147 domount(char *fsname, struct mounta *uap, vnode_t *vp, struct cred *credp,
1148     struct vfs **vfspp)
1149 {
1150 	struct vfssw	*vswp;
1151 	vfsops_t	*vfsops;
1152 	struct vfs	*vfsp;
1153 	struct vnode	*bvp;
1154 	dev_t		bdev = 0;
1155 	mntopts_t	mnt_mntopts;
1156 	int		error = 0;
1157 	int		copyout_error = 0;
1158 	int		ovflags;
1159 	char		*opts = uap->optptr;
1160 	char		*inargs = opts;
1161 	int		optlen = uap->optlen;
1162 	int		remount;
1163 	int		rdonly;
1164 	int		nbmand = 0;
1165 	int		delmip = 0;
1166 	int		addmip = 0;
1167 	int		splice = ((uap->flags & MS_NOSPLICE) == 0);
1168 	int		fromspace = (uap->flags & MS_SYSSPACE) ?
1169 	    UIO_SYSSPACE : UIO_USERSPACE;
1170 	char		*resource = NULL, *mountpt = NULL;
1171 	refstr_t	*oldresource, *oldmntpt;
1172 	struct pathname	pn, rpn;
1173 	vsk_anchor_t	*vskap;
1174 	char fstname[FSTYPSZ];
1175 	zone_t		*zone;
1176 
1177 	/*
1178 	 * The v_flag value for the mount point vp is permanently set
1179 	 * to VVFSLOCK so that no one bypasses the vn_vfs*locks routine
1180 	 * for mount point locking.
1181 	 */
1182 	mutex_enter(&vp->v_lock);
1183 	vp->v_flag |= VVFSLOCK;
1184 	mutex_exit(&vp->v_lock);
1185 
1186 	mnt_mntopts.mo_count = 0;
1187 	/*
1188 	 * Find the ops vector to use to invoke the file system-specific mount
1189 	 * method.  If the fsname argument is non-NULL, use it directly.
1190 	 * Otherwise, dig the file system type information out of the mount
1191 	 * arguments.
1192 	 *
1193 	 * A side effect is to hold the vfssw entry.
1194 	 *
1195 	 * Mount arguments can be specified in several ways, which are
1196 	 * distinguished by flag bit settings.  The preferred way is to set
1197 	 * MS_OPTIONSTR, indicating an 8 argument mount with the file system
1198 	 * type supplied as a character string and the last two arguments
1199 	 * being a pointer to a character buffer and the size of the buffer.
1200 	 * On entry, the buffer holds a null terminated list of options; on
1201 	 * return, the string is the list of options the file system
1202 	 * recognized. If MS_DATA is set arguments five and six point to a
1203 	 * block of binary data which the file system interprets.
1204 	 * A further wrinkle is that some callers don't set MS_FSS and MS_DATA
1205 	 * consistently with these conventions.  To handle them, we check to
1206 	 * see whether the pointer to the file system name has a numeric value
1207 	 * less than 256.  If so, we treat it as an index.
1208 	 */
1209 	if (fsname != NULL) {
1210 		if ((vswp = vfs_getvfssw(fsname)) == NULL) {
1211 			return (EINVAL);
1212 		}
1213 	} else if (uap->flags & (MS_OPTIONSTR | MS_DATA | MS_FSS)) {
1214 		size_t n;
1215 		uint_t fstype;
1216 
1217 		fsname = fstname;
1218 
1219 		if ((fstype = (uintptr_t)uap->fstype) < 256) {
1220 			RLOCK_VFSSW();
1221 			if (fstype == 0 || fstype >= nfstype ||
1222 			    !ALLOCATED_VFSSW(&vfssw[fstype])) {
1223 				RUNLOCK_VFSSW();
1224 				return (EINVAL);
1225 			}
1226 			(void) strcpy(fsname, vfssw[fstype].vsw_name);
1227 			RUNLOCK_VFSSW();
1228 			if ((vswp = vfs_getvfssw(fsname)) == NULL)
1229 				return (EINVAL);
1230 		} else {
1231 			/*
1232 			 * Handle either kernel or user address space.
1233 			 */
1234 			if (uap->flags & MS_SYSSPACE) {
1235 				error = copystr(uap->fstype, fsname,
1236 				    FSTYPSZ, &n);
1237 			} else {
1238 				error = copyinstr(uap->fstype, fsname,
1239 				    FSTYPSZ, &n);
1240 			}
1241 			if (error) {
1242 				if (error == ENAMETOOLONG)
1243 					return (EINVAL);
1244 				return (error);
1245 			}
1246 			if ((vswp = vfs_getvfssw(fsname)) == NULL)
1247 				return (EINVAL);
1248 		}
1249 	} else {
1250 		if ((vswp = vfs_getvfsswbyvfsops(vfs_getops(rootvfs))) == NULL)
1251 			return (EINVAL);
1252 		fsname = vswp->vsw_name;
1253 	}
1254 	if (!VFS_INSTALLED(vswp))
1255 		return (EINVAL);
1256 
1257 	if ((error = secpolicy_fs_allowed_mount(fsname)) != 0)  {
1258 		vfs_unrefvfssw(vswp);
1259 		return (error);
1260 	}
1261 
1262 	vfsops = &vswp->vsw_vfsops;
1263 
1264 	vfs_copyopttbl(&vswp->vsw_optproto, &mnt_mntopts);
1265 	/*
1266 	 * Fetch mount options and parse them for generic vfs options
1267 	 */
1268 	if (uap->flags & MS_OPTIONSTR) {
1269 		/*
1270 		 * Limit the buffer size
1271 		 */
1272 		if (optlen < 0 || optlen > MAX_MNTOPT_STR) {
1273 			error = EINVAL;
1274 			goto errout;
1275 		}
1276 		if ((uap->flags & MS_SYSSPACE) == 0) {
1277 			inargs = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
1278 			inargs[0] = '\0';
1279 			if (optlen) {
1280 				error = copyinstr(opts, inargs, (size_t)optlen,
1281 				    NULL);
1282 				if (error) {
1283 					goto errout;
1284 				}
1285 			}
1286 		}
1287 		vfs_parsemntopts(&mnt_mntopts, inargs, 0);
1288 	}
1289 	/*
1290 	 * Flag bits override the options string.
1291 	 */
1292 	if (uap->flags & MS_REMOUNT)
1293 		vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_REMOUNT, NULL, 0, 0);
1294 	if (uap->flags & MS_RDONLY)
1295 		vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_RO, NULL, 0, 0);
1296 	if (uap->flags & MS_NOSUID)
1297 		vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL, 0, 0);
1298 
1299 	/*
1300 	 * Check if this is a remount; must be set in the option string and
1301 	 * the file system must support a remount option.
1302 	 */
1303 	if (remount = vfs_optionisset_nolock(&mnt_mntopts,
1304 	    MNTOPT_REMOUNT, NULL)) {
1305 		if (!(vswp->vsw_flag & VSW_CANREMOUNT)) {
1306 			error = ENOTSUP;
1307 			goto errout;
1308 		}
1309 		uap->flags |= MS_REMOUNT;
1310 	}
1311 
1312 	/*
1313 	 * uap->flags and vfs_optionisset() should agree.
1314 	 */
1315 	if (rdonly = vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_RO, NULL)) {
1316 		uap->flags |= MS_RDONLY;
1317 	}
1318 	if (vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL)) {
1319 		uap->flags |= MS_NOSUID;
1320 	}
1321 	nbmand = vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_NBMAND, NULL);
1322 	ASSERT(splice || !remount);
1323 	/*
1324 	 * If we are splicing the fs into the namespace,
1325 	 * perform mount point checks.
1326 	 *
1327 	 * We want to resolve the path for the mount point to eliminate
1328 	 * '.' and ".." and symlinks in mount points; we can't do the
1329 	 * same for the resource string, since it would turn
1330 	 * "/dev/dsk/c0t0d0s0" into "/devices/pci@...".  We need to do
1331 	 * this before grabbing vn_vfswlock(), because otherwise we
1332 	 * would deadlock with lookuppn().
1333 	 */
1334 	if (splice) {
1335 		ASSERT(vp->v_count > 0);
1336 
1337 		/*
1338 		 * Pick up mount point and device from appropriate space.
1339 		 */
1340 		if (pn_get(uap->spec, fromspace, &pn) == 0) {
1341 			resource = kmem_alloc(pn.pn_pathlen + 1,
1342 			    KM_SLEEP);
1343 			(void) strcpy(resource, pn.pn_path);
1344 			pn_free(&pn);
1345 		}
1346 		/*
1347 		 * Do a lookupname prior to taking the
1348 		 * writelock. Mark this as completed if
1349 		 * successful for later cleanup and addition to
1350 		 * the mount in progress table.
1351 		 */
1352 		if ((uap->flags & MS_GLOBAL) == 0 &&
1353 		    lookupname(uap->spec, fromspace,
1354 		    FOLLOW, NULL, &bvp) == 0) {
1355 			addmip = 1;
1356 		}
1357 
1358 		if ((error = pn_get(uap->dir, fromspace, &pn)) == 0) {
1359 			pathname_t *pnp;
1360 
1361 			if (*pn.pn_path != '/') {
1362 				error = EINVAL;
1363 				pn_free(&pn);
1364 				goto errout;
1365 			}
1366 			pn_alloc(&rpn);
1367 			/*
1368 			 * Kludge to prevent autofs from deadlocking with
1369 			 * itself when it calls domount().
1370 			 *
1371 			 * If autofs is calling, it is because it is doing
1372 			 * (autofs) mounts in the process of an NFS mount.  A
1373 			 * lookuppn() here would cause us to block waiting for
1374 			 * said NFS mount to complete, which can't since this
1375 			 * is the thread that was supposed to doing it.
1376 			 */
1377 			if (fromspace == UIO_USERSPACE) {
1378 				if ((error = lookuppn(&pn, &rpn, FOLLOW, NULL,
1379 				    NULL)) == 0) {
1380 					pnp = &rpn;
1381 				} else {
1382 					/*
1383 					 * The file disappeared or otherwise
1384 					 * became inaccessible since we opened
1385 					 * it; might as well fail the mount
1386 					 * since the mount point is no longer
1387 					 * accessible.
1388 					 */
1389 					pn_free(&rpn);
1390 					pn_free(&pn);
1391 					goto errout;
1392 				}
1393 			} else {
1394 				pnp = &pn;
1395 			}
1396 			mountpt = kmem_alloc(pnp->pn_pathlen + 1, KM_SLEEP);
1397 			(void) strcpy(mountpt, pnp->pn_path);
1398 
1399 			/*
1400 			 * If the addition of the zone's rootpath
1401 			 * would push us over a total path length
1402 			 * of MAXPATHLEN, we fail the mount with
1403 			 * ENAMETOOLONG, which is what we would have
1404 			 * gotten if we were trying to perform the same
1405 			 * mount in the global zone.
1406 			 *
1407 			 * strlen() doesn't count the trailing
1408 			 * '\0', but zone_rootpathlen counts both a
1409 			 * trailing '/' and the terminating '\0'.
1410 			 */
1411 			if ((curproc->p_zone->zone_rootpathlen - 1 +
1412 			    strlen(mountpt)) > MAXPATHLEN ||
1413 			    (resource != NULL &&
1414 			    (curproc->p_zone->zone_rootpathlen - 1 +
1415 			    strlen(resource)) > MAXPATHLEN)) {
1416 				error = ENAMETOOLONG;
1417 			}
1418 
1419 			pn_free(&rpn);
1420 			pn_free(&pn);
1421 		}
1422 
1423 		if (error)
1424 			goto errout;
1425 
1426 		/*
1427 		 * Prevent path name resolution from proceeding past
1428 		 * the mount point.
1429 		 */
1430 		if (vn_vfswlock(vp) != 0) {
1431 			error = EBUSY;
1432 			goto errout;
1433 		}
1434 
1435 		/*
1436 		 * Verify that it's legitimate to establish a mount on
1437 		 * the prospective mount point.
1438 		 */
1439 		if (vn_mountedvfs(vp) != NULL) {
1440 			/*
1441 			 * The mount point lock was obtained after some
1442 			 * other thread raced through and established a mount.
1443 			 */
1444 			vn_vfsunlock(vp);
1445 			error = EBUSY;
1446 			goto errout;
1447 		}
1448 		if (vp->v_flag & VNOMOUNT) {
1449 			vn_vfsunlock(vp);
1450 			error = EINVAL;
1451 			goto errout;
1452 		}
1453 	}
1454 	if ((uap->flags & (MS_DATA | MS_OPTIONSTR)) == 0) {
1455 		uap->dataptr = NULL;
1456 		uap->datalen = 0;
1457 	}
1458 
1459 	/*
1460 	 * If this is a remount, we don't want to create a new VFS.
1461 	 * Instead, we pass the existing one with a remount flag.
1462 	 */
1463 	if (remount) {
1464 		/*
1465 		 * Confirm that the mount point is the root vnode of the
1466 		 * file system that is being remounted.
1467 		 * This can happen if the user specifies a different
1468 		 * mount point directory pathname in the (re)mount command.
1469 		 *
1470 		 * Code below can only be reached if splice is true, so it's
1471 		 * safe to do vn_vfsunlock() here.
1472 		 */
1473 		if ((vp->v_flag & VROOT) == 0) {
1474 			vn_vfsunlock(vp);
1475 			error = ENOENT;
1476 			goto errout;
1477 		}
1478 		/*
1479 		 * Disallow making file systems read-only unless file system
1480 		 * explicitly allows it in its vfssw.  Ignore other flags.
1481 		 */
1482 		if (rdonly && vn_is_readonly(vp) == 0 &&
1483 		    (vswp->vsw_flag & VSW_CANRWRO) == 0) {
1484 			vn_vfsunlock(vp);
1485 			error = EINVAL;
1486 			goto errout;
1487 		}
1488 		/*
1489 		 * Disallow changing the NBMAND disposition of the file
1490 		 * system on remounts.
1491 		 */
1492 		if ((nbmand && ((vp->v_vfsp->vfs_flag & VFS_NBMAND) == 0)) ||
1493 		    (!nbmand && (vp->v_vfsp->vfs_flag & VFS_NBMAND))) {
1494 			vn_vfsunlock(vp);
1495 			error = EINVAL;
1496 			goto errout;
1497 		}
1498 		vfsp = vp->v_vfsp;
1499 		ovflags = vfsp->vfs_flag;
1500 		vfsp->vfs_flag |= VFS_REMOUNT;
1501 		vfsp->vfs_flag &= ~VFS_RDONLY;
1502 	} else {
1503 		vfsp = vfs_alloc(KM_SLEEP);
1504 		VFS_INIT(vfsp, vfsops, NULL);
1505 	}
1506 
1507 	VFS_HOLD(vfsp);
1508 
1509 	if ((error = lofi_add(fsname, vfsp, &mnt_mntopts, uap)) != 0) {
1510 		if (!remount) {
1511 			if (splice)
1512 				vn_vfsunlock(vp);
1513 			vfs_free(vfsp);
1514 		} else {
1515 			vn_vfsunlock(vp);
1516 			VFS_RELE(vfsp);
1517 		}
1518 		goto errout;
1519 	}
1520 
1521 	/*
1522 	 * PRIV_SYS_MOUNT doesn't mean you can become root.
1523 	 */
1524 	if (vfsp->vfs_lofi_id != 0) {
1525 		uap->flags |= MS_NOSUID;
1526 		vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL, 0, 0);
1527 	}
1528 
1529 	/*
1530 	 * The vfs_reflock is not used anymore the code below explicitly
1531 	 * holds it preventing others accesing it directly.
1532 	 */
1533 	if ((sema_tryp(&vfsp->vfs_reflock) == 0) &&
1534 	    !(vfsp->vfs_flag & VFS_REMOUNT))
1535 		cmn_err(CE_WARN,
1536 		    "mount type %s couldn't get vfs_reflock", vswp->vsw_name);
1537 
1538 	/*
1539 	 * Lock the vfs. If this is a remount we want to avoid spurious umount
1540 	 * failures that happen as a side-effect of fsflush() and other mount
1541 	 * and unmount operations that might be going on simultaneously and
1542 	 * may have locked the vfs currently. To not return EBUSY immediately
1543 	 * here we use vfs_lock_wait() instead vfs_lock() for the remount case.
1544 	 */
1545 	if (!remount) {
1546 		if (error = vfs_lock(vfsp)) {
1547 			vfsp->vfs_flag = ovflags;
1548 
1549 			lofi_remove(vfsp);
1550 
1551 			if (splice)
1552 				vn_vfsunlock(vp);
1553 			vfs_free(vfsp);
1554 			goto errout;
1555 		}
1556 	} else {
1557 		vfs_lock_wait(vfsp);
1558 	}
1559 
1560 	/*
1561 	 * Add device to mount in progress table, global mounts require special
1562 	 * handling. It is possible that we have already done the lookupname
1563 	 * on a spliced, non-global fs. If so, we don't want to do it again
1564 	 * since we cannot do a lookupname after taking the
1565 	 * wlock above. This case is for a non-spliced, non-global filesystem.
1566 	 */
1567 	if (!addmip) {
1568 		if ((uap->flags & MS_GLOBAL) == 0 &&
1569 		    lookupname(uap->spec, fromspace, FOLLOW, NULL, &bvp) == 0) {
1570 			addmip = 1;
1571 		}
1572 	}
1573 
1574 	if (addmip) {
1575 		vnode_t *lvp = NULL;
1576 
1577 		error = vfs_get_lofi(vfsp, &lvp);
1578 		if (error > 0) {
1579 			lofi_remove(vfsp);
1580 
1581 			if (splice)
1582 				vn_vfsunlock(vp);
1583 			vfs_unlock(vfsp);
1584 
1585 			if (remount) {
1586 				VFS_RELE(vfsp);
1587 			} else {
1588 				vfs_free(vfsp);
1589 			}
1590 
1591 			goto errout;
1592 		} else if (error == -1) {
1593 			bdev = bvp->v_rdev;
1594 			VN_RELE(bvp);
1595 		} else {
1596 			bdev = lvp->v_rdev;
1597 			VN_RELE(lvp);
1598 			VN_RELE(bvp);
1599 		}
1600 
1601 		vfs_addmip(bdev, vfsp);
1602 		addmip = 0;
1603 		delmip = 1;
1604 	}
1605 	/*
1606 	 * Invalidate cached entry for the mount point.
1607 	 */
1608 	if (splice)
1609 		dnlc_purge_vp(vp);
1610 
1611 	/*
1612 	 * If have an option string but the filesystem doesn't supply a
1613 	 * prototype options table, create a table with the global
1614 	 * options and sufficient room to accept all the options in the
1615 	 * string.  Then parse the passed in option string
1616 	 * accepting all the options in the string.  This gives us an
1617 	 * option table with all the proper cancel properties for the
1618 	 * global options.
1619 	 *
1620 	 * Filesystems that supply a prototype options table are handled
1621 	 * earlier in this function.
1622 	 */
1623 	if (uap->flags & MS_OPTIONSTR) {
1624 		if (!(vswp->vsw_flag & VSW_HASPROTO)) {
1625 			mntopts_t tmp_mntopts;
1626 
1627 			tmp_mntopts.mo_count = 0;
1628 			vfs_createopttbl_extend(&tmp_mntopts, inargs,
1629 			    &mnt_mntopts);
1630 			vfs_parsemntopts(&tmp_mntopts, inargs, 1);
1631 			vfs_swapopttbl_nolock(&mnt_mntopts, &tmp_mntopts);
1632 			vfs_freeopttbl(&tmp_mntopts);
1633 		}
1634 	}
1635 
1636 	/*
1637 	 * Serialize with zone state transitions.
1638 	 * See vfs_list_add; zone mounted into is:
1639 	 * 	zone_find_by_path(refstr_value(vfsp->vfs_mntpt))
1640 	 * not the zone doing the mount (curproc->p_zone), but if we're already
1641 	 * inside a NGZ, then we know what zone we are.
1642 	 */
1643 	if (INGLOBALZONE(curproc)) {
1644 		zone = zone_find_by_path(mountpt);
1645 		ASSERT(zone != NULL);
1646 	} else {
1647 		zone = curproc->p_zone;
1648 		/*
1649 		 * zone_find_by_path does a hold, so do one here too so that
1650 		 * we can do a zone_rele after mount_completed.
1651 		 */
1652 		zone_hold(zone);
1653 	}
1654 	mount_in_progress(zone);
1655 	/*
1656 	 * Instantiate (or reinstantiate) the file system.  If appropriate,
1657 	 * splice it into the file system name space.
1658 	 *
1659 	 * We want VFS_MOUNT() to be able to override the vfs_resource
1660 	 * string if necessary (ie, mntfs), and also for a remount to
1661 	 * change the same (necessary when remounting '/' during boot).
1662 	 * So we set up vfs_mntpt and vfs_resource to what we think they
1663 	 * should be, then hand off control to VFS_MOUNT() which can
1664 	 * override this.
1665 	 *
1666 	 * For safety's sake, when changing vfs_resource or vfs_mntpt of
1667 	 * a vfs which is on the vfs list (i.e. during a remount), we must
1668 	 * never set those fields to NULL. Several bits of code make
1669 	 * assumptions that the fields are always valid.
1670 	 */
1671 	vfs_swapopttbl(&mnt_mntopts, &vfsp->vfs_mntopts);
1672 	if (remount) {
1673 		if ((oldresource = vfsp->vfs_resource) != NULL)
1674 			refstr_hold(oldresource);
1675 		if ((oldmntpt = vfsp->vfs_mntpt) != NULL)
1676 			refstr_hold(oldmntpt);
1677 	}
1678 	vfs_setresource(vfsp, resource, 0);
1679 	vfs_setmntpoint(vfsp, mountpt, 0);
1680 
1681 	/*
1682 	 * going to mount on this vnode, so notify.
1683 	 */
1684 	vnevent_mountedover(vp, NULL);
1685 	error = VFS_MOUNT(vfsp, vp, uap, credp);
1686 
1687 	if (uap->flags & MS_RDONLY)
1688 		vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
1689 	if (uap->flags & MS_NOSUID)
1690 		vfs_setmntopt(vfsp, MNTOPT_NOSUID, NULL, 0);
1691 	if (uap->flags & MS_GLOBAL)
1692 		vfs_setmntopt(vfsp, MNTOPT_GLOBAL, NULL, 0);
1693 
1694 	if (error) {
1695 		lofi_remove(vfsp);
1696 
1697 		if (remount) {
1698 			/* put back pre-remount options */
1699 			vfs_swapopttbl(&mnt_mntopts, &vfsp->vfs_mntopts);
1700 			vfs_setmntpoint(vfsp, refstr_value(oldmntpt),
1701 			    VFSSP_VERBATIM);
1702 			if (oldmntpt)
1703 				refstr_rele(oldmntpt);
1704 			vfs_setresource(vfsp, refstr_value(oldresource),
1705 			    VFSSP_VERBATIM);
1706 			if (oldresource)
1707 				refstr_rele(oldresource);
1708 			vfsp->vfs_flag = ovflags;
1709 			vfs_unlock(vfsp);
1710 			VFS_RELE(vfsp);
1711 		} else {
1712 			vfs_unlock(vfsp);
1713 			vfs_freemnttab(vfsp);
1714 			vfs_free(vfsp);
1715 		}
1716 	} else {
1717 		/*
1718 		 * Set the mount time to now
1719 		 */
1720 		vfsp->vfs_mtime = ddi_get_time();
1721 		if (remount) {
1722 			vfsp->vfs_flag &= ~VFS_REMOUNT;
1723 			if (oldresource)
1724 				refstr_rele(oldresource);
1725 			if (oldmntpt)
1726 				refstr_rele(oldmntpt);
1727 		} else if (splice) {
1728 			/*
1729 			 * Link vfsp into the name space at the mount
1730 			 * point. Vfs_add() is responsible for
1731 			 * holding the mount point which will be
1732 			 * released when vfs_remove() is called.
1733 			 */
1734 			vfs_add(vp, vfsp, uap->flags);
1735 		} else {
1736 			/*
1737 			 * Hold the reference to file system which is
1738 			 * not linked into the name space.
1739 			 */
1740 			vfsp->vfs_zone = NULL;
1741 			VFS_HOLD(vfsp);
1742 			vfsp->vfs_vnodecovered = NULL;
1743 		}
1744 		/*
1745 		 * Set flags for global options encountered
1746 		 */
1747 		if (vfs_optionisset(vfsp, MNTOPT_RO, NULL))
1748 			vfsp->vfs_flag |= VFS_RDONLY;
1749 		else
1750 			vfsp->vfs_flag &= ~VFS_RDONLY;
1751 		if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
1752 			vfsp->vfs_flag |= (VFS_NOSETUID|VFS_NODEVICES);
1753 		} else {
1754 			if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
1755 				vfsp->vfs_flag |= VFS_NODEVICES;
1756 			else
1757 				vfsp->vfs_flag &= ~VFS_NODEVICES;
1758 			if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))
1759 				vfsp->vfs_flag |= VFS_NOSETUID;
1760 			else
1761 				vfsp->vfs_flag &= ~VFS_NOSETUID;
1762 		}
1763 		if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL))
1764 			vfsp->vfs_flag |= VFS_NBMAND;
1765 		else
1766 			vfsp->vfs_flag &= ~VFS_NBMAND;
1767 
1768 		if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL))
1769 			vfsp->vfs_flag |= VFS_XATTR;
1770 		else
1771 			vfsp->vfs_flag &= ~VFS_XATTR;
1772 
1773 		if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL))
1774 			vfsp->vfs_flag |= VFS_NOEXEC;
1775 		else
1776 			vfsp->vfs_flag &= ~VFS_NOEXEC;
1777 
1778 		if (vfs_optionisset(vfsp, MNTOPT_NOFOLLOW, NULL))
1779 			vfsp->vfs_flag |= VFS_NOFOLLOW;
1780 		else
1781 			vfsp->vfs_flag &= ~VFS_NOFOLLOW;
1782 
1783 		/*
1784 		 * Now construct the output option string of options
1785 		 * we recognized.
1786 		 */
1787 		if (uap->flags & MS_OPTIONSTR) {
1788 			vfs_list_read_lock();
1789 			copyout_error = vfs_buildoptionstr(
1790 			    &vfsp->vfs_mntopts, inargs, optlen);
1791 			vfs_list_unlock();
1792 			if (copyout_error == 0 &&
1793 			    (uap->flags & MS_SYSSPACE) == 0) {
1794 				copyout_error = copyoutstr(inargs, opts,
1795 				    optlen, NULL);
1796 			}
1797 		}
1798 
1799 		/*
1800 		 * If this isn't a remount, set up the vopstats before
1801 		 * anyone can touch this. We only allow spliced file
1802 		 * systems (file systems which are in the namespace) to
1803 		 * have the VFS_STATS flag set.
1804 		 * NOTE: PxFS mounts the underlying file system with
1805 		 * MS_NOSPLICE set and copies those vfs_flags to its private
1806 		 * vfs structure. As a result, PxFS should never have
1807 		 * the VFS_STATS flag or else we might access the vfs
1808 		 * statistics-related fields prior to them being
1809 		 * properly initialized.
1810 		 */
1811 		if (!remount && (vswp->vsw_flag & VSW_STATS) && splice) {
1812 			initialize_vopstats(&vfsp->vfs_vopstats);
1813 			/*
1814 			 * We need to set vfs_vskap to NULL because there's
1815 			 * a chance it won't be set below.  This is checked
1816 			 * in teardown_vopstats() so we can't have garbage.
1817 			 */
1818 			vfsp->vfs_vskap = NULL;
1819 			vfsp->vfs_flag |= VFS_STATS;
1820 			vfsp->vfs_fstypevsp = get_fstype_vopstats(vfsp, vswp);
1821 		}
1822 
1823 		if (vswp->vsw_flag & VSW_XID)
1824 			vfsp->vfs_flag |= VFS_XID;
1825 
1826 		vfs_unlock(vfsp);
1827 	}
1828 	mount_completed(zone);
1829 	zone_rele(zone);
1830 	if (splice)
1831 		vn_vfsunlock(vp);
1832 
1833 	if ((error == 0) && (copyout_error == 0)) {
1834 		if (!remount) {
1835 			/*
1836 			 * Don't call get_vskstat_anchor() while holding
1837 			 * locks since it allocates memory and calls
1838 			 * VFS_STATVFS().  For NFS, the latter can generate
1839 			 * an over-the-wire call.
1840 			 */
1841 			vskap = get_vskstat_anchor(vfsp);
1842 			/* Only take the lock if we have something to do */
1843 			if (vskap != NULL) {
1844 				vfs_lock_wait(vfsp);
1845 				if (vfsp->vfs_flag & VFS_STATS) {
1846 					vfsp->vfs_vskap = vskap;
1847 				}
1848 				vfs_unlock(vfsp);
1849 			}
1850 		}
1851 		/* Return vfsp to caller. */
1852 		*vfspp = vfsp;
1853 	}
1854 errout:
1855 	vfs_freeopttbl(&mnt_mntopts);
1856 	if (resource != NULL)
1857 		kmem_free(resource, strlen(resource) + 1);
1858 	if (mountpt != NULL)
1859 		kmem_free(mountpt, strlen(mountpt) + 1);
1860 	/*
1861 	 * It is possible we errored prior to adding to mount in progress
1862 	 * table. Must free vnode we acquired with successful lookupname.
1863 	 */
1864 	if (addmip)
1865 		VN_RELE(bvp);
1866 	if (delmip)
1867 		vfs_delmip(vfsp);
1868 	ASSERT(vswp != NULL);
1869 	vfs_unrefvfssw(vswp);
1870 	if (inargs != opts)
1871 		kmem_free(inargs, MAX_MNTOPT_STR);
1872 	if (copyout_error) {
1873 		lofi_remove(vfsp);
1874 		VFS_RELE(vfsp);
1875 		error = copyout_error;
1876 	}
1877 	return (error);
1878 }
1879 
1880 static void
1881 vfs_setpath(
1882     struct vfs *vfsp,		/* vfs being updated */
1883     refstr_t **refp,		/* Ref-count string to contain the new path */
1884     const char *newpath,	/* Path to add to refp (above) */
1885     uint32_t flag)		/* flag */
1886 {
1887 	size_t len;
1888 	refstr_t *ref;
1889 	zone_t *zone = curproc->p_zone;
1890 	char *sp;
1891 	int have_list_lock = 0;
1892 
1893 	ASSERT(!VFS_ON_LIST(vfsp) || vfs_lock_held(vfsp));
1894 
1895 	/*
1896 	 * New path must be less than MAXPATHLEN because mntfs
1897 	 * will only display up to MAXPATHLEN bytes. This is currently
1898 	 * safe, because domount() uses pn_get(), and other callers
1899 	 * similarly cap the size to fewer than MAXPATHLEN bytes.
1900 	 */
1901 
1902 	ASSERT(strlen(newpath) < MAXPATHLEN);
1903 
1904 	/* mntfs requires consistency while vfs list lock is held */
1905 
1906 	if (VFS_ON_LIST(vfsp)) {
1907 		have_list_lock = 1;
1908 		vfs_list_lock();
1909 	}
1910 
1911 	if (*refp != NULL)
1912 		refstr_rele(*refp);
1913 
1914 	/*
1915 	 * If we are in a non-global zone then we prefix the supplied path,
1916 	 * newpath, with the zone's root path, with two exceptions. The first
1917 	 * is where we have been explicitly directed to avoid doing so; this
1918 	 * will be the case following a failed remount, where the path supplied
1919 	 * will be a saved version which must now be restored. The second
1920 	 * exception is where newpath is not a pathname but a descriptive name,
1921 	 * e.g. "procfs".
1922 	 */
1923 	if (zone == global_zone || (flag & VFSSP_VERBATIM) || *newpath != '/') {
1924 		ref = refstr_alloc(newpath);
1925 		goto out;
1926 	}
1927 
1928 	/*
1929 	 * Truncate the trailing '/' in the zoneroot, and merge
1930 	 * in the zone's rootpath with the "newpath" (resource
1931 	 * or mountpoint) passed in.
1932 	 *
1933 	 * The size of the required buffer is thus the size of
1934 	 * the buffer required for the passed-in newpath
1935 	 * (strlen(newpath) + 1), plus the size of the buffer
1936 	 * required to hold zone_rootpath (zone_rootpathlen)
1937 	 * minus one for one of the now-superfluous NUL
1938 	 * terminations, minus one for the trailing '/'.
1939 	 *
1940 	 * That gives us:
1941 	 *
1942 	 * (strlen(newpath) + 1) + zone_rootpathlen - 1 - 1
1943 	 *
1944 	 * Which is what we have below.
1945 	 */
1946 
1947 	len = strlen(newpath) + zone->zone_rootpathlen - 1;
1948 	sp = kmem_alloc(len, KM_SLEEP);
1949 
1950 	/*
1951 	 * Copy everything including the trailing slash, which
1952 	 * we then overwrite with the NUL character.
1953 	 */
1954 
1955 	(void) strcpy(sp, zone->zone_rootpath);
1956 	sp[zone->zone_rootpathlen - 2] = '\0';
1957 	(void) strcat(sp, newpath);
1958 
1959 	ref = refstr_alloc(sp);
1960 	kmem_free(sp, len);
1961 out:
1962 	*refp = ref;
1963 
1964 	if (have_list_lock) {
1965 		vfs_mnttab_modtimeupd();
1966 		vfs_list_unlock();
1967 	}
1968 }
1969 
1970 /*
1971  * Record a mounted resource name in a vfs structure.
1972  * If vfsp is already mounted, caller must hold the vfs lock.
1973  */
1974 void
1975 vfs_setresource(struct vfs *vfsp, const char *resource, uint32_t flag)
1976 {
1977 	if (resource == NULL || resource[0] == '\0')
1978 		resource = VFS_NORESOURCE;
1979 	vfs_setpath(vfsp, &vfsp->vfs_resource, resource, flag);
1980 }
1981 
1982 /*
1983  * Record a mount point name in a vfs structure.
1984  * If vfsp is already mounted, caller must hold the vfs lock.
1985  */
1986 void
1987 vfs_setmntpoint(struct vfs *vfsp, const char *mntpt, uint32_t flag)
1988 {
1989 	if (mntpt == NULL || mntpt[0] == '\0')
1990 		mntpt = VFS_NOMNTPT;
1991 	vfs_setpath(vfsp, &vfsp->vfs_mntpt, mntpt, flag);
1992 }
1993 
1994 /* Returns the vfs_resource. Caller must call refstr_rele() when finished. */
1995 
1996 refstr_t *
1997 vfs_getresource(const struct vfs *vfsp)
1998 {
1999 	refstr_t *resource;
2000 
2001 	vfs_list_read_lock();
2002 	resource = vfsp->vfs_resource;
2003 	refstr_hold(resource);
2004 	vfs_list_unlock();
2005 
2006 	return (resource);
2007 }
2008 
2009 /* Returns the vfs_mntpt. Caller must call refstr_rele() when finished. */
2010 
2011 refstr_t *
2012 vfs_getmntpoint(const struct vfs *vfsp)
2013 {
2014 	refstr_t *mntpt;
2015 
2016 	vfs_list_read_lock();
2017 	mntpt = vfsp->vfs_mntpt;
2018 	refstr_hold(mntpt);
2019 	vfs_list_unlock();
2020 
2021 	return (mntpt);
2022 }
2023 
2024 /*
2025  * Create an empty options table with enough empty slots to hold all
2026  * The options in the options string passed as an argument.
2027  * Potentially prepend another options table.
2028  *
2029  * Note: caller is responsible for locking the vfs list, if needed,
2030  *       to protect mops.
2031  */
2032 static void
2033 vfs_createopttbl_extend(mntopts_t *mops, const char *opts,
2034     const mntopts_t *mtmpl)
2035 {
2036 	const char *s = opts;
2037 	uint_t count;
2038 
2039 	if (opts == NULL || *opts == '\0') {
2040 		count = 0;
2041 	} else {
2042 		count = 1;
2043 
2044 		/*
2045 		 * Count number of options in the string
2046 		 */
2047 		for (s = strchr(s, ','); s != NULL; s = strchr(s, ',')) {
2048 			count++;
2049 			s++;
2050 		}
2051 	}
2052 	vfs_copyopttbl_extend(mtmpl, mops, count);
2053 }
2054 
2055 /*
2056  * Create an empty options table with enough empty slots to hold all
2057  * The options in the options string passed as an argument.
2058  *
2059  * This function is *not* for general use by filesystems.
2060  *
2061  * Note: caller is responsible for locking the vfs list, if needed,
2062  *       to protect mops.
2063  */
2064 void
2065 vfs_createopttbl(mntopts_t *mops, const char *opts)
2066 {
2067 	vfs_createopttbl_extend(mops, opts, NULL);
2068 }
2069 
2070 
2071 /*
2072  * Swap two mount options tables
2073  */
2074 static void
2075 vfs_swapopttbl_nolock(mntopts_t *optbl1, mntopts_t *optbl2)
2076 {
2077 	uint_t tmpcnt;
2078 	mntopt_t *tmplist;
2079 
2080 	tmpcnt = optbl2->mo_count;
2081 	tmplist = optbl2->mo_list;
2082 	optbl2->mo_count = optbl1->mo_count;
2083 	optbl2->mo_list = optbl1->mo_list;
2084 	optbl1->mo_count = tmpcnt;
2085 	optbl1->mo_list = tmplist;
2086 }
2087 
2088 static void
2089 vfs_swapopttbl(mntopts_t *optbl1, mntopts_t *optbl2)
2090 {
2091 	vfs_list_lock();
2092 	vfs_swapopttbl_nolock(optbl1, optbl2);
2093 	vfs_mnttab_modtimeupd();
2094 	vfs_list_unlock();
2095 }
2096 
2097 static char **
2098 vfs_copycancelopt_extend(char **const moc, int extend)
2099 {
2100 	int i = 0;
2101 	int j;
2102 	char **result;
2103 
2104 	if (moc != NULL) {
2105 		for (; moc[i] != NULL; i++)
2106 			/* count number of options to cancel */;
2107 	}
2108 
2109 	if (i + extend == 0)
2110 		return (NULL);
2111 
2112 	result = kmem_alloc((i + extend + 1) * sizeof (char *), KM_SLEEP);
2113 
2114 	for (j = 0; j < i; j++) {
2115 		result[j] = kmem_alloc(strlen(moc[j]) + 1, KM_SLEEP);
2116 		(void) strcpy(result[j], moc[j]);
2117 	}
2118 	for (; j <= i + extend; j++)
2119 		result[j] = NULL;
2120 
2121 	return (result);
2122 }
2123 
2124 static void
2125 vfs_copyopt(const mntopt_t *s, mntopt_t *d)
2126 {
2127 	char *sp, *dp;
2128 
2129 	d->mo_flags = s->mo_flags;
2130 	d->mo_data = s->mo_data;
2131 	sp = s->mo_name;
2132 	if (sp != NULL) {
2133 		dp = kmem_alloc(strlen(sp) + 1, KM_SLEEP);
2134 		(void) strcpy(dp, sp);
2135 		d->mo_name = dp;
2136 	} else {
2137 		d->mo_name = NULL; /* should never happen */
2138 	}
2139 
2140 	d->mo_cancel = vfs_copycancelopt_extend(s->mo_cancel, 0);
2141 
2142 	sp = s->mo_arg;
2143 	if (sp != NULL) {
2144 		dp = kmem_alloc(strlen(sp) + 1, KM_SLEEP);
2145 		(void) strcpy(dp, sp);
2146 		d->mo_arg = dp;
2147 	} else {
2148 		d->mo_arg = NULL;
2149 	}
2150 }
2151 
2152 /*
2153  * Copy a mount options table, possibly allocating some spare
2154  * slots at the end.  It is permissible to copy_extend the NULL table.
2155  */
2156 static void
2157 vfs_copyopttbl_extend(const mntopts_t *smo, mntopts_t *dmo, int extra)
2158 {
2159 	uint_t i, count;
2160 	mntopt_t *motbl;
2161 
2162 	/*
2163 	 * Clear out any existing stuff in the options table being initialized
2164 	 */
2165 	vfs_freeopttbl(dmo);
2166 	count = (smo == NULL) ? 0 : smo->mo_count;
2167 	if ((count + extra) == 0)	/* nothing to do */
2168 		return;
2169 	dmo->mo_count = count + extra;
2170 	motbl = kmem_zalloc((count + extra) * sizeof (mntopt_t), KM_SLEEP);
2171 	dmo->mo_list = motbl;
2172 	for (i = 0; i < count; i++) {
2173 		vfs_copyopt(&smo->mo_list[i], &motbl[i]);
2174 	}
2175 	for (i = count; i < count + extra; i++) {
2176 		motbl[i].mo_flags = MO_EMPTY;
2177 	}
2178 }
2179 
2180 /*
2181  * Copy a mount options table.
2182  *
2183  * This function is *not* for general use by filesystems.
2184  *
2185  * Note: caller is responsible for locking the vfs list, if needed,
2186  *       to protect smo and dmo.
2187  */
2188 void
2189 vfs_copyopttbl(const mntopts_t *smo, mntopts_t *dmo)
2190 {
2191 	vfs_copyopttbl_extend(smo, dmo, 0);
2192 }
2193 
2194 static char **
2195 vfs_mergecancelopts(const mntopt_t *mop1, const mntopt_t *mop2)
2196 {
2197 	int c1 = 0;
2198 	int c2 = 0;
2199 	char **result;
2200 	char **sp1, **sp2, **dp;
2201 
2202 	/*
2203 	 * First we count both lists of cancel options.
2204 	 * If either is NULL or has no elements, we return a copy of
2205 	 * the other.
2206 	 */
2207 	if (mop1->mo_cancel != NULL) {
2208 		for (; mop1->mo_cancel[c1] != NULL; c1++)
2209 			/* count cancel options in mop1 */;
2210 	}
2211 
2212 	if (c1 == 0)
2213 		return (vfs_copycancelopt_extend(mop2->mo_cancel, 0));
2214 
2215 	if (mop2->mo_cancel != NULL) {
2216 		for (; mop2->mo_cancel[c2] != NULL; c2++)
2217 			/* count cancel options in mop2 */;
2218 	}
2219 
2220 	result = vfs_copycancelopt_extend(mop1->mo_cancel, c2);
2221 
2222 	if (c2 == 0)
2223 		return (result);
2224 
2225 	/*
2226 	 * When we get here, we've got two sets of cancel options;
2227 	 * we need to merge the two sets.  We know that the result
2228 	 * array has "c1+c2+1" entries and in the end we might shrink
2229 	 * it.
2230 	 * Result now has a copy of the c1 entries from mop1; we'll
2231 	 * now lookup all the entries of mop2 in mop1 and copy it if
2232 	 * it is unique.
2233 	 * This operation is O(n^2) but it's only called once per
2234 	 * filesystem per duplicate option.  This is a situation
2235 	 * which doesn't arise with the filesystems in ON and
2236 	 * n is generally 1.
2237 	 */
2238 
2239 	dp = &result[c1];
2240 	for (sp2 = mop2->mo_cancel; *sp2 != NULL; sp2++) {
2241 		for (sp1 = mop1->mo_cancel; *sp1 != NULL; sp1++) {
2242 			if (strcmp(*sp1, *sp2) == 0)
2243 				break;
2244 		}
2245 		if (*sp1 == NULL) {
2246 			/*
2247 			 * Option *sp2 not found in mop1, so copy it.
2248 			 * The calls to vfs_copycancelopt_extend()
2249 			 * guarantee that there's enough room.
2250 			 */
2251 			*dp = kmem_alloc(strlen(*sp2) + 1, KM_SLEEP);
2252 			(void) strcpy(*dp++, *sp2);
2253 		}
2254 	}
2255 	if (dp != &result[c1+c2]) {
2256 		size_t bytes = (dp - result + 1) * sizeof (char *);
2257 		char **nres = kmem_alloc(bytes, KM_SLEEP);
2258 
2259 		bcopy(result, nres, bytes);
2260 		kmem_free(result, (c1 + c2 + 1) * sizeof (char *));
2261 		result = nres;
2262 	}
2263 	return (result);
2264 }
2265 
2266 /*
2267  * Merge two mount option tables (outer and inner) into one.  This is very
2268  * similar to "merging" global variables and automatic variables in C.
2269  *
2270  * This isn't (and doesn't have to be) fast.
2271  *
2272  * This function is *not* for general use by filesystems.
2273  *
2274  * Note: caller is responsible for locking the vfs list, if needed,
2275  *       to protect omo, imo & dmo.
2276  */
2277 void
2278 vfs_mergeopttbl(const mntopts_t *omo, const mntopts_t *imo, mntopts_t *dmo)
2279 {
2280 	uint_t i, count;
2281 	mntopt_t *mop, *motbl;
2282 	uint_t freeidx;
2283 
2284 	/*
2285 	 * First determine how much space we need to allocate.
2286 	 */
2287 	count = omo->mo_count;
2288 	for (i = 0; i < imo->mo_count; i++) {
2289 		if (imo->mo_list[i].mo_flags & MO_EMPTY)
2290 			continue;
2291 		if (vfs_hasopt(omo, imo->mo_list[i].mo_name) == NULL)
2292 			count++;
2293 	}
2294 	ASSERT(count >= omo->mo_count &&
2295 	    count <= omo->mo_count + imo->mo_count);
2296 	motbl = kmem_alloc(count * sizeof (mntopt_t), KM_SLEEP);
2297 	for (i = 0; i < omo->mo_count; i++)
2298 		vfs_copyopt(&omo->mo_list[i], &motbl[i]);
2299 	freeidx = omo->mo_count;
2300 	for (i = 0; i < imo->mo_count; i++) {
2301 		if (imo->mo_list[i].mo_flags & MO_EMPTY)
2302 			continue;
2303 		if ((mop = vfs_hasopt(omo, imo->mo_list[i].mo_name)) != NULL) {
2304 			char **newcanp;
2305 			uint_t index = mop - omo->mo_list;
2306 
2307 			newcanp = vfs_mergecancelopts(mop, &motbl[index]);
2308 
2309 			vfs_freeopt(&motbl[index]);
2310 			vfs_copyopt(&imo->mo_list[i], &motbl[index]);
2311 
2312 			vfs_freecancelopt(motbl[index].mo_cancel);
2313 			motbl[index].mo_cancel = newcanp;
2314 		} else {
2315 			/*
2316 			 * If it's a new option, just copy it over to the first
2317 			 * free location.
2318 			 */
2319 			vfs_copyopt(&imo->mo_list[i], &motbl[freeidx++]);
2320 		}
2321 	}
2322 	dmo->mo_count = count;
2323 	dmo->mo_list = motbl;
2324 }
2325 
2326 /*
2327  * Functions to set and clear mount options in a mount options table.
2328  */
2329 
2330 /*
2331  * Clear a mount option, if it exists.
2332  *
2333  * The update_mnttab arg indicates whether mops is part of a vfs that is on
2334  * the vfs list.
2335  */
2336 static void
2337 vfs_clearmntopt_nolock(mntopts_t *mops, const char *opt, int update_mnttab)
2338 {
2339 	struct mntopt *mop;
2340 	uint_t i, count;
2341 
2342 	ASSERT(!update_mnttab || RW_WRITE_HELD(&vfslist));
2343 
2344 	count = mops->mo_count;
2345 	for (i = 0; i < count; i++) {
2346 		mop = &mops->mo_list[i];
2347 
2348 		if (mop->mo_flags & MO_EMPTY)
2349 			continue;
2350 		if (strcmp(opt, mop->mo_name))
2351 			continue;
2352 		mop->mo_flags &= ~MO_SET;
2353 		if (mop->mo_arg != NULL) {
2354 			kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
2355 		}
2356 		mop->mo_arg = NULL;
2357 		if (update_mnttab)
2358 			vfs_mnttab_modtimeupd();
2359 		break;
2360 	}
2361 }
2362 
2363 void
2364 vfs_clearmntopt(struct vfs *vfsp, const char *opt)
2365 {
2366 	int gotlock = 0;
2367 
2368 	if (VFS_ON_LIST(vfsp)) {
2369 		gotlock = 1;
2370 		vfs_list_lock();
2371 	}
2372 	vfs_clearmntopt_nolock(&vfsp->vfs_mntopts, opt, gotlock);
2373 	if (gotlock)
2374 		vfs_list_unlock();
2375 }
2376 
2377 
2378 /*
2379  * Set a mount option on.  If it's not found in the table, it's silently
2380  * ignored.  If the option has MO_IGNORE set, it is still set unless the
2381  * VFS_NOFORCEOPT bit is set in the flags.  Also, VFS_DISPLAY/VFS_NODISPLAY flag
2382  * bits can be used to toggle the MO_NODISPLAY bit for the option.
2383  * If the VFS_CREATEOPT flag bit is set then the first option slot with
2384  * MO_EMPTY set is created as the option passed in.
2385  *
2386  * The update_mnttab arg indicates whether mops is part of a vfs that is on
2387  * the vfs list.
2388  */
2389 static void
2390 vfs_setmntopt_nolock(mntopts_t *mops, const char *opt,
2391     const char *arg, int flags, int update_mnttab)
2392 {
2393 	mntopt_t *mop;
2394 	uint_t i, count;
2395 	char *sp;
2396 
2397 	ASSERT(!update_mnttab || RW_WRITE_HELD(&vfslist));
2398 
2399 	if (flags & VFS_CREATEOPT) {
2400 		if (vfs_hasopt(mops, opt) != NULL) {
2401 			flags &= ~VFS_CREATEOPT;
2402 		}
2403 	}
2404 	count = mops->mo_count;
2405 	for (i = 0; i < count; i++) {
2406 		mop = &mops->mo_list[i];
2407 
2408 		if (mop->mo_flags & MO_EMPTY) {
2409 			if ((flags & VFS_CREATEOPT) == 0)
2410 				continue;
2411 			sp = kmem_alloc(strlen(opt) + 1, KM_SLEEP);
2412 			(void) strcpy(sp, opt);
2413 			mop->mo_name = sp;
2414 			if (arg != NULL)
2415 				mop->mo_flags = MO_HASVALUE;
2416 			else
2417 				mop->mo_flags = 0;
2418 		} else if (strcmp(opt, mop->mo_name)) {
2419 			continue;
2420 		}
2421 		if ((mop->mo_flags & MO_IGNORE) && (flags & VFS_NOFORCEOPT))
2422 			break;
2423 		if (arg != NULL && (mop->mo_flags & MO_HASVALUE) != 0) {
2424 			sp = kmem_alloc(strlen(arg) + 1, KM_SLEEP);
2425 			(void) strcpy(sp, arg);
2426 		} else {
2427 			sp = NULL;
2428 		}
2429 		if (mop->mo_arg != NULL)
2430 			kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
2431 		mop->mo_arg = sp;
2432 		if (flags & VFS_DISPLAY)
2433 			mop->mo_flags &= ~MO_NODISPLAY;
2434 		if (flags & VFS_NODISPLAY)
2435 			mop->mo_flags |= MO_NODISPLAY;
2436 		mop->mo_flags |= MO_SET;
2437 		if (mop->mo_cancel != NULL) {
2438 			char **cp;
2439 
2440 			for (cp = mop->mo_cancel; *cp != NULL; cp++)
2441 				vfs_clearmntopt_nolock(mops, *cp, 0);
2442 		}
2443 		if (update_mnttab)
2444 			vfs_mnttab_modtimeupd();
2445 		break;
2446 	}
2447 }
2448 
2449 void
2450 vfs_setmntopt(struct vfs *vfsp, const char *opt, const char *arg, int flags)
2451 {
2452 	int gotlock = 0;
2453 
2454 	if (VFS_ON_LIST(vfsp)) {
2455 		gotlock = 1;
2456 		vfs_list_lock();
2457 	}
2458 	vfs_setmntopt_nolock(&vfsp->vfs_mntopts, opt, arg, flags, gotlock);
2459 	if (gotlock)
2460 		vfs_list_unlock();
2461 }
2462 
2463 
2464 /*
2465  * Add a "tag" option to a mounted file system's options list.
2466  *
2467  * Note: caller is responsible for locking the vfs list, if needed,
2468  *       to protect mops.
2469  */
2470 static mntopt_t *
2471 vfs_addtag(mntopts_t *mops, const char *tag)
2472 {
2473 	uint_t count;
2474 	mntopt_t *mop, *motbl;
2475 
2476 	count = mops->mo_count + 1;
2477 	motbl = kmem_zalloc(count * sizeof (mntopt_t), KM_SLEEP);
2478 	if (mops->mo_count) {
2479 		size_t len = (count - 1) * sizeof (mntopt_t);
2480 
2481 		bcopy(mops->mo_list, motbl, len);
2482 		kmem_free(mops->mo_list, len);
2483 	}
2484 	mops->mo_count = count;
2485 	mops->mo_list = motbl;
2486 	mop = &motbl[count - 1];
2487 	mop->mo_flags = MO_TAG;
2488 	mop->mo_name = kmem_alloc(strlen(tag) + 1, KM_SLEEP);
2489 	(void) strcpy(mop->mo_name, tag);
2490 	return (mop);
2491 }
2492 
2493 /*
2494  * Allow users to set arbitrary "tags" in a vfs's mount options.
2495  * Broader use within the kernel is discouraged.
2496  */
2497 int
2498 vfs_settag(uint_t major, uint_t minor, const char *mntpt, const char *tag,
2499     cred_t *cr)
2500 {
2501 	vfs_t *vfsp;
2502 	mntopts_t *mops;
2503 	mntopt_t *mop;
2504 	int found = 0;
2505 	dev_t dev = makedevice(major, minor);
2506 	int err = 0;
2507 	char *buf = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
2508 
2509 	/*
2510 	 * Find the desired mounted file system
2511 	 */
2512 	vfs_list_lock();
2513 	vfsp = rootvfs;
2514 	do {
2515 		if (vfsp->vfs_dev == dev &&
2516 		    strcmp(mntpt, refstr_value(vfsp->vfs_mntpt)) == 0) {
2517 			found = 1;
2518 			break;
2519 		}
2520 		vfsp = vfsp->vfs_next;
2521 	} while (vfsp != rootvfs);
2522 
2523 	if (!found) {
2524 		err = EINVAL;
2525 		goto out;
2526 	}
2527 	err = secpolicy_fs_config(cr, vfsp);
2528 	if (err != 0)
2529 		goto out;
2530 
2531 	mops = &vfsp->vfs_mntopts;
2532 	/*
2533 	 * Add tag if it doesn't already exist
2534 	 */
2535 	if ((mop = vfs_hasopt(mops, tag)) == NULL) {
2536 		int len;
2537 
2538 		(void) vfs_buildoptionstr(mops, buf, MAX_MNTOPT_STR);
2539 		len = strlen(buf);
2540 		if (len + strlen(tag) + 2 > MAX_MNTOPT_STR) {
2541 			err = ENAMETOOLONG;
2542 			goto out;
2543 		}
2544 		mop = vfs_addtag(mops, tag);
2545 	}
2546 	if ((mop->mo_flags & MO_TAG) == 0) {
2547 		err = EINVAL;
2548 		goto out;
2549 	}
2550 	vfs_setmntopt_nolock(mops, tag, NULL, 0, 1);
2551 out:
2552 	vfs_list_unlock();
2553 	kmem_free(buf, MAX_MNTOPT_STR);
2554 	return (err);
2555 }
2556 
2557 /*
2558  * Allow users to remove arbitrary "tags" in a vfs's mount options.
2559  * Broader use within the kernel is discouraged.
2560  */
2561 int
2562 vfs_clrtag(uint_t major, uint_t minor, const char *mntpt, const char *tag,
2563     cred_t *cr)
2564 {
2565 	vfs_t *vfsp;
2566 	mntopt_t *mop;
2567 	int found = 0;
2568 	dev_t dev = makedevice(major, minor);
2569 	int err = 0;
2570 
2571 	/*
2572 	 * Find the desired mounted file system
2573 	 */
2574 	vfs_list_lock();
2575 	vfsp = rootvfs;
2576 	do {
2577 		if (vfsp->vfs_dev == dev &&
2578 		    strcmp(mntpt, refstr_value(vfsp->vfs_mntpt)) == 0) {
2579 			found = 1;
2580 			break;
2581 		}
2582 		vfsp = vfsp->vfs_next;
2583 	} while (vfsp != rootvfs);
2584 
2585 	if (!found) {
2586 		err = EINVAL;
2587 		goto out;
2588 	}
2589 	err = secpolicy_fs_config(cr, vfsp);
2590 	if (err != 0)
2591 		goto out;
2592 
2593 	if ((mop = vfs_hasopt(&vfsp->vfs_mntopts, tag)) == NULL) {
2594 		err = EINVAL;
2595 		goto out;
2596 	}
2597 	if ((mop->mo_flags & MO_TAG) == 0) {
2598 		err = EINVAL;
2599 		goto out;
2600 	}
2601 	vfs_clearmntopt_nolock(&vfsp->vfs_mntopts, tag, 1);
2602 out:
2603 	vfs_list_unlock();
2604 	return (err);
2605 }
2606 
2607 /*
2608  * Function to parse an option string and fill in a mount options table.
2609  * Unknown options are silently ignored.  The input option string is modified
2610  * by replacing separators with nulls.  If the create flag is set, options
2611  * not found in the table are just added on the fly.  The table must have
2612  * an option slot marked MO_EMPTY to add an option on the fly.
2613  *
2614  * This function is *not* for general use by filesystems.
2615  *
2616  * Note: caller is responsible for locking the vfs list, if needed,
2617  *       to protect mops..
2618  */
2619 void
2620 vfs_parsemntopts(mntopts_t *mops, char *osp, int create)
2621 {
2622 	char *s = osp, *p, *nextop, *valp, *cp, *ep;
2623 	int setflg = VFS_NOFORCEOPT;
2624 
2625 	if (osp == NULL)
2626 		return;
2627 	while (*s != '\0') {
2628 		p = strchr(s, ',');	/* find next option */
2629 		if (p == NULL) {
2630 			cp = NULL;
2631 			p = s + strlen(s);
2632 		} else {
2633 			cp = p;		/* save location of comma */
2634 			*p++ = '\0';	/* mark end and point to next option */
2635 		}
2636 		nextop = p;
2637 		p = strchr(s, '=');	/* look for value */
2638 		if (p == NULL) {
2639 			valp = NULL;	/* no value supplied */
2640 		} else {
2641 			ep = p;		/* save location of equals */
2642 			*p++ = '\0';	/* end option and point to value */
2643 			valp = p;
2644 		}
2645 		/*
2646 		 * set option into options table
2647 		 */
2648 		if (create)
2649 			setflg |= VFS_CREATEOPT;
2650 		vfs_setmntopt_nolock(mops, s, valp, setflg, 0);
2651 		if (cp != NULL)
2652 			*cp = ',';	/* restore the comma */
2653 		if (valp != NULL)
2654 			*ep = '=';	/* restore the equals */
2655 		s = nextop;
2656 	}
2657 }
2658 
2659 /*
2660  * Function to inquire if an option exists in a mount options table.
2661  * Returns a pointer to the option if it exists, else NULL.
2662  *
2663  * This function is *not* for general use by filesystems.
2664  *
2665  * Note: caller is responsible for locking the vfs list, if needed,
2666  *       to protect mops.
2667  */
2668 struct mntopt *
2669 vfs_hasopt(const mntopts_t *mops, const char *opt)
2670 {
2671 	struct mntopt *mop;
2672 	uint_t i, count;
2673 
2674 	count = mops->mo_count;
2675 	for (i = 0; i < count; i++) {
2676 		mop = &mops->mo_list[i];
2677 
2678 		if (mop->mo_flags & MO_EMPTY)
2679 			continue;
2680 		if (strcmp(opt, mop->mo_name) == 0)
2681 			return (mop);
2682 	}
2683 	return (NULL);
2684 }
2685 
2686 /*
2687  * Function to inquire if an option is set in a mount options table.
2688  * Returns non-zero if set and fills in the arg pointer with a pointer to
2689  * the argument string or NULL if there is no argument string.
2690  */
2691 static int
2692 vfs_optionisset_nolock(const mntopts_t *mops, const char *opt, char **argp)
2693 {
2694 	struct mntopt *mop;
2695 	uint_t i, count;
2696 
2697 	count = mops->mo_count;
2698 	for (i = 0; i < count; i++) {
2699 		mop = &mops->mo_list[i];
2700 
2701 		if (mop->mo_flags & MO_EMPTY)
2702 			continue;
2703 		if (strcmp(opt, mop->mo_name))
2704 			continue;
2705 		if ((mop->mo_flags & MO_SET) == 0)
2706 			return (0);
2707 		if (argp != NULL && (mop->mo_flags & MO_HASVALUE) != 0)
2708 			*argp = mop->mo_arg;
2709 		return (1);
2710 	}
2711 	return (0);
2712 }
2713 
2714 
2715 int
2716 vfs_optionisset(const struct vfs *vfsp, const char *opt, char **argp)
2717 {
2718 	int ret;
2719 
2720 	vfs_list_read_lock();
2721 	ret = vfs_optionisset_nolock(&vfsp->vfs_mntopts, opt, argp);
2722 	vfs_list_unlock();
2723 	return (ret);
2724 }
2725 
2726 
2727 /*
2728  * Construct a comma separated string of the options set in the given
2729  * mount table, return the string in the given buffer.  Return non-zero if
2730  * the buffer would overflow.
2731  *
2732  * This function is *not* for general use by filesystems.
2733  *
2734  * Note: caller is responsible for locking the vfs list, if needed,
2735  *       to protect mp.
2736  */
2737 int
2738 vfs_buildoptionstr(const mntopts_t *mp, char *buf, int len)
2739 {
2740 	char *cp;
2741 	uint_t i;
2742 
2743 	buf[0] = '\0';
2744 	cp = buf;
2745 	for (i = 0; i < mp->mo_count; i++) {
2746 		struct mntopt *mop;
2747 
2748 		mop = &mp->mo_list[i];
2749 		if (mop->mo_flags & MO_SET) {
2750 			int optlen, comma = 0;
2751 
2752 			if (buf[0] != '\0')
2753 				comma = 1;
2754 			optlen = strlen(mop->mo_name);
2755 			if (strlen(buf) + comma + optlen + 1 > len)
2756 				goto err;
2757 			if (comma)
2758 				*cp++ = ',';
2759 			(void) strcpy(cp, mop->mo_name);
2760 			cp += optlen;
2761 			/*
2762 			 * Append option value if there is one
2763 			 */
2764 			if (mop->mo_arg != NULL) {
2765 				int arglen;
2766 
2767 				arglen = strlen(mop->mo_arg);
2768 				if (strlen(buf) + arglen + 2 > len)
2769 					goto err;
2770 				*cp++ = '=';
2771 				(void) strcpy(cp, mop->mo_arg);
2772 				cp += arglen;
2773 			}
2774 		}
2775 	}
2776 	return (0);
2777 err:
2778 	return (EOVERFLOW);
2779 }
2780 
2781 static void
2782 vfs_freecancelopt(char **moc)
2783 {
2784 	if (moc != NULL) {
2785 		int ccnt = 0;
2786 		char **cp;
2787 
2788 		for (cp = moc; *cp != NULL; cp++) {
2789 			kmem_free(*cp, strlen(*cp) + 1);
2790 			ccnt++;
2791 		}
2792 		kmem_free(moc, (ccnt + 1) * sizeof (char *));
2793 	}
2794 }
2795 
2796 static void
2797 vfs_freeopt(mntopt_t *mop)
2798 {
2799 	if (mop->mo_name != NULL)
2800 		kmem_free(mop->mo_name, strlen(mop->mo_name) + 1);
2801 
2802 	vfs_freecancelopt(mop->mo_cancel);
2803 
2804 	if (mop->mo_arg != NULL)
2805 		kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
2806 }
2807 
2808 /*
2809  * Free a mount options table
2810  *
2811  * This function is *not* for general use by filesystems.
2812  *
2813  * Note: caller is responsible for locking the vfs list, if needed,
2814  *       to protect mp.
2815  */
2816 void
2817 vfs_freeopttbl(mntopts_t *mp)
2818 {
2819 	uint_t i, count;
2820 
2821 	count = mp->mo_count;
2822 	for (i = 0; i < count; i++) {
2823 		vfs_freeopt(&mp->mo_list[i]);
2824 	}
2825 	if (count) {
2826 		kmem_free(mp->mo_list, sizeof (mntopt_t) * count);
2827 		mp->mo_count = 0;
2828 		mp->mo_list = NULL;
2829 	}
2830 }
2831 
2832 
2833 /* ARGSUSED */
2834 static int
2835 vfs_mntdummyread(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cred,
2836     caller_context_t *ct)
2837 {
2838 	return (0);
2839 }
2840 
2841 /* ARGSUSED */
2842 static int
2843 vfs_mntdummywrite(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cred,
2844     caller_context_t *ct)
2845 {
2846 	return (0);
2847 }
2848 
2849 /*
2850  * The dummy vnode is currently used only by file events notification
2851  * module which is just interested in the timestamps.
2852  */
2853 /* ARGSUSED */
2854 static int
2855 vfs_mntdummygetattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2856     caller_context_t *ct)
2857 {
2858 	bzero(vap, sizeof (vattr_t));
2859 	vap->va_type = VREG;
2860 	vap->va_nlink = 1;
2861 	vap->va_ctime = vfs_mnttab_ctime;
2862 	/*
2863 	 * it is ok to just copy mtime as the time will be monotonically
2864 	 * increasing.
2865 	 */
2866 	vap->va_mtime = vfs_mnttab_mtime;
2867 	vap->va_atime = vap->va_mtime;
2868 	return (0);
2869 }
2870 
2871 static void
2872 vfs_mnttabvp_setup(void)
2873 {
2874 	vnode_t *tvp;
2875 	vnodeops_t *vfs_mntdummyvnops;
2876 	const fs_operation_def_t mnt_dummyvnodeops_template[] = {
2877 		VOPNAME_READ, 		{ .vop_read = vfs_mntdummyread },
2878 		VOPNAME_WRITE, 		{ .vop_write = vfs_mntdummywrite },
2879 		VOPNAME_GETATTR,	{ .vop_getattr = vfs_mntdummygetattr },
2880 		VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
2881 		NULL,			NULL
2882 	};
2883 
2884 	if (vn_make_ops("mnttab", mnt_dummyvnodeops_template,
2885 	    &vfs_mntdummyvnops) != 0) {
2886 		cmn_err(CE_WARN, "vfs_mnttabvp_setup: vn_make_ops failed");
2887 		/* Shouldn't happen, but not bad enough to panic */
2888 		return;
2889 	}
2890 
2891 	/*
2892 	 * A global dummy vnode is allocated to represent mntfs files.
2893 	 * The mntfs file (/etc/mnttab) can be monitored for file events
2894 	 * and receive an event when mnttab changes. Dummy VOP calls
2895 	 * will be made on this vnode. The file events notification module
2896 	 * intercepts this vnode and delivers relevant events.
2897 	 */
2898 	tvp = vn_alloc(KM_SLEEP);
2899 	tvp->v_flag = VNOMOUNT|VNOMAP|VNOSWAP|VNOCACHE;
2900 	vn_setops(tvp, vfs_mntdummyvnops);
2901 	tvp->v_type = VREG;
2902 	/*
2903 	 * The mnt dummy ops do not reference v_data.
2904 	 * No other module intercepting this vnode should either.
2905 	 * Just set it to point to itself.
2906 	 */
2907 	tvp->v_data = (caddr_t)tvp;
2908 	tvp->v_vfsp = rootvfs;
2909 	vfs_mntdummyvp = tvp;
2910 }
2911 
2912 /*
2913  * performs fake read/write ops
2914  */
2915 static void
2916 vfs_mnttab_rwop(int rw)
2917 {
2918 	struct uio	uio;
2919 	struct iovec	iov;
2920 	char	buf[1];
2921 
2922 	if (vfs_mntdummyvp == NULL)
2923 		return;
2924 
2925 	bzero(&uio, sizeof (uio));
2926 	bzero(&iov, sizeof (iov));
2927 	iov.iov_base = buf;
2928 	iov.iov_len = 0;
2929 	uio.uio_iov = &iov;
2930 	uio.uio_iovcnt = 1;
2931 	uio.uio_loffset = 0;
2932 	uio.uio_segflg = UIO_SYSSPACE;
2933 	uio.uio_resid = 0;
2934 	if (rw) {
2935 		(void) VOP_WRITE(vfs_mntdummyvp, &uio, 0, kcred, NULL);
2936 	} else {
2937 		(void) VOP_READ(vfs_mntdummyvp, &uio, 0, kcred, NULL);
2938 	}
2939 }
2940 
2941 /*
2942  * Generate a write operation.
2943  */
2944 void
2945 vfs_mnttab_writeop(void)
2946 {
2947 	vfs_mnttab_rwop(1);
2948 }
2949 
2950 /*
2951  * Generate a read operation.
2952  */
2953 void
2954 vfs_mnttab_readop(void)
2955 {
2956 	vfs_mnttab_rwop(0);
2957 }
2958 
2959 /*
2960  * Free any mnttab information recorded in the vfs struct.
2961  * The vfs must not be on the vfs list.
2962  */
2963 static void
2964 vfs_freemnttab(struct vfs *vfsp)
2965 {
2966 	ASSERT(!VFS_ON_LIST(vfsp));
2967 
2968 	/*
2969 	 * Free device and mount point information
2970 	 */
2971 	if (vfsp->vfs_mntpt != NULL) {
2972 		refstr_rele(vfsp->vfs_mntpt);
2973 		vfsp->vfs_mntpt = NULL;
2974 	}
2975 	if (vfsp->vfs_resource != NULL) {
2976 		refstr_rele(vfsp->vfs_resource);
2977 		vfsp->vfs_resource = NULL;
2978 	}
2979 	/*
2980 	 * Now free mount options information
2981 	 */
2982 	vfs_freeopttbl(&vfsp->vfs_mntopts);
2983 }
2984 
2985 /*
2986  * Return the last mnttab modification time
2987  */
2988 void
2989 vfs_mnttab_modtime(timespec_t *ts)
2990 {
2991 	ASSERT(RW_LOCK_HELD(&vfslist));
2992 	*ts = vfs_mnttab_mtime;
2993 }
2994 
2995 /*
2996  * See if mnttab is changed
2997  */
2998 void
2999 vfs_mnttab_poll(timespec_t *old, struct pollhead **phpp)
3000 {
3001 	int changed;
3002 
3003 	*phpp = (struct pollhead *)NULL;
3004 
3005 	/*
3006 	 * Note: don't grab vfs list lock before accessing vfs_mnttab_mtime.
3007 	 * Can lead to deadlock against vfs_mnttab_modtimeupd(). It is safe
3008 	 * to not grab the vfs list lock because tv_sec is monotonically
3009 	 * increasing.
3010 	 */
3011 
3012 	changed = (old->tv_nsec != vfs_mnttab_mtime.tv_nsec) ||
3013 	    (old->tv_sec != vfs_mnttab_mtime.tv_sec);
3014 	if (!changed) {
3015 		*phpp = &vfs_pollhd;
3016 	}
3017 }
3018 
3019 /* Provide a unique and monotonically-increasing timestamp. */
3020 void
3021 vfs_mono_time(timespec_t *ts)
3022 {
3023 	static volatile hrtime_t hrt;		/* The saved time. */
3024 	hrtime_t	newhrt, oldhrt;		/* For effecting the CAS. */
3025 	timespec_t	newts;
3026 
3027 	/*
3028 	 * Try gethrestime() first, but be prepared to fabricate a sensible
3029 	 * answer at the first sign of any trouble.
3030 	 */
3031 	gethrestime(&newts);
3032 	newhrt = ts2hrt(&newts);
3033 	for (;;) {
3034 		oldhrt = hrt;
3035 		if (newhrt <= hrt)
3036 			newhrt = hrt + 1;
3037 		if (atomic_cas_64((uint64_t *)&hrt, oldhrt, newhrt) == oldhrt)
3038 			break;
3039 	}
3040 	hrt2ts(newhrt, ts);
3041 }
3042 
3043 /*
3044  * Update the mnttab modification time and wake up any waiters for
3045  * mnttab changes
3046  */
3047 void
3048 vfs_mnttab_modtimeupd()
3049 {
3050 	hrtime_t oldhrt, newhrt;
3051 
3052 	ASSERT(RW_WRITE_HELD(&vfslist));
3053 	oldhrt = ts2hrt(&vfs_mnttab_mtime);
3054 	gethrestime(&vfs_mnttab_mtime);
3055 	newhrt = ts2hrt(&vfs_mnttab_mtime);
3056 	if (oldhrt == (hrtime_t)0)
3057 		vfs_mnttab_ctime = vfs_mnttab_mtime;
3058 	/*
3059 	 * Attempt to provide unique mtime (like uniqtime but not).
3060 	 */
3061 	if (newhrt == oldhrt) {
3062 		newhrt++;
3063 		hrt2ts(newhrt, &vfs_mnttab_mtime);
3064 	}
3065 	pollwakeup(&vfs_pollhd, (short)POLLRDBAND);
3066 	vfs_mnttab_writeop();
3067 }
3068 
3069 int
3070 dounmount(struct vfs *vfsp, int flag, cred_t *cr)
3071 {
3072 	vnode_t *coveredvp;
3073 	int error;
3074 	extern void teardown_vopstats(vfs_t *);
3075 
3076 	/*
3077 	 * Get covered vnode. This will be NULL if the vfs is not linked
3078 	 * into the file system name space (i.e., domount() with MNT_NOSPICE).
3079 	 */
3080 	coveredvp = vfsp->vfs_vnodecovered;
3081 	ASSERT(coveredvp == NULL || vn_vfswlock_held(coveredvp));
3082 
3083 	/*
3084 	 * Purge all dnlc entries for this vfs.
3085 	 */
3086 	(void) dnlc_purge_vfsp(vfsp, 0);
3087 
3088 	/* For forcible umount, skip VFS_SYNC() since it may hang */
3089 	if ((flag & MS_FORCE) == 0)
3090 		(void) VFS_SYNC(vfsp, 0, cr);
3091 
3092 	/*
3093 	 * Lock the vfs to maintain fs status quo during unmount.  This
3094 	 * has to be done after the sync because ufs_update tries to acquire
3095 	 * the vfs_reflock.
3096 	 */
3097 	vfs_lock_wait(vfsp);
3098 
3099 	if (error = VFS_UNMOUNT(vfsp, flag, cr)) {
3100 		vfs_unlock(vfsp);
3101 		if (coveredvp != NULL)
3102 			vn_vfsunlock(coveredvp);
3103 	} else if (coveredvp != NULL) {
3104 		teardown_vopstats(vfsp);
3105 		/*
3106 		 * vfs_remove() will do a VN_RELE(vfsp->vfs_vnodecovered)
3107 		 * when it frees vfsp so we do a VN_HOLD() so we can
3108 		 * continue to use coveredvp afterwards.
3109 		 */
3110 		VN_HOLD(coveredvp);
3111 		vfs_remove(vfsp);
3112 		vn_vfsunlock(coveredvp);
3113 		VN_RELE(coveredvp);
3114 	} else {
3115 		teardown_vopstats(vfsp);
3116 		/*
3117 		 * Release the reference to vfs that is not linked
3118 		 * into the name space.
3119 		 */
3120 		vfs_unlock(vfsp);
3121 		VFS_RELE(vfsp);
3122 	}
3123 	return (error);
3124 }
3125 
3126 
3127 /*
3128  * Vfs_unmountall() is called by uadmin() to unmount all
3129  * mounted file systems (except the root file system) during shutdown.
3130  * It follows the existing locking protocol when traversing the vfs list
3131  * to sync and unmount vfses. Even though there should be no
3132  * other thread running while the system is shutting down, it is prudent
3133  * to still follow the locking protocol.
3134  */
3135 void
3136 vfs_unmountall(void)
3137 {
3138 	struct vfs *vfsp;
3139 	struct vfs *prev_vfsp = NULL;
3140 	int error;
3141 
3142 	/*
3143 	 * Toss all dnlc entries now so that the per-vfs sync
3144 	 * and unmount operations don't have to slog through
3145 	 * a bunch of uninteresting vnodes over and over again.
3146 	 */
3147 	dnlc_purge();
3148 
3149 	vfs_list_lock();
3150 	for (vfsp = rootvfs->vfs_prev; vfsp != rootvfs; vfsp = prev_vfsp) {
3151 		prev_vfsp = vfsp->vfs_prev;
3152 
3153 		if (vfs_lock(vfsp) != 0)
3154 			continue;
3155 		error = vn_vfswlock(vfsp->vfs_vnodecovered);
3156 		vfs_unlock(vfsp);
3157 		if (error)
3158 			continue;
3159 
3160 		vfs_list_unlock();
3161 
3162 		(void) VFS_SYNC(vfsp, SYNC_CLOSE, CRED());
3163 		(void) dounmount(vfsp, 0, CRED());
3164 
3165 		/*
3166 		 * Since we dropped the vfslist lock above we must
3167 		 * verify that next_vfsp still exists, else start over.
3168 		 */
3169 		vfs_list_lock();
3170 		for (vfsp = rootvfs->vfs_prev;
3171 		    vfsp != rootvfs; vfsp = vfsp->vfs_prev)
3172 			if (vfsp == prev_vfsp)
3173 				break;
3174 		if (vfsp == rootvfs && prev_vfsp != rootvfs)
3175 			prev_vfsp = rootvfs->vfs_prev;
3176 	}
3177 	vfs_list_unlock();
3178 }
3179 
3180 /*
3181  * Called to add an entry to the end of the vfs mount in progress list
3182  */
3183 void
3184 vfs_addmip(dev_t dev, struct vfs *vfsp)
3185 {
3186 	struct ipmnt *mipp;
3187 
3188 	mipp = (struct ipmnt *)kmem_alloc(sizeof (struct ipmnt), KM_SLEEP);
3189 	mipp->mip_next = NULL;
3190 	mipp->mip_dev = dev;
3191 	mipp->mip_vfsp = vfsp;
3192 	mutex_enter(&vfs_miplist_mutex);
3193 	if (vfs_miplist_end != NULL)
3194 		vfs_miplist_end->mip_next = mipp;
3195 	else
3196 		vfs_miplist = mipp;
3197 	vfs_miplist_end = mipp;
3198 	mutex_exit(&vfs_miplist_mutex);
3199 }
3200 
3201 /*
3202  * Called to remove an entry from the mount in progress list
3203  * Either because the mount completed or it failed.
3204  */
3205 void
3206 vfs_delmip(struct vfs *vfsp)
3207 {
3208 	struct ipmnt *mipp, *mipprev;
3209 
3210 	mutex_enter(&vfs_miplist_mutex);
3211 	mipprev = NULL;
3212 	for (mipp = vfs_miplist;
3213 	    mipp && mipp->mip_vfsp != vfsp; mipp = mipp->mip_next) {
3214 		mipprev = mipp;
3215 	}
3216 	if (mipp == NULL)
3217 		return; /* shouldn't happen */
3218 	if (mipp == vfs_miplist_end)
3219 		vfs_miplist_end = mipprev;
3220 	if (mipprev == NULL)
3221 		vfs_miplist = mipp->mip_next;
3222 	else
3223 		mipprev->mip_next = mipp->mip_next;
3224 	mutex_exit(&vfs_miplist_mutex);
3225 	kmem_free(mipp, sizeof (struct ipmnt));
3226 }
3227 
3228 /*
3229  * vfs_add is called by a specific filesystem's mount routine to add
3230  * the new vfs into the vfs list/hash and to cover the mounted-on vnode.
3231  * The vfs should already have been locked by the caller.
3232  *
3233  * coveredvp is NULL if this is the root.
3234  */
3235 void
3236 vfs_add(vnode_t *coveredvp, struct vfs *vfsp, int mflag)
3237 {
3238 	int newflag;
3239 
3240 	ASSERT(vfs_lock_held(vfsp));
3241 	VFS_HOLD(vfsp);
3242 	newflag = vfsp->vfs_flag;
3243 	if (mflag & MS_RDONLY)
3244 		newflag |= VFS_RDONLY;
3245 	else
3246 		newflag &= ~VFS_RDONLY;
3247 	if (mflag & MS_NOSUID)
3248 		newflag |= (VFS_NOSETUID|VFS_NODEVICES);
3249 	else
3250 		newflag &= ~(VFS_NOSETUID|VFS_NODEVICES);
3251 	if (mflag & MS_NOMNTTAB)
3252 		newflag |= VFS_NOMNTTAB;
3253 	else
3254 		newflag &= ~VFS_NOMNTTAB;
3255 
3256 	if (coveredvp != NULL) {
3257 		ASSERT(vn_vfswlock_held(coveredvp));
3258 		coveredvp->v_vfsmountedhere = vfsp;
3259 		VN_HOLD(coveredvp);
3260 	}
3261 	vfsp->vfs_vnodecovered = coveredvp;
3262 	vfsp->vfs_flag = newflag;
3263 
3264 	vfs_list_add(vfsp);
3265 }
3266 
3267 /*
3268  * Remove a vfs from the vfs list, null out the pointer from the
3269  * covered vnode to the vfs (v_vfsmountedhere), and null out the pointer
3270  * from the vfs to the covered vnode (vfs_vnodecovered). Release the
3271  * reference to the vfs and to the covered vnode.
3272  *
3273  * Called from dounmount after it's confirmed with the file system
3274  * that the unmount is legal.
3275  */
3276 void
3277 vfs_remove(struct vfs *vfsp)
3278 {
3279 	vnode_t *vp;
3280 
3281 	ASSERT(vfs_lock_held(vfsp));
3282 
3283 	/*
3284 	 * Can't unmount root.  Should never happen because fs will
3285 	 * be busy.
3286 	 */
3287 	if (vfsp == rootvfs)
3288 		panic("vfs_remove: unmounting root");
3289 
3290 	vfs_list_remove(vfsp);
3291 
3292 	/*
3293 	 * Unhook from the file system name space.
3294 	 */
3295 	vp = vfsp->vfs_vnodecovered;
3296 	ASSERT(vn_vfswlock_held(vp));
3297 	vp->v_vfsmountedhere = NULL;
3298 	vfsp->vfs_vnodecovered = NULL;
3299 	VN_RELE(vp);
3300 
3301 	/*
3302 	 * Release lock and wakeup anybody waiting.
3303 	 */
3304 	vfs_unlock(vfsp);
3305 	VFS_RELE(vfsp);
3306 }
3307 
3308 /*
3309  * Lock a filesystem to prevent access to it while mounting,
3310  * unmounting and syncing.  Return EBUSY immediately if lock
3311  * can't be acquired.
3312  */
3313 int
3314 vfs_lock(vfs_t *vfsp)
3315 {
3316 	vn_vfslocks_entry_t *vpvfsentry;
3317 
3318 	vpvfsentry = vn_vfslocks_getlock(vfsp);
3319 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER))
3320 		return (0);
3321 
3322 	vn_vfslocks_rele(vpvfsentry);
3323 	return (EBUSY);
3324 }
3325 
3326 int
3327 vfs_rlock(vfs_t *vfsp)
3328 {
3329 	vn_vfslocks_entry_t *vpvfsentry;
3330 
3331 	vpvfsentry = vn_vfslocks_getlock(vfsp);
3332 
3333 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER))
3334 		return (0);
3335 
3336 	vn_vfslocks_rele(vpvfsentry);
3337 	return (EBUSY);
3338 }
3339 
3340 void
3341 vfs_lock_wait(vfs_t *vfsp)
3342 {
3343 	vn_vfslocks_entry_t *vpvfsentry;
3344 
3345 	vpvfsentry = vn_vfslocks_getlock(vfsp);
3346 	rwst_enter(&vpvfsentry->ve_lock, RW_WRITER);
3347 }
3348 
3349 void
3350 vfs_rlock_wait(vfs_t *vfsp)
3351 {
3352 	vn_vfslocks_entry_t *vpvfsentry;
3353 
3354 	vpvfsentry = vn_vfslocks_getlock(vfsp);
3355 	rwst_enter(&vpvfsentry->ve_lock, RW_READER);
3356 }
3357 
3358 /*
3359  * Unlock a locked filesystem.
3360  */
3361 void
3362 vfs_unlock(vfs_t *vfsp)
3363 {
3364 	vn_vfslocks_entry_t *vpvfsentry;
3365 
3366 	/*
3367 	 * vfs_unlock will mimic sema_v behaviour to fix 4748018.
3368 	 * And these changes should remain for the patch changes as it is.
3369 	 */
3370 	if (panicstr)
3371 		return;
3372 
3373 	/*
3374 	 * ve_refcount needs to be dropped twice here.
3375 	 * 1. To release refernce after a call to vfs_locks_getlock()
3376 	 * 2. To release the reference from the locking routines like
3377 	 *    vfs_rlock_wait/vfs_wlock_wait/vfs_wlock etc,.
3378 	 */
3379 
3380 	vpvfsentry = vn_vfslocks_getlock(vfsp);
3381 	vn_vfslocks_rele(vpvfsentry);
3382 
3383 	rwst_exit(&vpvfsentry->ve_lock);
3384 	vn_vfslocks_rele(vpvfsentry);
3385 }
3386 
3387 /*
3388  * Utility routine that allows a filesystem to construct its
3389  * fsid in "the usual way" - by munging some underlying dev_t and
3390  * the filesystem type number into the 64-bit fsid.  Note that
3391  * this implicitly relies on dev_t persistence to make filesystem
3392  * id's persistent.
3393  *
3394  * There's nothing to prevent an individual fs from constructing its
3395  * fsid in a different way, and indeed they should.
3396  *
3397  * Since we want fsids to be 32-bit quantities (so that they can be
3398  * exported identically by either 32-bit or 64-bit APIs, as well as
3399  * the fact that fsid's are "known" to NFS), we compress the device
3400  * number given down to 32-bits, and panic if that isn't possible.
3401  */
3402 void
3403 vfs_make_fsid(fsid_t *fsi, dev_t dev, int val)
3404 {
3405 	if (!cmpldev((dev32_t *)&fsi->val[0], dev))
3406 		panic("device number too big for fsid!");
3407 	fsi->val[1] = val;
3408 }
3409 
3410 int
3411 vfs_lock_held(vfs_t *vfsp)
3412 {
3413 	int held;
3414 	vn_vfslocks_entry_t *vpvfsentry;
3415 
3416 	/*
3417 	 * vfs_lock_held will mimic sema_held behaviour
3418 	 * if panicstr is set. And these changes should remain
3419 	 * for the patch changes as it is.
3420 	 */
3421 	if (panicstr)
3422 		return (1);
3423 
3424 	vpvfsentry = vn_vfslocks_getlock(vfsp);
3425 	held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER);
3426 
3427 	vn_vfslocks_rele(vpvfsentry);
3428 	return (held);
3429 }
3430 
3431 struct _kthread *
3432 vfs_lock_owner(vfs_t *vfsp)
3433 {
3434 	struct _kthread *owner;
3435 	vn_vfslocks_entry_t *vpvfsentry;
3436 
3437 	/*
3438 	 * vfs_wlock_held will mimic sema_held behaviour
3439 	 * if panicstr is set. And these changes should remain
3440 	 * for the patch changes as it is.
3441 	 */
3442 	if (panicstr)
3443 		return (NULL);
3444 
3445 	vpvfsentry = vn_vfslocks_getlock(vfsp);
3446 	owner = rwst_owner(&vpvfsentry->ve_lock);
3447 
3448 	vn_vfslocks_rele(vpvfsentry);
3449 	return (owner);
3450 }
3451 
3452 /*
3453  * vfs list locking.
3454  *
3455  * Rather than manipulate the vfslist lock directly, we abstract into lock
3456  * and unlock routines to allow the locking implementation to be changed for
3457  * clustering.
3458  *
3459  * Whenever the vfs list is modified through its hash links, the overall list
3460  * lock must be obtained before locking the relevant hash bucket.  But to see
3461  * whether a given vfs is on the list, it suffices to obtain the lock for the
3462  * hash bucket without getting the overall list lock.  (See getvfs() below.)
3463  */
3464 
3465 void
3466 vfs_list_lock()
3467 {
3468 	rw_enter(&vfslist, RW_WRITER);
3469 }
3470 
3471 void
3472 vfs_list_read_lock()
3473 {
3474 	rw_enter(&vfslist, RW_READER);
3475 }
3476 
3477 void
3478 vfs_list_unlock()
3479 {
3480 	rw_exit(&vfslist);
3481 }
3482 
3483 /*
3484  * Low level worker routines for adding entries to and removing entries from
3485  * the vfs list.
3486  */
3487 
3488 static void
3489 vfs_hash_add(struct vfs *vfsp, int insert_at_head)
3490 {
3491 	int vhno;
3492 	struct vfs **hp;
3493 	dev_t dev;
3494 
3495 	ASSERT(RW_WRITE_HELD(&vfslist));
3496 
3497 	dev = expldev(vfsp->vfs_fsid.val[0]);
3498 	vhno = VFSHASH(getmajor(dev), getminor(dev));
3499 
3500 	mutex_enter(&rvfs_list[vhno].rvfs_lock);
3501 
3502 	/*
3503 	 * Link into the hash table, inserting it at the end, so that LOFS
3504 	 * with the same fsid as UFS (or other) file systems will not hide the
3505 	 * UFS.
3506 	 */
3507 	if (insert_at_head) {
3508 		vfsp->vfs_hash = rvfs_list[vhno].rvfs_head;
3509 		rvfs_list[vhno].rvfs_head = vfsp;
3510 	} else {
3511 		for (hp = &rvfs_list[vhno].rvfs_head; *hp != NULL;
3512 		    hp = &(*hp)->vfs_hash)
3513 			continue;
3514 		/*
3515 		 * hp now contains the address of the pointer to update
3516 		 * to effect the insertion.
3517 		 */
3518 		vfsp->vfs_hash = NULL;
3519 		*hp = vfsp;
3520 	}
3521 
3522 	rvfs_list[vhno].rvfs_len++;
3523 	mutex_exit(&rvfs_list[vhno].rvfs_lock);
3524 }
3525 
3526 
3527 static void
3528 vfs_hash_remove(struct vfs *vfsp)
3529 {
3530 	int vhno;
3531 	struct vfs *tvfsp;
3532 	dev_t dev;
3533 
3534 	ASSERT(RW_WRITE_HELD(&vfslist));
3535 
3536 	dev = expldev(vfsp->vfs_fsid.val[0]);
3537 	vhno = VFSHASH(getmajor(dev), getminor(dev));
3538 
3539 	mutex_enter(&rvfs_list[vhno].rvfs_lock);
3540 
3541 	/*
3542 	 * Remove from hash.
3543 	 */
3544 	if (rvfs_list[vhno].rvfs_head == vfsp) {
3545 		rvfs_list[vhno].rvfs_head = vfsp->vfs_hash;
3546 		rvfs_list[vhno].rvfs_len--;
3547 		goto foundit;
3548 	}
3549 	for (tvfsp = rvfs_list[vhno].rvfs_head; tvfsp != NULL;
3550 	    tvfsp = tvfsp->vfs_hash) {
3551 		if (tvfsp->vfs_hash == vfsp) {
3552 			tvfsp->vfs_hash = vfsp->vfs_hash;
3553 			rvfs_list[vhno].rvfs_len--;
3554 			goto foundit;
3555 		}
3556 	}
3557 	cmn_err(CE_WARN, "vfs_list_remove: vfs not found in hash");
3558 
3559 foundit:
3560 
3561 	mutex_exit(&rvfs_list[vhno].rvfs_lock);
3562 }
3563 
3564 
3565 void
3566 vfs_list_add(struct vfs *vfsp)
3567 {
3568 	zone_t *zone;
3569 
3570 	/*
3571 	 * Typically, the vfs_t will have been created on behalf of the file
3572 	 * system in vfs_init, where it will have been provided with a
3573 	 * vfs_impl_t. This, however, might be lacking if the vfs_t was created
3574 	 * by an unbundled file system. We therefore check for such an example
3575 	 * before stamping the vfs_t with its creation time for the benefit of
3576 	 * mntfs.
3577 	 */
3578 	if (vfsp->vfs_implp == NULL)
3579 		vfsimpl_setup(vfsp);
3580 	vfs_mono_time(&vfsp->vfs_hrctime);
3581 
3582 	/*
3583 	 * The zone that owns the mount is the one that performed the mount.
3584 	 * Note that this isn't necessarily the same as the zone mounted into.
3585 	 * The corresponding zone_rele_ref() will be done when the vfs_t
3586 	 * is being free'd.
3587 	 */
3588 	vfsp->vfs_zone = curproc->p_zone;
3589 	zone_init_ref(&vfsp->vfs_implp->vi_zone_ref);
3590 	zone_hold_ref(vfsp->vfs_zone, &vfsp->vfs_implp->vi_zone_ref,
3591 	    ZONE_REF_VFS);
3592 
3593 	/*
3594 	 * Find the zone mounted into, and put this mount on its vfs list.
3595 	 */
3596 	zone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
3597 	ASSERT(zone != NULL);
3598 	/*
3599 	 * Special casing for the root vfs.  This structure is allocated
3600 	 * statically and hooked onto rootvfs at link time.  During the
3601 	 * vfs_mountroot call at system startup time, the root file system's
3602 	 * VFS_MOUNTROOT routine will call vfs_add with this root vfs struct
3603 	 * as argument.  The code below must detect and handle this special
3604 	 * case.  The only apparent justification for this special casing is
3605 	 * to ensure that the root file system appears at the head of the
3606 	 * list.
3607 	 *
3608 	 * XXX:	I'm assuming that it's ok to do normal list locking when
3609 	 *	adding the entry for the root file system (this used to be
3610 	 *	done with no locks held).
3611 	 */
3612 	vfs_list_lock();
3613 	/*
3614 	 * Link into the vfs list proper.
3615 	 */
3616 	if (vfsp == &root) {
3617 		/*
3618 		 * Assert: This vfs is already on the list as its first entry.
3619 		 * Thus, there's nothing to do.
3620 		 */
3621 		ASSERT(rootvfs == vfsp);
3622 		/*
3623 		 * Add it to the head of the global zone's vfslist.
3624 		 */
3625 		ASSERT(zone == global_zone);
3626 		ASSERT(zone->zone_vfslist == NULL);
3627 		zone->zone_vfslist = vfsp;
3628 	} else {
3629 		/*
3630 		 * Link to end of list using vfs_prev (as rootvfs is now a
3631 		 * doubly linked circular list) so list is in mount order for
3632 		 * mnttab use.
3633 		 */
3634 		rootvfs->vfs_prev->vfs_next = vfsp;
3635 		vfsp->vfs_prev = rootvfs->vfs_prev;
3636 		rootvfs->vfs_prev = vfsp;
3637 		vfsp->vfs_next = rootvfs;
3638 
3639 		/*
3640 		 * Do it again for the zone-private list (which may be NULL).
3641 		 */
3642 		if (zone->zone_vfslist == NULL) {
3643 			ASSERT(zone != global_zone);
3644 			zone->zone_vfslist = vfsp;
3645 		} else {
3646 			zone->zone_vfslist->vfs_zone_prev->vfs_zone_next = vfsp;
3647 			vfsp->vfs_zone_prev = zone->zone_vfslist->vfs_zone_prev;
3648 			zone->zone_vfslist->vfs_zone_prev = vfsp;
3649 			vfsp->vfs_zone_next = zone->zone_vfslist;
3650 		}
3651 	}
3652 
3653 	/*
3654 	 * Link into the hash table, inserting it at the end, so that LOFS
3655 	 * with the same fsid as UFS (or other) file systems will not hide
3656 	 * the UFS.
3657 	 */
3658 	vfs_hash_add(vfsp, 0);
3659 
3660 	/*
3661 	 * Link into tree indexed by mntpoint, for vfs_mntpoint2vfsp
3662 	 * mntix discerns entries with the same key
3663 	 */
3664 	vfsp->vfs_mntix = ++vfs_curr_mntix;
3665 	avl_add(&vfs_by_dev, vfsp);
3666 
3667 	/*
3668 	 * Link into tree indexed by dev, for vfs_devismounted
3669 	 */
3670 	avl_add(&vfs_by_mntpnt, vfsp);
3671 
3672 	/*
3673 	 * update the mnttab modification time
3674 	 */
3675 	vfs_mnttab_modtimeupd();
3676 	vfs_list_unlock();
3677 	zone_rele(zone);
3678 }
3679 
3680 void
3681 vfs_list_remove(struct vfs *vfsp)
3682 {
3683 	zone_t *zone;
3684 
3685 	zone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
3686 	ASSERT(zone != NULL);
3687 	/*
3688 	 * Callers are responsible for preventing attempts to unmount the
3689 	 * root.
3690 	 */
3691 	ASSERT(vfsp != rootvfs);
3692 
3693 	vfs_list_lock();
3694 
3695 	/*
3696 	 * Remove from avl trees
3697 	 */
3698 	avl_remove(&vfs_by_mntpnt, vfsp);
3699 	avl_remove(&vfs_by_dev, vfsp);
3700 
3701 	/*
3702 	 * Remove from hash.
3703 	 */
3704 	vfs_hash_remove(vfsp);
3705 
3706 	/*
3707 	 * Remove from vfs list.
3708 	 */
3709 	vfsp->vfs_prev->vfs_next = vfsp->vfs_next;
3710 	vfsp->vfs_next->vfs_prev = vfsp->vfs_prev;
3711 	vfsp->vfs_next = vfsp->vfs_prev = NULL;
3712 
3713 	/*
3714 	 * Remove from zone-specific vfs list.
3715 	 */
3716 	if (zone->zone_vfslist == vfsp)
3717 		zone->zone_vfslist = vfsp->vfs_zone_next;
3718 
3719 	if (vfsp->vfs_zone_next == vfsp) {
3720 		ASSERT(vfsp->vfs_zone_prev == vfsp);
3721 		ASSERT(zone->zone_vfslist == vfsp);
3722 		zone->zone_vfslist = NULL;
3723 	}
3724 
3725 	vfsp->vfs_zone_prev->vfs_zone_next = vfsp->vfs_zone_next;
3726 	vfsp->vfs_zone_next->vfs_zone_prev = vfsp->vfs_zone_prev;
3727 	vfsp->vfs_zone_next = vfsp->vfs_zone_prev = NULL;
3728 
3729 	/*
3730 	 * update the mnttab modification time
3731 	 */
3732 	vfs_mnttab_modtimeupd();
3733 	vfs_list_unlock();
3734 	zone_rele(zone);
3735 }
3736 
3737 struct vfs *
3738 getvfs(fsid_t *fsid)
3739 {
3740 	struct vfs *vfsp;
3741 	int val0 = fsid->val[0];
3742 	int val1 = fsid->val[1];
3743 	dev_t dev = expldev(val0);
3744 	int vhno = VFSHASH(getmajor(dev), getminor(dev));
3745 	kmutex_t *hmp = &rvfs_list[vhno].rvfs_lock;
3746 
3747 	mutex_enter(hmp);
3748 	for (vfsp = rvfs_list[vhno].rvfs_head; vfsp; vfsp = vfsp->vfs_hash) {
3749 		if (vfsp->vfs_fsid.val[0] == val0 &&
3750 		    vfsp->vfs_fsid.val[1] == val1) {
3751 			VFS_HOLD(vfsp);
3752 			mutex_exit(hmp);
3753 			return (vfsp);
3754 		}
3755 	}
3756 	mutex_exit(hmp);
3757 	return (NULL);
3758 }
3759 
3760 /*
3761  * Search the vfs mount in progress list for a specified device/vfs entry.
3762  * Returns 0 if the first entry in the list that the device matches has the
3763  * given vfs pointer as well.  If the device matches but a different vfs
3764  * pointer is encountered in the list before the given vfs pointer then
3765  * a 1 is returned.
3766  */
3767 
3768 int
3769 vfs_devmounting(dev_t dev, struct vfs *vfsp)
3770 {
3771 	int retval = 0;
3772 	struct ipmnt *mipp;
3773 
3774 	mutex_enter(&vfs_miplist_mutex);
3775 	for (mipp = vfs_miplist; mipp != NULL; mipp = mipp->mip_next) {
3776 		if (mipp->mip_dev == dev) {
3777 			if (mipp->mip_vfsp != vfsp)
3778 				retval = 1;
3779 			break;
3780 		}
3781 	}
3782 	mutex_exit(&vfs_miplist_mutex);
3783 	return (retval);
3784 }
3785 
3786 /*
3787  * Search the vfs list for a specified device.  Returns 1, if entry is found
3788  * or 0 if no suitable entry is found.
3789  */
3790 
3791 int
3792 vfs_devismounted(dev_t dev)
3793 {
3794 	struct vfs *vfsp;
3795 	int found = 0;
3796 	struct vfs search;
3797 	avl_index_t index;
3798 
3799 	search.vfs_dev = dev;
3800 	search.vfs_mntix = 0;
3801 
3802 	vfs_list_read_lock();
3803 
3804 	/*
3805 	 * there might be several entries with the same dev in the tree,
3806 	 * only discerned by mntix. To find the first, we start with a mntix
3807 	 * of 0. The search will fail. The following avl_nearest will give
3808 	 * us the actual first entry.
3809 	 */
3810 	VERIFY(avl_find(&vfs_by_dev, &search, &index) == NULL);
3811 	vfsp = avl_nearest(&vfs_by_dev, index, AVL_AFTER);
3812 
3813 	if (vfsp != NULL && vfsp->vfs_dev == dev)
3814 		found = 1;
3815 
3816 	vfs_list_unlock();
3817 	return (found);
3818 }
3819 
3820 /*
3821  * Search the vfs list for a specified device.  Returns a pointer to it
3822  * or NULL if no suitable entry is found. The caller of this routine
3823  * is responsible for releasing the returned vfs pointer.
3824  */
3825 struct vfs *
3826 vfs_dev2vfsp(dev_t dev)
3827 {
3828 	struct vfs *vfsp;
3829 	int found;
3830 	struct vfs search;
3831 	avl_index_t index;
3832 
3833 	search.vfs_dev = dev;
3834 	search.vfs_mntix = 0;
3835 
3836 	vfs_list_read_lock();
3837 
3838 	/*
3839 	 * there might be several entries with the same dev in the tree,
3840 	 * only discerned by mntix. To find the first, we start with a mntix
3841 	 * of 0. The search will fail. The following avl_nearest will give
3842 	 * us the actual first entry.
3843 	 */
3844 	VERIFY(avl_find(&vfs_by_dev, &search, &index) == NULL);
3845 	vfsp = avl_nearest(&vfs_by_dev, index, AVL_AFTER);
3846 
3847 	found = 0;
3848 	while (vfsp != NULL && vfsp->vfs_dev == dev) {
3849 		/*
3850 		 * The following could be made more efficient by making
3851 		 * the entire loop use vfs_zone_next if the call is from
3852 		 * a zone.  The only callers, however, ustat(2) and
3853 		 * umount2(2), don't seem to justify the added
3854 		 * complexity at present.
3855 		 */
3856 		if (ZONE_PATH_VISIBLE(refstr_value(vfsp->vfs_mntpt),
3857 		    curproc->p_zone)) {
3858 			VFS_HOLD(vfsp);
3859 			found = 1;
3860 			break;
3861 		}
3862 		vfsp = AVL_NEXT(&vfs_by_dev, vfsp);
3863 	}
3864 	vfs_list_unlock();
3865 	return (found ? vfsp : NULL);
3866 }
3867 
3868 /*
3869  * Search the vfs list for a specified mntpoint.  Returns a pointer to it
3870  * or NULL if no suitable entry is found. The caller of this routine
3871  * is responsible for releasing the returned vfs pointer.
3872  *
3873  * Note that if multiple mntpoints match, the last one matching is
3874  * returned in an attempt to return the "top" mount when overlay
3875  * mounts are covering the same mount point.  This is accomplished by starting
3876  * at the end of the list and working our way backwards, stopping at the first
3877  * matching mount.
3878  */
3879 struct vfs *
3880 vfs_mntpoint2vfsp(const char *mp)
3881 {
3882 	struct vfs *vfsp;
3883 	struct vfs *retvfsp = NULL;
3884 	zone_t *zone = curproc->p_zone;
3885 	struct vfs *list;
3886 
3887 	vfs_list_read_lock();
3888 	if (getzoneid() == GLOBAL_ZONEID) {
3889 		/*
3890 		 * The global zone may see filesystems in any zone.
3891 		 */
3892 		struct vfs search;
3893 		search.vfs_mntpt = refstr_alloc(mp);
3894 		search.vfs_mntix = UINT64_MAX;
3895 		avl_index_t index;
3896 
3897 		/*
3898 		 * there might be several entries with the same mntpnt in the
3899 		 * tree, only discerned by mntix. To find the last, we start
3900 		 * with a mntix of UINT64_MAX. The search will fail. The
3901 		 * following avl_nearest will give  us the actual last entry
3902 		 * matching the mntpnt.
3903 		 */
3904 		VERIFY(avl_find(&vfs_by_mntpnt, &search, &index) == 0);
3905 		vfsp = avl_nearest(&vfs_by_mntpnt, index, AVL_BEFORE);
3906 
3907 		refstr_rele(search.vfs_mntpt);
3908 
3909 		if (vfsp != NULL &&
3910 		    strcmp(refstr_value(vfsp->vfs_mntpt), mp) == 0)
3911 			retvfsp = vfsp;
3912 	} else if ((list = zone->zone_vfslist) != NULL) {
3913 		const char *mntpt;
3914 
3915 		vfsp = list->vfs_zone_prev;
3916 		do {
3917 			mntpt = refstr_value(vfsp->vfs_mntpt);
3918 			mntpt = ZONE_PATH_TRANSLATE(mntpt, zone);
3919 			if (strcmp(mntpt, mp) == 0) {
3920 				retvfsp = vfsp;
3921 				break;
3922 			}
3923 			vfsp = vfsp->vfs_zone_prev;
3924 		} while (vfsp != list->vfs_zone_prev);
3925 	}
3926 	if (retvfsp)
3927 		VFS_HOLD(retvfsp);
3928 	vfs_list_unlock();
3929 	return (retvfsp);
3930 }
3931 
3932 /*
3933  * Search the vfs list for a specified vfsops.
3934  * if vfs entry is found then return 1, else 0.
3935  */
3936 int
3937 vfs_opsinuse(vfsops_t *ops)
3938 {
3939 	struct vfs *vfsp;
3940 	int found;
3941 
3942 	vfs_list_read_lock();
3943 	vfsp = rootvfs;
3944 	found = 0;
3945 	do {
3946 		if (vfs_getops(vfsp) == ops) {
3947 			found = 1;
3948 			break;
3949 		}
3950 		vfsp = vfsp->vfs_next;
3951 	} while (vfsp != rootvfs);
3952 	vfs_list_unlock();
3953 	return (found);
3954 }
3955 
3956 /*
3957  * Allocate an entry in vfssw for a file system type
3958  */
3959 struct vfssw *
3960 allocate_vfssw(const char *type)
3961 {
3962 	struct vfssw *vswp;
3963 
3964 	if (type[0] == '\0' || strlen(type) + 1 > _ST_FSTYPSZ) {
3965 		/*
3966 		 * The vfssw table uses the empty string to identify an
3967 		 * available entry; we cannot add any type which has
3968 		 * a leading NUL. The string length is limited to
3969 		 * the size of the st_fstype array in struct stat.
3970 		 */
3971 		return (NULL);
3972 	}
3973 
3974 	ASSERT(VFSSW_WRITE_LOCKED());
3975 	for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++)
3976 		if (!ALLOCATED_VFSSW(vswp)) {
3977 			vswp->vsw_name = kmem_alloc(strlen(type) + 1, KM_SLEEP);
3978 			(void) strcpy(vswp->vsw_name, type);
3979 			ASSERT(vswp->vsw_count == 0);
3980 			vswp->vsw_count = 1;
3981 			mutex_init(&vswp->vsw_lock, NULL, MUTEX_DEFAULT, NULL);
3982 			return (vswp);
3983 		}
3984 	return (NULL);
3985 }
3986 
3987 /*
3988  * Impose additional layer of translation between vfstype names
3989  * and module names in the filesystem.
3990  */
3991 static const char *
3992 vfs_to_modname(const char *vfstype)
3993 {
3994 	if (strcmp(vfstype, "proc") == 0) {
3995 		vfstype = "procfs";
3996 	} else if (strcmp(vfstype, "fd") == 0) {
3997 		vfstype = "fdfs";
3998 	} else if (strncmp(vfstype, "nfs", 3) == 0) {
3999 		vfstype = "nfs";
4000 	}
4001 
4002 	return (vfstype);
4003 }
4004 
4005 /*
4006  * Find a vfssw entry given a file system type name.
4007  * Try to autoload the filesystem if it's not found.
4008  * If it's installed, return the vfssw locked to prevent unloading.
4009  */
4010 struct vfssw *
4011 vfs_getvfssw(const char *type)
4012 {
4013 	struct vfssw *vswp;
4014 	const char *modname;
4015 
4016 	RLOCK_VFSSW();
4017 	vswp = vfs_getvfsswbyname(type);
4018 	modname = vfs_to_modname(type);
4019 
4020 	if (rootdir == NULL) {
4021 		/*
4022 		 * If we haven't yet loaded the root file system, then our
4023 		 * _init won't be called until later. Allocate vfssw entry,
4024 		 * because mod_installfs won't be called.
4025 		 */
4026 		if (vswp == NULL) {
4027 			RUNLOCK_VFSSW();
4028 			WLOCK_VFSSW();
4029 			if ((vswp = vfs_getvfsswbyname(type)) == NULL) {
4030 				if ((vswp = allocate_vfssw(type)) == NULL) {
4031 					WUNLOCK_VFSSW();
4032 					return (NULL);
4033 				}
4034 			}
4035 			WUNLOCK_VFSSW();
4036 			RLOCK_VFSSW();
4037 		}
4038 		if (!VFS_INSTALLED(vswp)) {
4039 			RUNLOCK_VFSSW();
4040 			(void) modloadonly("fs", modname);
4041 		} else
4042 			RUNLOCK_VFSSW();
4043 		return (vswp);
4044 	}
4045 
4046 	/*
4047 	 * Try to load the filesystem.  Before calling modload(), we drop
4048 	 * our lock on the VFS switch table, and pick it up after the
4049 	 * module is loaded.  However, there is a potential race:  the
4050 	 * module could be unloaded after the call to modload() completes
4051 	 * but before we pick up the lock and drive on.  Therefore,
4052 	 * we keep reloading the module until we've loaded the module
4053 	 * _and_ we have the lock on the VFS switch table.
4054 	 */
4055 	while (vswp == NULL || !VFS_INSTALLED(vswp)) {
4056 		RUNLOCK_VFSSW();
4057 		if (modload("fs", modname) == -1)
4058 			return (NULL);
4059 		RLOCK_VFSSW();
4060 		if (vswp == NULL)
4061 			if ((vswp = vfs_getvfsswbyname(type)) == NULL)
4062 				break;
4063 	}
4064 	RUNLOCK_VFSSW();
4065 
4066 	return (vswp);
4067 }
4068 
4069 /*
4070  * Find a vfssw entry given a file system type name.
4071  */
4072 struct vfssw *
4073 vfs_getvfsswbyname(const char *type)
4074 {
4075 	struct vfssw *vswp;
4076 
4077 	ASSERT(VFSSW_LOCKED());
4078 	if (type == NULL || *type == '\0')
4079 		return (NULL);
4080 
4081 	for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
4082 		if (strcmp(type, vswp->vsw_name) == 0) {
4083 			vfs_refvfssw(vswp);
4084 			return (vswp);
4085 		}
4086 	}
4087 
4088 	return (NULL);
4089 }
4090 
4091 /*
4092  * Find a vfssw entry given a set of vfsops.
4093  */
4094 struct vfssw *
4095 vfs_getvfsswbyvfsops(vfsops_t *vfsops)
4096 {
4097 	struct vfssw *vswp;
4098 
4099 	RLOCK_VFSSW();
4100 	for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
4101 		if (ALLOCATED_VFSSW(vswp) && &vswp->vsw_vfsops == vfsops) {
4102 			vfs_refvfssw(vswp);
4103 			RUNLOCK_VFSSW();
4104 			return (vswp);
4105 		}
4106 	}
4107 	RUNLOCK_VFSSW();
4108 
4109 	return (NULL);
4110 }
4111 
4112 /*
4113  * Reference a vfssw entry.
4114  */
4115 void
4116 vfs_refvfssw(struct vfssw *vswp)
4117 {
4118 
4119 	mutex_enter(&vswp->vsw_lock);
4120 	vswp->vsw_count++;
4121 	mutex_exit(&vswp->vsw_lock);
4122 }
4123 
4124 /*
4125  * Unreference a vfssw entry.
4126  */
4127 void
4128 vfs_unrefvfssw(struct vfssw *vswp)
4129 {
4130 
4131 	mutex_enter(&vswp->vsw_lock);
4132 	vswp->vsw_count--;
4133 	mutex_exit(&vswp->vsw_lock);
4134 }
4135 
4136 int sync_timeout = 30;		/* timeout for syncing a page during panic */
4137 int sync_timeleft;		/* portion of sync_timeout remaining */
4138 
4139 static int sync_retries = 20;	/* number of retries when not making progress */
4140 static int sync_triesleft;	/* portion of sync_retries remaining */
4141 
4142 static pgcnt_t old_pgcnt, new_pgcnt;
4143 static int new_bufcnt, old_bufcnt;
4144 
4145 /*
4146  * Sync all of the mounted filesystems, and then wait for the actual i/o to
4147  * complete.  We wait by counting the number of dirty pages and buffers,
4148  * pushing them out using bio_busy() and page_busy(), and then counting again.
4149  * This routine is used during both the uadmin A_SHUTDOWN code as well as
4150  * the SYNC phase of the panic code (see comments in panic.c).  It should only
4151  * be used after some higher-level mechanism has quiesced the system so that
4152  * new writes are not being initiated while we are waiting for completion.
4153  *
4154  * To ensure finite running time, our algorithm uses two timeout mechanisms:
4155  * sync_timeleft (a timer implemented by the omnipresent deadman() cyclic), and
4156  * sync_triesleft (a progress counter used by the vfs_syncall() loop below).
4157  * Together these ensure that syncing completes if our i/o paths are stuck.
4158  * The counters are declared above so they can be found easily in the debugger.
4159  *
4160  * The sync_timeleft counter is reset by bio_busy() and page_busy() using the
4161  * vfs_syncprogress() subroutine whenever we make progress through the lists of
4162  * pages and buffers.  It is decremented and expired by the deadman() cyclic.
4163  * When vfs_syncall() decides it is done, we disable the deadman() counter by
4164  * setting sync_timeleft to zero.  This timer guards against vfs_syncall()
4165  * deadlocking or hanging inside of a broken filesystem or driver routine.
4166  *
4167  * The sync_triesleft counter is updated by vfs_syncall() itself.  If we make
4168  * sync_retries consecutive calls to bio_busy() and page_busy() without
4169  * decreasing either the number of dirty buffers or dirty pages below the
4170  * lowest count we have seen so far, we give up and return from vfs_syncall().
4171  *
4172  * Each loop iteration ends with a call to delay() one second to allow time for
4173  * i/o completion and to permit the user time to read our progress messages.
4174  */
4175 void
4176 vfs_syncall(void)
4177 {
4178 	if (rootdir == NULL && !modrootloaded)
4179 		return; /* panic during boot - no filesystems yet */
4180 
4181 	printf("syncing file systems...");
4182 	vfs_syncprogress();
4183 	sync();
4184 
4185 	vfs_syncprogress();
4186 	sync_triesleft = sync_retries;
4187 
4188 	old_bufcnt = new_bufcnt = INT_MAX;
4189 	old_pgcnt = new_pgcnt = ULONG_MAX;
4190 
4191 	while (sync_triesleft > 0) {
4192 		old_bufcnt = MIN(old_bufcnt, new_bufcnt);
4193 		old_pgcnt = MIN(old_pgcnt, new_pgcnt);
4194 
4195 		new_bufcnt = bio_busy(B_TRUE);
4196 		new_pgcnt = page_busy(B_TRUE);
4197 		vfs_syncprogress();
4198 
4199 		if (new_bufcnt == 0 && new_pgcnt == 0)
4200 			break;
4201 
4202 		if (new_bufcnt < old_bufcnt || new_pgcnt < old_pgcnt)
4203 			sync_triesleft = sync_retries;
4204 		else
4205 			sync_triesleft--;
4206 
4207 		if (new_bufcnt)
4208 			printf(" [%d]", new_bufcnt);
4209 		if (new_pgcnt)
4210 			printf(" %lu", new_pgcnt);
4211 
4212 		delay(hz);
4213 	}
4214 
4215 	if (new_bufcnt != 0 || new_pgcnt != 0)
4216 		printf(" done (not all i/o completed)\n");
4217 	else
4218 		printf(" done\n");
4219 
4220 	sync_timeleft = 0;
4221 	delay(hz);
4222 }
4223 
4224 /*
4225  * If we are in the middle of the sync phase of panic, reset sync_timeleft to
4226  * sync_timeout to indicate that we are making progress and the deadman()
4227  * omnipresent cyclic should not yet time us out.  Note that it is safe to
4228  * store to sync_timeleft here since the deadman() is firing at high-level
4229  * on top of us.  If we are racing with the deadman(), either the deadman()
4230  * will decrement the old value and then we will reset it, or we will
4231  * reset it and then the deadman() will immediately decrement it.  In either
4232  * case, correct behavior results.
4233  */
4234 void
4235 vfs_syncprogress(void)
4236 {
4237 	if (panicstr)
4238 		sync_timeleft = sync_timeout;
4239 }
4240 
4241 /*
4242  * Map VFS flags to statvfs flags.  These shouldn't really be separate
4243  * flags at all.
4244  */
4245 uint_t
4246 vf_to_stf(uint_t vf)
4247 {
4248 	uint_t stf = 0;
4249 
4250 	if (vf & VFS_RDONLY)
4251 		stf |= ST_RDONLY;
4252 	if (vf & VFS_NOSETUID)
4253 		stf |= ST_NOSUID;
4254 	if (vf & VFS_NOTRUNC)
4255 		stf |= ST_NOTRUNC;
4256 
4257 	return (stf);
4258 }
4259 
4260 /*
4261  * Entries for (illegal) fstype 0.
4262  */
4263 /* ARGSUSED */
4264 int
4265 vfsstray_sync(struct vfs *vfsp, short arg, struct cred *cr)
4266 {
4267 	cmn_err(CE_PANIC, "stray vfs operation");
4268 	return (0);
4269 }
4270 
4271 /*
4272  * Entries for (illegal) fstype 0.
4273  */
4274 int
4275 vfsstray(void)
4276 {
4277 	cmn_err(CE_PANIC, "stray vfs operation");
4278 	return (0);
4279 }
4280 
4281 /*
4282  * Support for dealing with forced UFS unmount and its interaction with
4283  * LOFS. Could be used by any filesystem.
4284  * See bug 1203132.
4285  */
4286 int
4287 vfs_EIO(void)
4288 {
4289 	return (EIO);
4290 }
4291 
4292 /*
4293  * We've gotta define the op for sync separately, since the compiler gets
4294  * confused if we mix and match ANSI and normal style prototypes when
4295  * a "short" argument is present and spits out a warning.
4296  */
4297 /*ARGSUSED*/
4298 int
4299 vfs_EIO_sync(struct vfs *vfsp, short arg, struct cred *cr)
4300 {
4301 	return (EIO);
4302 }
4303 
4304 vfs_t EIO_vfs;
4305 vfsops_t *EIO_vfsops;
4306 
4307 /*
4308  * Called from startup() to initialize all loaded vfs's
4309  */
4310 void
4311 vfsinit(void)
4312 {
4313 	struct vfssw *vswp;
4314 	int error;
4315 	extern int vopstats_enabled;
4316 	extern void vopstats_startup();
4317 
4318 	static const fs_operation_def_t EIO_vfsops_template[] = {
4319 		VFSNAME_MOUNT,		{ .error = vfs_EIO },
4320 		VFSNAME_UNMOUNT,	{ .error = vfs_EIO },
4321 		VFSNAME_ROOT,		{ .error = vfs_EIO },
4322 		VFSNAME_STATVFS,	{ .error = vfs_EIO },
4323 		VFSNAME_SYNC, 		{ .vfs_sync = vfs_EIO_sync },
4324 		VFSNAME_VGET,		{ .error = vfs_EIO },
4325 		VFSNAME_MOUNTROOT,	{ .error = vfs_EIO },
4326 		VFSNAME_FREEVFS,	{ .error = vfs_EIO },
4327 		VFSNAME_VNSTATE,	{ .error = vfs_EIO },
4328 		NULL, NULL
4329 	};
4330 
4331 	static const fs_operation_def_t stray_vfsops_template[] = {
4332 		VFSNAME_MOUNT,		{ .error = vfsstray },
4333 		VFSNAME_UNMOUNT,	{ .error = vfsstray },
4334 		VFSNAME_ROOT,		{ .error = vfsstray },
4335 		VFSNAME_STATVFS,	{ .error = vfsstray },
4336 		VFSNAME_SYNC, 		{ .vfs_sync = vfsstray_sync },
4337 		VFSNAME_VGET,		{ .error = vfsstray },
4338 		VFSNAME_MOUNTROOT,	{ .error = vfsstray },
4339 		VFSNAME_FREEVFS,	{ .error = vfsstray },
4340 		VFSNAME_VNSTATE,	{ .error = vfsstray },
4341 		NULL, NULL
4342 	};
4343 
4344 	/* Create vfs cache */
4345 	vfs_cache = kmem_cache_create("vfs_cache", sizeof (struct vfs),
4346 	    sizeof (uintptr_t), NULL, NULL, NULL, NULL, NULL, 0);
4347 
4348 	/* Initialize the vnode cache (file systems may use it during init). */
4349 	vn_create_cache();
4350 
4351 	/* Setup event monitor framework */
4352 	fem_init();
4353 
4354 	/* Initialize the dummy stray file system type. */
4355 	error = vfs_setfsops(0, stray_vfsops_template, NULL);
4356 
4357 	/* Initialize the dummy EIO file system. */
4358 	error = vfs_makefsops(EIO_vfsops_template, &EIO_vfsops);
4359 	if (error != 0) {
4360 		cmn_err(CE_WARN, "vfsinit: bad EIO vfs ops template");
4361 		/* Shouldn't happen, but not bad enough to panic */
4362 	}
4363 
4364 	VFS_INIT(&EIO_vfs, EIO_vfsops, (caddr_t)NULL);
4365 
4366 	/*
4367 	 * Default EIO_vfs.vfs_flag to VFS_UNMOUNTED so a lookup
4368 	 * on this vfs can immediately notice it's invalid.
4369 	 */
4370 	EIO_vfs.vfs_flag |= VFS_UNMOUNTED;
4371 
4372 	/*
4373 	 * Call the init routines of non-loadable filesystems only.
4374 	 * Filesystems which are loaded as separate modules will be
4375 	 * initialized by the module loading code instead.
4376 	 */
4377 
4378 	for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
4379 		RLOCK_VFSSW();
4380 		if (vswp->vsw_init != NULL)
4381 			(*vswp->vsw_init)(vswp - vfssw, vswp->vsw_name);
4382 		RUNLOCK_VFSSW();
4383 	}
4384 
4385 	vopstats_startup();
4386 
4387 	if (vopstats_enabled) {
4388 		/* EIO_vfs can collect stats, but we don't retrieve them */
4389 		initialize_vopstats(&EIO_vfs.vfs_vopstats);
4390 		EIO_vfs.vfs_fstypevsp = NULL;
4391 		EIO_vfs.vfs_vskap = NULL;
4392 		EIO_vfs.vfs_flag |= VFS_STATS;
4393 	}
4394 
4395 	xattr_init();
4396 
4397 	reparse_point_init();
4398 }
4399 
4400 vfs_t *
4401 vfs_alloc(int kmflag)
4402 {
4403 	vfs_t *vfsp;
4404 
4405 	vfsp = kmem_cache_alloc(vfs_cache, kmflag);
4406 
4407 	/*
4408 	 * Do the simplest initialization here.
4409 	 * Everything else gets done in vfs_init()
4410 	 */
4411 	bzero(vfsp, sizeof (vfs_t));
4412 	return (vfsp);
4413 }
4414 
4415 void
4416 vfs_free(vfs_t *vfsp)
4417 {
4418 	/*
4419 	 * One would be tempted to assert that "vfsp->vfs_count == 0".
4420 	 * The problem is that this gets called out of domount() with
4421 	 * a partially initialized vfs and a vfs_count of 1.  This is
4422 	 * also called from vfs_rele() with a vfs_count of 0.  We can't
4423 	 * call VFS_RELE() from domount() if VFS_MOUNT() hasn't successfully
4424 	 * returned.  This is because VFS_MOUNT() fully initializes the
4425 	 * vfs structure and its associated data.  VFS_RELE() will call
4426 	 * VFS_FREEVFS() which may panic the system if the data structures
4427 	 * aren't fully initialized from a successful VFS_MOUNT()).
4428 	 */
4429 
4430 	/* If FEM was in use, make sure everything gets cleaned up */
4431 	if (vfsp->vfs_femhead) {
4432 		ASSERT(vfsp->vfs_femhead->femh_list == NULL);
4433 		mutex_destroy(&vfsp->vfs_femhead->femh_lock);
4434 		kmem_free(vfsp->vfs_femhead, sizeof (*(vfsp->vfs_femhead)));
4435 		vfsp->vfs_femhead = NULL;
4436 	}
4437 
4438 	if (vfsp->vfs_implp)
4439 		vfsimpl_teardown(vfsp);
4440 	sema_destroy(&vfsp->vfs_reflock);
4441 	kmem_cache_free(vfs_cache, vfsp);
4442 }
4443 
4444 /*
4445  * Increments the vfs reference count by one atomically.
4446  */
4447 void
4448 vfs_hold(vfs_t *vfsp)
4449 {
4450 	atomic_inc_32(&vfsp->vfs_count);
4451 	ASSERT(vfsp->vfs_count != 0);
4452 }
4453 
4454 /*
4455  * Decrements the vfs reference count by one atomically. When
4456  * vfs reference count becomes zero, it calls the file system
4457  * specific vfs_freevfs() to free up the resources.
4458  */
4459 void
4460 vfs_rele(vfs_t *vfsp)
4461 {
4462 	ASSERT(vfsp->vfs_count != 0);
4463 	if (atomic_dec_32_nv(&vfsp->vfs_count) == 0) {
4464 		VFS_FREEVFS(vfsp);
4465 		lofi_remove(vfsp);
4466 		if (vfsp->vfs_zone)
4467 			zone_rele_ref(&vfsp->vfs_implp->vi_zone_ref,
4468 			    ZONE_REF_VFS);
4469 		vfs_freemnttab(vfsp);
4470 		vfs_free(vfsp);
4471 	}
4472 }
4473 
4474 /*
4475  * Generic operations vector support.
4476  *
4477  * This is used to build operations vectors for both the vfs and vnode.
4478  * It's normally called only when a file system is loaded.
4479  *
4480  * There are many possible algorithms for this, including the following:
4481  *
4482  *   (1) scan the list of known operations; for each, see if the file system
4483  *       includes an entry for it, and fill it in as appropriate.
4484  *
4485  *   (2) set up defaults for all known operations.  scan the list of ops
4486  *       supplied by the file system; for each which is both supplied and
4487  *       known, fill it in.
4488  *
4489  *   (3) sort the lists of known ops & supplied ops; scan the list, filling
4490  *       in entries as we go.
4491  *
4492  * we choose (1) for simplicity, and because performance isn't critical here.
4493  * note that (2) could be sped up using a precomputed hash table on known ops.
4494  * (3) could be faster than either, but only if the lists were very large or
4495  * supplied in sorted order.
4496  *
4497  */
4498 
4499 int
4500 fs_build_vector(void *vector, int *unused_ops,
4501     const fs_operation_trans_def_t *translation,
4502     const fs_operation_def_t *operations)
4503 {
4504 	int i, num_trans, num_ops, used;
4505 
4506 	/*
4507 	 * Count the number of translations and the number of supplied
4508 	 * operations.
4509 	 */
4510 
4511 	{
4512 		const fs_operation_trans_def_t *p;
4513 
4514 		for (num_trans = 0, p = translation;
4515 		    p->name != NULL;
4516 		    num_trans++, p++)
4517 			;
4518 	}
4519 
4520 	{
4521 		const fs_operation_def_t *p;
4522 
4523 		for (num_ops = 0, p = operations;
4524 		    p->name != NULL;
4525 		    num_ops++, p++)
4526 			;
4527 	}
4528 
4529 	/* Walk through each operation known to our caller.  There will be */
4530 	/* one entry in the supplied "translation table" for each. */
4531 
4532 	used = 0;
4533 
4534 	for (i = 0; i < num_trans; i++) {
4535 		int j, found;
4536 		char *curname;
4537 		fs_generic_func_p result;
4538 		fs_generic_func_p *location;
4539 
4540 		curname = translation[i].name;
4541 
4542 		/* Look for a matching operation in the list supplied by the */
4543 		/* file system. */
4544 
4545 		found = 0;
4546 
4547 		for (j = 0; j < num_ops; j++) {
4548 			if (strcmp(operations[j].name, curname) == 0) {
4549 				used++;
4550 				found = 1;
4551 				break;
4552 			}
4553 		}
4554 
4555 		/*
4556 		 * If the file system is using a "placeholder" for default
4557 		 * or error functions, grab the appropriate function out of
4558 		 * the translation table.  If the file system didn't supply
4559 		 * this operation at all, use the default function.
4560 		 */
4561 
4562 		if (found) {
4563 			result = operations[j].func.fs_generic;
4564 			if (result == fs_default) {
4565 				result = translation[i].defaultFunc;
4566 			} else if (result == fs_error) {
4567 				result = translation[i].errorFunc;
4568 			} else if (result == NULL) {
4569 				/* Null values are PROHIBITED */
4570 				return (EINVAL);
4571 			}
4572 		} else {
4573 			result = translation[i].defaultFunc;
4574 		}
4575 
4576 		/* Now store the function into the operations vector. */
4577 
4578 		location = (fs_generic_func_p *)
4579 		    (((char *)vector) + translation[i].offset);
4580 
4581 		*location = result;
4582 	}
4583 
4584 	*unused_ops = num_ops - used;
4585 
4586 	return (0);
4587 }
4588 
4589 /* Placeholder functions, should never be called. */
4590 
4591 int
4592 fs_error(void)
4593 {
4594 	cmn_err(CE_PANIC, "fs_error called");
4595 	return (0);
4596 }
4597 
4598 int
4599 fs_default(void)
4600 {
4601 	cmn_err(CE_PANIC, "fs_default called");
4602 	return (0);
4603 }
4604 
4605 #ifdef __sparc
4606 
4607 /*
4608  * Part of the implementation of booting off a mirrored root
4609  * involves a change of dev_t for the root device.  To
4610  * accomplish this, first remove the existing hash table
4611  * entry for the root device, convert to the new dev_t,
4612  * then re-insert in the hash table at the head of the list.
4613  */
4614 void
4615 vfs_root_redev(vfs_t *vfsp, dev_t ndev, int fstype)
4616 {
4617 	vfs_list_lock();
4618 
4619 	vfs_hash_remove(vfsp);
4620 
4621 	vfsp->vfs_dev = ndev;
4622 	vfs_make_fsid(&vfsp->vfs_fsid, ndev, fstype);
4623 
4624 	vfs_hash_add(vfsp, 1);
4625 
4626 	vfs_list_unlock();
4627 }
4628 
4629 #else /* x86 NEWBOOT */
4630 
4631 #if defined(__x86)
4632 extern int hvmboot_rootconf();
4633 #endif /* __x86 */
4634 
4635 extern ib_boot_prop_t *iscsiboot_prop;
4636 
4637 int
4638 rootconf()
4639 {
4640 	int error;
4641 	struct vfssw *vsw;
4642 	extern void pm_init();
4643 	char *fstyp, *fsmod;
4644 	int ret = -1;
4645 
4646 	getrootfs(&fstyp, &fsmod);
4647 
4648 #if defined(__x86)
4649 	/*
4650 	 * hvmboot_rootconf() is defined in the hvm_bootstrap misc module,
4651 	 * which lives in /platform/i86hvm, and hence is only available when
4652 	 * booted in an x86 hvm environment.  If the hvm_bootstrap misc module
4653 	 * is not available then the modstub for this function will return 0.
4654 	 * If the hvm_bootstrap misc module is available it will be loaded
4655 	 * and hvmboot_rootconf() will be invoked.
4656 	 */
4657 	if (error = hvmboot_rootconf())
4658 		return (error);
4659 #endif /* __x86 */
4660 
4661 	if (error = clboot_rootconf())
4662 		return (error);
4663 
4664 	if (modload("fs", fsmod) == -1)
4665 		panic("Cannot _init %s module", fsmod);
4666 
4667 	RLOCK_VFSSW();
4668 	vsw = vfs_getvfsswbyname(fstyp);
4669 	RUNLOCK_VFSSW();
4670 	if (vsw == NULL) {
4671 		cmn_err(CE_CONT, "Cannot find %s filesystem\n", fstyp);
4672 		return (ENXIO);
4673 	}
4674 	VFS_INIT(rootvfs, &vsw->vsw_vfsops, 0);
4675 	VFS_HOLD(rootvfs);
4676 
4677 	/* always mount readonly first */
4678 	rootvfs->vfs_flag |= VFS_RDONLY;
4679 
4680 	pm_init();
4681 
4682 	if (netboot && iscsiboot_prop) {
4683 		cmn_err(CE_WARN, "NFS boot and iSCSI boot"
4684 		    " shouldn't happen in the same time");
4685 		return (EINVAL);
4686 	}
4687 
4688 	if (netboot || iscsiboot_prop) {
4689 		ret = strplumb();
4690 		if (ret != 0) {
4691 			cmn_err(CE_WARN, "Cannot plumb network device %d", ret);
4692 			return (EFAULT);
4693 		}
4694 	}
4695 
4696 	if ((ret == 0) && iscsiboot_prop) {
4697 		ret = modload("drv", "iscsi");
4698 		/* -1 indicates fail */
4699 		if (ret == -1) {
4700 			cmn_err(CE_WARN, "Failed to load iscsi module");
4701 			iscsi_boot_prop_free();
4702 			return (EINVAL);
4703 		} else {
4704 			if (!i_ddi_attach_pseudo_node("iscsi")) {
4705 				cmn_err(CE_WARN,
4706 				    "Failed to attach iscsi driver");
4707 				iscsi_boot_prop_free();
4708 				return (ENODEV);
4709 			}
4710 		}
4711 	}
4712 
4713 	error = VFS_MOUNTROOT(rootvfs, ROOT_INIT);
4714 	vfs_unrefvfssw(vsw);
4715 	rootdev = rootvfs->vfs_dev;
4716 
4717 	if (error)
4718 		cmn_err(CE_CONT, "Cannot mount root on %s fstype %s\n",
4719 		    rootfs.bo_name, fstyp);
4720 	else
4721 		cmn_err(CE_CONT, "?root on %s fstype %s\n",
4722 		    rootfs.bo_name, fstyp);
4723 	return (error);
4724 }
4725 
4726 /*
4727  * XXX this is called by nfs only and should probably be removed
4728  * If booted with ASKNAME, prompt on the console for a filesystem
4729  * name and return it.
4730  */
4731 void
4732 getfsname(char *askfor, char *name, size_t namelen)
4733 {
4734 	if (boothowto & RB_ASKNAME) {
4735 		printf("%s name: ", askfor);
4736 		console_gets(name, namelen);
4737 	}
4738 }
4739 
4740 /*
4741  * Init the root filesystem type (rootfs.bo_fstype) from the "fstype"
4742  * property.
4743  *
4744  * Filesystem types starting with the prefix "nfs" are diskless clients;
4745  * init the root filename name (rootfs.bo_name), too.
4746  *
4747  * If we are booting via NFS we currently have these options:
4748  *	nfs -	dynamically choose NFS V2, V3, or V4 (default)
4749  *	nfs2 -	force NFS V2
4750  *	nfs3 -	force NFS V3
4751  *	nfs4 -	force NFS V4
4752  * Because we need to maintain backward compatibility with the naming
4753  * convention that the NFS V2 filesystem name is "nfs" (see vfs_conf.c)
4754  * we need to map "nfs" => "nfsdyn" and "nfs2" => "nfs".  The dynamic
4755  * nfs module will map the type back to either "nfs", "nfs3", or "nfs4".
4756  * This is only for root filesystems, all other uses will expect
4757  * that "nfs" == NFS V2.
4758  */
4759 static void
4760 getrootfs(char **fstypp, char **fsmodp)
4761 {
4762 	extern char *strplumb_get_netdev_path(void);
4763 	char *propstr = NULL;
4764 
4765 	/*
4766 	 * Check fstype property; for diskless it should be one of "nfs",
4767 	 * "nfs2", "nfs3" or "nfs4".
4768 	 */
4769 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4770 	    DDI_PROP_DONTPASS, "fstype", &propstr)
4771 	    == DDI_SUCCESS) {
4772 		(void) strncpy(rootfs.bo_fstype, propstr, BO_MAXFSNAME);
4773 		ddi_prop_free(propstr);
4774 
4775 	/*
4776 	 * if the boot property 'fstype' is not set, but 'zfs-bootfs' is set,
4777 	 * assume the type of this root filesystem is 'zfs'.
4778 	 */
4779 	} else if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4780 	    DDI_PROP_DONTPASS, "zfs-bootfs", &propstr)
4781 	    == DDI_SUCCESS) {
4782 		(void) strncpy(rootfs.bo_fstype, "zfs", BO_MAXFSNAME);
4783 		ddi_prop_free(propstr);
4784 	}
4785 
4786 	if (strncmp(rootfs.bo_fstype, "nfs", 3) != 0) {
4787 		*fstypp = *fsmodp = rootfs.bo_fstype;
4788 		return;
4789 	}
4790 
4791 	++netboot;
4792 
4793 	if (strcmp(rootfs.bo_fstype, "nfs2") == 0)
4794 		(void) strcpy(rootfs.bo_fstype, "nfs");
4795 	else if (strcmp(rootfs.bo_fstype, "nfs") == 0)
4796 		(void) strcpy(rootfs.bo_fstype, "nfsdyn");
4797 
4798 	/*
4799 	 * check if path to network interface is specified in bootpath
4800 	 * or by a hypervisor domain configuration file.
4801 	 * XXPV - enable strlumb_get_netdev_path()
4802 	 */
4803 	if (ddi_prop_exists(DDI_DEV_T_ANY, ddi_root_node(), DDI_PROP_DONTPASS,
4804 	    "xpv-nfsroot")) {
4805 		(void) strcpy(rootfs.bo_name, "/xpvd/xnf@0");
4806 	} else if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4807 	    DDI_PROP_DONTPASS, "bootpath", &propstr)
4808 	    == DDI_SUCCESS) {
4809 		(void) strncpy(rootfs.bo_name, propstr, BO_MAXOBJNAME);
4810 		ddi_prop_free(propstr);
4811 	} else {
4812 		/* attempt to determine netdev_path via boot_mac address */
4813 		netdev_path = strplumb_get_netdev_path();
4814 		if (netdev_path == NULL)
4815 			panic("cannot find boot network interface");
4816 		(void) strncpy(rootfs.bo_name, netdev_path, BO_MAXOBJNAME);
4817 	}
4818 	*fstypp = rootfs.bo_fstype;
4819 	*fsmodp = "nfs";
4820 }
4821 #endif
4822 
4823 /*
4824  * VFS feature routines
4825  */
4826 
4827 #define	VFTINDEX(feature)	(((feature) >> 32) & 0xFFFFFFFF)
4828 #define	VFTBITS(feature)	((feature) & 0xFFFFFFFFLL)
4829 
4830 /* Register a feature in the vfs */
4831 void
4832 vfs_set_feature(vfs_t *vfsp, vfs_feature_t feature)
4833 {
4834 	/* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4835 	if (vfsp->vfs_implp == NULL)
4836 		return;
4837 
4838 	vfsp->vfs_featureset[VFTINDEX(feature)] |= VFTBITS(feature);
4839 }
4840 
4841 void
4842 vfs_clear_feature(vfs_t *vfsp, vfs_feature_t feature)
4843 {
4844 	/* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4845 	if (vfsp->vfs_implp == NULL)
4846 		return;
4847 	vfsp->vfs_featureset[VFTINDEX(feature)] &= VFTBITS(~feature);
4848 }
4849 
4850 /*
4851  * Query a vfs for a feature.
4852  * Returns 1 if feature is present, 0 if not
4853  */
4854 int
4855 vfs_has_feature(vfs_t *vfsp, vfs_feature_t feature)
4856 {
4857 	int	ret = 0;
4858 
4859 	/* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4860 	if (vfsp->vfs_implp == NULL)
4861 		return (ret);
4862 
4863 	if (vfsp->vfs_featureset[VFTINDEX(feature)] & VFTBITS(feature))
4864 		ret = 1;
4865 
4866 	return (ret);
4867 }
4868 
4869 /*
4870  * Propagate feature set from one vfs to another
4871  */
4872 void
4873 vfs_propagate_features(vfs_t *from, vfs_t *to)
4874 {
4875 	int i;
4876 
4877 	if (to->vfs_implp == NULL || from->vfs_implp == NULL)
4878 		return;
4879 
4880 	for (i = 1; i <= to->vfs_featureset[0]; i++) {
4881 		to->vfs_featureset[i] = from->vfs_featureset[i];
4882 	}
4883 }
4884 
4885 #define	LOFINODE_PATH "/dev/lofi/%d"
4886 
4887 /*
4888  * Return the vnode for the lofi node if there's a lofi mount in place.
4889  * Returns -1 when there's no lofi node, 0 on success, and > 0 on
4890  * failure.
4891  */
4892 int
4893 vfs_get_lofi(vfs_t *vfsp, vnode_t **vpp)
4894 {
4895 	char *path = NULL;
4896 	int strsize;
4897 	int err;
4898 
4899 	if (vfsp->vfs_lofi_id == 0) {
4900 		*vpp = NULL;
4901 		return (-1);
4902 	}
4903 
4904 	strsize = snprintf(NULL, 0, LOFINODE_PATH, vfsp->vfs_lofi_id);
4905 	path = kmem_alloc(strsize + 1, KM_SLEEP);
4906 	(void) snprintf(path, strsize + 1, LOFINODE_PATH, vfsp->vfs_lofi_id);
4907 
4908 	/*
4909 	 * We may be inside a zone, so we need to use the /dev path, but
4910 	 * it's created asynchronously, so we wait here.
4911 	 */
4912 	for (;;) {
4913 		err = lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, vpp);
4914 
4915 		if (err != ENOENT)
4916 			break;
4917 
4918 		if ((err = delay_sig(hz / 8)) == EINTR)
4919 			break;
4920 	}
4921 
4922 	if (err)
4923 		*vpp = NULL;
4924 
4925 	kmem_free(path, strsize + 1);
4926 	return (err);
4927 }
4928