nfs4state.c 45 KB

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