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