delegation.c 39 KB

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