]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/zfs_vfsops.c
port async unlinked drain from illumos-nexenta
[mirror_zfs.git] / module / zfs / zfs_vfsops.c
index 2577860acb0e6f5c96bd2ed474321420b3d16c14..cdc1bc70758136fb47c2bb68dded69cf7d432f4b 100644 (file)
  */
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
- * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
+ * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
  */
 
 /* Portions Copyright 2010 Robert Milkowski */
 
 #include <sys/types.h>
 #include <sys/param.h>
-#include <sys/systm.h>
 #include <sys/sysmacros.h>
 #include <sys/kmem.h>
 #include <sys/pathname.h>
 #include <sys/vnode.h>
 #include <sys/vfs.h>
-#include <sys/vfs_opreg.h>
 #include <sys/mntent.h>
-#include <sys/mount.h>
 #include <sys/cmn_err.h>
-#include "fs/fs_subr.h"
 #include <sys/zfs_znode.h>
 #include <sys/zfs_vnops.h>
 #include <sys/zfs_dir.h>
 #include <sys/zap.h>
 #include <sys/sa.h>
 #include <sys/sa_impl.h>
-#include <sys/varargs.h>
 #include <sys/policy.h>
 #include <sys/atomic.h>
-#include <sys/mkdev.h>
-#include <sys/modctl.h>
-#include <sys/refstr.h>
 #include <sys/zfs_ioctl.h>
 #include <sys/zfs_ctldir.h>
 #include <sys/zfs_fuid.h>
-#include <sys/bootconf.h>
 #include <sys/sunddi.h>
-#include <sys/dnlc.h>
 #include <sys/dmu_objset.h>
 #include <sys/spa_boot.h>
 #include <sys/zpl.h>
+#include <linux/vfs_compat.h>
 #include "zfs_comutil.h"
 
 enum {
@@ -259,7 +250,7 @@ zfsvfs_parse_options(char *mntopts, vfs_t **vfsp)
 boolean_t
 zfs_is_readonly(zfsvfs_t *zfsvfs)
 {
-       return (!!(zfsvfs->z_sb->s_flags & MS_RDONLY));
+       return (!!(zfsvfs->z_sb->s_flags & SB_RDONLY));
 }
 
 /*ARGSUSED*/
@@ -268,13 +259,6 @@ zfs_sync(struct super_block *sb, int wait, cred_t *cr)
 {
        zfsvfs_t *zfsvfs = sb->s_fs_info;
 
-       /*
-        * Data integrity is job one.  We don't want a compromised kernel
-        * writing to the storage pool, so we never sync during panic.
-        */
-       if (unlikely(oops_in_progress))
-               return (0);
-
        /*
         * Semantically, the only requirement is that the sync be initiated.
         * The DMU syncs out txgs frequently, so there's nothing to do.
@@ -353,15 +337,15 @@ acltype_changed_cb(void *arg, uint64_t newval)
        switch (newval) {
        case ZFS_ACLTYPE_OFF:
                zfsvfs->z_acl_type = ZFS_ACLTYPE_OFF;
-               zfsvfs->z_sb->s_flags &= ~MS_POSIXACL;
+               zfsvfs->z_sb->s_flags &= ~SB_POSIXACL;
                break;
        case ZFS_ACLTYPE_POSIXACL:
 #ifdef CONFIG_FS_POSIX_ACL
                zfsvfs->z_acl_type = ZFS_ACLTYPE_POSIXACL;
-               zfsvfs->z_sb->s_flags |= MS_POSIXACL;
+               zfsvfs->z_sb->s_flags |= SB_POSIXACL;
 #else
                zfsvfs->z_acl_type = ZFS_ACLTYPE_OFF;
-               zfsvfs->z_sb->s_flags &= ~MS_POSIXACL;
+               zfsvfs->z_sb->s_flags &= ~SB_POSIXACL;
 #endif /* CONFIG_FS_POSIX_ACL */
                break;
        default:
@@ -390,9 +374,9 @@ readonly_changed_cb(void *arg, uint64_t newval)
                return;
 
        if (newval)
-               sb->s_flags |= MS_RDONLY;
+               sb->s_flags |= SB_RDONLY;
        else
-               sb->s_flags &= ~MS_RDONLY;
+               sb->s_flags &= ~SB_RDONLY;
 }
 
 static void
@@ -420,9 +404,9 @@ nbmand_changed_cb(void *arg, uint64_t newval)
                return;
 
        if (newval == TRUE)
-               sb->s_flags |= MS_MANDLOCK;
+               sb->s_flags |= SB_MANDLOCK;
        else
-               sb->s_flags &= ~MS_MANDLOCK;
+               sb->s_flags &= ~SB_MANDLOCK;
 }
 
 static void
@@ -1194,6 +1178,10 @@ zfsvfs_create_impl(zfsvfs_t **zfvp, zfsvfs_t *zfsvfs, objset_t *os)
                return (error);
        }
 
+       zfsvfs->z_drain_task = TASKQID_INVALID;
+       zfsvfs->z_draining = B_FALSE;
+       zfsvfs->z_drain_cancel = B_TRUE;
+
        *zfvp = zfsvfs;
        return (0);
 }
@@ -1216,14 +1204,27 @@ zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
         * operations out since we closed the ZIL.
         */
        if (mounting) {
+               ASSERT3P(zfsvfs->z_kstat.dk_kstats, ==, NULL);
+               dataset_kstats_create(&zfsvfs->z_kstat, zfsvfs->z_os);
+
                /*
                 * During replay we remove the read only flag to
                 * allow replays to succeed.
                 */
-               if (readonly != 0)
+               if (readonly != 0) {
                        readonly_changed_cb(zfsvfs, B_FALSE);
-               else
+               } else {
+                       zap_stats_t zs;
+                       if (zap_get_stats(zfsvfs->z_os, zfsvfs->z_unlinkedobj,
+                           &zs) == 0) {
+                               dataset_kstats_update_nunlinks_kstat(
+                                   &zfsvfs->z_kstat, zs.zs_num_entries);
+                       }
+                       dprintf_ds(zfsvfs->z_os->os_dsl_dataset,
+                           "num_entries in unlinked set: %llu",
+                           zs.zs_num_entries);
                        zfs_unlinked_drain(zfsvfs);
+               }
 
                /*
                 * Parse and replay the intent log.
@@ -1298,6 +1299,7 @@ zfsvfs_free(zfsvfs_t *zfsvfs)
        vmem_free(zfsvfs->z_hold_trees, sizeof (avl_tree_t) * size);
        vmem_free(zfsvfs->z_hold_locks, sizeof (kmutex_t) * size);
        zfsvfs_vfs_free(zfsvfs->z_vfs);
+       dataset_kstats_destroy(&zfsvfs->z_kstat);
        kmem_free(zfsvfs, sizeof (zfsvfs_t));
 }
 
@@ -1428,8 +1430,6 @@ zfs_statvfs(struct dentry *dentry, struct kstatfs *statp)
 {
        zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info;
        uint64_t refdbytes, availbytes, usedobjs, availobjs;
-       uint64_t fsid;
-       uint32_t bshift;
        int err = 0;
 
        ZFS_ENTER(zfsvfs);
@@ -1437,7 +1437,7 @@ zfs_statvfs(struct dentry *dentry, struct kstatfs *statp)
        dmu_objset_space(zfsvfs->z_os,
            &refdbytes, &availbytes, &usedobjs, &availobjs);
 
-       fsid = dmu_objset_fsid_guid(zfsvfs->z_os);
+       uint64_t fsid = dmu_objset_fsid_guid(zfsvfs->z_os);
        /*
         * The underlying storage pool actually uses multiple block
         * size.  Under Solaris frsize (fragment size) is reported as
@@ -1449,7 +1449,7 @@ zfs_statvfs(struct dentry *dentry, struct kstatfs *statp)
         */
        statp->f_frsize = zfsvfs->z_max_blksz;
        statp->f_bsize = zfsvfs->z_max_blksz;
-       bshift = fls(statp->f_bsize) - 1;
+       uint32_t bshift = fls(statp->f_bsize) - 1;
 
        /*
         * The following report "total" blocks of various kinds in
@@ -1457,6 +1457,8 @@ zfs_statvfs(struct dentry *dentry, struct kstatfs *statp)
         * "preferred" size.
         */
 
+       /* Round up so we never have a filesytem using 0 blocks. */
+       refdbytes = P2ROUNDUP(refdbytes, statp->f_bsize);
        statp->f_blocks = (refdbytes + availbytes) >> bshift;
        statp->f_bfree = availbytes >> bshift;
        statp->f_bavail = statp->f_bfree; /* no root reservation */
@@ -1466,7 +1468,7 @@ zfs_statvfs(struct dentry *dentry, struct kstatfs *statp)
         * static metadata.  ZFS doesn't preallocate files, so the best
         * we can do is report the max that could possibly fit in f_files,
         * and that minus the number actually used in f_ffree.
-        * For f_ffree, report the smaller of the number of object available
+        * For f_ffree, report the smaller of the number of objects available
         * and the number of blocks (each object will take at least a block).
         */
        statp->f_ffree = MIN(availobjs, availbytes >> DNODE_SHIFT);
@@ -1645,6 +1647,8 @@ zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
 {
        znode_t *zp;
 
+       zfs_unlinked_drain_stop_wait(zfsvfs);
+
        /*
         * If someone has not already unmounted this file system,
         * drain the iput_taskq to ensure all active references to the
@@ -1751,10 +1755,10 @@ zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
        zfs_unregister_callbacks(zfsvfs);
 
        /*
-        * Evict cached data
+        * Evict cached data. We must write out any dirty data before
+        * disowning the dataset.
         */
-       if (dsl_dataset_is_dirty(dmu_objset_ds(zfsvfs->z_os)) &&
-           !zfs_is_readonly(zfsvfs))
+       if (!zfs_is_readonly(zfsvfs))
                txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
        dmu_objset_evict_dbufs(zfsvfs->z_os);
 
@@ -1896,6 +1900,7 @@ zfs_preumount(struct super_block *sb)
 
        /* zfsvfs is NULL when zfs_domount fails during mount */
        if (zfsvfs) {
+               zfs_unlinked_drain_stop_wait(zfsvfs);
                zfsctl_destroy(sb->s_fs_info);
                /*
                 * Wait for iput_async before entering evict_inodes in
@@ -1967,8 +1972,8 @@ zfs_remount(struct super_block *sb, int *flags, zfs_mnt_t *zm)
        int error;
 
        if ((issnap || !spa_writeable(dmu_objset_spa(zfsvfs->z_os))) &&
-           !(*flags & MS_RDONLY)) {
-               *flags |= MS_RDONLY;
+           !(*flags & SB_RDONLY)) {
+               *flags |= SB_RDONLY;
                return (EROFS);
        }
 
@@ -1976,6 +1981,9 @@ zfs_remount(struct super_block *sb, int *flags, zfs_mnt_t *zm)
        if (error)
                return (error);
 
+       if (!zfs_is_readonly(zfsvfs) && (*flags & SB_RDONLY))
+               txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
+
        zfs_unregister_callbacks(zfsvfs);
        zfsvfs_vfs_free(zfsvfs->z_vfs);
 
@@ -2168,6 +2176,15 @@ zfs_resume_fs(zfsvfs_t *zfsvfs, dsl_dataset_t *ds)
        }
        mutex_exit(&zfsvfs->z_znodes_lock);
 
+       if (!zfs_is_readonly(zfsvfs) && !zfsvfs->z_unmounted) {
+               /*
+                * zfs_suspend_fs() could have interrupted freeing
+                * of dnodes. We need to restart this freeing so
+                * that we don't "leak" the space.
+                */
+               zfs_unlinked_drain(zfsvfs);
+       }
+
 bail:
        /* release the VFS ops */
        rw_exit(&zfsvfs->z_teardown_inactive_lock);
@@ -2244,6 +2261,7 @@ zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
        dmu_tx_commit(tx);
 
        zfsvfs->z_version = newvers;
+       os->os_version = newvers;
 
        zfs_set_fuid_feature(zfsvfs);
 
@@ -2256,13 +2274,42 @@ zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
 int
 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
 {
-       const char *pname;
-       int error = SET_ERROR(ENOENT);
+       uint64_t *cached_copy = NULL;
+
+       /*
+        * Figure out where in the objset_t the cached copy would live, if it
+        * is available for the requested property.
+        */
+       if (os != NULL) {
+               switch (prop) {
+               case ZFS_PROP_VERSION:
+                       cached_copy = &os->os_version;
+                       break;
+               case ZFS_PROP_NORMALIZE:
+                       cached_copy = &os->os_normalization;
+                       break;
+               case ZFS_PROP_UTF8ONLY:
+                       cached_copy = &os->os_utf8only;
+                       break;
+               case ZFS_PROP_CASE:
+                       cached_copy = &os->os_casesensitivity;
+                       break;
+               default:
+                       break;
+               }
+       }
+       if (cached_copy != NULL && *cached_copy != OBJSET_PROP_UNINITIALIZED) {
+               *value = *cached_copy;
+               return (0);
+       }
 
        /*
-        * Look up the file system's value for the property.  For the
-        * version property, we look up a slightly different string.
+        * If the property wasn't cached, look up the file system's value for
+        * the property. For the version property, we look up a slightly
+        * different string.
         */
+       const char *pname;
+       int error = ENOENT;
        if (prop == ZFS_PROP_VERSION)
                pname = ZPL_VERSION_STR;
        else
@@ -2294,6 +2341,15 @@ zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
                }
                error = 0;
        }
+
+       /*
+        * If one of the methods for getting the property value above worked,
+        * copy it into the objset_t's cache.
+        */
+       if (error == 0 && cached_copy != NULL) {
+               *cached_copy = *value;
+       }
+
        return (error);
 }
 
@@ -2341,7 +2397,7 @@ zfs_fini(void)
        zfsctl_fini();
 }
 
-#if defined(_KERNEL) && defined(HAVE_SPL)
+#if defined(_KERNEL)
 EXPORT_SYMBOL(zfs_suspend_fs);
 EXPORT_SYMBOL(zfs_resume_fs);
 EXPORT_SYMBOL(zfs_userspace_one);