nfs4state.c 67 KB

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