mirror of
https://github.com/pbatard/rufus.git
synced 2025-05-24 19:54:25 -04:00
[core] switch to async I/O for image writing
* Combined with the increase in buffer size from previous commits, this should help us get close to a device's maximum write speed. * Also add async write support to winio.h * Also increase the buffer size for bad blocks check operations
This commit is contained in:
parent
80eca5739d
commit
a0904cad35
5 changed files with 121 additions and 84 deletions
97
src/format.c
97
src/format.c
|
@ -37,6 +37,7 @@
|
|||
#include "missing.h"
|
||||
#include "resource.h"
|
||||
#include "settings.h"
|
||||
#include "winio.h"
|
||||
#include "msapi_utf8.h"
|
||||
#include "localization.h"
|
||||
|
||||
|
@ -52,6 +53,9 @@
|
|||
#include "bled/bled.h"
|
||||
#include "../res/grub/grub_version.h"
|
||||
|
||||
/* Numbers of buffer used for asynchronous DD reads */
|
||||
#define NUM_BUFFERS 2
|
||||
|
||||
/*
|
||||
* Globals
|
||||
*/
|
||||
|
@ -1489,13 +1493,13 @@ static BOOL WriteDrive(HANDLE hPhysicalDrive, BOOL bZeroDrive)
|
|||
BOOL s, ret = FALSE;
|
||||
LARGE_INTEGER li;
|
||||
HANDLE hSourceImage = INVALID_HANDLE_VALUE;
|
||||
DWORD i, read_size, write_size, comp_size, buf_size;
|
||||
DWORD i, read_size[NUM_BUFFERS], write_size, comp_size, buf_size;
|
||||
uint64_t wb, target_size = bZeroDrive ? SelectedDrive.DiskSize : img_report.image_size;
|
||||
uint64_t cur_value, last_value = UINT64_MAX;
|
||||
int64_t bled_ret;
|
||||
uint8_t* buffer = NULL;
|
||||
uint32_t zero_data, *cmp_buffer = NULL;
|
||||
int throttle_fast_zeroing = 0;
|
||||
int throttle_fast_zeroing = 0, read_bufnum = 0, proc_bufnum = 1;
|
||||
|
||||
if (SelectedDrive.SectorSize < 512) {
|
||||
uprintf("Unexpected sector size (%d) - Aborting", SelectedDrive.SectorSize);
|
||||
|
@ -1533,7 +1537,7 @@ static BOOL WriteDrive(HANDLE hPhysicalDrive, BOOL bZeroDrive)
|
|||
assert((uintptr_t)cmp_buffer % SelectedDrive.SectorSize == 0);
|
||||
}
|
||||
|
||||
read_size = buf_size;
|
||||
read_size[0] = buf_size;
|
||||
for (wb = 0, write_size = 0; wb < target_size; wb += write_size) {
|
||||
UpdateProgressWithInfo(OP_FORMAT, fast_zeroing ? MSG_306 : MSG_286, wb, target_size);
|
||||
cur_value = (wb * min(80, target_size)) / target_size;
|
||||
|
@ -1542,13 +1546,13 @@ static BOOL WriteDrive(HANDLE hPhysicalDrive, BOOL bZeroDrive)
|
|||
uprintfs("+");
|
||||
}
|
||||
// Don't overflow our projected size (mostly for VHDs)
|
||||
if (wb + read_size > target_size) {
|
||||
read_size = (DWORD)(target_size - wb);
|
||||
if (wb + read_size[0] > target_size) {
|
||||
read_size[0] = (DWORD)(target_size - wb);
|
||||
}
|
||||
|
||||
// WriteFile fails unless the size is a multiple of sector size
|
||||
if (read_size % SelectedDrive.SectorSize != 0)
|
||||
read_size = ((read_size + SelectedDrive.SectorSize - 1) / SelectedDrive.SectorSize) * SelectedDrive.SectorSize;
|
||||
if (read_size[0] % SelectedDrive.SectorSize != 0)
|
||||
read_size[0] = ((read_size[0] + SelectedDrive.SectorSize - 1) / SelectedDrive.SectorSize) * SelectedDrive.SectorSize;
|
||||
|
||||
// Fast-zeroing: Depending on your hardware, reading from flash may be much faster than writing, so
|
||||
// we might speed things up by skipping empty blocks, or skipping the write if the data is the same.
|
||||
|
@ -1561,8 +1565,8 @@ static BOOL WriteDrive(HANDLE hPhysicalDrive, BOOL bZeroDrive)
|
|||
CHECK_FOR_USER_CANCEL;
|
||||
|
||||
// Read block and compare against the block that needs to be written
|
||||
s = ReadFile(hPhysicalDrive, cmp_buffer, read_size, &comp_size, NULL);
|
||||
if ((!s) || (comp_size != read_size)) {
|
||||
s = ReadFile(hPhysicalDrive, cmp_buffer, read_size[0], &comp_size, NULL);
|
||||
if ((!s) || (comp_size != read_size[0])) {
|
||||
uprintf("Read error: Could not read data for fast zeroing comparison - %s", WindowsErrorString());
|
||||
goto out;
|
||||
}
|
||||
|
@ -1572,10 +1576,10 @@ static BOOL WriteDrive(HANDLE hPhysicalDrive, BOOL bZeroDrive)
|
|||
// Check all bits are the same
|
||||
if ((zero_data == 0) || (zero_data == 0xffffffff)) {
|
||||
// Compare the rest of the block against the first element
|
||||
for (i = 1; (i < read_size / sizeof(uint32_t)) && (cmp_buffer[i] == zero_data); i++);
|
||||
if (i >= read_size / sizeof(uint32_t)) {
|
||||
for (i = 1; (i < read_size[0] / sizeof(uint32_t)) && (cmp_buffer[i] == zero_data); i++);
|
||||
if (i >= read_size[0] / sizeof(uint32_t)) {
|
||||
// Block is empty, skip write
|
||||
write_size = read_size;
|
||||
write_size = read_size[0];
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
@ -1592,11 +1596,11 @@ static BOOL WriteDrive(HANDLE hPhysicalDrive, BOOL bZeroDrive)
|
|||
|
||||
for (i = 1; i <= WRITE_RETRIES; i++) {
|
||||
CHECK_FOR_USER_CANCEL;
|
||||
s = WriteFile(hPhysicalDrive, buffer, read_size, &write_size, NULL);
|
||||
if ((s) && (write_size == read_size))
|
||||
s = WriteFile(hPhysicalDrive, buffer, read_size[0], &write_size, NULL);
|
||||
if ((s) && (write_size == read_size[0]))
|
||||
break;
|
||||
if (s)
|
||||
uprintf("Write error: Wrote %d bytes, expected %d bytes", write_size, read_size);
|
||||
uprintf("Write error: Wrote %d bytes, expected %d bytes", write_size, read_size[0]);
|
||||
else
|
||||
uprintf("Write error at sector %lld: %s", wb / SelectedDrive.SectorSize, WindowsErrorString());
|
||||
if (i < WRITE_RETRIES) {
|
||||
|
@ -1652,9 +1656,9 @@ static BOOL WriteDrive(HANDLE hPhysicalDrive, BOOL bZeroDrive)
|
|||
goto out;
|
||||
}
|
||||
} else {
|
||||
hSourceImage = CreateFileU(image_path, GENERIC_READ, FILE_SHARE_READ, NULL,
|
||||
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
|
||||
if (hSourceImage == INVALID_HANDLE_VALUE) {
|
||||
hSourceImage = CreateFileAsync(image_path, GENERIC_READ, FILE_SHARE_READ,
|
||||
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN);
|
||||
if (hSourceImage == NULL) {
|
||||
uprintf("Could not open image '%s': %s", image_path, WindowsErrorString());
|
||||
FormatStatus = ERROR_SEVERITY_ERROR | FAC(FACILITY_STORAGE) | ERROR_OPEN_FAILED;
|
||||
goto out;
|
||||
|
@ -1662,7 +1666,7 @@ static BOOL WriteDrive(HANDLE hPhysicalDrive, BOOL bZeroDrive)
|
|||
|
||||
// Our buffer size must be a multiple of the sector size and *ALIGNED* to the sector size
|
||||
buf_size = ((DD_BUFFER_SIZE + SelectedDrive.SectorSize - 1) / SelectedDrive.SectorSize) * SelectedDrive.SectorSize;
|
||||
buffer = (uint8_t*)_mm_malloc(buf_size, SelectedDrive.SectorSize);
|
||||
buffer = (uint8_t*)_mm_malloc(buf_size * NUM_BUFFERS, SelectedDrive.SectorSize);
|
||||
if (buffer == NULL) {
|
||||
FormatStatus = ERROR_SEVERITY_ERROR | FAC(FACILITY_STORAGE) | ERROR_NOT_ENOUGH_MEMORY;
|
||||
uprintf("Could not allocate disk write buffer");
|
||||
|
@ -1670,41 +1674,51 @@ static BOOL WriteDrive(HANDLE hPhysicalDrive, BOOL bZeroDrive)
|
|||
}
|
||||
assert((uintptr_t)buffer % SelectedDrive.SectorSize == 0);
|
||||
|
||||
// Don't bother trying for something clever, using double buffering overlapped and whatnot:
|
||||
// With Windows' default optimizations, sync read + sync write for sequential operations
|
||||
// will be as fast, if not faster, than whatever async scheme you can come up with.
|
||||
read_size = buf_size;
|
||||
for (wb = 0, write_size = 0; wb < target_size; wb += write_size) {
|
||||
// Start the initial read
|
||||
ReadFileAsync(hSourceImage, &buffer[read_bufnum * buf_size], buf_size);
|
||||
|
||||
read_size[proc_bufnum] = 1; // To avoid early loop exit
|
||||
for (wb = 0; read_size[proc_bufnum] != 0; wb += read_size[proc_bufnum]) {
|
||||
// 0. Update the progress
|
||||
UpdateProgressWithInfo(OP_FORMAT, MSG_261, wb, target_size);
|
||||
cur_value = (wb * min(80, target_size)) / target_size;
|
||||
if (cur_value != last_value) {
|
||||
last_value = cur_value;
|
||||
uprintfs("+");
|
||||
}
|
||||
s = ReadFile(hSourceImage, buffer, buf_size, &read_size, NULL);
|
||||
if (!s) {
|
||||
FormatStatus = ERROR_SEVERITY_ERROR | FAC(FACILITY_STORAGE) | ERROR_READ_FAULT;
|
||||
|
||||
// 1. Wait for the current read operation to complete (and update the read size)
|
||||
if ((!WaitFileAsync(hSourceImage, DRIVE_ACCESS_TIMEOUT)) ||
|
||||
(!GetSizeAsync(hSourceImage, &read_size[read_bufnum]))) {
|
||||
uprintf("Read error: %s", WindowsErrorString());
|
||||
FormatStatus = ERROR_SEVERITY_ERROR | FAC(FACILITY_STORAGE) | ERROR_READ_FAULT;
|
||||
goto out;
|
||||
}
|
||||
if (read_size == 0)
|
||||
break;
|
||||
// Don't overflow our projected size (mostly for VHDs)
|
||||
if (wb + read_size > target_size) {
|
||||
read_size = (DWORD)(target_size - wb);
|
||||
}
|
||||
|
||||
// WriteFile fails unless the size is a multiple of sector size
|
||||
if (read_size % SelectedDrive.SectorSize != 0)
|
||||
read_size = ((read_size + SelectedDrive.SectorSize - 1) / SelectedDrive.SectorSize) * SelectedDrive.SectorSize;
|
||||
// 2. Update the read size
|
||||
// 2a) Don't overflow our projected size (mostly for VHDs)
|
||||
if (wb + read_size[read_bufnum] > target_size)
|
||||
read_size[read_bufnum] = (DWORD)(target_size - wb);
|
||||
// 2b) WriteFile fails unless the size is a multiple of sector size
|
||||
if (read_size[read_bufnum] % SelectedDrive.SectorSize != 0)
|
||||
read_size[read_bufnum] = ((read_size[read_bufnum] + SelectedDrive.SectorSize - 1) /
|
||||
SelectedDrive.SectorSize) * SelectedDrive.SectorSize;
|
||||
|
||||
// 3. Switch to the next reading buffer
|
||||
proc_bufnum = read_bufnum;
|
||||
read_bufnum = (read_bufnum + 1) % NUM_BUFFERS;
|
||||
|
||||
// 3. Launch the next asynchronous read operation
|
||||
ReadFileAsync(hSourceImage, &buffer[read_bufnum * buf_size], buf_size);
|
||||
|
||||
// 4. Synchronously write the current data buffer
|
||||
for (i = 1; i <= WRITE_RETRIES; i++) {
|
||||
CHECK_FOR_USER_CANCEL;
|
||||
s = WriteFile(hPhysicalDrive, buffer, read_size, &write_size, NULL);
|
||||
if ((s) && (write_size == read_size))
|
||||
s = WriteFile(hPhysicalDrive, &buffer[proc_bufnum * buf_size], read_size[proc_bufnum], &write_size, NULL);
|
||||
if ((s) && (write_size == read_size[proc_bufnum]))
|
||||
break;
|
||||
if (s)
|
||||
uprintf("Write error: Wrote %d bytes, expected %d bytes", write_size, read_size);
|
||||
uprintf("Write error: Wrote %d bytes, expected %d bytes", write_size, read_size[proc_bufnum]);
|
||||
else
|
||||
uprintf("Write error at sector %lld: %s", wb / SelectedDrive.SectorSize, WindowsErrorString());
|
||||
if (i < WRITE_RETRIES) {
|
||||
|
@ -1729,7 +1743,10 @@ static BOOL WriteDrive(HANDLE hPhysicalDrive, BOOL bZeroDrive)
|
|||
RefreshDriveLayout(hPhysicalDrive);
|
||||
ret = TRUE;
|
||||
out:
|
||||
safe_closehandle(hSourceImage);
|
||||
if (img_report.compression_type != BLED_COMPRESSION_NONE)
|
||||
safe_closehandle(hSourceImage);
|
||||
else
|
||||
CloseFileAsync(hSourceImage);
|
||||
safe_mm_free(buffer);
|
||||
safe_mm_free(cmp_buffer);
|
||||
return ret;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue