nfs4state.c 73 KB

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