delegation.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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_do_free_delegation(struct nfs_delegation *delegation)
  21. {
  22. kfree(delegation);
  23. }
  24. static void nfs_free_delegation_callback(struct rcu_head *head)
  25. {
  26. struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu);
  27. nfs_do_free_delegation(delegation);
  28. }
  29. static void nfs_free_delegation(struct nfs_delegation *delegation)
  30. {
  31. struct rpc_cred *cred;
  32. cred = rcu_dereference(delegation->cred);
  33. rcu_assign_pointer(delegation->cred, NULL);
  34. call_rcu(&delegation->rcu, nfs_free_delegation_callback);
  35. if (cred)
  36. put_rpccred(cred);
  37. }
  38. static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
  39. {
  40. struct inode *inode = state->inode;
  41. struct file_lock *fl;
  42. int status;
  43. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  44. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  45. continue;
  46. if (nfs_file_open_context(fl->fl_file) != ctx)
  47. continue;
  48. status = nfs4_lock_delegation_recall(state, fl);
  49. if (status >= 0)
  50. continue;
  51. switch (status) {
  52. default:
  53. printk(KERN_ERR "%s: unhandled error %d.\n",
  54. __func__, status);
  55. case -NFS4ERR_EXPIRED:
  56. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  57. case -NFS4ERR_STALE_CLIENTID:
  58. nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client);
  59. goto out_err;
  60. }
  61. }
  62. return 0;
  63. out_err:
  64. return status;
  65. }
  66. static void nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
  67. {
  68. struct nfs_inode *nfsi = NFS_I(inode);
  69. struct nfs_open_context *ctx;
  70. struct nfs4_state *state;
  71. int err;
  72. again:
  73. spin_lock(&inode->i_lock);
  74. list_for_each_entry(ctx, &nfsi->open_files, list) {
  75. state = ctx->state;
  76. if (state == NULL)
  77. continue;
  78. if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
  79. continue;
  80. if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
  81. continue;
  82. get_nfs_open_context(ctx);
  83. spin_unlock(&inode->i_lock);
  84. err = nfs4_open_delegation_recall(ctx, state, stateid);
  85. if (err >= 0)
  86. err = nfs_delegation_claim_locks(ctx, state);
  87. put_nfs_open_context(ctx);
  88. if (err != 0)
  89. return;
  90. goto again;
  91. }
  92. spin_unlock(&inode->i_lock);
  93. }
  94. /*
  95. * Set up a delegation on an inode
  96. */
  97. void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
  98. {
  99. struct nfs_delegation *delegation = NFS_I(inode)->delegation;
  100. struct rpc_cred *oldcred;
  101. if (delegation == NULL)
  102. return;
  103. memcpy(delegation->stateid.data, res->delegation.data,
  104. sizeof(delegation->stateid.data));
  105. delegation->type = res->delegation_type;
  106. delegation->maxsize = res->maxsize;
  107. oldcred = delegation->cred;
  108. delegation->cred = get_rpccred(cred);
  109. clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  110. NFS_I(inode)->delegation_state = delegation->type;
  111. smp_wmb();
  112. put_rpccred(oldcred);
  113. }
  114. static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
  115. {
  116. int res = 0;
  117. res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
  118. nfs_free_delegation(delegation);
  119. return res;
  120. }
  121. static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
  122. {
  123. struct inode *inode = NULL;
  124. spin_lock(&delegation->lock);
  125. if (delegation->inode != NULL)
  126. inode = igrab(delegation->inode);
  127. spin_unlock(&delegation->lock);
  128. return inode;
  129. }
  130. static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
  131. {
  132. struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
  133. if (delegation == NULL)
  134. goto nomatch;
  135. spin_lock(&delegation->lock);
  136. if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
  137. sizeof(delegation->stateid.data)) != 0)
  138. goto nomatch_unlock;
  139. list_del_rcu(&delegation->super_list);
  140. delegation->inode = NULL;
  141. nfsi->delegation_state = 0;
  142. rcu_assign_pointer(nfsi->delegation, NULL);
  143. spin_unlock(&delegation->lock);
  144. return delegation;
  145. nomatch_unlock:
  146. spin_unlock(&delegation->lock);
  147. nomatch:
  148. return NULL;
  149. }
  150. /*
  151. * Set up a delegation on an inode
  152. */
  153. int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
  154. {
  155. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  156. struct nfs_inode *nfsi = NFS_I(inode);
  157. struct nfs_delegation *delegation;
  158. struct nfs_delegation *freeme = NULL;
  159. int status = 0;
  160. delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
  161. if (delegation == NULL)
  162. return -ENOMEM;
  163. memcpy(delegation->stateid.data, res->delegation.data,
  164. sizeof(delegation->stateid.data));
  165. delegation->type = res->delegation_type;
  166. delegation->maxsize = res->maxsize;
  167. delegation->change_attr = nfsi->change_attr;
  168. delegation->cred = get_rpccred(cred);
  169. delegation->inode = inode;
  170. spin_lock_init(&delegation->lock);
  171. spin_lock(&clp->cl_lock);
  172. if (rcu_dereference(nfsi->delegation) != NULL) {
  173. if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
  174. sizeof(delegation->stateid)) == 0 &&
  175. delegation->type == nfsi->delegation->type) {
  176. goto out;
  177. }
  178. /*
  179. * Deal with broken servers that hand out two
  180. * delegations for the same file.
  181. */
  182. dfprintk(FILE, "%s: server %s handed out "
  183. "a duplicate delegation!\n",
  184. __func__, clp->cl_hostname);
  185. if (delegation->type <= nfsi->delegation->type) {
  186. freeme = delegation;
  187. delegation = NULL;
  188. goto out;
  189. }
  190. freeme = nfs_detach_delegation_locked(nfsi, NULL);
  191. }
  192. list_add_rcu(&delegation->super_list, &clp->cl_delegations);
  193. nfsi->delegation_state = delegation->type;
  194. rcu_assign_pointer(nfsi->delegation, delegation);
  195. delegation = NULL;
  196. /* Ensure we revalidate the attributes and page cache! */
  197. spin_lock(&inode->i_lock);
  198. nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
  199. spin_unlock(&inode->i_lock);
  200. out:
  201. spin_unlock(&clp->cl_lock);
  202. if (delegation != NULL)
  203. nfs_free_delegation(delegation);
  204. if (freeme != NULL)
  205. nfs_do_return_delegation(inode, freeme, 0);
  206. return status;
  207. }
  208. /* Sync all data to disk upon delegation return */
  209. static void nfs_msync_inode(struct inode *inode)
  210. {
  211. filemap_fdatawrite(inode->i_mapping);
  212. nfs_wb_all(inode);
  213. filemap_fdatawait(inode->i_mapping);
  214. }
  215. /*
  216. * Basic procedure for returning a delegation to the server
  217. */
  218. static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
  219. {
  220. struct nfs_inode *nfsi = NFS_I(inode);
  221. nfs_msync_inode(inode);
  222. /* Guard against new delegated open calls */
  223. down_write(&nfsi->rwsem);
  224. nfs_delegation_claim_opens(inode, &delegation->stateid);
  225. up_write(&nfsi->rwsem);
  226. nfs_msync_inode(inode);
  227. return nfs_do_return_delegation(inode, delegation, 1);
  228. }
  229. /*
  230. * Return all delegations that have been marked for return
  231. */
  232. void nfs_client_return_marked_delegations(struct nfs_client *clp)
  233. {
  234. struct nfs_delegation *delegation;
  235. struct inode *inode;
  236. restart:
  237. rcu_read_lock();
  238. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  239. if (!test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
  240. continue;
  241. inode = nfs_delegation_grab_inode(delegation);
  242. if (inode == NULL)
  243. continue;
  244. spin_lock(&clp->cl_lock);
  245. delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
  246. spin_unlock(&clp->cl_lock);
  247. rcu_read_unlock();
  248. if (delegation != NULL)
  249. __nfs_inode_return_delegation(inode, delegation);
  250. iput(inode);
  251. goto restart;
  252. }
  253. rcu_read_unlock();
  254. }
  255. /*
  256. * This function returns the delegation without reclaiming opens
  257. * or protecting against delegation reclaims.
  258. * It is therefore really only safe to be called from
  259. * nfs4_clear_inode()
  260. */
  261. void nfs_inode_return_delegation_noreclaim(struct inode *inode)
  262. {
  263. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  264. struct nfs_inode *nfsi = NFS_I(inode);
  265. struct nfs_delegation *delegation;
  266. if (rcu_dereference(nfsi->delegation) != NULL) {
  267. spin_lock(&clp->cl_lock);
  268. delegation = nfs_detach_delegation_locked(nfsi, NULL);
  269. spin_unlock(&clp->cl_lock);
  270. if (delegation != NULL)
  271. nfs_do_return_delegation(inode, delegation, 0);
  272. }
  273. }
  274. int nfs_inode_return_delegation(struct inode *inode)
  275. {
  276. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  277. struct nfs_inode *nfsi = NFS_I(inode);
  278. struct nfs_delegation *delegation;
  279. int err = 0;
  280. if (rcu_dereference(nfsi->delegation) != NULL) {
  281. spin_lock(&clp->cl_lock);
  282. delegation = nfs_detach_delegation_locked(nfsi, NULL);
  283. spin_unlock(&clp->cl_lock);
  284. if (delegation != NULL)
  285. err = __nfs_inode_return_delegation(inode, delegation);
  286. }
  287. return err;
  288. }
  289. static void nfs_mark_return_delegation(struct nfs_client *clp, struct nfs_delegation *delegation)
  290. {
  291. set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
  292. set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
  293. }
  294. /*
  295. * Return all delegations associated to a super block
  296. */
  297. void nfs_super_return_all_delegations(struct super_block *sb)
  298. {
  299. struct nfs_client *clp = NFS_SB(sb)->nfs_client;
  300. struct nfs_delegation *delegation;
  301. if (clp == NULL)
  302. return;
  303. rcu_read_lock();
  304. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  305. spin_lock(&delegation->lock);
  306. if (delegation->inode != NULL && delegation->inode->i_sb == sb)
  307. set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
  308. spin_unlock(&delegation->lock);
  309. }
  310. rcu_read_unlock();
  311. nfs_client_return_marked_delegations(clp);
  312. }
  313. static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
  314. {
  315. struct nfs_delegation *delegation;
  316. rcu_read_lock();
  317. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  318. set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
  319. set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
  320. }
  321. rcu_read_unlock();
  322. }
  323. static void nfs_delegation_run_state_manager(struct nfs_client *clp)
  324. {
  325. if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
  326. nfs4_schedule_state_manager(clp);
  327. }
  328. void nfs_expire_all_delegations(struct nfs_client *clp)
  329. {
  330. nfs_client_mark_return_all_delegations(clp);
  331. nfs_delegation_run_state_manager(clp);
  332. }
  333. /*
  334. * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
  335. */
  336. void nfs_handle_cb_pathdown(struct nfs_client *clp)
  337. {
  338. if (clp == NULL)
  339. return;
  340. nfs_client_mark_return_all_delegations(clp);
  341. }
  342. /*
  343. * Asynchronous delegation recall!
  344. */
  345. int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
  346. {
  347. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  348. struct nfs_delegation *delegation;
  349. rcu_read_lock();
  350. delegation = rcu_dereference(NFS_I(inode)->delegation);
  351. if (delegation == NULL || memcmp(delegation->stateid.data, stateid->data,
  352. sizeof(delegation->stateid.data)) != 0) {
  353. rcu_read_unlock();
  354. return -ENOENT;
  355. }
  356. nfs_mark_return_delegation(clp, delegation);
  357. rcu_read_unlock();
  358. nfs_delegation_run_state_manager(clp);
  359. return 0;
  360. }
  361. /*
  362. * Retrieve the inode associated with a delegation
  363. */
  364. struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
  365. {
  366. struct nfs_delegation *delegation;
  367. struct inode *res = NULL;
  368. rcu_read_lock();
  369. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  370. spin_lock(&delegation->lock);
  371. if (delegation->inode != NULL &&
  372. nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
  373. res = igrab(delegation->inode);
  374. }
  375. spin_unlock(&delegation->lock);
  376. if (res != NULL)
  377. break;
  378. }
  379. rcu_read_unlock();
  380. return res;
  381. }
  382. /*
  383. * Mark all delegations as needing to be reclaimed
  384. */
  385. void nfs_delegation_mark_reclaim(struct nfs_client *clp)
  386. {
  387. struct nfs_delegation *delegation;
  388. rcu_read_lock();
  389. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
  390. set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  391. rcu_read_unlock();
  392. }
  393. /*
  394. * Reap all unclaimed delegations after reboot recovery is done
  395. */
  396. void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
  397. {
  398. struct nfs_delegation *delegation;
  399. struct inode *inode;
  400. restart:
  401. rcu_read_lock();
  402. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  403. if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
  404. continue;
  405. inode = nfs_delegation_grab_inode(delegation);
  406. if (inode == NULL)
  407. continue;
  408. spin_lock(&clp->cl_lock);
  409. delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
  410. spin_unlock(&clp->cl_lock);
  411. rcu_read_unlock();
  412. if (delegation != NULL)
  413. nfs_free_delegation(delegation);
  414. iput(inode);
  415. goto restart;
  416. }
  417. rcu_read_unlock();
  418. }
  419. int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
  420. {
  421. struct nfs_inode *nfsi = NFS_I(inode);
  422. struct nfs_delegation *delegation;
  423. int ret = 0;
  424. rcu_read_lock();
  425. delegation = rcu_dereference(nfsi->delegation);
  426. if (delegation != NULL) {
  427. memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
  428. ret = 1;
  429. }
  430. rcu_read_unlock();
  431. return ret;
  432. }