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