acl.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. switch (type) {
  92. case ACL_TYPE_ACCESS:
  93. name = POSIX_ACL_XATTR_ACCESS;
  94. if (acl) {
  95. ret = posix_acl_update_mode(inode, &new_mode, &acl);
  96. if (ret)
  97. goto out;
  98. }
  99. break;
  100. case ACL_TYPE_DEFAULT:
  101. if (!S_ISDIR(inode->i_mode)) {
  102. ret = acl ? -EINVAL : 0;
  103. goto out;
  104. }
  105. name = POSIX_ACL_XATTR_DEFAULT;
  106. break;
  107. default:
  108. ret = -EINVAL;
  109. goto out;
  110. }
  111. if (acl) {
  112. size = posix_acl_xattr_size(acl->a_count);
  113. value = kmalloc(size, GFP_NOFS);
  114. if (!value) {
  115. ret = -ENOMEM;
  116. goto out;
  117. }
  118. ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  119. if (ret < 0)
  120. goto out_free;
  121. }
  122. dentry = d_find_alias(inode);
  123. if (new_mode != old_mode) {
  124. newattrs.ia_mode = new_mode;
  125. newattrs.ia_valid = ATTR_MODE;
  126. ret = ceph_setattr(dentry, &newattrs);
  127. if (ret)
  128. goto out_dput;
  129. }
  130. ret = __ceph_setxattr(dentry, name, value, size, 0);
  131. if (ret) {
  132. if (new_mode != old_mode) {
  133. newattrs.ia_mode = old_mode;
  134. newattrs.ia_valid = ATTR_MODE;
  135. ceph_setattr(dentry, &newattrs);
  136. }
  137. goto out_dput;
  138. }
  139. ceph_set_cached_acl(inode, type, acl);
  140. out_dput:
  141. dput(dentry);
  142. out_free:
  143. kfree(value);
  144. out:
  145. return ret;
  146. }
  147. int ceph_pre_init_acls(struct inode *dir, umode_t *mode,
  148. struct ceph_acls_info *info)
  149. {
  150. struct posix_acl *acl, *default_acl;
  151. size_t val_size1 = 0, val_size2 = 0;
  152. struct ceph_pagelist *pagelist = NULL;
  153. void *tmp_buf = NULL;
  154. int err;
  155. err = posix_acl_create(dir, mode, &default_acl, &acl);
  156. if (err)
  157. return err;
  158. if (acl) {
  159. int ret = posix_acl_equiv_mode(acl, mode);
  160. if (ret < 0)
  161. goto out_err;
  162. if (ret == 0) {
  163. posix_acl_release(acl);
  164. acl = NULL;
  165. }
  166. }
  167. if (!default_acl && !acl)
  168. return 0;
  169. if (acl)
  170. val_size1 = posix_acl_xattr_size(acl->a_count);
  171. if (default_acl)
  172. val_size2 = posix_acl_xattr_size(default_acl->a_count);
  173. err = -ENOMEM;
  174. tmp_buf = kmalloc(max(val_size1, val_size2), GFP_NOFS);
  175. if (!tmp_buf)
  176. goto out_err;
  177. pagelist = kmalloc(sizeof(struct ceph_pagelist), GFP_NOFS);
  178. if (!pagelist)
  179. goto out_err;
  180. ceph_pagelist_init(pagelist);
  181. err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
  182. if (err)
  183. goto out_err;
  184. ceph_pagelist_encode_32(pagelist, acl && default_acl ? 2 : 1);
  185. if (acl) {
  186. size_t len = strlen(POSIX_ACL_XATTR_ACCESS);
  187. err = ceph_pagelist_reserve(pagelist, len + val_size1 + 8);
  188. if (err)
  189. goto out_err;
  190. ceph_pagelist_encode_string(pagelist, POSIX_ACL_XATTR_ACCESS,
  191. len);
  192. err = posix_acl_to_xattr(&init_user_ns, acl,
  193. tmp_buf, val_size1);
  194. if (err < 0)
  195. goto out_err;
  196. ceph_pagelist_encode_32(pagelist, val_size1);
  197. ceph_pagelist_append(pagelist, tmp_buf, val_size1);
  198. }
  199. if (default_acl) {
  200. size_t len = strlen(POSIX_ACL_XATTR_DEFAULT);
  201. err = ceph_pagelist_reserve(pagelist, len + val_size2 + 8);
  202. if (err)
  203. goto out_err;
  204. err = ceph_pagelist_encode_string(pagelist,
  205. POSIX_ACL_XATTR_DEFAULT, len);
  206. err = posix_acl_to_xattr(&init_user_ns, default_acl,
  207. tmp_buf, val_size2);
  208. if (err < 0)
  209. goto out_err;
  210. ceph_pagelist_encode_32(pagelist, val_size2);
  211. ceph_pagelist_append(pagelist, tmp_buf, val_size2);
  212. }
  213. kfree(tmp_buf);
  214. info->acl = acl;
  215. info->default_acl = default_acl;
  216. info->pagelist = pagelist;
  217. return 0;
  218. out_err:
  219. posix_acl_release(acl);
  220. posix_acl_release(default_acl);
  221. kfree(tmp_buf);
  222. if (pagelist)
  223. ceph_pagelist_release(pagelist);
  224. return err;
  225. }
  226. void ceph_init_inode_acls(struct inode* inode, struct ceph_acls_info *info)
  227. {
  228. if (!inode)
  229. return;
  230. ceph_set_cached_acl(inode, ACL_TYPE_ACCESS, info->acl);
  231. ceph_set_cached_acl(inode, ACL_TYPE_DEFAULT, info->default_acl);
  232. }
  233. void ceph_release_acls_info(struct ceph_acls_info *info)
  234. {
  235. posix_acl_release(info->acl);
  236. posix_acl_release(info->default_acl);
  237. if (info->pagelist)
  238. ceph_pagelist_release(info->pagelist);
  239. }