acl.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/ceph/acl.c
  4. *
  5. * Copyright (C) 2013 Guangliang Zhao, <[email protected]>
  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, bool rcu)
  28. {
  29. int size;
  30. unsigned int retry_cnt = 0;
  31. const char *name;
  32. char *value = NULL;
  33. struct posix_acl *acl;
  34. if (rcu)
  35. return ERR_PTR(-ECHILD);
  36. switch (type) {
  37. case ACL_TYPE_ACCESS:
  38. name = XATTR_NAME_POSIX_ACL_ACCESS;
  39. break;
  40. case ACL_TYPE_DEFAULT:
  41. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  42. break;
  43. default:
  44. BUG();
  45. }
  46. retry:
  47. size = __ceph_getxattr(inode, name, "", 0);
  48. if (size > 0) {
  49. value = kzalloc(size, GFP_NOFS);
  50. if (!value)
  51. return ERR_PTR(-ENOMEM);
  52. size = __ceph_getxattr(inode, name, value, size);
  53. }
  54. if (size == -ERANGE && retry_cnt < 10) {
  55. retry_cnt++;
  56. kfree(value);
  57. value = NULL;
  58. goto retry;
  59. }
  60. if (size > 0) {
  61. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  62. } else if (size == -ENODATA || size == 0) {
  63. acl = NULL;
  64. } else {
  65. pr_err_ratelimited("get acl %llx.%llx failed, err=%d\n",
  66. ceph_vinop(inode), size);
  67. acl = ERR_PTR(-EIO);
  68. }
  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 user_namespace *mnt_userns, struct inode *inode,
  75. struct posix_acl *acl, int type)
  76. {
  77. int ret = 0, size = 0;
  78. const char *name = NULL;
  79. char *value = NULL;
  80. struct iattr newattrs;
  81. struct timespec64 old_ctime = inode->i_ctime;
  82. umode_t new_mode = inode->i_mode, old_mode = inode->i_mode;
  83. if (ceph_snap(inode) != CEPH_NOSNAP) {
  84. ret = -EROFS;
  85. goto out;
  86. }
  87. switch (type) {
  88. case ACL_TYPE_ACCESS:
  89. name = XATTR_NAME_POSIX_ACL_ACCESS;
  90. if (acl) {
  91. ret = posix_acl_update_mode(&init_user_ns, inode,
  92. &new_mode, &acl);
  93. if (ret)
  94. goto out;
  95. }
  96. break;
  97. case ACL_TYPE_DEFAULT:
  98. if (!S_ISDIR(inode->i_mode)) {
  99. ret = acl ? -EINVAL : 0;
  100. goto out;
  101. }
  102. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  103. break;
  104. default:
  105. ret = -EINVAL;
  106. goto out;
  107. }
  108. if (acl) {
  109. size = posix_acl_xattr_size(acl->a_count);
  110. value = kmalloc(size, GFP_NOFS);
  111. if (!value) {
  112. ret = -ENOMEM;
  113. goto out;
  114. }
  115. ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  116. if (ret < 0)
  117. goto out_free;
  118. }
  119. if (new_mode != old_mode) {
  120. newattrs.ia_ctime = current_time(inode);
  121. newattrs.ia_mode = new_mode;
  122. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  123. ret = __ceph_setattr(inode, &newattrs);
  124. if (ret)
  125. goto out_free;
  126. }
  127. ret = __ceph_setxattr(inode, name, value, size, 0);
  128. if (ret) {
  129. if (new_mode != old_mode) {
  130. newattrs.ia_ctime = old_ctime;
  131. newattrs.ia_mode = old_mode;
  132. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  133. __ceph_setattr(inode, &newattrs);
  134. }
  135. goto out_free;
  136. }
  137. ceph_set_cached_acl(inode, type, acl);
  138. out_free:
  139. kfree(value);
  140. out:
  141. return ret;
  142. }
  143. int ceph_pre_init_acls(struct inode *dir, umode_t *mode,
  144. struct ceph_acl_sec_ctx *as_ctx)
  145. {
  146. struct posix_acl *acl, *default_acl;
  147. size_t val_size1 = 0, val_size2 = 0;
  148. struct ceph_pagelist *pagelist = NULL;
  149. void *tmp_buf = NULL;
  150. int err;
  151. err = posix_acl_create(dir, mode, &default_acl, &acl);
  152. if (err)
  153. return err;
  154. if (acl) {
  155. err = posix_acl_equiv_mode(acl, mode);
  156. if (err < 0)
  157. goto out_err;
  158. if (err == 0) {
  159. posix_acl_release(acl);
  160. acl = NULL;
  161. }
  162. }
  163. if (!default_acl && !acl)
  164. return 0;
  165. if (acl)
  166. val_size1 = posix_acl_xattr_size(acl->a_count);
  167. if (default_acl)
  168. val_size2 = posix_acl_xattr_size(default_acl->a_count);
  169. err = -ENOMEM;
  170. tmp_buf = kmalloc(max(val_size1, val_size2), GFP_KERNEL);
  171. if (!tmp_buf)
  172. goto out_err;
  173. pagelist = ceph_pagelist_alloc(GFP_KERNEL);
  174. if (!pagelist)
  175. goto out_err;
  176. err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
  177. if (err)
  178. goto out_err;
  179. ceph_pagelist_encode_32(pagelist, acl && default_acl ? 2 : 1);
  180. if (acl) {
  181. size_t len = strlen(XATTR_NAME_POSIX_ACL_ACCESS);
  182. err = ceph_pagelist_reserve(pagelist, len + val_size1 + 8);
  183. if (err)
  184. goto out_err;
  185. ceph_pagelist_encode_string(pagelist, XATTR_NAME_POSIX_ACL_ACCESS,
  186. len);
  187. err = posix_acl_to_xattr(&init_user_ns, acl,
  188. tmp_buf, val_size1);
  189. if (err < 0)
  190. goto out_err;
  191. ceph_pagelist_encode_32(pagelist, val_size1);
  192. ceph_pagelist_append(pagelist, tmp_buf, val_size1);
  193. }
  194. if (default_acl) {
  195. size_t len = strlen(XATTR_NAME_POSIX_ACL_DEFAULT);
  196. err = ceph_pagelist_reserve(pagelist, len + val_size2 + 8);
  197. if (err)
  198. goto out_err;
  199. ceph_pagelist_encode_string(pagelist,
  200. XATTR_NAME_POSIX_ACL_DEFAULT, len);
  201. err = posix_acl_to_xattr(&init_user_ns, default_acl,
  202. tmp_buf, val_size2);
  203. if (err < 0)
  204. goto out_err;
  205. ceph_pagelist_encode_32(pagelist, val_size2);
  206. ceph_pagelist_append(pagelist, tmp_buf, val_size2);
  207. }
  208. kfree(tmp_buf);
  209. as_ctx->acl = acl;
  210. as_ctx->default_acl = default_acl;
  211. as_ctx->pagelist = pagelist;
  212. return 0;
  213. out_err:
  214. posix_acl_release(acl);
  215. posix_acl_release(default_acl);
  216. kfree(tmp_buf);
  217. if (pagelist)
  218. ceph_pagelist_release(pagelist);
  219. return err;
  220. }
  221. void ceph_init_inode_acls(struct inode *inode, struct ceph_acl_sec_ctx *as_ctx)
  222. {
  223. if (!inode)
  224. return;
  225. ceph_set_cached_acl(inode, ACL_TYPE_ACCESS, as_ctx->acl);
  226. ceph_set_cached_acl(inode, ACL_TYPE_DEFAULT, as_ctx->default_acl);
  227. }