lfs.c 192 KB

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