diff --git a/bd/lfs3_emubd.c b/bd/lfs3_emubd.c index b627c163..f72d5fe9 100644 --- a/bd/lfs3_emubd.c +++ b/bd/lfs3_emubd.c @@ -166,23 +166,52 @@ static uint32_t lfs3_emubd_prng_(uint32_t *state) { int lfs3_emubd_createcfg(const struct lfs3_cfg *cfg, const char *path, const struct lfs3_emubd_cfg *bdcfg) { - LFS3_EMUBD_TRACE("lfs3_emubd_createcfg(%p {.context=%p, " - ".read=%p, .prog=%p, .erase=%p, .sync=%p, " - ".read_size=%"PRIu32", .prog_size=%"PRIu32", " - ".block_size=%"PRIu32", .block_count=%"PRIu32"}, " + LFS3_EMUBD_TRACE("lfs3_emubd_createcfg(" + "%p {" + ".context=%p, " + ".read=%p, " + ".prog=%p, " + ".erase=%p, " + ".sync=%p, " + ".read_size=%"PRIu32", " + ".prog_size=%"PRIu32", " + ".block_size=%"PRIu32", " + ".block_count=%"PRIu32"}, " "\"%s\", " - "%p {.erase_value=%"PRId32", .erase_cycles=%"PRIu32", " - ".badblock_behavior=%"PRIu8", .power_cycles=%"PRIu32", " - ".powerloss_behavior=%"PRIu8", .powerloss_cb=%p, " - ".powerloss_data=%p, seed=%"PRIu32"})", - (void*)cfg, cfg->context, - (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog, - (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync, - cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count, - path, (void*)bdcfg, bdcfg->erase_value, bdcfg->erase_cycles, - bdcfg->badblock_behavior, bdcfg->power_cycles, - bdcfg->powerloss_behavior, (void*)(uintptr_t)bdcfg->powerloss_cb, - bdcfg->powerloss_data, bdcfg->seed); + "%p {.erase_value=%"PRId32", " + ".erase_cycles=%"PRIu32", " + ".badblock_behavior=%"PRIu8", " + ".power_cycles=%"PRIu32", " + ".powerloss_behavior=%"PRIu8", " + ".powerloss_cb=%p, " + ".powerloss_data=%p, " + ".seed=%"PRIu32", " + ".read_sleep=%"PRIu64", " + ".prog_sleep=%"PRIu64", " + ".erase_sleep=%"PRIu64"})", + (void*)cfg, + cfg->context, + (void*)(uintptr_t)cfg->read, + (void*)(uintptr_t)cfg->prog, + (void*)(uintptr_t)cfg->erase, + (void*)(uintptr_t)cfg->sync, + cfg->read_size, + cfg->prog_size, + cfg->block_size, + cfg->block_count, + path, + (void*)bdcfg, + bdcfg->erase_value, + bdcfg->erase_cycles, + bdcfg->badblock_behavior, + bdcfg->power_cycles, + bdcfg->powerloss_behavior, + (void*)(uintptr_t)bdcfg->powerloss_cb, + bdcfg->powerloss_data, + bdcfg->seed, + bdcfg->read_sleep, + bdcfg->prog_sleep, + bdcfg->erase_sleep); lfs3_emubd_t *bd = cfg->context; bd->cfg = bdcfg; @@ -229,7 +258,7 @@ int lfs3_emubd_createcfg(const struct lfs3_cfg *cfg, const char *path, cfg->block_count * sizeof(lfs3_emubd_block_t*)); } - if (bd->cfg->disk_path) { + if (path) { bd->disk = malloc(sizeof(lfs3_emubd_disk_t)); if (!bd->disk) { err = LFS3_ERR_NOMEM; @@ -240,11 +269,9 @@ int lfs3_emubd_createcfg(const struct lfs3_cfg *cfg, const char *path, bd->disk->scratch = NULL; #ifdef _WIN32 - bd->disk->fd = open(bd->cfg->disk_path, - O_RDWR | O_CREAT | O_BINARY, 0666); + bd->disk->fd = open(path, O_RDWR | O_CREAT | O_BINARY, 0666); #else - bd->disk->fd = open(bd->cfg->disk_path, - O_RDWR | O_CREAT, 0666); + bd->disk->fd = open(path, O_RDWR | O_CREAT, 0666); #endif if (bd->disk->fd < 0) { err = -errno; @@ -293,15 +320,28 @@ failed:; } int lfs3_emubd_create(const struct lfs3_cfg *cfg, const char *path) { - LFS3_EMUBD_TRACE("lfs3_emubd_create(%p {.context=%p, " - ".read=%p, .prog=%p, .erase=%p, .sync=%p, " - ".read_size=%"PRIu32", .prog_size=%"PRIu32", " - ".block_size=%"PRIu32", .block_count=%"PRIu32"}, " + LFS3_EMUBD_TRACE("lfs3_emubd_create(" + "%p {" + ".context=%p, " + ".read=%p, " + ".prog=%p, " + ".erase=%p, " + ".sync=%p, " + ".read_size=%"PRIu32", " + ".prog_size=%"PRIu32", " + ".block_size=%"PRIu32", " + ".block_count=%"PRIu32"}, " "\"%s\")", - (void*)cfg, cfg->context, - (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog, - (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync, - cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count, + (void*)cfg, + cfg->context, + (void*)(uintptr_t)cfg->read, + (void*)(uintptr_t)cfg->prog, + (void*)(uintptr_t)cfg->erase, + (void*)(uintptr_t)cfg->sync, + cfg->read_size, + cfg->prog_size, + cfg->block_size, + cfg->block_count, path); static const struct lfs3_emubd_cfg defaults = {.erase_value=-1}; int err = lfs3_emubd_createcfg(cfg, path, &defaults); diff --git a/bd/lfs3_emubd.h b/bd/lfs3_emubd.h index f9783948..f1b67bc7 100644 --- a/bd/lfs3_emubd.h +++ b/bd/lfs3_emubd.h @@ -100,11 +100,6 @@ struct lfs3_emubd_cfg { // not affect normal operation. uint32_t seed; - // Path to file to use as a mirror of the disk. This provides a way to view - // the current state of the block device, but does not eliminate the RAM - // requirement. - const char *disk_path; - // Artificial delay in nanoseconds, there is no purpose for this other // than slowing down the simulation. lfs3_emubd_sleep_t read_sleep; @@ -160,11 +155,10 @@ typedef struct lfs3_emubd { // Create an emulating block device using the geometry in lfs3_cfg // -// If disk_path is provided, emubd will mirror the block device in the -// file. Note this is a write-only mirror intended for introspection, -// and does not eliminate the RAM requirement. +// If path is provided, emubd will mirror the block device in the file. +// This provides a way to view the current state of the block device, +// but does not eliminate the RAM requirement. // -// TODO wait, why do we have both disk_path and path here? int lfs3_emubd_create(const struct lfs3_cfg *cfg, const char *path); int lfs3_emubd_createcfg(const struct lfs3_cfg *cfg, const char *path, const struct lfs3_emubd_cfg *bdcfg); diff --git a/bd/lfs3_kiwibd.c b/bd/lfs3_kiwibd.c index e2b84b95..53dec9be 100644 --- a/bd/lfs3_kiwibd.c +++ b/bd/lfs3_kiwibd.c @@ -92,7 +92,6 @@ int lfs3_kiwibd_createcfg(const struct lfs3_cfg *cfg, const char *path, "\"%s\", " "%p {" ".erase_value=%"PRId32", " - ".disk_path=\"%s\", " ".buffer=%p, " ".read_sleep=%"PRIu64", " ".prog_sleep=%"PRIu64", " @@ -110,7 +109,6 @@ int lfs3_kiwibd_createcfg(const struct lfs3_cfg *cfg, const char *path, path, (void*)bdcfg, bdcfg->erase_value, - bdcfg->disk_path, bdcfg->buffer, bdcfg->read_sleep, bdcfg->prog_sleep, @@ -122,35 +120,34 @@ int lfs3_kiwibd_createcfg(const struct lfs3_cfg *cfg, const char *path, bd->readed = 0; bd->proged = 0; bd->erased = 0; - if (bd->cfg->disk_path) { - bd->u.disk.fd = -1; - bd->u.disk.scratch = NULL; + bd->fd = -1; + if (path) { + bd->u.scratch = NULL; } else { bd->u.mem = NULL; } int err; - // if we have a disk_path, try to open the backing file - if (bd->cfg->disk_path) { - bd->u.disk.fd = open(bd->cfg->disk_path, - O_RDWR | O_CREAT, 0666); - if (bd->u.disk.fd < 0) { + // if we have a path, try to open the backing file + if (path) { + bd->fd = open(path, O_RDWR | O_CREAT, 0666); + if (bd->fd < 0) { err = -errno; goto failed; } // allocate a scratch buffer to help with zeroing/masking/etc - bd->u.disk.scratch = malloc(cfg->block_size); - if (!bd->u.disk.scratch) { + bd->u.scratch = malloc(cfg->block_size); + if (!bd->u.scratch) { err = LFS3_ERR_NOMEM; goto failed; } // zero for reproducibility - lfs3_kiwibd_memzero(cfg, bd->u.disk.scratch, cfg->block_size); + lfs3_kiwibd_memzero(cfg, bd->u.scratch, cfg->block_size); for (lfs3_block_t i = 0; i < cfg->block_count; i++) { - ssize_t res = write(bd->u.disk.fd, - bd->u.disk.scratch, + ssize_t res = write(bd->fd, + bd->u.scratch, cfg->block_size); if (res < 0) { err = -errno; @@ -177,11 +174,9 @@ int lfs3_kiwibd_createcfg(const struct lfs3_cfg *cfg, const char *path, failed:; LFS3_KIWIBD_TRACE("lfs3_kiwibd_createcfg -> %d", err); // clean up memory - if (bd->cfg->disk_path) { - if (bd->u.disk.fd != -1) { - close(bd->u.disk.fd); - } - free(bd->u.disk.scratch); + if (bd->fd >= 0) { + close(bd->fd); + free(bd->u.scratch); } else { free(bd->u.mem); } @@ -223,9 +218,9 @@ int lfs3_kiwibd_destroy(const struct lfs3_cfg *cfg) { lfs3_kiwibd_t *bd = cfg->context; // clean up memory - if (bd->cfg->disk_path) { - close(bd->u.disk.fd); - free(bd->u.disk.scratch); + if (bd->fd >= 0) { + close(bd->fd); + free(bd->u.scratch); } else { free(bd->u.mem); } @@ -251,12 +246,12 @@ int lfs3_kiwibd_read(const struct lfs3_cfg *cfg, lfs3_block_t block, LFS3_ASSERT(off+size <= cfg->block_size); // read in file? - if (bd->cfg->disk_path) { + if (bd->fd >= 0) { lfs3_kiwibd_memerase(cfg, - bd->u.disk.scratch, + bd->u.scratch, cfg->block_size); - off_t res = lseek(bd->u.disk.fd, + off_t res = lseek(bd->fd, (off_t)block*cfg->block_size + (off_t)off, SEEK_SET); if (res < 0) { @@ -265,7 +260,7 @@ int lfs3_kiwibd_read(const struct lfs3_cfg *cfg, lfs3_block_t block, return err; } - ssize_t res_ = read(bd->u.disk.fd, buffer, size); + ssize_t res_ = read(bd->fd, buffer, size); if (res_ < 0) { int err = -errno; LFS3_KIWIBD_TRACE("lfs3_kiwibd_read -> %d", err); @@ -312,10 +307,10 @@ int lfs3_kiwibd_prog(const struct lfs3_cfg *cfg, lfs3_block_t block, LFS3_ASSERT(off+size <= cfg->block_size); // prog in file? - if (bd->cfg->disk_path) { + if (bd->fd >= 0) { // were we erased properly? if (bd->cfg->erase_value >= 0) { - off_t res = lseek(bd->u.disk.fd, + off_t res = lseek(bd->fd, (off_t)block*cfg->block_size + (off_t)off, SEEK_SET); if (res < 0) { @@ -324,7 +319,7 @@ int lfs3_kiwibd_prog(const struct lfs3_cfg *cfg, lfs3_block_t block, return err; } - ssize_t res_ = read(bd->u.disk.fd, bd->u.disk.scratch, size); + ssize_t res_ = read(bd->fd, bd->u.scratch, size); if (res_ < 0) { int err = -errno; LFS3_KIWIBD_TRACE("lfs3_kiwibd_prog -> %d", err); @@ -332,13 +327,13 @@ int lfs3_kiwibd_prog(const struct lfs3_cfg *cfg, lfs3_block_t block, } for (lfs3_off_t i = 0; i < size; i++) { - LFS3_ASSERT(bd->u.disk.scratch[i] == bd->cfg->erase_value); + LFS3_ASSERT(bd->u.scratch[i] == bd->cfg->erase_value); } } // masking progs? if (bd->cfg->erase_value == -2) { - off_t res = lseek(bd->u.disk.fd, + off_t res = lseek(bd->fd, (off_t)block*cfg->block_size + (off_t)off, SEEK_SET); if (res < 0) { @@ -347,16 +342,16 @@ int lfs3_kiwibd_prog(const struct lfs3_cfg *cfg, lfs3_block_t block, return err; } - ssize_t res_ = read(bd->u.disk.fd, bd->u.disk.scratch, size); + ssize_t res_ = read(bd->fd, bd->u.scratch, size); if (res_ < 0) { int err = -errno; LFS3_KIWIBD_TRACE("lfs3_kiwibd_prog -> %d", err); return err; } - lfs3_kiwibd_memprog(cfg, bd->u.disk.scratch, buffer, size); + lfs3_kiwibd_memprog(cfg, bd->u.scratch, buffer, size); - res = lseek(bd->u.disk.fd, + res = lseek(bd->fd, (off_t)block*cfg->block_size + (off_t)off, SEEK_SET); if (res < 0) { @@ -365,7 +360,7 @@ int lfs3_kiwibd_prog(const struct lfs3_cfg *cfg, lfs3_block_t block, return err; } - res_ = write(bd->u.disk.fd, bd->u.disk.scratch, size); + res_ = write(bd->fd, bd->u.scratch, size); if (res_ < 0) { int err = -errno; LFS3_KIWIBD_TRACE("lfs3_kiwibd_prog -> %d", err); @@ -374,7 +369,7 @@ int lfs3_kiwibd_prog(const struct lfs3_cfg *cfg, lfs3_block_t block, // normal progs? } else { - off_t res = lseek(bd->u.disk.fd, + off_t res = lseek(bd->fd, (off_t)block*cfg->block_size + (off_t)off, SEEK_SET); if (res < 0) { @@ -383,7 +378,7 @@ int lfs3_kiwibd_prog(const struct lfs3_cfg *cfg, lfs3_block_t block, return err; } - ssize_t res_ = write(bd->u.disk.fd, buffer, size); + ssize_t res_ = write(bd->fd, buffer, size); if (res_ < 0) { int err = -errno; LFS3_KIWIBD_TRACE("lfs3_kiwibd_prog -> %d", err); @@ -437,8 +432,8 @@ int lfs3_kiwibd_erase(const struct lfs3_cfg *cfg, lfs3_block_t block) { // emulate an erase value? if (bd->cfg->erase_value != -1) { // erase in file? - if (bd->cfg->disk_path) { - off_t res = lseek(bd->u.disk.fd, + if (bd->fd >= 0) { + off_t res = lseek(bd->fd, (off_t)block*cfg->block_size, SEEK_SET); if (res < 0) { @@ -448,11 +443,11 @@ int lfs3_kiwibd_erase(const struct lfs3_cfg *cfg, lfs3_block_t block) { } lfs3_kiwibd_memerase(cfg, - bd->u.disk.scratch, + bd->u.scratch, cfg->block_size); - ssize_t res_ = write(bd->u.disk.fd, - bd->u.disk.scratch, + ssize_t res_ = write(bd->fd, + bd->u.scratch, cfg->block_size); if (res_ < 0) { int err = -errno; diff --git a/bd/lfs3_kiwibd.h b/bd/lfs3_kiwibd.h index 2e182fa9..0cd36728 100644 --- a/bd/lfs3_kiwibd.h +++ b/bd/lfs3_kiwibd.h @@ -39,10 +39,6 @@ struct lfs3_kiwibd_cfg { // does _not_ rely on this!). int32_t erase_value; - // Path to file to back the block device. If provided kiwibd uses this - // instead of RAM, allowing emulation of block devices > available RAM. - const char *disk_path; - // Optional statically allocated buffer for the block device. Ignored // if disk_path is provided. void *buffer; @@ -63,11 +59,9 @@ struct lfs3_kiwibd_cfg { // kiwibd state typedef struct lfs3_kiwibd { // backing disk + int fd; union { - struct { - int fd; - uint8_t *scratch; - } disk; + uint8_t *scratch; uint8_t *mem; } u; @@ -84,10 +78,9 @@ typedef struct lfs3_kiwibd { // Create a kiwibd using the geometry in lfs3_cfg // -// If disk_path is provided, it will be used to back the kiwibd, -// otherwise kiwibd will try to use RAM. +// If path is provided, kiwibd will use the file to back the block +// device, allowing emulation of block devices > available RAM. // -// TODO wait, why do we have both disk_path and path here? int lfs3_kiwibd_create(const struct lfs3_cfg *cfg, const char *path); int lfs3_kiwibd_createcfg(const struct lfs3_cfg *cfg, const char *path, const struct lfs3_kiwibd_cfg *bdcfg); diff --git a/runners/bench_runner.c b/runners/bench_runner.c index 7805e333..50bc8be9 100644 --- a/runners/bench_runner.c +++ b/runners/bench_runner.c @@ -1355,7 +1355,6 @@ void perm_run( }; struct lfs3_emubd_cfg bdcfg = { - .disk_path = bench_disk_path, .read_sleep = bench_read_sleep, .prog_sleep = bench_prog_sleep, .erase_sleep = bench_erase_sleep, diff --git a/runners/test_runner.c b/runners/test_runner.c index 3b948d2d..ae2d4d52 100644 --- a/runners/test_runner.c +++ b/runners/test_runner.c @@ -1319,7 +1319,6 @@ static void run_powerloss_none( }; struct lfs3_emubd_cfg bdcfg = { - .disk_path = test_disk_path, .read_sleep = test_read_sleep, .prog_sleep = test_prog_sleep, .erase_sleep = test_erase_sleep, @@ -1380,7 +1379,6 @@ static void run_powerloss_linear( }; struct lfs3_emubd_cfg bdcfg = { - .disk_path = test_disk_path, .read_sleep = test_read_sleep, .prog_sleep = test_prog_sleep, .erase_sleep = test_erase_sleep, @@ -1457,7 +1455,6 @@ static void run_powerloss_log( }; struct lfs3_emubd_cfg bdcfg = { - .disk_path = test_disk_path, .read_sleep = test_read_sleep, .prog_sleep = test_prog_sleep, .erase_sleep = test_erase_sleep, @@ -1534,7 +1531,6 @@ static void run_powerloss_cycles( }; struct lfs3_emubd_cfg bdcfg = { - .disk_path = test_disk_path, .read_sleep = test_read_sleep, .prog_sleep = test_prog_sleep, .erase_sleep = test_erase_sleep, @@ -1709,7 +1705,6 @@ static void run_powerloss_exhaustive( }; struct lfs3_emubd_cfg bdcfg = { - .disk_path = test_disk_path, .read_sleep = test_read_sleep, .prog_sleep = test_prog_sleep, .erase_sleep = test_erase_sleep,