1 // SPDX-License-Identifier: GPL-2.0
3 * (C) 2001 Clemson University and The University of Chicago
5 * See COPYING in top-level directory.
9 * Linux VFS extended attribute operations.
13 #include "orangefs-kernel.h"
14 #include "orangefs-bufmap.h"
15 #include <linux/posix_acl_xattr.h>
16 #include <linux/xattr.h>
19 #define SYSTEM_ORANGEFS_KEY "system.pvfs2."
20 #define SYSTEM_ORANGEFS_KEY_LEN 13
23 * this function returns
24 * 0 if the key corresponding to name is not meant to be printed as part
26 * 1 if the key corresponding to name is meant to be returned as part of
28 * The ones that start SYSTEM_ORANGEFS_KEY are the ones to avoid printing.
30 static int is_reserved_key(const char *key, size_t size)
33 if (size < SYSTEM_ORANGEFS_KEY_LEN)
36 return strncmp(key, SYSTEM_ORANGEFS_KEY, SYSTEM_ORANGEFS_KEY_LEN) ? 1 : 0;
39 static inline int convert_to_internal_xattr_flags(int setxattr_flags)
41 int internal_flag = 0;
43 if (setxattr_flags & XATTR_REPLACE) {
44 /* Attribute must exist! */
45 internal_flag = ORANGEFS_XATTR_REPLACE;
46 } else if (setxattr_flags & XATTR_CREATE) {
47 /* Attribute must not exist */
48 internal_flag = ORANGEFS_XATTR_CREATE;
55 * Tries to get a specified key's attributes of a given
56 * file into a user-specified buffer. Note that the getxattr
57 * interface allows for the users to probe the size of an
58 * extended attribute by passing in a value of 0 to size.
59 * Thus our return value is always the size of the attribute
60 * unless the key does not exist for the file and/or if
61 * there were errors in fetching the attribute value.
63 ssize_t orangefs_inode_getxattr(struct inode *inode, const char *name,
64 void *buffer, size_t size)
66 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
67 struct orangefs_kernel_op_s *new_op = NULL;
68 ssize_t ret = -ENOMEM;
73 gossip_debug(GOSSIP_XATTR_DEBUG,
74 "%s: name %s, buffer_size %zd\n",
75 __func__, name, size);
77 if (S_ISLNK(inode->i_mode))
80 if (strlen(name) >= ORANGEFS_MAX_XATTR_NAMELEN)
83 fsuid = from_kuid(&init_user_ns, current_fsuid());
84 fsgid = from_kgid(&init_user_ns, current_fsgid());
86 gossip_debug(GOSSIP_XATTR_DEBUG,
87 "getxattr on inode %pU, name %s "
89 get_khandle_from_ino(inode),
94 down_read(&orangefs_inode->xattr_sem);
96 new_op = op_alloc(ORANGEFS_VFS_OP_GETXATTR);
100 new_op->upcall.req.getxattr.refn = orangefs_inode->refn;
101 strcpy(new_op->upcall.req.getxattr.key, 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 = strlen(name) + 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, const char *name,
169 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
170 struct orangefs_kernel_op_s *new_op = NULL;
173 if (strlen(name) >= ORANGEFS_MAX_XATTR_NAMELEN)
176 down_write(&orangefs_inode->xattr_sem);
177 new_op = op_alloc(ORANGEFS_VFS_OP_REMOVEXATTR);
181 new_op->upcall.req.removexattr.refn = orangefs_inode->refn;
183 * NOTE: Although keys are meant to be NULL terminated
184 * textual strings, I am going to explicitly pass the
185 * length just in case we change this later on...
187 strcpy(new_op->upcall.req.removexattr.key, name);
188 new_op->upcall.req.removexattr.key_sz = strlen(name) + 1;
190 gossip_debug(GOSSIP_XATTR_DEBUG,
191 "orangefs_inode_removexattr: key %s, key_sz %d\n",
192 (char *)new_op->upcall.req.removexattr.key,
193 (int)new_op->upcall.req.removexattr.key_sz);
195 ret = service_operation(new_op,
196 "orangefs_inode_removexattr",
197 get_interruptible_flag(inode));
198 if (ret == -ENOENT) {
200 * Request to replace a non-existent attribute is an error.
202 if (flags & XATTR_REPLACE)
208 gossip_debug(GOSSIP_XATTR_DEBUG,
209 "orangefs_inode_removexattr: returning %d\n", ret);
213 up_write(&orangefs_inode->xattr_sem);
218 * Tries to set an attribute for a given key on a file.
220 * Returns a -ve number on error and 0 on success. Key is text, but value
223 int orangefs_inode_setxattr(struct inode *inode, const char *name,
224 const void *value, size_t size, int flags)
226 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
227 struct orangefs_kernel_op_s *new_op;
228 int internal_flag = 0;
231 gossip_debug(GOSSIP_XATTR_DEBUG,
232 "%s: name %s, buffer_size %zd\n",
233 __func__, name, size);
235 if (size > ORANGEFS_MAX_XATTR_VALUELEN)
237 if (strlen(name) >= ORANGEFS_MAX_XATTR_NAMELEN)
240 internal_flag = convert_to_internal_xattr_flags(flags);
242 /* This is equivalent to a removexattr */
243 if (size == 0 && !value) {
244 gossip_debug(GOSSIP_XATTR_DEBUG,
245 "removing xattr (%s)\n",
247 return orangefs_inode_removexattr(inode, name, flags);
250 gossip_debug(GOSSIP_XATTR_DEBUG,
251 "setxattr on inode %pU, name %s\n",
252 get_khandle_from_ino(inode),
255 down_write(&orangefs_inode->xattr_sem);
256 new_op = op_alloc(ORANGEFS_VFS_OP_SETXATTR);
261 new_op->upcall.req.setxattr.refn = orangefs_inode->refn;
262 new_op->upcall.req.setxattr.flags = internal_flag;
264 * NOTE: Although keys are meant to be NULL terminated textual
265 * strings, I am going to explicitly pass the length just in
266 * case we change this later on...
268 strcpy(new_op->upcall.req.setxattr.keyval.key, name);
269 new_op->upcall.req.setxattr.keyval.key_sz = strlen(name) + 1;
270 memcpy(new_op->upcall.req.setxattr.keyval.val, value, size);
271 new_op->upcall.req.setxattr.keyval.val_sz = size;
273 gossip_debug(GOSSIP_XATTR_DEBUG,
274 "orangefs_inode_setxattr: key %s, key_sz %d "
276 (char *)new_op->upcall.req.setxattr.keyval.key,
277 (int)new_op->upcall.req.setxattr.keyval.key_sz,
280 ret = service_operation(new_op,
281 "orangefs_inode_setxattr",
282 get_interruptible_flag(inode));
284 gossip_debug(GOSSIP_XATTR_DEBUG,
285 "orangefs_inode_setxattr: returning %d\n",
288 /* when request is serviced properly, free req op struct */
291 up_write(&orangefs_inode->xattr_sem);
296 * Tries to get a specified object's keys into a user-specified buffer of a
297 * given size. Note that like the previous instances of xattr routines, this
298 * also allows you to pass in a NULL pointer and 0 size to probe the size for
299 * subsequent memory allocations. Thus our return value is always the size of
300 * all the keys unless there were errors in fetching the keys!
302 ssize_t orangefs_listxattr(struct dentry *dentry, char *buffer, size_t size)
304 struct inode *inode = dentry->d_inode;
305 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
306 struct orangefs_kernel_op_s *new_op;
307 __u64 token = ORANGEFS_ITERATE_START;
308 ssize_t ret = -ENOMEM;
313 int returned_count = 0;
315 if (size > 0 && !buffer) {
316 gossip_err("%s: bogus NULL pointers\n", __func__);
320 down_read(&orangefs_inode->xattr_sem);
321 new_op = op_alloc(ORANGEFS_VFS_OP_LISTXATTR);
325 if (buffer && size > 0)
326 memset(buffer, 0, size);
330 new_op->upcall.req.listxattr.refn = orangefs_inode->refn;
331 new_op->upcall.req.listxattr.token = token;
332 new_op->upcall.req.listxattr.requested_count =
333 (size == 0) ? 0 : ORANGEFS_MAX_XATTR_LISTLEN;
334 ret = service_operation(new_op, __func__,
335 get_interruptible_flag(inode));
341 * This is a bit of a big upper limit, but I did not want to
342 * spend too much time getting this correct, since users end
343 * up allocating memory rather than us...
345 total = new_op->downcall.resp.listxattr.returned_count *
346 ORANGEFS_MAX_XATTR_NAMELEN;
350 returned_count = new_op->downcall.resp.listxattr.returned_count;
351 if (returned_count < 0 ||
352 returned_count > ORANGEFS_MAX_XATTR_LISTLEN) {
353 gossip_err("%s: impossible value for returned_count:%d:\n",
361 * Check to see how much can be fit in the buffer. Fit only whole keys.
363 for (i = 0; i < returned_count; i++) {
364 if (new_op->downcall.resp.listxattr.lengths[i] < 0 ||
365 new_op->downcall.resp.listxattr.lengths[i] >
366 ORANGEFS_MAX_XATTR_NAMELEN) {
367 gossip_err("%s: impossible value for lengths[%d]\n",
369 new_op->downcall.resp.listxattr.lengths[i]);
373 if (total + new_op->downcall.resp.listxattr.lengths[i] > size)
377 * Since many dumb programs try to setxattr() on our reserved
378 * xattrs this is a feeble attempt at defeating those by not
379 * listing them in the output of listxattr.. sigh
381 if (is_reserved_key(new_op->downcall.resp.listxattr.key +
383 new_op->downcall.resp.
384 listxattr.lengths[i])) {
385 gossip_debug(GOSSIP_XATTR_DEBUG, "Copying key %d -> %s\n",
386 i, new_op->downcall.resp.listxattr.key +
388 memcpy(buffer + total,
389 new_op->downcall.resp.listxattr.key + key_size,
390 new_op->downcall.resp.listxattr.lengths[i]);
391 total += new_op->downcall.resp.listxattr.lengths[i];
394 gossip_debug(GOSSIP_XATTR_DEBUG, "[RESERVED] key %d -> %s\n",
395 i, new_op->downcall.resp.listxattr.key +
398 key_size += new_op->downcall.resp.listxattr.lengths[i];
402 * Since the buffer was large enough, we might have to continue
403 * fetching more keys!
405 token = new_op->downcall.resp.listxattr.token;
406 if (token != ORANGEFS_ITERATE_END)
410 gossip_debug(GOSSIP_XATTR_DEBUG, "%s: returning %d"
411 " [size of buffer %ld] (filled in %d keys)\n",
413 ret ? (int)ret : (int)total,
420 up_read(&orangefs_inode->xattr_sem);
424 static int orangefs_xattr_set_default(const struct xattr_handler *handler,
425 struct dentry *unused,
432 return orangefs_inode_setxattr(inode, name, buffer, size, flags);
435 static int orangefs_xattr_get_default(const struct xattr_handler *handler,
436 struct dentry *unused,
442 return orangefs_inode_getxattr(inode, name, buffer, size);
446 static const struct xattr_handler orangefs_xattr_default_handler = {
447 .prefix = "", /* match any name => handlers called with full name */
448 .get = orangefs_xattr_get_default,
449 .set = orangefs_xattr_set_default,
452 const struct xattr_handler *orangefs_xattr_handlers[] = {
453 &posix_acl_access_xattr_handler,
454 &posix_acl_default_xattr_handler,
455 &orangefs_xattr_default_handler,