From 612d531dae3b878af8d2e110a805df6fff147695 Mon Sep 17 00:00:00 2001 From: localhorst Date: Fri, 12 Dec 2025 23:16:57 +0100 Subject: [PATCH] use O_DIRECT --- include/shred.h | 5 +++-- src/shred.cpp | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/include/shred.h b/include/shred.h index 635f265..044166e 100644 --- a/include/shred.h +++ b/include/shred.h @@ -19,6 +19,7 @@ #define CHUNK_SIZE 1024 * 1024 * 32 // amount of bytes that are overwritten at once --> 32MB #define TFNG_DATA_SIZE CHUNK_SIZE // amount of bytes used by tfng +#define O_DIRECT_PAGE_SIZE 4096 // needed page size for O_DIRECT // #define DEMO_DRIVE_SIZE 1024*1024*256L // 256MB // #define DEMO_DRIVE_SIZE 1024*1024*1024L // 1GB @@ -38,8 +39,8 @@ public: private: fileDescriptor randomSrcFileDiscr; fileDescriptor driveFileDiscr; - unsigned char caTfngData[TFNG_DATA_SIZE]; - unsigned char caReadBuffer[CHUNK_SIZE]; + unsigned char *caTfngData; + unsigned char *caReadBuffer; unsigned long ulDriveByteSize; unsigned long ulDriveByteOverallCount = 0; // all bytes shredded in all iterations + checking -> used for progress calculation double d32Percent = 0.0; diff --git a/src/shred.cpp b/src/shred.cpp index 6cddd6c..abff03d 100644 --- a/src/shred.cpp +++ b/src/shred.cpp @@ -21,10 +21,30 @@ const static char *randomsrc = (char *)"/dev/urandom"; Shred::Shred() { + // Allocate aligned buffers for O_DIRECT + if (posix_memalign((void **)&caTfngData, O_DIRECT_PAGE_SIZE, TFNG_DATA_SIZE) != 0) + { + Logger::logThis()->error("Failed to allocate aligned buffer for tfng data"); + caTfngData = nullptr; + } + + if (posix_memalign((void **)&caReadBuffer, O_DIRECT_PAGE_SIZE, CHUNK_SIZE) != 0) + { + Logger::logThis()->error("Failed to allocate aligned buffer for read buffer"); + caReadBuffer = nullptr; + } } Shred::~Shred() { + if (caTfngData != nullptr) + { + free(caTfngData); + } + if (caReadBuffer != nullptr) + { + free(caReadBuffer); + } } /** @@ -43,6 +63,12 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd) drive->u32DriveChecksumAfterShredding = UINT32_MAX; drive->state = Drive::TaskState::SHRED_ACTIVE; + if ((caTfngData == nullptr) || (caReadBuffer == nullptr)) + { + Logger::logThis()->error("Shred-Task: Aligned memory not available! - Drive: " + drive->getSerial()); + return -1; + } + #ifdef DRYRUN for (int i = 0; i <= 100; i++) { @@ -69,7 +95,13 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd) } // open disk - driveFileDiscr = open(cpDrivePath, O_RDWR | O_LARGEFILE); + driveFileDiscr = open(cpDrivePath, O_RDWR | O_LARGEFILE | O_DIRECT); + if (driveFileDiscr == -1 && errno == EINVAL) + { + driveFileDiscr = open(cpDrivePath, O_RDWR | O_LARGEFILE); + Logger::logThis()->warning("O_DIRECT not supported, using standard I/O - Drive: " + drive->getSerial()); + } + if (driveFileDiscr == -1) { std::string errorMsg(strerror(errno));