nfs4state.c 66 KB

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