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> |
| 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> |
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) { |
| 36 | if (IS_RDONLY(inode)) |
| 37 | return -EROFS; |
| 38 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 39 | return -EPERM; |
| 40 | } |
| 41 | |
| 42 | /* |
| 43 | * No restriction for security.* and system.* from the VFS. Decision |
| 44 | * on these is left to the underlying filesystem / security module. |
| 45 | */ |
| 46 | if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) || |
| 47 | !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) |
| 48 | return 0; |
| 49 | |
| 50 | /* |
| 51 | * The trusted.* namespace can only accessed by a privilegued user. |
| 52 | */ |
| 53 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) |
| 54 | return (capable(CAP_SYS_ADMIN) ? 0 : -EPERM); |
| 55 | |
| 56 | if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) { |
| 57 | if (!S_ISREG(inode->i_mode) && |
| 58 | (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX)) |
| 59 | return -EPERM; |
| 60 | } |
| 61 | |
| 62 | return permission(inode, mask, NULL); |
| 63 | } |
| 64 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 65 | int |
| 66 | vfs_setxattr(struct dentry *dentry, char *name, void *value, |
| 67 | size_t size, int flags) |
| 68 | { |
| 69 | struct inode *inode = dentry->d_inode; |
| 70 | int error; |
| 71 | |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 72 | error = xattr_permission(inode, name, MAY_WRITE); |
| 73 | if (error) |
| 74 | return error; |
| 75 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 76 | mutex_lock(&inode->i_mutex); |
| 77 | error = security_inode_setxattr(dentry, name, value, size, flags); |
| 78 | if (error) |
| 79 | goto out; |
| 80 | error = -EOPNOTSUPP; |
| 81 | if (inode->i_op->setxattr) { |
| 82 | error = inode->i_op->setxattr(dentry, name, value, size, flags); |
| 83 | if (!error) { |
| 84 | fsnotify_xattr(dentry); |
| 85 | security_inode_post_setxattr(dentry, name, value, |
| 86 | size, flags); |
| 87 | } |
| 88 | } else if (!strncmp(name, XATTR_SECURITY_PREFIX, |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 89 | XATTR_SECURITY_PREFIX_LEN)) { |
| 90 | const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 91 | error = security_inode_setsecurity(inode, suffix, value, |
| 92 | size, flags); |
| 93 | if (!error) |
| 94 | fsnotify_xattr(dentry); |
| 95 | } |
| 96 | out: |
| 97 | mutex_unlock(&inode->i_mutex); |
| 98 | return error; |
| 99 | } |
| 100 | EXPORT_SYMBOL_GPL(vfs_setxattr); |
| 101 | |
| 102 | ssize_t |
| 103 | vfs_getxattr(struct dentry *dentry, char *name, void *value, size_t size) |
| 104 | { |
| 105 | struct inode *inode = dentry->d_inode; |
| 106 | int error; |
| 107 | |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 108 | error = xattr_permission(inode, name, MAY_READ); |
| 109 | if (error) |
| 110 | return error; |
| 111 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 112 | error = security_inode_getxattr(dentry, name); |
| 113 | if (error) |
| 114 | return error; |
| 115 | |
| 116 | if (inode->i_op->getxattr) |
| 117 | error = inode->i_op->getxattr(dentry, name, value, size); |
| 118 | else |
| 119 | error = -EOPNOTSUPP; |
| 120 | |
| 121 | if (!strncmp(name, XATTR_SECURITY_PREFIX, |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 122 | XATTR_SECURITY_PREFIX_LEN)) { |
| 123 | const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 124 | int ret = security_inode_getsecurity(inode, suffix, value, |
| 125 | size, error); |
| 126 | /* |
| 127 | * Only overwrite the return value if a security module |
| 128 | * is actually active. |
| 129 | */ |
| 130 | if (ret != -EOPNOTSUPP) |
| 131 | error = ret; |
| 132 | } |
| 133 | |
| 134 | return error; |
| 135 | } |
| 136 | EXPORT_SYMBOL_GPL(vfs_getxattr); |
| 137 | |
Bill Nottingham | 659564c | 2006-10-09 16:10:48 -0400 | [diff] [blame^] | 138 | ssize_t |
| 139 | vfs_listxattr(struct dentry *d, char *list, size_t size) |
| 140 | { |
| 141 | ssize_t error; |
| 142 | |
| 143 | error = security_inode_listxattr(d); |
| 144 | if (error) |
| 145 | return error; |
| 146 | error = -EOPNOTSUPP; |
| 147 | if (d->d_inode->i_op && d->d_inode->i_op->listxattr) { |
| 148 | error = d->d_inode->i_op->listxattr(d, list, size); |
| 149 | } else { |
| 150 | error = security_inode_listsecurity(d->d_inode, list, size); |
| 151 | if (size && error > size) |
| 152 | error = -ERANGE; |
| 153 | } |
| 154 | return error; |
| 155 | } |
| 156 | EXPORT_SYMBOL_GPL(vfs_listxattr); |
| 157 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 158 | int |
| 159 | vfs_removexattr(struct dentry *dentry, char *name) |
| 160 | { |
| 161 | struct inode *inode = dentry->d_inode; |
| 162 | int error; |
| 163 | |
| 164 | if (!inode->i_op->removexattr) |
| 165 | return -EOPNOTSUPP; |
| 166 | |
akpm@osdl.org | e0ad7b0 | 2006-01-09 20:51:56 -0800 | [diff] [blame] | 167 | error = xattr_permission(inode, name, MAY_WRITE); |
| 168 | if (error) |
| 169 | return error; |
| 170 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 171 | error = security_inode_removexattr(dentry, name); |
| 172 | if (error) |
| 173 | return error; |
| 174 | |
| 175 | mutex_lock(&inode->i_mutex); |
| 176 | error = inode->i_op->removexattr(dentry, name); |
| 177 | mutex_unlock(&inode->i_mutex); |
| 178 | |
| 179 | if (!error) |
| 180 | fsnotify_xattr(dentry); |
| 181 | return error; |
| 182 | } |
| 183 | EXPORT_SYMBOL_GPL(vfs_removexattr); |
| 184 | |
| 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | /* |
| 187 | * Extended attribute SET operations |
| 188 | */ |
| 189 | static long |
| 190 | setxattr(struct dentry *d, char __user *name, void __user *value, |
| 191 | size_t size, int flags) |
| 192 | { |
| 193 | int error; |
| 194 | void *kvalue = NULL; |
| 195 | char kname[XATTR_NAME_MAX + 1]; |
| 196 | |
| 197 | if (flags & ~(XATTR_CREATE|XATTR_REPLACE)) |
| 198 | return -EINVAL; |
| 199 | |
| 200 | error = strncpy_from_user(kname, name, sizeof(kname)); |
| 201 | if (error == 0 || error == sizeof(kname)) |
| 202 | error = -ERANGE; |
| 203 | if (error < 0) |
| 204 | return error; |
| 205 | |
| 206 | if (size) { |
| 207 | if (size > XATTR_SIZE_MAX) |
| 208 | return -E2BIG; |
| 209 | kvalue = kmalloc(size, GFP_KERNEL); |
| 210 | if (!kvalue) |
| 211 | return -ENOMEM; |
| 212 | if (copy_from_user(kvalue, value, size)) { |
| 213 | kfree(kvalue); |
| 214 | return -EFAULT; |
| 215 | } |
| 216 | } |
| 217 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 218 | error = vfs_setxattr(d, kname, kvalue, size, flags); |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 219 | kfree(kvalue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | return error; |
| 221 | } |
| 222 | |
| 223 | asmlinkage long |
| 224 | sys_setxattr(char __user *path, char __user *name, void __user *value, |
| 225 | size_t size, int flags) |
| 226 | { |
| 227 | struct nameidata nd; |
| 228 | int error; |
| 229 | |
| 230 | error = user_path_walk(path, &nd); |
| 231 | if (error) |
| 232 | return error; |
| 233 | error = setxattr(nd.dentry, name, value, size, flags); |
| 234 | path_release(&nd); |
| 235 | return error; |
| 236 | } |
| 237 | |
| 238 | asmlinkage long |
| 239 | sys_lsetxattr(char __user *path, char __user *name, void __user *value, |
| 240 | size_t size, int flags) |
| 241 | { |
| 242 | struct nameidata nd; |
| 243 | int error; |
| 244 | |
| 245 | error = user_path_walk_link(path, &nd); |
| 246 | if (error) |
| 247 | return error; |
| 248 | error = setxattr(nd.dentry, name, value, size, flags); |
| 249 | path_release(&nd); |
| 250 | return error; |
| 251 | } |
| 252 | |
| 253 | asmlinkage long |
| 254 | sys_fsetxattr(int fd, char __user *name, void __user *value, |
| 255 | size_t size, int flags) |
| 256 | { |
| 257 | struct file *f; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 258 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | int error = -EBADF; |
| 260 | |
| 261 | f = fget(fd); |
| 262 | if (!f) |
| 263 | return error; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 264 | dentry = f->f_dentry; |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 265 | audit_inode(NULL, dentry->d_inode); |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 266 | error = setxattr(dentry, name, value, size, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | fput(f); |
| 268 | return error; |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Extended attribute GET operations |
| 273 | */ |
| 274 | static ssize_t |
| 275 | getxattr(struct dentry *d, char __user *name, void __user *value, size_t size) |
| 276 | { |
| 277 | ssize_t error; |
| 278 | void *kvalue = NULL; |
| 279 | char kname[XATTR_NAME_MAX + 1]; |
| 280 | |
| 281 | error = strncpy_from_user(kname, name, sizeof(kname)); |
| 282 | if (error == 0 || error == sizeof(kname)) |
| 283 | error = -ERANGE; |
| 284 | if (error < 0) |
| 285 | return error; |
| 286 | |
| 287 | if (size) { |
| 288 | if (size > XATTR_SIZE_MAX) |
| 289 | size = XATTR_SIZE_MAX; |
James Morris | d381d8a | 2005-10-30 14:59:22 -0800 | [diff] [blame] | 290 | kvalue = kzalloc(size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | if (!kvalue) |
| 292 | return -ENOMEM; |
| 293 | } |
| 294 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 295 | error = vfs_getxattr(d, kname, kvalue, size); |
Stephen Smalley | f549d6c | 2005-09-03 15:55:18 -0700 | [diff] [blame] | 296 | if (error > 0) { |
| 297 | if (size && copy_to_user(value, kvalue, error)) |
| 298 | error = -EFAULT; |
| 299 | } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) { |
| 300 | /* The file system tried to returned a value bigger |
| 301 | than XATTR_SIZE_MAX bytes. Not possible. */ |
| 302 | error = -E2BIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 304 | kfree(kvalue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | return error; |
| 306 | } |
| 307 | |
| 308 | asmlinkage ssize_t |
| 309 | sys_getxattr(char __user *path, char __user *name, void __user *value, |
| 310 | size_t size) |
| 311 | { |
| 312 | struct nameidata nd; |
| 313 | ssize_t error; |
| 314 | |
| 315 | error = user_path_walk(path, &nd); |
| 316 | if (error) |
| 317 | return error; |
| 318 | error = getxattr(nd.dentry, name, value, size); |
| 319 | path_release(&nd); |
| 320 | return error; |
| 321 | } |
| 322 | |
| 323 | asmlinkage ssize_t |
| 324 | sys_lgetxattr(char __user *path, char __user *name, void __user *value, |
| 325 | size_t size) |
| 326 | { |
| 327 | struct nameidata nd; |
| 328 | ssize_t error; |
| 329 | |
| 330 | error = user_path_walk_link(path, &nd); |
| 331 | if (error) |
| 332 | return error; |
| 333 | error = getxattr(nd.dentry, name, value, size); |
| 334 | path_release(&nd); |
| 335 | return error; |
| 336 | } |
| 337 | |
| 338 | asmlinkage ssize_t |
| 339 | sys_fgetxattr(int fd, char __user *name, void __user *value, size_t size) |
| 340 | { |
| 341 | struct file *f; |
| 342 | ssize_t error = -EBADF; |
| 343 | |
| 344 | f = fget(fd); |
| 345 | if (!f) |
| 346 | return error; |
| 347 | error = getxattr(f->f_dentry, name, value, size); |
| 348 | fput(f); |
| 349 | return error; |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Extended attribute LIST operations |
| 354 | */ |
| 355 | static ssize_t |
| 356 | listxattr(struct dentry *d, char __user *list, size_t size) |
| 357 | { |
| 358 | ssize_t error; |
| 359 | char *klist = NULL; |
| 360 | |
| 361 | if (size) { |
| 362 | if (size > XATTR_LIST_MAX) |
| 363 | size = XATTR_LIST_MAX; |
| 364 | klist = kmalloc(size, GFP_KERNEL); |
| 365 | if (!klist) |
| 366 | return -ENOMEM; |
| 367 | } |
| 368 | |
Bill Nottingham | 659564c | 2006-10-09 16:10:48 -0400 | [diff] [blame^] | 369 | error = vfs_listxattr(d, klist, size); |
Stephen Smalley | f549d6c | 2005-09-03 15:55:18 -0700 | [diff] [blame] | 370 | if (error > 0) { |
| 371 | if (size && copy_to_user(list, klist, error)) |
| 372 | error = -EFAULT; |
| 373 | } else if (error == -ERANGE && size >= XATTR_LIST_MAX) { |
| 374 | /* The file system tried to returned a list bigger |
| 375 | than XATTR_LIST_MAX bytes. Not possible. */ |
| 376 | error = -E2BIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | } |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 378 | kfree(klist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | return error; |
| 380 | } |
| 381 | |
| 382 | asmlinkage ssize_t |
| 383 | sys_listxattr(char __user *path, char __user *list, size_t size) |
| 384 | { |
| 385 | struct nameidata nd; |
| 386 | ssize_t error; |
| 387 | |
| 388 | error = user_path_walk(path, &nd); |
| 389 | if (error) |
| 390 | return error; |
| 391 | error = listxattr(nd.dentry, list, size); |
| 392 | path_release(&nd); |
| 393 | return error; |
| 394 | } |
| 395 | |
| 396 | asmlinkage ssize_t |
| 397 | sys_llistxattr(char __user *path, char __user *list, size_t size) |
| 398 | { |
| 399 | struct nameidata nd; |
| 400 | ssize_t error; |
| 401 | |
| 402 | error = user_path_walk_link(path, &nd); |
| 403 | if (error) |
| 404 | return error; |
| 405 | error = listxattr(nd.dentry, list, size); |
| 406 | path_release(&nd); |
| 407 | return error; |
| 408 | } |
| 409 | |
| 410 | asmlinkage ssize_t |
| 411 | sys_flistxattr(int fd, char __user *list, size_t size) |
| 412 | { |
| 413 | struct file *f; |
| 414 | ssize_t error = -EBADF; |
| 415 | |
| 416 | f = fget(fd); |
| 417 | if (!f) |
| 418 | return error; |
| 419 | error = listxattr(f->f_dentry, list, size); |
| 420 | fput(f); |
| 421 | return error; |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * Extended attribute REMOVE operations |
| 426 | */ |
| 427 | static long |
| 428 | removexattr(struct dentry *d, char __user *name) |
| 429 | { |
| 430 | int error; |
| 431 | char kname[XATTR_NAME_MAX + 1]; |
| 432 | |
| 433 | error = strncpy_from_user(kname, name, sizeof(kname)); |
| 434 | if (error == 0 || error == sizeof(kname)) |
| 435 | error = -ERANGE; |
| 436 | if (error < 0) |
| 437 | return error; |
| 438 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 439 | return vfs_removexattr(d, kname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | asmlinkage long |
| 443 | sys_removexattr(char __user *path, char __user *name) |
| 444 | { |
| 445 | struct nameidata nd; |
| 446 | int error; |
| 447 | |
| 448 | error = user_path_walk(path, &nd); |
| 449 | if (error) |
| 450 | return error; |
| 451 | error = removexattr(nd.dentry, name); |
| 452 | path_release(&nd); |
| 453 | return error; |
| 454 | } |
| 455 | |
| 456 | asmlinkage long |
| 457 | sys_lremovexattr(char __user *path, char __user *name) |
| 458 | { |
| 459 | struct nameidata nd; |
| 460 | int error; |
| 461 | |
| 462 | error = user_path_walk_link(path, &nd); |
| 463 | if (error) |
| 464 | return error; |
| 465 | error = removexattr(nd.dentry, name); |
| 466 | path_release(&nd); |
| 467 | return error; |
| 468 | } |
| 469 | |
| 470 | asmlinkage long |
| 471 | sys_fremovexattr(int fd, char __user *name) |
| 472 | { |
| 473 | struct file *f; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 474 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | int error = -EBADF; |
| 476 | |
| 477 | f = fget(fd); |
| 478 | if (!f) |
| 479 | return error; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 480 | dentry = f->f_dentry; |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 481 | audit_inode(NULL, dentry->d_inode); |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 482 | error = removexattr(dentry, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | fput(f); |
| 484 | return error; |
| 485 | } |
| 486 | |
| 487 | |
| 488 | static const char * |
| 489 | strcmp_prefix(const char *a, const char *a_prefix) |
| 490 | { |
| 491 | while (*a_prefix && *a == *a_prefix) { |
| 492 | a++; |
| 493 | a_prefix++; |
| 494 | } |
| 495 | return *a_prefix ? NULL : a; |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * In order to implement different sets of xattr operations for each xattr |
| 500 | * prefix with the generic xattr API, a filesystem should create a |
| 501 | * null-terminated array of struct xattr_handler (one for each prefix) and |
| 502 | * hang a pointer to it off of the s_xattr field of the superblock. |
| 503 | * |
| 504 | * The generic_fooxattr() functions will use this list to dispatch xattr |
| 505 | * operations to the correct xattr_handler. |
| 506 | */ |
| 507 | #define for_each_xattr_handler(handlers, handler) \ |
| 508 | for ((handler) = *(handlers)++; \ |
| 509 | (handler) != NULL; \ |
| 510 | (handler) = *(handlers)++) |
| 511 | |
| 512 | /* |
| 513 | * Find the xattr_handler with the matching prefix. |
| 514 | */ |
| 515 | static struct xattr_handler * |
| 516 | xattr_resolve_name(struct xattr_handler **handlers, const char **name) |
| 517 | { |
| 518 | struct xattr_handler *handler; |
| 519 | |
| 520 | if (!*name) |
| 521 | return NULL; |
| 522 | |
| 523 | for_each_xattr_handler(handlers, handler) { |
| 524 | const char *n = strcmp_prefix(*name, handler->prefix); |
| 525 | if (n) { |
| 526 | *name = n; |
| 527 | break; |
| 528 | } |
| 529 | } |
| 530 | return handler; |
| 531 | } |
| 532 | |
| 533 | /* |
| 534 | * Find the handler for the prefix and dispatch its get() operation. |
| 535 | */ |
| 536 | ssize_t |
| 537 | generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size) |
| 538 | { |
| 539 | struct xattr_handler *handler; |
| 540 | struct inode *inode = dentry->d_inode; |
| 541 | |
| 542 | handler = xattr_resolve_name(inode->i_sb->s_xattr, &name); |
| 543 | if (!handler) |
| 544 | return -EOPNOTSUPP; |
| 545 | return handler->get(inode, name, buffer, size); |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | * Combine the results of the list() operation from every xattr_handler in the |
| 550 | * list. |
| 551 | */ |
| 552 | ssize_t |
| 553 | generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) |
| 554 | { |
| 555 | struct inode *inode = dentry->d_inode; |
| 556 | struct xattr_handler *handler, **handlers = inode->i_sb->s_xattr; |
| 557 | unsigned int size = 0; |
| 558 | |
| 559 | if (!buffer) { |
| 560 | for_each_xattr_handler(handlers, handler) |
| 561 | size += handler->list(inode, NULL, 0, NULL, 0); |
| 562 | } else { |
| 563 | char *buf = buffer; |
| 564 | |
| 565 | for_each_xattr_handler(handlers, handler) { |
| 566 | size = handler->list(inode, buf, buffer_size, NULL, 0); |
| 567 | if (size > buffer_size) |
| 568 | return -ERANGE; |
| 569 | buf += size; |
| 570 | buffer_size -= size; |
| 571 | } |
| 572 | size = buf - buffer; |
| 573 | } |
| 574 | return size; |
| 575 | } |
| 576 | |
| 577 | /* |
| 578 | * Find the handler for the prefix and dispatch its set() operation. |
| 579 | */ |
| 580 | int |
| 581 | generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags) |
| 582 | { |
| 583 | struct xattr_handler *handler; |
| 584 | struct inode *inode = dentry->d_inode; |
| 585 | |
| 586 | if (size == 0) |
| 587 | value = ""; /* empty EA, do not remove */ |
| 588 | handler = xattr_resolve_name(inode->i_sb->s_xattr, &name); |
| 589 | if (!handler) |
| 590 | return -EOPNOTSUPP; |
| 591 | return handler->set(inode, name, value, size, flags); |
| 592 | } |
| 593 | |
| 594 | /* |
| 595 | * Find the handler for the prefix and dispatch its set() operation to remove |
| 596 | * any associated extended attribute. |
| 597 | */ |
| 598 | int |
| 599 | generic_removexattr(struct dentry *dentry, const char *name) |
| 600 | { |
| 601 | struct xattr_handler *handler; |
| 602 | struct inode *inode = dentry->d_inode; |
| 603 | |
| 604 | handler = xattr_resolve_name(inode->i_sb->s_xattr, &name); |
| 605 | if (!handler) |
| 606 | return -EOPNOTSUPP; |
| 607 | return handler->set(inode, name, NULL, 0, XATTR_REPLACE); |
| 608 | } |
| 609 | |
| 610 | EXPORT_SYMBOL(generic_getxattr); |
| 611 | EXPORT_SYMBOL(generic_listxattr); |
| 612 | EXPORT_SYMBOL(generic_setxattr); |
| 613 | EXPORT_SYMBOL(generic_removexattr); |