acl.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * (C) 2001 Clemson University and The University of Chicago
  3. *
  4. * See COPYING in top-level directory.
  5. */
  6. #include "protocol.h"
  7. #include "orangefs-kernel.h"
  8. #include "orangefs-bufmap.h"
  9. #include <linux/posix_acl_xattr.h>
  10. #include <linux/fs_struct.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 == NULL)
  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. int orangefs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  60. {
  61. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  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. if (acl) {
  70. umode_t mode;
  71. error = posix_acl_update_mode(inode, &mode, &acl);
  72. if (error) {
  73. gossip_err("%s: posix_acl_update_mode err: %d\n",
  74. __func__,
  75. error);
  76. return error;
  77. }
  78. if (inode->i_mode != mode)
  79. SetModeFlag(orangefs_inode);
  80. inode->i_mode = mode;
  81. mark_inode_dirty_sync(inode);
  82. }
  83. break;
  84. case ACL_TYPE_DEFAULT:
  85. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  86. break;
  87. default:
  88. gossip_err("%s: invalid type %d!\n", __func__, type);
  89. return -EINVAL;
  90. }
  91. gossip_debug(GOSSIP_ACL_DEBUG,
  92. "%s: inode %pU, key %s type %d\n",
  93. __func__, get_khandle_from_ino(inode),
  94. name,
  95. type);
  96. if (acl) {
  97. size = posix_acl_xattr_size(acl->a_count);
  98. value = kmalloc(size, GFP_KERNEL);
  99. if (!value)
  100. return -ENOMEM;
  101. error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  102. if (error < 0)
  103. goto out;
  104. }
  105. gossip_debug(GOSSIP_ACL_DEBUG,
  106. "%s: name %s, value %p, size %zd, acl %p\n",
  107. __func__, name, value, size, acl);
  108. /*
  109. * Go ahead and set the extended attribute now. NOTE: Suppose acl
  110. * was NULL, then value will be NULL and size will be 0 and that
  111. * will xlate to a removexattr. However, we don't want removexattr
  112. * complain if attributes does not exist.
  113. */
  114. error = orangefs_inode_setxattr(inode, name, value, size, 0);
  115. out:
  116. kfree(value);
  117. if (!error)
  118. set_cached_acl(inode, type, acl);
  119. return error;
  120. }
  121. int orangefs_init_acl(struct inode *inode, struct inode *dir)
  122. {
  123. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  124. struct posix_acl *default_acl, *acl;
  125. umode_t mode = inode->i_mode;
  126. int error = 0;
  127. ClearModeFlag(orangefs_inode);
  128. error = posix_acl_create(dir, &mode, &default_acl, &acl);
  129. if (error)
  130. return error;
  131. if (default_acl) {
  132. error = orangefs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
  133. posix_acl_release(default_acl);
  134. }
  135. if (acl) {
  136. if (!error)
  137. error = orangefs_set_acl(inode, acl, ACL_TYPE_ACCESS);
  138. posix_acl_release(acl);
  139. }
  140. /* If mode of the inode was changed, then do a forcible ->setattr */
  141. if (mode != inode->i_mode) {
  142. SetModeFlag(orangefs_inode);
  143. inode->i_mode = mode;
  144. orangefs_flush_inode(inode);
  145. }
  146. return error;
  147. }