acl.c 6.3 KB

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