delegation.c 21 KB

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