nfs4state.c 47 KB

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