acl.c 6.1 KB

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