delegation.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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, const nfs4_stateid *stateid)
  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. if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
  69. continue;
  70. get_nfs_open_context(ctx);
  71. spin_unlock(&inode->i_lock);
  72. err = nfs4_open_delegation_recall(ctx, state, stateid);
  73. if (err >= 0)
  74. err = nfs_delegation_claim_locks(ctx, state);
  75. put_nfs_open_context(ctx);
  76. if (err != 0)
  77. return;
  78. goto again;
  79. }
  80. spin_unlock(&inode->i_lock);
  81. }
  82. /*
  83. * Set up a delegation on an inode
  84. */
  85. void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
  86. {
  87. struct nfs_delegation *delegation = NFS_I(inode)->delegation;
  88. if (delegation == NULL)
  89. return;
  90. memcpy(delegation->stateid.data, res->delegation.data,
  91. sizeof(delegation->stateid.data));
  92. delegation->type = res->delegation_type;
  93. delegation->maxsize = res->maxsize;
  94. put_rpccred(cred);
  95. delegation->cred = get_rpccred(cred);
  96. delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM;
  97. NFS_I(inode)->delegation_state = delegation->type;
  98. smp_wmb();
  99. }
  100. /*
  101. * Set up a delegation on an inode
  102. */
  103. int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
  104. {
  105. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  106. struct nfs_inode *nfsi = NFS_I(inode);
  107. struct nfs_delegation *delegation;
  108. int status = 0;
  109. /* Ensure we first revalidate the attributes and page cache! */
  110. if ((nfsi->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_ATTR)))
  111. __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  112. delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
  113. if (delegation == NULL)
  114. return -ENOMEM;
  115. memcpy(delegation->stateid.data, res->delegation.data,
  116. sizeof(delegation->stateid.data));
  117. delegation->type = res->delegation_type;
  118. delegation->maxsize = res->maxsize;
  119. delegation->change_attr = nfsi->change_attr;
  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.sin_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. res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid);
  145. nfs_free_delegation(delegation);
  146. return res;
  147. }
  148. /* Sync all data to disk upon delegation return */
  149. static void nfs_msync_inode(struct inode *inode)
  150. {
  151. filemap_fdatawrite(inode->i_mapping);
  152. nfs_wb_all(inode);
  153. filemap_fdatawait(inode->i_mapping);
  154. }
  155. /*
  156. * Basic procedure for returning a delegation to the server
  157. */
  158. static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
  159. {
  160. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  161. struct nfs_inode *nfsi = NFS_I(inode);
  162. nfs_msync_inode(inode);
  163. down_read(&clp->cl_sem);
  164. /* Guard against new delegated open calls */
  165. down_write(&nfsi->rwsem);
  166. nfs_delegation_claim_opens(inode, &delegation->stateid);
  167. up_write(&nfsi->rwsem);
  168. up_read(&clp->cl_sem);
  169. nfs_msync_inode(inode);
  170. return nfs_do_return_delegation(inode, delegation);
  171. }
  172. static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
  173. {
  174. struct nfs_delegation *delegation = nfsi->delegation;
  175. if (delegation == NULL)
  176. goto nomatch;
  177. if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
  178. sizeof(delegation->stateid.data)) != 0)
  179. goto nomatch;
  180. list_del_init(&delegation->super_list);
  181. nfsi->delegation = NULL;
  182. nfsi->delegation_state = 0;
  183. return delegation;
  184. nomatch:
  185. return NULL;
  186. }
  187. int nfs_inode_return_delegation(struct inode *inode)
  188. {
  189. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  190. struct nfs_inode *nfsi = NFS_I(inode);
  191. struct nfs_delegation *delegation;
  192. int err = 0;
  193. if (nfsi->delegation_state != 0) {
  194. spin_lock(&clp->cl_lock);
  195. delegation = nfs_detach_delegation_locked(nfsi, NULL);
  196. spin_unlock(&clp->cl_lock);
  197. if (delegation != NULL)
  198. err = __nfs_inode_return_delegation(inode, delegation);
  199. }
  200. return err;
  201. }
  202. /*
  203. * Return all delegations associated to a super block
  204. */
  205. void nfs_return_all_delegations(struct super_block *sb)
  206. {
  207. struct nfs_client *clp = NFS_SB(sb)->nfs_client;
  208. struct nfs_delegation *delegation;
  209. struct inode *inode;
  210. if (clp == NULL)
  211. return;
  212. restart:
  213. spin_lock(&clp->cl_lock);
  214. list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
  215. if (delegation->inode->i_sb != sb)
  216. continue;
  217. inode = igrab(delegation->inode);
  218. if (inode == NULL)
  219. continue;
  220. nfs_detach_delegation_locked(NFS_I(inode), NULL);
  221. spin_unlock(&clp->cl_lock);
  222. __nfs_inode_return_delegation(inode, delegation);
  223. iput(inode);
  224. goto restart;
  225. }
  226. spin_unlock(&clp->cl_lock);
  227. }
  228. static int nfs_do_expire_all_delegations(void *ptr)
  229. {
  230. struct nfs_client *clp = ptr;
  231. struct nfs_delegation *delegation;
  232. struct inode *inode;
  233. allow_signal(SIGKILL);
  234. restart:
  235. spin_lock(&clp->cl_lock);
  236. if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
  237. goto out;
  238. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
  239. goto out;
  240. list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
  241. inode = igrab(delegation->inode);
  242. if (inode == NULL)
  243. continue;
  244. nfs_detach_delegation_locked(NFS_I(inode), NULL);
  245. spin_unlock(&clp->cl_lock);
  246. __nfs_inode_return_delegation(inode, delegation);
  247. iput(inode);
  248. goto restart;
  249. }
  250. out:
  251. spin_unlock(&clp->cl_lock);
  252. nfs_put_client(clp);
  253. module_put_and_exit(0);
  254. }
  255. void nfs_expire_all_delegations(struct nfs_client *clp)
  256. {
  257. struct task_struct *task;
  258. __module_get(THIS_MODULE);
  259. atomic_inc(&clp->cl_count);
  260. task = kthread_run(nfs_do_expire_all_delegations, clp,
  261. "%u.%u.%u.%u-delegreturn",
  262. NIPQUAD(clp->cl_addr.sin_addr));
  263. if (!IS_ERR(task))
  264. return;
  265. nfs_put_client(clp);
  266. module_put(THIS_MODULE);
  267. }
  268. /*
  269. * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
  270. */
  271. void nfs_handle_cb_pathdown(struct nfs_client *clp)
  272. {
  273. struct nfs_delegation *delegation;
  274. struct inode *inode;
  275. if (clp == NULL)
  276. return;
  277. restart:
  278. spin_lock(&clp->cl_lock);
  279. list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
  280. inode = igrab(delegation->inode);
  281. if (inode == NULL)
  282. continue;
  283. nfs_detach_delegation_locked(NFS_I(inode), NULL);
  284. spin_unlock(&clp->cl_lock);
  285. __nfs_inode_return_delegation(inode, delegation);
  286. iput(inode);
  287. goto restart;
  288. }
  289. spin_unlock(&clp->cl_lock);
  290. }
  291. struct recall_threadargs {
  292. struct inode *inode;
  293. struct nfs_client *clp;
  294. const nfs4_stateid *stateid;
  295. struct completion started;
  296. int result;
  297. };
  298. static int recall_thread(void *data)
  299. {
  300. struct recall_threadargs *args = (struct recall_threadargs *)data;
  301. struct inode *inode = igrab(args->inode);
  302. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  303. struct nfs_inode *nfsi = NFS_I(inode);
  304. struct nfs_delegation *delegation;
  305. daemonize("nfsv4-delegreturn");
  306. nfs_msync_inode(inode);
  307. down_read(&clp->cl_sem);
  308. down_write(&nfsi->rwsem);
  309. spin_lock(&clp->cl_lock);
  310. delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
  311. if (delegation != NULL)
  312. args->result = 0;
  313. else
  314. args->result = -ENOENT;
  315. spin_unlock(&clp->cl_lock);
  316. complete(&args->started);
  317. nfs_delegation_claim_opens(inode, args->stateid);
  318. up_write(&nfsi->rwsem);
  319. up_read(&clp->cl_sem);
  320. nfs_msync_inode(inode);
  321. if (delegation != NULL)
  322. nfs_do_return_delegation(inode, delegation);
  323. iput(inode);
  324. module_put_and_exit(0);
  325. }
  326. /*
  327. * Asynchronous delegation recall!
  328. */
  329. int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
  330. {
  331. struct recall_threadargs data = {
  332. .inode = inode,
  333. .stateid = stateid,
  334. };
  335. int status;
  336. init_completion(&data.started);
  337. __module_get(THIS_MODULE);
  338. status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
  339. if (status < 0)
  340. goto out_module_put;
  341. wait_for_completion(&data.started);
  342. return data.result;
  343. out_module_put:
  344. module_put(THIS_MODULE);
  345. return status;
  346. }
  347. /*
  348. * Retrieve the inode associated with a delegation
  349. */
  350. struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
  351. {
  352. struct nfs_delegation *delegation;
  353. struct inode *res = NULL;
  354. spin_lock(&clp->cl_lock);
  355. list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
  356. if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
  357. res = igrab(delegation->inode);
  358. break;
  359. }
  360. }
  361. spin_unlock(&clp->cl_lock);
  362. return res;
  363. }
  364. /*
  365. * Mark all delegations as needing to be reclaimed
  366. */
  367. void nfs_delegation_mark_reclaim(struct nfs_client *clp)
  368. {
  369. struct nfs_delegation *delegation;
  370. spin_lock(&clp->cl_lock);
  371. list_for_each_entry(delegation, &clp->cl_delegations, super_list)
  372. delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
  373. spin_unlock(&clp->cl_lock);
  374. }
  375. /*
  376. * Reap all unclaimed delegations after reboot recovery is done
  377. */
  378. void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
  379. {
  380. struct nfs_delegation *delegation, *n;
  381. LIST_HEAD(head);
  382. spin_lock(&clp->cl_lock);
  383. list_for_each_entry_safe(delegation, n, &clp->cl_delegations, super_list) {
  384. if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
  385. continue;
  386. list_move(&delegation->super_list, &head);
  387. NFS_I(delegation->inode)->delegation = NULL;
  388. NFS_I(delegation->inode)->delegation_state = 0;
  389. }
  390. spin_unlock(&clp->cl_lock);
  391. while(!list_empty(&head)) {
  392. delegation = list_entry(head.next, struct nfs_delegation, super_list);
  393. list_del(&delegation->super_list);
  394. nfs_free_delegation(delegation);
  395. }
  396. }
  397. int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
  398. {
  399. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  400. struct nfs_inode *nfsi = NFS_I(inode);
  401. struct nfs_delegation *delegation;
  402. int res = 0;
  403. if (nfsi->delegation_state == 0)
  404. return 0;
  405. spin_lock(&clp->cl_lock);
  406. delegation = nfsi->delegation;
  407. if (delegation != NULL) {
  408. memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
  409. res = 1;
  410. }
  411. spin_unlock(&clp->cl_lock);
  412. return res;
  413. }