9ae3f9
From 7ad8c03a28fca67150972cda964ebe9233766b54 Mon Sep 17 00:00:00 2001
9ae3f9
From: Xavi Hernandez <xhernandez@redhat.com>
9ae3f9
Date: Mon, 30 Mar 2020 11:09:39 +0200
9ae3f9
Subject: [PATCH 418/449] md-cache: avoid clearing cache when not necessary
9ae3f9
9ae3f9
mdc_inode_xatt_set() blindly cleared current cache when dict was not
9ae3f9
NULL, even if there was no xattr requested.
9ae3f9
9ae3f9
This patch fixes this by only calling mdc_inode_xatt_set() when we have
9ae3f9
explicitly requested something to cache.
9ae3f9
9ae3f9
Backport of:
9ae3f9
> Upstream-patch-link: https://review.gluster.org/24267
9ae3f9
> Change-Id: Idc91a4693f1ff39f7059acde26682ccc361b947d
9ae3f9
> Fixes: #1140
9ae3f9
> Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
9ae3f9
9ae3f9
BUG: 1815434
9ae3f9
Change-Id: Idc91a4693f1ff39f7059acde26682ccc361b947d
9ae3f9
Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
9ae3f9
Reviewed-on: https://code.engineering.redhat.com/gerrit/202487
9ae3f9
Tested-by: RHGS Build Bot <nigelb@redhat.com>
9ae3f9
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
9ae3f9
---
9ae3f9
 xlators/performance/md-cache/src/md-cache.c | 165 ++++++++++++++++------------
9ae3f9
 1 file changed, 93 insertions(+), 72 deletions(-)
9ae3f9
9ae3f9
diff --git a/xlators/performance/md-cache/src/md-cache.c b/xlators/performance/md-cache/src/md-cache.c
9ae3f9
index a6b363f..bbbee3b 100644
9ae3f9
--- a/xlators/performance/md-cache/src/md-cache.c
9ae3f9
+++ b/xlators/performance/md-cache/src/md-cache.c
9ae3f9
@@ -133,6 +133,7 @@ struct mdc_local {
9ae3f9
     char *key;
9ae3f9
     dict_t *xattr;
9ae3f9
     uint64_t incident_time;
9ae3f9
+    bool update_cache;
9ae3f9
 };
9ae3f9
 
9ae3f9
 int
9ae3f9
@@ -969,7 +970,7 @@ out:
9ae3f9
     return ret;
9ae3f9
 }
9ae3f9
 
9ae3f9
-void
9ae3f9
+static bool
9ae3f9
 mdc_load_reqs(xlator_t *this, dict_t *dict)
9ae3f9
 {
9ae3f9
     struct mdc_conf *conf = this->private;
9ae3f9
@@ -978,6 +979,7 @@ mdc_load_reqs(xlator_t *this, dict_t *dict)
9ae3f9
     char *tmp = NULL;
9ae3f9
     char *tmp1 = NULL;
9ae3f9
     int ret = 0;
9ae3f9
+    bool loaded = false;
9ae3f9
 
9ae3f9
     tmp1 = conf->mdc_xattr_str;
9ae3f9
     if (!tmp1)
9ae3f9
@@ -995,13 +997,17 @@ mdc_load_reqs(xlator_t *this, dict_t *dict)
9ae3f9
             conf->mdc_xattr_str = NULL;
9ae3f9
             gf_msg("md-cache", GF_LOG_ERROR, 0, MD_CACHE_MSG_NO_XATTR_CACHE,
9ae3f9
                    "Disabled cache for xattrs, dict_set failed");
9ae3f9
+            goto out;
9ae3f9
         }
9ae3f9
         pattern = strtok_r(NULL, ",", &tmp);
9ae3f9
     }
9ae3f9
 
9ae3f9
-    GF_FREE(mdc_xattr_str);
9ae3f9
+    loaded = true;
9ae3f9
+
9ae3f9
 out:
9ae3f9
-    return;
9ae3f9
+    GF_FREE(mdc_xattr_str);
9ae3f9
+
9ae3f9
+    return loaded;
9ae3f9
 }
9ae3f9
 
9ae3f9
 struct checkpair {
9ae3f9
@@ -1092,6 +1098,25 @@ err:
9ae3f9
     return ret;
9ae3f9
 }
9ae3f9
 
9ae3f9
+static dict_t *
9ae3f9
+mdc_prepare_request(xlator_t *this, mdc_local_t *local, dict_t *xdata)
9ae3f9
+{
9ae3f9
+    if (xdata == NULL) {
9ae3f9
+        xdata = dict_new();
9ae3f9
+        if (xdata == NULL) {
9ae3f9
+            local->update_cache = false;
9ae3f9
+
9ae3f9
+            return NULL;
9ae3f9
+        }
9ae3f9
+    } else {
9ae3f9
+        dict_ref(xdata);
9ae3f9
+    }
9ae3f9
+
9ae3f9
+    local->update_cache = mdc_load_reqs(this, xdata);
9ae3f9
+
9ae3f9
+    return xdata;
9ae3f9
+}
9ae3f9
+
9ae3f9
 int
9ae3f9
 mdc_statfs_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
9ae3f9
                int32_t op_ret, int32_t op_errno, struct statvfs *buf,
9ae3f9
@@ -1201,7 +1226,9 @@ mdc_lookup_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
9ae3f9
 
9ae3f9
     if (local->loc.inode) {
9ae3f9
         mdc_inode_iatt_set(this, local->loc.inode, stbuf, local->incident_time);
9ae3f9
-        mdc_inode_xatt_set(this, local->loc.inode, dict);
9ae3f9
+        if (local->update_cache) {
9ae3f9
+            mdc_inode_xatt_set(this, local->loc.inode, dict);
9ae3f9
+        }
9ae3f9
     }
9ae3f9
 out:
9ae3f9
     MDC_STACK_UNWIND(lookup, frame, op_ret, op_errno, inode, stbuf, dict,
9ae3f9
@@ -1220,7 +1247,6 @@ mdc_lookup(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata)
9ae3f9
         0,
9ae3f9
     };
9ae3f9
     dict_t *xattr_rsp = NULL;
9ae3f9
-    dict_t *xattr_alloc = NULL;
9ae3f9
     mdc_local_t *local = NULL;
9ae3f9
     struct mdc_conf *conf = this->private;
9ae3f9
 
9ae3f9
@@ -1271,18 +1297,18 @@ mdc_lookup(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata)
9ae3f9
     return 0;
9ae3f9
 
9ae3f9
 uncached:
9ae3f9
-    if (!xdata)
9ae3f9
-        xdata = xattr_alloc = dict_new();
9ae3f9
-    if (xdata)
9ae3f9
-        mdc_load_reqs(this, xdata);
9ae3f9
+    xdata = mdc_prepare_request(this, local, xdata);
9ae3f9
 
9ae3f9
     STACK_WIND(frame, mdc_lookup_cbk, FIRST_CHILD(this),
9ae3f9
                FIRST_CHILD(this)->fops->lookup, loc, xdata);
9ae3f9
 
9ae3f9
     if (xattr_rsp)
9ae3f9
         dict_unref(xattr_rsp);
9ae3f9
-    if (xattr_alloc)
9ae3f9
-        dict_unref(xattr_alloc);
9ae3f9
+
9ae3f9
+    if (xdata != NULL) {
9ae3f9
+        dict_unref(xdata);
9ae3f9
+    }
9ae3f9
+
9ae3f9
     return 0;
9ae3f9
 }
9ae3f9
 
9ae3f9
@@ -1305,7 +1331,9 @@ mdc_stat_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret,
9ae3f9
     }
9ae3f9
 
9ae3f9
     mdc_inode_iatt_set(this, local->loc.inode, buf, local->incident_time);
9ae3f9
-    mdc_inode_xatt_set(this, local->loc.inode, xdata);
9ae3f9
+    if (local->update_cache) {
9ae3f9
+        mdc_inode_xatt_set(this, local->loc.inode, xdata);
9ae3f9
+    }
9ae3f9
 
9ae3f9
 out:
9ae3f9
     MDC_STACK_UNWIND(stat, frame, op_ret, op_errno, buf, xdata);
9ae3f9
@@ -1319,7 +1347,6 @@ mdc_stat(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata)
9ae3f9
     int ret;
9ae3f9
     struct iatt stbuf;
9ae3f9
     mdc_local_t *local = NULL;
9ae3f9
-    dict_t *xattr_alloc = NULL;
9ae3f9
     struct mdc_conf *conf = this->private;
9ae3f9
 
9ae3f9
     local = mdc_local_get(frame, loc->inode);
9ae3f9
@@ -1343,17 +1370,16 @@ mdc_stat(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata)
9ae3f9
     return 0;
9ae3f9
 
9ae3f9
 uncached:
9ae3f9
-    if (!xdata)
9ae3f9
-        xdata = xattr_alloc = dict_new();
9ae3f9
-    if (xdata)
9ae3f9
-        mdc_load_reqs(this, xdata);
9ae3f9
+    xdata = mdc_prepare_request(this, local, xdata);
9ae3f9
 
9ae3f9
     GF_ATOMIC_INC(conf->mdc_counter.stat_miss);
9ae3f9
     STACK_WIND(frame, mdc_stat_cbk, FIRST_CHILD(this),
9ae3f9
                FIRST_CHILD(this)->fops->stat, loc, xdata);
9ae3f9
 
9ae3f9
-    if (xattr_alloc)
9ae3f9
-        dict_unref(xattr_alloc);
9ae3f9
+    if (xdata != NULL) {
9ae3f9
+        dict_unref(xdata);
9ae3f9
+    }
9ae3f9
+
9ae3f9
     return 0;
9ae3f9
 }
9ae3f9
 
9ae3f9
@@ -1376,7 +1402,9 @@ mdc_fstat_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret,
9ae3f9
     }
9ae3f9
 
9ae3f9
     mdc_inode_iatt_set(this, local->fd->inode, buf, local->incident_time);
9ae3f9
-    mdc_inode_xatt_set(this, local->fd->inode, xdata);
9ae3f9
+    if (local->update_cache) {
9ae3f9
+        mdc_inode_xatt_set(this, local->fd->inode, xdata);
9ae3f9
+    }
9ae3f9
 
9ae3f9
 out:
9ae3f9
     MDC_STACK_UNWIND(fstat, frame, op_ret, op_errno, buf, xdata);
9ae3f9
@@ -1390,7 +1418,6 @@ mdc_fstat(call_frame_t *frame, xlator_t *this, fd_t *fd, dict_t *xdata)
9ae3f9
     int ret;
9ae3f9
     struct iatt stbuf;
9ae3f9
     mdc_local_t *local = NULL;
9ae3f9
-    dict_t *xattr_alloc = NULL;
9ae3f9
     struct mdc_conf *conf = this->private;
9ae3f9
 
9ae3f9
     local = mdc_local_get(frame, fd->inode);
9ae3f9
@@ -1409,17 +1436,16 @@ mdc_fstat(call_frame_t *frame, xlator_t *this, fd_t *fd, dict_t *xdata)
9ae3f9
     return 0;
9ae3f9
 
9ae3f9
 uncached:
9ae3f9
-    if (!xdata)
9ae3f9
-        xdata = xattr_alloc = dict_new();
9ae3f9
-    if (xdata)
9ae3f9
-        mdc_load_reqs(this, xdata);
9ae3f9
+    xdata = mdc_prepare_request(this, local, xdata);
9ae3f9
 
9ae3f9
     GF_ATOMIC_INC(conf->mdc_counter.stat_miss);
9ae3f9
     STACK_WIND(frame, mdc_fstat_cbk, FIRST_CHILD(this),
9ae3f9
                FIRST_CHILD(this)->fops->fstat, fd, xdata);
9ae3f9
 
9ae3f9
-    if (xattr_alloc)
9ae3f9
-        dict_unref(xattr_alloc);
9ae3f9
+    if (xdata != NULL) {
9ae3f9
+        dict_unref(xdata);
9ae3f9
+    }
9ae3f9
+
9ae3f9
     return 0;
9ae3f9
 }
9ae3f9
 
9ae3f9
@@ -2393,7 +2419,9 @@ mdc_getxattr_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
9ae3f9
         goto out;
9ae3f9
     }
9ae3f9
 
9ae3f9
-    mdc_inode_xatt_set(this, local->loc.inode, xdata);
9ae3f9
+    if (local->update_cache) {
9ae3f9
+        mdc_inode_xatt_set(this, local->loc.inode, xdata);
9ae3f9
+    }
9ae3f9
 
9ae3f9
 out:
9ae3f9
     MDC_STACK_UNWIND(getxattr, frame, op_ret, op_errno, xattr, xdata);
9ae3f9
@@ -2410,7 +2438,6 @@ mdc_getxattr(call_frame_t *frame, xlator_t *this, loc_t *loc, const char *key,
9ae3f9
     mdc_local_t *local = NULL;
9ae3f9
     dict_t *xattr = NULL;
9ae3f9
     struct mdc_conf *conf = this->private;
9ae3f9
-    dict_t *xattr_alloc = NULL;
9ae3f9
     gf_boolean_t key_satisfied = _gf_true;
9ae3f9
 
9ae3f9
     local = mdc_local_get(frame, loc->inode);
9ae3f9
@@ -2443,18 +2470,17 @@ mdc_getxattr(call_frame_t *frame, xlator_t *this, loc_t *loc, const char *key,
9ae3f9
 
9ae3f9
 uncached:
9ae3f9
     if (key_satisfied) {
9ae3f9
-        if (!xdata)
9ae3f9
-            xdata = xattr_alloc = dict_new();
9ae3f9
-        if (xdata)
9ae3f9
-            mdc_load_reqs(this, xdata);
9ae3f9
+        xdata = mdc_prepare_request(this, local, xdata);
9ae3f9
     }
9ae3f9
 
9ae3f9
     GF_ATOMIC_INC(conf->mdc_counter.xattr_miss);
9ae3f9
     STACK_WIND(frame, mdc_getxattr_cbk, FIRST_CHILD(this),
9ae3f9
                FIRST_CHILD(this)->fops->getxattr, loc, key, xdata);
9ae3f9
 
9ae3f9
-    if (xattr_alloc)
9ae3f9
-        dict_unref(xattr_alloc);
9ae3f9
+    if (key_satisfied && (xdata != NULL)) {
9ae3f9
+        dict_unref(xdata);
9ae3f9
+    }
9ae3f9
+
9ae3f9
     return 0;
9ae3f9
 }
9ae3f9
 
9ae3f9
@@ -2481,7 +2507,9 @@ mdc_fgetxattr_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
9ae3f9
         goto out;
9ae3f9
     }
9ae3f9
 
9ae3f9
-    mdc_inode_xatt_set(this, local->fd->inode, xdata);
9ae3f9
+    if (local->update_cache) {
9ae3f9
+        mdc_inode_xatt_set(this, local->fd->inode, xdata);
9ae3f9
+    }
9ae3f9
 
9ae3f9
 out:
9ae3f9
     MDC_STACK_UNWIND(fgetxattr, frame, op_ret, op_errno, xattr, xdata);
9ae3f9
@@ -2498,7 +2526,6 @@ mdc_fgetxattr(call_frame_t *frame, xlator_t *this, fd_t *fd, const char *key,
9ae3f9
     dict_t *xattr = NULL;
9ae3f9
     int op_errno = ENODATA;
9ae3f9
     struct mdc_conf *conf = this->private;
9ae3f9
-    dict_t *xattr_alloc = NULL;
9ae3f9
     gf_boolean_t key_satisfied = _gf_true;
9ae3f9
 
9ae3f9
     local = mdc_local_get(frame, fd->inode);
9ae3f9
@@ -2531,18 +2558,17 @@ mdc_fgetxattr(call_frame_t *frame, xlator_t *this, fd_t *fd, const char *key,
9ae3f9
 
9ae3f9
 uncached:
9ae3f9
     if (key_satisfied) {
9ae3f9
-        if (!xdata)
9ae3f9
-            xdata = xattr_alloc = dict_new();
9ae3f9
-        if (xdata)
9ae3f9
-            mdc_load_reqs(this, xdata);
9ae3f9
+        xdata = mdc_prepare_request(this, local, xdata);
9ae3f9
     }
9ae3f9
 
9ae3f9
     GF_ATOMIC_INC(conf->mdc_counter.xattr_miss);
9ae3f9
     STACK_WIND(frame, mdc_fgetxattr_cbk, FIRST_CHILD(this),
9ae3f9
                FIRST_CHILD(this)->fops->fgetxattr, fd, key, xdata);
9ae3f9
 
9ae3f9
-    if (xattr_alloc)
9ae3f9
-        dict_unref(xattr_alloc);
9ae3f9
+    if (key_satisfied && (xdata != NULL)) {
9ae3f9
+        dict_unref(xdata);
9ae3f9
+    }
9ae3f9
+
9ae3f9
     return 0;
9ae3f9
 }
9ae3f9
 
9ae3f9
@@ -2752,27 +2778,22 @@ int
9ae3f9
 mdc_opendir(call_frame_t *frame, xlator_t *this, loc_t *loc, fd_t *fd,
9ae3f9
             dict_t *xdata)
9ae3f9
 {
9ae3f9
-    dict_t *xattr_alloc = NULL;
9ae3f9
     mdc_local_t *local = NULL;
9ae3f9
 
9ae3f9
     local = mdc_local_get(frame, loc->inode);
9ae3f9
 
9ae3f9
     loc_copy(&local->loc, loc);
9ae3f9
 
9ae3f9
-    if (!xdata)
9ae3f9
-        xdata = xattr_alloc = dict_new();
9ae3f9
-
9ae3f9
-    if (xdata) {
9ae3f9
-        /* Tell readdir-ahead to include these keys in xdata when it
9ae3f9
-         * internally issues readdirp() in it's opendir_cbk */
9ae3f9
-        mdc_load_reqs(this, xdata);
9ae3f9
-    }
9ae3f9
+    /* Tell readdir-ahead to include these keys in xdata when it
9ae3f9
+     * internally issues readdirp() in it's opendir_cbk */
9ae3f9
+    xdata = mdc_prepare_request(this, local, xdata);
9ae3f9
 
9ae3f9
     STACK_WIND(frame, mdc_opendir_cbk, FIRST_CHILD(this),
9ae3f9
                FIRST_CHILD(this)->fops->opendir, loc, fd, xdata);
9ae3f9
 
9ae3f9
-    if (xattr_alloc)
9ae3f9
-        dict_unref(xattr_alloc);
9ae3f9
+    if (xdata != NULL) {
9ae3f9
+        dict_unref(xdata);
9ae3f9
+    }
9ae3f9
 
9ae3f9
     return 0;
9ae3f9
 }
9ae3f9
@@ -2800,7 +2821,9 @@ mdc_readdirp_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int op_ret,
9ae3f9
             continue;
9ae3f9
         mdc_inode_iatt_set(this, entry->inode, &entry->d_stat,
9ae3f9
                            local->incident_time);
9ae3f9
-        mdc_inode_xatt_set(this, entry->inode, entry->dict);
9ae3f9
+        if (local->update_cache) {
9ae3f9
+            mdc_inode_xatt_set(this, entry->inode, entry->dict);
9ae3f9
+        }
9ae3f9
     }
9ae3f9
 
9ae3f9
 unwind:
9ae3f9
@@ -2812,7 +2835,6 @@ int
9ae3f9
 mdc_readdirp(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
9ae3f9
              off_t offset, dict_t *xdata)
9ae3f9
 {
9ae3f9
-    dict_t *xattr_alloc = NULL;
9ae3f9
     mdc_local_t *local = NULL;
9ae3f9
 
9ae3f9
     local = mdc_local_get(frame, fd->inode);
9ae3f9
@@ -2821,15 +2843,15 @@ mdc_readdirp(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
9ae3f9
 
9ae3f9
     local->fd = fd_ref(fd);
9ae3f9
 
9ae3f9
-    if (!xdata)
9ae3f9
-        xdata = xattr_alloc = dict_new();
9ae3f9
-    if (xdata)
9ae3f9
-        mdc_load_reqs(this, xdata);
9ae3f9
+    xdata = mdc_prepare_request(this, local, xdata);
9ae3f9
 
9ae3f9
     STACK_WIND(frame, mdc_readdirp_cbk, FIRST_CHILD(this),
9ae3f9
                FIRST_CHILD(this)->fops->readdirp, fd, size, offset, xdata);
9ae3f9
-    if (xattr_alloc)
9ae3f9
-        dict_unref(xattr_alloc);
9ae3f9
+
9ae3f9
+    if (xdata != NULL) {
9ae3f9
+        dict_unref(xdata);
9ae3f9
+    }
9ae3f9
+
9ae3f9
     return 0;
9ae3f9
 out:
9ae3f9
     MDC_STACK_UNWIND(readdirp, frame, -1, ENOMEM, NULL, NULL);
9ae3f9
@@ -2860,7 +2882,6 @@ int
9ae3f9
 mdc_readdir(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
9ae3f9
             off_t offset, dict_t *xdata)
9ae3f9
 {
9ae3f9
-    int need_unref = 0;
9ae3f9
     mdc_local_t *local = NULL;
9ae3f9
     struct mdc_conf *conf = this->private;
9ae3f9
 
9ae3f9
@@ -2876,19 +2897,14 @@ mdc_readdir(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
9ae3f9
         return 0;
9ae3f9
     }
9ae3f9
 
9ae3f9
-    if (!xdata) {
9ae3f9
-        xdata = dict_new();
9ae3f9
-        need_unref = 1;
9ae3f9
-    }
9ae3f9
-
9ae3f9
-    if (xdata)
9ae3f9
-        mdc_load_reqs(this, xdata);
9ae3f9
+    xdata = mdc_prepare_request(this, local, xdata);
9ae3f9
 
9ae3f9
     STACK_WIND(frame, mdc_readdirp_cbk, FIRST_CHILD(this),
9ae3f9
                FIRST_CHILD(this)->fops->readdirp, fd, size, offset, xdata);
9ae3f9
 
9ae3f9
-    if (need_unref && xdata)
9ae3f9
+    if (xdata != NULL) {
9ae3f9
         dict_unref(xdata);
9ae3f9
+    }
9ae3f9
 
9ae3f9
     return 0;
9ae3f9
 unwind:
9ae3f9
@@ -3468,7 +3484,12 @@ mdc_register_xattr_inval(xlator_t *this)
9ae3f9
         goto out;
9ae3f9
     }
9ae3f9
 
9ae3f9
-    mdc_load_reqs(this, xattr);
9ae3f9
+    if (!mdc_load_reqs(this, xattr)) {
9ae3f9
+        gf_msg(this->name, GF_LOG_WARNING, ENOMEM, MD_CACHE_MSG_NO_MEMORY,
9ae3f9
+               "failed to populate cache entries");
9ae3f9
+        ret = -1;
9ae3f9
+        goto out;
9ae3f9
+    }
9ae3f9
 
9ae3f9
     frame = create_frame(this, this->ctx->pool);
9ae3f9
     if (!frame) {
9ae3f9
-- 
9ae3f9
1.8.3.1
9ae3f9