nfs4state.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  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 != -1) {
  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_copy_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. memcpy(dst, &state->stateid, sizeof(*dst));
  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. memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
  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. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  928. return;
  929. __module_get(THIS_MODULE);
  930. atomic_inc(&clp->cl_count);
  931. task = kthread_run(nfs4_run_state_manager, clp, "%s-manager",
  932. rpc_peeraddr2str(clp->cl_rpcclient,
  933. RPC_DISPLAY_ADDR));
  934. if (!IS_ERR(task))
  935. return;
  936. nfs4_clear_state_manager_bit(clp);
  937. nfs_put_client(clp);
  938. module_put(THIS_MODULE);
  939. }
  940. /*
  941. * Schedule a lease recovery attempt
  942. */
  943. void nfs4_schedule_lease_recovery(struct nfs_client *clp)
  944. {
  945. if (!clp)
  946. return;
  947. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  948. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  949. nfs4_schedule_state_manager(clp);
  950. }
  951. void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
  952. {
  953. nfs_handle_cb_pathdown(clp);
  954. nfs4_schedule_state_manager(clp);
  955. }
  956. static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
  957. {
  958. set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  959. /* Don't recover state that expired before the reboot */
  960. if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
  961. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  962. return 0;
  963. }
  964. set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
  965. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  966. return 1;
  967. }
  968. static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
  969. {
  970. set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
  971. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  972. set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
  973. set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
  974. return 1;
  975. }
  976. void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
  977. {
  978. struct nfs_client *clp = server->nfs_client;
  979. nfs4_state_mark_reclaim_nograce(clp, state);
  980. nfs4_schedule_state_manager(clp);
  981. }
  982. static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
  983. {
  984. struct inode *inode = state->inode;
  985. struct nfs_inode *nfsi = NFS_I(inode);
  986. struct file_lock *fl;
  987. int status = 0;
  988. if (inode->i_flock == NULL)
  989. return 0;
  990. /* Guard against delegation returns and new lock/unlock calls */
  991. down_write(&nfsi->rwsem);
  992. /* Protect inode->i_flock using the BKL */
  993. lock_flocks();
  994. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  995. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  996. continue;
  997. if (nfs_file_open_context(fl->fl_file)->state != state)
  998. continue;
  999. unlock_flocks();
  1000. status = ops->recover_lock(state, fl);
  1001. switch (status) {
  1002. case 0:
  1003. break;
  1004. case -ESTALE:
  1005. case -NFS4ERR_ADMIN_REVOKED:
  1006. case -NFS4ERR_STALE_STATEID:
  1007. case -NFS4ERR_BAD_STATEID:
  1008. case -NFS4ERR_EXPIRED:
  1009. case -NFS4ERR_NO_GRACE:
  1010. case -NFS4ERR_STALE_CLIENTID:
  1011. case -NFS4ERR_BADSESSION:
  1012. case -NFS4ERR_BADSLOT:
  1013. case -NFS4ERR_BAD_HIGH_SLOT:
  1014. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1015. goto out;
  1016. default:
  1017. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  1018. __func__, status);
  1019. case -ENOMEM:
  1020. case -NFS4ERR_DENIED:
  1021. case -NFS4ERR_RECLAIM_BAD:
  1022. case -NFS4ERR_RECLAIM_CONFLICT:
  1023. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  1024. status = 0;
  1025. }
  1026. lock_flocks();
  1027. }
  1028. unlock_flocks();
  1029. out:
  1030. up_write(&nfsi->rwsem);
  1031. return status;
  1032. }
  1033. static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
  1034. {
  1035. struct nfs4_state *state;
  1036. struct nfs4_lock_state *lock;
  1037. int status = 0;
  1038. /* Note: we rely on the sp->so_states list being ordered
  1039. * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
  1040. * states first.
  1041. * This is needed to ensure that the server won't give us any
  1042. * read delegations that we have to return if, say, we are
  1043. * recovering after a network partition or a reboot from a
  1044. * server that doesn't support a grace period.
  1045. */
  1046. restart:
  1047. spin_lock(&sp->so_lock);
  1048. list_for_each_entry(state, &sp->so_states, open_states) {
  1049. if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
  1050. continue;
  1051. if (state->state == 0)
  1052. continue;
  1053. atomic_inc(&state->count);
  1054. spin_unlock(&sp->so_lock);
  1055. status = ops->recover_open(sp, state);
  1056. if (status >= 0) {
  1057. status = nfs4_reclaim_locks(state, ops);
  1058. if (status >= 0) {
  1059. spin_lock(&state->state_lock);
  1060. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  1061. if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
  1062. printk("%s: Lock reclaim failed!\n",
  1063. __func__);
  1064. }
  1065. spin_unlock(&state->state_lock);
  1066. nfs4_put_open_state(state);
  1067. goto restart;
  1068. }
  1069. }
  1070. switch (status) {
  1071. default:
  1072. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  1073. __func__, status);
  1074. case -ENOENT:
  1075. case -ENOMEM:
  1076. case -ESTALE:
  1077. /*
  1078. * Open state on this file cannot be recovered
  1079. * All we can do is revert to using the zero stateid.
  1080. */
  1081. memset(state->stateid.data, 0,
  1082. sizeof(state->stateid.data));
  1083. /* Mark the file as being 'closed' */
  1084. state->state = 0;
  1085. break;
  1086. case -EKEYEXPIRED:
  1087. /*
  1088. * User RPCSEC_GSS context has expired.
  1089. * We cannot recover this stateid now, so
  1090. * skip it and allow recovery thread to
  1091. * proceed.
  1092. */
  1093. break;
  1094. case -NFS4ERR_ADMIN_REVOKED:
  1095. case -NFS4ERR_STALE_STATEID:
  1096. case -NFS4ERR_BAD_STATEID:
  1097. case -NFS4ERR_RECLAIM_BAD:
  1098. case -NFS4ERR_RECLAIM_CONFLICT:
  1099. nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
  1100. break;
  1101. case -NFS4ERR_EXPIRED:
  1102. case -NFS4ERR_NO_GRACE:
  1103. nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
  1104. case -NFS4ERR_STALE_CLIENTID:
  1105. case -NFS4ERR_BADSESSION:
  1106. case -NFS4ERR_BADSLOT:
  1107. case -NFS4ERR_BAD_HIGH_SLOT:
  1108. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1109. goto out_err;
  1110. }
  1111. nfs4_put_open_state(state);
  1112. goto restart;
  1113. }
  1114. spin_unlock(&sp->so_lock);
  1115. return 0;
  1116. out_err:
  1117. nfs4_put_open_state(state);
  1118. return status;
  1119. }
  1120. static void nfs4_clear_open_state(struct nfs4_state *state)
  1121. {
  1122. struct nfs4_lock_state *lock;
  1123. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1124. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1125. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1126. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1127. spin_lock(&state->state_lock);
  1128. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  1129. lock->ls_seqid.flags = 0;
  1130. lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
  1131. }
  1132. spin_unlock(&state->state_lock);
  1133. }
  1134. static void nfs4_reset_seqids(struct nfs_server *server,
  1135. int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  1136. {
  1137. struct nfs_client *clp = server->nfs_client;
  1138. struct nfs4_state_owner *sp;
  1139. struct rb_node *pos;
  1140. struct nfs4_state *state;
  1141. spin_lock(&clp->cl_lock);
  1142. for (pos = rb_first(&server->state_owners);
  1143. pos != NULL;
  1144. pos = rb_next(pos)) {
  1145. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  1146. sp->so_seqid.flags = 0;
  1147. spin_lock(&sp->so_lock);
  1148. list_for_each_entry(state, &sp->so_states, open_states) {
  1149. if (mark_reclaim(clp, state))
  1150. nfs4_clear_open_state(state);
  1151. }
  1152. spin_unlock(&sp->so_lock);
  1153. }
  1154. spin_unlock(&clp->cl_lock);
  1155. }
  1156. static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
  1157. int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  1158. {
  1159. struct nfs_server *server;
  1160. rcu_read_lock();
  1161. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1162. nfs4_reset_seqids(server, mark_reclaim);
  1163. rcu_read_unlock();
  1164. }
  1165. static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
  1166. {
  1167. /* Mark all delegations for reclaim */
  1168. nfs_delegation_mark_reclaim(clp);
  1169. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
  1170. }
  1171. static void nfs4_reclaim_complete(struct nfs_client *clp,
  1172. const struct nfs4_state_recovery_ops *ops)
  1173. {
  1174. /* Notify the server we're done reclaiming our state */
  1175. if (ops->reclaim_complete)
  1176. (void)ops->reclaim_complete(clp);
  1177. }
  1178. static void nfs4_clear_reclaim_server(struct nfs_server *server)
  1179. {
  1180. struct nfs_client *clp = server->nfs_client;
  1181. struct nfs4_state_owner *sp;
  1182. struct rb_node *pos;
  1183. struct nfs4_state *state;
  1184. spin_lock(&clp->cl_lock);
  1185. for (pos = rb_first(&server->state_owners);
  1186. pos != NULL;
  1187. pos = rb_next(pos)) {
  1188. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  1189. spin_lock(&sp->so_lock);
  1190. list_for_each_entry(state, &sp->so_states, open_states) {
  1191. if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
  1192. &state->flags))
  1193. continue;
  1194. nfs4_state_mark_reclaim_nograce(clp, state);
  1195. }
  1196. spin_unlock(&sp->so_lock);
  1197. }
  1198. spin_unlock(&clp->cl_lock);
  1199. }
  1200. static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
  1201. {
  1202. struct nfs_server *server;
  1203. if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1204. return 0;
  1205. rcu_read_lock();
  1206. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1207. nfs4_clear_reclaim_server(server);
  1208. rcu_read_unlock();
  1209. nfs_delegation_reap_unclaimed(clp);
  1210. return 1;
  1211. }
  1212. static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
  1213. {
  1214. if (!nfs4_state_clear_reclaim_reboot(clp))
  1215. return;
  1216. nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
  1217. }
  1218. static void nfs_delegation_clear_all(struct nfs_client *clp)
  1219. {
  1220. nfs_delegation_mark_reclaim(clp);
  1221. nfs_delegation_reap_unclaimed(clp);
  1222. }
  1223. static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
  1224. {
  1225. nfs_delegation_clear_all(clp);
  1226. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
  1227. }
  1228. static void nfs4_warn_keyexpired(const char *s)
  1229. {
  1230. printk_ratelimited(KERN_WARNING "Error: state manager"
  1231. " encountered RPCSEC_GSS session"
  1232. " expired against NFSv4 server %s.\n",
  1233. s);
  1234. }
  1235. static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
  1236. {
  1237. switch (error) {
  1238. case 0:
  1239. break;
  1240. case -NFS4ERR_CB_PATH_DOWN:
  1241. nfs_handle_cb_pathdown(clp);
  1242. break;
  1243. case -NFS4ERR_NO_GRACE:
  1244. nfs4_state_end_reclaim_reboot(clp);
  1245. break;
  1246. case -NFS4ERR_STALE_CLIENTID:
  1247. case -NFS4ERR_LEASE_MOVED:
  1248. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1249. nfs4_state_clear_reclaim_reboot(clp);
  1250. nfs4_state_start_reclaim_reboot(clp);
  1251. break;
  1252. case -NFS4ERR_EXPIRED:
  1253. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1254. nfs4_state_start_reclaim_nograce(clp);
  1255. break;
  1256. case -NFS4ERR_BADSESSION:
  1257. case -NFS4ERR_BADSLOT:
  1258. case -NFS4ERR_BAD_HIGH_SLOT:
  1259. case -NFS4ERR_DEADSESSION:
  1260. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1261. case -NFS4ERR_SEQ_FALSE_RETRY:
  1262. case -NFS4ERR_SEQ_MISORDERED:
  1263. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1264. /* Zero session reset errors */
  1265. break;
  1266. case -EKEYEXPIRED:
  1267. /* Nothing we can do */
  1268. nfs4_warn_keyexpired(clp->cl_hostname);
  1269. break;
  1270. default:
  1271. return error;
  1272. }
  1273. return 0;
  1274. }
  1275. static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
  1276. {
  1277. struct nfs4_state_owner *sp;
  1278. struct nfs_server *server;
  1279. struct rb_node *pos;
  1280. int status = 0;
  1281. restart:
  1282. rcu_read_lock();
  1283. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  1284. nfs4_purge_state_owners(server);
  1285. spin_lock(&clp->cl_lock);
  1286. for (pos = rb_first(&server->state_owners);
  1287. pos != NULL;
  1288. pos = rb_next(pos)) {
  1289. sp = rb_entry(pos,
  1290. struct nfs4_state_owner, so_server_node);
  1291. if (!test_and_clear_bit(ops->owner_flag_bit,
  1292. &sp->so_flags))
  1293. continue;
  1294. atomic_inc(&sp->so_count);
  1295. spin_unlock(&clp->cl_lock);
  1296. rcu_read_unlock();
  1297. status = nfs4_reclaim_open_state(sp, ops);
  1298. if (status < 0) {
  1299. set_bit(ops->owner_flag_bit, &sp->so_flags);
  1300. nfs4_put_state_owner(sp);
  1301. return nfs4_recovery_handle_error(clp, status);
  1302. }
  1303. nfs4_put_state_owner(sp);
  1304. goto restart;
  1305. }
  1306. spin_unlock(&clp->cl_lock);
  1307. }
  1308. rcu_read_unlock();
  1309. return status;
  1310. }
  1311. static int nfs4_check_lease(struct nfs_client *clp)
  1312. {
  1313. struct rpc_cred *cred;
  1314. const struct nfs4_state_maintenance_ops *ops =
  1315. clp->cl_mvops->state_renewal_ops;
  1316. int status;
  1317. /* Is the client already known to have an expired lease? */
  1318. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1319. return 0;
  1320. spin_lock(&clp->cl_lock);
  1321. cred = ops->get_state_renewal_cred_locked(clp);
  1322. spin_unlock(&clp->cl_lock);
  1323. if (cred == NULL) {
  1324. cred = nfs4_get_setclientid_cred(clp);
  1325. status = -ENOKEY;
  1326. if (cred == NULL)
  1327. goto out;
  1328. }
  1329. status = ops->renew_lease(clp, cred);
  1330. put_rpccred(cred);
  1331. out:
  1332. return nfs4_recovery_handle_error(clp, status);
  1333. }
  1334. static int nfs4_reclaim_lease(struct nfs_client *clp)
  1335. {
  1336. struct rpc_cred *cred;
  1337. const struct nfs4_state_recovery_ops *ops =
  1338. clp->cl_mvops->reboot_recovery_ops;
  1339. int status = -ENOENT;
  1340. cred = ops->get_clid_cred(clp);
  1341. if (cred != NULL) {
  1342. status = ops->establish_clid(clp, cred);
  1343. put_rpccred(cred);
  1344. /* Handle case where the user hasn't set up machine creds */
  1345. if (status == -EACCES && cred == clp->cl_machine_cred) {
  1346. nfs4_clear_machine_cred(clp);
  1347. status = -EAGAIN;
  1348. }
  1349. if (status == -NFS4ERR_MINOR_VERS_MISMATCH)
  1350. status = -EPROTONOSUPPORT;
  1351. }
  1352. return status;
  1353. }
  1354. #ifdef CONFIG_NFS_V4_1
  1355. void nfs4_schedule_session_recovery(struct nfs4_session *session)
  1356. {
  1357. struct nfs_client *clp = session->clp;
  1358. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1359. nfs4_schedule_lease_recovery(clp);
  1360. }
  1361. EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
  1362. void nfs41_handle_recall_slot(struct nfs_client *clp)
  1363. {
  1364. set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
  1365. nfs4_schedule_state_manager(clp);
  1366. }
  1367. static void nfs4_reset_all_state(struct nfs_client *clp)
  1368. {
  1369. if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
  1370. clp->cl_boot_time = CURRENT_TIME;
  1371. nfs4_state_start_reclaim_nograce(clp);
  1372. nfs4_schedule_state_manager(clp);
  1373. }
  1374. }
  1375. static void nfs41_handle_server_reboot(struct nfs_client *clp)
  1376. {
  1377. if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
  1378. nfs4_state_start_reclaim_reboot(clp);
  1379. nfs4_schedule_state_manager(clp);
  1380. }
  1381. }
  1382. static void nfs41_handle_state_revoked(struct nfs_client *clp)
  1383. {
  1384. /* Temporary */
  1385. nfs4_reset_all_state(clp);
  1386. }
  1387. static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
  1388. {
  1389. /* This will need to handle layouts too */
  1390. nfs_expire_all_delegations(clp);
  1391. }
  1392. static void nfs41_handle_cb_path_down(struct nfs_client *clp)
  1393. {
  1394. nfs_expire_all_delegations(clp);
  1395. if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
  1396. nfs4_schedule_state_manager(clp);
  1397. }
  1398. void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
  1399. {
  1400. if (!flags)
  1401. return;
  1402. if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
  1403. nfs41_handle_server_reboot(clp);
  1404. if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
  1405. SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
  1406. SEQ4_STATUS_ADMIN_STATE_REVOKED |
  1407. SEQ4_STATUS_LEASE_MOVED))
  1408. nfs41_handle_state_revoked(clp);
  1409. if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
  1410. nfs41_handle_recallable_state_revoked(clp);
  1411. if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
  1412. SEQ4_STATUS_BACKCHANNEL_FAULT |
  1413. SEQ4_STATUS_CB_PATH_DOWN_SESSION))
  1414. nfs41_handle_cb_path_down(clp);
  1415. }
  1416. static int nfs4_reset_session(struct nfs_client *clp)
  1417. {
  1418. int status;
  1419. nfs4_begin_drain_session(clp);
  1420. status = nfs4_proc_destroy_session(clp->cl_session);
  1421. if (status && status != -NFS4ERR_BADSESSION &&
  1422. status != -NFS4ERR_DEADSESSION) {
  1423. status = nfs4_recovery_handle_error(clp, status);
  1424. goto out;
  1425. }
  1426. memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
  1427. status = nfs4_proc_create_session(clp);
  1428. if (status) {
  1429. status = nfs4_recovery_handle_error(clp, status);
  1430. goto out;
  1431. }
  1432. clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1433. /* create_session negotiated new slot table */
  1434. clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
  1435. /* Let the state manager reestablish state */
  1436. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1437. nfs41_setup_state_renewal(clp);
  1438. out:
  1439. return status;
  1440. }
  1441. static int nfs4_recall_slot(struct nfs_client *clp)
  1442. {
  1443. struct nfs4_slot_table *fc_tbl = &clp->cl_session->fc_slot_table;
  1444. struct nfs4_channel_attrs *fc_attrs = &clp->cl_session->fc_attrs;
  1445. struct nfs4_slot *new, *old;
  1446. int i;
  1447. nfs4_begin_drain_session(clp);
  1448. new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot),
  1449. GFP_NOFS);
  1450. if (!new)
  1451. return -ENOMEM;
  1452. spin_lock(&fc_tbl->slot_tbl_lock);
  1453. for (i = 0; i < fc_tbl->target_max_slots; i++)
  1454. new[i].seq_nr = fc_tbl->slots[i].seq_nr;
  1455. old = fc_tbl->slots;
  1456. fc_tbl->slots = new;
  1457. fc_tbl->max_slots = fc_tbl->target_max_slots;
  1458. fc_tbl->target_max_slots = 0;
  1459. fc_attrs->max_reqs = fc_tbl->max_slots;
  1460. spin_unlock(&fc_tbl->slot_tbl_lock);
  1461. kfree(old);
  1462. nfs4_end_drain_session(clp);
  1463. return 0;
  1464. }
  1465. #else /* CONFIG_NFS_V4_1 */
  1466. static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
  1467. static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
  1468. static int nfs4_recall_slot(struct nfs_client *clp) { return 0; }
  1469. #endif /* CONFIG_NFS_V4_1 */
  1470. /* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
  1471. * on EXCHANGE_ID for v4.1
  1472. */
  1473. static void nfs4_set_lease_expired(struct nfs_client *clp, int status)
  1474. {
  1475. switch (status) {
  1476. case -NFS4ERR_CLID_INUSE:
  1477. case -NFS4ERR_STALE_CLIENTID:
  1478. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1479. break;
  1480. case -NFS4ERR_DELAY:
  1481. case -ETIMEDOUT:
  1482. case -EAGAIN:
  1483. ssleep(1);
  1484. break;
  1485. case -EKEYEXPIRED:
  1486. nfs4_warn_keyexpired(clp->cl_hostname);
  1487. case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
  1488. * in nfs4_exchange_id */
  1489. default:
  1490. return;
  1491. }
  1492. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1493. }
  1494. static void nfs4_state_manager(struct nfs_client *clp)
  1495. {
  1496. int status = 0;
  1497. /* Ensure exclusive access to NFSv4 state */
  1498. do {
  1499. if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
  1500. /* We're going to have to re-establish a clientid */
  1501. status = nfs4_reclaim_lease(clp);
  1502. if (status) {
  1503. nfs4_set_lease_expired(clp, status);
  1504. if (test_bit(NFS4CLNT_LEASE_EXPIRED,
  1505. &clp->cl_state))
  1506. continue;
  1507. if (clp->cl_cons_state ==
  1508. NFS_CS_SESSION_INITING)
  1509. nfs_mark_client_ready(clp, status);
  1510. goto out_error;
  1511. }
  1512. clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1513. if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH,
  1514. &clp->cl_state))
  1515. nfs4_state_start_reclaim_nograce(clp);
  1516. else
  1517. set_bit(NFS4CLNT_RECLAIM_REBOOT,
  1518. &clp->cl_state);
  1519. pnfs_destroy_all_layouts(clp);
  1520. }
  1521. if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
  1522. status = nfs4_check_lease(clp);
  1523. if (status < 0)
  1524. goto out_error;
  1525. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1526. continue;
  1527. }
  1528. /* Initialize or reset the session */
  1529. if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)
  1530. && nfs4_has_session(clp)) {
  1531. status = nfs4_reset_session(clp);
  1532. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1533. continue;
  1534. if (status < 0)
  1535. goto out_error;
  1536. }
  1537. /* First recover reboot state... */
  1538. if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
  1539. status = nfs4_do_reclaim(clp,
  1540. clp->cl_mvops->reboot_recovery_ops);
  1541. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
  1542. test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
  1543. continue;
  1544. nfs4_state_end_reclaim_reboot(clp);
  1545. if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
  1546. continue;
  1547. if (status < 0)
  1548. goto out_error;
  1549. }
  1550. /* Now recover expired state... */
  1551. if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
  1552. status = nfs4_do_reclaim(clp,
  1553. clp->cl_mvops->nograce_recovery_ops);
  1554. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
  1555. test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
  1556. test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1557. continue;
  1558. if (status < 0)
  1559. goto out_error;
  1560. }
  1561. nfs4_end_drain_session(clp);
  1562. if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
  1563. nfs_client_return_marked_delegations(clp);
  1564. continue;
  1565. }
  1566. /* Recall session slots */
  1567. if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state)
  1568. && nfs4_has_session(clp)) {
  1569. status = nfs4_recall_slot(clp);
  1570. if (status < 0)
  1571. goto out_error;
  1572. continue;
  1573. }
  1574. nfs4_clear_state_manager_bit(clp);
  1575. /* Did we race with an attempt to give us more work? */
  1576. if (clp->cl_state == 0)
  1577. break;
  1578. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  1579. break;
  1580. } while (atomic_read(&clp->cl_count) > 1);
  1581. return;
  1582. out_error:
  1583. printk(KERN_WARNING "NFS: state manager failed on NFSv4 server %s"
  1584. " with error %d\n", clp->cl_hostname, -status);
  1585. nfs4_end_drain_session(clp);
  1586. nfs4_clear_state_manager_bit(clp);
  1587. }
  1588. static int nfs4_run_state_manager(void *ptr)
  1589. {
  1590. struct nfs_client *clp = ptr;
  1591. allow_signal(SIGKILL);
  1592. nfs4_state_manager(clp);
  1593. nfs_put_client(clp);
  1594. module_put_and_exit(0);
  1595. return 0;
  1596. }
  1597. /*
  1598. * Local variables:
  1599. * c-basic-offset: 8
  1600. * End:
  1601. */