nfs3acl.c 9.7 KB

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