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