4 Extended attribute handling.
6 Copyright (C) 2001 by Andreas Gruenbacher <a.gruenbacher@computer.org>
7 Copyright (C) 2001 SGI - Silicon Graphics, Inc <linux-xfs@oss.sgi.com>
8 Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
11 #include <linux/slab.h>
12 #include <linux/smp_lock.h>
13 #include <linux/file.h>
14 #include <linux/xattr.h>
15 #include <linux/namei.h>
16 #include <linux/security.h>
17 #include <linux/syscalls.h>
18 #include <linux/module.h>
19 #include <linux/fsnotify.h>
20 #include <asm/uaccess.h>
23 * Extended attribute SET operations
26 setxattr(struct dentry
*d
, char __user
*name
, void __user
*value
,
27 size_t size
, int flags
)
31 char kname
[XATTR_NAME_MAX
+ 1];
33 if (flags
& ~(XATTR_CREATE
|XATTR_REPLACE
))
36 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
37 if (error
== 0 || error
== sizeof(kname
))
43 if (size
> XATTR_SIZE_MAX
)
45 kvalue
= kmalloc(size
, GFP_KERNEL
);
48 if (copy_from_user(kvalue
, value
, size
)) {
54 down(&d
->d_inode
->i_sem
);
55 error
= security_inode_setxattr(d
, kname
, kvalue
, size
, flags
);
59 if (d
->d_inode
->i_op
&& d
->d_inode
->i_op
->setxattr
) {
60 error
= d
->d_inode
->i_op
->setxattr(d
, kname
, kvalue
,
64 security_inode_post_setxattr(d
, kname
, kvalue
,
67 } else if (!strncmp(kname
, XATTR_SECURITY_PREFIX
,
68 sizeof XATTR_SECURITY_PREFIX
- 1)) {
69 const char *suffix
= kname
+ sizeof XATTR_SECURITY_PREFIX
- 1;
70 error
= security_inode_setsecurity(d
->d_inode
, suffix
, kvalue
,
76 up(&d
->d_inode
->i_sem
);
82 sys_setxattr(char __user
*path
, char __user
*name
, void __user
*value
,
83 size_t size
, int flags
)
88 error
= user_path_walk(path
, &nd
);
91 error
= setxattr(nd
.dentry
, name
, value
, size
, flags
);
97 sys_lsetxattr(char __user
*path
, char __user
*name
, void __user
*value
,
98 size_t size
, int flags
)
103 error
= user_path_walk_link(path
, &nd
);
106 error
= setxattr(nd
.dentry
, name
, value
, size
, flags
);
112 sys_fsetxattr(int fd
, char __user
*name
, void __user
*value
,
113 size_t size
, int flags
)
121 error
= setxattr(f
->f_dentry
, name
, value
, size
, flags
);
127 * Extended attribute GET operations
130 getxattr(struct dentry
*d
, char __user
*name
, void __user
*value
, size_t size
)
134 char kname
[XATTR_NAME_MAX
+ 1];
136 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
137 if (error
== 0 || error
== sizeof(kname
))
143 if (size
> XATTR_SIZE_MAX
)
144 size
= XATTR_SIZE_MAX
;
145 kvalue
= kzalloc(size
, GFP_KERNEL
);
150 error
= security_inode_getxattr(d
, kname
);
154 if (d
->d_inode
->i_op
&& d
->d_inode
->i_op
->getxattr
)
155 error
= d
->d_inode
->i_op
->getxattr(d
, kname
, kvalue
, size
);
157 if (!strncmp(kname
, XATTR_SECURITY_PREFIX
,
158 sizeof XATTR_SECURITY_PREFIX
- 1)) {
159 const char *suffix
= kname
+ sizeof XATTR_SECURITY_PREFIX
- 1;
160 int rv
= security_inode_getsecurity(d
->d_inode
, suffix
, kvalue
,
162 /* Security module active: overwrite error value */
163 if (rv
!= -EOPNOTSUPP
)
167 if (size
&& copy_to_user(value
, kvalue
, error
))
169 } else if (error
== -ERANGE
&& size
>= XATTR_SIZE_MAX
) {
170 /* The file system tried to returned a value bigger
171 than XATTR_SIZE_MAX bytes. Not possible. */
180 sys_getxattr(char __user
*path
, char __user
*name
, void __user
*value
,
186 error
= user_path_walk(path
, &nd
);
189 error
= getxattr(nd
.dentry
, name
, value
, size
);
195 sys_lgetxattr(char __user
*path
, char __user
*name
, void __user
*value
,
201 error
= user_path_walk_link(path
, &nd
);
204 error
= getxattr(nd
.dentry
, name
, value
, size
);
210 sys_fgetxattr(int fd
, char __user
*name
, void __user
*value
, size_t size
)
213 ssize_t error
= -EBADF
;
218 error
= getxattr(f
->f_dentry
, name
, value
, size
);
224 * Extended attribute LIST operations
227 listxattr(struct dentry
*d
, char __user
*list
, size_t size
)
233 if (size
> XATTR_LIST_MAX
)
234 size
= XATTR_LIST_MAX
;
235 klist
= kmalloc(size
, GFP_KERNEL
);
240 error
= security_inode_listxattr(d
);
244 if (d
->d_inode
->i_op
&& d
->d_inode
->i_op
->listxattr
) {
245 error
= d
->d_inode
->i_op
->listxattr(d
, klist
, size
);
247 error
= security_inode_listsecurity(d
->d_inode
, klist
, size
);
248 if (size
&& error
>= size
)
252 if (size
&& copy_to_user(list
, klist
, error
))
254 } else if (error
== -ERANGE
&& size
>= XATTR_LIST_MAX
) {
255 /* The file system tried to returned a list bigger
256 than XATTR_LIST_MAX bytes. Not possible. */
265 sys_listxattr(char __user
*path
, char __user
*list
, size_t size
)
270 error
= user_path_walk(path
, &nd
);
273 error
= listxattr(nd
.dentry
, list
, size
);
279 sys_llistxattr(char __user
*path
, char __user
*list
, size_t size
)
284 error
= user_path_walk_link(path
, &nd
);
287 error
= listxattr(nd
.dentry
, list
, size
);
293 sys_flistxattr(int fd
, char __user
*list
, size_t size
)
296 ssize_t error
= -EBADF
;
301 error
= listxattr(f
->f_dentry
, list
, size
);
307 * Extended attribute REMOVE operations
310 removexattr(struct dentry
*d
, char __user
*name
)
313 char kname
[XATTR_NAME_MAX
+ 1];
315 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
316 if (error
== 0 || error
== sizeof(kname
))
322 if (d
->d_inode
->i_op
&& d
->d_inode
->i_op
->removexattr
) {
323 error
= security_inode_removexattr(d
, kname
);
326 down(&d
->d_inode
->i_sem
);
327 error
= d
->d_inode
->i_op
->removexattr(d
, kname
);
328 up(&d
->d_inode
->i_sem
);
337 sys_removexattr(char __user
*path
, char __user
*name
)
342 error
= user_path_walk(path
, &nd
);
345 error
= removexattr(nd
.dentry
, name
);
351 sys_lremovexattr(char __user
*path
, char __user
*name
)
356 error
= user_path_walk_link(path
, &nd
);
359 error
= removexattr(nd
.dentry
, name
);
365 sys_fremovexattr(int fd
, char __user
*name
)
373 error
= removexattr(f
->f_dentry
, name
);
380 strcmp_prefix(const char *a
, const char *a_prefix
)
382 while (*a_prefix
&& *a
== *a_prefix
) {
386 return *a_prefix
? NULL
: a
;
390 * In order to implement different sets of xattr operations for each xattr
391 * prefix with the generic xattr API, a filesystem should create a
392 * null-terminated array of struct xattr_handler (one for each prefix) and
393 * hang a pointer to it off of the s_xattr field of the superblock.
395 * The generic_fooxattr() functions will use this list to dispatch xattr
396 * operations to the correct xattr_handler.
398 #define for_each_xattr_handler(handlers, handler) \
399 for ((handler) = *(handlers)++; \
401 (handler) = *(handlers)++)
404 * Find the xattr_handler with the matching prefix.
406 static struct xattr_handler
*
407 xattr_resolve_name(struct xattr_handler
**handlers
, const char **name
)
409 struct xattr_handler
*handler
;
414 for_each_xattr_handler(handlers
, handler
) {
415 const char *n
= strcmp_prefix(*name
, handler
->prefix
);
425 * Find the handler for the prefix and dispatch its get() operation.
428 generic_getxattr(struct dentry
*dentry
, const char *name
, void *buffer
, size_t size
)
430 struct xattr_handler
*handler
;
431 struct inode
*inode
= dentry
->d_inode
;
433 handler
= xattr_resolve_name(inode
->i_sb
->s_xattr
, &name
);
436 return handler
->get(inode
, name
, buffer
, size
);
440 * Combine the results of the list() operation from every xattr_handler in the
444 generic_listxattr(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
446 struct inode
*inode
= dentry
->d_inode
;
447 struct xattr_handler
*handler
, **handlers
= inode
->i_sb
->s_xattr
;
448 unsigned int size
= 0;
451 for_each_xattr_handler(handlers
, handler
)
452 size
+= handler
->list(inode
, NULL
, 0, NULL
, 0);
456 for_each_xattr_handler(handlers
, handler
) {
457 size
= handler
->list(inode
, buf
, buffer_size
, NULL
, 0);
458 if (size
> buffer_size
)
469 * Find the handler for the prefix and dispatch its set() operation.
472 generic_setxattr(struct dentry
*dentry
, const char *name
, const void *value
, size_t size
, int flags
)
474 struct xattr_handler
*handler
;
475 struct inode
*inode
= dentry
->d_inode
;
478 value
= ""; /* empty EA, do not remove */
479 handler
= xattr_resolve_name(inode
->i_sb
->s_xattr
, &name
);
482 return handler
->set(inode
, name
, value
, size
, flags
);
486 * Find the handler for the prefix and dispatch its set() operation to remove
487 * any associated extended attribute.
490 generic_removexattr(struct dentry
*dentry
, const char *name
)
492 struct xattr_handler
*handler
;
493 struct inode
*inode
= dentry
->d_inode
;
495 handler
= xattr_resolve_name(inode
->i_sb
->s_xattr
, &name
);
498 return handler
->set(inode
, name
, NULL
, 0, XATTR_REPLACE
);
501 EXPORT_SYMBOL(generic_getxattr
);
502 EXPORT_SYMBOL(generic_listxattr
);
503 EXPORT_SYMBOL(generic_setxattr
);
504 EXPORT_SYMBOL(generic_removexattr
);