acl.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. unsigned int retry_cnt = 0;
  44. const char *name;
  45. char *value = NULL;
  46. struct posix_acl *acl;
  47. switch (type) {
  48. case ACL_TYPE_ACCESS:
  49. name = XATTR_NAME_POSIX_ACL_ACCESS;
  50. break;
  51. case ACL_TYPE_DEFAULT:
  52. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  53. break;
  54. default:
  55. BUG();
  56. }
  57. retry:
  58. size = __ceph_getxattr(inode, name, "", 0);
  59. if (size > 0) {
  60. value = kzalloc(size, GFP_NOFS);
  61. if (!value)
  62. return ERR_PTR(-ENOMEM);
  63. size = __ceph_getxattr(inode, name, value, size);
  64. }
  65. if (size == -ERANGE && retry_cnt < 10) {
  66. retry_cnt++;
  67. kfree(value);
  68. value = NULL;
  69. goto retry;
  70. }
  71. if (size > 0) {
  72. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  73. } else if (size == -ENODATA || size == 0) {
  74. acl = NULL;
  75. } else {
  76. pr_err_ratelimited("get acl %llx.%llx failed, err=%d\n",
  77. ceph_vinop(inode), size);
  78. acl = ERR_PTR(-EIO);
  79. }
  80. kfree(value);
  81. if (!IS_ERR(acl))
  82. ceph_set_cached_acl(inode, type, acl);
  83. return acl;
  84. }
  85. int ceph_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  86. {
  87. int ret = 0, size = 0;
  88. const char *name = NULL;
  89. char *value = NULL;
  90. struct iattr newattrs;
  91. struct timespec64 old_ctime = inode->i_ctime;
  92. umode_t new_mode = inode->i_mode, old_mode = inode->i_mode;
  93. switch (type) {
  94. case ACL_TYPE_ACCESS:
  95. name = XATTR_NAME_POSIX_ACL_ACCESS;
  96. if (acl) {
  97. ret = posix_acl_update_mode(inode, &new_mode, &acl);
  98. if (ret)
  99. goto out;
  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 = XATTR_NAME_POSIX_ACL_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. if (ceph_snap(inode) != CEPH_NOSNAP) {
  125. ret = -EROFS;
  126. goto out_free;
  127. }
  128. if (new_mode != old_mode) {
  129. newattrs.ia_ctime = current_time(inode);
  130. newattrs.ia_mode = new_mode;
  131. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  132. ret = __ceph_setattr(inode, &newattrs);
  133. if (ret)
  134. goto out_free;
  135. }
  136. ret = __ceph_setxattr(inode, name, value, size, 0);
  137. if (ret) {
  138. if (new_mode != old_mode) {
  139. newattrs.ia_ctime = old_ctime;
  140. newattrs.ia_mode = old_mode;
  141. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  142. __ceph_setattr(inode, &newattrs);
  143. }
  144. goto out_free;
  145. }
  146. ceph_set_cached_acl(inode, type, acl);
  147. out_free:
  148. kfree(value);
  149. out:
  150. return ret;
  151. }
  152. int ceph_pre_init_acls(struct inode *dir, umode_t *mode,
  153. struct ceph_acls_info *info)
  154. {
  155. struct posix_acl *acl, *default_acl;
  156. size_t val_size1 = 0, val_size2 = 0;
  157. struct ceph_pagelist *pagelist = NULL;
  158. void *tmp_buf = NULL;
  159. int err;
  160. err = posix_acl_create(dir, mode, &default_acl, &acl);
  161. if (err)
  162. return err;
  163. if (acl) {
  164. err = posix_acl_equiv_mode(acl, mode);
  165. if (err < 0)
  166. goto out_err;
  167. if (err == 0) {
  168. posix_acl_release(acl);
  169. acl = NULL;
  170. }
  171. }
  172. if (!default_acl && !acl)
  173. return 0;
  174. if (acl)
  175. val_size1 = posix_acl_xattr_size(acl->a_count);
  176. if (default_acl)
  177. val_size2 = posix_acl_xattr_size(default_acl->a_count);
  178. err = -ENOMEM;
  179. tmp_buf = kmalloc(max(val_size1, val_size2), GFP_KERNEL);
  180. if (!tmp_buf)
  181. goto out_err;
  182. pagelist = kmalloc(sizeof(struct ceph_pagelist), GFP_KERNEL);
  183. if (!pagelist)
  184. goto out_err;
  185. ceph_pagelist_init(pagelist);
  186. err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
  187. if (err)
  188. goto out_err;
  189. ceph_pagelist_encode_32(pagelist, acl && default_acl ? 2 : 1);
  190. if (acl) {
  191. size_t len = strlen(XATTR_NAME_POSIX_ACL_ACCESS);
  192. err = ceph_pagelist_reserve(pagelist, len + val_size1 + 8);
  193. if (err)
  194. goto out_err;
  195. ceph_pagelist_encode_string(pagelist, XATTR_NAME_POSIX_ACL_ACCESS,
  196. len);
  197. err = posix_acl_to_xattr(&init_user_ns, acl,
  198. tmp_buf, val_size1);
  199. if (err < 0)
  200. goto out_err;
  201. ceph_pagelist_encode_32(pagelist, val_size1);
  202. ceph_pagelist_append(pagelist, tmp_buf, val_size1);
  203. }
  204. if (default_acl) {
  205. size_t len = strlen(XATTR_NAME_POSIX_ACL_DEFAULT);
  206. err = ceph_pagelist_reserve(pagelist, len + val_size2 + 8);
  207. if (err)
  208. goto out_err;
  209. err = ceph_pagelist_encode_string(pagelist,
  210. XATTR_NAME_POSIX_ACL_DEFAULT, len);
  211. err = posix_acl_to_xattr(&init_user_ns, default_acl,
  212. tmp_buf, val_size2);
  213. if (err < 0)
  214. goto out_err;
  215. ceph_pagelist_encode_32(pagelist, val_size2);
  216. ceph_pagelist_append(pagelist, tmp_buf, val_size2);
  217. }
  218. kfree(tmp_buf);
  219. info->acl = acl;
  220. info->default_acl = default_acl;
  221. info->pagelist = pagelist;
  222. return 0;
  223. out_err:
  224. posix_acl_release(acl);
  225. posix_acl_release(default_acl);
  226. kfree(tmp_buf);
  227. if (pagelist)
  228. ceph_pagelist_release(pagelist);
  229. return err;
  230. }
  231. void ceph_init_inode_acls(struct inode* inode, struct ceph_acls_info *info)
  232. {
  233. if (!inode)
  234. return;
  235. ceph_set_cached_acl(inode, ACL_TYPE_ACCESS, info->acl);
  236. ceph_set_cached_acl(inode, ACL_TYPE_DEFAULT, info->default_acl);
  237. }
  238. void ceph_release_acls_info(struct ceph_acls_info *info)
  239. {
  240. posix_acl_release(info->acl);
  241. posix_acl_release(info->default_acl);
  242. if (info->pagelist)
  243. ceph_pagelist_release(info->pagelist);
  244. }