delegation.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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. rcu_read_unlock();
  796. return res;
  797. }
  798. }
  799. rcu_read_unlock();
  800. return ERR_PTR(-ENOENT);
  801. }
  802. static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
  803. {
  804. struct nfs_delegation *delegation;
  805. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  806. /*
  807. * If the delegation may have been admin revoked, then we
  808. * cannot reclaim it.
  809. */
  810. if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
  811. continue;
  812. set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  813. }
  814. }
  815. /**
  816. * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
  817. * @clp: nfs_client to process
  818. *
  819. */
  820. void nfs_delegation_mark_reclaim(struct nfs_client *clp)
  821. {
  822. struct nfs_server *server;
  823. rcu_read_lock();
  824. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  825. nfs_delegation_mark_reclaim_server(server);
  826. rcu_read_unlock();
  827. }
  828. /**
  829. * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
  830. * @clp: nfs_client to process
  831. *
  832. */
  833. void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
  834. {
  835. struct nfs_delegation *delegation;
  836. struct nfs_server *server;
  837. struct inode *inode;
  838. restart:
  839. rcu_read_lock();
  840. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  841. list_for_each_entry_rcu(delegation, &server->delegations,
  842. super_list) {
  843. if (test_bit(NFS_DELEGATION_RETURNING,
  844. &delegation->flags))
  845. continue;
  846. if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
  847. &delegation->flags) == 0)
  848. continue;
  849. if (!nfs_sb_active(server->super))
  850. break; /* continue in outer loop */
  851. inode = nfs_delegation_grab_inode(delegation);
  852. if (inode == NULL) {
  853. rcu_read_unlock();
  854. nfs_sb_deactive(server->super);
  855. goto restart;
  856. }
  857. delegation = nfs_start_delegation_return_locked(NFS_I(inode));
  858. rcu_read_unlock();
  859. if (delegation != NULL) {
  860. delegation = nfs_detach_delegation(NFS_I(inode),
  861. delegation, server);
  862. if (delegation != NULL)
  863. nfs_free_delegation(delegation);
  864. }
  865. iput(inode);
  866. nfs_sb_deactive(server->super);
  867. cond_resched();
  868. goto restart;
  869. }
  870. }
  871. rcu_read_unlock();
  872. }
  873. static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
  874. {
  875. return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
  876. BIT(NFS4CLNT_LEASE_EXPIRED) |
  877. BIT(NFS4CLNT_SESSION_RESET))) != 0;
  878. }
  879. static void nfs_mark_test_expired_delegation(struct nfs_server *server,
  880. struct nfs_delegation *delegation)
  881. {
  882. if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
  883. return;
  884. clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  885. set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
  886. set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
  887. }
  888. static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
  889. struct inode *inode)
  890. {
  891. struct nfs_delegation *delegation;
  892. rcu_read_lock();
  893. delegation = rcu_dereference(NFS_I(inode)->delegation);
  894. if (delegation)
  895. nfs_mark_test_expired_delegation(server, delegation);
  896. rcu_read_unlock();
  897. }
  898. static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
  899. {
  900. struct nfs_delegation *delegation;
  901. list_for_each_entry_rcu(delegation, &server->delegations, super_list)
  902. nfs_mark_test_expired_delegation(server, delegation);
  903. }
  904. /**
  905. * nfs_mark_test_expired_all_delegations - mark all delegations for testing
  906. * @clp: nfs_client to process
  907. *
  908. * Iterates through all the delegations associated with this server and
  909. * marks them as needing to be checked for validity.
  910. */
  911. void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
  912. {
  913. struct nfs_server *server;
  914. rcu_read_lock();
  915. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  916. nfs_delegation_mark_test_expired_server(server);
  917. rcu_read_unlock();
  918. }
  919. /**
  920. * nfs_reap_expired_delegations - reap expired delegations
  921. * @clp: nfs_client to process
  922. *
  923. * Iterates through all the delegations associated with this server and
  924. * checks if they have may have been revoked. This function is usually
  925. * expected to be called in cases where the server may have lost its
  926. * lease.
  927. */
  928. void nfs_reap_expired_delegations(struct nfs_client *clp)
  929. {
  930. const struct nfs4_minor_version_ops *ops = clp->cl_mvops;
  931. struct nfs_delegation *delegation;
  932. struct nfs_server *server;
  933. struct inode *inode;
  934. struct rpc_cred *cred;
  935. nfs4_stateid stateid;
  936. restart:
  937. rcu_read_lock();
  938. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  939. list_for_each_entry_rcu(delegation, &server->delegations,
  940. super_list) {
  941. if (test_bit(NFS_DELEGATION_RETURNING,
  942. &delegation->flags))
  943. continue;
  944. if (test_bit(NFS_DELEGATION_TEST_EXPIRED,
  945. &delegation->flags) == 0)
  946. continue;
  947. if (!nfs_sb_active(server->super))
  948. break; /* continue in outer loop */
  949. inode = nfs_delegation_grab_inode(delegation);
  950. if (inode == NULL) {
  951. rcu_read_unlock();
  952. nfs_sb_deactive(server->super);
  953. goto restart;
  954. }
  955. cred = get_rpccred_rcu(delegation->cred);
  956. nfs4_stateid_copy(&stateid, &delegation->stateid);
  957. clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
  958. rcu_read_unlock();
  959. if (cred != NULL &&
  960. ops->test_and_free_expired(server, &stateid, cred) < 0) {
  961. nfs_revoke_delegation(inode, &stateid);
  962. nfs_inode_find_state_and_recover(inode, &stateid);
  963. }
  964. put_rpccred(cred);
  965. if (nfs4_server_rebooted(clp)) {
  966. nfs_inode_mark_test_expired_delegation(server,inode);
  967. iput(inode);
  968. nfs_sb_deactive(server->super);
  969. return;
  970. }
  971. iput(inode);
  972. nfs_sb_deactive(server->super);
  973. cond_resched();
  974. goto restart;
  975. }
  976. }
  977. rcu_read_unlock();
  978. }
  979. void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
  980. const nfs4_stateid *stateid)
  981. {
  982. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  983. struct nfs_delegation *delegation;
  984. bool found = false;
  985. rcu_read_lock();
  986. delegation = rcu_dereference(NFS_I(inode)->delegation);
  987. if (delegation &&
  988. nfs4_stateid_match_other(&delegation->stateid, stateid)) {
  989. nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
  990. found = true;
  991. }
  992. rcu_read_unlock();
  993. if (found)
  994. nfs4_schedule_state_manager(clp);
  995. }
  996. /**
  997. * nfs_delegations_present - check for existence of delegations
  998. * @clp: client state handle
  999. *
  1000. * Returns one if there are any nfs_delegation structures attached
  1001. * to this nfs_client.
  1002. */
  1003. int nfs_delegations_present(struct nfs_client *clp)
  1004. {
  1005. struct nfs_server *server;
  1006. int ret = 0;
  1007. rcu_read_lock();
  1008. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1009. if (!list_empty(&server->delegations)) {
  1010. ret = 1;
  1011. break;
  1012. }
  1013. rcu_read_unlock();
  1014. return ret;
  1015. }
  1016. /**
  1017. * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
  1018. * @dst: stateid to refresh
  1019. * @inode: inode to check
  1020. *
  1021. * Returns "true" and updates "dst->seqid" * if inode had a delegation
  1022. * that matches our delegation stateid. Otherwise "false" is returned.
  1023. */
  1024. bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
  1025. {
  1026. struct nfs_delegation *delegation;
  1027. bool ret = false;
  1028. if (!inode)
  1029. goto out;
  1030. rcu_read_lock();
  1031. delegation = rcu_dereference(NFS_I(inode)->delegation);
  1032. if (delegation != NULL &&
  1033. nfs4_stateid_match_other(dst, &delegation->stateid)) {
  1034. dst->seqid = delegation->stateid.seqid;
  1035. return ret;
  1036. }
  1037. rcu_read_unlock();
  1038. out:
  1039. return ret;
  1040. }
  1041. /**
  1042. * nfs4_copy_delegation_stateid - Copy inode's state ID information
  1043. * @inode: inode to check
  1044. * @flags: delegation type requirement
  1045. * @dst: stateid data structure to fill in
  1046. * @cred: optional argument to retrieve credential
  1047. *
  1048. * Returns "true" and fills in "dst->data" * if inode had a delegation,
  1049. * otherwise "false" is returned.
  1050. */
  1051. bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
  1052. nfs4_stateid *dst, struct rpc_cred **cred)
  1053. {
  1054. struct nfs_inode *nfsi = NFS_I(inode);
  1055. struct nfs_delegation *delegation;
  1056. bool ret;
  1057. flags &= FMODE_READ|FMODE_WRITE;
  1058. rcu_read_lock();
  1059. delegation = rcu_dereference(nfsi->delegation);
  1060. ret = nfs4_is_valid_delegation(delegation, flags);
  1061. if (ret) {
  1062. nfs4_stateid_copy(dst, &delegation->stateid);
  1063. nfs_mark_delegation_referenced(delegation);
  1064. if (cred)
  1065. *cred = get_rpccred(delegation->cred);
  1066. }
  1067. rcu_read_unlock();
  1068. return ret;
  1069. }
  1070. /**
  1071. * nfs4_delegation_flush_on_close - Check if we must flush file on close
  1072. * @inode: inode to check
  1073. *
  1074. * This function checks the number of outstanding writes to the file
  1075. * against the delegation 'space_limit' field to see if
  1076. * the spec requires us to flush the file on close.
  1077. */
  1078. bool nfs4_delegation_flush_on_close(const struct inode *inode)
  1079. {
  1080. struct nfs_inode *nfsi = NFS_I(inode);
  1081. struct nfs_delegation *delegation;
  1082. bool ret = true;
  1083. rcu_read_lock();
  1084. delegation = rcu_dereference(nfsi->delegation);
  1085. if (delegation == NULL || !(delegation->type & FMODE_WRITE))
  1086. goto out;
  1087. if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
  1088. ret = false;
  1089. out:
  1090. rcu_read_unlock();
  1091. return ret;
  1092. }