nfs4state.c 64 KB

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