A File System for the 21st Century Previewing the Windows NT 50 File System.docx
《A File System for the 21st Century Previewing the Windows NT 50 File System.docx》由会员分享,可在线阅读,更多相关《A File System for the 21st Century Previewing the Windows NT 50 File System.docx(13页珍藏版)》请在冰豆网上搜索。
![A File System for the 21st Century Previewing the Windows NT 50 File System.docx](https://file1.bdocx.com/fileroot1/2023-1/29/49453453-057b-46bf-afec-e267a6b2d5fa/49453453-057b-46bf-afec-e267a6b2d5fa1.gif)
AFileSystemforthe21stCenturyPreviewingtheWindowsNT50FileSystem
AFileSystemforthe21stCentury:
PreviewingtheWindowsNT5.0FileSystem
JeffreyRichterandLuisFelipeCabrera
BecauseofthewayNTFSstoresattributes,itispossibleforalloftheattributesofasinglefile,includingitsDataattribute,toberesident.Thisimprovesperformancewhenaccessingsmallfiles.NTFSalsostoresthemostcommonattributesofafileinthefile’sdirectoryentry.
Thisarticleassumesyou’refamiliarwithC++andWin32
Codeforthisarticle:
Nov98NTFS.exe(5KB)
JeffreyRichterwroteAdvancedWindows,ThirdEdition(MicrosoftPress,1997)andWindows95:
ADeveloper’sGuide(M&TBooks,1995).Jeffcanbereachedatwww.JeffreyR.
LuisFelipeCabreraisanarchitectintheWindowsNTBaseDevelopmentgroupatMicrosoft.HisresponsibilitiesareinWindowsNT5.0storagemanagement.
ManyofyourprogrammingtaskswillbesimplifiedwhenyoutakeadvantageofthenewinnovationsintheWindowsNT®5.0filesystem(NTFS).Let'sgoonawhirlwindtourofthesenewfeatures.Remember,wearediscussingsoftwarethatisinbeta,soeverythingissubjecttochange.PleasecheckMicrosoft'smostrecentdocumentationbeforewritinganycodebasedonthisinformation.
Let'sbeginwithanoverviewoftheNTFSfilesystemlayoutondisk.Whilethisinformationisprogrammaticallyoff-limitstotheapplicationdeveloper,ahigh-levelexplanationwillmakeitmucheasierforyoutounderstandmanyofthenewNTFSfeatures.
AttheheartoftheNTFSfilesystemisaspecialfilecalledthemasterfiletable(MFT).ThisfileiscreatedwhenyouformatavolumeforNTFS.TheMFTconsistsofanarrayof1KBentries;eachentryidentifiesasinglefileonthevolume.Whenyoucreateafile,NTFSmustfirstlocateanemptyentrywithintheMFTarray(growingthearrayifnecessary);thenitfillsthe1KBentrywithinformationaboutthefile.Afile'sinformationconsistsofacollectionofattributes.Figure1showsalistofstandardattributesthatcanbeassociatedwithasinglefile(ordirectory).
Whenafileiscreated,thesystemcreatesthesetofattributesfortheMFT'sfileentryandattemptstoplacetheminsidethe1KBblock.Buttherearetwoproblems:
mostattributesarevariablelength,andmanyattributes(likeName,Data,andNamedData)canbemuchlargerthan1KB.SoNTFScan'tjustsimplythrowalltheattributesinsideanMFTentry.Instead,NTFSmustexaminetheattributes;ifthelengthofanattribute'svalueissmall,theattribute'svalueisplacedinsidetheMFTentry.Thisiscalledaresidentattribute.Iftheattribute'svalueislarge,thenthesystemplacestheattributevalueinanotherlocationonthedisk(makingthisanonresidentattribute),andsimplyplacesapointertotheattribute'svalueinsidetheMFTentry.
Today,everybodyhaslotsandlotsofsmallfilesstoredthroughouttheirharddrives.Weallhavelotsofshortcut(.LNK)filesandprobablylotsofDESKTOP.INIfilessprinkledaround.BecauseofthewaythatNTFSstoresattributesinanMFTentry,itispossibleforalloftheattributesofasinglefile,includingitsDataattribute,toberesident.Thisgreatlyimprovesperformancewhenaccessingsmallfiles.Inaddition,NTFSalsostoresthemostcommonattributesofafileinthedirectoryentrythatrepresentsthefile.ThismeansthatwhenthesystemdoesaFindFirstFile/FindNextFileoperationtoretrieveafile'snameorbasicattributes,thedatafortheseattributesisfoundinthedirectoryentry,sonootherdiskaccessisneeded.
PriortoWindowsNT4.0,anMFTentryinNTFSwas4KBinsize.This,ofcourse,allowedfileswithslightlymoredatatohavetheirdataresident.InWindowsNT4.0,MicrosoftparedthesizeofanMFTentryto1KB.MicrosoftstudiedthenumberoffilesandtheirsizesonmanytypicalsystemsandsawthatNTFSwaswastingalotofspaceinMFTentriesandthatitwouldbemoreefficienttomaketheMFTentry1KB.
Nowlet'sgooversomeofthefeaturesofferedbyNTFSthatsoftwaredeveloperscan(andshould)takeadvantageof.
Streams
It'slittleknownthatNTFSallowsasinglefiletohavemultipledatastreams.ThisfeaturehasactuallybeeninNTFSsinceitsveryfirstversion(inWindowsNT3.1)buthasbeendownplayedbyMicrosoft.Thisisunfortunatebecausestreamscanbeincrediblyusefulinmanysituations.
Forinstance,let'ssaythatyouaredevelopingabitmap-editorapplication.Whentheusersavestheirdata,youcreateaBMPfileontheharddisk.You'dalsoliketostoreathumbnailversionoftheimageaswell.Thumbnailsaretypicallystoredattheendofafile,afterthemainbitmapimage.Toshowthethumbnailimage,youmustopenthefile,parsetheheaderinformation,seektothebytesfollowingthemainimage'sdata,readinthethumbnailimage'sdata,andthendisplaythethumbnail.Youcouldstorethethumbnaildatainaseparatefile,butit'snotagoodideabecauseit'stooeasyforthemainimagefileandthethumbnailfiletogetseparated.
AnNTFSnamedstreamoffersthebestofbothworlds.Whenyourapplicationcreatesitsfile,youcanwritethemainimage'sdatatothedefault(unnamed)streamandthencreateanother(named)datastreaminsidethesamefileforthethumbnailimage'sdata.Youhaveonlyonefile,butitcontainstwodatastreams.
Tounderstandhowthisworks,let'sperformanexperiment.OnaWindowsNT-basedmachine(anyversion)openacommandshell.ThenchangetoanNTFSpartitionandenterthefollowing:
C:
\>ECHO"HiReader">XX.TXT:
MyStream
Whenyouexecutethiscommand,thesystemcreatesafilecalledXX.TXT.Thisfilecontainstwostreams:
anunnamedstreamthatcontains0bytesandanamedstream(calledMyStream)thatcontainsthetext"HiReader".Ifyouhaven'tguessedbynow,youaccessafile'snamedstreambyplacingacolonafterthefilenamefollowedbythenameofthestream.Aswithfilenames,Win32®functionstreatstreamnamesascase-preservedandsearchesarecase-insensitive.
Unfortunately,thetoolssuppliedwiththesystemtreatstreamsassecondclasscitizensatbest.Forexample,executethefollowingcommand:
C:
\>DIRXX.TXT
VolumeindriveCisWizard
VolumeSerialNumberis40E5-92D4
DirectoryofC:
\
03/18/9808:
36a0XX.TXT
1File(s)0bytes
0Dir(s)3,399,192,576bytesfree
Asyoucansee,DIRreportsthatthefilesizeis0bytes,butthisisnottrue.TheDIRcommandonlyreportstotheuserthesizeofafile'sunnamedstream;thesizesofnamedstreamswithinthefilearenotshowntotheuser.Bytheway,Exploreralsoreportsafilesizeof0bytes.Thisallowsforsomegeekypartygameswhereyoucanallocatealargestreaminafileonafriend'sdisk.Thefriendwon'tbeabletodiscoverwhereallthediskspacehasgonebecauseallofthetoolsreportthatthefileoccupiesonly0bytes!
Whenworkingwithstreams,rememberthatit'sonlythetoolsthatdon'ttreatstreamswiththerespectthattheydeserve;NTFShasfullsupportforstreams(theyevencountagainstyourstoragequota).
Now,toseethecontentsofthestream,executethiscommand:
C:
\>MOREMyStream
"HiReader"
Here'sanotherwaytousestreams.Saythatyouarewritingaword-processingapplication.Whentheuseropensupanexistingdocument,youwillprobablycreateatemporaryfilethatholdsalloftheuser'schanges.Then,whentheuserdecidestosavethechanges,youwillwritealloftheupdatedinformationtothetemporaryfile,deletetheoriginalfile,andfinallymovethetemporaryfilebacktotheoriginalfile'slocationwhilerenamingthefile.
Thissoundsfairlysimpleandstraightforward,butyou'reprobablyforgettingafewthings.Thefinalfileshouldhavethesamecreationtimestampastheoriginal,soyou'llhavetofixthat.Thefinalfileshouldalsohavethesamefileattributesandsecurityinformationastheoriginal.Itisveryeasytomissproperlyupdatingsomeoftheseattributesduringthisfile-saveoperation.
Ifyouusestreams,alloftheseproblemsgoaway.Allstreamswithinasinglefilesharethefile'sattributes(timestamp,security,andsoon).Youshouldreviseyourapplicationsothattheuser'stemporaryinformationiswrittentoanamedstreamwithinthefile.Then,whentheusersavesthedata,renamethetemporarynameddatastreamtotheunnameddatastream,andNTFSwilldeletetheoldunnameddatastreamanddotherenameinanall-or-nothingmanner.Youwon'thavetodoanythingtothefile'sattributesatall;they'llalljustbethesame.
Beforeweleavestreams,letusjustpointoutafewmorethings.First,ifyoucopyafilecontainingstreamstoafilesystemthatdoesn'tsupportstreams(liketheFATfilesystemusedonfloppydisks),onlythedataintheunnamedstreamiscopiedover;thedatainanynamedstreamsdoesnotgetcopied.
Second,nameddatastreamscanalsobeassociatedwithadirectory.Directoriesneverhaveanunnameddatastreamassociatedwiththembuttheycertainlycanhavenamedstreams.SomeofyoumaybefamiliarwiththeDESKTOP.INIfileusedbytheExplorer.IftheExplorerseesthisfileinadirectory,itknowstoloadashellnamespaceextensionandallowstheshellnamespaceextensiontoparsethecontentsofthedirectory.ThesystemusesthisforfolderssuchasMyDocuments,Fonts,InternetChannels,andmanymore.SincetheDESKTOP.INIfiledescribeshowtheExplorershoulddisplaythecontentsofadirectory,wouldn'titmakemoresenseforMicrosofttoplacetheDESKTOP.INIdataintoanamedstreamwithinadirectory?
ThereasonMicrosoftdoesn'tdothisisbackwardcompatibility.StreamsareimplementedonlyonNTFSdrives;theydonotexistonFATfilesystemsoronCD-ROMdrives.For