nfs4state.c 30 KB

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