nfs4state.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  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. 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. if (nfs4_release_lockowner(lsp) == 0)
  758. return;
  759. }
  760. nfs4_free_lock_state(lsp);
  761. }
  762. static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
  763. {
  764. struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
  765. dst->fl_u.nfs4_fl.owner = lsp;
  766. atomic_inc(&lsp->ls_count);
  767. }
  768. static void nfs4_fl_release_lock(struct file_lock *fl)
  769. {
  770. nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
  771. }
  772. static const struct file_lock_operations nfs4_fl_lock_ops = {
  773. .fl_copy_lock = nfs4_fl_copy_lock,
  774. .fl_release_private = nfs4_fl_release_lock,
  775. };
  776. int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
  777. {
  778. struct nfs4_lock_state *lsp;
  779. if (fl->fl_ops != NULL)
  780. return 0;
  781. if (fl->fl_flags & FL_POSIX)
  782. lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
  783. else if (fl->fl_flags & FL_FLOCK)
  784. lsp = nfs4_get_lock_state(state, 0, fl->fl_pid, NFS4_FLOCK_LOCK_TYPE);
  785. else
  786. return -EINVAL;
  787. if (lsp == NULL)
  788. return -ENOMEM;
  789. fl->fl_u.nfs4_fl.owner = lsp;
  790. fl->fl_ops = &nfs4_fl_lock_ops;
  791. return 0;
  792. }
  793. static bool nfs4_copy_lock_stateid(nfs4_stateid *dst, struct nfs4_state *state,
  794. fl_owner_t fl_owner, pid_t fl_pid)
  795. {
  796. struct nfs4_lock_state *lsp;
  797. bool ret = false;
  798. if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
  799. goto out;
  800. spin_lock(&state->state_lock);
  801. lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
  802. if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0) {
  803. nfs4_stateid_copy(dst, &lsp->ls_stateid);
  804. ret = true;
  805. }
  806. spin_unlock(&state->state_lock);
  807. nfs4_put_lock_state(lsp);
  808. out:
  809. return ret;
  810. }
  811. static void nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
  812. {
  813. int seq;
  814. do {
  815. seq = read_seqbegin(&state->seqlock);
  816. nfs4_stateid_copy(dst, &state->stateid);
  817. } while (read_seqretry(&state->seqlock, seq));
  818. }
  819. /*
  820. * Byte-range lock aware utility to initialize the stateid of read/write
  821. * requests.
  822. */
  823. void nfs4_select_rw_stateid(nfs4_stateid *dst, struct nfs4_state *state,
  824. fmode_t fmode, fl_owner_t fl_owner, pid_t fl_pid)
  825. {
  826. if (nfs4_copy_delegation_stateid(dst, state->inode, fmode))
  827. return;
  828. if (nfs4_copy_lock_stateid(dst, state, fl_owner, fl_pid))
  829. return;
  830. nfs4_copy_open_stateid(dst, state);
  831. }
  832. struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
  833. {
  834. struct nfs_seqid *new;
  835. new = kmalloc(sizeof(*new), gfp_mask);
  836. if (new != NULL) {
  837. new->sequence = counter;
  838. INIT_LIST_HEAD(&new->list);
  839. new->task = NULL;
  840. }
  841. return new;
  842. }
  843. void nfs_release_seqid(struct nfs_seqid *seqid)
  844. {
  845. struct nfs_seqid_counter *sequence;
  846. if (list_empty(&seqid->list))
  847. return;
  848. sequence = seqid->sequence;
  849. spin_lock(&sequence->lock);
  850. list_del_init(&seqid->list);
  851. if (!list_empty(&sequence->list)) {
  852. struct nfs_seqid *next;
  853. next = list_first_entry(&sequence->list,
  854. struct nfs_seqid, list);
  855. rpc_wake_up_queued_task(&sequence->wait, next->task);
  856. }
  857. spin_unlock(&sequence->lock);
  858. }
  859. void nfs_free_seqid(struct nfs_seqid *seqid)
  860. {
  861. nfs_release_seqid(seqid);
  862. kfree(seqid);
  863. }
  864. /*
  865. * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
  866. * failed with a seqid incrementing error -
  867. * see comments nfs_fs.h:seqid_mutating_error()
  868. */
  869. static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
  870. {
  871. BUG_ON(list_first_entry(&seqid->sequence->list, struct nfs_seqid, list) != seqid);
  872. switch (status) {
  873. case 0:
  874. break;
  875. case -NFS4ERR_BAD_SEQID:
  876. if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
  877. return;
  878. printk(KERN_WARNING "NFS: v4 server returned a bad"
  879. " sequence-id error on an"
  880. " unconfirmed sequence %p!\n",
  881. seqid->sequence);
  882. case -NFS4ERR_STALE_CLIENTID:
  883. case -NFS4ERR_STALE_STATEID:
  884. case -NFS4ERR_BAD_STATEID:
  885. case -NFS4ERR_BADXDR:
  886. case -NFS4ERR_RESOURCE:
  887. case -NFS4ERR_NOFILEHANDLE:
  888. /* Non-seqid mutating errors */
  889. return;
  890. };
  891. /*
  892. * Note: no locking needed as we are guaranteed to be first
  893. * on the sequence list
  894. */
  895. seqid->sequence->counter++;
  896. }
  897. void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
  898. {
  899. struct nfs4_state_owner *sp = container_of(seqid->sequence,
  900. struct nfs4_state_owner, so_seqid);
  901. struct nfs_server *server = sp->so_server;
  902. if (status == -NFS4ERR_BAD_SEQID)
  903. nfs4_drop_state_owner(sp);
  904. if (!nfs4_has_session(server->nfs_client))
  905. nfs_increment_seqid(status, seqid);
  906. }
  907. /*
  908. * Increment the seqid if the LOCK/LOCKU succeeded, or
  909. * failed with a seqid incrementing error -
  910. * see comments nfs_fs.h:seqid_mutating_error()
  911. */
  912. void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
  913. {
  914. nfs_increment_seqid(status, seqid);
  915. }
  916. int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
  917. {
  918. struct nfs_seqid_counter *sequence = seqid->sequence;
  919. int status = 0;
  920. spin_lock(&sequence->lock);
  921. seqid->task = task;
  922. if (list_empty(&seqid->list))
  923. list_add_tail(&seqid->list, &sequence->list);
  924. if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
  925. goto unlock;
  926. rpc_sleep_on(&sequence->wait, task, NULL);
  927. status = -EAGAIN;
  928. unlock:
  929. spin_unlock(&sequence->lock);
  930. return status;
  931. }
  932. static int nfs4_run_state_manager(void *);
  933. static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
  934. {
  935. smp_mb__before_clear_bit();
  936. clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
  937. smp_mb__after_clear_bit();
  938. wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
  939. rpc_wake_up(&clp->cl_rpcwaitq);
  940. }
  941. /*
  942. * Schedule the nfs_client asynchronous state management routine
  943. */
  944. void nfs4_schedule_state_manager(struct nfs_client *clp)
  945. {
  946. struct task_struct *task;
  947. char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
  948. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  949. return;
  950. __module_get(THIS_MODULE);
  951. atomic_inc(&clp->cl_count);
  952. /* The rcu_read_lock() is not strictly necessary, as the state
  953. * manager is the only thread that ever changes the rpc_xprt
  954. * after it's initialized. At this point, we're single threaded. */
  955. rcu_read_lock();
  956. snprintf(buf, sizeof(buf), "%s-manager",
  957. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  958. rcu_read_unlock();
  959. task = kthread_run(nfs4_run_state_manager, clp, buf);
  960. if (IS_ERR(task)) {
  961. printk(KERN_ERR "%s: kthread_run: %ld\n",
  962. __func__, PTR_ERR(task));
  963. nfs4_clear_state_manager_bit(clp);
  964. nfs_put_client(clp);
  965. module_put(THIS_MODULE);
  966. }
  967. }
  968. /*
  969. * Schedule a lease recovery attempt
  970. */
  971. void nfs4_schedule_lease_recovery(struct nfs_client *clp)
  972. {
  973. if (!clp)
  974. return;
  975. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  976. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  977. nfs4_schedule_state_manager(clp);
  978. }
  979. EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
  980. /*
  981. * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
  982. * @clp: client to process
  983. *
  984. * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
  985. * resend of the SETCLIENTID and hence re-establish the
  986. * callback channel. Then return all existing delegations.
  987. */
  988. static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
  989. {
  990. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  991. nfs_expire_all_delegations(clp);
  992. }
  993. void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
  994. {
  995. nfs40_handle_cb_pathdown(clp);
  996. nfs4_schedule_state_manager(clp);
  997. }
  998. static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
  999. {
  1000. set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1001. /* Don't recover state that expired before the reboot */
  1002. if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
  1003. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1004. return 0;
  1005. }
  1006. set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
  1007. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  1008. return 1;
  1009. }
  1010. static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
  1011. {
  1012. set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
  1013. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1014. set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
  1015. set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
  1016. return 1;
  1017. }
  1018. void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
  1019. {
  1020. struct nfs_client *clp = server->nfs_client;
  1021. nfs4_state_mark_reclaim_nograce(clp, state);
  1022. nfs4_schedule_state_manager(clp);
  1023. }
  1024. EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
  1025. void nfs_inode_find_state_and_recover(struct inode *inode,
  1026. const nfs4_stateid *stateid)
  1027. {
  1028. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  1029. struct nfs_inode *nfsi = NFS_I(inode);
  1030. struct nfs_open_context *ctx;
  1031. struct nfs4_state *state;
  1032. bool found = false;
  1033. spin_lock(&inode->i_lock);
  1034. list_for_each_entry(ctx, &nfsi->open_files, list) {
  1035. state = ctx->state;
  1036. if (state == NULL)
  1037. continue;
  1038. if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
  1039. continue;
  1040. if (!nfs4_stateid_match(&state->stateid, stateid))
  1041. continue;
  1042. nfs4_state_mark_reclaim_nograce(clp, state);
  1043. found = true;
  1044. }
  1045. spin_unlock(&inode->i_lock);
  1046. if (found)
  1047. nfs4_schedule_state_manager(clp);
  1048. }
  1049. static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
  1050. {
  1051. struct inode *inode = state->inode;
  1052. struct nfs_inode *nfsi = NFS_I(inode);
  1053. struct file_lock *fl;
  1054. int status = 0;
  1055. if (inode->i_flock == NULL)
  1056. return 0;
  1057. /* Guard against delegation returns and new lock/unlock calls */
  1058. down_write(&nfsi->rwsem);
  1059. /* Protect inode->i_flock using the BKL */
  1060. lock_flocks();
  1061. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  1062. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  1063. continue;
  1064. if (nfs_file_open_context(fl->fl_file)->state != state)
  1065. continue;
  1066. unlock_flocks();
  1067. status = ops->recover_lock(state, fl);
  1068. switch (status) {
  1069. case 0:
  1070. break;
  1071. case -ESTALE:
  1072. case -NFS4ERR_ADMIN_REVOKED:
  1073. case -NFS4ERR_STALE_STATEID:
  1074. case -NFS4ERR_BAD_STATEID:
  1075. case -NFS4ERR_EXPIRED:
  1076. case -NFS4ERR_NO_GRACE:
  1077. case -NFS4ERR_STALE_CLIENTID:
  1078. case -NFS4ERR_BADSESSION:
  1079. case -NFS4ERR_BADSLOT:
  1080. case -NFS4ERR_BAD_HIGH_SLOT:
  1081. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1082. goto out;
  1083. default:
  1084. printk(KERN_ERR "NFS: %s: unhandled error %d. "
  1085. "Zeroing state\n", __func__, status);
  1086. case -ENOMEM:
  1087. case -NFS4ERR_DENIED:
  1088. case -NFS4ERR_RECLAIM_BAD:
  1089. case -NFS4ERR_RECLAIM_CONFLICT:
  1090. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  1091. status = 0;
  1092. }
  1093. lock_flocks();
  1094. }
  1095. unlock_flocks();
  1096. out:
  1097. up_write(&nfsi->rwsem);
  1098. return status;
  1099. }
  1100. static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
  1101. {
  1102. struct nfs4_state *state;
  1103. struct nfs4_lock_state *lock;
  1104. int status = 0;
  1105. /* Note: we rely on the sp->so_states list being ordered
  1106. * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
  1107. * states first.
  1108. * This is needed to ensure that the server won't give us any
  1109. * read delegations that we have to return if, say, we are
  1110. * recovering after a network partition or a reboot from a
  1111. * server that doesn't support a grace period.
  1112. */
  1113. restart:
  1114. spin_lock(&sp->so_lock);
  1115. list_for_each_entry(state, &sp->so_states, open_states) {
  1116. if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
  1117. continue;
  1118. if (state->state == 0)
  1119. continue;
  1120. atomic_inc(&state->count);
  1121. spin_unlock(&sp->so_lock);
  1122. status = ops->recover_open(sp, state);
  1123. if (status >= 0) {
  1124. status = nfs4_reclaim_locks(state, ops);
  1125. if (status >= 0) {
  1126. spin_lock(&state->state_lock);
  1127. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  1128. if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
  1129. printk("NFS: %s: Lock reclaim "
  1130. "failed!\n", __func__);
  1131. }
  1132. spin_unlock(&state->state_lock);
  1133. nfs4_put_open_state(state);
  1134. goto restart;
  1135. }
  1136. }
  1137. switch (status) {
  1138. default:
  1139. printk(KERN_ERR "NFS: %s: unhandled error %d. "
  1140. "Zeroing state\n", __func__, status);
  1141. case -ENOENT:
  1142. case -ENOMEM:
  1143. case -ESTALE:
  1144. /*
  1145. * Open state on this file cannot be recovered
  1146. * All we can do is revert to using the zero stateid.
  1147. */
  1148. memset(&state->stateid, 0,
  1149. sizeof(state->stateid));
  1150. /* Mark the file as being 'closed' */
  1151. state->state = 0;
  1152. break;
  1153. case -EKEYEXPIRED:
  1154. /*
  1155. * User RPCSEC_GSS context has expired.
  1156. * We cannot recover this stateid now, so
  1157. * skip it and allow recovery thread to
  1158. * proceed.
  1159. */
  1160. break;
  1161. case -NFS4ERR_ADMIN_REVOKED:
  1162. case -NFS4ERR_STALE_STATEID:
  1163. case -NFS4ERR_BAD_STATEID:
  1164. case -NFS4ERR_RECLAIM_BAD:
  1165. case -NFS4ERR_RECLAIM_CONFLICT:
  1166. nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
  1167. break;
  1168. case -NFS4ERR_EXPIRED:
  1169. case -NFS4ERR_NO_GRACE:
  1170. nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
  1171. case -NFS4ERR_STALE_CLIENTID:
  1172. case -NFS4ERR_BADSESSION:
  1173. case -NFS4ERR_BADSLOT:
  1174. case -NFS4ERR_BAD_HIGH_SLOT:
  1175. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1176. goto out_err;
  1177. }
  1178. nfs4_put_open_state(state);
  1179. goto restart;
  1180. }
  1181. spin_unlock(&sp->so_lock);
  1182. return 0;
  1183. out_err:
  1184. nfs4_put_open_state(state);
  1185. return status;
  1186. }
  1187. static void nfs4_clear_open_state(struct nfs4_state *state)
  1188. {
  1189. struct nfs4_lock_state *lock;
  1190. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1191. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1192. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1193. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1194. spin_lock(&state->state_lock);
  1195. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  1196. lock->ls_seqid.flags = 0;
  1197. lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
  1198. }
  1199. spin_unlock(&state->state_lock);
  1200. }
  1201. static void nfs4_reset_seqids(struct nfs_server *server,
  1202. int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  1203. {
  1204. struct nfs_client *clp = server->nfs_client;
  1205. struct nfs4_state_owner *sp;
  1206. struct rb_node *pos;
  1207. struct nfs4_state *state;
  1208. spin_lock(&clp->cl_lock);
  1209. for (pos = rb_first(&server->state_owners);
  1210. pos != NULL;
  1211. pos = rb_next(pos)) {
  1212. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  1213. sp->so_seqid.flags = 0;
  1214. spin_lock(&sp->so_lock);
  1215. list_for_each_entry(state, &sp->so_states, open_states) {
  1216. if (mark_reclaim(clp, state))
  1217. nfs4_clear_open_state(state);
  1218. }
  1219. spin_unlock(&sp->so_lock);
  1220. }
  1221. spin_unlock(&clp->cl_lock);
  1222. }
  1223. static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
  1224. int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  1225. {
  1226. struct nfs_server *server;
  1227. rcu_read_lock();
  1228. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1229. nfs4_reset_seqids(server, mark_reclaim);
  1230. rcu_read_unlock();
  1231. }
  1232. static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
  1233. {
  1234. /* Mark all delegations for reclaim */
  1235. nfs_delegation_mark_reclaim(clp);
  1236. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
  1237. }
  1238. static void nfs4_reclaim_complete(struct nfs_client *clp,
  1239. const struct nfs4_state_recovery_ops *ops)
  1240. {
  1241. /* Notify the server we're done reclaiming our state */
  1242. if (ops->reclaim_complete)
  1243. (void)ops->reclaim_complete(clp);
  1244. }
  1245. static void nfs4_clear_reclaim_server(struct nfs_server *server)
  1246. {
  1247. struct nfs_client *clp = server->nfs_client;
  1248. struct nfs4_state_owner *sp;
  1249. struct rb_node *pos;
  1250. struct nfs4_state *state;
  1251. spin_lock(&clp->cl_lock);
  1252. for (pos = rb_first(&server->state_owners);
  1253. pos != NULL;
  1254. pos = rb_next(pos)) {
  1255. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  1256. spin_lock(&sp->so_lock);
  1257. list_for_each_entry(state, &sp->so_states, open_states) {
  1258. if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
  1259. &state->flags))
  1260. continue;
  1261. nfs4_state_mark_reclaim_nograce(clp, state);
  1262. }
  1263. spin_unlock(&sp->so_lock);
  1264. }
  1265. spin_unlock(&clp->cl_lock);
  1266. }
  1267. static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
  1268. {
  1269. struct nfs_server *server;
  1270. if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1271. return 0;
  1272. rcu_read_lock();
  1273. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1274. nfs4_clear_reclaim_server(server);
  1275. rcu_read_unlock();
  1276. nfs_delegation_reap_unclaimed(clp);
  1277. return 1;
  1278. }
  1279. static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
  1280. {
  1281. if (!nfs4_state_clear_reclaim_reboot(clp))
  1282. return;
  1283. nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
  1284. }
  1285. static void nfs_delegation_clear_all(struct nfs_client *clp)
  1286. {
  1287. nfs_delegation_mark_reclaim(clp);
  1288. nfs_delegation_reap_unclaimed(clp);
  1289. }
  1290. static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
  1291. {
  1292. nfs_delegation_clear_all(clp);
  1293. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
  1294. }
  1295. static void nfs4_warn_keyexpired(const char *s)
  1296. {
  1297. printk_ratelimited(KERN_WARNING "Error: state manager"
  1298. " encountered RPCSEC_GSS session"
  1299. " expired against NFSv4 server %s.\n",
  1300. s);
  1301. }
  1302. static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
  1303. {
  1304. switch (error) {
  1305. case 0:
  1306. break;
  1307. case -NFS4ERR_CB_PATH_DOWN:
  1308. nfs40_handle_cb_pathdown(clp);
  1309. break;
  1310. case -NFS4ERR_NO_GRACE:
  1311. nfs4_state_end_reclaim_reboot(clp);
  1312. break;
  1313. case -NFS4ERR_STALE_CLIENTID:
  1314. case -NFS4ERR_LEASE_MOVED:
  1315. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1316. nfs4_state_clear_reclaim_reboot(clp);
  1317. nfs4_state_start_reclaim_reboot(clp);
  1318. break;
  1319. case -NFS4ERR_EXPIRED:
  1320. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1321. nfs4_state_start_reclaim_nograce(clp);
  1322. break;
  1323. case -NFS4ERR_BADSESSION:
  1324. case -NFS4ERR_BADSLOT:
  1325. case -NFS4ERR_BAD_HIGH_SLOT:
  1326. case -NFS4ERR_DEADSESSION:
  1327. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1328. case -NFS4ERR_SEQ_FALSE_RETRY:
  1329. case -NFS4ERR_SEQ_MISORDERED:
  1330. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1331. /* Zero session reset errors */
  1332. break;
  1333. case -EKEYEXPIRED:
  1334. /* Nothing we can do */
  1335. nfs4_warn_keyexpired(clp->cl_hostname);
  1336. break;
  1337. default:
  1338. return error;
  1339. }
  1340. return 0;
  1341. }
  1342. static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
  1343. {
  1344. struct nfs4_state_owner *sp;
  1345. struct nfs_server *server;
  1346. struct rb_node *pos;
  1347. int status = 0;
  1348. restart:
  1349. rcu_read_lock();
  1350. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  1351. nfs4_purge_state_owners(server);
  1352. spin_lock(&clp->cl_lock);
  1353. for (pos = rb_first(&server->state_owners);
  1354. pos != NULL;
  1355. pos = rb_next(pos)) {
  1356. sp = rb_entry(pos,
  1357. struct nfs4_state_owner, so_server_node);
  1358. if (!test_and_clear_bit(ops->owner_flag_bit,
  1359. &sp->so_flags))
  1360. continue;
  1361. atomic_inc(&sp->so_count);
  1362. spin_unlock(&clp->cl_lock);
  1363. rcu_read_unlock();
  1364. status = nfs4_reclaim_open_state(sp, ops);
  1365. if (status < 0) {
  1366. set_bit(ops->owner_flag_bit, &sp->so_flags);
  1367. nfs4_put_state_owner(sp);
  1368. return nfs4_recovery_handle_error(clp, status);
  1369. }
  1370. nfs4_put_state_owner(sp);
  1371. goto restart;
  1372. }
  1373. spin_unlock(&clp->cl_lock);
  1374. }
  1375. rcu_read_unlock();
  1376. return status;
  1377. }
  1378. static int nfs4_check_lease(struct nfs_client *clp)
  1379. {
  1380. struct rpc_cred *cred;
  1381. const struct nfs4_state_maintenance_ops *ops =
  1382. clp->cl_mvops->state_renewal_ops;
  1383. int status;
  1384. /* Is the client already known to have an expired lease? */
  1385. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1386. return 0;
  1387. spin_lock(&clp->cl_lock);
  1388. cred = ops->get_state_renewal_cred_locked(clp);
  1389. spin_unlock(&clp->cl_lock);
  1390. if (cred == NULL) {
  1391. cred = nfs4_get_setclientid_cred(clp);
  1392. status = -ENOKEY;
  1393. if (cred == NULL)
  1394. goto out;
  1395. }
  1396. status = ops->renew_lease(clp, cred);
  1397. put_rpccred(cred);
  1398. out:
  1399. return nfs4_recovery_handle_error(clp, status);
  1400. }
  1401. static int nfs4_reclaim_lease(struct nfs_client *clp)
  1402. {
  1403. struct rpc_cred *cred;
  1404. const struct nfs4_state_recovery_ops *ops =
  1405. clp->cl_mvops->reboot_recovery_ops;
  1406. int status = -ENOENT;
  1407. cred = ops->get_clid_cred(clp);
  1408. if (cred != NULL) {
  1409. status = ops->establish_clid(clp, cred);
  1410. put_rpccred(cred);
  1411. /* Handle case where the user hasn't set up machine creds */
  1412. if (status == -EACCES && cred == clp->cl_machine_cred) {
  1413. nfs4_clear_machine_cred(clp);
  1414. status = -EAGAIN;
  1415. }
  1416. if (status == -NFS4ERR_MINOR_VERS_MISMATCH)
  1417. status = -EPROTONOSUPPORT;
  1418. }
  1419. return status;
  1420. }
  1421. #ifdef CONFIG_NFS_V4_1
  1422. void nfs4_schedule_session_recovery(struct nfs4_session *session)
  1423. {
  1424. struct nfs_client *clp = session->clp;
  1425. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1426. nfs4_schedule_lease_recovery(clp);
  1427. }
  1428. EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
  1429. void nfs41_handle_recall_slot(struct nfs_client *clp)
  1430. {
  1431. set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
  1432. nfs4_schedule_state_manager(clp);
  1433. }
  1434. static void nfs4_reset_all_state(struct nfs_client *clp)
  1435. {
  1436. if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
  1437. clp->cl_boot_time = CURRENT_TIME;
  1438. nfs4_state_start_reclaim_nograce(clp);
  1439. nfs4_schedule_state_manager(clp);
  1440. }
  1441. }
  1442. static void nfs41_handle_server_reboot(struct nfs_client *clp)
  1443. {
  1444. if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
  1445. nfs4_state_start_reclaim_reboot(clp);
  1446. nfs4_schedule_state_manager(clp);
  1447. }
  1448. }
  1449. static void nfs41_handle_state_revoked(struct nfs_client *clp)
  1450. {
  1451. /* Temporary */
  1452. nfs4_reset_all_state(clp);
  1453. }
  1454. static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
  1455. {
  1456. /* This will need to handle layouts too */
  1457. nfs_expire_all_delegations(clp);
  1458. }
  1459. static void nfs41_handle_cb_path_down(struct nfs_client *clp)
  1460. {
  1461. nfs_expire_all_delegations(clp);
  1462. if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
  1463. nfs4_schedule_state_manager(clp);
  1464. }
  1465. void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
  1466. {
  1467. if (!flags)
  1468. return;
  1469. if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
  1470. nfs41_handle_server_reboot(clp);
  1471. if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
  1472. SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
  1473. SEQ4_STATUS_ADMIN_STATE_REVOKED |
  1474. SEQ4_STATUS_LEASE_MOVED))
  1475. nfs41_handle_state_revoked(clp);
  1476. if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
  1477. nfs41_handle_recallable_state_revoked(clp);
  1478. if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
  1479. SEQ4_STATUS_BACKCHANNEL_FAULT |
  1480. SEQ4_STATUS_CB_PATH_DOWN_SESSION))
  1481. nfs41_handle_cb_path_down(clp);
  1482. }
  1483. static int nfs4_reset_session(struct nfs_client *clp)
  1484. {
  1485. int status;
  1486. nfs4_begin_drain_session(clp);
  1487. status = nfs4_proc_destroy_session(clp->cl_session);
  1488. if (status && status != -NFS4ERR_BADSESSION &&
  1489. status != -NFS4ERR_DEADSESSION) {
  1490. status = nfs4_recovery_handle_error(clp, status);
  1491. goto out;
  1492. }
  1493. memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
  1494. status = nfs4_proc_create_session(clp);
  1495. if (status) {
  1496. status = nfs4_recovery_handle_error(clp, status);
  1497. goto out;
  1498. }
  1499. clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1500. /* create_session negotiated new slot table */
  1501. clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
  1502. /* Let the state manager reestablish state */
  1503. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1504. nfs41_setup_state_renewal(clp);
  1505. out:
  1506. return status;
  1507. }
  1508. static int nfs4_recall_slot(struct nfs_client *clp)
  1509. {
  1510. struct nfs4_slot_table *fc_tbl = &clp->cl_session->fc_slot_table;
  1511. struct nfs4_channel_attrs *fc_attrs = &clp->cl_session->fc_attrs;
  1512. struct nfs4_slot *new, *old;
  1513. int i;
  1514. nfs4_begin_drain_session(clp);
  1515. new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot),
  1516. GFP_NOFS);
  1517. if (!new)
  1518. return -ENOMEM;
  1519. spin_lock(&fc_tbl->slot_tbl_lock);
  1520. for (i = 0; i < fc_tbl->target_max_slots; i++)
  1521. new[i].seq_nr = fc_tbl->slots[i].seq_nr;
  1522. old = fc_tbl->slots;
  1523. fc_tbl->slots = new;
  1524. fc_tbl->max_slots = fc_tbl->target_max_slots;
  1525. fc_tbl->target_max_slots = 0;
  1526. fc_attrs->max_reqs = fc_tbl->max_slots;
  1527. spin_unlock(&fc_tbl->slot_tbl_lock);
  1528. kfree(old);
  1529. nfs4_end_drain_session(clp);
  1530. return 0;
  1531. }
  1532. #else /* CONFIG_NFS_V4_1 */
  1533. static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
  1534. static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
  1535. static int nfs4_recall_slot(struct nfs_client *clp) { return 0; }
  1536. #endif /* CONFIG_NFS_V4_1 */
  1537. /* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
  1538. * on EXCHANGE_ID for v4.1
  1539. */
  1540. static void nfs4_set_lease_expired(struct nfs_client *clp, int status)
  1541. {
  1542. switch (status) {
  1543. case -NFS4ERR_CLID_INUSE:
  1544. case -NFS4ERR_STALE_CLIENTID:
  1545. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1546. break;
  1547. case -NFS4ERR_DELAY:
  1548. case -ETIMEDOUT:
  1549. case -EAGAIN:
  1550. ssleep(1);
  1551. break;
  1552. case -EKEYEXPIRED:
  1553. nfs4_warn_keyexpired(clp->cl_hostname);
  1554. case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
  1555. * in nfs4_exchange_id */
  1556. default:
  1557. return;
  1558. }
  1559. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1560. }
  1561. static void nfs4_state_manager(struct nfs_client *clp)
  1562. {
  1563. int status = 0;
  1564. /* Ensure exclusive access to NFSv4 state */
  1565. do {
  1566. if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
  1567. /* We're going to have to re-establish a clientid */
  1568. status = nfs4_reclaim_lease(clp);
  1569. if (status) {
  1570. nfs4_set_lease_expired(clp, status);
  1571. if (test_bit(NFS4CLNT_LEASE_EXPIRED,
  1572. &clp->cl_state))
  1573. continue;
  1574. if (clp->cl_cons_state ==
  1575. NFS_CS_SESSION_INITING)
  1576. nfs_mark_client_ready(clp, status);
  1577. goto out_error;
  1578. }
  1579. clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1580. if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH,
  1581. &clp->cl_state))
  1582. nfs4_state_start_reclaim_nograce(clp);
  1583. else
  1584. set_bit(NFS4CLNT_RECLAIM_REBOOT,
  1585. &clp->cl_state);
  1586. pnfs_destroy_all_layouts(clp);
  1587. }
  1588. if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
  1589. status = nfs4_check_lease(clp);
  1590. if (status < 0)
  1591. goto out_error;
  1592. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1593. continue;
  1594. }
  1595. /* Initialize or reset the session */
  1596. if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)
  1597. && nfs4_has_session(clp)) {
  1598. status = nfs4_reset_session(clp);
  1599. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1600. continue;
  1601. if (status < 0)
  1602. goto out_error;
  1603. }
  1604. /* First recover reboot state... */
  1605. if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
  1606. status = nfs4_do_reclaim(clp,
  1607. clp->cl_mvops->reboot_recovery_ops);
  1608. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
  1609. test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
  1610. continue;
  1611. nfs4_state_end_reclaim_reboot(clp);
  1612. if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
  1613. continue;
  1614. if (status < 0)
  1615. goto out_error;
  1616. }
  1617. /* Now recover expired state... */
  1618. if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
  1619. status = nfs4_do_reclaim(clp,
  1620. clp->cl_mvops->nograce_recovery_ops);
  1621. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
  1622. test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
  1623. test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1624. continue;
  1625. if (status < 0)
  1626. goto out_error;
  1627. }
  1628. nfs4_end_drain_session(clp);
  1629. if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
  1630. nfs_client_return_marked_delegations(clp);
  1631. continue;
  1632. }
  1633. /* Recall session slots */
  1634. if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state)
  1635. && nfs4_has_session(clp)) {
  1636. status = nfs4_recall_slot(clp);
  1637. if (status < 0)
  1638. goto out_error;
  1639. continue;
  1640. }
  1641. nfs4_clear_state_manager_bit(clp);
  1642. /* Did we race with an attempt to give us more work? */
  1643. if (clp->cl_state == 0)
  1644. break;
  1645. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  1646. break;
  1647. } while (atomic_read(&clp->cl_count) > 1);
  1648. return;
  1649. out_error:
  1650. printk(KERN_WARNING "NFS: state manager failed on NFSv4 server %s"
  1651. " with error %d\n", clp->cl_hostname, -status);
  1652. nfs4_end_drain_session(clp);
  1653. nfs4_clear_state_manager_bit(clp);
  1654. }
  1655. static int nfs4_run_state_manager(void *ptr)
  1656. {
  1657. struct nfs_client *clp = ptr;
  1658. allow_signal(SIGKILL);
  1659. nfs4_state_manager(clp);
  1660. nfs_put_client(clp);
  1661. module_put_and_exit(0);
  1662. return 0;
  1663. }
  1664. /*
  1665. * Local variables:
  1666. * c-basic-offset: 8
  1667. * End:
  1668. */