nfs4state.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  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. int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
  58. {
  59. unsigned short port;
  60. int status;
  61. port = nfs_callback_tcpport;
  62. if (clp->cl_addr.ss_family == AF_INET6)
  63. port = nfs_callback_tcpport6;
  64. status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred);
  65. if (status == 0)
  66. status = nfs4_proc_setclientid_confirm(clp, cred);
  67. if (status == 0)
  68. nfs4_schedule_state_renewal(clp);
  69. return status;
  70. }
  71. struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
  72. {
  73. struct rpc_cred *cred = NULL;
  74. if (clp->cl_machine_cred != NULL)
  75. cred = get_rpccred(clp->cl_machine_cred);
  76. return cred;
  77. }
  78. static void nfs4_clear_machine_cred(struct nfs_client *clp)
  79. {
  80. struct rpc_cred *cred;
  81. spin_lock(&clp->cl_lock);
  82. cred = clp->cl_machine_cred;
  83. clp->cl_machine_cred = NULL;
  84. spin_unlock(&clp->cl_lock);
  85. if (cred != NULL)
  86. put_rpccred(cred);
  87. }
  88. struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
  89. {
  90. struct nfs4_state_owner *sp;
  91. struct rb_node *pos;
  92. struct rpc_cred *cred = NULL;
  93. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  94. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  95. if (list_empty(&sp->so_states))
  96. continue;
  97. cred = get_rpccred(sp->so_cred);
  98. break;
  99. }
  100. return cred;
  101. }
  102. #if defined(CONFIG_NFS_V4_1)
  103. static int nfs41_setup_state_renewal(struct nfs_client *clp)
  104. {
  105. int status;
  106. struct nfs_fsinfo fsinfo;
  107. status = nfs4_proc_get_lease_time(clp, &fsinfo);
  108. if (status == 0) {
  109. /* Update lease time and schedule renewal */
  110. spin_lock(&clp->cl_lock);
  111. clp->cl_lease_time = fsinfo.lease_time * HZ;
  112. clp->cl_last_renewal = jiffies;
  113. spin_unlock(&clp->cl_lock);
  114. nfs4_schedule_state_renewal(clp);
  115. }
  116. return status;
  117. }
  118. static void nfs41_end_drain_session(struct nfs_client *clp,
  119. struct nfs4_session *ses)
  120. {
  121. if (test_and_clear_bit(NFS4CLNT_SESSION_DRAINING, &clp->cl_state))
  122. rpc_wake_up(&ses->fc_slot_table.slot_tbl_waitq);
  123. }
  124. static int nfs41_begin_drain_session(struct nfs_client *clp,
  125. struct nfs4_session *ses)
  126. {
  127. struct nfs4_slot_table *tbl = &ses->fc_slot_table;
  128. spin_lock(&tbl->slot_tbl_lock);
  129. set_bit(NFS4CLNT_SESSION_DRAINING, &clp->cl_state);
  130. if (tbl->highest_used_slotid != -1) {
  131. INIT_COMPLETION(ses->complete);
  132. spin_unlock(&tbl->slot_tbl_lock);
  133. return wait_for_completion_interruptible(&ses->complete);
  134. }
  135. spin_unlock(&tbl->slot_tbl_lock);
  136. return 0;
  137. }
  138. int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
  139. {
  140. int status;
  141. status = nfs41_begin_drain_session(clp, clp->cl_session);
  142. if (status != 0)
  143. goto out;
  144. status = nfs4_proc_exchange_id(clp, cred);
  145. if (status != 0)
  146. goto out;
  147. status = nfs4_proc_create_session(clp);
  148. if (status != 0)
  149. goto out;
  150. nfs41_end_drain_session(clp, clp->cl_session);
  151. nfs41_setup_state_renewal(clp);
  152. nfs_mark_client_ready(clp, NFS_CS_READY);
  153. out:
  154. return status;
  155. }
  156. struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp)
  157. {
  158. struct rpc_cred *cred;
  159. spin_lock(&clp->cl_lock);
  160. cred = nfs4_get_machine_cred_locked(clp);
  161. spin_unlock(&clp->cl_lock);
  162. return cred;
  163. }
  164. #endif /* CONFIG_NFS_V4_1 */
  165. struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
  166. {
  167. struct nfs4_state_owner *sp;
  168. struct rb_node *pos;
  169. struct rpc_cred *cred;
  170. spin_lock(&clp->cl_lock);
  171. cred = nfs4_get_machine_cred_locked(clp);
  172. if (cred != NULL)
  173. goto out;
  174. pos = rb_first(&clp->cl_state_owners);
  175. if (pos != NULL) {
  176. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  177. cred = get_rpccred(sp->so_cred);
  178. }
  179. out:
  180. spin_unlock(&clp->cl_lock);
  181. return cred;
  182. }
  183. static void nfs_alloc_unique_id(struct rb_root *root, struct nfs_unique_id *new,
  184. __u64 minval, int maxbits)
  185. {
  186. struct rb_node **p, *parent;
  187. struct nfs_unique_id *pos;
  188. __u64 mask = ~0ULL;
  189. if (maxbits < 64)
  190. mask = (1ULL << maxbits) - 1ULL;
  191. /* Ensure distribution is more or less flat */
  192. get_random_bytes(&new->id, sizeof(new->id));
  193. new->id &= mask;
  194. if (new->id < minval)
  195. new->id += minval;
  196. retry:
  197. p = &root->rb_node;
  198. parent = NULL;
  199. while (*p != NULL) {
  200. parent = *p;
  201. pos = rb_entry(parent, struct nfs_unique_id, rb_node);
  202. if (new->id < pos->id)
  203. p = &(*p)->rb_left;
  204. else if (new->id > pos->id)
  205. p = &(*p)->rb_right;
  206. else
  207. goto id_exists;
  208. }
  209. rb_link_node(&new->rb_node, parent, p);
  210. rb_insert_color(&new->rb_node, root);
  211. return;
  212. id_exists:
  213. for (;;) {
  214. new->id++;
  215. if (new->id < minval || (new->id & mask) != new->id) {
  216. new->id = minval;
  217. break;
  218. }
  219. parent = rb_next(parent);
  220. if (parent == NULL)
  221. break;
  222. pos = rb_entry(parent, struct nfs_unique_id, rb_node);
  223. if (new->id < pos->id)
  224. break;
  225. }
  226. goto retry;
  227. }
  228. static void nfs_free_unique_id(struct rb_root *root, struct nfs_unique_id *id)
  229. {
  230. rb_erase(&id->rb_node, root);
  231. }
  232. static struct nfs4_state_owner *
  233. nfs4_find_state_owner(struct nfs_server *server, struct rpc_cred *cred)
  234. {
  235. struct nfs_client *clp = server->nfs_client;
  236. struct rb_node **p = &clp->cl_state_owners.rb_node,
  237. *parent = NULL;
  238. struct nfs4_state_owner *sp, *res = NULL;
  239. while (*p != NULL) {
  240. parent = *p;
  241. sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
  242. if (server < sp->so_server) {
  243. p = &parent->rb_left;
  244. continue;
  245. }
  246. if (server > sp->so_server) {
  247. p = &parent->rb_right;
  248. continue;
  249. }
  250. if (cred < sp->so_cred)
  251. p = &parent->rb_left;
  252. else if (cred > sp->so_cred)
  253. p = &parent->rb_right;
  254. else {
  255. atomic_inc(&sp->so_count);
  256. res = sp;
  257. break;
  258. }
  259. }
  260. return res;
  261. }
  262. static struct nfs4_state_owner *
  263. nfs4_insert_state_owner(struct nfs_client *clp, struct nfs4_state_owner *new)
  264. {
  265. struct rb_node **p = &clp->cl_state_owners.rb_node,
  266. *parent = NULL;
  267. struct nfs4_state_owner *sp;
  268. while (*p != NULL) {
  269. parent = *p;
  270. sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
  271. if (new->so_server < sp->so_server) {
  272. p = &parent->rb_left;
  273. continue;
  274. }
  275. if (new->so_server > sp->so_server) {
  276. p = &parent->rb_right;
  277. continue;
  278. }
  279. if (new->so_cred < sp->so_cred)
  280. p = &parent->rb_left;
  281. else if (new->so_cred > sp->so_cred)
  282. p = &parent->rb_right;
  283. else {
  284. atomic_inc(&sp->so_count);
  285. return sp;
  286. }
  287. }
  288. nfs_alloc_unique_id(&clp->cl_openowner_id, &new->so_owner_id, 1, 64);
  289. rb_link_node(&new->so_client_node, parent, p);
  290. rb_insert_color(&new->so_client_node, &clp->cl_state_owners);
  291. return new;
  292. }
  293. static void
  294. nfs4_remove_state_owner(struct nfs_client *clp, struct nfs4_state_owner *sp)
  295. {
  296. if (!RB_EMPTY_NODE(&sp->so_client_node))
  297. rb_erase(&sp->so_client_node, &clp->cl_state_owners);
  298. nfs_free_unique_id(&clp->cl_openowner_id, &sp->so_owner_id);
  299. }
  300. /*
  301. * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
  302. * create a new state_owner.
  303. *
  304. */
  305. static struct nfs4_state_owner *
  306. nfs4_alloc_state_owner(void)
  307. {
  308. struct nfs4_state_owner *sp;
  309. sp = kzalloc(sizeof(*sp),GFP_KERNEL);
  310. if (!sp)
  311. return NULL;
  312. spin_lock_init(&sp->so_lock);
  313. INIT_LIST_HEAD(&sp->so_states);
  314. INIT_LIST_HEAD(&sp->so_delegations);
  315. rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
  316. sp->so_seqid.sequence = &sp->so_sequence;
  317. spin_lock_init(&sp->so_sequence.lock);
  318. INIT_LIST_HEAD(&sp->so_sequence.list);
  319. atomic_set(&sp->so_count, 1);
  320. return sp;
  321. }
  322. static void
  323. nfs4_drop_state_owner(struct nfs4_state_owner *sp)
  324. {
  325. if (!RB_EMPTY_NODE(&sp->so_client_node)) {
  326. struct nfs_client *clp = sp->so_client;
  327. spin_lock(&clp->cl_lock);
  328. rb_erase(&sp->so_client_node, &clp->cl_state_owners);
  329. RB_CLEAR_NODE(&sp->so_client_node);
  330. spin_unlock(&clp->cl_lock);
  331. }
  332. }
  333. struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred)
  334. {
  335. struct nfs_client *clp = server->nfs_client;
  336. struct nfs4_state_owner *sp, *new;
  337. spin_lock(&clp->cl_lock);
  338. sp = nfs4_find_state_owner(server, cred);
  339. spin_unlock(&clp->cl_lock);
  340. if (sp != NULL)
  341. return sp;
  342. new = nfs4_alloc_state_owner();
  343. if (new == NULL)
  344. return NULL;
  345. new->so_client = clp;
  346. new->so_server = server;
  347. new->so_cred = cred;
  348. spin_lock(&clp->cl_lock);
  349. sp = nfs4_insert_state_owner(clp, new);
  350. spin_unlock(&clp->cl_lock);
  351. if (sp == new)
  352. get_rpccred(cred);
  353. else {
  354. rpc_destroy_wait_queue(&new->so_sequence.wait);
  355. kfree(new);
  356. }
  357. return sp;
  358. }
  359. void nfs4_put_state_owner(struct nfs4_state_owner *sp)
  360. {
  361. struct nfs_client *clp = sp->so_client;
  362. struct rpc_cred *cred = sp->so_cred;
  363. if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
  364. return;
  365. nfs4_remove_state_owner(clp, sp);
  366. spin_unlock(&clp->cl_lock);
  367. rpc_destroy_wait_queue(&sp->so_sequence.wait);
  368. put_rpccred(cred);
  369. kfree(sp);
  370. }
  371. static struct nfs4_state *
  372. nfs4_alloc_open_state(void)
  373. {
  374. struct nfs4_state *state;
  375. state = kzalloc(sizeof(*state), GFP_KERNEL);
  376. if (!state)
  377. return NULL;
  378. atomic_set(&state->count, 1);
  379. INIT_LIST_HEAD(&state->lock_states);
  380. spin_lock_init(&state->state_lock);
  381. seqlock_init(&state->seqlock);
  382. return state;
  383. }
  384. void
  385. nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
  386. {
  387. if (state->state == fmode)
  388. return;
  389. /* NB! List reordering - see the reclaim code for why. */
  390. if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
  391. if (fmode & FMODE_WRITE)
  392. list_move(&state->open_states, &state->owner->so_states);
  393. else
  394. list_move_tail(&state->open_states, &state->owner->so_states);
  395. }
  396. state->state = fmode;
  397. }
  398. static struct nfs4_state *
  399. __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
  400. {
  401. struct nfs_inode *nfsi = NFS_I(inode);
  402. struct nfs4_state *state;
  403. list_for_each_entry(state, &nfsi->open_states, inode_states) {
  404. if (state->owner != owner)
  405. continue;
  406. if (atomic_inc_not_zero(&state->count))
  407. return state;
  408. }
  409. return NULL;
  410. }
  411. static void
  412. nfs4_free_open_state(struct nfs4_state *state)
  413. {
  414. kfree(state);
  415. }
  416. struct nfs4_state *
  417. nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
  418. {
  419. struct nfs4_state *state, *new;
  420. struct nfs_inode *nfsi = NFS_I(inode);
  421. spin_lock(&inode->i_lock);
  422. state = __nfs4_find_state_byowner(inode, owner);
  423. spin_unlock(&inode->i_lock);
  424. if (state)
  425. goto out;
  426. new = nfs4_alloc_open_state();
  427. spin_lock(&owner->so_lock);
  428. spin_lock(&inode->i_lock);
  429. state = __nfs4_find_state_byowner(inode, owner);
  430. if (state == NULL && new != NULL) {
  431. state = new;
  432. state->owner = owner;
  433. atomic_inc(&owner->so_count);
  434. list_add(&state->inode_states, &nfsi->open_states);
  435. state->inode = igrab(inode);
  436. spin_unlock(&inode->i_lock);
  437. /* Note: The reclaim code dictates that we add stateless
  438. * and read-only stateids to the end of the list */
  439. list_add_tail(&state->open_states, &owner->so_states);
  440. spin_unlock(&owner->so_lock);
  441. } else {
  442. spin_unlock(&inode->i_lock);
  443. spin_unlock(&owner->so_lock);
  444. if (new)
  445. nfs4_free_open_state(new);
  446. }
  447. out:
  448. return state;
  449. }
  450. void nfs4_put_open_state(struct nfs4_state *state)
  451. {
  452. struct inode *inode = state->inode;
  453. struct nfs4_state_owner *owner = state->owner;
  454. if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
  455. return;
  456. spin_lock(&inode->i_lock);
  457. list_del(&state->inode_states);
  458. list_del(&state->open_states);
  459. spin_unlock(&inode->i_lock);
  460. spin_unlock(&owner->so_lock);
  461. iput(inode);
  462. nfs4_free_open_state(state);
  463. nfs4_put_state_owner(owner);
  464. }
  465. /*
  466. * Close the current file.
  467. */
  468. static void __nfs4_close(struct path *path, struct nfs4_state *state, fmode_t fmode, int wait)
  469. {
  470. struct nfs4_state_owner *owner = state->owner;
  471. int call_close = 0;
  472. fmode_t newstate;
  473. atomic_inc(&owner->so_count);
  474. /* Protect against nfs4_find_state() */
  475. spin_lock(&owner->so_lock);
  476. switch (fmode & (FMODE_READ | FMODE_WRITE)) {
  477. case FMODE_READ:
  478. state->n_rdonly--;
  479. break;
  480. case FMODE_WRITE:
  481. state->n_wronly--;
  482. break;
  483. case FMODE_READ|FMODE_WRITE:
  484. state->n_rdwr--;
  485. }
  486. newstate = FMODE_READ|FMODE_WRITE;
  487. if (state->n_rdwr == 0) {
  488. if (state->n_rdonly == 0) {
  489. newstate &= ~FMODE_READ;
  490. call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
  491. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  492. }
  493. if (state->n_wronly == 0) {
  494. newstate &= ~FMODE_WRITE;
  495. call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
  496. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  497. }
  498. if (newstate == 0)
  499. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  500. }
  501. nfs4_state_set_mode_locked(state, newstate);
  502. spin_unlock(&owner->so_lock);
  503. if (!call_close) {
  504. nfs4_put_open_state(state);
  505. nfs4_put_state_owner(owner);
  506. } else
  507. nfs4_do_close(path, state, wait);
  508. }
  509. void nfs4_close_state(struct path *path, struct nfs4_state *state, fmode_t fmode)
  510. {
  511. __nfs4_close(path, state, fmode, 0);
  512. }
  513. void nfs4_close_sync(struct path *path, struct nfs4_state *state, fmode_t fmode)
  514. {
  515. __nfs4_close(path, state, fmode, 1);
  516. }
  517. /*
  518. * Search the state->lock_states for an existing lock_owner
  519. * that is compatible with current->files
  520. */
  521. static struct nfs4_lock_state *
  522. __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
  523. {
  524. struct nfs4_lock_state *pos;
  525. list_for_each_entry(pos, &state->lock_states, ls_locks) {
  526. if (pos->ls_owner != fl_owner)
  527. continue;
  528. atomic_inc(&pos->ls_count);
  529. return pos;
  530. }
  531. return NULL;
  532. }
  533. /*
  534. * Return a compatible lock_state. If no initialized lock_state structure
  535. * exists, return an uninitialized one.
  536. *
  537. */
  538. static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
  539. {
  540. struct nfs4_lock_state *lsp;
  541. struct nfs_client *clp = state->owner->so_client;
  542. lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
  543. if (lsp == NULL)
  544. return NULL;
  545. rpc_init_wait_queue(&lsp->ls_sequence.wait, "lock_seqid_waitqueue");
  546. spin_lock_init(&lsp->ls_sequence.lock);
  547. INIT_LIST_HEAD(&lsp->ls_sequence.list);
  548. lsp->ls_seqid.sequence = &lsp->ls_sequence;
  549. atomic_set(&lsp->ls_count, 1);
  550. lsp->ls_state = state;
  551. lsp->ls_owner = fl_owner;
  552. spin_lock(&clp->cl_lock);
  553. nfs_alloc_unique_id(&clp->cl_lockowner_id, &lsp->ls_id, 1, 64);
  554. spin_unlock(&clp->cl_lock);
  555. INIT_LIST_HEAD(&lsp->ls_locks);
  556. return lsp;
  557. }
  558. static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
  559. {
  560. struct nfs_client *clp = lsp->ls_state->owner->so_client;
  561. spin_lock(&clp->cl_lock);
  562. nfs_free_unique_id(&clp->cl_lockowner_id, &lsp->ls_id);
  563. spin_unlock(&clp->cl_lock);
  564. rpc_destroy_wait_queue(&lsp->ls_sequence.wait);
  565. kfree(lsp);
  566. }
  567. /*
  568. * Return a compatible lock_state. If no initialized lock_state structure
  569. * exists, return an uninitialized one.
  570. *
  571. */
  572. static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
  573. {
  574. struct nfs4_lock_state *lsp, *new = NULL;
  575. for(;;) {
  576. spin_lock(&state->state_lock);
  577. lsp = __nfs4_find_lock_state(state, owner);
  578. if (lsp != NULL)
  579. break;
  580. if (new != NULL) {
  581. list_add(&new->ls_locks, &state->lock_states);
  582. set_bit(LK_STATE_IN_USE, &state->flags);
  583. lsp = new;
  584. new = NULL;
  585. break;
  586. }
  587. spin_unlock(&state->state_lock);
  588. new = nfs4_alloc_lock_state(state, owner);
  589. if (new == NULL)
  590. return NULL;
  591. }
  592. spin_unlock(&state->state_lock);
  593. if (new != NULL)
  594. nfs4_free_lock_state(new);
  595. return lsp;
  596. }
  597. /*
  598. * Release reference to lock_state, and free it if we see that
  599. * it is no longer in use
  600. */
  601. void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
  602. {
  603. struct nfs4_state *state;
  604. if (lsp == NULL)
  605. return;
  606. state = lsp->ls_state;
  607. if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
  608. return;
  609. list_del(&lsp->ls_locks);
  610. if (list_empty(&state->lock_states))
  611. clear_bit(LK_STATE_IN_USE, &state->flags);
  612. spin_unlock(&state->state_lock);
  613. nfs4_free_lock_state(lsp);
  614. }
  615. static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
  616. {
  617. struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
  618. dst->fl_u.nfs4_fl.owner = lsp;
  619. atomic_inc(&lsp->ls_count);
  620. }
  621. static void nfs4_fl_release_lock(struct file_lock *fl)
  622. {
  623. nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
  624. }
  625. static const struct file_lock_operations nfs4_fl_lock_ops = {
  626. .fl_copy_lock = nfs4_fl_copy_lock,
  627. .fl_release_private = nfs4_fl_release_lock,
  628. };
  629. int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
  630. {
  631. struct nfs4_lock_state *lsp;
  632. if (fl->fl_ops != NULL)
  633. return 0;
  634. lsp = nfs4_get_lock_state(state, fl->fl_owner);
  635. if (lsp == NULL)
  636. return -ENOMEM;
  637. fl->fl_u.nfs4_fl.owner = lsp;
  638. fl->fl_ops = &nfs4_fl_lock_ops;
  639. return 0;
  640. }
  641. /*
  642. * Byte-range lock aware utility to initialize the stateid of read/write
  643. * requests.
  644. */
  645. void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
  646. {
  647. struct nfs4_lock_state *lsp;
  648. int seq;
  649. do {
  650. seq = read_seqbegin(&state->seqlock);
  651. memcpy(dst, &state->stateid, sizeof(*dst));
  652. } while (read_seqretry(&state->seqlock, seq));
  653. if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
  654. return;
  655. spin_lock(&state->state_lock);
  656. lsp = __nfs4_find_lock_state(state, fl_owner);
  657. if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
  658. memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
  659. spin_unlock(&state->state_lock);
  660. nfs4_put_lock_state(lsp);
  661. }
  662. struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
  663. {
  664. struct nfs_seqid *new;
  665. new = kmalloc(sizeof(*new), GFP_KERNEL);
  666. if (new != NULL) {
  667. new->sequence = counter;
  668. INIT_LIST_HEAD(&new->list);
  669. }
  670. return new;
  671. }
  672. void nfs_free_seqid(struct nfs_seqid *seqid)
  673. {
  674. if (!list_empty(&seqid->list)) {
  675. struct rpc_sequence *sequence = seqid->sequence->sequence;
  676. spin_lock(&sequence->lock);
  677. list_del(&seqid->list);
  678. spin_unlock(&sequence->lock);
  679. rpc_wake_up(&sequence->wait);
  680. }
  681. kfree(seqid);
  682. }
  683. /*
  684. * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
  685. * failed with a seqid incrementing error -
  686. * see comments nfs_fs.h:seqid_mutating_error()
  687. */
  688. static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
  689. {
  690. BUG_ON(list_first_entry(&seqid->sequence->sequence->list, struct nfs_seqid, list) != seqid);
  691. switch (status) {
  692. case 0:
  693. break;
  694. case -NFS4ERR_BAD_SEQID:
  695. if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
  696. return;
  697. printk(KERN_WARNING "NFS: v4 server returned a bad"
  698. " sequence-id error on an"
  699. " unconfirmed sequence %p!\n",
  700. seqid->sequence);
  701. case -NFS4ERR_STALE_CLIENTID:
  702. case -NFS4ERR_STALE_STATEID:
  703. case -NFS4ERR_BAD_STATEID:
  704. case -NFS4ERR_BADXDR:
  705. case -NFS4ERR_RESOURCE:
  706. case -NFS4ERR_NOFILEHANDLE:
  707. /* Non-seqid mutating errors */
  708. return;
  709. };
  710. /*
  711. * Note: no locking needed as we are guaranteed to be first
  712. * on the sequence list
  713. */
  714. seqid->sequence->counter++;
  715. }
  716. void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
  717. {
  718. struct nfs4_state_owner *sp = container_of(seqid->sequence,
  719. struct nfs4_state_owner, so_seqid);
  720. struct nfs_server *server = sp->so_server;
  721. if (status == -NFS4ERR_BAD_SEQID)
  722. nfs4_drop_state_owner(sp);
  723. if (!nfs4_has_session(server->nfs_client))
  724. nfs_increment_seqid(status, seqid);
  725. }
  726. /*
  727. * Increment the seqid if the LOCK/LOCKU succeeded, or
  728. * failed with a seqid incrementing error -
  729. * see comments nfs_fs.h:seqid_mutating_error()
  730. */
  731. void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
  732. {
  733. nfs_increment_seqid(status, seqid);
  734. }
  735. int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
  736. {
  737. struct rpc_sequence *sequence = seqid->sequence->sequence;
  738. int status = 0;
  739. spin_lock(&sequence->lock);
  740. if (list_empty(&seqid->list))
  741. list_add_tail(&seqid->list, &sequence->list);
  742. if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
  743. goto unlock;
  744. rpc_sleep_on(&sequence->wait, task, NULL);
  745. status = -EAGAIN;
  746. unlock:
  747. spin_unlock(&sequence->lock);
  748. return status;
  749. }
  750. static int nfs4_run_state_manager(void *);
  751. static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
  752. {
  753. smp_mb__before_clear_bit();
  754. clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
  755. smp_mb__after_clear_bit();
  756. wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
  757. rpc_wake_up(&clp->cl_rpcwaitq);
  758. }
  759. /*
  760. * Schedule the nfs_client asynchronous state management routine
  761. */
  762. void nfs4_schedule_state_manager(struct nfs_client *clp)
  763. {
  764. struct task_struct *task;
  765. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  766. return;
  767. __module_get(THIS_MODULE);
  768. atomic_inc(&clp->cl_count);
  769. task = kthread_run(nfs4_run_state_manager, clp, "%s-manager",
  770. rpc_peeraddr2str(clp->cl_rpcclient,
  771. RPC_DISPLAY_ADDR));
  772. if (!IS_ERR(task))
  773. return;
  774. nfs4_clear_state_manager_bit(clp);
  775. nfs_put_client(clp);
  776. module_put(THIS_MODULE);
  777. }
  778. /*
  779. * Schedule a state recovery attempt
  780. */
  781. void nfs4_schedule_state_recovery(struct nfs_client *clp)
  782. {
  783. if (!clp)
  784. return;
  785. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  786. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  787. nfs4_schedule_state_manager(clp);
  788. }
  789. static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
  790. {
  791. set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  792. /* Don't recover state that expired before the reboot */
  793. if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
  794. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  795. return 0;
  796. }
  797. set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
  798. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  799. return 1;
  800. }
  801. int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
  802. {
  803. set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
  804. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  805. set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
  806. set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
  807. return 1;
  808. }
  809. static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
  810. {
  811. struct inode *inode = state->inode;
  812. struct nfs_inode *nfsi = NFS_I(inode);
  813. struct file_lock *fl;
  814. int status = 0;
  815. if (inode->i_flock == NULL)
  816. return 0;
  817. /* Guard against delegation returns and new lock/unlock calls */
  818. down_write(&nfsi->rwsem);
  819. /* Protect inode->i_flock using the BKL */
  820. lock_kernel();
  821. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  822. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  823. continue;
  824. if (nfs_file_open_context(fl->fl_file)->state != state)
  825. continue;
  826. unlock_kernel();
  827. status = ops->recover_lock(state, fl);
  828. switch (status) {
  829. case 0:
  830. break;
  831. case -ESTALE:
  832. case -NFS4ERR_ADMIN_REVOKED:
  833. case -NFS4ERR_STALE_STATEID:
  834. case -NFS4ERR_BAD_STATEID:
  835. case -NFS4ERR_EXPIRED:
  836. case -NFS4ERR_NO_GRACE:
  837. case -NFS4ERR_STALE_CLIENTID:
  838. case -NFS4ERR_BADSESSION:
  839. case -NFS4ERR_BADSLOT:
  840. case -NFS4ERR_BAD_HIGH_SLOT:
  841. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  842. goto out;
  843. default:
  844. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  845. __func__, status);
  846. case -ENOMEM:
  847. case -NFS4ERR_DENIED:
  848. case -NFS4ERR_RECLAIM_BAD:
  849. case -NFS4ERR_RECLAIM_CONFLICT:
  850. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  851. status = 0;
  852. }
  853. lock_kernel();
  854. }
  855. unlock_kernel();
  856. out:
  857. up_write(&nfsi->rwsem);
  858. return status;
  859. }
  860. static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
  861. {
  862. struct nfs4_state *state;
  863. struct nfs4_lock_state *lock;
  864. int status = 0;
  865. /* Note: we rely on the sp->so_states list being ordered
  866. * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
  867. * states first.
  868. * This is needed to ensure that the server won't give us any
  869. * read delegations that we have to return if, say, we are
  870. * recovering after a network partition or a reboot from a
  871. * server that doesn't support a grace period.
  872. */
  873. restart:
  874. spin_lock(&sp->so_lock);
  875. list_for_each_entry(state, &sp->so_states, open_states) {
  876. if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
  877. continue;
  878. if (state->state == 0)
  879. continue;
  880. atomic_inc(&state->count);
  881. spin_unlock(&sp->so_lock);
  882. status = ops->recover_open(sp, state);
  883. if (status >= 0) {
  884. status = nfs4_reclaim_locks(state, ops);
  885. if (status >= 0) {
  886. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  887. if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
  888. printk("%s: Lock reclaim failed!\n",
  889. __func__);
  890. }
  891. nfs4_put_open_state(state);
  892. goto restart;
  893. }
  894. }
  895. switch (status) {
  896. default:
  897. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  898. __func__, status);
  899. case -ENOENT:
  900. case -ENOMEM:
  901. case -ESTALE:
  902. /*
  903. * Open state on this file cannot be recovered
  904. * All we can do is revert to using the zero stateid.
  905. */
  906. memset(state->stateid.data, 0,
  907. sizeof(state->stateid.data));
  908. /* Mark the file as being 'closed' */
  909. state->state = 0;
  910. break;
  911. case -NFS4ERR_ADMIN_REVOKED:
  912. case -NFS4ERR_STALE_STATEID:
  913. case -NFS4ERR_BAD_STATEID:
  914. case -NFS4ERR_RECLAIM_BAD:
  915. case -NFS4ERR_RECLAIM_CONFLICT:
  916. nfs4_state_mark_reclaim_nograce(sp->so_client, state);
  917. break;
  918. case -NFS4ERR_EXPIRED:
  919. case -NFS4ERR_NO_GRACE:
  920. nfs4_state_mark_reclaim_nograce(sp->so_client, state);
  921. case -NFS4ERR_STALE_CLIENTID:
  922. case -NFS4ERR_BADSESSION:
  923. case -NFS4ERR_BADSLOT:
  924. case -NFS4ERR_BAD_HIGH_SLOT:
  925. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  926. goto out_err;
  927. }
  928. nfs4_put_open_state(state);
  929. goto restart;
  930. }
  931. spin_unlock(&sp->so_lock);
  932. return 0;
  933. out_err:
  934. nfs4_put_open_state(state);
  935. return status;
  936. }
  937. static void nfs4_clear_open_state(struct nfs4_state *state)
  938. {
  939. struct nfs4_lock_state *lock;
  940. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  941. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  942. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  943. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  944. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  945. lock->ls_seqid.flags = 0;
  946. lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
  947. }
  948. }
  949. static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp, int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  950. {
  951. struct nfs4_state_owner *sp;
  952. struct rb_node *pos;
  953. struct nfs4_state *state;
  954. /* Reset all sequence ids to zero */
  955. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  956. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  957. sp->so_seqid.flags = 0;
  958. spin_lock(&sp->so_lock);
  959. list_for_each_entry(state, &sp->so_states, open_states) {
  960. if (mark_reclaim(clp, state))
  961. nfs4_clear_open_state(state);
  962. }
  963. spin_unlock(&sp->so_lock);
  964. }
  965. }
  966. static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
  967. {
  968. /* Mark all delegations for reclaim */
  969. nfs_delegation_mark_reclaim(clp);
  970. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
  971. }
  972. static void nfs4_reclaim_complete(struct nfs_client *clp,
  973. const struct nfs4_state_recovery_ops *ops)
  974. {
  975. /* Notify the server we're done reclaiming our state */
  976. if (ops->reclaim_complete)
  977. (void)ops->reclaim_complete(clp);
  978. }
  979. static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
  980. {
  981. struct nfs4_state_owner *sp;
  982. struct rb_node *pos;
  983. struct nfs4_state *state;
  984. if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  985. return;
  986. nfs4_reclaim_complete(clp,
  987. nfs4_reboot_recovery_ops[clp->cl_minorversion]);
  988. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  989. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  990. spin_lock(&sp->so_lock);
  991. list_for_each_entry(state, &sp->so_states, open_states) {
  992. if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags))
  993. continue;
  994. nfs4_state_mark_reclaim_nograce(clp, state);
  995. }
  996. spin_unlock(&sp->so_lock);
  997. }
  998. nfs_delegation_reap_unclaimed(clp);
  999. }
  1000. static void nfs_delegation_clear_all(struct nfs_client *clp)
  1001. {
  1002. nfs_delegation_mark_reclaim(clp);
  1003. nfs_delegation_reap_unclaimed(clp);
  1004. }
  1005. static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
  1006. {
  1007. nfs_delegation_clear_all(clp);
  1008. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
  1009. }
  1010. static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
  1011. {
  1012. switch (error) {
  1013. case -NFS4ERR_CB_PATH_DOWN:
  1014. nfs_handle_cb_pathdown(clp);
  1015. return 0;
  1016. case -NFS4ERR_NO_GRACE:
  1017. nfs4_state_end_reclaim_reboot(clp);
  1018. return 0;
  1019. case -NFS4ERR_STALE_CLIENTID:
  1020. case -NFS4ERR_LEASE_MOVED:
  1021. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1022. nfs4_state_end_reclaim_reboot(clp);
  1023. nfs4_state_start_reclaim_reboot(clp);
  1024. break;
  1025. case -NFS4ERR_EXPIRED:
  1026. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1027. nfs4_state_start_reclaim_nograce(clp);
  1028. break;
  1029. case -NFS4ERR_BADSESSION:
  1030. case -NFS4ERR_BADSLOT:
  1031. case -NFS4ERR_BAD_HIGH_SLOT:
  1032. case -NFS4ERR_DEADSESSION:
  1033. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1034. case -NFS4ERR_SEQ_FALSE_RETRY:
  1035. case -NFS4ERR_SEQ_MISORDERED:
  1036. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1037. /* Zero session reset errors */
  1038. return 0;
  1039. }
  1040. return error;
  1041. }
  1042. static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
  1043. {
  1044. struct rb_node *pos;
  1045. int status = 0;
  1046. restart:
  1047. spin_lock(&clp->cl_lock);
  1048. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  1049. struct nfs4_state_owner *sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  1050. if (!test_and_clear_bit(ops->owner_flag_bit, &sp->so_flags))
  1051. continue;
  1052. atomic_inc(&sp->so_count);
  1053. spin_unlock(&clp->cl_lock);
  1054. status = nfs4_reclaim_open_state(sp, ops);
  1055. if (status < 0) {
  1056. set_bit(ops->owner_flag_bit, &sp->so_flags);
  1057. nfs4_put_state_owner(sp);
  1058. return nfs4_recovery_handle_error(clp, status);
  1059. }
  1060. nfs4_put_state_owner(sp);
  1061. goto restart;
  1062. }
  1063. spin_unlock(&clp->cl_lock);
  1064. return status;
  1065. }
  1066. static int nfs4_check_lease(struct nfs_client *clp)
  1067. {
  1068. struct rpc_cred *cred;
  1069. struct nfs4_state_maintenance_ops *ops =
  1070. nfs4_state_renewal_ops[clp->cl_minorversion];
  1071. int status = -NFS4ERR_EXPIRED;
  1072. /* Is the client already known to have an expired lease? */
  1073. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1074. return 0;
  1075. spin_lock(&clp->cl_lock);
  1076. cred = ops->get_state_renewal_cred_locked(clp);
  1077. spin_unlock(&clp->cl_lock);
  1078. if (cred == NULL) {
  1079. cred = nfs4_get_setclientid_cred(clp);
  1080. if (cred == NULL)
  1081. goto out;
  1082. }
  1083. status = ops->renew_lease(clp, cred);
  1084. put_rpccred(cred);
  1085. out:
  1086. return nfs4_recovery_handle_error(clp, status);
  1087. }
  1088. static int nfs4_reclaim_lease(struct nfs_client *clp)
  1089. {
  1090. struct rpc_cred *cred;
  1091. struct nfs4_state_recovery_ops *ops =
  1092. nfs4_reboot_recovery_ops[clp->cl_minorversion];
  1093. int status = -ENOENT;
  1094. cred = ops->get_clid_cred(clp);
  1095. if (cred != NULL) {
  1096. status = ops->establish_clid(clp, cred);
  1097. put_rpccred(cred);
  1098. /* Handle case where the user hasn't set up machine creds */
  1099. if (status == -EACCES && cred == clp->cl_machine_cred) {
  1100. nfs4_clear_machine_cred(clp);
  1101. status = -EAGAIN;
  1102. }
  1103. if (status == -NFS4ERR_MINOR_VERS_MISMATCH)
  1104. status = -EPROTONOSUPPORT;
  1105. }
  1106. return status;
  1107. }
  1108. #ifdef CONFIG_NFS_V4_1
  1109. void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
  1110. {
  1111. if (!flags)
  1112. return;
  1113. else if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED) {
  1114. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1115. nfs4_state_start_reclaim_reboot(clp);
  1116. nfs4_schedule_state_recovery(clp);
  1117. } else if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
  1118. SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
  1119. SEQ4_STATUS_ADMIN_STATE_REVOKED |
  1120. SEQ4_STATUS_RECALLABLE_STATE_REVOKED |
  1121. SEQ4_STATUS_LEASE_MOVED)) {
  1122. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1123. nfs4_state_start_reclaim_nograce(clp);
  1124. nfs4_schedule_state_recovery(clp);
  1125. } else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
  1126. SEQ4_STATUS_BACKCHANNEL_FAULT |
  1127. SEQ4_STATUS_CB_PATH_DOWN_SESSION))
  1128. nfs_expire_all_delegations(clp);
  1129. }
  1130. static int nfs4_reset_session(struct nfs_client *clp)
  1131. {
  1132. struct nfs4_session *ses = clp->cl_session;
  1133. int status;
  1134. status = nfs41_begin_drain_session(clp, ses);
  1135. if (status != 0)
  1136. return status;
  1137. status = nfs4_proc_destroy_session(clp->cl_session);
  1138. if (status && status != -NFS4ERR_BADSESSION &&
  1139. status != -NFS4ERR_DEADSESSION) {
  1140. status = nfs4_recovery_handle_error(clp, status);
  1141. goto out;
  1142. }
  1143. memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
  1144. status = nfs4_proc_create_session(clp);
  1145. if (status)
  1146. status = nfs4_recovery_handle_error(clp, status);
  1147. out:
  1148. /*
  1149. * Let the state manager reestablish state
  1150. * without waking other tasks yet.
  1151. */
  1152. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
  1153. /* Wake up the next rpc task */
  1154. nfs41_end_drain_session(clp, ses);
  1155. if (status == 0)
  1156. nfs41_setup_state_renewal(clp);
  1157. }
  1158. return status;
  1159. }
  1160. #else /* CONFIG_NFS_V4_1 */
  1161. static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
  1162. #endif /* CONFIG_NFS_V4_1 */
  1163. /* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
  1164. * on EXCHANGE_ID for v4.1
  1165. */
  1166. static void nfs4_set_lease_expired(struct nfs_client *clp, int status)
  1167. {
  1168. if (nfs4_has_session(clp)) {
  1169. switch (status) {
  1170. case -NFS4ERR_DELAY:
  1171. case -NFS4ERR_CLID_INUSE:
  1172. case -EAGAIN:
  1173. break;
  1174. case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
  1175. * in nfs4_exchange_id */
  1176. default:
  1177. return;
  1178. }
  1179. }
  1180. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1181. }
  1182. static void nfs4_state_manager(struct nfs_client *clp)
  1183. {
  1184. int status = 0;
  1185. /* Ensure exclusive access to NFSv4 state */
  1186. for(;;) {
  1187. if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
  1188. /* We're going to have to re-establish a clientid */
  1189. status = nfs4_reclaim_lease(clp);
  1190. if (status) {
  1191. nfs4_set_lease_expired(clp, status);
  1192. if (test_bit(NFS4CLNT_LEASE_EXPIRED,
  1193. &clp->cl_state))
  1194. continue;
  1195. if (clp->cl_cons_state ==
  1196. NFS_CS_SESSION_INITING)
  1197. nfs_mark_client_ready(clp, status);
  1198. goto out_error;
  1199. }
  1200. clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1201. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  1202. }
  1203. if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
  1204. status = nfs4_check_lease(clp);
  1205. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1206. continue;
  1207. if (status < 0 && status != -NFS4ERR_CB_PATH_DOWN)
  1208. goto out_error;
  1209. }
  1210. /* Initialize or reset the session */
  1211. if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)
  1212. && nfs4_has_session(clp)) {
  1213. status = nfs4_reset_session(clp);
  1214. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1215. continue;
  1216. if (status < 0)
  1217. goto out_error;
  1218. }
  1219. /* First recover reboot state... */
  1220. if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
  1221. status = nfs4_do_reclaim(clp,
  1222. nfs4_reboot_recovery_ops[clp->cl_minorversion]);
  1223. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
  1224. test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
  1225. continue;
  1226. nfs4_state_end_reclaim_reboot(clp);
  1227. if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
  1228. continue;
  1229. if (status < 0)
  1230. goto out_error;
  1231. }
  1232. /* Now recover expired state... */
  1233. if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
  1234. status = nfs4_do_reclaim(clp,
  1235. nfs4_nograce_recovery_ops[clp->cl_minorversion]);
  1236. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
  1237. test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
  1238. test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1239. continue;
  1240. if (status < 0)
  1241. goto out_error;
  1242. }
  1243. if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
  1244. nfs_client_return_marked_delegations(clp);
  1245. continue;
  1246. }
  1247. nfs4_clear_state_manager_bit(clp);
  1248. /* Did we race with an attempt to give us more work? */
  1249. if (clp->cl_state == 0)
  1250. break;
  1251. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  1252. break;
  1253. }
  1254. return;
  1255. out_error:
  1256. printk(KERN_WARNING "Error: state manager failed on NFSv4 server %s"
  1257. " with error %d\n", clp->cl_hostname, -status);
  1258. nfs4_clear_state_manager_bit(clp);
  1259. }
  1260. static int nfs4_run_state_manager(void *ptr)
  1261. {
  1262. struct nfs_client *clp = ptr;
  1263. allow_signal(SIGKILL);
  1264. nfs4_state_manager(clp);
  1265. nfs_put_client(clp);
  1266. module_put_and_exit(0);
  1267. return 0;
  1268. }
  1269. /*
  1270. * Local variables:
  1271. * c-basic-offset: 8
  1272. * End:
  1273. */