delegation.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. rcu_read_unlock();
  139. put_rpccred(oldcred);
  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. * Returns zero on success, or a negative errno value.
  289. */
  290. int nfs_client_return_marked_delegations(struct nfs_client *clp)
  291. {
  292. struct nfs_delegation *delegation;
  293. struct nfs_server *server;
  294. struct inode *inode;
  295. int err = 0;
  296. restart:
  297. rcu_read_lock();
  298. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  299. list_for_each_entry_rcu(delegation, &server->delegations,
  300. super_list) {
  301. if (!test_and_clear_bit(NFS_DELEGATION_RETURN,
  302. &delegation->flags))
  303. continue;
  304. inode = nfs_delegation_grab_inode(delegation);
  305. if (inode == NULL)
  306. continue;
  307. delegation = nfs_detach_delegation(NFS_I(inode),
  308. server);
  309. rcu_read_unlock();
  310. if (delegation != NULL) {
  311. filemap_flush(inode->i_mapping);
  312. err = __nfs_inode_return_delegation(inode,
  313. delegation, 0);
  314. }
  315. iput(inode);
  316. if (!err)
  317. goto restart;
  318. set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
  319. return err;
  320. }
  321. }
  322. rcu_read_unlock();
  323. return 0;
  324. }
  325. /**
  326. * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
  327. * @inode: inode to process
  328. *
  329. * Does not protect against delegation reclaims, therefore really only safe
  330. * to be called from nfs4_clear_inode().
  331. */
  332. void nfs_inode_return_delegation_noreclaim(struct inode *inode)
  333. {
  334. struct nfs_server *server = NFS_SERVER(inode);
  335. struct nfs_inode *nfsi = NFS_I(inode);
  336. struct nfs_delegation *delegation;
  337. if (rcu_access_pointer(nfsi->delegation) != NULL) {
  338. delegation = nfs_detach_delegation(nfsi, server);
  339. if (delegation != NULL)
  340. nfs_do_return_delegation(inode, delegation, 0);
  341. }
  342. }
  343. /**
  344. * nfs_inode_return_delegation - synchronously return a delegation
  345. * @inode: inode to process
  346. *
  347. * Returns zero on success, or a negative errno value.
  348. */
  349. int nfs_inode_return_delegation(struct inode *inode)
  350. {
  351. struct nfs_server *server = NFS_SERVER(inode);
  352. struct nfs_inode *nfsi = NFS_I(inode);
  353. struct nfs_delegation *delegation;
  354. int err = 0;
  355. if (rcu_access_pointer(nfsi->delegation) != NULL) {
  356. delegation = nfs_detach_delegation(nfsi, server);
  357. if (delegation != NULL) {
  358. nfs_wb_all(inode);
  359. err = __nfs_inode_return_delegation(inode, delegation, 1);
  360. }
  361. }
  362. return err;
  363. }
  364. static void nfs_mark_return_delegation(struct nfs_server *server,
  365. struct nfs_delegation *delegation)
  366. {
  367. set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
  368. set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
  369. }
  370. /**
  371. * nfs_super_return_all_delegations - return delegations for one superblock
  372. * @sb: sb to process
  373. *
  374. */
  375. void nfs_super_return_all_delegations(struct super_block *sb)
  376. {
  377. struct nfs_server *server = NFS_SB(sb);
  378. struct nfs_client *clp = server->nfs_client;
  379. struct nfs_delegation *delegation;
  380. if (clp == NULL)
  381. return;
  382. rcu_read_lock();
  383. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  384. spin_lock(&delegation->lock);
  385. set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
  386. spin_unlock(&delegation->lock);
  387. }
  388. rcu_read_unlock();
  389. if (nfs_client_return_marked_delegations(clp) != 0)
  390. nfs4_schedule_state_manager(clp);
  391. }
  392. static void nfs_mark_return_all_delegation_types(struct nfs_server *server,
  393. fmode_t flags)
  394. {
  395. struct nfs_delegation *delegation;
  396. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  397. if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
  398. continue;
  399. if (delegation->type & flags)
  400. nfs_mark_return_delegation(server, delegation);
  401. }
  402. }
  403. static void nfs_client_mark_return_all_delegation_types(struct nfs_client *clp,
  404. fmode_t flags)
  405. {
  406. struct nfs_server *server;
  407. rcu_read_lock();
  408. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  409. nfs_mark_return_all_delegation_types(server, flags);
  410. rcu_read_unlock();
  411. }
  412. static void nfs_delegation_run_state_manager(struct nfs_client *clp)
  413. {
  414. if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
  415. nfs4_schedule_state_manager(clp);
  416. }
  417. void nfs_remove_bad_delegation(struct inode *inode)
  418. {
  419. struct nfs_delegation *delegation;
  420. delegation = nfs_detach_delegation(NFS_I(inode), NFS_SERVER(inode));
  421. if (delegation) {
  422. nfs_inode_find_state_and_recover(inode, &delegation->stateid);
  423. nfs_free_delegation(delegation);
  424. }
  425. }
  426. EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
  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. static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
  448. {
  449. struct nfs_delegation *delegation;
  450. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  451. if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
  452. continue;
  453. nfs_mark_return_delegation(server, delegation);
  454. }
  455. }
  456. /**
  457. * nfs_expire_unreferenced_delegations - Eliminate unused delegations
  458. * @clp: nfs_client to process
  459. *
  460. */
  461. void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
  462. {
  463. struct nfs_server *server;
  464. rcu_read_lock();
  465. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  466. nfs_mark_return_unreferenced_delegations(server);
  467. rcu_read_unlock();
  468. nfs_delegation_run_state_manager(clp);
  469. }
  470. /**
  471. * nfs_async_inode_return_delegation - asynchronously return a delegation
  472. * @inode: inode to process
  473. * @stateid: state ID information
  474. *
  475. * Returns zero on success, or a negative errno value.
  476. */
  477. int nfs_async_inode_return_delegation(struct inode *inode,
  478. const nfs4_stateid *stateid)
  479. {
  480. struct nfs_server *server = NFS_SERVER(inode);
  481. struct nfs_client *clp = server->nfs_client;
  482. struct nfs_delegation *delegation;
  483. rcu_read_lock();
  484. delegation = rcu_dereference(NFS_I(inode)->delegation);
  485. if (delegation == NULL)
  486. goto out_enoent;
  487. if (!clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
  488. goto out_enoent;
  489. nfs_mark_return_delegation(server, delegation);
  490. rcu_read_unlock();
  491. nfs_delegation_run_state_manager(clp);
  492. return 0;
  493. out_enoent:
  494. rcu_read_unlock();
  495. return -ENOENT;
  496. }
  497. static struct inode *
  498. nfs_delegation_find_inode_server(struct nfs_server *server,
  499. const struct nfs_fh *fhandle)
  500. {
  501. struct nfs_delegation *delegation;
  502. struct inode *res = NULL;
  503. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  504. spin_lock(&delegation->lock);
  505. if (delegation->inode != NULL &&
  506. nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
  507. res = igrab(delegation->inode);
  508. }
  509. spin_unlock(&delegation->lock);
  510. if (res != NULL)
  511. break;
  512. }
  513. return res;
  514. }
  515. /**
  516. * nfs_delegation_find_inode - retrieve the inode associated with a delegation
  517. * @clp: client state handle
  518. * @fhandle: filehandle from a delegation recall
  519. *
  520. * Returns pointer to inode matching "fhandle," or NULL if a matching inode
  521. * cannot be found.
  522. */
  523. struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
  524. const struct nfs_fh *fhandle)
  525. {
  526. struct nfs_server *server;
  527. struct inode *res = NULL;
  528. rcu_read_lock();
  529. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  530. res = nfs_delegation_find_inode_server(server, fhandle);
  531. if (res != NULL)
  532. break;
  533. }
  534. rcu_read_unlock();
  535. return res;
  536. }
  537. static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
  538. {
  539. struct nfs_delegation *delegation;
  540. list_for_each_entry_rcu(delegation, &server->delegations, super_list)
  541. set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  542. }
  543. /**
  544. * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
  545. * @clp: nfs_client to process
  546. *
  547. */
  548. void nfs_delegation_mark_reclaim(struct nfs_client *clp)
  549. {
  550. struct nfs_server *server;
  551. rcu_read_lock();
  552. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  553. nfs_delegation_mark_reclaim_server(server);
  554. rcu_read_unlock();
  555. }
  556. /**
  557. * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
  558. * @clp: nfs_client to process
  559. *
  560. */
  561. void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
  562. {
  563. struct nfs_delegation *delegation;
  564. struct nfs_server *server;
  565. struct inode *inode;
  566. restart:
  567. rcu_read_lock();
  568. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  569. list_for_each_entry_rcu(delegation, &server->delegations,
  570. super_list) {
  571. if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
  572. &delegation->flags) == 0)
  573. continue;
  574. inode = nfs_delegation_grab_inode(delegation);
  575. if (inode == NULL)
  576. continue;
  577. delegation = nfs_detach_delegation(NFS_I(inode),
  578. server);
  579. rcu_read_unlock();
  580. if (delegation != NULL)
  581. nfs_free_delegation(delegation);
  582. iput(inode);
  583. goto restart;
  584. }
  585. }
  586. rcu_read_unlock();
  587. }
  588. /**
  589. * nfs_delegations_present - check for existence of delegations
  590. * @clp: client state handle
  591. *
  592. * Returns one if there are any nfs_delegation structures attached
  593. * to this nfs_client.
  594. */
  595. int nfs_delegations_present(struct nfs_client *clp)
  596. {
  597. struct nfs_server *server;
  598. int ret = 0;
  599. rcu_read_lock();
  600. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  601. if (!list_empty(&server->delegations)) {
  602. ret = 1;
  603. break;
  604. }
  605. rcu_read_unlock();
  606. return ret;
  607. }
  608. /**
  609. * nfs4_copy_delegation_stateid - Copy inode's state ID information
  610. * @dst: stateid data structure to fill in
  611. * @inode: inode to check
  612. * @flags: delegation type requirement
  613. *
  614. * Returns "true" and fills in "dst->data" * if inode had a delegation,
  615. * otherwise "false" is returned.
  616. */
  617. bool nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode,
  618. fmode_t flags)
  619. {
  620. struct nfs_inode *nfsi = NFS_I(inode);
  621. struct nfs_delegation *delegation;
  622. bool ret;
  623. flags &= FMODE_READ|FMODE_WRITE;
  624. rcu_read_lock();
  625. delegation = rcu_dereference(nfsi->delegation);
  626. ret = (delegation != NULL && (delegation->type & flags) == flags);
  627. if (ret) {
  628. nfs4_stateid_copy(dst, &delegation->stateid);
  629. nfs_mark_delegation_referenced(delegation);
  630. }
  631. rcu_read_unlock();
  632. return ret;
  633. }