1 /**
2 * main.c
3 *
4 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 * Copyright (c) 2015 Jaegeuk Kim <jaegeuk@kernel.org>
7 * : implement defrag.f2fs
8 * Copyright (C) 2015 Huawei Ltd.
9 * Hou Pengyang <houpengyang@huawei.com>
10 * Liu Shuoran <liushuoran@huawei.com>
11 * Jaegeuk Kim <jaegeuk@kernel.org>
12 * : add sload.f2fs
13 * Copyright (c) 2019 Google Inc.
14 * Robin Hsu <robinhsu@google.com>
15 * : add cache layer
16 * Copyright (c) 2020 Google Inc.
17 * Robin Hsu <robinhsu@google.com>
18 * : add sload compression support
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License version 2 as
22 * published by the Free Software Foundation.
23 */
24 #include "fsck.h"
25 #include <libgen.h>
26 #include <ctype.h>
27 #include <time.h>
28 #include <getopt.h>
29 #include <stdbool.h>
30 #include "quotaio.h"
31 #include "compress.h"
32
33 struct f2fs_fsck gfsck;
34
35 INIT_FEATURE_TABLE;
36
37 #if defined(WITH_SLOAD) || defined(WITH_DUMP)
absolute_path(const char * file)38 static char *absolute_path(const char *file)
39 {
40 char *ret;
41 char cwd[PATH_MAX];
42
43 if (file[0] != '/') {
44 if (getcwd(cwd, PATH_MAX) == NULL) {
45 fprintf(stderr, "Failed to getcwd\n");
46 exit(EXIT_FAILURE);
47 }
48 ret = malloc(strlen(cwd) + 1 + strlen(file) + 1);
49 if (ret)
50 sprintf(ret, "%s/%s", cwd, file);
51 } else
52 ret = strdup(file);
53 return ret;
54 }
55 #endif
56
fsck_usage()57 void fsck_usage()
58 {
59 MSG(0, "\nUsage: fsck.f2fs [options] device\n");
60 MSG(0, "[options]:\n");
61 MSG(0, " -a check/fix potential corruption, reported by f2fs\n");
62 MSG(0, " -c <num-cache-entry> set number of cache entries"
63 " (default 0)\n");
64 MSG(0, " -m <max-hash-collision> set max cache hash collision"
65 " (default 16)\n");
66 MSG(0, " -C encoding[:flag1,flag2] Set options for enabling"
67 " casefolding\n");
68 MSG(0, " -d debug level [default:0]\n");
69 MSG(0, " -f check/fix entire partition\n");
70 MSG(0, " -g add default options\n");
71 MSG(0, " -l show superblock/checkpoint\n");
72 MSG(0, " -M show a file map\n");
73 MSG(0, " -O feature1[feature2,feature3,...] e.g. \"encrypt\"\n");
74 MSG(0, " -p preen mode [default:0 the same as -a [0|1|2]]\n");
75 MSG(0, " -S sparse_mode\n");
76 MSG(0, " -t show directory tree\n");
77 MSG(0, " -q preserve quota limits\n");
78 MSG(0, " -y fix all the time\n");
79 MSG(0, " -V print the version number and exit\n");
80 MSG(0, " --dry-run do not really fix corruptions\n");
81 MSG(0, " --no-kernel-check skips detecting kernel change\n");
82 MSG(0, " --kernel-check checks kernel change\n");
83 MSG(0, " --debug-cache to debug cache when -c is used\n");
84 exit(1);
85 }
86
dump_usage()87 void dump_usage()
88 {
89 MSG(0, "\nUsage: dump.f2fs [options] device\n");
90 MSG(0, "[options]:\n");
91 MSG(0, " -d debug level [default:0]\n");
92 MSG(0, " -i inode no (hex)\n");
93 MSG(0, " -I inode no (hex) scan full disk\n");
94 MSG(0, " -n [NAT dump nid from #1~#2 (decimal), for all 0~-1]\n");
95 MSG(0, " -M show a block map\n");
96 MSG(0, " -s [SIT dump segno from #1~#2 (decimal), for all 0~-1]\n");
97 MSG(0, " -S sparse_mode\n");
98 MSG(0, " -a [SSA dump segno from #1~#2 (decimal), for all 0~-1]\n");
99 MSG(0, " -b blk_addr (in 4KB)\n");
100 MSG(0, " -V print the version number and exit\n");
101
102 exit(1);
103 }
104
defrag_usage()105 void defrag_usage()
106 {
107 MSG(0, "\nUsage: defrag.f2fs [options] device\n");
108 MSG(0, "[options]:\n");
109 MSG(0, " -d debug level [default:0]\n");
110 MSG(0, " -s start block address [default: main_blkaddr]\n");
111 MSG(0, " -S sparse_mode\n");
112 MSG(0, " -l length [default:512 (2MB)]\n");
113 MSG(0, " -t target block address [default: main_blkaddr + 2MB]\n");
114 MSG(0, " -i set direction as shrink [default: expand]\n");
115 MSG(0, " -V print the version number and exit\n");
116 exit(1);
117 }
118
resize_usage()119 void resize_usage()
120 {
121 MSG(0, "\nUsage: resize.f2fs [options] device\n");
122 MSG(0, "[options]:\n");
123 MSG(0, " -d debug level [default:0]\n");
124 MSG(0, " -i extended node bitmap, node ratio is 20%% by default\n");
125 MSG(0, " -o overprovision percentage [default:auto]\n");
126 MSG(0, " -s safe resize (Does not resize metadata)\n");
127 MSG(0, " -t target sectors [default: device size]\n");
128 MSG(0, " -V print the version number and exit\n");
129 exit(1);
130 }
131
sload_usage()132 void sload_usage()
133 {
134 MSG(0, "\nUsage: sload.f2fs [options] device\n");
135 MSG(0, "[options]:\n");
136 MSG(0, " -C fs_config\n");
137 MSG(0, " -f source directory [path of the source directory]\n");
138 MSG(0, " -p product out directory\n");
139 MSG(0, " -s file_contexts\n");
140 MSG(0, " -S sparse_mode\n");
141 MSG(0, " -t mount point [prefix of target fs path, default:/]\n");
142 MSG(0, " -T timestamp\n");
143 MSG(0, " -P preserve owner: user and group\n");
144 MSG(0, " -c enable compression (default allow policy)\n");
145 MSG(0, " ------------ Compression sub-options -----------------\n");
146 MSG(0, " -L <log-of-blocks-per-cluster>, default 2\n");
147 MSG(0, " -a <algorithm> compression algorithm, default LZ4\n");
148 MSG(0, " -x <ext> compress files except for these extensions.\n");
149 MSG(0, " -i <ext> compress files with these extensions only.\n");
150 MSG(0, " * -i or -x: use it many times for multiple extensions.\n");
151 MSG(0, " * -i and -x cannot be used together..\n");
152 MSG(0, " -m <num> min compressed blocks per cluster\n");
153 MSG(0, " -r read only (to release unused blocks) for compressed "
154 "files\n");
155 MSG(0, " ------------------------------------------------------\n");
156 MSG(0, " -d debug level [default:0]\n");
157 MSG(0, " -V print the version number and exit\n");
158 exit(1);
159 }
160
label_usage()161 void label_usage()
162 {
163 MSG(0, "\nUsage: f2fslabel [options] device [volume-label]\n");
164 MSG(0, "[options]:\n");
165 MSG(0, " -V print the version number and exit\n");
166 exit(1);
167 }
168
is_digits(char * optarg)169 static int is_digits(char *optarg)
170 {
171 unsigned int i;
172
173 for (i = 0; i < strlen(optarg); i++)
174 if (!isdigit(optarg[i]))
175 break;
176 return i == strlen(optarg);
177 }
178
error_out(char * prog)179 static void error_out(char *prog)
180 {
181 if (!strcmp("fsck.f2fs", prog))
182 fsck_usage();
183 else if (!strcmp("dump.f2fs", prog))
184 dump_usage();
185 else if (!strcmp("defrag.f2fs", prog))
186 defrag_usage();
187 else if (!strcmp("resize.f2fs", prog))
188 resize_usage();
189 else if (!strcmp("sload.f2fs", prog))
190 sload_usage();
191 else if (!strcmp("f2fslabel", prog))
192 label_usage();
193 else
194 MSG(0, "\nWrong program.\n");
195 }
196
__add_fsck_options(void)197 static void __add_fsck_options(void)
198 {
199 /* -a */
200 c.auto_fix = 1;
201 }
202
add_default_options(void)203 static void add_default_options(void)
204 {
205 switch (c.defset) {
206 case CONF_ANDROID:
207 __add_fsck_options();
208 }
209 c.quota_fix = 1;
210 }
211
f2fs_parse_options(int argc,char * argv[])212 void f2fs_parse_options(int argc, char *argv[])
213 {
214 int option = 0;
215 char *prog = basename(argv[0]);
216 int err = NOERROR;
217 #ifdef WITH_ANDROID
218 int i;
219
220 /* Allow prog names (e.g, sload_f2fs, fsck_f2fs, etc) */
221 for (i = 0; i < strlen(prog); i++) {
222 if (prog[i] == '_')
223 prog[i] = '.';
224 }
225 #endif
226 if (argc < 2) {
227 MSG(0, "\tError: Device not specified\n");
228 error_out(prog);
229 }
230
231 if (!strcmp("fsck.f2fs", prog)) {
232 const char *option_string = ":aC:c:m:Md:fg:lO:p:q:StyV";
233 int opt = 0, val;
234 char *token;
235 struct option long_opt[] = {
236 {"dry-run", no_argument, 0, 1},
237 {"no-kernel-check", no_argument, 0, 2},
238 {"kernel-check", no_argument, 0, 3},
239 {"debug-cache", no_argument, 0, 4},
240 {0, 0, 0, 0}
241 };
242
243 c.func = FSCK;
244 c.cache_config.max_hash_collision = 16;
245 c.cache_config.dbg_en = false;
246 while ((option = getopt_long(argc, argv, option_string,
247 long_opt, &opt)) != EOF) {
248 switch (option) {
249 case 1:
250 c.dry_run = 1;
251 MSG(0, "Info: Dry run\n");
252 break;
253 case 2:
254 c.no_kernel_check = 1;
255 MSG(0, "Info: No Kernel Check\n");
256 break;
257 case 3:
258 c.no_kernel_check = 0;
259 MSG(0, "Info: Do Kernel Check\n");
260 break;
261 case 4:
262 c.cache_config.dbg_en = true;
263 break;
264 case 'a':
265 c.auto_fix = 1;
266 MSG(0, "Info: Automatic fix mode enabled.\n");
267 break;
268 case 'c':
269 c.cache_config.num_cache_entry = atoi(optarg);
270 break;
271 case 'm':
272 c.cache_config.max_hash_collision =
273 atoi(optarg);
274 break;
275 case 'g':
276 if (!strcmp(optarg, "android")) {
277 c.defset = CONF_ANDROID;
278 MSG(0, "Info: Set conf for android\n");
279 }
280 break;
281 case 'l':
282 c.layout = 1;
283 break;
284 case 'M':
285 c.show_file_map = 1;
286 break;
287 case 'O':
288 if (parse_feature(feature_table, optarg))
289 fsck_usage();
290 break;
291 case 'p':
292 /* preen mode has different levels:
293 * 0: default level, the same as -a
294 * 1: check meta
295 * 2: same as 0, but will skip some
296 * check for old kernel
297 */
298 if (optarg[0] == '-' || !is_digits(optarg) ||
299 optind == argc) {
300 MSG(0, "Info: Use default preen mode\n");
301 c.preen_mode = PREEN_MODE_0;
302 c.auto_fix = 1;
303 optind--;
304 break;
305 }
306 c.preen_mode = atoi(optarg);
307 if (c.preen_mode < 0)
308 c.preen_mode = PREEN_MODE_0;
309 else if (c.preen_mode >= PREEN_MODE_MAX)
310 c.preen_mode = PREEN_MODE_MAX - 1;
311 if (c.preen_mode == PREEN_MODE_0 ||
312 c.preen_mode == PREEN_MODE_2)
313 c.auto_fix = 1;
314 MSG(0, "Info: Fix the reported corruption in "
315 "preen mode %d\n", c.preen_mode);
316 break;
317 case 'd':
318 if (optarg[0] == '-') {
319 err = ENEED_ARG;
320 break;
321 } else if (!is_digits(optarg)) {
322 err = EWRONG_OPT;
323 break;
324 }
325 c.dbg_lv = atoi(optarg);
326 MSG(0, "Info: Debug level = %d\n", c.dbg_lv);
327 break;
328 case 'f':
329 case 'y':
330 c.fix_on = 1;
331 c.force = 1;
332 MSG(0, "Info: Force to fix corruption\n");
333 break;
334 case 'q':
335 c.preserve_limits = atoi(optarg);
336 MSG(0, "Info: Preserve quota limits = %d\n",
337 c.preserve_limits);
338 break;
339 case 'S':
340 c.sparse_mode = 1;
341 break;
342 case 't':
343 c.show_dentry = 1;
344 break;
345 case ':':
346 if (optopt == 'p') {
347 MSG(0, "Info: Use default preen mode\n");
348 c.preen_mode = PREEN_MODE_0;
349 c.auto_fix = 1;
350 } else {
351 option = optopt;
352 err = ENEED_ARG;
353 break;
354 }
355 break;
356 case 'C':
357 token = strtok(optarg, ":");
358 val = f2fs_str2encoding(token);
359 if (val < 0) {
360 MSG(0, "\tError: Unknown encoding %s\n", token);
361 fsck_usage();
362 }
363 c.s_encoding = val;
364 token = strtok(NULL, "");
365 val = f2fs_str2encoding_flags(&token, &c.s_encoding_flags);
366 if (val) {
367 MSG(0, "\tError: Unknown flag %s\n", token);
368 fsck_usage();
369 }
370 c.feature |= F2FS_FEATURE_CASEFOLD;
371 break;
372 case 'V':
373 show_version(prog);
374 exit(0);
375 case '?':
376 option = optopt;
377 fallthrough;
378 default:
379 err = EUNKNOWN_OPT;
380 break;
381 }
382 if (err != NOERROR)
383 break;
384 }
385 } else if (!strcmp("dump.f2fs", prog)) {
386 #ifdef WITH_DUMP
387 const char *option_string = "d:fi:I:n:Mo:Prs:Sa:b:Vy";
388 static struct dump_option dump_opt = {
389 .nid = 0, /* default root ino */
390 .start_nat = -1,
391 .end_nat = -1,
392 .start_sit = -1,
393 .end_sit = -1,
394 .start_ssa = -1,
395 .end_ssa = -1,
396 .blk_addr = -1,
397 .scan_nid = 0,
398 .use_root_nid = 0,
399 .base_path = NULL,
400 };
401
402 c.func = DUMP;
403 while ((option = getopt(argc, argv, option_string)) != EOF) {
404 int ret = 0;
405
406 switch (option) {
407 case 'd':
408 if (!is_digits(optarg)) {
409 err = EWRONG_OPT;
410 break;
411 }
412 c.dbg_lv = atoi(optarg);
413 MSG(0, "Info: Debug level = %d\n",
414 c.dbg_lv);
415 break;
416 case 'i':
417 if (strncmp(optarg, "0x", 2))
418 ret = sscanf(optarg, "%d",
419 &dump_opt.nid);
420 else
421 ret = sscanf(optarg, "%x",
422 &dump_opt.nid);
423 break;
424 case 'I':
425 if (strncmp(optarg, "0x", 2))
426 ret = sscanf(optarg, "%d",
427 &dump_opt.scan_nid);
428 else
429 ret = sscanf(optarg, "%x",
430 &dump_opt.scan_nid);
431 break;
432 case 'n':
433 ret = sscanf(optarg, "%d~%d",
434 &dump_opt.start_nat,
435 &dump_opt.end_nat);
436 break;
437 case 'M':
438 c.show_file_map = 1;
439 break;
440 case 's':
441 ret = sscanf(optarg, "%d~%d",
442 &dump_opt.start_sit,
443 &dump_opt.end_sit);
444 break;
445 case 'S':
446 c.sparse_mode = 1;
447 break;
448 case 'a':
449 ret = sscanf(optarg, "%d~%d",
450 &dump_opt.start_ssa,
451 &dump_opt.end_ssa);
452 break;
453 case 'b':
454 if (strncmp(optarg, "0x", 2))
455 ret = sscanf(optarg, "%d",
456 &dump_opt.blk_addr);
457 else
458 ret = sscanf(optarg, "%x",
459 &dump_opt.blk_addr);
460 break;
461 case 'y':
462 case 'f':
463 c.force = 1;
464 break;
465 case 'r':
466 dump_opt.use_root_nid = 1;
467 break;
468 case 'o':
469 dump_opt.base_path = absolute_path(optarg);
470 break;
471 case 'P':
472 #if defined(__MINGW32__)
473 MSG(0, "-P not supported for Windows\n");
474 err = EWRONG_OPT;
475 #else
476 c.preserve_perms = 1;
477 #endif
478 break;
479 case 'V':
480 show_version(prog);
481 exit(0);
482 default:
483 err = EUNKNOWN_OPT;
484 break;
485 }
486 ASSERT(ret >= 0);
487 if (err != NOERROR)
488 break;
489 }
490
491 c.private = &dump_opt;
492 #endif
493 } else if (!strcmp("defrag.f2fs", prog)) {
494 #ifdef WITH_DEFRAG
495 const char *option_string = "d:s:Sl:t:iV";
496
497 c.func = DEFRAG;
498 while ((option = getopt(argc, argv, option_string)) != EOF) {
499 int ret = 0;
500
501 switch (option) {
502 case 'd':
503 if (!is_digits(optarg)) {
504 err = EWRONG_OPT;
505 break;
506 }
507 c.dbg_lv = atoi(optarg);
508 MSG(0, "Info: Debug level = %d\n",
509 c.dbg_lv);
510 break;
511 case 's':
512 if (strncmp(optarg, "0x", 2))
513 ret = sscanf(optarg, "%"PRIu64"",
514 &c.defrag_start);
515 else
516 ret = sscanf(optarg, "%"PRIx64"",
517 &c.defrag_start);
518 break;
519 case 'S':
520 c.sparse_mode = 1;
521 break;
522 case 'l':
523 if (strncmp(optarg, "0x", 2))
524 ret = sscanf(optarg, "%"PRIu64"",
525 &c.defrag_len);
526 else
527 ret = sscanf(optarg, "%"PRIx64"",
528 &c.defrag_len);
529 break;
530 case 't':
531 if (strncmp(optarg, "0x", 2))
532 ret = sscanf(optarg, "%"PRIu64"",
533 &c.defrag_target);
534 else
535 ret = sscanf(optarg, "%"PRIx64"",
536 &c.defrag_target);
537 break;
538 case 'i':
539 c.defrag_shrink = 1;
540 break;
541 case 'V':
542 show_version(prog);
543 exit(0);
544 default:
545 err = EUNKNOWN_OPT;
546 break;
547 }
548 ASSERT(ret >= 0);
549 if (err != NOERROR)
550 break;
551 }
552 #endif
553 } else if (!strcmp("resize.f2fs", prog)) {
554 #ifdef WITH_RESIZE
555 const char *option_string = "d:fst:io:V";
556
557 c.func = RESIZE;
558 while ((option = getopt(argc, argv, option_string)) != EOF) {
559 int ret = 0;
560
561 switch (option) {
562 case 'd':
563 if (!is_digits(optarg)) {
564 err = EWRONG_OPT;
565 break;
566 }
567 c.dbg_lv = atoi(optarg);
568 MSG(0, "Info: Debug level = %d\n",
569 c.dbg_lv);
570 break;
571 case 'f':
572 c.force = 1;
573 MSG(0, "Info: Force to resize\n");
574 break;
575 case 's':
576 c.safe_resize = 1;
577 break;
578 case 't':
579 if (strncmp(optarg, "0x", 2))
580 ret = sscanf(optarg, "%"PRIu64"",
581 &c.target_sectors);
582 else
583 ret = sscanf(optarg, "%"PRIx64"",
584 &c.target_sectors);
585 break;
586 case 'i':
587 c.large_nat_bitmap = 1;
588 break;
589 case 'o':
590 c.new_overprovision = atof(optarg);
591 break;
592 case 'V':
593 show_version(prog);
594 exit(0);
595 default:
596 err = EUNKNOWN_OPT;
597 break;
598 }
599 ASSERT(ret >= 0);
600 if (err != NOERROR)
601 break;
602 }
603 #endif
604 } else if (!strcmp("sload.f2fs", prog)) {
605 #ifdef WITH_SLOAD
606 const char *option_string = "cL:a:i:x:m:rC:d:f:p:s:St:T:VP";
607 #ifdef HAVE_LIBSELINUX
608 int max_nr_opt = (int)sizeof(c.seopt_file) /
609 sizeof(c.seopt_file[0]);
610 char *token;
611 #endif
612 char *p;
613
614 c.func = SLOAD;
615 c.compress.cc.log_cluster_size = 2;
616 c.compress.alg = COMPR_LZ4;
617 c.compress.min_blocks = 1;
618 c.compress.filter_ops = &ext_filter;
619 while ((option = getopt(argc, argv, option_string)) != EOF) {
620 unsigned int i;
621 int val;
622
623 switch (option) {
624 case 'c': /* compression support */
625 c.compress.enabled = true;
626 break;
627 case 'L': /* compression: log of blocks-per-cluster */
628 c.compress.required = true;
629 val = atoi(optarg);
630 if (val < MIN_COMPRESS_LOG_SIZE ||
631 val > MAX_COMPRESS_LOG_SIZE) {
632 MSG(0, "\tError: log of blocks per"
633 " cluster must be in the range"
634 " of %d .. %d.\n",
635 MIN_COMPRESS_LOG_SIZE,
636 MAX_COMPRESS_LOG_SIZE);
637 error_out(prog);
638 }
639 c.compress.cc.log_cluster_size = val;
640 break;
641 case 'a': /* compression: choose algorithm */
642 c.compress.required = true;
643 c.compress.alg = MAX_COMPRESS_ALGS;
644 for (i = 0; i < MAX_COMPRESS_ALGS; i++) {
645 if (!strcmp(supported_comp_names[i],
646 optarg)) {
647 c.compress.alg = i;
648 break;
649 }
650 }
651 if (c.compress.alg == MAX_COMPRESS_ALGS) {
652 MSG(0, "\tError: Unknown compression"
653 " algorithm %s\n", optarg);
654 error_out(prog);
655 }
656 break;
657 case 'i': /* compress only these extensions */
658 c.compress.required = true;
659 if (c.compress.filter == COMPR_FILTER_ALLOW) {
660 MSG(0, "\tError: could not mix option"
661 " -i and -x\n");
662 error_out(prog);
663 }
664 c.compress.filter = COMPR_FILTER_DENY;
665 c.compress.filter_ops->add(optarg);
666 break;
667 case 'x': /* compress except for these extensions */
668 c.compress.required = true;
669 if (c.compress.filter == COMPR_FILTER_DENY) {
670 MSG(0, "\tError: could not mix option"
671 " -i and -x\n");
672 error_out(prog);
673 }
674 c.compress.filter = COMPR_FILTER_ALLOW;
675 c.compress.filter_ops->add(optarg);
676 break;
677 case 'm': /* minimum compressed blocks per cluster */
678 c.compress.required = true;
679 val = atoi(optarg);
680 if (val <= 0) {
681 MSG(0, "\tError: minimum compressed"
682 " blocks per cluster must be"
683 " positive.\n");
684 error_out(prog);
685 }
686 c.compress.min_blocks = val;
687 break;
688 case 'r': /* for setting FI_COMPRESS_RELEASED */
689 c.compress.required = true;
690 c.compress.readonly = true;
691 break;
692 case 'C':
693 c.fs_config_file = absolute_path(optarg);
694 break;
695 case 'd':
696 if (!is_digits(optarg)) {
697 err = EWRONG_OPT;
698 break;
699 }
700 c.dbg_lv = atoi(optarg);
701 MSG(0, "Info: Debug level = %d\n",
702 c.dbg_lv);
703 break;
704 case 'f':
705 c.from_dir = absolute_path(optarg);
706 break;
707 case 'p':
708 c.target_out_dir = absolute_path(optarg);
709 break;
710 case 's':
711 #ifdef HAVE_LIBSELINUX
712 token = strtok(optarg, ",");
713 while (token) {
714 if (c.nr_opt == max_nr_opt) {
715 MSG(0, "\tError: Expected at most %d selinux opts\n",
716 max_nr_opt);
717 error_out(prog);
718 }
719 c.seopt_file[c.nr_opt].type =
720 SELABEL_OPT_PATH;
721 c.seopt_file[c.nr_opt].value =
722 absolute_path(token);
723 c.nr_opt++;
724 token = strtok(NULL, ",");
725 }
726 #else
727 MSG(0, "Info: Not support selinux opts\n");
728 #endif
729 break;
730 case 'S':
731 c.sparse_mode = 1;
732 break;
733 case 't':
734 c.mount_point = (char *)optarg;
735 break;
736 case 'T':
737 c.fixed_time = strtoul(optarg, &p, 0);
738 break;
739 case 'V':
740 show_version(prog);
741 exit(0);
742 case 'P':
743 c.preserve_perms = 1;
744 break;
745 default:
746 err = EUNKNOWN_OPT;
747 break;
748 }
749 if (err != NOERROR)
750 break;
751 }
752 if (c.compress.required && !c.compress.enabled) {
753 MSG(0, "\tError: compression sub-options are used"
754 " without the compression enable (-c) option\n"
755 );
756 error_out(prog);
757 }
758 if (err == NOERROR && c.compress.enabled) {
759 c.compress.cc.cluster_size = 1
760 << c.compress.cc.log_cluster_size;
761 if (c.compress.filter == COMPR_FILTER_UNASSIGNED)
762 c.compress.filter = COMPR_FILTER_ALLOW;
763 if (c.compress.min_blocks >=
764 c.compress.cc.cluster_size) {
765 MSG(0, "\tError: minimum reduced blocks by"
766 " compression per cluster must be at"
767 " most one less than blocks per"
768 " cluster, i.e. %d\n",
769 c.compress.cc.cluster_size - 1);
770 error_out(prog);
771 }
772 }
773 #endif /* WITH_SLOAD */
774 } else if (!strcmp("f2fslabel", prog)) {
775 #ifdef WITH_LABEL
776 const char *option_string = "V";
777
778 c.func = LABEL;
779 while ((option = getopt(argc, argv, option_string)) != EOF) {
780 switch (option) {
781 case 'V':
782 show_version(prog);
783 exit(0);
784 default:
785 err = EUNKNOWN_OPT;
786 break;
787 }
788 if (err != NOERROR)
789 break;
790 }
791
792 if (argc > (optind + 2)) { /* unknown argument(s) is(are) passed */
793 optind += 2;
794 err = EUNKNOWN_ARG;
795 } else if (argc == (optind + 2)) { /* change label */
796 c.vol_label = argv[optind + 1];
797 argc--;
798 } else { /* print label */
799 /*
800 * Since vol_label was initialized as "", in order to
801 * distinguish between clear label and print, set
802 * vol_label as NULL for print case
803 */
804 c.vol_label = NULL;
805 }
806 #endif /* WITH_LABEL */
807 }
808
809 if (err == NOERROR) {
810 add_default_options();
811
812 if (optind >= argc) {
813 MSG(0, "\tError: Device not specified\n");
814 error_out(prog);
815 }
816
817 c.devices[0].path = strdup(argv[optind]);
818 if (argc > (optind + 1)) {
819 c.dbg_lv = 0;
820 err = EUNKNOWN_ARG;
821 }
822 if (err == NOERROR)
823 return;
824 }
825
826 check_block_struct_sizes();
827 /* print out error */
828 switch (err) {
829 case EWRONG_OPT:
830 MSG(0, "\tError: Wrong option -%c %s\n", option, optarg);
831 break;
832 case ENEED_ARG:
833 MSG(0, "\tError: Need argument for -%c\n", option);
834 break;
835 case EUNKNOWN_OPT:
836 MSG(0, "\tError: Unknown option %c\n", option);
837 break;
838 case EUNKNOWN_ARG:
839 MSG(0, "\tError: Unknown argument %s\n", argv[optind]);
840 break;
841 }
842 error_out(prog);
843 }
844
do_fsck(struct f2fs_sb_info * sbi)845 static int do_fsck(struct f2fs_sb_info *sbi)
846 {
847 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
848 u32 flag = le32_to_cpu(ckpt->ckpt_flags);
849 u32 blk_cnt;
850 struct f2fs_compr_blk_cnt cbc;
851 errcode_t ret;
852
853 fsck_init(sbi);
854
855 print_cp_state(flag);
856
857 if (c.roll_forward && c.zoned_model == F2FS_ZONED_HM)
858 save_curseg_warm_node_info(sbi);
859
860 fsck_chk_and_fix_write_pointers(sbi);
861
862 fsck_chk_curseg_info(sbi);
863
864 if (!c.fix_on && !c.bug_on) {
865 switch (c.preen_mode) {
866 case PREEN_MODE_1:
867 if (fsck_chk_meta(sbi)) {
868 MSG(0, "[FSCK] F2FS metadata [Fail]");
869 MSG(0, "\tError: meta does not match, "
870 "force check all\n");
871 } else {
872 MSG(0, "[FSCK] F2FS metadata [Ok..]");
873 fsck_free(sbi);
874 return FSCK_SUCCESS;
875 }
876
877 if (!c.ro)
878 c.fix_on = 1;
879 break;
880 }
881 } else if (c.preen_mode) {
882 /*
883 * we can hit this in 3 situations:
884 * 1. fsck -f, fix_on has already been set to 1 when
885 * parsing options;
886 * 2. fsck -a && CP_FSCK_FLAG is set, fix_on has already
887 * been set to 1 when checking CP_FSCK_FLAG;
888 * 3. fsck -p 1 && error is detected, then bug_on is set,
889 * we set fix_on = 1 here, so that fsck can fix errors
890 * automatically
891 */
892 c.fix_on = 1;
893 }
894
895 fsck_chk_checkpoint(sbi);
896
897 fsck_chk_quota_node(sbi);
898
899 /* Traverse all block recursively from root inode */
900 blk_cnt = 1;
901 cbc.cnt = 0;
902 cbc.cheader_pgofs = CHEADER_PGOFS_NONE;
903
904 if (c.feature & F2FS_FEATURE_QUOTA_INO) {
905 ret = quota_init_context(sbi);
906 if (ret) {
907 ASSERT_MSG("quota_init_context failure: %d", ret);
908 return FSCK_OPERATIONAL_ERROR;
909 }
910 }
911 fsck_chk_orphan_node(sbi);
912
913 if (fsck_sanity_check_nat(sbi, sbi->root_ino_num))
914 fsck_chk_root_inode(sbi);
915
916 fsck_chk_node_blk(sbi, NULL, sbi->root_ino_num,
917 F2FS_FT_DIR, TYPE_INODE, &blk_cnt, &cbc, NULL);
918 fsck_chk_quota_files(sbi);
919
920 ret = fsck_verify(sbi);
921 fsck_free(sbi);
922
923 if (!c.bug_on)
924 return FSCK_SUCCESS;
925 if (!ret)
926 return FSCK_ERROR_CORRECTED;
927 return FSCK_ERRORS_LEFT_UNCORRECTED;
928 }
929
930 #ifdef WITH_DUMP
do_dump(struct f2fs_sb_info * sbi)931 static void do_dump(struct f2fs_sb_info *sbi)
932 {
933 struct dump_option *opt = (struct dump_option *)c.private;
934 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
935 u32 flag = le32_to_cpu(ckpt->ckpt_flags);
936
937 if (opt->use_root_nid)
938 opt->nid = sbi->root_ino_num;
939
940 if (opt->end_nat == -1)
941 opt->end_nat = NM_I(sbi)->max_nid;
942 if (opt->end_sit == -1)
943 opt->end_sit = SM_I(sbi)->main_segments;
944 if (opt->end_ssa == -1)
945 opt->end_ssa = SM_I(sbi)->main_segments;
946 if (opt->start_nat != -1)
947 nat_dump(sbi, opt->start_nat, opt->end_nat);
948 if (opt->start_sit != -1)
949 sit_dump(sbi, opt->start_sit, opt->end_sit);
950 if (opt->start_ssa != -1)
951 ssa_dump(sbi, opt->start_ssa, opt->end_ssa);
952 if (opt->blk_addr != -1)
953 dump_info_from_blkaddr(sbi, opt->blk_addr);
954 if (opt->nid)
955 dump_node(sbi, opt->nid, c.force, opt->base_path, 1, 1);
956 if (opt->scan_nid)
957 dump_node_scan_disk(sbi, opt->scan_nid);
958
959 print_cp_state(flag);
960
961 }
962 #endif
963
964 #ifdef WITH_DEFRAG
do_defrag(struct f2fs_sb_info * sbi)965 static int do_defrag(struct f2fs_sb_info *sbi)
966 {
967 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
968
969 if (get_sb(feature) & F2FS_FEATURE_RO) {
970 MSG(0, "Not support on readonly image.\n");
971 return -1;
972 }
973
974 if (c.defrag_start > get_sb(block_count))
975 goto out_range;
976 if (c.defrag_start < SM_I(sbi)->main_blkaddr)
977 c.defrag_start = SM_I(sbi)->main_blkaddr;
978
979 if (c.defrag_len == 0)
980 c.defrag_len = sbi->blocks_per_seg;
981
982 if (c.defrag_start + c.defrag_len > get_sb(block_count))
983 c.defrag_len = get_sb(block_count) - c.defrag_start;
984
985 if (c.defrag_target == 0) {
986 c.defrag_target = c.defrag_start - 1;
987 if (!c.defrag_shrink)
988 c.defrag_target += c.defrag_len + 1;
989 }
990
991 if (c.defrag_target < SM_I(sbi)->main_blkaddr ||
992 c.defrag_target > get_sb(block_count))
993 goto out_range;
994 if (c.defrag_target >= c.defrag_start &&
995 c.defrag_target < c.defrag_start + c.defrag_len)
996 goto out_range;
997
998 if (c.defrag_start > c.defrag_target)
999 MSG(0, "Info: Move 0x%"PRIx64" <- [0x%"PRIx64"-0x%"PRIx64"]\n",
1000 c.defrag_target,
1001 c.defrag_start,
1002 c.defrag_start + c.defrag_len - 1);
1003 else
1004 MSG(0, "Info: Move [0x%"PRIx64"-0x%"PRIx64"] -> 0x%"PRIx64"\n",
1005 c.defrag_start,
1006 c.defrag_start + c.defrag_len - 1,
1007 c.defrag_target);
1008
1009 return f2fs_defragment(sbi, c.defrag_start, c.defrag_len,
1010 c.defrag_target, c.defrag_shrink);
1011 out_range:
1012 ASSERT_MSG("Out-of-range [0x%"PRIx64" ~ 0x%"PRIx64"] to 0x%"PRIx64"",
1013 c.defrag_start,
1014 c.defrag_start + c.defrag_len - 1,
1015 c.defrag_target);
1016 return -1;
1017 }
1018 #endif
1019
1020 #ifdef WITH_RESIZE
do_resize(struct f2fs_sb_info * sbi)1021 static int do_resize(struct f2fs_sb_info *sbi)
1022 {
1023 if (!c.target_sectors)
1024 c.target_sectors = c.total_sectors;
1025
1026 if (c.target_sectors > c.total_sectors) {
1027 ASSERT_MSG("Out-of-range Target=0x%"PRIx64" / 0x%"PRIx64"",
1028 c.target_sectors, c.total_sectors);
1029 return -1;
1030 }
1031
1032 return f2fs_resize(sbi);
1033 }
1034 #endif
1035
1036 #ifdef WITH_SLOAD
init_compr(struct f2fs_sb_info * sbi)1037 static int init_compr(struct f2fs_sb_info *sbi)
1038 {
1039 if (!c.compress.enabled)
1040 return 0;
1041
1042 if (!(sbi->raw_super->feature
1043 & cpu_to_le32(F2FS_FEATURE_COMPRESSION))) {
1044 MSG(0, "Error: Compression (-c) was requested "
1045 "but the file system is not created "
1046 "with such feature.\n");
1047 return -1;
1048 }
1049 if (!supported_comp_ops[c.compress.alg].init) {
1050 MSG(0, "Error: The selected compression algorithm is not"
1051 " supported\n");
1052 return -1;
1053 }
1054 c.compress.ops = supported_comp_ops + c.compress.alg;
1055 c.compress.ops->init(&c.compress.cc);
1056 c.compress.ops->reset(&c.compress.cc);
1057 c.compress.cc.rlen = c.compress.cc.cluster_size * F2FS_BLKSIZE;
1058 return 0;
1059 }
1060
do_sload(struct f2fs_sb_info * sbi)1061 static int do_sload(struct f2fs_sb_info *sbi)
1062 {
1063 if (!c.from_dir) {
1064 MSG(0, "Info: No source directory, but it's okay.\n");
1065 return 0;
1066 }
1067 if (!c.mount_point)
1068 c.mount_point = "/";
1069
1070 if (init_compr(sbi))
1071 return -1;
1072
1073 return f2fs_sload(sbi);
1074 }
1075 #endif
1076
1077 #ifdef WITH_LABEL
do_label(struct f2fs_sb_info * sbi)1078 static int do_label(struct f2fs_sb_info *sbi)
1079 {
1080 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1081
1082 if (!c.vol_label) {
1083 char label[MAX_VOLUME_NAME];
1084
1085 utf16_to_utf8(label, (const char *)sb->volume_name,
1086 MAX_VOLUME_NAME, MAX_VOLUME_NAME);
1087 MSG(0, "Info: volume label = %s\n", label);
1088 return 0;
1089 }
1090
1091 if (strlen(c.vol_label) > MAX_VOLUME_NAME) {
1092 ERR_MSG("Label should not exceed %d characters\n", MAX_VOLUME_NAME);
1093 return -1;
1094 }
1095
1096 utf8_to_utf16((char *)sb->volume_name, (const char *)c.vol_label,
1097 MAX_VOLUME_NAME, strlen(c.vol_label));
1098
1099 update_superblock(sb, SB_MASK_ALL);
1100
1101 MSG(0, "Info: volume label is changed to %s\n", c.vol_label);
1102
1103 return 0;
1104 }
1105 #endif
1106
1107 #ifdef HAVE_MACH_TIME_H
get_boottime_ns()1108 static u64 get_boottime_ns()
1109 {
1110 return mach_absolute_time();
1111 }
1112 #elif defined(HAVE_CLOCK_GETTIME) && defined(HAVE_CLOCK_BOOTTIME)
get_boottime_ns()1113 static u64 get_boottime_ns()
1114 {
1115 struct timespec t;
1116 t.tv_sec = t.tv_nsec = 0;
1117 clock_gettime(CLOCK_BOOTTIME, &t);
1118 return (u64)t.tv_sec * 1000000000LL + t.tv_nsec;
1119 }
1120 #else
get_boottime_ns()1121 static u64 get_boottime_ns()
1122 {
1123 return 0;
1124 }
1125 #endif
1126
main(int argc,char ** argv)1127 int main(int argc, char **argv)
1128 {
1129 struct f2fs_sb_info *sbi;
1130 int ret = 0, ret2;
1131 u64 start = get_boottime_ns();
1132
1133 f2fs_init_configuration();
1134
1135 f2fs_parse_options(argc, argv);
1136
1137 if (c.func != DUMP && (ret = f2fs_devs_are_umounted()) < 0) {
1138 if (ret == -EBUSY) {
1139 ret = -1;
1140 if (c.func == FSCK)
1141 ret = FSCK_OPERATIONAL_ERROR;
1142 goto quick_err;
1143 }
1144 if (!c.ro || c.func == DEFRAG) {
1145 MSG(0, "\tError: Not available on mounted device!\n");
1146 ret = -1;
1147 if (c.func == FSCK)
1148 ret = FSCK_OPERATIONAL_ERROR;
1149 goto quick_err;
1150 }
1151
1152 /* allow ro-mounted partition */
1153 if (c.force) {
1154 MSG(0, "Info: Force to check/repair FS on RO mounted device\n");
1155 } else {
1156 MSG(0, "Info: Check FS only on RO mounted device\n");
1157 c.fix_on = 0;
1158 c.auto_fix = 0;
1159 }
1160 }
1161
1162 /* Get device */
1163 if (f2fs_get_device_info() < 0 || f2fs_get_f2fs_info() < 0) {
1164 ret = -1;
1165 if (c.func == FSCK)
1166 ret = FSCK_OPERATIONAL_ERROR;
1167 goto quick_err;
1168 }
1169
1170 fsck_again:
1171 memset(&gfsck, 0, sizeof(gfsck));
1172 gfsck.sbi.fsck = &gfsck;
1173 sbi = &gfsck.sbi;
1174
1175 ret = f2fs_do_mount(sbi);
1176 if (ret != 0) {
1177 if (ret == 1) {
1178 MSG(0, "Info: No error was reported\n");
1179 ret = 0;
1180 }
1181 goto out_err;
1182 }
1183
1184 switch (c.func) {
1185 case FSCK:
1186 ret = do_fsck(sbi);
1187 break;
1188 #ifdef WITH_DUMP
1189 case DUMP:
1190 do_dump(sbi);
1191 break;
1192 #endif
1193 #ifdef WITH_DEFRAG
1194 case DEFRAG:
1195 ret = do_defrag(sbi);
1196 if (ret)
1197 goto out_err;
1198 break;
1199 #endif
1200 #ifdef WITH_RESIZE
1201 case RESIZE:
1202 if (do_resize(sbi))
1203 goto out_err;
1204 break;
1205 #endif
1206 #ifdef WITH_SLOAD
1207 case SLOAD:
1208 if (do_sload(sbi))
1209 goto out_err;
1210
1211 ret = f2fs_sparse_initialize_meta(sbi);
1212 if (ret < 0)
1213 goto out_err;
1214
1215 f2fs_do_umount(sbi);
1216
1217 /* fsck to fix missing quota */
1218 c.func = FSCK;
1219 c.fix_on = 1;
1220 goto fsck_again;
1221 #endif
1222 #ifdef WITH_LABEL
1223 case LABEL:
1224 if (do_label(sbi))
1225 goto out_err;
1226 break;
1227 #endif
1228 default:
1229 ERR_MSG("Wrong program name\n");
1230 ASSERT(0);
1231 }
1232
1233 f2fs_do_umount(sbi);
1234
1235 if (c.func == FSCK && c.bug_on) {
1236 if (!c.ro && c.fix_on == 0 && c.auto_fix == 0 && !c.dry_run) {
1237 char ans[255] = {0};
1238 retry:
1239 printf("Do you want to fix this partition? [Y/N] ");
1240 ret2 = scanf("%s", ans);
1241 ASSERT(ret2 >= 0);
1242 if (!strcasecmp(ans, "y"))
1243 c.fix_on = 1;
1244 else if (!strcasecmp(ans, "n"))
1245 c.fix_on = 0;
1246 else
1247 goto retry;
1248
1249 if (c.fix_on)
1250 goto fsck_again;
1251 }
1252 }
1253 ret2 = f2fs_finalize_device();
1254 if (ret2) {
1255 if (c.func == FSCK)
1256 return FSCK_OPERATIONAL_ERROR;
1257 return ret2;
1258 }
1259
1260 if (c.func == SLOAD)
1261 c.compress.filter_ops->destroy();
1262
1263 if (!c.show_file_map)
1264 printf("\nDone: %lf secs\n", (get_boottime_ns() - start) / 1000000000.0);
1265 return ret;
1266
1267 out_err:
1268 if (sbi->ckpt)
1269 free(sbi->ckpt);
1270 if (sbi->raw_super)
1271 free(sbi->raw_super);
1272 quick_err:
1273 f2fs_release_sparse_resource();
1274 return ret;
1275 }
1276