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 : * Returns 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 : * Returns 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 kill_after_sync; /**< Kill the process after sync without saving the final state. */
95 : int force_murmur3; /**< Force Murmur3 choice. */
96 : int force_spooky2; /**< Force Spooky2 choice. */
97 : int force_order; /**< Force sorting order. One of the SORT_* defines. */
98 : unsigned force_scrub_at; /**< Force scrub for the specified number of blocks. */
99 : int force_scrub_even; /**< Force scrub of all the even blocks. */
100 : int force_content_write; /**< Force the update of the content file. */
101 : int force_content_text; /**< Force the use of text version of 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 : * Initializes the state.
180 : */
181 : void state_init(struct snapraid_state* state);
182 :
183 : /**
184 : * Deinitializes the state.
185 : */
186 : void state_done(struct snapraid_state* state);
187 :
188 : /**
189 : * Reads 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 : * Reads the state.
195 : */
196 : void state_read(struct snapraid_state* state);
197 :
198 : /**
199 : * Writes 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 : * Scans all the disks to update the state.
210 : */
211 : void state_scan(struct snapraid_state* state);
212 :
213 : /**
214 : * Sets 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 : * Syncs the parity data.
225 : */
226 : int state_sync(struct snapraid_state* state, block_off_t blockstart, block_off_t blockcount);
227 :
228 : /**
229 : * Checks (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 the files.
246 : */
247 : int state_scrub(struct snapraid_state* state, int percentage, int olderthan);
248 :
249 : /**
250 : * Print the status.
251 : */
252 : int state_status(struct snapraid_state* state);
253 :
254 : /**
255 : * Finds duplicates.
256 : */
257 : void state_dup(struct snapraid_state* state);
258 :
259 : /**
260 : * List content.
261 : */
262 : void state_list(struct snapraid_state* state);
263 :
264 : /**
265 : * Creates pool tree.
266 : */
267 : void state_pool(struct snapraid_state* state);
268 :
269 : /**
270 : * Refresh the free space info.
271 : *
272 : * Note that it requires disks access.
273 : */
274 : void state_refresh(struct snapraid_state* state);
275 :
276 : /**
277 : * Filter files, symlinks and dirs.
278 : * Apply an additional filter to the list currently loaded.
279 : */
280 : void state_filter(struct snapraid_state* state, tommy_list* filterlist_file, tommy_list* filterlist_disk, int filter_missing, int filter_error);
281 :
282 : /**
283 : * Begins the progress visualization.
284 : */
285 : int state_progress_begin(struct snapraid_state* state, block_off_t blockstart, block_off_t blockmax, block_off_t countmax);
286 :
287 : /**
288 : * Ends the progress visualization.
289 : */
290 : void state_progress_end(struct snapraid_state* state, block_off_t countpos, block_off_t countmax, data_off_t countsize);
291 :
292 : /**
293 : * Writes the progress.
294 : */
295 : int state_progress(struct snapraid_state* state, block_off_t blockpos, block_off_t countpos, block_off_t countmax, data_off_t countsize);
296 :
297 : /**
298 : * Stop temporarely the progress.
299 : */
300 : void state_progress_stop(struct snapraid_state* state);
301 :
302 : /**
303 : * Restart the progress.
304 : */
305 : void state_progress_restart(struct snapraid_state* state);
306 :
307 : /**
308 : * Set the usage time as wasted one not counted.
309 : */
310 80 : static inline void state_usage_waste(struct snapraid_state* state)
311 : {
312 80 : uint64_t now = tick();
313 :
314 80 : state->tick_last = now;
315 80 : }
316 :
317 : /**
318 : * Set the usage time for CPU.
319 : */
320 682853 : static inline void state_usage_cpu(struct snapraid_state* state)
321 : {
322 682853 : uint64_t now = tick();
323 682853 : uint64_t delta = now - state->tick_last;
324 :
325 : /* increment the time spent in computations */
326 682853 : state->tick_cpu += delta;
327 :
328 682853 : state->tick_last = now;
329 682853 : }
330 :
331 : /**
332 : * Set the usage time for data disk.
333 : */
334 544351 : static inline void state_usage_disk(struct snapraid_state* state, struct snapraid_disk* disk)
335 : {
336 544351 : uint64_t now = tick();
337 544351 : uint64_t delta = now - state->tick_last;
338 :
339 : /* increment the time spent in the data disk */
340 544351 : disk->tick += delta;
341 544351 : state->tick_io += delta;
342 :
343 544351 : state->tick_last = now;
344 544351 : }
345 :
346 : /**
347 : * Set the usage time for parity disk.
348 : */
349 582974 : static inline void state_usage_parity(struct snapraid_state* state, unsigned level)
350 : {
351 582974 : uint64_t now = tick();
352 582974 : uint64_t delta = now - state->tick_last;
353 :
354 582974 : assert(level < LEV_MAX);
355 :
356 : /* increment the time spent in the parity disk */
357 582974 : state->parity[level].tick += delta;
358 582974 : state->tick_io += delta;
359 :
360 582974 : state->tick_last = now;
361 582974 : }
362 :
363 : /**
364 : * Prints the stats of the usage time.
365 : */
366 : void state_usage_print(struct snapraid_state* state);
367 :
368 : /****************************************************************************/
369 : /* misc */
370 :
371 : /**
372 : * Generate a dummy configuration file from a content file.
373 : */
374 : void generate_configuration(const char* content);
375 :
376 : #endif
377 :
|