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 (name
== NULL
|| (size
> 0 && buffer
== NULL
)) {
77 gossip_err("orangefs_inode_getxattr: bogus NULL pointers\n");
80 if ((strlen(name
) + strlen(prefix
)) >= ORANGEFS_MAX_XATTR_NAMELEN
) {
81 gossip_err("Invalid key length (%d)\n",
82 (int)(strlen(name
) + strlen(prefix
)));
86 fsuid
= from_kuid(current_user_ns(), current_fsuid());
87 fsgid
= from_kgid(current_user_ns(), current_fsgid());
89 gossip_debug(GOSSIP_XATTR_DEBUG
,
90 "getxattr on inode %pU, name %s "
92 get_khandle_from_ino(inode
),
97 down_read(&orangefs_inode
->xattr_sem
);
99 new_op
= op_alloc(ORANGEFS_VFS_OP_GETXATTR
);
103 new_op
->upcall
.req
.getxattr
.refn
= orangefs_inode
->refn
;
104 ret
= snprintf((char *)new_op
->upcall
.req
.getxattr
.key
,
105 ORANGEFS_MAX_XATTR_NAMELEN
, "%s%s", prefix
, name
);
108 * NOTE: Although keys are meant to be NULL terminated textual
109 * strings, I am going to explicitly pass the length just in case
110 * we change this later on...
112 new_op
->upcall
.req
.getxattr
.key_sz
= ret
+ 1;
114 ret
= service_operation(new_op
, "orangefs_inode_getxattr",
115 get_interruptible_flag(inode
));
117 if (ret
== -ENOENT
) {
119 gossip_debug(GOSSIP_XATTR_DEBUG
,
120 "orangefs_inode_getxattr: inode %pU key %s"
121 " does not exist!\n",
122 get_khandle_from_ino(inode
),
123 (char *)new_op
->upcall
.req
.getxattr
.key
);
129 * Length returned includes null terminator.
131 length
= new_op
->downcall
.resp
.getxattr
.val_sz
;
134 * Just return the length of the queried attribute.
142 * Check to see if key length is > provided buffer size.
149 memset(buffer
, 0, size
);
150 memcpy(buffer
, new_op
->downcall
.resp
.getxattr
.val
, length
);
151 gossip_debug(GOSSIP_XATTR_DEBUG
,
152 "orangefs_inode_getxattr: inode %pU "
153 "key %s key_sz %d, val_len %d\n",
154 get_khandle_from_ino(inode
),
156 upcall
.req
.getxattr
.key
,
158 upcall
.req
.getxattr
.key_sz
,
166 up_read(&orangefs_inode
->xattr_sem
);
170 static int orangefs_inode_removexattr(struct inode
*inode
,
175 struct orangefs_inode_s
*orangefs_inode
= ORANGEFS_I(inode
);
176 struct orangefs_kernel_op_s
*new_op
= NULL
;
179 down_write(&orangefs_inode
->xattr_sem
);
180 new_op
= op_alloc(ORANGEFS_VFS_OP_REMOVEXATTR
);
184 new_op
->upcall
.req
.removexattr
.refn
= orangefs_inode
->refn
;
186 * NOTE: Although keys are meant to be NULL terminated
187 * textual strings, I am going to explicitly pass the
188 * length just in case we change this later on...
190 ret
= snprintf((char *)new_op
->upcall
.req
.removexattr
.key
,
191 ORANGEFS_MAX_XATTR_NAMELEN
,
193 (prefix
? prefix
: ""),
195 new_op
->upcall
.req
.removexattr
.key_sz
= ret
+ 1;
197 gossip_debug(GOSSIP_XATTR_DEBUG
,
198 "orangefs_inode_removexattr: key %s, key_sz %d\n",
199 (char *)new_op
->upcall
.req
.removexattr
.key
,
200 (int)new_op
->upcall
.req
.removexattr
.key_sz
);
202 ret
= service_operation(new_op
,
203 "orangefs_inode_removexattr",
204 get_interruptible_flag(inode
));
205 if (ret
== -ENOENT
) {
207 * Request to replace a non-existent attribute is an error.
209 if (flags
& XATTR_REPLACE
)
215 gossip_debug(GOSSIP_XATTR_DEBUG
,
216 "orangefs_inode_removexattr: returning %d\n", ret
);
220 up_write(&orangefs_inode
->xattr_sem
);
225 * Tries to set an attribute for a given key on a file.
227 * Returns a -ve number on error and 0 on success. Key is text, but value
230 int orangefs_inode_setxattr(struct inode
*inode
, const char *prefix
,
231 const char *name
, const void *value
, size_t size
, int flags
)
233 struct orangefs_inode_s
*orangefs_inode
= ORANGEFS_I(inode
);
234 struct orangefs_kernel_op_s
*new_op
;
235 int internal_flag
= 0;
238 gossip_debug(GOSSIP_XATTR_DEBUG
,
239 "%s: prefix %s, name %s, buffer_size %zd\n",
240 __func__
, prefix
, name
, size
);
243 size
>= ORANGEFS_MAX_XATTR_VALUELEN
||
245 gossip_err("orangefs_inode_setxattr: bogus values of size(%d), flags(%d)\n",
252 (size
> 0 && value
== NULL
)) {
253 gossip_err("orangefs_inode_setxattr: bogus NULL pointers!\n");
257 internal_flag
= convert_to_internal_xattr_flags(flags
);
260 if (strlen(name
) + strlen(prefix
) >= ORANGEFS_MAX_XATTR_NAMELEN
) {
262 ("orangefs_inode_setxattr: bogus key size (%d)\n",
263 (int)(strlen(name
) + strlen(prefix
)));
267 if (strlen(name
) >= ORANGEFS_MAX_XATTR_NAMELEN
) {
269 ("orangefs_inode_setxattr: bogus key size (%d)\n",
270 (int)(strlen(name
)));
275 /* This is equivalent to a removexattr */
276 if (size
== 0 && value
== NULL
) {
277 gossip_debug(GOSSIP_XATTR_DEBUG
,
278 "removing xattr (%s%s)\n",
281 return orangefs_inode_removexattr(inode
, prefix
, name
, flags
);
284 gossip_debug(GOSSIP_XATTR_DEBUG
,
285 "setxattr on inode %pU, name %s\n",
286 get_khandle_from_ino(inode
),
289 down_write(&orangefs_inode
->xattr_sem
);
290 new_op
= op_alloc(ORANGEFS_VFS_OP_SETXATTR
);
295 new_op
->upcall
.req
.setxattr
.refn
= orangefs_inode
->refn
;
296 new_op
->upcall
.req
.setxattr
.flags
= internal_flag
;
298 * NOTE: Although keys are meant to be NULL terminated textual
299 * strings, I am going to explicitly pass the length just in
300 * case we change this later on...
302 ret
= snprintf((char *)new_op
->upcall
.req
.setxattr
.keyval
.key
,
303 ORANGEFS_MAX_XATTR_NAMELEN
,
306 new_op
->upcall
.req
.setxattr
.keyval
.key_sz
= ret
+ 1;
307 memcpy(new_op
->upcall
.req
.setxattr
.keyval
.val
, value
, size
);
308 new_op
->upcall
.req
.setxattr
.keyval
.val_sz
= size
;
310 gossip_debug(GOSSIP_XATTR_DEBUG
,
311 "orangefs_inode_setxattr: key %s, key_sz %d "
313 (char *)new_op
->upcall
.req
.setxattr
.keyval
.key
,
314 (int)new_op
->upcall
.req
.setxattr
.keyval
.key_sz
,
317 ret
= service_operation(new_op
,
318 "orangefs_inode_setxattr",
319 get_interruptible_flag(inode
));
321 gossip_debug(GOSSIP_XATTR_DEBUG
,
322 "orangefs_inode_setxattr: returning %d\n",
325 /* when request is serviced properly, free req op struct */
328 up_write(&orangefs_inode
->xattr_sem
);
333 * Tries to get a specified object's keys into a user-specified buffer of a
334 * given size. Note that like the previous instances of xattr routines, this
335 * also allows you to pass in a NULL pointer and 0 size to probe the size for
336 * subsequent memory allocations. Thus our return value is always the size of
337 * all the keys unless there were errors in fetching the keys!
339 ssize_t
orangefs_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
341 struct inode
*inode
= dentry
->d_inode
;
342 struct orangefs_inode_s
*orangefs_inode
= ORANGEFS_I(inode
);
343 struct orangefs_kernel_op_s
*new_op
;
344 __u64 token
= ORANGEFS_ITERATE_START
;
345 ssize_t ret
= -ENOMEM
;
350 int returned_count
= 0;
352 if (size
> 0 && buffer
== NULL
) {
353 gossip_err("%s: bogus NULL pointers\n", __func__
);
357 gossip_err("Invalid size (%d)\n", (int)size
);
361 down_read(&orangefs_inode
->xattr_sem
);
362 new_op
= op_alloc(ORANGEFS_VFS_OP_LISTXATTR
);
366 if (buffer
&& size
> 0)
367 memset(buffer
, 0, size
);
371 new_op
->upcall
.req
.listxattr
.refn
= orangefs_inode
->refn
;
372 new_op
->upcall
.req
.listxattr
.token
= token
;
373 new_op
->upcall
.req
.listxattr
.requested_count
=
374 (size
== 0) ? 0 : ORANGEFS_MAX_XATTR_LISTLEN
;
375 ret
= service_operation(new_op
, __func__
,
376 get_interruptible_flag(inode
));
382 * This is a bit of a big upper limit, but I did not want to
383 * spend too much time getting this correct, since users end
384 * up allocating memory rather than us...
386 total
= new_op
->downcall
.resp
.listxattr
.returned_count
*
387 ORANGEFS_MAX_XATTR_NAMELEN
;
391 returned_count
= new_op
->downcall
.resp
.listxattr
.returned_count
;
392 if (returned_count
< 0 ||
393 returned_count
>= ORANGEFS_MAX_XATTR_LISTLEN
) {
394 gossip_err("%s: impossible value for returned_count:%d:\n",
402 * Check to see how much can be fit in the buffer. Fit only whole keys.
404 for (i
= 0; i
< returned_count
; i
++) {
405 if (new_op
->downcall
.resp
.listxattr
.lengths
[i
] < 0 ||
406 new_op
->downcall
.resp
.listxattr
.lengths
[i
] >
407 ORANGEFS_MAX_XATTR_NAMELEN
) {
408 gossip_err("%s: impossible value for lengths[%d]\n",
410 new_op
->downcall
.resp
.listxattr
.lengths
[i
]);
414 if (total
+ new_op
->downcall
.resp
.listxattr
.lengths
[i
] > size
)
418 * Since many dumb programs try to setxattr() on our reserved
419 * xattrs this is a feeble attempt at defeating those by not
420 * listing them in the output of listxattr.. sigh
422 if (is_reserved_key(new_op
->downcall
.resp
.listxattr
.key
+
424 new_op
->downcall
.resp
.
425 listxattr
.lengths
[i
])) {
426 gossip_debug(GOSSIP_XATTR_DEBUG
, "Copying key %d -> %s\n",
427 i
, new_op
->downcall
.resp
.listxattr
.key
+
429 memcpy(buffer
+ total
,
430 new_op
->downcall
.resp
.listxattr
.key
+ key_size
,
431 new_op
->downcall
.resp
.listxattr
.lengths
[i
]);
432 total
+= new_op
->downcall
.resp
.listxattr
.lengths
[i
];
435 gossip_debug(GOSSIP_XATTR_DEBUG
, "[RESERVED] key %d -> %s\n",
436 i
, new_op
->downcall
.resp
.listxattr
.key
+
439 key_size
+= new_op
->downcall
.resp
.listxattr
.lengths
[i
];
443 * Since the buffer was large enough, we might have to continue
444 * fetching more keys!
446 token
= new_op
->downcall
.resp
.listxattr
.token
;
447 if (token
!= ORANGEFS_ITERATE_END
)
451 gossip_debug(GOSSIP_XATTR_DEBUG
, "%s: returning %d"
452 " [size of buffer %ld] (filled in %d keys)\n",
454 ret
? (int)ret
: (int)total
,
461 up_read(&orangefs_inode
->xattr_sem
);
465 static int orangefs_xattr_set_default(const struct xattr_handler
*handler
,
466 struct dentry
*dentry
,
472 return orangefs_inode_setxattr(dentry
->d_inode
,
473 ORANGEFS_XATTR_NAME_DEFAULT_PREFIX
,
480 static int orangefs_xattr_get_default(const struct xattr_handler
*handler
,
481 struct dentry
*dentry
,
486 return orangefs_inode_getxattr(dentry
->d_inode
,
487 ORANGEFS_XATTR_NAME_DEFAULT_PREFIX
,
494 static int orangefs_xattr_set_trusted(const struct xattr_handler
*handler
,
495 struct dentry
*dentry
,
501 return orangefs_inode_setxattr(dentry
->d_inode
,
502 ORANGEFS_XATTR_NAME_TRUSTED_PREFIX
,
509 static int orangefs_xattr_get_trusted(const struct xattr_handler
*handler
,
510 struct dentry
*dentry
,
515 return orangefs_inode_getxattr(dentry
->d_inode
,
516 ORANGEFS_XATTR_NAME_TRUSTED_PREFIX
,
522 static struct xattr_handler orangefs_xattr_trusted_handler
= {
523 .prefix
= ORANGEFS_XATTR_NAME_TRUSTED_PREFIX
,
524 .get
= orangefs_xattr_get_trusted
,
525 .set
= orangefs_xattr_set_trusted
,
528 static struct xattr_handler orangefs_xattr_default_handler
= {
530 * NOTE: this is set to be the empty string.
531 * so that all un-prefixed xattrs keys get caught
534 .prefix
= ORANGEFS_XATTR_NAME_DEFAULT_PREFIX
,
535 .get
= orangefs_xattr_get_default
,
536 .set
= orangefs_xattr_set_default
,
539 const struct xattr_handler
*orangefs_xattr_handlers
[] = {
540 &posix_acl_access_xattr_handler
,
541 &posix_acl_default_xattr_handler
,
542 &orangefs_xattr_trusted_handler
,
543 &orangefs_xattr_default_handler
,