nfs4state.c 64 KB

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