lfs.c 185 KB

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