fs: invoke fem & fsem ops directly from vfs
[unleashed.git] / kernel / fs / vfs.c
blob7b7d512f6d5f322b160af64c270470bd1b8f64ec
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
25 * Copyright 2016 Toomas Soome <tsoome@me.com>
26 * Copyright 2016 Nexenta Systems, Inc.
27 * Copyright (c) 2016 by Delphix. All rights reserved.
28 * Copyright 2016 Nexenta Systems, Inc.
29 * Copyright 2017 RackTop Systems.
32 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
33 /* All Rights Reserved */
36 * University Copyright- Copyright (c) 1982, 1986, 1988
37 * The Regents of the University of California
38 * All Rights Reserved
40 * University Acknowledgment- Portions of this document are derived from
41 * software developed by the University of California, Berkeley, and its
42 * contributors.
45 #include <sys/types.h>
46 #include <sys/t_lock.h>
47 #include <sys/param.h>
48 #include <sys/errno.h>
49 #include <sys/user.h>
50 #include <sys/fstyp.h>
51 #include <sys/kmem.h>
52 #include <sys/systm.h>
53 #include <sys/proc.h>
54 #include <sys/mount.h>
55 #include <sys/vfs.h>
56 #include <sys/vfs_dispatch.h>
57 #include <sys/vfs_opreg.h>
58 #include <sys/fem.h>
59 #include <sys/mntent.h>
60 #include <sys/stat.h>
61 #include <sys/statvfs.h>
62 #include <sys/statfs.h>
63 #include <sys/cred.h>
64 #include <sys/vnode.h>
65 #include <sys/rwstlock.h>
66 #include <sys/dnlc.h>
67 #include <sys/file.h>
68 #include <sys/time.h>
69 #include <sys/atomic.h>
70 #include <sys/cmn_err.h>
71 #include <sys/buf.h>
72 #include <sys/swap.h>
73 #include <sys/debug.h>
74 #include <sys/vnode.h>
75 #include <sys/modctl.h>
76 #include <sys/ddi.h>
77 #include <sys/pathname.h>
78 #include <sys/bootconf.h>
79 #include <sys/dumphdr.h>
80 #include <sys/poll.h>
81 #include <sys/sunddi.h>
82 #include <sys/sysmacros.h>
83 #include <sys/zone.h>
84 #include <sys/policy.h>
85 #include <sys/ctfs.h>
86 #include <sys/objfs.h>
87 #include <sys/console.h>
88 #include <sys/reboot.h>
89 #include <sys/attr.h>
90 #include <sys/zio.h>
91 #include <sys/spa.h>
92 #include <sys/lofi.h>
93 #include <sys/bootprops.h>
95 #include <vm/page.h>
97 #include <sys/fs_subr.h>
98 /* Private interfaces to create vopstats-related data structures */
99 extern void initialize_vopstats(vopstats_t *);
100 extern vopstats_t *get_fstype_vopstats(struct vfs *, struct vfssw *);
101 extern vsk_anchor_t *get_vskstat_anchor(struct vfs *);
103 static void vfs_clearmntopt_nolock(mntopts_t *, const char *, int);
104 static void vfs_setmntopt_nolock(mntopts_t *, const char *,
105 const char *, int, int);
106 static int vfs_optionisset_nolock(const mntopts_t *, const char *, char **);
107 static void vfs_freemnttab(struct vfs *);
108 static void vfs_freeopt(mntopt_t *);
109 static void vfs_swapopttbl_nolock(mntopts_t *, mntopts_t *);
110 static void vfs_swapopttbl(mntopts_t *, mntopts_t *);
111 static void vfs_copyopttbl_extend(const mntopts_t *, mntopts_t *, int);
112 static void vfs_createopttbl_extend(mntopts_t *, const char *,
113 const mntopts_t *);
114 static char **vfs_copycancelopt_extend(char **const, int);
115 static void vfs_freecancelopt(char **);
116 static void getrootfs(char **, char **);
117 static int getmacpath(dev_info_t *, void *);
118 static void vfs_mnttabvp_setup(void);
120 struct ipmnt {
121 struct ipmnt *mip_next;
122 dev_t mip_dev;
123 struct vfs *mip_vfsp;
126 static kmutex_t vfs_miplist_mutex;
127 static struct ipmnt *vfs_miplist = NULL;
128 static struct ipmnt *vfs_miplist_end = NULL;
130 static kmem_cache_t *vfs_cache; /* Pointer to VFS kmem cache */
133 * VFS global data.
135 vnode_t *rootdir; /* pointer to root inode vnode. */
136 vnode_t *devicesdir; /* pointer to inode of devices root */
137 vnode_t *devdir; /* pointer to inode of dev root */
139 char *server_rootpath; /* root path for diskless clients */
140 char *server_hostname; /* hostname of diskless server */
142 static struct vfs root;
143 static struct vfs devices;
144 static struct vfs dev;
145 struct vfs *rootvfs = &root; /* pointer to root vfs; head of VFS list. */
146 rvfs_t *rvfs_list; /* array of vfs ptrs for vfs hash list */
147 int vfshsz = 512; /* # of heads/locks in vfs hash arrays */
148 /* must be power of 2! */
149 timespec_t vfs_mnttab_ctime; /* mnttab created time */
150 timespec_t vfs_mnttab_mtime; /* mnttab last modified time */
151 char *vfs_dummyfstype = "\0";
152 struct pollhead vfs_pollhd; /* for mnttab pollers */
153 struct vnode *vfs_mntdummyvp; /* to fake mnttab read/write for file events */
154 int mntfstype; /* will be set once mnt fs is mounted */
157 * Table for generic options recognized in the VFS layer and acted
158 * on at this level before parsing file system specific options.
159 * The nosuid option is stronger than any of the devices and setuid
160 * options, so those are canceled when nosuid is seen.
162 * All options which are added here need to be added to the
163 * list of standard options in usr/src/cmd/fs.d/fslib.c as well.
166 * VFS Mount options table
168 static char *ro_cancel[] = { MNTOPT_RW, NULL };
169 static char *rw_cancel[] = { MNTOPT_RO, NULL };
170 static char *suid_cancel[] = { MNTOPT_NOSUID, NULL };
171 static char *nosuid_cancel[] = { MNTOPT_SUID, MNTOPT_DEVICES, MNTOPT_NODEVICES,
172 MNTOPT_NOSETUID, MNTOPT_SETUID, NULL };
173 static char *devices_cancel[] = { MNTOPT_NODEVICES, NULL };
174 static char *nodevices_cancel[] = { MNTOPT_DEVICES, NULL };
175 static char *setuid_cancel[] = { MNTOPT_NOSETUID, NULL };
176 static char *nosetuid_cancel[] = { MNTOPT_SETUID, NULL };
177 static char *nbmand_cancel[] = { MNTOPT_NONBMAND, NULL };
178 static char *nonbmand_cancel[] = { MNTOPT_NBMAND, NULL };
179 static char *exec_cancel[] = { MNTOPT_NOEXEC, NULL };
180 static char *noexec_cancel[] = { MNTOPT_EXEC, NULL };
182 static const mntopt_t mntopts[] = {
184 * option name cancel options default arg flags
186 { MNTOPT_REMOUNT, NULL, NULL,
187 MO_NODISPLAY, NULL },
188 { MNTOPT_RO, ro_cancel, NULL, 0,
189 NULL },
190 { MNTOPT_RW, rw_cancel, NULL, 0,
191 NULL },
192 { MNTOPT_SUID, suid_cancel, NULL, 0,
193 NULL },
194 { MNTOPT_NOSUID, nosuid_cancel, NULL, 0,
195 NULL },
196 { MNTOPT_DEVICES, devices_cancel, NULL, 0,
197 NULL },
198 { MNTOPT_NODEVICES, nodevices_cancel, NULL, 0,
199 NULL },
200 { MNTOPT_SETUID, setuid_cancel, NULL, 0,
201 NULL },
202 { MNTOPT_NOSETUID, nosetuid_cancel, NULL, 0,
203 NULL },
204 { MNTOPT_NBMAND, nbmand_cancel, NULL, 0,
205 NULL },
206 { MNTOPT_NONBMAND, nonbmand_cancel, NULL, 0,
207 NULL },
208 { MNTOPT_EXEC, exec_cancel, NULL, 0,
209 NULL },
210 { MNTOPT_NOEXEC, noexec_cancel, NULL, 0,
211 NULL },
214 const mntopts_t vfs_mntopts = {
215 sizeof (mntopts) / sizeof (mntopt_t),
216 (mntopt_t *)&mntopts[0]
220 * File system operation dispatch functions.
224 fsop_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
226 return fsop_mount_dispatch(vfsp, mvp, uap, cr, true);
230 fsop_unmount(vfs_t *vfsp, int flag, cred_t *cr)
232 return fsop_unmount_dispatch(vfsp, flag, cr, true);
236 fsop_root(vfs_t *vfsp, vnode_t **vpp)
238 refstr_t *mntpt;
239 int ret;
241 ret = fsop_root_dispatch(vfsp, vpp, true);
244 * Make sure this root has a path. With lofs, it is possible to have
245 * a NULL mountpoint.
247 if (ret == 0 && vfsp->vfs_mntpt != NULL && (*vpp)->v_path == NULL) {
248 mntpt = vfs_getmntpoint(vfsp);
249 vn_setpath_str(*vpp, refstr_value(mntpt),
250 strlen(refstr_value(mntpt)));
251 refstr_rele(mntpt);
254 return (ret);
258 fsop_statfs(vfs_t *vfsp, statvfs64_t *sp)
260 return fsop_statfs_dispatch(vfsp, sp, true);
264 fsop_sync(vfs_t *vfsp, short flag, cred_t *cr)
266 return fsop_sync_dispatch(vfsp, flag, cr, true);
270 fsop_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
273 * In order to handle system attribute fids in a manner
274 * transparent to the underlying fs, we embed the fid for
275 * the sysattr parent object in the sysattr fid and tack on
276 * some extra bytes that only the sysattr layer knows about.
278 * This guarantees that sysattr fids are larger than other fids
279 * for this vfs. If the vfs supports the sysattr view interface
280 * (as indicated by VFSFT_SYSATTR_VIEWS), we cannot have a size
281 * collision with XATTR_FIDSZ.
283 if (vfs_has_feature(vfsp, VFSFT_SYSATTR_VIEWS) &&
284 fidp->fid_len == XATTR_FIDSZ)
285 return (xattr_dir_vget(vfsp, vpp, fidp));
287 return fsop_vget_dispatch(vfsp, vpp, fidp, true);
291 fsop_mountroot(vfs_t *vfsp, enum whymountroot reason)
293 return fsop_mountroot_dispatch(vfsp, reason, true);
296 void
297 fsop_freefs(vfs_t *vfsp)
299 fsop_freefs_dispatch(vfsp, true);
303 fsop_vnstate(vfs_t *vfsp, vnode_t *vp, vntrans_t nstate)
305 return fsop_vnstate_dispatch(vfsp, vp, nstate, true);
309 fsop_sync_by_kind(int fstype, short flag, cred_t *cr)
311 ASSERT((fstype >= 0) && (fstype < nfstype));
313 if (ALLOCATED_VFSSW(&vfssw[fstype]) && VFS_INSTALLED(&vfssw[fstype]))
314 return (*vfssw[fstype].vsw_vfsops.vfs_sync) (NULL, flag, cr);
315 else
316 return (ENOTSUP);
320 * File system initialization. vfs_setfsops() must be called from a file
321 * system's init routine.
324 static int
325 fs_copyfsops(const fs_operation_def_t *template, vfsops_t *actual,
326 int *unused_ops)
328 static const fs_operation_trans_def_t vfs_ops_table[] = {
329 VFSNAME_MOUNT, offsetof(vfsops_t, vfs_mount),
330 fs_nosys,
332 VFSNAME_UNMOUNT, offsetof(vfsops_t, vfs_unmount),
333 fs_nosys,
335 VFSNAME_ROOT, offsetof(vfsops_t, vfs_root),
336 fs_nosys,
338 VFSNAME_STATVFS, offsetof(vfsops_t, vfs_statvfs),
339 fs_nosys,
341 VFSNAME_SYNC, offsetof(vfsops_t, vfs_sync),
342 (fs_generic_func_p) fs_sync,
344 VFSNAME_VGET, offsetof(vfsops_t, vfs_vget),
345 fs_nosys,
347 VFSNAME_MOUNTROOT, offsetof(vfsops_t, vfs_mountroot),
348 fs_nosys,
350 VFSNAME_FREEVFS, offsetof(vfsops_t, vfs_freevfs),
351 (fs_generic_func_p)fs_freevfs,
353 VFSNAME_VNSTATE, offsetof(vfsops_t, vfs_vnstate),
354 (fs_generic_func_p)fs_nosys,
356 NULL, 0, NULL,
359 return (fs_build_vector(actual, unused_ops, vfs_ops_table, template));
362 void
363 zfs_boot_init(void)
365 if (strcmp(rootfs.bo_fstype, MNTTYPE_ZFS) == 0)
366 spa_boot_init();
370 vfs_setfsops(int fstype, const fs_operation_def_t *template, vfsops_t **actual)
372 int error;
373 int unused_ops;
376 * Verify that fstype refers to a valid fs. Note that
377 * 0 is valid since it's used to set "stray" ops.
379 if ((fstype < 0) || (fstype >= nfstype))
380 return (EINVAL);
382 if (!ALLOCATED_VFSSW(&vfssw[fstype]))
383 return (EINVAL);
385 /* Set up the operations vector. */
387 error = fs_copyfsops(template, &vfssw[fstype].vsw_vfsops, &unused_ops);
389 if (error != 0)
390 return (error);
392 vfssw[fstype].vsw_flag |= VSW_INSTALLED;
394 if (actual != NULL)
395 *actual = &vfssw[fstype].vsw_vfsops;
397 #if DEBUG
398 if (unused_ops != 0)
399 cmn_err(CE_WARN, "vfs_setfsops: %s: %d operations supplied "
400 "but not used", vfssw[fstype].vsw_name, unused_ops);
401 #endif
403 return (0);
407 vfs_makefsops(const fs_operation_def_t *template, vfsops_t **actual)
409 int error;
410 int unused_ops;
412 *actual = (vfsops_t *)kmem_alloc(sizeof (vfsops_t), KM_SLEEP);
414 error = fs_copyfsops(template, *actual, &unused_ops);
415 if (error != 0) {
416 kmem_free(*actual, sizeof (vfsops_t));
417 *actual = NULL;
418 return (error);
421 return (0);
425 * Free a vfsops structure created as a result of vfs_makefsops().
426 * NOTE: For a vfsops structure initialized by vfs_setfsops(), use
427 * vfs_freevfsops_by_type().
429 void
430 vfs_freevfsops(vfsops_t *vfsops)
432 kmem_free(vfsops, sizeof (vfsops_t));
436 * Since the vfsops structure is part of the vfssw table and wasn't
437 * really allocated, we're not really freeing anything. We keep
438 * the name for consistency with vfs_freevfsops(). We do, however,
439 * need to take care of a little bookkeeping.
440 * NOTE: For a vfsops structure created by vfs_setfsops(), use
441 * vfs_freevfsops_by_type().
444 vfs_freevfsops_by_type(int fstype)
447 /* Verify that fstype refers to a loaded fs (and not fsid 0). */
448 if ((fstype <= 0) || (fstype >= nfstype))
449 return (EINVAL);
451 WLOCK_VFSSW();
452 if ((vfssw[fstype].vsw_flag & VSW_INSTALLED) == 0) {
453 WUNLOCK_VFSSW();
454 return (EINVAL);
457 vfssw[fstype].vsw_flag &= ~VSW_INSTALLED;
458 WUNLOCK_VFSSW();
460 return (0);
463 /* Support routines used to reference vfs_op */
465 /* Set the operations vector for a vfs */
466 void
467 vfs_setops(struct vfs *vfs, struct vfsops *ops)
469 vfs->vfs_op = ops;
472 /* Retrieve the operations vector for a vfs */
473 struct vfsops *
474 vfs_getops(struct vfs *vfs)
476 return vfs->vfs_op;
480 * Returns non-zero (1) if the vfsops matches that of the vfs.
481 * Returns zero (0) if not.
484 vfs_matchops(vfs_t *vfsp, vfsops_t *vfsops)
486 return (vfs_getops(vfsp) == vfsops);
490 * Returns non-zero (1) if the file system has installed a non-default,
491 * non-error vfs_sync routine. Returns zero (0) otherwise.
494 vfs_can_sync(vfs_t *vfsp)
496 /* vfs_sync() routine is not the default/error function */
497 return (vfs_getops(vfsp)->vfs_sync != fs_sync);
501 * Initialize a vfs structure.
503 void
504 vfs_init(vfs_t *vfsp, vfsops_t *op, void *data)
506 /* Other initialization has been moved to vfs_alloc() */
507 vfsp->vfs_count = 0;
508 vfsp->vfs_next = vfsp;
509 vfsp->vfs_prev = vfsp;
510 vfsp->vfs_zone_next = vfsp;
511 vfsp->vfs_zone_prev = vfsp;
512 vfsp->vfs_lofi_id = 0;
513 sema_init(&vfsp->vfs_reflock, 1, NULL, SEMA_DEFAULT, NULL);
514 vfsimpl_setup(vfsp);
515 vfsp->vfs_data = (data);
516 vfs_setops((vfsp), (op));
520 * Allocate and initialize the vfs implementation private data
521 * structure, vfs_impl_t.
523 void
524 vfsimpl_setup(vfs_t *vfsp)
526 int i;
528 if (vfsp->vfs_implp != NULL) {
529 return;
532 vfsp->vfs_implp = kmem_alloc(sizeof (vfs_impl_t), KM_SLEEP);
533 /* Note that these are #define'd in vfs.h */
534 vfsp->vfs_vskap = NULL;
535 vfsp->vfs_fstypevsp = NULL;
537 /* Set size of counted array, then zero the array */
538 vfsp->vfs_featureset[0] = VFS_FEATURE_MAXSZ - 1;
539 for (i = 1; i < VFS_FEATURE_MAXSZ; i++) {
540 vfsp->vfs_featureset[i] = 0;
545 * Release the vfs_impl_t structure, if it exists. Some unbundled
546 * filesystems may not use the newer version of vfs and thus
547 * would not contain this implementation private data structure.
549 void
550 vfsimpl_teardown(vfs_t *vfsp)
552 vfs_impl_t *vip = vfsp->vfs_implp;
554 if (vip == NULL)
555 return;
557 kmem_free(vfsp->vfs_implp, sizeof (vfs_impl_t));
558 vfsp->vfs_implp = NULL;
562 * VFS system calls: mount, umount, syssync, statfs, fstatfs, statvfs,
563 * fstatvfs, and sysfs are in kernel/syscall.
567 * Update every mounted file system. We call the vfs_sync operation of
568 * each file system type, passing it a NULL vfsp to indicate that all
569 * mounted file systems of that type should be updated.
571 void
572 vfs_sync(int flag)
574 struct vfssw *vswp;
575 RLOCK_VFSSW();
576 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
577 if (ALLOCATED_VFSSW(vswp) && VFS_INSTALLED(vswp)) {
578 vfs_refvfssw(vswp);
579 RUNLOCK_VFSSW();
580 (void) (*vswp->vsw_vfsops.vfs_sync)(NULL, flag,
581 CRED());
582 vfs_unrefvfssw(vswp);
583 RLOCK_VFSSW();
586 RUNLOCK_VFSSW();
589 void
590 sync(void)
592 vfs_sync(0);
596 * External routines.
599 krwlock_t vfssw_lock; /* lock accesses to vfssw */
602 * Lock for accessing the vfs linked list. Initialized in vfs_mountroot(),
603 * but otherwise should be accessed only via vfs_list_lock() and
604 * vfs_list_unlock(). Also used to protect the timestamp for mods to the list.
606 static krwlock_t vfslist;
609 * Mount devfs on /devices. This is done right after root is mounted
610 * to provide device access support for the system
612 static void
613 vfs_mountdevices(void)
615 struct vfssw *vsw;
616 struct vnode *mvp;
617 struct mounta mounta = { /* fake mounta for devfs_mount() */
618 NULL,
619 NULL,
620 MS_SYSSPACE,
621 NULL,
622 NULL,
624 NULL,
629 * _init devfs module to fill in the vfssw
631 if (modload("fs", "devfs") == -1)
632 panic("Cannot _init devfs module");
635 * Hold vfs
637 RLOCK_VFSSW();
638 vsw = vfs_getvfsswbyname("devfs");
639 VFS_INIT(&devices, &vsw->vsw_vfsops, NULL);
640 VFS_HOLD(&devices);
643 * Locate mount point
645 if (lookupname("/devices", UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp))
646 panic("Cannot find /devices");
649 * Perform the mount of /devices
651 if (VFS_MOUNT(&devices, mvp, &mounta, CRED()))
652 panic("Cannot mount /devices");
654 RUNLOCK_VFSSW();
657 * Set appropriate members and add to vfs list for mnttab display
659 vfs_setresource(&devices, "/devices", 0);
660 vfs_setmntpoint(&devices, "/devices", 0);
663 * Hold the root of /devices so it won't go away
665 if (VFS_ROOT(&devices, &devicesdir))
666 panic("vfs_mountdevices: not devices root");
668 if (vfs_lock(&devices) != 0) {
669 VN_RELE(devicesdir);
670 cmn_err(CE_NOTE, "Cannot acquire vfs_lock of /devices");
671 return;
674 if (vn_vfswlock(mvp) != 0) {
675 vfs_unlock(&devices);
676 VN_RELE(devicesdir);
677 cmn_err(CE_NOTE, "Cannot acquire vfswlock of /devices");
678 return;
681 vfs_add(mvp, &devices, 0);
682 vn_vfsunlock(mvp);
683 vfs_unlock(&devices);
684 VN_RELE(devicesdir);
688 * mount the first instance of /dev to root and remain mounted
690 static void
691 vfs_mountdev1(void)
693 struct vfssw *vsw;
694 struct vnode *mvp;
695 struct mounta mounta = { /* fake mounta for sdev_mount() */
696 NULL,
697 NULL,
698 MS_SYSSPACE | MS_OVERLAY,
699 NULL,
700 NULL,
702 NULL,
707 * _init dev module to fill in the vfssw
709 if (modload("fs", "dev") == -1)
710 cmn_err(CE_PANIC, "Cannot _init dev module\n");
713 * Hold vfs
715 RLOCK_VFSSW();
716 vsw = vfs_getvfsswbyname("dev");
717 VFS_INIT(&dev, &vsw->vsw_vfsops, NULL);
718 VFS_HOLD(&dev);
721 * Locate mount point
723 if (lookupname("/dev", UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp))
724 cmn_err(CE_PANIC, "Cannot find /dev\n");
727 * Perform the mount of /dev
729 if (VFS_MOUNT(&dev, mvp, &mounta, CRED()))
730 cmn_err(CE_PANIC, "Cannot mount /dev 1\n");
732 RUNLOCK_VFSSW();
735 * Set appropriate members and add to vfs list for mnttab display
737 vfs_setresource(&dev, "/dev", 0);
738 vfs_setmntpoint(&dev, "/dev", 0);
741 * Hold the root of /dev so it won't go away
743 if (VFS_ROOT(&dev, &devdir))
744 cmn_err(CE_PANIC, "vfs_mountdev1: not dev root");
746 if (vfs_lock(&dev) != 0) {
747 VN_RELE(devdir);
748 cmn_err(CE_NOTE, "Cannot acquire vfs_lock of /dev");
749 return;
752 if (vn_vfswlock(mvp) != 0) {
753 vfs_unlock(&dev);
754 VN_RELE(devdir);
755 cmn_err(CE_NOTE, "Cannot acquire vfswlock of /dev");
756 return;
759 vfs_add(mvp, &dev, 0);
760 vn_vfsunlock(mvp);
761 vfs_unlock(&dev);
762 VN_RELE(devdir);
766 * Mount required filesystem. This is done right after root is mounted.
768 static void
769 vfs_mountfs(char *module, char *spec, char *path)
771 struct vnode *mvp;
772 struct mounta mounta;
773 vfs_t *vfsp;
775 bzero(&mounta, sizeof (mounta));
776 mounta.flags = MS_SYSSPACE | MS_DATA;
777 mounta.fstype = module;
778 mounta.spec = spec;
779 mounta.dir = path;
780 if (lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp)) {
781 cmn_err(CE_WARN, "Cannot find %s", path);
782 return;
784 if (domount(NULL, &mounta, mvp, CRED(), &vfsp))
785 cmn_err(CE_WARN, "Cannot mount %s", path);
786 else
787 VFS_RELE(vfsp);
788 VN_RELE(mvp);
792 * vfs_mountroot is called by main() to mount the root filesystem.
794 void
795 vfs_mountroot(void)
797 struct vnode *rvp = NULL;
798 char *path;
799 size_t plen;
800 struct vfssw *vswp;
801 proc_t *p;
803 rw_init(&vfssw_lock, NULL, RW_DEFAULT, NULL);
804 rw_init(&vfslist, NULL, RW_DEFAULT, NULL);
807 * Alloc the vfs hash bucket array and locks
809 rvfs_list = kmem_zalloc(vfshsz * sizeof (rvfs_t), KM_SLEEP);
812 * Call machine-dependent routine "rootconf" to choose a root
813 * file system type.
815 if (rootconf())
816 panic("vfs_mountroot: cannot mount root");
818 * Get vnode for '/'. Set up rootdir, u.u_rdir and u.u_cdir
819 * to point to it. These are used by lookuppn() so that it
820 * knows where to start from ('/' or '.').
822 vfs_setmntpoint(rootvfs, "/", 0);
823 if (VFS_ROOT(rootvfs, &rootdir))
824 panic("vfs_mountroot: no root vnode");
827 * At this point, the process tree consists of p0 and possibly some
828 * direct children of p0. (i.e. there are no grandchildren)
830 * Walk through them all, setting their current directory.
832 mutex_enter(&pidlock);
833 for (p = practive; p != NULL; p = p->p_next) {
834 ASSERT(p == &p0 || p->p_parent == &p0);
836 PTOU(p)->u_cdir = rootdir;
837 VN_HOLD(PTOU(p)->u_cdir);
838 PTOU(p)->u_rdir = NULL;
840 mutex_exit(&pidlock);
843 * Setup the global zone's rootvp, now that it exists.
845 global_zone->zone_rootvp = rootdir;
846 VN_HOLD(global_zone->zone_rootvp);
849 * Notify the module code that it can begin using the
850 * root filesystem instead of the boot program's services.
852 modrootloaded = 1;
855 * Special handling for a ZFS root file system.
857 zfs_boot_init();
860 * Set up mnttab information for root
862 vfs_setresource(rootvfs, rootfs.bo_name, 0);
864 /* Now that we're all done with the root FS, set up its vopstats */
865 if ((vswp = vfs_getvfsswbyvfsops(vfs_getops(rootvfs))) != NULL) {
866 /* Set flag for statistics collection */
867 if (vswp->vsw_flag & VSW_STATS) {
868 initialize_vopstats(&rootvfs->vfs_vopstats);
869 rootvfs->vfs_flag |= VFS_STATS;
870 rootvfs->vfs_fstypevsp =
871 get_fstype_vopstats(rootvfs, vswp);
872 rootvfs->vfs_vskap = get_vskstat_anchor(rootvfs);
874 vfs_unrefvfssw(vswp);
878 * Mount /devices, /dev instance 1, /system/contract, /etc/mnttab,
879 * /etc/svc/volatile, /etc/dfs/sharetab, /system/object, and /proc.
881 vfs_mountdevices();
882 vfs_mountdev1();
884 vfs_mountfs("ctfs", "ctfs", CTFS_ROOT);
885 vfs_mountfs("proc", "/proc", "/proc");
886 vfs_mountfs("mntfs", "/etc/mnttab", "/etc/mnttab");
887 vfs_mountfs("tmpfs", "/etc/svc/volatile", "/etc/svc/volatile");
888 vfs_mountfs("objfs", "objfs", OBJFS_ROOT);
889 vfs_mountfs("bootfs", "bootfs", "/system/boot");
891 if (getzoneid() == GLOBAL_ZONEID) {
892 vfs_mountfs("sharefs", "sharefs", "/etc/dfs/sharetab");
895 if (strcmp(rootfs.bo_fstype, "zfs") != 0) {
897 * Look up the root device via devfs so that a dv_node is
898 * created for it. The vnode is never VN_RELE()ed.
899 * We allocate more than MAXPATHLEN so that the
900 * buffer passed to i_ddi_prompath_to_devfspath() is
901 * exactly MAXPATHLEN (the function expects a buffer
902 * of that length).
904 plen = strlen("/devices");
905 path = kmem_alloc(plen + MAXPATHLEN, KM_SLEEP);
906 (void) strcpy(path, "/devices");
908 if (i_ddi_prompath_to_devfspath(rootfs.bo_name, path + plen)
909 != DDI_SUCCESS ||
910 lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &rvp)) {
912 /* NUL terminate in case "path" has garbage */
913 path[plen + MAXPATHLEN - 1] = '\0';
914 #ifdef DEBUG
915 cmn_err(CE_WARN, "!Cannot lookup root device: %s",
916 path);
917 #endif
919 kmem_free(path, plen + MAXPATHLEN);
922 vfs_mnttabvp_setup();
926 * Check to see if our "block device" is actually a file. If so,
927 * automatically add a lofi device, and keep track of this fact.
929 static int
930 lofi_add(const char *fsname, struct vfs *vfsp,
931 mntopts_t *mntopts, struct mounta *uap)
933 int fromspace = (uap->flags & MS_SYSSPACE) ?
934 UIO_SYSSPACE : UIO_USERSPACE;
935 struct lofi_ioctl *li = NULL;
936 struct vnode *vp = NULL;
937 struct pathname pn = { NULL };
938 ldi_ident_t ldi_id;
939 ldi_handle_t ldi_hdl;
940 vfssw_t *vfssw;
941 int id;
942 int err = 0;
944 if ((vfssw = vfs_getvfssw(fsname)) == NULL)
945 return (0);
947 if (!(vfssw->vsw_flag & VSW_CANLOFI)) {
948 vfs_unrefvfssw(vfssw);
949 return (0);
952 vfs_unrefvfssw(vfssw);
953 vfssw = NULL;
955 if (pn_get(uap->spec, fromspace, &pn) != 0)
956 return (0);
958 if (lookupname(uap->spec, fromspace, FOLLOW, NULL, &vp) != 0)
959 goto out;
961 if (vp->v_type != VREG)
962 goto out;
964 /* OK, this is a lofi mount. */
966 if ((uap->flags & MS_REMOUNT) ||
967 vfs_optionisset_nolock(mntopts, MNTOPT_SUID, NULL) ||
968 vfs_optionisset_nolock(mntopts, MNTOPT_SETUID, NULL) ||
969 vfs_optionisset_nolock(mntopts, MNTOPT_DEVICES, NULL)) {
970 err = EINVAL;
971 goto out;
974 ldi_id = ldi_ident_from_anon();
975 li = kmem_zalloc(sizeof (*li), KM_SLEEP);
976 (void) strlcpy(li->li_filename, pn.pn_path, MAXPATHLEN);
978 err = ldi_open_by_name("/dev/lofictl", FREAD | FWRITE, kcred,
979 &ldi_hdl, ldi_id);
981 if (err)
982 goto out2;
984 err = ldi_ioctl(ldi_hdl, LOFI_MAP_FILE, (intptr_t)li,
985 FREAD | FWRITE | FKIOCTL, kcred, &id);
987 (void) ldi_close(ldi_hdl, FREAD | FWRITE, kcred);
989 if (!err)
990 vfsp->vfs_lofi_id = id;
992 out2:
993 ldi_ident_release(ldi_id);
994 out:
995 if (li != NULL)
996 kmem_free(li, sizeof (*li));
997 if (vp != NULL)
998 VN_RELE(vp);
999 pn_free(&pn);
1000 return (err);
1003 static void
1004 lofi_remove(struct vfs *vfsp)
1006 struct lofi_ioctl *li = NULL;
1007 ldi_ident_t ldi_id;
1008 ldi_handle_t ldi_hdl;
1009 int err;
1011 if (vfsp->vfs_lofi_id == 0)
1012 return;
1014 ldi_id = ldi_ident_from_anon();
1016 li = kmem_zalloc(sizeof (*li), KM_SLEEP);
1017 li->li_id = vfsp->vfs_lofi_id;
1018 li->li_cleanup = B_TRUE;
1020 err = ldi_open_by_name("/dev/lofictl", FREAD | FWRITE, kcred,
1021 &ldi_hdl, ldi_id);
1023 if (err)
1024 goto out;
1026 err = ldi_ioctl(ldi_hdl, LOFI_UNMAP_FILE_MINOR, (intptr_t)li,
1027 FREAD | FWRITE | FKIOCTL, kcred, NULL);
1029 (void) ldi_close(ldi_hdl, FREAD | FWRITE, kcred);
1031 if (!err)
1032 vfsp->vfs_lofi_id = 0;
1034 out:
1035 ldi_ident_release(ldi_id);
1036 if (li != NULL)
1037 kmem_free(li, sizeof (*li));
1041 * Common mount code. Called from the system call entry point, from autofs,
1042 * nfsv4 trigger mounts, and from pxfs.
1044 * Takes the effective file system type, mount arguments, the mount point
1045 * vnode, flags specifying whether the mount is a remount and whether it
1046 * should be entered into the vfs list, and credentials. Fills in its vfspp
1047 * parameter with the mounted file system instance's vfs.
1049 * Note that the effective file system type is specified as a string. It may
1050 * be null, in which case it's determined from the mount arguments, and may
1051 * differ from the type specified in the mount arguments; this is a hook to
1052 * allow interposition when instantiating file system instances.
1054 * The caller is responsible for releasing its own hold on the mount point
1055 * vp (this routine does its own hold when necessary).
1056 * Also note that for remounts, the mount point vp should be the vnode for
1057 * the root of the file system rather than the vnode that the file system
1058 * is mounted on top of.
1061 domount(char *fsname, struct mounta *uap, vnode_t *vp, struct cred *credp,
1062 struct vfs **vfspp)
1064 struct vfssw *vswp;
1065 vfsops_t *vfsops;
1066 struct vfs *vfsp;
1067 struct vnode *bvp;
1068 dev_t bdev = 0;
1069 mntopts_t mnt_mntopts;
1070 int error = 0;
1071 int copyout_error = 0;
1072 int ovflags;
1073 char *opts = uap->optptr;
1074 char *inargs = opts;
1075 int optlen = uap->optlen;
1076 int remount;
1077 int rdonly;
1078 int nbmand = 0;
1079 int delmip = 0;
1080 int addmip = 0;
1081 int splice = ((uap->flags & MS_NOSPLICE) == 0);
1082 int fromspace = (uap->flags & MS_SYSSPACE) ?
1083 UIO_SYSSPACE : UIO_USERSPACE;
1084 char *resource = NULL, *mountpt = NULL;
1085 refstr_t *oldresource, *oldmntpt;
1086 struct pathname pn, rpn;
1087 vsk_anchor_t *vskap;
1088 char fstname[FSTYPSZ];
1089 zone_t *zone;
1092 * The v_flag value for the mount point vp is permanently set
1093 * to VVFSLOCK so that no one bypasses the vn_vfs*locks routine
1094 * for mount point locking.
1096 mutex_enter(&vp->v_lock);
1097 vp->v_flag |= VVFSLOCK;
1098 mutex_exit(&vp->v_lock);
1100 mnt_mntopts.mo_count = 0;
1102 * Find the ops vector to use to invoke the file system-specific mount
1103 * method. If the fsname argument is non-NULL, use it directly.
1104 * Otherwise, dig the file system type information out of the mount
1105 * arguments.
1107 * A side effect is to hold the vfssw entry.
1109 * Mount arguments can be specified in several ways, which are
1110 * distinguished by flag bit settings. The preferred way is to set
1111 * MS_OPTIONSTR, indicating an 8 argument mount with the file system
1112 * type supplied as a character string and the last two arguments
1113 * being a pointer to a character buffer and the size of the buffer.
1114 * On entry, the buffer holds a null terminated list of options; on
1115 * return, the string is the list of options the file system
1116 * recognized. If MS_DATA is set arguments five and six point to a
1117 * block of binary data which the file system interprets.
1118 * A further wrinkle is that some callers don't set MS_FSS and MS_DATA
1119 * consistently with these conventions. To handle them, we check to
1120 * see whether the pointer to the file system name has a numeric value
1121 * less than 256. If so, we treat it as an index.
1123 if (fsname != NULL) {
1124 if ((vswp = vfs_getvfssw(fsname)) == NULL) {
1125 return (EINVAL);
1127 } else if (uap->flags & (MS_OPTIONSTR | MS_DATA | MS_FSS)) {
1128 size_t n;
1129 uint_t fstype;
1131 fsname = fstname;
1133 if ((fstype = (uintptr_t)uap->fstype) < 256) {
1134 RLOCK_VFSSW();
1135 if (fstype == 0 || fstype >= nfstype ||
1136 !ALLOCATED_VFSSW(&vfssw[fstype])) {
1137 RUNLOCK_VFSSW();
1138 return (EINVAL);
1140 (void) strcpy(fsname, vfssw[fstype].vsw_name);
1141 RUNLOCK_VFSSW();
1142 if ((vswp = vfs_getvfssw(fsname)) == NULL)
1143 return (EINVAL);
1144 } else {
1146 * Handle either kernel or user address space.
1148 if (uap->flags & MS_SYSSPACE) {
1149 error = copystr(uap->fstype, fsname,
1150 FSTYPSZ, &n);
1151 } else {
1152 error = copyinstr(uap->fstype, fsname,
1153 FSTYPSZ, &n);
1155 if (error) {
1156 if (error == ENAMETOOLONG)
1157 return (EINVAL);
1158 return (error);
1160 if ((vswp = vfs_getvfssw(fsname)) == NULL)
1161 return (EINVAL);
1163 } else {
1164 if ((vswp = vfs_getvfsswbyvfsops(vfs_getops(rootvfs))) == NULL)
1165 return (EINVAL);
1166 fsname = vswp->vsw_name;
1168 if (!VFS_INSTALLED(vswp))
1169 return (EINVAL);
1171 if ((error = secpolicy_fs_allowed_mount(fsname)) != 0) {
1172 vfs_unrefvfssw(vswp);
1173 return (error);
1176 vfsops = &vswp->vsw_vfsops;
1178 vfs_copyopttbl(&vswp->vsw_optproto, &mnt_mntopts);
1180 * Fetch mount options and parse them for generic vfs options
1182 if (uap->flags & MS_OPTIONSTR) {
1184 * Limit the buffer size
1186 if (optlen < 0 || optlen > MAX_MNTOPT_STR) {
1187 error = EINVAL;
1188 goto errout;
1190 if ((uap->flags & MS_SYSSPACE) == 0) {
1191 inargs = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
1192 inargs[0] = '\0';
1193 if (optlen) {
1194 error = copyinstr(opts, inargs, (size_t)optlen,
1195 NULL);
1196 if (error) {
1197 goto errout;
1201 vfs_parsemntopts(&mnt_mntopts, inargs, 0);
1204 * Flag bits override the options string.
1206 if (uap->flags & MS_REMOUNT)
1207 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_REMOUNT, NULL, 0, 0);
1208 if (uap->flags & MS_RDONLY)
1209 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_RO, NULL, 0, 0);
1210 if (uap->flags & MS_NOSUID)
1211 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL, 0, 0);
1214 * Check if this is a remount; must be set in the option string and
1215 * the file system must support a remount option.
1217 if (remount = vfs_optionisset_nolock(&mnt_mntopts,
1218 MNTOPT_REMOUNT, NULL)) {
1219 if (!(vswp->vsw_flag & VSW_CANREMOUNT)) {
1220 error = ENOTSUP;
1221 goto errout;
1223 uap->flags |= MS_REMOUNT;
1227 * uap->flags and vfs_optionisset() should agree.
1229 if (rdonly = vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_RO, NULL)) {
1230 uap->flags |= MS_RDONLY;
1232 if (vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL)) {
1233 uap->flags |= MS_NOSUID;
1235 nbmand = vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_NBMAND, NULL);
1236 ASSERT(splice || !remount);
1238 * If we are splicing the fs into the namespace,
1239 * perform mount point checks.
1241 * We want to resolve the path for the mount point to eliminate
1242 * '.' and ".." and symlinks in mount points; we can't do the
1243 * same for the resource string, since it would turn
1244 * "/dev/dsk/c0t0d0s0" into "/devices/pci@...". We need to do
1245 * this before grabbing vn_vfswlock(), because otherwise we
1246 * would deadlock with lookuppn().
1248 if (splice) {
1249 ASSERT(vp->v_count > 0);
1252 * Pick up mount point and device from appropriate space.
1254 if (pn_get(uap->spec, fromspace, &pn) == 0) {
1255 resource = kmem_alloc(pn.pn_pathlen + 1,
1256 KM_SLEEP);
1257 (void) strcpy(resource, pn.pn_path);
1258 pn_free(&pn);
1261 * Do a lookupname prior to taking the
1262 * writelock. Mark this as completed if
1263 * successful for later cleanup and addition to
1264 * the mount in progress table.
1266 if (lookupname(uap->spec, fromspace,
1267 FOLLOW, NULL, &bvp) == 0) {
1268 addmip = 1;
1271 if ((error = pn_get(uap->dir, fromspace, &pn)) == 0) {
1272 pathname_t *pnp;
1274 if (*pn.pn_path != '/') {
1275 error = EINVAL;
1276 pn_free(&pn);
1277 goto errout;
1279 pn_alloc(&rpn);
1281 * Kludge to prevent autofs from deadlocking with
1282 * itself when it calls domount().
1284 * If autofs is calling, it is because it is doing
1285 * (autofs) mounts in the process of an NFS mount. A
1286 * lookuppn() here would cause us to block waiting for
1287 * said NFS mount to complete, which can't since this
1288 * is the thread that was supposed to doing it.
1290 if (fromspace == UIO_USERSPACE) {
1291 if ((error = lookuppn(&pn, &rpn, FOLLOW, NULL,
1292 NULL)) == 0) {
1293 pnp = &rpn;
1294 } else {
1296 * The file disappeared or otherwise
1297 * became inaccessible since we opened
1298 * it; might as well fail the mount
1299 * since the mount point is no longer
1300 * accessible.
1302 pn_free(&rpn);
1303 pn_free(&pn);
1304 goto errout;
1306 } else {
1307 pnp = &pn;
1309 mountpt = kmem_alloc(pnp->pn_pathlen + 1, KM_SLEEP);
1310 (void) strcpy(mountpt, pnp->pn_path);
1313 * If the addition of the zone's rootpath
1314 * would push us over a total path length
1315 * of MAXPATHLEN, we fail the mount with
1316 * ENAMETOOLONG, which is what we would have
1317 * gotten if we were trying to perform the same
1318 * mount in the global zone.
1320 * strlen() doesn't count the trailing
1321 * '\0', but zone_rootpathlen counts both a
1322 * trailing '/' and the terminating '\0'.
1324 if ((curproc->p_zone->zone_rootpathlen - 1 +
1325 strlen(mountpt)) > MAXPATHLEN ||
1326 (resource != NULL &&
1327 (curproc->p_zone->zone_rootpathlen - 1 +
1328 strlen(resource)) > MAXPATHLEN)) {
1329 error = ENAMETOOLONG;
1332 pn_free(&rpn);
1333 pn_free(&pn);
1336 if (error)
1337 goto errout;
1340 * Prevent path name resolution from proceeding past
1341 * the mount point.
1343 if (vn_vfswlock(vp) != 0) {
1344 error = EBUSY;
1345 goto errout;
1349 * Verify that it's legitimate to establish a mount on
1350 * the prospective mount point.
1352 if (vn_mountedvfs(vp) != NULL) {
1354 * The mount point lock was obtained after some
1355 * other thread raced through and established a mount.
1357 vn_vfsunlock(vp);
1358 error = EBUSY;
1359 goto errout;
1361 if (vp->v_flag & VNOMOUNT) {
1362 vn_vfsunlock(vp);
1363 error = EINVAL;
1364 goto errout;
1367 if ((uap->flags & (MS_DATA | MS_OPTIONSTR)) == 0) {
1368 uap->dataptr = NULL;
1369 uap->datalen = 0;
1373 * If this is a remount, we don't want to create a new VFS.
1374 * Instead, we pass the existing one with a remount flag.
1376 if (remount) {
1378 * Confirm that the mount point is the root vnode of the
1379 * file system that is being remounted.
1380 * This can happen if the user specifies a different
1381 * mount point directory pathname in the (re)mount command.
1383 * Code below can only be reached if splice is true, so it's
1384 * safe to do vn_vfsunlock() here.
1386 if ((vp->v_flag & VROOT) == 0) {
1387 vn_vfsunlock(vp);
1388 error = ENOENT;
1389 goto errout;
1392 * Disallow making file systems read-only unless file system
1393 * explicitly allows it in its vfssw. Ignore other flags.
1395 if (rdonly && vn_is_readonly(vp) == 0 &&
1396 (vswp->vsw_flag & VSW_CANRWRO) == 0) {
1397 vn_vfsunlock(vp);
1398 error = EINVAL;
1399 goto errout;
1402 * Disallow changing the NBMAND disposition of the file
1403 * system on remounts.
1405 if ((nbmand && ((vp->v_vfsp->vfs_flag & VFS_NBMAND) == 0)) ||
1406 (!nbmand && (vp->v_vfsp->vfs_flag & VFS_NBMAND))) {
1407 vn_vfsunlock(vp);
1408 error = EINVAL;
1409 goto errout;
1411 vfsp = vp->v_vfsp;
1412 ovflags = vfsp->vfs_flag;
1413 vfsp->vfs_flag |= VFS_REMOUNT;
1414 vfsp->vfs_flag &= ~VFS_RDONLY;
1415 } else {
1416 vfsp = vfs_alloc(KM_SLEEP);
1417 VFS_INIT(vfsp, vfsops, NULL);
1420 VFS_HOLD(vfsp);
1422 if ((error = lofi_add(fsname, vfsp, &mnt_mntopts, uap)) != 0) {
1423 if (!remount) {
1424 if (splice)
1425 vn_vfsunlock(vp);
1426 vfs_free(vfsp);
1427 } else {
1428 vn_vfsunlock(vp);
1429 VFS_RELE(vfsp);
1431 goto errout;
1435 * PRIV_SYS_MOUNT doesn't mean you can become root.
1437 if (vfsp->vfs_lofi_id != 0) {
1438 uap->flags |= MS_NOSUID;
1439 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL, 0, 0);
1443 * The vfs_reflock is not used anymore the code below explicitly
1444 * holds it preventing others accesing it directly.
1446 if ((sema_tryp(&vfsp->vfs_reflock) == 0) &&
1447 !(vfsp->vfs_flag & VFS_REMOUNT))
1448 cmn_err(CE_WARN,
1449 "mount type %s couldn't get vfs_reflock", vswp->vsw_name);
1452 * Lock the vfs. If this is a remount we want to avoid spurious umount
1453 * failures that happen as a side-effect of fsflush() and other mount
1454 * and unmount operations that might be going on simultaneously and
1455 * may have locked the vfs currently. To not return EBUSY immediately
1456 * here we use vfs_lock_wait() instead vfs_lock() for the remount case.
1458 if (!remount) {
1459 if (error = vfs_lock(vfsp)) {
1460 vfsp->vfs_flag = ovflags;
1462 lofi_remove(vfsp);
1464 if (splice)
1465 vn_vfsunlock(vp);
1466 vfs_free(vfsp);
1467 goto errout;
1469 } else {
1470 vfs_lock_wait(vfsp);
1474 * Add device to mount in progress table, global mounts require special
1475 * handling. It is possible that we have already done the lookupname
1476 * on a spliced, non-global fs. If so, we don't want to do it again
1477 * since we cannot do a lookupname after taking the
1478 * wlock above. This case is for a non-spliced, non-global filesystem.
1480 if (!addmip) {
1481 if (lookupname(uap->spec, fromspace, FOLLOW, NULL, &bvp) == 0) {
1482 addmip = 1;
1486 if (addmip) {
1487 vnode_t *lvp = NULL;
1489 error = vfs_get_lofi(vfsp, &lvp);
1490 if (error > 0) {
1491 lofi_remove(vfsp);
1493 if (splice)
1494 vn_vfsunlock(vp);
1495 vfs_unlock(vfsp);
1497 if (remount) {
1498 VFS_RELE(vfsp);
1499 } else {
1500 vfs_free(vfsp);
1503 goto errout;
1504 } else if (error == -1) {
1505 bdev = bvp->v_rdev;
1506 VN_RELE(bvp);
1507 } else {
1508 bdev = lvp->v_rdev;
1509 VN_RELE(lvp);
1510 VN_RELE(bvp);
1513 vfs_addmip(bdev, vfsp);
1514 addmip = 0;
1515 delmip = 1;
1518 * Invalidate cached entry for the mount point.
1520 if (splice)
1521 dnlc_purge_vp(vp);
1524 * If have an option string but the filesystem doesn't supply a
1525 * prototype options table, create a table with the global
1526 * options and sufficient room to accept all the options in the
1527 * string. Then parse the passed in option string
1528 * accepting all the options in the string. This gives us an
1529 * option table with all the proper cancel properties for the
1530 * global options.
1532 * Filesystems that supply a prototype options table are handled
1533 * earlier in this function.
1535 if (uap->flags & MS_OPTIONSTR) {
1536 if (!(vswp->vsw_flag & VSW_HASPROTO)) {
1537 mntopts_t tmp_mntopts;
1539 tmp_mntopts.mo_count = 0;
1540 vfs_createopttbl_extend(&tmp_mntopts, inargs,
1541 &mnt_mntopts);
1542 vfs_parsemntopts(&tmp_mntopts, inargs, 1);
1543 vfs_swapopttbl_nolock(&mnt_mntopts, &tmp_mntopts);
1544 vfs_freeopttbl(&tmp_mntopts);
1549 * Serialize with zone state transitions.
1550 * See vfs_list_add; zone mounted into is:
1551 * zone_find_by_path(refstr_value(vfsp->vfs_mntpt))
1552 * not the zone doing the mount (curproc->p_zone), but if we're already
1553 * inside a NGZ, then we know what zone we are.
1555 if (INGLOBALZONE(curproc)) {
1556 zone = zone_find_by_path(mountpt);
1557 ASSERT(zone != NULL);
1558 } else {
1559 zone = curproc->p_zone;
1561 * zone_find_by_path does a hold, so do one here too so that
1562 * we can do a zone_rele after mount_completed.
1564 zone_hold(zone);
1566 mount_in_progress(zone);
1568 * Instantiate (or reinstantiate) the file system. If appropriate,
1569 * splice it into the file system name space.
1571 * We want VFS_MOUNT() to be able to override the vfs_resource
1572 * string if necessary (ie, mntfs), and also for a remount to
1573 * change the same (necessary when remounting '/' during boot).
1574 * So we set up vfs_mntpt and vfs_resource to what we think they
1575 * should be, then hand off control to VFS_MOUNT() which can
1576 * override this.
1578 * For safety's sake, when changing vfs_resource or vfs_mntpt of
1579 * a vfs which is on the vfs list (i.e. during a remount), we must
1580 * never set those fields to NULL. Several bits of code make
1581 * assumptions that the fields are always valid.
1583 vfs_swapopttbl(&mnt_mntopts, &vfsp->vfs_mntopts);
1584 if (remount) {
1585 if ((oldresource = vfsp->vfs_resource) != NULL)
1586 refstr_hold(oldresource);
1587 if ((oldmntpt = vfsp->vfs_mntpt) != NULL)
1588 refstr_hold(oldmntpt);
1590 vfs_setresource(vfsp, resource, 0);
1591 vfs_setmntpoint(vfsp, mountpt, 0);
1594 * going to mount on this vnode, so notify.
1596 vnevent_mountedover(vp, NULL);
1597 error = VFS_MOUNT(vfsp, vp, uap, credp);
1599 if (uap->flags & MS_RDONLY)
1600 vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
1601 if (uap->flags & MS_NOSUID)
1602 vfs_setmntopt(vfsp, MNTOPT_NOSUID, NULL, 0);
1604 if (error) {
1605 lofi_remove(vfsp);
1607 if (remount) {
1608 /* put back pre-remount options */
1609 vfs_swapopttbl(&mnt_mntopts, &vfsp->vfs_mntopts);
1610 vfs_setmntpoint(vfsp, refstr_value(oldmntpt),
1611 VFSSP_VERBATIM);
1612 if (oldmntpt)
1613 refstr_rele(oldmntpt);
1614 vfs_setresource(vfsp, refstr_value(oldresource),
1615 VFSSP_VERBATIM);
1616 if (oldresource)
1617 refstr_rele(oldresource);
1618 vfsp->vfs_flag = ovflags;
1619 vfs_unlock(vfsp);
1620 VFS_RELE(vfsp);
1621 } else {
1622 vfs_unlock(vfsp);
1623 vfs_freemnttab(vfsp);
1624 vfs_free(vfsp);
1626 } else {
1628 * Set the mount time to now
1630 vfsp->vfs_mtime = ddi_get_time();
1631 if (remount) {
1632 vfsp->vfs_flag &= ~VFS_REMOUNT;
1633 if (oldresource)
1634 refstr_rele(oldresource);
1635 if (oldmntpt)
1636 refstr_rele(oldmntpt);
1637 } else if (splice) {
1639 * Link vfsp into the name space at the mount
1640 * point. Vfs_add() is responsible for
1641 * holding the mount point which will be
1642 * released when vfs_remove() is called.
1644 vfs_add(vp, vfsp, uap->flags);
1645 } else {
1647 * Hold the reference to file system which is
1648 * not linked into the name space.
1650 vfsp->vfs_zone = NULL;
1651 VFS_HOLD(vfsp);
1652 vfsp->vfs_vnodecovered = NULL;
1655 * Set flags for global options encountered
1657 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL))
1658 vfsp->vfs_flag |= VFS_RDONLY;
1659 else
1660 vfsp->vfs_flag &= ~VFS_RDONLY;
1661 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
1662 vfsp->vfs_flag |= (VFS_NOSETUID|VFS_NODEVICES);
1663 } else {
1664 if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
1665 vfsp->vfs_flag |= VFS_NODEVICES;
1666 else
1667 vfsp->vfs_flag &= ~VFS_NODEVICES;
1668 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))
1669 vfsp->vfs_flag |= VFS_NOSETUID;
1670 else
1671 vfsp->vfs_flag &= ~VFS_NOSETUID;
1673 if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL))
1674 vfsp->vfs_flag |= VFS_NBMAND;
1675 else
1676 vfsp->vfs_flag &= ~VFS_NBMAND;
1678 if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL))
1679 vfsp->vfs_flag |= VFS_XATTR;
1680 else
1681 vfsp->vfs_flag &= ~VFS_XATTR;
1683 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL))
1684 vfsp->vfs_flag |= VFS_NOEXEC;
1685 else
1686 vfsp->vfs_flag &= ~VFS_NOEXEC;
1689 * Now construct the output option string of options
1690 * we recognized.
1692 if (uap->flags & MS_OPTIONSTR) {
1693 vfs_list_read_lock();
1694 copyout_error = vfs_buildoptionstr(
1695 &vfsp->vfs_mntopts, inargs, optlen);
1696 vfs_list_unlock();
1697 if (copyout_error == 0 &&
1698 (uap->flags & MS_SYSSPACE) == 0) {
1699 copyout_error = copyoutstr(inargs, opts,
1700 optlen, NULL);
1705 * If this isn't a remount, set up the vopstats before
1706 * anyone can touch this. We only allow spliced file
1707 * systems (file systems which are in the namespace) to
1708 * have the VFS_STATS flag set.
1709 * NOTE: PxFS mounts the underlying file system with
1710 * MS_NOSPLICE set and copies those vfs_flags to its private
1711 * vfs structure. As a result, PxFS should never have
1712 * the VFS_STATS flag or else we might access the vfs
1713 * statistics-related fields prior to them being
1714 * properly initialized.
1716 if (!remount && (vswp->vsw_flag & VSW_STATS) && splice) {
1717 initialize_vopstats(&vfsp->vfs_vopstats);
1719 * We need to set vfs_vskap to NULL because there's
1720 * a chance it won't be set below. This is checked
1721 * in teardown_vopstats() so we can't have garbage.
1723 vfsp->vfs_vskap = NULL;
1724 vfsp->vfs_flag |= VFS_STATS;
1725 vfsp->vfs_fstypevsp = get_fstype_vopstats(vfsp, vswp);
1728 if (vswp->vsw_flag & VSW_XID)
1729 vfsp->vfs_flag |= VFS_XID;
1731 vfs_unlock(vfsp);
1733 mount_completed(zone);
1734 zone_rele(zone);
1735 if (splice)
1736 vn_vfsunlock(vp);
1738 if ((error == 0) && (copyout_error == 0)) {
1739 if (!remount) {
1741 * Don't call get_vskstat_anchor() while holding
1742 * locks since it allocates memory and calls
1743 * VFS_STATVFS(). For NFS, the latter can generate
1744 * an over-the-wire call.
1746 vskap = get_vskstat_anchor(vfsp);
1747 /* Only take the lock if we have something to do */
1748 if (vskap != NULL) {
1749 vfs_lock_wait(vfsp);
1750 if (vfsp->vfs_flag & VFS_STATS) {
1751 vfsp->vfs_vskap = vskap;
1753 vfs_unlock(vfsp);
1756 /* Return vfsp to caller. */
1757 *vfspp = vfsp;
1759 errout:
1760 vfs_freeopttbl(&mnt_mntopts);
1761 if (resource != NULL)
1762 kmem_free(resource, strlen(resource) + 1);
1763 if (mountpt != NULL)
1764 kmem_free(mountpt, strlen(mountpt) + 1);
1766 * It is possible we errored prior to adding to mount in progress
1767 * table. Must free vnode we acquired with successful lookupname.
1769 if (addmip)
1770 VN_RELE(bvp);
1771 if (delmip)
1772 vfs_delmip(vfsp);
1773 ASSERT(vswp != NULL);
1774 vfs_unrefvfssw(vswp);
1775 if (inargs != opts)
1776 kmem_free(inargs, MAX_MNTOPT_STR);
1777 if (copyout_error) {
1778 lofi_remove(vfsp);
1779 VFS_RELE(vfsp);
1780 error = copyout_error;
1782 return (error);
1785 static void
1786 vfs_setpath(
1787 struct vfs *vfsp, /* vfs being updated */
1788 refstr_t **refp, /* Ref-count string to contain the new path */
1789 const char *newpath, /* Path to add to refp (above) */
1790 uint32_t flag) /* flag */
1792 size_t len;
1793 refstr_t *ref;
1794 zone_t *zone = curproc->p_zone;
1795 char *sp;
1796 int have_list_lock = 0;
1798 ASSERT(!VFS_ON_LIST(vfsp) || vfs_lock_held(vfsp));
1801 * New path must be less than MAXPATHLEN because mntfs
1802 * will only display up to MAXPATHLEN bytes. This is currently
1803 * safe, because domount() uses pn_get(), and other callers
1804 * similarly cap the size to fewer than MAXPATHLEN bytes.
1807 ASSERT(strlen(newpath) < MAXPATHLEN);
1809 /* mntfs requires consistency while vfs list lock is held */
1811 if (VFS_ON_LIST(vfsp)) {
1812 have_list_lock = 1;
1813 vfs_list_lock();
1816 if (*refp != NULL)
1817 refstr_rele(*refp);
1820 * If we are in a non-global zone then we prefix the supplied path,
1821 * newpath, with the zone's root path, with two exceptions. The first
1822 * is where we have been explicitly directed to avoid doing so; this
1823 * will be the case following a failed remount, where the path supplied
1824 * will be a saved version which must now be restored. The second
1825 * exception is where newpath is not a pathname but a descriptive name,
1826 * e.g. "procfs".
1828 if (zone == global_zone || (flag & VFSSP_VERBATIM) || *newpath != '/') {
1829 ref = refstr_alloc(newpath);
1830 goto out;
1834 * Truncate the trailing '/' in the zoneroot, and merge
1835 * in the zone's rootpath with the "newpath" (resource
1836 * or mountpoint) passed in.
1838 * The size of the required buffer is thus the size of
1839 * the buffer required for the passed-in newpath
1840 * (strlen(newpath) + 1), plus the size of the buffer
1841 * required to hold zone_rootpath (zone_rootpathlen)
1842 * minus one for one of the now-superfluous NUL
1843 * terminations, minus one for the trailing '/'.
1845 * That gives us:
1847 * (strlen(newpath) + 1) + zone_rootpathlen - 1 - 1
1849 * Which is what we have below.
1852 len = strlen(newpath) + zone->zone_rootpathlen - 1;
1853 sp = kmem_alloc(len, KM_SLEEP);
1856 * Copy everything including the trailing slash, which
1857 * we then overwrite with the NUL character.
1860 (void) strcpy(sp, zone->zone_rootpath);
1861 sp[zone->zone_rootpathlen - 2] = '\0';
1862 (void) strcat(sp, newpath);
1864 ref = refstr_alloc(sp);
1865 kmem_free(sp, len);
1866 out:
1867 *refp = ref;
1869 if (have_list_lock) {
1870 vfs_mnttab_modtimeupd();
1871 vfs_list_unlock();
1876 * Record a mounted resource name in a vfs structure.
1877 * If vfsp is already mounted, caller must hold the vfs lock.
1879 void
1880 vfs_setresource(struct vfs *vfsp, const char *resource, uint32_t flag)
1882 if (resource == NULL || resource[0] == '\0')
1883 resource = VFS_NORESOURCE;
1884 vfs_setpath(vfsp, &vfsp->vfs_resource, resource, flag);
1888 * Record a mount point name in a vfs structure.
1889 * If vfsp is already mounted, caller must hold the vfs lock.
1891 void
1892 vfs_setmntpoint(struct vfs *vfsp, const char *mntpt, uint32_t flag)
1894 if (mntpt == NULL || mntpt[0] == '\0')
1895 mntpt = VFS_NOMNTPT;
1896 vfs_setpath(vfsp, &vfsp->vfs_mntpt, mntpt, flag);
1899 /* Returns the vfs_resource. Caller must call refstr_rele() when finished. */
1901 refstr_t *
1902 vfs_getresource(const struct vfs *vfsp)
1904 refstr_t *resource;
1906 vfs_list_read_lock();
1907 resource = vfsp->vfs_resource;
1908 refstr_hold(resource);
1909 vfs_list_unlock();
1911 return (resource);
1914 /* Returns the vfs_mntpt. Caller must call refstr_rele() when finished. */
1916 refstr_t *
1917 vfs_getmntpoint(const struct vfs *vfsp)
1919 refstr_t *mntpt;
1921 vfs_list_read_lock();
1922 mntpt = vfsp->vfs_mntpt;
1923 refstr_hold(mntpt);
1924 vfs_list_unlock();
1926 return (mntpt);
1930 * Create an empty options table with enough empty slots to hold all
1931 * The options in the options string passed as an argument.
1932 * Potentially prepend another options table.
1934 * Note: caller is responsible for locking the vfs list, if needed,
1935 * to protect mops.
1937 static void
1938 vfs_createopttbl_extend(mntopts_t *mops, const char *opts,
1939 const mntopts_t *mtmpl)
1941 const char *s = opts;
1942 uint_t count;
1944 if (opts == NULL || *opts == '\0') {
1945 count = 0;
1946 } else {
1947 count = 1;
1950 * Count number of options in the string
1952 for (s = strchr(s, ','); s != NULL; s = strchr(s, ',')) {
1953 count++;
1954 s++;
1957 vfs_copyopttbl_extend(mtmpl, mops, count);
1961 * Create an empty options table with enough empty slots to hold all
1962 * The options in the options string passed as an argument.
1964 * This function is *not* for general use by filesystems.
1966 * Note: caller is responsible for locking the vfs list, if needed,
1967 * to protect mops.
1969 void
1970 vfs_createopttbl(mntopts_t *mops, const char *opts)
1972 vfs_createopttbl_extend(mops, opts, NULL);
1977 * Swap two mount options tables
1979 static void
1980 vfs_swapopttbl_nolock(mntopts_t *optbl1, mntopts_t *optbl2)
1982 uint_t tmpcnt;
1983 mntopt_t *tmplist;
1985 tmpcnt = optbl2->mo_count;
1986 tmplist = optbl2->mo_list;
1987 optbl2->mo_count = optbl1->mo_count;
1988 optbl2->mo_list = optbl1->mo_list;
1989 optbl1->mo_count = tmpcnt;
1990 optbl1->mo_list = tmplist;
1993 static void
1994 vfs_swapopttbl(mntopts_t *optbl1, mntopts_t *optbl2)
1996 vfs_list_lock();
1997 vfs_swapopttbl_nolock(optbl1, optbl2);
1998 vfs_mnttab_modtimeupd();
1999 vfs_list_unlock();
2002 static char **
2003 vfs_copycancelopt_extend(char **const moc, int extend)
2005 int i = 0;
2006 int j;
2007 char **result;
2009 if (moc != NULL) {
2010 for (; moc[i] != NULL; i++)
2011 /* count number of options to cancel */;
2014 if (i + extend == 0)
2015 return (NULL);
2017 result = kmem_alloc((i + extend + 1) * sizeof (char *), KM_SLEEP);
2019 for (j = 0; j < i; j++) {
2020 result[j] = kmem_alloc(strlen(moc[j]) + 1, KM_SLEEP);
2021 (void) strcpy(result[j], moc[j]);
2023 for (; j <= i + extend; j++)
2024 result[j] = NULL;
2026 return (result);
2029 static void
2030 vfs_copyopt(const mntopt_t *s, mntopt_t *d)
2032 char *sp, *dp;
2034 d->mo_flags = s->mo_flags;
2035 d->mo_data = s->mo_data;
2036 sp = s->mo_name;
2037 if (sp != NULL) {
2038 dp = kmem_alloc(strlen(sp) + 1, KM_SLEEP);
2039 (void) strcpy(dp, sp);
2040 d->mo_name = dp;
2041 } else {
2042 d->mo_name = NULL; /* should never happen */
2045 d->mo_cancel = vfs_copycancelopt_extend(s->mo_cancel, 0);
2047 sp = s->mo_arg;
2048 if (sp != NULL) {
2049 dp = kmem_alloc(strlen(sp) + 1, KM_SLEEP);
2050 (void) strcpy(dp, sp);
2051 d->mo_arg = dp;
2052 } else {
2053 d->mo_arg = NULL;
2058 * Copy a mount options table, possibly allocating some spare
2059 * slots at the end. It is permissible to copy_extend the NULL table.
2061 static void
2062 vfs_copyopttbl_extend(const mntopts_t *smo, mntopts_t *dmo, int extra)
2064 uint_t i, count;
2065 mntopt_t *motbl;
2068 * Clear out any existing stuff in the options table being initialized
2070 vfs_freeopttbl(dmo);
2071 count = (smo == NULL) ? 0 : smo->mo_count;
2072 if ((count + extra) == 0) /* nothing to do */
2073 return;
2074 dmo->mo_count = count + extra;
2075 motbl = kmem_zalloc((count + extra) * sizeof (mntopt_t), KM_SLEEP);
2076 dmo->mo_list = motbl;
2077 for (i = 0; i < count; i++) {
2078 vfs_copyopt(&smo->mo_list[i], &motbl[i]);
2080 for (i = count; i < count + extra; i++) {
2081 motbl[i].mo_flags = MO_EMPTY;
2086 * Copy a mount options table.
2088 * This function is *not* for general use by filesystems.
2090 * Note: caller is responsible for locking the vfs list, if needed,
2091 * to protect smo and dmo.
2093 void
2094 vfs_copyopttbl(const mntopts_t *smo, mntopts_t *dmo)
2096 vfs_copyopttbl_extend(smo, dmo, 0);
2099 static char **
2100 vfs_mergecancelopts(const mntopt_t *mop1, const mntopt_t *mop2)
2102 int c1 = 0;
2103 int c2 = 0;
2104 char **result;
2105 char **sp1, **sp2, **dp;
2108 * First we count both lists of cancel options.
2109 * If either is NULL or has no elements, we return a copy of
2110 * the other.
2112 if (mop1->mo_cancel != NULL) {
2113 for (; mop1->mo_cancel[c1] != NULL; c1++)
2114 /* count cancel options in mop1 */;
2117 if (c1 == 0)
2118 return (vfs_copycancelopt_extend(mop2->mo_cancel, 0));
2120 if (mop2->mo_cancel != NULL) {
2121 for (; mop2->mo_cancel[c2] != NULL; c2++)
2122 /* count cancel options in mop2 */;
2125 result = vfs_copycancelopt_extend(mop1->mo_cancel, c2);
2127 if (c2 == 0)
2128 return (result);
2131 * When we get here, we've got two sets of cancel options;
2132 * we need to merge the two sets. We know that the result
2133 * array has "c1+c2+1" entries and in the end we might shrink
2134 * it.
2135 * Result now has a copy of the c1 entries from mop1; we'll
2136 * now lookup all the entries of mop2 in mop1 and copy it if
2137 * it is unique.
2138 * This operation is O(n^2) but it's only called once per
2139 * filesystem per duplicate option. This is a situation
2140 * which doesn't arise with the filesystems in ON and
2141 * n is generally 1.
2144 dp = &result[c1];
2145 for (sp2 = mop2->mo_cancel; *sp2 != NULL; sp2++) {
2146 for (sp1 = mop1->mo_cancel; *sp1 != NULL; sp1++) {
2147 if (strcmp(*sp1, *sp2) == 0)
2148 break;
2150 if (*sp1 == NULL) {
2152 * Option *sp2 not found in mop1, so copy it.
2153 * The calls to vfs_copycancelopt_extend()
2154 * guarantee that there's enough room.
2156 *dp = kmem_alloc(strlen(*sp2) + 1, KM_SLEEP);
2157 (void) strcpy(*dp++, *sp2);
2160 if (dp != &result[c1+c2]) {
2161 size_t bytes = (dp - result + 1) * sizeof (char *);
2162 char **nres = kmem_alloc(bytes, KM_SLEEP);
2164 bcopy(result, nres, bytes);
2165 kmem_free(result, (c1 + c2 + 1) * sizeof (char *));
2166 result = nres;
2168 return (result);
2172 * Merge two mount option tables (outer and inner) into one. This is very
2173 * similar to "merging" global variables and automatic variables in C.
2175 * This isn't (and doesn't have to be) fast.
2177 * This function is *not* for general use by filesystems.
2179 * Note: caller is responsible for locking the vfs list, if needed,
2180 * to protect omo, imo & dmo.
2182 void
2183 vfs_mergeopttbl(const mntopts_t *omo, const mntopts_t *imo, mntopts_t *dmo)
2185 uint_t i, count;
2186 mntopt_t *mop, *motbl;
2187 uint_t freeidx;
2190 * First determine how much space we need to allocate.
2192 count = omo->mo_count;
2193 for (i = 0; i < imo->mo_count; i++) {
2194 if (imo->mo_list[i].mo_flags & MO_EMPTY)
2195 continue;
2196 if (vfs_hasopt(omo, imo->mo_list[i].mo_name) == NULL)
2197 count++;
2199 ASSERT(count >= omo->mo_count &&
2200 count <= omo->mo_count + imo->mo_count);
2201 motbl = kmem_alloc(count * sizeof (mntopt_t), KM_SLEEP);
2202 for (i = 0; i < omo->mo_count; i++)
2203 vfs_copyopt(&omo->mo_list[i], &motbl[i]);
2204 freeidx = omo->mo_count;
2205 for (i = 0; i < imo->mo_count; i++) {
2206 if (imo->mo_list[i].mo_flags & MO_EMPTY)
2207 continue;
2208 if ((mop = vfs_hasopt(omo, imo->mo_list[i].mo_name)) != NULL) {
2209 char **newcanp;
2210 uint_t index = mop - omo->mo_list;
2212 newcanp = vfs_mergecancelopts(mop, &motbl[index]);
2214 vfs_freeopt(&motbl[index]);
2215 vfs_copyopt(&imo->mo_list[i], &motbl[index]);
2217 vfs_freecancelopt(motbl[index].mo_cancel);
2218 motbl[index].mo_cancel = newcanp;
2219 } else {
2221 * If it's a new option, just copy it over to the first
2222 * free location.
2224 vfs_copyopt(&imo->mo_list[i], &motbl[freeidx++]);
2227 dmo->mo_count = count;
2228 dmo->mo_list = motbl;
2232 * Functions to set and clear mount options in a mount options table.
2236 * Clear a mount option, if it exists.
2238 * The update_mnttab arg indicates whether mops is part of a vfs that is on
2239 * the vfs list.
2241 static void
2242 vfs_clearmntopt_nolock(mntopts_t *mops, const char *opt, int update_mnttab)
2244 struct mntopt *mop;
2245 uint_t i, count;
2247 ASSERT(!update_mnttab || RW_WRITE_HELD(&vfslist));
2249 count = mops->mo_count;
2250 for (i = 0; i < count; i++) {
2251 mop = &mops->mo_list[i];
2253 if (mop->mo_flags & MO_EMPTY)
2254 continue;
2255 if (strcmp(opt, mop->mo_name))
2256 continue;
2257 mop->mo_flags &= ~MO_SET;
2258 if (mop->mo_arg != NULL) {
2259 kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
2261 mop->mo_arg = NULL;
2262 if (update_mnttab)
2263 vfs_mnttab_modtimeupd();
2264 break;
2268 void
2269 vfs_clearmntopt(struct vfs *vfsp, const char *opt)
2271 int gotlock = 0;
2273 if (VFS_ON_LIST(vfsp)) {
2274 gotlock = 1;
2275 vfs_list_lock();
2277 vfs_clearmntopt_nolock(&vfsp->vfs_mntopts, opt, gotlock);
2278 if (gotlock)
2279 vfs_list_unlock();
2284 * Set a mount option on. If it's not found in the table, it's silently
2285 * ignored. If the option has MO_IGNORE set, it is still set unless the
2286 * VFS_NOFORCEOPT bit is set in the flags. Also, VFS_DISPLAY/VFS_NODISPLAY flag
2287 * bits can be used to toggle the MO_NODISPLAY bit for the option.
2288 * If the VFS_CREATEOPT flag bit is set then the first option slot with
2289 * MO_EMPTY set is created as the option passed in.
2291 * The update_mnttab arg indicates whether mops is part of a vfs that is on
2292 * the vfs list.
2294 static void
2295 vfs_setmntopt_nolock(mntopts_t *mops, const char *opt,
2296 const char *arg, int flags, int update_mnttab)
2298 mntopt_t *mop;
2299 uint_t i, count;
2300 char *sp;
2302 ASSERT(!update_mnttab || RW_WRITE_HELD(&vfslist));
2304 if (flags & VFS_CREATEOPT) {
2305 if (vfs_hasopt(mops, opt) != NULL) {
2306 flags &= ~VFS_CREATEOPT;
2309 count = mops->mo_count;
2310 for (i = 0; i < count; i++) {
2311 mop = &mops->mo_list[i];
2313 if (mop->mo_flags & MO_EMPTY) {
2314 if ((flags & VFS_CREATEOPT) == 0)
2315 continue;
2316 sp = kmem_alloc(strlen(opt) + 1, KM_SLEEP);
2317 (void) strcpy(sp, opt);
2318 mop->mo_name = sp;
2319 if (arg != NULL)
2320 mop->mo_flags = MO_HASVALUE;
2321 else
2322 mop->mo_flags = 0;
2323 } else if (strcmp(opt, mop->mo_name)) {
2324 continue;
2326 if ((mop->mo_flags & MO_IGNORE) && (flags & VFS_NOFORCEOPT))
2327 break;
2328 if (arg != NULL && (mop->mo_flags & MO_HASVALUE) != 0) {
2329 sp = kmem_alloc(strlen(arg) + 1, KM_SLEEP);
2330 (void) strcpy(sp, arg);
2331 } else {
2332 sp = NULL;
2334 if (mop->mo_arg != NULL)
2335 kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
2336 mop->mo_arg = sp;
2337 if (flags & VFS_DISPLAY)
2338 mop->mo_flags &= ~MO_NODISPLAY;
2339 if (flags & VFS_NODISPLAY)
2340 mop->mo_flags |= MO_NODISPLAY;
2341 mop->mo_flags |= MO_SET;
2342 if (mop->mo_cancel != NULL) {
2343 char **cp;
2345 for (cp = mop->mo_cancel; *cp != NULL; cp++)
2346 vfs_clearmntopt_nolock(mops, *cp, 0);
2348 if (update_mnttab)
2349 vfs_mnttab_modtimeupd();
2350 break;
2354 void
2355 vfs_setmntopt(struct vfs *vfsp, const char *opt, const char *arg, int flags)
2357 int gotlock = 0;
2359 if (VFS_ON_LIST(vfsp)) {
2360 gotlock = 1;
2361 vfs_list_lock();
2363 vfs_setmntopt_nolock(&vfsp->vfs_mntopts, opt, arg, flags, gotlock);
2364 if (gotlock)
2365 vfs_list_unlock();
2370 * Add a "tag" option to a mounted file system's options list.
2372 * Note: caller is responsible for locking the vfs list, if needed,
2373 * to protect mops.
2375 static mntopt_t *
2376 vfs_addtag(mntopts_t *mops, const char *tag)
2378 uint_t count;
2379 mntopt_t *mop, *motbl;
2381 count = mops->mo_count + 1;
2382 motbl = kmem_zalloc(count * sizeof (mntopt_t), KM_SLEEP);
2383 if (mops->mo_count) {
2384 size_t len = (count - 1) * sizeof (mntopt_t);
2386 bcopy(mops->mo_list, motbl, len);
2387 kmem_free(mops->mo_list, len);
2389 mops->mo_count = count;
2390 mops->mo_list = motbl;
2391 mop = &motbl[count - 1];
2392 mop->mo_flags = MO_TAG;
2393 mop->mo_name = kmem_alloc(strlen(tag) + 1, KM_SLEEP);
2394 (void) strcpy(mop->mo_name, tag);
2395 return (mop);
2399 * Allow users to set arbitrary "tags" in a vfs's mount options.
2400 * Broader use within the kernel is discouraged.
2403 vfs_settag(uint_t major, uint_t minor, const char *mntpt, const char *tag,
2404 cred_t *cr)
2406 vfs_t *vfsp;
2407 mntopts_t *mops;
2408 mntopt_t *mop;
2409 int found = 0;
2410 dev_t dev = makedevice(major, minor);
2411 int err = 0;
2412 char *buf = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
2415 * Find the desired mounted file system
2417 vfs_list_lock();
2418 vfsp = rootvfs;
2419 do {
2420 if (vfsp->vfs_dev == dev &&
2421 strcmp(mntpt, refstr_value(vfsp->vfs_mntpt)) == 0) {
2422 found = 1;
2423 break;
2425 vfsp = vfsp->vfs_next;
2426 } while (vfsp != rootvfs);
2428 if (!found) {
2429 err = EINVAL;
2430 goto out;
2432 err = secpolicy_fs_config(cr, vfsp);
2433 if (err != 0)
2434 goto out;
2436 mops = &vfsp->vfs_mntopts;
2438 * Add tag if it doesn't already exist
2440 if ((mop = vfs_hasopt(mops, tag)) == NULL) {
2441 int len;
2443 (void) vfs_buildoptionstr(mops, buf, MAX_MNTOPT_STR);
2444 len = strlen(buf);
2445 if (len + strlen(tag) + 2 > MAX_MNTOPT_STR) {
2446 err = ENAMETOOLONG;
2447 goto out;
2449 mop = vfs_addtag(mops, tag);
2451 if ((mop->mo_flags & MO_TAG) == 0) {
2452 err = EINVAL;
2453 goto out;
2455 vfs_setmntopt_nolock(mops, tag, NULL, 0, 1);
2456 out:
2457 vfs_list_unlock();
2458 kmem_free(buf, MAX_MNTOPT_STR);
2459 return (err);
2463 * Allow users to remove arbitrary "tags" in a vfs's mount options.
2464 * Broader use within the kernel is discouraged.
2467 vfs_clrtag(uint_t major, uint_t minor, const char *mntpt, const char *tag,
2468 cred_t *cr)
2470 vfs_t *vfsp;
2471 mntopt_t *mop;
2472 int found = 0;
2473 dev_t dev = makedevice(major, minor);
2474 int err = 0;
2477 * Find the desired mounted file system
2479 vfs_list_lock();
2480 vfsp = rootvfs;
2481 do {
2482 if (vfsp->vfs_dev == dev &&
2483 strcmp(mntpt, refstr_value(vfsp->vfs_mntpt)) == 0) {
2484 found = 1;
2485 break;
2487 vfsp = vfsp->vfs_next;
2488 } while (vfsp != rootvfs);
2490 if (!found) {
2491 err = EINVAL;
2492 goto out;
2494 err = secpolicy_fs_config(cr, vfsp);
2495 if (err != 0)
2496 goto out;
2498 if ((mop = vfs_hasopt(&vfsp->vfs_mntopts, tag)) == NULL) {
2499 err = EINVAL;
2500 goto out;
2502 if ((mop->mo_flags & MO_TAG) == 0) {
2503 err = EINVAL;
2504 goto out;
2506 vfs_clearmntopt_nolock(&vfsp->vfs_mntopts, tag, 1);
2507 out:
2508 vfs_list_unlock();
2509 return (err);
2513 * Function to parse an option string and fill in a mount options table.
2514 * Unknown options are silently ignored. The input option string is modified
2515 * by replacing separators with nulls. If the create flag is set, options
2516 * not found in the table are just added on the fly. The table must have
2517 * an option slot marked MO_EMPTY to add an option on the fly.
2519 * This function is *not* for general use by filesystems.
2521 * Note: caller is responsible for locking the vfs list, if needed,
2522 * to protect mops..
2524 void
2525 vfs_parsemntopts(mntopts_t *mops, char *osp, int create)
2527 char *s = osp, *p, *nextop, *valp, *cp, *ep;
2528 int setflg = VFS_NOFORCEOPT;
2530 if (osp == NULL)
2531 return;
2532 while (*s != '\0') {
2533 p = strchr(s, ','); /* find next option */
2534 if (p == NULL) {
2535 cp = NULL;
2536 p = s + strlen(s);
2537 } else {
2538 cp = p; /* save location of comma */
2539 *p++ = '\0'; /* mark end and point to next option */
2541 nextop = p;
2542 p = strchr(s, '='); /* look for value */
2543 if (p == NULL) {
2544 valp = NULL; /* no value supplied */
2545 } else {
2546 ep = p; /* save location of equals */
2547 *p++ = '\0'; /* end option and point to value */
2548 valp = p;
2551 * set option into options table
2553 if (create)
2554 setflg |= VFS_CREATEOPT;
2555 vfs_setmntopt_nolock(mops, s, valp, setflg, 0);
2556 if (cp != NULL)
2557 *cp = ','; /* restore the comma */
2558 if (valp != NULL)
2559 *ep = '='; /* restore the equals */
2560 s = nextop;
2565 * Function to inquire if an option exists in a mount options table.
2566 * Returns a pointer to the option if it exists, else NULL.
2568 * This function is *not* for general use by filesystems.
2570 * Note: caller is responsible for locking the vfs list, if needed,
2571 * to protect mops.
2573 struct mntopt *
2574 vfs_hasopt(const mntopts_t *mops, const char *opt)
2576 struct mntopt *mop;
2577 uint_t i, count;
2579 count = mops->mo_count;
2580 for (i = 0; i < count; i++) {
2581 mop = &mops->mo_list[i];
2583 if (mop->mo_flags & MO_EMPTY)
2584 continue;
2585 if (strcmp(opt, mop->mo_name) == 0)
2586 return (mop);
2588 return (NULL);
2592 * Function to inquire if an option is set in a mount options table.
2593 * Returns non-zero if set and fills in the arg pointer with a pointer to
2594 * the argument string or NULL if there is no argument string.
2596 static int
2597 vfs_optionisset_nolock(const mntopts_t *mops, const char *opt, char **argp)
2599 struct mntopt *mop;
2600 uint_t i, count;
2602 count = mops->mo_count;
2603 for (i = 0; i < count; i++) {
2604 mop = &mops->mo_list[i];
2606 if (mop->mo_flags & MO_EMPTY)
2607 continue;
2608 if (strcmp(opt, mop->mo_name))
2609 continue;
2610 if ((mop->mo_flags & MO_SET) == 0)
2611 return (0);
2612 if (argp != NULL && (mop->mo_flags & MO_HASVALUE) != 0)
2613 *argp = mop->mo_arg;
2614 return (1);
2616 return (0);
2621 vfs_optionisset(const struct vfs *vfsp, const char *opt, char **argp)
2623 int ret;
2625 vfs_list_read_lock();
2626 ret = vfs_optionisset_nolock(&vfsp->vfs_mntopts, opt, argp);
2627 vfs_list_unlock();
2628 return (ret);
2633 * Construct a comma separated string of the options set in the given
2634 * mount table, return the string in the given buffer. Return non-zero if
2635 * the buffer would overflow.
2637 * This function is *not* for general use by filesystems.
2639 * Note: caller is responsible for locking the vfs list, if needed,
2640 * to protect mp.
2643 vfs_buildoptionstr(const mntopts_t *mp, char *buf, int len)
2645 char *cp;
2646 uint_t i;
2648 buf[0] = '\0';
2649 cp = buf;
2650 for (i = 0; i < mp->mo_count; i++) {
2651 struct mntopt *mop;
2653 mop = &mp->mo_list[i];
2654 if (mop->mo_flags & MO_SET) {
2655 int optlen, comma = 0;
2657 if (buf[0] != '\0')
2658 comma = 1;
2659 optlen = strlen(mop->mo_name);
2660 if (strlen(buf) + comma + optlen + 1 > len)
2661 goto err;
2662 if (comma)
2663 *cp++ = ',';
2664 (void) strcpy(cp, mop->mo_name);
2665 cp += optlen;
2667 * Append option value if there is one
2669 if (mop->mo_arg != NULL) {
2670 int arglen;
2672 arglen = strlen(mop->mo_arg);
2673 if (strlen(buf) + arglen + 2 > len)
2674 goto err;
2675 *cp++ = '=';
2676 (void) strcpy(cp, mop->mo_arg);
2677 cp += arglen;
2681 return (0);
2682 err:
2683 return (EOVERFLOW);
2686 static void
2687 vfs_freecancelopt(char **moc)
2689 if (moc != NULL) {
2690 int ccnt = 0;
2691 char **cp;
2693 for (cp = moc; *cp != NULL; cp++) {
2694 kmem_free(*cp, strlen(*cp) + 1);
2695 ccnt++;
2697 kmem_free(moc, (ccnt + 1) * sizeof (char *));
2701 static void
2702 vfs_freeopt(mntopt_t *mop)
2704 if (mop->mo_name != NULL)
2705 kmem_free(mop->mo_name, strlen(mop->mo_name) + 1);
2707 vfs_freecancelopt(mop->mo_cancel);
2709 if (mop->mo_arg != NULL)
2710 kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
2714 * Free a mount options table
2716 * This function is *not* for general use by filesystems.
2718 * Note: caller is responsible for locking the vfs list, if needed,
2719 * to protect mp.
2721 void
2722 vfs_freeopttbl(mntopts_t *mp)
2724 uint_t i, count;
2726 count = mp->mo_count;
2727 for (i = 0; i < count; i++) {
2728 vfs_freeopt(&mp->mo_list[i]);
2730 if (count) {
2731 kmem_free(mp->mo_list, sizeof (mntopt_t) * count);
2732 mp->mo_count = 0;
2733 mp->mo_list = NULL;
2738 /* ARGSUSED */
2739 static int
2740 vfs_mntdummyread(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cred,
2741 caller_context_t *ct)
2743 return (0);
2746 /* ARGSUSED */
2747 static int
2748 vfs_mntdummywrite(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cred,
2749 caller_context_t *ct)
2751 return (0);
2755 * The dummy vnode is currently used only by file events notification
2756 * module which is just interested in the timestamps.
2758 /* ARGSUSED */
2759 static int
2760 vfs_mntdummygetattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2761 caller_context_t *ct)
2763 bzero(vap, sizeof (vattr_t));
2764 vap->va_type = VREG;
2765 vap->va_nlink = 1;
2766 vap->va_ctime = vfs_mnttab_ctime;
2768 * it is ok to just copy mtime as the time will be monotonically
2769 * increasing.
2771 vap->va_mtime = vfs_mnttab_mtime;
2772 vap->va_atime = vap->va_mtime;
2773 return (0);
2776 static void
2777 vfs_mnttabvp_setup(void)
2779 vnode_t *tvp;
2780 vnodeops_t *vfs_mntdummyvnops;
2781 const fs_operation_def_t mnt_dummyvnodeops_template[] = {
2782 VOPNAME_READ, { .vop_read = vfs_mntdummyread },
2783 VOPNAME_WRITE, { .vop_write = vfs_mntdummywrite },
2784 VOPNAME_GETATTR, { .vop_getattr = vfs_mntdummygetattr },
2785 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
2786 NULL, NULL
2789 if (vn_make_ops("mnttab", mnt_dummyvnodeops_template,
2790 &vfs_mntdummyvnops) != 0) {
2791 cmn_err(CE_WARN, "vfs_mnttabvp_setup: vn_make_ops failed");
2792 /* Shouldn't happen, but not bad enough to panic */
2793 return;
2797 * A global dummy vnode is allocated to represent mntfs files.
2798 * The mntfs file (/etc/mnttab) can be monitored for file events
2799 * and receive an event when mnttab changes. Dummy VOP calls
2800 * will be made on this vnode. The file events notification module
2801 * intercepts this vnode and delivers relevant events.
2803 tvp = vn_alloc(KM_SLEEP);
2804 tvp->v_flag = VNOMOUNT|VNOMAP|VNOSWAP|VNOCACHE;
2805 vn_setops(tvp, vfs_mntdummyvnops);
2806 tvp->v_type = VREG;
2808 * The mnt dummy ops do not reference v_data.
2809 * No other module intercepting this vnode should either.
2810 * Just set it to point to itself.
2812 tvp->v_data = (caddr_t)tvp;
2813 tvp->v_vfsp = rootvfs;
2814 vfs_mntdummyvp = tvp;
2818 * performs fake read/write ops
2820 static void
2821 vfs_mnttab_rwop(int rw)
2823 struct uio uio;
2824 struct iovec iov;
2825 char buf[1];
2827 if (vfs_mntdummyvp == NULL)
2828 return;
2830 bzero(&uio, sizeof (uio));
2831 bzero(&iov, sizeof (iov));
2832 iov.iov_base = buf;
2833 iov.iov_len = 0;
2834 uio.uio_iov = &iov;
2835 uio.uio_iovcnt = 1;
2836 uio.uio_loffset = 0;
2837 uio.uio_segflg = UIO_SYSSPACE;
2838 uio.uio_resid = 0;
2839 if (rw) {
2840 (void) fop_write(vfs_mntdummyvp, &uio, 0, kcred, NULL);
2841 } else {
2842 (void) fop_read(vfs_mntdummyvp, &uio, 0, kcred, NULL);
2847 * Generate a write operation.
2849 void
2850 vfs_mnttab_writeop(void)
2852 vfs_mnttab_rwop(1);
2856 * Generate a read operation.
2858 void
2859 vfs_mnttab_readop(void)
2861 vfs_mnttab_rwop(0);
2865 * Free any mnttab information recorded in the vfs struct.
2866 * The vfs must not be on the vfs list.
2868 static void
2869 vfs_freemnttab(struct vfs *vfsp)
2871 ASSERT(!VFS_ON_LIST(vfsp));
2874 * Free device and mount point information
2876 if (vfsp->vfs_mntpt != NULL) {
2877 refstr_rele(vfsp->vfs_mntpt);
2878 vfsp->vfs_mntpt = NULL;
2880 if (vfsp->vfs_resource != NULL) {
2881 refstr_rele(vfsp->vfs_resource);
2882 vfsp->vfs_resource = NULL;
2885 * Now free mount options information
2887 vfs_freeopttbl(&vfsp->vfs_mntopts);
2891 * Return the last mnttab modification time
2893 void
2894 vfs_mnttab_modtime(timespec_t *ts)
2896 ASSERT(RW_LOCK_HELD(&vfslist));
2897 *ts = vfs_mnttab_mtime;
2901 * See if mnttab is changed
2903 void
2904 vfs_mnttab_poll(timespec_t *old, struct pollhead **phpp)
2906 int changed;
2908 *phpp = NULL;
2911 * Note: don't grab vfs list lock before accessing vfs_mnttab_mtime.
2912 * Can lead to deadlock against vfs_mnttab_modtimeupd(). It is safe
2913 * to not grab the vfs list lock because tv_sec is monotonically
2914 * increasing.
2917 changed = (old->tv_nsec != vfs_mnttab_mtime.tv_nsec) ||
2918 (old->tv_sec != vfs_mnttab_mtime.tv_sec);
2919 if (!changed) {
2920 *phpp = &vfs_pollhd;
2924 /* Provide a unique and monotonically-increasing timestamp. */
2925 void
2926 vfs_mono_time(timespec_t *ts)
2928 static volatile hrtime_t hrt; /* The saved time. */
2929 hrtime_t newhrt, oldhrt; /* For effecting the CAS. */
2930 timespec_t newts;
2933 * Try gethrestime() first, but be prepared to fabricate a sensible
2934 * answer at the first sign of any trouble.
2936 gethrestime(&newts);
2937 newhrt = ts2hrt(&newts);
2938 for (;;) {
2939 oldhrt = hrt;
2940 if (newhrt <= hrt)
2941 newhrt = hrt + 1;
2942 if (atomic_cas_64((uint64_t *)&hrt, oldhrt, newhrt) == oldhrt)
2943 break;
2945 hrt2ts(newhrt, ts);
2949 * Update the mnttab modification time and wake up any waiters for
2950 * mnttab changes
2952 void
2953 vfs_mnttab_modtimeupd()
2955 hrtime_t oldhrt, newhrt;
2957 ASSERT(RW_WRITE_HELD(&vfslist));
2958 oldhrt = ts2hrt(&vfs_mnttab_mtime);
2959 gethrestime(&vfs_mnttab_mtime);
2960 newhrt = ts2hrt(&vfs_mnttab_mtime);
2961 if (oldhrt == (hrtime_t)0)
2962 vfs_mnttab_ctime = vfs_mnttab_mtime;
2964 * Attempt to provide unique mtime (like uniqtime but not).
2966 if (newhrt == oldhrt) {
2967 newhrt++;
2968 hrt2ts(newhrt, &vfs_mnttab_mtime);
2970 pollwakeup(&vfs_pollhd, (short)POLLRDBAND);
2971 vfs_mnttab_writeop();
2975 dounmount(struct vfs *vfsp, int flag, cred_t *cr)
2977 vnode_t *coveredvp;
2978 int error;
2979 extern void teardown_vopstats(vfs_t *);
2982 * Get covered vnode. This will be NULL if the vfs is not linked
2983 * into the file system name space (i.e., domount() with MNT_NOSPICE).
2985 coveredvp = vfsp->vfs_vnodecovered;
2986 ASSERT(coveredvp == NULL || vn_vfswlock_held(coveredvp));
2989 * Purge all dnlc entries for this vfs.
2991 (void) dnlc_purge_vfsp(vfsp, 0);
2993 /* For forcible umount, skip VFS_SYNC() since it may hang */
2994 if ((flag & MS_FORCE) == 0)
2995 (void) VFS_SYNC(vfsp, 0, cr);
2998 * Lock the vfs to maintain fs status quo during unmount. This
2999 * has to be done after the sync because ufs_update tries to acquire
3000 * the vfs_reflock.
3002 vfs_lock_wait(vfsp);
3004 if (error = VFS_UNMOUNT(vfsp, flag, cr)) {
3005 vfs_unlock(vfsp);
3006 if (coveredvp != NULL)
3007 vn_vfsunlock(coveredvp);
3008 } else if (coveredvp != NULL) {
3009 teardown_vopstats(vfsp);
3011 * vfs_remove() will do a VN_RELE(vfsp->vfs_vnodecovered)
3012 * when it frees vfsp so we do a VN_HOLD() so we can
3013 * continue to use coveredvp afterwards.
3015 VN_HOLD(coveredvp);
3016 vfs_remove(vfsp);
3017 vn_vfsunlock(coveredvp);
3018 VN_RELE(coveredvp);
3019 } else {
3020 teardown_vopstats(vfsp);
3022 * Release the reference to vfs that is not linked
3023 * into the name space.
3025 vfs_unlock(vfsp);
3026 VFS_RELE(vfsp);
3028 return (error);
3033 * Vfs_unmountall() is called by uadmin() to unmount all
3034 * mounted file systems (except the root file system) during shutdown.
3035 * It follows the existing locking protocol when traversing the vfs list
3036 * to sync and unmount vfses. Even though there should be no
3037 * other thread running while the system is shutting down, it is prudent
3038 * to still follow the locking protocol.
3040 void
3041 vfs_unmountall(void)
3043 struct vfs *vfsp;
3044 struct vfs *prev_vfsp = NULL;
3045 int error;
3048 * Toss all dnlc entries now so that the per-vfs sync
3049 * and unmount operations don't have to slog through
3050 * a bunch of uninteresting vnodes over and over again.
3052 dnlc_purge();
3054 vfs_list_lock();
3055 for (vfsp = rootvfs->vfs_prev; vfsp != rootvfs; vfsp = prev_vfsp) {
3056 prev_vfsp = vfsp->vfs_prev;
3058 if (vfs_lock(vfsp) != 0)
3059 continue;
3060 error = vn_vfswlock(vfsp->vfs_vnodecovered);
3061 vfs_unlock(vfsp);
3062 if (error)
3063 continue;
3065 vfs_list_unlock();
3067 (void) VFS_SYNC(vfsp, SYNC_CLOSE, CRED());
3068 (void) dounmount(vfsp, 0, CRED());
3071 * Since we dropped the vfslist lock above we must
3072 * verify that next_vfsp still exists, else start over.
3074 vfs_list_lock();
3075 for (vfsp = rootvfs->vfs_prev;
3076 vfsp != rootvfs; vfsp = vfsp->vfs_prev)
3077 if (vfsp == prev_vfsp)
3078 break;
3079 if (vfsp == rootvfs && prev_vfsp != rootvfs)
3080 prev_vfsp = rootvfs->vfs_prev;
3082 vfs_list_unlock();
3086 * Called to add an entry to the end of the vfs mount in progress list
3088 void
3089 vfs_addmip(dev_t dev, struct vfs *vfsp)
3091 struct ipmnt *mipp;
3093 mipp = kmem_alloc(sizeof (struct ipmnt), KM_SLEEP);
3094 mipp->mip_next = NULL;
3095 mipp->mip_dev = dev;
3096 mipp->mip_vfsp = vfsp;
3097 mutex_enter(&vfs_miplist_mutex);
3098 if (vfs_miplist_end != NULL)
3099 vfs_miplist_end->mip_next = mipp;
3100 else
3101 vfs_miplist = mipp;
3102 vfs_miplist_end = mipp;
3103 mutex_exit(&vfs_miplist_mutex);
3107 * Called to remove an entry from the mount in progress list
3108 * Either because the mount completed or it failed.
3110 void
3111 vfs_delmip(struct vfs *vfsp)
3113 struct ipmnt *mipp, *mipprev;
3115 mutex_enter(&vfs_miplist_mutex);
3116 mipprev = NULL;
3117 for (mipp = vfs_miplist;
3118 mipp && mipp->mip_vfsp != vfsp; mipp = mipp->mip_next) {
3119 mipprev = mipp;
3121 if (mipp == NULL)
3122 return; /* shouldn't happen */
3123 if (mipp == vfs_miplist_end)
3124 vfs_miplist_end = mipprev;
3125 if (mipprev == NULL)
3126 vfs_miplist = mipp->mip_next;
3127 else
3128 mipprev->mip_next = mipp->mip_next;
3129 mutex_exit(&vfs_miplist_mutex);
3130 kmem_free(mipp, sizeof (struct ipmnt));
3134 * vfs_add is called by a specific filesystem's mount routine to add
3135 * the new vfs into the vfs list/hash and to cover the mounted-on vnode.
3136 * The vfs should already have been locked by the caller.
3138 * coveredvp is NULL if this is the root.
3140 void
3141 vfs_add(vnode_t *coveredvp, struct vfs *vfsp, int mflag)
3143 int newflag;
3145 ASSERT(vfs_lock_held(vfsp));
3146 VFS_HOLD(vfsp);
3147 newflag = vfsp->vfs_flag;
3148 if (mflag & MS_RDONLY)
3149 newflag |= VFS_RDONLY;
3150 else
3151 newflag &= ~VFS_RDONLY;
3152 if (mflag & MS_NOSUID)
3153 newflag |= (VFS_NOSETUID|VFS_NODEVICES);
3154 else
3155 newflag &= ~(VFS_NOSETUID|VFS_NODEVICES);
3156 if (mflag & MS_NOMNTTAB)
3157 newflag |= VFS_NOMNTTAB;
3158 else
3159 newflag &= ~VFS_NOMNTTAB;
3161 if (coveredvp != NULL) {
3162 ASSERT(vn_vfswlock_held(coveredvp));
3163 coveredvp->v_vfsmountedhere = vfsp;
3164 VN_HOLD(coveredvp);
3166 vfsp->vfs_vnodecovered = coveredvp;
3167 vfsp->vfs_flag = newflag;
3169 vfs_list_add(vfsp);
3173 * Remove a vfs from the vfs list, null out the pointer from the
3174 * covered vnode to the vfs (v_vfsmountedhere), and null out the pointer
3175 * from the vfs to the covered vnode (vfs_vnodecovered). Release the
3176 * reference to the vfs and to the covered vnode.
3178 * Called from dounmount after it's confirmed with the file system
3179 * that the unmount is legal.
3181 void
3182 vfs_remove(struct vfs *vfsp)
3184 vnode_t *vp;
3186 ASSERT(vfs_lock_held(vfsp));
3189 * Can't unmount root. Should never happen because fs will
3190 * be busy.
3192 if (vfsp == rootvfs)
3193 panic("vfs_remove: unmounting root");
3195 vfs_list_remove(vfsp);
3198 * Unhook from the file system name space.
3200 vp = vfsp->vfs_vnodecovered;
3201 ASSERT(vn_vfswlock_held(vp));
3202 vp->v_vfsmountedhere = NULL;
3203 vfsp->vfs_vnodecovered = NULL;
3204 VN_RELE(vp);
3207 * Release lock and wakeup anybody waiting.
3209 vfs_unlock(vfsp);
3210 VFS_RELE(vfsp);
3214 * Lock a filesystem to prevent access to it while mounting,
3215 * unmounting and syncing. Return EBUSY immediately if lock
3216 * can't be acquired.
3219 vfs_lock(vfs_t *vfsp)
3221 vn_vfslocks_entry_t *vpvfsentry;
3223 vpvfsentry = vn_vfslocks_getlock(vfsp);
3224 if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER))
3225 return (0);
3227 vn_vfslocks_rele(vpvfsentry);
3228 return (EBUSY);
3232 vfs_rlock(vfs_t *vfsp)
3234 vn_vfslocks_entry_t *vpvfsentry;
3236 vpvfsentry = vn_vfslocks_getlock(vfsp);
3238 if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER))
3239 return (0);
3241 vn_vfslocks_rele(vpvfsentry);
3242 return (EBUSY);
3245 void
3246 vfs_lock_wait(vfs_t *vfsp)
3248 vn_vfslocks_entry_t *vpvfsentry;
3250 vpvfsentry = vn_vfslocks_getlock(vfsp);
3251 rwst_enter(&vpvfsentry->ve_lock, RW_WRITER);
3254 void
3255 vfs_rlock_wait(vfs_t *vfsp)
3257 vn_vfslocks_entry_t *vpvfsentry;
3259 vpvfsentry = vn_vfslocks_getlock(vfsp);
3260 rwst_enter(&vpvfsentry->ve_lock, RW_READER);
3264 * Unlock a locked filesystem.
3266 void
3267 vfs_unlock(vfs_t *vfsp)
3269 vn_vfslocks_entry_t *vpvfsentry;
3272 * vfs_unlock will mimic sema_v behaviour to fix 4748018.
3273 * And these changes should remain for the patch changes as it is.
3275 if (panicstr)
3276 return;
3279 * ve_refcount needs to be dropped twice here.
3280 * 1. To release refernce after a call to vfs_locks_getlock()
3281 * 2. To release the reference from the locking routines like
3282 * vfs_rlock_wait/vfs_wlock_wait/vfs_wlock etc,.
3285 vpvfsentry = vn_vfslocks_getlock(vfsp);
3286 vn_vfslocks_rele(vpvfsentry);
3288 rwst_exit(&vpvfsentry->ve_lock);
3289 vn_vfslocks_rele(vpvfsentry);
3293 * Utility routine that allows a filesystem to construct its
3294 * fsid in "the usual way" - by munging some underlying dev_t and
3295 * the filesystem type number into the 64-bit fsid. Note that
3296 * this implicitly relies on dev_t persistence to make filesystem
3297 * id's persistent.
3299 * There's nothing to prevent an individual fs from constructing its
3300 * fsid in a different way, and indeed they should.
3302 * Since we want fsids to be 32-bit quantities (so that they can be
3303 * exported identically by either 32-bit or 64-bit APIs, as well as
3304 * the fact that fsid's are "known" to NFS), we compress the device
3305 * number given down to 32-bits, and panic if that isn't possible.
3307 void
3308 vfs_make_fsid(fsid_t *fsi, dev_t dev, int val)
3310 if (!cmpldev((dev32_t *)&fsi->val[0], dev))
3311 panic("device number too big for fsid!");
3312 fsi->val[1] = val;
3316 vfs_lock_held(vfs_t *vfsp)
3318 int held;
3319 vn_vfslocks_entry_t *vpvfsentry;
3322 * vfs_lock_held will mimic sema_held behaviour
3323 * if panicstr is set. And these changes should remain
3324 * for the patch changes as it is.
3326 if (panicstr)
3327 return (1);
3329 vpvfsentry = vn_vfslocks_getlock(vfsp);
3330 held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER);
3332 vn_vfslocks_rele(vpvfsentry);
3333 return (held);
3336 struct _kthread *
3337 vfs_lock_owner(vfs_t *vfsp)
3339 struct _kthread *owner;
3340 vn_vfslocks_entry_t *vpvfsentry;
3343 * vfs_wlock_held will mimic sema_held behaviour
3344 * if panicstr is set. And these changes should remain
3345 * for the patch changes as it is.
3347 if (panicstr)
3348 return (NULL);
3350 vpvfsentry = vn_vfslocks_getlock(vfsp);
3351 owner = rwst_owner(&vpvfsentry->ve_lock);
3353 vn_vfslocks_rele(vpvfsentry);
3354 return (owner);
3358 * vfs list locking.
3360 * Rather than manipulate the vfslist lock directly, we abstract into lock
3361 * and unlock routines to allow the locking implementation to be changed for
3362 * clustering.
3364 * Whenever the vfs list is modified through its hash links, the overall list
3365 * lock must be obtained before locking the relevant hash bucket. But to see
3366 * whether a given vfs is on the list, it suffices to obtain the lock for the
3367 * hash bucket without getting the overall list lock. (See getvfs() below.)
3370 void
3371 vfs_list_lock()
3373 rw_enter(&vfslist, RW_WRITER);
3376 void
3377 vfs_list_read_lock()
3379 rw_enter(&vfslist, RW_READER);
3382 void
3383 vfs_list_unlock()
3385 rw_exit(&vfslist);
3389 * Low level worker routines for adding entries to and removing entries from
3390 * the vfs list.
3393 static void
3394 vfs_hash_add(struct vfs *vfsp, int insert_at_head)
3396 int vhno;
3397 struct vfs **hp;
3398 dev_t dev;
3400 ASSERT(RW_WRITE_HELD(&vfslist));
3402 dev = expldev(vfsp->vfs_fsid.val[0]);
3403 vhno = VFSHASH(getmajor(dev), getminor(dev));
3405 mutex_enter(&rvfs_list[vhno].rvfs_lock);
3408 * Link into the hash table, inserting it at the end, so that LOFS
3409 * with the same fsid as UFS (or other) file systems will not hide the
3410 * UFS.
3412 if (insert_at_head) {
3413 vfsp->vfs_hash = rvfs_list[vhno].rvfs_head;
3414 rvfs_list[vhno].rvfs_head = vfsp;
3415 } else {
3416 for (hp = &rvfs_list[vhno].rvfs_head; *hp != NULL;
3417 hp = &(*hp)->vfs_hash)
3418 continue;
3420 * hp now contains the address of the pointer to update
3421 * to effect the insertion.
3423 vfsp->vfs_hash = NULL;
3424 *hp = vfsp;
3427 rvfs_list[vhno].rvfs_len++;
3428 mutex_exit(&rvfs_list[vhno].rvfs_lock);
3432 static void
3433 vfs_hash_remove(struct vfs *vfsp)
3435 int vhno;
3436 struct vfs *tvfsp;
3437 dev_t dev;
3439 ASSERT(RW_WRITE_HELD(&vfslist));
3441 dev = expldev(vfsp->vfs_fsid.val[0]);
3442 vhno = VFSHASH(getmajor(dev), getminor(dev));
3444 mutex_enter(&rvfs_list[vhno].rvfs_lock);
3447 * Remove from hash.
3449 if (rvfs_list[vhno].rvfs_head == vfsp) {
3450 rvfs_list[vhno].rvfs_head = vfsp->vfs_hash;
3451 rvfs_list[vhno].rvfs_len--;
3452 goto foundit;
3454 for (tvfsp = rvfs_list[vhno].rvfs_head; tvfsp != NULL;
3455 tvfsp = tvfsp->vfs_hash) {
3456 if (tvfsp->vfs_hash == vfsp) {
3457 tvfsp->vfs_hash = vfsp->vfs_hash;
3458 rvfs_list[vhno].rvfs_len--;
3459 goto foundit;
3462 cmn_err(CE_WARN, "vfs_list_remove: vfs not found in hash");
3464 foundit:
3466 mutex_exit(&rvfs_list[vhno].rvfs_lock);
3470 void
3471 vfs_list_add(struct vfs *vfsp)
3473 zone_t *zone;
3476 * Typically, the vfs_t will have been created on behalf of the file
3477 * system in vfs_init, where it will have been provided with a
3478 * vfs_impl_t. This, however, might be lacking if the vfs_t was created
3479 * by an unbundled file system. We therefore check for such an example
3480 * before stamping the vfs_t with its creation time for the benefit of
3481 * mntfs.
3483 if (vfsp->vfs_implp == NULL)
3484 vfsimpl_setup(vfsp);
3485 vfs_mono_time(&vfsp->vfs_hrctime);
3488 * The zone that owns the mount is the one that performed the mount.
3489 * Note that this isn't necessarily the same as the zone mounted into.
3490 * The corresponding zone_rele_ref() will be done when the vfs_t
3491 * is being free'd.
3493 vfsp->vfs_zone = curproc->p_zone;
3494 zone_init_ref(&vfsp->vfs_implp->vi_zone_ref);
3495 zone_hold_ref(vfsp->vfs_zone, &vfsp->vfs_implp->vi_zone_ref,
3496 ZONE_REF_VFS);
3499 * Find the zone mounted into, and put this mount on its vfs list.
3501 zone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
3502 ASSERT(zone != NULL);
3504 * Special casing for the root vfs. This structure is allocated
3505 * statically and hooked onto rootvfs at link time. During the
3506 * vfs_mountroot call at system startup time, the root file system's
3507 * VFS_MOUNTROOT routine will call vfs_add with this root vfs struct
3508 * as argument. The code below must detect and handle this special
3509 * case. The only apparent justification for this special casing is
3510 * to ensure that the root file system appears at the head of the
3511 * list.
3513 * XXX: I'm assuming that it's ok to do normal list locking when
3514 * adding the entry for the root file system (this used to be
3515 * done with no locks held).
3517 vfs_list_lock();
3519 * Link into the vfs list proper.
3521 if (vfsp == &root) {
3523 * Assert: This vfs is already on the list as its first entry.
3524 * Thus, there's nothing to do.
3526 ASSERT(rootvfs == vfsp);
3528 * Add it to the head of the global zone's vfslist.
3530 ASSERT(zone == global_zone);
3531 ASSERT(zone->zone_vfslist == NULL);
3532 zone->zone_vfslist = vfsp;
3533 } else {
3535 * Link to end of list using vfs_prev (as rootvfs is now a
3536 * doubly linked circular list) so list is in mount order for
3537 * mnttab use.
3539 rootvfs->vfs_prev->vfs_next = vfsp;
3540 vfsp->vfs_prev = rootvfs->vfs_prev;
3541 rootvfs->vfs_prev = vfsp;
3542 vfsp->vfs_next = rootvfs;
3545 * Do it again for the zone-private list (which may be NULL).
3547 if (zone->zone_vfslist == NULL) {
3548 ASSERT(zone != global_zone);
3549 zone->zone_vfslist = vfsp;
3550 } else {
3551 zone->zone_vfslist->vfs_zone_prev->vfs_zone_next = vfsp;
3552 vfsp->vfs_zone_prev = zone->zone_vfslist->vfs_zone_prev;
3553 zone->zone_vfslist->vfs_zone_prev = vfsp;
3554 vfsp->vfs_zone_next = zone->zone_vfslist;
3559 * Link into the hash table, inserting it at the end, so that LOFS
3560 * with the same fsid as UFS (or other) file systems will not hide
3561 * the UFS.
3563 vfs_hash_add(vfsp, 0);
3566 * update the mnttab modification time
3568 vfs_mnttab_modtimeupd();
3569 vfs_list_unlock();
3570 zone_rele(zone);
3573 void
3574 vfs_list_remove(struct vfs *vfsp)
3576 zone_t *zone;
3578 zone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
3579 ASSERT(zone != NULL);
3581 * Callers are responsible for preventing attempts to unmount the
3582 * root.
3584 ASSERT(vfsp != rootvfs);
3586 vfs_list_lock();
3589 * Remove from hash.
3591 vfs_hash_remove(vfsp);
3594 * Remove from vfs list.
3596 vfsp->vfs_prev->vfs_next = vfsp->vfs_next;
3597 vfsp->vfs_next->vfs_prev = vfsp->vfs_prev;
3598 vfsp->vfs_next = vfsp->vfs_prev = NULL;
3601 * Remove from zone-specific vfs list.
3603 if (zone->zone_vfslist == vfsp)
3604 zone->zone_vfslist = vfsp->vfs_zone_next;
3606 if (vfsp->vfs_zone_next == vfsp) {
3607 ASSERT(vfsp->vfs_zone_prev == vfsp);
3608 ASSERT(zone->zone_vfslist == vfsp);
3609 zone->zone_vfslist = NULL;
3612 vfsp->vfs_zone_prev->vfs_zone_next = vfsp->vfs_zone_next;
3613 vfsp->vfs_zone_next->vfs_zone_prev = vfsp->vfs_zone_prev;
3614 vfsp->vfs_zone_next = vfsp->vfs_zone_prev = NULL;
3617 * update the mnttab modification time
3619 vfs_mnttab_modtimeupd();
3620 vfs_list_unlock();
3621 zone_rele(zone);
3624 struct vfs *
3625 getvfs(fsid_t *fsid)
3627 struct vfs *vfsp;
3628 int val0 = fsid->val[0];
3629 int val1 = fsid->val[1];
3630 dev_t dev = expldev(val0);
3631 int vhno = VFSHASH(getmajor(dev), getminor(dev));
3632 kmutex_t *hmp = &rvfs_list[vhno].rvfs_lock;
3634 mutex_enter(hmp);
3635 for (vfsp = rvfs_list[vhno].rvfs_head; vfsp; vfsp = vfsp->vfs_hash) {
3636 if (vfsp->vfs_fsid.val[0] == val0 &&
3637 vfsp->vfs_fsid.val[1] == val1) {
3638 VFS_HOLD(vfsp);
3639 mutex_exit(hmp);
3640 return (vfsp);
3643 mutex_exit(hmp);
3644 return (NULL);
3648 * Search the vfs mount in progress list for a specified device/vfs entry.
3649 * Returns 0 if the first entry in the list that the device matches has the
3650 * given vfs pointer as well. If the device matches but a different vfs
3651 * pointer is encountered in the list before the given vfs pointer then
3652 * a 1 is returned.
3656 vfs_devmounting(dev_t dev, struct vfs *vfsp)
3658 int retval = 0;
3659 struct ipmnt *mipp;
3661 mutex_enter(&vfs_miplist_mutex);
3662 for (mipp = vfs_miplist; mipp != NULL; mipp = mipp->mip_next) {
3663 if (mipp->mip_dev == dev) {
3664 if (mipp->mip_vfsp != vfsp)
3665 retval = 1;
3666 break;
3669 mutex_exit(&vfs_miplist_mutex);
3670 return (retval);
3674 * Search the vfs list for a specified device. Returns 1, if entry is found
3675 * or 0 if no suitable entry is found.
3679 vfs_devismounted(dev_t dev)
3681 struct vfs *vfsp;
3682 int found;
3684 vfs_list_read_lock();
3685 vfsp = rootvfs;
3686 found = 0;
3687 do {
3688 if (vfsp->vfs_dev == dev) {
3689 found = 1;
3690 break;
3692 vfsp = vfsp->vfs_next;
3693 } while (vfsp != rootvfs);
3695 vfs_list_unlock();
3696 return (found);
3700 * Search the vfs list for a specified device. Returns a pointer to it
3701 * or NULL if no suitable entry is found. The caller of this routine
3702 * is responsible for releasing the returned vfs pointer.
3704 struct vfs *
3705 vfs_dev2vfsp(dev_t dev)
3707 struct vfs *vfsp;
3708 int found;
3710 vfs_list_read_lock();
3711 vfsp = rootvfs;
3712 found = 0;
3713 do {
3715 * The following could be made more efficient by making
3716 * the entire loop use vfs_zone_next if the call is from
3717 * a zone. The only callers, however, ustat(2) and
3718 * umount2(2), don't seem to justify the added
3719 * complexity at present.
3721 if (vfsp->vfs_dev == dev &&
3722 ZONE_PATH_VISIBLE(refstr_value(vfsp->vfs_mntpt),
3723 curproc->p_zone)) {
3724 VFS_HOLD(vfsp);
3725 found = 1;
3726 break;
3728 vfsp = vfsp->vfs_next;
3729 } while (vfsp != rootvfs);
3730 vfs_list_unlock();
3731 return (found ? vfsp: NULL);
3735 * Search the vfs list for a specified mntpoint. Returns a pointer to it
3736 * or NULL if no suitable entry is found. The caller of this routine
3737 * is responsible for releasing the returned vfs pointer.
3739 * Note that if multiple mntpoints match, the last one matching is
3740 * returned in an attempt to return the "top" mount when overlay
3741 * mounts are covering the same mount point. This is accomplished by starting
3742 * at the end of the list and working our way backwards, stopping at the first
3743 * matching mount.
3745 struct vfs *
3746 vfs_mntpoint2vfsp(const char *mp)
3748 struct vfs *vfsp;
3749 struct vfs *retvfsp = NULL;
3750 zone_t *zone = curproc->p_zone;
3751 struct vfs *list;
3753 vfs_list_read_lock();
3754 if (getzoneid() == GLOBAL_ZONEID) {
3756 * The global zone may see filesystems in any zone.
3758 vfsp = rootvfs->vfs_prev;
3759 do {
3760 if (strcmp(refstr_value(vfsp->vfs_mntpt), mp) == 0) {
3761 retvfsp = vfsp;
3762 break;
3764 vfsp = vfsp->vfs_prev;
3765 } while (vfsp != rootvfs->vfs_prev);
3766 } else if ((list = zone->zone_vfslist) != NULL) {
3767 const char *mntpt;
3769 vfsp = list->vfs_zone_prev;
3770 do {
3771 mntpt = refstr_value(vfsp->vfs_mntpt);
3772 mntpt = ZONE_PATH_TRANSLATE(mntpt, zone);
3773 if (strcmp(mntpt, mp) == 0) {
3774 retvfsp = vfsp;
3775 break;
3777 vfsp = vfsp->vfs_zone_prev;
3778 } while (vfsp != list->vfs_zone_prev);
3780 if (retvfsp)
3781 VFS_HOLD(retvfsp);
3782 vfs_list_unlock();
3783 return (retvfsp);
3787 * Search the vfs list for a specified vfsops.
3788 * if vfs entry is found then return 1, else 0.
3791 vfs_opsinuse(vfsops_t *ops)
3793 struct vfs *vfsp;
3794 int found;
3796 vfs_list_read_lock();
3797 vfsp = rootvfs;
3798 found = 0;
3799 do {
3800 if (vfs_getops(vfsp) == ops) {
3801 found = 1;
3802 break;
3804 vfsp = vfsp->vfs_next;
3805 } while (vfsp != rootvfs);
3806 vfs_list_unlock();
3807 return (found);
3811 * Allocate an entry in vfssw for a file system type
3813 struct vfssw *
3814 allocate_vfssw(const char *type)
3816 struct vfssw *vswp;
3818 if (type[0] == '\0' || strlen(type) + 1 > _ST_FSTYPSZ) {
3820 * The vfssw table uses the empty string to identify an
3821 * available entry; we cannot add any type which has
3822 * a leading NUL. The string length is limited to
3823 * the size of the st_fstype array in struct stat.
3825 return (NULL);
3828 ASSERT(VFSSW_WRITE_LOCKED());
3829 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++)
3830 if (!ALLOCATED_VFSSW(vswp)) {
3831 vswp->vsw_name = kmem_alloc(strlen(type) + 1, KM_SLEEP);
3832 (void) strcpy(vswp->vsw_name, type);
3833 ASSERT(vswp->vsw_count == 0);
3834 vswp->vsw_count = 1;
3835 mutex_init(&vswp->vsw_lock, NULL, MUTEX_DEFAULT, NULL);
3836 return (vswp);
3838 return (NULL);
3842 * Impose additional layer of translation between vfstype names
3843 * and module names in the filesystem.
3845 static const char *
3846 vfs_to_modname(const char *vfstype)
3848 if (strcmp(vfstype, "proc") == 0) {
3849 vfstype = "procfs";
3850 } else if (strcmp(vfstype, "fd") == 0) {
3851 vfstype = "fdfs";
3852 } else if (strncmp(vfstype, "nfs", 3) == 0) {
3853 vfstype = "nfs";
3856 return (vfstype);
3860 * Find a vfssw entry given a file system type name.
3861 * Try to autoload the filesystem if it's not found.
3862 * If it's installed, return the vfssw locked to prevent unloading.
3864 struct vfssw *
3865 vfs_getvfssw(const char *type)
3867 struct vfssw *vswp;
3868 const char *modname;
3870 RLOCK_VFSSW();
3871 vswp = vfs_getvfsswbyname(type);
3872 modname = vfs_to_modname(type);
3874 if (rootdir == NULL) {
3876 * If we haven't yet loaded the root file system, then our
3877 * _init won't be called until later. Allocate vfssw entry,
3878 * because mod_installfs won't be called.
3880 if (vswp == NULL) {
3881 RUNLOCK_VFSSW();
3882 WLOCK_VFSSW();
3883 if ((vswp = vfs_getvfsswbyname(type)) == NULL) {
3884 if ((vswp = allocate_vfssw(type)) == NULL) {
3885 WUNLOCK_VFSSW();
3886 return (NULL);
3889 WUNLOCK_VFSSW();
3890 RLOCK_VFSSW();
3892 if (!VFS_INSTALLED(vswp)) {
3893 RUNLOCK_VFSSW();
3894 (void) modloadonly("fs", modname);
3895 } else
3896 RUNLOCK_VFSSW();
3897 return (vswp);
3901 * Try to load the filesystem. Before calling modload(), we drop
3902 * our lock on the VFS switch table, and pick it up after the
3903 * module is loaded. However, there is a potential race: the
3904 * module could be unloaded after the call to modload() completes
3905 * but before we pick up the lock and drive on. Therefore,
3906 * we keep reloading the module until we've loaded the module
3907 * _and_ we have the lock on the VFS switch table.
3909 while (vswp == NULL || !VFS_INSTALLED(vswp)) {
3910 RUNLOCK_VFSSW();
3911 if (modload("fs", modname) == -1)
3912 return (NULL);
3913 RLOCK_VFSSW();
3914 if (vswp == NULL)
3915 if ((vswp = vfs_getvfsswbyname(type)) == NULL)
3916 break;
3918 RUNLOCK_VFSSW();
3920 return (vswp);
3924 * Find a vfssw entry given a file system type name.
3926 struct vfssw *
3927 vfs_getvfsswbyname(const char *type)
3929 struct vfssw *vswp;
3931 ASSERT(VFSSW_LOCKED());
3932 if (type == NULL || *type == '\0')
3933 return (NULL);
3935 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
3936 if (strcmp(type, vswp->vsw_name) == 0) {
3937 vfs_refvfssw(vswp);
3938 return (vswp);
3942 return (NULL);
3946 * Find a vfssw entry given a set of vfsops.
3948 struct vfssw *
3949 vfs_getvfsswbyvfsops(vfsops_t *vfsops)
3951 struct vfssw *vswp;
3953 RLOCK_VFSSW();
3954 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
3955 if (ALLOCATED_VFSSW(vswp) && &vswp->vsw_vfsops == vfsops) {
3956 vfs_refvfssw(vswp);
3957 RUNLOCK_VFSSW();
3958 return (vswp);
3961 RUNLOCK_VFSSW();
3963 return (NULL);
3967 * Reference a vfssw entry.
3969 void
3970 vfs_refvfssw(struct vfssw *vswp)
3973 mutex_enter(&vswp->vsw_lock);
3974 vswp->vsw_count++;
3975 mutex_exit(&vswp->vsw_lock);
3979 * Unreference a vfssw entry.
3981 void
3982 vfs_unrefvfssw(struct vfssw *vswp)
3985 mutex_enter(&vswp->vsw_lock);
3986 vswp->vsw_count--;
3987 mutex_exit(&vswp->vsw_lock);
3990 static int sync_retries = 20; /* number of retries when not making progress */
3991 static int sync_triesleft; /* portion of sync_retries remaining */
3993 static pgcnt_t old_pgcnt, new_pgcnt;
3994 static int new_bufcnt, old_bufcnt;
3997 * Sync all of the mounted filesystems, and then wait for the actual i/o to
3998 * complete. We wait by counting the number of dirty pages and buffers,
3999 * pushing them out using bio_busy() and page_busy(), and then counting again.
4000 * This routine is used during the uadmin A_SHUTDOWN code. It should only
4001 * be used after some higher-level mechanism has quiesced the system so that
4002 * new writes are not being initiated while we are waiting for completion.
4004 * To ensure finite running time, our algorithm uses sync_triesleft (a progress
4005 * counter used by the vfs_syncall() loop below). It is declared above so
4006 * it can be found easily in the debugger.
4008 * The sync_triesleft counter is updated by vfs_syncall() itself. If we make
4009 * sync_retries consecutive calls to bio_busy() and page_busy() without
4010 * decreasing either the number of dirty buffers or dirty pages below the
4011 * lowest count we have seen so far, we give up and return from vfs_syncall().
4013 * Each loop iteration ends with a call to delay() one second to allow time for
4014 * i/o completion and to permit the user time to read our progress messages.
4016 void
4017 vfs_syncall(void)
4019 if (rootdir == NULL && !modrootloaded)
4020 return; /* no filesystems have been loaded yet */
4022 printf("syncing file systems...");
4023 sync();
4025 sync_triesleft = sync_retries;
4027 old_bufcnt = new_bufcnt = INT_MAX;
4028 old_pgcnt = new_pgcnt = ULONG_MAX;
4030 while (sync_triesleft > 0) {
4031 old_bufcnt = MIN(old_bufcnt, new_bufcnt);
4032 old_pgcnt = MIN(old_pgcnt, new_pgcnt);
4034 new_bufcnt = bio_busy(B_TRUE);
4035 new_pgcnt = page_busy(B_TRUE);
4037 if (new_bufcnt == 0 && new_pgcnt == 0)
4038 break;
4040 if (new_bufcnt < old_bufcnt || new_pgcnt < old_pgcnt)
4041 sync_triesleft = sync_retries;
4042 else
4043 sync_triesleft--;
4045 if (new_bufcnt)
4046 printf(" [%d]", new_bufcnt);
4047 if (new_pgcnt)
4048 printf(" %lu", new_pgcnt);
4050 delay(hz);
4053 if (new_bufcnt != 0 || new_pgcnt != 0)
4054 printf(" done (not all i/o completed)\n");
4055 else
4056 printf(" done\n");
4058 delay(hz);
4062 * Map VFS flags to statvfs flags. These shouldn't really be separate
4063 * flags at all.
4065 uint_t
4066 vf_to_stf(uint_t vf)
4068 uint_t stf = 0;
4070 if (vf & VFS_RDONLY)
4071 stf |= ST_RDONLY;
4072 if (vf & VFS_NOSETUID)
4073 stf |= ST_NOSUID;
4074 if (vf & VFS_NOTRUNC)
4075 stf |= ST_NOTRUNC;
4077 return (stf);
4081 * Entries for (illegal) fstype 0.
4083 /* ARGSUSED */
4085 vfsstray_sync(struct vfs *vfsp, short arg, struct cred *cr)
4087 cmn_err(CE_PANIC, "stray vfs operation");
4088 return (0);
4092 * Entries for (illegal) fstype 0.
4095 vfsstray(void)
4097 cmn_err(CE_PANIC, "stray vfs operation");
4098 return (0);
4102 * Support for dealing with forced UFS unmount and its interaction with
4103 * LOFS. Could be used by any filesystem.
4104 * See bug 1203132.
4107 vfs_EIO(void)
4109 return (EIO);
4113 * We've gotta define the op for sync separately, since the compiler gets
4114 * confused if we mix and match ANSI and normal style prototypes when
4115 * a "short" argument is present and spits out a warning.
4117 /*ARGSUSED*/
4119 vfs_EIO_sync(struct vfs *vfsp, short arg, struct cred *cr)
4121 return (EIO);
4124 vfs_t EIO_vfs;
4125 vfsops_t *EIO_vfsops;
4128 * Called from startup() to initialize all loaded vfs's
4130 void
4131 vfsinit(void)
4133 struct vfssw *vswp;
4134 int error;
4135 extern int vopstats_enabled;
4136 extern void vopstats_startup();
4138 static const fs_operation_def_t EIO_vfsops_template[] = {
4139 VFSNAME_MOUNT, { .error = vfs_EIO },
4140 VFSNAME_UNMOUNT, { .error = vfs_EIO },
4141 VFSNAME_ROOT, { .error = vfs_EIO },
4142 VFSNAME_STATVFS, { .error = vfs_EIO },
4143 VFSNAME_SYNC, { .vfs_sync = vfs_EIO_sync },
4144 VFSNAME_VGET, { .error = vfs_EIO },
4145 VFSNAME_MOUNTROOT, { .error = vfs_EIO },
4146 VFSNAME_FREEVFS, { .error = vfs_EIO },
4147 VFSNAME_VNSTATE, { .error = vfs_EIO },
4148 NULL, NULL
4151 static const fs_operation_def_t stray_vfsops_template[] = {
4152 VFSNAME_MOUNT, { .error = vfsstray },
4153 VFSNAME_UNMOUNT, { .error = vfsstray },
4154 VFSNAME_ROOT, { .error = vfsstray },
4155 VFSNAME_STATVFS, { .error = vfsstray },
4156 VFSNAME_SYNC, { .vfs_sync = vfsstray_sync },
4157 VFSNAME_VGET, { .error = vfsstray },
4158 VFSNAME_MOUNTROOT, { .error = vfsstray },
4159 VFSNAME_FREEVFS, { .error = vfsstray },
4160 VFSNAME_VNSTATE, { .error = vfsstray },
4161 NULL, NULL
4164 /* Create vfs cache */
4165 vfs_cache = kmem_cache_create("vfs_cache", sizeof (struct vfs),
4166 sizeof (uintptr_t), NULL, NULL, NULL, NULL, NULL, 0);
4168 /* Initialize the vnode cache (file systems may use it during init). */
4169 vn_create_cache();
4171 /* Setup event monitor framework */
4172 fem_init();
4174 /* Initialize the dummy stray file system type. */
4175 error = vfs_setfsops(0, stray_vfsops_template, NULL);
4177 /* Initialize the dummy EIO file system. */
4178 error = vfs_makefsops(EIO_vfsops_template, &EIO_vfsops);
4179 if (error != 0) {
4180 cmn_err(CE_WARN, "vfsinit: bad EIO vfs ops template");
4181 /* Shouldn't happen, but not bad enough to panic */
4184 VFS_INIT(&EIO_vfs, EIO_vfsops, (caddr_t)NULL);
4187 * Default EIO_vfs.vfs_flag to VFS_UNMOUNTED so a lookup
4188 * on this vfs can immediately notice it's invalid.
4190 EIO_vfs.vfs_flag |= VFS_UNMOUNTED;
4193 * Call the init routines of non-loadable filesystems only.
4194 * Filesystems which are loaded as separate modules will be
4195 * initialized by the module loading code instead.
4198 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
4199 RLOCK_VFSSW();
4200 if (vswp->vsw_init != NULL)
4201 (*vswp->vsw_init)(vswp - vfssw, vswp->vsw_name);
4202 RUNLOCK_VFSSW();
4205 vopstats_startup();
4207 if (vopstats_enabled) {
4208 /* EIO_vfs can collect stats, but we don't retrieve them */
4209 initialize_vopstats(&EIO_vfs.vfs_vopstats);
4210 EIO_vfs.vfs_fstypevsp = NULL;
4211 EIO_vfs.vfs_vskap = NULL;
4212 EIO_vfs.vfs_flag |= VFS_STATS;
4215 xattr_init();
4217 reparse_point_init();
4220 vfs_t *
4221 vfs_alloc(int kmflag)
4223 vfs_t *vfsp;
4225 vfsp = kmem_cache_alloc(vfs_cache, kmflag);
4228 * Do the simplest initialization here.
4229 * Everything else gets done in vfs_init()
4231 bzero(vfsp, sizeof (vfs_t));
4232 return (vfsp);
4235 void
4236 vfs_free(vfs_t *vfsp)
4239 * One would be tempted to assert that "vfsp->vfs_count == 0".
4240 * The problem is that this gets called out of domount() with
4241 * a partially initialized vfs and a vfs_count of 1. This is
4242 * also called from vfs_rele() with a vfs_count of 0. We can't
4243 * call VFS_RELE() from domount() if VFS_MOUNT() hasn't successfully
4244 * returned. This is because VFS_MOUNT() fully initializes the
4245 * vfs structure and its associated data. VFS_RELE() will call
4246 * VFS_FREEVFS() which may panic the system if the data structures
4247 * aren't fully initialized from a successful VFS_MOUNT()).
4250 /* If FEM was in use, make sure everything gets cleaned up */
4251 if (vfsp->vfs_femhead) {
4252 ASSERT(vfsp->vfs_femhead->femh_list == NULL);
4253 mutex_destroy(&vfsp->vfs_femhead->femh_lock);
4254 kmem_free(vfsp->vfs_femhead, sizeof (*(vfsp->vfs_femhead)));
4255 vfsp->vfs_femhead = NULL;
4258 if (vfsp->vfs_implp)
4259 vfsimpl_teardown(vfsp);
4260 sema_destroy(&vfsp->vfs_reflock);
4261 kmem_cache_free(vfs_cache, vfsp);
4265 * Increments the vfs reference count by one atomically.
4267 void
4268 vfs_hold(vfs_t *vfsp)
4270 atomic_inc_32(&vfsp->vfs_count);
4271 ASSERT(vfsp->vfs_count != 0);
4275 * Decrements the vfs reference count by one atomically. When
4276 * vfs reference count becomes zero, it calls the file system
4277 * specific vfs_freevfs() to free up the resources.
4279 void
4280 vfs_rele(vfs_t *vfsp)
4282 ASSERT(vfsp->vfs_count != 0);
4283 if (atomic_dec_32_nv(&vfsp->vfs_count) == 0) {
4284 VFS_FREEVFS(vfsp);
4285 lofi_remove(vfsp);
4286 if (vfsp->vfs_zone)
4287 zone_rele_ref(&vfsp->vfs_implp->vi_zone_ref,
4288 ZONE_REF_VFS);
4289 vfs_freemnttab(vfsp);
4290 vfs_free(vfsp);
4295 * Generic operations vector support.
4297 * This is used to build operations vectors for both the vfs and vnode.
4298 * It's normally called only when a file system is loaded.
4300 * There are many possible algorithms for this, including the following:
4302 * (1) scan the list of known operations; for each, see if the file system
4303 * includes an entry for it, and fill it in as appropriate.
4305 * (2) set up defaults for all known operations. scan the list of ops
4306 * supplied by the file system; for each which is both supplied and
4307 * known, fill it in.
4309 * (3) sort the lists of known ops & supplied ops; scan the list, filling
4310 * in entries as we go.
4312 * we choose (1) for simplicity, and because performance isn't critical here.
4313 * note that (2) could be sped up using a precomputed hash table on known ops.
4314 * (3) could be faster than either, but only if the lists were very large or
4315 * supplied in sorted order.
4320 fs_build_vector(void *vector, int *unused_ops,
4321 const fs_operation_trans_def_t *translation,
4322 const fs_operation_def_t *operations)
4324 int i, num_trans, num_ops, used;
4327 * Count the number of translations and the number of supplied
4328 * operations.
4332 const fs_operation_trans_def_t *p;
4334 for (num_trans = 0, p = translation;
4335 p->name != NULL;
4336 num_trans++, p++)
4341 const fs_operation_def_t *p;
4343 for (num_ops = 0, p = operations;
4344 p->name != NULL;
4345 num_ops++, p++)
4349 /* Walk through each operation known to our caller. There will be */
4350 /* one entry in the supplied "translation table" for each. */
4352 used = 0;
4354 for (i = 0; i < num_trans; i++) {
4355 int j, found;
4356 char *curname;
4357 fs_generic_func_p result;
4358 fs_generic_func_p *location;
4360 curname = translation[i].name;
4362 /* Look for a matching operation in the list supplied by the */
4363 /* file system. */
4365 found = 0;
4367 for (j = 0; j < num_ops; j++) {
4368 if (strcmp(operations[j].name, curname) == 0) {
4369 used++;
4370 found = 1;
4371 break;
4376 * If the file system is using a "placeholder" for default
4377 * or error functions, grab the appropriate function out of
4378 * the translation table. If the file system didn't supply
4379 * this operation at all, use the default function.
4382 if (found) {
4383 result = operations[j].func.fs_generic;
4384 if (result == NULL) {
4385 /* Null values are PROHIBITED */
4386 return (EINVAL);
4388 } else {
4389 result = translation[i].defaultFunc;
4392 /* Now store the function into the operations vector. */
4394 location = (fs_generic_func_p *)
4395 (((char *)vector) + translation[i].offset);
4397 *location = result;
4400 *unused_ops = num_ops - used;
4402 return (0);
4405 #ifdef __sparc
4408 * Part of the implementation of booting off a mirrored root
4409 * involves a change of dev_t for the root device. To
4410 * accomplish this, first remove the existing hash table
4411 * entry for the root device, convert to the new dev_t,
4412 * then re-insert in the hash table at the head of the list.
4414 void
4415 vfs_root_redev(vfs_t *vfsp, dev_t ndev, int fstype)
4417 vfs_list_lock();
4419 vfs_hash_remove(vfsp);
4421 vfsp->vfs_dev = ndev;
4422 vfs_make_fsid(&vfsp->vfs_fsid, ndev, fstype);
4424 vfs_hash_add(vfsp, 1);
4426 vfs_list_unlock();
4429 #else /* x86 NEWBOOT */
4431 #if defined(__x86)
4432 extern int hvmboot_rootconf();
4433 #endif /* __x86 */
4435 extern ib_boot_prop_t *iscsiboot_prop;
4438 rootconf()
4440 int error;
4441 struct vfssw *vsw;
4442 extern void pm_init();
4443 char *fstyp, *fsmod;
4444 int ret = -1;
4446 getrootfs(&fstyp, &fsmod);
4448 #if defined(__x86)
4450 * hvmboot_rootconf() is defined in the hvm_bootstrap misc module,
4451 * which lives in /platform/i86hvm, and hence is only available when
4452 * booted in an x86 hvm environment. If the hvm_bootstrap misc module
4453 * is not available then the modstub for this function will return 0.
4454 * If the hvm_bootstrap misc module is available it will be loaded
4455 * and hvmboot_rootconf() will be invoked.
4457 if (error = hvmboot_rootconf())
4458 return (error);
4459 #endif /* __x86 */
4461 if (modload("fs", fsmod) == -1)
4462 panic("Cannot _init %s module", fsmod);
4464 RLOCK_VFSSW();
4465 vsw = vfs_getvfsswbyname(fstyp);
4466 RUNLOCK_VFSSW();
4467 if (vsw == NULL) {
4468 cmn_err(CE_CONT, "Cannot find %s filesystem\n", fstyp);
4469 return (ENXIO);
4471 VFS_INIT(rootvfs, &vsw->vsw_vfsops, 0);
4472 VFS_HOLD(rootvfs);
4474 /* always mount readonly first */
4475 rootvfs->vfs_flag |= VFS_RDONLY;
4477 pm_init();
4479 if (netboot && iscsiboot_prop) {
4480 cmn_err(CE_WARN, "NFS boot and iSCSI boot"
4481 " shouldn't happen in the same time");
4482 return (EINVAL);
4485 if (netboot || iscsiboot_prop) {
4486 ret = strplumb();
4487 if (ret != 0) {
4488 cmn_err(CE_WARN, "Cannot plumb network device %d", ret);
4489 return (EFAULT);
4493 if ((ret == 0) && iscsiboot_prop) {
4494 ret = modload("drv", "iscsi");
4495 /* -1 indicates fail */
4496 if (ret == -1) {
4497 cmn_err(CE_WARN, "Failed to load iscsi module");
4498 iscsi_boot_prop_free();
4499 return (EINVAL);
4500 } else {
4501 if (!i_ddi_attach_pseudo_node("iscsi")) {
4502 cmn_err(CE_WARN,
4503 "Failed to attach iscsi driver");
4504 iscsi_boot_prop_free();
4505 return (ENODEV);
4510 error = VFS_MOUNTROOT(rootvfs, ROOT_INIT);
4511 vfs_unrefvfssw(vsw);
4512 rootdev = rootvfs->vfs_dev;
4514 if (error)
4515 cmn_err(CE_CONT, "Cannot mount root on %s fstype %s\n",
4516 rootfs.bo_name, fstyp);
4517 else
4518 cmn_err(CE_CONT, "?root on %s fstype %s\n",
4519 rootfs.bo_name, fstyp);
4520 return (error);
4524 * XXX this is called by nfs only and should probably be removed
4525 * If booted with ASKNAME, prompt on the console for a filesystem
4526 * name and return it.
4528 void
4529 getfsname(char *askfor, char *name, size_t namelen)
4531 if (boothowto & RB_ASKNAME) {
4532 printf("%s name: ", askfor);
4533 console_gets(name, namelen);
4538 * Init the root filesystem type (rootfs.bo_fstype) from the "fstype"
4539 * property.
4541 * Filesystem types starting with the prefix "nfs" are diskless clients;
4542 * init the root filename name (rootfs.bo_name), too.
4544 * If we are booting via NFS we currently have these options:
4545 * nfs - dynamically choose NFS V2, V3, or V4 (default)
4546 * nfs2 - force NFS V2
4547 * nfs3 - force NFS V3
4548 * nfs4 - force NFS V4
4549 * Because we need to maintain backward compatibility with the naming
4550 * convention that the NFS V2 filesystem name is "nfs" (see vfs_conf.c)
4551 * we need to map "nfs" => "nfsdyn" and "nfs2" => "nfs". The dynamic
4552 * nfs module will map the type back to either "nfs", "nfs3", or "nfs4".
4553 * This is only for root filesystems, all other uses will expect
4554 * that "nfs" == NFS V2.
4556 static void
4557 getrootfs(char **fstypp, char **fsmodp)
4559 char *propstr = NULL;
4562 * Check fstype property; for diskless it should be one of "nfs",
4563 * "nfs2", "nfs3" or "nfs4".
4565 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4566 DDI_PROP_DONTPASS, "fstype", &propstr)
4567 == DDI_SUCCESS) {
4568 (void) strncpy(rootfs.bo_fstype, propstr, BO_MAXFSNAME);
4569 ddi_prop_free(propstr);
4572 * if the boot property 'fstype' is not set, but 'zfs-bootfs' is set,
4573 * assume the type of this root filesystem is 'zfs'.
4575 } else if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4576 DDI_PROP_DONTPASS, "zfs-bootfs", &propstr)
4577 == DDI_SUCCESS) {
4578 (void) strncpy(rootfs.bo_fstype, "zfs", BO_MAXFSNAME);
4579 ddi_prop_free(propstr);
4582 if (strncmp(rootfs.bo_fstype, "nfs", 3) != 0) {
4583 *fstypp = *fsmodp = rootfs.bo_fstype;
4584 return;
4587 ++netboot;
4589 if (strcmp(rootfs.bo_fstype, "nfs2") == 0)
4590 (void) strcpy(rootfs.bo_fstype, "nfs");
4591 else if (strcmp(rootfs.bo_fstype, "nfs") == 0)
4592 (void) strcpy(rootfs.bo_fstype, "nfsdyn");
4595 * check if path to network interface is specified in bootpath
4596 * or by a hypervisor domain configuration file.
4597 * XXPV - enable strlumb_get_netdev_path()
4599 if (ddi_prop_exists(DDI_DEV_T_ANY, ddi_root_node(), DDI_PROP_DONTPASS,
4600 "xpv-nfsroot")) {
4601 (void) strcpy(rootfs.bo_name, "/xpvd/xnf@0");
4602 } else if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4603 DDI_PROP_DONTPASS, "bootpath", &propstr)
4604 == DDI_SUCCESS) {
4605 (void) strncpy(rootfs.bo_name, propstr, BO_MAXOBJNAME);
4606 ddi_prop_free(propstr);
4607 } else {
4608 rootfs.bo_name[0] = '\0';
4610 *fstypp = rootfs.bo_fstype;
4611 *fsmodp = "nfs";
4613 #endif
4616 * VFS feature routines
4619 #define VFTINDEX(feature) (((feature) >> 32) & 0xFFFFFFFF)
4620 #define VFTBITS(feature) ((feature) & 0xFFFFFFFFLL)
4622 /* Register a feature in the vfs */
4623 void
4624 vfs_set_feature(vfs_t *vfsp, vfs_feature_t feature)
4626 /* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4627 if (vfsp->vfs_implp == NULL)
4628 return;
4630 vfsp->vfs_featureset[VFTINDEX(feature)] |= VFTBITS(feature);
4633 void
4634 vfs_clear_feature(vfs_t *vfsp, vfs_feature_t feature)
4636 /* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4637 if (vfsp->vfs_implp == NULL)
4638 return;
4639 vfsp->vfs_featureset[VFTINDEX(feature)] &= VFTBITS(~feature);
4643 * Query a vfs for a feature.
4644 * Returns 1 if feature is present, 0 if not
4647 vfs_has_feature(vfs_t *vfsp, vfs_feature_t feature)
4649 int ret = 0;
4651 /* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4652 if (vfsp->vfs_implp == NULL)
4653 return (ret);
4655 if (vfsp->vfs_featureset[VFTINDEX(feature)] & VFTBITS(feature))
4656 ret = 1;
4658 return (ret);
4662 * Propagate feature set from one vfs to another
4664 void
4665 vfs_propagate_features(vfs_t *from, vfs_t *to)
4667 int i;
4669 if (to->vfs_implp == NULL || from->vfs_implp == NULL)
4670 return;
4672 for (i = 1; i <= to->vfs_featureset[0]; i++) {
4673 to->vfs_featureset[i] = from->vfs_featureset[i];
4677 #define LOFINODE_PATH "/dev/lofi/%d"
4680 * Return the vnode for the lofi node if there's a lofi mount in place.
4681 * Returns -1 when there's no lofi node, 0 on success, and > 0 on
4682 * failure.
4685 vfs_get_lofi(vfs_t *vfsp, vnode_t **vpp)
4687 char *path = NULL;
4688 int strsize;
4689 int err;
4691 if (vfsp->vfs_lofi_id == 0) {
4692 *vpp = NULL;
4693 return (-1);
4696 strsize = snprintf(NULL, 0, LOFINODE_PATH, vfsp->vfs_lofi_id);
4697 path = kmem_alloc(strsize + 1, KM_SLEEP);
4698 (void) snprintf(path, strsize + 1, LOFINODE_PATH, vfsp->vfs_lofi_id);
4701 * We may be inside a zone, so we need to use the /dev path, but
4702 * it's created asynchronously, so we wait here.
4704 for (;;) {
4705 err = lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, vpp);
4707 if (err != ENOENT)
4708 break;
4710 if ((err = delay_sig(hz / 8)) == EINTR)
4711 break;
4714 if (err)
4715 *vpp = NULL;
4717 kmem_free(path, strsize + 1);
4718 return (err);