nfs4state.c 52 KB

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