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