nfs4state.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428
  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 <linux/sunrpc/clnt.h>
  53. #include "nfs4_fs.h"
  54. #include "callback.h"
  55. #include "delegation.h"
  56. #include "internal.h"
  57. #include "nfs4session.h"
  58. #include "pnfs.h"
  59. #include "netns.h"
  60. #define NFSDBG_FACILITY NFSDBG_STATE
  61. #define OPENOWNER_POOL_SIZE 8
  62. const nfs4_stateid zero_stateid;
  63. static DEFINE_MUTEX(nfs_clid_init_mutex);
  64. int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
  65. {
  66. struct nfs4_setclientid_res clid = {
  67. .clientid = clp->cl_clientid,
  68. .confirm = clp->cl_confirm,
  69. };
  70. unsigned short port;
  71. int status;
  72. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  73. if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
  74. goto do_confirm;
  75. port = nn->nfs_callback_tcpport;
  76. if (clp->cl_addr.ss_family == AF_INET6)
  77. port = nn->nfs_callback_tcpport6;
  78. status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
  79. if (status != 0)
  80. goto out;
  81. clp->cl_clientid = clid.clientid;
  82. clp->cl_confirm = clid.confirm;
  83. set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  84. do_confirm:
  85. status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
  86. if (status != 0)
  87. goto out;
  88. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  89. nfs4_schedule_state_renewal(clp);
  90. out:
  91. return status;
  92. }
  93. /**
  94. * nfs40_discover_server_trunking - Detect server IP address trunking (mv0)
  95. *
  96. * @clp: nfs_client under test
  97. * @result: OUT: found nfs_client, or clp
  98. * @cred: credential to use for trunking test
  99. *
  100. * Returns zero, a negative errno, or a negative NFS4ERR status.
  101. * If zero is returned, an nfs_client pointer is planted in
  102. * "result".
  103. *
  104. * Note: The returned client may not yet be marked ready.
  105. */
  106. int nfs40_discover_server_trunking(struct nfs_client *clp,
  107. struct nfs_client **result,
  108. struct rpc_cred *cred)
  109. {
  110. struct nfs4_setclientid_res clid = {
  111. .clientid = clp->cl_clientid,
  112. .confirm = clp->cl_confirm,
  113. };
  114. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  115. unsigned short port;
  116. int status;
  117. port = nn->nfs_callback_tcpport;
  118. if (clp->cl_addr.ss_family == AF_INET6)
  119. port = nn->nfs_callback_tcpport6;
  120. status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
  121. if (status != 0)
  122. goto out;
  123. clp->cl_clientid = clid.clientid;
  124. clp->cl_confirm = clid.confirm;
  125. status = nfs40_walk_client_list(clp, result, cred);
  126. if (status == 0) {
  127. /* Sustain the lease, even if it's empty. If the clientid4
  128. * goes stale it's of no use for trunking discovery. */
  129. nfs4_schedule_state_renewal(*result);
  130. }
  131. out:
  132. return status;
  133. }
  134. struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
  135. {
  136. struct rpc_cred *cred = NULL;
  137. if (clp->cl_machine_cred != NULL)
  138. cred = get_rpccred(clp->cl_machine_cred);
  139. return cred;
  140. }
  141. static void nfs4_root_machine_cred(struct nfs_client *clp)
  142. {
  143. struct rpc_cred *cred, *new;
  144. new = rpc_lookup_machine_cred(NULL);
  145. spin_lock(&clp->cl_lock);
  146. cred = clp->cl_machine_cred;
  147. clp->cl_machine_cred = new;
  148. spin_unlock(&clp->cl_lock);
  149. if (cred != NULL)
  150. put_rpccred(cred);
  151. }
  152. static struct rpc_cred *
  153. nfs4_get_renew_cred_server_locked(struct nfs_server *server)
  154. {
  155. struct rpc_cred *cred = NULL;
  156. struct nfs4_state_owner *sp;
  157. struct rb_node *pos;
  158. for (pos = rb_first(&server->state_owners);
  159. pos != NULL;
  160. pos = rb_next(pos)) {
  161. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  162. if (list_empty(&sp->so_states))
  163. continue;
  164. cred = get_rpccred(sp->so_cred);
  165. break;
  166. }
  167. return cred;
  168. }
  169. /**
  170. * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
  171. * @clp: client state handle
  172. *
  173. * Returns an rpc_cred with reference count bumped, or NULL.
  174. * Caller must hold clp->cl_lock.
  175. */
  176. struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
  177. {
  178. struct rpc_cred *cred = NULL;
  179. struct nfs_server *server;
  180. /* Use machine credentials if available */
  181. cred = nfs4_get_machine_cred_locked(clp);
  182. if (cred != NULL)
  183. goto out;
  184. rcu_read_lock();
  185. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  186. cred = nfs4_get_renew_cred_server_locked(server);
  187. if (cred != NULL)
  188. break;
  189. }
  190. rcu_read_unlock();
  191. out:
  192. return cred;
  193. }
  194. static void nfs4_end_drain_slot_table(struct nfs4_slot_table *tbl)
  195. {
  196. if (test_and_clear_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state)) {
  197. spin_lock(&tbl->slot_tbl_lock);
  198. nfs41_wake_slot_table(tbl);
  199. spin_unlock(&tbl->slot_tbl_lock);
  200. }
  201. }
  202. static void nfs4_end_drain_session(struct nfs_client *clp)
  203. {
  204. struct nfs4_session *ses = clp->cl_session;
  205. if (clp->cl_slot_tbl) {
  206. nfs4_end_drain_slot_table(clp->cl_slot_tbl);
  207. return;
  208. }
  209. if (ses != NULL) {
  210. nfs4_end_drain_slot_table(&ses->bc_slot_table);
  211. nfs4_end_drain_slot_table(&ses->fc_slot_table);
  212. }
  213. }
  214. static int nfs4_drain_slot_tbl(struct nfs4_slot_table *tbl)
  215. {
  216. set_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state);
  217. spin_lock(&tbl->slot_tbl_lock);
  218. if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
  219. reinit_completion(&tbl->complete);
  220. spin_unlock(&tbl->slot_tbl_lock);
  221. return wait_for_completion_interruptible(&tbl->complete);
  222. }
  223. spin_unlock(&tbl->slot_tbl_lock);
  224. return 0;
  225. }
  226. static int nfs4_begin_drain_session(struct nfs_client *clp)
  227. {
  228. struct nfs4_session *ses = clp->cl_session;
  229. int ret = 0;
  230. if (clp->cl_slot_tbl)
  231. return nfs4_drain_slot_tbl(clp->cl_slot_tbl);
  232. /* back channel */
  233. ret = nfs4_drain_slot_tbl(&ses->bc_slot_table);
  234. if (ret)
  235. return ret;
  236. /* fore channel */
  237. return nfs4_drain_slot_tbl(&ses->fc_slot_table);
  238. }
  239. #if defined(CONFIG_NFS_V4_1)
  240. static int nfs41_setup_state_renewal(struct nfs_client *clp)
  241. {
  242. int status;
  243. struct nfs_fsinfo fsinfo;
  244. if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
  245. nfs4_schedule_state_renewal(clp);
  246. return 0;
  247. }
  248. status = nfs4_proc_get_lease_time(clp, &fsinfo);
  249. if (status == 0) {
  250. /* Update lease time and schedule renewal */
  251. spin_lock(&clp->cl_lock);
  252. clp->cl_lease_time = fsinfo.lease_time * HZ;
  253. clp->cl_last_renewal = jiffies;
  254. spin_unlock(&clp->cl_lock);
  255. nfs4_schedule_state_renewal(clp);
  256. }
  257. return status;
  258. }
  259. static void nfs41_finish_session_reset(struct nfs_client *clp)
  260. {
  261. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  262. clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  263. /* create_session negotiated new slot table */
  264. clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  265. nfs41_setup_state_renewal(clp);
  266. }
  267. int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
  268. {
  269. int status;
  270. if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
  271. goto do_confirm;
  272. nfs4_begin_drain_session(clp);
  273. status = nfs4_proc_exchange_id(clp, cred);
  274. if (status != 0)
  275. goto out;
  276. set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  277. do_confirm:
  278. status = nfs4_proc_create_session(clp, cred);
  279. if (status != 0)
  280. goto out;
  281. nfs41_finish_session_reset(clp);
  282. nfs_mark_client_ready(clp, NFS_CS_READY);
  283. out:
  284. return status;
  285. }
  286. /**
  287. * nfs41_discover_server_trunking - Detect server IP address trunking (mv1)
  288. *
  289. * @clp: nfs_client under test
  290. * @result: OUT: found nfs_client, or clp
  291. * @cred: credential to use for trunking test
  292. *
  293. * Returns NFS4_OK, a negative errno, or a negative NFS4ERR status.
  294. * If NFS4_OK is returned, an nfs_client pointer is planted in
  295. * "result".
  296. *
  297. * Note: The returned client may not yet be marked ready.
  298. */
  299. int nfs41_discover_server_trunking(struct nfs_client *clp,
  300. struct nfs_client **result,
  301. struct rpc_cred *cred)
  302. {
  303. int status;
  304. status = nfs4_proc_exchange_id(clp, cred);
  305. if (status != NFS4_OK)
  306. return status;
  307. set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  308. return nfs41_walk_client_list(clp, result, cred);
  309. }
  310. #endif /* CONFIG_NFS_V4_1 */
  311. /**
  312. * nfs4_get_clid_cred - Acquire credential for a setclientid operation
  313. * @clp: client state handle
  314. *
  315. * Returns an rpc_cred with reference count bumped, or NULL.
  316. */
  317. struct rpc_cred *nfs4_get_clid_cred(struct nfs_client *clp)
  318. {
  319. struct rpc_cred *cred;
  320. spin_lock(&clp->cl_lock);
  321. cred = nfs4_get_machine_cred_locked(clp);
  322. spin_unlock(&clp->cl_lock);
  323. return cred;
  324. }
  325. static struct nfs4_state_owner *
  326. nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
  327. {
  328. struct rb_node **p = &server->state_owners.rb_node,
  329. *parent = NULL;
  330. struct nfs4_state_owner *sp;
  331. while (*p != NULL) {
  332. parent = *p;
  333. sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
  334. if (cred < sp->so_cred)
  335. p = &parent->rb_left;
  336. else if (cred > sp->so_cred)
  337. p = &parent->rb_right;
  338. else {
  339. if (!list_empty(&sp->so_lru))
  340. list_del_init(&sp->so_lru);
  341. atomic_inc(&sp->so_count);
  342. return sp;
  343. }
  344. }
  345. return NULL;
  346. }
  347. static struct nfs4_state_owner *
  348. nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
  349. {
  350. struct nfs_server *server = new->so_server;
  351. struct rb_node **p = &server->state_owners.rb_node,
  352. *parent = NULL;
  353. struct nfs4_state_owner *sp;
  354. int err;
  355. while (*p != NULL) {
  356. parent = *p;
  357. sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
  358. if (new->so_cred < sp->so_cred)
  359. p = &parent->rb_left;
  360. else if (new->so_cred > sp->so_cred)
  361. p = &parent->rb_right;
  362. else {
  363. if (!list_empty(&sp->so_lru))
  364. list_del_init(&sp->so_lru);
  365. atomic_inc(&sp->so_count);
  366. return sp;
  367. }
  368. }
  369. err = ida_get_new(&server->openowner_id, &new->so_seqid.owner_id);
  370. if (err)
  371. return ERR_PTR(err);
  372. rb_link_node(&new->so_server_node, parent, p);
  373. rb_insert_color(&new->so_server_node, &server->state_owners);
  374. return new;
  375. }
  376. static void
  377. nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
  378. {
  379. struct nfs_server *server = sp->so_server;
  380. if (!RB_EMPTY_NODE(&sp->so_server_node))
  381. rb_erase(&sp->so_server_node, &server->state_owners);
  382. ida_remove(&server->openowner_id, sp->so_seqid.owner_id);
  383. }
  384. static void
  385. nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
  386. {
  387. sc->create_time = ktime_get();
  388. sc->flags = 0;
  389. sc->counter = 0;
  390. spin_lock_init(&sc->lock);
  391. INIT_LIST_HEAD(&sc->list);
  392. rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
  393. }
  394. static void
  395. nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
  396. {
  397. rpc_destroy_wait_queue(&sc->wait);
  398. }
  399. /*
  400. * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
  401. * create a new state_owner.
  402. *
  403. */
  404. static struct nfs4_state_owner *
  405. nfs4_alloc_state_owner(struct nfs_server *server,
  406. struct rpc_cred *cred,
  407. gfp_t gfp_flags)
  408. {
  409. struct nfs4_state_owner *sp;
  410. sp = kzalloc(sizeof(*sp), gfp_flags);
  411. if (!sp)
  412. return NULL;
  413. sp->so_server = server;
  414. sp->so_cred = get_rpccred(cred);
  415. spin_lock_init(&sp->so_lock);
  416. INIT_LIST_HEAD(&sp->so_states);
  417. nfs4_init_seqid_counter(&sp->so_seqid);
  418. atomic_set(&sp->so_count, 1);
  419. INIT_LIST_HEAD(&sp->so_lru);
  420. seqcount_init(&sp->so_reclaim_seqcount);
  421. mutex_init(&sp->so_delegreturn_mutex);
  422. return sp;
  423. }
  424. static void
  425. nfs4_drop_state_owner(struct nfs4_state_owner *sp)
  426. {
  427. struct rb_node *rb_node = &sp->so_server_node;
  428. if (!RB_EMPTY_NODE(rb_node)) {
  429. struct nfs_server *server = sp->so_server;
  430. struct nfs_client *clp = server->nfs_client;
  431. spin_lock(&clp->cl_lock);
  432. if (!RB_EMPTY_NODE(rb_node)) {
  433. rb_erase(rb_node, &server->state_owners);
  434. RB_CLEAR_NODE(rb_node);
  435. }
  436. spin_unlock(&clp->cl_lock);
  437. }
  438. }
  439. static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
  440. {
  441. nfs4_destroy_seqid_counter(&sp->so_seqid);
  442. put_rpccred(sp->so_cred);
  443. kfree(sp);
  444. }
  445. static void nfs4_gc_state_owners(struct nfs_server *server)
  446. {
  447. struct nfs_client *clp = server->nfs_client;
  448. struct nfs4_state_owner *sp, *tmp;
  449. unsigned long time_min, time_max;
  450. LIST_HEAD(doomed);
  451. spin_lock(&clp->cl_lock);
  452. time_max = jiffies;
  453. time_min = (long)time_max - (long)clp->cl_lease_time;
  454. list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
  455. /* NB: LRU is sorted so that oldest is at the head */
  456. if (time_in_range(sp->so_expires, time_min, time_max))
  457. break;
  458. list_move(&sp->so_lru, &doomed);
  459. nfs4_remove_state_owner_locked(sp);
  460. }
  461. spin_unlock(&clp->cl_lock);
  462. list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
  463. list_del(&sp->so_lru);
  464. nfs4_free_state_owner(sp);
  465. }
  466. }
  467. /**
  468. * nfs4_get_state_owner - Look up a state owner given a credential
  469. * @server: nfs_server to search
  470. * @cred: RPC credential to match
  471. *
  472. * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
  473. */
  474. struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
  475. struct rpc_cred *cred,
  476. gfp_t gfp_flags)
  477. {
  478. struct nfs_client *clp = server->nfs_client;
  479. struct nfs4_state_owner *sp, *new;
  480. spin_lock(&clp->cl_lock);
  481. sp = nfs4_find_state_owner_locked(server, cred);
  482. spin_unlock(&clp->cl_lock);
  483. if (sp != NULL)
  484. goto out;
  485. new = nfs4_alloc_state_owner(server, cred, gfp_flags);
  486. if (new == NULL)
  487. goto out;
  488. do {
  489. if (ida_pre_get(&server->openowner_id, gfp_flags) == 0)
  490. break;
  491. spin_lock(&clp->cl_lock);
  492. sp = nfs4_insert_state_owner_locked(new);
  493. spin_unlock(&clp->cl_lock);
  494. } while (sp == ERR_PTR(-EAGAIN));
  495. if (sp != new)
  496. nfs4_free_state_owner(new);
  497. out:
  498. nfs4_gc_state_owners(server);
  499. return sp;
  500. }
  501. /**
  502. * nfs4_put_state_owner - Release a nfs4_state_owner
  503. * @sp: state owner data to release
  504. *
  505. * Note that we keep released state owners on an LRU
  506. * list.
  507. * This caches valid state owners so that they can be
  508. * reused, to avoid the OPEN_CONFIRM on minor version 0.
  509. * It also pins the uniquifier of dropped state owners for
  510. * a while, to ensure that those state owner names are
  511. * never reused.
  512. */
  513. void nfs4_put_state_owner(struct nfs4_state_owner *sp)
  514. {
  515. struct nfs_server *server = sp->so_server;
  516. struct nfs_client *clp = server->nfs_client;
  517. if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
  518. return;
  519. sp->so_expires = jiffies;
  520. list_add_tail(&sp->so_lru, &server->state_owners_lru);
  521. spin_unlock(&clp->cl_lock);
  522. }
  523. /**
  524. * nfs4_purge_state_owners - Release all cached state owners
  525. * @server: nfs_server with cached state owners to release
  526. *
  527. * Called at umount time. Remaining state owners will be on
  528. * the LRU with ref count of zero.
  529. */
  530. void nfs4_purge_state_owners(struct nfs_server *server)
  531. {
  532. struct nfs_client *clp = server->nfs_client;
  533. struct nfs4_state_owner *sp, *tmp;
  534. LIST_HEAD(doomed);
  535. spin_lock(&clp->cl_lock);
  536. list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
  537. list_move(&sp->so_lru, &doomed);
  538. nfs4_remove_state_owner_locked(sp);
  539. }
  540. spin_unlock(&clp->cl_lock);
  541. list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
  542. list_del(&sp->so_lru);
  543. nfs4_free_state_owner(sp);
  544. }
  545. }
  546. static struct nfs4_state *
  547. nfs4_alloc_open_state(void)
  548. {
  549. struct nfs4_state *state;
  550. state = kzalloc(sizeof(*state), GFP_NOFS);
  551. if (!state)
  552. return NULL;
  553. atomic_set(&state->count, 1);
  554. INIT_LIST_HEAD(&state->lock_states);
  555. spin_lock_init(&state->state_lock);
  556. seqlock_init(&state->seqlock);
  557. return state;
  558. }
  559. void
  560. nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
  561. {
  562. if (state->state == fmode)
  563. return;
  564. /* NB! List reordering - see the reclaim code for why. */
  565. if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
  566. if (fmode & FMODE_WRITE)
  567. list_move(&state->open_states, &state->owner->so_states);
  568. else
  569. list_move_tail(&state->open_states, &state->owner->so_states);
  570. }
  571. state->state = fmode;
  572. }
  573. static struct nfs4_state *
  574. __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
  575. {
  576. struct nfs_inode *nfsi = NFS_I(inode);
  577. struct nfs4_state *state;
  578. list_for_each_entry(state, &nfsi->open_states, inode_states) {
  579. if (state->owner != owner)
  580. continue;
  581. if (!nfs4_valid_open_stateid(state))
  582. continue;
  583. if (atomic_inc_not_zero(&state->count))
  584. return state;
  585. }
  586. return NULL;
  587. }
  588. static void
  589. nfs4_free_open_state(struct nfs4_state *state)
  590. {
  591. kfree(state);
  592. }
  593. struct nfs4_state *
  594. nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
  595. {
  596. struct nfs4_state *state, *new;
  597. struct nfs_inode *nfsi = NFS_I(inode);
  598. spin_lock(&inode->i_lock);
  599. state = __nfs4_find_state_byowner(inode, owner);
  600. spin_unlock(&inode->i_lock);
  601. if (state)
  602. goto out;
  603. new = nfs4_alloc_open_state();
  604. spin_lock(&owner->so_lock);
  605. spin_lock(&inode->i_lock);
  606. state = __nfs4_find_state_byowner(inode, owner);
  607. if (state == NULL && new != NULL) {
  608. state = new;
  609. state->owner = owner;
  610. atomic_inc(&owner->so_count);
  611. list_add(&state->inode_states, &nfsi->open_states);
  612. ihold(inode);
  613. state->inode = inode;
  614. spin_unlock(&inode->i_lock);
  615. /* Note: The reclaim code dictates that we add stateless
  616. * and read-only stateids to the end of the list */
  617. list_add_tail(&state->open_states, &owner->so_states);
  618. spin_unlock(&owner->so_lock);
  619. } else {
  620. spin_unlock(&inode->i_lock);
  621. spin_unlock(&owner->so_lock);
  622. if (new)
  623. nfs4_free_open_state(new);
  624. }
  625. out:
  626. return state;
  627. }
  628. void nfs4_put_open_state(struct nfs4_state *state)
  629. {
  630. struct inode *inode = state->inode;
  631. struct nfs4_state_owner *owner = state->owner;
  632. if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
  633. return;
  634. spin_lock(&inode->i_lock);
  635. list_del(&state->inode_states);
  636. list_del(&state->open_states);
  637. spin_unlock(&inode->i_lock);
  638. spin_unlock(&owner->so_lock);
  639. iput(inode);
  640. nfs4_free_open_state(state);
  641. nfs4_put_state_owner(owner);
  642. }
  643. /*
  644. * Close the current file.
  645. */
  646. static void __nfs4_close(struct nfs4_state *state,
  647. fmode_t fmode, gfp_t gfp_mask, int wait)
  648. {
  649. struct nfs4_state_owner *owner = state->owner;
  650. int call_close = 0;
  651. fmode_t newstate;
  652. atomic_inc(&owner->so_count);
  653. /* Protect against nfs4_find_state() */
  654. spin_lock(&owner->so_lock);
  655. switch (fmode & (FMODE_READ | FMODE_WRITE)) {
  656. case FMODE_READ:
  657. state->n_rdonly--;
  658. break;
  659. case FMODE_WRITE:
  660. state->n_wronly--;
  661. break;
  662. case FMODE_READ|FMODE_WRITE:
  663. state->n_rdwr--;
  664. }
  665. newstate = FMODE_READ|FMODE_WRITE;
  666. if (state->n_rdwr == 0) {
  667. if (state->n_rdonly == 0) {
  668. newstate &= ~FMODE_READ;
  669. call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
  670. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  671. }
  672. if (state->n_wronly == 0) {
  673. newstate &= ~FMODE_WRITE;
  674. call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
  675. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  676. }
  677. if (newstate == 0)
  678. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  679. }
  680. nfs4_state_set_mode_locked(state, newstate);
  681. spin_unlock(&owner->so_lock);
  682. if (!call_close) {
  683. nfs4_put_open_state(state);
  684. nfs4_put_state_owner(owner);
  685. } else
  686. nfs4_do_close(state, gfp_mask, wait);
  687. }
  688. void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
  689. {
  690. __nfs4_close(state, fmode, GFP_NOFS, 0);
  691. }
  692. void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
  693. {
  694. __nfs4_close(state, fmode, GFP_KERNEL, 1);
  695. }
  696. /*
  697. * Search the state->lock_states for an existing lock_owner
  698. * that is compatible with current->files
  699. */
  700. static struct nfs4_lock_state *
  701. __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
  702. {
  703. struct nfs4_lock_state *pos;
  704. list_for_each_entry(pos, &state->lock_states, ls_locks) {
  705. if (pos->ls_owner != fl_owner)
  706. continue;
  707. atomic_inc(&pos->ls_count);
  708. return pos;
  709. }
  710. return NULL;
  711. }
  712. /*
  713. * Return a compatible lock_state. If no initialized lock_state structure
  714. * exists, return an uninitialized one.
  715. *
  716. */
  717. static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
  718. {
  719. struct nfs4_lock_state *lsp;
  720. struct nfs_server *server = state->owner->so_server;
  721. lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
  722. if (lsp == NULL)
  723. return NULL;
  724. nfs4_init_seqid_counter(&lsp->ls_seqid);
  725. atomic_set(&lsp->ls_count, 1);
  726. lsp->ls_state = state;
  727. lsp->ls_owner = fl_owner;
  728. lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS);
  729. if (lsp->ls_seqid.owner_id < 0)
  730. goto out_free;
  731. INIT_LIST_HEAD(&lsp->ls_locks);
  732. return lsp;
  733. out_free:
  734. kfree(lsp);
  735. return NULL;
  736. }
  737. void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
  738. {
  739. ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id);
  740. nfs4_destroy_seqid_counter(&lsp->ls_seqid);
  741. kfree(lsp);
  742. }
  743. /*
  744. * Return a compatible lock_state. If no initialized lock_state structure
  745. * exists, return an uninitialized one.
  746. *
  747. */
  748. static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
  749. {
  750. struct nfs4_lock_state *lsp, *new = NULL;
  751. for(;;) {
  752. spin_lock(&state->state_lock);
  753. lsp = __nfs4_find_lock_state(state, owner);
  754. if (lsp != NULL)
  755. break;
  756. if (new != NULL) {
  757. list_add(&new->ls_locks, &state->lock_states);
  758. set_bit(LK_STATE_IN_USE, &state->flags);
  759. lsp = new;
  760. new = NULL;
  761. break;
  762. }
  763. spin_unlock(&state->state_lock);
  764. new = nfs4_alloc_lock_state(state, owner);
  765. if (new == NULL)
  766. return NULL;
  767. }
  768. spin_unlock(&state->state_lock);
  769. if (new != NULL)
  770. nfs4_free_lock_state(state->owner->so_server, new);
  771. return lsp;
  772. }
  773. /*
  774. * Release reference to lock_state, and free it if we see that
  775. * it is no longer in use
  776. */
  777. void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
  778. {
  779. struct nfs_server *server;
  780. struct nfs4_state *state;
  781. if (lsp == NULL)
  782. return;
  783. state = lsp->ls_state;
  784. if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
  785. return;
  786. list_del(&lsp->ls_locks);
  787. if (list_empty(&state->lock_states))
  788. clear_bit(LK_STATE_IN_USE, &state->flags);
  789. spin_unlock(&state->state_lock);
  790. server = state->owner->so_server;
  791. if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
  792. struct nfs_client *clp = server->nfs_client;
  793. clp->cl_mvops->free_lock_state(server, lsp);
  794. } else
  795. nfs4_free_lock_state(server, lsp);
  796. }
  797. static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
  798. {
  799. struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
  800. dst->fl_u.nfs4_fl.owner = lsp;
  801. atomic_inc(&lsp->ls_count);
  802. }
  803. static void nfs4_fl_release_lock(struct file_lock *fl)
  804. {
  805. nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
  806. }
  807. static const struct file_lock_operations nfs4_fl_lock_ops = {
  808. .fl_copy_lock = nfs4_fl_copy_lock,
  809. .fl_release_private = nfs4_fl_release_lock,
  810. };
  811. int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
  812. {
  813. struct nfs4_lock_state *lsp;
  814. if (fl->fl_ops != NULL)
  815. return 0;
  816. lsp = nfs4_get_lock_state(state, fl->fl_owner);
  817. if (lsp == NULL)
  818. return -ENOMEM;
  819. fl->fl_u.nfs4_fl.owner = lsp;
  820. fl->fl_ops = &nfs4_fl_lock_ops;
  821. return 0;
  822. }
  823. static int nfs4_copy_lock_stateid(nfs4_stateid *dst,
  824. struct nfs4_state *state,
  825. const struct nfs_lockowner *lockowner)
  826. {
  827. struct nfs4_lock_state *lsp;
  828. fl_owner_t fl_owner;
  829. int ret = -ENOENT;
  830. if (lockowner == NULL)
  831. goto out;
  832. if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
  833. goto out;
  834. fl_owner = lockowner->l_owner;
  835. spin_lock(&state->state_lock);
  836. lsp = __nfs4_find_lock_state(state, fl_owner);
  837. if (lsp && test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
  838. ret = -EIO;
  839. else if (lsp != NULL && test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) {
  840. nfs4_stateid_copy(dst, &lsp->ls_stateid);
  841. ret = 0;
  842. }
  843. spin_unlock(&state->state_lock);
  844. nfs4_put_lock_state(lsp);
  845. out:
  846. return ret;
  847. }
  848. static void nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
  849. {
  850. const nfs4_stateid *src;
  851. int seq;
  852. do {
  853. src = &zero_stateid;
  854. seq = read_seqbegin(&state->seqlock);
  855. if (test_bit(NFS_OPEN_STATE, &state->flags))
  856. src = &state->open_stateid;
  857. nfs4_stateid_copy(dst, src);
  858. } while (read_seqretry(&state->seqlock, seq));
  859. }
  860. /*
  861. * Byte-range lock aware utility to initialize the stateid of read/write
  862. * requests.
  863. */
  864. int nfs4_select_rw_stateid(nfs4_stateid *dst, struct nfs4_state *state,
  865. fmode_t fmode, const struct nfs_lockowner *lockowner)
  866. {
  867. int ret = nfs4_copy_lock_stateid(dst, state, lockowner);
  868. if (ret == -EIO)
  869. /* A lost lock - don't even consider delegations */
  870. goto out;
  871. /* returns true if delegation stateid found and copied */
  872. if (nfs4_copy_delegation_stateid(dst, state->inode, fmode)) {
  873. ret = 0;
  874. goto out;
  875. }
  876. if (ret != -ENOENT)
  877. /* nfs4_copy_delegation_stateid() didn't over-write
  878. * dst, so it still has the lock stateid which we now
  879. * choose to use.
  880. */
  881. goto out;
  882. nfs4_copy_open_stateid(dst, state);
  883. ret = 0;
  884. out:
  885. if (nfs_server_capable(state->inode, NFS_CAP_STATEID_NFSV41))
  886. dst->seqid = 0;
  887. return ret;
  888. }
  889. struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
  890. {
  891. struct nfs_seqid *new;
  892. new = kmalloc(sizeof(*new), gfp_mask);
  893. if (new != NULL) {
  894. new->sequence = counter;
  895. INIT_LIST_HEAD(&new->list);
  896. new->task = NULL;
  897. }
  898. return new;
  899. }
  900. void nfs_release_seqid(struct nfs_seqid *seqid)
  901. {
  902. struct nfs_seqid_counter *sequence;
  903. if (list_empty(&seqid->list))
  904. return;
  905. sequence = seqid->sequence;
  906. spin_lock(&sequence->lock);
  907. list_del_init(&seqid->list);
  908. if (!list_empty(&sequence->list)) {
  909. struct nfs_seqid *next;
  910. next = list_first_entry(&sequence->list,
  911. struct nfs_seqid, list);
  912. rpc_wake_up_queued_task(&sequence->wait, next->task);
  913. }
  914. spin_unlock(&sequence->lock);
  915. }
  916. void nfs_free_seqid(struct nfs_seqid *seqid)
  917. {
  918. nfs_release_seqid(seqid);
  919. kfree(seqid);
  920. }
  921. /*
  922. * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
  923. * failed with a seqid incrementing error -
  924. * see comments nfs4.h:seqid_mutating_error()
  925. */
  926. static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
  927. {
  928. switch (status) {
  929. case 0:
  930. break;
  931. case -NFS4ERR_BAD_SEQID:
  932. if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
  933. return;
  934. pr_warn_ratelimited("NFS: v4 server returned a bad"
  935. " sequence-id error on an"
  936. " unconfirmed sequence %p!\n",
  937. seqid->sequence);
  938. case -NFS4ERR_STALE_CLIENTID:
  939. case -NFS4ERR_STALE_STATEID:
  940. case -NFS4ERR_BAD_STATEID:
  941. case -NFS4ERR_BADXDR:
  942. case -NFS4ERR_RESOURCE:
  943. case -NFS4ERR_NOFILEHANDLE:
  944. /* Non-seqid mutating errors */
  945. return;
  946. };
  947. /*
  948. * Note: no locking needed as we are guaranteed to be first
  949. * on the sequence list
  950. */
  951. seqid->sequence->counter++;
  952. }
  953. void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
  954. {
  955. struct nfs4_state_owner *sp = container_of(seqid->sequence,
  956. struct nfs4_state_owner, so_seqid);
  957. struct nfs_server *server = sp->so_server;
  958. if (status == -NFS4ERR_BAD_SEQID)
  959. nfs4_drop_state_owner(sp);
  960. if (!nfs4_has_session(server->nfs_client))
  961. nfs_increment_seqid(status, seqid);
  962. }
  963. /*
  964. * Increment the seqid if the LOCK/LOCKU succeeded, or
  965. * failed with a seqid incrementing error -
  966. * see comments nfs4.h:seqid_mutating_error()
  967. */
  968. void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
  969. {
  970. nfs_increment_seqid(status, seqid);
  971. }
  972. int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
  973. {
  974. struct nfs_seqid_counter *sequence = seqid->sequence;
  975. int status = 0;
  976. spin_lock(&sequence->lock);
  977. seqid->task = task;
  978. if (list_empty(&seqid->list))
  979. list_add_tail(&seqid->list, &sequence->list);
  980. if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
  981. goto unlock;
  982. rpc_sleep_on(&sequence->wait, task, NULL);
  983. status = -EAGAIN;
  984. unlock:
  985. spin_unlock(&sequence->lock);
  986. return status;
  987. }
  988. static int nfs4_run_state_manager(void *);
  989. static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
  990. {
  991. smp_mb__before_atomic();
  992. clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
  993. smp_mb__after_atomic();
  994. wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
  995. rpc_wake_up(&clp->cl_rpcwaitq);
  996. }
  997. /*
  998. * Schedule the nfs_client asynchronous state management routine
  999. */
  1000. void nfs4_schedule_state_manager(struct nfs_client *clp)
  1001. {
  1002. struct task_struct *task;
  1003. char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
  1004. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  1005. return;
  1006. __module_get(THIS_MODULE);
  1007. atomic_inc(&clp->cl_count);
  1008. /* The rcu_read_lock() is not strictly necessary, as the state
  1009. * manager is the only thread that ever changes the rpc_xprt
  1010. * after it's initialized. At this point, we're single threaded. */
  1011. rcu_read_lock();
  1012. snprintf(buf, sizeof(buf), "%s-manager",
  1013. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  1014. rcu_read_unlock();
  1015. task = kthread_run(nfs4_run_state_manager, clp, "%s", buf);
  1016. if (IS_ERR(task)) {
  1017. printk(KERN_ERR "%s: kthread_run: %ld\n",
  1018. __func__, PTR_ERR(task));
  1019. nfs4_clear_state_manager_bit(clp);
  1020. nfs_put_client(clp);
  1021. module_put(THIS_MODULE);
  1022. }
  1023. }
  1024. /*
  1025. * Schedule a lease recovery attempt
  1026. */
  1027. void nfs4_schedule_lease_recovery(struct nfs_client *clp)
  1028. {
  1029. if (!clp)
  1030. return;
  1031. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1032. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1033. dprintk("%s: scheduling lease recovery for server %s\n", __func__,
  1034. clp->cl_hostname);
  1035. nfs4_schedule_state_manager(clp);
  1036. }
  1037. EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
  1038. /**
  1039. * nfs4_schedule_migration_recovery - trigger migration recovery
  1040. *
  1041. * @server: FSID that is migrating
  1042. *
  1043. * Returns zero if recovery has started, otherwise a negative NFS4ERR
  1044. * value is returned.
  1045. */
  1046. int nfs4_schedule_migration_recovery(const struct nfs_server *server)
  1047. {
  1048. struct nfs_client *clp = server->nfs_client;
  1049. if (server->fh_expire_type != NFS4_FH_PERSISTENT) {
  1050. pr_err("NFS: volatile file handles not supported (server %s)\n",
  1051. clp->cl_hostname);
  1052. return -NFS4ERR_IO;
  1053. }
  1054. if (test_bit(NFS_MIG_FAILED, &server->mig_status))
  1055. return -NFS4ERR_IO;
  1056. dprintk("%s: scheduling migration recovery for (%llx:%llx) on %s\n",
  1057. __func__,
  1058. (unsigned long long)server->fsid.major,
  1059. (unsigned long long)server->fsid.minor,
  1060. clp->cl_hostname);
  1061. set_bit(NFS_MIG_IN_TRANSITION,
  1062. &((struct nfs_server *)server)->mig_status);
  1063. set_bit(NFS4CLNT_MOVED, &clp->cl_state);
  1064. nfs4_schedule_state_manager(clp);
  1065. return 0;
  1066. }
  1067. EXPORT_SYMBOL_GPL(nfs4_schedule_migration_recovery);
  1068. /**
  1069. * nfs4_schedule_lease_moved_recovery - start lease-moved recovery
  1070. *
  1071. * @clp: server to check for moved leases
  1072. *
  1073. */
  1074. void nfs4_schedule_lease_moved_recovery(struct nfs_client *clp)
  1075. {
  1076. dprintk("%s: scheduling lease-moved recovery for client ID %llx on %s\n",
  1077. __func__, clp->cl_clientid, clp->cl_hostname);
  1078. set_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state);
  1079. nfs4_schedule_state_manager(clp);
  1080. }
  1081. EXPORT_SYMBOL_GPL(nfs4_schedule_lease_moved_recovery);
  1082. int nfs4_wait_clnt_recover(struct nfs_client *clp)
  1083. {
  1084. int res;
  1085. might_sleep();
  1086. atomic_inc(&clp->cl_count);
  1087. res = wait_on_bit_action(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
  1088. nfs_wait_bit_killable, TASK_KILLABLE);
  1089. if (res)
  1090. goto out;
  1091. if (clp->cl_cons_state < 0)
  1092. res = clp->cl_cons_state;
  1093. out:
  1094. nfs_put_client(clp);
  1095. return res;
  1096. }
  1097. int nfs4_client_recover_expired_lease(struct nfs_client *clp)
  1098. {
  1099. unsigned int loop;
  1100. int ret;
  1101. for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
  1102. ret = nfs4_wait_clnt_recover(clp);
  1103. if (ret != 0)
  1104. break;
  1105. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
  1106. !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
  1107. break;
  1108. nfs4_schedule_state_manager(clp);
  1109. ret = -EIO;
  1110. }
  1111. return ret;
  1112. }
  1113. /*
  1114. * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
  1115. * @clp: client to process
  1116. *
  1117. * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
  1118. * resend of the SETCLIENTID and hence re-establish the
  1119. * callback channel. Then return all existing delegations.
  1120. */
  1121. static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
  1122. {
  1123. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1124. nfs_expire_all_delegations(clp);
  1125. dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
  1126. clp->cl_hostname);
  1127. }
  1128. void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
  1129. {
  1130. nfs40_handle_cb_pathdown(clp);
  1131. nfs4_schedule_state_manager(clp);
  1132. }
  1133. static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
  1134. {
  1135. set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1136. /* Don't recover state that expired before the reboot */
  1137. if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
  1138. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1139. return 0;
  1140. }
  1141. set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
  1142. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  1143. return 1;
  1144. }
  1145. int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
  1146. {
  1147. set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
  1148. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1149. set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
  1150. set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
  1151. return 1;
  1152. }
  1153. int nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
  1154. {
  1155. struct nfs_client *clp = server->nfs_client;
  1156. if (!nfs4_valid_open_stateid(state))
  1157. return -EBADF;
  1158. nfs4_state_mark_reclaim_nograce(clp, state);
  1159. dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
  1160. clp->cl_hostname);
  1161. nfs4_schedule_state_manager(clp);
  1162. return 0;
  1163. }
  1164. EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
  1165. void nfs_inode_find_state_and_recover(struct inode *inode,
  1166. const nfs4_stateid *stateid)
  1167. {
  1168. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  1169. struct nfs_inode *nfsi = NFS_I(inode);
  1170. struct nfs_open_context *ctx;
  1171. struct nfs4_state *state;
  1172. bool found = false;
  1173. spin_lock(&inode->i_lock);
  1174. list_for_each_entry(ctx, &nfsi->open_files, list) {
  1175. state = ctx->state;
  1176. if (state == NULL)
  1177. continue;
  1178. if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
  1179. continue;
  1180. if (!nfs4_stateid_match(&state->stateid, stateid))
  1181. continue;
  1182. nfs4_state_mark_reclaim_nograce(clp, state);
  1183. found = true;
  1184. }
  1185. spin_unlock(&inode->i_lock);
  1186. if (found)
  1187. nfs4_schedule_state_manager(clp);
  1188. }
  1189. static void nfs4_state_mark_open_context_bad(struct nfs4_state *state)
  1190. {
  1191. struct inode *inode = state->inode;
  1192. struct nfs_inode *nfsi = NFS_I(inode);
  1193. struct nfs_open_context *ctx;
  1194. spin_lock(&inode->i_lock);
  1195. list_for_each_entry(ctx, &nfsi->open_files, list) {
  1196. if (ctx->state != state)
  1197. continue;
  1198. set_bit(NFS_CONTEXT_BAD, &ctx->flags);
  1199. }
  1200. spin_unlock(&inode->i_lock);
  1201. }
  1202. static void nfs4_state_mark_recovery_failed(struct nfs4_state *state, int error)
  1203. {
  1204. set_bit(NFS_STATE_RECOVERY_FAILED, &state->flags);
  1205. nfs4_state_mark_open_context_bad(state);
  1206. }
  1207. static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
  1208. {
  1209. struct inode *inode = state->inode;
  1210. struct nfs_inode *nfsi = NFS_I(inode);
  1211. struct file_lock *fl;
  1212. int status = 0;
  1213. if (inode->i_flock == NULL)
  1214. return 0;
  1215. /* Guard against delegation returns and new lock/unlock calls */
  1216. down_write(&nfsi->rwsem);
  1217. /* Protect inode->i_flock using the BKL */
  1218. spin_lock(&inode->i_lock);
  1219. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  1220. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  1221. continue;
  1222. if (nfs_file_open_context(fl->fl_file)->state != state)
  1223. continue;
  1224. spin_unlock(&inode->i_lock);
  1225. status = ops->recover_lock(state, fl);
  1226. switch (status) {
  1227. case 0:
  1228. break;
  1229. case -ESTALE:
  1230. case -NFS4ERR_ADMIN_REVOKED:
  1231. case -NFS4ERR_STALE_STATEID:
  1232. case -NFS4ERR_BAD_STATEID:
  1233. case -NFS4ERR_EXPIRED:
  1234. case -NFS4ERR_NO_GRACE:
  1235. case -NFS4ERR_STALE_CLIENTID:
  1236. case -NFS4ERR_BADSESSION:
  1237. case -NFS4ERR_BADSLOT:
  1238. case -NFS4ERR_BAD_HIGH_SLOT:
  1239. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1240. goto out;
  1241. default:
  1242. printk(KERN_ERR "NFS: %s: unhandled error %d\n",
  1243. __func__, status);
  1244. case -ENOMEM:
  1245. case -NFS4ERR_DENIED:
  1246. case -NFS4ERR_RECLAIM_BAD:
  1247. case -NFS4ERR_RECLAIM_CONFLICT:
  1248. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  1249. status = 0;
  1250. }
  1251. spin_lock(&inode->i_lock);
  1252. }
  1253. spin_unlock(&inode->i_lock);
  1254. out:
  1255. up_write(&nfsi->rwsem);
  1256. return status;
  1257. }
  1258. static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
  1259. {
  1260. struct nfs4_state *state;
  1261. struct nfs4_lock_state *lock;
  1262. int status = 0;
  1263. /* Note: we rely on the sp->so_states list being ordered
  1264. * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
  1265. * states first.
  1266. * This is needed to ensure that the server won't give us any
  1267. * read delegations that we have to return if, say, we are
  1268. * recovering after a network partition or a reboot from a
  1269. * server that doesn't support a grace period.
  1270. */
  1271. spin_lock(&sp->so_lock);
  1272. raw_write_seqcount_begin(&sp->so_reclaim_seqcount);
  1273. restart:
  1274. list_for_each_entry(state, &sp->so_states, open_states) {
  1275. if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
  1276. continue;
  1277. if (!nfs4_valid_open_stateid(state))
  1278. continue;
  1279. if (state->state == 0)
  1280. continue;
  1281. atomic_inc(&state->count);
  1282. spin_unlock(&sp->so_lock);
  1283. status = ops->recover_open(sp, state);
  1284. if (status >= 0) {
  1285. status = nfs4_reclaim_locks(state, ops);
  1286. if (status >= 0) {
  1287. if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  1288. spin_lock(&state->state_lock);
  1289. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  1290. if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags))
  1291. pr_warn_ratelimited("NFS: "
  1292. "%s: Lock reclaim "
  1293. "failed!\n", __func__);
  1294. }
  1295. spin_unlock(&state->state_lock);
  1296. }
  1297. nfs4_put_open_state(state);
  1298. spin_lock(&sp->so_lock);
  1299. goto restart;
  1300. }
  1301. }
  1302. switch (status) {
  1303. default:
  1304. printk(KERN_ERR "NFS: %s: unhandled error %d\n",
  1305. __func__, status);
  1306. case -ENOENT:
  1307. case -ENOMEM:
  1308. case -ESTALE:
  1309. /* Open state on this file cannot be recovered */
  1310. nfs4_state_mark_recovery_failed(state, status);
  1311. break;
  1312. case -EAGAIN:
  1313. ssleep(1);
  1314. case -NFS4ERR_ADMIN_REVOKED:
  1315. case -NFS4ERR_STALE_STATEID:
  1316. case -NFS4ERR_BAD_STATEID:
  1317. case -NFS4ERR_RECLAIM_BAD:
  1318. case -NFS4ERR_RECLAIM_CONFLICT:
  1319. nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
  1320. break;
  1321. case -NFS4ERR_EXPIRED:
  1322. case -NFS4ERR_NO_GRACE:
  1323. nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
  1324. case -NFS4ERR_STALE_CLIENTID:
  1325. case -NFS4ERR_BADSESSION:
  1326. case -NFS4ERR_BADSLOT:
  1327. case -NFS4ERR_BAD_HIGH_SLOT:
  1328. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1329. goto out_err;
  1330. }
  1331. nfs4_put_open_state(state);
  1332. spin_lock(&sp->so_lock);
  1333. goto restart;
  1334. }
  1335. raw_write_seqcount_end(&sp->so_reclaim_seqcount);
  1336. spin_unlock(&sp->so_lock);
  1337. return 0;
  1338. out_err:
  1339. nfs4_put_open_state(state);
  1340. spin_lock(&sp->so_lock);
  1341. raw_write_seqcount_end(&sp->so_reclaim_seqcount);
  1342. spin_unlock(&sp->so_lock);
  1343. return status;
  1344. }
  1345. static void nfs4_clear_open_state(struct nfs4_state *state)
  1346. {
  1347. struct nfs4_lock_state *lock;
  1348. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1349. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1350. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1351. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1352. spin_lock(&state->state_lock);
  1353. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  1354. lock->ls_seqid.flags = 0;
  1355. clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
  1356. }
  1357. spin_unlock(&state->state_lock);
  1358. }
  1359. static void nfs4_reset_seqids(struct nfs_server *server,
  1360. int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  1361. {
  1362. struct nfs_client *clp = server->nfs_client;
  1363. struct nfs4_state_owner *sp;
  1364. struct rb_node *pos;
  1365. struct nfs4_state *state;
  1366. spin_lock(&clp->cl_lock);
  1367. for (pos = rb_first(&server->state_owners);
  1368. pos != NULL;
  1369. pos = rb_next(pos)) {
  1370. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  1371. sp->so_seqid.flags = 0;
  1372. spin_lock(&sp->so_lock);
  1373. list_for_each_entry(state, &sp->so_states, open_states) {
  1374. if (mark_reclaim(clp, state))
  1375. nfs4_clear_open_state(state);
  1376. }
  1377. spin_unlock(&sp->so_lock);
  1378. }
  1379. spin_unlock(&clp->cl_lock);
  1380. }
  1381. static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
  1382. int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  1383. {
  1384. struct nfs_server *server;
  1385. rcu_read_lock();
  1386. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1387. nfs4_reset_seqids(server, mark_reclaim);
  1388. rcu_read_unlock();
  1389. }
  1390. static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
  1391. {
  1392. /* Mark all delegations for reclaim */
  1393. nfs_delegation_mark_reclaim(clp);
  1394. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
  1395. }
  1396. static void nfs4_reclaim_complete(struct nfs_client *clp,
  1397. const struct nfs4_state_recovery_ops *ops,
  1398. struct rpc_cred *cred)
  1399. {
  1400. /* Notify the server we're done reclaiming our state */
  1401. if (ops->reclaim_complete)
  1402. (void)ops->reclaim_complete(clp, cred);
  1403. }
  1404. static void nfs4_clear_reclaim_server(struct nfs_server *server)
  1405. {
  1406. struct nfs_client *clp = server->nfs_client;
  1407. struct nfs4_state_owner *sp;
  1408. struct rb_node *pos;
  1409. struct nfs4_state *state;
  1410. spin_lock(&clp->cl_lock);
  1411. for (pos = rb_first(&server->state_owners);
  1412. pos != NULL;
  1413. pos = rb_next(pos)) {
  1414. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  1415. spin_lock(&sp->so_lock);
  1416. list_for_each_entry(state, &sp->so_states, open_states) {
  1417. if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
  1418. &state->flags))
  1419. continue;
  1420. nfs4_state_mark_reclaim_nograce(clp, state);
  1421. }
  1422. spin_unlock(&sp->so_lock);
  1423. }
  1424. spin_unlock(&clp->cl_lock);
  1425. }
  1426. static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
  1427. {
  1428. struct nfs_server *server;
  1429. if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1430. return 0;
  1431. rcu_read_lock();
  1432. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1433. nfs4_clear_reclaim_server(server);
  1434. rcu_read_unlock();
  1435. nfs_delegation_reap_unclaimed(clp);
  1436. return 1;
  1437. }
  1438. static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
  1439. {
  1440. const struct nfs4_state_recovery_ops *ops;
  1441. struct rpc_cred *cred;
  1442. if (!nfs4_state_clear_reclaim_reboot(clp))
  1443. return;
  1444. ops = clp->cl_mvops->reboot_recovery_ops;
  1445. cred = nfs4_get_clid_cred(clp);
  1446. nfs4_reclaim_complete(clp, ops, cred);
  1447. put_rpccred(cred);
  1448. }
  1449. static void nfs_delegation_clear_all(struct nfs_client *clp)
  1450. {
  1451. nfs_delegation_mark_reclaim(clp);
  1452. nfs_delegation_reap_unclaimed(clp);
  1453. }
  1454. static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
  1455. {
  1456. nfs_delegation_clear_all(clp);
  1457. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
  1458. }
  1459. static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
  1460. {
  1461. switch (error) {
  1462. case 0:
  1463. break;
  1464. case -NFS4ERR_CB_PATH_DOWN:
  1465. nfs40_handle_cb_pathdown(clp);
  1466. break;
  1467. case -NFS4ERR_NO_GRACE:
  1468. nfs4_state_end_reclaim_reboot(clp);
  1469. break;
  1470. case -NFS4ERR_STALE_CLIENTID:
  1471. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1472. nfs4_state_clear_reclaim_reboot(clp);
  1473. nfs4_state_start_reclaim_reboot(clp);
  1474. break;
  1475. case -NFS4ERR_EXPIRED:
  1476. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1477. nfs4_state_start_reclaim_nograce(clp);
  1478. break;
  1479. case -NFS4ERR_BADSESSION:
  1480. case -NFS4ERR_BADSLOT:
  1481. case -NFS4ERR_BAD_HIGH_SLOT:
  1482. case -NFS4ERR_DEADSESSION:
  1483. case -NFS4ERR_SEQ_FALSE_RETRY:
  1484. case -NFS4ERR_SEQ_MISORDERED:
  1485. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1486. /* Zero session reset errors */
  1487. break;
  1488. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1489. set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  1490. break;
  1491. default:
  1492. dprintk("%s: failed to handle error %d for server %s\n",
  1493. __func__, error, clp->cl_hostname);
  1494. return error;
  1495. }
  1496. dprintk("%s: handled error %d for server %s\n", __func__, error,
  1497. clp->cl_hostname);
  1498. return 0;
  1499. }
  1500. static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
  1501. {
  1502. struct nfs4_state_owner *sp;
  1503. struct nfs_server *server;
  1504. struct rb_node *pos;
  1505. int status = 0;
  1506. restart:
  1507. rcu_read_lock();
  1508. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  1509. nfs4_purge_state_owners(server);
  1510. spin_lock(&clp->cl_lock);
  1511. for (pos = rb_first(&server->state_owners);
  1512. pos != NULL;
  1513. pos = rb_next(pos)) {
  1514. sp = rb_entry(pos,
  1515. struct nfs4_state_owner, so_server_node);
  1516. if (!test_and_clear_bit(ops->owner_flag_bit,
  1517. &sp->so_flags))
  1518. continue;
  1519. atomic_inc(&sp->so_count);
  1520. spin_unlock(&clp->cl_lock);
  1521. rcu_read_unlock();
  1522. status = nfs4_reclaim_open_state(sp, ops);
  1523. if (status < 0) {
  1524. set_bit(ops->owner_flag_bit, &sp->so_flags);
  1525. nfs4_put_state_owner(sp);
  1526. status = nfs4_recovery_handle_error(clp, status);
  1527. return (status != 0) ? status : -EAGAIN;
  1528. }
  1529. nfs4_put_state_owner(sp);
  1530. goto restart;
  1531. }
  1532. spin_unlock(&clp->cl_lock);
  1533. }
  1534. rcu_read_unlock();
  1535. return 0;
  1536. }
  1537. static int nfs4_check_lease(struct nfs_client *clp)
  1538. {
  1539. struct rpc_cred *cred;
  1540. const struct nfs4_state_maintenance_ops *ops =
  1541. clp->cl_mvops->state_renewal_ops;
  1542. int status;
  1543. /* Is the client already known to have an expired lease? */
  1544. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1545. return 0;
  1546. spin_lock(&clp->cl_lock);
  1547. cred = ops->get_state_renewal_cred_locked(clp);
  1548. spin_unlock(&clp->cl_lock);
  1549. if (cred == NULL) {
  1550. cred = nfs4_get_clid_cred(clp);
  1551. status = -ENOKEY;
  1552. if (cred == NULL)
  1553. goto out;
  1554. }
  1555. status = ops->renew_lease(clp, cred);
  1556. put_rpccred(cred);
  1557. if (status == -ETIMEDOUT) {
  1558. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1559. return 0;
  1560. }
  1561. out:
  1562. return nfs4_recovery_handle_error(clp, status);
  1563. }
  1564. /* Set NFS4CLNT_LEASE_EXPIRED and reclaim reboot state for all v4.0 errors
  1565. * and for recoverable errors on EXCHANGE_ID for v4.1
  1566. */
  1567. static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
  1568. {
  1569. switch (status) {
  1570. case -NFS4ERR_SEQ_MISORDERED:
  1571. if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
  1572. return -ESERVERFAULT;
  1573. /* Lease confirmation error: retry after purging the lease */
  1574. ssleep(1);
  1575. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1576. break;
  1577. case -NFS4ERR_STALE_CLIENTID:
  1578. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1579. nfs4_state_start_reclaim_reboot(clp);
  1580. break;
  1581. case -NFS4ERR_CLID_INUSE:
  1582. pr_err("NFS: Server %s reports our clientid is in use\n",
  1583. clp->cl_hostname);
  1584. nfs_mark_client_ready(clp, -EPERM);
  1585. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1586. return -EPERM;
  1587. case -EACCES:
  1588. case -NFS4ERR_DELAY:
  1589. case -ETIMEDOUT:
  1590. case -EAGAIN:
  1591. ssleep(1);
  1592. break;
  1593. case -NFS4ERR_MINOR_VERS_MISMATCH:
  1594. if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
  1595. nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
  1596. dprintk("%s: exit with error %d for server %s\n",
  1597. __func__, -EPROTONOSUPPORT, clp->cl_hostname);
  1598. return -EPROTONOSUPPORT;
  1599. case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
  1600. * in nfs4_exchange_id */
  1601. default:
  1602. dprintk("%s: exit with error %d for server %s\n", __func__,
  1603. status, clp->cl_hostname);
  1604. return status;
  1605. }
  1606. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1607. dprintk("%s: handled error %d for server %s\n", __func__, status,
  1608. clp->cl_hostname);
  1609. return 0;
  1610. }
  1611. static int nfs4_establish_lease(struct nfs_client *clp)
  1612. {
  1613. struct rpc_cred *cred;
  1614. const struct nfs4_state_recovery_ops *ops =
  1615. clp->cl_mvops->reboot_recovery_ops;
  1616. int status;
  1617. cred = nfs4_get_clid_cred(clp);
  1618. if (cred == NULL)
  1619. return -ENOENT;
  1620. status = ops->establish_clid(clp, cred);
  1621. put_rpccred(cred);
  1622. if (status != 0)
  1623. return status;
  1624. pnfs_destroy_all_layouts(clp);
  1625. return 0;
  1626. }
  1627. /*
  1628. * Returns zero or a negative errno. NFS4ERR values are converted
  1629. * to local errno values.
  1630. */
  1631. static int nfs4_reclaim_lease(struct nfs_client *clp)
  1632. {
  1633. int status;
  1634. status = nfs4_establish_lease(clp);
  1635. if (status < 0)
  1636. return nfs4_handle_reclaim_lease_error(clp, status);
  1637. if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
  1638. nfs4_state_start_reclaim_nograce(clp);
  1639. if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
  1640. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  1641. clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1642. clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1643. return 0;
  1644. }
  1645. static int nfs4_purge_lease(struct nfs_client *clp)
  1646. {
  1647. int status;
  1648. status = nfs4_establish_lease(clp);
  1649. if (status < 0)
  1650. return nfs4_handle_reclaim_lease_error(clp, status);
  1651. clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
  1652. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1653. nfs4_state_start_reclaim_nograce(clp);
  1654. return 0;
  1655. }
  1656. /*
  1657. * Try remote migration of one FSID from a source server to a
  1658. * destination server. The source server provides a list of
  1659. * potential destinations.
  1660. *
  1661. * Returns zero or a negative NFS4ERR status code.
  1662. */
  1663. static int nfs4_try_migration(struct nfs_server *server, struct rpc_cred *cred)
  1664. {
  1665. struct nfs_client *clp = server->nfs_client;
  1666. struct nfs4_fs_locations *locations = NULL;
  1667. struct inode *inode;
  1668. struct page *page;
  1669. int status, result;
  1670. dprintk("--> %s: FSID %llx:%llx on \"%s\"\n", __func__,
  1671. (unsigned long long)server->fsid.major,
  1672. (unsigned long long)server->fsid.minor,
  1673. clp->cl_hostname);
  1674. result = 0;
  1675. page = alloc_page(GFP_KERNEL);
  1676. locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  1677. if (page == NULL || locations == NULL) {
  1678. dprintk("<-- %s: no memory\n", __func__);
  1679. goto out;
  1680. }
  1681. inode = server->super->s_root->d_inode;
  1682. result = nfs4_proc_get_locations(inode, locations, page, cred);
  1683. if (result) {
  1684. dprintk("<-- %s: failed to retrieve fs_locations: %d\n",
  1685. __func__, result);
  1686. goto out;
  1687. }
  1688. result = -NFS4ERR_NXIO;
  1689. if (!(locations->fattr.valid & NFS_ATTR_FATTR_V4_LOCATIONS)) {
  1690. dprintk("<-- %s: No fs_locations data, migration skipped\n",
  1691. __func__);
  1692. goto out;
  1693. }
  1694. nfs4_begin_drain_session(clp);
  1695. status = nfs4_replace_transport(server, locations);
  1696. if (status != 0) {
  1697. dprintk("<-- %s: failed to replace transport: %d\n",
  1698. __func__, status);
  1699. goto out;
  1700. }
  1701. result = 0;
  1702. dprintk("<-- %s: migration succeeded\n", __func__);
  1703. out:
  1704. if (page != NULL)
  1705. __free_page(page);
  1706. kfree(locations);
  1707. if (result) {
  1708. pr_err("NFS: migration recovery failed (server %s)\n",
  1709. clp->cl_hostname);
  1710. set_bit(NFS_MIG_FAILED, &server->mig_status);
  1711. }
  1712. return result;
  1713. }
  1714. /*
  1715. * Returns zero or a negative NFS4ERR status code.
  1716. */
  1717. static int nfs4_handle_migration(struct nfs_client *clp)
  1718. {
  1719. const struct nfs4_state_maintenance_ops *ops =
  1720. clp->cl_mvops->state_renewal_ops;
  1721. struct nfs_server *server;
  1722. struct rpc_cred *cred;
  1723. dprintk("%s: migration reported on \"%s\"\n", __func__,
  1724. clp->cl_hostname);
  1725. spin_lock(&clp->cl_lock);
  1726. cred = ops->get_state_renewal_cred_locked(clp);
  1727. spin_unlock(&clp->cl_lock);
  1728. if (cred == NULL)
  1729. return -NFS4ERR_NOENT;
  1730. clp->cl_mig_gen++;
  1731. restart:
  1732. rcu_read_lock();
  1733. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  1734. int status;
  1735. if (server->mig_gen == clp->cl_mig_gen)
  1736. continue;
  1737. server->mig_gen = clp->cl_mig_gen;
  1738. if (!test_and_clear_bit(NFS_MIG_IN_TRANSITION,
  1739. &server->mig_status))
  1740. continue;
  1741. rcu_read_unlock();
  1742. status = nfs4_try_migration(server, cred);
  1743. if (status < 0) {
  1744. put_rpccred(cred);
  1745. return status;
  1746. }
  1747. goto restart;
  1748. }
  1749. rcu_read_unlock();
  1750. put_rpccred(cred);
  1751. return 0;
  1752. }
  1753. /*
  1754. * Test each nfs_server on the clp's cl_superblocks list to see
  1755. * if it's moved to another server. Stop when the server no longer
  1756. * returns NFS4ERR_LEASE_MOVED.
  1757. */
  1758. static int nfs4_handle_lease_moved(struct nfs_client *clp)
  1759. {
  1760. const struct nfs4_state_maintenance_ops *ops =
  1761. clp->cl_mvops->state_renewal_ops;
  1762. struct nfs_server *server;
  1763. struct rpc_cred *cred;
  1764. dprintk("%s: lease moved reported on \"%s\"\n", __func__,
  1765. clp->cl_hostname);
  1766. spin_lock(&clp->cl_lock);
  1767. cred = ops->get_state_renewal_cred_locked(clp);
  1768. spin_unlock(&clp->cl_lock);
  1769. if (cred == NULL)
  1770. return -NFS4ERR_NOENT;
  1771. clp->cl_mig_gen++;
  1772. restart:
  1773. rcu_read_lock();
  1774. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  1775. struct inode *inode;
  1776. int status;
  1777. if (server->mig_gen == clp->cl_mig_gen)
  1778. continue;
  1779. server->mig_gen = clp->cl_mig_gen;
  1780. rcu_read_unlock();
  1781. inode = server->super->s_root->d_inode;
  1782. status = nfs4_proc_fsid_present(inode, cred);
  1783. if (status != -NFS4ERR_MOVED)
  1784. goto restart; /* wasn't this one */
  1785. if (nfs4_try_migration(server, cred) == -NFS4ERR_LEASE_MOVED)
  1786. goto restart; /* there are more */
  1787. goto out;
  1788. }
  1789. rcu_read_unlock();
  1790. out:
  1791. put_rpccred(cred);
  1792. return 0;
  1793. }
  1794. /**
  1795. * nfs4_discover_server_trunking - Detect server IP address trunking
  1796. *
  1797. * @clp: nfs_client under test
  1798. * @result: OUT: found nfs_client, or clp
  1799. *
  1800. * Returns zero or a negative errno. If zero is returned,
  1801. * an nfs_client pointer is planted in "result".
  1802. *
  1803. * Note: since we are invoked in process context, and
  1804. * not from inside the state manager, we cannot use
  1805. * nfs4_handle_reclaim_lease_error().
  1806. */
  1807. int nfs4_discover_server_trunking(struct nfs_client *clp,
  1808. struct nfs_client **result)
  1809. {
  1810. const struct nfs4_state_recovery_ops *ops =
  1811. clp->cl_mvops->reboot_recovery_ops;
  1812. struct rpc_clnt *clnt;
  1813. struct rpc_cred *cred;
  1814. int i, status;
  1815. dprintk("NFS: %s: testing '%s'\n", __func__, clp->cl_hostname);
  1816. clnt = clp->cl_rpcclient;
  1817. i = 0;
  1818. mutex_lock(&nfs_clid_init_mutex);
  1819. again:
  1820. status = -ENOENT;
  1821. cred = nfs4_get_clid_cred(clp);
  1822. if (cred == NULL)
  1823. goto out_unlock;
  1824. status = ops->detect_trunking(clp, result, cred);
  1825. put_rpccred(cred);
  1826. switch (status) {
  1827. case 0:
  1828. break;
  1829. case -ETIMEDOUT:
  1830. if (clnt->cl_softrtry)
  1831. break;
  1832. case -NFS4ERR_DELAY:
  1833. case -EAGAIN:
  1834. ssleep(1);
  1835. case -NFS4ERR_STALE_CLIENTID:
  1836. dprintk("NFS: %s after status %d, retrying\n",
  1837. __func__, status);
  1838. goto again;
  1839. case -EACCES:
  1840. if (i++ == 0) {
  1841. nfs4_root_machine_cred(clp);
  1842. goto again;
  1843. }
  1844. if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX)
  1845. break;
  1846. case -NFS4ERR_CLID_INUSE:
  1847. case -NFS4ERR_WRONGSEC:
  1848. /* No point in retrying if we already used RPC_AUTH_UNIX */
  1849. if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX) {
  1850. status = -EPERM;
  1851. break;
  1852. }
  1853. clnt = rpc_clone_client_set_auth(clnt, RPC_AUTH_UNIX);
  1854. if (IS_ERR(clnt)) {
  1855. status = PTR_ERR(clnt);
  1856. break;
  1857. }
  1858. /* Note: this is safe because we haven't yet marked the
  1859. * client as ready, so we are the only user of
  1860. * clp->cl_rpcclient
  1861. */
  1862. clnt = xchg(&clp->cl_rpcclient, clnt);
  1863. rpc_shutdown_client(clnt);
  1864. clnt = clp->cl_rpcclient;
  1865. goto again;
  1866. case -NFS4ERR_MINOR_VERS_MISMATCH:
  1867. status = -EPROTONOSUPPORT;
  1868. break;
  1869. case -EKEYEXPIRED:
  1870. case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
  1871. * in nfs4_exchange_id */
  1872. status = -EKEYEXPIRED;
  1873. break;
  1874. default:
  1875. pr_warn("NFS: %s unhandled error %d. Exiting with error EIO\n",
  1876. __func__, status);
  1877. status = -EIO;
  1878. }
  1879. out_unlock:
  1880. mutex_unlock(&nfs_clid_init_mutex);
  1881. dprintk("NFS: %s: status = %d\n", __func__, status);
  1882. return status;
  1883. }
  1884. #ifdef CONFIG_NFS_V4_1
  1885. void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
  1886. {
  1887. struct nfs_client *clp = session->clp;
  1888. switch (err) {
  1889. default:
  1890. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1891. break;
  1892. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1893. set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  1894. }
  1895. nfs4_schedule_lease_recovery(clp);
  1896. }
  1897. EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
  1898. static void nfs41_ping_server(struct nfs_client *clp)
  1899. {
  1900. /* Use CHECK_LEASE to ping the server with a SEQUENCE */
  1901. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1902. nfs4_schedule_state_manager(clp);
  1903. }
  1904. void nfs41_server_notify_target_slotid_update(struct nfs_client *clp)
  1905. {
  1906. nfs41_ping_server(clp);
  1907. }
  1908. void nfs41_server_notify_highest_slotid_update(struct nfs_client *clp)
  1909. {
  1910. nfs41_ping_server(clp);
  1911. }
  1912. static void nfs4_reset_all_state(struct nfs_client *clp)
  1913. {
  1914. if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
  1915. set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
  1916. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1917. nfs4_state_start_reclaim_nograce(clp);
  1918. dprintk("%s: scheduling reset of all state for server %s!\n",
  1919. __func__, clp->cl_hostname);
  1920. nfs4_schedule_state_manager(clp);
  1921. }
  1922. }
  1923. static void nfs41_handle_server_reboot(struct nfs_client *clp)
  1924. {
  1925. if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
  1926. nfs4_state_start_reclaim_reboot(clp);
  1927. dprintk("%s: server %s rebooted!\n", __func__,
  1928. clp->cl_hostname);
  1929. nfs4_schedule_state_manager(clp);
  1930. }
  1931. }
  1932. static void nfs41_handle_state_revoked(struct nfs_client *clp)
  1933. {
  1934. nfs4_reset_all_state(clp);
  1935. dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
  1936. }
  1937. static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
  1938. {
  1939. /* This will need to handle layouts too */
  1940. nfs_expire_all_delegations(clp);
  1941. dprintk("%s: Recallable state revoked on server %s!\n", __func__,
  1942. clp->cl_hostname);
  1943. }
  1944. static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
  1945. {
  1946. nfs_expire_all_delegations(clp);
  1947. if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
  1948. nfs4_schedule_state_manager(clp);
  1949. dprintk("%s: server %s declared a backchannel fault\n", __func__,
  1950. clp->cl_hostname);
  1951. }
  1952. static void nfs41_handle_cb_path_down(struct nfs_client *clp)
  1953. {
  1954. if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
  1955. &clp->cl_state) == 0)
  1956. nfs4_schedule_state_manager(clp);
  1957. }
  1958. void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
  1959. {
  1960. if (!flags)
  1961. return;
  1962. dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
  1963. __func__, clp->cl_hostname, clp->cl_clientid, flags);
  1964. if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
  1965. nfs41_handle_server_reboot(clp);
  1966. if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
  1967. SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
  1968. SEQ4_STATUS_ADMIN_STATE_REVOKED))
  1969. nfs41_handle_state_revoked(clp);
  1970. if (flags & SEQ4_STATUS_LEASE_MOVED)
  1971. nfs4_schedule_lease_moved_recovery(clp);
  1972. if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
  1973. nfs41_handle_recallable_state_revoked(clp);
  1974. if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
  1975. nfs41_handle_backchannel_fault(clp);
  1976. else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
  1977. SEQ4_STATUS_CB_PATH_DOWN_SESSION))
  1978. nfs41_handle_cb_path_down(clp);
  1979. }
  1980. static int nfs4_reset_session(struct nfs_client *clp)
  1981. {
  1982. struct rpc_cred *cred;
  1983. int status;
  1984. if (!nfs4_has_session(clp))
  1985. return 0;
  1986. nfs4_begin_drain_session(clp);
  1987. cred = nfs4_get_clid_cred(clp);
  1988. status = nfs4_proc_destroy_session(clp->cl_session, cred);
  1989. switch (status) {
  1990. case 0:
  1991. case -NFS4ERR_BADSESSION:
  1992. case -NFS4ERR_DEADSESSION:
  1993. break;
  1994. case -NFS4ERR_BACK_CHAN_BUSY:
  1995. case -NFS4ERR_DELAY:
  1996. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1997. status = 0;
  1998. ssleep(1);
  1999. goto out;
  2000. default:
  2001. status = nfs4_recovery_handle_error(clp, status);
  2002. goto out;
  2003. }
  2004. memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
  2005. status = nfs4_proc_create_session(clp, cred);
  2006. if (status) {
  2007. dprintk("%s: session reset failed with status %d for server %s!\n",
  2008. __func__, status, clp->cl_hostname);
  2009. status = nfs4_handle_reclaim_lease_error(clp, status);
  2010. goto out;
  2011. }
  2012. nfs41_finish_session_reset(clp);
  2013. dprintk("%s: session reset was successful for server %s!\n",
  2014. __func__, clp->cl_hostname);
  2015. out:
  2016. if (cred)
  2017. put_rpccred(cred);
  2018. return status;
  2019. }
  2020. static int nfs4_bind_conn_to_session(struct nfs_client *clp)
  2021. {
  2022. struct rpc_cred *cred;
  2023. int ret;
  2024. if (!nfs4_has_session(clp))
  2025. return 0;
  2026. nfs4_begin_drain_session(clp);
  2027. cred = nfs4_get_clid_cred(clp);
  2028. ret = nfs4_proc_bind_conn_to_session(clp, cred);
  2029. if (cred)
  2030. put_rpccred(cred);
  2031. clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  2032. switch (ret) {
  2033. case 0:
  2034. dprintk("%s: bind_conn_to_session was successful for server %s!\n",
  2035. __func__, clp->cl_hostname);
  2036. break;
  2037. case -NFS4ERR_DELAY:
  2038. ssleep(1);
  2039. set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  2040. break;
  2041. default:
  2042. return nfs4_recovery_handle_error(clp, ret);
  2043. }
  2044. return 0;
  2045. }
  2046. #else /* CONFIG_NFS_V4_1 */
  2047. static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
  2048. static int nfs4_bind_conn_to_session(struct nfs_client *clp)
  2049. {
  2050. return 0;
  2051. }
  2052. #endif /* CONFIG_NFS_V4_1 */
  2053. static void nfs4_state_manager(struct nfs_client *clp)
  2054. {
  2055. int status = 0;
  2056. const char *section = "", *section_sep = "";
  2057. /* Ensure exclusive access to NFSv4 state */
  2058. do {
  2059. if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
  2060. section = "purge state";
  2061. status = nfs4_purge_lease(clp);
  2062. if (status < 0)
  2063. goto out_error;
  2064. continue;
  2065. }
  2066. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
  2067. section = "lease expired";
  2068. /* We're going to have to re-establish a clientid */
  2069. status = nfs4_reclaim_lease(clp);
  2070. if (status < 0)
  2071. goto out_error;
  2072. continue;
  2073. }
  2074. /* Initialize or reset the session */
  2075. if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
  2076. section = "reset session";
  2077. status = nfs4_reset_session(clp);
  2078. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  2079. continue;
  2080. if (status < 0)
  2081. goto out_error;
  2082. }
  2083. /* Send BIND_CONN_TO_SESSION */
  2084. if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
  2085. &clp->cl_state)) {
  2086. section = "bind conn to session";
  2087. status = nfs4_bind_conn_to_session(clp);
  2088. if (status < 0)
  2089. goto out_error;
  2090. continue;
  2091. }
  2092. if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
  2093. section = "check lease";
  2094. status = nfs4_check_lease(clp);
  2095. if (status < 0)
  2096. goto out_error;
  2097. continue;
  2098. }
  2099. if (test_and_clear_bit(NFS4CLNT_MOVED, &clp->cl_state)) {
  2100. section = "migration";
  2101. status = nfs4_handle_migration(clp);
  2102. if (status < 0)
  2103. goto out_error;
  2104. }
  2105. if (test_and_clear_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state)) {
  2106. section = "lease moved";
  2107. status = nfs4_handle_lease_moved(clp);
  2108. if (status < 0)
  2109. goto out_error;
  2110. }
  2111. /* First recover reboot state... */
  2112. if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
  2113. section = "reclaim reboot";
  2114. status = nfs4_do_reclaim(clp,
  2115. clp->cl_mvops->reboot_recovery_ops);
  2116. if (status == -EAGAIN)
  2117. continue;
  2118. if (status < 0)
  2119. goto out_error;
  2120. nfs4_state_end_reclaim_reboot(clp);
  2121. }
  2122. /* Now recover expired state... */
  2123. if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
  2124. section = "reclaim nograce";
  2125. status = nfs4_do_reclaim(clp,
  2126. clp->cl_mvops->nograce_recovery_ops);
  2127. if (status == -EAGAIN)
  2128. continue;
  2129. if (status < 0)
  2130. goto out_error;
  2131. }
  2132. nfs4_end_drain_session(clp);
  2133. if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
  2134. nfs_client_return_marked_delegations(clp);
  2135. continue;
  2136. }
  2137. nfs4_clear_state_manager_bit(clp);
  2138. /* Did we race with an attempt to give us more work? */
  2139. if (clp->cl_state == 0)
  2140. break;
  2141. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  2142. break;
  2143. } while (atomic_read(&clp->cl_count) > 1);
  2144. return;
  2145. out_error:
  2146. if (strlen(section))
  2147. section_sep = ": ";
  2148. pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
  2149. " with error %d\n", section_sep, section,
  2150. clp->cl_hostname, -status);
  2151. ssleep(1);
  2152. nfs4_end_drain_session(clp);
  2153. nfs4_clear_state_manager_bit(clp);
  2154. }
  2155. static int nfs4_run_state_manager(void *ptr)
  2156. {
  2157. struct nfs_client *clp = ptr;
  2158. allow_signal(SIGKILL);
  2159. nfs4_state_manager(clp);
  2160. nfs_put_client(clp);
  2161. module_put_and_exit(0);
  2162. return 0;
  2163. }
  2164. /*
  2165. * Local variables:
  2166. * c-basic-offset: 8
  2167. * End:
  2168. */