nfs4state.c 24 KB

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