acl.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/ceph/acl.c
  4. *
  5. * Copyright (C) 2013 Guangliang Zhao, <lucienchao@gmail.com>
  6. */
  7. #include <linux/ceph/ceph_debug.h>
  8. #include <linux/fs.h>
  9. #include <linux/string.h>
  10. #include <linux/xattr.h>
  11. #include <linux/posix_acl_xattr.h>
  12. #include <linux/posix_acl.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include "super.h"
  16. static inline void ceph_set_cached_acl(struct inode *inode,
  17. int type, struct posix_acl *acl)
  18. {
  19. struct ceph_inode_info *ci = ceph_inode(inode);
  20. spin_lock(&ci->i_ceph_lock);
  21. if (__ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 0))
  22. set_cached_acl(inode, type, acl);
  23. else
  24. forget_cached_acl(inode, type);
  25. spin_unlock(&ci->i_ceph_lock);
  26. }
  27. struct posix_acl *ceph_get_acl(struct inode *inode, int type)
  28. {
  29. int size;
  30. unsigned int retry_cnt = 0;
  31. const char *name;
  32. char *value = NULL;
  33. struct posix_acl *acl;
  34. switch (type) {
  35. case ACL_TYPE_ACCESS:
  36. name = XATTR_NAME_POSIX_ACL_ACCESS;
  37. break;
  38. case ACL_TYPE_DEFAULT:
  39. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  40. break;
  41. default:
  42. BUG();
  43. }
  44. retry:
  45. size = __ceph_getxattr(inode, name, "", 0);
  46. if (size > 0) {
  47. value = kzalloc(size, GFP_NOFS);
  48. if (!value)
  49. return ERR_PTR(-ENOMEM);
  50. size = __ceph_getxattr(inode, name, value, size);
  51. }
  52. if (size == -ERANGE && retry_cnt < 10) {
  53. retry_cnt++;
  54. kfree(value);
  55. value = NULL;
  56. goto retry;
  57. }
  58. if (size > 0) {
  59. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  60. } else if (size == -ENODATA || size == 0) {
  61. acl = NULL;
  62. } else {
  63. pr_err_ratelimited("get acl %llx.%llx failed, err=%d\n",
  64. ceph_vinop(inode), size);
  65. acl = ERR_PTR(-EIO);
  66. }
  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. struct timespec64 old_ctime = inode->i_ctime;
  79. umode_t new_mode = inode->i_mode, old_mode = inode->i_mode;
  80. if (ceph_snap(inode) != CEPH_NOSNAP) {
  81. ret = -EROFS;
  82. goto out;
  83. }
  84. switch (type) {
  85. case ACL_TYPE_ACCESS:
  86. name = XATTR_NAME_POSIX_ACL_ACCESS;
  87. if (acl) {
  88. ret = posix_acl_update_mode(inode, &new_mode, &acl);
  89. if (ret)
  90. goto out;
  91. }
  92. break;
  93. case ACL_TYPE_DEFAULT:
  94. if (!S_ISDIR(inode->i_mode)) {
  95. ret = acl ? -EINVAL : 0;
  96. goto out;
  97. }
  98. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  99. break;
  100. default:
  101. ret = -EINVAL;
  102. goto out;
  103. }
  104. if (acl) {
  105. size = posix_acl_xattr_size(acl->a_count);
  106. value = kmalloc(size, GFP_NOFS);
  107. if (!value) {
  108. ret = -ENOMEM;
  109. goto out;
  110. }
  111. ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  112. if (ret < 0)
  113. goto out_free;
  114. }
  115. if (new_mode != old_mode) {
  116. newattrs.ia_ctime = current_time(inode);
  117. newattrs.ia_mode = new_mode;
  118. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  119. ret = __ceph_setattr(inode, &newattrs);
  120. if (ret)
  121. goto out_free;
  122. }
  123. ret = __ceph_setxattr(inode, name, value, size, 0);
  124. if (ret) {
  125. if (new_mode != old_mode) {
  126. newattrs.ia_ctime = old_ctime;
  127. newattrs.ia_mode = old_mode;
  128. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  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_acl_sec_ctx *as_ctx)
  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. err = posix_acl_equiv_mode(acl, mode);
  152. if (err < 0)
  153. goto out_err;
  154. if (err == 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 = ceph_pagelist_alloc(GFP_KERNEL);
  170. if (!pagelist)
  171. goto out_err;
  172. err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
  173. if (err)
  174. goto out_err;
  175. ceph_pagelist_encode_32(pagelist, acl && default_acl ? 2 : 1);
  176. if (acl) {
  177. size_t len = strlen(XATTR_NAME_POSIX_ACL_ACCESS);
  178. err = ceph_pagelist_reserve(pagelist, len + val_size1 + 8);
  179. if (err)
  180. goto out_err;
  181. ceph_pagelist_encode_string(pagelist, XATTR_NAME_POSIX_ACL_ACCESS,
  182. len);
  183. err = posix_acl_to_xattr(&init_user_ns, acl,
  184. tmp_buf, val_size1);
  185. if (err < 0)
  186. goto out_err;
  187. ceph_pagelist_encode_32(pagelist, val_size1);
  188. ceph_pagelist_append(pagelist, tmp_buf, val_size1);
  189. }
  190. if (default_acl) {
  191. size_t len = strlen(XATTR_NAME_POSIX_ACL_DEFAULT);
  192. err = ceph_pagelist_reserve(pagelist, len + val_size2 + 8);
  193. if (err)
  194. goto out_err;
  195. ceph_pagelist_encode_string(pagelist,
  196. XATTR_NAME_POSIX_ACL_DEFAULT, len);
  197. err = posix_acl_to_xattr(&init_user_ns, default_acl,
  198. tmp_buf, val_size2);
  199. if (err < 0)
  200. goto out_err;
  201. ceph_pagelist_encode_32(pagelist, val_size2);
  202. ceph_pagelist_append(pagelist, tmp_buf, val_size2);
  203. }
  204. kfree(tmp_buf);
  205. as_ctx->acl = acl;
  206. as_ctx->default_acl = default_acl;
  207. as_ctx->pagelist = pagelist;
  208. return 0;
  209. out_err:
  210. posix_acl_release(acl);
  211. posix_acl_release(default_acl);
  212. kfree(tmp_buf);
  213. if (pagelist)
  214. ceph_pagelist_release(pagelist);
  215. return err;
  216. }
  217. void ceph_init_inode_acls(struct inode *inode, struct ceph_acl_sec_ctx *as_ctx)
  218. {
  219. if (!inode)
  220. return;
  221. ceph_set_cached_acl(inode, ACL_TYPE_ACCESS, as_ctx->acl);
  222. ceph_set_cached_acl(inode, ACL_TYPE_DEFAULT, as_ctx->default_acl);
  223. }