acl.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Red Hat. All rights reserved.
  4. */
  5. #include <linux/fs.h>
  6. #include <linux/string.h>
  7. #include <linux/xattr.h>
  8. #include <linux/posix_acl_xattr.h>
  9. #include <linux/posix_acl.h>
  10. #include <linux/sched.h>
  11. #include <linux/slab.h>
  12. #include "ctree.h"
  13. #include "btrfs_inode.h"
  14. #include "xattr.h"
  15. struct posix_acl *btrfs_get_acl(struct inode *inode, int type)
  16. {
  17. int size;
  18. const char *name;
  19. char *value = NULL;
  20. struct posix_acl *acl;
  21. switch (type) {
  22. case ACL_TYPE_ACCESS:
  23. name = XATTR_NAME_POSIX_ACL_ACCESS;
  24. break;
  25. case ACL_TYPE_DEFAULT:
  26. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  27. break;
  28. default:
  29. BUG();
  30. }
  31. size = btrfs_getxattr(inode, name, "", 0);
  32. if (size > 0) {
  33. value = kzalloc(size, GFP_KERNEL);
  34. if (!value)
  35. return ERR_PTR(-ENOMEM);
  36. size = btrfs_getxattr(inode, name, value, size);
  37. }
  38. if (size > 0) {
  39. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  40. } else if (size == -ERANGE || size == -ENODATA || size == 0) {
  41. acl = NULL;
  42. } else {
  43. acl = ERR_PTR(-EIO);
  44. }
  45. kfree(value);
  46. return acl;
  47. }
  48. static int __btrfs_set_acl(struct btrfs_trans_handle *trans,
  49. struct inode *inode, struct posix_acl *acl, int type)
  50. {
  51. int ret, size = 0;
  52. const char *name;
  53. char *value = NULL;
  54. switch (type) {
  55. case ACL_TYPE_ACCESS:
  56. name = XATTR_NAME_POSIX_ACL_ACCESS;
  57. break;
  58. case ACL_TYPE_DEFAULT:
  59. if (!S_ISDIR(inode->i_mode))
  60. return acl ? -EINVAL : 0;
  61. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  62. break;
  63. default:
  64. return -EINVAL;
  65. }
  66. if (acl) {
  67. size = posix_acl_xattr_size(acl->a_count);
  68. value = kmalloc(size, GFP_KERNEL);
  69. if (!value) {
  70. ret = -ENOMEM;
  71. goto out;
  72. }
  73. ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  74. if (ret < 0)
  75. goto out;
  76. }
  77. ret = btrfs_setxattr(trans, inode, name, value, size, 0);
  78. out:
  79. kfree(value);
  80. if (!ret)
  81. set_cached_acl(inode, type, acl);
  82. return ret;
  83. }
  84. int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  85. {
  86. int ret;
  87. umode_t old_mode = inode->i_mode;
  88. if (type == ACL_TYPE_ACCESS && acl) {
  89. ret = posix_acl_update_mode(inode, &inode->i_mode, &acl);
  90. if (ret)
  91. return ret;
  92. }
  93. ret = __btrfs_set_acl(NULL, inode, acl, type);
  94. if (ret)
  95. inode->i_mode = old_mode;
  96. return ret;
  97. }
  98. int btrfs_init_acl(struct btrfs_trans_handle *trans,
  99. struct inode *inode, struct inode *dir)
  100. {
  101. struct posix_acl *default_acl, *acl;
  102. int ret = 0;
  103. /* this happens with subvols */
  104. if (!dir)
  105. return 0;
  106. ret = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  107. if (ret)
  108. return ret;
  109. if (default_acl) {
  110. ret = __btrfs_set_acl(trans, inode, default_acl,
  111. ACL_TYPE_DEFAULT);
  112. posix_acl_release(default_acl);
  113. }
  114. if (acl) {
  115. if (!ret)
  116. ret = __btrfs_set_acl(trans, inode, acl,
  117. ACL_TYPE_ACCESS);
  118. posix_acl_release(acl);
  119. }
  120. if (!default_acl && !acl)
  121. cache_no_acl(inode);
  122. return ret;
  123. }