nfs3acl.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/fs.h>
  3. #include <linux/gfp.h>
  4. #include <linux/nfs.h>
  5. #include <linux/nfs3.h>
  6. #include <linux/nfs_fs.h>
  7. #include <linux/posix_acl_xattr.h>
  8. #include <linux/nfsacl.h>
  9. #include "internal.h"
  10. #include "nfs3_fs.h"
  11. #define NFSDBG_FACILITY NFSDBG_PROC
  12. /*
  13. * nfs3_prepare_get_acl, nfs3_complete_get_acl, nfs3_abort_get_acl: Helpers for
  14. * caching get_acl results in a race-free way. See fs/posix_acl.c:get_acl()
  15. * for explanations.
  16. */
  17. static void nfs3_prepare_get_acl(struct posix_acl **p)
  18. {
  19. struct posix_acl *sentinel = uncached_acl_sentinel(current);
  20. if (cmpxchg(p, ACL_NOT_CACHED, sentinel) != ACL_NOT_CACHED) {
  21. /* Not the first reader or sentinel already in place. */
  22. }
  23. }
  24. static void nfs3_complete_get_acl(struct posix_acl **p, struct posix_acl *acl)
  25. {
  26. struct posix_acl *sentinel = uncached_acl_sentinel(current);
  27. /* Only cache the ACL if our sentinel is still in place. */
  28. posix_acl_dup(acl);
  29. if (cmpxchg(p, sentinel, acl) != sentinel)
  30. posix_acl_release(acl);
  31. }
  32. static void nfs3_abort_get_acl(struct posix_acl **p)
  33. {
  34. struct posix_acl *sentinel = uncached_acl_sentinel(current);
  35. /* Remove our sentinel upon failure. */
  36. cmpxchg(p, sentinel, ACL_NOT_CACHED);
  37. }
  38. struct posix_acl *nfs3_get_acl(struct inode *inode, int type, bool rcu)
  39. {
  40. struct nfs_server *server = NFS_SERVER(inode);
  41. struct page *pages[NFSACL_MAXPAGES] = { };
  42. struct nfs3_getaclargs args = {
  43. .fh = NFS_FH(inode),
  44. /* The xdr layer may allocate pages here. */
  45. .pages = pages,
  46. };
  47. struct nfs3_getaclres res = {
  48. NULL,
  49. };
  50. struct rpc_message msg = {
  51. .rpc_argp = &args,
  52. .rpc_resp = &res,
  53. };
  54. int status, count;
  55. if (rcu)
  56. return ERR_PTR(-ECHILD);
  57. if (!nfs_server_capable(inode, NFS_CAP_ACLS))
  58. return ERR_PTR(-EOPNOTSUPP);
  59. status = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
  60. if (status < 0)
  61. return ERR_PTR(status);
  62. /*
  63. * Only get the access acl when explicitly requested: We don't
  64. * need it for access decisions, and only some applications use
  65. * it. Applications which request the access acl first are not
  66. * penalized from this optimization.
  67. */
  68. if (type == ACL_TYPE_ACCESS)
  69. args.mask |= NFS_ACLCNT|NFS_ACL;
  70. if (S_ISDIR(inode->i_mode))
  71. args.mask |= NFS_DFACLCNT|NFS_DFACL;
  72. if (args.mask == 0)
  73. return NULL;
  74. dprintk("NFS call getacl\n");
  75. msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_GETACL];
  76. res.fattr = nfs_alloc_fattr();
  77. if (res.fattr == NULL)
  78. return ERR_PTR(-ENOMEM);
  79. if (args.mask & NFS_ACL)
  80. nfs3_prepare_get_acl(&inode->i_acl);
  81. if (args.mask & NFS_DFACL)
  82. nfs3_prepare_get_acl(&inode->i_default_acl);
  83. status = rpc_call_sync(server->client_acl, &msg, 0);
  84. dprintk("NFS reply getacl: %d\n", status);
  85. /* pages may have been allocated at the xdr layer. */
  86. for (count = 0; count < NFSACL_MAXPAGES && args.pages[count]; count++)
  87. __free_page(args.pages[count]);
  88. switch (status) {
  89. case 0:
  90. status = nfs_refresh_inode(inode, res.fattr);
  91. break;
  92. case -EPFNOSUPPORT:
  93. case -EPROTONOSUPPORT:
  94. dprintk("NFS_V3_ACL extension not supported; disabling\n");
  95. server->caps &= ~NFS_CAP_ACLS;
  96. fallthrough;
  97. case -ENOTSUPP:
  98. status = -EOPNOTSUPP;
  99. goto getout;
  100. default:
  101. goto getout;
  102. }
  103. if ((args.mask & res.mask) != args.mask) {
  104. status = -EIO;
  105. goto getout;
  106. }
  107. if (res.acl_access != NULL) {
  108. if ((posix_acl_equiv_mode(res.acl_access, NULL) == 0) ||
  109. res.acl_access->a_count == 0) {
  110. posix_acl_release(res.acl_access);
  111. res.acl_access = NULL;
  112. }
  113. }
  114. if (res.mask & NFS_ACL)
  115. nfs3_complete_get_acl(&inode->i_acl, res.acl_access);
  116. else
  117. forget_cached_acl(inode, ACL_TYPE_ACCESS);
  118. if (res.mask & NFS_DFACL)
  119. nfs3_complete_get_acl(&inode->i_default_acl, res.acl_default);
  120. else
  121. forget_cached_acl(inode, ACL_TYPE_DEFAULT);
  122. nfs_free_fattr(res.fattr);
  123. if (type == ACL_TYPE_ACCESS) {
  124. posix_acl_release(res.acl_default);
  125. return res.acl_access;
  126. } else {
  127. posix_acl_release(res.acl_access);
  128. return res.acl_default;
  129. }
  130. getout:
  131. nfs3_abort_get_acl(&inode->i_acl);
  132. nfs3_abort_get_acl(&inode->i_default_acl);
  133. posix_acl_release(res.acl_access);
  134. posix_acl_release(res.acl_default);
  135. nfs_free_fattr(res.fattr);
  136. return ERR_PTR(status);
  137. }
  138. static int __nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
  139. struct posix_acl *dfacl)
  140. {
  141. struct nfs_server *server = NFS_SERVER(inode);
  142. struct nfs_fattr *fattr;
  143. struct page *pages[NFSACL_MAXPAGES];
  144. struct nfs3_setaclargs args = {
  145. .inode = inode,
  146. .mask = NFS_ACL,
  147. .acl_access = acl,
  148. .pages = pages,
  149. };
  150. struct rpc_message msg = {
  151. .rpc_argp = &args,
  152. .rpc_resp = &fattr,
  153. };
  154. int status = 0;
  155. if (acl == NULL && (!S_ISDIR(inode->i_mode) || dfacl == NULL))
  156. goto out;
  157. status = -EOPNOTSUPP;
  158. if (!nfs_server_capable(inode, NFS_CAP_ACLS))
  159. goto out;
  160. /* We are doing this here because XDR marshalling does not
  161. * return any results, it BUGs. */
  162. status = -ENOSPC;
  163. if (acl != NULL && acl->a_count > NFS_ACL_MAX_ENTRIES)
  164. goto out;
  165. if (dfacl != NULL && dfacl->a_count > NFS_ACL_MAX_ENTRIES)
  166. goto out;
  167. if (S_ISDIR(inode->i_mode)) {
  168. args.mask |= NFS_DFACL;
  169. args.acl_default = dfacl;
  170. args.len = nfsacl_size(acl, dfacl);
  171. } else
  172. args.len = nfsacl_size(acl, NULL);
  173. if (args.len > NFS_ACL_INLINE_BUFSIZE) {
  174. unsigned int npages = 1 + ((args.len - 1) >> PAGE_SHIFT);
  175. status = -ENOMEM;
  176. do {
  177. args.pages[args.npages] = alloc_page(GFP_KERNEL);
  178. if (args.pages[args.npages] == NULL)
  179. goto out_freepages;
  180. args.npages++;
  181. } while (args.npages < npages);
  182. }
  183. dprintk("NFS call setacl\n");
  184. status = -ENOMEM;
  185. fattr = nfs_alloc_fattr();
  186. if (fattr == NULL)
  187. goto out_freepages;
  188. msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_SETACL];
  189. msg.rpc_resp = fattr;
  190. status = rpc_call_sync(server->client_acl, &msg, 0);
  191. nfs_access_zap_cache(inode);
  192. nfs_zap_acl_cache(inode);
  193. dprintk("NFS reply setacl: %d\n", status);
  194. switch (status) {
  195. case 0:
  196. status = nfs_refresh_inode(inode, fattr);
  197. break;
  198. case -EPFNOSUPPORT:
  199. case -EPROTONOSUPPORT:
  200. dprintk("NFS_V3_ACL SETACL RPC not supported"
  201. "(will not retry)\n");
  202. server->caps &= ~NFS_CAP_ACLS;
  203. fallthrough;
  204. case -ENOTSUPP:
  205. status = -EOPNOTSUPP;
  206. }
  207. nfs_free_fattr(fattr);
  208. out_freepages:
  209. while (args.npages != 0) {
  210. args.npages--;
  211. __free_page(args.pages[args.npages]);
  212. }
  213. out:
  214. return status;
  215. }
  216. int nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
  217. struct posix_acl *dfacl)
  218. {
  219. int ret;
  220. ret = __nfs3_proc_setacls(inode, acl, dfacl);
  221. return (ret == -EOPNOTSUPP) ? 0 : ret;
  222. }
  223. int nfs3_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
  224. struct posix_acl *acl, int type)
  225. {
  226. struct posix_acl *orig = acl, *dfacl = NULL, *alloc;
  227. int status;
  228. if (S_ISDIR(inode->i_mode)) {
  229. switch(type) {
  230. case ACL_TYPE_ACCESS:
  231. alloc = get_acl(inode, ACL_TYPE_DEFAULT);
  232. if (IS_ERR(alloc))
  233. goto fail;
  234. dfacl = alloc;
  235. break;
  236. case ACL_TYPE_DEFAULT:
  237. alloc = get_acl(inode, ACL_TYPE_ACCESS);
  238. if (IS_ERR(alloc))
  239. goto fail;
  240. dfacl = acl;
  241. acl = alloc;
  242. break;
  243. }
  244. }
  245. if (acl == NULL) {
  246. alloc = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
  247. if (IS_ERR(alloc))
  248. goto fail;
  249. acl = alloc;
  250. }
  251. status = __nfs3_proc_setacls(inode, acl, dfacl);
  252. out:
  253. if (acl != orig)
  254. posix_acl_release(acl);
  255. if (dfacl != orig)
  256. posix_acl_release(dfacl);
  257. return status;
  258. fail:
  259. status = PTR_ERR(alloc);
  260. goto out;
  261. }
  262. const struct xattr_handler *nfs3_xattr_handlers[] = {
  263. &posix_acl_access_xattr_handler,
  264. &posix_acl_default_xattr_handler,
  265. NULL,
  266. };
  267. static int
  268. nfs3_list_one_acl(struct inode *inode, int type, const char *name, void *data,
  269. size_t size, ssize_t *result)
  270. {
  271. struct posix_acl *acl;
  272. char *p = data + *result;
  273. acl = get_acl(inode, type);
  274. if (IS_ERR_OR_NULL(acl))
  275. return 0;
  276. posix_acl_release(acl);
  277. *result += strlen(name);
  278. *result += 1;
  279. if (!size)
  280. return 0;
  281. if (*result > size)
  282. return -ERANGE;
  283. strcpy(p, name);
  284. return 0;
  285. }
  286. ssize_t
  287. nfs3_listxattr(struct dentry *dentry, char *data, size_t size)
  288. {
  289. struct inode *inode = d_inode(dentry);
  290. ssize_t result = 0;
  291. int error;
  292. error = nfs3_list_one_acl(inode, ACL_TYPE_ACCESS,
  293. XATTR_NAME_POSIX_ACL_ACCESS, data, size, &result);
  294. if (error)
  295. return error;
  296. error = nfs3_list_one_acl(inode, ACL_TYPE_DEFAULT,
  297. XATTR_NAME_POSIX_ACL_DEFAULT, data, size, &result);
  298. if (error)
  299. return error;
  300. return result;
  301. }