nfs4state.c 24 KB

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