|
| 1 | +//! Disable write cache flushing (i.e. write-durability guarantees) on Windows. |
| 2 | +//! Only run this on ephemeral machines, otherwise it may cause data loss. |
| 3 | +use std::ffi::CString; |
| 4 | +use std::ptr::{null, null_mut}; |
| 5 | +use std::{mem, ptr}; |
| 6 | + |
| 7 | +use windows_sys::Win32::Foundation::{GENERIC_READ, GENERIC_WRITE, INVALID_HANDLE_VALUE}; |
| 8 | +use windows_sys::Win32::Storage::FileSystem::{ |
| 9 | + CreateFileA, FILE_SHARE_READ, FILE_SHARE_WRITE, OPEN_EXISTING, |
| 10 | +}; |
| 11 | +use windows_sys::Win32::System::IO::DeviceIoControl; |
| 12 | + |
| 13 | +const IOCTL_DISK_GET_CACHE_SETTINGS: u32 = 0x740e0; |
| 14 | +const IOCTL_DISK_SET_CACHE_SETTINGS: u32 = 0x7c0e4; |
| 15 | + |
| 16 | +#[repr(C)] |
| 17 | +struct DiskCacheSettings { |
| 18 | + pub version: u32, |
| 19 | + pub state: u32, |
| 20 | + pub is_power_protected: u8, |
| 21 | +} |
| 22 | + |
| 23 | +fn main() { |
| 24 | + // 3 drives ought to be enough for everyone |
| 25 | + for i in 0..=2 { |
| 26 | + unsafe { |
| 27 | + let disk = format!(r#"\\.\PHYSICALDRIVE{}"#, i); |
| 28 | + let hd1 = CString::new(disk.clone()).unwrap(); |
| 29 | + let handle = CreateFileA( |
| 30 | + hd1.as_ptr().cast(), |
| 31 | + GENERIC_READ | GENERIC_WRITE, |
| 32 | + FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 33 | + null(), |
| 34 | + OPEN_EXISTING, |
| 35 | + 0, |
| 36 | + 0, |
| 37 | + ); |
| 38 | + |
| 39 | + if handle == INVALID_HANDLE_VALUE { |
| 40 | + let error_code = windows_sys::Win32::Foundation::GetLastError(); |
| 41 | + eprintln!("Failed to open device {disk}, {:x}", error_code); |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + let mut settings: DiskCacheSettings = mem::zeroed(); |
| 46 | + let mut lpbytesreturned: u32 = 0; |
| 47 | + |
| 48 | + let r = DeviceIoControl( |
| 49 | + handle, |
| 50 | + IOCTL_DISK_GET_CACHE_SETTINGS, |
| 51 | + null(), |
| 52 | + 0, |
| 53 | + ptr::from_mut(&mut settings).cast(), |
| 54 | + size_of::<DiskCacheSettings>() as u32, |
| 55 | + &mut lpbytesreturned, |
| 56 | + null_mut(), |
| 57 | + ); |
| 58 | + if r == 0 { |
| 59 | + eprintln!("Failed to get cache settings"); |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + // Enable YOLO mode |
| 64 | + settings.is_power_protected = 1; |
| 65 | + |
| 66 | + let r = DeviceIoControl( |
| 67 | + handle, |
| 68 | + IOCTL_DISK_SET_CACHE_SETTINGS, |
| 69 | + ptr::from_ref(&settings).cast(), |
| 70 | + size_of::<DiskCacheSettings>() as u32, |
| 71 | + null_mut(), |
| 72 | + 0, |
| 73 | + &mut lpbytesreturned, |
| 74 | + null_mut(), |
| 75 | + ); |
| 76 | + if r == 0 { |
| 77 | + eprintln!("Failed to set cache settings"); |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + // verify that it sticks |
| 82 | + |
| 83 | + let r = DeviceIoControl( |
| 84 | + handle, |
| 85 | + IOCTL_DISK_GET_CACHE_SETTINGS, |
| 86 | + null(), |
| 87 | + 0, |
| 88 | + ptr::from_mut(&mut settings).cast(), |
| 89 | + size_of::<DiskCacheSettings>() as u32, |
| 90 | + &mut lpbytesreturned, |
| 91 | + null_mut(), |
| 92 | + ); |
| 93 | + |
| 94 | + if r == 0 { |
| 95 | + eprintln!("Failed to get cache settings the second time"); |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + if settings.is_power_protected != 1 { |
| 100 | + eprintln!( |
| 101 | + "Failed to enable YOLO mode for drive {i}, is_power_protected was not retained" |
| 102 | + ); |
| 103 | + } else { |
| 104 | + eprintln!("YOLO mode enabled successfully for drive {i}!"); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments