14f8ab
From 6565749c95e90f360a994bde1416cffd22cd8ce9 Mon Sep 17 00:00:00 2001
14f8ab
From: N Balachandran <nbalacha@redhat.com>
14f8ab
Date: Mon, 25 Mar 2019 15:56:56 +0530
14f8ab
Subject: [PATCH 126/141] cluster/dht: refactor dht lookup functions
14f8ab
14f8ab
Part 1:  refactor the dht_lookup_dir_cbk
14f8ab
and dht_selfheal_directory functions.
14f8ab
Added a simple dht selfheal directory test
14f8ab
14f8ab
upstream: https://review.gluster.org/#/c/glusterfs/+/22407/
14f8ab
> Change-Id: I1410c26359e3c14b396adbe751937a52bd2fcff9
14f8ab
> updates: bz#1590385
14f8ab
14f8ab
Change-Id: Idd0a7df7122d634c371ecf30c0dbb94dc6063416
14f8ab
BUG: 1703897
14f8ab
Signed-off-by: N Balachandran <nbalacha@redhat.com>
14f8ab
Reviewed-on: https://code.engineering.redhat.com/gerrit/169037
14f8ab
Tested-by: RHGS Build Bot <nigelb@redhat.com>
14f8ab
Reviewed-by: Susant Palai <spalai@redhat.com>
14f8ab
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
14f8ab
---
14f8ab
 tests/basic/distribute/dir-heal.t      | 145 +++++++++++++++++++++++++++
14f8ab
 xlators/cluster/dht/src/dht-common.c   | 178 +++++++++++++++------------------
14f8ab
 xlators/cluster/dht/src/dht-selfheal.c |  65 +++++++-----
14f8ab
 3 files changed, 264 insertions(+), 124 deletions(-)
14f8ab
 create mode 100644 tests/basic/distribute/dir-heal.t
14f8ab
14f8ab
diff --git a/tests/basic/distribute/dir-heal.t b/tests/basic/distribute/dir-heal.t
14f8ab
new file mode 100644
14f8ab
index 0000000..851f765
14f8ab
--- /dev/null
14f8ab
+++ b/tests/basic/distribute/dir-heal.t
14f8ab
@@ -0,0 +1,145 @@
14f8ab
+#!/bin/bash
14f8ab
+
14f8ab
+. $(dirname $0)/../../include.rc
14f8ab
+. $(dirname $0)/../../volume.rc
14f8ab
+. $(dirname $0)/../../nfs.rc
14f8ab
+. $(dirname $0)/../../common-utils.rc
14f8ab
+
14f8ab
+# Test 1 overview:
14f8ab
+# ----------------
14f8ab
+#
14f8ab
+# 1. Kill one brick of the volume.
14f8ab
+# 2. Create directories and change directory properties.
14f8ab
+# 3. Bring up the brick and access the directory
14f8ab
+# 4. Check the permissions and xattrs on the backend
14f8ab
+
14f8ab
+cleanup
14f8ab
+
14f8ab
+TEST glusterd
14f8ab
+TEST pidof glusterd
14f8ab
+
14f8ab
+TEST $CLI volume create $V0 $H0:$B0/$V0-{1..3}
14f8ab
+TEST $CLI volume start $V0
14f8ab
+
14f8ab
+# We want the lookup to reach DHT
14f8ab
+TEST $CLI volume set $V0 performance.stat-prefetch off
14f8ab
+
14f8ab
+# Mount using FUSE , kill a brick and create directories
14f8ab
+TEST glusterfs --entry-timeout=0 --attribute-timeout=0 -s $H0 --volfile-id $V0 $M0
14f8ab
+
14f8ab
+ls $M0/
14f8ab
+cd $M0
14f8ab
+
14f8ab
+TEST kill_brick $V0 $H0 $B0/$V0-1
14f8ab
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "0" brick_up_status $V0 $H0 $B0/$V0-1
14f8ab
+
14f8ab
+TEST mkdir dir{1..4}
14f8ab
+
14f8ab
+# No change for dir1
14f8ab
+# Change permissions for dir2
14f8ab
+# Set xattr on dir3
14f8ab
+# Change permissions and set xattr on dir4
14f8ab
+
14f8ab
+TEST chmod 777 $M0/dir2
14f8ab
+
14f8ab
+TEST setfattr -n "user.test" -v "test" $M0/dir3
14f8ab
+
14f8ab
+TEST chmod 777 $M0/dir4
14f8ab
+TEST setfattr -n "user.test" -v "test" $M0/dir4
14f8ab
+
14f8ab
+
14f8ab
+# Start all bricks
14f8ab
+
14f8ab
+TEST $CLI volume start $V0 force
14f8ab
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/$V0-1
14f8ab
+
14f8ab
+#$CLI volume status
14f8ab
+
14f8ab
+# It takes a while for the client to reconnect to the brick
14f8ab
+sleep 5
14f8ab
+
14f8ab
+stat $M0/dir* > /dev/null
14f8ab
+
14f8ab
+# Check that directories have been created on the brick that was killed
14f8ab
+
14f8ab
+TEST ls $B0/$V0-1/dir1
14f8ab
+
14f8ab
+TEST ls $B0/$V0-1/dir2
14f8ab
+EXPECT "777" stat -c "%a" $B0/$V0-1/dir2
14f8ab
+
14f8ab
+TEST ls $B0/$V0-1/dir3
14f8ab
+EXPECT "test" getfattr -n "user.test" --absolute-names --only-values $B0/$V0-1/dir3
14f8ab
+
14f8ab
+
14f8ab
+TEST ls $B0/$V0-1/dir4
14f8ab
+EXPECT "777" stat -c "%a" $B0/$V0-1/dir4
14f8ab
+EXPECT "test" getfattr -n "user.test" --absolute-names --only-values $B0/$V0-1/dir4
14f8ab
+
14f8ab
+
14f8ab
+TEST rm -rf $M0/*
14f8ab
+
14f8ab
+cd
14f8ab
+
14f8ab
+EXPECT_WITHIN $UMOUNT_TIMEOUT "Y" force_umount $M0
14f8ab
+
14f8ab
+
14f8ab
+# Test 2 overview:
14f8ab
+# ----------------
14f8ab
+# 1. Create directories with all bricks up.
14f8ab
+# 2. Kill a brick and change directory properties and set user xattr.
14f8ab
+# 2. Bring up the brick and access the directory
14f8ab
+# 3. Check the permissions and xattrs on the backend
14f8ab
+
14f8ab
+
14f8ab
+TEST glusterfs --entry-timeout=0 --attribute-timeout=0 -s $H0 --volfile-id $V0 $M0
14f8ab
+
14f8ab
+ls $M0/
14f8ab
+cd $M0
14f8ab
+TEST mkdir dir{1..4}
14f8ab
+
14f8ab
+TEST kill_brick $V0 $H0 $B0/$V0-1
14f8ab
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "0" brick_up_status $V0 $H0 $B0/$V0-1
14f8ab
+
14f8ab
+# No change for dir1
14f8ab
+# Change permissions for dir2
14f8ab
+# Set xattr on dir3
14f8ab
+# Change permissions and set xattr on dir4
14f8ab
+
14f8ab
+TEST chmod 777 $M0/dir2
14f8ab
+
14f8ab
+TEST setfattr -n "user.test" -v "test" $M0/dir3
14f8ab
+
14f8ab
+TEST chmod 777 $M0/dir4
14f8ab
+TEST setfattr -n "user.test" -v "test" $M0/dir4
14f8ab
+
14f8ab
+
14f8ab
+# Start all bricks
14f8ab
+
14f8ab
+TEST $CLI volume start $V0 force
14f8ab
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" brick_up_status $V0 $H0 $B0/$V0-1
14f8ab
+
14f8ab
+#$CLI volume status
14f8ab
+
14f8ab
+# It takes a while for the client to reconnect to the brick
14f8ab
+sleep 5
14f8ab
+
14f8ab
+stat $M0/dir* > /dev/null
14f8ab
+
14f8ab
+# Check directories on the brick that was killed
14f8ab
+
14f8ab
+TEST ls $B0/$V0-1/dir2
14f8ab
+EXPECT "777" stat -c "%a" $B0/$V0-1/dir2
14f8ab
+
14f8ab
+TEST ls $B0/$V0-1/dir3
14f8ab
+EXPECT "test" getfattr -n "user.test" --absolute-names --only-values $B0/$V0-1/dir3
14f8ab
+
14f8ab
+
14f8ab
+TEST ls $B0/$V0-1/dir4
14f8ab
+EXPECT "777" stat -c "%a" $B0/$V0-1/dir4
14f8ab
+EXPECT "test" getfattr -n "user.test" --absolute-names --only-values $B0/$V0-1/dir4
14f8ab
+cd
14f8ab
+
14f8ab
+
14f8ab
+# Cleanup
14f8ab
+cleanup
14f8ab
+
14f8ab
diff --git a/xlators/cluster/dht/src/dht-common.c b/xlators/cluster/dht/src/dht-common.c
14f8ab
index 2a68193..d3e900c 100644
14f8ab
--- a/xlators/cluster/dht/src/dht-common.c
14f8ab
+++ b/xlators/cluster/dht/src/dht-common.c
14f8ab
@@ -801,9 +801,8 @@ dht_common_mark_mdsxattr(call_frame_t *frame, int *errst,
14f8ab
     call_frame_t *xattr_frame = NULL;
14f8ab
     gf_boolean_t vol_down = _gf_false;
14f8ab
 
14f8ab
-    this = frame->this;
14f8ab
-
14f8ab
     GF_VALIDATE_OR_GOTO("dht", frame, out);
14f8ab
+    this = frame->this;
14f8ab
     GF_VALIDATE_OR_GOTO("dht", this, out);
14f8ab
     GF_VALIDATE_OR_GOTO(this->name, frame->local, out);
14f8ab
     GF_VALIDATE_OR_GOTO(this->name, this->private, out);
14f8ab
@@ -812,6 +811,7 @@ dht_common_mark_mdsxattr(call_frame_t *frame, int *errst,
14f8ab
     conf = this->private;
14f8ab
     layout = local->selfheal.layout;
14f8ab
     local->mds_heal_fresh_lookup = mark_during_fresh_lookup;
14f8ab
+
14f8ab
     gf_uuid_unparse(local->gfid, gfid_local);
14f8ab
 
14f8ab
     /* Code to update hashed subvol consider as a mds subvol
14f8ab
@@ -1240,6 +1240,31 @@ out:
14f8ab
 }
14f8ab
 
14f8ab
 int
14f8ab
+dht_needs_selfheal(call_frame_t *frame, xlator_t *this)
14f8ab
+{
14f8ab
+    dht_local_t *local = NULL;
14f8ab
+    dht_layout_t *layout = NULL;
14f8ab
+    int needs_selfheal = 0;
14f8ab
+    int ret = 0;
14f8ab
+
14f8ab
+    local = frame->local;
14f8ab
+    layout = local->layout;
14f8ab
+
14f8ab
+    if (local->need_attrheal || local->need_xattr_heal ||
14f8ab
+        local->need_selfheal) {
14f8ab
+        needs_selfheal = 1;
14f8ab
+    }
14f8ab
+
14f8ab
+    ret = dht_layout_normalize(this, &local->loc, layout);
14f8ab
+
14f8ab
+    if (ret != 0) {
14f8ab
+        gf_msg_debug(this->name, 0, "fixing assignment on %s", local->loc.path);
14f8ab
+        needs_selfheal = 1;
14f8ab
+    }
14f8ab
+    return needs_selfheal;
14f8ab
+}
14f8ab
+
14f8ab
+int
14f8ab
 dht_lookup_dir_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
14f8ab
                    int op_ret, int op_errno, inode_t *inode, struct iatt *stbuf,
14f8ab
                    dict_t *xattr, struct iatt *postparent)
14f8ab
@@ -1256,8 +1281,6 @@ dht_lookup_dir_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
14f8ab
     char gfid_local[GF_UUID_BUF_SIZE] = {0};
14f8ab
     char gfid_node[GF_UUID_BUF_SIZE] = {0};
14f8ab
     int32_t mds_xattr_val[1] = {0};
14f8ab
-    call_frame_t *copy = NULL;
14f8ab
-    dht_local_t *copy_local = NULL;
14f8ab
 
14f8ab
     GF_VALIDATE_OR_GOTO("dht", frame, out);
14f8ab
     GF_VALIDATE_OR_GOTO("dht", this, out);
14f8ab
@@ -1270,7 +1293,11 @@ dht_lookup_dir_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
14f8ab
     conf = this->private;
14f8ab
 
14f8ab
     layout = local->layout;
14f8ab
+    gf_msg_debug(this->name, op_errno,
14f8ab
+                 "%s: lookup on %s returned with op_ret = %d, op_errno = %d",
14f8ab
+                 local->loc.path, prev->name, op_ret, op_errno);
14f8ab
 
14f8ab
+    /* The first successful lookup*/
14f8ab
     if (!op_ret && gf_uuid_is_null(local->gfid)) {
14f8ab
         memcpy(local->gfid, stbuf->ia_gfid, 16);
14f8ab
     }
14f8ab
@@ -1298,13 +1325,10 @@ dht_lookup_dir_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
14f8ab
 
14f8ab
         if (op_ret == -1) {
14f8ab
             local->op_errno = op_errno;
14f8ab
-            gf_msg_debug(this->name, op_errno,
14f8ab
-                         "%s: lookup on %s returned error", local->loc.path,
14f8ab
-                         prev->name);
14f8ab
 
14f8ab
             /* The GFID is missing on this subvol. Force a heal. */
14f8ab
             if (op_errno == ENODATA) {
14f8ab
-                local->need_selfheal = 1;
14f8ab
+                local->need_lookup_everywhere = 1;
14f8ab
             }
14f8ab
             goto unlock;
14f8ab
         }
14f8ab
@@ -1312,12 +1336,11 @@ dht_lookup_dir_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
14f8ab
         is_dir = check_is_dir(inode, stbuf, xattr);
14f8ab
         if (!is_dir) {
14f8ab
             gf_msg_debug(this->name, 0,
14f8ab
-                         "lookup of %s on %s returned non"
14f8ab
-                         "dir 0%o"
14f8ab
+                         "%s: lookup on %s returned non dir 0%o"
14f8ab
                          "calling lookup_everywhere",
14f8ab
                          local->loc.path, prev->name, stbuf->ia_type);
14f8ab
 
14f8ab
-            local->need_selfheal = 1;
14f8ab
+            local->need_lookup_everywhere = 1;
14f8ab
             goto unlock;
14f8ab
         }
14f8ab
 
14f8ab
@@ -1328,14 +1351,8 @@ dht_lookup_dir_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
14f8ab
             dht_aggregate_xattr(local->xattr, xattr);
14f8ab
         }
14f8ab
 
14f8ab
-        if (dict_get(xattr, conf->mds_xattr_key)) {
14f8ab
-            local->mds_subvol = prev;
14f8ab
-            local->mds_stbuf.ia_gid = stbuf->ia_gid;
14f8ab
-            local->mds_stbuf.ia_uid = stbuf->ia_uid;
14f8ab
-            local->mds_stbuf.ia_prot = stbuf->ia_prot;
14f8ab
-        }
14f8ab
-
14f8ab
         if (local->stbuf.ia_type != IA_INVAL) {
14f8ab
+            /* This is not the first subvol to respond */
14f8ab
             if (!__is_root_gfid(stbuf->ia_gfid) &&
14f8ab
                 ((local->stbuf.ia_gid != stbuf->ia_gid) ||
14f8ab
                  (local->stbuf.ia_uid != stbuf->ia_uid) ||
14f8ab
@@ -1348,65 +1365,64 @@ dht_lookup_dir_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
14f8ab
         if (local->inode == NULL)
14f8ab
             local->inode = inode_ref(inode);
14f8ab
 
14f8ab
+        /* This could be a problem */
14f8ab
         dht_iatt_merge(this, &local->stbuf, stbuf);
14f8ab
         dht_iatt_merge(this, &local->postparent, postparent);
14f8ab
 
14f8ab
         if (!dict_get(xattr, conf->mds_xattr_key)) {
14f8ab
             gf_msg_debug(this->name, 0,
14f8ab
-                         "Internal xattr %s is not present "
14f8ab
-                         " on path %s gfid is %s ",
14f8ab
-                         conf->mds_xattr_key, local->loc.path, gfid_local);
14f8ab
+                         "%s: mds xattr %s is not present "
14f8ab
+                         "on %s(gfid = %s)",
14f8ab
+                         local->loc.path, conf->mds_xattr_key, prev->name,
14f8ab
+                         gfid_local);
14f8ab
             goto unlock;
14f8ab
-        } else {
14f8ab
-            /* Save mds subvol on inode ctx */
14f8ab
-            ret = dht_inode_ctx_mdsvol_set(local->inode, this, prev);
14f8ab
-            if (ret) {
14f8ab
-                gf_msg(this->name, GF_LOG_ERROR, 0,
14f8ab
-                       DHT_MSG_SET_INODE_CTX_FAILED,
14f8ab
-                       "Failed to set hashed subvol for %s vol is %s",
14f8ab
-                       local->loc.path, prev->name);
14f8ab
-            }
14f8ab
+        }
14f8ab
+
14f8ab
+        local->mds_subvol = prev;
14f8ab
+        local->mds_stbuf = *stbuf;
14f8ab
+
14f8ab
+        /* Save mds subvol on inode ctx */
14f8ab
+
14f8ab
+        ret = dht_inode_ctx_mdsvol_set(local->inode, this, prev);
14f8ab
+        if (ret) {
14f8ab
+            gf_msg(this->name, GF_LOG_ERROR, 0, DHT_MSG_SET_INODE_CTX_FAILED,
14f8ab
+                   "%s: Failed to set mds (%s)", local->loc.path, prev->name);
14f8ab
         }
14f8ab
         check_mds = dht_dict_get_array(xattr, conf->mds_xattr_key,
14f8ab
                                        mds_xattr_val, 1, &errst);
14f8ab
         if ((check_mds < 0) && !errst) {
14f8ab
             local->mds_xattr = dict_ref(xattr);
14f8ab
             gf_msg_debug(this->name, 0,
14f8ab
-                         "Value of %s is not zero on hashed subvol "
14f8ab
-                         "so xattr needs to be heal on non hashed"
14f8ab
-                         " path is %s and vol name is %s "
14f8ab
-                         " gfid is %s",
14f8ab
-                         conf->mds_xattr_key, local->loc.path, prev->name,
14f8ab
+                         "%s: %s is not zero on %s. Xattrs need to be healed."
14f8ab
+                         "(gfid = %s)",
14f8ab
+                         local->loc.path, conf->mds_xattr_key, prev->name,
14f8ab
                          gfid_local);
14f8ab
             local->need_xattr_heal = 1;
14f8ab
-            local->mds_subvol = prev;
14f8ab
         }
14f8ab
     }
14f8ab
+
14f8ab
 unlock:
14f8ab
     UNLOCK(&frame->lock);
14f8ab
 
14f8ab
     this_call_cnt = dht_frame_return(frame);
14f8ab
 
14f8ab
     if (is_last_call(this_call_cnt)) {
14f8ab
+        /* If the mds subvol is not set correctly*/
14f8ab
+        if (!__is_root_gfid(local->gfid) &&
14f8ab
+            (!dict_get(local->xattr, conf->mds_xattr_key))) {
14f8ab
+            local->need_selfheal = 1;
14f8ab
+        }
14f8ab
+
14f8ab
         /* No need to call xattr heal code if volume count is 1
14f8ab
          */
14f8ab
-        if (conf->subvolume_cnt == 1)
14f8ab
+        if (conf->subvolume_cnt == 1) {
14f8ab
             local->need_xattr_heal = 0;
14f8ab
-
14f8ab
-        /* Code to update all extended attributed from hashed subvol
14f8ab
-           to local->xattr
14f8ab
-        */
14f8ab
-        if (local->need_xattr_heal && (local->mds_xattr)) {
14f8ab
-            dht_dir_set_heal_xattr(this, local, local->xattr, local->mds_xattr,
14f8ab
-                                   NULL, NULL);
14f8ab
-            dict_unref(local->mds_xattr);
14f8ab
-            local->mds_xattr = NULL;
14f8ab
         }
14f8ab
 
14f8ab
-        if (local->need_selfheal) {
14f8ab
-            local->need_selfheal = 0;
14f8ab
+        if (local->need_selfheal || local->need_lookup_everywhere) {
14f8ab
             /* Set the gfid-req so posix will set the GFID*/
14f8ab
             if (!gf_uuid_is_null(local->gfid)) {
14f8ab
+                /* Ok, this should _never_ happen */
14f8ab
                 ret = dict_set_static_bin(local->xattr_req, "gfid-req",
14f8ab
                                           local->gfid, 16);
14f8ab
             } else {
14f8ab
@@ -1414,73 +1430,36 @@ unlock:
14f8ab
                     ret = dict_set_static_bin(local->xattr_req, "gfid-req",
14f8ab
                                               local->gfid_req, 16);
14f8ab
             }
14f8ab
+        }
14f8ab
+
14f8ab
+        if (local->need_lookup_everywhere) {
14f8ab
+            local->need_lookup_everywhere = 0;
14f8ab
             dht_lookup_everywhere(frame, this, &local->loc);
14f8ab
             return 0;
14f8ab
         }
14f8ab
 
14f8ab
         if (local->op_ret == 0) {
14f8ab
-            ret = dht_layout_normalize(this, &local->loc, layout);
14f8ab
-
14f8ab
-            if (ret != 0) {
14f8ab
-                gf_msg_debug(this->name, 0, "fixing assignment on %s",
14f8ab
-                             local->loc.path);
14f8ab
+            if (dht_needs_selfheal(frame, this)) {
14f8ab
                 goto selfheal;
14f8ab
             }
14f8ab
 
14f8ab
             dht_layout_set(this, local->inode, layout);
14f8ab
-            if (!dict_get(local->xattr, conf->mds_xattr_key) ||
14f8ab
-                local->need_xattr_heal)
14f8ab
-                goto selfheal;
14f8ab
-        }
14f8ab
-
14f8ab
-        if (local->inode) {
14f8ab
-            dht_inode_ctx_time_update(local->inode, this, &local->stbuf, 1);
14f8ab
-        }
14f8ab
-
14f8ab
-        if (local->loc.parent) {
14f8ab
-            dht_inode_ctx_time_update(local->loc.parent, this,
14f8ab
-                                      &local->postparent, 1);
14f8ab
-        }
14f8ab
-
14f8ab
-        if (local->need_attrheal) {
14f8ab
-            local->need_attrheal = 0;
14f8ab
-            if (!__is_root_gfid(inode->gfid)) {
14f8ab
-                local->stbuf.ia_gid = local->mds_stbuf.ia_gid;
14f8ab
-                local->stbuf.ia_uid = local->mds_stbuf.ia_uid;
14f8ab
-                local->stbuf.ia_prot = local->mds_stbuf.ia_prot;
14f8ab
+            if (local->inode) {
14f8ab
+                dht_inode_ctx_time_update(local->inode, this, &local->stbuf, 1);
14f8ab
             }
14f8ab
-            copy = create_frame(this, this->ctx->pool);
14f8ab
-            if (copy) {
14f8ab
-                copy_local = dht_local_init(copy, &local->loc, NULL, 0);
14f8ab
-                if (!copy_local) {
14f8ab
-                    DHT_STACK_DESTROY(copy);
14f8ab
-                    goto skip_attr_heal;
14f8ab
-                }
14f8ab
-                copy_local->stbuf = local->stbuf;
14f8ab
-                gf_uuid_copy(copy_local->loc.gfid, local->stbuf.ia_gfid);
14f8ab
-                copy_local->mds_stbuf = local->mds_stbuf;
14f8ab
-                copy_local->mds_subvol = local->mds_subvol;
14f8ab
-                copy->local = copy_local;
14f8ab
-                FRAME_SU_DO(copy, dht_local_t);
14f8ab
-                ret = synctask_new(this->ctx->env, dht_dir_attr_heal,
14f8ab
-                                   dht_dir_attr_heal_done, copy, copy);
14f8ab
-                if (ret) {
14f8ab
-                    gf_msg(this->name, GF_LOG_ERROR, ENOMEM,
14f8ab
-                           DHT_MSG_DIR_ATTR_HEAL_FAILED,
14f8ab
-                           "Synctask creation failed to heal attr "
14f8ab
-                           "for path %s gfid %s ",
14f8ab
-                           local->loc.path, local->gfid);
14f8ab
-                    DHT_STACK_DESTROY(copy);
14f8ab
-                }
14f8ab
+
14f8ab
+            if (local->loc.parent) {
14f8ab
+                dht_inode_ctx_time_update(local->loc.parent, this,
14f8ab
+                                          &local->postparent, 1);
14f8ab
             }
14f8ab
         }
14f8ab
 
14f8ab
-    skip_attr_heal:
14f8ab
         DHT_STRIP_PHASE1_FLAGS(&local->stbuf);
14f8ab
         dht_set_fixed_dir_stat(&local->postparent);
14f8ab
         /* Delete mds xattr at the time of STACK UNWIND */
14f8ab
         if (local->xattr)
14f8ab
             GF_REMOVE_INTERNAL_XATTR(conf->mds_xattr_key, local->xattr);
14f8ab
+
14f8ab
         DHT_STACK_UNWIND(lookup, frame, local->op_ret, local->op_errno,
14f8ab
                          local->inode, &local->stbuf, local->xattr,
14f8ab
                          &local->postparent);
14f8ab
@@ -5444,9 +5423,8 @@ dht_dir_common_set_remove_xattr(call_frame_t *frame, xlator_t *this, loc_t *loc,
14f8ab
         } else {
14f8ab
             gf_msg(this->name, GF_LOG_ERROR, 0,
14f8ab
                    DHT_MSG_HASHED_SUBVOL_GET_FAILED,
14f8ab
-                   "Failed to get mds subvol for path %s"
14f8ab
-                   "gfid is %s ",
14f8ab
-                   loc->path, gfid_local);
14f8ab
+                   "%s: Failed to get mds subvol. (gfid is %s)", loc->path,
14f8ab
+                   gfid_local);
14f8ab
         }
14f8ab
         (*op_errno) = ENOENT;
14f8ab
         goto err;
14f8ab
diff --git a/xlators/cluster/dht/src/dht-selfheal.c b/xlators/cluster/dht/src/dht-selfheal.c
14f8ab
index bd1b7ea..5420fca 100644
14f8ab
--- a/xlators/cluster/dht/src/dht-selfheal.c
14f8ab
+++ b/xlators/cluster/dht/src/dht-selfheal.c
14f8ab
@@ -1033,18 +1033,27 @@ dht_selfheal_dir_setattr(call_frame_t *frame, loc_t *loc, struct iatt *stbuf,
14f8ab
     int missing_attr = 0;
14f8ab
     int i = 0, ret = -1;
14f8ab
     dht_local_t *local = NULL;
14f8ab
+    dht_conf_t *conf = NULL;
14f8ab
     xlator_t *this = NULL;
14f8ab
     int cnt = 0;
14f8ab
 
14f8ab
     local = frame->local;
14f8ab
     this = frame->this;
14f8ab
+    conf = this->private;
14f8ab
+
14f8ab
+    /* We need to heal the attrs if:
14f8ab
+     * 1. Any directories were missing - the newly created dirs will need
14f8ab
+     *    to have the correct attrs set
14f8ab
+     * 2. An existing dir does not have the correct permissions -they may
14f8ab
+     *    have been changed when a brick was down.
14f8ab
+     */
14f8ab
 
14f8ab
     for (i = 0; i < layout->cnt; i++) {
14f8ab
         if (layout->list[i].err == -1)
14f8ab
             missing_attr++;
14f8ab
     }
14f8ab
 
14f8ab
-    if (missing_attr == 0) {
14f8ab
+    if ((missing_attr == 0) && (local->need_attrheal == 0)) {
14f8ab
         if (!local->heal_layout) {
14f8ab
             gf_msg_trace(this->name, 0, "Skip heal layout for %s gfid = %s ",
14f8ab
                          loc->path, uuid_utoa(loc->gfid));
14f8ab
@@ -1062,19 +1071,12 @@ dht_selfheal_dir_setattr(call_frame_t *frame, loc_t *loc, struct iatt *stbuf,
14f8ab
         return 0;
14f8ab
     }
14f8ab
 
14f8ab
-    local->call_cnt = missing_attr;
14f8ab
-    cnt = layout->cnt;
14f8ab
+    cnt = local->call_cnt = conf->subvolume_cnt;
14f8ab
 
14f8ab
     for (i = 0; i < cnt; i++) {
14f8ab
-        if (layout->list[i].err == -1) {
14f8ab
-            gf_msg_trace(this->name, 0, "%s: setattr on subvol %s, gfid = %s",
14f8ab
-                         loc->path, layout->list[i].xlator->name,
14f8ab
-                         uuid_utoa(loc->gfid));
14f8ab
-
14f8ab
-            STACK_WIND(
14f8ab
-                frame, dht_selfheal_dir_setattr_cbk, layout->list[i].xlator,
14f8ab
-                layout->list[i].xlator->fops->setattr, loc, stbuf, valid, NULL);
14f8ab
-        }
14f8ab
+        STACK_WIND(frame, dht_selfheal_dir_setattr_cbk, layout->list[i].xlator,
14f8ab
+                   layout->list[i].xlator->fops->setattr, loc, stbuf, valid,
14f8ab
+                   NULL);
14f8ab
     }
14f8ab
 
14f8ab
     return 0;
14f8ab
@@ -1492,6 +1494,9 @@ dht_selfheal_dir_mkdir(call_frame_t *frame, loc_t *loc, dht_layout_t *layout,
14f8ab
     }
14f8ab
 
14f8ab
     if (missing_dirs == 0) {
14f8ab
+        /* We don't need to create any directories. Proceed to heal the
14f8ab
+         * attrs and xattrs
14f8ab
+         */
14f8ab
         if (!__is_root_gfid(local->stbuf.ia_gfid)) {
14f8ab
             if (local->need_xattr_heal) {
14f8ab
                 local->need_xattr_heal = 0;
14f8ab
@@ -1499,8 +1504,8 @@ dht_selfheal_dir_mkdir(call_frame_t *frame, loc_t *loc, dht_layout_t *layout,
14f8ab
                 if (ret)
14f8ab
                     gf_msg(this->name, GF_LOG_ERROR, ret,
14f8ab
                            DHT_MSG_DIR_XATTR_HEAL_FAILED,
14f8ab
-                           "xattr heal failed for "
14f8ab
-                           "directory  %s gfid %s ",
14f8ab
+                           "%s:xattr heal failed for "
14f8ab
+                           "directory (gfid = %s)",
14f8ab
                            local->loc.path, local->gfid);
14f8ab
             } else {
14f8ab
                 if (!gf_uuid_is_null(local->gfid))
14f8ab
@@ -1512,8 +1517,8 @@ dht_selfheal_dir_mkdir(call_frame_t *frame, loc_t *loc, dht_layout_t *layout,
14f8ab
 
14f8ab
                 gf_msg(this->name, GF_LOG_INFO, 0,
14f8ab
                        DHT_MSG_DIR_XATTR_HEAL_FAILED,
14f8ab
-                       "Failed to set mds xattr "
14f8ab
-                       "for directory  %s gfid %s ",
14f8ab
+                       "%s: Failed to set mds xattr "
14f8ab
+                       "for directory (gfid = %s)",
14f8ab
                        local->loc.path, local->gfid);
14f8ab
             }
14f8ab
         }
14f8ab
@@ -2085,10 +2090,10 @@ dht_selfheal_directory(call_frame_t *frame, dht_selfheal_dir_cbk_t dir_cbk,
14f8ab
                        loc_t *loc, dht_layout_t *layout)
14f8ab
 {
14f8ab
     dht_local_t *local = NULL;
14f8ab
+    xlator_t *this = NULL;
14f8ab
     uint32_t down = 0;
14f8ab
     uint32_t misc = 0;
14f8ab
     int ret = 0;
14f8ab
-    xlator_t *this = NULL;
14f8ab
     char pgfid[GF_UUID_BUF_SIZE] = {0};
14f8ab
     char gfid[GF_UUID_BUF_SIZE] = {0};
14f8ab
     inode_t *linked_inode = NULL, *inode = NULL;
14f8ab
@@ -2099,6 +2104,11 @@ dht_selfheal_directory(call_frame_t *frame, dht_selfheal_dir_cbk_t dir_cbk,
14f8ab
     local->selfheal.dir_cbk = dir_cbk;
14f8ab
     local->selfheal.layout = dht_layout_ref(this, layout);
14f8ab
 
14f8ab
+    if (local->need_attrheal && !IA_ISINVAL(local->mds_stbuf.ia_type)) {
14f8ab
+        /*Use the one in the mds_stbuf*/
14f8ab
+        local->stbuf = local->mds_stbuf;
14f8ab
+    }
14f8ab
+
14f8ab
     if (!__is_root_gfid(local->stbuf.ia_gfid)) {
14f8ab
         gf_uuid_unparse(local->stbuf.ia_gfid, gfid);
14f8ab
         gf_uuid_unparse(loc->parent->gfid, pgfid);
14f8ab
@@ -2118,6 +2128,13 @@ dht_selfheal_directory(call_frame_t *frame, dht_selfheal_dir_cbk_t dir_cbk,
14f8ab
         inode_unref(inode);
14f8ab
     }
14f8ab
 
14f8ab
+    if (local->need_xattr_heal && (local->mds_xattr)) {
14f8ab
+        dht_dir_set_heal_xattr(this, local, local->xattr, local->mds_xattr,
14f8ab
+                               NULL, NULL);
14f8ab
+        dict_unref(local->mds_xattr);
14f8ab
+        local->mds_xattr = NULL;
14f8ab
+    }
14f8ab
+
14f8ab
     dht_layout_anomalies(this, loc, layout, &local->selfheal.hole_cnt,
14f8ab
                          &local->selfheal.overlaps_cnt,
14f8ab
                          &local->selfheal.missing_cnt, &local->selfheal.down,
14f8ab
@@ -2128,18 +2145,18 @@ dht_selfheal_directory(call_frame_t *frame, dht_selfheal_dir_cbk_t dir_cbk,
14f8ab
 
14f8ab
     if (down) {
14f8ab
         gf_msg(this->name, GF_LOG_WARNING, 0, DHT_MSG_DIR_SELFHEAL_FAILED,
14f8ab
-               "Directory selfheal failed: %d subvolumes down."
14f8ab
-               "Not fixing. path = %s, gfid = %s",
14f8ab
-               down, loc->path, gfid);
14f8ab
+               "%s: Directory selfheal failed: %d subvolumes down."
14f8ab
+               "Not fixing. gfid = %s",
14f8ab
+               loc->path, down, gfid);
14f8ab
         ret = 0;
14f8ab
         goto sorry_no_fix;
14f8ab
     }
14f8ab
 
14f8ab
     if (misc) {
14f8ab
         gf_msg(this->name, GF_LOG_WARNING, 0, DHT_MSG_DIR_SELFHEAL_FAILED,
14f8ab
-               "Directory selfheal failed : %d subvolumes "
14f8ab
-               "have unrecoverable errors. path = %s, gfid = %s",
14f8ab
-               misc, loc->path, gfid);
14f8ab
+               "%s: Directory selfheal failed : %d subvolumes "
14f8ab
+               "have unrecoverable errors. gfid = %s",
14f8ab
+               loc->path, misc, gfid);
14f8ab
 
14f8ab
         ret = 0;
14f8ab
         goto sorry_no_fix;
14f8ab
@@ -2369,13 +2386,13 @@ dht_dir_attr_heal(void *data)
14f8ab
 
14f8ab
     frame = data;
14f8ab
     local = frame->local;
14f8ab
-    mds_subvol = local->mds_subvol;
14f8ab
     this = frame->this;
14f8ab
     GF_VALIDATE_OR_GOTO("dht", this, out);
14f8ab
     GF_VALIDATE_OR_GOTO("dht", local, out);
14f8ab
     conf = this->private;
14f8ab
     GF_VALIDATE_OR_GOTO("dht", conf, out);
14f8ab
 
14f8ab
+    mds_subvol = local->mds_subvol;
14f8ab
     call_cnt = conf->subvolume_cnt;
14f8ab
 
14f8ab
     if (!__is_root_gfid(local->stbuf.ia_gfid) && (!mds_subvol)) {
14f8ab
-- 
14f8ab
1.8.3.1
14f8ab