Debian Codegraph @

xlators/storage/posix/src/posix-gfid-path.c

raw symbols SCIP
1 /*
2 Copyright (c) 2017 Red Hat, Inc. <http://www.redhat.com>
3 This file is part of GlusterFS.
4
5 This file is licensed to you under your choice of the GNU Lesser
6 General Public License, version 3 or any later version (LGPLv3 or
7 later), or the GNU General Public License, version 2 (GPLv2), in all
8 cases as published by the Free Software Foundation.
9 */
10
11 #include <stdint.h>
12
13 #include <glusterfs/compat-errno.h>
14 #include <glusterfs/syscall.h>
15 #include <glusterfs/logging.h>
16 #include "posix-messages.h"
17 #include "posix-mem-types.h"
18 #include "posix-gfid-path.h"
19 #include "posix.h"
20
21 gf_boolean_t
22 posix_is_gfid2path_xattr(const char *name)
23 {
24 if (name && strncmp(GFID2PATH_XATTR_KEY_PREFIX, name,
25 GFID2PATH_XATTR_KEY_PREFIX_LENGTH) == 0)
26 return _gf_true;
27
28 return _gf_false;
29 }
30
31 static int gf_posix_xattr_enotsup_log;
32
33 int32_t
34 posix_get_gfid2path(xlator_t *this, inode_t *inode, const char *real_path,
35 int *op_errno, dict_t *dict)
36 {
37 int ret = 0;
38 char *path = NULL;
39 ssize_t size = 0;
40 char *list = NULL;
41 int32_t list_offset = 0;
42 int32_t i = 0;
43 int32_t j = 0;
44 char *paths[MAX_GFID2PATH_LINK_SUP] = {
45 NULL,
46 };
47 char *value = NULL;
48 size_t remaining_size = 0;
49 size_t bytes = 0;
50 char keybuffer[4096] = {
51 0,
52 };
53
54 uuid_t pargfid = {
55 0,
56 };
57 gf_boolean_t have_val = _gf_false;
58 struct posix_private *priv = NULL;
59 char pargfid_str[UUID_CANONICAL_FORM_LEN + 1] = {
60 0,
61 };
62 gf_boolean_t found = _gf_false;
63 int len;
64
65 priv = this->private;
66
67 if (IA_ISDIR(inode->ia_type)) {
68 ret = posix_resolve_dirgfid_to_path(inode->gfid, priv->base_path, NULL,
69 &path);
70 if (ret < 0) {
71 ret = -1;
72 goto err;
73 }
74 ret = dict_set_dynstr(dict, GFID2PATH_VIRT_XATTR_KEY, path);
75 if (ret < 0) {
76 gf_msg(this->name, GF_LOG_WARNING, -ret, P_MSG_DICT_SET_FAILED,
77 "could not set "
78 "value for key (%s)",
79 GFID2PATH_VIRT_XATTR_KEY);
80 goto err;
81 }
82 found = _gf_true;
83 } else {
84 char value_buf[8192] = {
85 0,
86 };
87 char xattr_value[8192] = {
88 0,
89 };
90 have_val = _gf_false;
91 size = sys_llistxattr(real_path, value_buf, sizeof(value_buf) - 1);
92 if (size > 0) {
93 have_val = _gf_true;
94 } else {
95 if (errno == ERANGE) {
96 gf_msg(this->name, GF_LOG_DEBUG, errno, P_MSG_XATTR_FAILED,
97 "listxattr failed due to overflow of"
98 " buffer on %s ",
99 real_path);
100 size = sys_llistxattr(real_path, NULL, 0);
101 }
102 if (size == -1) {
103 *op_errno = errno;
104 if ((errno == ENOTSUP) || (errno == ENOSYS)) {
105 GF_LOG_OCCASIONALLY(gf_posix_xattr_enotsup_log, this->name,
106 GF_LOG_WARNING,
107 "Extended attributes not "
108 "supported (try remounting"
109 " brick with 'user_xattr' "
110 "flag)");
111 } else {
112 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
113 "listxattr failed on %s", real_path);
114 }
115 goto err;
116 }
117 if (size == 0)
118 goto done;
119 }
120
121 list = GF_MALLOC(size, gf_posix_mt_char);
122 if (!list) {
123 ret = -1;
124 *op_errno = errno;
125 goto err;
126 }
127 if (have_val) {
128 memcpy(list, value_buf, size);
129 } else {
130 size = sys_llistxattr(real_path, list, size);
131 if (size < 0) {
132 ret = -1;
133 *op_errno = errno;
134 goto err;
135 }
136 }
137 remaining_size = size;
138 list_offset = 0;
139 while (remaining_size > 0) {
140 len = snprintf(keybuffer, sizeof(keybuffer), "%s",
141 list + list_offset);
142
143 if (!posix_is_gfid2path_xattr(keybuffer)) {
144 goto ignore;
145 }
146
147 found = _gf_true;
148 size = sys_lgetxattr(real_path, keybuffer, xattr_value,
149 sizeof(xattr_value) - 1);
150 if (size == -1) {
151 ret = -1;
152 *op_errno = errno;
153 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_XATTR_FAILED,
154 "getxattr failed on"
155 " %s: key = %s ",
156 real_path, keybuffer);
157 break;
158 }
159
160 /* Parse pargfid from xattr value*/
161 strncpy(pargfid_str, xattr_value, 36);
162 pargfid_str[36] = '\0';
163 gf_uuid_parse(pargfid_str, pargfid);
164
165 /* Convert pargfid to path */
166 ret = posix_resolve_dirgfid_to_path(pargfid, priv->base_path,
167 &xattr_value[37], &paths[i]);
168 if (ret == -1) {
169 *op_errno = errno;
170 gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_PGFID_OP,
171 "Parent GFID to path conversion failed"
172 " %s: key = %s parebt GFID: %s base name: %s",
173 real_path, keybuffer, pargfid_str, &xattr_value[37]);
174 /* if ret == -1, then paths[i] will not be allocated, hence
175 * no need to free the memory here
176 */
177 break;
178 }
179
180 i++;
181
182 ignore:
183 remaining_size -= (len + 1);
184 list_offset += (len + 1);
185 } /* while (remaining_size > 0) */
186
187 /* gfid2path xattr is absent in the list of xattrs */
188 if (!found) {
189 ret = -1;
190 /*
191 * ENODATA because xattr is not present in the
192 * list of xattrs. Thus the consumer should
193 * face error instead of a success and a empty
194 * string in the dict for the key.
195 */
196 *op_errno = ENODATA;
197 goto err;
198 }
199
200 /*
201 * gfid2path xattr is found in list of xattrs, but getxattr
202 * on the 1st gfid2path xattr itself failed and the while
203 * loop above broke. So there is nothing in the value. So
204 * it would be better not to send "" as the value for any
205 * key, as it is not true.
206 */
207 if (found && !i)
208 goto err; /* both errno and ret are set before beak */
209
210 /* Calculate memory to be allocated */
211 for (j = 0; j < i; j++) {
212 bytes += strlen(paths[j]);
213 if (j < i - 1)
214 bytes += strlen(priv->gfid2path_sep);
215 }
216 value = GF_CALLOC(bytes + 1, sizeof(char), gf_posix_mt_char);
217 if (!value) {
218 ret = -1;
219 *op_errno = errno;
220 goto err;
221 }
222
223 for (j = 0; j < i; j++) {
224 strcat(value, paths[j]);
225 if (j != i - 1)
226 strcat(value, priv->gfid2path_sep);
227 }
228 value[bytes] = '\0';
229
230 ret = dict_set_dynptr(dict, GFID2PATH_VIRT_XATTR_KEY, value, bytes);
231 if (ret < 0) {
232 *op_errno = -ret;
233 gf_msg(this->name, GF_LOG_ERROR, *op_errno, P_MSG_DICT_SET_FAILED,
234 "dict set operation "
235 "on %s for the key %s failed.",
236 real_path, GFID2PATH_VIRT_XATTR_KEY);
237 GF_FREE(value);
238 goto err;
239 }
240 }
241
242 done:
243 for (j = 0; j < i; j++) {
244 if (paths[j])
245 GF_FREE(paths[j]);
246 }
247 ret = 0;
248 GF_FREE(list);
249 return ret;
250 err:
251 if (path)
252 GF_FREE(path);
253 for (j = 0; j < i; j++) {
254 if (paths[j])
255 GF_FREE(paths[j]);
256 }
257 GF_FREE(list);
258 return ret;
259 }