delegation.c 11 KB

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