delegation.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. delegation = nfs_alloc_delegation();
  79. if (delegation == NULL)
  80. return -ENOMEM;
  81. memcpy(delegation->stateid.data, res->delegation.data,
  82. sizeof(delegation->stateid.data));
  83. delegation->type = res->delegation_type;
  84. delegation->maxsize = res->maxsize;
  85. delegation->cred = get_rpccred(cred);
  86. delegation->inode = inode;
  87. spin_lock(&clp->cl_lock);
  88. if (nfsi->delegation == NULL) {
  89. list_add(&delegation->super_list, &clp->cl_delegations);
  90. nfsi->delegation = delegation;
  91. nfsi->delegation_state = delegation->type;
  92. delegation = NULL;
  93. } else {
  94. if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
  95. sizeof(delegation->stateid)) != 0 ||
  96. delegation->type != nfsi->delegation->type) {
  97. printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n",
  98. __FUNCTION__, NIPQUAD(clp->cl_addr));
  99. status = -EIO;
  100. }
  101. }
  102. spin_unlock(&clp->cl_lock);
  103. if (delegation != NULL)
  104. kfree(delegation);
  105. return status;
  106. }
  107. static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
  108. {
  109. int res = 0;
  110. __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  111. res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid);
  112. nfs_free_delegation(delegation);
  113. return res;
  114. }
  115. /* Sync all data to disk upon delegation return */
  116. static void nfs_msync_inode(struct inode *inode)
  117. {
  118. filemap_fdatawrite(inode->i_mapping);
  119. nfs_wb_all(inode);
  120. filemap_fdatawait(inode->i_mapping);
  121. }
  122. /*
  123. * Basic procedure for returning a delegation to the server
  124. */
  125. int nfs_inode_return_delegation(struct inode *inode)
  126. {
  127. struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
  128. struct nfs_inode *nfsi = NFS_I(inode);
  129. struct nfs_delegation *delegation;
  130. int res = 0;
  131. nfs_msync_inode(inode);
  132. down_read(&clp->cl_sem);
  133. /* Guard against new delegated open calls */
  134. down_write(&nfsi->rwsem);
  135. spin_lock(&clp->cl_lock);
  136. delegation = nfsi->delegation;
  137. if (delegation != NULL) {
  138. list_del_init(&delegation->super_list);
  139. nfsi->delegation = NULL;
  140. nfsi->delegation_state = 0;
  141. }
  142. spin_unlock(&clp->cl_lock);
  143. nfs_delegation_claim_opens(inode);
  144. up_write(&nfsi->rwsem);
  145. up_read(&clp->cl_sem);
  146. nfs_msync_inode(inode);
  147. if (delegation != NULL)
  148. res = nfs_do_return_delegation(inode, delegation);
  149. return res;
  150. }
  151. /*
  152. * Return all delegations associated to a super block
  153. */
  154. void nfs_return_all_delegations(struct super_block *sb)
  155. {
  156. struct nfs4_client *clp = NFS_SB(sb)->nfs4_state;
  157. struct nfs_delegation *delegation;
  158. struct inode *inode;
  159. if (clp == NULL)
  160. return;
  161. restart:
  162. spin_lock(&clp->cl_lock);
  163. list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
  164. if (delegation->inode->i_sb != sb)
  165. continue;
  166. inode = igrab(delegation->inode);
  167. if (inode == NULL)
  168. continue;
  169. spin_unlock(&clp->cl_lock);
  170. nfs_inode_return_delegation(inode);
  171. iput(inode);
  172. goto restart;
  173. }
  174. spin_unlock(&clp->cl_lock);
  175. }
  176. /*
  177. * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
  178. */
  179. void nfs_handle_cb_pathdown(struct nfs4_client *clp)
  180. {
  181. struct nfs_delegation *delegation;
  182. struct inode *inode;
  183. if (clp == NULL)
  184. return;
  185. restart:
  186. spin_lock(&clp->cl_lock);
  187. list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
  188. inode = igrab(delegation->inode);
  189. if (inode == NULL)
  190. continue;
  191. spin_unlock(&clp->cl_lock);
  192. nfs_inode_return_delegation(inode);
  193. iput(inode);
  194. goto restart;
  195. }
  196. spin_unlock(&clp->cl_lock);
  197. }
  198. struct recall_threadargs {
  199. struct inode *inode;
  200. struct nfs4_client *clp;
  201. const nfs4_stateid *stateid;
  202. struct completion started;
  203. int result;
  204. };
  205. static int recall_thread(void *data)
  206. {
  207. struct recall_threadargs *args = (struct recall_threadargs *)data;
  208. struct inode *inode = igrab(args->inode);
  209. struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
  210. struct nfs_inode *nfsi = NFS_I(inode);
  211. struct nfs_delegation *delegation;
  212. daemonize("nfsv4-delegreturn");
  213. nfs_msync_inode(inode);
  214. down_read(&clp->cl_sem);
  215. down_write(&nfsi->rwsem);
  216. spin_lock(&clp->cl_lock);
  217. delegation = nfsi->delegation;
  218. if (delegation != NULL && memcmp(delegation->stateid.data,
  219. args->stateid->data,
  220. sizeof(delegation->stateid.data)) == 0) {
  221. list_del_init(&delegation->super_list);
  222. nfsi->delegation = NULL;
  223. nfsi->delegation_state = 0;
  224. args->result = 0;
  225. } else {
  226. delegation = NULL;
  227. args->result = -ENOENT;
  228. }
  229. spin_unlock(&clp->cl_lock);
  230. complete(&args->started);
  231. nfs_delegation_claim_opens(inode);
  232. up_write(&nfsi->rwsem);
  233. up_read(&clp->cl_sem);
  234. nfs_msync_inode(inode);
  235. if (delegation != NULL)
  236. nfs_do_return_delegation(inode, delegation);
  237. iput(inode);
  238. module_put_and_exit(0);
  239. }
  240. /*
  241. * Asynchronous delegation recall!
  242. */
  243. int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
  244. {
  245. struct recall_threadargs data = {
  246. .inode = inode,
  247. .stateid = stateid,
  248. };
  249. int status;
  250. init_completion(&data.started);
  251. __module_get(THIS_MODULE);
  252. status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
  253. if (status < 0)
  254. goto out_module_put;
  255. wait_for_completion(&data.started);
  256. return data.result;
  257. out_module_put:
  258. module_put(THIS_MODULE);
  259. return status;
  260. }
  261. /*
  262. * Retrieve the inode associated with a delegation
  263. */
  264. struct inode *nfs_delegation_find_inode(struct nfs4_client *clp, const struct nfs_fh *fhandle)
  265. {
  266. struct nfs_delegation *delegation;
  267. struct inode *res = NULL;
  268. spin_lock(&clp->cl_lock);
  269. list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
  270. if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
  271. res = igrab(delegation->inode);
  272. break;
  273. }
  274. }
  275. spin_unlock(&clp->cl_lock);
  276. return res;
  277. }
  278. /*
  279. * Mark all delegations as needing to be reclaimed
  280. */
  281. void nfs_delegation_mark_reclaim(struct nfs4_client *clp)
  282. {
  283. struct nfs_delegation *delegation;
  284. spin_lock(&clp->cl_lock);
  285. list_for_each_entry(delegation, &clp->cl_delegations, super_list)
  286. delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
  287. spin_unlock(&clp->cl_lock);
  288. }
  289. /*
  290. * Reap all unclaimed delegations after reboot recovery is done
  291. */
  292. void nfs_delegation_reap_unclaimed(struct nfs4_client *clp)
  293. {
  294. struct nfs_delegation *delegation, *n;
  295. LIST_HEAD(head);
  296. spin_lock(&clp->cl_lock);
  297. list_for_each_entry_safe(delegation, n, &clp->cl_delegations, super_list) {
  298. if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
  299. continue;
  300. list_move(&delegation->super_list, &head);
  301. NFS_I(delegation->inode)->delegation = NULL;
  302. NFS_I(delegation->inode)->delegation_state = 0;
  303. }
  304. spin_unlock(&clp->cl_lock);
  305. while(!list_empty(&head)) {
  306. delegation = list_entry(head.next, struct nfs_delegation, super_list);
  307. list_del(&delegation->super_list);
  308. nfs_free_delegation(delegation);
  309. }
  310. }