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]
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 (c) 2016 by Delphix. All rights reserved.
27 * Copyright 2016 Nexenta Systems, Inc.
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
39 * University Acknowledgment- Portions of this document are derived from
40 * software developed by the University of California, Berkeley, and its
44 #include <sys/types.h>
45 #include <sys/t_lock.h>
46 #include <sys/param.h>
47 #include <sys/errno.h>
49 #include <sys/fstyp.h>
51 #include <sys/systm.h>
53 #include <sys/mount.h>
55 #include <sys/vfs_opreg.h>
57 #include <sys/mntent.h>
59 #include <sys/statvfs.h>
60 #include <sys/statfs.h>
62 #include <sys/vnode.h>
63 #include <sys/rwstlock.h>
67 #include <sys/atomic.h>
68 #include <sys/cmn_err.h>
71 #include <sys/debug.h>
72 #include <sys/vnode.h>
73 #include <sys/modctl.h>
75 #include <sys/pathname.h>
76 #include <sys/bootconf.h>
77 #include <sys/dumphdr.h>
78 #include <sys/dc_ki.h>
80 #include <sys/sunddi.h>
81 #include <sys/sysmacros.h>
83 #include <sys/policy.h>
85 #include <sys/objfs.h>
86 #include <sys/console.h>
87 #include <sys/reboot.h>
92 #include <sys/bootprops.h>
96 #include <fs/fs_subr.h>
97 /* Private interfaces to create vopstats-related data structures */
98 extern void initialize_vopstats(vopstats_t
*);
99 extern vopstats_t
*get_fstype_vopstats(struct vfs
*, struct vfssw
*);
100 extern vsk_anchor_t
*get_vskstat_anchor(struct vfs
*);
102 static void vfs_clearmntopt_nolock(mntopts_t
*, const char *, int);
103 static void vfs_setmntopt_nolock(mntopts_t
*, const char *,
104 const char *, int, int);
105 static int vfs_optionisset_nolock(const mntopts_t
*, const char *, char **);
106 static void vfs_freemnttab(struct vfs
*);
107 static void vfs_freeopt(mntopt_t
*);
108 static void vfs_swapopttbl_nolock(mntopts_t
*, mntopts_t
*);
109 static void vfs_swapopttbl(mntopts_t
*, mntopts_t
*);
110 static void vfs_copyopttbl_extend(const mntopts_t
*, mntopts_t
*, int);
111 static void vfs_createopttbl_extend(mntopts_t
*, const char *,
113 static char **vfs_copycancelopt_extend(char **const, int);
114 static void vfs_freecancelopt(char **);
115 static void getrootfs(char **, char **);
116 static int getmacpath(dev_info_t
*, void *);
117 static void vfs_mnttabvp_setup(void);
120 struct ipmnt
*mip_next
;
122 struct vfs
*mip_vfsp
;
125 static kmutex_t vfs_miplist_mutex
;
126 static struct ipmnt
*vfs_miplist
= NULL
;
127 static struct ipmnt
*vfs_miplist_end
= NULL
;
129 static kmem_cache_t
*vfs_cache
; /* Pointer to VFS kmem cache */
134 vnode_t
*rootdir
; /* pointer to root inode vnode. */
135 vnode_t
*devicesdir
; /* pointer to inode of devices root */
136 vnode_t
*devdir
; /* pointer to inode of dev root */
138 char *server_rootpath
; /* root path for diskless clients */
139 char *server_hostname
; /* hostname of diskless server */
141 static struct vfs root
;
142 static struct vfs devices
;
143 static struct vfs dev
;
144 struct vfs
*rootvfs
= &root
; /* pointer to root vfs; head of VFS list. */
145 rvfs_t
*rvfs_list
; /* array of vfs ptrs for vfs hash list */
146 int vfshsz
= 512; /* # of heads/locks in vfs hash arrays */
147 /* must be power of 2! */
148 timespec_t vfs_mnttab_ctime
; /* mnttab created time */
149 timespec_t vfs_mnttab_mtime
; /* mnttab last modified time */
150 char *vfs_dummyfstype
= "\0";
151 struct pollhead vfs_pollhd
; /* for mnttab pollers */
152 struct vnode
*vfs_mntdummyvp
; /* to fake mnttab read/write for file events */
153 int mntfstype
; /* will be set once mnt fs is mounted */
156 * Table for generic options recognized in the VFS layer and acted
157 * on at this level before parsing file system specific options.
158 * The nosuid option is stronger than any of the devices and setuid
159 * options, so those are canceled when nosuid is seen.
161 * All options which are added here need to be added to the
162 * list of standard options in usr/src/cmd/fs.d/fslib.c as well.
165 * VFS Mount options table
167 static char *ro_cancel
[] = { MNTOPT_RW
, NULL
};
168 static char *rw_cancel
[] = { MNTOPT_RO
, NULL
};
169 static char *suid_cancel
[] = { MNTOPT_NOSUID
, NULL
};
170 static char *nosuid_cancel
[] = { MNTOPT_SUID
, MNTOPT_DEVICES
, MNTOPT_NODEVICES
,
171 MNTOPT_NOSETUID
, MNTOPT_SETUID
, NULL
};
172 static char *devices_cancel
[] = { MNTOPT_NODEVICES
, NULL
};
173 static char *nodevices_cancel
[] = { MNTOPT_DEVICES
, NULL
};
174 static char *setuid_cancel
[] = { MNTOPT_NOSETUID
, NULL
};
175 static char *nosetuid_cancel
[] = { MNTOPT_SETUID
, NULL
};
176 static char *nbmand_cancel
[] = { MNTOPT_NONBMAND
, NULL
};
177 static char *nonbmand_cancel
[] = { MNTOPT_NBMAND
, NULL
};
178 static char *exec_cancel
[] = { MNTOPT_NOEXEC
, NULL
};
179 static char *noexec_cancel
[] = { MNTOPT_EXEC
, NULL
};
181 static const mntopt_t mntopts
[] = {
183 * option name cancel options default arg flags
185 { MNTOPT_REMOUNT
, NULL
, NULL
,
186 MO_NODISPLAY
, (void *)0 },
187 { MNTOPT_RO
, ro_cancel
, NULL
, 0,
189 { MNTOPT_RW
, rw_cancel
, NULL
, 0,
191 { MNTOPT_SUID
, suid_cancel
, NULL
, 0,
193 { MNTOPT_NOSUID
, nosuid_cancel
, NULL
, 0,
195 { MNTOPT_DEVICES
, devices_cancel
, NULL
, 0,
197 { MNTOPT_NODEVICES
, nodevices_cancel
, NULL
, 0,
199 { MNTOPT_SETUID
, setuid_cancel
, NULL
, 0,
201 { MNTOPT_NOSETUID
, nosetuid_cancel
, NULL
, 0,
203 { MNTOPT_NBMAND
, nbmand_cancel
, NULL
, 0,
205 { MNTOPT_NONBMAND
, nonbmand_cancel
, NULL
, 0,
207 { MNTOPT_EXEC
, exec_cancel
, NULL
, 0,
209 { MNTOPT_NOEXEC
, noexec_cancel
, NULL
, 0,
213 const mntopts_t vfs_mntopts
= {
214 sizeof (mntopts
) / sizeof (mntopt_t
),
215 (mntopt_t
*)&mntopts
[0]
219 * File system operation dispatch functions.
223 fsop_mount(vfs_t
*vfsp
, vnode_t
*mvp
, struct mounta
*uap
, cred_t
*cr
)
225 return (*(vfsp
)->vfs_op
->vfs_mount
)(vfsp
, mvp
, uap
, cr
);
229 fsop_unmount(vfs_t
*vfsp
, int flag
, cred_t
*cr
)
231 return (*(vfsp
)->vfs_op
->vfs_unmount
)(vfsp
, flag
, cr
);
235 fsop_root(vfs_t
*vfsp
, vnode_t
**vpp
)
238 int ret
= (*(vfsp
)->vfs_op
->vfs_root
)(vfsp
, vpp
);
240 * Make sure this root has a path. With lofs, it is possible to have
243 if (ret
== 0 && vfsp
->vfs_mntpt
!= NULL
&&
244 (*vpp
)->v_path
== vn_vpath_empty
) {
247 mntpt
= vfs_getmntpoint(vfsp
);
248 path
= refstr_value(mntpt
);
249 vn_setpath_str(*vpp
, path
, strlen(path
));
257 fsop_statfs(vfs_t
*vfsp
, statvfs64_t
*sp
)
259 return (*(vfsp
)->vfs_op
->vfs_statvfs
)(vfsp
, sp
);
263 fsop_sync(vfs_t
*vfsp
, short flag
, cred_t
*cr
)
265 return (*(vfsp
)->vfs_op
->vfs_sync
)(vfsp
, flag
, cr
);
269 fsop_vget(vfs_t
*vfsp
, vnode_t
**vpp
, fid_t
*fidp
)
272 * In order to handle system attribute fids in a manner
273 * transparent to the underlying fs, we embed the fid for
274 * the sysattr parent object in the sysattr fid and tack on
275 * some extra bytes that only the sysattr layer knows about.
277 * This guarantees that sysattr fids are larger than other fids
278 * for this vfs. If the vfs supports the sysattr view interface
279 * (as indicated by VFSFT_SYSATTR_VIEWS), we cannot have a size
280 * collision with XATTR_FIDSZ.
282 if (vfs_has_feature(vfsp
, VFSFT_SYSATTR_VIEWS
) &&
283 fidp
->fid_len
== XATTR_FIDSZ
)
284 return (xattr_dir_vget(vfsp
, vpp
, fidp
));
286 return (*(vfsp
)->vfs_op
->vfs_vget
)(vfsp
, vpp
, fidp
);
290 fsop_mountroot(vfs_t
*vfsp
, enum whymountroot reason
)
292 return (*(vfsp
)->vfs_op
->vfs_mountroot
)(vfsp
, reason
);
296 fsop_freefs(vfs_t
*vfsp
)
298 (*(vfsp
)->vfs_op
->vfs_freevfs
)(vfsp
);
302 fsop_vnstate(vfs_t
*vfsp
, vnode_t
*vp
, vntrans_t nstate
)
304 return ((*(vfsp
)->vfs_op
->vfs_vnstate
)(vfsp
, vp
, nstate
));
308 fsop_sync_by_kind(int fstype
, short flag
, cred_t
*cr
)
310 ASSERT((fstype
>= 0) && (fstype
< nfstype
));
312 if (ALLOCATED_VFSSW(&vfssw
[fstype
]) && VFS_INSTALLED(&vfssw
[fstype
]))
313 return (*vfssw
[fstype
].vsw_vfsops
.vfs_sync
) (NULL
, flag
, cr
);
319 * File system initialization. vfs_setfsops() must be called from a file
320 * system's init routine.
324 fs_copyfsops(const fs_operation_def_t
*template, vfsops_t
*actual
,
327 static const fs_operation_trans_def_t vfs_ops_table
[] = {
328 VFSNAME_MOUNT
, offsetof(vfsops_t
, vfs_mount
),
331 VFSNAME_UNMOUNT
, offsetof(vfsops_t
, vfs_unmount
),
334 VFSNAME_ROOT
, offsetof(vfsops_t
, vfs_root
),
337 VFSNAME_STATVFS
, offsetof(vfsops_t
, vfs_statvfs
),
340 VFSNAME_SYNC
, offsetof(vfsops_t
, vfs_sync
),
341 (fs_generic_func_p
) fs_sync
,
342 (fs_generic_func_p
) fs_sync
, /* No errors allowed */
344 VFSNAME_VGET
, offsetof(vfsops_t
, vfs_vget
),
347 VFSNAME_MOUNTROOT
, offsetof(vfsops_t
, vfs_mountroot
),
350 VFSNAME_FREEVFS
, offsetof(vfsops_t
, vfs_freevfs
),
351 (fs_generic_func_p
)fs_freevfs
,
352 (fs_generic_func_p
)fs_freevfs
, /* Shouldn't fail */
354 VFSNAME_VNSTATE
, offsetof(vfsops_t
, vfs_vnstate
),
355 (fs_generic_func_p
)fs_nosys
,
356 (fs_generic_func_p
)fs_nosys
,
361 return (fs_build_vector(actual
, unused_ops
, vfs_ops_table
, template));
367 if (strcmp(rootfs
.bo_fstype
, MNTTYPE_ZFS
) == 0)
372 vfs_setfsops(int fstype
, const fs_operation_def_t
*template, vfsops_t
**actual
)
378 * Verify that fstype refers to a valid fs. Note that
379 * 0 is valid since it's used to set "stray" ops.
381 if ((fstype
< 0) || (fstype
>= nfstype
))
384 if (!ALLOCATED_VFSSW(&vfssw
[fstype
]))
387 /* Set up the operations vector. */
389 error
= fs_copyfsops(template, &vfssw
[fstype
].vsw_vfsops
, &unused_ops
);
394 vfssw
[fstype
].vsw_flag
|= VSW_INSTALLED
;
397 *actual
= &vfssw
[fstype
].vsw_vfsops
;
401 cmn_err(CE_WARN
, "vfs_setfsops: %s: %d operations supplied "
402 "but not used", vfssw
[fstype
].vsw_name
, unused_ops
);
409 vfs_makefsops(const fs_operation_def_t
*template, vfsops_t
**actual
)
414 *actual
= (vfsops_t
*)kmem_alloc(sizeof (vfsops_t
), KM_SLEEP
);
416 error
= fs_copyfsops(template, *actual
, &unused_ops
);
418 kmem_free(*actual
, sizeof (vfsops_t
));
427 * Free a vfsops structure created as a result of vfs_makefsops().
428 * NOTE: For a vfsops structure initialized by vfs_setfsops(), use
429 * vfs_freevfsops_by_type().
432 vfs_freevfsops(vfsops_t
*vfsops
)
434 kmem_free(vfsops
, sizeof (vfsops_t
));
438 * Since the vfsops structure is part of the vfssw table and wasn't
439 * really allocated, we're not really freeing anything. We keep
440 * the name for consistency with vfs_freevfsops(). We do, however,
441 * need to take care of a little bookkeeping.
442 * NOTE: For a vfsops structure created by vfs_setfsops(), use
443 * vfs_freevfsops_by_type().
446 vfs_freevfsops_by_type(int fstype
)
449 /* Verify that fstype refers to a loaded fs (and not fsid 0). */
450 if ((fstype
<= 0) || (fstype
>= nfstype
))
454 if ((vfssw
[fstype
].vsw_flag
& VSW_INSTALLED
) == 0) {
459 vfssw
[fstype
].vsw_flag
&= ~VSW_INSTALLED
;
465 /* Support routines used to reference vfs_op */
467 /* Set the operations vector for a vfs */
469 vfs_setops(vfs_t
*vfsp
, vfsops_t
*vfsops
)
473 ASSERT(vfsp
!= NULL
);
474 ASSERT(vfsops
!= NULL
);
478 if (vfsp
->vfs_femhead
== NULL
&&
479 atomic_cas_ptr(&vfsp
->vfs_op
, op
, vfsops
) == op
) {
482 fsem_setvfsops(vfsp
, vfsops
);
485 /* Retrieve the operations vector for a vfs */
487 vfs_getops(vfs_t
*vfsp
)
491 ASSERT(vfsp
!= NULL
);
495 if (vfsp
->vfs_femhead
== NULL
&& op
== vfsp
->vfs_op
) {
498 return (fsem_getvfsops(vfsp
));
503 * Returns non-zero (1) if the vfsops matches that of the vfs.
504 * Returns zero (0) if not.
507 vfs_matchops(vfs_t
*vfsp
, vfsops_t
*vfsops
)
509 return (vfs_getops(vfsp
) == vfsops
);
513 * Returns non-zero (1) if the file system has installed a non-default,
514 * non-error vfs_sync routine. Returns zero (0) otherwise.
517 vfs_can_sync(vfs_t
*vfsp
)
519 /* vfs_sync() routine is not the default/error function */
520 return (vfs_getops(vfsp
)->vfs_sync
!= fs_sync
);
524 * Initialize a vfs structure.
527 vfs_init(vfs_t
*vfsp
, vfsops_t
*op
, void *data
)
529 /* Other initialization has been moved to vfs_alloc() */
531 vfsp
->vfs_next
= vfsp
;
532 vfsp
->vfs_prev
= vfsp
;
533 vfsp
->vfs_zone_next
= vfsp
;
534 vfsp
->vfs_zone_prev
= vfsp
;
535 vfsp
->vfs_lofi_id
= 0;
536 sema_init(&vfsp
->vfs_reflock
, 1, NULL
, SEMA_DEFAULT
, NULL
);
538 vfsp
->vfs_data
= (data
);
539 vfs_setops((vfsp
), (op
));
543 * Allocate and initialize the vfs implementation private data
544 * structure, vfs_impl_t.
547 vfsimpl_setup(vfs_t
*vfsp
)
551 if (vfsp
->vfs_implp
!= NULL
) {
555 vfsp
->vfs_implp
= kmem_alloc(sizeof (vfs_impl_t
), KM_SLEEP
);
556 /* Note that these are #define'd in vfs.h */
557 vfsp
->vfs_vskap
= NULL
;
558 vfsp
->vfs_fstypevsp
= NULL
;
560 /* Set size of counted array, then zero the array */
561 vfsp
->vfs_featureset
[0] = VFS_FEATURE_MAXSZ
- 1;
562 for (i
= 1; i
< VFS_FEATURE_MAXSZ
; i
++) {
563 vfsp
->vfs_featureset
[i
] = 0;
568 * Release the vfs_impl_t structure, if it exists. Some unbundled
569 * filesystems may not use the newer version of vfs and thus
570 * would not contain this implementation private data structure.
573 vfsimpl_teardown(vfs_t
*vfsp
)
575 vfs_impl_t
*vip
= vfsp
->vfs_implp
;
580 kmem_free(vfsp
->vfs_implp
, sizeof (vfs_impl_t
));
581 vfsp
->vfs_implp
= NULL
;
585 * VFS system calls: mount, umount, syssync, statfs, fstatfs, statvfs,
586 * fstatvfs, and sysfs moved to common/syscall.
590 * Update every mounted file system. We call the vfs_sync operation of
591 * each file system type, passing it a NULL vfsp to indicate that all
592 * mounted file systems of that type should be updated.
599 for (vswp
= &vfssw
[1]; vswp
< &vfssw
[nfstype
]; vswp
++) {
600 if (ALLOCATED_VFSSW(vswp
) && VFS_INSTALLED(vswp
)) {
603 (void) (*vswp
->vsw_vfsops
.vfs_sync
)(NULL
, flag
,
605 vfs_unrefvfssw(vswp
);
622 krwlock_t vfssw_lock
; /* lock accesses to vfssw */
625 * Lock for accessing the vfs linked list. Initialized in vfs_mountroot(),
626 * but otherwise should be accessed only via vfs_list_lock() and
627 * vfs_list_unlock(). Also used to protect the timestamp for mods to the list.
629 static krwlock_t vfslist
;
632 * Mount devfs on /devices. This is done right after root is mounted
633 * to provide device access support for the system
636 vfs_mountdevices(void)
640 struct mounta mounta
= { /* fake mounta for devfs_mount() */
652 * _init devfs module to fill in the vfssw
654 if (modload("fs", "devfs") == -1)
655 panic("Cannot _init devfs module");
661 vsw
= vfs_getvfsswbyname("devfs");
662 VFS_INIT(&devices
, &vsw
->vsw_vfsops
, NULL
);
668 if (lookupname("/devices", UIO_SYSSPACE
, FOLLOW
, NULLVPP
, &mvp
))
669 panic("Cannot find /devices");
672 * Perform the mount of /devices
674 if (VFS_MOUNT(&devices
, mvp
, &mounta
, CRED()))
675 panic("Cannot mount /devices");
680 * Set appropriate members and add to vfs list for mnttab display
682 vfs_setresource(&devices
, "/devices", 0);
683 vfs_setmntpoint(&devices
, "/devices", 0);
686 * Hold the root of /devices so it won't go away
688 if (VFS_ROOT(&devices
, &devicesdir
))
689 panic("vfs_mountdevices: not devices root");
691 if (vfs_lock(&devices
) != 0) {
693 cmn_err(CE_NOTE
, "Cannot acquire vfs_lock of /devices");
697 if (vn_vfswlock(mvp
) != 0) {
698 vfs_unlock(&devices
);
700 cmn_err(CE_NOTE
, "Cannot acquire vfswlock of /devices");
704 vfs_add(mvp
, &devices
, 0);
706 vfs_unlock(&devices
);
711 * mount the first instance of /dev to root and remain mounted
718 struct mounta mounta
= { /* fake mounta for sdev_mount() */
721 MS_SYSSPACE
| MS_OVERLAY
,
730 * _init dev module to fill in the vfssw
732 if (modload("fs", "dev") == -1)
733 cmn_err(CE_PANIC
, "Cannot _init dev module\n");
739 vsw
= vfs_getvfsswbyname("dev");
740 VFS_INIT(&dev
, &vsw
->vsw_vfsops
, NULL
);
746 if (lookupname("/dev", UIO_SYSSPACE
, FOLLOW
, NULLVPP
, &mvp
))
747 cmn_err(CE_PANIC
, "Cannot find /dev\n");
750 * Perform the mount of /dev
752 if (VFS_MOUNT(&dev
, mvp
, &mounta
, CRED()))
753 cmn_err(CE_PANIC
, "Cannot mount /dev 1\n");
758 * Set appropriate members and add to vfs list for mnttab display
760 vfs_setresource(&dev
, "/dev", 0);
761 vfs_setmntpoint(&dev
, "/dev", 0);
764 * Hold the root of /dev so it won't go away
766 if (VFS_ROOT(&dev
, &devdir
))
767 cmn_err(CE_PANIC
, "vfs_mountdev1: not dev root");
769 if (vfs_lock(&dev
) != 0) {
771 cmn_err(CE_NOTE
, "Cannot acquire vfs_lock of /dev");
775 if (vn_vfswlock(mvp
) != 0) {
778 cmn_err(CE_NOTE
, "Cannot acquire vfswlock of /dev");
782 vfs_add(mvp
, &dev
, 0);
789 * Mount required filesystem. This is done right after root is mounted.
792 vfs_mountfs(char *module
, char *spec
, char *path
)
795 struct mounta mounta
;
798 bzero(&mounta
, sizeof (mounta
));
799 mounta
.flags
= MS_SYSSPACE
| MS_DATA
;
800 mounta
.fstype
= module
;
803 if (lookupname(path
, UIO_SYSSPACE
, FOLLOW
, NULLVPP
, &mvp
)) {
804 cmn_err(CE_WARN
, "Cannot find %s", path
);
807 if (domount(NULL
, &mounta
, mvp
, CRED(), &vfsp
))
808 cmn_err(CE_WARN
, "Cannot mount %s", path
);
815 * vfs_mountroot is called by main() to mount the root filesystem.
820 struct vnode
*rvp
= NULL
;
826 rw_init(&vfssw_lock
, NULL
, RW_DEFAULT
, NULL
);
827 rw_init(&vfslist
, NULL
, RW_DEFAULT
, NULL
);
830 * Alloc the vfs hash bucket array and locks
832 rvfs_list
= kmem_zalloc(vfshsz
* sizeof (rvfs_t
), KM_SLEEP
);
835 * Call machine-dependent routine "rootconf" to choose a root
839 panic("vfs_mountroot: cannot mount root");
841 * Get vnode for '/'. Set up rootdir, u.u_rdir and u.u_cdir
842 * to point to it. These are used by lookuppn() so that it
843 * knows where to start from ('/' or '.').
845 vfs_setmntpoint(rootvfs
, "/", 0);
846 if (VFS_ROOT(rootvfs
, &rootdir
))
847 panic("vfs_mountroot: no root vnode");
850 * At this point, the process tree consists of p0 and possibly some
851 * direct children of p0. (i.e. there are no grandchildren)
853 * Walk through them all, setting their current directory.
855 mutex_enter(&pidlock
);
856 for (p
= practive
; p
!= NULL
; p
= p
->p_next
) {
857 ASSERT(p
== &p0
|| p
->p_parent
== &p0
);
859 PTOU(p
)->u_cdir
= rootdir
;
860 VN_HOLD(PTOU(p
)->u_cdir
);
861 PTOU(p
)->u_rdir
= NULL
;
863 mutex_exit(&pidlock
);
866 * Setup the global zone's rootvp, now that it exists.
868 global_zone
->zone_rootvp
= rootdir
;
869 VN_HOLD(global_zone
->zone_rootvp
);
872 * Notify the module code that it can begin using the
873 * root filesystem instead of the boot program's services.
878 * Special handling for a ZFS root file system.
883 * Set up mnttab information for root
885 vfs_setresource(rootvfs
, rootfs
.bo_name
, 0);
888 * Notify cluster software that the root filesystem is available.
892 /* Now that we're all done with the root FS, set up its vopstats */
893 if ((vswp
= vfs_getvfsswbyvfsops(vfs_getops(rootvfs
))) != NULL
) {
894 /* Set flag for statistics collection */
895 if (vswp
->vsw_flag
& VSW_STATS
) {
896 initialize_vopstats(&rootvfs
->vfs_vopstats
);
897 rootvfs
->vfs_flag
|= VFS_STATS
;
898 rootvfs
->vfs_fstypevsp
=
899 get_fstype_vopstats(rootvfs
, vswp
);
900 rootvfs
->vfs_vskap
= get_vskstat_anchor(rootvfs
);
902 vfs_unrefvfssw(vswp
);
906 * Mount /devices, /dev instance 1, /system/contract, /etc/mnttab,
907 * /etc/svc/volatile, /etc/dfs/sharetab, /system/object, and /proc.
912 vfs_mountfs("ctfs", "ctfs", CTFS_ROOT
);
913 vfs_mountfs("proc", "/proc", "/proc");
914 vfs_mountfs("mntfs", "/etc/mnttab", "/etc/mnttab");
915 vfs_mountfs("tmpfs", "/etc/svc/volatile", "/etc/svc/volatile");
916 vfs_mountfs("objfs", "objfs", OBJFS_ROOT
);
917 vfs_mountfs("bootfs", "bootfs", "/system/boot");
919 if (getzoneid() == GLOBAL_ZONEID
) {
920 vfs_mountfs("sharefs", "sharefs", "/etc/dfs/sharetab");
923 if (strcmp(rootfs
.bo_fstype
, "zfs") != 0) {
925 * Look up the root device via devfs so that a dv_node is
926 * created for it. The vnode is never VN_RELE()ed.
927 * We allocate more than MAXPATHLEN so that the
928 * buffer passed to i_ddi_prompath_to_devfspath() is
929 * exactly MAXPATHLEN (the function expects a buffer
932 plen
= strlen("/devices");
933 path
= kmem_alloc(plen
+ MAXPATHLEN
, KM_SLEEP
);
934 (void) strcpy(path
, "/devices");
936 if (i_ddi_prompath_to_devfspath(rootfs
.bo_name
, path
+ plen
)
938 lookupname(path
, UIO_SYSSPACE
, FOLLOW
, NULLVPP
, &rvp
)) {
940 /* NUL terminate in case "path" has garbage */
941 path
[plen
+ MAXPATHLEN
- 1] = '\0';
943 cmn_err(CE_WARN
, "!Cannot lookup root device: %s",
947 kmem_free(path
, plen
+ MAXPATHLEN
);
950 vfs_mnttabvp_setup();
954 * Check to see if our "block device" is actually a file. If so,
955 * automatically add a lofi device, and keep track of this fact.
958 lofi_add(const char *fsname
, struct vfs
*vfsp
,
959 mntopts_t
*mntopts
, struct mounta
*uap
)
961 int fromspace
= (uap
->flags
& MS_SYSSPACE
) ?
962 UIO_SYSSPACE
: UIO_USERSPACE
;
963 struct lofi_ioctl
*li
= NULL
;
964 struct vnode
*vp
= NULL
;
965 struct pathname pn
= { NULL
};
967 ldi_handle_t ldi_hdl
;
972 if ((vfssw
= vfs_getvfssw(fsname
)) == NULL
)
975 if (!(vfssw
->vsw_flag
& VSW_CANLOFI
)) {
976 vfs_unrefvfssw(vfssw
);
980 vfs_unrefvfssw(vfssw
);
983 if (pn_get(uap
->spec
, fromspace
, &pn
) != 0)
986 if (lookupname(uap
->spec
, fromspace
, FOLLOW
, NULL
, &vp
) != 0)
989 if (vp
->v_type
!= VREG
)
992 /* OK, this is a lofi mount. */
994 if ((uap
->flags
& (MS_REMOUNT
|MS_GLOBAL
)) ||
995 vfs_optionisset_nolock(mntopts
, MNTOPT_SUID
, NULL
) ||
996 vfs_optionisset_nolock(mntopts
, MNTOPT_SETUID
, NULL
) ||
997 vfs_optionisset_nolock(mntopts
, MNTOPT_DEVICES
, NULL
)) {
1002 ldi_id
= ldi_ident_from_anon();
1003 li
= kmem_zalloc(sizeof (*li
), KM_SLEEP
);
1004 (void) strlcpy(li
->li_filename
, pn
.pn_path
, MAXPATHLEN
);
1006 err
= ldi_open_by_name("/dev/lofictl", FREAD
| FWRITE
, kcred
,
1012 err
= ldi_ioctl(ldi_hdl
, LOFI_MAP_FILE
, (intptr_t)li
,
1013 FREAD
| FWRITE
| FKIOCTL
, kcred
, &id
);
1015 (void) ldi_close(ldi_hdl
, FREAD
| FWRITE
, kcred
);
1018 vfsp
->vfs_lofi_id
= id
;
1021 ldi_ident_release(ldi_id
);
1024 kmem_free(li
, sizeof (*li
));
1032 lofi_remove(struct vfs
*vfsp
)
1034 struct lofi_ioctl
*li
= NULL
;
1036 ldi_handle_t ldi_hdl
;
1039 if (vfsp
->vfs_lofi_id
== 0)
1042 ldi_id
= ldi_ident_from_anon();
1044 li
= kmem_zalloc(sizeof (*li
), KM_SLEEP
);
1045 li
->li_id
= vfsp
->vfs_lofi_id
;
1046 li
->li_cleanup
= B_TRUE
;
1048 err
= ldi_open_by_name("/dev/lofictl", FREAD
| FWRITE
, kcred
,
1054 err
= ldi_ioctl(ldi_hdl
, LOFI_UNMAP_FILE_MINOR
, (intptr_t)li
,
1055 FREAD
| FWRITE
| FKIOCTL
, kcred
, NULL
);
1057 (void) ldi_close(ldi_hdl
, FREAD
| FWRITE
, kcred
);
1060 vfsp
->vfs_lofi_id
= 0;
1063 ldi_ident_release(ldi_id
);
1065 kmem_free(li
, sizeof (*li
));
1069 * Common mount code. Called from the system call entry point, from autofs,
1070 * nfsv4 trigger mounts, and from pxfs.
1072 * Takes the effective file system type, mount arguments, the mount point
1073 * vnode, flags specifying whether the mount is a remount and whether it
1074 * should be entered into the vfs list, and credentials. Fills in its vfspp
1075 * parameter with the mounted file system instance's vfs.
1077 * Note that the effective file system type is specified as a string. It may
1078 * be null, in which case it's determined from the mount arguments, and may
1079 * differ from the type specified in the mount arguments; this is a hook to
1080 * allow interposition when instantiating file system instances.
1082 * The caller is responsible for releasing its own hold on the mount point
1083 * vp (this routine does its own hold when necessary).
1084 * Also note that for remounts, the mount point vp should be the vnode for
1085 * the root of the file system rather than the vnode that the file system
1086 * is mounted on top of.
1089 domount(char *fsname
, struct mounta
*uap
, vnode_t
*vp
, struct cred
*credp
,
1097 mntopts_t mnt_mntopts
;
1099 int copyout_error
= 0;
1101 char *opts
= uap
->optptr
;
1102 char *inargs
= opts
;
1103 int optlen
= uap
->optlen
;
1109 int splice
= ((uap
->flags
& MS_NOSPLICE
) == 0);
1110 int fromspace
= (uap
->flags
& MS_SYSSPACE
) ?
1111 UIO_SYSSPACE
: UIO_USERSPACE
;
1112 char *resource
= NULL
, *mountpt
= NULL
;
1113 refstr_t
*oldresource
, *oldmntpt
;
1114 struct pathname pn
, rpn
;
1115 vsk_anchor_t
*vskap
;
1116 char fstname
[FSTYPSZ
];
1120 * The v_flag value for the mount point vp is permanently set
1121 * to VVFSLOCK so that no one bypasses the vn_vfs*locks routine
1122 * for mount point locking.
1124 mutex_enter(&vp
->v_lock
);
1125 vp
->v_flag
|= VVFSLOCK
;
1126 mutex_exit(&vp
->v_lock
);
1128 mnt_mntopts
.mo_count
= 0;
1130 * Find the ops vector to use to invoke the file system-specific mount
1131 * method. If the fsname argument is non-NULL, use it directly.
1132 * Otherwise, dig the file system type information out of the mount
1135 * A side effect is to hold the vfssw entry.
1137 * Mount arguments can be specified in several ways, which are
1138 * distinguished by flag bit settings. The preferred way is to set
1139 * MS_OPTIONSTR, indicating an 8 argument mount with the file system
1140 * type supplied as a character string and the last two arguments
1141 * being a pointer to a character buffer and the size of the buffer.
1142 * On entry, the buffer holds a null terminated list of options; on
1143 * return, the string is the list of options the file system
1144 * recognized. If MS_DATA is set arguments five and six point to a
1145 * block of binary data which the file system interprets.
1146 * A further wrinkle is that some callers don't set MS_FSS and MS_DATA
1147 * consistently with these conventions. To handle them, we check to
1148 * see whether the pointer to the file system name has a numeric value
1149 * less than 256. If so, we treat it as an index.
1151 if (fsname
!= NULL
) {
1152 if ((vswp
= vfs_getvfssw(fsname
)) == NULL
) {
1155 } else if (uap
->flags
& (MS_OPTIONSTR
| MS_DATA
| MS_FSS
)) {
1161 if ((fstype
= (uintptr_t)uap
->fstype
) < 256) {
1163 if (fstype
== 0 || fstype
>= nfstype
||
1164 !ALLOCATED_VFSSW(&vfssw
[fstype
])) {
1168 (void) strcpy(fsname
, vfssw
[fstype
].vsw_name
);
1170 if ((vswp
= vfs_getvfssw(fsname
)) == NULL
)
1174 * Handle either kernel or user address space.
1176 if (uap
->flags
& MS_SYSSPACE
) {
1177 error
= copystr(uap
->fstype
, fsname
,
1180 error
= copyinstr(uap
->fstype
, fsname
,
1184 if (error
== ENAMETOOLONG
)
1188 if ((vswp
= vfs_getvfssw(fsname
)) == NULL
)
1192 if ((vswp
= vfs_getvfsswbyvfsops(vfs_getops(rootvfs
))) == NULL
)
1194 fsname
= vswp
->vsw_name
;
1196 if (!VFS_INSTALLED(vswp
))
1199 if ((error
= secpolicy_fs_allowed_mount(fsname
)) != 0) {
1200 vfs_unrefvfssw(vswp
);
1204 vfsops
= &vswp
->vsw_vfsops
;
1206 vfs_copyopttbl(&vswp
->vsw_optproto
, &mnt_mntopts
);
1208 * Fetch mount options and parse them for generic vfs options
1210 if (uap
->flags
& MS_OPTIONSTR
) {
1212 * Limit the buffer size
1214 if (optlen
< 0 || optlen
> MAX_MNTOPT_STR
) {
1218 if ((uap
->flags
& MS_SYSSPACE
) == 0) {
1219 inargs
= kmem_alloc(MAX_MNTOPT_STR
, KM_SLEEP
);
1222 error
= copyinstr(opts
, inargs
, (size_t)optlen
,
1229 vfs_parsemntopts(&mnt_mntopts
, inargs
, 0);
1232 * Flag bits override the options string.
1234 if (uap
->flags
& MS_REMOUNT
)
1235 vfs_setmntopt_nolock(&mnt_mntopts
, MNTOPT_REMOUNT
, NULL
, 0, 0);
1236 if (uap
->flags
& MS_RDONLY
)
1237 vfs_setmntopt_nolock(&mnt_mntopts
, MNTOPT_RO
, NULL
, 0, 0);
1238 if (uap
->flags
& MS_NOSUID
)
1239 vfs_setmntopt_nolock(&mnt_mntopts
, MNTOPT_NOSUID
, NULL
, 0, 0);
1242 * Check if this is a remount; must be set in the option string and
1243 * the file system must support a remount option.
1245 if (remount
= vfs_optionisset_nolock(&mnt_mntopts
,
1246 MNTOPT_REMOUNT
, NULL
)) {
1247 if (!(vswp
->vsw_flag
& VSW_CANREMOUNT
)) {
1251 uap
->flags
|= MS_REMOUNT
;
1255 * uap->flags and vfs_optionisset() should agree.
1257 if (rdonly
= vfs_optionisset_nolock(&mnt_mntopts
, MNTOPT_RO
, NULL
)) {
1258 uap
->flags
|= MS_RDONLY
;
1260 if (vfs_optionisset_nolock(&mnt_mntopts
, MNTOPT_NOSUID
, NULL
)) {
1261 uap
->flags
|= MS_NOSUID
;
1263 nbmand
= vfs_optionisset_nolock(&mnt_mntopts
, MNTOPT_NBMAND
, NULL
);
1264 ASSERT(splice
|| !remount
);
1266 * If we are splicing the fs into the namespace,
1267 * perform mount point checks.
1269 * We want to resolve the path for the mount point to eliminate
1270 * '.' and ".." and symlinks in mount points; we can't do the
1271 * same for the resource string, since it would turn
1272 * "/dev/dsk/c0t0d0s0" into "/devices/pci@...". We need to do
1273 * this before grabbing vn_vfswlock(), because otherwise we
1274 * would deadlock with lookuppn().
1277 ASSERT(vp
->v_count
> 0);
1280 * Pick up mount point and device from appropriate space.
1282 if (pn_get(uap
->spec
, fromspace
, &pn
) == 0) {
1283 resource
= kmem_alloc(pn
.pn_pathlen
+ 1,
1285 (void) strcpy(resource
, pn
.pn_path
);
1289 * Do a lookupname prior to taking the
1290 * writelock. Mark this as completed if
1291 * successful for later cleanup and addition to
1292 * the mount in progress table.
1294 if ((uap
->flags
& MS_GLOBAL
) == 0 &&
1295 lookupname(uap
->spec
, fromspace
,
1296 FOLLOW
, NULL
, &bvp
) == 0) {
1300 if ((error
= pn_get(uap
->dir
, fromspace
, &pn
)) == 0) {
1303 if (*pn
.pn_path
!= '/') {
1310 * Kludge to prevent autofs from deadlocking with
1311 * itself when it calls domount().
1313 * If autofs is calling, it is because it is doing
1314 * (autofs) mounts in the process of an NFS mount. A
1315 * lookuppn() here would cause us to block waiting for
1316 * said NFS mount to complete, which can't since this
1317 * is the thread that was supposed to doing it.
1319 if (fromspace
== UIO_USERSPACE
) {
1320 if ((error
= lookuppn(&pn
, &rpn
, FOLLOW
, NULL
,
1325 * The file disappeared or otherwise
1326 * became inaccessible since we opened
1327 * it; might as well fail the mount
1328 * since the mount point is no longer
1338 mountpt
= kmem_alloc(pnp
->pn_pathlen
+ 1, KM_SLEEP
);
1339 (void) strcpy(mountpt
, pnp
->pn_path
);
1342 * If the addition of the zone's rootpath
1343 * would push us over a total path length
1344 * of MAXPATHLEN, we fail the mount with
1345 * ENAMETOOLONG, which is what we would have
1346 * gotten if we were trying to perform the same
1347 * mount in the global zone.
1349 * strlen() doesn't count the trailing
1350 * '\0', but zone_rootpathlen counts both a
1351 * trailing '/' and the terminating '\0'.
1353 if ((curproc
->p_zone
->zone_rootpathlen
- 1 +
1354 strlen(mountpt
)) > MAXPATHLEN
||
1355 (resource
!= NULL
&&
1356 (curproc
->p_zone
->zone_rootpathlen
- 1 +
1357 strlen(resource
)) > MAXPATHLEN
)) {
1358 error
= ENAMETOOLONG
;
1369 * Prevent path name resolution from proceeding past
1372 if (vn_vfswlock(vp
) != 0) {
1378 * Verify that it's legitimate to establish a mount on
1379 * the prospective mount point.
1381 if (vn_mountedvfs(vp
) != NULL
) {
1383 * The mount point lock was obtained after some
1384 * other thread raced through and established a mount.
1390 if (vp
->v_flag
& VNOMOUNT
) {
1396 if ((uap
->flags
& (MS_DATA
| MS_OPTIONSTR
)) == 0) {
1397 uap
->dataptr
= NULL
;
1402 * If this is a remount, we don't want to create a new VFS.
1403 * Instead, we pass the existing one with a remount flag.
1407 * Confirm that the mount point is the root vnode of the
1408 * file system that is being remounted.
1409 * This can happen if the user specifies a different
1410 * mount point directory pathname in the (re)mount command.
1412 * Code below can only be reached if splice is true, so it's
1413 * safe to do vn_vfsunlock() here.
1415 if ((vp
->v_flag
& VROOT
) == 0) {
1421 * Disallow making file systems read-only unless file system
1422 * explicitly allows it in its vfssw. Ignore other flags.
1424 if (rdonly
&& vn_is_readonly(vp
) == 0 &&
1425 (vswp
->vsw_flag
& VSW_CANRWRO
) == 0) {
1431 * Disallow changing the NBMAND disposition of the file
1432 * system on remounts.
1434 if ((nbmand
&& ((vp
->v_vfsp
->vfs_flag
& VFS_NBMAND
) == 0)) ||
1435 (!nbmand
&& (vp
->v_vfsp
->vfs_flag
& VFS_NBMAND
))) {
1441 ovflags
= vfsp
->vfs_flag
;
1442 vfsp
->vfs_flag
|= VFS_REMOUNT
;
1443 vfsp
->vfs_flag
&= ~VFS_RDONLY
;
1445 vfsp
= vfs_alloc(KM_SLEEP
);
1446 VFS_INIT(vfsp
, vfsops
, NULL
);
1451 if ((error
= lofi_add(fsname
, vfsp
, &mnt_mntopts
, uap
)) != 0) {
1464 * PRIV_SYS_MOUNT doesn't mean you can become root.
1466 if (vfsp
->vfs_lofi_id
!= 0) {
1467 uap
->flags
|= MS_NOSUID
;
1468 vfs_setmntopt_nolock(&mnt_mntopts
, MNTOPT_NOSUID
, NULL
, 0, 0);
1472 * The vfs_reflock is not used anymore the code below explicitly
1473 * holds it preventing others accesing it directly.
1475 if ((sema_tryp(&vfsp
->vfs_reflock
) == 0) &&
1476 !(vfsp
->vfs_flag
& VFS_REMOUNT
))
1478 "mount type %s couldn't get vfs_reflock", vswp
->vsw_name
);
1481 * Lock the vfs. If this is a remount we want to avoid spurious umount
1482 * failures that happen as a side-effect of fsflush() and other mount
1483 * and unmount operations that might be going on simultaneously and
1484 * may have locked the vfs currently. To not return EBUSY immediately
1485 * here we use vfs_lock_wait() instead vfs_lock() for the remount case.
1488 if (error
= vfs_lock(vfsp
)) {
1489 vfsp
->vfs_flag
= ovflags
;
1499 vfs_lock_wait(vfsp
);
1503 * Add device to mount in progress table, global mounts require special
1504 * handling. It is possible that we have already done the lookupname
1505 * on a spliced, non-global fs. If so, we don't want to do it again
1506 * since we cannot do a lookupname after taking the
1507 * wlock above. This case is for a non-spliced, non-global filesystem.
1510 if ((uap
->flags
& MS_GLOBAL
) == 0 &&
1511 lookupname(uap
->spec
, fromspace
, FOLLOW
, NULL
, &bvp
) == 0) {
1517 vnode_t
*lvp
= NULL
;
1519 error
= vfs_get_lofi(vfsp
, &lvp
);
1534 } else if (error
== -1) {
1543 vfs_addmip(bdev
, vfsp
);
1548 * Invalidate cached entry for the mount point.
1554 * If have an option string but the filesystem doesn't supply a
1555 * prototype options table, create a table with the global
1556 * options and sufficient room to accept all the options in the
1557 * string. Then parse the passed in option string
1558 * accepting all the options in the string. This gives us an
1559 * option table with all the proper cancel properties for the
1562 * Filesystems that supply a prototype options table are handled
1563 * earlier in this function.
1565 if (uap
->flags
& MS_OPTIONSTR
) {
1566 if (!(vswp
->vsw_flag
& VSW_HASPROTO
)) {
1567 mntopts_t tmp_mntopts
;
1569 tmp_mntopts
.mo_count
= 0;
1570 vfs_createopttbl_extend(&tmp_mntopts
, inargs
,
1572 vfs_parsemntopts(&tmp_mntopts
, inargs
, 1);
1573 vfs_swapopttbl_nolock(&mnt_mntopts
, &tmp_mntopts
);
1574 vfs_freeopttbl(&tmp_mntopts
);
1579 * Serialize with zone state transitions.
1580 * See vfs_list_add; zone mounted into is:
1581 * zone_find_by_path(refstr_value(vfsp->vfs_mntpt))
1582 * not the zone doing the mount (curproc->p_zone), but if we're already
1583 * inside a NGZ, then we know what zone we are.
1585 if (INGLOBALZONE(curproc
)) {
1586 zone
= zone_find_by_path(mountpt
);
1587 ASSERT(zone
!= NULL
);
1589 zone
= curproc
->p_zone
;
1591 * zone_find_by_path does a hold, so do one here too so that
1592 * we can do a zone_rele after mount_completed.
1596 mount_in_progress(zone
);
1598 * Instantiate (or reinstantiate) the file system. If appropriate,
1599 * splice it into the file system name space.
1601 * We want VFS_MOUNT() to be able to override the vfs_resource
1602 * string if necessary (ie, mntfs), and also for a remount to
1603 * change the same (necessary when remounting '/' during boot).
1604 * So we set up vfs_mntpt and vfs_resource to what we think they
1605 * should be, then hand off control to VFS_MOUNT() which can
1608 * For safety's sake, when changing vfs_resource or vfs_mntpt of
1609 * a vfs which is on the vfs list (i.e. during a remount), we must
1610 * never set those fields to NULL. Several bits of code make
1611 * assumptions that the fields are always valid.
1613 vfs_swapopttbl(&mnt_mntopts
, &vfsp
->vfs_mntopts
);
1615 if ((oldresource
= vfsp
->vfs_resource
) != NULL
)
1616 refstr_hold(oldresource
);
1617 if ((oldmntpt
= vfsp
->vfs_mntpt
) != NULL
)
1618 refstr_hold(oldmntpt
);
1620 vfs_setresource(vfsp
, resource
, 0);
1621 vfs_setmntpoint(vfsp
, mountpt
, 0);
1624 * going to mount on this vnode, so notify.
1626 vnevent_mountedover(vp
, NULL
);
1627 error
= VFS_MOUNT(vfsp
, vp
, uap
, credp
);
1629 if (uap
->flags
& MS_RDONLY
)
1630 vfs_setmntopt(vfsp
, MNTOPT_RO
, NULL
, 0);
1631 if (uap
->flags
& MS_NOSUID
)
1632 vfs_setmntopt(vfsp
, MNTOPT_NOSUID
, NULL
, 0);
1633 if (uap
->flags
& MS_GLOBAL
)
1634 vfs_setmntopt(vfsp
, MNTOPT_GLOBAL
, NULL
, 0);
1640 /* put back pre-remount options */
1641 vfs_swapopttbl(&mnt_mntopts
, &vfsp
->vfs_mntopts
);
1642 vfs_setmntpoint(vfsp
, refstr_value(oldmntpt
),
1645 refstr_rele(oldmntpt
);
1646 vfs_setresource(vfsp
, refstr_value(oldresource
),
1649 refstr_rele(oldresource
);
1650 vfsp
->vfs_flag
= ovflags
;
1655 vfs_freemnttab(vfsp
);
1660 * Set the mount time to now
1662 vfsp
->vfs_mtime
= ddi_get_time();
1664 vfsp
->vfs_flag
&= ~VFS_REMOUNT
;
1666 refstr_rele(oldresource
);
1668 refstr_rele(oldmntpt
);
1669 } else if (splice
) {
1671 * Link vfsp into the name space at the mount
1672 * point. Vfs_add() is responsible for
1673 * holding the mount point which will be
1674 * released when vfs_remove() is called.
1676 vfs_add(vp
, vfsp
, uap
->flags
);
1679 * Hold the reference to file system which is
1680 * not linked into the name space.
1682 vfsp
->vfs_zone
= NULL
;
1684 vfsp
->vfs_vnodecovered
= NULL
;
1687 * Set flags for global options encountered
1689 if (vfs_optionisset(vfsp
, MNTOPT_RO
, NULL
))
1690 vfsp
->vfs_flag
|= VFS_RDONLY
;
1692 vfsp
->vfs_flag
&= ~VFS_RDONLY
;
1693 if (vfs_optionisset(vfsp
, MNTOPT_NOSUID
, NULL
)) {
1694 vfsp
->vfs_flag
|= (VFS_NOSETUID
|VFS_NODEVICES
);
1696 if (vfs_optionisset(vfsp
, MNTOPT_NODEVICES
, NULL
))
1697 vfsp
->vfs_flag
|= VFS_NODEVICES
;
1699 vfsp
->vfs_flag
&= ~VFS_NODEVICES
;
1700 if (vfs_optionisset(vfsp
, MNTOPT_NOSETUID
, NULL
))
1701 vfsp
->vfs_flag
|= VFS_NOSETUID
;
1703 vfsp
->vfs_flag
&= ~VFS_NOSETUID
;
1705 if (vfs_optionisset(vfsp
, MNTOPT_NBMAND
, NULL
))
1706 vfsp
->vfs_flag
|= VFS_NBMAND
;
1708 vfsp
->vfs_flag
&= ~VFS_NBMAND
;
1710 if (vfs_optionisset(vfsp
, MNTOPT_XATTR
, NULL
))
1711 vfsp
->vfs_flag
|= VFS_XATTR
;
1713 vfsp
->vfs_flag
&= ~VFS_XATTR
;
1715 if (vfs_optionisset(vfsp
, MNTOPT_NOEXEC
, NULL
))
1716 vfsp
->vfs_flag
|= VFS_NOEXEC
;
1718 vfsp
->vfs_flag
&= ~VFS_NOEXEC
;
1721 * Now construct the output option string of options
1724 if (uap
->flags
& MS_OPTIONSTR
) {
1725 vfs_list_read_lock();
1726 copyout_error
= vfs_buildoptionstr(
1727 &vfsp
->vfs_mntopts
, inargs
, optlen
);
1729 if (copyout_error
== 0 &&
1730 (uap
->flags
& MS_SYSSPACE
) == 0) {
1731 copyout_error
= copyoutstr(inargs
, opts
,
1737 * If this isn't a remount, set up the vopstats before
1738 * anyone can touch this. We only allow spliced file
1739 * systems (file systems which are in the namespace) to
1740 * have the VFS_STATS flag set.
1741 * NOTE: PxFS mounts the underlying file system with
1742 * MS_NOSPLICE set and copies those vfs_flags to its private
1743 * vfs structure. As a result, PxFS should never have
1744 * the VFS_STATS flag or else we might access the vfs
1745 * statistics-related fields prior to them being
1746 * properly initialized.
1748 if (!remount
&& (vswp
->vsw_flag
& VSW_STATS
) && splice
) {
1749 initialize_vopstats(&vfsp
->vfs_vopstats
);
1751 * We need to set vfs_vskap to NULL because there's
1752 * a chance it won't be set below. This is checked
1753 * in teardown_vopstats() so we can't have garbage.
1755 vfsp
->vfs_vskap
= NULL
;
1756 vfsp
->vfs_flag
|= VFS_STATS
;
1757 vfsp
->vfs_fstypevsp
= get_fstype_vopstats(vfsp
, vswp
);
1760 if (vswp
->vsw_flag
& VSW_XID
)
1761 vfsp
->vfs_flag
|= VFS_XID
;
1765 mount_completed(zone
);
1770 if ((error
== 0) && (copyout_error
== 0)) {
1773 * Don't call get_vskstat_anchor() while holding
1774 * locks since it allocates memory and calls
1775 * VFS_STATVFS(). For NFS, the latter can generate
1776 * an over-the-wire call.
1778 vskap
= get_vskstat_anchor(vfsp
);
1779 /* Only take the lock if we have something to do */
1780 if (vskap
!= NULL
) {
1781 vfs_lock_wait(vfsp
);
1782 if (vfsp
->vfs_flag
& VFS_STATS
) {
1783 vfsp
->vfs_vskap
= vskap
;
1788 /* Return vfsp to caller. */
1792 vfs_freeopttbl(&mnt_mntopts
);
1793 if (resource
!= NULL
)
1794 kmem_free(resource
, strlen(resource
) + 1);
1795 if (mountpt
!= NULL
)
1796 kmem_free(mountpt
, strlen(mountpt
) + 1);
1798 * It is possible we errored prior to adding to mount in progress
1799 * table. Must free vnode we acquired with successful lookupname.
1805 ASSERT(vswp
!= NULL
);
1806 vfs_unrefvfssw(vswp
);
1808 kmem_free(inargs
, MAX_MNTOPT_STR
);
1809 if (copyout_error
) {
1812 error
= copyout_error
;
1819 struct vfs
*vfsp
, /* vfs being updated */
1820 refstr_t
**refp
, /* Ref-count string to contain the new path */
1821 const char *newpath
, /* Path to add to refp (above) */
1822 uint32_t flag
) /* flag */
1826 zone_t
*zone
= curproc
->p_zone
;
1828 int have_list_lock
= 0;
1830 ASSERT(!VFS_ON_LIST(vfsp
) || vfs_lock_held(vfsp
));
1833 * New path must be less than MAXPATHLEN because mntfs
1834 * will only display up to MAXPATHLEN bytes. This is currently
1835 * safe, because domount() uses pn_get(), and other callers
1836 * similarly cap the size to fewer than MAXPATHLEN bytes.
1839 ASSERT(strlen(newpath
) < MAXPATHLEN
);
1841 /* mntfs requires consistency while vfs list lock is held */
1843 if (VFS_ON_LIST(vfsp
)) {
1852 * If we are in a non-global zone then we prefix the supplied path,
1853 * newpath, with the zone's root path, with two exceptions. The first
1854 * is where we have been explicitly directed to avoid doing so; this
1855 * will be the case following a failed remount, where the path supplied
1856 * will be a saved version which must now be restored. The second
1857 * exception is where newpath is not a pathname but a descriptive name,
1860 if (zone
== global_zone
|| (flag
& VFSSP_VERBATIM
) || *newpath
!= '/') {
1861 ref
= refstr_alloc(newpath
);
1866 * Truncate the trailing '/' in the zoneroot, and merge
1867 * in the zone's rootpath with the "newpath" (resource
1868 * or mountpoint) passed in.
1870 * The size of the required buffer is thus the size of
1871 * the buffer required for the passed-in newpath
1872 * (strlen(newpath) + 1), plus the size of the buffer
1873 * required to hold zone_rootpath (zone_rootpathlen)
1874 * minus one for one of the now-superfluous NUL
1875 * terminations, minus one for the trailing '/'.
1879 * (strlen(newpath) + 1) + zone_rootpathlen - 1 - 1
1881 * Which is what we have below.
1884 len
= strlen(newpath
) + zone
->zone_rootpathlen
- 1;
1885 sp
= kmem_alloc(len
, KM_SLEEP
);
1888 * Copy everything including the trailing slash, which
1889 * we then overwrite with the NUL character.
1892 (void) strcpy(sp
, zone
->zone_rootpath
);
1893 sp
[zone
->zone_rootpathlen
- 2] = '\0';
1894 (void) strcat(sp
, newpath
);
1896 ref
= refstr_alloc(sp
);
1901 if (have_list_lock
) {
1902 vfs_mnttab_modtimeupd();
1908 * Record a mounted resource name in a vfs structure.
1909 * If vfsp is already mounted, caller must hold the vfs lock.
1912 vfs_setresource(struct vfs
*vfsp
, const char *resource
, uint32_t flag
)
1914 if (resource
== NULL
|| resource
[0] == '\0')
1915 resource
= VFS_NORESOURCE
;
1916 vfs_setpath(vfsp
, &vfsp
->vfs_resource
, resource
, flag
);
1920 * Record a mount point name in a vfs structure.
1921 * If vfsp is already mounted, caller must hold the vfs lock.
1924 vfs_setmntpoint(struct vfs
*vfsp
, const char *mntpt
, uint32_t flag
)
1926 if (mntpt
== NULL
|| mntpt
[0] == '\0')
1927 mntpt
= VFS_NOMNTPT
;
1928 vfs_setpath(vfsp
, &vfsp
->vfs_mntpt
, mntpt
, flag
);
1931 /* Returns the vfs_resource. Caller must call refstr_rele() when finished. */
1934 vfs_getresource(const struct vfs
*vfsp
)
1938 vfs_list_read_lock();
1939 resource
= vfsp
->vfs_resource
;
1940 refstr_hold(resource
);
1946 /* Returns the vfs_mntpt. Caller must call refstr_rele() when finished. */
1949 vfs_getmntpoint(const struct vfs
*vfsp
)
1953 vfs_list_read_lock();
1954 mntpt
= vfsp
->vfs_mntpt
;
1962 * Create an empty options table with enough empty slots to hold all
1963 * The options in the options string passed as an argument.
1964 * Potentially prepend another options table.
1966 * Note: caller is responsible for locking the vfs list, if needed,
1970 vfs_createopttbl_extend(mntopts_t
*mops
, const char *opts
,
1971 const mntopts_t
*mtmpl
)
1973 const char *s
= opts
;
1976 if (opts
== NULL
|| *opts
== '\0') {
1982 * Count number of options in the string
1984 for (s
= strchr(s
, ','); s
!= NULL
; s
= strchr(s
, ',')) {
1989 vfs_copyopttbl_extend(mtmpl
, mops
, count
);
1993 * Create an empty options table with enough empty slots to hold all
1994 * The options in the options string passed as an argument.
1996 * This function is *not* for general use by filesystems.
1998 * Note: caller is responsible for locking the vfs list, if needed,
2002 vfs_createopttbl(mntopts_t
*mops
, const char *opts
)
2004 vfs_createopttbl_extend(mops
, opts
, NULL
);
2009 * Swap two mount options tables
2012 vfs_swapopttbl_nolock(mntopts_t
*optbl1
, mntopts_t
*optbl2
)
2017 tmpcnt
= optbl2
->mo_count
;
2018 tmplist
= optbl2
->mo_list
;
2019 optbl2
->mo_count
= optbl1
->mo_count
;
2020 optbl2
->mo_list
= optbl1
->mo_list
;
2021 optbl1
->mo_count
= tmpcnt
;
2022 optbl1
->mo_list
= tmplist
;
2026 vfs_swapopttbl(mntopts_t
*optbl1
, mntopts_t
*optbl2
)
2029 vfs_swapopttbl_nolock(optbl1
, optbl2
);
2030 vfs_mnttab_modtimeupd();
2035 vfs_copycancelopt_extend(char **const moc
, int extend
)
2042 for (; moc
[i
] != NULL
; i
++)
2043 /* count number of options to cancel */;
2046 if (i
+ extend
== 0)
2049 result
= kmem_alloc((i
+ extend
+ 1) * sizeof (char *), KM_SLEEP
);
2051 for (j
= 0; j
< i
; j
++) {
2052 result
[j
] = kmem_alloc(strlen(moc
[j
]) + 1, KM_SLEEP
);
2053 (void) strcpy(result
[j
], moc
[j
]);
2055 for (; j
<= i
+ extend
; j
++)
2062 vfs_copyopt(const mntopt_t
*s
, mntopt_t
*d
)
2066 d
->mo_flags
= s
->mo_flags
;
2067 d
->mo_data
= s
->mo_data
;
2070 dp
= kmem_alloc(strlen(sp
) + 1, KM_SLEEP
);
2071 (void) strcpy(dp
, sp
);
2074 d
->mo_name
= NULL
; /* should never happen */
2077 d
->mo_cancel
= vfs_copycancelopt_extend(s
->mo_cancel
, 0);
2081 dp
= kmem_alloc(strlen(sp
) + 1, KM_SLEEP
);
2082 (void) strcpy(dp
, sp
);
2090 * Copy a mount options table, possibly allocating some spare
2091 * slots at the end. It is permissible to copy_extend the NULL table.
2094 vfs_copyopttbl_extend(const mntopts_t
*smo
, mntopts_t
*dmo
, int extra
)
2100 * Clear out any existing stuff in the options table being initialized
2102 vfs_freeopttbl(dmo
);
2103 count
= (smo
== NULL
) ? 0 : smo
->mo_count
;
2104 if ((count
+ extra
) == 0) /* nothing to do */
2106 dmo
->mo_count
= count
+ extra
;
2107 motbl
= kmem_zalloc((count
+ extra
) * sizeof (mntopt_t
), KM_SLEEP
);
2108 dmo
->mo_list
= motbl
;
2109 for (i
= 0; i
< count
; i
++) {
2110 vfs_copyopt(&smo
->mo_list
[i
], &motbl
[i
]);
2112 for (i
= count
; i
< count
+ extra
; i
++) {
2113 motbl
[i
].mo_flags
= MO_EMPTY
;
2118 * Copy a mount options table.
2120 * This function is *not* for general use by filesystems.
2122 * Note: caller is responsible for locking the vfs list, if needed,
2123 * to protect smo and dmo.
2126 vfs_copyopttbl(const mntopts_t
*smo
, mntopts_t
*dmo
)
2128 vfs_copyopttbl_extend(smo
, dmo
, 0);
2132 vfs_mergecancelopts(const mntopt_t
*mop1
, const mntopt_t
*mop2
)
2137 char **sp1
, **sp2
, **dp
;
2140 * First we count both lists of cancel options.
2141 * If either is NULL or has no elements, we return a copy of
2144 if (mop1
->mo_cancel
!= NULL
) {
2145 for (; mop1
->mo_cancel
[c1
] != NULL
; c1
++)
2146 /* count cancel options in mop1 */;
2150 return (vfs_copycancelopt_extend(mop2
->mo_cancel
, 0));
2152 if (mop2
->mo_cancel
!= NULL
) {
2153 for (; mop2
->mo_cancel
[c2
] != NULL
; c2
++)
2154 /* count cancel options in mop2 */;
2157 result
= vfs_copycancelopt_extend(mop1
->mo_cancel
, c2
);
2163 * When we get here, we've got two sets of cancel options;
2164 * we need to merge the two sets. We know that the result
2165 * array has "c1+c2+1" entries and in the end we might shrink
2167 * Result now has a copy of the c1 entries from mop1; we'll
2168 * now lookup all the entries of mop2 in mop1 and copy it if
2170 * This operation is O(n^2) but it's only called once per
2171 * filesystem per duplicate option. This is a situation
2172 * which doesn't arise with the filesystems in ON and
2177 for (sp2
= mop2
->mo_cancel
; *sp2
!= NULL
; sp2
++) {
2178 for (sp1
= mop1
->mo_cancel
; *sp1
!= NULL
; sp1
++) {
2179 if (strcmp(*sp1
, *sp2
) == 0)
2184 * Option *sp2 not found in mop1, so copy it.
2185 * The calls to vfs_copycancelopt_extend()
2186 * guarantee that there's enough room.
2188 *dp
= kmem_alloc(strlen(*sp2
) + 1, KM_SLEEP
);
2189 (void) strcpy(*dp
++, *sp2
);
2192 if (dp
!= &result
[c1
+c2
]) {
2193 size_t bytes
= (dp
- result
+ 1) * sizeof (char *);
2194 char **nres
= kmem_alloc(bytes
, KM_SLEEP
);
2196 bcopy(result
, nres
, bytes
);
2197 kmem_free(result
, (c1
+ c2
+ 1) * sizeof (char *));
2204 * Merge two mount option tables (outer and inner) into one. This is very
2205 * similar to "merging" global variables and automatic variables in C.
2207 * This isn't (and doesn't have to be) fast.
2209 * This function is *not* for general use by filesystems.
2211 * Note: caller is responsible for locking the vfs list, if needed,
2212 * to protect omo, imo & dmo.
2215 vfs_mergeopttbl(const mntopts_t
*omo
, const mntopts_t
*imo
, mntopts_t
*dmo
)
2218 mntopt_t
*mop
, *motbl
;
2222 * First determine how much space we need to allocate.
2224 count
= omo
->mo_count
;
2225 for (i
= 0; i
< imo
->mo_count
; i
++) {
2226 if (imo
->mo_list
[i
].mo_flags
& MO_EMPTY
)
2228 if (vfs_hasopt(omo
, imo
->mo_list
[i
].mo_name
) == NULL
)
2231 ASSERT(count
>= omo
->mo_count
&&
2232 count
<= omo
->mo_count
+ imo
->mo_count
);
2233 motbl
= kmem_alloc(count
* sizeof (mntopt_t
), KM_SLEEP
);
2234 for (i
= 0; i
< omo
->mo_count
; i
++)
2235 vfs_copyopt(&omo
->mo_list
[i
], &motbl
[i
]);
2236 freeidx
= omo
->mo_count
;
2237 for (i
= 0; i
< imo
->mo_count
; i
++) {
2238 if (imo
->mo_list
[i
].mo_flags
& MO_EMPTY
)
2240 if ((mop
= vfs_hasopt(omo
, imo
->mo_list
[i
].mo_name
)) != NULL
) {
2242 uint_t index
= mop
- omo
->mo_list
;
2244 newcanp
= vfs_mergecancelopts(mop
, &motbl
[index
]);
2246 vfs_freeopt(&motbl
[index
]);
2247 vfs_copyopt(&imo
->mo_list
[i
], &motbl
[index
]);
2249 vfs_freecancelopt(motbl
[index
].mo_cancel
);
2250 motbl
[index
].mo_cancel
= newcanp
;
2253 * If it's a new option, just copy it over to the first
2256 vfs_copyopt(&imo
->mo_list
[i
], &motbl
[freeidx
++]);
2259 dmo
->mo_count
= count
;
2260 dmo
->mo_list
= motbl
;
2264 * Functions to set and clear mount options in a mount options table.
2268 * Clear a mount option, if it exists.
2270 * The update_mnttab arg indicates whether mops is part of a vfs that is on
2274 vfs_clearmntopt_nolock(mntopts_t
*mops
, const char *opt
, int update_mnttab
)
2279 ASSERT(!update_mnttab
|| RW_WRITE_HELD(&vfslist
));
2281 count
= mops
->mo_count
;
2282 for (i
= 0; i
< count
; i
++) {
2283 mop
= &mops
->mo_list
[i
];
2285 if (mop
->mo_flags
& MO_EMPTY
)
2287 if (strcmp(opt
, mop
->mo_name
))
2289 mop
->mo_flags
&= ~MO_SET
;
2290 if (mop
->mo_arg
!= NULL
) {
2291 kmem_free(mop
->mo_arg
, strlen(mop
->mo_arg
) + 1);
2295 vfs_mnttab_modtimeupd();
2301 vfs_clearmntopt(struct vfs
*vfsp
, const char *opt
)
2305 if (VFS_ON_LIST(vfsp
)) {
2309 vfs_clearmntopt_nolock(&vfsp
->vfs_mntopts
, opt
, gotlock
);
2316 * Set a mount option on. If it's not found in the table, it's silently
2317 * ignored. If the option has MO_IGNORE set, it is still set unless the
2318 * VFS_NOFORCEOPT bit is set in the flags. Also, VFS_DISPLAY/VFS_NODISPLAY flag
2319 * bits can be used to toggle the MO_NODISPLAY bit for the option.
2320 * If the VFS_CREATEOPT flag bit is set then the first option slot with
2321 * MO_EMPTY set is created as the option passed in.
2323 * The update_mnttab arg indicates whether mops is part of a vfs that is on
2327 vfs_setmntopt_nolock(mntopts_t
*mops
, const char *opt
,
2328 const char *arg
, int flags
, int update_mnttab
)
2334 ASSERT(!update_mnttab
|| RW_WRITE_HELD(&vfslist
));
2336 if (flags
& VFS_CREATEOPT
) {
2337 if (vfs_hasopt(mops
, opt
) != NULL
) {
2338 flags
&= ~VFS_CREATEOPT
;
2341 count
= mops
->mo_count
;
2342 for (i
= 0; i
< count
; i
++) {
2343 mop
= &mops
->mo_list
[i
];
2345 if (mop
->mo_flags
& MO_EMPTY
) {
2346 if ((flags
& VFS_CREATEOPT
) == 0)
2348 sp
= kmem_alloc(strlen(opt
) + 1, KM_SLEEP
);
2349 (void) strcpy(sp
, opt
);
2352 mop
->mo_flags
= MO_HASVALUE
;
2355 } else if (strcmp(opt
, mop
->mo_name
)) {
2358 if ((mop
->mo_flags
& MO_IGNORE
) && (flags
& VFS_NOFORCEOPT
))
2360 if (arg
!= NULL
&& (mop
->mo_flags
& MO_HASVALUE
) != 0) {
2361 sp
= kmem_alloc(strlen(arg
) + 1, KM_SLEEP
);
2362 (void) strcpy(sp
, arg
);
2366 if (mop
->mo_arg
!= NULL
)
2367 kmem_free(mop
->mo_arg
, strlen(mop
->mo_arg
) + 1);
2369 if (flags
& VFS_DISPLAY
)
2370 mop
->mo_flags
&= ~MO_NODISPLAY
;
2371 if (flags
& VFS_NODISPLAY
)
2372 mop
->mo_flags
|= MO_NODISPLAY
;
2373 mop
->mo_flags
|= MO_SET
;
2374 if (mop
->mo_cancel
!= NULL
) {
2377 for (cp
= mop
->mo_cancel
; *cp
!= NULL
; cp
++)
2378 vfs_clearmntopt_nolock(mops
, *cp
, 0);
2381 vfs_mnttab_modtimeupd();
2387 vfs_setmntopt(struct vfs
*vfsp
, const char *opt
, const char *arg
, int flags
)
2391 if (VFS_ON_LIST(vfsp
)) {
2395 vfs_setmntopt_nolock(&vfsp
->vfs_mntopts
, opt
, arg
, flags
, gotlock
);
2402 * Add a "tag" option to a mounted file system's options list.
2404 * Note: caller is responsible for locking the vfs list, if needed,
2408 vfs_addtag(mntopts_t
*mops
, const char *tag
)
2411 mntopt_t
*mop
, *motbl
;
2413 count
= mops
->mo_count
+ 1;
2414 motbl
= kmem_zalloc(count
* sizeof (mntopt_t
), KM_SLEEP
);
2415 if (mops
->mo_count
) {
2416 size_t len
= (count
- 1) * sizeof (mntopt_t
);
2418 bcopy(mops
->mo_list
, motbl
, len
);
2419 kmem_free(mops
->mo_list
, len
);
2421 mops
->mo_count
= count
;
2422 mops
->mo_list
= motbl
;
2423 mop
= &motbl
[count
- 1];
2424 mop
->mo_flags
= MO_TAG
;
2425 mop
->mo_name
= kmem_alloc(strlen(tag
) + 1, KM_SLEEP
);
2426 (void) strcpy(mop
->mo_name
, tag
);
2431 * Allow users to set arbitrary "tags" in a vfs's mount options.
2432 * Broader use within the kernel is discouraged.
2435 vfs_settag(uint_t major
, uint_t minor
, const char *mntpt
, const char *tag
,
2442 dev_t dev
= makedevice(major
, minor
);
2444 char *buf
= kmem_alloc(MAX_MNTOPT_STR
, KM_SLEEP
);
2447 * Find the desired mounted file system
2452 if (vfsp
->vfs_dev
== dev
&&
2453 strcmp(mntpt
, refstr_value(vfsp
->vfs_mntpt
)) == 0) {
2457 vfsp
= vfsp
->vfs_next
;
2458 } while (vfsp
!= rootvfs
);
2464 err
= secpolicy_fs_config(cr
, vfsp
);
2468 mops
= &vfsp
->vfs_mntopts
;
2470 * Add tag if it doesn't already exist
2472 if ((mop
= vfs_hasopt(mops
, tag
)) == NULL
) {
2475 (void) vfs_buildoptionstr(mops
, buf
, MAX_MNTOPT_STR
);
2477 if (len
+ strlen(tag
) + 2 > MAX_MNTOPT_STR
) {
2481 mop
= vfs_addtag(mops
, tag
);
2483 if ((mop
->mo_flags
& MO_TAG
) == 0) {
2487 vfs_setmntopt_nolock(mops
, tag
, NULL
, 0, 1);
2490 kmem_free(buf
, MAX_MNTOPT_STR
);
2495 * Allow users to remove arbitrary "tags" in a vfs's mount options.
2496 * Broader use within the kernel is discouraged.
2499 vfs_clrtag(uint_t major
, uint_t minor
, const char *mntpt
, const char *tag
,
2505 dev_t dev
= makedevice(major
, minor
);
2509 * Find the desired mounted file system
2514 if (vfsp
->vfs_dev
== dev
&&
2515 strcmp(mntpt
, refstr_value(vfsp
->vfs_mntpt
)) == 0) {
2519 vfsp
= vfsp
->vfs_next
;
2520 } while (vfsp
!= rootvfs
);
2526 err
= secpolicy_fs_config(cr
, vfsp
);
2530 if ((mop
= vfs_hasopt(&vfsp
->vfs_mntopts
, tag
)) == NULL
) {
2534 if ((mop
->mo_flags
& MO_TAG
) == 0) {
2538 vfs_clearmntopt_nolock(&vfsp
->vfs_mntopts
, tag
, 1);
2545 * Function to parse an option string and fill in a mount options table.
2546 * Unknown options are silently ignored. The input option string is modified
2547 * by replacing separators with nulls. If the create flag is set, options
2548 * not found in the table are just added on the fly. The table must have
2549 * an option slot marked MO_EMPTY to add an option on the fly.
2551 * This function is *not* for general use by filesystems.
2553 * Note: caller is responsible for locking the vfs list, if needed,
2557 vfs_parsemntopts(mntopts_t
*mops
, char *osp
, int create
)
2559 char *s
= osp
, *p
, *nextop
, *valp
, *cp
, *ep
;
2560 int setflg
= VFS_NOFORCEOPT
;
2564 while (*s
!= '\0') {
2565 p
= strchr(s
, ','); /* find next option */
2570 cp
= p
; /* save location of comma */
2571 *p
++ = '\0'; /* mark end and point to next option */
2574 p
= strchr(s
, '='); /* look for value */
2576 valp
= NULL
; /* no value supplied */
2578 ep
= p
; /* save location of equals */
2579 *p
++ = '\0'; /* end option and point to value */
2583 * set option into options table
2586 setflg
|= VFS_CREATEOPT
;
2587 vfs_setmntopt_nolock(mops
, s
, valp
, setflg
, 0);
2589 *cp
= ','; /* restore the comma */
2591 *ep
= '='; /* restore the equals */
2597 * Function to inquire if an option exists in a mount options table.
2598 * Returns a pointer to the option if it exists, else NULL.
2600 * This function is *not* for general use by filesystems.
2602 * Note: caller is responsible for locking the vfs list, if needed,
2606 vfs_hasopt(const mntopts_t
*mops
, const char *opt
)
2611 count
= mops
->mo_count
;
2612 for (i
= 0; i
< count
; i
++) {
2613 mop
= &mops
->mo_list
[i
];
2615 if (mop
->mo_flags
& MO_EMPTY
)
2617 if (strcmp(opt
, mop
->mo_name
) == 0)
2624 * Function to inquire if an option is set in a mount options table.
2625 * Returns non-zero if set and fills in the arg pointer with a pointer to
2626 * the argument string or NULL if there is no argument string.
2629 vfs_optionisset_nolock(const mntopts_t
*mops
, const char *opt
, char **argp
)
2634 count
= mops
->mo_count
;
2635 for (i
= 0; i
< count
; i
++) {
2636 mop
= &mops
->mo_list
[i
];
2638 if (mop
->mo_flags
& MO_EMPTY
)
2640 if (strcmp(opt
, mop
->mo_name
))
2642 if ((mop
->mo_flags
& MO_SET
) == 0)
2644 if (argp
!= NULL
&& (mop
->mo_flags
& MO_HASVALUE
) != 0)
2645 *argp
= mop
->mo_arg
;
2653 vfs_optionisset(const struct vfs
*vfsp
, const char *opt
, char **argp
)
2657 vfs_list_read_lock();
2658 ret
= vfs_optionisset_nolock(&vfsp
->vfs_mntopts
, opt
, argp
);
2665 * Construct a comma separated string of the options set in the given
2666 * mount table, return the string in the given buffer. Return non-zero if
2667 * the buffer would overflow.
2669 * This function is *not* for general use by filesystems.
2671 * Note: caller is responsible for locking the vfs list, if needed,
2675 vfs_buildoptionstr(const mntopts_t
*mp
, char *buf
, int len
)
2682 for (i
= 0; i
< mp
->mo_count
; i
++) {
2685 mop
= &mp
->mo_list
[i
];
2686 if (mop
->mo_flags
& MO_SET
) {
2687 int optlen
, comma
= 0;
2691 optlen
= strlen(mop
->mo_name
);
2692 if (strlen(buf
) + comma
+ optlen
+ 1 > len
)
2696 (void) strcpy(cp
, mop
->mo_name
);
2699 * Append option value if there is one
2701 if (mop
->mo_arg
!= NULL
) {
2704 arglen
= strlen(mop
->mo_arg
);
2705 if (strlen(buf
) + arglen
+ 2 > len
)
2708 (void) strcpy(cp
, mop
->mo_arg
);
2719 vfs_freecancelopt(char **moc
)
2725 for (cp
= moc
; *cp
!= NULL
; cp
++) {
2726 kmem_free(*cp
, strlen(*cp
) + 1);
2729 kmem_free(moc
, (ccnt
+ 1) * sizeof (char *));
2734 vfs_freeopt(mntopt_t
*mop
)
2736 if (mop
->mo_name
!= NULL
)
2737 kmem_free(mop
->mo_name
, strlen(mop
->mo_name
) + 1);
2739 vfs_freecancelopt(mop
->mo_cancel
);
2741 if (mop
->mo_arg
!= NULL
)
2742 kmem_free(mop
->mo_arg
, strlen(mop
->mo_arg
) + 1);
2746 * Free a mount options table
2748 * This function is *not* for general use by filesystems.
2750 * Note: caller is responsible for locking the vfs list, if needed,
2754 vfs_freeopttbl(mntopts_t
*mp
)
2758 count
= mp
->mo_count
;
2759 for (i
= 0; i
< count
; i
++) {
2760 vfs_freeopt(&mp
->mo_list
[i
]);
2763 kmem_free(mp
->mo_list
, sizeof (mntopt_t
) * count
);
2772 vfs_mntdummyread(vnode_t
*vp
, uio_t
*uio
, int ioflag
, cred_t
*cred
,
2773 caller_context_t
*ct
)
2780 vfs_mntdummywrite(vnode_t
*vp
, uio_t
*uio
, int ioflag
, cred_t
*cred
,
2781 caller_context_t
*ct
)
2787 * The dummy vnode is currently used only by file events notification
2788 * module which is just interested in the timestamps.
2792 vfs_mntdummygetattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, cred_t
*cr
,
2793 caller_context_t
*ct
)
2795 bzero(vap
, sizeof (vattr_t
));
2796 vap
->va_type
= VREG
;
2798 vap
->va_ctime
= vfs_mnttab_ctime
;
2800 * it is ok to just copy mtime as the time will be monotonically
2803 vap
->va_mtime
= vfs_mnttab_mtime
;
2804 vap
->va_atime
= vap
->va_mtime
;
2809 vfs_mnttabvp_setup(void)
2812 vnodeops_t
*vfs_mntdummyvnops
;
2813 const fs_operation_def_t mnt_dummyvnodeops_template
[] = {
2814 VOPNAME_READ
, { .vop_read
= vfs_mntdummyread
},
2815 VOPNAME_WRITE
, { .vop_write
= vfs_mntdummywrite
},
2816 VOPNAME_GETATTR
, { .vop_getattr
= vfs_mntdummygetattr
},
2817 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
2821 if (vn_make_ops("mnttab", mnt_dummyvnodeops_template
,
2822 &vfs_mntdummyvnops
) != 0) {
2823 cmn_err(CE_WARN
, "vfs_mnttabvp_setup: vn_make_ops failed");
2824 /* Shouldn't happen, but not bad enough to panic */
2829 * A global dummy vnode is allocated to represent mntfs files.
2830 * The mntfs file (/etc/mnttab) can be monitored for file events
2831 * and receive an event when mnttab changes. Dummy VOP calls
2832 * will be made on this vnode. The file events notification module
2833 * intercepts this vnode and delivers relevant events.
2835 tvp
= vn_alloc(KM_SLEEP
);
2836 tvp
->v_flag
= VNOMOUNT
|VNOMAP
|VNOSWAP
|VNOCACHE
;
2837 vn_setops(tvp
, vfs_mntdummyvnops
);
2840 * The mnt dummy ops do not reference v_data.
2841 * No other module intercepting this vnode should either.
2842 * Just set it to point to itself.
2844 tvp
->v_data
= (caddr_t
)tvp
;
2845 tvp
->v_vfsp
= rootvfs
;
2846 vfs_mntdummyvp
= tvp
;
2850 * performs fake read/write ops
2853 vfs_mnttab_rwop(int rw
)
2859 if (vfs_mntdummyvp
== NULL
)
2862 bzero(&uio
, sizeof (uio
));
2863 bzero(&iov
, sizeof (iov
));
2868 uio
.uio_loffset
= 0;
2869 uio
.uio_segflg
= UIO_SYSSPACE
;
2872 (void) VOP_WRITE(vfs_mntdummyvp
, &uio
, 0, kcred
, NULL
);
2874 (void) VOP_READ(vfs_mntdummyvp
, &uio
, 0, kcred
, NULL
);
2879 * Generate a write operation.
2882 vfs_mnttab_writeop(void)
2888 * Generate a read operation.
2891 vfs_mnttab_readop(void)
2897 * Free any mnttab information recorded in the vfs struct.
2898 * The vfs must not be on the vfs list.
2901 vfs_freemnttab(struct vfs
*vfsp
)
2903 ASSERT(!VFS_ON_LIST(vfsp
));
2906 * Free device and mount point information
2908 if (vfsp
->vfs_mntpt
!= NULL
) {
2909 refstr_rele(vfsp
->vfs_mntpt
);
2910 vfsp
->vfs_mntpt
= NULL
;
2912 if (vfsp
->vfs_resource
!= NULL
) {
2913 refstr_rele(vfsp
->vfs_resource
);
2914 vfsp
->vfs_resource
= NULL
;
2917 * Now free mount options information
2919 vfs_freeopttbl(&vfsp
->vfs_mntopts
);
2923 * Return the last mnttab modification time
2926 vfs_mnttab_modtime(timespec_t
*ts
)
2928 ASSERT(RW_LOCK_HELD(&vfslist
));
2929 *ts
= vfs_mnttab_mtime
;
2933 * See if mnttab is changed
2936 vfs_mnttab_poll(timespec_t
*old
, struct pollhead
**phpp
)
2940 *phpp
= (struct pollhead
*)NULL
;
2943 * Note: don't grab vfs list lock before accessing vfs_mnttab_mtime.
2944 * Can lead to deadlock against vfs_mnttab_modtimeupd(). It is safe
2945 * to not grab the vfs list lock because tv_sec is monotonically
2949 changed
= (old
->tv_nsec
!= vfs_mnttab_mtime
.tv_nsec
) ||
2950 (old
->tv_sec
!= vfs_mnttab_mtime
.tv_sec
);
2952 *phpp
= &vfs_pollhd
;
2956 /* Provide a unique and monotonically-increasing timestamp. */
2958 vfs_mono_time(timespec_t
*ts
)
2960 static volatile hrtime_t hrt
; /* The saved time. */
2961 hrtime_t newhrt
, oldhrt
; /* For effecting the CAS. */
2965 * Try gethrestime() first, but be prepared to fabricate a sensible
2966 * answer at the first sign of any trouble.
2968 gethrestime(&newts
);
2969 newhrt
= ts2hrt(&newts
);
2974 if (atomic_cas_64((uint64_t *)&hrt
, oldhrt
, newhrt
) == oldhrt
)
2981 * Update the mnttab modification time and wake up any waiters for
2985 vfs_mnttab_modtimeupd()
2987 hrtime_t oldhrt
, newhrt
;
2989 ASSERT(RW_WRITE_HELD(&vfslist
));
2990 oldhrt
= ts2hrt(&vfs_mnttab_mtime
);
2991 gethrestime(&vfs_mnttab_mtime
);
2992 newhrt
= ts2hrt(&vfs_mnttab_mtime
);
2993 if (oldhrt
== (hrtime_t
)0)
2994 vfs_mnttab_ctime
= vfs_mnttab_mtime
;
2996 * Attempt to provide unique mtime (like uniqtime but not).
2998 if (newhrt
== oldhrt
) {
3000 hrt2ts(newhrt
, &vfs_mnttab_mtime
);
3002 pollwakeup(&vfs_pollhd
, (short)POLLRDBAND
);
3003 vfs_mnttab_writeop();
3007 dounmount(struct vfs
*vfsp
, int flag
, cred_t
*cr
)
3011 extern void teardown_vopstats(vfs_t
*);
3014 * Get covered vnode. This will be NULL if the vfs is not linked
3015 * into the file system name space (i.e., domount() with MNT_NOSPICE).
3017 coveredvp
= vfsp
->vfs_vnodecovered
;
3018 ASSERT(coveredvp
== NULL
|| vn_vfswlock_held(coveredvp
));
3021 * Purge all dnlc entries for this vfs.
3023 (void) dnlc_purge_vfsp(vfsp
, 0);
3025 /* For forcible umount, skip VFS_SYNC() since it may hang */
3026 if ((flag
& MS_FORCE
) == 0)
3027 (void) VFS_SYNC(vfsp
, 0, cr
);
3030 * Lock the vfs to maintain fs status quo during unmount. This
3031 * has to be done after the sync because ufs_update tries to acquire
3034 vfs_lock_wait(vfsp
);
3036 if (error
= VFS_UNMOUNT(vfsp
, flag
, cr
)) {
3038 if (coveredvp
!= NULL
)
3039 vn_vfsunlock(coveredvp
);
3040 } else if (coveredvp
!= NULL
) {
3041 teardown_vopstats(vfsp
);
3043 * vfs_remove() will do a VN_RELE(vfsp->vfs_vnodecovered)
3044 * when it frees vfsp so we do a VN_HOLD() so we can
3045 * continue to use coveredvp afterwards.
3049 vn_vfsunlock(coveredvp
);
3052 teardown_vopstats(vfsp
);
3054 * Release the reference to vfs that is not linked
3055 * into the name space.
3065 * Vfs_unmountall() is called by uadmin() to unmount all
3066 * mounted file systems (except the root file system) during shutdown.
3067 * It follows the existing locking protocol when traversing the vfs list
3068 * to sync and unmount vfses. Even though there should be no
3069 * other thread running while the system is shutting down, it is prudent
3070 * to still follow the locking protocol.
3073 vfs_unmountall(void)
3076 struct vfs
*prev_vfsp
= NULL
;
3080 * Toss all dnlc entries now so that the per-vfs sync
3081 * and unmount operations don't have to slog through
3082 * a bunch of uninteresting vnodes over and over again.
3087 for (vfsp
= rootvfs
->vfs_prev
; vfsp
!= rootvfs
; vfsp
= prev_vfsp
) {
3088 prev_vfsp
= vfsp
->vfs_prev
;
3090 if (vfs_lock(vfsp
) != 0)
3092 error
= vn_vfswlock(vfsp
->vfs_vnodecovered
);
3099 (void) VFS_SYNC(vfsp
, SYNC_CLOSE
, CRED());
3100 (void) dounmount(vfsp
, 0, CRED());
3103 * Since we dropped the vfslist lock above we must
3104 * verify that next_vfsp still exists, else start over.
3107 for (vfsp
= rootvfs
->vfs_prev
;
3108 vfsp
!= rootvfs
; vfsp
= vfsp
->vfs_prev
)
3109 if (vfsp
== prev_vfsp
)
3111 if (vfsp
== rootvfs
&& prev_vfsp
!= rootvfs
)
3112 prev_vfsp
= rootvfs
->vfs_prev
;
3118 * Called to add an entry to the end of the vfs mount in progress list
3121 vfs_addmip(dev_t dev
, struct vfs
*vfsp
)
3125 mipp
= (struct ipmnt
*)kmem_alloc(sizeof (struct ipmnt
), KM_SLEEP
);
3126 mipp
->mip_next
= NULL
;
3127 mipp
->mip_dev
= dev
;
3128 mipp
->mip_vfsp
= vfsp
;
3129 mutex_enter(&vfs_miplist_mutex
);
3130 if (vfs_miplist_end
!= NULL
)
3131 vfs_miplist_end
->mip_next
= mipp
;
3134 vfs_miplist_end
= mipp
;
3135 mutex_exit(&vfs_miplist_mutex
);
3139 * Called to remove an entry from the mount in progress list
3140 * Either because the mount completed or it failed.
3143 vfs_delmip(struct vfs
*vfsp
)
3145 struct ipmnt
*mipp
, *mipprev
;
3147 mutex_enter(&vfs_miplist_mutex
);
3149 for (mipp
= vfs_miplist
;
3150 mipp
&& mipp
->mip_vfsp
!= vfsp
; mipp
= mipp
->mip_next
) {
3154 return; /* shouldn't happen */
3155 if (mipp
== vfs_miplist_end
)
3156 vfs_miplist_end
= mipprev
;
3157 if (mipprev
== NULL
)
3158 vfs_miplist
= mipp
->mip_next
;
3160 mipprev
->mip_next
= mipp
->mip_next
;
3161 mutex_exit(&vfs_miplist_mutex
);
3162 kmem_free(mipp
, sizeof (struct ipmnt
));
3166 * vfs_add is called by a specific filesystem's mount routine to add
3167 * the new vfs into the vfs list/hash and to cover the mounted-on vnode.
3168 * The vfs should already have been locked by the caller.
3170 * coveredvp is NULL if this is the root.
3173 vfs_add(vnode_t
*coveredvp
, struct vfs
*vfsp
, int mflag
)
3177 ASSERT(vfs_lock_held(vfsp
));
3179 newflag
= vfsp
->vfs_flag
;
3180 if (mflag
& MS_RDONLY
)
3181 newflag
|= VFS_RDONLY
;
3183 newflag
&= ~VFS_RDONLY
;
3184 if (mflag
& MS_NOSUID
)
3185 newflag
|= (VFS_NOSETUID
|VFS_NODEVICES
);
3187 newflag
&= ~(VFS_NOSETUID
|VFS_NODEVICES
);
3188 if (mflag
& MS_NOMNTTAB
)
3189 newflag
|= VFS_NOMNTTAB
;
3191 newflag
&= ~VFS_NOMNTTAB
;
3193 if (coveredvp
!= NULL
) {
3194 ASSERT(vn_vfswlock_held(coveredvp
));
3195 coveredvp
->v_vfsmountedhere
= vfsp
;
3198 vfsp
->vfs_vnodecovered
= coveredvp
;
3199 vfsp
->vfs_flag
= newflag
;
3205 * Remove a vfs from the vfs list, null out the pointer from the
3206 * covered vnode to the vfs (v_vfsmountedhere), and null out the pointer
3207 * from the vfs to the covered vnode (vfs_vnodecovered). Release the
3208 * reference to the vfs and to the covered vnode.
3210 * Called from dounmount after it's confirmed with the file system
3211 * that the unmount is legal.
3214 vfs_remove(struct vfs
*vfsp
)
3218 ASSERT(vfs_lock_held(vfsp
));
3221 * Can't unmount root. Should never happen because fs will
3224 if (vfsp
== rootvfs
)
3225 panic("vfs_remove: unmounting root");
3227 vfs_list_remove(vfsp
);
3230 * Unhook from the file system name space.
3232 vp
= vfsp
->vfs_vnodecovered
;
3233 ASSERT(vn_vfswlock_held(vp
));
3234 vp
->v_vfsmountedhere
= NULL
;
3235 vfsp
->vfs_vnodecovered
= NULL
;
3239 * Release lock and wakeup anybody waiting.
3246 * Lock a filesystem to prevent access to it while mounting,
3247 * unmounting and syncing. Return EBUSY immediately if lock
3248 * can't be acquired.
3251 vfs_lock(vfs_t
*vfsp
)
3253 vn_vfslocks_entry_t
*vpvfsentry
;
3255 vpvfsentry
= vn_vfslocks_getlock(vfsp
);
3256 if (rwst_tryenter(&vpvfsentry
->ve_lock
, RW_WRITER
))
3259 vn_vfslocks_rele(vpvfsentry
);
3264 vfs_rlock(vfs_t
*vfsp
)
3266 vn_vfslocks_entry_t
*vpvfsentry
;
3268 vpvfsentry
= vn_vfslocks_getlock(vfsp
);
3270 if (rwst_tryenter(&vpvfsentry
->ve_lock
, RW_READER
))
3273 vn_vfslocks_rele(vpvfsentry
);
3278 vfs_lock_wait(vfs_t
*vfsp
)
3280 vn_vfslocks_entry_t
*vpvfsentry
;
3282 vpvfsentry
= vn_vfslocks_getlock(vfsp
);
3283 rwst_enter(&vpvfsentry
->ve_lock
, RW_WRITER
);
3287 vfs_rlock_wait(vfs_t
*vfsp
)
3289 vn_vfslocks_entry_t
*vpvfsentry
;
3291 vpvfsentry
= vn_vfslocks_getlock(vfsp
);
3292 rwst_enter(&vpvfsentry
->ve_lock
, RW_READER
);
3296 * Unlock a locked filesystem.
3299 vfs_unlock(vfs_t
*vfsp
)
3301 vn_vfslocks_entry_t
*vpvfsentry
;
3304 * vfs_unlock will mimic sema_v behaviour to fix 4748018.
3305 * And these changes should remain for the patch changes as it is.
3311 * ve_refcount needs to be dropped twice here.
3312 * 1. To release refernce after a call to vfs_locks_getlock()
3313 * 2. To release the reference from the locking routines like
3314 * vfs_rlock_wait/vfs_wlock_wait/vfs_wlock etc,.
3317 vpvfsentry
= vn_vfslocks_getlock(vfsp
);
3318 vn_vfslocks_rele(vpvfsentry
);
3320 rwst_exit(&vpvfsentry
->ve_lock
);
3321 vn_vfslocks_rele(vpvfsentry
);
3325 * Utility routine that allows a filesystem to construct its
3326 * fsid in "the usual way" - by munging some underlying dev_t and
3327 * the filesystem type number into the 64-bit fsid. Note that
3328 * this implicitly relies on dev_t persistence to make filesystem
3331 * There's nothing to prevent an individual fs from constructing its
3332 * fsid in a different way, and indeed they should.
3334 * Since we want fsids to be 32-bit quantities (so that they can be
3335 * exported identically by either 32-bit or 64-bit APIs, as well as
3336 * the fact that fsid's are "known" to NFS), we compress the device
3337 * number given down to 32-bits, and panic if that isn't possible.
3340 vfs_make_fsid(fsid_t
*fsi
, dev_t dev
, int val
)
3342 if (!cmpldev((dev32_t
*)&fsi
->val
[0], dev
))
3343 panic("device number too big for fsid!");
3348 vfs_lock_held(vfs_t
*vfsp
)
3351 vn_vfslocks_entry_t
*vpvfsentry
;
3354 * vfs_lock_held will mimic sema_held behaviour
3355 * if panicstr is set. And these changes should remain
3356 * for the patch changes as it is.
3361 vpvfsentry
= vn_vfslocks_getlock(vfsp
);
3362 held
= rwst_lock_held(&vpvfsentry
->ve_lock
, RW_WRITER
);
3364 vn_vfslocks_rele(vpvfsentry
);
3369 vfs_lock_owner(vfs_t
*vfsp
)
3371 struct _kthread
*owner
;
3372 vn_vfslocks_entry_t
*vpvfsentry
;
3375 * vfs_wlock_held will mimic sema_held behaviour
3376 * if panicstr is set. And these changes should remain
3377 * for the patch changes as it is.
3382 vpvfsentry
= vn_vfslocks_getlock(vfsp
);
3383 owner
= rwst_owner(&vpvfsentry
->ve_lock
);
3385 vn_vfslocks_rele(vpvfsentry
);
3392 * Rather than manipulate the vfslist lock directly, we abstract into lock
3393 * and unlock routines to allow the locking implementation to be changed for
3396 * Whenever the vfs list is modified through its hash links, the overall list
3397 * lock must be obtained before locking the relevant hash bucket. But to see
3398 * whether a given vfs is on the list, it suffices to obtain the lock for the
3399 * hash bucket without getting the overall list lock. (See getvfs() below.)
3405 rw_enter(&vfslist
, RW_WRITER
);
3409 vfs_list_read_lock()
3411 rw_enter(&vfslist
, RW_READER
);
3421 * Low level worker routines for adding entries to and removing entries from
3426 vfs_hash_add(struct vfs
*vfsp
, int insert_at_head
)
3432 ASSERT(RW_WRITE_HELD(&vfslist
));
3434 dev
= expldev(vfsp
->vfs_fsid
.val
[0]);
3435 vhno
= VFSHASH(getmajor(dev
), getminor(dev
));
3437 mutex_enter(&rvfs_list
[vhno
].rvfs_lock
);
3440 * Link into the hash table, inserting it at the end, so that LOFS
3441 * with the same fsid as UFS (or other) file systems will not hide the
3444 if (insert_at_head
) {
3445 vfsp
->vfs_hash
= rvfs_list
[vhno
].rvfs_head
;
3446 rvfs_list
[vhno
].rvfs_head
= vfsp
;
3448 for (hp
= &rvfs_list
[vhno
].rvfs_head
; *hp
!= NULL
;
3449 hp
= &(*hp
)->vfs_hash
)
3452 * hp now contains the address of the pointer to update
3453 * to effect the insertion.
3455 vfsp
->vfs_hash
= NULL
;
3459 rvfs_list
[vhno
].rvfs_len
++;
3460 mutex_exit(&rvfs_list
[vhno
].rvfs_lock
);
3465 vfs_hash_remove(struct vfs
*vfsp
)
3471 ASSERT(RW_WRITE_HELD(&vfslist
));
3473 dev
= expldev(vfsp
->vfs_fsid
.val
[0]);
3474 vhno
= VFSHASH(getmajor(dev
), getminor(dev
));
3476 mutex_enter(&rvfs_list
[vhno
].rvfs_lock
);
3481 if (rvfs_list
[vhno
].rvfs_head
== vfsp
) {
3482 rvfs_list
[vhno
].rvfs_head
= vfsp
->vfs_hash
;
3483 rvfs_list
[vhno
].rvfs_len
--;
3486 for (tvfsp
= rvfs_list
[vhno
].rvfs_head
; tvfsp
!= NULL
;
3487 tvfsp
= tvfsp
->vfs_hash
) {
3488 if (tvfsp
->vfs_hash
== vfsp
) {
3489 tvfsp
->vfs_hash
= vfsp
->vfs_hash
;
3490 rvfs_list
[vhno
].rvfs_len
--;
3494 cmn_err(CE_WARN
, "vfs_list_remove: vfs not found in hash");
3498 mutex_exit(&rvfs_list
[vhno
].rvfs_lock
);
3503 vfs_list_add(struct vfs
*vfsp
)
3508 * Typically, the vfs_t will have been created on behalf of the file
3509 * system in vfs_init, where it will have been provided with a
3510 * vfs_impl_t. This, however, might be lacking if the vfs_t was created
3511 * by an unbundled file system. We therefore check for such an example
3512 * before stamping the vfs_t with its creation time for the benefit of
3515 if (vfsp
->vfs_implp
== NULL
)
3516 vfsimpl_setup(vfsp
);
3517 vfs_mono_time(&vfsp
->vfs_hrctime
);
3520 * The zone that owns the mount is the one that performed the mount.
3521 * Note that this isn't necessarily the same as the zone mounted into.
3522 * The corresponding zone_rele_ref() will be done when the vfs_t
3525 vfsp
->vfs_zone
= curproc
->p_zone
;
3526 zone_init_ref(&vfsp
->vfs_implp
->vi_zone_ref
);
3527 zone_hold_ref(vfsp
->vfs_zone
, &vfsp
->vfs_implp
->vi_zone_ref
,
3531 * Find the zone mounted into, and put this mount on its vfs list.
3533 zone
= zone_find_by_path(refstr_value(vfsp
->vfs_mntpt
));
3534 ASSERT(zone
!= NULL
);
3536 * Special casing for the root vfs. This structure is allocated
3537 * statically and hooked onto rootvfs at link time. During the
3538 * vfs_mountroot call at system startup time, the root file system's
3539 * VFS_MOUNTROOT routine will call vfs_add with this root vfs struct
3540 * as argument. The code below must detect and handle this special
3541 * case. The only apparent justification for this special casing is
3542 * to ensure that the root file system appears at the head of the
3545 * XXX: I'm assuming that it's ok to do normal list locking when
3546 * adding the entry for the root file system (this used to be
3547 * done with no locks held).
3551 * Link into the vfs list proper.
3553 if (vfsp
== &root
) {
3555 * Assert: This vfs is already on the list as its first entry.
3556 * Thus, there's nothing to do.
3558 ASSERT(rootvfs
== vfsp
);
3560 * Add it to the head of the global zone's vfslist.
3562 ASSERT(zone
== global_zone
);
3563 ASSERT(zone
->zone_vfslist
== NULL
);
3564 zone
->zone_vfslist
= vfsp
;
3567 * Link to end of list using vfs_prev (as rootvfs is now a
3568 * doubly linked circular list) so list is in mount order for
3571 rootvfs
->vfs_prev
->vfs_next
= vfsp
;
3572 vfsp
->vfs_prev
= rootvfs
->vfs_prev
;
3573 rootvfs
->vfs_prev
= vfsp
;
3574 vfsp
->vfs_next
= rootvfs
;
3577 * Do it again for the zone-private list (which may be NULL).
3579 if (zone
->zone_vfslist
== NULL
) {
3580 ASSERT(zone
!= global_zone
);
3581 zone
->zone_vfslist
= vfsp
;
3583 zone
->zone_vfslist
->vfs_zone_prev
->vfs_zone_next
= vfsp
;
3584 vfsp
->vfs_zone_prev
= zone
->zone_vfslist
->vfs_zone_prev
;
3585 zone
->zone_vfslist
->vfs_zone_prev
= vfsp
;
3586 vfsp
->vfs_zone_next
= zone
->zone_vfslist
;
3591 * Link into the hash table, inserting it at the end, so that LOFS
3592 * with the same fsid as UFS (or other) file systems will not hide
3595 vfs_hash_add(vfsp
, 0);
3598 * update the mnttab modification time
3600 vfs_mnttab_modtimeupd();
3606 vfs_list_remove(struct vfs
*vfsp
)
3610 zone
= zone_find_by_path(refstr_value(vfsp
->vfs_mntpt
));
3611 ASSERT(zone
!= NULL
);
3613 * Callers are responsible for preventing attempts to unmount the
3616 ASSERT(vfsp
!= rootvfs
);
3623 vfs_hash_remove(vfsp
);
3626 * Remove from vfs list.
3628 vfsp
->vfs_prev
->vfs_next
= vfsp
->vfs_next
;
3629 vfsp
->vfs_next
->vfs_prev
= vfsp
->vfs_prev
;
3630 vfsp
->vfs_next
= vfsp
->vfs_prev
= NULL
;
3633 * Remove from zone-specific vfs list.
3635 if (zone
->zone_vfslist
== vfsp
)
3636 zone
->zone_vfslist
= vfsp
->vfs_zone_next
;
3638 if (vfsp
->vfs_zone_next
== vfsp
) {
3639 ASSERT(vfsp
->vfs_zone_prev
== vfsp
);
3640 ASSERT(zone
->zone_vfslist
== vfsp
);
3641 zone
->zone_vfslist
= NULL
;
3644 vfsp
->vfs_zone_prev
->vfs_zone_next
= vfsp
->vfs_zone_next
;
3645 vfsp
->vfs_zone_next
->vfs_zone_prev
= vfsp
->vfs_zone_prev
;
3646 vfsp
->vfs_zone_next
= vfsp
->vfs_zone_prev
= NULL
;
3649 * update the mnttab modification time
3651 vfs_mnttab_modtimeupd();
3657 getvfs(fsid_t
*fsid
)
3660 int val0
= fsid
->val
[0];
3661 int val1
= fsid
->val
[1];
3662 dev_t dev
= expldev(val0
);
3663 int vhno
= VFSHASH(getmajor(dev
), getminor(dev
));
3664 kmutex_t
*hmp
= &rvfs_list
[vhno
].rvfs_lock
;
3667 for (vfsp
= rvfs_list
[vhno
].rvfs_head
; vfsp
; vfsp
= vfsp
->vfs_hash
) {
3668 if (vfsp
->vfs_fsid
.val
[0] == val0
&&
3669 vfsp
->vfs_fsid
.val
[1] == val1
) {
3680 * Search the vfs mount in progress list for a specified device/vfs entry.
3681 * Returns 0 if the first entry in the list that the device matches has the
3682 * given vfs pointer as well. If the device matches but a different vfs
3683 * pointer is encountered in the list before the given vfs pointer then
3688 vfs_devmounting(dev_t dev
, struct vfs
*vfsp
)
3693 mutex_enter(&vfs_miplist_mutex
);
3694 for (mipp
= vfs_miplist
; mipp
!= NULL
; mipp
= mipp
->mip_next
) {
3695 if (mipp
->mip_dev
== dev
) {
3696 if (mipp
->mip_vfsp
!= vfsp
)
3701 mutex_exit(&vfs_miplist_mutex
);
3706 * Search the vfs list for a specified device. Returns 1, if entry is found
3707 * or 0 if no suitable entry is found.
3711 vfs_devismounted(dev_t dev
)
3716 vfs_list_read_lock();
3720 if (vfsp
->vfs_dev
== dev
) {
3724 vfsp
= vfsp
->vfs_next
;
3725 } while (vfsp
!= rootvfs
);
3732 * Search the vfs list for a specified device. Returns a pointer to it
3733 * or NULL if no suitable entry is found. The caller of this routine
3734 * is responsible for releasing the returned vfs pointer.
3737 vfs_dev2vfsp(dev_t dev
)
3742 vfs_list_read_lock();
3747 * The following could be made more efficient by making
3748 * the entire loop use vfs_zone_next if the call is from
3749 * a zone. The only callers, however, ustat(2) and
3750 * umount2(2), don't seem to justify the added
3751 * complexity at present.
3753 if (vfsp
->vfs_dev
== dev
&&
3754 ZONE_PATH_VISIBLE(refstr_value(vfsp
->vfs_mntpt
),
3760 vfsp
= vfsp
->vfs_next
;
3761 } while (vfsp
!= rootvfs
);
3763 return (found
? vfsp
: NULL
);
3767 * Search the vfs list for a specified mntpoint. Returns a pointer to it
3768 * or NULL if no suitable entry is found. The caller of this routine
3769 * is responsible for releasing the returned vfs pointer.
3771 * Note that if multiple mntpoints match, the last one matching is
3772 * returned in an attempt to return the "top" mount when overlay
3773 * mounts are covering the same mount point. This is accomplished by starting
3774 * at the end of the list and working our way backwards, stopping at the first
3778 vfs_mntpoint2vfsp(const char *mp
)
3781 struct vfs
*retvfsp
= NULL
;
3782 zone_t
*zone
= curproc
->p_zone
;
3785 vfs_list_read_lock();
3786 if (getzoneid() == GLOBAL_ZONEID
) {
3788 * The global zone may see filesystems in any zone.
3790 vfsp
= rootvfs
->vfs_prev
;
3792 if (strcmp(refstr_value(vfsp
->vfs_mntpt
), mp
) == 0) {
3796 vfsp
= vfsp
->vfs_prev
;
3797 } while (vfsp
!= rootvfs
->vfs_prev
);
3798 } else if ((list
= zone
->zone_vfslist
) != NULL
) {
3801 vfsp
= list
->vfs_zone_prev
;
3803 mntpt
= refstr_value(vfsp
->vfs_mntpt
);
3804 mntpt
= ZONE_PATH_TRANSLATE(mntpt
, zone
);
3805 if (strcmp(mntpt
, mp
) == 0) {
3809 vfsp
= vfsp
->vfs_zone_prev
;
3810 } while (vfsp
!= list
->vfs_zone_prev
);
3819 * Search the vfs list for a specified vfsops.
3820 * if vfs entry is found then return 1, else 0.
3823 vfs_opsinuse(vfsops_t
*ops
)
3828 vfs_list_read_lock();
3832 if (vfs_getops(vfsp
) == ops
) {
3836 vfsp
= vfsp
->vfs_next
;
3837 } while (vfsp
!= rootvfs
);
3843 * Allocate an entry in vfssw for a file system type
3846 allocate_vfssw(const char *type
)
3850 if (type
[0] == '\0' || strlen(type
) + 1 > _ST_FSTYPSZ
) {
3852 * The vfssw table uses the empty string to identify an
3853 * available entry; we cannot add any type which has
3854 * a leading NUL. The string length is limited to
3855 * the size of the st_fstype array in struct stat.
3860 ASSERT(VFSSW_WRITE_LOCKED());
3861 for (vswp
= &vfssw
[1]; vswp
< &vfssw
[nfstype
]; vswp
++)
3862 if (!ALLOCATED_VFSSW(vswp
)) {
3863 vswp
->vsw_name
= kmem_alloc(strlen(type
) + 1, KM_SLEEP
);
3864 (void) strcpy(vswp
->vsw_name
, type
);
3865 ASSERT(vswp
->vsw_count
== 0);
3866 vswp
->vsw_count
= 1;
3867 mutex_init(&vswp
->vsw_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
3874 * Impose additional layer of translation between vfstype names
3875 * and module names in the filesystem.
3878 vfs_to_modname(const char *vfstype
)
3880 if (strcmp(vfstype
, "proc") == 0) {
3882 } else if (strcmp(vfstype
, "fd") == 0) {
3884 } else if (strncmp(vfstype
, "nfs", 3) == 0) {
3892 * Find a vfssw entry given a file system type name.
3893 * Try to autoload the filesystem if it's not found.
3894 * If it's installed, return the vfssw locked to prevent unloading.
3897 vfs_getvfssw(const char *type
)
3900 const char *modname
;
3903 vswp
= vfs_getvfsswbyname(type
);
3904 modname
= vfs_to_modname(type
);
3906 if (rootdir
== NULL
) {
3908 * If we haven't yet loaded the root file system, then our
3909 * _init won't be called until later. Allocate vfssw entry,
3910 * because mod_installfs won't be called.
3915 if ((vswp
= vfs_getvfsswbyname(type
)) == NULL
) {
3916 if ((vswp
= allocate_vfssw(type
)) == NULL
) {
3924 if (!VFS_INSTALLED(vswp
)) {
3926 (void) modloadonly("fs", modname
);
3933 * Try to load the filesystem. Before calling modload(), we drop
3934 * our lock on the VFS switch table, and pick it up after the
3935 * module is loaded. However, there is a potential race: the
3936 * module could be unloaded after the call to modload() completes
3937 * but before we pick up the lock and drive on. Therefore,
3938 * we keep reloading the module until we've loaded the module
3939 * _and_ we have the lock on the VFS switch table.
3941 while (vswp
== NULL
|| !VFS_INSTALLED(vswp
)) {
3943 if (modload("fs", modname
) == -1)
3947 if ((vswp
= vfs_getvfsswbyname(type
)) == NULL
)
3956 * Find a vfssw entry given a file system type name.
3959 vfs_getvfsswbyname(const char *type
)
3963 ASSERT(VFSSW_LOCKED());
3964 if (type
== NULL
|| *type
== '\0')
3967 for (vswp
= &vfssw
[1]; vswp
< &vfssw
[nfstype
]; vswp
++) {
3968 if (strcmp(type
, vswp
->vsw_name
) == 0) {
3978 * Find a vfssw entry given a set of vfsops.
3981 vfs_getvfsswbyvfsops(vfsops_t
*vfsops
)
3986 for (vswp
= &vfssw
[1]; vswp
< &vfssw
[nfstype
]; vswp
++) {
3987 if (ALLOCATED_VFSSW(vswp
) && &vswp
->vsw_vfsops
== vfsops
) {
3999 * Reference a vfssw entry.
4002 vfs_refvfssw(struct vfssw
*vswp
)
4005 mutex_enter(&vswp
->vsw_lock
);
4007 mutex_exit(&vswp
->vsw_lock
);
4011 * Unreference a vfssw entry.
4014 vfs_unrefvfssw(struct vfssw
*vswp
)
4017 mutex_enter(&vswp
->vsw_lock
);
4019 mutex_exit(&vswp
->vsw_lock
);
4022 static int sync_retries
= 20; /* number of retries when not making progress */
4023 static int sync_triesleft
; /* portion of sync_retries remaining */
4025 static pgcnt_t old_pgcnt
, new_pgcnt
;
4026 static int new_bufcnt
, old_bufcnt
;
4029 * Sync all of the mounted filesystems, and then wait for the actual i/o to
4030 * complete. We wait by counting the number of dirty pages and buffers,
4031 * pushing them out using bio_busy() and page_busy(), and then counting again.
4032 * This routine is used during the uadmin A_SHUTDOWN code. It should only
4033 * be used after some higher-level mechanism has quiesced the system so that
4034 * new writes are not being initiated while we are waiting for completion.
4036 * To ensure finite running time, our algorithm uses sync_triesleft (a progress
4037 * counter used by the vfs_syncall() loop below). It is declared above so
4038 * it can be found easily in the debugger.
4040 * The sync_triesleft counter is updated by vfs_syncall() itself. If we make
4041 * sync_retries consecutive calls to bio_busy() and page_busy() without
4042 * decreasing either the number of dirty buffers or dirty pages below the
4043 * lowest count we have seen so far, we give up and return from vfs_syncall().
4045 * Each loop iteration ends with a call to delay() one second to allow time for
4046 * i/o completion and to permit the user time to read our progress messages.
4051 if (rootdir
== NULL
&& !modrootloaded
)
4052 return; /* no filesystems have been loaded yet */
4054 printf("syncing file systems...");
4057 sync_triesleft
= sync_retries
;
4059 old_bufcnt
= new_bufcnt
= INT_MAX
;
4060 old_pgcnt
= new_pgcnt
= ULONG_MAX
;
4062 while (sync_triesleft
> 0) {
4063 old_bufcnt
= MIN(old_bufcnt
, new_bufcnt
);
4064 old_pgcnt
= MIN(old_pgcnt
, new_pgcnt
);
4066 new_bufcnt
= bio_busy(B_TRUE
);
4067 new_pgcnt
= page_busy(B_TRUE
);
4069 if (new_bufcnt
== 0 && new_pgcnt
== 0)
4072 if (new_bufcnt
< old_bufcnt
|| new_pgcnt
< old_pgcnt
)
4073 sync_triesleft
= sync_retries
;
4078 printf(" [%d]", new_bufcnt
);
4080 printf(" %lu", new_pgcnt
);
4085 if (new_bufcnt
!= 0 || new_pgcnt
!= 0)
4086 printf(" done (not all i/o completed)\n");
4094 * Map VFS flags to statvfs flags. These shouldn't really be separate
4098 vf_to_stf(uint_t vf
)
4102 if (vf
& VFS_RDONLY
)
4104 if (vf
& VFS_NOSETUID
)
4106 if (vf
& VFS_NOTRUNC
)
4113 * Entries for (illegal) fstype 0.
4117 vfsstray_sync(struct vfs
*vfsp
, short arg
, struct cred
*cr
)
4119 cmn_err(CE_PANIC
, "stray vfs operation");
4124 * Entries for (illegal) fstype 0.
4129 cmn_err(CE_PANIC
, "stray vfs operation");
4134 * Support for dealing with forced UFS unmount and its interaction with
4135 * LOFS. Could be used by any filesystem.
4145 * We've gotta define the op for sync separately, since the compiler gets
4146 * confused if we mix and match ANSI and normal style prototypes when
4147 * a "short" argument is present and spits out a warning.
4151 vfs_EIO_sync(struct vfs
*vfsp
, short arg
, struct cred
*cr
)
4157 vfsops_t
*EIO_vfsops
;
4160 * Called from startup() to initialize all loaded vfs's
4167 extern int vopstats_enabled
;
4168 extern void vopstats_startup();
4170 static const fs_operation_def_t EIO_vfsops_template
[] = {
4171 VFSNAME_MOUNT
, { .error
= vfs_EIO
},
4172 VFSNAME_UNMOUNT
, { .error
= vfs_EIO
},
4173 VFSNAME_ROOT
, { .error
= vfs_EIO
},
4174 VFSNAME_STATVFS
, { .error
= vfs_EIO
},
4175 VFSNAME_SYNC
, { .vfs_sync
= vfs_EIO_sync
},
4176 VFSNAME_VGET
, { .error
= vfs_EIO
},
4177 VFSNAME_MOUNTROOT
, { .error
= vfs_EIO
},
4178 VFSNAME_FREEVFS
, { .error
= vfs_EIO
},
4179 VFSNAME_VNSTATE
, { .error
= vfs_EIO
},
4183 static const fs_operation_def_t stray_vfsops_template
[] = {
4184 VFSNAME_MOUNT
, { .error
= vfsstray
},
4185 VFSNAME_UNMOUNT
, { .error
= vfsstray
},
4186 VFSNAME_ROOT
, { .error
= vfsstray
},
4187 VFSNAME_STATVFS
, { .error
= vfsstray
},
4188 VFSNAME_SYNC
, { .vfs_sync
= vfsstray_sync
},
4189 VFSNAME_VGET
, { .error
= vfsstray
},
4190 VFSNAME_MOUNTROOT
, { .error
= vfsstray
},
4191 VFSNAME_FREEVFS
, { .error
= vfsstray
},
4192 VFSNAME_VNSTATE
, { .error
= vfsstray
},
4196 /* Create vfs cache */
4197 vfs_cache
= kmem_cache_create("vfs_cache", sizeof (struct vfs
),
4198 sizeof (uintptr_t), NULL
, NULL
, NULL
, NULL
, NULL
, 0);
4200 /* Initialize the vnode cache (file systems may use it during init). */
4203 /* Setup event monitor framework */
4206 /* Initialize the dummy stray file system type. */
4207 error
= vfs_setfsops(0, stray_vfsops_template
, NULL
);
4209 /* Initialize the dummy EIO file system. */
4210 error
= vfs_makefsops(EIO_vfsops_template
, &EIO_vfsops
);
4212 cmn_err(CE_WARN
, "vfsinit: bad EIO vfs ops template");
4213 /* Shouldn't happen, but not bad enough to panic */
4216 VFS_INIT(&EIO_vfs
, EIO_vfsops
, (caddr_t
)NULL
);
4219 * Default EIO_vfs.vfs_flag to VFS_UNMOUNTED so a lookup
4220 * on this vfs can immediately notice it's invalid.
4222 EIO_vfs
.vfs_flag
|= VFS_UNMOUNTED
;
4225 * Call the init routines of non-loadable filesystems only.
4226 * Filesystems which are loaded as separate modules will be
4227 * initialized by the module loading code instead.
4230 for (vswp
= &vfssw
[1]; vswp
< &vfssw
[nfstype
]; vswp
++) {
4232 if (vswp
->vsw_init
!= NULL
)
4233 (*vswp
->vsw_init
)(vswp
- vfssw
, vswp
->vsw_name
);
4239 if (vopstats_enabled
) {
4240 /* EIO_vfs can collect stats, but we don't retrieve them */
4241 initialize_vopstats(&EIO_vfs
.vfs_vopstats
);
4242 EIO_vfs
.vfs_fstypevsp
= NULL
;
4243 EIO_vfs
.vfs_vskap
= NULL
;
4244 EIO_vfs
.vfs_flag
|= VFS_STATS
;
4249 reparse_point_init();
4253 vfs_alloc(int kmflag
)
4257 vfsp
= kmem_cache_alloc(vfs_cache
, kmflag
);
4260 * Do the simplest initialization here.
4261 * Everything else gets done in vfs_init()
4263 bzero(vfsp
, sizeof (vfs_t
));
4268 vfs_free(vfs_t
*vfsp
)
4271 * One would be tempted to assert that "vfsp->vfs_count == 0".
4272 * The problem is that this gets called out of domount() with
4273 * a partially initialized vfs and a vfs_count of 1. This is
4274 * also called from vfs_rele() with a vfs_count of 0. We can't
4275 * call VFS_RELE() from domount() if VFS_MOUNT() hasn't successfully
4276 * returned. This is because VFS_MOUNT() fully initializes the
4277 * vfs structure and its associated data. VFS_RELE() will call
4278 * VFS_FREEVFS() which may panic the system if the data structures
4279 * aren't fully initialized from a successful VFS_MOUNT()).
4282 /* If FEM was in use, make sure everything gets cleaned up */
4283 if (vfsp
->vfs_femhead
) {
4284 ASSERT(vfsp
->vfs_femhead
->femh_list
== NULL
);
4285 mutex_destroy(&vfsp
->vfs_femhead
->femh_lock
);
4286 kmem_free(vfsp
->vfs_femhead
, sizeof (*(vfsp
->vfs_femhead
)));
4287 vfsp
->vfs_femhead
= NULL
;
4290 if (vfsp
->vfs_implp
)
4291 vfsimpl_teardown(vfsp
);
4292 sema_destroy(&vfsp
->vfs_reflock
);
4293 kmem_cache_free(vfs_cache
, vfsp
);
4297 * Increments the vfs reference count by one atomically.
4300 vfs_hold(vfs_t
*vfsp
)
4302 atomic_inc_32(&vfsp
->vfs_count
);
4303 ASSERT(vfsp
->vfs_count
!= 0);
4307 * Decrements the vfs reference count by one atomically. When
4308 * vfs reference count becomes zero, it calls the file system
4309 * specific vfs_freevfs() to free up the resources.
4312 vfs_rele(vfs_t
*vfsp
)
4314 ASSERT(vfsp
->vfs_count
!= 0);
4315 if (atomic_dec_32_nv(&vfsp
->vfs_count
) == 0) {
4319 zone_rele_ref(&vfsp
->vfs_implp
->vi_zone_ref
,
4321 vfs_freemnttab(vfsp
);
4327 * Generic operations vector support.
4329 * This is used to build operations vectors for both the vfs and vnode.
4330 * It's normally called only when a file system is loaded.
4332 * There are many possible algorithms for this, including the following:
4334 * (1) scan the list of known operations; for each, see if the file system
4335 * includes an entry for it, and fill it in as appropriate.
4337 * (2) set up defaults for all known operations. scan the list of ops
4338 * supplied by the file system; for each which is both supplied and
4339 * known, fill it in.
4341 * (3) sort the lists of known ops & supplied ops; scan the list, filling
4342 * in entries as we go.
4344 * we choose (1) for simplicity, and because performance isn't critical here.
4345 * note that (2) could be sped up using a precomputed hash table on known ops.
4346 * (3) could be faster than either, but only if the lists were very large or
4347 * supplied in sorted order.
4352 fs_build_vector(void *vector
, int *unused_ops
,
4353 const fs_operation_trans_def_t
*translation
,
4354 const fs_operation_def_t
*operations
)
4356 int i
, num_trans
, num_ops
, used
;
4359 * Count the number of translations and the number of supplied
4364 const fs_operation_trans_def_t
*p
;
4366 for (num_trans
= 0, p
= translation
;
4373 const fs_operation_def_t
*p
;
4375 for (num_ops
= 0, p
= operations
;
4381 /* Walk through each operation known to our caller. There will be */
4382 /* one entry in the supplied "translation table" for each. */
4386 for (i
= 0; i
< num_trans
; i
++) {
4389 fs_generic_func_p result
;
4390 fs_generic_func_p
*location
;
4392 curname
= translation
[i
].name
;
4394 /* Look for a matching operation in the list supplied by the */
4399 for (j
= 0; j
< num_ops
; j
++) {
4400 if (strcmp(operations
[j
].name
, curname
) == 0) {
4408 * If the file system is using a "placeholder" for default
4409 * or error functions, grab the appropriate function out of
4410 * the translation table. If the file system didn't supply
4411 * this operation at all, use the default function.
4415 result
= operations
[j
].func
.fs_generic
;
4416 if (result
== fs_default
) {
4417 result
= translation
[i
].defaultFunc
;
4418 } else if (result
== fs_error
) {
4419 result
= translation
[i
].errorFunc
;
4420 } else if (result
== NULL
) {
4421 /* Null values are PROHIBITED */
4425 result
= translation
[i
].defaultFunc
;
4428 /* Now store the function into the operations vector. */
4430 location
= (fs_generic_func_p
*)
4431 (((char *)vector
) + translation
[i
].offset
);
4436 *unused_ops
= num_ops
- used
;
4441 /* Placeholder functions, should never be called. */
4446 cmn_err(CE_PANIC
, "fs_error called");
4453 cmn_err(CE_PANIC
, "fs_default called");
4460 * Part of the implementation of booting off a mirrored root
4461 * involves a change of dev_t for the root device. To
4462 * accomplish this, first remove the existing hash table
4463 * entry for the root device, convert to the new dev_t,
4464 * then re-insert in the hash table at the head of the list.
4467 vfs_root_redev(vfs_t
*vfsp
, dev_t ndev
, int fstype
)
4471 vfs_hash_remove(vfsp
);
4473 vfsp
->vfs_dev
= ndev
;
4474 vfs_make_fsid(&vfsp
->vfs_fsid
, ndev
, fstype
);
4476 vfs_hash_add(vfsp
, 1);
4481 #else /* x86 NEWBOOT */
4484 extern int hvmboot_rootconf();
4487 extern ib_boot_prop_t
*iscsiboot_prop
;
4494 extern void pm_init();
4495 char *fstyp
, *fsmod
;
4498 getrootfs(&fstyp
, &fsmod
);
4502 * hvmboot_rootconf() is defined in the hvm_bootstrap misc module,
4503 * which lives in /platform/i86hvm, and hence is only available when
4504 * booted in an x86 hvm environment. If the hvm_bootstrap misc module
4505 * is not available then the modstub for this function will return 0.
4506 * If the hvm_bootstrap misc module is available it will be loaded
4507 * and hvmboot_rootconf() will be invoked.
4509 if (error
= hvmboot_rootconf())
4513 if (error
= clboot_rootconf())
4516 if (modload("fs", fsmod
) == -1)
4517 panic("Cannot _init %s module", fsmod
);
4520 vsw
= vfs_getvfsswbyname(fstyp
);
4523 cmn_err(CE_CONT
, "Cannot find %s filesystem\n", fstyp
);
4526 VFS_INIT(rootvfs
, &vsw
->vsw_vfsops
, 0);
4529 /* always mount readonly first */
4530 rootvfs
->vfs_flag
|= VFS_RDONLY
;
4534 if (netboot
&& iscsiboot_prop
) {
4535 cmn_err(CE_WARN
, "NFS boot and iSCSI boot"
4536 " shouldn't happen in the same time");
4540 if (netboot
|| iscsiboot_prop
) {
4543 cmn_err(CE_WARN
, "Cannot plumb network device %d", ret
);
4548 if ((ret
== 0) && iscsiboot_prop
) {
4549 ret
= modload("drv", "iscsi");
4550 /* -1 indicates fail */
4552 cmn_err(CE_WARN
, "Failed to load iscsi module");
4553 iscsi_boot_prop_free();
4556 if (!i_ddi_attach_pseudo_node("iscsi")) {
4558 "Failed to attach iscsi driver");
4559 iscsi_boot_prop_free();
4565 error
= VFS_MOUNTROOT(rootvfs
, ROOT_INIT
);
4566 vfs_unrefvfssw(vsw
);
4567 rootdev
= rootvfs
->vfs_dev
;
4570 cmn_err(CE_CONT
, "Cannot mount root on %s fstype %s\n",
4571 rootfs
.bo_name
, fstyp
);
4573 cmn_err(CE_CONT
, "?root on %s fstype %s\n",
4574 rootfs
.bo_name
, fstyp
);
4579 * XXX this is called by nfs only and should probably be removed
4580 * If booted with ASKNAME, prompt on the console for a filesystem
4581 * name and return it.
4584 getfsname(char *askfor
, char *name
, size_t namelen
)
4586 if (boothowto
& RB_ASKNAME
) {
4587 printf("%s name: ", askfor
);
4588 console_gets(name
, namelen
);
4593 * Init the root filesystem type (rootfs.bo_fstype) from the "fstype"
4596 * Filesystem types starting with the prefix "nfs" are diskless clients;
4597 * init the root filename name (rootfs.bo_name), too.
4599 * If we are booting via NFS we currently have these options:
4600 * nfs - dynamically choose NFS V2, V3, or V4 (default)
4601 * nfs2 - force NFS V2
4602 * nfs3 - force NFS V3
4603 * nfs4 - force NFS V4
4604 * Because we need to maintain backward compatibility with the naming
4605 * convention that the NFS V2 filesystem name is "nfs" (see vfs_conf.c)
4606 * we need to map "nfs" => "nfsdyn" and "nfs2" => "nfs". The dynamic
4607 * nfs module will map the type back to either "nfs", "nfs3", or "nfs4".
4608 * This is only for root filesystems, all other uses will expect
4609 * that "nfs" == NFS V2.
4612 getrootfs(char **fstypp
, char **fsmodp
)
4614 char *propstr
= NULL
;
4617 * Check fstype property; for diskless it should be one of "nfs",
4618 * "nfs2", "nfs3" or "nfs4".
4620 if (ddi_prop_lookup_string(DDI_DEV_T_ANY
, ddi_root_node(),
4621 DDI_PROP_DONTPASS
, "fstype", &propstr
)
4623 (void) strncpy(rootfs
.bo_fstype
, propstr
, BO_MAXFSNAME
);
4624 ddi_prop_free(propstr
);
4627 * if the boot property 'fstype' is not set, but 'zfs-bootfs' is set,
4628 * assume the type of this root filesystem is 'zfs'.
4630 } else if (ddi_prop_lookup_string(DDI_DEV_T_ANY
, ddi_root_node(),
4631 DDI_PROP_DONTPASS
, "zfs-bootfs", &propstr
)
4633 (void) strncpy(rootfs
.bo_fstype
, "zfs", BO_MAXFSNAME
);
4634 ddi_prop_free(propstr
);
4637 if (strncmp(rootfs
.bo_fstype
, "nfs", 3) != 0) {
4638 *fstypp
= *fsmodp
= rootfs
.bo_fstype
;
4644 if (strcmp(rootfs
.bo_fstype
, "nfs2") == 0)
4645 (void) strcpy(rootfs
.bo_fstype
, "nfs");
4646 else if (strcmp(rootfs
.bo_fstype
, "nfs") == 0)
4647 (void) strcpy(rootfs
.bo_fstype
, "nfsdyn");
4650 * check if path to network interface is specified in bootpath
4651 * or by a hypervisor domain configuration file.
4652 * XXPV - enable strlumb_get_netdev_path()
4654 if (ddi_prop_exists(DDI_DEV_T_ANY
, ddi_root_node(), DDI_PROP_DONTPASS
,
4656 (void) strcpy(rootfs
.bo_name
, "/xpvd/xnf@0");
4657 } else if (ddi_prop_lookup_string(DDI_DEV_T_ANY
, ddi_root_node(),
4658 DDI_PROP_DONTPASS
, "bootpath", &propstr
)
4660 (void) strncpy(rootfs
.bo_name
, propstr
, BO_MAXOBJNAME
);
4661 ddi_prop_free(propstr
);
4663 rootfs
.bo_name
[0] = '\0';
4665 *fstypp
= rootfs
.bo_fstype
;
4671 * VFS feature routines
4674 #define VFTINDEX(feature) (((feature) >> 32) & 0xFFFFFFFF)
4675 #define VFTBITS(feature) ((feature) & 0xFFFFFFFFLL)
4677 /* Register a feature in the vfs */
4679 vfs_set_feature(vfs_t
*vfsp
, vfs_feature_t feature
)
4681 /* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4682 if (vfsp
->vfs_implp
== NULL
)
4685 vfsp
->vfs_featureset
[VFTINDEX(feature
)] |= VFTBITS(feature
);
4689 vfs_clear_feature(vfs_t
*vfsp
, vfs_feature_t feature
)
4691 /* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4692 if (vfsp
->vfs_implp
== NULL
)
4694 vfsp
->vfs_featureset
[VFTINDEX(feature
)] &= VFTBITS(~feature
);
4698 * Query a vfs for a feature.
4699 * Returns 1 if feature is present, 0 if not
4702 vfs_has_feature(vfs_t
*vfsp
, vfs_feature_t feature
)
4706 /* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4707 if (vfsp
->vfs_implp
== NULL
)
4710 if (vfsp
->vfs_featureset
[VFTINDEX(feature
)] & VFTBITS(feature
))
4717 * Propagate feature set from one vfs to another
4720 vfs_propagate_features(vfs_t
*from
, vfs_t
*to
)
4724 if (to
->vfs_implp
== NULL
|| from
->vfs_implp
== NULL
)
4727 for (i
= 1; i
<= to
->vfs_featureset
[0]; i
++) {
4728 to
->vfs_featureset
[i
] = from
->vfs_featureset
[i
];
4732 #define LOFINODE_PATH "/dev/lofi/%d"
4735 * Return the vnode for the lofi node if there's a lofi mount in place.
4736 * Returns -1 when there's no lofi node, 0 on success, and > 0 on
4740 vfs_get_lofi(vfs_t
*vfsp
, vnode_t
**vpp
)
4746 if (vfsp
->vfs_lofi_id
== 0) {
4751 strsize
= snprintf(NULL
, 0, LOFINODE_PATH
, vfsp
->vfs_lofi_id
);
4752 path
= kmem_alloc(strsize
+ 1, KM_SLEEP
);
4753 (void) snprintf(path
, strsize
+ 1, LOFINODE_PATH
, vfsp
->vfs_lofi_id
);
4756 * We may be inside a zone, so we need to use the /dev path, but
4757 * it's created asynchronously, so we wait here.
4760 err
= lookupname(path
, UIO_SYSSPACE
, FOLLOW
, NULLVPP
, vpp
);
4765 if ((err
= delay_sig(hz
/ 8)) == EINTR
)
4772 kmem_free(path
, strsize
+ 1);