2 * (C) 2001 Clemson University and The University of Chicago
4 * See COPYING in top-level directory.
8 * Linux VFS extended attribute operations.
12 #include "orangefs-kernel.h"
13 #include "orangefs-bufmap.h"
14 #include <linux/posix_acl_xattr.h>
15 #include <linux/xattr.h>
18 #define SYSTEM_ORANGEFS_KEY "system.pvfs2."
19 #define SYSTEM_ORANGEFS_KEY_LEN 13
22 * this function returns
23 * 0 if the key corresponding to name is not meant to be printed as part
25 * 1 if the key corresponding to name is meant to be returned as part of
27 * The ones that start SYSTEM_ORANGEFS_KEY are the ones to avoid printing.
29 static int is_reserved_key(const char *key, size_t size)
32 if (size < SYSTEM_ORANGEFS_KEY_LEN)
35 return strncmp(key, SYSTEM_ORANGEFS_KEY, SYSTEM_ORANGEFS_KEY_LEN) ? 1 : 0;
38 static inline int convert_to_internal_xattr_flags(int setxattr_flags)
40 int internal_flag = 0;
42 if (setxattr_flags & XATTR_REPLACE) {
43 /* Attribute must exist! */
44 internal_flag = ORANGEFS_XATTR_REPLACE;
45 } else if (setxattr_flags & XATTR_CREATE) {
46 /* Attribute must not exist */
47 internal_flag = ORANGEFS_XATTR_CREATE;
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.
62 ssize_t orangefs_inode_getxattr(struct inode *inode, const char *prefix,
63 const char *name, void *buffer, size_t size)
65 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
66 struct orangefs_kernel_op_s *new_op = NULL;
67 ssize_t ret = -ENOMEM;
72 gossip_debug(GOSSIP_XATTR_DEBUG,
73 "%s: prefix %s name %s, buffer_size %zd\n",
74 __func__, prefix, name, size);
76 if ((strlen(name) + strlen(prefix)) >= ORANGEFS_MAX_XATTR_NAMELEN) {
77 gossip_err("Invalid key length (%d)\n",
78 (int)(strlen(name) + strlen(prefix)));
82 fsuid = from_kuid(current_user_ns(), current_fsuid());
83 fsgid = from_kgid(current_user_ns(), current_fsgid());
85 gossip_debug(GOSSIP_XATTR_DEBUG,
86 "getxattr on inode %pU, name %s "
88 get_khandle_from_ino(inode),
93 down_read(&orangefs_inode->xattr_sem);
95 new_op = op_alloc(ORANGEFS_VFS_OP_GETXATTR);
99 new_op->upcall.req.getxattr.refn = orangefs_inode->refn;
100 ret = snprintf((char *)new_op->upcall.req.getxattr.key,
101 ORANGEFS_MAX_XATTR_NAMELEN, "%s%s", prefix, name);
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...
108 new_op->upcall.req.getxattr.key_sz = ret + 1;
110 ret = service_operation(new_op, "orangefs_inode_getxattr",
111 get_interruptible_flag(inode));
113 if (ret == -ENOENT) {
115 gossip_debug(GOSSIP_XATTR_DEBUG,
116 "orangefs_inode_getxattr: inode %pU key %s"
117 " does not exist!\n",
118 get_khandle_from_ino(inode),
119 (char *)new_op->upcall.req.getxattr.key);
125 * Length returned includes null terminator.
127 length = new_op->downcall.resp.getxattr.val_sz;
130 * Just return the length of the queried attribute.
138 * Check to see if key length is > provided buffer size.
145 memcpy(buffer, new_op->downcall.resp.getxattr.val, length);
146 memset(buffer + length, 0, size - length);
147 gossip_debug(GOSSIP_XATTR_DEBUG,
148 "orangefs_inode_getxattr: inode %pU "
149 "key %s key_sz %d, val_len %d\n",
150 get_khandle_from_ino(inode),
152 upcall.req.getxattr.key,
154 upcall.req.getxattr.key_sz,
162 up_read(&orangefs_inode->xattr_sem);
166 static int orangefs_inode_removexattr(struct inode *inode,
171 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
172 struct orangefs_kernel_op_s *new_op = NULL;
175 down_write(&orangefs_inode->xattr_sem);
176 new_op = op_alloc(ORANGEFS_VFS_OP_REMOVEXATTR);
180 new_op->upcall.req.removexattr.refn = orangefs_inode->refn;
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...
186 ret = snprintf((char *)new_op->upcall.req.removexattr.key,
187 ORANGEFS_MAX_XATTR_NAMELEN,
189 (prefix ? prefix : ""),
191 new_op->upcall.req.removexattr.key_sz = ret + 1;
193 gossip_debug(GOSSIP_XATTR_DEBUG,
194 "orangefs_inode_removexattr: key %s, key_sz %d\n",
195 (char *)new_op->upcall.req.removexattr.key,
196 (int)new_op->upcall.req.removexattr.key_sz);
198 ret = service_operation(new_op,
199 "orangefs_inode_removexattr",
200 get_interruptible_flag(inode));
201 if (ret == -ENOENT) {
203 * Request to replace a non-existent attribute is an error.
205 if (flags & XATTR_REPLACE)
211 gossip_debug(GOSSIP_XATTR_DEBUG,
212 "orangefs_inode_removexattr: returning %d\n", ret);
216 up_write(&orangefs_inode->xattr_sem);
221 * Tries to set an attribute for a given key on a file.
223 * Returns a -ve number on error and 0 on success. Key is text, but value
226 int orangefs_inode_setxattr(struct inode *inode, const char *prefix,
227 const char *name, const void *value, size_t size, int flags)
229 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
230 struct orangefs_kernel_op_s *new_op;
231 int internal_flag = 0;
234 gossip_debug(GOSSIP_XATTR_DEBUG,
235 "%s: prefix %s, name %s, buffer_size %zd\n",
236 __func__, prefix, name, size);
238 if (size >= ORANGEFS_MAX_XATTR_VALUELEN ||
240 gossip_err("orangefs_inode_setxattr: bogus values of size(%d), flags(%d)\n",
246 internal_flag = convert_to_internal_xattr_flags(flags);
249 if (strlen(name) + strlen(prefix) >= ORANGEFS_MAX_XATTR_NAMELEN) {
251 ("orangefs_inode_setxattr: bogus key size (%d)\n",
252 (int)(strlen(name) + strlen(prefix)));
256 if (strlen(name) >= ORANGEFS_MAX_XATTR_NAMELEN) {
258 ("orangefs_inode_setxattr: bogus key size (%d)\n",
259 (int)(strlen(name)));
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",
270 return orangefs_inode_removexattr(inode, prefix, name, flags);
273 gossip_debug(GOSSIP_XATTR_DEBUG,
274 "setxattr on inode %pU, name %s\n",
275 get_khandle_from_ino(inode),
278 down_write(&orangefs_inode->xattr_sem);
279 new_op = op_alloc(ORANGEFS_VFS_OP_SETXATTR);
284 new_op->upcall.req.setxattr.refn = orangefs_inode->refn;
285 new_op->upcall.req.setxattr.flags = internal_flag;
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...
291 ret = snprintf((char *)new_op->upcall.req.setxattr.keyval.key,
292 ORANGEFS_MAX_XATTR_NAMELEN,
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;
299 gossip_debug(GOSSIP_XATTR_DEBUG,
300 "orangefs_inode_setxattr: key %s, key_sz %d "
302 (char *)new_op->upcall.req.setxattr.keyval.key,
303 (int)new_op->upcall.req.setxattr.keyval.key_sz,
306 ret = service_operation(new_op,
307 "orangefs_inode_setxattr",
308 get_interruptible_flag(inode));
310 gossip_debug(GOSSIP_XATTR_DEBUG,
311 "orangefs_inode_setxattr: returning %d\n",
314 /* when request is serviced properly, free req op struct */
317 up_write(&orangefs_inode->xattr_sem);
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!
328 ssize_t orangefs_listxattr(struct dentry *dentry, char *buffer, size_t size)
330 struct inode *inode = dentry->d_inode;
331 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
332 struct orangefs_kernel_op_s *new_op;
333 __u64 token = ORANGEFS_ITERATE_START;
334 ssize_t ret = -ENOMEM;
339 int returned_count = 0;
341 if (size > 0 && buffer == NULL) {
342 gossip_err("%s: bogus NULL pointers\n", __func__);
346 down_read(&orangefs_inode->xattr_sem);
347 new_op = op_alloc(ORANGEFS_VFS_OP_LISTXATTR);
351 if (buffer && size > 0)
352 memset(buffer, 0, size);
356 new_op->upcall.req.listxattr.refn = orangefs_inode->refn;
357 new_op->upcall.req.listxattr.token = token;
358 new_op->upcall.req.listxattr.requested_count =
359 (size == 0) ? 0 : ORANGEFS_MAX_XATTR_LISTLEN;
360 ret = service_operation(new_op, __func__,
361 get_interruptible_flag(inode));
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...
371 total = new_op->downcall.resp.listxattr.returned_count *
372 ORANGEFS_MAX_XATTR_NAMELEN;
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",
387 * Check to see how much can be fit in the buffer. Fit only whole keys.
389 for (i = 0; i < returned_count; i++) {
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",
395 new_op->downcall.resp.listxattr.lengths[i]);
399 if (total + new_op->downcall.resp.listxattr.lengths[i] > size)
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
407 if (is_reserved_key(new_op->downcall.resp.listxattr.key +
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 +
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];
420 gossip_debug(GOSSIP_XATTR_DEBUG, "[RESERVED] key %d -> %s\n",
421 i, new_op->downcall.resp.listxattr.key +
424 key_size += new_op->downcall.resp.listxattr.lengths[i];
428 * Since the buffer was large enough, we might have to continue
429 * fetching more keys!
431 token = new_op->downcall.resp.listxattr.token;
432 if (token != ORANGEFS_ITERATE_END)
436 gossip_debug(GOSSIP_XATTR_DEBUG, "%s: returning %d"
437 " [size of buffer %ld] (filled in %d keys)\n",
439 ret ? (int)ret : (int)total,
446 up_read(&orangefs_inode->xattr_sem);
450 static int orangefs_xattr_set_default(const struct xattr_handler *handler,
451 struct dentry *dentry,
457 return orangefs_inode_setxattr(dentry->d_inode,
458 ORANGEFS_XATTR_NAME_DEFAULT_PREFIX,
465 static int orangefs_xattr_get_default(const struct xattr_handler *handler,
466 struct dentry *dentry,
471 return orangefs_inode_getxattr(dentry->d_inode,
472 ORANGEFS_XATTR_NAME_DEFAULT_PREFIX,
479 static int orangefs_xattr_set_trusted(const struct xattr_handler *handler,
480 struct dentry *dentry,
486 return orangefs_inode_setxattr(dentry->d_inode,
487 ORANGEFS_XATTR_NAME_TRUSTED_PREFIX,
494 static int orangefs_xattr_get_trusted(const struct xattr_handler *handler,
495 struct dentry *dentry,
500 return orangefs_inode_getxattr(dentry->d_inode,
501 ORANGEFS_XATTR_NAME_TRUSTED_PREFIX,
507 static struct xattr_handler orangefs_xattr_trusted_handler = {
508 .prefix = ORANGEFS_XATTR_NAME_TRUSTED_PREFIX,
509 .get = orangefs_xattr_get_trusted,
510 .set = orangefs_xattr_set_trusted,
513 static struct xattr_handler orangefs_xattr_default_handler = {
515 * NOTE: this is set to be the empty string.
516 * so that all un-prefixed xattrs keys get caught
519 .prefix = ORANGEFS_XATTR_NAME_DEFAULT_PREFIX,
520 .get = orangefs_xattr_get_default,
521 .set = orangefs_xattr_set_default,
524 const struct xattr_handler *orangefs_xattr_handlers[] = {
525 &posix_acl_access_xattr_handler,
526 &posix_acl_default_xattr_handler,
527 &orangefs_xattr_trusted_handler,
528 &orangefs_xattr_default_handler,