delegation.c 40 KB

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