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_checkexp(vfs_t *vfsp, struct sockaddr *nam, int *extflagsp,
101 struct ucred **credanonp, int *numsecflavors, int **secflavors);
102 static int zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, vnode_t **vpp);
103 static void zfs_objset_close(zfsvfs_t *zfsvfs);
104 static void zfs_freevfs(vfs_t *vfsp);
106 static struct vfsops zfs_vfsops = {
107 .vfs_mount = zfs_mount,
108 .vfs_unmount = zfs_umount,
109 .vfs_root = zfs_root,
110 .vfs_statfs = zfs_statfs,
111 .vfs_vget = zfs_vget,
112 .vfs_sync = zfs_sync,
113 .vfs_checkexp = zfs_checkexp,
114 .vfs_fhtovp = zfs_fhtovp,
117 VFS_SET(zfs_vfsops, zfs, VFCF_JAIL | VFCF_DELEGADMIN);
120 * We need to keep a count of active fs's.
121 * This is necessary to prevent our module
122 * from being unloaded after a umount -f
124 static uint32_t zfs_active_fs_count = 0;
128 zfs_sync(vfs_t *vfsp, int waitfor)
132 * Data integrity is job one. We don't want a compromised kernel
133 * writing to the storage pool, so we never sync during panic.
140 * Sync a specific filesystem.
142 zfsvfs_t *zfsvfs = vfsp->vfs_data;
145 error = vfs_stdsync(vfsp, waitfor);
150 if (zfsvfs->z_log != NULL)
151 zil_commit(zfsvfs->z_log, UINT64_MAX, 0);
153 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
157 * Sync all ZFS filesystems. This is what happens when you
158 * run sync(1M). Unlike other filesystems, ZFS honors the
159 * request by waiting for all pools to commit all dirty data.
168 atime_changed_cb(void *arg, uint64_t newval)
170 zfsvfs_t *zfsvfs = arg;
172 if (newval == TRUE) {
173 zfsvfs->z_atime = TRUE;
174 zfsvfs->z_vfs->vfs_flag &= ~MNT_NOATIME;
175 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
176 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
178 zfsvfs->z_atime = FALSE;
179 zfsvfs->z_vfs->vfs_flag |= MNT_NOATIME;
180 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
181 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
186 xattr_changed_cb(void *arg, uint64_t newval)
188 zfsvfs_t *zfsvfs = arg;
190 if (newval == TRUE) {
191 /* XXX locking on vfs_flag? */
193 zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
195 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
196 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
198 /* XXX locking on vfs_flag? */
200 zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
202 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
203 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
208 blksz_changed_cb(void *arg, uint64_t newval)
210 zfsvfs_t *zfsvfs = arg;
212 if (newval < SPA_MINBLOCKSIZE ||
213 newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
214 newval = SPA_MAXBLOCKSIZE;
216 zfsvfs->z_max_blksz = newval;
217 zfsvfs->z_vfs->mnt_stat.f_iosize = newval;
221 readonly_changed_cb(void *arg, uint64_t newval)
223 zfsvfs_t *zfsvfs = arg;
226 /* XXX locking on vfs_flag? */
227 zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
228 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
229 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
231 /* XXX locking on vfs_flag? */
232 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
233 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
234 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
239 setuid_changed_cb(void *arg, uint64_t newval)
241 zfsvfs_t *zfsvfs = arg;
243 if (newval == FALSE) {
244 zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
245 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
246 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
248 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
249 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
250 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
255 exec_changed_cb(void *arg, uint64_t newval)
257 zfsvfs_t *zfsvfs = arg;
259 if (newval == FALSE) {
260 zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
261 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
262 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
264 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
265 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
266 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
271 * The nbmand mount option can be changed at mount time.
272 * We can't allow it to be toggled on live file systems or incorrect
273 * behavior may be seen from cifs clients
275 * This property isn't registered via dsl_prop_register(), but this callback
276 * will be called when a file system is first mounted
279 nbmand_changed_cb(void *arg, uint64_t newval)
281 zfsvfs_t *zfsvfs = arg;
282 if (newval == FALSE) {
283 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
284 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
286 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
287 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
292 snapdir_changed_cb(void *arg, uint64_t newval)
294 zfsvfs_t *zfsvfs = arg;
296 zfsvfs->z_show_ctldir = newval;
300 vscan_changed_cb(void *arg, uint64_t newval)
302 zfsvfs_t *zfsvfs = arg;
304 zfsvfs->z_vscan = newval;
308 acl_mode_changed_cb(void *arg, uint64_t newval)
310 zfsvfs_t *zfsvfs = arg;
312 zfsvfs->z_acl_mode = newval;
316 acl_inherit_changed_cb(void *arg, uint64_t newval)
318 zfsvfs_t *zfsvfs = arg;
320 zfsvfs->z_acl_inherit = newval;
324 zfs_register_callbacks(vfs_t *vfsp)
326 struct dsl_dataset *ds = NULL;
328 zfsvfs_t *zfsvfs = NULL;
330 int readonly, do_readonly = FALSE;
331 int setuid, do_setuid = FALSE;
332 int exec, do_exec = FALSE;
333 int xattr, do_xattr = FALSE;
334 int atime, do_atime = FALSE;
338 zfsvfs = vfsp->vfs_data;
343 * This function can be called for a snapshot when we update snapshot's
344 * mount point, which isn't really supported.
346 if (dmu_objset_is_snapshot(os))
350 * The act of registering our callbacks will destroy any mount
351 * options we may have. In order to enable temporary overrides
352 * of mount options, we stash away the current values and
353 * restore them after we register the callbacks.
355 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
357 do_readonly = B_TRUE;
358 } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
360 do_readonly = B_TRUE;
362 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
366 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
369 } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
374 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
377 } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
381 if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
384 } else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
388 if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
391 } else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
397 * nbmand is a special property. It can only be changed at
400 * This is weird, but it is documented to only be changeable
403 if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
405 } else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
408 char osname[MAXNAMELEN];
410 dmu_objset_name(os, osname);
411 if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
418 * Register property callbacks.
420 * It would probably be fine to just check for i/o error from
421 * the first prop_register(), but I guess I like to go
424 ds = dmu_objset_ds(os);
425 error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
426 error = error ? error : dsl_prop_register(ds,
427 "xattr", xattr_changed_cb, zfsvfs);
428 error = error ? error : dsl_prop_register(ds,
429 "recordsize", blksz_changed_cb, zfsvfs);
430 error = error ? error : dsl_prop_register(ds,
431 "readonly", readonly_changed_cb, zfsvfs);
432 error = error ? error : dsl_prop_register(ds,
433 "setuid", setuid_changed_cb, zfsvfs);
434 error = error ? error : dsl_prop_register(ds,
435 "exec", exec_changed_cb, zfsvfs);
436 error = error ? error : dsl_prop_register(ds,
437 "snapdir", snapdir_changed_cb, zfsvfs);
438 error = error ? error : dsl_prop_register(ds,
439 "aclmode", acl_mode_changed_cb, zfsvfs);
440 error = error ? error : dsl_prop_register(ds,
441 "aclinherit", acl_inherit_changed_cb, zfsvfs);
442 error = error ? error : dsl_prop_register(ds,
443 "vscan", vscan_changed_cb, zfsvfs);
448 * Invoke our callbacks to restore temporary mount options.
451 readonly_changed_cb(zfsvfs, readonly);
453 setuid_changed_cb(zfsvfs, setuid);
455 exec_changed_cb(zfsvfs, exec);
457 xattr_changed_cb(zfsvfs, xattr);
459 atime_changed_cb(zfsvfs, atime);
461 nbmand_changed_cb(zfsvfs, nbmand);
467 * We may attempt to unregister some callbacks that are not
468 * registered, but this is OK; it will simply return ENOMSG,
469 * which we will ignore.
471 (void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
472 (void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
473 (void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
474 (void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
475 (void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
476 (void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
477 (void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
478 (void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
479 (void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
481 (void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
487 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
491 error = zfs_register_callbacks(zfsvfs->z_vfs);
496 * Set the objset user_ptr to track its zfsvfs.
498 mutex_enter(&zfsvfs->z_os->os->os_user_ptr_lock);
499 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
500 mutex_exit(&zfsvfs->z_os->os->os_user_ptr_lock);
502 zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
504 zil_destroy(zfsvfs->z_log, B_FALSE);
505 zfsvfs->z_log = NULL;
509 * If we are not mounting (ie: online recv), then we don't
510 * have to worry about replaying the log as we blocked all
511 * operations out since we closed the ZIL.
517 * During replay we remove the read only flag to
518 * allow replays to succeed.
520 readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
522 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
524 zfs_unlinked_drain(zfsvfs);
528 * Parse and replay the intent log.
530 * Because of ziltest, this must be done after
531 * zfs_unlinked_drain(). (Further note: ziltest
532 * doesn't use readonly mounts, where
533 * zfs_unlinked_drain() isn't called.) This is because
534 * ziltest causes spa_sync() to think it's committed,
535 * but actually it is not, so the intent log contains
536 * many txg's worth of changes.
538 * In particular, if object N is in the unlinked set in
539 * the last txg to actually sync, then it could be
540 * actually freed in a later txg and then reallocated
541 * in a yet later txg. This would write a "create
542 * object N" record to the intent log. Normally, this
543 * would be fine because the spa_sync() would have
544 * written out the fact that object N is free, before
545 * we could write the "create object N" intent log
548 * But when we are in ziltest mode, we advance the "open
549 * txg" without actually spa_sync()-ing the changes to
550 * disk. So we would see that object N is still
551 * allocated and in the unlinked set, and there is an
552 * intent log record saying to allocate it.
554 zil_replay(zfsvfs->z_os, zfsvfs, &zfsvfs->z_assign,
555 zfs_replay_vector, zfs_unlinked_drain);
557 zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
564 zfs_freezfsvfs(zfsvfs_t *zfsvfs)
566 mutex_destroy(&zfsvfs->z_znodes_lock);
567 mutex_destroy(&zfsvfs->z_online_recv_lock);
568 list_destroy(&zfsvfs->z_all_znodes);
569 rrw_destroy(&zfsvfs->z_teardown_lock);
570 rw_destroy(&zfsvfs->z_teardown_inactive_lock);
571 rw_destroy(&zfsvfs->z_fuid_lock);
572 kmem_free(zfsvfs, sizeof (zfsvfs_t));
576 zfs_domount(vfs_t *vfsp, char *osname)
578 uint64_t recordsize, readonly;
588 * Initialize the zfs-specific filesystem structure.
589 * Should probably make this a kmem cache, shuffle fields,
590 * and just bzero up to z_hold_mtx[].
592 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
593 zfsvfs->z_vfs = vfsp;
594 zfsvfs->z_parent = zfsvfs;
595 zfsvfs->z_assign = TXG_NOWAIT;
596 zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
597 zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
599 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
600 mutex_init(&zfsvfs->z_online_recv_lock, NULL, MUTEX_DEFAULT, NULL);
601 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
602 offsetof(znode_t, z_link_node));
603 rrw_init(&zfsvfs->z_teardown_lock);
604 rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
605 rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
607 if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
610 zfsvfs->z_vfs->vfs_bsize = SPA_MINBLOCKSIZE;
611 zfsvfs->z_vfs->mnt_stat.f_iosize = recordsize;
613 vfsp->vfs_data = zfsvfs;
614 vfsp->mnt_flag |= MNT_LOCAL;
615 vfsp->mnt_kern_flag |= MNTK_MPSAFE;
616 vfsp->mnt_kern_flag |= MNTK_LOOKUP_SHARED;
617 vfsp->mnt_kern_flag |= MNTK_SHARED_WRITES;
619 if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL))
622 mode = DS_MODE_OWNER;
624 mode |= DS_MODE_READONLY;
626 error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
627 if (error == EROFS) {
628 mode = DS_MODE_OWNER | DS_MODE_READONLY;
629 error = dmu_objset_open(osname, DMU_OST_ZFS, mode,
636 if (error = zfs_init_fs(zfsvfs, &zp))
640 * Set features for file system.
642 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
643 if (zfsvfs->z_use_fuids) {
644 vfs_set_feature(vfsp, VFSFT_XVATTR);
645 vfs_set_feature(vfsp, VFSFT_SYSATTR_VIEWS);
646 vfs_set_feature(vfsp, VFSFT_ACEMASKONACCESS);
647 vfs_set_feature(vfsp, VFSFT_ACLONCREATE);
649 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
650 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
651 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
652 vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
653 } else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
654 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
655 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
658 if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
661 ASSERT(mode & DS_MODE_READONLY);
662 atime_changed_cb(zfsvfs, B_FALSE);
663 readonly_changed_cb(zfsvfs, B_TRUE);
664 if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
666 xattr_changed_cb(zfsvfs, pval);
667 zfsvfs->z_issnap = B_TRUE;
669 error = zfsvfs_setup(zfsvfs, B_TRUE);
672 vfs_mountedfrom(vfsp, osname);
674 if (!zfsvfs->z_issnap)
675 zfsctl_create(zfsvfs);
679 dmu_objset_close(zfsvfs->z_os);
680 zfs_freezfsvfs(zfsvfs);
682 atomic_add_32(&zfs_active_fs_count, 1);
689 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
691 objset_t *os = zfsvfs->z_os;
692 struct dsl_dataset *ds;
695 * Unregister properties.
697 if (!dmu_objset_is_snapshot(os)) {
698 ds = dmu_objset_ds(os);
699 VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
702 VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
705 VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
708 VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
711 VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
714 VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
717 VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
720 VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
723 VERIFY(dsl_prop_unregister(ds, "aclinherit",
724 acl_inherit_changed_cb, zfsvfs) == 0);
726 VERIFY(dsl_prop_unregister(ds, "vscan",
727 vscan_changed_cb, zfsvfs) == 0);
733 zfs_mount(vfs_t *vfsp)
735 kthread_t *td = curthread;
736 vnode_t *mvp = vfsp->mnt_vnodecovered;
737 cred_t *cr = td->td_ucred;
742 if (vfs_getopt(vfsp->mnt_optnew, "from", (void **)&osname, NULL))
746 * If full-owner-access is enabled and delegated administration is
747 * turned on, we must set nosuid.
749 if (zfs_super_owner &&
750 dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != ECANCELED) {
751 secpolicy_fs_mount_clearopts(cr, vfsp);
755 * Check for mount privilege?
757 * If we don't have privilege then see if
758 * we have local permission to allow it
760 error = secpolicy_fs_mount(cr, mvp, vfsp);
762 error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr);
766 if (!(vfsp->vfs_flag & MS_REMOUNT)) {
770 * Make sure user is the owner of the mount point
771 * or has sufficient privileges.
774 vattr.va_mask = AT_UID;
776 vn_lock(mvp, LK_SHARED | LK_RETRY);
777 if (error = VOP_GETATTR(mvp, &vattr, cr)) {
782 #if 0 /* CHECK THIS! Is probably needed for zfs_suser. */
783 if (secpolicy_vnode_owner(mvp, cr, vattr.va_uid) != 0 &&
784 VOP_ACCESS(mvp, VWRITE, cr, td) != 0) {
789 if (error = secpolicy_vnode_owner(mvp, cr, vattr.va_uid)) {
794 if (error = VOP_ACCESS(mvp, VWRITE, cr, td)) {
802 secpolicy_fs_mount_clearopts(cr, vfsp);
806 * Refuse to mount a filesystem if we are in a local zone and the
807 * dataset is not visible.
809 if (!INGLOBALZONE(curthread) &&
810 (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
816 * When doing a remount, we simply refresh our temporary properties
817 * according to those options set in the current VFS options.
819 if (vfsp->vfs_flag & MS_REMOUNT) {
820 /* refresh mount options */
821 zfs_unregister_callbacks(vfsp->vfs_data);
822 error = zfs_register_callbacks(vfsp);
827 error = zfs_domount(vfsp, osname);
834 zfs_statfs(vfs_t *vfsp, struct statfs *statp)
836 zfsvfs_t *zfsvfs = vfsp->vfs_data;
837 uint64_t refdbytes, availbytes, usedobjs, availobjs;
839 statp->f_version = STATFS_VERSION;
843 dmu_objset_space(zfsvfs->z_os,
844 &refdbytes, &availbytes, &usedobjs, &availobjs);
847 * The underlying storage pool actually uses multiple block sizes.
848 * We report the fragsize as the smallest block size we support,
849 * and we report our blocksize as the filesystem's maximum blocksize.
851 statp->f_bsize = SPA_MINBLOCKSIZE;
852 statp->f_iosize = zfsvfs->z_vfs->mnt_stat.f_iosize;
855 * The following report "total" blocks of various kinds in the
856 * file system, but reported in terms of f_frsize - the
860 statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
861 statp->f_bfree = availbytes / statp->f_bsize;
862 statp->f_bavail = statp->f_bfree; /* no root reservation */
865 * statvfs() should really be called statufs(), because it assumes
866 * static metadata. ZFS doesn't preallocate files, so the best
867 * we can do is report the max that could possibly fit in f_files,
868 * and that minus the number actually used in f_ffree.
869 * For f_ffree, report the smaller of the number of object available
870 * and the number of blocks (each object will take at least a block).
872 statp->f_ffree = MIN(availobjs, statp->f_bfree);
873 statp->f_files = statp->f_ffree + usedobjs;
876 * We're a zfs filesystem.
878 (void) strlcpy(statp->f_fstypename, "zfs", sizeof(statp->f_fstypename));
880 strlcpy(statp->f_mntfromname, vfsp->mnt_stat.f_mntfromname,
881 sizeof(statp->f_mntfromname));
882 strlcpy(statp->f_mntonname, vfsp->mnt_stat.f_mntonname,
883 sizeof(statp->f_mntonname));
885 statp->f_namemax = ZFS_MAXNAMELEN;
892 zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp)
894 zfsvfs_t *zfsvfs = vfsp->vfs_data;
898 ZFS_ENTER_NOERROR(zfsvfs);
900 error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
906 error = vn_lock(*vpp, flags);
907 (*vpp)->v_vflag |= VV_ROOT;
914 * Teardown the zfsvfs::z_os.
916 * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
917 * and 'z_teardown_inactive_lock' held.
920 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
924 rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
928 * We purge the parent filesystem's vfsp as the parent
929 * filesystem and all of its snapshots have their vnode's
930 * v_vfsp set to the parent's filesystem's vfsp. Note,
931 * 'z_parent' is self referential for non-snapshots.
933 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
934 #ifdef FREEBSD_NAMECACHE
935 cache_purgevfs(zfsvfs->z_parent->z_vfs);
940 * Close the zil. NB: Can't close the zil while zfs_inactive
941 * threads are blocked as zil_close can call zfs_inactive.
944 zil_close(zfsvfs->z_log);
945 zfsvfs->z_log = NULL;
948 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
951 * If we are not unmounting (ie: online recv) and someone already
952 * unmounted this file system while we were doing the switcheroo,
953 * or a reopen of z_os failed then just bail out now.
955 if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
956 rw_exit(&zfsvfs->z_teardown_inactive_lock);
957 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
962 * At this point there are no vops active, and any new vops will
963 * fail with EIO since we have z_teardown_lock for writer (only
964 * relavent for forced unmount).
966 * Release all holds on dbufs.
968 mutex_enter(&zfsvfs->z_znodes_lock);
969 for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
970 zp = list_next(&zfsvfs->z_all_znodes, zp))
972 ASSERT(ZTOV(zp)->v_count >= 0);
973 zfs_znode_dmu_fini(zp);
975 mutex_exit(&zfsvfs->z_znodes_lock);
978 * If we are unmounting, set the unmounted flag and let new vops
979 * unblock. zfs_inactive will have the unmounted behavior, and all
980 * other vops will fail with EIO.
983 zfsvfs->z_unmounted = B_TRUE;
984 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
985 rw_exit(&zfsvfs->z_teardown_inactive_lock);
989 * Some znodes might not be fully reclaimed, wait for them.
991 mutex_enter(&zfsvfs->z_znodes_lock);
992 while (list_head(&zfsvfs->z_all_znodes) != NULL) {
993 msleep(zfsvfs, &zfsvfs->z_znodes_lock, 0,
996 mutex_exit(&zfsvfs->z_znodes_lock);
1001 * z_os will be NULL if there was an error in attempting to reopen
1002 * zfsvfs, so just return as the properties had already been
1003 * unregistered and cached data had been evicted before.
1005 if (zfsvfs->z_os == NULL)
1009 * Unregister properties.
1011 zfs_unregister_callbacks(zfsvfs);
1016 if (dmu_objset_evict_dbufs(zfsvfs->z_os)) {
1017 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1018 (void) dmu_objset_evict_dbufs(zfsvfs->z_os);
1026 zfs_umount(vfs_t *vfsp, int fflag)
1028 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1030 cred_t *cr = curthread->td_ucred;
1033 ret = secpolicy_fs_unmount(cr, vfsp);
1035 ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
1036 ZFS_DELEG_PERM_MOUNT, cr);
1041 * We purge the parent filesystem's vfsp as the parent filesystem
1042 * and all of its snapshots have their vnode's v_vfsp set to the
1043 * parent's filesystem's vfsp. Note, 'z_parent' is self
1044 * referential for non-snapshots.
1046 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1049 * Unmount any snapshots mounted under .zfs before unmounting the
1052 if (zfsvfs->z_ctldir != NULL) {
1053 if ((ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0)
1055 ret = vflush(vfsp, 0, 0, curthread);
1056 ASSERT(ret == EBUSY);
1057 if (!(fflag & MS_FORCE)) {
1058 if (zfsvfs->z_ctldir->v_count > 1)
1060 ASSERT(zfsvfs->z_ctldir->v_count == 1);
1062 zfsctl_destroy(zfsvfs);
1063 ASSERT(zfsvfs->z_ctldir == NULL);
1066 if (fflag & MS_FORCE) {
1068 * Mark file system as unmounted before calling
1069 * vflush(FORCECLOSE). This way we ensure no future vnops
1070 * will be called and risk operating on DOOMED vnodes.
1072 rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1073 zfsvfs->z_unmounted = B_TRUE;
1074 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1078 * Flush all the files.
1080 ret = vflush(vfsp, 1, (fflag & MS_FORCE) ? FORCECLOSE : 0, curthread);
1082 if (!zfsvfs->z_issnap) {
1083 zfsctl_create(zfsvfs);
1084 ASSERT(zfsvfs->z_ctldir != NULL);
1089 if (!(fflag & MS_FORCE)) {
1091 * Check the number of active vnodes in the file system.
1092 * Our count is maintained in the vfs structure, but the
1093 * number is off by 1 to indicate a hold on the vfs
1096 * The '.zfs' directory maintains a reference of its
1097 * own, and any active references underneath are
1098 * reflected in the vnode count.
1100 if (zfsvfs->z_ctldir == NULL) {
1101 if (vfsp->vfs_count > 1)
1104 if (vfsp->vfs_count > 2 ||
1105 zfsvfs->z_ctldir->v_count > 1)
1110 vfsp->mnt_kern_flag |= MNTK_UNMOUNTF;
1114 VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1118 * z_os will be NULL if there was an error in
1119 * attempting to reopen zfsvfs.
1123 * Unset the objset user_ptr.
1125 mutex_enter(&os->os->os_user_ptr_lock);
1126 dmu_objset_set_user(os, NULL);
1127 mutex_exit(&os->os->os_user_ptr_lock);
1130 * Finally release the objset
1132 dmu_objset_close(os);
1136 * We can now safely destroy the '.zfs' directory node.
1138 if (zfsvfs->z_ctldir != NULL)
1139 zfsctl_destroy(zfsvfs);
1140 if (zfsvfs->z_issnap) {
1141 vnode_t *svp = vfsp->mnt_vnodecovered;
1143 if (svp->v_count >= 2)
1152 zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp)
1154 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1159 * XXXPJD: zfs_zget() can't operate on virtual entires like .zfs/ or
1160 * .zfs/snapshot/ directories, so for now just return EOPNOTSUPP.
1161 * This will make NFS to fall back to using READDIR instead of
1163 * Also snapshots are stored in AVL tree, but based on their names,
1164 * not inode numbers, so it will be very inefficient to iterate
1165 * over all snapshots to find the right one.
1166 * Note that OpenSolaris READDIRPLUS implementation does LOOKUP on
1167 * d_name, and not VGET on d_fileno as we do.
1169 if (ino == ZFSCTL_INO_ROOT || ino == ZFSCTL_INO_SNAPDIR)
1170 return (EOPNOTSUPP);
1173 err = zfs_zget(zfsvfs, ino, &zp);
1174 if (err == 0 && zp->z_unlinked) {
1183 vn_lock(*vpp, flags);
1189 zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, int *extflagsp,
1190 struct ucred **credanonp, int *numsecflavors, int **secflavors)
1192 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1195 * If this is regular file system vfsp is the same as
1196 * zfsvfs->z_parent->z_vfs, but if it is snapshot,
1197 * zfsvfs->z_parent->z_vfs represents parent file system
1198 * which we have to use here, because only this file system
1199 * has mnt_export configured.
1201 vfsp = zfsvfs->z_parent->z_vfs;
1203 return (vfs_stdcheckexp(zfsvfs->z_parent->z_vfs, nam, extflagsp,
1204 credanonp, numsecflavors, secflavors));
1207 CTASSERT(SHORT_FID_LEN <= sizeof(struct fid));
1208 CTASSERT(LONG_FID_LEN <= sizeof(struct fid));
1211 zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, vnode_t **vpp)
1213 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1215 uint64_t object = 0;
1216 uint64_t fid_gen = 0;
1226 * On FreeBSD we can get snapshot's mount point or its parent file
1227 * system mount point depending if snapshot is already mounted or not.
1229 if (zfsvfs->z_parent == zfsvfs && fidp->fid_len == LONG_FID_LEN) {
1230 zfid_long_t *zlfid = (zfid_long_t *)fidp;
1231 uint64_t objsetid = 0;
1232 uint64_t setgen = 0;
1234 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1235 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1237 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1238 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1242 err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1248 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1249 zfid_short_t *zfid = (zfid_short_t *)fidp;
1251 for (i = 0; i < sizeof (zfid->zf_object); i++)
1252 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1254 for (i = 0; i < sizeof (zfid->zf_gen); i++)
1255 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1261 /* A zero fid_gen means we are in the .zfs control directories */
1263 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1264 *vpp = zfsvfs->z_ctldir;
1265 ASSERT(*vpp != NULL);
1266 if (object == ZFSCTL_INO_SNAPDIR) {
1267 VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1268 0, NULL, NULL, NULL, NULL, NULL) == 0);
1273 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1277 gen_mask = -1ULL >> (64 - 8 * i);
1279 dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1280 if (err = zfs_zget(zfsvfs, object, &zp)) {
1284 zp_gen = zp->z_phys->zp_gen & gen_mask;
1287 if (zp->z_unlinked || zp_gen != fid_gen) {
1288 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1297 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1298 vnode_create_vobject(*vpp, zp->z_phys->zp_size, curthread);
1303 * Block out VOPs and close zfsvfs_t::z_os
1305 * Note, if successful, then we return with the 'z_teardown_lock' and
1306 * 'z_teardown_inactive_lock' write held.
1309 zfs_suspend_fs(zfsvfs_t *zfsvfs, char *name, int *mode)
1313 if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
1316 *mode = zfsvfs->z_os->os_mode;
1317 dmu_objset_name(zfsvfs->z_os, name);
1318 dmu_objset_close(zfsvfs->z_os);
1324 * Reopen zfsvfs_t::z_os and release VOPs.
1327 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname, int mode)
1331 ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
1332 ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
1334 err = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
1336 zfsvfs->z_os = NULL;
1340 VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
1343 * Attempt to re-establish all the active znodes with
1344 * their dbufs. If a zfs_rezget() fails, then we'll let
1345 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1346 * when they try to use their znode.
1348 mutex_enter(&zfsvfs->z_znodes_lock);
1349 for (zp = list_head(&zfsvfs->z_all_znodes); zp;
1350 zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1351 (void) zfs_rezget(zp);
1353 mutex_exit(&zfsvfs->z_znodes_lock);
1357 /* release the VOPs */
1358 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1359 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1363 * Since we couldn't reopen zfsvfs::z_os, force
1364 * unmount this file system.
1366 if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
1367 (void) dounmount(zfsvfs->z_vfs, MS_FORCE, curthread);
1373 zfs_freevfs(vfs_t *vfsp)
1375 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1378 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1379 mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1381 zfs_fuid_destroy(zfsvfs);
1382 zfs_freezfsvfs(zfsvfs);
1384 atomic_add_32(&zfs_active_fs_count, -1);
1388 static int desiredvnodes_backup;
1392 zfs_vnodes_adjust(void)
1395 int newdesiredvnodes;
1397 desiredvnodes_backup = desiredvnodes;
1400 * We calculate newdesiredvnodes the same way it is done in
1401 * vntblinit(). If it is equal to desiredvnodes, it means that
1402 * it wasn't tuned by the administrator and we can tune it down.
1404 newdesiredvnodes = min(maxproc + cnt.v_page_count / 4, 2 *
1405 vm_kmem_size / (5 * (sizeof(struct vm_object) +
1406 sizeof(struct vnode))));
1407 if (newdesiredvnodes == desiredvnodes)
1408 desiredvnodes = (3 * newdesiredvnodes) / 4;
1413 zfs_vnodes_adjust_back(void)
1417 desiredvnodes = desiredvnodes_backup;
1425 printf("ZFS filesystem version " ZPL_VERSION_STRING "\n");
1428 * Initialize znode cache, vnode ops, etc...
1433 * Initialize .zfs directory structures
1438 * Reduce number of vnode. Originally number of vnodes is calculated
1439 * with UFS inode in mind. We reduce it here, because it's too big for
1442 zfs_vnodes_adjust();
1450 zfs_vnodes_adjust_back();
1456 return (zfs_active_fs_count != 0);
1460 zfs_set_version(const char *name, uint64_t newvers)
1468 * XXX for now, require that the filesystem be unmounted. Would
1469 * be nice to find the zfsvfs_t and just update that if
1473 if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
1476 error = dmu_objset_open(name, DMU_OST_ZFS, DS_MODE_OWNER, &os);
1480 error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1484 if (newvers < curvers) {
1489 tx = dmu_tx_create(os);
1490 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 0, ZPL_VERSION_STR);
1491 error = dmu_tx_assign(tx, TXG_WAIT);
1496 error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1,
1499 spa_history_internal_log(LOG_DS_UPGRADE,
1500 dmu_objset_spa(os), tx, CRED(),
1501 "oldver=%llu newver=%llu dataset = %llu", curvers, newvers,
1506 dmu_objset_close(os);
1510 * Read a property stored within the master node.
1513 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
1519 * Look up the file system's value for the property. For the
1520 * version property, we look up a slightly different string.
1522 if (prop == ZFS_PROP_VERSION)
1523 pname = ZPL_VERSION_STR;
1525 pname = zfs_prop_to_name(prop);
1528 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
1530 if (error == ENOENT) {
1531 /* No value set, use the default value */
1533 case ZFS_PROP_VERSION:
1534 *value = ZPL_VERSION;
1536 case ZFS_PROP_NORMALIZE:
1537 case ZFS_PROP_UTF8ONLY:
1541 *value = ZFS_CASE_SENSITIVE;