6334 Cannot unlink files when over quota
[illumos-gate.git] / usr / src / uts / common / fs / vfs.c
blob1c19c8bfe2ac21470586316b2e547ada6c64da86
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
33 * All Rights Reserved
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
37 * contributors.
40 #include <sys/types.h>
41 #include <sys/t_lock.h>
42 #include <sys/param.h>
43 #include <sys/errno.h>
44 #include <sys/user.h>
45 #include <sys/fstyp.h>
46 #include <sys/kmem.h>
47 #include <sys/systm.h>
48 #include <sys/proc.h>
49 #include <sys/mount.h>
50 #include <sys/vfs.h>
51 #include <sys/vfs_opreg.h>
52 #include <sys/fem.h>
53 #include <sys/mntent.h>
54 #include <sys/stat.h>
55 #include <sys/statvfs.h>
56 #include <sys/statfs.h>
57 #include <sys/cred.h>
58 #include <sys/vnode.h>
59 #include <sys/rwstlock.h>
60 #include <sys/dnlc.h>
61 #include <sys/file.h>
62 #include <sys/time.h>
63 #include <sys/atomic.h>
64 #include <sys/cmn_err.h>
65 #include <sys/buf.h>
66 #include <sys/swap.h>
67 #include <sys/debug.h>
68 #include <sys/vnode.h>
69 #include <sys/modctl.h>
70 #include <sys/ddi.h>
71 #include <sys/pathname.h>
72 #include <sys/bootconf.h>
73 #include <sys/dumphdr.h>
74 #include <sys/dc_ki.h>
75 #include <sys/poll.h>
76 #include <sys/sunddi.h>
77 #include <sys/sysmacros.h>
78 #include <sys/zone.h>
79 #include <sys/policy.h>
80 #include <sys/ctfs.h>
81 #include <sys/objfs.h>
82 #include <sys/console.h>
83 #include <sys/reboot.h>
84 #include <sys/attr.h>
85 #include <sys/zio.h>
86 #include <sys/spa.h>
87 #include <sys/lofi.h>
88 #include <sys/bootprops.h>
90 #include <vm/page.h>
92 #include <fs/fs_subr.h>
93 /* Private interfaces to create vopstats-related data structures */
94 extern void initialize_vopstats(vopstats_t *);
95 extern vopstats_t *get_fstype_vopstats(struct vfs *, struct vfssw *);
96 extern vsk_anchor_t *get_vskstat_anchor(struct vfs *);
98 static void vfs_clearmntopt_nolock(mntopts_t *, const char *, int);
99 static void vfs_setmntopt_nolock(mntopts_t *, const char *,
100 const char *, int, int);
101 static int vfs_optionisset_nolock(const mntopts_t *, const char *, char **);
102 static void vfs_freemnttab(struct vfs *);
103 static void vfs_freeopt(mntopt_t *);
104 static void vfs_swapopttbl_nolock(mntopts_t *, mntopts_t *);
105 static void vfs_swapopttbl(mntopts_t *, mntopts_t *);
106 static void vfs_copyopttbl_extend(const mntopts_t *, mntopts_t *, int);
107 static void vfs_createopttbl_extend(mntopts_t *, const char *,
108 const mntopts_t *);
109 static char **vfs_copycancelopt_extend(char **const, int);
110 static void vfs_freecancelopt(char **);
111 static void getrootfs(char **, char **);
112 static int getmacpath(dev_info_t *, void *);
113 static void vfs_mnttabvp_setup(void);
115 struct ipmnt {
116 struct ipmnt *mip_next;
117 dev_t mip_dev;
118 struct vfs *mip_vfsp;
121 static kmutex_t vfs_miplist_mutex;
122 static struct ipmnt *vfs_miplist = NULL;
123 static struct ipmnt *vfs_miplist_end = NULL;
125 static kmem_cache_t *vfs_cache; /* Pointer to VFS kmem cache */
128 * VFS global data.
130 vnode_t *rootdir; /* pointer to root inode vnode. */
131 vnode_t *devicesdir; /* pointer to inode of devices root */
132 vnode_t *devdir; /* pointer to inode of dev root */
134 char *server_rootpath; /* root path for diskless clients */
135 char *server_hostname; /* hostname of diskless server */
137 static struct vfs root;
138 static struct vfs devices;
139 static struct vfs dev;
140 struct vfs *rootvfs = &root; /* pointer to root vfs; head of VFS list. */
141 rvfs_t *rvfs_list; /* array of vfs ptrs for vfs hash list */
142 int vfshsz = 512; /* # of heads/locks in vfs hash arrays */
143 /* must be power of 2! */
144 timespec_t vfs_mnttab_ctime; /* mnttab created time */
145 timespec_t vfs_mnttab_mtime; /* mnttab last modified time */
146 char *vfs_dummyfstype = "\0";
147 struct pollhead vfs_pollhd; /* for mnttab pollers */
148 struct vnode *vfs_mntdummyvp; /* to fake mnttab read/write for file events */
149 int mntfstype; /* will be set once mnt fs is mounted */
152 * Table for generic options recognized in the VFS layer and acted
153 * on at this level before parsing file system specific options.
154 * The nosuid option is stronger than any of the devices and setuid
155 * options, so those are canceled when nosuid is seen.
157 * All options which are added here need to be added to the
158 * list of standard options in usr/src/cmd/fs.d/fslib.c as well.
161 * VFS Mount options table
163 static char *ro_cancel[] = { MNTOPT_RW, NULL };
164 static char *rw_cancel[] = { MNTOPT_RO, NULL };
165 static char *suid_cancel[] = { MNTOPT_NOSUID, NULL };
166 static char *nosuid_cancel[] = { MNTOPT_SUID, MNTOPT_DEVICES, MNTOPT_NODEVICES,
167 MNTOPT_NOSETUID, MNTOPT_SETUID, NULL };
168 static char *devices_cancel[] = { MNTOPT_NODEVICES, NULL };
169 static char *nodevices_cancel[] = { MNTOPT_DEVICES, NULL };
170 static char *setuid_cancel[] = { MNTOPT_NOSETUID, NULL };
171 static char *nosetuid_cancel[] = { MNTOPT_SETUID, NULL };
172 static char *nbmand_cancel[] = { MNTOPT_NONBMAND, NULL };
173 static char *nonbmand_cancel[] = { MNTOPT_NBMAND, NULL };
174 static char *exec_cancel[] = { MNTOPT_NOEXEC, NULL };
175 static char *noexec_cancel[] = { MNTOPT_EXEC, NULL };
177 static const mntopt_t mntopts[] = {
179 * option name cancel options default arg flags
181 { MNTOPT_REMOUNT, NULL, NULL,
182 MO_NODISPLAY, (void *)0 },
183 { MNTOPT_RO, ro_cancel, NULL, 0,
184 (void *)0 },
185 { MNTOPT_RW, rw_cancel, NULL, 0,
186 (void *)0 },
187 { MNTOPT_SUID, suid_cancel, NULL, 0,
188 (void *)0 },
189 { MNTOPT_NOSUID, nosuid_cancel, NULL, 0,
190 (void *)0 },
191 { MNTOPT_DEVICES, devices_cancel, NULL, 0,
192 (void *)0 },
193 { MNTOPT_NODEVICES, nodevices_cancel, NULL, 0,
194 (void *)0 },
195 { MNTOPT_SETUID, setuid_cancel, NULL, 0,
196 (void *)0 },
197 { MNTOPT_NOSETUID, nosetuid_cancel, NULL, 0,
198 (void *)0 },
199 { MNTOPT_NBMAND, nbmand_cancel, NULL, 0,
200 (void *)0 },
201 { MNTOPT_NONBMAND, nonbmand_cancel, NULL, 0,
202 (void *)0 },
203 { MNTOPT_EXEC, exec_cancel, NULL, 0,
204 (void *)0 },
205 { MNTOPT_NOEXEC, noexec_cancel, NULL, 0,
206 (void *)0 },
209 const mntopts_t vfs_mntopts = {
210 sizeof (mntopts) / sizeof (mntopt_t),
211 (mntopt_t *)&mntopts[0]
215 * File system operation dispatch functions.
219 fsop_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
221 return (*(vfsp)->vfs_op->vfs_mount)(vfsp, mvp, uap, cr);
225 fsop_unmount(vfs_t *vfsp, int flag, cred_t *cr)
227 return (*(vfsp)->vfs_op->vfs_unmount)(vfsp, flag, cr);
231 fsop_root(vfs_t *vfsp, vnode_t **vpp)
233 refstr_t *mntpt;
234 int ret = (*(vfsp)->vfs_op->vfs_root)(vfsp, vpp);
236 * Make sure this root has a path. With lofs, it is possible to have
237 * a NULL mountpoint.
239 if (ret == 0 && vfsp->vfs_mntpt != NULL && (*vpp)->v_path == NULL) {
240 mntpt = vfs_getmntpoint(vfsp);
241 vn_setpath_str(*vpp, refstr_value(mntpt),
242 strlen(refstr_value(mntpt)));
243 refstr_rele(mntpt);
246 return (ret);
250 fsop_statfs(vfs_t *vfsp, statvfs64_t *sp)
252 return (*(vfsp)->vfs_op->vfs_statvfs)(vfsp, sp);
256 fsop_sync(vfs_t *vfsp, short flag, cred_t *cr)
258 return (*(vfsp)->vfs_op->vfs_sync)(vfsp, flag, cr);
262 fsop_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
265 * In order to handle system attribute fids in a manner
266 * transparent to the underlying fs, we embed the fid for
267 * the sysattr parent object in the sysattr fid and tack on
268 * some extra bytes that only the sysattr layer knows about.
270 * This guarantees that sysattr fids are larger than other fids
271 * for this vfs. If the vfs supports the sysattr view interface
272 * (as indicated by VFSFT_SYSATTR_VIEWS), we cannot have a size
273 * collision with XATTR_FIDSZ.
275 if (vfs_has_feature(vfsp, VFSFT_SYSATTR_VIEWS) &&
276 fidp->fid_len == XATTR_FIDSZ)
277 return (xattr_dir_vget(vfsp, vpp, fidp));
279 return (*(vfsp)->vfs_op->vfs_vget)(vfsp, vpp, fidp);
283 fsop_mountroot(vfs_t *vfsp, enum whymountroot reason)
285 return (*(vfsp)->vfs_op->vfs_mountroot)(vfsp, reason);
288 void
289 fsop_freefs(vfs_t *vfsp)
291 (*(vfsp)->vfs_op->vfs_freevfs)(vfsp);
295 fsop_vnstate(vfs_t *vfsp, vnode_t *vp, vntrans_t nstate)
297 return ((*(vfsp)->vfs_op->vfs_vnstate)(vfsp, vp, nstate));
301 fsop_sync_by_kind(int fstype, short flag, cred_t *cr)
303 ASSERT((fstype >= 0) && (fstype < nfstype));
305 if (ALLOCATED_VFSSW(&vfssw[fstype]) && VFS_INSTALLED(&vfssw[fstype]))
306 return (*vfssw[fstype].vsw_vfsops.vfs_sync) (NULL, flag, cr);
307 else
308 return (ENOTSUP);
312 * File system initialization. vfs_setfsops() must be called from a file
313 * system's init routine.
316 static int
317 fs_copyfsops(const fs_operation_def_t *template, vfsops_t *actual,
318 int *unused_ops)
320 static const fs_operation_trans_def_t vfs_ops_table[] = {
321 VFSNAME_MOUNT, offsetof(vfsops_t, vfs_mount),
322 fs_nosys, fs_nosys,
324 VFSNAME_UNMOUNT, offsetof(vfsops_t, vfs_unmount),
325 fs_nosys, fs_nosys,
327 VFSNAME_ROOT, offsetof(vfsops_t, vfs_root),
328 fs_nosys, fs_nosys,
330 VFSNAME_STATVFS, offsetof(vfsops_t, vfs_statvfs),
331 fs_nosys, fs_nosys,
333 VFSNAME_SYNC, offsetof(vfsops_t, vfs_sync),
334 (fs_generic_func_p) fs_sync,
335 (fs_generic_func_p) fs_sync, /* No errors allowed */
337 VFSNAME_VGET, offsetof(vfsops_t, vfs_vget),
338 fs_nosys, fs_nosys,
340 VFSNAME_MOUNTROOT, offsetof(vfsops_t, vfs_mountroot),
341 fs_nosys, fs_nosys,
343 VFSNAME_FREEVFS, offsetof(vfsops_t, vfs_freevfs),
344 (fs_generic_func_p)fs_freevfs,
345 (fs_generic_func_p)fs_freevfs, /* Shouldn't fail */
347 VFSNAME_VNSTATE, offsetof(vfsops_t, vfs_vnstate),
348 (fs_generic_func_p)fs_nosys,
349 (fs_generic_func_p)fs_nosys,
351 NULL, 0, NULL, NULL
354 return (fs_build_vector(actual, unused_ops, vfs_ops_table, template));
357 void
358 zfs_boot_init() {
360 if (strcmp(rootfs.bo_fstype, MNTTYPE_ZFS) == 0)
361 spa_boot_init();
365 vfs_setfsops(int fstype, const fs_operation_def_t *template, vfsops_t **actual)
367 int error;
368 int unused_ops;
371 * Verify that fstype refers to a valid fs. Note that
372 * 0 is valid since it's used to set "stray" ops.
374 if ((fstype < 0) || (fstype >= nfstype))
375 return (EINVAL);
377 if (!ALLOCATED_VFSSW(&vfssw[fstype]))
378 return (EINVAL);
380 /* Set up the operations vector. */
382 error = fs_copyfsops(template, &vfssw[fstype].vsw_vfsops, &unused_ops);
384 if (error != 0)
385 return (error);
387 vfssw[fstype].vsw_flag |= VSW_INSTALLED;
389 if (actual != NULL)
390 *actual = &vfssw[fstype].vsw_vfsops;
392 #if DEBUG
393 if (unused_ops != 0)
394 cmn_err(CE_WARN, "vfs_setfsops: %s: %d operations supplied "
395 "but not used", vfssw[fstype].vsw_name, unused_ops);
396 #endif
398 return (0);
402 vfs_makefsops(const fs_operation_def_t *template, vfsops_t **actual)
404 int error;
405 int unused_ops;
407 *actual = (vfsops_t *)kmem_alloc(sizeof (vfsops_t), KM_SLEEP);
409 error = fs_copyfsops(template, *actual, &unused_ops);
410 if (error != 0) {
411 kmem_free(*actual, sizeof (vfsops_t));
412 *actual = NULL;
413 return (error);
416 return (0);
420 * Free a vfsops structure created as a result of vfs_makefsops().
421 * NOTE: For a vfsops structure initialized by vfs_setfsops(), use
422 * vfs_freevfsops_by_type().
424 void
425 vfs_freevfsops(vfsops_t *vfsops)
427 kmem_free(vfsops, sizeof (vfsops_t));
431 * Since the vfsops structure is part of the vfssw table and wasn't
432 * really allocated, we're not really freeing anything. We keep
433 * the name for consistency with vfs_freevfsops(). We do, however,
434 * need to take care of a little bookkeeping.
435 * NOTE: For a vfsops structure created by vfs_setfsops(), use
436 * vfs_freevfsops_by_type().
439 vfs_freevfsops_by_type(int fstype)
442 /* Verify that fstype refers to a loaded fs (and not fsid 0). */
443 if ((fstype <= 0) || (fstype >= nfstype))
444 return (EINVAL);
446 WLOCK_VFSSW();
447 if ((vfssw[fstype].vsw_flag & VSW_INSTALLED) == 0) {
448 WUNLOCK_VFSSW();
449 return (EINVAL);
452 vfssw[fstype].vsw_flag &= ~VSW_INSTALLED;
453 WUNLOCK_VFSSW();
455 return (0);
458 /* Support routines used to reference vfs_op */
460 /* Set the operations vector for a vfs */
461 void
462 vfs_setops(vfs_t *vfsp, vfsops_t *vfsops)
464 vfsops_t *op;
466 ASSERT(vfsp != NULL);
467 ASSERT(vfsops != NULL);
469 op = vfsp->vfs_op;
470 membar_consumer();
471 if (vfsp->vfs_femhead == NULL &&
472 atomic_cas_ptr(&vfsp->vfs_op, op, vfsops) == op) {
473 return;
475 fsem_setvfsops(vfsp, vfsops);
478 /* Retrieve the operations vector for a vfs */
479 vfsops_t *
480 vfs_getops(vfs_t *vfsp)
482 vfsops_t *op;
484 ASSERT(vfsp != NULL);
486 op = vfsp->vfs_op;
487 membar_consumer();
488 if (vfsp->vfs_femhead == NULL && op == vfsp->vfs_op) {
489 return (op);
490 } else {
491 return (fsem_getvfsops(vfsp));
496 * Returns non-zero (1) if the vfsops matches that of the vfs.
497 * Returns zero (0) if not.
500 vfs_matchops(vfs_t *vfsp, vfsops_t *vfsops)
502 return (vfs_getops(vfsp) == vfsops);
506 * Returns non-zero (1) if the file system has installed a non-default,
507 * non-error vfs_sync routine. Returns zero (0) otherwise.
510 vfs_can_sync(vfs_t *vfsp)
512 /* vfs_sync() routine is not the default/error function */
513 return (vfs_getops(vfsp)->vfs_sync != fs_sync);
517 * Initialize a vfs structure.
519 void
520 vfs_init(vfs_t *vfsp, vfsops_t *op, void *data)
522 /* Other initialization has been moved to vfs_alloc() */
523 vfsp->vfs_count = 0;
524 vfsp->vfs_next = vfsp;
525 vfsp->vfs_prev = vfsp;
526 vfsp->vfs_zone_next = vfsp;
527 vfsp->vfs_zone_prev = vfsp;
528 vfsp->vfs_lofi_minor = 0;
529 sema_init(&vfsp->vfs_reflock, 1, NULL, SEMA_DEFAULT, NULL);
530 vfsimpl_setup(vfsp);
531 vfsp->vfs_data = (data);
532 vfs_setops((vfsp), (op));
536 * Allocate and initialize the vfs implementation private data
537 * structure, vfs_impl_t.
539 void
540 vfsimpl_setup(vfs_t *vfsp)
542 int i;
544 if (vfsp->vfs_implp != NULL) {
545 return;
548 vfsp->vfs_implp = kmem_alloc(sizeof (vfs_impl_t), KM_SLEEP);
549 /* Note that these are #define'd in vfs.h */
550 vfsp->vfs_vskap = NULL;
551 vfsp->vfs_fstypevsp = NULL;
553 /* Set size of counted array, then zero the array */
554 vfsp->vfs_featureset[0] = VFS_FEATURE_MAXSZ - 1;
555 for (i = 1; i < VFS_FEATURE_MAXSZ; i++) {
556 vfsp->vfs_featureset[i] = 0;
561 * Release the vfs_impl_t structure, if it exists. Some unbundled
562 * filesystems may not use the newer version of vfs and thus
563 * would not contain this implementation private data structure.
565 void
566 vfsimpl_teardown(vfs_t *vfsp)
568 vfs_impl_t *vip = vfsp->vfs_implp;
570 if (vip == NULL)
571 return;
573 kmem_free(vfsp->vfs_implp, sizeof (vfs_impl_t));
574 vfsp->vfs_implp = NULL;
578 * VFS system calls: mount, umount, syssync, statfs, fstatfs, statvfs,
579 * fstatvfs, and sysfs moved to common/syscall.
583 * Update every mounted file system. We call the vfs_sync operation of
584 * each file system type, passing it a NULL vfsp to indicate that all
585 * mounted file systems of that type should be updated.
587 void
588 vfs_sync(int flag)
590 struct vfssw *vswp;
591 RLOCK_VFSSW();
592 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
593 if (ALLOCATED_VFSSW(vswp) && VFS_INSTALLED(vswp)) {
594 vfs_refvfssw(vswp);
595 RUNLOCK_VFSSW();
596 (void) (*vswp->vsw_vfsops.vfs_sync)(NULL, flag,
597 CRED());
598 vfs_unrefvfssw(vswp);
599 RLOCK_VFSSW();
602 RUNLOCK_VFSSW();
605 void
606 sync(void)
608 vfs_sync(0);
612 * External routines.
615 krwlock_t vfssw_lock; /* lock accesses to vfssw */
618 * Lock for accessing the vfs linked list. Initialized in vfs_mountroot(),
619 * but otherwise should be accessed only via vfs_list_lock() and
620 * vfs_list_unlock(). Also used to protect the timestamp for mods to the list.
622 static krwlock_t vfslist;
625 * Mount devfs on /devices. This is done right after root is mounted
626 * to provide device access support for the system
628 static void
629 vfs_mountdevices(void)
631 struct vfssw *vsw;
632 struct vnode *mvp;
633 struct mounta mounta = { /* fake mounta for devfs_mount() */
634 NULL,
635 NULL,
636 MS_SYSSPACE,
637 NULL,
638 NULL,
640 NULL,
645 * _init devfs module to fill in the vfssw
647 if (modload("fs", "devfs") == -1)
648 panic("Cannot _init devfs module");
651 * Hold vfs
653 RLOCK_VFSSW();
654 vsw = vfs_getvfsswbyname("devfs");
655 VFS_INIT(&devices, &vsw->vsw_vfsops, NULL);
656 VFS_HOLD(&devices);
659 * Locate mount point
661 if (lookupname("/devices", UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp))
662 panic("Cannot find /devices");
665 * Perform the mount of /devices
667 if (VFS_MOUNT(&devices, mvp, &mounta, CRED()))
668 panic("Cannot mount /devices");
670 RUNLOCK_VFSSW();
673 * Set appropriate members and add to vfs list for mnttab display
675 vfs_setresource(&devices, "/devices", 0);
676 vfs_setmntpoint(&devices, "/devices", 0);
679 * Hold the root of /devices so it won't go away
681 if (VFS_ROOT(&devices, &devicesdir))
682 panic("vfs_mountdevices: not devices root");
684 if (vfs_lock(&devices) != 0) {
685 VN_RELE(devicesdir);
686 cmn_err(CE_NOTE, "Cannot acquire vfs_lock of /devices");
687 return;
690 if (vn_vfswlock(mvp) != 0) {
691 vfs_unlock(&devices);
692 VN_RELE(devicesdir);
693 cmn_err(CE_NOTE, "Cannot acquire vfswlock of /devices");
694 return;
697 vfs_add(mvp, &devices, 0);
698 vn_vfsunlock(mvp);
699 vfs_unlock(&devices);
700 VN_RELE(devicesdir);
704 * mount the first instance of /dev to root and remain mounted
706 static void
707 vfs_mountdev1(void)
709 struct vfssw *vsw;
710 struct vnode *mvp;
711 struct mounta mounta = { /* fake mounta for sdev_mount() */
712 NULL,
713 NULL,
714 MS_SYSSPACE | MS_OVERLAY,
715 NULL,
716 NULL,
718 NULL,
723 * _init dev module to fill in the vfssw
725 if (modload("fs", "dev") == -1)
726 cmn_err(CE_PANIC, "Cannot _init dev module\n");
729 * Hold vfs
731 RLOCK_VFSSW();
732 vsw = vfs_getvfsswbyname("dev");
733 VFS_INIT(&dev, &vsw->vsw_vfsops, NULL);
734 VFS_HOLD(&dev);
737 * Locate mount point
739 if (lookupname("/dev", UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp))
740 cmn_err(CE_PANIC, "Cannot find /dev\n");
743 * Perform the mount of /dev
745 if (VFS_MOUNT(&dev, mvp, &mounta, CRED()))
746 cmn_err(CE_PANIC, "Cannot mount /dev 1\n");
748 RUNLOCK_VFSSW();
751 * Set appropriate members and add to vfs list for mnttab display
753 vfs_setresource(&dev, "/dev", 0);
754 vfs_setmntpoint(&dev, "/dev", 0);
757 * Hold the root of /dev so it won't go away
759 if (VFS_ROOT(&dev, &devdir))
760 cmn_err(CE_PANIC, "vfs_mountdev1: not dev root");
762 if (vfs_lock(&dev) != 0) {
763 VN_RELE(devdir);
764 cmn_err(CE_NOTE, "Cannot acquire vfs_lock of /dev");
765 return;
768 if (vn_vfswlock(mvp) != 0) {
769 vfs_unlock(&dev);
770 VN_RELE(devdir);
771 cmn_err(CE_NOTE, "Cannot acquire vfswlock of /dev");
772 return;
775 vfs_add(mvp, &dev, 0);
776 vn_vfsunlock(mvp);
777 vfs_unlock(&dev);
778 VN_RELE(devdir);
782 * Mount required filesystem. This is done right after root is mounted.
784 static void
785 vfs_mountfs(char *module, char *spec, char *path)
787 struct vnode *mvp;
788 struct mounta mounta;
789 vfs_t *vfsp;
791 mounta.flags = MS_SYSSPACE | MS_DATA;
792 mounta.fstype = module;
793 mounta.spec = spec;
794 mounta.dir = path;
795 if (lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp)) {
796 cmn_err(CE_WARN, "Cannot find %s", path);
797 return;
799 if (domount(NULL, &mounta, mvp, CRED(), &vfsp))
800 cmn_err(CE_WARN, "Cannot mount %s", path);
801 else
802 VFS_RELE(vfsp);
803 VN_RELE(mvp);
807 * vfs_mountroot is called by main() to mount the root filesystem.
809 void
810 vfs_mountroot(void)
812 struct vnode *rvp = NULL;
813 char *path;
814 size_t plen;
815 struct vfssw *vswp;
816 proc_t *p;
818 rw_init(&vfssw_lock, NULL, RW_DEFAULT, NULL);
819 rw_init(&vfslist, NULL, RW_DEFAULT, NULL);
822 * Alloc the vfs hash bucket array and locks
824 rvfs_list = kmem_zalloc(vfshsz * sizeof (rvfs_t), KM_SLEEP);
827 * Call machine-dependent routine "rootconf" to choose a root
828 * file system type.
830 if (rootconf())
831 panic("vfs_mountroot: cannot mount root");
833 * Get vnode for '/'. Set up rootdir, u.u_rdir and u.u_cdir
834 * to point to it. These are used by lookuppn() so that it
835 * knows where to start from ('/' or '.').
837 vfs_setmntpoint(rootvfs, "/", 0);
838 if (VFS_ROOT(rootvfs, &rootdir))
839 panic("vfs_mountroot: no root vnode");
842 * At this point, the process tree consists of p0 and possibly some
843 * direct children of p0. (i.e. there are no grandchildren)
845 * Walk through them all, setting their current directory.
847 mutex_enter(&pidlock);
848 for (p = practive; p != NULL; p = p->p_next) {
849 ASSERT(p == &p0 || p->p_parent == &p0);
851 PTOU(p)->u_cdir = rootdir;
852 VN_HOLD(PTOU(p)->u_cdir);
853 PTOU(p)->u_rdir = NULL;
855 mutex_exit(&pidlock);
858 * Setup the global zone's rootvp, now that it exists.
860 global_zone->zone_rootvp = rootdir;
861 VN_HOLD(global_zone->zone_rootvp);
864 * Notify the module code that it can begin using the
865 * root filesystem instead of the boot program's services.
867 modrootloaded = 1;
870 * Special handling for a ZFS root file system.
872 zfs_boot_init();
875 * Set up mnttab information for root
877 vfs_setresource(rootvfs, rootfs.bo_name, 0);
880 * Notify cluster software that the root filesystem is available.
882 clboot_mountroot();
884 /* Now that we're all done with the root FS, set up its vopstats */
885 if ((vswp = vfs_getvfsswbyvfsops(vfs_getops(rootvfs))) != NULL) {
886 /* Set flag for statistics collection */
887 if (vswp->vsw_flag & VSW_STATS) {
888 initialize_vopstats(&rootvfs->vfs_vopstats);
889 rootvfs->vfs_flag |= VFS_STATS;
890 rootvfs->vfs_fstypevsp =
891 get_fstype_vopstats(rootvfs, vswp);
892 rootvfs->vfs_vskap = get_vskstat_anchor(rootvfs);
894 vfs_unrefvfssw(vswp);
898 * Mount /devices, /dev instance 1, /system/contract, /etc/mnttab,
899 * /etc/svc/volatile, /etc/dfs/sharetab, /system/object, and /proc.
901 vfs_mountdevices();
902 vfs_mountdev1();
904 vfs_mountfs("ctfs", "ctfs", CTFS_ROOT);
905 vfs_mountfs("proc", "/proc", "/proc");
906 vfs_mountfs("mntfs", "/etc/mnttab", "/etc/mnttab");
907 vfs_mountfs("tmpfs", "/etc/svc/volatile", "/etc/svc/volatile");
908 vfs_mountfs("objfs", "objfs", OBJFS_ROOT);
910 if (getzoneid() == GLOBAL_ZONEID) {
911 vfs_mountfs("sharefs", "sharefs", "/etc/dfs/sharetab");
914 #ifdef __sparc
916 * This bit of magic can go away when we convert sparc to
917 * the new boot architecture based on ramdisk.
919 * Booting off a mirrored root volume:
920 * At this point, we have booted and mounted root on a
921 * single component of the mirror. Complete the boot
922 * by configuring SVM and converting the root to the
923 * dev_t of the mirrored root device. This dev_t conversion
924 * only works because the underlying device doesn't change.
926 if (root_is_svm) {
927 if (svm_rootconf()) {
928 panic("vfs_mountroot: cannot remount root");
932 * mnttab should reflect the new root device
934 vfs_lock_wait(rootvfs);
935 vfs_setresource(rootvfs, rootfs.bo_name, 0);
936 vfs_unlock(rootvfs);
938 #endif /* __sparc */
940 if (strcmp(rootfs.bo_fstype, "zfs") != 0) {
942 * Look up the root device via devfs so that a dv_node is
943 * created for it. The vnode is never VN_RELE()ed.
944 * We allocate more than MAXPATHLEN so that the
945 * buffer passed to i_ddi_prompath_to_devfspath() is
946 * exactly MAXPATHLEN (the function expects a buffer
947 * of that length).
949 plen = strlen("/devices");
950 path = kmem_alloc(plen + MAXPATHLEN, KM_SLEEP);
951 (void) strcpy(path, "/devices");
953 if (i_ddi_prompath_to_devfspath(rootfs.bo_name, path + plen)
954 != DDI_SUCCESS ||
955 lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &rvp)) {
957 /* NUL terminate in case "path" has garbage */
958 path[plen + MAXPATHLEN - 1] = '\0';
959 #ifdef DEBUG
960 cmn_err(CE_WARN, "!Cannot lookup root device: %s",
961 path);
962 #endif
964 kmem_free(path, plen + MAXPATHLEN);
967 vfs_mnttabvp_setup();
971 * Check to see if our "block device" is actually a file. If so,
972 * automatically add a lofi device, and keep track of this fact.
974 static int
975 lofi_add(const char *fsname, struct vfs *vfsp,
976 mntopts_t *mntopts, struct mounta *uap)
978 int fromspace = (uap->flags & MS_SYSSPACE) ?
979 UIO_SYSSPACE : UIO_USERSPACE;
980 struct lofi_ioctl *li = NULL;
981 struct vnode *vp = NULL;
982 struct pathname pn = { NULL };
983 ldi_ident_t ldi_id;
984 ldi_handle_t ldi_hdl;
985 vfssw_t *vfssw;
986 int minor;
987 int err = 0;
989 if ((vfssw = vfs_getvfssw(fsname)) == NULL)
990 return (0);
992 if (!(vfssw->vsw_flag & VSW_CANLOFI)) {
993 vfs_unrefvfssw(vfssw);
994 return (0);
997 vfs_unrefvfssw(vfssw);
998 vfssw = NULL;
1000 if (pn_get(uap->spec, fromspace, &pn) != 0)
1001 return (0);
1003 if (lookupname(uap->spec, fromspace, FOLLOW, NULL, &vp) != 0)
1004 goto out;
1006 if (vp->v_type != VREG)
1007 goto out;
1009 /* OK, this is a lofi mount. */
1011 if ((uap->flags & (MS_REMOUNT|MS_GLOBAL)) ||
1012 vfs_optionisset_nolock(mntopts, MNTOPT_SUID, NULL) ||
1013 vfs_optionisset_nolock(mntopts, MNTOPT_SETUID, NULL) ||
1014 vfs_optionisset_nolock(mntopts, MNTOPT_DEVICES, NULL)) {
1015 err = EINVAL;
1016 goto out;
1019 ldi_id = ldi_ident_from_anon();
1020 li = kmem_zalloc(sizeof (*li), KM_SLEEP);
1021 (void) strlcpy(li->li_filename, pn.pn_path, MAXPATHLEN);
1023 err = ldi_open_by_name("/dev/lofictl", FREAD | FWRITE, kcred,
1024 &ldi_hdl, ldi_id);
1026 if (err)
1027 goto out2;
1029 err = ldi_ioctl(ldi_hdl, LOFI_MAP_FILE, (intptr_t)li,
1030 FREAD | FWRITE | FKIOCTL, kcred, &minor);
1032 (void) ldi_close(ldi_hdl, FREAD | FWRITE, kcred);
1034 if (!err)
1035 vfsp->vfs_lofi_minor = minor;
1037 out2:
1038 ldi_ident_release(ldi_id);
1039 out:
1040 if (li != NULL)
1041 kmem_free(li, sizeof (*li));
1042 if (vp != NULL)
1043 VN_RELE(vp);
1044 pn_free(&pn);
1045 return (err);
1048 static void
1049 lofi_remove(struct vfs *vfsp)
1051 struct lofi_ioctl *li = NULL;
1052 ldi_ident_t ldi_id;
1053 ldi_handle_t ldi_hdl;
1054 int err;
1056 if (vfsp->vfs_lofi_minor == 0)
1057 return;
1059 ldi_id = ldi_ident_from_anon();
1061 li = kmem_zalloc(sizeof (*li), KM_SLEEP);
1062 li->li_minor = vfsp->vfs_lofi_minor;
1063 li->li_cleanup = B_TRUE;
1065 err = ldi_open_by_name("/dev/lofictl", FREAD | FWRITE, kcred,
1066 &ldi_hdl, ldi_id);
1068 if (err)
1069 goto out;
1071 err = ldi_ioctl(ldi_hdl, LOFI_UNMAP_FILE_MINOR, (intptr_t)li,
1072 FREAD | FWRITE | FKIOCTL, kcred, NULL);
1074 (void) ldi_close(ldi_hdl, FREAD | FWRITE, kcred);
1076 if (!err)
1077 vfsp->vfs_lofi_minor = 0;
1079 out:
1080 ldi_ident_release(ldi_id);
1081 if (li != NULL)
1082 kmem_free(li, sizeof (*li));
1086 * Common mount code. Called from the system call entry point, from autofs,
1087 * nfsv4 trigger mounts, and from pxfs.
1089 * Takes the effective file system type, mount arguments, the mount point
1090 * vnode, flags specifying whether the mount is a remount and whether it
1091 * should be entered into the vfs list, and credentials. Fills in its vfspp
1092 * parameter with the mounted file system instance's vfs.
1094 * Note that the effective file system type is specified as a string. It may
1095 * be null, in which case it's determined from the mount arguments, and may
1096 * differ from the type specified in the mount arguments; this is a hook to
1097 * allow interposition when instantiating file system instances.
1099 * The caller is responsible for releasing its own hold on the mount point
1100 * vp (this routine does its own hold when necessary).
1101 * Also note that for remounts, the mount point vp should be the vnode for
1102 * the root of the file system rather than the vnode that the file system
1103 * is mounted on top of.
1106 domount(char *fsname, struct mounta *uap, vnode_t *vp, struct cred *credp,
1107 struct vfs **vfspp)
1109 struct vfssw *vswp;
1110 vfsops_t *vfsops;
1111 struct vfs *vfsp;
1112 struct vnode *bvp;
1113 dev_t bdev = 0;
1114 mntopts_t mnt_mntopts;
1115 int error = 0;
1116 int copyout_error = 0;
1117 int ovflags;
1118 char *opts = uap->optptr;
1119 char *inargs = opts;
1120 int optlen = uap->optlen;
1121 int remount;
1122 int rdonly;
1123 int nbmand = 0;
1124 int delmip = 0;
1125 int addmip = 0;
1126 int splice = ((uap->flags & MS_NOSPLICE) == 0);
1127 int fromspace = (uap->flags & MS_SYSSPACE) ?
1128 UIO_SYSSPACE : UIO_USERSPACE;
1129 char *resource = NULL, *mountpt = NULL;
1130 refstr_t *oldresource, *oldmntpt;
1131 struct pathname pn, rpn;
1132 vsk_anchor_t *vskap;
1133 char fstname[FSTYPSZ];
1134 zone_t *zone;
1137 * The v_flag value for the mount point vp is permanently set
1138 * to VVFSLOCK so that no one bypasses the vn_vfs*locks routine
1139 * for mount point locking.
1141 mutex_enter(&vp->v_lock);
1142 vp->v_flag |= VVFSLOCK;
1143 mutex_exit(&vp->v_lock);
1145 mnt_mntopts.mo_count = 0;
1147 * Find the ops vector to use to invoke the file system-specific mount
1148 * method. If the fsname argument is non-NULL, use it directly.
1149 * Otherwise, dig the file system type information out of the mount
1150 * arguments.
1152 * A side effect is to hold the vfssw entry.
1154 * Mount arguments can be specified in several ways, which are
1155 * distinguished by flag bit settings. The preferred way is to set
1156 * MS_OPTIONSTR, indicating an 8 argument mount with the file system
1157 * type supplied as a character string and the last two arguments
1158 * being a pointer to a character buffer and the size of the buffer.
1159 * On entry, the buffer holds a null terminated list of options; on
1160 * return, the string is the list of options the file system
1161 * recognized. If MS_DATA is set arguments five and six point to a
1162 * block of binary data which the file system interprets.
1163 * A further wrinkle is that some callers don't set MS_FSS and MS_DATA
1164 * consistently with these conventions. To handle them, we check to
1165 * see whether the pointer to the file system name has a numeric value
1166 * less than 256. If so, we treat it as an index.
1168 if (fsname != NULL) {
1169 if ((vswp = vfs_getvfssw(fsname)) == NULL) {
1170 return (EINVAL);
1172 } else if (uap->flags & (MS_OPTIONSTR | MS_DATA | MS_FSS)) {
1173 size_t n;
1174 uint_t fstype;
1176 fsname = fstname;
1178 if ((fstype = (uintptr_t)uap->fstype) < 256) {
1179 RLOCK_VFSSW();
1180 if (fstype == 0 || fstype >= nfstype ||
1181 !ALLOCATED_VFSSW(&vfssw[fstype])) {
1182 RUNLOCK_VFSSW();
1183 return (EINVAL);
1185 (void) strcpy(fsname, vfssw[fstype].vsw_name);
1186 RUNLOCK_VFSSW();
1187 if ((vswp = vfs_getvfssw(fsname)) == NULL)
1188 return (EINVAL);
1189 } else {
1191 * Handle either kernel or user address space.
1193 if (uap->flags & MS_SYSSPACE) {
1194 error = copystr(uap->fstype, fsname,
1195 FSTYPSZ, &n);
1196 } else {
1197 error = copyinstr(uap->fstype, fsname,
1198 FSTYPSZ, &n);
1200 if (error) {
1201 if (error == ENAMETOOLONG)
1202 return (EINVAL);
1203 return (error);
1205 if ((vswp = vfs_getvfssw(fsname)) == NULL)
1206 return (EINVAL);
1208 } else {
1209 if ((vswp = vfs_getvfsswbyvfsops(vfs_getops(rootvfs))) == NULL)
1210 return (EINVAL);
1211 fsname = vswp->vsw_name;
1213 if (!VFS_INSTALLED(vswp))
1214 return (EINVAL);
1216 if ((error = secpolicy_fs_allowed_mount(fsname)) != 0) {
1217 vfs_unrefvfssw(vswp);
1218 return (error);
1221 vfsops = &vswp->vsw_vfsops;
1223 vfs_copyopttbl(&vswp->vsw_optproto, &mnt_mntopts);
1225 * Fetch mount options and parse them for generic vfs options
1227 if (uap->flags & MS_OPTIONSTR) {
1229 * Limit the buffer size
1231 if (optlen < 0 || optlen > MAX_MNTOPT_STR) {
1232 error = EINVAL;
1233 goto errout;
1235 if ((uap->flags & MS_SYSSPACE) == 0) {
1236 inargs = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
1237 inargs[0] = '\0';
1238 if (optlen) {
1239 error = copyinstr(opts, inargs, (size_t)optlen,
1240 NULL);
1241 if (error) {
1242 goto errout;
1246 vfs_parsemntopts(&mnt_mntopts, inargs, 0);
1249 * Flag bits override the options string.
1251 if (uap->flags & MS_REMOUNT)
1252 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_REMOUNT, NULL, 0, 0);
1253 if (uap->flags & MS_RDONLY)
1254 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_RO, NULL, 0, 0);
1255 if (uap->flags & MS_NOSUID)
1256 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL, 0, 0);
1259 * Check if this is a remount; must be set in the option string and
1260 * the file system must support a remount option.
1262 if (remount = vfs_optionisset_nolock(&mnt_mntopts,
1263 MNTOPT_REMOUNT, NULL)) {
1264 if (!(vswp->vsw_flag & VSW_CANREMOUNT)) {
1265 error = ENOTSUP;
1266 goto errout;
1268 uap->flags |= MS_REMOUNT;
1272 * uap->flags and vfs_optionisset() should agree.
1274 if (rdonly = vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_RO, NULL)) {
1275 uap->flags |= MS_RDONLY;
1277 if (vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL)) {
1278 uap->flags |= MS_NOSUID;
1280 nbmand = vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_NBMAND, NULL);
1281 ASSERT(splice || !remount);
1283 * If we are splicing the fs into the namespace,
1284 * perform mount point checks.
1286 * We want to resolve the path for the mount point to eliminate
1287 * '.' and ".." and symlinks in mount points; we can't do the
1288 * same for the resource string, since it would turn
1289 * "/dev/dsk/c0t0d0s0" into "/devices/pci@...". We need to do
1290 * this before grabbing vn_vfswlock(), because otherwise we
1291 * would deadlock with lookuppn().
1293 if (splice) {
1294 ASSERT(vp->v_count > 0);
1297 * Pick up mount point and device from appropriate space.
1299 if (pn_get(uap->spec, fromspace, &pn) == 0) {
1300 resource = kmem_alloc(pn.pn_pathlen + 1,
1301 KM_SLEEP);
1302 (void) strcpy(resource, pn.pn_path);
1303 pn_free(&pn);
1306 * Do a lookupname prior to taking the
1307 * writelock. Mark this as completed if
1308 * successful for later cleanup and addition to
1309 * the mount in progress table.
1311 if ((uap->flags & MS_GLOBAL) == 0 &&
1312 lookupname(uap->spec, fromspace,
1313 FOLLOW, NULL, &bvp) == 0) {
1314 addmip = 1;
1317 if ((error = pn_get(uap->dir, fromspace, &pn)) == 0) {
1318 pathname_t *pnp;
1320 if (*pn.pn_path != '/') {
1321 error = EINVAL;
1322 pn_free(&pn);
1323 goto errout;
1325 pn_alloc(&rpn);
1327 * Kludge to prevent autofs from deadlocking with
1328 * itself when it calls domount().
1330 * If autofs is calling, it is because it is doing
1331 * (autofs) mounts in the process of an NFS mount. A
1332 * lookuppn() here would cause us to block waiting for
1333 * said NFS mount to complete, which can't since this
1334 * is the thread that was supposed to doing it.
1336 if (fromspace == UIO_USERSPACE) {
1337 if ((error = lookuppn(&pn, &rpn, FOLLOW, NULL,
1338 NULL)) == 0) {
1339 pnp = &rpn;
1340 } else {
1342 * The file disappeared or otherwise
1343 * became inaccessible since we opened
1344 * it; might as well fail the mount
1345 * since the mount point is no longer
1346 * accessible.
1348 pn_free(&rpn);
1349 pn_free(&pn);
1350 goto errout;
1352 } else {
1353 pnp = &pn;
1355 mountpt = kmem_alloc(pnp->pn_pathlen + 1, KM_SLEEP);
1356 (void) strcpy(mountpt, pnp->pn_path);
1359 * If the addition of the zone's rootpath
1360 * would push us over a total path length
1361 * of MAXPATHLEN, we fail the mount with
1362 * ENAMETOOLONG, which is what we would have
1363 * gotten if we were trying to perform the same
1364 * mount in the global zone.
1366 * strlen() doesn't count the trailing
1367 * '\0', but zone_rootpathlen counts both a
1368 * trailing '/' and the terminating '\0'.
1370 if ((curproc->p_zone->zone_rootpathlen - 1 +
1371 strlen(mountpt)) > MAXPATHLEN ||
1372 (resource != NULL &&
1373 (curproc->p_zone->zone_rootpathlen - 1 +
1374 strlen(resource)) > MAXPATHLEN)) {
1375 error = ENAMETOOLONG;
1378 pn_free(&rpn);
1379 pn_free(&pn);
1382 if (error)
1383 goto errout;
1386 * Prevent path name resolution from proceeding past
1387 * the mount point.
1389 if (vn_vfswlock(vp) != 0) {
1390 error = EBUSY;
1391 goto errout;
1395 * Verify that it's legitimate to establish a mount on
1396 * the prospective mount point.
1398 if (vn_mountedvfs(vp) != NULL) {
1400 * The mount point lock was obtained after some
1401 * other thread raced through and established a mount.
1403 vn_vfsunlock(vp);
1404 error = EBUSY;
1405 goto errout;
1407 if (vp->v_flag & VNOMOUNT) {
1408 vn_vfsunlock(vp);
1409 error = EINVAL;
1410 goto errout;
1413 if ((uap->flags & (MS_DATA | MS_OPTIONSTR)) == 0) {
1414 uap->dataptr = NULL;
1415 uap->datalen = 0;
1419 * If this is a remount, we don't want to create a new VFS.
1420 * Instead, we pass the existing one with a remount flag.
1422 if (remount) {
1424 * Confirm that the mount point is the root vnode of the
1425 * file system that is being remounted.
1426 * This can happen if the user specifies a different
1427 * mount point directory pathname in the (re)mount command.
1429 * Code below can only be reached if splice is true, so it's
1430 * safe to do vn_vfsunlock() here.
1432 if ((vp->v_flag & VROOT) == 0) {
1433 vn_vfsunlock(vp);
1434 error = ENOENT;
1435 goto errout;
1438 * Disallow making file systems read-only unless file system
1439 * explicitly allows it in its vfssw. Ignore other flags.
1441 if (rdonly && vn_is_readonly(vp) == 0 &&
1442 (vswp->vsw_flag & VSW_CANRWRO) == 0) {
1443 vn_vfsunlock(vp);
1444 error = EINVAL;
1445 goto errout;
1448 * Disallow changing the NBMAND disposition of the file
1449 * system on remounts.
1451 if ((nbmand && ((vp->v_vfsp->vfs_flag & VFS_NBMAND) == 0)) ||
1452 (!nbmand && (vp->v_vfsp->vfs_flag & VFS_NBMAND))) {
1453 vn_vfsunlock(vp);
1454 error = EINVAL;
1455 goto errout;
1457 vfsp = vp->v_vfsp;
1458 ovflags = vfsp->vfs_flag;
1459 vfsp->vfs_flag |= VFS_REMOUNT;
1460 vfsp->vfs_flag &= ~VFS_RDONLY;
1461 } else {
1462 vfsp = vfs_alloc(KM_SLEEP);
1463 VFS_INIT(vfsp, vfsops, NULL);
1466 VFS_HOLD(vfsp);
1468 if ((error = lofi_add(fsname, vfsp, &mnt_mntopts, uap)) != 0) {
1469 if (!remount) {
1470 if (splice)
1471 vn_vfsunlock(vp);
1472 vfs_free(vfsp);
1473 } else {
1474 vn_vfsunlock(vp);
1475 VFS_RELE(vfsp);
1477 goto errout;
1481 * PRIV_SYS_MOUNT doesn't mean you can become root.
1483 if (vfsp->vfs_lofi_minor != 0) {
1484 uap->flags |= MS_NOSUID;
1485 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL, 0, 0);
1489 * The vfs_reflock is not used anymore the code below explicitly
1490 * holds it preventing others accesing it directly.
1492 if ((sema_tryp(&vfsp->vfs_reflock) == 0) &&
1493 !(vfsp->vfs_flag & VFS_REMOUNT))
1494 cmn_err(CE_WARN,
1495 "mount type %s couldn't get vfs_reflock", vswp->vsw_name);
1498 * Lock the vfs. If this is a remount we want to avoid spurious umount
1499 * failures that happen as a side-effect of fsflush() and other mount
1500 * and unmount operations that might be going on simultaneously and
1501 * may have locked the vfs currently. To not return EBUSY immediately
1502 * here we use vfs_lock_wait() instead vfs_lock() for the remount case.
1504 if (!remount) {
1505 if (error = vfs_lock(vfsp)) {
1506 vfsp->vfs_flag = ovflags;
1508 lofi_remove(vfsp);
1510 if (splice)
1511 vn_vfsunlock(vp);
1512 vfs_free(vfsp);
1513 goto errout;
1515 } else {
1516 vfs_lock_wait(vfsp);
1520 * Add device to mount in progress table, global mounts require special
1521 * handling. It is possible that we have already done the lookupname
1522 * on a spliced, non-global fs. If so, we don't want to do it again
1523 * since we cannot do a lookupname after taking the
1524 * wlock above. This case is for a non-spliced, non-global filesystem.
1526 if (!addmip) {
1527 if ((uap->flags & MS_GLOBAL) == 0 &&
1528 lookupname(uap->spec, fromspace, FOLLOW, NULL, &bvp) == 0) {
1529 addmip = 1;
1533 if (addmip) {
1534 vnode_t *lvp = NULL;
1536 error = vfs_get_lofi(vfsp, &lvp);
1537 if (error > 0) {
1538 lofi_remove(vfsp);
1540 if (splice)
1541 vn_vfsunlock(vp);
1542 vfs_unlock(vfsp);
1544 if (remount) {
1545 VFS_RELE(vfsp);
1546 } else {
1547 vfs_free(vfsp);
1550 goto errout;
1551 } else if (error == -1) {
1552 bdev = bvp->v_rdev;
1553 VN_RELE(bvp);
1554 } else {
1555 bdev = lvp->v_rdev;
1556 VN_RELE(lvp);
1557 VN_RELE(bvp);
1560 vfs_addmip(bdev, vfsp);
1561 addmip = 0;
1562 delmip = 1;
1565 * Invalidate cached entry for the mount point.
1567 if (splice)
1568 dnlc_purge_vp(vp);
1571 * If have an option string but the filesystem doesn't supply a
1572 * prototype options table, create a table with the global
1573 * options and sufficient room to accept all the options in the
1574 * string. Then parse the passed in option string
1575 * accepting all the options in the string. This gives us an
1576 * option table with all the proper cancel properties for the
1577 * global options.
1579 * Filesystems that supply a prototype options table are handled
1580 * earlier in this function.
1582 if (uap->flags & MS_OPTIONSTR) {
1583 if (!(vswp->vsw_flag & VSW_HASPROTO)) {
1584 mntopts_t tmp_mntopts;
1586 tmp_mntopts.mo_count = 0;
1587 vfs_createopttbl_extend(&tmp_mntopts, inargs,
1588 &mnt_mntopts);
1589 vfs_parsemntopts(&tmp_mntopts, inargs, 1);
1590 vfs_swapopttbl_nolock(&mnt_mntopts, &tmp_mntopts);
1591 vfs_freeopttbl(&tmp_mntopts);
1596 * Serialize with zone state transitions.
1597 * See vfs_list_add; zone mounted into is:
1598 * zone_find_by_path(refstr_value(vfsp->vfs_mntpt))
1599 * not the zone doing the mount (curproc->p_zone), but if we're already
1600 * inside a NGZ, then we know what zone we are.
1602 if (INGLOBALZONE(curproc)) {
1603 zone = zone_find_by_path(mountpt);
1604 ASSERT(zone != NULL);
1605 } else {
1606 zone = curproc->p_zone;
1608 * zone_find_by_path does a hold, so do one here too so that
1609 * we can do a zone_rele after mount_completed.
1611 zone_hold(zone);
1613 mount_in_progress(zone);
1615 * Instantiate (or reinstantiate) the file system. If appropriate,
1616 * splice it into the file system name space.
1618 * We want VFS_MOUNT() to be able to override the vfs_resource
1619 * string if necessary (ie, mntfs), and also for a remount to
1620 * change the same (necessary when remounting '/' during boot).
1621 * So we set up vfs_mntpt and vfs_resource to what we think they
1622 * should be, then hand off control to VFS_MOUNT() which can
1623 * override this.
1625 * For safety's sake, when changing vfs_resource or vfs_mntpt of
1626 * a vfs which is on the vfs list (i.e. during a remount), we must
1627 * never set those fields to NULL. Several bits of code make
1628 * assumptions that the fields are always valid.
1630 vfs_swapopttbl(&mnt_mntopts, &vfsp->vfs_mntopts);
1631 if (remount) {
1632 if ((oldresource = vfsp->vfs_resource) != NULL)
1633 refstr_hold(oldresource);
1634 if ((oldmntpt = vfsp->vfs_mntpt) != NULL)
1635 refstr_hold(oldmntpt);
1637 vfs_setresource(vfsp, resource, 0);
1638 vfs_setmntpoint(vfsp, mountpt, 0);
1641 * going to mount on this vnode, so notify.
1643 vnevent_mountedover(vp, NULL);
1644 error = VFS_MOUNT(vfsp, vp, uap, credp);
1646 if (uap->flags & MS_RDONLY)
1647 vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
1648 if (uap->flags & MS_NOSUID)
1649 vfs_setmntopt(vfsp, MNTOPT_NOSUID, NULL, 0);
1650 if (uap->flags & MS_GLOBAL)
1651 vfs_setmntopt(vfsp, MNTOPT_GLOBAL, NULL, 0);
1653 if (error) {
1654 lofi_remove(vfsp);
1656 if (remount) {
1657 /* put back pre-remount options */
1658 vfs_swapopttbl(&mnt_mntopts, &vfsp->vfs_mntopts);
1659 vfs_setmntpoint(vfsp, refstr_value(oldmntpt),
1660 VFSSP_VERBATIM);
1661 if (oldmntpt)
1662 refstr_rele(oldmntpt);
1663 vfs_setresource(vfsp, refstr_value(oldresource),
1664 VFSSP_VERBATIM);
1665 if (oldresource)
1666 refstr_rele(oldresource);
1667 vfsp->vfs_flag = ovflags;
1668 vfs_unlock(vfsp);
1669 VFS_RELE(vfsp);
1670 } else {
1671 vfs_unlock(vfsp);
1672 vfs_freemnttab(vfsp);
1673 vfs_free(vfsp);
1675 } else {
1677 * Set the mount time to now
1679 vfsp->vfs_mtime = ddi_get_time();
1680 if (remount) {
1681 vfsp->vfs_flag &= ~VFS_REMOUNT;
1682 if (oldresource)
1683 refstr_rele(oldresource);
1684 if (oldmntpt)
1685 refstr_rele(oldmntpt);
1686 } else if (splice) {
1688 * Link vfsp into the name space at the mount
1689 * point. Vfs_add() is responsible for
1690 * holding the mount point which will be
1691 * released when vfs_remove() is called.
1693 vfs_add(vp, vfsp, uap->flags);
1694 } else {
1696 * Hold the reference to file system which is
1697 * not linked into the name space.
1699 vfsp->vfs_zone = NULL;
1700 VFS_HOLD(vfsp);
1701 vfsp->vfs_vnodecovered = NULL;
1704 * Set flags for global options encountered
1706 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL))
1707 vfsp->vfs_flag |= VFS_RDONLY;
1708 else
1709 vfsp->vfs_flag &= ~VFS_RDONLY;
1710 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
1711 vfsp->vfs_flag |= (VFS_NOSETUID|VFS_NODEVICES);
1712 } else {
1713 if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
1714 vfsp->vfs_flag |= VFS_NODEVICES;
1715 else
1716 vfsp->vfs_flag &= ~VFS_NODEVICES;
1717 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))
1718 vfsp->vfs_flag |= VFS_NOSETUID;
1719 else
1720 vfsp->vfs_flag &= ~VFS_NOSETUID;
1722 if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL))
1723 vfsp->vfs_flag |= VFS_NBMAND;
1724 else
1725 vfsp->vfs_flag &= ~VFS_NBMAND;
1727 if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL))
1728 vfsp->vfs_flag |= VFS_XATTR;
1729 else
1730 vfsp->vfs_flag &= ~VFS_XATTR;
1732 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL))
1733 vfsp->vfs_flag |= VFS_NOEXEC;
1734 else
1735 vfsp->vfs_flag &= ~VFS_NOEXEC;
1738 * Now construct the output option string of options
1739 * we recognized.
1741 if (uap->flags & MS_OPTIONSTR) {
1742 vfs_list_read_lock();
1743 copyout_error = vfs_buildoptionstr(
1744 &vfsp->vfs_mntopts, inargs, optlen);
1745 vfs_list_unlock();
1746 if (copyout_error == 0 &&
1747 (uap->flags & MS_SYSSPACE) == 0) {
1748 copyout_error = copyoutstr(inargs, opts,
1749 optlen, NULL);
1754 * If this isn't a remount, set up the vopstats before
1755 * anyone can touch this. We only allow spliced file
1756 * systems (file systems which are in the namespace) to
1757 * have the VFS_STATS flag set.
1758 * NOTE: PxFS mounts the underlying file system with
1759 * MS_NOSPLICE set and copies those vfs_flags to its private
1760 * vfs structure. As a result, PxFS should never have
1761 * the VFS_STATS flag or else we might access the vfs
1762 * statistics-related fields prior to them being
1763 * properly initialized.
1765 if (!remount && (vswp->vsw_flag & VSW_STATS) && splice) {
1766 initialize_vopstats(&vfsp->vfs_vopstats);
1768 * We need to set vfs_vskap to NULL because there's
1769 * a chance it won't be set below. This is checked
1770 * in teardown_vopstats() so we can't have garbage.
1772 vfsp->vfs_vskap = NULL;
1773 vfsp->vfs_flag |= VFS_STATS;
1774 vfsp->vfs_fstypevsp = get_fstype_vopstats(vfsp, vswp);
1777 if (vswp->vsw_flag & VSW_XID)
1778 vfsp->vfs_flag |= VFS_XID;
1780 vfs_unlock(vfsp);
1782 mount_completed(zone);
1783 zone_rele(zone);
1784 if (splice)
1785 vn_vfsunlock(vp);
1787 if ((error == 0) && (copyout_error == 0)) {
1788 if (!remount) {
1790 * Don't call get_vskstat_anchor() while holding
1791 * locks since it allocates memory and calls
1792 * VFS_STATVFS(). For NFS, the latter can generate
1793 * an over-the-wire call.
1795 vskap = get_vskstat_anchor(vfsp);
1796 /* Only take the lock if we have something to do */
1797 if (vskap != NULL) {
1798 vfs_lock_wait(vfsp);
1799 if (vfsp->vfs_flag & VFS_STATS) {
1800 vfsp->vfs_vskap = vskap;
1802 vfs_unlock(vfsp);
1805 /* Return vfsp to caller. */
1806 *vfspp = vfsp;
1808 errout:
1809 vfs_freeopttbl(&mnt_mntopts);
1810 if (resource != NULL)
1811 kmem_free(resource, strlen(resource) + 1);
1812 if (mountpt != NULL)
1813 kmem_free(mountpt, strlen(mountpt) + 1);
1815 * It is possible we errored prior to adding to mount in progress
1816 * table. Must free vnode we acquired with successful lookupname.
1818 if (addmip)
1819 VN_RELE(bvp);
1820 if (delmip)
1821 vfs_delmip(vfsp);
1822 ASSERT(vswp != NULL);
1823 vfs_unrefvfssw(vswp);
1824 if (inargs != opts)
1825 kmem_free(inargs, MAX_MNTOPT_STR);
1826 if (copyout_error) {
1827 lofi_remove(vfsp);
1828 VFS_RELE(vfsp);
1829 error = copyout_error;
1831 return (error);
1834 static void
1835 vfs_setpath(
1836 struct vfs *vfsp, /* vfs being updated */
1837 refstr_t **refp, /* Ref-count string to contain the new path */
1838 const char *newpath, /* Path to add to refp (above) */
1839 uint32_t flag) /* flag */
1841 size_t len;
1842 refstr_t *ref;
1843 zone_t *zone = curproc->p_zone;
1844 char *sp;
1845 int have_list_lock = 0;
1847 ASSERT(!VFS_ON_LIST(vfsp) || vfs_lock_held(vfsp));
1850 * New path must be less than MAXPATHLEN because mntfs
1851 * will only display up to MAXPATHLEN bytes. This is currently
1852 * safe, because domount() uses pn_get(), and other callers
1853 * similarly cap the size to fewer than MAXPATHLEN bytes.
1856 ASSERT(strlen(newpath) < MAXPATHLEN);
1858 /* mntfs requires consistency while vfs list lock is held */
1860 if (VFS_ON_LIST(vfsp)) {
1861 have_list_lock = 1;
1862 vfs_list_lock();
1865 if (*refp != NULL)
1866 refstr_rele(*refp);
1869 * If we are in a non-global zone then we prefix the supplied path,
1870 * newpath, with the zone's root path, with two exceptions. The first
1871 * is where we have been explicitly directed to avoid doing so; this
1872 * will be the case following a failed remount, where the path supplied
1873 * will be a saved version which must now be restored. The second
1874 * exception is where newpath is not a pathname but a descriptive name,
1875 * e.g. "procfs".
1877 if (zone == global_zone || (flag & VFSSP_VERBATIM) || *newpath != '/') {
1878 ref = refstr_alloc(newpath);
1879 goto out;
1883 * Truncate the trailing '/' in the zoneroot, and merge
1884 * in the zone's rootpath with the "newpath" (resource
1885 * or mountpoint) passed in.
1887 * The size of the required buffer is thus the size of
1888 * the buffer required for the passed-in newpath
1889 * (strlen(newpath) + 1), plus the size of the buffer
1890 * required to hold zone_rootpath (zone_rootpathlen)
1891 * minus one for one of the now-superfluous NUL
1892 * terminations, minus one for the trailing '/'.
1894 * That gives us:
1896 * (strlen(newpath) + 1) + zone_rootpathlen - 1 - 1
1898 * Which is what we have below.
1901 len = strlen(newpath) + zone->zone_rootpathlen - 1;
1902 sp = kmem_alloc(len, KM_SLEEP);
1905 * Copy everything including the trailing slash, which
1906 * we then overwrite with the NUL character.
1909 (void) strcpy(sp, zone->zone_rootpath);
1910 sp[zone->zone_rootpathlen - 2] = '\0';
1911 (void) strcat(sp, newpath);
1913 ref = refstr_alloc(sp);
1914 kmem_free(sp, len);
1915 out:
1916 *refp = ref;
1918 if (have_list_lock) {
1919 vfs_mnttab_modtimeupd();
1920 vfs_list_unlock();
1925 * Record a mounted resource name in a vfs structure.
1926 * If vfsp is already mounted, caller must hold the vfs lock.
1928 void
1929 vfs_setresource(struct vfs *vfsp, const char *resource, uint32_t flag)
1931 if (resource == NULL || resource[0] == '\0')
1932 resource = VFS_NORESOURCE;
1933 vfs_setpath(vfsp, &vfsp->vfs_resource, resource, flag);
1937 * Record a mount point name in a vfs structure.
1938 * If vfsp is already mounted, caller must hold the vfs lock.
1940 void
1941 vfs_setmntpoint(struct vfs *vfsp, const char *mntpt, uint32_t flag)
1943 if (mntpt == NULL || mntpt[0] == '\0')
1944 mntpt = VFS_NOMNTPT;
1945 vfs_setpath(vfsp, &vfsp->vfs_mntpt, mntpt, flag);
1948 /* Returns the vfs_resource. Caller must call refstr_rele() when finished. */
1950 refstr_t *
1951 vfs_getresource(const struct vfs *vfsp)
1953 refstr_t *resource;
1955 vfs_list_read_lock();
1956 resource = vfsp->vfs_resource;
1957 refstr_hold(resource);
1958 vfs_list_unlock();
1960 return (resource);
1963 /* Returns the vfs_mntpt. Caller must call refstr_rele() when finished. */
1965 refstr_t *
1966 vfs_getmntpoint(const struct vfs *vfsp)
1968 refstr_t *mntpt;
1970 vfs_list_read_lock();
1971 mntpt = vfsp->vfs_mntpt;
1972 refstr_hold(mntpt);
1973 vfs_list_unlock();
1975 return (mntpt);
1979 * Create an empty options table with enough empty slots to hold all
1980 * The options in the options string passed as an argument.
1981 * Potentially prepend another options table.
1983 * Note: caller is responsible for locking the vfs list, if needed,
1984 * to protect mops.
1986 static void
1987 vfs_createopttbl_extend(mntopts_t *mops, const char *opts,
1988 const mntopts_t *mtmpl)
1990 const char *s = opts;
1991 uint_t count;
1993 if (opts == NULL || *opts == '\0') {
1994 count = 0;
1995 } else {
1996 count = 1;
1999 * Count number of options in the string
2001 for (s = strchr(s, ','); s != NULL; s = strchr(s, ',')) {
2002 count++;
2003 s++;
2006 vfs_copyopttbl_extend(mtmpl, mops, count);
2010 * Create an empty options table with enough empty slots to hold all
2011 * The options in the options string passed as an argument.
2013 * This function is *not* for general use by filesystems.
2015 * Note: caller is responsible for locking the vfs list, if needed,
2016 * to protect mops.
2018 void
2019 vfs_createopttbl(mntopts_t *mops, const char *opts)
2021 vfs_createopttbl_extend(mops, opts, NULL);
2026 * Swap two mount options tables
2028 static void
2029 vfs_swapopttbl_nolock(mntopts_t *optbl1, mntopts_t *optbl2)
2031 uint_t tmpcnt;
2032 mntopt_t *tmplist;
2034 tmpcnt = optbl2->mo_count;
2035 tmplist = optbl2->mo_list;
2036 optbl2->mo_count = optbl1->mo_count;
2037 optbl2->mo_list = optbl1->mo_list;
2038 optbl1->mo_count = tmpcnt;
2039 optbl1->mo_list = tmplist;
2042 static void
2043 vfs_swapopttbl(mntopts_t *optbl1, mntopts_t *optbl2)
2045 vfs_list_lock();
2046 vfs_swapopttbl_nolock(optbl1, optbl2);
2047 vfs_mnttab_modtimeupd();
2048 vfs_list_unlock();
2051 static char **
2052 vfs_copycancelopt_extend(char **const moc, int extend)
2054 int i = 0;
2055 int j;
2056 char **result;
2058 if (moc != NULL) {
2059 for (; moc[i] != NULL; i++)
2060 /* count number of options to cancel */;
2063 if (i + extend == 0)
2064 return (NULL);
2066 result = kmem_alloc((i + extend + 1) * sizeof (char *), KM_SLEEP);
2068 for (j = 0; j < i; j++) {
2069 result[j] = kmem_alloc(strlen(moc[j]) + 1, KM_SLEEP);
2070 (void) strcpy(result[j], moc[j]);
2072 for (; j <= i + extend; j++)
2073 result[j] = NULL;
2075 return (result);
2078 static void
2079 vfs_copyopt(const mntopt_t *s, mntopt_t *d)
2081 char *sp, *dp;
2083 d->mo_flags = s->mo_flags;
2084 d->mo_data = s->mo_data;
2085 sp = s->mo_name;
2086 if (sp != NULL) {
2087 dp = kmem_alloc(strlen(sp) + 1, KM_SLEEP);
2088 (void) strcpy(dp, sp);
2089 d->mo_name = dp;
2090 } else {
2091 d->mo_name = NULL; /* should never happen */
2094 d->mo_cancel = vfs_copycancelopt_extend(s->mo_cancel, 0);
2096 sp = s->mo_arg;
2097 if (sp != NULL) {
2098 dp = kmem_alloc(strlen(sp) + 1, KM_SLEEP);
2099 (void) strcpy(dp, sp);
2100 d->mo_arg = dp;
2101 } else {
2102 d->mo_arg = NULL;
2107 * Copy a mount options table, possibly allocating some spare
2108 * slots at the end. It is permissible to copy_extend the NULL table.
2110 static void
2111 vfs_copyopttbl_extend(const mntopts_t *smo, mntopts_t *dmo, int extra)
2113 uint_t i, count;
2114 mntopt_t *motbl;
2117 * Clear out any existing stuff in the options table being initialized
2119 vfs_freeopttbl(dmo);
2120 count = (smo == NULL) ? 0 : smo->mo_count;
2121 if ((count + extra) == 0) /* nothing to do */
2122 return;
2123 dmo->mo_count = count + extra;
2124 motbl = kmem_zalloc((count + extra) * sizeof (mntopt_t), KM_SLEEP);
2125 dmo->mo_list = motbl;
2126 for (i = 0; i < count; i++) {
2127 vfs_copyopt(&smo->mo_list[i], &motbl[i]);
2129 for (i = count; i < count + extra; i++) {
2130 motbl[i].mo_flags = MO_EMPTY;
2135 * Copy a mount options table.
2137 * This function is *not* for general use by filesystems.
2139 * Note: caller is responsible for locking the vfs list, if needed,
2140 * to protect smo and dmo.
2142 void
2143 vfs_copyopttbl(const mntopts_t *smo, mntopts_t *dmo)
2145 vfs_copyopttbl_extend(smo, dmo, 0);
2148 static char **
2149 vfs_mergecancelopts(const mntopt_t *mop1, const mntopt_t *mop2)
2151 int c1 = 0;
2152 int c2 = 0;
2153 char **result;
2154 char **sp1, **sp2, **dp;
2157 * First we count both lists of cancel options.
2158 * If either is NULL or has no elements, we return a copy of
2159 * the other.
2161 if (mop1->mo_cancel != NULL) {
2162 for (; mop1->mo_cancel[c1] != NULL; c1++)
2163 /* count cancel options in mop1 */;
2166 if (c1 == 0)
2167 return (vfs_copycancelopt_extend(mop2->mo_cancel, 0));
2169 if (mop2->mo_cancel != NULL) {
2170 for (; mop2->mo_cancel[c2] != NULL; c2++)
2171 /* count cancel options in mop2 */;
2174 result = vfs_copycancelopt_extend(mop1->mo_cancel, c2);
2176 if (c2 == 0)
2177 return (result);
2180 * When we get here, we've got two sets of cancel options;
2181 * we need to merge the two sets. We know that the result
2182 * array has "c1+c2+1" entries and in the end we might shrink
2183 * it.
2184 * Result now has a copy of the c1 entries from mop1; we'll
2185 * now lookup all the entries of mop2 in mop1 and copy it if
2186 * it is unique.
2187 * This operation is O(n^2) but it's only called once per
2188 * filesystem per duplicate option. This is a situation
2189 * which doesn't arise with the filesystems in ON and
2190 * n is generally 1.
2193 dp = &result[c1];
2194 for (sp2 = mop2->mo_cancel; *sp2 != NULL; sp2++) {
2195 for (sp1 = mop1->mo_cancel; *sp1 != NULL; sp1++) {
2196 if (strcmp(*sp1, *sp2) == 0)
2197 break;
2199 if (*sp1 == NULL) {
2201 * Option *sp2 not found in mop1, so copy it.
2202 * The calls to vfs_copycancelopt_extend()
2203 * guarantee that there's enough room.
2205 *dp = kmem_alloc(strlen(*sp2) + 1, KM_SLEEP);
2206 (void) strcpy(*dp++, *sp2);
2209 if (dp != &result[c1+c2]) {
2210 size_t bytes = (dp - result + 1) * sizeof (char *);
2211 char **nres = kmem_alloc(bytes, KM_SLEEP);
2213 bcopy(result, nres, bytes);
2214 kmem_free(result, (c1 + c2 + 1) * sizeof (char *));
2215 result = nres;
2217 return (result);
2221 * Merge two mount option tables (outer and inner) into one. This is very
2222 * similar to "merging" global variables and automatic variables in C.
2224 * This isn't (and doesn't have to be) fast.
2226 * This function is *not* for general use by filesystems.
2228 * Note: caller is responsible for locking the vfs list, if needed,
2229 * to protect omo, imo & dmo.
2231 void
2232 vfs_mergeopttbl(const mntopts_t *omo, const mntopts_t *imo, mntopts_t *dmo)
2234 uint_t i, count;
2235 mntopt_t *mop, *motbl;
2236 uint_t freeidx;
2239 * First determine how much space we need to allocate.
2241 count = omo->mo_count;
2242 for (i = 0; i < imo->mo_count; i++) {
2243 if (imo->mo_list[i].mo_flags & MO_EMPTY)
2244 continue;
2245 if (vfs_hasopt(omo, imo->mo_list[i].mo_name) == NULL)
2246 count++;
2248 ASSERT(count >= omo->mo_count &&
2249 count <= omo->mo_count + imo->mo_count);
2250 motbl = kmem_alloc(count * sizeof (mntopt_t), KM_SLEEP);
2251 for (i = 0; i < omo->mo_count; i++)
2252 vfs_copyopt(&omo->mo_list[i], &motbl[i]);
2253 freeidx = omo->mo_count;
2254 for (i = 0; i < imo->mo_count; i++) {
2255 if (imo->mo_list[i].mo_flags & MO_EMPTY)
2256 continue;
2257 if ((mop = vfs_hasopt(omo, imo->mo_list[i].mo_name)) != NULL) {
2258 char **newcanp;
2259 uint_t index = mop - omo->mo_list;
2261 newcanp = vfs_mergecancelopts(mop, &motbl[index]);
2263 vfs_freeopt(&motbl[index]);
2264 vfs_copyopt(&imo->mo_list[i], &motbl[index]);
2266 vfs_freecancelopt(motbl[index].mo_cancel);
2267 motbl[index].mo_cancel = newcanp;
2268 } else {
2270 * If it's a new option, just copy it over to the first
2271 * free location.
2273 vfs_copyopt(&imo->mo_list[i], &motbl[freeidx++]);
2276 dmo->mo_count = count;
2277 dmo->mo_list = motbl;
2281 * Functions to set and clear mount options in a mount options table.
2285 * Clear a mount option, if it exists.
2287 * The update_mnttab arg indicates whether mops is part of a vfs that is on
2288 * the vfs list.
2290 static void
2291 vfs_clearmntopt_nolock(mntopts_t *mops, const char *opt, int update_mnttab)
2293 struct mntopt *mop;
2294 uint_t i, count;
2296 ASSERT(!update_mnttab || RW_WRITE_HELD(&vfslist));
2298 count = mops->mo_count;
2299 for (i = 0; i < count; i++) {
2300 mop = &mops->mo_list[i];
2302 if (mop->mo_flags & MO_EMPTY)
2303 continue;
2304 if (strcmp(opt, mop->mo_name))
2305 continue;
2306 mop->mo_flags &= ~MO_SET;
2307 if (mop->mo_arg != NULL) {
2308 kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
2310 mop->mo_arg = NULL;
2311 if (update_mnttab)
2312 vfs_mnttab_modtimeupd();
2313 break;
2317 void
2318 vfs_clearmntopt(struct vfs *vfsp, const char *opt)
2320 int gotlock = 0;
2322 if (VFS_ON_LIST(vfsp)) {
2323 gotlock = 1;
2324 vfs_list_lock();
2326 vfs_clearmntopt_nolock(&vfsp->vfs_mntopts, opt, gotlock);
2327 if (gotlock)
2328 vfs_list_unlock();
2333 * Set a mount option on. If it's not found in the table, it's silently
2334 * ignored. If the option has MO_IGNORE set, it is still set unless the
2335 * VFS_NOFORCEOPT bit is set in the flags. Also, VFS_DISPLAY/VFS_NODISPLAY flag
2336 * bits can be used to toggle the MO_NODISPLAY bit for the option.
2337 * If the VFS_CREATEOPT flag bit is set then the first option slot with
2338 * MO_EMPTY set is created as the option passed in.
2340 * The update_mnttab arg indicates whether mops is part of a vfs that is on
2341 * the vfs list.
2343 static void
2344 vfs_setmntopt_nolock(mntopts_t *mops, const char *opt,
2345 const char *arg, int flags, int update_mnttab)
2347 mntopt_t *mop;
2348 uint_t i, count;
2349 char *sp;
2351 ASSERT(!update_mnttab || RW_WRITE_HELD(&vfslist));
2353 if (flags & VFS_CREATEOPT) {
2354 if (vfs_hasopt(mops, opt) != NULL) {
2355 flags &= ~VFS_CREATEOPT;
2358 count = mops->mo_count;
2359 for (i = 0; i < count; i++) {
2360 mop = &mops->mo_list[i];
2362 if (mop->mo_flags & MO_EMPTY) {
2363 if ((flags & VFS_CREATEOPT) == 0)
2364 continue;
2365 sp = kmem_alloc(strlen(opt) + 1, KM_SLEEP);
2366 (void) strcpy(sp, opt);
2367 mop->mo_name = sp;
2368 if (arg != NULL)
2369 mop->mo_flags = MO_HASVALUE;
2370 else
2371 mop->mo_flags = 0;
2372 } else if (strcmp(opt, mop->mo_name)) {
2373 continue;
2375 if ((mop->mo_flags & MO_IGNORE) && (flags & VFS_NOFORCEOPT))
2376 break;
2377 if (arg != NULL && (mop->mo_flags & MO_HASVALUE) != 0) {
2378 sp = kmem_alloc(strlen(arg) + 1, KM_SLEEP);
2379 (void) strcpy(sp, arg);
2380 } else {
2381 sp = NULL;
2383 if (mop->mo_arg != NULL)
2384 kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
2385 mop->mo_arg = sp;
2386 if (flags & VFS_DISPLAY)
2387 mop->mo_flags &= ~MO_NODISPLAY;
2388 if (flags & VFS_NODISPLAY)
2389 mop->mo_flags |= MO_NODISPLAY;
2390 mop->mo_flags |= MO_SET;
2391 if (mop->mo_cancel != NULL) {
2392 char **cp;
2394 for (cp = mop->mo_cancel; *cp != NULL; cp++)
2395 vfs_clearmntopt_nolock(mops, *cp, 0);
2397 if (update_mnttab)
2398 vfs_mnttab_modtimeupd();
2399 break;
2403 void
2404 vfs_setmntopt(struct vfs *vfsp, const char *opt, const char *arg, int flags)
2406 int gotlock = 0;
2408 if (VFS_ON_LIST(vfsp)) {
2409 gotlock = 1;
2410 vfs_list_lock();
2412 vfs_setmntopt_nolock(&vfsp->vfs_mntopts, opt, arg, flags, gotlock);
2413 if (gotlock)
2414 vfs_list_unlock();
2419 * Add a "tag" option to a mounted file system's options list.
2421 * Note: caller is responsible for locking the vfs list, if needed,
2422 * to protect mops.
2424 static mntopt_t *
2425 vfs_addtag(mntopts_t *mops, const char *tag)
2427 uint_t count;
2428 mntopt_t *mop, *motbl;
2430 count = mops->mo_count + 1;
2431 motbl = kmem_zalloc(count * sizeof (mntopt_t), KM_SLEEP);
2432 if (mops->mo_count) {
2433 size_t len = (count - 1) * sizeof (mntopt_t);
2435 bcopy(mops->mo_list, motbl, len);
2436 kmem_free(mops->mo_list, len);
2438 mops->mo_count = count;
2439 mops->mo_list = motbl;
2440 mop = &motbl[count - 1];
2441 mop->mo_flags = MO_TAG;
2442 mop->mo_name = kmem_alloc(strlen(tag) + 1, KM_SLEEP);
2443 (void) strcpy(mop->mo_name, tag);
2444 return (mop);
2448 * Allow users to set arbitrary "tags" in a vfs's mount options.
2449 * Broader use within the kernel is discouraged.
2452 vfs_settag(uint_t major, uint_t minor, const char *mntpt, const char *tag,
2453 cred_t *cr)
2455 vfs_t *vfsp;
2456 mntopts_t *mops;
2457 mntopt_t *mop;
2458 int found = 0;
2459 dev_t dev = makedevice(major, minor);
2460 int err = 0;
2461 char *buf = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
2464 * Find the desired mounted file system
2466 vfs_list_lock();
2467 vfsp = rootvfs;
2468 do {
2469 if (vfsp->vfs_dev == dev &&
2470 strcmp(mntpt, refstr_value(vfsp->vfs_mntpt)) == 0) {
2471 found = 1;
2472 break;
2474 vfsp = vfsp->vfs_next;
2475 } while (vfsp != rootvfs);
2477 if (!found) {
2478 err = EINVAL;
2479 goto out;
2481 err = secpolicy_fs_config(cr, vfsp);
2482 if (err != 0)
2483 goto out;
2485 mops = &vfsp->vfs_mntopts;
2487 * Add tag if it doesn't already exist
2489 if ((mop = vfs_hasopt(mops, tag)) == NULL) {
2490 int len;
2492 (void) vfs_buildoptionstr(mops, buf, MAX_MNTOPT_STR);
2493 len = strlen(buf);
2494 if (len + strlen(tag) + 2 > MAX_MNTOPT_STR) {
2495 err = ENAMETOOLONG;
2496 goto out;
2498 mop = vfs_addtag(mops, tag);
2500 if ((mop->mo_flags & MO_TAG) == 0) {
2501 err = EINVAL;
2502 goto out;
2504 vfs_setmntopt_nolock(mops, tag, NULL, 0, 1);
2505 out:
2506 vfs_list_unlock();
2507 kmem_free(buf, MAX_MNTOPT_STR);
2508 return (err);
2512 * Allow users to remove arbitrary "tags" in a vfs's mount options.
2513 * Broader use within the kernel is discouraged.
2516 vfs_clrtag(uint_t major, uint_t minor, const char *mntpt, const char *tag,
2517 cred_t *cr)
2519 vfs_t *vfsp;
2520 mntopt_t *mop;
2521 int found = 0;
2522 dev_t dev = makedevice(major, minor);
2523 int err = 0;
2526 * Find the desired mounted file system
2528 vfs_list_lock();
2529 vfsp = rootvfs;
2530 do {
2531 if (vfsp->vfs_dev == dev &&
2532 strcmp(mntpt, refstr_value(vfsp->vfs_mntpt)) == 0) {
2533 found = 1;
2534 break;
2536 vfsp = vfsp->vfs_next;
2537 } while (vfsp != rootvfs);
2539 if (!found) {
2540 err = EINVAL;
2541 goto out;
2543 err = secpolicy_fs_config(cr, vfsp);
2544 if (err != 0)
2545 goto out;
2547 if ((mop = vfs_hasopt(&vfsp->vfs_mntopts, tag)) == NULL) {
2548 err = EINVAL;
2549 goto out;
2551 if ((mop->mo_flags & MO_TAG) == 0) {
2552 err = EINVAL;
2553 goto out;
2555 vfs_clearmntopt_nolock(&vfsp->vfs_mntopts, tag, 1);
2556 out:
2557 vfs_list_unlock();
2558 return (err);
2562 * Function to parse an option string and fill in a mount options table.
2563 * Unknown options are silently ignored. The input option string is modified
2564 * by replacing separators with nulls. If the create flag is set, options
2565 * not found in the table are just added on the fly. The table must have
2566 * an option slot marked MO_EMPTY to add an option on the fly.
2568 * This function is *not* for general use by filesystems.
2570 * Note: caller is responsible for locking the vfs list, if needed,
2571 * to protect mops..
2573 void
2574 vfs_parsemntopts(mntopts_t *mops, char *osp, int create)
2576 char *s = osp, *p, *nextop, *valp, *cp, *ep;
2577 int setflg = VFS_NOFORCEOPT;
2579 if (osp == NULL)
2580 return;
2581 while (*s != '\0') {
2582 p = strchr(s, ','); /* find next option */
2583 if (p == NULL) {
2584 cp = NULL;
2585 p = s + strlen(s);
2586 } else {
2587 cp = p; /* save location of comma */
2588 *p++ = '\0'; /* mark end and point to next option */
2590 nextop = p;
2591 p = strchr(s, '='); /* look for value */
2592 if (p == NULL) {
2593 valp = NULL; /* no value supplied */
2594 } else {
2595 ep = p; /* save location of equals */
2596 *p++ = '\0'; /* end option and point to value */
2597 valp = p;
2600 * set option into options table
2602 if (create)
2603 setflg |= VFS_CREATEOPT;
2604 vfs_setmntopt_nolock(mops, s, valp, setflg, 0);
2605 if (cp != NULL)
2606 *cp = ','; /* restore the comma */
2607 if (valp != NULL)
2608 *ep = '='; /* restore the equals */
2609 s = nextop;
2614 * Function to inquire if an option exists in a mount options table.
2615 * Returns a pointer to the option if it exists, else NULL.
2617 * This function is *not* for general use by filesystems.
2619 * Note: caller is responsible for locking the vfs list, if needed,
2620 * to protect mops.
2622 struct mntopt *
2623 vfs_hasopt(const mntopts_t *mops, const char *opt)
2625 struct mntopt *mop;
2626 uint_t i, count;
2628 count = mops->mo_count;
2629 for (i = 0; i < count; i++) {
2630 mop = &mops->mo_list[i];
2632 if (mop->mo_flags & MO_EMPTY)
2633 continue;
2634 if (strcmp(opt, mop->mo_name) == 0)
2635 return (mop);
2637 return (NULL);
2641 * Function to inquire if an option is set in a mount options table.
2642 * Returns non-zero if set and fills in the arg pointer with a pointer to
2643 * the argument string or NULL if there is no argument string.
2645 static int
2646 vfs_optionisset_nolock(const mntopts_t *mops, const char *opt, char **argp)
2648 struct mntopt *mop;
2649 uint_t i, count;
2651 count = mops->mo_count;
2652 for (i = 0; i < count; i++) {
2653 mop = &mops->mo_list[i];
2655 if (mop->mo_flags & MO_EMPTY)
2656 continue;
2657 if (strcmp(opt, mop->mo_name))
2658 continue;
2659 if ((mop->mo_flags & MO_SET) == 0)
2660 return (0);
2661 if (argp != NULL && (mop->mo_flags & MO_HASVALUE) != 0)
2662 *argp = mop->mo_arg;
2663 return (1);
2665 return (0);
2670 vfs_optionisset(const struct vfs *vfsp, const char *opt, char **argp)
2672 int ret;
2674 vfs_list_read_lock();
2675 ret = vfs_optionisset_nolock(&vfsp->vfs_mntopts, opt, argp);
2676 vfs_list_unlock();
2677 return (ret);
2682 * Construct a comma separated string of the options set in the given
2683 * mount table, return the string in the given buffer. Return non-zero if
2684 * the buffer would overflow.
2686 * This function is *not* for general use by filesystems.
2688 * Note: caller is responsible for locking the vfs list, if needed,
2689 * to protect mp.
2692 vfs_buildoptionstr(const mntopts_t *mp, char *buf, int len)
2694 char *cp;
2695 uint_t i;
2697 buf[0] = '\0';
2698 cp = buf;
2699 for (i = 0; i < mp->mo_count; i++) {
2700 struct mntopt *mop;
2702 mop = &mp->mo_list[i];
2703 if (mop->mo_flags & MO_SET) {
2704 int optlen, comma = 0;
2706 if (buf[0] != '\0')
2707 comma = 1;
2708 optlen = strlen(mop->mo_name);
2709 if (strlen(buf) + comma + optlen + 1 > len)
2710 goto err;
2711 if (comma)
2712 *cp++ = ',';
2713 (void) strcpy(cp, mop->mo_name);
2714 cp += optlen;
2716 * Append option value if there is one
2718 if (mop->mo_arg != NULL) {
2719 int arglen;
2721 arglen = strlen(mop->mo_arg);
2722 if (strlen(buf) + arglen + 2 > len)
2723 goto err;
2724 *cp++ = '=';
2725 (void) strcpy(cp, mop->mo_arg);
2726 cp += arglen;
2730 return (0);
2731 err:
2732 return (EOVERFLOW);
2735 static void
2736 vfs_freecancelopt(char **moc)
2738 if (moc != NULL) {
2739 int ccnt = 0;
2740 char **cp;
2742 for (cp = moc; *cp != NULL; cp++) {
2743 kmem_free(*cp, strlen(*cp) + 1);
2744 ccnt++;
2746 kmem_free(moc, (ccnt + 1) * sizeof (char *));
2750 static void
2751 vfs_freeopt(mntopt_t *mop)
2753 if (mop->mo_name != NULL)
2754 kmem_free(mop->mo_name, strlen(mop->mo_name) + 1);
2756 vfs_freecancelopt(mop->mo_cancel);
2758 if (mop->mo_arg != NULL)
2759 kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
2763 * Free a mount options table
2765 * This function is *not* for general use by filesystems.
2767 * Note: caller is responsible for locking the vfs list, if needed,
2768 * to protect mp.
2770 void
2771 vfs_freeopttbl(mntopts_t *mp)
2773 uint_t i, count;
2775 count = mp->mo_count;
2776 for (i = 0; i < count; i++) {
2777 vfs_freeopt(&mp->mo_list[i]);
2779 if (count) {
2780 kmem_free(mp->mo_list, sizeof (mntopt_t) * count);
2781 mp->mo_count = 0;
2782 mp->mo_list = NULL;
2787 /* ARGSUSED */
2788 static int
2789 vfs_mntdummyread(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cred,
2790 caller_context_t *ct)
2792 return (0);
2795 /* ARGSUSED */
2796 static int
2797 vfs_mntdummywrite(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cred,
2798 caller_context_t *ct)
2800 return (0);
2804 * The dummy vnode is currently used only by file events notification
2805 * module which is just interested in the timestamps.
2807 /* ARGSUSED */
2808 static int
2809 vfs_mntdummygetattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2810 caller_context_t *ct)
2812 bzero(vap, sizeof (vattr_t));
2813 vap->va_type = VREG;
2814 vap->va_nlink = 1;
2815 vap->va_ctime = vfs_mnttab_ctime;
2817 * it is ok to just copy mtime as the time will be monotonically
2818 * increasing.
2820 vap->va_mtime = vfs_mnttab_mtime;
2821 vap->va_atime = vap->va_mtime;
2822 return (0);
2825 static void
2826 vfs_mnttabvp_setup(void)
2828 vnode_t *tvp;
2829 vnodeops_t *vfs_mntdummyvnops;
2830 const fs_operation_def_t mnt_dummyvnodeops_template[] = {
2831 VOPNAME_READ, { .vop_read = vfs_mntdummyread },
2832 VOPNAME_WRITE, { .vop_write = vfs_mntdummywrite },
2833 VOPNAME_GETATTR, { .vop_getattr = vfs_mntdummygetattr },
2834 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
2835 NULL, NULL
2838 if (vn_make_ops("mnttab", mnt_dummyvnodeops_template,
2839 &vfs_mntdummyvnops) != 0) {
2840 cmn_err(CE_WARN, "vfs_mnttabvp_setup: vn_make_ops failed");
2841 /* Shouldn't happen, but not bad enough to panic */
2842 return;
2846 * A global dummy vnode is allocated to represent mntfs files.
2847 * The mntfs file (/etc/mnttab) can be monitored for file events
2848 * and receive an event when mnttab changes. Dummy VOP calls
2849 * will be made on this vnode. The file events notification module
2850 * intercepts this vnode and delivers relevant events.
2852 tvp = vn_alloc(KM_SLEEP);
2853 tvp->v_flag = VNOMOUNT|VNOMAP|VNOSWAP|VNOCACHE;
2854 vn_setops(tvp, vfs_mntdummyvnops);
2855 tvp->v_type = VREG;
2857 * The mnt dummy ops do not reference v_data.
2858 * No other module intercepting this vnode should either.
2859 * Just set it to point to itself.
2861 tvp->v_data = (caddr_t)tvp;
2862 tvp->v_vfsp = rootvfs;
2863 vfs_mntdummyvp = tvp;
2867 * performs fake read/write ops
2869 static void
2870 vfs_mnttab_rwop(int rw)
2872 struct uio uio;
2873 struct iovec iov;
2874 char buf[1];
2876 if (vfs_mntdummyvp == NULL)
2877 return;
2879 bzero(&uio, sizeof (uio));
2880 bzero(&iov, sizeof (iov));
2881 iov.iov_base = buf;
2882 iov.iov_len = 0;
2883 uio.uio_iov = &iov;
2884 uio.uio_iovcnt = 1;
2885 uio.uio_loffset = 0;
2886 uio.uio_segflg = UIO_SYSSPACE;
2887 uio.uio_resid = 0;
2888 if (rw) {
2889 (void) VOP_WRITE(vfs_mntdummyvp, &uio, 0, kcred, NULL);
2890 } else {
2891 (void) VOP_READ(vfs_mntdummyvp, &uio, 0, kcred, NULL);
2896 * Generate a write operation.
2898 void
2899 vfs_mnttab_writeop(void)
2901 vfs_mnttab_rwop(1);
2905 * Generate a read operation.
2907 void
2908 vfs_mnttab_readop(void)
2910 vfs_mnttab_rwop(0);
2914 * Free any mnttab information recorded in the vfs struct.
2915 * The vfs must not be on the vfs list.
2917 static void
2918 vfs_freemnttab(struct vfs *vfsp)
2920 ASSERT(!VFS_ON_LIST(vfsp));
2923 * Free device and mount point information
2925 if (vfsp->vfs_mntpt != NULL) {
2926 refstr_rele(vfsp->vfs_mntpt);
2927 vfsp->vfs_mntpt = NULL;
2929 if (vfsp->vfs_resource != NULL) {
2930 refstr_rele(vfsp->vfs_resource);
2931 vfsp->vfs_resource = NULL;
2934 * Now free mount options information
2936 vfs_freeopttbl(&vfsp->vfs_mntopts);
2940 * Return the last mnttab modification time
2942 void
2943 vfs_mnttab_modtime(timespec_t *ts)
2945 ASSERT(RW_LOCK_HELD(&vfslist));
2946 *ts = vfs_mnttab_mtime;
2950 * See if mnttab is changed
2952 void
2953 vfs_mnttab_poll(timespec_t *old, struct pollhead **phpp)
2955 int changed;
2957 *phpp = (struct pollhead *)NULL;
2960 * Note: don't grab vfs list lock before accessing vfs_mnttab_mtime.
2961 * Can lead to deadlock against vfs_mnttab_modtimeupd(). It is safe
2962 * to not grab the vfs list lock because tv_sec is monotonically
2963 * increasing.
2966 changed = (old->tv_nsec != vfs_mnttab_mtime.tv_nsec) ||
2967 (old->tv_sec != vfs_mnttab_mtime.tv_sec);
2968 if (!changed) {
2969 *phpp = &vfs_pollhd;
2973 /* Provide a unique and monotonically-increasing timestamp. */
2974 void
2975 vfs_mono_time(timespec_t *ts)
2977 static volatile hrtime_t hrt; /* The saved time. */
2978 hrtime_t newhrt, oldhrt; /* For effecting the CAS. */
2979 timespec_t newts;
2982 * Try gethrestime() first, but be prepared to fabricate a sensible
2983 * answer at the first sign of any trouble.
2985 gethrestime(&newts);
2986 newhrt = ts2hrt(&newts);
2987 for (;;) {
2988 oldhrt = hrt;
2989 if (newhrt <= hrt)
2990 newhrt = hrt + 1;
2991 if (atomic_cas_64((uint64_t *)&hrt, oldhrt, newhrt) == oldhrt)
2992 break;
2994 hrt2ts(newhrt, ts);
2998 * Update the mnttab modification time and wake up any waiters for
2999 * mnttab changes
3001 void
3002 vfs_mnttab_modtimeupd()
3004 hrtime_t oldhrt, newhrt;
3006 ASSERT(RW_WRITE_HELD(&vfslist));
3007 oldhrt = ts2hrt(&vfs_mnttab_mtime);
3008 gethrestime(&vfs_mnttab_mtime);
3009 newhrt = ts2hrt(&vfs_mnttab_mtime);
3010 if (oldhrt == (hrtime_t)0)
3011 vfs_mnttab_ctime = vfs_mnttab_mtime;
3013 * Attempt to provide unique mtime (like uniqtime but not).
3015 if (newhrt == oldhrt) {
3016 newhrt++;
3017 hrt2ts(newhrt, &vfs_mnttab_mtime);
3019 pollwakeup(&vfs_pollhd, (short)POLLRDBAND);
3020 vfs_mnttab_writeop();
3024 dounmount(struct vfs *vfsp, int flag, cred_t *cr)
3026 vnode_t *coveredvp;
3027 int error;
3028 extern void teardown_vopstats(vfs_t *);
3031 * Get covered vnode. This will be NULL if the vfs is not linked
3032 * into the file system name space (i.e., domount() with MNT_NOSPICE).
3034 coveredvp = vfsp->vfs_vnodecovered;
3035 ASSERT(coveredvp == NULL || vn_vfswlock_held(coveredvp));
3038 * Purge all dnlc entries for this vfs.
3040 (void) dnlc_purge_vfsp(vfsp, 0);
3042 /* For forcible umount, skip VFS_SYNC() since it may hang */
3043 if ((flag & MS_FORCE) == 0)
3044 (void) VFS_SYNC(vfsp, 0, cr);
3047 * Lock the vfs to maintain fs status quo during unmount. This
3048 * has to be done after the sync because ufs_update tries to acquire
3049 * the vfs_reflock.
3051 vfs_lock_wait(vfsp);
3053 if (error = VFS_UNMOUNT(vfsp, flag, cr)) {
3054 vfs_unlock(vfsp);
3055 if (coveredvp != NULL)
3056 vn_vfsunlock(coveredvp);
3057 } else if (coveredvp != NULL) {
3058 teardown_vopstats(vfsp);
3060 * vfs_remove() will do a VN_RELE(vfsp->vfs_vnodecovered)
3061 * when it frees vfsp so we do a VN_HOLD() so we can
3062 * continue to use coveredvp afterwards.
3064 VN_HOLD(coveredvp);
3065 vfs_remove(vfsp);
3066 vn_vfsunlock(coveredvp);
3067 VN_RELE(coveredvp);
3068 } else {
3069 teardown_vopstats(vfsp);
3071 * Release the reference to vfs that is not linked
3072 * into the name space.
3074 vfs_unlock(vfsp);
3075 VFS_RELE(vfsp);
3077 return (error);
3082 * Vfs_unmountall() is called by uadmin() to unmount all
3083 * mounted file systems (except the root file system) during shutdown.
3084 * It follows the existing locking protocol when traversing the vfs list
3085 * to sync and unmount vfses. Even though there should be no
3086 * other thread running while the system is shutting down, it is prudent
3087 * to still follow the locking protocol.
3089 void
3090 vfs_unmountall(void)
3092 struct vfs *vfsp;
3093 struct vfs *prev_vfsp = NULL;
3094 int error;
3097 * Toss all dnlc entries now so that the per-vfs sync
3098 * and unmount operations don't have to slog through
3099 * a bunch of uninteresting vnodes over and over again.
3101 dnlc_purge();
3103 vfs_list_lock();
3104 for (vfsp = rootvfs->vfs_prev; vfsp != rootvfs; vfsp = prev_vfsp) {
3105 prev_vfsp = vfsp->vfs_prev;
3107 if (vfs_lock(vfsp) != 0)
3108 continue;
3109 error = vn_vfswlock(vfsp->vfs_vnodecovered);
3110 vfs_unlock(vfsp);
3111 if (error)
3112 continue;
3114 vfs_list_unlock();
3116 (void) VFS_SYNC(vfsp, SYNC_CLOSE, CRED());
3117 (void) dounmount(vfsp, 0, CRED());
3120 * Since we dropped the vfslist lock above we must
3121 * verify that next_vfsp still exists, else start over.
3123 vfs_list_lock();
3124 for (vfsp = rootvfs->vfs_prev;
3125 vfsp != rootvfs; vfsp = vfsp->vfs_prev)
3126 if (vfsp == prev_vfsp)
3127 break;
3128 if (vfsp == rootvfs && prev_vfsp != rootvfs)
3129 prev_vfsp = rootvfs->vfs_prev;
3131 vfs_list_unlock();
3135 * Called to add an entry to the end of the vfs mount in progress list
3137 void
3138 vfs_addmip(dev_t dev, struct vfs *vfsp)
3140 struct ipmnt *mipp;
3142 mipp = (struct ipmnt *)kmem_alloc(sizeof (struct ipmnt), KM_SLEEP);
3143 mipp->mip_next = NULL;
3144 mipp->mip_dev = dev;
3145 mipp->mip_vfsp = vfsp;
3146 mutex_enter(&vfs_miplist_mutex);
3147 if (vfs_miplist_end != NULL)
3148 vfs_miplist_end->mip_next = mipp;
3149 else
3150 vfs_miplist = mipp;
3151 vfs_miplist_end = mipp;
3152 mutex_exit(&vfs_miplist_mutex);
3156 * Called to remove an entry from the mount in progress list
3157 * Either because the mount completed or it failed.
3159 void
3160 vfs_delmip(struct vfs *vfsp)
3162 struct ipmnt *mipp, *mipprev;
3164 mutex_enter(&vfs_miplist_mutex);
3165 mipprev = NULL;
3166 for (mipp = vfs_miplist;
3167 mipp && mipp->mip_vfsp != vfsp; mipp = mipp->mip_next) {
3168 mipprev = mipp;
3170 if (mipp == NULL)
3171 return; /* shouldn't happen */
3172 if (mipp == vfs_miplist_end)
3173 vfs_miplist_end = mipprev;
3174 if (mipprev == NULL)
3175 vfs_miplist = mipp->mip_next;
3176 else
3177 mipprev->mip_next = mipp->mip_next;
3178 mutex_exit(&vfs_miplist_mutex);
3179 kmem_free(mipp, sizeof (struct ipmnt));
3183 * vfs_add is called by a specific filesystem's mount routine to add
3184 * the new vfs into the vfs list/hash and to cover the mounted-on vnode.
3185 * The vfs should already have been locked by the caller.
3187 * coveredvp is NULL if this is the root.
3189 void
3190 vfs_add(vnode_t *coveredvp, struct vfs *vfsp, int mflag)
3192 int newflag;
3194 ASSERT(vfs_lock_held(vfsp));
3195 VFS_HOLD(vfsp);
3196 newflag = vfsp->vfs_flag;
3197 if (mflag & MS_RDONLY)
3198 newflag |= VFS_RDONLY;
3199 else
3200 newflag &= ~VFS_RDONLY;
3201 if (mflag & MS_NOSUID)
3202 newflag |= (VFS_NOSETUID|VFS_NODEVICES);
3203 else
3204 newflag &= ~(VFS_NOSETUID|VFS_NODEVICES);
3205 if (mflag & MS_NOMNTTAB)
3206 newflag |= VFS_NOMNTTAB;
3207 else
3208 newflag &= ~VFS_NOMNTTAB;
3210 if (coveredvp != NULL) {
3211 ASSERT(vn_vfswlock_held(coveredvp));
3212 coveredvp->v_vfsmountedhere = vfsp;
3213 VN_HOLD(coveredvp);
3215 vfsp->vfs_vnodecovered = coveredvp;
3216 vfsp->vfs_flag = newflag;
3218 vfs_list_add(vfsp);
3222 * Remove a vfs from the vfs list, null out the pointer from the
3223 * covered vnode to the vfs (v_vfsmountedhere), and null out the pointer
3224 * from the vfs to the covered vnode (vfs_vnodecovered). Release the
3225 * reference to the vfs and to the covered vnode.
3227 * Called from dounmount after it's confirmed with the file system
3228 * that the unmount is legal.
3230 void
3231 vfs_remove(struct vfs *vfsp)
3233 vnode_t *vp;
3235 ASSERT(vfs_lock_held(vfsp));
3238 * Can't unmount root. Should never happen because fs will
3239 * be busy.
3241 if (vfsp == rootvfs)
3242 panic("vfs_remove: unmounting root");
3244 vfs_list_remove(vfsp);
3247 * Unhook from the file system name space.
3249 vp = vfsp->vfs_vnodecovered;
3250 ASSERT(vn_vfswlock_held(vp));
3251 vp->v_vfsmountedhere = NULL;
3252 vfsp->vfs_vnodecovered = NULL;
3253 VN_RELE(vp);
3256 * Release lock and wakeup anybody waiting.
3258 vfs_unlock(vfsp);
3259 VFS_RELE(vfsp);
3263 * Lock a filesystem to prevent access to it while mounting,
3264 * unmounting and syncing. Return EBUSY immediately if lock
3265 * can't be acquired.
3268 vfs_lock(vfs_t *vfsp)
3270 vn_vfslocks_entry_t *vpvfsentry;
3272 vpvfsentry = vn_vfslocks_getlock(vfsp);
3273 if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER))
3274 return (0);
3276 vn_vfslocks_rele(vpvfsentry);
3277 return (EBUSY);
3281 vfs_rlock(vfs_t *vfsp)
3283 vn_vfslocks_entry_t *vpvfsentry;
3285 vpvfsentry = vn_vfslocks_getlock(vfsp);
3287 if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER))
3288 return (0);
3290 vn_vfslocks_rele(vpvfsentry);
3291 return (EBUSY);
3294 void
3295 vfs_lock_wait(vfs_t *vfsp)
3297 vn_vfslocks_entry_t *vpvfsentry;
3299 vpvfsentry = vn_vfslocks_getlock(vfsp);
3300 rwst_enter(&vpvfsentry->ve_lock, RW_WRITER);
3303 void
3304 vfs_rlock_wait(vfs_t *vfsp)
3306 vn_vfslocks_entry_t *vpvfsentry;
3308 vpvfsentry = vn_vfslocks_getlock(vfsp);
3309 rwst_enter(&vpvfsentry->ve_lock, RW_READER);
3313 * Unlock a locked filesystem.
3315 void
3316 vfs_unlock(vfs_t *vfsp)
3318 vn_vfslocks_entry_t *vpvfsentry;
3321 * vfs_unlock will mimic sema_v behaviour to fix 4748018.
3322 * And these changes should remain for the patch changes as it is.
3324 if (panicstr)
3325 return;
3328 * ve_refcount needs to be dropped twice here.
3329 * 1. To release refernce after a call to vfs_locks_getlock()
3330 * 2. To release the reference from the locking routines like
3331 * vfs_rlock_wait/vfs_wlock_wait/vfs_wlock etc,.
3334 vpvfsentry = vn_vfslocks_getlock(vfsp);
3335 vn_vfslocks_rele(vpvfsentry);
3337 rwst_exit(&vpvfsentry->ve_lock);
3338 vn_vfslocks_rele(vpvfsentry);
3342 * Utility routine that allows a filesystem to construct its
3343 * fsid in "the usual way" - by munging some underlying dev_t and
3344 * the filesystem type number into the 64-bit fsid. Note that
3345 * this implicitly relies on dev_t persistence to make filesystem
3346 * id's persistent.
3348 * There's nothing to prevent an individual fs from constructing its
3349 * fsid in a different way, and indeed they should.
3351 * Since we want fsids to be 32-bit quantities (so that they can be
3352 * exported identically by either 32-bit or 64-bit APIs, as well as
3353 * the fact that fsid's are "known" to NFS), we compress the device
3354 * number given down to 32-bits, and panic if that isn't possible.
3356 void
3357 vfs_make_fsid(fsid_t *fsi, dev_t dev, int val)
3359 if (!cmpldev((dev32_t *)&fsi->val[0], dev))
3360 panic("device number too big for fsid!");
3361 fsi->val[1] = val;
3365 vfs_lock_held(vfs_t *vfsp)
3367 int held;
3368 vn_vfslocks_entry_t *vpvfsentry;
3371 * vfs_lock_held will mimic sema_held behaviour
3372 * if panicstr is set. And these changes should remain
3373 * for the patch changes as it is.
3375 if (panicstr)
3376 return (1);
3378 vpvfsentry = vn_vfslocks_getlock(vfsp);
3379 held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER);
3381 vn_vfslocks_rele(vpvfsentry);
3382 return (held);
3385 struct _kthread *
3386 vfs_lock_owner(vfs_t *vfsp)
3388 struct _kthread *owner;
3389 vn_vfslocks_entry_t *vpvfsentry;
3392 * vfs_wlock_held will mimic sema_held behaviour
3393 * if panicstr is set. And these changes should remain
3394 * for the patch changes as it is.
3396 if (panicstr)
3397 return (NULL);
3399 vpvfsentry = vn_vfslocks_getlock(vfsp);
3400 owner = rwst_owner(&vpvfsentry->ve_lock);
3402 vn_vfslocks_rele(vpvfsentry);
3403 return (owner);
3407 * vfs list locking.
3409 * Rather than manipulate the vfslist lock directly, we abstract into lock
3410 * and unlock routines to allow the locking implementation to be changed for
3411 * clustering.
3413 * Whenever the vfs list is modified through its hash links, the overall list
3414 * lock must be obtained before locking the relevant hash bucket. But to see
3415 * whether a given vfs is on the list, it suffices to obtain the lock for the
3416 * hash bucket without getting the overall list lock. (See getvfs() below.)
3419 void
3420 vfs_list_lock()
3422 rw_enter(&vfslist, RW_WRITER);
3425 void
3426 vfs_list_read_lock()
3428 rw_enter(&vfslist, RW_READER);
3431 void
3432 vfs_list_unlock()
3434 rw_exit(&vfslist);
3438 * Low level worker routines for adding entries to and removing entries from
3439 * the vfs list.
3442 static void
3443 vfs_hash_add(struct vfs *vfsp, int insert_at_head)
3445 int vhno;
3446 struct vfs **hp;
3447 dev_t dev;
3449 ASSERT(RW_WRITE_HELD(&vfslist));
3451 dev = expldev(vfsp->vfs_fsid.val[0]);
3452 vhno = VFSHASH(getmajor(dev), getminor(dev));
3454 mutex_enter(&rvfs_list[vhno].rvfs_lock);
3457 * Link into the hash table, inserting it at the end, so that LOFS
3458 * with the same fsid as UFS (or other) file systems will not hide the
3459 * UFS.
3461 if (insert_at_head) {
3462 vfsp->vfs_hash = rvfs_list[vhno].rvfs_head;
3463 rvfs_list[vhno].rvfs_head = vfsp;
3464 } else {
3465 for (hp = &rvfs_list[vhno].rvfs_head; *hp != NULL;
3466 hp = &(*hp)->vfs_hash)
3467 continue;
3469 * hp now contains the address of the pointer to update
3470 * to effect the insertion.
3472 vfsp->vfs_hash = NULL;
3473 *hp = vfsp;
3476 rvfs_list[vhno].rvfs_len++;
3477 mutex_exit(&rvfs_list[vhno].rvfs_lock);
3481 static void
3482 vfs_hash_remove(struct vfs *vfsp)
3484 int vhno;
3485 struct vfs *tvfsp;
3486 dev_t dev;
3488 ASSERT(RW_WRITE_HELD(&vfslist));
3490 dev = expldev(vfsp->vfs_fsid.val[0]);
3491 vhno = VFSHASH(getmajor(dev), getminor(dev));
3493 mutex_enter(&rvfs_list[vhno].rvfs_lock);
3496 * Remove from hash.
3498 if (rvfs_list[vhno].rvfs_head == vfsp) {
3499 rvfs_list[vhno].rvfs_head = vfsp->vfs_hash;
3500 rvfs_list[vhno].rvfs_len--;
3501 goto foundit;
3503 for (tvfsp = rvfs_list[vhno].rvfs_head; tvfsp != NULL;
3504 tvfsp = tvfsp->vfs_hash) {
3505 if (tvfsp->vfs_hash == vfsp) {
3506 tvfsp->vfs_hash = vfsp->vfs_hash;
3507 rvfs_list[vhno].rvfs_len--;
3508 goto foundit;
3511 cmn_err(CE_WARN, "vfs_list_remove: vfs not found in hash");
3513 foundit:
3515 mutex_exit(&rvfs_list[vhno].rvfs_lock);
3519 void
3520 vfs_list_add(struct vfs *vfsp)
3522 zone_t *zone;
3525 * Typically, the vfs_t will have been created on behalf of the file
3526 * system in vfs_init, where it will have been provided with a
3527 * vfs_impl_t. This, however, might be lacking if the vfs_t was created
3528 * by an unbundled file system. We therefore check for such an example
3529 * before stamping the vfs_t with its creation time for the benefit of
3530 * mntfs.
3532 if (vfsp->vfs_implp == NULL)
3533 vfsimpl_setup(vfsp);
3534 vfs_mono_time(&vfsp->vfs_hrctime);
3537 * The zone that owns the mount is the one that performed the mount.
3538 * Note that this isn't necessarily the same as the zone mounted into.
3539 * The corresponding zone_rele_ref() will be done when the vfs_t
3540 * is being free'd.
3542 vfsp->vfs_zone = curproc->p_zone;
3543 zone_init_ref(&vfsp->vfs_implp->vi_zone_ref);
3544 zone_hold_ref(vfsp->vfs_zone, &vfsp->vfs_implp->vi_zone_ref,
3545 ZONE_REF_VFS);
3548 * Find the zone mounted into, and put this mount on its vfs list.
3550 zone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
3551 ASSERT(zone != NULL);
3553 * Special casing for the root vfs. This structure is allocated
3554 * statically and hooked onto rootvfs at link time. During the
3555 * vfs_mountroot call at system startup time, the root file system's
3556 * VFS_MOUNTROOT routine will call vfs_add with this root vfs struct
3557 * as argument. The code below must detect and handle this special
3558 * case. The only apparent justification for this special casing is
3559 * to ensure that the root file system appears at the head of the
3560 * list.
3562 * XXX: I'm assuming that it's ok to do normal list locking when
3563 * adding the entry for the root file system (this used to be
3564 * done with no locks held).
3566 vfs_list_lock();
3568 * Link into the vfs list proper.
3570 if (vfsp == &root) {
3572 * Assert: This vfs is already on the list as its first entry.
3573 * Thus, there's nothing to do.
3575 ASSERT(rootvfs == vfsp);
3577 * Add it to the head of the global zone's vfslist.
3579 ASSERT(zone == global_zone);
3580 ASSERT(zone->zone_vfslist == NULL);
3581 zone->zone_vfslist = vfsp;
3582 } else {
3584 * Link to end of list using vfs_prev (as rootvfs is now a
3585 * doubly linked circular list) so list is in mount order for
3586 * mnttab use.
3588 rootvfs->vfs_prev->vfs_next = vfsp;
3589 vfsp->vfs_prev = rootvfs->vfs_prev;
3590 rootvfs->vfs_prev = vfsp;
3591 vfsp->vfs_next = rootvfs;
3594 * Do it again for the zone-private list (which may be NULL).
3596 if (zone->zone_vfslist == NULL) {
3597 ASSERT(zone != global_zone);
3598 zone->zone_vfslist = vfsp;
3599 } else {
3600 zone->zone_vfslist->vfs_zone_prev->vfs_zone_next = vfsp;
3601 vfsp->vfs_zone_prev = zone->zone_vfslist->vfs_zone_prev;
3602 zone->zone_vfslist->vfs_zone_prev = vfsp;
3603 vfsp->vfs_zone_next = zone->zone_vfslist;
3608 * Link into the hash table, inserting it at the end, so that LOFS
3609 * with the same fsid as UFS (or other) file systems will not hide
3610 * the UFS.
3612 vfs_hash_add(vfsp, 0);
3615 * update the mnttab modification time
3617 vfs_mnttab_modtimeupd();
3618 vfs_list_unlock();
3619 zone_rele(zone);
3622 void
3623 vfs_list_remove(struct vfs *vfsp)
3625 zone_t *zone;
3627 zone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
3628 ASSERT(zone != NULL);
3630 * Callers are responsible for preventing attempts to unmount the
3631 * root.
3633 ASSERT(vfsp != rootvfs);
3635 vfs_list_lock();
3638 * Remove from hash.
3640 vfs_hash_remove(vfsp);
3643 * Remove from vfs list.
3645 vfsp->vfs_prev->vfs_next = vfsp->vfs_next;
3646 vfsp->vfs_next->vfs_prev = vfsp->vfs_prev;
3647 vfsp->vfs_next = vfsp->vfs_prev = NULL;
3650 * Remove from zone-specific vfs list.
3652 if (zone->zone_vfslist == vfsp)
3653 zone->zone_vfslist = vfsp->vfs_zone_next;
3655 if (vfsp->vfs_zone_next == vfsp) {
3656 ASSERT(vfsp->vfs_zone_prev == vfsp);
3657 ASSERT(zone->zone_vfslist == vfsp);
3658 zone->zone_vfslist = NULL;
3661 vfsp->vfs_zone_prev->vfs_zone_next = vfsp->vfs_zone_next;
3662 vfsp->vfs_zone_next->vfs_zone_prev = vfsp->vfs_zone_prev;
3663 vfsp->vfs_zone_next = vfsp->vfs_zone_prev = NULL;
3666 * update the mnttab modification time
3668 vfs_mnttab_modtimeupd();
3669 vfs_list_unlock();
3670 zone_rele(zone);
3673 struct vfs *
3674 getvfs(fsid_t *fsid)
3676 struct vfs *vfsp;
3677 int val0 = fsid->val[0];
3678 int val1 = fsid->val[1];
3679 dev_t dev = expldev(val0);
3680 int vhno = VFSHASH(getmajor(dev), getminor(dev));
3681 kmutex_t *hmp = &rvfs_list[vhno].rvfs_lock;
3683 mutex_enter(hmp);
3684 for (vfsp = rvfs_list[vhno].rvfs_head; vfsp; vfsp = vfsp->vfs_hash) {
3685 if (vfsp->vfs_fsid.val[0] == val0 &&
3686 vfsp->vfs_fsid.val[1] == val1) {
3687 VFS_HOLD(vfsp);
3688 mutex_exit(hmp);
3689 return (vfsp);
3692 mutex_exit(hmp);
3693 return (NULL);
3697 * Search the vfs mount in progress list for a specified device/vfs entry.
3698 * Returns 0 if the first entry in the list that the device matches has the
3699 * given vfs pointer as well. If the device matches but a different vfs
3700 * pointer is encountered in the list before the given vfs pointer then
3701 * a 1 is returned.
3705 vfs_devmounting(dev_t dev, struct vfs *vfsp)
3707 int retval = 0;
3708 struct ipmnt *mipp;
3710 mutex_enter(&vfs_miplist_mutex);
3711 for (mipp = vfs_miplist; mipp != NULL; mipp = mipp->mip_next) {
3712 if (mipp->mip_dev == dev) {
3713 if (mipp->mip_vfsp != vfsp)
3714 retval = 1;
3715 break;
3718 mutex_exit(&vfs_miplist_mutex);
3719 return (retval);
3723 * Search the vfs list for a specified device. Returns 1, if entry is found
3724 * or 0 if no suitable entry is found.
3728 vfs_devismounted(dev_t dev)
3730 struct vfs *vfsp;
3731 int found;
3733 vfs_list_read_lock();
3734 vfsp = rootvfs;
3735 found = 0;
3736 do {
3737 if (vfsp->vfs_dev == dev) {
3738 found = 1;
3739 break;
3741 vfsp = vfsp->vfs_next;
3742 } while (vfsp != rootvfs);
3744 vfs_list_unlock();
3745 return (found);
3749 * Search the vfs list for a specified device. Returns a pointer to it
3750 * or NULL if no suitable entry is found. The caller of this routine
3751 * is responsible for releasing the returned vfs pointer.
3753 struct vfs *
3754 vfs_dev2vfsp(dev_t dev)
3756 struct vfs *vfsp;
3757 int found;
3759 vfs_list_read_lock();
3760 vfsp = rootvfs;
3761 found = 0;
3762 do {
3764 * The following could be made more efficient by making
3765 * the entire loop use vfs_zone_next if the call is from
3766 * a zone. The only callers, however, ustat(2) and
3767 * umount2(2), don't seem to justify the added
3768 * complexity at present.
3770 if (vfsp->vfs_dev == dev &&
3771 ZONE_PATH_VISIBLE(refstr_value(vfsp->vfs_mntpt),
3772 curproc->p_zone)) {
3773 VFS_HOLD(vfsp);
3774 found = 1;
3775 break;
3777 vfsp = vfsp->vfs_next;
3778 } while (vfsp != rootvfs);
3779 vfs_list_unlock();
3780 return (found ? vfsp: NULL);
3784 * Search the vfs list for a specified mntpoint. Returns a pointer to it
3785 * or NULL if no suitable entry is found. The caller of this routine
3786 * is responsible for releasing the returned vfs pointer.
3788 * Note that if multiple mntpoints match, the last one matching is
3789 * returned in an attempt to return the "top" mount when overlay
3790 * mounts are covering the same mount point. This is accomplished by starting
3791 * at the end of the list and working our way backwards, stopping at the first
3792 * matching mount.
3794 struct vfs *
3795 vfs_mntpoint2vfsp(const char *mp)
3797 struct vfs *vfsp;
3798 struct vfs *retvfsp = NULL;
3799 zone_t *zone = curproc->p_zone;
3800 struct vfs *list;
3802 vfs_list_read_lock();
3803 if (getzoneid() == GLOBAL_ZONEID) {
3805 * The global zone may see filesystems in any zone.
3807 vfsp = rootvfs->vfs_prev;
3808 do {
3809 if (strcmp(refstr_value(vfsp->vfs_mntpt), mp) == 0) {
3810 retvfsp = vfsp;
3811 break;
3813 vfsp = vfsp->vfs_prev;
3814 } while (vfsp != rootvfs->vfs_prev);
3815 } else if ((list = zone->zone_vfslist) != NULL) {
3816 const char *mntpt;
3818 vfsp = list->vfs_zone_prev;
3819 do {
3820 mntpt = refstr_value(vfsp->vfs_mntpt);
3821 mntpt = ZONE_PATH_TRANSLATE(mntpt, zone);
3822 if (strcmp(mntpt, mp) == 0) {
3823 retvfsp = vfsp;
3824 break;
3826 vfsp = vfsp->vfs_zone_prev;
3827 } while (vfsp != list->vfs_zone_prev);
3829 if (retvfsp)
3830 VFS_HOLD(retvfsp);
3831 vfs_list_unlock();
3832 return (retvfsp);
3836 * Search the vfs list for a specified vfsops.
3837 * if vfs entry is found then return 1, else 0.
3840 vfs_opsinuse(vfsops_t *ops)
3842 struct vfs *vfsp;
3843 int found;
3845 vfs_list_read_lock();
3846 vfsp = rootvfs;
3847 found = 0;
3848 do {
3849 if (vfs_getops(vfsp) == ops) {
3850 found = 1;
3851 break;
3853 vfsp = vfsp->vfs_next;
3854 } while (vfsp != rootvfs);
3855 vfs_list_unlock();
3856 return (found);
3860 * Allocate an entry in vfssw for a file system type
3862 struct vfssw *
3863 allocate_vfssw(const char *type)
3865 struct vfssw *vswp;
3867 if (type[0] == '\0' || strlen(type) + 1 > _ST_FSTYPSZ) {
3869 * The vfssw table uses the empty string to identify an
3870 * available entry; we cannot add any type which has
3871 * a leading NUL. The string length is limited to
3872 * the size of the st_fstype array in struct stat.
3874 return (NULL);
3877 ASSERT(VFSSW_WRITE_LOCKED());
3878 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++)
3879 if (!ALLOCATED_VFSSW(vswp)) {
3880 vswp->vsw_name = kmem_alloc(strlen(type) + 1, KM_SLEEP);
3881 (void) strcpy(vswp->vsw_name, type);
3882 ASSERT(vswp->vsw_count == 0);
3883 vswp->vsw_count = 1;
3884 mutex_init(&vswp->vsw_lock, NULL, MUTEX_DEFAULT, NULL);
3885 return (vswp);
3887 return (NULL);
3891 * Impose additional layer of translation between vfstype names
3892 * and module names in the filesystem.
3894 static const char *
3895 vfs_to_modname(const char *vfstype)
3897 if (strcmp(vfstype, "proc") == 0) {
3898 vfstype = "procfs";
3899 } else if (strcmp(vfstype, "fd") == 0) {
3900 vfstype = "fdfs";
3901 } else if (strncmp(vfstype, "nfs", 3) == 0) {
3902 vfstype = "nfs";
3905 return (vfstype);
3909 * Find a vfssw entry given a file system type name.
3910 * Try to autoload the filesystem if it's not found.
3911 * If it's installed, return the vfssw locked to prevent unloading.
3913 struct vfssw *
3914 vfs_getvfssw(const char *type)
3916 struct vfssw *vswp;
3917 const char *modname;
3919 RLOCK_VFSSW();
3920 vswp = vfs_getvfsswbyname(type);
3921 modname = vfs_to_modname(type);
3923 if (rootdir == NULL) {
3925 * If we haven't yet loaded the root file system, then our
3926 * _init won't be called until later. Allocate vfssw entry,
3927 * because mod_installfs won't be called.
3929 if (vswp == NULL) {
3930 RUNLOCK_VFSSW();
3931 WLOCK_VFSSW();
3932 if ((vswp = vfs_getvfsswbyname(type)) == NULL) {
3933 if ((vswp = allocate_vfssw(type)) == NULL) {
3934 WUNLOCK_VFSSW();
3935 return (NULL);
3938 WUNLOCK_VFSSW();
3939 RLOCK_VFSSW();
3941 if (!VFS_INSTALLED(vswp)) {
3942 RUNLOCK_VFSSW();
3943 (void) modloadonly("fs", modname);
3944 } else
3945 RUNLOCK_VFSSW();
3946 return (vswp);
3950 * Try to load the filesystem. Before calling modload(), we drop
3951 * our lock on the VFS switch table, and pick it up after the
3952 * module is loaded. However, there is a potential race: the
3953 * module could be unloaded after the call to modload() completes
3954 * but before we pick up the lock and drive on. Therefore,
3955 * we keep reloading the module until we've loaded the module
3956 * _and_ we have the lock on the VFS switch table.
3958 while (vswp == NULL || !VFS_INSTALLED(vswp)) {
3959 RUNLOCK_VFSSW();
3960 if (modload("fs", modname) == -1)
3961 return (NULL);
3962 RLOCK_VFSSW();
3963 if (vswp == NULL)
3964 if ((vswp = vfs_getvfsswbyname(type)) == NULL)
3965 break;
3967 RUNLOCK_VFSSW();
3969 return (vswp);
3973 * Find a vfssw entry given a file system type name.
3975 struct vfssw *
3976 vfs_getvfsswbyname(const char *type)
3978 struct vfssw *vswp;
3980 ASSERT(VFSSW_LOCKED());
3981 if (type == NULL || *type == '\0')
3982 return (NULL);
3984 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
3985 if (strcmp(type, vswp->vsw_name) == 0) {
3986 vfs_refvfssw(vswp);
3987 return (vswp);
3991 return (NULL);
3995 * Find a vfssw entry given a set of vfsops.
3997 struct vfssw *
3998 vfs_getvfsswbyvfsops(vfsops_t *vfsops)
4000 struct vfssw *vswp;
4002 RLOCK_VFSSW();
4003 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
4004 if (ALLOCATED_VFSSW(vswp) && &vswp->vsw_vfsops == vfsops) {
4005 vfs_refvfssw(vswp);
4006 RUNLOCK_VFSSW();
4007 return (vswp);
4010 RUNLOCK_VFSSW();
4012 return (NULL);
4016 * Reference a vfssw entry.
4018 void
4019 vfs_refvfssw(struct vfssw *vswp)
4022 mutex_enter(&vswp->vsw_lock);
4023 vswp->vsw_count++;
4024 mutex_exit(&vswp->vsw_lock);
4028 * Unreference a vfssw entry.
4030 void
4031 vfs_unrefvfssw(struct vfssw *vswp)
4034 mutex_enter(&vswp->vsw_lock);
4035 vswp->vsw_count--;
4036 mutex_exit(&vswp->vsw_lock);
4039 int sync_timeout = 30; /* timeout for syncing a page during panic */
4040 int sync_timeleft; /* portion of sync_timeout remaining */
4042 static int sync_retries = 20; /* number of retries when not making progress */
4043 static int sync_triesleft; /* portion of sync_retries remaining */
4045 static pgcnt_t old_pgcnt, new_pgcnt;
4046 static int new_bufcnt, old_bufcnt;
4049 * Sync all of the mounted filesystems, and then wait for the actual i/o to
4050 * complete. We wait by counting the number of dirty pages and buffers,
4051 * pushing them out using bio_busy() and page_busy(), and then counting again.
4052 * This routine is used during both the uadmin A_SHUTDOWN code as well as
4053 * the SYNC phase of the panic code (see comments in panic.c). It should only
4054 * be used after some higher-level mechanism has quiesced the system so that
4055 * new writes are not being initiated while we are waiting for completion.
4057 * To ensure finite running time, our algorithm uses two timeout mechanisms:
4058 * sync_timeleft (a timer implemented by the omnipresent deadman() cyclic), and
4059 * sync_triesleft (a progress counter used by the vfs_syncall() loop below).
4060 * Together these ensure that syncing completes if our i/o paths are stuck.
4061 * The counters are declared above so they can be found easily in the debugger.
4063 * The sync_timeleft counter is reset by bio_busy() and page_busy() using the
4064 * vfs_syncprogress() subroutine whenever we make progress through the lists of
4065 * pages and buffers. It is decremented and expired by the deadman() cyclic.
4066 * When vfs_syncall() decides it is done, we disable the deadman() counter by
4067 * setting sync_timeleft to zero. This timer guards against vfs_syncall()
4068 * deadlocking or hanging inside of a broken filesystem or driver routine.
4070 * The sync_triesleft counter is updated by vfs_syncall() itself. If we make
4071 * sync_retries consecutive calls to bio_busy() and page_busy() without
4072 * decreasing either the number of dirty buffers or dirty pages below the
4073 * lowest count we have seen so far, we give up and return from vfs_syncall().
4075 * Each loop iteration ends with a call to delay() one second to allow time for
4076 * i/o completion and to permit the user time to read our progress messages.
4078 void
4079 vfs_syncall(void)
4081 if (rootdir == NULL && !modrootloaded)
4082 return; /* panic during boot - no filesystems yet */
4084 printf("syncing file systems...");
4085 vfs_syncprogress();
4086 sync();
4088 vfs_syncprogress();
4089 sync_triesleft = sync_retries;
4091 old_bufcnt = new_bufcnt = INT_MAX;
4092 old_pgcnt = new_pgcnt = ULONG_MAX;
4094 while (sync_triesleft > 0) {
4095 old_bufcnt = MIN(old_bufcnt, new_bufcnt);
4096 old_pgcnt = MIN(old_pgcnt, new_pgcnt);
4098 new_bufcnt = bio_busy(B_TRUE);
4099 new_pgcnt = page_busy(B_TRUE);
4100 vfs_syncprogress();
4102 if (new_bufcnt == 0 && new_pgcnt == 0)
4103 break;
4105 if (new_bufcnt < old_bufcnt || new_pgcnt < old_pgcnt)
4106 sync_triesleft = sync_retries;
4107 else
4108 sync_triesleft--;
4110 if (new_bufcnt)
4111 printf(" [%d]", new_bufcnt);
4112 if (new_pgcnt)
4113 printf(" %lu", new_pgcnt);
4115 delay(hz);
4118 if (new_bufcnt != 0 || new_pgcnt != 0)
4119 printf(" done (not all i/o completed)\n");
4120 else
4121 printf(" done\n");
4123 sync_timeleft = 0;
4124 delay(hz);
4128 * If we are in the middle of the sync phase of panic, reset sync_timeleft to
4129 * sync_timeout to indicate that we are making progress and the deadman()
4130 * omnipresent cyclic should not yet time us out. Note that it is safe to
4131 * store to sync_timeleft here since the deadman() is firing at high-level
4132 * on top of us. If we are racing with the deadman(), either the deadman()
4133 * will decrement the old value and then we will reset it, or we will
4134 * reset it and then the deadman() will immediately decrement it. In either
4135 * case, correct behavior results.
4137 void
4138 vfs_syncprogress(void)
4140 if (panicstr)
4141 sync_timeleft = sync_timeout;
4145 * Map VFS flags to statvfs flags. These shouldn't really be separate
4146 * flags at all.
4148 uint_t
4149 vf_to_stf(uint_t vf)
4151 uint_t stf = 0;
4153 if (vf & VFS_RDONLY)
4154 stf |= ST_RDONLY;
4155 if (vf & VFS_NOSETUID)
4156 stf |= ST_NOSUID;
4157 if (vf & VFS_NOTRUNC)
4158 stf |= ST_NOTRUNC;
4160 return (stf);
4164 * Entries for (illegal) fstype 0.
4166 /* ARGSUSED */
4168 vfsstray_sync(struct vfs *vfsp, short arg, struct cred *cr)
4170 cmn_err(CE_PANIC, "stray vfs operation");
4171 return (0);
4175 * Entries for (illegal) fstype 0.
4178 vfsstray(void)
4180 cmn_err(CE_PANIC, "stray vfs operation");
4181 return (0);
4185 * Support for dealing with forced UFS unmount and its interaction with
4186 * LOFS. Could be used by any filesystem.
4187 * See bug 1203132.
4190 vfs_EIO(void)
4192 return (EIO);
4196 * We've gotta define the op for sync separately, since the compiler gets
4197 * confused if we mix and match ANSI and normal style prototypes when
4198 * a "short" argument is present and spits out a warning.
4200 /*ARGSUSED*/
4202 vfs_EIO_sync(struct vfs *vfsp, short arg, struct cred *cr)
4204 return (EIO);
4207 vfs_t EIO_vfs;
4208 vfsops_t *EIO_vfsops;
4211 * Called from startup() to initialize all loaded vfs's
4213 void
4214 vfsinit(void)
4216 struct vfssw *vswp;
4217 int error;
4218 extern int vopstats_enabled;
4219 extern void vopstats_startup();
4221 static const fs_operation_def_t EIO_vfsops_template[] = {
4222 VFSNAME_MOUNT, { .error = vfs_EIO },
4223 VFSNAME_UNMOUNT, { .error = vfs_EIO },
4224 VFSNAME_ROOT, { .error = vfs_EIO },
4225 VFSNAME_STATVFS, { .error = vfs_EIO },
4226 VFSNAME_SYNC, { .vfs_sync = vfs_EIO_sync },
4227 VFSNAME_VGET, { .error = vfs_EIO },
4228 VFSNAME_MOUNTROOT, { .error = vfs_EIO },
4229 VFSNAME_FREEVFS, { .error = vfs_EIO },
4230 VFSNAME_VNSTATE, { .error = vfs_EIO },
4231 NULL, NULL
4234 static const fs_operation_def_t stray_vfsops_template[] = {
4235 VFSNAME_MOUNT, { .error = vfsstray },
4236 VFSNAME_UNMOUNT, { .error = vfsstray },
4237 VFSNAME_ROOT, { .error = vfsstray },
4238 VFSNAME_STATVFS, { .error = vfsstray },
4239 VFSNAME_SYNC, { .vfs_sync = vfsstray_sync },
4240 VFSNAME_VGET, { .error = vfsstray },
4241 VFSNAME_MOUNTROOT, { .error = vfsstray },
4242 VFSNAME_FREEVFS, { .error = vfsstray },
4243 VFSNAME_VNSTATE, { .error = vfsstray },
4244 NULL, NULL
4247 /* Create vfs cache */
4248 vfs_cache = kmem_cache_create("vfs_cache", sizeof (struct vfs),
4249 sizeof (uintptr_t), NULL, NULL, NULL, NULL, NULL, 0);
4251 /* Initialize the vnode cache (file systems may use it during init). */
4252 vn_create_cache();
4254 /* Setup event monitor framework */
4255 fem_init();
4257 /* Initialize the dummy stray file system type. */
4258 error = vfs_setfsops(0, stray_vfsops_template, NULL);
4260 /* Initialize the dummy EIO file system. */
4261 error = vfs_makefsops(EIO_vfsops_template, &EIO_vfsops);
4262 if (error != 0) {
4263 cmn_err(CE_WARN, "vfsinit: bad EIO vfs ops template");
4264 /* Shouldn't happen, but not bad enough to panic */
4267 VFS_INIT(&EIO_vfs, EIO_vfsops, (caddr_t)NULL);
4270 * Default EIO_vfs.vfs_flag to VFS_UNMOUNTED so a lookup
4271 * on this vfs can immediately notice it's invalid.
4273 EIO_vfs.vfs_flag |= VFS_UNMOUNTED;
4276 * Call the init routines of non-loadable filesystems only.
4277 * Filesystems which are loaded as separate modules will be
4278 * initialized by the module loading code instead.
4281 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
4282 RLOCK_VFSSW();
4283 if (vswp->vsw_init != NULL)
4284 (*vswp->vsw_init)(vswp - vfssw, vswp->vsw_name);
4285 RUNLOCK_VFSSW();
4288 vopstats_startup();
4290 if (vopstats_enabled) {
4291 /* EIO_vfs can collect stats, but we don't retrieve them */
4292 initialize_vopstats(&EIO_vfs.vfs_vopstats);
4293 EIO_vfs.vfs_fstypevsp = NULL;
4294 EIO_vfs.vfs_vskap = NULL;
4295 EIO_vfs.vfs_flag |= VFS_STATS;
4298 xattr_init();
4300 reparse_point_init();
4303 vfs_t *
4304 vfs_alloc(int kmflag)
4306 vfs_t *vfsp;
4308 vfsp = kmem_cache_alloc(vfs_cache, kmflag);
4311 * Do the simplest initialization here.
4312 * Everything else gets done in vfs_init()
4314 bzero(vfsp, sizeof (vfs_t));
4315 return (vfsp);
4318 void
4319 vfs_free(vfs_t *vfsp)
4322 * One would be tempted to assert that "vfsp->vfs_count == 0".
4323 * The problem is that this gets called out of domount() with
4324 * a partially initialized vfs and a vfs_count of 1. This is
4325 * also called from vfs_rele() with a vfs_count of 0. We can't
4326 * call VFS_RELE() from domount() if VFS_MOUNT() hasn't successfully
4327 * returned. This is because VFS_MOUNT() fully initializes the
4328 * vfs structure and its associated data. VFS_RELE() will call
4329 * VFS_FREEVFS() which may panic the system if the data structures
4330 * aren't fully initialized from a successful VFS_MOUNT()).
4333 /* If FEM was in use, make sure everything gets cleaned up */
4334 if (vfsp->vfs_femhead) {
4335 ASSERT(vfsp->vfs_femhead->femh_list == NULL);
4336 mutex_destroy(&vfsp->vfs_femhead->femh_lock);
4337 kmem_free(vfsp->vfs_femhead, sizeof (*(vfsp->vfs_femhead)));
4338 vfsp->vfs_femhead = NULL;
4341 if (vfsp->vfs_implp)
4342 vfsimpl_teardown(vfsp);
4343 sema_destroy(&vfsp->vfs_reflock);
4344 kmem_cache_free(vfs_cache, vfsp);
4348 * Increments the vfs reference count by one atomically.
4350 void
4351 vfs_hold(vfs_t *vfsp)
4353 atomic_inc_32(&vfsp->vfs_count);
4354 ASSERT(vfsp->vfs_count != 0);
4358 * Decrements the vfs reference count by one atomically. When
4359 * vfs reference count becomes zero, it calls the file system
4360 * specific vfs_freevfs() to free up the resources.
4362 void
4363 vfs_rele(vfs_t *vfsp)
4365 ASSERT(vfsp->vfs_count != 0);
4366 if (atomic_dec_32_nv(&vfsp->vfs_count) == 0) {
4367 VFS_FREEVFS(vfsp);
4368 lofi_remove(vfsp);
4369 if (vfsp->vfs_zone)
4370 zone_rele_ref(&vfsp->vfs_implp->vi_zone_ref,
4371 ZONE_REF_VFS);
4372 vfs_freemnttab(vfsp);
4373 vfs_free(vfsp);
4378 * Generic operations vector support.
4380 * This is used to build operations vectors for both the vfs and vnode.
4381 * It's normally called only when a file system is loaded.
4383 * There are many possible algorithms for this, including the following:
4385 * (1) scan the list of known operations; for each, see if the file system
4386 * includes an entry for it, and fill it in as appropriate.
4388 * (2) set up defaults for all known operations. scan the list of ops
4389 * supplied by the file system; for each which is both supplied and
4390 * known, fill it in.
4392 * (3) sort the lists of known ops & supplied ops; scan the list, filling
4393 * in entries as we go.
4395 * we choose (1) for simplicity, and because performance isn't critical here.
4396 * note that (2) could be sped up using a precomputed hash table on known ops.
4397 * (3) could be faster than either, but only if the lists were very large or
4398 * supplied in sorted order.
4403 fs_build_vector(void *vector, int *unused_ops,
4404 const fs_operation_trans_def_t *translation,
4405 const fs_operation_def_t *operations)
4407 int i, num_trans, num_ops, used;
4410 * Count the number of translations and the number of supplied
4411 * operations.
4415 const fs_operation_trans_def_t *p;
4417 for (num_trans = 0, p = translation;
4418 p->name != NULL;
4419 num_trans++, p++)
4424 const fs_operation_def_t *p;
4426 for (num_ops = 0, p = operations;
4427 p->name != NULL;
4428 num_ops++, p++)
4432 /* Walk through each operation known to our caller. There will be */
4433 /* one entry in the supplied "translation table" for each. */
4435 used = 0;
4437 for (i = 0; i < num_trans; i++) {
4438 int j, found;
4439 char *curname;
4440 fs_generic_func_p result;
4441 fs_generic_func_p *location;
4443 curname = translation[i].name;
4445 /* Look for a matching operation in the list supplied by the */
4446 /* file system. */
4448 found = 0;
4450 for (j = 0; j < num_ops; j++) {
4451 if (strcmp(operations[j].name, curname) == 0) {
4452 used++;
4453 found = 1;
4454 break;
4459 * If the file system is using a "placeholder" for default
4460 * or error functions, grab the appropriate function out of
4461 * the translation table. If the file system didn't supply
4462 * this operation at all, use the default function.
4465 if (found) {
4466 result = operations[j].func.fs_generic;
4467 if (result == fs_default) {
4468 result = translation[i].defaultFunc;
4469 } else if (result == fs_error) {
4470 result = translation[i].errorFunc;
4471 } else if (result == NULL) {
4472 /* Null values are PROHIBITED */
4473 return (EINVAL);
4475 } else {
4476 result = translation[i].defaultFunc;
4479 /* Now store the function into the operations vector. */
4481 location = (fs_generic_func_p *)
4482 (((char *)vector) + translation[i].offset);
4484 *location = result;
4487 *unused_ops = num_ops - used;
4489 return (0);
4492 /* Placeholder functions, should never be called. */
4495 fs_error(void)
4497 cmn_err(CE_PANIC, "fs_error called");
4498 return (0);
4502 fs_default(void)
4504 cmn_err(CE_PANIC, "fs_default called");
4505 return (0);
4508 #ifdef __sparc
4511 * Part of the implementation of booting off a mirrored root
4512 * involves a change of dev_t for the root device. To
4513 * accomplish this, first remove the existing hash table
4514 * entry for the root device, convert to the new dev_t,
4515 * then re-insert in the hash table at the head of the list.
4517 void
4518 vfs_root_redev(vfs_t *vfsp, dev_t ndev, int fstype)
4520 vfs_list_lock();
4522 vfs_hash_remove(vfsp);
4524 vfsp->vfs_dev = ndev;
4525 vfs_make_fsid(&vfsp->vfs_fsid, ndev, fstype);
4527 vfs_hash_add(vfsp, 1);
4529 vfs_list_unlock();
4532 #else /* x86 NEWBOOT */
4534 #if defined(__x86)
4535 extern int hvmboot_rootconf();
4536 #endif /* __x86 */
4538 extern ib_boot_prop_t *iscsiboot_prop;
4541 rootconf()
4543 int error;
4544 struct vfssw *vsw;
4545 extern void pm_init();
4546 char *fstyp, *fsmod;
4547 int ret = -1;
4549 getrootfs(&fstyp, &fsmod);
4551 #if defined(__x86)
4553 * hvmboot_rootconf() is defined in the hvm_bootstrap misc module,
4554 * which lives in /platform/i86hvm, and hence is only available when
4555 * booted in an x86 hvm environment. If the hvm_bootstrap misc module
4556 * is not available then the modstub for this function will return 0.
4557 * If the hvm_bootstrap misc module is available it will be loaded
4558 * and hvmboot_rootconf() will be invoked.
4560 if (error = hvmboot_rootconf())
4561 return (error);
4562 #endif /* __x86 */
4564 if (error = clboot_rootconf())
4565 return (error);
4567 if (modload("fs", fsmod) == -1)
4568 panic("Cannot _init %s module", fsmod);
4570 RLOCK_VFSSW();
4571 vsw = vfs_getvfsswbyname(fstyp);
4572 RUNLOCK_VFSSW();
4573 if (vsw == NULL) {
4574 cmn_err(CE_CONT, "Cannot find %s filesystem\n", fstyp);
4575 return (ENXIO);
4577 VFS_INIT(rootvfs, &vsw->vsw_vfsops, 0);
4578 VFS_HOLD(rootvfs);
4580 /* always mount readonly first */
4581 rootvfs->vfs_flag |= VFS_RDONLY;
4583 pm_init();
4585 if (netboot && iscsiboot_prop) {
4586 cmn_err(CE_WARN, "NFS boot and iSCSI boot"
4587 " shouldn't happen in the same time");
4588 return (EINVAL);
4591 if (netboot || iscsiboot_prop) {
4592 ret = strplumb();
4593 if (ret != 0) {
4594 cmn_err(CE_WARN, "Cannot plumb network device %d", ret);
4595 return (EFAULT);
4599 if ((ret == 0) && iscsiboot_prop) {
4600 ret = modload("drv", "iscsi");
4601 /* -1 indicates fail */
4602 if (ret == -1) {
4603 cmn_err(CE_WARN, "Failed to load iscsi module");
4604 iscsi_boot_prop_free();
4605 return (EINVAL);
4606 } else {
4607 if (!i_ddi_attach_pseudo_node("iscsi")) {
4608 cmn_err(CE_WARN,
4609 "Failed to attach iscsi driver");
4610 iscsi_boot_prop_free();
4611 return (ENODEV);
4616 error = VFS_MOUNTROOT(rootvfs, ROOT_INIT);
4617 vfs_unrefvfssw(vsw);
4618 rootdev = rootvfs->vfs_dev;
4620 if (error)
4621 cmn_err(CE_CONT, "Cannot mount root on %s fstype %s\n",
4622 rootfs.bo_name, fstyp);
4623 else
4624 cmn_err(CE_CONT, "?root on %s fstype %s\n",
4625 rootfs.bo_name, fstyp);
4626 return (error);
4630 * XXX this is called by nfs only and should probably be removed
4631 * If booted with ASKNAME, prompt on the console for a filesystem
4632 * name and return it.
4634 void
4635 getfsname(char *askfor, char *name, size_t namelen)
4637 if (boothowto & RB_ASKNAME) {
4638 printf("%s name: ", askfor);
4639 console_gets(name, namelen);
4644 * Init the root filesystem type (rootfs.bo_fstype) from the "fstype"
4645 * property.
4647 * Filesystem types starting with the prefix "nfs" are diskless clients;
4648 * init the root filename name (rootfs.bo_name), too.
4650 * If we are booting via NFS we currently have these options:
4651 * nfs - dynamically choose NFS V2, V3, or V4 (default)
4652 * nfs2 - force NFS V2
4653 * nfs3 - force NFS V3
4654 * nfs4 - force NFS V4
4655 * Because we need to maintain backward compatibility with the naming
4656 * convention that the NFS V2 filesystem name is "nfs" (see vfs_conf.c)
4657 * we need to map "nfs" => "nfsdyn" and "nfs2" => "nfs". The dynamic
4658 * nfs module will map the type back to either "nfs", "nfs3", or "nfs4".
4659 * This is only for root filesystems, all other uses will expect
4660 * that "nfs" == NFS V2.
4662 static void
4663 getrootfs(char **fstypp, char **fsmodp)
4665 extern char *strplumb_get_netdev_path(void);
4666 char *propstr = NULL;
4669 * Check fstype property; for diskless it should be one of "nfs",
4670 * "nfs2", "nfs3" or "nfs4".
4672 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4673 DDI_PROP_DONTPASS, "fstype", &propstr)
4674 == DDI_SUCCESS) {
4675 (void) strncpy(rootfs.bo_fstype, propstr, BO_MAXFSNAME);
4676 ddi_prop_free(propstr);
4679 * if the boot property 'fstype' is not set, but 'zfs-bootfs' is set,
4680 * assume the type of this root filesystem is 'zfs'.
4682 } else if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4683 DDI_PROP_DONTPASS, "zfs-bootfs", &propstr)
4684 == DDI_SUCCESS) {
4685 (void) strncpy(rootfs.bo_fstype, "zfs", BO_MAXFSNAME);
4686 ddi_prop_free(propstr);
4689 if (strncmp(rootfs.bo_fstype, "nfs", 3) != 0) {
4690 *fstypp = *fsmodp = rootfs.bo_fstype;
4691 return;
4694 ++netboot;
4696 if (strcmp(rootfs.bo_fstype, "nfs2") == 0)
4697 (void) strcpy(rootfs.bo_fstype, "nfs");
4698 else if (strcmp(rootfs.bo_fstype, "nfs") == 0)
4699 (void) strcpy(rootfs.bo_fstype, "nfsdyn");
4702 * check if path to network interface is specified in bootpath
4703 * or by a hypervisor domain configuration file.
4704 * XXPV - enable strlumb_get_netdev_path()
4706 if (ddi_prop_exists(DDI_DEV_T_ANY, ddi_root_node(), DDI_PROP_DONTPASS,
4707 "xpv-nfsroot")) {
4708 (void) strcpy(rootfs.bo_name, "/xpvd/xnf@0");
4709 } else if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4710 DDI_PROP_DONTPASS, "bootpath", &propstr)
4711 == DDI_SUCCESS) {
4712 (void) strncpy(rootfs.bo_name, propstr, BO_MAXOBJNAME);
4713 ddi_prop_free(propstr);
4714 } else {
4715 /* attempt to determine netdev_path via boot_mac address */
4716 netdev_path = strplumb_get_netdev_path();
4717 if (netdev_path == NULL)
4718 panic("cannot find boot network interface");
4719 (void) strncpy(rootfs.bo_name, netdev_path, BO_MAXOBJNAME);
4721 *fstypp = rootfs.bo_fstype;
4722 *fsmodp = "nfs";
4724 #endif
4727 * VFS feature routines
4730 #define VFTINDEX(feature) (((feature) >> 32) & 0xFFFFFFFF)
4731 #define VFTBITS(feature) ((feature) & 0xFFFFFFFFLL)
4733 /* Register a feature in the vfs */
4734 void
4735 vfs_set_feature(vfs_t *vfsp, vfs_feature_t feature)
4737 /* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4738 if (vfsp->vfs_implp == NULL)
4739 return;
4741 vfsp->vfs_featureset[VFTINDEX(feature)] |= VFTBITS(feature);
4744 void
4745 vfs_clear_feature(vfs_t *vfsp, vfs_feature_t feature)
4747 /* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4748 if (vfsp->vfs_implp == NULL)
4749 return;
4750 vfsp->vfs_featureset[VFTINDEX(feature)] &= VFTBITS(~feature);
4754 * Query a vfs for a feature.
4755 * Returns 1 if feature is present, 0 if not
4758 vfs_has_feature(vfs_t *vfsp, vfs_feature_t feature)
4760 int ret = 0;
4762 /* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4763 if (vfsp->vfs_implp == NULL)
4764 return (ret);
4766 if (vfsp->vfs_featureset[VFTINDEX(feature)] & VFTBITS(feature))
4767 ret = 1;
4769 return (ret);
4773 * Propagate feature set from one vfs to another
4775 void
4776 vfs_propagate_features(vfs_t *from, vfs_t *to)
4778 int i;
4780 if (to->vfs_implp == NULL || from->vfs_implp == NULL)
4781 return;
4783 for (i = 1; i <= to->vfs_featureset[0]; i++) {
4784 to->vfs_featureset[i] = from->vfs_featureset[i];
4788 #define LOFINODE_PATH "/dev/lofi/%d"
4791 * Return the vnode for the lofi node if there's a lofi mount in place.
4792 * Returns -1 when there's no lofi node, 0 on success, and > 0 on
4793 * failure.
4796 vfs_get_lofi(vfs_t *vfsp, vnode_t **vpp)
4798 char *path = NULL;
4799 int strsize;
4800 int err;
4802 if (vfsp->vfs_lofi_minor == 0) {
4803 *vpp = NULL;
4804 return (-1);
4807 strsize = snprintf(NULL, 0, LOFINODE_PATH, vfsp->vfs_lofi_minor);
4808 path = kmem_alloc(strsize + 1, KM_SLEEP);
4809 (void) snprintf(path, strsize + 1, LOFINODE_PATH, vfsp->vfs_lofi_minor);
4812 * We may be inside a zone, so we need to use the /dev path, but
4813 * it's created asynchronously, so we wait here.
4815 for (;;) {
4816 err = lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, vpp);
4818 if (err != ENOENT)
4819 break;
4821 if ((err = delay_sig(hz / 8)) == EINTR)
4822 break;
4825 if (err)
4826 *vpp = NULL;
4828 kmem_free(path, strsize + 1);
4829 return (err);