delegation.c 11 KB

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