acl.c 4.6 KB

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