lfs.c 156 KB

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