[Gluster-devel] Get GFID for a file through libgfapi

Niels de Vos ndevos at redhat.com
Tue Jan 10 09:25:35 UTC 2017


On Mon, Jan 09, 2017 at 08:58:07PM +0000, Ankireddypalle Reddy wrote:
> Neils,
>             Thanks for pointing to the sample code. The use case is to use  GFID as a unique key for maintaining indexing information about a file when it is being backed up from GlusterFS storage.  Extending GFAPI to extract GFID for a file would be great.
>              As  per the example If I need to find GFID for a path p1/p2/p3/p4 on a glusterfs volume then should I do a look up for every level?
>              LOOKUP (/)->LOOKUP(p1)-> LOOKUP(p2)-> LOOKUP(p3)-> LOOKUP(p4)

No, that is not required. You can use glfs_h_lookupat() with the full
path. Note that glfs_h_extract_handle() just does a memcpy() of the GFID
into the given (unsigned char*), the format is of 'uuid_t'.

Attached is the modified test that shows the UUID without the need for a
lookup of each component of the directory (a LOOKUP will be done by
gfapi if needed).

  $ make CFLAGS="-lgfapi -luuid" resolve
  cc -lgfapi -luuid    resolve.c   -o resolve
  $ ./resolve storage.example.com media resolve.log
  Starting libgfapi_fini
  glfs_set_volfile_server : returned 0
  glfs_set_logging : returned 0
  glfs_init : returned 0
  glfs_set_volfile_server : returned 0
  glfs_set_logging : returned 0
  glfs_init : returned 0
  glfs_h_extract_handle : returned 0
  UUID of /installation/CentOS-7-x86_64-Everything-1503-01.iso: b1b20352-c71c-4579-b678-a7a38b0e9a84
  glfs_fini : returned 0
  End of libgfapi_fini
  
  $ getfattr -n glusterfs.gfid -ehex /lan/storage.example.com/media/installation/CentOS-7-x86_64-Everything-1503-01.iso
  getfattr: Removing leading '/' from absolute path names
  # file: lan/storage.example.com/media/installation/CentOS-7-x86_64-Everything-1503-01.iso
  glusterfs.gfid=0xb1b20352c71c4579b678a7a38b0e9a84

HTH,
Niels


> 
> Thanks and Regards,
> Ram
> 
> 
> -----Original Message-----
> From: Niels de Vos [mailto:ndevos at redhat.com] 
> Sent: Monday, January 09, 2017 3:39 PM
> To: Ankireddypalle Reddy
> Cc: Gluster Devel (gluster-devel at gluster.org); integration at gluster.org
> Subject: Re: [Gluster-devel] Get GFID for a file through libgfapi
> 
> On Mon, Jan 09, 2017 at 05:53:03PM +0000, Ankireddypalle Reddy wrote:
> > Hi,
> >         I am trying to extract the GFID for a file through libgfapi interface. When I try to extract the value of extended attribute glusterfs.gfid through libgfapi I get the errorno: 95.  This works for FUSE though. Is there a way to extract the GFID for a file through libgfapi.
> 
> It seems that this is a case where FUSE handles the xatts special. The glusterfs.gfid and glusterfs.gfid.string (VIRTUAL_GFID_XATTR_KEY and
> VIRTUAL_GFID_XATTR_KEY_STR) are specifically handled in xlators/mount/fuse/src/fuse-bridge.c.
> 
> There is a way to get the GFID, but it probably is rather a cumbersome workaround for you. The handle-API is used heavily by NFS-Ganesha (because NFS uses filehandles more than filenames), and extracts the GFID from the 'struct glfs_object' with glfs_h_extract_handle(). A basic example of how to obtain and extract the handle is in https://github.com/gluster/glusterfs/blob/master/tests/basic/gfapi/bug1291259.c
> 
> Could you explain the need for knowing the GFID in the application? We can extend gfapi with fetching the GFID if that would help you.
> 
> Niels
> 
> 
> PS: we have a new integration at gluster.org where external projects can ask gfapi related questions. The gluster-devel list tends to be a little heavy on traffic for non-Gluster developers.
> ***************************Legal Disclaimer***************************
> "This communication may contain confidential and privileged material for the
> sole use of the intended recipient. Any unauthorized review, use or distribution
> by others is strictly prohibited. If you have received the message by mistake,
> please advise the sender by reply email and delete the message. Thank you."
> **********************************************************************
> 
-------------- next part --------------
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <glusterfs/api/glfs.h>
#include <glusterfs/api/glfs-handles.h>
#include <uuid/uuid.h>

int gfapi = 1;

#define LOG_ERR(func, ret) do { \
        if (ret != 0) {            \
                fprintf (stderr, "%s : returned error ret(%d), errno(%d)\n", \
                         func, ret, errno); \
                exit(1); \
        } else { \
                fprintf (stderr, "%s : returned %d\n", func, ret); \
        } \
        } while (0)
#define LOG_IF_NO_ERR(func, ret) do { \
        if (ret == 0) {            \
                fprintf (stderr, "%s : hasn't returned error %d\n", \
                         func, ret); \
                exit(1); \
        } else { \
                fprintf (stderr, "%s : returned %d\n", func, ret); \
        } \
        } while (0)

#define GLAPI_UUID_LENGTH 16
int
main (int argc, char *argv[])
{
        glfs_t                     *fs = NULL;
        glfs_t                     *fs2 = NULL;
        int                         ret = 0, i;
        glfs_fd_t                  *fd = NULL;
        char                       *filename = "/installation/CentOS-7-x86_64-Everything-1503-01.iso";
        struct                      stat sb = {0, };
        char                       *logfile = NULL;
        char                       *volname = NULL;
        char                       *hostname = NULL;
        struct glfs_object         *root = NULL, *leaf = NULL;
//        unsigned char               globjhdl[GFAPI_HANDLE_LENGTH];
        uuid_t                      globjhdl;
        char                        gfid[37];

        fprintf (stderr, "Starting libgfapi_fini\n");
        if (argc != 4) {
                fprintf (stderr, "Invalid argument\n");
                exit(1);
        }

        hostname = argv[1];
        volname = argv[2];
        logfile = argv[3];


        fs = glfs_new (volname);
        if (!fs) {
                fprintf (stderr, "glfs_new: returned NULL\n");
                return 1;
        }

        ret = glfs_set_volfile_server (fs, "tcp", hostname, 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);

        fs2 = glfs_new (volname);
        if (!fs) {
                fprintf (stderr, "glfs_new: returned NULL\n");
                return 1;
        }

        ret = glfs_set_volfile_server (fs2, "tcp", hostname, 24007);
        LOG_ERR("glfs_set_volfile_server", ret);

        ret = glfs_set_logging (fs2, logfile, 7);
        LOG_ERR("glfs_set_logging", ret);

        ret = glfs_init (fs2);
        LOG_ERR("glfs_init", ret);

        sleep (2);
#if 0
        root = glfs_h_lookupat (fs, NULL, "/", &sb, 0);
        if (!root) {
                ret = -1;
                LOG_ERR ("glfs_h_lookupat root", ret);
        }
        leaf = glfs_h_lookupat (fs, root, filename, &sb, 0);
#endif
        leaf = glfs_h_lookupat (fs, NULL, filename, &sb, 0);
        if (!leaf) {
                ret = -1;
                LOG_IF_NO_ERR ("glfs_h_lookupat leaf", ret);
        }

        ret = glfs_h_extract_handle (leaf,
                                     (unsigned char*) globjhdl,
                                     GFAPI_HANDLE_LENGTH);
        LOG_ERR("glfs_h_extract_handle", (ret != 16));

        uuid_unparse (globjhdl, gfid);
        fprintf (stderr, "UUID of %s: %s\n", filename, gfid);

        ret = glfs_fini(fs);
        LOG_ERR("glfs_fini", ret);

        fprintf (stderr, "End of libgfapi_fini\n");

        exit(0);
}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://www.gluster.org/pipermail/gluster-devel/attachments/20170110/8eb17823/attachment.sig>


More information about the Gluster-devel mailing list