lfs.c 156 KB

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