delegation.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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->cred = get_rpccred(cred);
  122. delegation->inode = inode;
  123. spin_lock(&clp->cl_lock);
  124. if (nfsi->delegation == NULL) {
  125. list_add(&delegation->super_list, &clp->cl_delegations);
  126. nfsi->delegation = delegation;
  127. nfsi->delegation_state = delegation->type;
  128. delegation = NULL;
  129. } else {
  130. if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
  131. sizeof(delegation->stateid)) != 0 ||
  132. delegation->type != nfsi->delegation->type) {
  133. printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n",
  134. __FUNCTION__, NIPQUAD(clp->cl_addr));
  135. status = -EIO;
  136. }
  137. }
  138. spin_unlock(&clp->cl_lock);
  139. kfree(delegation);
  140. return status;
  141. }
  142. static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
  143. {
  144. int res = 0;
  145. __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  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. int err = 0;
  217. allow_signal(SIGKILL);
  218. restart:
  219. spin_lock(&clp->cl_lock);
  220. if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
  221. goto out;
  222. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
  223. goto out;
  224. list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
  225. inode = igrab(delegation->inode);
  226. if (inode == NULL)
  227. continue;
  228. spin_unlock(&clp->cl_lock);
  229. err = nfs_inode_return_delegation(inode);
  230. iput(inode);
  231. if (!err)
  232. goto restart;
  233. }
  234. out:
  235. spin_unlock(&clp->cl_lock);
  236. nfs4_put_client(clp);
  237. module_put_and_exit(0);
  238. }
  239. void nfs_expire_all_delegations(struct nfs4_client *clp)
  240. {
  241. struct task_struct *task;
  242. __module_get(THIS_MODULE);
  243. atomic_inc(&clp->cl_count);
  244. task = kthread_run(nfs_do_expire_all_delegations, clp,
  245. "%u.%u.%u.%u-delegreturn",
  246. NIPQUAD(clp->cl_addr));
  247. if (!IS_ERR(task))
  248. return;
  249. nfs4_put_client(clp);
  250. module_put(THIS_MODULE);
  251. }
  252. /*
  253. * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
  254. */
  255. void nfs_handle_cb_pathdown(struct nfs4_client *clp)
  256. {
  257. struct nfs_delegation *delegation;
  258. struct inode *inode;
  259. if (clp == NULL)
  260. return;
  261. restart:
  262. spin_lock(&clp->cl_lock);
  263. list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
  264. inode = igrab(delegation->inode);
  265. if (inode == NULL)
  266. continue;
  267. spin_unlock(&clp->cl_lock);
  268. nfs_inode_return_delegation(inode);
  269. iput(inode);
  270. goto restart;
  271. }
  272. spin_unlock(&clp->cl_lock);
  273. }
  274. struct recall_threadargs {
  275. struct inode *inode;
  276. struct nfs4_client *clp;
  277. const nfs4_stateid *stateid;
  278. struct completion started;
  279. int result;
  280. };
  281. static int recall_thread(void *data)
  282. {
  283. struct recall_threadargs *args = (struct recall_threadargs *)data;
  284. struct inode *inode = igrab(args->inode);
  285. struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
  286. struct nfs_inode *nfsi = NFS_I(inode);
  287. struct nfs_delegation *delegation;
  288. daemonize("nfsv4-delegreturn");
  289. nfs_msync_inode(inode);
  290. down_read(&clp->cl_sem);
  291. down_write(&nfsi->rwsem);
  292. spin_lock(&clp->cl_lock);
  293. delegation = nfsi->delegation;
  294. if (delegation != NULL && memcmp(delegation->stateid.data,
  295. args->stateid->data,
  296. sizeof(delegation->stateid.data)) == 0) {
  297. list_del_init(&delegation->super_list);
  298. nfsi->delegation = NULL;
  299. nfsi->delegation_state = 0;
  300. args->result = 0;
  301. } else {
  302. delegation = NULL;
  303. args->result = -ENOENT;
  304. }
  305. spin_unlock(&clp->cl_lock);
  306. complete(&args->started);
  307. nfs_delegation_claim_opens(inode);
  308. up_write(&nfsi->rwsem);
  309. up_read(&clp->cl_sem);
  310. nfs_msync_inode(inode);
  311. if (delegation != NULL)
  312. nfs_do_return_delegation(inode, delegation);
  313. iput(inode);
  314. module_put_and_exit(0);
  315. }
  316. /*
  317. * Asynchronous delegation recall!
  318. */
  319. int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
  320. {
  321. struct recall_threadargs data = {
  322. .inode = inode,
  323. .stateid = stateid,
  324. };
  325. int status;
  326. init_completion(&data.started);
  327. __module_get(THIS_MODULE);
  328. status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
  329. if (status < 0)
  330. goto out_module_put;
  331. wait_for_completion(&data.started);
  332. return data.result;
  333. out_module_put:
  334. module_put(THIS_MODULE);
  335. return status;
  336. }
  337. /*
  338. * Retrieve the inode associated with a delegation
  339. */
  340. struct inode *nfs_delegation_find_inode(struct nfs4_client *clp, const struct nfs_fh *fhandle)
  341. {
  342. struct nfs_delegation *delegation;
  343. struct inode *res = NULL;
  344. spin_lock(&clp->cl_lock);
  345. list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
  346. if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
  347. res = igrab(delegation->inode);
  348. break;
  349. }
  350. }
  351. spin_unlock(&clp->cl_lock);
  352. return res;
  353. }
  354. /*
  355. * Mark all delegations as needing to be reclaimed
  356. */
  357. void nfs_delegation_mark_reclaim(struct nfs4_client *clp)
  358. {
  359. struct nfs_delegation *delegation;
  360. spin_lock(&clp->cl_lock);
  361. list_for_each_entry(delegation, &clp->cl_delegations, super_list)
  362. delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
  363. spin_unlock(&clp->cl_lock);
  364. }
  365. /*
  366. * Reap all unclaimed delegations after reboot recovery is done
  367. */
  368. void nfs_delegation_reap_unclaimed(struct nfs4_client *clp)
  369. {
  370. struct nfs_delegation *delegation, *n;
  371. LIST_HEAD(head);
  372. spin_lock(&clp->cl_lock);
  373. list_for_each_entry_safe(delegation, n, &clp->cl_delegations, super_list) {
  374. if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
  375. continue;
  376. list_move(&delegation->super_list, &head);
  377. NFS_I(delegation->inode)->delegation = NULL;
  378. NFS_I(delegation->inode)->delegation_state = 0;
  379. }
  380. spin_unlock(&clp->cl_lock);
  381. while(!list_empty(&head)) {
  382. delegation = list_entry(head.next, struct nfs_delegation, super_list);
  383. list_del(&delegation->super_list);
  384. nfs_free_delegation(delegation);
  385. }
  386. }