lfs.c 191 KB

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