nfs4state.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465
  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 <linux/sunrpc/clnt.h>
  53. #include "nfs4_fs.h"
  54. #include "callback.h"
  55. #include "delegation.h"
  56. #include "internal.h"
  57. #include "nfs4session.h"
  58. #include "pnfs.h"
  59. #include "netns.h"
  60. #define NFSDBG_FACILITY NFSDBG_STATE
  61. #define OPENOWNER_POOL_SIZE 8
  62. const nfs4_stateid zero_stateid;
  63. static DEFINE_MUTEX(nfs_clid_init_mutex);
  64. int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
  65. {
  66. struct nfs4_setclientid_res clid = {
  67. .clientid = clp->cl_clientid,
  68. .confirm = clp->cl_confirm,
  69. };
  70. unsigned short port;
  71. int status;
  72. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  73. if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
  74. goto do_confirm;
  75. port = nn->nfs_callback_tcpport;
  76. if (clp->cl_addr.ss_family == AF_INET6)
  77. port = nn->nfs_callback_tcpport6;
  78. status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
  79. if (status != 0)
  80. goto out;
  81. clp->cl_clientid = clid.clientid;
  82. clp->cl_confirm = clid.confirm;
  83. set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  84. do_confirm:
  85. status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
  86. if (status != 0)
  87. goto out;
  88. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  89. nfs4_schedule_state_renewal(clp);
  90. out:
  91. return status;
  92. }
  93. /**
  94. * nfs40_discover_server_trunking - Detect server IP address trunking (mv0)
  95. *
  96. * @clp: nfs_client under test
  97. * @result: OUT: found nfs_client, or clp
  98. * @cred: credential to use for trunking test
  99. *
  100. * Returns zero, a negative errno, or a negative NFS4ERR status.
  101. * If zero is returned, an nfs_client pointer is planted in
  102. * "result".
  103. *
  104. * Note: The returned client may not yet be marked ready.
  105. */
  106. int nfs40_discover_server_trunking(struct nfs_client *clp,
  107. struct nfs_client **result,
  108. struct rpc_cred *cred)
  109. {
  110. struct nfs4_setclientid_res clid = {
  111. .clientid = clp->cl_clientid,
  112. .confirm = clp->cl_confirm,
  113. };
  114. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  115. unsigned short port;
  116. int status;
  117. port = nn->nfs_callback_tcpport;
  118. if (clp->cl_addr.ss_family == AF_INET6)
  119. port = nn->nfs_callback_tcpport6;
  120. status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
  121. if (status != 0)
  122. goto out;
  123. clp->cl_clientid = clid.clientid;
  124. clp->cl_confirm = clid.confirm;
  125. status = nfs40_walk_client_list(clp, result, cred);
  126. if (status == 0) {
  127. /* Sustain the lease, even if it's empty. If the clientid4
  128. * goes stale it's of no use for trunking discovery. */
  129. nfs4_schedule_state_renewal(*result);
  130. }
  131. out:
  132. return status;
  133. }
  134. struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
  135. {
  136. struct rpc_cred *cred = NULL;
  137. if (clp->cl_machine_cred != NULL)
  138. cred = get_rpccred(clp->cl_machine_cred);
  139. return cred;
  140. }
  141. static void nfs4_root_machine_cred(struct nfs_client *clp)
  142. {
  143. struct rpc_cred *cred, *new;
  144. new = rpc_lookup_machine_cred(NULL);
  145. spin_lock(&clp->cl_lock);
  146. cred = clp->cl_machine_cred;
  147. clp->cl_machine_cred = new;
  148. spin_unlock(&clp->cl_lock);
  149. if (cred != NULL)
  150. put_rpccred(cred);
  151. }
  152. static struct rpc_cred *
  153. nfs4_get_renew_cred_server_locked(struct nfs_server *server)
  154. {
  155. struct rpc_cred *cred = NULL;
  156. struct nfs4_state_owner *sp;
  157. struct rb_node *pos;
  158. for (pos = rb_first(&server->state_owners);
  159. pos != NULL;
  160. pos = rb_next(pos)) {
  161. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  162. if (list_empty(&sp->so_states))
  163. continue;
  164. cred = get_rpccred(sp->so_cred);
  165. break;
  166. }
  167. return cred;
  168. }
  169. /**
  170. * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
  171. * @clp: client state handle
  172. *
  173. * Returns an rpc_cred with reference count bumped, or NULL.
  174. * Caller must hold clp->cl_lock.
  175. */
  176. struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
  177. {
  178. struct rpc_cred *cred = NULL;
  179. struct nfs_server *server;
  180. /* Use machine credentials if available */
  181. cred = nfs4_get_machine_cred_locked(clp);
  182. if (cred != NULL)
  183. goto out;
  184. rcu_read_lock();
  185. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  186. cred = nfs4_get_renew_cred_server_locked(server);
  187. if (cred != NULL)
  188. break;
  189. }
  190. rcu_read_unlock();
  191. out:
  192. return cred;
  193. }
  194. static void nfs4_end_drain_slot_table(struct nfs4_slot_table *tbl)
  195. {
  196. if (test_and_clear_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state)) {
  197. spin_lock(&tbl->slot_tbl_lock);
  198. nfs41_wake_slot_table(tbl);
  199. spin_unlock(&tbl->slot_tbl_lock);
  200. }
  201. }
  202. static void nfs4_end_drain_session(struct nfs_client *clp)
  203. {
  204. struct nfs4_session *ses = clp->cl_session;
  205. if (clp->cl_slot_tbl) {
  206. nfs4_end_drain_slot_table(clp->cl_slot_tbl);
  207. return;
  208. }
  209. if (ses != NULL) {
  210. nfs4_end_drain_slot_table(&ses->bc_slot_table);
  211. nfs4_end_drain_slot_table(&ses->fc_slot_table);
  212. }
  213. }
  214. static int nfs4_drain_slot_tbl(struct nfs4_slot_table *tbl)
  215. {
  216. set_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state);
  217. spin_lock(&tbl->slot_tbl_lock);
  218. if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
  219. reinit_completion(&tbl->complete);
  220. spin_unlock(&tbl->slot_tbl_lock);
  221. return wait_for_completion_interruptible(&tbl->complete);
  222. }
  223. spin_unlock(&tbl->slot_tbl_lock);
  224. return 0;
  225. }
  226. static int nfs4_begin_drain_session(struct nfs_client *clp)
  227. {
  228. struct nfs4_session *ses = clp->cl_session;
  229. int ret = 0;
  230. if (clp->cl_slot_tbl)
  231. return nfs4_drain_slot_tbl(clp->cl_slot_tbl);
  232. /* back channel */
  233. ret = nfs4_drain_slot_tbl(&ses->bc_slot_table);
  234. if (ret)
  235. return ret;
  236. /* fore channel */
  237. return nfs4_drain_slot_tbl(&ses->fc_slot_table);
  238. }
  239. #if defined(CONFIG_NFS_V4_1)
  240. static int nfs41_setup_state_renewal(struct nfs_client *clp)
  241. {
  242. int status;
  243. struct nfs_fsinfo fsinfo;
  244. if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
  245. nfs4_schedule_state_renewal(clp);
  246. return 0;
  247. }
  248. status = nfs4_proc_get_lease_time(clp, &fsinfo);
  249. if (status == 0) {
  250. /* Update lease time and schedule renewal */
  251. spin_lock(&clp->cl_lock);
  252. clp->cl_lease_time = fsinfo.lease_time * HZ;
  253. clp->cl_last_renewal = jiffies;
  254. spin_unlock(&clp->cl_lock);
  255. nfs4_schedule_state_renewal(clp);
  256. }
  257. return status;
  258. }
  259. static void nfs41_finish_session_reset(struct nfs_client *clp)
  260. {
  261. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  262. clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  263. /* create_session negotiated new slot table */
  264. clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  265. nfs41_setup_state_renewal(clp);
  266. }
  267. int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
  268. {
  269. int status;
  270. if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
  271. goto do_confirm;
  272. nfs4_begin_drain_session(clp);
  273. status = nfs4_proc_exchange_id(clp, cred);
  274. if (status != 0)
  275. goto out;
  276. set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  277. do_confirm:
  278. status = nfs4_proc_create_session(clp, cred);
  279. if (status != 0)
  280. goto out;
  281. nfs41_finish_session_reset(clp);
  282. nfs_mark_client_ready(clp, NFS_CS_READY);
  283. out:
  284. return status;
  285. }
  286. /**
  287. * nfs41_discover_server_trunking - Detect server IP address trunking (mv1)
  288. *
  289. * @clp: nfs_client under test
  290. * @result: OUT: found nfs_client, or clp
  291. * @cred: credential to use for trunking test
  292. *
  293. * Returns NFS4_OK, a negative errno, or a negative NFS4ERR status.
  294. * If NFS4_OK is returned, an nfs_client pointer is planted in
  295. * "result".
  296. *
  297. * Note: The returned client may not yet be marked ready.
  298. */
  299. int nfs41_discover_server_trunking(struct nfs_client *clp,
  300. struct nfs_client **result,
  301. struct rpc_cred *cred)
  302. {
  303. int status;
  304. status = nfs4_proc_exchange_id(clp, cred);
  305. if (status != NFS4_OK)
  306. return status;
  307. set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  308. return nfs41_walk_client_list(clp, result, cred);
  309. }
  310. #endif /* CONFIG_NFS_V4_1 */
  311. /**
  312. * nfs4_get_clid_cred - Acquire credential for a setclientid operation
  313. * @clp: client state handle
  314. *
  315. * Returns an rpc_cred with reference count bumped, or NULL.
  316. */
  317. struct rpc_cred *nfs4_get_clid_cred(struct nfs_client *clp)
  318. {
  319. struct rpc_cred *cred;
  320. spin_lock(&clp->cl_lock);
  321. cred = nfs4_get_machine_cred_locked(clp);
  322. spin_unlock(&clp->cl_lock);
  323. return cred;
  324. }
  325. static struct nfs4_state_owner *
  326. nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
  327. {
  328. struct rb_node **p = &server->state_owners.rb_node,
  329. *parent = NULL;
  330. struct nfs4_state_owner *sp;
  331. while (*p != NULL) {
  332. parent = *p;
  333. sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
  334. if (cred < sp->so_cred)
  335. p = &parent->rb_left;
  336. else if (cred > sp->so_cred)
  337. p = &parent->rb_right;
  338. else {
  339. if (!list_empty(&sp->so_lru))
  340. list_del_init(&sp->so_lru);
  341. atomic_inc(&sp->so_count);
  342. return sp;
  343. }
  344. }
  345. return NULL;
  346. }
  347. static struct nfs4_state_owner *
  348. nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
  349. {
  350. struct nfs_server *server = new->so_server;
  351. struct rb_node **p = &server->state_owners.rb_node,
  352. *parent = NULL;
  353. struct nfs4_state_owner *sp;
  354. int err;
  355. while (*p != NULL) {
  356. parent = *p;
  357. sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
  358. if (new->so_cred < sp->so_cred)
  359. p = &parent->rb_left;
  360. else if (new->so_cred > sp->so_cred)
  361. p = &parent->rb_right;
  362. else {
  363. if (!list_empty(&sp->so_lru))
  364. list_del_init(&sp->so_lru);
  365. atomic_inc(&sp->so_count);
  366. return sp;
  367. }
  368. }
  369. err = ida_get_new(&server->openowner_id, &new->so_seqid.owner_id);
  370. if (err)
  371. return ERR_PTR(err);
  372. rb_link_node(&new->so_server_node, parent, p);
  373. rb_insert_color(&new->so_server_node, &server->state_owners);
  374. return new;
  375. }
  376. static void
  377. nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
  378. {
  379. struct nfs_server *server = sp->so_server;
  380. if (!RB_EMPTY_NODE(&sp->so_server_node))
  381. rb_erase(&sp->so_server_node, &server->state_owners);
  382. ida_remove(&server->openowner_id, sp->so_seqid.owner_id);
  383. }
  384. static void
  385. nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
  386. {
  387. sc->create_time = ktime_get();
  388. sc->flags = 0;
  389. sc->counter = 0;
  390. spin_lock_init(&sc->lock);
  391. INIT_LIST_HEAD(&sc->list);
  392. rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
  393. }
  394. static void
  395. nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
  396. {
  397. rpc_destroy_wait_queue(&sc->wait);
  398. }
  399. /*
  400. * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
  401. * create a new state_owner.
  402. *
  403. */
  404. static struct nfs4_state_owner *
  405. nfs4_alloc_state_owner(struct nfs_server *server,
  406. struct rpc_cred *cred,
  407. gfp_t gfp_flags)
  408. {
  409. struct nfs4_state_owner *sp;
  410. sp = kzalloc(sizeof(*sp), gfp_flags);
  411. if (!sp)
  412. return NULL;
  413. sp->so_server = server;
  414. sp->so_cred = get_rpccred(cred);
  415. spin_lock_init(&sp->so_lock);
  416. INIT_LIST_HEAD(&sp->so_states);
  417. nfs4_init_seqid_counter(&sp->so_seqid);
  418. atomic_set(&sp->so_count, 1);
  419. INIT_LIST_HEAD(&sp->so_lru);
  420. seqcount_init(&sp->so_reclaim_seqcount);
  421. mutex_init(&sp->so_delegreturn_mutex);
  422. return sp;
  423. }
  424. static void
  425. nfs4_drop_state_owner(struct nfs4_state_owner *sp)
  426. {
  427. struct rb_node *rb_node = &sp->so_server_node;
  428. if (!RB_EMPTY_NODE(rb_node)) {
  429. struct nfs_server *server = sp->so_server;
  430. struct nfs_client *clp = server->nfs_client;
  431. spin_lock(&clp->cl_lock);
  432. if (!RB_EMPTY_NODE(rb_node)) {
  433. rb_erase(rb_node, &server->state_owners);
  434. RB_CLEAR_NODE(rb_node);
  435. }
  436. spin_unlock(&clp->cl_lock);
  437. }
  438. }
  439. static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
  440. {
  441. nfs4_destroy_seqid_counter(&sp->so_seqid);
  442. put_rpccred(sp->so_cred);
  443. kfree(sp);
  444. }
  445. static void nfs4_gc_state_owners(struct nfs_server *server)
  446. {
  447. struct nfs_client *clp = server->nfs_client;
  448. struct nfs4_state_owner *sp, *tmp;
  449. unsigned long time_min, time_max;
  450. LIST_HEAD(doomed);
  451. spin_lock(&clp->cl_lock);
  452. time_max = jiffies;
  453. time_min = (long)time_max - (long)clp->cl_lease_time;
  454. list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
  455. /* NB: LRU is sorted so that oldest is at the head */
  456. if (time_in_range(sp->so_expires, time_min, time_max))
  457. break;
  458. list_move(&sp->so_lru, &doomed);
  459. nfs4_remove_state_owner_locked(sp);
  460. }
  461. spin_unlock(&clp->cl_lock);
  462. list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
  463. list_del(&sp->so_lru);
  464. nfs4_free_state_owner(sp);
  465. }
  466. }
  467. /**
  468. * nfs4_get_state_owner - Look up a state owner given a credential
  469. * @server: nfs_server to search
  470. * @cred: RPC credential to match
  471. *
  472. * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
  473. */
  474. struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
  475. struct rpc_cred *cred,
  476. gfp_t gfp_flags)
  477. {
  478. struct nfs_client *clp = server->nfs_client;
  479. struct nfs4_state_owner *sp, *new;
  480. spin_lock(&clp->cl_lock);
  481. sp = nfs4_find_state_owner_locked(server, cred);
  482. spin_unlock(&clp->cl_lock);
  483. if (sp != NULL)
  484. goto out;
  485. new = nfs4_alloc_state_owner(server, cred, gfp_flags);
  486. if (new == NULL)
  487. goto out;
  488. do {
  489. if (ida_pre_get(&server->openowner_id, gfp_flags) == 0)
  490. break;
  491. spin_lock(&clp->cl_lock);
  492. sp = nfs4_insert_state_owner_locked(new);
  493. spin_unlock(&clp->cl_lock);
  494. } while (sp == ERR_PTR(-EAGAIN));
  495. if (sp != new)
  496. nfs4_free_state_owner(new);
  497. out:
  498. nfs4_gc_state_owners(server);
  499. return sp;
  500. }
  501. /**
  502. * nfs4_put_state_owner - Release a nfs4_state_owner
  503. * @sp: state owner data to release
  504. *
  505. * Note that we keep released state owners on an LRU
  506. * list.
  507. * This caches valid state owners so that they can be
  508. * reused, to avoid the OPEN_CONFIRM on minor version 0.
  509. * It also pins the uniquifier of dropped state owners for
  510. * a while, to ensure that those state owner names are
  511. * never reused.
  512. */
  513. void nfs4_put_state_owner(struct nfs4_state_owner *sp)
  514. {
  515. struct nfs_server *server = sp->so_server;
  516. struct nfs_client *clp = server->nfs_client;
  517. if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
  518. return;
  519. sp->so_expires = jiffies;
  520. list_add_tail(&sp->so_lru, &server->state_owners_lru);
  521. spin_unlock(&clp->cl_lock);
  522. }
  523. /**
  524. * nfs4_purge_state_owners - Release all cached state owners
  525. * @server: nfs_server with cached state owners to release
  526. *
  527. * Called at umount time. Remaining state owners will be on
  528. * the LRU with ref count of zero.
  529. */
  530. void nfs4_purge_state_owners(struct nfs_server *server)
  531. {
  532. struct nfs_client *clp = server->nfs_client;
  533. struct nfs4_state_owner *sp, *tmp;
  534. LIST_HEAD(doomed);
  535. spin_lock(&clp->cl_lock);
  536. list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
  537. list_move(&sp->so_lru, &doomed);
  538. nfs4_remove_state_owner_locked(sp);
  539. }
  540. spin_unlock(&clp->cl_lock);
  541. list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
  542. list_del(&sp->so_lru);
  543. nfs4_free_state_owner(sp);
  544. }
  545. }
  546. static struct nfs4_state *
  547. nfs4_alloc_open_state(void)
  548. {
  549. struct nfs4_state *state;
  550. state = kzalloc(sizeof(*state), GFP_NOFS);
  551. if (!state)
  552. return NULL;
  553. atomic_set(&state->count, 1);
  554. INIT_LIST_HEAD(&state->lock_states);
  555. spin_lock_init(&state->state_lock);
  556. seqlock_init(&state->seqlock);
  557. return state;
  558. }
  559. void
  560. nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
  561. {
  562. if (state->state == fmode)
  563. return;
  564. /* NB! List reordering - see the reclaim code for why. */
  565. if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
  566. if (fmode & FMODE_WRITE)
  567. list_move(&state->open_states, &state->owner->so_states);
  568. else
  569. list_move_tail(&state->open_states, &state->owner->so_states);
  570. }
  571. state->state = fmode;
  572. }
  573. static struct nfs4_state *
  574. __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
  575. {
  576. struct nfs_inode *nfsi = NFS_I(inode);
  577. struct nfs4_state *state;
  578. list_for_each_entry(state, &nfsi->open_states, inode_states) {
  579. if (state->owner != owner)
  580. continue;
  581. if (!nfs4_valid_open_stateid(state))
  582. continue;
  583. if (atomic_inc_not_zero(&state->count))
  584. return state;
  585. }
  586. return NULL;
  587. }
  588. static void
  589. nfs4_free_open_state(struct nfs4_state *state)
  590. {
  591. kfree(state);
  592. }
  593. struct nfs4_state *
  594. nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
  595. {
  596. struct nfs4_state *state, *new;
  597. struct nfs_inode *nfsi = NFS_I(inode);
  598. spin_lock(&inode->i_lock);
  599. state = __nfs4_find_state_byowner(inode, owner);
  600. spin_unlock(&inode->i_lock);
  601. if (state)
  602. goto out;
  603. new = nfs4_alloc_open_state();
  604. spin_lock(&owner->so_lock);
  605. spin_lock(&inode->i_lock);
  606. state = __nfs4_find_state_byowner(inode, owner);
  607. if (state == NULL && new != NULL) {
  608. state = new;
  609. state->owner = owner;
  610. atomic_inc(&owner->so_count);
  611. list_add(&state->inode_states, &nfsi->open_states);
  612. ihold(inode);
  613. state->inode = inode;
  614. spin_unlock(&inode->i_lock);
  615. /* Note: The reclaim code dictates that we add stateless
  616. * and read-only stateids to the end of the list */
  617. list_add_tail(&state->open_states, &owner->so_states);
  618. spin_unlock(&owner->so_lock);
  619. } else {
  620. spin_unlock(&inode->i_lock);
  621. spin_unlock(&owner->so_lock);
  622. if (new)
  623. nfs4_free_open_state(new);
  624. }
  625. out:
  626. return state;
  627. }
  628. void nfs4_put_open_state(struct nfs4_state *state)
  629. {
  630. struct inode *inode = state->inode;
  631. struct nfs4_state_owner *owner = state->owner;
  632. if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
  633. return;
  634. spin_lock(&inode->i_lock);
  635. list_del(&state->inode_states);
  636. list_del(&state->open_states);
  637. spin_unlock(&inode->i_lock);
  638. spin_unlock(&owner->so_lock);
  639. iput(inode);
  640. nfs4_free_open_state(state);
  641. nfs4_put_state_owner(owner);
  642. }
  643. /*
  644. * Close the current file.
  645. */
  646. static void __nfs4_close(struct nfs4_state *state,
  647. fmode_t fmode, gfp_t gfp_mask, int wait)
  648. {
  649. struct nfs4_state_owner *owner = state->owner;
  650. int call_close = 0;
  651. fmode_t newstate;
  652. atomic_inc(&owner->so_count);
  653. /* Protect against nfs4_find_state() */
  654. spin_lock(&owner->so_lock);
  655. switch (fmode & (FMODE_READ | FMODE_WRITE)) {
  656. case FMODE_READ:
  657. state->n_rdonly--;
  658. break;
  659. case FMODE_WRITE:
  660. state->n_wronly--;
  661. break;
  662. case FMODE_READ|FMODE_WRITE:
  663. state->n_rdwr--;
  664. }
  665. newstate = FMODE_READ|FMODE_WRITE;
  666. if (state->n_rdwr == 0) {
  667. if (state->n_rdonly == 0) {
  668. newstate &= ~FMODE_READ;
  669. call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
  670. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  671. }
  672. if (state->n_wronly == 0) {
  673. newstate &= ~FMODE_WRITE;
  674. call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
  675. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  676. }
  677. if (newstate == 0)
  678. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  679. }
  680. nfs4_state_set_mode_locked(state, newstate);
  681. spin_unlock(&owner->so_lock);
  682. if (!call_close) {
  683. nfs4_put_open_state(state);
  684. nfs4_put_state_owner(owner);
  685. } else
  686. nfs4_do_close(state, gfp_mask, wait);
  687. }
  688. void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
  689. {
  690. __nfs4_close(state, fmode, GFP_NOFS, 0);
  691. }
  692. void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
  693. {
  694. __nfs4_close(state, fmode, GFP_KERNEL, 1);
  695. }
  696. /*
  697. * Search the state->lock_states for an existing lock_owner
  698. * that is compatible with current->files
  699. */
  700. static struct nfs4_lock_state *
  701. __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
  702. {
  703. struct nfs4_lock_state *pos;
  704. list_for_each_entry(pos, &state->lock_states, ls_locks) {
  705. if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
  706. continue;
  707. switch (pos->ls_owner.lo_type) {
  708. case NFS4_POSIX_LOCK_TYPE:
  709. if (pos->ls_owner.lo_u.posix_owner != fl_owner)
  710. continue;
  711. break;
  712. case NFS4_FLOCK_LOCK_TYPE:
  713. if (pos->ls_owner.lo_u.flock_owner != fl_pid)
  714. continue;
  715. }
  716. atomic_inc(&pos->ls_count);
  717. return pos;
  718. }
  719. return NULL;
  720. }
  721. /*
  722. * Return a compatible lock_state. If no initialized lock_state structure
  723. * exists, return an uninitialized one.
  724. *
  725. */
  726. 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)
  727. {
  728. struct nfs4_lock_state *lsp;
  729. struct nfs_server *server = state->owner->so_server;
  730. lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
  731. if (lsp == NULL)
  732. return NULL;
  733. nfs4_init_seqid_counter(&lsp->ls_seqid);
  734. atomic_set(&lsp->ls_count, 1);
  735. lsp->ls_state = state;
  736. lsp->ls_owner.lo_type = type;
  737. switch (lsp->ls_owner.lo_type) {
  738. case NFS4_FLOCK_LOCK_TYPE:
  739. lsp->ls_owner.lo_u.flock_owner = fl_pid;
  740. break;
  741. case NFS4_POSIX_LOCK_TYPE:
  742. lsp->ls_owner.lo_u.posix_owner = fl_owner;
  743. break;
  744. default:
  745. goto out_free;
  746. }
  747. lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS);
  748. if (lsp->ls_seqid.owner_id < 0)
  749. goto out_free;
  750. INIT_LIST_HEAD(&lsp->ls_locks);
  751. return lsp;
  752. out_free:
  753. kfree(lsp);
  754. return NULL;
  755. }
  756. void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
  757. {
  758. ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id);
  759. nfs4_destroy_seqid_counter(&lsp->ls_seqid);
  760. kfree(lsp);
  761. }
  762. /*
  763. * Return a compatible lock_state. If no initialized lock_state structure
  764. * exists, return an uninitialized one.
  765. *
  766. */
  767. static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type)
  768. {
  769. struct nfs4_lock_state *lsp, *new = NULL;
  770. for(;;) {
  771. spin_lock(&state->state_lock);
  772. lsp = __nfs4_find_lock_state(state, owner, pid, type);
  773. if (lsp != NULL)
  774. break;
  775. if (new != NULL) {
  776. list_add(&new->ls_locks, &state->lock_states);
  777. set_bit(LK_STATE_IN_USE, &state->flags);
  778. lsp = new;
  779. new = NULL;
  780. break;
  781. }
  782. spin_unlock(&state->state_lock);
  783. new = nfs4_alloc_lock_state(state, owner, pid, type);
  784. if (new == NULL)
  785. return NULL;
  786. }
  787. spin_unlock(&state->state_lock);
  788. if (new != NULL)
  789. nfs4_free_lock_state(state->owner->so_server, new);
  790. return lsp;
  791. }
  792. /*
  793. * Release reference to lock_state, and free it if we see that
  794. * it is no longer in use
  795. */
  796. void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
  797. {
  798. struct nfs_server *server;
  799. struct nfs4_state *state;
  800. if (lsp == NULL)
  801. return;
  802. state = lsp->ls_state;
  803. if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
  804. return;
  805. list_del(&lsp->ls_locks);
  806. if (list_empty(&state->lock_states))
  807. clear_bit(LK_STATE_IN_USE, &state->flags);
  808. spin_unlock(&state->state_lock);
  809. server = state->owner->so_server;
  810. if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
  811. struct nfs_client *clp = server->nfs_client;
  812. clp->cl_mvops->free_lock_state(server, lsp);
  813. } else
  814. nfs4_free_lock_state(server, lsp);
  815. }
  816. static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
  817. {
  818. struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
  819. dst->fl_u.nfs4_fl.owner = lsp;
  820. atomic_inc(&lsp->ls_count);
  821. }
  822. static void nfs4_fl_release_lock(struct file_lock *fl)
  823. {
  824. nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
  825. }
  826. static const struct file_lock_operations nfs4_fl_lock_ops = {
  827. .fl_copy_lock = nfs4_fl_copy_lock,
  828. .fl_release_private = nfs4_fl_release_lock,
  829. };
  830. int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
  831. {
  832. struct nfs4_lock_state *lsp;
  833. if (fl->fl_ops != NULL)
  834. return 0;
  835. if (fl->fl_flags & FL_POSIX)
  836. lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
  837. else if (fl->fl_flags & FL_FLOCK)
  838. lsp = nfs4_get_lock_state(state, NULL, fl->fl_pid,
  839. NFS4_FLOCK_LOCK_TYPE);
  840. else
  841. return -EINVAL;
  842. if (lsp == NULL)
  843. return -ENOMEM;
  844. fl->fl_u.nfs4_fl.owner = lsp;
  845. fl->fl_ops = &nfs4_fl_lock_ops;
  846. return 0;
  847. }
  848. static int nfs4_copy_lock_stateid(nfs4_stateid *dst,
  849. struct nfs4_state *state,
  850. const struct nfs_lockowner *lockowner)
  851. {
  852. struct nfs4_lock_state *lsp;
  853. fl_owner_t fl_owner;
  854. pid_t fl_pid;
  855. int ret = -ENOENT;
  856. if (lockowner == NULL)
  857. goto out;
  858. if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
  859. goto out;
  860. fl_owner = lockowner->l_owner;
  861. fl_pid = lockowner->l_pid;
  862. spin_lock(&state->state_lock);
  863. lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
  864. if (lsp && test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
  865. ret = -EIO;
  866. else if (lsp != NULL && test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) {
  867. nfs4_stateid_copy(dst, &lsp->ls_stateid);
  868. ret = 0;
  869. smp_rmb();
  870. if (!list_empty(&lsp->ls_seqid.list))
  871. ret = -EWOULDBLOCK;
  872. }
  873. spin_unlock(&state->state_lock);
  874. nfs4_put_lock_state(lsp);
  875. out:
  876. return ret;
  877. }
  878. static int nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
  879. {
  880. const nfs4_stateid *src;
  881. int ret;
  882. int seq;
  883. do {
  884. src = &zero_stateid;
  885. seq = read_seqbegin(&state->seqlock);
  886. if (test_bit(NFS_OPEN_STATE, &state->flags))
  887. src = &state->open_stateid;
  888. nfs4_stateid_copy(dst, src);
  889. ret = 0;
  890. smp_rmb();
  891. if (!list_empty(&state->owner->so_seqid.list))
  892. ret = -EWOULDBLOCK;
  893. } while (read_seqretry(&state->seqlock, seq));
  894. return ret;
  895. }
  896. /*
  897. * Byte-range lock aware utility to initialize the stateid of read/write
  898. * requests.
  899. */
  900. int nfs4_select_rw_stateid(nfs4_stateid *dst, struct nfs4_state *state,
  901. fmode_t fmode, const struct nfs_lockowner *lockowner)
  902. {
  903. int ret = nfs4_copy_lock_stateid(dst, state, lockowner);
  904. if (ret == -EIO)
  905. /* A lost lock - don't even consider delegations */
  906. goto out;
  907. /* returns true if delegation stateid found and copied */
  908. if (nfs4_copy_delegation_stateid(dst, state->inode, fmode)) {
  909. ret = 0;
  910. goto out;
  911. }
  912. if (ret != -ENOENT)
  913. /* nfs4_copy_delegation_stateid() didn't over-write
  914. * dst, so it still has the lock stateid which we now
  915. * choose to use.
  916. */
  917. goto out;
  918. ret = nfs4_copy_open_stateid(dst, state);
  919. out:
  920. if (nfs_server_capable(state->inode, NFS_CAP_STATEID_NFSV41))
  921. dst->seqid = 0;
  922. return ret;
  923. }
  924. struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
  925. {
  926. struct nfs_seqid *new;
  927. new = kmalloc(sizeof(*new), gfp_mask);
  928. if (new != NULL) {
  929. new->sequence = counter;
  930. INIT_LIST_HEAD(&new->list);
  931. new->task = NULL;
  932. }
  933. return new;
  934. }
  935. void nfs_release_seqid(struct nfs_seqid *seqid)
  936. {
  937. struct nfs_seqid_counter *sequence;
  938. if (list_empty(&seqid->list))
  939. return;
  940. sequence = seqid->sequence;
  941. spin_lock(&sequence->lock);
  942. list_del_init(&seqid->list);
  943. if (!list_empty(&sequence->list)) {
  944. struct nfs_seqid *next;
  945. next = list_first_entry(&sequence->list,
  946. struct nfs_seqid, list);
  947. rpc_wake_up_queued_task(&sequence->wait, next->task);
  948. }
  949. spin_unlock(&sequence->lock);
  950. }
  951. void nfs_free_seqid(struct nfs_seqid *seqid)
  952. {
  953. nfs_release_seqid(seqid);
  954. kfree(seqid);
  955. }
  956. /*
  957. * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
  958. * failed with a seqid incrementing error -
  959. * see comments nfs4.h:seqid_mutating_error()
  960. */
  961. static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
  962. {
  963. switch (status) {
  964. case 0:
  965. break;
  966. case -NFS4ERR_BAD_SEQID:
  967. if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
  968. return;
  969. pr_warn_ratelimited("NFS: v4 server returned a bad"
  970. " sequence-id error on an"
  971. " unconfirmed sequence %p!\n",
  972. seqid->sequence);
  973. case -NFS4ERR_STALE_CLIENTID:
  974. case -NFS4ERR_STALE_STATEID:
  975. case -NFS4ERR_BAD_STATEID:
  976. case -NFS4ERR_BADXDR:
  977. case -NFS4ERR_RESOURCE:
  978. case -NFS4ERR_NOFILEHANDLE:
  979. /* Non-seqid mutating errors */
  980. return;
  981. };
  982. /*
  983. * Note: no locking needed as we are guaranteed to be first
  984. * on the sequence list
  985. */
  986. seqid->sequence->counter++;
  987. }
  988. void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
  989. {
  990. struct nfs4_state_owner *sp = container_of(seqid->sequence,
  991. struct nfs4_state_owner, so_seqid);
  992. struct nfs_server *server = sp->so_server;
  993. if (status == -NFS4ERR_BAD_SEQID)
  994. nfs4_drop_state_owner(sp);
  995. if (!nfs4_has_session(server->nfs_client))
  996. nfs_increment_seqid(status, seqid);
  997. }
  998. /*
  999. * Increment the seqid if the LOCK/LOCKU succeeded, or
  1000. * failed with a seqid incrementing error -
  1001. * see comments nfs4.h:seqid_mutating_error()
  1002. */
  1003. void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
  1004. {
  1005. nfs_increment_seqid(status, seqid);
  1006. }
  1007. int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
  1008. {
  1009. struct nfs_seqid_counter *sequence = seqid->sequence;
  1010. int status = 0;
  1011. spin_lock(&sequence->lock);
  1012. seqid->task = task;
  1013. if (list_empty(&seqid->list))
  1014. list_add_tail(&seqid->list, &sequence->list);
  1015. if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
  1016. goto unlock;
  1017. rpc_sleep_on(&sequence->wait, task, NULL);
  1018. status = -EAGAIN;
  1019. unlock:
  1020. spin_unlock(&sequence->lock);
  1021. return status;
  1022. }
  1023. static int nfs4_run_state_manager(void *);
  1024. static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
  1025. {
  1026. smp_mb__before_clear_bit();
  1027. clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
  1028. smp_mb__after_clear_bit();
  1029. wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
  1030. rpc_wake_up(&clp->cl_rpcwaitq);
  1031. }
  1032. /*
  1033. * Schedule the nfs_client asynchronous state management routine
  1034. */
  1035. void nfs4_schedule_state_manager(struct nfs_client *clp)
  1036. {
  1037. struct task_struct *task;
  1038. char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
  1039. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  1040. return;
  1041. __module_get(THIS_MODULE);
  1042. atomic_inc(&clp->cl_count);
  1043. /* The rcu_read_lock() is not strictly necessary, as the state
  1044. * manager is the only thread that ever changes the rpc_xprt
  1045. * after it's initialized. At this point, we're single threaded. */
  1046. rcu_read_lock();
  1047. snprintf(buf, sizeof(buf), "%s-manager",
  1048. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  1049. rcu_read_unlock();
  1050. task = kthread_run(nfs4_run_state_manager, clp, "%s", buf);
  1051. if (IS_ERR(task)) {
  1052. printk(KERN_ERR "%s: kthread_run: %ld\n",
  1053. __func__, PTR_ERR(task));
  1054. nfs4_clear_state_manager_bit(clp);
  1055. nfs_put_client(clp);
  1056. module_put(THIS_MODULE);
  1057. }
  1058. }
  1059. /*
  1060. * Schedule a lease recovery attempt
  1061. */
  1062. void nfs4_schedule_lease_recovery(struct nfs_client *clp)
  1063. {
  1064. if (!clp)
  1065. return;
  1066. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1067. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1068. dprintk("%s: scheduling lease recovery for server %s\n", __func__,
  1069. clp->cl_hostname);
  1070. nfs4_schedule_state_manager(clp);
  1071. }
  1072. EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
  1073. /**
  1074. * nfs4_schedule_migration_recovery - trigger migration recovery
  1075. *
  1076. * @server: FSID that is migrating
  1077. *
  1078. * Returns zero if recovery has started, otherwise a negative NFS4ERR
  1079. * value is returned.
  1080. */
  1081. int nfs4_schedule_migration_recovery(const struct nfs_server *server)
  1082. {
  1083. struct nfs_client *clp = server->nfs_client;
  1084. if (server->fh_expire_type != NFS4_FH_PERSISTENT) {
  1085. pr_err("NFS: volatile file handles not supported (server %s)\n",
  1086. clp->cl_hostname);
  1087. return -NFS4ERR_IO;
  1088. }
  1089. if (test_bit(NFS_MIG_FAILED, &server->mig_status))
  1090. return -NFS4ERR_IO;
  1091. dprintk("%s: scheduling migration recovery for (%llx:%llx) on %s\n",
  1092. __func__,
  1093. (unsigned long long)server->fsid.major,
  1094. (unsigned long long)server->fsid.minor,
  1095. clp->cl_hostname);
  1096. set_bit(NFS_MIG_IN_TRANSITION,
  1097. &((struct nfs_server *)server)->mig_status);
  1098. set_bit(NFS4CLNT_MOVED, &clp->cl_state);
  1099. nfs4_schedule_state_manager(clp);
  1100. return 0;
  1101. }
  1102. EXPORT_SYMBOL_GPL(nfs4_schedule_migration_recovery);
  1103. /**
  1104. * nfs4_schedule_lease_moved_recovery - start lease-moved recovery
  1105. *
  1106. * @clp: server to check for moved leases
  1107. *
  1108. */
  1109. void nfs4_schedule_lease_moved_recovery(struct nfs_client *clp)
  1110. {
  1111. dprintk("%s: scheduling lease-moved recovery for client ID %llx on %s\n",
  1112. __func__, clp->cl_clientid, clp->cl_hostname);
  1113. set_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state);
  1114. nfs4_schedule_state_manager(clp);
  1115. }
  1116. EXPORT_SYMBOL_GPL(nfs4_schedule_lease_moved_recovery);
  1117. int nfs4_wait_clnt_recover(struct nfs_client *clp)
  1118. {
  1119. int res;
  1120. might_sleep();
  1121. atomic_inc(&clp->cl_count);
  1122. res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
  1123. nfs_wait_bit_killable, TASK_KILLABLE);
  1124. if (res)
  1125. goto out;
  1126. if (clp->cl_cons_state < 0)
  1127. res = clp->cl_cons_state;
  1128. out:
  1129. nfs_put_client(clp);
  1130. return res;
  1131. }
  1132. int nfs4_client_recover_expired_lease(struct nfs_client *clp)
  1133. {
  1134. unsigned int loop;
  1135. int ret;
  1136. for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
  1137. ret = nfs4_wait_clnt_recover(clp);
  1138. if (ret != 0)
  1139. break;
  1140. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
  1141. !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
  1142. break;
  1143. nfs4_schedule_state_manager(clp);
  1144. ret = -EIO;
  1145. }
  1146. return ret;
  1147. }
  1148. /*
  1149. * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
  1150. * @clp: client to process
  1151. *
  1152. * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
  1153. * resend of the SETCLIENTID and hence re-establish the
  1154. * callback channel. Then return all existing delegations.
  1155. */
  1156. static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
  1157. {
  1158. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1159. nfs_expire_all_delegations(clp);
  1160. dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
  1161. clp->cl_hostname);
  1162. }
  1163. void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
  1164. {
  1165. nfs40_handle_cb_pathdown(clp);
  1166. nfs4_schedule_state_manager(clp);
  1167. }
  1168. static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
  1169. {
  1170. set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1171. /* Don't recover state that expired before the reboot */
  1172. if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
  1173. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1174. return 0;
  1175. }
  1176. set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
  1177. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  1178. return 1;
  1179. }
  1180. static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
  1181. {
  1182. set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
  1183. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1184. set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
  1185. set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
  1186. return 1;
  1187. }
  1188. int nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
  1189. {
  1190. struct nfs_client *clp = server->nfs_client;
  1191. if (!nfs4_valid_open_stateid(state))
  1192. return -EBADF;
  1193. nfs4_state_mark_reclaim_nograce(clp, state);
  1194. dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
  1195. clp->cl_hostname);
  1196. nfs4_schedule_state_manager(clp);
  1197. return 0;
  1198. }
  1199. EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
  1200. void nfs_inode_find_state_and_recover(struct inode *inode,
  1201. const nfs4_stateid *stateid)
  1202. {
  1203. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  1204. struct nfs_inode *nfsi = NFS_I(inode);
  1205. struct nfs_open_context *ctx;
  1206. struct nfs4_state *state;
  1207. bool found = false;
  1208. spin_lock(&inode->i_lock);
  1209. list_for_each_entry(ctx, &nfsi->open_files, list) {
  1210. state = ctx->state;
  1211. if (state == NULL)
  1212. continue;
  1213. if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
  1214. continue;
  1215. if (!nfs4_stateid_match(&state->stateid, stateid))
  1216. continue;
  1217. nfs4_state_mark_reclaim_nograce(clp, state);
  1218. found = true;
  1219. }
  1220. spin_unlock(&inode->i_lock);
  1221. if (found)
  1222. nfs4_schedule_state_manager(clp);
  1223. }
  1224. static void nfs4_state_mark_open_context_bad(struct nfs4_state *state)
  1225. {
  1226. struct inode *inode = state->inode;
  1227. struct nfs_inode *nfsi = NFS_I(inode);
  1228. struct nfs_open_context *ctx;
  1229. spin_lock(&inode->i_lock);
  1230. list_for_each_entry(ctx, &nfsi->open_files, list) {
  1231. if (ctx->state != state)
  1232. continue;
  1233. set_bit(NFS_CONTEXT_BAD, &ctx->flags);
  1234. }
  1235. spin_unlock(&inode->i_lock);
  1236. }
  1237. static void nfs4_state_mark_recovery_failed(struct nfs4_state *state, int error)
  1238. {
  1239. set_bit(NFS_STATE_RECOVERY_FAILED, &state->flags);
  1240. nfs4_state_mark_open_context_bad(state);
  1241. }
  1242. static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
  1243. {
  1244. struct inode *inode = state->inode;
  1245. struct nfs_inode *nfsi = NFS_I(inode);
  1246. struct file_lock *fl;
  1247. int status = 0;
  1248. if (inode->i_flock == NULL)
  1249. return 0;
  1250. /* Guard against delegation returns and new lock/unlock calls */
  1251. down_write(&nfsi->rwsem);
  1252. /* Protect inode->i_flock using the BKL */
  1253. spin_lock(&inode->i_lock);
  1254. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  1255. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  1256. continue;
  1257. if (nfs_file_open_context(fl->fl_file)->state != state)
  1258. continue;
  1259. spin_unlock(&inode->i_lock);
  1260. status = ops->recover_lock(state, fl);
  1261. switch (status) {
  1262. case 0:
  1263. break;
  1264. case -ESTALE:
  1265. case -NFS4ERR_ADMIN_REVOKED:
  1266. case -NFS4ERR_STALE_STATEID:
  1267. case -NFS4ERR_BAD_STATEID:
  1268. case -NFS4ERR_EXPIRED:
  1269. case -NFS4ERR_NO_GRACE:
  1270. case -NFS4ERR_STALE_CLIENTID:
  1271. case -NFS4ERR_BADSESSION:
  1272. case -NFS4ERR_BADSLOT:
  1273. case -NFS4ERR_BAD_HIGH_SLOT:
  1274. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1275. goto out;
  1276. default:
  1277. printk(KERN_ERR "NFS: %s: unhandled error %d\n",
  1278. __func__, status);
  1279. case -ENOMEM:
  1280. case -NFS4ERR_DENIED:
  1281. case -NFS4ERR_RECLAIM_BAD:
  1282. case -NFS4ERR_RECLAIM_CONFLICT:
  1283. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  1284. status = 0;
  1285. }
  1286. spin_lock(&inode->i_lock);
  1287. }
  1288. spin_unlock(&inode->i_lock);
  1289. out:
  1290. up_write(&nfsi->rwsem);
  1291. return status;
  1292. }
  1293. static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
  1294. {
  1295. struct nfs4_state *state;
  1296. struct nfs4_lock_state *lock;
  1297. int status = 0;
  1298. /* Note: we rely on the sp->so_states list being ordered
  1299. * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
  1300. * states first.
  1301. * This is needed to ensure that the server won't give us any
  1302. * read delegations that we have to return if, say, we are
  1303. * recovering after a network partition or a reboot from a
  1304. * server that doesn't support a grace period.
  1305. */
  1306. spin_lock(&sp->so_lock);
  1307. write_seqcount_begin(&sp->so_reclaim_seqcount);
  1308. restart:
  1309. list_for_each_entry(state, &sp->so_states, open_states) {
  1310. if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
  1311. continue;
  1312. if (!nfs4_valid_open_stateid(state))
  1313. continue;
  1314. if (state->state == 0)
  1315. continue;
  1316. atomic_inc(&state->count);
  1317. spin_unlock(&sp->so_lock);
  1318. status = ops->recover_open(sp, state);
  1319. if (status >= 0) {
  1320. status = nfs4_reclaim_locks(state, ops);
  1321. if (status >= 0) {
  1322. if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  1323. spin_lock(&state->state_lock);
  1324. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  1325. if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags))
  1326. pr_warn_ratelimited("NFS: "
  1327. "%s: Lock reclaim "
  1328. "failed!\n", __func__);
  1329. }
  1330. spin_unlock(&state->state_lock);
  1331. }
  1332. nfs4_put_open_state(state);
  1333. spin_lock(&sp->so_lock);
  1334. goto restart;
  1335. }
  1336. }
  1337. switch (status) {
  1338. default:
  1339. printk(KERN_ERR "NFS: %s: unhandled error %d\n",
  1340. __func__, status);
  1341. case -ENOENT:
  1342. case -ENOMEM:
  1343. case -ESTALE:
  1344. /* Open state on this file cannot be recovered */
  1345. nfs4_state_mark_recovery_failed(state, status);
  1346. break;
  1347. case -EAGAIN:
  1348. ssleep(1);
  1349. case -NFS4ERR_ADMIN_REVOKED:
  1350. case -NFS4ERR_STALE_STATEID:
  1351. case -NFS4ERR_BAD_STATEID:
  1352. case -NFS4ERR_RECLAIM_BAD:
  1353. case -NFS4ERR_RECLAIM_CONFLICT:
  1354. nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
  1355. break;
  1356. case -NFS4ERR_EXPIRED:
  1357. case -NFS4ERR_NO_GRACE:
  1358. nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
  1359. case -NFS4ERR_STALE_CLIENTID:
  1360. case -NFS4ERR_BADSESSION:
  1361. case -NFS4ERR_BADSLOT:
  1362. case -NFS4ERR_BAD_HIGH_SLOT:
  1363. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1364. goto out_err;
  1365. }
  1366. nfs4_put_open_state(state);
  1367. spin_lock(&sp->so_lock);
  1368. goto restart;
  1369. }
  1370. write_seqcount_end(&sp->so_reclaim_seqcount);
  1371. spin_unlock(&sp->so_lock);
  1372. return 0;
  1373. out_err:
  1374. nfs4_put_open_state(state);
  1375. spin_lock(&sp->so_lock);
  1376. write_seqcount_end(&sp->so_reclaim_seqcount);
  1377. spin_unlock(&sp->so_lock);
  1378. return status;
  1379. }
  1380. static void nfs4_clear_open_state(struct nfs4_state *state)
  1381. {
  1382. struct nfs4_lock_state *lock;
  1383. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1384. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1385. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1386. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1387. spin_lock(&state->state_lock);
  1388. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  1389. lock->ls_seqid.flags = 0;
  1390. clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
  1391. }
  1392. spin_unlock(&state->state_lock);
  1393. }
  1394. static void nfs4_reset_seqids(struct nfs_server *server,
  1395. int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  1396. {
  1397. struct nfs_client *clp = server->nfs_client;
  1398. struct nfs4_state_owner *sp;
  1399. struct rb_node *pos;
  1400. struct nfs4_state *state;
  1401. spin_lock(&clp->cl_lock);
  1402. for (pos = rb_first(&server->state_owners);
  1403. pos != NULL;
  1404. pos = rb_next(pos)) {
  1405. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  1406. sp->so_seqid.flags = 0;
  1407. spin_lock(&sp->so_lock);
  1408. list_for_each_entry(state, &sp->so_states, open_states) {
  1409. if (mark_reclaim(clp, state))
  1410. nfs4_clear_open_state(state);
  1411. }
  1412. spin_unlock(&sp->so_lock);
  1413. }
  1414. spin_unlock(&clp->cl_lock);
  1415. }
  1416. static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
  1417. int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  1418. {
  1419. struct nfs_server *server;
  1420. rcu_read_lock();
  1421. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1422. nfs4_reset_seqids(server, mark_reclaim);
  1423. rcu_read_unlock();
  1424. }
  1425. static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
  1426. {
  1427. /* Mark all delegations for reclaim */
  1428. nfs_delegation_mark_reclaim(clp);
  1429. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
  1430. }
  1431. static void nfs4_reclaim_complete(struct nfs_client *clp,
  1432. const struct nfs4_state_recovery_ops *ops,
  1433. struct rpc_cred *cred)
  1434. {
  1435. /* Notify the server we're done reclaiming our state */
  1436. if (ops->reclaim_complete)
  1437. (void)ops->reclaim_complete(clp, cred);
  1438. }
  1439. static void nfs4_clear_reclaim_server(struct nfs_server *server)
  1440. {
  1441. struct nfs_client *clp = server->nfs_client;
  1442. struct nfs4_state_owner *sp;
  1443. struct rb_node *pos;
  1444. struct nfs4_state *state;
  1445. spin_lock(&clp->cl_lock);
  1446. for (pos = rb_first(&server->state_owners);
  1447. pos != NULL;
  1448. pos = rb_next(pos)) {
  1449. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  1450. spin_lock(&sp->so_lock);
  1451. list_for_each_entry(state, &sp->so_states, open_states) {
  1452. if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
  1453. &state->flags))
  1454. continue;
  1455. nfs4_state_mark_reclaim_nograce(clp, state);
  1456. }
  1457. spin_unlock(&sp->so_lock);
  1458. }
  1459. spin_unlock(&clp->cl_lock);
  1460. }
  1461. static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
  1462. {
  1463. struct nfs_server *server;
  1464. if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1465. return 0;
  1466. rcu_read_lock();
  1467. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1468. nfs4_clear_reclaim_server(server);
  1469. rcu_read_unlock();
  1470. nfs_delegation_reap_unclaimed(clp);
  1471. return 1;
  1472. }
  1473. static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
  1474. {
  1475. const struct nfs4_state_recovery_ops *ops;
  1476. struct rpc_cred *cred;
  1477. if (!nfs4_state_clear_reclaim_reboot(clp))
  1478. return;
  1479. ops = clp->cl_mvops->reboot_recovery_ops;
  1480. cred = nfs4_get_clid_cred(clp);
  1481. nfs4_reclaim_complete(clp, ops, cred);
  1482. put_rpccred(cred);
  1483. }
  1484. static void nfs_delegation_clear_all(struct nfs_client *clp)
  1485. {
  1486. nfs_delegation_mark_reclaim(clp);
  1487. nfs_delegation_reap_unclaimed(clp);
  1488. }
  1489. static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
  1490. {
  1491. nfs_delegation_clear_all(clp);
  1492. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
  1493. }
  1494. static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
  1495. {
  1496. switch (error) {
  1497. case 0:
  1498. break;
  1499. case -NFS4ERR_CB_PATH_DOWN:
  1500. nfs40_handle_cb_pathdown(clp);
  1501. break;
  1502. case -NFS4ERR_NO_GRACE:
  1503. nfs4_state_end_reclaim_reboot(clp);
  1504. break;
  1505. case -NFS4ERR_STALE_CLIENTID:
  1506. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1507. nfs4_state_clear_reclaim_reboot(clp);
  1508. nfs4_state_start_reclaim_reboot(clp);
  1509. break;
  1510. case -NFS4ERR_EXPIRED:
  1511. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1512. nfs4_state_start_reclaim_nograce(clp);
  1513. break;
  1514. case -NFS4ERR_BADSESSION:
  1515. case -NFS4ERR_BADSLOT:
  1516. case -NFS4ERR_BAD_HIGH_SLOT:
  1517. case -NFS4ERR_DEADSESSION:
  1518. case -NFS4ERR_SEQ_FALSE_RETRY:
  1519. case -NFS4ERR_SEQ_MISORDERED:
  1520. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1521. /* Zero session reset errors */
  1522. break;
  1523. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1524. set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  1525. break;
  1526. default:
  1527. dprintk("%s: failed to handle error %d for server %s\n",
  1528. __func__, error, clp->cl_hostname);
  1529. return error;
  1530. }
  1531. dprintk("%s: handled error %d for server %s\n", __func__, error,
  1532. clp->cl_hostname);
  1533. return 0;
  1534. }
  1535. static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
  1536. {
  1537. struct nfs4_state_owner *sp;
  1538. struct nfs_server *server;
  1539. struct rb_node *pos;
  1540. int status = 0;
  1541. restart:
  1542. rcu_read_lock();
  1543. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  1544. nfs4_purge_state_owners(server);
  1545. spin_lock(&clp->cl_lock);
  1546. for (pos = rb_first(&server->state_owners);
  1547. pos != NULL;
  1548. pos = rb_next(pos)) {
  1549. sp = rb_entry(pos,
  1550. struct nfs4_state_owner, so_server_node);
  1551. if (!test_and_clear_bit(ops->owner_flag_bit,
  1552. &sp->so_flags))
  1553. continue;
  1554. atomic_inc(&sp->so_count);
  1555. spin_unlock(&clp->cl_lock);
  1556. rcu_read_unlock();
  1557. status = nfs4_reclaim_open_state(sp, ops);
  1558. if (status < 0) {
  1559. set_bit(ops->owner_flag_bit, &sp->so_flags);
  1560. nfs4_put_state_owner(sp);
  1561. return nfs4_recovery_handle_error(clp, status);
  1562. }
  1563. nfs4_put_state_owner(sp);
  1564. goto restart;
  1565. }
  1566. spin_unlock(&clp->cl_lock);
  1567. }
  1568. rcu_read_unlock();
  1569. return status;
  1570. }
  1571. static int nfs4_check_lease(struct nfs_client *clp)
  1572. {
  1573. struct rpc_cred *cred;
  1574. const struct nfs4_state_maintenance_ops *ops =
  1575. clp->cl_mvops->state_renewal_ops;
  1576. int status;
  1577. /* Is the client already known to have an expired lease? */
  1578. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1579. return 0;
  1580. spin_lock(&clp->cl_lock);
  1581. cred = ops->get_state_renewal_cred_locked(clp);
  1582. spin_unlock(&clp->cl_lock);
  1583. if (cred == NULL) {
  1584. cred = nfs4_get_clid_cred(clp);
  1585. status = -ENOKEY;
  1586. if (cred == NULL)
  1587. goto out;
  1588. }
  1589. status = ops->renew_lease(clp, cred);
  1590. put_rpccred(cred);
  1591. if (status == -ETIMEDOUT) {
  1592. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1593. return 0;
  1594. }
  1595. out:
  1596. return nfs4_recovery_handle_error(clp, status);
  1597. }
  1598. /* Set NFS4CLNT_LEASE_EXPIRED and reclaim reboot state for all v4.0 errors
  1599. * and for recoverable errors on EXCHANGE_ID for v4.1
  1600. */
  1601. static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
  1602. {
  1603. switch (status) {
  1604. case -NFS4ERR_SEQ_MISORDERED:
  1605. if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
  1606. return -ESERVERFAULT;
  1607. /* Lease confirmation error: retry after purging the lease */
  1608. ssleep(1);
  1609. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1610. break;
  1611. case -NFS4ERR_STALE_CLIENTID:
  1612. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1613. nfs4_state_clear_reclaim_reboot(clp);
  1614. nfs4_state_start_reclaim_reboot(clp);
  1615. break;
  1616. case -NFS4ERR_CLID_INUSE:
  1617. pr_err("NFS: Server %s reports our clientid is in use\n",
  1618. clp->cl_hostname);
  1619. nfs_mark_client_ready(clp, -EPERM);
  1620. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1621. return -EPERM;
  1622. case -EACCES:
  1623. case -NFS4ERR_DELAY:
  1624. case -ETIMEDOUT:
  1625. case -EAGAIN:
  1626. ssleep(1);
  1627. break;
  1628. case -NFS4ERR_MINOR_VERS_MISMATCH:
  1629. if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
  1630. nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
  1631. dprintk("%s: exit with error %d for server %s\n",
  1632. __func__, -EPROTONOSUPPORT, clp->cl_hostname);
  1633. return -EPROTONOSUPPORT;
  1634. case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
  1635. * in nfs4_exchange_id */
  1636. default:
  1637. dprintk("%s: exit with error %d for server %s\n", __func__,
  1638. status, clp->cl_hostname);
  1639. return status;
  1640. }
  1641. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1642. dprintk("%s: handled error %d for server %s\n", __func__, status,
  1643. clp->cl_hostname);
  1644. return 0;
  1645. }
  1646. static int nfs4_establish_lease(struct nfs_client *clp)
  1647. {
  1648. struct rpc_cred *cred;
  1649. const struct nfs4_state_recovery_ops *ops =
  1650. clp->cl_mvops->reboot_recovery_ops;
  1651. int status;
  1652. cred = nfs4_get_clid_cred(clp);
  1653. if (cred == NULL)
  1654. return -ENOENT;
  1655. status = ops->establish_clid(clp, cred);
  1656. put_rpccred(cred);
  1657. if (status != 0)
  1658. return status;
  1659. pnfs_destroy_all_layouts(clp);
  1660. return 0;
  1661. }
  1662. /*
  1663. * Returns zero or a negative errno. NFS4ERR values are converted
  1664. * to local errno values.
  1665. */
  1666. static int nfs4_reclaim_lease(struct nfs_client *clp)
  1667. {
  1668. int status;
  1669. status = nfs4_establish_lease(clp);
  1670. if (status < 0)
  1671. return nfs4_handle_reclaim_lease_error(clp, status);
  1672. if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
  1673. nfs4_state_start_reclaim_nograce(clp);
  1674. if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
  1675. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  1676. clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1677. clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1678. return 0;
  1679. }
  1680. static int nfs4_purge_lease(struct nfs_client *clp)
  1681. {
  1682. int status;
  1683. status = nfs4_establish_lease(clp);
  1684. if (status < 0)
  1685. return nfs4_handle_reclaim_lease_error(clp, status);
  1686. clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
  1687. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1688. nfs4_state_start_reclaim_nograce(clp);
  1689. return 0;
  1690. }
  1691. /*
  1692. * Try remote migration of one FSID from a source server to a
  1693. * destination server. The source server provides a list of
  1694. * potential destinations.
  1695. *
  1696. * Returns zero or a negative NFS4ERR status code.
  1697. */
  1698. static int nfs4_try_migration(struct nfs_server *server, struct rpc_cred *cred)
  1699. {
  1700. struct nfs_client *clp = server->nfs_client;
  1701. struct nfs4_fs_locations *locations = NULL;
  1702. struct inode *inode;
  1703. struct page *page;
  1704. int status, result;
  1705. dprintk("--> %s: FSID %llx:%llx on \"%s\"\n", __func__,
  1706. (unsigned long long)server->fsid.major,
  1707. (unsigned long long)server->fsid.minor,
  1708. clp->cl_hostname);
  1709. result = 0;
  1710. page = alloc_page(GFP_KERNEL);
  1711. locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  1712. if (page == NULL || locations == NULL) {
  1713. dprintk("<-- %s: no memory\n", __func__);
  1714. goto out;
  1715. }
  1716. inode = server->super->s_root->d_inode;
  1717. result = nfs4_proc_get_locations(inode, locations, page, cred);
  1718. if (result) {
  1719. dprintk("<-- %s: failed to retrieve fs_locations: %d\n",
  1720. __func__, result);
  1721. goto out;
  1722. }
  1723. result = -NFS4ERR_NXIO;
  1724. if (!(locations->fattr.valid & NFS_ATTR_FATTR_V4_LOCATIONS)) {
  1725. dprintk("<-- %s: No fs_locations data, migration skipped\n",
  1726. __func__);
  1727. goto out;
  1728. }
  1729. nfs4_begin_drain_session(clp);
  1730. status = nfs4_replace_transport(server, locations);
  1731. if (status != 0) {
  1732. dprintk("<-- %s: failed to replace transport: %d\n",
  1733. __func__, status);
  1734. goto out;
  1735. }
  1736. result = 0;
  1737. dprintk("<-- %s: migration succeeded\n", __func__);
  1738. out:
  1739. if (page != NULL)
  1740. __free_page(page);
  1741. kfree(locations);
  1742. if (result) {
  1743. pr_err("NFS: migration recovery failed (server %s)\n",
  1744. clp->cl_hostname);
  1745. set_bit(NFS_MIG_FAILED, &server->mig_status);
  1746. }
  1747. return result;
  1748. }
  1749. /*
  1750. * Returns zero or a negative NFS4ERR status code.
  1751. */
  1752. static int nfs4_handle_migration(struct nfs_client *clp)
  1753. {
  1754. const struct nfs4_state_maintenance_ops *ops =
  1755. clp->cl_mvops->state_renewal_ops;
  1756. struct nfs_server *server;
  1757. struct rpc_cred *cred;
  1758. dprintk("%s: migration reported on \"%s\"\n", __func__,
  1759. clp->cl_hostname);
  1760. spin_lock(&clp->cl_lock);
  1761. cred = ops->get_state_renewal_cred_locked(clp);
  1762. spin_unlock(&clp->cl_lock);
  1763. if (cred == NULL)
  1764. return -NFS4ERR_NOENT;
  1765. clp->cl_mig_gen++;
  1766. restart:
  1767. rcu_read_lock();
  1768. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  1769. int status;
  1770. if (server->mig_gen == clp->cl_mig_gen)
  1771. continue;
  1772. server->mig_gen = clp->cl_mig_gen;
  1773. if (!test_and_clear_bit(NFS_MIG_IN_TRANSITION,
  1774. &server->mig_status))
  1775. continue;
  1776. rcu_read_unlock();
  1777. status = nfs4_try_migration(server, cred);
  1778. if (status < 0) {
  1779. put_rpccred(cred);
  1780. return status;
  1781. }
  1782. goto restart;
  1783. }
  1784. rcu_read_unlock();
  1785. put_rpccred(cred);
  1786. return 0;
  1787. }
  1788. /*
  1789. * Test each nfs_server on the clp's cl_superblocks list to see
  1790. * if it's moved to another server. Stop when the server no longer
  1791. * returns NFS4ERR_LEASE_MOVED.
  1792. */
  1793. static int nfs4_handle_lease_moved(struct nfs_client *clp)
  1794. {
  1795. const struct nfs4_state_maintenance_ops *ops =
  1796. clp->cl_mvops->state_renewal_ops;
  1797. struct nfs_server *server;
  1798. struct rpc_cred *cred;
  1799. dprintk("%s: lease moved reported on \"%s\"\n", __func__,
  1800. clp->cl_hostname);
  1801. spin_lock(&clp->cl_lock);
  1802. cred = ops->get_state_renewal_cred_locked(clp);
  1803. spin_unlock(&clp->cl_lock);
  1804. if (cred == NULL)
  1805. return -NFS4ERR_NOENT;
  1806. clp->cl_mig_gen++;
  1807. restart:
  1808. rcu_read_lock();
  1809. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  1810. struct inode *inode;
  1811. int status;
  1812. if (server->mig_gen == clp->cl_mig_gen)
  1813. continue;
  1814. server->mig_gen = clp->cl_mig_gen;
  1815. rcu_read_unlock();
  1816. inode = server->super->s_root->d_inode;
  1817. status = nfs4_proc_fsid_present(inode, cred);
  1818. if (status != -NFS4ERR_MOVED)
  1819. goto restart; /* wasn't this one */
  1820. if (nfs4_try_migration(server, cred) == -NFS4ERR_LEASE_MOVED)
  1821. goto restart; /* there are more */
  1822. goto out;
  1823. }
  1824. rcu_read_unlock();
  1825. out:
  1826. put_rpccred(cred);
  1827. return 0;
  1828. }
  1829. /**
  1830. * nfs4_discover_server_trunking - Detect server IP address trunking
  1831. *
  1832. * @clp: nfs_client under test
  1833. * @result: OUT: found nfs_client, or clp
  1834. *
  1835. * Returns zero or a negative errno. If zero is returned,
  1836. * an nfs_client pointer is planted in "result".
  1837. *
  1838. * Note: since we are invoked in process context, and
  1839. * not from inside the state manager, we cannot use
  1840. * nfs4_handle_reclaim_lease_error().
  1841. */
  1842. int nfs4_discover_server_trunking(struct nfs_client *clp,
  1843. struct nfs_client **result)
  1844. {
  1845. const struct nfs4_state_recovery_ops *ops =
  1846. clp->cl_mvops->reboot_recovery_ops;
  1847. struct rpc_clnt *clnt;
  1848. struct rpc_cred *cred;
  1849. int i, status;
  1850. dprintk("NFS: %s: testing '%s'\n", __func__, clp->cl_hostname);
  1851. clnt = clp->cl_rpcclient;
  1852. i = 0;
  1853. mutex_lock(&nfs_clid_init_mutex);
  1854. again:
  1855. status = -ENOENT;
  1856. cred = nfs4_get_clid_cred(clp);
  1857. if (cred == NULL)
  1858. goto out_unlock;
  1859. status = ops->detect_trunking(clp, result, cred);
  1860. put_rpccred(cred);
  1861. switch (status) {
  1862. case 0:
  1863. break;
  1864. case -NFS4ERR_DELAY:
  1865. case -ETIMEDOUT:
  1866. case -EAGAIN:
  1867. ssleep(1);
  1868. case -NFS4ERR_STALE_CLIENTID:
  1869. dprintk("NFS: %s after status %d, retrying\n",
  1870. __func__, status);
  1871. goto again;
  1872. case -EACCES:
  1873. if (i++ == 0) {
  1874. nfs4_root_machine_cred(clp);
  1875. goto again;
  1876. }
  1877. if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX)
  1878. break;
  1879. case -NFS4ERR_CLID_INUSE:
  1880. case -NFS4ERR_WRONGSEC:
  1881. /* No point in retrying if we already used RPC_AUTH_UNIX */
  1882. if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX) {
  1883. status = -EPERM;
  1884. break;
  1885. }
  1886. clnt = rpc_clone_client_set_auth(clnt, RPC_AUTH_UNIX);
  1887. if (IS_ERR(clnt)) {
  1888. status = PTR_ERR(clnt);
  1889. break;
  1890. }
  1891. /* Note: this is safe because we haven't yet marked the
  1892. * client as ready, so we are the only user of
  1893. * clp->cl_rpcclient
  1894. */
  1895. clnt = xchg(&clp->cl_rpcclient, clnt);
  1896. rpc_shutdown_client(clnt);
  1897. clnt = clp->cl_rpcclient;
  1898. goto again;
  1899. case -NFS4ERR_MINOR_VERS_MISMATCH:
  1900. status = -EPROTONOSUPPORT;
  1901. break;
  1902. case -EKEYEXPIRED:
  1903. case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
  1904. * in nfs4_exchange_id */
  1905. status = -EKEYEXPIRED;
  1906. break;
  1907. default:
  1908. pr_warn("NFS: %s unhandled error %d. Exiting with error EIO\n",
  1909. __func__, status);
  1910. status = -EIO;
  1911. }
  1912. out_unlock:
  1913. mutex_unlock(&nfs_clid_init_mutex);
  1914. dprintk("NFS: %s: status = %d\n", __func__, status);
  1915. return status;
  1916. }
  1917. #ifdef CONFIG_NFS_V4_1
  1918. void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
  1919. {
  1920. struct nfs_client *clp = session->clp;
  1921. switch (err) {
  1922. default:
  1923. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1924. break;
  1925. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1926. set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  1927. }
  1928. nfs4_schedule_lease_recovery(clp);
  1929. }
  1930. EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
  1931. static void nfs41_ping_server(struct nfs_client *clp)
  1932. {
  1933. /* Use CHECK_LEASE to ping the server with a SEQUENCE */
  1934. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1935. nfs4_schedule_state_manager(clp);
  1936. }
  1937. void nfs41_server_notify_target_slotid_update(struct nfs_client *clp)
  1938. {
  1939. nfs41_ping_server(clp);
  1940. }
  1941. void nfs41_server_notify_highest_slotid_update(struct nfs_client *clp)
  1942. {
  1943. nfs41_ping_server(clp);
  1944. }
  1945. static void nfs4_reset_all_state(struct nfs_client *clp)
  1946. {
  1947. if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
  1948. set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
  1949. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1950. nfs4_state_start_reclaim_nograce(clp);
  1951. dprintk("%s: scheduling reset of all state for server %s!\n",
  1952. __func__, clp->cl_hostname);
  1953. nfs4_schedule_state_manager(clp);
  1954. }
  1955. }
  1956. static void nfs41_handle_server_reboot(struct nfs_client *clp)
  1957. {
  1958. if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
  1959. nfs4_state_start_reclaim_reboot(clp);
  1960. dprintk("%s: server %s rebooted!\n", __func__,
  1961. clp->cl_hostname);
  1962. nfs4_schedule_state_manager(clp);
  1963. }
  1964. }
  1965. static void nfs41_handle_state_revoked(struct nfs_client *clp)
  1966. {
  1967. nfs4_reset_all_state(clp);
  1968. dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
  1969. }
  1970. static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
  1971. {
  1972. /* This will need to handle layouts too */
  1973. nfs_expire_all_delegations(clp);
  1974. dprintk("%s: Recallable state revoked on server %s!\n", __func__,
  1975. clp->cl_hostname);
  1976. }
  1977. static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
  1978. {
  1979. nfs_expire_all_delegations(clp);
  1980. if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
  1981. nfs4_schedule_state_manager(clp);
  1982. dprintk("%s: server %s declared a backchannel fault\n", __func__,
  1983. clp->cl_hostname);
  1984. }
  1985. static void nfs41_handle_cb_path_down(struct nfs_client *clp)
  1986. {
  1987. if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
  1988. &clp->cl_state) == 0)
  1989. nfs4_schedule_state_manager(clp);
  1990. }
  1991. void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
  1992. {
  1993. if (!flags)
  1994. return;
  1995. dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
  1996. __func__, clp->cl_hostname, clp->cl_clientid, flags);
  1997. if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
  1998. nfs41_handle_server_reboot(clp);
  1999. if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
  2000. SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
  2001. SEQ4_STATUS_ADMIN_STATE_REVOKED))
  2002. nfs41_handle_state_revoked(clp);
  2003. if (flags & SEQ4_STATUS_LEASE_MOVED)
  2004. nfs4_schedule_lease_moved_recovery(clp);
  2005. if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
  2006. nfs41_handle_recallable_state_revoked(clp);
  2007. if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
  2008. nfs41_handle_backchannel_fault(clp);
  2009. else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
  2010. SEQ4_STATUS_CB_PATH_DOWN_SESSION))
  2011. nfs41_handle_cb_path_down(clp);
  2012. }
  2013. static int nfs4_reset_session(struct nfs_client *clp)
  2014. {
  2015. struct rpc_cred *cred;
  2016. int status;
  2017. if (!nfs4_has_session(clp))
  2018. return 0;
  2019. nfs4_begin_drain_session(clp);
  2020. cred = nfs4_get_clid_cred(clp);
  2021. status = nfs4_proc_destroy_session(clp->cl_session, cred);
  2022. switch (status) {
  2023. case 0:
  2024. case -NFS4ERR_BADSESSION:
  2025. case -NFS4ERR_DEADSESSION:
  2026. break;
  2027. case -NFS4ERR_BACK_CHAN_BUSY:
  2028. case -NFS4ERR_DELAY:
  2029. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  2030. status = 0;
  2031. ssleep(1);
  2032. goto out;
  2033. default:
  2034. status = nfs4_recovery_handle_error(clp, status);
  2035. goto out;
  2036. }
  2037. memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
  2038. status = nfs4_proc_create_session(clp, cred);
  2039. if (status) {
  2040. dprintk("%s: session reset failed with status %d for server %s!\n",
  2041. __func__, status, clp->cl_hostname);
  2042. status = nfs4_handle_reclaim_lease_error(clp, status);
  2043. goto out;
  2044. }
  2045. nfs41_finish_session_reset(clp);
  2046. dprintk("%s: session reset was successful for server %s!\n",
  2047. __func__, clp->cl_hostname);
  2048. out:
  2049. if (cred)
  2050. put_rpccred(cred);
  2051. return status;
  2052. }
  2053. static int nfs4_bind_conn_to_session(struct nfs_client *clp)
  2054. {
  2055. struct rpc_cred *cred;
  2056. int ret;
  2057. if (!nfs4_has_session(clp))
  2058. return 0;
  2059. nfs4_begin_drain_session(clp);
  2060. cred = nfs4_get_clid_cred(clp);
  2061. ret = nfs4_proc_bind_conn_to_session(clp, cred);
  2062. if (cred)
  2063. put_rpccred(cred);
  2064. clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  2065. switch (ret) {
  2066. case 0:
  2067. dprintk("%s: bind_conn_to_session was successful for server %s!\n",
  2068. __func__, clp->cl_hostname);
  2069. break;
  2070. case -NFS4ERR_DELAY:
  2071. ssleep(1);
  2072. set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  2073. break;
  2074. default:
  2075. return nfs4_recovery_handle_error(clp, ret);
  2076. }
  2077. return 0;
  2078. }
  2079. #else /* CONFIG_NFS_V4_1 */
  2080. static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
  2081. static int nfs4_bind_conn_to_session(struct nfs_client *clp)
  2082. {
  2083. return 0;
  2084. }
  2085. #endif /* CONFIG_NFS_V4_1 */
  2086. static void nfs4_state_manager(struct nfs_client *clp)
  2087. {
  2088. int status = 0;
  2089. const char *section = "", *section_sep = "";
  2090. /* Ensure exclusive access to NFSv4 state */
  2091. do {
  2092. if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
  2093. section = "purge state";
  2094. status = nfs4_purge_lease(clp);
  2095. if (status < 0)
  2096. goto out_error;
  2097. continue;
  2098. }
  2099. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
  2100. section = "lease expired";
  2101. /* We're going to have to re-establish a clientid */
  2102. status = nfs4_reclaim_lease(clp);
  2103. if (status < 0)
  2104. goto out_error;
  2105. continue;
  2106. }
  2107. /* Initialize or reset the session */
  2108. if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
  2109. section = "reset session";
  2110. status = nfs4_reset_session(clp);
  2111. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  2112. continue;
  2113. if (status < 0)
  2114. goto out_error;
  2115. }
  2116. /* Send BIND_CONN_TO_SESSION */
  2117. if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
  2118. &clp->cl_state)) {
  2119. section = "bind conn to session";
  2120. status = nfs4_bind_conn_to_session(clp);
  2121. if (status < 0)
  2122. goto out_error;
  2123. continue;
  2124. }
  2125. if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
  2126. section = "check lease";
  2127. status = nfs4_check_lease(clp);
  2128. if (status < 0)
  2129. goto out_error;
  2130. }
  2131. if (test_and_clear_bit(NFS4CLNT_MOVED, &clp->cl_state)) {
  2132. section = "migration";
  2133. status = nfs4_handle_migration(clp);
  2134. if (status < 0)
  2135. goto out_error;
  2136. }
  2137. if (test_and_clear_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state)) {
  2138. section = "lease moved";
  2139. status = nfs4_handle_lease_moved(clp);
  2140. if (status < 0)
  2141. goto out_error;
  2142. }
  2143. /* First recover reboot state... */
  2144. if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
  2145. section = "reclaim reboot";
  2146. status = nfs4_do_reclaim(clp,
  2147. clp->cl_mvops->reboot_recovery_ops);
  2148. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
  2149. test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
  2150. continue;
  2151. nfs4_state_end_reclaim_reboot(clp);
  2152. if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
  2153. continue;
  2154. if (status < 0)
  2155. goto out_error;
  2156. }
  2157. /* Now recover expired state... */
  2158. if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
  2159. section = "reclaim nograce";
  2160. status = nfs4_do_reclaim(clp,
  2161. clp->cl_mvops->nograce_recovery_ops);
  2162. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
  2163. test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
  2164. test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  2165. continue;
  2166. if (status < 0)
  2167. goto out_error;
  2168. }
  2169. nfs4_end_drain_session(clp);
  2170. if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
  2171. nfs_client_return_marked_delegations(clp);
  2172. continue;
  2173. }
  2174. nfs4_clear_state_manager_bit(clp);
  2175. /* Did we race with an attempt to give us more work? */
  2176. if (clp->cl_state == 0)
  2177. break;
  2178. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  2179. break;
  2180. } while (atomic_read(&clp->cl_count) > 1);
  2181. return;
  2182. out_error:
  2183. if (strlen(section))
  2184. section_sep = ": ";
  2185. pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
  2186. " with error %d\n", section_sep, section,
  2187. clp->cl_hostname, -status);
  2188. ssleep(1);
  2189. nfs4_end_drain_session(clp);
  2190. nfs4_clear_state_manager_bit(clp);
  2191. }
  2192. static int nfs4_run_state_manager(void *ptr)
  2193. {
  2194. struct nfs_client *clp = ptr;
  2195. allow_signal(SIGKILL);
  2196. nfs4_state_manager(clp);
  2197. nfs_put_client(clp);
  2198. module_put_and_exit(0);
  2199. return 0;
  2200. }
  2201. /*
  2202. * Local variables:
  2203. * c-basic-offset: 8
  2204. * End:
  2205. */