nfs4state.c 65 KB

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