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