lfs.c 160 KB

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