nfs4state.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. 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 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 && nfs4_do_close(inode, state) == 0)
  344. return;
  345. nfs4_put_open_state(state);
  346. nfs4_put_state_owner(owner);
  347. }
  348. /*
  349. * Search the state->lock_states for an existing lock_owner
  350. * that is compatible with current->files
  351. */
  352. static struct nfs4_lock_state *
  353. __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
  354. {
  355. struct nfs4_lock_state *pos;
  356. list_for_each_entry(pos, &state->lock_states, ls_locks) {
  357. if (pos->ls_owner != fl_owner)
  358. continue;
  359. atomic_inc(&pos->ls_count);
  360. return pos;
  361. }
  362. return NULL;
  363. }
  364. /*
  365. * Return a compatible lock_state. If no initialized lock_state structure
  366. * exists, return an uninitialized one.
  367. *
  368. */
  369. static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
  370. {
  371. struct nfs4_lock_state *lsp;
  372. struct nfs_client *clp = state->owner->so_client;
  373. lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
  374. if (lsp == NULL)
  375. return NULL;
  376. lsp->ls_seqid.sequence = &state->owner->so_sequence;
  377. atomic_set(&lsp->ls_count, 1);
  378. lsp->ls_owner = fl_owner;
  379. spin_lock(&clp->cl_lock);
  380. lsp->ls_id = nfs4_alloc_lockowner_id(clp);
  381. spin_unlock(&clp->cl_lock);
  382. INIT_LIST_HEAD(&lsp->ls_locks);
  383. return lsp;
  384. }
  385. /*
  386. * Return a compatible lock_state. If no initialized lock_state structure
  387. * exists, return an uninitialized one.
  388. *
  389. * The caller must be holding clp->cl_sem
  390. */
  391. static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
  392. {
  393. struct nfs4_lock_state *lsp, *new = NULL;
  394. for(;;) {
  395. spin_lock(&state->state_lock);
  396. lsp = __nfs4_find_lock_state(state, owner);
  397. if (lsp != NULL)
  398. break;
  399. if (new != NULL) {
  400. new->ls_state = state;
  401. list_add(&new->ls_locks, &state->lock_states);
  402. set_bit(LK_STATE_IN_USE, &state->flags);
  403. lsp = new;
  404. new = NULL;
  405. break;
  406. }
  407. spin_unlock(&state->state_lock);
  408. new = nfs4_alloc_lock_state(state, owner);
  409. if (new == NULL)
  410. return NULL;
  411. }
  412. spin_unlock(&state->state_lock);
  413. kfree(new);
  414. return lsp;
  415. }
  416. /*
  417. * Release reference to lock_state, and free it if we see that
  418. * it is no longer in use
  419. */
  420. void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
  421. {
  422. struct nfs4_state *state;
  423. if (lsp == NULL)
  424. return;
  425. state = lsp->ls_state;
  426. if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
  427. return;
  428. list_del(&lsp->ls_locks);
  429. if (list_empty(&state->lock_states))
  430. clear_bit(LK_STATE_IN_USE, &state->flags);
  431. spin_unlock(&state->state_lock);
  432. kfree(lsp);
  433. }
  434. static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
  435. {
  436. struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
  437. dst->fl_u.nfs4_fl.owner = lsp;
  438. atomic_inc(&lsp->ls_count);
  439. }
  440. static void nfs4_fl_release_lock(struct file_lock *fl)
  441. {
  442. nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
  443. }
  444. static struct file_lock_operations nfs4_fl_lock_ops = {
  445. .fl_copy_lock = nfs4_fl_copy_lock,
  446. .fl_release_private = nfs4_fl_release_lock,
  447. };
  448. int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
  449. {
  450. struct nfs4_lock_state *lsp;
  451. if (fl->fl_ops != NULL)
  452. return 0;
  453. lsp = nfs4_get_lock_state(state, fl->fl_owner);
  454. if (lsp == NULL)
  455. return -ENOMEM;
  456. fl->fl_u.nfs4_fl.owner = lsp;
  457. fl->fl_ops = &nfs4_fl_lock_ops;
  458. return 0;
  459. }
  460. /*
  461. * Byte-range lock aware utility to initialize the stateid of read/write
  462. * requests.
  463. */
  464. void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
  465. {
  466. struct nfs4_lock_state *lsp;
  467. memcpy(dst, &state->stateid, sizeof(*dst));
  468. if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
  469. return;
  470. spin_lock(&state->state_lock);
  471. lsp = __nfs4_find_lock_state(state, fl_owner);
  472. if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
  473. memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
  474. spin_unlock(&state->state_lock);
  475. nfs4_put_lock_state(lsp);
  476. }
  477. struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
  478. {
  479. struct rpc_sequence *sequence = counter->sequence;
  480. struct nfs_seqid *new;
  481. new = kmalloc(sizeof(*new), GFP_KERNEL);
  482. if (new != NULL) {
  483. new->sequence = counter;
  484. spin_lock(&sequence->lock);
  485. list_add_tail(&new->list, &sequence->list);
  486. spin_unlock(&sequence->lock);
  487. }
  488. return new;
  489. }
  490. void nfs_free_seqid(struct nfs_seqid *seqid)
  491. {
  492. struct rpc_sequence *sequence = seqid->sequence->sequence;
  493. spin_lock(&sequence->lock);
  494. list_del(&seqid->list);
  495. spin_unlock(&sequence->lock);
  496. rpc_wake_up(&sequence->wait);
  497. kfree(seqid);
  498. }
  499. /*
  500. * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
  501. * failed with a seqid incrementing error -
  502. * see comments nfs_fs.h:seqid_mutating_error()
  503. */
  504. static inline void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
  505. {
  506. switch (status) {
  507. case 0:
  508. break;
  509. case -NFS4ERR_BAD_SEQID:
  510. case -NFS4ERR_STALE_CLIENTID:
  511. case -NFS4ERR_STALE_STATEID:
  512. case -NFS4ERR_BAD_STATEID:
  513. case -NFS4ERR_BADXDR:
  514. case -NFS4ERR_RESOURCE:
  515. case -NFS4ERR_NOFILEHANDLE:
  516. /* Non-seqid mutating errors */
  517. return;
  518. };
  519. /*
  520. * Note: no locking needed as we are guaranteed to be first
  521. * on the sequence list
  522. */
  523. seqid->sequence->counter++;
  524. }
  525. void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
  526. {
  527. if (status == -NFS4ERR_BAD_SEQID) {
  528. struct nfs4_state_owner *sp = container_of(seqid->sequence,
  529. struct nfs4_state_owner, so_seqid);
  530. nfs4_drop_state_owner(sp);
  531. }
  532. return nfs_increment_seqid(status, seqid);
  533. }
  534. /*
  535. * Increment the seqid if the LOCK/LOCKU succeeded, or
  536. * failed with a seqid incrementing error -
  537. * see comments nfs_fs.h:seqid_mutating_error()
  538. */
  539. void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
  540. {
  541. return nfs_increment_seqid(status, seqid);
  542. }
  543. int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
  544. {
  545. struct rpc_sequence *sequence = seqid->sequence->sequence;
  546. int status = 0;
  547. if (sequence->list.next == &seqid->list)
  548. goto out;
  549. spin_lock(&sequence->lock);
  550. if (sequence->list.next != &seqid->list) {
  551. rpc_sleep_on(&sequence->wait, task, NULL, NULL);
  552. status = -EAGAIN;
  553. }
  554. spin_unlock(&sequence->lock);
  555. out:
  556. return status;
  557. }
  558. static int reclaimer(void *);
  559. static inline void nfs4_clear_recover_bit(struct nfs_client *clp)
  560. {
  561. smp_mb__before_clear_bit();
  562. clear_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state);
  563. smp_mb__after_clear_bit();
  564. wake_up_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER);
  565. rpc_wake_up(&clp->cl_rpcwaitq);
  566. }
  567. /*
  568. * State recovery routine
  569. */
  570. static void nfs4_recover_state(struct nfs_client *clp)
  571. {
  572. struct task_struct *task;
  573. __module_get(THIS_MODULE);
  574. atomic_inc(&clp->cl_count);
  575. task = kthread_run(reclaimer, clp, "%u.%u.%u.%u-reclaim",
  576. NIPQUAD(clp->cl_addr.sin_addr));
  577. if (!IS_ERR(task))
  578. return;
  579. nfs4_clear_recover_bit(clp);
  580. nfs_put_client(clp);
  581. module_put(THIS_MODULE);
  582. }
  583. /*
  584. * Schedule a state recovery attempt
  585. */
  586. void nfs4_schedule_state_recovery(struct nfs_client *clp)
  587. {
  588. if (!clp)
  589. return;
  590. if (test_and_set_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
  591. nfs4_recover_state(clp);
  592. }
  593. static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state)
  594. {
  595. struct inode *inode = state->inode;
  596. struct file_lock *fl;
  597. int status = 0;
  598. for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
  599. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  600. continue;
  601. if (((struct nfs_open_context *)fl->fl_file->private_data)->state != state)
  602. continue;
  603. status = ops->recover_lock(state, fl);
  604. if (status >= 0)
  605. continue;
  606. switch (status) {
  607. default:
  608. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  609. __FUNCTION__, status);
  610. case -NFS4ERR_EXPIRED:
  611. case -NFS4ERR_NO_GRACE:
  612. case -NFS4ERR_RECLAIM_BAD:
  613. case -NFS4ERR_RECLAIM_CONFLICT:
  614. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  615. break;
  616. case -NFS4ERR_STALE_CLIENTID:
  617. goto out_err;
  618. }
  619. }
  620. return 0;
  621. out_err:
  622. return status;
  623. }
  624. static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp)
  625. {
  626. struct nfs4_state *state;
  627. struct nfs4_lock_state *lock;
  628. int status = 0;
  629. /* Note: we rely on the sp->so_states list being ordered
  630. * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
  631. * states first.
  632. * This is needed to ensure that the server won't give us any
  633. * read delegations that we have to return if, say, we are
  634. * recovering after a network partition or a reboot from a
  635. * server that doesn't support a grace period.
  636. */
  637. list_for_each_entry(state, &sp->so_states, open_states) {
  638. if (state->state == 0)
  639. continue;
  640. status = ops->recover_open(sp, state);
  641. if (status >= 0) {
  642. status = nfs4_reclaim_locks(ops, state);
  643. if (status < 0)
  644. goto out_err;
  645. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  646. if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
  647. printk("%s: Lock reclaim failed!\n",
  648. __FUNCTION__);
  649. }
  650. continue;
  651. }
  652. switch (status) {
  653. default:
  654. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  655. __FUNCTION__, status);
  656. case -ENOENT:
  657. case -NFS4ERR_RECLAIM_BAD:
  658. case -NFS4ERR_RECLAIM_CONFLICT:
  659. /*
  660. * Open state on this file cannot be recovered
  661. * All we can do is revert to using the zero stateid.
  662. */
  663. memset(state->stateid.data, 0,
  664. sizeof(state->stateid.data));
  665. /* Mark the file as being 'closed' */
  666. state->state = 0;
  667. break;
  668. case -NFS4ERR_EXPIRED:
  669. case -NFS4ERR_NO_GRACE:
  670. case -NFS4ERR_STALE_CLIENTID:
  671. goto out_err;
  672. }
  673. }
  674. return 0;
  675. out_err:
  676. return status;
  677. }
  678. static void nfs4_state_mark_reclaim(struct nfs_client *clp)
  679. {
  680. struct nfs4_state_owner *sp;
  681. struct nfs4_state *state;
  682. struct nfs4_lock_state *lock;
  683. /* Reset all sequence ids to zero */
  684. list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
  685. sp->so_seqid.counter = 0;
  686. sp->so_seqid.flags = 0;
  687. spin_lock(&sp->so_lock);
  688. list_for_each_entry(state, &sp->so_states, open_states) {
  689. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  690. lock->ls_seqid.counter = 0;
  691. lock->ls_seqid.flags = 0;
  692. lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
  693. }
  694. }
  695. spin_unlock(&sp->so_lock);
  696. }
  697. }
  698. static int reclaimer(void *ptr)
  699. {
  700. struct nfs_client *clp = ptr;
  701. struct nfs4_state_owner *sp;
  702. struct nfs4_state_recovery_ops *ops;
  703. struct rpc_cred *cred;
  704. int status = 0;
  705. allow_signal(SIGKILL);
  706. /* Ensure exclusive access to NFSv4 state */
  707. lock_kernel();
  708. down_write(&clp->cl_sem);
  709. /* Are there any NFS mounts out there? */
  710. if (list_empty(&clp->cl_superblocks))
  711. goto out;
  712. restart_loop:
  713. ops = &nfs4_network_partition_recovery_ops;
  714. /* Are there any open files on this volume? */
  715. cred = nfs4_get_renew_cred(clp);
  716. if (cred != NULL) {
  717. /* Yes there are: try to renew the old lease */
  718. status = nfs4_proc_renew(clp, cred);
  719. switch (status) {
  720. case 0:
  721. case -NFS4ERR_CB_PATH_DOWN:
  722. put_rpccred(cred);
  723. goto out;
  724. case -NFS4ERR_STALE_CLIENTID:
  725. case -NFS4ERR_LEASE_MOVED:
  726. ops = &nfs4_reboot_recovery_ops;
  727. }
  728. } else {
  729. /* "reboot" to ensure we clear all state on the server */
  730. clp->cl_boot_time = CURRENT_TIME;
  731. cred = nfs4_get_setclientid_cred(clp);
  732. }
  733. /* We're going to have to re-establish a clientid */
  734. nfs4_state_mark_reclaim(clp);
  735. status = -ENOENT;
  736. if (cred != NULL) {
  737. status = nfs4_init_client(clp, cred);
  738. put_rpccred(cred);
  739. }
  740. if (status)
  741. goto out_error;
  742. /* Mark all delegations for reclaim */
  743. nfs_delegation_mark_reclaim(clp);
  744. /* Note: list is protected by exclusive lock on cl->cl_sem */
  745. list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
  746. status = nfs4_reclaim_open_state(ops, sp);
  747. if (status < 0) {
  748. if (status == -NFS4ERR_NO_GRACE) {
  749. ops = &nfs4_network_partition_recovery_ops;
  750. status = nfs4_reclaim_open_state(ops, sp);
  751. }
  752. if (status == -NFS4ERR_STALE_CLIENTID)
  753. goto restart_loop;
  754. if (status == -NFS4ERR_EXPIRED)
  755. goto restart_loop;
  756. }
  757. }
  758. nfs_delegation_reap_unclaimed(clp);
  759. out:
  760. up_write(&clp->cl_sem);
  761. unlock_kernel();
  762. if (status == -NFS4ERR_CB_PATH_DOWN)
  763. nfs_handle_cb_pathdown(clp);
  764. nfs4_clear_recover_bit(clp);
  765. nfs_put_client(clp);
  766. module_put_and_exit(0);
  767. return 0;
  768. out_error:
  769. printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %u.%u.%u.%u with error %d\n",
  770. NIPQUAD(clp->cl_addr.sin_addr), -status);
  771. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  772. goto out;
  773. }
  774. /*
  775. * Local variables:
  776. * c-basic-offset: 8
  777. * End:
  778. */