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