nfs4state.c 25 KB

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