nfs4state.c 22 KB

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