nfs4state.c 39 KB

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