nfs4state.c 49 KB

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