lfs.c 192 KB

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