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