nfs3acl.c 9.3 KB

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