Tải bản đầy đủ - 0 (trang)
Chapter 12.  The Virtual Filesystem

Chapter 12.  The Virtual Filesystem

Tải bản đầy đủ - 0trang

12.1.TheRoleoftheVirtualFilesystem(VFS)

TheVirtualFilesystem(alsoknownasVirtualFilesystemSwitch

orVFS)isakernelsoftwarelayerthathandlesallsystemcalls

relatedtoastandardUnixfilesystem.Itsmainstrengthis

providingacommoninterfacetoseveralkindsoffilesystems.

Forinstance,let'sassumethatauserissuestheshell

command:

$cp/floppy/TEST/tmp/test



where/floppyisthemountpointofanMS-DOSdisketteand

/tmpisanormalSecondExtendedFilesystem(Ext2)directory.

TheVFSisanabstractionlayerbetweentheapplication

programandthefilesystemimplementations(seeFigure121(a)).Therefore,thecpprogramisnotrequiredtoknowthe

filesystemtypesof/floppy/TESTand/tmp/test.Instead,cp

interactswiththeVFSbymeansofgenericsystemcallsknown

toanyonewhohasdoneUnixprogramming(seethesection

"File-HandlingSystemCalls"inChapter1);thecodeexecuted

bycpisshowninFigure12-1(b).



Figure12-1.VFSroleinasimplefilecopy

operation



FilesystemssupportedbytheVFSmaybegroupedintothree

mainclasses:



Disk-basedfilesystems

Thesemanagememoryspaceavailableinalocaldiskorin

someotherdevicethatemulatesadisk(suchasaUSB

flashdrive).Someofthewell-knowndisk-basedfilesystems

supportedbytheVFSare:

FilesystemsforLinuxsuchasthewidelyusedSecond

ExtendedFilesystem(Ext2),therecentThirdExtended

Filesystem(Ext3),andtheReiserFilesystems(ReiserFS

)[*]

[*]AlthoughthesefilesystemsowetheirbirthtoLinux,theyhavebeenportedto

severalotheroperatingsystems.



FilesystemsforUnixvariantssuchassysvfilesystem

(SystemV,Coherent,Xenix),UFS(BSD,Solaris,

NEXTSTEP),MINIXfilesystem,andVERITASVxFS(SCO

UnixWare)



MicrosoftfilesystemssuchasMS-DOS,VFAT(Windows

95andlaterreleases),andNTFS(WindowsNT4and

laterreleases)

ISO9660CD-ROMfilesystem(formerlyHighSierra

Filesystem)andUniversalDiskFormat(UDF)DVD

filesystem

OtherproprietaryfilesystemssuchasIBM'sOS/2(HPFS

),Apple'sMacintosh(HFS),Amiga'sFastFilesystem

(AFFS),andAcornDiskFilingSystem(ADFS)

Additionaljournalingfilesystemsoriginatinginsystems

otherthanLinuxsuchasIBM'sJFSandSGI'sXFS



Networkfilesystems

Thesealloweasyaccesstofilesincludedinfilesystems

belongingtoothernetworkedcomputers.Somewell-known

networkfilesystemssupportedbytheVFSareNFS,Coda,

AFS(Andrewfilesystem),CIFS(CommonInternetFile

System,usedinMicrosoftWindows),andNCP(Novell's

NetWareCoreProtocol).



Specialfilesystems

Thesedonotmanagediskspace,eitherlocallyorremotely.

The/procfilesystemisatypicalexampleofaspecial

filesystem(seethelatersection"SpecialFilesystems").

Inthisbook,wedescribeindetailtheExt2andExt3filesystems

only(seeChapter18);theotherfilesystemsarenotcoveredfor

lackofspace.

Asmentionedinthesection"AnOverviewoftheUnix



Filesystem"inChapter1,Unixdirectoriesbuildatreewhose

rootisthe/directory.Therootdirectoryiscontainedintheroot

filesystem,whichinLinux,isusuallyoftypeExt2orExt3.All

otherfilesystemscanbe"mounted"onsubdirectoriesofthe

rootfilesystem.[*]

[*]Whenafilesystemismountedonadirectory,thecontentsofthedirectoryintheparentfilesystemareno

longeraccessible,becauseeverypathname,includingthemountpoint,willrefertothemountedfilesystem.

However,theoriginaldirectory'scontentshowsupagainwhenthefilesystemisunmounted.Thissomewhat

surprisingfeatureofUnixfilesystemsisusedbysystemadministratorstohidefiles;theysimplymounta

filesystemonthedirectorycontainingthefilestobehidden.



Adisk-basedfilesystemisusuallystoredinahardwareblock

devicesuchasaharddisk,afloppy,oraCD-ROM.Auseful

featureofLinux'sVFSallowsittohandlevirtualblockdevices

suchas/dev/loop0,whichmaybeusedtomountfilesystems

storedinregularfiles.Asapossibleapplication,ausermay

protectherownprivatefilesystembystoringanencrypted

versionofitinaregularfile.

ThefirstVirtualFilesystemwasincludedinSunMicrosystems's

SunOSin1986.Sincethen,mostUnixfilesystemsincludea

VFS.Linux'sVFS,however,supportsthewidestrangeof

filesystems.



12.1.1.TheCommonFileModel

ThekeyideabehindtheVFSconsistsofintroducingacommon

filemodelcapableofrepresentingallsupportedfilesystems.

Thismodelstrictlymirrorsthefilemodelprovidedbythe

traditionalUnixfilesystem.Thisisnotsurprising,becauseLinux

wantstorunitsnativefilesystemwithminimumoverhead.

However,eachspecificfilesystemimplementationmust

translateitsphysicalorganizationintotheVFS'scommonfile

model.

Forinstance,inthecommonfilemodel,eachdirectoryis



regardedasafile,whichcontainsalistoffilesandother

directories.However,severalnon-Unixdisk-basedfilesystems

useaFileAllocationTable(FAT),whichstoresthepositionof

eachfileinthedirectorytree.Inthesefilesystems,directories

arenotfiles.TosticktotheVFS'scommonfilemodel,theLinux

implementationsofsuchFAT-basedfilesystemsmustbeableto

constructonthefly,whenneeded,thefilescorrespondingto

thedirectories.Suchfilesexistonlyasobjectsinkernel

memory.

Moreessentially,theLinuxkernelcannothardcodeaparticular

functiontohandleanoperationsuchasread()orioctl().

Instead,itmustuseapointerforeachoperation;thepointeris

madetopointtotheproperfunctionfortheparticular

filesystembeingaccessed.

Let'sillustratethisconceptbyshowinghowtheread()shownin

Figure12-1wouldbetranslatedbythekernelintoacallspecific

totheMS-DOSfilesystem.Theapplication'scalltoread()

makesthekernelinvokethecorrespondingsys_read()service

routine,likeeveryothersystemcall.Thefileisrepresentedby

afiledatastructureinkernelmemory,aswe'llseelaterinthis

chapter.Thisdatastructurecontainsafieldcalledf_opthat

containspointerstofunctionsspecifictoMS-DOSfiles,including

afunctionthatreadsafile.sys_read()findsthepointertothis

functionandinvokesit.Thus,theapplication'sread()isturned

intotheratherindirectcall:

file->f_op->read(...);



Similarly,thewrite()operationtriggerstheexecutionofa

properExt2writefunctionassociatedwiththeoutputfile.In

short,thekernelisresponsibleforassigningtherightsetof

pointerstothefilevariableassociatedwitheachopenfile,and

thenforinvokingthecallspecifictoeachfilesystemthatthe



f_opfieldpointsto.



Onecanthinkofthecommonfilemodelasobject-oriented,

whereanobjectisasoftwareconstructthatdefinesbothadata

structureandthemethodsthatoperateonit.Forreasonsof

efficiency,Linuxisnotcodedinanobject-orientedlanguage

suchasC++.ObjectsarethereforeimplementedasplainC

datastructureswithsomefieldspointingtofunctionsthat

correspondtotheobject'smethods.

Thecommonfilemodelconsistsofthefollowingobjecttypes:



Thesuperblockobject

Storesinformationconcerningamountedfilesystem.For

disk-basedfilesystems,thisobjectusuallycorrespondstoa

filesystemcontrolblockstoredondisk.



Theinodeobject

Storesgeneralinformationaboutaspecificfile.Fordiskbasedfilesystems,thisobjectusuallycorrespondstoafile

controlblockstoredondisk.Eachinodeobjectisassociated

withaninodenumber,whichuniquelyidentifiesthefile

withinthefilesystem.



Thefileobject

Storesinformationabouttheinteractionbetweenanopen

fileandaprocess.Thisinformationexistsonlyinkernel

memoryduringtheperiodwhenaprocesshasthefileopen.



Thedentryobject

Storesinformationaboutthelinkingofadirectoryentry

(thatis,aparticularnameofthefile)withthe

correspondingfile.Eachdisk-basedfilesystemstoresthis

informationinitsownparticularwayondisk.

Figure12-2illustrateswithasimpleexamplehowprocesses

interactwithfiles.Threedifferentprocesseshaveopenedthe

samefile,twoofthemusingthesamehardlink.Inthiscase,

eachofthethreeprocessesusesitsownfileobject,whileonly

twodentryobjectsarerequiredoneforeachhardlink.Both

dentryobjectsrefertothesameinodeobject,whichidentifies

thesuperblockobjectand,togetherwiththelatter,thecommon

diskfile.



Figure12-2.Interactionbetweenprocessesand

VFSobjects



Besidesprovidingacommoninterfacetoallfilesystem

implementations,theVFShasanotherimportantrolerelatedto

systemperformance.Themostrecentlyuseddentryobjectsare

containedinadiskcachenamedthedentrycache,which

speedsupthetranslationfromafilepathnametotheinodeof

thelastpathnamecomponent.

Generallyspeaking,adiskcacheisasoftwaremechanismthat

allowsthekerneltokeepinRAMsomeinformationthatis

normallystoredonadisk,sothatfurtheraccessestothatdata

canbequicklysatisfiedwithoutaslowaccesstothediskitself.

Noticehowadiskcachediffersfromahardwarecacheora

memorycache,neitherofwhichhasanythingtodowithdisks

orotherdevices.AhardwarecacheisafaststaticRAMthat

speedsuprequestsdirectedtotheslowerdynamicRAM(see

thesection"HardwareCache"inChapter2).Amemorycacheis

asoftwaremechanismintroducedtobypasstheKernelMemory

Allocator(seethesection"TheSlabAllocator"inChapter8).

Besidethedentrycacheandtheinodecache,Linuxusesother

diskcaches.Themostimportantone,calledthepagecache,is

describedindetailinChapter15.



12.1.2.SystemCallsHandledbytheVFS

Table12-1illustratestheVFSsystemcallsthatreferto

filesystems,regularfiles,directories,andsymboliclinks.Afew

othersystemcallshandledbytheVFS,suchasioperm(),ioctl(

),pipe(),andmknod(),refertodevicefilesandpipes.These

arediscussedinlaterchapters.Alastgroupofsystemcalls

handledbytheVFS,suchassocket(),connect(),andbind(),

refertosocketsandareusedtoimplementnetworking.Some

ofthekernelserviceroutinesthatcorrespondtothesystem

callslistedinTable12-1arediscussedeitherinthischapteror

inChapter18.



Table12-1.SomesystemcallshandledbytheVFS

Systemcallname



Description



mount()umount()umount2()



Mount/unmountfilesystems



sysfs()



Getfilesysteminformation



statfs()fstatfs()statfs64()fstatfs64()



Getfilesystemstatistics



ustat()



chroot()pivot_root()



Changerootdirectory



chdir()fchdir()getcwd()



Manipulatecurrentdirectory



mkdir()rmdir()



Createanddestroy

directories



getdents()getdents64()readdir()link()



Manipulatedirectoryentries

unlink()rename()lookup_dcookie()



readlink()symlink()



Manipulatesoftlinks



chown()fchown()lchown()chown16()



Modifyfileowner

fchown16()lchown16()



chmod()fchmod()utime()



stat()fstat()lstat()access()oldstat()oldfstat()

oldlstat()stat64()lstat64()



Modifyfileattributes



Readfilestatus



fstat64()



open()close()creat()umask()



Open,close,andcreatefiles



dup()dup2()fcntl()fcntl64()



Manipulatefiledescriptors



select()poll()



Waitforeventsonasetof

filedescriptors



truncate()ftruncate()truncate64()



ftruncate64()



Changefilesize



lseek()_llseek()



Changefilepointer



read()write()readv()writev()sendfile()sendfile64()

readahead()



CarryoutfileI/Ooperations



io_setup()io_submit()io_getevents()io_cancel()

io_destroy()



AsynchronousI/O(allows

multipleoutstandingread

andwriterequests)



pread64()pwrite64()



Seekfileandaccessit



mmap()mmap2()munmap()madvise()mincore()



Handlefilememorymapping



remap_file_pages()



fdatasync()fsync()sync()msync()



Synchronizefiledata



flock()



Manipulatefilelock



setxattr()lsetxattr()fsetxattr()getxattr()lgetxattr()

Manipulatefileextended

fgetxattr()listxattr()llistxattr()flistxattr()removexattr()

attributes

lremovexattr()fremovexattr()



WesaidearlierthattheVFSisalayerbetweenapplication

programsandspecificfilesystems.However,insomecases,a

fileoperationcanbeperformedbytheVFSitself,without

invokingalower-levelprocedure.Forinstance,whenaprocess

closesanopenfile,thefileondiskdoesn'tusuallyneedtobe

touched,andhencetheVFSsimplyreleasesthecorresponding

fileobject.Similarly,whenthelseek()systemcallmodifiesa

filepointer,whichisanattributerelatedtotheinteraction

betweenanopenedfileandaprocess,theVFSneedstomodify

onlythecorrespondingfileobjectwithoutaccessingthefileon

disk,andthereforeitdoesnothavetoinvokeaspecific

filesystemprocedure.Insomesense,theVFScouldbe



considereda"generic"filesystemthatrelies,whennecessary,

onspecificones.







Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 12.  The Virtual Filesystem

Tải bản đầy đủ ngay(0 tr)

×