delegation.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * linux/fs/nfs/delegation.c
  3. *
  4. * Copyright (C) 2004 Trond Myklebust
  5. *
  6. * NFS file delegation management
  7. *
  8. */
  9. #include <linux/config.h>
  10. #include <linux/completion.h>
  11. #include <linux/module.h>
  12. #include <linux/sched.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/nfs4.h>
  15. #include <linux/nfs_fs.h>
  16. #include <linux/nfs_xdr.h>
  17. #include "nfs4_fs.h"
  18. #include "delegation.h"
  19. static struct nfs_delegation *nfs_alloc_delegation(void)
  20. {
  21. return (struct nfs_delegation *)kmalloc(sizeof(struct nfs_delegation), GFP_KERNEL);
  22. }
  23. static void nfs_free_delegation(struct nfs_delegation *delegation)
  24. {
  25. if (delegation->cred)
  26. put_rpccred(delegation->cred);
  27. kfree(delegation);
  28. }
  29. static void nfs_delegation_claim_opens(struct inode *inode)
  30. {
  31. struct nfs_inode *nfsi = NFS_I(inode);
  32. struct nfs_open_context *ctx;
  33. struct nfs4_state *state;
  34. again:
  35. spin_lock(&inode->i_lock);
  36. list_for_each_entry(ctx, &nfsi->open_files, list) {
  37. state = ctx->state;
  38. if (state == NULL)
  39. continue;
  40. if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
  41. continue;
  42. get_nfs_open_context(ctx);
  43. spin_unlock(&inode->i_lock);
  44. if (nfs4_open_delegation_recall(ctx->dentry, state) < 0)
  45. return;
  46. put_nfs_open_context(ctx);
  47. goto again;
  48. }
  49. spin_unlock(&inode->i_lock);
  50. }
  51. /*
  52. * Set up a delegation on an inode
  53. */
  54. void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
  55. {
  56. struct nfs_delegation *delegation = NFS_I(inode)->delegation;
  57. if (delegation == NULL)
  58. return;
  59. memcpy(delegation->stateid.data, res->delegation.data,
  60. sizeof(delegation->stateid.data));
  61. delegation->type = res->delegation_type;
  62. delegation->maxsize = res->maxsize;
  63. put_rpccred(cred);
  64. delegation->cred = get_rpccred(cred);
  65. delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM;
  66. NFS_I(inode)->delegation_state = delegation->type;
  67. smp_wmb();
  68. }
  69. /*
  70. * Set up a delegation on an inode
  71. */
  72. int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
  73. {
  74. struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
  75. struct nfs_inode *nfsi = NFS_I(inode);
  76. struct nfs_delegation *delegation;
  77. int status = 0;
  78. /* Ensure we first revalidate the attributes and page cache! */
  79. if ((nfsi->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_ATTR)))
  80. __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  81. delegation = nfs_alloc_delegation();
  82. if (delegation == NULL)
  83. return -ENOMEM;
  84. memcpy(delegation->stateid.data, res->delegation.data,
  85. sizeof(delegation->stateid.data));
  86. delegation->type = res->delegation_type;
  87. delegation->maxsize = res->maxsize;
  88. delegation->cred = get_rpccred(cred);
  89. delegation->inode = inode;
  90. spin_lock(&clp->cl_lock);
  91. if (nfsi->delegation == NULL) {
  92. list_add(&delegation->super_list, &clp->cl_delegations);
  93. nfsi->delegation = delegation;
  94. nfsi->delegation_state = delegation->type;
  95. delegation = NULL;
  96. } else {
  97. if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
  98. sizeof(delegation->stateid)) != 0 ||
  99. delegation->type != nfsi->delegation->type) {
  100. printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n",
  101. __FUNCTION__, NIPQUAD(clp->cl_addr));
  102. status = -EIO;
  103. }
  104. }
  105. spin_unlock(&clp->cl_lock);
  106. if (delegation != NULL)
  107. kfree(delegation);
  108. return status;
  109. }
  110. static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
  111. {
  112. int res = 0;
  113. __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  114. res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid);
  115. nfs_free_delegation(delegation);
  116. return res;
  117. }
  118. /* Sync all data to disk upon delegation return */
  119. static void nfs_msync_inode(struct inode *inode)
  120. {
  121. filemap_fdatawrite(inode->i_mapping);
  122. nfs_wb_all(inode);
  123. filemap_fdatawait(inode->i_mapping);
  124. }
  125. /*
  126. * Basic procedure for returning a delegation to the server
  127. */
  128. int __nfs_inode_return_delegation(struct inode *inode)
  129. {
  130. struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
  131. struct nfs_inode *nfsi = NFS_I(inode);
  132. struct nfs_delegation *delegation;
  133. int res = 0;
  134. nfs_msync_inode(inode);
  135. down_read(&clp->cl_sem);
  136. /* Guard against new delegated open calls */
  137. down_write(&nfsi->rwsem);
  138. spin_lock(&clp->cl_lock);
  139. delegation = nfsi->delegation;
  140. if (delegation != NULL) {
  141. list_del_init(&delegation->super_list);
  142. nfsi->delegation = NULL;
  143. nfsi->delegation_state = 0;
  144. }
  145. spin_unlock(&clp->cl_lock);
  146. nfs_delegation_claim_opens(inode);
  147. up_write(&nfsi->rwsem);
  148. up_read(&clp->cl_sem);
  149. nfs_msync_inode(inode);
  150. if (delegation != NULL)
  151. res = nfs_do_return_delegation(inode, delegation);
  152. return res;
  153. }
  154. /*
  155. * Return all delegations associated to a super block
  156. */
  157. void nfs_return_all_delegations(struct super_block *sb)
  158. {
  159. struct nfs4_client *clp = NFS_SB(sb)->nfs4_state;
  160. struct nfs_delegation *delegation;
  161. struct inode *inode;
  162. if (clp == NULL)
  163. return;
  164. restart:
  165. spin_lock(&clp->cl_lock);
  166. list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
  167. if (delegation->inode->i_sb != sb)
  168. continue;
  169. inode = igrab(delegation->inode);
  170. if (inode == NULL)
  171. continue;
  172. spin_unlock(&clp->cl_lock);
  173. nfs_inode_return_delegation(inode);
  174. iput(inode);
  175. goto restart;
  176. }
  177. spin_unlock(&clp->cl_lock);
  178. }
  179. /*
  180. * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
  181. */
  182. void nfs_handle_cb_pathdown(struct nfs4_client *clp)
  183. {
  184. struct nfs_delegation *delegation;
  185. struct inode *inode;
  186. if (clp == NULL)
  187. return;
  188. restart:
  189. spin_lock(&clp->cl_lock);
  190. list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
  191. inode = igrab(delegation->inode);
  192. if (inode == NULL)
  193. continue;
  194. spin_unlock(&clp->cl_lock);
  195. nfs_inode_return_delegation(inode);
  196. iput(inode);
  197. goto restart;
  198. }
  199. spin_unlock(&clp->cl_lock);
  200. }
  201. struct recall_threadargs {
  202. struct inode *inode;
  203. struct nfs4_client *clp;
  204. const nfs4_stateid *stateid;
  205. struct completion started;
  206. int result;
  207. };
  208. static int recall_thread(void *data)
  209. {
  210. struct recall_threadargs *args = (struct recall_threadargs *)data;
  211. struct inode *inode = igrab(args->inode);
  212. struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
  213. struct nfs_inode *nfsi = NFS_I(inode);
  214. struct nfs_delegation *delegation;
  215. daemonize("nfsv4-delegreturn");
  216. nfs_msync_inode(inode);
  217. down_read(&clp->cl_sem);
  218. down_write(&nfsi->rwsem);
  219. spin_lock(&clp->cl_lock);
  220. delegation = nfsi->delegation;
  221. if (delegation != NULL && memcmp(delegation->stateid.data,
  222. args->stateid->data,
  223. sizeof(delegation->stateid.data)) == 0) {
  224. list_del_init(&delegation->super_list);
  225. nfsi->delegation = NULL;
  226. nfsi->delegation_state = 0;
  227. args->result = 0;
  228. } else {
  229. delegation = NULL;
  230. args->result = -ENOENT;
  231. }
  232. spin_unlock(&clp->cl_lock);
  233. complete(&args->started);
  234. nfs_delegation_claim_opens(inode);
  235. up_write(&nfsi->rwsem);
  236. up_read(&clp->cl_sem);
  237. nfs_msync_inode(inode);
  238. if (delegation != NULL)
  239. nfs_do_return_delegation(inode, delegation);
  240. iput(inode);
  241. module_put_and_exit(0);
  242. }
  243. /*
  244. * Asynchronous delegation recall!
  245. */
  246. int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
  247. {
  248. struct recall_threadargs data = {
  249. .inode = inode,
  250. .stateid = stateid,
  251. };
  252. int status;
  253. init_completion(&data.started);
  254. __module_get(THIS_MODULE);
  255. status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
  256. if (status < 0)
  257. goto out_module_put;
  258. wait_for_completion(&data.started);
  259. return data.result;
  260. out_module_put:
  261. module_put(THIS_MODULE);
  262. return status;
  263. }
  264. /*
  265. * Retrieve the inode associated with a delegation
  266. */
  267. struct inode *nfs_delegation_find_inode(struct nfs4_client *clp, const struct nfs_fh *fhandle)
  268. {
  269. struct nfs_delegation *delegation;
  270. struct inode *res = NULL;
  271. spin_lock(&clp->cl_lock);
  272. list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
  273. if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
  274. res = igrab(delegation->inode);
  275. break;
  276. }
  277. }
  278. spin_unlock(&clp->cl_lock);
  279. return res;
  280. }
  281. /*
  282. * Mark all delegations as needing to be reclaimed
  283. */
  284. void nfs_delegation_mark_reclaim(struct nfs4_client *clp)
  285. {
  286. struct nfs_delegation *delegation;
  287. spin_lock(&clp->cl_lock);
  288. list_for_each_entry(delegation, &clp->cl_delegations, super_list)
  289. delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
  290. spin_unlock(&clp->cl_lock);
  291. }
  292. /*
  293. * Reap all unclaimed delegations after reboot recovery is done
  294. */
  295. void nfs_delegation_reap_unclaimed(struct nfs4_client *clp)
  296. {
  297. struct nfs_delegation *delegation, *n;
  298. LIST_HEAD(head);
  299. spin_lock(&clp->cl_lock);
  300. list_for_each_entry_safe(delegation, n, &clp->cl_delegations, super_list) {
  301. if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
  302. continue;
  303. list_move(&delegation->super_list, &head);
  304. NFS_I(delegation->inode)->delegation = NULL;
  305. NFS_I(delegation->inode)->delegation_state = 0;
  306. }
  307. spin_unlock(&clp->cl_lock);
  308. while(!list_empty(&head)) {
  309. delegation = list_entry(head.next, struct nfs_delegation, super_list);
  310. list_del(&delegation->super_list);
  311. nfs_free_delegation(delegation);
  312. }
  313. }