acl.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * (C) 2001 Clemson University and The University of Chicago
  4. *
  5. * See COPYING in top-level directory.
  6. */
  7. #include "protocol.h"
  8. #include "orangefs-kernel.h"
  9. #include "orangefs-bufmap.h"
  10. #include <linux/posix_acl_xattr.h>
  11. struct posix_acl *orangefs_get_acl(struct inode *inode, int type, bool rcu)
  12. {
  13. struct posix_acl *acl;
  14. int ret;
  15. char *key = NULL, *value = NULL;
  16. if (rcu)
  17. return ERR_PTR(-ECHILD);
  18. switch (type) {
  19. case ACL_TYPE_ACCESS:
  20. key = XATTR_NAME_POSIX_ACL_ACCESS;
  21. break;
  22. case ACL_TYPE_DEFAULT:
  23. key = XATTR_NAME_POSIX_ACL_DEFAULT;
  24. break;
  25. default:
  26. gossip_err("orangefs_get_acl: bogus value of type %d\n", type);
  27. return ERR_PTR(-EINVAL);
  28. }
  29. /*
  30. * Rather than incurring a network call just to determine the exact
  31. * length of the attribute, I just allocate a max length to save on
  32. * the network call. Conceivably, we could pass NULL to
  33. * orangefs_inode_getxattr() to probe the length of the value, but
  34. * I don't do that for now.
  35. */
  36. value = kmalloc(ORANGEFS_MAX_XATTR_VALUELEN, GFP_KERNEL);
  37. if (!value)
  38. return ERR_PTR(-ENOMEM);
  39. gossip_debug(GOSSIP_ACL_DEBUG,
  40. "inode %pU, key %s, type %d\n",
  41. get_khandle_from_ino(inode),
  42. key,
  43. type);
  44. ret = orangefs_inode_getxattr(inode, key, value,
  45. ORANGEFS_MAX_XATTR_VALUELEN);
  46. /* if the key exists, convert it to an in-memory rep */
  47. if (ret > 0) {
  48. acl = posix_acl_from_xattr(&init_user_ns, value, ret);
  49. } else if (ret == -ENODATA || ret == -ENOSYS) {
  50. acl = NULL;
  51. } else {
  52. gossip_err("inode %pU retrieving acl's failed with error %d\n",
  53. get_khandle_from_ino(inode),
  54. ret);
  55. acl = ERR_PTR(ret);
  56. }
  57. /* kfree(NULL) is safe, so don't worry if value ever got used */
  58. kfree(value);
  59. return acl;
  60. }
  61. static int __orangefs_set_acl(struct inode *inode, struct posix_acl *acl,
  62. int type)
  63. {
  64. int error = 0;
  65. void *value = NULL;
  66. size_t size = 0;
  67. const char *name = NULL;
  68. switch (type) {
  69. case ACL_TYPE_ACCESS:
  70. name = XATTR_NAME_POSIX_ACL_ACCESS;
  71. break;
  72. case ACL_TYPE_DEFAULT:
  73. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  74. break;
  75. default:
  76. gossip_err("%s: invalid type %d!\n", __func__, type);
  77. return -EINVAL;
  78. }
  79. gossip_debug(GOSSIP_ACL_DEBUG,
  80. "%s: inode %pU, key %s type %d\n",
  81. __func__, get_khandle_from_ino(inode),
  82. name,
  83. type);
  84. if (acl) {
  85. size = posix_acl_xattr_size(acl->a_count);
  86. value = kmalloc(size, GFP_KERNEL);
  87. if (!value)
  88. return -ENOMEM;
  89. error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  90. if (error < 0)
  91. goto out;
  92. }
  93. gossip_debug(GOSSIP_ACL_DEBUG,
  94. "%s: name %s, value %p, size %zd, acl %p\n",
  95. __func__, name, value, size, acl);
  96. /*
  97. * Go ahead and set the extended attribute now. NOTE: Suppose acl
  98. * was NULL, then value will be NULL and size will be 0 and that
  99. * will xlate to a removexattr. However, we don't want removexattr
  100. * complain if attributes does not exist.
  101. */
  102. error = orangefs_inode_setxattr(inode, name, value, size, 0);
  103. out:
  104. kfree(value);
  105. if (!error)
  106. set_cached_acl(inode, type, acl);
  107. return error;
  108. }
  109. int orangefs_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
  110. struct posix_acl *acl, int type)
  111. {
  112. int error;
  113. struct iattr iattr;
  114. int rc;
  115. memset(&iattr, 0, sizeof iattr);
  116. if (type == ACL_TYPE_ACCESS && acl) {
  117. /*
  118. * posix_acl_update_mode checks to see if the permissions
  119. * described by the ACL can be encoded into the
  120. * object's mode. If so, it sets "acl" to NULL
  121. * and "mode" to the new desired value. It is up to
  122. * us to propagate the new mode back to the server...
  123. */
  124. error = posix_acl_update_mode(&init_user_ns, inode,
  125. &iattr.ia_mode, &acl);
  126. if (error) {
  127. gossip_err("%s: posix_acl_update_mode err: %d\n",
  128. __func__,
  129. error);
  130. return error;
  131. }
  132. if (inode->i_mode != iattr.ia_mode)
  133. iattr.ia_valid = ATTR_MODE;
  134. }
  135. rc = __orangefs_set_acl(inode, acl, type);
  136. if (!rc && (iattr.ia_valid == ATTR_MODE))
  137. rc = __orangefs_setattr(inode, &iattr);
  138. return rc;
  139. }
  140. int orangefs_init_acl(struct inode *inode, struct inode *dir)
  141. {
  142. struct posix_acl *default_acl, *acl;
  143. umode_t mode = inode->i_mode;
  144. struct iattr iattr;
  145. int error = 0;
  146. error = posix_acl_create(dir, &mode, &default_acl, &acl);
  147. if (error)
  148. return error;
  149. if (default_acl) {
  150. error = __orangefs_set_acl(inode, default_acl,
  151. ACL_TYPE_DEFAULT);
  152. posix_acl_release(default_acl);
  153. } else {
  154. inode->i_default_acl = NULL;
  155. }
  156. if (acl) {
  157. if (!error)
  158. error = __orangefs_set_acl(inode, acl, ACL_TYPE_ACCESS);
  159. posix_acl_release(acl);
  160. } else {
  161. inode->i_acl = NULL;
  162. }
  163. /* If mode of the inode was changed, then do a forcible ->setattr */
  164. if (mode != inode->i_mode) {
  165. memset(&iattr, 0, sizeof iattr);
  166. inode->i_mode = mode;
  167. iattr.ia_mode = mode;
  168. iattr.ia_valid |= ATTR_MODE;
  169. __orangefs_setattr(inode, &iattr);
  170. }
  171. return error;
  172. }