delegation.c 18 KB

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