nfs4state.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. /*
  2. * fs/nfs/nfs4state.c
  3. *
  4. * Client-side XDR for NFSv4.
  5. *
  6. * Copyright (c) 2002 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Kendrick Smith <kmsmith@umich.edu>
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  25. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  26. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  31. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. * Implementation of the NFSv4 state model. For the time being,
  37. * this is minimal, but will be made much more complex in a
  38. * subsequent patch.
  39. */
  40. #include <linux/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. rpc_destroy_wait_queue(&new->so_sequence.wait);
  268. kfree(new);
  269. }
  270. return sp;
  271. }
  272. /*
  273. * Must be called with clp->cl_sem held in order to avoid races
  274. * with state recovery...
  275. */
  276. void nfs4_put_state_owner(struct nfs4_state_owner *sp)
  277. {
  278. struct nfs_client *clp = sp->so_client;
  279. struct rpc_cred *cred = sp->so_cred;
  280. if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
  281. return;
  282. nfs4_remove_state_owner(clp, sp);
  283. spin_unlock(&clp->cl_lock);
  284. rpc_destroy_wait_queue(&sp->so_sequence.wait);
  285. put_rpccred(cred);
  286. kfree(sp);
  287. }
  288. static struct nfs4_state *
  289. nfs4_alloc_open_state(void)
  290. {
  291. struct nfs4_state *state;
  292. state = kzalloc(sizeof(*state), GFP_KERNEL);
  293. if (!state)
  294. return NULL;
  295. atomic_set(&state->count, 1);
  296. INIT_LIST_HEAD(&state->lock_states);
  297. spin_lock_init(&state->state_lock);
  298. seqlock_init(&state->seqlock);
  299. return state;
  300. }
  301. void
  302. nfs4_state_set_mode_locked(struct nfs4_state *state, mode_t mode)
  303. {
  304. if (state->state == mode)
  305. return;
  306. /* NB! List reordering - see the reclaim code for why. */
  307. if ((mode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
  308. if (mode & FMODE_WRITE)
  309. list_move(&state->open_states, &state->owner->so_states);
  310. else
  311. list_move_tail(&state->open_states, &state->owner->so_states);
  312. }
  313. state->state = mode;
  314. }
  315. static struct nfs4_state *
  316. __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
  317. {
  318. struct nfs_inode *nfsi = NFS_I(inode);
  319. struct nfs4_state *state;
  320. list_for_each_entry(state, &nfsi->open_states, inode_states) {
  321. if (state->owner != owner)
  322. continue;
  323. if (atomic_inc_not_zero(&state->count))
  324. return state;
  325. }
  326. return NULL;
  327. }
  328. static void
  329. nfs4_free_open_state(struct nfs4_state *state)
  330. {
  331. kfree(state);
  332. }
  333. struct nfs4_state *
  334. nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
  335. {
  336. struct nfs4_state *state, *new;
  337. struct nfs_inode *nfsi = NFS_I(inode);
  338. spin_lock(&inode->i_lock);
  339. state = __nfs4_find_state_byowner(inode, owner);
  340. spin_unlock(&inode->i_lock);
  341. if (state)
  342. goto out;
  343. new = nfs4_alloc_open_state();
  344. spin_lock(&owner->so_lock);
  345. spin_lock(&inode->i_lock);
  346. state = __nfs4_find_state_byowner(inode, owner);
  347. if (state == NULL && new != NULL) {
  348. state = new;
  349. state->owner = owner;
  350. atomic_inc(&owner->so_count);
  351. list_add(&state->inode_states, &nfsi->open_states);
  352. state->inode = igrab(inode);
  353. spin_unlock(&inode->i_lock);
  354. /* Note: The reclaim code dictates that we add stateless
  355. * and read-only stateids to the end of the list */
  356. list_add_tail(&state->open_states, &owner->so_states);
  357. spin_unlock(&owner->so_lock);
  358. } else {
  359. spin_unlock(&inode->i_lock);
  360. spin_unlock(&owner->so_lock);
  361. if (new)
  362. nfs4_free_open_state(new);
  363. }
  364. out:
  365. return state;
  366. }
  367. /*
  368. * Beware! Caller must be holding exactly one
  369. * reference to clp->cl_sem!
  370. */
  371. void nfs4_put_open_state(struct nfs4_state *state)
  372. {
  373. struct inode *inode = state->inode;
  374. struct nfs4_state_owner *owner = state->owner;
  375. if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
  376. return;
  377. spin_lock(&inode->i_lock);
  378. list_del(&state->inode_states);
  379. list_del(&state->open_states);
  380. spin_unlock(&inode->i_lock);
  381. spin_unlock(&owner->so_lock);
  382. iput(inode);
  383. nfs4_free_open_state(state);
  384. nfs4_put_state_owner(owner);
  385. }
  386. /*
  387. * Close the current file.
  388. */
  389. static void __nfs4_close(struct path *path, struct nfs4_state *state, mode_t mode, int wait)
  390. {
  391. struct nfs4_state_owner *owner = state->owner;
  392. int call_close = 0;
  393. int newstate;
  394. atomic_inc(&owner->so_count);
  395. /* Protect against nfs4_find_state() */
  396. spin_lock(&owner->so_lock);
  397. switch (mode & (FMODE_READ | FMODE_WRITE)) {
  398. case FMODE_READ:
  399. state->n_rdonly--;
  400. break;
  401. case FMODE_WRITE:
  402. state->n_wronly--;
  403. break;
  404. case FMODE_READ|FMODE_WRITE:
  405. state->n_rdwr--;
  406. }
  407. newstate = FMODE_READ|FMODE_WRITE;
  408. if (state->n_rdwr == 0) {
  409. if (state->n_rdonly == 0) {
  410. newstate &= ~FMODE_READ;
  411. call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
  412. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  413. }
  414. if (state->n_wronly == 0) {
  415. newstate &= ~FMODE_WRITE;
  416. call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
  417. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  418. }
  419. if (newstate == 0)
  420. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  421. }
  422. nfs4_state_set_mode_locked(state, newstate);
  423. spin_unlock(&owner->so_lock);
  424. if (!call_close) {
  425. nfs4_put_open_state(state);
  426. nfs4_put_state_owner(owner);
  427. } else
  428. nfs4_do_close(path, state, wait);
  429. }
  430. void nfs4_close_state(struct path *path, struct nfs4_state *state, mode_t mode)
  431. {
  432. __nfs4_close(path, state, mode, 0);
  433. }
  434. void nfs4_close_sync(struct path *path, struct nfs4_state *state, mode_t mode)
  435. {
  436. __nfs4_close(path, state, mode, 1);
  437. }
  438. /*
  439. * Search the state->lock_states for an existing lock_owner
  440. * that is compatible with current->files
  441. */
  442. static struct nfs4_lock_state *
  443. __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
  444. {
  445. struct nfs4_lock_state *pos;
  446. list_for_each_entry(pos, &state->lock_states, ls_locks) {
  447. if (pos->ls_owner != fl_owner)
  448. continue;
  449. atomic_inc(&pos->ls_count);
  450. return pos;
  451. }
  452. return NULL;
  453. }
  454. /*
  455. * Return a compatible lock_state. If no initialized lock_state structure
  456. * exists, return an uninitialized one.
  457. *
  458. */
  459. static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
  460. {
  461. struct nfs4_lock_state *lsp;
  462. struct nfs_client *clp = state->owner->so_client;
  463. lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
  464. if (lsp == NULL)
  465. return NULL;
  466. rpc_init_wait_queue(&lsp->ls_sequence.wait, "lock_seqid_waitqueue");
  467. spin_lock_init(&lsp->ls_sequence.lock);
  468. INIT_LIST_HEAD(&lsp->ls_sequence.list);
  469. lsp->ls_seqid.sequence = &lsp->ls_sequence;
  470. atomic_set(&lsp->ls_count, 1);
  471. lsp->ls_owner = fl_owner;
  472. spin_lock(&clp->cl_lock);
  473. nfs_alloc_unique_id(&clp->cl_lockowner_id, &lsp->ls_id, 1, 64);
  474. spin_unlock(&clp->cl_lock);
  475. INIT_LIST_HEAD(&lsp->ls_locks);
  476. return lsp;
  477. }
  478. static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
  479. {
  480. struct nfs_client *clp = lsp->ls_state->owner->so_client;
  481. spin_lock(&clp->cl_lock);
  482. nfs_free_unique_id(&clp->cl_lockowner_id, &lsp->ls_id);
  483. spin_unlock(&clp->cl_lock);
  484. rpc_destroy_wait_queue(&lsp->ls_sequence.wait);
  485. kfree(lsp);
  486. }
  487. /*
  488. * Return a compatible lock_state. If no initialized lock_state structure
  489. * exists, return an uninitialized one.
  490. *
  491. * The caller must be holding clp->cl_sem
  492. */
  493. static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
  494. {
  495. struct nfs4_lock_state *lsp, *new = NULL;
  496. for(;;) {
  497. spin_lock(&state->state_lock);
  498. lsp = __nfs4_find_lock_state(state, owner);
  499. if (lsp != NULL)
  500. break;
  501. if (new != NULL) {
  502. new->ls_state = state;
  503. list_add(&new->ls_locks, &state->lock_states);
  504. set_bit(LK_STATE_IN_USE, &state->flags);
  505. lsp = new;
  506. new = NULL;
  507. break;
  508. }
  509. spin_unlock(&state->state_lock);
  510. new = nfs4_alloc_lock_state(state, owner);
  511. if (new == NULL)
  512. return NULL;
  513. }
  514. spin_unlock(&state->state_lock);
  515. if (new != NULL)
  516. nfs4_free_lock_state(new);
  517. return lsp;
  518. }
  519. /*
  520. * Release reference to lock_state, and free it if we see that
  521. * it is no longer in use
  522. */
  523. void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
  524. {
  525. struct nfs4_state *state;
  526. if (lsp == NULL)
  527. return;
  528. state = lsp->ls_state;
  529. if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
  530. return;
  531. list_del(&lsp->ls_locks);
  532. if (list_empty(&state->lock_states))
  533. clear_bit(LK_STATE_IN_USE, &state->flags);
  534. spin_unlock(&state->state_lock);
  535. nfs4_free_lock_state(lsp);
  536. }
  537. static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
  538. {
  539. struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
  540. dst->fl_u.nfs4_fl.owner = lsp;
  541. atomic_inc(&lsp->ls_count);
  542. }
  543. static void nfs4_fl_release_lock(struct file_lock *fl)
  544. {
  545. nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
  546. }
  547. static struct file_lock_operations nfs4_fl_lock_ops = {
  548. .fl_copy_lock = nfs4_fl_copy_lock,
  549. .fl_release_private = nfs4_fl_release_lock,
  550. };
  551. int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
  552. {
  553. struct nfs4_lock_state *lsp;
  554. if (fl->fl_ops != NULL)
  555. return 0;
  556. lsp = nfs4_get_lock_state(state, fl->fl_owner);
  557. if (lsp == NULL)
  558. return -ENOMEM;
  559. fl->fl_u.nfs4_fl.owner = lsp;
  560. fl->fl_ops = &nfs4_fl_lock_ops;
  561. return 0;
  562. }
  563. /*
  564. * Byte-range lock aware utility to initialize the stateid of read/write
  565. * requests.
  566. */
  567. void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
  568. {
  569. struct nfs4_lock_state *lsp;
  570. int seq;
  571. do {
  572. seq = read_seqbegin(&state->seqlock);
  573. memcpy(dst, &state->stateid, sizeof(*dst));
  574. } while (read_seqretry(&state->seqlock, seq));
  575. if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
  576. return;
  577. spin_lock(&state->state_lock);
  578. lsp = __nfs4_find_lock_state(state, fl_owner);
  579. if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
  580. memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
  581. spin_unlock(&state->state_lock);
  582. nfs4_put_lock_state(lsp);
  583. }
  584. struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
  585. {
  586. struct nfs_seqid *new;
  587. new = kmalloc(sizeof(*new), GFP_KERNEL);
  588. if (new != NULL) {
  589. new->sequence = counter;
  590. INIT_LIST_HEAD(&new->list);
  591. }
  592. return new;
  593. }
  594. void nfs_free_seqid(struct nfs_seqid *seqid)
  595. {
  596. if (!list_empty(&seqid->list)) {
  597. struct rpc_sequence *sequence = seqid->sequence->sequence;
  598. spin_lock(&sequence->lock);
  599. list_del(&seqid->list);
  600. spin_unlock(&sequence->lock);
  601. rpc_wake_up(&sequence->wait);
  602. }
  603. kfree(seqid);
  604. }
  605. /*
  606. * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
  607. * failed with a seqid incrementing error -
  608. * see comments nfs_fs.h:seqid_mutating_error()
  609. */
  610. static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
  611. {
  612. BUG_ON(list_first_entry(&seqid->sequence->sequence->list, struct nfs_seqid, list) != seqid);
  613. switch (status) {
  614. case 0:
  615. break;
  616. case -NFS4ERR_BAD_SEQID:
  617. if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
  618. return;
  619. printk(KERN_WARNING "NFS: v4 server returned a bad"
  620. " sequence-id error on an"
  621. " unconfirmed sequence %p!\n",
  622. seqid->sequence);
  623. case -NFS4ERR_STALE_CLIENTID:
  624. case -NFS4ERR_STALE_STATEID:
  625. case -NFS4ERR_BAD_STATEID:
  626. case -NFS4ERR_BADXDR:
  627. case -NFS4ERR_RESOURCE:
  628. case -NFS4ERR_NOFILEHANDLE:
  629. /* Non-seqid mutating errors */
  630. return;
  631. };
  632. /*
  633. * Note: no locking needed as we are guaranteed to be first
  634. * on the sequence list
  635. */
  636. seqid->sequence->counter++;
  637. }
  638. void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
  639. {
  640. if (status == -NFS4ERR_BAD_SEQID) {
  641. struct nfs4_state_owner *sp = container_of(seqid->sequence,
  642. struct nfs4_state_owner, so_seqid);
  643. nfs4_drop_state_owner(sp);
  644. }
  645. nfs_increment_seqid(status, seqid);
  646. }
  647. /*
  648. * Increment the seqid if the LOCK/LOCKU succeeded, or
  649. * failed with a seqid incrementing error -
  650. * see comments nfs_fs.h:seqid_mutating_error()
  651. */
  652. void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
  653. {
  654. nfs_increment_seqid(status, seqid);
  655. }
  656. int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
  657. {
  658. struct rpc_sequence *sequence = seqid->sequence->sequence;
  659. int status = 0;
  660. spin_lock(&sequence->lock);
  661. if (list_empty(&seqid->list))
  662. list_add_tail(&seqid->list, &sequence->list);
  663. if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
  664. goto unlock;
  665. rpc_sleep_on(&sequence->wait, task, NULL);
  666. status = -EAGAIN;
  667. unlock:
  668. spin_unlock(&sequence->lock);
  669. return status;
  670. }
  671. static int reclaimer(void *);
  672. static inline void nfs4_clear_recover_bit(struct nfs_client *clp)
  673. {
  674. smp_mb__before_clear_bit();
  675. clear_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state);
  676. smp_mb__after_clear_bit();
  677. wake_up_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER);
  678. rpc_wake_up(&clp->cl_rpcwaitq);
  679. }
  680. /*
  681. * State recovery routine
  682. */
  683. static void nfs4_recover_state(struct nfs_client *clp)
  684. {
  685. struct task_struct *task;
  686. __module_get(THIS_MODULE);
  687. atomic_inc(&clp->cl_count);
  688. task = kthread_run(reclaimer, clp, "%s-reclaim",
  689. rpc_peeraddr2str(clp->cl_rpcclient,
  690. RPC_DISPLAY_ADDR));
  691. if (!IS_ERR(task))
  692. return;
  693. nfs4_clear_recover_bit(clp);
  694. nfs_put_client(clp);
  695. module_put(THIS_MODULE);
  696. }
  697. /*
  698. * Schedule a state recovery attempt
  699. */
  700. void nfs4_schedule_state_recovery(struct nfs_client *clp)
  701. {
  702. if (!clp)
  703. return;
  704. if (test_and_set_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
  705. nfs4_recover_state(clp);
  706. }
  707. static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state)
  708. {
  709. struct inode *inode = state->inode;
  710. struct file_lock *fl;
  711. int status = 0;
  712. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  713. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  714. continue;
  715. if (nfs_file_open_context(fl->fl_file)->state != state)
  716. continue;
  717. status = ops->recover_lock(state, fl);
  718. if (status >= 0)
  719. continue;
  720. switch (status) {
  721. default:
  722. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  723. __FUNCTION__, status);
  724. case -NFS4ERR_EXPIRED:
  725. case -NFS4ERR_NO_GRACE:
  726. case -NFS4ERR_RECLAIM_BAD:
  727. case -NFS4ERR_RECLAIM_CONFLICT:
  728. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  729. break;
  730. case -NFS4ERR_STALE_CLIENTID:
  731. goto out_err;
  732. }
  733. }
  734. return 0;
  735. out_err:
  736. return status;
  737. }
  738. static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp)
  739. {
  740. struct nfs4_state *state;
  741. struct nfs4_lock_state *lock;
  742. int status = 0;
  743. /* Note: we rely on the sp->so_states list being ordered
  744. * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
  745. * states first.
  746. * This is needed to ensure that the server won't give us any
  747. * read delegations that we have to return if, say, we are
  748. * recovering after a network partition or a reboot from a
  749. * server that doesn't support a grace period.
  750. */
  751. list_for_each_entry(state, &sp->so_states, open_states) {
  752. if (state->state == 0)
  753. continue;
  754. status = ops->recover_open(sp, state);
  755. if (status >= 0) {
  756. status = nfs4_reclaim_locks(ops, state);
  757. if (status < 0)
  758. goto out_err;
  759. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  760. if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
  761. printk("%s: Lock reclaim failed!\n",
  762. __FUNCTION__);
  763. }
  764. continue;
  765. }
  766. switch (status) {
  767. default:
  768. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  769. __FUNCTION__, status);
  770. case -ENOENT:
  771. case -NFS4ERR_RECLAIM_BAD:
  772. case -NFS4ERR_RECLAIM_CONFLICT:
  773. /*
  774. * Open state on this file cannot be recovered
  775. * All we can do is revert to using the zero stateid.
  776. */
  777. memset(state->stateid.data, 0,
  778. sizeof(state->stateid.data));
  779. /* Mark the file as being 'closed' */
  780. state->state = 0;
  781. break;
  782. case -NFS4ERR_EXPIRED:
  783. case -NFS4ERR_NO_GRACE:
  784. case -NFS4ERR_STALE_CLIENTID:
  785. goto out_err;
  786. }
  787. }
  788. return 0;
  789. out_err:
  790. return status;
  791. }
  792. static void nfs4_state_mark_reclaim(struct nfs_client *clp)
  793. {
  794. struct nfs4_state_owner *sp;
  795. struct rb_node *pos;
  796. struct nfs4_state *state;
  797. struct nfs4_lock_state *lock;
  798. /* Reset all sequence ids to zero */
  799. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  800. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  801. sp->so_seqid.counter = 0;
  802. sp->so_seqid.flags = 0;
  803. spin_lock(&sp->so_lock);
  804. list_for_each_entry(state, &sp->so_states, open_states) {
  805. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  806. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  807. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  808. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  809. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  810. lock->ls_seqid.counter = 0;
  811. lock->ls_seqid.flags = 0;
  812. lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
  813. }
  814. }
  815. spin_unlock(&sp->so_lock);
  816. }
  817. }
  818. static int reclaimer(void *ptr)
  819. {
  820. struct nfs_client *clp = ptr;
  821. struct nfs4_state_owner *sp;
  822. struct rb_node *pos;
  823. struct nfs4_state_recovery_ops *ops;
  824. struct rpc_cred *cred;
  825. int status = 0;
  826. allow_signal(SIGKILL);
  827. /* Ensure exclusive access to NFSv4 state */
  828. lock_kernel();
  829. down_write(&clp->cl_sem);
  830. /* Are there any NFS mounts out there? */
  831. if (list_empty(&clp->cl_superblocks))
  832. goto out;
  833. restart_loop:
  834. ops = &nfs4_network_partition_recovery_ops;
  835. /* Are there any open files on this volume? */
  836. cred = nfs4_get_renew_cred(clp);
  837. if (cred != NULL) {
  838. /* Yes there are: try to renew the old lease */
  839. status = nfs4_proc_renew(clp, cred);
  840. switch (status) {
  841. case 0:
  842. case -NFS4ERR_CB_PATH_DOWN:
  843. put_rpccred(cred);
  844. goto out;
  845. case -NFS4ERR_STALE_CLIENTID:
  846. case -NFS4ERR_LEASE_MOVED:
  847. ops = &nfs4_reboot_recovery_ops;
  848. }
  849. } else {
  850. /* "reboot" to ensure we clear all state on the server */
  851. clp->cl_boot_time = CURRENT_TIME;
  852. cred = nfs4_get_setclientid_cred(clp);
  853. }
  854. /* We're going to have to re-establish a clientid */
  855. nfs4_state_mark_reclaim(clp);
  856. status = -ENOENT;
  857. if (cred != NULL) {
  858. status = nfs4_init_client(clp, cred);
  859. put_rpccred(cred);
  860. }
  861. if (status)
  862. goto out_error;
  863. /* Mark all delegations for reclaim */
  864. nfs_delegation_mark_reclaim(clp);
  865. /* Note: list is protected by exclusive lock on cl->cl_sem */
  866. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  867. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  868. status = nfs4_reclaim_open_state(ops, sp);
  869. if (status < 0) {
  870. if (status == -NFS4ERR_NO_GRACE) {
  871. ops = &nfs4_network_partition_recovery_ops;
  872. status = nfs4_reclaim_open_state(ops, sp);
  873. }
  874. if (status == -NFS4ERR_STALE_CLIENTID)
  875. goto restart_loop;
  876. if (status == -NFS4ERR_EXPIRED)
  877. goto restart_loop;
  878. }
  879. }
  880. nfs_delegation_reap_unclaimed(clp);
  881. out:
  882. up_write(&clp->cl_sem);
  883. unlock_kernel();
  884. if (status == -NFS4ERR_CB_PATH_DOWN)
  885. nfs_handle_cb_pathdown(clp);
  886. nfs4_clear_recover_bit(clp);
  887. nfs_put_client(clp);
  888. module_put_and_exit(0);
  889. return 0;
  890. out_error:
  891. printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %s"
  892. " with error %d\n", clp->cl_hostname, -status);
  893. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  894. goto out;
  895. }
  896. /*
  897. * Local variables:
  898. * c-basic-offset: 8
  899. * End:
  900. */