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