nfs4state.c 67 KB

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