acl.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #include "mds_client.h"
  17. static inline void ceph_set_cached_acl(struct inode *inode,
  18. int type, struct posix_acl *acl)
  19. {
  20. struct ceph_inode_info *ci = ceph_inode(inode);
  21. spin_lock(&ci->i_ceph_lock);
  22. if (__ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 0))
  23. set_cached_acl(inode, type, acl);
  24. else
  25. forget_cached_acl(inode, type);
  26. spin_unlock(&ci->i_ceph_lock);
  27. }
  28. struct posix_acl *ceph_get_acl(struct inode *inode, int type, bool rcu)
  29. {
  30. struct ceph_client *cl = ceph_inode_to_client(inode);
  31. int size;
  32. unsigned int retry_cnt = 0;
  33. const char *name;
  34. char *value = NULL;
  35. struct posix_acl *acl;
  36. if (rcu)
  37. return ERR_PTR(-ECHILD);
  38. switch (type) {
  39. case ACL_TYPE_ACCESS:
  40. name = XATTR_NAME_POSIX_ACL_ACCESS;
  41. break;
  42. case ACL_TYPE_DEFAULT:
  43. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  44. break;
  45. default:
  46. BUG();
  47. }
  48. retry:
  49. size = __ceph_getxattr(inode, name, "", 0);
  50. if (size > 0) {
  51. value = kzalloc(size, GFP_NOFS);
  52. if (!value)
  53. return ERR_PTR(-ENOMEM);
  54. size = __ceph_getxattr(inode, name, value, size);
  55. }
  56. if (size == -ERANGE && retry_cnt < 10) {
  57. retry_cnt++;
  58. kfree(value);
  59. value = NULL;
  60. goto retry;
  61. }
  62. if (size > 0) {
  63. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  64. } else if (size == -ENODATA || size == 0) {
  65. acl = NULL;
  66. } else {
  67. pr_err_ratelimited_client(cl, "%llx.%llx failed, err=%d\n",
  68. ceph_vinop(inode), size);
  69. acl = ERR_PTR(-EIO);
  70. }
  71. kfree(value);
  72. if (!IS_ERR(acl))
  73. ceph_set_cached_acl(inode, type, acl);
  74. return acl;
  75. }
  76. int ceph_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
  77. struct posix_acl *acl, int type)
  78. {
  79. int ret = 0;
  80. size_t size = 0;
  81. const char *name = NULL;
  82. char *value = NULL;
  83. struct iattr newattrs;
  84. struct inode *inode = d_inode(dentry);
  85. struct timespec64 old_ctime = inode_get_ctime(inode);
  86. umode_t new_mode = inode->i_mode, old_mode = inode->i_mode;
  87. if (ceph_snap(inode) != CEPH_NOSNAP) {
  88. ret = -EROFS;
  89. goto out;
  90. }
  91. switch (type) {
  92. case ACL_TYPE_ACCESS:
  93. name = XATTR_NAME_POSIX_ACL_ACCESS;
  94. if (acl) {
  95. ret = posix_acl_update_mode(idmap, inode,
  96. &new_mode, &acl);
  97. if (ret)
  98. goto out;
  99. }
  100. break;
  101. case ACL_TYPE_DEFAULT:
  102. if (!S_ISDIR(inode->i_mode)) {
  103. ret = acl ? -EINVAL : 0;
  104. goto out;
  105. }
  106. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  107. break;
  108. default:
  109. ret = -EINVAL;
  110. goto out;
  111. }
  112. if (acl) {
  113. value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_NOFS);
  114. if (!value) {
  115. ret = -ENOMEM;
  116. goto out;
  117. }
  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(idmap, inode, &newattrs, NULL);
  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(idmap, inode, &newattrs, NULL);
  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_buf1 = NULL, *tmp_buf2 = 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. err = -ENOMEM;
  166. pagelist = ceph_pagelist_alloc(GFP_KERNEL);
  167. if (!pagelist)
  168. goto out_err;
  169. err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
  170. if (err)
  171. goto out_err;
  172. ceph_pagelist_encode_32(pagelist, acl && default_acl ? 2 : 1);
  173. if (acl) {
  174. size_t len = strlen(XATTR_NAME_POSIX_ACL_ACCESS);
  175. err = -ENOMEM;
  176. tmp_buf1 = posix_acl_to_xattr(&init_user_ns, acl,
  177. &val_size1, GFP_KERNEL);
  178. if (!tmp_buf1)
  179. goto out_err;
  180. err = ceph_pagelist_reserve(pagelist, len + val_size1 + 8);
  181. if (err)
  182. goto out_err;
  183. ceph_pagelist_encode_string(pagelist, XATTR_NAME_POSIX_ACL_ACCESS,
  184. len);
  185. ceph_pagelist_encode_32(pagelist, val_size1);
  186. ceph_pagelist_append(pagelist, tmp_buf1, val_size1);
  187. }
  188. if (default_acl) {
  189. size_t len = strlen(XATTR_NAME_POSIX_ACL_DEFAULT);
  190. err = -ENOMEM;
  191. tmp_buf2 = posix_acl_to_xattr(&init_user_ns, default_acl,
  192. &val_size2, GFP_KERNEL);
  193. if (!tmp_buf2)
  194. goto out_err;
  195. err = ceph_pagelist_reserve(pagelist, len + val_size2 + 8);
  196. if (err)
  197. goto out_err;
  198. ceph_pagelist_encode_string(pagelist,
  199. XATTR_NAME_POSIX_ACL_DEFAULT, len);
  200. ceph_pagelist_encode_32(pagelist, val_size2);
  201. ceph_pagelist_append(pagelist, tmp_buf2, val_size2);
  202. }
  203. kfree(tmp_buf1);
  204. kfree(tmp_buf2);
  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_buf1);
  213. kfree(tmp_buf2);
  214. if (pagelist)
  215. ceph_pagelist_release(pagelist);
  216. return err;
  217. }
  218. void ceph_init_inode_acls(struct inode *inode, struct ceph_acl_sec_ctx *as_ctx)
  219. {
  220. if (!inode)
  221. return;
  222. ceph_set_cached_acl(inode, ACL_TYPE_ACCESS, as_ctx->acl);
  223. ceph_set_cached_acl(inode, ACL_TYPE_DEFAULT, as_ctx->default_acl);
  224. }