csc cache win7
番茄系统家园 · 2021-07-18 12:32:38

|-May-03|-May-03
网络文件系统的机制
,()ningthestateoftheI/ORequestPacket(IRP).TheIRP_NOCACHEbitintheFlagsfieldwilltellthefilesystem(and,ofcourse,thefilter)thatthefileI/Oinquestionisnottobecached.Normally,thisisthecluetothefilesystemdriverthatthisdatashouldnotbecached.
对于文件系统过滤驱动程序来说确定本地文件系统NTFS或者FAT的cache策略是相当容易的只要检查一下I/O请求包IRP的状态就可以了。如果发现IRP的flag域带有IRP_NOCACHE标志就说明文件系统不允许文件cache。
Networkfilesystemsareabitmorecomplexthanthis.WhiletheyalsousetheIRP_NOCACHEbit,theymayalsoneedtodisablecachingasaresultoftheirowninternalpolicy-perhapsdirectedbythestateoftheremotefileonthefiles
erver,aswellasotherclientsinthenetworkthatmightbeusingthefile.Therdbss.sys,whichimplementspartoftheminiredirectormodelallowstheredirector(forexamplemrxsmb.sys,whichisthedriverthatimplementsCIFSorLan
ManagerfunctionalityinWindows2000andmorerecent)tochangethecachingpolicyonaper-filebasis.Inthiscase,anormalIRP_MJ_READIRP,whichwouldnormallybecached,maybetreatedasnon-cached.
而网络文件系统在cache策略方面则有点复杂。虽然它们也使用IPR_NOCACHE标志它们也需要在它们的内部策略当中禁止使用文件cache这些内部的cache策略是由文件服务器上的远程文件的状态决定的而网络上其它的客户端都会使用这些文件。rdbss.sys驱动程序实现了被称为“miniredirector”模型的一部分功能它能够允许每一个重定向器redirector基于每一个文件来改变其缓冲策略。在这种情况下一个普通的IRP_MJ_READ类型的IRP可能被当作可被缓冲的也可以被当作非缓冲来处理。
Forafilterdriverthatismodifyingthedata,theusualtechniqueistolookforandoperateonnon-cachedI/Ooperations.ThiswillcapturebothpagingI/Ooperationsaswellasuserlevelnon-cachedI/Ooperations.However,ifthefilterwishestoalsofilteranyofthemini-redirectors(therearetwoshippedinWindowsXPforexample)itneedstolookatthefieldsoftheFileControlBlock(FCB).
对于一个正在修改文件读数据的过滤驱动程序来说例如对文件内容做透明加解密的驱动程序通常是通过检查和拦截非缓冲I/O请求来实现自己的功能。这样它们就会捕获分页I/O操作或者用户层的非缓冲I/O操作。但是如果过滤驱动程序如果也想拦截mini-redirector的话它就必须检查文件控制块FCB的相关域。
Formostfilesystems,theformatofthisstructureismostlyunderthecontrolofthefilesystem(exceptforthecommonheaderstructure)butformini-redirectorstheformatofthefilecontrolblockisdefinedbythemini-redirectormode
l.Seemrxfcb.hintheIFSKitforthefulldefinition.Thekeydatastructurehere(forafilter)istheMRX_FCB.TheFcbStatefieldwillindicateifthecurrentstateofthefileiscachedornon-cached.Ifthefileallowscachingthe
FCB_STATE_READCACHING_ENABLEDbitwillbeset.Otherwise,I/Otothegivenfilewillbetreatedasnon-cached.
对于大多数文件系统来说FCB结构的格式主要由文件系统来决定除了通用的头结构以外但是mini-redirector的FCB结构的格式由mini-redirector模式定义完整的FCB定义可以参考mrxfcb.h。对过滤驱动程序来说最关键的数据结构是MRX_FCB。该结构的FcbState域描述了该文件是否需要cache或者非cache。如果文件可以被cache则FCB_STATE_READCACHING_ENABLED标志将置位。否则对于指定的文件将被视为非cached。值得注意的是在WindowsServer2003IFSKit中这个标志的拼写已经发生了变化现在的拼写是FCB_STATE_READCACHING_ENABLED。
Note:IntheWindowsServer2003IFSKitthespellingofthisflaghasbeenchan
gedsothatitisnowFCB_STATE_READCACHING_ENABLED.
Whilethisallowsafiltertodeterminethecurrentstateofthefile,theredoesnotappeartobeanysimplewayforafiltertoensurethatthestateofthisfielddoesnotchangebetweenthetimethefilterchecksitandthetimethecal
lisactuallyprocessedbythefilesystem.Thus,itispossiblethatthefilestatemightchangetodisallowcachingafterthischeckismade.Similarly,ifthecheckisdoneaftertheI/Ohasbeenprocessed,itispossiblethefilestat
emightchangetoindicatethatcachingisnowallowedonceagain.SamplecodeforthiscanbeseenintheIFSKit(seesmbmrx/wnet/sys/openclos.c)todemonstrateonepotentialimplementationmodel.
通过检查FCB结构的FcbStatus域的状态允许过滤驱动程序检查当前文件的状态但是却没有什么有效而简单的方法确保过滤驱动程序检查时的状态与文件系统真正处理这个文件之间该文件状态保持不变。因此这样的情况很可能发生文件过滤驱动程序检查文件状态时文件是允许cache的但是真正处理的时候变为不允许cache了。相反的情况也同样会出现。详细的示例请看IFS的例子代码smbmrx/wnet/sys/openclos.c。
Topreventthestatefromchanging,thecallermustacquiretheFCBresource;inordertoavoiddeadlockswhilecallingtheredirector,itmustbeownedexclusive(usingtheERESOURCEintheFCBitself).Again,todothisrequiesrelyingu
pontheimplementationandpublishedinterfaceavailableintheIFSKit.
为了防止这种状态的变化调用者必须获取FCB的资源。为了避免调用redirector其间出现死锁现象调用者必须排他的拥有FCB资源。
Note:thissynchronizationisonlyneededforuserlevelcachedrequests,sincepagingI/Ooruserlevelnon-cachedrequestswillalreadynotbecachedasamatterofcourse.ThisisimportantbecausethislockcannotbesafelyacquiredwhenprocessingpagingI/O-thiswouldviolatetheexistinglockhierarchyandintroducethepossibilityofdeadlocks.
EventuallyIfiguredoutthiswasbecausenetworkredirectorsliketosetaninternalflagcalledSRVOPEN_FLAG_DONTUSE_WRITE_CACHEINGwhenafileisopenedforwrite-only,whichcausestheredirectortosendallwritesacrossthenetworkassoonasitgetsthem,bypasstheNTcache.Thismeansanylayeredfilterwillseetheordinarywriterequest,butneveracorrespondingpaging-I/Orequest.Togetaroundthis,myfilternowhastoforciblyturneverywrite-onlynetworkfileopenintoaread/writeopen.
当一个文件以只写write-only方式打开时网络重定向器会设置一个内部标志SRVOPEN_FLAG_DONTUSE_WRITE_CACHEING这将导致重定向器发送所有的写请求到网络文件服务器上绕过了NTcache机制。这意味着所有的分层过滤驱动程序只能看到普通的写请求但是不会看到任何对应的分页I/O请求。为了能够过滤网络文件的读写分页请求我的过滤驱动程序不得不强迫将所有的以只写write-only方式打开网络文件变为读写方式打开。
下面是如何将write-only打开的文件转换为读写打开的文件的方式
ThereasonwhyImrantinginpublicisthatseemsthatIcanneverknowa-prioriwhetherIwillseeareadorwriterequestasbothpagingandnon-pagingI/O,oroneortheother,foragivenfilesystem.Instead,Imustscsc cache win7pecialcasemycodeforeachfilesystemandpraythatIvecoveredeveryscenariothatcanresultinmynothandlingaread/writeor
handlingittwice.TheonlyalternativeIcancomeupwithistoforceALLreads/writestoafilteredfiletobenon-cached,withthecorrespondingperformancepenalties.Isthereanelegantwayoutofthismess?
IthinkIsentamessageaboutthisbefore.ThelongandtheshortofitisthatyouCANNOTforcetheredirectortocachewritesforfilesthatareopenwrite-only.Youmustinsteadtweakthefilepermissionstoconvertawrite-onlyopenofanetworkfileintoaread/writeopen.Usecodelikethefollowingtodoso:
if((0(desiredAccess(FILE_EXECUTE|FILE_READ_DATA)))(0!(desiredAccess(FILE_WRITE_DATA|
FILE_APPEND_DATA))))
{
pIrpSp-Parameters.Create.SecurityContext-DesiredAccess|FILE_READ_DATA;
}
下面解释了为什么write-only类型的文件不能执行cache
writeonlyhandleinredirectordoesntcache
--------------------------------------------------------------------------------
Igotbittenbythisafewmonthsago,sohereismytakeonthesituation...
ThisisbecausetheNTcacheneedsreadaccesstothefileinordertowork.Thecacheworksonpage-sizedchunks.Ifyouopenafileandwrite1byteatlocation0,andcachingisenabled,theNTcachewillpage-inthememorypagerepresentingthefirst4096bytesofthefile,whichrequiresittoissueapagingI/Oreadforthefirst4096bytes.Thenitwillpasteinyournewbyteandmarkthepagedirty,whichcausesthepagetobewrittenoutlaterbythelazywriter.
Forlocalfiles,pagingI/Oisallowedtobypassallsecuritychecks(sinceonlytrustedkernelcomponentscanissuepagingI/Orequests),sothatpagingreadsareallowedonallopensforallfiles.Fornetworkfiles,thereisnoreasontheremotePCshouldtrustyourPCandgrantitreadaccessifyouonlyhavewrite-accesstothefile.ThereforetheNTcachecannotbeusedonwrite-onlyremotefiles.
免责声明: 凡标注转载/编译字样内容并非本站原创,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如果你觉得本文好,欢迎推荐给朋友阅读;本文链接: https://m.nndssk.com/post/14884.html。

