delegation.c 20 KB

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