lfs.c 192 KB

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