lfs.c 192 KB

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