lfs.c 185 KB

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