snapraid/rehash.c

89 lines
2.3 KiB
C
Raw Normal View History

/*
* Copyright (C) 2013 Andrea Mazzoleni
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "portable.h"
#include "util.h"
#include "elem.h"
#include "import.h"
#include "state.h"
#include "parity.h"
#include "handle.h"
#include "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;
2013-07-01 23:08:25 +02:00
blockmax = parity_size(state);
2013-07-01 23:08:25 +02:00
/* check if a rehash is already in progress */
if (state->prevhash != HASH_UNDEFINED) {
fprintf(stderr, "You already have a rehash in progress.\n");
exit(EXIT_FAILURE);
}
2013-07-01 23:08:25 +02:00
if (state->hash == state->besthash) {
fprintf(stderr, "You are already using the best hash for your platform.\n");
exit(EXIT_FAILURE);
}
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_SIZE);
2013-06-16 00:33:46 +02:00
/* set the new hash and seed */
state->hash = state->besthash;
2013-07-01 23:08:25 +02:00
randomize(state->hashseed, HASH_SIZE);
2013-07-01 23:08:25 +02:00
/* mark all the block for rehashing */
for(i=0;i<blockmax;++i) {
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)) {
2013-09-22 23:54:49 +02:00
fprintf(stderr, "Internal inconsistency for a rehash already in progress\n");
2013-07-01 23:08:25 +02:00
exit(EXIT_FAILURE);
}
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;
2013-07-01 23:08:25 +02:00
printf(
2013-08-18 17:21:39 +02:00
"A rehash is now scheduled. It will take place progressively in the next\n"
"'sync' and 'scrub' commands. You can check the rehash progress using the\n"
"'status' command.\n");
}