blk-mq: really fix plug list flushing for nomerge queues
[linux-2.6-block.git] / fs / orangefs / xattr.c
CommitLineData
1182fca3
MM
1/*
2 * (C) 2001 Clemson University and The University of Chicago
3 *
4 * See COPYING in top-level directory.
5 */
6
7/*
8 * Linux VFS extended attribute operations.
9 */
10
11#include "protocol.h"
575e9461
MM
12#include "orangefs-kernel.h"
13#include "orangefs-bufmap.h"
1182fca3
MM
14#include <linux/posix_acl_xattr.h>
15#include <linux/xattr.h>
16
17
8bb8aefd
YL
18#define SYSTEM_ORANGEFS_KEY "system.pvfs2."
19#define SYSTEM_ORANGEFS_KEY_LEN 13
1182fca3
MM
20
21/*
22 * this function returns
23 * 0 if the key corresponding to name is not meant to be printed as part
24 * of a listxattr.
25 * 1 if the key corresponding to name is meant to be returned as part of
26 * a listxattr.
8bb8aefd 27 * The ones that start SYSTEM_ORANGEFS_KEY are the ones to avoid printing.
1182fca3
MM
28 */
29static int is_reserved_key(const char *key, size_t size)
30{
31
8bb8aefd 32 if (size < SYSTEM_ORANGEFS_KEY_LEN)
1182fca3
MM
33 return 1;
34
8bb8aefd 35 return strncmp(key, SYSTEM_ORANGEFS_KEY, SYSTEM_ORANGEFS_KEY_LEN) ? 1 : 0;
1182fca3
MM
36}
37
38static inline int convert_to_internal_xattr_flags(int setxattr_flags)
39{
40 int internal_flag = 0;
41
42 if (setxattr_flags & XATTR_REPLACE) {
43 /* Attribute must exist! */
8bb8aefd 44 internal_flag = ORANGEFS_XATTR_REPLACE;
1182fca3
MM
45 } else if (setxattr_flags & XATTR_CREATE) {
46 /* Attribute must not exist */
8bb8aefd 47 internal_flag = ORANGEFS_XATTR_CREATE;
1182fca3
MM
48 }
49 return internal_flag;
50}
51
52
53/*
54 * Tries to get a specified key's attributes of a given
55 * file into a user-specified buffer. Note that the getxattr
56 * interface allows for the users to probe the size of an
57 * extended attribute by passing in a value of 0 to size.
58 * Thus our return value is always the size of the attribute
59 * unless the key does not exist for the file and/or if
60 * there were errors in fetching the attribute value.
61 */
8bb8aefd 62ssize_t orangefs_inode_getxattr(struct inode *inode, const char *prefix,
1182fca3
MM
63 const char *name, void *buffer, size_t size)
64{
8bb8aefd
YL
65 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
66 struct orangefs_kernel_op_s *new_op = NULL;
1182fca3
MM
67 ssize_t ret = -ENOMEM;
68 ssize_t length = 0;
69 int fsuid;
70 int fsgid;
71
72 gossip_debug(GOSSIP_XATTR_DEBUG,
73 "%s: prefix %s name %s, buffer_size %zd\n",
74 __func__, prefix, name, size);
75
8bb8aefd 76 if ((strlen(name) + strlen(prefix)) >= ORANGEFS_MAX_XATTR_NAMELEN) {
eeaa3d44 77 gossip_err("Invalid key length (%d)\n",
1182fca3
MM
78 (int)(strlen(name) + strlen(prefix)));
79 return -EINVAL;
80 }
81
82 fsuid = from_kuid(current_user_ns(), current_fsuid());
83 fsgid = from_kgid(current_user_ns(), current_fsgid());
84
85 gossip_debug(GOSSIP_XATTR_DEBUG,
86 "getxattr on inode %pU, name %s "
87 "(uid %o, gid %o)\n",
88 get_khandle_from_ino(inode),
89 name,
90 fsuid,
91 fsgid);
92
8bb8aefd 93 down_read(&orangefs_inode->xattr_sem);
1182fca3 94
8bb8aefd 95 new_op = op_alloc(ORANGEFS_VFS_OP_GETXATTR);
1182fca3
MM
96 if (!new_op)
97 goto out_unlock;
98
8bb8aefd 99 new_op->upcall.req.getxattr.refn = orangefs_inode->refn;
1182fca3 100 ret = snprintf((char *)new_op->upcall.req.getxattr.key,
8bb8aefd 101 ORANGEFS_MAX_XATTR_NAMELEN, "%s%s", prefix, name);
1182fca3
MM
102
103 /*
104 * NOTE: Although keys are meant to be NULL terminated textual
105 * strings, I am going to explicitly pass the length just in case
106 * we change this later on...
107 */
108 new_op->upcall.req.getxattr.key_sz = ret + 1;
109
8bb8aefd 110 ret = service_operation(new_op, "orangefs_inode_getxattr",
1182fca3
MM
111 get_interruptible_flag(inode));
112 if (ret != 0) {
113 if (ret == -ENOENT) {
114 ret = -ENODATA;
115 gossip_debug(GOSSIP_XATTR_DEBUG,
8bb8aefd 116 "orangefs_inode_getxattr: inode %pU key %s"
1182fca3
MM
117 " does not exist!\n",
118 get_khandle_from_ino(inode),
119 (char *)new_op->upcall.req.getxattr.key);
120 }
121 goto out_release_op;
122 }
123
124 /*
125 * Length returned includes null terminator.
126 */
127 length = new_op->downcall.resp.getxattr.val_sz;
128
129 /*
130 * Just return the length of the queried attribute.
131 */
132 if (size == 0) {
133 ret = length;
134 goto out_release_op;
135 }
136
137 /*
138 * Check to see if key length is > provided buffer size.
139 */
140 if (length > size) {
141 ret = -ERANGE;
142 goto out_release_op;
143 }
144
1182fca3 145 memcpy(buffer, new_op->downcall.resp.getxattr.val, length);
a9bb3ba8 146 memset(buffer + length, 0, size - length);
1182fca3 147 gossip_debug(GOSSIP_XATTR_DEBUG,
8bb8aefd 148 "orangefs_inode_getxattr: inode %pU "
1182fca3
MM
149 "key %s key_sz %d, val_len %d\n",
150 get_khandle_from_ino(inode),
151 (char *)new_op->
152 upcall.req.getxattr.key,
153 (int)new_op->
154 upcall.req.getxattr.key_sz,
155 (int)ret);
156
157 ret = length;
158
159out_release_op:
160 op_release(new_op);
161out_unlock:
8bb8aefd 162 up_read(&orangefs_inode->xattr_sem);
1182fca3
MM
163 return ret;
164}
165
8bb8aefd 166static int orangefs_inode_removexattr(struct inode *inode,
1182fca3
MM
167 const char *prefix,
168 const char *name,
169 int flags)
170{
8bb8aefd
YL
171 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
172 struct orangefs_kernel_op_s *new_op = NULL;
1182fca3
MM
173 int ret = -ENOMEM;
174
8bb8aefd
YL
175 down_write(&orangefs_inode->xattr_sem);
176 new_op = op_alloc(ORANGEFS_VFS_OP_REMOVEXATTR);
1182fca3
MM
177 if (!new_op)
178 goto out_unlock;
179
8bb8aefd 180 new_op->upcall.req.removexattr.refn = orangefs_inode->refn;
1182fca3
MM
181 /*
182 * NOTE: Although keys are meant to be NULL terminated
183 * textual strings, I am going to explicitly pass the
184 * length just in case we change this later on...
185 */
186 ret = snprintf((char *)new_op->upcall.req.removexattr.key,
8bb8aefd 187 ORANGEFS_MAX_XATTR_NAMELEN,
1182fca3
MM
188 "%s%s",
189 (prefix ? prefix : ""),
190 name);
191 new_op->upcall.req.removexattr.key_sz = ret + 1;
192
193 gossip_debug(GOSSIP_XATTR_DEBUG,
8bb8aefd 194 "orangefs_inode_removexattr: key %s, key_sz %d\n",
1182fca3
MM
195 (char *)new_op->upcall.req.removexattr.key,
196 (int)new_op->upcall.req.removexattr.key_sz);
197
198 ret = service_operation(new_op,
8bb8aefd 199 "orangefs_inode_removexattr",
1182fca3
MM
200 get_interruptible_flag(inode));
201 if (ret == -ENOENT) {
202 /*
203 * Request to replace a non-existent attribute is an error.
204 */
205 if (flags & XATTR_REPLACE)
206 ret = -ENODATA;
207 else
208 ret = 0;
209 }
210
211 gossip_debug(GOSSIP_XATTR_DEBUG,
8bb8aefd 212 "orangefs_inode_removexattr: returning %d\n", ret);
1182fca3
MM
213
214 op_release(new_op);
215out_unlock:
8bb8aefd 216 up_write(&orangefs_inode->xattr_sem);
1182fca3
MM
217 return ret;
218}
219
220/*
221 * Tries to set an attribute for a given key on a file.
222 *
223 * Returns a -ve number on error and 0 on success. Key is text, but value
224 * can be binary!
225 */
8bb8aefd 226int orangefs_inode_setxattr(struct inode *inode, const char *prefix,
1182fca3
MM
227 const char *name, const void *value, size_t size, int flags)
228{
8bb8aefd
YL
229 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
230 struct orangefs_kernel_op_s *new_op;
1182fca3
MM
231 int internal_flag = 0;
232 int ret = -ENOMEM;
233
234 gossip_debug(GOSSIP_XATTR_DEBUG,
235 "%s: prefix %s, name %s, buffer_size %zd\n",
236 __func__, prefix, name, size);
237
2d09a2ca 238 if (size >= ORANGEFS_MAX_XATTR_VALUELEN ||
1182fca3 239 flags < 0) {
8bb8aefd 240 gossip_err("orangefs_inode_setxattr: bogus values of size(%d), flags(%d)\n",
1182fca3
MM
241 (int)size,
242 flags);
243 return -EINVAL;
244 }
245
1182fca3
MM
246 internal_flag = convert_to_internal_xattr_flags(flags);
247
248 if (prefix) {
8bb8aefd 249 if (strlen(name) + strlen(prefix) >= ORANGEFS_MAX_XATTR_NAMELEN) {
1182fca3 250 gossip_err
8bb8aefd 251 ("orangefs_inode_setxattr: bogus key size (%d)\n",
1182fca3
MM
252 (int)(strlen(name) + strlen(prefix)));
253 return -EINVAL;
254 }
255 } else {
8bb8aefd 256 if (strlen(name) >= ORANGEFS_MAX_XATTR_NAMELEN) {
1182fca3 257 gossip_err
8bb8aefd 258 ("orangefs_inode_setxattr: bogus key size (%d)\n",
1182fca3
MM
259 (int)(strlen(name)));
260 return -EINVAL;
261 }
262 }
263
264 /* This is equivalent to a removexattr */
265 if (size == 0 && value == NULL) {
266 gossip_debug(GOSSIP_XATTR_DEBUG,
267 "removing xattr (%s%s)\n",
268 prefix,
269 name);
8bb8aefd 270 return orangefs_inode_removexattr(inode, prefix, name, flags);
1182fca3
MM
271 }
272
273 gossip_debug(GOSSIP_XATTR_DEBUG,
274 "setxattr on inode %pU, name %s\n",
275 get_khandle_from_ino(inode),
276 name);
277
8bb8aefd
YL
278 down_write(&orangefs_inode->xattr_sem);
279 new_op = op_alloc(ORANGEFS_VFS_OP_SETXATTR);
1182fca3
MM
280 if (!new_op)
281 goto out_unlock;
282
283
8bb8aefd 284 new_op->upcall.req.setxattr.refn = orangefs_inode->refn;
1182fca3
MM
285 new_op->upcall.req.setxattr.flags = internal_flag;
286 /*
287 * NOTE: Although keys are meant to be NULL terminated textual
288 * strings, I am going to explicitly pass the length just in
289 * case we change this later on...
290 */
291 ret = snprintf((char *)new_op->upcall.req.setxattr.keyval.key,
8bb8aefd 292 ORANGEFS_MAX_XATTR_NAMELEN,
1182fca3
MM
293 "%s%s",
294 prefix, name);
295 new_op->upcall.req.setxattr.keyval.key_sz = ret + 1;
296 memcpy(new_op->upcall.req.setxattr.keyval.val, value, size);
297 new_op->upcall.req.setxattr.keyval.val_sz = size;
298
299 gossip_debug(GOSSIP_XATTR_DEBUG,
8bb8aefd 300 "orangefs_inode_setxattr: key %s, key_sz %d "
1182fca3
MM
301 " value size %zd\n",
302 (char *)new_op->upcall.req.setxattr.keyval.key,
303 (int)new_op->upcall.req.setxattr.keyval.key_sz,
304 size);
305
306 ret = service_operation(new_op,
8bb8aefd 307 "orangefs_inode_setxattr",
1182fca3
MM
308 get_interruptible_flag(inode));
309
310 gossip_debug(GOSSIP_XATTR_DEBUG,
8bb8aefd 311 "orangefs_inode_setxattr: returning %d\n",
1182fca3
MM
312 ret);
313
314 /* when request is serviced properly, free req op struct */
315 op_release(new_op);
316out_unlock:
8bb8aefd 317 up_write(&orangefs_inode->xattr_sem);
1182fca3
MM
318 return ret;
319}
320
321/*
322 * Tries to get a specified object's keys into a user-specified buffer of a
323 * given size. Note that like the previous instances of xattr routines, this
324 * also allows you to pass in a NULL pointer and 0 size to probe the size for
325 * subsequent memory allocations. Thus our return value is always the size of
326 * all the keys unless there were errors in fetching the keys!
327 */
8bb8aefd 328ssize_t orangefs_listxattr(struct dentry *dentry, char *buffer, size_t size)
1182fca3
MM
329{
330 struct inode *inode = dentry->d_inode;
8bb8aefd
YL
331 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
332 struct orangefs_kernel_op_s *new_op;
333 __u64 token = ORANGEFS_ITERATE_START;
1182fca3
MM
334 ssize_t ret = -ENOMEM;
335 ssize_t total = 0;
1182fca3
MM
336 int count_keys = 0;
337 int key_size;
338 int i = 0;
62441fa5 339 int returned_count = 0;
1182fca3
MM
340
341 if (size > 0 && buffer == NULL) {
342 gossip_err("%s: bogus NULL pointers\n", __func__);
343 return -EINVAL;
344 }
1182fca3 345
8bb8aefd
YL
346 down_read(&orangefs_inode->xattr_sem);
347 new_op = op_alloc(ORANGEFS_VFS_OP_LISTXATTR);
1182fca3
MM
348 if (!new_op)
349 goto out_unlock;
350
351 if (buffer && size > 0)
352 memset(buffer, 0, size);
353
354try_again:
355 key_size = 0;
8bb8aefd 356 new_op->upcall.req.listxattr.refn = orangefs_inode->refn;
1182fca3
MM
357 new_op->upcall.req.listxattr.token = token;
358 new_op->upcall.req.listxattr.requested_count =
8bb8aefd 359 (size == 0) ? 0 : ORANGEFS_MAX_XATTR_LISTLEN;
1182fca3
MM
360 ret = service_operation(new_op, __func__,
361 get_interruptible_flag(inode));
362 if (ret != 0)
363 goto done;
364
365 if (size == 0) {
366 /*
367 * This is a bit of a big upper limit, but I did not want to
368 * spend too much time getting this correct, since users end
369 * up allocating memory rather than us...
370 */
371 total = new_op->downcall.resp.listxattr.returned_count *
8bb8aefd 372 ORANGEFS_MAX_XATTR_NAMELEN;
1182fca3
MM
373 goto done;
374 }
375
62441fa5
MM
376 returned_count = new_op->downcall.resp.listxattr.returned_count;
377 if (returned_count < 0 ||
378 returned_count >= ORANGEFS_MAX_XATTR_LISTLEN) {
379 gossip_err("%s: impossible value for returned_count:%d:\n",
380 __func__,
381 returned_count);
02a5cc53 382 ret = -EIO;
62441fa5
MM
383 goto done;
384 }
385
1182fca3
MM
386 /*
387 * Check to see how much can be fit in the buffer. Fit only whole keys.
388 */
62441fa5 389 for (i = 0; i < returned_count; i++) {
02a5cc53
MB
390 if (new_op->downcall.resp.listxattr.lengths[i] < 0 ||
391 new_op->downcall.resp.listxattr.lengths[i] >
392 ORANGEFS_MAX_XATTR_NAMELEN) {
393 gossip_err("%s: impossible value for lengths[%d]\n",
394 __func__,
395 new_op->downcall.resp.listxattr.lengths[i]);
396 ret = -EIO;
397 goto done;
398 }
1182fca3
MM
399 if (total + new_op->downcall.resp.listxattr.lengths[i] > size)
400 goto done;
401
402 /*
403 * Since many dumb programs try to setxattr() on our reserved
404 * xattrs this is a feeble attempt at defeating those by not
405 * listing them in the output of listxattr.. sigh
406 */
407 if (is_reserved_key(new_op->downcall.resp.listxattr.key +
408 key_size,
409 new_op->downcall.resp.
410 listxattr.lengths[i])) {
411 gossip_debug(GOSSIP_XATTR_DEBUG, "Copying key %d -> %s\n",
412 i, new_op->downcall.resp.listxattr.key +
413 key_size);
414 memcpy(buffer + total,
415 new_op->downcall.resp.listxattr.key + key_size,
416 new_op->downcall.resp.listxattr.lengths[i]);
417 total += new_op->downcall.resp.listxattr.lengths[i];
418 count_keys++;
419 } else {
420 gossip_debug(GOSSIP_XATTR_DEBUG, "[RESERVED] key %d -> %s\n",
421 i, new_op->downcall.resp.listxattr.key +
422 key_size);
423 }
424 key_size += new_op->downcall.resp.listxattr.lengths[i];
425 }
426
427 /*
428 * Since the buffer was large enough, we might have to continue
429 * fetching more keys!
430 */
431 token = new_op->downcall.resp.listxattr.token;
8bb8aefd 432 if (token != ORANGEFS_ITERATE_END)
1182fca3
MM
433 goto try_again;
434
435done:
436 gossip_debug(GOSSIP_XATTR_DEBUG, "%s: returning %d"
437 " [size of buffer %ld] (filled in %d keys)\n",
438 __func__,
439 ret ? (int)ret : (int)total,
440 (long)size,
441 count_keys);
442 op_release(new_op);
443 if (ret == 0)
444 ret = total;
445out_unlock:
8bb8aefd 446 up_read(&orangefs_inode->xattr_sem);
1182fca3
MM
447 return ret;
448}
449
8bb8aefd 450static int orangefs_xattr_set_default(const struct xattr_handler *handler,
59301226
AV
451 struct dentry *unused,
452 struct inode *inode,
8bb8aefd
YL
453 const char *name,
454 const void *buffer,
455 size_t size,
456 int flags)
1182fca3 457{
59301226 458 return orangefs_inode_setxattr(inode,
8bb8aefd 459 ORANGEFS_XATTR_NAME_DEFAULT_PREFIX,
1182fca3
MM
460 name,
461 buffer,
462 size,
463 flags);
464}
465
8bb8aefd 466static int orangefs_xattr_get_default(const struct xattr_handler *handler,
b296821a
AV
467 struct dentry *unused,
468 struct inode *inode,
8bb8aefd
YL
469 const char *name,
470 void *buffer,
471 size_t size)
1182fca3 472{
b296821a 473 return orangefs_inode_getxattr(inode,
8bb8aefd 474 ORANGEFS_XATTR_NAME_DEFAULT_PREFIX,
1182fca3
MM
475 name,
476 buffer,
477 size);
478
479}
480
8bb8aefd 481static int orangefs_xattr_set_trusted(const struct xattr_handler *handler,
59301226
AV
482 struct dentry *unused,
483 struct inode *inode,
8bb8aefd
YL
484 const char *name,
485 const void *buffer,
486 size_t size,
487 int flags)
1182fca3 488{
59301226 489 return orangefs_inode_setxattr(inode,
8bb8aefd 490 ORANGEFS_XATTR_NAME_TRUSTED_PREFIX,
1182fca3
MM
491 name,
492 buffer,
493 size,
494 flags);
495}
496
8bb8aefd 497static int orangefs_xattr_get_trusted(const struct xattr_handler *handler,
b296821a
AV
498 struct dentry *unused,
499 struct inode *inode,
8bb8aefd
YL
500 const char *name,
501 void *buffer,
502 size_t size)
1182fca3 503{
b296821a 504 return orangefs_inode_getxattr(inode,
8bb8aefd 505 ORANGEFS_XATTR_NAME_TRUSTED_PREFIX,
1182fca3
MM
506 name,
507 buffer,
508 size);
509}
510
8bb8aefd
YL
511static struct xattr_handler orangefs_xattr_trusted_handler = {
512 .prefix = ORANGEFS_XATTR_NAME_TRUSTED_PREFIX,
513 .get = orangefs_xattr_get_trusted,
514 .set = orangefs_xattr_set_trusted,
1182fca3
MM
515};
516
8bb8aefd 517static struct xattr_handler orangefs_xattr_default_handler = {
1182fca3
MM
518 /*
519 * NOTE: this is set to be the empty string.
520 * so that all un-prefixed xattrs keys get caught
521 * here!
522 */
8bb8aefd
YL
523 .prefix = ORANGEFS_XATTR_NAME_DEFAULT_PREFIX,
524 .get = orangefs_xattr_get_default,
525 .set = orangefs_xattr_set_default,
1182fca3
MM
526};
527
8bb8aefd 528const struct xattr_handler *orangefs_xattr_handlers[] = {
1182fca3
MM
529 &posix_acl_access_xattr_handler,
530 &posix_acl_default_xattr_handler,
8bb8aefd
YL
531 &orangefs_xattr_trusted_handler,
532 &orangefs_xattr_default_handler,
1182fca3
MM
533 NULL
534};