nfs4state.c 64 KB

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