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