nfs4state.c 66 KB

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