From 74fa6e9274be243c1aa53b29a92942e17eebd64e Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Fri, 15 Jan 2016 20:32:49 +0100 Subject: [PATCH] Add the "maybe" function also to par2block --- cmdline/check.c | 6 +++--- cmdline/dry.c | 2 +- cmdline/elem.c | 2 +- cmdline/elem.h | 23 ++++++++++++++++++++--- cmdline/parity.c | 6 +++--- cmdline/scan.c | 4 ++-- cmdline/scrub.c | 2 +- cmdline/state.c | 8 ++++---- cmdline/status.c | 2 +- cmdline/sync.c | 10 +++++----- 10 files changed, 41 insertions(+), 24 deletions(-) diff --git a/cmdline/check.c b/cmdline/check.c index dd80763e..8a65b269 100644 --- a/cmdline/check.c +++ b/cmdline/check.c @@ -620,7 +620,7 @@ static int file_post(struct snapraid_state* state, int fix, unsigned i, struct s continue; } - block = fs_par2block_get(disk, i); + block = fs_par2block_maybe(disk, i); if (!block_has_file(block)) { /* if no file, nothing to do */ continue; @@ -827,7 +827,7 @@ static int block_is_enabled(struct snapraid_state* state, block_off_t i, struct if (!handle[j].disk) continue; - block = fs_par2block_get(handle[j].disk, i); + block = fs_par2block_maybe(handle[j].disk, i); /* try to recover all files, even the ones without hash */ /* because in some cases we can recover also them */ @@ -948,7 +948,7 @@ static int state_check_process(struct snapraid_state* state, int fix, struct sna } /* if the disk block is not used */ - block = fs_par2block_get(disk, i); + block = fs_par2block_maybe(disk, i); if (block == BLOCK_EMPTY) { /* use an empty block */ memset(buffer[j], 0, state->block_size); diff --git a/cmdline/dry.c b/cmdline/dry.c index af60b7de..2061aa84 100644 --- a/cmdline/dry.c +++ b/cmdline/dry.c @@ -57,7 +57,7 @@ static void dry_data_reader(struct snapraid_worker* worker, struct snapraid_task } /* get the block */ - task->block = fs_par2block_get(disk, blockcur); + task->block = fs_par2block_maybe(disk, blockcur); /* if the block is not used */ if (!block_has_file(task->block)) { diff --git a/cmdline/elem.c b/cmdline/elem.c index 9ec6305e..714b6744 100644 --- a/cmdline/elem.c +++ b/cmdline/elem.c @@ -1353,7 +1353,7 @@ struct snapraid_block* fs_file2block_get(struct snapraid_file* file, block_off_t return &file->blockvec[file_pos]; } -struct snapraid_block* fs_par2block_get(struct snapraid_disk* disk, block_off_t parity_pos) +struct snapraid_block* fs_par2block_maybe(struct snapraid_disk* disk, block_off_t parity_pos) { struct snapraid_file* file; block_off_t file_pos; diff --git a/cmdline/elem.h b/cmdline/elem.h index 11bee14a..b80d0588 100644 --- a/cmdline/elem.h +++ b/cmdline/elem.h @@ -946,7 +946,7 @@ static inline struct snapraid_file* fs_par2file_get(struct snapraid_disk* disk, ret = fs_par2file_maybe(disk, parity_pos, file_pos); if (ret == 0) { /* LCOV_EXCL_START */ - log_fatal("Internal inconsistency when deresolving parity position '%u' in disk '%s'\n", parity_pos, disk->name); + log_fatal("Internal inconsistency when deresolving parity to file at position '%u' in disk '%s'\n", parity_pos, disk->name); os_abort(); /* LCOV_EXCL_STOP */ } @@ -981,9 +981,26 @@ static inline block_off_t fs_file2par_get(struct snapraid_disk* disk, struct sna /** * Get the block from the parity position. * Return BLOCK_EMPTY==0 if the block is over the end of the disk or not used. - * \note This function is NOT thread-safe as it uses the the disk cache. */ -struct snapraid_block* fs_par2block_get(struct snapraid_disk* disk, block_off_t parity_pos); +struct snapraid_block* fs_par2block_maybe(struct snapraid_disk* disk, block_off_t parity_pos); + +/** + * Get the block from the parity position. + */ +static inline struct snapraid_block* fs_par2block_get(struct snapraid_disk* disk, block_off_t parity_pos) +{ + struct snapraid_block* ret; + + ret = fs_par2block_maybe(disk, parity_pos); + if (ret == BLOCK_EMPTY) { + /* LCOV_EXCL_START */ + log_fatal("Internal inconsistency when deresolving parity to block at position '%u' in disk '%s'\n", parity_pos, disk->name); + os_abort(); + /* LCOV_EXCL_STOP */ + } + + return ret; +} /** * Allocate a disk mapping. diff --git a/cmdline/parity.c b/cmdline/parity.c index ab7ce9ac..f926c7bb 100644 --- a/cmdline/parity.c +++ b/cmdline/parity.c @@ -42,7 +42,7 @@ block_off_t parity_allocated_size(struct snapraid_state* state) /* decrease the block until an allocated one, but part of a file */ /* we don't stop at deleted blocks, because we want to have them cleared */ /* if they are at the end of the parity */ - while (block > parity_block && !block_has_file(fs_par2block_get(disk, block - 1))) + while (block > parity_block && !block_has_file(fs_par2block_maybe(disk, block - 1))) --block; /* get the highest value */ @@ -67,7 +67,7 @@ block_off_t parity_used_size(struct snapraid_state* state) block_off_t block = fs_size(disk); /* decrease the block until an used one */ - while (block > parity_block && !block_has_file_and_valid_parity(fs_par2block_get(disk, block - 1))) + while (block > parity_block && !block_has_file_and_valid_parity(fs_par2block_maybe(disk, block - 1))) --block; /* get the highest value */ @@ -95,7 +95,7 @@ int parity_is_invalid(struct snapraid_state* state) one_valid = 0; for (node_disk = state->disklist; node_disk != 0; node_disk = node_disk->next) { struct snapraid_disk* disk = node_disk->data; - struct snapraid_block* block = fs_par2block_get(disk, i); + struct snapraid_block* block = fs_par2block_maybe(disk, i); if (block_has_file(block)) one_valid = 1; diff --git a/cmdline/scan.c b/cmdline/scan.c index c3eeff1f..03085733 100644 --- a/cmdline/scan.c +++ b/cmdline/scan.c @@ -175,11 +175,11 @@ static void scan_file_allocate(struct snapraid_scan* scan, struct snapraid_file* snapraid_info info; /* increment the position until the first really free block */ - while (block_has_file(fs_par2block_get(disk, parity_pos))) + while (block_has_file(fs_par2block_maybe(disk, parity_pos))) ++parity_pos; /* get block we are going to overwrite, if any */ - over_block = fs_par2block_get(disk, parity_pos); + over_block = fs_par2block_maybe(disk, parity_pos); /* deallocate it */ if (over_block != BLOCK_EMPTY) diff --git a/cmdline/scrub.c b/cmdline/scrub.c index f234fb61..10d36ab3 100644 --- a/cmdline/scrub.c +++ b/cmdline/scrub.c @@ -121,7 +121,7 @@ static void scrub_data_reader(struct snapraid_worker* worker, struct snapraid_ta } /* get the block */ - task->block = fs_par2block_get(disk, blockcur); + task->block = fs_par2block_maybe(disk, blockcur); /* if the block is not used */ if (!block_has_file(task->block)) { diff --git a/cmdline/state.c b/cmdline/state.c index ca091173..9ef9e49f 100644 --- a/cmdline/state.c +++ b/cmdline/state.c @@ -1541,7 +1541,7 @@ static int fs_position_is_required(struct snapraid_state* state, block_off_t pos /* check for each disk */ for (i = state->disklist; i != 0; i = i->next) { struct snapraid_disk* disk = i->data; - struct snapraid_block* block = fs_par2block_get(disk, pos); + struct snapraid_block* block = fs_par2block_maybe(disk, pos); /* if we have at least one file, the position is needed */ if (block_has_file(block)) @@ -1570,7 +1570,7 @@ static int fs_info_is_required(struct snapraid_state* state, block_off_t pos) /* check for each disk */ for (i = state->disklist; i != 0; i = i->next) { struct snapraid_disk* disk = i->data; - struct snapraid_block* block = fs_par2block_get(disk, pos); + struct snapraid_block* block = fs_par2block_maybe(disk, pos); /* if we have at least one synced file, the info is required */ if (block_state_get(block) == BLOCK_STATE_BLK) @@ -1587,7 +1587,7 @@ static void fs_position_clear_deleted(struct snapraid_state* state, block_off_t /* check for each disk if block is really used */ for (i = state->disklist; i != 0; i = i->next) { struct snapraid_disk* disk = i->data; - struct snapraid_block* block = fs_par2block_get(disk, pos); + struct snapraid_block* block = fs_par2block_maybe(disk, pos); /* if the block is deleted */ if (block_state_get(block) == BLOCK_STATE_DELETED) { @@ -1602,7 +1602,7 @@ static void fs_position_clear_deleted(struct snapraid_state* state, block_off_t */ static int fs_is_block_deleted(struct snapraid_disk* disk, block_off_t pos) { - struct snapraid_block* block = fs_par2block_get(disk, pos); + struct snapraid_block* block = fs_par2block_maybe(disk, pos); return block_state_get(block) == BLOCK_STATE_DELETED; } diff --git a/cmdline/status.c b/cmdline/status.c index 325ca6be..6aeb2f76 100644 --- a/cmdline/status.c +++ b/cmdline/status.c @@ -303,7 +303,7 @@ int state_status(struct snapraid_state* state) one_valid = 0; for (node_disk = state->disklist; node_disk != 0; node_disk = node_disk->next) { struct snapraid_disk* disk = node_disk->data; - struct snapraid_block* block = fs_par2block_get(disk, i); + struct snapraid_block* block = fs_par2block_maybe(disk, i); if (block_has_file(block)) one_valid = 1; diff --git a/cmdline/sync.c b/cmdline/sync.c index 37371ac9..6fd5a776 100644 --- a/cmdline/sync.c +++ b/cmdline/sync.c @@ -68,7 +68,7 @@ static int state_hash_process(struct snapraid_state* state, block_off_t blocksta struct snapraid_block* block; unsigned block_state; - block = fs_par2block_get(disk, i); + block = fs_par2block_maybe(disk, i); /* get the state of the block */ block_state = block_state_get(block); @@ -106,7 +106,7 @@ static int state_hash_process(struct snapraid_state* state, block_off_t blocksta struct snapraid_file* file; block_off_t file_pos; - block = fs_par2block_get(disk, i); + block = fs_par2block_maybe(disk, i); /* get the state of the block */ block_state = block_state_get(block); @@ -450,7 +450,7 @@ static int block_is_enabled(void* void_plan, block_off_t i) if (!disk) continue; - block = fs_par2block_get(disk, i); + block = fs_par2block_maybe(disk, i); if (block_has_file(block)) one_valid = 1; @@ -485,7 +485,7 @@ static void sync_data_reader(struct snapraid_worker* worker, struct snapraid_tas } /* get the block */ - task->block = fs_par2block_get(disk, blockcur); + task->block = fs_par2block_maybe(disk, blockcur); /* if the block has no file, meanining that it's EMPTY or DELETED, */ /* it doesn't partecipate in the new parity computation */ @@ -1137,7 +1137,7 @@ static int state_sync_process(struct snapraid_state* state, struct snapraid_pari if (!handle[j].disk) continue; - block = fs_par2block_get(handle[j].disk, blockcur); + block = fs_par2block_maybe(handle[j].disk, blockcur); if (block == BLOCK_EMPTY) { /* nothing to do */