acl.c 4.7 KB

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