delegation.c 32 KB

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