delegation.c 18 KB

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