Professional Documents
Culture Documents
Page1/25
1 Purpose
Thepurposeofthisdocumentistogiveareasonableexplanationofmostofthecoremechanismsthatmakeyaffswork.As ofthetimeofwritingthis,theyaffs2codebaseincludesapproximately19,000linesofcodemakingaverydetailed discussionimpractical. Thisdocumentshouldserveasafirstportofcallforunderstandingyaffs.Furtherdetailedquestionsanddiscussionsare welcomedontheyaffsdiscussionlist. Thisdocumentisupdatedperiodically.Yaffsmechanismdesignchangesfromtimetotimeandthisdocumentwillnot alwaysreflectthemostuptodatecode.
2 WhatIsYAFFS?
YAFFSstandsforYetAnotherFlashFileSystem,atermcoinedbyCharlesManningin2001whensuggestingthatthe alreadyclutteredflashfilesystemspacecoulddowithyetanotherofferingthistimeaflashfilesystemdesignedfromthe grounduptoworkwithNANDflash. ThefirstreleaseofYAFFSwasdevelopedduringtheendof2001andthebeginningof2002.Bymid2002,YAFFSwas beingtrialledinvariousproducts.InMay2002,YAFFSwasannouncedandafewmoreprojectsstartedplayingwithit.In June,portingtootherOSsstarted.InSeptember2002,YAFFSwasannouncedonlinuxdevices.comwhichstartedafar wideruptakeofYAFFS. YAFFS1,theoriginalversionofYAFFS,supported512bytepageNANDdeviceswithaSmartMedialikememorylayout. YAFFS2isafollowuptoYAFFS1extendingYAFFS1tosupportlargeranddifferentdeviceswithdifferentconstraints. YAFFSishighlyportableandhasbeenusedinmanydifferentproductsandapplicationsasvariedassewingmachines, pointofsale,phonesandaerospace.YAFFShasbeenusedwithmultipledifferentoperatingsystems.Nativesupportis availableforLinux,WindowsCEandeCOSwhileyaffsDirectInterfaceprovidesalayerthatcanbeusedinother applicationssuchasRTOSs.YAFFShasbeenusedwithmultipledifferentCPUsandcompilers. YAFFShasalsobeenusedasaNORfilesystemandevenasaRAMfilesystem.
3 Designandcodingstrategies
YAFFSisdesignedtoworkinmultipleenvironmentswhichdrivestheneedforportability.Portabilityalsoimprovesthe debuggingandtestingsinceitiseasiertodevelopanddebugcodeinanapplicationenvironmentthanwithinanOSkernel. ThisgenerallyallowsYAFFStoprogressfasterwithlessprogrammingresources.Primarystrategiesthatimprove portabilityinclude:
Page2/25
4 Terminology:YAFFSvsYAFFS1vsYAFFS2
BeforeweembarkonourquesttounderstandwhatYAFFSis,aterminologyexplanationisinorder. TherearetwoversionsofYAFFScode:theoriginalyaffscodebaseandthecurrentyaffs2codebase.Theyaffs2codebase supportsfunctionalityprovidedbytheyaffscodebaseaswellasbeingextendedwithnewfunctionalitytoprovideextra modesofoperation,mostimportantlythoserequiredtoworkwithlargerandmoremodernNANDparts.Theyaffs2code basesupportstheyaffs1modeofoperationthroughabackwardcompatibilitymode. Unlessexplicitlymentioned,thistextreferstotheyaffs2codebase. ThistextusesthreetermsYAFFS,YAFFS1andYAFFS2dependingonvariousmodesofoperation.
YAFFS1isasimplermodeofoperationthatusesdeletionmarkerstotrackstate. YAFFS2isamorecomplexmodeofoperatingthatwasdevelopedtosupportlargerflashtypesthatcannotuseddeletion
markers.
YAFFSmeansoperationscommontoboth.
5 Objects
InYAFFSspeak,anObjectisanythingthatisstoredinthefilesystem.Theseare:
Page3/25
Specialobjects(pipes,devicesetc).
6 TheYAFFSNANDModel
ThememoryinNANDflashisarrangedinpages.Apageistheunitofallocationandprogramming.InYAFFS,theunitof allocationisthechunk.TypicallyachunkwillbethesameasaNANDpage,butthereisflexibilitytousechunkswhich maptomultiplepages(eg.AsystemmayhavetwoNANDchipsinparallelrequiring2x512=1024bytechunks).This distinctiongivesalotofflexibilityinhowthesystemcanbeconfigured.Inthistextthetermpageandchunkare synonymousunlessstatedotherwise. Many,typically32to128butasmanyasafewhundred,chunksformablock.Ablockistheunitoferasure. NANDflashmaybeshippedwithbadblocksandfurtherblocksmaygobadduringtheoperationofthedevice.Thus, YAFFSisawareofbadblocksandneedstobeabletodetectandmarkbadblocks. NANDflashalsotypicallyrequirestheuseofsomesortoferrordetectionandcorrectioncode(ECC).YAFFScaneither useexistingECClogicorprovideitsown.
7 HowYAFFS1StoresFiles
YAFFS1hasamodifiedlogstructure,whileYAFFS2(seebelow)hasatruelogstructure.Atruelogstructuredfilesystem onlyeverwritessequentially.YAFFS1usesdeletionmarkers,whichbreaksthesequentialwriterule.YAFFS2doesnotuse deletionmarkersandisthusatruelogstructuredfilesystem. Insteadofwritingdatainlocationsspecifictothefiles,thefilesystemdataiswrittenintheformofasequentiallog,The entriesinthelogareallonechunkinsizeandcanholdoneoftwotypesofchunk:
Page4/25
Eachchunkhastagsassociatedwithit.Thetagscomprisethefollowingimportantfields(othersbeingignoredfornow):
Block 1
ChunkId 0
Deletion Live
Comment ObjectHeaderforthisfile(length0)
Nextwewriteafewchunksofdatatothefile.
Block 1 1 1 1
ChunkId 0 1 2 3
Nextweclosethefile.Thiswritersanewobjectheaderforthefile.Noticehowthepreviousobjectheaderisdeleted.
Block 1 1 1 1
ChunkId 0 1 2 3
Page5/25
500
Live
Newobjectheader(lengthn)
Let'snowopenthefileforread/write,overwritepartofthefirstchunkinthefileandclosethefile.Thereplaceddataand objectheaderchunksbecomedeleted.
Block 1 1 1 1 2 2 2
ChunkId 0 1 2 3 0 1 0
Nowlet'sresizethefiletozerobyopeningthefilewithO_TRUNCandclosingthefile.Thiswritesanewobjectheader withlength0andthecontentsareprunedmakingthedatachunksdeleted.
Block 1 1 1 1 2 2 2 2
Chunk ObjectId 0 1 2 3 0 1 2 3 500 500 500 500 500 500 500 500
ChunkId 0 1 2 3 0 1 0 0
Page6/25
Block 1 1 1 1 2 2 2 2 3
ChunkId
Deleted
0 1 2 0 0
Block2nowcontainsonlydeletedchunksandcanbeerasedandreused. Notehowthetagstellus:
whichchunksbelongtowhichobject, thepositionofthechunkswithinafile, whichchunksarecurrent
8 Garbagecollection
Aswehaveshownsofar,whenablockismadeuponlyofdeletedchunks,thatblockcanbeerasedandreused.However, considerwhatwillhappenifwehavemanyblockswhicheachhaveafewcurrentchunksinthem.Clearlywecannoterase anyofthecurrentblocksorwewilldestroydata.Whatweneedtodoiscopytheusefuldatachunksoffablock,deleting theoriginalsandallowingtheblocktobeerasedandreused.Thisprocessisreferredtoasgarbagecollection. TheYAFFSgarbagecollectionprocessworksasfollows: 1. 2. Findablockworthcollecting(accordingthetheheuristicsbelow).Ifnonefound,thenexit. Iteratethroughthechunksintheblock.Ifthechunkisinusethencreateanewcopyanddeletetheoriginal.Fixup theRAMdatastructurestoreflectthechange.
Page7/25
Onceallchunksinthisblockhavebeendeleted,theblockcanbeerasedreadyforreuse. Notethatalthoughstep2deletestheoriginalchunkwhenitgetscopiedthereisnotmuchpointinactuallyprogrammingthe deletionmarkerinflashsincewe'reabouttoerasetheblockanyway.Thusyaffsdoesnotprogramthedeletionmarkerin thesecases.Ifpowerislostpartwaythroughtheoperationwecanstilltellthedifferencebetweentheoriginalandthecopy bylookingattheserialnumber(notthatitreallymattersanywaysincethetwohavethesamecontentssoeithercanbe used). Theheuristicstodeterminewhetherablockisworthcollectingareasfollows: 1. 2. Iftherearemanyerasedblocksavailable,thenYAFFSdoesnotneedtoworkhard,butwillinsteadonlyattemptto performgarbagecollectionofblockswithveryfewchunksinuse.Thisistermedpassivegarbagecollection. If,however,thereareveryfewerasedblocks,YAFFSwillworkmuchhardertorecovermorespaceandwill garbagecollectblockswithmorechunksinuse.Thisistermedaggressivegarbagecollection.
OK,thepassive/aggressivenamesareabitsilly,buttheywerecontrivedaroundmidnight...givemeabreak! Ifgarbagecollectionisaggressive,thewholeblockiscollectedinasinglegarbagecollectioncycle.Ifthecollectionis passivethenthenumberofcopiesisreducedthusspreadingtheeffortovermanygarbagecollectioncycles.Thisisdoneto reducegarbagecollectionloadandimproveresponsiveness. Therationalebehindtheaboveheuristicsistodelaygarbagecollectionwhenpossibletoreducetheamountofcollection thatneedstobeperformed,thusincreasingaveragesystemperformance.Yetthereisaconflictinggoaloftryingtospread thegarbagecollectionsothatitdoesnotallhappenatthesamecausingfluctuationsinfilesystemthroughput.These conflictinggoalsmakegarbagetuningquitechallenging. Allflashfilesystemsneedsomesortofgarbagecollectortoreclaimspace.Thegarbagecollectorisoftenadetermining factorintheperformanceoftheflashfilesystem.Manyfilesystemshavetodoasignificantamountofeffortinasingle collectionsweep.YAFFSonlydealswithoneblockatatimewhichconstrainstheamountofeffortinagarbagecollection cycle,thusreducingthestalltime. TheYAFFSgarbagecollectionalgorithmisunderreviewwiththegoalofreducingstalltimeandincreasingthroughput.
9 YAFFS1Serialnumbers
InYAFFS1,eachchunkismarkedwitha2bitserialnumberthatisincrementedeverytimeachunkwiththesametags valueisreplaced(eg.Whenachunkisoverwrittenorwhencopiedbygarbagecollection). Nowlet'ssupposefirstthatwedidnothaveaserialnumber.
Page8/25
Let'sconsiderarenamewhereanobjectheaderchunkmustbereplacedwithanewonewiththenewname.YAFFScould dothefollowing:
Deletetheoldchunk. Writethenewchunk.
Thus,byinspectingtheserialnumberattachedtotheoldandnewtagsthecurrentchunkcanbedeterminedandtheold chunkcanbedeleted.
10 YAFFS2NANDmodel
YAFFS2isanextensionofYAFFSdesignedtofulfillanewsetofobjectivestoworkwithnewerNANDtypesincluding:
SinceYAFFS2performslesswrites,thereispotentialforYAFFS2tohaveimprovedwriteperformance. YAFFS2cannotuseadeletionmarkerbecausethatwouldviolatethezerooverwritesmandateandalternatemechanisms
Page9/25
areprovidedtodeterminewhichchunksarecurrentandwhicharedeleted.Thesemechanismsare:
Explainingthepurposeoftheshrinkheadermarkerisabitconvoluted.Thepurposeofashrinkheadermarkeristoshow thatthisobjectheaderindicatesthatafilehasshrunkeninsize,andtopreventthoseobjectheadersfrombeingdeletedby garbagecollection.Tounderstandthis,itiseasiesttoconsiderwhatwouldhappeniftheshrinkheadermarkersdidnot exist. Forsimplificationwe'llassumenogarbagecollectionhappensduringthispartoftheexercise.Itissafetomakethat assumptionsinceitdoesnotalterthereasoning. Considerafilethathasbeenthroughthefollowing: h=open(foo,O_CREAT|O_RDWR,S_IREAD|S_IWRITE);/*createfile/ write(h,data,51024*1024);/*write5MBofdata/ truncate(h,1024*1024);/*truncateto1MB*/ lseek(h,2*1024*1024,SEEK_SET);/*setthefileaccesspositionto2MB*/ write(h,data,1024*1024);/*write1MBofdata*/ close(h); Thefilewillnowbe3MBinlength,buttherewillbeaholebetween1MBand2MB.AccordingtoPOSIX(andtoaid securityetc)thatholeshouldalwaysreadbackaszeros. YAFFS2wouldrepresentthatinNANDwiththefollowingsequenceofchunks: 1. 2. Objectheaderforfilecreation(filelength=0) 5MBworthofdatachunks(0to5MB)
Page10/25
3. 4. 5.
Atthisstage,onlythethefollowingdatachunksarecurrent:
10.1. YAFFS2Extendedtags
YAFFS2extendsthetagsinobjectheaderswithextrafieldstoimprovemountscanningperformance.Theoperationof extendedtagsisdescribedlaterinthisdocument.
11 BadblockhandlingNANDerrorhandling
NANDflashisdesignedtobeverylowcostandveryhighdensity.Inordertogethighyields,andthuskeepcostsdown, NANDflashistypicallyshippedwithafewbadblocksandsomefurtherblocksareexpectedtogobadduringtheuseofthe device.Thus,anyflashfilesystemwithoutaneffectivebadblockhandlingpolicyisunsuitableforNANDflash. YAFFS1usestheSmartMediastyleofbadblockmarking.Thischecksthesixthbyte(byte5)ofthesparearea.Inagood blockthisshouldbe0xFF.Afactorymarkedbadblockshouldbe0x00.IfYAFFS1determinesthatablockhasgonebadit ismarkedbadwith0x59('Y').UsingadistinctivemarkerallowsYAFFSmarkedbadblockstobedifferentiatedfrom factorymarkedbadblocks. YAFFS2modeisdesignedtosupportawiderrangeofdevicesandsparearealayouts.Thus,YAFFS2doesnotdecide whichbytestomarkbutinsteadcallsdriverfunctionstodeterminewhethertheblockisbad,ortomarkitbad. So,whendoesYAFFSmarkablockbad?YAFFSwillmarkablockbadifareadorwriteoperationfailsorifthreeECC errorsaredetected.Ablockthatismarkedbadisretiredfromuse,thusimprovingtherobustnessofthefilesystem.This policyissuitableforSLCflash.MLCpoliciesareunderdevelopment. Further,NANDflashcellscansometimesbedisturbed(corrupted)byNANDactivityandchargeloss.Theseerrorsare correctedbyerrorcorrectioncodes(ECC)whichmaybeimplementedinhardware,softwaredrivers,orwithinYAFFS
Page11/25
12 RAMstructures
WhileitistheoreticallypossibletousealogstructuredfilesystemwithveryfewRAMstructures,thiswouldprovide reduceperformance.Thus,significantRAMdatastructuresarerequiredtostoreenoughinformationtoprovidesufficiently highperformance. Beforewejumpin,itisagoodideatofirstfigureoutwhatpurposetheRAMstructuresserve.Themainstructuresare definedinyaffs_guts.handtheirmainpurposes,are:
Device/partition:Thisisnamedyaffs_Device.Thisisrequiredtoholdinformationrelatingtoayaffspartitionor mountpoint.Usingthese,ratherthanglobals,allowsYAFFStosimultaneouslysupportmultiplepartitionsand partitionsofdifferenttypes.Infact,almostotherdatastructuresmentionedherearepartof,oraccessedvia,the structure. NANDBlockInformation:Thisisnamedyaffs_BlockInfoandholdsthecurrentstateoftheNANDblocks.Each yaffs_Devicehasanarrayofthese. NANDChunkInformation:Thisisabitfieldattachedtotheyaffs_Devicethatholdsthecurrentinusestateofeach chunkinthesystem.Thereisonebitperchunkinthepartition. Object:Thisisnamedyaffs_Objectandholdsthestateofanobject(seesection3).Thereisoneoftheseforeach objectinthefilesystem,whereanobjectisoneofaregularfile,directory,hardlink,symboliclinkorspeciallink. Therearedifferentvariantsofyaffs_Objectstoreflectthedifferentdatarequiredforthesedifferentobjecttypes. EachobjectisuniquelyidentifiedbyanobjectId. Filestructure:Foreachfileobject,YAFFSholdsatreewhichprovidesthemechanismtofindthedatachunksina file.Thetreeconsistsofnodescalledyaffs_Tnodes(treenodes). Directorystructure:Thedirectorystructureallowsobjectstobefoundbyname.Thedirectorystructureisbuilt fromadoublylinkedlistthatisrootedinthedirectoryandbindstogetherthesiblingobjectswithinadirectory. Objectnumberhashtable:TheobjectnumberhashtableprovidesamechanismtofindanobjectfromitsobjectId. TheobjectIdishashedtoselectahashbucket.Eachhashbuckethasadoublylinkedlistofobjectsthatbelongin thishashbucket,aswellasacountoftheobjectsinthebucket. Cache:YAFFSprovidesaread/writecachethatsignificantlyimprovesperformanceforshortoperations.Thesize ofthecachecanbesetatruntime.
Page12/25
12.1. yaffs_Object
Eachobject(file,directory,...)inthesystemisrepresentedbyayaffs_Object.Theyaffs_Object'smainfunctionistostore mostoftheobjectmetadataandtypespecificinformation.Thismeansthatalmostalltheinformationaboutanobjectcanbe accessedimmediatelywithoutreadingflash. Themetadataincludes,amongstothers:
objectId:Thenumberusedtoidentifytheobject. parent:Apointertotheparentdirectory.Notallobjectshaveaparent.Therootdirectorandspecialdirectoriesfor
unlinkedanddeletedfilesdonothaveparentssothisisNULL.
shortname:Ifthenameisshortenoughtofitinasmallfixedsizedarray(default16charaacters)thenitisstoredhere
otherwiseitmustbefetchedfromflasheachtimeitisrequrested.
type:Typeofobject. permissions,time,overshipandotherattributes
Dependingontheobjecttype,yaffs_Objectalsostores:
atnodetree,fileextentsetcfordatafiles adirectorychainfordirectories anequivalentobjectforhardlinks astringforsymboliclinks
12.2. ObjectIdlookup
ThepurposeoftheobjectIdlookupmechanismistorapidlyaccessanobjectbyitsdeviceandobjectId.Thisisrequired duringgarbagecollection,scanningandsimilaroperations. Eachyaffs_Deviceusesahashtableforthispurpose.Thehashtablehas256buckets(tunable),eachholdingadoubly linkedlistoftheyaffs_Objectsinthatbucket. Thehashtable'shashfunctionisjustmaskingtheleastsignificantbitsoftheobjectId.Thisisaverycheapfunctionthat providesreasonablespread. YAFFSmakesanattempttoassignobjectIdsinawaythatkeepsthenumberofobjectsineachhashbucketquitelow,thus preventinganyonehashentrygettingtoolong.
Page13/25
12.3. Directorystructure
Thepurposeofthedirectorystructureistorapidlyaccessanobjectbyname.Thisisrequiredtoperformfileoperations suchasopening,renamingetc. TheYAFFSdirectorystructureisformedbyatreeofyaffs_ObjectsoftypeYAFFS_OBJECT_TYPE_DIRECTORY.These objectshaveadoublylinkednodeofchildren. Eachyaffs_Objectalsohasadoublylinkedlistnodecalledsiblingwhichchaintogetherthesiblingsinadirectory. EachYAFFSpartitionhasarootdirectory. EachYAFFSpartitionalsohassomespecialpurposefakedirectorieswhicharenotstoredinNANDbutareinstead createdonmounting:
cheapertocomparetehashthancomparethewholenamestring.
Sibling
Children
Sibling
Children
Sibling
data
Sibling
data
Sibling
Children
Children
Page14/25
Theabovedirectorystructurerepresentsthefollowingdirectorytree / /a /b /lost+found /a/c /a/d /a/e Rootdirectory Directorycontainingc,d,e Emptydirectory Emptydirectory Directory Datafile Datafile
12.4. Hardlinks
InPOSIXfilesystems,theobjects(inodesinLinuxterminology,vnodeinBSDspeak)andtheobjectnames(dentryin Linuxterminology)areindependentinthatanobjectcanhavezero,oneormorenames. Havingfileswithzerolinksiscausedbyconstructssuchas:
h=open(foo,O_CREAT|O_RDWR,S_IREAD|S_IWRITE);/*createfilefoo*/ unlink(foo); /*unlinkitsoitcan'tbefoundinthedirectory*/ /*westillhaveahandletothefilesocanstillaccessitviathehandle*/ read(h,buffer,100); write(h,buffer,100); close(h);/*assoonasthehandleisclosedthefileisdeleted*/
Thelinkagebetweenhardlinkpseudoobjectsandtheirequivalentobjectisthroughtwofields:
EachobjecthasahardLinksdoublylinkedlistnodethatlinksthelistofhardlinkstothatobject. Eachhardlinkhasapointertotheequivalentobject.
Page15/25
2 3
#lnab #rma
Createahardlinkbwithequivalentobjecta. Removea.Theobjectmuststillexistunderthenameb
12.5. Symboliclinkandspecialobject
TheseobjecttypesjuststorevaluesthatareneededtoprovideafullPOSIXfilesystem. AsymboliclinkobjectsavesasymboliclinkstringthatisusedtoprovidethePOSIXsymbolicstringmechanism. Aspecialobjectisusedtostoredevicenumbers,pipesandothersimilarspecialobjects.
12.6. Fileobject
Theyaffs_Objectshaveatypeandavariantportionwhichisaunionofdifferentdatarequiredtoholdthestateofdifferent objecttypes. ThefileobjectshavetypeYAFFS_OBJECT_TYPE_FILE,andanassociatedfilevariant.Thisstoresthefollowingmain values:
12.6.1 Tnodetree
EachfilehasaTnodetreetoprovidethemappingfromfilepositiontoactualNANDchunkaddress.topisapointertothe topoftheTnodetree
Page16/25
yaffs_Object
Level2
Level1
Level0
NAND
Note,only4entriesperTnodeareshowntosimplifythediagram.
TheTnodetreeismadeupofTnodes(treenodes)arrangedinlevels,eachofwhichholdseither:
Atlevelsgreaterthan0,aTnodehas8pointerstootherTnodesinthenextleveldown. Atlevel0,aTnodehas16NANDchunkIdswhichidentifythechunk'slocationinRAM.
13 Howvariousmechanismswork
HavinglookedattheYAFFSdatastructures,wenowconsiderhowsomeofthemechanismsinYAFFSoperates.Thisis mosteasilydonebywalkingthroughsomeoperationsthatYAFFSperforms.
Page17/25
NEEDS_SCANNING Duringprescanningithasbeendeterminedthatthisblockhassomethingonitandneeds scanning. SCANNING EMPTY ALLOCATING FULL DIRTY Thisblockiscurrentlybeingscanned. Thisblockhasnothinginit(iserased)andmaybeusedforallocation.Theblockcangettothis statebybeingerased. Thisistheblockcurrentlybeingusedforallocationbythechunkallocator. Allthechunksinthisblockhavebeenallocatedandatleastonechunkcontainsusefulinformation thathasnotyetbeendeleted. Allthechunksinthisblockhavebeenallocatedandallhavebeendeleted.Theblockmighthave beenintheFULLorCOLLECTINGstatebeforethis.Thisblockcannowbeerasedandreturned toEMPTYstate. Thisblockhascheckpointdatainit.Whenthecheckpointisinvalidatedthisblockwillbeerased andreturnedtotheEMPTYstate. Thisblockisbeinggarbagecollected.Assoonasalllivedatahasbeeninspectedthisblock becomesDIRTY. Thisblockhasbeenretiredorwasmarkedbad.Thisisaterminalstate.
Page18/25
UNKNOWN
Regularruntimestates Prescanning Scanning
SCANNING
FULL
ALLOCATING
CHECKPOINT
Checkpointing
DIRTY COLLECTING
Garbagecollection
13.1.2 BlockandChunkAllocation
Anavailablechunkmustbeallocatedbeforeitcanbewritten.Thechunkallocationmechanismisprovidedby yaffs_AllocateChunk()andisverysimple.Eachpartitionhasacurrentblockthatitisallocatingfrom.Thisistermedthe allocationblock.Chunksareallocatedsequentiallyfromtheallocationblock.Asthechunksareallocated,blockandchunk managementinformationisupdated,includingtheblock'spagesInUsecountandthechunkusebitmap. Whentheblockisfullyallocated,anotheremptyblockisselectedtobecometheallocationblock.Theblocksareselected bysearchingupwardsfromthepreviousallocationblock.
Page19/25
13.1.3 Awordaboutwearleveling
Sinceflashmemoryblockscanonlyendurealimitednumberofwritesanderases(termedtheflashendurance),aflashfile systemneedstoensurethatthesamefewblocksarenotoverwrittencontinuouslycausingthoseblockstowearoutwhilethe otherblocksarehardlyusedatall..Insteaditisimportantthatthefilesystemspreadsoutthewear. ThisisextremelyimportantinafilesystemsuchasFATwhichusesalimitedsetoflogicalblockstostorethefile allocationtableentrieswhichgetmodifiedfrequently.IfFATisusedwithastaticlogicaltophysicalblockmappingthen thefileallocationtableblockswillwearoutfarbeforeotherblockscausingprematuredevicefailure.Thus,forFAT,itis veryimportantthatthelogicaltophysicalmappinggetsstirredupabittocausethefileallocationtablestobewrittento differentphysicalblocks,thuslevellingoutthewear. WearlevellingisfarlessofaconcerninalogstructuredfilesystemsuchasYAFFSsincewealwayswritetotheendofthe logandthusdon'tkeeponoverwritingthesameblocks. Therearebasicallytwowaystoachievewearleveling:
useaspecificallywrittensetoffunctionsthatactivelyperformwearleveling. allowadegreeofwearlevelingtohappenasasideeffectofotheractivity.
13.1.4 yaffs_Tnodeandyaffs_ObjectManagement
yaffs_Tnodesandyaffs_Objectsaredynamicallycreatedandreleasedastheyareneeded. Theallocationandmanagementcouldbedonebyjustusingthestandardmemoryallocator(malloc,kmallocetc),butthis couldcauseperformanceproblems,particularlyinembeddedsystemswhichtendtohaverathersimplisticmemory allocators. Theallocationanddeletionofmanysmallstructurescancausememoryfragmentationcausingoutofmemoryand slowdownproblems. Topreventtheseproblems,YAFFSmanagestheseobjectsinternally.YAFFScreatesmanystructures(100orso)atatime
Page20/25
andthenallocatesthemsinglyfromafreelist.Whenstructuresarereleasedtheyareplacedonthefreelistforreuse.
13.2. InternalCache
ThisdescribesthestandardYAFFSinternalcacheimplementation.Alternativecacheshavebeen devisedforotherpurposesandatsomefuturepointYAFFSmightbeextendedtosupportpluggable cachepolicies. ThemainpurposeoftheinternalcacheistoreduceNANDaccessforbadlybehavedapplications whichperformmanysmallreadsandwrites.Believeitornot,butthereareapplicationsouttherethat readorwritefilesafewbytesatatime:
while(...) write(h,buf,1); Thissortofbehaviourwouldbeterribleforperformanceandflashmemorylifeifeachreadorwriteoperationresultedina writetoflash.YAFFSprovidesaninternalcachetomitigateagainstthissortofbehaviour. TheYAFFSinternalcacheiscalledaShortOpCache(shortoperationcache)andonlycomesintoplayforoperations whicharenotchunkaligned.ThecachingmechanismisverysimpleandwasprimarilywrittenforlesssophisticatedOSs thatlacktheirownfilecachinglayer.ThecacheisstillbeneficialinOSslikeLinux,whichprovideacachinglayer,because itcachesboththereadandwritepath.Thus,whileshortreadswillbeservicedbytheLinuxpagecache,theshortOpCache doesaddressnonchunkalignedwrites. Pagealignedreads/writesbypassthecacheunlessthatchunkisalreadyinthecache. Thecacheisstoredinanarrayofcacheentries.Thenumberofcacheentriesissetintheyaffs_Deviceconfigurationby dev>nShortOpCachesandthecacheisconfiguredduringinitialisation.Acachesizeofzerodisablesthecache.Thecache managementalgorithmsaresimplisticandprobablydon'tscalewelltoverylargenumbers.Thismeansthatitisprobably besttostickwitharound5to20cacheentries. Eachcacheentryholds:
Findinganunusedcacheentryisasimpleoperation.Wejustiteratethroughthecacheentriessearchingforonethatisnot busy.Ifallcacheentriesarebusythenacachepushoutisperformedandthenweretrythefind. Wheneveracacheentryisaccessed(readorwritten)thelastusecounterisissettoanincrementingnumber.Thiscounter valueisusedtodrivetheleastrecentlyused(LRU)algorithm. Cacheentrieswhichhavebeenmodifiedandnotyetwrittenbacktoflasharemarkedwithadirtyflag.Thisistellsus whetherweneedtowriteoutthedataduringaflushorpushout. Apushouthappenswheneverthecacheisfullandweneedtopushanentryoutofthecachetomakespacetoloadanew entry.TodothisweusetheLRUalgorithmtoselecttheLRUcacheentry.Wethenwriteoutalldirtyentriesforthatfile,in ascendingorder,andmarktheseentriesasfree. Wheneverafileisflushed(fileflushorclose),thefile'sdirtyentriesarewrittenout.Atcertaintimes(eg.beforean unmountoratsync())theentirecacheisflushed.
Page21/25
13.3. Scanning
Scanningistheprocesswherebyfilesystemstateisrebuiltfromscratch.ThishappensonmountingaYAFFSpartitionifno checkpointdataisavailableorifthemountingwastoldtoignorethecheckpointdata. Scanninginvolvesreadingthetagsfromalltheactivechunksinthesystemsocantakequiteawhile,butprobablylesstime thanyou'dexpect. ThescanningprocessisdifferentforYAFFS1vsYAFFS2becauseofthedifferencesmentionedbefore.
13.3.1 YAFFS1Scanning
YAFFS1scanningisratherstraightforwardbecauseYAFFS1usesdeletionmarkers. Allwereallyneedtodoisreadthetagsoffallthechunksinthepartitionanddetermineifthechunkisactive.Orderisnot importantandblockscouldbereadinanyorder.Ifthedeletionmarkerhasnotbeensetthenitisactiveandweneedtoadd thechunktothefilesystemstateasfollows: Ifthechunkisadatachunk(chunkId>0)thenthechunkmustbeaddedtotherelevantfile'stnodetree.Iftheobjectdoes notexist,thenwecreateitinlost+found,althoughithasnonameandotherfiledetailsyet. Ifthechunkisanobjectheader(chunkId==0)thentheobjectmustbecreated(oritmightalreadyexistinlost+foundand weneedtofixupitsdetailsandslotitintothecorrectpartofthedirectorytree. IfatanytimeduringscanningwefindthatwearereadingasecondchunkwiththesameobjectId:chunkIdasapreviously encounteredchunkthenthe2bitserialnumberisusedtoresolvethedispute. Oncethescanningiscompletewestillneedtofixupafewthings: Hardlinksmustbeconnectedupproperly(itismoreefficienttodeferthisuntilafterthescanninghascompleted). Also,objectswhichweremarkedasunlinkedmustbedeletedsincetherearenolongerhandlesopentotheseobjects.
13.3.2 YAFFS2Scanning
YAFFS2hasnodeletionmarkerswhichmakesthescanningsignificantlymorecomplex. Thefirstthingthatneedstobedoneistoprescantheblockstodeterminethesequencenumberfortheblocks.Thelistof blocksandsequencenumbersisthensortedtomakeachronologicallyorderedlist. Wenowscanbackwardsthroughtheseblocks(ieinreversechronologicalorder).Thus,thefirstoccurrenceofany objectId:chunkIdisthemostcurrentoneanditiseasytodeterminewhichischunksareoutofdate. Wereadthetagsforeachchunkineachblock. Ifitisadatachunk(ie.chunkId>0)then:
Page22/25
uncleanshutdownwiththefilestillopen.WecancreatethefileandusethechunkIdandnbytestosetthe fileextents.
Elseiftheobjectdoesexistandthechunkliesbeyondtheobject'sfilescansize(extents)thenitisa truncatedchunkandcanbedeleted. Elseitisplacedintothefile'stnodetree. Iftheobjectisinthedeleteddirectorythenmarktheobjectasdeletedandsettheobject'sshrinksizeto zero. Elseifanobjectheaderhasnotyetbeenfoundforthisobject,thenthisisthemostcurrentobjectheader andholdsthemostcurrentnameetc.Ifnodatachunkshaveyetbeenfoundforthischunkthenthesizein theobjectheaderisalsothefileextents.Thefilesizeisusedtosetthefilescansizesinceatthetimeit waswrittenthefiledidnotextendpastthatpointandwecansafelyignorefurtherdatachunksforthatfile thatextendpastthatpoint. Elsejustusethefilesizeasthescansizeifitissmallerthanthecurrentfilescansize. Elsethisanobsoleteobjectheader.Itisdeleted.
Ifitisanobjectheader(ie.chunkId==0)then:
Abitmoreexplanationisprobablyneededtodescribethefilescansize.Thewholepurposeofthisistodeterminedata chunksthatshouldbeignoredduetothefilebeingtruncated.
13.4. Checkpoint
Mountscanningtakesquitealotoftimeandslowsmounting,thoughperhapsbylessthanyou'dthink.Checkpointingisa mechanismtospeedthemountingbytakingasnapshotoftheYAFFSruntimestateatunmountorsync()andthen reconstitutingtheruntimestateonremounting.Thespeedupisdramatic. Theactualcheckpointmechanismisquitesimple.Astreamofdataiswrittentoasetofblockswhicharemarkedas holdingcheckpointdataandtheimportantruntimestateiswrittentothestream. Notallstateneedstobesaved,onlythestateneededtoreconstitutethestructureoftheruntimedata.Forexample,file metadataneednotbesavedsinceitisveryeasytoloadthatthroughlazyloading. Thecheckpointstoresdatainthefollowingorder:
startmarker,includingcheckpointformatid. yaffs_Deviceinformation blockinformation chunkflags objects,includingfilestructure endmarker checksum
Thecheckpointvalidityisensuredbythefollowingmechanisms:
thedataisstoredusingwhateverECCmechanismsareinplace. thestartmarkerincludesacheckpointversionsothatanobsoletecheckpointwillnotbereadifthecheckpointcode
changes.
dataiswrittentothestreamasstructuredrecords,eachwithatypeandsize.
Page23/25
theendmarkermustbereadwhenexpected. twochecksumsaremaintainedacrossthewholecheckpointdataset.
wrtitngthecheckpointsotheirstateiswrittentothecheckpointasEMPTYorCHECKPOINT,itdoesnotreallymatter which.
Afterreadingthecheckpoint,weknowwhichblockshavebeenusedtostorecheckpointdata.Weupdatethoseblocksto
reflecttheCHECKPOINTstate.
13.5. ExtendedTagsandPackedTags
Extendedtagsisanabstractionofawaytostoremoreinformationinthetags,allowingforfasterscantimes.PackedTagsis aphysicalimplementationofthestorageofextendedtags. Whenwe'rescanningwereallyonlyneedtobuildupthestructureofthefilesystem.Inotherwords,rebuildthedirectory relationships,tnodetreesandsuch.Muchofthedetailsuchasfilenames,permissionsetccanbelazyloaded. Thisrequiredstoringextrainformation,suchastheparentdirectoryandobjecttype,inthetagsofobjectheaders.Buthow dowedothiswithoutenlargingthetagssize? ObjectheadersalwayshaveachunkIdofzeroandnbyteshasnomeaningbecauseitisnotadatachunk.Byusingasingle bittoidentifyobjectheaderswesuddenlyopenupawholelotofspacethatwecanstufffullofusefuldata.Bybeingclever astowhatdataweuse,wecanpackintheimportantdatainmostcircumstances. Sometimes,forexampleverylargefiles,thepackingwillnotworkbecausethereareinsufficientbitstoholdthefieldsowe justwriteregulartagsinstead.That'sOKsincetherecanonlybefewsuchfilesinthesystemsotheperformancereduction onlyimpactsveryfewfilesandthesystemlevelimpactisunnoticable.Asimple,cheap,strategythatgivesspeedup99%of thetimeisaveryhandyone. Otherdetailsarelazyloadedondemand.Thiswillhappenwhentheobjectislookedup(eg.byafileopenorsearchingfor thefileinthedirectory).
13.6. Inbandtags
Thusfarwehaveconsideredchunkswheretherearesufficientunallocatedbytesinthespare(oroutofband)areatostore thetags.Thismeansthatthedatapartofthepageisapowerof2insize.Powersof2areveryfasttodealwithwhichis whymostoperatingsystemsusevariouspowersof2intheirinternals. Butwhathappensifwedon'thaveenoughspaceinthespareareatostorethetags?Inbandtagstotherescue!Withinband tagswestorethetagsintheflashdataareaalongsidethedata.Thisgivesusfarmoreflexibilityinthetypesofstoragethat yaffscansupport,butbreaksthepowerof2alignment.Therearethreemajorpenaltieswithinbandtags:
Page24/25
multiplicationsanddivisionswhichcanrelativelyexpensive.
Readingtagsduringscanningetccaninvolvereadingthewholepage,makingthisslower.Theimpactisminimised byusingcheckpointing.
Thus,itisbetter,performancewise,toonlyuseinbandtagswhenregulartagswillnotwork.
13.7. SoftDeletion
SoftdeletionisamechanismthatwasaddedtospeedupfiledeletioninYAFFS1mode. LookingbacktothedescriptionofhowfilesarestoredinYAFFS1,youcanseethatdeletingafileinvolveswritingthe deletionmarkerofeverydatachunkinthefile.Thiscantakequitesometimeforlargefiles. Thesoftdeletionmechanismwasaddedtoavoidwritingallthosedeletionmarkers.Insteadofwritingthedeletionmarkers, softdeletioninsteadtracksthenumberofchunksineachblockthatbelongtodeletedfilesandleavesthecleanuptothe garbagecollector. Whenthegarbagecollectorlooksatablockitcanseewhatcurrentchunks(iechunksnotyetdeleted)belongtodeleted filesanddoesnotcopythose,thuseffectivelydeletingthechunk.Onceallthechunksassociatedwithadeletedfilehave cleanedupbythegarbagecollectorthefileheaderobjectcanalsobedeleted. YAFFS2modedoesnotwritedeletionmarkersandthusdoesnotusesoftdeletion.
Page25/25