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