nfs4state.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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. lsp->ls_seqid.sequence = &state->owner->so_sequence;
  464. atomic_set(&lsp->ls_count, 1);
  465. lsp->ls_owner = fl_owner;
  466. spin_lock(&clp->cl_lock);
  467. nfs_alloc_unique_id(&clp->cl_lockowner_id, &lsp->ls_id, 1, 64);
  468. spin_unlock(&clp->cl_lock);
  469. INIT_LIST_HEAD(&lsp->ls_locks);
  470. return lsp;
  471. }
  472. static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
  473. {
  474. struct nfs_client *clp = lsp->ls_state->owner->so_client;
  475. spin_lock(&clp->cl_lock);
  476. nfs_free_unique_id(&clp->cl_lockowner_id, &lsp->ls_id);
  477. spin_unlock(&clp->cl_lock);
  478. kfree(lsp);
  479. }
  480. /*
  481. * Return a compatible lock_state. If no initialized lock_state structure
  482. * exists, return an uninitialized one.
  483. *
  484. * The caller must be holding clp->cl_sem
  485. */
  486. static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
  487. {
  488. struct nfs4_lock_state *lsp, *new = NULL;
  489. for(;;) {
  490. spin_lock(&state->state_lock);
  491. lsp = __nfs4_find_lock_state(state, owner);
  492. if (lsp != NULL)
  493. break;
  494. if (new != NULL) {
  495. new->ls_state = state;
  496. list_add(&new->ls_locks, &state->lock_states);
  497. set_bit(LK_STATE_IN_USE, &state->flags);
  498. lsp = new;
  499. new = NULL;
  500. break;
  501. }
  502. spin_unlock(&state->state_lock);
  503. new = nfs4_alloc_lock_state(state, owner);
  504. if (new == NULL)
  505. return NULL;
  506. }
  507. spin_unlock(&state->state_lock);
  508. if (new != NULL)
  509. nfs4_free_lock_state(new);
  510. return lsp;
  511. }
  512. /*
  513. * Release reference to lock_state, and free it if we see that
  514. * it is no longer in use
  515. */
  516. void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
  517. {
  518. struct nfs4_state *state;
  519. if (lsp == NULL)
  520. return;
  521. state = lsp->ls_state;
  522. if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
  523. return;
  524. list_del(&lsp->ls_locks);
  525. if (list_empty(&state->lock_states))
  526. clear_bit(LK_STATE_IN_USE, &state->flags);
  527. spin_unlock(&state->state_lock);
  528. nfs4_free_lock_state(lsp);
  529. }
  530. static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
  531. {
  532. struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
  533. dst->fl_u.nfs4_fl.owner = lsp;
  534. atomic_inc(&lsp->ls_count);
  535. }
  536. static void nfs4_fl_release_lock(struct file_lock *fl)
  537. {
  538. nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
  539. }
  540. static struct file_lock_operations nfs4_fl_lock_ops = {
  541. .fl_copy_lock = nfs4_fl_copy_lock,
  542. .fl_release_private = nfs4_fl_release_lock,
  543. };
  544. int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
  545. {
  546. struct nfs4_lock_state *lsp;
  547. if (fl->fl_ops != NULL)
  548. return 0;
  549. lsp = nfs4_get_lock_state(state, fl->fl_owner);
  550. if (lsp == NULL)
  551. return -ENOMEM;
  552. fl->fl_u.nfs4_fl.owner = lsp;
  553. fl->fl_ops = &nfs4_fl_lock_ops;
  554. return 0;
  555. }
  556. /*
  557. * Byte-range lock aware utility to initialize the stateid of read/write
  558. * requests.
  559. */
  560. void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
  561. {
  562. struct nfs4_lock_state *lsp;
  563. int seq;
  564. do {
  565. seq = read_seqbegin(&state->seqlock);
  566. memcpy(dst, &state->stateid, sizeof(*dst));
  567. } while (read_seqretry(&state->seqlock, seq));
  568. if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
  569. return;
  570. spin_lock(&state->state_lock);
  571. lsp = __nfs4_find_lock_state(state, fl_owner);
  572. if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
  573. memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
  574. spin_unlock(&state->state_lock);
  575. nfs4_put_lock_state(lsp);
  576. }
  577. struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
  578. {
  579. struct rpc_sequence *sequence = counter->sequence;
  580. struct nfs_seqid *new;
  581. new = kmalloc(sizeof(*new), GFP_KERNEL);
  582. if (new != NULL) {
  583. new->sequence = counter;
  584. spin_lock(&sequence->lock);
  585. list_add_tail(&new->list, &sequence->list);
  586. spin_unlock(&sequence->lock);
  587. }
  588. return new;
  589. }
  590. void nfs_free_seqid(struct nfs_seqid *seqid)
  591. {
  592. struct rpc_sequence *sequence = seqid->sequence->sequence;
  593. spin_lock(&sequence->lock);
  594. list_del(&seqid->list);
  595. spin_unlock(&sequence->lock);
  596. rpc_wake_up(&sequence->wait);
  597. kfree(seqid);
  598. }
  599. /*
  600. * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
  601. * failed with a seqid incrementing error -
  602. * see comments nfs_fs.h:seqid_mutating_error()
  603. */
  604. static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
  605. {
  606. switch (status) {
  607. case 0:
  608. break;
  609. case -NFS4ERR_BAD_SEQID:
  610. if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
  611. return;
  612. printk(KERN_WARNING "NFS: v4 server returned a bad"
  613. "sequence-id error on an"
  614. "unconfirmed sequence %p!\n",
  615. seqid->sequence);
  616. case -NFS4ERR_STALE_CLIENTID:
  617. case -NFS4ERR_STALE_STATEID:
  618. case -NFS4ERR_BAD_STATEID:
  619. case -NFS4ERR_BADXDR:
  620. case -NFS4ERR_RESOURCE:
  621. case -NFS4ERR_NOFILEHANDLE:
  622. /* Non-seqid mutating errors */
  623. return;
  624. };
  625. /*
  626. * Note: no locking needed as we are guaranteed to be first
  627. * on the sequence list
  628. */
  629. seqid->sequence->counter++;
  630. }
  631. void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
  632. {
  633. if (status == -NFS4ERR_BAD_SEQID) {
  634. struct nfs4_state_owner *sp = container_of(seqid->sequence,
  635. struct nfs4_state_owner, so_seqid);
  636. nfs4_drop_state_owner(sp);
  637. }
  638. nfs_increment_seqid(status, seqid);
  639. }
  640. /*
  641. * Increment the seqid if the LOCK/LOCKU succeeded, or
  642. * failed with a seqid incrementing error -
  643. * see comments nfs_fs.h:seqid_mutating_error()
  644. */
  645. void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
  646. {
  647. nfs_increment_seqid(status, seqid);
  648. }
  649. int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
  650. {
  651. struct rpc_sequence *sequence = seqid->sequence->sequence;
  652. int status = 0;
  653. if (sequence->list.next == &seqid->list)
  654. goto out;
  655. spin_lock(&sequence->lock);
  656. if (sequence->list.next != &seqid->list) {
  657. rpc_sleep_on(&sequence->wait, task, NULL, NULL);
  658. status = -EAGAIN;
  659. }
  660. spin_unlock(&sequence->lock);
  661. out:
  662. return status;
  663. }
  664. static int reclaimer(void *);
  665. static inline void nfs4_clear_recover_bit(struct nfs_client *clp)
  666. {
  667. smp_mb__before_clear_bit();
  668. clear_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state);
  669. smp_mb__after_clear_bit();
  670. wake_up_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER);
  671. rpc_wake_up(&clp->cl_rpcwaitq);
  672. }
  673. /*
  674. * State recovery routine
  675. */
  676. static void nfs4_recover_state(struct nfs_client *clp)
  677. {
  678. struct task_struct *task;
  679. __module_get(THIS_MODULE);
  680. atomic_inc(&clp->cl_count);
  681. task = kthread_run(reclaimer, clp, "%u.%u.%u.%u-reclaim",
  682. NIPQUAD(clp->cl_addr.sin_addr));
  683. if (!IS_ERR(task))
  684. return;
  685. nfs4_clear_recover_bit(clp);
  686. nfs_put_client(clp);
  687. module_put(THIS_MODULE);
  688. }
  689. /*
  690. * Schedule a state recovery attempt
  691. */
  692. void nfs4_schedule_state_recovery(struct nfs_client *clp)
  693. {
  694. if (!clp)
  695. return;
  696. if (test_and_set_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
  697. nfs4_recover_state(clp);
  698. }
  699. static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state)
  700. {
  701. struct inode *inode = state->inode;
  702. struct file_lock *fl;
  703. int status = 0;
  704. for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
  705. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  706. continue;
  707. if (nfs_file_open_context(fl->fl_file)->state != state)
  708. continue;
  709. status = ops->recover_lock(state, fl);
  710. if (status >= 0)
  711. continue;
  712. switch (status) {
  713. default:
  714. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  715. __FUNCTION__, status);
  716. case -NFS4ERR_EXPIRED:
  717. case -NFS4ERR_NO_GRACE:
  718. case -NFS4ERR_RECLAIM_BAD:
  719. case -NFS4ERR_RECLAIM_CONFLICT:
  720. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  721. break;
  722. case -NFS4ERR_STALE_CLIENTID:
  723. goto out_err;
  724. }
  725. }
  726. return 0;
  727. out_err:
  728. return status;
  729. }
  730. static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp)
  731. {
  732. struct nfs4_state *state;
  733. struct nfs4_lock_state *lock;
  734. int status = 0;
  735. /* Note: we rely on the sp->so_states list being ordered
  736. * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
  737. * states first.
  738. * This is needed to ensure that the server won't give us any
  739. * read delegations that we have to return if, say, we are
  740. * recovering after a network partition or a reboot from a
  741. * server that doesn't support a grace period.
  742. */
  743. list_for_each_entry(state, &sp->so_states, open_states) {
  744. if (state->state == 0)
  745. continue;
  746. status = ops->recover_open(sp, state);
  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 void nfs4_state_mark_reclaim(struct nfs_client *clp)
  785. {
  786. struct nfs4_state_owner *sp;
  787. struct rb_node *pos;
  788. struct nfs4_state *state;
  789. struct nfs4_lock_state *lock;
  790. /* Reset all sequence ids to zero */
  791. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  792. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  793. sp->so_seqid.counter = 0;
  794. sp->so_seqid.flags = 0;
  795. spin_lock(&sp->so_lock);
  796. list_for_each_entry(state, &sp->so_states, open_states) {
  797. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  798. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  799. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  800. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  801. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  802. lock->ls_seqid.counter = 0;
  803. lock->ls_seqid.flags = 0;
  804. lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
  805. }
  806. }
  807. spin_unlock(&sp->so_lock);
  808. }
  809. }
  810. static int reclaimer(void *ptr)
  811. {
  812. struct nfs_client *clp = ptr;
  813. struct nfs4_state_owner *sp;
  814. struct rb_node *pos;
  815. struct nfs4_state_recovery_ops *ops;
  816. struct rpc_cred *cred;
  817. int status = 0;
  818. allow_signal(SIGKILL);
  819. /* Ensure exclusive access to NFSv4 state */
  820. lock_kernel();
  821. down_write(&clp->cl_sem);
  822. /* Are there any NFS mounts out there? */
  823. if (list_empty(&clp->cl_superblocks))
  824. goto out;
  825. restart_loop:
  826. ops = &nfs4_network_partition_recovery_ops;
  827. /* Are there any open files on this volume? */
  828. cred = nfs4_get_renew_cred(clp);
  829. if (cred != NULL) {
  830. /* Yes there are: try to renew the old lease */
  831. status = nfs4_proc_renew(clp, cred);
  832. switch (status) {
  833. case 0:
  834. case -NFS4ERR_CB_PATH_DOWN:
  835. put_rpccred(cred);
  836. goto out;
  837. case -NFS4ERR_STALE_CLIENTID:
  838. case -NFS4ERR_LEASE_MOVED:
  839. ops = &nfs4_reboot_recovery_ops;
  840. }
  841. } else {
  842. /* "reboot" to ensure we clear all state on the server */
  843. clp->cl_boot_time = CURRENT_TIME;
  844. cred = nfs4_get_setclientid_cred(clp);
  845. }
  846. /* We're going to have to re-establish a clientid */
  847. nfs4_state_mark_reclaim(clp);
  848. status = -ENOENT;
  849. if (cred != NULL) {
  850. status = nfs4_init_client(clp, cred);
  851. put_rpccred(cred);
  852. }
  853. if (status)
  854. goto out_error;
  855. /* Mark all delegations for reclaim */
  856. nfs_delegation_mark_reclaim(clp);
  857. /* Note: list is protected by exclusive lock on cl->cl_sem */
  858. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  859. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  860. status = nfs4_reclaim_open_state(ops, sp);
  861. if (status < 0) {
  862. if (status == -NFS4ERR_NO_GRACE) {
  863. ops = &nfs4_network_partition_recovery_ops;
  864. status = nfs4_reclaim_open_state(ops, sp);
  865. }
  866. if (status == -NFS4ERR_STALE_CLIENTID)
  867. goto restart_loop;
  868. if (status == -NFS4ERR_EXPIRED)
  869. goto restart_loop;
  870. }
  871. }
  872. nfs_delegation_reap_unclaimed(clp);
  873. out:
  874. up_write(&clp->cl_sem);
  875. unlock_kernel();
  876. if (status == -NFS4ERR_CB_PATH_DOWN)
  877. nfs_handle_cb_pathdown(clp);
  878. nfs4_clear_recover_bit(clp);
  879. nfs_put_client(clp);
  880. module_put_and_exit(0);
  881. return 0;
  882. out_error:
  883. printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %u.%u.%u.%u with error %d\n",
  884. NIPQUAD(clp->cl_addr.sin_addr), -status);
  885. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  886. goto out;
  887. }
  888. /*
  889. * Local variables:
  890. * c-basic-offset: 8
  891. * End:
  892. */