Blob Blame History Raw
From e1e80bc293f5a84087d5a35054cd8bc0abfa4836 Mon Sep 17 00:00:00 2001
From: Pranith Kumar K <pkarampu@redhat.com>
Date: Wed, 13 Jun 2018 12:17:28 +0530
Subject: [PATCH 302/305] storage/posix: Handle ENOSPC correctly in zero_fill

	Upstream patch: https://review.gluster.org/20254

BUG: 1594658
Change-Id: Icc521d86cc510f88b67d334b346095713899087a
Signed-off-by: Pranith Kumar K <pkarampu@redhat.com>
Reviewed-on: https://code.engineering.redhat.com/gerrit/142311
Tested-by: RHGS Build Bot <nigelb@redhat.com>
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
---
 tests/basic/posix/zero-fill-enospace.c | 64 ++++++++++++++++++++++++++++++++++
 tests/basic/posix/zero-fill-enospace.t | 35 +++++++++++++++++++
 xlators/storage/posix/src/posix.c      | 23 +++++++++++-
 3 files changed, 121 insertions(+), 1 deletion(-)
 create mode 100644 tests/basic/posix/zero-fill-enospace.c
 create mode 100644 tests/basic/posix/zero-fill-enospace.t

diff --git a/tests/basic/posix/zero-fill-enospace.c b/tests/basic/posix/zero-fill-enospace.c
new file mode 100644
index 0000000..b1aaa57
--- /dev/null
+++ b/tests/basic/posix/zero-fill-enospace.c
@@ -0,0 +1,64 @@
+#include <stdio.h>
+#include <glusterfs/api/glfs.h>
+#include <glusterfs/api/glfs-handles.h>
+
+int
+main (int argc, char *argv[])
+{
+        glfs_t    *fs = NULL;
+        glfs_fd_t *fd = NULL;
+        int        ret = 1;
+        int        size = 0;
+
+        if (argc != 6) {
+                fprintf (stderr, "Syntax: %s <host> <volname> <file-path> <log-file> <size>\n", argv[0]);
+                return 1;
+        }
+
+        fs = glfs_new (argv[2]);
+        if (!fs) {
+                fprintf (stderr, "glfs_new: returned NULL\n");
+                return 1;
+        }
+
+        ret = glfs_set_volfile_server (fs, "tcp", argv[1], 24007);
+        if (ret != 0) {
+                fprintf (stderr, "glfs_set_volfile_server: retuned %d\n", ret);
+                goto out;
+        }
+        ret = glfs_set_logging (fs, argv[4], 7);
+        if (ret != 0) {
+                fprintf (stderr, "glfs_set_logging: returned %d\n", ret);
+                goto out;
+        }
+        ret = glfs_init (fs);
+        if (ret != 0) {
+                fprintf (stderr, "glfs_init: returned %d\n", ret);
+                goto out;
+        }
+
+        fd = glfs_open (fs, argv[3], O_RDWR);
+        if (fd == NULL) {
+                fprintf (stderr, "glfs_open: returned NULL\n");
+                goto out;
+        }
+
+        size = atoi(argv[5]);
+        if (size < 0) {
+                fprintf (stderr, "Wrong size %s", argv[5]);
+                goto out;
+        }
+        ret = glfs_zerofill (fd, 0, atoi(argv[5]));
+        if (ret <= 0) {
+                fprintf (stderr, "glfs_zerofill: returned %d\n", ret);
+                goto out;
+        }
+
+        ret = 0;
+
+out:
+        if (fd)
+                glfs_close(fd);
+        glfs_fini (fs);
+        return ret;
+}
diff --git a/tests/basic/posix/zero-fill-enospace.t b/tests/basic/posix/zero-fill-enospace.t
new file mode 100644
index 0000000..ac2e61b
--- /dev/null
+++ b/tests/basic/posix/zero-fill-enospace.t
@@ -0,0 +1,35 @@
+#!/bin/bash
+
+. $(dirname $0)/../../include.rc
+. $(dirname $0)/../../volume.rc
+. $(dirname $0)/../../dht.rc
+
+cleanup;
+
+TEST glusterd;
+TEST pidof glusterd;
+
+TEST truncate -s 100M $B0/brick1
+
+TEST L1=`SETUP_LOOP $B0/brick1`
+TEST MKFS_LOOP $L1
+
+TEST mkdir -p $B0/${V0}1
+
+TEST MOUNT_LOOP $L1 $B0/${V0}1
+
+TEST $CLI volume create $V0 $H0:$B0/${V0}1
+
+TEST $CLI volume start $V0;
+
+TEST glusterfs -s $H0 --volfile-id=$V0 $M0
+TEST touch $M0/foo
+TEST build_tester $(dirname $0)/zero-fill-enospace.c -lgfapi -Wall -O2
+TEST ! $(dirname $0)/zero-fill-enospace $H0 $V0 /foo `gluster --print-logdir`/glfs-$V0.log 104857600
+
+TEST force_umount $M0
+TEST $CLI volume stop $V0
+UMOUNT_LOOP ${B0}/${V0}1
+rm -f ${B0}/brick1
+
+cleanup
diff --git a/xlators/storage/posix/src/posix.c b/xlators/storage/posix/src/posix.c
index af54882..01f472b 100644
--- a/xlators/storage/posix/src/posix.c
+++ b/xlators/storage/posix/src/posix.c
@@ -943,17 +943,32 @@ _posix_do_zerofill(int fd, off_t offset, off_t len, int o_direct)
                 op_ret = sys_writev (fd, vector, num_vect);
                 if (op_ret < 0)
                         goto err;
+                if (op_ret != (vect_size * num_vect)) {
+                        op_ret = -1;
+                        errno = ENOSPC;
+                        goto err;
+                }
         }
         if (extra) {
                 op_ret = sys_writev (fd, vector, extra);
                 if (op_ret < 0)
                         goto err;
+                if (op_ret != (vect_size * extra)) {
+                        op_ret = -1;
+                        errno = ENOSPC;
+                        goto err;
+                }
         }
         if (remain) {
                 vector[0].iov_len = remain;
                 op_ret = sys_writev (fd, vector , 1);
                 if (op_ret < 0)
                         goto err;
+                if (op_ret != remain) {
+                        op_ret = -1;
+                        errno = ENOSPC;
+                        goto err;
+                }
         }
 err:
         if (o_direct)
@@ -1014,8 +1029,14 @@ posix_do_zerofill (call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset,
          */
         flags = FALLOC_FL_ZERO_RANGE;
         ret = sys_fallocate (pfd->fd, flags, offset, len);
-        if (ret == 0)
+        if (ret == 0) {
                 goto fsync;
+        } else {
+                ret = -errno;
+                if ((ret != -ENOSYS) && (ret != -EOPNOTSUPP)) {
+                        goto out;
+                }
+        }
 
         ret = _posix_do_zerofill (pfd->fd, offset, len, pfd->flags & O_DIRECT);
         if (ret < 0) {
-- 
1.8.3.1