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