diff options
| author | Jiffin Tony Thottan <jthottan@redhat.com> | 2015-03-11 14:20:48 +0530 | 
|---|---|---|
| committer | Kaleb KEITHLEY <kkeithle@redhat.com> | 2015-05-05 18:05:24 -0700 | 
| commit | fa0ad231745846918b2625d0e1a89c0a5c3c24dc (patch) | |
| tree | 35ff8022e965847d19198277414636f785172e61 | |
| parent | a5fc34604aef973606431bf7d873abb91ee03d5a (diff) | |
libgfapi : anonymous fd support
Anonymous fd's are floating fd assigned to a glusterfs client
without a explicit file open. Here either it will create a new
anonymous fd or existing anonymous fd in the client stack for
requested file.The anonymous fd's are mainly used for IO's.
This patch introduces two api's glfs_h_anonymous_read and
glfs_h_anonymous_write which performs read and write respectively
Change-Id: Id646f2220e8387b2f8bb244c848dc1db6761444f
BUG: 1204651
Signed-off-by: Jiffin Tony Thottan <jthottan@redhat.com>
Reviewed-by: Niels de Vos <ndevos@redhat.com>
Reviewed-on: http://review.gluster.org/9971
Tested-by: Gluster Build System <jenkins@build.gluster.com>
| -rw-r--r-- | api/src/gfapi.aliases | 3 | ||||
| -rw-r--r-- | api/src/gfapi.map | 2 | ||||
| -rw-r--r-- | api/src/glfs-fops.c | 155 | ||||
| -rw-r--r-- | api/src/glfs-handleops.c | 48 | ||||
| -rw-r--r-- | api/src/glfs-handles.h | 10 | ||||
| -rw-r--r-- | api/src/glfs-internal.h | 8 | ||||
| -rw-r--r-- | tests/basic/gfapi/Makefile.am | 2 | ||||
| -rwxr-xr-x | tests/basic/gfapi/anonymous_fd.sh | 26 | ||||
| -rw-r--r-- | tests/basic/gfapi/anonymous_fd_read_write.c | 101 | 
9 files changed, 354 insertions, 1 deletions
diff --git a/api/src/gfapi.aliases b/api/src/gfapi.aliases index 6d48ecdf2c9..9776c87fdcb 100644 --- a/api/src/gfapi.aliases +++ b/api/src/gfapi.aliases @@ -131,6 +131,9 @@ _pub_glfs_h_acl_set _glfs_h_acl_set$GFAPI_3.7.0  _pub_glfs_h_acl_get _glfs_h_acl_get$GFAPI_3.7.0  _pub_glfs_h_statfs _glfs_h_statfs$GFAPI_3.7.0 +_pub_glfs_h_anonymous_read _glfs_h_anonymous_read$GFAPI_3.7.0 +_pub_glfs_h_anonymous_write _glfs_h_anonymous_write$GFAPI_3.7.0 +  _priv_glfs_free_from_ctx _glfs_free_from_ctx$GFAPI_PRIVATE_3.7.0  _priv_glfs_new_from_ctx _glfs_new_from_ctx$GFAPI_PRIVATE_3.7.0  _priv_glfs_resolve _glfs_resolve$GFAPI_PRIVATE_3.7.0 diff --git a/api/src/gfapi.map b/api/src/gfapi.map index 24209d666dc..48863985135 100644 --- a/api/src/gfapi.map +++ b/api/src/gfapi.map @@ -152,6 +152,8 @@ GFAPI_3.7.0 {  		glfs_h_acl_set;  		glfs_h_acl_get;  		glfs_h_statfs; +		glfs_h_anonymous_read; +		glfs_h_anonymous_write;  } GFAPI_3.6.0;  GFAPI_PRIVATE_3.7.0 { diff --git a/api/src/glfs-fops.c b/api/src/glfs-fops.c index 4aad28005f2..08f0884c7ee 100644 --- a/api/src/glfs-fops.c +++ b/api/src/glfs-fops.c @@ -3625,3 +3625,158 @@ out:  }  GFAPI_SYMVER_PRIVATE_DEFAULT(glfs_process_upcall_event, 3.7.0); + +ssize_t +glfs_anonymous_pwritev (struct glfs *fs, struct glfs_object *object, +                        const struct iovec *iovec, int iovcnt, +                        off_t offset, int flags) +{ +        xlator_t        *subvol = NULL; +        struct iobref   *iobref = NULL; +        struct iobuf    *iobuf  = NULL; +        struct iovec    iov     = {0, }; +        inode_t         *inode  = NULL; +        fd_t            *fd     = NULL; +        int             ret     = -1; +        size_t          size    = -1; + +        __glfs_entry_fs (fs); + +        subvol = glfs_active_subvol (fs); +        if (!subvol) { +                ret = -1; +                errno = EIO; +                goto out; +        } + +        /* get/refresh the in arg objects inode in correlation to the xlator */ +        inode = glfs_resolve_inode (fs, subvol, object); +        if (!inode) { +                ret = -1; +                errno = ESTALE; +                goto out; +        } + +        fd = fd_anonymous (inode); +        if (!fd) { +                ret = -1; +                gf_msg ("gfapi", GF_LOG_ERROR, ENOMEM, API_MSG_FDCREATE_FAILED, +                        "Allocating anonymous fd failed"); +                errno = ENOMEM; +                goto out; +        } + +        size = iov_length (iovec, iovcnt); + +        iobuf = iobuf_get2 (subvol->ctx->iobuf_pool, size); +        if (!iobuf) { +                ret = -1; +                errno = ENOMEM; +                goto out; +        } + +        iobref = iobref_new (); +        if (!iobref) { +                iobuf_unref (iobuf); +                errno = ENOMEM; +                ret = -1; +                goto out; +        } + +        ret = iobref_add (iobref, iobuf); +        if (ret) { +                iobuf_unref (iobuf); +                iobref_unref (iobref); +                errno = ENOMEM; +                ret = -1; +                goto out; +        } + +        iov_unload (iobuf_ptr (iobuf), iovec, iovcnt); + +        iov.iov_base = iobuf_ptr (iobuf); +        iov.iov_len = size; + +        ret = syncop_writev (subvol, fd, &iov, 1, offset, iobref, flags, +                             NULL, NULL); +        DECODE_SYNCOP_ERR (ret); + +        iobuf_unref (iobuf); +        iobref_unref (iobref); + +        if (ret <= 0) +                goto out; + +out: + +        if (fd) +                fd_unref(fd); + +        glfs_subvol_done (fs, subvol); + +        return ret; +} + +ssize_t +glfs_anonymous_preadv (struct glfs *fs,  struct glfs_object *object, +                       const struct iovec *iovec, int iovcnt, +                       off_t offset, int flags) +{ +        xlator_t        *subvol = NULL; +        struct iovec    *iov    = NULL; +        struct iobref   *iobref = NULL; +        inode_t         *inode  = NULL; +        fd_t            *fd     = NULL; +        int             cnt     = 0; +        ssize_t         ret     = -1; +        ssize_t         size    = -1; + +        __glfs_entry_fs (fs); + +        subvol = glfs_active_subvol (fs); +        if (!subvol) { +                ret = -1; +                errno = EIO; +                goto out; +        } + +        /* get/refresh the in arg objects inode in correlation to the xlator */ +        inode = glfs_resolve_inode (fs, subvol, object); +        if (!inode) { +                ret = -1; +                errno = ESTALE; +                goto out; +        } + +        fd = fd_anonymous (inode); +        if (!fd) { +                ret = -1; +                gf_msg ("gfapi", GF_LOG_ERROR, ENOMEM, API_MSG_FDCREATE_FAILED, +                        "Allocating anonymous fd failed"); +                errno = ENOMEM; +                goto out; +        } + +        size = iov_length (iovec, iovcnt); + +	ret = syncop_readv (subvol, fd, size, offset, flags, &iov, &cnt, +                            &iobref, NULL, NULL); +        DECODE_SYNCOP_ERR (ret); +        if (ret <= 0) +                goto out; + +        size = iov_copy (iovec, iovcnt, iov, cnt); + +        ret = size; +out: +        if (iov) +                GF_FREE (iov); +        if (iobref) +                iobref_unref (iobref); +        if (fd) +                fd_unref(fd); + +        glfs_subvol_done (fs, subvol); + +        return ret; +} diff --git a/api/src/glfs-handleops.c b/api/src/glfs-handleops.c index 4a544f7c905..2793e1cb118 100644 --- a/api/src/glfs-handleops.c +++ b/api/src/glfs-handleops.c @@ -1962,3 +1962,51 @@ pub_glfs_h_acl_set (struct glfs *fs, struct glfs_object *object,  #endif  GFAPI_SYMVER_PUBLIC_DEFAULT(glfs_h_acl_set, 3.7.0);  GFAPI_SYMVER_PUBLIC_DEFAULT(glfs_h_acl_get, 3.7.0); + +/* The API to perform read using anonymous fd */ +ssize_t +pub_glfs_h_anonymous_read (struct glfs *fs, struct glfs_object *object, +                           const void *buf, size_t count, off_t offset) +{ +        struct iovec    iov     = {0, }; +        ssize_t         ret     = 0; + +        /* validate in args */ +        if ((fs == NULL) || (object == NULL)) { +                errno = EINVAL; +                return -1; +        } + +        iov.iov_base = (void *) buf; +        iov.iov_len = count; + +        ret = glfs_anonymous_preadv (fs, object, &iov, 1, offset, 0); + +        return ret; +} + +GFAPI_SYMVER_PUBLIC_DEFAULT(glfs_h_anonymous_read, 3.7.0); + +/* The API to perform write using anonymous fd */ +ssize_t +pub_glfs_h_anonymous_write (struct glfs *fs, struct glfs_object *object, +                            const void *buf, size_t count, off_t offset) +{ +        struct iovec iov        = {0, }; +        ssize_t      ret        = 0; + +        /* validate in args */ +        if ((fs == NULL) || (object == NULL)) { +                errno = EINVAL; +                return -1; +        } + +        iov.iov_base = (void *) buf; +        iov.iov_len = count; + +        ret = glfs_anonymous_pwritev (fs, object, &iov, 1, offset, 0); + +        return ret; +} + +GFAPI_SYMVER_PUBLIC_DEFAULT(glfs_h_anonymous_write, 3.7.0); diff --git a/api/src/glfs-handles.h b/api/src/glfs-handles.h index 28e9e79b9a3..19c4e8f7a62 100644 --- a/api/src/glfs-handles.h +++ b/api/src/glfs-handles.h @@ -310,6 +310,16 @@ glfs_h_acl_get (struct glfs *fs, struct glfs_object *object,                  const acl_type_t type) __THROW;          GFAPI_PUBLIC(glfs_h_acl_get, 3.7.0); +size_t +glfs_h_anonymous_write (struct glfs *fs, struct glfs_object *object, +                        const void *buf, size_t count, off_t offset) __THROW +        GFAPI_PUBLIC(glfs_h_anonymous_write, 3.7.0); + +ssize_t +glfs_h_anonymous_read (struct glfs *fs, struct glfs_object *object, +                      const void *buf, size_t count, off_t offset) __THROW +        GFAPI_PUBLIC(glfs_h_anonymous_read, 3.7.0); +  __END_DECLS  #endif /* !_GLFS_HANDLES_H */ diff --git a/api/src/glfs-internal.h b/api/src/glfs-internal.h index 972f2e4cf49..275f8d1534b 100644 --- a/api/src/glfs-internal.h +++ b/api/src/glfs-internal.h @@ -342,4 +342,12 @@ glfs_h_poll_cache_invalidation (struct glfs *fs,                                  struct callback_arg *up_arg,                                  struct gf_upcall *upcall_data); +ssize_t +glfs_anonymous_preadv (struct glfs *fs, struct glfs_object *object, +                       const struct iovec *iovec, int iovcnt, +                       off_t offset, int flags); +ssize_t +glfs_anonymous_pwritev (struct glfs *fs, struct glfs_object *object, +                        const struct iovec *iovec, int iovcnt, +                        off_t offset, int flags);  #endif /* !_GLFS_INTERNAL_H */ diff --git a/tests/basic/gfapi/Makefile.am b/tests/basic/gfapi/Makefile.am index 39fc04b0f39..cdb0e543803 100644 --- a/tests/basic/gfapi/Makefile.am +++ b/tests/basic/gfapi/Makefile.am @@ -4,7 +4,7 @@  CFLAGS   = -Wall -g $(shell pkg-config --cflags glusterfs-api)  LDFLAGS  = $(shell pkg-config --libs glusterfs-api) -BINARIES = upcall-cache-invalidate libgfapi-fini-hang +BINARIES = upcall-cache-invalidate libgfapi-fini-hang anonymous_fd  %: %.c diff --git a/tests/basic/gfapi/anonymous_fd.sh b/tests/basic/gfapi/anonymous_fd.sh new file mode 100755 index 00000000000..2184f8efc8e --- /dev/null +++ b/tests/basic/gfapi/anonymous_fd.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +. $(dirname $0)/../../include.rc +. $(dirname $0)/../../volume.rc + +cleanup; + +TEST glusterd + +TEST $CLI volume create $V0 $H0:$B0/brick1; +EXPECT 'Created' volinfo_field $V0 'Status'; + +TEST $CLI volume start $V0; +EXPECT 'Started' volinfo_field $V0 'Status'; + +logdir=`gluster --print-logdir` + +TEST build_tester $(dirname $0)/anonymous_fd_read_write.c -lgfapi -o $(dirname $0)/anonymous_fd +TEST ./$(dirname $0)/anonymous_fd $V0  $logdir/anonymous_fd.log + +cleanup_tester $(dirname $0)/anonymous_fd + +TEST $CLI volume stop $V0 +TEST $CLI volume delete $V0 + +cleanup; diff --git a/tests/basic/gfapi/anonymous_fd_read_write.c b/tests/basic/gfapi/anonymous_fd_read_write.c new file mode 100644 index 00000000000..281184e8223 --- /dev/null +++ b/tests/basic/gfapi/anonymous_fd_read_write.c @@ -0,0 +1,101 @@ +#include <fcntl.h> +#include <unistd.h> +#include <time.h> +#include <limits.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <glusterfs/api/glfs.h> +#include <glusterfs/api/glfs-handles.h> + +#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[]) +{ +        int                     ret            = 0; +        glfs_t                  *fs            = NULL; +        struct glfs_object      *root = NULL, *file_obj = NULL; +        struct stat             sb             = {0, }; +        char                    readbuf[32], writebuf[32]; +        char                    *filename      = "file.txt"; +        char                    *logfile       = NULL; +        char                    *volname       = NULL; + +        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"); +                ret = -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); + +        root = glfs_h_lookupat (fs, NULL, "/", &sb); +        if (root == NULL) { +                fprintf (stderr, "glfs_h_lookupat: error on lookup of / ,%s\n", +                         strerror (errno)); +                goto out; +        } + +        file_obj = glfs_h_creat (fs, root, filename, O_CREAT, 0644, &sb); +        if (file_obj == NULL) { +                fprintf (stderr, "glfs_h_creat: error on create of %s: from (%p),%s\n", +                         filename, root, strerror (errno)); +                goto out; +        } + +        /* test read/write based on anonymous fd */ +        memcpy (writebuf, "abcdefghijklmnopqrstuvwxyz012345", 32); + +        ret = glfs_h_anonymous_write (fs, file_obj, writebuf, 32, 0); +        if (ret < 0) +                LOG_ERR ("glfs_h_anonymous_write", ret); + +        ret = glfs_h_anonymous_read (fs, file_obj, readbuf, 32, 0); +        if (ret < 0) +                LOG_ERR ("glfs_h_anonymous_read", ret); + +        if (memcmp (readbuf, writebuf, 32)) { +                fprintf (stderr, "Failed to read what I wrote: %s %s\n", readbuf, +                         writebuf); +                ret = -1; +                goto out; +        } + +        ret = 0; +out: +        if (file_obj) +                glfs_h_close (file_obj); + +        if (fs) { +                ret = glfs_fini(fs); +                fprintf (stderr, "glfs_fini(fs) returned %d \n", ret); +        } +        if (ret) +                exit(1); +        exit(0); +}  | 
