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