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, struct vfsmount *mnt)
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);
60 if (MNT_IS_RDONLY(mnt))
62 error = d->d_inode->i_op->setxattr(d, kname, kvalue, size, flags);
64 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, nd.mnt);
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, nd.mnt);
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, f->f_vfsmnt);
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, struct vfsmount *mnt)
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);
308 if (MNT_IS_RDONLY(mnt))
310 down(&d->d_inode->i_sem);
311 error = d->d_inode->i_op->removexattr(d, kname);
312 up(&d->d_inode->i_sem);
319 sys_removexattr(char __user *path, char __user *name)
324 error = user_path_walk(path, &nd);
327 error = removexattr(nd.dentry, name, nd.mnt);
333 sys_lremovexattr(char __user *path, char __user *name)
338 error = user_path_walk_link(path, &nd);
341 error = removexattr(nd.dentry, name, nd.mnt);
347 sys_fremovexattr(int fd, char __user *name)
355 error = removexattr(f->f_dentry, name, f->f_vfsmnt);
362 strcmp_prefix(const char *a, const char *a_prefix)
364 while (*a_prefix && *a == *a_prefix) {
368 return *a_prefix ? NULL : a;
372 * In order to implement different sets of xattr operations for each xattr
373 * prefix with the generic xattr API, a filesystem should create a
374 * null-terminated array of struct xattr_handler (one for each prefix) and
375 * hang a pointer to it off of the s_xattr field of the superblock.
377 * The generic_fooxattr() functions will use this list to dispatch xattr
378 * operations to the correct xattr_handler.
380 #define for_each_xattr_handler(handlers, handler) \
381 for ((handler) = *(handlers)++; \
383 (handler) = *(handlers)++)
386 * Find the xattr_handler with the matching prefix.
388 static struct xattr_handler *
389 xattr_resolve_name(struct xattr_handler **handlers, const char **name)
391 struct xattr_handler *handler;
396 for_each_xattr_handler(handlers, handler) {
397 const char *n = strcmp_prefix(*name, handler->prefix);
407 * Find the handler for the prefix and dispatch its get() operation.
410 generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
412 struct xattr_handler *handler;
413 struct inode *inode = dentry->d_inode;
415 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
418 return handler->get(inode, name, buffer, size);
422 * Combine the results of the list() operation from every xattr_handler in the
426 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
428 struct inode *inode = dentry->d_inode;
429 struct xattr_handler *handler, **handlers = inode->i_sb->s_xattr;
430 unsigned int size = 0;
433 for_each_xattr_handler(handlers, handler)
434 size += handler->list(inode, NULL, 0, NULL, 0);
438 for_each_xattr_handler(handlers, handler) {
439 size = handler->list(inode, buf, buffer_size, NULL, 0);
440 if (size > buffer_size)
451 * Find the handler for the prefix and dispatch its set() operation.
454 generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
456 struct xattr_handler *handler;
457 struct inode *inode = dentry->d_inode;
460 value = ""; /* empty EA, do not remove */
461 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
464 return handler->set(inode, name, value, size, flags);
468 * Find the handler for the prefix and dispatch its set() operation to remove
469 * any associated extended attribute.
472 generic_removexattr(struct dentry *dentry, const char *name)
474 struct xattr_handler *handler;
475 struct inode *inode = dentry->d_inode;
477 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
480 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
483 EXPORT_SYMBOL(generic_getxattr);
484 EXPORT_SYMBOL(generic_listxattr);
485 EXPORT_SYMBOL(generic_setxattr);
486 EXPORT_SYMBOL(generic_removexattr);