delegation.c 30 KB

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