delegation.c 32 KB

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