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]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/kernel.h>
30 #include <sys/sysmacros.h>
33 #include <sys/vnode.h>
35 #include <sys/mntent.h>
36 #include <sys/mount.h>
37 #include <sys/cmn_err.h>
38 #include <sys/zfs_znode.h>
39 #include <sys/zfs_dir.h>
41 #include <sys/fs/zfs.h>
43 #include <sys/dsl_prop.h>
44 #include <sys/dsl_dataset.h>
45 #include <sys/dsl_deleg.h>
48 #include <sys/varargs.h>
49 #include <sys/policy.h>
50 #include <sys/atomic.h>
51 #include <sys/zfs_ioctl.h>
52 #include <sys/zfs_ctldir.h>
53 #include <sys/zfs_fuid.h>
54 #include <sys/sunddi.h>
56 #include <sys/dmu_objset.h>
57 #include <sys/spa_boot.h>
58 #include <sys/vdev_impl.h> /* VDEV_BOOT_VERSION */
60 struct mtx zfs_debug_mtx;
61 MTX_SYSINIT(zfs_debug_mtx, &zfs_debug_mtx, "zfs_debug", MTX_DEF);
63 SYSCTL_NODE(_vfs, OID_AUTO, zfs, CTLFLAG_RW, 0, "ZFS file system");
65 int zfs_super_owner = 0;
66 SYSCTL_INT(_vfs_zfs, OID_AUTO, super_owner, CTLFLAG_RW, &zfs_super_owner, 0,
67 "File system owner can perform privileged operation on his file systems");
69 int zfs_debug_level = 0;
70 TUNABLE_INT("vfs.zfs.debug", &zfs_debug_level);
71 SYSCTL_INT(_vfs_zfs, OID_AUTO, debug, CTLFLAG_RW, &zfs_debug_level, 0,
74 SYSCTL_NODE(_vfs_zfs, OID_AUTO, version, CTLFLAG_RD, 0, "ZFS versions");
75 static int zfs_version_acl = ZFS_ACL_VERSION;
76 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, acl, CTLFLAG_RD, &zfs_version_acl, 0,
78 static int zfs_version_dmu_backup_header = DMU_BACKUP_HEADER_VERSION;
79 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, dmu_backup_header, CTLFLAG_RD,
80 &zfs_version_dmu_backup_header, 0, "DMU_BACKUP_HEADER_VERSION");
81 static int zfs_version_dmu_backup_stream = DMU_BACKUP_STREAM_VERSION;
82 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, dmu_backup_stream, CTLFLAG_RD,
83 &zfs_version_dmu_backup_stream, 0, "DMU_BACKUP_STREAM_VERSION");
84 static int zfs_version_spa = SPA_VERSION;
85 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, spa, CTLFLAG_RD, &zfs_version_spa, 0,
87 static int zfs_version_vdev_boot = VDEV_BOOT_VERSION;
88 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, vdev_boot, CTLFLAG_RD,
89 &zfs_version_vdev_boot, 0, "VDEV_BOOT_VERSION");
90 static int zfs_version_zpl = ZPL_VERSION;
91 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, zpl, CTLFLAG_RD, &zfs_version_zpl, 0,
94 static int zfs_mount(vfs_t *vfsp);
95 static int zfs_umount(vfs_t *vfsp, int fflag);
96 static int zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp);
97 static int zfs_statfs(vfs_t *vfsp, struct statfs *statp);
98 static int zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp);
99 static int zfs_sync(vfs_t *vfsp, int waitfor);
100 static int zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, vnode_t **vpp);
101 static void zfs_objset_close(zfsvfs_t *zfsvfs);
102 static void zfs_freevfs(vfs_t *vfsp);
104 static struct vfsops zfs_vfsops = {
105 .vfs_mount = zfs_mount,
106 .vfs_unmount = zfs_umount,
107 .vfs_root = zfs_root,
108 .vfs_statfs = zfs_statfs,
109 .vfs_vget = zfs_vget,
110 .vfs_sync = zfs_sync,
111 .vfs_fhtovp = zfs_fhtovp,
114 VFS_SET(zfs_vfsops, zfs, VFCF_JAIL | VFCF_DELEGADMIN);
117 * We need to keep a count of active fs's.
118 * This is necessary to prevent our module
119 * from being unloaded after a umount -f
121 static uint32_t zfs_active_fs_count = 0;
125 zfs_sync(vfs_t *vfsp, int waitfor)
129 * Data integrity is job one. We don't want a compromised kernel
130 * writing to the storage pool, so we never sync during panic.
137 * Sync a specific filesystem.
139 zfsvfs_t *zfsvfs = vfsp->vfs_data;
142 error = vfs_stdsync(vfsp, waitfor);
147 if (zfsvfs->z_log != NULL)
148 zil_commit(zfsvfs->z_log, UINT64_MAX, 0);
150 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
154 * Sync all ZFS filesystems. This is what happens when you
155 * run sync(1M). Unlike other filesystems, ZFS honors the
156 * request by waiting for all pools to commit all dirty data.
165 atime_changed_cb(void *arg, uint64_t newval)
167 zfsvfs_t *zfsvfs = arg;
169 if (newval == TRUE) {
170 zfsvfs->z_atime = TRUE;
171 zfsvfs->z_vfs->vfs_flag &= ~MNT_NOATIME;
172 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
173 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
175 zfsvfs->z_atime = FALSE;
176 zfsvfs->z_vfs->vfs_flag |= MNT_NOATIME;
177 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
178 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
183 xattr_changed_cb(void *arg, uint64_t newval)
185 zfsvfs_t *zfsvfs = arg;
187 if (newval == TRUE) {
188 /* XXX locking on vfs_flag? */
190 zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
192 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
193 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
195 /* XXX locking on vfs_flag? */
197 zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
199 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
200 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
205 blksz_changed_cb(void *arg, uint64_t newval)
207 zfsvfs_t *zfsvfs = arg;
209 if (newval < SPA_MINBLOCKSIZE ||
210 newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
211 newval = SPA_MAXBLOCKSIZE;
213 zfsvfs->z_max_blksz = newval;
214 zfsvfs->z_vfs->vfs_bsize = newval;
218 readonly_changed_cb(void *arg, uint64_t newval)
220 zfsvfs_t *zfsvfs = arg;
223 /* XXX locking on vfs_flag? */
224 zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
225 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
226 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
228 /* XXX locking on vfs_flag? */
229 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
230 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
231 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
236 setuid_changed_cb(void *arg, uint64_t newval)
238 zfsvfs_t *zfsvfs = arg;
240 if (newval == FALSE) {
241 zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
242 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
243 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
245 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
246 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
247 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
252 exec_changed_cb(void *arg, uint64_t newval)
254 zfsvfs_t *zfsvfs = arg;
256 if (newval == FALSE) {
257 zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
258 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
259 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
261 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
262 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
263 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
268 * The nbmand mount option can be changed at mount time.
269 * We can't allow it to be toggled on live file systems or incorrect
270 * behavior may be seen from cifs clients
272 * This property isn't registered via dsl_prop_register(), but this callback
273 * will be called when a file system is first mounted
276 nbmand_changed_cb(void *arg, uint64_t newval)
278 zfsvfs_t *zfsvfs = arg;
279 if (newval == FALSE) {
280 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
281 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
283 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
284 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
289 snapdir_changed_cb(void *arg, uint64_t newval)
291 zfsvfs_t *zfsvfs = arg;
293 zfsvfs->z_show_ctldir = newval;
297 vscan_changed_cb(void *arg, uint64_t newval)
299 zfsvfs_t *zfsvfs = arg;
301 zfsvfs->z_vscan = newval;
305 acl_mode_changed_cb(void *arg, uint64_t newval)
307 zfsvfs_t *zfsvfs = arg;
309 zfsvfs->z_acl_mode = newval;
313 acl_inherit_changed_cb(void *arg, uint64_t newval)
315 zfsvfs_t *zfsvfs = arg;
317 zfsvfs->z_acl_inherit = newval;
321 zfs_register_callbacks(vfs_t *vfsp)
323 struct dsl_dataset *ds = NULL;
325 zfsvfs_t *zfsvfs = NULL;
327 int readonly, do_readonly = FALSE;
328 int setuid, do_setuid = FALSE;
329 int exec, do_exec = FALSE;
330 int xattr, do_xattr = FALSE;
331 int atime, do_atime = FALSE;
335 zfsvfs = vfsp->vfs_data;
340 * The act of registering our callbacks will destroy any mount
341 * options we may have. In order to enable temporary overrides
342 * of mount options, we stash away the current values and
343 * restore them after we register the callbacks.
345 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
347 do_readonly = B_TRUE;
348 } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
350 do_readonly = B_TRUE;
352 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
356 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
359 } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
364 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
367 } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
371 if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
374 } else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
378 if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
381 } else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
387 * nbmand is a special property. It can only be changed at
390 * This is weird, but it is documented to only be changeable
393 if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
395 } else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
398 char osname[MAXNAMELEN];
400 dmu_objset_name(os, osname);
401 if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
408 * Register property callbacks.
410 * It would probably be fine to just check for i/o error from
411 * the first prop_register(), but I guess I like to go
414 ds = dmu_objset_ds(os);
415 error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
416 error = error ? error : dsl_prop_register(ds,
417 "xattr", xattr_changed_cb, zfsvfs);
418 error = error ? error : dsl_prop_register(ds,
419 "recordsize", blksz_changed_cb, zfsvfs);
420 error = error ? error : dsl_prop_register(ds,
421 "readonly", readonly_changed_cb, zfsvfs);
422 error = error ? error : dsl_prop_register(ds,
423 "setuid", setuid_changed_cb, zfsvfs);
424 error = error ? error : dsl_prop_register(ds,
425 "exec", exec_changed_cb, zfsvfs);
426 error = error ? error : dsl_prop_register(ds,
427 "snapdir", snapdir_changed_cb, zfsvfs);
428 error = error ? error : dsl_prop_register(ds,
429 "aclmode", acl_mode_changed_cb, zfsvfs);
430 error = error ? error : dsl_prop_register(ds,
431 "aclinherit", acl_inherit_changed_cb, zfsvfs);
432 error = error ? error : dsl_prop_register(ds,
433 "vscan", vscan_changed_cb, zfsvfs);
438 * Invoke our callbacks to restore temporary mount options.
441 readonly_changed_cb(zfsvfs, readonly);
443 setuid_changed_cb(zfsvfs, setuid);
445 exec_changed_cb(zfsvfs, exec);
447 xattr_changed_cb(zfsvfs, xattr);
449 atime_changed_cb(zfsvfs, atime);
451 nbmand_changed_cb(zfsvfs, nbmand);
457 * We may attempt to unregister some callbacks that are not
458 * registered, but this is OK; it will simply return ENOMSG,
459 * which we will ignore.
461 (void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
462 (void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
463 (void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
464 (void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
465 (void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
466 (void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
467 (void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
468 (void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
469 (void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
471 (void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
477 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
481 error = zfs_register_callbacks(zfsvfs->z_vfs);
486 * Set the objset user_ptr to track its zfsvfs.
488 mutex_enter(&zfsvfs->z_os->os->os_user_ptr_lock);
489 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
490 mutex_exit(&zfsvfs->z_os->os->os_user_ptr_lock);
493 * If we are not mounting (ie: online recv), then we don't
494 * have to worry about replaying the log as we blocked all
495 * operations out since we closed the ZIL.
501 * During replay we remove the read only flag to
502 * allow replays to succeed.
504 readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
505 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
508 * Parse and replay the intent log.
510 zil_replay(zfsvfs->z_os, zfsvfs, &zfsvfs->z_assign,
511 zfs_replay_vector, zfs_unlinked_drain);
513 zfs_unlinked_drain(zfsvfs);
514 zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
518 zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
524 zfs_freezfsvfs(zfsvfs_t *zfsvfs)
526 mutex_destroy(&zfsvfs->z_znodes_lock);
527 mutex_destroy(&zfsvfs->z_online_recv_lock);
528 list_destroy(&zfsvfs->z_all_znodes);
529 rrw_destroy(&zfsvfs->z_teardown_lock);
530 rw_destroy(&zfsvfs->z_teardown_inactive_lock);
531 rw_destroy(&zfsvfs->z_fuid_lock);
532 kmem_free(zfsvfs, sizeof (zfsvfs_t));
536 zfs_domount(vfs_t *vfsp, char *osname)
538 uint64_t recordsize, readonly;
548 * Initialize the zfs-specific filesystem structure.
549 * Should probably make this a kmem cache, shuffle fields,
550 * and just bzero up to z_hold_mtx[].
552 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
553 zfsvfs->z_vfs = vfsp;
554 zfsvfs->z_parent = zfsvfs;
555 zfsvfs->z_assign = TXG_NOWAIT;
556 zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
557 zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
559 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
560 mutex_init(&zfsvfs->z_online_recv_lock, NULL, MUTEX_DEFAULT, NULL);
561 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
562 offsetof(znode_t, z_link_node));
563 rrw_init(&zfsvfs->z_teardown_lock);
564 rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
565 rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
567 if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
570 zfsvfs->z_vfs->vfs_bsize = recordsize;
572 vfsp->vfs_data = zfsvfs;
573 vfsp->mnt_flag |= MNT_LOCAL;
574 vfsp->mnt_kern_flag |= MNTK_MPSAFE;
575 vfsp->mnt_kern_flag |= MNTK_LOOKUP_SHARED;
576 vfsp->mnt_kern_flag |= MNTK_SHARED_WRITES;
578 if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL))
581 mode = DS_MODE_OWNER;
583 mode |= DS_MODE_READONLY;
585 error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
586 if (error == EROFS) {
587 mode = DS_MODE_OWNER | DS_MODE_READONLY;
588 error = dmu_objset_open(osname, DMU_OST_ZFS, mode,
595 if (error = zfs_init_fs(zfsvfs, &zp))
599 * Set features for file system.
601 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
602 if (zfsvfs->z_use_fuids) {
603 vfs_set_feature(vfsp, VFSFT_XVATTR);
604 vfs_set_feature(vfsp, VFSFT_SYSATTR_VIEWS);
605 vfs_set_feature(vfsp, VFSFT_ACEMASKONACCESS);
606 vfs_set_feature(vfsp, VFSFT_ACLONCREATE);
608 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
609 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
610 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
611 vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
612 } else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
613 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
614 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
617 if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
620 ASSERT(mode & DS_MODE_READONLY);
621 atime_changed_cb(zfsvfs, B_FALSE);
622 readonly_changed_cb(zfsvfs, B_TRUE);
623 if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
625 xattr_changed_cb(zfsvfs, pval);
626 zfsvfs->z_issnap = B_TRUE;
628 error = zfsvfs_setup(zfsvfs, B_TRUE);
631 vfs_mountedfrom(vfsp, osname);
633 if (!zfsvfs->z_issnap)
634 zfsctl_create(zfsvfs);
638 dmu_objset_close(zfsvfs->z_os);
639 zfs_freezfsvfs(zfsvfs);
641 atomic_add_32(&zfs_active_fs_count, 1);
648 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
650 objset_t *os = zfsvfs->z_os;
651 struct dsl_dataset *ds;
654 * Unregister properties.
656 if (!dmu_objset_is_snapshot(os)) {
657 ds = dmu_objset_ds(os);
658 VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
661 VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
664 VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
667 VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
670 VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
673 VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
676 VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
679 VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
682 VERIFY(dsl_prop_unregister(ds, "aclinherit",
683 acl_inherit_changed_cb, zfsvfs) == 0);
685 VERIFY(dsl_prop_unregister(ds, "vscan",
686 vscan_changed_cb, zfsvfs) == 0);
692 zfs_mount(vfs_t *vfsp)
694 kthread_t *td = curthread;
695 vnode_t *mvp = vfsp->mnt_vnodecovered;
696 cred_t *cr = td->td_ucred;
701 if (vfs_getopt(vfsp->mnt_optnew, "from", (void **)&osname, NULL))
705 * If full-owner-access is enabled and delegated administration is
706 * turned on, we must set nosuid.
708 if (zfs_super_owner &&
709 dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != ECANCELED) {
710 secpolicy_fs_mount_clearopts(cr, vfsp);
714 * Check for mount privilege?
716 * If we don't have privilege then see if
717 * we have local permission to allow it
719 error = secpolicy_fs_mount(cr, mvp, vfsp);
721 error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr);
726 * Make sure user is the owner of the mount point
727 * or has sufficient privileges.
730 vattr.va_mask = AT_UID;
732 if (error = VOP_GETATTR(mvp, &vattr, cr)) {
736 #if 0 /* CHECK THIS! Is probably needed for zfs_suser. */
737 if (secpolicy_vnode_owner(mvp, cr, vattr.va_uid) != 0 &&
738 VOP_ACCESS(mvp, VWRITE, cr, td) != 0) {
743 if (error = secpolicy_vnode_owner(mvp, cr, vattr.va_uid)) {
747 if (error = VOP_ACCESS(mvp, VWRITE, cr, td)) {
752 secpolicy_fs_mount_clearopts(cr, vfsp);
759 * Refuse to mount a filesystem if we are in a local zone and the
760 * dataset is not visible.
762 if (!INGLOBALZONE(curthread) &&
763 (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
769 * When doing a remount, we simply refresh our temporary properties
770 * according to those options set in the current VFS options.
772 if (vfsp->vfs_flag & MS_REMOUNT) {
773 /* refresh mount options */
774 zfs_unregister_callbacks(vfsp->vfs_data);
775 error = zfs_register_callbacks(vfsp);
780 error = zfs_domount(vfsp, osname);
787 zfs_statfs(vfs_t *vfsp, struct statfs *statp)
789 zfsvfs_t *zfsvfs = vfsp->vfs_data;
790 uint64_t refdbytes, availbytes, usedobjs, availobjs;
792 statp->f_version = STATFS_VERSION;
796 dmu_objset_space(zfsvfs->z_os,
797 &refdbytes, &availbytes, &usedobjs, &availobjs);
800 * The underlying storage pool actually uses multiple block sizes.
801 * We report the fragsize as the smallest block size we support,
802 * and we report our blocksize as the filesystem's maximum blocksize.
804 statp->f_bsize = zfsvfs->z_vfs->vfs_bsize;
805 statp->f_iosize = zfsvfs->z_vfs->vfs_bsize;
808 * The following report "total" blocks of various kinds in the
809 * file system, but reported in terms of f_frsize - the
813 statp->f_blocks = (refdbytes + availbytes) / statp->f_bsize;
814 statp->f_bfree = availbytes / statp->f_bsize;
815 statp->f_bavail = statp->f_bfree; /* no root reservation */
818 * statvfs() should really be called statufs(), because it assumes
819 * static metadata. ZFS doesn't preallocate files, so the best
820 * we can do is report the max that could possibly fit in f_files,
821 * and that minus the number actually used in f_ffree.
822 * For f_ffree, report the smaller of the number of object available
823 * and the number of blocks (each object will take at least a block).
825 statp->f_ffree = MIN(availobjs, statp->f_bfree);
826 statp->f_files = statp->f_ffree + usedobjs;
829 * We're a zfs filesystem.
831 (void) strlcpy(statp->f_fstypename, "zfs", sizeof(statp->f_fstypename));
833 strlcpy(statp->f_mntfromname, vfsp->mnt_stat.f_mntfromname,
834 sizeof(statp->f_mntfromname));
835 strlcpy(statp->f_mntonname, vfsp->mnt_stat.f_mntonname,
836 sizeof(statp->f_mntonname));
838 statp->f_namemax = ZFS_MAXNAMELEN;
845 zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp)
847 zfsvfs_t *zfsvfs = vfsp->vfs_data;
853 error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
856 error = vn_lock(*vpp, flags);
857 (*vpp)->v_vflag |= VV_ROOT;
865 * Teardown the zfsvfs::z_os.
867 * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
868 * and 'z_teardown_inactive_lock' held.
871 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
875 rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
879 * We purge the parent filesystem's vfsp as the parent
880 * filesystem and all of its snapshots have their vnode's
881 * v_vfsp set to the parent's filesystem's vfsp. Note,
882 * 'z_parent' is self referential for non-snapshots.
884 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
888 * Close the zil. NB: Can't close the zil while zfs_inactive
889 * threads are blocked as zil_close can call zfs_inactive.
892 zil_close(zfsvfs->z_log);
893 zfsvfs->z_log = NULL;
896 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
899 * If we are not unmounting (ie: online recv) and someone already
900 * unmounted this file system while we were doing the switcheroo,
901 * or a reopen of z_os failed then just bail out now.
903 if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
904 rw_exit(&zfsvfs->z_teardown_inactive_lock);
905 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
910 * At this point there are no vops active, and any new vops will
911 * fail with EIO since we have z_teardown_lock for writer (only
912 * relavent for forced unmount).
914 * Release all holds on dbufs.
916 mutex_enter(&zfsvfs->z_znodes_lock);
917 for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
918 zp = list_next(&zfsvfs->z_all_znodes, zp))
920 ASSERT(ZTOV(zp)->v_count > 0);
921 zfs_znode_dmu_fini(zp);
923 mutex_exit(&zfsvfs->z_znodes_lock);
926 * If we are unmounting, set the unmounted flag and let new vops
927 * unblock. zfs_inactive will have the unmounted behavior, and all
928 * other vops will fail with EIO.
931 zfsvfs->z_unmounted = B_TRUE;
932 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
933 rw_exit(&zfsvfs->z_teardown_inactive_lock);
937 * z_os will be NULL if there was an error in attempting to reopen
938 * zfsvfs, so just return as the properties had already been
939 * unregistered and cached data had been evicted before.
941 if (zfsvfs->z_os == NULL)
945 * Unregister properties.
947 zfs_unregister_callbacks(zfsvfs);
952 if (dmu_objset_evict_dbufs(zfsvfs->z_os)) {
953 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
954 (void) dmu_objset_evict_dbufs(zfsvfs->z_os);
962 zfs_umount(vfs_t *vfsp, int fflag)
964 zfsvfs_t *zfsvfs = vfsp->vfs_data;
966 cred_t *cr = curthread->td_ucred;
969 if (fflag & MS_FORCE) {
970 /* TODO: Force unmount is not well implemented yet, so deny it. */
971 ZFS_LOG(0, "Force unmount is experimental - report any problems.");
974 ret = secpolicy_fs_unmount(cr, vfsp);
976 ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
977 ZFS_DELEG_PERM_MOUNT, cr);
982 * We purge the parent filesystem's vfsp as the parent filesystem
983 * and all of its snapshots have their vnode's v_vfsp set to the
984 * parent's filesystem's vfsp. Note, 'z_parent' is self
985 * referential for non-snapshots.
987 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
990 * Unmount any snapshots mounted under .zfs before unmounting the
993 if (zfsvfs->z_ctldir != NULL) {
994 if ((ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0)
996 ret = vflush(vfsp, 0, 0, curthread);
997 ASSERT(ret == EBUSY);
998 if (!(fflag & MS_FORCE)) {
999 if (zfsvfs->z_ctldir->v_count > 1)
1001 ASSERT(zfsvfs->z_ctldir->v_count == 1);
1003 zfsctl_destroy(zfsvfs);
1004 ASSERT(zfsvfs->z_ctldir == NULL);
1008 * Flush all the files.
1010 ret = vflush(vfsp, 1, (fflag & MS_FORCE) ? FORCECLOSE : 0, curthread);
1012 if (!zfsvfs->z_issnap) {
1013 zfsctl_create(zfsvfs);
1014 ASSERT(zfsvfs->z_ctldir != NULL);
1019 if (!(fflag & MS_FORCE)) {
1021 * Check the number of active vnodes in the file system.
1022 * Our count is maintained in the vfs structure, but the
1023 * number is off by 1 to indicate a hold on the vfs
1026 * The '.zfs' directory maintains a reference of its
1027 * own, and any active references underneath are
1028 * reflected in the vnode count.
1030 if (zfsvfs->z_ctldir == NULL) {
1031 if (vfsp->vfs_count > 1)
1034 if (vfsp->vfs_count > 2 ||
1035 zfsvfs->z_ctldir->v_count > 1)
1040 vfsp->mnt_kern_flag |= MNTK_UNMOUNTF;
1044 VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1048 * z_os will be NULL if there was an error in
1049 * attempting to reopen zfsvfs.
1053 * Unset the objset user_ptr.
1055 mutex_enter(&os->os->os_user_ptr_lock);
1056 dmu_objset_set_user(os, NULL);
1057 mutex_exit(&os->os->os_user_ptr_lock);
1060 * Finally release the objset
1062 dmu_objset_close(os);
1066 * We can now safely destroy the '.zfs' directory node.
1068 if (zfsvfs->z_ctldir != NULL)
1069 zfsctl_destroy(zfsvfs);
1070 if (zfsvfs->z_issnap) {
1071 vnode_t *svp = vfsp->mnt_vnodecovered;
1073 ASSERT(svp->v_count == 2 || svp->v_count == 1);
1074 if (svp->v_count == 2)
1083 zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp)
1085 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1090 err = zfs_zget(zfsvfs, ino, &zp);
1091 if (err == 0 && zp->z_unlinked) {
1099 vn_lock(*vpp, flags);
1106 zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, vnode_t **vpp)
1108 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1110 uint64_t object = 0;
1111 uint64_t fid_gen = 0;
1120 if (fidp->fid_len == LONG_FID_LEN) {
1121 zfid_long_t *zlfid = (zfid_long_t *)fidp;
1122 uint64_t objsetid = 0;
1123 uint64_t setgen = 0;
1125 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1126 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1128 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1129 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1133 err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1139 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1140 zfid_short_t *zfid = (zfid_short_t *)fidp;
1142 for (i = 0; i < sizeof (zfid->zf_object); i++)
1143 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1145 for (i = 0; i < sizeof (zfid->zf_gen); i++)
1146 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1152 /* A zero fid_gen means we are in the .zfs control directories */
1154 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1155 *vpp = zfsvfs->z_ctldir;
1156 ASSERT(*vpp != NULL);
1157 if (object == ZFSCTL_INO_SNAPDIR) {
1158 VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1159 0, NULL, NULL, NULL, NULL, NULL) == 0);
1164 /* XXX: LK_RETRY? */
1165 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1169 gen_mask = -1ULL >> (64 - 8 * i);
1171 dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1172 if (err = zfs_zget(zfsvfs, object, &zp)) {
1176 zp_gen = zp->z_phys->zp_gen & gen_mask;
1179 if (zp->z_unlinked || zp_gen != fid_gen) {
1180 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1187 /* XXX: LK_RETRY? */
1188 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1189 vnode_create_vobject(*vpp, zp->z_phys->zp_size, curthread);
1195 * Block out VOPs and close zfsvfs_t::z_os
1197 * Note, if successful, then we return with the 'z_teardown_lock' and
1198 * 'z_teardown_inactive_lock' write held.
1201 zfs_suspend_fs(zfsvfs_t *zfsvfs, char *name, int *mode)
1205 if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
1208 *mode = zfsvfs->z_os->os_mode;
1209 dmu_objset_name(zfsvfs->z_os, name);
1210 dmu_objset_close(zfsvfs->z_os);
1216 * Reopen zfsvfs_t::z_os and release VOPs.
1219 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname, int mode)
1223 ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
1224 ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
1226 err = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
1228 zfsvfs->z_os = NULL;
1232 VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
1235 * Attempt to re-establish all the active znodes with
1236 * their dbufs. If a zfs_rezget() fails, then we'll let
1237 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1238 * when they try to use their znode.
1240 mutex_enter(&zfsvfs->z_znodes_lock);
1241 for (zp = list_head(&zfsvfs->z_all_znodes); zp;
1242 zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1243 (void) zfs_rezget(zp);
1245 mutex_exit(&zfsvfs->z_znodes_lock);
1249 /* release the VOPs */
1250 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1251 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1255 * Since we couldn't reopen zfsvfs::z_os, force
1256 * unmount this file system.
1258 if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
1259 (void) dounmount(zfsvfs->z_vfs, MS_FORCE, curthread);
1265 zfs_freevfs(vfs_t *vfsp)
1267 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1270 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1271 mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1273 zfs_fuid_destroy(zfsvfs);
1274 zfs_freezfsvfs(zfsvfs);
1276 atomic_add_32(&zfs_active_fs_count, -1);
1280 static int desiredvnodes_backup;
1284 zfs_vnodes_adjust(void)
1287 int newdesiredvnodes;
1289 desiredvnodes_backup = desiredvnodes;
1292 * We calculate newdesiredvnodes the same way it is done in
1293 * vntblinit(). If it is equal to desiredvnodes, it means that
1294 * it wasn't tuned by the administrator and we can tune it down.
1296 newdesiredvnodes = min(maxproc + cnt.v_page_count / 4, 2 *
1297 vm_kmem_size / (5 * (sizeof(struct vm_object) +
1298 sizeof(struct vnode))));
1299 if (newdesiredvnodes == desiredvnodes)
1300 desiredvnodes = (3 * newdesiredvnodes) / 4;
1305 zfs_vnodes_adjust_back(void)
1309 desiredvnodes = desiredvnodes_backup;
1317 printf("ZFS filesystem version " SPA_VERSION_STRING "\n");
1320 * Initialize znode cache, vnode ops, etc...
1325 * Initialize .zfs directory structures
1330 * Reduce number of vnode. Originally number of vnodes is calculated
1331 * with UFS inode in mind. We reduce it here, because it's too big for
1334 zfs_vnodes_adjust();
1342 zfs_vnodes_adjust_back();
1348 return (zfs_active_fs_count != 0);
1352 zfs_set_version(const char *name, uint64_t newvers)
1360 * XXX for now, require that the filesystem be unmounted. Would
1361 * be nice to find the zfsvfs_t and just update that if
1365 if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
1368 error = dmu_objset_open(name, DMU_OST_ZFS, DS_MODE_OWNER, &os);
1372 error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1376 if (newvers < curvers) {
1381 tx = dmu_tx_create(os);
1382 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 0, ZPL_VERSION_STR);
1383 error = dmu_tx_assign(tx, TXG_WAIT);
1388 error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1,
1391 spa_history_internal_log(LOG_DS_UPGRADE,
1392 dmu_objset_spa(os), tx, CRED(),
1393 "oldver=%llu newver=%llu dataset = %llu", curvers, newvers,
1398 dmu_objset_close(os);
1402 * Read a property stored within the master node.
1405 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
1411 * Look up the file system's value for the property. For the
1412 * version property, we look up a slightly different string.
1414 if (prop == ZFS_PROP_VERSION)
1415 pname = ZPL_VERSION_STR;
1417 pname = zfs_prop_to_name(prop);
1420 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
1422 if (error == ENOENT) {
1423 /* No value set, use the default value */
1425 case ZFS_PROP_VERSION:
1426 *value = ZPL_VERSION;
1428 case ZFS_PROP_NORMALIZE:
1429 case ZFS_PROP_UTF8ONLY:
1433 *value = ZFS_CASE_SENSITIVE;