delegation.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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/spinlock.h>
  15. #include <linux/nfs4.h>
  16. #include <linux/nfs_fs.h>
  17. #include <linux/nfs_xdr.h>
  18. #include "nfs4_fs.h"
  19. #include "delegation.h"
  20. #include "internal.h"
  21. #include "nfs4trace.h"
  22. static void nfs_free_delegation(struct nfs_delegation *delegation)
  23. {
  24. if (delegation->cred) {
  25. put_rpccred(delegation->cred);
  26. delegation->cred = NULL;
  27. }
  28. kfree_rcu(delegation, rcu);
  29. }
  30. /**
  31. * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
  32. * @delegation: delegation to process
  33. *
  34. */
  35. void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
  36. {
  37. set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
  38. }
  39. static bool
  40. nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
  41. fmode_t flags)
  42. {
  43. if (delegation != NULL && (delegation->type & flags) == flags &&
  44. !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
  45. !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
  46. return true;
  47. return false;
  48. }
  49. static int
  50. nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
  51. {
  52. struct nfs_delegation *delegation;
  53. int ret = 0;
  54. flags &= FMODE_READ|FMODE_WRITE;
  55. rcu_read_lock();
  56. delegation = rcu_dereference(NFS_I(inode)->delegation);
  57. if (nfs4_is_valid_delegation(delegation, flags)) {
  58. if (mark)
  59. nfs_mark_delegation_referenced(delegation);
  60. ret = 1;
  61. }
  62. rcu_read_unlock();
  63. return ret;
  64. }
  65. /**
  66. * nfs_have_delegation - check if inode has a delegation, mark it
  67. * NFS_DELEGATION_REFERENCED if there is one.
  68. * @inode: inode to check
  69. * @flags: delegation types to check for
  70. *
  71. * Returns one if inode has the indicated delegation, otherwise zero.
  72. */
  73. int nfs4_have_delegation(struct inode *inode, fmode_t flags)
  74. {
  75. return nfs4_do_check_delegation(inode, flags, true);
  76. }
  77. /*
  78. * nfs4_check_delegation - check if inode has a delegation, do not mark
  79. * NFS_DELEGATION_REFERENCED if it has one.
  80. */
  81. int nfs4_check_delegation(struct inode *inode, fmode_t flags)
  82. {
  83. return nfs4_do_check_delegation(inode, flags, false);
  84. }
  85. static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
  86. {
  87. struct inode *inode = state->inode;
  88. struct file_lock *fl;
  89. struct file_lock_context *flctx = inode->i_flctx;
  90. struct list_head *list;
  91. int status = 0;
  92. if (flctx == NULL)
  93. goto out;
  94. list = &flctx->flc_posix;
  95. spin_lock(&flctx->flc_lock);
  96. restart:
  97. list_for_each_entry(fl, list, fl_list) {
  98. if (nfs_file_open_context(fl->fl_file) != ctx)
  99. continue;
  100. spin_unlock(&flctx->flc_lock);
  101. status = nfs4_lock_delegation_recall(fl, state, stateid);
  102. if (status < 0)
  103. goto out;
  104. spin_lock(&flctx->flc_lock);
  105. }
  106. if (list == &flctx->flc_posix) {
  107. list = &flctx->flc_flock;
  108. goto restart;
  109. }
  110. spin_unlock(&flctx->flc_lock);
  111. out:
  112. return status;
  113. }
  114. static int nfs_delegation_claim_opens(struct inode *inode,
  115. const nfs4_stateid *stateid, fmode_t type)
  116. {
  117. struct nfs_inode *nfsi = NFS_I(inode);
  118. struct nfs_open_context *ctx;
  119. struct nfs4_state_owner *sp;
  120. struct nfs4_state *state;
  121. unsigned int seq;
  122. int err;
  123. again:
  124. spin_lock(&inode->i_lock);
  125. list_for_each_entry(ctx, &nfsi->open_files, list) {
  126. state = ctx->state;
  127. if (state == NULL)
  128. continue;
  129. if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
  130. continue;
  131. if (!nfs4_valid_open_stateid(state))
  132. continue;
  133. if (!nfs4_stateid_match(&state->stateid, stateid))
  134. continue;
  135. get_nfs_open_context(ctx);
  136. spin_unlock(&inode->i_lock);
  137. sp = state->owner;
  138. /* Block nfs4_proc_unlck */
  139. mutex_lock(&sp->so_delegreturn_mutex);
  140. seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
  141. err = nfs4_open_delegation_recall(ctx, state, stateid, type);
  142. if (!err)
  143. err = nfs_delegation_claim_locks(ctx, state, stateid);
  144. if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
  145. err = -EAGAIN;
  146. mutex_unlock(&sp->so_delegreturn_mutex);
  147. put_nfs_open_context(ctx);
  148. if (err != 0)
  149. return err;
  150. goto again;
  151. }
  152. spin_unlock(&inode->i_lock);
  153. return 0;
  154. }
  155. /**
  156. * nfs_inode_reclaim_delegation - process a delegation reclaim request
  157. * @inode: inode to process
  158. * @cred: credential to use for request
  159. * @res: new delegation state from server
  160. *
  161. */
  162. void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred,
  163. struct nfs_openres *res)
  164. {
  165. struct nfs_delegation *delegation;
  166. struct rpc_cred *oldcred = NULL;
  167. rcu_read_lock();
  168. delegation = rcu_dereference(NFS_I(inode)->delegation);
  169. if (delegation != NULL) {
  170. spin_lock(&delegation->lock);
  171. if (delegation->inode != NULL) {
  172. nfs4_stateid_copy(&delegation->stateid, &res->delegation);
  173. delegation->type = res->delegation_type;
  174. delegation->pagemod_limit = res->pagemod_limit;
  175. oldcred = delegation->cred;
  176. delegation->cred = get_rpccred(cred);
  177. clear_bit(NFS_DELEGATION_NEED_RECLAIM,
  178. &delegation->flags);
  179. spin_unlock(&delegation->lock);
  180. rcu_read_unlock();
  181. put_rpccred(oldcred);
  182. trace_nfs4_reclaim_delegation(inode, res->delegation_type);
  183. } else {
  184. /* We appear to have raced with a delegation return. */
  185. spin_unlock(&delegation->lock);
  186. rcu_read_unlock();
  187. nfs_inode_set_delegation(inode, cred, res);
  188. }
  189. } else {
  190. rcu_read_unlock();
  191. }
  192. }
  193. static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
  194. {
  195. int res = 0;
  196. if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
  197. res = nfs4_proc_delegreturn(inode,
  198. delegation->cred,
  199. &delegation->stateid,
  200. issync);
  201. nfs_free_delegation(delegation);
  202. return res;
  203. }
  204. static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
  205. {
  206. struct inode *inode = NULL;
  207. spin_lock(&delegation->lock);
  208. if (delegation->inode != NULL)
  209. inode = igrab(delegation->inode);
  210. spin_unlock(&delegation->lock);
  211. return inode;
  212. }
  213. static struct nfs_delegation *
  214. nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
  215. {
  216. struct nfs_delegation *ret = NULL;
  217. struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
  218. if (delegation == NULL)
  219. goto out;
  220. spin_lock(&delegation->lock);
  221. if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
  222. ret = delegation;
  223. spin_unlock(&delegation->lock);
  224. out:
  225. return ret;
  226. }
  227. static struct nfs_delegation *
  228. nfs_start_delegation_return(struct nfs_inode *nfsi)
  229. {
  230. struct nfs_delegation *delegation;
  231. rcu_read_lock();
  232. delegation = nfs_start_delegation_return_locked(nfsi);
  233. rcu_read_unlock();
  234. return delegation;
  235. }
  236. static void
  237. nfs_abort_delegation_return(struct nfs_delegation *delegation,
  238. struct nfs_client *clp)
  239. {
  240. spin_lock(&delegation->lock);
  241. clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
  242. set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
  243. spin_unlock(&delegation->lock);
  244. set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
  245. }
  246. static struct nfs_delegation *
  247. nfs_detach_delegation_locked(struct nfs_inode *nfsi,
  248. struct nfs_delegation *delegation,
  249. struct nfs_client *clp)
  250. {
  251. struct nfs_delegation *deleg_cur =
  252. rcu_dereference_protected(nfsi->delegation,
  253. lockdep_is_held(&clp->cl_lock));
  254. if (deleg_cur == NULL || delegation != deleg_cur)
  255. return NULL;
  256. spin_lock(&delegation->lock);
  257. set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
  258. list_del_rcu(&delegation->super_list);
  259. delegation->inode = NULL;
  260. rcu_assign_pointer(nfsi->delegation, NULL);
  261. spin_unlock(&delegation->lock);
  262. return delegation;
  263. }
  264. static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
  265. struct nfs_delegation *delegation,
  266. struct nfs_server *server)
  267. {
  268. struct nfs_client *clp = server->nfs_client;
  269. spin_lock(&clp->cl_lock);
  270. delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
  271. spin_unlock(&clp->cl_lock);
  272. return delegation;
  273. }
  274. static struct nfs_delegation *
  275. nfs_inode_detach_delegation(struct inode *inode)
  276. {
  277. struct nfs_inode *nfsi = NFS_I(inode);
  278. struct nfs_server *server = NFS_SERVER(inode);
  279. struct nfs_delegation *delegation;
  280. delegation = nfs_start_delegation_return(nfsi);
  281. if (delegation == NULL)
  282. return NULL;
  283. return nfs_detach_delegation(nfsi, delegation, server);
  284. }
  285. static void
  286. nfs_update_inplace_delegation(struct nfs_delegation *delegation,
  287. const struct nfs_delegation *update)
  288. {
  289. if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
  290. delegation->stateid.seqid = update->stateid.seqid;
  291. smp_wmb();
  292. delegation->type = update->type;
  293. }
  294. }
  295. /**
  296. * nfs_inode_set_delegation - set up a delegation on an inode
  297. * @inode: inode to which delegation applies
  298. * @cred: cred to use for subsequent delegation processing
  299. * @res: new delegation state from server
  300. *
  301. * Returns zero on success, or a negative errno value.
  302. */
  303. int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
  304. {
  305. struct nfs_server *server = NFS_SERVER(inode);
  306. struct nfs_client *clp = server->nfs_client;
  307. struct nfs_inode *nfsi = NFS_I(inode);
  308. struct nfs_delegation *delegation, *old_delegation;
  309. struct nfs_delegation *freeme = NULL;
  310. int status = 0;
  311. delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
  312. if (delegation == NULL)
  313. return -ENOMEM;
  314. nfs4_stateid_copy(&delegation->stateid, &res->delegation);
  315. delegation->type = res->delegation_type;
  316. delegation->pagemod_limit = res->pagemod_limit;
  317. delegation->change_attr = inode->i_version;
  318. delegation->cred = get_rpccred(cred);
  319. delegation->inode = inode;
  320. delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
  321. spin_lock_init(&delegation->lock);
  322. spin_lock(&clp->cl_lock);
  323. old_delegation = rcu_dereference_protected(nfsi->delegation,
  324. lockdep_is_held(&clp->cl_lock));
  325. if (old_delegation != NULL) {
  326. /* Is this an update of the existing delegation? */
  327. if (nfs4_stateid_match_other(&old_delegation->stateid,
  328. &delegation->stateid)) {
  329. nfs_update_inplace_delegation(old_delegation,
  330. delegation);
  331. goto out;
  332. }
  333. /*
  334. * Deal with broken servers that hand out two
  335. * delegations for the same file.
  336. * Allow for upgrades to a WRITE delegation, but
  337. * nothing else.
  338. */
  339. dfprintk(FILE, "%s: server %s handed out "
  340. "a duplicate delegation!\n",
  341. __func__, clp->cl_hostname);
  342. if (delegation->type == old_delegation->type ||
  343. !(delegation->type & FMODE_WRITE)) {
  344. freeme = delegation;
  345. delegation = NULL;
  346. goto out;
  347. }
  348. if (test_and_set_bit(NFS_DELEGATION_RETURNING,
  349. &old_delegation->flags))
  350. goto out;
  351. freeme = nfs_detach_delegation_locked(nfsi,
  352. old_delegation, clp);
  353. if (freeme == NULL)
  354. goto out;
  355. }
  356. list_add_tail_rcu(&delegation->super_list, &server->delegations);
  357. rcu_assign_pointer(nfsi->delegation, delegation);
  358. delegation = NULL;
  359. /* Ensure we revalidate the attributes and page cache! */
  360. spin_lock(&inode->i_lock);
  361. nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
  362. spin_unlock(&inode->i_lock);
  363. trace_nfs4_set_delegation(inode, res->delegation_type);
  364. out:
  365. spin_unlock(&clp->cl_lock);
  366. if (delegation != NULL)
  367. nfs_free_delegation(delegation);
  368. if (freeme != NULL)
  369. nfs_do_return_delegation(inode, freeme, 0);
  370. return status;
  371. }
  372. /*
  373. * Basic procedure for returning a delegation to the server
  374. */
  375. static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
  376. {
  377. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  378. struct nfs_inode *nfsi = NFS_I(inode);
  379. int err = 0;
  380. if (delegation == NULL)
  381. return 0;
  382. do {
  383. if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
  384. break;
  385. err = nfs_delegation_claim_opens(inode, &delegation->stateid,
  386. delegation->type);
  387. if (!issync || err != -EAGAIN)
  388. break;
  389. /*
  390. * Guard against state recovery
  391. */
  392. err = nfs4_wait_clnt_recover(clp);
  393. } while (err == 0);
  394. if (err) {
  395. nfs_abort_delegation_return(delegation, clp);
  396. goto out;
  397. }
  398. if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
  399. goto out;
  400. err = nfs_do_return_delegation(inode, delegation, issync);
  401. out:
  402. return err;
  403. }
  404. static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
  405. {
  406. bool ret = false;
  407. if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
  408. goto out;
  409. if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
  410. ret = true;
  411. if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
  412. struct inode *inode;
  413. spin_lock(&delegation->lock);
  414. inode = delegation->inode;
  415. if (inode && list_empty(&NFS_I(inode)->open_files))
  416. ret = true;
  417. spin_unlock(&delegation->lock);
  418. }
  419. out:
  420. return ret;
  421. }
  422. /**
  423. * nfs_client_return_marked_delegations - return previously marked delegations
  424. * @clp: nfs_client to process
  425. *
  426. * Note that this function is designed to be called by the state
  427. * manager thread. For this reason, it cannot flush the dirty data,
  428. * since that could deadlock in case of a state recovery error.
  429. *
  430. * Returns zero on success, or a negative errno value.
  431. */
  432. int nfs_client_return_marked_delegations(struct nfs_client *clp)
  433. {
  434. struct nfs_delegation *delegation;
  435. struct nfs_server *server;
  436. struct inode *inode;
  437. int err = 0;
  438. restart:
  439. rcu_read_lock();
  440. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  441. list_for_each_entry_rcu(delegation, &server->delegations,
  442. super_list) {
  443. if (!nfs_delegation_need_return(delegation))
  444. continue;
  445. if (!nfs_sb_active(server->super))
  446. continue;
  447. inode = nfs_delegation_grab_inode(delegation);
  448. if (inode == NULL) {
  449. rcu_read_unlock();
  450. nfs_sb_deactive(server->super);
  451. goto restart;
  452. }
  453. delegation = nfs_start_delegation_return_locked(NFS_I(inode));
  454. rcu_read_unlock();
  455. err = nfs_end_delegation_return(inode, delegation, 0);
  456. iput(inode);
  457. nfs_sb_deactive(server->super);
  458. if (!err)
  459. goto restart;
  460. set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
  461. return err;
  462. }
  463. }
  464. rcu_read_unlock();
  465. return 0;
  466. }
  467. /**
  468. * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
  469. * @inode: inode to process
  470. *
  471. * Does not protect against delegation reclaims, therefore really only safe
  472. * to be called from nfs4_clear_inode().
  473. */
  474. void nfs_inode_return_delegation_noreclaim(struct inode *inode)
  475. {
  476. struct nfs_delegation *delegation;
  477. delegation = nfs_inode_detach_delegation(inode);
  478. if (delegation != NULL)
  479. nfs_do_return_delegation(inode, delegation, 1);
  480. }
  481. /**
  482. * nfs_inode_return_delegation - synchronously return a delegation
  483. * @inode: inode to process
  484. *
  485. * This routine will always flush any dirty data to disk on the
  486. * assumption that if we need to return the delegation, then
  487. * we should stop caching.
  488. *
  489. * Returns zero on success, or a negative errno value.
  490. */
  491. int nfs4_inode_return_delegation(struct inode *inode)
  492. {
  493. struct nfs_inode *nfsi = NFS_I(inode);
  494. struct nfs_delegation *delegation;
  495. int err = 0;
  496. nfs_wb_all(inode);
  497. delegation = nfs_start_delegation_return(nfsi);
  498. if (delegation != NULL)
  499. err = nfs_end_delegation_return(inode, delegation, 1);
  500. return err;
  501. }
  502. static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
  503. struct nfs_delegation *delegation)
  504. {
  505. set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
  506. set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
  507. }
  508. static void nfs_mark_return_delegation(struct nfs_server *server,
  509. struct nfs_delegation *delegation)
  510. {
  511. set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
  512. set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
  513. }
  514. static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
  515. {
  516. struct nfs_delegation *delegation;
  517. bool ret = false;
  518. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  519. nfs_mark_return_delegation(server, delegation);
  520. ret = true;
  521. }
  522. return ret;
  523. }
  524. static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
  525. {
  526. struct nfs_server *server;
  527. rcu_read_lock();
  528. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  529. nfs_server_mark_return_all_delegations(server);
  530. rcu_read_unlock();
  531. }
  532. static void nfs_delegation_run_state_manager(struct nfs_client *clp)
  533. {
  534. if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
  535. nfs4_schedule_state_manager(clp);
  536. }
  537. /**
  538. * nfs_expire_all_delegations
  539. * @clp: client to process
  540. *
  541. */
  542. void nfs_expire_all_delegations(struct nfs_client *clp)
  543. {
  544. nfs_client_mark_return_all_delegations(clp);
  545. nfs_delegation_run_state_manager(clp);
  546. }
  547. /**
  548. * nfs_super_return_all_delegations - return delegations for one superblock
  549. * @sb: sb to process
  550. *
  551. */
  552. void nfs_server_return_all_delegations(struct nfs_server *server)
  553. {
  554. struct nfs_client *clp = server->nfs_client;
  555. bool need_wait;
  556. if (clp == NULL)
  557. return;
  558. rcu_read_lock();
  559. need_wait = nfs_server_mark_return_all_delegations(server);
  560. rcu_read_unlock();
  561. if (need_wait) {
  562. nfs4_schedule_state_manager(clp);
  563. nfs4_wait_clnt_recover(clp);
  564. }
  565. }
  566. static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
  567. fmode_t flags)
  568. {
  569. struct nfs_delegation *delegation;
  570. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  571. if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
  572. continue;
  573. if (delegation->type & flags)
  574. nfs_mark_return_if_closed_delegation(server, delegation);
  575. }
  576. }
  577. static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
  578. fmode_t flags)
  579. {
  580. struct nfs_server *server;
  581. rcu_read_lock();
  582. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  583. nfs_mark_return_unused_delegation_types(server, flags);
  584. rcu_read_unlock();
  585. }
  586. static void nfs_revoke_delegation(struct inode *inode)
  587. {
  588. struct nfs_delegation *delegation;
  589. rcu_read_lock();
  590. delegation = rcu_dereference(NFS_I(inode)->delegation);
  591. if (delegation != NULL) {
  592. set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
  593. nfs_mark_return_delegation(NFS_SERVER(inode), delegation);
  594. }
  595. rcu_read_unlock();
  596. }
  597. void nfs_remove_bad_delegation(struct inode *inode)
  598. {
  599. struct nfs_delegation *delegation;
  600. nfs_revoke_delegation(inode);
  601. delegation = nfs_inode_detach_delegation(inode);
  602. if (delegation) {
  603. nfs_inode_find_state_and_recover(inode, &delegation->stateid);
  604. nfs_free_delegation(delegation);
  605. }
  606. }
  607. EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
  608. /**
  609. * nfs_expire_unused_delegation_types
  610. * @clp: client to process
  611. * @flags: delegation types to expire
  612. *
  613. */
  614. void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
  615. {
  616. nfs_client_mark_return_unused_delegation_types(clp, flags);
  617. nfs_delegation_run_state_manager(clp);
  618. }
  619. static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
  620. {
  621. struct nfs_delegation *delegation;
  622. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  623. if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
  624. continue;
  625. nfs_mark_return_if_closed_delegation(server, delegation);
  626. }
  627. }
  628. /**
  629. * nfs_expire_unreferenced_delegations - Eliminate unused delegations
  630. * @clp: nfs_client to process
  631. *
  632. */
  633. void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
  634. {
  635. struct nfs_server *server;
  636. rcu_read_lock();
  637. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  638. nfs_mark_return_unreferenced_delegations(server);
  639. rcu_read_unlock();
  640. nfs_delegation_run_state_manager(clp);
  641. }
  642. /**
  643. * nfs_async_inode_return_delegation - asynchronously return a delegation
  644. * @inode: inode to process
  645. * @stateid: state ID information
  646. *
  647. * Returns zero on success, or a negative errno value.
  648. */
  649. int nfs_async_inode_return_delegation(struct inode *inode,
  650. const nfs4_stateid *stateid)
  651. {
  652. struct nfs_server *server = NFS_SERVER(inode);
  653. struct nfs_client *clp = server->nfs_client;
  654. struct nfs_delegation *delegation;
  655. rcu_read_lock();
  656. delegation = rcu_dereference(NFS_I(inode)->delegation);
  657. if (delegation == NULL)
  658. goto out_enoent;
  659. if (stateid != NULL &&
  660. !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
  661. goto out_enoent;
  662. nfs_mark_return_delegation(server, delegation);
  663. rcu_read_unlock();
  664. nfs_delegation_run_state_manager(clp);
  665. return 0;
  666. out_enoent:
  667. rcu_read_unlock();
  668. return -ENOENT;
  669. }
  670. static struct inode *
  671. nfs_delegation_find_inode_server(struct nfs_server *server,
  672. const struct nfs_fh *fhandle)
  673. {
  674. struct nfs_delegation *delegation;
  675. struct inode *res = NULL;
  676. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  677. spin_lock(&delegation->lock);
  678. if (delegation->inode != NULL &&
  679. nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
  680. res = igrab(delegation->inode);
  681. }
  682. spin_unlock(&delegation->lock);
  683. if (res != NULL)
  684. break;
  685. }
  686. return res;
  687. }
  688. /**
  689. * nfs_delegation_find_inode - retrieve the inode associated with a delegation
  690. * @clp: client state handle
  691. * @fhandle: filehandle from a delegation recall
  692. *
  693. * Returns pointer to inode matching "fhandle," or NULL if a matching inode
  694. * cannot be found.
  695. */
  696. struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
  697. const struct nfs_fh *fhandle)
  698. {
  699. struct nfs_server *server;
  700. struct inode *res = NULL;
  701. rcu_read_lock();
  702. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  703. res = nfs_delegation_find_inode_server(server, fhandle);
  704. if (res != NULL)
  705. break;
  706. }
  707. rcu_read_unlock();
  708. return res;
  709. }
  710. static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
  711. {
  712. struct nfs_delegation *delegation;
  713. list_for_each_entry_rcu(delegation, &server->delegations, super_list)
  714. set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  715. }
  716. /**
  717. * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
  718. * @clp: nfs_client to process
  719. *
  720. */
  721. void nfs_delegation_mark_reclaim(struct nfs_client *clp)
  722. {
  723. struct nfs_server *server;
  724. rcu_read_lock();
  725. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  726. nfs_delegation_mark_reclaim_server(server);
  727. rcu_read_unlock();
  728. }
  729. /**
  730. * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
  731. * @clp: nfs_client to process
  732. *
  733. */
  734. void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
  735. {
  736. struct nfs_delegation *delegation;
  737. struct nfs_server *server;
  738. struct inode *inode;
  739. restart:
  740. rcu_read_lock();
  741. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  742. list_for_each_entry_rcu(delegation, &server->delegations,
  743. super_list) {
  744. if (test_bit(NFS_DELEGATION_RETURNING,
  745. &delegation->flags))
  746. continue;
  747. if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
  748. &delegation->flags) == 0)
  749. continue;
  750. if (!nfs_sb_active(server->super))
  751. continue;
  752. inode = nfs_delegation_grab_inode(delegation);
  753. if (inode == NULL) {
  754. rcu_read_unlock();
  755. nfs_sb_deactive(server->super);
  756. goto restart;
  757. }
  758. delegation = nfs_start_delegation_return_locked(NFS_I(inode));
  759. rcu_read_unlock();
  760. if (delegation != NULL) {
  761. delegation = nfs_detach_delegation(NFS_I(inode),
  762. delegation, server);
  763. if (delegation != NULL)
  764. nfs_free_delegation(delegation);
  765. }
  766. iput(inode);
  767. nfs_sb_deactive(server->super);
  768. goto restart;
  769. }
  770. }
  771. rcu_read_unlock();
  772. }
  773. /**
  774. * nfs_delegations_present - check for existence of delegations
  775. * @clp: client state handle
  776. *
  777. * Returns one if there are any nfs_delegation structures attached
  778. * to this nfs_client.
  779. */
  780. int nfs_delegations_present(struct nfs_client *clp)
  781. {
  782. struct nfs_server *server;
  783. int ret = 0;
  784. rcu_read_lock();
  785. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  786. if (!list_empty(&server->delegations)) {
  787. ret = 1;
  788. break;
  789. }
  790. rcu_read_unlock();
  791. return ret;
  792. }
  793. /**
  794. * nfs4_copy_delegation_stateid - Copy inode's state ID information
  795. * @dst: stateid data structure to fill in
  796. * @inode: inode to check
  797. * @flags: delegation type requirement
  798. *
  799. * Returns "true" and fills in "dst->data" * if inode had a delegation,
  800. * otherwise "false" is returned.
  801. */
  802. bool nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode,
  803. fmode_t flags)
  804. {
  805. struct nfs_inode *nfsi = NFS_I(inode);
  806. struct nfs_delegation *delegation;
  807. bool ret;
  808. flags &= FMODE_READ|FMODE_WRITE;
  809. rcu_read_lock();
  810. delegation = rcu_dereference(nfsi->delegation);
  811. ret = nfs4_is_valid_delegation(delegation, flags);
  812. if (ret) {
  813. nfs4_stateid_copy(dst, &delegation->stateid);
  814. nfs_mark_delegation_referenced(delegation);
  815. }
  816. rcu_read_unlock();
  817. return ret;
  818. }
  819. /**
  820. * nfs4_delegation_flush_on_close - Check if we must flush file on close
  821. * @inode: inode to check
  822. *
  823. * This function checks the number of outstanding writes to the file
  824. * against the delegation 'space_limit' field to see if
  825. * the spec requires us to flush the file on close.
  826. */
  827. bool nfs4_delegation_flush_on_close(const struct inode *inode)
  828. {
  829. struct nfs_inode *nfsi = NFS_I(inode);
  830. struct nfs_delegation *delegation;
  831. bool ret = true;
  832. rcu_read_lock();
  833. delegation = rcu_dereference(nfsi->delegation);
  834. if (delegation == NULL || !(delegation->type & FMODE_WRITE))
  835. goto out;
  836. if (nfsi->nrequests < delegation->pagemod_limit)
  837. ret = false;
  838. out:
  839. rcu_read_unlock();
  840. return ret;
  841. }