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 : #include "portable.h"
19 :
20 : #include "snapraid.h"
21 : #include "support.h"
22 : #include "elem.h"
23 : #include "import.h"
24 : #include "search.h"
25 : #include "state.h"
26 : #include "io.h"
27 : #include "raid/raid.h"
28 :
29 : /****************************************************************************/
30 : /* main */
31 :
32 2 : void version(void)
33 : {
34 2 : msg_status(PACKAGE " v" VERSION " by Andrea Mazzoleni, " PACKAGE_URL "\n");
35 2 : }
36 :
37 1 : void usage(void)
38 : {
39 1 : version();
40 :
41 1 : printf("Usage: " PACKAGE " status|diff|sync|scrub|list|dup|up|down|smart|pool|check|fix [options]\n");
42 1 : printf("\n");
43 1 : printf("Commands:\n");
44 1 : printf(" status Print the status of the array\n");
45 1 : printf(" diff Show the changes that needs to be synchronized\n");
46 1 : printf(" sync Synchronize the state of the array\n");
47 1 : printf(" scrub Scrub the array\n");
48 1 : printf(" list List the array content\n");
49 1 : printf(" dup Find duplicate files\n");
50 1 : printf(" up Spin-up the array\n");
51 1 : printf(" down Spin-down the array\n");
52 1 : printf(" smart SMART attributes of the array\n");
53 1 : printf(" pool Create or update the virtual view of the array\n");
54 1 : printf(" check Check the array\n");
55 1 : printf(" fix Fix the array\n");
56 1 : printf("\n");
57 1 : printf("Options:\n");
58 1 : printf(" " SWITCH_GETOPT_LONG("-c, --conf FILE ", "-c") " Configuration file\n");
59 1 : printf(" " SWITCH_GETOPT_LONG("-f, --filter PATTERN ", "-f") " Process only files matching the pattern\n");
60 1 : printf(" " SWITCH_GETOPT_LONG("-d, --filter-disk NAME", "-f") " Process only files in the specified disk\n");
61 1 : printf(" " SWITCH_GETOPT_LONG("-m, --filter-missing ", "-m") " Process only missing/deleted files\n");
62 1 : printf(" " SWITCH_GETOPT_LONG("-e, --filter-error ", "-e") " Process only files with errors\n");
63 1 : printf(" " SWITCH_GETOPT_LONG("-p, --plan PLAN ", "-p") " Define a scrub plan or percentage\n");
64 1 : printf(" " SWITCH_GETOPT_LONG("-o, --older-than DAYS ", "-o") " Process only the older part of the array\n");
65 1 : printf(" " SWITCH_GETOPT_LONG("-i, --import DIR ", "-i") " Import deleted files\n");
66 1 : printf(" " SWITCH_GETOPT_LONG("-l, --log FILE ", "-l") " Log file. Default none\n");
67 1 : printf(" " SWITCH_GETOPT_LONG("-a, --audit-only ", "-a") " Check only file data and not parity\n");
68 1 : printf(" " SWITCH_GETOPT_LONG("-h, --pre-hash ", "-h") " Pre-hash all the new data\n");
69 1 : printf(" " SWITCH_GETOPT_LONG("-Z, --force-zero ", "-Z") " Force syncing of files that get zero size\n");
70 1 : printf(" " SWITCH_GETOPT_LONG("-E, --force-empty ", "-E") " Force syncing of disks that get empty\n");
71 1 : printf(" " SWITCH_GETOPT_LONG("-U, --force-uuid ", "-U") " Force commands on disks with uuid changed\n");
72 1 : printf(" " SWITCH_GETOPT_LONG("-D, --force-device ", "-D") " Force commands with inaccessible/shared disks\n");
73 1 : printf(" " SWITCH_GETOPT_LONG("-N, --force-nocopy ", "-N") " Force commands disabling the copy detection\n");
74 1 : printf(" " SWITCH_GETOPT_LONG("-F, --force-full ", "-F") " Force a full parity computation in sync\n");
75 1 : printf(" " SWITCH_GETOPT_LONG("-R, --force-realloc ", "-R") " Force a full parity reallocation in sync\n");
76 1 : printf(" " SWITCH_GETOPT_LONG("-v, --verbose ", "-v") " Verbose\n");
77 1 : }
78 :
79 237 : void memory(void)
80 : {
81 237 : log_tag("memory:used:%" PRIu64 "\n", (uint64_t)malloc_counter_get());
82 :
83 : /* size of the block */
84 237 : log_tag("memory:block:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_block)));
85 237 : log_tag("memory:extent:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_extent)));
86 237 : log_tag("memory:file:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_file)));
87 237 : log_tag("memory:link:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_link)));
88 237 : log_tag("memory:dir:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_dir)));
89 :
90 237 : msg_progress("Using %u MiB of memory for the FileSystem.\n", (unsigned)(malloc_counter_get() / MEBI));
91 237 : }
92 :
93 276 : void test(int argc, char* argv[])
94 : {
95 : int i;
96 : char buffer[ESC_MAX];
97 :
98 : /* special testing code for quoting */
99 276 : if (argc < 2 || strcmp(argv[1], "test") != 0)
100 550 : return;
101 :
102 1 : for (i = 2; i < argc; ++i) {
103 0 : printf("argv[%d]\n", i);
104 0 : printf("\t#%s#\n", argv[i]);
105 0 : printf("\t#%s#\n", esc_shell(argv[i], buffer));
106 : }
107 :
108 : #ifdef _WIN32
109 : assert(strcmp(esc_shell(" ", buffer), "\" \"") == 0);
110 : assert(strcmp(esc_shell(" \" ", buffer), "\" \"\\\"\" \"") == 0);
111 : assert(strcmp(esc_shell("&|()<>^", buffer), "^&^|^(^)^<^>^^") == 0);
112 : assert(strcmp(esc_shell("&|()<>^ ", buffer), "\"&|()<>^ \"") == 0);
113 : #else
114 1 : assert(strcmp(esc_shell(",._+:@%%/-", buffer), ",._+:@%%/-") == 0);
115 1 : assert(strcmp(esc_shell(" ", buffer), "\\ ") == 0);
116 : #endif
117 :
118 1 : printf("Everything OK\n");
119 :
120 1 : exit(EXIT_SUCCESS);
121 : }
122 :
123 : /****************************************************************************/
124 : /* log */
125 :
126 265 : void log_open(const char* file)
127 : {
128 : char path[PATH_MAX];
129 : const char* mode;
130 : char text_T[32];
131 : char text_D[32];
132 : time_t t;
133 : struct tm* tm;
134 :
135 : /* leave stdlog at 0 if not specified */
136 265 : if (file == 0)
137 376 : return;
138 :
139 80 : t = time(0);
140 80 : tm = localtime(&t);
141 80 : if (tm) {
142 80 : strftime(text_T, sizeof(text_T), "%H%M%S", tm);
143 80 : strftime(text_D, sizeof(text_T), "%Y%m%d", tm);
144 : } else {
145 : /* LCOV_EXCL_START */
146 : strcpy(text_T, "invalid");
147 : strcpy(text_D, "invalid");
148 : /* LCOV_EXCL_STOP */
149 : }
150 :
151 : /* file mode */
152 80 : mode = "wt";
153 80 : if (*file == '>') {
154 7 : ++file;
155 :
156 7 : if (*file == '>') {
157 1 : mode = "at";
158 1 : ++file;
159 : }
160 :
161 7 : if (file[0] == '&' && file[1] == '1') {
162 5 : stdlog = stdout;
163 5 : return;
164 : }
165 :
166 2 : if (file[0] == '&' && file[1] == '2') {
167 1 : stdlog = stderr;
168 1 : return;
169 : }
170 : }
171 :
172 : /* process the path */
173 938 : for (*path = 0; *file != 0; ) {
174 790 : switch (*file) {
175 : case '%' :
176 2 : ++file;
177 2 : switch (*file) {
178 : case '%' :
179 0 : pathcatc(path, sizeof(path), '%');
180 0 : break;
181 : case 'T' :
182 1 : pathcat(path, sizeof(path), text_T);
183 1 : break;
184 : case 'D' :
185 1 : pathcat(path, sizeof(path), text_D);
186 1 : break;
187 : default :
188 : /* LCOV_EXCL_START */
189 : log_fatal("Invalid type specifier '%c' in the log file.\n", *file);
190 : exit(EXIT_FAILURE);
191 : /* LCOV_EXCL_STOP */
192 : }
193 2 : break;
194 : default :
195 788 : pathcatc(path, sizeof(path), *file);
196 788 : break;
197 : }
198 790 : ++file;
199 : }
200 :
201 74 : stdlog = fopen(path, mode);
202 74 : if (!stdlog) {
203 : /* LCOV_EXCL_START */
204 : log_fatal("Error opening the log file '%s'. %s.\n", path, strerror(errno));
205 : exit(EXIT_FAILURE);
206 : /* LCOV_EXCL_STOP */
207 : }
208 : }
209 :
210 242 : void log_close(const char* file)
211 : {
212 242 : if (stdlog != stdout && stdlog != stderr && stdlog != 0) {
213 74 : if (fclose(stdlog) != 0) {
214 : /* LCOV_EXCL_START */
215 : log_fatal("Error closing the log file '%s'. %s.\n", file, strerror(errno));
216 : exit(EXIT_FAILURE);
217 : /* LCOV_EXCL_STOP */
218 : }
219 : }
220 :
221 242 : stdlog = 0;
222 242 : }
223 :
224 : /****************************************************************************/
225 : /* config */
226 :
227 275 : void config(char* conf, size_t conf_size, const char* argv0)
228 : {
229 : #ifdef _WIN32
230 : char* slash;
231 :
232 : pathimport(conf, conf_size, argv0);
233 :
234 : slash = strrchr(conf, '/');
235 : if (slash) {
236 : slash[1] = 0;
237 : pathcat(conf, conf_size, PACKAGE ".conf");
238 : } else {
239 : pathcpy(conf, conf_size, PACKAGE ".conf");
240 : }
241 : #else
242 : (void)argv0;
243 :
244 275 : pathcpy(conf, conf_size, "/etc/" PACKAGE ".conf");
245 : #endif
246 275 : }
247 :
248 : /****************************************************************************/
249 : /* main */
250 :
251 : #define OPT_TEST_SKIP_SELF 256
252 : #define OPT_TEST_KILL_AFTER_SYNC 257
253 : #define OPT_TEST_EXPECT_UNRECOVERABLE 258
254 : #define OPT_TEST_EXPECT_RECOVERABLE 259
255 : #define OPT_TEST_SKIP_SIGN 260
256 : #define OPT_TEST_SKIP_FALLOCATE 261
257 : #define OPT_TEST_SKIP_DEVICE 262
258 : #define OPT_TEST_FORCE_MURMUR3 264
259 : #define OPT_TEST_FORCE_SPOOKY2 265
260 : #define OPT_TEST_SKIP_LOCK 266
261 : #define OPT_TEST_FORCE_ORDER_PHYSICAL 267
262 : #define OPT_TEST_FORCE_ORDER_INODE 268
263 : #define OPT_TEST_FORCE_ORDER_ALPHA 269
264 : #define OPT_TEST_FORCE_ORDER_DIR 270
265 : #define OPT_TEST_FORCE_SCRUB_AT 271
266 : #define OPT_TEST_FORCE_SCRUB_EVEN 272
267 : #define OPT_TEST_FORCE_CONTENT_WRITE 273
268 : #define OPT_TEST_SKIP_CONTENT_CHECK 275
269 : #define OPT_TEST_SKIP_PARITY_ACCESS 276
270 : #define OPT_TEST_EXPECT_FAILURE 277
271 : #define OPT_TEST_RUN 278
272 : #define OPT_TEST_FORCE_SCAN_WINFIND 279
273 : #define OPT_TEST_IMPORT_CONTENT 280
274 : #define OPT_TEST_FORCE_PROGRESS 281
275 : #define OPT_TEST_SKIP_DISK_ACCESS 282
276 : #define OPT_TEST_FORCE_AUTOSAVE_AT 283
277 : #define OPT_TEST_FAKE_DEVICE 284
278 : #define OPT_TEST_EXPECT_NEED_SYNC 285
279 : #define OPT_NO_WARNINGS 286
280 : #define OPT_TEST_FAKE_UUID 287
281 : #define OPT_TEST_MATCH_FIRST_UUID 288
282 : #define OPT_TEST_FORCE_PARITY_UPDATE 289
283 : #define OPT_TEST_IO_CACHE 290
284 : #define OPT_TEST_IO_STATS 291
285 : #define OPT_TEST_COND_SIGNAL_OUTSIDE 292
286 : #define OPT_TEST_IO_ADVISE_NONE 293
287 : #define OPT_TEST_IO_ADVISE_SEQUENTIAL 294
288 : #define OPT_TEST_IO_ADVISE_FLUSH 295
289 : #define OPT_TEST_IO_ADVISE_FLUSH_WINDOW 296
290 : #define OPT_TEST_IO_ADVISE_DISCARD 297
291 : #define OPT_TEST_IO_ADVISE_DISCARD_WINDOW 298
292 : #define OPT_TEST_IO_ADVISE_DIRECT 299
293 : #define OPT_TEST_PARITY_LIMIT 301
294 : #define OPT_TEST_SKIP_CONTENT_WRITE 302
295 : #define OPT_TEST_SKIP_SPACE_HOLDER 303
296 : #define OPT_TEST_FORMAT 304
297 :
298 : #if HAVE_GETOPT_LONG
299 : struct option long_options[] = {
300 : { "conf", 1, 0, 'c' },
301 : { "filter", 1, 0, 'f' },
302 : { "filter-disk", 1, 0, 'd' },
303 : { "filter-missing", 0, 0, 'm' },
304 : { "filter-error", 0, 0, 'e' },
305 : { "percentage", 1, 0, 'p' }, /* legacy name for --plan */
306 : { "plan", 1, 0, 'p' },
307 : { "older-than", 1, 0, 'o' },
308 : { "start", 1, 0, 'S' },
309 : { "count", 1, 0, 'B' },
310 : { "error-limit", 1, 0, 'L' },
311 : { "import", 1, 0, 'i' },
312 : { "log", 1, 0, 'l' },
313 : { "force-zero", 0, 0, 'Z' },
314 : { "force-empty", 0, 0, 'E' },
315 : { "force-uuid", 0, 0, 'U' },
316 : { "force-device", 0, 0, 'D' },
317 : { "force-nocopy", 0, 0, 'N' },
318 : { "force-full", 0, 0, 'F' },
319 : { "force-realloc", 0, 0, 'R' },
320 : { "audit-only", 0, 0, 'a' },
321 : { "pre-hash", 0, 0, 'h' },
322 : { "speed-test", 0, 0, 'T' }, /* undocumented speed test command */
323 : { "gen-conf", 1, 0, 'C' },
324 : { "verbose", 0, 0, 'v' },
325 : { "quiet", 0, 0, 'q' }, /* undocumented quiet option */
326 : { "gui", 0, 0, 'G' }, /* undocumented GUI interface option */
327 : { "help", 0, 0, 'H' },
328 : { "version", 0, 0, 'V' },
329 :
330 : /* The following are test specific options, DO NOT USE! */
331 :
332 : /* After syncing, do not write the new content file */
333 : { "test-kill-after-sync", 0, 0, OPT_TEST_KILL_AFTER_SYNC },
334 :
335 : /* Exit with failure if after check/fix there ARE NOT unrecoverable errors. */
336 : { "test-expect-unrecoverable", 0, 0, OPT_TEST_EXPECT_UNRECOVERABLE },
337 :
338 : /* Exit with failure if after check/fix there ARE NOT recoverable errors. */
339 : { "test-expect-recoverable", 0, 0, OPT_TEST_EXPECT_RECOVERABLE },
340 :
341 : /* Skip the initial self test */
342 : { "test-skip-self", 0, 0, OPT_TEST_SKIP_SELF },
343 :
344 : /* Skip the initial sign check when reading the content file */
345 : { "test-skip-sign", 0, 0, OPT_TEST_SKIP_SIGN },
346 :
347 : /* Skip the fallocate() when growing the parity files */
348 : { "test-skip-fallocate", 0, 0, OPT_TEST_SKIP_FALLOCATE },
349 :
350 : /* Skip the device check */
351 : { "test-skip-device", 0, 0, OPT_TEST_SKIP_DEVICE },
352 :
353 : /* Force Murmur3 hash */
354 : { "test-force-murmur3", 0, 0, OPT_TEST_FORCE_MURMUR3 },
355 :
356 : /* Force Spooky2 hash */
357 : { "test-force-spooky2", 0, 0, OPT_TEST_FORCE_SPOOKY2 },
358 :
359 : /* Skip the use of lock file */
360 : { "test-skip-lock", 0, 0, OPT_TEST_SKIP_LOCK },
361 :
362 : /* Force a sort order for files */
363 : { "test-force-order-physical", 0, 0, OPT_TEST_FORCE_ORDER_PHYSICAL },
364 : { "test-force-order-inode", 0, 0, OPT_TEST_FORCE_ORDER_INODE },
365 : { "test-force-order-alpha", 0, 0, OPT_TEST_FORCE_ORDER_ALPHA },
366 : { "test-force-order-dir", 0, 0, OPT_TEST_FORCE_ORDER_DIR },
367 :
368 : /* Force scrub of the specified number of blocks */
369 : { "test-force-scrub-at", 1, 0, OPT_TEST_FORCE_SCRUB_AT },
370 :
371 : /* Force scrub of all the even blocks. This is really for testing, don't try it */
372 : { "test-force-scrub-even", 0, 0, OPT_TEST_FORCE_SCRUB_EVEN },
373 :
374 : /* Force write of the content file even if no modification is done */
375 : { "test-force-content-write", 0, 0, OPT_TEST_FORCE_CONTENT_WRITE },
376 :
377 : /* Relax the checks done at the content file */
378 : { "test-skip-content-check", 0, 0, OPT_TEST_SKIP_CONTENT_CHECK },
379 :
380 : /* Skip the parity access */
381 : { "test-skip-parity-access", 0, 0, OPT_TEST_SKIP_PARITY_ACCESS },
382 :
383 : /* Exit generic failure */
384 : { "test-expect-failure", 0, 0, OPT_TEST_EXPECT_FAILURE },
385 :
386 : /* Exit generic need sync */
387 : { "test-expect-need-sync", 0, 0, OPT_TEST_EXPECT_NEED_SYNC },
388 :
389 : /* Run some command after loading the state and before the command */
390 : { "test-run", 1, 0, OPT_TEST_RUN },
391 :
392 : /* Use the FindFirst/Next approach in Windows to list files */
393 : { "test-force-scan-winfind", 0, 0, OPT_TEST_FORCE_SCAN_WINFIND },
394 :
395 : /* Alternative import working by data */
396 : { "test-import-content", 1, 0, OPT_TEST_IMPORT_CONTENT },
397 :
398 : /* Force immediate progress state update */
399 : { "test-force-progress", 0, 0, OPT_TEST_FORCE_PROGRESS },
400 :
401 : /* Skip the disk access */
402 : { "test-skip-disk-access", 0, 0, OPT_TEST_SKIP_DISK_ACCESS },
403 :
404 : /* Force autosave at the specified block */
405 : { "test-force-autosave-at", 1, 0, OPT_TEST_FORCE_AUTOSAVE_AT },
406 :
407 : /* Fake device data */
408 : { "test-fake-device", 0, 0, OPT_TEST_FAKE_DEVICE },
409 :
410 : /* Disable annoying warnings */
411 : { "no-warnings", 0, 0, OPT_NO_WARNINGS },
412 :
413 : /* Fake UUID */
414 : { "test-fake-uuid", 0, 0, OPT_TEST_FAKE_UUID },
415 :
416 : /* Match first UUID */
417 : { "test-match-first-uuid", 0, 0, OPT_TEST_MATCH_FIRST_UUID },
418 :
419 : /* Force parity update even if all the data hash is already matching */
420 : { "test-force-parity-update", 0, 0, OPT_TEST_FORCE_PARITY_UPDATE },
421 :
422 : /* Number of IO buffers */
423 : { "test-io-cache", 1, 0, OPT_TEST_IO_CACHE },
424 :
425 : /* Print IO stats */
426 : { "test-io-stats", 0, 0, OPT_TEST_IO_STATS },
427 :
428 : /* Signal condition variable outside the mutex */
429 : { "test-cond-signal-outside", 0, 0, OPT_TEST_COND_SIGNAL_OUTSIDE },
430 :
431 : /* Set the io advise to none */
432 : { "test-io-advise-none", 0, 0, OPT_TEST_IO_ADVISE_NONE },
433 :
434 : /* Set the io advise to sequential */
435 : { "test-io-advise-sequential", 0, 0, OPT_TEST_IO_ADVISE_SEQUENTIAL },
436 :
437 : /* Set the io advise to flush */
438 : { "test-io-advise-flush", 0, 0, OPT_TEST_IO_ADVISE_FLUSH },
439 :
440 : /* Set the io advise to flush window */
441 : { "test-io-advise-flush-window", 0, 0, OPT_TEST_IO_ADVISE_FLUSH_WINDOW },
442 :
443 : /* Set the io advise to discard */
444 : { "test-io-advise-discard", 0, 0, OPT_TEST_IO_ADVISE_DISCARD },
445 :
446 : /* Set the io advise to discard window */
447 : { "test-io-advise-discard-window", 0, 0, OPT_TEST_IO_ADVISE_DISCARD_WINDOW },
448 :
449 : /* Set the io advise to direct */
450 : { "test-io-advise-direct", 0, 0, OPT_TEST_IO_ADVISE_DIRECT },
451 :
452 : /* Set an artificial parity limit */
453 : { "test-parity-limit", 1, 0, OPT_TEST_PARITY_LIMIT },
454 :
455 : /* Skip content write */
456 : { "test-skip-content-write", 0, 0, OPT_TEST_SKIP_CONTENT_WRITE },
457 :
458 : /* Skip space holder file in parity disks */
459 : { "test-skip-space-holder", 0, 0, OPT_TEST_SKIP_SPACE_HOLDER },
460 :
461 : /* Set the ourput format */
462 : { "test-fmt", 1, 0, OPT_TEST_FORMAT },
463 :
464 : { 0, 0, 0, 0 }
465 : };
466 : #endif
467 :
468 : #define OPTIONS "c:f:d:mep:o:S:B:L:i:l:ZEUDNFRahTC:vqHVG"
469 :
470 : volatile int global_interrupt = 0;
471 :
472 : /* LCOV_EXCL_START */
473 : void signal_handler(int signum)
474 : {
475 : (void)signum;
476 :
477 : /* report the request of interruption */
478 : global_interrupt = 1;
479 : }
480 : /* LCOV_EXCL_STOP */
481 :
482 223 : void signal_init(void)
483 : {
484 : #if HAVE_SIGACTION
485 : struct sigaction sa;
486 :
487 223 : sa.sa_handler = signal_handler;
488 223 : sigemptyset(&sa.sa_mask);
489 :
490 : /* use the SA_RESTART to automatically restart interrupted system calls */
491 223 : sa.sa_flags = SA_RESTART;
492 :
493 223 : sigaction(SIGHUP, &sa, 0);
494 223 : sigaction(SIGTERM, &sa, 0);
495 223 : sigaction(SIGINT, &sa, 0);
496 223 : sigaction(SIGQUIT, &sa, 0);
497 : #else
498 : signal(SIGINT, signal_handler);
499 : #endif
500 223 : }
501 :
502 : #define OPERATION_DIFF 0
503 : #define OPERATION_SYNC 1
504 : #define OPERATION_CHECK 2
505 : #define OPERATION_FIX 3
506 : #define OPERATION_DRY 4
507 : #define OPERATION_DUP 5
508 : #define OPERATION_LIST 6
509 : #define OPERATION_POOL 7
510 : #define OPERATION_REHASH 8
511 : #define OPERATION_SCRUB 9
512 : #define OPERATION_STATUS 10
513 : #define OPERATION_REWRITE 11
514 : #define OPERATION_READ 12
515 : #define OPERATION_TOUCH 13
516 : #define OPERATION_SPINUP 14
517 : #define OPERATION_SPINDOWN 15
518 : #define OPERATION_DEVICES 16
519 : #define OPERATION_SMART 17
520 :
521 276 : int main(int argc, char* argv[])
522 : {
523 : int c;
524 : struct snapraid_option opt;
525 : char conf[PATH_MAX];
526 : struct snapraid_state state;
527 : int operation;
528 : block_off_t blockstart;
529 : block_off_t blockcount;
530 : int ret;
531 : tommy_list filterlist_file;
532 : tommy_list filterlist_disk;
533 : int filter_missing;
534 : int filter_error;
535 : int plan;
536 : int olderthan;
537 : char* e;
538 : const char* command;
539 : const char* import_timestamp;
540 : const char* import_content;
541 : const char* log_file;
542 : int lock;
543 : const char* gen_conf;
544 : const char* run;
545 : int speedtest;
546 : int period;
547 : time_t t;
548 : struct tm* tm;
549 : int i;
550 :
551 276 : test(argc, argv);
552 :
553 275 : lock_init();
554 :
555 : /* defaults */
556 275 : config(conf, sizeof(conf), argv[0]);
557 275 : memset(&opt, 0, sizeof(opt));
558 275 : opt.io_error_limit = 100;
559 275 : blockstart = 0;
560 275 : blockcount = 0;
561 275 : tommy_list_init(&filterlist_file);
562 275 : tommy_list_init(&filterlist_disk);
563 275 : period = 1000;
564 275 : filter_missing = 0;
565 275 : filter_error = 0;
566 275 : plan = SCRUB_AUTO;
567 275 : olderthan = SCRUB_AUTO;
568 275 : import_timestamp = 0;
569 275 : import_content = 0;
570 275 : log_file = 0;
571 275 : lock = 0;
572 275 : gen_conf = 0;
573 275 : speedtest = 0;
574 275 : run = 0;
575 :
576 275 : opterr = 0;
577 3463 : while ((c =
578 : #if HAVE_GETOPT_LONG
579 : getopt_long(argc, argv, OPTIONS, long_options, 0))
580 : #else
581 : getopt(argc, argv, OPTIONS))
582 : #endif
583 : != EOF) {
584 2918 : switch (c) {
585 : case 'c' :
586 268 : pathimport(conf, sizeof(conf), optarg);
587 268 : break;
588 : case 'f' : {
589 6 : struct snapraid_filter* filter = filter_alloc_file(1, optarg);
590 6 : if (!filter) {
591 : /* LCOV_EXCL_START */
592 : log_fatal("Invalid filter specification '%s'\n", optarg);
593 : log_fatal("Filters using relative paths are not supported. Ensure to add an initial slash\n");
594 : exit(EXIT_FAILURE);
595 : /* LCOV_EXCL_STOP */
596 : }
597 3 : tommy_list_insert_tail(&filterlist_file, &filter->node, filter);
598 3 : } break;
599 : case 'd' : {
600 10 : struct snapraid_filter* filter = filter_alloc_disk(1, optarg);
601 10 : if (!filter) {
602 : /* LCOV_EXCL_START */
603 : log_fatal("Invalid filter specification '%s'\n", optarg);
604 : exit(EXIT_FAILURE);
605 : /* LCOV_EXCL_STOP */
606 : }
607 10 : tommy_list_insert_tail(&filterlist_disk, &filter->node, filter);
608 10 : } break;
609 : case 'm' :
610 2 : filter_missing = 1;
611 2 : opt.expected_missing = 1;
612 2 : break;
613 : case 'e' :
614 : /* when processing only error, we filter both files and blocks */
615 : /* and we apply fixes only to synced ones */
616 2 : filter_error = 1;
617 2 : opt.badonly = 1;
618 2 : opt.syncedonly = 1;
619 2 : break;
620 : case 'p' :
621 5 : if (strcmp(optarg, "bad") == 0) {
622 1 : plan = SCRUB_BAD;
623 4 : } else if (strcmp(optarg, "new") == 0) {
624 1 : plan = SCRUB_NEW;
625 3 : } else if (strcmp(optarg, "full") == 0) {
626 2 : plan = SCRUB_FULL;
627 : } else {
628 1 : plan = strtoul(optarg, &e, 10);
629 1 : if (!e || *e || plan > 100) {
630 : /* LCOV_EXCL_START */
631 : log_fatal("Invalid plan/percentage '%s'\n", optarg);
632 : exit(EXIT_FAILURE);
633 : /* LCOV_EXCL_STOP */
634 : }
635 : }
636 5 : break;
637 : case 'o' :
638 1 : olderthan = strtoul(optarg, &e, 10);
639 1 : if (!e || *e || olderthan > 1000) {
640 : /* LCOV_EXCL_START */
641 : log_fatal("Invalid number of days '%s'\n", optarg);
642 : exit(EXIT_FAILURE);
643 : /* LCOV_EXCL_STOP */
644 : }
645 1 : break;
646 : case 'S' :
647 2 : blockstart = strtoul(optarg, &e, 0);
648 2 : if (!e || *e) {
649 : /* LCOV_EXCL_START */
650 : log_fatal("Invalid start position '%s'\n", optarg);
651 : exit(EXIT_FAILURE);
652 : /* LCOV_EXCL_STOP */
653 : }
654 2 : break;
655 : case 'B' :
656 7 : blockcount = strtoul(optarg, &e, 0);
657 7 : if (!e || *e) {
658 : /* LCOV_EXCL_START */
659 : log_fatal("Invalid count number '%s'\n", optarg);
660 : exit(EXIT_FAILURE);
661 : /* LCOV_EXCL_STOP */
662 : }
663 7 : break;
664 : case 'L' :
665 0 : opt.io_error_limit = strtoul(optarg, &e, 0);
666 0 : if (!e || *e) {
667 : /* LCOV_EXCL_START */
668 : log_fatal("Invalid error limit number '%s'\n", optarg);
669 : exit(EXIT_FAILURE);
670 : /* LCOV_EXCL_STOP */
671 : }
672 0 : break;
673 : case 'i' :
674 1 : if (import_timestamp) {
675 : /* LCOV_EXCL_START */
676 : log_fatal("Import directory '%s' already specified as '%s'\n", optarg, import_timestamp);
677 : exit(EXIT_FAILURE);
678 : /* LCOV_EXCL_STOP */
679 : }
680 1 : import_timestamp = optarg;
681 1 : break;
682 : case OPT_TEST_IMPORT_CONTENT :
683 1 : if (import_content) {
684 : /* LCOV_EXCL_START */
685 : log_fatal("Import directory '%s' already specified as '%s'\n", optarg, import_content);
686 : exit(EXIT_FAILURE);
687 : /* LCOV_EXCL_STOP */
688 : }
689 1 : import_content = optarg;
690 1 : break;
691 : case 'l' :
692 80 : if (log_file) {
693 : /* LCOV_EXCL_START */
694 : log_fatal("Log file '%s' already specified as '%s'\n", optarg, log_file);
695 : exit(EXIT_FAILURE);
696 : /* LCOV_EXCL_STOP */
697 : }
698 80 : log_file = optarg;
699 80 : break;
700 : case 'Z' :
701 0 : opt.force_zero = 1;
702 0 : break;
703 : case 'E' :
704 6 : opt.force_empty = 1;
705 6 : break;
706 : case 'U' :
707 0 : opt.force_uuid = 1;
708 0 : break;
709 : case 'D' :
710 1 : opt.force_device = 1;
711 1 : break;
712 : case 'N' :
713 1 : opt.force_nocopy = 1;
714 1 : break;
715 : case 'F' :
716 6 : opt.force_full = 1;
717 6 : break;
718 : case 'R' :
719 1 : opt.force_realloc = 1;
720 1 : break;
721 : case 'a' :
722 6 : opt.auditonly = 1;
723 6 : break;
724 : case 'h' :
725 7 : opt.prehash = 1;
726 7 : break;
727 : case 'v' :
728 26 : ++msg_level;
729 26 : break;
730 : case 'q' :
731 726 : --msg_level;
732 726 : break;
733 : case 'G' :
734 26 : opt.gui = 1;
735 26 : break;
736 : case 'H' :
737 1 : usage();
738 1 : exit(EXIT_SUCCESS);
739 : case 'V' :
740 1 : version();
741 1 : exit(EXIT_SUCCESS);
742 : case 'T' :
743 4 : speedtest = 1;
744 4 : break;
745 : case 'C' :
746 1 : gen_conf = optarg;
747 1 : break;
748 : case OPT_TEST_KILL_AFTER_SYNC :
749 10 : opt.kill_after_sync = 1;
750 10 : break;
751 : case OPT_TEST_EXPECT_UNRECOVERABLE :
752 13 : opt.expect_unrecoverable = 1;
753 13 : break;
754 : case OPT_TEST_EXPECT_RECOVERABLE :
755 23 : opt.expect_recoverable = 1;
756 23 : break;
757 : case OPT_TEST_SKIP_SELF :
758 268 : opt.skip_self = 1;
759 268 : break;
760 : case OPT_TEST_SKIP_SIGN :
761 0 : opt.skip_sign = 1;
762 0 : break;
763 : case OPT_TEST_SKIP_FALLOCATE :
764 1 : opt.skip_fallocate = 1;
765 1 : break;
766 : case OPT_TEST_SKIP_DEVICE :
767 275 : opt.skip_device = 1;
768 275 : period = 50; /* reduce period of the speed test */
769 275 : break;
770 : case OPT_TEST_SKIP_CONTENT_CHECK :
771 0 : opt.skip_content_check = 1;
772 0 : break;
773 : case OPT_TEST_SKIP_PARITY_ACCESS :
774 0 : opt.skip_parity_access = 1;
775 0 : break;
776 : case OPT_TEST_SKIP_DISK_ACCESS :
777 0 : opt.skip_disk_access = 1;
778 0 : break;
779 : case OPT_TEST_FORCE_MURMUR3 :
780 1 : opt.force_murmur3 = 1;
781 1 : break;
782 : case OPT_TEST_FORCE_SPOOKY2 :
783 1 : opt.force_spooky2 = 1;
784 1 : break;
785 : case OPT_TEST_SKIP_LOCK :
786 0 : opt.skip_lock = 1;
787 0 : break;
788 : case OPT_TEST_FORCE_ORDER_PHYSICAL :
789 2 : opt.force_order = SORT_PHYSICAL;
790 2 : break;
791 : case OPT_TEST_FORCE_ORDER_INODE :
792 0 : opt.force_order = SORT_INODE;
793 0 : break;
794 : case OPT_TEST_FORCE_ORDER_ALPHA :
795 266 : opt.force_order = SORT_ALPHA;
796 266 : break;
797 : case OPT_TEST_FORCE_ORDER_DIR :
798 0 : opt.force_order = SORT_DIR;
799 0 : break;
800 : case OPT_TEST_FORCE_SCRUB_AT :
801 4 : opt.force_scrub_at = atoi(optarg);
802 4 : break;
803 : case OPT_TEST_FORCE_SCRUB_EVEN :
804 1 : opt.force_scrub_even = 1;
805 1 : break;
806 : case OPT_TEST_FORCE_CONTENT_WRITE :
807 0 : opt.force_content_write = 1;
808 0 : break;
809 : case OPT_TEST_EXPECT_FAILURE :
810 : /* invert the exit codes */
811 19 : exit_success = 1;
812 19 : exit_failure = 0;
813 19 : break;
814 : case OPT_TEST_EXPECT_NEED_SYNC :
815 : /* invert the exit codes */
816 7 : exit_success = 1;
817 7 : exit_sync_needed = 0;
818 7 : break;
819 : case OPT_TEST_RUN :
820 10 : run = optarg;
821 10 : break;
822 : case OPT_TEST_FORCE_SCAN_WINFIND :
823 0 : opt.force_scan_winfind = 1;
824 0 : break;
825 : case OPT_TEST_FORCE_PROGRESS :
826 268 : opt.force_progress = 1;
827 268 : break;
828 : case OPT_TEST_FORCE_AUTOSAVE_AT :
829 1 : opt.force_autosave_at = atoi(optarg);
830 1 : break;
831 : case OPT_TEST_FAKE_DEVICE :
832 1 : opt.fake_device = 1;
833 1 : break;
834 : case OPT_NO_WARNINGS :
835 269 : opt.no_warnings = 1;
836 269 : break;
837 : case OPT_TEST_FAKE_UUID :
838 1 : opt.fake_uuid = 2;
839 1 : break;
840 : case OPT_TEST_MATCH_FIRST_UUID :
841 2 : opt.match_first_uuid = 1;
842 2 : break;
843 : case OPT_TEST_FORCE_PARITY_UPDATE :
844 0 : opt.force_parity_update = 1;
845 0 : break;
846 : case OPT_TEST_IO_CACHE :
847 1 : opt.io_cache = atoi(optarg);
848 1 : if (opt.io_cache != 1 && (opt.io_cache < IO_MIN || opt.io_cache > IO_MAX)) {
849 : /* LCOV_EXCL_START */
850 : log_fatal("The IO cache should be between %u and %u.\n", IO_MIN, IO_MAX);
851 : exit(EXIT_FAILURE);
852 : /* LCOV_EXCL_STOP */
853 : }
854 1 : break;
855 : case OPT_TEST_IO_STATS :
856 1 : opt.force_stats = 1;
857 1 : break;
858 : case OPT_TEST_COND_SIGNAL_OUTSIDE :
859 : #if HAVE_PTHREAD
860 0 : thread_cond_signal_outside = 1;
861 : #endif
862 0 : break;
863 : case OPT_TEST_IO_ADVISE_NONE :
864 1 : opt.file_mode = ADVISE_NONE;
865 1 : break;
866 : case OPT_TEST_IO_ADVISE_SEQUENTIAL :
867 1 : opt.file_mode = ADVISE_SEQUENTIAL;
868 1 : break;
869 : case OPT_TEST_IO_ADVISE_FLUSH :
870 0 : opt.file_mode = ADVISE_FLUSH;
871 0 : break;
872 : case OPT_TEST_IO_ADVISE_FLUSH_WINDOW :
873 1 : opt.file_mode = ADVISE_FLUSH_WINDOW;
874 1 : break;
875 : case OPT_TEST_IO_ADVISE_DISCARD :
876 0 : opt.file_mode = ADVISE_DISCARD;
877 0 : break;
878 : case OPT_TEST_IO_ADVISE_DISCARD_WINDOW :
879 1 : opt.file_mode = ADVISE_DISCARD_WINDOW;
880 1 : break;
881 : case OPT_TEST_IO_ADVISE_DIRECT :
882 0 : opt.file_mode = ADVISE_DIRECT;
883 0 : break;
884 : case OPT_TEST_PARITY_LIMIT :
885 268 : opt.parity_limit_size = atoll(optarg);
886 268 : break;
887 : case OPT_TEST_SKIP_CONTENT_WRITE :
888 0 : opt.skip_content_write = 1;
889 0 : break;
890 : case OPT_TEST_SKIP_SPACE_HOLDER :
891 0 : opt.skip_space_holder = 1;
892 0 : break;
893 : case OPT_TEST_FORMAT :
894 3 : if (strcmp(optarg, "file") == 0)
895 1 : FMT_MODE = FMT_FILE;
896 2 : else if (strcmp(optarg, "disk") == 0)
897 1 : FMT_MODE = FMT_DISK;
898 1 : else if (strcmp(optarg, "path") == 0)
899 1 : FMT_MODE = FMT_PATH;
900 : else {
901 : /* LCOV_EXCL_START */
902 : log_fatal("Unknown format '%s'\n", optarg);
903 : exit(EXIT_FAILURE);
904 : /* LCOV_EXCL_STOP */
905 : }
906 3 : break;
907 : default :
908 : /* LCOV_EXCL_START */
909 : log_fatal("Unknown option '%c'\n", (char)c);
910 : exit(EXIT_FAILURE);
911 : /* LCOV_EXCL_STOP */
912 : }
913 : }
914 :
915 270 : os_init(opt.force_scan_winfind);
916 270 : raid_init();
917 270 : crc32c_init();
918 :
919 270 : if (speedtest != 0) {
920 4 : speed(period);
921 4 : os_done();
922 4 : exit(EXIT_SUCCESS);
923 : }
924 :
925 266 : if (gen_conf != 0) {
926 1 : generate_configuration(gen_conf);
927 1 : os_done();
928 1 : exit(EXIT_SUCCESS);
929 : }
930 :
931 265 : if (optind + 1 != argc) {
932 : /* LCOV_EXCL_START */
933 : usage();
934 : exit(EXIT_FAILURE);
935 : /* LCOV_EXCL_STOP */
936 : }
937 :
938 265 : command = argv[optind];
939 265 : if (strcmp(command, "diff") == 0) {
940 8 : operation = OPERATION_DIFF;
941 257 : } else if (strcmp(argv[optind], "sync") == 0) {
942 89 : operation = OPERATION_SYNC;
943 168 : } else if (strcmp(argv[optind], "check") == 0) {
944 75 : operation = OPERATION_CHECK;
945 93 : } else if (strcmp(argv[optind], "fix") == 0) {
946 46 : operation = OPERATION_FIX;
947 47 : } else if (strcmp(argv[optind], "test-dry") == 0) {
948 2 : operation = OPERATION_DRY;
949 45 : } else if (strcmp(argv[optind], "dup") == 0) {
950 3 : operation = OPERATION_DUP;
951 42 : } else if (strcmp(argv[optind], "list") == 0) {
952 5 : operation = OPERATION_LIST;
953 37 : } else if (strcmp(argv[optind], "pool") == 0) {
954 3 : operation = OPERATION_POOL;
955 34 : } else if (strcmp(argv[optind], "rehash") == 0) {
956 1 : operation = OPERATION_REHASH;
957 33 : } else if (strcmp(argv[optind], "scrub") == 0) {
958 11 : operation = OPERATION_SCRUB;
959 22 : } else if (strcmp(argv[optind], "status") == 0) {
960 14 : operation = OPERATION_STATUS;
961 8 : } else if (strcmp(argv[optind], "test-rewrite") == 0) {
962 1 : operation = OPERATION_REWRITE;
963 7 : } else if (strcmp(argv[optind], "test-read") == 0) {
964 1 : operation = OPERATION_READ;
965 6 : } else if (strcmp(argv[optind], "touch") == 0) {
966 1 : operation = OPERATION_TOUCH;
967 5 : } else if (strcmp(argv[optind], "up") == 0) {
968 1 : operation = OPERATION_SPINUP;
969 4 : } else if (strcmp(argv[optind], "down") == 0) {
970 1 : operation = OPERATION_SPINDOWN;
971 3 : } else if (strcmp(argv[optind], "devices") == 0) {
972 1 : operation = OPERATION_DEVICES;
973 2 : } else if (strcmp(argv[optind], "smart") == 0) {
974 2 : operation = OPERATION_SMART;
975 : } else {
976 : /* LCOV_EXCL_START */
977 : log_fatal("Unknown command '%s'\n", argv[optind]);
978 : exit(EXIT_FAILURE);
979 : /* LCOV_EXCL_STOP */
980 : }
981 :
982 : /* check options compatibility */
983 265 : switch (operation) {
984 : case OPERATION_CHECK :
985 75 : break;
986 : default :
987 190 : if (opt.auditonly) {
988 : /* LCOV_EXCL_START */
989 : log_fatal("You cannot use -a, --audit-only with the '%s' command\n", command);
990 : exit(EXIT_FAILURE);
991 : /* LCOV_EXCL_STOP */
992 : }
993 : }
994 :
995 265 : switch (operation) {
996 : case OPERATION_FIX :
997 : case OPERATION_CHECK :
998 121 : break;
999 : default :
1000 144 : if (opt.force_device) {
1001 : /* LCOV_EXCL_START */
1002 : log_fatal("You cannot use -D, --force-device with the '%s' command\n", command);
1003 : exit(EXIT_FAILURE);
1004 : /* LCOV_EXCL_STOP */
1005 : }
1006 : }
1007 :
1008 265 : switch (operation) {
1009 : case OPERATION_SYNC :
1010 : case OPERATION_CHECK :
1011 : case OPERATION_FIX :
1012 210 : break;
1013 : default :
1014 55 : if (opt.force_nocopy) {
1015 : /* LCOV_EXCL_START */
1016 : log_fatal("You cannot use -N, --force-nocopy with the '%s' command\n", command);
1017 : exit(EXIT_FAILURE);
1018 : /* LCOV_EXCL_STOP */
1019 : }
1020 : }
1021 :
1022 265 : switch (operation) {
1023 : case OPERATION_SYNC :
1024 89 : break;
1025 : default :
1026 176 : if (opt.prehash) {
1027 : /* LCOV_EXCL_START */
1028 : log_fatal("You cannot use -h, --pre-hash with the '%s' command\n", command);
1029 : exit(EXIT_FAILURE);
1030 : /* LCOV_EXCL_STOP */
1031 : }
1032 :
1033 176 : if (opt.force_full) {
1034 : /* LCOV_EXCL_START */
1035 : log_fatal("You cannot use -F, --force-full with the '%s' command\n", command);
1036 : exit(EXIT_FAILURE);
1037 : /* LCOV_EXCL_STOP */
1038 : }
1039 :
1040 176 : if (opt.force_realloc) {
1041 : /* LCOV_EXCL_START */
1042 : log_fatal("You cannot use -R, --force-realloc with the '%s' command\n", command);
1043 : exit(EXIT_FAILURE);
1044 : /* LCOV_EXCL_STOP */
1045 : }
1046 : }
1047 :
1048 265 : if (opt.force_full && opt.force_nocopy) {
1049 : /* LCOV_EXCL_START */
1050 : log_fatal("You cannot use the -F, --force-full and -N, --force-nocopy options at the same time\n");
1051 : exit(EXIT_FAILURE);
1052 : /* LCOV_EXCL_STOP */
1053 : }
1054 :
1055 265 : if (opt.force_realloc && opt.force_nocopy) {
1056 : /* LCOV_EXCL_START */
1057 : log_fatal("You cannot use the -R, --force-realloc and -N, --force-nocopy options at the same time\n");
1058 : exit(EXIT_FAILURE);
1059 : /* LCOV_EXCL_STOP */
1060 : }
1061 :
1062 265 : if (opt.force_realloc && opt.force_full) {
1063 : /* LCOV_EXCL_START */
1064 : log_fatal("You cannot use the -R, --force-realloc and -F, --force-full options at the same time\n");
1065 : exit(EXIT_FAILURE);
1066 : /* LCOV_EXCL_STOP */
1067 : }
1068 :
1069 265 : if (opt.prehash && opt.force_nocopy) {
1070 : /* LCOV_EXCL_START */
1071 : log_fatal("You cannot use the -h, --pre-hash and -N, --force-nocopy options at the same time\n");
1072 : exit(EXIT_FAILURE);
1073 : /* LCOV_EXCL_STOP */
1074 : }
1075 :
1076 265 : switch (operation) {
1077 : case OPERATION_CHECK :
1078 : case OPERATION_FIX :
1079 : case OPERATION_DRY :
1080 123 : break;
1081 : default :
1082 140 : if (!tommy_list_empty(&filterlist_disk)) {
1083 : /* LCOV_EXCL_START */
1084 : log_fatal("You cannot use -d, --filter-disk with the '%s' command\n", command);
1085 : exit(EXIT_FAILURE);
1086 : /* LCOV_EXCL_STOP */
1087 : }
1088 : /* follow */
1089 : case OPERATION_SPINUP :
1090 : case OPERATION_SPINDOWN :
1091 142 : if (!tommy_list_empty(&filterlist_file)) {
1092 : /* LCOV_EXCL_START */
1093 : log_fatal("You cannot use -f, --filter with the '%s' command\n", command);
1094 : exit(EXIT_FAILURE);
1095 : /* LCOV_EXCL_STOP */
1096 : }
1097 142 : if (filter_missing != 0) {
1098 : /* LCOV_EXCL_START */
1099 : log_fatal("You cannot use -m, --filter-missing with the '%s' command\n", command);
1100 : exit(EXIT_FAILURE);
1101 : /* LCOV_EXCL_STOP */
1102 : }
1103 142 : if (filter_error != 0) {
1104 : /* LCOV_EXCL_START */
1105 : log_fatal("You cannot use -e, --filter-error with the '%s' command\n", command);
1106 : exit(EXIT_FAILURE);
1107 : /* LCOV_EXCL_STOP */
1108 : }
1109 : }
1110 :
1111 : /* errors must be always fixed on all disks */
1112 : /* because we don't keep the information on what disk is the error */
1113 265 : if (filter_error != 0 && !tommy_list_empty(&filterlist_disk)) {
1114 : /* LCOV_EXCL_START */
1115 : log_fatal("You cannot use -e, --filter-error and -d, --filter-disk at the same time\n");
1116 : exit(EXIT_FAILURE);
1117 : /* LCOV_EXCL_STOP */
1118 : }
1119 :
1120 265 : switch (operation) {
1121 : case OPERATION_CHECK :
1122 : case OPERATION_FIX :
1123 121 : break;
1124 : default :
1125 144 : if (import_timestamp != 0 || import_content != 0) {
1126 : /* LCOV_EXCL_START */
1127 : log_fatal("You cannot import with the '%s' command\n", command);
1128 : exit(EXIT_FAILURE);
1129 : /* LCOV_EXCL_STOP */
1130 : }
1131 : }
1132 :
1133 265 : switch (operation) {
1134 : case OPERATION_LIST :
1135 : case OPERATION_DUP :
1136 : case OPERATION_STATUS :
1137 : case OPERATION_REWRITE :
1138 : case OPERATION_READ :
1139 : case OPERATION_REHASH :
1140 : case OPERATION_SPINUP : /* we want to do it in different threads to avoid blocking */
1141 : /* avoid to check and access data disks if not needed */
1142 26 : opt.skip_disk_access = 1;
1143 26 : break;
1144 : }
1145 :
1146 265 : switch (operation) {
1147 : case OPERATION_DIFF :
1148 : case OPERATION_LIST :
1149 : case OPERATION_DUP :
1150 : case OPERATION_POOL :
1151 : case OPERATION_STATUS :
1152 : case OPERATION_REWRITE :
1153 : case OPERATION_READ :
1154 : case OPERATION_REHASH :
1155 : case OPERATION_TOUCH :
1156 : case OPERATION_SPINUP : /* we want to do it in different threads to avoid blocking */
1157 : /* avoid to check and access parity disks if not needed */
1158 38 : opt.skip_parity_access = 1;
1159 38 : break;
1160 : }
1161 :
1162 265 : switch (operation) {
1163 : case OPERATION_FIX :
1164 : case OPERATION_CHECK :
1165 : /* avoid to stop processing if a content file is not accessible */
1166 121 : opt.skip_content_access = 1;
1167 121 : break;
1168 : }
1169 :
1170 265 : switch (operation) {
1171 : case OPERATION_DIFF :
1172 : case OPERATION_LIST :
1173 : case OPERATION_DUP :
1174 : case OPERATION_POOL :
1175 : case OPERATION_TOUCH :
1176 : case OPERATION_SPINUP :
1177 : case OPERATION_SPINDOWN :
1178 : case OPERATION_DEVICES :
1179 : case OPERATION_SMART :
1180 25 : opt.skip_self = 1;
1181 25 : break;
1182 : }
1183 :
1184 265 : switch (operation) {
1185 : #if HAVE_DIRECT_IO
1186 : case OPERATION_SYNC :
1187 : case OPERATION_SCRUB :
1188 : case OPERATION_DRY :
1189 102 : break;
1190 : #endif
1191 : default:
1192 : /* we allow direct IO only on some commands */
1193 163 : if (opt.file_mode == ADVISE_DIRECT)
1194 0 : opt.file_mode = ADVISE_SEQUENTIAL;
1195 163 : break;
1196 : }
1197 :
1198 265 : switch (operation) {
1199 : case OPERATION_DEVICES :
1200 : case OPERATION_SMART :
1201 : /* we may need to use these commands during operations */
1202 3 : opt.skip_lock = 1;
1203 3 : break;
1204 : }
1205 :
1206 265 : switch (operation) {
1207 : case OPERATION_SMART :
1208 : /* allow to run without configuration file */
1209 2 : opt.auto_conf = 1;
1210 2 : break;
1211 : }
1212 :
1213 : /* open the log file */
1214 265 : log_open(log_file);
1215 :
1216 : /* print generic info into the log */
1217 265 : t = time(0);
1218 265 : tm = localtime(&t);
1219 265 : log_tag("version:%s\n", PACKAGE_VERSION);
1220 265 : log_tag("unixtime:%" PRIi64 "\n", (int64_t)t);
1221 265 : if (tm) {
1222 : char datetime[64];
1223 265 : strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", tm);
1224 265 : log_tag("time:%s\n", datetime);
1225 : }
1226 265 : log_tag("command:%s\n", command);
1227 4052 : for (i = 0; i < argc; ++i)
1228 3787 : log_tag("argv:%u:%s\n", i, argv[i]);
1229 265 : log_flush();
1230 :
1231 265 : if (!opt.skip_self)
1232 1 : selftest();
1233 :
1234 265 : state_init(&state);
1235 :
1236 : /* read the configuration file */
1237 265 : state_config(&state, conf, command, &opt, &filterlist_disk);
1238 :
1239 : /* set the raid mode */
1240 264 : raid_mode(state.raid_mode);
1241 :
1242 : #if HAVE_LOCKFILE
1243 : /* create the lock file */
1244 264 : if (!opt.skip_lock && state.lockfile[0]) {
1245 261 : lock = lock_lock(state.lockfile);
1246 261 : if (lock == -1) {
1247 : /* LCOV_EXCL_START */
1248 : if (errno != EWOULDBLOCK) {
1249 : log_fatal("Error creating the lock file '%s'. %s.\n", state.lockfile, strerror(errno));
1250 : } else {
1251 : log_fatal("The lock file '%s' is already locked!\n", state.lockfile);
1252 : log_fatal("SnapRAID is already in use!\n");
1253 : }
1254 : exit(EXIT_FAILURE);
1255 : /* LCOV_EXCL_STOP */
1256 : }
1257 : }
1258 : #else
1259 : (void)lock;
1260 : #endif
1261 :
1262 264 : if (operation == OPERATION_DIFF) {
1263 8 : state_read(&state);
1264 :
1265 8 : ret = state_diff(&state);
1266 :
1267 : /* abort if sync needed */
1268 8 : if (ret > 0)
1269 7 : exit(EXIT_SYNC_NEEDED);
1270 256 : } else if (operation == OPERATION_SYNC) {
1271 :
1272 : /* in the next state read ensures to clear all the past hashes in case */
1273 : /* we are reading from an incomplete sync */
1274 : /* The indeterminate hash are only for CHG/DELETED blocks for which we don't */
1275 : /* know if the previous interrupted sync was able to update or not the parity. */
1276 : /* The sync process instead needs to trust this information because it's used */
1277 : /* to avoid to recompute the parity if all the input are equals as before. */
1278 :
1279 : /* In these cases we don't know if the old state is still the one */
1280 : /* stored inside the parity, because after an aborted sync, the parity */
1281 : /* may be or may be not have been updated with the data that may be now */
1282 : /* deleted. Then we reset the hash to a bogus value. */
1283 :
1284 : /* An example for CHG blocks is: */
1285 : /* - One file is added creating a CHG block with ZERO state */
1286 : /* - Sync aborted after updating the parity to the new state, */
1287 : /* but without saving the content file representing this new BLK state. */
1288 : /* - File is now deleted after the aborted sync */
1289 : /* - Sync again, deleting the blocks over the CHG ones */
1290 : /* with the hash of CHG blocks not representing the real parity state */
1291 :
1292 : /* An example for DELETED blocks is: */
1293 : /* - One file is deleted creating DELETED blocks */
1294 : /* - Sync aborted after, updating the parity to the new state, */
1295 : /* but without saving the content file representing this new EMPTY state. */
1296 : /* - Another file is added again over the DELETE ones */
1297 : /* with the hash of DELETED blocks not representing the real parity state */
1298 89 : state.clear_past_hash = 1;
1299 :
1300 89 : state_read(&state);
1301 :
1302 88 : state_scan(&state);
1303 :
1304 : /* refresh the size info before the content write */
1305 87 : state_refresh(&state);
1306 :
1307 87 : memory();
1308 :
1309 : /* intercept signals while operating */
1310 87 : signal_init();
1311 :
1312 : /* run a test command if required */
1313 87 : if (run != 0) {
1314 10 : ret = system(run); /* ignore error */
1315 10 : if (ret != 0) {
1316 : /* LCOV_EXCL_START */
1317 : log_fatal("Error in running command '%s'.\n", run);
1318 : exit(EXIT_FAILURE);
1319 : /* LCOV_EXCL_STOP */
1320 : }
1321 : }
1322 :
1323 : /* waits some time to ensure that any concurrent modification done at the files, */
1324 : /* using the same mtime read by the scan process, will be read by sync. */
1325 : /* Note that any later modification done, potentially not read by sync, will have */
1326 : /* a different mtime, and it will be synchronized at the next sync. */
1327 : /* The worst case is the FAT file-system with a two seconds resolution for mtime. */
1328 : /* If you don't use FAT, the wait is not needed, because most file-systems have now */
1329 : /* at least microseconds resolution, but better to be safe. */
1330 87 : if (!opt.skip_self)
1331 0 : sleep(2);
1332 :
1333 87 : ret = state_sync(&state, blockstart, blockcount);
1334 :
1335 : /* save the new state if required */
1336 87 : if (!opt.kill_after_sync) {
1337 77 : if ((state.need_write || state.opt.force_content_write))
1338 58 : state_write(&state);
1339 : } else {
1340 10 : log_fatal("WARNING! Skipped state write for --test-kill-after-sync option.\n");
1341 : }
1342 :
1343 : /* abort if required */
1344 87 : if (ret != 0) {
1345 : /* LCOV_EXCL_START */
1346 : exit(EXIT_FAILURE);
1347 : /* LCOV_EXCL_STOP */
1348 : }
1349 167 : } else if (operation == OPERATION_DRY) {
1350 2 : state_read(&state);
1351 :
1352 : /* filter */
1353 2 : state_skip(&state);
1354 2 : state_filter(&state, &filterlist_file, &filterlist_disk, filter_missing, filter_error);
1355 :
1356 2 : memory();
1357 :
1358 : /* intercept signals while operating */
1359 2 : signal_init();
1360 :
1361 2 : state_dry(&state, blockstart, blockcount);
1362 165 : } else if (operation == OPERATION_REHASH) {
1363 1 : state_read(&state);
1364 :
1365 : /* intercept signals while operating */
1366 1 : signal_init();
1367 :
1368 1 : state_rehash(&state);
1369 :
1370 : /* save the new state if required */
1371 1 : if (state.need_write)
1372 1 : state_write(&state);
1373 164 : } else if (operation == OPERATION_SCRUB) {
1374 11 : state_read(&state);
1375 :
1376 11 : memory();
1377 :
1378 : /* intercept signals while operating */
1379 11 : signal_init();
1380 :
1381 11 : ret = state_scrub(&state, plan, olderthan);
1382 :
1383 : /* save the new state if required */
1384 11 : if (state.need_write || state.opt.force_content_write)
1385 10 : state_write(&state);
1386 :
1387 : /* abort if required */
1388 11 : if (ret != 0) {
1389 : /* LCOV_EXCL_START */
1390 : exit(EXIT_FAILURE);
1391 : /* LCOV_EXCL_STOP */
1392 : }
1393 153 : } else if (operation == OPERATION_REWRITE) {
1394 1 : state_read(&state);
1395 :
1396 : /* intercept signals while operating */
1397 1 : signal_init();
1398 :
1399 1 : state_write(&state);
1400 :
1401 1 : memory();
1402 152 : } else if (operation == OPERATION_READ) {
1403 1 : state_read(&state);
1404 :
1405 1 : memory();
1406 151 : } else if (operation == OPERATION_TOUCH) {
1407 1 : state_read(&state);
1408 :
1409 1 : state_touch(&state);
1410 :
1411 : /* intercept signals while operating */
1412 1 : signal_init();
1413 :
1414 1 : state_write(&state);
1415 :
1416 1 : memory();
1417 150 : } else if (operation == OPERATION_SPINUP) {
1418 1 : state_device(&state, DEVICE_UP, &filterlist_disk);
1419 149 : } else if (operation == OPERATION_SPINDOWN) {
1420 1 : state_device(&state, DEVICE_DOWN, &filterlist_disk);
1421 148 : } else if (operation == OPERATION_DEVICES) {
1422 1 : state_device(&state, DEVICE_LIST, 0);
1423 147 : } else if (operation == OPERATION_SMART) {
1424 2 : state_device(&state, DEVICE_SMART, 0);
1425 145 : } else if (operation == OPERATION_STATUS) {
1426 14 : state_read(&state);
1427 :
1428 14 : memory();
1429 :
1430 14 : state_status(&state);
1431 131 : } else if (operation == OPERATION_DUP) {
1432 3 : state_read(&state);
1433 :
1434 3 : state_dup(&state);
1435 128 : } else if (operation == OPERATION_LIST) {
1436 5 : state_read(&state);
1437 :
1438 5 : state_list(&state);
1439 123 : } else if (operation == OPERATION_POOL) {
1440 3 : state_read(&state);
1441 :
1442 3 : state_pool(&state);
1443 : } else {
1444 120 : state_read(&state);
1445 :
1446 : /* if we are also trying to recover */
1447 120 : if (!state.opt.auditonly) {
1448 : /* import the user specified dirs */
1449 114 : if (import_timestamp != 0)
1450 1 : state_search(&state, import_timestamp);
1451 114 : if (import_content != 0)
1452 1 : state_import(&state, import_content);
1453 :
1454 : /* import from all the array */
1455 114 : if (!state.opt.force_nocopy)
1456 114 : state_search_array(&state);
1457 : }
1458 :
1459 : /* filter */
1460 120 : state_skip(&state);
1461 120 : state_filter(&state, &filterlist_file, &filterlist_disk, filter_missing, filter_error);
1462 :
1463 120 : memory();
1464 :
1465 : /* intercept signals while operating */
1466 120 : signal_init();
1467 :
1468 120 : if (operation == OPERATION_CHECK) {
1469 75 : ret = state_check(&state, 0, blockstart, blockcount);
1470 : } else { /* it's fix */
1471 45 : ret = state_check(&state, 1, blockstart, blockcount);
1472 : }
1473 :
1474 : /* abort if required */
1475 120 : if (ret != 0) {
1476 : /* LCOV_EXCL_START */
1477 : exit(EXIT_FAILURE);
1478 : /* LCOV_EXCL_STOP */
1479 : }
1480 : }
1481 :
1482 : /* close log file */
1483 242 : log_close(log_file);
1484 :
1485 : #if HAVE_LOCKFILE
1486 242 : if (!opt.skip_lock && state.lockfile[0]) {
1487 240 : if (lock_unlock(lock) == -1) {
1488 : /* LCOV_EXCL_START */
1489 : log_fatal("Error closing the lock file '%s'. %s.\n", state.lockfile, strerror(errno));
1490 : exit(EXIT_FAILURE);
1491 : /* LCOV_EXCL_STOP */
1492 : }
1493 : }
1494 : #endif
1495 :
1496 242 : state_done(&state);
1497 242 : tommy_list_foreach(&filterlist_file, (tommy_foreach_func*)filter_free);
1498 242 : tommy_list_foreach(&filterlist_disk, (tommy_foreach_func*)filter_free);
1499 :
1500 242 : os_done();
1501 242 : lock_done();
1502 :
1503 242 : return EXIT_SUCCESS;
1504 : }
1505 :
|