]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/xattr.c
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
);
83 sys_setxattr(char __user
*path
, char __user
*name
, void __user
*value
,
84 size_t size
, int flags
)
89 error
= user_path_walk(path
, &nd
);
92 error
= setxattr(nd
.dentry
, name
, value
, size
, flags
);
98 sys_lsetxattr(char __user
*path
, char __user
*name
, void __user
*value
,
99 size_t size
, int flags
)
104 error
= user_path_walk_link(path
, &nd
);
107 error
= setxattr(nd
.dentry
, name
, value
, size
, flags
);
113 sys_fsetxattr(int fd
, char __user
*name
, void __user
*value
,
114 size_t size
, int flags
)
122 error
= setxattr(f
->f_dentry
, name
, value
, size
, flags
);
128 * Extended attribute GET operations
131 getxattr(struct dentry
*d
, char __user
*name
, void __user
*value
, size_t size
)
135 char kname
[XATTR_NAME_MAX
+ 1];
137 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
138 if (error
== 0 || error
== sizeof(kname
))
144 if (size
> XATTR_SIZE_MAX
)
145 size
= XATTR_SIZE_MAX
;
146 kvalue
= kzalloc(size
, GFP_KERNEL
);
151 error
= security_inode_getxattr(d
, kname
);
155 if (d
->d_inode
->i_op
&& d
->d_inode
->i_op
->getxattr
)
156 error
= d
->d_inode
->i_op
->getxattr(d
, kname
, kvalue
, size
);
158 if (!strncmp(kname
, XATTR_SECURITY_PREFIX
,
159 sizeof XATTR_SECURITY_PREFIX
- 1)) {
160 const char *suffix
= kname
+ sizeof XATTR_SECURITY_PREFIX
- 1;
161 int rv
= security_inode_getsecurity(d
->d_inode
, suffix
, kvalue
,
163 /* Security module active: overwrite error value */
164 if (rv
!= -EOPNOTSUPP
)
168 if (size
&& copy_to_user(value
, kvalue
, error
))
170 } else if (error
== -ERANGE
&& size
>= XATTR_SIZE_MAX
) {
171 /* The file system tried to returned a value bigger
172 than XATTR_SIZE_MAX bytes. Not possible. */
182 sys_getxattr(char __user
*path
, char __user
*name
, void __user
*value
,
188 error
= user_path_walk(path
, &nd
);
191 error
= getxattr(nd
.dentry
, name
, value
, size
);
197 sys_lgetxattr(char __user
*path
, char __user
*name
, void __user
*value
,
203 error
= user_path_walk_link(path
, &nd
);
206 error
= getxattr(nd
.dentry
, name
, value
, size
);
212 sys_fgetxattr(int fd
, char __user
*name
, void __user
*value
, size_t size
)
215 ssize_t error
= -EBADF
;
220 error
= getxattr(f
->f_dentry
, name
, value
, size
);
226 * Extended attribute LIST operations
229 listxattr(struct dentry
*d
, char __user
*list
, size_t size
)
235 if (size
> XATTR_LIST_MAX
)
236 size
= XATTR_LIST_MAX
;
237 klist
= kmalloc(size
, GFP_KERNEL
);
242 error
= security_inode_listxattr(d
);
246 if (d
->d_inode
->i_op
&& d
->d_inode
->i_op
->listxattr
) {
247 error
= d
->d_inode
->i_op
->listxattr(d
, klist
, size
);
249 error
= security_inode_listsecurity(d
->d_inode
, klist
, size
);
250 if (size
&& error
>= size
)
254 if (size
&& copy_to_user(list
, klist
, error
))
256 } else if (error
== -ERANGE
&& size
>= XATTR_LIST_MAX
) {
257 /* The file system tried to returned a list bigger
258 than XATTR_LIST_MAX bytes. Not possible. */
268 sys_listxattr(char __user
*path
, char __user
*list
, size_t size
)
273 error
= user_path_walk(path
, &nd
);
276 error
= listxattr(nd
.dentry
, list
, size
);
282 sys_llistxattr(char __user
*path
, char __user
*list
, size_t size
)
287 error
= user_path_walk_link(path
, &nd
);
290 error
= listxattr(nd
.dentry
, list
, size
);
296 sys_flistxattr(int fd
, char __user
*list
, size_t size
)
299 ssize_t error
= -EBADF
;
304 error
= listxattr(f
->f_dentry
, list
, size
);
310 * Extended attribute REMOVE operations
313 removexattr(struct dentry
*d
, char __user
*name
)
316 char kname
[XATTR_NAME_MAX
+ 1];
318 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
319 if (error
== 0 || error
== sizeof(kname
))
325 if (d
->d_inode
->i_op
&& d
->d_inode
->i_op
->removexattr
) {
326 error
= security_inode_removexattr(d
, kname
);
329 down(&d
->d_inode
->i_sem
);
330 error
= d
->d_inode
->i_op
->removexattr(d
, kname
);
331 up(&d
->d_inode
->i_sem
);
340 sys_removexattr(char __user
*path
, char __user
*name
)
345 error
= user_path_walk(path
, &nd
);
348 error
= removexattr(nd
.dentry
, name
);
354 sys_lremovexattr(char __user
*path
, char __user
*name
)
359 error
= user_path_walk_link(path
, &nd
);
362 error
= removexattr(nd
.dentry
, name
);
368 sys_fremovexattr(int fd
, char __user
*name
)
376 error
= removexattr(f
->f_dentry
, name
);
383 strcmp_prefix(const char *a
, const char *a_prefix
)
385 while (*a_prefix
&& *a
== *a_prefix
) {
389 return *a_prefix
? NULL
: a
;
393 * In order to implement different sets of xattr operations for each xattr
394 * prefix with the generic xattr API, a filesystem should create a
395 * null-terminated array of struct xattr_handler (one for each prefix) and
396 * hang a pointer to it off of the s_xattr field of the superblock.
398 * The generic_fooxattr() functions will use this list to dispatch xattr
399 * operations to the correct xattr_handler.
401 #define for_each_xattr_handler(handlers, handler) \
402 for ((handler) = *(handlers)++; \
404 (handler) = *(handlers)++)
407 * Find the xattr_handler with the matching prefix.
409 static struct xattr_handler
*
410 xattr_resolve_name(struct xattr_handler
**handlers
, const char **name
)
412 struct xattr_handler
*handler
;
417 for_each_xattr_handler(handlers
, handler
) {
418 const char *n
= strcmp_prefix(*name
, handler
->prefix
);
428 * Find the handler for the prefix and dispatch its get() operation.
431 generic_getxattr(struct dentry
*dentry
, const char *name
, void *buffer
, size_t size
)
433 struct xattr_handler
*handler
;
434 struct inode
*inode
= dentry
->d_inode
;
436 handler
= xattr_resolve_name(inode
->i_sb
->s_xattr
, &name
);
439 return handler
->get(inode
, name
, buffer
, size
);
443 * Combine the results of the list() operation from every xattr_handler in the
447 generic_listxattr(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
449 struct inode
*inode
= dentry
->d_inode
;
450 struct xattr_handler
*handler
, **handlers
= inode
->i_sb
->s_xattr
;
451 unsigned int size
= 0;
454 for_each_xattr_handler(handlers
, handler
)
455 size
+= handler
->list(inode
, NULL
, 0, NULL
, 0);
459 for_each_xattr_handler(handlers
, handler
) {
460 size
= handler
->list(inode
, buf
, buffer_size
, NULL
, 0);
461 if (size
> buffer_size
)
472 * Find the handler for the prefix and dispatch its set() operation.
475 generic_setxattr(struct dentry
*dentry
, const char *name
, const void *value
, size_t size
, int flags
)
477 struct xattr_handler
*handler
;
478 struct inode
*inode
= dentry
->d_inode
;
481 value
= ""; /* empty EA, do not remove */
482 handler
= xattr_resolve_name(inode
->i_sb
->s_xattr
, &name
);
485 return handler
->set(inode
, name
, value
, size
, flags
);
489 * Find the handler for the prefix and dispatch its set() operation to remove
490 * any associated extended attribute.
493 generic_removexattr(struct dentry
*dentry
, const char *name
)
495 struct xattr_handler
*handler
;
496 struct inode
*inode
= dentry
->d_inode
;
498 handler
= xattr_resolve_name(inode
->i_sb
->s_xattr
, &name
);
501 return handler
->set(inode
, name
, NULL
, 0, XATTR_REPLACE
);
504 EXPORT_SYMBOL(generic_getxattr
);
505 EXPORT_SYMBOL(generic_listxattr
);
506 EXPORT_SYMBOL(generic_setxattr
);
507 EXPORT_SYMBOL(generic_removexattr
);