nfs3acl.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
  181. nfs_zap_acl_cache(inode);
  182. acl = nfs3_get_cached_acl(inode, type);
  183. if (acl != ERR_PTR(-EAGAIN))
  184. return acl;
  185. acl = NULL;
  186. /*
  187. * Only get the access acl when explicitly requested: We don't
  188. * need it for access decisions, and only some applications use
  189. * it. Applications which request the access acl first are not
  190. * penalized from this optimization.
  191. */
  192. if (type == ACL_TYPE_ACCESS)
  193. args.mask |= NFS_ACLCNT|NFS_ACL;
  194. if (S_ISDIR(inode->i_mode))
  195. args.mask |= NFS_DFACLCNT|NFS_DFACL;
  196. if (args.mask == 0)
  197. return NULL;
  198. dprintk("NFS call getacl\n");
  199. msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_GETACL];
  200. nfs_fattr_init(&fattr);
  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, &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. if (status != 0) {
  245. posix_acl_release(acl);
  246. acl = ERR_PTR(status);
  247. }
  248. return acl;
  249. }
  250. static int nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
  251. struct posix_acl *dfacl)
  252. {
  253. struct nfs_server *server = NFS_SERVER(inode);
  254. struct nfs_fattr fattr;
  255. struct page *pages[NFSACL_MAXPAGES] = { };
  256. struct nfs3_setaclargs args = {
  257. .inode = inode,
  258. .mask = NFS_ACL,
  259. .acl_access = acl,
  260. .pages = pages,
  261. };
  262. struct rpc_message msg = {
  263. .rpc_argp = &args,
  264. .rpc_resp = &fattr,
  265. };
  266. int status, count;
  267. status = -EOPNOTSUPP;
  268. if (!nfs_server_capable(inode, NFS_CAP_ACLS))
  269. goto out;
  270. /* We are doing this here, because XDR marshalling can only
  271. return -ENOMEM. */
  272. status = -ENOSPC;
  273. if (acl != NULL && acl->a_count > NFS_ACL_MAX_ENTRIES)
  274. goto out;
  275. if (dfacl != NULL && dfacl->a_count > NFS_ACL_MAX_ENTRIES)
  276. goto out;
  277. if (S_ISDIR(inode->i_mode)) {
  278. args.mask |= NFS_DFACL;
  279. args.acl_default = dfacl;
  280. }
  281. dprintk("NFS call setacl\n");
  282. msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_SETACL];
  283. nfs_fattr_init(&fattr);
  284. status = rpc_call_sync(server->client_acl, &msg, 0);
  285. nfs_access_zap_cache(inode);
  286. nfs_zap_acl_cache(inode);
  287. dprintk("NFS reply setacl: %d\n", status);
  288. /* pages may have been allocated at the xdr layer. */
  289. for (count = 0; count < NFSACL_MAXPAGES && args.pages[count]; count++)
  290. __free_page(args.pages[count]);
  291. switch (status) {
  292. case 0:
  293. status = nfs_refresh_inode(inode, &fattr);
  294. nfs3_cache_acls(inode, acl, dfacl);
  295. break;
  296. case -EPFNOSUPPORT:
  297. case -EPROTONOSUPPORT:
  298. dprintk("NFS_V3_ACL SETACL RPC not supported"
  299. "(will not retry)\n");
  300. server->caps &= ~NFS_CAP_ACLS;
  301. case -ENOTSUPP:
  302. status = -EOPNOTSUPP;
  303. }
  304. out:
  305. return status;
  306. }
  307. int nfs3_proc_setacl(struct inode *inode, int type, struct posix_acl *acl)
  308. {
  309. struct posix_acl *alloc = NULL, *dfacl = NULL;
  310. int status;
  311. if (S_ISDIR(inode->i_mode)) {
  312. switch(type) {
  313. case ACL_TYPE_ACCESS:
  314. alloc = dfacl = nfs3_proc_getacl(inode,
  315. ACL_TYPE_DEFAULT);
  316. if (IS_ERR(alloc))
  317. goto fail;
  318. break;
  319. case ACL_TYPE_DEFAULT:
  320. dfacl = acl;
  321. alloc = acl = nfs3_proc_getacl(inode,
  322. ACL_TYPE_ACCESS);
  323. if (IS_ERR(alloc))
  324. goto fail;
  325. break;
  326. default:
  327. return -EINVAL;
  328. }
  329. } else if (type != ACL_TYPE_ACCESS)
  330. return -EINVAL;
  331. if (acl == NULL) {
  332. alloc = acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
  333. if (IS_ERR(alloc))
  334. goto fail;
  335. }
  336. status = nfs3_proc_setacls(inode, acl, dfacl);
  337. posix_acl_release(alloc);
  338. return status;
  339. fail:
  340. return PTR_ERR(alloc);
  341. }
  342. int nfs3_proc_set_default_acl(struct inode *dir, struct inode *inode,
  343. mode_t mode)
  344. {
  345. struct posix_acl *dfacl, *acl;
  346. int error = 0;
  347. dfacl = nfs3_proc_getacl(dir, ACL_TYPE_DEFAULT);
  348. if (IS_ERR(dfacl)) {
  349. error = PTR_ERR(dfacl);
  350. return (error == -EOPNOTSUPP) ? 0 : error;
  351. }
  352. if (!dfacl)
  353. return 0;
  354. acl = posix_acl_clone(dfacl, GFP_KERNEL);
  355. error = -ENOMEM;
  356. if (!acl)
  357. goto out_release_dfacl;
  358. error = posix_acl_create_masq(acl, &mode);
  359. if (error < 0)
  360. goto out_release_acl;
  361. error = nfs3_proc_setacls(inode, acl, S_ISDIR(inode->i_mode) ?
  362. dfacl : NULL);
  363. out_release_acl:
  364. posix_acl_release(acl);
  365. out_release_dfacl:
  366. posix_acl_release(dfacl);
  367. return error;
  368. }