a3470f
From 27081fda822921e7f452304bea170d2d13cba257 Mon Sep 17 00:00:00 2001
a3470f
From: Mohit Agrawal <moagrawa@redhat.com>
a3470f
Date: Fri, 12 May 2017 21:12:47 +0530
a3470f
Subject: [PATCH 205/212] cluster/dht : User xattrs are not healed after brick
a3470f
 stop/start
a3470f
a3470f
Problem: In a distributed volume custom extended attribute value for a directory
a3470f
         does not display correct value after stop/start or added newly brick.
a3470f
         If any extended(acl) attribute value is set for a directory after stop/added
a3470f
         the brick the attribute(user|acl|quota) value is not updated on brick
a3470f
         after start the brick.
a3470f
a3470f
Solution: First store hashed subvol or subvol(has internal xattr) on inode ctx and
a3470f
          consider it as a MDS subvol.At the time of update custom xattr
a3470f
          (user,quota,acl, selinux) on directory first check the mds from
a3470f
          inode ctx, if mds is not present on inode ctx then throw EINVAL error
a3470f
          to application otherwise set xattr on MDS subvol with internal xattr
a3470f
          value of -1 and then try to update the attribute on other non MDS
a3470f
          volumes also.If mds subvol is down in that case throw an
a3470f
          error "Transport endpoint is not connected". In dht_dir_lookup_cbk|
a3470f
          dht_revalidate_cbk|dht_discover_complete call dht_call_dir_xattr_heal
a3470f
          to heal custom extended attribute.
a3470f
          In case of gnfs server if hashed subvol has not found based on
a3470f
          loc then wind a call on all subvol to update xattr.
a3470f
a3470f
Fix:    1) Save MDS subvol on inode ctx
a3470f
        2) Check if mds subvol is present on inode ctx
a3470f
        3) If mds subvol is down then call unwind with error ENOTCONN and if it is up
a3470f
           then set new xattr "GF_DHT_XATTR_MDS" to -1 and wind a call on other
a3470f
           subvol.
a3470f
        4) If setxattr fop is successful on non-mds subvol then increment the value of
a3470f
           internal xattr to +1
a3470f
        5) At the time of directory_lookup check the value of new xattr GF_DHT_XATTR_MDS
a3470f
        6) If value is not 0 in dht_lookup_dir_cbk(other cbk) functions then call heal
a3470f
           function to heal user xattr
a3470f
        7) syncop_setxattr on hashed_subvol to reset the value of xattr to 0
a3470f
           if heal is successful on all subvol.
a3470f
a3470f
Test : To reproduce the issue followed below steps
a3470f
       1) Create a distributed volume and create mount point
a3470f
       2) Create some directory from mount point mkdir tmp{1..5}
a3470f
       3) Kill any one brick from the volume
a3470f
       4) Set extended attribute from mount point on directory
a3470f
          setfattr -n user.foo -v "abc" ./tmp{1..5}
a3470f
          It will throw error " Transport End point is not connected "
a3470f
          for those hashed subvol is down
a3470f
       5) Start volume with force option to start brick process
a3470f
       6) Execute getfattr command on mount point for directory
a3470f
       7) Check extended attribute on brick
a3470f
          getfattr -n user.foo <volume-location>/tmp{1..5}
a3470f
          It shows correct value for directories for those
a3470f
          xattr fop were executed successfully.
a3470f
a3470f
Note: The patch will resolve xattr healing problem only for fuse mount
a3470f
      not for nfs mount.
a3470f
a3470f
> BUG: 1371806
a3470f
> Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
a3470f
> (Cherry pick from commit 9b4de61a136b8e5ba7bf0e48690cdb1292d0dee8)
a3470f
> (Upstream patch link https://review.gluster.org/#/c/15468/)
a3470f
a3470f
BUG: 1550315
a3470f
Change-Id: I4eb137eace24a8cb796712b742f1d177a65343d5
a3470f
Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
a3470f
Reviewed-on: https://code.engineering.redhat.com/gerrit/132383
a3470f
Tested-by: RHGS Build Bot <nigelb@redhat.com>
a3470f
Reviewed-by: Raghavendra Gowdappa <rgowdapp@redhat.com>
a3470f
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
a3470f
---
a3470f
 tests/bugs/bug-1368312.t                  |   30 +-
a3470f
 tests/bugs/bug-1371806.t                  |   80 ++
a3470f
 tests/bugs/bug-1371806_1.t                |   49 +
a3470f
 tests/bugs/bug-1371806_2.t                |   52 ++
a3470f
 tests/bugs/bug-1371806_3.t                |   63 ++
a3470f
 tests/bugs/bug-1371806_acl.t              |   90 ++
a3470f
 tests/bugs/distribute/bug-862967.t        |    7 +-
a3470f
 xlators/cluster/dht/src/dht-common.c      | 1389 ++++++++++++++++++++++++++---
a3470f
 xlators/cluster/dht/src/dht-common.h      |   72 +-
a3470f
 xlators/cluster/dht/src/dht-helper.c      |   65 ++
a3470f
 xlators/cluster/dht/src/dht-inode-write.c |  163 +++-
a3470f
 xlators/cluster/dht/src/dht-messages.h    |   28 +-
a3470f
 xlators/cluster/dht/src/dht-selfheal.c    |  519 ++++++++++-
a3470f
 xlators/cluster/dht/src/dht-shared.c      |    2 +
a3470f
 14 files changed, 2436 insertions(+), 173 deletions(-)
a3470f
 create mode 100644 tests/bugs/bug-1371806.t
a3470f
 create mode 100644 tests/bugs/bug-1371806_1.t
a3470f
 create mode 100644 tests/bugs/bug-1371806_2.t
a3470f
 create mode 100644 tests/bugs/bug-1371806_3.t
a3470f
 create mode 100644 tests/bugs/bug-1371806_acl.t
a3470f
a3470f
diff --git a/tests/bugs/bug-1368312.t b/tests/bugs/bug-1368312.t
a3470f
index 135048f..61e5606 100644
a3470f
--- a/tests/bugs/bug-1368312.t
a3470f
+++ b/tests/bugs/bug-1368312.t
a3470f
@@ -29,46 +29,46 @@ TEST glusterfs --volfile-id=$V0 --volfile-server=$H0 --entry-timeout=0 $M0;
a3470f
 TEST mkdir $M0/tmp1
a3470f
 
a3470f
 #Create metadata split-brain
a3470f
-TEST kill_brick $V0 $H0 $B0/${V0}0
a3470f
+TEST kill_brick $V0 $H0 $B0/${V0}2
a3470f
 TEST chmod 666 $M0/tmp1
a3470f
 TEST $CLI volume start $V0 force
a3470f
-TEST kill_brick $V0 $H0 $B0/${V0}1
a3470f
-EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" afr_child_up_status $V0 0
a3470f
+TEST kill_brick $V0 $H0 $B0/${V0}3
a3470f
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" afr_child_up_status $V0 2
a3470f
 
a3470f
 TEST chmod 757 $M0/tmp1
a3470f
 
a3470f
 TEST $CLI volume start $V0 force
a3470f
-EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" afr_child_up_status $V0 0
a3470f
-EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" afr_child_up_status $V0 1
a3470f
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" afr_child_up_status $V0 2
a3470f
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" afr_child_up_status $V0 3
a3470f
 
a3470f
 EXPECT 2 get_pending_heal_count $V0
a3470f
 
a3470f
 
a3470f
-TEST kill_brick $V0 $H0 $B0/${V0}2
a3470f
+TEST kill_brick $V0 $H0 $B0/${V0}4
a3470f
 TEST chmod 755 $M0/tmp1
a3470f
 TEST $CLI volume start $V0 force
a3470f
-TEST kill_brick $V0 $H0 $B0/${V0}3
a3470f
-EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" afr_child_up_status $V0 2
a3470f
+TEST kill_brick $V0 $H0 $B0/${V0}5
a3470f
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" afr_child_up_status $V0 4
a3470f
 
a3470f
 TEST chmod 766 $M0/tmp1
a3470f
 
a3470f
 TEST $CLI volume start $V0 force
a3470f
-EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" afr_child_up_status $V0 2
a3470f
-EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" afr_child_up_status $V0 3
a3470f
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" afr_child_up_status $V0 4
a3470f
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" afr_child_up_status $V0 5
a3470f
 
a3470f
 EXPECT 4 get_pending_heal_count $V0
a3470f
 
a3470f
-TEST kill_brick $V0 $H0 $B0/${V0}4
a3470f
+TEST kill_brick $V0 $H0 $B0/${V0}0
a3470f
 TEST chmod 765 $M0/tmp1
a3470f
 TEST $CLI volume start $V0 force
a3470f
-TEST kill_brick $V0 $H0 $B0/${V0}5
a3470f
-EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" afr_child_up_status $V0 4
a3470f
+TEST kill_brick $V0 $H0 $B0/${V0}1
a3470f
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" afr_child_up_status $V0 0
a3470f
 
a3470f
 TEST chmod 756 $M0/tmp1
a3470f
 
a3470f
 TEST $CLI volume start $V0 force
a3470f
-EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" afr_child_up_status $V0 4
a3470f
-EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" afr_child_up_status $V0 5
a3470f
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" afr_child_up_status $V0 0
a3470f
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" afr_child_up_status $V0 1
a3470f
 
a3470f
 EXPECT 6 get_pending_heal_count $V0
a3470f
 
a3470f
diff --git a/tests/bugs/bug-1371806.t b/tests/bugs/bug-1371806.t
a3470f
new file mode 100644
a3470f
index 0000000..7dc1613
a3470f
--- /dev/null
a3470f
+++ b/tests/bugs/bug-1371806.t
a3470f
@@ -0,0 +1,80 @@
a3470f
+#!/bin/bash
a3470f
+. $(dirname $0)/../include.rc
a3470f
+. $(dirname $0)/../volume.rc
a3470f
+. $(dirname $0)/../dht.rc
a3470f
+cleanup;
a3470f
+
a3470f
+function get_getfattr {
a3470f
+        local path=$1
a3470f
+        echo `getfattr -n user.foo $path` | cut -f2 -d"=" | sed -e 's/^"//'  -e 's/"$//'
a3470f
+}
a3470f
+
a3470f
+function set_fattr {
a3470f
+        for i in `seq 1 10`
a3470f
+        do
a3470f
+                setfattr -n user.foo -v "newabc" ./tmp${i}
a3470f
+                if [ "$?" = "0" ]
a3470f
+                 then
a3470f
+                    succ=$((succ+1))
a3470f
+                else
a3470f
+                    fail=$((fail+1))
a3470f
+                fi
a3470f
+        done
a3470f
+}
a3470f
+
a3470f
+
a3470f
+
a3470f
+TEST glusterd
a3470f
+TEST pidof glusterd
a3470f
+TEST $CLI volume create $V0 $H0:$B0/${V0}{0,1,2,3,4,5}
a3470f
+TEST $CLI volume start $V0
a3470f
+
a3470f
+TEST glusterfs --volfile-id=$V0 --volfile-server=$H0 --entry-timeout=0 $M0;
a3470f
+
a3470f
+cd $M0
a3470f
+TEST mkdir tmp{1..10}
a3470f
+
a3470f
+##First set user.foo xattr with value abc on all dirs
a3470f
+
a3470f
+TEST setfattr -n user.foo -v "abc" ./tmp{1..10}
a3470f
+EXPECT "abc" get_getfattr ./tmp{1..10}
a3470f
+EXPECT "abc" get_getfattr $B0/${V0}5/tmp{1..10}
a3470f
+
a3470f
+TEST kill_brick $V0 $H0 $B0/${V0}5
a3470f
+EXPECT_WITHIN ${PROCESS_UP_TIMEOUT} "5" online_brick_count
a3470f
+
a3470f
+succ=fail=0
a3470f
+## set user.foo xattr with value newabc after kill one brick
a3470f
+set_fattr
a3470f
+TEST $CLI volume start $V0 force
a3470f
+EXPECT_WITHIN ${PROCESS_UP_TIMEOUT} "6" online_brick_count
a3470f
+
a3470f
+cd -
a3470f
+TEST umount $M0
a3470f
+TEST glusterfs --volfile-id=$V0 --volfile-server=$H0 --entry-timeout=0 $M0;
a3470f
+
a3470f
+cd $M0
a3470f
+## At this point dht code will heal xattr on down brick only for those dirs
a3470f
+## hashed subvol was up at the time of update xattr
a3470f
+TEST stat ./tmp{1..10}
a3470f
+
a3470f
+## Count the user.foo xattr value with abc on mount point and compare with fail value
a3470f
+count=`getfattr -n user.foo ./tmp{1..10} | grep "user.foo" | grep -iw "abc" | wc -l`
a3470f
+EXPECT "$fail" echo $count
a3470f
+
a3470f
+## Count the user.foo xattr value with newabc on mount point and compare with succ value
a3470f
+count=`getfattr -n user.foo ./tmp{1..10} | grep "user.foo" | grep -iw "newabc" | wc -l`
a3470f
+EXPECT "$succ" echo $count
a3470f
+
a3470f
+## Count the user.foo xattr value with abc on brick and compare with succ value
a3470f
+count=`getfattr -n user.foo $B0/${V0}5/tmp{1..10} | grep "user.foo" | grep -iw "abc" | wc -l`
a3470f
+EXPECT "$fail" echo $count
a3470f
+
a3470f
+## Count the user.foo xattr value with newabc on brick and compare with succ value
a3470f
+count=`getfattr -n user.foo $B0/${V0}5/tmp{1..10} | grep "user.foo" | grep -iw "newabc" | wc -l`
a3470f
+EXPECT "$succ" echo $count
a3470f
+
a3470f
+
a3470f
+cd -
a3470f
+cleanup
a3470f
+exit
a3470f
diff --git a/tests/bugs/bug-1371806_1.t b/tests/bugs/bug-1371806_1.t
a3470f
new file mode 100644
a3470f
index 0000000..44a57a9
a3470f
--- /dev/null
a3470f
+++ b/tests/bugs/bug-1371806_1.t
a3470f
@@ -0,0 +1,49 @@
a3470f
+#!/bin/bash
a3470f
+. $(dirname $0)/../include.rc
a3470f
+. $(dirname $0)/../volume.rc
a3470f
+. $(dirname $0)/../dht.rc
a3470f
+cleanup;
a3470f
+
a3470f
+function get_getfattr {
a3470f
+        local path=$1
a3470f
+        echo `getfattr -n user.foo $path` | cut -f2 -d"=" | sed -e 's/^"//'  -e 's/"$//'
a3470f
+}
a3470f
+
a3470f
+function remove_mds_xattr {
a3470f
+
a3470f
+       for i in `seq 1 10`
a3470f
+       do
a3470f
+               setfattr -x trusted.glusterfs.dht.mds $1/tmp${i} 2> /dev/null
a3470f
+       done
a3470f
+}
a3470f
+
a3470f
+
a3470f
+
a3470f
+TEST glusterd
a3470f
+TEST pidof glusterd
a3470f
+TEST $CLI volume create $V0 $H0:$B0/${V0}{0,1,2,3}
a3470f
+TEST $CLI volume start $V0
a3470f
+
a3470f
+TEST glusterfs --volfile-id=$V0 --volfile-server=$H0 $M0;
a3470f
+
a3470f
+cd $M0
a3470f
+TEST mkdir tmp{1..10}
a3470f
+
a3470f
+##Remove internal mds xattr from all directory
a3470f
+remove_mds_xattr $B0/${V0}0
a3470f
+remove_mds_xattr $B0/${V0}1
a3470f
+remove_mds_xattr $B0/${V0}2
a3470f
+remove_mds_xattr $B0/${V0}3
a3470f
+
a3470f
+cd -
a3470f
+umount $M0
a3470f
+
a3470f
+TEST glusterfs --volfile-id=$V0 --volfile-server=$H0 $M0;
a3470f
+cd $M0
a3470f
+
a3470f
+TEST setfattr -n user.foo -v "abc" ./tmp{1..10}
a3470f
+EXPECT "abc" get_getfattr ./tmp{1..10}
a3470f
+
a3470f
+cd -
a3470f
+cleanup
a3470f
+exit
a3470f
diff --git a/tests/bugs/bug-1371806_2.t b/tests/bugs/bug-1371806_2.t
a3470f
new file mode 100644
a3470f
index 0000000..e6aa8e7
a3470f
--- /dev/null
a3470f
+++ b/tests/bugs/bug-1371806_2.t
a3470f
@@ -0,0 +1,52 @@
a3470f
+#!/bin/bash
a3470f
+. $(dirname $0)/../include.rc
a3470f
+. $(dirname $0)/../volume.rc
a3470f
+. $(dirname $0)/../dht.rc
a3470f
+cleanup;
a3470f
+
a3470f
+function get_getfattr {
a3470f
+        local path=$1
a3470f
+        echo `getfattr -n user.foo $path` | cut -f2 -d"=" | sed -e 's/^"//'  -e 's/"$//'
a3470f
+}
a3470f
+
a3470f
+function remove_mds_xattr {
a3470f
+
a3470f
+       for i in `seq 1 10`
a3470f
+       do
a3470f
+               setfattr -x trusted.glusterfs.dht.mds $1/tmp${i} 2> /dev/null
a3470f
+       done
a3470f
+}
a3470f
+
a3470f
+
a3470f
+
a3470f
+
a3470f
+TEST glusterd
a3470f
+TEST pidof glusterd
a3470f
+TEST $CLI volume create $V0 $H0:$B0/${V0}{0,1,2,3}
a3470f
+TEST $CLI volume start $V0
a3470f
+
a3470f
+TEST glusterfs --volfile-id=$V0 --volfile-server=$H0 --entry-timeout=0 --attribute-timeout=0 $M0;
a3470f
+cd $M0
a3470f
+TEST mkdir tmp{1..10}
a3470f
+
a3470f
+##Remove internal mds xattr from all directory
a3470f
+remove_mds_xattr $B0/${V0}0
a3470f
+remove_mds_xattr $B0/${V0}1
a3470f
+remove_mds_xattr $B0/${V0}2
a3470f
+remove_mds_xattr $B0/${V0}3
a3470f
+
a3470f
+##First set user.foo xattr with value abc on all dirs
a3470f
+
a3470f
+TEST setfattr -n user.foo -v "abc" ./tmp{1..10}
a3470f
+EXPECT "abc" get_getfattr ./tmp{1..10}
a3470f
+EXPECT "abc" get_getfattr $B0/${V0}0/tmp{1..10}
a3470f
+EXPECT "abc" get_getfattr $B0/${V0}1/tmp{1..10}
a3470f
+EXPECT "abc" get_getfattr $B0/${V0}2/tmp{1..10}
a3470f
+EXPECT "abc" get_getfattr $B0/${V0}3/tmp{1..10}
a3470f
+
a3470f
+cd -
a3470f
+TEST umount $M0
a3470f
+
a3470f
+cd -
a3470f
+cleanup
a3470f
+exit
a3470f
diff --git a/tests/bugs/bug-1371806_3.t b/tests/bugs/bug-1371806_3.t
a3470f
new file mode 100644
a3470f
index 0000000..cb13f37
a3470f
--- /dev/null
a3470f
+++ b/tests/bugs/bug-1371806_3.t
a3470f
@@ -0,0 +1,63 @@
a3470f
+#!/bin/bash
a3470f
+. $(dirname $0)/../include.rc
a3470f
+. $(dirname $0)/../volume.rc
a3470f
+. $(dirname $0)/../dht.rc
a3470f
+cleanup;
a3470f
+
a3470f
+function get_getfattr {
a3470f
+        local path=$1
a3470f
+        echo `getfattr -n user.foo $path` | cut -f2 -d"=" | sed -e 's/^"//'  -e 's/"$//'
a3470f
+}
a3470f
+
a3470f
+function set_fattr {
a3470f
+        for i in `seq 1 10`
a3470f
+        do
a3470f
+                setfattr -n user.foo -v "newabc" ./tmp${i}
a3470f
+                if [ "$?" = "0" ]
a3470f
+                 then
a3470f
+                    succ=$((succ+1))
a3470f
+                else
a3470f
+                    fail=$((fail+1))
a3470f
+                fi
a3470f
+        done
a3470f
+}
a3470f
+
a3470f
+
a3470f
+
a3470f
+TEST glusterd
a3470f
+TEST pidof glusterd
a3470f
+TEST $CLI volume create $V0 $H0:$B0/${V0}{0,1,2,3}
a3470f
+TEST $CLI volume start $V0
a3470f
+
a3470f
+TEST glusterfs --volfile-id=$V0 --volfile-server=$H0 --entry-timeout=0 --attribute-timeout=0 $M0;
a3470f
+
a3470f
+cd $M0
a3470f
+TEST mkdir tmp{1..10}
a3470f
+
a3470f
+TEST kill_brick $V0 $H0 $B0/${V0}3
a3470f
+EXPECT_WITHIN ${PROCESS_UP_TIMEOUT} "3" online_brick_count
a3470f
+
a3470f
+succ=fail=0
a3470f
+## set user.foo xattr with value newabc after kill one brick
a3470f
+set_fattr
a3470f
+TEST $CLI volume start $V0 force
a3470f
+EXPECT_WITHIN ${PROCESS_UP_TIMEOUT} "4" online_brick_count
a3470f
+
a3470f
+cd -
a3470f
+TEST umount $M0
a3470f
+TEST glusterfs --volfile-id=$V0 --volfile-server=$H0 --entry-timeout=0 --attribute-timeout=0 $M0;
a3470f
+
a3470f
+cd $M0
a3470f
+## At this point dht code will heal xattr on down brick only for those dirs
a3470f
+## hashed subvol was up at the time of update xattr
a3470f
+TEST stat ./tmp{1..10}
a3470f
+
a3470f
+
a3470f
+## Count the user.foo xattr value with newabc on brick and compare with succ value
a3470f
+count=`getfattr -n user.foo $B0/${V0}3/tmp{1..10} | grep "user.foo" | grep -iw "newabc" | wc -l`
a3470f
+EXPECT "$succ" echo $count
a3470f
+
a3470f
+
a3470f
+cd -
a3470f
+cleanup
a3470f
+exit
a3470f
diff --git a/tests/bugs/bug-1371806_acl.t b/tests/bugs/bug-1371806_acl.t
a3470f
new file mode 100644
a3470f
index 0000000..aa41e04
a3470f
--- /dev/null
a3470f
+++ b/tests/bugs/bug-1371806_acl.t
a3470f
@@ -0,0 +1,90 @@
a3470f
+#!/bin/bash
a3470f
+. $(dirname $0)/../include.rc
a3470f
+. $(dirname $0)/../volume.rc
a3470f
+
a3470f
+cleanup;
a3470f
+TEST useradd tmpuser
a3470f
+
a3470f
+function set_facl_user {
a3470f
+        for i in `seq 1 10`
a3470f
+        do
a3470f
+                setfacl -m u:tmpuser:rw ./tmp${i}
a3470f
+                if [ "$?" = "0" ]
a3470f
+                 then
a3470f
+                    succ=$((succ+1))
a3470f
+                else
a3470f
+                    fail=$((fail+1))
a3470f
+                fi
a3470f
+        done
a3470f
+}
a3470f
+
a3470f
+function set_facl_default {
a3470f
+        for i in `seq 1 10`
a3470f
+        do
a3470f
+                setfacl -m d:o:rw ./tmp${i}
a3470f
+                if [ "$?" = "0" ]
a3470f
+                 then
a3470f
+                    succ1=$((succ1+1))
a3470f
+                else
a3470f
+                    fail1=$((fail1+1))
a3470f
+                fi
a3470f
+        done
a3470f
+}
a3470f
+
a3470f
+
a3470f
+
a3470f
+
a3470f
+TEST glusterd
a3470f
+TEST pidof glusterd
a3470f
+TEST $CLI volume create $V0 $H0:$B0/${V0}{0,1,2,3,4,5}
a3470f
+TEST $CLI volume set $V0 diagnostics.client-log-level DEBUG
a3470f
+TEST $CLI volume start $V0
a3470f
+
a3470f
+TEST glusterfs --volfile-id=$V0 --acl --volfile-server=$H0 --entry-timeout=0 $M0;
a3470f
+
a3470f
+cd $M0
a3470f
+TEST mkdir tmp{1..10}
a3470f
+TEST setfacl -m u:tmpuser:rwx ./tmp{1..10}
a3470f
+count=`getfacl -p $M0/tmp{1..10} | grep -c "user:tmpuser:rwx"`
a3470f
+EXPECT "10" echo $count
a3470f
+TEST setfacl -m d:o:rwx ./tmp{1..10}
a3470f
+count=`getfacl -p $M0/tmp{1..10} | grep -c "default:other::rwx"`
a3470f
+EXPECT "10" echo $count
a3470f
+count=`getfacl -p $B0/${V0}5/tmp{1..10} | grep -c "user:tmpuser:rwx"`
a3470f
+EXPECT "10" echo $count
a3470f
+count=`getfacl -p $B0/${V0}5/tmp{1..10} | grep -c "default:other::rwx"`
a3470f
+EXPECT "10" echo $count
a3470f
+
a3470f
+
a3470f
+TEST kill_brick $V0 $H0 $B0/${V0}5
a3470f
+EXPECT_WITHIN ${PROCESS_UP_TIMEOUT} "5" online_brick_count
a3470f
+
a3470f
+succ=fail=0
a3470f
+## Update acl attributes on dir after kill one brick
a3470f
+set_facl_user
a3470f
+succ1=fail1=0
a3470f
+set_facl_default
a3470f
+
a3470f
+TEST $CLI volume start $V0 force
a3470f
+EXPECT_WITHIN ${PROCESS_UP_TIMEOUT} "6" online_brick_count
a3470f
+
a3470f
+cd -
a3470f
+TEST umount $M0
a3470f
+TEST glusterfs --volfile-id=$V0 --acl --volfile-server=$H0 --entry-timeout=0 $M0;
a3470f
+
a3470f
+cd $M0
a3470f
+## At this point dht will heal xatts on down brick only for those hashed_subvol
a3470f
+## was up at the time of updated xattrs
a3470f
+TEST stat ./tmp{1..10}
a3470f
+
a3470f
+## Compare succ value with updated acl attributes
a3470f
+count=`getfacl -p $B0/${V0}5/tmp{1..10} | grep -c "user:tmpuser:rw-"`
a3470f
+EXPECT "$succ" echo $count
a3470f
+
a3470f
+
a3470f
+count=`getfacl -p $B0/${V0}5/tmp{1..10} | grep -c "default:other::rw-"`
a3470f
+EXPECT "$succ1" echo $count
a3470f
+
a3470f
+cd -
a3470f
+userdel --force tmpuser
a3470f
+cleanup
a3470f
diff --git a/tests/bugs/distribute/bug-862967.t b/tests/bugs/distribute/bug-862967.t
a3470f
index 09dac37..2fb0848 100644
a3470f
--- a/tests/bugs/distribute/bug-862967.t
a3470f
+++ b/tests/bugs/distribute/bug-862967.t
a3470f
@@ -37,7 +37,7 @@ chown 1:1 $M0/dir;
a3470f
 
a3470f
 # Kill a brick process
a3470f
 
a3470f
-kill_brick $V0 $H0 $B0/${V0}1
a3470f
+kill_brick $V0 $H0 $B0/${V0}2
a3470f
 # change dir ownership
a3470f
 NEW_UID=36;
a3470f
 NEW_GID=36;
a3470f
@@ -51,9 +51,8 @@ sleep 10;
a3470f
 ls -l $M0/dir;
a3470f
 
a3470f
 # check if uid/gid is healed on backend brick which was taken down
a3470f
-BACKEND_UID=`stat -c %u $B0/${V0}1/dir`;
a3470f
-BACKEND_GID=`stat -c %g $B0/${V0}1/dir`;
a3470f
-
a3470f
+BACKEND_UID=`stat -c %u $B0/${V0}2/dir`;
a3470f
+BACKEND_GID=`stat -c %g $B0/${V0}2/dir`;
a3470f
 
a3470f
 EXPECT "0" uid_gid_compare $NEW_UID $NEW_GID $BACKEND_UID $BACKEND_GID
a3470f
 
a3470f
diff --git a/xlators/cluster/dht/src/dht-common.c b/xlators/cluster/dht/src/dht-common.c
a3470f
index 5641330..f1e6a92 100644
a3470f
--- a/xlators/cluster/dht/src/dht-common.c
a3470f
+++ b/xlators/cluster/dht/src/dht-common.c
a3470f
@@ -110,6 +110,24 @@ int dht_read_iatt_from_xdata (xlator_t *this, dict_t *xdata,
a3470f
 int
a3470f
 dht_rmdir_unlock (call_frame_t *frame, xlator_t *this);
a3470f
 
a3470f
+char *xattrs_to_heal[] = {
a3470f
+        "user.",
a3470f
+        POSIX_ACL_ACCESS_XATTR,
a3470f
+        POSIX_ACL_DEFAULT_XATTR,
a3470f
+        QUOTA_LIMIT_KEY,
a3470f
+        QUOTA_LIMIT_OBJECTS_KEY,
a3470f
+        GF_SELINUX_XATTR_KEY,
a3470f
+        NULL
a3470f
+};
a3470f
+
a3470f
+/* Return true if key exists in array
a3470f
+*/
a3470f
+static gf_boolean_t
a3470f
+dht_match_xattr (const char *key)
a3470f
+{
a3470f
+        return gf_get_index_by_elem (xattrs_to_heal, (char *)key) >= 0;
a3470f
+}
a3470f
+
a3470f
 int
a3470f
 dht_aggregate_quota_xattr (dict_t *dst, char *key, data_t *value)
a3470f
 {
a3470f
@@ -205,7 +223,7 @@ int add_opt(char **optsp, const char *opt)
a3470f
 }
a3470f
 
a3470f
 /* Return Choice list from Split brain status */
a3470f
-char *
a3470f
+static char *
a3470f
 getChoices (const char *value)
a3470f
 {
a3470f
         int i = 0;
a3470f
@@ -428,6 +446,74 @@ out:
a3470f
         return;
a3470f
 }
a3470f
 
a3470f
+/* Code to save hashed subvol on inode ctx as a mds subvol
a3470f
+*/
a3470f
+int
a3470f
+dht_inode_ctx_mdsvol_set (inode_t *inode, xlator_t *this, xlator_t *mds_subvol)
a3470f
+{
a3470f
+        dht_inode_ctx_t         *ctx            = NULL;
a3470f
+        int                      ret            = -1;
a3470f
+        uint64_t                 ctx_int        = 0;
a3470f
+        gf_boolean_t             ctx_free       = _gf_false;
a3470f
+
a3470f
+
a3470f
+        LOCK (&inode->lock);
a3470f
+        {
a3470f
+                ret = __inode_ctx_get (inode, this , &ctx_int);
a3470f
+                if (ctx_int) {
a3470f
+                        ctx = (dht_inode_ctx_t *)ctx_int;
a3470f
+                        ctx->mds_subvol = mds_subvol;
a3470f
+                } else {
a3470f
+                        ctx = GF_CALLOC (1, sizeof(*ctx), gf_dht_mt_inode_ctx_t);
a3470f
+                        if (!ctx)
a3470f
+                                goto unlock;
a3470f
+                        ctx->mds_subvol = mds_subvol;
a3470f
+                        ctx_free        = _gf_true;
a3470f
+                        ctx_int = (long) ctx;
a3470f
+                        ret =  __inode_ctx_set (inode, this, &ctx_int);
a3470f
+                }
a3470f
+        }
a3470f
+unlock:
a3470f
+        UNLOCK (&inode->lock);
a3470f
+        if (ret && ctx_free)
a3470f
+                GF_FREE (ctx);
a3470f
+        return ret;
a3470f
+}
a3470f
+
a3470f
+/*Code to get mds subvol from inode ctx */
a3470f
+
a3470f
+int
a3470f
+dht_inode_ctx_mdsvol_get (inode_t *inode, xlator_t *this, xlator_t **mdsvol)
a3470f
+{
a3470f
+        dht_inode_ctx_t         *ctx            = NULL;
a3470f
+        int                      ret            = -1;
a3470f
+
a3470f
+        if (!mdsvol)
a3470f
+                return ret;
a3470f
+
a3470f
+        if (__is_root_gfid(inode->gfid)) {
a3470f
+                (*mdsvol) = FIRST_CHILD (this);
a3470f
+                return 0;
a3470f
+        }
a3470f
+
a3470f
+        ret = dht_inode_ctx_get (inode, this, &ctx;;
a3470f
+
a3470f
+        if (!ret && ctx) {
a3470f
+                if (ctx->mds_subvol) {
a3470f
+                        *mdsvol = ctx->mds_subvol;
a3470f
+                        ret = 0;
a3470f
+                } else {
a3470f
+                        ret = -1;
a3470f
+                }
a3470f
+        }
a3470f
+
a3470f
+        return ret;
a3470f
+}
a3470f
+
a3470f
+
a3470f
+
a3470f
+
a3470f
+
a3470f
 /* TODO:
a3470f
    - use volumename in xattr instead of "dht"
a3470f
    - use NS locks
a3470f
@@ -443,6 +529,7 @@ dht_lookup_selfheal_cbk (call_frame_t *frame, void *cookie,
a3470f
 {
a3470f
         dht_local_t  *local = NULL;
a3470f
         dht_layout_t *layout = NULL;
a3470f
+        dht_conf_t   *conf   = NULL;
a3470f
         int           ret = -1;
a3470f
 
a3470f
         GF_VALIDATE_OR_GOTO ("dht", frame, out);
a3470f
@@ -450,6 +537,7 @@ dht_lookup_selfheal_cbk (call_frame_t *frame, void *cookie,
a3470f
         GF_VALIDATE_OR_GOTO ("dht", frame->local, out);
a3470f
 
a3470f
         local = frame->local;
a3470f
+        conf  = this->private;
a3470f
         ret = op_ret;
a3470f
 
a3470f
         FRAME_SU_UNDO (frame, dht_local_t);
a3470f
@@ -467,6 +555,8 @@ dht_lookup_selfheal_cbk (call_frame_t *frame, void *cookie,
a3470f
 
a3470f
         DHT_STRIP_PHASE1_FLAGS (&local->stbuf);
a3470f
         dht_set_fixed_dir_stat (&local->postparent);
a3470f
+        /* Delete mds xattr at the time of STACK UNWIND */
a3470f
+        GF_REMOVE_INTERNAL_XATTR (conf->mds_xattr_key, local->xattr);
a3470f
 
a3470f
         DHT_STACK_UNWIND (lookup, frame, ret, local->op_errno, local->inode,
a3470f
                           &local->stbuf, local->xattr, &local->postparent);
a3470f
@@ -492,10 +582,12 @@ dht_discover_complete (xlator_t *this, call_frame_t *discover_frame)
a3470f
         int              i               = 0;
a3470f
         loc_t            loc             = {0 };
a3470f
         int8_t           is_read_only    = 0, layout_anomalies = 0;
a3470f
+        char             gfid_local[GF_UUID_BUF_SIZE] = {0};
a3470f
 
a3470f
         local = discover_frame->local;
a3470f
         layout = local->layout;
a3470f
         conf = this->private;
a3470f
+        gf_uuid_unparse(local->gfid, gfid_local);
a3470f
 
a3470f
         LOCK(&discover_frame->lock);
a3470f
         {
a3470f
@@ -507,6 +599,18 @@ dht_discover_complete (xlator_t *this, call_frame_t *discover_frame)
a3470f
         if (!main_frame)
a3470f
                 return 0;
a3470f
 
a3470f
+        /* Code to update all extended attributed from
a3470f
+           subvol to local->xattr on that internal xattr has found
a3470f
+        */
a3470f
+        if (conf->subvolume_cnt == 1)
a3470f
+                local->need_xattr_heal = 0;
a3470f
+        if (local->need_xattr_heal && (local->mds_xattr)) {
a3470f
+                dht_dir_set_heal_xattr (this, local, local->xattr,
a3470f
+                                        local->mds_xattr, NULL, NULL);
a3470f
+                dict_unref (local->mds_xattr);
a3470f
+                local->mds_xattr = NULL;
a3470f
+        }
a3470f
+
a3470f
         ret = dict_get_int8 (local->xattr_req, QUOTA_READ_ONLY_KEY,
a3470f
                              &is_read_only);
a3470f
         if (ret < 0)
a3470f
@@ -575,6 +679,26 @@ dht_discover_complete (xlator_t *this, call_frame_t *discover_frame)
a3470f
                 }
a3470f
         }
a3470f
 
a3470f
+        if (IA_ISDIR (local->stbuf.ia_type)) {
a3470f
+                /* Call function to save hashed subvol on inode ctx if
a3470f
+                   internal mds xattr is not present and all subvols are up
a3470f
+                */
a3470f
+                if (!local->op_ret && !__is_root_gfid (local->stbuf.ia_gfid))
a3470f
+                        (void) dht_mark_mds_subvolume (discover_frame, this);
a3470f
+
a3470f
+                if (local->need_xattr_heal && !heal_path) {
a3470f
+                        local->need_xattr_heal = 0;
a3470f
+                        ret =  dht_dir_xattr_heal (this, local);
a3470f
+                        if (ret)
a3470f
+                                gf_msg (this->name, GF_LOG_ERROR,
a3470f
+                                        ret,
a3470f
+                                        DHT_MSG_DIR_XATTR_HEAL_FAILED,
a3470f
+                                        "xattr heal failed for "
a3470f
+                                        "directory  gfid is %s ",
a3470f
+                                        gfid_local);
a3470f
+                }
a3470f
+        }
a3470f
+
a3470f
         if (source && (heal_path || layout_anomalies)) {
a3470f
                 gf_uuid_copy (loc.gfid, local->gfid);
a3470f
                 if (gf_uuid_is_null (loc.gfid)) {
a3470f
@@ -621,10 +745,14 @@ cleanup:
a3470f
         }
a3470f
 done:
a3470f
         dht_set_fixed_dir_stat (&local->postparent);
a3470f
+        /* Delete mds xattr at the time of STACK UNWIND */
a3470f
+        GF_REMOVE_INTERNAL_XATTR (conf->mds_xattr_key, local->xattr);
a3470f
+
a3470f
         DHT_STACK_UNWIND (lookup, main_frame, local->op_ret, local->op_errno,
a3470f
                           local->inode, &local->stbuf, local->xattr,
a3470f
                           &local->postparent);
a3470f
         return 0;
a3470f
+
a3470f
 out:
a3470f
         DHT_STACK_UNWIND (lookup, main_frame, -1, op_errno, NULL, NULL, NULL,
a3470f
                           NULL);
a3470f
@@ -633,6 +761,170 @@ out:
a3470f
 }
a3470f
 
a3470f
 int
a3470f
+dht_mds_internal_setxattr_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
+                               int op_ret, int op_errno, dict_t *xdata)
a3470f
+{
a3470f
+        dht_local_t  *local                   = NULL;
a3470f
+        xlator_t     *hashed_subvol           = NULL;
a3470f
+        dht_conf_t   *conf                    = NULL;
a3470f
+        int           ret                     = 0;
a3470f
+
a3470f
+        GF_VALIDATE_OR_GOTO (this->name, frame, out);
a3470f
+        GF_VALIDATE_OR_GOTO (this->name, frame->local, out);
a3470f
+
a3470f
+        local = frame->local;
a3470f
+        hashed_subvol  = cookie;
a3470f
+        conf = this->private;
a3470f
+
a3470f
+        if (op_ret) {
a3470f
+                gf_msg_debug (this->name, op_ret,
a3470f
+                              "Failed to set %s on the MDS for path %s. ",
a3470f
+                              conf->mds_xattr_key, local->loc.path);
a3470f
+        } else {
a3470f
+               /* Save mds subvol on inode ctx */
a3470f
+                ret = dht_inode_ctx_mdsvol_set (local->inode, this,
a3470f
+                                                hashed_subvol);
a3470f
+                if (ret) {
a3470f
+                        gf_msg (this->name, GF_LOG_ERROR, 0,
a3470f
+                                DHT_MSG_SET_INODE_CTX_FAILED,
a3470f
+                                "Failed to set mds subvol on inode ctx"
a3470f
+                                " %s for %s", hashed_subvol->name,
a3470f
+                                local->loc.path);
a3470f
+                }
a3470f
+        }
a3470f
+out:
a3470f
+        DHT_STACK_DESTROY (frame);
a3470f
+        return 0;
a3470f
+}
a3470f
+
a3470f
+
a3470f
+
a3470f
+/* Code to save hashed subvol on inode ctx only while no
a3470f
+   mds xattr is availble and all subvols are up for fresh
a3470f
+*/
a3470f
+int
a3470f
+dht_mark_mds_subvolume (call_frame_t *frame, xlator_t *this)
a3470f
+{
a3470f
+        dht_local_t  *local                   = NULL;
a3470f
+        xlator_t     *hashed_subvol           = NULL;
a3470f
+        int           i                       = 0;
a3470f
+        gf_boolean_t  vol_down                = _gf_false;
a3470f
+        dht_conf_t   *conf                    = 0;
a3470f
+        int           ret                     = -1;
a3470f
+        char          gfid_local[GF_UUID_BUF_SIZE] = {0};
a3470f
+        dict_t       *xattrs                      = NULL;
a3470f
+        dht_local_t  *copy_local                  = NULL;
a3470f
+        call_frame_t *xattr_frame                 = NULL;
a3470f
+        int32_t       zero[1]                     = {0};
a3470f
+
a3470f
+
a3470f
+        GF_VALIDATE_OR_GOTO ("dht", frame, out);
a3470f
+        GF_VALIDATE_OR_GOTO ("dht", this, out);
a3470f
+        GF_VALIDATE_OR_GOTO (this->name, frame->local, out);
a3470f
+        GF_VALIDATE_OR_GOTO (this->name, this->private, out);
a3470f
+
a3470f
+        local = frame->local;
a3470f
+        conf = this->private;
a3470f
+        gf_uuid_unparse(local->gfid, gfid_local);
a3470f
+
a3470f
+
a3470f
+        /* Code to update hashed subvol consider as a mds subvol
a3470f
+           and save on inode ctx if all subvols are up and no internal
a3470f
+           xattr has been set yet
a3470f
+        */
a3470f
+        if (!dict_get (local->xattr, conf->mds_xattr_key)) {
a3470f
+                /* It means no internal MDS xattr has been set yet
a3470f
+                */
a3470f
+                /* Check the status of all subvol are up
a3470f
+                */
a3470f
+                for (i = 0; i < conf->subvolume_cnt; i++) {
a3470f
+                        if (!conf->subvolume_status[i]) {
a3470f
+                                vol_down = _gf_true;
a3470f
+                                break;
a3470f
+                        }
a3470f
+                }
a3470f
+                if (vol_down) {
a3470f
+                        ret = 0;
a3470f
+                        gf_msg_debug (this->name, 0,
a3470f
+                                      "subvol %s is down. Unable to "
a3470f
+                                      " save mds subvol on inode for "
a3470f
+                                      " path %s gfid is %s " ,
a3470f
+                                      conf->subvolumes[i]->name, local->loc.path,
a3470f
+                                      gfid_local);
a3470f
+                       goto out;
a3470f
+                }
a3470f
+                /* Calculate hashed subvol based on inode and
a3470f
+                   parent inode
a3470f
+                */
a3470f
+                hashed_subvol = dht_inode_get_hashed_subvol (local->inode,
a3470f
+                                                             this, &local->loc);
a3470f
+                if (!hashed_subvol) {
a3470f
+                        gf_msg (this->name, GF_LOG_DEBUG, 0,
a3470f
+                                DHT_MSG_HASHED_SUBVOL_GET_FAILED,
a3470f
+                                "Failed to get hashed subvol for path %s"
a3470f
+                                " gfid is %s ",
a3470f
+                                local->loc.path, gfid_local);
a3470f
+                } else {
a3470f
+                        xattrs = dict_new ();
a3470f
+                        if (!xattrs) {
a3470f
+                                gf_msg (this->name, GF_LOG_ERROR, ENOMEM,
a3470f
+                                        DHT_MSG_NO_MEMORY, "dict_new failed");
a3470f
+                                ret = -1;
a3470f
+                                goto out;
a3470f
+                        }
a3470f
+                        /* Add internal MDS xattr on disk for hashed subvol
a3470f
+                        */
a3470f
+                        ret = dht_dict_set_array (xattrs, conf->mds_xattr_key, zero, 1);
a3470f
+                        if (ret) {
a3470f
+                                gf_msg (this->name, GF_LOG_WARNING, ENOMEM,
a3470f
+                                        DHT_MSG_DICT_SET_FAILED,
a3470f
+                                        "Failed to set dictionary"
a3470f
+                                        "  value:key = %s for "
a3470f
+                                        "path %s", conf->mds_xattr_key,
a3470f
+                                        local->loc.path);
a3470f
+                                ret = -1;
a3470f
+                                goto out;
a3470f
+                        }
a3470f
+                        xattr_frame = create_frame (this, this->ctx->pool);
a3470f
+                        if (!xattr_frame) {
a3470f
+                                ret = -1;
a3470f
+                                goto out;
a3470f
+                        }
a3470f
+                        copy_local = dht_local_init (xattr_frame, &(local->loc),
a3470f
+                                                     NULL, 0);
a3470f
+                        if (!copy_local) {
a3470f
+                                ret = -1;
a3470f
+                                DHT_STACK_DESTROY (xattr_frame);
a3470f
+                                goto out;
a3470f
+                        }
a3470f
+                        copy_local->stbuf = local->stbuf;
a3470f
+                        if (!copy_local->inode)
a3470f
+                                copy_local->inode = inode_ref (local->inode);
a3470f
+                        gf_uuid_copy (copy_local->loc.gfid, local->gfid);
a3470f
+                        STACK_WIND_COOKIE (xattr_frame, dht_mds_internal_setxattr_cbk,
a3470f
+                                           hashed_subvol, hashed_subvol,
a3470f
+                                           hashed_subvol->fops->setxattr,
a3470f
+                                           &local->loc, xattrs, 0, NULL);
a3470f
+                        ret = 0;
a3470f
+                }
a3470f
+        } else {
a3470f
+                ret = 0;
a3470f
+                gf_msg_debug (this->name, 0,
a3470f
+                              "internal xattr %s is present on subvol"
a3470f
+                              "on path %s gfid is %s " , conf->mds_xattr_key,
a3470f
+                               local->loc.path, gfid_local);
a3470f
+        }
a3470f
+
a3470f
+
a3470f
+out:
a3470f
+        if (xattrs)
a3470f
+                dict_unref (xattrs);
a3470f
+       return ret;
a3470f
+}
a3470f
+
a3470f
+
a3470f
+
a3470f
+int
a3470f
 dht_discover_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
                   int op_ret, int op_errno,
a3470f
                   inode_t *inode, struct iatt *stbuf, dict_t *xattr,
a3470f
@@ -644,11 +936,15 @@ dht_discover_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
         dht_layout_t *layout                  = NULL;
a3470f
         int           ret                     = -1;
a3470f
         int           is_dir                  = 0;
a3470f
+        int32_t       check_mds               = 0;
a3470f
         int           is_linkfile             = 0;
a3470f
         int           attempt_unwind          = 0;
a3470f
         dht_conf_t   *conf                    = 0;
a3470f
-        char         gfid_local[GF_UUID_BUF_SIZE]  = {0};
a3470f
-        char         gfid_node[GF_UUID_BUF_SIZE]  = {0};
a3470f
+        char          gfid_local[GF_UUID_BUF_SIZE] = {0};
a3470f
+        char          gfid_node[GF_UUID_BUF_SIZE]  = {0};
a3470f
+        int32_t       mds_xattr_val[1]             = {0};
a3470f
+        int           errst                        = 0;
a3470f
+
a3470f
 
a3470f
         GF_VALIDATE_OR_GOTO ("dht", frame, out);
a3470f
         GF_VALIDATE_OR_GOTO ("dht", this, out);
a3470f
@@ -743,6 +1039,41 @@ dht_discover_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
                 dht_iatt_merge (this, &local->stbuf, stbuf, prev);
a3470f
                 dht_iatt_merge (this, &local->postparent, postparent,
a3470f
                                 prev);
a3470f
+                if (!dict_get (xattr, conf->mds_xattr_key)) {
a3470f
+                        goto unlock;
a3470f
+                } else {
a3470f
+                        gf_msg_debug (this->name, 0,
a3470f
+                                      "internal xattr %s is present on subvol"
a3470f
+                                      "on path %s gfid is %s " ,
a3470f
+                                      conf->mds_xattr_key,
a3470f
+                                      local->loc.path, gfid_local);
a3470f
+                }
a3470f
+                check_mds = dht_dict_get_array (xattr, conf->mds_xattr_key,
a3470f
+                                                mds_xattr_val, 1, &errst);
a3470f
+                /* save mds subvol on inode ctx */
a3470f
+                ret = dht_inode_ctx_mdsvol_set (local->inode, this,
a3470f
+                                                prev);
a3470f
+                if (ret) {
a3470f
+                        gf_msg (this->name, GF_LOG_ERROR, 0,
a3470f
+                                DHT_MSG_SET_INODE_CTX_FAILED,
a3470f
+                                "Failed to set hashed subvol for %s vol is %s",
a3470f
+                                local->loc.path, prev->name);
a3470f
+                }
a3470f
+
a3470f
+                if ((check_mds < 0) && !errst) {
a3470f
+                        local->mds_xattr = dict_ref (xattr);
a3470f
+                        gf_msg_debug (this->name, 0,
a3470f
+                                      "Value of %s is not zero on mds subvol"
a3470f
+                                      "so xattr needs to be healed on non mds"
a3470f
+                                      " path is %s and vol name is %s "
a3470f
+                                      " gfid is %s" ,
a3470f
+                                      conf->mds_xattr_key,
a3470f
+                                      local->loc.path,
a3470f
+                                      prev->name, gfid_local);
a3470f
+                        local->need_xattr_heal = 1;
a3470f
+                        local->mds_subvol  = prev;
a3470f
+                }
a3470f
+
a3470f
         }
a3470f
 unlock:
a3470f
         UNLOCK (&frame->lock);
a3470f
@@ -841,6 +1172,99 @@ err:
a3470f
         return 0;
a3470f
 }
a3470f
 
a3470f
+/* Get the value of key from dict in the bytewise and save in array after
a3470f
+   convert from network byte order to host byte order
a3470f
+*/
a3470f
+int32_t
a3470f
+dht_dict_get_array (dict_t *dict, char *key, int32_t value[], int32_t size, int *errst)
a3470f
+{
a3470f
+        void    *ptr          = NULL;
a3470f
+        int32_t len           = -1;
a3470f
+        int32_t vindex        = -1;
a3470f
+        int32_t err           = -1;
a3470f
+        int     ret          = 0;
a3470f
+
a3470f
+        if (dict == NULL) {
a3470f
+                (*errst) = -1;
a3470f
+                return -EINVAL;
a3470f
+        }
a3470f
+        err = dict_get_ptr_and_len(dict, key, &ptr, &len;;
a3470f
+        if (err != 0) {
a3470f
+                (*errst) = -1;
a3470f
+                return err;
a3470f
+        }
a3470f
+
a3470f
+        if (len != (size * sizeof (int32_t))) {
a3470f
+                (*errst) = -1;
a3470f
+                return -EINVAL;
a3470f
+        }
a3470f
+
a3470f
+        memset (value, 0, size * sizeof(int32_t));
a3470f
+        for (vindex = 0; vindex < size; vindex++) {
a3470f
+                value[vindex] = ntoh32(*((int32_t *)ptr + vindex));
a3470f
+                if (value[vindex] < 0)
a3470f
+                        ret = -1;
a3470f
+        }
a3470f
+
a3470f
+        return ret;
a3470f
+}
a3470f
+
a3470f
+
a3470f
+/* Code to call syntask to heal custom xattr from hashed subvol
a3470f
+   to non hashed subvol
a3470f
+*/
a3470f
+int
a3470f
+dht_dir_xattr_heal (xlator_t *this, dht_local_t *local)
a3470f
+{
a3470f
+        dht_local_t  *copy_local                  = NULL;
a3470f
+        call_frame_t *copy                        = NULL;
a3470f
+        int          ret                          = -1;
a3470f
+        char         gfid_local[GF_UUID_BUF_SIZE] = {0};
a3470f
+
a3470f
+        if (local->gfid) {
a3470f
+                gf_uuid_unparse(local->gfid, gfid_local);
a3470f
+        } else {
a3470f
+                gf_msg (this->name, GF_LOG_ERROR, 0,
a3470f
+                        DHT_MSG_DIR_XATTR_HEAL_FAILED,
a3470f
+                        "No gfid exists for path %s "
a3470f
+                        "so healing xattr is not possible",
a3470f
+                        local->loc.path);
a3470f
+                goto out;
a3470f
+        }
a3470f
+
a3470f
+        copy = create_frame (this, this->ctx->pool);
a3470f
+        if (copy) {
a3470f
+                copy_local = dht_local_init (copy, &(local->loc), NULL, 0);
a3470f
+                if (!copy_local) {
a3470f
+                        gf_msg (this->name, GF_LOG_ERROR, ENOMEM,
a3470f
+                                DHT_MSG_DIR_XATTR_HEAL_FAILED,
a3470f
+                                "Memory allocation failed "
a3470f
+                                "for path %s gfid %s ",
a3470f
+                                local->loc.path, gfid_local);
a3470f
+                        DHT_STACK_DESTROY (copy);
a3470f
+                } else {
a3470f
+                        copy_local->stbuf = local->stbuf;
a3470f
+                        gf_uuid_copy (copy_local->loc.gfid, local->gfid);
a3470f
+                        copy_local->mds_subvol = local->mds_subvol;
a3470f
+                        FRAME_SU_DO (copy, dht_local_t);
a3470f
+                        ret = synctask_new (this->ctx->env, dht_dir_heal_xattrs,
a3470f
+                                            dht_dir_heal_xattrs_done,
a3470f
+                                            copy, copy);
a3470f
+                        if (ret) {
a3470f
+                                gf_msg (this->name, GF_LOG_ERROR, ENOMEM,
a3470f
+                                        DHT_MSG_DIR_XATTR_HEAL_FAILED,
a3470f
+                                        "Synctask creation failed to heal xattr "
a3470f
+                                        "for path %s gfid %s ",
a3470f
+                                        local->loc.path, gfid_local);
a3470f
+                                DHT_STACK_DESTROY (copy);
a3470f
+                        }
a3470f
+                }
a3470f
+        }
a3470f
+out:
a3470f
+        return ret;
a3470f
+}
a3470f
+
a3470f
+
a3470f
 
a3470f
 int
a3470f
 dht_lookup_dir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
@@ -849,13 +1273,17 @@ dht_lookup_dir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
                     struct iatt *postparent)
a3470f
 {
a3470f
         dht_local_t  *local                   = NULL;
a3470f
+        dht_conf_t   *conf                    = NULL;
a3470f
         int           this_call_cnt           = 0;
a3470f
         xlator_t     *prev                    = NULL;
a3470f
         dht_layout_t *layout                  = NULL;
a3470f
         int           ret                     = -1;
a3470f
         int           is_dir                  = 0;
a3470f
-        char         gfid_local[GF_UUID_BUF_SIZE]  = {0};
a3470f
-        char         gfid_node[GF_UUID_BUF_SIZE]  = {0};
a3470f
+        int32_t       check_mds               = 0;
a3470f
+        int           errst                   = 0;
a3470f
+        char          gfid_local[GF_UUID_BUF_SIZE] = {0};
a3470f
+        char          gfid_node[GF_UUID_BUF_SIZE]  = {0};
a3470f
+        int32_t       mds_xattr_val[1]                 = {0};
a3470f
 
a3470f
         GF_VALIDATE_OR_GOTO ("dht", frame, out);
a3470f
         GF_VALIDATE_OR_GOTO ("dht", this, out);
a3470f
@@ -865,17 +1293,20 @@ dht_lookup_dir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
 
a3470f
         local = frame->local;
a3470f
         prev  = cookie;
a3470f
+        conf  = this->private;
a3470f
 
a3470f
         layout = local->layout;
a3470f
 
a3470f
-        if (!op_ret && gf_uuid_is_null (local->gfid))
a3470f
+        if (!op_ret && gf_uuid_is_null (local->gfid)) {
a3470f
                 memcpy (local->gfid, stbuf->ia_gfid, 16);
a3470f
+        }
a3470f
+        if (local->gfid)
a3470f
+                gf_uuid_unparse(local->gfid, gfid_local);
a3470f
 
a3470f
         /* Check if the gfid is different for file from other node */
a3470f
         if (!op_ret && gf_uuid_compare (local->gfid, stbuf->ia_gfid)) {
a3470f
 
a3470f
                 gf_uuid_unparse(stbuf->ia_gfid, gfid_node);
a3470f
-                gf_uuid_unparse(local->gfid, gfid_local);
a3470f
 
a3470f
                 gf_msg (this->name, GF_LOG_WARNING, 0,
a3470f
                         DHT_MSG_GFID_MISMATCH,
a3470f
@@ -930,6 +1361,41 @@ dht_lookup_dir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
 
a3470f
                 dht_iatt_merge (this, &local->stbuf, stbuf, prev);
a3470f
                 dht_iatt_merge (this, &local->postparent, postparent, prev);
a3470f
+
a3470f
+                if (!dict_get (xattr, conf->mds_xattr_key)) {
a3470f
+                        gf_msg_debug (this->name, 0,
a3470f
+                                      "Internal xattr %s is not present "
a3470f
+                                      " on path %s gfid is %s " ,
a3470f
+                                      conf->mds_xattr_key,
a3470f
+                                      local->loc.path, gfid_local);
a3470f
+                        goto unlock;
a3470f
+                } else {
a3470f
+                        /* Save mds subvol on inode ctx */
a3470f
+                        ret = dht_inode_ctx_mdsvol_set (local->inode, this,
a3470f
+                                                        prev);
a3470f
+                        if (ret) {
a3470f
+                                gf_msg (this->name, GF_LOG_ERROR, 0,
a3470f
+                                        DHT_MSG_SET_INODE_CTX_FAILED,
a3470f
+                                        "Failed to set hashed subvol for %s vol is %s",
a3470f
+                                        local->loc.path, prev->name);
a3470f
+                         }
a3470f
+                }
a3470f
+                check_mds = dht_dict_get_array (xattr, conf->mds_xattr_key,
a3470f
+                                                mds_xattr_val, 1, &errst);
a3470f
+                if ((check_mds < 0) && !errst) {
a3470f
+                        local->mds_xattr = dict_ref (xattr);
a3470f
+                        gf_msg_debug (this->name, 0,
a3470f
+                                      "Value of %s is not zero on hashed subvol "
a3470f
+                                      "so xattr needs to be heal on non hashed"
a3470f
+                                      " path is %s and vol name is %s "
a3470f
+                                      " gfid is %s" ,
a3470f
+                                      conf->mds_xattr_key,
a3470f
+                                      local->loc.path,
a3470f
+                                      prev->name, gfid_local);
a3470f
+                        local->need_xattr_heal = 1;
a3470f
+                        local->mds_subvol  = prev;
a3470f
+                }
a3470f
+
a3470f
         }
a3470f
 unlock:
a3470f
         UNLOCK (&frame->lock);
a3470f
@@ -938,7 +1404,20 @@ unlock:
a3470f
         this_call_cnt = dht_frame_return (frame);
a3470f
 
a3470f
         if (is_last_call (this_call_cnt)) {
a3470f
-                gf_uuid_copy (local->loc.gfid, local->gfid);
a3470f
+                /* No need to call xattr heal code if volume count is 1
a3470f
+                */
a3470f
+                if (conf->subvolume_cnt == 1)
a3470f
+                        local->need_xattr_heal = 0;
a3470f
+
a3470f
+                /* Code to update all extended attributed from hashed subvol
a3470f
+                   to local->xattr
a3470f
+                */
a3470f
+                if (local->need_xattr_heal && (local->mds_xattr)) {
a3470f
+                        dht_dir_set_heal_xattr (this, local, local->xattr,
a3470f
+                                                local->mds_xattr, NULL, NULL);
a3470f
+                        dict_unref (local->mds_xattr);
a3470f
+                        local->mds_xattr = NULL;
a3470f
+                }
a3470f
 
a3470f
                 if (local->need_selfheal) {
a3470f
                         local->need_selfheal = 0;
a3470f
@@ -957,6 +1436,9 @@ unlock:
a3470f
                         }
a3470f
 
a3470f
                         dht_layout_set (this, local->inode, layout);
a3470f
+                        if (!dict_get (local->xattr, conf->mds_xattr_key) ||
a3470f
+                            local->need_xattr_heal)
a3470f
+                                goto selfheal;
a3470f
                 }
a3470f
 
a3470f
                 if (local->inode) {
a3470f
@@ -971,6 +1453,8 @@ unlock:
a3470f
 
a3470f
                 DHT_STRIP_PHASE1_FLAGS (&local->stbuf);
a3470f
                 dht_set_fixed_dir_stat (&local->postparent);
a3470f
+                /* Delete mds xattr at the time of STACK UNWIND */
a3470f
+                GF_REMOVE_INTERNAL_XATTR (conf->mds_xattr_key, local->xattr);
a3470f
                 DHT_STACK_UNWIND (lookup, frame, local->op_ret, local->op_errno,
a3470f
                                   local->inode, &local->stbuf, local->xattr,
a3470f
                                   &local->postparent);
a3470f
@@ -1027,6 +1511,9 @@ dht_revalidate_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
         char gfid[GF_UUID_BUF_SIZE] = {0};
a3470f
         uint32_t      vol_commit_hash = 0;
a3470f
         xlator_t      *subvol = NULL;
a3470f
+        int32_t       check_mds       = 0;
a3470f
+        int           errst           = 0;
a3470f
+        int32_t       mds_xattr_val[1] = {0};
a3470f
 
a3470f
         GF_VALIDATE_OR_GOTO ("dht", frame, err);
a3470f
         GF_VALIDATE_OR_GOTO ("dht", this, err);
a3470f
@@ -1051,6 +1538,9 @@ dht_revalidate_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
 
a3470f
         LOCK (&frame->lock);
a3470f
         {
a3470f
+                if (gf_uuid_is_null (local->gfid)) {
a3470f
+                        memcpy (local->gfid, local->loc.gfid, 16);
a3470f
+                }
a3470f
 
a3470f
                 gf_msg_debug (this->name, op_errno,
a3470f
                               "revalidate lookup of %s "
a3470f
@@ -1136,6 +1626,7 @@ dht_revalidate_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
                                                 local->prebuf.ia_prot = stbuf->ia_prot;
a3470f
                                 }
a3470f
                         }
a3470f
+
a3470f
                         if (local->stbuf.ia_type != IA_INVAL)
a3470f
                         {
a3470f
                                 if ((local->stbuf.ia_gid != stbuf->ia_gid) ||
a3470f
@@ -1146,6 +1637,44 @@ dht_revalidate_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
                                         local->need_selfheal = 1;
a3470f
                                 }
a3470f
                         }
a3470f
+                        if (!dict_get (xattr, conf->mds_xattr_key)) {
a3470f
+                                gf_msg_debug (this->name, 0,
a3470f
+                                              "internal xattr %s is not present"
a3470f
+                                              " on path %s gfid is %s " ,
a3470f
+                                              conf->mds_xattr_key,
a3470f
+                                              local->loc.path, gfid);
a3470f
+                        } else {
a3470f
+                                check_mds = dht_dict_get_array (xattr, conf->mds_xattr_key,
a3470f
+                                                                mds_xattr_val, 1, &errst);
a3470f
+                                if (local->mds_subvol == prev) {
a3470f
+                                        local->mds_stbuf.ia_gid = stbuf->ia_gid;
a3470f
+                                        local->mds_stbuf.ia_uid = stbuf->ia_uid;
a3470f
+                                        local->mds_stbuf.ia_prot = stbuf->ia_prot;
a3470f
+                                }
a3470f
+                                /* save mds subvol on inode ctx */
a3470f
+                                ret = dht_inode_ctx_mdsvol_set (local->inode, this,
a3470f
+                                                                prev);
a3470f
+                                if (ret) {
a3470f
+                                        gf_msg (this->name, GF_LOG_ERROR, 0,
a3470f
+                                                DHT_MSG_SET_INODE_CTX_FAILED,
a3470f
+                                                "Failed to set MDS subvol for %s vol is %s",
a3470f
+                                                local->loc.path, prev->name);
a3470f
+                                }
a3470f
+                                if ((check_mds < 0) && !errst) {
a3470f
+                                        local->mds_xattr = dict_ref (xattr);
a3470f
+                                        gf_msg_debug (this->name, 0,
a3470f
+                                                      "Value of %s is not zero on "
a3470f
+                                                      "hashed subvol so xattr needs to"
a3470f
+                                                      " be healed on non hashed"
a3470f
+                                                      " path is %s and vol name is %s "
a3470f
+                                                      " gfid is %s" ,
a3470f
+                                                      conf->mds_xattr_key,
a3470f
+                                                      local->loc.path,
a3470f
+                                                      prev->name, gfid);
a3470f
+                                        local->need_xattr_heal = 1;
a3470f
+                                        local->mds_subvol  = prev;
a3470f
+                                }
a3470f
+                        }
a3470f
                         ret = dht_layout_dir_mismatch (this, layout,
a3470f
                                                        prev, &local->loc,
a3470f
                                                        xattr);
a3470f
@@ -1215,13 +1744,52 @@ out:
a3470f
                     && (conf && conf->unhashed_sticky_bit)) {
a3470f
                         local->stbuf.ia_prot.sticky = 1;
a3470f
                 }
a3470f
+                /* No need to call heal code if volume count is 1
a3470f
+                */
a3470f
+                if (conf->subvolume_cnt == 1)
a3470f
+                        local->need_xattr_heal = 0;
a3470f
+
a3470f
+                /* Code to update all extended attributed from hashed subvol
a3470f
+                   to local->xattr
a3470f
+                */
a3470f
+                if (local->need_xattr_heal && (local->mds_xattr)) {
a3470f
+                        dht_dir_set_heal_xattr (this, local, local->xattr,
a3470f
+                                                local->mds_xattr, NULL, NULL);
a3470f
+                        dict_unref (local->mds_xattr);
a3470f
+                        local->mds_xattr = NULL;
a3470f
+                }
a3470f
+                /* Call function to save hashed subvol on inode ctx if
a3470f
+                   internal mds xattr is not present and all subvols are up
a3470f
+                */
a3470f
+                if (inode && !__is_root_gfid (inode->gfid) &&
a3470f
+                    (!local->op_ret) && (IA_ISDIR (local->stbuf.ia_type)))
a3470f
+                        (void) dht_mark_mds_subvolume (frame, this);
a3470f
+
a3470f
+                if (local->need_xattr_heal) {
a3470f
+                        local->need_xattr_heal = 0;
a3470f
+                        ret =  dht_dir_xattr_heal (this, local);
a3470f
+                        if (ret)
a3470f
+                                gf_msg (this->name, GF_LOG_ERROR,
a3470f
+                                        ret, DHT_MSG_DIR_XATTR_HEAL_FAILED,
a3470f
+                                        "xattr heal failed for directory %s "
a3470f
+                                        " gfid %s ", local->loc.path,
a3470f
+                                        gfid);
a3470f
+                }
a3470f
                 if (local->need_selfheal) {
a3470f
                         local->need_selfheal = 0;
a3470f
-                        gf_uuid_copy (local->gfid, local->stbuf.ia_gfid);
a3470f
-                        local->stbuf.ia_gid = local->prebuf.ia_gid;
a3470f
-                        local->stbuf.ia_uid = local->prebuf.ia_uid;
a3470f
-                        if (__is_root_gfid(local->stbuf.ia_gfid))
a3470f
+                        if (!__is_root_gfid (inode->gfid)) {
a3470f
+                                gf_uuid_copy (local->gfid, local->mds_stbuf.ia_gfid);
a3470f
+                                if (local->mds_stbuf.ia_gid || local->mds_stbuf.ia_uid) {
a3470f
+                                        local->stbuf.ia_gid = local->mds_stbuf.ia_gid;
a3470f
+                                        local->stbuf.ia_uid = local->mds_stbuf.ia_uid;
a3470f
+                                }
a3470f
+                        } else {
a3470f
+                                gf_uuid_copy (local->gfid, local->stbuf.ia_gfid);
a3470f
+                                local->stbuf.ia_gid = local->prebuf.ia_gid;
a3470f
+                                local->stbuf.ia_uid = local->prebuf.ia_uid;
a3470f
                                 local->stbuf.ia_prot = local->prebuf.ia_prot;
a3470f
+                        }
a3470f
+
a3470f
                         copy = create_frame (this, this->ctx->pool);
a3470f
                         if (copy) {
a3470f
                                 copy_local = dht_local_init (copy, &local->loc,
a3470f
@@ -1229,6 +1797,8 @@ out:
a3470f
                                 if (!copy_local)
a3470f
                                         goto cont;
a3470f
                                 copy_local->stbuf = local->stbuf;
a3470f
+                                copy_local->mds_stbuf = local->mds_stbuf;
a3470f
+                                copy_local->mds_subvol = local->mds_subvol;
a3470f
                                 copy->local = copy_local;
a3470f
                                 FRAME_SU_DO (copy, dht_local_t);
a3470f
                                 ret = synctask_new (this->ctx->env,
a3470f
@@ -1283,6 +1853,8 @@ cont:
a3470f
                         local->op_ret = -1;
a3470f
                         local->op_errno = ESTALE;
a3470f
                 }
a3470f
+                /* Delete mds xattr at the time of STACK UNWIND */
a3470f
+                GF_REMOVE_INTERNAL_XATTR (conf->mds_xattr_key, local->xattr);
a3470f
 
a3470f
                 DHT_STACK_UNWIND (lookup, frame, local->op_ret, local->op_errno,
a3470f
                                   local->inode, &local->stbuf, local->xattr,
a3470f
@@ -2303,6 +2875,62 @@ out:
a3470f
 
a3470f
 }
a3470f
 
a3470f
+/* Code to get hashed subvol based on inode and loc
a3470f
+   First it check if loc->parent and loc->path exist then it get
a3470f
+   hashed subvol based on loc.
a3470f
+*/
a3470f
+
a3470f
+xlator_t *
a3470f
+dht_inode_get_hashed_subvol (inode_t *inode, xlator_t *this, loc_t *loc)
a3470f
+{
a3470f
+        char                    *path           = NULL;
a3470f
+        loc_t                    populate_loc   = {0, };
a3470f
+        char                    *name           = NULL;
a3470f
+        xlator_t                *hash_subvol    = NULL;
a3470f
+
a3470f
+        if (!inode)
a3470f
+                return hash_subvol;
a3470f
+
a3470f
+        if (loc && loc->parent && loc->path) {
a3470f
+                if (!loc->name) {
a3470f
+                        name = strrchr (loc->path, '/');
a3470f
+                        if (name) {
a3470f
+                                loc->name = name + 1;
a3470f
+                        } else {
a3470f
+                                goto out;
a3470f
+                        }
a3470f
+                }
a3470f
+                hash_subvol = dht_subvol_get_hashed (this, loc);
a3470f
+                goto out;
a3470f
+        }
a3470f
+
a3470f
+        if (!gf_uuid_is_null (inode->gfid)) {
a3470f
+                populate_loc.inode = inode_ref (inode);
a3470f
+                populate_loc.parent = inode_parent (populate_loc.inode,
a3470f
+                                                    NULL, NULL);
a3470f
+                inode_path (populate_loc.inode, NULL, &path);
a3470f
+
a3470f
+                if (!path)
a3470f
+                        goto out;
a3470f
+
a3470f
+                populate_loc.path = path;
a3470f
+                if (!populate_loc.name && populate_loc.path) {
a3470f
+                        name = strrchr (populate_loc.path, '/');
a3470f
+                        if (name) {
a3470f
+                                populate_loc.name = name + 1;
a3470f
+
a3470f
+                        } else {
a3470f
+                                goto out;
a3470f
+                        }
a3470f
+                }
a3470f
+                hash_subvol = dht_subvol_get_hashed (this, &populate_loc);
a3470f
+        }
a3470f
+out:
a3470f
+        if (populate_loc.inode)
a3470f
+                loc_wipe (&populate_loc);
a3470f
+        return hash_subvol;
a3470f
+}
a3470f
+
a3470f
 
a3470f
 int
a3470f
 dht_lookup_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
@@ -2537,6 +3165,7 @@ dht_lookup (call_frame_t *frame, xlator_t *this,
a3470f
 {
a3470f
         xlator_t     *subvol = NULL;
a3470f
         xlator_t     *hashed_subvol = NULL;
a3470f
+        xlator_t     *mds_subvol = NULL;
a3470f
         dht_local_t  *local  = NULL;
a3470f
         dht_conf_t   *conf = NULL;
a3470f
         int           ret    = -1;
a3470f
@@ -2587,6 +3216,15 @@ dht_lookup (call_frame_t *frame, xlator_t *this,
a3470f
                 local->xattr_req = dict_new ();
a3470f
         }
a3470f
 
a3470f
+        ret = dict_set_uint32 (local->xattr_req, conf->mds_xattr_key, 4);
a3470f
+
a3470f
+        if (ret) {
a3470f
+                gf_msg (this->name, GF_LOG_WARNING, ENOMEM,
a3470f
+                        DHT_MSG_DICT_SET_FAILED,
a3470f
+                        "Failed to set dictionary value:key = %s for "
a3470f
+                        "path %s", conf->mds_xattr_key, loc->path);
a3470f
+        }
a3470f
+
a3470f
         /* Nameless lookup */
a3470f
 
a3470f
         if (gf_uuid_is_null (loc->pargfid) && !gf_uuid_is_null (loc->gfid) &&
a3470f
@@ -2663,6 +3301,14 @@ dht_lookup (call_frame_t *frame, xlator_t *this,
a3470f
                         goto err;
a3470f
                 }
a3470f
                 if (IA_ISDIR (local->inode->ia_type)) {
a3470f
+                        ret = dht_inode_ctx_mdsvol_get (local->inode, this,
a3470f
+                                                        &mds_subvol);
a3470f
+                        if (ret || !mds_subvol) {
a3470f
+                                gf_msg_debug (this->name, 0,
a3470f
+                                              "Failed to get mds subvol for path %s",
a3470f
+                                              local->loc.path);
a3470f
+                        }
a3470f
+                        local->mds_subvol = mds_subvol;
a3470f
                         local->call_cnt = call_cnt = conf->subvolume_cnt;
a3470f
                         for (i = 0; i < call_cnt; i++) {
a3470f
                                 STACK_WIND_COOKIE (frame, dht_revalidate_cbk,
a3470f
@@ -2851,76 +3497,300 @@ dht_unlink_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
 
a3470f
                 local->op_ret = 0;
a3470f
 
a3470f
-                local->postparent = *postparent;
a3470f
-                local->preparent = *preparent;
a3470f
+                local->postparent = *postparent;
a3470f
+                local->preparent = *preparent;
a3470f
+
a3470f
+                if (local->loc.parent) {
a3470f
+                        dht_inode_ctx_time_update (local->loc.parent, this,
a3470f
+                                                   &local->preparent, 0);
a3470f
+                        dht_inode_ctx_time_update (local->loc.parent, this,
a3470f
+                                                   &local->postparent, 1);
a3470f
+                }
a3470f
+        }
a3470f
+unlock:
a3470f
+        UNLOCK (&frame->lock);
a3470f
+
a3470f
+        if (!local->op_ret) {
a3470f
+                hashed_subvol = dht_subvol_get_hashed (this, &local->loc);
a3470f
+                if (hashed_subvol && hashed_subvol != local->cached_subvol) {
a3470f
+                        /*
a3470f
+                         * If hashed and cached are different, then we need
a3470f
+                         * to unlink linkfile from hashed subvol if data
a3470f
+                         * file is deleted successfully
a3470f
+                         */
a3470f
+                         STACK_WIND_COOKIE (frame, dht_unlink_linkfile_cbk,
a3470f
+                                            hashed_subvol, hashed_subvol,
a3470f
+                                            hashed_subvol->fops->unlink,
a3470f
+                                            &local->loc, local->flags, xdata);
a3470f
+                         return 0;
a3470f
+                }
a3470f
+        }
a3470f
+
a3470f
+        dht_set_fixed_dir_stat (&local->preparent);
a3470f
+        dht_set_fixed_dir_stat (&local->postparent);
a3470f
+        DHT_STACK_UNWIND (unlink, frame, local->op_ret, local->op_errno,
a3470f
+                          &local->preparent, &local->postparent, xdata);
a3470f
+
a3470f
+        return 0;
a3470f
+}
a3470f
+
a3470f
+static int
a3470f
+dht_common_setxattr_cbk (call_frame_t *frame, void *cookie,
a3470f
+                         xlator_t *this, int32_t op_ret, int32_t op_errno,
a3470f
+                         dict_t *xdata)
a3470f
+{
a3470f
+         DHT_STACK_UNWIND (setxattr, frame, op_ret, op_errno, xdata);
a3470f
+         return 0;
a3470f
+}
a3470f
+
a3470f
+
a3470f
+
a3470f
+int
a3470f
+dht_err_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
+             int op_ret, int op_errno, dict_t *xdata)
a3470f
+{
a3470f
+        dht_local_t  *local = NULL;
a3470f
+        int           this_call_cnt = 0;
a3470f
+        xlator_t     *prev = NULL;
a3470f
+
a3470f
+        local = frame->local;
a3470f
+        prev = cookie;
a3470f
+
a3470f
+        LOCK (&frame->lock);
a3470f
+        {
a3470f
+                if (op_ret == -1) {
a3470f
+                        local->op_errno = op_errno;
a3470f
+                        gf_msg_debug (this->name, op_errno,
a3470f
+                                      "subvolume %s returned -1",
a3470f
+                                      prev->name);
a3470f
+                        goto unlock;
a3470f
+                }
a3470f
+
a3470f
+                local->op_ret = 0;
a3470f
+        }
a3470f
+unlock:
a3470f
+        UNLOCK (&frame->lock);
a3470f
+
a3470f
+        this_call_cnt = dht_frame_return (frame);
a3470f
+        if (is_last_call (this_call_cnt)) {
a3470f
+                DHT_STACK_UNWIND (setxattr, frame, local->op_ret,
a3470f
+                                  local->op_errno, NULL);
a3470f
+        }
a3470f
+
a3470f
+        return 0;
a3470f
+}
a3470f
+
a3470f
+/* Set the value[] of key into dict after convert from
a3470f
+   host byte order to network byte order
a3470f
+*/
a3470f
+int32_t dht_dict_set_array (dict_t *dict, char *key, int32_t value[],
a3470f
+                            int32_t size)
a3470f
+{
a3470f
+        int         ret = -1;
a3470f
+        int32_t   *ptr = NULL;
a3470f
+        int32_t     vindex;
a3470f
+
a3470f
+        if (value == NULL) {
a3470f
+                return -EINVAL;
a3470f
+        }
a3470f
+
a3470f
+        ptr = GF_MALLOC(sizeof(int32_t) * size, gf_common_mt_char);
a3470f
+        if (ptr == NULL) {
a3470f
+                return -ENOMEM;
a3470f
+        }
a3470f
+        for (vindex = 0; vindex < size; vindex++) {
a3470f
+                ptr[vindex] = hton32(value[vindex]);
a3470f
+        }
a3470f
+        ret = dict_set_bin(dict, key, ptr, sizeof(int32_t) * size);
a3470f
+        if (ret)
a3470f
+                GF_FREE (ptr);
a3470f
+        return ret;
a3470f
+}
a3470f
+
a3470f
+/* Code to wind a xattrop call to add 1 on current mds internal xattr
a3470f
+   value
a3470f
+*/
a3470f
+int
a3470f
+dht_setxattr_non_mds_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
+                          int op_ret, int op_errno, dict_t *xdata)
a3470f
+{
a3470f
+        dht_local_t  *local = NULL;
a3470f
+        int           this_call_cnt = 0;
a3470f
+        int           ret           = 0;
a3470f
+        dict_t        *xattrop      = NULL;
a3470f
+        int32_t       addone[1]     = {1};
a3470f
+        call_frame_t  *prev         = NULL;
a3470f
+        dht_conf_t    *conf         = NULL;
a3470f
+
a3470f
+        local = frame->local;
a3470f
+        prev = cookie;
a3470f
+        conf = this->private;
a3470f
+
a3470f
+        LOCK (&frame->lock);
a3470f
+        {
a3470f
+             if (op_ret && !local->op_ret) {
a3470f
+                        local->op_ret = op_ret;
a3470f
+                        local->op_errno = op_errno;
a3470f
+                         gf_msg_debug (this->name, op_errno,
a3470f
+                                       "subvolume %s returned -1",
a3470f
+                                       prev->this->name);
a3470f
+             }
a3470f
+        }
a3470f
+        UNLOCK (&frame->lock);
a3470f
+        this_call_cnt = dht_frame_return (frame);
a3470f
+
a3470f
+        if (is_last_call (this_call_cnt)) {
a3470f
+                if (!local->op_ret) {
a3470f
+                        xattrop = dict_new ();
a3470f
+                        if (!xattrop) {
a3470f
+                                gf_msg (this->name, GF_LOG_ERROR,
a3470f
+                                        DHT_MSG_NO_MEMORY, 0,
a3470f
+                                        "dictionary creation failed");
a3470f
+                                ret = -1;
a3470f
+                                goto out;
a3470f
+                        }
a3470f
+                        ret = dht_dict_set_array (xattrop,
a3470f
+                                                  conf->mds_xattr_key,
a3470f
+                                                  addone, 1);
a3470f
+                        if (ret != 0) {
a3470f
+                                gf_msg (this->name, GF_LOG_ERROR, 0,
a3470f
+                                        DHT_MSG_DICT_SET_FAILED,
a3470f
+                                        "dictionary set array failed ");
a3470f
+                                ret = -1;
a3470f
+                                goto out;
a3470f
+                        }
a3470f
+                        if (local->fop == GF_FOP_SETXATTR) {
a3470f
+                                STACK_WIND (frame, dht_common_xattrop_cbk,
a3470f
+                                            local->mds_subvol,
a3470f
+                                            local->mds_subvol->fops->xattrop,
a3470f
+                                            &local->loc, GF_XATTROP_ADD_ARRAY,
a3470f
+                                            xattrop, NULL);
a3470f
+                        } else {
a3470f
+                                STACK_WIND (frame, dht_common_xattrop_cbk,
a3470f
+                                            local->mds_subvol,
a3470f
+                                            local->mds_subvol->fops->fxattrop,
a3470f
+                                            local->fd, GF_XATTROP_ADD_ARRAY,
a3470f
+                                            xattrop, NULL);
a3470f
+                        }
a3470f
+                } else  {
a3470f
+                        if (local->fop == GF_FOP_SETXATTR)
a3470f
+                                DHT_STACK_UNWIND (setxattr, frame, 0, 0, local->xdata);
a3470f
+                        else
a3470f
+                                DHT_STACK_UNWIND (fsetxattr, frame, 0, 0, local->xdata);
a3470f
+                }
a3470f
+        }
a3470f
+out:
a3470f
+        if (xattrop)
a3470f
+                dict_unref (xattrop);
a3470f
+        if (ret) {
a3470f
+                if (local->fop == GF_FOP_SETXATTR)
a3470f
+                        DHT_STACK_UNWIND (setxattr, frame, 0, 0, local->xdata);
a3470f
+                else
a3470f
+                        DHT_STACK_UNWIND (fsetxattr, frame, 0, 0, local->xdata);
a3470f
+        }
a3470f
+        return 0;
a3470f
+}
a3470f
+
a3470f
+
a3470f
+int
a3470f
+dht_setxattr_mds_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
+                      int op_ret, int op_errno, dict_t *xdata)
a3470f
+{
a3470f
+        dht_local_t  *local = NULL;
a3470f
+        dht_conf_t   *conf  = NULL;
a3470f
+        call_frame_t *prev = NULL;
a3470f
+        xlator_t     *mds_subvol = NULL;
a3470f
+        int i = 0;
a3470f
+
a3470f
+        local = frame->local;
a3470f
+        prev = cookie;
a3470f
+        conf = this->private;
a3470f
+        mds_subvol = local->mds_subvol;
a3470f
+
a3470f
+        if (op_ret == -1) {
a3470f
+                local->op_ret  = op_ret;
a3470f
+                local->op_errno = op_errno;
a3470f
+                gf_msg_debug (this->name, op_errno,
a3470f
+                              "subvolume %s returned -1",
a3470f
+                              prev->this->name);
a3470f
+                goto out;
a3470f
+        }
a3470f
+
a3470f
+        local->op_ret = 0;
a3470f
+        local->call_cnt = conf->subvolume_cnt - 1;
a3470f
+        local->xdata    = dict_ref (xdata);
a3470f
 
a3470f
-                if (local->loc.parent) {
a3470f
-                        dht_inode_ctx_time_update (local->loc.parent, this,
a3470f
-                                                   &local->preparent, 0);
a3470f
-                        dht_inode_ctx_time_update (local->loc.parent, this,
a3470f
-                                                   &local->postparent, 1);
a3470f
+        for (i = 0; i < conf->subvolume_cnt; i++) {
a3470f
+                if (mds_subvol && (mds_subvol == conf->subvolumes[i]))
a3470f
+                        continue;
a3470f
+                if (local->fop == GF_FOP_SETXATTR) {
a3470f
+                        STACK_WIND (frame, dht_setxattr_non_mds_cbk,
a3470f
+                                    conf->subvolumes[i],
a3470f
+                                    conf->subvolumes[i]->fops->setxattr,
a3470f
+                                    &local->loc, local->xattr,
a3470f
+                                    local->flags, local->xattr_req);
a3470f
+                } else {
a3470f
+                        STACK_WIND (frame, dht_setxattr_non_mds_cbk,
a3470f
+                                    conf->subvolumes[i],
a3470f
+                                    conf->subvolumes[i]->fops->fsetxattr,
a3470f
+                                    local->fd, local->xattr,
a3470f
+                                    local->flags, local->xattr_req);
a3470f
                 }
a3470f
         }
a3470f
-unlock:
a3470f
-        UNLOCK (&frame->lock);
a3470f
 
a3470f
-        if (!local->op_ret) {
a3470f
-                hashed_subvol = dht_subvol_get_hashed (this, &local->loc);
a3470f
-                if (hashed_subvol &&
a3470f
-                hashed_subvol != local->cached_subvol) {
a3470f
-                        /*
a3470f
-                         * If hashed and cached are different, then we need
a3470f
-                         * to unlink linkfile from hashed subvol if data
a3470f
-                         * file is deleted successfully
a3470f
-                         */
a3470f
-                        STACK_WIND_COOKIE (frame, dht_unlink_linkfile_cbk,
a3470f
-                                           hashed_subvol, hashed_subvol,
a3470f
-                                           hashed_subvol->fops->unlink, &local->loc,
a3470f
-                                           local->flags, xdata);
a3470f
-                        return 0;
a3470f
-                }
a3470f
+        return 0;
a3470f
+out:
a3470f
+        if (local->fop == GF_FOP_SETXATTR) {
a3470f
+                DHT_STACK_UNWIND (setxattr, frame, local->op_ret,
a3470f
+                                  local->op_errno, xdata);
a3470f
+        } else {
a3470f
+                DHT_STACK_UNWIND (fsetxattr, frame, local->op_ret,
a3470f
+                                  local->op_errno, xdata);
a3470f
         }
a3470f
 
a3470f
-        dht_set_fixed_dir_stat (&local->preparent);
a3470f
-        dht_set_fixed_dir_stat (&local->postparent);
a3470f
-        DHT_STACK_UNWIND (unlink, frame, local->op_ret, local->op_errno,
a3470f
-                          &local->preparent, &local->postparent, xdata);
a3470f
-
a3470f
         return 0;
a3470f
 }
a3470f
 
a3470f
 int
a3470f
-dht_err_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
-             int op_ret, int op_errno, dict_t *xdata)
a3470f
+dht_xattrop_mds_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
+                     int op_ret, int op_errno, dict_t *dict, dict_t *xdata)
a3470f
 {
a3470f
         dht_local_t  *local = NULL;
a3470f
-        int           this_call_cnt = 0;
a3470f
-        xlator_t     *prev = NULL;
a3470f
+        call_frame_t *prev = NULL;
a3470f
 
a3470f
         local = frame->local;
a3470f
         prev = cookie;
a3470f
 
a3470f
-        LOCK (&frame->lock);
a3470f
-        {
a3470f
-                if (op_ret == -1) {
a3470f
-                        local->op_errno = op_errno;
a3470f
-                        gf_msg_debug (this->name, op_errno,
a3470f
-                                      "subvolume %s returned -1",
a3470f
-                                      prev->name);
a3470f
-                        goto unlock;
a3470f
-                }
a3470f
-
a3470f
-                local->op_ret = 0;
a3470f
+        if (op_ret == -1) {
a3470f
+                local->op_errno = op_errno;
a3470f
+                local->op_ret   = op_ret;
a3470f
+                gf_msg_debug (this->name, op_errno,
a3470f
+                              "subvolume %s returned -1",
a3470f
+                              prev->this->name);
a3470f
+                goto out;
a3470f
         }
a3470f
-unlock:
a3470f
-        UNLOCK (&frame->lock);
a3470f
 
a3470f
-        this_call_cnt = dht_frame_return (frame);
a3470f
-        if (is_last_call (this_call_cnt)) {
a3470f
-                DHT_STACK_UNWIND (setxattr, frame, local->op_ret,
a3470f
-                                  local->op_errno, NULL);
a3470f
+        if (local->fop == GF_FOP_SETXATTR) {
a3470f
+                STACK_WIND (frame, dht_setxattr_mds_cbk,
a3470f
+                            local->mds_subvol,
a3470f
+                            local->mds_subvol->fops->setxattr,
a3470f
+                            &local->loc, local->xattr,
a3470f
+                            local->flags, local->xattr_req);
a3470f
+        } else {
a3470f
+                STACK_WIND (frame, dht_setxattr_mds_cbk,
a3470f
+                            local->mds_subvol,
a3470f
+                            local->mds_subvol->fops->fsetxattr,
a3470f
+                            local->fd, local->xattr,
a3470f
+                            local->flags, local->xattr_req);
a3470f
         }
a3470f
-
a3470f
+        return 0;
a3470f
+out:
a3470f
+        if (local->fop == GF_FOP_SETXATTR)
a3470f
+                DHT_STACK_UNWIND (setxattr, frame, local->op_ret,
a3470f
+                                  local->op_errno, xdata);
a3470f
+        else
a3470f
+                DHT_STACK_UNWIND (fsetxattr, frame, local->op_ret,
a3470f
+                                  local->op_errno, xdata);
a3470f
         return 0;
a3470f
 }
a3470f
 
a3470f
@@ -3371,6 +4241,41 @@ dht_linkinfo_getxattr_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
         return 0;
a3470f
 }
a3470f
 
a3470f
+
a3470f
+int
a3470f
+dht_mds_getxattr_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
+                      int op_ret, int op_errno, dict_t *xattr, dict_t *xdata)
a3470f
+{
a3470f
+        dht_local_t     *local = NULL;
a3470f
+        dht_conf_t      *conf = NULL;
a3470f
+
a3470f
+        VALIDATE_OR_GOTO (frame, out);
a3470f
+        VALIDATE_OR_GOTO (frame->local, out);
a3470f
+        VALIDATE_OR_GOTO (this->private, out);
a3470f
+
a3470f
+        conf = this->private;
a3470f
+        local = frame->local;
a3470f
+
a3470f
+        if (!xattr || (op_ret == -1)) {
a3470f
+                local->op_ret = op_ret;
a3470f
+                goto out;
a3470f
+        }
a3470f
+        if (dict_get (xattr, conf->xattr_name)) {
a3470f
+                dict_del (xattr, conf->xattr_name);
a3470f
+        }
a3470f
+        local->op_ret = 0;
a3470f
+
a3470f
+        if (!local->xattr) {
a3470f
+                local->xattr = dict_copy_with_ref (xattr, NULL);
a3470f
+        }
a3470f
+
a3470f
+out:
a3470f
+        DHT_STACK_UNWIND (getxattr, frame, local->op_ret, op_errno,
a3470f
+                          local->xattr, xdata);
a3470f
+        return 0;
a3470f
+}
a3470f
+
a3470f
+
a3470f
 int
a3470f
 dht_getxattr_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
                   int op_ret, int op_errno, dict_t *xattr, dict_t *xdata)
a3470f
@@ -3600,6 +4505,7 @@ dht_getxattr (call_frame_t *frame, xlator_t *this,
a3470f
 
a3470f
         xlator_t     *subvol        = NULL;
a3470f
         xlator_t     *hashed_subvol = NULL;
a3470f
+        xlator_t     *mds_subvol = NULL;
a3470f
         xlator_t     *cached_subvol = NULL;
a3470f
         dht_conf_t   *conf          = NULL;
a3470f
         dht_local_t  *local         = NULL;
a3470f
@@ -3642,6 +4548,12 @@ dht_getxattr (call_frame_t *frame, xlator_t *this,
a3470f
         }
a3470f
 
a3470f
         if (key &&
a3470f
+            (strncmp (key, conf->mds_xattr_key, strlen(key)) == 0)) {
a3470f
+                op_errno = ENOTSUP;
a3470f
+                goto err;
a3470f
+        }
a3470f
+
a3470f
+        if (key &&
a3470f
             (strncmp (key, GF_XATTR_GET_REAL_FILENAME_KEY,
a3470f
                       strlen (GF_XATTR_GET_REAL_FILENAME_KEY)) == 0)
a3470f
             && DHT_IS_DIR(layout)) {
a3470f
@@ -3771,26 +4683,53 @@ dht_getxattr (call_frame_t *frame, xlator_t *this,
a3470f
                 return 0;
a3470f
         }
a3470f
 
a3470f
-        if (key && (!strcmp (QUOTA_LIMIT_KEY, key) ||
a3470f
-                    !strcmp (QUOTA_LIMIT_OBJECTS_KEY, key))) {
a3470f
-                /* quota hardlimit and aggregated size of a directory is stored
a3470f
-                 * in inode contexts of each brick. Hence its good enough that
a3470f
-                 * we send getxattr for this key to any brick.
a3470f
-                 */
a3470f
-                local->call_cnt = 1;
a3470f
-                subvol = dht_first_up_subvol (this);
a3470f
-                STACK_WIND (frame, dht_getxattr_cbk, subvol,
a3470f
-                            subvol->fops->getxattr, loc, key, xdata);
a3470f
-                return 0;
a3470f
-        }
a3470f
-
a3470f
         if (cluster_handle_marker_getxattr (frame, loc, key, conf->vol_uuid,
a3470f
                                             dht_getxattr_unwind,
a3470f
                                             dht_marker_populate_args) == 0)
a3470f
                 return 0;
a3470f
 
a3470f
         if (DHT_IS_DIR(layout)) {
a3470f
-                cnt = local->call_cnt = layout->cnt;
a3470f
+                local->call_cnt = conf->subvolume_cnt;
a3470f
+                cnt = conf->subvolume_cnt;
a3470f
+                ret = dht_inode_ctx_mdsvol_get (loc->inode, this, &mds_subvol);
a3470f
+                if (!mds_subvol) {
a3470f
+                        gf_msg (this->name, GF_LOG_INFO, 0,
a3470f
+                                DHT_MSG_HASHED_SUBVOL_GET_FAILED,
a3470f
+                                "Cannot determine MDS, fetching xattr %s randomly"
a3470f
+                                " from a subvol for path %s ", key, loc->path);
a3470f
+                } else {
a3470f
+                        /* TODO need to handle it, As of now we are
a3470f
+                           choosing availability instead of chossing
a3470f
+                           consistencty, in case of mds_subvol is
a3470f
+                           down winding a getxattr call on other subvol
a3470f
+                           and return xattr
a3470f
+                        */
a3470f
+                        local->mds_subvol = mds_subvol;
a3470f
+                        for (i = 0; i < cnt; i++) {
a3470f
+                                if (conf->subvolumes[i] == mds_subvol) {
a3470f
+                                        if (!conf->subvolume_status[i]) {
a3470f
+                                                gf_msg (this->name,
a3470f
+                                                        GF_LOG_INFO, 0,
a3470f
+                                                        DHT_MSG_HASHED_SUBVOL_DOWN,
a3470f
+                                                        "MDS %s is down for path"
a3470f
+                                                        " path %s so fetching xattr "
a3470f
+                                                        "%s randomly from a subvol ",
a3470f
+                                                        local->mds_subvol->name,
a3470f
+                                                        loc->path, key);
a3470f
+                                                ret = 1;
a3470f
+                                        }
a3470f
+                                }
a3470f
+                        }
a3470f
+                }
a3470f
+
a3470f
+                if (!ret && key && local->mds_subvol && dht_match_xattr (key)) {
a3470f
+                        STACK_WIND (frame, dht_mds_getxattr_cbk,
a3470f
+                                    local->mds_subvol,
a3470f
+                                    local->mds_subvol->fops->getxattr,
a3470f
+                                    loc, key, xdata);
a3470f
+
a3470f
+                        return 0;
a3470f
+                }
a3470f
         } else {
a3470f
                 cnt = local->call_cnt  = 1;
a3470f
         }
a3470f
@@ -3821,6 +4760,10 @@ dht_fgetxattr (call_frame_t *frame, xlator_t *this,
a3470f
         int           op_errno      = -1;
a3470f
         int           i             = 0;
a3470f
         int           cnt           = 0;
a3470f
+        xlator_t      *mds_subvol = NULL;
a3470f
+        int           ret           = -1;
a3470f
+        dht_conf_t    *conf         = NULL;
a3470f
+        char           gfid[GF_UUID_BUF_SIZE] = {0};
a3470f
 
a3470f
         VALIDATE_OR_GOTO (frame, err);
a3470f
         VALIDATE_OR_GOTO (this, err);
a3470f
@@ -3828,6 +4771,8 @@ dht_fgetxattr (call_frame_t *frame, xlator_t *this,
a3470f
         VALIDATE_OR_GOTO (fd->inode, err);
a3470f
         VALIDATE_OR_GOTO (this->private, err);
a3470f
 
a3470f
+        conf = this->private;
a3470f
+
a3470f
         local = dht_local_init (frame, NULL, fd, GF_FOP_FGETXATTR);
a3470f
         if (!local) {
a3470f
                 op_errno = ENOMEM;
a3470f
@@ -3852,15 +4797,63 @@ dht_fgetxattr (call_frame_t *frame, xlator_t *this,
a3470f
                 }
a3470f
         }
a3470f
 
a3470f
+        if (fd->inode)
a3470f
+                gf_uuid_unparse(fd->inode->gfid, gfid);
a3470f
+
a3470f
         if ((fd->inode->ia_type == IA_IFDIR)
a3470f
             && key
a3470f
             && (strncmp (key, GF_XATTR_LOCKINFO_KEY,
a3470f
                          strlen (GF_XATTR_LOCKINFO_KEY)) != 0)) {
a3470f
-                cnt = local->call_cnt = layout->cnt;
a3470f
+                local->call_cnt = conf->subvolume_cnt;
a3470f
+                cnt             = conf->subvolume_cnt;
a3470f
+                ret = dht_inode_ctx_mdsvol_get (fd->inode, this, &mds_subvol);
a3470f
+
a3470f
+                if (!mds_subvol) {
a3470f
+                        gf_msg (this->name, GF_LOG_ERROR, 0,
a3470f
+                                DHT_MSG_HASHED_SUBVOL_GET_FAILED,
a3470f
+                                "cannot determine MDS, fetching xattr %s "
a3470f
+                                " randomly from a subvol for gfid %s ",
a3470f
+                                key, gfid);
a3470f
+                } else {
a3470f
+                        /* TODO need to handle it, As of now we are
a3470f
+                           choosing availability instead of chossing
a3470f
+                           consistencty, in case of hashed_subvol is
a3470f
+                           down winding a getxattr call on other subvol
a3470f
+                           and return xattr
a3470f
+                        */
a3470f
+                        local->mds_subvol = mds_subvol;
a3470f
+                        for (i = 0; i < cnt; i++) {
a3470f
+                                if (conf->subvolumes[i] == mds_subvol) {
a3470f
+                                        if (!conf->subvolume_status[i]) {
a3470f
+                                                gf_msg (this->name,
a3470f
+                                                        GF_LOG_WARNING, 0,
a3470f
+                                                        DHT_MSG_HASHED_SUBVOL_DOWN,
a3470f
+                                                        "MDS subvolume %s is down"
a3470f
+                                                        " for gfid %s so fetching xattr "
a3470f
+                                                        " %s randomly from a subvol ",
a3470f
+                                                        local->mds_subvol->name,
a3470f
+                                                        gfid, key);
a3470f
+                                                ret = 1;
a3470f
+                                        }
a3470f
+                                }
a3470f
+                        }
a3470f
+                }
a3470f
+
a3470f
+                if (!ret && key && local->mds_subvol &&
a3470f
+                    dht_match_xattr (key)) {
a3470f
+                        STACK_WIND (frame, dht_mds_getxattr_cbk,
a3470f
+                                    local->mds_subvol,
a3470f
+                                    local->mds_subvol->fops->fgetxattr,
a3470f
+                                    fd, key, NULL);
a3470f
+
a3470f
+                        return 0;
a3470f
+                }
a3470f
+
a3470f
         } else {
a3470f
                 cnt = local->call_cnt  = 1;
a3470f
         }
a3470f
 
a3470f
+
a3470f
         for (i = 0; i < cnt; i++) {
a3470f
                 subvol = layout->list[i].xlator;
a3470f
                 STACK_WIND (frame, dht_getxattr_cbk,
a3470f
@@ -3956,6 +4949,169 @@ out:
a3470f
         return 0;
a3470f
 }
a3470f
 
a3470f
+/* Function is call by dict_foreach_fnmatch if key is match with
a3470f
+   user.* and set boolean flag to true
a3470f
+*/
a3470f
+static int
a3470f
+dht_is_user_xattr (dict_t *this, char *key, data_t *value, void *data)
a3470f
+{
a3470f
+        gf_boolean_t *user_xattr_found = data;
a3470f
+        *user_xattr_found = _gf_true;
a3470f
+        return 0;
a3470f
+}
a3470f
+
a3470f
+
a3470f
+/* Common code to wind a (f)setxattr call to set xattr on directory
a3470f
+*/
a3470f
+int
a3470f
+dht_dir_common_setxattr (call_frame_t *frame, xlator_t *this, loc_t *loc,
a3470f
+                         fd_t *fd, dict_t *xattr, int flags, dict_t *xdata,
a3470f
+                         int *op_errno)
a3470f
+
a3470f
+{
a3470f
+        dict_t       *xattrop             = NULL;
a3470f
+        int32_t       subone[1]            = {-1};
a3470f
+        gf_boolean_t  uxattr_key_found     = _gf_false;
a3470f
+        xlator_t     *mds_subvol          = NULL;
a3470f
+        xlator_t     *travvol              = NULL;
a3470f
+        dht_conf_t   *conf                = NULL;
a3470f
+        int           ret                  = -1;
a3470f
+        int           i                    = 0;
a3470f
+        int           call_cnt             = 0;
a3470f
+        dht_local_t  *local                = NULL;
a3470f
+        char          gfid_local[GF_UUID_BUF_SIZE] = {0};
a3470f
+
a3470f
+        conf = this->private;
a3470f
+        local    = frame->local;
a3470f
+        call_cnt = conf->subvolume_cnt;
a3470f
+        local->flags = flags;
a3470f
+
a3470f
+        if (local->gfid)
a3470f
+                gf_uuid_unparse(local->gfid, gfid_local);
a3470f
+
a3470f
+        /* Check if any user xattr present in xattr
a3470f
+        */
a3470f
+        dict_foreach_fnmatch (xattr, "user*", dht_is_user_xattr,
a3470f
+                              &uxattr_key_found);
a3470f
+
a3470f
+        /* Check if any custom key xattr present in dict xattr
a3470f
+           and start index from 1 because user xattr already
a3470f
+           checked in previous line
a3470f
+        */
a3470f
+        for (i = 1; xattrs_to_heal[i]; i++)
a3470f
+                if (dict_get (xattr, xattrs_to_heal[i]))
a3470f
+                        uxattr_key_found = _gf_true;
a3470f
+
a3470f
+        /* If there is no custom key xattr present or gfid is root
a3470f
+           or call_cnt is 1 then wind a (f)setxattr call on all subvols
a3470f
+        */
a3470f
+        if (!uxattr_key_found || __is_root_gfid (local->gfid) || call_cnt == 1) {
a3470f
+                for (i = 0; i < conf->subvolume_cnt; i++) {
a3470f
+                        travvol = conf->subvolumes[i];
a3470f
+                        if (fd) {
a3470f
+                                STACK_WIND_COOKIE (frame, dht_err_cbk,
a3470f
+                                                   travvol, travvol,
a3470f
+                                                   travvol->fops->fsetxattr,
a3470f
+                                                   fd, xattr, flags, xdata);
a3470f
+                        } else {
a3470f
+                                STACK_WIND_COOKIE (frame, dht_err_cbk,
a3470f
+                                                   travvol, travvol,
a3470f
+                                                   travvol->fops->setxattr,
a3470f
+                                                   loc, xattr, flags, xdata);
a3470f
+                        }
a3470f
+                }
a3470f
+
a3470f
+                return 0;
a3470f
+        }
a3470f
+
a3470f
+        /* Calculate hash subvol based on inode and parent inode
a3470f
+         */
a3470f
+        if (fd) {
a3470f
+                ret = dht_inode_ctx_mdsvol_get (fd->inode, this, &mds_subvol);
a3470f
+        } else {
a3470f
+                ret = dht_inode_ctx_mdsvol_get (loc->inode, this, &mds_subvol);
a3470f
+        }
a3470f
+        if (ret || !mds_subvol) {
a3470f
+                if (fd) {
a3470f
+                        gf_msg (this->name, GF_LOG_ERROR, 0,
a3470f
+                                DHT_MSG_HASHED_SUBVOL_GET_FAILED,
a3470f
+                                "Failed to get mds subvol for fd %p"
a3470f
+                                "gfid is %s ", fd, gfid_local);
a3470f
+                } else {
a3470f
+                        gf_msg (this->name, GF_LOG_ERROR, 0,
a3470f
+                                DHT_MSG_HASHED_SUBVOL_GET_FAILED,
a3470f
+                                "Failed to get mds subvol for path %s"
a3470f
+                                "gfid is %s ", loc->path, gfid_local);
a3470f
+                }
a3470f
+                (*op_errno) = ENOENT;
a3470f
+                goto err;
a3470f
+        }
a3470f
+
a3470f
+        local->mds_subvol = mds_subvol;
a3470f
+
a3470f
+        for (i = 0; i < conf->subvolume_cnt; i++) {
a3470f
+                if (conf->subvolumes[i] ==  mds_subvol) {
a3470f
+                        if (!conf->subvolume_status[i]) {
a3470f
+                                gf_msg (this->name, GF_LOG_WARNING,
a3470f
+                                        0, DHT_MSG_HASHED_SUBVOL_DOWN,
a3470f
+                                        "MDS subvol is down for path "
a3470f
+                                        " %s gfid is %s Unable to set xattr " ,
a3470f
+                                        local->loc.path, gfid_local);
a3470f
+                                (*op_errno) = ENOTCONN;
a3470f
+                                goto err;
a3470f
+                        }
a3470f
+                }
a3470f
+        }
a3470f
+
a3470f
+        if (uxattr_key_found) {
a3470f
+                xattrop = dict_new ();
a3470f
+                if (!xattrop) {
a3470f
+                        gf_msg (this->name, GF_LOG_ERROR, DHT_MSG_NO_MEMORY,
a3470f
+                                0, "dictionary creation failed for path %s "
a3470f
+                                "for gfid is %s ", local->loc.path, gfid_local);
a3470f
+                        (*op_errno) = ENOMEM;
a3470f
+                        goto err;
a3470f
+                }
a3470f
+                local->xattr = dict_ref (xattr);
a3470f
+                /* Subtract current MDS xattr value to -1 , value of MDS
a3470f
+                   xattr represents no. of times xattr modification failed
a3470f
+                   on non MDS subvols.
a3470f
+                */
a3470f
+                ret = dht_dict_set_array (xattrop, conf->mds_xattr_key, subone, 1);
a3470f
+                if (ret != 0) {
a3470f
+                        gf_msg (this->name, GF_LOG_ERROR, 0, DHT_MSG_DICT_SET_FAILED,
a3470f
+                                "dictionary set array failed for path %s "
a3470f
+                                "for gfid is %s ", local->loc.path, gfid_local);
a3470f
+                        if (xattrop)
a3470f
+                                dict_unref (xattrop);
a3470f
+                        (*op_errno) = ret;
a3470f
+                        goto err;
a3470f
+                }
a3470f
+                /* Wind a xattrop call to use ref counting approach
a3470f
+                   update mds xattr to -1 before update xattr on
a3470f
+                   hashed subvol and update mds xattr to +1 after update
a3470f
+                   xattr on all non hashed subvol
a3470f
+                */
a3470f
+                if (fd) {
a3470f
+                        STACK_WIND (frame, dht_xattrop_mds_cbk,
a3470f
+                                    local->mds_subvol,
a3470f
+                                    local->mds_subvol->fops->fxattrop,
a3470f
+                                     fd, GF_XATTROP_ADD_ARRAY, xattrop, NULL);
a3470f
+                } else {
a3470f
+                        STACK_WIND (frame, dht_xattrop_mds_cbk,
a3470f
+                                    local->mds_subvol,
a3470f
+                                    local->mds_subvol->fops->xattrop,
a3470f
+                                    loc, GF_XATTROP_ADD_ARRAY,
a3470f
+                                    xattrop, NULL);
a3470f
+                }
a3470f
+                if (xattrop)
a3470f
+                        dict_unref (xattrop);
a3470f
+        }
a3470f
+
a3470f
+        return 0;
a3470f
+err:
a3470f
+        return -1;
a3470f
+}
a3470f
 
a3470f
 
a3470f
 int
a3470f
@@ -3969,7 +5125,6 @@ dht_fsetxattr (call_frame_t *frame, xlator_t *this,
a3470f
         dht_layout_t *layout   = NULL;
a3470f
         int           ret      = -1;
a3470f
         int           call_cnt = 0;
a3470f
-        int           i        = 0;
a3470f
 
a3470f
         VALIDATE_OR_GOTO (frame, err);
a3470f
         VALIDATE_OR_GOTO (this, err);
a3470f
@@ -4009,14 +5164,11 @@ dht_fsetxattr (call_frame_t *frame, xlator_t *this,
a3470f
         local->call_cnt = call_cnt = layout->cnt;
a3470f
 
a3470f
         if (IA_ISDIR (fd->inode->ia_type)) {
a3470f
-                for (i = 0; i < call_cnt; i++) {
a3470f
-                        STACK_WIND_COOKIE (frame, dht_err_cbk,
a3470f
-                                           layout->list[i].xlator,
a3470f
-                                           layout->list[i].xlator,
a3470f
-                                           layout->list[i].xlator->fops->fsetxattr,
a3470f
-                                           fd, xattr, flags, xdata);
a3470f
-                }
a3470f
-
a3470f
+                local->hashed_subvol = NULL;
a3470f
+                ret = dht_dir_common_setxattr (frame, this, NULL, fd,
a3470f
+                                               xattr, flags, xdata, &op_errno);
a3470f
+                if (ret)
a3470f
+                        goto err;
a3470f
         } else {
a3470f
 
a3470f
                 local->call_cnt = 1;
a3470f
@@ -4043,16 +5195,6 @@ err:
a3470f
         return 0;
a3470f
 }
a3470f
 
a3470f
-static int
a3470f
-dht_common_setxattr_cbk (call_frame_t *frame, void *cookie,
a3470f
-                         xlator_t *this, int32_t op_ret, int32_t op_errno,
a3470f
-                         dict_t *xdata)
a3470f
-{
a3470f
-        DHT_STACK_UNWIND (setxattr, frame, op_ret, op_errno, xdata);
a3470f
-
a3470f
-        return 0;
a3470f
-}
a3470f
-
a3470f
 
a3470f
 int
a3470f
 dht_checking_pathinfo_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
@@ -4190,6 +5332,7 @@ dht_nuke_dir (call_frame_t *frame, xlator_t *this, loc_t *loc, data_t *tmp)
a3470f
         return 0;
a3470f
 }
a3470f
 
a3470f
+
a3470f
 int
a3470f
 dht_setxattr (call_frame_t *frame, xlator_t *this,
a3470f
               loc_t *loc, dict_t *xattr, int flags, dict_t *xdata)
a3470f
@@ -4209,6 +5352,7 @@ dht_setxattr (call_frame_t *frame, xlator_t *this,
a3470f
         int           call_cnt = 0;
a3470f
         uint32_t      new_hash = 0;
a3470f
 
a3470f
+
a3470f
         VALIDATE_OR_GOTO (frame, err);
a3470f
         VALIDATE_OR_GOTO (this, err);
a3470f
         VALIDATE_OR_GOTO (loc, err);
a3470f
@@ -4248,6 +5392,11 @@ dht_setxattr (call_frame_t *frame, xlator_t *this,
a3470f
         }
a3470f
 
a3470f
         local->call_cnt = call_cnt = layout->cnt;
a3470f
+        tmp = dict_get (xattr, conf->mds_xattr_key);
a3470f
+        if (tmp) {
a3470f
+                op_errno = ENOTSUP;
a3470f
+                goto err;
a3470f
+        }
a3470f
 
a3470f
         tmp = dict_get (xattr, GF_XATTR_FILE_MIGRATE_KEY);
a3470f
         if (tmp) {
a3470f
@@ -4423,15 +5572,11 @@ dht_setxattr (call_frame_t *frame, xlator_t *this,
a3470f
         local->xattr_req = xdata ? dict_ref (xdata) : dict_new ();
a3470f
 
a3470f
         if (IA_ISDIR (loc->inode->ia_type)) {
a3470f
-
a3470f
-                for (i = 0; i < call_cnt; i++) {
a3470f
-                        STACK_WIND_COOKIE (frame, dht_err_cbk,
a3470f
-                                           layout->list[i].xlator,
a3470f
-                                           layout->list[i].xlator,
a3470f
-                                           layout->list[i].xlator->fops->setxattr,
a3470f
-                                           loc, xattr, flags, xdata);
a3470f
-                }
a3470f
-
a3470f
+                local->hashed_subvol = NULL;
a3470f
+                ret = dht_dir_common_setxattr (frame, this, loc, NULL,
a3470f
+                                               xattr, flags, xdata, &op_errno);
a3470f
+                if (ret)
a3470f
+                        goto err;
a3470f
         } else {
a3470f
 
a3470f
                 local->rebalance.xattr = dict_ref (xattr);
a3470f
@@ -4670,6 +5815,12 @@ dht_removexattr (call_frame_t *frame, xlator_t *this,
a3470f
         local->call_cnt = call_cnt = layout->cnt;
a3470f
         local->key = gf_strdup (key);
a3470f
 
a3470f
+        if (key &&
a3470f
+            (strncmp (key, conf->mds_xattr_key, strlen(key)) == 0)) {
a3470f
+                op_errno = ENOTSUP;
a3470f
+                goto err;
a3470f
+        }
a3470f
+
a3470f
         if (IA_ISDIR (loc->inode->ia_type)) {
a3470f
                 for (i = 0; i < call_cnt; i++) {
a3470f
                         STACK_WIND_COOKIE (frame, dht_removexattr_cbk,
a3470f
@@ -7641,6 +8792,10 @@ dht_mkdir_hashed_cbk (call_frame_t *frame, void *cookie,
a3470f
         dht_iatt_merge (this, &local->postparent, postparent, prev);
a3470f
 
a3470f
         local->call_cnt = conf->subvolume_cnt - 1;
a3470f
+        /* Delete internal mds xattr from params dict to avoid store
a3470f
+          internal mds xattr on other subvols
a3470f
+        */
a3470f
+        dict_del (local->params, conf->mds_xattr_key);
a3470f
 
a3470f
         if (gf_uuid_is_null (local->loc.gfid))
a3470f
                 gf_uuid_copy (local->loc.gfid, stbuf->ia_gfid);
a3470f
@@ -7652,6 +8807,14 @@ dht_mkdir_hashed_cbk (call_frame_t *frame, void *cookie,
a3470f
                                         &local->loc, layout);
a3470f
         }
a3470f
 
a3470f
+        /* Set hashed subvol as a mds subvol on inode ctx */
a3470f
+        ret = dht_inode_ctx_mdsvol_set (local->inode, this, hashed_subvol);
a3470f
+        if (ret) {
a3470f
+                gf_msg (this->name, GF_LOG_ERROR, 0, DHT_MSG_SET_INODE_CTX_FAILED,
a3470f
+                        "Failed to set hashed subvol for %s on inode vol is %s",
a3470f
+                        local->loc.path, hashed_subvol->name);
a3470f
+        }
a3470f
+
a3470f
         for (i = 0; i < conf->subvolume_cnt; i++) {
a3470f
                 if (conf->subvolumes[i] == hashed_subvol)
a3470f
                         continue;
a3470f
@@ -7661,6 +8824,7 @@ dht_mkdir_hashed_cbk (call_frame_t *frame, void *cookie,
a3470f
                                    &local->loc, local->mode, local->umask,
a3470f
                                    local->params);
a3470f
         }
a3470f
+
a3470f
         return 0;
a3470f
 err:
a3470f
         if (local->op_ret != 0) {
a3470f
@@ -7682,9 +8846,13 @@ dht_mkdir_guard_parent_layout_cbk (call_frame_t *frame, xlator_t *this,
a3470f
                                    dict_t *params)
a3470f
 {
a3470f
         dht_local_t *local                    = NULL;
a3470f
+        dht_conf_t  *conf                     = 0;
a3470f
         char          pgfid[GF_UUID_BUF_SIZE] = {0};
a3470f
+        int          ret                      = -1;
a3470f
+        int32_t      zero[1]                  = {0};
a3470f
 
a3470f
         local = frame->local;
a3470f
+        conf  = this->private;
a3470f
 
a3470f
         gf_uuid_unparse (loc->parent->gfid, pgfid);
a3470f
 
a3470f
@@ -7698,6 +8866,15 @@ dht_mkdir_guard_parent_layout_cbk (call_frame_t *frame, xlator_t *this,
a3470f
         }
a3470f
 
a3470f
         local->op_ret = -1;
a3470f
+        /* Add internal MDS xattr on disk for hashed subvol
a3470f
+        */
a3470f
+        ret = dht_dict_set_array (params, conf->mds_xattr_key, zero, 1);
a3470f
+        if (ret) {
a3470f
+                gf_msg (this->name, GF_LOG_WARNING, ENOMEM,
a3470f
+                        DHT_MSG_DICT_SET_FAILED,
a3470f
+                        "Failed to set dictionary value:key = %s for "
a3470f
+                        "path %s", conf->mds_xattr_key, loc->path);
a3470f
+        }
a3470f
 
a3470f
         STACK_WIND_COOKIE (frame, dht_mkdir_hashed_cbk, local->hashed_subvol,
a3470f
                            local->hashed_subvol,
a3470f
diff --git a/xlators/cluster/dht/src/dht-common.h b/xlators/cluster/dht/src/dht-common.h
a3470f
index 47a2e23..2aa7251 100644
a3470f
--- a/xlators/cluster/dht/src/dht-common.h
a3470f
+++ b/xlators/cluster/dht/src/dht-common.h
a3470f
@@ -28,6 +28,7 @@
a3470f
 #define GF_XATTR_FIX_LAYOUT_KEY         "distribute.fix.layout"
a3470f
 #define GF_XATTR_TIER_LAYOUT_FIXED_KEY  "trusted.tier.fix.layout.complete"
a3470f
 #define GF_XATTR_FILE_MIGRATE_KEY       "trusted.distribute.migrate-data"
a3470f
+#define DHT_MDS_STR                     "mds"
a3470f
 #define GF_DHT_LOOKUP_UNHASHED_ON       1
a3470f
 #define GF_DHT_LOOKUP_UNHASHED_AUTO     2
a3470f
 #define DHT_PATHINFO_HEADER             "DISTRIBUTE:"
a3470f
@@ -43,6 +44,12 @@
a3470f
 #define DHT_DIR_STAT_BLOCKS          8
a3470f
 #define DHT_DIR_STAT_SIZE            4096
a3470f
 
a3470f
+/* Array to hold custom xattr keys
a3470f
+*/
a3470f
+extern char *xattrs_to_heal[];
a3470f
+
a3470f
+
a3470f
+
a3470f
 #include <fnmatch.h>
a3470f
 
a3470f
 /* Array to hold custom xattr keys
a3470f
@@ -116,6 +123,7 @@ struct dht_inode_ctx {
a3470f
         dht_layout_t    *layout;
a3470f
         dht_stat_time_t  time;
a3470f
         xlator_t        *lock_subvol;
a3470f
+        xlator_t        *mds_subvol;     /* This is only used for directories */
a3470f
 };
a3470f
 
a3470f
 typedef struct dht_inode_ctx dht_inode_ctx_t;
a3470f
@@ -262,6 +270,7 @@ struct dht_local {
a3470f
         /* Use stbuf as the postbuf, when we require both
a3470f
          * pre and post attrs */
a3470f
         struct iatt              stbuf;
a3470f
+        struct iatt              mds_stbuf;
a3470f
         struct iatt              prebuf;
a3470f
         struct iatt              preoldparent;
a3470f
         struct iatt              postoldparent;
a3470f
@@ -273,6 +282,8 @@ struct dht_local {
a3470f
         inode_t                 *inode;
a3470f
         dict_t                  *params;
a3470f
         dict_t                  *xattr;
a3470f
+        dict_t                  *mds_xattr;
a3470f
+        dict_t                  *xdata;      /* dict used to save xdata response by xattr fop */
a3470f
         dict_t                  *xattr_req;
a3470f
         dht_layout_t            *layout;
a3470f
         size_t                   size;
a3470f
@@ -281,7 +292,9 @@ struct dht_local {
a3470f
         xlator_t                *dst_hashed, *dst_cached;
a3470f
         xlator_t                *cached_subvol;
a3470f
         xlator_t                *hashed_subvol;
a3470f
+        xlator_t                *mds_subvol; /* This is use for dir only */
a3470f
         char                     need_selfheal;
a3470f
+        char                     need_xattr_heal;
a3470f
         int                      file_count;
a3470f
         int                      dir_count;
a3470f
         call_frame_t            *main_frame;
a3470f
@@ -365,6 +378,9 @@ struct dht_local {
a3470f
 
a3470f
         /* fd open check */
a3470f
         gf_boolean_t fd_checked;
a3470f
+        /* This is use only for directory operation */
a3470f
+        int32_t valid;
a3470f
+        gf_boolean_t heal_layout;
a3470f
 };
a3470f
 typedef struct dht_local dht_local_t;
a3470f
 
a3470f
@@ -651,6 +667,7 @@ struct dht_conf {
a3470f
 
a3470f
         /* Support variable xattr names. */
a3470f
         char            *xattr_name;
a3470f
+        char            *mds_xattr_key;
a3470f
         char            *link_xattr_name;
a3470f
         char            *commithash_xattr_name;
a3470f
         char            *wild_xattr_name;
a3470f
@@ -1333,9 +1350,6 @@ dht_normalize_stats (struct statvfs *buf, unsigned long bsize,
a3470f
 int
a3470f
 add_opt(char **optsp, const char *opt);
a3470f
 
a3470f
-char *
a3470f
-getChoices (const char *value);
a3470f
-
a3470f
 int
a3470f
 dht_aggregate_split_brain_xattr (dict_t *dst, char *key, data_t *value);
a3470f
 
a3470f
@@ -1345,18 +1359,12 @@ dht_remove_stale_linkto (void *data);
a3470f
 int
a3470f
 dht_remove_stale_linkto_cbk (int ret, call_frame_t *sync_frame, void *data);
a3470f
 
a3470f
-
a3470f
 int
a3470f
 dht_fd_ctx_set (xlator_t *this, fd_t *fd, xlator_t *subvol);
a3470f
 
a3470f
 int
a3470f
 dht_check_and_open_fd_on_subvol (xlator_t *this, call_frame_t *frame);
a3470f
 
a3470f
-
a3470f
-
a3470f
-
a3470f
-
a3470f
-
a3470f
 /* FD fop callbacks */
a3470f
 
a3470f
 int
a3470f
@@ -1409,12 +1417,10 @@ int
a3470f
 dht_file_attr_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
                    int op_ret, int op_errno, struct iatt *stbuf, dict_t *xdata);
a3470f
 
a3470f
-
a3470f
 int
a3470f
 dht_file_removexattr_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
                           int op_ret, int op_errno, dict_t *xdata);
a3470f
 
a3470f
-
a3470f
 int
a3470f
 dht_file_setxattr_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
                        int op_ret, int op_errno, dict_t *xdata);
a3470f
@@ -1426,4 +1432,48 @@ int dht_request_iatt_in_xdata (xlator_t *this, dict_t *xattr_req);
a3470f
 
a3470f
 int dht_read_iatt_from_xdata (xlator_t *this, dict_t *xdata,
a3470f
                               struct iatt *stbuf);
a3470f
+
a3470f
+/* All custom xattr heal functions */
a3470f
+int
a3470f
+dht_dir_heal_xattrs (void *data);
a3470f
+
a3470f
+int
a3470f
+dht_dir_heal_xattrs_done (int ret, call_frame_t *sync_frame, void *data);
a3470f
+
a3470f
+void
a3470f
+dht_aggregate_xattr (dict_t *dst, dict_t *src);
a3470f
+
a3470f
+int32_t
a3470f
+dht_dict_set_array(dict_t *dict, char *key, int32_t value[], int32_t size);
a3470f
+
a3470f
+int
a3470f
+dht_set_user_xattr (dict_t *dict, char *k, data_t *v, void *data);
a3470f
+
a3470f
+void
a3470f
+dht_dir_set_heal_xattr (xlator_t *this, dht_local_t *local, dict_t *dst,
a3470f
+                        dict_t *src, int *uret, int *uflag);
a3470f
+
a3470f
+int
a3470f
+dht_dir_xattr_heal (xlator_t *this, dht_local_t *local);
a3470f
+
a3470f
+int32_t
a3470f
+dht_dict_get_array (dict_t *dict, char *key, int32_t value[], int32_t size, int *errst);
a3470f
+
a3470f
+xlator_t *
a3470f
+dht_inode_get_hashed_subvol (inode_t *inode, xlator_t *this, loc_t *loc);
a3470f
+
a3470f
+int
a3470f
+dht_mark_mds_subvolume (call_frame_t *frame, xlator_t *this);
a3470f
+
a3470f
+int
a3470f
+dht_mds_internal_setxattr_cbk (call_frame_t *frame, void *cookie,
a3470f
+                               xlator_t *this, int op_ret, int op_errno,
a3470f
+                               dict_t *xdata);
a3470f
+int
a3470f
+dht_inode_ctx_mdsvol_set (inode_t *inode, xlator_t *this,
a3470f
+                          xlator_t *mds_subvol);
a3470f
+int
a3470f
+dht_inode_ctx_mdsvol_get (inode_t *inode, xlator_t *this,
a3470f
+                          xlator_t **mdsvol);
a3470f
+
a3470f
 #endif/* _DHT_H */
a3470f
diff --git a/xlators/cluster/dht/src/dht-helper.c b/xlators/cluster/dht/src/dht-helper.c
a3470f
index e56a085..6e20aea 100644
a3470f
--- a/xlators/cluster/dht/src/dht-helper.c
a3470f
+++ b/xlators/cluster/dht/src/dht-helper.c
a3470f
@@ -767,6 +767,10 @@ dht_local_wipe (xlator_t *this, dht_local_t *local)
a3470f
 
a3470f
         if (local->xattr_req)
a3470f
                 dict_unref (local->xattr_req);
a3470f
+        if (local->mds_xattr)
a3470f
+                dict_unref (local->mds_xattr);
a3470f
+        if (local->xdata)
a3470f
+                dict_unref (local->xdata);
a3470f
 
a3470f
         if (local->selfheal.layout) {
a3470f
                 dht_layout_unref (this, local->selfheal.layout);
a3470f
@@ -2085,12 +2089,24 @@ dht_heal_full_path_done (int op_ret, call_frame_t *heal_frame, void *data)
a3470f
 
a3470f
         call_frame_t            *main_frame       = NULL;
a3470f
         dht_local_t             *local            = NULL;
a3470f
+        xlator_t                *this             = NULL;
a3470f
+        int                     ret               = -1;
a3470f
 
a3470f
         local = heal_frame->local;
a3470f
         main_frame = local->main_frame;
a3470f
         local->main_frame = NULL;
a3470f
+        this = heal_frame->this;
a3470f
 
a3470f
         dht_set_fixed_dir_stat (&local->postparent);
a3470f
+        if (local->need_xattr_heal) {
a3470f
+                local->need_xattr_heal = 0;
a3470f
+                ret =  dht_dir_xattr_heal (this, local);
a3470f
+                if (ret)
a3470f
+                        gf_msg (this->name, GF_LOG_ERROR, ret,
a3470f
+                                DHT_MSG_DIR_XATTR_HEAL_FAILED,
a3470f
+                                "xattr heal failed for directory  %s ",
a3470f
+                                local->loc.path);
a3470f
+        }
a3470f
 
a3470f
         DHT_STACK_UNWIND (lookup, main_frame, 0, 0,
a3470f
                           local->inode, &local->stbuf, local->xattr,
a3470f
@@ -2254,3 +2270,52 @@ dht_lk_inode_unref (call_frame_t *frame, int32_t op_ret)
a3470f
 out:
a3470f
         return ret;
a3470f
 }
a3470f
+
a3470f
+/* Code to update custom extended attributes from src dict to dst dict
a3470f
+*/
a3470f
+void
a3470f
+dht_dir_set_heal_xattr (xlator_t *this, dht_local_t *local, dict_t *dst,
a3470f
+                        dict_t *src, int *uret, int *uflag)
a3470f
+{
a3470f
+        int               ret                 = -1;
a3470f
+        data_t           *keyval              = NULL;
a3470f
+        int               luret               = -1;
a3470f
+        int               luflag              = -1;
a3470f
+        int               i                   = 0;
a3470f
+
a3470f
+        if (!src || !dst) {
a3470f
+                gf_msg (this->name, GF_LOG_WARNING, EINVAL,
a3470f
+                        DHT_MSG_DICT_SET_FAILED,
a3470f
+                        "src or dst is NULL. Failed to set "
a3470f
+                        " dictionary value for path %s",
a3470f
+                        local->loc.path);
a3470f
+                return;
a3470f
+        }
a3470f
+        /* Check if any user xattr present in src dict and set
a3470f
+           it to dst dict
a3470f
+        */
a3470f
+        luret = dict_foreach_fnmatch (src, "user.*",
a3470f
+                                      dht_set_user_xattr, dst);
a3470f
+        /* Check if any other custom xattr present in src dict
a3470f
+           and set it to dst dict, here index start from 1 because
a3470f
+           user xattr already checked in previous statement
a3470f
+        */
a3470f
+        for (i = 1; xattrs_to_heal[i]; i++) {
a3470f
+                keyval = dict_get (src, xattrs_to_heal[i]);
a3470f
+                if (keyval) {
a3470f
+                        luflag = 1;
a3470f
+                        ret = dict_set (dst, xattrs_to_heal[i], keyval);
a3470f
+                        if (ret)
a3470f
+                                gf_msg (this->name, GF_LOG_WARNING, ENOMEM,
a3470f
+                                        DHT_MSG_DICT_SET_FAILED,
a3470f
+                                        "Failed to set dictionary value:key = %s for "
a3470f
+                                        "path %s", xattrs_to_heal[i],
a3470f
+                                        local->loc.path);
a3470f
+                        keyval = NULL;
a3470f
+                }
a3470f
+        }
a3470f
+        if (uret)
a3470f
+                (*uret) = luret;
a3470f
+        if (uflag)
a3470f
+                (*uflag) = luflag;
a3470f
+}
a3470f
diff --git a/xlators/cluster/dht/src/dht-inode-write.c b/xlators/cluster/dht/src/dht-inode-write.c
a3470f
index 9709acf..7c596b1 100644
a3470f
--- a/xlators/cluster/dht/src/dht-inode-write.c
a3470f
+++ b/xlators/cluster/dht/src/dht-inode-write.c
a3470f
@@ -1161,6 +1161,7 @@ dht_setattr_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
                 dht_iatt_merge (this, &local->stbuf, statpost, prev);
a3470f
 
a3470f
                 local->op_ret = 0;
a3470f
+                local->op_errno = 0;
a3470f
         }
a3470f
 unlock:
a3470f
         UNLOCK (&frame->lock);
a3470f
@@ -1178,16 +1179,117 @@ unlock:
a3470f
 }
a3470f
 
a3470f
 
a3470f
+/* Keep the existing code same for all the cases other than regular file */
a3470f
+int
a3470f
+dht_non_mds_setattr_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
+                         int op_ret, int op_errno, struct iatt *statpre,
a3470f
+                         struct iatt *statpost, dict_t *xdata)
a3470f
+{
a3470f
+        dht_local_t  *local = NULL;
a3470f
+        int           this_call_cnt = 0;
a3470f
+        xlator_t     *prev = NULL;
a3470f
+
a3470f
+
a3470f
+        local = frame->local;
a3470f
+        prev = cookie;
a3470f
+
a3470f
+        LOCK (&frame->lock);
a3470f
+        {
a3470f
+                if (op_ret == -1) {
a3470f
+                        gf_msg (this->name, op_errno, 0,
a3470f
+                                0, "subvolume %s returned -1",
a3470f
+                                prev->name);
a3470f
+
a3470f
+                        goto unlock;
a3470f
+                }
a3470f
+
a3470f
+                dht_iatt_merge (this, &local->prebuf, statpre, prev);
a3470f
+                dht_iatt_merge (this, &local->stbuf, statpost, prev);
a3470f
+
a3470f
+                local->op_ret = 0;
a3470f
+                local->op_errno = 0;
a3470f
+        }
a3470f
+unlock:
a3470f
+        UNLOCK (&frame->lock);
a3470f
+
a3470f
+        this_call_cnt = dht_frame_return (frame);
a3470f
+        if (is_last_call (this_call_cnt)) {
a3470f
+                dht_inode_ctx_time_set (local->loc.inode, this, &local->stbuf);
a3470f
+                DHT_STACK_UNWIND (setattr, frame, 0, 0,
a3470f
+                                  &local->prebuf, &local->stbuf, xdata);
a3470f
+	}
a3470f
+
a3470f
+        return 0;
a3470f
+}
a3470f
+
a3470f
+
a3470f
+
a3470f
+
a3470f
+
a3470f
+int
a3470f
+dht_mds_setattr_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
+                     int op_ret, int op_errno, struct iatt *statpre,
a3470f
+                     struct iatt *statpost, dict_t *xdata)
a3470f
+
a3470f
+{
a3470f
+        dht_local_t  *local = NULL;
a3470f
+        dht_conf_t   *conf  = NULL;
a3470f
+        xlator_t     *prev = NULL;
a3470f
+        xlator_t     *mds_subvol = NULL;
a3470f
+        struct iatt  loc_stbuf = {0,};
a3470f
+        int i = 0;
a3470f
+
a3470f
+        local = frame->local;
a3470f
+        prev = cookie;
a3470f
+        conf = this->private;
a3470f
+        mds_subvol = local->mds_subvol;
a3470f
+
a3470f
+        if (op_ret == -1) {
a3470f
+                local->op_ret  = op_ret;
a3470f
+                local->op_errno = op_errno;
a3470f
+                gf_msg_debug (this->name, op_errno,
a3470f
+                              "subvolume %s returned -1",
a3470f
+                              prev->name);
a3470f
+                goto out;
a3470f
+        }
a3470f
+
a3470f
+        local->op_ret = 0;
a3470f
+        loc_stbuf = local->stbuf;
a3470f
+        dht_iatt_merge (this, &local->prebuf, statpre, prev);
a3470f
+        dht_iatt_merge (this, &local->stbuf, statpost, prev);
a3470f
+
a3470f
+        local->call_cnt = conf->subvolume_cnt - 1;
a3470f
+        for (i = 0; i < conf->subvolume_cnt; i++) {
a3470f
+                if (mds_subvol == conf->subvolumes[i])
a3470f
+                        continue;
a3470f
+                STACK_WIND_COOKIE (frame, dht_non_mds_setattr_cbk,
a3470f
+                                   conf->subvolumes[i], conf->subvolumes[i],
a3470f
+                                   conf->subvolumes[i]->fops->setattr,
a3470f
+                                   &local->loc, &loc_stbuf,
a3470f
+                                   local->valid, local->xattr_req);
a3470f
+        }
a3470f
+
a3470f
+        return 0;
a3470f
+out:
a3470f
+        DHT_STACK_UNWIND (setattr, frame, local->op_ret, local->op_errno,
a3470f
+                          &local->prebuf, &local->stbuf, xdata);
a3470f
+
a3470f
+        return 0;
a3470f
+}
a3470f
+
a3470f
 int
a3470f
 dht_setattr (call_frame_t *frame, xlator_t *this, loc_t *loc,
a3470f
              struct iatt *stbuf, int32_t valid, dict_t *xdata)
a3470f
 {
a3470f
-        xlator_t     *subvol = NULL;
a3470f
-        dht_layout_t *layout = NULL;
a3470f
-        dht_local_t  *local  = NULL;
a3470f
-        int           op_errno = -1;
a3470f
-        int           i = -1;
a3470f
-        int           call_cnt = 0;
a3470f
+        xlator_t     *subvol     = NULL;
a3470f
+        xlator_t     *mds_subvol = NULL;
a3470f
+        dht_layout_t *layout     = NULL;
a3470f
+        dht_local_t  *local      = NULL;
a3470f
+        int           op_errno   = -1;
a3470f
+        int           i          = -1;
a3470f
+        int           ret        = -1;
a3470f
+        int           call_cnt   = 0;
a3470f
+        dht_conf_t   *conf       = NULL;
a3470f
 
a3470f
         VALIDATE_OR_GOTO (frame, err);
a3470f
         VALIDATE_OR_GOTO (this, err);
a3470f
@@ -1195,6 +1297,7 @@ dht_setattr (call_frame_t *frame, xlator_t *this, loc_t *loc,
a3470f
         VALIDATE_OR_GOTO (loc->inode, err);
a3470f
         VALIDATE_OR_GOTO (loc->path, err);
a3470f
 
a3470f
+        conf = this->private;
a3470f
         local = dht_local_init (frame, loc, NULL, GF_FOP_SETATTR);
a3470f
         if (!local) {
a3470f
                 op_errno = ENOMEM;
a3470f
@@ -1235,12 +1338,50 @@ dht_setattr (call_frame_t *frame, xlator_t *this, loc_t *loc,
a3470f
 
a3470f
         local->call_cnt = call_cnt = layout->cnt;
a3470f
 
a3470f
-        for (i = 0; i < call_cnt; i++) {
a3470f
-                STACK_WIND_COOKIE (frame, dht_setattr_cbk,
a3470f
-                                   layout->list[i].xlator,
a3470f
-                                   layout->list[i].xlator,
a3470f
-                                   layout->list[i].xlator->fops->setattr,
a3470f
+        if (IA_ISDIR (loc->inode->ia_type) &&
a3470f
+            !__is_root_gfid (loc->inode->gfid) && call_cnt != 1) {
a3470f
+                ret = dht_inode_ctx_mdsvol_get (loc->inode, this, &mds_subvol);
a3470f
+                if (ret || !mds_subvol) {
a3470f
+                        gf_msg (this->name, GF_LOG_ERROR, 0,
a3470f
+                                DHT_MSG_HASHED_SUBVOL_GET_FAILED,
a3470f
+                                "Failed to get mds subvol for path %s",
a3470f
+                                local->loc.path);
a3470f
+                        op_errno = EINVAL;
a3470f
+                        goto err;
a3470f
+                }
a3470f
+
a3470f
+                local->mds_subvol = mds_subvol;
a3470f
+                for (i = 0; i < conf->subvolume_cnt; i++) {
a3470f
+                        if (conf->subvolumes[i] ==  mds_subvol) {
a3470f
+                                if (!conf->subvolume_status[i]) {
a3470f
+                                        gf_msg (this->name, GF_LOG_WARNING,
a3470f
+                                                layout->list[i].err,
a3470f
+                                                DHT_MSG_HASHED_SUBVOL_DOWN,
a3470f
+                                                "MDS subvol is down for path "
a3470f
+                                                " %s Unable to set attr " ,
a3470f
+                                                local->loc.path);
a3470f
+                                        op_errno = ENOTCONN;
a3470f
+                                        goto err;
a3470f
+                                }
a3470f
+                        }
a3470f
+                }
a3470f
+                local->valid = valid;
a3470f
+                local->stbuf = *stbuf;
a3470f
+
a3470f
+                STACK_WIND_COOKIE (frame, dht_mds_setattr_cbk,
a3470f
+                                   local->mds_subvol,
a3470f
+                                   local->mds_subvol,
a3470f
+                                   local->mds_subvol->fops->setattr,
a3470f
                                    loc, stbuf, valid, xdata);
a3470f
+                return 0;
a3470f
+        } else {
a3470f
+                for (i = 0; i < call_cnt; i++) {
a3470f
+                        STACK_WIND_COOKIE (frame, dht_setattr_cbk,
a3470f
+                                           layout->list[i].xlator,
a3470f
+                                           layout->list[i].xlator,
a3470f
+                                           layout->list[i].xlator->fops->setattr,
a3470f
+                                           loc, stbuf, valid, xdata);
a3470f
+                }
a3470f
         }
a3470f
 
a3470f
         return 0;
a3470f
diff --git a/xlators/cluster/dht/src/dht-messages.h b/xlators/cluster/dht/src/dht-messages.h
a3470f
index dcfd747..ade32e4 100644
a3470f
--- a/xlators/cluster/dht/src/dht-messages.h
a3470f
+++ b/xlators/cluster/dht/src/dht-messages.h
a3470f
@@ -40,7 +40,7 @@
a3470f
  */
a3470f
 
a3470f
 #define GLFS_DHT_BASE                   GLFS_MSGID_COMP_DHT
a3470f
-#define GLFS_DHT_NUM_MESSAGES           126
a3470f
+#define GLFS_DHT_NUM_MESSAGES           129
a3470f
 #define GLFS_MSGID_END          (GLFS_DHT_BASE + GLFS_DHT_NUM_MESSAGES + 1)
a3470f
 
a3470f
 /* Messages with message IDs */
a3470f
@@ -1083,6 +1083,7 @@
a3470f
  * @diagnosis
a3470f
  * @recommendedaction None
a3470f
  */
a3470f
+
a3470f
 #define DHT_MSG_DIR_LOOKUP_FAILED          (GLFS_DHT_BASE + 118)
a3470f
 
a3470f
 /*
a3470f
@@ -1111,6 +1112,7 @@
a3470f
  * @diagnosis
a3470f
  * @recommendedaction None
a3470f
  */
a3470f
+
a3470f
 #define DHT_MSG_ENTRYLK_ERROR          (GLFS_DHT_BASE + 122)
a3470f
 
a3470f
 /*
a3470f
@@ -1132,7 +1134,7 @@
a3470f
  * @diagnosis
a3470f
  * @recommendedaction None
a3470f
  */
a3470f
-#define DHT_MSG_UNKNOWN_FOP            (GLFS_DHT_BASE + 125)
a3470f
+#define DHT_MSG_UNKNOWN_FOP                     (GLFS_DHT_BASE + 125)
a3470f
 
a3470f
 /*
a3470f
  * @messageid 109126
a3470f
@@ -1141,5 +1143,27 @@
a3470f
  */
a3470f
 #define DHT_MSG_MIGRATE_FILE_SKIPPED        (GLFS_DHT_BASE + 126)
a3470f
 
a3470f
+/*
a3470f
+ * @messageid 109127
a3470f
+ * @diagnosis
a3470f
+ * @recommendedaction None
a3470f
+ */
a3470f
+#define DHT_MSG_DIR_XATTR_HEAL_FAILED           (GLFS_DHT_BASE + 127)
a3470f
+
a3470f
+/*
a3470f
+ * @messageid 109128
a3470f
+ * @diagnosis
a3470f
+ * @recommendedaction None
a3470f
+ */
a3470f
+#define DHT_MSG_HASHED_SUBVOL_DOWN             (GLFS_DHT_BASE + 128)
a3470f
+
a3470f
+/*
a3470f
+ * @messageid 109129
a3470f
+ * @diagnosis
a3470f
+ * @recommendedaction None
a3470f
+ */
a3470f
+#define DHT_MSG_NON_HASHED_SUBVOL_DOWN             (GLFS_DHT_BASE + 129)
a3470f
+
a3470f
+
a3470f
 #define glfs_msg_end_x GLFS_MSGID_END, "Invalid: End of messages"
a3470f
 #endif /* _DHT_MESSAGES_H_ */
a3470f
diff --git a/xlators/cluster/dht/src/dht-selfheal.c b/xlators/cluster/dht/src/dht-selfheal.c
a3470f
index 3b9fcf1..328251d 100644
a3470f
--- a/xlators/cluster/dht/src/dht-selfheal.c
a3470f
+++ b/xlators/cluster/dht/src/dht-selfheal.c
a3470f
@@ -703,6 +703,18 @@ dht_selfheal_dir_xattr_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
         return 0;
a3470f
 }
a3470f
 
a3470f
+/* Code is required to set user xattr to local->xattr
a3470f
+*/
a3470f
+int
a3470f
+dht_set_user_xattr (dict_t *dict, char *k, data_t *v, void *data)
a3470f
+{
a3470f
+        dict_t          *set_xattr          = data;
a3470f
+        int              ret                = -1;
a3470f
+
a3470f
+        ret = dict_set (set_xattr, k, v);
a3470f
+        return ret;
a3470f
+}
a3470f
+
a3470f
 
a3470f
 int
a3470f
 dht_selfheal_dir_xattr_persubvol (call_frame_t *frame, loc_t *loc,
a3470f
@@ -830,7 +842,6 @@ dht_selfheal_dir_xattr_persubvol (call_frame_t *frame, loc_t *loc,
a3470f
 err:
a3470f
         if (xattr)
a3470f
                 dict_unref (xattr);
a3470f
-
a3470f
         if (xdata)
a3470f
                 dict_unref (xdata);
a3470f
 
a3470f
@@ -1128,6 +1139,14 @@ dht_selfheal_dir_setattr_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
         this_call_cnt = dht_frame_return (frame);
a3470f
 
a3470f
         if (is_last_call (this_call_cnt)) {
a3470f
+                if (!local->heal_layout) {
a3470f
+                        gf_msg_trace (this->name, 0,
a3470f
+                                      "Skip heal layout for %s gfid = %s ",
a3470f
+                                      local->loc.path, uuid_utoa(local->gfid));
a3470f
+
a3470f
+                        dht_selfheal_dir_finish (frame, this, 0, 1);
a3470f
+                        return 0;
a3470f
+                }
a3470f
                 ret = dht_selfheal_layout_lock (frame, layout, _gf_false,
a3470f
                                                 dht_selfheal_dir_xattr,
a3470f
                                                 dht_should_heal_layout);
a3470f
@@ -1140,6 +1159,141 @@ dht_selfheal_dir_setattr_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
         return 0;
a3470f
 }
a3470f
 
a3470f
+int
a3470f
+dht_selfheal_dir_check_set_mdsxattr_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
a3470f
+                                         int op_ret, int op_errno, dict_t *xdata)
a3470f
+{
a3470f
+        dht_local_t  *local = NULL;
a3470f
+        xlator_t     *prev  = cookie;
a3470f
+        int           ret   = -1;
a3470f
+        dht_conf_t   *conf  = 0;
a3470f
+
a3470f
+        GF_VALIDATE_OR_GOTO (this->name, frame, out);
a3470f
+        GF_VALIDATE_OR_GOTO (this->name, frame->local, out);
a3470f
+
a3470f
+        local = frame->local;
a3470f
+        conf = this->private;
a3470f
+
a3470f
+        if (op_ret) {
a3470f
+                gf_msg_debug (this->name, op_ret,
a3470f
+                              "internal mds setxattr %s is failed on mds subvol "
a3470f
+                              "at the time of heal on path %s " ,
a3470f
+                               conf->mds_xattr_key, local->loc.path);
a3470f
+        } else {
a3470f
+                /* Save mds subvol on inode ctx */
a3470f
+                ret = dht_inode_ctx_mdsvol_set (local->inode, this, prev);
a3470f
+                if (ret) {
a3470f
+                        gf_msg (this->name, GF_LOG_ERROR, 0,
a3470f
+                                DHT_MSG_SET_INODE_CTX_FAILED,
a3470f
+                                "Failed to set hashed subvol "
a3470f
+                                " %s for %s ", prev->name,
a3470f
+                                local->loc.path);
a3470f
+                }
a3470f
+        }
a3470f
+
a3470f
+out:
a3470f
+        DHT_STACK_DESTROY (frame);
a3470f
+        return 0;
a3470f
+}
a3470f
+
a3470f
+/* Code to set internal mds xattr if it is not present
a3470f
+*/
a3470f
+int
a3470f
+dht_selfheal_dir_check_set_mdsxattr (call_frame_t *frame, loc_t *loc)
a3470f
+{
a3470f
+        dht_local_t  *local          = NULL;
a3470f
+        xlator_t     *this           = NULL;
a3470f
+        xlator_t     *hashed_subvol  = NULL;
a3470f
+        int ret                      = -1;
a3470f
+        dict_t       *xattrs         = NULL;
a3470f
+        char          gfid_local[GF_UUID_BUF_SIZE] = {0,};
a3470f
+        int32_t       zero[1]        = {0};
a3470f
+        call_frame_t *xattr_frame    = NULL;
a3470f
+        dht_local_t  *copy_local     = NULL;
a3470f
+        dht_conf_t   *conf           = 0;
a3470f
+
a3470f
+        local = frame->local;
a3470f
+        this = frame->this;
a3470f
+        conf = this->private;
a3470f
+        gf_uuid_unparse(local->gfid, gfid_local);
a3470f
+
a3470f
+        if (!dict_get (local->xattr, conf->mds_xattr_key)) {
a3470f
+                /* It means no internal MDS xattr has been set yet
a3470f
+                */
a3470f
+                /* Calculate hashed subvol based on inode and
a3470f
+                   parent inode
a3470f
+                */
a3470f
+                hashed_subvol = dht_inode_get_hashed_subvol (local->inode, this,
a3470f
+                                                             loc);
a3470f
+                if (!hashed_subvol) {
a3470f
+                        gf_msg (this->name, GF_LOG_DEBUG, 0,
a3470f
+                                DHT_MSG_HASHED_SUBVOL_GET_FAILED,
a3470f
+                                "Failed to get hashed subvol for path %s"
a3470f
+                                "gfid is %s ",
a3470f
+                                local->loc.path, gfid_local);
a3470f
+                        ret = -1;
a3470f
+                        goto out;
a3470f
+                } else {
a3470f
+                        /* Set internal mds xattr on disk   */
a3470f
+                        xattrs = dict_new ();
a3470f
+                        if (!xattrs) {
a3470f
+                                gf_msg (this->name, GF_LOG_ERROR, ENOMEM,
a3470f
+                                        DHT_MSG_NO_MEMORY, "dict_new failed");
a3470f
+                                ret = -1;
a3470f
+                                goto out;
a3470f
+                        }
a3470f
+                        /* Add internal MDS xattr on disk for hashed subvol
a3470f
+                        */
a3470f
+                        ret = dht_dict_set_array (xattrs, conf->mds_xattr_key, zero, 1);
a3470f
+                        if (ret) {
a3470f
+                                gf_msg (this->name, GF_LOG_WARNING, ENOMEM,
a3470f
+                                        DHT_MSG_DICT_SET_FAILED,
a3470f
+                                        "Failed to set dictionary"
a3470f
+                                        "  value:key = %s for "
a3470f
+                                        "path %s", conf->mds_xattr_key,
a3470f
+                                        local->loc.path);
a3470f
+                                ret = -1;
a3470f
+                                goto out;
a3470f
+                        }
a3470f
+
a3470f
+                        xattr_frame = create_frame (this, this->ctx->pool);
a3470f
+                        if (!xattr_frame) {
a3470f
+                                ret = -1;
a3470f
+                                goto out;
a3470f
+                        }
a3470f
+                        copy_local = dht_local_init (xattr_frame, &(local->loc),
a3470f
+                                                     NULL, 0);
a3470f
+                        if (!copy_local) {
a3470f
+                                ret = -1;
a3470f
+                                DHT_STACK_DESTROY (xattr_frame);
a3470f
+                                goto out;
a3470f
+                        }
a3470f
+
a3470f
+                        copy_local->stbuf = local->stbuf;
a3470f
+                        copy_local->inode = inode_ref (local->inode);
a3470f
+                        gf_uuid_copy (copy_local->loc.gfid, local->gfid);
a3470f
+
a3470f
+                        STACK_WIND_COOKIE (xattr_frame,
a3470f
+                                           dht_selfheal_dir_check_set_mdsxattr_cbk,
a3470f
+                                           (void *)hashed_subvol, hashed_subvol,
a3470f
+                                           hashed_subvol->fops->setxattr,
a3470f
+                                           loc, xattrs, 0, NULL);
a3470f
+                        ret = 0;
a3470f
+                }
a3470f
+        } else {
a3470f
+                ret = 0;
a3470f
+                gf_msg_debug (this->name, 0,
a3470f
+                              "internal xattr %s is present on subvol"
a3470f
+                              "on path %s gfid is %s " , conf->mds_xattr_key,
a3470f
+                               local->loc.path, gfid_local);
a3470f
+        }
a3470f
+
a3470f
+out:
a3470f
+        if (xattrs)
a3470f
+                dict_unref (xattrs);
a3470f
+        return ret;
a3470f
+}
a3470f
+
a3470f
 
a3470f
 int
a3470f
 dht_selfheal_dir_setattr (call_frame_t *frame, loc_t *loc, struct iatt *stbuf,
a3470f
@@ -1159,7 +1313,40 @@ dht_selfheal_dir_setattr (call_frame_t *frame, loc_t *loc, struct iatt *stbuf,
a3470f
                         missing_attr++;
a3470f
         }
a3470f
 
a3470f
+        if (!__is_root_gfid (local->stbuf.ia_gfid)) {
a3470f
+                if (local->need_xattr_heal) {
a3470f
+                        local->need_xattr_heal = 0;
a3470f
+                        ret =  dht_dir_xattr_heal (this, local);
a3470f
+                        if (ret)
a3470f
+                                gf_msg (this->name, GF_LOG_ERROR,
a3470f
+                                        ret,
a3470f
+                                        DHT_MSG_DIR_XATTR_HEAL_FAILED,
a3470f
+                                        "xattr heal failed for "
a3470f
+                                        "directory  %s gfid %s ",
a3470f
+                                        local->loc.path,
a3470f
+                                        local->gfid);
a3470f
+                } else {
a3470f
+                        ret = dht_selfheal_dir_check_set_mdsxattr (frame, loc);
a3470f
+                        if (ret)
a3470f
+                                gf_msg (this->name, GF_LOG_INFO, ret,
a3470f
+                                        DHT_MSG_DIR_XATTR_HEAL_FAILED,
a3470f
+                                        "set mds internal xattr failed for "
a3470f
+                                        "directory  %s gfid %s ", local->loc.path,
a3470f
+                                        local->gfid);
a3470f
+                }
a3470f
+        }
a3470f
+
a3470f
+        if (!gf_uuid_is_null (local->gfid))
a3470f
+                gf_uuid_copy (loc->gfid, local->gfid);
a3470f
+
a3470f
         if (missing_attr == 0) {
a3470f
+                if (!local->heal_layout) {
a3470f
+                        gf_msg_trace (this->name, 0,
a3470f
+                                      "Skip heal layout for %s gfid = %s ",
a3470f
+                                      loc->path, uuid_utoa(loc->gfid));
a3470f
+                        dht_selfheal_dir_finish (frame, this, 0, 1);
a3470f
+                        return 0;
a3470f
+                }
a3470f
                 ret = dht_selfheal_layout_lock (frame, layout, _gf_false,
a3470f
                                                 dht_selfheal_dir_xattr,
a3470f
                                                 dht_should_heal_layout);
a3470f
@@ -1171,11 +1358,9 @@ dht_selfheal_dir_setattr (call_frame_t *frame, loc_t *loc, struct iatt *stbuf,
a3470f
                 return 0;
a3470f
         }
a3470f
 
a3470f
-        if (!gf_uuid_is_null (local->gfid))
a3470f
-                gf_uuid_copy (loc->gfid, local->gfid);
a3470f
-
a3470f
         local->call_cnt = missing_attr;
a3470f
         cnt = layout->cnt;
a3470f
+
a3470f
         for (i = 0; i < cnt; i++) {
a3470f
                 if (layout->list[i].err == -1) {
a3470f
                         gf_msg_trace (this->name, 0,
a3470f
@@ -1291,16 +1476,66 @@ out:
a3470f
         return;
a3470f
 }
a3470f
 
a3470f
+
a3470f
+void
a3470f
+dht_selfheal_dir_mkdir_setquota (dict_t *src, dict_t *dst)
a3470f
+{
a3470f
+        data_t           *quota_limit_key = NULL;
a3470f
+        data_t           *quota_limit_obj_key = NULL;
a3470f
+        xlator_t        *this = NULL;
a3470f
+        int     ret = -1;
a3470f
+
a3470f
+        GF_ASSERT (src);
a3470f
+        GF_ASSERT (dst);
a3470f
+
a3470f
+        this = THIS;
a3470f
+        GF_ASSERT (this);
a3470f
+
a3470f
+        quota_limit_key = dict_get (src, QUOTA_LIMIT_KEY);
a3470f
+        if (!quota_limit_key) {
a3470f
+                gf_msg_debug (this->name, 0,
a3470f
+                              "QUOTA_LIMIT_KEY xattr not present");
a3470f
+                goto cont;
a3470f
+        }
a3470f
+        ret = dict_set(dst, QUOTA_LIMIT_KEY, quota_limit_key);
a3470f
+        if (ret)
a3470f
+                gf_msg (this->name, GF_LOG_WARNING, 0,
a3470f
+                        DHT_MSG_DICT_SET_FAILED,
a3470f
+                        "Failed to set dictionary value.key = %s",
a3470f
+                        QUOTA_LIMIT_KEY);
a3470f
+
a3470f
+cont:
a3470f
+        quota_limit_obj_key = dict_get (src, QUOTA_LIMIT_OBJECTS_KEY);
a3470f
+        if (!quota_limit_obj_key) {
a3470f
+                gf_msg_debug (this->name, 0,
a3470f
+                              "QUOTA_LIMIT_OBJECTS_KEY xattr not present");
a3470f
+                goto out;
a3470f
+        }
a3470f
+        ret = dict_set (dst, QUOTA_LIMIT_OBJECTS_KEY, quota_limit_obj_key);
a3470f
+        if (ret)
a3470f
+                gf_msg (this->name, GF_LOG_WARNING, 0,
a3470f
+                        DHT_MSG_DICT_SET_FAILED,
a3470f
+                        "Failed to set dictionary value.key = %s",
a3470f
+                        QUOTA_LIMIT_OBJECTS_KEY);
a3470f
+
a3470f
+out:
a3470f
+        return;
a3470f
+}
a3470f
+
a3470f
+
a3470f
+
a3470f
+
a3470f
+
a3470f
 int
a3470f
 dht_selfheal_dir_mkdir_lookup_done (call_frame_t *frame, xlator_t *this)
a3470f
 {
a3470f
         dht_local_t  *local = NULL;
a3470f
         int           i     = 0;
a3470f
-        int           ret   = -1;
a3470f
         dict_t       *dict = NULL;
a3470f
         dht_layout_t  *layout = NULL;
a3470f
         loc_t        *loc   = NULL;
a3470f
         int           cnt   = 0;
a3470f
+        int          ret    = -1;
a3470f
 
a3470f
         VALIDATE_OR_GOTO (this->private, err);
a3470f
 
a3470f
@@ -1324,9 +1559,11 @@ dht_selfheal_dir_mkdir_lookup_done (call_frame_t *frame, xlator_t *this)
a3470f
 
a3470f
                 dict = dict_ref (local->params);
a3470f
         }
a3470f
-        /* Set acls */
a3470f
-        if (local->xattr && dict)
a3470f
-                dht_selfheal_dir_mkdir_setacl (local->xattr, dict);
a3470f
+        /* Code to update all extended attributed from local->xattr
a3470f
+           to dict
a3470f
+        */
a3470f
+        dht_dir_set_heal_xattr (this, local, dict, local->xattr, NULL,
a3470f
+                                NULL);
a3470f
 
a3470f
         if (!dict)
a3470f
                 gf_msg (this->name, GF_LOG_WARNING, 0,
a3470f
@@ -1374,8 +1611,13 @@ dht_selfheal_dir_mkdir_lookup_cbk (call_frame_t *frame, void *cookie,
a3470f
         int           this_call_cnt = 0;
a3470f
         int           missing_dirs = 0;
a3470f
         dht_layout_t  *layout = NULL;
a3470f
+        dht_conf_t    *conf   = 0;
a3470f
         loc_t         *loc    = NULL;
a3470f
         xlator_t      *prev    = NULL;
a3470f
+        int           check_mds = 0;
a3470f
+        int           errst     = 0;
a3470f
+        int32_t       mds_xattr_val[1] = {0};
a3470f
+        char          gfid_local[GF_UUID_BUF_SIZE] = {0};
a3470f
 
a3470f
         VALIDATE_OR_GOTO (this->private, err);
a3470f
 
a3470f
@@ -1383,6 +1625,10 @@ dht_selfheal_dir_mkdir_lookup_cbk (call_frame_t *frame, void *cookie,
a3470f
         layout = local->layout;
a3470f
         loc = &local->loc;
a3470f
         prev  = cookie;
a3470f
+        conf = this->private;
a3470f
+
a3470f
+        if (local->gfid)
a3470f
+                gf_uuid_unparse(local->gfid, gfid_local);
a3470f
 
a3470f
         this_call_cnt = dht_frame_return (frame);
a3470f
 
a3470f
@@ -1397,6 +1643,12 @@ dht_selfheal_dir_mkdir_lookup_cbk (call_frame_t *frame, void *cookie,
a3470f
                 if (!op_ret) {
a3470f
                         dht_iatt_merge (this, &local->stbuf, stbuf, prev);
a3470f
                 }
a3470f
+                check_mds = dht_dict_get_array (xattr, conf->mds_xattr_key,
a3470f
+                                                mds_xattr_val, 1, &errst);
a3470f
+                if (dict_get (xattr, conf->mds_xattr_key) && check_mds && !errst) {
a3470f
+                        dict_unref (local->xattr);
a3470f
+                        local->xattr = dict_ref (xattr);
a3470f
+                }
a3470f
 
a3470f
         }
a3470f
         UNLOCK (&frame->lock);
a3470f
@@ -1445,13 +1697,16 @@ dht_selfheal_dir_mkdir_lock_cbk (call_frame_t *frame, void *cookie,
a3470f
         dht_local_t  *local = NULL;
a3470f
         dht_conf_t   *conf  = NULL;
a3470f
         int           i     = 0;
a3470f
+        int           ret   = -1;
a3470f
+        xlator_t     *mds_subvol = NULL;
a3470f
 
a3470f
         VALIDATE_OR_GOTO (this->private, err);
a3470f
 
a3470f
         conf = this->private;
a3470f
         local = frame->local;
a3470f
+        mds_subvol = local->mds_subvol;
a3470f
 
a3470f
-	    local->call_cnt = conf->subvolume_cnt;
a3470f
+        local->call_cnt = conf->subvolume_cnt;
a3470f
 
a3470f
         if (op_ret < 0) {
a3470f
 
a3470f
@@ -1477,12 +1732,32 @@ dht_selfheal_dir_mkdir_lock_cbk (call_frame_t *frame, void *cookie,
a3470f
         /* After getting locks, perform lookup again to ensure that the
a3470f
            directory was not deleted by a racing rmdir
a3470f
         */
a3470f
+        if (!local->xattr_req)
a3470f
+                local->xattr_req = dict_new ();
a3470f
+
a3470f
+        ret = dict_set_int32 (local->xattr_req, "list-xattr", 1);
a3470f
+        if (ret)
a3470f
+                gf_msg (this->name, GF_LOG_ERROR, 0,
a3470f
+                        DHT_MSG_DICT_SET_FAILED,
a3470f
+                        "Failed to set dictionary key list-xattr value "
a3470f
+                        " for path %s ", local->loc.path);
a3470f
 
a3470f
         for (i = 0; i < conf->subvolume_cnt; i++) {
a3470f
-                STACK_WIND_COOKIE (frame, dht_selfheal_dir_mkdir_lookup_cbk,
a3470f
-                                   conf->subvolumes[i], conf->subvolumes[i],
a3470f
-                                   conf->subvolumes[i]->fops->lookup,
a3470f
-                                   &local->loc, NULL);
a3470f
+                if (mds_subvol && conf->subvolumes[i] == mds_subvol) {
a3470f
+                        STACK_WIND_COOKIE (frame,
a3470f
+                                           dht_selfheal_dir_mkdir_lookup_cbk,
a3470f
+                                           conf->subvolumes[i],
a3470f
+                                           conf->subvolumes[i],
a3470f
+                                           conf->subvolumes[i]->fops->lookup,
a3470f
+                                           &local->loc, local->xattr_req);
a3470f
+                } else {
a3470f
+                       STACK_WIND_COOKIE (frame,
a3470f
+                                          dht_selfheal_dir_mkdir_lookup_cbk,
a3470f
+                                          conf->subvolumes[i],
a3470f
+                                          conf->subvolumes[i],
a3470f
+                                          conf->subvolumes[i]->fops->lookup,
a3470f
+                                          &local->loc, NULL);
a3470f
+                }
a3470f
         }
a3470f
 
a3470f
         return 0;
a3470f
@@ -2171,15 +2446,16 @@ dht_selfheal_directory (call_frame_t *frame, dht_selfheal_dir_cbk_t dir_cbk,
a3470f
         }
a3470f
 
a3470f
         dht_layout_sort_volname (layout);
a3470f
+        local->heal_layout = _gf_true;
a3470f
         ret = dht_selfheal_dir_getafix (frame, loc, layout);
a3470f
 
a3470f
         if (ret == -1) {
a3470f
-                gf_msg (this->name, GF_LOG_WARNING, 0,
a3470f
+                gf_msg (this->name, GF_LOG_INFO, 0,
a3470f
                         DHT_MSG_DIR_SELFHEAL_FAILED,
a3470f
                         "Directory selfheal failed: "
a3470f
                         "Unable to form layout for directory %s",
a3470f
                         loc->path);
a3470f
-                goto sorry_no_fix;
a3470f
+                local->heal_layout = _gf_false;
a3470f
         }
a3470f
 
a3470f
         dht_selfheal_dir_mkdir (frame, loc, layout, 0);
a3470f
@@ -2281,23 +2557,196 @@ dht_selfheal_restore (call_frame_t *frame, dht_selfheal_dir_cbk_t dir_cbk,
a3470f
 }
a3470f
 
a3470f
 int
a3470f
+dht_dir_heal_xattrs (void *data)
a3470f
+{
a3470f
+        call_frame_t    *frame          = NULL;
a3470f
+        dht_local_t     *local          = NULL;
a3470f
+        xlator_t        *subvol         = NULL;
a3470f
+        xlator_t        *mds_subvol     = NULL;
a3470f
+        xlator_t        *this           = NULL;
a3470f
+        dht_conf_t      *conf           = NULL;
a3470f
+        dict_t          *user_xattr     = NULL;
a3470f
+        dict_t          *internal_xattr = NULL;
a3470f
+        dict_t          *mds_xattr   = NULL;
a3470f
+        dict_t          *xdata          = NULL;
a3470f
+        int              call_cnt       = 0;
a3470f
+        int              ret            = -1;
a3470f
+        int              uret           = 0;
a3470f
+        int              uflag          = 0;
a3470f
+        int              i              = 0;
a3470f
+        int              xattr_hashed   = 0;
a3470f
+        char     gfid[GF_UUID_BUF_SIZE] = {0};
a3470f
+        int32_t    allzero[1]           = {0};
a3470f
+
a3470f
+        GF_VALIDATE_OR_GOTO ("dht", data, out);
a3470f
+
a3470f
+        frame = data;
a3470f
+        local = frame->local;
a3470f
+        this = frame->this;
a3470f
+        GF_VALIDATE_OR_GOTO ("dht", this, out);
a3470f
+        GF_VALIDATE_OR_GOTO (this->name, local, out);
a3470f
+        mds_subvol = local->mds_subvol;
a3470f
+        conf = this->private;
a3470f
+        GF_VALIDATE_OR_GOTO (this->name, conf, out);
a3470f
+        gf_uuid_unparse(local->loc.gfid, gfid);
a3470f
+
a3470f
+        if (!mds_subvol) {
a3470f
+                gf_msg (this->name, GF_LOG_WARNING, 0,
a3470f
+                        DHT_MSG_DIR_XATTR_HEAL_FAILED,
a3470f
+                        "No mds subvol for %s gfid = %s",
a3470f
+                        local->loc.path, gfid);
a3470f
+                goto out;
a3470f
+        }
a3470f
+
a3470f
+        if ((local->loc.inode && gf_uuid_is_null (local->loc.inode->gfid)) ||
a3470f
+            gf_uuid_is_null (local->loc.gfid)) {
a3470f
+                gf_msg (this->name, GF_LOG_WARNING, 0,
a3470f
+                        DHT_MSG_DIR_XATTR_HEAL_FAILED,
a3470f
+                        "No gfid present so skip heal for path %s gfid = %s",
a3470f
+                        local->loc.path, gfid);
a3470f
+                goto out;
a3470f
+        }
a3470f
+
a3470f
+        internal_xattr = dict_new ();
a3470f
+        if (!internal_xattr) {
a3470f
+                gf_msg (this->name, GF_LOG_ERROR, DHT_MSG_NO_MEMORY, 0,
a3470f
+                        "dictionary creation failed");
a3470f
+                goto out;
a3470f
+        }
a3470f
+        xdata = dict_new ();
a3470f
+        if (!xdata) {
a3470f
+                gf_msg (this->name, GF_LOG_ERROR, DHT_MSG_NO_MEMORY, 0,
a3470f
+                        "dictionary creation failed");
a3470f
+                goto out;
a3470f
+        }
a3470f
+
a3470f
+        call_cnt = conf->subvolume_cnt;
a3470f
+
a3470f
+        user_xattr = dict_new ();
a3470f
+        if (!user_xattr) {
a3470f
+                gf_msg (this->name, GF_LOG_ERROR, DHT_MSG_NO_MEMORY, 0,
a3470f
+                        "dictionary creation failed");
a3470f
+                goto out;
a3470f
+        }
a3470f
+
a3470f
+        ret = syncop_listxattr (local->mds_subvol, &local->loc,
a3470f
+                                &mds_xattr, NULL, NULL);
a3470f
+        if (ret < 0) {
a3470f
+                gf_msg (this->name, GF_LOG_ERROR, -ret,
a3470f
+                        DHT_MSG_DIR_XATTR_HEAL_FAILED,
a3470f
+                        "failed to list xattrs for "
a3470f
+                        "%s: on %s ",
a3470f
+                        local->loc.path, local->mds_subvol->name);
a3470f
+        }
a3470f
+
a3470f
+        if (!mds_xattr)
a3470f
+                goto out;
a3470f
+
a3470f
+        dht_dir_set_heal_xattr (this, local, user_xattr, mds_xattr,
a3470f
+                                &uret, &uflag);
a3470f
+
a3470f
+        /* To set quota related xattr need to set GLUSTERFS_INTERNAL_FOP_KEY
a3470f
+         * key value to 1
a3470f
+         */
a3470f
+        if (dict_get (user_xattr, QUOTA_LIMIT_KEY) ||
a3470f
+            dict_get (user_xattr, QUOTA_LIMIT_OBJECTS_KEY)) {
a3470f
+                ret = dict_set_int32 (xdata, GLUSTERFS_INTERNAL_FOP_KEY, 1);
a3470f
+                if (ret) {
a3470f
+                        gf_msg (this->name, GF_LOG_ERROR, 0,
a3470f
+                                DHT_MSG_DICT_SET_FAILED,
a3470f
+                                "Failed to set dictionary value: key = %s,"
a3470f
+                                " path = %s", GLUSTERFS_INTERNAL_FOP_KEY,
a3470f
+                                 local->loc.path);
a3470f
+                        goto out;
a3470f
+                }
a3470f
+        }
a3470f
+        if (uret <= 0 && !uflag)
a3470f
+                goto out;
a3470f
+
a3470f
+        for (i = 0; i < call_cnt; i++) {
a3470f
+                subvol = conf->subvolumes[i];
a3470f
+                if (subvol == mds_subvol)
a3470f
+                        continue;
a3470f
+                if (uret || uflag) {
a3470f
+                        ret = syncop_setxattr (subvol, &local->loc, user_xattr,
a3470f
+                                               0, xdata, NULL);
a3470f
+                        if (ret) {
a3470f
+                                xattr_hashed = 1;
a3470f
+                                gf_msg (this->name, GF_LOG_ERROR, -ret,
a3470f
+                                        DHT_MSG_DIR_XATTR_HEAL_FAILED,
a3470f
+                                        "Directory xattr heal failed. Failed to set"
a3470f
+                                        "user xattr on path %s on "
a3470f
+                                        "subvol %s, gfid = %s ",
a3470f
+                                        local->loc.path, subvol->name, gfid);
a3470f
+                        }
a3470f
+                }
a3470f
+        }
a3470f
+        /* After heal all custom xattr reset internal MDS xattr to 0 */
a3470f
+        if (!xattr_hashed) {
a3470f
+                ret = dht_dict_set_array (internal_xattr,
a3470f
+                                          conf->mds_xattr_key,
a3470f
+                                          allzero, 1);
a3470f
+                if (ret) {
a3470f
+                        gf_msg (this->name, GF_LOG_WARNING, ENOMEM,
a3470f
+                                DHT_MSG_DICT_SET_FAILED,
a3470f
+                                "Failed to set dictionary value:key = %s for "
a3470f
+                                "path %s", conf->mds_xattr_key,
a3470f
+                                local->loc.path);
a3470f
+                        goto out;
a3470f
+                }
a3470f
+                ret = syncop_setxattr (mds_subvol, &local->loc, internal_xattr,
a3470f
+                                       0, NULL, NULL);
a3470f
+                if (ret) {
a3470f
+                        gf_msg (this->name, GF_LOG_ERROR, -ret,
a3470f
+                                DHT_MSG_DIR_XATTR_HEAL_FAILED,
a3470f
+                                "Failed to reset internal xattr "
a3470f
+                                "on path %s on subvol %s"
a3470f
+                                "gfid = %s ", local->loc.path,
a3470f
+                                mds_subvol->name, gfid);
a3470f
+                }
a3470f
+        }
a3470f
+
a3470f
+out:
a3470f
+        if (user_xattr)
a3470f
+                dict_unref (user_xattr);
a3470f
+        if (mds_xattr)
a3470f
+                dict_unref (mds_xattr);
a3470f
+        if (internal_xattr)
a3470f
+                dict_unref (internal_xattr);
a3470f
+        if (xdata)
a3470f
+                dict_unref (xdata);
a3470f
+        return 0;
a3470f
+}
a3470f
+
a3470f
+
a3470f
+int
a3470f
+dht_dir_heal_xattrs_done (int ret, call_frame_t *sync_frame, void *data)
a3470f
+{
a3470f
+        DHT_STACK_DESTROY (sync_frame);
a3470f
+        return 0;
a3470f
+}
a3470f
+
a3470f
+
a3470f
+int
a3470f
 dht_dir_attr_heal (void *data)
a3470f
 {
a3470f
-        call_frame_t    *frame = NULL;
a3470f
-        dht_local_t     *local = NULL;
a3470f
-        xlator_t        *subvol = NULL;
a3470f
-        xlator_t        *this  = NULL;
a3470f
+        call_frame_t    *frame      = NULL;
a3470f
+        dht_local_t     *local      = NULL;
a3470f
+        xlator_t        *subvol     = NULL;
a3470f
+        xlator_t        *mds_subvol = NULL;
a3470f
+        xlator_t        *this       = NULL;
a3470f
         dht_conf_t      *conf  = NULL;
a3470f
         int              call_cnt = 0;
a3470f
         int              ret   = -1;
a3470f
         int              i     = 0;
a3470f
-        char         gfid[GF_UUID_BUF_SIZE] = {0};
a3470f
+        char             gfid[GF_UUID_BUF_SIZE] = {0};
a3470f
 
a3470f
 
a3470f
         GF_VALIDATE_OR_GOTO ("dht", data, out);
a3470f
 
a3470f
         frame = data;
a3470f
         local = frame->local;
a3470f
+        mds_subvol = local->mds_subvol;
a3470f
         this = frame->this;
a3470f
         GF_VALIDATE_OR_GOTO ("dht", this, out);
a3470f
         GF_VALIDATE_OR_GOTO ("dht", local, out);
a3470f
@@ -2306,17 +2755,39 @@ dht_dir_attr_heal (void *data)
a3470f
 
a3470f
         call_cnt = conf->subvolume_cnt;
a3470f
 
a3470f
+        if (!__is_root_gfid (local->stbuf.ia_gfid) && (!mds_subvol)) {
a3470f
+                gf_msg (this->name, GF_LOG_WARNING, 0,
a3470f
+                        DHT_MSG_DIR_ATTR_HEAL_FAILED,
a3470f
+                        "No mds subvol for %s gfid = %s",
a3470f
+                        local->loc.path, gfid);
a3470f
+                goto out;
a3470f
+        }
a3470f
+
a3470f
+        if (!__is_root_gfid (local->stbuf.ia_gfid)) {
a3470f
+                for (i = 0; i < conf->subvolume_cnt; i++) {
a3470f
+                        if (conf->subvolumes[i] ==  mds_subvol) {
a3470f
+                                if (!conf->subvolume_status[i]) {
a3470f
+                                        gf_msg (this->name, GF_LOG_ERROR,
a3470f
+                                                0,  DHT_MSG_HASHED_SUBVOL_DOWN,
a3470f
+                                                "mds subvol is down for path "
a3470f
+                                                " %s gfid is %s Unable to set xattr " ,
a3470f
+                                                local->loc.path, gfid);
a3470f
+                                        goto out;
a3470f
+                                }
a3470f
+                        }
a3470f
+                }
a3470f
+        }
a3470f
+
a3470f
         for (i = 0; i < call_cnt; i++) {
a3470f
                 subvol = conf->subvolumes[i];
a3470f
-                if (!subvol)
a3470f
+                if (!subvol || subvol == mds_subvol)
a3470f
                         continue;
a3470f
-
a3470f
                 if (__is_root_gfid (local->stbuf.ia_gfid)) {
a3470f
                         ret = syncop_setattr (subvol, &local->loc, &local->stbuf,
a3470f
                                               (GF_SET_ATTR_UID | GF_SET_ATTR_GID | GF_SET_ATTR_MODE),
a3470f
                                               NULL, NULL, NULL, NULL);
a3470f
                 } else {
a3470f
-                        ret = syncop_setattr (subvol, &local->loc, &local->stbuf,
a3470f
+                        ret = syncop_setattr (subvol, &local->loc, &local->mds_stbuf,
a3470f
                                               (GF_SET_ATTR_UID | GF_SET_ATTR_GID),
a3470f
                                               NULL, NULL, NULL, NULL);
a3470f
                 }
a3470f
@@ -2324,7 +2795,7 @@ dht_dir_attr_heal (void *data)
a3470f
                 if (ret) {
a3470f
                         gf_uuid_unparse(local->loc.gfid, gfid);
a3470f
 
a3470f
-                        gf_msg ("dht", GF_LOG_ERROR, -ret,
a3470f
+                        gf_msg (this->name, GF_LOG_ERROR, -ret,
a3470f
                                 DHT_MSG_DIR_ATTR_HEAL_FAILED,
a3470f
                                 "Directory attr heal failed. Failed to set"
a3470f
                                 " uid/gid on path %s on subvol %s, gfid = %s ",
a3470f
diff --git a/xlators/cluster/dht/src/dht-shared.c b/xlators/cluster/dht/src/dht-shared.c
a3470f
index 0373ebf..42daff0 100644
a3470f
--- a/xlators/cluster/dht/src/dht-shared.c
a3470f
+++ b/xlators/cluster/dht/src/dht-shared.c
a3470f
@@ -868,6 +868,7 @@ dht_init (xlator_t *this)
a3470f
         }
a3470f
 
a3470f
         GF_OPTION_INIT ("xattr-name", conf->xattr_name, str, err);
a3470f
+        gf_asprintf (&conf->mds_xattr_key, "%s."DHT_MDS_STR, conf->xattr_name);
a3470f
         gf_asprintf (&conf->link_xattr_name, "%s."DHT_LINKFILE_STR,
a3470f
                      conf->xattr_name);
a3470f
         gf_asprintf (&conf->commithash_xattr_name, "%s."DHT_COMMITHASH_STR,
a3470f
@@ -917,6 +918,7 @@ err:
a3470f
                 GF_FREE (conf->xattr_name);
a3470f
                 GF_FREE (conf->link_xattr_name);
a3470f
                 GF_FREE (conf->wild_xattr_name);
a3470f
+                GF_FREE (conf->mds_xattr_key);
a3470f
 
a3470f
                 if (conf->lock_pool)
a3470f
                         mem_pool_destroy (conf->lock_pool);
a3470f
-- 
a3470f
1.8.3.1
a3470f