use O_DIRECT

This commit is contained in:
2025-12-12 23:16:57 +01:00
parent 8a42ccf9c0
commit 612d531dae
2 changed files with 36 additions and 3 deletions

View File

@ -19,6 +19,7 @@
#define CHUNK_SIZE 1024 * 1024 * 32 // amount of bytes that are overwritten at once --> 32MB #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 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*256L // 256MB
// #define DEMO_DRIVE_SIZE 1024*1024*1024L // 1GB // #define DEMO_DRIVE_SIZE 1024*1024*1024L // 1GB
@ -38,8 +39,8 @@ public:
private: private:
fileDescriptor randomSrcFileDiscr; fileDescriptor randomSrcFileDiscr;
fileDescriptor driveFileDiscr; fileDescriptor driveFileDiscr;
unsigned char caTfngData[TFNG_DATA_SIZE]; unsigned char *caTfngData;
unsigned char caReadBuffer[CHUNK_SIZE]; unsigned char *caReadBuffer;
unsigned long ulDriveByteSize; unsigned long ulDriveByteSize;
unsigned long ulDriveByteOverallCount = 0; // all bytes shredded in all iterations + checking -> used for progress calculation unsigned long ulDriveByteOverallCount = 0; // all bytes shredded in all iterations + checking -> used for progress calculation
double d32Percent = 0.0; double d32Percent = 0.0;

View File

@ -21,10 +21,30 @@ const static char *randomsrc = (char *)"/dev/urandom";
Shred::Shred() 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() 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->u32DriveChecksumAfterShredding = UINT32_MAX;
drive->state = Drive::TaskState::SHRED_ACTIVE; 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 #ifdef DRYRUN
for (int i = 0; i <= 100; i++) for (int i = 0; i <= 100; i++)
{ {
@ -69,7 +95,13 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
} }
// open disk // open disk
driveFileDiscr = open(cpDrivePath, O_RDWR | O_LARGEFILE | O_DIRECT);
if (driveFileDiscr == -1 && errno == EINVAL)
{
driveFileDiscr = open(cpDrivePath, O_RDWR | O_LARGEFILE); driveFileDiscr = open(cpDrivePath, O_RDWR | O_LARGEFILE);
Logger::logThis()->warning("O_DIRECT not supported, using standard I/O - Drive: " + drive->getSerial());
}
if (driveFileDiscr == -1) if (driveFileDiscr == -1)
{ {
std::string errorMsg(strerror(errno)); std::string errorMsg(strerror(errno));