delegation.c 8.5 KB

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