nfs3acl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. #define NFSDBG_FACILITY NFSDBG_PROC
  10. ssize_t nfs3_listxattr(struct dentry *dentry, char *buffer, size_t size)
  11. {
  12. struct inode *inode = dentry->d_inode;
  13. struct posix_acl *acl;
  14. int pos=0, len=0;
  15. # define output(s) do { \
  16. if (pos + sizeof(s) <= size) { \
  17. memcpy(buffer + pos, s, sizeof(s)); \
  18. pos += sizeof(s); \
  19. } \
  20. len += sizeof(s); \
  21. } while(0)
  22. acl = nfs3_proc_getacl(inode, ACL_TYPE_ACCESS);
  23. if (IS_ERR(acl))
  24. return PTR_ERR(acl);
  25. if (acl) {
  26. output("system.posix_acl_access");
  27. posix_acl_release(acl);
  28. }
  29. if (S_ISDIR(inode->i_mode)) {
  30. acl = nfs3_proc_getacl(inode, ACL_TYPE_DEFAULT);
  31. if (IS_ERR(acl))
  32. return PTR_ERR(acl);
  33. if (acl) {
  34. output("system.posix_acl_default");
  35. posix_acl_release(acl);
  36. }
  37. }
  38. # undef output
  39. if (!buffer || len <= size)
  40. return len;
  41. return -ERANGE;
  42. }
  43. ssize_t nfs3_getxattr(struct dentry *dentry, const char *name,
  44. void *buffer, size_t size)
  45. {
  46. struct inode *inode = dentry->d_inode;
  47. struct posix_acl *acl;
  48. int type, error = 0;
  49. if (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0)
  50. type = ACL_TYPE_ACCESS;
  51. else if (strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0)
  52. type = ACL_TYPE_DEFAULT;
  53. else
  54. return -EOPNOTSUPP;
  55. acl = nfs3_proc_getacl(inode, type);
  56. if (IS_ERR(acl))
  57. return PTR_ERR(acl);
  58. else if (acl) {
  59. if (type == ACL_TYPE_ACCESS && acl->a_count == 0)
  60. error = -ENODATA;
  61. else
  62. error = posix_acl_to_xattr(acl, buffer, size);
  63. posix_acl_release(acl);
  64. } else
  65. error = -ENODATA;
  66. return error;
  67. }
  68. int nfs3_setxattr(struct dentry *dentry, const char *name,
  69. const void *value, size_t size, int flags)
  70. {
  71. struct inode *inode = dentry->d_inode;
  72. struct posix_acl *acl;
  73. int type, error;
  74. if (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0)
  75. type = ACL_TYPE_ACCESS;
  76. else if (strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0)
  77. type = ACL_TYPE_DEFAULT;
  78. else
  79. return -EOPNOTSUPP;
  80. acl = posix_acl_from_xattr(value, size);
  81. if (IS_ERR(acl))
  82. return PTR_ERR(acl);
  83. error = nfs3_proc_setacl(inode, type, acl);
  84. posix_acl_release(acl);
  85. return error;
  86. }
  87. int nfs3_removexattr(struct dentry *dentry, const char *name)
  88. {
  89. struct inode *inode = dentry->d_inode;
  90. int type;
  91. if (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0)
  92. type = ACL_TYPE_ACCESS;
  93. else if (strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0)
  94. type = ACL_TYPE_DEFAULT;
  95. else
  96. return -EOPNOTSUPP;
  97. return nfs3_proc_setacl(inode, type, NULL);
  98. }
  99. static void __nfs3_forget_cached_acls(struct nfs_inode *nfsi)
  100. {
  101. if (!IS_ERR(nfsi->acl_access)) {
  102. posix_acl_release(nfsi->acl_access);
  103. nfsi->acl_access = ERR_PTR(-EAGAIN);
  104. }
  105. if (!IS_ERR(nfsi->acl_default)) {
  106. posix_acl_release(nfsi->acl_default);
  107. nfsi->acl_default = ERR_PTR(-EAGAIN);
  108. }
  109. }
  110. void nfs3_forget_cached_acls(struct inode *inode)
  111. {
  112. dprintk("NFS: nfs3_forget_cached_acls(%s/%ld)\n", inode->i_sb->s_id,
  113. inode->i_ino);
  114. spin_lock(&inode->i_lock);
  115. __nfs3_forget_cached_acls(NFS_I(inode));
  116. spin_unlock(&inode->i_lock);
  117. }
  118. static struct posix_acl *nfs3_get_cached_acl(struct inode *inode, int type)
  119. {
  120. struct nfs_inode *nfsi = NFS_I(inode);
  121. struct posix_acl *acl = ERR_PTR(-EINVAL);
  122. spin_lock(&inode->i_lock);
  123. switch(type) {
  124. case ACL_TYPE_ACCESS:
  125. acl = nfsi->acl_access;
  126. break;
  127. case ACL_TYPE_DEFAULT:
  128. acl = nfsi->acl_default;
  129. break;
  130. default:
  131. goto out;
  132. }
  133. if (IS_ERR(acl))
  134. acl = ERR_PTR(-EAGAIN);
  135. else
  136. acl = posix_acl_dup(acl);
  137. out:
  138. spin_unlock(&inode->i_lock);
  139. dprintk("NFS: nfs3_get_cached_acl(%s/%ld, %d) = %p\n", inode->i_sb->s_id,
  140. inode->i_ino, type, acl);
  141. return acl;
  142. }
  143. static void nfs3_cache_acls(struct inode *inode, struct posix_acl *acl,
  144. struct posix_acl *dfacl)
  145. {
  146. struct nfs_inode *nfsi = NFS_I(inode);
  147. dprintk("nfs3_cache_acls(%s/%ld, %p, %p)\n", inode->i_sb->s_id,
  148. inode->i_ino, acl, dfacl);
  149. spin_lock(&inode->i_lock);
  150. __nfs3_forget_cached_acls(NFS_I(inode));
  151. if (!IS_ERR(acl))
  152. nfsi->acl_access = posix_acl_dup(acl);
  153. if (!IS_ERR(dfacl))
  154. nfsi->acl_default = posix_acl_dup(dfacl);
  155. spin_unlock(&inode->i_lock);
  156. }
  157. struct posix_acl *nfs3_proc_getacl(struct inode *inode, int type)
  158. {
  159. struct nfs_server *server = NFS_SERVER(inode);
  160. struct nfs_fattr fattr;
  161. struct page *pages[NFSACL_MAXPAGES] = { };
  162. struct nfs3_getaclargs args = {
  163. .fh = NFS_FH(inode),
  164. /* The xdr layer may allocate pages here. */
  165. .pages = pages,
  166. };
  167. struct nfs3_getaclres res = {
  168. .fattr = &fattr,
  169. };
  170. struct rpc_message msg = {
  171. .rpc_argp = &args,
  172. .rpc_resp = &res,
  173. };
  174. struct posix_acl *acl;
  175. int status, count;
  176. if (!nfs_server_capable(inode, NFS_CAP_ACLS))
  177. return ERR_PTR(-EOPNOTSUPP);
  178. status = nfs_revalidate_inode(server, inode);
  179. if (status < 0)
  180. return ERR_PTR(status);
  181. acl = nfs3_get_cached_acl(inode, type);
  182. if (acl != ERR_PTR(-EAGAIN))
  183. return acl;
  184. acl = NULL;
  185. /*
  186. * Only get the access acl when explicitly requested: We don't
  187. * need it for access decisions, and only some applications use
  188. * it. Applications which request the access acl first are not
  189. * penalized from this optimization.
  190. */
  191. if (type == ACL_TYPE_ACCESS)
  192. args.mask |= NFS_ACLCNT|NFS_ACL;
  193. if (S_ISDIR(inode->i_mode))
  194. args.mask |= NFS_DFACLCNT|NFS_DFACL;
  195. if (args.mask == 0)
  196. return NULL;
  197. dprintk("NFS call getacl\n");
  198. msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_GETACL];
  199. nfs_fattr_init(&fattr);
  200. status = rpc_call_sync(server->client_acl, &msg, 0);
  201. dprintk("NFS reply getacl: %d\n", status);
  202. /* pages may have been allocated at the xdr layer. */
  203. for (count = 0; count < NFSACL_MAXPAGES && args.pages[count]; count++)
  204. __free_page(args.pages[count]);
  205. switch (status) {
  206. case 0:
  207. status = nfs_refresh_inode(inode, &fattr);
  208. break;
  209. case -EPFNOSUPPORT:
  210. case -EPROTONOSUPPORT:
  211. dprintk("NFS_V3_ACL extension not supported; disabling\n");
  212. server->caps &= ~NFS_CAP_ACLS;
  213. case -ENOTSUPP:
  214. status = -EOPNOTSUPP;
  215. default:
  216. goto getout;
  217. }
  218. if ((args.mask & res.mask) != args.mask) {
  219. status = -EIO;
  220. goto getout;
  221. }
  222. if (res.acl_access != NULL) {
  223. if (posix_acl_equiv_mode(res.acl_access, NULL) == 0) {
  224. posix_acl_release(res.acl_access);
  225. res.acl_access = NULL;
  226. }
  227. }
  228. nfs3_cache_acls(inode,
  229. (res.mask & NFS_ACL) ? res.acl_access : ERR_PTR(-EINVAL),
  230. (res.mask & NFS_DFACL) ? res.acl_default : ERR_PTR(-EINVAL));
  231. switch(type) {
  232. case ACL_TYPE_ACCESS:
  233. acl = res.acl_access;
  234. res.acl_access = NULL;
  235. break;
  236. case ACL_TYPE_DEFAULT:
  237. acl = res.acl_default;
  238. res.acl_default = NULL;
  239. }
  240. getout:
  241. posix_acl_release(res.acl_access);
  242. posix_acl_release(res.acl_default);
  243. if (status != 0) {
  244. posix_acl_release(acl);
  245. acl = ERR_PTR(status);
  246. }
  247. return acl;
  248. }
  249. static int nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
  250. struct posix_acl *dfacl)
  251. {
  252. struct nfs_server *server = NFS_SERVER(inode);
  253. struct nfs_fattr fattr;
  254. struct page *pages[NFSACL_MAXPAGES];
  255. struct nfs3_setaclargs args = {
  256. .inode = inode,
  257. .mask = NFS_ACL,
  258. .acl_access = acl,
  259. .pages = pages,
  260. };
  261. struct rpc_message msg = {
  262. .rpc_argp = &args,
  263. .rpc_resp = &fattr,
  264. };
  265. int status;
  266. status = -EOPNOTSUPP;
  267. if (!nfs_server_capable(inode, NFS_CAP_ACLS))
  268. goto out;
  269. /* We are doing this here, because XDR marshalling can only
  270. return -ENOMEM. */
  271. status = -ENOSPC;
  272. if (acl != NULL && acl->a_count > NFS_ACL_MAX_ENTRIES)
  273. goto out;
  274. if (dfacl != NULL && dfacl->a_count > NFS_ACL_MAX_ENTRIES)
  275. goto out;
  276. if (S_ISDIR(inode->i_mode)) {
  277. args.mask |= NFS_DFACL;
  278. args.acl_default = dfacl;
  279. args.len = nfsacl_size(acl, dfacl);
  280. } else
  281. args.len = nfsacl_size(acl, NULL);
  282. if (args.len > NFS_ACL_INLINE_BUFSIZE) {
  283. unsigned int npages = 1 + ((args.len - 1) >> PAGE_SHIFT);
  284. status = -ENOMEM;
  285. do {
  286. args.pages[args.npages] = alloc_page(GFP_KERNEL);
  287. if (args.pages[args.npages] == NULL)
  288. goto out_freepages;
  289. args.npages++;
  290. } while (args.npages < npages);
  291. }
  292. dprintk("NFS call setacl\n");
  293. msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_SETACL];
  294. nfs_fattr_init(&fattr);
  295. status = rpc_call_sync(server->client_acl, &msg, 0);
  296. nfs_access_zap_cache(inode);
  297. nfs_zap_acl_cache(inode);
  298. dprintk("NFS reply setacl: %d\n", status);
  299. switch (status) {
  300. case 0:
  301. status = nfs_refresh_inode(inode, &fattr);
  302. nfs3_cache_acls(inode, acl, dfacl);
  303. break;
  304. case -EPFNOSUPPORT:
  305. case -EPROTONOSUPPORT:
  306. dprintk("NFS_V3_ACL SETACL RPC not supported"
  307. "(will not retry)\n");
  308. server->caps &= ~NFS_CAP_ACLS;
  309. case -ENOTSUPP:
  310. status = -EOPNOTSUPP;
  311. }
  312. out_freepages:
  313. while (args.npages != 0) {
  314. args.npages--;
  315. __free_page(args.pages[args.npages]);
  316. }
  317. out:
  318. return status;
  319. }
  320. int nfs3_proc_setacl(struct inode *inode, int type, struct posix_acl *acl)
  321. {
  322. struct posix_acl *alloc = NULL, *dfacl = NULL;
  323. int status;
  324. if (S_ISDIR(inode->i_mode)) {
  325. switch(type) {
  326. case ACL_TYPE_ACCESS:
  327. alloc = dfacl = nfs3_proc_getacl(inode,
  328. ACL_TYPE_DEFAULT);
  329. if (IS_ERR(alloc))
  330. goto fail;
  331. break;
  332. case ACL_TYPE_DEFAULT:
  333. dfacl = acl;
  334. alloc = acl = nfs3_proc_getacl(inode,
  335. ACL_TYPE_ACCESS);
  336. if (IS_ERR(alloc))
  337. goto fail;
  338. break;
  339. default:
  340. return -EINVAL;
  341. }
  342. } else if (type != ACL_TYPE_ACCESS)
  343. return -EINVAL;
  344. if (acl == NULL) {
  345. alloc = acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
  346. if (IS_ERR(alloc))
  347. goto fail;
  348. }
  349. status = nfs3_proc_setacls(inode, acl, dfacl);
  350. posix_acl_release(alloc);
  351. return status;
  352. fail:
  353. return PTR_ERR(alloc);
  354. }
  355. int nfs3_proc_set_default_acl(struct inode *dir, struct inode *inode,
  356. mode_t mode)
  357. {
  358. struct posix_acl *dfacl, *acl;
  359. int error = 0;
  360. dfacl = nfs3_proc_getacl(dir, ACL_TYPE_DEFAULT);
  361. if (IS_ERR(dfacl)) {
  362. error = PTR_ERR(dfacl);
  363. return (error == -EOPNOTSUPP) ? 0 : error;
  364. }
  365. if (!dfacl)
  366. return 0;
  367. acl = posix_acl_clone(dfacl, GFP_KERNEL);
  368. error = -ENOMEM;
  369. if (!acl)
  370. goto out_release_dfacl;
  371. error = posix_acl_create_masq(acl, &mode);
  372. if (error < 0)
  373. goto out_release_acl;
  374. error = nfs3_proc_setacls(inode, acl, S_ISDIR(inode->i_mode) ?
  375. dfacl : NULL);
  376. out_release_acl:
  377. posix_acl_release(acl);
  378. out_release_dfacl:
  379. posix_acl_release(dfacl);
  380. return error;
  381. }