delegation.c 22 KB

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