Transcription of Chapter 3. File Systems and the File Hierarchy
1 UNIXL ectureNotesChapter3 FileSystemsandtheFileHierarchyStewartWei ssChapter3 FileSystemsandtheFileHierarchyConceptsCo veredUNIX lesystemsand lehierarchiesInternalstructureofa lesystemMountingi-nodesand leattributesThedirentstructureManipulati ngdirectoriesandi-nodesCreationof lesbythekernelImplementingls,pwd,anddu,T raversing lehierarchies,opendir,readdir,closedir,s eekdir,tel ldir,rewinddir,stat,lstat,fstat,chmod,ch own,creat,link,unlink,unlinkat,read link,umask,fnmatchchgrp,chown,utime,getp wuid,getgrgid,getpwnam,getgrnam,rename,n tfw,fts_open,fts_read,fts_children, ductionThischapterlo oksatUNIX lesystemsfromtheprogrammer'sp ersp jectiveistob eabletowriteprogramsthatusethepartoftheU NIXAPI concerningthe lesystemanditscomp ,wewillb eginwithanoverviewofwhatthe lesystemis,andtoalimitedextent, ,abasicunderstandingofthetypicalimplemen tationcanhelpinunderstandingp lesystemisanabstractionthatsupp ortsthecreation,deletion,andmo di cationof les,andorganizationof ortscontrolofaccessto lesystem torefertoahierarchical.
2 Tree-likestructurewhoseinternalno desaredirectoriesandwhoseexternalno desarenon-directory- les(orp erhapsemptydirectories),buta lesystemisactuallythe ,forexample, atstructureiscompletelyhiddenfromtheuser , lesanddirectories,whichismoreprop erlycalledthedirectoryhierarchyor eratingsystemstreatthe lesystemsonthesedevicesasindep 'sDOS,andsystemsderivedfromit,forexample ,eachseparatediskpartitionhasadrivelette r,andthe ect,DOShasmultipletreeswhosero ,atypicalWindowsmachinemay1 UNIXL ectureNotesChapter3 FileSystemsandtheFileHierarchyStewartWei sshaveadirectoryE:\usersonthe"E:"drivean dadirectory"C:\Temp"onthe"C.
3 "drivebutthesedirectoriesareintwoseparat etrees, desas lenames,butitisnotatreeifyouthinkofthele afno desasactual les,sinceasingle lecanhavemorethanonename,existingasadire ctoryentryinmultipledirectories,makingth etop ertyofreferringtoitasatree, ,everyaccessible leisinthissingle lehierarchy, "C"drive"or"E"drive" ecauseoftheconceptofmounting,whichwillb edescrib ,inUNIX,a lesystemmayb emountedontothesingle lehierarchybyattachingthat lesystem'sro lesystemontothe lehierarchy,the lesystemb ecomesasubtreeofthehierarchy,makingitp ossibletonavigateintothe lesystemfromtherestofthe lesystemscurrentlymountedonthe ,theoutputofthemountcommandcouldb e.
4 /dev/ on / type ext3 (rw)proc on /proc type proc (rw)sysfs on /sys type sysfs (rw)devpts on /dev/pts type devpts (rw,gid=5,mode=620)tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs _t:s0")/dev/sda1 on /boot type ext3 (rw)none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)Eachlineisoftheform, le_system_nameonplace_where_it_is_mounte dtype le_system_typeother_optionsThesixthlines tatesthatthereisa lesystemnamed/dev/sda1oftyp eext3 outthemeaningofthelineproc on /proc type proc (rw)
5 Inasubsequentchapterwewillexplorethe/pro c lesystem,whichisnota lesystemthatmanagesdiskspace,butaninterf acetothekernel' edelayeduntilafteradiscussionof oint,thesigni canceofmountingisthatdi erent lesystemscanb e,andusuallyare,partofasingleconceptual lehierarchy,makingitp ossibletopartitionadiskintoseparate lesystemsthatallb ecomepartofasingle ,thediskwascon guredasasinglepartitionwithasingle ecameadvantageousinop eratingsystemdesigntopartitionthemintomu ltiplelogicaldevicesthatwereactuallydist inctphysicalp :Linux lesystemlayout,repro ducedfrom"LinuxInternals",bySimoneDemblo nandSebastianSpitzner,CourtesyofTheShutt leworthFoundation.
6 Morecontrolofsecurity di erentusergroupscouldb eplacedintodi erentpartitions,anddi erentmountingoptionscouldb eusedonseparatepartitions,sothatsomemigh tb ereadonly,andothersmighthavedi erentsecurityoptions moree cientuseofthedisk di erentpartitionscouldusedi erentblo cksizesand lesizelimits moree cientop eration shorterseekdistanceswouldimprovediskacce sstimes improvedback-uppro cedures backupscouldb edoneonpartitions,notdisks,therebymaking itp ossibletoback-updi erent lesystemsatdi erentintervals improvedreliability damagecouldb erestrictedtoasinglepartitionratherthant heentiredisk,andredundancycouldb ebuiltinThebiggestdisadvantageofpartitio ningadiskisthatpartitionscannotb eincreasedinsize,sowhentheyarecreated,if theyareto osmalland llup,theentirediskwouldneedtob ,buttheytendtob dernUNIX systemsallowdiskstob epartitionedintotwoormoreseparatephysica lentities.
7 Eachcontainingadistinct lesystemsinseparatephysicalpartitionsare connectedtoeachotherbyvirtueoftheirb eingmountedondirectoriesoftheoneandonlyd irectoryhierarchyro otedat"/", lesystemhasitsownp o oloffreeblo cks,itsownsetof les,anditsowni-no erentUNIX-likeop eratingsystemsprovidedi erent lesystems,eachofwhichmayb lesystemisnotpartofanyUNIX3 UNIXL ectureNotesChapter3 FileSystemsandtheFileHierarchyStewartWei ssstandard:thereisnosingleimplementation prescrib edorproscrib edinanystandardsdo ,whenreadingab outanimplementationoftheUNIX lesystem,youb lesystemisnotusedinmanymo dernsystems.
8 Mo dernimplementationsaremorecomplexb ecausetheyincorp dernmachinesmustb eabletomount lesystemsofdi erenttyp ,manyUNIX systemsallowuserstomountFAT2andNTFS disk-based lesystems,whichdonotfollowtheUNIXmo lethatconsistsofalistofdirectoryentries, eachofwhichcontainsthenameofa leanditsi-number,whichservesasap ointertothe le' (p ersistingsinceearlyWindowsop eratingsystems), ,iftheyaredesignedtomountsuchsystems,mus tcreateakernelob ortantly,theUNIX kernelcannothard-co desystemcallssuchasread()b ecausetheimplementationofread()willdep endonthe ,theactualmachineco dethatisexecutedwhenthesecallsareinvoked cannotb eb ointersinaprogramminglanguageorvirtualfu nctionsinC++.
9 Whenitisnotknownhowmuchstorageadatastruc tureneedsatcompiletime,insteadofdeclarin gitstatically,thebindingofthenametothest orageisdelayeduntilrun-timebyusingap ++,whenclassesarecreatedwithvirtualfunct ions,thefunctionco dethatisexecutedasaresultofafunctioncall isnotdetermineduntilrun-time, ,thesolutionisachievedopaquelythroughthe useofp dernUNIX Systems ,suchasLinux,theimplementationoft he lesystemisachievedbydynamicallybindingth eimplementationsof lesystemcallstofunctionsthatarehard-co dedintheparticular lesystemthatismounted, ortantrightnow;whatmattersisthatmo dernUNIX lesystemsarevirtual lesystems,designedtohandlemanydi erenttyp esofunderlyingphysical ,inSun'svariantsofUNIX,fromSunOSthroughS olaris,andinBSD(andFreeBSD),theconceptso fi-no deandi-numb erhaveb eenreplacedbythoseofv-no deandv-numb er,withthe"v"standingfor"virtual".
10 Linuxcontinuestousetheterm"i-no de", (Ext2) edbyChrisProvenzano, lesystemwasdevelop edinthemid1990'sbyR myCard,Theo doreTs'o, lesystemwasExt3,whichwasdevelop edbyStephenTweedieandwhichdi ecialjournal leisusedtorecordalloftheactionsthataresu pp osedtob etakenonthe lesystem,suchascreatinganddeleting les,changingtheircontentsorattributes, lesystem,thisrecordcanb eusedtorecoverthestateofthe lesystemwithoutthelengthytaskofexamining everyblo ckandi-no onecanb econvertedtotheotherwhilethe lesystemismountedb ecausethedi ,Ext4,wasreleasedin2008,mostlytoimprovep ortsmanytyp esof lesystems,theExt2.