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