nfs4state.c 58 KB

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