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 *name
,
63 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: name %s, buffer_size %zd\n",
74 __func__
, name
, size
);
76 if (strlen(name
) >= ORANGEFS_MAX_XATTR_NAMELEN
) {
77 gossip_err("Invalid key length (%d)\n",
82 fsuid
= from_kuid(&init_user_ns
, current_fsuid());
83 fsgid
= from_kgid(&init_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 strcpy(new_op
->upcall
.req
.getxattr
.key
, name
);
103 * NOTE: Although keys are meant to be NULL terminated textual
104 * strings, I am going to explicitly pass the length just in case
105 * we change this later on...
107 new_op
->upcall
.req
.getxattr
.key_sz
= strlen(name
) + 1;
109 ret
= service_operation(new_op
, "orangefs_inode_getxattr",
110 get_interruptible_flag(inode
));
112 if (ret
== -ENOENT
) {
114 gossip_debug(GOSSIP_XATTR_DEBUG
,
115 "orangefs_inode_getxattr: inode %pU key %s"
116 " does not exist!\n",
117 get_khandle_from_ino(inode
),
118 (char *)new_op
->upcall
.req
.getxattr
.key
);
124 * Length returned includes null terminator.
126 length
= new_op
->downcall
.resp
.getxattr
.val_sz
;
129 * Just return the length of the queried attribute.
137 * Check to see if key length is > provided buffer size.
144 memcpy(buffer
, new_op
->downcall
.resp
.getxattr
.val
, length
);
145 memset(buffer
+ length
, 0, size
- length
);
146 gossip_debug(GOSSIP_XATTR_DEBUG
,
147 "orangefs_inode_getxattr: inode %pU "
148 "key %s key_sz %d, val_len %d\n",
149 get_khandle_from_ino(inode
),
151 upcall
.req
.getxattr
.key
,
153 upcall
.req
.getxattr
.key_sz
,
161 up_read(&orangefs_inode
->xattr_sem
);
165 static int orangefs_inode_removexattr(struct inode
*inode
, const char *name
,
168 struct orangefs_inode_s
*orangefs_inode
= ORANGEFS_I(inode
);
169 struct orangefs_kernel_op_s
*new_op
= NULL
;
172 down_write(&orangefs_inode
->xattr_sem
);
173 new_op
= op_alloc(ORANGEFS_VFS_OP_REMOVEXATTR
);
177 new_op
->upcall
.req
.removexattr
.refn
= orangefs_inode
->refn
;
179 * NOTE: Although keys are meant to be NULL terminated
180 * textual strings, I am going to explicitly pass the
181 * length just in case we change this later on...
183 strcpy(new_op
->upcall
.req
.removexattr
.key
, name
);
184 new_op
->upcall
.req
.removexattr
.key_sz
= strlen(name
) + 1;
186 gossip_debug(GOSSIP_XATTR_DEBUG
,
187 "orangefs_inode_removexattr: key %s, key_sz %d\n",
188 (char *)new_op
->upcall
.req
.removexattr
.key
,
189 (int)new_op
->upcall
.req
.removexattr
.key_sz
);
191 ret
= service_operation(new_op
,
192 "orangefs_inode_removexattr",
193 get_interruptible_flag(inode
));
194 if (ret
== -ENOENT
) {
196 * Request to replace a non-existent attribute is an error.
198 if (flags
& XATTR_REPLACE
)
204 gossip_debug(GOSSIP_XATTR_DEBUG
,
205 "orangefs_inode_removexattr: returning %d\n", ret
);
209 up_write(&orangefs_inode
->xattr_sem
);
214 * Tries to set an attribute for a given key on a file.
216 * Returns a -ve number on error and 0 on success. Key is text, but value
219 int orangefs_inode_setxattr(struct inode
*inode
, const char *name
,
220 const void *value
, size_t size
, int flags
)
222 struct orangefs_inode_s
*orangefs_inode
= ORANGEFS_I(inode
);
223 struct orangefs_kernel_op_s
*new_op
;
224 int internal_flag
= 0;
227 gossip_debug(GOSSIP_XATTR_DEBUG
,
228 "%s: name %s, buffer_size %zd\n",
229 __func__
, name
, size
);
231 if (size
>= ORANGEFS_MAX_XATTR_VALUELEN
||
233 gossip_err("orangefs_inode_setxattr: bogus values of size(%d), flags(%d)\n",
239 internal_flag
= convert_to_internal_xattr_flags(flags
);
241 if (strlen(name
) >= ORANGEFS_MAX_XATTR_NAMELEN
) {
243 ("orangefs_inode_setxattr: bogus key size (%d)\n",
244 (int)(strlen(name
)));
248 /* This is equivalent to a removexattr */
249 if (size
== 0 && value
== NULL
) {
250 gossip_debug(GOSSIP_XATTR_DEBUG
,
251 "removing xattr (%s)\n",
253 return orangefs_inode_removexattr(inode
, name
, flags
);
256 gossip_debug(GOSSIP_XATTR_DEBUG
,
257 "setxattr on inode %pU, name %s\n",
258 get_khandle_from_ino(inode
),
261 down_write(&orangefs_inode
->xattr_sem
);
262 new_op
= op_alloc(ORANGEFS_VFS_OP_SETXATTR
);
267 new_op
->upcall
.req
.setxattr
.refn
= orangefs_inode
->refn
;
268 new_op
->upcall
.req
.setxattr
.flags
= internal_flag
;
270 * NOTE: Although keys are meant to be NULL terminated textual
271 * strings, I am going to explicitly pass the length just in
272 * case we change this later on...
274 strcpy(new_op
->upcall
.req
.setxattr
.keyval
.key
, name
);
275 new_op
->upcall
.req
.setxattr
.keyval
.key_sz
= strlen(name
) + 1;
276 memcpy(new_op
->upcall
.req
.setxattr
.keyval
.val
, value
, size
);
277 new_op
->upcall
.req
.setxattr
.keyval
.val_sz
= size
;
279 gossip_debug(GOSSIP_XATTR_DEBUG
,
280 "orangefs_inode_setxattr: key %s, key_sz %d "
282 (char *)new_op
->upcall
.req
.setxattr
.keyval
.key
,
283 (int)new_op
->upcall
.req
.setxattr
.keyval
.key_sz
,
286 ret
= service_operation(new_op
,
287 "orangefs_inode_setxattr",
288 get_interruptible_flag(inode
));
290 gossip_debug(GOSSIP_XATTR_DEBUG
,
291 "orangefs_inode_setxattr: returning %d\n",
294 /* when request is serviced properly, free req op struct */
297 up_write(&orangefs_inode
->xattr_sem
);
302 * Tries to get a specified object's keys into a user-specified buffer of a
303 * given size. Note that like the previous instances of xattr routines, this
304 * also allows you to pass in a NULL pointer and 0 size to probe the size for
305 * subsequent memory allocations. Thus our return value is always the size of
306 * all the keys unless there were errors in fetching the keys!
308 ssize_t
orangefs_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
310 struct inode
*inode
= dentry
->d_inode
;
311 struct orangefs_inode_s
*orangefs_inode
= ORANGEFS_I(inode
);
312 struct orangefs_kernel_op_s
*new_op
;
313 __u64 token
= ORANGEFS_ITERATE_START
;
314 ssize_t ret
= -ENOMEM
;
319 int returned_count
= 0;
321 if (size
> 0 && buffer
== NULL
) {
322 gossip_err("%s: bogus NULL pointers\n", __func__
);
326 down_read(&orangefs_inode
->xattr_sem
);
327 new_op
= op_alloc(ORANGEFS_VFS_OP_LISTXATTR
);
331 if (buffer
&& size
> 0)
332 memset(buffer
, 0, size
);
336 new_op
->upcall
.req
.listxattr
.refn
= orangefs_inode
->refn
;
337 new_op
->upcall
.req
.listxattr
.token
= token
;
338 new_op
->upcall
.req
.listxattr
.requested_count
=
339 (size
== 0) ? 0 : ORANGEFS_MAX_XATTR_LISTLEN
;
340 ret
= service_operation(new_op
, __func__
,
341 get_interruptible_flag(inode
));
347 * This is a bit of a big upper limit, but I did not want to
348 * spend too much time getting this correct, since users end
349 * up allocating memory rather than us...
351 total
= new_op
->downcall
.resp
.listxattr
.returned_count
*
352 ORANGEFS_MAX_XATTR_NAMELEN
;
356 returned_count
= new_op
->downcall
.resp
.listxattr
.returned_count
;
357 if (returned_count
< 0 ||
358 returned_count
>= ORANGEFS_MAX_XATTR_LISTLEN
) {
359 gossip_err("%s: impossible value for returned_count:%d:\n",
367 * Check to see how much can be fit in the buffer. Fit only whole keys.
369 for (i
= 0; i
< returned_count
; i
++) {
370 if (new_op
->downcall
.resp
.listxattr
.lengths
[i
] < 0 ||
371 new_op
->downcall
.resp
.listxattr
.lengths
[i
] >
372 ORANGEFS_MAX_XATTR_NAMELEN
) {
373 gossip_err("%s: impossible value for lengths[%d]\n",
375 new_op
->downcall
.resp
.listxattr
.lengths
[i
]);
379 if (total
+ new_op
->downcall
.resp
.listxattr
.lengths
[i
] > size
)
383 * Since many dumb programs try to setxattr() on our reserved
384 * xattrs this is a feeble attempt at defeating those by not
385 * listing them in the output of listxattr.. sigh
387 if (is_reserved_key(new_op
->downcall
.resp
.listxattr
.key
+
389 new_op
->downcall
.resp
.
390 listxattr
.lengths
[i
])) {
391 gossip_debug(GOSSIP_XATTR_DEBUG
, "Copying key %d -> %s\n",
392 i
, new_op
->downcall
.resp
.listxattr
.key
+
394 memcpy(buffer
+ total
,
395 new_op
->downcall
.resp
.listxattr
.key
+ key_size
,
396 new_op
->downcall
.resp
.listxattr
.lengths
[i
]);
397 total
+= new_op
->downcall
.resp
.listxattr
.lengths
[i
];
400 gossip_debug(GOSSIP_XATTR_DEBUG
, "[RESERVED] key %d -> %s\n",
401 i
, new_op
->downcall
.resp
.listxattr
.key
+
404 key_size
+= new_op
->downcall
.resp
.listxattr
.lengths
[i
];
408 * Since the buffer was large enough, we might have to continue
409 * fetching more keys!
411 token
= new_op
->downcall
.resp
.listxattr
.token
;
412 if (token
!= ORANGEFS_ITERATE_END
)
416 gossip_debug(GOSSIP_XATTR_DEBUG
, "%s: returning %d"
417 " [size of buffer %ld] (filled in %d keys)\n",
419 ret
? (int)ret
: (int)total
,
426 up_read(&orangefs_inode
->xattr_sem
);
430 static int orangefs_xattr_set_default(const struct xattr_handler
*handler
,
431 struct dentry
*unused
,
438 return orangefs_inode_setxattr(inode
, name
, buffer
, size
, flags
);
441 static int orangefs_xattr_get_default(const struct xattr_handler
*handler
,
442 struct dentry
*unused
,
448 return orangefs_inode_getxattr(inode
, name
, buffer
, size
);
452 static struct xattr_handler orangefs_xattr_default_handler
= {
453 .prefix
= "", /* match any name => handlers called with full name */
454 .get
= orangefs_xattr_get_default
,
455 .set
= orangefs_xattr_set_default
,
458 const struct xattr_handler
*orangefs_xattr_handlers
[] = {
459 &posix_acl_access_xattr_handler
,
460 &posix_acl_default_xattr_handler
,
461 &orangefs_xattr_default_handler
,