{"id":95505,"date":"2017-05-10T07:00:00","date_gmt":"2017-05-10T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=95505"},"modified":"2019-03-13T01:11:18","modified_gmt":"2019-03-13T08:11:18","slug":"20170510-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170510-00\/?p=95505","title":{"rendered":"Revised notes on the reliability of FlushFileBuffers"},"content":{"rendered":"<p>Some time ago, I wrote on <a HREF=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/\">the hard drives that lie about flushing file buffers (and the drivers who love them)<\/a>. Here&#8217;s a check-in on what&#8217;s happened over the past few years. <\/p>\n<p>As things stand today, you can rely on <code>Flush&shy;File&shy;Buffers<\/code> committing your changes to physical disk. (Noting of course that <a HREF=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/\">this operation is expensive and may not actually help you<\/a> if you aren&#8217;t updating your data transactionally.) <\/p>\n<p>Historically, NTFS used the <a HREF=\"https:\/\/msdn.microsoft.com\/library\/windows\/desktop\/dd979523(v=vs.85).aspx\">Force Unit Access flag<\/a> to indicate to the driver that it should wait until the information is committed to non-volatile storage before signaling that the operation has completed. The conventional technique to force writes to go to media is to pass <code>FILE_FLAG_WRITE_THROUGH<\/code>, which internally tells the file system to set the FUA flag on I\/O operations to that file. But as noted in <a HREF=\"http:\/\/perspectives.mvdirona.com\/2008\/04\/disks-lies-and-damn-disks\/\">this article<\/a>, in the Windows 7 time frame, EIDE and SATA drivers do not respect the FUA flag, which means that your writes may still be buffered by the drive&#8217;s internal memory. <\/p>\n<p>Fortunately, even with those drivers, the <code>Flush&shy;File&shy;Buffers<\/code> function will send the <code>FLUSH_CACHE<\/code> command, which tells the drive, &#8220;Hey, I know you have internal buffers. Look at me when I&#8217;m talking to you. Yes, you. With the internal buffers. Go flush them.&#8221; Fortunately, nearly all drivers in the Windows 7 era respect that command. (There are a few stragglers that still ignore it.) <\/p>\n<p>Fast-forward to Windows 8. Support for <code>FLUSH_CACHE<\/code> is required by all drives in order to be declared compatible with Windows 8, and the Windows storage team has worked with hard drive vendors to ensure that <code>FLUSH_CACHE<\/code> is properly respected. NTFS switched from using the FUA flag to the <code>FLUSH_CACHE<\/code> command to enforce consistency of its metadata. <\/p>\n<p>Note that if an application opens a file with the <code>FILE_FLAG_WRITE_THROUGH<\/code> flag, NTFS still sets the FUA flag on the write operations. But as noted above, EIDE and SATA drivers do not respect the FUA flag, so asking for <code>FILE_FLAG_WRITE_THROUGH<\/code> doesn&#8217;t give you any additional robustness in those cases. <\/p>\n<p>If you want to flush the data to physical media, the <code>Flush&shy;File&shy;Buffers<\/code> function is your ticket. <\/p>\n<p>As noted in the linked article, there are options in the UI to disable the signal to flush intermediate buffers, but you should use them only if you have a separate UPS for the hard drive, or if you simply don&#8217;t mind that the drive can get corrupted in the event of a power failure. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>A check-in on what&#8217;s happened over the past few years.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-95505","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A check-in on what&#8217;s happened over the past few years.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/95505","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=95505"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/95505\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=95505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=95505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=95505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}