17b94a
From da75c2857fd8b173d47fb7fc3b925ffd14105f64 Mon Sep 17 00:00:00 2001
17b94a
From: "Kaleb S. KEITHLEY" <kkeithle@rhel7x.kkeithle.usersys.redhat.com>
17b94a
Date: Wed, 23 Dec 2020 07:39:13 -0500
17b94a
Subject: [PATCH 517/517] gfapi: 'glfs_h_creat_open' - new API to create handle
17b94a
 and open fd
17b94a
17b94a
Right now we have two separate APIs, one
17b94a
- 'glfs_h_creat_handle' to create handle & another
17b94a
- 'glfs_h_open' to create a glfd to return to application
17b94a
17b94a
Having two separate routines can result in access errors
17b94a
while trying to create and write into a read-only file.
17b94a
17b94a
Since a fd is opened even during file/directory creation,
17b94a
introducing a new API to make these two operations atomic i.e,
17b94a
which can create both handle & fd and pass them to application
17b94a
17b94a
This is backport of below mainline patch -
17b94a
- https://review.gluster.org/#/c/glusterfs/+/23448/
17b94a
- bz#1753569
17b94a
17b94a
> Signed-off-by: Soumya Koduri <skoduri@redhat.com>
17b94a
> Change-Id: Ibf513fcfcdad175f4d7eb6fa7a61b8feec6d33b5
17b94a
> release-6: commit 5a2af2fd06356f6fc79d591c352caffd4c511c9e
17b94a
> master:    commit 41a0f2aa755ec7162facd30209f2fa3f40308766
17b94a
17b94a
BUG: 1910119
17b94a
Change-Id: Ib397dbe82a6928d8f24251809d30febddd007bfc
17b94a
Signed-off-by: Kaleb S. KEITHLEY <kkeithle@redhat.com>
17b94a
Reviewed-on: https://code.engineering.redhat.com/gerrit/222083
17b94a
Reviewed-by: Soumya Koduri <skoduri@redhat.com>
17b94a
Tested-by: RHGS Build Bot <nigelb@redhat.com>
17b94a
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
17b94a
---
17b94a
 api/src/gfapi.aliases                 |   1 +
17b94a
 api/src/gfapi.map                     |   5 ++
17b94a
 api/src/glfs-handleops.c              | 135 ++++++++++++++++++++++++++++++++++
17b94a
 api/src/glfs-handles.h                |   5 ++
17b94a
 tests/basic/gfapi/glfs_h_creat_open.c | 118 +++++++++++++++++++++++++++++
17b94a
 tests/basic/gfapi/glfs_h_creat_open.t |  27 +++++++
17b94a
 6 files changed, 291 insertions(+)
17b94a
 create mode 100644 tests/basic/gfapi/glfs_h_creat_open.c
17b94a
 create mode 100755 tests/basic/gfapi/glfs_h_creat_open.t
17b94a
17b94a
diff --git a/api/src/gfapi.aliases b/api/src/gfapi.aliases
17b94a
index 692ae13..3d3415c 100644
17b94a
--- a/api/src/gfapi.aliases
17b94a
+++ b/api/src/gfapi.aliases
17b94a
@@ -197,3 +197,4 @@ _pub_glfs_fsetattr _glfs_fsetattr$GFAPI_6.0
17b94a
 _pub_glfs_setattr _glfs_setattr$GFAPI_6.0
17b94a
 
17b94a
 _pub_glfs_set_statedump_path _glfs_set_statedump_path@GFAPI_6.4
17b94a
+_pub_glfs_h_creat_open _glfs_h_creat_open@GFAPI_6.6
17b94a
diff --git a/api/src/gfapi.map b/api/src/gfapi.map
17b94a
index df65837..614f3f6 100644
17b94a
--- a/api/src/gfapi.map
17b94a
+++ b/api/src/gfapi.map
17b94a
@@ -276,3 +276,8 @@ GFAPI_6.4 {
17b94a
 	global:
17b94a
 		glfs_set_statedump_path;
17b94a
 } GFAPI_PRIVATE_6.1;
17b94a
+
17b94a
+GFAPI_6.6 {
17b94a
+	global:
17b94a
+		glfs_h_creat_open;
17b94a
+} GFAPI_6.4;
17b94a
diff --git a/api/src/glfs-handleops.c b/api/src/glfs-handleops.c
17b94a
index d4e1545..7b8ff14 100644
17b94a
--- a/api/src/glfs-handleops.c
17b94a
+++ b/api/src/glfs-handleops.c
17b94a
@@ -843,6 +843,141 @@ invalid_fs:
17b94a
 GFAPI_SYMVER_PUBLIC_DEFAULT(glfs_h_creat, 3.4.2);
17b94a
 
17b94a
 struct glfs_object *
17b94a
+pub_glfs_h_creat_open(struct glfs *fs, struct glfs_object *parent,
17b94a
+                      const char *path, int flags, mode_t mode,
17b94a
+                      struct stat *stat, struct glfs_fd **out_fd)
17b94a
+{
17b94a
+    int ret = -1;
17b94a
+    struct glfs_fd *glfd = NULL;
17b94a
+    xlator_t *subvol = NULL;
17b94a
+    inode_t *inode = NULL;
17b94a
+    loc_t loc = {
17b94a
+        0,
17b94a
+    };
17b94a
+    struct iatt iatt = {
17b94a
+        0,
17b94a
+    };
17b94a
+    uuid_t gfid;
17b94a
+    dict_t *xattr_req = NULL;
17b94a
+    struct glfs_object *object = NULL;
17b94a
+    dict_t *fop_attr = NULL;
17b94a
+
17b94a
+    /* validate in args */
17b94a
+    if ((fs == NULL) || (parent == NULL) || (path == NULL) ||
17b94a
+        (out_fd == NULL)) {
17b94a
+        errno = EINVAL;
17b94a
+        return NULL;
17b94a
+    }
17b94a
+
17b94a
+    DECLARE_OLD_THIS;
17b94a
+    __GLFS_ENTRY_VALIDATE_FS(fs, invalid_fs);
17b94a
+
17b94a
+    /* get the active volume */
17b94a
+    subvol = glfs_active_subvol(fs);
17b94a
+    if (!subvol) {
17b94a
+        ret = -1;
17b94a
+        goto out;
17b94a
+    }
17b94a
+
17b94a
+    /* get/refresh the in arg objects inode in correlation to the xlator */
17b94a
+    inode = glfs_resolve_inode(fs, subvol, parent);
17b94a
+    if (!inode) {
17b94a
+        ret = -1;
17b94a
+        goto out;
17b94a
+    }
17b94a
+
17b94a
+    xattr_req = dict_new();
17b94a
+    if (!xattr_req) {
17b94a
+        ret = -1;
17b94a
+        errno = ENOMEM;
17b94a
+        goto out;
17b94a
+    }
17b94a
+
17b94a
+    gf_uuid_generate(gfid);
17b94a
+    ret = dict_set_gfuuid(xattr_req, "gfid-req", gfid, true);
17b94a
+    if (ret) {
17b94a
+        ret = -1;
17b94a
+        errno = ENOMEM;
17b94a
+        goto out;
17b94a
+    }
17b94a
+
17b94a
+    GLFS_LOC_FILL_PINODE(inode, loc, ret, errno, out, path);
17b94a
+
17b94a
+    glfd = glfs_fd_new(fs);
17b94a
+    if (!glfd) {
17b94a
+        ret = -1;
17b94a
+        errno = ENOMEM;
17b94a
+        goto out;
17b94a
+    }
17b94a
+
17b94a
+    glfd->fd = fd_create(loc.inode, getpid());
17b94a
+    if (!glfd->fd) {
17b94a
+        ret = -1;
17b94a
+        errno = ENOMEM;
17b94a
+        goto out;
17b94a
+    }
17b94a
+    glfd->fd->flags = flags;
17b94a
+
17b94a
+    ret = get_fop_attr_thrd_key(&fop_attr);
17b94a
+    if (ret)
17b94a
+        gf_msg_debug("gfapi", 0, "Getting leaseid from thread failed");
17b94a
+
17b94a
+    /* fop/op */
17b94a
+    ret = syncop_create(subvol, &loc, flags, mode, glfd->fd, &iatt, xattr_req,
17b94a
+                        NULL);
17b94a
+    DECODE_SYNCOP_ERR(ret);
17b94a
+
17b94a
+    /* populate out args */
17b94a
+    if (ret == 0) {
17b94a
+        glfd->fd->flags = flags;
17b94a
+
17b94a
+        ret = glfs_loc_link(&loc, &iatt);
17b94a
+        if (ret != 0) {
17b94a
+            goto out;
17b94a
+        }
17b94a
+
17b94a
+        if (stat)
17b94a
+            glfs_iatt_to_stat(fs, &iatt, stat);
17b94a
+
17b94a
+        ret = glfs_create_object(&loc, &object);
17b94a
+    }
17b94a
+
17b94a
+out:
17b94a
+    if (ret && object != NULL) {
17b94a
+        /* Release the held reference */
17b94a
+        glfs_h_close(object);
17b94a
+        object = NULL;
17b94a
+    }
17b94a
+
17b94a
+    loc_wipe(&loc;;
17b94a
+
17b94a
+    if (inode)
17b94a
+        inode_unref(inode);
17b94a
+
17b94a
+    if (fop_attr)
17b94a
+        dict_unref(fop_attr);
17b94a
+
17b94a
+    if (xattr_req)
17b94a
+        dict_unref(xattr_req);
17b94a
+
17b94a
+    if (ret && glfd) {
17b94a
+        GF_REF_PUT(glfd);
17b94a
+    } else if (glfd) {
17b94a
+        glfd_set_state_bind(glfd);
17b94a
+        *out_fd = glfd;
17b94a
+    }
17b94a
+
17b94a
+    glfs_subvol_done(fs, subvol);
17b94a
+
17b94a
+    __GLFS_EXIT_FS;
17b94a
+
17b94a
+invalid_fs:
17b94a
+    return object;
17b94a
+}
17b94a
+
17b94a
+GFAPI_SYMVER_PUBLIC_DEFAULT(glfs_h_creat_open, 6.6);
17b94a
+
17b94a
+struct glfs_object *
17b94a
 pub_glfs_h_mkdir(struct glfs *fs, struct glfs_object *parent, const char *path,
17b94a
                  mode_t mode, struct stat *stat)
17b94a
 {
17b94a
diff --git a/api/src/glfs-handles.h b/api/src/glfs-handles.h
17b94a
index f7e6a06..4d039b9 100644
17b94a
--- a/api/src/glfs-handles.h
17b94a
+++ b/api/src/glfs-handles.h
17b94a
@@ -250,6 +250,11 @@ int
17b94a
 glfs_h_access(glfs_t *fs, glfs_object_t *object, int mask) __THROW
17b94a
     GFAPI_PUBLIC(glfs_h_access, 3.6.0);
17b94a
 
17b94a
+struct glfs_object *
17b94a
+glfs_h_creat_open(struct glfs *fs, struct glfs_object *parent, const char *path,
17b94a
+                  int flags, mode_t mode, struct stat *stat,
17b94a
+                  struct glfs_fd **out_fd) __THROW
17b94a
+    GFAPI_PUBLIC(glfs_h_creat_open, 6.6);
17b94a
 /*
17b94a
   SYNOPSIS
17b94a
 
17b94a
diff --git a/tests/basic/gfapi/glfs_h_creat_open.c b/tests/basic/gfapi/glfs_h_creat_open.c
17b94a
new file mode 100644
17b94a
index 0000000..7672561
17b94a
--- /dev/null
17b94a
+++ b/tests/basic/gfapi/glfs_h_creat_open.c
17b94a
@@ -0,0 +1,118 @@
17b94a
+#include <fcntl.h>
17b94a
+#include <unistd.h>
17b94a
+#include <time.h>
17b94a
+#include <limits.h>
17b94a
+#include <string.h>
17b94a
+#include <stdio.h>
17b94a
+#include <errno.h>
17b94a
+#include <stdlib.h>
17b94a
+#include <glusterfs/api/glfs.h>
17b94a
+#include <glusterfs/api/glfs-handles.h>
17b94a
+
17b94a
+#define LOG_ERR(func, ret)                                                     \
17b94a
+    do {                                                                       \
17b94a
+        if (ret != 0) {                                                        \
17b94a
+            fprintf(stderr, "%s : returned error ret(%d), errno(%d)\n", func,  \
17b94a
+                    ret, errno);                                               \
17b94a
+            exit(1);                                                           \
17b94a
+        } else {                                                               \
17b94a
+            fprintf(stderr, "%s : returned %d\n", func, ret);                  \
17b94a
+        }                                                                      \
17b94a
+    } while (0)
17b94a
+#define LOG_IF_NO_ERR(func, ret)                                               \
17b94a
+    do {                                                                       \
17b94a
+        if (ret == 0) {                                                        \
17b94a
+            fprintf(stderr, "%s : hasn't returned error %d\n", func, ret);     \
17b94a
+            exit(1);                                                           \
17b94a
+        } else {                                                               \
17b94a
+            fprintf(stderr, "%s : returned %d\n", func, ret);                  \
17b94a
+        }                                                                      \
17b94a
+    } while (0)
17b94a
+int
17b94a
+main(int argc, char *argv[])
17b94a
+{
17b94a
+    glfs_t *fs = NULL;
17b94a
+    int ret = 0;
17b94a
+    struct glfs_object *root = NULL, *leaf = NULL;
17b94a
+    glfs_fd_t *fd = NULL;
17b94a
+    char *filename = "/ro-file";
17b94a
+    struct stat sb = {
17b94a
+        0,
17b94a
+    };
17b94a
+    char *logfile = NULL;
17b94a
+    char *volname = NULL;
17b94a
+    char *hostname = NULL;
17b94a
+    char buf[32] = "abcdefghijklmnopqrstuvwxyz012345";
17b94a
+
17b94a
+    fprintf(stderr, "Starting glfs_h_creat_open\n");
17b94a
+
17b94a
+    if (argc != 4) {
17b94a
+        fprintf(stderr, "Invalid argument\n");
17b94a
+        exit(1);
17b94a
+    }
17b94a
+
17b94a
+    hostname = argv[1];
17b94a
+    volname = argv[2];
17b94a
+    logfile = argv[3];
17b94a
+
17b94a
+    fs = glfs_new(volname);
17b94a
+    if (!fs) {
17b94a
+        fprintf(stderr, "glfs_new: returned NULL\n");
17b94a
+        return 1;
17b94a
+    }
17b94a
+
17b94a
+    ret = glfs_set_volfile_server(fs, "tcp", hostname, 24007);
17b94a
+    LOG_ERR("glfs_set_volfile_server", ret);
17b94a
+
17b94a
+    ret = glfs_set_logging(fs, logfile, 7);
17b94a
+    LOG_ERR("glfs_set_logging", ret);
17b94a
+
17b94a
+    ret = glfs_init(fs);
17b94a
+    LOG_ERR("glfs_init", ret);
17b94a
+
17b94a
+    sleep(2);
17b94a
+    root = glfs_h_lookupat(fs, NULL, "/", &sb, 0);
17b94a
+    if (!root) {
17b94a
+        ret = -1;
17b94a
+        LOG_ERR("glfs_h_lookupat root", ret);
17b94a
+    }
17b94a
+    leaf = glfs_h_lookupat(fs, root, filename, &sb, 0);
17b94a
+    if (!leaf) {
17b94a
+        ret = -1;
17b94a
+        LOG_IF_NO_ERR("glfs_h_lookupat leaf", ret);
17b94a
+    }
17b94a
+
17b94a
+    leaf = glfs_h_creat_open(fs, root, filename, O_RDONLY, 00444, &sb, &fd;;
17b94a
+    if (!leaf || !fd) {
17b94a
+        ret = -1;
17b94a
+        LOG_ERR("glfs_h_creat leaf", ret);
17b94a
+    }
17b94a
+    fprintf(stderr, "glfs_h_create_open leaf - %p\n", leaf);
17b94a
+
17b94a
+    ret = glfs_write(fd, buf, 32, 0);
17b94a
+    if (ret < 0) {
17b94a
+        fprintf(stderr, "glfs_write: error writing to file %s, %s\n", filename,
17b94a
+                strerror(errno));
17b94a
+        goto out;
17b94a
+    }
17b94a
+
17b94a
+    ret = glfs_h_getattrs(fs, leaf, &sb);
17b94a
+    LOG_ERR("glfs_h_getattrs", ret);
17b94a
+
17b94a
+    if (sb.st_size != 32) {
17b94a
+        fprintf(stderr, "glfs_write: post size mismatch\n");
17b94a
+        goto out;
17b94a
+    }
17b94a
+
17b94a
+    fprintf(stderr, "Successfully opened and written to a read-only file \n");
17b94a
+out:
17b94a
+    if (fd)
17b94a
+        glfs_close(fd);
17b94a
+
17b94a
+    ret = glfs_fini(fs);
17b94a
+    LOG_ERR("glfs_fini", ret);
17b94a
+
17b94a
+    fprintf(stderr, "End of libgfapi_fini\n");
17b94a
+
17b94a
+    exit(0);
17b94a
+}
17b94a
diff --git a/tests/basic/gfapi/glfs_h_creat_open.t b/tests/basic/gfapi/glfs_h_creat_open.t
17b94a
new file mode 100755
17b94a
index 0000000..f24ae73
17b94a
--- /dev/null
17b94a
+++ b/tests/basic/gfapi/glfs_h_creat_open.t
17b94a
@@ -0,0 +1,27 @@
17b94a
+#!/bin/bash
17b94a
+
17b94a
+. $(dirname $0)/../../include.rc
17b94a
+. $(dirname $0)/../../volume.rc
17b94a
+
17b94a
+cleanup;
17b94a
+
17b94a
+TEST glusterd
17b94a
+
17b94a
+TEST $CLI volume create $V0 $H0:$B0/brick1;
17b94a
+EXPECT 'Created' volinfo_field $V0 'Status';
17b94a
+
17b94a
+TEST $CLI volume start $V0;
17b94a
+EXPECT 'Started' volinfo_field $V0 'Status';
17b94a
+
17b94a
+logdir=`gluster --print-logdir`
17b94a
+
17b94a
+TEST build_tester $(dirname $0)/glfs_h_creat_open.c -lgfapi
17b94a
+
17b94a
+TEST ./$(dirname $0)/glfs_h_creat_open $H0 $V0  $logdir/glfs.log
17b94a
+
17b94a
+cleanup_tester $(dirname $0)/glfs_h_creat_open
17b94a
+
17b94a
+TEST $CLI volume stop $V0
17b94a
+TEST $CLI volume delete $V0
17b94a
+
17b94a
+cleanup;
17b94a
-- 
17b94a
1.8.3.1
17b94a