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 synching files now with zero size. */
78 : int force_empty; /**< Forced dangerous operations of synching disks now empty. */
79 : int force_uuid; /**< Forced dangerous operations of synching 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 synching 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 expected_missing; /**< If missing files are expected and should not be reported. */
107 : };
108 :
109 : /**
110 : * Number of measures of the operation progress.
111 : */
112 : #define PROGRESS_MAX 100
113 :
114 : struct snapraid_state {
115 : struct snapraid_option opt; /**< Setup options. */
116 : int filter_hidden; /**< Filter out hidden files. */
117 : uint64_t autosave; /**< Autosave after the specified amount of data. 0 to disable. */
118 : int need_write; /**< If the state is changed. */
119 : int checked_read; /**< If the state was read and checked. */
120 : uint32_t block_size; /**< Block size in bytes. */
121 : unsigned raid_mode; /**< Raid mode to use. RAID_MODE_DEFAULT or RAID_MODE_ALTERNATE. */
122 : int file_mode; /**< File access mode. Combination of MODE_* flags. */
123 : struct snapraid_parity parity[LEV_MAX]; /**< Parity vector. */
124 : char share[PATH_MAX]; /**< Path of the share tree. If !=0 pool links are created in a different way. */
125 : char pool[PATH_MAX]; /**< Path of the pool tree. */
126 : uint64_t pool_device; /**< Device identifier of the pool. */
127 : unsigned char hashseed[HASH_SIZE]; /**< Hash seed. Just after a uint64 to provide a minimal alignment. */
128 : unsigned char prevhashseed[HASH_SIZE]; /**< Previous hash seed. In case of rehash. */
129 : char lockfile[PATH_MAX]; /**< Path of the lock file to use. */
130 : unsigned level; /**< Number of parity levels. 1 for PAR1, 2 for PAR2. */
131 : unsigned hash; /**< Hash kind used. */
132 : unsigned prevhash; /**< Previous hash kind used. In case of rehash. */
133 : unsigned besthash; /**< Best hash suggested. */
134 : const char* command; /**< Command running. */
135 : tommy_list contentlist; /**< List of content files. */
136 : tommy_list disklist; /**< List of all the disks. */
137 : tommy_list maplist; /**< List of all the disk mappings. */
138 : tommy_list filterlist; /**< List of inclusion/exclusion. */
139 : tommy_list importlist; /**< List of import file. */
140 : tommy_hashdyn importset; /**< Hashtable by hash of all the import blocks. */
141 : tommy_hashdyn previmportset; /**< Hashtable by prevhash of all the import blocks. Valid only if we are in a rehash state. */
142 : tommy_hashdyn searchset; /**< Hashtable by timestamp of all the search files. */
143 : tommy_arrayblkof infoarr; /**< Block information array. */
144 :
145 : /**
146 : * Cumulative time used for computations.
147 : */
148 : uint64_t tick_cpu;
149 :
150 : /**
151 : * Cumulative time used for all io operations of disks.
152 : */
153 : uint64_t tick_io;
154 :
155 : /**
156 : * Last time used for time measure.
157 : */
158 : uint64_t tick_last;
159 :
160 : int clear_past_hash; /**< Clear all the hash from CHG and DELETED blocks when reading the state from an incomplete sync. */
161 :
162 : time_t progress_whole_start; /**< Initial start of the whole process. */
163 : time_t progress_interruption; /**< Time of the start of the progress interruption. */
164 : time_t progress_wasted; /**< Time wasted in interruptions. */
165 :
166 : time_t progress_time[PROGRESS_MAX]; /**< Last times of progress. */
167 : block_off_t progress_pos[PROGRESS_MAX]; /**< Last positions of progress. */
168 : data_off_t progress_size[PROGRESS_MAX]; /**< Last sizes of progress. */
169 : uint64_t progress_tick_cpu[PROGRESS_MAX]; /**< Last cpu ticks of progress. */
170 : uint64_t progress_tick_total[PROGRESS_MAX]; /**< Last total ticks of progress. */
171 :
172 : int progress_ptr; /**< Pointer to the next position to fill. Rolling over. */
173 : int progress_tick; /**< Number of measures done. */
174 :
175 : int no_conf; /**< Automatically add missing info. Used to load content without a configuration file. */
176 : };
177 :
178 : /**
179 : * Initialize the state.
180 : */
181 : void state_init(struct snapraid_state* state);
182 :
183 : /**
184 : * Deinitialize the state.
185 : */
186 : void state_done(struct snapraid_state* state);
187 :
188 : /**
189 : * Read the configuration file.
190 : */
191 : void state_config(struct snapraid_state* state, const char* path, const char* command, struct snapraid_option* opt, tommy_list* filterlist_disk);
192 :
193 : /**
194 : * Read the state.
195 : */
196 : void state_read(struct snapraid_state* state);
197 :
198 : /**
199 : * Write the new state.
200 : */
201 : void state_write(struct snapraid_state* state);
202 :
203 : /**
204 : * Diff all the disks.
205 : */
206 : int state_diff(struct snapraid_state* state);
207 :
208 : /**
209 : * Scan all the disks to update the state.
210 : */
211 : void state_scan(struct snapraid_state* state);
212 :
213 : /**
214 : * Set the nanosecond timestamp of all files that have a zero value.
215 : */
216 : void state_nano(struct snapraid_state* state);
217 :
218 : /**
219 : * Devices operations.
220 : */
221 : void state_device(struct snapraid_state* state, int operation);
222 :
223 : /**
224 : * Sync the parity data.
225 : */
226 : int state_sync(struct snapraid_state* state, block_off_t blockstart, block_off_t blockcount);
227 :
228 : /**
229 : * Check (and fixes) all the files and parity data.
230 : * \param fix If we have to fix, after checking.
231 : */
232 : int state_check(struct snapraid_state* state, int fix, block_off_t blockstart, block_off_t blockcount);
233 :
234 : /**
235 : * Dry the files.
236 : */
237 : void state_dry(struct snapraid_state* state, block_off_t blockstart, block_off_t blockcount);
238 :
239 : /**
240 : * Rehash the files.
241 : */
242 : void state_rehash(struct snapraid_state* state);
243 :
244 : /**
245 : * Scrub levels.
246 : */
247 : #define SCRUB_AUTO -1 /**< Automatic selection. */
248 : #define SCRUB_BAD -2 /**< Scrub only the bad blocks. */
249 : #define SCRUB_NEW -3 /**< Scub the new blocks. */
250 : #define SCRUB_FULL -4 /**< Scrub everything. */
251 : #define SCRUB_EVEN -5 /**< Even blocks. */
252 :
253 : /**
254 : * Scrub the files.
255 : */
256 : int state_scrub(struct snapraid_state* state, int plan, int olderthan);
257 :
258 : /**
259 : * Print the status.
260 : */
261 : int state_status(struct snapraid_state* state);
262 :
263 : /**
264 : * Find duplicates.
265 : */
266 : void state_dup(struct snapraid_state* state);
267 :
268 : /**
269 : * List content.
270 : */
271 : void state_list(struct snapraid_state* state);
272 :
273 : /**
274 : * Create pool tree.
275 : */
276 : void state_pool(struct snapraid_state* state);
277 :
278 : /**
279 : * Refresh the free space info.
280 : *
281 : * Note that it requires disks access.
282 : */
283 : void state_refresh(struct snapraid_state* state);
284 :
285 : /**
286 : * Skip files, symlinks and dirs.
287 : * Apply any skip access disk.
288 : */
289 : void state_skip(struct snapraid_state* state);
290 :
291 : /**
292 : * Filter files, symlinks and dirs.
293 : * Apply an additional filter to the list currently loaded.
294 : */
295 : void state_filter(struct snapraid_state* state, tommy_list* filterlist_file, tommy_list* filterlist_disk, int filter_missing, int filter_error);
296 :
297 : /**
298 : * Begin the progress visualization.
299 : */
300 : int state_progress_begin(struct snapraid_state* state, block_off_t blockstart, block_off_t blockmax, block_off_t countmax);
301 :
302 : /**
303 : * End the progress visualization.
304 : */
305 : void state_progress_end(struct snapraid_state* state, block_off_t countpos, block_off_t countmax, data_off_t countsize);
306 :
307 : /**
308 : * Write the progress.
309 : */
310 : int state_progress(struct snapraid_state* state, block_off_t blockpos, block_off_t countpos, block_off_t countmax, data_off_t countsize);
311 :
312 : /**
313 : * Stop temporarely the progress.
314 : */
315 : void state_progress_stop(struct snapraid_state* state);
316 :
317 : /**
318 : * Restart the progress.
319 : */
320 : void state_progress_restart(struct snapraid_state* state);
321 :
322 : /**
323 : * Set the usage time as wasted one not counted.
324 : */
325 89 : static inline void state_usage_waste(struct snapraid_state* state)
326 : {
327 89 : uint64_t now = tick();
328 :
329 89 : state->tick_last = now;
330 89 : }
331 :
332 : /**
333 : * Set the usage time for CPU.
334 : */
335 771844 : static inline void state_usage_cpu(struct snapraid_state* state)
336 : {
337 771844 : uint64_t now = tick();
338 771844 : uint64_t delta = now - state->tick_last;
339 :
340 : /* increment the time spent in computations */
341 771844 : state->tick_cpu += delta;
342 :
343 771844 : state->tick_last = now;
344 771844 : }
345 :
346 : /**
347 : * Set the usage time for data disk.
348 : */
349 621730 : static inline void state_usage_disk(struct snapraid_state* state, struct snapraid_disk* disk)
350 : {
351 621730 : uint64_t now = tick();
352 621730 : uint64_t delta = now - state->tick_last;
353 :
354 : /* increment the time spent in the data disk */
355 621730 : disk->tick += delta;
356 621730 : state->tick_io += delta;
357 :
358 621730 : state->tick_last = now;
359 621730 : }
360 :
361 : /**
362 : * Set the usage time for parity disk.
363 : */
364 652636 : static inline void state_usage_parity(struct snapraid_state* state, unsigned level)
365 : {
366 652636 : uint64_t now = tick();
367 652636 : uint64_t delta = now - state->tick_last;
368 :
369 652636 : assert(level < LEV_MAX);
370 :
371 : /* increment the time spent in the parity disk */
372 652636 : state->parity[level].tick += delta;
373 652636 : state->tick_io += delta;
374 :
375 652636 : state->tick_last = now;
376 652636 : }
377 :
378 : /**
379 : * Print the stats of the usage time.
380 : */
381 : void state_usage_print(struct snapraid_state* state);
382 :
383 : /**
384 : * Check the filesystem on all disks.
385 : * On error it aborts.
386 : */
387 : void state_fscheck(struct snapraid_state* state, const char* ope);
388 :
389 : /****************************************************************************/
390 : /* misc */
391 :
392 : /**
393 : * Generate a dummy configuration file from a content file.
394 : */
395 : void generate_configuration(const char* content);
396 :
397 : #endif
398 :
|