nfs4state.c 69 KB

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