nfs4state.c 21 KB

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