50dc83
From 52d71ad0e5c27808e7d8eea8a0920298837e408c Mon Sep 17 00:00:00 2001
50dc83
From: Xavi Hernandez <xhernandez@redhat.com>
50dc83
Date: Wed, 17 Jul 2019 14:50:22 +0200
50dc83
Subject: [PATCH 271/276] cluster/ec: fix EIO error for concurrent writes on
50dc83
 sparse files
50dc83
50dc83
EC doesn't allow concurrent writes on overlapping areas, they are
50dc83
serialized. However non-overlapping writes are serviced in parallel.
50dc83
When a write is not aligned, EC first needs to read the entire chunk
50dc83
from disk, apply the modified fragment and write it again.
50dc83
50dc83
The problem appears on sparse files because a write to an offset
50dc83
implicitly creates data on offsets below it (so, in some way, they
50dc83
are overlapping). For example, if a file is empty and we read 10 bytes
50dc83
from offset 10, read() will return 0 bytes. Now, if we write one byte
50dc83
at offset 1M and retry the same read, the system call will return 10
50dc83
bytes (all containing 0's).
50dc83
50dc83
So if we have two writes, the first one at offset 10 and the second one
50dc83
at offset 1M, EC will send both in parallel because they do not overlap.
50dc83
However, the first one will try to read missing data from the first chunk
50dc83
(i.e. offsets 0 to 9) to recombine the entire chunk and do the final write.
50dc83
This read will happen in parallel with the write to 1M. What could happen
50dc83
is that half of the bricks process the write before the read, and the
50dc83
half do the read before the write. Some bricks will return 10 bytes of
50dc83
data while the otherw will return 0 bytes (because the file on the brick
50dc83
has not been expanded yet).
50dc83
50dc83
When EC tries to recombine the answers from the bricks, it can't, because
50dc83
it needs more than half consistent answers to recover the data. So this
50dc83
read fails with EIO error. This error is propagated to the parent write,
50dc83
which is aborted and EIO is returned to the application.
50dc83
50dc83
The issue happened because EC assumed that a write to a given offset
50dc83
implies that offsets below it exist.
50dc83
50dc83
This fix prevents the read of the chunk from bricks if the current size
50dc83
of the file is smaller than the read chunk offset. This size is
50dc83
correctly tracked, so this fixes the issue.
50dc83
50dc83
Also modifying ec-stripe.t file for Test #13 within it.
50dc83
In this patch, if a file size is less than the offset we are writing, we
50dc83
fill zeros in head and tail and do not consider it strip cache miss.
50dc83
That actually make sense as we know what data that part holds and there is
50dc83
no need of reading it from bricks.
50dc83
50dc83
Upstream-patch: https://review.gluster.org/c/glusterfs/+/23066
50dc83
Change-Id: Ic342e8c35c555b8534109e9314c9a0710b6225d6
50dc83
fixes: bz#1731448
50dc83
Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
50dc83
Reviewed-on: https://code.engineering.redhat.com/gerrit/177975
50dc83
Tested-by: RHGS Build Bot <nigelb@redhat.com>
50dc83
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
50dc83
---
50dc83
 tests/basic/ec/ec-stripe.t              |  2 +-
50dc83
 xlators/cluster/ec/src/ec-inode-write.c | 26 +++++++++++++++++---------
50dc83
 2 files changed, 18 insertions(+), 10 deletions(-)
50dc83
50dc83
diff --git a/tests/basic/ec/ec-stripe.t b/tests/basic/ec/ec-stripe.t
50dc83
index 1e940eb..98b9229 100644
50dc83
--- a/tests/basic/ec/ec-stripe.t
50dc83
+++ b/tests/basic/ec/ec-stripe.t
50dc83
@@ -202,7 +202,7 @@ TEST truncate -s 0 $B0/test_file
50dc83
 TEST truncate -s 0 $M0/test_file
50dc83
 TEST dd if=$B0/misc_file of=$B0/test_file  bs=1022 count=5  oflag=seek_bytes,sync seek=400 conv=notrunc
50dc83
 TEST dd if=$B0/misc_file of=$M0/test_file  bs=1022 count=5  oflag=seek_bytes,sync seek=400 conv=notrunc
50dc83
-check_statedump_md5sum 4 5
50dc83
+check_statedump_md5sum 4 4
50dc83
 clean_file_unmount
50dc83
 
50dc83
 ### 14 - Truncate to invalidate  all but one the stripe in cache  ####
50dc83
diff --git a/xlators/cluster/ec/src/ec-inode-write.c b/xlators/cluster/ec/src/ec-inode-write.c
50dc83
index ea55140..a45e6d6 100644
50dc83
--- a/xlators/cluster/ec/src/ec-inode-write.c
50dc83
+++ b/xlators/cluster/ec/src/ec-inode-write.c
50dc83
@@ -2013,20 +2013,28 @@ ec_writev_start(ec_fop_data_t *fop)
50dc83
     if (err != 0) {
50dc83
         goto failed_fd;
50dc83
     }
50dc83
+    tail = fop->size - fop->user_size - fop->head;
50dc83
     if (fop->head > 0) {
50dc83
-        found_stripe = ec_get_and_merge_stripe(ec, fop, EC_STRIPE_HEAD);
50dc83
-        if (!found_stripe) {
50dc83
-            if (ec_make_internal_fop_xdata(&xdata)) {
50dc83
-                err = -ENOMEM;
50dc83
-                goto failed_xdata;
50dc83
+        if (current > fop->offset) {
50dc83
+            found_stripe = ec_get_and_merge_stripe(ec, fop, EC_STRIPE_HEAD);
50dc83
+            if (!found_stripe) {
50dc83
+                if (ec_make_internal_fop_xdata(&xdata)) {
50dc83
+                    err = -ENOMEM;
50dc83
+                    goto failed_xdata;
50dc83
+                }
50dc83
+                ec_readv(fop->frame, fop->xl, -1, EC_MINIMUM_MIN,
50dc83
+                         ec_writev_merge_head, NULL, fd, ec->stripe_size,
50dc83
+                         fop->offset, 0, xdata);
50dc83
+            }
50dc83
+        } else {
50dc83
+            memset(fop->vector[0].iov_base, 0, fop->head);
50dc83
+            memset(fop->vector[0].iov_base + fop->size - tail, 0, tail);
50dc83
+            if (ec->stripe_cache && (fop->size <= ec->stripe_size)) {
50dc83
+                ec_add_stripe_in_cache(ec, fop);
50dc83
             }
50dc83
-            ec_readv(fop->frame, fop->xl, -1, EC_MINIMUM_MIN,
50dc83
-                     ec_writev_merge_head, NULL, fd, ec->stripe_size,
50dc83
-                     fop->offset, 0, xdata);
50dc83
         }
50dc83
     }
50dc83
 
50dc83
-    tail = fop->size - fop->user_size - fop->head;
50dc83
     if ((tail > 0) && ((fop->head == 0) || (fop->size > ec->stripe_size))) {
50dc83
         /* Current locking scheme will make sure the 'current' below will
50dc83
          * never decrease while the fop is in progress, so the checks will
50dc83
-- 
50dc83
1.8.3.1
50dc83