delegation.c 15 KB

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