lfs.c 156 KB

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