nfs4state.c 45 KB

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