nfs4state.c 71 KB

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