Line data Source code
1 : /*
2 : * Copyright (C) 2011 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 : #ifndef __STATE_H
19 : #define __STATE_H
20 :
21 : #include "elem.h"
22 :
23 : /****************************************************************************/
24 : /* parity level */
25 :
26 : /**
27 : * Max level of parity supported.
28 : */
29 : #define LEV_MAX 6
30 :
31 : /**
32 : * Return the parity name: Parity, 2-Parity, 3-Parity, 4-Parity, 5-Parity, 6-Parity.
33 : */
34 : const char* lev_name(unsigned level);
35 :
36 : /**
37 : * Return the parity name used in the config file: parity, 2-parity, 3-parity, 4-parity, 5-parity, 6-parity.
38 : */
39 : const char* lev_config_name(unsigned level);
40 :
41 : /****************************************************************************/
42 : /* state */
43 :
44 : /**
45 : * Units for disk space.
46 : */
47 : #define MEGA (1000 * 1000)
48 : #define GIGA (1000 * 1000 * 1000)
49 : #define TERA (1000 * 1000 * 1000 * 1000LL)
50 :
51 : /**
52 : * File modes.
53 : */
54 : #define MODE_SEQUENTIAL 1 /**< Open the file in sequential mode. */
55 :
56 : /**
57 : * Global variable to identify if Ctrl+C is pressed.
58 : */
59 : extern volatile int global_interrupt;
60 :
61 : #define SORT_PHYSICAL 1 /**< Sort by physical order. */
62 : #define SORT_INODE 2 /**< Sort by inode. */
63 : #define SORT_ALPHA 3 /**< Sort by alphabetic order. */
64 : #define SORT_DIR 4 /**< Sort by directory order. */
65 :
66 : /**
67 : * Options set only at startup.
68 : * For all these options a value of 0 means nothing set, and to use the default.
69 : */
70 : struct snapraid_option {
71 : int gui; /**< Gui output. */
72 : int auditonly; /**< In check, checks only the hash and not the parity. */
73 : int badonly; /**< In fix, fixes only the blocks marked as bad. */
74 : int syncedonly; /**< In fix, fixes only files that are synced. */
75 : int prehash; /**< Enables the prehash mode for sync. */
76 : unsigned io_error_limit; /**< Max number of input/output errors before aborting. */
77 : int force_zero; /**< Forced dangerous operations of syncing files now with zero size. */
78 : int force_empty; /**< Forced dangerous operations of syncing disks now empty. */
79 : int force_uuid; /**< Forced dangerous operations of syncing disks with uuid changed. */
80 : int force_device; /**< Forced dangerous operations of using disks with save device id. */
81 : int force_nocopy; /**< Force dangerous operations of syncing files without using copy detection. */
82 : int force_full; /**< Force a full sync when using an old content file. */
83 : int expect_unrecoverable; /**< Expect presence of unrecoverable error in checking or fixing. */
84 : int expect_recoverable; /**< Expect presence of recoverable error in checking. */
85 : int skip_device; /**< Skip devices matching checks. */
86 : int skip_sign; /**< Skip the sign check for content files. */
87 : int skip_fallocate; /**< Skip the use of fallocate(). */
88 : int skip_sequential; /**< Skip sequential hint. */
89 : int skip_lock; /**< Skip the lock file protection. */
90 : int skip_self; /**< Skip the self-test. */
91 : int skip_content_check; /**< Relax some content file checks. */
92 : int skip_parity_access; /**< Skip the parity access for commands that don't need it. */
93 : int skip_disk_access; /**< Skip the data disk access for commands that don't need it. */
94 : int skip_content_access; /**< Skip the content access for commands that don't need it. */
95 : int kill_after_sync; /**< Kill the process after sync without saving the final state. */
96 : int force_murmur3; /**< Force Murmur3 choice. */
97 : int force_spooky2; /**< Force Spooky2 choice. */
98 : int force_order; /**< Force sorting order. One of the SORT_* defines. */
99 : unsigned force_scrub_at; /**< Force scrub for the specified number of blocks. */
100 : int force_scrub_even; /**< Force scrub of all the even blocks. */
101 : int force_content_write; /**< Force the update of the content file. */
102 : int force_scan_winfind; /**< Force the use of FindFirst/Next in Windows to list directories. */
103 : int force_progress; /**< Force the use of the progress status. */
104 : unsigned force_autosave_at; /**< Force autosave at the specified block. */
105 : int fake_device; /**< Fake device data. */
106 : int no_warnings; /**< Remove some warning messages. */
107 : int expected_missing; /**< If missing files are expected and should not be reported. */
108 : int fake_uuid; /**< Set fakes UUID for testing. */
109 : int match_first_uuid; /**< Force the matching of the first UUID. */
110 : int force_parity_update; /**< Force parity update even if data is not changed. */
111 : };
112 :
113 : /**
114 : * Number of measures of the operation progress.
115 : */
116 : #define PROGRESS_MAX 100
117 :
118 : struct snapraid_state {
119 : struct snapraid_option opt; /**< Setup options. */
120 : int filter_hidden; /**< Filter out hidden files. */
121 : uint64_t autosave; /**< Autosave after the specified amount of data. 0 to disable. */
122 : int need_write; /**< If the state is changed. */
123 : int checked_read; /**< If the state was read and checked. */
124 : uint32_t block_size; /**< Block size in bytes. */
125 : unsigned raid_mode; /**< Raid mode to use. RAID_MODE_DEFAULT or RAID_MODE_ALTERNATE. */
126 : int file_mode; /**< File access mode. Combination of MODE_* flags. */
127 : struct snapraid_parity parity[LEV_MAX]; /**< Parity vector. */
128 : char share[PATH_MAX]; /**< Path of the share tree. If !=0 pool links are created in a different way. */
129 : char pool[PATH_MAX]; /**< Path of the pool tree. */
130 : uint64_t pool_device; /**< Device identifier of the pool. */
131 : unsigned char hashseed[HASH_SIZE]; /**< Hash seed. Just after a uint64 to provide a minimal alignment. */
132 : unsigned char prevhashseed[HASH_SIZE]; /**< Previous hash seed. In case of rehash. */
133 : char lockfile[PATH_MAX]; /**< Path of the lock file to use. */
134 : unsigned level; /**< Number of parity levels. 1 for PAR1, 2 for PAR2. */
135 : unsigned hash; /**< Hash kind used. */
136 : unsigned prevhash; /**< Previous hash kind used. In case of rehash. */
137 : unsigned besthash; /**< Best hash suggested. */
138 : const char* command; /**< Command running. */
139 : tommy_list contentlist; /**< List of content files. */
140 : tommy_list disklist; /**< List of all the disks. */
141 : tommy_list maplist; /**< List of all the disk mappings. */
142 : tommy_list filterlist; /**< List of inclusion/exclusion. */
143 : tommy_list importlist; /**< List of import file. */
144 : tommy_hashdyn importset; /**< Hashtable by hash of all the import blocks. */
145 : tommy_hashdyn previmportset; /**< Hashtable by prevhash of all the import blocks. Valid only if we are in a rehash state. */
146 : tommy_hashdyn searchset; /**< Hashtable by timestamp of all the search files. */
147 : tommy_arrayblkof infoarr; /**< Block information array. */
148 :
149 : /**
150 : * Cumulative time used for computations.
151 : */
152 : uint64_t tick_cpu;
153 :
154 : /**
155 : * Cumulative time used for all io operations of disks.
156 : */
157 : uint64_t tick_io;
158 :
159 : /**
160 : * Last time used for time measure.
161 : */
162 : uint64_t tick_last;
163 :
164 : int clear_past_hash; /**< Clear all the hash from CHG and DELETED blocks when reading the state from an incomplete sync. */
165 :
166 : time_t progress_whole_start; /**< Initial start of the whole process. */
167 : time_t progress_interruption; /**< Time of the start of the progress interruption. */
168 : time_t progress_wasted; /**< Time wasted in interruptions. */
169 :
170 : time_t progress_time[PROGRESS_MAX]; /**< Last times of progress. */
171 : block_off_t progress_pos[PROGRESS_MAX]; /**< Last positions of progress. */
172 : data_off_t progress_size[PROGRESS_MAX]; /**< Last sizes of progress. */
173 : uint64_t progress_tick_cpu[PROGRESS_MAX]; /**< Last cpu ticks of progress. */
174 : uint64_t progress_tick_total[PROGRESS_MAX]; /**< Last total ticks of progress. */
175 :
176 : int progress_ptr; /**< Pointer to the next position to fill. Rolling over. */
177 : int progress_tick; /**< Number of measures done. */
178 :
179 : int no_conf; /**< Automatically add missing info. Used to load content without a configuration file. */
180 : };
181 :
182 : /**
183 : * Initialize the state.
184 : */
185 : void state_init(struct snapraid_state* state);
186 :
187 : /**
188 : * Deinitialize the state.
189 : */
190 : void state_done(struct snapraid_state* state);
191 :
192 : /**
193 : * Read the configuration file.
194 : */
195 : void state_config(struct snapraid_state* state, const char* path, const char* command, struct snapraid_option* opt, tommy_list* filterlist_disk);
196 :
197 : /**
198 : * Read the state.
199 : */
200 : void state_read(struct snapraid_state* state);
201 :
202 : /**
203 : * Write the new state.
204 : */
205 : void state_write(struct snapraid_state* state);
206 :
207 : /**
208 : * Diff all the disks.
209 : */
210 : int state_diff(struct snapraid_state* state);
211 :
212 : /**
213 : * Scan all the disks to update the state.
214 : */
215 : void state_scan(struct snapraid_state* state);
216 :
217 : /**
218 : * Set the nanosecond timestamp of all files that have a zero value.
219 : */
220 : void state_nano(struct snapraid_state* state);
221 :
222 : /**
223 : * Devices operations.
224 : */
225 : void state_device(struct snapraid_state* state, int operation);
226 :
227 : /**
228 : * Sync the parity data.
229 : */
230 : int state_sync(struct snapraid_state* state, block_off_t blockstart, block_off_t blockcount);
231 :
232 : /**
233 : * Check (and fixes) all the files and parity data.
234 : * \param fix If we have to fix, after checking.
235 : */
236 : int state_check(struct snapraid_state* state, int fix, block_off_t blockstart, block_off_t blockcount);
237 :
238 : /**
239 : * Dry the files.
240 : */
241 : void state_dry(struct snapraid_state* state, block_off_t blockstart, block_off_t blockcount);
242 :
243 : /**
244 : * Rehash the files.
245 : */
246 : void state_rehash(struct snapraid_state* state);
247 :
248 : /**
249 : * Scrub levels.
250 : */
251 : #define SCRUB_AUTO -1 /**< Automatic selection. */
252 : #define SCRUB_BAD -2 /**< Scrub only the bad blocks. */
253 : #define SCRUB_NEW -3 /**< Scub the new blocks. */
254 : #define SCRUB_FULL -4 /**< Scrub everything. */
255 : #define SCRUB_EVEN -5 /**< Even blocks. */
256 :
257 : /**
258 : * Scrub the files.
259 : */
260 : int state_scrub(struct snapraid_state* state, int plan, int olderthan);
261 :
262 : /**
263 : * Print the status.
264 : */
265 : int state_status(struct snapraid_state* state);
266 :
267 : /**
268 : * Find duplicates.
269 : */
270 : void state_dup(struct snapraid_state* state);
271 :
272 : /**
273 : * List content.
274 : */
275 : void state_list(struct snapraid_state* state);
276 :
277 : /**
278 : * Create pool tree.
279 : */
280 : void state_pool(struct snapraid_state* state);
281 :
282 : /**
283 : * Refresh the free space info.
284 : *
285 : * Note that it requires disks access.
286 : */
287 : void state_refresh(struct snapraid_state* state);
288 :
289 : /**
290 : * Skip files, symlinks and dirs.
291 : * Apply any skip access disk.
292 : */
293 : void state_skip(struct snapraid_state* state);
294 :
295 : /**
296 : * Filter files, symlinks and dirs.
297 : * Apply an additional filter to the list currently loaded.
298 : */
299 : void state_filter(struct snapraid_state* state, tommy_list* filterlist_file, tommy_list* filterlist_disk, int filter_missing, int filter_error);
300 :
301 : /**
302 : * Begin the progress visualization.
303 : */
304 : int state_progress_begin(struct snapraid_state* state, block_off_t blockstart, block_off_t blockmax, block_off_t countmax);
305 :
306 : /**
307 : * End the progress visualization.
308 : */
309 : void state_progress_end(struct snapraid_state* state, block_off_t countpos, block_off_t countmax, data_off_t countsize);
310 :
311 : /**
312 : * Write the progress.
313 : */
314 : int state_progress(struct snapraid_state* state, block_off_t blockpos, block_off_t countpos, block_off_t countmax, data_off_t countsize);
315 :
316 : /**
317 : * Stop temporarily the progress.
318 : */
319 : void state_progress_stop(struct snapraid_state* state);
320 :
321 : /**
322 : * Restart the progress.
323 : */
324 : void state_progress_restart(struct snapraid_state* state);
325 :
326 : /**
327 : * Set the usage time as wasted one not counted.
328 : */
329 99 : static inline void state_usage_waste(struct snapraid_state* state)
330 : {
331 99 : uint64_t now = tick();
332 :
333 99 : state->tick_last = now;
334 99 : }
335 :
336 : /**
337 : * Set the usage time for CPU.
338 : */
339 833647 : static inline void state_usage_cpu(struct snapraid_state* state)
340 : {
341 833647 : uint64_t now = tick();
342 833647 : uint64_t delta = now - state->tick_last;
343 :
344 : /* increment the time spent in computations */
345 833647 : state->tick_cpu += delta;
346 :
347 833647 : state->tick_last = now;
348 833647 : }
349 :
350 : /**
351 : * Set the usage time for data disk.
352 : */
353 674079 : static inline void state_usage_disk(struct snapraid_state* state, struct snapraid_disk* disk)
354 : {
355 674079 : uint64_t now = tick();
356 674079 : uint64_t delta = now - state->tick_last;
357 :
358 : /* increment the time spent in the data disk */
359 674079 : disk->tick += delta;
360 674079 : state->tick_io += delta;
361 :
362 674079 : state->tick_last = now;
363 674079 : }
364 :
365 : /**
366 : * Set the usage time for parity disk.
367 : */
368 707815 : static inline void state_usage_parity(struct snapraid_state* state, unsigned level)
369 : {
370 707815 : uint64_t now = tick();
371 707815 : uint64_t delta = now - state->tick_last;
372 :
373 707815 : assert(level < LEV_MAX);
374 :
375 : /* increment the time spent in the parity disk */
376 707815 : state->parity[level].tick += delta;
377 707815 : state->tick_io += delta;
378 :
379 707815 : state->tick_last = now;
380 707815 : }
381 :
382 : /**
383 : * Print the stats of the usage time.
384 : */
385 : void state_usage_print(struct snapraid_state* state);
386 :
387 : /**
388 : * Check the file-system on all disks.
389 : * On error it aborts.
390 : */
391 : void state_fscheck(struct snapraid_state* state, const char* ope);
392 :
393 : /****************************************************************************/
394 : /* misc */
395 :
396 : /**
397 : * Generate a dummy configuration file from a content file.
398 : */
399 : void generate_configuration(const char* content);
400 :
401 : #endif
402 :
|