acl.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * linux/fs/ceph/acl.c
  3. *
  4. * Copyright (C) 2013 Guangliang Zhao, <lucienchao@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public
  8. * License v2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public
  16. * License along with this program; if not, write to the
  17. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. * Boston, MA 021110-1307, USA.
  19. */
  20. #include <linux/ceph/ceph_debug.h>
  21. #include <linux/fs.h>
  22. #include <linux/string.h>
  23. #include <linux/xattr.h>
  24. #include <linux/posix_acl_xattr.h>
  25. #include <linux/posix_acl.h>
  26. #include <linux/sched.h>
  27. #include <linux/slab.h>
  28. #include "super.h"
  29. static inline void ceph_set_cached_acl(struct inode *inode,
  30. int type, struct posix_acl *acl)
  31. {
  32. struct ceph_inode_info *ci = ceph_inode(inode);
  33. spin_lock(&ci->i_ceph_lock);
  34. if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0))
  35. set_cached_acl(inode, type, acl);
  36. spin_unlock(&ci->i_ceph_lock);
  37. }
  38. static inline struct posix_acl *ceph_get_cached_acl(struct inode *inode,
  39. int type)
  40. {
  41. struct ceph_inode_info *ci = ceph_inode(inode);
  42. struct posix_acl *acl = ACL_NOT_CACHED;
  43. spin_lock(&ci->i_ceph_lock);
  44. if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0))
  45. acl = get_cached_acl(inode, type);
  46. spin_unlock(&ci->i_ceph_lock);
  47. return acl;
  48. }
  49. void ceph_forget_all_cached_acls(struct inode *inode)
  50. {
  51. forget_all_cached_acls(inode);
  52. }
  53. struct posix_acl *ceph_get_acl(struct inode *inode, int type)
  54. {
  55. int size;
  56. const char *name;
  57. char *value = NULL;
  58. struct posix_acl *acl;
  59. switch (type) {
  60. case ACL_TYPE_ACCESS:
  61. name = POSIX_ACL_XATTR_ACCESS;
  62. break;
  63. case ACL_TYPE_DEFAULT:
  64. name = POSIX_ACL_XATTR_DEFAULT;
  65. break;
  66. default:
  67. BUG();
  68. }
  69. size = __ceph_getxattr(inode, name, "", 0);
  70. if (size > 0) {
  71. value = kzalloc(size, GFP_NOFS);
  72. if (!value)
  73. return ERR_PTR(-ENOMEM);
  74. size = __ceph_getxattr(inode, name, value, size);
  75. }
  76. if (size > 0)
  77. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  78. else if (size == -ERANGE || size == -ENODATA || size == 0)
  79. acl = NULL;
  80. else
  81. acl = ERR_PTR(-EIO);
  82. kfree(value);
  83. if (!IS_ERR(acl))
  84. ceph_set_cached_acl(inode, type, acl);
  85. return acl;
  86. }
  87. int ceph_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  88. {
  89. int ret = 0, size = 0;
  90. const char *name = NULL;
  91. char *value = NULL;
  92. struct iattr newattrs;
  93. umode_t new_mode = inode->i_mode, old_mode = inode->i_mode;
  94. struct dentry *dentry;
  95. if (acl) {
  96. ret = posix_acl_valid(acl);
  97. if (ret < 0)
  98. goto out;
  99. }
  100. switch (type) {
  101. case ACL_TYPE_ACCESS:
  102. name = POSIX_ACL_XATTR_ACCESS;
  103. if (acl) {
  104. ret = posix_acl_equiv_mode(acl, &new_mode);
  105. if (ret < 0)
  106. goto out;
  107. if (ret == 0)
  108. acl = NULL;
  109. }
  110. break;
  111. case ACL_TYPE_DEFAULT:
  112. if (!S_ISDIR(inode->i_mode)) {
  113. ret = acl ? -EINVAL : 0;
  114. goto out;
  115. }
  116. name = POSIX_ACL_XATTR_DEFAULT;
  117. break;
  118. default:
  119. ret = -EINVAL;
  120. goto out;
  121. }
  122. if (acl) {
  123. size = posix_acl_xattr_size(acl->a_count);
  124. value = kmalloc(size, GFP_NOFS);
  125. if (!value) {
  126. ret = -ENOMEM;
  127. goto out;
  128. }
  129. ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  130. if (ret < 0)
  131. goto out_free;
  132. }
  133. dentry = d_find_alias(inode);
  134. if (new_mode != old_mode) {
  135. newattrs.ia_mode = new_mode;
  136. newattrs.ia_valid = ATTR_MODE;
  137. ret = ceph_setattr(dentry, &newattrs);
  138. if (ret)
  139. goto out_dput;
  140. }
  141. if (value)
  142. ret = __ceph_setxattr(dentry, name, value, size, 0);
  143. else
  144. ret = __ceph_removexattr(dentry, name);
  145. if (ret) {
  146. if (new_mode != old_mode) {
  147. newattrs.ia_mode = old_mode;
  148. newattrs.ia_valid = ATTR_MODE;
  149. ceph_setattr(dentry, &newattrs);
  150. }
  151. goto out_dput;
  152. }
  153. ceph_set_cached_acl(inode, type, acl);
  154. out_dput:
  155. dput(dentry);
  156. out_free:
  157. kfree(value);
  158. out:
  159. return ret;
  160. }
  161. int ceph_init_acl(struct dentry *dentry, struct inode *inode, struct inode *dir)
  162. {
  163. struct posix_acl *default_acl, *acl;
  164. int error;
  165. error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  166. if (error)
  167. return error;
  168. if (!default_acl && !acl)
  169. cache_no_acl(inode);
  170. if (default_acl) {
  171. error = ceph_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
  172. posix_acl_release(default_acl);
  173. }
  174. if (acl) {
  175. if (!error)
  176. error = ceph_set_acl(inode, acl, ACL_TYPE_ACCESS);
  177. posix_acl_release(acl);
  178. }
  179. return error;
  180. }