delegation.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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 bool
  40. nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
  41. fmode_t flags)
  42. {
  43. if (delegation != NULL && (delegation->type & flags) == flags &&
  44. !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
  45. !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
  46. return true;
  47. return false;
  48. }
  49. static int
  50. nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
  51. {
  52. struct nfs_delegation *delegation;
  53. int ret = 0;
  54. flags &= FMODE_READ|FMODE_WRITE;
  55. rcu_read_lock();
  56. delegation = rcu_dereference(NFS_I(inode)->delegation);
  57. if (nfs4_is_valid_delegation(delegation, flags)) {
  58. if (mark)
  59. nfs_mark_delegation_referenced(delegation);
  60. ret = 1;
  61. }
  62. rcu_read_unlock();
  63. return ret;
  64. }
  65. /**
  66. * nfs_have_delegation - check if inode has a delegation, mark it
  67. * NFS_DELEGATION_REFERENCED if there is one.
  68. * @inode: inode to check
  69. * @flags: delegation types to check for
  70. *
  71. * Returns one if inode has the indicated delegation, otherwise zero.
  72. */
  73. int nfs4_have_delegation(struct inode *inode, fmode_t flags)
  74. {
  75. return nfs4_do_check_delegation(inode, flags, true);
  76. }
  77. /*
  78. * nfs4_check_delegation - check if inode has a delegation, do not mark
  79. * NFS_DELEGATION_REFERENCED if it has one.
  80. */
  81. int nfs4_check_delegation(struct inode *inode, fmode_t flags)
  82. {
  83. return nfs4_do_check_delegation(inode, flags, false);
  84. }
  85. static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
  86. {
  87. struct inode *inode = state->inode;
  88. struct file_lock *fl;
  89. struct file_lock_context *flctx = inode->i_flctx;
  90. struct list_head *list;
  91. int status = 0;
  92. if (flctx == NULL)
  93. goto out;
  94. list = &flctx->flc_posix;
  95. spin_lock(&flctx->flc_lock);
  96. restart:
  97. list_for_each_entry(fl, list, fl_list) {
  98. if (nfs_file_open_context(fl->fl_file) != ctx)
  99. continue;
  100. spin_unlock(&flctx->flc_lock);
  101. status = nfs4_lock_delegation_recall(fl, state, stateid);
  102. if (status < 0)
  103. goto out;
  104. spin_lock(&flctx->flc_lock);
  105. }
  106. if (list == &flctx->flc_posix) {
  107. list = &flctx->flc_flock;
  108. goto restart;
  109. }
  110. spin_unlock(&flctx->flc_lock);
  111. out:
  112. return status;
  113. }
  114. static int nfs_delegation_claim_opens(struct inode *inode,
  115. const nfs4_stateid *stateid, fmode_t type)
  116. {
  117. struct nfs_inode *nfsi = NFS_I(inode);
  118. struct nfs_open_context *ctx;
  119. struct nfs4_state_owner *sp;
  120. struct nfs4_state *state;
  121. unsigned int seq;
  122. int err;
  123. again:
  124. spin_lock(&inode->i_lock);
  125. list_for_each_entry(ctx, &nfsi->open_files, list) {
  126. state = ctx->state;
  127. if (state == NULL)
  128. continue;
  129. if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
  130. continue;
  131. if (!nfs4_valid_open_stateid(state))
  132. continue;
  133. if (!nfs4_stateid_match(&state->stateid, stateid))
  134. continue;
  135. get_nfs_open_context(ctx);
  136. spin_unlock(&inode->i_lock);
  137. sp = state->owner;
  138. /* Block nfs4_proc_unlck */
  139. mutex_lock(&sp->so_delegreturn_mutex);
  140. seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
  141. err = nfs4_open_delegation_recall(ctx, state, stateid, type);
  142. if (!err)
  143. err = nfs_delegation_claim_locks(ctx, state, stateid);
  144. if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
  145. err = -EAGAIN;
  146. mutex_unlock(&sp->so_delegreturn_mutex);
  147. put_nfs_open_context(ctx);
  148. if (err != 0)
  149. return err;
  150. goto again;
  151. }
  152. spin_unlock(&inode->i_lock);
  153. return 0;
  154. }
  155. /**
  156. * nfs_inode_reclaim_delegation - process a delegation reclaim request
  157. * @inode: inode to process
  158. * @cred: credential to use for request
  159. * @res: new delegation state from server
  160. *
  161. */
  162. void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred,
  163. struct nfs_openres *res)
  164. {
  165. struct nfs_delegation *delegation;
  166. struct rpc_cred *oldcred = NULL;
  167. rcu_read_lock();
  168. delegation = rcu_dereference(NFS_I(inode)->delegation);
  169. if (delegation != NULL) {
  170. spin_lock(&delegation->lock);
  171. if (delegation->inode != NULL) {
  172. nfs4_stateid_copy(&delegation->stateid, &res->delegation);
  173. delegation->type = res->delegation_type;
  174. delegation->pagemod_limit = res->pagemod_limit;
  175. oldcred = delegation->cred;
  176. delegation->cred = get_rpccred(cred);
  177. clear_bit(NFS_DELEGATION_NEED_RECLAIM,
  178. &delegation->flags);
  179. spin_unlock(&delegation->lock);
  180. rcu_read_unlock();
  181. put_rpccred(oldcred);
  182. trace_nfs4_reclaim_delegation(inode, res->delegation_type);
  183. return;
  184. }
  185. /* We appear to have raced with a delegation return. */
  186. spin_unlock(&delegation->lock);
  187. }
  188. rcu_read_unlock();
  189. nfs_inode_set_delegation(inode, cred, res);
  190. }
  191. static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
  192. {
  193. int res = 0;
  194. if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
  195. res = nfs4_proc_delegreturn(inode,
  196. delegation->cred,
  197. &delegation->stateid,
  198. issync);
  199. nfs_free_delegation(delegation);
  200. return res;
  201. }
  202. static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
  203. {
  204. struct inode *inode = NULL;
  205. spin_lock(&delegation->lock);
  206. if (delegation->inode != NULL)
  207. inode = igrab(delegation->inode);
  208. spin_unlock(&delegation->lock);
  209. return inode;
  210. }
  211. static struct nfs_delegation *
  212. nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
  213. {
  214. struct nfs_delegation *ret = NULL;
  215. struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
  216. if (delegation == NULL)
  217. goto out;
  218. spin_lock(&delegation->lock);
  219. if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
  220. ret = delegation;
  221. spin_unlock(&delegation->lock);
  222. out:
  223. return ret;
  224. }
  225. static struct nfs_delegation *
  226. nfs_start_delegation_return(struct nfs_inode *nfsi)
  227. {
  228. struct nfs_delegation *delegation;
  229. rcu_read_lock();
  230. delegation = nfs_start_delegation_return_locked(nfsi);
  231. rcu_read_unlock();
  232. return delegation;
  233. }
  234. static void
  235. nfs_abort_delegation_return(struct nfs_delegation *delegation,
  236. struct nfs_client *clp)
  237. {
  238. spin_lock(&delegation->lock);
  239. clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
  240. set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
  241. spin_unlock(&delegation->lock);
  242. set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
  243. }
  244. static struct nfs_delegation *
  245. nfs_detach_delegation_locked(struct nfs_inode *nfsi,
  246. struct nfs_delegation *delegation,
  247. struct nfs_client *clp)
  248. {
  249. struct nfs_delegation *deleg_cur =
  250. rcu_dereference_protected(nfsi->delegation,
  251. lockdep_is_held(&clp->cl_lock));
  252. if (deleg_cur == NULL || delegation != deleg_cur)
  253. return NULL;
  254. spin_lock(&delegation->lock);
  255. set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
  256. list_del_rcu(&delegation->super_list);
  257. delegation->inode = NULL;
  258. rcu_assign_pointer(nfsi->delegation, NULL);
  259. spin_unlock(&delegation->lock);
  260. return delegation;
  261. }
  262. static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
  263. struct nfs_delegation *delegation,
  264. struct nfs_server *server)
  265. {
  266. struct nfs_client *clp = server->nfs_client;
  267. spin_lock(&clp->cl_lock);
  268. delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
  269. spin_unlock(&clp->cl_lock);
  270. return delegation;
  271. }
  272. static struct nfs_delegation *
  273. nfs_inode_detach_delegation(struct inode *inode)
  274. {
  275. struct nfs_inode *nfsi = NFS_I(inode);
  276. struct nfs_server *server = NFS_SERVER(inode);
  277. struct nfs_delegation *delegation;
  278. delegation = nfs_start_delegation_return(nfsi);
  279. if (delegation == NULL)
  280. return NULL;
  281. return nfs_detach_delegation(nfsi, delegation, server);
  282. }
  283. static void
  284. nfs_update_inplace_delegation(struct nfs_delegation *delegation,
  285. const struct nfs_delegation *update)
  286. {
  287. if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
  288. delegation->stateid.seqid = update->stateid.seqid;
  289. smp_wmb();
  290. delegation->type = update->type;
  291. }
  292. }
  293. /**
  294. * nfs_inode_set_delegation - set up a delegation on an inode
  295. * @inode: inode to which delegation applies
  296. * @cred: cred to use for subsequent delegation processing
  297. * @res: new delegation state from server
  298. *
  299. * Returns zero on success, or a negative errno value.
  300. */
  301. int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
  302. {
  303. struct nfs_server *server = NFS_SERVER(inode);
  304. struct nfs_client *clp = server->nfs_client;
  305. struct nfs_inode *nfsi = NFS_I(inode);
  306. struct nfs_delegation *delegation, *old_delegation;
  307. struct nfs_delegation *freeme = NULL;
  308. int status = 0;
  309. delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
  310. if (delegation == NULL)
  311. return -ENOMEM;
  312. nfs4_stateid_copy(&delegation->stateid, &res->delegation);
  313. delegation->type = res->delegation_type;
  314. delegation->pagemod_limit = res->pagemod_limit;
  315. delegation->change_attr = inode->i_version;
  316. delegation->cred = get_rpccred(cred);
  317. delegation->inode = inode;
  318. delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
  319. spin_lock_init(&delegation->lock);
  320. spin_lock(&clp->cl_lock);
  321. old_delegation = rcu_dereference_protected(nfsi->delegation,
  322. lockdep_is_held(&clp->cl_lock));
  323. if (old_delegation != NULL) {
  324. /* Is this an update of the existing delegation? */
  325. if (nfs4_stateid_match_other(&old_delegation->stateid,
  326. &delegation->stateid)) {
  327. nfs_update_inplace_delegation(old_delegation,
  328. delegation);
  329. goto out;
  330. }
  331. /*
  332. * Deal with broken servers that hand out two
  333. * delegations for the same file.
  334. * Allow for upgrades to a WRITE delegation, but
  335. * nothing else.
  336. */
  337. dfprintk(FILE, "%s: server %s handed out "
  338. "a duplicate delegation!\n",
  339. __func__, clp->cl_hostname);
  340. if (delegation->type == old_delegation->type ||
  341. !(delegation->type & FMODE_WRITE)) {
  342. freeme = delegation;
  343. delegation = NULL;
  344. goto out;
  345. }
  346. if (test_and_set_bit(NFS_DELEGATION_RETURNING,
  347. &old_delegation->flags))
  348. goto out;
  349. freeme = nfs_detach_delegation_locked(nfsi,
  350. old_delegation, clp);
  351. if (freeme == NULL)
  352. goto out;
  353. }
  354. list_add_tail_rcu(&delegation->super_list, &server->delegations);
  355. rcu_assign_pointer(nfsi->delegation, delegation);
  356. delegation = NULL;
  357. /* Ensure we revalidate the attributes and page cache! */
  358. spin_lock(&inode->i_lock);
  359. nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
  360. spin_unlock(&inode->i_lock);
  361. trace_nfs4_set_delegation(inode, res->delegation_type);
  362. out:
  363. spin_unlock(&clp->cl_lock);
  364. if (delegation != NULL)
  365. nfs_free_delegation(delegation);
  366. if (freeme != NULL)
  367. nfs_do_return_delegation(inode, freeme, 0);
  368. return status;
  369. }
  370. /*
  371. * Basic procedure for returning a delegation to the server
  372. */
  373. static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
  374. {
  375. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  376. struct nfs_inode *nfsi = NFS_I(inode);
  377. int err = 0;
  378. if (delegation == NULL)
  379. return 0;
  380. do {
  381. if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
  382. break;
  383. err = nfs_delegation_claim_opens(inode, &delegation->stateid,
  384. delegation->type);
  385. if (!issync || err != -EAGAIN)
  386. break;
  387. /*
  388. * Guard against state recovery
  389. */
  390. err = nfs4_wait_clnt_recover(clp);
  391. } while (err == 0);
  392. if (err) {
  393. nfs_abort_delegation_return(delegation, clp);
  394. goto out;
  395. }
  396. if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
  397. goto out;
  398. err = nfs_do_return_delegation(inode, delegation, issync);
  399. out:
  400. return err;
  401. }
  402. static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
  403. {
  404. bool ret = false;
  405. if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
  406. goto out;
  407. if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
  408. ret = true;
  409. if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
  410. struct inode *inode;
  411. spin_lock(&delegation->lock);
  412. inode = delegation->inode;
  413. if (inode && list_empty(&NFS_I(inode)->open_files))
  414. ret = true;
  415. spin_unlock(&delegation->lock);
  416. }
  417. out:
  418. return ret;
  419. }
  420. /**
  421. * nfs_client_return_marked_delegations - return previously marked delegations
  422. * @clp: nfs_client to process
  423. *
  424. * Note that this function is designed to be called by the state
  425. * manager thread. For this reason, it cannot flush the dirty data,
  426. * since that could deadlock in case of a state recovery error.
  427. *
  428. * Returns zero on success, or a negative errno value.
  429. */
  430. int nfs_client_return_marked_delegations(struct nfs_client *clp)
  431. {
  432. struct nfs_delegation *delegation;
  433. struct nfs_server *server;
  434. struct inode *inode;
  435. int err = 0;
  436. restart:
  437. rcu_read_lock();
  438. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  439. list_for_each_entry_rcu(delegation, &server->delegations,
  440. super_list) {
  441. if (!nfs_delegation_need_return(delegation))
  442. continue;
  443. if (!nfs_sb_active(server->super))
  444. continue;
  445. inode = nfs_delegation_grab_inode(delegation);
  446. if (inode == NULL) {
  447. rcu_read_unlock();
  448. nfs_sb_deactive(server->super);
  449. goto restart;
  450. }
  451. delegation = nfs_start_delegation_return_locked(NFS_I(inode));
  452. rcu_read_unlock();
  453. err = nfs_end_delegation_return(inode, delegation, 0);
  454. iput(inode);
  455. nfs_sb_deactive(server->super);
  456. if (!err)
  457. goto restart;
  458. set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
  459. return err;
  460. }
  461. }
  462. rcu_read_unlock();
  463. return 0;
  464. }
  465. /**
  466. * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
  467. * @inode: inode to process
  468. *
  469. * Does not protect against delegation reclaims, therefore really only safe
  470. * to be called from nfs4_clear_inode().
  471. */
  472. void nfs_inode_return_delegation_noreclaim(struct inode *inode)
  473. {
  474. struct nfs_delegation *delegation;
  475. delegation = nfs_inode_detach_delegation(inode);
  476. if (delegation != NULL)
  477. nfs_do_return_delegation(inode, delegation, 1);
  478. }
  479. /**
  480. * nfs_inode_return_delegation - synchronously return a delegation
  481. * @inode: inode to process
  482. *
  483. * This routine will always flush any dirty data to disk on the
  484. * assumption that if we need to return the delegation, then
  485. * we should stop caching.
  486. *
  487. * Returns zero on success, or a negative errno value.
  488. */
  489. int nfs4_inode_return_delegation(struct inode *inode)
  490. {
  491. struct nfs_inode *nfsi = NFS_I(inode);
  492. struct nfs_delegation *delegation;
  493. int err = 0;
  494. nfs_wb_all(inode);
  495. delegation = nfs_start_delegation_return(nfsi);
  496. if (delegation != NULL)
  497. err = nfs_end_delegation_return(inode, delegation, 1);
  498. return err;
  499. }
  500. static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
  501. struct nfs_delegation *delegation)
  502. {
  503. set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
  504. set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
  505. }
  506. static void nfs_mark_return_delegation(struct nfs_server *server,
  507. struct nfs_delegation *delegation)
  508. {
  509. set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
  510. set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
  511. }
  512. static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
  513. {
  514. struct nfs_delegation *delegation;
  515. bool ret = false;
  516. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  517. nfs_mark_return_delegation(server, delegation);
  518. ret = true;
  519. }
  520. return ret;
  521. }
  522. static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
  523. {
  524. struct nfs_server *server;
  525. rcu_read_lock();
  526. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  527. nfs_server_mark_return_all_delegations(server);
  528. rcu_read_unlock();
  529. }
  530. static void nfs_delegation_run_state_manager(struct nfs_client *clp)
  531. {
  532. if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
  533. nfs4_schedule_state_manager(clp);
  534. }
  535. /**
  536. * nfs_expire_all_delegations
  537. * @clp: client to process
  538. *
  539. */
  540. void nfs_expire_all_delegations(struct nfs_client *clp)
  541. {
  542. nfs_client_mark_return_all_delegations(clp);
  543. nfs_delegation_run_state_manager(clp);
  544. }
  545. /**
  546. * nfs_super_return_all_delegations - return delegations for one superblock
  547. * @sb: sb to process
  548. *
  549. */
  550. void nfs_server_return_all_delegations(struct nfs_server *server)
  551. {
  552. struct nfs_client *clp = server->nfs_client;
  553. bool need_wait;
  554. if (clp == NULL)
  555. return;
  556. rcu_read_lock();
  557. need_wait = nfs_server_mark_return_all_delegations(server);
  558. rcu_read_unlock();
  559. if (need_wait) {
  560. nfs4_schedule_state_manager(clp);
  561. nfs4_wait_clnt_recover(clp);
  562. }
  563. }
  564. static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
  565. fmode_t flags)
  566. {
  567. struct nfs_delegation *delegation;
  568. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  569. if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
  570. continue;
  571. if (delegation->type & flags)
  572. nfs_mark_return_if_closed_delegation(server, delegation);
  573. }
  574. }
  575. static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
  576. fmode_t flags)
  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_unused_delegation_types(server, flags);
  582. rcu_read_unlock();
  583. }
  584. static void nfs_mark_delegation_revoked(struct nfs_server *server,
  585. struct nfs_delegation *delegation)
  586. {
  587. set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
  588. delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
  589. nfs_mark_return_delegation(server, delegation);
  590. }
  591. static bool nfs_revoke_delegation(struct inode *inode,
  592. const nfs4_stateid *stateid)
  593. {
  594. struct nfs_delegation *delegation;
  595. nfs4_stateid tmp;
  596. bool ret = false;
  597. rcu_read_lock();
  598. delegation = rcu_dereference(NFS_I(inode)->delegation);
  599. if (delegation == NULL)
  600. goto out;
  601. if (stateid == NULL) {
  602. nfs4_stateid_copy(&tmp, &delegation->stateid);
  603. stateid = &tmp;
  604. } else if (!nfs4_stateid_match(stateid, &delegation->stateid))
  605. goto out;
  606. nfs_mark_delegation_revoked(NFS_SERVER(inode), delegation);
  607. ret = true;
  608. out:
  609. rcu_read_unlock();
  610. if (ret)
  611. nfs_inode_find_state_and_recover(inode, stateid);
  612. return ret;
  613. }
  614. void nfs_remove_bad_delegation(struct inode *inode,
  615. const nfs4_stateid *stateid)
  616. {
  617. struct nfs_delegation *delegation;
  618. if (!nfs_revoke_delegation(inode, stateid))
  619. return;
  620. delegation = nfs_inode_detach_delegation(inode);
  621. if (delegation)
  622. nfs_free_delegation(delegation);
  623. }
  624. EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
  625. /**
  626. * nfs_expire_unused_delegation_types
  627. * @clp: client to process
  628. * @flags: delegation types to expire
  629. *
  630. */
  631. void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
  632. {
  633. nfs_client_mark_return_unused_delegation_types(clp, flags);
  634. nfs_delegation_run_state_manager(clp);
  635. }
  636. static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
  637. {
  638. struct nfs_delegation *delegation;
  639. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  640. if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
  641. continue;
  642. nfs_mark_return_if_closed_delegation(server, delegation);
  643. }
  644. }
  645. /**
  646. * nfs_expire_unreferenced_delegations - Eliminate unused delegations
  647. * @clp: nfs_client to process
  648. *
  649. */
  650. void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
  651. {
  652. struct nfs_server *server;
  653. rcu_read_lock();
  654. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  655. nfs_mark_return_unreferenced_delegations(server);
  656. rcu_read_unlock();
  657. nfs_delegation_run_state_manager(clp);
  658. }
  659. /**
  660. * nfs_async_inode_return_delegation - asynchronously return a delegation
  661. * @inode: inode to process
  662. * @stateid: state ID information
  663. *
  664. * Returns zero on success, or a negative errno value.
  665. */
  666. int nfs_async_inode_return_delegation(struct inode *inode,
  667. const nfs4_stateid *stateid)
  668. {
  669. struct nfs_server *server = NFS_SERVER(inode);
  670. struct nfs_client *clp = server->nfs_client;
  671. struct nfs_delegation *delegation;
  672. rcu_read_lock();
  673. delegation = rcu_dereference(NFS_I(inode)->delegation);
  674. if (delegation == NULL)
  675. goto out_enoent;
  676. if (stateid != NULL &&
  677. !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
  678. goto out_enoent;
  679. nfs_mark_return_delegation(server, delegation);
  680. rcu_read_unlock();
  681. nfs_delegation_run_state_manager(clp);
  682. return 0;
  683. out_enoent:
  684. rcu_read_unlock();
  685. return -ENOENT;
  686. }
  687. static struct inode *
  688. nfs_delegation_find_inode_server(struct nfs_server *server,
  689. const struct nfs_fh *fhandle)
  690. {
  691. struct nfs_delegation *delegation;
  692. struct inode *res = NULL;
  693. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  694. spin_lock(&delegation->lock);
  695. if (delegation->inode != NULL &&
  696. nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
  697. res = igrab(delegation->inode);
  698. }
  699. spin_unlock(&delegation->lock);
  700. if (res != NULL)
  701. break;
  702. }
  703. return res;
  704. }
  705. /**
  706. * nfs_delegation_find_inode - retrieve the inode associated with a delegation
  707. * @clp: client state handle
  708. * @fhandle: filehandle from a delegation recall
  709. *
  710. * Returns pointer to inode matching "fhandle," or NULL if a matching inode
  711. * cannot be found.
  712. */
  713. struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
  714. const struct nfs_fh *fhandle)
  715. {
  716. struct nfs_server *server;
  717. struct inode *res = NULL;
  718. rcu_read_lock();
  719. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  720. res = nfs_delegation_find_inode_server(server, fhandle);
  721. if (res != NULL)
  722. break;
  723. }
  724. rcu_read_unlock();
  725. return res;
  726. }
  727. static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
  728. {
  729. struct nfs_delegation *delegation;
  730. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  731. /*
  732. * If the delegation may have been admin revoked, then we
  733. * cannot reclaim it.
  734. */
  735. if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
  736. continue;
  737. set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  738. }
  739. }
  740. /**
  741. * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
  742. * @clp: nfs_client to process
  743. *
  744. */
  745. void nfs_delegation_mark_reclaim(struct nfs_client *clp)
  746. {
  747. struct nfs_server *server;
  748. rcu_read_lock();
  749. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  750. nfs_delegation_mark_reclaim_server(server);
  751. rcu_read_unlock();
  752. }
  753. /**
  754. * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
  755. * @clp: nfs_client to process
  756. *
  757. */
  758. void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
  759. {
  760. struct nfs_delegation *delegation;
  761. struct nfs_server *server;
  762. struct inode *inode;
  763. restart:
  764. rcu_read_lock();
  765. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  766. list_for_each_entry_rcu(delegation, &server->delegations,
  767. super_list) {
  768. if (test_bit(NFS_DELEGATION_RETURNING,
  769. &delegation->flags))
  770. continue;
  771. if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
  772. &delegation->flags) == 0)
  773. continue;
  774. if (!nfs_sb_active(server->super))
  775. continue;
  776. inode = nfs_delegation_grab_inode(delegation);
  777. if (inode == NULL) {
  778. rcu_read_unlock();
  779. nfs_sb_deactive(server->super);
  780. goto restart;
  781. }
  782. delegation = nfs_start_delegation_return_locked(NFS_I(inode));
  783. rcu_read_unlock();
  784. if (delegation != NULL) {
  785. delegation = nfs_detach_delegation(NFS_I(inode),
  786. delegation, server);
  787. if (delegation != NULL)
  788. nfs_free_delegation(delegation);
  789. }
  790. iput(inode);
  791. nfs_sb_deactive(server->super);
  792. goto restart;
  793. }
  794. }
  795. rcu_read_unlock();
  796. }
  797. static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
  798. {
  799. return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
  800. BIT(NFS4CLNT_LEASE_EXPIRED) |
  801. BIT(NFS4CLNT_SESSION_RESET))) != 0;
  802. }
  803. static void nfs_mark_test_expired_delegation(struct nfs_server *server,
  804. struct nfs_delegation *delegation)
  805. {
  806. if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
  807. return;
  808. clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  809. set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
  810. set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
  811. }
  812. static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
  813. struct inode *inode)
  814. {
  815. struct nfs_delegation *delegation;
  816. rcu_read_lock();
  817. delegation = rcu_dereference(NFS_I(inode)->delegation);
  818. if (delegation)
  819. nfs_mark_test_expired_delegation(server, delegation);
  820. rcu_read_unlock();
  821. }
  822. static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
  823. {
  824. struct nfs_delegation *delegation;
  825. list_for_each_entry_rcu(delegation, &server->delegations, super_list)
  826. nfs_mark_test_expired_delegation(server, delegation);
  827. }
  828. /**
  829. * nfs_mark_test_expired_all_delegations - mark all delegations for testing
  830. * @clp: nfs_client to process
  831. *
  832. * Iterates through all the delegations associated with this server and
  833. * marks them as needing to be checked for validity.
  834. */
  835. void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
  836. {
  837. struct nfs_server *server;
  838. rcu_read_lock();
  839. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  840. nfs_delegation_mark_test_expired_server(server);
  841. rcu_read_unlock();
  842. }
  843. /**
  844. * nfs_reap_expired_delegations - reap expired delegations
  845. * @clp: nfs_client to process
  846. *
  847. * Iterates through all the delegations associated with this server and
  848. * checks if they have may have been revoked. This function is usually
  849. * expected to be called in cases where the server may have lost its
  850. * lease.
  851. */
  852. void nfs_reap_expired_delegations(struct nfs_client *clp)
  853. {
  854. const struct nfs4_minor_version_ops *ops = clp->cl_mvops;
  855. struct nfs_delegation *delegation;
  856. struct nfs_server *server;
  857. struct inode *inode;
  858. struct rpc_cred *cred;
  859. nfs4_stateid stateid;
  860. restart:
  861. rcu_read_lock();
  862. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  863. list_for_each_entry_rcu(delegation, &server->delegations,
  864. super_list) {
  865. if (test_bit(NFS_DELEGATION_RETURNING,
  866. &delegation->flags))
  867. continue;
  868. if (test_bit(NFS_DELEGATION_TEST_EXPIRED,
  869. &delegation->flags) == 0)
  870. continue;
  871. if (!nfs_sb_active(server->super))
  872. continue;
  873. inode = nfs_delegation_grab_inode(delegation);
  874. if (inode == NULL) {
  875. rcu_read_unlock();
  876. nfs_sb_deactive(server->super);
  877. goto restart;
  878. }
  879. cred = get_rpccred_rcu(delegation->cred);
  880. nfs4_stateid_copy(&stateid, &delegation->stateid);
  881. clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
  882. rcu_read_unlock();
  883. if (cred != NULL &&
  884. ops->test_and_free_expired(server, &stateid, cred) < 0) {
  885. nfs_revoke_delegation(inode, &stateid);
  886. nfs_inode_find_state_and_recover(inode, &stateid);
  887. }
  888. put_rpccred(cred);
  889. if (nfs4_server_rebooted(clp)) {
  890. nfs_inode_mark_test_expired_delegation(server,inode);
  891. iput(inode);
  892. nfs_sb_deactive(server->super);
  893. return;
  894. }
  895. iput(inode);
  896. nfs_sb_deactive(server->super);
  897. goto restart;
  898. }
  899. }
  900. rcu_read_unlock();
  901. }
  902. void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
  903. const nfs4_stateid *stateid)
  904. {
  905. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  906. struct nfs_delegation *delegation;
  907. bool found = false;
  908. rcu_read_lock();
  909. delegation = rcu_dereference(NFS_I(inode)->delegation);
  910. if (delegation &&
  911. nfs4_stateid_match_other(&delegation->stateid, stateid)) {
  912. nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
  913. found = true;
  914. }
  915. rcu_read_unlock();
  916. if (found)
  917. nfs4_schedule_state_manager(clp);
  918. }
  919. /**
  920. * nfs_delegations_present - check for existence of delegations
  921. * @clp: client state handle
  922. *
  923. * Returns one if there are any nfs_delegation structures attached
  924. * to this nfs_client.
  925. */
  926. int nfs_delegations_present(struct nfs_client *clp)
  927. {
  928. struct nfs_server *server;
  929. int ret = 0;
  930. rcu_read_lock();
  931. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  932. if (!list_empty(&server->delegations)) {
  933. ret = 1;
  934. break;
  935. }
  936. rcu_read_unlock();
  937. return ret;
  938. }
  939. /**
  940. * nfs4_copy_delegation_stateid - Copy inode's state ID information
  941. * @inode: inode to check
  942. * @flags: delegation type requirement
  943. * @dst: stateid data structure to fill in
  944. * @cred: optional argument to retrieve credential
  945. *
  946. * Returns "true" and fills in "dst->data" * if inode had a delegation,
  947. * otherwise "false" is returned.
  948. */
  949. bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
  950. nfs4_stateid *dst, struct rpc_cred **cred)
  951. {
  952. struct nfs_inode *nfsi = NFS_I(inode);
  953. struct nfs_delegation *delegation;
  954. bool ret;
  955. flags &= FMODE_READ|FMODE_WRITE;
  956. rcu_read_lock();
  957. delegation = rcu_dereference(nfsi->delegation);
  958. ret = nfs4_is_valid_delegation(delegation, flags);
  959. if (ret) {
  960. nfs4_stateid_copy(dst, &delegation->stateid);
  961. nfs_mark_delegation_referenced(delegation);
  962. if (cred)
  963. *cred = get_rpccred(delegation->cred);
  964. }
  965. rcu_read_unlock();
  966. return ret;
  967. }
  968. /**
  969. * nfs4_delegation_flush_on_close - Check if we must flush file on close
  970. * @inode: inode to check
  971. *
  972. * This function checks the number of outstanding writes to the file
  973. * against the delegation 'space_limit' field to see if
  974. * the spec requires us to flush the file on close.
  975. */
  976. bool nfs4_delegation_flush_on_close(const struct inode *inode)
  977. {
  978. struct nfs_inode *nfsi = NFS_I(inode);
  979. struct nfs_delegation *delegation;
  980. bool ret = true;
  981. rcu_read_lock();
  982. delegation = rcu_dereference(nfsi->delegation);
  983. if (delegation == NULL || !(delegation->type & FMODE_WRITE))
  984. goto out;
  985. if (nfsi->nrequests < delegation->pagemod_limit)
  986. ret = false;
  987. out:
  988. rcu_read_unlock();
  989. return ret;
  990. }