nfs4state.c 46 KB

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