delegation.c 11 KB

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