From 5b06549ee3b4bafbe9f143ae9e7bb636a834000a Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Thu, 7 Apr 2016 21:39:02 +0200 Subject: [PATCH] Make the hash size configurable Only at source code level. --- cmdline/check.c | 10 +++++----- cmdline/dup.c | 12 ++++++------ cmdline/elem.c | 23 ++++++++++++++--------- cmdline/elem.h | 47 +++++++++++++++++++++++++++++++++++++++------- cmdline/import.c | 8 ++++---- cmdline/import.h | 4 ++-- cmdline/rehash.c | 4 ++-- cmdline/scan.c | 2 +- cmdline/scrub.c | 10 +++++----- cmdline/search.c | 4 ++-- cmdline/selftest.c | 12 ++++++------ cmdline/speed.c | 6 +++--- cmdline/state.c | 20 ++++++++++---------- cmdline/state.h | 4 ++-- cmdline/sync.c | 24 +++++++++++------------ cmdline/util.h | 3 +-- 16 files changed, 115 insertions(+), 78 deletions(-) diff --git a/cmdline/check.c b/cmdline/check.c index 7c960e77..d3941a9c 100644 --- a/cmdline/check.c +++ b/cmdline/check.c @@ -69,7 +69,7 @@ struct failed_struct { */ static int blockcmp(struct snapraid_state* state, int rehash, struct snapraid_block* block, unsigned pos_size, unsigned char* buffer, unsigned char* buffer_zero) { - unsigned char hash[HASH_SIZE]; + unsigned char hash[HASH_MAX]; /* now compute the hash of the valid part */ if (rehash) { @@ -79,7 +79,7 @@ static int blockcmp(struct snapraid_state* state, int rehash, struct snapraid_bl } /* compare the hash */ - if (memcmp(hash, block->hash, HASH_SIZE) != 0) { + if (memcmp(hash, block->hash, BLOCK_HASH_SIZE) != 0) { return -1; } @@ -937,7 +937,7 @@ static int state_check_process(struct snapraid_state* state, int fix, struct sna /* for each disk, process the block */ for (j = 0; j < diskmax; ++j) { int read_size; - unsigned char hash[HASH_SIZE]; + unsigned char hash[HASH_MAX]; struct snapraid_disk* disk; struct snapraid_block* block; struct snapraid_file* file; @@ -1166,8 +1166,8 @@ static int state_check_process(struct snapraid_state* state, int fix, struct sna } /* compare the hash */ - if (memcmp(hash, block->hash, HASH_SIZE) != 0) { - unsigned diff = memdiff(hash, block->hash, HASH_SIZE); + if (memcmp(hash, block->hash, BLOCK_HASH_SIZE) != 0) { + unsigned diff = memdiff(hash, block->hash, BLOCK_HASH_SIZE); /* save the failed block for the check/fix */ failed[failed_count].is_bad = 1; /* it's bad because the hash doesn't match */ diff --git a/cmdline/dup.c b/cmdline/dup.c index b669436f..65947e03 100644 --- a/cmdline/dup.c +++ b/cmdline/dup.c @@ -30,7 +30,7 @@ struct snapraid_hash { struct snapraid_disk* disk; /**< Disk. */ struct snapraid_file* file; /**< File. */ - unsigned char hash[HASH_SIZE]; /**< Hash of the whole file. */ + unsigned char hash[HASH_MAX]; /**< Hash of the whole file. */ /* nodes for data structures */ tommy_hashdyn_node node; @@ -46,13 +46,13 @@ struct snapraid_hash* hash_alloc(struct snapraid_state* state, struct snapraid_d hash->disk = disk; hash->file = file; - buf = malloc_nofail(file->blockmax * HASH_SIZE); + buf = malloc_nofail(file->blockmax * BLOCK_HASH_SIZE); /* set the back pointer */ for (i = 0; i < file->blockmax; ++i) { struct snapraid_block* block = fs_file2block_get(file, i); - memcpy(buf + i * HASH_SIZE, block->hash, HASH_SIZE); + memcpy(buf + i * BLOCK_HASH_SIZE, block->hash, BLOCK_HASH_SIZE); if (!block_has_updated_hash(block)) { free(buf); @@ -61,7 +61,7 @@ struct snapraid_hash* hash_alloc(struct snapraid_state* state, struct snapraid_d } } - memhash(state->besthash, state->hashseed, hash->hash, buf, file->blockmax * HASH_SIZE); + memhash(state->besthash, state->hashseed, hash->hash, buf, file->blockmax * BLOCK_HASH_SIZE); free(buf); @@ -70,7 +70,7 @@ struct snapraid_hash* hash_alloc(struct snapraid_state* state, struct snapraid_d static inline tommy_uint32_t hash_hash(struct snapraid_hash* hash) { - return tommy_hash_u32(0, hash->hash, HASH_SIZE); + return tommy_hash_u32(0, hash->hash, HASH_MAX); } void hash_free(struct snapraid_hash* hash) @@ -83,7 +83,7 @@ int hash_compare(const void* void_arg, const void* void_data) const char* arg = void_arg; const struct snapraid_hash* hash = void_data; - return memcmp(arg, hash->hash, HASH_SIZE); + return memcmp(arg, hash->hash, HASH_MAX); } void state_dup(struct snapraid_state* state) diff --git a/cmdline/elem.c b/cmdline/elem.c index c33baee2..1735cb6a 100644 --- a/cmdline/elem.c +++ b/cmdline/elem.c @@ -24,6 +24,8 @@ /****************************************************************************/ /* snapraid */ +int BLOCK_HASH_SIZE = HASH_MAX; + struct snapraid_content* content_alloc(const char* path, uint64_t dev) { struct snapraid_content* content; @@ -369,11 +371,12 @@ struct snapraid_file* file_alloc(unsigned block_size, const char* sub, data_off_ file->inode = inode; file->physical = physical; file->flag = 0; - file->blockvec = malloc_nofail(file->blockmax * sizeof(struct snapraid_block)); + file->blockvec = malloc_nofail(file->blockmax * block_sizeof()); for (i = 0; i < file->blockmax; ++i) { - block_state_set(&file->blockvec[i], BLOCK_STATE_CHG); - hash_invalid_set(file->blockvec[i].hash); + struct snapraid_block* block = file_block(file, i); + block_state_set(block, BLOCK_STATE_CHG); + hash_invalid_set(block->hash); } return file; @@ -393,11 +396,13 @@ struct snapraid_file* file_dup(struct snapraid_file* copy) file->inode = copy->inode; file->physical = copy->physical; file->flag = copy->flag; - file->blockvec = malloc_nofail(file->blockmax * sizeof(struct snapraid_block)); + file->blockvec = malloc_nofail(file->blockmax * block_sizeof()); for (i = 0; i < file->blockmax; ++i) { - file->blockvec[i].state = copy->blockvec[i].state; - memcpy(file->blockvec[i].hash, copy->blockvec[i].hash, HASH_SIZE); + struct snapraid_block* block = file_block(file, i); + struct snapraid_block* copy_block = file_block(copy, i); + block->state = copy_block->state; + memcpy(block->hash, copy_block->hash, BLOCK_HASH_SIZE); } return file; @@ -445,10 +450,10 @@ void file_copy(struct snapraid_file* src_file, struct snapraid_file* dst_file) for (i = 0; i < dst_file->blockmax; ++i) { /* set a block with hash computed but without parity */ - block_state_set(&dst_file->blockvec[i], BLOCK_STATE_REP); + block_state_set(file_block(dst_file, i), BLOCK_STATE_REP); /* copy the hash */ - memcpy(dst_file->blockvec[i].hash, src_file->blockvec[i].hash, HASH_SIZE); + memcpy(file_block(dst_file, i)->hash, file_block(src_file, i)->hash, BLOCK_HASH_SIZE); } file_flag_set(dst_file, FILE_IS_COPY); @@ -1363,7 +1368,7 @@ struct snapraid_block* fs_file2block_get(struct snapraid_file* file, block_off_t /* LCOV_EXCL_STOP */ } - return &file->blockvec[file_pos]; + return file_block(file, file_pos); } struct snapraid_block* fs_par2block_find(struct snapraid_disk* disk, block_off_t parity_pos) diff --git a/cmdline/elem.h b/cmdline/elem.h index c9f13974..a36a8286 100644 --- a/cmdline/elem.h +++ b/cmdline/elem.h @@ -164,12 +164,25 @@ struct snapraid_filter { */ #define BLOCK_STATE_DELETED 4 +/** + * Block hash size. + * + * At max HASH_MAX. + */ +extern int BLOCK_HASH_SIZE; + /** * Block of a file. */ struct snapraid_block { - unsigned char hash[HASH_SIZE]; /**< Hash of the block. */ unsigned char state; /**< State of the block. */ + + /** + * Hash of the block. + * + * The effective stored size is BLOCK_HASH_SIZE. + */ + unsigned char hash[HASH_MAX]; }; /** @@ -526,9 +539,9 @@ int filter_content(tommy_list* contentlist, const char* path); */ static inline int hash_is_invalid(const unsigned char* hash) { - unsigned i; + int i; - for (i = 0; i < HASH_SIZE; ++i) + for (i = 0; i < BLOCK_HASH_SIZE; ++i) if (hash[i] != 0x00) return 0; @@ -542,9 +555,9 @@ static inline int hash_is_invalid(const unsigned char* hash) */ static inline int hash_is_zero(const unsigned char* hash) { - unsigned i; + int i; - for (i = 0; i < HASH_SIZE; ++i) + for (i = 0; i < BLOCK_HASH_SIZE; ++i) if (hash[i] != 0xFF) return 0; @@ -564,7 +577,7 @@ static inline int hash_is_real(const unsigned char* hash) */ static inline void hash_invalid_set(unsigned char* hash) { - memset(hash, 0x00, HASH_SIZE); + memset(hash, 0x00, BLOCK_HASH_SIZE); } /** @@ -572,7 +585,15 @@ static inline void hash_invalid_set(unsigned char* hash) */ static inline void hash_zero_set(unsigned char* hash) { - memset(hash, 0xFF, HASH_SIZE); + memset(hash, 0xFF, BLOCK_HASH_SIZE); +} + +/** + * Allocated space for block. + */ +static inline size_t block_sizeof(void) +{ + return 1 + BLOCK_HASH_SIZE; } /** @@ -700,6 +721,18 @@ void file_rename(struct snapraid_file* file, const char* sub); */ void file_copy(struct snapraid_file* src_file, struct snapraid_file* dest_file); +/** + * Return the block at the specified position. + * + * Note that the block size if a runtime value. + */ +static inline struct snapraid_block* file_block(struct snapraid_file* file, size_t pos) +{ + unsigned char* ptr = (unsigned char*)file->blockvec; + + return (struct snapraid_block*)(ptr + pos * block_sizeof()); +} + /** * Return the name of the file, without the dir. */ diff --git a/cmdline/import.c b/cmdline/import.c index c878fb84..916f76ca 100644 --- a/cmdline/import.c +++ b/cmdline/import.c @@ -31,7 +31,7 @@ int import_block_hash_compare(const void* void_arg, const void* void_data) const unsigned char* arg = void_arg; const struct snapraid_import_block* block = void_data; - return memcmp(arg, block->hash, HASH_SIZE); + return memcmp(arg, block->hash, BLOCK_HASH_SIZE); } int import_block_prevhash_compare(const void* void_arg, const void* void_data) @@ -39,7 +39,7 @@ int import_block_prevhash_compare(const void* void_arg, const void* void_data) const unsigned char* arg = void_arg; const struct snapraid_import_block* block = void_data; - return memcmp(arg, block->prevhash, HASH_SIZE); + return memcmp(arg, block->prevhash, BLOCK_HASH_SIZE); } /** @@ -158,7 +158,7 @@ int state_import_fetch(struct snapraid_state* state, int rehash, struct snapraid const unsigned char* hash = missing_block->hash; unsigned block_size = state->block_size; unsigned read_size; - unsigned char buffer_hash[HASH_SIZE]; + unsigned char buffer_hash[HASH_MAX]; const char* path; if (rehash) { @@ -212,7 +212,7 @@ int state_import_fetch(struct snapraid_state* state, int rehash, struct snapraid else memhash(state->hash, state->hashseed, buffer_hash, buffer, read_size); - if (memcmp(buffer_hash, hash, HASH_SIZE) != 0) { + if (memcmp(buffer_hash, hash, BLOCK_HASH_SIZE) != 0) { /* LCOV_EXCL_START */ log_fatal("Error in data reading file '%s'.\n", path); log_fatal("Please don't change imported files while running.\n"); diff --git a/cmdline/import.h b/cmdline/import.h index 4dc414e5..abc79cb7 100644 --- a/cmdline/import.h +++ b/cmdline/import.h @@ -32,8 +32,8 @@ struct snapraid_import_block { struct snapraid_import_file* file; /**< Back pointer to the file owning this block. */ unsigned size; /**< Size of the block. */ data_off_t offset; /**< Position of the block in the file. */ - unsigned char hash[HASH_SIZE]; /**< Hash of the block. */ - unsigned char prevhash[HASH_SIZE]; /**< Previous hash of the block. Valid only if we are in rehash state. */ + unsigned char hash[HASH_MAX]; /**< Hash of the block. */ + unsigned char prevhash[HASH_MAX]; /**< Previous hash of the block. Valid only if we are in rehash state. */ /* nodes for data structures */ tommy_hashdyn_node nodeset; diff --git a/cmdline/rehash.c b/cmdline/rehash.c index ec903e16..6b5376bf 100644 --- a/cmdline/rehash.c +++ b/cmdline/rehash.c @@ -52,11 +52,11 @@ void state_rehash(struct snapraid_state* state) /* copy the present hash as previous one */ state->prevhash = state->hash; - memcpy(state->prevhashseed, state->hashseed, HASH_SIZE); + memcpy(state->prevhashseed, state->hashseed, HASH_MAX); /* set the new hash and seed */ state->hash = state->besthash; - if (randomize(state->hashseed, HASH_SIZE) != 0) { + if (randomize(state->hashseed, HASH_MAX) != 0) { /* LCOV_EXCL_START */ log_fatal("Failed to get random values.\n"); exit(EXIT_FAILURE); diff --git a/cmdline/scan.c b/cmdline/scan.c index 9f8ca2cc..1e1497ae 100644 --- a/cmdline/scan.c +++ b/cmdline/scan.c @@ -220,7 +220,7 @@ static void scan_file_allocate(struct snapraid_scan* scan, struct snapraid_file* assert(over_state == BLOCK_STATE_DELETED); /* copy the past hash of the block */ - memcpy(block->hash, over_block->hash, HASH_SIZE); + memcpy(block->hash, over_block->hash, BLOCK_HASH_SIZE); /* if we have not already cleared the past hash */ if (!state->clear_past_hash) { diff --git a/cmdline/scrub.c b/cmdline/scrub.c index c5657457..2dbf8cd8 100644 --- a/cmdline/scrub.c +++ b/cmdline/scrub.c @@ -32,7 +32,7 @@ * Buffer for storing the new hashes. */ struct snapraid_rehash { - unsigned char hash[HASH_SIZE]; + unsigned char hash[HASH_MAX]; struct snapraid_block* block; }; @@ -357,7 +357,7 @@ static int state_scrub_process(struct snapraid_state* state, struct snapraid_par for (j = 0; j < diskmax; ++j) { struct snapraid_task* task; int read_size; - unsigned char hash[HASH_SIZE]; + unsigned char hash[HASH_MAX]; struct snapraid_block* block; int file_is_unsynced; struct snapraid_disk* disk; @@ -466,8 +466,8 @@ static int state_scrub_process(struct snapraid_state* state, struct snapraid_par if (block_has_updated_hash(block)) { /* compare the hash */ - if (memcmp(hash, block->hash, HASH_SIZE) != 0) { - unsigned diff = memdiff(hash, block->hash, HASH_SIZE); + if (memcmp(hash, block->hash, BLOCK_HASH_SIZE) != 0) { + unsigned diff = memdiff(hash, block->hash, BLOCK_HASH_SIZE); log_tag("error:%u:%s:%s: Data error at position %u, diff bits %u\n", blockcur, disk->name, esc(file->sub, esc_buffer), file_pos, diff); @@ -588,7 +588,7 @@ static int state_scrub_process(struct snapraid_state* state, struct snapraid_par /* store all the new hash already computed */ for (j = 0; j < diskmax; ++j) { if (rehandle[j].block) - memcpy(rehandle[j].block->hash, rehandle[j].hash, HASH_SIZE); + memcpy(rehandle[j].block->hash, rehandle[j].hash, BLOCK_HASH_SIZE); } } diff --git a/cmdline/search.c b/cmdline/search.c index 357e0498..82a1c5bc 100644 --- a/cmdline/search.c +++ b/cmdline/search.c @@ -60,7 +60,7 @@ int search_file_compare(const void* void_arg, const void* void_data) const struct search_file_compare_arg* arg = void_arg; const struct snapraid_search_file* file = void_data; const struct snapraid_state* state = arg->state; - unsigned char buffer_hash[HASH_SIZE]; + unsigned char buffer_hash[HASH_MAX]; const char* path = file->path; int f; ssize_t ret; @@ -112,7 +112,7 @@ int search_file_compare(const void* void_arg, const void* void_data) memhash(state->hash, state->hashseed, buffer_hash, arg->buffer, arg->read_size); /* check if the hash is matching */ - if (memcmp(buffer_hash, arg->block->hash, HASH_SIZE) != 0) + if (memcmp(buffer_hash, arg->block->hash, BLOCK_HASH_SIZE) != 0) return -1; if (arg->read_size != state->block_size) { diff --git a/cmdline/selftest.c b/cmdline/selftest.c index 445db422..8785c0fe 100644 --- a/cmdline/selftest.c +++ b/cmdline/selftest.c @@ -48,7 +48,7 @@ struct hash64_test_vector { struct hash_test_vector { const char* data; int len; - unsigned char digest[HASH_SIZE]; + unsigned char digest[HASH_MAX]; }; /** @@ -131,7 +131,7 @@ static void test_hash(void) uint32_t seed32; uint64_t seed64; - seed_aligned = malloc_nofail_align(HASH_SIZE, &seed_alloc); + seed_aligned = malloc_nofail_align(HASH_MAX, &seed_alloc); buffer_aligned = malloc_nofail_align(HASH_TEST_MAX, &buffer_alloc); seed32 = 0xa766795d; @@ -180,10 +180,10 @@ static void test_hash(void) for (i = 0; TEST_MURMUR3[i].data; ++i) { - unsigned char digest[HASH_SIZE]; + unsigned char digest[HASH_MAX]; memcpy(buffer_aligned, TEST_MURMUR3[i].data, TEST_MURMUR3[i].len); memhash(HASH_MURMUR3, seed_aligned, digest, buffer_aligned, TEST_MURMUR3[i].len); - if (memcmp(digest, TEST_MURMUR3[i].digest, HASH_SIZE) != 0) { + if (memcmp(digest, TEST_MURMUR3[i].digest, HASH_MAX) != 0) { /* LCOV_EXCL_START */ log_fatal("Failed Murmur3 test\n"); exit(EXIT_FAILURE); @@ -192,10 +192,10 @@ static void test_hash(void) } for (i = 0; TEST_SPOOKY2[i].data; ++i) { - unsigned char digest[HASH_SIZE]; + unsigned char digest[HASH_MAX]; memcpy(buffer_aligned, TEST_SPOOKY2[i].data, TEST_SPOOKY2[i].len); memhash(HASH_SPOOKY2, seed_aligned, digest, buffer_aligned, TEST_SPOOKY2[i].len); - if (memcmp(digest, TEST_SPOOKY2[i].digest, HASH_SIZE) != 0) { + if (memcmp(digest, TEST_SPOOKY2[i].digest, HASH_MAX) != 0) { /* LCOV_EXCL_START */ log_fatal("Failed Spooky2 test\n"); exit(EXIT_FAILURE); diff --git a/cmdline/speed.c b/cmdline/speed.c index f5ecf785..f3ecdf8f 100644 --- a/cmdline/speed.c +++ b/cmdline/speed.c @@ -82,8 +82,8 @@ void speed(int period) int64_t ds; int64_t dt; int i, j; - unsigned char digest[HASH_SIZE]; - unsigned char seed[HASH_SIZE]; + unsigned char digest[HASH_MAX]; + unsigned char seed[HASH_MAX]; int id[RAID_PARITY_MAX]; int ip[RAID_PARITY_MAX]; int count; @@ -107,7 +107,7 @@ void speed(int period) raid_zero(v[nd + RAID_PARITY_MAX]); /* hash seed */ - for (i = 0; i < HASH_SIZE; ++i) + for (i = 0; i < HASH_MAX; ++i) seed[i] = i; /* basic disks and parity mapping */ diff --git a/cmdline/state.c b/cmdline/state.c index 3f4cd5f1..b3fc0e07 100644 --- a/cmdline/state.c +++ b/cmdline/state.c @@ -1150,7 +1150,7 @@ void state_config(struct snapraid_state* state, const char* path, const char* co state->hash = state->besthash; /* by default use a random hash seed */ - if (randomize(state->hashseed, HASH_SIZE) != 0) { + if (randomize(state->hashseed, HASH_MAX) != 0) { /* LCOV_EXCL_START */ log_fatal("Failed to get random values.\n"); exit(EXIT_FAILURE); @@ -1904,7 +1904,7 @@ static void state_read_content(struct snapraid_state* state, const char* path, S /* read the hash only for 'blk/chg/rep', and not for 'new' */ if (c != 'n') { - ret = sread(f, block->hash, HASH_SIZE); + ret = sread(f, block->hash, BLOCK_HASH_SIZE); if (ret < 0) { /* LCOV_EXCL_START */ decoding_error(path, f); @@ -2123,7 +2123,7 @@ static void state_read_content(struct snapraid_state* state, const char* path, S block_state_set(block, BLOCK_STATE_DELETED); /* read the hash */ - ret = sread(f, block->hash, HASH_SIZE); + ret = sread(f, block->hash, BLOCK_HASH_SIZE); if (ret < 0) { /* LCOV_EXCL_START */ decoding_error(path, f); @@ -2330,7 +2330,7 @@ static void state_read_content(struct snapraid_state* state, const char* path, S } /* read the seed */ - ret = sread(f, state->hashseed, HASH_SIZE); + ret = sread(f, state->hashseed, HASH_MAX); if (ret < 0) { /* LCOV_EXCL_START */ decoding_error(path, f); @@ -2357,7 +2357,7 @@ static void state_read_content(struct snapraid_state* state, const char* path, S } /* read the seed */ - ret = sread(f, state->prevhashseed, HASH_SIZE); + ret = sread(f, state->prevhashseed, HASH_MAX); if (ret < 0) { /* LCOV_EXCL_START */ decoding_error(path, f); @@ -2683,7 +2683,7 @@ static void* state_write_thread(void* arg) return context; /* LCOV_EXCL_STOP */ } - swrite(state->hashseed, HASH_SIZE, f); + swrite(state->hashseed, HASH_MAX, f); if (serror(f)) { /* LCOV_EXCL_START */ log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno)); @@ -2706,7 +2706,7 @@ static void* state_write_thread(void* arg) return context; /* LCOV_EXCL_STOP */ } - swrite(state->prevhashseed, HASH_SIZE, f); + swrite(state->prevhashseed, HASH_MAX, f); if (serror(f)) { /* LCOV_EXCL_START */ log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno)); @@ -2847,7 +2847,7 @@ static void* state_write_thread(void* arg) for (idx = begin; idx < end; ++idx) { struct snapraid_block* block = fs_file2block_get(file, idx); - swrite(block->hash, HASH_SIZE, f); + swrite(block->hash, BLOCK_HASH_SIZE, f); } if (serror(f)) { @@ -2941,7 +2941,7 @@ static void* state_write_thread(void* arg) while (begin < end) { struct snapraid_block* block = fs_par2block_get(disk, begin); - swrite(block->hash, HASH_SIZE, f); + swrite(block->hash, BLOCK_HASH_SIZE, f); ++begin; } @@ -3457,7 +3457,7 @@ void state_read(struct snapraid_state* state) state->hash = HASH_UNDEFINED; /* start with a zero seed, it was the default in old versions */ - memset(state->hashseed, 0, HASH_SIZE); + memset(state->hashseed, 0, HASH_MAX); /* previous hash, start with an undefined value */ state->prevhash = HASH_UNDEFINED; diff --git a/cmdline/state.h b/cmdline/state.h index e51e4382..4f2d9322 100644 --- a/cmdline/state.h +++ b/cmdline/state.h @@ -134,8 +134,8 @@ struct snapraid_state { char share[PATH_MAX]; /**< Path of the share tree. If !=0 pool links are created in a different way. */ char pool[PATH_MAX]; /**< Path of the pool tree. */ uint64_t pool_device; /**< Device identifier of the pool. */ - unsigned char hashseed[HASH_SIZE]; /**< Hash seed. Just after a uint64 to provide a minimal alignment. */ - unsigned char prevhashseed[HASH_SIZE]; /**< Previous hash seed. In case of rehash. */ + unsigned char hashseed[HASH_MAX]; /**< Hash seed. Just after a uint64 to provide a minimal alignment. */ + unsigned char prevhashseed[HASH_MAX]; /**< Previous hash seed. In case of rehash. */ char lockfile[PATH_MAX]; /**< Path of the lock file to use. */ unsigned level; /**< Number of parity levels. 1 for PAR1, 2 for PAR2. */ unsigned hash; /**< Hash kind used. */ diff --git a/cmdline/sync.c b/cmdline/sync.c index dec7c702..063c973b 100644 --- a/cmdline/sync.c +++ b/cmdline/sync.c @@ -103,7 +103,7 @@ static int state_hash_process(struct snapraid_state* state, block_off_t blocksta int rehash; struct snapraid_block* block; int read_size; - unsigned char hash[HASH_SIZE]; + unsigned char hash[HASH_MAX]; unsigned block_state; struct snapraid_file* file; block_off_t file_pos; @@ -262,7 +262,7 @@ static int state_hash_process(struct snapraid_state* state, block_off_t blocksta if (block_state == BLOCK_STATE_REP) { /* compare the hash */ - if (memcmp(hash, block->hash, HASH_SIZE) != 0) { + if (memcmp(hash, block->hash, BLOCK_HASH_SIZE) != 0) { log_tag("error:%u:%s:%s: Unexpected data change\n", i, disk->name, esc(file->sub, esc_buffer)); log_error("Data change at file '%s' at position '%u'\n", handle[j].path, file_pos); log_error("WARNING! Unexpected data modification of a file without parity!\n"); @@ -288,7 +288,7 @@ static int state_hash_process(struct snapraid_state* state, block_off_t blocksta assert(block_state == BLOCK_STATE_CHG); /* copy the hash in the block */ - memcpy(block->hash, hash, HASH_SIZE); + memcpy(block->hash, hash, BLOCK_HASH_SIZE); /* and mark the block as hashed */ block_state_set(block, BLOCK_STATE_REP); @@ -430,7 +430,7 @@ int failed_compare_by_index(const void* void_a, const void* void_b) * Buffer for storing the new hashes. */ struct snapraid_rehash { - unsigned char hash[HASH_SIZE]; + unsigned char hash[HASH_MAX]; struct snapraid_block* block; }; @@ -820,7 +820,7 @@ static int state_sync_process(struct snapraid_state* state, struct snapraid_pari for (j = 0; j < diskmax; ++j) { struct snapraid_task* task; int read_size; - unsigned char hash[HASH_SIZE]; + unsigned char hash[HASH_MAX]; struct snapraid_block* block; unsigned block_state; struct snapraid_disk* disk; @@ -933,7 +933,7 @@ static int state_sync_process(struct snapraid_state* state, struct snapraid_pari if (block_has_updated_hash(block)) { /* compare the hash */ - if (memcmp(hash, block->hash, HASH_SIZE) != 0) { + if (memcmp(hash, block->hash, BLOCK_HASH_SIZE) != 0) { /* if the file has invalid parity, it's a REP changed during the sync */ if (block_has_invalid_parity(block)) { log_tag("error:%u:%s:%s: Unexpected data change\n", blockcur, disk->name, esc(file->sub, esc_buffer)); @@ -956,7 +956,7 @@ static int state_sync_process(struct snapraid_state* state, struct snapraid_pari error_on_this_block = 1; continue; } else { /* otherwise it's a BLK with silent error */ - unsigned diff = memdiff(hash, block->hash, HASH_SIZE); + unsigned diff = memdiff(hash, block->hash, BLOCK_HASH_SIZE); log_tag("error:%u:%s:%s: Data error at position %u, diff bits %u\n", blockcur, disk->name, esc(file->sub, esc_buffer), file_pos, diff); log_error("Data error in file '%s' at position '%u', diff bits %u\n", task->path, file_pos, diff); @@ -985,7 +985,7 @@ static int state_sync_process(struct snapraid_state* state, struct snapraid_pari /* if there is an hash */ if (hash_is_real(block->hash)) { /* check if the hash is changed */ - if (memcmp(hash, block->hash, HASH_SIZE) != 0) { + if (memcmp(hash, block->hash, BLOCK_HASH_SIZE) != 0) { /* the block is different, and we must update parity */ parity_needs_to_be_updated = 1; } @@ -997,7 +997,7 @@ static int state_sync_process(struct snapraid_state* state, struct snapraid_pari /* copy the hash in the block, but doesn't mark the block as hashed */ /* this allow in case of skipped block to do not save the failed computation */ - memcpy(block->hash, hash, HASH_SIZE); + memcpy(block->hash, hash, BLOCK_HASH_SIZE); /* note that in case of rehash, this is the wrong hash, */ /* but it will be overwritten later */ @@ -1101,7 +1101,7 @@ static int state_sync_process(struct snapraid_state* state, struct snapraid_pari /* check the result and prepare the data */ for (j = 0; j < failed_count; ++j) { - unsigned char hash[HASH_SIZE]; + unsigned char hash[HASH_MAX]; unsigned char* block_buffer = buffer[failed[j].index]; unsigned char* block_copy = copy[failed[j].index]; unsigned block_state = block_state_get(failed[j].block); @@ -1120,7 +1120,7 @@ static int state_sync_process(struct snapraid_state* state, struct snapraid_pari state_usage_hash(state); /* if the hash doesn't match */ - if (memcmp(hash, failed[j].block->hash, HASH_SIZE) != 0) { + if (memcmp(hash, failed[j].block->hash, BLOCK_HASH_SIZE) != 0) { /* we have not recovered */ break; } @@ -1197,7 +1197,7 @@ static int state_sync_process(struct snapraid_state* state, struct snapraid_pari /* store all the new hash already computed */ for (j = 0; j < diskmax; ++j) { if (rehandle[j].block) - memcpy(rehandle[j].block->hash, rehandle[j].hash, HASH_SIZE); + memcpy(rehandle[j].block->hash, rehandle[j].hash, BLOCK_HASH_SIZE); } } diff --git a/cmdline/util.h b/cmdline/util.h index 43656f1a..aa0ef9b3 100644 --- a/cmdline/util.h +++ b/cmdline/util.h @@ -169,8 +169,7 @@ void crc32c_init(void); /** * Size of the hash. */ - -#define HASH_SIZE 16 +#define HASH_MAX 16 /** * Hash kinds.