nfs4state.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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/slab.h>
  41. #include <linux/smp_lock.h>
  42. #include <linux/nfs_fs.h>
  43. #include <linux/nfs_idmap.h>
  44. #include <linux/kthread.h>
  45. #include <linux/module.h>
  46. #include <linux/random.h>
  47. #include <linux/workqueue.h>
  48. #include <linux/bitops.h>
  49. #include "nfs4_fs.h"
  50. #include "callback.h"
  51. #include "delegation.h"
  52. #include "internal.h"
  53. #define OPENOWNER_POOL_SIZE 8
  54. const nfs4_stateid zero_stateid;
  55. static LIST_HEAD(nfs4_clientid_list);
  56. static int nfs4_init_client(struct nfs_client *clp, struct rpc_cred *cred)
  57. {
  58. int status = nfs4_proc_setclientid(clp, NFS4_CALLBACK,
  59. nfs_callback_tcpport, cred);
  60. if (status == 0)
  61. status = nfs4_proc_setclientid_confirm(clp, cred);
  62. if (status == 0)
  63. nfs4_schedule_state_renewal(clp);
  64. return status;
  65. }
  66. struct rpc_cred *nfs4_get_renew_cred(struct nfs_client *clp)
  67. {
  68. struct nfs4_state_owner *sp;
  69. struct rb_node *pos;
  70. struct rpc_cred *cred = NULL;
  71. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  72. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  73. if (list_empty(&sp->so_states))
  74. continue;
  75. cred = get_rpccred(sp->so_cred);
  76. break;
  77. }
  78. return cred;
  79. }
  80. static struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
  81. {
  82. struct nfs4_state_owner *sp;
  83. struct rb_node *pos;
  84. pos = rb_first(&clp->cl_state_owners);
  85. if (pos != NULL) {
  86. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  87. return get_rpccred(sp->so_cred);
  88. }
  89. return NULL;
  90. }
  91. static void nfs_alloc_unique_id(struct rb_root *root, struct nfs_unique_id *new,
  92. __u64 minval, int maxbits)
  93. {
  94. struct rb_node **p, *parent;
  95. struct nfs_unique_id *pos;
  96. __u64 mask = ~0ULL;
  97. if (maxbits < 64)
  98. mask = (1ULL << maxbits) - 1ULL;
  99. /* Ensure distribution is more or less flat */
  100. get_random_bytes(&new->id, sizeof(new->id));
  101. new->id &= mask;
  102. if (new->id < minval)
  103. new->id += minval;
  104. retry:
  105. p = &root->rb_node;
  106. parent = NULL;
  107. while (*p != NULL) {
  108. parent = *p;
  109. pos = rb_entry(parent, struct nfs_unique_id, rb_node);
  110. if (new->id < pos->id)
  111. p = &(*p)->rb_left;
  112. else if (new->id > pos->id)
  113. p = &(*p)->rb_right;
  114. else
  115. goto id_exists;
  116. }
  117. rb_link_node(&new->rb_node, parent, p);
  118. rb_insert_color(&new->rb_node, root);
  119. return;
  120. id_exists:
  121. for (;;) {
  122. new->id++;
  123. if (new->id < minval || (new->id & mask) != new->id) {
  124. new->id = minval;
  125. break;
  126. }
  127. parent = rb_next(parent);
  128. if (parent == NULL)
  129. break;
  130. pos = rb_entry(parent, struct nfs_unique_id, rb_node);
  131. if (new->id < pos->id)
  132. break;
  133. }
  134. goto retry;
  135. }
  136. static void nfs_free_unique_id(struct rb_root *root, struct nfs_unique_id *id)
  137. {
  138. rb_erase(&id->rb_node, root);
  139. }
  140. static struct nfs4_state_owner *
  141. nfs4_find_state_owner(struct nfs_client *clp, struct rpc_cred *cred)
  142. {
  143. struct rb_node **p = &clp->cl_state_owners.rb_node,
  144. *parent = NULL;
  145. struct nfs4_state_owner *sp, *res = NULL;
  146. while (*p != NULL) {
  147. parent = *p;
  148. sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
  149. if (cred < sp->so_cred)
  150. p = &parent->rb_left;
  151. else if (cred > sp->so_cred)
  152. p = &parent->rb_right;
  153. else {
  154. atomic_inc(&sp->so_count);
  155. res = sp;
  156. break;
  157. }
  158. }
  159. return res;
  160. }
  161. static struct nfs4_state_owner *
  162. nfs4_insert_state_owner(struct nfs_client *clp, struct nfs4_state_owner *new)
  163. {
  164. struct rb_node **p = &clp->cl_state_owners.rb_node,
  165. *parent = NULL;
  166. struct nfs4_state_owner *sp;
  167. while (*p != NULL) {
  168. parent = *p;
  169. sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
  170. if (new->so_cred < sp->so_cred)
  171. p = &parent->rb_left;
  172. else if (new->so_cred > sp->so_cred)
  173. p = &parent->rb_right;
  174. else {
  175. atomic_inc(&sp->so_count);
  176. return sp;
  177. }
  178. }
  179. nfs_alloc_unique_id(&clp->cl_openowner_id, &new->so_owner_id, 1, 64);
  180. rb_link_node(&new->so_client_node, parent, p);
  181. rb_insert_color(&new->so_client_node, &clp->cl_state_owners);
  182. return new;
  183. }
  184. static void
  185. nfs4_remove_state_owner(struct nfs_client *clp, struct nfs4_state_owner *sp)
  186. {
  187. if (!RB_EMPTY_NODE(&sp->so_client_node))
  188. rb_erase(&sp->so_client_node, &clp->cl_state_owners);
  189. nfs_free_unique_id(&clp->cl_openowner_id, &sp->so_owner_id);
  190. }
  191. /*
  192. * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
  193. * create a new state_owner.
  194. *
  195. */
  196. static struct nfs4_state_owner *
  197. nfs4_alloc_state_owner(void)
  198. {
  199. struct nfs4_state_owner *sp;
  200. sp = kzalloc(sizeof(*sp),GFP_KERNEL);
  201. if (!sp)
  202. return NULL;
  203. spin_lock_init(&sp->so_lock);
  204. INIT_LIST_HEAD(&sp->so_states);
  205. INIT_LIST_HEAD(&sp->so_delegations);
  206. rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
  207. sp->so_seqid.sequence = &sp->so_sequence;
  208. spin_lock_init(&sp->so_sequence.lock);
  209. INIT_LIST_HEAD(&sp->so_sequence.list);
  210. atomic_set(&sp->so_count, 1);
  211. return sp;
  212. }
  213. void
  214. nfs4_drop_state_owner(struct nfs4_state_owner *sp)
  215. {
  216. if (!RB_EMPTY_NODE(&sp->so_client_node)) {
  217. struct nfs_client *clp = sp->so_client;
  218. spin_lock(&clp->cl_lock);
  219. rb_erase(&sp->so_client_node, &clp->cl_state_owners);
  220. RB_CLEAR_NODE(&sp->so_client_node);
  221. spin_unlock(&clp->cl_lock);
  222. }
  223. }
  224. /*
  225. * Note: must be called with clp->cl_sem held in order to prevent races
  226. * with reboot recovery!
  227. */
  228. struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred)
  229. {
  230. struct nfs_client *clp = server->nfs_client;
  231. struct nfs4_state_owner *sp, *new;
  232. spin_lock(&clp->cl_lock);
  233. sp = nfs4_find_state_owner(clp, cred);
  234. spin_unlock(&clp->cl_lock);
  235. if (sp != NULL)
  236. return sp;
  237. new = nfs4_alloc_state_owner();
  238. if (new == NULL)
  239. return NULL;
  240. new->so_client = clp;
  241. new->so_cred = cred;
  242. spin_lock(&clp->cl_lock);
  243. sp = nfs4_insert_state_owner(clp, new);
  244. spin_unlock(&clp->cl_lock);
  245. if (sp == new)
  246. get_rpccred(cred);
  247. else
  248. kfree(new);
  249. return sp;
  250. }
  251. /*
  252. * Must be called with clp->cl_sem held in order to avoid races
  253. * with state recovery...
  254. */
  255. void nfs4_put_state_owner(struct nfs4_state_owner *sp)
  256. {
  257. struct nfs_client *clp = sp->so_client;
  258. struct rpc_cred *cred = sp->so_cred;
  259. if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
  260. return;
  261. nfs4_remove_state_owner(clp, sp);
  262. spin_unlock(&clp->cl_lock);
  263. put_rpccred(cred);
  264. kfree(sp);
  265. }
  266. static struct nfs4_state *
  267. nfs4_alloc_open_state(void)
  268. {
  269. struct nfs4_state *state;
  270. state = kzalloc(sizeof(*state), GFP_KERNEL);
  271. if (!state)
  272. return NULL;
  273. atomic_set(&state->count, 1);
  274. INIT_LIST_HEAD(&state->lock_states);
  275. spin_lock_init(&state->state_lock);
  276. return state;
  277. }
  278. void
  279. nfs4_state_set_mode_locked(struct nfs4_state *state, mode_t mode)
  280. {
  281. if (state->state == mode)
  282. return;
  283. /* NB! List reordering - see the reclaim code for why. */
  284. if ((mode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
  285. if (mode & FMODE_WRITE)
  286. list_move(&state->open_states, &state->owner->so_states);
  287. else
  288. list_move_tail(&state->open_states, &state->owner->so_states);
  289. }
  290. if (mode == 0)
  291. list_del_init(&state->inode_states);
  292. state->state = mode;
  293. }
  294. static struct nfs4_state *
  295. __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
  296. {
  297. struct nfs_inode *nfsi = NFS_I(inode);
  298. struct nfs4_state *state;
  299. list_for_each_entry(state, &nfsi->open_states, inode_states) {
  300. if (state->owner != owner)
  301. continue;
  302. if (atomic_inc_not_zero(&state->count))
  303. return state;
  304. }
  305. return NULL;
  306. }
  307. static void
  308. nfs4_free_open_state(struct nfs4_state *state)
  309. {
  310. kfree(state);
  311. }
  312. struct nfs4_state *
  313. nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
  314. {
  315. struct nfs4_state *state, *new;
  316. struct nfs_inode *nfsi = NFS_I(inode);
  317. spin_lock(&inode->i_lock);
  318. state = __nfs4_find_state_byowner(inode, owner);
  319. spin_unlock(&inode->i_lock);
  320. if (state)
  321. goto out;
  322. new = nfs4_alloc_open_state();
  323. spin_lock(&owner->so_lock);
  324. spin_lock(&inode->i_lock);
  325. state = __nfs4_find_state_byowner(inode, owner);
  326. if (state == NULL && new != NULL) {
  327. state = new;
  328. state->owner = owner;
  329. atomic_inc(&owner->so_count);
  330. list_add(&state->inode_states, &nfsi->open_states);
  331. state->inode = igrab(inode);
  332. spin_unlock(&inode->i_lock);
  333. /* Note: The reclaim code dictates that we add stateless
  334. * and read-only stateids to the end of the list */
  335. list_add_tail(&state->open_states, &owner->so_states);
  336. spin_unlock(&owner->so_lock);
  337. } else {
  338. spin_unlock(&inode->i_lock);
  339. spin_unlock(&owner->so_lock);
  340. if (new)
  341. nfs4_free_open_state(new);
  342. }
  343. out:
  344. return state;
  345. }
  346. /*
  347. * Beware! Caller must be holding exactly one
  348. * reference to clp->cl_sem!
  349. */
  350. void nfs4_put_open_state(struct nfs4_state *state)
  351. {
  352. struct inode *inode = state->inode;
  353. struct nfs4_state_owner *owner = state->owner;
  354. if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
  355. return;
  356. spin_lock(&inode->i_lock);
  357. if (!list_empty(&state->inode_states))
  358. list_del(&state->inode_states);
  359. list_del(&state->open_states);
  360. spin_unlock(&inode->i_lock);
  361. spin_unlock(&owner->so_lock);
  362. iput(inode);
  363. nfs4_free_open_state(state);
  364. nfs4_put_state_owner(owner);
  365. }
  366. /*
  367. * Close the current file.
  368. */
  369. void nfs4_close_state(struct path *path, struct nfs4_state *state, mode_t mode)
  370. {
  371. struct inode *inode = state->inode;
  372. struct nfs4_state_owner *owner = state->owner;
  373. int call_close = 0;
  374. int newstate;
  375. atomic_inc(&owner->so_count);
  376. /* Protect against nfs4_find_state() */
  377. spin_lock(&owner->so_lock);
  378. spin_lock(&inode->i_lock);
  379. switch (mode & (FMODE_READ | FMODE_WRITE)) {
  380. case FMODE_READ:
  381. state->n_rdonly--;
  382. break;
  383. case FMODE_WRITE:
  384. state->n_wronly--;
  385. break;
  386. case FMODE_READ|FMODE_WRITE:
  387. state->n_rdwr--;
  388. }
  389. newstate = FMODE_READ|FMODE_WRITE;
  390. if (state->n_rdwr == 0) {
  391. if (state->n_rdonly == 0) {
  392. newstate &= ~FMODE_READ;
  393. call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
  394. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  395. }
  396. if (state->n_wronly == 0) {
  397. newstate &= ~FMODE_WRITE;
  398. call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
  399. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  400. }
  401. if (newstate == 0)
  402. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  403. }
  404. nfs4_state_set_mode_locked(state, newstate);
  405. spin_unlock(&inode->i_lock);
  406. spin_unlock(&owner->so_lock);
  407. if (!call_close) {
  408. nfs4_put_open_state(state);
  409. nfs4_put_state_owner(owner);
  410. } else
  411. nfs4_do_close(path, state);
  412. }
  413. /*
  414. * Search the state->lock_states for an existing lock_owner
  415. * that is compatible with current->files
  416. */
  417. static struct nfs4_lock_state *
  418. __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
  419. {
  420. struct nfs4_lock_state *pos;
  421. list_for_each_entry(pos, &state->lock_states, ls_locks) {
  422. if (pos->ls_owner != fl_owner)
  423. continue;
  424. atomic_inc(&pos->ls_count);
  425. return pos;
  426. }
  427. return NULL;
  428. }
  429. /*
  430. * Return a compatible lock_state. If no initialized lock_state structure
  431. * exists, return an uninitialized one.
  432. *
  433. */
  434. static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
  435. {
  436. struct nfs4_lock_state *lsp;
  437. struct nfs_client *clp = state->owner->so_client;
  438. lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
  439. if (lsp == NULL)
  440. return NULL;
  441. lsp->ls_seqid.sequence = &state->owner->so_sequence;
  442. atomic_set(&lsp->ls_count, 1);
  443. lsp->ls_owner = fl_owner;
  444. spin_lock(&clp->cl_lock);
  445. nfs_alloc_unique_id(&clp->cl_lockowner_id, &lsp->ls_id, 1, 64);
  446. spin_unlock(&clp->cl_lock);
  447. INIT_LIST_HEAD(&lsp->ls_locks);
  448. return lsp;
  449. }
  450. static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
  451. {
  452. struct nfs_client *clp = lsp->ls_state->owner->so_client;
  453. spin_lock(&clp->cl_lock);
  454. nfs_free_unique_id(&clp->cl_lockowner_id, &lsp->ls_id);
  455. spin_unlock(&clp->cl_lock);
  456. kfree(lsp);
  457. }
  458. /*
  459. * Return a compatible lock_state. If no initialized lock_state structure
  460. * exists, return an uninitialized one.
  461. *
  462. * The caller must be holding clp->cl_sem
  463. */
  464. static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
  465. {
  466. struct nfs4_lock_state *lsp, *new = NULL;
  467. for(;;) {
  468. spin_lock(&state->state_lock);
  469. lsp = __nfs4_find_lock_state(state, owner);
  470. if (lsp != NULL)
  471. break;
  472. if (new != NULL) {
  473. new->ls_state = state;
  474. list_add(&new->ls_locks, &state->lock_states);
  475. set_bit(LK_STATE_IN_USE, &state->flags);
  476. lsp = new;
  477. new = NULL;
  478. break;
  479. }
  480. spin_unlock(&state->state_lock);
  481. new = nfs4_alloc_lock_state(state, owner);
  482. if (new == NULL)
  483. return NULL;
  484. }
  485. spin_unlock(&state->state_lock);
  486. if (new != NULL)
  487. nfs4_free_lock_state(new);
  488. return lsp;
  489. }
  490. /*
  491. * Release reference to lock_state, and free it if we see that
  492. * it is no longer in use
  493. */
  494. void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
  495. {
  496. struct nfs4_state *state;
  497. if (lsp == NULL)
  498. return;
  499. state = lsp->ls_state;
  500. if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
  501. return;
  502. list_del(&lsp->ls_locks);
  503. if (list_empty(&state->lock_states))
  504. clear_bit(LK_STATE_IN_USE, &state->flags);
  505. spin_unlock(&state->state_lock);
  506. nfs4_free_lock_state(lsp);
  507. }
  508. static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
  509. {
  510. struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
  511. dst->fl_u.nfs4_fl.owner = lsp;
  512. atomic_inc(&lsp->ls_count);
  513. }
  514. static void nfs4_fl_release_lock(struct file_lock *fl)
  515. {
  516. nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
  517. }
  518. static struct file_lock_operations nfs4_fl_lock_ops = {
  519. .fl_copy_lock = nfs4_fl_copy_lock,
  520. .fl_release_private = nfs4_fl_release_lock,
  521. };
  522. int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
  523. {
  524. struct nfs4_lock_state *lsp;
  525. if (fl->fl_ops != NULL)
  526. return 0;
  527. lsp = nfs4_get_lock_state(state, fl->fl_owner);
  528. if (lsp == NULL)
  529. return -ENOMEM;
  530. fl->fl_u.nfs4_fl.owner = lsp;
  531. fl->fl_ops = &nfs4_fl_lock_ops;
  532. return 0;
  533. }
  534. /*
  535. * Byte-range lock aware utility to initialize the stateid of read/write
  536. * requests.
  537. */
  538. void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
  539. {
  540. struct nfs4_lock_state *lsp;
  541. memcpy(dst, &state->stateid, sizeof(*dst));
  542. if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
  543. return;
  544. spin_lock(&state->state_lock);
  545. lsp = __nfs4_find_lock_state(state, fl_owner);
  546. if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
  547. memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
  548. spin_unlock(&state->state_lock);
  549. nfs4_put_lock_state(lsp);
  550. }
  551. struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
  552. {
  553. struct rpc_sequence *sequence = counter->sequence;
  554. struct nfs_seqid *new;
  555. new = kmalloc(sizeof(*new), GFP_KERNEL);
  556. if (new != NULL) {
  557. new->sequence = counter;
  558. spin_lock(&sequence->lock);
  559. list_add_tail(&new->list, &sequence->list);
  560. spin_unlock(&sequence->lock);
  561. }
  562. return new;
  563. }
  564. void nfs_free_seqid(struct nfs_seqid *seqid)
  565. {
  566. struct rpc_sequence *sequence = seqid->sequence->sequence;
  567. spin_lock(&sequence->lock);
  568. list_del(&seqid->list);
  569. spin_unlock(&sequence->lock);
  570. rpc_wake_up(&sequence->wait);
  571. kfree(seqid);
  572. }
  573. /*
  574. * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
  575. * failed with a seqid incrementing error -
  576. * see comments nfs_fs.h:seqid_mutating_error()
  577. */
  578. static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
  579. {
  580. switch (status) {
  581. case 0:
  582. break;
  583. case -NFS4ERR_BAD_SEQID:
  584. case -NFS4ERR_STALE_CLIENTID:
  585. case -NFS4ERR_STALE_STATEID:
  586. case -NFS4ERR_BAD_STATEID:
  587. case -NFS4ERR_BADXDR:
  588. case -NFS4ERR_RESOURCE:
  589. case -NFS4ERR_NOFILEHANDLE:
  590. /* Non-seqid mutating errors */
  591. return;
  592. };
  593. /*
  594. * Note: no locking needed as we are guaranteed to be first
  595. * on the sequence list
  596. */
  597. seqid->sequence->counter++;
  598. }
  599. void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
  600. {
  601. if (status == -NFS4ERR_BAD_SEQID) {
  602. struct nfs4_state_owner *sp = container_of(seqid->sequence,
  603. struct nfs4_state_owner, so_seqid);
  604. nfs4_drop_state_owner(sp);
  605. }
  606. nfs_increment_seqid(status, seqid);
  607. }
  608. /*
  609. * Increment the seqid if the LOCK/LOCKU succeeded, or
  610. * failed with a seqid incrementing error -
  611. * see comments nfs_fs.h:seqid_mutating_error()
  612. */
  613. void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
  614. {
  615. nfs_increment_seqid(status, seqid);
  616. }
  617. int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
  618. {
  619. struct rpc_sequence *sequence = seqid->sequence->sequence;
  620. int status = 0;
  621. if (sequence->list.next == &seqid->list)
  622. goto out;
  623. spin_lock(&sequence->lock);
  624. if (sequence->list.next != &seqid->list) {
  625. rpc_sleep_on(&sequence->wait, task, NULL, NULL);
  626. status = -EAGAIN;
  627. }
  628. spin_unlock(&sequence->lock);
  629. out:
  630. return status;
  631. }
  632. static int reclaimer(void *);
  633. static inline void nfs4_clear_recover_bit(struct nfs_client *clp)
  634. {
  635. smp_mb__before_clear_bit();
  636. clear_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state);
  637. smp_mb__after_clear_bit();
  638. wake_up_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER);
  639. rpc_wake_up(&clp->cl_rpcwaitq);
  640. }
  641. /*
  642. * State recovery routine
  643. */
  644. static void nfs4_recover_state(struct nfs_client *clp)
  645. {
  646. struct task_struct *task;
  647. __module_get(THIS_MODULE);
  648. atomic_inc(&clp->cl_count);
  649. task = kthread_run(reclaimer, clp, "%u.%u.%u.%u-reclaim",
  650. NIPQUAD(clp->cl_addr.sin_addr));
  651. if (!IS_ERR(task))
  652. return;
  653. nfs4_clear_recover_bit(clp);
  654. nfs_put_client(clp);
  655. module_put(THIS_MODULE);
  656. }
  657. /*
  658. * Schedule a state recovery attempt
  659. */
  660. void nfs4_schedule_state_recovery(struct nfs_client *clp)
  661. {
  662. if (!clp)
  663. return;
  664. if (test_and_set_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
  665. nfs4_recover_state(clp);
  666. }
  667. static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state)
  668. {
  669. struct inode *inode = state->inode;
  670. struct file_lock *fl;
  671. int status = 0;
  672. for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
  673. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  674. continue;
  675. if (((struct nfs_open_context *)fl->fl_file->private_data)->state != state)
  676. continue;
  677. status = ops->recover_lock(state, fl);
  678. if (status >= 0)
  679. continue;
  680. switch (status) {
  681. default:
  682. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  683. __FUNCTION__, status);
  684. case -NFS4ERR_EXPIRED:
  685. case -NFS4ERR_NO_GRACE:
  686. case -NFS4ERR_RECLAIM_BAD:
  687. case -NFS4ERR_RECLAIM_CONFLICT:
  688. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  689. break;
  690. case -NFS4ERR_STALE_CLIENTID:
  691. goto out_err;
  692. }
  693. }
  694. return 0;
  695. out_err:
  696. return status;
  697. }
  698. static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp)
  699. {
  700. struct nfs4_state *state;
  701. struct nfs4_lock_state *lock;
  702. int status = 0;
  703. /* Note: we rely on the sp->so_states list being ordered
  704. * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
  705. * states first.
  706. * This is needed to ensure that the server won't give us any
  707. * read delegations that we have to return if, say, we are
  708. * recovering after a network partition or a reboot from a
  709. * server that doesn't support a grace period.
  710. */
  711. list_for_each_entry(state, &sp->so_states, open_states) {
  712. if (state->state == 0)
  713. continue;
  714. status = ops->recover_open(sp, state);
  715. if (status >= 0) {
  716. status = nfs4_reclaim_locks(ops, state);
  717. if (status < 0)
  718. goto out_err;
  719. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  720. if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
  721. printk("%s: Lock reclaim failed!\n",
  722. __FUNCTION__);
  723. }
  724. continue;
  725. }
  726. switch (status) {
  727. default:
  728. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  729. __FUNCTION__, status);
  730. case -ENOENT:
  731. case -NFS4ERR_RECLAIM_BAD:
  732. case -NFS4ERR_RECLAIM_CONFLICT:
  733. /*
  734. * Open state on this file cannot be recovered
  735. * All we can do is revert to using the zero stateid.
  736. */
  737. memset(state->stateid.data, 0,
  738. sizeof(state->stateid.data));
  739. /* Mark the file as being 'closed' */
  740. state->state = 0;
  741. break;
  742. case -NFS4ERR_EXPIRED:
  743. case -NFS4ERR_NO_GRACE:
  744. case -NFS4ERR_STALE_CLIENTID:
  745. goto out_err;
  746. }
  747. }
  748. return 0;
  749. out_err:
  750. return status;
  751. }
  752. static void nfs4_state_mark_reclaim(struct nfs_client *clp)
  753. {
  754. struct nfs4_state_owner *sp;
  755. struct rb_node *pos;
  756. struct nfs4_state *state;
  757. struct nfs4_lock_state *lock;
  758. /* Reset all sequence ids to zero */
  759. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  760. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  761. sp->so_seqid.counter = 0;
  762. sp->so_seqid.flags = 0;
  763. spin_lock(&sp->so_lock);
  764. list_for_each_entry(state, &sp->so_states, open_states) {
  765. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  766. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  767. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  768. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  769. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  770. lock->ls_seqid.counter = 0;
  771. lock->ls_seqid.flags = 0;
  772. lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
  773. }
  774. }
  775. spin_unlock(&sp->so_lock);
  776. }
  777. }
  778. static int reclaimer(void *ptr)
  779. {
  780. struct nfs_client *clp = ptr;
  781. struct nfs4_state_owner *sp;
  782. struct rb_node *pos;
  783. struct nfs4_state_recovery_ops *ops;
  784. struct rpc_cred *cred;
  785. int status = 0;
  786. allow_signal(SIGKILL);
  787. /* Ensure exclusive access to NFSv4 state */
  788. lock_kernel();
  789. down_write(&clp->cl_sem);
  790. /* Are there any NFS mounts out there? */
  791. if (list_empty(&clp->cl_superblocks))
  792. goto out;
  793. restart_loop:
  794. ops = &nfs4_network_partition_recovery_ops;
  795. /* Are there any open files on this volume? */
  796. cred = nfs4_get_renew_cred(clp);
  797. if (cred != NULL) {
  798. /* Yes there are: try to renew the old lease */
  799. status = nfs4_proc_renew(clp, cred);
  800. switch (status) {
  801. case 0:
  802. case -NFS4ERR_CB_PATH_DOWN:
  803. put_rpccred(cred);
  804. goto out;
  805. case -NFS4ERR_STALE_CLIENTID:
  806. case -NFS4ERR_LEASE_MOVED:
  807. ops = &nfs4_reboot_recovery_ops;
  808. }
  809. } else {
  810. /* "reboot" to ensure we clear all state on the server */
  811. clp->cl_boot_time = CURRENT_TIME;
  812. cred = nfs4_get_setclientid_cred(clp);
  813. }
  814. /* We're going to have to re-establish a clientid */
  815. nfs4_state_mark_reclaim(clp);
  816. status = -ENOENT;
  817. if (cred != NULL) {
  818. status = nfs4_init_client(clp, cred);
  819. put_rpccred(cred);
  820. }
  821. if (status)
  822. goto out_error;
  823. /* Mark all delegations for reclaim */
  824. nfs_delegation_mark_reclaim(clp);
  825. /* Note: list is protected by exclusive lock on cl->cl_sem */
  826. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  827. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  828. status = nfs4_reclaim_open_state(ops, sp);
  829. if (status < 0) {
  830. if (status == -NFS4ERR_NO_GRACE) {
  831. ops = &nfs4_network_partition_recovery_ops;
  832. status = nfs4_reclaim_open_state(ops, sp);
  833. }
  834. if (status == -NFS4ERR_STALE_CLIENTID)
  835. goto restart_loop;
  836. if (status == -NFS4ERR_EXPIRED)
  837. goto restart_loop;
  838. }
  839. }
  840. nfs_delegation_reap_unclaimed(clp);
  841. out:
  842. up_write(&clp->cl_sem);
  843. unlock_kernel();
  844. if (status == -NFS4ERR_CB_PATH_DOWN)
  845. nfs_handle_cb_pathdown(clp);
  846. nfs4_clear_recover_bit(clp);
  847. nfs_put_client(clp);
  848. module_put_and_exit(0);
  849. return 0;
  850. out_error:
  851. printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %u.%u.%u.%u with error %d\n",
  852. NIPQUAD(clp->cl_addr.sin_addr), -status);
  853. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  854. goto out;
  855. }
  856. /*
  857. * Local variables:
  858. * c-basic-offset: 8
  859. * End:
  860. */