summaryrefslogtreecommitdiffstats
path: root/xlators/storage/posix/src/posix-gfid-path.c
blob: 500f4d81c244b2ce2158a398fb25ec6e6f1bd692 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
   Copyright (c) 2017 Red Hat, Inc. <http://www.redhat.com>
   This file is part of GlusterFS.

   This file is licensed to you under your choice of the GNU Lesser
   General Public License, version 3 or any later version (LGPLv3 or
   later), or the GNU General Public License, version 2 (GPLv2), in all
   cases as published by the Free Software Foundation.
*/

#include "common-utils.h"
#include "xlator.h"
#include "syscall.h"
#include "logging.h"
#include "posix-messages.h"

int32_t
posix_set_gfid2path_xattr (xlator_t *this, const char *path, uuid_t pgfid,
                           const char *bname)
{
        char        xxh64[GF_XXH64_DIGEST_LENGTH*2+1] = {0,};
        char        pgfid_bname[1024]                 = {0,};
        char       *key                               = NULL;
        char       *val                               = NULL;
        size_t      key_size                          = 0;
        size_t      val_size                          = 0;
        int         ret                               = 0;

        GF_VALIDATE_OR_GOTO ("posix", this, err);

        snprintf (pgfid_bname, sizeof (pgfid_bname), "%s/%s", uuid_utoa (pgfid),
                  bname);
        gf_xxh64_wrapper ((unsigned char *) pgfid_bname,
                          strlen(pgfid_bname), GF_XXHSUM64_DEFAULT_SEED, xxh64);
        key_size = strlen(GFID2PATH_XATTR_KEY_PREFIX) + GF_XXH64_DIGEST_LENGTH*2+1;
        key = alloca (key_size);
        snprintf (key, key_size, GFID2PATH_XATTR_KEY_PREFIX"%s", xxh64);

        val_size = UUID_CANONICAL_FORM_LEN + NAME_MAX + 2;
        val = alloca (val_size);
        snprintf (val, val_size, "%s/%s", uuid_utoa (pgfid), bname);

        ret = sys_lsetxattr (path, key, val, strlen(val), XATTR_CREATE);
        if (ret == -1) {
                gf_msg (this->name, GF_LOG_WARNING, errno, P_MSG_PGFID_OP,
                        "setting gfid2path xattr failed on %s: key = %s ",
                        path, key);
                goto err;
        }
        return 0;
 err:
        return -1;
}

int32_t
posix_remove_gfid2path_xattr (xlator_t *this, const char *path,
                              uuid_t pgfid, const char *bname)
{
        char        xxh64[GF_XXH64_DIGEST_LENGTH*2+1] = {0,};
        char        pgfid_bname[1024]                 = {0,};
        int         ret                               = 0;
        char       *key                               = NULL;
        size_t      key_size                          = 0;

        GF_VALIDATE_OR_GOTO ("posix", this, err);

        snprintf (pgfid_bname, sizeof (pgfid_bname), "%s/%s", uuid_utoa (pgfid),
                  bname);
        gf_xxh64_wrapper ((unsigned char *) pgfid_bname,
                          strlen(pgfid_bname), GF_XXHSUM64_DEFAULT_SEED, xxh64);
        key_size = strlen(GFID2PATH_XATTR_KEY_PREFIX) + GF_XXH64_DIGEST_LENGTH*2+1;
        key = alloca (key_size);
        snprintf (key, key_size, GFID2PATH_XATTR_KEY_PREFIX"%s", xxh64);

        ret = sys_lremovexattr (path, key);
        if (ret == -1) {
                gf_msg (this->name, GF_LOG_WARNING, errno, P_MSG_PGFID_OP,
                        "removing gfid2path xattr failed on %s: key = %s",
                        path, key);
                goto err;
        }
        return 0;
 err:
        return -1;
}