nfs4state.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  1. /*
  2. * fs/nfs/nfs4state.c
  3. *
  4. * Client-side XDR for NFSv4.
  5. *
  6. * Copyright (c) 2002 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Kendrick Smith <kmsmith@umich.edu>
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  25. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  26. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  31. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. * Implementation of the NFSv4 state model. For the time being,
  37. * this is minimal, but will be made much more complex in a
  38. * subsequent patch.
  39. */
  40. #include <linux/kernel.h>
  41. #include <linux/slab.h>
  42. #include <linux/fs.h>
  43. #include <linux/nfs_fs.h>
  44. #include <linux/nfs_idmap.h>
  45. #include <linux/kthread.h>
  46. #include <linux/module.h>
  47. #include <linux/random.h>
  48. #include <linux/ratelimit.h>
  49. #include <linux/workqueue.h>
  50. #include <linux/bitops.h>
  51. #include <linux/jiffies.h>
  52. #include "nfs4_fs.h"
  53. #include "callback.h"
  54. #include "delegation.h"
  55. #include "internal.h"
  56. #include "pnfs.h"
  57. #define OPENOWNER_POOL_SIZE 8
  58. const nfs4_stateid zero_stateid;
  59. static LIST_HEAD(nfs4_clientid_list);
  60. int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
  61. {
  62. struct nfs4_setclientid_res clid = {
  63. .clientid = clp->cl_clientid,
  64. .confirm = clp->cl_confirm,
  65. };
  66. unsigned short port;
  67. int status;
  68. if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
  69. goto do_confirm;
  70. port = nfs_callback_tcpport;
  71. if (clp->cl_addr.ss_family == AF_INET6)
  72. port = nfs_callback_tcpport6;
  73. status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
  74. if (status != 0)
  75. goto out;
  76. clp->cl_clientid = clid.clientid;
  77. clp->cl_confirm = clid.confirm;
  78. set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  79. do_confirm:
  80. status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
  81. if (status != 0)
  82. goto out;
  83. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  84. nfs4_schedule_state_renewal(clp);
  85. out:
  86. return status;
  87. }
  88. struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
  89. {
  90. struct rpc_cred *cred = NULL;
  91. if (clp->cl_machine_cred != NULL)
  92. cred = get_rpccred(clp->cl_machine_cred);
  93. return cred;
  94. }
  95. static void nfs4_clear_machine_cred(struct nfs_client *clp)
  96. {
  97. struct rpc_cred *cred;
  98. spin_lock(&clp->cl_lock);
  99. cred = clp->cl_machine_cred;
  100. clp->cl_machine_cred = NULL;
  101. spin_unlock(&clp->cl_lock);
  102. if (cred != NULL)
  103. put_rpccred(cred);
  104. }
  105. static struct rpc_cred *
  106. nfs4_get_renew_cred_server_locked(struct nfs_server *server)
  107. {
  108. struct rpc_cred *cred = NULL;
  109. struct nfs4_state_owner *sp;
  110. struct rb_node *pos;
  111. for (pos = rb_first(&server->state_owners);
  112. pos != NULL;
  113. pos = rb_next(pos)) {
  114. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  115. if (list_empty(&sp->so_states))
  116. continue;
  117. cred = get_rpccred(sp->so_cred);
  118. break;
  119. }
  120. return cred;
  121. }
  122. /**
  123. * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
  124. * @clp: client state handle
  125. *
  126. * Returns an rpc_cred with reference count bumped, or NULL.
  127. * Caller must hold clp->cl_lock.
  128. */
  129. struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
  130. {
  131. struct rpc_cred *cred = NULL;
  132. struct nfs_server *server;
  133. rcu_read_lock();
  134. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  135. cred = nfs4_get_renew_cred_server_locked(server);
  136. if (cred != NULL)
  137. break;
  138. }
  139. rcu_read_unlock();
  140. return cred;
  141. }
  142. #if defined(CONFIG_NFS_V4_1)
  143. static int nfs41_setup_state_renewal(struct nfs_client *clp)
  144. {
  145. int status;
  146. struct nfs_fsinfo fsinfo;
  147. if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
  148. nfs4_schedule_state_renewal(clp);
  149. return 0;
  150. }
  151. status = nfs4_proc_get_lease_time(clp, &fsinfo);
  152. if (status == 0) {
  153. /* Update lease time and schedule renewal */
  154. spin_lock(&clp->cl_lock);
  155. clp->cl_lease_time = fsinfo.lease_time * HZ;
  156. clp->cl_last_renewal = jiffies;
  157. spin_unlock(&clp->cl_lock);
  158. nfs4_schedule_state_renewal(clp);
  159. }
  160. return status;
  161. }
  162. /*
  163. * Back channel returns NFS4ERR_DELAY for new requests when
  164. * NFS4_SESSION_DRAINING is set so there is no work to be done when draining
  165. * is ended.
  166. */
  167. static void nfs4_end_drain_session(struct nfs_client *clp)
  168. {
  169. struct nfs4_session *ses = clp->cl_session;
  170. struct nfs4_slot_table *tbl;
  171. int max_slots;
  172. if (ses == NULL)
  173. return;
  174. tbl = &ses->fc_slot_table;
  175. if (test_and_clear_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
  176. spin_lock(&tbl->slot_tbl_lock);
  177. max_slots = tbl->max_slots;
  178. while (max_slots--) {
  179. if (rpc_wake_up_first(&tbl->slot_tbl_waitq,
  180. nfs4_set_task_privileged,
  181. NULL) == NULL)
  182. break;
  183. }
  184. spin_unlock(&tbl->slot_tbl_lock);
  185. }
  186. }
  187. static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table *tbl)
  188. {
  189. spin_lock(&tbl->slot_tbl_lock);
  190. if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
  191. INIT_COMPLETION(tbl->complete);
  192. spin_unlock(&tbl->slot_tbl_lock);
  193. return wait_for_completion_interruptible(&tbl->complete);
  194. }
  195. spin_unlock(&tbl->slot_tbl_lock);
  196. return 0;
  197. }
  198. static int nfs4_begin_drain_session(struct nfs_client *clp)
  199. {
  200. struct nfs4_session *ses = clp->cl_session;
  201. int ret = 0;
  202. set_bit(NFS4_SESSION_DRAINING, &ses->session_state);
  203. /* back channel */
  204. ret = nfs4_wait_on_slot_tbl(&ses->bc_slot_table);
  205. if (ret)
  206. return ret;
  207. /* fore channel */
  208. return nfs4_wait_on_slot_tbl(&ses->fc_slot_table);
  209. }
  210. int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
  211. {
  212. int status;
  213. if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
  214. goto do_confirm;
  215. nfs4_begin_drain_session(clp);
  216. status = nfs4_proc_exchange_id(clp, cred);
  217. if (status != 0)
  218. goto out;
  219. set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  220. do_confirm:
  221. status = nfs4_proc_create_session(clp);
  222. if (status != 0)
  223. goto out;
  224. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  225. nfs41_setup_state_renewal(clp);
  226. nfs_mark_client_ready(clp, NFS_CS_READY);
  227. out:
  228. return status;
  229. }
  230. struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp)
  231. {
  232. struct rpc_cred *cred;
  233. spin_lock(&clp->cl_lock);
  234. cred = nfs4_get_machine_cred_locked(clp);
  235. spin_unlock(&clp->cl_lock);
  236. return cred;
  237. }
  238. #endif /* CONFIG_NFS_V4_1 */
  239. static struct rpc_cred *
  240. nfs4_get_setclientid_cred_server(struct nfs_server *server)
  241. {
  242. struct nfs_client *clp = server->nfs_client;
  243. struct rpc_cred *cred = NULL;
  244. struct nfs4_state_owner *sp;
  245. struct rb_node *pos;
  246. spin_lock(&clp->cl_lock);
  247. pos = rb_first(&server->state_owners);
  248. if (pos != NULL) {
  249. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  250. cred = get_rpccred(sp->so_cred);
  251. }
  252. spin_unlock(&clp->cl_lock);
  253. return cred;
  254. }
  255. /**
  256. * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
  257. * @clp: client state handle
  258. *
  259. * Returns an rpc_cred with reference count bumped, or NULL.
  260. */
  261. struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
  262. {
  263. struct nfs_server *server;
  264. struct rpc_cred *cred;
  265. spin_lock(&clp->cl_lock);
  266. cred = nfs4_get_machine_cred_locked(clp);
  267. spin_unlock(&clp->cl_lock);
  268. if (cred != NULL)
  269. goto out;
  270. rcu_read_lock();
  271. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  272. cred = nfs4_get_setclientid_cred_server(server);
  273. if (cred != NULL)
  274. break;
  275. }
  276. rcu_read_unlock();
  277. out:
  278. return cred;
  279. }
  280. static struct nfs4_state_owner *
  281. nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
  282. {
  283. struct rb_node **p = &server->state_owners.rb_node,
  284. *parent = NULL;
  285. struct nfs4_state_owner *sp;
  286. while (*p != NULL) {
  287. parent = *p;
  288. sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
  289. if (cred < sp->so_cred)
  290. p = &parent->rb_left;
  291. else if (cred > sp->so_cred)
  292. p = &parent->rb_right;
  293. else {
  294. if (!list_empty(&sp->so_lru))
  295. list_del_init(&sp->so_lru);
  296. atomic_inc(&sp->so_count);
  297. return sp;
  298. }
  299. }
  300. return NULL;
  301. }
  302. static struct nfs4_state_owner *
  303. nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
  304. {
  305. struct nfs_server *server = new->so_server;
  306. struct rb_node **p = &server->state_owners.rb_node,
  307. *parent = NULL;
  308. struct nfs4_state_owner *sp;
  309. int err;
  310. while (*p != NULL) {
  311. parent = *p;
  312. sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
  313. if (new->so_cred < sp->so_cred)
  314. p = &parent->rb_left;
  315. else if (new->so_cred > sp->so_cred)
  316. p = &parent->rb_right;
  317. else {
  318. if (!list_empty(&sp->so_lru))
  319. list_del_init(&sp->so_lru);
  320. atomic_inc(&sp->so_count);
  321. return sp;
  322. }
  323. }
  324. err = ida_get_new(&server->openowner_id, &new->so_seqid.owner_id);
  325. if (err)
  326. return ERR_PTR(err);
  327. rb_link_node(&new->so_server_node, parent, p);
  328. rb_insert_color(&new->so_server_node, &server->state_owners);
  329. return new;
  330. }
  331. static void
  332. nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
  333. {
  334. struct nfs_server *server = sp->so_server;
  335. if (!RB_EMPTY_NODE(&sp->so_server_node))
  336. rb_erase(&sp->so_server_node, &server->state_owners);
  337. ida_remove(&server->openowner_id, sp->so_seqid.owner_id);
  338. }
  339. static void
  340. nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
  341. {
  342. sc->flags = 0;
  343. sc->counter = 0;
  344. spin_lock_init(&sc->lock);
  345. INIT_LIST_HEAD(&sc->list);
  346. rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
  347. }
  348. static void
  349. nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
  350. {
  351. rpc_destroy_wait_queue(&sc->wait);
  352. }
  353. /*
  354. * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
  355. * create a new state_owner.
  356. *
  357. */
  358. static struct nfs4_state_owner *
  359. nfs4_alloc_state_owner(struct nfs_server *server,
  360. struct rpc_cred *cred,
  361. gfp_t gfp_flags)
  362. {
  363. struct nfs4_state_owner *sp;
  364. sp = kzalloc(sizeof(*sp), gfp_flags);
  365. if (!sp)
  366. return NULL;
  367. sp->so_server = server;
  368. sp->so_cred = get_rpccred(cred);
  369. spin_lock_init(&sp->so_lock);
  370. INIT_LIST_HEAD(&sp->so_states);
  371. nfs4_init_seqid_counter(&sp->so_seqid);
  372. atomic_set(&sp->so_count, 1);
  373. INIT_LIST_HEAD(&sp->so_lru);
  374. return sp;
  375. }
  376. static void
  377. nfs4_drop_state_owner(struct nfs4_state_owner *sp)
  378. {
  379. if (!RB_EMPTY_NODE(&sp->so_server_node)) {
  380. struct nfs_server *server = sp->so_server;
  381. struct nfs_client *clp = server->nfs_client;
  382. spin_lock(&clp->cl_lock);
  383. rb_erase(&sp->so_server_node, &server->state_owners);
  384. RB_CLEAR_NODE(&sp->so_server_node);
  385. spin_unlock(&clp->cl_lock);
  386. }
  387. }
  388. static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
  389. {
  390. nfs4_destroy_seqid_counter(&sp->so_seqid);
  391. put_rpccred(sp->so_cred);
  392. kfree(sp);
  393. }
  394. static void nfs4_gc_state_owners(struct nfs_server *server)
  395. {
  396. struct nfs_client *clp = server->nfs_client;
  397. struct nfs4_state_owner *sp, *tmp;
  398. unsigned long time_min, time_max;
  399. LIST_HEAD(doomed);
  400. spin_lock(&clp->cl_lock);
  401. time_max = jiffies;
  402. time_min = (long)time_max - (long)clp->cl_lease_time;
  403. list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
  404. /* NB: LRU is sorted so that oldest is at the head */
  405. if (time_in_range(sp->so_expires, time_min, time_max))
  406. break;
  407. list_move(&sp->so_lru, &doomed);
  408. nfs4_remove_state_owner_locked(sp);
  409. }
  410. spin_unlock(&clp->cl_lock);
  411. list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
  412. list_del(&sp->so_lru);
  413. nfs4_free_state_owner(sp);
  414. }
  415. }
  416. /**
  417. * nfs4_get_state_owner - Look up a state owner given a credential
  418. * @server: nfs_server to search
  419. * @cred: RPC credential to match
  420. *
  421. * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
  422. */
  423. struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
  424. struct rpc_cred *cred,
  425. gfp_t gfp_flags)
  426. {
  427. struct nfs_client *clp = server->nfs_client;
  428. struct nfs4_state_owner *sp, *new;
  429. spin_lock(&clp->cl_lock);
  430. sp = nfs4_find_state_owner_locked(server, cred);
  431. spin_unlock(&clp->cl_lock);
  432. if (sp != NULL)
  433. goto out;
  434. new = nfs4_alloc_state_owner(server, cred, gfp_flags);
  435. if (new == NULL)
  436. goto out;
  437. do {
  438. if (ida_pre_get(&server->openowner_id, gfp_flags) == 0)
  439. break;
  440. spin_lock(&clp->cl_lock);
  441. sp = nfs4_insert_state_owner_locked(new);
  442. spin_unlock(&clp->cl_lock);
  443. } while (sp == ERR_PTR(-EAGAIN));
  444. if (sp != new)
  445. nfs4_free_state_owner(new);
  446. out:
  447. nfs4_gc_state_owners(server);
  448. return sp;
  449. }
  450. /**
  451. * nfs4_put_state_owner - Release a nfs4_state_owner
  452. * @sp: state owner data to release
  453. */
  454. void nfs4_put_state_owner(struct nfs4_state_owner *sp)
  455. {
  456. struct nfs_server *server = sp->so_server;
  457. struct nfs_client *clp = server->nfs_client;
  458. if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
  459. return;
  460. if (!RB_EMPTY_NODE(&sp->so_server_node)) {
  461. sp->so_expires = jiffies;
  462. list_add_tail(&sp->so_lru, &server->state_owners_lru);
  463. spin_unlock(&clp->cl_lock);
  464. } else {
  465. nfs4_remove_state_owner_locked(sp);
  466. spin_unlock(&clp->cl_lock);
  467. nfs4_free_state_owner(sp);
  468. }
  469. }
  470. /**
  471. * nfs4_purge_state_owners - Release all cached state owners
  472. * @server: nfs_server with cached state owners to release
  473. *
  474. * Called at umount time. Remaining state owners will be on
  475. * the LRU with ref count of zero.
  476. */
  477. void nfs4_purge_state_owners(struct nfs_server *server)
  478. {
  479. struct nfs_client *clp = server->nfs_client;
  480. struct nfs4_state_owner *sp, *tmp;
  481. LIST_HEAD(doomed);
  482. spin_lock(&clp->cl_lock);
  483. list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
  484. list_move(&sp->so_lru, &doomed);
  485. nfs4_remove_state_owner_locked(sp);
  486. }
  487. spin_unlock(&clp->cl_lock);
  488. list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
  489. list_del(&sp->so_lru);
  490. nfs4_free_state_owner(sp);
  491. }
  492. }
  493. static struct nfs4_state *
  494. nfs4_alloc_open_state(void)
  495. {
  496. struct nfs4_state *state;
  497. state = kzalloc(sizeof(*state), GFP_NOFS);
  498. if (!state)
  499. return NULL;
  500. atomic_set(&state->count, 1);
  501. INIT_LIST_HEAD(&state->lock_states);
  502. spin_lock_init(&state->state_lock);
  503. seqlock_init(&state->seqlock);
  504. return state;
  505. }
  506. void
  507. nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
  508. {
  509. if (state->state == fmode)
  510. return;
  511. /* NB! List reordering - see the reclaim code for why. */
  512. if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
  513. if (fmode & FMODE_WRITE)
  514. list_move(&state->open_states, &state->owner->so_states);
  515. else
  516. list_move_tail(&state->open_states, &state->owner->so_states);
  517. }
  518. state->state = fmode;
  519. }
  520. static struct nfs4_state *
  521. __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
  522. {
  523. struct nfs_inode *nfsi = NFS_I(inode);
  524. struct nfs4_state *state;
  525. list_for_each_entry(state, &nfsi->open_states, inode_states) {
  526. if (state->owner != owner)
  527. continue;
  528. if (atomic_inc_not_zero(&state->count))
  529. return state;
  530. }
  531. return NULL;
  532. }
  533. static void
  534. nfs4_free_open_state(struct nfs4_state *state)
  535. {
  536. kfree(state);
  537. }
  538. struct nfs4_state *
  539. nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
  540. {
  541. struct nfs4_state *state, *new;
  542. struct nfs_inode *nfsi = NFS_I(inode);
  543. spin_lock(&inode->i_lock);
  544. state = __nfs4_find_state_byowner(inode, owner);
  545. spin_unlock(&inode->i_lock);
  546. if (state)
  547. goto out;
  548. new = nfs4_alloc_open_state();
  549. spin_lock(&owner->so_lock);
  550. spin_lock(&inode->i_lock);
  551. state = __nfs4_find_state_byowner(inode, owner);
  552. if (state == NULL && new != NULL) {
  553. state = new;
  554. state->owner = owner;
  555. atomic_inc(&owner->so_count);
  556. list_add(&state->inode_states, &nfsi->open_states);
  557. ihold(inode);
  558. state->inode = inode;
  559. spin_unlock(&inode->i_lock);
  560. /* Note: The reclaim code dictates that we add stateless
  561. * and read-only stateids to the end of the list */
  562. list_add_tail(&state->open_states, &owner->so_states);
  563. spin_unlock(&owner->so_lock);
  564. } else {
  565. spin_unlock(&inode->i_lock);
  566. spin_unlock(&owner->so_lock);
  567. if (new)
  568. nfs4_free_open_state(new);
  569. }
  570. out:
  571. return state;
  572. }
  573. void nfs4_put_open_state(struct nfs4_state *state)
  574. {
  575. struct inode *inode = state->inode;
  576. struct nfs4_state_owner *owner = state->owner;
  577. if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
  578. return;
  579. spin_lock(&inode->i_lock);
  580. list_del(&state->inode_states);
  581. list_del(&state->open_states);
  582. spin_unlock(&inode->i_lock);
  583. spin_unlock(&owner->so_lock);
  584. iput(inode);
  585. nfs4_free_open_state(state);
  586. nfs4_put_state_owner(owner);
  587. }
  588. /*
  589. * Close the current file.
  590. */
  591. static void __nfs4_close(struct nfs4_state *state,
  592. fmode_t fmode, gfp_t gfp_mask, int wait)
  593. {
  594. struct nfs4_state_owner *owner = state->owner;
  595. int call_close = 0;
  596. fmode_t newstate;
  597. atomic_inc(&owner->so_count);
  598. /* Protect against nfs4_find_state() */
  599. spin_lock(&owner->so_lock);
  600. switch (fmode & (FMODE_READ | FMODE_WRITE)) {
  601. case FMODE_READ:
  602. state->n_rdonly--;
  603. break;
  604. case FMODE_WRITE:
  605. state->n_wronly--;
  606. break;
  607. case FMODE_READ|FMODE_WRITE:
  608. state->n_rdwr--;
  609. }
  610. newstate = FMODE_READ|FMODE_WRITE;
  611. if (state->n_rdwr == 0) {
  612. if (state->n_rdonly == 0) {
  613. newstate &= ~FMODE_READ;
  614. call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
  615. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  616. }
  617. if (state->n_wronly == 0) {
  618. newstate &= ~FMODE_WRITE;
  619. call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
  620. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  621. }
  622. if (newstate == 0)
  623. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  624. }
  625. nfs4_state_set_mode_locked(state, newstate);
  626. spin_unlock(&owner->so_lock);
  627. if (!call_close) {
  628. nfs4_put_open_state(state);
  629. nfs4_put_state_owner(owner);
  630. } else {
  631. bool roc = pnfs_roc(state->inode);
  632. nfs4_do_close(state, gfp_mask, wait, roc);
  633. }
  634. }
  635. void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
  636. {
  637. __nfs4_close(state, fmode, GFP_NOFS, 0);
  638. }
  639. void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
  640. {
  641. __nfs4_close(state, fmode, GFP_KERNEL, 1);
  642. }
  643. /*
  644. * Search the state->lock_states for an existing lock_owner
  645. * that is compatible with current->files
  646. */
  647. static struct nfs4_lock_state *
  648. __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
  649. {
  650. struct nfs4_lock_state *pos;
  651. list_for_each_entry(pos, &state->lock_states, ls_locks) {
  652. if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
  653. continue;
  654. switch (pos->ls_owner.lo_type) {
  655. case NFS4_POSIX_LOCK_TYPE:
  656. if (pos->ls_owner.lo_u.posix_owner != fl_owner)
  657. continue;
  658. break;
  659. case NFS4_FLOCK_LOCK_TYPE:
  660. if (pos->ls_owner.lo_u.flock_owner != fl_pid)
  661. continue;
  662. }
  663. atomic_inc(&pos->ls_count);
  664. return pos;
  665. }
  666. return NULL;
  667. }
  668. /*
  669. * Return a compatible lock_state. If no initialized lock_state structure
  670. * exists, return an uninitialized one.
  671. *
  672. */
  673. static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
  674. {
  675. struct nfs4_lock_state *lsp;
  676. struct nfs_server *server = state->owner->so_server;
  677. lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
  678. if (lsp == NULL)
  679. return NULL;
  680. nfs4_init_seqid_counter(&lsp->ls_seqid);
  681. atomic_set(&lsp->ls_count, 1);
  682. lsp->ls_state = state;
  683. lsp->ls_owner.lo_type = type;
  684. switch (lsp->ls_owner.lo_type) {
  685. case NFS4_FLOCK_LOCK_TYPE:
  686. lsp->ls_owner.lo_u.flock_owner = fl_pid;
  687. break;
  688. case NFS4_POSIX_LOCK_TYPE:
  689. lsp->ls_owner.lo_u.posix_owner = fl_owner;
  690. break;
  691. default:
  692. goto out_free;
  693. }
  694. lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS);
  695. if (lsp->ls_seqid.owner_id < 0)
  696. goto out_free;
  697. INIT_LIST_HEAD(&lsp->ls_locks);
  698. return lsp;
  699. out_free:
  700. kfree(lsp);
  701. return NULL;
  702. }
  703. void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
  704. {
  705. struct nfs_server *server = lsp->ls_state->owner->so_server;
  706. ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id);
  707. nfs4_destroy_seqid_counter(&lsp->ls_seqid);
  708. kfree(lsp);
  709. }
  710. /*
  711. * Return a compatible lock_state. If no initialized lock_state structure
  712. * exists, return an uninitialized one.
  713. *
  714. */
  715. static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type)
  716. {
  717. struct nfs4_lock_state *lsp, *new = NULL;
  718. for(;;) {
  719. spin_lock(&state->state_lock);
  720. lsp = __nfs4_find_lock_state(state, owner, pid, type);
  721. if (lsp != NULL)
  722. break;
  723. if (new != NULL) {
  724. list_add(&new->ls_locks, &state->lock_states);
  725. set_bit(LK_STATE_IN_USE, &state->flags);
  726. lsp = new;
  727. new = NULL;
  728. break;
  729. }
  730. spin_unlock(&state->state_lock);
  731. new = nfs4_alloc_lock_state(state, owner, pid, type);
  732. if (new == NULL)
  733. return NULL;
  734. }
  735. spin_unlock(&state->state_lock);
  736. if (new != NULL)
  737. nfs4_free_lock_state(new);
  738. return lsp;
  739. }
  740. /*
  741. * Release reference to lock_state, and free it if we see that
  742. * it is no longer in use
  743. */
  744. void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
  745. {
  746. struct nfs4_state *state;
  747. if (lsp == NULL)
  748. return;
  749. state = lsp->ls_state;
  750. if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
  751. return;
  752. list_del(&lsp->ls_locks);
  753. if (list_empty(&state->lock_states))
  754. clear_bit(LK_STATE_IN_USE, &state->flags);
  755. spin_unlock(&state->state_lock);
  756. if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
  757. if (nfs4_release_lockowner(lsp) == 0)
  758. return;
  759. }
  760. nfs4_free_lock_state(lsp);
  761. }
  762. static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
  763. {
  764. struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
  765. dst->fl_u.nfs4_fl.owner = lsp;
  766. atomic_inc(&lsp->ls_count);
  767. }
  768. static void nfs4_fl_release_lock(struct file_lock *fl)
  769. {
  770. nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
  771. }
  772. static const struct file_lock_operations nfs4_fl_lock_ops = {
  773. .fl_copy_lock = nfs4_fl_copy_lock,
  774. .fl_release_private = nfs4_fl_release_lock,
  775. };
  776. int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
  777. {
  778. struct nfs4_lock_state *lsp;
  779. if (fl->fl_ops != NULL)
  780. return 0;
  781. if (fl->fl_flags & FL_POSIX)
  782. lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
  783. else if (fl->fl_flags & FL_FLOCK)
  784. lsp = nfs4_get_lock_state(state, NULL, fl->fl_pid,
  785. NFS4_FLOCK_LOCK_TYPE);
  786. else
  787. return -EINVAL;
  788. if (lsp == NULL)
  789. return -ENOMEM;
  790. fl->fl_u.nfs4_fl.owner = lsp;
  791. fl->fl_ops = &nfs4_fl_lock_ops;
  792. return 0;
  793. }
  794. static bool nfs4_copy_lock_stateid(nfs4_stateid *dst, struct nfs4_state *state,
  795. fl_owner_t fl_owner, pid_t fl_pid)
  796. {
  797. struct nfs4_lock_state *lsp;
  798. bool ret = false;
  799. if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
  800. goto out;
  801. spin_lock(&state->state_lock);
  802. lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
  803. if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0) {
  804. nfs4_stateid_copy(dst, &lsp->ls_stateid);
  805. ret = true;
  806. }
  807. spin_unlock(&state->state_lock);
  808. nfs4_put_lock_state(lsp);
  809. out:
  810. return ret;
  811. }
  812. static void nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
  813. {
  814. int seq;
  815. do {
  816. seq = read_seqbegin(&state->seqlock);
  817. nfs4_stateid_copy(dst, &state->stateid);
  818. } while (read_seqretry(&state->seqlock, seq));
  819. }
  820. /*
  821. * Byte-range lock aware utility to initialize the stateid of read/write
  822. * requests.
  823. */
  824. void nfs4_select_rw_stateid(nfs4_stateid *dst, struct nfs4_state *state,
  825. fmode_t fmode, fl_owner_t fl_owner, pid_t fl_pid)
  826. {
  827. if (nfs4_copy_delegation_stateid(dst, state->inode, fmode))
  828. return;
  829. if (nfs4_copy_lock_stateid(dst, state, fl_owner, fl_pid))
  830. return;
  831. nfs4_copy_open_stateid(dst, state);
  832. }
  833. struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
  834. {
  835. struct nfs_seqid *new;
  836. new = kmalloc(sizeof(*new), gfp_mask);
  837. if (new != NULL) {
  838. new->sequence = counter;
  839. INIT_LIST_HEAD(&new->list);
  840. new->task = NULL;
  841. }
  842. return new;
  843. }
  844. void nfs_release_seqid(struct nfs_seqid *seqid)
  845. {
  846. struct nfs_seqid_counter *sequence;
  847. if (list_empty(&seqid->list))
  848. return;
  849. sequence = seqid->sequence;
  850. spin_lock(&sequence->lock);
  851. list_del_init(&seqid->list);
  852. if (!list_empty(&sequence->list)) {
  853. struct nfs_seqid *next;
  854. next = list_first_entry(&sequence->list,
  855. struct nfs_seqid, list);
  856. rpc_wake_up_queued_task(&sequence->wait, next->task);
  857. }
  858. spin_unlock(&sequence->lock);
  859. }
  860. void nfs_free_seqid(struct nfs_seqid *seqid)
  861. {
  862. nfs_release_seqid(seqid);
  863. kfree(seqid);
  864. }
  865. /*
  866. * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
  867. * failed with a seqid incrementing error -
  868. * see comments nfs_fs.h:seqid_mutating_error()
  869. */
  870. static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
  871. {
  872. BUG_ON(list_first_entry(&seqid->sequence->list, struct nfs_seqid, list) != seqid);
  873. switch (status) {
  874. case 0:
  875. break;
  876. case -NFS4ERR_BAD_SEQID:
  877. if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
  878. return;
  879. pr_warn_ratelimited("NFS: v4 server returned a bad"
  880. " sequence-id error on an"
  881. " unconfirmed sequence %p!\n",
  882. seqid->sequence);
  883. case -NFS4ERR_STALE_CLIENTID:
  884. case -NFS4ERR_STALE_STATEID:
  885. case -NFS4ERR_BAD_STATEID:
  886. case -NFS4ERR_BADXDR:
  887. case -NFS4ERR_RESOURCE:
  888. case -NFS4ERR_NOFILEHANDLE:
  889. /* Non-seqid mutating errors */
  890. return;
  891. };
  892. /*
  893. * Note: no locking needed as we are guaranteed to be first
  894. * on the sequence list
  895. */
  896. seqid->sequence->counter++;
  897. }
  898. void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
  899. {
  900. struct nfs4_state_owner *sp = container_of(seqid->sequence,
  901. struct nfs4_state_owner, so_seqid);
  902. struct nfs_server *server = sp->so_server;
  903. if (status == -NFS4ERR_BAD_SEQID)
  904. nfs4_drop_state_owner(sp);
  905. if (!nfs4_has_session(server->nfs_client))
  906. nfs_increment_seqid(status, seqid);
  907. }
  908. /*
  909. * Increment the seqid if the LOCK/LOCKU succeeded, or
  910. * failed with a seqid incrementing error -
  911. * see comments nfs_fs.h:seqid_mutating_error()
  912. */
  913. void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
  914. {
  915. nfs_increment_seqid(status, seqid);
  916. }
  917. int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
  918. {
  919. struct nfs_seqid_counter *sequence = seqid->sequence;
  920. int status = 0;
  921. spin_lock(&sequence->lock);
  922. seqid->task = task;
  923. if (list_empty(&seqid->list))
  924. list_add_tail(&seqid->list, &sequence->list);
  925. if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
  926. goto unlock;
  927. rpc_sleep_on(&sequence->wait, task, NULL);
  928. status = -EAGAIN;
  929. unlock:
  930. spin_unlock(&sequence->lock);
  931. return status;
  932. }
  933. static int nfs4_run_state_manager(void *);
  934. static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
  935. {
  936. smp_mb__before_clear_bit();
  937. clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
  938. smp_mb__after_clear_bit();
  939. wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
  940. rpc_wake_up(&clp->cl_rpcwaitq);
  941. }
  942. /*
  943. * Schedule the nfs_client asynchronous state management routine
  944. */
  945. void nfs4_schedule_state_manager(struct nfs_client *clp)
  946. {
  947. struct task_struct *task;
  948. char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
  949. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  950. return;
  951. __module_get(THIS_MODULE);
  952. atomic_inc(&clp->cl_count);
  953. /* The rcu_read_lock() is not strictly necessary, as the state
  954. * manager is the only thread that ever changes the rpc_xprt
  955. * after it's initialized. At this point, we're single threaded. */
  956. rcu_read_lock();
  957. snprintf(buf, sizeof(buf), "%s-manager",
  958. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  959. rcu_read_unlock();
  960. task = kthread_run(nfs4_run_state_manager, clp, buf);
  961. if (IS_ERR(task)) {
  962. printk(KERN_ERR "%s: kthread_run: %ld\n",
  963. __func__, PTR_ERR(task));
  964. nfs4_clear_state_manager_bit(clp);
  965. nfs_put_client(clp);
  966. module_put(THIS_MODULE);
  967. }
  968. }
  969. /*
  970. * Schedule a lease recovery attempt
  971. */
  972. void nfs4_schedule_lease_recovery(struct nfs_client *clp)
  973. {
  974. if (!clp)
  975. return;
  976. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  977. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  978. nfs4_schedule_state_manager(clp);
  979. }
  980. EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
  981. /*
  982. * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
  983. * @clp: client to process
  984. *
  985. * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
  986. * resend of the SETCLIENTID and hence re-establish the
  987. * callback channel. Then return all existing delegations.
  988. */
  989. static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
  990. {
  991. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  992. nfs_expire_all_delegations(clp);
  993. }
  994. void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
  995. {
  996. nfs40_handle_cb_pathdown(clp);
  997. nfs4_schedule_state_manager(clp);
  998. }
  999. static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
  1000. {
  1001. set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1002. /* Don't recover state that expired before the reboot */
  1003. if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
  1004. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1005. return 0;
  1006. }
  1007. set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
  1008. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  1009. return 1;
  1010. }
  1011. static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
  1012. {
  1013. set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
  1014. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1015. set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
  1016. set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
  1017. return 1;
  1018. }
  1019. void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
  1020. {
  1021. struct nfs_client *clp = server->nfs_client;
  1022. nfs4_state_mark_reclaim_nograce(clp, state);
  1023. nfs4_schedule_state_manager(clp);
  1024. }
  1025. EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
  1026. void nfs_inode_find_state_and_recover(struct inode *inode,
  1027. const nfs4_stateid *stateid)
  1028. {
  1029. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  1030. struct nfs_inode *nfsi = NFS_I(inode);
  1031. struct nfs_open_context *ctx;
  1032. struct nfs4_state *state;
  1033. bool found = false;
  1034. spin_lock(&inode->i_lock);
  1035. list_for_each_entry(ctx, &nfsi->open_files, list) {
  1036. state = ctx->state;
  1037. if (state == NULL)
  1038. continue;
  1039. if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
  1040. continue;
  1041. if (!nfs4_stateid_match(&state->stateid, stateid))
  1042. continue;
  1043. nfs4_state_mark_reclaim_nograce(clp, state);
  1044. found = true;
  1045. }
  1046. spin_unlock(&inode->i_lock);
  1047. if (found)
  1048. nfs4_schedule_state_manager(clp);
  1049. }
  1050. static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
  1051. {
  1052. struct inode *inode = state->inode;
  1053. struct nfs_inode *nfsi = NFS_I(inode);
  1054. struct file_lock *fl;
  1055. int status = 0;
  1056. if (inode->i_flock == NULL)
  1057. return 0;
  1058. /* Guard against delegation returns and new lock/unlock calls */
  1059. down_write(&nfsi->rwsem);
  1060. /* Protect inode->i_flock using the BKL */
  1061. lock_flocks();
  1062. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  1063. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  1064. continue;
  1065. if (nfs_file_open_context(fl->fl_file)->state != state)
  1066. continue;
  1067. unlock_flocks();
  1068. status = ops->recover_lock(state, fl);
  1069. switch (status) {
  1070. case 0:
  1071. break;
  1072. case -ESTALE:
  1073. case -NFS4ERR_ADMIN_REVOKED:
  1074. case -NFS4ERR_STALE_STATEID:
  1075. case -NFS4ERR_BAD_STATEID:
  1076. case -NFS4ERR_EXPIRED:
  1077. case -NFS4ERR_NO_GRACE:
  1078. case -NFS4ERR_STALE_CLIENTID:
  1079. case -NFS4ERR_BADSESSION:
  1080. case -NFS4ERR_BADSLOT:
  1081. case -NFS4ERR_BAD_HIGH_SLOT:
  1082. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1083. goto out;
  1084. default:
  1085. printk(KERN_ERR "NFS: %s: unhandled error %d. "
  1086. "Zeroing state\n", __func__, status);
  1087. case -ENOMEM:
  1088. case -NFS4ERR_DENIED:
  1089. case -NFS4ERR_RECLAIM_BAD:
  1090. case -NFS4ERR_RECLAIM_CONFLICT:
  1091. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  1092. status = 0;
  1093. }
  1094. lock_flocks();
  1095. }
  1096. unlock_flocks();
  1097. out:
  1098. up_write(&nfsi->rwsem);
  1099. return status;
  1100. }
  1101. static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
  1102. {
  1103. struct nfs4_state *state;
  1104. struct nfs4_lock_state *lock;
  1105. int status = 0;
  1106. /* Note: we rely on the sp->so_states list being ordered
  1107. * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
  1108. * states first.
  1109. * This is needed to ensure that the server won't give us any
  1110. * read delegations that we have to return if, say, we are
  1111. * recovering after a network partition or a reboot from a
  1112. * server that doesn't support a grace period.
  1113. */
  1114. restart:
  1115. spin_lock(&sp->so_lock);
  1116. list_for_each_entry(state, &sp->so_states, open_states) {
  1117. if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
  1118. continue;
  1119. if (state->state == 0)
  1120. continue;
  1121. atomic_inc(&state->count);
  1122. spin_unlock(&sp->so_lock);
  1123. status = ops->recover_open(sp, state);
  1124. if (status >= 0) {
  1125. status = nfs4_reclaim_locks(state, ops);
  1126. if (status >= 0) {
  1127. spin_lock(&state->state_lock);
  1128. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  1129. if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
  1130. pr_warn_ratelimited("NFS: "
  1131. "%s: Lock reclaim "
  1132. "failed!\n", __func__);
  1133. }
  1134. spin_unlock(&state->state_lock);
  1135. nfs4_put_open_state(state);
  1136. goto restart;
  1137. }
  1138. }
  1139. switch (status) {
  1140. default:
  1141. printk(KERN_ERR "NFS: %s: unhandled error %d. "
  1142. "Zeroing state\n", __func__, status);
  1143. case -ENOENT:
  1144. case -ENOMEM:
  1145. case -ESTALE:
  1146. /*
  1147. * Open state on this file cannot be recovered
  1148. * All we can do is revert to using the zero stateid.
  1149. */
  1150. memset(&state->stateid, 0,
  1151. sizeof(state->stateid));
  1152. /* Mark the file as being 'closed' */
  1153. state->state = 0;
  1154. break;
  1155. case -EKEYEXPIRED:
  1156. /*
  1157. * User RPCSEC_GSS context has expired.
  1158. * We cannot recover this stateid now, so
  1159. * skip it and allow recovery thread to
  1160. * proceed.
  1161. */
  1162. break;
  1163. case -NFS4ERR_ADMIN_REVOKED:
  1164. case -NFS4ERR_STALE_STATEID:
  1165. case -NFS4ERR_BAD_STATEID:
  1166. case -NFS4ERR_RECLAIM_BAD:
  1167. case -NFS4ERR_RECLAIM_CONFLICT:
  1168. nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
  1169. break;
  1170. case -NFS4ERR_EXPIRED:
  1171. case -NFS4ERR_NO_GRACE:
  1172. nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
  1173. case -NFS4ERR_STALE_CLIENTID:
  1174. case -NFS4ERR_BADSESSION:
  1175. case -NFS4ERR_BADSLOT:
  1176. case -NFS4ERR_BAD_HIGH_SLOT:
  1177. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1178. goto out_err;
  1179. }
  1180. nfs4_put_open_state(state);
  1181. goto restart;
  1182. }
  1183. spin_unlock(&sp->so_lock);
  1184. return 0;
  1185. out_err:
  1186. nfs4_put_open_state(state);
  1187. return status;
  1188. }
  1189. static void nfs4_clear_open_state(struct nfs4_state *state)
  1190. {
  1191. struct nfs4_lock_state *lock;
  1192. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1193. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1194. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1195. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1196. spin_lock(&state->state_lock);
  1197. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  1198. lock->ls_seqid.flags = 0;
  1199. lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
  1200. }
  1201. spin_unlock(&state->state_lock);
  1202. }
  1203. static void nfs4_reset_seqids(struct nfs_server *server,
  1204. int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  1205. {
  1206. struct nfs_client *clp = server->nfs_client;
  1207. struct nfs4_state_owner *sp;
  1208. struct rb_node *pos;
  1209. struct nfs4_state *state;
  1210. spin_lock(&clp->cl_lock);
  1211. for (pos = rb_first(&server->state_owners);
  1212. pos != NULL;
  1213. pos = rb_next(pos)) {
  1214. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  1215. sp->so_seqid.flags = 0;
  1216. spin_lock(&sp->so_lock);
  1217. list_for_each_entry(state, &sp->so_states, open_states) {
  1218. if (mark_reclaim(clp, state))
  1219. nfs4_clear_open_state(state);
  1220. }
  1221. spin_unlock(&sp->so_lock);
  1222. }
  1223. spin_unlock(&clp->cl_lock);
  1224. }
  1225. static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
  1226. int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  1227. {
  1228. struct nfs_server *server;
  1229. rcu_read_lock();
  1230. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1231. nfs4_reset_seqids(server, mark_reclaim);
  1232. rcu_read_unlock();
  1233. }
  1234. static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
  1235. {
  1236. /* Mark all delegations for reclaim */
  1237. nfs_delegation_mark_reclaim(clp);
  1238. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
  1239. }
  1240. static void nfs4_reclaim_complete(struct nfs_client *clp,
  1241. const struct nfs4_state_recovery_ops *ops)
  1242. {
  1243. /* Notify the server we're done reclaiming our state */
  1244. if (ops->reclaim_complete)
  1245. (void)ops->reclaim_complete(clp);
  1246. }
  1247. static void nfs4_clear_reclaim_server(struct nfs_server *server)
  1248. {
  1249. struct nfs_client *clp = server->nfs_client;
  1250. struct nfs4_state_owner *sp;
  1251. struct rb_node *pos;
  1252. struct nfs4_state *state;
  1253. spin_lock(&clp->cl_lock);
  1254. for (pos = rb_first(&server->state_owners);
  1255. pos != NULL;
  1256. pos = rb_next(pos)) {
  1257. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  1258. spin_lock(&sp->so_lock);
  1259. list_for_each_entry(state, &sp->so_states, open_states) {
  1260. if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
  1261. &state->flags))
  1262. continue;
  1263. nfs4_state_mark_reclaim_nograce(clp, state);
  1264. }
  1265. spin_unlock(&sp->so_lock);
  1266. }
  1267. spin_unlock(&clp->cl_lock);
  1268. }
  1269. static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
  1270. {
  1271. struct nfs_server *server;
  1272. if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1273. return 0;
  1274. rcu_read_lock();
  1275. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1276. nfs4_clear_reclaim_server(server);
  1277. rcu_read_unlock();
  1278. nfs_delegation_reap_unclaimed(clp);
  1279. return 1;
  1280. }
  1281. static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
  1282. {
  1283. if (!nfs4_state_clear_reclaim_reboot(clp))
  1284. return;
  1285. nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
  1286. }
  1287. static void nfs_delegation_clear_all(struct nfs_client *clp)
  1288. {
  1289. nfs_delegation_mark_reclaim(clp);
  1290. nfs_delegation_reap_unclaimed(clp);
  1291. }
  1292. static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
  1293. {
  1294. nfs_delegation_clear_all(clp);
  1295. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
  1296. }
  1297. static void nfs4_warn_keyexpired(const char *s)
  1298. {
  1299. printk_ratelimited(KERN_WARNING "Error: state manager"
  1300. " encountered RPCSEC_GSS session"
  1301. " expired against NFSv4 server %s.\n",
  1302. s);
  1303. }
  1304. static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
  1305. {
  1306. switch (error) {
  1307. case 0:
  1308. break;
  1309. case -NFS4ERR_CB_PATH_DOWN:
  1310. nfs40_handle_cb_pathdown(clp);
  1311. break;
  1312. case -NFS4ERR_NO_GRACE:
  1313. nfs4_state_end_reclaim_reboot(clp);
  1314. break;
  1315. case -NFS4ERR_STALE_CLIENTID:
  1316. case -NFS4ERR_LEASE_MOVED:
  1317. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1318. nfs4_state_clear_reclaim_reboot(clp);
  1319. nfs4_state_start_reclaim_reboot(clp);
  1320. break;
  1321. case -NFS4ERR_EXPIRED:
  1322. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1323. nfs4_state_start_reclaim_nograce(clp);
  1324. break;
  1325. case -NFS4ERR_BADSESSION:
  1326. case -NFS4ERR_BADSLOT:
  1327. case -NFS4ERR_BAD_HIGH_SLOT:
  1328. case -NFS4ERR_DEADSESSION:
  1329. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1330. case -NFS4ERR_SEQ_FALSE_RETRY:
  1331. case -NFS4ERR_SEQ_MISORDERED:
  1332. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1333. /* Zero session reset errors */
  1334. break;
  1335. case -EKEYEXPIRED:
  1336. /* Nothing we can do */
  1337. nfs4_warn_keyexpired(clp->cl_hostname);
  1338. break;
  1339. default:
  1340. return error;
  1341. }
  1342. return 0;
  1343. }
  1344. static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
  1345. {
  1346. struct nfs4_state_owner *sp;
  1347. struct nfs_server *server;
  1348. struct rb_node *pos;
  1349. int status = 0;
  1350. restart:
  1351. rcu_read_lock();
  1352. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  1353. nfs4_purge_state_owners(server);
  1354. spin_lock(&clp->cl_lock);
  1355. for (pos = rb_first(&server->state_owners);
  1356. pos != NULL;
  1357. pos = rb_next(pos)) {
  1358. sp = rb_entry(pos,
  1359. struct nfs4_state_owner, so_server_node);
  1360. if (!test_and_clear_bit(ops->owner_flag_bit,
  1361. &sp->so_flags))
  1362. continue;
  1363. atomic_inc(&sp->so_count);
  1364. spin_unlock(&clp->cl_lock);
  1365. rcu_read_unlock();
  1366. status = nfs4_reclaim_open_state(sp, ops);
  1367. if (status < 0) {
  1368. set_bit(ops->owner_flag_bit, &sp->so_flags);
  1369. nfs4_put_state_owner(sp);
  1370. return nfs4_recovery_handle_error(clp, status);
  1371. }
  1372. nfs4_put_state_owner(sp);
  1373. goto restart;
  1374. }
  1375. spin_unlock(&clp->cl_lock);
  1376. }
  1377. rcu_read_unlock();
  1378. return status;
  1379. }
  1380. static int nfs4_check_lease(struct nfs_client *clp)
  1381. {
  1382. struct rpc_cred *cred;
  1383. const struct nfs4_state_maintenance_ops *ops =
  1384. clp->cl_mvops->state_renewal_ops;
  1385. int status;
  1386. /* Is the client already known to have an expired lease? */
  1387. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1388. return 0;
  1389. spin_lock(&clp->cl_lock);
  1390. cred = ops->get_state_renewal_cred_locked(clp);
  1391. spin_unlock(&clp->cl_lock);
  1392. if (cred == NULL) {
  1393. cred = nfs4_get_setclientid_cred(clp);
  1394. status = -ENOKEY;
  1395. if (cred == NULL)
  1396. goto out;
  1397. }
  1398. status = ops->renew_lease(clp, cred);
  1399. put_rpccred(cred);
  1400. out:
  1401. return nfs4_recovery_handle_error(clp, status);
  1402. }
  1403. static int nfs4_reclaim_lease(struct nfs_client *clp)
  1404. {
  1405. struct rpc_cred *cred;
  1406. const struct nfs4_state_recovery_ops *ops =
  1407. clp->cl_mvops->reboot_recovery_ops;
  1408. int status = -ENOENT;
  1409. cred = ops->get_clid_cred(clp);
  1410. if (cred != NULL) {
  1411. status = ops->establish_clid(clp, cred);
  1412. put_rpccred(cred);
  1413. /* Handle case where the user hasn't set up machine creds */
  1414. if (status == -EACCES && cred == clp->cl_machine_cred) {
  1415. nfs4_clear_machine_cred(clp);
  1416. status = -EAGAIN;
  1417. }
  1418. if (status == -NFS4ERR_MINOR_VERS_MISMATCH)
  1419. status = -EPROTONOSUPPORT;
  1420. }
  1421. return status;
  1422. }
  1423. #ifdef CONFIG_NFS_V4_1
  1424. void nfs4_schedule_session_recovery(struct nfs4_session *session)
  1425. {
  1426. struct nfs_client *clp = session->clp;
  1427. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1428. nfs4_schedule_lease_recovery(clp);
  1429. }
  1430. EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
  1431. void nfs41_handle_recall_slot(struct nfs_client *clp)
  1432. {
  1433. set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
  1434. nfs4_schedule_state_manager(clp);
  1435. }
  1436. static void nfs4_reset_all_state(struct nfs_client *clp)
  1437. {
  1438. if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
  1439. clp->cl_boot_time = CURRENT_TIME;
  1440. nfs4_state_start_reclaim_nograce(clp);
  1441. nfs4_schedule_state_manager(clp);
  1442. }
  1443. }
  1444. static void nfs41_handle_server_reboot(struct nfs_client *clp)
  1445. {
  1446. if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
  1447. nfs4_state_start_reclaim_reboot(clp);
  1448. nfs4_schedule_state_manager(clp);
  1449. }
  1450. }
  1451. static void nfs41_handle_state_revoked(struct nfs_client *clp)
  1452. {
  1453. /* Temporary */
  1454. nfs4_reset_all_state(clp);
  1455. }
  1456. static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
  1457. {
  1458. /* This will need to handle layouts too */
  1459. nfs_expire_all_delegations(clp);
  1460. }
  1461. static void nfs41_handle_cb_path_down(struct nfs_client *clp)
  1462. {
  1463. nfs_expire_all_delegations(clp);
  1464. if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
  1465. nfs4_schedule_state_manager(clp);
  1466. }
  1467. void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
  1468. {
  1469. if (!flags)
  1470. return;
  1471. if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
  1472. nfs41_handle_server_reboot(clp);
  1473. if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
  1474. SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
  1475. SEQ4_STATUS_ADMIN_STATE_REVOKED |
  1476. SEQ4_STATUS_LEASE_MOVED))
  1477. nfs41_handle_state_revoked(clp);
  1478. if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
  1479. nfs41_handle_recallable_state_revoked(clp);
  1480. if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
  1481. SEQ4_STATUS_BACKCHANNEL_FAULT |
  1482. SEQ4_STATUS_CB_PATH_DOWN_SESSION))
  1483. nfs41_handle_cb_path_down(clp);
  1484. }
  1485. static int nfs4_reset_session(struct nfs_client *clp)
  1486. {
  1487. int status;
  1488. nfs4_begin_drain_session(clp);
  1489. status = nfs4_proc_destroy_session(clp->cl_session);
  1490. if (status && status != -NFS4ERR_BADSESSION &&
  1491. status != -NFS4ERR_DEADSESSION) {
  1492. status = nfs4_recovery_handle_error(clp, status);
  1493. goto out;
  1494. }
  1495. memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
  1496. status = nfs4_proc_create_session(clp);
  1497. if (status) {
  1498. status = nfs4_recovery_handle_error(clp, status);
  1499. goto out;
  1500. }
  1501. clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1502. /* create_session negotiated new slot table */
  1503. clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
  1504. /* Let the state manager reestablish state */
  1505. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1506. nfs41_setup_state_renewal(clp);
  1507. out:
  1508. return status;
  1509. }
  1510. static int nfs4_recall_slot(struct nfs_client *clp)
  1511. {
  1512. struct nfs4_slot_table *fc_tbl = &clp->cl_session->fc_slot_table;
  1513. struct nfs4_channel_attrs *fc_attrs = &clp->cl_session->fc_attrs;
  1514. struct nfs4_slot *new, *old;
  1515. int i;
  1516. nfs4_begin_drain_session(clp);
  1517. new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot),
  1518. GFP_NOFS);
  1519. if (!new)
  1520. return -ENOMEM;
  1521. spin_lock(&fc_tbl->slot_tbl_lock);
  1522. for (i = 0; i < fc_tbl->target_max_slots; i++)
  1523. new[i].seq_nr = fc_tbl->slots[i].seq_nr;
  1524. old = fc_tbl->slots;
  1525. fc_tbl->slots = new;
  1526. fc_tbl->max_slots = fc_tbl->target_max_slots;
  1527. fc_tbl->target_max_slots = 0;
  1528. fc_attrs->max_reqs = fc_tbl->max_slots;
  1529. spin_unlock(&fc_tbl->slot_tbl_lock);
  1530. kfree(old);
  1531. nfs4_end_drain_session(clp);
  1532. return 0;
  1533. }
  1534. #else /* CONFIG_NFS_V4_1 */
  1535. static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
  1536. static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
  1537. static int nfs4_recall_slot(struct nfs_client *clp) { return 0; }
  1538. #endif /* CONFIG_NFS_V4_1 */
  1539. /* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
  1540. * on EXCHANGE_ID for v4.1
  1541. */
  1542. static void nfs4_set_lease_expired(struct nfs_client *clp, int status)
  1543. {
  1544. switch (status) {
  1545. case -NFS4ERR_CLID_INUSE:
  1546. case -NFS4ERR_STALE_CLIENTID:
  1547. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1548. break;
  1549. case -NFS4ERR_DELAY:
  1550. case -ETIMEDOUT:
  1551. case -EAGAIN:
  1552. ssleep(1);
  1553. break;
  1554. case -EKEYEXPIRED:
  1555. nfs4_warn_keyexpired(clp->cl_hostname);
  1556. case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
  1557. * in nfs4_exchange_id */
  1558. default:
  1559. return;
  1560. }
  1561. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1562. }
  1563. static void nfs4_state_manager(struct nfs_client *clp)
  1564. {
  1565. int status = 0;
  1566. /* Ensure exclusive access to NFSv4 state */
  1567. do {
  1568. if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
  1569. /* We're going to have to re-establish a clientid */
  1570. status = nfs4_reclaim_lease(clp);
  1571. if (status) {
  1572. nfs4_set_lease_expired(clp, status);
  1573. if (test_bit(NFS4CLNT_LEASE_EXPIRED,
  1574. &clp->cl_state))
  1575. continue;
  1576. if (clp->cl_cons_state ==
  1577. NFS_CS_SESSION_INITING)
  1578. nfs_mark_client_ready(clp, status);
  1579. goto out_error;
  1580. }
  1581. clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1582. if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH,
  1583. &clp->cl_state))
  1584. nfs4_state_start_reclaim_nograce(clp);
  1585. else
  1586. set_bit(NFS4CLNT_RECLAIM_REBOOT,
  1587. &clp->cl_state);
  1588. pnfs_destroy_all_layouts(clp);
  1589. }
  1590. if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
  1591. status = nfs4_check_lease(clp);
  1592. if (status < 0)
  1593. goto out_error;
  1594. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1595. continue;
  1596. }
  1597. /* Initialize or reset the session */
  1598. if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)
  1599. && nfs4_has_session(clp)) {
  1600. status = nfs4_reset_session(clp);
  1601. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1602. continue;
  1603. if (status < 0)
  1604. goto out_error;
  1605. }
  1606. /* First recover reboot state... */
  1607. if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
  1608. status = nfs4_do_reclaim(clp,
  1609. clp->cl_mvops->reboot_recovery_ops);
  1610. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
  1611. test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
  1612. continue;
  1613. nfs4_state_end_reclaim_reboot(clp);
  1614. if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
  1615. continue;
  1616. if (status < 0)
  1617. goto out_error;
  1618. }
  1619. /* Now recover expired state... */
  1620. if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
  1621. status = nfs4_do_reclaim(clp,
  1622. clp->cl_mvops->nograce_recovery_ops);
  1623. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
  1624. test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
  1625. test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1626. continue;
  1627. if (status < 0)
  1628. goto out_error;
  1629. }
  1630. nfs4_end_drain_session(clp);
  1631. if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
  1632. nfs_client_return_marked_delegations(clp);
  1633. continue;
  1634. }
  1635. /* Recall session slots */
  1636. if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state)
  1637. && nfs4_has_session(clp)) {
  1638. status = nfs4_recall_slot(clp);
  1639. if (status < 0)
  1640. goto out_error;
  1641. continue;
  1642. }
  1643. nfs4_clear_state_manager_bit(clp);
  1644. /* Did we race with an attempt to give us more work? */
  1645. if (clp->cl_state == 0)
  1646. break;
  1647. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  1648. break;
  1649. } while (atomic_read(&clp->cl_count) > 1);
  1650. return;
  1651. out_error:
  1652. pr_warn_ratelimited("NFS: state manager failed on NFSv4 server %s"
  1653. " with error %d\n", clp->cl_hostname, -status);
  1654. nfs4_end_drain_session(clp);
  1655. nfs4_clear_state_manager_bit(clp);
  1656. }
  1657. static int nfs4_run_state_manager(void *ptr)
  1658. {
  1659. struct nfs_client *clp = ptr;
  1660. allow_signal(SIGKILL);
  1661. nfs4_state_manager(clp);
  1662. nfs_put_client(clp);
  1663. module_put_and_exit(0);
  1664. return 0;
  1665. }
  1666. /*
  1667. * Local variables:
  1668. * c-basic-offset: 8
  1669. * End:
  1670. */