lfs.c 192 KB

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