delegation.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * linux/fs/nfs/delegation.c
  3. *
  4. * Copyright (C) 2004 Trond Myklebust
  5. *
  6. * NFS file delegation management
  7. *
  8. */
  9. #include <linux/completion.h>
  10. #include <linux/kthread.h>
  11. #include <linux/module.h>
  12. #include <linux/sched.h>
  13. #include <linux/slab.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/nfs4.h>
  16. #include <linux/nfs_fs.h>
  17. #include <linux/nfs_xdr.h>
  18. #include "nfs4_fs.h"
  19. #include "delegation.h"
  20. #include "internal.h"
  21. static void nfs_free_delegation(struct nfs_delegation *delegation)
  22. {
  23. if (delegation->cred) {
  24. put_rpccred(delegation->cred);
  25. delegation->cred = NULL;
  26. }
  27. kfree_rcu(delegation, rcu);
  28. }
  29. /**
  30. * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
  31. * @delegation: delegation to process
  32. *
  33. */
  34. void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
  35. {
  36. set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
  37. }
  38. /**
  39. * nfs_have_delegation - check if inode has a delegation
  40. * @inode: inode to check
  41. * @flags: delegation types to check for
  42. *
  43. * Returns one if inode has the indicated delegation, otherwise zero.
  44. */
  45. int nfs4_have_delegation(struct inode *inode, fmode_t flags)
  46. {
  47. struct nfs_delegation *delegation;
  48. int ret = 0;
  49. flags &= FMODE_READ|FMODE_WRITE;
  50. rcu_read_lock();
  51. delegation = rcu_dereference(NFS_I(inode)->delegation);
  52. if (delegation != NULL && (delegation->type & flags) == flags) {
  53. 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 nfs4_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_server_return_all_delegations(struct nfs_server *server)
  382. {
  383. struct nfs_client *clp = server->nfs_client;
  384. struct nfs_delegation *delegation;
  385. if (clp == NULL)
  386. return;
  387. rcu_read_lock();
  388. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  389. spin_lock(&delegation->lock);
  390. set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
  391. spin_unlock(&delegation->lock);
  392. }
  393. rcu_read_unlock();
  394. if (nfs_client_return_marked_delegations(clp) != 0)
  395. nfs4_schedule_state_manager(clp);
  396. }
  397. static void nfs_mark_return_all_delegation_types(struct nfs_server *server,
  398. fmode_t flags)
  399. {
  400. struct nfs_delegation *delegation;
  401. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  402. if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
  403. continue;
  404. if (delegation->type & flags)
  405. nfs_mark_return_delegation(server, delegation);
  406. }
  407. }
  408. static void nfs_client_mark_return_all_delegation_types(struct nfs_client *clp,
  409. fmode_t flags)
  410. {
  411. struct nfs_server *server;
  412. rcu_read_lock();
  413. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  414. nfs_mark_return_all_delegation_types(server, flags);
  415. rcu_read_unlock();
  416. }
  417. static void nfs_delegation_run_state_manager(struct nfs_client *clp)
  418. {
  419. if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
  420. nfs4_schedule_state_manager(clp);
  421. }
  422. void nfs_remove_bad_delegation(struct inode *inode)
  423. {
  424. struct nfs_delegation *delegation;
  425. delegation = nfs_detach_delegation(NFS_I(inode), NFS_SERVER(inode));
  426. if (delegation) {
  427. nfs_inode_find_state_and_recover(inode, &delegation->stateid);
  428. nfs_free_delegation(delegation);
  429. }
  430. }
  431. EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
  432. /**
  433. * nfs_expire_all_delegation_types
  434. * @clp: client to process
  435. * @flags: delegation types to expire
  436. *
  437. */
  438. void nfs_expire_all_delegation_types(struct nfs_client *clp, fmode_t flags)
  439. {
  440. nfs_client_mark_return_all_delegation_types(clp, flags);
  441. nfs_delegation_run_state_manager(clp);
  442. }
  443. /**
  444. * nfs_expire_all_delegations
  445. * @clp: client to process
  446. *
  447. */
  448. void nfs_expire_all_delegations(struct nfs_client *clp)
  449. {
  450. nfs_expire_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
  451. }
  452. static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
  453. {
  454. struct nfs_delegation *delegation;
  455. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  456. if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
  457. continue;
  458. nfs_mark_return_delegation(server, delegation);
  459. }
  460. }
  461. /**
  462. * nfs_expire_unreferenced_delegations - Eliminate unused delegations
  463. * @clp: nfs_client to process
  464. *
  465. */
  466. void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
  467. {
  468. struct nfs_server *server;
  469. rcu_read_lock();
  470. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  471. nfs_mark_return_unreferenced_delegations(server);
  472. rcu_read_unlock();
  473. nfs_delegation_run_state_manager(clp);
  474. }
  475. /**
  476. * nfs_async_inode_return_delegation - asynchronously return a delegation
  477. * @inode: inode to process
  478. * @stateid: state ID information
  479. *
  480. * Returns zero on success, or a negative errno value.
  481. */
  482. int nfs_async_inode_return_delegation(struct inode *inode,
  483. const nfs4_stateid *stateid)
  484. {
  485. struct nfs_server *server = NFS_SERVER(inode);
  486. struct nfs_client *clp = server->nfs_client;
  487. struct nfs_delegation *delegation;
  488. filemap_flush(inode->i_mapping);
  489. rcu_read_lock();
  490. delegation = rcu_dereference(NFS_I(inode)->delegation);
  491. if (!clp->cl_mvops->match_stateid(&delegation->stateid, stateid)) {
  492. rcu_read_unlock();
  493. return -ENOENT;
  494. }
  495. nfs_mark_return_delegation(server, delegation);
  496. rcu_read_unlock();
  497. nfs_delegation_run_state_manager(clp);
  498. return 0;
  499. }
  500. static struct inode *
  501. nfs_delegation_find_inode_server(struct nfs_server *server,
  502. const struct nfs_fh *fhandle)
  503. {
  504. struct nfs_delegation *delegation;
  505. struct inode *res = NULL;
  506. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  507. spin_lock(&delegation->lock);
  508. if (delegation->inode != NULL &&
  509. nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
  510. res = igrab(delegation->inode);
  511. }
  512. spin_unlock(&delegation->lock);
  513. if (res != NULL)
  514. break;
  515. }
  516. return res;
  517. }
  518. /**
  519. * nfs_delegation_find_inode - retrieve the inode associated with a delegation
  520. * @clp: client state handle
  521. * @fhandle: filehandle from a delegation recall
  522. *
  523. * Returns pointer to inode matching "fhandle," or NULL if a matching inode
  524. * cannot be found.
  525. */
  526. struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
  527. const struct nfs_fh *fhandle)
  528. {
  529. struct nfs_server *server;
  530. struct inode *res = NULL;
  531. rcu_read_lock();
  532. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  533. res = nfs_delegation_find_inode_server(server, fhandle);
  534. if (res != NULL)
  535. break;
  536. }
  537. rcu_read_unlock();
  538. return res;
  539. }
  540. static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
  541. {
  542. struct nfs_delegation *delegation;
  543. list_for_each_entry_rcu(delegation, &server->delegations, super_list)
  544. set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  545. }
  546. /**
  547. * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
  548. * @clp: nfs_client to process
  549. *
  550. */
  551. void nfs_delegation_mark_reclaim(struct nfs_client *clp)
  552. {
  553. struct nfs_server *server;
  554. rcu_read_lock();
  555. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  556. nfs_delegation_mark_reclaim_server(server);
  557. rcu_read_unlock();
  558. }
  559. /**
  560. * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
  561. * @clp: nfs_client to process
  562. *
  563. */
  564. void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
  565. {
  566. struct nfs_delegation *delegation;
  567. struct nfs_server *server;
  568. struct inode *inode;
  569. restart:
  570. rcu_read_lock();
  571. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  572. list_for_each_entry_rcu(delegation, &server->delegations,
  573. super_list) {
  574. if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
  575. &delegation->flags) == 0)
  576. continue;
  577. inode = nfs_delegation_grab_inode(delegation);
  578. if (inode == NULL)
  579. continue;
  580. delegation = nfs_detach_delegation(NFS_I(inode),
  581. server);
  582. rcu_read_unlock();
  583. if (delegation != NULL)
  584. nfs_free_delegation(delegation);
  585. iput(inode);
  586. goto restart;
  587. }
  588. }
  589. rcu_read_unlock();
  590. }
  591. /**
  592. * nfs_delegations_present - check for existence of delegations
  593. * @clp: client state handle
  594. *
  595. * Returns one if there are any nfs_delegation structures attached
  596. * to this nfs_client.
  597. */
  598. int nfs_delegations_present(struct nfs_client *clp)
  599. {
  600. struct nfs_server *server;
  601. int ret = 0;
  602. rcu_read_lock();
  603. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  604. if (!list_empty(&server->delegations)) {
  605. ret = 1;
  606. break;
  607. }
  608. rcu_read_unlock();
  609. return ret;
  610. }
  611. /**
  612. * nfs4_copy_delegation_stateid - Copy inode's state ID information
  613. * @dst: stateid data structure to fill in
  614. * @inode: inode to check
  615. * @flags: delegation type requirement
  616. *
  617. * Returns "true" and fills in "dst->data" * if inode had a delegation,
  618. * otherwise "false" is returned.
  619. */
  620. bool nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode,
  621. fmode_t flags)
  622. {
  623. struct nfs_inode *nfsi = NFS_I(inode);
  624. struct nfs_delegation *delegation;
  625. bool ret;
  626. flags &= FMODE_READ|FMODE_WRITE;
  627. rcu_read_lock();
  628. delegation = rcu_dereference(nfsi->delegation);
  629. ret = (delegation != NULL && (delegation->type & flags) == flags);
  630. if (ret) {
  631. nfs4_stateid_copy(dst, &delegation->stateid);
  632. nfs_mark_delegation_referenced(delegation);
  633. }
  634. rcu_read_unlock();
  635. return ret;
  636. }