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