2021-03-14 19:00:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-03-14 19:00:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Random.h>
|
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
bool verify_block(int fd, int seed, off_t block, AK::ByteBuffer& buffer);
|
|
|
|
bool write_block(int fd, int seed, off_t block, AK::ByteBuffer& buffer);
|
|
|
|
|
|
|
|
bool verify_block(int fd, int seed, off_t block, AK::ByteBuffer& buffer)
|
|
|
|
{
|
|
|
|
auto offset = block * buffer.size();
|
|
|
|
auto rs = lseek(fd, offset, SEEK_SET);
|
|
|
|
if (rs < 0) {
|
2021-05-01 22:00:52 +00:00
|
|
|
fprintf(stderr, "Couldn't seek to block %" PRIi64 " (offset %" PRIi64 ") while verifying: %s\n", block, offset, strerror(errno));
|
2021-03-14 19:00:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto rw = read(fd, buffer.data(), buffer.size());
|
|
|
|
if (rw != static_cast<int>(buffer.size())) {
|
2021-05-01 22:00:52 +00:00
|
|
|
fprintf(stderr, "Failure to read block %" PRIi64 ": %s\n", block, strerror(errno));
|
2021-03-14 19:00:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
srand((seed + 1) * (block + 1));
|
|
|
|
for (size_t i = 0; i < buffer.size(); i++) {
|
|
|
|
if (buffer[i] != rand() % 256) {
|
2021-05-01 22:00:52 +00:00
|
|
|
fprintf(stderr, "Discrepancy detected at block %" PRIi64 " offset %zd\n", block, i);
|
2021-03-14 19:00:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool write_block(int fd, int seed, off_t block, AK::ByteBuffer& buffer)
|
|
|
|
{
|
|
|
|
auto offset = block * buffer.size();
|
|
|
|
auto rs = lseek(fd, offset, SEEK_SET);
|
|
|
|
if (rs < 0) {
|
2021-05-01 22:00:52 +00:00
|
|
|
fprintf(stderr, "Couldn't seek to block %" PRIi64 " (offset %" PRIi64 ") while verifying: %s\n", block, offset, strerror(errno));
|
2021-03-14 19:00:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
srand((seed + 1) * (block + 1));
|
|
|
|
for (size_t i = 0; i < buffer.size(); i++)
|
|
|
|
buffer[i] = rand();
|
|
|
|
auto rw = write(fd, buffer.data(), buffer.size());
|
|
|
|
if (rw != static_cast<int>(buffer.size())) {
|
2021-05-01 22:00:52 +00:00
|
|
|
fprintf(stderr, "Failure to write block %" PRIi64 ": %s\n", block, strerror(errno));
|
2021-03-14 19:00:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2023-02-21 11:44:41 +00:00
|
|
|
Vector<StringView> arguments;
|
|
|
|
arguments.ensure_capacity(argc);
|
|
|
|
for (auto i = 0; i < argc; ++i)
|
|
|
|
arguments.append({ argv[i], strlen(argv[i]) });
|
|
|
|
|
2023-02-28 20:41:43 +00:00
|
|
|
DeprecatedString target;
|
2021-03-14 19:00:25 +00:00
|
|
|
int min_block_offset = 0;
|
|
|
|
int block_length = 2048;
|
|
|
|
int block_size = 512;
|
|
|
|
int count = 1024;
|
|
|
|
int rng_seed = 0;
|
|
|
|
bool paranoid_mode = false;
|
|
|
|
bool random_mode = false;
|
|
|
|
bool stop_mode = false;
|
|
|
|
bool uninitialized_mode = false;
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_option(min_block_offset, "Minimum block offset to consider", "min-offset", 'o', "size");
|
|
|
|
args_parser.add_option(block_length, "Number of blocks to consider", "length", 's', "size");
|
|
|
|
args_parser.add_option(block_size, "Block size", "block-size", 'b', "size");
|
|
|
|
args_parser.add_option(count, "Number of write/read cycles to run", "number", 'n', "number");
|
|
|
|
args_parser.add_option(rng_seed, "Random number generator seed", "seed", 'S', "number");
|
|
|
|
args_parser.add_option(paranoid_mode, "Check entire range for consistency after each write", "paranoid", 'p');
|
|
|
|
args_parser.add_option(random_mode, "Write one block inside range at random", "random", 'r');
|
|
|
|
args_parser.add_option(stop_mode, "Stop after first error", "abort-on-error", 'a');
|
|
|
|
args_parser.add_option(uninitialized_mode, "Don't pre-initialize block range", "uninitialized", 'u');
|
|
|
|
args_parser.add_positional_argument(target, "Target device/file path", "target");
|
2023-02-21 11:44:41 +00:00
|
|
|
args_parser.parse(arguments);
|
2021-03-14 19:00:25 +00:00
|
|
|
|
2021-09-05 22:59:52 +00:00
|
|
|
auto buffer_result = AK::ByteBuffer::create_zeroed(block_size);
|
2022-01-20 17:47:39 +00:00
|
|
|
if (buffer_result.is_error()) {
|
2021-09-05 22:59:52 +00:00
|
|
|
warnln("Failed to allocate a buffer of {} bytes", block_size);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
auto buffer = buffer_result.release_value();
|
2021-03-14 19:00:25 +00:00
|
|
|
|
2023-02-28 20:41:43 +00:00
|
|
|
int fd = open(target.characters(), O_CREAT | O_RDWR, 0666);
|
2021-03-14 19:00:25 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
perror("Couldn't create target file");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!uninitialized_mode) {
|
|
|
|
int old_percent = -100;
|
|
|
|
for (int i = min_block_offset; i < min_block_offset + block_length; i++) {
|
|
|
|
int percent;
|
|
|
|
if (block_length <= 1)
|
|
|
|
percent = 100;
|
|
|
|
else
|
|
|
|
percent = 100 * (i - min_block_offset) / (block_length - 1);
|
|
|
|
if (old_percent != percent) {
|
|
|
|
printf("Pre-initializing entire block range (%3d%%)...\n", percent);
|
|
|
|
old_percent = percent;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!write_block(fd, rng_seed, i, buffer))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int r = EXIT_SUCCESS;
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
printf("(%d/%d)\tPass %d...\n", i + 1, count, i + 1);
|
|
|
|
|
|
|
|
for (int j = min_block_offset; j < min_block_offset + block_length; j++) {
|
|
|
|
off_t block;
|
|
|
|
if (random_mode)
|
|
|
|
while ((block = AK::get_random<off_t>()) < 0)
|
|
|
|
;
|
|
|
|
else
|
|
|
|
block = j;
|
|
|
|
block = min_block_offset + block % block_length;
|
|
|
|
|
|
|
|
if (paranoid_mode) {
|
|
|
|
for (int k = min_block_offset; j < min_block_offset + block_length; j++) {
|
|
|
|
if (!verify_block(fd, rng_seed, k, buffer)) {
|
|
|
|
if (stop_mode)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
else
|
|
|
|
r = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!verify_block(fd, rng_seed, block, buffer)) {
|
|
|
|
if (stop_mode)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
else
|
|
|
|
r = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!write_block(fd, rng_seed, block, buffer)) {
|
|
|
|
if (stop_mode)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
else
|
|
|
|
r = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return r;
|
|
|
|
}
|