nfs4state.c 48 KB

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