question

Aaron-6686 avatar image
0 Votes"
Aaron-6686 asked XiaopoYang-MSFT answered

Does FSCTL_DISMOUNT_VOLUME flush the write cache for that volume?

I know that there is the "FlushFileBuffers" function that can flush the write cache for a volume. However, this requires Administrator privileges. Meanwhile, the documentation for FSCTL_LOCK_VOLUME says that "The system flushes all cached data to the volume before locking it." However, FSCTL_LOCK_VOLUME does not require Administrator privileges.

So I am currently using FSCTL_LOCK_VOLUME as the method to flush the write cache for a volume, and I am then dismounting the drive. However, if I want to force a dismount by only calling the IOCTL FSCTL_DISMOUNT_VOLUME(and not locking it first), will it still flush the volume in the same way as FSCTL_LOCK_VOLUME does?

Ultimately I am trying to programmatically prepare an ssd for hot swapping, by making sure the write cache is flushed and the volumes on the ssd are dismounted. If you have anything else to add concerning this use case, please feel free to add it.

windows-apic++windows-hardware-code-storage
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

XiaopoYang-MSFT avatar image
0 Votes"
XiaopoYang-MSFT answered

No. According to FSCTL_DISMOUNT_VOLUME Remarks,

The FSCTL_DISMOUNT_VOLUME control code will attempt to dismount a volume regardless of whether or not any other processes are using the volume, which can have unpredictable results for those processes if they do not hold a lock on the volume.

As far as the behavior concerned, when any other processes which do not hold a lock on the volume are using the volume, It’s useless even if SCTL_DISMOUNT_VOLUME flushing the write cache.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.