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 <asm/uaccess.h>
22 * Extended attribute SET operations
25 setxattr(struct dentry *d, char __user *name, void __user *value,
26 size_t size, int flags)
30 char kname[XATTR_NAME_MAX + 1];
32 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
35 error = strncpy_from_user(kname, name, sizeof(kname));
36 if (error == 0 || error == sizeof(kname))
42 if (size > XATTR_SIZE_MAX)
44 kvalue = kmalloc(size, GFP_KERNEL);
47 if (copy_from_user(kvalue, value, size)) {
54 if (d->d_inode->i_op && d->d_inode->i_op->setxattr) {
55 down(&d->d_inode->i_sem);
56 error = security_inode_setxattr(d, kname, kvalue, size, flags);
59 error = d->d_inode->i_op->setxattr(d, kname, kvalue, size, flags);
61 security_inode_post_setxattr(d, kname, kvalue, size, flags);
63 up(&d->d_inode->i_sem);
71 sys_setxattr(char __user *path, char __user *name, void __user *value,
72 size_t size, int flags)
77 error = user_path_walk(path, &nd);
80 error = setxattr(nd.dentry, name, value, size, flags);
86 sys_lsetxattr(char __user *path, char __user *name, void __user *value,
87 size_t size, int flags)
92 error = user_path_walk_link(path, &nd);
95 error = setxattr(nd.dentry, name, value, size, flags);
101 sys_fsetxattr(int fd, char __user *name, void __user *value,
102 size_t size, int flags)
110 error = setxattr(f->f_dentry, name, value, size, flags);
116 * Extended attribute GET operations
119 getxattr(struct dentry *d, char __user *name, void __user *value, size_t size)
123 char kname[XATTR_NAME_MAX + 1];
125 error = strncpy_from_user(kname, name, sizeof(kname));
126 if (error == 0 || error == sizeof(kname))
132 if (size > XATTR_SIZE_MAX)
133 size = XATTR_SIZE_MAX;
134 kvalue = kmalloc(size, GFP_KERNEL);
140 if (d->d_inode->i_op && d->d_inode->i_op->getxattr) {
141 error = security_inode_getxattr(d, kname);
144 error = d->d_inode->i_op->getxattr(d, kname, kvalue, size);
146 if (size && copy_to_user(value, kvalue, error))
148 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
149 /* The file system tried to returned a value bigger
150 than XATTR_SIZE_MAX bytes. Not possible. */
161 sys_getxattr(char __user *path, char __user *name, void __user *value,
167 error = user_path_walk(path, &nd);
170 error = getxattr(nd.dentry, name, value, size);
176 sys_lgetxattr(char __user *path, char __user *name, void __user *value,
182 error = user_path_walk_link(path, &nd);
185 error = getxattr(nd.dentry, name, value, size);
191 sys_fgetxattr(int fd, char __user *name, void __user *value, size_t size)
194 ssize_t error = -EBADF;
199 error = getxattr(f->f_dentry, name, value, size);
205 * Extended attribute LIST operations
208 listxattr(struct dentry *d, char __user *list, size_t size)
214 if (size > XATTR_LIST_MAX)
215 size = XATTR_LIST_MAX;
216 klist = kmalloc(size, GFP_KERNEL);
222 if (d->d_inode->i_op && d->d_inode->i_op->listxattr) {
223 error = security_inode_listxattr(d);
226 error = d->d_inode->i_op->listxattr(d, klist, size);
228 if (size && copy_to_user(list, klist, error))
230 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
231 /* The file system tried to returned a list bigger
232 than XATTR_LIST_MAX bytes. Not possible. */
243 sys_listxattr(char __user *path, char __user *list, size_t size)
248 error = user_path_walk(path, &nd);
251 error = listxattr(nd.dentry, list, size);
257 sys_llistxattr(char __user *path, char __user *list, size_t size)
262 error = user_path_walk_link(path, &nd);
265 error = listxattr(nd.dentry, list, size);
271 sys_flistxattr(int fd, char __user *list, size_t size)
274 ssize_t error = -EBADF;
279 error = listxattr(f->f_dentry, list, size);
285 * Extended attribute REMOVE operations
288 removexattr(struct dentry *d, char __user *name)
291 char kname[XATTR_NAME_MAX + 1];
293 error = strncpy_from_user(kname, name, sizeof(kname));
294 if (error == 0 || error == sizeof(kname))
300 if (d->d_inode->i_op && d->d_inode->i_op->removexattr) {
301 error = security_inode_removexattr(d, kname);
304 down(&d->d_inode->i_sem);
305 error = d->d_inode->i_op->removexattr(d, kname);
306 up(&d->d_inode->i_sem);
313 sys_removexattr(char __user *path, char __user *name)
318 error = user_path_walk(path, &nd);
321 error = removexattr(nd.dentry, name);
327 sys_lremovexattr(char __user *path, char __user *name)
332 error = user_path_walk_link(path, &nd);
335 error = removexattr(nd.dentry, name);
341 sys_fremovexattr(int fd, char __user *name)
349 error = removexattr(f->f_dentry, name);
356 strcmp_prefix(const char *a, const char *a_prefix)
358 while (*a_prefix && *a == *a_prefix) {
362 return *a_prefix ? NULL : a;
366 * In order to implement different sets of xattr operations for each xattr
367 * prefix with the generic xattr API, a filesystem should create a
368 * null-terminated array of struct xattr_handler (one for each prefix) and
369 * hang a pointer to it off of the s_xattr field of the superblock.
371 * The generic_fooxattr() functions will use this list to dispatch xattr
372 * operations to the correct xattr_handler.
374 #define for_each_xattr_handler(handlers, handler) \
375 for ((handler) = *(handlers)++; \
377 (handler) = *(handlers)++)
380 * Find the xattr_handler with the matching prefix.
382 static struct xattr_handler *
383 xattr_resolve_name(struct xattr_handler **handlers, const char **name)
385 struct xattr_handler *handler;
390 for_each_xattr_handler(handlers, handler) {
391 const char *n = strcmp_prefix(*name, handler->prefix);
401 * Find the handler for the prefix and dispatch its get() operation.
404 generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
406 struct xattr_handler *handler;
407 struct inode *inode = dentry->d_inode;
409 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
412 return handler->get(inode, name, buffer, size);
416 * Combine the results of the list() operation from every xattr_handler in the
420 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
422 struct inode *inode = dentry->d_inode;
423 struct xattr_handler *handler, **handlers = inode->i_sb->s_xattr;
424 unsigned int size = 0;
427 for_each_xattr_handler(handlers, handler)
428 size += handler->list(inode, NULL, 0, NULL, 0);
432 for_each_xattr_handler(handlers, handler) {
433 size = handler->list(inode, buf, buffer_size, NULL, 0);
434 if (size > buffer_size)
445 * Find the handler for the prefix and dispatch its set() operation.
448 generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
450 struct xattr_handler *handler;
451 struct inode *inode = dentry->d_inode;
454 value = ""; /* empty EA, do not remove */
455 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
458 return handler->set(inode, name, value, size, flags);
462 * Find the handler for the prefix and dispatch its set() operation to remove
463 * any associated extended attribute.
466 generic_removexattr(struct dentry *dentry, const char *name)
468 struct xattr_handler *handler;
469 struct inode *inode = dentry->d_inode;
471 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
474 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
477 EXPORT_SYMBOL(generic_getxattr);
478 EXPORT_SYMBOL(generic_listxattr);
479 EXPORT_SYMBOL(generic_setxattr);
480 EXPORT_SYMBOL(generic_removexattr);