acl.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. 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. if (type == ACL_TYPE_ACCESS && acl) {
  111. umode_t mode;
  112. error = posix_acl_update_mode(inode, &mode, &acl);
  113. if (error) {
  114. gossip_err("%s: posix_acl_update_mode err: %d\n",
  115. __func__,
  116. error);
  117. return error;
  118. }
  119. if (inode->i_mode != mode)
  120. SetModeFlag(ORANGEFS_I(inode));
  121. inode->i_mode = mode;
  122. mark_inode_dirty_sync(inode);
  123. }
  124. return __orangefs_set_acl(inode, acl, type);
  125. }
  126. int orangefs_init_acl(struct inode *inode, struct inode *dir)
  127. {
  128. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  129. struct posix_acl *default_acl, *acl;
  130. umode_t mode = inode->i_mode;
  131. int error = 0;
  132. ClearModeFlag(orangefs_inode);
  133. error = posix_acl_create(dir, &mode, &default_acl, &acl);
  134. if (error)
  135. return error;
  136. if (default_acl) {
  137. error = __orangefs_set_acl(inode, default_acl,
  138. 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. }