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/module.h>
18 #include <asm/uaccess.h>
21 * Extended attribute SET operations
24 setxattr(struct dentry *d, char __user *name, void __user *value,
25 size_t size, int flags)
29 char kname[XATTR_NAME_MAX + 1];
31 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
34 error = strncpy_from_user(kname, name, sizeof(kname));
35 if (error == 0 || error == sizeof(kname))
41 if (size > XATTR_SIZE_MAX)
43 kvalue = kmalloc(size, GFP_KERNEL);
46 if (copy_from_user(kvalue, value, size)) {
53 if (d->d_inode->i_op && d->d_inode->i_op->setxattr) {
54 down(&d->d_inode->i_sem);
55 error = security_inode_setxattr(d, kname, kvalue, size, flags);
58 error = d->d_inode->i_op->setxattr(d, kname, kvalue, size, flags);
60 security_inode_post_setxattr(d, kname, kvalue, size, flags);
62 up(&d->d_inode->i_sem);
70 sys_setxattr(char __user *path, char __user *name, void __user *value,
71 size_t size, int flags)
76 error = user_path_walk(path, &nd);
79 error = setxattr(nd.dentry, name, value, size, flags);
85 sys_lsetxattr(char __user *path, char __user *name, void __user *value,
86 size_t size, int flags)
91 error = user_path_walk_link(path, &nd);
94 error = setxattr(nd.dentry, name, value, size, flags);
100 sys_fsetxattr(int fd, char __user *name, void __user *value,
101 size_t size, int flags)
109 error = setxattr(f->f_dentry, name, value, size, flags);
115 * Extended attribute GET operations
118 getxattr(struct dentry *d, char __user *name, void __user *value, size_t size)
122 char kname[XATTR_NAME_MAX + 1];
124 error = strncpy_from_user(kname, name, sizeof(kname));
125 if (error == 0 || error == sizeof(kname))
131 if (size > XATTR_SIZE_MAX)
132 size = XATTR_SIZE_MAX;
133 kvalue = kmalloc(size, GFP_KERNEL);
139 if (d->d_inode->i_op && d->d_inode->i_op->getxattr) {
140 error = security_inode_getxattr(d, kname);
143 error = d->d_inode->i_op->getxattr(d, kname, kvalue, size);
145 if (size && copy_to_user(value, kvalue, error))
147 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
148 /* The file system tried to returned a value bigger
149 than XATTR_SIZE_MAX bytes. Not possible. */
160 sys_getxattr(char __user *path, char __user *name, void __user *value,
166 error = user_path_walk(path, &nd);
169 error = getxattr(nd.dentry, name, value, size);
175 sys_lgetxattr(char __user *path, char __user *name, void __user *value,
181 error = user_path_walk_link(path, &nd);
184 error = getxattr(nd.dentry, name, value, size);
190 sys_fgetxattr(int fd, char __user *name, void __user *value, size_t size)
193 ssize_t error = -EBADF;
198 error = getxattr(f->f_dentry, name, value, size);
204 * Extended attribute LIST operations
207 listxattr(struct dentry *d, char __user *list, size_t size)
213 if (size > XATTR_LIST_MAX)
214 size = XATTR_LIST_MAX;
215 klist = kmalloc(size, GFP_KERNEL);
221 if (d->d_inode->i_op && d->d_inode->i_op->listxattr) {
222 error = security_inode_listxattr(d);
225 error = d->d_inode->i_op->listxattr(d, klist, size);
227 if (size && copy_to_user(list, klist, error))
229 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
230 /* The file system tried to returned a list bigger
231 than XATTR_LIST_MAX bytes. Not possible. */
242 sys_listxattr(char __user *path, char __user *list, size_t size)
247 error = user_path_walk(path, &nd);
250 error = listxattr(nd.dentry, list, size);
256 sys_llistxattr(char __user *path, char __user *list, size_t size)
261 error = user_path_walk_link(path, &nd);
264 error = listxattr(nd.dentry, list, size);
270 sys_flistxattr(int fd, char __user *list, size_t size)
273 ssize_t error = -EBADF;
278 error = listxattr(f->f_dentry, list, size);
284 * Extended attribute REMOVE operations
287 removexattr(struct dentry *d, char __user *name)
290 char kname[XATTR_NAME_MAX + 1];
292 error = strncpy_from_user(kname, name, sizeof(kname));
293 if (error == 0 || error == sizeof(kname))
299 if (d->d_inode->i_op && d->d_inode->i_op->removexattr) {
300 error = security_inode_removexattr(d, kname);
303 down(&d->d_inode->i_sem);
304 error = d->d_inode->i_op->removexattr(d, kname);
305 up(&d->d_inode->i_sem);
312 sys_removexattr(char __user *path, char __user *name)
317 error = user_path_walk(path, &nd);
320 error = removexattr(nd.dentry, name);
326 sys_lremovexattr(char __user *path, char __user *name)
331 error = user_path_walk_link(path, &nd);
334 error = removexattr(nd.dentry, name);
340 sys_fremovexattr(int fd, char __user *name)
348 error = removexattr(f->f_dentry, name);
355 strcmp_prefix(const char *a, const char *a_prefix)
357 while (*a_prefix && *a == *a_prefix) {
361 return *a_prefix ? NULL : a;
365 * In order to implement different sets of xattr operations for each xattr
366 * prefix with the generic xattr API, a filesystem should create a
367 * null-terminated array of struct xattr_handler (one for each prefix) and
368 * hang a pointer to it off of the s_xattr field of the superblock.
370 * The generic_fooxattr() functions will use this list to dispatch xattr
371 * operations to the correct xattr_handler.
373 #define for_each_xattr_handler(handlers, handler) \
374 for ((handler) = *(handlers)++; \
376 (handler) = *(handlers)++)
379 * Find the xattr_handler with the matching prefix.
381 static struct xattr_handler *
382 xattr_resolve_name(struct xattr_handler **handlers, const char **name)
384 struct xattr_handler *handler;
389 for_each_xattr_handler(handlers, handler) {
390 const char *n = strcmp_prefix(*name, handler->prefix);
400 * Find the handler for the prefix and dispatch its get() operation.
403 generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
405 struct xattr_handler *handler;
406 struct inode *inode = dentry->d_inode;
408 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
411 return handler->get(inode, name, buffer, size);
415 * Combine the results of the list() operation from every xattr_handler in the
419 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
421 struct inode *inode = dentry->d_inode;
422 struct xattr_handler *handler, **handlers = inode->i_sb->s_xattr;
423 unsigned int size = 0;
426 for_each_xattr_handler(handlers, handler)
427 size += handler->list(inode, NULL, 0, NULL, 0);
431 for_each_xattr_handler(handlers, handler) {
432 size = handler->list(inode, buf, buffer_size, NULL, 0);
433 if (size > buffer_size)
444 * Find the handler for the prefix and dispatch its set() operation.
447 generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
449 struct xattr_handler *handler;
450 struct inode *inode = dentry->d_inode;
453 value = ""; /* empty EA, do not remove */
454 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
457 return handler->set(inode, name, value, size, flags);
461 * Find the handler for the prefix and dispatch its set() operation to remove
462 * any associated extended attribute.
465 generic_removexattr(struct dentry *dentry, const char *name)
467 struct xattr_handler *handler;
468 struct inode *inode = dentry->d_inode;
470 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
473 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
476 EXPORT_SYMBOL(generic_getxattr);
477 EXPORT_SYMBOL(generic_listxattr);
478 EXPORT_SYMBOL(generic_setxattr);
479 EXPORT_SYMBOL(generic_removexattr);