nfs4state.c 25 KB

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