nfs3acl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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(&init_user_ns, 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(&init_user_ns, 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 page *pages[NFSACL_MAXPAGES] = { };
  161. struct nfs3_getaclargs args = {
  162. .fh = NFS_FH(inode),
  163. /* The xdr layer may allocate pages here. */
  164. .pages = pages,
  165. };
  166. struct nfs3_getaclres res = {
  167. NULL,
  168. };
  169. struct rpc_message msg = {
  170. .rpc_argp = &args,
  171. .rpc_resp = &res,
  172. };
  173. struct posix_acl *acl;
  174. int status, count;
  175. if (!nfs_server_capable(inode, NFS_CAP_ACLS))
  176. return ERR_PTR(-EOPNOTSUPP);
  177. status = nfs_revalidate_inode(server, inode);
  178. if (status < 0)
  179. return ERR_PTR(status);
  180. acl = nfs3_get_cached_acl(inode, type);
  181. if (acl != ERR_PTR(-EAGAIN))
  182. return acl;
  183. acl = NULL;
  184. /*
  185. * Only get the access acl when explicitly requested: We don't
  186. * need it for access decisions, and only some applications use
  187. * it. Applications which request the access acl first are not
  188. * penalized from this optimization.
  189. */
  190. if (type == ACL_TYPE_ACCESS)
  191. args.mask |= NFS_ACLCNT|NFS_ACL;
  192. if (S_ISDIR(inode->i_mode))
  193. args.mask |= NFS_DFACLCNT|NFS_DFACL;
  194. if (args.mask == 0)
  195. return NULL;
  196. dprintk("NFS call getacl\n");
  197. msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_GETACL];
  198. res.fattr = nfs_alloc_fattr();
  199. if (res.fattr == NULL)
  200. return ERR_PTR(-ENOMEM);
  201. status = rpc_call_sync(server->client_acl, &msg, 0);
  202. dprintk("NFS reply getacl: %d\n", status);
  203. /* pages may have been allocated at the xdr layer. */
  204. for (count = 0; count < NFSACL_MAXPAGES && args.pages[count]; count++)
  205. __free_page(args.pages[count]);
  206. switch (status) {
  207. case 0:
  208. status = nfs_refresh_inode(inode, res.fattr);
  209. break;
  210. case -EPFNOSUPPORT:
  211. case -EPROTONOSUPPORT:
  212. dprintk("NFS_V3_ACL extension not supported; disabling\n");
  213. server->caps &= ~NFS_CAP_ACLS;
  214. case -ENOTSUPP:
  215. status = -EOPNOTSUPP;
  216. default:
  217. goto getout;
  218. }
  219. if ((args.mask & res.mask) != args.mask) {
  220. status = -EIO;
  221. goto getout;
  222. }
  223. if (res.acl_access != NULL) {
  224. if (posix_acl_equiv_mode(res.acl_access, NULL) == 0) {
  225. posix_acl_release(res.acl_access);
  226. res.acl_access = NULL;
  227. }
  228. }
  229. nfs3_cache_acls(inode,
  230. (res.mask & NFS_ACL) ? res.acl_access : ERR_PTR(-EINVAL),
  231. (res.mask & NFS_DFACL) ? res.acl_default : ERR_PTR(-EINVAL));
  232. switch(type) {
  233. case ACL_TYPE_ACCESS:
  234. acl = res.acl_access;
  235. res.acl_access = NULL;
  236. break;
  237. case ACL_TYPE_DEFAULT:
  238. acl = res.acl_default;
  239. res.acl_default = NULL;
  240. }
  241. getout:
  242. posix_acl_release(res.acl_access);
  243. posix_acl_release(res.acl_default);
  244. nfs_free_fattr(res.fattr);
  245. if (status != 0) {
  246. posix_acl_release(acl);
  247. acl = ERR_PTR(status);
  248. }
  249. return acl;
  250. }
  251. static int nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
  252. struct posix_acl *dfacl)
  253. {
  254. struct nfs_server *server = NFS_SERVER(inode);
  255. struct nfs_fattr *fattr;
  256. struct page *pages[NFSACL_MAXPAGES];
  257. struct nfs3_setaclargs args = {
  258. .inode = inode,
  259. .mask = NFS_ACL,
  260. .acl_access = acl,
  261. .pages = pages,
  262. };
  263. struct rpc_message msg = {
  264. .rpc_argp = &args,
  265. .rpc_resp = &fattr,
  266. };
  267. int status;
  268. status = -EOPNOTSUPP;
  269. if (!nfs_server_capable(inode, NFS_CAP_ACLS))
  270. goto out;
  271. /* We are doing this here because XDR marshalling does not
  272. * return any results, it BUGs. */
  273. status = -ENOSPC;
  274. if (acl != NULL && acl->a_count > NFS_ACL_MAX_ENTRIES)
  275. goto out;
  276. if (dfacl != NULL && dfacl->a_count > NFS_ACL_MAX_ENTRIES)
  277. goto out;
  278. if (S_ISDIR(inode->i_mode)) {
  279. args.mask |= NFS_DFACL;
  280. args.acl_default = dfacl;
  281. args.len = nfsacl_size(acl, dfacl);
  282. } else
  283. args.len = nfsacl_size(acl, NULL);
  284. if (args.len > NFS_ACL_INLINE_BUFSIZE) {
  285. unsigned int npages = 1 + ((args.len - 1) >> PAGE_SHIFT);
  286. status = -ENOMEM;
  287. do {
  288. args.pages[args.npages] = alloc_page(GFP_KERNEL);
  289. if (args.pages[args.npages] == NULL)
  290. goto out_freepages;
  291. args.npages++;
  292. } while (args.npages < npages);
  293. }
  294. dprintk("NFS call setacl\n");
  295. status = -ENOMEM;
  296. fattr = nfs_alloc_fattr();
  297. if (fattr == NULL)
  298. goto out_freepages;
  299. msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_SETACL];
  300. msg.rpc_resp = fattr;
  301. status = rpc_call_sync(server->client_acl, &msg, 0);
  302. nfs_access_zap_cache(inode);
  303. nfs_zap_acl_cache(inode);
  304. dprintk("NFS reply setacl: %d\n", status);
  305. switch (status) {
  306. case 0:
  307. status = nfs_refresh_inode(inode, fattr);
  308. nfs3_cache_acls(inode, acl, dfacl);
  309. break;
  310. case -EPFNOSUPPORT:
  311. case -EPROTONOSUPPORT:
  312. dprintk("NFS_V3_ACL SETACL RPC not supported"
  313. "(will not retry)\n");
  314. server->caps &= ~NFS_CAP_ACLS;
  315. case -ENOTSUPP:
  316. status = -EOPNOTSUPP;
  317. }
  318. nfs_free_fattr(fattr);
  319. out_freepages:
  320. while (args.npages != 0) {
  321. args.npages--;
  322. __free_page(args.pages[args.npages]);
  323. }
  324. out:
  325. return status;
  326. }
  327. int nfs3_proc_setacl(struct inode *inode, int type, struct posix_acl *acl)
  328. {
  329. struct posix_acl *alloc = NULL, *dfacl = NULL;
  330. int status;
  331. if (S_ISDIR(inode->i_mode)) {
  332. switch(type) {
  333. case ACL_TYPE_ACCESS:
  334. alloc = dfacl = nfs3_proc_getacl(inode,
  335. ACL_TYPE_DEFAULT);
  336. if (IS_ERR(alloc))
  337. goto fail;
  338. break;
  339. case ACL_TYPE_DEFAULT:
  340. dfacl = acl;
  341. alloc = acl = nfs3_proc_getacl(inode,
  342. ACL_TYPE_ACCESS);
  343. if (IS_ERR(alloc))
  344. goto fail;
  345. break;
  346. default:
  347. return -EINVAL;
  348. }
  349. } else if (type != ACL_TYPE_ACCESS)
  350. return -EINVAL;
  351. if (acl == NULL) {
  352. alloc = acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
  353. if (IS_ERR(alloc))
  354. goto fail;
  355. }
  356. status = nfs3_proc_setacls(inode, acl, dfacl);
  357. posix_acl_release(alloc);
  358. return status;
  359. fail:
  360. return PTR_ERR(alloc);
  361. }
  362. int nfs3_proc_set_default_acl(struct inode *dir, struct inode *inode,
  363. umode_t mode)
  364. {
  365. struct posix_acl *dfacl, *acl;
  366. int error = 0;
  367. dfacl = nfs3_proc_getacl(dir, ACL_TYPE_DEFAULT);
  368. if (IS_ERR(dfacl)) {
  369. error = PTR_ERR(dfacl);
  370. return (error == -EOPNOTSUPP) ? 0 : error;
  371. }
  372. if (!dfacl)
  373. return 0;
  374. acl = posix_acl_dup(dfacl);
  375. error = __posix_acl_create(&acl, GFP_KERNEL, &mode);
  376. if (error < 0)
  377. goto out_release_dfacl;
  378. error = nfs3_proc_setacls(inode, acl, S_ISDIR(inode->i_mode) ?
  379. dfacl : NULL);
  380. posix_acl_release(acl);
  381. out_release_dfacl:
  382. posix_acl_release(dfacl);
  383. return error;
  384. }