lfs.c 192 KB

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