nfs4state.c 65 KB

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