delegation.c 24 KB

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