nfs4state.c 73 KB

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