e7a346
From 8b596eacd72527b55ccff6a26a44014b6cf76b48 Mon Sep 17 00:00:00 2001
e7a346
From: Sunil Kumar Acharya <sheggodu@redhat.com>
e7a346
Date: Wed, 14 Jun 2017 16:28:40 +0530
e7a346
Subject: [PATCH 088/128] cluster/ec: Implement DISCARD FOP for EC
e7a346
e7a346
Updates #254
e7a346
e7a346
This code change implements DISCARD FOP support for
e7a346
EC.
e7a346
e7a346
>BUG: 1461018
e7a346
>Change-Id: I09a9cb2aa9d91ec27add4f422dc9074af5b8b2db
e7a346
>Signed-off-by: Sunil Kumar Acharya <sheggodu@redhat.com>
e7a346
e7a346
Upstream Patch: https://review.gluster.org/#/c/17777/
e7a346
e7a346
BUG: 1499865
e7a346
Change-Id: I09a9cb2aa9d91ec27add4f422dc9074af5b8b2db
e7a346
Signed-off-by: Sunil Kumar Acharya <sheggodu@redhat.com>
e7a346
Reviewed-on: https://code.engineering.redhat.com/gerrit/123694
e7a346
Tested-by: RHGS Build Bot <nigelb@redhat.com>
e7a346
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
e7a346
---
e7a346
 tests/basic/ec/ec-discard.t             | 197 +++++++++++++++++
e7a346
 tests/include.rc                        |   7 +
e7a346
 xlators/cluster/ec/src/ec-common.h      |   3 +-
e7a346
 xlators/cluster/ec/src/ec-fops.h        |   4 +
e7a346
 xlators/cluster/ec/src/ec-helpers.h     |   5 +-
e7a346
 xlators/cluster/ec/src/ec-inode-write.c | 365 ++++++++++++++++++++++++++++----
e7a346
 xlators/cluster/ec/src/ec.c             |   3 +-
e7a346
 7 files changed, 536 insertions(+), 48 deletions(-)
e7a346
 create mode 100644 tests/basic/ec/ec-discard.t
e7a346
e7a346
diff --git a/tests/basic/ec/ec-discard.t b/tests/basic/ec/ec-discard.t
e7a346
new file mode 100644
e7a346
index 0000000..4a44cec
e7a346
--- /dev/null
e7a346
+++ b/tests/basic/ec/ec-discard.t
e7a346
@@ -0,0 +1,197 @@
e7a346
+#!/bin/bash
e7a346
+#
e7a346
+# Test discard functionality
e7a346
+#
e7a346
+# Test that basic discard (hole punch) functionality works via the fallocate
e7a346
+# command line tool. Hole punch deallocates a region of a file, creating a hole
e7a346
+# and a zero-filled data region. We verify that hole punch works, frees blocks
e7a346
+# and that subsequent reads do not read stale data (caches are invalidated).
e7a346
+#
e7a346
+# NOTE: fuse fallocate is known to be broken with regard to cache invalidation
e7a346
+# 	up to 3.9.0 kernels. Therefore, FOPEN_KEEP_CACHE is not used in this
e7a346
+#	test (opens will invalidate the fuse cache).
e7a346
+###
e7a346
+
e7a346
+. $(dirname $0)/../../include.rc
e7a346
+. $(dirname $0)/../../fallocate.rc
e7a346
+. $(dirname $0)/../../volume.rc
e7a346
+
e7a346
+cleanup
e7a346
+
e7a346
+#cleate and start volume
e7a346
+TEST glusterd
e7a346
+TEST pidof glusterd
e7a346
+TEST $CLI volume create $V0 disperse 6 redundancy 2 $H0:$B0/${V0}{0..5}
e7a346
+TEST $CLI volume set $V0 disperse.optimistic-change-log on
e7a346
+TEST $CLI volume start $V0
e7a346
+
e7a346
+#Mount the volume
e7a346
+TEST $GFS --volfile-id=/$V0 --volfile-server=$H0 $M0;
e7a346
+EXPECT_WITHIN $CHILD_UP_TIMEOUT "6" ec_child_up_count $V0 0
e7a346
+
e7a346
+#Check for fallocate and hole punch support
e7a346
+require_fallocate -l 1m $M0/file
e7a346
+require_fallocate -p -l 512k $M0/file && rm -f $M0/file
e7a346
+
e7a346
+#Write some data, punch a hole and verify the file content changes
e7a346
+TEST dd if=/dev/urandom of=$M0/file bs=1024k count=1
e7a346
+TEST cp $M0/file $M0/file.copy.pre
e7a346
+TEST fallocate -p -o 512k -l 128k $M0/file
e7a346
+TEST ! cmp $M0/file.copy.pre $M0/file
e7a346
+TEST rm -f $M0/file $M0/file.copy.pre
e7a346
+
e7a346
+#Allocate some blocks, punch a hole and verify block allocation
e7a346
+TEST fallocate -l 1m $M0/file
e7a346
+blksz=`stat -c %B $M0/file`
e7a346
+nblks=`stat -c %b $M0/file`
e7a346
+TEST [ $(($blksz * $nblks)) -ge 1048576 ]
e7a346
+TEST fallocate -p -o 512k -l 128k $M0/file
e7a346
+nblks=`stat -c %b $M0/file`
e7a346
+TEST [ $(($blksz * $nblks)) -lt $((933889)) ]
e7a346
+TEST unlink $M0/file
e7a346
+
e7a346
+###Punch hole test cases without fallocate
e7a346
+##With write
e7a346
+#Touching starting boundary
e7a346
+TEST dd if=/dev/urandom of=$B0/test_file bs=1024 count=8
e7a346
+TEST cp $B0/test_file $M0/test_file
e7a346
+TEST fallocate -p -o 0 -l 500 $B0/test_file
e7a346
+TEST fallocate -p -o 0 -l 500 $M0/test_file
e7a346
+TEST md5_sum=`get_md5_sum $B0/test_file`
e7a346
+EXPECT $md5_sum get_md5_sum $M0/test_file
e7a346
+TEST rm -f $B0/test_file $M0/test_file
e7a346
+
e7a346
+#Touching boundary
e7a346
+TEST dd if=/dev/urandom of=$B0/test_file bs=1024 count=8
e7a346
+TEST cp $B0/test_file $M0/test_file
e7a346
+TEST fallocate -p -o 500 -l 1548 $B0/test_file
e7a346
+TEST fallocate -p -o 500 -l 1548 $M0/test_file
e7a346
+TEST md5_sum=`get_md5_sum $B0/test_file`
e7a346
+EXPECT $md5_sum get_md5_sum $M0/test_file
e7a346
+TEST rm -f $B0/test_file $M0/test_file
e7a346
+
e7a346
+#Not touching boundary
e7a346
+TEST dd if=/dev/urandom of=$B0/test_file bs=1024 count=8
e7a346
+TEST cp $B0/test_file $M0/test_file
e7a346
+TEST fallocate -p -o 500 -l 1000 $B0/test_file
e7a346
+TEST fallocate -p -o 500 -l 1000 $M0/test_file
e7a346
+TEST md5_sum=`get_md5_sum $B0/test_file`
e7a346
+EXPECT $md5_sum get_md5_sum $M0/test_file
e7a346
+TEST rm -f $B0/test_file $M0/test_file
e7a346
+
e7a346
+#Over boundary
e7a346
+TEST dd if=/dev/urandom of=$B0/test_file bs=1024 count=8
e7a346
+TEST cp $B0/test_file $M0/test_file
e7a346
+TEST fallocate -p -o 1500 -l 1000 $B0/test_file
e7a346
+TEST fallocate -p -o 1500 -l 1000 $M0/test_file
e7a346
+TEST md5_sum=`get_md5_sum $B0/test_file`
e7a346
+EXPECT $md5_sum get_md5_sum $M0/test_file
e7a346
+TEST rm -f $B0/test_file $M0/test_file
e7a346
+
e7a346
+###Punch hole test cases with fallocate
e7a346
+##Without write
e7a346
+
e7a346
+#Zero size
e7a346
+TEST dd if=/dev/urandom of=$M0/test_file bs=1024 count=8
e7a346
+TEST ! fallocate -p -o 1500 -l 0 $M0/test_file
e7a346
+
e7a346
+#Negative size
e7a346
+TEST ! fallocate -p -o 1500 -l -100 $M0/test_file
e7a346
+TEST rm -f $M0/test_file
e7a346
+
e7a346
+#Touching boundary
e7a346
+TEST dd if=/dev/urandom of=$B0/test_file bs=1024 count=8
e7a346
+TEST cp $B0/test_file $M0/test_file
e7a346
+TEST fallocate -p -o 2048 -l 2048 $B0/test_file
e7a346
+TEST fallocate -p -o 2048 -l 2048 $M0/test_file
e7a346
+TEST md5_sum=`get_md5_sum $B0/test_file`
e7a346
+EXPECT $md5_sum get_md5_sum $M0/test_file
e7a346
+TEST rm -f $B0/test_file $M0/test_file
e7a346
+
e7a346
+#Touching boundary,multiple stripe
e7a346
+TEST dd if=/dev/urandom of=$B0/test_file bs=1024 count=8
e7a346
+TEST cp $B0/test_file $M0/test_file
e7a346
+TEST fallocate -p -o 2048 -l 4096 $B0/test_file
e7a346
+TEST fallocate -p -o 2048 -l 4096 $M0/test_file
e7a346
+TEST md5_sum=`get_md5_sum $B0/test_file`
e7a346
+EXPECT $md5_sum get_md5_sum $M0/test_file
e7a346
+TEST rm -f $B0/test_file $M0/test_file
e7a346
+
e7a346
+##With write
e7a346
+
e7a346
+#Size ends in boundary
e7a346
+TEST dd if=/dev/urandom of=$B0/test_file bs=1024 count=8
e7a346
+TEST cp $B0/test_file $M0/test_file
e7a346
+TEST fallocate -p -o 600 -l 3496 $B0/test_file
e7a346
+TEST fallocate -p -o 600 -l 3496 $M0/test_file
e7a346
+TEST md5_sum=`get_md5_sum $B0/test_file`
e7a346
+EXPECT $md5_sum get_md5_sum $M0/test_file
e7a346
+TEST rm -f $B0/test_file $M0/test_file
e7a346
+
e7a346
+#Offset at boundary
e7a346
+TEST dd if=/dev/urandom of=$B0/test_file bs=1024 count=8
e7a346
+TEST cp $B0/test_file $M0/test_file
e7a346
+TEST fallocate -p -o 2048 -l 3072 $B0/test_file
e7a346
+TEST fallocate -p -o 2048 -l 3072 $M0/test_file
e7a346
+TEST md5_sum=`get_md5_sum $B0/test_file`
e7a346
+EXPECT $md5_sum get_md5_sum $M0/test_file
e7a346
+TEST rm -f $B0/test_file $M0/test_file
e7a346
+
e7a346
+#Offset and Size not at boundary
e7a346
+TEST dd if=/dev/urandom of=$B0/test_file bs=1024 count=8
e7a346
+TEST cp $B0/test_file $M0/test_file
e7a346
+TEST fallocate -p -o 1000 -l 3072 $B0/test_file
e7a346
+TEST fallocate -p -o 1000 -l 3072 $M0/test_file
e7a346
+TEST md5_sum=`get_md5_sum $B0/test_file`
e7a346
+EXPECT $md5_sum get_md5_sum $M0/test_file
e7a346
+#TEST rm -f $B0/test_file $M0/test_file
e7a346
+
e7a346
+#Data Corruption Tests
e7a346
+#Kill brick1 and brick2
e7a346
+TEST kill_brick $V0 $H0 $B0/${V0}0
e7a346
+TEST kill_brick $V0 $H0 $B0/${V0}1
e7a346
+EXPECT_WITHIN $CHILD_UP_TIMEOUT "4" ec_child_up_count $V0 0
e7a346
+
e7a346
+#Unmount and mount
e7a346
+EXPECT_WITHIN $UMOUNT_TIMEOUT "Y" force_umount $M0;
e7a346
+TEST $GFS --volfile-id=/$V0 --volfile-server=$H0 $M0;
e7a346
+EXPECT_WITHIN $CHILD_UP_TIMEOUT "4" ec_child_up_count $V0 0
e7a346
+
e7a346
+#verify md5 sum
e7a346
+EXPECT $md5_sum get_md5_sum $M0/test_file
e7a346
+
e7a346
+#Bring up the bricks
e7a346
+TEST $CLI volume start $V0 force
e7a346
+EXPECT_WITHIN $CHILD_UP_TIMEOUT "6" ec_child_up_count $V0 0
e7a346
+
e7a346
+#Kill brick3 and brick4
e7a346
+TEST kill_brick $V0 $H0 $B0/${V0}2
e7a346
+TEST kill_brick $V0 $H0 $B0/${V0}3
e7a346
+EXPECT_WITHIN $CHILD_UP_TIMEOUT "4" ec_child_up_count $V0 0
e7a346
+
e7a346
+#Unmount and mount
e7a346
+EXPECT_WITHIN $UMOUNT_TIMEOUT "Y" force_umount $M0;
e7a346
+TEST $GFS --volfile-id=/$V0 --volfile-server=$H0 $M0;
e7a346
+EXPECT_WITHIN $CHILD_UP_TIMEOUT "4" ec_child_up_count $V0 0
e7a346
+
e7a346
+#verify md5 sum
e7a346
+EXPECT $md5_sum get_md5_sum $M0/test_file
e7a346
+
e7a346
+#Bring up the bricks
e7a346
+TEST $CLI volume start $V0 force
e7a346
+EXPECT_WITHIN $CHILD_UP_TIMEOUT "6" ec_child_up_count $V0 0
e7a346
+
e7a346
+#Kill brick5 and brick6
e7a346
+TEST kill_brick $V0 $H0 $B0/${V0}4
e7a346
+TEST kill_brick $V0 $H0 $B0/${V0}5
e7a346
+EXPECT_WITHIN $CHILD_UP_TIMEOUT "4" ec_child_up_count $V0 0
e7a346
+
e7a346
+#Unmount and mount
e7a346
+EXPECT_WITHIN $UMOUNT_TIMEOUT "Y" force_umount $M0;
e7a346
+TEST $GFS --volfile-id=/$V0 --volfile-server=$H0 $M0;
e7a346
+EXPECT_WITHIN $CHILD_UP_TIMEOUT "4" ec_child_up_count $V0 0
e7a346
+
e7a346
+#verify md5 sum
e7a346
+EXPECT $md5_sum get_md5_sum $M0/test_file
e7a346
+
e7a346
+cleanup
e7a346
diff --git a/tests/include.rc b/tests/include.rc
e7a346
index 7470ea1..45392e0 100644
e7a346
--- a/tests/include.rc
e7a346
+++ b/tests/include.rc
e7a346
@@ -1229,3 +1229,10 @@ function STAT_INO()
e7a346
                 echo 0
e7a346
         fi
e7a346
 }
e7a346
+
e7a346
+function get_md5_sum()
e7a346
+{
e7a346
+    local file=$1;
e7a346
+    md5_sum=$(md5sum $file | awk '{print $1}');
e7a346
+    echo $md5_sum
e7a346
+}
e7a346
diff --git a/xlators/cluster/ec/src/ec-common.h b/xlators/cluster/ec/src/ec-common.h
e7a346
index 1a947cc..0f7a252 100644
e7a346
--- a/xlators/cluster/ec/src/ec-common.h
e7a346
+++ b/xlators/cluster/ec/src/ec-common.h
e7a346
@@ -85,6 +85,8 @@ void ec_update_good(ec_fop_data_t *fop, uintptr_t good);
e7a346
 
e7a346
 void ec_fop_set_error(ec_fop_data_t *fop, int32_t error);
e7a346
 
e7a346
+void __ec_fop_set_error(ec_fop_data_t *fop, int32_t error);
e7a346
+
e7a346
 ec_cbk_data_t *
e7a346
 ec_fop_prepare_answer(ec_fop_data_t *fop, gf_boolean_t ro);
e7a346
 
e7a346
@@ -133,5 +135,4 @@ ec_heal_inspect (call_frame_t *frame, ec_t *ec,
e7a346
                  gf_boolean_t *need_heal);
e7a346
 int32_t
e7a346
 ec_get_heal_info (xlator_t *this, loc_t *loc, dict_t **dict);
e7a346
-
e7a346
 #endif /* __EC_COMMON_H__ */
e7a346
diff --git a/xlators/cluster/ec/src/ec-fops.h b/xlators/cluster/ec/src/ec-fops.h
e7a346
index fab22d8..4a926cf 100644
e7a346
--- a/xlators/cluster/ec/src/ec-fops.h
e7a346
+++ b/xlators/cluster/ec/src/ec-fops.h
e7a346
@@ -172,6 +172,10 @@ void ec_fallocate(call_frame_t *frame, xlator_t *this, uintptr_t target,
e7a346
               int32_t minimum, fop_fallocate_cbk_t func, void *data, fd_t *fd,
e7a346
               int32_t mode, off_t offset, size_t len, dict_t *xdata);
e7a346
 
e7a346
+void ec_discard(call_frame_t *frame, xlator_t *this, uintptr_t target,
e7a346
+                int32_t minimum, fop_discard_cbk_t func, void *data, fd_t *fd,
e7a346
+                off_t offset, size_t len, dict_t *xdata);
e7a346
+
e7a346
 void ec_truncate(call_frame_t * frame, xlator_t * this, uintptr_t target,
e7a346
                  int32_t minimum, fop_truncate_cbk_t func, void *data,
e7a346
                  loc_t * loc, off_t offset, dict_t * xdata);
e7a346
diff --git a/xlators/cluster/ec/src/ec-helpers.h b/xlators/cluster/ec/src/ec-helpers.h
e7a346
index cfd7daa..a8f153a 100644
e7a346
--- a/xlators/cluster/ec/src/ec-helpers.h
e7a346
+++ b/xlators/cluster/ec/src/ec-helpers.h
e7a346
@@ -178,8 +178,5 @@ ec_is_data_fop (glusterfs_fop_t fop);
e7a346
 
e7a346
 int32_t
e7a346
 ec_launch_replace_heal (ec_t *ec);
e7a346
-/*
e7a346
-gf_boolean_t
e7a346
-ec_is_metadata_fop (glusterfs_fop_t fop);
e7a346
-*/
e7a346
+
e7a346
 #endif /* __EC_HELPERS_H__ */
e7a346
diff --git a/xlators/cluster/ec/src/ec-inode-write.c b/xlators/cluster/ec/src/ec-inode-write.c
e7a346
index e6a67cf..ae51202 100644
e7a346
--- a/xlators/cluster/ec/src/ec-inode-write.c
e7a346
+++ b/xlators/cluster/ec/src/ec-inode-write.c
e7a346
@@ -19,6 +19,97 @@
e7a346
 #include "ec-method.h"
e7a346
 #include "ec-fops.h"
e7a346
 
e7a346
+int32_t
e7a346
+ec_update_writev_cbk (call_frame_t *frame, void *cookie,
e7a346
+                      xlator_t *this, int32_t op_ret, int32_t op_errno,
e7a346
+                      struct iatt *prebuf, struct iatt *postbuf,
e7a346
+                      dict_t *xdata)
e7a346
+{
e7a346
+    ec_fop_data_t *fop    = cookie;
e7a346
+    ec_cbk_data_t *cbk    = NULL;
e7a346
+    ec_fop_data_t *parent = fop->parent;
e7a346
+    int           i       = 0;
e7a346
+
e7a346
+    ec_trace("UPDATE_WRITEV_CBK", cookie, "ret=%d, errno=%d, parent-fop=%s",
e7a346
+             op_ret, op_errno, ec_fop_name (parent->id));
e7a346
+
e7a346
+    if (op_ret < 0) {
e7a346
+            ec_fop_set_error (parent, op_errno);
e7a346
+            goto out;
e7a346
+    }
e7a346
+    cbk = ec_cbk_data_allocate (parent->frame, this, parent,
e7a346
+                                parent->id, 0, op_ret, op_errno);
e7a346
+    if (!cbk) {
e7a346
+            ec_fop_set_error (parent, ENOMEM);
e7a346
+            goto out;
e7a346
+    }
e7a346
+
e7a346
+    if (xdata)
e7a346
+            cbk->xdata = dict_ref (xdata);
e7a346
+
e7a346
+    if (prebuf)
e7a346
+            cbk->iatt[i++] = *prebuf;
e7a346
+
e7a346
+    if (postbuf)
e7a346
+            cbk->iatt[i++] = *postbuf;
e7a346
+
e7a346
+    LOCK (&parent->lock);
e7a346
+    {
e7a346
+            parent->good &= fop->good;
e7a346
+
e7a346
+            if (gf_bits_count (parent->good) < parent->minimum) {
e7a346
+                    __ec_fop_set_error (parent, EIO);
e7a346
+            } else if (fop->error == 0 && parent->answer == NULL) {
e7a346
+                    parent->answer = cbk;
e7a346
+            }
e7a346
+    }
e7a346
+    UNLOCK (&parent->lock);
e7a346
+out:
e7a346
+    return 0;
e7a346
+}
e7a346
+
e7a346
+int32_t ec_update_write(ec_fop_data_t *fop, uintptr_t mask, off_t offset,
e7a346
+                        size_t size)
e7a346
+{
e7a346
+    struct iobref *iobref = NULL;
e7a346
+    struct iobuf *iobuf = NULL;
e7a346
+    struct iovec vector;
e7a346
+    int32_t err = -ENOMEM;
e7a346
+
e7a346
+    iobref = iobref_new();
e7a346
+    if (iobref == NULL) {
e7a346
+        goto out;
e7a346
+    }
e7a346
+    iobuf = iobuf_get(fop->xl->ctx->iobuf_pool);
e7a346
+    if (iobuf == NULL) {
e7a346
+        goto out;
e7a346
+    }
e7a346
+    err = iobref_add(iobref, iobuf);
e7a346
+    if (err != 0) {
e7a346
+        goto out;
e7a346
+    }
e7a346
+
e7a346
+    vector.iov_base = iobuf->ptr;
e7a346
+    vector.iov_len = size;
e7a346
+    memset(vector.iov_base, 0, vector.iov_len);
e7a346
+
e7a346
+    ec_writev(fop->frame, fop->xl, mask, fop->minimum,
e7a346
+              ec_update_writev_cbk, NULL, fop->fd, &vector, 1,
e7a346
+              offset, 0, iobref, NULL);
e7a346
+
e7a346
+    err = 0;
e7a346
+
e7a346
+out:
e7a346
+    if (iobuf != NULL) {
e7a346
+        iobuf_unref(iobuf);
e7a346
+    }
e7a346
+    if (iobref != NULL) {
e7a346
+        iobref_unref(iobref);
e7a346
+    }
e7a346
+
e7a346
+    return err;
e7a346
+}
e7a346
+
e7a346
 int
e7a346
 ec_inode_write_cbk (call_frame_t *frame, xlator_t *this, void *cookie,
e7a346
                     int op_ret, int op_errno, struct iatt *prestat,
e7a346
@@ -1034,62 +1125,252 @@ out:
e7a346
     }
e7a346
 }
e7a346
 
e7a346
-int32_t
e7a346
-ec_truncate_writev_cbk (call_frame_t *frame, void *cookie,
e7a346
-                        xlator_t *this, int32_t op_ret, int32_t op_errno,
e7a346
-                        struct iatt *prebuf, struct iatt *postbuf,
e7a346
-                        dict_t *xdata)
e7a346
+/*********************************************************************
e7a346
+ *
e7a346
+ * File Operation : Discard
e7a346
+ *
e7a346
+ *********************************************************************/
e7a346
+void ec_update_discard_write(ec_fop_data_t *fop, uintptr_t mask)
e7a346
 {
e7a346
-    ec_fop_data_t *fop = cookie;
e7a346
+    ec_t   *ec       = fop->xl->private;
e7a346
+    off_t  off_head  = 0;
e7a346
+    off_t  off_tail  = 0;
e7a346
+    size_t size_head = 0;
e7a346
+    size_t size_tail = 0;
e7a346
+    int    error     = 0;
e7a346
+
e7a346
+    off_head = fop->offset * ec->fragments - fop->int32;
e7a346
+    if (fop->size == 0) {
e7a346
+            error = ec_update_write (fop, mask, off_head, fop->user_size);
e7a346
+    } else {
e7a346
+            size_head = fop->int32;
e7a346
+            size_tail = (fop->user_size - fop->int32) % ec->stripe_size;
e7a346
+            off_tail = off_head + fop->user_size - size_tail;
e7a346
+            if (size_head) {
e7a346
+                    error = ec_update_write (fop, mask, off_head, size_head);
e7a346
+                    goto out;
e7a346
+            }
e7a346
+            if (size_tail) {
e7a346
+                    error = ec_update_write (fop, mask, off_tail, size_tail);
e7a346
+            }
e7a346
+    }
e7a346
+out:
e7a346
+    if (error)
e7a346
+            ec_fop_set_error (fop, -error);
e7a346
+}
e7a346
 
e7a346
-    fop->parent->good &= fop->good;
e7a346
-    ec_trace("TRUNCATE_WRITEV_CBK", cookie, "ret=%d, errno=%d",
e7a346
-             op_ret, op_errno);
e7a346
-    return 0;
e7a346
+void ec_discard_adjust_offset_size(ec_fop_data_t *fop)
e7a346
+{
e7a346
+        ec_t *ec = fop->xl->private;
e7a346
+
e7a346
+        fop->user_size = fop->size;
e7a346
+        /* If discard length covers atleast a fragment on brick, we will
e7a346
+         * perform discard operation(when fop->size is non-zero) else we just
e7a346
+         * write zeros.
e7a346
+         */
e7a346
+        fop->int32 = ec_adjust_offset_up(ec, &fop->offset, _gf_true);
e7a346
+        if (fop->size < fop->int32) {
e7a346
+                fop->size = 0;
e7a346
+        } else {
e7a346
+                fop->size -= fop->int32;
e7a346
+                ec_adjust_size_down(ec, &fop->size, _gf_true);
e7a346
+        }
e7a346
 }
e7a346
 
e7a346
-int32_t ec_truncate_write(ec_fop_data_t * fop, uintptr_t mask)
e7a346
+int32_t ec_discard_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
e7a346
+                       int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
e7a346
+                       struct iatt *postbuf, dict_t *xdata)
e7a346
 {
e7a346
-    ec_t * ec = fop->xl->private;
e7a346
-    struct iobref * iobref = NULL;
e7a346
-    struct iobuf * iobuf = NULL;
e7a346
-    struct iovec vector;
e7a346
-    int32_t err = -ENOMEM;
e7a346
+    return ec_inode_write_cbk (frame, this, cookie, op_ret, op_errno,
e7a346
+                               prebuf, postbuf, xdata);
e7a346
+}
e7a346
 
e7a346
-    iobref = iobref_new();
e7a346
-    if (iobref == NULL) {
e7a346
-        goto out;
e7a346
-    }
e7a346
-    iobuf = iobuf_get(fop->xl->ctx->iobuf_pool);
e7a346
-    if (iobuf == NULL) {
e7a346
-        goto out;
e7a346
+void ec_wind_discard(ec_t *ec, ec_fop_data_t *fop, int32_t idx)
e7a346
+{
e7a346
+    ec_trace("WIND", fop, "idx=%d", idx);
e7a346
+
e7a346
+    STACK_WIND_COOKIE(fop->frame, ec_discard_cbk, (void *)(uintptr_t)idx,
e7a346
+                      ec->xl_list[idx], ec->xl_list[idx]->fops->discard,
e7a346
+                      fop->fd, fop->offset, fop->size, fop->xdata);
e7a346
+}
e7a346
+
e7a346
+int32_t ec_manager_discard(ec_fop_data_t *fop, int32_t state)
e7a346
+{
e7a346
+    ec_cbk_data_t *cbk     = NULL;
e7a346
+    off_t         fl_start = 0;
e7a346
+    size_t        fl_size  = 0;
e7a346
+
e7a346
+
e7a346
+    switch (state) {
e7a346
+    case EC_STATE_INIT:
e7a346
+        if ((fop->size <= 0) || (fop->offset < 0)) {
e7a346
+                ec_fop_set_error(fop, EINVAL);
e7a346
+                return EC_STATE_REPORT;
e7a346
+        }
e7a346
+        /* Because of the head/tail writes, "discard" happens on the remaining
e7a346
+         * regions, but we need to compute region including head/tail writes
e7a346
+         * so compute them separately*/
e7a346
+        fl_start = fop->offset;
e7a346
+        fl_size = fop->size;
e7a346
+        fl_size += ec_adjust_offset_down (fop->xl->private, &fl_start,
e7a346
+                                          _gf_true);
e7a346
+        ec_adjust_size_up (fop->xl->private, &fl_size, _gf_true);
e7a346
+
e7a346
+        ec_discard_adjust_offset_size(fop);
e7a346
+
e7a346
+    /* Fall through */
e7a346
+
e7a346
+    case EC_STATE_LOCK:
e7a346
+        ec_lock_prepare_fd(fop, fop->fd,
e7a346
+                           EC_UPDATE_DATA | EC_UPDATE_META |
e7a346
+                           EC_QUERY_INFO, fl_start, fl_size);
e7a346
+        ec_lock(fop);
e7a346
+
e7a346
+        return EC_STATE_DISPATCH;
e7a346
+
e7a346
+    case EC_STATE_DISPATCH:
e7a346
+
e7a346
+        /* Dispatch discard fop only if we have whole fragment
e7a346
+         * to deallocate */
e7a346
+        if (fop->size) {
e7a346
+                ec_dispatch_all(fop);
e7a346
+                return EC_STATE_DELAYED_START;
e7a346
+        } else {
e7a346
+                /*Assume discard to have succeeded on mask*/
e7a346
+                fop->good = fop->mask;
e7a346
+        }
e7a346
+
e7a346
+        /* Fall through */
e7a346
+
e7a346
+    case EC_STATE_DELAYED_START:
e7a346
+
e7a346
+        if (fop->size) {
e7a346
+                if (fop->answer && fop->answer->op_ret == 0)
e7a346
+                        ec_update_discard_write (fop, fop->answer->mask);
e7a346
+        } else {
e7a346
+                ec_update_discard_write (fop, fop->mask);
e7a346
+        }
e7a346
+
e7a346
+        return EC_STATE_PREPARE_ANSWER;
e7a346
+
e7a346
+    case EC_STATE_PREPARE_ANSWER:
e7a346
+        cbk = ec_fop_prepare_answer(fop, _gf_false);
e7a346
+        if (cbk != NULL) {
e7a346
+                ec_iatt_rebuild(fop->xl->private, cbk->iatt, 2,
e7a346
+                                cbk->count);
e7a346
+
e7a346
+                /* This shouldn't fail because we have the inode locked. */
e7a346
+                GF_ASSERT(ec_get_inode_size(fop, fop->locks[0].lock->loc.inode,
e7a346
+                                            &cbk->iatt[0].ia_size));
e7a346
+
e7a346
+                cbk->iatt[1].ia_size = cbk->iatt[0].ia_size;
e7a346
+        }
e7a346
+        return EC_STATE_REPORT;
e7a346
+
e7a346
+    case EC_STATE_REPORT:
e7a346
+        cbk = fop->answer;
e7a346
+
e7a346
+        GF_ASSERT(cbk != NULL);
e7a346
+
e7a346
+        if (fop->cbks.discard != NULL) {
e7a346
+                fop->cbks.discard(fop->req_frame, fop, fop->xl, cbk->op_ret,
e7a346
+                                  cbk->op_errno, &cbk->iatt[0], &cbk->iatt[1],
e7a346
+                                  cbk->xdata);
e7a346
+        }
e7a346
+
e7a346
+        return EC_STATE_LOCK_REUSE;
e7a346
+
e7a346
+    case -EC_STATE_INIT:
e7a346
+    case -EC_STATE_LOCK:
e7a346
+    case -EC_STATE_DISPATCH:
e7a346
+    case -EC_STATE_DELAYED_START:
e7a346
+    case -EC_STATE_PREPARE_ANSWER:
e7a346
+    case -EC_STATE_REPORT:
e7a346
+        GF_ASSERT(fop->error != 0);
e7a346
+
e7a346
+        if (fop->cbks.discard != NULL) {
e7a346
+                fop->cbks.discard(fop->req_frame, fop, fop->xl, -1,
e7a346
+                                  fop->error, NULL, NULL, NULL);
e7a346
+        }
e7a346
+
e7a346
+        return EC_STATE_LOCK_REUSE;
e7a346
+
e7a346
+    case -EC_STATE_LOCK_REUSE:
e7a346
+    case EC_STATE_LOCK_REUSE:
e7a346
+        ec_lock_reuse(fop);
e7a346
+
e7a346
+        return EC_STATE_UNLOCK;
e7a346
+
e7a346
+    case -EC_STATE_UNLOCK:
e7a346
+    case EC_STATE_UNLOCK:
e7a346
+        ec_unlock(fop);
e7a346
+
e7a346
+        return EC_STATE_END;
e7a346
+
e7a346
+    default:
e7a346
+        gf_msg (fop->xl->name, GF_LOG_ERROR, EINVAL,
e7a346
+                EC_MSG_UNHANDLED_STATE,
e7a346
+                "Unhandled state %d for %s",
e7a346
+                state, ec_fop_name(fop->id));
e7a346
+
e7a346
+        return EC_STATE_END;
e7a346
     }
e7a346
-    err = iobref_add(iobref, iobuf);
e7a346
-    if (err != 0) {
e7a346
+}
e7a346
+
e7a346
+void ec_discard(call_frame_t *frame, xlator_t *this, uintptr_t target,
e7a346
+                int32_t minimum, fop_discard_cbk_t func, void *data, fd_t *fd,
e7a346
+                off_t offset, size_t len, dict_t *xdata)
e7a346
+{
e7a346
+    ec_cbk_t callback = { .discard = func };
e7a346
+    ec_fop_data_t *fop = NULL;
e7a346
+    int32_t error = ENOMEM;
e7a346
+
e7a346
+    gf_msg_trace ("ec", 0, "EC(DISCARD) %p", frame);
e7a346
+
e7a346
+    VALIDATE_OR_GOTO(this, out);
e7a346
+    GF_VALIDATE_OR_GOTO(this->name, frame, out);
e7a346
+    GF_VALIDATE_OR_GOTO(this->name, this->private, out);
e7a346
+
e7a346
+    fop = ec_fop_data_allocate(frame, this, GF_FOP_DISCARD, 0, target,
e7a346
+                               minimum, ec_wind_discard, ec_manager_discard,
e7a346
+                               callback, data);
e7a346
+    if (fop == NULL) {
e7a346
         goto out;
e7a346
     }
e7a346
 
e7a346
-    vector.iov_base = iobuf->ptr;
e7a346
-    vector.iov_len = fop->offset * ec->fragments - fop->user_size;
e7a346
-    memset(vector.iov_base, 0, vector.iov_len);
e7a346
+    fop->use_fd = 1;
e7a346
+    fop->offset = offset;
e7a346
+    fop->size = len;
e7a346
 
e7a346
-    iobuf_unref (iobuf);
e7a346
-    iobuf = NULL;
e7a346
+    if (fd != NULL) {
e7a346
+        fop->fd = fd_ref(fd);
e7a346
+    }
e7a346
 
e7a346
-    ec_writev(fop->frame, fop->xl, mask, fop->minimum, ec_truncate_writev_cbk,
e7a346
-              NULL, fop->fd, &vector, 1, fop->user_size, 0, iobref, NULL);
e7a346
+    if (xdata != NULL) {
e7a346
+        fop->xdata = dict_ref(xdata);
e7a346
+    }
e7a346
 
e7a346
-    err = 0;
e7a346
+    error = 0;
e7a346
 
e7a346
 out:
e7a346
-    if (iobuf != NULL) {
e7a346
-        iobuf_unref(iobuf);
e7a346
-    }
e7a346
-    if (iobref != NULL) {
e7a346
-        iobref_unref(iobref);
e7a346
+    if (fop != NULL) {
e7a346
+        ec_manager(fop, error);
e7a346
+    } else {
e7a346
+        func(frame, NULL, this, -1, error, NULL, NULL, NULL);
e7a346
     }
e7a346
+}
e7a346
 
e7a346
-    return err;
e7a346
+/*********************************************************************
e7a346
+ *
e7a346
+ * File Operation : truncate
e7a346
+ *
e7a346
+ *********************************************************************/
e7a346
+
e7a346
+int32_t ec_update_truncate_write (ec_fop_data_t *fop, uintptr_t mask)
e7a346
+{
e7a346
+        ec_t *ec = fop->xl->private;
e7a346
+        size_t size = fop->offset * ec->fragments - fop->user_size;
e7a346
+        return ec_update_write (fop, mask, fop->user_size, size);
e7a346
 }
e7a346
 
e7a346
 int32_t ec_truncate_open_cbk(call_frame_t * frame, void * cookie,
e7a346
@@ -1102,9 +1383,9 @@ int32_t ec_truncate_open_cbk(call_frame_t * frame, void * cookie,
e7a346
     fop->parent->good &= fop->good;
e7a346
     if (op_ret >= 0) {
e7a346
         fd_bind (fd);
e7a346
-        err = ec_truncate_write(fop->parent, fop->answer->mask);
e7a346
+        err = ec_update_truncate_write (fop->parent, fop->answer->mask);
e7a346
         if (err != 0) {
e7a346
-            fop->error = -err;
e7a346
+            ec_fop_set_error (fop->parent, -err);
e7a346
         }
e7a346
     }
e7a346
 
e7a346
@@ -1125,7 +1406,7 @@ int32_t ec_truncate_clean(ec_fop_data_t * fop)
e7a346
 
e7a346
         return 0;
e7a346
     } else {
e7a346
-        return ec_truncate_write(fop, fop->answer->mask);
e7a346
+        return ec_update_truncate_write (fop, fop->answer->mask);
e7a346
     }
e7a346
 }
e7a346
 
e7a346
diff --git a/xlators/cluster/ec/src/ec.c b/xlators/cluster/ec/src/ec.c
e7a346
index 856d60c..09c5fa8 100644
e7a346
--- a/xlators/cluster/ec/src/ec.c
e7a346
+++ b/xlators/cluster/ec/src/ec.c
e7a346
@@ -729,7 +729,8 @@ int32_t ec_gf_create(call_frame_t * frame, xlator_t * this, loc_t * loc,
e7a346
 int32_t ec_gf_discard(call_frame_t * frame, xlator_t * this, fd_t * fd,
e7a346
                       off_t offset, size_t len, dict_t * xdata)
e7a346
 {
e7a346
-    default_discard_failure_cbk(frame, ENOTSUP);
e7a346
+    ec_discard(frame, this, -1, EC_MINIMUM_MIN, default_discard_cbk,
e7a346
+               NULL, fd, offset, len, xdata);
e7a346
 
e7a346
     return 0;
e7a346
 }
e7a346
-- 
e7a346
1.8.3.1
e7a346