I am working on a feature in which the DB resides on an external storage device such as a Fibre channel drive. As part of the shutdown process, I want to take the storage drive offline. Currently I do it this way (code is in C++ but this is only a high level description):
For each volume in the disk:
Flush it (by calling FlushFileBuffers with its handle)
Lock it (via calling DeviceIoControl with the FSCTL_LOCK_VOLUME code)
Dismount it (via the FSCTL_DISMOUNT_VOLUME code)
Take it offline (via the IOCTL_VOLUME_OFFLINE code)
Only if all the volumes associated with the disk go through the series of the above 4 steps successfully, I take the disk offline, by using the IOTCTL_DISK_SET_DISK_ATTRIBUTES and IOCTL_UPDATE_DISK_PROPERTIES control codes.
The problem I ran into is that very frequently, users open files that reside on the external disk via other programs such as notepad. In that case, step 2 - Locking the volume - will fail and in turn this will fail the entire shutdown attempt, which causes the program to get stuck because I don't allow the shutdown to complete without the lock being successful. The reason is that from what I understand, this is the only reliable way to force a full flush of all unwritten data to the disk, which is essential in preventing the disk from being in an inconsistent state (files ending up as half-written, etc.).
So basically I'd like to know if there's a reliable way to guarantee a full flush of data to the disk without locking its volumes. I noticed that for example the MS Failover Cluster doesn't lock any volume when taking a shared disk offline, does anyone happen to know how that works?
Thanks!
Amit
5 Related questions 32 Safely remove a USB drive using the Win32 API? 0 Locking removable drive? 8 Graceful File Reading without Locking Related questions 32 Safely remove a USB drive using the Win32 API? 0 Locking removable drive? 8 Graceful File Reading without Locking 22 Spin Down Hard Disk Programmatically on Windows? 1 How do I release an external drive after it's been accessed by my program? 6 Take disks online/offline 9 How to programatically prevent Windows from hard disk drive spin down? 1 How do I write data to a volume locked with FSCTL_LOCK_VOLUME? 1 Locking a drive with FSCTL_LOCK_VOLUME 3 How to explicitly lock a mounted file system? Load 7 more related questions Show fewer related questions Reset to default