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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "chain_xattr.h"
#include <errno.h> // for ERANGE, ENODATA, ENOMEM
#include <stdio.h> // for size_t, snprintf
#include <stdlib.h> // for free, malloc
#include <string.h> // for strcpy, strlen
#include "include/ceph_assert.h" // for assert
#include "include/buffer.h"
#if defined(__linux__)
#include <linux/fs.h>
#endif
#include "include/ceph_assert.h"
/*
* chaining xattrs
*
* In order to support xattrs that are larger than the xattr size limit that some file systems
* impose, we use multiple xattrs to store the value of a single xattr. The xattrs keys
* are set as follows:
* The first xattr in the chain, has a key that holds the original xattr name, with any '@' char
* being esacped ("@@").
* The chained keys will have the first xattr's key (with the escaping), and a suffix: "@<id>"
* where <id> marks the num of xattr in the chain.
*/
void get_raw_xattr_name(const char *name, int i, char *raw_name, int raw_len)
{
int pos = 0;
while (*name) {
switch (*name) {
case '@': /* escape it */
pos += 2;
ceph_assert (pos < raw_len - 1);
*raw_name = '@';
raw_name++;
*raw_name = '@';
break;
default:
pos++;
ceph_assert(pos < raw_len - 1);
*raw_name = *name;
break;
}
name++;
raw_name++;
}
if (!i) {
*raw_name = '\0';
} else {
int r = snprintf(raw_name, raw_len - pos, "@%d", i);
ceph_assert(r < raw_len - pos);
}
}
static int translate_raw_name(const char *raw_name, char *name, int name_len, bool *is_first)
{
int pos = 0;
*is_first = true;
while (*raw_name) {
switch (*raw_name) {
case '@': /* escape it */
raw_name++;
if (!*raw_name)
break;
if (*raw_name != '@') {
*is_first = false;
goto done;
}
/* fall through */
default:
*name = *raw_name;
break;
}
pos++;
ceph_assert(pos < name_len);
name++;
raw_name++;
}
done:
*name = '\0';
return pos;
}
// setxattr
static int getxattr_len(const char *fn, const char *name)
{
int i = 0, total = 0;
char raw_name[CHAIN_XATTR_MAX_NAME_LEN * 2 + 16];
int r;
do {
get_raw_xattr_name(name, i, raw_name, sizeof(raw_name));
r = sys_getxattr(fn, raw_name, 0, 0);
if (!i && r < 0)
return r;
if (r < 0)
break;
total += r;
i++;
} while (r == CHAIN_XATTR_MAX_BLOCK_LEN ||
r == CHAIN_XATTR_SHORT_BLOCK_LEN);
return total;
}
int chain_getxattr(const char *fn, const char *name, void *val, size_t size)
{
int i = 0, pos = 0;
char raw_name[CHAIN_XATTR_MAX_NAME_LEN * 2 + 16];
int ret = 0;
int r;
size_t chunk_size;
if (!size)
return getxattr_len(fn, name);
do {
chunk_size = size;
get_raw_xattr_name(name, i, raw_name, sizeof(raw_name));
r = sys_getxattr(fn, raw_name, (char *)val + pos, chunk_size);
if (i && r == -ENODATA) {
ret = pos;
break;
}
if (r < 0) {
ret = r;
break;
}
if (r > 0) {
pos += r;
size -= r;
}
i++;
} while (size && (r == CHAIN_XATTR_MAX_BLOCK_LEN ||
r == CHAIN_XATTR_SHORT_BLOCK_LEN));
if (r >= 0) {
ret = pos;
/* is there another chunk? that can happen if the last read size span over
exactly one block */
if (chunk_size == CHAIN_XATTR_MAX_BLOCK_LEN ||
chunk_size == CHAIN_XATTR_SHORT_BLOCK_LEN) {
get_raw_xattr_name(name, i, raw_name, sizeof(raw_name));
r = sys_getxattr(fn, raw_name, 0, 0);
if (r > 0) { // there's another chunk.. the original buffer was too small
ret = -ERANGE;
}
}
}
return ret;
}
int chain_getxattr_buf(const char *fn, const char *name, bufferptr *bp)
{
size_t size = 1024; // Initial
while (1) {
bufferptr buf(size);
int r = chain_getxattr(
fn,
name,
buf.c_str(),
size);
if (r > 0) {
buf.set_length(r);
if (bp)
bp->swap(buf);
return r;
} else if (r == 0) {
return 0;
} else {
if (r == -ERANGE) {
size *= 2;
} else {
return r;
}
}
}
ceph_abort_msg("unreachable");
return 0;
}
static int chain_fgetxattr_len(int fd, const char *name)
{
int i = 0, total = 0;
char raw_name[CHAIN_XATTR_MAX_NAME_LEN * 2 + 16];
int r;
do {
get_raw_xattr_name(name, i, raw_name, sizeof(raw_name));
r = sys_fgetxattr(fd, raw_name, 0, 0);
if (!i && r < 0)
return r;
if (r < 0)
break;
total += r;
i++;
} while (r == CHAIN_XATTR_MAX_BLOCK_LEN ||
r == CHAIN_XATTR_SHORT_BLOCK_LEN);
return total;
}
int chain_fgetxattr(int fd, const char *name, void *val, size_t size)
{
int i = 0, pos = 0;
char raw_name[CHAIN_XATTR_MAX_NAME_LEN * 2 + 16];
int ret = 0;
int r;
size_t chunk_size;
if (!size)
return chain_fgetxattr_len(fd, name);
do {
chunk_size = size;
get_raw_xattr_name(name, i, raw_name, sizeof(raw_name));
r = sys_fgetxattr(fd, raw_name, (char *)val + pos, chunk_size);
if (i && r == -ENODATA) {
ret = pos;
break;
}
if (r < 0) {
ret = r;
break;
}
if (r > 0) {
pos += r;
size -= r;
}
i++;
} while (size && (r == CHAIN_XATTR_MAX_BLOCK_LEN ||
r == CHAIN_XATTR_SHORT_BLOCK_LEN));
if (r >= 0) {
ret = pos;
/* is there another chunk? that can happen if the last read size span over
exactly one block */
if (chunk_size == CHAIN_XATTR_MAX_BLOCK_LEN ||
chunk_size == CHAIN_XATTR_SHORT_BLOCK_LEN) {
get_raw_xattr_name(name, i, raw_name, sizeof(raw_name));
r = sys_fgetxattr(fd, raw_name, 0, 0);
if (r > 0) { // there's another chunk.. the original buffer was too small
ret = -ERANGE;
}
}
}
return ret;
}
// setxattr
int get_xattr_block_size(size_t size)
{
if (size <= CHAIN_XATTR_SHORT_LEN_THRESHOLD)
// this may fit in the inode; stripe over short attrs so that XFS
// won't kick it out.
return CHAIN_XATTR_SHORT_BLOCK_LEN;
return CHAIN_XATTR_MAX_BLOCK_LEN;
}
// removexattr
int chain_removexattr(const char *fn, const char *name)
{
int i = 0;
char raw_name[CHAIN_XATTR_MAX_NAME_LEN * 2 + 16];
int r;
do {
get_raw_xattr_name(name, i, raw_name, sizeof(raw_name));
r = sys_removexattr(fn, raw_name);
if (!i && r < 0) {
return r;
}
i++;
} while (r >= 0);
return 0;
}
int chain_fremovexattr(int fd, const char *name)
{
int i = 0;
char raw_name[CHAIN_XATTR_MAX_NAME_LEN * 2 + 16];
int r;
do {
get_raw_xattr_name(name, i, raw_name, sizeof(raw_name));
r = sys_fremovexattr(fd, raw_name);
if (!i && r < 0) {
return r;
}
i++;
} while (r >= 0);
return 0;
}
// listxattr
int chain_listxattr(const char *fn, char *names, size_t len) {
int r;
if (!len)
return sys_listxattr(fn, names, len) * 2;
r = sys_listxattr(fn, 0, 0);
if (r < 0)
return r;
size_t total_len = r * 2; // should be enough
char *full_buf = (char *)malloc(total_len);
if (!full_buf)
return -ENOMEM;
r = sys_listxattr(fn, full_buf, total_len);
if (r < 0) {
free(full_buf);
return r;
}
char *p = full_buf;
const char *end = full_buf + r;
char *dest = names;
char *dest_end = names + len;
while (p < end) {
char name[CHAIN_XATTR_MAX_NAME_LEN * 2 + 16];
int attr_len = strlen(p);
bool is_first;
int name_len = translate_raw_name(p, name, sizeof(name), &is_first);
if (is_first) {
if (dest + name_len > dest_end) {
r = -ERANGE;
goto done;
}
strcpy(dest, name);
dest += name_len + 1;
}
p += attr_len + 1;
}
r = dest - names;
done:
free(full_buf);
return r;
}
int chain_flistxattr(int fd, char *names, size_t len) {
int r;
char *p;
const char * end;
char *dest;
char *dest_end;
if (!len)
return sys_flistxattr(fd, names, len) * 2;
r = sys_flistxattr(fd, 0, 0);
if (r < 0)
return r;
size_t total_len = r * 2; // should be enough
char *full_buf = (char *)malloc(total_len);
if (!full_buf)
return -ENOMEM;
r = sys_flistxattr(fd, full_buf, total_len);
if (r < 0)
goto done;
p = full_buf;
end = full_buf + r;
dest = names;
dest_end = names + len;
while (p < end) {
char name[CHAIN_XATTR_MAX_NAME_LEN * 2 + 16];
int attr_len = strlen(p);
bool is_first;
int name_len = translate_raw_name(p, name, sizeof(name), &is_first);
if (is_first) {
if (dest + name_len > dest_end) {
r = -ERANGE;
goto done;
}
strcpy(dest, name);
dest += name_len + 1;
}
p += attr_len + 1;
}
r = dest - names;
done:
free(full_buf);
return r;
}
|