snapraid/cmdline/rehash.c

88 lines
2.1 KiB
C
Raw Permalink Normal View History

// SPDX-License-Identifier: GPL-3.0-or-later
2026-04-02 16:25:49 +02:00
// Copyright (C) 2013 Andrea Mazzoleni
#include "portable.h"
#include "util.h"
#include "elem.h"
#include "import.h"
#include "state.h"
#include "parity.h"
#include "handle.h"
#include "raid/raid.h"
/****************************************************************************/
/* rehash */
2013-07-01 23:08:25 +02:00
void state_rehash(struct snapraid_state* state)
{
2013-07-01 23:08:25 +02:00
block_off_t blockmax;
block_off_t i;
blockmax = parity_allocated_size(state);
2013-07-01 23:08:25 +02:00
/* check if a rehash is already in progress */
if (state->prevhash != HASH_UNDEFINED) {
/* LCOV_EXCL_START */
2025-12-12 09:38:54 +01:00
log_tag("summary:exit:already_in_progress\n");
log_fatal(EUSER, "You already have a rehash in progress.\n");
2013-07-01 23:08:25 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
2013-07-01 23:08:25 +02:00
}
2013-07-01 23:08:25 +02:00
if (state->hash == state->besthash) {
/* LCOV_EXCL_START */
2025-12-12 09:38:54 +01:00
log_tag("summary:exit:not_required\n");
log_fatal(EUSER, "You are already using the best hash for your platform.\n");
2013-07-01 23:08:25 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
2013-07-01 23:08:25 +02:00
/* copy the present hash as previous one */
state->prevhash = state->hash;
memcpy(state->prevhashseed, state->hashseed, HASH_MAX);
2013-06-16 00:33:46 +02:00
/* set the new hash and seed */
state->hash = state->besthash;
if (randomize(state->hashseed, HASH_MAX) != 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Failed to get random values.\n");
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
2013-07-01 23:08:25 +02:00
/* mark all the block for rehashing */
2014-09-27 00:32:39 +02:00
for (i = 0; i < blockmax; ++i) {
2013-07-01 23:08:25 +02:00
snapraid_info info;
2013-07-01 23:08:25 +02:00
/* if it's unused */
info = info_get(&state->infoarr, i);
if (info == 0) {
/* skip it */
continue;
}
2013-07-01 23:08:25 +02:00
if (info_get_rehash(info)) {
/* LCOV_EXCL_START */
log_fatal(EINTERNAL, "Internal inconsistency for a rehash already in progress\n");
os_abort();
/* LCOV_EXCL_STOP */
2013-07-01 23:08:25 +02:00
}
2013-07-01 23:08:25 +02:00
/* enable the rehash */
info = info_set_rehash(info);
2013-07-01 23:08:25 +02:00
/* save it */
info_set(&state->infoarr, i, info);
}
2013-07-01 23:08:25 +02:00
/* save the new content file */
state->need_write = 1;
2015-02-22 14:31:03 +01:00
msg_status("A rehash is now scheduled. It will take place progressively in the next\n");
msg_status("'sync' and 'scrub' commands. You can check the rehash progress using the\n");
msg_status("'status' command.\n");
2025-12-12 09:38:54 +01:00
log_tag("summary:exit:scheduled\n");
}