acl.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/sched/mm.h>
  12. #include <linux/slab.h>
  13. #include "ctree.h"
  14. #include "btrfs_inode.h"
  15. #include "xattr.h"
  16. struct posix_acl *btrfs_get_acl(struct inode *inode, int type)
  17. {
  18. int size;
  19. const char *name;
  20. char *value = NULL;
  21. struct posix_acl *acl;
  22. switch (type) {
  23. case ACL_TYPE_ACCESS:
  24. name = XATTR_NAME_POSIX_ACL_ACCESS;
  25. break;
  26. case ACL_TYPE_DEFAULT:
  27. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  28. break;
  29. default:
  30. return ERR_PTR(-EINVAL);
  31. }
  32. size = btrfs_getxattr(inode, name, NULL, 0);
  33. if (size > 0) {
  34. value = kzalloc(size, GFP_KERNEL);
  35. if (!value)
  36. return ERR_PTR(-ENOMEM);
  37. size = btrfs_getxattr(inode, name, value, size);
  38. }
  39. if (size > 0)
  40. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  41. else if (size == -ENODATA || size == 0)
  42. acl = NULL;
  43. else
  44. acl = ERR_PTR(size);
  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. unsigned int nofs_flag;
  68. size = posix_acl_xattr_size(acl->a_count);
  69. /*
  70. * We're holding a transaction handle, so use a NOFS memory
  71. * allocation context to avoid deadlock if reclaim happens.
  72. */
  73. nofs_flag = memalloc_nofs_save();
  74. value = kmalloc(size, GFP_KERNEL);
  75. memalloc_nofs_restore(nofs_flag);
  76. if (!value) {
  77. ret = -ENOMEM;
  78. goto out;
  79. }
  80. ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  81. if (ret < 0)
  82. goto out;
  83. }
  84. ret = btrfs_setxattr(trans, inode, name, value, size, 0);
  85. out:
  86. kfree(value);
  87. if (!ret)
  88. set_cached_acl(inode, type, acl);
  89. return ret;
  90. }
  91. int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  92. {
  93. int ret;
  94. umode_t old_mode = inode->i_mode;
  95. if (type == ACL_TYPE_ACCESS && acl) {
  96. ret = posix_acl_update_mode(inode, &inode->i_mode, &acl);
  97. if (ret)
  98. return ret;
  99. }
  100. ret = __btrfs_set_acl(NULL, inode, acl, type);
  101. if (ret)
  102. inode->i_mode = old_mode;
  103. return ret;
  104. }
  105. int btrfs_init_acl(struct btrfs_trans_handle *trans,
  106. struct inode *inode, struct inode *dir)
  107. {
  108. struct posix_acl *default_acl, *acl;
  109. int ret = 0;
  110. /* this happens with subvols */
  111. if (!dir)
  112. return 0;
  113. ret = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  114. if (ret)
  115. return ret;
  116. if (default_acl) {
  117. ret = __btrfs_set_acl(trans, inode, default_acl,
  118. ACL_TYPE_DEFAULT);
  119. posix_acl_release(default_acl);
  120. }
  121. if (acl) {
  122. if (!ret)
  123. ret = __btrfs_set_acl(trans, inode, acl,
  124. ACL_TYPE_ACCESS);
  125. posix_acl_release(acl);
  126. }
  127. if (!default_acl && !acl)
  128. cache_no_acl(inode);
  129. return ret;
  130. }