acl.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 = inode->i_mode;
  71. /*
  72. * can we represent this with the traditional file
  73. * mode permission bits?
  74. */
  75. error = posix_acl_equiv_mode(acl, &mode);
  76. if (error < 0) {
  77. gossip_err("%s: posix_acl_equiv_mode err: %d\n",
  78. __func__,
  79. error);
  80. return error;
  81. }
  82. if (inode->i_mode != mode)
  83. SetModeFlag(orangefs_inode);
  84. inode->i_mode = mode;
  85. mark_inode_dirty_sync(inode);
  86. if (error == 0)
  87. acl = NULL;
  88. }
  89. break;
  90. case ACL_TYPE_DEFAULT:
  91. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  92. break;
  93. default:
  94. gossip_err("%s: invalid type %d!\n", __func__, type);
  95. return -EINVAL;
  96. }
  97. gossip_debug(GOSSIP_ACL_DEBUG,
  98. "%s: inode %pU, key %s type %d\n",
  99. __func__, get_khandle_from_ino(inode),
  100. name,
  101. type);
  102. if (acl) {
  103. size = posix_acl_xattr_size(acl->a_count);
  104. value = kmalloc(size, GFP_KERNEL);
  105. if (!value)
  106. return -ENOMEM;
  107. error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  108. if (error < 0)
  109. goto out;
  110. }
  111. gossip_debug(GOSSIP_ACL_DEBUG,
  112. "%s: name %s, value %p, size %zd, acl %p\n",
  113. __func__, name, value, size, acl);
  114. /*
  115. * Go ahead and set the extended attribute now. NOTE: Suppose acl
  116. * was NULL, then value will be NULL and size will be 0 and that
  117. * will xlate to a removexattr. However, we don't want removexattr
  118. * complain if attributes does not exist.
  119. */
  120. error = orangefs_inode_setxattr(inode, name, value, size, 0);
  121. out:
  122. kfree(value);
  123. if (!error)
  124. set_cached_acl(inode, type, acl);
  125. return error;
  126. }
  127. int orangefs_init_acl(struct inode *inode, struct inode *dir)
  128. {
  129. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  130. struct posix_acl *default_acl, *acl;
  131. umode_t mode = inode->i_mode;
  132. int error = 0;
  133. ClearModeFlag(orangefs_inode);
  134. error = posix_acl_create(dir, &mode, &default_acl, &acl);
  135. if (error)
  136. return error;
  137. if (default_acl) {
  138. error = orangefs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
  139. posix_acl_release(default_acl);
  140. }
  141. if (acl) {
  142. if (!error)
  143. error = orangefs_set_acl(inode, acl, ACL_TYPE_ACCESS);
  144. posix_acl_release(acl);
  145. }
  146. /* If mode of the inode was changed, then do a forcible ->setattr */
  147. if (mode != inode->i_mode) {
  148. SetModeFlag(orangefs_inode);
  149. inode->i_mode = mode;
  150. orangefs_flush_inode(inode);
  151. }
  152. return error;
  153. }