nfs4state.c 23 KB

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