acl.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. struct posix_acl *ceph_get_acl(struct inode *inode, int type)
  50. {
  51. int size;
  52. const char *name;
  53. char *value = NULL;
  54. struct posix_acl *acl;
  55. switch (type) {
  56. case ACL_TYPE_ACCESS:
  57. name = POSIX_ACL_XATTR_ACCESS;
  58. break;
  59. case ACL_TYPE_DEFAULT:
  60. name = POSIX_ACL_XATTR_DEFAULT;
  61. break;
  62. default:
  63. BUG();
  64. }
  65. size = __ceph_getxattr(inode, name, "", 0);
  66. if (size > 0) {
  67. value = kzalloc(size, GFP_NOFS);
  68. if (!value)
  69. return ERR_PTR(-ENOMEM);
  70. size = __ceph_getxattr(inode, name, value, size);
  71. }
  72. if (size > 0)
  73. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  74. else if (size == -ERANGE || size == -ENODATA || size == 0)
  75. acl = NULL;
  76. else
  77. acl = ERR_PTR(-EIO);
  78. kfree(value);
  79. if (!IS_ERR(acl))
  80. ceph_set_cached_acl(inode, type, acl);
  81. return acl;
  82. }
  83. int ceph_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  84. {
  85. int ret = 0, size = 0;
  86. const char *name = NULL;
  87. char *value = NULL;
  88. struct iattr newattrs;
  89. umode_t new_mode = inode->i_mode, old_mode = inode->i_mode;
  90. struct dentry *dentry;
  91. if (acl) {
  92. ret = posix_acl_valid(acl);
  93. if (ret < 0)
  94. goto out;
  95. }
  96. switch (type) {
  97. case ACL_TYPE_ACCESS:
  98. name = POSIX_ACL_XATTR_ACCESS;
  99. if (acl) {
  100. ret = posix_acl_equiv_mode(acl, &new_mode);
  101. if (ret < 0)
  102. goto out;
  103. if (ret == 0)
  104. acl = NULL;
  105. }
  106. break;
  107. case ACL_TYPE_DEFAULT:
  108. if (!S_ISDIR(inode->i_mode)) {
  109. ret = acl ? -EINVAL : 0;
  110. goto out;
  111. }
  112. name = POSIX_ACL_XATTR_DEFAULT;
  113. break;
  114. default:
  115. ret = -EINVAL;
  116. goto out;
  117. }
  118. if (acl) {
  119. size = posix_acl_xattr_size(acl->a_count);
  120. value = kmalloc(size, GFP_NOFS);
  121. if (!value) {
  122. ret = -ENOMEM;
  123. goto out;
  124. }
  125. ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  126. if (ret < 0)
  127. goto out_free;
  128. }
  129. dentry = d_find_alias(inode);
  130. if (new_mode != old_mode) {
  131. newattrs.ia_mode = new_mode;
  132. newattrs.ia_valid = ATTR_MODE;
  133. ret = ceph_setattr(dentry, &newattrs);
  134. if (ret)
  135. goto out_dput;
  136. }
  137. ret = __ceph_setxattr(dentry, name, value, size, 0);
  138. if (ret) {
  139. if (new_mode != old_mode) {
  140. newattrs.ia_mode = old_mode;
  141. newattrs.ia_valid = ATTR_MODE;
  142. ceph_setattr(dentry, &newattrs);
  143. }
  144. goto out_dput;
  145. }
  146. ceph_set_cached_acl(inode, type, acl);
  147. out_dput:
  148. dput(dentry);
  149. out_free:
  150. kfree(value);
  151. out:
  152. return ret;
  153. }
  154. int ceph_init_acl(struct dentry *dentry, struct inode *inode, struct inode *dir)
  155. {
  156. struct posix_acl *default_acl, *acl;
  157. int error;
  158. error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  159. if (error)
  160. return error;
  161. if (!default_acl && !acl)
  162. cache_no_acl(inode);
  163. if (default_acl) {
  164. error = ceph_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
  165. posix_acl_release(default_acl);
  166. }
  167. if (acl) {
  168. if (!error)
  169. error = ceph_set_acl(inode, acl, ACL_TYPE_ACCESS);
  170. posix_acl_release(acl);
  171. }
  172. return error;
  173. }