nfs3acl.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. case -ENOTSUPP:
  95. status = -EOPNOTSUPP;
  96. default:
  97. goto getout;
  98. }
  99. if ((args.mask & res.mask) != args.mask) {
  100. status = -EIO;
  101. goto getout;
  102. }
  103. if (res.acl_access != NULL) {
  104. if ((posix_acl_equiv_mode(res.acl_access, NULL) == 0) ||
  105. res.acl_access->a_count == 0) {
  106. posix_acl_release(res.acl_access);
  107. res.acl_access = NULL;
  108. }
  109. }
  110. if (res.mask & NFS_ACL)
  111. nfs3_complete_get_acl(&inode->i_acl, res.acl_access);
  112. else
  113. forget_cached_acl(inode, ACL_TYPE_ACCESS);
  114. if (res.mask & NFS_DFACL)
  115. nfs3_complete_get_acl(&inode->i_default_acl, res.acl_default);
  116. else
  117. forget_cached_acl(inode, ACL_TYPE_DEFAULT);
  118. nfs_free_fattr(res.fattr);
  119. if (type == ACL_TYPE_ACCESS) {
  120. posix_acl_release(res.acl_default);
  121. return res.acl_access;
  122. } else {
  123. posix_acl_release(res.acl_access);
  124. return res.acl_default;
  125. }
  126. getout:
  127. nfs3_abort_get_acl(&inode->i_acl);
  128. nfs3_abort_get_acl(&inode->i_default_acl);
  129. posix_acl_release(res.acl_access);
  130. posix_acl_release(res.acl_default);
  131. nfs_free_fattr(res.fattr);
  132. return ERR_PTR(status);
  133. }
  134. static int __nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
  135. struct posix_acl *dfacl)
  136. {
  137. struct nfs_server *server = NFS_SERVER(inode);
  138. struct nfs_fattr *fattr;
  139. struct page *pages[NFSACL_MAXPAGES];
  140. struct nfs3_setaclargs args = {
  141. .inode = inode,
  142. .mask = NFS_ACL,
  143. .acl_access = acl,
  144. .pages = pages,
  145. };
  146. struct rpc_message msg = {
  147. .rpc_argp = &args,
  148. .rpc_resp = &fattr,
  149. };
  150. int status = 0;
  151. if (acl == NULL && (!S_ISDIR(inode->i_mode) || dfacl == NULL))
  152. goto out;
  153. status = -EOPNOTSUPP;
  154. if (!nfs_server_capable(inode, NFS_CAP_ACLS))
  155. goto out;
  156. /* We are doing this here because XDR marshalling does not
  157. * return any results, it BUGs. */
  158. status = -ENOSPC;
  159. if (acl != NULL && acl->a_count > NFS_ACL_MAX_ENTRIES)
  160. goto out;
  161. if (dfacl != NULL && dfacl->a_count > NFS_ACL_MAX_ENTRIES)
  162. goto out;
  163. if (S_ISDIR(inode->i_mode)) {
  164. args.mask |= NFS_DFACL;
  165. args.acl_default = dfacl;
  166. args.len = nfsacl_size(acl, dfacl);
  167. } else
  168. args.len = nfsacl_size(acl, NULL);
  169. if (args.len > NFS_ACL_INLINE_BUFSIZE) {
  170. unsigned int npages = 1 + ((args.len - 1) >> PAGE_SHIFT);
  171. status = -ENOMEM;
  172. do {
  173. args.pages[args.npages] = alloc_page(GFP_KERNEL);
  174. if (args.pages[args.npages] == NULL)
  175. goto out_freepages;
  176. args.npages++;
  177. } while (args.npages < npages);
  178. }
  179. dprintk("NFS call setacl\n");
  180. status = -ENOMEM;
  181. fattr = nfs_alloc_fattr();
  182. if (fattr == NULL)
  183. goto out_freepages;
  184. msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_SETACL];
  185. msg.rpc_resp = fattr;
  186. status = rpc_call_sync(server->client_acl, &msg, 0);
  187. nfs_access_zap_cache(inode);
  188. nfs_zap_acl_cache(inode);
  189. dprintk("NFS reply setacl: %d\n", status);
  190. switch (status) {
  191. case 0:
  192. status = nfs_refresh_inode(inode, fattr);
  193. set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
  194. set_cached_acl(inode, ACL_TYPE_DEFAULT, dfacl);
  195. break;
  196. case -EPFNOSUPPORT:
  197. case -EPROTONOSUPPORT:
  198. dprintk("NFS_V3_ACL SETACL RPC not supported"
  199. "(will not retry)\n");
  200. server->caps &= ~NFS_CAP_ACLS;
  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 *alloc = NULL, *dfacl = NULL;
  223. int status;
  224. if (S_ISDIR(inode->i_mode)) {
  225. switch(type) {
  226. case ACL_TYPE_ACCESS:
  227. alloc = dfacl = get_acl(inode, ACL_TYPE_DEFAULT);
  228. if (IS_ERR(alloc))
  229. goto fail;
  230. break;
  231. case ACL_TYPE_DEFAULT:
  232. dfacl = acl;
  233. alloc = acl = get_acl(inode, ACL_TYPE_ACCESS);
  234. if (IS_ERR(alloc))
  235. goto fail;
  236. break;
  237. }
  238. }
  239. if (acl == NULL) {
  240. alloc = acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
  241. if (IS_ERR(alloc))
  242. goto fail;
  243. }
  244. status = __nfs3_proc_setacls(inode, acl, dfacl);
  245. posix_acl_release(alloc);
  246. return status;
  247. fail:
  248. return PTR_ERR(alloc);
  249. }
  250. const struct xattr_handler *nfs3_xattr_handlers[] = {
  251. &posix_acl_access_xattr_handler,
  252. &posix_acl_default_xattr_handler,
  253. NULL,
  254. };
  255. static int
  256. nfs3_list_one_acl(struct inode *inode, int type, const char *name, void *data,
  257. size_t size, ssize_t *result)
  258. {
  259. struct posix_acl *acl;
  260. char *p = data + *result;
  261. acl = get_acl(inode, type);
  262. if (IS_ERR_OR_NULL(acl))
  263. return 0;
  264. posix_acl_release(acl);
  265. *result += strlen(name);
  266. *result += 1;
  267. if (!size)
  268. return 0;
  269. if (*result > size)
  270. return -ERANGE;
  271. strcpy(p, name);
  272. return 0;
  273. }
  274. ssize_t
  275. nfs3_listxattr(struct dentry *dentry, char *data, size_t size)
  276. {
  277. struct inode *inode = d_inode(dentry);
  278. ssize_t result = 0;
  279. int error;
  280. error = nfs3_list_one_acl(inode, ACL_TYPE_ACCESS,
  281. XATTR_NAME_POSIX_ACL_ACCESS, data, size, &result);
  282. if (error)
  283. return error;
  284. error = nfs3_list_one_acl(inode, ACL_TYPE_DEFAULT,
  285. XATTR_NAME_POSIX_ACL_DEFAULT, data, size, &result);
  286. if (error)
  287. return error;
  288. return result;
  289. }