acl.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. int __orangefs_set_acl(struct inode *inode, struct posix_acl *acl, 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 mnt_idmap *idmap, struct dentry *dentry,
  109. struct posix_acl *acl, int type)
  110. {
  111. int error;
  112. struct iattr iattr;
  113. int rc;
  114. struct inode *inode = d_inode(dentry);
  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(&nop_mnt_idmap, 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_mode(dentry, &iattr);
  138. return rc;
  139. }