Line data Source code
1 : /*
2 : * Copyright (C) 2013 Andrea Mazzoleni
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "portable.h"
19 :
20 : #include "util.h"
21 : #include "elem.h"
22 : #include "import.h"
23 : #include "state.h"
24 : #include "parity.h"
25 : #include "handle.h"
26 : #include "raid/raid.h"
27 :
28 : /****************************************************************************/
29 : /* rehash */
30 :
31 1 : void state_rehash(struct snapraid_state* state)
32 : {
33 : block_off_t blockmax;
34 : block_off_t i;
35 :
36 1 : blockmax = parity_allocated_size(state);
37 :
38 : /* check if a rehash is already in progress */
39 1 : if (state->prevhash != HASH_UNDEFINED) {
40 : /* LCOV_EXCL_START */
41 : log_fatal("You already have a rehash in progress.\n");
42 : exit(EXIT_FAILURE);
43 : /* LCOV_EXCL_STOP */
44 : }
45 :
46 1 : if (state->hash == state->besthash) {
47 : /* LCOV_EXCL_START */
48 : log_fatal("You are already using the best hash for your platform.\n");
49 : exit(EXIT_FAILURE);
50 : /* LCOV_EXCL_STOP */
51 : }
52 :
53 : /* copy the present hash as previous one */
54 1 : state->prevhash = state->hash;
55 1 : memcpy(state->prevhashseed, state->hashseed, HASH_MAX);
56 :
57 : /* set the new hash and seed */
58 1 : state->hash = state->besthash;
59 1 : if (randomize(state->hashseed, HASH_MAX) != 0) {
60 : /* LCOV_EXCL_START */
61 : log_fatal("Failed to get random values.\n");
62 : exit(EXIT_FAILURE);
63 : /* LCOV_EXCL_STOP */
64 : }
65 :
66 : /* mark all the block for rehashing */
67 9216 : for (i = 0; i < blockmax; ++i) {
68 : snapraid_info info;
69 :
70 : /* if it's unused */
71 9215 : info = info_get(&state->infoarr, i);
72 9215 : if (info == 0) {
73 : /* skip it */
74 0 : continue;
75 : }
76 :
77 9215 : if (info_get_rehash(info)) {
78 : /* LCOV_EXCL_START */
79 : log_fatal("Internal inconsistency for a rehash already in progress\n");
80 : os_abort();
81 : /* LCOV_EXCL_STOP */
82 : }
83 :
84 : /* enable the rehash */
85 9215 : info = info_set_rehash(info);
86 :
87 : /* save it */
88 9215 : info_set(&state->infoarr, i, info);
89 : }
90 :
91 : /* save the new content file */
92 1 : state->need_write = 1;
93 :
94 1 : msg_status("A rehash is now scheduled. It will take place progressively in the next\n");
95 1 : msg_status("'sync' and 'scrub' commands. You can check the rehash progress using the\n");
96 1 : msg_status("'status' command.\n");
97 1 : }
98 :
|