acl.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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, bool rcu)
  17. {
  18. int size;
  19. const char *name;
  20. char *value = NULL;
  21. struct posix_acl *acl;
  22. if (rcu)
  23. return ERR_PTR(-ECHILD);
  24. switch (type) {
  25. case ACL_TYPE_ACCESS:
  26. name = XATTR_NAME_POSIX_ACL_ACCESS;
  27. break;
  28. case ACL_TYPE_DEFAULT:
  29. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  30. break;
  31. default:
  32. return ERR_PTR(-EINVAL);
  33. }
  34. size = btrfs_getxattr(inode, name, NULL, 0);
  35. if (size > 0) {
  36. value = kzalloc(size, GFP_KERNEL);
  37. if (!value)
  38. return ERR_PTR(-ENOMEM);
  39. size = btrfs_getxattr(inode, name, value, size);
  40. }
  41. if (size > 0)
  42. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  43. else if (size == -ENODATA || size == 0)
  44. acl = NULL;
  45. else
  46. acl = ERR_PTR(size);
  47. kfree(value);
  48. return acl;
  49. }
  50. int __btrfs_set_acl(struct btrfs_trans_handle *trans, struct inode *inode,
  51. struct posix_acl *acl, int type)
  52. {
  53. int ret, size = 0;
  54. const char *name;
  55. char *value = NULL;
  56. switch (type) {
  57. case ACL_TYPE_ACCESS:
  58. name = XATTR_NAME_POSIX_ACL_ACCESS;
  59. break;
  60. case ACL_TYPE_DEFAULT:
  61. if (!S_ISDIR(inode->i_mode))
  62. return acl ? -EINVAL : 0;
  63. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  64. break;
  65. default:
  66. return -EINVAL;
  67. }
  68. if (acl) {
  69. unsigned int nofs_flag;
  70. size = posix_acl_xattr_size(acl->a_count);
  71. /*
  72. * We're holding a transaction handle, so use a NOFS memory
  73. * allocation context to avoid deadlock if reclaim happens.
  74. */
  75. nofs_flag = memalloc_nofs_save();
  76. value = kmalloc(size, GFP_KERNEL);
  77. memalloc_nofs_restore(nofs_flag);
  78. if (!value) {
  79. ret = -ENOMEM;
  80. goto out;
  81. }
  82. ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  83. if (ret < 0)
  84. goto out;
  85. }
  86. if (trans)
  87. ret = btrfs_setxattr(trans, inode, name, value, size, 0);
  88. else
  89. ret = btrfs_setxattr_trans(inode, name, value, size, 0);
  90. out:
  91. kfree(value);
  92. if (!ret)
  93. set_cached_acl(inode, type, acl);
  94. return ret;
  95. }
  96. int btrfs_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
  97. struct posix_acl *acl, int type)
  98. {
  99. int ret;
  100. umode_t old_mode = inode->i_mode;
  101. if (type == ACL_TYPE_ACCESS && acl) {
  102. ret = posix_acl_update_mode(mnt_userns, inode,
  103. &inode->i_mode, &acl);
  104. if (ret)
  105. return ret;
  106. }
  107. ret = __btrfs_set_acl(NULL, inode, acl, type);
  108. if (ret)
  109. inode->i_mode = old_mode;
  110. return ret;
  111. }