nfs4state.c 71 KB

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