nfs4state.c 25 KB

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