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>
24 vfs_setxattr(struct dentry *dentry, char *name, void *value,
25 size_t size, int flags)
27 struct inode *inode = dentry->d_inode;
30 mutex_lock(&inode->i_mutex);
31 error = security_inode_setxattr(dentry, name, value, size, flags);
35 if (inode->i_op->setxattr) {
36 error = inode->i_op->setxattr(dentry, name, value, size, flags);
38 fsnotify_xattr(dentry);
39 security_inode_post_setxattr(dentry, name, value,
42 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
43 sizeof XATTR_SECURITY_PREFIX - 1)) {
44 const char *suffix = name + sizeof XATTR_SECURITY_PREFIX - 1;
45 error = security_inode_setsecurity(inode, suffix, value,
48 fsnotify_xattr(dentry);
51 mutex_unlock(&inode->i_mutex);
54 EXPORT_SYMBOL_GPL(vfs_setxattr);
57 vfs_getxattr(struct dentry *dentry, char *name, void *value, size_t size)
59 struct inode *inode = dentry->d_inode;
62 error = security_inode_getxattr(dentry, name);
66 if (inode->i_op->getxattr)
67 error = inode->i_op->getxattr(dentry, name, value, size);
71 if (!strncmp(name, XATTR_SECURITY_PREFIX,
72 sizeof XATTR_SECURITY_PREFIX - 1)) {
73 const char *suffix = name + sizeof XATTR_SECURITY_PREFIX - 1;
74 int ret = security_inode_getsecurity(inode, suffix, value,
77 * Only overwrite the return value if a security module
80 if (ret != -EOPNOTSUPP)
86 EXPORT_SYMBOL_GPL(vfs_getxattr);
89 vfs_removexattr(struct dentry *dentry, char *name)
91 struct inode *inode = dentry->d_inode;
94 if (!inode->i_op->removexattr)
97 error = security_inode_removexattr(dentry, name);
101 mutex_lock(&inode->i_mutex);
102 error = inode->i_op->removexattr(dentry, name);
103 mutex_unlock(&inode->i_mutex);
106 fsnotify_xattr(dentry);
109 EXPORT_SYMBOL_GPL(vfs_removexattr);
113 * Extended attribute SET operations
116 setxattr(struct dentry *d, char __user *name, void __user *value,
117 size_t size, int flags)
121 char kname[XATTR_NAME_MAX + 1];
123 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
126 error = strncpy_from_user(kname, name, sizeof(kname));
127 if (error == 0 || error == sizeof(kname))
133 if (size > XATTR_SIZE_MAX)
135 kvalue = kmalloc(size, GFP_KERNEL);
138 if (copy_from_user(kvalue, value, size)) {
144 error = vfs_setxattr(d, kname, kvalue, size, flags);
150 sys_setxattr(char __user *path, char __user *name, void __user *value,
151 size_t size, int flags)
156 error = user_path_walk(path, &nd);
159 error = setxattr(nd.dentry, name, value, size, flags);
165 sys_lsetxattr(char __user *path, char __user *name, void __user *value,
166 size_t size, int flags)
171 error = user_path_walk_link(path, &nd);
174 error = setxattr(nd.dentry, name, value, size, flags);
180 sys_fsetxattr(int fd, char __user *name, void __user *value,
181 size_t size, int flags)
189 error = setxattr(f->f_dentry, name, value, size, flags);
195 * Extended attribute GET operations
198 getxattr(struct dentry *d, char __user *name, void __user *value, size_t size)
202 char kname[XATTR_NAME_MAX + 1];
204 error = strncpy_from_user(kname, name, sizeof(kname));
205 if (error == 0 || error == sizeof(kname))
211 if (size > XATTR_SIZE_MAX)
212 size = XATTR_SIZE_MAX;
213 kvalue = kzalloc(size, GFP_KERNEL);
218 error = vfs_getxattr(d, kname, kvalue, size);
220 if (size && copy_to_user(value, kvalue, error))
222 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
223 /* The file system tried to returned a value bigger
224 than XATTR_SIZE_MAX bytes. Not possible. */
232 sys_getxattr(char __user *path, char __user *name, void __user *value,
238 error = user_path_walk(path, &nd);
241 error = getxattr(nd.dentry, name, value, size);
247 sys_lgetxattr(char __user *path, char __user *name, void __user *value,
253 error = user_path_walk_link(path, &nd);
256 error = getxattr(nd.dentry, name, value, size);
262 sys_fgetxattr(int fd, char __user *name, void __user *value, size_t size)
265 ssize_t error = -EBADF;
270 error = getxattr(f->f_dentry, name, value, size);
276 * Extended attribute LIST operations
279 listxattr(struct dentry *d, char __user *list, size_t size)
285 if (size > XATTR_LIST_MAX)
286 size = XATTR_LIST_MAX;
287 klist = kmalloc(size, GFP_KERNEL);
292 error = security_inode_listxattr(d);
296 if (d->d_inode->i_op && d->d_inode->i_op->listxattr) {
297 error = d->d_inode->i_op->listxattr(d, klist, size);
299 error = security_inode_listsecurity(d->d_inode, klist, size);
300 if (size && error > size)
304 if (size && copy_to_user(list, klist, error))
306 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
307 /* The file system tried to returned a list bigger
308 than XATTR_LIST_MAX bytes. Not possible. */
317 sys_listxattr(char __user *path, char __user *list, size_t size)
322 error = user_path_walk(path, &nd);
325 error = listxattr(nd.dentry, list, size);
331 sys_llistxattr(char __user *path, char __user *list, size_t size)
336 error = user_path_walk_link(path, &nd);
339 error = listxattr(nd.dentry, list, size);
345 sys_flistxattr(int fd, char __user *list, size_t size)
348 ssize_t error = -EBADF;
353 error = listxattr(f->f_dentry, list, size);
359 * Extended attribute REMOVE operations
362 removexattr(struct dentry *d, char __user *name)
365 char kname[XATTR_NAME_MAX + 1];
367 error = strncpy_from_user(kname, name, sizeof(kname));
368 if (error == 0 || error == sizeof(kname))
373 return vfs_removexattr(d, kname);
377 sys_removexattr(char __user *path, char __user *name)
382 error = user_path_walk(path, &nd);
385 error = removexattr(nd.dentry, name);
391 sys_lremovexattr(char __user *path, char __user *name)
396 error = user_path_walk_link(path, &nd);
399 error = removexattr(nd.dentry, name);
405 sys_fremovexattr(int fd, char __user *name)
413 error = removexattr(f->f_dentry, name);
420 strcmp_prefix(const char *a, const char *a_prefix)
422 while (*a_prefix && *a == *a_prefix) {
426 return *a_prefix ? NULL : a;
430 * In order to implement different sets of xattr operations for each xattr
431 * prefix with the generic xattr API, a filesystem should create a
432 * null-terminated array of struct xattr_handler (one for each prefix) and
433 * hang a pointer to it off of the s_xattr field of the superblock.
435 * The generic_fooxattr() functions will use this list to dispatch xattr
436 * operations to the correct xattr_handler.
438 #define for_each_xattr_handler(handlers, handler) \
439 for ((handler) = *(handlers)++; \
441 (handler) = *(handlers)++)
444 * Find the xattr_handler with the matching prefix.
446 static struct xattr_handler *
447 xattr_resolve_name(struct xattr_handler **handlers, const char **name)
449 struct xattr_handler *handler;
454 for_each_xattr_handler(handlers, handler) {
455 const char *n = strcmp_prefix(*name, handler->prefix);
465 * Find the handler for the prefix and dispatch its get() operation.
468 generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
470 struct xattr_handler *handler;
471 struct inode *inode = dentry->d_inode;
473 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
476 return handler->get(inode, name, buffer, size);
480 * Combine the results of the list() operation from every xattr_handler in the
484 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
486 struct inode *inode = dentry->d_inode;
487 struct xattr_handler *handler, **handlers = inode->i_sb->s_xattr;
488 unsigned int size = 0;
491 for_each_xattr_handler(handlers, handler)
492 size += handler->list(inode, NULL, 0, NULL, 0);
496 for_each_xattr_handler(handlers, handler) {
497 size = handler->list(inode, buf, buffer_size, NULL, 0);
498 if (size > buffer_size)
509 * Find the handler for the prefix and dispatch its set() operation.
512 generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
514 struct xattr_handler *handler;
515 struct inode *inode = dentry->d_inode;
518 value = ""; /* empty EA, do not remove */
519 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
522 return handler->set(inode, name, value, size, flags);
526 * Find the handler for the prefix and dispatch its set() operation to remove
527 * any associated extended attribute.
530 generic_removexattr(struct dentry *dentry, const char *name)
532 struct xattr_handler *handler;
533 struct inode *inode = dentry->d_inode;
535 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
538 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
541 EXPORT_SYMBOL(generic_getxattr);
542 EXPORT_SYMBOL(generic_listxattr);
543 EXPORT_SYMBOL(generic_setxattr);
544 EXPORT_SYMBOL(generic_removexattr);