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