delegation.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. * Allow for upgrades to a WRITE delegation, but
  235. * nothing else.
  236. */
  237. dfprintk(FILE, "%s: server %s handed out "
  238. "a duplicate delegation!\n",
  239. __func__, clp->cl_hostname);
  240. if (delegation->type == old_delegation->type ||
  241. !(delegation->type & FMODE_WRITE)) {
  242. freeme = delegation;
  243. delegation = NULL;
  244. goto out;
  245. }
  246. freeme = nfs_detach_delegation_locked(nfsi, server);
  247. }
  248. list_add_rcu(&delegation->super_list, &server->delegations);
  249. nfsi->delegation_state = delegation->type;
  250. rcu_assign_pointer(nfsi->delegation, delegation);
  251. delegation = NULL;
  252. /* Ensure we revalidate the attributes and page cache! */
  253. spin_lock(&inode->i_lock);
  254. nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
  255. spin_unlock(&inode->i_lock);
  256. out:
  257. spin_unlock(&clp->cl_lock);
  258. if (delegation != NULL)
  259. nfs_free_delegation(delegation);
  260. if (freeme != NULL)
  261. nfs_do_return_delegation(inode, freeme, 0);
  262. return status;
  263. }
  264. /*
  265. * Basic procedure for returning a delegation to the server
  266. */
  267. static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
  268. {
  269. struct nfs_inode *nfsi = NFS_I(inode);
  270. int err;
  271. /*
  272. * Guard against new delegated open/lock/unlock calls and against
  273. * state recovery
  274. */
  275. down_write(&nfsi->rwsem);
  276. err = nfs_delegation_claim_opens(inode, &delegation->stateid);
  277. up_write(&nfsi->rwsem);
  278. if (err)
  279. goto out;
  280. err = nfs_do_return_delegation(inode, delegation, issync);
  281. out:
  282. return err;
  283. }
  284. /**
  285. * nfs_client_return_marked_delegations - return previously marked delegations
  286. * @clp: nfs_client to process
  287. *
  288. * Note that this function is designed to be called by the state
  289. * manager thread. For this reason, it cannot flush the dirty data,
  290. * since that could deadlock in case of a state recovery error.
  291. *
  292. * Returns zero on success, or a negative errno value.
  293. */
  294. int nfs_client_return_marked_delegations(struct nfs_client *clp)
  295. {
  296. struct nfs_delegation *delegation;
  297. struct nfs_server *server;
  298. struct inode *inode;
  299. int err = 0;
  300. restart:
  301. rcu_read_lock();
  302. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  303. list_for_each_entry_rcu(delegation, &server->delegations,
  304. super_list) {
  305. if (!test_and_clear_bit(NFS_DELEGATION_RETURN,
  306. &delegation->flags))
  307. continue;
  308. inode = nfs_delegation_grab_inode(delegation);
  309. if (inode == NULL)
  310. continue;
  311. delegation = nfs_detach_delegation(NFS_I(inode),
  312. server);
  313. rcu_read_unlock();
  314. if (delegation != NULL)
  315. err = __nfs_inode_return_delegation(inode,
  316. delegation, 0);
  317. iput(inode);
  318. if (!err)
  319. goto restart;
  320. set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
  321. return err;
  322. }
  323. }
  324. rcu_read_unlock();
  325. return 0;
  326. }
  327. /**
  328. * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
  329. * @inode: inode to process
  330. *
  331. * Does not protect against delegation reclaims, therefore really only safe
  332. * to be called from nfs4_clear_inode().
  333. */
  334. void nfs_inode_return_delegation_noreclaim(struct inode *inode)
  335. {
  336. struct nfs_server *server = NFS_SERVER(inode);
  337. struct nfs_inode *nfsi = NFS_I(inode);
  338. struct nfs_delegation *delegation;
  339. if (rcu_access_pointer(nfsi->delegation) != NULL) {
  340. delegation = nfs_detach_delegation(nfsi, server);
  341. if (delegation != NULL)
  342. nfs_do_return_delegation(inode, delegation, 0);
  343. }
  344. }
  345. /**
  346. * nfs_inode_return_delegation - synchronously return a delegation
  347. * @inode: inode to process
  348. *
  349. * This routine will always flush any dirty data to disk on the
  350. * assumption that if we need to return the delegation, then
  351. * we should stop caching.
  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. nfs_wb_all(inode);
  362. if (rcu_access_pointer(nfsi->delegation) != NULL) {
  363. delegation = nfs_detach_delegation(nfsi, server);
  364. if (delegation != NULL) {
  365. err = __nfs_inode_return_delegation(inode, delegation, 1);
  366. }
  367. }
  368. return err;
  369. }
  370. static void nfs_mark_return_delegation(struct nfs_server *server,
  371. struct nfs_delegation *delegation)
  372. {
  373. set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
  374. set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->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(server, 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_delegation_run_state_manager(struct nfs_client *clp)
  419. {
  420. if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
  421. nfs4_schedule_state_manager(clp);
  422. }
  423. void nfs_remove_bad_delegation(struct inode *inode)
  424. {
  425. struct nfs_delegation *delegation;
  426. delegation = nfs_detach_delegation(NFS_I(inode), NFS_SERVER(inode));
  427. if (delegation) {
  428. nfs_inode_find_state_and_recover(inode, &delegation->stateid);
  429. nfs_free_delegation(delegation);
  430. }
  431. }
  432. EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
  433. /**
  434. * nfs_expire_all_delegation_types
  435. * @clp: client to process
  436. * @flags: delegation types to expire
  437. *
  438. */
  439. void nfs_expire_all_delegation_types(struct nfs_client *clp, fmode_t flags)
  440. {
  441. nfs_client_mark_return_all_delegation_types(clp, flags);
  442. nfs_delegation_run_state_manager(clp);
  443. }
  444. /**
  445. * nfs_expire_all_delegations
  446. * @clp: client to process
  447. *
  448. */
  449. void nfs_expire_all_delegations(struct nfs_client *clp)
  450. {
  451. nfs_expire_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
  452. }
  453. static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
  454. {
  455. struct nfs_delegation *delegation;
  456. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  457. if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
  458. continue;
  459. nfs_mark_return_delegation(server, delegation);
  460. }
  461. }
  462. /**
  463. * nfs_expire_unreferenced_delegations - Eliminate unused delegations
  464. * @clp: nfs_client to process
  465. *
  466. */
  467. void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
  468. {
  469. struct nfs_server *server;
  470. rcu_read_lock();
  471. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  472. nfs_mark_return_unreferenced_delegations(server);
  473. rcu_read_unlock();
  474. nfs_delegation_run_state_manager(clp);
  475. }
  476. /**
  477. * nfs_async_inode_return_delegation - asynchronously return a delegation
  478. * @inode: inode to process
  479. * @stateid: state ID information
  480. *
  481. * Returns zero on success, or a negative errno value.
  482. */
  483. int nfs_async_inode_return_delegation(struct inode *inode,
  484. const nfs4_stateid *stateid)
  485. {
  486. struct nfs_server *server = NFS_SERVER(inode);
  487. struct nfs_client *clp = server->nfs_client;
  488. struct nfs_delegation *delegation;
  489. filemap_flush(inode->i_mapping);
  490. rcu_read_lock();
  491. delegation = rcu_dereference(NFS_I(inode)->delegation);
  492. if (!clp->cl_mvops->match_stateid(&delegation->stateid, stateid)) {
  493. rcu_read_unlock();
  494. return -ENOENT;
  495. }
  496. nfs_mark_return_delegation(server, delegation);
  497. rcu_read_unlock();
  498. nfs_delegation_run_state_manager(clp);
  499. return 0;
  500. }
  501. static struct inode *
  502. nfs_delegation_find_inode_server(struct nfs_server *server,
  503. const struct nfs_fh *fhandle)
  504. {
  505. struct nfs_delegation *delegation;
  506. struct inode *res = NULL;
  507. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  508. spin_lock(&delegation->lock);
  509. if (delegation->inode != NULL &&
  510. nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
  511. res = igrab(delegation->inode);
  512. }
  513. spin_unlock(&delegation->lock);
  514. if (res != NULL)
  515. break;
  516. }
  517. return res;
  518. }
  519. /**
  520. * nfs_delegation_find_inode - retrieve the inode associated with a delegation
  521. * @clp: client state handle
  522. * @fhandle: filehandle from a delegation recall
  523. *
  524. * Returns pointer to inode matching "fhandle," or NULL if a matching inode
  525. * cannot be found.
  526. */
  527. struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
  528. const struct nfs_fh *fhandle)
  529. {
  530. struct nfs_server *server;
  531. struct inode *res = NULL;
  532. rcu_read_lock();
  533. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  534. res = nfs_delegation_find_inode_server(server, fhandle);
  535. if (res != NULL)
  536. break;
  537. }
  538. rcu_read_unlock();
  539. return res;
  540. }
  541. static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
  542. {
  543. struct nfs_delegation *delegation;
  544. list_for_each_entry_rcu(delegation, &server->delegations, super_list)
  545. set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  546. }
  547. /**
  548. * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
  549. * @clp: nfs_client to process
  550. *
  551. */
  552. void nfs_delegation_mark_reclaim(struct nfs_client *clp)
  553. {
  554. struct nfs_server *server;
  555. rcu_read_lock();
  556. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  557. nfs_delegation_mark_reclaim_server(server);
  558. rcu_read_unlock();
  559. }
  560. /**
  561. * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
  562. * @clp: nfs_client to process
  563. *
  564. */
  565. void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
  566. {
  567. struct nfs_delegation *delegation;
  568. struct nfs_server *server;
  569. struct inode *inode;
  570. restart:
  571. rcu_read_lock();
  572. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  573. list_for_each_entry_rcu(delegation, &server->delegations,
  574. super_list) {
  575. if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
  576. &delegation->flags) == 0)
  577. continue;
  578. inode = nfs_delegation_grab_inode(delegation);
  579. if (inode == NULL)
  580. continue;
  581. delegation = nfs_detach_delegation(NFS_I(inode),
  582. server);
  583. rcu_read_unlock();
  584. if (delegation != NULL)
  585. nfs_free_delegation(delegation);
  586. iput(inode);
  587. goto restart;
  588. }
  589. }
  590. rcu_read_unlock();
  591. }
  592. /**
  593. * nfs_delegations_present - check for existence of delegations
  594. * @clp: client state handle
  595. *
  596. * Returns one if there are any nfs_delegation structures attached
  597. * to this nfs_client.
  598. */
  599. int nfs_delegations_present(struct nfs_client *clp)
  600. {
  601. struct nfs_server *server;
  602. int ret = 0;
  603. rcu_read_lock();
  604. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  605. if (!list_empty(&server->delegations)) {
  606. ret = 1;
  607. break;
  608. }
  609. rcu_read_unlock();
  610. return ret;
  611. }
  612. /**
  613. * nfs4_copy_delegation_stateid - Copy inode's state ID information
  614. * @dst: stateid data structure to fill in
  615. * @inode: inode to check
  616. * @flags: delegation type requirement
  617. *
  618. * Returns "true" and fills in "dst->data" * if inode had a delegation,
  619. * otherwise "false" is returned.
  620. */
  621. bool nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode,
  622. fmode_t flags)
  623. {
  624. struct nfs_inode *nfsi = NFS_I(inode);
  625. struct nfs_delegation *delegation;
  626. bool ret;
  627. flags &= FMODE_READ|FMODE_WRITE;
  628. rcu_read_lock();
  629. delegation = rcu_dereference(nfsi->delegation);
  630. ret = (delegation != NULL && (delegation->type & flags) == flags);
  631. if (ret) {
  632. nfs4_stateid_copy(dst, &delegation->stateid);
  633. nfs_mark_delegation_referenced(delegation);
  634. }
  635. rcu_read_unlock();
  636. return ret;
  637. }