lfs.c 159 KB

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