nfs3acl.c 8.0 KB

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