lfs.c 192 KB

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