lfs.c 156 KB

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