lfs.c 159 KB

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