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)) {
55 if (d->d_inode->i_op && d->d_inode->i_op->setxattr) {
56 down(&d->d_inode->i_sem);
57 error = security_inode_setxattr(d, kname, kvalue, size, flags);
60 error = d->d_inode->i_op->setxattr(d, kname, kvalue, size, flags);
63 security_inode_post_setxattr(d, kname, kvalue, size, flags);
66 up(&d->d_inode->i_sem);
74 sys_setxattr(char __user *path, char __user *name, void __user *value,
75 size_t size, int flags)
80 error = user_path_walk(path, &nd);
83 error = setxattr(nd.dentry, name, value, size, flags);
89 sys_lsetxattr(char __user *path, char __user *name, void __user *value,
90 size_t size, int flags)
95 error = user_path_walk_link(path, &nd);
98 error = setxattr(nd.dentry, name, value, size, flags);
104 sys_fsetxattr(int fd, char __user *name, void __user *value,
105 size_t size, int flags)
113 error = setxattr(f->f_dentry, name, value, size, flags);
119 * Extended attribute GET operations
122 getxattr(struct dentry *d, char __user *name, void __user *value, size_t size)
126 char kname[XATTR_NAME_MAX + 1];
128 error = strncpy_from_user(kname, name, sizeof(kname));
129 if (error == 0 || error == sizeof(kname))
135 if (size > XATTR_SIZE_MAX)
136 size = XATTR_SIZE_MAX;
137 kvalue = kmalloc(size, GFP_KERNEL);
143 if (d->d_inode->i_op && d->d_inode->i_op->getxattr) {
144 error = security_inode_getxattr(d, kname);
147 error = d->d_inode->i_op->getxattr(d, kname, kvalue, size);
149 if (size && copy_to_user(value, kvalue, error))
151 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
152 /* The file system tried to returned a value bigger
153 than XATTR_SIZE_MAX bytes. Not possible. */
164 sys_getxattr(char __user *path, char __user *name, void __user *value,
170 error = user_path_walk(path, &nd);
173 error = getxattr(nd.dentry, name, value, size);
179 sys_lgetxattr(char __user *path, char __user *name, void __user *value,
185 error = user_path_walk_link(path, &nd);
188 error = getxattr(nd.dentry, name, value, size);
194 sys_fgetxattr(int fd, char __user *name, void __user *value, size_t size)
197 ssize_t error = -EBADF;
202 error = getxattr(f->f_dentry, name, value, size);
208 * Extended attribute LIST operations
211 listxattr(struct dentry *d, char __user *list, size_t size)
217 if (size > XATTR_LIST_MAX)
218 size = XATTR_LIST_MAX;
219 klist = kmalloc(size, GFP_KERNEL);
225 if (d->d_inode->i_op && d->d_inode->i_op->listxattr) {
226 error = security_inode_listxattr(d);
229 error = d->d_inode->i_op->listxattr(d, klist, size);
231 if (size && copy_to_user(list, klist, error))
233 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
234 /* The file system tried to returned a list bigger
235 than XATTR_LIST_MAX bytes. Not possible. */
246 sys_listxattr(char __user *path, char __user *list, size_t size)
251 error = user_path_walk(path, &nd);
254 error = listxattr(nd.dentry, list, size);
260 sys_llistxattr(char __user *path, char __user *list, size_t size)
265 error = user_path_walk_link(path, &nd);
268 error = listxattr(nd.dentry, list, size);
274 sys_flistxattr(int fd, char __user *list, size_t size)
277 ssize_t error = -EBADF;
282 error = listxattr(f->f_dentry, list, size);
288 * Extended attribute REMOVE operations
291 removexattr(struct dentry *d, char __user *name)
294 char kname[XATTR_NAME_MAX + 1];
296 error = strncpy_from_user(kname, name, sizeof(kname));
297 if (error == 0 || error == sizeof(kname))
303 if (d->d_inode->i_op && d->d_inode->i_op->removexattr) {
304 error = security_inode_removexattr(d, kname);
307 down(&d->d_inode->i_sem);
308 error = d->d_inode->i_op->removexattr(d, kname);
309 up(&d->d_inode->i_sem);
316 sys_removexattr(char __user *path, char __user *name)
321 error = user_path_walk(path, &nd);
324 error = removexattr(nd.dentry, name);
330 sys_lremovexattr(char __user *path, char __user *name)
335 error = user_path_walk_link(path, &nd);
338 error = removexattr(nd.dentry, name);
344 sys_fremovexattr(int fd, char __user *name)
352 error = removexattr(f->f_dentry, name);
359 strcmp_prefix(const char *a, const char *a_prefix)
361 while (*a_prefix && *a == *a_prefix) {
365 return *a_prefix ? NULL : a;
369 * In order to implement different sets of xattr operations for each xattr
370 * prefix with the generic xattr API, a filesystem should create a
371 * null-terminated array of struct xattr_handler (one for each prefix) and
372 * hang a pointer to it off of the s_xattr field of the superblock.
374 * The generic_fooxattr() functions will use this list to dispatch xattr
375 * operations to the correct xattr_handler.
377 #define for_each_xattr_handler(handlers, handler) \
378 for ((handler) = *(handlers)++; \
380 (handler) = *(handlers)++)
383 * Find the xattr_handler with the matching prefix.
385 static struct xattr_handler *
386 xattr_resolve_name(struct xattr_handler **handlers, const char **name)
388 struct xattr_handler *handler;
393 for_each_xattr_handler(handlers, handler) {
394 const char *n = strcmp_prefix(*name, handler->prefix);
404 * Find the handler for the prefix and dispatch its get() operation.
407 generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
409 struct xattr_handler *handler;
410 struct inode *inode = dentry->d_inode;
412 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
415 return handler->get(inode, name, buffer, size);
419 * Combine the results of the list() operation from every xattr_handler in the
423 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
425 struct inode *inode = dentry->d_inode;
426 struct xattr_handler *handler, **handlers = inode->i_sb->s_xattr;
427 unsigned int size = 0;
430 for_each_xattr_handler(handlers, handler)
431 size += handler->list(inode, NULL, 0, NULL, 0);
435 for_each_xattr_handler(handlers, handler) {
436 size = handler->list(inode, buf, buffer_size, NULL, 0);
437 if (size > buffer_size)
448 * Find the handler for the prefix and dispatch its set() operation.
451 generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
453 struct xattr_handler *handler;
454 struct inode *inode = dentry->d_inode;
457 value = ""; /* empty EA, do not remove */
458 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
461 return handler->set(inode, name, value, size, flags);
465 * Find the handler for the prefix and dispatch its set() operation to remove
466 * any associated extended attribute.
469 generic_removexattr(struct dentry *dentry, const char *name)
471 struct xattr_handler *handler;
472 struct inode *inode = dentry->d_inode;
474 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
477 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
480 EXPORT_SYMBOL(generic_getxattr);
481 EXPORT_SYMBOL(generic_listxattr);
482 EXPORT_SYMBOL(generic_setxattr);
483 EXPORT_SYMBOL(generic_removexattr);