acl.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. return ERR_PTR(-EINVAL);
  30. }
  31. size = btrfs_getxattr(inode, name, NULL, 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 == -ENODATA || size == 0)
  41. acl = NULL;
  42. else
  43. acl = ERR_PTR(size);
  44. kfree(value);
  45. return acl;
  46. }
  47. static int __btrfs_set_acl(struct btrfs_trans_handle *trans,
  48. struct inode *inode, struct posix_acl *acl, int type)
  49. {
  50. int ret, size = 0;
  51. const char *name;
  52. char *value = NULL;
  53. switch (type) {
  54. case ACL_TYPE_ACCESS:
  55. name = XATTR_NAME_POSIX_ACL_ACCESS;
  56. break;
  57. case ACL_TYPE_DEFAULT:
  58. if (!S_ISDIR(inode->i_mode))
  59. return acl ? -EINVAL : 0;
  60. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  61. break;
  62. default:
  63. return -EINVAL;
  64. }
  65. if (acl) {
  66. size = posix_acl_xattr_size(acl->a_count);
  67. value = kmalloc(size, GFP_KERNEL);
  68. if (!value) {
  69. ret = -ENOMEM;
  70. goto out;
  71. }
  72. ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  73. if (ret < 0)
  74. goto out;
  75. }
  76. ret = btrfs_setxattr(trans, inode, name, value, size, 0);
  77. out:
  78. kfree(value);
  79. if (!ret)
  80. set_cached_acl(inode, type, acl);
  81. return ret;
  82. }
  83. int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  84. {
  85. int ret;
  86. umode_t old_mode = inode->i_mode;
  87. if (type == ACL_TYPE_ACCESS && acl) {
  88. ret = posix_acl_update_mode(inode, &inode->i_mode, &acl);
  89. if (ret)
  90. return ret;
  91. }
  92. ret = __btrfs_set_acl(NULL, inode, acl, type);
  93. if (ret)
  94. inode->i_mode = old_mode;
  95. return ret;
  96. }
  97. int btrfs_init_acl(struct btrfs_trans_handle *trans,
  98. struct inode *inode, struct inode *dir)
  99. {
  100. struct posix_acl *default_acl, *acl;
  101. int ret = 0;
  102. /* this happens with subvols */
  103. if (!dir)
  104. return 0;
  105. ret = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  106. if (ret)
  107. return ret;
  108. if (default_acl) {
  109. ret = __btrfs_set_acl(trans, inode, default_acl,
  110. ACL_TYPE_DEFAULT);
  111. posix_acl_release(default_acl);
  112. }
  113. if (acl) {
  114. if (!ret)
  115. ret = __btrfs_set_acl(trans, inode, acl,
  116. ACL_TYPE_ACCESS);
  117. posix_acl_release(acl);
  118. }
  119. if (!default_acl && !acl)
  120. cache_no_acl(inode);
  121. return ret;
  122. }