Merge commit '0b09d754d66bb2026be92bbbc38f7c8ba454cf0c'
[unleashed.git] / kernel / fs / vfs.c
blobb88ca74933b51af6997548f84a059efd30249248
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 2016 Joyent, Inc.
25 * Copyright 2016 Toomas Soome <tsoome@me.com>
26 * Copyright 2016 Nexenta Systems, Inc.
27 * Copyright (c) 2016, 2017 by Delphix. All rights reserved.
28 * Copyright 2017 RackTop Systems.
31 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
32 /* All Rights Reserved */
35 * University Copyright- Copyright (c) 1982, 1986, 1988
36 * The Regents of the University of California
37 * All Rights Reserved
39 * University Acknowledgment- Portions of this document are derived from
40 * software developed by the University of California, Berkeley, and its
41 * contributors.
44 #include <sys/types.h>
45 #include <sys/t_lock.h>
46 #include <sys/param.h>
47 #include <sys/errno.h>
48 #include <sys/user.h>
49 #include <sys/fstyp.h>
50 #include <sys/kmem.h>
51 #include <sys/systm.h>
52 #include <sys/proc.h>
53 #include <sys/mount.h>
54 #include <sys/vfs.h>
55 #include <sys/vfs_dispatch.h>
56 #include <sys/fem.h>
57 #include <sys/mntent.h>
58 #include <sys/stat.h>
59 #include <sys/statvfs.h>
60 #include <sys/statfs.h>
61 #include <sys/cred.h>
62 #include <sys/vnode.h>
63 #include <sys/rwstlock.h>
64 #include <sys/dnlc.h>
65 #include <sys/file.h>
66 #include <sys/time.h>
67 #include <sys/atomic.h>
68 #include <sys/cmn_err.h>
69 #include <sys/buf.h>
70 #include <sys/swap.h>
71 #include <sys/debug.h>
72 #include <sys/vnode.h>
73 #include <sys/modctl.h>
74 #include <sys/ddi.h>
75 #include <sys/pathname.h>
76 #include <sys/bootconf.h>
77 #include <sys/dumphdr.h>
78 #include <sys/poll.h>
79 #include <sys/sunddi.h>
80 #include <sys/sysmacros.h>
81 #include <sys/zone.h>
82 #include <sys/policy.h>
83 #include <sys/ctfs.h>
84 #include <sys/objfs.h>
85 #include <sys/console.h>
86 #include <sys/reboot.h>
87 #include <sys/attr.h>
88 #include <sys/zio.h>
89 #include <sys/spa.h>
90 #include <sys/lofi.h>
91 #include <sys/bootprops.h>
93 #include <vm/page.h>
95 #include <sys/fs_subr.h>
96 /* Private interfaces to create vopstats-related data structures */
97 extern void initialize_vopstats(vopstats_t *);
98 extern vopstats_t *get_fstype_vopstats(struct vfs *, struct vfssw *);
99 extern vsk_anchor_t *get_vskstat_anchor(struct vfs *);
101 static void vfs_clearmntopt_nolock(mntopts_t *, const char *, int);
102 static void vfs_setmntopt_nolock(mntopts_t *, const char *,
103 const char *, int, int);
104 static int vfs_optionisset_nolock(const mntopts_t *, const char *, char **);
105 static void vfs_freemnttab(struct vfs *);
106 static void vfs_freeopt(mntopt_t *);
107 static void vfs_swapopttbl_nolock(mntopts_t *, mntopts_t *);
108 static void vfs_swapopttbl(mntopts_t *, mntopts_t *);
109 static void vfs_copyopttbl_extend(const mntopts_t *, mntopts_t *, int);
110 static void vfs_createopttbl_extend(mntopts_t *, const char *,
111 const mntopts_t *);
112 static char **vfs_copycancelopt_extend(char **const, int);
113 static void vfs_freecancelopt(char **);
114 static void getrootfs(char **, char **);
115 static int getmacpath(dev_info_t *, void *);
116 static void vfs_mnttabvp_setup(void);
118 struct ipmnt {
119 struct ipmnt *mip_next;
120 dev_t mip_dev;
121 struct vfs *mip_vfsp;
124 static kmutex_t vfs_miplist_mutex;
125 static struct ipmnt *vfs_miplist = NULL;
126 static struct ipmnt *vfs_miplist_end = NULL;
128 static kmem_cache_t *vfs_cache; /* Pointer to VFS kmem cache */
131 * VFS global data.
133 vnode_t *rootdir; /* pointer to root inode vnode. */
134 vnode_t *devicesdir; /* pointer to inode of devices root */
135 vnode_t *devdir; /* pointer to inode of dev root */
137 char *server_rootpath; /* root path for diskless clients */
138 char *server_hostname; /* hostname of diskless server */
140 static struct vfs root;
141 static struct vfs devices;
142 static struct vfs dev;
143 struct vfs *rootvfs = &root; /* pointer to root vfs; head of VFS list. */
144 rvfs_t *rvfs_list; /* array of vfs ptrs for vfs hash list */
145 int vfshsz = 512; /* # of heads/locks in vfs hash arrays */
146 /* must be power of 2! */
147 timespec_t vfs_mnttab_ctime; /* mnttab created time */
148 timespec_t vfs_mnttab_mtime; /* mnttab last modified time */
149 char *vfs_dummyfstype = "\0";
150 struct pollhead vfs_pollhd; /* for mnttab pollers */
151 struct vnode *vfs_mntdummyvp; /* to fake mnttab read/write for file events */
152 int mntfstype; /* will be set once mnt fs is mounted */
155 * Table for generic options recognized in the VFS layer and acted
156 * on at this level before parsing file system specific options.
157 * The nosuid option is stronger than any of the devices and setuid
158 * options, so those are canceled when nosuid is seen.
160 * All options which are added here need to be added to the
161 * list of standard options in usr/src/cmd/fs.d/fslib.c as well.
164 * VFS Mount options table
166 static char *ro_cancel[] = { MNTOPT_RW, NULL };
167 static char *rw_cancel[] = { MNTOPT_RO, NULL };
168 static char *suid_cancel[] = { MNTOPT_NOSUID, NULL };
169 static char *nosuid_cancel[] = { MNTOPT_SUID, MNTOPT_DEVICES, MNTOPT_NODEVICES,
170 MNTOPT_NOSETUID, MNTOPT_SETUID, NULL };
171 static char *devices_cancel[] = { MNTOPT_NODEVICES, NULL };
172 static char *nodevices_cancel[] = { MNTOPT_DEVICES, NULL };
173 static char *setuid_cancel[] = { MNTOPT_NOSETUID, NULL };
174 static char *nosetuid_cancel[] = { MNTOPT_SETUID, NULL };
175 static char *nbmand_cancel[] = { MNTOPT_NONBMAND, NULL };
176 static char *nonbmand_cancel[] = { MNTOPT_NBMAND, NULL };
177 static char *exec_cancel[] = { MNTOPT_NOEXEC, NULL };
178 static char *noexec_cancel[] = { MNTOPT_EXEC, NULL };
180 static const mntopt_t mntopts[] = {
182 * option name cancel options default arg flags
184 { MNTOPT_REMOUNT, NULL, NULL,
185 MO_NODISPLAY, NULL },
186 { MNTOPT_RO, ro_cancel, NULL, 0,
187 NULL },
188 { MNTOPT_RW, rw_cancel, NULL, 0,
189 NULL },
190 { MNTOPT_SUID, suid_cancel, NULL, 0,
191 NULL },
192 { MNTOPT_NOSUID, nosuid_cancel, NULL, 0,
193 NULL },
194 { MNTOPT_DEVICES, devices_cancel, NULL, 0,
195 NULL },
196 { MNTOPT_NODEVICES, nodevices_cancel, NULL, 0,
197 NULL },
198 { MNTOPT_SETUID, setuid_cancel, NULL, 0,
199 NULL },
200 { MNTOPT_NOSETUID, nosetuid_cancel, NULL, 0,
201 NULL },
202 { MNTOPT_NBMAND, nbmand_cancel, NULL, 0,
203 NULL },
204 { MNTOPT_NONBMAND, nonbmand_cancel, NULL, 0,
205 NULL },
206 { MNTOPT_EXEC, exec_cancel, NULL, 0,
207 NULL },
208 { MNTOPT_NOEXEC, noexec_cancel, NULL, 0,
209 NULL },
212 const mntopts_t vfs_mntopts = {
213 sizeof (mntopts) / sizeof (mntopt_t),
214 (mntopt_t *)&mntopts[0]
218 * File system operation dispatch functions.
222 fsop_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
224 return fsop_mount_dispatch(vfsp, mvp, uap, cr, true);
228 fsop_unmount(vfs_t *vfsp, int flag, cred_t *cr)
230 return fsop_unmount_dispatch(vfsp, flag, cr, true);
234 fsop_root(vfs_t *vfsp, vnode_t **vpp)
236 refstr_t *mntpt;
237 int ret;
239 ret = fsop_root_dispatch(vfsp, vpp, true);
242 * Make sure this root has a path. With lofs, it is possible to have
243 * a NULL mountpoint.
245 if (ret == 0 && vfsp->vfs_mntpt != NULL &&
246 (*vpp)->v_path == vn_vpath_empty) {
247 const char *path;
249 mntpt = vfs_getmntpoint(vfsp);
250 path = refstr_value(mntpt);
251 vn_setpath_str(*vpp, path, strlen(path));
252 refstr_rele(mntpt);
255 return (ret);
259 fsop_statfs(vfs_t *vfsp, statvfs64_t *sp)
261 return fsop_statfs_dispatch(vfsp, sp, true);
265 fsop_sync(vfs_t *vfsp, short flag, cred_t *cr)
267 return fsop_sync_dispatch(vfsp, flag, cr, true);
271 fsop_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
274 * In order to handle system attribute fids in a manner
275 * transparent to the underlying fs, we embed the fid for
276 * the sysattr parent object in the sysattr fid and tack on
277 * some extra bytes that only the sysattr layer knows about.
279 * This guarantees that sysattr fids are larger than other fids
280 * for this vfs. If the vfs supports the sysattr view interface
281 * (as indicated by VFSFT_SYSATTR_VIEWS), we cannot have a size
282 * collision with XATTR_FIDSZ.
284 if (vfs_has_feature(vfsp, VFSFT_SYSATTR_VIEWS) &&
285 fidp->fid_len == XATTR_FIDSZ)
286 return (xattr_dir_vget(vfsp, vpp, fidp));
288 return fsop_vget_dispatch(vfsp, vpp, fidp, true);
292 fsop_mountroot(vfs_t *vfsp, enum whymountroot reason)
294 return fsop_mountroot_dispatch(vfsp, reason, true);
297 void
298 fsop_freefs(vfs_t *vfsp)
300 fsop_freefs_dispatch(vfsp, true);
304 fsop_vnstate(vfs_t *vfsp, vnode_t *vp, vntrans_t nstate)
306 return fsop_vnstate_dispatch(vfsp, vp, nstate, true);
310 fsop_sync_by_kind(int fstype, short flag, cred_t *cr)
312 ASSERT((fstype >= 0) && (fstype < nfstype));
314 if (!ALLOCATED_VFSSW(&vfssw[fstype]) || !VFS_INSTALLED(&vfssw[fstype]))
315 return ENOTSUP;
317 if (vfssw[fstype].vsw_vfsops.vfs_sync == NULL)
318 return ENOSYS;
320 return vfssw[fstype].vsw_vfsops.vfs_sync(NULL, flag, cr);
324 * File system initialization. vfs_setfsops() must be called from a file
325 * system's init routine.
328 void
329 zfs_boot_init(void)
331 if (strcmp(rootfs.bo_fstype, MNTTYPE_ZFS) == 0)
332 spa_boot_init();
336 vfs_setfsops(int fstype, const struct vfsops *ops)
339 * Verify that fstype refers to a valid fs. Note that
340 * 0 is valid since it's used to set "stray" ops.
342 if ((fstype < 0) || (fstype >= nfstype))
343 return (EINVAL);
345 if (!ALLOCATED_VFSSW(&vfssw[fstype]))
346 return (EINVAL);
348 vfssw[fstype].vsw_vfsops = *ops;
349 vfssw[fstype].vsw_flag |= VSW_INSTALLED;
351 return (0);
355 * Since the vfsops structure is part of the vfssw table and wasn't
356 * really allocated, we're not really freeing anything. However, we need to
357 * take care of a little bookkeeping.
360 vfs_freevfsops_by_type(int fstype)
363 /* Verify that fstype refers to a loaded fs (and not fsid 0). */
364 if ((fstype <= 0) || (fstype >= nfstype))
365 return (EINVAL);
367 WLOCK_VFSSW();
368 if ((vfssw[fstype].vsw_flag & VSW_INSTALLED) == 0) {
369 WUNLOCK_VFSSW();
370 return (EINVAL);
373 vfssw[fstype].vsw_flag &= ~VSW_INSTALLED;
374 WUNLOCK_VFSSW();
376 return (0);
379 /* Support routines used to reference vfs_op */
381 /* Set the operations vector for a vfs */
382 void
383 vfs_setops(struct vfs *vfs, const struct vfsops *ops)
385 vfs->vfs_op = ops;
388 /* Retrieve the operations vector for a vfs */
389 const struct vfsops *
390 vfs_getops(struct vfs *vfs)
392 return vfs->vfs_op;
396 * Returns non-zero (1) if the vfsops matches that of the vfs.
397 * Returns zero (0) if not.
400 vfs_matchops(struct vfs *vfs, const struct vfsops *ops)
402 return (vfs_getops(vfs) == ops);
406 * Returns non-zero (1) if the file system has installed a non-default,
407 * non-error vfs_sync routine. Returns zero (0) otherwise.
410 vfs_can_sync(vfs_t *vfsp)
412 /* vfs_sync() routine is not the default */
413 return vfs_getops(vfsp)->vfs_sync != NULL;
417 * Initialize a vfs structure.
419 void
420 vfs_init(struct vfs *vfs, const struct vfsops *ops, void *data)
422 /* Other initialization has been moved to vfs_alloc() */
423 vfs->vfs_count = 0;
424 vfs->vfs_next = vfs;
425 vfs->vfs_prev = vfs;
426 vfs->vfs_zone_next = vfs;
427 vfs->vfs_zone_prev = vfs;
428 vfs->vfs_lofi_id = 0;
429 sema_init(&vfs->vfs_reflock, 1, NULL, SEMA_DEFAULT, NULL);
430 vfsimpl_setup(vfs);
431 vfs->vfs_data = data;
432 vfs_setops(vfs, ops);
436 * Allocate and initialize the vfs implementation private data
437 * structure, vfs_impl_t.
439 void
440 vfsimpl_setup(vfs_t *vfsp)
442 int i;
444 if (vfsp->vfs_implp != NULL) {
445 return;
448 vfsp->vfs_implp = kmem_alloc(sizeof (vfs_impl_t), KM_SLEEP);
449 /* Note that these are #define'd in vfs.h */
450 vfsp->vfs_vskap = NULL;
451 vfsp->vfs_fstypevsp = NULL;
453 /* Set size of counted array, then zero the array */
454 vfsp->vfs_featureset[0] = VFS_FEATURE_MAXSZ - 1;
455 for (i = 1; i < VFS_FEATURE_MAXSZ; i++) {
456 vfsp->vfs_featureset[i] = 0;
461 * Release the vfs_impl_t structure, if it exists. Some unbundled
462 * filesystems may not use the newer version of vfs and thus
463 * would not contain this implementation private data structure.
465 void
466 vfsimpl_teardown(vfs_t *vfsp)
468 vfs_impl_t *vip = vfsp->vfs_implp;
470 if (vip == NULL)
471 return;
473 kmem_free(vfsp->vfs_implp, sizeof (vfs_impl_t));
474 vfsp->vfs_implp = NULL;
478 * VFS system calls: mount, umount, syssync, statfs, fstatfs, statvfs,
479 * fstatvfs, and sysfs are in kernel/syscall.
483 * Update every mounted file system. We call the vfs_sync operation of
484 * each file system type, passing it a NULL vfsp to indicate that all
485 * mounted file systems of that type should be updated.
487 void
488 vfs_sync(int flag)
490 struct vfssw *vswp;
491 RLOCK_VFSSW();
492 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
493 if (ALLOCATED_VFSSW(vswp) && VFS_INSTALLED(vswp)) {
494 vfs_refvfssw(vswp);
495 RUNLOCK_VFSSW();
496 if (vswp->vsw_vfsops.vfs_sync != NULL)
497 vswp->vsw_vfsops.vfs_sync(NULL, flag, CRED());
498 vfs_unrefvfssw(vswp);
499 RLOCK_VFSSW();
502 RUNLOCK_VFSSW();
505 void
506 sync(void)
508 vfs_sync(0);
512 * External routines.
515 krwlock_t vfssw_lock; /* lock accesses to vfssw */
518 * Lock for accessing the vfs linked list. Initialized in vfs_mountroot(),
519 * but otherwise should be accessed only via vfs_list_lock() and
520 * vfs_list_unlock(). Also used to protect the timestamp for mods to the list.
522 static krwlock_t vfslist;
525 * Mount devfs on /devices. This is done right after root is mounted
526 * to provide device access support for the system
528 static void
529 vfs_mountdevices(void)
531 struct vfssw *vsw;
532 struct vnode *mvp;
533 struct mounta mounta = { /* fake mounta for devfs_mount() */
534 NULL,
535 NULL,
536 MS_SYSSPACE,
537 NULL,
538 NULL,
540 NULL,
545 * _init devfs module to fill in the vfssw
547 if (modload("fs", "devfs") == -1)
548 panic("Cannot _init devfs module");
551 * Hold vfs
553 RLOCK_VFSSW();
554 vsw = vfs_getvfsswbyname("devfs");
555 VFS_INIT(&devices, &vsw->vsw_vfsops, NULL);
556 VFS_HOLD(&devices);
559 * Locate mount point
561 if (lookupname("/devices", UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp))
562 panic("Cannot find /devices");
565 * Perform the mount of /devices
567 if (VFS_MOUNT(&devices, mvp, &mounta, CRED()))
568 panic("Cannot mount /devices");
570 RUNLOCK_VFSSW();
573 * Set appropriate members and add to vfs list for mnttab display
575 vfs_setresource(&devices, "/devices", 0);
576 vfs_setmntpoint(&devices, "/devices", 0);
579 * Hold the root of /devices so it won't go away
581 if (VFS_ROOT(&devices, &devicesdir))
582 panic("vfs_mountdevices: not devices root");
584 if (vfs_lock(&devices) != 0) {
585 VN_RELE(devicesdir);
586 cmn_err(CE_NOTE, "Cannot acquire vfs_lock of /devices");
587 return;
590 if (vn_vfswlock(mvp) != 0) {
591 vfs_unlock(&devices);
592 VN_RELE(devicesdir);
593 cmn_err(CE_NOTE, "Cannot acquire vfswlock of /devices");
594 return;
597 vfs_add(mvp, &devices, 0);
598 vn_vfsunlock(mvp);
599 vfs_unlock(&devices);
600 VN_RELE(devicesdir);
604 * mount the first instance of /dev to root and remain mounted
606 static void
607 vfs_mountdev1(void)
609 struct vfssw *vsw;
610 struct vnode *mvp;
611 struct mounta mounta = { /* fake mounta for sdev_mount() */
612 NULL,
613 NULL,
614 MS_SYSSPACE | MS_OVERLAY,
615 NULL,
616 NULL,
618 NULL,
623 * _init dev module to fill in the vfssw
625 if (modload("fs", "dev") == -1)
626 cmn_err(CE_PANIC, "Cannot _init dev module\n");
629 * Hold vfs
631 RLOCK_VFSSW();
632 vsw = vfs_getvfsswbyname("dev");
633 VFS_INIT(&dev, &vsw->vsw_vfsops, NULL);
634 VFS_HOLD(&dev);
637 * Locate mount point
639 if (lookupname("/dev", UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp))
640 cmn_err(CE_PANIC, "Cannot find /dev\n");
643 * Perform the mount of /dev
645 if (VFS_MOUNT(&dev, mvp, &mounta, CRED()))
646 cmn_err(CE_PANIC, "Cannot mount /dev 1\n");
648 RUNLOCK_VFSSW();
651 * Set appropriate members and add to vfs list for mnttab display
653 vfs_setresource(&dev, "/dev", 0);
654 vfs_setmntpoint(&dev, "/dev", 0);
657 * Hold the root of /dev so it won't go away
659 if (VFS_ROOT(&dev, &devdir))
660 cmn_err(CE_PANIC, "vfs_mountdev1: not dev root");
662 if (vfs_lock(&dev) != 0) {
663 VN_RELE(devdir);
664 cmn_err(CE_NOTE, "Cannot acquire vfs_lock of /dev");
665 return;
668 if (vn_vfswlock(mvp) != 0) {
669 vfs_unlock(&dev);
670 VN_RELE(devdir);
671 cmn_err(CE_NOTE, "Cannot acquire vfswlock of /dev");
672 return;
675 vfs_add(mvp, &dev, 0);
676 vn_vfsunlock(mvp);
677 vfs_unlock(&dev);
678 VN_RELE(devdir);
682 * Mount required filesystem. This is done right after root is mounted.
684 static void
685 vfs_mountfs(char *module, char *spec, char *path)
687 struct vnode *mvp;
688 struct mounta mounta;
689 vfs_t *vfsp;
691 bzero(&mounta, sizeof (mounta));
692 mounta.flags = MS_SYSSPACE | MS_DATA;
693 mounta.fstype = module;
694 mounta.spec = spec;
695 mounta.dir = path;
696 if (lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp)) {
697 cmn_err(CE_WARN, "Cannot find %s", path);
698 return;
700 if (domount(NULL, &mounta, mvp, CRED(), &vfsp))
701 cmn_err(CE_WARN, "Cannot mount %s", path);
702 else
703 VFS_RELE(vfsp);
704 VN_RELE(mvp);
708 * vfs_mountroot is called by main() to mount the root filesystem.
710 void
711 vfs_mountroot(void)
713 struct vnode *rvp = NULL;
714 char *path;
715 size_t plen;
716 struct vfssw *vswp;
717 proc_t *p;
719 rw_init(&vfssw_lock, NULL, RW_DEFAULT, NULL);
720 rw_init(&vfslist, NULL, RW_DEFAULT, NULL);
723 * Alloc the vfs hash bucket array and locks
725 rvfs_list = kmem_zalloc(vfshsz * sizeof (rvfs_t), KM_SLEEP);
728 * Call machine-dependent routine "rootconf" to choose a root
729 * file system type.
731 if (rootconf())
732 panic("vfs_mountroot: cannot mount root");
734 * Get vnode for '/'. Set up rootdir, u.u_rdir and u.u_cdir
735 * to point to it. These are used by lookuppn() so that it
736 * knows where to start from ('/' or '.').
738 vfs_setmntpoint(rootvfs, "/", 0);
739 if (VFS_ROOT(rootvfs, &rootdir))
740 panic("vfs_mountroot: no root vnode");
743 * At this point, the process tree consists of p0 and possibly some
744 * direct children of p0. (i.e. there are no grandchildren)
746 * Walk through them all, setting their current directory.
748 mutex_enter(&pidlock);
749 for (p = practive; p != NULL; p = p->p_next) {
750 ASSERT(p == &p0 || p->p_parent == &p0);
752 PTOU(p)->u_cdir = rootdir;
753 VN_HOLD(PTOU(p)->u_cdir);
754 PTOU(p)->u_rdir = NULL;
756 mutex_exit(&pidlock);
759 * Setup the global zone's rootvp, now that it exists.
761 global_zone->zone_rootvp = rootdir;
762 VN_HOLD(global_zone->zone_rootvp);
765 * Notify the module code that it can begin using the
766 * root filesystem instead of the boot program's services.
768 modrootloaded = 1;
771 * Special handling for a ZFS root file system.
773 zfs_boot_init();
776 * Set up mnttab information for root
778 vfs_setresource(rootvfs, rootfs.bo_name, 0);
780 /* Now that we're all done with the root FS, set up its vopstats */
781 if ((vswp = vfs_getvfsswbyvfsops(vfs_getops(rootvfs))) != NULL) {
782 /* Set flag for statistics collection */
783 if (vswp->vsw_flag & VSW_STATS) {
784 initialize_vopstats(&rootvfs->vfs_vopstats);
785 rootvfs->vfs_flag |= VFS_STATS;
786 rootvfs->vfs_fstypevsp =
787 get_fstype_vopstats(rootvfs, vswp);
788 rootvfs->vfs_vskap = get_vskstat_anchor(rootvfs);
790 vfs_unrefvfssw(vswp);
794 * Mount /devices, /dev instance 1, /system/contract, /etc/mnttab,
795 * /etc/svc/volatile, /etc/dfs/sharetab, /system/object, and /proc.
797 vfs_mountdevices();
798 vfs_mountdev1();
800 vfs_mountfs("ctfs", "ctfs", CTFS_ROOT);
801 vfs_mountfs("proc", "/proc", "/proc");
802 vfs_mountfs("mntfs", "/etc/mnttab", "/etc/mnttab");
803 vfs_mountfs("tmpfs", "/etc/svc/volatile", "/etc/svc/volatile");
804 vfs_mountfs("objfs", "objfs", OBJFS_ROOT);
805 vfs_mountfs("bootfs", "bootfs", "/system/boot");
807 if (getzoneid() == GLOBAL_ZONEID) {
808 vfs_mountfs("sharefs", "sharefs", "/etc/dfs/sharetab");
811 if (strcmp(rootfs.bo_fstype, "zfs") != 0) {
813 * Look up the root device via devfs so that a dv_node is
814 * created for it. The vnode is never VN_RELE()ed.
815 * We allocate more than MAXPATHLEN so that the
816 * buffer passed to i_ddi_prompath_to_devfspath() is
817 * exactly MAXPATHLEN (the function expects a buffer
818 * of that length).
820 plen = strlen("/devices");
821 path = kmem_alloc(plen + MAXPATHLEN, KM_SLEEP);
822 (void) strcpy(path, "/devices");
824 if (i_ddi_prompath_to_devfspath(rootfs.bo_name, path + plen)
825 != DDI_SUCCESS ||
826 lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &rvp)) {
828 /* NUL terminate in case "path" has garbage */
829 path[plen + MAXPATHLEN - 1] = '\0';
830 #ifdef DEBUG
831 cmn_err(CE_WARN, "!Cannot lookup root device: %s",
832 path);
833 #endif
835 kmem_free(path, plen + MAXPATHLEN);
838 vfs_mnttabvp_setup();
842 * Check to see if our "block device" is actually a file. If so,
843 * automatically add a lofi device, and keep track of this fact.
845 static int
846 lofi_add(const char *fsname, struct vfs *vfsp,
847 mntopts_t *mntopts, struct mounta *uap)
849 int fromspace = (uap->flags & MS_SYSSPACE) ?
850 UIO_SYSSPACE : UIO_USERSPACE;
851 struct lofi_ioctl *li = NULL;
852 struct vnode *vp = NULL;
853 struct pathname pn = { NULL };
854 ldi_ident_t ldi_id;
855 ldi_handle_t ldi_hdl;
856 vfssw_t *vfssw;
857 int id;
858 int err = 0;
860 if ((vfssw = vfs_getvfssw(fsname)) == NULL)
861 return (0);
863 if (!(vfssw->vsw_flag & VSW_CANLOFI)) {
864 vfs_unrefvfssw(vfssw);
865 return (0);
868 vfs_unrefvfssw(vfssw);
869 vfssw = NULL;
871 if (pn_get(uap->spec, fromspace, &pn) != 0)
872 return (0);
874 if (lookupname(uap->spec, fromspace, FOLLOW, NULL, &vp) != 0)
875 goto out;
877 if (vp->v_type != VREG)
878 goto out;
880 /* OK, this is a lofi mount. */
882 if ((uap->flags & MS_REMOUNT) ||
883 vfs_optionisset_nolock(mntopts, MNTOPT_SUID, NULL) ||
884 vfs_optionisset_nolock(mntopts, MNTOPT_SETUID, NULL) ||
885 vfs_optionisset_nolock(mntopts, MNTOPT_DEVICES, NULL)) {
886 err = EINVAL;
887 goto out;
890 ldi_id = ldi_ident_from_anon();
891 li = kmem_zalloc(sizeof (*li), KM_SLEEP);
892 (void) strlcpy(li->li_filename, pn.pn_path, MAXPATHLEN);
894 err = ldi_open_by_name("/dev/lofictl", FREAD | FWRITE, kcred,
895 &ldi_hdl, ldi_id);
897 if (err)
898 goto out2;
900 err = ldi_ioctl(ldi_hdl, LOFI_MAP_FILE, (intptr_t)li,
901 FREAD | FWRITE | FKIOCTL, kcred, &id);
903 (void) ldi_close(ldi_hdl, FREAD | FWRITE, kcred);
905 if (!err)
906 vfsp->vfs_lofi_id = id;
908 out2:
909 ldi_ident_release(ldi_id);
910 out:
911 if (li != NULL)
912 kmem_free(li, sizeof (*li));
913 if (vp != NULL)
914 VN_RELE(vp);
915 pn_free(&pn);
916 return (err);
919 static void
920 lofi_remove(struct vfs *vfsp)
922 struct lofi_ioctl *li = NULL;
923 ldi_ident_t ldi_id;
924 ldi_handle_t ldi_hdl;
925 int err;
927 if (vfsp->vfs_lofi_id == 0)
928 return;
930 ldi_id = ldi_ident_from_anon();
932 li = kmem_zalloc(sizeof (*li), KM_SLEEP);
933 li->li_id = vfsp->vfs_lofi_id;
934 li->li_cleanup = B_TRUE;
936 err = ldi_open_by_name("/dev/lofictl", FREAD | FWRITE, kcred,
937 &ldi_hdl, ldi_id);
939 if (err)
940 goto out;
942 err = ldi_ioctl(ldi_hdl, LOFI_UNMAP_FILE_MINOR, (intptr_t)li,
943 FREAD | FWRITE | FKIOCTL, kcred, NULL);
945 (void) ldi_close(ldi_hdl, FREAD | FWRITE, kcred);
947 if (!err)
948 vfsp->vfs_lofi_id = 0;
950 out:
951 ldi_ident_release(ldi_id);
952 if (li != NULL)
953 kmem_free(li, sizeof (*li));
957 * Common mount code. Called from the system call entry point, from autofs,
958 * nfsv4 trigger mounts, and from pxfs.
960 * Takes the effective file system type, mount arguments, the mount point
961 * vnode, flags specifying whether the mount is a remount and whether it
962 * should be entered into the vfs list, and credentials. Fills in its vfspp
963 * parameter with the mounted file system instance's vfs.
965 * Note that the effective file system type is specified as a string. It may
966 * be null, in which case it's determined from the mount arguments, and may
967 * differ from the type specified in the mount arguments; this is a hook to
968 * allow interposition when instantiating file system instances.
970 * The caller is responsible for releasing its own hold on the mount point
971 * vp (this routine does its own hold when necessary).
972 * Also note that for remounts, the mount point vp should be the vnode for
973 * the root of the file system rather than the vnode that the file system
974 * is mounted on top of.
977 domount(char *fsname, struct mounta *uap, vnode_t *vp, struct cred *credp,
978 struct vfs **vfspp)
980 struct vfssw *vswp;
981 vfsops_t *vfsops;
982 struct vfs *vfsp;
983 struct vnode *bvp;
984 dev_t bdev = 0;
985 mntopts_t mnt_mntopts;
986 int error = 0;
987 int copyout_error = 0;
988 int ovflags;
989 char *opts = uap->optptr;
990 char *inargs = opts;
991 int optlen = uap->optlen;
992 int remount;
993 int rdonly;
994 int nbmand = 0;
995 int delmip = 0;
996 int addmip = 0;
997 int splice = ((uap->flags & MS_NOSPLICE) == 0);
998 int fromspace = (uap->flags & MS_SYSSPACE) ?
999 UIO_SYSSPACE : UIO_USERSPACE;
1000 char *resource = NULL, *mountpt = NULL;
1001 refstr_t *oldresource, *oldmntpt;
1002 struct pathname pn, rpn;
1003 vsk_anchor_t *vskap;
1004 char fstname[FSTYPSZ];
1005 zone_t *zone;
1008 * The v_flag value for the mount point vp is permanently set
1009 * to VVFSLOCK so that no one bypasses the vn_vfs*locks routine
1010 * for mount point locking.
1012 mutex_enter(&vp->v_lock);
1013 vp->v_flag |= VVFSLOCK;
1014 mutex_exit(&vp->v_lock);
1016 mnt_mntopts.mo_count = 0;
1018 * Find the ops vector to use to invoke the file system-specific mount
1019 * method. If the fsname argument is non-NULL, use it directly.
1020 * Otherwise, dig the file system type information out of the mount
1021 * arguments.
1023 * A side effect is to hold the vfssw entry.
1025 * Mount arguments can be specified in several ways, which are
1026 * distinguished by flag bit settings. The preferred way is to set
1027 * MS_OPTIONSTR, indicating an 8 argument mount with the file system
1028 * type supplied as a character string and the last two arguments
1029 * being a pointer to a character buffer and the size of the buffer.
1030 * On entry, the buffer holds a null terminated list of options; on
1031 * return, the string is the list of options the file system
1032 * recognized. If MS_DATA is set arguments five and six point to a
1033 * block of binary data which the file system interprets.
1034 * A further wrinkle is that some callers don't set MS_FSS and MS_DATA
1035 * consistently with these conventions. To handle them, we check to
1036 * see whether the pointer to the file system name has a numeric value
1037 * less than 256. If so, we treat it as an index.
1039 if (fsname != NULL) {
1040 if ((vswp = vfs_getvfssw(fsname)) == NULL) {
1041 return (EINVAL);
1043 } else if (uap->flags & (MS_OPTIONSTR | MS_DATA | MS_FSS)) {
1044 size_t n;
1045 uint_t fstype;
1047 fsname = fstname;
1049 if ((fstype = (uintptr_t)uap->fstype) < 256) {
1050 RLOCK_VFSSW();
1051 if (fstype == 0 || fstype >= nfstype ||
1052 !ALLOCATED_VFSSW(&vfssw[fstype])) {
1053 RUNLOCK_VFSSW();
1054 return (EINVAL);
1056 (void) strcpy(fsname, vfssw[fstype].vsw_name);
1057 RUNLOCK_VFSSW();
1058 if ((vswp = vfs_getvfssw(fsname)) == NULL)
1059 return (EINVAL);
1060 } else {
1062 * Handle either kernel or user address space.
1064 if (uap->flags & MS_SYSSPACE) {
1065 error = copystr(uap->fstype, fsname,
1066 FSTYPSZ, &n);
1067 } else {
1068 error = copyinstr(uap->fstype, fsname,
1069 FSTYPSZ, &n);
1071 if (error) {
1072 if (error == ENAMETOOLONG)
1073 return (EINVAL);
1074 return (error);
1076 if ((vswp = vfs_getvfssw(fsname)) == NULL)
1077 return (EINVAL);
1079 } else {
1080 if ((vswp = vfs_getvfsswbyvfsops(vfs_getops(rootvfs))) == NULL)
1081 return (EINVAL);
1082 fsname = vswp->vsw_name;
1084 if (!VFS_INSTALLED(vswp))
1085 return (EINVAL);
1087 if ((error = secpolicy_fs_allowed_mount(fsname)) != 0) {
1088 vfs_unrefvfssw(vswp);
1089 return (error);
1092 vfsops = &vswp->vsw_vfsops;
1094 vfs_copyopttbl(&vswp->vsw_optproto, &mnt_mntopts);
1096 * Fetch mount options and parse them for generic vfs options
1098 if (uap->flags & MS_OPTIONSTR) {
1100 * Limit the buffer size
1102 if (optlen < 0 || optlen > MAX_MNTOPT_STR) {
1103 error = EINVAL;
1104 goto errout;
1106 if ((uap->flags & MS_SYSSPACE) == 0) {
1107 inargs = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
1108 inargs[0] = '\0';
1109 if (optlen) {
1110 error = copyinstr(opts, inargs, (size_t)optlen,
1111 NULL);
1112 if (error) {
1113 goto errout;
1117 vfs_parsemntopts(&mnt_mntopts, inargs, 0);
1120 * Flag bits override the options string.
1122 if (uap->flags & MS_REMOUNT)
1123 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_REMOUNT, NULL, 0, 0);
1124 if (uap->flags & MS_RDONLY)
1125 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_RO, NULL, 0, 0);
1126 if (uap->flags & MS_NOSUID)
1127 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL, 0, 0);
1130 * Check if this is a remount; must be set in the option string and
1131 * the file system must support a remount option.
1133 if (remount = vfs_optionisset_nolock(&mnt_mntopts,
1134 MNTOPT_REMOUNT, NULL)) {
1135 if (!(vswp->vsw_flag & VSW_CANREMOUNT)) {
1136 error = ENOTSUP;
1137 goto errout;
1139 uap->flags |= MS_REMOUNT;
1143 * uap->flags and vfs_optionisset() should agree.
1145 if (rdonly = vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_RO, NULL)) {
1146 uap->flags |= MS_RDONLY;
1148 if (vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL)) {
1149 uap->flags |= MS_NOSUID;
1151 nbmand = vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_NBMAND, NULL);
1152 ASSERT(splice || !remount);
1154 * If we are splicing the fs into the namespace,
1155 * perform mount point checks.
1157 * We want to resolve the path for the mount point to eliminate
1158 * '.' and ".." and symlinks in mount points; we can't do the
1159 * same for the resource string, since it would turn
1160 * "/dev/dsk/c0t0d0s0" into "/devices/pci@...". We need to do
1161 * this before grabbing vn_vfswlock(), because otherwise we
1162 * would deadlock with lookuppn().
1164 if (splice) {
1165 ASSERT(vp->v_count > 0);
1168 * Pick up mount point and device from appropriate space.
1170 if (pn_get(uap->spec, fromspace, &pn) == 0) {
1171 resource = kmem_alloc(pn.pn_pathlen + 1,
1172 KM_SLEEP);
1173 (void) strcpy(resource, pn.pn_path);
1174 pn_free(&pn);
1177 * Do a lookupname prior to taking the
1178 * writelock. Mark this as completed if
1179 * successful for later cleanup and addition to
1180 * the mount in progress table.
1182 if ((vswp->vsw_flag & VSW_MOUNTDEV) &&
1183 lookupname(uap->spec, fromspace,
1184 FOLLOW, NULL, &bvp) == 0) {
1185 addmip = 1;
1188 if ((error = pn_get(uap->dir, fromspace, &pn)) == 0) {
1189 pathname_t *pnp;
1191 if (*pn.pn_path != '/') {
1192 error = EINVAL;
1193 pn_free(&pn);
1194 goto errout;
1196 pn_alloc(&rpn);
1198 * Kludge to prevent autofs from deadlocking with
1199 * itself when it calls domount().
1201 * If autofs is calling, it is because it is doing
1202 * (autofs) mounts in the process of an NFS mount. A
1203 * lookuppn() here would cause us to block waiting for
1204 * said NFS mount to complete, which can't since this
1205 * is the thread that was supposed to doing it.
1207 if (fromspace == UIO_USERSPACE) {
1208 if ((error = lookuppn(&pn, &rpn, FOLLOW, NULL,
1209 NULL)) == 0) {
1210 pnp = &rpn;
1211 } else {
1213 * The file disappeared or otherwise
1214 * became inaccessible since we opened
1215 * it; might as well fail the mount
1216 * since the mount point is no longer
1217 * accessible.
1219 pn_free(&rpn);
1220 pn_free(&pn);
1221 goto errout;
1223 } else {
1224 pnp = &pn;
1226 mountpt = kmem_alloc(pnp->pn_pathlen + 1, KM_SLEEP);
1227 (void) strcpy(mountpt, pnp->pn_path);
1230 * If the addition of the zone's rootpath
1231 * would push us over a total path length
1232 * of MAXPATHLEN, we fail the mount with
1233 * ENAMETOOLONG, which is what we would have
1234 * gotten if we were trying to perform the same
1235 * mount in the global zone.
1237 * strlen() doesn't count the trailing
1238 * '\0', but zone_rootpathlen counts both a
1239 * trailing '/' and the terminating '\0'.
1241 if ((curproc->p_zone->zone_rootpathlen - 1 +
1242 strlen(mountpt)) > MAXPATHLEN ||
1243 (resource != NULL &&
1244 (curproc->p_zone->zone_rootpathlen - 1 +
1245 strlen(resource)) > MAXPATHLEN)) {
1246 error = ENAMETOOLONG;
1249 pn_free(&rpn);
1250 pn_free(&pn);
1253 if (error)
1254 goto errout;
1257 * Prevent path name resolution from proceeding past
1258 * the mount point.
1260 if (vn_vfswlock(vp) != 0) {
1261 error = EBUSY;
1262 goto errout;
1266 * Verify that it's legitimate to establish a mount on
1267 * the prospective mount point.
1269 if (vn_mountedvfs(vp) != NULL) {
1271 * The mount point lock was obtained after some
1272 * other thread raced through and established a mount.
1274 vn_vfsunlock(vp);
1275 error = EBUSY;
1276 goto errout;
1278 if (vp->v_flag & VNOMOUNT) {
1279 vn_vfsunlock(vp);
1280 error = EINVAL;
1281 goto errout;
1284 if ((uap->flags & (MS_DATA | MS_OPTIONSTR)) == 0) {
1285 uap->dataptr = NULL;
1286 uap->datalen = 0;
1290 * If this is a remount, we don't want to create a new VFS.
1291 * Instead, we pass the existing one with a remount flag.
1293 if (remount) {
1295 * Confirm that the mount point is the root vnode of the
1296 * file system that is being remounted.
1297 * This can happen if the user specifies a different
1298 * mount point directory pathname in the (re)mount command.
1300 * Code below can only be reached if splice is true, so it's
1301 * safe to do vn_vfsunlock() here.
1303 if ((vp->v_flag & VROOT) == 0) {
1304 vn_vfsunlock(vp);
1305 error = ENOENT;
1306 goto errout;
1309 * Disallow making file systems read-only unless file system
1310 * explicitly allows it in its vfssw. Ignore other flags.
1312 if (rdonly && vn_is_readonly(vp) == 0 &&
1313 (vswp->vsw_flag & VSW_CANRWRO) == 0) {
1314 vn_vfsunlock(vp);
1315 error = EINVAL;
1316 goto errout;
1319 * Disallow changing the NBMAND disposition of the file
1320 * system on remounts.
1322 if ((nbmand && ((vp->v_vfsp->vfs_flag & VFS_NBMAND) == 0)) ||
1323 (!nbmand && (vp->v_vfsp->vfs_flag & VFS_NBMAND))) {
1324 vn_vfsunlock(vp);
1325 error = EINVAL;
1326 goto errout;
1328 vfsp = vp->v_vfsp;
1329 ovflags = vfsp->vfs_flag;
1330 vfsp->vfs_flag |= VFS_REMOUNT;
1331 vfsp->vfs_flag &= ~VFS_RDONLY;
1332 } else {
1333 vfsp = vfs_alloc(KM_SLEEP);
1334 VFS_INIT(vfsp, vfsops, NULL);
1337 VFS_HOLD(vfsp);
1339 if ((error = lofi_add(fsname, vfsp, &mnt_mntopts, uap)) != 0) {
1340 if (!remount) {
1341 if (splice)
1342 vn_vfsunlock(vp);
1343 vfs_free(vfsp);
1344 } else {
1345 vn_vfsunlock(vp);
1346 VFS_RELE(vfsp);
1348 goto errout;
1352 * PRIV_SYS_MOUNT doesn't mean you can become root.
1354 if (vfsp->vfs_lofi_id != 0) {
1355 uap->flags |= MS_NOSUID;
1356 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL, 0, 0);
1360 * The vfs_reflock is not used anymore the code below explicitly
1361 * holds it preventing others accesing it directly.
1363 if ((sema_tryp(&vfsp->vfs_reflock) == 0) &&
1364 !(vfsp->vfs_flag & VFS_REMOUNT))
1365 cmn_err(CE_WARN,
1366 "mount type %s couldn't get vfs_reflock", vswp->vsw_name);
1369 * Lock the vfs. If this is a remount we want to avoid spurious umount
1370 * failures that happen as a side-effect of fsflush() and other mount
1371 * and unmount operations that might be going on simultaneously and
1372 * may have locked the vfs currently. To not return EBUSY immediately
1373 * here we use vfs_lock_wait() instead vfs_lock() for the remount case.
1375 if (!remount) {
1376 if (error = vfs_lock(vfsp)) {
1377 vfsp->vfs_flag = ovflags;
1379 lofi_remove(vfsp);
1381 if (splice)
1382 vn_vfsunlock(vp);
1383 vfs_free(vfsp);
1384 goto errout;
1386 } else {
1387 vfs_lock_wait(vfsp);
1391 * Add device to mount in progress table, global mounts require special
1392 * handling. It is possible that we have already done the lookupname
1393 * on a spliced, non-global fs. If so, we don't want to do it again
1394 * since we cannot do a lookupname after taking the
1395 * wlock above. This case is for a non-spliced, non-global filesystem.
1397 if (!addmip) {
1398 if ((vswp->vsw_flag & VSW_MOUNTDEV) &&
1399 lookupname(uap->spec, fromspace, FOLLOW, NULL, &bvp) == 0) {
1400 addmip = 1;
1404 if (addmip) {
1405 vnode_t *lvp = NULL;
1407 error = vfs_get_lofi(vfsp, &lvp);
1408 if (error > 0) {
1409 lofi_remove(vfsp);
1411 if (splice)
1412 vn_vfsunlock(vp);
1413 vfs_unlock(vfsp);
1415 if (remount) {
1416 VFS_RELE(vfsp);
1417 } else {
1418 vfs_free(vfsp);
1421 goto errout;
1422 } else if (error == -1) {
1423 bdev = bvp->v_rdev;
1424 VN_RELE(bvp);
1425 } else {
1426 bdev = lvp->v_rdev;
1427 VN_RELE(lvp);
1428 VN_RELE(bvp);
1431 vfs_addmip(bdev, vfsp);
1432 addmip = 0;
1433 delmip = 1;
1436 * Invalidate cached entry for the mount point.
1438 if (splice)
1439 dnlc_purge_vp(vp);
1442 * If have an option string but the filesystem doesn't supply a
1443 * prototype options table, create a table with the global
1444 * options and sufficient room to accept all the options in the
1445 * string. Then parse the passed in option string
1446 * accepting all the options in the string. This gives us an
1447 * option table with all the proper cancel properties for the
1448 * global options.
1450 * Filesystems that supply a prototype options table are handled
1451 * earlier in this function.
1453 if (uap->flags & MS_OPTIONSTR) {
1454 if (!(vswp->vsw_flag & VSW_HASPROTO)) {
1455 mntopts_t tmp_mntopts;
1457 tmp_mntopts.mo_count = 0;
1458 vfs_createopttbl_extend(&tmp_mntopts, inargs,
1459 &mnt_mntopts);
1460 vfs_parsemntopts(&tmp_mntopts, inargs, 1);
1461 vfs_swapopttbl_nolock(&mnt_mntopts, &tmp_mntopts);
1462 vfs_freeopttbl(&tmp_mntopts);
1467 * Serialize with zone state transitions.
1468 * See vfs_list_add; zone mounted into is:
1469 * zone_find_by_path(refstr_value(vfsp->vfs_mntpt))
1470 * not the zone doing the mount (curproc->p_zone), but if we're already
1471 * inside a NGZ, then we know what zone we are.
1473 if (INGLOBALZONE(curproc)) {
1474 zone = zone_find_by_path(mountpt);
1475 ASSERT(zone != NULL);
1476 } else {
1477 zone = curproc->p_zone;
1479 * zone_find_by_path does a hold, so do one here too so that
1480 * we can do a zone_rele after mount_completed.
1482 zone_hold(zone);
1484 mount_in_progress(zone);
1486 * Instantiate (or reinstantiate) the file system. If appropriate,
1487 * splice it into the file system name space.
1489 * We want VFS_MOUNT() to be able to override the vfs_resource
1490 * string if necessary (ie, mntfs), and also for a remount to
1491 * change the same (necessary when remounting '/' during boot).
1492 * So we set up vfs_mntpt and vfs_resource to what we think they
1493 * should be, then hand off control to VFS_MOUNT() which can
1494 * override this.
1496 * For safety's sake, when changing vfs_resource or vfs_mntpt of
1497 * a vfs which is on the vfs list (i.e. during a remount), we must
1498 * never set those fields to NULL. Several bits of code make
1499 * assumptions that the fields are always valid.
1501 vfs_swapopttbl(&mnt_mntopts, &vfsp->vfs_mntopts);
1502 if (remount) {
1503 if ((oldresource = vfsp->vfs_resource) != NULL)
1504 refstr_hold(oldresource);
1505 if ((oldmntpt = vfsp->vfs_mntpt) != NULL)
1506 refstr_hold(oldmntpt);
1508 vfs_setresource(vfsp, resource, 0);
1509 vfs_setmntpoint(vfsp, mountpt, 0);
1512 * going to mount on this vnode, so notify.
1514 vnevent_mountedover(vp, NULL);
1515 error = VFS_MOUNT(vfsp, vp, uap, credp);
1517 if (uap->flags & MS_RDONLY)
1518 vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
1519 if (uap->flags & MS_NOSUID)
1520 vfs_setmntopt(vfsp, MNTOPT_NOSUID, NULL, 0);
1522 if (error) {
1523 lofi_remove(vfsp);
1525 if (remount) {
1526 /* put back pre-remount options */
1527 vfs_swapopttbl(&mnt_mntopts, &vfsp->vfs_mntopts);
1528 vfs_setmntpoint(vfsp, refstr_value(oldmntpt),
1529 VFSSP_VERBATIM);
1530 if (oldmntpt)
1531 refstr_rele(oldmntpt);
1532 vfs_setresource(vfsp, refstr_value(oldresource),
1533 VFSSP_VERBATIM);
1534 if (oldresource)
1535 refstr_rele(oldresource);
1536 vfsp->vfs_flag = ovflags;
1537 vfs_unlock(vfsp);
1538 VFS_RELE(vfsp);
1539 } else {
1540 vfs_unlock(vfsp);
1541 vfs_freemnttab(vfsp);
1542 vfs_free(vfsp);
1544 } else {
1546 * Set the mount time to now
1548 vfsp->vfs_mtime = ddi_get_time();
1549 if (remount) {
1550 vfsp->vfs_flag &= ~VFS_REMOUNT;
1551 if (oldresource)
1552 refstr_rele(oldresource);
1553 if (oldmntpt)
1554 refstr_rele(oldmntpt);
1555 } else if (splice) {
1557 * Link vfsp into the name space at the mount
1558 * point. Vfs_add() is responsible for
1559 * holding the mount point which will be
1560 * released when vfs_remove() is called.
1562 vfs_add(vp, vfsp, uap->flags);
1563 } else {
1565 * Hold the reference to file system which is
1566 * not linked into the name space.
1568 vfsp->vfs_zone = NULL;
1569 VFS_HOLD(vfsp);
1570 vfsp->vfs_vnodecovered = NULL;
1573 * Set flags for global options encountered
1575 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL))
1576 vfsp->vfs_flag |= VFS_RDONLY;
1577 else
1578 vfsp->vfs_flag &= ~VFS_RDONLY;
1579 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
1580 vfsp->vfs_flag |= (VFS_NOSETUID|VFS_NODEVICES);
1581 } else {
1582 if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
1583 vfsp->vfs_flag |= VFS_NODEVICES;
1584 else
1585 vfsp->vfs_flag &= ~VFS_NODEVICES;
1586 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))
1587 vfsp->vfs_flag |= VFS_NOSETUID;
1588 else
1589 vfsp->vfs_flag &= ~VFS_NOSETUID;
1591 if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL))
1592 vfsp->vfs_flag |= VFS_NBMAND;
1593 else
1594 vfsp->vfs_flag &= ~VFS_NBMAND;
1596 if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL))
1597 vfsp->vfs_flag |= VFS_XATTR;
1598 else
1599 vfsp->vfs_flag &= ~VFS_XATTR;
1601 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL))
1602 vfsp->vfs_flag |= VFS_NOEXEC;
1603 else
1604 vfsp->vfs_flag &= ~VFS_NOEXEC;
1607 * Now construct the output option string of options
1608 * we recognized.
1610 if (uap->flags & MS_OPTIONSTR) {
1611 vfs_list_read_lock();
1612 copyout_error = vfs_buildoptionstr(
1613 &vfsp->vfs_mntopts, inargs, optlen);
1614 vfs_list_unlock();
1615 if (copyout_error == 0 &&
1616 (uap->flags & MS_SYSSPACE) == 0) {
1617 copyout_error = copyoutstr(inargs, opts,
1618 optlen, NULL);
1623 * If this isn't a remount, set up the vopstats before
1624 * anyone can touch this. We only allow spliced file
1625 * systems (file systems which are in the namespace) to
1626 * have the VFS_STATS flag set.
1627 * NOTE: PxFS mounts the underlying file system with
1628 * MS_NOSPLICE set and copies those vfs_flags to its private
1629 * vfs structure. As a result, PxFS should never have
1630 * the VFS_STATS flag or else we might access the vfs
1631 * statistics-related fields prior to them being
1632 * properly initialized.
1634 if (!remount && (vswp->vsw_flag & VSW_STATS) && splice) {
1635 initialize_vopstats(&vfsp->vfs_vopstats);
1637 * We need to set vfs_vskap to NULL because there's
1638 * a chance it won't be set below. This is checked
1639 * in teardown_vopstats() so we can't have garbage.
1641 vfsp->vfs_vskap = NULL;
1642 vfsp->vfs_flag |= VFS_STATS;
1643 vfsp->vfs_fstypevsp = get_fstype_vopstats(vfsp, vswp);
1646 if (vswp->vsw_flag & VSW_XID)
1647 vfsp->vfs_flag |= VFS_XID;
1649 vfs_unlock(vfsp);
1651 mount_completed(zone);
1652 zone_rele(zone);
1653 if (splice)
1654 vn_vfsunlock(vp);
1656 if ((error == 0) && (copyout_error == 0)) {
1657 if (!remount) {
1659 * Don't call get_vskstat_anchor() while holding
1660 * locks since it allocates memory and calls
1661 * VFS_STATVFS(). For NFS, the latter can generate
1662 * an over-the-wire call.
1664 vskap = get_vskstat_anchor(vfsp);
1665 /* Only take the lock if we have something to do */
1666 if (vskap != NULL) {
1667 vfs_lock_wait(vfsp);
1668 if (vfsp->vfs_flag & VFS_STATS) {
1669 vfsp->vfs_vskap = vskap;
1671 vfs_unlock(vfsp);
1674 /* Return vfsp to caller. */
1675 *vfspp = vfsp;
1677 errout:
1678 vfs_freeopttbl(&mnt_mntopts);
1679 if (resource != NULL)
1680 kmem_free(resource, strlen(resource) + 1);
1681 if (mountpt != NULL)
1682 kmem_free(mountpt, strlen(mountpt) + 1);
1684 * It is possible we errored prior to adding to mount in progress
1685 * table. Must free vnode we acquired with successful lookupname.
1687 if (addmip)
1688 VN_RELE(bvp);
1689 if (delmip)
1690 vfs_delmip(vfsp);
1691 ASSERT(vswp != NULL);
1692 vfs_unrefvfssw(vswp);
1693 if (inargs != opts)
1694 kmem_free(inargs, MAX_MNTOPT_STR);
1695 if (copyout_error) {
1696 lofi_remove(vfsp);
1697 VFS_RELE(vfsp);
1698 error = copyout_error;
1700 return (error);
1703 static void
1704 vfs_setpath(
1705 struct vfs *vfsp, /* vfs being updated */
1706 refstr_t **refp, /* Ref-count string to contain the new path */
1707 const char *newpath, /* Path to add to refp (above) */
1708 uint32_t flag) /* flag */
1710 size_t len;
1711 refstr_t *ref;
1712 zone_t *zone = curproc->p_zone;
1713 char *sp;
1714 int have_list_lock = 0;
1716 ASSERT(!VFS_ON_LIST(vfsp) || vfs_lock_held(vfsp));
1719 * New path must be less than MAXPATHLEN because mntfs
1720 * will only display up to MAXPATHLEN bytes. This is currently
1721 * safe, because domount() uses pn_get(), and other callers
1722 * similarly cap the size to fewer than MAXPATHLEN bytes.
1725 ASSERT(strlen(newpath) < MAXPATHLEN);
1727 /* mntfs requires consistency while vfs list lock is held */
1729 if (VFS_ON_LIST(vfsp)) {
1730 have_list_lock = 1;
1731 vfs_list_lock();
1734 if (*refp != NULL)
1735 refstr_rele(*refp);
1738 * If we are in a non-global zone then we prefix the supplied path,
1739 * newpath, with the zone's root path, with two exceptions. The first
1740 * is where we have been explicitly directed to avoid doing so; this
1741 * will be the case following a failed remount, where the path supplied
1742 * will be a saved version which must now be restored. The second
1743 * exception is where newpath is not a pathname but a descriptive name,
1744 * e.g. "procfs".
1746 if (zone == global_zone || (flag & VFSSP_VERBATIM) || *newpath != '/') {
1747 ref = refstr_alloc(newpath);
1748 goto out;
1752 * Truncate the trailing '/' in the zoneroot, and merge
1753 * in the zone's rootpath with the "newpath" (resource
1754 * or mountpoint) passed in.
1756 * The size of the required buffer is thus the size of
1757 * the buffer required for the passed-in newpath
1758 * (strlen(newpath) + 1), plus the size of the buffer
1759 * required to hold zone_rootpath (zone_rootpathlen)
1760 * minus one for one of the now-superfluous NUL
1761 * terminations, minus one for the trailing '/'.
1763 * That gives us:
1765 * (strlen(newpath) + 1) + zone_rootpathlen - 1 - 1
1767 * Which is what we have below.
1770 len = strlen(newpath) + zone->zone_rootpathlen - 1;
1771 sp = kmem_alloc(len, KM_SLEEP);
1774 * Copy everything including the trailing slash, which
1775 * we then overwrite with the NUL character.
1778 (void) strcpy(sp, zone->zone_rootpath);
1779 sp[zone->zone_rootpathlen - 2] = '\0';
1780 (void) strcat(sp, newpath);
1782 ref = refstr_alloc(sp);
1783 kmem_free(sp, len);
1784 out:
1785 *refp = ref;
1787 if (have_list_lock) {
1788 vfs_mnttab_modtimeupd();
1789 vfs_list_unlock();
1794 * Record a mounted resource name in a vfs structure.
1795 * If vfsp is already mounted, caller must hold the vfs lock.
1797 void
1798 vfs_setresource(struct vfs *vfsp, const char *resource, uint32_t flag)
1800 if (resource == NULL || resource[0] == '\0')
1801 resource = VFS_NORESOURCE;
1802 vfs_setpath(vfsp, &vfsp->vfs_resource, resource, flag);
1806 * Record a mount point name in a vfs structure.
1807 * If vfsp is already mounted, caller must hold the vfs lock.
1809 void
1810 vfs_setmntpoint(struct vfs *vfsp, const char *mntpt, uint32_t flag)
1812 if (mntpt == NULL || mntpt[0] == '\0')
1813 mntpt = VFS_NOMNTPT;
1814 vfs_setpath(vfsp, &vfsp->vfs_mntpt, mntpt, flag);
1817 /* Returns the vfs_resource. Caller must call refstr_rele() when finished. */
1819 refstr_t *
1820 vfs_getresource(const struct vfs *vfsp)
1822 refstr_t *resource;
1824 vfs_list_read_lock();
1825 resource = vfsp->vfs_resource;
1826 refstr_hold(resource);
1827 vfs_list_unlock();
1829 return (resource);
1832 /* Returns the vfs_mntpt. Caller must call refstr_rele() when finished. */
1834 refstr_t *
1835 vfs_getmntpoint(const struct vfs *vfsp)
1837 refstr_t *mntpt;
1839 vfs_list_read_lock();
1840 mntpt = vfsp->vfs_mntpt;
1841 refstr_hold(mntpt);
1842 vfs_list_unlock();
1844 return (mntpt);
1848 * Create an empty options table with enough empty slots to hold all
1849 * The options in the options string passed as an argument.
1850 * Potentially prepend another options table.
1852 * Note: caller is responsible for locking the vfs list, if needed,
1853 * to protect mops.
1855 static void
1856 vfs_createopttbl_extend(mntopts_t *mops, const char *opts,
1857 const mntopts_t *mtmpl)
1859 const char *s = opts;
1860 uint_t count;
1862 if (opts == NULL || *opts == '\0') {
1863 count = 0;
1864 } else {
1865 count = 1;
1868 * Count number of options in the string
1870 for (s = strchr(s, ','); s != NULL; s = strchr(s, ',')) {
1871 count++;
1872 s++;
1875 vfs_copyopttbl_extend(mtmpl, mops, count);
1879 * Create an empty options table with enough empty slots to hold all
1880 * The options in the options string passed as an argument.
1882 * This function is *not* for general use by filesystems.
1884 * Note: caller is responsible for locking the vfs list, if needed,
1885 * to protect mops.
1887 void
1888 vfs_createopttbl(mntopts_t *mops, const char *opts)
1890 vfs_createopttbl_extend(mops, opts, NULL);
1895 * Swap two mount options tables
1897 static void
1898 vfs_swapopttbl_nolock(mntopts_t *optbl1, mntopts_t *optbl2)
1900 uint_t tmpcnt;
1901 mntopt_t *tmplist;
1903 tmpcnt = optbl2->mo_count;
1904 tmplist = optbl2->mo_list;
1905 optbl2->mo_count = optbl1->mo_count;
1906 optbl2->mo_list = optbl1->mo_list;
1907 optbl1->mo_count = tmpcnt;
1908 optbl1->mo_list = tmplist;
1911 static void
1912 vfs_swapopttbl(mntopts_t *optbl1, mntopts_t *optbl2)
1914 vfs_list_lock();
1915 vfs_swapopttbl_nolock(optbl1, optbl2);
1916 vfs_mnttab_modtimeupd();
1917 vfs_list_unlock();
1920 static char **
1921 vfs_copycancelopt_extend(char **const moc, int extend)
1923 int i = 0;
1924 int j;
1925 char **result;
1927 if (moc != NULL) {
1928 for (; moc[i] != NULL; i++)
1929 /* count number of options to cancel */;
1932 if (i + extend == 0)
1933 return (NULL);
1935 result = kmem_alloc((i + extend + 1) * sizeof (char *), KM_SLEEP);
1937 for (j = 0; j < i; j++) {
1938 result[j] = kmem_alloc(strlen(moc[j]) + 1, KM_SLEEP);
1939 (void) strcpy(result[j], moc[j]);
1941 for (; j <= i + extend; j++)
1942 result[j] = NULL;
1944 return (result);
1947 static void
1948 vfs_copyopt(const mntopt_t *s, mntopt_t *d)
1950 char *sp, *dp;
1952 d->mo_flags = s->mo_flags;
1953 d->mo_data = s->mo_data;
1954 sp = s->mo_name;
1955 if (sp != NULL) {
1956 dp = kmem_alloc(strlen(sp) + 1, KM_SLEEP);
1957 (void) strcpy(dp, sp);
1958 d->mo_name = dp;
1959 } else {
1960 d->mo_name = NULL; /* should never happen */
1963 d->mo_cancel = vfs_copycancelopt_extend(s->mo_cancel, 0);
1965 sp = s->mo_arg;
1966 if (sp != NULL) {
1967 dp = kmem_alloc(strlen(sp) + 1, KM_SLEEP);
1968 (void) strcpy(dp, sp);
1969 d->mo_arg = dp;
1970 } else {
1971 d->mo_arg = NULL;
1976 * Copy a mount options table, possibly allocating some spare
1977 * slots at the end. It is permissible to copy_extend the NULL table.
1979 static void
1980 vfs_copyopttbl_extend(const mntopts_t *smo, mntopts_t *dmo, int extra)
1982 uint_t i, count;
1983 mntopt_t *motbl;
1986 * Clear out any existing stuff in the options table being initialized
1988 vfs_freeopttbl(dmo);
1989 count = (smo == NULL) ? 0 : smo->mo_count;
1990 if ((count + extra) == 0) /* nothing to do */
1991 return;
1992 dmo->mo_count = count + extra;
1993 motbl = kmem_zalloc((count + extra) * sizeof (mntopt_t), KM_SLEEP);
1994 dmo->mo_list = motbl;
1995 for (i = 0; i < count; i++) {
1996 vfs_copyopt(&smo->mo_list[i], &motbl[i]);
1998 for (i = count; i < count + extra; i++) {
1999 motbl[i].mo_flags = MO_EMPTY;
2004 * Copy a mount options table.
2006 * This function is *not* for general use by filesystems.
2008 * Note: caller is responsible for locking the vfs list, if needed,
2009 * to protect smo and dmo.
2011 void
2012 vfs_copyopttbl(const mntopts_t *smo, mntopts_t *dmo)
2014 vfs_copyopttbl_extend(smo, dmo, 0);
2017 static char **
2018 vfs_mergecancelopts(const mntopt_t *mop1, const mntopt_t *mop2)
2020 int c1 = 0;
2021 int c2 = 0;
2022 char **result;
2023 char **sp1, **sp2, **dp;
2026 * First we count both lists of cancel options.
2027 * If either is NULL or has no elements, we return a copy of
2028 * the other.
2030 if (mop1->mo_cancel != NULL) {
2031 for (; mop1->mo_cancel[c1] != NULL; c1++)
2032 /* count cancel options in mop1 */;
2035 if (c1 == 0)
2036 return (vfs_copycancelopt_extend(mop2->mo_cancel, 0));
2038 if (mop2->mo_cancel != NULL) {
2039 for (; mop2->mo_cancel[c2] != NULL; c2++)
2040 /* count cancel options in mop2 */;
2043 result = vfs_copycancelopt_extend(mop1->mo_cancel, c2);
2045 if (c2 == 0)
2046 return (result);
2049 * When we get here, we've got two sets of cancel options;
2050 * we need to merge the two sets. We know that the result
2051 * array has "c1+c2+1" entries and in the end we might shrink
2052 * it.
2053 * Result now has a copy of the c1 entries from mop1; we'll
2054 * now lookup all the entries of mop2 in mop1 and copy it if
2055 * it is unique.
2056 * This operation is O(n^2) but it's only called once per
2057 * filesystem per duplicate option. This is a situation
2058 * which doesn't arise with the filesystems in ON and
2059 * n is generally 1.
2062 dp = &result[c1];
2063 for (sp2 = mop2->mo_cancel; *sp2 != NULL; sp2++) {
2064 for (sp1 = mop1->mo_cancel; *sp1 != NULL; sp1++) {
2065 if (strcmp(*sp1, *sp2) == 0)
2066 break;
2068 if (*sp1 == NULL) {
2070 * Option *sp2 not found in mop1, so copy it.
2071 * The calls to vfs_copycancelopt_extend()
2072 * guarantee that there's enough room.
2074 *dp = kmem_alloc(strlen(*sp2) + 1, KM_SLEEP);
2075 (void) strcpy(*dp++, *sp2);
2078 if (dp != &result[c1+c2]) {
2079 size_t bytes = (dp - result + 1) * sizeof (char *);
2080 char **nres = kmem_alloc(bytes, KM_SLEEP);
2082 bcopy(result, nres, bytes);
2083 kmem_free(result, (c1 + c2 + 1) * sizeof (char *));
2084 result = nres;
2086 return (result);
2090 * Merge two mount option tables (outer and inner) into one. This is very
2091 * similar to "merging" global variables and automatic variables in C.
2093 * This isn't (and doesn't have to be) fast.
2095 * This function is *not* for general use by filesystems.
2097 * Note: caller is responsible for locking the vfs list, if needed,
2098 * to protect omo, imo & dmo.
2100 void
2101 vfs_mergeopttbl(const mntopts_t *omo, const mntopts_t *imo, mntopts_t *dmo)
2103 uint_t i, count;
2104 mntopt_t *mop, *motbl;
2105 uint_t freeidx;
2108 * First determine how much space we need to allocate.
2110 count = omo->mo_count;
2111 for (i = 0; i < imo->mo_count; i++) {
2112 if (imo->mo_list[i].mo_flags & MO_EMPTY)
2113 continue;
2114 if (vfs_hasopt(omo, imo->mo_list[i].mo_name) == NULL)
2115 count++;
2117 ASSERT(count >= omo->mo_count &&
2118 count <= omo->mo_count + imo->mo_count);
2119 motbl = kmem_alloc(count * sizeof (mntopt_t), KM_SLEEP);
2120 for (i = 0; i < omo->mo_count; i++)
2121 vfs_copyopt(&omo->mo_list[i], &motbl[i]);
2122 freeidx = omo->mo_count;
2123 for (i = 0; i < imo->mo_count; i++) {
2124 if (imo->mo_list[i].mo_flags & MO_EMPTY)
2125 continue;
2126 if ((mop = vfs_hasopt(omo, imo->mo_list[i].mo_name)) != NULL) {
2127 char **newcanp;
2128 uint_t index = mop - omo->mo_list;
2130 newcanp = vfs_mergecancelopts(mop, &motbl[index]);
2132 vfs_freeopt(&motbl[index]);
2133 vfs_copyopt(&imo->mo_list[i], &motbl[index]);
2135 vfs_freecancelopt(motbl[index].mo_cancel);
2136 motbl[index].mo_cancel = newcanp;
2137 } else {
2139 * If it's a new option, just copy it over to the first
2140 * free location.
2142 vfs_copyopt(&imo->mo_list[i], &motbl[freeidx++]);
2145 dmo->mo_count = count;
2146 dmo->mo_list = motbl;
2150 * Functions to set and clear mount options in a mount options table.
2154 * Clear a mount option, if it exists.
2156 * The update_mnttab arg indicates whether mops is part of a vfs that is on
2157 * the vfs list.
2159 static void
2160 vfs_clearmntopt_nolock(mntopts_t *mops, const char *opt, int update_mnttab)
2162 struct mntopt *mop;
2163 uint_t i, count;
2165 ASSERT(!update_mnttab || RW_WRITE_HELD(&vfslist));
2167 count = mops->mo_count;
2168 for (i = 0; i < count; i++) {
2169 mop = &mops->mo_list[i];
2171 if (mop->mo_flags & MO_EMPTY)
2172 continue;
2173 if (strcmp(opt, mop->mo_name))
2174 continue;
2175 mop->mo_flags &= ~MO_SET;
2176 if (mop->mo_arg != NULL) {
2177 kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
2179 mop->mo_arg = NULL;
2180 if (update_mnttab)
2181 vfs_mnttab_modtimeupd();
2182 break;
2186 void
2187 vfs_clearmntopt(struct vfs *vfsp, const char *opt)
2189 int gotlock = 0;
2191 if (VFS_ON_LIST(vfsp)) {
2192 gotlock = 1;
2193 vfs_list_lock();
2195 vfs_clearmntopt_nolock(&vfsp->vfs_mntopts, opt, gotlock);
2196 if (gotlock)
2197 vfs_list_unlock();
2202 * Set a mount option on. If it's not found in the table, it's silently
2203 * ignored. If the option has MO_IGNORE set, it is still set unless the
2204 * VFS_NOFORCEOPT bit is set in the flags. Also, VFS_DISPLAY/VFS_NODISPLAY flag
2205 * bits can be used to toggle the MO_NODISPLAY bit for the option.
2206 * If the VFS_CREATEOPT flag bit is set then the first option slot with
2207 * MO_EMPTY set is created as the option passed in.
2209 * The update_mnttab arg indicates whether mops is part of a vfs that is on
2210 * the vfs list.
2212 static void
2213 vfs_setmntopt_nolock(mntopts_t *mops, const char *opt,
2214 const char *arg, int flags, int update_mnttab)
2216 mntopt_t *mop;
2217 uint_t i, count;
2218 char *sp;
2220 ASSERT(!update_mnttab || RW_WRITE_HELD(&vfslist));
2222 if (flags & VFS_CREATEOPT) {
2223 if (vfs_hasopt(mops, opt) != NULL) {
2224 flags &= ~VFS_CREATEOPT;
2227 count = mops->mo_count;
2228 for (i = 0; i < count; i++) {
2229 mop = &mops->mo_list[i];
2231 if (mop->mo_flags & MO_EMPTY) {
2232 if ((flags & VFS_CREATEOPT) == 0)
2233 continue;
2234 sp = kmem_alloc(strlen(opt) + 1, KM_SLEEP);
2235 (void) strcpy(sp, opt);
2236 mop->mo_name = sp;
2237 if (arg != NULL)
2238 mop->mo_flags = MO_HASVALUE;
2239 else
2240 mop->mo_flags = 0;
2241 } else if (strcmp(opt, mop->mo_name)) {
2242 continue;
2244 if ((mop->mo_flags & MO_IGNORE) && (flags & VFS_NOFORCEOPT))
2245 break;
2246 if (arg != NULL && (mop->mo_flags & MO_HASVALUE) != 0) {
2247 sp = kmem_alloc(strlen(arg) + 1, KM_SLEEP);
2248 (void) strcpy(sp, arg);
2249 } else {
2250 sp = NULL;
2252 if (mop->mo_arg != NULL)
2253 kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
2254 mop->mo_arg = sp;
2255 if (flags & VFS_DISPLAY)
2256 mop->mo_flags &= ~MO_NODISPLAY;
2257 if (flags & VFS_NODISPLAY)
2258 mop->mo_flags |= MO_NODISPLAY;
2259 mop->mo_flags |= MO_SET;
2260 if (mop->mo_cancel != NULL) {
2261 char **cp;
2263 for (cp = mop->mo_cancel; *cp != NULL; cp++)
2264 vfs_clearmntopt_nolock(mops, *cp, 0);
2266 if (update_mnttab)
2267 vfs_mnttab_modtimeupd();
2268 break;
2272 void
2273 vfs_setmntopt(struct vfs *vfsp, const char *opt, const char *arg, int flags)
2275 int gotlock = 0;
2277 if (VFS_ON_LIST(vfsp)) {
2278 gotlock = 1;
2279 vfs_list_lock();
2281 vfs_setmntopt_nolock(&vfsp->vfs_mntopts, opt, arg, flags, gotlock);
2282 if (gotlock)
2283 vfs_list_unlock();
2288 * Add a "tag" option to a mounted file system's options list.
2290 * Note: caller is responsible for locking the vfs list, if needed,
2291 * to protect mops.
2293 static mntopt_t *
2294 vfs_addtag(mntopts_t *mops, const char *tag)
2296 uint_t count;
2297 mntopt_t *mop, *motbl;
2299 count = mops->mo_count + 1;
2300 motbl = kmem_zalloc(count * sizeof (mntopt_t), KM_SLEEP);
2301 if (mops->mo_count) {
2302 size_t len = (count - 1) * sizeof (mntopt_t);
2304 bcopy(mops->mo_list, motbl, len);
2305 kmem_free(mops->mo_list, len);
2307 mops->mo_count = count;
2308 mops->mo_list = motbl;
2309 mop = &motbl[count - 1];
2310 mop->mo_flags = MO_TAG;
2311 mop->mo_name = kmem_alloc(strlen(tag) + 1, KM_SLEEP);
2312 (void) strcpy(mop->mo_name, tag);
2313 return (mop);
2317 * Allow users to set arbitrary "tags" in a vfs's mount options.
2318 * Broader use within the kernel is discouraged.
2321 vfs_settag(uint_t major, uint_t minor, const char *mntpt, const char *tag,
2322 cred_t *cr)
2324 vfs_t *vfsp;
2325 mntopts_t *mops;
2326 mntopt_t *mop;
2327 int found = 0;
2328 dev_t dev = makedevice(major, minor);
2329 int err = 0;
2330 char *buf = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
2333 * Find the desired mounted file system
2335 vfs_list_lock();
2336 vfsp = rootvfs;
2337 do {
2338 if (vfsp->vfs_dev == dev &&
2339 strcmp(mntpt, refstr_value(vfsp->vfs_mntpt)) == 0) {
2340 found = 1;
2341 break;
2343 vfsp = vfsp->vfs_next;
2344 } while (vfsp != rootvfs);
2346 if (!found) {
2347 err = EINVAL;
2348 goto out;
2350 err = secpolicy_fs_config(cr, vfsp);
2351 if (err != 0)
2352 goto out;
2354 mops = &vfsp->vfs_mntopts;
2356 * Add tag if it doesn't already exist
2358 if ((mop = vfs_hasopt(mops, tag)) == NULL) {
2359 int len;
2361 (void) vfs_buildoptionstr(mops, buf, MAX_MNTOPT_STR);
2362 len = strlen(buf);
2363 if (len + strlen(tag) + 2 > MAX_MNTOPT_STR) {
2364 err = ENAMETOOLONG;
2365 goto out;
2367 mop = vfs_addtag(mops, tag);
2369 if ((mop->mo_flags & MO_TAG) == 0) {
2370 err = EINVAL;
2371 goto out;
2373 vfs_setmntopt_nolock(mops, tag, NULL, 0, 1);
2374 out:
2375 vfs_list_unlock();
2376 kmem_free(buf, MAX_MNTOPT_STR);
2377 return (err);
2381 * Allow users to remove arbitrary "tags" in a vfs's mount options.
2382 * Broader use within the kernel is discouraged.
2385 vfs_clrtag(uint_t major, uint_t minor, const char *mntpt, const char *tag,
2386 cred_t *cr)
2388 vfs_t *vfsp;
2389 mntopt_t *mop;
2390 int found = 0;
2391 dev_t dev = makedevice(major, minor);
2392 int err = 0;
2395 * Find the desired mounted file system
2397 vfs_list_lock();
2398 vfsp = rootvfs;
2399 do {
2400 if (vfsp->vfs_dev == dev &&
2401 strcmp(mntpt, refstr_value(vfsp->vfs_mntpt)) == 0) {
2402 found = 1;
2403 break;
2405 vfsp = vfsp->vfs_next;
2406 } while (vfsp != rootvfs);
2408 if (!found) {
2409 err = EINVAL;
2410 goto out;
2412 err = secpolicy_fs_config(cr, vfsp);
2413 if (err != 0)
2414 goto out;
2416 if ((mop = vfs_hasopt(&vfsp->vfs_mntopts, tag)) == NULL) {
2417 err = EINVAL;
2418 goto out;
2420 if ((mop->mo_flags & MO_TAG) == 0) {
2421 err = EINVAL;
2422 goto out;
2424 vfs_clearmntopt_nolock(&vfsp->vfs_mntopts, tag, 1);
2425 out:
2426 vfs_list_unlock();
2427 return (err);
2431 * Function to parse an option string and fill in a mount options table.
2432 * Unknown options are silently ignored. The input option string is modified
2433 * by replacing separators with nulls. If the create flag is set, options
2434 * not found in the table are just added on the fly. The table must have
2435 * an option slot marked MO_EMPTY to add an option on the fly.
2437 * This function is *not* for general use by filesystems.
2439 * Note: caller is responsible for locking the vfs list, if needed,
2440 * to protect mops..
2442 void
2443 vfs_parsemntopts(mntopts_t *mops, char *osp, int create)
2445 char *s = osp, *p, *nextop, *valp, *cp, *ep;
2446 int setflg = VFS_NOFORCEOPT;
2448 if (osp == NULL)
2449 return;
2450 while (*s != '\0') {
2451 p = strchr(s, ','); /* find next option */
2452 if (p == NULL) {
2453 cp = NULL;
2454 p = s + strlen(s);
2455 } else {
2456 cp = p; /* save location of comma */
2457 *p++ = '\0'; /* mark end and point to next option */
2459 nextop = p;
2460 p = strchr(s, '='); /* look for value */
2461 if (p == NULL) {
2462 valp = NULL; /* no value supplied */
2463 } else {
2464 ep = p; /* save location of equals */
2465 *p++ = '\0'; /* end option and point to value */
2466 valp = p;
2469 * set option into options table
2471 if (create)
2472 setflg |= VFS_CREATEOPT;
2473 vfs_setmntopt_nolock(mops, s, valp, setflg, 0);
2474 if (cp != NULL)
2475 *cp = ','; /* restore the comma */
2476 if (valp != NULL)
2477 *ep = '='; /* restore the equals */
2478 s = nextop;
2483 * Function to inquire if an option exists in a mount options table.
2484 * Returns a pointer to the option if it exists, else NULL.
2486 * This function is *not* for general use by filesystems.
2488 * Note: caller is responsible for locking the vfs list, if needed,
2489 * to protect mops.
2491 struct mntopt *
2492 vfs_hasopt(const mntopts_t *mops, const char *opt)
2494 struct mntopt *mop;
2495 uint_t i, count;
2497 count = mops->mo_count;
2498 for (i = 0; i < count; i++) {
2499 mop = &mops->mo_list[i];
2501 if (mop->mo_flags & MO_EMPTY)
2502 continue;
2503 if (strcmp(opt, mop->mo_name) == 0)
2504 return (mop);
2506 return (NULL);
2510 * Function to inquire if an option is set in a mount options table.
2511 * Returns non-zero if set and fills in the arg pointer with a pointer to
2512 * the argument string or NULL if there is no argument string.
2514 static int
2515 vfs_optionisset_nolock(const mntopts_t *mops, const char *opt, char **argp)
2517 struct mntopt *mop;
2518 uint_t i, count;
2520 count = mops->mo_count;
2521 for (i = 0; i < count; i++) {
2522 mop = &mops->mo_list[i];
2524 if (mop->mo_flags & MO_EMPTY)
2525 continue;
2526 if (strcmp(opt, mop->mo_name))
2527 continue;
2528 if ((mop->mo_flags & MO_SET) == 0)
2529 return (0);
2530 if (argp != NULL && (mop->mo_flags & MO_HASVALUE) != 0)
2531 *argp = mop->mo_arg;
2532 return (1);
2534 return (0);
2539 vfs_optionisset(const struct vfs *vfsp, const char *opt, char **argp)
2541 int ret;
2543 vfs_list_read_lock();
2544 ret = vfs_optionisset_nolock(&vfsp->vfs_mntopts, opt, argp);
2545 vfs_list_unlock();
2546 return (ret);
2551 * Construct a comma separated string of the options set in the given
2552 * mount table, return the string in the given buffer. Return non-zero if
2553 * the buffer would overflow.
2555 * This function is *not* for general use by filesystems.
2557 * Note: caller is responsible for locking the vfs list, if needed,
2558 * to protect mp.
2561 vfs_buildoptionstr(const mntopts_t *mp, char *buf, int len)
2563 char *cp;
2564 uint_t i;
2566 buf[0] = '\0';
2567 cp = buf;
2568 for (i = 0; i < mp->mo_count; i++) {
2569 struct mntopt *mop;
2571 mop = &mp->mo_list[i];
2572 if (mop->mo_flags & MO_SET) {
2573 int optlen, comma = 0;
2575 if (buf[0] != '\0')
2576 comma = 1;
2577 optlen = strlen(mop->mo_name);
2578 if (strlen(buf) + comma + optlen + 1 > len)
2579 goto err;
2580 if (comma)
2581 *cp++ = ',';
2582 (void) strcpy(cp, mop->mo_name);
2583 cp += optlen;
2585 * Append option value if there is one
2587 if (mop->mo_arg != NULL) {
2588 int arglen;
2590 arglen = strlen(mop->mo_arg);
2591 if (strlen(buf) + arglen + 2 > len)
2592 goto err;
2593 *cp++ = '=';
2594 (void) strcpy(cp, mop->mo_arg);
2595 cp += arglen;
2599 return (0);
2600 err:
2601 return (EOVERFLOW);
2604 static void
2605 vfs_freecancelopt(char **moc)
2607 if (moc != NULL) {
2608 int ccnt = 0;
2609 char **cp;
2611 for (cp = moc; *cp != NULL; cp++) {
2612 kmem_free(*cp, strlen(*cp) + 1);
2613 ccnt++;
2615 kmem_free(moc, (ccnt + 1) * sizeof (char *));
2619 static void
2620 vfs_freeopt(mntopt_t *mop)
2622 if (mop->mo_name != NULL)
2623 kmem_free(mop->mo_name, strlen(mop->mo_name) + 1);
2625 vfs_freecancelopt(mop->mo_cancel);
2627 if (mop->mo_arg != NULL)
2628 kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
2632 * Free a mount options table
2634 * This function is *not* for general use by filesystems.
2636 * Note: caller is responsible for locking the vfs list, if needed,
2637 * to protect mp.
2639 void
2640 vfs_freeopttbl(mntopts_t *mp)
2642 uint_t i, count;
2644 count = mp->mo_count;
2645 for (i = 0; i < count; i++) {
2646 vfs_freeopt(&mp->mo_list[i]);
2648 if (count) {
2649 kmem_free(mp->mo_list, sizeof (mntopt_t) * count);
2650 mp->mo_count = 0;
2651 mp->mo_list = NULL;
2656 /* ARGSUSED */
2657 static int
2658 vfs_mntdummyread(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cred,
2659 caller_context_t *ct)
2661 return (0);
2664 /* ARGSUSED */
2665 static int
2666 vfs_mntdummywrite(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cred,
2667 caller_context_t *ct)
2669 return (0);
2673 * The dummy vnode is currently used only by file events notification
2674 * module which is just interested in the timestamps.
2676 /* ARGSUSED */
2677 static int
2678 vfs_mntdummygetattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2679 caller_context_t *ct)
2681 bzero(vap, sizeof (vattr_t));
2682 vap->va_type = VREG;
2683 vap->va_nlink = 1;
2684 vap->va_ctime = vfs_mnttab_ctime;
2686 * it is ok to just copy mtime as the time will be monotonically
2687 * increasing.
2689 vap->va_mtime = vfs_mnttab_mtime;
2690 vap->va_atime = vap->va_mtime;
2691 return (0);
2694 static void
2695 vfs_mnttabvp_setup(void)
2697 static const struct vnodeops dummyops = {
2698 .vnop_name = "mnttab",
2699 .vop_read = vfs_mntdummyread,
2700 .vop_write = vfs_mntdummywrite,
2701 .vop_getattr = vfs_mntdummygetattr,
2702 .vop_vnevent = fs_vnevent_support,
2704 vnode_t *tvp;
2707 * A global dummy vnode is allocated to represent mntfs files.
2708 * The mntfs file (/etc/mnttab) can be monitored for file events
2709 * and receive an event when mnttab changes. Dummy VOP calls
2710 * will be made on this vnode. The file events notification module
2711 * intercepts this vnode and delivers relevant events.
2713 tvp = vn_alloc(KM_SLEEP);
2714 tvp->v_flag = VNOMOUNT|VNOMAP|VNOSWAP|VNOCACHE;
2715 vn_setops(tvp, &dummyops);
2716 tvp->v_type = VREG;
2718 * The mnt dummy ops do not reference v_data.
2719 * No other module intercepting this vnode should either.
2720 * Just set it to point to itself.
2722 tvp->v_data = (caddr_t)tvp;
2723 tvp->v_vfsp = rootvfs;
2724 vfs_mntdummyvp = tvp;
2728 * performs fake read/write ops
2730 static void
2731 vfs_mnttab_rwop(int rw)
2733 struct uio uio;
2734 struct iovec iov;
2735 char buf[1];
2737 if (vfs_mntdummyvp == NULL)
2738 return;
2740 bzero(&uio, sizeof (uio));
2741 bzero(&iov, sizeof (iov));
2742 iov.iov_base = buf;
2743 iov.iov_len = 0;
2744 uio.uio_iov = &iov;
2745 uio.uio_iovcnt = 1;
2746 uio.uio_loffset = 0;
2747 uio.uio_segflg = UIO_SYSSPACE;
2748 uio.uio_resid = 0;
2749 if (rw) {
2750 (void) fop_write(vfs_mntdummyvp, &uio, 0, kcred, NULL);
2751 } else {
2752 (void) fop_read(vfs_mntdummyvp, &uio, 0, kcred, NULL);
2757 * Generate a write operation.
2759 void
2760 vfs_mnttab_writeop(void)
2762 vfs_mnttab_rwop(1);
2766 * Generate a read operation.
2768 void
2769 vfs_mnttab_readop(void)
2771 vfs_mnttab_rwop(0);
2775 * Free any mnttab information recorded in the vfs struct.
2776 * The vfs must not be on the vfs list.
2778 static void
2779 vfs_freemnttab(struct vfs *vfsp)
2781 ASSERT(!VFS_ON_LIST(vfsp));
2784 * Free device and mount point information
2786 if (vfsp->vfs_mntpt != NULL) {
2787 refstr_rele(vfsp->vfs_mntpt);
2788 vfsp->vfs_mntpt = NULL;
2790 if (vfsp->vfs_resource != NULL) {
2791 refstr_rele(vfsp->vfs_resource);
2792 vfsp->vfs_resource = NULL;
2795 * Now free mount options information
2797 vfs_freeopttbl(&vfsp->vfs_mntopts);
2801 * Return the last mnttab modification time
2803 void
2804 vfs_mnttab_modtime(timespec_t *ts)
2806 ASSERT(RW_LOCK_HELD(&vfslist));
2807 *ts = vfs_mnttab_mtime;
2811 * See if mnttab is changed
2813 void
2814 vfs_mnttab_poll(timespec_t *old, struct pollhead **phpp)
2816 int changed;
2818 *phpp = NULL;
2821 * Note: don't grab vfs list lock before accessing vfs_mnttab_mtime.
2822 * Can lead to deadlock against vfs_mnttab_modtimeupd(). It is safe
2823 * to not grab the vfs list lock because tv_sec is monotonically
2824 * increasing.
2827 changed = (old->tv_nsec != vfs_mnttab_mtime.tv_nsec) ||
2828 (old->tv_sec != vfs_mnttab_mtime.tv_sec);
2829 if (!changed) {
2830 *phpp = &vfs_pollhd;
2834 /* Provide a unique and monotonically-increasing timestamp. */
2835 void
2836 vfs_mono_time(timespec_t *ts)
2838 static volatile hrtime_t hrt; /* The saved time. */
2839 hrtime_t newhrt, oldhrt; /* For effecting the CAS. */
2840 timespec_t newts;
2843 * Try gethrestime() first, but be prepared to fabricate a sensible
2844 * answer at the first sign of any trouble.
2846 gethrestime(&newts);
2847 newhrt = ts2hrt(&newts);
2848 for (;;) {
2849 oldhrt = hrt;
2850 if (newhrt <= hrt)
2851 newhrt = hrt + 1;
2852 if (atomic_cas_64((uint64_t *)&hrt, oldhrt, newhrt) == oldhrt)
2853 break;
2855 hrt2ts(newhrt, ts);
2859 * Update the mnttab modification time and wake up any waiters for
2860 * mnttab changes
2862 void
2863 vfs_mnttab_modtimeupd()
2865 hrtime_t oldhrt, newhrt;
2867 ASSERT(RW_WRITE_HELD(&vfslist));
2868 oldhrt = ts2hrt(&vfs_mnttab_mtime);
2869 gethrestime(&vfs_mnttab_mtime);
2870 newhrt = ts2hrt(&vfs_mnttab_mtime);
2871 if (oldhrt == (hrtime_t)0)
2872 vfs_mnttab_ctime = vfs_mnttab_mtime;
2874 * Attempt to provide unique mtime (like uniqtime but not).
2876 if (newhrt == oldhrt) {
2877 newhrt++;
2878 hrt2ts(newhrt, &vfs_mnttab_mtime);
2880 pollwakeup(&vfs_pollhd, (short)POLLRDBAND);
2881 vfs_mnttab_writeop();
2885 dounmount(struct vfs *vfsp, int flag, cred_t *cr)
2887 vnode_t *coveredvp;
2888 int error;
2889 extern void teardown_vopstats(vfs_t *);
2892 * Get covered vnode. This will be NULL if the vfs is not linked
2893 * into the file system name space (i.e., domount() with MNT_NOSPICE).
2895 coveredvp = vfsp->vfs_vnodecovered;
2896 ASSERT(coveredvp == NULL || vn_vfswlock_held(coveredvp));
2899 * Purge all dnlc entries for this vfs.
2901 (void) dnlc_purge_vfsp(vfsp, 0);
2903 /* For forcible umount, skip VFS_SYNC() since it may hang */
2904 if ((flag & MS_FORCE) == 0)
2905 (void) VFS_SYNC(vfsp, 0, cr);
2908 * Lock the vfs to maintain fs status quo during unmount. This
2909 * has to be done after the sync because ufs_update tries to acquire
2910 * the vfs_reflock.
2912 vfs_lock_wait(vfsp);
2914 if (error = VFS_UNMOUNT(vfsp, flag, cr)) {
2915 vfs_unlock(vfsp);
2916 if (coveredvp != NULL)
2917 vn_vfsunlock(coveredvp);
2918 } else if (coveredvp != NULL) {
2919 teardown_vopstats(vfsp);
2921 * vfs_remove() will do a VN_RELE(vfsp->vfs_vnodecovered)
2922 * when it frees vfsp so we do a VN_HOLD() so we can
2923 * continue to use coveredvp afterwards.
2925 VN_HOLD(coveredvp);
2926 vfs_remove(vfsp);
2927 vn_vfsunlock(coveredvp);
2928 VN_RELE(coveredvp);
2929 } else {
2930 teardown_vopstats(vfsp);
2932 * Release the reference to vfs that is not linked
2933 * into the name space.
2935 vfs_unlock(vfsp);
2936 VFS_RELE(vfsp);
2938 return (error);
2943 * Vfs_unmountall() is called by uadmin() to unmount all
2944 * mounted file systems (except the root file system) during shutdown.
2945 * It follows the existing locking protocol when traversing the vfs list
2946 * to sync and unmount vfses. Even though there should be no
2947 * other thread running while the system is shutting down, it is prudent
2948 * to still follow the locking protocol.
2950 void
2951 vfs_unmountall(void)
2953 struct vfs *vfsp;
2954 struct vfs *prev_vfsp = NULL;
2955 int error;
2958 * Toss all dnlc entries now so that the per-vfs sync
2959 * and unmount operations don't have to slog through
2960 * a bunch of uninteresting vnodes over and over again.
2962 dnlc_purge();
2964 vfs_list_lock();
2965 for (vfsp = rootvfs->vfs_prev; vfsp != rootvfs; vfsp = prev_vfsp) {
2966 prev_vfsp = vfsp->vfs_prev;
2968 if (vfs_lock(vfsp) != 0)
2969 continue;
2970 error = vn_vfswlock(vfsp->vfs_vnodecovered);
2971 vfs_unlock(vfsp);
2972 if (error)
2973 continue;
2975 vfs_list_unlock();
2977 (void) VFS_SYNC(vfsp, SYNC_CLOSE, CRED());
2978 (void) dounmount(vfsp, 0, CRED());
2981 * Since we dropped the vfslist lock above we must
2982 * verify that next_vfsp still exists, else start over.
2984 vfs_list_lock();
2985 for (vfsp = rootvfs->vfs_prev;
2986 vfsp != rootvfs; vfsp = vfsp->vfs_prev)
2987 if (vfsp == prev_vfsp)
2988 break;
2989 if (vfsp == rootvfs && prev_vfsp != rootvfs)
2990 prev_vfsp = rootvfs->vfs_prev;
2992 vfs_list_unlock();
2996 * Called to add an entry to the end of the vfs mount in progress list
2998 void
2999 vfs_addmip(dev_t dev, struct vfs *vfsp)
3001 struct ipmnt *mipp;
3003 mipp = kmem_alloc(sizeof (struct ipmnt), KM_SLEEP);
3004 mipp->mip_next = NULL;
3005 mipp->mip_dev = dev;
3006 mipp->mip_vfsp = vfsp;
3007 mutex_enter(&vfs_miplist_mutex);
3008 if (vfs_miplist_end != NULL)
3009 vfs_miplist_end->mip_next = mipp;
3010 else
3011 vfs_miplist = mipp;
3012 vfs_miplist_end = mipp;
3013 mutex_exit(&vfs_miplist_mutex);
3017 * Called to remove an entry from the mount in progress list
3018 * Either because the mount completed or it failed.
3020 void
3021 vfs_delmip(struct vfs *vfsp)
3023 struct ipmnt *mipp, *mipprev;
3025 mutex_enter(&vfs_miplist_mutex);
3026 mipprev = NULL;
3027 for (mipp = vfs_miplist;
3028 mipp && mipp->mip_vfsp != vfsp; mipp = mipp->mip_next) {
3029 mipprev = mipp;
3031 if (mipp == NULL)
3032 return; /* shouldn't happen */
3033 if (mipp == vfs_miplist_end)
3034 vfs_miplist_end = mipprev;
3035 if (mipprev == NULL)
3036 vfs_miplist = mipp->mip_next;
3037 else
3038 mipprev->mip_next = mipp->mip_next;
3039 mutex_exit(&vfs_miplist_mutex);
3040 kmem_free(mipp, sizeof (struct ipmnt));
3044 * vfs_add is called by a specific filesystem's mount routine to add
3045 * the new vfs into the vfs list/hash and to cover the mounted-on vnode.
3046 * The vfs should already have been locked by the caller.
3048 * coveredvp is NULL if this is the root.
3050 void
3051 vfs_add(vnode_t *coveredvp, struct vfs *vfsp, int mflag)
3053 int newflag;
3055 ASSERT(vfs_lock_held(vfsp));
3056 VFS_HOLD(vfsp);
3057 newflag = vfsp->vfs_flag;
3058 if (mflag & MS_RDONLY)
3059 newflag |= VFS_RDONLY;
3060 else
3061 newflag &= ~VFS_RDONLY;
3062 if (mflag & MS_NOSUID)
3063 newflag |= (VFS_NOSETUID|VFS_NODEVICES);
3064 else
3065 newflag &= ~(VFS_NOSETUID|VFS_NODEVICES);
3066 if (mflag & MS_NOMNTTAB)
3067 newflag |= VFS_NOMNTTAB;
3068 else
3069 newflag &= ~VFS_NOMNTTAB;
3071 if (coveredvp != NULL) {
3072 ASSERT(vn_vfswlock_held(coveredvp));
3073 coveredvp->v_vfsmountedhere = vfsp;
3074 VN_HOLD(coveredvp);
3076 vfsp->vfs_vnodecovered = coveredvp;
3077 vfsp->vfs_flag = newflag;
3079 vfs_list_add(vfsp);
3083 * Remove a vfs from the vfs list, null out the pointer from the
3084 * covered vnode to the vfs (v_vfsmountedhere), and null out the pointer
3085 * from the vfs to the covered vnode (vfs_vnodecovered). Release the
3086 * reference to the vfs and to the covered vnode.
3088 * Called from dounmount after it's confirmed with the file system
3089 * that the unmount is legal.
3091 void
3092 vfs_remove(struct vfs *vfsp)
3094 vnode_t *vp;
3096 ASSERT(vfs_lock_held(vfsp));
3099 * Can't unmount root. Should never happen because fs will
3100 * be busy.
3102 if (vfsp == rootvfs)
3103 panic("vfs_remove: unmounting root");
3105 vfs_list_remove(vfsp);
3108 * Unhook from the file system name space.
3110 vp = vfsp->vfs_vnodecovered;
3111 ASSERT(vn_vfswlock_held(vp));
3112 vp->v_vfsmountedhere = NULL;
3113 vfsp->vfs_vnodecovered = NULL;
3114 VN_RELE(vp);
3117 * Release lock and wakeup anybody waiting.
3119 vfs_unlock(vfsp);
3120 VFS_RELE(vfsp);
3124 * Lock a filesystem to prevent access to it while mounting,
3125 * unmounting and syncing. Return EBUSY immediately if lock
3126 * can't be acquired.
3129 vfs_lock(vfs_t *vfsp)
3131 vn_vfslocks_entry_t *vpvfsentry;
3133 vpvfsentry = vn_vfslocks_getlock(vfsp);
3134 if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER))
3135 return (0);
3137 vn_vfslocks_rele(vpvfsentry);
3138 return (EBUSY);
3142 vfs_rlock(vfs_t *vfsp)
3144 vn_vfslocks_entry_t *vpvfsentry;
3146 vpvfsentry = vn_vfslocks_getlock(vfsp);
3148 if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER))
3149 return (0);
3151 vn_vfslocks_rele(vpvfsentry);
3152 return (EBUSY);
3155 void
3156 vfs_lock_wait(vfs_t *vfsp)
3158 vn_vfslocks_entry_t *vpvfsentry;
3160 vpvfsentry = vn_vfslocks_getlock(vfsp);
3161 rwst_enter(&vpvfsentry->ve_lock, RW_WRITER);
3164 void
3165 vfs_rlock_wait(vfs_t *vfsp)
3167 vn_vfslocks_entry_t *vpvfsentry;
3169 vpvfsentry = vn_vfslocks_getlock(vfsp);
3170 rwst_enter(&vpvfsentry->ve_lock, RW_READER);
3174 * Unlock a locked filesystem.
3176 void
3177 vfs_unlock(vfs_t *vfsp)
3179 vn_vfslocks_entry_t *vpvfsentry;
3182 * vfs_unlock will mimic sema_v behaviour to fix 4748018.
3183 * And these changes should remain for the patch changes as it is.
3185 if (panicstr)
3186 return;
3189 * ve_refcount needs to be dropped twice here.
3190 * 1. To release refernce after a call to vfs_locks_getlock()
3191 * 2. To release the reference from the locking routines like
3192 * vfs_rlock_wait/vfs_wlock_wait/vfs_wlock etc,.
3195 vpvfsentry = vn_vfslocks_getlock(vfsp);
3196 vn_vfslocks_rele(vpvfsentry);
3198 rwst_exit(&vpvfsentry->ve_lock);
3199 vn_vfslocks_rele(vpvfsentry);
3203 * Utility routine that allows a filesystem to construct its
3204 * fsid in "the usual way" - by munging some underlying dev_t and
3205 * the filesystem type number into the 64-bit fsid. Note that
3206 * this implicitly relies on dev_t persistence to make filesystem
3207 * id's persistent.
3209 * There's nothing to prevent an individual fs from constructing its
3210 * fsid in a different way, and indeed they should.
3212 * Since we want fsids to be 32-bit quantities (so that they can be
3213 * exported identically by either 32-bit or 64-bit APIs, as well as
3214 * the fact that fsid's are "known" to NFS), we compress the device
3215 * number given down to 32-bits, and panic if that isn't possible.
3217 void
3218 vfs_make_fsid(fsid_t *fsi, dev_t dev, int val)
3220 if (!cmpldev((dev32_t *)&fsi->val[0], dev))
3221 panic("device number too big for fsid!");
3222 fsi->val[1] = val;
3226 vfs_lock_held(vfs_t *vfsp)
3228 int held;
3229 vn_vfslocks_entry_t *vpvfsentry;
3232 * vfs_lock_held will mimic sema_held behaviour
3233 * if panicstr is set. And these changes should remain
3234 * for the patch changes as it is.
3236 if (panicstr)
3237 return (1);
3239 vpvfsentry = vn_vfslocks_getlock(vfsp);
3240 held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER);
3242 vn_vfslocks_rele(vpvfsentry);
3243 return (held);
3246 struct _kthread *
3247 vfs_lock_owner(vfs_t *vfsp)
3249 struct _kthread *owner;
3250 vn_vfslocks_entry_t *vpvfsentry;
3253 * vfs_wlock_held will mimic sema_held behaviour
3254 * if panicstr is set. And these changes should remain
3255 * for the patch changes as it is.
3257 if (panicstr)
3258 return (NULL);
3260 vpvfsentry = vn_vfslocks_getlock(vfsp);
3261 owner = rwst_owner(&vpvfsentry->ve_lock);
3263 vn_vfslocks_rele(vpvfsentry);
3264 return (owner);
3268 * vfs list locking.
3270 * Rather than manipulate the vfslist lock directly, we abstract into lock
3271 * and unlock routines to allow the locking implementation to be changed for
3272 * clustering.
3274 * Whenever the vfs list is modified through its hash links, the overall list
3275 * lock must be obtained before locking the relevant hash bucket. But to see
3276 * whether a given vfs is on the list, it suffices to obtain the lock for the
3277 * hash bucket without getting the overall list lock. (See getvfs() below.)
3280 void
3281 vfs_list_lock()
3283 rw_enter(&vfslist, RW_WRITER);
3286 void
3287 vfs_list_read_lock()
3289 rw_enter(&vfslist, RW_READER);
3292 void
3293 vfs_list_unlock()
3295 rw_exit(&vfslist);
3299 * Low level worker routines for adding entries to and removing entries from
3300 * the vfs list.
3303 static void
3304 vfs_hash_add(struct vfs *vfsp, int insert_at_head)
3306 int vhno;
3307 struct vfs **hp;
3308 dev_t dev;
3310 ASSERT(RW_WRITE_HELD(&vfslist));
3312 dev = expldev(vfsp->vfs_fsid.val[0]);
3313 vhno = VFSHASH(getmajor(dev), getminor(dev));
3315 mutex_enter(&rvfs_list[vhno].rvfs_lock);
3318 * Link into the hash table, inserting it at the end, so that LOFS
3319 * with the same fsid as UFS (or other) file systems will not hide the
3320 * UFS.
3322 if (insert_at_head) {
3323 vfsp->vfs_hash = rvfs_list[vhno].rvfs_head;
3324 rvfs_list[vhno].rvfs_head = vfsp;
3325 } else {
3326 for (hp = &rvfs_list[vhno].rvfs_head; *hp != NULL;
3327 hp = &(*hp)->vfs_hash)
3328 continue;
3330 * hp now contains the address of the pointer to update
3331 * to effect the insertion.
3333 vfsp->vfs_hash = NULL;
3334 *hp = vfsp;
3337 rvfs_list[vhno].rvfs_len++;
3338 mutex_exit(&rvfs_list[vhno].rvfs_lock);
3342 static void
3343 vfs_hash_remove(struct vfs *vfsp)
3345 int vhno;
3346 struct vfs *tvfsp;
3347 dev_t dev;
3349 ASSERT(RW_WRITE_HELD(&vfslist));
3351 dev = expldev(vfsp->vfs_fsid.val[0]);
3352 vhno = VFSHASH(getmajor(dev), getminor(dev));
3354 mutex_enter(&rvfs_list[vhno].rvfs_lock);
3357 * Remove from hash.
3359 if (rvfs_list[vhno].rvfs_head == vfsp) {
3360 rvfs_list[vhno].rvfs_head = vfsp->vfs_hash;
3361 rvfs_list[vhno].rvfs_len--;
3362 goto foundit;
3364 for (tvfsp = rvfs_list[vhno].rvfs_head; tvfsp != NULL;
3365 tvfsp = tvfsp->vfs_hash) {
3366 if (tvfsp->vfs_hash == vfsp) {
3367 tvfsp->vfs_hash = vfsp->vfs_hash;
3368 rvfs_list[vhno].rvfs_len--;
3369 goto foundit;
3372 cmn_err(CE_WARN, "vfs_list_remove: vfs not found in hash");
3374 foundit:
3376 mutex_exit(&rvfs_list[vhno].rvfs_lock);
3380 void
3381 vfs_list_add(struct vfs *vfsp)
3383 zone_t *zone;
3386 * Typically, the vfs_t will have been created on behalf of the file
3387 * system in vfs_init, where it will have been provided with a
3388 * vfs_impl_t. This, however, might be lacking if the vfs_t was created
3389 * by an unbundled file system. We therefore check for such an example
3390 * before stamping the vfs_t with its creation time for the benefit of
3391 * mntfs.
3393 if (vfsp->vfs_implp == NULL)
3394 vfsimpl_setup(vfsp);
3395 vfs_mono_time(&vfsp->vfs_hrctime);
3398 * The zone that owns the mount is the one that performed the mount.
3399 * Note that this isn't necessarily the same as the zone mounted into.
3400 * The corresponding zone_rele_ref() will be done when the vfs_t
3401 * is being free'd.
3403 vfsp->vfs_zone = curproc->p_zone;
3404 zone_init_ref(&vfsp->vfs_implp->vi_zone_ref);
3405 zone_hold_ref(vfsp->vfs_zone, &vfsp->vfs_implp->vi_zone_ref,
3406 ZONE_REF_VFS);
3409 * Find the zone mounted into, and put this mount on its vfs list.
3411 zone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
3412 ASSERT(zone != NULL);
3414 * Special casing for the root vfs. This structure is allocated
3415 * statically and hooked onto rootvfs at link time. During the
3416 * vfs_mountroot call at system startup time, the root file system's
3417 * VFS_MOUNTROOT routine will call vfs_add with this root vfs struct
3418 * as argument. The code below must detect and handle this special
3419 * case. The only apparent justification for this special casing is
3420 * to ensure that the root file system appears at the head of the
3421 * list.
3423 * XXX: I'm assuming that it's ok to do normal list locking when
3424 * adding the entry for the root file system (this used to be
3425 * done with no locks held).
3427 vfs_list_lock();
3429 * Link into the vfs list proper.
3431 if (vfsp == &root) {
3433 * Assert: This vfs is already on the list as its first entry.
3434 * Thus, there's nothing to do.
3436 ASSERT(rootvfs == vfsp);
3438 * Add it to the head of the global zone's vfslist.
3440 ASSERT(zone == global_zone);
3441 ASSERT(zone->zone_vfslist == NULL);
3442 zone->zone_vfslist = vfsp;
3443 } else {
3445 * Link to end of list using vfs_prev (as rootvfs is now a
3446 * doubly linked circular list) so list is in mount order for
3447 * mnttab use.
3449 rootvfs->vfs_prev->vfs_next = vfsp;
3450 vfsp->vfs_prev = rootvfs->vfs_prev;
3451 rootvfs->vfs_prev = vfsp;
3452 vfsp->vfs_next = rootvfs;
3455 * Do it again for the zone-private list (which may be NULL).
3457 if (zone->zone_vfslist == NULL) {
3458 ASSERT(zone != global_zone);
3459 zone->zone_vfslist = vfsp;
3460 } else {
3461 zone->zone_vfslist->vfs_zone_prev->vfs_zone_next = vfsp;
3462 vfsp->vfs_zone_prev = zone->zone_vfslist->vfs_zone_prev;
3463 zone->zone_vfslist->vfs_zone_prev = vfsp;
3464 vfsp->vfs_zone_next = zone->zone_vfslist;
3469 * Link into the hash table, inserting it at the end, so that LOFS
3470 * with the same fsid as UFS (or other) file systems will not hide
3471 * the UFS.
3473 vfs_hash_add(vfsp, 0);
3476 * update the mnttab modification time
3478 vfs_mnttab_modtimeupd();
3479 vfs_list_unlock();
3480 zone_rele(zone);
3483 void
3484 vfs_list_remove(struct vfs *vfsp)
3486 zone_t *zone;
3488 zone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
3489 ASSERT(zone != NULL);
3491 * Callers are responsible for preventing attempts to unmount the
3492 * root.
3494 ASSERT(vfsp != rootvfs);
3496 vfs_list_lock();
3499 * Remove from hash.
3501 vfs_hash_remove(vfsp);
3504 * Remove from vfs list.
3506 vfsp->vfs_prev->vfs_next = vfsp->vfs_next;
3507 vfsp->vfs_next->vfs_prev = vfsp->vfs_prev;
3508 vfsp->vfs_next = vfsp->vfs_prev = NULL;
3511 * Remove from zone-specific vfs list.
3513 if (zone->zone_vfslist == vfsp)
3514 zone->zone_vfslist = vfsp->vfs_zone_next;
3516 if (vfsp->vfs_zone_next == vfsp) {
3517 ASSERT(vfsp->vfs_zone_prev == vfsp);
3518 ASSERT(zone->zone_vfslist == vfsp);
3519 zone->zone_vfslist = NULL;
3522 vfsp->vfs_zone_prev->vfs_zone_next = vfsp->vfs_zone_next;
3523 vfsp->vfs_zone_next->vfs_zone_prev = vfsp->vfs_zone_prev;
3524 vfsp->vfs_zone_next = vfsp->vfs_zone_prev = NULL;
3527 * update the mnttab modification time
3529 vfs_mnttab_modtimeupd();
3530 vfs_list_unlock();
3531 zone_rele(zone);
3534 struct vfs *
3535 getvfs(fsid_t *fsid)
3537 struct vfs *vfsp;
3538 int val0 = fsid->val[0];
3539 int val1 = fsid->val[1];
3540 dev_t dev = expldev(val0);
3541 int vhno = VFSHASH(getmajor(dev), getminor(dev));
3542 kmutex_t *hmp = &rvfs_list[vhno].rvfs_lock;
3544 mutex_enter(hmp);
3545 for (vfsp = rvfs_list[vhno].rvfs_head; vfsp; vfsp = vfsp->vfs_hash) {
3546 if (vfsp->vfs_fsid.val[0] == val0 &&
3547 vfsp->vfs_fsid.val[1] == val1) {
3548 VFS_HOLD(vfsp);
3549 mutex_exit(hmp);
3550 return (vfsp);
3553 mutex_exit(hmp);
3554 return (NULL);
3558 * Search the vfs mount in progress list for a specified device/vfs entry.
3559 * Returns 0 if the first entry in the list that the device matches has the
3560 * given vfs pointer as well. If the device matches but a different vfs
3561 * pointer is encountered in the list before the given vfs pointer then
3562 * a 1 is returned.
3566 vfs_devmounting(dev_t dev, struct vfs *vfsp)
3568 int retval = 0;
3569 struct ipmnt *mipp;
3571 mutex_enter(&vfs_miplist_mutex);
3572 for (mipp = vfs_miplist; mipp != NULL; mipp = mipp->mip_next) {
3573 if (mipp->mip_dev == dev) {
3574 if (mipp->mip_vfsp != vfsp)
3575 retval = 1;
3576 break;
3579 mutex_exit(&vfs_miplist_mutex);
3580 return (retval);
3584 * Search the vfs list for a specified device. Returns 1, if entry is found
3585 * or 0 if no suitable entry is found.
3589 vfs_devismounted(dev_t dev)
3591 struct vfs *vfsp;
3592 int found;
3594 vfs_list_read_lock();
3595 vfsp = rootvfs;
3596 found = 0;
3597 do {
3598 if (vfsp->vfs_dev == dev) {
3599 found = 1;
3600 break;
3602 vfsp = vfsp->vfs_next;
3603 } while (vfsp != rootvfs);
3605 vfs_list_unlock();
3606 return (found);
3610 * Search the vfs list for a specified device. Returns a pointer to it
3611 * or NULL if no suitable entry is found. The caller of this routine
3612 * is responsible for releasing the returned vfs pointer.
3614 struct vfs *
3615 vfs_dev2vfsp(dev_t dev)
3617 struct vfs *vfsp;
3618 int found;
3620 vfs_list_read_lock();
3621 vfsp = rootvfs;
3622 found = 0;
3623 do {
3625 * The following could be made more efficient by making
3626 * the entire loop use vfs_zone_next if the call is from
3627 * a zone. The only callers, however, ustat(2) and
3628 * umount2(2), don't seem to justify the added
3629 * complexity at present.
3631 if (vfsp->vfs_dev == dev &&
3632 ZONE_PATH_VISIBLE(refstr_value(vfsp->vfs_mntpt),
3633 curproc->p_zone)) {
3634 VFS_HOLD(vfsp);
3635 found = 1;
3636 break;
3638 vfsp = vfsp->vfs_next;
3639 } while (vfsp != rootvfs);
3640 vfs_list_unlock();
3641 return (found ? vfsp: NULL);
3645 * Search the vfs list for a specified mntpoint. Returns a pointer to it
3646 * or NULL if no suitable entry is found. The caller of this routine
3647 * is responsible for releasing the returned vfs pointer.
3649 * Note that if multiple mntpoints match, the last one matching is
3650 * returned in an attempt to return the "top" mount when overlay
3651 * mounts are covering the same mount point. This is accomplished by starting
3652 * at the end of the list and working our way backwards, stopping at the first
3653 * matching mount.
3655 struct vfs *
3656 vfs_mntpoint2vfsp(const char *mp)
3658 struct vfs *vfsp;
3659 struct vfs *retvfsp = NULL;
3660 zone_t *zone = curproc->p_zone;
3661 struct vfs *list;
3663 vfs_list_read_lock();
3664 if (getzoneid() == GLOBAL_ZONEID) {
3666 * The global zone may see filesystems in any zone.
3668 vfsp = rootvfs->vfs_prev;
3669 do {
3670 if (strcmp(refstr_value(vfsp->vfs_mntpt), mp) == 0) {
3671 retvfsp = vfsp;
3672 break;
3674 vfsp = vfsp->vfs_prev;
3675 } while (vfsp != rootvfs->vfs_prev);
3676 } else if ((list = zone->zone_vfslist) != NULL) {
3677 const char *mntpt;
3679 vfsp = list->vfs_zone_prev;
3680 do {
3681 mntpt = refstr_value(vfsp->vfs_mntpt);
3682 mntpt = ZONE_PATH_TRANSLATE(mntpt, zone);
3683 if (strcmp(mntpt, mp) == 0) {
3684 retvfsp = vfsp;
3685 break;
3687 vfsp = vfsp->vfs_zone_prev;
3688 } while (vfsp != list->vfs_zone_prev);
3690 if (retvfsp)
3691 VFS_HOLD(retvfsp);
3692 vfs_list_unlock();
3693 return (retvfsp);
3697 * Search the vfs list for a specified vfsops.
3698 * if vfs entry is found then return 1, else 0.
3701 vfs_opsinuse(const struct vfsops *ops)
3703 struct vfs *vfsp;
3704 int found;
3706 vfs_list_read_lock();
3707 vfsp = rootvfs;
3708 found = 0;
3709 do {
3710 if (vfs_getops(vfsp) == ops) {
3711 found = 1;
3712 break;
3714 vfsp = vfsp->vfs_next;
3715 } while (vfsp != rootvfs);
3716 vfs_list_unlock();
3717 return (found);
3721 * Allocate an entry in vfssw for a file system type
3723 struct vfssw *
3724 allocate_vfssw(const char *type)
3726 struct vfssw *vswp;
3728 if (type[0] == '\0' || strlen(type) + 1 > _ST_FSTYPSZ) {
3730 * The vfssw table uses the empty string to identify an
3731 * available entry; we cannot add any type which has
3732 * a leading NUL. The string length is limited to
3733 * the size of the st_fstype array in struct stat.
3735 return (NULL);
3738 ASSERT(VFSSW_WRITE_LOCKED());
3739 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++)
3740 if (!ALLOCATED_VFSSW(vswp)) {
3741 vswp->vsw_name = kmem_alloc(strlen(type) + 1, KM_SLEEP);
3742 (void) strcpy(vswp->vsw_name, type);
3743 ASSERT(vswp->vsw_count == 0);
3744 vswp->vsw_count = 1;
3745 mutex_init(&vswp->vsw_lock, NULL, MUTEX_DEFAULT, NULL);
3746 return (vswp);
3748 return (NULL);
3752 * Impose additional layer of translation between vfstype names
3753 * and module names in the filesystem.
3755 static const char *
3756 vfs_to_modname(const char *vfstype)
3758 if (strcmp(vfstype, "proc") == 0) {
3759 vfstype = "procfs";
3760 } else if (strcmp(vfstype, "fd") == 0) {
3761 vfstype = "fdfs";
3762 } else if (strncmp(vfstype, "nfs", 3) == 0) {
3763 vfstype = "nfs";
3766 return (vfstype);
3770 * Find a vfssw entry given a file system type name.
3771 * Try to autoload the filesystem if it's not found.
3772 * If it's installed, return the vfssw locked to prevent unloading.
3774 struct vfssw *
3775 vfs_getvfssw(const char *type)
3777 struct vfssw *vswp;
3778 const char *modname;
3780 RLOCK_VFSSW();
3781 vswp = vfs_getvfsswbyname(type);
3782 modname = vfs_to_modname(type);
3784 if (rootdir == NULL) {
3786 * If we haven't yet loaded the root file system, then our
3787 * _init won't be called until later. Allocate vfssw entry,
3788 * because mod_installfs won't be called.
3790 if (vswp == NULL) {
3791 RUNLOCK_VFSSW();
3792 WLOCK_VFSSW();
3793 if ((vswp = vfs_getvfsswbyname(type)) == NULL) {
3794 if ((vswp = allocate_vfssw(type)) == NULL) {
3795 WUNLOCK_VFSSW();
3796 return (NULL);
3799 WUNLOCK_VFSSW();
3800 RLOCK_VFSSW();
3802 if (!VFS_INSTALLED(vswp)) {
3803 RUNLOCK_VFSSW();
3804 (void) modloadonly("fs", modname);
3805 } else
3806 RUNLOCK_VFSSW();
3807 return (vswp);
3811 * Try to load the filesystem. Before calling modload(), we drop
3812 * our lock on the VFS switch table, and pick it up after the
3813 * module is loaded. However, there is a potential race: the
3814 * module could be unloaded after the call to modload() completes
3815 * but before we pick up the lock and drive on. Therefore,
3816 * we keep reloading the module until we've loaded the module
3817 * _and_ we have the lock on the VFS switch table.
3819 while (vswp == NULL || !VFS_INSTALLED(vswp)) {
3820 RUNLOCK_VFSSW();
3821 if (modload("fs", modname) == -1)
3822 return (NULL);
3823 RLOCK_VFSSW();
3824 if (vswp == NULL)
3825 if ((vswp = vfs_getvfsswbyname(type)) == NULL)
3826 break;
3828 RUNLOCK_VFSSW();
3830 return (vswp);
3834 * Find a vfssw entry given a file system type name.
3836 struct vfssw *
3837 vfs_getvfsswbyname(const char *type)
3839 struct vfssw *vswp;
3841 ASSERT(VFSSW_LOCKED());
3842 if (type == NULL || *type == '\0')
3843 return (NULL);
3845 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
3846 if (strcmp(type, vswp->vsw_name) == 0) {
3847 vfs_refvfssw(vswp);
3848 return (vswp);
3852 return (NULL);
3856 * Find a vfssw entry given a set of vfsops.
3858 struct vfssw *
3859 vfs_getvfsswbyvfsops(const struct vfsops *ops)
3861 struct vfssw *vswp;
3863 RLOCK_VFSSW();
3864 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
3865 if (ALLOCATED_VFSSW(vswp) && &vswp->vsw_vfsops == ops) {
3866 vfs_refvfssw(vswp);
3867 RUNLOCK_VFSSW();
3868 return (vswp);
3871 RUNLOCK_VFSSW();
3873 return (NULL);
3877 * Reference a vfssw entry.
3879 void
3880 vfs_refvfssw(struct vfssw *vswp)
3883 mutex_enter(&vswp->vsw_lock);
3884 vswp->vsw_count++;
3885 mutex_exit(&vswp->vsw_lock);
3889 * Unreference a vfssw entry.
3891 void
3892 vfs_unrefvfssw(struct vfssw *vswp)
3895 mutex_enter(&vswp->vsw_lock);
3896 vswp->vsw_count--;
3897 mutex_exit(&vswp->vsw_lock);
3900 static int sync_retries = 20; /* number of retries when not making progress */
3901 static int sync_triesleft; /* portion of sync_retries remaining */
3903 static pgcnt_t old_pgcnt, new_pgcnt;
3904 static int new_bufcnt, old_bufcnt;
3907 * Sync all of the mounted filesystems, and then wait for the actual i/o to
3908 * complete. We wait by counting the number of dirty pages and buffers,
3909 * pushing them out using bio_busy() and page_busy(), and then counting again.
3910 * This routine is used during the uadmin A_SHUTDOWN code. It should only
3911 * be used after some higher-level mechanism has quiesced the system so that
3912 * new writes are not being initiated while we are waiting for completion.
3914 * To ensure finite running time, our algorithm uses sync_triesleft (a progress
3915 * counter used by the vfs_syncall() loop below). It is declared above so
3916 * it can be found easily in the debugger.
3918 * The sync_triesleft counter is updated by vfs_syncall() itself. If we make
3919 * sync_retries consecutive calls to bio_busy() and page_busy() without
3920 * decreasing either the number of dirty buffers or dirty pages below the
3921 * lowest count we have seen so far, we give up and return from vfs_syncall().
3923 * Each loop iteration ends with a call to delay() one second to allow time for
3924 * i/o completion and to permit the user time to read our progress messages.
3926 void
3927 vfs_syncall(void)
3929 if (rootdir == NULL && !modrootloaded)
3930 return; /* no filesystems have been loaded yet */
3932 printf("syncing file systems...");
3933 sync();
3935 sync_triesleft = sync_retries;
3937 old_bufcnt = new_bufcnt = INT_MAX;
3938 old_pgcnt = new_pgcnt = ULONG_MAX;
3940 while (sync_triesleft > 0) {
3941 old_bufcnt = MIN(old_bufcnt, new_bufcnt);
3942 old_pgcnt = MIN(old_pgcnt, new_pgcnt);
3944 new_bufcnt = bio_busy(B_TRUE);
3945 new_pgcnt = page_busy(B_TRUE);
3947 if (new_bufcnt == 0 && new_pgcnt == 0)
3948 break;
3950 if (new_bufcnt < old_bufcnt || new_pgcnt < old_pgcnt)
3951 sync_triesleft = sync_retries;
3952 else
3953 sync_triesleft--;
3955 if (new_bufcnt)
3956 printf(" [%d]", new_bufcnt);
3957 if (new_pgcnt)
3958 printf(" %lu", new_pgcnt);
3960 ddi_sleep(1);
3963 if (new_bufcnt != 0 || new_pgcnt != 0)
3964 printf(" done (not all i/o completed)\n");
3965 else
3966 printf(" done\n");
3968 ddi_sleep(1);
3972 * Map VFS flags to statvfs flags. These shouldn't really be separate
3973 * flags at all.
3975 uint_t
3976 vf_to_stf(uint_t vf)
3978 uint_t stf = 0;
3980 if (vf & VFS_RDONLY)
3981 stf |= ST_RDONLY;
3982 if (vf & VFS_NOSETUID)
3983 stf |= ST_NOSUID;
3984 if (vf & VFS_NOTRUNC)
3985 stf |= ST_NOTRUNC;
3987 return (stf);
3991 * Entries for (illegal) fstype 0.
3993 /* ARGSUSED */
3995 vfsstray_sync(struct vfs *vfsp, short arg, struct cred *cr)
3997 cmn_err(CE_PANIC, "stray vfs operation");
3998 return (0);
4002 * Entries for (illegal) fstype 0.
4005 vfsstray(void)
4007 cmn_err(CE_PANIC, "stray vfs operation");
4008 return (0);
4012 * Support for dealing with forced UFS unmount and its interaction with
4013 * LOFS. Could be used by any filesystem.
4014 * See bug 1203132.
4017 vfs_EIO(void)
4019 return (EIO);
4023 * We've gotta define the op for sync separately, since the compiler gets
4024 * confused if we mix and match ANSI and normal style prototypes when
4025 * a "short" argument is present and spits out a warning.
4027 /*ARGSUSED*/
4029 vfs_EIO_sync(struct vfs *vfsp, short arg, struct cred *cr)
4031 return (EIO);
4034 vfs_t EIO_vfs;
4036 const struct vfsops EIO_vfsops = {
4037 .vfs_mount = (void *) vfs_EIO,
4038 .vfs_unmount = (void *) vfs_EIO,
4039 .vfs_root = (void *) vfs_EIO,
4040 .vfs_statvfs = (void *) vfs_EIO,
4041 .vfs_sync = (void *) vfs_EIO_sync,
4042 .vfs_vget = (void *) vfs_EIO,
4043 .vfs_mountroot = (void *) vfs_EIO,
4044 .vfs_freevfs = (void *) vfs_EIO,
4045 .vfs_vnstate = (void *) vfs_EIO,
4048 static const struct vfsops stray_vfsops = {
4049 .vfs_mount = (void *) vfsstray,
4050 .vfs_unmount = (void *) vfsstray,
4051 .vfs_root = (void *) vfsstray,
4052 .vfs_statvfs = (void *) vfsstray,
4053 .vfs_sync = (void *) vfsstray_sync,
4054 .vfs_vget = (void *) vfsstray,
4055 .vfs_mountroot = (void *) vfsstray,
4056 .vfs_freevfs = (void *) vfsstray,
4057 .vfs_vnstate = (void *) vfsstray,
4061 * Called from startup() to initialize all loaded vfs's
4063 void
4064 vfsinit(void)
4066 struct vfssw *vswp;
4067 int error;
4068 extern int vopstats_enabled;
4069 extern void vopstats_startup();
4071 /* Create vfs cache */
4072 vfs_cache = kmem_cache_create("vfs_cache", sizeof (struct vfs),
4073 sizeof (uintptr_t), NULL, NULL, NULL, NULL, NULL, 0);
4075 /* Initialize the vnode cache (file systems may use it during init). */
4076 vn_create_cache();
4078 /* Setup event monitor framework */
4079 fem_init();
4081 /* Initialize the dummy stray file system type. */
4082 error = vfs_setfsops(0, &stray_vfsops);
4084 VFS_INIT(&EIO_vfs, &EIO_vfsops, NULL);
4087 * Default EIO_vfs.vfs_flag to VFS_UNMOUNTED so a lookup
4088 * on this vfs can immediately notice it's invalid.
4090 EIO_vfs.vfs_flag |= VFS_UNMOUNTED;
4093 * Call the init routines of non-loadable filesystems only.
4094 * Filesystems which are loaded as separate modules will be
4095 * initialized by the module loading code instead.
4098 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
4099 RLOCK_VFSSW();
4100 if (vswp->vsw_init != NULL)
4101 (*vswp->vsw_init)(vswp - vfssw, vswp->vsw_name);
4102 RUNLOCK_VFSSW();
4105 vopstats_startup();
4107 if (vopstats_enabled) {
4108 /* EIO_vfs can collect stats, but we don't retrieve them */
4109 initialize_vopstats(&EIO_vfs.vfs_vopstats);
4110 EIO_vfs.vfs_fstypevsp = NULL;
4111 EIO_vfs.vfs_vskap = NULL;
4112 EIO_vfs.vfs_flag |= VFS_STATS;
4115 xattr_init();
4117 reparse_point_init();
4120 vfs_t *
4121 vfs_alloc(int kmflag)
4123 vfs_t *vfsp;
4125 vfsp = kmem_cache_alloc(vfs_cache, kmflag);
4128 * Do the simplest initialization here.
4129 * Everything else gets done in vfs_init()
4131 bzero(vfsp, sizeof (vfs_t));
4132 return (vfsp);
4135 void
4136 vfs_free(vfs_t *vfsp)
4139 * One would be tempted to assert that "vfsp->vfs_count == 0".
4140 * The problem is that this gets called out of domount() with
4141 * a partially initialized vfs and a vfs_count of 1. This is
4142 * also called from vfs_rele() with a vfs_count of 0. We can't
4143 * call VFS_RELE() from domount() if VFS_MOUNT() hasn't successfully
4144 * returned. This is because VFS_MOUNT() fully initializes the
4145 * vfs structure and its associated data. VFS_RELE() will call
4146 * VFS_FREEVFS() which may panic the system if the data structures
4147 * aren't fully initialized from a successful VFS_MOUNT()).
4150 /* If FEM was in use, make sure everything gets cleaned up */
4151 if (vfsp->vfs_femhead) {
4152 ASSERT(vfsp->vfs_femhead->femh_list == NULL);
4153 mutex_destroy(&vfsp->vfs_femhead->femh_lock);
4154 kmem_free(vfsp->vfs_femhead, sizeof (*(vfsp->vfs_femhead)));
4155 vfsp->vfs_femhead = NULL;
4158 if (vfsp->vfs_implp)
4159 vfsimpl_teardown(vfsp);
4160 sema_destroy(&vfsp->vfs_reflock);
4161 kmem_cache_free(vfs_cache, vfsp);
4165 * Increments the vfs reference count by one atomically.
4167 void
4168 vfs_hold(vfs_t *vfsp)
4170 atomic_inc_32(&vfsp->vfs_count);
4171 ASSERT(vfsp->vfs_count != 0);
4175 * Decrements the vfs reference count by one atomically. When
4176 * vfs reference count becomes zero, it calls the file system
4177 * specific vfs_freevfs() to free up the resources.
4179 void
4180 vfs_rele(vfs_t *vfsp)
4182 ASSERT(vfsp->vfs_count != 0);
4183 if (atomic_dec_32_nv(&vfsp->vfs_count) == 0) {
4184 VFS_FREEVFS(vfsp);
4185 lofi_remove(vfsp);
4186 if (vfsp->vfs_zone)
4187 zone_rele_ref(&vfsp->vfs_implp->vi_zone_ref,
4188 ZONE_REF_VFS);
4189 vfs_freemnttab(vfsp);
4190 vfs_free(vfsp);
4195 #if defined(__x86)
4196 extern int hvmboot_rootconf();
4197 #endif /* __x86 */
4199 extern ib_boot_prop_t *iscsiboot_prop;
4202 rootconf()
4204 int error;
4205 struct vfssw *vsw;
4206 extern void pm_init();
4207 char *fstyp, *fsmod;
4208 int ret = -1;
4210 getrootfs(&fstyp, &fsmod);
4212 #if defined(__x86)
4214 * hvmboot_rootconf() is defined in the hvm_bootstrap misc module,
4215 * which lives in /platform/i86hvm, and hence is only available when
4216 * booted in an x86 hvm environment. If the hvm_bootstrap misc module
4217 * is not available then the modstub for this function will return 0.
4218 * If the hvm_bootstrap misc module is available it will be loaded
4219 * and hvmboot_rootconf() will be invoked.
4221 if (error = hvmboot_rootconf())
4222 return (error);
4223 #endif /* __x86 */
4225 if (modload("fs", fsmod) == -1)
4226 panic("Cannot _init %s module", fsmod);
4228 RLOCK_VFSSW();
4229 vsw = vfs_getvfsswbyname(fstyp);
4230 RUNLOCK_VFSSW();
4231 if (vsw == NULL) {
4232 cmn_err(CE_CONT, "Cannot find %s filesystem\n", fstyp);
4233 return (ENXIO);
4235 VFS_INIT(rootvfs, &vsw->vsw_vfsops, 0);
4236 VFS_HOLD(rootvfs);
4238 /* always mount readonly first */
4239 rootvfs->vfs_flag |= VFS_RDONLY;
4241 pm_init();
4243 if (netboot && iscsiboot_prop) {
4244 cmn_err(CE_WARN, "NFS boot and iSCSI boot"
4245 " shouldn't happen in the same time");
4246 return (EINVAL);
4249 if (netboot || iscsiboot_prop) {
4250 ret = strplumb();
4251 if (ret != 0) {
4252 cmn_err(CE_WARN, "Cannot plumb network device %d", ret);
4253 return (EFAULT);
4257 if ((ret == 0) && iscsiboot_prop) {
4258 ret = modload("drv", "iscsi");
4259 /* -1 indicates fail */
4260 if (ret == -1) {
4261 cmn_err(CE_WARN, "Failed to load iscsi module");
4262 iscsi_boot_prop_free();
4263 return (EINVAL);
4264 } else {
4265 if (!i_ddi_attach_pseudo_node("iscsi")) {
4266 cmn_err(CE_WARN,
4267 "Failed to attach iscsi driver");
4268 iscsi_boot_prop_free();
4269 return (ENODEV);
4274 error = VFS_MOUNTROOT(rootvfs, ROOT_INIT);
4275 vfs_unrefvfssw(vsw);
4276 rootdev = rootvfs->vfs_dev;
4278 if (error)
4279 cmn_err(CE_CONT, "Cannot mount root on %s fstype %s\n",
4280 rootfs.bo_name, fstyp);
4281 else
4282 cmn_err(CE_CONT, "?root on %s fstype %s\n",
4283 rootfs.bo_name, fstyp);
4284 return (error);
4288 * XXX this is called by nfs only and should probably be removed
4289 * If booted with ASKNAME, prompt on the console for a filesystem
4290 * name and return it.
4292 void
4293 getfsname(char *askfor, char *name, size_t namelen)
4295 if (boothowto & RB_ASKNAME) {
4296 printf("%s name: ", askfor);
4297 console_gets(name, namelen);
4302 * Init the root filesystem type (rootfs.bo_fstype) from the "fstype"
4303 * property.
4305 * Filesystem types starting with the prefix "nfs" are diskless clients;
4306 * init the root filename name (rootfs.bo_name), too.
4308 * If we are booting via NFS we currently have these options:
4309 * nfs - dynamically choose NFS V2, V3, or V4 (default)
4310 * nfs2 - force NFS V2
4311 * nfs3 - force NFS V3
4312 * nfs4 - force NFS V4
4313 * Because we need to maintain backward compatibility with the naming
4314 * convention that the NFS V2 filesystem name is "nfs" (see vfs_conf.c)
4315 * we need to map "nfs" => "nfsdyn" and "nfs2" => "nfs". The dynamic
4316 * nfs module will map the type back to either "nfs", "nfs3", or "nfs4".
4317 * This is only for root filesystems, all other uses will expect
4318 * that "nfs" == NFS V2.
4320 static void
4321 getrootfs(char **fstypp, char **fsmodp)
4323 char *propstr = NULL;
4326 * Check fstype property; for diskless it should be one of "nfs",
4327 * "nfs2", "nfs3" or "nfs4".
4329 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4330 DDI_PROP_DONTPASS, "fstype", &propstr)
4331 == DDI_SUCCESS) {
4332 (void) strncpy(rootfs.bo_fstype, propstr, BO_MAXFSNAME);
4333 ddi_prop_free(propstr);
4336 * if the boot property 'fstype' is not set, but 'zfs-bootfs' is set,
4337 * assume the type of this root filesystem is 'zfs'.
4339 } else if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4340 DDI_PROP_DONTPASS, "zfs-bootfs", &propstr)
4341 == DDI_SUCCESS) {
4342 (void) strncpy(rootfs.bo_fstype, "zfs", BO_MAXFSNAME);
4343 ddi_prop_free(propstr);
4346 if (strncmp(rootfs.bo_fstype, "nfs", 3) != 0) {
4347 *fstypp = *fsmodp = rootfs.bo_fstype;
4348 return;
4351 ++netboot;
4353 if (strcmp(rootfs.bo_fstype, "nfs2") == 0)
4354 (void) strcpy(rootfs.bo_fstype, "nfs");
4355 else if (strcmp(rootfs.bo_fstype, "nfs") == 0)
4356 (void) strcpy(rootfs.bo_fstype, "nfsdyn");
4359 * check if path to network interface is specified in bootpath
4360 * or by a hypervisor domain configuration file.
4361 * XXPV - enable strlumb_get_netdev_path()
4363 if (ddi_prop_exists(DDI_DEV_T_ANY, ddi_root_node(), DDI_PROP_DONTPASS,
4364 "xpv-nfsroot")) {
4365 (void) strcpy(rootfs.bo_name, "/xpvd/xnf@0");
4366 } else if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4367 DDI_PROP_DONTPASS, "bootpath", &propstr)
4368 == DDI_SUCCESS) {
4369 (void) strncpy(rootfs.bo_name, propstr, BO_MAXOBJNAME);
4370 ddi_prop_free(propstr);
4371 } else {
4372 rootfs.bo_name[0] = '\0';
4374 *fstypp = rootfs.bo_fstype;
4375 *fsmodp = "nfs";
4379 * VFS feature routines
4382 #define VFTINDEX(feature) (((feature) >> 32) & 0xFFFFFFFF)
4383 #define VFTBITS(feature) ((feature) & 0xFFFFFFFFLL)
4385 /* Register a feature in the vfs */
4386 void
4387 vfs_set_feature(vfs_t *vfsp, vfs_feature_t feature)
4389 /* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4390 if (vfsp->vfs_implp == NULL)
4391 return;
4393 vfsp->vfs_featureset[VFTINDEX(feature)] |= VFTBITS(feature);
4396 void
4397 vfs_clear_feature(vfs_t *vfsp, vfs_feature_t feature)
4399 /* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4400 if (vfsp->vfs_implp == NULL)
4401 return;
4402 vfsp->vfs_featureset[VFTINDEX(feature)] &= VFTBITS(~feature);
4406 * Query a vfs for a feature.
4407 * Returns 1 if feature is present, 0 if not
4410 vfs_has_feature(vfs_t *vfsp, vfs_feature_t feature)
4412 int ret = 0;
4414 /* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4415 if (vfsp->vfs_implp == NULL)
4416 return (ret);
4418 if (vfsp->vfs_featureset[VFTINDEX(feature)] & VFTBITS(feature))
4419 ret = 1;
4421 return (ret);
4425 * Propagate feature set from one vfs to another
4427 void
4428 vfs_propagate_features(vfs_t *from, vfs_t *to)
4430 int i;
4432 if (to->vfs_implp == NULL || from->vfs_implp == NULL)
4433 return;
4435 for (i = 1; i <= to->vfs_featureset[0]; i++) {
4436 to->vfs_featureset[i] = from->vfs_featureset[i];
4440 #define LOFINODE_PATH "/dev/lofi/%d"
4443 * Return the vnode for the lofi node if there's a lofi mount in place.
4444 * Returns -1 when there's no lofi node, 0 on success, and > 0 on
4445 * failure.
4448 vfs_get_lofi(vfs_t *vfsp, vnode_t **vpp)
4450 char *path = NULL;
4451 int strsize;
4452 int err;
4454 if (vfsp->vfs_lofi_id == 0) {
4455 *vpp = NULL;
4456 return (-1);
4459 strsize = snprintf(NULL, 0, LOFINODE_PATH, vfsp->vfs_lofi_id);
4460 path = kmem_alloc(strsize + 1, KM_SLEEP);
4461 (void) snprintf(path, strsize + 1, LOFINODE_PATH, vfsp->vfs_lofi_id);
4464 * We may be inside a zone, so we need to use the /dev path, but
4465 * it's created asynchronously, so we wait here.
4467 for (;;) {
4468 err = lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, vpp);
4470 if (err != ENOENT)
4471 break;
4473 if ((err = delay_sig(hz / 8)) == EINTR)
4474 break;
4477 if (err)
4478 *vpp = NULL;
4480 kmem_free(path, strsize + 1);
4481 return (err);