delegation.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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. rcu_read_unlock();
  171. put_rpccred(oldcred);
  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. if (test_and_set_bit(NFS_DELEGATION_RETURNING,
  339. &old_delegation->flags))
  340. goto out;
  341. freeme = nfs_detach_delegation_locked(nfsi,
  342. old_delegation, clp);
  343. if (freeme == NULL)
  344. goto out;
  345. }
  346. list_add_rcu(&delegation->super_list, &server->delegations);
  347. rcu_assign_pointer(nfsi->delegation, delegation);
  348. delegation = NULL;
  349. /* Ensure we revalidate the attributes and page cache! */
  350. spin_lock(&inode->i_lock);
  351. nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
  352. spin_unlock(&inode->i_lock);
  353. trace_nfs4_set_delegation(inode, res->delegation_type);
  354. out:
  355. spin_unlock(&clp->cl_lock);
  356. if (delegation != NULL)
  357. nfs_free_delegation(delegation);
  358. if (freeme != NULL)
  359. nfs_do_return_delegation(inode, freeme, 0);
  360. return status;
  361. }
  362. /*
  363. * Basic procedure for returning a delegation to the server
  364. */
  365. static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
  366. {
  367. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  368. struct nfs_inode *nfsi = NFS_I(inode);
  369. int err = 0;
  370. if (delegation == NULL)
  371. return 0;
  372. do {
  373. if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
  374. break;
  375. err = nfs_delegation_claim_opens(inode, &delegation->stateid);
  376. if (!issync || err != -EAGAIN)
  377. break;
  378. /*
  379. * Guard against state recovery
  380. */
  381. err = nfs4_wait_clnt_recover(clp);
  382. } while (err == 0);
  383. if (err) {
  384. nfs_abort_delegation_return(delegation, clp);
  385. goto out;
  386. }
  387. if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
  388. goto out;
  389. err = nfs_do_return_delegation(inode, delegation, issync);
  390. out:
  391. return err;
  392. }
  393. static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
  394. {
  395. bool ret = false;
  396. if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
  397. goto out;
  398. if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
  399. ret = true;
  400. if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
  401. struct inode *inode;
  402. spin_lock(&delegation->lock);
  403. inode = delegation->inode;
  404. if (inode && list_empty(&NFS_I(inode)->open_files))
  405. ret = true;
  406. spin_unlock(&delegation->lock);
  407. }
  408. out:
  409. return ret;
  410. }
  411. /**
  412. * nfs_client_return_marked_delegations - return previously marked delegations
  413. * @clp: nfs_client to process
  414. *
  415. * Note that this function is designed to be called by the state
  416. * manager thread. For this reason, it cannot flush the dirty data,
  417. * since that could deadlock in case of a state recovery error.
  418. *
  419. * Returns zero on success, or a negative errno value.
  420. */
  421. int nfs_client_return_marked_delegations(struct nfs_client *clp)
  422. {
  423. struct nfs_delegation *delegation;
  424. struct nfs_server *server;
  425. struct inode *inode;
  426. int err = 0;
  427. restart:
  428. rcu_read_lock();
  429. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  430. list_for_each_entry_rcu(delegation, &server->delegations,
  431. super_list) {
  432. if (!nfs_delegation_need_return(delegation))
  433. continue;
  434. if (!nfs_sb_active(server->super))
  435. continue;
  436. inode = nfs_delegation_grab_inode(delegation);
  437. if (inode == NULL) {
  438. rcu_read_unlock();
  439. nfs_sb_deactive(server->super);
  440. goto restart;
  441. }
  442. delegation = nfs_start_delegation_return_locked(NFS_I(inode));
  443. rcu_read_unlock();
  444. err = nfs_end_delegation_return(inode, delegation, 0);
  445. iput(inode);
  446. nfs_sb_deactive(server->super);
  447. if (!err)
  448. goto restart;
  449. set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
  450. return err;
  451. }
  452. }
  453. rcu_read_unlock();
  454. return 0;
  455. }
  456. /**
  457. * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
  458. * @inode: inode to process
  459. *
  460. * Does not protect against delegation reclaims, therefore really only safe
  461. * to be called from nfs4_clear_inode().
  462. */
  463. void nfs_inode_return_delegation_noreclaim(struct inode *inode)
  464. {
  465. struct nfs_delegation *delegation;
  466. delegation = nfs_inode_detach_delegation(inode);
  467. if (delegation != NULL)
  468. nfs_do_return_delegation(inode, delegation, 0);
  469. }
  470. /**
  471. * nfs_inode_return_delegation - synchronously return a delegation
  472. * @inode: inode to process
  473. *
  474. * This routine will always flush any dirty data to disk on the
  475. * assumption that if we need to return the delegation, then
  476. * we should stop caching.
  477. *
  478. * Returns zero on success, or a negative errno value.
  479. */
  480. int nfs4_inode_return_delegation(struct inode *inode)
  481. {
  482. struct nfs_inode *nfsi = NFS_I(inode);
  483. struct nfs_delegation *delegation;
  484. int err = 0;
  485. nfs_wb_all(inode);
  486. delegation = nfs_start_delegation_return(nfsi);
  487. if (delegation != NULL)
  488. err = nfs_end_delegation_return(inode, delegation, 1);
  489. return err;
  490. }
  491. static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
  492. struct nfs_delegation *delegation)
  493. {
  494. set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
  495. set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
  496. }
  497. static void nfs_mark_return_delegation(struct nfs_server *server,
  498. struct nfs_delegation *delegation)
  499. {
  500. set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
  501. set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
  502. }
  503. static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
  504. {
  505. struct nfs_delegation *delegation;
  506. bool ret = false;
  507. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  508. nfs_mark_return_delegation(server, delegation);
  509. ret = true;
  510. }
  511. return ret;
  512. }
  513. static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
  514. {
  515. struct nfs_server *server;
  516. rcu_read_lock();
  517. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  518. nfs_server_mark_return_all_delegations(server);
  519. rcu_read_unlock();
  520. }
  521. static void nfs_delegation_run_state_manager(struct nfs_client *clp)
  522. {
  523. if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
  524. nfs4_schedule_state_manager(clp);
  525. }
  526. /**
  527. * nfs_expire_all_delegations
  528. * @clp: client to process
  529. *
  530. */
  531. void nfs_expire_all_delegations(struct nfs_client *clp)
  532. {
  533. nfs_client_mark_return_all_delegations(clp);
  534. nfs_delegation_run_state_manager(clp);
  535. }
  536. /**
  537. * nfs_super_return_all_delegations - return delegations for one superblock
  538. * @sb: sb to process
  539. *
  540. */
  541. void nfs_server_return_all_delegations(struct nfs_server *server)
  542. {
  543. struct nfs_client *clp = server->nfs_client;
  544. bool need_wait;
  545. if (clp == NULL)
  546. return;
  547. rcu_read_lock();
  548. need_wait = nfs_server_mark_return_all_delegations(server);
  549. rcu_read_unlock();
  550. if (need_wait) {
  551. nfs4_schedule_state_manager(clp);
  552. nfs4_wait_clnt_recover(clp);
  553. }
  554. }
  555. static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
  556. fmode_t flags)
  557. {
  558. struct nfs_delegation *delegation;
  559. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  560. if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
  561. continue;
  562. if (delegation->type & flags)
  563. nfs_mark_return_if_closed_delegation(server, delegation);
  564. }
  565. }
  566. static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
  567. fmode_t flags)
  568. {
  569. struct nfs_server *server;
  570. rcu_read_lock();
  571. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  572. nfs_mark_return_unused_delegation_types(server, flags);
  573. rcu_read_unlock();
  574. }
  575. static void nfs_revoke_delegation(struct inode *inode)
  576. {
  577. struct nfs_delegation *delegation;
  578. rcu_read_lock();
  579. delegation = rcu_dereference(NFS_I(inode)->delegation);
  580. if (delegation != NULL) {
  581. set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
  582. nfs_mark_return_delegation(NFS_SERVER(inode), delegation);
  583. }
  584. rcu_read_unlock();
  585. }
  586. void nfs_remove_bad_delegation(struct inode *inode)
  587. {
  588. struct nfs_delegation *delegation;
  589. nfs_revoke_delegation(inode);
  590. delegation = nfs_inode_detach_delegation(inode);
  591. if (delegation) {
  592. nfs_inode_find_state_and_recover(inode, &delegation->stateid);
  593. nfs_free_delegation(delegation);
  594. }
  595. }
  596. EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
  597. /**
  598. * nfs_expire_unused_delegation_types
  599. * @clp: client to process
  600. * @flags: delegation types to expire
  601. *
  602. */
  603. void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
  604. {
  605. nfs_client_mark_return_unused_delegation_types(clp, flags);
  606. nfs_delegation_run_state_manager(clp);
  607. }
  608. static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
  609. {
  610. struct nfs_delegation *delegation;
  611. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  612. if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
  613. continue;
  614. nfs_mark_return_if_closed_delegation(server, delegation);
  615. }
  616. }
  617. /**
  618. * nfs_expire_unreferenced_delegations - Eliminate unused delegations
  619. * @clp: nfs_client to process
  620. *
  621. */
  622. void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
  623. {
  624. struct nfs_server *server;
  625. rcu_read_lock();
  626. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  627. nfs_mark_return_unreferenced_delegations(server);
  628. rcu_read_unlock();
  629. nfs_delegation_run_state_manager(clp);
  630. }
  631. /**
  632. * nfs_async_inode_return_delegation - asynchronously return a delegation
  633. * @inode: inode to process
  634. * @stateid: state ID information
  635. *
  636. * Returns zero on success, or a negative errno value.
  637. */
  638. int nfs_async_inode_return_delegation(struct inode *inode,
  639. const nfs4_stateid *stateid)
  640. {
  641. struct nfs_server *server = NFS_SERVER(inode);
  642. struct nfs_client *clp = server->nfs_client;
  643. struct nfs_delegation *delegation;
  644. filemap_flush(inode->i_mapping);
  645. rcu_read_lock();
  646. delegation = rcu_dereference(NFS_I(inode)->delegation);
  647. if (delegation == NULL)
  648. goto out_enoent;
  649. if (!clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
  650. goto out_enoent;
  651. nfs_mark_return_delegation(server, delegation);
  652. rcu_read_unlock();
  653. nfs_delegation_run_state_manager(clp);
  654. return 0;
  655. out_enoent:
  656. rcu_read_unlock();
  657. return -ENOENT;
  658. }
  659. static struct inode *
  660. nfs_delegation_find_inode_server(struct nfs_server *server,
  661. const struct nfs_fh *fhandle)
  662. {
  663. struct nfs_delegation *delegation;
  664. struct inode *res = NULL;
  665. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  666. spin_lock(&delegation->lock);
  667. if (delegation->inode != NULL &&
  668. nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
  669. res = igrab(delegation->inode);
  670. }
  671. spin_unlock(&delegation->lock);
  672. if (res != NULL)
  673. break;
  674. }
  675. return res;
  676. }
  677. /**
  678. * nfs_delegation_find_inode - retrieve the inode associated with a delegation
  679. * @clp: client state handle
  680. * @fhandle: filehandle from a delegation recall
  681. *
  682. * Returns pointer to inode matching "fhandle," or NULL if a matching inode
  683. * cannot be found.
  684. */
  685. struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
  686. const struct nfs_fh *fhandle)
  687. {
  688. struct nfs_server *server;
  689. struct inode *res = NULL;
  690. rcu_read_lock();
  691. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  692. res = nfs_delegation_find_inode_server(server, fhandle);
  693. if (res != NULL)
  694. break;
  695. }
  696. rcu_read_unlock();
  697. return res;
  698. }
  699. static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
  700. {
  701. struct nfs_delegation *delegation;
  702. list_for_each_entry_rcu(delegation, &server->delegations, super_list)
  703. set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  704. }
  705. /**
  706. * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
  707. * @clp: nfs_client to process
  708. *
  709. */
  710. void nfs_delegation_mark_reclaim(struct nfs_client *clp)
  711. {
  712. struct nfs_server *server;
  713. rcu_read_lock();
  714. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  715. nfs_delegation_mark_reclaim_server(server);
  716. rcu_read_unlock();
  717. }
  718. /**
  719. * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
  720. * @clp: nfs_client to process
  721. *
  722. */
  723. void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
  724. {
  725. struct nfs_delegation *delegation;
  726. struct nfs_server *server;
  727. struct inode *inode;
  728. restart:
  729. rcu_read_lock();
  730. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  731. list_for_each_entry_rcu(delegation, &server->delegations,
  732. super_list) {
  733. if (test_bit(NFS_DELEGATION_RETURNING,
  734. &delegation->flags))
  735. continue;
  736. if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
  737. &delegation->flags) == 0)
  738. continue;
  739. if (!nfs_sb_active(server->super))
  740. continue;
  741. inode = nfs_delegation_grab_inode(delegation);
  742. if (inode == NULL) {
  743. rcu_read_unlock();
  744. nfs_sb_deactive(server->super);
  745. goto restart;
  746. }
  747. delegation = nfs_start_delegation_return_locked(NFS_I(inode));
  748. rcu_read_unlock();
  749. if (delegation != NULL) {
  750. delegation = nfs_detach_delegation(NFS_I(inode),
  751. delegation, server);
  752. if (delegation != NULL)
  753. nfs_free_delegation(delegation);
  754. }
  755. iput(inode);
  756. nfs_sb_deactive(server->super);
  757. goto restart;
  758. }
  759. }
  760. rcu_read_unlock();
  761. }
  762. /**
  763. * nfs_delegations_present - check for existence of delegations
  764. * @clp: client state handle
  765. *
  766. * Returns one if there are any nfs_delegation structures attached
  767. * to this nfs_client.
  768. */
  769. int nfs_delegations_present(struct nfs_client *clp)
  770. {
  771. struct nfs_server *server;
  772. int ret = 0;
  773. rcu_read_lock();
  774. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  775. if (!list_empty(&server->delegations)) {
  776. ret = 1;
  777. break;
  778. }
  779. rcu_read_unlock();
  780. return ret;
  781. }
  782. /**
  783. * nfs4_copy_delegation_stateid - Copy inode's state ID information
  784. * @dst: stateid data structure to fill in
  785. * @inode: inode to check
  786. * @flags: delegation type requirement
  787. *
  788. * Returns "true" and fills in "dst->data" * if inode had a delegation,
  789. * otherwise "false" is returned.
  790. */
  791. bool nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode,
  792. fmode_t flags)
  793. {
  794. struct nfs_inode *nfsi = NFS_I(inode);
  795. struct nfs_delegation *delegation;
  796. bool ret;
  797. flags &= FMODE_READ|FMODE_WRITE;
  798. rcu_read_lock();
  799. delegation = rcu_dereference(nfsi->delegation);
  800. ret = (delegation != NULL && (delegation->type & flags) == flags);
  801. if (ret) {
  802. nfs4_stateid_copy(dst, &delegation->stateid);
  803. nfs_mark_delegation_referenced(delegation);
  804. }
  805. rcu_read_unlock();
  806. return ret;
  807. }