nfs4state.c 40 KB

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