Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

btrfs: Use xattr handler infrastructure

Use the VFS xattr handler infrastructure and get rid of similar code in
the filesystem.

Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>

authored by

Andreas Gruenbacher and committed by
Al Viro
9172abbc 98e9cb57

+64 -114
+4 -4
fs/btrfs/inode.c
··· 9994 9994 .setattr = btrfs_setattr, 9995 9995 .mknod = btrfs_mknod, 9996 9996 .setxattr = btrfs_setxattr, 9997 - .getxattr = btrfs_getxattr, 9997 + .getxattr = generic_getxattr, 9998 9998 .listxattr = btrfs_listxattr, 9999 9999 .removexattr = btrfs_removexattr, 10000 10000 .permission = btrfs_permission, ··· 10071 10071 .getattr = btrfs_getattr, 10072 10072 .setattr = btrfs_setattr, 10073 10073 .setxattr = btrfs_setxattr, 10074 - .getxattr = btrfs_getxattr, 10074 + .getxattr = generic_getxattr, 10075 10075 .listxattr = btrfs_listxattr, 10076 10076 .removexattr = btrfs_removexattr, 10077 10077 .permission = btrfs_permission, ··· 10085 10085 .setattr = btrfs_setattr, 10086 10086 .permission = btrfs_permission, 10087 10087 .setxattr = btrfs_setxattr, 10088 - .getxattr = btrfs_getxattr, 10088 + .getxattr = generic_getxattr, 10089 10089 .listxattr = btrfs_listxattr, 10090 10090 .removexattr = btrfs_removexattr, 10091 10091 .get_acl = btrfs_get_acl, ··· 10100 10100 .setattr = btrfs_setattr, 10101 10101 .permission = btrfs_permission, 10102 10102 .setxattr = btrfs_setxattr, 10103 - .getxattr = btrfs_getxattr, 10103 + .getxattr = generic_getxattr, 10104 10104 .listxattr = btrfs_listxattr, 10105 10105 .removexattr = btrfs_removexattr, 10106 10106 .update_time = btrfs_update_time,
+60 -108
fs/btrfs/xattr.c
··· 351 351 return ret; 352 352 } 353 353 354 - /* 355 - * List of handlers for synthetic system.* attributes. All real ondisk 356 - * attributes are handled directly. 357 - */ 354 + static int btrfs_xattr_handler_get(const struct xattr_handler *handler, 355 + struct dentry *dentry, const char *name, 356 + void *buffer, size_t size) 357 + { 358 + struct inode *inode = d_inode(dentry); 359 + 360 + name = xattr_full_name(handler, name); 361 + return __btrfs_getxattr(inode, name, buffer, size); 362 + } 363 + 364 + static int btrfs_xattr_handler_set(const struct xattr_handler *handler, 365 + struct dentry *dentry, const char *name, 366 + const void *buffer, size_t size, 367 + int flags) 368 + { 369 + struct inode *inode = d_inode(dentry); 370 + 371 + name = xattr_full_name(handler, name); 372 + return __btrfs_setxattr(NULL, inode, name, buffer, size, flags); 373 + } 374 + 375 + static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler, 376 + struct dentry *dentry, 377 + const char *name, const void *value, 378 + size_t size, int flags) 379 + { 380 + name = xattr_full_name(handler, name); 381 + return btrfs_set_prop(d_inode(dentry), name, value, size, flags); 382 + } 383 + 384 + static const struct xattr_handler btrfs_security_xattr_handler = { 385 + .prefix = XATTR_SECURITY_PREFIX, 386 + .get = btrfs_xattr_handler_get, 387 + .set = btrfs_xattr_handler_set, 388 + }; 389 + 390 + static const struct xattr_handler btrfs_trusted_xattr_handler = { 391 + .prefix = XATTR_TRUSTED_PREFIX, 392 + .get = btrfs_xattr_handler_get, 393 + .set = btrfs_xattr_handler_set, 394 + }; 395 + 396 + static const struct xattr_handler btrfs_user_xattr_handler = { 397 + .prefix = XATTR_USER_PREFIX, 398 + .get = btrfs_xattr_handler_get, 399 + .set = btrfs_xattr_handler_set, 400 + }; 401 + 402 + static const struct xattr_handler btrfs_btrfs_xattr_handler = { 403 + .prefix = XATTR_BTRFS_PREFIX, 404 + .get = btrfs_xattr_handler_get, 405 + .set = btrfs_xattr_handler_set_prop, 406 + }; 407 + 358 408 const struct xattr_handler *btrfs_xattr_handlers[] = { 409 + &btrfs_security_xattr_handler, 359 410 #ifdef CONFIG_BTRFS_FS_POSIX_ACL 360 411 &posix_acl_access_xattr_handler, 361 412 &posix_acl_default_xattr_handler, 362 413 #endif 414 + &btrfs_trusted_xattr_handler, 415 + &btrfs_user_xattr_handler, 416 + &btrfs_btrfs_xattr_handler, 363 417 NULL, 364 418 }; 365 - 366 - /* 367 - * Check if the attribute is in a supported namespace. 368 - * 369 - * This is applied after the check for the synthetic attributes in the system 370 - * namespace. 371 - */ 372 - static int btrfs_is_valid_xattr(const char *name) 373 - { 374 - int len = strlen(name); 375 - int prefixlen = 0; 376 - 377 - if (!strncmp(name, XATTR_SECURITY_PREFIX, 378 - XATTR_SECURITY_PREFIX_LEN)) 379 - prefixlen = XATTR_SECURITY_PREFIX_LEN; 380 - else if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 381 - prefixlen = XATTR_SYSTEM_PREFIX_LEN; 382 - else if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) 383 - prefixlen = XATTR_TRUSTED_PREFIX_LEN; 384 - else if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) 385 - prefixlen = XATTR_USER_PREFIX_LEN; 386 - else if (!strncmp(name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN)) 387 - prefixlen = XATTR_BTRFS_PREFIX_LEN; 388 - else 389 - return -EOPNOTSUPP; 390 - 391 - /* 392 - * The name cannot consist of just prefix 393 - */ 394 - if (len <= prefixlen) 395 - return -EINVAL; 396 - 397 - return 0; 398 - } 399 - 400 - ssize_t btrfs_getxattr(struct dentry *dentry, const char *name, 401 - void *buffer, size_t size) 402 - { 403 - int ret; 404 - 405 - /* 406 - * If this is a request for a synthetic attribute in the system.* 407 - * namespace use the generic infrastructure to resolve a handler 408 - * for it via sb->s_xattr. 409 - */ 410 - if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 411 - return generic_getxattr(dentry, name, buffer, size); 412 - 413 - ret = btrfs_is_valid_xattr(name); 414 - if (ret) 415 - return ret; 416 - return __btrfs_getxattr(d_inode(dentry), name, buffer, size); 417 - } 418 419 419 420 int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value, 420 421 size_t size, int flags) 421 422 { 422 423 struct btrfs_root *root = BTRFS_I(d_inode(dentry))->root; 423 - int ret; 424 424 425 - /* 426 - * The permission on security.* and system.* is not checked 427 - * in permission(). 428 - */ 429 425 if (btrfs_root_readonly(root)) 430 426 return -EROFS; 431 - 432 - /* 433 - * If this is a request for a synthetic attribute in the system.* 434 - * namespace use the generic infrastructure to resolve a handler 435 - * for it via sb->s_xattr. 436 - */ 437 - if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 438 - return generic_setxattr(dentry, name, value, size, flags); 439 - 440 - ret = btrfs_is_valid_xattr(name); 441 - if (ret) 442 - return ret; 443 - 444 - if (!strncmp(name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN)) 445 - return btrfs_set_prop(d_inode(dentry), name, 446 - value, size, flags); 447 - 448 - if (size == 0) 449 - value = ""; /* empty EA, do not remove */ 450 - 451 - return __btrfs_setxattr(NULL, d_inode(dentry), name, value, size, 452 - flags); 427 + return generic_setxattr(dentry, name, value, size, flags); 453 428 } 454 429 455 430 int btrfs_removexattr(struct dentry *dentry, const char *name) 456 431 { 457 432 struct btrfs_root *root = BTRFS_I(d_inode(dentry))->root; 458 - int ret; 459 433 460 - /* 461 - * The permission on security.* and system.* is not checked 462 - * in permission(). 463 - */ 464 434 if (btrfs_root_readonly(root)) 465 435 return -EROFS; 466 - 467 - /* 468 - * If this is a request for a synthetic attribute in the system.* 469 - * namespace use the generic infrastructure to resolve a handler 470 - * for it via sb->s_xattr. 471 - */ 472 - if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 473 - return generic_removexattr(dentry, name); 474 - 475 - ret = btrfs_is_valid_xattr(name); 476 - if (ret) 477 - return ret; 478 - 479 - if (!strncmp(name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN)) 480 - return btrfs_set_prop(d_inode(dentry), name, 481 - NULL, 0, XATTR_REPLACE); 482 - 483 - return __btrfs_setxattr(NULL, d_inode(dentry), name, NULL, 0, 484 - XATTR_REPLACE); 436 + return generic_removexattr(dentry, name); 485 437 } 486 438 487 439 static int btrfs_initxattrs(struct inode *inode,
-2
fs/btrfs/xattr.h
··· 28 28 extern int __btrfs_setxattr(struct btrfs_trans_handle *trans, 29 29 struct inode *inode, const char *name, 30 30 const void *value, size_t size, int flags); 31 - extern ssize_t btrfs_getxattr(struct dentry *dentry, const char *name, 32 - void *buffer, size_t size); 33 31 extern int btrfs_setxattr(struct dentry *dentry, const char *name, 34 32 const void *value, size_t size, int flags); 35 33 extern int btrfs_removexattr(struct dentry *dentry, const char *name);