nfs4state.c 44 KB

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