nfs4state.c 44 KB

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