acl.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. struct posix_acl *ceph_get_acl(struct inode *inode, int type)
  39. {
  40. int size;
  41. const char *name;
  42. char *value = NULL;
  43. struct posix_acl *acl;
  44. switch (type) {
  45. case ACL_TYPE_ACCESS:
  46. name = POSIX_ACL_XATTR_ACCESS;
  47. break;
  48. case ACL_TYPE_DEFAULT:
  49. name = POSIX_ACL_XATTR_DEFAULT;
  50. break;
  51. default:
  52. BUG();
  53. }
  54. size = __ceph_getxattr(inode, name, "", 0);
  55. if (size > 0) {
  56. value = kzalloc(size, GFP_NOFS);
  57. if (!value)
  58. return ERR_PTR(-ENOMEM);
  59. size = __ceph_getxattr(inode, name, value, size);
  60. }
  61. if (size > 0)
  62. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  63. else if (size == -ERANGE || size == -ENODATA || size == 0)
  64. acl = NULL;
  65. else
  66. acl = ERR_PTR(-EIO);
  67. kfree(value);
  68. if (!IS_ERR(acl))
  69. ceph_set_cached_acl(inode, type, acl);
  70. return acl;
  71. }
  72. int ceph_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  73. {
  74. int ret = 0, size = 0;
  75. const char *name = NULL;
  76. char *value = NULL;
  77. struct iattr newattrs;
  78. umode_t new_mode = inode->i_mode, old_mode = inode->i_mode;
  79. struct dentry *dentry;
  80. switch (type) {
  81. case ACL_TYPE_ACCESS:
  82. name = POSIX_ACL_XATTR_ACCESS;
  83. if (acl) {
  84. ret = posix_acl_update_mode(inode, &new_mode, &acl);
  85. if (ret)
  86. goto out;
  87. }
  88. break;
  89. case ACL_TYPE_DEFAULT:
  90. if (!S_ISDIR(inode->i_mode)) {
  91. ret = acl ? -EINVAL : 0;
  92. goto out;
  93. }
  94. name = POSIX_ACL_XATTR_DEFAULT;
  95. break;
  96. default:
  97. ret = -EINVAL;
  98. goto out;
  99. }
  100. if (acl) {
  101. size = posix_acl_xattr_size(acl->a_count);
  102. value = kmalloc(size, GFP_NOFS);
  103. if (!value) {
  104. ret = -ENOMEM;
  105. goto out;
  106. }
  107. ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  108. if (ret < 0)
  109. goto out_free;
  110. }
  111. dentry = d_find_alias(inode);
  112. if (new_mode != old_mode) {
  113. newattrs.ia_mode = new_mode;
  114. newattrs.ia_valid = ATTR_MODE;
  115. ret = __ceph_setattr(dentry, &newattrs);
  116. if (ret)
  117. goto out_dput;
  118. }
  119. ret = __ceph_setxattr(dentry, name, value, size, 0);
  120. if (ret) {
  121. if (new_mode != old_mode) {
  122. newattrs.ia_mode = old_mode;
  123. newattrs.ia_valid = ATTR_MODE;
  124. __ceph_setattr(dentry, &newattrs);
  125. }
  126. goto out_dput;
  127. }
  128. ceph_set_cached_acl(inode, type, acl);
  129. out_dput:
  130. dput(dentry);
  131. out_free:
  132. kfree(value);
  133. out:
  134. return ret;
  135. }
  136. int ceph_pre_init_acls(struct inode *dir, umode_t *mode,
  137. struct ceph_acls_info *info)
  138. {
  139. struct posix_acl *acl, *default_acl;
  140. size_t val_size1 = 0, val_size2 = 0;
  141. struct ceph_pagelist *pagelist = NULL;
  142. void *tmp_buf = NULL;
  143. int err;
  144. err = posix_acl_create(dir, mode, &default_acl, &acl);
  145. if (err)
  146. return err;
  147. if (acl) {
  148. int ret = posix_acl_equiv_mode(acl, mode);
  149. if (ret < 0)
  150. goto out_err;
  151. if (ret == 0) {
  152. posix_acl_release(acl);
  153. acl = NULL;
  154. }
  155. }
  156. if (!default_acl && !acl)
  157. return 0;
  158. if (acl)
  159. val_size1 = posix_acl_xattr_size(acl->a_count);
  160. if (default_acl)
  161. val_size2 = posix_acl_xattr_size(default_acl->a_count);
  162. err = -ENOMEM;
  163. tmp_buf = kmalloc(max(val_size1, val_size2), GFP_KERNEL);
  164. if (!tmp_buf)
  165. goto out_err;
  166. pagelist = kmalloc(sizeof(struct ceph_pagelist), GFP_KERNEL);
  167. if (!pagelist)
  168. goto out_err;
  169. ceph_pagelist_init(pagelist);
  170. err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
  171. if (err)
  172. goto out_err;
  173. ceph_pagelist_encode_32(pagelist, acl && default_acl ? 2 : 1);
  174. if (acl) {
  175. size_t len = strlen(POSIX_ACL_XATTR_ACCESS);
  176. err = ceph_pagelist_reserve(pagelist, len + val_size1 + 8);
  177. if (err)
  178. goto out_err;
  179. ceph_pagelist_encode_string(pagelist, POSIX_ACL_XATTR_ACCESS,
  180. len);
  181. err = posix_acl_to_xattr(&init_user_ns, acl,
  182. tmp_buf, val_size1);
  183. if (err < 0)
  184. goto out_err;
  185. ceph_pagelist_encode_32(pagelist, val_size1);
  186. ceph_pagelist_append(pagelist, tmp_buf, val_size1);
  187. }
  188. if (default_acl) {
  189. size_t len = strlen(POSIX_ACL_XATTR_DEFAULT);
  190. err = ceph_pagelist_reserve(pagelist, len + val_size2 + 8);
  191. if (err)
  192. goto out_err;
  193. err = ceph_pagelist_encode_string(pagelist,
  194. POSIX_ACL_XATTR_DEFAULT, len);
  195. err = posix_acl_to_xattr(&init_user_ns, default_acl,
  196. tmp_buf, val_size2);
  197. if (err < 0)
  198. goto out_err;
  199. ceph_pagelist_encode_32(pagelist, val_size2);
  200. ceph_pagelist_append(pagelist, tmp_buf, val_size2);
  201. }
  202. kfree(tmp_buf);
  203. info->acl = acl;
  204. info->default_acl = default_acl;
  205. info->pagelist = pagelist;
  206. return 0;
  207. out_err:
  208. posix_acl_release(acl);
  209. posix_acl_release(default_acl);
  210. kfree(tmp_buf);
  211. if (pagelist)
  212. ceph_pagelist_release(pagelist);
  213. return err;
  214. }
  215. void ceph_init_inode_acls(struct inode* inode, struct ceph_acls_info *info)
  216. {
  217. if (!inode)
  218. return;
  219. ceph_set_cached_acl(inode, ACL_TYPE_ACCESS, info->acl);
  220. ceph_set_cached_acl(inode, ACL_TYPE_DEFAULT, info->default_acl);
  221. }
  222. void ceph_release_acls_info(struct ceph_acls_info *info)
  223. {
  224. posix_acl_release(info->acl);
  225. posix_acl_release(info->default_acl);
  226. if (info->pagelist)
  227. ceph_pagelist_release(info->pagelist);
  228. }