nfs3acl.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. struct posix_acl *nfs3_get_acl(struct inode *inode, int type)
  12. {
  13. struct nfs_server *server = NFS_SERVER(inode);
  14. struct page *pages[NFSACL_MAXPAGES] = { };
  15. struct nfs3_getaclargs args = {
  16. .fh = NFS_FH(inode),
  17. /* The xdr layer may allocate pages here. */
  18. .pages = pages,
  19. };
  20. struct nfs3_getaclres res = {
  21. NULL,
  22. };
  23. struct rpc_message msg = {
  24. .rpc_argp = &args,
  25. .rpc_resp = &res,
  26. };
  27. int status, count;
  28. if (!nfs_server_capable(inode, NFS_CAP_ACLS))
  29. return ERR_PTR(-EOPNOTSUPP);
  30. status = nfs_revalidate_inode(server, inode);
  31. if (status < 0)
  32. return ERR_PTR(status);
  33. /*
  34. * Only get the access acl when explicitly requested: We don't
  35. * need it for access decisions, and only some applications use
  36. * it. Applications which request the access acl first are not
  37. * penalized from this optimization.
  38. */
  39. if (type == ACL_TYPE_ACCESS)
  40. args.mask |= NFS_ACLCNT|NFS_ACL;
  41. if (S_ISDIR(inode->i_mode))
  42. args.mask |= NFS_DFACLCNT|NFS_DFACL;
  43. if (args.mask == 0)
  44. return NULL;
  45. dprintk("NFS call getacl\n");
  46. msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_GETACL];
  47. res.fattr = nfs_alloc_fattr();
  48. if (res.fattr == NULL)
  49. return ERR_PTR(-ENOMEM);
  50. status = rpc_call_sync(server->client_acl, &msg, 0);
  51. dprintk("NFS reply getacl: %d\n", status);
  52. /* pages may have been allocated at the xdr layer. */
  53. for (count = 0; count < NFSACL_MAXPAGES && args.pages[count]; count++)
  54. __free_page(args.pages[count]);
  55. switch (status) {
  56. case 0:
  57. status = nfs_refresh_inode(inode, res.fattr);
  58. break;
  59. case -EPFNOSUPPORT:
  60. case -EPROTONOSUPPORT:
  61. dprintk("NFS_V3_ACL extension not supported; disabling\n");
  62. server->caps &= ~NFS_CAP_ACLS;
  63. case -ENOTSUPP:
  64. status = -EOPNOTSUPP;
  65. default:
  66. goto getout;
  67. }
  68. if ((args.mask & res.mask) != args.mask) {
  69. status = -EIO;
  70. goto getout;
  71. }
  72. if (res.acl_access != NULL) {
  73. if ((posix_acl_equiv_mode(res.acl_access, NULL) == 0) ||
  74. res.acl_access->a_count == 0) {
  75. posix_acl_release(res.acl_access);
  76. res.acl_access = NULL;
  77. }
  78. }
  79. if (res.mask & NFS_ACL)
  80. set_cached_acl(inode, ACL_TYPE_ACCESS, res.acl_access);
  81. else
  82. forget_cached_acl(inode, ACL_TYPE_ACCESS);
  83. if (res.mask & NFS_DFACL)
  84. set_cached_acl(inode, ACL_TYPE_DEFAULT, res.acl_default);
  85. else
  86. forget_cached_acl(inode, ACL_TYPE_DEFAULT);
  87. nfs_free_fattr(res.fattr);
  88. if (type == ACL_TYPE_ACCESS) {
  89. posix_acl_release(res.acl_default);
  90. return res.acl_access;
  91. } else {
  92. posix_acl_release(res.acl_access);
  93. return res.acl_default;
  94. }
  95. getout:
  96. posix_acl_release(res.acl_access);
  97. posix_acl_release(res.acl_default);
  98. nfs_free_fattr(res.fattr);
  99. return ERR_PTR(status);
  100. }
  101. static int __nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
  102. struct posix_acl *dfacl)
  103. {
  104. struct nfs_server *server = NFS_SERVER(inode);
  105. struct nfs_fattr *fattr;
  106. struct page *pages[NFSACL_MAXPAGES];
  107. struct nfs3_setaclargs args = {
  108. .inode = inode,
  109. .mask = NFS_ACL,
  110. .acl_access = acl,
  111. .pages = pages,
  112. };
  113. struct rpc_message msg = {
  114. .rpc_argp = &args,
  115. .rpc_resp = &fattr,
  116. };
  117. int status = 0;
  118. if (acl == NULL && (!S_ISDIR(inode->i_mode) || dfacl == NULL))
  119. goto out;
  120. status = -EOPNOTSUPP;
  121. if (!nfs_server_capable(inode, NFS_CAP_ACLS))
  122. goto out;
  123. /* We are doing this here because XDR marshalling does not
  124. * return any results, it BUGs. */
  125. status = -ENOSPC;
  126. if (acl != NULL && acl->a_count > NFS_ACL_MAX_ENTRIES)
  127. goto out;
  128. if (dfacl != NULL && dfacl->a_count > NFS_ACL_MAX_ENTRIES)
  129. goto out;
  130. if (S_ISDIR(inode->i_mode)) {
  131. args.mask |= NFS_DFACL;
  132. args.acl_default = dfacl;
  133. args.len = nfsacl_size(acl, dfacl);
  134. } else
  135. args.len = nfsacl_size(acl, NULL);
  136. if (args.len > NFS_ACL_INLINE_BUFSIZE) {
  137. unsigned int npages = 1 + ((args.len - 1) >> PAGE_SHIFT);
  138. status = -ENOMEM;
  139. do {
  140. args.pages[args.npages] = alloc_page(GFP_KERNEL);
  141. if (args.pages[args.npages] == NULL)
  142. goto out_freepages;
  143. args.npages++;
  144. } while (args.npages < npages);
  145. }
  146. dprintk("NFS call setacl\n");
  147. status = -ENOMEM;
  148. fattr = nfs_alloc_fattr();
  149. if (fattr == NULL)
  150. goto out_freepages;
  151. msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_SETACL];
  152. msg.rpc_resp = fattr;
  153. status = rpc_call_sync(server->client_acl, &msg, 0);
  154. nfs_access_zap_cache(inode);
  155. nfs_zap_acl_cache(inode);
  156. dprintk("NFS reply setacl: %d\n", status);
  157. switch (status) {
  158. case 0:
  159. status = nfs_refresh_inode(inode, fattr);
  160. set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
  161. set_cached_acl(inode, ACL_TYPE_DEFAULT, dfacl);
  162. break;
  163. case -EPFNOSUPPORT:
  164. case -EPROTONOSUPPORT:
  165. dprintk("NFS_V3_ACL SETACL RPC not supported"
  166. "(will not retry)\n");
  167. server->caps &= ~NFS_CAP_ACLS;
  168. case -ENOTSUPP:
  169. status = -EOPNOTSUPP;
  170. }
  171. nfs_free_fattr(fattr);
  172. out_freepages:
  173. while (args.npages != 0) {
  174. args.npages--;
  175. __free_page(args.pages[args.npages]);
  176. }
  177. out:
  178. return status;
  179. }
  180. int nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
  181. struct posix_acl *dfacl)
  182. {
  183. int ret;
  184. ret = __nfs3_proc_setacls(inode, acl, dfacl);
  185. return (ret == -EOPNOTSUPP) ? 0 : ret;
  186. }
  187. int nfs3_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  188. {
  189. struct posix_acl *alloc = NULL, *dfacl = NULL;
  190. int status;
  191. if (S_ISDIR(inode->i_mode)) {
  192. switch(type) {
  193. case ACL_TYPE_ACCESS:
  194. alloc = dfacl = get_acl(inode, ACL_TYPE_DEFAULT);
  195. if (IS_ERR(alloc))
  196. goto fail;
  197. break;
  198. case ACL_TYPE_DEFAULT:
  199. dfacl = acl;
  200. alloc = acl = get_acl(inode, ACL_TYPE_ACCESS);
  201. if (IS_ERR(alloc))
  202. goto fail;
  203. break;
  204. }
  205. }
  206. if (acl == NULL) {
  207. alloc = acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
  208. if (IS_ERR(alloc))
  209. goto fail;
  210. }
  211. status = __nfs3_proc_setacls(inode, acl, dfacl);
  212. posix_acl_release(alloc);
  213. return status;
  214. fail:
  215. return PTR_ERR(alloc);
  216. }
  217. const struct xattr_handler *nfs3_xattr_handlers[] = {
  218. &posix_acl_access_xattr_handler,
  219. &posix_acl_default_xattr_handler,
  220. NULL,
  221. };
  222. static int
  223. nfs3_list_one_acl(struct inode *inode, int type, const char *name, void *data,
  224. size_t size, ssize_t *result)
  225. {
  226. struct posix_acl *acl;
  227. char *p = data + *result;
  228. acl = get_acl(inode, type);
  229. if (IS_ERR_OR_NULL(acl))
  230. return 0;
  231. posix_acl_release(acl);
  232. *result += strlen(name);
  233. *result += 1;
  234. if (!size)
  235. return 0;
  236. if (*result > size)
  237. return -ERANGE;
  238. strcpy(p, name);
  239. return 0;
  240. }
  241. ssize_t
  242. nfs3_listxattr(struct dentry *dentry, char *data, size_t size)
  243. {
  244. struct inode *inode = d_inode(dentry);
  245. ssize_t result = 0;
  246. int error;
  247. error = nfs3_list_one_acl(inode, ACL_TYPE_ACCESS,
  248. POSIX_ACL_XATTR_ACCESS, data, size, &result);
  249. if (error)
  250. return error;
  251. error = nfs3_list_one_acl(inode, ACL_TYPE_DEFAULT,
  252. POSIX_ACL_XATTR_DEFAULT, data, size, &result);
  253. if (error)
  254. return error;
  255. return result;
  256. }