nfs4state.c 23 KB

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