delegation.c 21 KB

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