diff options
| -rw-r--r-- | tests/basic/gfapi/bug-1241104.c | 91 | ||||
| -rwxr-xr-x | tests/basic/gfapi/bug-1241104.sh | 30 | ||||
| -rw-r--r-- | xlators/features/locks/src/posix.c | 13 | 
3 files changed, 133 insertions, 1 deletions
diff --git a/tests/basic/gfapi/bug-1241104.c b/tests/basic/gfapi/bug-1241104.c new file mode 100644 index 00000000000..0f1a616bd39 --- /dev/null +++ b/tests/basic/gfapi/bug-1241104.c @@ -0,0 +1,91 @@ +#include <fcntl.h> +#include <unistd.h> +#include <time.h> +#include <limits.h> +#include <alloca.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <glusterfs/api/glfs.h> +#include <glusterfs/api/glfs-handles.h> +#include <sys/wait.h> + +int gfapi = 1; + +#define LOG_ERR(func, ret) do { \ +        if (ret != 0) {            \ +                fprintf (stderr, "%s : returned error %d (%s)\n", \ +                         func, ret, strerror (errno)); \ +                goto out; \ +        } else { \ +                fprintf (stderr, "%s : returned %d\n", func, ret); \ +        } \ +        } while (0) + +int +main (int argc, char *argv[]) +{ +        glfs_t    *fs = NULL; +        int        ret = 0, i, status = 0; +        glfs_fd_t *fd = NULL; +        char      *filename = "file_tmp"; +        char      *volname = NULL; +        char      *logfile = NULL; +        struct flock lock = {0, }; + +        if (argc != 3) { +                fprintf (stderr, "Invalid argument\n"); +                exit(1); +        } + +        volname = argv[1]; +        logfile = argv[2]; + +        fs = glfs_new (volname); +        if (!fs) { +                fprintf (stderr, "glfs_new: returned NULL\n"); +                return -1; +        } + +        ret = glfs_set_volfile_server (fs, "tcp", "localhost", 24007); +        LOG_ERR("glfs_set_volfile_server", ret); + +        ret = glfs_set_logging (fs, logfile, 7); +        LOG_ERR("glfs_set_logging", ret); + +        ret = glfs_init (fs); +        LOG_ERR("glfs_init", ret); + +        fd = glfs_creat(fs, filename, O_RDWR|O_SYNC, 0644); +        if (fd <= 0) { +                ret = -1; +                LOG_ERR ("glfs_creat", ret); +        } +        fprintf (stderr, "glfs-create fd - %d\n", fd); + +	/* validate locks for negative range */ +        lock.l_type = F_WRLCK; +        lock.l_whence = SEEK_SET; +        lock.l_start = 10; +        lock.l_len = -9; + +        ret = glfs_posix_lock (fd, F_SETLK, &lock); +        LOG_ERR ("glfs_posix_lock", ret); + +err: +        glfs_close(fd); +        LOG_ERR ("glfs_close", ret); + +out: +        if (fs) { +                ret = glfs_fini(fs); +                fprintf (stderr, "glfs_fini(fs) returned %d \n", ret); +        } + +        if (ret) +                exit(1); +        exit(0); +} + + diff --git a/tests/basic/gfapi/bug-1241104.sh b/tests/basic/gfapi/bug-1241104.sh new file mode 100755 index 00000000000..e071835758d --- /dev/null +++ b/tests/basic/gfapi/bug-1241104.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +. $(dirname $0)/../../include.rc +. $(dirname $0)/../../volume.rc + +cleanup; + +TEST glusterd + +TEST $CLI volume create $V0 localhost:$B0/brick1; +EXPECT 'Created' volinfo_field $V0 'Status'; + +TEST $CLI volume start $V0; +EXPECT 'Started' volinfo_field $V0 'Status'; + +logdir=`gluster --print-logdir` + +## Enable Upcall cache-invalidation feature +TEST $CLI volume set $V0 features.cache-invalidation on; + +build_tester $(dirname $0)/bug-1241104.c -lgfapi -o $(dirname $0)/bug-1241104 + +TEST ./$(dirname $0)/bug-1241104 $V0  $logdir/bug-1241104.log + +cleanup_tester $(dirname $0)/bug1241104 + +TEST $CLI volume stop $V0 +TEST $CLI volume delete $V0 + +cleanup; diff --git a/xlators/features/locks/src/posix.c b/xlators/features/locks/src/posix.c index 8d1e67e703c..dae881d4f20 100644 --- a/xlators/features/locks/src/posix.c +++ b/xlators/features/locks/src/posix.c @@ -1806,12 +1806,23 @@ pl_lk (call_frame_t *frame, xlator_t *this,          posix_lock_t *conf       = NULL;          int           ret        = 0; -        if ((flock->l_start < 0) || (flock->l_len < 0)) { +        if ((flock->l_start < 0) || +            ((flock->l_start + flock->l_len) < 0)) {                  op_ret = -1;                  op_errno = EINVAL;                  goto unwind;          } +        /* As per 'man 3 fcntl', the value of l_len may be +         * negative. In such cases, lock request should be +         * considered for the range starting at 'l_start+l_len' +         * and ending at 'l_start-1'. Update the fields accordingly. +         */ +        if (flock->l_len < 0) { +                flock->l_start += flock->l_len; +                flock->l_len = abs (flock->l_len); +        } +          pl_inode = pl_inode_get (this, fd->inode);          if (!pl_inode) {                  op_ret = -1;  | 
