delegation.c 33 KB

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