nfs4state.c 50 KB

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