nfs4state.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  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/smp_lock.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/workqueue.h>
  49. #include <linux/bitops.h>
  50. #include "nfs4_fs.h"
  51. #include "callback.h"
  52. #include "delegation.h"
  53. #include "internal.h"
  54. #define OPENOWNER_POOL_SIZE 8
  55. const nfs4_stateid zero_stateid;
  56. static LIST_HEAD(nfs4_clientid_list);
  57. int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
  58. {
  59. unsigned short port;
  60. int status;
  61. port = nfs_callback_tcpport;
  62. if (clp->cl_addr.ss_family == AF_INET6)
  63. port = nfs_callback_tcpport6;
  64. status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred);
  65. if (status == 0)
  66. status = nfs4_proc_setclientid_confirm(clp, cred);
  67. if (status == 0)
  68. nfs4_schedule_state_renewal(clp);
  69. return status;
  70. }
  71. struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
  72. {
  73. struct rpc_cred *cred = NULL;
  74. if (clp->cl_machine_cred != NULL)
  75. cred = get_rpccred(clp->cl_machine_cred);
  76. return cred;
  77. }
  78. static void nfs4_clear_machine_cred(struct nfs_client *clp)
  79. {
  80. struct rpc_cred *cred;
  81. spin_lock(&clp->cl_lock);
  82. cred = clp->cl_machine_cred;
  83. clp->cl_machine_cred = NULL;
  84. spin_unlock(&clp->cl_lock);
  85. if (cred != NULL)
  86. put_rpccred(cred);
  87. }
  88. struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
  89. {
  90. struct nfs4_state_owner *sp;
  91. struct rb_node *pos;
  92. struct rpc_cred *cred = NULL;
  93. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  94. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  95. if (list_empty(&sp->so_states))
  96. continue;
  97. cred = get_rpccred(sp->so_cred);
  98. break;
  99. }
  100. return cred;
  101. }
  102. struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
  103. {
  104. struct nfs4_state_owner *sp;
  105. struct rb_node *pos;
  106. struct rpc_cred *cred;
  107. spin_lock(&clp->cl_lock);
  108. cred = nfs4_get_machine_cred_locked(clp);
  109. if (cred != NULL)
  110. goto out;
  111. pos = rb_first(&clp->cl_state_owners);
  112. if (pos != NULL) {
  113. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  114. cred = get_rpccred(sp->so_cred);
  115. }
  116. out:
  117. spin_unlock(&clp->cl_lock);
  118. return cred;
  119. }
  120. static void nfs_alloc_unique_id(struct rb_root *root, struct nfs_unique_id *new,
  121. __u64 minval, int maxbits)
  122. {
  123. struct rb_node **p, *parent;
  124. struct nfs_unique_id *pos;
  125. __u64 mask = ~0ULL;
  126. if (maxbits < 64)
  127. mask = (1ULL << maxbits) - 1ULL;
  128. /* Ensure distribution is more or less flat */
  129. get_random_bytes(&new->id, sizeof(new->id));
  130. new->id &= mask;
  131. if (new->id < minval)
  132. new->id += minval;
  133. retry:
  134. p = &root->rb_node;
  135. parent = NULL;
  136. while (*p != NULL) {
  137. parent = *p;
  138. pos = rb_entry(parent, struct nfs_unique_id, rb_node);
  139. if (new->id < pos->id)
  140. p = &(*p)->rb_left;
  141. else if (new->id > pos->id)
  142. p = &(*p)->rb_right;
  143. else
  144. goto id_exists;
  145. }
  146. rb_link_node(&new->rb_node, parent, p);
  147. rb_insert_color(&new->rb_node, root);
  148. return;
  149. id_exists:
  150. for (;;) {
  151. new->id++;
  152. if (new->id < minval || (new->id & mask) != new->id) {
  153. new->id = minval;
  154. break;
  155. }
  156. parent = rb_next(parent);
  157. if (parent == NULL)
  158. break;
  159. pos = rb_entry(parent, struct nfs_unique_id, rb_node);
  160. if (new->id < pos->id)
  161. break;
  162. }
  163. goto retry;
  164. }
  165. static void nfs_free_unique_id(struct rb_root *root, struct nfs_unique_id *id)
  166. {
  167. rb_erase(&id->rb_node, root);
  168. }
  169. static struct nfs4_state_owner *
  170. nfs4_find_state_owner(struct nfs_server *server, struct rpc_cred *cred)
  171. {
  172. struct nfs_client *clp = server->nfs_client;
  173. struct rb_node **p = &clp->cl_state_owners.rb_node,
  174. *parent = NULL;
  175. struct nfs4_state_owner *sp, *res = NULL;
  176. while (*p != NULL) {
  177. parent = *p;
  178. sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
  179. if (server < sp->so_server) {
  180. p = &parent->rb_left;
  181. continue;
  182. }
  183. if (server > sp->so_server) {
  184. p = &parent->rb_right;
  185. continue;
  186. }
  187. if (cred < sp->so_cred)
  188. p = &parent->rb_left;
  189. else if (cred > sp->so_cred)
  190. p = &parent->rb_right;
  191. else {
  192. atomic_inc(&sp->so_count);
  193. res = sp;
  194. break;
  195. }
  196. }
  197. return res;
  198. }
  199. static struct nfs4_state_owner *
  200. nfs4_insert_state_owner(struct nfs_client *clp, struct nfs4_state_owner *new)
  201. {
  202. struct rb_node **p = &clp->cl_state_owners.rb_node,
  203. *parent = NULL;
  204. struct nfs4_state_owner *sp;
  205. while (*p != NULL) {
  206. parent = *p;
  207. sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
  208. if (new->so_server < sp->so_server) {
  209. p = &parent->rb_left;
  210. continue;
  211. }
  212. if (new->so_server > sp->so_server) {
  213. p = &parent->rb_right;
  214. continue;
  215. }
  216. if (new->so_cred < sp->so_cred)
  217. p = &parent->rb_left;
  218. else if (new->so_cred > sp->so_cred)
  219. p = &parent->rb_right;
  220. else {
  221. atomic_inc(&sp->so_count);
  222. return sp;
  223. }
  224. }
  225. nfs_alloc_unique_id(&clp->cl_openowner_id, &new->so_owner_id, 1, 64);
  226. rb_link_node(&new->so_client_node, parent, p);
  227. rb_insert_color(&new->so_client_node, &clp->cl_state_owners);
  228. return new;
  229. }
  230. static void
  231. nfs4_remove_state_owner(struct nfs_client *clp, struct nfs4_state_owner *sp)
  232. {
  233. if (!RB_EMPTY_NODE(&sp->so_client_node))
  234. rb_erase(&sp->so_client_node, &clp->cl_state_owners);
  235. nfs_free_unique_id(&clp->cl_openowner_id, &sp->so_owner_id);
  236. }
  237. /*
  238. * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
  239. * create a new state_owner.
  240. *
  241. */
  242. static struct nfs4_state_owner *
  243. nfs4_alloc_state_owner(void)
  244. {
  245. struct nfs4_state_owner *sp;
  246. sp = kzalloc(sizeof(*sp),GFP_KERNEL);
  247. if (!sp)
  248. return NULL;
  249. spin_lock_init(&sp->so_lock);
  250. INIT_LIST_HEAD(&sp->so_states);
  251. INIT_LIST_HEAD(&sp->so_delegations);
  252. rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
  253. sp->so_seqid.sequence = &sp->so_sequence;
  254. spin_lock_init(&sp->so_sequence.lock);
  255. INIT_LIST_HEAD(&sp->so_sequence.list);
  256. atomic_set(&sp->so_count, 1);
  257. return sp;
  258. }
  259. static void
  260. nfs4_drop_state_owner(struct nfs4_state_owner *sp)
  261. {
  262. if (!RB_EMPTY_NODE(&sp->so_client_node)) {
  263. struct nfs_client *clp = sp->so_client;
  264. spin_lock(&clp->cl_lock);
  265. rb_erase(&sp->so_client_node, &clp->cl_state_owners);
  266. RB_CLEAR_NODE(&sp->so_client_node);
  267. spin_unlock(&clp->cl_lock);
  268. }
  269. }
  270. struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred)
  271. {
  272. struct nfs_client *clp = server->nfs_client;
  273. struct nfs4_state_owner *sp, *new;
  274. spin_lock(&clp->cl_lock);
  275. sp = nfs4_find_state_owner(server, cred);
  276. spin_unlock(&clp->cl_lock);
  277. if (sp != NULL)
  278. return sp;
  279. new = nfs4_alloc_state_owner();
  280. if (new == NULL)
  281. return NULL;
  282. new->so_client = clp;
  283. new->so_server = server;
  284. new->so_cred = cred;
  285. spin_lock(&clp->cl_lock);
  286. sp = nfs4_insert_state_owner(clp, new);
  287. spin_unlock(&clp->cl_lock);
  288. if (sp == new)
  289. get_rpccred(cred);
  290. else {
  291. rpc_destroy_wait_queue(&new->so_sequence.wait);
  292. kfree(new);
  293. }
  294. return sp;
  295. }
  296. void nfs4_put_state_owner(struct nfs4_state_owner *sp)
  297. {
  298. struct nfs_client *clp = sp->so_client;
  299. struct rpc_cred *cred = sp->so_cred;
  300. if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
  301. return;
  302. nfs4_remove_state_owner(clp, sp);
  303. spin_unlock(&clp->cl_lock);
  304. rpc_destroy_wait_queue(&sp->so_sequence.wait);
  305. put_rpccred(cred);
  306. kfree(sp);
  307. }
  308. static struct nfs4_state *
  309. nfs4_alloc_open_state(void)
  310. {
  311. struct nfs4_state *state;
  312. state = kzalloc(sizeof(*state), GFP_KERNEL);
  313. if (!state)
  314. return NULL;
  315. atomic_set(&state->count, 1);
  316. INIT_LIST_HEAD(&state->lock_states);
  317. spin_lock_init(&state->state_lock);
  318. seqlock_init(&state->seqlock);
  319. return state;
  320. }
  321. void
  322. nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
  323. {
  324. if (state->state == fmode)
  325. return;
  326. /* NB! List reordering - see the reclaim code for why. */
  327. if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
  328. if (fmode & FMODE_WRITE)
  329. list_move(&state->open_states, &state->owner->so_states);
  330. else
  331. list_move_tail(&state->open_states, &state->owner->so_states);
  332. }
  333. state->state = fmode;
  334. }
  335. static struct nfs4_state *
  336. __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
  337. {
  338. struct nfs_inode *nfsi = NFS_I(inode);
  339. struct nfs4_state *state;
  340. list_for_each_entry(state, &nfsi->open_states, inode_states) {
  341. if (state->owner != owner)
  342. continue;
  343. if (atomic_inc_not_zero(&state->count))
  344. return state;
  345. }
  346. return NULL;
  347. }
  348. static void
  349. nfs4_free_open_state(struct nfs4_state *state)
  350. {
  351. kfree(state);
  352. }
  353. struct nfs4_state *
  354. nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
  355. {
  356. struct nfs4_state *state, *new;
  357. struct nfs_inode *nfsi = NFS_I(inode);
  358. spin_lock(&inode->i_lock);
  359. state = __nfs4_find_state_byowner(inode, owner);
  360. spin_unlock(&inode->i_lock);
  361. if (state)
  362. goto out;
  363. new = nfs4_alloc_open_state();
  364. spin_lock(&owner->so_lock);
  365. spin_lock(&inode->i_lock);
  366. state = __nfs4_find_state_byowner(inode, owner);
  367. if (state == NULL && new != NULL) {
  368. state = new;
  369. state->owner = owner;
  370. atomic_inc(&owner->so_count);
  371. list_add(&state->inode_states, &nfsi->open_states);
  372. state->inode = igrab(inode);
  373. spin_unlock(&inode->i_lock);
  374. /* Note: The reclaim code dictates that we add stateless
  375. * and read-only stateids to the end of the list */
  376. list_add_tail(&state->open_states, &owner->so_states);
  377. spin_unlock(&owner->so_lock);
  378. } else {
  379. spin_unlock(&inode->i_lock);
  380. spin_unlock(&owner->so_lock);
  381. if (new)
  382. nfs4_free_open_state(new);
  383. }
  384. out:
  385. return state;
  386. }
  387. void nfs4_put_open_state(struct nfs4_state *state)
  388. {
  389. struct inode *inode = state->inode;
  390. struct nfs4_state_owner *owner = state->owner;
  391. if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
  392. return;
  393. spin_lock(&inode->i_lock);
  394. list_del(&state->inode_states);
  395. list_del(&state->open_states);
  396. spin_unlock(&inode->i_lock);
  397. spin_unlock(&owner->so_lock);
  398. iput(inode);
  399. nfs4_free_open_state(state);
  400. nfs4_put_state_owner(owner);
  401. }
  402. /*
  403. * Close the current file.
  404. */
  405. static void __nfs4_close(struct path *path, struct nfs4_state *state, fmode_t fmode, int wait)
  406. {
  407. struct nfs4_state_owner *owner = state->owner;
  408. int call_close = 0;
  409. fmode_t newstate;
  410. atomic_inc(&owner->so_count);
  411. /* Protect against nfs4_find_state() */
  412. spin_lock(&owner->so_lock);
  413. switch (fmode & (FMODE_READ | FMODE_WRITE)) {
  414. case FMODE_READ:
  415. state->n_rdonly--;
  416. break;
  417. case FMODE_WRITE:
  418. state->n_wronly--;
  419. break;
  420. case FMODE_READ|FMODE_WRITE:
  421. state->n_rdwr--;
  422. }
  423. newstate = FMODE_READ|FMODE_WRITE;
  424. if (state->n_rdwr == 0) {
  425. if (state->n_rdonly == 0) {
  426. newstate &= ~FMODE_READ;
  427. call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
  428. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  429. }
  430. if (state->n_wronly == 0) {
  431. newstate &= ~FMODE_WRITE;
  432. call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
  433. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  434. }
  435. if (newstate == 0)
  436. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  437. }
  438. nfs4_state_set_mode_locked(state, newstate);
  439. spin_unlock(&owner->so_lock);
  440. if (!call_close) {
  441. nfs4_put_open_state(state);
  442. nfs4_put_state_owner(owner);
  443. } else
  444. nfs4_do_close(path, state, wait);
  445. }
  446. void nfs4_close_state(struct path *path, struct nfs4_state *state, fmode_t fmode)
  447. {
  448. __nfs4_close(path, state, fmode, 0);
  449. }
  450. void nfs4_close_sync(struct path *path, struct nfs4_state *state, fmode_t fmode)
  451. {
  452. __nfs4_close(path, state, fmode, 1);
  453. }
  454. /*
  455. * Search the state->lock_states for an existing lock_owner
  456. * that is compatible with current->files
  457. */
  458. static struct nfs4_lock_state *
  459. __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
  460. {
  461. struct nfs4_lock_state *pos;
  462. list_for_each_entry(pos, &state->lock_states, ls_locks) {
  463. if (pos->ls_owner != fl_owner)
  464. continue;
  465. atomic_inc(&pos->ls_count);
  466. return pos;
  467. }
  468. return NULL;
  469. }
  470. /*
  471. * Return a compatible lock_state. If no initialized lock_state structure
  472. * exists, return an uninitialized one.
  473. *
  474. */
  475. static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
  476. {
  477. struct nfs4_lock_state *lsp;
  478. struct nfs_client *clp = state->owner->so_client;
  479. lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
  480. if (lsp == NULL)
  481. return NULL;
  482. rpc_init_wait_queue(&lsp->ls_sequence.wait, "lock_seqid_waitqueue");
  483. spin_lock_init(&lsp->ls_sequence.lock);
  484. INIT_LIST_HEAD(&lsp->ls_sequence.list);
  485. lsp->ls_seqid.sequence = &lsp->ls_sequence;
  486. atomic_set(&lsp->ls_count, 1);
  487. lsp->ls_owner = fl_owner;
  488. spin_lock(&clp->cl_lock);
  489. nfs_alloc_unique_id(&clp->cl_lockowner_id, &lsp->ls_id, 1, 64);
  490. spin_unlock(&clp->cl_lock);
  491. INIT_LIST_HEAD(&lsp->ls_locks);
  492. return lsp;
  493. }
  494. static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
  495. {
  496. struct nfs_client *clp = lsp->ls_state->owner->so_client;
  497. spin_lock(&clp->cl_lock);
  498. nfs_free_unique_id(&clp->cl_lockowner_id, &lsp->ls_id);
  499. spin_unlock(&clp->cl_lock);
  500. rpc_destroy_wait_queue(&lsp->ls_sequence.wait);
  501. kfree(lsp);
  502. }
  503. /*
  504. * Return a compatible lock_state. If no initialized lock_state structure
  505. * exists, return an uninitialized one.
  506. *
  507. */
  508. static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
  509. {
  510. struct nfs4_lock_state *lsp, *new = NULL;
  511. for(;;) {
  512. spin_lock(&state->state_lock);
  513. lsp = __nfs4_find_lock_state(state, owner);
  514. if (lsp != NULL)
  515. break;
  516. if (new != NULL) {
  517. new->ls_state = state;
  518. list_add(&new->ls_locks, &state->lock_states);
  519. set_bit(LK_STATE_IN_USE, &state->flags);
  520. lsp = new;
  521. new = NULL;
  522. break;
  523. }
  524. spin_unlock(&state->state_lock);
  525. new = nfs4_alloc_lock_state(state, owner);
  526. if (new == NULL)
  527. return NULL;
  528. }
  529. spin_unlock(&state->state_lock);
  530. if (new != NULL)
  531. nfs4_free_lock_state(new);
  532. return lsp;
  533. }
  534. /*
  535. * Release reference to lock_state, and free it if we see that
  536. * it is no longer in use
  537. */
  538. void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
  539. {
  540. struct nfs4_state *state;
  541. if (lsp == NULL)
  542. return;
  543. state = lsp->ls_state;
  544. if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
  545. return;
  546. list_del(&lsp->ls_locks);
  547. if (list_empty(&state->lock_states))
  548. clear_bit(LK_STATE_IN_USE, &state->flags);
  549. spin_unlock(&state->state_lock);
  550. nfs4_free_lock_state(lsp);
  551. }
  552. static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
  553. {
  554. struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
  555. dst->fl_u.nfs4_fl.owner = lsp;
  556. atomic_inc(&lsp->ls_count);
  557. }
  558. static void nfs4_fl_release_lock(struct file_lock *fl)
  559. {
  560. nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
  561. }
  562. static struct file_lock_operations nfs4_fl_lock_ops = {
  563. .fl_copy_lock = nfs4_fl_copy_lock,
  564. .fl_release_private = nfs4_fl_release_lock,
  565. };
  566. int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
  567. {
  568. struct nfs4_lock_state *lsp;
  569. if (fl->fl_ops != NULL)
  570. return 0;
  571. lsp = nfs4_get_lock_state(state, fl->fl_owner);
  572. if (lsp == NULL)
  573. return -ENOMEM;
  574. fl->fl_u.nfs4_fl.owner = lsp;
  575. fl->fl_ops = &nfs4_fl_lock_ops;
  576. return 0;
  577. }
  578. /*
  579. * Byte-range lock aware utility to initialize the stateid of read/write
  580. * requests.
  581. */
  582. void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
  583. {
  584. struct nfs4_lock_state *lsp;
  585. int seq;
  586. do {
  587. seq = read_seqbegin(&state->seqlock);
  588. memcpy(dst, &state->stateid, sizeof(*dst));
  589. } while (read_seqretry(&state->seqlock, seq));
  590. if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
  591. return;
  592. spin_lock(&state->state_lock);
  593. lsp = __nfs4_find_lock_state(state, fl_owner);
  594. if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
  595. memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
  596. spin_unlock(&state->state_lock);
  597. nfs4_put_lock_state(lsp);
  598. }
  599. struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
  600. {
  601. struct nfs_seqid *new;
  602. new = kmalloc(sizeof(*new), GFP_KERNEL);
  603. if (new != NULL) {
  604. new->sequence = counter;
  605. INIT_LIST_HEAD(&new->list);
  606. }
  607. return new;
  608. }
  609. void nfs_free_seqid(struct nfs_seqid *seqid)
  610. {
  611. if (!list_empty(&seqid->list)) {
  612. struct rpc_sequence *sequence = seqid->sequence->sequence;
  613. spin_lock(&sequence->lock);
  614. list_del(&seqid->list);
  615. spin_unlock(&sequence->lock);
  616. rpc_wake_up(&sequence->wait);
  617. }
  618. kfree(seqid);
  619. }
  620. /*
  621. * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
  622. * failed with a seqid incrementing error -
  623. * see comments nfs_fs.h:seqid_mutating_error()
  624. */
  625. static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
  626. {
  627. BUG_ON(list_first_entry(&seqid->sequence->sequence->list, struct nfs_seqid, list) != seqid);
  628. switch (status) {
  629. case 0:
  630. break;
  631. case -NFS4ERR_BAD_SEQID:
  632. if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
  633. return;
  634. printk(KERN_WARNING "NFS: v4 server returned a bad"
  635. " sequence-id error on an"
  636. " unconfirmed sequence %p!\n",
  637. seqid->sequence);
  638. case -NFS4ERR_STALE_CLIENTID:
  639. case -NFS4ERR_STALE_STATEID:
  640. case -NFS4ERR_BAD_STATEID:
  641. case -NFS4ERR_BADXDR:
  642. case -NFS4ERR_RESOURCE:
  643. case -NFS4ERR_NOFILEHANDLE:
  644. /* Non-seqid mutating errors */
  645. return;
  646. };
  647. /*
  648. * Note: no locking needed as we are guaranteed to be first
  649. * on the sequence list
  650. */
  651. seqid->sequence->counter++;
  652. }
  653. void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
  654. {
  655. if (status == -NFS4ERR_BAD_SEQID) {
  656. struct nfs4_state_owner *sp = container_of(seqid->sequence,
  657. struct nfs4_state_owner, so_seqid);
  658. nfs4_drop_state_owner(sp);
  659. }
  660. nfs_increment_seqid(status, seqid);
  661. }
  662. /*
  663. * Increment the seqid if the LOCK/LOCKU succeeded, or
  664. * failed with a seqid incrementing error -
  665. * see comments nfs_fs.h:seqid_mutating_error()
  666. */
  667. void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
  668. {
  669. nfs_increment_seqid(status, seqid);
  670. }
  671. int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
  672. {
  673. struct rpc_sequence *sequence = seqid->sequence->sequence;
  674. int status = 0;
  675. spin_lock(&sequence->lock);
  676. if (list_empty(&seqid->list))
  677. list_add_tail(&seqid->list, &sequence->list);
  678. if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
  679. goto unlock;
  680. rpc_sleep_on(&sequence->wait, task, NULL);
  681. status = -EAGAIN;
  682. unlock:
  683. spin_unlock(&sequence->lock);
  684. return status;
  685. }
  686. static int nfs4_run_state_manager(void *);
  687. static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
  688. {
  689. smp_mb__before_clear_bit();
  690. clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
  691. smp_mb__after_clear_bit();
  692. wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
  693. rpc_wake_up(&clp->cl_rpcwaitq);
  694. }
  695. /*
  696. * Schedule the nfs_client asynchronous state management routine
  697. */
  698. void nfs4_schedule_state_manager(struct nfs_client *clp)
  699. {
  700. struct task_struct *task;
  701. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  702. return;
  703. __module_get(THIS_MODULE);
  704. atomic_inc(&clp->cl_count);
  705. task = kthread_run(nfs4_run_state_manager, clp, "%s-manager",
  706. rpc_peeraddr2str(clp->cl_rpcclient,
  707. RPC_DISPLAY_ADDR));
  708. if (!IS_ERR(task))
  709. return;
  710. nfs4_clear_state_manager_bit(clp);
  711. nfs_put_client(clp);
  712. module_put(THIS_MODULE);
  713. }
  714. /*
  715. * Schedule a state recovery attempt
  716. */
  717. void nfs4_schedule_state_recovery(struct nfs_client *clp)
  718. {
  719. if (!clp)
  720. return;
  721. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  722. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  723. nfs4_schedule_state_manager(clp);
  724. }
  725. static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
  726. {
  727. set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  728. /* Don't recover state that expired before the reboot */
  729. if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
  730. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  731. return 0;
  732. }
  733. set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
  734. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  735. return 1;
  736. }
  737. int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
  738. {
  739. set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
  740. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  741. set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
  742. set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
  743. return 1;
  744. }
  745. static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
  746. {
  747. struct inode *inode = state->inode;
  748. struct nfs_inode *nfsi = NFS_I(inode);
  749. struct file_lock *fl;
  750. int status = 0;
  751. down_write(&nfsi->rwsem);
  752. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  753. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  754. continue;
  755. if (nfs_file_open_context(fl->fl_file)->state != state)
  756. continue;
  757. status = ops->recover_lock(state, fl);
  758. if (status >= 0)
  759. continue;
  760. switch (status) {
  761. default:
  762. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  763. __func__, status);
  764. case -NFS4ERR_EXPIRED:
  765. case -NFS4ERR_NO_GRACE:
  766. case -NFS4ERR_RECLAIM_BAD:
  767. case -NFS4ERR_RECLAIM_CONFLICT:
  768. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  769. break;
  770. case -NFS4ERR_STALE_CLIENTID:
  771. goto out_err;
  772. }
  773. }
  774. up_write(&nfsi->rwsem);
  775. return 0;
  776. out_err:
  777. up_write(&nfsi->rwsem);
  778. return status;
  779. }
  780. static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
  781. {
  782. struct nfs4_state *state;
  783. struct nfs4_lock_state *lock;
  784. int status = 0;
  785. /* Note: we rely on the sp->so_states list being ordered
  786. * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
  787. * states first.
  788. * This is needed to ensure that the server won't give us any
  789. * read delegations that we have to return if, say, we are
  790. * recovering after a network partition or a reboot from a
  791. * server that doesn't support a grace period.
  792. */
  793. restart:
  794. spin_lock(&sp->so_lock);
  795. list_for_each_entry(state, &sp->so_states, open_states) {
  796. if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
  797. continue;
  798. if (state->state == 0)
  799. continue;
  800. atomic_inc(&state->count);
  801. spin_unlock(&sp->so_lock);
  802. status = ops->recover_open(sp, state);
  803. if (status >= 0) {
  804. status = nfs4_reclaim_locks(state, ops);
  805. if (status >= 0) {
  806. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  807. if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
  808. printk("%s: Lock reclaim failed!\n",
  809. __func__);
  810. }
  811. nfs4_put_open_state(state);
  812. goto restart;
  813. }
  814. }
  815. switch (status) {
  816. default:
  817. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  818. __func__, status);
  819. case -ENOENT:
  820. case -ESTALE:
  821. /*
  822. * Open state on this file cannot be recovered
  823. * All we can do is revert to using the zero stateid.
  824. */
  825. memset(state->stateid.data, 0,
  826. sizeof(state->stateid.data));
  827. /* Mark the file as being 'closed' */
  828. state->state = 0;
  829. break;
  830. case -NFS4ERR_RECLAIM_BAD:
  831. case -NFS4ERR_RECLAIM_CONFLICT:
  832. nfs4_state_mark_reclaim_nograce(sp->so_client, state);
  833. break;
  834. case -NFS4ERR_EXPIRED:
  835. case -NFS4ERR_NO_GRACE:
  836. nfs4_state_mark_reclaim_nograce(sp->so_client, state);
  837. case -NFS4ERR_STALE_CLIENTID:
  838. goto out_err;
  839. }
  840. nfs4_put_open_state(state);
  841. goto restart;
  842. }
  843. spin_unlock(&sp->so_lock);
  844. return 0;
  845. out_err:
  846. nfs4_put_open_state(state);
  847. return status;
  848. }
  849. static void nfs4_clear_open_state(struct nfs4_state *state)
  850. {
  851. struct nfs4_lock_state *lock;
  852. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  853. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  854. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  855. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  856. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  857. lock->ls_seqid.flags = 0;
  858. lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
  859. }
  860. }
  861. static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp, int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  862. {
  863. struct nfs4_state_owner *sp;
  864. struct rb_node *pos;
  865. struct nfs4_state *state;
  866. /* Reset all sequence ids to zero */
  867. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  868. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  869. sp->so_seqid.flags = 0;
  870. spin_lock(&sp->so_lock);
  871. list_for_each_entry(state, &sp->so_states, open_states) {
  872. if (mark_reclaim(clp, state))
  873. nfs4_clear_open_state(state);
  874. }
  875. spin_unlock(&sp->so_lock);
  876. }
  877. }
  878. static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
  879. {
  880. /* Mark all delegations for reclaim */
  881. nfs_delegation_mark_reclaim(clp);
  882. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
  883. }
  884. static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
  885. {
  886. struct nfs4_state_owner *sp;
  887. struct rb_node *pos;
  888. struct nfs4_state *state;
  889. if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  890. return;
  891. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  892. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  893. spin_lock(&sp->so_lock);
  894. list_for_each_entry(state, &sp->so_states, open_states) {
  895. if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags))
  896. continue;
  897. nfs4_state_mark_reclaim_nograce(clp, state);
  898. }
  899. spin_unlock(&sp->so_lock);
  900. }
  901. nfs_delegation_reap_unclaimed(clp);
  902. }
  903. static void nfs_delegation_clear_all(struct nfs_client *clp)
  904. {
  905. nfs_delegation_mark_reclaim(clp);
  906. nfs_delegation_reap_unclaimed(clp);
  907. }
  908. static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
  909. {
  910. nfs_delegation_clear_all(clp);
  911. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
  912. }
  913. static void nfs4_state_end_reclaim_nograce(struct nfs_client *clp)
  914. {
  915. clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
  916. }
  917. static void nfs4_recovery_handle_error(struct nfs_client *clp, int error)
  918. {
  919. switch (error) {
  920. case -NFS4ERR_CB_PATH_DOWN:
  921. nfs_handle_cb_pathdown(clp);
  922. break;
  923. case -NFS4ERR_STALE_CLIENTID:
  924. case -NFS4ERR_LEASE_MOVED:
  925. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  926. nfs4_state_start_reclaim_reboot(clp);
  927. break;
  928. case -NFS4ERR_EXPIRED:
  929. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  930. nfs4_state_start_reclaim_nograce(clp);
  931. case -NFS4ERR_BADSESSION:
  932. case -NFS4ERR_BADSLOT:
  933. case -NFS4ERR_BAD_HIGH_SLOT:
  934. case -NFS4ERR_DEADSESSION:
  935. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  936. case -NFS4ERR_SEQ_FALSE_RETRY:
  937. case -NFS4ERR_SEQ_MISORDERED:
  938. set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
  939. }
  940. }
  941. static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
  942. {
  943. struct rb_node *pos;
  944. int status = 0;
  945. restart:
  946. spin_lock(&clp->cl_lock);
  947. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  948. struct nfs4_state_owner *sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  949. if (!test_and_clear_bit(ops->owner_flag_bit, &sp->so_flags))
  950. continue;
  951. atomic_inc(&sp->so_count);
  952. spin_unlock(&clp->cl_lock);
  953. status = nfs4_reclaim_open_state(sp, ops);
  954. if (status < 0) {
  955. set_bit(ops->owner_flag_bit, &sp->so_flags);
  956. nfs4_put_state_owner(sp);
  957. nfs4_recovery_handle_error(clp, status);
  958. return status;
  959. }
  960. nfs4_put_state_owner(sp);
  961. goto restart;
  962. }
  963. spin_unlock(&clp->cl_lock);
  964. return status;
  965. }
  966. static int nfs4_check_lease(struct nfs_client *clp)
  967. {
  968. struct rpc_cred *cred;
  969. struct nfs4_state_maintenance_ops *ops =
  970. nfs4_state_renewal_ops[clp->cl_minorversion];
  971. int status = -NFS4ERR_EXPIRED;
  972. /* Is the client already known to have an expired lease? */
  973. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  974. return 0;
  975. spin_lock(&clp->cl_lock);
  976. cred = ops->get_state_renewal_cred_locked(clp);
  977. spin_unlock(&clp->cl_lock);
  978. if (cred == NULL) {
  979. cred = nfs4_get_setclientid_cred(clp);
  980. if (cred == NULL)
  981. goto out;
  982. }
  983. status = ops->renew_lease(clp, cred);
  984. put_rpccred(cred);
  985. out:
  986. nfs4_recovery_handle_error(clp, status);
  987. return status;
  988. }
  989. static int nfs4_reclaim_lease(struct nfs_client *clp)
  990. {
  991. struct rpc_cred *cred;
  992. struct nfs4_state_recovery_ops *ops =
  993. nfs4_reboot_recovery_ops[clp->cl_minorversion];
  994. int status = -ENOENT;
  995. cred = ops->get_clid_cred(clp);
  996. if (cred != NULL) {
  997. status = ops->establish_clid(clp, cred);
  998. put_rpccred(cred);
  999. /* Handle case where the user hasn't set up machine creds */
  1000. if (status == -EACCES && cred == clp->cl_machine_cred) {
  1001. nfs4_clear_machine_cred(clp);
  1002. status = -EAGAIN;
  1003. }
  1004. if (status == -NFS4ERR_MINOR_VERS_MISMATCH)
  1005. status = -EPROTONOSUPPORT;
  1006. }
  1007. return status;
  1008. }
  1009. #ifdef CONFIG_NFS_V4_1
  1010. static void nfs4_session_recovery_handle_error(struct nfs_client *clp, int err)
  1011. {
  1012. switch (err) {
  1013. case -NFS4ERR_STALE_CLIENTID:
  1014. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1015. set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
  1016. }
  1017. }
  1018. static int nfs4_reset_session(struct nfs_client *clp)
  1019. {
  1020. int status;
  1021. status = nfs4_proc_destroy_session(clp->cl_session);
  1022. if (status && status != -NFS4ERR_BADSESSION &&
  1023. status != -NFS4ERR_DEADSESSION) {
  1024. nfs4_session_recovery_handle_error(clp, status);
  1025. goto out;
  1026. }
  1027. memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
  1028. status = nfs4_proc_create_session(clp, 1);
  1029. if (status)
  1030. nfs4_session_recovery_handle_error(clp, status);
  1031. /* fall through*/
  1032. out:
  1033. /* Wake up the next rpc task even on error */
  1034. rpc_wake_up_next(&clp->cl_session->fc_slot_table.slot_tbl_waitq);
  1035. return status;
  1036. }
  1037. static int nfs4_initialize_session(struct nfs_client *clp)
  1038. {
  1039. int status;
  1040. status = nfs4_proc_create_session(clp, 0);
  1041. if (!status) {
  1042. nfs_mark_client_ready(clp, NFS_CS_READY);
  1043. } else if (status == -NFS4ERR_STALE_CLIENTID) {
  1044. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1045. set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
  1046. } else {
  1047. nfs_mark_client_ready(clp, status);
  1048. }
  1049. return status;
  1050. }
  1051. #else /* CONFIG_NFS_V4_1 */
  1052. static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
  1053. static int nfs4_initialize_session(struct nfs_client *clp) { return 0; }
  1054. #endif /* CONFIG_NFS_V4_1 */
  1055. static void nfs4_state_manager(struct nfs_client *clp)
  1056. {
  1057. int status = 0;
  1058. /* Ensure exclusive access to NFSv4 state */
  1059. for(;;) {
  1060. if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
  1061. /* We're going to have to re-establish a clientid */
  1062. status = nfs4_reclaim_lease(clp);
  1063. if (status) {
  1064. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1065. if (status == -EAGAIN)
  1066. continue;
  1067. if (clp->cl_cons_state ==
  1068. NFS_CS_SESSION_INITING)
  1069. nfs_mark_client_ready(clp, status);
  1070. goto out_error;
  1071. }
  1072. clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1073. }
  1074. if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
  1075. status = nfs4_check_lease(clp);
  1076. if (status != 0)
  1077. continue;
  1078. }
  1079. /* Initialize or reset the session */
  1080. if (nfs4_has_session(clp) &&
  1081. test_and_clear_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state)) {
  1082. if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
  1083. status = nfs4_initialize_session(clp);
  1084. else
  1085. status = nfs4_reset_session(clp);
  1086. if (status) {
  1087. if (status == -NFS4ERR_STALE_CLIENTID)
  1088. continue;
  1089. goto out_error;
  1090. }
  1091. }
  1092. /* First recover reboot state... */
  1093. if (test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
  1094. status = nfs4_do_reclaim(clp,
  1095. nfs4_reboot_recovery_ops[clp->cl_minorversion]);
  1096. if (status == -NFS4ERR_STALE_CLIENTID)
  1097. continue;
  1098. if (test_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state))
  1099. continue;
  1100. nfs4_state_end_reclaim_reboot(clp);
  1101. continue;
  1102. }
  1103. /* Now recover expired state... */
  1104. if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
  1105. status = nfs4_do_reclaim(clp,
  1106. nfs4_nograce_recovery_ops[clp->cl_minorversion]);
  1107. if (status < 0) {
  1108. set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
  1109. if (status == -NFS4ERR_STALE_CLIENTID)
  1110. continue;
  1111. if (status == -NFS4ERR_EXPIRED)
  1112. continue;
  1113. if (test_bit(NFS4CLNT_SESSION_SETUP,
  1114. &clp->cl_state))
  1115. continue;
  1116. goto out_error;
  1117. } else
  1118. nfs4_state_end_reclaim_nograce(clp);
  1119. continue;
  1120. }
  1121. if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
  1122. nfs_client_return_marked_delegations(clp);
  1123. continue;
  1124. }
  1125. nfs4_clear_state_manager_bit(clp);
  1126. /* Did we race with an attempt to give us more work? */
  1127. if (clp->cl_state == 0)
  1128. break;
  1129. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  1130. break;
  1131. }
  1132. return;
  1133. out_error:
  1134. printk(KERN_WARNING "Error: state manager failed on NFSv4 server %s"
  1135. " with error %d\n", clp->cl_hostname, -status);
  1136. if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1137. nfs4_state_end_reclaim_reboot(clp);
  1138. nfs4_clear_state_manager_bit(clp);
  1139. }
  1140. static int nfs4_run_state_manager(void *ptr)
  1141. {
  1142. struct nfs_client *clp = ptr;
  1143. allow_signal(SIGKILL);
  1144. nfs4_state_manager(clp);
  1145. nfs_put_client(clp);
  1146. module_put_and_exit(0);
  1147. return 0;
  1148. }
  1149. /*
  1150. * Local variables:
  1151. * c-basic-offset: 8
  1152. * End:
  1153. */