nfs3acl.c 8.0 KB

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