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>
10 #include <linux/slab.h>
11 #include <linux/vmalloc.h>
12 #include <linux/smp_lock.h>
13 #include <linux/file.h>
14 #include <linux/xattr.h>
15 #include <asm/uaccess.h>
18 * Extended attribute memory allocation wrappers, originally
19 * based on the Intermezzo PRESTO_ALLOC/PRESTO_FREE macros.
20 * The vmalloc use here is very uncommon - extended attributes
21 * are supposed to be small chunks of metadata, and it is quite
22 * unusual to have very many extended attributes, so lists tend
23 * to be quite short as well. The 64K upper limit is derived
24 * from the extended attribute size limit used by XFS.
25 * Intentionally allow zero @size for value/list size requests.
28 xattr_alloc(size_t size, size_t limit)
33 return ERR_PTR(-E2BIG);
35 if (!size) /* size request, no buffer is needed */
37 else if (size <= PAGE_SIZE)
38 ptr = kmalloc((unsigned long) size, GFP_KERNEL);
40 ptr = vmalloc((unsigned long) size);
42 return ERR_PTR(-ENOMEM);
47 xattr_free(void *ptr, size_t size)
49 if (!size) /* size request, no buffer was needed */
51 else if (size <= PAGE_SIZE)
58 * Extended attribute SET operations
61 setxattr(struct dentry *d, char *name, void *value, size_t size, int flags)
65 char kname[XATTR_NAME_MAX + 1];
67 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
70 error = strncpy_from_user(kname, name, sizeof(kname));
71 if (error == 0 || error == sizeof(kname))
76 kvalue = xattr_alloc(size, XATTR_SIZE_MAX);
78 return PTR_ERR(kvalue);
80 if (size > 0 && copy_from_user(kvalue, value, size)) {
81 xattr_free(kvalue, size);
86 if (d->d_inode->i_op && d->d_inode->i_op->setxattr) {
87 down(&d->d_inode->i_sem);
89 error = d->d_inode->i_op->setxattr(d, kname, kvalue, size, flags);
91 up(&d->d_inode->i_sem);
94 xattr_free(kvalue, size);
99 sys_setxattr(char *path, char *name, void *value, size_t size, int flags)
104 error = user_path_walk(path, &nd);
107 error = setxattr(nd.dentry, name, value, size, flags);
113 sys_lsetxattr(char *path, char *name, void *value, size_t size, int flags)
118 error = user_path_walk_link(path, &nd);
121 error = setxattr(nd.dentry, name, value, size, flags);
127 sys_fsetxattr(int fd, char *name, void *value, size_t size, int flags)
135 error = setxattr(f->f_dentry, name, value, size, flags);
141 * Extended attribute GET operations
144 getxattr(struct dentry *d, char *name, void *value, size_t size)
148 char kname[XATTR_NAME_MAX + 1];
150 error = strncpy_from_user(kname, name, sizeof(kname));
151 if (error == 0 || error == sizeof(kname))
156 kvalue = xattr_alloc(size, XATTR_SIZE_MAX);
158 return PTR_ERR(kvalue);
161 if (d->d_inode->i_op && d->d_inode->i_op->getxattr) {
162 down(&d->d_inode->i_sem);
164 error = d->d_inode->i_op->getxattr(d, kname, kvalue, size);
166 up(&d->d_inode->i_sem);
169 if (kvalue && error > 0)
170 if (copy_to_user(value, kvalue, error))
172 xattr_free(kvalue, size);
177 sys_getxattr(char *path, char *name, void *value, size_t size)
182 error = user_path_walk(path, &nd);
185 error = getxattr(nd.dentry, name, value, size);
191 sys_lgetxattr(char *path, char *name, void *value, size_t size)
196 error = user_path_walk_link(path, &nd);
199 error = getxattr(nd.dentry, name, value, size);
205 sys_fgetxattr(int fd, char *name, void *value, size_t size)
208 ssize_t error = -EBADF;
213 error = getxattr(f->f_dentry, name, value, size);
219 * Extended attribute LIST operations
222 listxattr(struct dentry *d, char *list, size_t size)
227 klist = (char *)xattr_alloc(size, XATTR_LIST_MAX);
229 return PTR_ERR(klist);
232 if (d->d_inode->i_op && d->d_inode->i_op->listxattr) {
233 down(&d->d_inode->i_sem);
235 error = d->d_inode->i_op->listxattr(d, klist, size);
237 up(&d->d_inode->i_sem);
240 if (klist && error > 0)
241 if (copy_to_user(list, klist, error))
243 xattr_free(klist, size);
248 sys_listxattr(char *path, char *list, size_t size)
253 error = user_path_walk(path, &nd);
256 error = listxattr(nd.dentry, list, size);
262 sys_llistxattr(char *path, char *list, size_t size)
267 error = user_path_walk_link(path, &nd);
270 error = listxattr(nd.dentry, list, size);
276 sys_flistxattr(int fd, char *list, size_t size)
279 ssize_t error = -EBADF;
284 error = listxattr(f->f_dentry, list, size);
290 * Extended attribute REMOVE operations
293 removexattr(struct dentry *d, char *name)
296 char kname[XATTR_NAME_MAX + 1];
298 error = strncpy_from_user(kname, name, sizeof(kname));
299 if (error == 0 || error == sizeof(kname))
305 if (d->d_inode->i_op && d->d_inode->i_op->removexattr) {
306 down(&d->d_inode->i_sem);
308 error = d->d_inode->i_op->removexattr(d, kname);
310 up(&d->d_inode->i_sem);
316 sys_removexattr(char *path, char *name)
321 error = user_path_walk(path, &nd);
324 error = removexattr(nd.dentry, name);
330 sys_lremovexattr(char *path, char *name)
335 error = user_path_walk_link(path, &nd);
338 error = removexattr(nd.dentry, name);
344 sys_fremovexattr(int fd, char *name)
352 error = removexattr(f->f_dentry, name);