delegation.c 23 KB

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