Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | File: fs/xattr.c |
| 3 | |
| 4 | Extended attribute handling. |
| 5 | |
| 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> |
| 9 | */ |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/file.h> |
| 13 | #include <linux/xattr.h> |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 14 | #include <linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/namei.h> |
| 16 | #include <linux/security.h> |
| 17 | #include <linux/syscalls.h> |
| 18 | #include <linux/module.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 19 | #include <linux/fsnotify.h> |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 20 | #include <linux/audit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/uaccess.h> |
| 22 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 23 | |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 24 | /* |
| 25 | * Check permissions for extended attribute access. This is a bit complicated |
| 26 | * because different namespaces have very different rules. |
| 27 | */ |
| 28 | static int |
| 29 | xattr_permission(struct inode *inode, const char *name, int mask) |
| 30 | { |
| 31 | /* |
| 32 | * We can never set or remove an extended attribute on a read-only |
| 33 | * filesystem or on an immutable / append-only inode. |
| 34 | */ |
| 35 | if (mask & MAY_WRITE) { |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 36 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 37 | return -EPERM; |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * No restriction for security.* and system.* from the VFS. Decision |
| 42 | * on these is left to the underlying filesystem / security module. |
| 43 | */ |
| 44 | if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) || |
| 45 | !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) |
| 46 | return 0; |
| 47 | |
| 48 | /* |
Andreas Gruenbacher | 55b23bd | 2011-05-27 14:50:36 +0200 | [diff] [blame] | 49 | * The trusted.* namespace can only be accessed by privileged users. |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 50 | */ |
Andreas Gruenbacher | 55b23bd | 2011-05-27 14:50:36 +0200 | [diff] [blame] | 51 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) { |
| 52 | if (!capable(CAP_SYS_ADMIN)) |
| 53 | return (mask & MAY_WRITE) ? -EPERM : -ENODATA; |
| 54 | return 0; |
| 55 | } |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 56 | |
Andreas Gruenbacher | 55b23bd | 2011-05-27 14:50:36 +0200 | [diff] [blame] | 57 | /* |
| 58 | * In the user.* namespace, only regular files and directories can have |
Andreas Gruenbacher | f1f2d87 | 2006-11-02 22:07:29 -0800 | [diff] [blame] | 59 | * extended attributes. For sticky directories, only the owner and |
Andreas Gruenbacher | 55b23bd | 2011-05-27 14:50:36 +0200 | [diff] [blame] | 60 | * privileged users can write attributes. |
Andreas Gruenbacher | f1f2d87 | 2006-11-02 22:07:29 -0800 | [diff] [blame] | 61 | */ |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 62 | if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) { |
Andreas Gruenbacher | f1f2d87 | 2006-11-02 22:07:29 -0800 | [diff] [blame] | 63 | if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode)) |
Andreas Gruenbacher | 55b23bd | 2011-05-27 14:50:36 +0200 | [diff] [blame] | 64 | return (mask & MAY_WRITE) ? -EPERM : -ENODATA; |
Andreas Gruenbacher | f1f2d87 | 2006-11-02 22:07:29 -0800 | [diff] [blame] | 65 | if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) && |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 66 | (mask & MAY_WRITE) && !inode_owner_or_capable(inode)) |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 67 | return -EPERM; |
| 68 | } |
| 69 | |
Al Viro | f419a2e | 2008-07-22 00:07:17 -0400 | [diff] [blame] | 70 | return inode_permission(inode, mask); |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 71 | } |
| 72 | |
David P. Quigley | b1ab7e4 | 2009-09-03 14:25:56 -0400 | [diff] [blame] | 73 | /** |
| 74 | * __vfs_setxattr_noperm - perform setxattr operation without performing |
| 75 | * permission checks. |
| 76 | * |
| 77 | * @dentry - object to perform setxattr on |
| 78 | * @name - xattr name to set |
| 79 | * @value - value to set @name to |
| 80 | * @size - size of @value |
| 81 | * @flags - flags to pass into filesystem operations |
| 82 | * |
| 83 | * returns the result of the internal setxattr or setsecurity operations. |
| 84 | * |
| 85 | * This function requires the caller to lock the inode's i_mutex before it |
| 86 | * is executed. It also assumes that the caller will make the appropriate |
| 87 | * permission checks. |
| 88 | */ |
| 89 | int __vfs_setxattr_noperm(struct dentry *dentry, const char *name, |
| 90 | const void *value, size_t size, int flags) |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 91 | { |
| 92 | struct inode *inode = dentry->d_inode; |
David P. Quigley | b1ab7e4 | 2009-09-03 14:25:56 -0400 | [diff] [blame] | 93 | int error = -EOPNOTSUPP; |
Andi Kleen | 69b4573 | 2011-05-28 08:25:51 -0700 | [diff] [blame] | 94 | int issec = !strncmp(name, XATTR_SECURITY_PREFIX, |
| 95 | XATTR_SECURITY_PREFIX_LEN); |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 96 | |
Andi Kleen | 69b4573 | 2011-05-28 08:25:51 -0700 | [diff] [blame] | 97 | if (issec) |
| 98 | inode->i_flags &= ~S_NOSEC; |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 99 | if (inode->i_op->setxattr) { |
| 100 | error = inode->i_op->setxattr(dentry, name, value, size, flags); |
| 101 | if (!error) { |
| 102 | fsnotify_xattr(dentry); |
| 103 | security_inode_post_setxattr(dentry, name, value, |
| 104 | size, flags); |
| 105 | } |
Andi Kleen | 69b4573 | 2011-05-28 08:25:51 -0700 | [diff] [blame] | 106 | } else if (issec) { |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 107 | const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 108 | error = security_inode_setsecurity(inode, suffix, value, |
| 109 | size, flags); |
| 110 | if (!error) |
| 111 | fsnotify_xattr(dentry); |
| 112 | } |
David P. Quigley | b1ab7e4 | 2009-09-03 14:25:56 -0400 | [diff] [blame] | 113 | |
| 114 | return error; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | int |
| 119 | vfs_setxattr(struct dentry *dentry, const char *name, const void *value, |
| 120 | size_t size, int flags) |
| 121 | { |
| 122 | struct inode *inode = dentry->d_inode; |
| 123 | int error; |
| 124 | |
| 125 | error = xattr_permission(inode, name, MAY_WRITE); |
| 126 | if (error) |
| 127 | return error; |
| 128 | |
| 129 | mutex_lock(&inode->i_mutex); |
| 130 | error = security_inode_setxattr(dentry, name, value, size, flags); |
| 131 | if (error) |
| 132 | goto out; |
| 133 | |
| 134 | error = __vfs_setxattr_noperm(dentry, name, value, size, flags); |
| 135 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 136 | out: |
| 137 | mutex_unlock(&inode->i_mutex); |
| 138 | return error; |
| 139 | } |
| 140 | EXPORT_SYMBOL_GPL(vfs_setxattr); |
| 141 | |
| 142 | ssize_t |
David P. Quigley | 4249259 | 2008-02-04 22:29:39 -0800 | [diff] [blame] | 143 | xattr_getsecurity(struct inode *inode, const char *name, void *value, |
| 144 | size_t size) |
| 145 | { |
| 146 | void *buffer = NULL; |
| 147 | ssize_t len; |
| 148 | |
| 149 | if (!value || !size) { |
| 150 | len = security_inode_getsecurity(inode, name, &buffer, false); |
| 151 | goto out_noalloc; |
| 152 | } |
| 153 | |
| 154 | len = security_inode_getsecurity(inode, name, &buffer, true); |
| 155 | if (len < 0) |
| 156 | return len; |
| 157 | if (size < len) { |
| 158 | len = -ERANGE; |
| 159 | goto out; |
| 160 | } |
| 161 | memcpy(value, buffer, len); |
| 162 | out: |
| 163 | security_release_secctx(buffer, len); |
| 164 | out_noalloc: |
| 165 | return len; |
| 166 | } |
| 167 | EXPORT_SYMBOL_GPL(xattr_getsecurity); |
| 168 | |
Mimi Zohar | 1601fba | 2011-03-09 14:23:34 -0500 | [diff] [blame^] | 169 | /* |
| 170 | * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr |
| 171 | * |
| 172 | * Allocate memory, if not already allocated, or re-allocate correct size, |
| 173 | * before retrieving the extended attribute. |
| 174 | * |
| 175 | * Returns the result of alloc, if failed, or the getxattr operation. |
| 176 | */ |
| 177 | ssize_t |
| 178 | vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value, |
| 179 | size_t xattr_size, gfp_t flags) |
| 180 | { |
| 181 | struct inode *inode = dentry->d_inode; |
| 182 | char *value = *xattr_value; |
| 183 | int error; |
| 184 | |
| 185 | error = xattr_permission(inode, name, MAY_READ); |
| 186 | if (error) |
| 187 | return error; |
| 188 | |
| 189 | if (!inode->i_op->getxattr) |
| 190 | return -EOPNOTSUPP; |
| 191 | |
| 192 | error = inode->i_op->getxattr(dentry, name, NULL, 0); |
| 193 | if (error < 0) |
| 194 | return error; |
| 195 | |
| 196 | if (!value || (error > xattr_size)) { |
| 197 | value = krealloc(*xattr_value, error + 1, flags); |
| 198 | if (!value) |
| 199 | return -ENOMEM; |
| 200 | memset(value, 0, error + 1); |
| 201 | } |
| 202 | |
| 203 | error = inode->i_op->getxattr(dentry, name, value, error); |
| 204 | *xattr_value = value; |
| 205 | return error; |
| 206 | } |
| 207 | |
| 208 | /* Compare an extended attribute value with the given value */ |
| 209 | int vfs_xattr_cmp(struct dentry *dentry, const char *xattr_name, |
| 210 | const char *value, size_t size, gfp_t flags) |
| 211 | { |
| 212 | char *xattr_value = NULL; |
| 213 | int rc; |
| 214 | |
| 215 | rc = vfs_getxattr_alloc(dentry, xattr_name, &xattr_value, 0, flags); |
| 216 | if (rc < 0) |
| 217 | return rc; |
| 218 | |
| 219 | if ((rc != size) || (memcmp(xattr_value, value, rc) != 0)) |
| 220 | rc = -EINVAL; |
| 221 | else |
| 222 | rc = 0; |
| 223 | kfree(xattr_value); |
| 224 | return rc; |
| 225 | } |
| 226 | |
David P. Quigley | 4249259 | 2008-02-04 22:29:39 -0800 | [diff] [blame] | 227 | ssize_t |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 228 | vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size) |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 229 | { |
| 230 | struct inode *inode = dentry->d_inode; |
| 231 | int error; |
| 232 | |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 233 | error = xattr_permission(inode, name, MAY_READ); |
| 234 | if (error) |
| 235 | return error; |
| 236 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 237 | error = security_inode_getxattr(dentry, name); |
| 238 | if (error) |
| 239 | return error; |
| 240 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 241 | if (!strncmp(name, XATTR_SECURITY_PREFIX, |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 242 | XATTR_SECURITY_PREFIX_LEN)) { |
| 243 | const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; |
David P. Quigley | 4249259 | 2008-02-04 22:29:39 -0800 | [diff] [blame] | 244 | int ret = xattr_getsecurity(inode, suffix, value, size); |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 245 | /* |
| 246 | * Only overwrite the return value if a security module |
| 247 | * is actually active. |
| 248 | */ |
David P. Quigley | 4bea580 | 2008-02-04 22:29:40 -0800 | [diff] [blame] | 249 | if (ret == -EOPNOTSUPP) |
| 250 | goto nolsm; |
| 251 | return ret; |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 252 | } |
David P. Quigley | 4bea580 | 2008-02-04 22:29:40 -0800 | [diff] [blame] | 253 | nolsm: |
| 254 | if (inode->i_op->getxattr) |
| 255 | error = inode->i_op->getxattr(dentry, name, value, size); |
| 256 | else |
| 257 | error = -EOPNOTSUPP; |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 258 | |
| 259 | return error; |
| 260 | } |
| 261 | EXPORT_SYMBOL_GPL(vfs_getxattr); |
| 262 | |
Bill Nottingham | 659564c | 2006-10-09 16:10:48 -0400 | [diff] [blame] | 263 | ssize_t |
| 264 | vfs_listxattr(struct dentry *d, char *list, size_t size) |
| 265 | { |
| 266 | ssize_t error; |
| 267 | |
| 268 | error = security_inode_listxattr(d); |
| 269 | if (error) |
| 270 | return error; |
| 271 | error = -EOPNOTSUPP; |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 272 | if (d->d_inode->i_op->listxattr) { |
Bill Nottingham | 659564c | 2006-10-09 16:10:48 -0400 | [diff] [blame] | 273 | error = d->d_inode->i_op->listxattr(d, list, size); |
| 274 | } else { |
| 275 | error = security_inode_listsecurity(d->d_inode, list, size); |
| 276 | if (size && error > size) |
| 277 | error = -ERANGE; |
| 278 | } |
| 279 | return error; |
| 280 | } |
| 281 | EXPORT_SYMBOL_GPL(vfs_listxattr); |
| 282 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 283 | int |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 284 | vfs_removexattr(struct dentry *dentry, const char *name) |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 285 | { |
| 286 | struct inode *inode = dentry->d_inode; |
| 287 | int error; |
| 288 | |
| 289 | if (!inode->i_op->removexattr) |
| 290 | return -EOPNOTSUPP; |
| 291 | |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 292 | error = xattr_permission(inode, name, MAY_WRITE); |
| 293 | if (error) |
| 294 | return error; |
| 295 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 296 | error = security_inode_removexattr(dentry, name); |
| 297 | if (error) |
| 298 | return error; |
| 299 | |
| 300 | mutex_lock(&inode->i_mutex); |
| 301 | error = inode->i_op->removexattr(dentry, name); |
| 302 | mutex_unlock(&inode->i_mutex); |
| 303 | |
| 304 | if (!error) |
| 305 | fsnotify_xattr(dentry); |
| 306 | return error; |
| 307 | } |
| 308 | EXPORT_SYMBOL_GPL(vfs_removexattr); |
| 309 | |
| 310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | /* |
| 312 | * Extended attribute SET operations |
| 313 | */ |
| 314 | static long |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 315 | setxattr(struct dentry *d, const char __user *name, const void __user *value, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | size_t size, int flags) |
| 317 | { |
| 318 | int error; |
| 319 | void *kvalue = NULL; |
| 320 | char kname[XATTR_NAME_MAX + 1]; |
| 321 | |
| 322 | if (flags & ~(XATTR_CREATE|XATTR_REPLACE)) |
| 323 | return -EINVAL; |
| 324 | |
| 325 | error = strncpy_from_user(kname, name, sizeof(kname)); |
| 326 | if (error == 0 || error == sizeof(kname)) |
| 327 | error = -ERANGE; |
| 328 | if (error < 0) |
| 329 | return error; |
| 330 | |
| 331 | if (size) { |
| 332 | if (size > XATTR_SIZE_MAX) |
| 333 | return -E2BIG; |
Li Zefan | 3939fcd | 2009-04-08 15:06:12 +0800 | [diff] [blame] | 334 | kvalue = memdup_user(value, size); |
| 335 | if (IS_ERR(kvalue)) |
| 336 | return PTR_ERR(kvalue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 339 | error = vfs_setxattr(d, kname, kvalue, size, flags); |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 340 | kfree(kvalue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | return error; |
| 342 | } |
| 343 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 344 | SYSCALL_DEFINE5(setxattr, const char __user *, pathname, |
| 345 | const char __user *, name, const void __user *, value, |
| 346 | size_t, size, int, flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 348 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | int error; |
| 350 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 351 | error = user_path(pathname, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | if (error) |
| 353 | return error; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 354 | error = mnt_want_write(path.mnt); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 355 | if (!error) { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 356 | error = setxattr(path.dentry, name, value, size, flags); |
| 357 | mnt_drop_write(path.mnt); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 358 | } |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 359 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | return error; |
| 361 | } |
| 362 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 363 | SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname, |
| 364 | const char __user *, name, const void __user *, value, |
| 365 | size_t, size, int, flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 367 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | int error; |
| 369 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 370 | error = user_lpath(pathname, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | if (error) |
| 372 | return error; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 373 | error = mnt_want_write(path.mnt); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 374 | if (!error) { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 375 | error = setxattr(path.dentry, name, value, size, flags); |
| 376 | mnt_drop_write(path.mnt); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 377 | } |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 378 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | return error; |
| 380 | } |
| 381 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 382 | SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name, |
| 383 | const void __user *,value, size_t, size, int, flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | { |
| 385 | struct file *f; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 386 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | int error = -EBADF; |
| 388 | |
| 389 | f = fget(fd); |
| 390 | if (!f) |
| 391 | return error; |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 392 | dentry = f->f_path.dentry; |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 393 | audit_inode(NULL, dentry); |
npiggin@suse.de | 96029c4 | 2009-04-26 20:25:55 +1000 | [diff] [blame] | 394 | error = mnt_want_write_file(f); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 395 | if (!error) { |
| 396 | error = setxattr(dentry, name, value, size, flags); |
| 397 | mnt_drop_write(f->f_path.mnt); |
| 398 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | fput(f); |
| 400 | return error; |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * Extended attribute GET operations |
| 405 | */ |
| 406 | static ssize_t |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 407 | getxattr(struct dentry *d, const char __user *name, void __user *value, |
| 408 | size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | { |
| 410 | ssize_t error; |
| 411 | void *kvalue = NULL; |
| 412 | char kname[XATTR_NAME_MAX + 1]; |
| 413 | |
| 414 | error = strncpy_from_user(kname, name, sizeof(kname)); |
| 415 | if (error == 0 || error == sizeof(kname)) |
| 416 | error = -ERANGE; |
| 417 | if (error < 0) |
| 418 | return error; |
| 419 | |
| 420 | if (size) { |
| 421 | if (size > XATTR_SIZE_MAX) |
| 422 | size = XATTR_SIZE_MAX; |
James Morris | d381d8a | 2005-10-30 14:59:22 -0800 | [diff] [blame] | 423 | kvalue = kzalloc(size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | if (!kvalue) |
| 425 | return -ENOMEM; |
| 426 | } |
| 427 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 428 | error = vfs_getxattr(d, kname, kvalue, size); |
Stephen Smalley | f549d6c | 2005-09-03 15:55:18 -0700 | [diff] [blame] | 429 | if (error > 0) { |
| 430 | if (size && copy_to_user(value, kvalue, error)) |
| 431 | error = -EFAULT; |
| 432 | } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) { |
| 433 | /* The file system tried to returned a value bigger |
| 434 | than XATTR_SIZE_MAX bytes. Not possible. */ |
| 435 | error = -E2BIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | } |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 437 | kfree(kvalue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | return error; |
| 439 | } |
| 440 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 441 | SYSCALL_DEFINE4(getxattr, const char __user *, pathname, |
| 442 | const char __user *, name, void __user *, value, size_t, size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 444 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | ssize_t error; |
| 446 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 447 | error = user_path(pathname, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | if (error) |
| 449 | return error; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 450 | error = getxattr(path.dentry, name, value, size); |
| 451 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | return error; |
| 453 | } |
| 454 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 455 | SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname, |
| 456 | const char __user *, name, void __user *, value, size_t, size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 458 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | ssize_t error; |
| 460 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 461 | error = user_lpath(pathname, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | if (error) |
| 463 | return error; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 464 | error = getxattr(path.dentry, name, value, size); |
| 465 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | return error; |
| 467 | } |
| 468 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 469 | SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name, |
| 470 | void __user *, value, size_t, size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { |
| 472 | struct file *f; |
| 473 | ssize_t error = -EBADF; |
| 474 | |
| 475 | f = fget(fd); |
| 476 | if (!f) |
| 477 | return error; |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 478 | audit_inode(NULL, f->f_path.dentry); |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 479 | error = getxattr(f->f_path.dentry, name, value, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | fput(f); |
| 481 | return error; |
| 482 | } |
| 483 | |
| 484 | /* |
| 485 | * Extended attribute LIST operations |
| 486 | */ |
| 487 | static ssize_t |
| 488 | listxattr(struct dentry *d, char __user *list, size_t size) |
| 489 | { |
| 490 | ssize_t error; |
| 491 | char *klist = NULL; |
| 492 | |
| 493 | if (size) { |
| 494 | if (size > XATTR_LIST_MAX) |
| 495 | size = XATTR_LIST_MAX; |
| 496 | klist = kmalloc(size, GFP_KERNEL); |
| 497 | if (!klist) |
| 498 | return -ENOMEM; |
| 499 | } |
| 500 | |
Bill Nottingham | 659564c | 2006-10-09 16:10:48 -0400 | [diff] [blame] | 501 | error = vfs_listxattr(d, klist, size); |
Stephen Smalley | f549d6c | 2005-09-03 15:55:18 -0700 | [diff] [blame] | 502 | if (error > 0) { |
| 503 | if (size && copy_to_user(list, klist, error)) |
| 504 | error = -EFAULT; |
| 505 | } else if (error == -ERANGE && size >= XATTR_LIST_MAX) { |
| 506 | /* The file system tried to returned a list bigger |
| 507 | than XATTR_LIST_MAX bytes. Not possible. */ |
| 508 | error = -E2BIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | } |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 510 | kfree(klist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | return error; |
| 512 | } |
| 513 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 514 | SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list, |
| 515 | size_t, size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 517 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | ssize_t error; |
| 519 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 520 | error = user_path(pathname, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | if (error) |
| 522 | return error; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 523 | error = listxattr(path.dentry, list, size); |
| 524 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | return error; |
| 526 | } |
| 527 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 528 | SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list, |
| 529 | size_t, size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 531 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | ssize_t error; |
| 533 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 534 | error = user_lpath(pathname, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | if (error) |
| 536 | return error; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 537 | error = listxattr(path.dentry, list, size); |
| 538 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | return error; |
| 540 | } |
| 541 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 542 | SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | { |
| 544 | struct file *f; |
| 545 | ssize_t error = -EBADF; |
| 546 | |
| 547 | f = fget(fd); |
| 548 | if (!f) |
| 549 | return error; |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 550 | audit_inode(NULL, f->f_path.dentry); |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 551 | error = listxattr(f->f_path.dentry, list, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | fput(f); |
| 553 | return error; |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | * Extended attribute REMOVE operations |
| 558 | */ |
| 559 | static long |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 560 | removexattr(struct dentry *d, const char __user *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | { |
| 562 | int error; |
| 563 | char kname[XATTR_NAME_MAX + 1]; |
| 564 | |
| 565 | error = strncpy_from_user(kname, name, sizeof(kname)); |
| 566 | if (error == 0 || error == sizeof(kname)) |
| 567 | error = -ERANGE; |
| 568 | if (error < 0) |
| 569 | return error; |
| 570 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 571 | return vfs_removexattr(d, kname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Heiko Carstens | 64fd1de | 2009-01-14 14:14:14 +0100 | [diff] [blame] | 574 | SYSCALL_DEFINE2(removexattr, const char __user *, pathname, |
| 575 | const char __user *, name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 577 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | int error; |
| 579 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 580 | error = user_path(pathname, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | if (error) |
| 582 | return error; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 583 | error = mnt_want_write(path.mnt); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 584 | if (!error) { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 585 | error = removexattr(path.dentry, name); |
| 586 | mnt_drop_write(path.mnt); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 587 | } |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 588 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | return error; |
| 590 | } |
| 591 | |
Heiko Carstens | 6a6160a | 2009-01-14 14:14:15 +0100 | [diff] [blame] | 592 | SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname, |
| 593 | const char __user *, name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 595 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | int error; |
| 597 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 598 | error = user_lpath(pathname, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | if (error) |
| 600 | return error; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 601 | error = mnt_want_write(path.mnt); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 602 | if (!error) { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 603 | error = removexattr(path.dentry, name); |
| 604 | mnt_drop_write(path.mnt); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 605 | } |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 606 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | return error; |
| 608 | } |
| 609 | |
Heiko Carstens | 6a6160a | 2009-01-14 14:14:15 +0100 | [diff] [blame] | 610 | SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | { |
| 612 | struct file *f; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 613 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | int error = -EBADF; |
| 615 | |
| 616 | f = fget(fd); |
| 617 | if (!f) |
| 618 | return error; |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 619 | dentry = f->f_path.dentry; |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 620 | audit_inode(NULL, dentry); |
npiggin@suse.de | 96029c4 | 2009-04-26 20:25:55 +1000 | [diff] [blame] | 621 | error = mnt_want_write_file(f); |
Dave Hansen | 18f335a | 2008-02-15 14:37:38 -0800 | [diff] [blame] | 622 | if (!error) { |
| 623 | error = removexattr(dentry, name); |
| 624 | mnt_drop_write(f->f_path.mnt); |
| 625 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | fput(f); |
| 627 | return error; |
| 628 | } |
| 629 | |
| 630 | |
| 631 | static const char * |
| 632 | strcmp_prefix(const char *a, const char *a_prefix) |
| 633 | { |
| 634 | while (*a_prefix && *a == *a_prefix) { |
| 635 | a++; |
| 636 | a_prefix++; |
| 637 | } |
| 638 | return *a_prefix ? NULL : a; |
| 639 | } |
| 640 | |
| 641 | /* |
| 642 | * In order to implement different sets of xattr operations for each xattr |
| 643 | * prefix with the generic xattr API, a filesystem should create a |
| 644 | * null-terminated array of struct xattr_handler (one for each prefix) and |
| 645 | * hang a pointer to it off of the s_xattr field of the superblock. |
| 646 | * |
| 647 | * The generic_fooxattr() functions will use this list to dispatch xattr |
| 648 | * operations to the correct xattr_handler. |
| 649 | */ |
| 650 | #define for_each_xattr_handler(handlers, handler) \ |
| 651 | for ((handler) = *(handlers)++; \ |
| 652 | (handler) != NULL; \ |
| 653 | (handler) = *(handlers)++) |
| 654 | |
| 655 | /* |
| 656 | * Find the xattr_handler with the matching prefix. |
| 657 | */ |
Stephen Hemminger | bb43545 | 2010-05-13 17:53:14 -0700 | [diff] [blame] | 658 | static const struct xattr_handler * |
| 659 | xattr_resolve_name(const struct xattr_handler **handlers, const char **name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | { |
Stephen Hemminger | bb43545 | 2010-05-13 17:53:14 -0700 | [diff] [blame] | 661 | const struct xattr_handler *handler; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
| 663 | if (!*name) |
| 664 | return NULL; |
| 665 | |
| 666 | for_each_xattr_handler(handlers, handler) { |
| 667 | const char *n = strcmp_prefix(*name, handler->prefix); |
| 668 | if (n) { |
| 669 | *name = n; |
| 670 | break; |
| 671 | } |
| 672 | } |
| 673 | return handler; |
| 674 | } |
| 675 | |
| 676 | /* |
| 677 | * Find the handler for the prefix and dispatch its get() operation. |
| 678 | */ |
| 679 | ssize_t |
| 680 | generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size) |
| 681 | { |
Stephen Hemminger | bb43545 | 2010-05-13 17:53:14 -0700 | [diff] [blame] | 682 | const struct xattr_handler *handler; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 684 | handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | if (!handler) |
| 686 | return -EOPNOTSUPP; |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 687 | return handler->get(dentry, name, buffer, size, handler->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /* |
| 691 | * Combine the results of the list() operation from every xattr_handler in the |
| 692 | * list. |
| 693 | */ |
| 694 | ssize_t |
| 695 | generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) |
| 696 | { |
Stephen Hemminger | bb43545 | 2010-05-13 17:53:14 -0700 | [diff] [blame] | 697 | const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | unsigned int size = 0; |
| 699 | |
| 700 | if (!buffer) { |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 701 | for_each_xattr_handler(handlers, handler) { |
| 702 | size += handler->list(dentry, NULL, 0, NULL, 0, |
| 703 | handler->flags); |
| 704 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } else { |
| 706 | char *buf = buffer; |
| 707 | |
| 708 | for_each_xattr_handler(handlers, handler) { |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 709 | size = handler->list(dentry, buf, buffer_size, |
| 710 | NULL, 0, handler->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | if (size > buffer_size) |
| 712 | return -ERANGE; |
| 713 | buf += size; |
| 714 | buffer_size -= size; |
| 715 | } |
| 716 | size = buf - buffer; |
| 717 | } |
| 718 | return size; |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | * Find the handler for the prefix and dispatch its set() operation. |
| 723 | */ |
| 724 | int |
| 725 | generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags) |
| 726 | { |
Stephen Hemminger | bb43545 | 2010-05-13 17:53:14 -0700 | [diff] [blame] | 727 | const struct xattr_handler *handler; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
| 729 | if (size == 0) |
| 730 | value = ""; /* empty EA, do not remove */ |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 731 | handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | if (!handler) |
| 733 | return -EOPNOTSUPP; |
Jan Kara | df7e130 | 2011-04-20 20:30:40 +0200 | [diff] [blame] | 734 | return handler->set(dentry, name, value, size, flags, handler->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /* |
| 738 | * Find the handler for the prefix and dispatch its set() operation to remove |
| 739 | * any associated extended attribute. |
| 740 | */ |
| 741 | int |
| 742 | generic_removexattr(struct dentry *dentry, const char *name) |
| 743 | { |
Stephen Hemminger | bb43545 | 2010-05-13 17:53:14 -0700 | [diff] [blame] | 744 | const struct xattr_handler *handler; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 746 | handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | if (!handler) |
| 748 | return -EOPNOTSUPP; |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 749 | return handler->set(dentry, name, NULL, 0, |
| 750 | XATTR_REPLACE, handler->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | EXPORT_SYMBOL(generic_getxattr); |
| 754 | EXPORT_SYMBOL(generic_listxattr); |
| 755 | EXPORT_SYMBOL(generic_setxattr); |
| 756 | EXPORT_SYMBOL(generic_removexattr); |