nfs3acl.c 10 KB

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