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