delegation.c 9.6 KB

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