delegation.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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. /**
  40. * nfs_have_delegation - check if inode has a delegation
  41. * @inode: inode to check
  42. * @flags: delegation types to check for
  43. *
  44. * Returns one if inode has the indicated delegation, otherwise zero.
  45. */
  46. int nfs4_have_delegation(struct inode *inode, fmode_t flags)
  47. {
  48. struct nfs_delegation *delegation;
  49. int ret = 0;
  50. flags &= FMODE_READ|FMODE_WRITE;
  51. rcu_read_lock();
  52. delegation = rcu_dereference(NFS_I(inode)->delegation);
  53. if (delegation != NULL && (delegation->type & flags) == flags &&
  54. !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
  55. nfs_mark_delegation_referenced(delegation);
  56. ret = 1;
  57. }
  58. rcu_read_unlock();
  59. return ret;
  60. }
  61. static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
  62. {
  63. struct inode *inode = state->inode;
  64. struct file_lock *fl;
  65. int status = 0;
  66. if (inode->i_flock == NULL)
  67. goto out;
  68. /* Protect inode->i_flock using the i_lock */
  69. spin_lock(&inode->i_lock);
  70. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  71. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  72. continue;
  73. if (nfs_file_open_context(fl->fl_file) != ctx)
  74. continue;
  75. spin_unlock(&inode->i_lock);
  76. status = nfs4_lock_delegation_recall(fl, state, stateid);
  77. if (status < 0)
  78. goto out;
  79. spin_lock(&inode->i_lock);
  80. }
  81. spin_unlock(&inode->i_lock);
  82. out:
  83. return status;
  84. }
  85. static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
  86. {
  87. struct nfs_inode *nfsi = NFS_I(inode);
  88. struct nfs_open_context *ctx;
  89. struct nfs4_state_owner *sp;
  90. struct nfs4_state *state;
  91. unsigned int seq;
  92. int err;
  93. again:
  94. spin_lock(&inode->i_lock);
  95. list_for_each_entry(ctx, &nfsi->open_files, list) {
  96. state = ctx->state;
  97. if (state == NULL)
  98. continue;
  99. if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
  100. continue;
  101. if (!nfs4_stateid_match(&state->stateid, stateid))
  102. continue;
  103. get_nfs_open_context(ctx);
  104. spin_unlock(&inode->i_lock);
  105. sp = state->owner;
  106. /* Block nfs4_proc_unlck */
  107. mutex_lock(&sp->so_delegreturn_mutex);
  108. seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
  109. err = nfs4_open_delegation_recall(ctx, state, stateid);
  110. if (!err)
  111. err = nfs_delegation_claim_locks(ctx, state, stateid);
  112. if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
  113. err = -EAGAIN;
  114. mutex_unlock(&sp->so_delegreturn_mutex);
  115. put_nfs_open_context(ctx);
  116. if (err != 0)
  117. return err;
  118. goto again;
  119. }
  120. spin_unlock(&inode->i_lock);
  121. return 0;
  122. }
  123. /**
  124. * nfs_inode_reclaim_delegation - process a delegation reclaim request
  125. * @inode: inode to process
  126. * @cred: credential to use for request
  127. * @res: new delegation state from server
  128. *
  129. */
  130. void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred,
  131. struct nfs_openres *res)
  132. {
  133. struct nfs_delegation *delegation;
  134. struct rpc_cred *oldcred = NULL;
  135. rcu_read_lock();
  136. delegation = rcu_dereference(NFS_I(inode)->delegation);
  137. if (delegation != NULL) {
  138. spin_lock(&delegation->lock);
  139. if (delegation->inode != NULL) {
  140. nfs4_stateid_copy(&delegation->stateid, &res->delegation);
  141. delegation->type = res->delegation_type;
  142. delegation->maxsize = res->maxsize;
  143. oldcred = delegation->cred;
  144. delegation->cred = get_rpccred(cred);
  145. clear_bit(NFS_DELEGATION_NEED_RECLAIM,
  146. &delegation->flags);
  147. NFS_I(inode)->delegation_state = delegation->type;
  148. spin_unlock(&delegation->lock);
  149. put_rpccred(oldcred);
  150. rcu_read_unlock();
  151. trace_nfs4_reclaim_delegation(inode, res->delegation_type);
  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. trace_nfs4_set_delegation(inode, res->delegation_type);
  316. out:
  317. spin_unlock(&clp->cl_lock);
  318. if (delegation != NULL)
  319. nfs_free_delegation(delegation);
  320. if (freeme != NULL)
  321. nfs_do_return_delegation(inode, freeme, 0);
  322. return status;
  323. }
  324. /*
  325. * Basic procedure for returning a delegation to the server
  326. */
  327. static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
  328. {
  329. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  330. struct nfs_inode *nfsi = NFS_I(inode);
  331. int err;
  332. if (delegation == NULL)
  333. return 0;
  334. do {
  335. err = nfs_delegation_claim_opens(inode, &delegation->stateid);
  336. if (!issync || err != -EAGAIN)
  337. break;
  338. /*
  339. * Guard against state recovery
  340. */
  341. err = nfs4_wait_clnt_recover(clp);
  342. } while (err == 0);
  343. if (err) {
  344. nfs_abort_delegation_return(delegation, clp);
  345. goto out;
  346. }
  347. if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
  348. goto out;
  349. err = nfs_do_return_delegation(inode, delegation, issync);
  350. out:
  351. return err;
  352. }
  353. static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
  354. {
  355. bool ret = false;
  356. if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
  357. ret = true;
  358. if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
  359. struct inode *inode;
  360. spin_lock(&delegation->lock);
  361. inode = delegation->inode;
  362. if (inode && list_empty(&NFS_I(inode)->open_files))
  363. ret = true;
  364. spin_unlock(&delegation->lock);
  365. }
  366. return ret;
  367. }
  368. /**
  369. * nfs_client_return_marked_delegations - return previously marked delegations
  370. * @clp: nfs_client to process
  371. *
  372. * Note that this function is designed to be called by the state
  373. * manager thread. For this reason, it cannot flush the dirty data,
  374. * since that could deadlock in case of a state recovery error.
  375. *
  376. * Returns zero on success, or a negative errno value.
  377. */
  378. int nfs_client_return_marked_delegations(struct nfs_client *clp)
  379. {
  380. struct nfs_delegation *delegation;
  381. struct nfs_server *server;
  382. struct inode *inode;
  383. int err = 0;
  384. restart:
  385. rcu_read_lock();
  386. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  387. list_for_each_entry_rcu(delegation, &server->delegations,
  388. super_list) {
  389. if (!nfs_delegation_need_return(delegation))
  390. continue;
  391. inode = nfs_delegation_grab_inode(delegation);
  392. if (inode == NULL)
  393. continue;
  394. delegation = nfs_start_delegation_return_locked(NFS_I(inode));
  395. rcu_read_unlock();
  396. err = nfs_end_delegation_return(inode, delegation, 0);
  397. iput(inode);
  398. if (!err)
  399. goto restart;
  400. set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
  401. return err;
  402. }
  403. }
  404. rcu_read_unlock();
  405. return 0;
  406. }
  407. /**
  408. * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
  409. * @inode: inode to process
  410. *
  411. * Does not protect against delegation reclaims, therefore really only safe
  412. * to be called from nfs4_clear_inode().
  413. */
  414. void nfs_inode_return_delegation_noreclaim(struct inode *inode)
  415. {
  416. struct nfs_delegation *delegation;
  417. delegation = nfs_inode_detach_delegation(inode);
  418. if (delegation != NULL)
  419. nfs_do_return_delegation(inode, delegation, 0);
  420. }
  421. /**
  422. * nfs_inode_return_delegation - synchronously return a delegation
  423. * @inode: inode to process
  424. *
  425. * This routine will always flush any dirty data to disk on the
  426. * assumption that if we need to return the delegation, then
  427. * we should stop caching.
  428. *
  429. * Returns zero on success, or a negative errno value.
  430. */
  431. int nfs4_inode_return_delegation(struct inode *inode)
  432. {
  433. struct nfs_inode *nfsi = NFS_I(inode);
  434. struct nfs_delegation *delegation;
  435. int err = 0;
  436. nfs_wb_all(inode);
  437. delegation = nfs_start_delegation_return(nfsi);
  438. if (delegation != NULL)
  439. err = nfs_end_delegation_return(inode, delegation, 1);
  440. return err;
  441. }
  442. static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
  443. struct nfs_delegation *delegation)
  444. {
  445. set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
  446. set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
  447. }
  448. static void nfs_mark_return_delegation(struct nfs_server *server,
  449. struct nfs_delegation *delegation)
  450. {
  451. set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
  452. set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
  453. }
  454. static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
  455. {
  456. struct nfs_delegation *delegation;
  457. bool ret = false;
  458. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  459. nfs_mark_return_delegation(server, delegation);
  460. ret = true;
  461. }
  462. return ret;
  463. }
  464. static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
  465. {
  466. struct nfs_server *server;
  467. rcu_read_lock();
  468. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  469. nfs_server_mark_return_all_delegations(server);
  470. rcu_read_unlock();
  471. }
  472. static void nfs_delegation_run_state_manager(struct nfs_client *clp)
  473. {
  474. if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
  475. nfs4_schedule_state_manager(clp);
  476. }
  477. /**
  478. * nfs_expire_all_delegations
  479. * @clp: client to process
  480. *
  481. */
  482. void nfs_expire_all_delegations(struct nfs_client *clp)
  483. {
  484. nfs_client_mark_return_all_delegations(clp);
  485. nfs_delegation_run_state_manager(clp);
  486. }
  487. /**
  488. * nfs_super_return_all_delegations - return delegations for one superblock
  489. * @sb: sb to process
  490. *
  491. */
  492. void nfs_server_return_all_delegations(struct nfs_server *server)
  493. {
  494. struct nfs_client *clp = server->nfs_client;
  495. bool need_wait;
  496. if (clp == NULL)
  497. return;
  498. rcu_read_lock();
  499. need_wait = nfs_server_mark_return_all_delegations(server);
  500. rcu_read_unlock();
  501. if (need_wait) {
  502. nfs4_schedule_state_manager(clp);
  503. nfs4_wait_clnt_recover(clp);
  504. }
  505. }
  506. static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
  507. fmode_t flags)
  508. {
  509. struct nfs_delegation *delegation;
  510. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  511. if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
  512. continue;
  513. if (delegation->type & flags)
  514. nfs_mark_return_if_closed_delegation(server, delegation);
  515. }
  516. }
  517. static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
  518. fmode_t flags)
  519. {
  520. struct nfs_server *server;
  521. rcu_read_lock();
  522. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  523. nfs_mark_return_unused_delegation_types(server, flags);
  524. rcu_read_unlock();
  525. }
  526. void nfs_remove_bad_delegation(struct inode *inode)
  527. {
  528. struct nfs_delegation *delegation;
  529. delegation = nfs_inode_detach_delegation(inode);
  530. if (delegation) {
  531. nfs_inode_find_state_and_recover(inode, &delegation->stateid);
  532. nfs_free_delegation(delegation);
  533. }
  534. }
  535. EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
  536. /**
  537. * nfs_expire_unused_delegation_types
  538. * @clp: client to process
  539. * @flags: delegation types to expire
  540. *
  541. */
  542. void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
  543. {
  544. nfs_client_mark_return_unused_delegation_types(clp, flags);
  545. nfs_delegation_run_state_manager(clp);
  546. }
  547. static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
  548. {
  549. struct nfs_delegation *delegation;
  550. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  551. if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
  552. continue;
  553. nfs_mark_return_if_closed_delegation(server, delegation);
  554. }
  555. }
  556. /**
  557. * nfs_expire_unreferenced_delegations - Eliminate unused delegations
  558. * @clp: nfs_client to process
  559. *
  560. */
  561. void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
  562. {
  563. struct nfs_server *server;
  564. rcu_read_lock();
  565. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  566. nfs_mark_return_unreferenced_delegations(server);
  567. rcu_read_unlock();
  568. nfs_delegation_run_state_manager(clp);
  569. }
  570. /**
  571. * nfs_async_inode_return_delegation - asynchronously return a delegation
  572. * @inode: inode to process
  573. * @stateid: state ID information
  574. *
  575. * Returns zero on success, or a negative errno value.
  576. */
  577. int nfs_async_inode_return_delegation(struct inode *inode,
  578. const nfs4_stateid *stateid)
  579. {
  580. struct nfs_server *server = NFS_SERVER(inode);
  581. struct nfs_client *clp = server->nfs_client;
  582. struct nfs_delegation *delegation;
  583. filemap_flush(inode->i_mapping);
  584. rcu_read_lock();
  585. delegation = rcu_dereference(NFS_I(inode)->delegation);
  586. if (!clp->cl_mvops->match_stateid(&delegation->stateid, stateid)) {
  587. rcu_read_unlock();
  588. return -ENOENT;
  589. }
  590. nfs_mark_return_delegation(server, delegation);
  591. rcu_read_unlock();
  592. nfs_delegation_run_state_manager(clp);
  593. return 0;
  594. }
  595. static struct inode *
  596. nfs_delegation_find_inode_server(struct nfs_server *server,
  597. const struct nfs_fh *fhandle)
  598. {
  599. struct nfs_delegation *delegation;
  600. struct inode *res = NULL;
  601. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  602. spin_lock(&delegation->lock);
  603. if (delegation->inode != NULL &&
  604. nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
  605. res = igrab(delegation->inode);
  606. }
  607. spin_unlock(&delegation->lock);
  608. if (res != NULL)
  609. break;
  610. }
  611. return res;
  612. }
  613. /**
  614. * nfs_delegation_find_inode - retrieve the inode associated with a delegation
  615. * @clp: client state handle
  616. * @fhandle: filehandle from a delegation recall
  617. *
  618. * Returns pointer to inode matching "fhandle," or NULL if a matching inode
  619. * cannot be found.
  620. */
  621. struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
  622. const struct nfs_fh *fhandle)
  623. {
  624. struct nfs_server *server;
  625. struct inode *res = NULL;
  626. rcu_read_lock();
  627. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  628. res = nfs_delegation_find_inode_server(server, fhandle);
  629. if (res != NULL)
  630. break;
  631. }
  632. rcu_read_unlock();
  633. return res;
  634. }
  635. static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
  636. {
  637. struct nfs_delegation *delegation;
  638. list_for_each_entry_rcu(delegation, &server->delegations, super_list)
  639. set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  640. }
  641. /**
  642. * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
  643. * @clp: nfs_client to process
  644. *
  645. */
  646. void nfs_delegation_mark_reclaim(struct nfs_client *clp)
  647. {
  648. struct nfs_server *server;
  649. rcu_read_lock();
  650. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  651. nfs_delegation_mark_reclaim_server(server);
  652. rcu_read_unlock();
  653. }
  654. /**
  655. * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
  656. * @clp: nfs_client to process
  657. *
  658. */
  659. void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
  660. {
  661. struct nfs_delegation *delegation;
  662. struct nfs_server *server;
  663. struct inode *inode;
  664. restart:
  665. rcu_read_lock();
  666. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  667. list_for_each_entry_rcu(delegation, &server->delegations,
  668. super_list) {
  669. if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
  670. &delegation->flags) == 0)
  671. continue;
  672. inode = nfs_delegation_grab_inode(delegation);
  673. if (inode == NULL)
  674. continue;
  675. delegation = nfs_detach_delegation(NFS_I(inode),
  676. delegation, server);
  677. rcu_read_unlock();
  678. if (delegation != NULL)
  679. nfs_free_delegation(delegation);
  680. iput(inode);
  681. goto restart;
  682. }
  683. }
  684. rcu_read_unlock();
  685. }
  686. /**
  687. * nfs_delegations_present - check for existence of delegations
  688. * @clp: client state handle
  689. *
  690. * Returns one if there are any nfs_delegation structures attached
  691. * to this nfs_client.
  692. */
  693. int nfs_delegations_present(struct nfs_client *clp)
  694. {
  695. struct nfs_server *server;
  696. int ret = 0;
  697. rcu_read_lock();
  698. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  699. if (!list_empty(&server->delegations)) {
  700. ret = 1;
  701. break;
  702. }
  703. rcu_read_unlock();
  704. return ret;
  705. }
  706. /**
  707. * nfs4_copy_delegation_stateid - Copy inode's state ID information
  708. * @dst: stateid data structure to fill in
  709. * @inode: inode to check
  710. * @flags: delegation type requirement
  711. *
  712. * Returns "true" and fills in "dst->data" * if inode had a delegation,
  713. * otherwise "false" is returned.
  714. */
  715. bool nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode,
  716. fmode_t flags)
  717. {
  718. struct nfs_inode *nfsi = NFS_I(inode);
  719. struct nfs_delegation *delegation;
  720. bool ret;
  721. flags &= FMODE_READ|FMODE_WRITE;
  722. rcu_read_lock();
  723. delegation = rcu_dereference(nfsi->delegation);
  724. ret = (delegation != NULL && (delegation->type & flags) == flags);
  725. if (ret) {
  726. nfs4_stateid_copy(dst, &delegation->stateid);
  727. nfs_mark_delegation_referenced(delegation);
  728. }
  729. rcu_read_unlock();
  730. return ret;
  731. }