nfs3acl.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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)
  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 (!nfs_server_capable(inode, NFS_CAP_ACLS))
  56. return ERR_PTR(-EOPNOTSUPP);
  57. status = nfs_revalidate_inode(server, inode);
  58. if (status < 0)
  59. return ERR_PTR(status);
  60. /*
  61. * Only get the access acl when explicitly requested: We don't
  62. * need it for access decisions, and only some applications use
  63. * it. Applications which request the access acl first are not
  64. * penalized from this optimization.
  65. */
  66. if (type == ACL_TYPE_ACCESS)
  67. args.mask |= NFS_ACLCNT|NFS_ACL;
  68. if (S_ISDIR(inode->i_mode))
  69. args.mask |= NFS_DFACLCNT|NFS_DFACL;
  70. if (args.mask == 0)
  71. return NULL;
  72. dprintk("NFS call getacl\n");
  73. msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_GETACL];
  74. res.fattr = nfs_alloc_fattr();
  75. if (res.fattr == NULL)
  76. return ERR_PTR(-ENOMEM);
  77. if (args.mask & NFS_ACL)
  78. nfs3_prepare_get_acl(&inode->i_acl);
  79. if (args.mask & NFS_DFACL)
  80. nfs3_prepare_get_acl(&inode->i_default_acl);
  81. status = rpc_call_sync(server->client_acl, &msg, 0);
  82. dprintk("NFS reply getacl: %d\n", status);
  83. /* pages may have been allocated at the xdr layer. */
  84. for (count = 0; count < NFSACL_MAXPAGES && args.pages[count]; count++)
  85. __free_page(args.pages[count]);
  86. switch (status) {
  87. case 0:
  88. status = nfs_refresh_inode(inode, res.fattr);
  89. break;
  90. case -EPFNOSUPPORT:
  91. case -EPROTONOSUPPORT:
  92. dprintk("NFS_V3_ACL extension not supported; disabling\n");
  93. server->caps &= ~NFS_CAP_ACLS;
  94. fallthrough;
  95. case -ENOTSUPP:
  96. status = -EOPNOTSUPP;
  97. default:
  98. goto getout;
  99. }
  100. if ((args.mask & res.mask) != args.mask) {
  101. status = -EIO;
  102. goto getout;
  103. }
  104. if (res.acl_access != NULL) {
  105. if ((posix_acl_equiv_mode(res.acl_access, NULL) == 0) ||
  106. res.acl_access->a_count == 0) {
  107. posix_acl_release(res.acl_access);
  108. res.acl_access = NULL;
  109. }
  110. }
  111. if (res.mask & NFS_ACL)
  112. nfs3_complete_get_acl(&inode->i_acl, res.acl_access);
  113. else
  114. forget_cached_acl(inode, ACL_TYPE_ACCESS);
  115. if (res.mask & NFS_DFACL)
  116. nfs3_complete_get_acl(&inode->i_default_acl, res.acl_default);
  117. else
  118. forget_cached_acl(inode, ACL_TYPE_DEFAULT);
  119. nfs_free_fattr(res.fattr);
  120. if (type == ACL_TYPE_ACCESS) {
  121. posix_acl_release(res.acl_default);
  122. return res.acl_access;
  123. } else {
  124. posix_acl_release(res.acl_access);
  125. return res.acl_default;
  126. }
  127. getout:
  128. nfs3_abort_get_acl(&inode->i_acl);
  129. nfs3_abort_get_acl(&inode->i_default_acl);
  130. posix_acl_release(res.acl_access);
  131. posix_acl_release(res.acl_default);
  132. nfs_free_fattr(res.fattr);
  133. return ERR_PTR(status);
  134. }
  135. static int __nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
  136. struct posix_acl *dfacl)
  137. {
  138. struct nfs_server *server = NFS_SERVER(inode);
  139. struct nfs_fattr *fattr;
  140. struct page *pages[NFSACL_MAXPAGES];
  141. struct nfs3_setaclargs args = {
  142. .inode = inode,
  143. .mask = NFS_ACL,
  144. .acl_access = acl,
  145. .pages = pages,
  146. };
  147. struct rpc_message msg = {
  148. .rpc_argp = &args,
  149. .rpc_resp = &fattr,
  150. };
  151. int status = 0;
  152. if (acl == NULL && (!S_ISDIR(inode->i_mode) || dfacl == NULL))
  153. goto out;
  154. status = -EOPNOTSUPP;
  155. if (!nfs_server_capable(inode, NFS_CAP_ACLS))
  156. goto out;
  157. /* We are doing this here because XDR marshalling does not
  158. * return any results, it BUGs. */
  159. status = -ENOSPC;
  160. if (acl != NULL && acl->a_count > NFS_ACL_MAX_ENTRIES)
  161. goto out;
  162. if (dfacl != NULL && dfacl->a_count > NFS_ACL_MAX_ENTRIES)
  163. goto out;
  164. if (S_ISDIR(inode->i_mode)) {
  165. args.mask |= NFS_DFACL;
  166. args.acl_default = dfacl;
  167. args.len = nfsacl_size(acl, dfacl);
  168. } else
  169. args.len = nfsacl_size(acl, NULL);
  170. if (args.len > NFS_ACL_INLINE_BUFSIZE) {
  171. unsigned int npages = 1 + ((args.len - 1) >> PAGE_SHIFT);
  172. status = -ENOMEM;
  173. do {
  174. args.pages[args.npages] = alloc_page(GFP_KERNEL);
  175. if (args.pages[args.npages] == NULL)
  176. goto out_freepages;
  177. args.npages++;
  178. } while (args.npages < npages);
  179. }
  180. dprintk("NFS call setacl\n");
  181. status = -ENOMEM;
  182. fattr = nfs_alloc_fattr();
  183. if (fattr == NULL)
  184. goto out_freepages;
  185. msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_SETACL];
  186. msg.rpc_resp = fattr;
  187. status = rpc_call_sync(server->client_acl, &msg, 0);
  188. nfs_access_zap_cache(inode);
  189. nfs_zap_acl_cache(inode);
  190. dprintk("NFS reply setacl: %d\n", status);
  191. switch (status) {
  192. case 0:
  193. status = nfs_refresh_inode(inode, fattr);
  194. break;
  195. case -EPFNOSUPPORT:
  196. case -EPROTONOSUPPORT:
  197. dprintk("NFS_V3_ACL SETACL RPC not supported"
  198. "(will not retry)\n");
  199. server->caps &= ~NFS_CAP_ACLS;
  200. fallthrough;
  201. case -ENOTSUPP:
  202. status = -EOPNOTSUPP;
  203. }
  204. nfs_free_fattr(fattr);
  205. out_freepages:
  206. while (args.npages != 0) {
  207. args.npages--;
  208. __free_page(args.pages[args.npages]);
  209. }
  210. out:
  211. return status;
  212. }
  213. int nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
  214. struct posix_acl *dfacl)
  215. {
  216. int ret;
  217. ret = __nfs3_proc_setacls(inode, acl, dfacl);
  218. return (ret == -EOPNOTSUPP) ? 0 : ret;
  219. }
  220. int nfs3_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  221. {
  222. struct posix_acl *orig = acl, *dfacl = NULL, *alloc;
  223. int status;
  224. if (S_ISDIR(inode->i_mode)) {
  225. switch(type) {
  226. case ACL_TYPE_ACCESS:
  227. alloc = get_acl(inode, ACL_TYPE_DEFAULT);
  228. if (IS_ERR(alloc))
  229. goto fail;
  230. dfacl = alloc;
  231. break;
  232. case ACL_TYPE_DEFAULT:
  233. alloc = get_acl(inode, ACL_TYPE_ACCESS);
  234. if (IS_ERR(alloc))
  235. goto fail;
  236. dfacl = acl;
  237. acl = alloc;
  238. break;
  239. }
  240. }
  241. if (acl == NULL) {
  242. alloc = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
  243. if (IS_ERR(alloc))
  244. goto fail;
  245. acl = alloc;
  246. }
  247. status = __nfs3_proc_setacls(inode, acl, dfacl);
  248. out:
  249. if (acl != orig)
  250. posix_acl_release(acl);
  251. if (dfacl != orig)
  252. posix_acl_release(dfacl);
  253. return status;
  254. fail:
  255. status = PTR_ERR(alloc);
  256. goto out;
  257. }
  258. const struct xattr_handler *nfs3_xattr_handlers[] = {
  259. &posix_acl_access_xattr_handler,
  260. &posix_acl_default_xattr_handler,
  261. NULL,
  262. };
  263. static int
  264. nfs3_list_one_acl(struct inode *inode, int type, const char *name, void *data,
  265. size_t size, ssize_t *result)
  266. {
  267. struct posix_acl *acl;
  268. char *p = data + *result;
  269. acl = get_acl(inode, type);
  270. if (IS_ERR_OR_NULL(acl))
  271. return 0;
  272. posix_acl_release(acl);
  273. *result += strlen(name);
  274. *result += 1;
  275. if (!size)
  276. return 0;
  277. if (*result > size)
  278. return -ERANGE;
  279. strcpy(p, name);
  280. return 0;
  281. }
  282. ssize_t
  283. nfs3_listxattr(struct dentry *dentry, char *data, size_t size)
  284. {
  285. struct inode *inode = d_inode(dentry);
  286. ssize_t result = 0;
  287. int error;
  288. error = nfs3_list_one_acl(inode, ACL_TYPE_ACCESS,
  289. XATTR_NAME_POSIX_ACL_ACCESS, data, size, &result);
  290. if (error)
  291. return error;
  292. error = nfs3_list_one_acl(inode, ACL_TYPE_DEFAULT,
  293. XATTR_NAME_POSIX_ACL_DEFAULT, data, size, &result);
  294. if (error)
  295. return error;
  296. return result;
  297. }