acl.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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, size = 0;
  80. const char *name = NULL;
  81. char *value = NULL;
  82. struct iattr newattrs;
  83. struct inode *inode = d_inode(dentry);
  84. struct timespec64 old_ctime = inode_get_ctime(inode);
  85. umode_t new_mode = inode->i_mode, old_mode = inode->i_mode;
  86. if (ceph_snap(inode) != CEPH_NOSNAP) {
  87. ret = -EROFS;
  88. goto out;
  89. }
  90. switch (type) {
  91. case ACL_TYPE_ACCESS:
  92. name = XATTR_NAME_POSIX_ACL_ACCESS;
  93. if (acl) {
  94. ret = posix_acl_update_mode(idmap, inode,
  95. &new_mode, &acl);
  96. if (ret)
  97. goto out;
  98. }
  99. break;
  100. case ACL_TYPE_DEFAULT:
  101. if (!S_ISDIR(inode->i_mode)) {
  102. ret = acl ? -EINVAL : 0;
  103. goto out;
  104. }
  105. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  106. break;
  107. default:
  108. ret = -EINVAL;
  109. goto out;
  110. }
  111. if (acl) {
  112. size = posix_acl_xattr_size(acl->a_count);
  113. value = kmalloc(size, GFP_NOFS);
  114. if (!value) {
  115. ret = -ENOMEM;
  116. goto out;
  117. }
  118. ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  119. if (ret < 0)
  120. goto out_free;
  121. }
  122. if (new_mode != old_mode) {
  123. newattrs.ia_ctime = current_time(inode);
  124. newattrs.ia_mode = new_mode;
  125. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  126. ret = __ceph_setattr(idmap, inode, &newattrs, NULL);
  127. if (ret)
  128. goto out_free;
  129. }
  130. ret = __ceph_setxattr(inode, name, value, size, 0);
  131. if (ret) {
  132. if (new_mode != old_mode) {
  133. newattrs.ia_ctime = old_ctime;
  134. newattrs.ia_mode = old_mode;
  135. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  136. __ceph_setattr(idmap, inode, &newattrs, NULL);
  137. }
  138. goto out_free;
  139. }
  140. ceph_set_cached_acl(inode, type, acl);
  141. out_free:
  142. kfree(value);
  143. out:
  144. return ret;
  145. }
  146. int ceph_pre_init_acls(struct inode *dir, umode_t *mode,
  147. struct ceph_acl_sec_ctx *as_ctx)
  148. {
  149. struct posix_acl *acl, *default_acl;
  150. size_t val_size1 = 0, val_size2 = 0;
  151. struct ceph_pagelist *pagelist = NULL;
  152. void *tmp_buf = NULL;
  153. int err;
  154. err = posix_acl_create(dir, mode, &default_acl, &acl);
  155. if (err)
  156. return err;
  157. if (acl) {
  158. err = posix_acl_equiv_mode(acl, mode);
  159. if (err < 0)
  160. goto out_err;
  161. if (err == 0) {
  162. posix_acl_release(acl);
  163. acl = NULL;
  164. }
  165. }
  166. if (!default_acl && !acl)
  167. return 0;
  168. if (acl)
  169. val_size1 = posix_acl_xattr_size(acl->a_count);
  170. if (default_acl)
  171. val_size2 = posix_acl_xattr_size(default_acl->a_count);
  172. err = -ENOMEM;
  173. tmp_buf = kmalloc(max(val_size1, val_size2), GFP_KERNEL);
  174. if (!tmp_buf)
  175. goto out_err;
  176. pagelist = ceph_pagelist_alloc(GFP_KERNEL);
  177. if (!pagelist)
  178. goto out_err;
  179. err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
  180. if (err)
  181. goto out_err;
  182. ceph_pagelist_encode_32(pagelist, acl && default_acl ? 2 : 1);
  183. if (acl) {
  184. size_t len = strlen(XATTR_NAME_POSIX_ACL_ACCESS);
  185. err = ceph_pagelist_reserve(pagelist, len + val_size1 + 8);
  186. if (err)
  187. goto out_err;
  188. ceph_pagelist_encode_string(pagelist, XATTR_NAME_POSIX_ACL_ACCESS,
  189. len);
  190. err = posix_acl_to_xattr(&init_user_ns, acl,
  191. tmp_buf, val_size1);
  192. if (err < 0)
  193. goto out_err;
  194. ceph_pagelist_encode_32(pagelist, val_size1);
  195. ceph_pagelist_append(pagelist, tmp_buf, val_size1);
  196. }
  197. if (default_acl) {
  198. size_t len = strlen(XATTR_NAME_POSIX_ACL_DEFAULT);
  199. err = ceph_pagelist_reserve(pagelist, len + val_size2 + 8);
  200. if (err)
  201. goto out_err;
  202. ceph_pagelist_encode_string(pagelist,
  203. XATTR_NAME_POSIX_ACL_DEFAULT, len);
  204. err = posix_acl_to_xattr(&init_user_ns, default_acl,
  205. tmp_buf, val_size2);
  206. if (err < 0)
  207. goto out_err;
  208. ceph_pagelist_encode_32(pagelist, val_size2);
  209. ceph_pagelist_append(pagelist, tmp_buf, val_size2);
  210. }
  211. kfree(tmp_buf);
  212. as_ctx->acl = acl;
  213. as_ctx->default_acl = default_acl;
  214. as_ctx->pagelist = pagelist;
  215. return 0;
  216. out_err:
  217. posix_acl_release(acl);
  218. posix_acl_release(default_acl);
  219. kfree(tmp_buf);
  220. if (pagelist)
  221. ceph_pagelist_release(pagelist);
  222. return err;
  223. }
  224. void ceph_init_inode_acls(struct inode *inode, struct ceph_acl_sec_ctx *as_ctx)
  225. {
  226. if (!inode)
  227. return;
  228. ceph_set_cached_acl(inode, ACL_TYPE_ACCESS, as_ctx->acl);
  229. ceph_set_cached_acl(inode, ACL_TYPE_DEFAULT, as_ctx->default_acl);
  230. }