lfs.c 176 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982
  1. /*
  2. * The little filesystem
  3. *
  4. * Copyright (c) 2022, The littlefs authors.
  5. * Copyright (c) 2017, Arm Limited. All rights reserved.
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "lfs.h"
  9. #include "lfs_util.h"
  10. // some constants used throughout the code
  11. #define LFS_BLOCK_NULL ((lfs_block_t)-1)
  12. #define LFS_BLOCK_INLINE ((lfs_block_t)-2)
  13. enum {
  14. LFS_OK_RELOCATED = 1,
  15. LFS_OK_DROPPED = 2,
  16. LFS_OK_ORPHANED = 3,
  17. };
  18. enum {
  19. LFS_CMP_EQ = 0,
  20. LFS_CMP_LT = 1,
  21. LFS_CMP_GT = 2,
  22. };
  23. /// Caching block device operations ///
  24. static inline void lfs_cache_drop(lfs_t *lfs, lfs_cache_t *rcache) {
  25. // do not zero, cheaper if cache is readonly or only going to be
  26. // written with identical data (during relocates)
  27. (void)lfs;
  28. rcache->block = LFS_BLOCK_NULL;
  29. }
  30. static inline void lfs_cache_zero(lfs_t *lfs, lfs_cache_t *pcache) {
  31. // zero to avoid information leak
  32. memset(pcache->buffer, 0xff, lfs->cfg->cache_size);
  33. pcache->block = LFS_BLOCK_NULL;
  34. }
  35. static int lfs_bd_read(lfs_t *lfs,
  36. const lfs_cache_t *pcache, lfs_cache_t *rcache, lfs_size_t hint,
  37. lfs_block_t block, lfs_off_t off,
  38. void *buffer, lfs_size_t size) {
  39. uint8_t *data = buffer;
  40. if (block >= lfs->cfg->block_count ||
  41. off+size > lfs->cfg->block_size) {
  42. return LFS_ERR_CORRUPT;
  43. }
  44. while (size > 0) {
  45. lfs_size_t diff = size;
  46. if (pcache && block == pcache->block &&
  47. off < pcache->off + pcache->size) {
  48. if (off >= pcache->off) {
  49. // is already in pcache?
  50. diff = lfs_min(diff, pcache->size - (off-pcache->off));
  51. memcpy(data, &pcache->buffer[off-pcache->off], diff);
  52. data += diff;
  53. off += diff;
  54. size -= diff;
  55. continue;
  56. }
  57. // pcache takes priority
  58. diff = lfs_min(diff, pcache->off-off);
  59. }
  60. if (block == rcache->block &&
  61. off < rcache->off + rcache->size) {
  62. if (off >= rcache->off) {
  63. // is already in rcache?
  64. diff = lfs_min(diff, rcache->size - (off-rcache->off));
  65. memcpy(data, &rcache->buffer[off-rcache->off], diff);
  66. data += diff;
  67. off += diff;
  68. size -= diff;
  69. continue;
  70. }
  71. // rcache takes priority
  72. diff = lfs_min(diff, rcache->off-off);
  73. }
  74. if (size >= hint && off % lfs->cfg->read_size == 0 &&
  75. size >= lfs->cfg->read_size) {
  76. // bypass cache?
  77. diff = lfs_aligndown(diff, lfs->cfg->read_size);
  78. int err = lfs->cfg->read(lfs->cfg, block, off, data, diff);
  79. if (err) {
  80. return err;
  81. }
  82. data += diff;
  83. off += diff;
  84. size -= diff;
  85. continue;
  86. }
  87. // load to cache, first condition can no longer fail
  88. LFS_ASSERT(block < lfs->cfg->block_count);
  89. rcache->block = block;
  90. rcache->off = lfs_aligndown(off, lfs->cfg->read_size);
  91. rcache->size = lfs_min(
  92. lfs_min(
  93. lfs_alignup(off+hint, lfs->cfg->read_size),
  94. lfs->cfg->block_size)
  95. - rcache->off,
  96. lfs->cfg->cache_size);
  97. int err = lfs->cfg->read(lfs->cfg, rcache->block,
  98. rcache->off, rcache->buffer, rcache->size);
  99. LFS_ASSERT(err <= 0);
  100. if (err) {
  101. return err;
  102. }
  103. }
  104. return 0;
  105. }
  106. static int lfs_bd_cmp(lfs_t *lfs,
  107. const lfs_cache_t *pcache, lfs_cache_t *rcache, lfs_size_t hint,
  108. lfs_block_t block, lfs_off_t off,
  109. const void *buffer, lfs_size_t size) {
  110. const uint8_t *data = buffer;
  111. lfs_size_t diff = 0;
  112. for (lfs_off_t i = 0; i < size; i += diff) {
  113. uint8_t dat[8];
  114. diff = lfs_min(size-i, sizeof(dat));
  115. int err = lfs_bd_read(lfs,
  116. pcache, rcache, hint-i,
  117. block, off+i, &dat, diff);
  118. if (err) {
  119. return err;
  120. }
  121. int res = memcmp(dat, data + i, diff);
  122. if (res) {
  123. return res < 0 ? LFS_CMP_LT : LFS_CMP_GT;
  124. }
  125. }
  126. return LFS_CMP_EQ;
  127. }
  128. static int lfs_bd_crc(lfs_t *lfs,
  129. const lfs_cache_t *pcache, lfs_cache_t *rcache, lfs_size_t hint,
  130. lfs_block_t block, lfs_off_t off, lfs_size_t size, uint32_t *crc) {
  131. lfs_size_t diff = 0;
  132. for (lfs_off_t i = 0; i < size; i += diff) {
  133. uint8_t dat[8];
  134. diff = lfs_min(size-i, sizeof(dat));
  135. int err = lfs_bd_read(lfs,
  136. pcache, rcache, hint-i,
  137. block, off+i, &dat, diff);
  138. if (err) {
  139. return err;
  140. }
  141. *crc = lfs_crc(*crc, &dat, diff);
  142. }
  143. return 0;
  144. }
  145. #ifndef LFS_READONLY
  146. static int lfs_bd_flush(lfs_t *lfs,
  147. lfs_cache_t *pcache, lfs_cache_t *rcache, bool validate) {
  148. if (pcache->block != LFS_BLOCK_NULL && pcache->block != LFS_BLOCK_INLINE) {
  149. LFS_ASSERT(pcache->block < lfs->cfg->block_count);
  150. lfs_size_t diff = lfs_alignup(pcache->size, lfs->cfg->prog_size);
  151. int err = lfs->cfg->prog(lfs->cfg, pcache->block,
  152. pcache->off, pcache->buffer, diff);
  153. LFS_ASSERT(err <= 0);
  154. if (err) {
  155. return err;
  156. }
  157. if (validate) {
  158. // check data on disk
  159. lfs_cache_drop(lfs, rcache);
  160. int res = lfs_bd_cmp(lfs,
  161. NULL, rcache, diff,
  162. pcache->block, pcache->off, pcache->buffer, diff);
  163. if (res < 0) {
  164. return res;
  165. }
  166. if (res != LFS_CMP_EQ) {
  167. return LFS_ERR_CORRUPT;
  168. }
  169. }
  170. lfs_cache_zero(lfs, pcache);
  171. }
  172. return 0;
  173. }
  174. #endif
  175. #ifndef LFS_READONLY
  176. static int lfs_bd_sync(lfs_t *lfs,
  177. lfs_cache_t *pcache, lfs_cache_t *rcache, bool validate) {
  178. lfs_cache_drop(lfs, rcache);
  179. int err = lfs_bd_flush(lfs, pcache, rcache, validate);
  180. if (err) {
  181. return err;
  182. }
  183. err = lfs->cfg->sync(lfs->cfg);
  184. LFS_ASSERT(err <= 0);
  185. return err;
  186. }
  187. #endif
  188. #ifndef LFS_READONLY
  189. static int lfs_bd_prog(lfs_t *lfs,
  190. lfs_cache_t *pcache, lfs_cache_t *rcache, bool validate,
  191. lfs_block_t block, lfs_off_t off,
  192. const void *buffer, lfs_size_t size) {
  193. const uint8_t *data = buffer;
  194. LFS_ASSERT(block == LFS_BLOCK_INLINE || block < lfs->cfg->block_count);
  195. LFS_ASSERT(off + size <= lfs->cfg->block_size);
  196. while (size > 0) {
  197. if (block == pcache->block &&
  198. off >= pcache->off &&
  199. off < pcache->off + lfs->cfg->cache_size) {
  200. // already fits in pcache?
  201. lfs_size_t diff = lfs_min(size,
  202. lfs->cfg->cache_size - (off-pcache->off));
  203. memcpy(&pcache->buffer[off-pcache->off], data, diff);
  204. data += diff;
  205. off += diff;
  206. size -= diff;
  207. pcache->size = lfs_max(pcache->size, off - pcache->off);
  208. if (pcache->size == lfs->cfg->cache_size) {
  209. // eagerly flush out pcache if we fill up
  210. int err = lfs_bd_flush(lfs, pcache, rcache, validate);
  211. if (err) {
  212. return err;
  213. }
  214. }
  215. continue;
  216. }
  217. // pcache must have been flushed, either by programming and
  218. // entire block or manually flushing the pcache
  219. LFS_ASSERT(pcache->block == LFS_BLOCK_NULL);
  220. // prepare pcache, first condition can no longer fail
  221. pcache->block = block;
  222. pcache->off = lfs_aligndown(off, lfs->cfg->prog_size);
  223. pcache->size = 0;
  224. }
  225. return 0;
  226. }
  227. #endif
  228. #ifndef LFS_READONLY
  229. static int lfs_bd_erase(lfs_t *lfs, lfs_block_t block) {
  230. LFS_ASSERT(block < lfs->cfg->block_count);
  231. int err = lfs->cfg->erase(lfs->cfg, block);
  232. LFS_ASSERT(err <= 0);
  233. return err;
  234. }
  235. #endif
  236. /// Small type-level utilities ///
  237. // operations on block pairs
  238. static inline void lfs_pair_swap(lfs_block_t pair[2]) {
  239. lfs_block_t t = pair[0];
  240. pair[0] = pair[1];
  241. pair[1] = t;
  242. }
  243. static inline bool lfs_pair_isnull(const lfs_block_t pair[2]) {
  244. return pair[0] == LFS_BLOCK_NULL || pair[1] == LFS_BLOCK_NULL;
  245. }
  246. static inline int lfs_pair_cmp(
  247. const lfs_block_t paira[2],
  248. const lfs_block_t pairb[2]) {
  249. return !(paira[0] == pairb[0] || paira[1] == pairb[1] ||
  250. paira[0] == pairb[1] || paira[1] == pairb[0]);
  251. }
  252. #ifndef LFS_READONLY
  253. static inline bool lfs_pair_sync(
  254. const lfs_block_t paira[2],
  255. const lfs_block_t pairb[2]) {
  256. return (paira[0] == pairb[0] && paira[1] == pairb[1]) ||
  257. (paira[0] == pairb[1] && paira[1] == pairb[0]);
  258. }
  259. #endif
  260. static inline void lfs_pair_fromle32(lfs_block_t pair[2]) {
  261. pair[0] = lfs_fromle32(pair[0]);
  262. pair[1] = lfs_fromle32(pair[1]);
  263. }
  264. #ifndef LFS_READONLY
  265. static inline void lfs_pair_tole32(lfs_block_t pair[2]) {
  266. pair[0] = lfs_tole32(pair[0]);
  267. pair[1] = lfs_tole32(pair[1]);
  268. }
  269. #endif
  270. // operations on 32-bit entry tags
  271. typedef uint32_t lfs_tag_t;
  272. typedef int32_t lfs_stag_t;
  273. #define LFS_MKTAG(type, id, size) \
  274. (((lfs_tag_t)(type) << 20) | ((lfs_tag_t)(id) << 10) | (lfs_tag_t)(size))
  275. #define LFS_MKTAG_IF(cond, type, id, size) \
  276. ((cond) ? LFS_MKTAG(type, id, size) : LFS_MKTAG(LFS_FROM_NOOP, 0, 0))
  277. #define LFS_MKTAG_IF_ELSE(cond, type1, id1, size1, type2, id2, size2) \
  278. ((cond) ? LFS_MKTAG(type1, id1, size1) : LFS_MKTAG(type2, id2, size2))
  279. static inline bool lfs_tag_isvalid(lfs_tag_t tag) {
  280. return !(tag & 0x80000000);
  281. }
  282. static inline bool lfs_tag_isdelete(lfs_tag_t tag) {
  283. return ((int32_t)(tag << 22) >> 22) == -1;
  284. }
  285. static inline uint16_t lfs_tag_type1(lfs_tag_t tag) {
  286. return (tag & 0x70000000) >> 20;
  287. }
  288. static inline uint16_t lfs_tag_type3(lfs_tag_t tag) {
  289. return (tag & 0x7ff00000) >> 20;
  290. }
  291. static inline uint8_t lfs_tag_chunk(lfs_tag_t tag) {
  292. return (tag & 0x0ff00000) >> 20;
  293. }
  294. static inline int8_t lfs_tag_splice(lfs_tag_t tag) {
  295. return (int8_t)lfs_tag_chunk(tag);
  296. }
  297. static inline uint16_t lfs_tag_id(lfs_tag_t tag) {
  298. return (tag & 0x000ffc00) >> 10;
  299. }
  300. static inline lfs_size_t lfs_tag_size(lfs_tag_t tag) {
  301. return tag & 0x000003ff;
  302. }
  303. static inline lfs_size_t lfs_tag_dsize(lfs_tag_t tag) {
  304. return sizeof(tag) + lfs_tag_size(tag + lfs_tag_isdelete(tag));
  305. }
  306. // operations on attributes in attribute lists
  307. struct lfs_mattr {
  308. lfs_tag_t tag;
  309. const void *buffer;
  310. };
  311. struct lfs_diskoff {
  312. lfs_block_t block;
  313. lfs_off_t off;
  314. };
  315. #define LFS_MKATTRS(...) \
  316. (struct lfs_mattr[]){__VA_ARGS__}, \
  317. sizeof((struct lfs_mattr[]){__VA_ARGS__}) / sizeof(struct lfs_mattr)
  318. // operations on global state
  319. static inline void lfs_gstate_xor(lfs_gstate_t *a, const lfs_gstate_t *b) {
  320. for (int i = 0; i < 3; i++) {
  321. ((uint32_t*)a)[i] ^= ((const uint32_t*)b)[i];
  322. }
  323. }
  324. static inline bool lfs_gstate_iszero(const lfs_gstate_t *a) {
  325. for (int i = 0; i < 3; i++) {
  326. if (((uint32_t*)a)[i] != 0) {
  327. return false;
  328. }
  329. }
  330. return true;
  331. }
  332. #ifndef LFS_READONLY
  333. static inline bool lfs_gstate_hasorphans(const lfs_gstate_t *a) {
  334. return lfs_tag_size(a->tag);
  335. }
  336. static inline uint8_t lfs_gstate_getorphans(const lfs_gstate_t *a) {
  337. return lfs_tag_size(a->tag);
  338. }
  339. static inline bool lfs_gstate_hasmove(const lfs_gstate_t *a) {
  340. return lfs_tag_type1(a->tag);
  341. }
  342. #endif
  343. static inline bool lfs_gstate_hasmovehere(const lfs_gstate_t *a,
  344. const lfs_block_t *pair) {
  345. return lfs_tag_type1(a->tag) && lfs_pair_cmp(a->pair, pair) == 0;
  346. }
  347. static inline void lfs_gstate_fromle32(lfs_gstate_t *a) {
  348. a->tag = lfs_fromle32(a->tag);
  349. a->pair[0] = lfs_fromle32(a->pair[0]);
  350. a->pair[1] = lfs_fromle32(a->pair[1]);
  351. }
  352. #ifndef LFS_READONLY
  353. static inline void lfs_gstate_tole32(lfs_gstate_t *a) {
  354. a->tag = lfs_tole32(a->tag);
  355. a->pair[0] = lfs_tole32(a->pair[0]);
  356. a->pair[1] = lfs_tole32(a->pair[1]);
  357. }
  358. #endif
  359. // operations on estate in CRC tags
  360. struct lfs_estate {
  361. lfs_size_t size;
  362. uint32_t crc;
  363. };
  364. static void lfs_estate_fromle32(struct lfs_estate *estate) {
  365. estate->size = lfs_fromle32(estate->size);
  366. estate->crc = lfs_fromle32(estate->crc);
  367. }
  368. static void lfs_estate_tole32(struct lfs_estate *estate) {
  369. estate->size = lfs_tole32(estate->size);
  370. estate->crc = lfs_tole32(estate->crc);
  371. }
  372. // other endianness operations
  373. static void lfs_ctz_fromle32(struct lfs_ctz *ctz) {
  374. ctz->head = lfs_fromle32(ctz->head);
  375. ctz->size = lfs_fromle32(ctz->size);
  376. }
  377. #ifndef LFS_READONLY
  378. static void lfs_ctz_tole32(struct lfs_ctz *ctz) {
  379. ctz->head = lfs_tole32(ctz->head);
  380. ctz->size = lfs_tole32(ctz->size);
  381. }
  382. #endif
  383. static inline void lfs_superblock_fromle32(lfs_superblock_t *superblock) {
  384. superblock->version = lfs_fromle32(superblock->version);
  385. superblock->block_size = lfs_fromle32(superblock->block_size);
  386. superblock->block_count = lfs_fromle32(superblock->block_count);
  387. superblock->name_max = lfs_fromle32(superblock->name_max);
  388. superblock->file_max = lfs_fromle32(superblock->file_max);
  389. superblock->attr_max = lfs_fromle32(superblock->attr_max);
  390. }
  391. #ifndef LFS_READONLY
  392. static inline void lfs_superblock_tole32(lfs_superblock_t *superblock) {
  393. superblock->version = lfs_tole32(superblock->version);
  394. superblock->block_size = lfs_tole32(superblock->block_size);
  395. superblock->block_count = lfs_tole32(superblock->block_count);
  396. superblock->name_max = lfs_tole32(superblock->name_max);
  397. superblock->file_max = lfs_tole32(superblock->file_max);
  398. superblock->attr_max = lfs_tole32(superblock->attr_max);
  399. }
  400. #endif
  401. #ifndef LFS_NO_ASSERT
  402. static bool lfs_mlist_isopen(struct lfs_mlist *head,
  403. struct lfs_mlist *node) {
  404. for (struct lfs_mlist **p = &head; *p; p = &(*p)->next) {
  405. if (*p == (struct lfs_mlist*)node) {
  406. return true;
  407. }
  408. }
  409. return false;
  410. }
  411. #endif
  412. static void lfs_mlist_remove(lfs_t *lfs, struct lfs_mlist *mlist) {
  413. for (struct lfs_mlist **p = &lfs->mlist; *p; p = &(*p)->next) {
  414. if (*p == mlist) {
  415. *p = (*p)->next;
  416. break;
  417. }
  418. }
  419. }
  420. static void lfs_mlist_append(lfs_t *lfs, struct lfs_mlist *mlist) {
  421. mlist->next = lfs->mlist;
  422. lfs->mlist = mlist;
  423. }
  424. /// Internal operations predeclared here ///
  425. #ifndef LFS_READONLY
  426. static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir,
  427. const struct lfs_mattr *attrs, int attrcount);
  428. static int lfs_dir_compact(lfs_t *lfs,
  429. lfs_mdir_t *dir, const struct lfs_mattr *attrs, int attrcount,
  430. lfs_mdir_t *source, uint16_t begin, uint16_t end);
  431. static lfs_ssize_t lfs_file_flushedwrite(lfs_t *lfs, lfs_file_t *file,
  432. const void *buffer, lfs_size_t size);
  433. static lfs_ssize_t lfs_file_rawwrite(lfs_t *lfs, lfs_file_t *file,
  434. const void *buffer, lfs_size_t size);
  435. static int lfs_file_rawsync(lfs_t *lfs, lfs_file_t *file);
  436. static int lfs_file_outline(lfs_t *lfs, lfs_file_t *file);
  437. static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file);
  438. static int lfs_fs_deorphan(lfs_t *lfs, bool powerloss);
  439. static int lfs_fs_preporphans(lfs_t *lfs, int8_t orphans);
  440. static void lfs_fs_prepmove(lfs_t *lfs,
  441. uint16_t id, const lfs_block_t pair[2]);
  442. static int lfs_fs_pred(lfs_t *lfs, const lfs_block_t dir[2],
  443. lfs_mdir_t *pdir);
  444. static lfs_stag_t lfs_fs_parent(lfs_t *lfs, const lfs_block_t dir[2],
  445. lfs_mdir_t *parent);
  446. static int lfs_fs_forceconsistency(lfs_t *lfs);
  447. #endif
  448. #ifdef LFS_MIGRATE
  449. static int lfs1_traverse(lfs_t *lfs,
  450. int (*cb)(void*, lfs_block_t), void *data);
  451. #endif
  452. static int lfs_dir_rawrewind(lfs_t *lfs, lfs_dir_t *dir);
  453. static lfs_ssize_t lfs_file_flushedread(lfs_t *lfs, lfs_file_t *file,
  454. void *buffer, lfs_size_t size);
  455. static lfs_ssize_t lfs_file_rawread(lfs_t *lfs, lfs_file_t *file,
  456. void *buffer, lfs_size_t size);
  457. static int lfs_file_rawclose(lfs_t *lfs, lfs_file_t *file);
  458. static lfs_soff_t lfs_file_rawsize(lfs_t *lfs, lfs_file_t *file);
  459. static lfs_ssize_t lfs_fs_rawsize(lfs_t *lfs);
  460. static int lfs_fs_rawtraverse(lfs_t *lfs,
  461. int (*cb)(void *data, lfs_block_t block), void *data,
  462. bool includeorphans);
  463. static int lfs_deinit(lfs_t *lfs);
  464. static int lfs_rawunmount(lfs_t *lfs);
  465. /// Block allocator ///
  466. #ifndef LFS_READONLY
  467. static int lfs_alloc_lookahead(void *p, lfs_block_t block) {
  468. lfs_t *lfs = (lfs_t*)p;
  469. lfs_block_t off = ((block - lfs->free.off)
  470. + lfs->cfg->block_count) % lfs->cfg->block_count;
  471. if (off < lfs->free.size) {
  472. lfs->free.buffer[off / 32] |= 1U << (off % 32);
  473. }
  474. return 0;
  475. }
  476. #endif
  477. // indicate allocated blocks have been committed into the filesystem, this
  478. // is to prevent blocks from being garbage collected in the middle of a
  479. // commit operation
  480. static void lfs_alloc_ack(lfs_t *lfs) {
  481. lfs->free.ack = lfs->cfg->block_count;
  482. }
  483. // drop the lookahead buffer, this is done during mounting and failed
  484. // traversals in order to avoid invalid lookahead state
  485. static void lfs_alloc_drop(lfs_t *lfs) {
  486. lfs->free.size = 0;
  487. lfs->free.i = 0;
  488. lfs_alloc_ack(lfs);
  489. }
  490. #ifndef LFS_READONLY
  491. static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
  492. while (true) {
  493. while (lfs->free.i != lfs->free.size) {
  494. lfs_block_t off = lfs->free.i;
  495. lfs->free.i += 1;
  496. lfs->free.ack -= 1;
  497. if (!(lfs->free.buffer[off / 32] & (1U << (off % 32)))) {
  498. // found a free block
  499. *block = (lfs->free.off + off) % lfs->cfg->block_count;
  500. // eagerly find next off so an alloc ack can
  501. // discredit old lookahead blocks
  502. while (lfs->free.i != lfs->free.size &&
  503. (lfs->free.buffer[lfs->free.i / 32]
  504. & (1U << (lfs->free.i % 32)))) {
  505. lfs->free.i += 1;
  506. lfs->free.ack -= 1;
  507. }
  508. return 0;
  509. }
  510. }
  511. // check if we have looked at all blocks since last ack
  512. if (lfs->free.ack == 0) {
  513. LFS_ERROR("No more free space %"PRIu32,
  514. lfs->free.i + lfs->free.off);
  515. return LFS_ERR_NOSPC;
  516. }
  517. lfs->free.off = (lfs->free.off + lfs->free.size)
  518. % lfs->cfg->block_count;
  519. lfs->free.size = lfs_min(8*lfs->cfg->lookahead_size, lfs->free.ack);
  520. lfs->free.i = 0;
  521. // find mask of free blocks from tree
  522. memset(lfs->free.buffer, 0, lfs->cfg->lookahead_size);
  523. int err = lfs_fs_rawtraverse(lfs, lfs_alloc_lookahead, lfs, true);
  524. if (err) {
  525. lfs_alloc_drop(lfs);
  526. return err;
  527. }
  528. }
  529. }
  530. #endif
  531. /// Metadata pair and directory operations ///
  532. static lfs_stag_t lfs_dir_getslice(lfs_t *lfs, const lfs_mdir_t *dir,
  533. lfs_tag_t gmask, lfs_tag_t gtag,
  534. lfs_off_t goff, void *gbuffer, lfs_size_t gsize) {
  535. lfs_off_t off = dir->off;
  536. lfs_tag_t ntag = dir->etag;
  537. lfs_stag_t gdiff = 0;
  538. if (lfs_gstate_hasmovehere(&lfs->gdisk, dir->pair) &&
  539. lfs_tag_id(gmask) != 0 &&
  540. lfs_tag_id(lfs->gdisk.tag) <= lfs_tag_id(gtag)) {
  541. // synthetic moves
  542. gdiff -= LFS_MKTAG(0, 1, 0);
  543. }
  544. // iterate over dir block backwards (for faster lookups)
  545. while (off >= sizeof(lfs_tag_t) + lfs_tag_dsize(ntag)) {
  546. off -= lfs_tag_dsize(ntag);
  547. lfs_tag_t tag = ntag;
  548. int err = lfs_bd_read(lfs,
  549. NULL, &lfs->rcache, sizeof(ntag),
  550. dir->pair[0], off, &ntag, sizeof(ntag));
  551. if (err) {
  552. return err;
  553. }
  554. ntag = (lfs_frombe32(ntag) ^ tag) & 0x7fffffff;
  555. if (lfs_tag_id(gmask) != 0 &&
  556. lfs_tag_type1(tag) == LFS_TYPE_SPLICE &&
  557. lfs_tag_id(tag) <= lfs_tag_id(gtag - gdiff)) {
  558. if (tag == (LFS_MKTAG(LFS_TYPE_CREATE, 0, 0) |
  559. (LFS_MKTAG(0, 0x3ff, 0) & (gtag - gdiff)))) {
  560. // found where we were created
  561. return LFS_ERR_NOENT;
  562. }
  563. // move around splices
  564. gdiff += LFS_MKTAG(0, lfs_tag_splice(tag), 0);
  565. }
  566. if ((gmask & tag) == (gmask & (gtag - gdiff))) {
  567. if (lfs_tag_isdelete(tag)) {
  568. return LFS_ERR_NOENT;
  569. }
  570. lfs_size_t diff = lfs_min(lfs_tag_size(tag), gsize);
  571. err = lfs_bd_read(lfs,
  572. NULL, &lfs->rcache, diff,
  573. dir->pair[0], off+sizeof(tag)+goff, gbuffer, diff);
  574. if (err) {
  575. return err;
  576. }
  577. memset((uint8_t*)gbuffer + diff, 0, gsize - diff);
  578. return tag + gdiff;
  579. }
  580. }
  581. return LFS_ERR_NOENT;
  582. }
  583. static lfs_stag_t lfs_dir_get(lfs_t *lfs, const lfs_mdir_t *dir,
  584. lfs_tag_t gmask, lfs_tag_t gtag, void *buffer) {
  585. return lfs_dir_getslice(lfs, dir,
  586. gmask, gtag,
  587. 0, buffer, lfs_tag_size(gtag));
  588. }
  589. static int lfs_dir_getread(lfs_t *lfs, const lfs_mdir_t *dir,
  590. const lfs_cache_t *pcache, lfs_cache_t *rcache, lfs_size_t hint,
  591. lfs_tag_t gmask, lfs_tag_t gtag,
  592. lfs_off_t off, void *buffer, lfs_size_t size) {
  593. uint8_t *data = buffer;
  594. if (off+size > lfs->cfg->block_size) {
  595. return LFS_ERR_CORRUPT;
  596. }
  597. while (size > 0) {
  598. lfs_size_t diff = size;
  599. if (pcache && pcache->block == LFS_BLOCK_INLINE &&
  600. off < pcache->off + pcache->size) {
  601. if (off >= pcache->off) {
  602. // is already in pcache?
  603. diff = lfs_min(diff, pcache->size - (off-pcache->off));
  604. memcpy(data, &pcache->buffer[off-pcache->off], diff);
  605. data += diff;
  606. off += diff;
  607. size -= diff;
  608. continue;
  609. }
  610. // pcache takes priority
  611. diff = lfs_min(diff, pcache->off-off);
  612. }
  613. if (rcache->block == LFS_BLOCK_INLINE &&
  614. off < rcache->off + rcache->size) {
  615. if (off >= rcache->off) {
  616. // is already in rcache?
  617. diff = lfs_min(diff, rcache->size - (off-rcache->off));
  618. memcpy(data, &rcache->buffer[off-rcache->off], diff);
  619. data += diff;
  620. off += diff;
  621. size -= diff;
  622. continue;
  623. }
  624. // rcache takes priority
  625. diff = lfs_min(diff, rcache->off-off);
  626. }
  627. // load to cache, first condition can no longer fail
  628. rcache->block = LFS_BLOCK_INLINE;
  629. rcache->off = lfs_aligndown(off, lfs->cfg->read_size);
  630. rcache->size = lfs_min(lfs_alignup(off+hint, lfs->cfg->read_size),
  631. lfs->cfg->cache_size);
  632. int err = lfs_dir_getslice(lfs, dir, gmask, gtag,
  633. rcache->off, rcache->buffer, rcache->size);
  634. if (err < 0) {
  635. return err;
  636. }
  637. }
  638. return 0;
  639. }
  640. #ifndef LFS_READONLY
  641. static int lfs_dir_traverse_filter(void *p,
  642. lfs_tag_t tag, const void *buffer) {
  643. lfs_tag_t *filtertag = p;
  644. (void)buffer;
  645. // which mask depends on unique bit in tag structure
  646. uint32_t mask = (tag & LFS_MKTAG(0x100, 0, 0))
  647. ? LFS_MKTAG(0x7ff, 0x3ff, 0)
  648. : LFS_MKTAG(0x700, 0x3ff, 0);
  649. // check for redundancy
  650. if ((mask & tag) == (mask & *filtertag) ||
  651. lfs_tag_isdelete(*filtertag) ||
  652. (LFS_MKTAG(0x7ff, 0x3ff, 0) & tag) == (
  653. LFS_MKTAG(LFS_TYPE_DELETE, 0, 0) |
  654. (LFS_MKTAG(0, 0x3ff, 0) & *filtertag))) {
  655. *filtertag = LFS_MKTAG(LFS_FROM_NOOP, 0, 0);
  656. return true;
  657. }
  658. // check if we need to adjust for created/deleted tags
  659. if (lfs_tag_type1(tag) == LFS_TYPE_SPLICE &&
  660. lfs_tag_id(tag) <= lfs_tag_id(*filtertag)) {
  661. *filtertag += LFS_MKTAG(0, lfs_tag_splice(tag), 0);
  662. }
  663. return false;
  664. }
  665. #endif
  666. #ifndef LFS_READONLY
  667. // maximum recursive depth of lfs_dir_traverse, the deepest call:
  668. //
  669. // traverse with commit
  670. // '-> traverse with move
  671. // '-> traverse with filter
  672. //
  673. #define LFS_DIR_TRAVERSE_DEPTH 3
  674. struct lfs_dir_traverse {
  675. const lfs_mdir_t *dir;
  676. lfs_off_t off;
  677. lfs_tag_t ptag;
  678. const struct lfs_mattr *attrs;
  679. int attrcount;
  680. lfs_tag_t tmask;
  681. lfs_tag_t ttag;
  682. uint16_t begin;
  683. uint16_t end;
  684. int16_t diff;
  685. int (*cb)(void *data, lfs_tag_t tag, const void *buffer);
  686. void *data;
  687. lfs_tag_t tag;
  688. const void *buffer;
  689. struct lfs_diskoff disk;
  690. };
  691. static int lfs_dir_traverse(lfs_t *lfs,
  692. const lfs_mdir_t *dir, lfs_off_t off, lfs_tag_t ptag,
  693. const struct lfs_mattr *attrs, int attrcount,
  694. lfs_tag_t tmask, lfs_tag_t ttag,
  695. uint16_t begin, uint16_t end, int16_t diff,
  696. int (*cb)(void *data, lfs_tag_t tag, const void *buffer), void *data) {
  697. // This function in inherently recursive, but bounded. To allow tool-based
  698. // analysis without unnecessary code-cost we use an explicit stack
  699. struct lfs_dir_traverse stack[LFS_DIR_TRAVERSE_DEPTH-1];
  700. unsigned sp = 0;
  701. int res;
  702. // iterate over directory and attrs
  703. lfs_tag_t tag;
  704. const void *buffer;
  705. struct lfs_diskoff disk;
  706. while (true) {
  707. {
  708. if (off+lfs_tag_dsize(ptag) < dir->off) {
  709. off += lfs_tag_dsize(ptag);
  710. int err = lfs_bd_read(lfs,
  711. NULL, &lfs->rcache, sizeof(tag),
  712. dir->pair[0], off, &tag, sizeof(tag));
  713. if (err) {
  714. return err;
  715. }
  716. tag = (lfs_frombe32(tag) ^ ptag) | 0x80000000;
  717. disk.block = dir->pair[0];
  718. disk.off = off+sizeof(lfs_tag_t);
  719. buffer = &disk;
  720. ptag = tag;
  721. } else if (attrcount > 0) {
  722. tag = attrs[0].tag;
  723. buffer = attrs[0].buffer;
  724. attrs += 1;
  725. attrcount -= 1;
  726. } else {
  727. // finished traversal, pop from stack?
  728. res = 0;
  729. break;
  730. }
  731. // do we need to filter?
  732. lfs_tag_t mask = LFS_MKTAG(0x7ff, 0, 0);
  733. if ((mask & tmask & tag) != (mask & tmask & ttag)) {
  734. continue;
  735. }
  736. if (lfs_tag_id(tmask) != 0) {
  737. LFS_ASSERT(sp < LFS_DIR_TRAVERSE_DEPTH);
  738. // recurse, scan for duplicates, and update tag based on
  739. // creates/deletes
  740. stack[sp] = (struct lfs_dir_traverse){
  741. .dir = dir,
  742. .off = off,
  743. .ptag = ptag,
  744. .attrs = attrs,
  745. .attrcount = attrcount,
  746. .tmask = tmask,
  747. .ttag = ttag,
  748. .begin = begin,
  749. .end = end,
  750. .diff = diff,
  751. .cb = cb,
  752. .data = data,
  753. .tag = tag,
  754. .buffer = buffer,
  755. .disk = disk,
  756. };
  757. sp += 1;
  758. tmask = 0;
  759. ttag = 0;
  760. begin = 0;
  761. end = 0;
  762. diff = 0;
  763. cb = lfs_dir_traverse_filter;
  764. data = &stack[sp-1].tag;
  765. continue;
  766. }
  767. }
  768. popped:
  769. // in filter range?
  770. if (lfs_tag_id(tmask) != 0 &&
  771. !(lfs_tag_id(tag) >= begin && lfs_tag_id(tag) < end)) {
  772. continue;
  773. }
  774. // handle special cases for mcu-side operations
  775. if (lfs_tag_type3(tag) == LFS_FROM_NOOP) {
  776. // do nothing
  777. } else if (lfs_tag_type3(tag) == LFS_FROM_MOVE) {
  778. // Without this condition, lfs_dir_traverse can exhibit an
  779. // extremely expensive O(n^3) of nested loops when renaming.
  780. // This happens because lfs_dir_traverse tries to filter tags by
  781. // the tags in the source directory, triggering a second
  782. // lfs_dir_traverse with its own filter operation.
  783. //
  784. // traverse with commit
  785. // '-> traverse with filter
  786. // '-> traverse with move
  787. // '-> traverse with filter
  788. //
  789. // However we don't actually care about filtering the second set of
  790. // tags, since duplicate tags have no effect when filtering.
  791. //
  792. // This check skips this unnecessary recursive filtering explicitly,
  793. // reducing this runtime from O(n^3) to O(n^2).
  794. if (cb == lfs_dir_traverse_filter) {
  795. continue;
  796. }
  797. // recurse into move
  798. stack[sp] = (struct lfs_dir_traverse){
  799. .dir = dir,
  800. .off = off,
  801. .ptag = ptag,
  802. .attrs = attrs,
  803. .attrcount = attrcount,
  804. .tmask = tmask,
  805. .ttag = ttag,
  806. .begin = begin,
  807. .end = end,
  808. .diff = diff,
  809. .cb = cb,
  810. .data = data,
  811. .tag = LFS_MKTAG(LFS_FROM_NOOP, 0, 0),
  812. };
  813. sp += 1;
  814. uint16_t fromid = lfs_tag_size(tag);
  815. uint16_t toid = lfs_tag_id(tag);
  816. dir = buffer;
  817. off = 0;
  818. ptag = 0xffffffff;
  819. attrs = NULL;
  820. attrcount = 0;
  821. tmask = LFS_MKTAG(0x600, 0x3ff, 0);
  822. ttag = LFS_MKTAG(LFS_TYPE_STRUCT, 0, 0);
  823. begin = fromid;
  824. end = fromid+1;
  825. diff = toid-fromid+diff;
  826. } else if (lfs_tag_type3(tag) == LFS_FROM_USERATTRS) {
  827. for (unsigned i = 0; i < lfs_tag_size(tag); i++) {
  828. const struct lfs_attr *a = buffer;
  829. res = cb(data, LFS_MKTAG(LFS_TYPE_USERATTR + a[i].type,
  830. lfs_tag_id(tag) + diff, a[i].size), a[i].buffer);
  831. if (res < 0) {
  832. return res;
  833. }
  834. if (res) {
  835. break;
  836. }
  837. }
  838. } else {
  839. res = cb(data, tag + LFS_MKTAG(0, diff, 0), buffer);
  840. if (res < 0) {
  841. return res;
  842. }
  843. if (res) {
  844. break;
  845. }
  846. }
  847. }
  848. if (sp > 0) {
  849. // pop from the stack and return, fortunately all pops share
  850. // a destination
  851. dir = stack[sp-1].dir;
  852. off = stack[sp-1].off;
  853. ptag = stack[sp-1].ptag;
  854. attrs = stack[sp-1].attrs;
  855. attrcount = stack[sp-1].attrcount;
  856. tmask = stack[sp-1].tmask;
  857. ttag = stack[sp-1].ttag;
  858. begin = stack[sp-1].begin;
  859. end = stack[sp-1].end;
  860. diff = stack[sp-1].diff;
  861. cb = stack[sp-1].cb;
  862. data = stack[sp-1].data;
  863. tag = stack[sp-1].tag;
  864. buffer = stack[sp-1].buffer;
  865. disk = stack[sp-1].disk;
  866. sp -= 1;
  867. goto popped;
  868. } else {
  869. return res;
  870. }
  871. }
  872. #endif
  873. static lfs_stag_t lfs_dir_fetchmatch(lfs_t *lfs,
  874. lfs_mdir_t *dir, const lfs_block_t pair[2],
  875. lfs_tag_t fmask, lfs_tag_t ftag, uint16_t *id,
  876. int (*cb)(void *data, lfs_tag_t tag, const void *buffer), void *data) {
  877. // we can find tag very efficiently during a fetch, since we're already
  878. // scanning the entire directory
  879. lfs_stag_t besttag = -1;
  880. // if either block address is invalid we return LFS_ERR_CORRUPT here,
  881. // otherwise later writes to the pair could fail
  882. if (pair[0] >= lfs->cfg->block_count || pair[1] >= lfs->cfg->block_count) {
  883. return LFS_ERR_CORRUPT;
  884. }
  885. // find the block with the most recent revision
  886. uint32_t revs[2] = {0, 0};
  887. int r = 0;
  888. for (int i = 0; i < 2; i++) {
  889. int err = lfs_bd_read(lfs,
  890. NULL, &lfs->rcache, sizeof(revs[i]),
  891. pair[i], 0, &revs[i], sizeof(revs[i]));
  892. revs[i] = lfs_fromle32(revs[i]);
  893. if (err && err != LFS_ERR_CORRUPT) {
  894. return err;
  895. }
  896. if (err != LFS_ERR_CORRUPT &&
  897. lfs_scmp(revs[i], revs[(i+1)%2]) > 0) {
  898. r = i;
  899. }
  900. }
  901. dir->pair[0] = pair[(r+0)%2];
  902. dir->pair[1] = pair[(r+1)%2];
  903. dir->rev = revs[(r+0)%2];
  904. dir->off = 0; // nonzero = found some commits
  905. // now scan tags to fetch the actual dir and find possible match
  906. for (int i = 0; i < 2; i++) {
  907. lfs_off_t off = 0;
  908. lfs_tag_t ptag = 0xffffffff;
  909. uint16_t tempcount = 0;
  910. lfs_block_t temptail[2] = {LFS_BLOCK_NULL, LFS_BLOCK_NULL};
  911. bool tempsplit = false;
  912. lfs_stag_t tempbesttag = besttag;
  913. dir->rev = lfs_tole32(dir->rev);
  914. uint32_t crc = lfs_crc(0xffffffff, &dir->rev, sizeof(dir->rev));
  915. dir->rev = lfs_fromle32(dir->rev);
  916. while (true) {
  917. // extract next tag
  918. lfs_tag_t tag;
  919. off += lfs_tag_dsize(ptag);
  920. int err = lfs_bd_read(lfs,
  921. NULL, &lfs->rcache, lfs->cfg->block_size,
  922. dir->pair[0], off, &tag, sizeof(tag));
  923. if (err) {
  924. if (err == LFS_ERR_CORRUPT) {
  925. // can't continue?
  926. dir->erased = false;
  927. break;
  928. }
  929. return err;
  930. }
  931. crc = lfs_crc(crc, &tag, sizeof(tag));
  932. tag = (lfs_frombe32(tag) ^ ptag) & 0x7fffffff;
  933. // out of range?
  934. if (off + lfs_tag_dsize(tag) > lfs->cfg->block_size) {
  935. dir->erased = false;
  936. break;
  937. }
  938. ptag = tag;
  939. if (lfs_tag_type1(tag) == LFS_TYPE_CRC) {
  940. lfs_off_t noff = off + sizeof(tag);
  941. struct lfs_estate estate;
  942. if (lfs_tag_chunk(tag) == 3) {
  943. err = lfs_bd_read(lfs,
  944. NULL, &lfs->rcache, lfs->cfg->block_size,
  945. dir->pair[0], noff, &estate, sizeof(estate));
  946. if (err) {
  947. if (err == LFS_ERR_CORRUPT) {
  948. dir->erased = false;
  949. break;
  950. }
  951. return err;
  952. }
  953. crc = lfs_crc(crc, &estate, sizeof(estate));
  954. lfs_estate_fromle32(&estate);
  955. noff += sizeof(estate);
  956. }
  957. // check the crc attr
  958. uint32_t dcrc;
  959. err = lfs_bd_read(lfs,
  960. NULL, &lfs->rcache, lfs->cfg->block_size,
  961. dir->pair[0], noff, &dcrc, sizeof(dcrc));
  962. if (err) {
  963. if (err == LFS_ERR_CORRUPT) {
  964. dir->erased = false;
  965. break;
  966. }
  967. return err;
  968. }
  969. dcrc = lfs_fromle32(dcrc);
  970. if (crc != dcrc) {
  971. dir->erased = false;
  972. break;
  973. }
  974. // toss our crc into the filesystem seed for
  975. // pseudorandom numbers, note we use another crc here
  976. // as a collection function because it is sufficiently
  977. // random and convenient
  978. lfs->seed = lfs_crc(lfs->seed, &crc, sizeof(crc));
  979. // update with what's found so far
  980. besttag = tempbesttag;
  981. dir->off = off + lfs_tag_dsize(tag);
  982. dir->etag = ptag;
  983. dir->count = tempcount;
  984. dir->tail[0] = temptail[0];
  985. dir->tail[1] = temptail[1];
  986. dir->split = tempsplit;
  987. // reset crc
  988. crc = 0xffffffff;
  989. // check if the next page is erased
  990. //
  991. // this may look inefficient, but it's surprisingly efficient
  992. // since cache_size is probably > prog_size, so the data will
  993. // always remain in cache for the next iteration
  994. if (lfs_tag_chunk(tag) == 3) {
  995. // first we get a tag-worth of bits, this is so we can
  996. // tweak our current tag to force future writes to be
  997. // different than the erased state
  998. lfs_tag_t etag;
  999. err = lfs_bd_read(lfs,
  1000. NULL, &lfs->rcache, lfs->cfg->block_size,
  1001. dir->pair[0], dir->off, &etag, sizeof(etag));
  1002. if (err) {
  1003. // TODO can we stop duplicating this error condition?
  1004. if (err == LFS_ERR_CORRUPT) {
  1005. dir->erased = false;
  1006. break;
  1007. }
  1008. return err;
  1009. }
  1010. // perturb valid bit?
  1011. dir->etag |= 0x80000000 & ~lfs_frombe32(etag);
  1012. // crc the rest the full prog_size, including etag in case
  1013. // the actual esize is < tag size (though this shouldn't
  1014. // happen normally)
  1015. uint32_t tcrc = 0xffffffff;
  1016. err = lfs_bd_crc(lfs,
  1017. NULL, &lfs->rcache, lfs->cfg->block_size,
  1018. dir->pair[0], dir->off, estate.size, &tcrc);
  1019. if (err) {
  1020. if (err == LFS_ERR_CORRUPT) {
  1021. dir->erased = false;
  1022. break;
  1023. }
  1024. return err;
  1025. }
  1026. if (tcrc == estate.crc) {
  1027. dir->erased = true;
  1028. break;
  1029. }
  1030. } else {
  1031. // end of block commit
  1032. // TODO handle backwards compat?
  1033. dir->erased = false;
  1034. break;
  1035. }
  1036. continue;
  1037. }
  1038. // crc the entry first, hopefully leaving it in the cache
  1039. err = lfs_bd_crc(lfs,
  1040. NULL, &lfs->rcache, lfs->cfg->block_size,
  1041. dir->pair[0], off+sizeof(tag),
  1042. lfs_tag_dsize(tag)-sizeof(tag), &crc);
  1043. if (err) {
  1044. if (err == LFS_ERR_CORRUPT) {
  1045. dir->erased = false;
  1046. break;
  1047. }
  1048. return err;
  1049. }
  1050. // directory modification tags?
  1051. if (lfs_tag_type1(tag) == LFS_TYPE_NAME) {
  1052. // increase count of files if necessary
  1053. if (lfs_tag_id(tag) >= tempcount) {
  1054. tempcount = lfs_tag_id(tag) + 1;
  1055. }
  1056. } else if (lfs_tag_type1(tag) == LFS_TYPE_SPLICE) {
  1057. tempcount += lfs_tag_splice(tag);
  1058. if (tag == (LFS_MKTAG(LFS_TYPE_DELETE, 0, 0) |
  1059. (LFS_MKTAG(0, 0x3ff, 0) & tempbesttag))) {
  1060. tempbesttag |= 0x80000000;
  1061. } else if (tempbesttag != -1 &&
  1062. lfs_tag_id(tag) <= lfs_tag_id(tempbesttag)) {
  1063. tempbesttag += LFS_MKTAG(0, lfs_tag_splice(tag), 0);
  1064. }
  1065. } else if (lfs_tag_type1(tag) == LFS_TYPE_TAIL) {
  1066. tempsplit = (lfs_tag_chunk(tag) & 1);
  1067. err = lfs_bd_read(lfs,
  1068. NULL, &lfs->rcache, lfs->cfg->block_size,
  1069. dir->pair[0], off+sizeof(tag), &temptail, 8);
  1070. if (err) {
  1071. if (err == LFS_ERR_CORRUPT) {
  1072. dir->erased = false;
  1073. break;
  1074. }
  1075. return err;
  1076. }
  1077. lfs_pair_fromle32(temptail);
  1078. }
  1079. // found a match for our fetcher?
  1080. if ((fmask & tag) == (fmask & ftag)) {
  1081. int res = cb(data, tag, &(struct lfs_diskoff){
  1082. dir->pair[0], off+sizeof(tag)});
  1083. if (res < 0) {
  1084. if (res == LFS_ERR_CORRUPT) {
  1085. dir->erased = false;
  1086. break;
  1087. }
  1088. return res;
  1089. }
  1090. if (res == LFS_CMP_EQ) {
  1091. // found a match
  1092. tempbesttag = tag;
  1093. } else if ((LFS_MKTAG(0x7ff, 0x3ff, 0) & tag) ==
  1094. (LFS_MKTAG(0x7ff, 0x3ff, 0) & tempbesttag)) {
  1095. // found an identical tag, but contents didn't match
  1096. // this must mean that our besttag has been overwritten
  1097. tempbesttag = -1;
  1098. } else if (res == LFS_CMP_GT &&
  1099. lfs_tag_id(tag) <= lfs_tag_id(tempbesttag)) {
  1100. // found a greater match, keep track to keep things sorted
  1101. tempbesttag = tag | 0x80000000;
  1102. }
  1103. }
  1104. }
  1105. // consider what we have good enough
  1106. if (dir->off > 0) {
  1107. // synthetic move
  1108. if (lfs_gstate_hasmovehere(&lfs->gdisk, dir->pair)) {
  1109. if (lfs_tag_id(lfs->gdisk.tag) == lfs_tag_id(besttag)) {
  1110. besttag |= 0x80000000;
  1111. } else if (besttag != -1 &&
  1112. lfs_tag_id(lfs->gdisk.tag) < lfs_tag_id(besttag)) {
  1113. besttag -= LFS_MKTAG(0, 1, 0);
  1114. }
  1115. }
  1116. // found tag? or found best id?
  1117. if (id) {
  1118. *id = lfs_min(lfs_tag_id(besttag), dir->count);
  1119. }
  1120. if (lfs_tag_isvalid(besttag)) {
  1121. return besttag;
  1122. } else if (lfs_tag_id(besttag) < dir->count) {
  1123. return LFS_ERR_NOENT;
  1124. } else {
  1125. return 0;
  1126. }
  1127. }
  1128. // failed, try the other block?
  1129. lfs_pair_swap(dir->pair);
  1130. dir->rev = revs[(r+1)%2];
  1131. }
  1132. LFS_ERROR("Corrupted dir pair at {0x%"PRIx32", 0x%"PRIx32"}",
  1133. dir->pair[0], dir->pair[1]);
  1134. return LFS_ERR_CORRUPT;
  1135. }
  1136. static int lfs_dir_fetch(lfs_t *lfs,
  1137. lfs_mdir_t *dir, const lfs_block_t pair[2]) {
  1138. // note, mask=-1, tag=-1 can never match a tag since this
  1139. // pattern has the invalid bit set
  1140. return (int)lfs_dir_fetchmatch(lfs, dir, pair,
  1141. (lfs_tag_t)-1, (lfs_tag_t)-1, NULL, NULL, NULL);
  1142. }
  1143. static int lfs_dir_getgstate(lfs_t *lfs, const lfs_mdir_t *dir,
  1144. lfs_gstate_t *gstate) {
  1145. lfs_gstate_t temp;
  1146. lfs_stag_t res = lfs_dir_get(lfs, dir, LFS_MKTAG(0x7ff, 0, 0),
  1147. LFS_MKTAG(LFS_TYPE_MOVESTATE, 0, sizeof(temp)), &temp);
  1148. if (res < 0 && res != LFS_ERR_NOENT) {
  1149. return res;
  1150. }
  1151. if (res != LFS_ERR_NOENT) {
  1152. // xor together to find resulting gstate
  1153. lfs_gstate_fromle32(&temp);
  1154. lfs_gstate_xor(gstate, &temp);
  1155. }
  1156. return 0;
  1157. }
  1158. static int lfs_dir_getinfo(lfs_t *lfs, lfs_mdir_t *dir,
  1159. uint16_t id, struct lfs_info *info) {
  1160. if (id == 0x3ff) {
  1161. // special case for root
  1162. strcpy(info->name, "/");
  1163. info->type = LFS_TYPE_DIR;
  1164. return 0;
  1165. }
  1166. lfs_stag_t tag = lfs_dir_get(lfs, dir, LFS_MKTAG(0x780, 0x3ff, 0),
  1167. LFS_MKTAG(LFS_TYPE_NAME, id, lfs->name_max+1), info->name);
  1168. if (tag < 0) {
  1169. return (int)tag;
  1170. }
  1171. info->type = lfs_tag_type3(tag);
  1172. struct lfs_ctz ctz;
  1173. tag = lfs_dir_get(lfs, dir, LFS_MKTAG(0x700, 0x3ff, 0),
  1174. LFS_MKTAG(LFS_TYPE_STRUCT, id, sizeof(ctz)), &ctz);
  1175. if (tag < 0) {
  1176. return (int)tag;
  1177. }
  1178. lfs_ctz_fromle32(&ctz);
  1179. if (lfs_tag_type3(tag) == LFS_TYPE_CTZSTRUCT) {
  1180. info->size = ctz.size;
  1181. } else if (lfs_tag_type3(tag) == LFS_TYPE_INLINESTRUCT) {
  1182. info->size = lfs_tag_size(tag);
  1183. }
  1184. return 0;
  1185. }
  1186. struct lfs_dir_find_match {
  1187. lfs_t *lfs;
  1188. const void *name;
  1189. lfs_size_t size;
  1190. };
  1191. static int lfs_dir_find_match(void *data,
  1192. lfs_tag_t tag, const void *buffer) {
  1193. struct lfs_dir_find_match *name = data;
  1194. lfs_t *lfs = name->lfs;
  1195. const struct lfs_diskoff *disk = buffer;
  1196. // compare with disk
  1197. lfs_size_t diff = lfs_min(name->size, lfs_tag_size(tag));
  1198. int res = lfs_bd_cmp(lfs,
  1199. NULL, &lfs->rcache, diff,
  1200. disk->block, disk->off, name->name, diff);
  1201. if (res != LFS_CMP_EQ) {
  1202. return res;
  1203. }
  1204. // only equal if our size is still the same
  1205. if (name->size != lfs_tag_size(tag)) {
  1206. return (name->size < lfs_tag_size(tag)) ? LFS_CMP_LT : LFS_CMP_GT;
  1207. }
  1208. // found a match!
  1209. return LFS_CMP_EQ;
  1210. }
  1211. static lfs_stag_t lfs_dir_find(lfs_t *lfs, lfs_mdir_t *dir,
  1212. const char **path, uint16_t *id) {
  1213. // we reduce path to a single name if we can find it
  1214. const char *name = *path;
  1215. if (id) {
  1216. *id = 0x3ff;
  1217. }
  1218. // default to root dir
  1219. lfs_stag_t tag = LFS_MKTAG(LFS_TYPE_DIR, 0x3ff, 0);
  1220. dir->tail[0] = lfs->root[0];
  1221. dir->tail[1] = lfs->root[1];
  1222. while (true) {
  1223. nextname:
  1224. // skip slashes
  1225. name += strspn(name, "/");
  1226. lfs_size_t namelen = strcspn(name, "/");
  1227. // skip '.' and root '..'
  1228. if ((namelen == 1 && memcmp(name, ".", 1) == 0) ||
  1229. (namelen == 2 && memcmp(name, "..", 2) == 0)) {
  1230. name += namelen;
  1231. goto nextname;
  1232. }
  1233. // skip if matched by '..' in name
  1234. const char *suffix = name + namelen;
  1235. lfs_size_t sufflen;
  1236. int depth = 1;
  1237. while (true) {
  1238. suffix += strspn(suffix, "/");
  1239. sufflen = strcspn(suffix, "/");
  1240. if (sufflen == 0) {
  1241. break;
  1242. }
  1243. if (sufflen == 2 && memcmp(suffix, "..", 2) == 0) {
  1244. depth -= 1;
  1245. if (depth == 0) {
  1246. name = suffix + sufflen;
  1247. goto nextname;
  1248. }
  1249. } else {
  1250. depth += 1;
  1251. }
  1252. suffix += sufflen;
  1253. }
  1254. // found path
  1255. if (name[0] == '\0') {
  1256. return tag;
  1257. }
  1258. // update what we've found so far
  1259. *path = name;
  1260. // only continue if we hit a directory
  1261. if (lfs_tag_type3(tag) != LFS_TYPE_DIR) {
  1262. return LFS_ERR_NOTDIR;
  1263. }
  1264. // grab the entry data
  1265. if (lfs_tag_id(tag) != 0x3ff) {
  1266. lfs_stag_t res = lfs_dir_get(lfs, dir, LFS_MKTAG(0x700, 0x3ff, 0),
  1267. LFS_MKTAG(LFS_TYPE_STRUCT, lfs_tag_id(tag), 8), dir->tail);
  1268. if (res < 0) {
  1269. return res;
  1270. }
  1271. lfs_pair_fromle32(dir->tail);
  1272. }
  1273. // find entry matching name
  1274. while (true) {
  1275. tag = lfs_dir_fetchmatch(lfs, dir, dir->tail,
  1276. LFS_MKTAG(0x780, 0, 0),
  1277. LFS_MKTAG(LFS_TYPE_NAME, 0, namelen),
  1278. // are we last name?
  1279. (strchr(name, '/') == NULL) ? id : NULL,
  1280. lfs_dir_find_match, &(struct lfs_dir_find_match){
  1281. lfs, name, namelen});
  1282. if (tag < 0) {
  1283. return tag;
  1284. }
  1285. if (tag) {
  1286. break;
  1287. }
  1288. if (!dir->split) {
  1289. return LFS_ERR_NOENT;
  1290. }
  1291. }
  1292. // to next name
  1293. name += namelen;
  1294. }
  1295. }
  1296. // commit logic
  1297. struct lfs_commit {
  1298. lfs_block_t block;
  1299. lfs_off_t off;
  1300. lfs_tag_t ptag;
  1301. uint32_t crc;
  1302. lfs_off_t begin;
  1303. lfs_off_t end;
  1304. };
  1305. #ifndef LFS_READONLY
  1306. static int lfs_dir_commitprog(lfs_t *lfs, struct lfs_commit *commit,
  1307. const void *buffer, lfs_size_t size) {
  1308. int err = lfs_bd_prog(lfs,
  1309. &lfs->pcache, &lfs->rcache, false,
  1310. commit->block, commit->off ,
  1311. (const uint8_t*)buffer, size);
  1312. if (err) {
  1313. return err;
  1314. }
  1315. commit->crc = lfs_crc(commit->crc, buffer, size);
  1316. commit->off += size;
  1317. return 0;
  1318. }
  1319. #endif
  1320. #ifndef LFS_READONLY
  1321. static int lfs_dir_commitattr(lfs_t *lfs, struct lfs_commit *commit,
  1322. lfs_tag_t tag, const void *buffer) {
  1323. // check if we fit
  1324. lfs_size_t dsize = lfs_tag_dsize(tag);
  1325. if (commit->off + dsize > commit->end) {
  1326. return LFS_ERR_NOSPC;
  1327. }
  1328. // write out tag
  1329. lfs_tag_t ntag = lfs_tobe32((tag & 0x7fffffff) ^ commit->ptag);
  1330. int err = lfs_dir_commitprog(lfs, commit, &ntag, sizeof(ntag));
  1331. if (err) {
  1332. return err;
  1333. }
  1334. if (!(tag & 0x80000000)) {
  1335. // from memory
  1336. err = lfs_dir_commitprog(lfs, commit, buffer, dsize-sizeof(tag));
  1337. if (err) {
  1338. return err;
  1339. }
  1340. } else {
  1341. // from disk
  1342. const struct lfs_diskoff *disk = buffer;
  1343. for (lfs_off_t i = 0; i < dsize-sizeof(tag); i++) {
  1344. // rely on caching to make this efficient
  1345. uint8_t dat;
  1346. err = lfs_bd_read(lfs,
  1347. NULL, &lfs->rcache, dsize-sizeof(tag)-i,
  1348. disk->block, disk->off+i, &dat, 1);
  1349. if (err) {
  1350. return err;
  1351. }
  1352. err = lfs_dir_commitprog(lfs, commit, &dat, 1);
  1353. if (err) {
  1354. return err;
  1355. }
  1356. }
  1357. }
  1358. commit->ptag = tag & 0x7fffffff;
  1359. return 0;
  1360. }
  1361. #endif
  1362. #ifndef LFS_READONLY
  1363. static int lfs_dir_commitcrc(lfs_t *lfs, struct lfs_commit *commit) {
  1364. // align to program units
  1365. //
  1366. // this gets a bit complex as we have two types of crcs:
  1367. // - 4-word crc with estate to check following prog (middle of block)
  1368. // - 2-word crc with no following prog (end of block)
  1369. const lfs_off_t end = lfs_alignup(
  1370. lfs_min(commit->off + 4*sizeof(uint32_t), lfs->cfg->block_size),
  1371. lfs->cfg->prog_size);
  1372. // clamp erase size to tag size, this gives us the full tag as potential
  1373. // to intentionally invalidate erase CRCs
  1374. const lfs_size_t esize = lfs_max(lfs->cfg->prog_size, sizeof(lfs_tag_t));
  1375. lfs_off_t off1 = 0;
  1376. uint32_t crc1 = 0;
  1377. // create crc tags to fill up remainder of commit, note that
  1378. // padding is not crced, which lets fetches skip padding but
  1379. // makes committing a bit more complicated
  1380. while (commit->off < end) {
  1381. lfs_off_t off = commit->off + sizeof(lfs_tag_t);
  1382. lfs_off_t noff = lfs_min(end - off, 0x3fe) + off;
  1383. if (noff < end) {
  1384. noff = lfs_min(noff, end - 2*sizeof(uint32_t));
  1385. }
  1386. // build crc tag
  1387. lfs_tag_t etag = 0;
  1388. lfs_tag_t tag = LFS_MKTAG(LFS_TYPE_CRC + 2, 0x3ff, noff - off);
  1389. lfs_size_t size = 2*sizeof(uint32_t);
  1390. struct {
  1391. lfs_tag_t tag;
  1392. union {
  1393. struct {
  1394. uint32_t crc;
  1395. } crc2;
  1396. struct {
  1397. struct lfs_estate estate;
  1398. uint32_t crc;
  1399. } crc3;
  1400. } u;
  1401. } data;
  1402. if (noff <= lfs->cfg->block_size - esize) {
  1403. // first we get a tag-worth of bits, this is so we can
  1404. // tweak our current tag to force future writes to be
  1405. // different than the erased state
  1406. int err = lfs_bd_read(lfs,
  1407. NULL, &lfs->rcache, esize,
  1408. commit->block, noff, &etag, sizeof(etag));
  1409. // TODO handle erased-as-corrupt correctly?
  1410. if (err && err != LFS_ERR_CORRUPT) {
  1411. return err;
  1412. }
  1413. // find expected erased state
  1414. uint32_t ecrc = 0xffffffff;
  1415. err = lfs_bd_crc(lfs,
  1416. NULL, &lfs->rcache, esize,
  1417. commit->block, noff, esize, &ecrc);
  1418. // TODO handle erased-as-corrupt correctly?
  1419. if (err && err != LFS_ERR_CORRUPT) {
  1420. return err;
  1421. }
  1422. data.u.crc3.estate.size = esize;
  1423. data.u.crc3.estate.crc = ecrc;
  1424. lfs_estate_tole32(&data.u.crc3.estate);
  1425. // indicate we include estate
  1426. tag |= LFS_MKTAG(1, 0, 0);
  1427. size += sizeof(struct lfs_estate);
  1428. }
  1429. data.tag = lfs_tobe32(tag ^ commit->ptag);
  1430. commit->crc = lfs_crc(commit->crc, &data, size-sizeof(uint32_t));
  1431. ((uint32_t*)&data)[size/sizeof(uint32_t) - 1] = lfs_tole32(commit->crc);
  1432. int err = lfs_bd_prog(lfs,
  1433. &lfs->pcache, &lfs->rcache, false,
  1434. commit->block, commit->off, &data, size);
  1435. if (err) {
  1436. return err;
  1437. }
  1438. // keep track of non-padding checksum to verify
  1439. if (off1 == 0) {
  1440. //off1 = commit->off + sizeof(uint32_t);
  1441. off1 = commit->off + size-sizeof(uint32_t);
  1442. crc1 = commit->crc;
  1443. }
  1444. commit->off += sizeof(tag)+lfs_tag_size(tag);
  1445. // perturb valid bit?
  1446. commit->ptag = tag | (0x80000000 & ~lfs_frombe32(etag));
  1447. // reset crc for next commit
  1448. commit->crc = 0xffffffff;
  1449. }
  1450. // flush buffers
  1451. int err = lfs_bd_sync(lfs, &lfs->pcache, &lfs->rcache, false);
  1452. if (err) {
  1453. return err;
  1454. }
  1455. // successful commit, check checksums to make sure
  1456. lfs_off_t off = commit->begin;
  1457. lfs_off_t noff = off1;
  1458. while (off < end) {
  1459. // TODO restructure to use lfs_bd_crc?
  1460. uint32_t crc = 0xffffffff;
  1461. for (lfs_off_t i = off; i < noff+sizeof(uint32_t); i++) {
  1462. // check against written crc, may catch blocks that
  1463. // become readonly and match our commit size exactly
  1464. if (i == off1 && crc != crc1) {
  1465. return LFS_ERR_CORRUPT;
  1466. }
  1467. uint8_t dat;
  1468. err = lfs_bd_read(lfs,
  1469. NULL, &lfs->rcache, noff+sizeof(uint32_t)-i,
  1470. commit->block, i, &dat, 1);
  1471. if (err) {
  1472. return err;
  1473. }
  1474. crc = lfs_crc(crc, &dat, 1);
  1475. }
  1476. // detected write error?
  1477. if (crc != 0) {
  1478. return LFS_ERR_CORRUPT;
  1479. }
  1480. // skip padding, note that these always contain estate
  1481. off = noff - 3*sizeof(uint32_t);
  1482. off = lfs_min(end - off, 0x3fe) + off;
  1483. if (off < end) {
  1484. off = lfs_min(off, end - 2*sizeof(uint32_t));
  1485. }
  1486. noff = off + sizeof(uint32_t);
  1487. if (noff <= lfs->cfg->block_size - esize) {
  1488. noff += 2*sizeof(uint32_t);
  1489. }
  1490. }
  1491. return 0;
  1492. }
  1493. #endif
  1494. #ifndef LFS_READONLY
  1495. static int lfs_dir_alloc(lfs_t *lfs, lfs_mdir_t *dir) {
  1496. // allocate pair of dir blocks (backwards, so we write block 1 first)
  1497. for (int i = 0; i < 2; i++) {
  1498. int err = lfs_alloc(lfs, &dir->pair[(i+1)%2]);
  1499. if (err) {
  1500. return err;
  1501. }
  1502. }
  1503. // zero for reproducibility in case initial block is unreadable
  1504. dir->rev = 0;
  1505. // rather than clobbering one of the blocks we just pretend
  1506. // the revision may be valid
  1507. int err = lfs_bd_read(lfs,
  1508. NULL, &lfs->rcache, sizeof(dir->rev),
  1509. dir->pair[0], 0, &dir->rev, sizeof(dir->rev));
  1510. dir->rev = lfs_fromle32(dir->rev);
  1511. if (err && err != LFS_ERR_CORRUPT) {
  1512. return err;
  1513. }
  1514. // to make sure we don't immediately evict, align the new revision count
  1515. // to our block_cycles modulus, see lfs_dir_compact for why our modulus
  1516. // is tweaked this way
  1517. if (lfs->cfg->block_cycles > 0) {
  1518. dir->rev = lfs_alignup(dir->rev, ((lfs->cfg->block_cycles+1)|1));
  1519. }
  1520. // set defaults
  1521. dir->off = sizeof(dir->rev);
  1522. dir->etag = 0xffffffff;
  1523. dir->count = 0;
  1524. dir->tail[0] = LFS_BLOCK_NULL;
  1525. dir->tail[1] = LFS_BLOCK_NULL;
  1526. dir->erased = false;
  1527. dir->split = false;
  1528. // don't write out yet, let caller take care of that
  1529. return 0;
  1530. }
  1531. #endif
  1532. #ifndef LFS_READONLY
  1533. static int lfs_dir_drop(lfs_t *lfs, lfs_mdir_t *dir, lfs_mdir_t *tail) {
  1534. // steal state
  1535. int err = lfs_dir_getgstate(lfs, tail, &lfs->gdelta);
  1536. if (err) {
  1537. return err;
  1538. }
  1539. // steal tail
  1540. lfs_pair_tole32(tail->tail);
  1541. err = lfs_dir_commit(lfs, dir, LFS_MKATTRS(
  1542. {LFS_MKTAG(LFS_TYPE_TAIL + tail->split, 0x3ff, 8), tail->tail}));
  1543. lfs_pair_fromle32(tail->tail);
  1544. if (err) {
  1545. return err;
  1546. }
  1547. return 0;
  1548. }
  1549. #endif
  1550. #ifndef LFS_READONLY
  1551. static int lfs_dir_split(lfs_t *lfs,
  1552. lfs_mdir_t *dir, const struct lfs_mattr *attrs, int attrcount,
  1553. lfs_mdir_t *source, uint16_t split, uint16_t end) {
  1554. // create tail metadata pair
  1555. lfs_mdir_t tail;
  1556. int err = lfs_dir_alloc(lfs, &tail);
  1557. if (err) {
  1558. return err;
  1559. }
  1560. tail.split = dir->split;
  1561. tail.tail[0] = dir->tail[0];
  1562. tail.tail[1] = dir->tail[1];
  1563. // note we don't care about LFS_OK_RELOCATED
  1564. int res = lfs_dir_compact(lfs, &tail, attrs, attrcount, source, split, end);
  1565. if (res < 0) {
  1566. return res;
  1567. }
  1568. dir->tail[0] = tail.pair[0];
  1569. dir->tail[1] = tail.pair[1];
  1570. dir->split = true;
  1571. // update root if needed
  1572. if (lfs_pair_cmp(dir->pair, lfs->root) == 0 && split == 0) {
  1573. lfs->root[0] = tail.pair[0];
  1574. lfs->root[1] = tail.pair[1];
  1575. }
  1576. return 0;
  1577. }
  1578. #endif
  1579. #ifndef LFS_READONLY
  1580. static int lfs_dir_commit_size(void *p, lfs_tag_t tag, const void *buffer) {
  1581. lfs_size_t *size = p;
  1582. (void)buffer;
  1583. *size += lfs_tag_dsize(tag);
  1584. return 0;
  1585. }
  1586. #endif
  1587. #ifndef LFS_READONLY
  1588. struct lfs_dir_commit_commit {
  1589. lfs_t *lfs;
  1590. struct lfs_commit *commit;
  1591. };
  1592. #endif
  1593. #ifndef LFS_READONLY
  1594. static int lfs_dir_commit_commit(void *p, lfs_tag_t tag, const void *buffer) {
  1595. struct lfs_dir_commit_commit *commit = p;
  1596. return lfs_dir_commitattr(commit->lfs, commit->commit, tag, buffer);
  1597. }
  1598. #endif
  1599. #ifndef LFS_READONLY
  1600. static bool lfs_dir_needsrelocation(lfs_t *lfs, lfs_mdir_t *dir) {
  1601. // If our revision count == n * block_cycles, we should force a relocation,
  1602. // this is how littlefs wear-levels at the metadata-pair level. Note that we
  1603. // actually use (block_cycles+1)|1, this is to avoid two corner cases:
  1604. // 1. block_cycles = 1, which would prevent relocations from terminating
  1605. // 2. block_cycles = 2n, which, due to aliasing, would only ever relocate
  1606. // one metadata block in the pair, effectively making this useless
  1607. return (lfs->cfg->block_cycles > 0
  1608. && ((dir->rev + 1) % ((lfs->cfg->block_cycles+1)|1) == 0));
  1609. }
  1610. #endif
  1611. #ifndef LFS_READONLY
  1612. static int lfs_dir_compact(lfs_t *lfs,
  1613. lfs_mdir_t *dir, const struct lfs_mattr *attrs, int attrcount,
  1614. lfs_mdir_t *source, uint16_t begin, uint16_t end) {
  1615. // save some state in case block is bad
  1616. bool relocated = false;
  1617. bool tired = lfs_dir_needsrelocation(lfs, dir);
  1618. // increment revision count
  1619. dir->rev += 1;
  1620. // do not proactively relocate blocks during migrations, this
  1621. // can cause a number of failure states such: clobbering the
  1622. // v1 superblock if we relocate root, and invalidating directory
  1623. // pointers if we relocate the head of a directory. On top of
  1624. // this, relocations increase the overall complexity of
  1625. // lfs_migration, which is already a delicate operation.
  1626. #ifdef LFS_MIGRATE
  1627. if (lfs->lfs1) {
  1628. tired = false;
  1629. }
  1630. #endif
  1631. if (tired && lfs_pair_cmp(dir->pair, (const lfs_block_t[2]){0, 1}) != 0) {
  1632. // we're writing too much, time to relocate
  1633. goto relocate;
  1634. }
  1635. // begin loop to commit compaction to blocks until a compact sticks
  1636. while (true) {
  1637. {
  1638. // setup commit state
  1639. struct lfs_commit commit = {
  1640. .block = dir->pair[1],
  1641. .off = 0,
  1642. .ptag = 0xffffffff,
  1643. .crc = 0xffffffff,
  1644. .begin = 0,
  1645. .end = (lfs->cfg->metadata_max ?
  1646. lfs->cfg->metadata_max : lfs->cfg->block_size) - 8,
  1647. };
  1648. // erase block to write to
  1649. int err = lfs_bd_erase(lfs, dir->pair[1]);
  1650. if (err) {
  1651. if (err == LFS_ERR_CORRUPT) {
  1652. goto relocate;
  1653. }
  1654. return err;
  1655. }
  1656. // write out header
  1657. dir->rev = lfs_tole32(dir->rev);
  1658. err = lfs_dir_commitprog(lfs, &commit,
  1659. &dir->rev, sizeof(dir->rev));
  1660. dir->rev = lfs_fromle32(dir->rev);
  1661. if (err) {
  1662. if (err == LFS_ERR_CORRUPT) {
  1663. goto relocate;
  1664. }
  1665. return err;
  1666. }
  1667. // traverse the directory, this time writing out all unique tags
  1668. err = lfs_dir_traverse(lfs,
  1669. source, 0, 0xffffffff, attrs, attrcount,
  1670. LFS_MKTAG(0x400, 0x3ff, 0),
  1671. LFS_MKTAG(LFS_TYPE_NAME, 0, 0),
  1672. begin, end, -begin,
  1673. lfs_dir_commit_commit, &(struct lfs_dir_commit_commit){
  1674. lfs, &commit});
  1675. if (err) {
  1676. if (err == LFS_ERR_CORRUPT) {
  1677. goto relocate;
  1678. }
  1679. return err;
  1680. }
  1681. // commit tail, which may be new after last size check
  1682. if (!lfs_pair_isnull(dir->tail)) {
  1683. lfs_pair_tole32(dir->tail);
  1684. err = lfs_dir_commitattr(lfs, &commit,
  1685. LFS_MKTAG(LFS_TYPE_TAIL + dir->split, 0x3ff, 8),
  1686. dir->tail);
  1687. lfs_pair_fromle32(dir->tail);
  1688. if (err) {
  1689. if (err == LFS_ERR_CORRUPT) {
  1690. goto relocate;
  1691. }
  1692. return err;
  1693. }
  1694. }
  1695. // bring over gstate?
  1696. lfs_gstate_t delta = {0};
  1697. if (!relocated) {
  1698. lfs_gstate_xor(&delta, &lfs->gdisk);
  1699. lfs_gstate_xor(&delta, &lfs->gstate);
  1700. }
  1701. lfs_gstate_xor(&delta, &lfs->gdelta);
  1702. delta.tag &= ~LFS_MKTAG(0, 0, 0x3ff);
  1703. err = lfs_dir_getgstate(lfs, dir, &delta);
  1704. if (err) {
  1705. return err;
  1706. }
  1707. if (!lfs_gstate_iszero(&delta)) {
  1708. lfs_gstate_tole32(&delta);
  1709. err = lfs_dir_commitattr(lfs, &commit,
  1710. LFS_MKTAG(LFS_TYPE_MOVESTATE, 0x3ff,
  1711. sizeof(delta)), &delta);
  1712. if (err) {
  1713. if (err == LFS_ERR_CORRUPT) {
  1714. goto relocate;
  1715. }
  1716. return err;
  1717. }
  1718. }
  1719. // complete commit with crc
  1720. err = lfs_dir_commitcrc(lfs, &commit);
  1721. if (err) {
  1722. if (err == LFS_ERR_CORRUPT) {
  1723. goto relocate;
  1724. }
  1725. return err;
  1726. }
  1727. // successful compaction, swap dir pair to indicate most recent
  1728. LFS_ASSERT(commit.off % lfs->cfg->prog_size == 0);
  1729. lfs_pair_swap(dir->pair);
  1730. dir->count = end - begin;
  1731. dir->off = commit.off;
  1732. dir->etag = commit.ptag;
  1733. // update gstate
  1734. lfs->gdelta = (lfs_gstate_t){0};
  1735. if (!relocated) {
  1736. lfs->gdisk = lfs->gstate;
  1737. }
  1738. }
  1739. break;
  1740. relocate:
  1741. // commit was corrupted, drop caches and prepare to relocate block
  1742. relocated = true;
  1743. lfs_cache_drop(lfs, &lfs->pcache);
  1744. if (!tired) {
  1745. LFS_DEBUG("Bad block at 0x%"PRIx32, dir->pair[1]);
  1746. }
  1747. // can't relocate superblock, filesystem is now frozen
  1748. if (lfs_pair_cmp(dir->pair, (const lfs_block_t[2]){0, 1}) == 0) {
  1749. LFS_WARN("Superblock 0x%"PRIx32" has become unwritable",
  1750. dir->pair[1]);
  1751. return LFS_ERR_NOSPC;
  1752. }
  1753. // relocate half of pair
  1754. int err = lfs_alloc(lfs, &dir->pair[1]);
  1755. if (err && (err != LFS_ERR_NOSPC || !tired)) {
  1756. return err;
  1757. }
  1758. tired = false;
  1759. continue;
  1760. }
  1761. return relocated ? LFS_OK_RELOCATED : 0;
  1762. }
  1763. #endif
  1764. #ifndef LFS_READONLY
  1765. static int lfs_dir_splittingcompact(lfs_t *lfs, lfs_mdir_t *dir,
  1766. const struct lfs_mattr *attrs, int attrcount,
  1767. lfs_mdir_t *source, uint16_t begin, uint16_t end) {
  1768. while (true) {
  1769. // find size of first split, we do this by halving the split until
  1770. // the metadata is guaranteed to fit
  1771. //
  1772. // Note that this isn't a true binary search, we never increase the
  1773. // split size. This may result in poorly distributed metadata but isn't
  1774. // worth the extra code size or performance hit to fix.
  1775. lfs_size_t split = begin;
  1776. while (end - split > 1) {
  1777. lfs_size_t size = 0;
  1778. int err = lfs_dir_traverse(lfs,
  1779. source, 0, 0xffffffff, attrs, attrcount,
  1780. LFS_MKTAG(0x400, 0x3ff, 0),
  1781. LFS_MKTAG(LFS_TYPE_NAME, 0, 0),
  1782. split, end, -split,
  1783. lfs_dir_commit_size, &size);
  1784. if (err) {
  1785. return err;
  1786. }
  1787. // space is complicated, we need room for:
  1788. //
  1789. // - tail: 4+2*4 = 12 bytes
  1790. // - gstate: 4+3*4 = 16 bytes
  1791. // - move delete: 4 = 4 bytes
  1792. // - crc: 4+4 = 8 bytes
  1793. // total = 40 bytes
  1794. //
  1795. // And we cap at half a block to avoid degenerate cases with
  1796. // nearly-full metadata blocks.
  1797. //
  1798. if (end - split < 0xff
  1799. && size <= lfs_min(
  1800. lfs->cfg->block_size - 40,
  1801. lfs_alignup(
  1802. (lfs->cfg->metadata_max
  1803. ? lfs->cfg->metadata_max
  1804. : lfs->cfg->block_size)/2,
  1805. lfs->cfg->prog_size))) {
  1806. break;
  1807. }
  1808. split = split + ((end - split) / 2);
  1809. }
  1810. if (split == begin) {
  1811. // no split needed
  1812. break;
  1813. }
  1814. // split into two metadata pairs and continue
  1815. int err = lfs_dir_split(lfs, dir, attrs, attrcount,
  1816. source, split, end);
  1817. if (err && err != LFS_ERR_NOSPC) {
  1818. return err;
  1819. }
  1820. if (err) {
  1821. // we can't allocate a new block, try to compact with degraded
  1822. // performance
  1823. LFS_WARN("Unable to split {0x%"PRIx32", 0x%"PRIx32"}",
  1824. dir->pair[0], dir->pair[1]);
  1825. break;
  1826. } else {
  1827. end = split;
  1828. }
  1829. }
  1830. if (lfs_dir_needsrelocation(lfs, dir)
  1831. && lfs_pair_cmp(dir->pair, (const lfs_block_t[2]){0, 1}) == 0) {
  1832. // oh no! we're writing too much to the superblock,
  1833. // should we expand?
  1834. lfs_ssize_t size = lfs_fs_rawsize(lfs);
  1835. if (size < 0) {
  1836. return size;
  1837. }
  1838. // do we have extra space? littlefs can't reclaim this space
  1839. // by itself, so expand cautiously
  1840. if ((lfs_size_t)size < lfs->cfg->block_count/2) {
  1841. LFS_DEBUG("Expanding superblock at rev %"PRIu32, dir->rev);
  1842. int err = lfs_dir_split(lfs, dir, attrs, attrcount,
  1843. source, begin, end);
  1844. if (err && err != LFS_ERR_NOSPC) {
  1845. return err;
  1846. }
  1847. if (err) {
  1848. // welp, we tried, if we ran out of space there's not much
  1849. // we can do, we'll error later if we've become frozen
  1850. LFS_WARN("Unable to expand superblock");
  1851. } else {
  1852. end = begin;
  1853. }
  1854. }
  1855. }
  1856. return lfs_dir_compact(lfs, dir, attrs, attrcount, source, begin, end);
  1857. }
  1858. #endif
  1859. #ifndef LFS_READONLY
  1860. static int lfs_dir_relocatingcommit(lfs_t *lfs, lfs_mdir_t *dir,
  1861. const lfs_block_t pair[2],
  1862. const struct lfs_mattr *attrs, int attrcount,
  1863. lfs_mdir_t *pdir) {
  1864. int state = 0;
  1865. // calculate changes to the directory
  1866. bool hasdelete = false;
  1867. for (int i = 0; i < attrcount; i++) {
  1868. if (lfs_tag_type3(attrs[i].tag) == LFS_TYPE_CREATE) {
  1869. dir->count += 1;
  1870. } else if (lfs_tag_type3(attrs[i].tag) == LFS_TYPE_DELETE) {
  1871. LFS_ASSERT(dir->count > 0);
  1872. dir->count -= 1;
  1873. hasdelete = true;
  1874. } else if (lfs_tag_type1(attrs[i].tag) == LFS_TYPE_TAIL) {
  1875. dir->tail[0] = ((lfs_block_t*)attrs[i].buffer)[0];
  1876. dir->tail[1] = ((lfs_block_t*)attrs[i].buffer)[1];
  1877. dir->split = (lfs_tag_chunk(attrs[i].tag) & 1);
  1878. lfs_pair_fromle32(dir->tail);
  1879. }
  1880. }
  1881. // should we actually drop the directory block?
  1882. if (hasdelete && dir->count == 0) {
  1883. LFS_ASSERT(pdir);
  1884. int err = lfs_fs_pred(lfs, dir->pair, pdir);
  1885. if (err && err != LFS_ERR_NOENT) {
  1886. return err;
  1887. }
  1888. if (err != LFS_ERR_NOENT && pdir->split) {
  1889. state = LFS_OK_DROPPED;
  1890. goto fixmlist;
  1891. }
  1892. }
  1893. if (dir->erased) {
  1894. // try to commit
  1895. struct lfs_commit commit = {
  1896. .block = dir->pair[0],
  1897. .off = dir->off,
  1898. .ptag = dir->etag,
  1899. .crc = 0xffffffff,
  1900. .begin = dir->off,
  1901. .end = (lfs->cfg->metadata_max ?
  1902. lfs->cfg->metadata_max : lfs->cfg->block_size) - 8,
  1903. };
  1904. // traverse attrs that need to be written out
  1905. lfs_pair_tole32(dir->tail);
  1906. int err = lfs_dir_traverse(lfs,
  1907. dir, dir->off, dir->etag, attrs, attrcount,
  1908. 0, 0, 0, 0, 0,
  1909. lfs_dir_commit_commit, &(struct lfs_dir_commit_commit){
  1910. lfs, &commit});
  1911. lfs_pair_fromle32(dir->tail);
  1912. if (err) {
  1913. if (err == LFS_ERR_NOSPC || err == LFS_ERR_CORRUPT) {
  1914. goto compact;
  1915. }
  1916. return err;
  1917. }
  1918. // commit any global diffs if we have any
  1919. lfs_gstate_t delta = {0};
  1920. lfs_gstate_xor(&delta, &lfs->gstate);
  1921. lfs_gstate_xor(&delta, &lfs->gdisk);
  1922. lfs_gstate_xor(&delta, &lfs->gdelta);
  1923. delta.tag &= ~LFS_MKTAG(0, 0, 0x3ff);
  1924. if (!lfs_gstate_iszero(&delta)) {
  1925. err = lfs_dir_getgstate(lfs, dir, &delta);
  1926. if (err) {
  1927. return err;
  1928. }
  1929. lfs_gstate_tole32(&delta);
  1930. err = lfs_dir_commitattr(lfs, &commit,
  1931. LFS_MKTAG(LFS_TYPE_MOVESTATE, 0x3ff,
  1932. sizeof(delta)), &delta);
  1933. if (err) {
  1934. if (err == LFS_ERR_NOSPC || err == LFS_ERR_CORRUPT) {
  1935. goto compact;
  1936. }
  1937. return err;
  1938. }
  1939. }
  1940. // finalize commit with the crc
  1941. err = lfs_dir_commitcrc(lfs, &commit);
  1942. if (err) {
  1943. if (err == LFS_ERR_NOSPC || err == LFS_ERR_CORRUPT) {
  1944. goto compact;
  1945. }
  1946. return err;
  1947. }
  1948. // successful commit, update dir
  1949. LFS_ASSERT(commit.off % lfs->cfg->prog_size == 0);
  1950. dir->off = commit.off;
  1951. dir->etag = commit.ptag;
  1952. // and update gstate
  1953. lfs->gdisk = lfs->gstate;
  1954. lfs->gdelta = (lfs_gstate_t){0};
  1955. goto fixmlist;
  1956. }
  1957. compact:
  1958. // fall back to compaction
  1959. lfs_cache_drop(lfs, &lfs->pcache);
  1960. state = lfs_dir_splittingcompact(lfs, dir, attrs, attrcount,
  1961. dir, 0, dir->count);
  1962. if (state < 0) {
  1963. return state;
  1964. }
  1965. goto fixmlist;
  1966. fixmlist:;
  1967. // this complicated bit of logic is for fixing up any active
  1968. // metadata-pairs that we may have affected
  1969. //
  1970. // note we have to make two passes since the mdir passed to
  1971. // lfs_dir_commit could also be in this list, and even then
  1972. // we need to copy the pair so they don't get clobbered if we refetch
  1973. // our mdir.
  1974. lfs_block_t oldpair[2] = {pair[0], pair[1]};
  1975. for (struct lfs_mlist *d = lfs->mlist; d; d = d->next) {
  1976. if (lfs_pair_cmp(d->m.pair, oldpair) == 0) {
  1977. d->m = *dir;
  1978. if (d->m.pair != pair) {
  1979. for (int i = 0; i < attrcount; i++) {
  1980. if (lfs_tag_type3(attrs[i].tag) == LFS_TYPE_DELETE &&
  1981. d->id == lfs_tag_id(attrs[i].tag)) {
  1982. d->m.pair[0] = LFS_BLOCK_NULL;
  1983. d->m.pair[1] = LFS_BLOCK_NULL;
  1984. } else if (lfs_tag_type3(attrs[i].tag) == LFS_TYPE_DELETE &&
  1985. d->id > lfs_tag_id(attrs[i].tag)) {
  1986. d->id -= 1;
  1987. if (d->type == LFS_TYPE_DIR) {
  1988. ((lfs_dir_t*)d)->pos -= 1;
  1989. }
  1990. } else if (lfs_tag_type3(attrs[i].tag) == LFS_TYPE_CREATE &&
  1991. d->id >= lfs_tag_id(attrs[i].tag)) {
  1992. d->id += 1;
  1993. if (d->type == LFS_TYPE_DIR) {
  1994. ((lfs_dir_t*)d)->pos += 1;
  1995. }
  1996. }
  1997. }
  1998. }
  1999. while (d->id >= d->m.count && d->m.split) {
  2000. // we split and id is on tail now
  2001. d->id -= d->m.count;
  2002. int err = lfs_dir_fetch(lfs, &d->m, d->m.tail);
  2003. if (err) {
  2004. return err;
  2005. }
  2006. }
  2007. }
  2008. }
  2009. return state;
  2010. }
  2011. #endif
  2012. #ifndef LFS_READONLY
  2013. static int lfs_dir_orphaningcommit(lfs_t *lfs, lfs_mdir_t *dir,
  2014. const struct lfs_mattr *attrs, int attrcount) {
  2015. // check for any inline files that aren't RAM backed and
  2016. // forcefully evict them, needed for filesystem consistency
  2017. for (lfs_file_t *f = (lfs_file_t*)lfs->mlist; f; f = f->next) {
  2018. if (dir != &f->m && lfs_pair_cmp(f->m.pair, dir->pair) == 0 &&
  2019. f->type == LFS_TYPE_REG && (f->flags & LFS_F_INLINE) &&
  2020. f->ctz.size > lfs->cfg->cache_size) {
  2021. int err = lfs_file_outline(lfs, f);
  2022. if (err) {
  2023. return err;
  2024. }
  2025. err = lfs_file_flush(lfs, f);
  2026. if (err) {
  2027. return err;
  2028. }
  2029. }
  2030. }
  2031. lfs_block_t lpair[2] = {dir->pair[0], dir->pair[1]};
  2032. lfs_mdir_t ldir = *dir;
  2033. lfs_mdir_t pdir;
  2034. int state = lfs_dir_relocatingcommit(lfs, &ldir, dir->pair,
  2035. attrs, attrcount, &pdir);
  2036. if (state < 0) {
  2037. return state;
  2038. }
  2039. // update if we're not in mlist, note we may have already been
  2040. // updated if we are in mlist
  2041. if (lfs_pair_cmp(dir->pair, lpair) == 0) {
  2042. *dir = ldir;
  2043. }
  2044. // commit was successful, but may require other changes in the
  2045. // filesystem, these would normally be tail recursive, but we have
  2046. // flattened them here avoid unbounded stack usage
  2047. // need to drop?
  2048. if (state == LFS_OK_DROPPED) {
  2049. // steal state
  2050. int err = lfs_dir_getgstate(lfs, dir, &lfs->gdelta);
  2051. if (err) {
  2052. return err;
  2053. }
  2054. // steal tail, note that this can't create a recursive drop
  2055. lpair[0] = pdir.pair[0];
  2056. lpair[1] = pdir.pair[1];
  2057. lfs_pair_tole32(dir->tail);
  2058. state = lfs_dir_relocatingcommit(lfs, &pdir, lpair, LFS_MKATTRS(
  2059. {LFS_MKTAG(LFS_TYPE_TAIL + dir->split, 0x3ff, 8),
  2060. dir->tail}),
  2061. NULL);
  2062. lfs_pair_fromle32(dir->tail);
  2063. if (state < 0) {
  2064. return state;
  2065. }
  2066. ldir = pdir;
  2067. }
  2068. // need to relocate?
  2069. bool orphans = false;
  2070. while (state == LFS_OK_RELOCATED) {
  2071. LFS_DEBUG("Relocating {0x%"PRIx32", 0x%"PRIx32"} "
  2072. "-> {0x%"PRIx32", 0x%"PRIx32"}",
  2073. lpair[0], lpair[1], ldir.pair[0], ldir.pair[1]);
  2074. state = 0;
  2075. // update internal root
  2076. if (lfs_pair_cmp(lpair, lfs->root) == 0) {
  2077. lfs->root[0] = ldir.pair[0];
  2078. lfs->root[1] = ldir.pair[1];
  2079. }
  2080. // update internally tracked dirs
  2081. for (struct lfs_mlist *d = lfs->mlist; d; d = d->next) {
  2082. if (lfs_pair_cmp(lpair, d->m.pair) == 0) {
  2083. d->m.pair[0] = ldir.pair[0];
  2084. d->m.pair[1] = ldir.pair[1];
  2085. }
  2086. if (d->type == LFS_TYPE_DIR &&
  2087. lfs_pair_cmp(lpair, ((lfs_dir_t*)d)->head) == 0) {
  2088. ((lfs_dir_t*)d)->head[0] = ldir.pair[0];
  2089. ((lfs_dir_t*)d)->head[1] = ldir.pair[1];
  2090. }
  2091. }
  2092. // find parent
  2093. lfs_stag_t tag = lfs_fs_parent(lfs, lpair, &pdir);
  2094. if (tag < 0 && tag != LFS_ERR_NOENT) {
  2095. return tag;
  2096. }
  2097. bool hasparent = (tag != LFS_ERR_NOENT);
  2098. if (tag != LFS_ERR_NOENT) {
  2099. // note that if we have a parent, we must have a pred, so this will
  2100. // always create an orphan
  2101. int err = lfs_fs_preporphans(lfs, +1);
  2102. if (err) {
  2103. return err;
  2104. }
  2105. // fix pending move in this pair? this looks like an optimization but
  2106. // is in fact _required_ since relocating may outdate the move.
  2107. uint16_t moveid = 0x3ff;
  2108. if (lfs_gstate_hasmovehere(&lfs->gstate, pdir.pair)) {
  2109. moveid = lfs_tag_id(lfs->gstate.tag);
  2110. LFS_DEBUG("Fixing move while relocating "
  2111. "{0x%"PRIx32", 0x%"PRIx32"} 0x%"PRIx16"\n",
  2112. pdir.pair[0], pdir.pair[1], moveid);
  2113. lfs_fs_prepmove(lfs, 0x3ff, NULL);
  2114. if (moveid < lfs_tag_id(tag)) {
  2115. tag -= LFS_MKTAG(0, 1, 0);
  2116. }
  2117. }
  2118. lfs_block_t ppair[2] = {pdir.pair[0], pdir.pair[1]};
  2119. lfs_pair_tole32(ldir.pair);
  2120. state = lfs_dir_relocatingcommit(lfs, &pdir, ppair, LFS_MKATTRS(
  2121. {LFS_MKTAG_IF(moveid != 0x3ff,
  2122. LFS_TYPE_DELETE, moveid, 0), NULL},
  2123. {tag, ldir.pair}),
  2124. NULL);
  2125. lfs_pair_fromle32(ldir.pair);
  2126. if (state < 0) {
  2127. return state;
  2128. }
  2129. if (state == LFS_OK_RELOCATED) {
  2130. lpair[0] = ppair[0];
  2131. lpair[1] = ppair[1];
  2132. ldir = pdir;
  2133. orphans = true;
  2134. continue;
  2135. }
  2136. }
  2137. // find pred
  2138. int err = lfs_fs_pred(lfs, lpair, &pdir);
  2139. if (err && err != LFS_ERR_NOENT) {
  2140. return err;
  2141. }
  2142. LFS_ASSERT(!(hasparent && err == LFS_ERR_NOENT));
  2143. // if we can't find dir, it must be new
  2144. if (err != LFS_ERR_NOENT) {
  2145. if (lfs_gstate_hasorphans(&lfs->gstate)) {
  2146. // next step, clean up orphans
  2147. err = lfs_fs_preporphans(lfs, -hasparent);
  2148. if (err) {
  2149. return err;
  2150. }
  2151. }
  2152. // fix pending move in this pair? this looks like an optimization
  2153. // but is in fact _required_ since relocating may outdate the move.
  2154. uint16_t moveid = 0x3ff;
  2155. if (lfs_gstate_hasmovehere(&lfs->gstate, pdir.pair)) {
  2156. moveid = lfs_tag_id(lfs->gstate.tag);
  2157. LFS_DEBUG("Fixing move while relocating "
  2158. "{0x%"PRIx32", 0x%"PRIx32"} 0x%"PRIx16"\n",
  2159. pdir.pair[0], pdir.pair[1], moveid);
  2160. lfs_fs_prepmove(lfs, 0x3ff, NULL);
  2161. }
  2162. // replace bad pair, either we clean up desync, or no desync occured
  2163. lpair[0] = pdir.pair[0];
  2164. lpair[1] = pdir.pair[1];
  2165. lfs_pair_tole32(ldir.pair);
  2166. state = lfs_dir_relocatingcommit(lfs, &pdir, lpair, LFS_MKATTRS(
  2167. {LFS_MKTAG_IF(moveid != 0x3ff,
  2168. LFS_TYPE_DELETE, moveid, 0), NULL},
  2169. {LFS_MKTAG(LFS_TYPE_TAIL + pdir.split, 0x3ff, 8),
  2170. ldir.pair}),
  2171. NULL);
  2172. lfs_pair_fromle32(ldir.pair);
  2173. if (state < 0) {
  2174. return state;
  2175. }
  2176. ldir = pdir;
  2177. }
  2178. }
  2179. return orphans ? LFS_OK_ORPHANED : 0;
  2180. }
  2181. #endif
  2182. #ifndef LFS_READONLY
  2183. static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir,
  2184. const struct lfs_mattr *attrs, int attrcount) {
  2185. int orphans = lfs_dir_orphaningcommit(lfs, dir, attrs, attrcount);
  2186. if (orphans < 0) {
  2187. return orphans;
  2188. }
  2189. if (orphans) {
  2190. // make sure we've removed all orphans, this is a noop if there
  2191. // are none, but if we had nested blocks failures we may have
  2192. // created some
  2193. int err = lfs_fs_deorphan(lfs, false);
  2194. if (err) {
  2195. return err;
  2196. }
  2197. }
  2198. return 0;
  2199. }
  2200. #endif
  2201. /// Top level directory operations ///
  2202. #ifndef LFS_READONLY
  2203. static int lfs_rawmkdir(lfs_t *lfs, const char *path) {
  2204. // deorphan if we haven't yet, needed at most once after poweron
  2205. int err = lfs_fs_forceconsistency(lfs);
  2206. if (err) {
  2207. return err;
  2208. }
  2209. struct lfs_mlist cwd;
  2210. cwd.next = lfs->mlist;
  2211. uint16_t id;
  2212. err = lfs_dir_find(lfs, &cwd.m, &path, &id);
  2213. if (!(err == LFS_ERR_NOENT && id != 0x3ff)) {
  2214. return (err < 0) ? err : LFS_ERR_EXIST;
  2215. }
  2216. // check that name fits
  2217. lfs_size_t nlen = strlen(path);
  2218. if (nlen > lfs->name_max) {
  2219. return LFS_ERR_NAMETOOLONG;
  2220. }
  2221. // build up new directory
  2222. lfs_alloc_ack(lfs);
  2223. lfs_mdir_t dir;
  2224. err = lfs_dir_alloc(lfs, &dir);
  2225. if (err) {
  2226. return err;
  2227. }
  2228. // find end of list
  2229. lfs_mdir_t pred = cwd.m;
  2230. while (pred.split) {
  2231. err = lfs_dir_fetch(lfs, &pred, pred.tail);
  2232. if (err) {
  2233. return err;
  2234. }
  2235. }
  2236. // setup dir
  2237. lfs_pair_tole32(pred.tail);
  2238. err = lfs_dir_commit(lfs, &dir, LFS_MKATTRS(
  2239. {LFS_MKTAG(LFS_TYPE_SOFTTAIL, 0x3ff, 8), pred.tail}));
  2240. lfs_pair_fromle32(pred.tail);
  2241. if (err) {
  2242. return err;
  2243. }
  2244. // current block not end of list?
  2245. if (cwd.m.split) {
  2246. // update tails, this creates a desync
  2247. err = lfs_fs_preporphans(lfs, +1);
  2248. if (err) {
  2249. return err;
  2250. }
  2251. // it's possible our predecessor has to be relocated, and if
  2252. // our parent is our predecessor's predecessor, this could have
  2253. // caused our parent to go out of date, fortunately we can hook
  2254. // ourselves into littlefs to catch this
  2255. cwd.type = 0;
  2256. cwd.id = 0;
  2257. lfs->mlist = &cwd;
  2258. lfs_pair_tole32(dir.pair);
  2259. err = lfs_dir_commit(lfs, &pred, LFS_MKATTRS(
  2260. {LFS_MKTAG(LFS_TYPE_SOFTTAIL, 0x3ff, 8), dir.pair}));
  2261. lfs_pair_fromle32(dir.pair);
  2262. if (err) {
  2263. lfs->mlist = cwd.next;
  2264. return err;
  2265. }
  2266. lfs->mlist = cwd.next;
  2267. err = lfs_fs_preporphans(lfs, -1);
  2268. if (err) {
  2269. return err;
  2270. }
  2271. }
  2272. // now insert into our parent block
  2273. lfs_pair_tole32(dir.pair);
  2274. err = lfs_dir_commit(lfs, &cwd.m, LFS_MKATTRS(
  2275. {LFS_MKTAG(LFS_TYPE_CREATE, id, 0), NULL},
  2276. {LFS_MKTAG(LFS_TYPE_DIR, id, nlen), path},
  2277. {LFS_MKTAG(LFS_TYPE_DIRSTRUCT, id, 8), dir.pair},
  2278. {LFS_MKTAG_IF(!cwd.m.split,
  2279. LFS_TYPE_SOFTTAIL, 0x3ff, 8), dir.pair}));
  2280. lfs_pair_fromle32(dir.pair);
  2281. if (err) {
  2282. return err;
  2283. }
  2284. return 0;
  2285. }
  2286. #endif
  2287. static int lfs_dir_rawopen(lfs_t *lfs, lfs_dir_t *dir, const char *path) {
  2288. lfs_stag_t tag = lfs_dir_find(lfs, &dir->m, &path, NULL);
  2289. if (tag < 0) {
  2290. return tag;
  2291. }
  2292. if (lfs_tag_type3(tag) != LFS_TYPE_DIR) {
  2293. return LFS_ERR_NOTDIR;
  2294. }
  2295. lfs_block_t pair[2];
  2296. if (lfs_tag_id(tag) == 0x3ff) {
  2297. // handle root dir separately
  2298. pair[0] = lfs->root[0];
  2299. pair[1] = lfs->root[1];
  2300. } else {
  2301. // get dir pair from parent
  2302. lfs_stag_t res = lfs_dir_get(lfs, &dir->m, LFS_MKTAG(0x700, 0x3ff, 0),
  2303. LFS_MKTAG(LFS_TYPE_STRUCT, lfs_tag_id(tag), 8), pair);
  2304. if (res < 0) {
  2305. return res;
  2306. }
  2307. lfs_pair_fromle32(pair);
  2308. }
  2309. // fetch first pair
  2310. int err = lfs_dir_fetch(lfs, &dir->m, pair);
  2311. if (err) {
  2312. return err;
  2313. }
  2314. // setup entry
  2315. dir->head[0] = dir->m.pair[0];
  2316. dir->head[1] = dir->m.pair[1];
  2317. dir->id = 0;
  2318. dir->pos = 0;
  2319. // add to list of mdirs
  2320. dir->type = LFS_TYPE_DIR;
  2321. lfs_mlist_append(lfs, (struct lfs_mlist *)dir);
  2322. return 0;
  2323. }
  2324. static int lfs_dir_rawclose(lfs_t *lfs, lfs_dir_t *dir) {
  2325. // remove from list of mdirs
  2326. lfs_mlist_remove(lfs, (struct lfs_mlist *)dir);
  2327. return 0;
  2328. }
  2329. static int lfs_dir_rawread(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) {
  2330. memset(info, 0, sizeof(*info));
  2331. // special offset for '.' and '..'
  2332. if (dir->pos == 0) {
  2333. info->type = LFS_TYPE_DIR;
  2334. strcpy(info->name, ".");
  2335. dir->pos += 1;
  2336. return true;
  2337. } else if (dir->pos == 1) {
  2338. info->type = LFS_TYPE_DIR;
  2339. strcpy(info->name, "..");
  2340. dir->pos += 1;
  2341. return true;
  2342. }
  2343. while (true) {
  2344. if (dir->id == dir->m.count) {
  2345. if (!dir->m.split) {
  2346. return false;
  2347. }
  2348. int err = lfs_dir_fetch(lfs, &dir->m, dir->m.tail);
  2349. if (err) {
  2350. return err;
  2351. }
  2352. dir->id = 0;
  2353. }
  2354. int err = lfs_dir_getinfo(lfs, &dir->m, dir->id, info);
  2355. if (err && err != LFS_ERR_NOENT) {
  2356. return err;
  2357. }
  2358. dir->id += 1;
  2359. if (err != LFS_ERR_NOENT) {
  2360. break;
  2361. }
  2362. }
  2363. dir->pos += 1;
  2364. return true;
  2365. }
  2366. static int lfs_dir_rawseek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) {
  2367. // simply walk from head dir
  2368. int err = lfs_dir_rawrewind(lfs, dir);
  2369. if (err) {
  2370. return err;
  2371. }
  2372. // first two for ./..
  2373. dir->pos = lfs_min(2, off);
  2374. off -= dir->pos;
  2375. // skip superblock entry
  2376. dir->id = (off > 0 && lfs_pair_cmp(dir->head, lfs->root) == 0);
  2377. while (off > 0) {
  2378. int diff = lfs_min(dir->m.count - dir->id, off);
  2379. dir->id += diff;
  2380. dir->pos += diff;
  2381. off -= diff;
  2382. if (dir->id == dir->m.count) {
  2383. if (!dir->m.split) {
  2384. return LFS_ERR_INVAL;
  2385. }
  2386. err = lfs_dir_fetch(lfs, &dir->m, dir->m.tail);
  2387. if (err) {
  2388. return err;
  2389. }
  2390. dir->id = 0;
  2391. }
  2392. }
  2393. return 0;
  2394. }
  2395. static lfs_soff_t lfs_dir_rawtell(lfs_t *lfs, lfs_dir_t *dir) {
  2396. (void)lfs;
  2397. return dir->pos;
  2398. }
  2399. static int lfs_dir_rawrewind(lfs_t *lfs, lfs_dir_t *dir) {
  2400. // reload the head dir
  2401. int err = lfs_dir_fetch(lfs, &dir->m, dir->head);
  2402. if (err) {
  2403. return err;
  2404. }
  2405. dir->id = 0;
  2406. dir->pos = 0;
  2407. return 0;
  2408. }
  2409. /// File index list operations ///
  2410. static int lfs_ctz_index(lfs_t *lfs, lfs_off_t *off) {
  2411. lfs_off_t size = *off;
  2412. lfs_off_t b = lfs->cfg->block_size - 2*4;
  2413. lfs_off_t i = size / b;
  2414. if (i == 0) {
  2415. return 0;
  2416. }
  2417. i = (size - 4*(lfs_popc(i-1)+2)) / b;
  2418. *off = size - b*i - 4*lfs_popc(i);
  2419. return i;
  2420. }
  2421. static int lfs_ctz_find(lfs_t *lfs,
  2422. const lfs_cache_t *pcache, lfs_cache_t *rcache,
  2423. lfs_block_t head, lfs_size_t size,
  2424. lfs_size_t pos, lfs_block_t *block, lfs_off_t *off) {
  2425. if (size == 0) {
  2426. *block = LFS_BLOCK_NULL;
  2427. *off = 0;
  2428. return 0;
  2429. }
  2430. lfs_off_t current = lfs_ctz_index(lfs, &(lfs_off_t){size-1});
  2431. lfs_off_t target = lfs_ctz_index(lfs, &pos);
  2432. while (current > target) {
  2433. lfs_size_t skip = lfs_min(
  2434. lfs_npw2(current-target+1) - 1,
  2435. lfs_ctz(current));
  2436. int err = lfs_bd_read(lfs,
  2437. pcache, rcache, sizeof(head),
  2438. head, 4*skip, &head, sizeof(head));
  2439. head = lfs_fromle32(head);
  2440. if (err) {
  2441. return err;
  2442. }
  2443. current -= 1 << skip;
  2444. }
  2445. *block = head;
  2446. *off = pos;
  2447. return 0;
  2448. }
  2449. #ifndef LFS_READONLY
  2450. static int lfs_ctz_extend(lfs_t *lfs,
  2451. lfs_cache_t *pcache, lfs_cache_t *rcache,
  2452. lfs_block_t head, lfs_size_t size,
  2453. lfs_block_t *block, lfs_off_t *off) {
  2454. while (true) {
  2455. // go ahead and grab a block
  2456. lfs_block_t nblock;
  2457. int err = lfs_alloc(lfs, &nblock);
  2458. if (err) {
  2459. return err;
  2460. }
  2461. {
  2462. err = lfs_bd_erase(lfs, nblock);
  2463. if (err) {
  2464. if (err == LFS_ERR_CORRUPT) {
  2465. goto relocate;
  2466. }
  2467. return err;
  2468. }
  2469. if (size == 0) {
  2470. *block = nblock;
  2471. *off = 0;
  2472. return 0;
  2473. }
  2474. lfs_size_t noff = size - 1;
  2475. lfs_off_t index = lfs_ctz_index(lfs, &noff);
  2476. noff = noff + 1;
  2477. // just copy out the last block if it is incomplete
  2478. if (noff != lfs->cfg->block_size) {
  2479. for (lfs_off_t i = 0; i < noff; i++) {
  2480. uint8_t data;
  2481. err = lfs_bd_read(lfs,
  2482. NULL, rcache, noff-i,
  2483. head, i, &data, 1);
  2484. if (err) {
  2485. return err;
  2486. }
  2487. err = lfs_bd_prog(lfs,
  2488. pcache, rcache, true,
  2489. nblock, i, &data, 1);
  2490. if (err) {
  2491. if (err == LFS_ERR_CORRUPT) {
  2492. goto relocate;
  2493. }
  2494. return err;
  2495. }
  2496. }
  2497. *block = nblock;
  2498. *off = noff;
  2499. return 0;
  2500. }
  2501. // append block
  2502. index += 1;
  2503. lfs_size_t skips = lfs_ctz(index) + 1;
  2504. lfs_block_t nhead = head;
  2505. for (lfs_off_t i = 0; i < skips; i++) {
  2506. nhead = lfs_tole32(nhead);
  2507. err = lfs_bd_prog(lfs, pcache, rcache, true,
  2508. nblock, 4*i, &nhead, 4);
  2509. nhead = lfs_fromle32(nhead);
  2510. if (err) {
  2511. if (err == LFS_ERR_CORRUPT) {
  2512. goto relocate;
  2513. }
  2514. return err;
  2515. }
  2516. if (i != skips-1) {
  2517. err = lfs_bd_read(lfs,
  2518. NULL, rcache, sizeof(nhead),
  2519. nhead, 4*i, &nhead, sizeof(nhead));
  2520. nhead = lfs_fromle32(nhead);
  2521. if (err) {
  2522. return err;
  2523. }
  2524. }
  2525. }
  2526. *block = nblock;
  2527. *off = 4*skips;
  2528. return 0;
  2529. }
  2530. relocate:
  2531. LFS_DEBUG("Bad block at 0x%"PRIx32, nblock);
  2532. // just clear cache and try a new block
  2533. lfs_cache_drop(lfs, pcache);
  2534. }
  2535. }
  2536. #endif
  2537. static int lfs_ctz_traverse(lfs_t *lfs,
  2538. const lfs_cache_t *pcache, lfs_cache_t *rcache,
  2539. lfs_block_t head, lfs_size_t size,
  2540. int (*cb)(void*, lfs_block_t), void *data) {
  2541. if (size == 0) {
  2542. return 0;
  2543. }
  2544. lfs_off_t index = lfs_ctz_index(lfs, &(lfs_off_t){size-1});
  2545. while (true) {
  2546. int err = cb(data, head);
  2547. if (err) {
  2548. return err;
  2549. }
  2550. if (index == 0) {
  2551. return 0;
  2552. }
  2553. lfs_block_t heads[2];
  2554. int count = 2 - (index & 1);
  2555. err = lfs_bd_read(lfs,
  2556. pcache, rcache, count*sizeof(head),
  2557. head, 0, &heads, count*sizeof(head));
  2558. heads[0] = lfs_fromle32(heads[0]);
  2559. heads[1] = lfs_fromle32(heads[1]);
  2560. if (err) {
  2561. return err;
  2562. }
  2563. for (int i = 0; i < count-1; i++) {
  2564. err = cb(data, heads[i]);
  2565. if (err) {
  2566. return err;
  2567. }
  2568. }
  2569. head = heads[count-1];
  2570. index -= count;
  2571. }
  2572. }
  2573. /// Top level file operations ///
  2574. static int lfs_file_rawopencfg(lfs_t *lfs, lfs_file_t *file,
  2575. const char *path, int flags,
  2576. const struct lfs_file_config *cfg) {
  2577. #ifndef LFS_READONLY
  2578. // deorphan if we haven't yet, needed at most once after poweron
  2579. if ((flags & LFS_O_WRONLY) == LFS_O_WRONLY) {
  2580. int err = lfs_fs_forceconsistency(lfs);
  2581. if (err) {
  2582. return err;
  2583. }
  2584. }
  2585. #else
  2586. LFS_ASSERT((flags & LFS_O_RDONLY) == LFS_O_RDONLY);
  2587. #endif
  2588. // setup simple file details
  2589. int err;
  2590. file->cfg = cfg;
  2591. file->flags = flags;
  2592. file->pos = 0;
  2593. file->off = 0;
  2594. file->cache.buffer = NULL;
  2595. // allocate entry for file if it doesn't exist
  2596. lfs_stag_t tag = lfs_dir_find(lfs, &file->m, &path, &file->id);
  2597. if (tag < 0 && !(tag == LFS_ERR_NOENT && file->id != 0x3ff)) {
  2598. err = tag;
  2599. goto cleanup;
  2600. }
  2601. // get id, add to list of mdirs to catch update changes
  2602. file->type = LFS_TYPE_REG;
  2603. lfs_mlist_append(lfs, (struct lfs_mlist *)file);
  2604. #ifdef LFS_READONLY
  2605. if (tag == LFS_ERR_NOENT) {
  2606. err = LFS_ERR_NOENT;
  2607. goto cleanup;
  2608. #else
  2609. if (tag == LFS_ERR_NOENT) {
  2610. if (!(flags & LFS_O_CREAT)) {
  2611. err = LFS_ERR_NOENT;
  2612. goto cleanup;
  2613. }
  2614. // check that name fits
  2615. lfs_size_t nlen = strlen(path);
  2616. if (nlen > lfs->name_max) {
  2617. err = LFS_ERR_NAMETOOLONG;
  2618. goto cleanup;
  2619. }
  2620. // get next slot and create entry to remember name
  2621. err = lfs_dir_commit(lfs, &file->m, LFS_MKATTRS(
  2622. {LFS_MKTAG(LFS_TYPE_CREATE, file->id, 0), NULL},
  2623. {LFS_MKTAG(LFS_TYPE_REG, file->id, nlen), path},
  2624. {LFS_MKTAG(LFS_TYPE_INLINESTRUCT, file->id, 0), NULL}));
  2625. // it may happen that the file name doesn't fit in the metadata blocks, e.g., a 256 byte file name will
  2626. // not fit in a 128 byte block.
  2627. err = (err == LFS_ERR_NOSPC) ? LFS_ERR_NAMETOOLONG : err;
  2628. if (err) {
  2629. goto cleanup;
  2630. }
  2631. tag = LFS_MKTAG(LFS_TYPE_INLINESTRUCT, 0, 0);
  2632. } else if (flags & LFS_O_EXCL) {
  2633. err = LFS_ERR_EXIST;
  2634. goto cleanup;
  2635. #endif
  2636. } else if (lfs_tag_type3(tag) != LFS_TYPE_REG) {
  2637. err = LFS_ERR_ISDIR;
  2638. goto cleanup;
  2639. #ifndef LFS_READONLY
  2640. } else if (flags & LFS_O_TRUNC) {
  2641. // truncate if requested
  2642. tag = LFS_MKTAG(LFS_TYPE_INLINESTRUCT, file->id, 0);
  2643. file->flags |= LFS_F_DIRTY;
  2644. #endif
  2645. } else {
  2646. // try to load what's on disk, if it's inlined we'll fix it later
  2647. tag = lfs_dir_get(lfs, &file->m, LFS_MKTAG(0x700, 0x3ff, 0),
  2648. LFS_MKTAG(LFS_TYPE_STRUCT, file->id, 8), &file->ctz);
  2649. if (tag < 0) {
  2650. err = tag;
  2651. goto cleanup;
  2652. }
  2653. lfs_ctz_fromle32(&file->ctz);
  2654. }
  2655. // fetch attrs
  2656. for (unsigned i = 0; i < file->cfg->attr_count; i++) {
  2657. // if opened for read / read-write operations
  2658. if ((file->flags & LFS_O_RDONLY) == LFS_O_RDONLY) {
  2659. lfs_stag_t res = lfs_dir_get(lfs, &file->m,
  2660. LFS_MKTAG(0x7ff, 0x3ff, 0),
  2661. LFS_MKTAG(LFS_TYPE_USERATTR + file->cfg->attrs[i].type,
  2662. file->id, file->cfg->attrs[i].size),
  2663. file->cfg->attrs[i].buffer);
  2664. if (res < 0 && res != LFS_ERR_NOENT) {
  2665. err = res;
  2666. goto cleanup;
  2667. }
  2668. }
  2669. #ifndef LFS_READONLY
  2670. // if opened for write / read-write operations
  2671. if ((file->flags & LFS_O_WRONLY) == LFS_O_WRONLY) {
  2672. if (file->cfg->attrs[i].size > lfs->attr_max) {
  2673. err = LFS_ERR_NOSPC;
  2674. goto cleanup;
  2675. }
  2676. file->flags |= LFS_F_DIRTY;
  2677. }
  2678. #endif
  2679. }
  2680. // allocate buffer if needed
  2681. if (file->cfg->buffer) {
  2682. file->cache.buffer = file->cfg->buffer;
  2683. } else {
  2684. file->cache.buffer = lfs_malloc(lfs->cfg->cache_size);
  2685. if (!file->cache.buffer) {
  2686. err = LFS_ERR_NOMEM;
  2687. goto cleanup;
  2688. }
  2689. }
  2690. // zero to avoid information leak
  2691. lfs_cache_zero(lfs, &file->cache);
  2692. if (lfs_tag_type3(tag) == LFS_TYPE_INLINESTRUCT) {
  2693. // load inline files
  2694. file->ctz.head = LFS_BLOCK_INLINE;
  2695. file->ctz.size = lfs_tag_size(tag);
  2696. file->flags |= LFS_F_INLINE;
  2697. file->cache.block = file->ctz.head;
  2698. file->cache.off = 0;
  2699. file->cache.size = lfs->cfg->cache_size;
  2700. // don't always read (may be new/trunc file)
  2701. if (file->ctz.size > 0) {
  2702. lfs_stag_t res = lfs_dir_get(lfs, &file->m,
  2703. LFS_MKTAG(0x700, 0x3ff, 0),
  2704. LFS_MKTAG(LFS_TYPE_STRUCT, file->id,
  2705. lfs_min(file->cache.size, 0x3fe)),
  2706. file->cache.buffer);
  2707. if (res < 0) {
  2708. err = res;
  2709. goto cleanup;
  2710. }
  2711. }
  2712. }
  2713. return 0;
  2714. cleanup:
  2715. // clean up lingering resources
  2716. #ifndef LFS_READONLY
  2717. file->flags |= LFS_F_ERRED;
  2718. #endif
  2719. lfs_file_rawclose(lfs, file);
  2720. return err;
  2721. }
  2722. #ifndef LFS_NO_MALLOC
  2723. static int lfs_file_rawopen(lfs_t *lfs, lfs_file_t *file,
  2724. const char *path, int flags) {
  2725. static const struct lfs_file_config defaults = {0};
  2726. int err = lfs_file_rawopencfg(lfs, file, path, flags, &defaults);
  2727. return err;
  2728. }
  2729. #endif
  2730. static int lfs_file_rawclose(lfs_t *lfs, lfs_file_t *file) {
  2731. #ifndef LFS_READONLY
  2732. int err = lfs_file_rawsync(lfs, file);
  2733. #else
  2734. int err = 0;
  2735. #endif
  2736. // remove from list of mdirs
  2737. lfs_mlist_remove(lfs, (struct lfs_mlist*)file);
  2738. // clean up memory
  2739. if (!file->cfg->buffer) {
  2740. lfs_free(file->cache.buffer);
  2741. }
  2742. return err;
  2743. }
  2744. #ifndef LFS_READONLY
  2745. static int lfs_file_relocate(lfs_t *lfs, lfs_file_t *file) {
  2746. while (true) {
  2747. // just relocate what exists into new block
  2748. lfs_block_t nblock;
  2749. int err = lfs_alloc(lfs, &nblock);
  2750. if (err) {
  2751. return err;
  2752. }
  2753. err = lfs_bd_erase(lfs, nblock);
  2754. if (err) {
  2755. if (err == LFS_ERR_CORRUPT) {
  2756. goto relocate;
  2757. }
  2758. return err;
  2759. }
  2760. // either read from dirty cache or disk
  2761. for (lfs_off_t i = 0; i < file->off; i++) {
  2762. uint8_t data;
  2763. if (file->flags & LFS_F_INLINE) {
  2764. err = lfs_dir_getread(lfs, &file->m,
  2765. // note we evict inline files before they can be dirty
  2766. NULL, &file->cache, file->off-i,
  2767. LFS_MKTAG(0xfff, 0x1ff, 0),
  2768. LFS_MKTAG(LFS_TYPE_INLINESTRUCT, file->id, 0),
  2769. i, &data, 1);
  2770. if (err) {
  2771. return err;
  2772. }
  2773. } else {
  2774. err = lfs_bd_read(lfs,
  2775. &file->cache, &lfs->rcache, file->off-i,
  2776. file->block, i, &data, 1);
  2777. if (err) {
  2778. return err;
  2779. }
  2780. }
  2781. err = lfs_bd_prog(lfs,
  2782. &lfs->pcache, &lfs->rcache, true,
  2783. nblock, i, &data, 1);
  2784. if (err) {
  2785. if (err == LFS_ERR_CORRUPT) {
  2786. goto relocate;
  2787. }
  2788. return err;
  2789. }
  2790. }
  2791. // copy over new state of file
  2792. memcpy(file->cache.buffer, lfs->pcache.buffer, lfs->cfg->cache_size);
  2793. file->cache.block = lfs->pcache.block;
  2794. file->cache.off = lfs->pcache.off;
  2795. file->cache.size = lfs->pcache.size;
  2796. lfs_cache_zero(lfs, &lfs->pcache);
  2797. file->block = nblock;
  2798. file->flags |= LFS_F_WRITING;
  2799. return 0;
  2800. relocate:
  2801. LFS_DEBUG("Bad block at 0x%"PRIx32, nblock);
  2802. // just clear cache and try a new block
  2803. lfs_cache_drop(lfs, &lfs->pcache);
  2804. }
  2805. }
  2806. #endif
  2807. #ifndef LFS_READONLY
  2808. static int lfs_file_outline(lfs_t *lfs, lfs_file_t *file) {
  2809. file->off = file->pos;
  2810. lfs_alloc_ack(lfs);
  2811. int err = lfs_file_relocate(lfs, file);
  2812. if (err) {
  2813. return err;
  2814. }
  2815. file->flags &= ~LFS_F_INLINE;
  2816. return 0;
  2817. }
  2818. #endif
  2819. static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) {
  2820. if (file->flags & LFS_F_READING) {
  2821. if (!(file->flags & LFS_F_INLINE)) {
  2822. lfs_cache_drop(lfs, &file->cache);
  2823. }
  2824. file->flags &= ~LFS_F_READING;
  2825. }
  2826. #ifndef LFS_READONLY
  2827. if (file->flags & LFS_F_WRITING) {
  2828. lfs_off_t pos = file->pos;
  2829. if (!(file->flags & LFS_F_INLINE)) {
  2830. // copy over anything after current branch
  2831. lfs_file_t orig = {
  2832. .ctz.head = file->ctz.head,
  2833. .ctz.size = file->ctz.size,
  2834. .flags = LFS_O_RDONLY,
  2835. .pos = file->pos,
  2836. .cache = lfs->rcache,
  2837. };
  2838. lfs_cache_drop(lfs, &lfs->rcache);
  2839. while (file->pos < file->ctz.size) {
  2840. // copy over a byte at a time, leave it up to caching
  2841. // to make this efficient
  2842. uint8_t data;
  2843. lfs_ssize_t res = lfs_file_flushedread(lfs, &orig, &data, 1);
  2844. if (res < 0) {
  2845. return res;
  2846. }
  2847. res = lfs_file_flushedwrite(lfs, file, &data, 1);
  2848. if (res < 0) {
  2849. return res;
  2850. }
  2851. // keep our reference to the rcache in sync
  2852. if (lfs->rcache.block != LFS_BLOCK_NULL) {
  2853. lfs_cache_drop(lfs, &orig.cache);
  2854. lfs_cache_drop(lfs, &lfs->rcache);
  2855. }
  2856. }
  2857. // write out what we have
  2858. while (true) {
  2859. int err = lfs_bd_flush(lfs, &file->cache, &lfs->rcache, true);
  2860. if (err) {
  2861. if (err == LFS_ERR_CORRUPT) {
  2862. goto relocate;
  2863. }
  2864. return err;
  2865. }
  2866. break;
  2867. relocate:
  2868. LFS_DEBUG("Bad block at 0x%"PRIx32, file->block);
  2869. err = lfs_file_relocate(lfs, file);
  2870. if (err) {
  2871. return err;
  2872. }
  2873. }
  2874. } else {
  2875. file->pos = lfs_max(file->pos, file->ctz.size);
  2876. }
  2877. // actual file updates
  2878. file->ctz.head = file->block;
  2879. file->ctz.size = file->pos;
  2880. file->flags &= ~LFS_F_WRITING;
  2881. file->flags |= LFS_F_DIRTY;
  2882. file->pos = pos;
  2883. }
  2884. #endif
  2885. return 0;
  2886. }
  2887. #ifndef LFS_READONLY
  2888. static int lfs_file_rawsync(lfs_t *lfs, lfs_file_t *file) {
  2889. if (file->flags & LFS_F_ERRED) {
  2890. // it's not safe to do anything if our file errored
  2891. return 0;
  2892. }
  2893. int err = lfs_file_flush(lfs, file);
  2894. if (err) {
  2895. file->flags |= LFS_F_ERRED;
  2896. return err;
  2897. }
  2898. if ((file->flags & LFS_F_DIRTY) &&
  2899. !lfs_pair_isnull(file->m.pair)) {
  2900. // update dir entry
  2901. uint16_t type;
  2902. const void *buffer;
  2903. lfs_size_t size;
  2904. struct lfs_ctz ctz;
  2905. if (file->flags & LFS_F_INLINE) {
  2906. // inline the whole file
  2907. type = LFS_TYPE_INLINESTRUCT;
  2908. buffer = file->cache.buffer;
  2909. size = file->ctz.size;
  2910. } else {
  2911. // update the ctz reference
  2912. type = LFS_TYPE_CTZSTRUCT;
  2913. // copy ctz so alloc will work during a relocate
  2914. ctz = file->ctz;
  2915. lfs_ctz_tole32(&ctz);
  2916. buffer = &ctz;
  2917. size = sizeof(ctz);
  2918. }
  2919. // commit file data and attributes
  2920. err = lfs_dir_commit(lfs, &file->m, LFS_MKATTRS(
  2921. {LFS_MKTAG(type, file->id, size), buffer},
  2922. {LFS_MKTAG(LFS_FROM_USERATTRS, file->id,
  2923. file->cfg->attr_count), file->cfg->attrs}));
  2924. if (err) {
  2925. file->flags |= LFS_F_ERRED;
  2926. return err;
  2927. }
  2928. file->flags &= ~LFS_F_DIRTY;
  2929. }
  2930. return 0;
  2931. }
  2932. #endif
  2933. static lfs_ssize_t lfs_file_flushedread(lfs_t *lfs, lfs_file_t *file,
  2934. void *buffer, lfs_size_t size) {
  2935. uint8_t *data = buffer;
  2936. lfs_size_t nsize = size;
  2937. if (file->pos >= file->ctz.size) {
  2938. // eof if past end
  2939. return 0;
  2940. }
  2941. size = lfs_min(size, file->ctz.size - file->pos);
  2942. nsize = size;
  2943. while (nsize > 0) {
  2944. // check if we need a new block
  2945. if (!(file->flags & LFS_F_READING) ||
  2946. file->off == lfs->cfg->block_size) {
  2947. if (!(file->flags & LFS_F_INLINE)) {
  2948. int err = lfs_ctz_find(lfs, NULL, &file->cache,
  2949. file->ctz.head, file->ctz.size,
  2950. file->pos, &file->block, &file->off);
  2951. if (err) {
  2952. return err;
  2953. }
  2954. } else {
  2955. file->block = LFS_BLOCK_INLINE;
  2956. file->off = file->pos;
  2957. }
  2958. file->flags |= LFS_F_READING;
  2959. }
  2960. // read as much as we can in current block
  2961. lfs_size_t diff = lfs_min(nsize, lfs->cfg->block_size - file->off);
  2962. if (file->flags & LFS_F_INLINE) {
  2963. int err = lfs_dir_getread(lfs, &file->m,
  2964. NULL, &file->cache, lfs->cfg->block_size,
  2965. LFS_MKTAG(0xfff, 0x1ff, 0),
  2966. LFS_MKTAG(LFS_TYPE_INLINESTRUCT, file->id, 0),
  2967. file->off, data, diff);
  2968. if (err) {
  2969. return err;
  2970. }
  2971. } else {
  2972. int err = lfs_bd_read(lfs,
  2973. NULL, &file->cache, lfs->cfg->block_size,
  2974. file->block, file->off, data, diff);
  2975. if (err) {
  2976. return err;
  2977. }
  2978. }
  2979. file->pos += diff;
  2980. file->off += diff;
  2981. data += diff;
  2982. nsize -= diff;
  2983. }
  2984. return size;
  2985. }
  2986. static lfs_ssize_t lfs_file_rawread(lfs_t *lfs, lfs_file_t *file,
  2987. void *buffer, lfs_size_t size) {
  2988. LFS_ASSERT((file->flags & LFS_O_RDONLY) == LFS_O_RDONLY);
  2989. #ifndef LFS_READONLY
  2990. if (file->flags & LFS_F_WRITING) {
  2991. // flush out any writes
  2992. int err = lfs_file_flush(lfs, file);
  2993. if (err) {
  2994. return err;
  2995. }
  2996. }
  2997. #endif
  2998. return lfs_file_flushedread(lfs, file, buffer, size);
  2999. }
  3000. #ifndef LFS_READONLY
  3001. static lfs_ssize_t lfs_file_flushedwrite(lfs_t *lfs, lfs_file_t *file,
  3002. const void *buffer, lfs_size_t size) {
  3003. const uint8_t *data = buffer;
  3004. lfs_size_t nsize = size;
  3005. if ((file->flags & LFS_F_INLINE) &&
  3006. lfs_max(file->pos+nsize, file->ctz.size) >
  3007. lfs_min(0x3fe, lfs_min(
  3008. lfs->cfg->cache_size,
  3009. (lfs->cfg->metadata_max ?
  3010. lfs->cfg->metadata_max : lfs->cfg->block_size) / 8))) {
  3011. // inline file doesn't fit anymore
  3012. int err = lfs_file_outline(lfs, file);
  3013. if (err) {
  3014. file->flags |= LFS_F_ERRED;
  3015. return err;
  3016. }
  3017. }
  3018. while (nsize > 0) {
  3019. // check if we need a new block
  3020. if (!(file->flags & LFS_F_WRITING) ||
  3021. file->off == lfs->cfg->block_size) {
  3022. if (!(file->flags & LFS_F_INLINE)) {
  3023. if (!(file->flags & LFS_F_WRITING) && file->pos > 0) {
  3024. // find out which block we're extending from
  3025. int err = lfs_ctz_find(lfs, NULL, &file->cache,
  3026. file->ctz.head, file->ctz.size,
  3027. file->pos-1, &file->block, &file->off);
  3028. if (err) {
  3029. file->flags |= LFS_F_ERRED;
  3030. return err;
  3031. }
  3032. // mark cache as dirty since we may have read data into it
  3033. lfs_cache_zero(lfs, &file->cache);
  3034. }
  3035. // extend file with new blocks
  3036. lfs_alloc_ack(lfs);
  3037. int err = lfs_ctz_extend(lfs, &file->cache, &lfs->rcache,
  3038. file->block, file->pos,
  3039. &file->block, &file->off);
  3040. if (err) {
  3041. file->flags |= LFS_F_ERRED;
  3042. return err;
  3043. }
  3044. } else {
  3045. file->block = LFS_BLOCK_INLINE;
  3046. file->off = file->pos;
  3047. }
  3048. file->flags |= LFS_F_WRITING;
  3049. }
  3050. // program as much as we can in current block
  3051. lfs_size_t diff = lfs_min(nsize, lfs->cfg->block_size - file->off);
  3052. while (true) {
  3053. int err = lfs_bd_prog(lfs, &file->cache, &lfs->rcache, true,
  3054. file->block, file->off, data, diff);
  3055. if (err) {
  3056. if (err == LFS_ERR_CORRUPT) {
  3057. goto relocate;
  3058. }
  3059. file->flags |= LFS_F_ERRED;
  3060. return err;
  3061. }
  3062. break;
  3063. relocate:
  3064. err = lfs_file_relocate(lfs, file);
  3065. if (err) {
  3066. file->flags |= LFS_F_ERRED;
  3067. return err;
  3068. }
  3069. }
  3070. file->pos += diff;
  3071. file->off += diff;
  3072. data += diff;
  3073. nsize -= diff;
  3074. lfs_alloc_ack(lfs);
  3075. }
  3076. return size;
  3077. }
  3078. static lfs_ssize_t lfs_file_rawwrite(lfs_t *lfs, lfs_file_t *file,
  3079. const void *buffer, lfs_size_t size) {
  3080. LFS_ASSERT((file->flags & LFS_O_WRONLY) == LFS_O_WRONLY);
  3081. if (file->flags & LFS_F_READING) {
  3082. // drop any reads
  3083. int err = lfs_file_flush(lfs, file);
  3084. if (err) {
  3085. return err;
  3086. }
  3087. }
  3088. if ((file->flags & LFS_O_APPEND) && file->pos < file->ctz.size) {
  3089. file->pos = file->ctz.size;
  3090. }
  3091. if (file->pos + size > lfs->file_max) {
  3092. // Larger than file limit?
  3093. return LFS_ERR_FBIG;
  3094. }
  3095. if (!(file->flags & LFS_F_WRITING) && file->pos > file->ctz.size) {
  3096. // fill with zeros
  3097. lfs_off_t pos = file->pos;
  3098. file->pos = file->ctz.size;
  3099. while (file->pos < pos) {
  3100. lfs_ssize_t res = lfs_file_flushedwrite(lfs, file, &(uint8_t){0}, 1);
  3101. if (res < 0) {
  3102. return res;
  3103. }
  3104. }
  3105. }
  3106. lfs_ssize_t nsize = lfs_file_flushedwrite(lfs, file, buffer, size);
  3107. if (nsize < 0) {
  3108. return nsize;
  3109. }
  3110. file->flags &= ~LFS_F_ERRED;
  3111. return nsize;
  3112. }
  3113. #endif
  3114. static lfs_soff_t lfs_file_rawseek(lfs_t *lfs, lfs_file_t *file,
  3115. lfs_soff_t off, int whence) {
  3116. // find new pos
  3117. lfs_off_t npos = file->pos;
  3118. if (whence == LFS_SEEK_SET) {
  3119. npos = off;
  3120. } else if (whence == LFS_SEEK_CUR) {
  3121. if ((lfs_soff_t)file->pos + off < 0) {
  3122. return LFS_ERR_INVAL;
  3123. } else {
  3124. npos = file->pos + off;
  3125. }
  3126. } else if (whence == LFS_SEEK_END) {
  3127. lfs_soff_t res = lfs_file_rawsize(lfs, file) + off;
  3128. if (res < 0) {
  3129. return LFS_ERR_INVAL;
  3130. } else {
  3131. npos = res;
  3132. }
  3133. }
  3134. if (npos > lfs->file_max) {
  3135. // file position out of range
  3136. return LFS_ERR_INVAL;
  3137. }
  3138. if (file->pos == npos) {
  3139. // noop - position has not changed
  3140. return npos;
  3141. }
  3142. // if we're only reading and our new offset is still in the file's cache
  3143. // we can avoid flushing and needing to reread the data
  3144. if (
  3145. #ifndef LFS_READONLY
  3146. !(file->flags & LFS_F_WRITING)
  3147. #else
  3148. true
  3149. #endif
  3150. ) {
  3151. int oindex = lfs_ctz_index(lfs, &(lfs_off_t){file->pos});
  3152. lfs_off_t noff = npos;
  3153. int nindex = lfs_ctz_index(lfs, &noff);
  3154. if (oindex == nindex
  3155. && noff >= file->cache.off
  3156. && noff < file->cache.off + file->cache.size) {
  3157. file->pos = npos;
  3158. file->off = noff;
  3159. return npos;
  3160. }
  3161. }
  3162. // write out everything beforehand, may be noop if rdonly
  3163. int err = lfs_file_flush(lfs, file);
  3164. if (err) {
  3165. return err;
  3166. }
  3167. // update pos
  3168. file->pos = npos;
  3169. return npos;
  3170. }
  3171. #ifndef LFS_READONLY
  3172. static int lfs_file_rawtruncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) {
  3173. LFS_ASSERT((file->flags & LFS_O_WRONLY) == LFS_O_WRONLY);
  3174. if (size > LFS_FILE_MAX) {
  3175. return LFS_ERR_INVAL;
  3176. }
  3177. lfs_off_t pos = file->pos;
  3178. lfs_off_t oldsize = lfs_file_rawsize(lfs, file);
  3179. if (size < oldsize) {
  3180. // need to flush since directly changing metadata
  3181. int err = lfs_file_flush(lfs, file);
  3182. if (err) {
  3183. return err;
  3184. }
  3185. // lookup new head in ctz skip list
  3186. err = lfs_ctz_find(lfs, NULL, &file->cache,
  3187. file->ctz.head, file->ctz.size,
  3188. size, &file->block, &file->off);
  3189. if (err) {
  3190. return err;
  3191. }
  3192. // need to set pos/block/off consistently so seeking back to
  3193. // the old position does not get confused
  3194. file->pos = size;
  3195. file->ctz.head = file->block;
  3196. file->ctz.size = size;
  3197. file->flags |= LFS_F_DIRTY | LFS_F_READING;
  3198. } else if (size > oldsize) {
  3199. // flush+seek if not already at end
  3200. lfs_soff_t res = lfs_file_rawseek(lfs, file, 0, LFS_SEEK_END);
  3201. if (res < 0) {
  3202. return (int)res;
  3203. }
  3204. // fill with zeros
  3205. while (file->pos < size) {
  3206. res = lfs_file_rawwrite(lfs, file, &(uint8_t){0}, 1);
  3207. if (res < 0) {
  3208. return (int)res;
  3209. }
  3210. }
  3211. }
  3212. // restore pos
  3213. lfs_soff_t res = lfs_file_rawseek(lfs, file, pos, LFS_SEEK_SET);
  3214. if (res < 0) {
  3215. return (int)res;
  3216. }
  3217. return 0;
  3218. }
  3219. #endif
  3220. static lfs_soff_t lfs_file_rawtell(lfs_t *lfs, lfs_file_t *file) {
  3221. (void)lfs;
  3222. return file->pos;
  3223. }
  3224. static int lfs_file_rawrewind(lfs_t *lfs, lfs_file_t *file) {
  3225. lfs_soff_t res = lfs_file_rawseek(lfs, file, 0, LFS_SEEK_SET);
  3226. if (res < 0) {
  3227. return (int)res;
  3228. }
  3229. return 0;
  3230. }
  3231. static lfs_soff_t lfs_file_rawsize(lfs_t *lfs, lfs_file_t *file) {
  3232. (void)lfs;
  3233. #ifndef LFS_READONLY
  3234. if (file->flags & LFS_F_WRITING) {
  3235. return lfs_max(file->pos, file->ctz.size);
  3236. }
  3237. #endif
  3238. return file->ctz.size;
  3239. }
  3240. /// General fs operations ///
  3241. static int lfs_rawstat(lfs_t *lfs, const char *path, struct lfs_info *info) {
  3242. lfs_mdir_t cwd;
  3243. lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL);
  3244. if (tag < 0) {
  3245. return (int)tag;
  3246. }
  3247. return lfs_dir_getinfo(lfs, &cwd, lfs_tag_id(tag), info);
  3248. }
  3249. #ifndef LFS_READONLY
  3250. static int lfs_rawremove(lfs_t *lfs, const char *path) {
  3251. // deorphan if we haven't yet, needed at most once after poweron
  3252. int err = lfs_fs_forceconsistency(lfs);
  3253. if (err) {
  3254. return err;
  3255. }
  3256. lfs_mdir_t cwd;
  3257. lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL);
  3258. if (tag < 0 || lfs_tag_id(tag) == 0x3ff) {
  3259. return (tag < 0) ? (int)tag : LFS_ERR_INVAL;
  3260. }
  3261. struct lfs_mlist dir;
  3262. dir.next = lfs->mlist;
  3263. if (lfs_tag_type3(tag) == LFS_TYPE_DIR) {
  3264. // must be empty before removal
  3265. lfs_block_t pair[2];
  3266. lfs_stag_t res = lfs_dir_get(lfs, &cwd, LFS_MKTAG(0x700, 0x3ff, 0),
  3267. LFS_MKTAG(LFS_TYPE_STRUCT, lfs_tag_id(tag), 8), pair);
  3268. if (res < 0) {
  3269. return (int)res;
  3270. }
  3271. lfs_pair_fromle32(pair);
  3272. err = lfs_dir_fetch(lfs, &dir.m, pair);
  3273. if (err) {
  3274. return err;
  3275. }
  3276. if (dir.m.count > 0 || dir.m.split) {
  3277. return LFS_ERR_NOTEMPTY;
  3278. }
  3279. // mark fs as orphaned
  3280. err = lfs_fs_preporphans(lfs, +1);
  3281. if (err) {
  3282. return err;
  3283. }
  3284. // I know it's crazy but yes, dir can be changed by our parent's
  3285. // commit (if predecessor is child)
  3286. dir.type = 0;
  3287. dir.id = 0;
  3288. lfs->mlist = &dir;
  3289. }
  3290. // delete the entry
  3291. err = lfs_dir_commit(lfs, &cwd, LFS_MKATTRS(
  3292. {LFS_MKTAG(LFS_TYPE_DELETE, lfs_tag_id(tag), 0), NULL}));
  3293. if (err) {
  3294. lfs->mlist = dir.next;
  3295. return err;
  3296. }
  3297. lfs->mlist = dir.next;
  3298. if (lfs_tag_type3(tag) == LFS_TYPE_DIR) {
  3299. // fix orphan
  3300. err = lfs_fs_preporphans(lfs, -1);
  3301. if (err) {
  3302. return err;
  3303. }
  3304. err = lfs_fs_pred(lfs, dir.m.pair, &cwd);
  3305. if (err) {
  3306. return err;
  3307. }
  3308. err = lfs_dir_drop(lfs, &cwd, &dir.m);
  3309. if (err) {
  3310. return err;
  3311. }
  3312. }
  3313. return 0;
  3314. }
  3315. #endif
  3316. #ifndef LFS_READONLY
  3317. static int lfs_rawrename(lfs_t *lfs, const char *oldpath, const char *newpath) {
  3318. // deorphan if we haven't yet, needed at most once after poweron
  3319. int err = lfs_fs_forceconsistency(lfs);
  3320. if (err) {
  3321. return err;
  3322. }
  3323. // find old entry
  3324. lfs_mdir_t oldcwd;
  3325. lfs_stag_t oldtag = lfs_dir_find(lfs, &oldcwd, &oldpath, NULL);
  3326. if (oldtag < 0 || lfs_tag_id(oldtag) == 0x3ff) {
  3327. return (oldtag < 0) ? (int)oldtag : LFS_ERR_INVAL;
  3328. }
  3329. // find new entry
  3330. lfs_mdir_t newcwd;
  3331. uint16_t newid;
  3332. lfs_stag_t prevtag = lfs_dir_find(lfs, &newcwd, &newpath, &newid);
  3333. if ((prevtag < 0 || lfs_tag_id(prevtag) == 0x3ff) &&
  3334. !(prevtag == LFS_ERR_NOENT && newid != 0x3ff)) {
  3335. return (prevtag < 0) ? (int)prevtag : LFS_ERR_INVAL;
  3336. }
  3337. // if we're in the same pair there's a few special cases...
  3338. bool samepair = (lfs_pair_cmp(oldcwd.pair, newcwd.pair) == 0);
  3339. uint16_t newoldid = lfs_tag_id(oldtag);
  3340. struct lfs_mlist prevdir;
  3341. prevdir.next = lfs->mlist;
  3342. if (prevtag == LFS_ERR_NOENT) {
  3343. // check that name fits
  3344. lfs_size_t nlen = strlen(newpath);
  3345. if (nlen > lfs->name_max) {
  3346. return LFS_ERR_NAMETOOLONG;
  3347. }
  3348. // there is a small chance we are being renamed in the same
  3349. // directory/ to an id less than our old id, the global update
  3350. // to handle this is a bit messy
  3351. if (samepair && newid <= newoldid) {
  3352. newoldid += 1;
  3353. }
  3354. } else if (lfs_tag_type3(prevtag) != lfs_tag_type3(oldtag)) {
  3355. return LFS_ERR_ISDIR;
  3356. } else if (samepair && newid == newoldid) {
  3357. // we're renaming to ourselves??
  3358. return 0;
  3359. } else if (lfs_tag_type3(prevtag) == LFS_TYPE_DIR) {
  3360. // must be empty before removal
  3361. lfs_block_t prevpair[2];
  3362. lfs_stag_t res = lfs_dir_get(lfs, &newcwd, LFS_MKTAG(0x700, 0x3ff, 0),
  3363. LFS_MKTAG(LFS_TYPE_STRUCT, newid, 8), prevpair);
  3364. if (res < 0) {
  3365. return (int)res;
  3366. }
  3367. lfs_pair_fromle32(prevpair);
  3368. // must be empty before removal
  3369. err = lfs_dir_fetch(lfs, &prevdir.m, prevpair);
  3370. if (err) {
  3371. return err;
  3372. }
  3373. if (prevdir.m.count > 0 || prevdir.m.split) {
  3374. return LFS_ERR_NOTEMPTY;
  3375. }
  3376. // mark fs as orphaned
  3377. err = lfs_fs_preporphans(lfs, +1);
  3378. if (err) {
  3379. return err;
  3380. }
  3381. // I know it's crazy but yes, dir can be changed by our parent's
  3382. // commit (if predecessor is child)
  3383. prevdir.type = 0;
  3384. prevdir.id = 0;
  3385. lfs->mlist = &prevdir;
  3386. }
  3387. if (!samepair) {
  3388. lfs_fs_prepmove(lfs, newoldid, oldcwd.pair);
  3389. }
  3390. // move over all attributes
  3391. err = lfs_dir_commit(lfs, &newcwd, LFS_MKATTRS(
  3392. {LFS_MKTAG_IF(prevtag != LFS_ERR_NOENT,
  3393. LFS_TYPE_DELETE, newid, 0), NULL},
  3394. {LFS_MKTAG(LFS_TYPE_CREATE, newid, 0), NULL},
  3395. {LFS_MKTAG(lfs_tag_type3(oldtag), newid, strlen(newpath)), newpath},
  3396. {LFS_MKTAG(LFS_FROM_MOVE, newid, lfs_tag_id(oldtag)), &oldcwd},
  3397. {LFS_MKTAG_IF(samepair,
  3398. LFS_TYPE_DELETE, newoldid, 0), NULL}));
  3399. if (err) {
  3400. lfs->mlist = prevdir.next;
  3401. return err;
  3402. }
  3403. // let commit clean up after move (if we're different! otherwise move
  3404. // logic already fixed it for us)
  3405. if (!samepair && lfs_gstate_hasmove(&lfs->gstate)) {
  3406. // prep gstate and delete move id
  3407. lfs_fs_prepmove(lfs, 0x3ff, NULL);
  3408. err = lfs_dir_commit(lfs, &oldcwd, LFS_MKATTRS(
  3409. {LFS_MKTAG(LFS_TYPE_DELETE, lfs_tag_id(oldtag), 0), NULL}));
  3410. if (err) {
  3411. lfs->mlist = prevdir.next;
  3412. return err;
  3413. }
  3414. }
  3415. lfs->mlist = prevdir.next;
  3416. if (prevtag != LFS_ERR_NOENT
  3417. && lfs_tag_type3(prevtag) == LFS_TYPE_DIR) {
  3418. // fix orphan
  3419. err = lfs_fs_preporphans(lfs, -1);
  3420. if (err) {
  3421. return err;
  3422. }
  3423. err = lfs_fs_pred(lfs, prevdir.m.pair, &newcwd);
  3424. if (err) {
  3425. return err;
  3426. }
  3427. err = lfs_dir_drop(lfs, &newcwd, &prevdir.m);
  3428. if (err) {
  3429. return err;
  3430. }
  3431. }
  3432. return 0;
  3433. }
  3434. #endif
  3435. static lfs_ssize_t lfs_rawgetattr(lfs_t *lfs, const char *path,
  3436. uint8_t type, void *buffer, lfs_size_t size) {
  3437. lfs_mdir_t cwd;
  3438. lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL);
  3439. if (tag < 0) {
  3440. return tag;
  3441. }
  3442. uint16_t id = lfs_tag_id(tag);
  3443. if (id == 0x3ff) {
  3444. // special case for root
  3445. id = 0;
  3446. int err = lfs_dir_fetch(lfs, &cwd, lfs->root);
  3447. if (err) {
  3448. return err;
  3449. }
  3450. }
  3451. tag = lfs_dir_get(lfs, &cwd, LFS_MKTAG(0x7ff, 0x3ff, 0),
  3452. LFS_MKTAG(LFS_TYPE_USERATTR + type,
  3453. id, lfs_min(size, lfs->attr_max)),
  3454. buffer);
  3455. if (tag < 0) {
  3456. if (tag == LFS_ERR_NOENT) {
  3457. return LFS_ERR_NOATTR;
  3458. }
  3459. return tag;
  3460. }
  3461. return lfs_tag_size(tag);
  3462. }
  3463. #ifndef LFS_READONLY
  3464. static int lfs_commitattr(lfs_t *lfs, const char *path,
  3465. uint8_t type, const void *buffer, lfs_size_t size) {
  3466. lfs_mdir_t cwd;
  3467. lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL);
  3468. if (tag < 0) {
  3469. return tag;
  3470. }
  3471. uint16_t id = lfs_tag_id(tag);
  3472. if (id == 0x3ff) {
  3473. // special case for root
  3474. id = 0;
  3475. int err = lfs_dir_fetch(lfs, &cwd, lfs->root);
  3476. if (err) {
  3477. return err;
  3478. }
  3479. }
  3480. return lfs_dir_commit(lfs, &cwd, LFS_MKATTRS(
  3481. {LFS_MKTAG(LFS_TYPE_USERATTR + type, id, size), buffer}));
  3482. }
  3483. #endif
  3484. #ifndef LFS_READONLY
  3485. static int lfs_rawsetattr(lfs_t *lfs, const char *path,
  3486. uint8_t type, const void *buffer, lfs_size_t size) {
  3487. if (size > lfs->attr_max) {
  3488. return LFS_ERR_NOSPC;
  3489. }
  3490. return lfs_commitattr(lfs, path, type, buffer, size);
  3491. }
  3492. #endif
  3493. #ifndef LFS_READONLY
  3494. static int lfs_rawremoveattr(lfs_t *lfs, const char *path, uint8_t type) {
  3495. return lfs_commitattr(lfs, path, type, NULL, 0x3ff);
  3496. }
  3497. #endif
  3498. /// Filesystem operations ///
  3499. static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
  3500. lfs->cfg = cfg;
  3501. int err = 0;
  3502. // validate that the lfs-cfg sizes were initiated properly before
  3503. // performing any arithmetic logics with them
  3504. LFS_ASSERT(lfs->cfg->read_size != 0);
  3505. LFS_ASSERT(lfs->cfg->prog_size != 0);
  3506. LFS_ASSERT(lfs->cfg->cache_size != 0);
  3507. // check that block size is a multiple of cache size is a multiple
  3508. // of prog and read sizes
  3509. LFS_ASSERT(lfs->cfg->cache_size % lfs->cfg->read_size == 0);
  3510. LFS_ASSERT(lfs->cfg->cache_size % lfs->cfg->prog_size == 0);
  3511. LFS_ASSERT(lfs->cfg->block_size % lfs->cfg->cache_size == 0);
  3512. // check that the block size is large enough to fit ctz pointers
  3513. LFS_ASSERT(4*lfs_npw2(0xffffffff / (lfs->cfg->block_size-2*4))
  3514. <= lfs->cfg->block_size);
  3515. // block_cycles = 0 is no longer supported.
  3516. //
  3517. // block_cycles is the number of erase cycles before littlefs evicts
  3518. // metadata logs as a part of wear leveling. Suggested values are in the
  3519. // range of 100-1000, or set block_cycles to -1 to disable block-level
  3520. // wear-leveling.
  3521. LFS_ASSERT(lfs->cfg->block_cycles != 0);
  3522. // setup read cache
  3523. if (lfs->cfg->read_buffer) {
  3524. lfs->rcache.buffer = lfs->cfg->read_buffer;
  3525. } else {
  3526. lfs->rcache.buffer = lfs_malloc(lfs->cfg->cache_size);
  3527. if (!lfs->rcache.buffer) {
  3528. err = LFS_ERR_NOMEM;
  3529. goto cleanup;
  3530. }
  3531. }
  3532. // setup program cache
  3533. if (lfs->cfg->prog_buffer) {
  3534. lfs->pcache.buffer = lfs->cfg->prog_buffer;
  3535. } else {
  3536. lfs->pcache.buffer = lfs_malloc(lfs->cfg->cache_size);
  3537. if (!lfs->pcache.buffer) {
  3538. err = LFS_ERR_NOMEM;
  3539. goto cleanup;
  3540. }
  3541. }
  3542. // zero to avoid information leaks
  3543. lfs_cache_zero(lfs, &lfs->rcache);
  3544. lfs_cache_zero(lfs, &lfs->pcache);
  3545. // setup lookahead, must be multiple of 64-bits, 32-bit aligned
  3546. LFS_ASSERT(lfs->cfg->lookahead_size > 0);
  3547. LFS_ASSERT(lfs->cfg->lookahead_size % 8 == 0 &&
  3548. (uintptr_t)lfs->cfg->lookahead_buffer % 4 == 0);
  3549. if (lfs->cfg->lookahead_buffer) {
  3550. lfs->free.buffer = lfs->cfg->lookahead_buffer;
  3551. } else {
  3552. lfs->free.buffer = lfs_malloc(lfs->cfg->lookahead_size);
  3553. if (!lfs->free.buffer) {
  3554. err = LFS_ERR_NOMEM;
  3555. goto cleanup;
  3556. }
  3557. }
  3558. // check that the size limits are sane
  3559. LFS_ASSERT(lfs->cfg->name_max <= LFS_NAME_MAX);
  3560. lfs->name_max = lfs->cfg->name_max;
  3561. if (!lfs->name_max) {
  3562. lfs->name_max = LFS_NAME_MAX;
  3563. }
  3564. LFS_ASSERT(lfs->cfg->file_max <= LFS_FILE_MAX);
  3565. lfs->file_max = lfs->cfg->file_max;
  3566. if (!lfs->file_max) {
  3567. lfs->file_max = LFS_FILE_MAX;
  3568. }
  3569. LFS_ASSERT(lfs->cfg->attr_max <= LFS_ATTR_MAX);
  3570. lfs->attr_max = lfs->cfg->attr_max;
  3571. if (!lfs->attr_max) {
  3572. lfs->attr_max = LFS_ATTR_MAX;
  3573. }
  3574. LFS_ASSERT(lfs->cfg->metadata_max <= lfs->cfg->block_size);
  3575. // setup default state
  3576. lfs->root[0] = LFS_BLOCK_NULL;
  3577. lfs->root[1] = LFS_BLOCK_NULL;
  3578. lfs->mlist = NULL;
  3579. lfs->seed = 0;
  3580. lfs->gdisk = (lfs_gstate_t){0};
  3581. lfs->gstate = (lfs_gstate_t){0};
  3582. lfs->gdelta = (lfs_gstate_t){0};
  3583. #ifdef LFS_MIGRATE
  3584. lfs->lfs1 = NULL;
  3585. #endif
  3586. return 0;
  3587. cleanup:
  3588. lfs_deinit(lfs);
  3589. return err;
  3590. }
  3591. static int lfs_deinit(lfs_t *lfs) {
  3592. // free allocated memory
  3593. if (!lfs->cfg->read_buffer) {
  3594. lfs_free(lfs->rcache.buffer);
  3595. }
  3596. if (!lfs->cfg->prog_buffer) {
  3597. lfs_free(lfs->pcache.buffer);
  3598. }
  3599. if (!lfs->cfg->lookahead_buffer) {
  3600. lfs_free(lfs->free.buffer);
  3601. }
  3602. return 0;
  3603. }
  3604. #ifndef LFS_READONLY
  3605. static int lfs_rawformat(lfs_t *lfs, const struct lfs_config *cfg) {
  3606. int err = 0;
  3607. {
  3608. err = lfs_init(lfs, cfg);
  3609. if (err) {
  3610. return err;
  3611. }
  3612. // create free lookahead
  3613. memset(lfs->free.buffer, 0, lfs->cfg->lookahead_size);
  3614. lfs->free.off = 0;
  3615. lfs->free.size = lfs_min(8*lfs->cfg->lookahead_size,
  3616. lfs->cfg->block_count);
  3617. lfs->free.i = 0;
  3618. lfs_alloc_ack(lfs);
  3619. // create root dir
  3620. lfs_mdir_t root;
  3621. err = lfs_dir_alloc(lfs, &root);
  3622. if (err) {
  3623. goto cleanup;
  3624. }
  3625. // write one superblock
  3626. lfs_superblock_t superblock = {
  3627. .version = LFS_DISK_VERSION,
  3628. .block_size = lfs->cfg->block_size,
  3629. .block_count = lfs->cfg->block_count,
  3630. .name_max = lfs->name_max,
  3631. .file_max = lfs->file_max,
  3632. .attr_max = lfs->attr_max,
  3633. };
  3634. lfs_superblock_tole32(&superblock);
  3635. err = lfs_dir_commit(lfs, &root, LFS_MKATTRS(
  3636. {LFS_MKTAG(LFS_TYPE_CREATE, 0, 0), NULL},
  3637. {LFS_MKTAG(LFS_TYPE_SUPERBLOCK, 0, 8), "littlefs"},
  3638. {LFS_MKTAG(LFS_TYPE_INLINESTRUCT, 0, sizeof(superblock)),
  3639. &superblock}));
  3640. if (err) {
  3641. goto cleanup;
  3642. }
  3643. // force compaction to prevent accidentally mounting any
  3644. // older version of littlefs that may live on disk
  3645. root.erased = false;
  3646. err = lfs_dir_commit(lfs, &root, NULL, 0);
  3647. if (err) {
  3648. goto cleanup;
  3649. }
  3650. // sanity check that fetch works
  3651. err = lfs_dir_fetch(lfs, &root, (const lfs_block_t[2]){0, 1});
  3652. if (err) {
  3653. goto cleanup;
  3654. }
  3655. }
  3656. cleanup:
  3657. lfs_deinit(lfs);
  3658. return err;
  3659. }
  3660. #endif
  3661. static int lfs_rawmount(lfs_t *lfs, const struct lfs_config *cfg) {
  3662. int err = lfs_init(lfs, cfg);
  3663. if (err) {
  3664. return err;
  3665. }
  3666. // scan directory blocks for superblock and any global updates
  3667. lfs_mdir_t dir = {.tail = {0, 1}};
  3668. lfs_block_t cycle = 0;
  3669. while (!lfs_pair_isnull(dir.tail)) {
  3670. if (cycle >= lfs->cfg->block_count/2) {
  3671. // loop detected
  3672. err = LFS_ERR_CORRUPT;
  3673. goto cleanup;
  3674. }
  3675. cycle += 1;
  3676. // fetch next block in tail list
  3677. lfs_stag_t tag = lfs_dir_fetchmatch(lfs, &dir, dir.tail,
  3678. LFS_MKTAG(0x7ff, 0x3ff, 0),
  3679. LFS_MKTAG(LFS_TYPE_SUPERBLOCK, 0, 8),
  3680. NULL,
  3681. lfs_dir_find_match, &(struct lfs_dir_find_match){
  3682. lfs, "littlefs", 8});
  3683. if (tag < 0) {
  3684. err = tag;
  3685. goto cleanup;
  3686. }
  3687. // has superblock?
  3688. if (tag && !lfs_tag_isdelete(tag)) {
  3689. // update root
  3690. lfs->root[0] = dir.pair[0];
  3691. lfs->root[1] = dir.pair[1];
  3692. // grab superblock
  3693. lfs_superblock_t superblock;
  3694. tag = lfs_dir_get(lfs, &dir, LFS_MKTAG(0x7ff, 0x3ff, 0),
  3695. LFS_MKTAG(LFS_TYPE_INLINESTRUCT, 0, sizeof(superblock)),
  3696. &superblock);
  3697. if (tag < 0) {
  3698. err = tag;
  3699. goto cleanup;
  3700. }
  3701. lfs_superblock_fromle32(&superblock);
  3702. // check version
  3703. uint16_t major_version = (0xffff & (superblock.version >> 16));
  3704. uint16_t minor_version = (0xffff & (superblock.version >> 0));
  3705. if ((major_version != LFS_DISK_VERSION_MAJOR ||
  3706. minor_version > LFS_DISK_VERSION_MINOR)) {
  3707. LFS_ERROR("Invalid version v%"PRIu16".%"PRIu16,
  3708. major_version, minor_version);
  3709. err = LFS_ERR_INVAL;
  3710. goto cleanup;
  3711. }
  3712. // check superblock configuration
  3713. if (superblock.name_max) {
  3714. if (superblock.name_max > lfs->name_max) {
  3715. LFS_ERROR("Unsupported name_max (%"PRIu32" > %"PRIu32")",
  3716. superblock.name_max, lfs->name_max);
  3717. err = LFS_ERR_INVAL;
  3718. goto cleanup;
  3719. }
  3720. lfs->name_max = superblock.name_max;
  3721. }
  3722. if (superblock.file_max) {
  3723. if (superblock.file_max > lfs->file_max) {
  3724. LFS_ERROR("Unsupported file_max (%"PRIu32" > %"PRIu32")",
  3725. superblock.file_max, lfs->file_max);
  3726. err = LFS_ERR_INVAL;
  3727. goto cleanup;
  3728. }
  3729. lfs->file_max = superblock.file_max;
  3730. }
  3731. if (superblock.attr_max) {
  3732. if (superblock.attr_max > lfs->attr_max) {
  3733. LFS_ERROR("Unsupported attr_max (%"PRIu32" > %"PRIu32")",
  3734. superblock.attr_max, lfs->attr_max);
  3735. err = LFS_ERR_INVAL;
  3736. goto cleanup;
  3737. }
  3738. lfs->attr_max = superblock.attr_max;
  3739. }
  3740. if (superblock.block_count != lfs->cfg->block_count) {
  3741. LFS_ERROR("Invalid block count (%"PRIu32" != %"PRIu32")",
  3742. superblock.block_count, lfs->cfg->block_count);
  3743. err = LFS_ERR_INVAL;
  3744. goto cleanup;
  3745. }
  3746. if (superblock.block_size != lfs->cfg->block_size) {
  3747. LFS_ERROR("Invalid block size (%"PRIu32" != %"PRIu32")",
  3748. superblock.block_size, lfs->cfg->block_size);
  3749. err = LFS_ERR_INVAL;
  3750. goto cleanup;
  3751. }
  3752. }
  3753. // has gstate?
  3754. err = lfs_dir_getgstate(lfs, &dir, &lfs->gstate);
  3755. if (err) {
  3756. goto cleanup;
  3757. }
  3758. }
  3759. // found superblock?
  3760. if (lfs_pair_isnull(lfs->root)) {
  3761. err = LFS_ERR_INVAL;
  3762. goto cleanup;
  3763. }
  3764. // update littlefs with gstate
  3765. if (!lfs_gstate_iszero(&lfs->gstate)) {
  3766. LFS_DEBUG("Found pending gstate 0x%08"PRIx32"%08"PRIx32"%08"PRIx32,
  3767. lfs->gstate.tag,
  3768. lfs->gstate.pair[0],
  3769. lfs->gstate.pair[1]);
  3770. }
  3771. lfs->gstate.tag += !lfs_tag_isvalid(lfs->gstate.tag);
  3772. lfs->gdisk = lfs->gstate;
  3773. // setup free lookahead, to distribute allocations uniformly across
  3774. // boots, we start the allocator at a random location
  3775. lfs->free.off = lfs->seed % lfs->cfg->block_count;
  3776. lfs_alloc_drop(lfs);
  3777. return 0;
  3778. cleanup:
  3779. lfs_rawunmount(lfs);
  3780. return err;
  3781. }
  3782. static int lfs_rawunmount(lfs_t *lfs) {
  3783. return lfs_deinit(lfs);
  3784. }
  3785. /// Filesystem filesystem operations ///
  3786. int lfs_fs_rawtraverse(lfs_t *lfs,
  3787. int (*cb)(void *data, lfs_block_t block), void *data,
  3788. bool includeorphans) {
  3789. // iterate over metadata pairs
  3790. lfs_mdir_t dir = {.tail = {0, 1}};
  3791. #ifdef LFS_MIGRATE
  3792. // also consider v1 blocks during migration
  3793. if (lfs->lfs1) {
  3794. int err = lfs1_traverse(lfs, cb, data);
  3795. if (err) {
  3796. return err;
  3797. }
  3798. dir.tail[0] = lfs->root[0];
  3799. dir.tail[1] = lfs->root[1];
  3800. }
  3801. #endif
  3802. lfs_block_t cycle = 0;
  3803. while (!lfs_pair_isnull(dir.tail)) {
  3804. if (cycle >= lfs->cfg->block_count/2) {
  3805. // loop detected
  3806. return LFS_ERR_CORRUPT;
  3807. }
  3808. cycle += 1;
  3809. for (int i = 0; i < 2; i++) {
  3810. int err = cb(data, dir.tail[i]);
  3811. if (err) {
  3812. return err;
  3813. }
  3814. }
  3815. // iterate through ids in directory
  3816. int err = lfs_dir_fetch(lfs, &dir, dir.tail);
  3817. if (err) {
  3818. return err;
  3819. }
  3820. for (uint16_t id = 0; id < dir.count; id++) {
  3821. struct lfs_ctz ctz;
  3822. lfs_stag_t tag = lfs_dir_get(lfs, &dir, LFS_MKTAG(0x700, 0x3ff, 0),
  3823. LFS_MKTAG(LFS_TYPE_STRUCT, id, sizeof(ctz)), &ctz);
  3824. if (tag < 0) {
  3825. if (tag == LFS_ERR_NOENT) {
  3826. continue;
  3827. }
  3828. return tag;
  3829. }
  3830. lfs_ctz_fromle32(&ctz);
  3831. if (lfs_tag_type3(tag) == LFS_TYPE_CTZSTRUCT) {
  3832. err = lfs_ctz_traverse(lfs, NULL, &lfs->rcache,
  3833. ctz.head, ctz.size, cb, data);
  3834. if (err) {
  3835. return err;
  3836. }
  3837. } else if (includeorphans &&
  3838. lfs_tag_type3(tag) == LFS_TYPE_DIRSTRUCT) {
  3839. for (int i = 0; i < 2; i++) {
  3840. err = cb(data, (&ctz.head)[i]);
  3841. if (err) {
  3842. return err;
  3843. }
  3844. }
  3845. }
  3846. }
  3847. }
  3848. #ifndef LFS_READONLY
  3849. // iterate over any open files
  3850. for (lfs_file_t *f = (lfs_file_t*)lfs->mlist; f; f = f->next) {
  3851. if (f->type != LFS_TYPE_REG) {
  3852. continue;
  3853. }
  3854. if ((f->flags & LFS_F_DIRTY) && !(f->flags & LFS_F_INLINE)) {
  3855. int err = lfs_ctz_traverse(lfs, &f->cache, &lfs->rcache,
  3856. f->ctz.head, f->ctz.size, cb, data);
  3857. if (err) {
  3858. return err;
  3859. }
  3860. }
  3861. if ((f->flags & LFS_F_WRITING) && !(f->flags & LFS_F_INLINE)) {
  3862. int err = lfs_ctz_traverse(lfs, &f->cache, &lfs->rcache,
  3863. f->block, f->pos, cb, data);
  3864. if (err) {
  3865. return err;
  3866. }
  3867. }
  3868. }
  3869. #endif
  3870. return 0;
  3871. }
  3872. #ifndef LFS_READONLY
  3873. static int lfs_fs_pred(lfs_t *lfs,
  3874. const lfs_block_t pair[2], lfs_mdir_t *pdir) {
  3875. // iterate over all directory directory entries
  3876. pdir->tail[0] = 0;
  3877. pdir->tail[1] = 1;
  3878. lfs_block_t cycle = 0;
  3879. while (!lfs_pair_isnull(pdir->tail)) {
  3880. if (cycle >= lfs->cfg->block_count/2) {
  3881. // loop detected
  3882. return LFS_ERR_CORRUPT;
  3883. }
  3884. cycle += 1;
  3885. if (lfs_pair_cmp(pdir->tail, pair) == 0) {
  3886. return 0;
  3887. }
  3888. int err = lfs_dir_fetch(lfs, pdir, pdir->tail);
  3889. if (err) {
  3890. return err;
  3891. }
  3892. }
  3893. return LFS_ERR_NOENT;
  3894. }
  3895. #endif
  3896. #ifndef LFS_READONLY
  3897. struct lfs_fs_parent_match {
  3898. lfs_t *lfs;
  3899. const lfs_block_t pair[2];
  3900. };
  3901. #endif
  3902. #ifndef LFS_READONLY
  3903. static int lfs_fs_parent_match(void *data,
  3904. lfs_tag_t tag, const void *buffer) {
  3905. struct lfs_fs_parent_match *find = data;
  3906. lfs_t *lfs = find->lfs;
  3907. const struct lfs_diskoff *disk = buffer;
  3908. (void)tag;
  3909. lfs_block_t child[2];
  3910. int err = lfs_bd_read(lfs,
  3911. &lfs->pcache, &lfs->rcache, lfs->cfg->block_size,
  3912. disk->block, disk->off, &child, sizeof(child));
  3913. if (err) {
  3914. return err;
  3915. }
  3916. lfs_pair_fromle32(child);
  3917. return (lfs_pair_cmp(child, find->pair) == 0) ? LFS_CMP_EQ : LFS_CMP_LT;
  3918. }
  3919. #endif
  3920. #ifndef LFS_READONLY
  3921. static lfs_stag_t lfs_fs_parent(lfs_t *lfs, const lfs_block_t pair[2],
  3922. lfs_mdir_t *parent) {
  3923. // use fetchmatch with callback to find pairs
  3924. parent->tail[0] = 0;
  3925. parent->tail[1] = 1;
  3926. lfs_block_t cycle = 0;
  3927. while (!lfs_pair_isnull(parent->tail)) {
  3928. if (cycle >= lfs->cfg->block_count/2) {
  3929. // loop detected
  3930. return LFS_ERR_CORRUPT;
  3931. }
  3932. cycle += 1;
  3933. lfs_stag_t tag = lfs_dir_fetchmatch(lfs, parent, parent->tail,
  3934. LFS_MKTAG(0x7ff, 0, 0x3ff),
  3935. LFS_MKTAG(LFS_TYPE_DIRSTRUCT, 0, 8),
  3936. NULL,
  3937. lfs_fs_parent_match, &(struct lfs_fs_parent_match){
  3938. lfs, {pair[0], pair[1]}});
  3939. if (tag && tag != LFS_ERR_NOENT) {
  3940. return tag;
  3941. }
  3942. }
  3943. return LFS_ERR_NOENT;
  3944. }
  3945. #endif
  3946. #ifndef LFS_READONLY
  3947. static int lfs_fs_preporphans(lfs_t *lfs, int8_t orphans) {
  3948. LFS_ASSERT(lfs_tag_size(lfs->gstate.tag) > 0 || orphans >= 0);
  3949. lfs->gstate.tag += orphans;
  3950. lfs->gstate.tag = ((lfs->gstate.tag & ~LFS_MKTAG(0x800, 0, 0)) |
  3951. ((uint32_t)lfs_gstate_hasorphans(&lfs->gstate) << 31));
  3952. return 0;
  3953. }
  3954. #endif
  3955. #ifndef LFS_READONLY
  3956. static void lfs_fs_prepmove(lfs_t *lfs,
  3957. uint16_t id, const lfs_block_t pair[2]) {
  3958. lfs->gstate.tag = ((lfs->gstate.tag & ~LFS_MKTAG(0x7ff, 0x3ff, 0)) |
  3959. ((id != 0x3ff) ? LFS_MKTAG(LFS_TYPE_DELETE, id, 0) : 0));
  3960. lfs->gstate.pair[0] = (id != 0x3ff) ? pair[0] : 0;
  3961. lfs->gstate.pair[1] = (id != 0x3ff) ? pair[1] : 0;
  3962. }
  3963. #endif
  3964. #ifndef LFS_READONLY
  3965. static int lfs_fs_demove(lfs_t *lfs) {
  3966. if (!lfs_gstate_hasmove(&lfs->gdisk)) {
  3967. return 0;
  3968. }
  3969. // Fix bad moves
  3970. LFS_DEBUG("Fixing move {0x%"PRIx32", 0x%"PRIx32"} 0x%"PRIx16,
  3971. lfs->gdisk.pair[0],
  3972. lfs->gdisk.pair[1],
  3973. lfs_tag_id(lfs->gdisk.tag));
  3974. // fetch and delete the moved entry
  3975. lfs_mdir_t movedir;
  3976. int err = lfs_dir_fetch(lfs, &movedir, lfs->gdisk.pair);
  3977. if (err) {
  3978. return err;
  3979. }
  3980. // prep gstate and delete move id
  3981. uint16_t moveid = lfs_tag_id(lfs->gdisk.tag);
  3982. lfs_fs_prepmove(lfs, 0x3ff, NULL);
  3983. err = lfs_dir_commit(lfs, &movedir, LFS_MKATTRS(
  3984. {LFS_MKTAG(LFS_TYPE_DELETE, moveid, 0), NULL}));
  3985. if (err) {
  3986. return err;
  3987. }
  3988. return 0;
  3989. }
  3990. #endif
  3991. #ifndef LFS_READONLY
  3992. static int lfs_fs_deorphan(lfs_t *lfs, bool powerloss) {
  3993. if (!lfs_gstate_hasorphans(&lfs->gstate)) {
  3994. return 0;
  3995. }
  3996. int8_t found = 0;
  3997. restart:
  3998. // Check for orphans in two separate passes:
  3999. // - 1 for half-orphans (relocations)
  4000. // - 2 for full-orphans (removes/renames)
  4001. //
  4002. // Two separate passes are needed as half-orphans can contain outdated
  4003. // references to full-orphans, effectively hiding them from the deorphan
  4004. // search.
  4005. //
  4006. for (int pass = 0; pass < 2; pass++) {
  4007. // Fix any orphans
  4008. lfs_mdir_t pdir = {.split = true, .tail = {0, 1}};
  4009. lfs_mdir_t dir;
  4010. // iterate over all directory directory entries
  4011. while (!lfs_pair_isnull(pdir.tail)) {
  4012. int err = lfs_dir_fetch(lfs, &dir, pdir.tail);
  4013. if (err) {
  4014. return err;
  4015. }
  4016. // check head blocks for orphans
  4017. if (!pdir.split) {
  4018. // check if we have a parent
  4019. lfs_mdir_t parent;
  4020. lfs_stag_t tag = lfs_fs_parent(lfs, pdir.tail, &parent);
  4021. if (tag < 0 && tag != LFS_ERR_NOENT) {
  4022. return tag;
  4023. }
  4024. if (pass == 0 && tag != LFS_ERR_NOENT) {
  4025. lfs_block_t pair[2];
  4026. lfs_stag_t state = lfs_dir_get(lfs, &parent,
  4027. LFS_MKTAG(0x7ff, 0x3ff, 0), tag, pair);
  4028. if (state < 0) {
  4029. return state;
  4030. }
  4031. lfs_pair_fromle32(pair);
  4032. if (!lfs_pair_sync(pair, pdir.tail)) {
  4033. // we have desynced
  4034. LFS_DEBUG("Fixing half-orphan "
  4035. "{0x%"PRIx32", 0x%"PRIx32"} "
  4036. "-> {0x%"PRIx32", 0x%"PRIx32"}",
  4037. pdir.tail[0], pdir.tail[1], pair[0], pair[1]);
  4038. // fix pending move in this pair? this looks like an
  4039. // optimization but is in fact _required_ since
  4040. // relocating may outdate the move.
  4041. uint16_t moveid = 0x3ff;
  4042. if (lfs_gstate_hasmovehere(&lfs->gstate, pdir.pair)) {
  4043. moveid = lfs_tag_id(lfs->gstate.tag);
  4044. LFS_DEBUG("Fixing move while fixing orphans "
  4045. "{0x%"PRIx32", 0x%"PRIx32"} 0x%"PRIx16"\n",
  4046. pdir.pair[0], pdir.pair[1], moveid);
  4047. lfs_fs_prepmove(lfs, 0x3ff, NULL);
  4048. }
  4049. lfs_pair_tole32(pair);
  4050. state = lfs_dir_orphaningcommit(lfs, &pdir, LFS_MKATTRS(
  4051. {LFS_MKTAG_IF(moveid != 0x3ff,
  4052. LFS_TYPE_DELETE, moveid, 0), NULL},
  4053. {LFS_MKTAG(LFS_TYPE_SOFTTAIL, 0x3ff, 8),
  4054. pair}));
  4055. lfs_pair_fromle32(pair);
  4056. if (state < 0) {
  4057. return state;
  4058. }
  4059. found += 1;
  4060. // did our commit create more orphans?
  4061. if (state == LFS_OK_ORPHANED) {
  4062. goto restart;
  4063. }
  4064. // refetch tail
  4065. continue;
  4066. }
  4067. }
  4068. // note we only check for full orphans if we may have had a
  4069. // power-loss, otherwise orphans are created intentionally
  4070. // during operations such as lfs_mkdir
  4071. if (pass == 1 && tag == LFS_ERR_NOENT && powerloss) {
  4072. // we are an orphan
  4073. LFS_DEBUG("Fixing orphan {0x%"PRIx32", 0x%"PRIx32"}",
  4074. pdir.tail[0], pdir.tail[1]);
  4075. // steal state
  4076. err = lfs_dir_getgstate(lfs, &dir, &lfs->gdelta);
  4077. if (err) {
  4078. return err;
  4079. }
  4080. // steal tail
  4081. lfs_pair_tole32(dir.tail);
  4082. int state = lfs_dir_orphaningcommit(lfs, &pdir, LFS_MKATTRS(
  4083. {LFS_MKTAG(LFS_TYPE_TAIL + dir.split, 0x3ff, 8),
  4084. dir.tail}));
  4085. lfs_pair_fromle32(dir.tail);
  4086. if (state < 0) {
  4087. return state;
  4088. }
  4089. found += 1;
  4090. // did our commit create more orphans?
  4091. if (state == LFS_OK_ORPHANED) {
  4092. goto restart;
  4093. }
  4094. // refetch tail
  4095. continue;
  4096. }
  4097. }
  4098. pdir = dir;
  4099. }
  4100. }
  4101. // mark orphans as fixed
  4102. return lfs_fs_preporphans(lfs, -lfs_min(
  4103. lfs_gstate_getorphans(&lfs->gstate),
  4104. found));
  4105. }
  4106. #endif
  4107. #ifndef LFS_READONLY
  4108. static int lfs_fs_forceconsistency(lfs_t *lfs) {
  4109. int err = lfs_fs_demove(lfs);
  4110. if (err) {
  4111. return err;
  4112. }
  4113. err = lfs_fs_deorphan(lfs, true);
  4114. if (err) {
  4115. return err;
  4116. }
  4117. return 0;
  4118. }
  4119. #endif
  4120. static int lfs_fs_size_count(void *p, lfs_block_t block) {
  4121. (void)block;
  4122. lfs_size_t *size = p;
  4123. *size += 1;
  4124. return 0;
  4125. }
  4126. static lfs_ssize_t lfs_fs_rawsize(lfs_t *lfs) {
  4127. lfs_size_t size = 0;
  4128. int err = lfs_fs_rawtraverse(lfs, lfs_fs_size_count, &size, false);
  4129. if (err) {
  4130. return err;
  4131. }
  4132. return size;
  4133. }
  4134. #ifdef LFS_MIGRATE
  4135. ////// Migration from littelfs v1 below this //////
  4136. /// Version info ///
  4137. // Software library version
  4138. // Major (top-nibble), incremented on backwards incompatible changes
  4139. // Minor (bottom-nibble), incremented on feature additions
  4140. #define LFS1_VERSION 0x00010007
  4141. #define LFS1_VERSION_MAJOR (0xffff & (LFS1_VERSION >> 16))
  4142. #define LFS1_VERSION_MINOR (0xffff & (LFS1_VERSION >> 0))
  4143. // Version of On-disk data structures
  4144. // Major (top-nibble), incremented on backwards incompatible changes
  4145. // Minor (bottom-nibble), incremented on feature additions
  4146. #define LFS1_DISK_VERSION 0x00010001
  4147. #define LFS1_DISK_VERSION_MAJOR (0xffff & (LFS1_DISK_VERSION >> 16))
  4148. #define LFS1_DISK_VERSION_MINOR (0xffff & (LFS1_DISK_VERSION >> 0))
  4149. /// v1 Definitions ///
  4150. // File types
  4151. enum lfs1_type {
  4152. LFS1_TYPE_REG = 0x11,
  4153. LFS1_TYPE_DIR = 0x22,
  4154. LFS1_TYPE_SUPERBLOCK = 0x2e,
  4155. };
  4156. typedef struct lfs1 {
  4157. lfs_block_t root[2];
  4158. } lfs1_t;
  4159. typedef struct lfs1_entry {
  4160. lfs_off_t off;
  4161. struct lfs1_disk_entry {
  4162. uint8_t type;
  4163. uint8_t elen;
  4164. uint8_t alen;
  4165. uint8_t nlen;
  4166. union {
  4167. struct {
  4168. lfs_block_t head;
  4169. lfs_size_t size;
  4170. } file;
  4171. lfs_block_t dir[2];
  4172. } u;
  4173. } d;
  4174. } lfs1_entry_t;
  4175. typedef struct lfs1_dir {
  4176. struct lfs1_dir *next;
  4177. lfs_block_t pair[2];
  4178. lfs_off_t off;
  4179. lfs_block_t head[2];
  4180. lfs_off_t pos;
  4181. struct lfs1_disk_dir {
  4182. uint32_t rev;
  4183. lfs_size_t size;
  4184. lfs_block_t tail[2];
  4185. } d;
  4186. } lfs1_dir_t;
  4187. typedef struct lfs1_superblock {
  4188. lfs_off_t off;
  4189. struct lfs1_disk_superblock {
  4190. uint8_t type;
  4191. uint8_t elen;
  4192. uint8_t alen;
  4193. uint8_t nlen;
  4194. lfs_block_t root[2];
  4195. uint32_t block_size;
  4196. uint32_t block_count;
  4197. uint32_t version;
  4198. char magic[8];
  4199. } d;
  4200. } lfs1_superblock_t;
  4201. /// Low-level wrappers v1->v2 ///
  4202. static void lfs1_crc(uint32_t *crc, const void *buffer, size_t size) {
  4203. *crc = lfs_crc(*crc, buffer, size);
  4204. }
  4205. static int lfs1_bd_read(lfs_t *lfs, lfs_block_t block,
  4206. lfs_off_t off, void *buffer, lfs_size_t size) {
  4207. // if we ever do more than writes to alternating pairs,
  4208. // this may need to consider pcache
  4209. return lfs_bd_read(lfs, &lfs->pcache, &lfs->rcache, size,
  4210. block, off, buffer, size);
  4211. }
  4212. static int lfs1_bd_crc(lfs_t *lfs, lfs_block_t block,
  4213. lfs_off_t off, lfs_size_t size, uint32_t *crc) {
  4214. for (lfs_off_t i = 0; i < size; i++) {
  4215. uint8_t c;
  4216. int err = lfs1_bd_read(lfs, block, off+i, &c, 1);
  4217. if (err) {
  4218. return err;
  4219. }
  4220. lfs1_crc(crc, &c, 1);
  4221. }
  4222. return 0;
  4223. }
  4224. /// Endian swapping functions ///
  4225. static void lfs1_dir_fromle32(struct lfs1_disk_dir *d) {
  4226. d->rev = lfs_fromle32(d->rev);
  4227. d->size = lfs_fromle32(d->size);
  4228. d->tail[0] = lfs_fromle32(d->tail[0]);
  4229. d->tail[1] = lfs_fromle32(d->tail[1]);
  4230. }
  4231. static void lfs1_dir_tole32(struct lfs1_disk_dir *d) {
  4232. d->rev = lfs_tole32(d->rev);
  4233. d->size = lfs_tole32(d->size);
  4234. d->tail[0] = lfs_tole32(d->tail[0]);
  4235. d->tail[1] = lfs_tole32(d->tail[1]);
  4236. }
  4237. static void lfs1_entry_fromle32(struct lfs1_disk_entry *d) {
  4238. d->u.dir[0] = lfs_fromle32(d->u.dir[0]);
  4239. d->u.dir[1] = lfs_fromle32(d->u.dir[1]);
  4240. }
  4241. static void lfs1_entry_tole32(struct lfs1_disk_entry *d) {
  4242. d->u.dir[0] = lfs_tole32(d->u.dir[0]);
  4243. d->u.dir[1] = lfs_tole32(d->u.dir[1]);
  4244. }
  4245. static void lfs1_superblock_fromle32(struct lfs1_disk_superblock *d) {
  4246. d->root[0] = lfs_fromle32(d->root[0]);
  4247. d->root[1] = lfs_fromle32(d->root[1]);
  4248. d->block_size = lfs_fromle32(d->block_size);
  4249. d->block_count = lfs_fromle32(d->block_count);
  4250. d->version = lfs_fromle32(d->version);
  4251. }
  4252. ///// Metadata pair and directory operations ///
  4253. static inline lfs_size_t lfs1_entry_size(const lfs1_entry_t *entry) {
  4254. return 4 + entry->d.elen + entry->d.alen + entry->d.nlen;
  4255. }
  4256. static int lfs1_dir_fetch(lfs_t *lfs,
  4257. lfs1_dir_t *dir, const lfs_block_t pair[2]) {
  4258. // copy out pair, otherwise may be aliasing dir
  4259. const lfs_block_t tpair[2] = {pair[0], pair[1]};
  4260. bool valid = false;
  4261. // check both blocks for the most recent revision
  4262. for (int i = 0; i < 2; i++) {
  4263. struct lfs1_disk_dir test;
  4264. int err = lfs1_bd_read(lfs, tpair[i], 0, &test, sizeof(test));
  4265. lfs1_dir_fromle32(&test);
  4266. if (err) {
  4267. if (err == LFS_ERR_CORRUPT) {
  4268. continue;
  4269. }
  4270. return err;
  4271. }
  4272. if (valid && lfs_scmp(test.rev, dir->d.rev) < 0) {
  4273. continue;
  4274. }
  4275. if ((0x7fffffff & test.size) < sizeof(test)+4 ||
  4276. (0x7fffffff & test.size) > lfs->cfg->block_size) {
  4277. continue;
  4278. }
  4279. uint32_t crc = 0xffffffff;
  4280. lfs1_dir_tole32(&test);
  4281. lfs1_crc(&crc, &test, sizeof(test));
  4282. lfs1_dir_fromle32(&test);
  4283. err = lfs1_bd_crc(lfs, tpair[i], sizeof(test),
  4284. (0x7fffffff & test.size) - sizeof(test), &crc);
  4285. if (err) {
  4286. if (err == LFS_ERR_CORRUPT) {
  4287. continue;
  4288. }
  4289. return err;
  4290. }
  4291. if (crc != 0) {
  4292. continue;
  4293. }
  4294. valid = true;
  4295. // setup dir in case it's valid
  4296. dir->pair[0] = tpair[(i+0) % 2];
  4297. dir->pair[1] = tpair[(i+1) % 2];
  4298. dir->off = sizeof(dir->d);
  4299. dir->d = test;
  4300. }
  4301. if (!valid) {
  4302. LFS_ERROR("Corrupted dir pair at {0x%"PRIx32", 0x%"PRIx32"}",
  4303. tpair[0], tpair[1]);
  4304. return LFS_ERR_CORRUPT;
  4305. }
  4306. return 0;
  4307. }
  4308. static int lfs1_dir_next(lfs_t *lfs, lfs1_dir_t *dir, lfs1_entry_t *entry) {
  4309. while (dir->off + sizeof(entry->d) > (0x7fffffff & dir->d.size)-4) {
  4310. if (!(0x80000000 & dir->d.size)) {
  4311. entry->off = dir->off;
  4312. return LFS_ERR_NOENT;
  4313. }
  4314. int err = lfs1_dir_fetch(lfs, dir, dir->d.tail);
  4315. if (err) {
  4316. return err;
  4317. }
  4318. dir->off = sizeof(dir->d);
  4319. dir->pos += sizeof(dir->d) + 4;
  4320. }
  4321. int err = lfs1_bd_read(lfs, dir->pair[0], dir->off,
  4322. &entry->d, sizeof(entry->d));
  4323. lfs1_entry_fromle32(&entry->d);
  4324. if (err) {
  4325. return err;
  4326. }
  4327. entry->off = dir->off;
  4328. dir->off += lfs1_entry_size(entry);
  4329. dir->pos += lfs1_entry_size(entry);
  4330. return 0;
  4331. }
  4332. /// littlefs v1 specific operations ///
  4333. int lfs1_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data) {
  4334. if (lfs_pair_isnull(lfs->lfs1->root)) {
  4335. return 0;
  4336. }
  4337. // iterate over metadata pairs
  4338. lfs1_dir_t dir;
  4339. lfs1_entry_t entry;
  4340. lfs_block_t cwd[2] = {0, 1};
  4341. while (true) {
  4342. for (int i = 0; i < 2; i++) {
  4343. int err = cb(data, cwd[i]);
  4344. if (err) {
  4345. return err;
  4346. }
  4347. }
  4348. int err = lfs1_dir_fetch(lfs, &dir, cwd);
  4349. if (err) {
  4350. return err;
  4351. }
  4352. // iterate over contents
  4353. while (dir.off + sizeof(entry.d) <= (0x7fffffff & dir.d.size)-4) {
  4354. err = lfs1_bd_read(lfs, dir.pair[0], dir.off,
  4355. &entry.d, sizeof(entry.d));
  4356. lfs1_entry_fromle32(&entry.d);
  4357. if (err) {
  4358. return err;
  4359. }
  4360. dir.off += lfs1_entry_size(&entry);
  4361. if ((0x70 & entry.d.type) == (0x70 & LFS1_TYPE_REG)) {
  4362. err = lfs_ctz_traverse(lfs, NULL, &lfs->rcache,
  4363. entry.d.u.file.head, entry.d.u.file.size, cb, data);
  4364. if (err) {
  4365. return err;
  4366. }
  4367. }
  4368. }
  4369. // we also need to check if we contain a threaded v2 directory
  4370. lfs_mdir_t dir2 = {.split=true, .tail={cwd[0], cwd[1]}};
  4371. while (dir2.split) {
  4372. err = lfs_dir_fetch(lfs, &dir2, dir2.tail);
  4373. if (err) {
  4374. break;
  4375. }
  4376. for (int i = 0; i < 2; i++) {
  4377. err = cb(data, dir2.pair[i]);
  4378. if (err) {
  4379. return err;
  4380. }
  4381. }
  4382. }
  4383. cwd[0] = dir.d.tail[0];
  4384. cwd[1] = dir.d.tail[1];
  4385. if (lfs_pair_isnull(cwd)) {
  4386. break;
  4387. }
  4388. }
  4389. return 0;
  4390. }
  4391. static int lfs1_moved(lfs_t *lfs, const void *e) {
  4392. if (lfs_pair_isnull(lfs->lfs1->root)) {
  4393. return 0;
  4394. }
  4395. // skip superblock
  4396. lfs1_dir_t cwd;
  4397. int err = lfs1_dir_fetch(lfs, &cwd, (const lfs_block_t[2]){0, 1});
  4398. if (err) {
  4399. return err;
  4400. }
  4401. // iterate over all directory directory entries
  4402. lfs1_entry_t entry;
  4403. while (!lfs_pair_isnull(cwd.d.tail)) {
  4404. err = lfs1_dir_fetch(lfs, &cwd, cwd.d.tail);
  4405. if (err) {
  4406. return err;
  4407. }
  4408. while (true) {
  4409. err = lfs1_dir_next(lfs, &cwd, &entry);
  4410. if (err && err != LFS_ERR_NOENT) {
  4411. return err;
  4412. }
  4413. if (err == LFS_ERR_NOENT) {
  4414. break;
  4415. }
  4416. if (!(0x80 & entry.d.type) &&
  4417. memcmp(&entry.d.u, e, sizeof(entry.d.u)) == 0) {
  4418. return true;
  4419. }
  4420. }
  4421. }
  4422. return false;
  4423. }
  4424. /// Filesystem operations ///
  4425. static int lfs1_mount(lfs_t *lfs, struct lfs1 *lfs1,
  4426. const struct lfs_config *cfg) {
  4427. int err = 0;
  4428. {
  4429. err = lfs_init(lfs, cfg);
  4430. if (err) {
  4431. return err;
  4432. }
  4433. lfs->lfs1 = lfs1;
  4434. lfs->lfs1->root[0] = LFS_BLOCK_NULL;
  4435. lfs->lfs1->root[1] = LFS_BLOCK_NULL;
  4436. // setup free lookahead
  4437. lfs->free.off = 0;
  4438. lfs->free.size = 0;
  4439. lfs->free.i = 0;
  4440. lfs_alloc_ack(lfs);
  4441. // load superblock
  4442. lfs1_dir_t dir;
  4443. lfs1_superblock_t superblock;
  4444. err = lfs1_dir_fetch(lfs, &dir, (const lfs_block_t[2]){0, 1});
  4445. if (err && err != LFS_ERR_CORRUPT) {
  4446. goto cleanup;
  4447. }
  4448. if (!err) {
  4449. err = lfs1_bd_read(lfs, dir.pair[0], sizeof(dir.d),
  4450. &superblock.d, sizeof(superblock.d));
  4451. lfs1_superblock_fromle32(&superblock.d);
  4452. if (err) {
  4453. goto cleanup;
  4454. }
  4455. lfs->lfs1->root[0] = superblock.d.root[0];
  4456. lfs->lfs1->root[1] = superblock.d.root[1];
  4457. }
  4458. if (err || memcmp(superblock.d.magic, "littlefs", 8) != 0) {
  4459. LFS_ERROR("Invalid superblock at {0x%"PRIx32", 0x%"PRIx32"}",
  4460. 0, 1);
  4461. err = LFS_ERR_CORRUPT;
  4462. goto cleanup;
  4463. }
  4464. uint16_t major_version = (0xffff & (superblock.d.version >> 16));
  4465. uint16_t minor_version = (0xffff & (superblock.d.version >> 0));
  4466. if ((major_version != LFS1_DISK_VERSION_MAJOR ||
  4467. minor_version > LFS1_DISK_VERSION_MINOR)) {
  4468. LFS_ERROR("Invalid version v%d.%d", major_version, minor_version);
  4469. err = LFS_ERR_INVAL;
  4470. goto cleanup;
  4471. }
  4472. return 0;
  4473. }
  4474. cleanup:
  4475. lfs_deinit(lfs);
  4476. return err;
  4477. }
  4478. static int lfs1_unmount(lfs_t *lfs) {
  4479. return lfs_deinit(lfs);
  4480. }
  4481. /// v1 migration ///
  4482. static int lfs_rawmigrate(lfs_t *lfs, const struct lfs_config *cfg) {
  4483. struct lfs1 lfs1;
  4484. int err = lfs1_mount(lfs, &lfs1, cfg);
  4485. if (err) {
  4486. return err;
  4487. }
  4488. {
  4489. // iterate through each directory, copying over entries
  4490. // into new directory
  4491. lfs1_dir_t dir1;
  4492. lfs_mdir_t dir2;
  4493. dir1.d.tail[0] = lfs->lfs1->root[0];
  4494. dir1.d.tail[1] = lfs->lfs1->root[1];
  4495. while (!lfs_pair_isnull(dir1.d.tail)) {
  4496. // iterate old dir
  4497. err = lfs1_dir_fetch(lfs, &dir1, dir1.d.tail);
  4498. if (err) {
  4499. goto cleanup;
  4500. }
  4501. // create new dir and bind as temporary pretend root
  4502. err = lfs_dir_alloc(lfs, &dir2);
  4503. if (err) {
  4504. goto cleanup;
  4505. }
  4506. dir2.rev = dir1.d.rev;
  4507. dir1.head[0] = dir1.pair[0];
  4508. dir1.head[1] = dir1.pair[1];
  4509. lfs->root[0] = dir2.pair[0];
  4510. lfs->root[1] = dir2.pair[1];
  4511. err = lfs_dir_commit(lfs, &dir2, NULL, 0);
  4512. if (err) {
  4513. goto cleanup;
  4514. }
  4515. while (true) {
  4516. lfs1_entry_t entry1;
  4517. err = lfs1_dir_next(lfs, &dir1, &entry1);
  4518. if (err && err != LFS_ERR_NOENT) {
  4519. goto cleanup;
  4520. }
  4521. if (err == LFS_ERR_NOENT) {
  4522. break;
  4523. }
  4524. // check that entry has not been moved
  4525. if (entry1.d.type & 0x80) {
  4526. int moved = lfs1_moved(lfs, &entry1.d.u);
  4527. if (moved < 0) {
  4528. err = moved;
  4529. goto cleanup;
  4530. }
  4531. if (moved) {
  4532. continue;
  4533. }
  4534. entry1.d.type &= ~0x80;
  4535. }
  4536. // also fetch name
  4537. char name[LFS_NAME_MAX+1];
  4538. memset(name, 0, sizeof(name));
  4539. err = lfs1_bd_read(lfs, dir1.pair[0],
  4540. entry1.off + 4+entry1.d.elen+entry1.d.alen,
  4541. name, entry1.d.nlen);
  4542. if (err) {
  4543. goto cleanup;
  4544. }
  4545. bool isdir = (entry1.d.type == LFS1_TYPE_DIR);
  4546. // create entry in new dir
  4547. err = lfs_dir_fetch(lfs, &dir2, lfs->root);
  4548. if (err) {
  4549. goto cleanup;
  4550. }
  4551. uint16_t id;
  4552. err = lfs_dir_find(lfs, &dir2, &(const char*){name}, &id);
  4553. if (!(err == LFS_ERR_NOENT && id != 0x3ff)) {
  4554. err = (err < 0) ? err : LFS_ERR_EXIST;
  4555. goto cleanup;
  4556. }
  4557. lfs1_entry_tole32(&entry1.d);
  4558. err = lfs_dir_commit(lfs, &dir2, LFS_MKATTRS(
  4559. {LFS_MKTAG(LFS_TYPE_CREATE, id, 0), NULL},
  4560. {LFS_MKTAG_IF_ELSE(isdir,
  4561. LFS_TYPE_DIR, id, entry1.d.nlen,
  4562. LFS_TYPE_REG, id, entry1.d.nlen),
  4563. name},
  4564. {LFS_MKTAG_IF_ELSE(isdir,
  4565. LFS_TYPE_DIRSTRUCT, id, sizeof(entry1.d.u),
  4566. LFS_TYPE_CTZSTRUCT, id, sizeof(entry1.d.u)),
  4567. &entry1.d.u}));
  4568. lfs1_entry_fromle32(&entry1.d);
  4569. if (err) {
  4570. goto cleanup;
  4571. }
  4572. }
  4573. if (!lfs_pair_isnull(dir1.d.tail)) {
  4574. // find last block and update tail to thread into fs
  4575. err = lfs_dir_fetch(lfs, &dir2, lfs->root);
  4576. if (err) {
  4577. goto cleanup;
  4578. }
  4579. while (dir2.split) {
  4580. err = lfs_dir_fetch(lfs, &dir2, dir2.tail);
  4581. if (err) {
  4582. goto cleanup;
  4583. }
  4584. }
  4585. lfs_pair_tole32(dir2.pair);
  4586. err = lfs_dir_commit(lfs, &dir2, LFS_MKATTRS(
  4587. {LFS_MKTAG(LFS_TYPE_SOFTTAIL, 0x3ff, 8), dir1.d.tail}));
  4588. lfs_pair_fromle32(dir2.pair);
  4589. if (err) {
  4590. goto cleanup;
  4591. }
  4592. }
  4593. // Copy over first block to thread into fs. Unfortunately
  4594. // if this fails there is not much we can do.
  4595. LFS_DEBUG("Migrating {0x%"PRIx32", 0x%"PRIx32"} "
  4596. "-> {0x%"PRIx32", 0x%"PRIx32"}",
  4597. lfs->root[0], lfs->root[1], dir1.head[0], dir1.head[1]);
  4598. err = lfs_bd_erase(lfs, dir1.head[1]);
  4599. if (err) {
  4600. goto cleanup;
  4601. }
  4602. err = lfs_dir_fetch(lfs, &dir2, lfs->root);
  4603. if (err) {
  4604. goto cleanup;
  4605. }
  4606. for (lfs_off_t i = 0; i < dir2.off; i++) {
  4607. uint8_t dat;
  4608. err = lfs_bd_read(lfs,
  4609. NULL, &lfs->rcache, dir2.off,
  4610. dir2.pair[0], i, &dat, 1);
  4611. if (err) {
  4612. goto cleanup;
  4613. }
  4614. err = lfs_bd_prog(lfs,
  4615. &lfs->pcache, &lfs->rcache, true,
  4616. dir1.head[1], i, &dat, 1);
  4617. if (err) {
  4618. goto cleanup;
  4619. }
  4620. }
  4621. err = lfs_bd_flush(lfs, &lfs->pcache, &lfs->rcache, true);
  4622. if (err) {
  4623. goto cleanup;
  4624. }
  4625. }
  4626. // Create new superblock. This marks a successful migration!
  4627. err = lfs1_dir_fetch(lfs, &dir1, (const lfs_block_t[2]){0, 1});
  4628. if (err) {
  4629. goto cleanup;
  4630. }
  4631. dir2.pair[0] = dir1.pair[0];
  4632. dir2.pair[1] = dir1.pair[1];
  4633. dir2.rev = dir1.d.rev;
  4634. dir2.off = sizeof(dir2.rev);
  4635. dir2.etag = 0xffffffff;
  4636. dir2.count = 0;
  4637. dir2.tail[0] = lfs->lfs1->root[0];
  4638. dir2.tail[1] = lfs->lfs1->root[1];
  4639. dir2.erased = false;
  4640. dir2.split = true;
  4641. lfs_superblock_t superblock = {
  4642. .version = LFS_DISK_VERSION,
  4643. .block_size = lfs->cfg->block_size,
  4644. .block_count = lfs->cfg->block_count,
  4645. .name_max = lfs->name_max,
  4646. .file_max = lfs->file_max,
  4647. .attr_max = lfs->attr_max,
  4648. };
  4649. lfs_superblock_tole32(&superblock);
  4650. err = lfs_dir_commit(lfs, &dir2, LFS_MKATTRS(
  4651. {LFS_MKTAG(LFS_TYPE_CREATE, 0, 0), NULL},
  4652. {LFS_MKTAG(LFS_TYPE_SUPERBLOCK, 0, 8), "littlefs"},
  4653. {LFS_MKTAG(LFS_TYPE_INLINESTRUCT, 0, sizeof(superblock)),
  4654. &superblock}));
  4655. if (err) {
  4656. goto cleanup;
  4657. }
  4658. // sanity check that fetch works
  4659. err = lfs_dir_fetch(lfs, &dir2, (const lfs_block_t[2]){0, 1});
  4660. if (err) {
  4661. goto cleanup;
  4662. }
  4663. // force compaction to prevent accidentally mounting v1
  4664. dir2.erased = false;
  4665. err = lfs_dir_commit(lfs, &dir2, NULL, 0);
  4666. if (err) {
  4667. goto cleanup;
  4668. }
  4669. }
  4670. cleanup:
  4671. lfs1_unmount(lfs);
  4672. return err;
  4673. }
  4674. #endif
  4675. /// Public API wrappers ///
  4676. // Here we can add tracing/thread safety easily
  4677. // Thread-safe wrappers if enabled
  4678. #ifdef LFS_THREADSAFE
  4679. #define LFS_LOCK(cfg) cfg->lock(cfg)
  4680. #define LFS_UNLOCK(cfg) cfg->unlock(cfg)
  4681. #else
  4682. #define LFS_LOCK(cfg) ((void)cfg, 0)
  4683. #define LFS_UNLOCK(cfg) ((void)cfg)
  4684. #endif
  4685. // Public API
  4686. #ifndef LFS_READONLY
  4687. int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) {
  4688. int err = LFS_LOCK(cfg);
  4689. if (err) {
  4690. return err;
  4691. }
  4692. LFS_TRACE("lfs_format(%p, %p {.context=%p, "
  4693. ".read=%p, .prog=%p, .erase=%p, .sync=%p, "
  4694. ".read_size=%"PRIu32", .prog_size=%"PRIu32", "
  4695. ".block_size=%"PRIu32", .block_count=%"PRIu32", "
  4696. ".block_cycles=%"PRIu32", .cache_size=%"PRIu32", "
  4697. ".lookahead_size=%"PRIu32", .read_buffer=%p, "
  4698. ".prog_buffer=%p, .lookahead_buffer=%p, "
  4699. ".name_max=%"PRIu32", .file_max=%"PRIu32", "
  4700. ".attr_max=%"PRIu32"})",
  4701. (void*)lfs, (void*)cfg, cfg->context,
  4702. (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog,
  4703. (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync,
  4704. cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count,
  4705. cfg->block_cycles, cfg->cache_size, cfg->lookahead_size,
  4706. cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer,
  4707. cfg->name_max, cfg->file_max, cfg->attr_max);
  4708. err = lfs_rawformat(lfs, cfg);
  4709. LFS_TRACE("lfs_format -> %d", err);
  4710. LFS_UNLOCK(cfg);
  4711. return err;
  4712. }
  4713. #endif
  4714. int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
  4715. int err = LFS_LOCK(cfg);
  4716. if (err) {
  4717. return err;
  4718. }
  4719. LFS_TRACE("lfs_mount(%p, %p {.context=%p, "
  4720. ".read=%p, .prog=%p, .erase=%p, .sync=%p, "
  4721. ".read_size=%"PRIu32", .prog_size=%"PRIu32", "
  4722. ".block_size=%"PRIu32", .block_count=%"PRIu32", "
  4723. ".block_cycles=%"PRIu32", .cache_size=%"PRIu32", "
  4724. ".lookahead_size=%"PRIu32", .read_buffer=%p, "
  4725. ".prog_buffer=%p, .lookahead_buffer=%p, "
  4726. ".name_max=%"PRIu32", .file_max=%"PRIu32", "
  4727. ".attr_max=%"PRIu32"})",
  4728. (void*)lfs, (void*)cfg, cfg->context,
  4729. (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog,
  4730. (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync,
  4731. cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count,
  4732. cfg->block_cycles, cfg->cache_size, cfg->lookahead_size,
  4733. cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer,
  4734. cfg->name_max, cfg->file_max, cfg->attr_max);
  4735. err = lfs_rawmount(lfs, cfg);
  4736. LFS_TRACE("lfs_mount -> %d", err);
  4737. LFS_UNLOCK(cfg);
  4738. return err;
  4739. }
  4740. int lfs_unmount(lfs_t *lfs) {
  4741. int err = LFS_LOCK(lfs->cfg);
  4742. if (err) {
  4743. return err;
  4744. }
  4745. LFS_TRACE("lfs_unmount(%p)", (void*)lfs);
  4746. err = lfs_rawunmount(lfs);
  4747. LFS_TRACE("lfs_unmount -> %d", err);
  4748. LFS_UNLOCK(lfs->cfg);
  4749. return err;
  4750. }
  4751. #ifndef LFS_READONLY
  4752. int lfs_remove(lfs_t *lfs, const char *path) {
  4753. int err = LFS_LOCK(lfs->cfg);
  4754. if (err) {
  4755. return err;
  4756. }
  4757. LFS_TRACE("lfs_remove(%p, \"%s\")", (void*)lfs, path);
  4758. err = lfs_rawremove(lfs, path);
  4759. LFS_TRACE("lfs_remove -> %d", err);
  4760. LFS_UNLOCK(lfs->cfg);
  4761. return err;
  4762. }
  4763. #endif
  4764. #ifndef LFS_READONLY
  4765. int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
  4766. int err = LFS_LOCK(lfs->cfg);
  4767. if (err) {
  4768. return err;
  4769. }
  4770. LFS_TRACE("lfs_rename(%p, \"%s\", \"%s\")", (void*)lfs, oldpath, newpath);
  4771. err = lfs_rawrename(lfs, oldpath, newpath);
  4772. LFS_TRACE("lfs_rename -> %d", err);
  4773. LFS_UNLOCK(lfs->cfg);
  4774. return err;
  4775. }
  4776. #endif
  4777. int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) {
  4778. int err = LFS_LOCK(lfs->cfg);
  4779. if (err) {
  4780. return err;
  4781. }
  4782. LFS_TRACE("lfs_stat(%p, \"%s\", %p)", (void*)lfs, path, (void*)info);
  4783. err = lfs_rawstat(lfs, path, info);
  4784. LFS_TRACE("lfs_stat -> %d", err);
  4785. LFS_UNLOCK(lfs->cfg);
  4786. return err;
  4787. }
  4788. lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path,
  4789. uint8_t type, void *buffer, lfs_size_t size) {
  4790. int err = LFS_LOCK(lfs->cfg);
  4791. if (err) {
  4792. return err;
  4793. }
  4794. LFS_TRACE("lfs_getattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")",
  4795. (void*)lfs, path, type, buffer, size);
  4796. lfs_ssize_t res = lfs_rawgetattr(lfs, path, type, buffer, size);
  4797. LFS_TRACE("lfs_getattr -> %"PRId32, res);
  4798. LFS_UNLOCK(lfs->cfg);
  4799. return res;
  4800. }
  4801. #ifndef LFS_READONLY
  4802. int lfs_setattr(lfs_t *lfs, const char *path,
  4803. uint8_t type, const void *buffer, lfs_size_t size) {
  4804. int err = LFS_LOCK(lfs->cfg);
  4805. if (err) {
  4806. return err;
  4807. }
  4808. LFS_TRACE("lfs_setattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")",
  4809. (void*)lfs, path, type, buffer, size);
  4810. err = lfs_rawsetattr(lfs, path, type, buffer, size);
  4811. LFS_TRACE("lfs_setattr -> %d", err);
  4812. LFS_UNLOCK(lfs->cfg);
  4813. return err;
  4814. }
  4815. #endif
  4816. #ifndef LFS_READONLY
  4817. int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type) {
  4818. int err = LFS_LOCK(lfs->cfg);
  4819. if (err) {
  4820. return err;
  4821. }
  4822. LFS_TRACE("lfs_removeattr(%p, \"%s\", %"PRIu8")", (void*)lfs, path, type);
  4823. err = lfs_rawremoveattr(lfs, path, type);
  4824. LFS_TRACE("lfs_removeattr -> %d", err);
  4825. LFS_UNLOCK(lfs->cfg);
  4826. return err;
  4827. }
  4828. #endif
  4829. #ifndef LFS_NO_MALLOC
  4830. int lfs_file_open(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) {
  4831. int err = LFS_LOCK(lfs->cfg);
  4832. if (err) {
  4833. return err;
  4834. }
  4835. LFS_TRACE("lfs_file_open(%p, %p, \"%s\", %x)",
  4836. (void*)lfs, (void*)file, path, flags);
  4837. LFS_ASSERT(!lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
  4838. err = lfs_file_rawopen(lfs, file, path, flags);
  4839. LFS_TRACE("lfs_file_open -> %d", err);
  4840. LFS_UNLOCK(lfs->cfg);
  4841. return err;
  4842. }
  4843. #endif
  4844. int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file,
  4845. const char *path, int flags,
  4846. const struct lfs_file_config *cfg) {
  4847. int err = LFS_LOCK(lfs->cfg);
  4848. if (err) {
  4849. return err;
  4850. }
  4851. LFS_TRACE("lfs_file_opencfg(%p, %p, \"%s\", %x, %p {"
  4852. ".buffer=%p, .attrs=%p, .attr_count=%"PRIu32"})",
  4853. (void*)lfs, (void*)file, path, flags,
  4854. (void*)cfg, cfg->buffer, (void*)cfg->attrs, cfg->attr_count);
  4855. LFS_ASSERT(!lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
  4856. err = lfs_file_rawopencfg(lfs, file, path, flags, cfg);
  4857. LFS_TRACE("lfs_file_opencfg -> %d", err);
  4858. LFS_UNLOCK(lfs->cfg);
  4859. return err;
  4860. }
  4861. int lfs_file_close(lfs_t *lfs, lfs_file_t *file) {
  4862. int err = LFS_LOCK(lfs->cfg);
  4863. if (err) {
  4864. return err;
  4865. }
  4866. LFS_TRACE("lfs_file_close(%p, %p)", (void*)lfs, (void*)file);
  4867. LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
  4868. err = lfs_file_rawclose(lfs, file);
  4869. LFS_TRACE("lfs_file_close -> %d", err);
  4870. LFS_UNLOCK(lfs->cfg);
  4871. return err;
  4872. }
  4873. #ifndef LFS_READONLY
  4874. int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) {
  4875. int err = LFS_LOCK(lfs->cfg);
  4876. if (err) {
  4877. return err;
  4878. }
  4879. LFS_TRACE("lfs_file_sync(%p, %p)", (void*)lfs, (void*)file);
  4880. LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
  4881. err = lfs_file_rawsync(lfs, file);
  4882. LFS_TRACE("lfs_file_sync -> %d", err);
  4883. LFS_UNLOCK(lfs->cfg);
  4884. return err;
  4885. }
  4886. #endif
  4887. lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
  4888. void *buffer, lfs_size_t size) {
  4889. int err = LFS_LOCK(lfs->cfg);
  4890. if (err) {
  4891. return err;
  4892. }
  4893. LFS_TRACE("lfs_file_read(%p, %p, %p, %"PRIu32")",
  4894. (void*)lfs, (void*)file, buffer, size);
  4895. LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
  4896. lfs_ssize_t res = lfs_file_rawread(lfs, file, buffer, size);
  4897. LFS_TRACE("lfs_file_read -> %"PRId32, res);
  4898. LFS_UNLOCK(lfs->cfg);
  4899. return res;
  4900. }
  4901. #ifndef LFS_READONLY
  4902. lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
  4903. const void *buffer, lfs_size_t size) {
  4904. int err = LFS_LOCK(lfs->cfg);
  4905. if (err) {
  4906. return err;
  4907. }
  4908. LFS_TRACE("lfs_file_write(%p, %p, %p, %"PRIu32")",
  4909. (void*)lfs, (void*)file, buffer, size);
  4910. LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
  4911. lfs_ssize_t res = lfs_file_rawwrite(lfs, file, buffer, size);
  4912. LFS_TRACE("lfs_file_write -> %"PRId32, res);
  4913. LFS_UNLOCK(lfs->cfg);
  4914. return res;
  4915. }
  4916. #endif
  4917. lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file,
  4918. lfs_soff_t off, int whence) {
  4919. int err = LFS_LOCK(lfs->cfg);
  4920. if (err) {
  4921. return err;
  4922. }
  4923. LFS_TRACE("lfs_file_seek(%p, %p, %"PRId32", %d)",
  4924. (void*)lfs, (void*)file, off, whence);
  4925. LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
  4926. lfs_soff_t res = lfs_file_rawseek(lfs, file, off, whence);
  4927. LFS_TRACE("lfs_file_seek -> %"PRId32, res);
  4928. LFS_UNLOCK(lfs->cfg);
  4929. return res;
  4930. }
  4931. #ifndef LFS_READONLY
  4932. int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) {
  4933. int err = LFS_LOCK(lfs->cfg);
  4934. if (err) {
  4935. return err;
  4936. }
  4937. LFS_TRACE("lfs_file_truncate(%p, %p, %"PRIu32")",
  4938. (void*)lfs, (void*)file, size);
  4939. LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
  4940. err = lfs_file_rawtruncate(lfs, file, size);
  4941. LFS_TRACE("lfs_file_truncate -> %d", err);
  4942. LFS_UNLOCK(lfs->cfg);
  4943. return err;
  4944. }
  4945. #endif
  4946. lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file) {
  4947. int err = LFS_LOCK(lfs->cfg);
  4948. if (err) {
  4949. return err;
  4950. }
  4951. LFS_TRACE("lfs_file_tell(%p, %p)", (void*)lfs, (void*)file);
  4952. LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
  4953. lfs_soff_t res = lfs_file_rawtell(lfs, file);
  4954. LFS_TRACE("lfs_file_tell -> %"PRId32, res);
  4955. LFS_UNLOCK(lfs->cfg);
  4956. return res;
  4957. }
  4958. int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) {
  4959. int err = LFS_LOCK(lfs->cfg);
  4960. if (err) {
  4961. return err;
  4962. }
  4963. LFS_TRACE("lfs_file_rewind(%p, %p)", (void*)lfs, (void*)file);
  4964. err = lfs_file_rawrewind(lfs, file);
  4965. LFS_TRACE("lfs_file_rewind -> %d", err);
  4966. LFS_UNLOCK(lfs->cfg);
  4967. return err;
  4968. }
  4969. lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) {
  4970. int err = LFS_LOCK(lfs->cfg);
  4971. if (err) {
  4972. return err;
  4973. }
  4974. LFS_TRACE("lfs_file_size(%p, %p)", (void*)lfs, (void*)file);
  4975. LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
  4976. lfs_soff_t res = lfs_file_rawsize(lfs, file);
  4977. LFS_TRACE("lfs_file_size -> %"PRId32, res);
  4978. LFS_UNLOCK(lfs->cfg);
  4979. return res;
  4980. }
  4981. #ifndef LFS_READONLY
  4982. int lfs_mkdir(lfs_t *lfs, const char *path) {
  4983. int err = LFS_LOCK(lfs->cfg);
  4984. if (err) {
  4985. return err;
  4986. }
  4987. LFS_TRACE("lfs_mkdir(%p, \"%s\")", (void*)lfs, path);
  4988. err = lfs_rawmkdir(lfs, path);
  4989. LFS_TRACE("lfs_mkdir -> %d", err);
  4990. LFS_UNLOCK(lfs->cfg);
  4991. return err;
  4992. }
  4993. #endif
  4994. int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) {
  4995. int err = LFS_LOCK(lfs->cfg);
  4996. if (err) {
  4997. return err;
  4998. }
  4999. LFS_TRACE("lfs_dir_open(%p, %p, \"%s\")", (void*)lfs, (void*)dir, path);
  5000. LFS_ASSERT(!lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)dir));
  5001. err = lfs_dir_rawopen(lfs, dir, path);
  5002. LFS_TRACE("lfs_dir_open -> %d", err);
  5003. LFS_UNLOCK(lfs->cfg);
  5004. return err;
  5005. }
  5006. int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir) {
  5007. int err = LFS_LOCK(lfs->cfg);
  5008. if (err) {
  5009. return err;
  5010. }
  5011. LFS_TRACE("lfs_dir_close(%p, %p)", (void*)lfs, (void*)dir);
  5012. err = lfs_dir_rawclose(lfs, dir);
  5013. LFS_TRACE("lfs_dir_close -> %d", err);
  5014. LFS_UNLOCK(lfs->cfg);
  5015. return err;
  5016. }
  5017. int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) {
  5018. int err = LFS_LOCK(lfs->cfg);
  5019. if (err) {
  5020. return err;
  5021. }
  5022. LFS_TRACE("lfs_dir_read(%p, %p, %p)",
  5023. (void*)lfs, (void*)dir, (void*)info);
  5024. err = lfs_dir_rawread(lfs, dir, info);
  5025. LFS_TRACE("lfs_dir_read -> %d", err);
  5026. LFS_UNLOCK(lfs->cfg);
  5027. return err;
  5028. }
  5029. int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) {
  5030. int err = LFS_LOCK(lfs->cfg);
  5031. if (err) {
  5032. return err;
  5033. }
  5034. LFS_TRACE("lfs_dir_seek(%p, %p, %"PRIu32")",
  5035. (void*)lfs, (void*)dir, off);
  5036. err = lfs_dir_rawseek(lfs, dir, off);
  5037. LFS_TRACE("lfs_dir_seek -> %d", err);
  5038. LFS_UNLOCK(lfs->cfg);
  5039. return err;
  5040. }
  5041. lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir) {
  5042. int err = LFS_LOCK(lfs->cfg);
  5043. if (err) {
  5044. return err;
  5045. }
  5046. LFS_TRACE("lfs_dir_tell(%p, %p)", (void*)lfs, (void*)dir);
  5047. lfs_soff_t res = lfs_dir_rawtell(lfs, dir);
  5048. LFS_TRACE("lfs_dir_tell -> %"PRId32, res);
  5049. LFS_UNLOCK(lfs->cfg);
  5050. return res;
  5051. }
  5052. int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir) {
  5053. int err = LFS_LOCK(lfs->cfg);
  5054. if (err) {
  5055. return err;
  5056. }
  5057. LFS_TRACE("lfs_dir_rewind(%p, %p)", (void*)lfs, (void*)dir);
  5058. err = lfs_dir_rawrewind(lfs, dir);
  5059. LFS_TRACE("lfs_dir_rewind -> %d", err);
  5060. LFS_UNLOCK(lfs->cfg);
  5061. return err;
  5062. }
  5063. lfs_ssize_t lfs_fs_size(lfs_t *lfs) {
  5064. int err = LFS_LOCK(lfs->cfg);
  5065. if (err) {
  5066. return err;
  5067. }
  5068. LFS_TRACE("lfs_fs_size(%p)", (void*)lfs);
  5069. lfs_ssize_t res = lfs_fs_rawsize(lfs);
  5070. LFS_TRACE("lfs_fs_size -> %"PRId32, res);
  5071. LFS_UNLOCK(lfs->cfg);
  5072. return res;
  5073. }
  5074. int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void *, lfs_block_t), void *data) {
  5075. int err = LFS_LOCK(lfs->cfg);
  5076. if (err) {
  5077. return err;
  5078. }
  5079. LFS_TRACE("lfs_fs_traverse(%p, %p, %p)",
  5080. (void*)lfs, (void*)(uintptr_t)cb, data);
  5081. err = lfs_fs_rawtraverse(lfs, cb, data, true);
  5082. LFS_TRACE("lfs_fs_traverse -> %d", err);
  5083. LFS_UNLOCK(lfs->cfg);
  5084. return err;
  5085. }
  5086. #ifdef LFS_MIGRATE
  5087. int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg) {
  5088. int err = LFS_LOCK(cfg);
  5089. if (err) {
  5090. return err;
  5091. }
  5092. LFS_TRACE("lfs_migrate(%p, %p {.context=%p, "
  5093. ".read=%p, .prog=%p, .erase=%p, .sync=%p, "
  5094. ".read_size=%"PRIu32", .prog_size=%"PRIu32", "
  5095. ".block_size=%"PRIu32", .block_count=%"PRIu32", "
  5096. ".block_cycles=%"PRIu32", .cache_size=%"PRIu32", "
  5097. ".lookahead_size=%"PRIu32", .read_buffer=%p, "
  5098. ".prog_buffer=%p, .lookahead_buffer=%p, "
  5099. ".name_max=%"PRIu32", .file_max=%"PRIu32", "
  5100. ".attr_max=%"PRIu32"})",
  5101. (void*)lfs, (void*)cfg, cfg->context,
  5102. (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog,
  5103. (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync,
  5104. cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count,
  5105. cfg->block_cycles, cfg->cache_size, cfg->lookahead_size,
  5106. cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer,
  5107. cfg->name_max, cfg->file_max, cfg->attr_max);
  5108. err = lfs_rawmigrate(lfs, cfg);
  5109. LFS_TRACE("lfs_migrate -> %d", err);
  5110. LFS_UNLOCK(cfg);
  5111. return err;
  5112. }
  5113. #endif