delegation.c 23 KB

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