lfs.c 185 KB

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