#1 优盘如何热插拔

在win2k/2k3这样的早期系统上面,优盘热插拔需要在设备管理器的优盘设备上面手动设置 -- 禁用写入缓存。在windows 7上面,操作系统自动识别优盘,无需设置即可热插拔。
我们知道,根据局部性原理,存储体系参与者众多,从CPU内部最快的寄存器开始,又有CPU高速缓存L1、L2&&L3,然后再到RAM,SSD,HDD。今天的磁盘上面也拥有片上存储器,因为读磁盘的代价很大,读操作一旦开始就不会只读几个字节而罢休,而是读取适量数据放入缓冲区。

存储器层次结构特点就是价格越高容量越小,最终构成的存储器层次结构就是cost/performance平衡后的产物:大容量、高速度的存储器。更多信息可以参考david patterson的COD及其辅助材料。

显然这是一层复杂的抽象,我们也知道,抽象会有失效的时候。皇帝依靠大臣治理天下,他会谨慎授权,类似于嬴政与王翦灭楚之类的事情,最终这套法则漏洞越来越多,于是重起炉灶。或者像汽车,汽车的雨刷抽象了天气,平时你不需要注意这些,但雨刷失效的时候你就不得不来处理它了。

下面是收集到的一些关于写入缓存的材料。

备注1、The Disk Properties Tab "Write Cache Enabled" Feature 
If you enable this feature, your computer sends an enable-write-cache command to the hard disk activating the hard disk write-back cache, and if you disable this feature, the hard disk write-back cache  is deactivated.

When you enable this feature, you receive a warning message that says enabling this option could lead to file system damage or data loss. This could happen if a computer or power failure occurs that would prevent your computer from shutting down properly. This is because until the cached data is written to disk, the last few write operations were reported to the operating system as being complete by the hardware, but the hardware still contains the data in its physical memory. Should a power loss occur, those last few write operations may not take place, possibly leading to data loss or file system damage.

备注2、 通常可使用两个命令强制将缓存的数据立即写入物理磁盘       

  • flush buffers 命令(用于 SCSI 和 IDE/ATAPI 磁盘设备)会指示磁盘立即将所有缓存数据写入磁盘。对于 SCSI 磁盘,这是通过向磁盘发出 SYNCHRONIZE CACHE 命令实现的。对于 IDE/ATAPI 磁盘,会向磁盘发出 FLUSH CACHE 命令。此命令一般是作为 Windows 程序调用 FlushFileBuffers API 的结果而发出的。写入注册表就是导致调用 FlushFileBuffers API 和向磁盘发出 SYNCHRONIZE CACHE 命令的操作的一个实例。
  • 写入命令(只用于 SCSI 磁盘设备)通过向具有 ForceUnitAccess (FUA) 位设置的磁盘发出 WRITE 命令来实现。此类命令指示磁盘将当前的数据包立即写入磁盘,而绕过机载写入缓存。这通常是调用 WriteFile API 的 Windows 程序向它已打开(通过调用设置了 FILE_FLAG_WRITE_THROUGH 标志的 CreateFile API)的文件写入的结果。病毒扫描软件通常都在设置 FILE_FLAG_WRITE_THROUGH 标志的情况下打开文件。对 FUA 位的支持是可选的,只有一些 SCSI 和光纤信道 (FC) 设备(通常是单个的驱动器,而非 RAID 阵列)实现了此功能。

备注3、前两条备注都来自于windows 2000知识库文件,应当注意到其时效性。

备注4、Flushing your performance down the drain, that is

备注5、查询MSDN&&windows via c/c++,windows via c/c++上面只有短短的几句话,差不多和没说一个效果。下面是MSDN的解释:

Due to disk caching interactions within the system, the FlushFileBuffers function can be inefficient
when used after every write to a disk drive device when many writes are being
performed separately. If an application is performing multiple writes to disk
and also needs to ensure critical data is written to persistent media, the
application should use unbuffered I/O instead of frequently calling FlushFileBuffers. To open a file for unbuffered
I/O, call the CreateFile function with the FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags. This prevents
the file contents from being cached and flushes the metadata to disk with each
write.

#2 总结,以windows 7为例

不打开写缓存:优盘可以自由热插拔,文件会直接写入磁盘。

打开写缓存不打开"Turn off Windows write-cache buffer flushing on the device":除非使用FlushFileBuffers否则是否写入磁盘要看磁盘的心情。

打开写缓存并打开"Turn off Windows write-cache buffer flushing on the device":即便使用了FlushFileBuffers是否写入磁盘也要看磁盘的心情。

以上如有错误,请指正。

另外本文是因为看了另一篇博文而写:文件是否真的写入了磁盘?