nfs4state.c 35 KB

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