nfs4state.c 69 KB

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