nfs4state.c 54 KB

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