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 #ifdef WITH_SLOAD
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, " -n [NAT dump nid from #1~#2 (decimal), for all 0~-1]\n");
94 MSG(0, " -M show a block map\n");
95 MSG(0, " -s [SIT dump segno from #1~#2 (decimal), for all 0~-1]\n");
96 MSG(0, " -S sparse_mode\n");
97 MSG(0, " -a [SSA dump segno from #1~#2 (decimal), for all 0~-1]\n");
98 MSG(0, " -b blk_addr (in 4KB)\n");
99 MSG(0, " -V print the version number and exit\n");
100
101 exit(1);
102 }
103
defrag_usage()104 void defrag_usage()
105 {
106 MSG(0, "\nUsage: defrag.f2fs [options] device\n");
107 MSG(0, "[options]:\n");
108 MSG(0, " -d debug level [default:0]\n");
109 MSG(0, " -s start block address [default: main_blkaddr]\n");
110 MSG(0, " -S sparse_mode\n");
111 MSG(0, " -l length [default:512 (2MB)]\n");
112 MSG(0, " -t target block address [default: main_blkaddr + 2MB]\n");
113 MSG(0, " -i set direction as shrink [default: expand]\n");
114 MSG(0, " -V print the version number and exit\n");
115 exit(1);
116 }
117
resize_usage()118 void resize_usage()
119 {
120 MSG(0, "\nUsage: resize.f2fs [options] device\n");
121 MSG(0, "[options]:\n");
122 MSG(0, " -d debug level [default:0]\n");
123 MSG(0, " -i extended node bitmap, node ratio is 20%% by default\n");
124 MSG(0, " -s safe resize (Does not resize metadata)");
125 MSG(0, " -t target sectors [default: device size]\n");
126 MSG(0, " -O feature1[,feature2,...] e.g. \"fsprojquota,fscasefold\"\n");
127 MSG(0, " -C [encoding[:flag1,...]] Support casefolding with optional flags\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: Fix the reported corruption.\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 break;
279 case 'l':
280 c.layout = 1;
281 break;
282 case 'M':
283 c.show_file_map = 1;
284 break;
285 case 'O':
286 if (parse_feature(feature_table, optarg))
287 fsck_usage();
288 break;
289 case 'p':
290 /* preen mode has different levels:
291 * 0: default level, the same as -a
292 * 1: check meta
293 * 2: same as 0, but will skip some
294 * check for old kernel
295 */
296 if (optarg[0] == '-' || !is_digits(optarg) ||
297 optind == argc) {
298 MSG(0, "Info: Use default preen mode\n");
299 c.preen_mode = PREEN_MODE_0;
300 c.auto_fix = 1;
301 optind--;
302 break;
303 }
304 c.preen_mode = atoi(optarg);
305 if (c.preen_mode < 0)
306 c.preen_mode = PREEN_MODE_0;
307 else if (c.preen_mode >= PREEN_MODE_MAX)
308 c.preen_mode = PREEN_MODE_MAX - 1;
309 if (c.preen_mode == PREEN_MODE_0 ||
310 c.preen_mode == PREEN_MODE_2)
311 c.auto_fix = 1;
312 MSG(0, "Info: Fix the reported corruption in "
313 "preen mode %d\n", c.preen_mode);
314 break;
315 case 'd':
316 if (optarg[0] == '-') {
317 err = ENEED_ARG;
318 break;
319 } else if (!is_digits(optarg)) {
320 err = EWRONG_OPT;
321 break;
322 }
323 c.dbg_lv = atoi(optarg);
324 MSG(0, "Info: Debug level = %d\n", c.dbg_lv);
325 break;
326 case 'f':
327 case 'y':
328 c.fix_on = 1;
329 c.force = 1;
330 MSG(0, "Info: Force to fix corruption\n");
331 break;
332 case 'q':
333 c.preserve_limits = atoi(optarg);
334 MSG(0, "Info: Preserve quota limits = %d\n",
335 c.preserve_limits);
336 break;
337 case 'S':
338 c.sparse_mode = 1;
339 break;
340 case 't':
341 c.show_dentry = 1;
342 break;
343 case ':':
344 if (optopt == 'p') {
345 MSG(0, "Info: Use default preen mode\n");
346 c.preen_mode = PREEN_MODE_0;
347 c.auto_fix = 1;
348 } else {
349 option = optopt;
350 err = ENEED_ARG;
351 break;
352 }
353 break;
354 case 'C':
355 token = strtok(optarg, ":");
356 val = f2fs_str2encoding(token);
357 if (val < 0) {
358 MSG(0, "\tError: Unknown encoding %s\n", token);
359 fsck_usage();
360 }
361 c.s_encoding = val;
362 token = strtok(NULL, "");
363 val = f2fs_str2encoding_flags(&token, &c.s_encoding_flags);
364 if (val) {
365 MSG(0, "\tError: Unknown flag %s\n", token);
366 fsck_usage();
367 }
368 c.feature |= cpu_to_le32(F2FS_FEATURE_CASEFOLD);
369 break;
370 case 'V':
371 show_version(prog);
372 exit(0);
373 case '?':
374 option = optopt;
375 fallthrough;
376 default:
377 err = EUNKNOWN_OPT;
378 break;
379 }
380 if (err != NOERROR)
381 break;
382 }
383 } else if (!strcmp("dump.f2fs", prog)) {
384 #ifdef WITH_DUMP
385 const char *option_string = "d:i:n:Ms:Sa:b:V";
386 static struct dump_option dump_opt = {
387 .nid = 0, /* default root ino */
388 .start_nat = -1,
389 .end_nat = -1,
390 .start_sit = -1,
391 .end_sit = -1,
392 .start_ssa = -1,
393 .end_ssa = -1,
394 .blk_addr = -1,
395 };
396
397 c.func = DUMP;
398 while ((option = getopt(argc, argv, option_string)) != EOF) {
399 int ret = 0;
400
401 switch (option) {
402 case 'd':
403 if (!is_digits(optarg)) {
404 err = EWRONG_OPT;
405 break;
406 }
407 c.dbg_lv = atoi(optarg);
408 MSG(0, "Info: Debug level = %d\n",
409 c.dbg_lv);
410 break;
411 case 'g':
412 if (!strcmp(optarg, "android")) {
413 c.defset = CONF_ANDROID;
414 MSG(0, "Info: Set conf for android\n");
415 break;
416 }
417 err = EWRONG_OPT;
418 break;
419 case 'i':
420 if (strncmp(optarg, "0x", 2))
421 ret = sscanf(optarg, "%d",
422 &dump_opt.nid);
423 else
424 ret = sscanf(optarg, "%x",
425 &dump_opt.nid);
426 break;
427 case 'n':
428 ret = sscanf(optarg, "%d~%d",
429 &dump_opt.start_nat,
430 &dump_opt.end_nat);
431 break;
432 case 'M':
433 c.show_file_map = 1;
434 break;
435 case 's':
436 ret = sscanf(optarg, "%d~%d",
437 &dump_opt.start_sit,
438 &dump_opt.end_sit);
439 break;
440 case 'S':
441 c.sparse_mode = 1;
442 break;
443 case 'a':
444 ret = sscanf(optarg, "%d~%d",
445 &dump_opt.start_ssa,
446 &dump_opt.end_ssa);
447 break;
448 case 'b':
449 if (strncmp(optarg, "0x", 2))
450 ret = sscanf(optarg, "%d",
451 &dump_opt.blk_addr);
452 else
453 ret = sscanf(optarg, "%x",
454 &dump_opt.blk_addr);
455 break;
456 case 'V':
457 show_version(prog);
458 exit(0);
459 default:
460 err = EUNKNOWN_OPT;
461 break;
462 }
463 ASSERT(ret >= 0);
464 if (err != NOERROR)
465 break;
466 }
467
468 c.private = &dump_opt;
469 #endif
470 } else if (!strcmp("defrag.f2fs", prog)) {
471 #ifdef WITH_DEFRAG
472 const char *option_string = "d:s:Sl:t:iV";
473
474 c.func = DEFRAG;
475 while ((option = getopt(argc, argv, option_string)) != EOF) {
476 int ret = 0;
477
478 switch (option) {
479 case 'd':
480 if (!is_digits(optarg)) {
481 err = EWRONG_OPT;
482 break;
483 }
484 c.dbg_lv = atoi(optarg);
485 MSG(0, "Info: Debug level = %d\n",
486 c.dbg_lv);
487 break;
488 case 's':
489 if (strncmp(optarg, "0x", 2))
490 ret = sscanf(optarg, "%"PRIu64"",
491 &c.defrag_start);
492 else
493 ret = sscanf(optarg, "%"PRIx64"",
494 &c.defrag_start);
495 break;
496 case 'S':
497 c.sparse_mode = 1;
498 break;
499 case 'l':
500 if (strncmp(optarg, "0x", 2))
501 ret = sscanf(optarg, "%"PRIu64"",
502 &c.defrag_len);
503 else
504 ret = sscanf(optarg, "%"PRIx64"",
505 &c.defrag_len);
506 break;
507 case 't':
508 if (strncmp(optarg, "0x", 2))
509 ret = sscanf(optarg, "%"PRIu64"",
510 &c.defrag_target);
511 else
512 ret = sscanf(optarg, "%"PRIx64"",
513 &c.defrag_target);
514 break;
515 case 'i':
516 c.defrag_shrink = 1;
517 break;
518 case 'V':
519 show_version(prog);
520 exit(0);
521 default:
522 err = EUNKNOWN_OPT;
523 break;
524 }
525 ASSERT(ret >= 0);
526 if (err != NOERROR)
527 break;
528 }
529 #endif
530 } else if (!strcmp("resize.f2fs", prog)) {
531 #ifdef WITH_RESIZE
532 const char *option_string = "d:fst:O:C:iV";
533 int val;
534 char *token;
535
536 c.func = RESIZE;
537 while ((option = getopt(argc, argv, option_string)) != EOF) {
538 int ret = 0;
539
540 switch (option) {
541 case 'd':
542 if (!is_digits(optarg)) {
543 err = EWRONG_OPT;
544 break;
545 }
546 c.dbg_lv = atoi(optarg);
547 MSG(0, "Info: Debug level = %d\n",
548 c.dbg_lv);
549 break;
550 case 'f':
551 c.force = 1;
552 MSG(0, "Info: Force to resize\n");
553 break;
554 case 's':
555 c.safe_resize = 1;
556 break;
557 case 't':
558 if (strncmp(optarg, "0x", 2))
559 ret = sscanf(optarg, "%"PRIu64"",
560 &c.target_sectors);
561 else
562 ret = sscanf(optarg, "%"PRIx64"",
563 &c.target_sectors);
564 break;
565 case 'i':
566 c.large_nat_bitmap = 1;
567 break;
568 case 'O':
569 if (parse_feature(feature_table, optarg))
570 resize_usage();
571 break;
572 case 'C':
573 token = strtok(optarg, ":");
574 val = f2fs_str2encoding(token);
575 if (val < 0) {
576 MSG(0, "\tError: Unknown encoding %s\n", token);
577 }
578 c.s_encoding = val;
579 token = strtok(NULL, "");
580 val = f2fs_str2encoding_flags(&token, &c.s_encoding_flags);
581 if (val) {
582 MSG(0, "\tError: Unknown flag %s\n",token);
583 }
584 c.feature |= cpu_to_le32(F2FS_FEATURE_CASEFOLD);
585 break;
586 case 'V':
587 show_version(prog);
588 exit(0);
589 default:
590 err = EUNKNOWN_OPT;
591 break;
592 }
593 ASSERT(ret >= 0);
594 if (err != NOERROR)
595 break;
596 }
597 #endif
598 } else if (!strcmp("sload.f2fs", prog)) {
599 #ifdef WITH_SLOAD
600 const char *option_string = "cL:a:i:x:m:rC:d:f:p:s:St:T:VP";
601 #ifdef HAVE_LIBSELINUX
602 int max_nr_opt = (int)sizeof(c.seopt_file) /
603 sizeof(c.seopt_file[0]);
604 char *token;
605 #endif
606 char *p;
607
608 c.func = SLOAD;
609 c.compress.cc.log_cluster_size = 2;
610 c.compress.alg = COMPR_LZ4;
611 c.compress.min_blocks = 1;
612 c.compress.filter_ops = &ext_filter;
613 while ((option = getopt(argc, argv, option_string)) != EOF) {
614 unsigned int i;
615 int val;
616
617 switch (option) {
618 case 'c': /* compression support */
619 c.compress.enabled = true;
620 break;
621 case 'L': /* compression: log of blocks-per-cluster */
622 c.compress.required = true;
623 val = atoi(optarg);
624 if (val < MIN_COMPRESS_LOG_SIZE ||
625 val > MAX_COMPRESS_LOG_SIZE) {
626 MSG(0, "\tError: log of blocks per"
627 " cluster must be in the range"
628 " of %d .. %d.\n",
629 MIN_COMPRESS_LOG_SIZE,
630 MAX_COMPRESS_LOG_SIZE);
631 error_out(prog);
632 }
633 c.compress.cc.log_cluster_size = val;
634 break;
635 case 'a': /* compression: choose algorithm */
636 c.compress.required = true;
637 c.compress.alg = MAX_COMPRESS_ALGS;
638 for (i = 0; i < MAX_COMPRESS_ALGS; i++) {
639 if (!strcmp(supported_comp_names[i],
640 optarg)) {
641 c.compress.alg = i;
642 break;
643 }
644 }
645 if (c.compress.alg == MAX_COMPRESS_ALGS) {
646 MSG(0, "\tError: Unknown compression"
647 " algorithm %s\n", optarg);
648 error_out(prog);
649 }
650 break;
651 case 'i': /* compress only these extensions */
652 c.compress.required = true;
653 if (c.compress.filter == COMPR_FILTER_ALLOW) {
654 MSG(0, "\tError: could not mix option"
655 " -i and -x\n");
656 error_out(prog);
657 }
658 c.compress.filter = COMPR_FILTER_DENY;
659 c.compress.filter_ops->add(optarg);
660 break;
661 case 'x': /* compress except for these extensions */
662 c.compress.required = true;
663 if (c.compress.filter == COMPR_FILTER_DENY) {
664 MSG(0, "\tError: could not mix option"
665 " -i and -x\n");
666 error_out(prog);
667 }
668 c.compress.filter = COMPR_FILTER_ALLOW;
669 c.compress.filter_ops->add(optarg);
670 break;
671 case 'm': /* minimum compressed blocks per cluster */
672 c.compress.required = true;
673 val = atoi(optarg);
674 if (val <= 0) {
675 MSG(0, "\tError: minimum compressed"
676 " blocks per cluster must be"
677 " positive.\n");
678 error_out(prog);
679 }
680 c.compress.min_blocks = val;
681 break;
682 case 'r': /* for setting FI_COMPRESS_RELEASED */
683 c.compress.required = true;
684 c.compress.readonly = true;
685 break;
686 case 'C':
687 c.fs_config_file = absolute_path(optarg);
688 break;
689 case 'd':
690 if (!is_digits(optarg)) {
691 err = EWRONG_OPT;
692 break;
693 }
694 c.dbg_lv = atoi(optarg);
695 MSG(0, "Info: Debug level = %d\n",
696 c.dbg_lv);
697 break;
698 case 'f':
699 c.from_dir = absolute_path(optarg);
700 break;
701 case 'p':
702 c.target_out_dir = absolute_path(optarg);
703 break;
704 case 's':
705 #ifdef HAVE_LIBSELINUX
706 token = strtok(optarg, ",");
707 while (token) {
708 if (c.nr_opt == max_nr_opt) {
709 MSG(0, "\tError: Expected at most %d selinux opts\n",
710 max_nr_opt);
711 error_out(prog);
712 }
713 c.seopt_file[c.nr_opt].type =
714 SELABEL_OPT_PATH;
715 c.seopt_file[c.nr_opt].value =
716 absolute_path(token);
717 c.nr_opt++;
718 token = strtok(NULL, ",");
719 }
720 #else
721 MSG(0, "Info: Not support selinux opts\n");
722 #endif
723 break;
724 case 'S':
725 c.sparse_mode = 1;
726 break;
727 case 't':
728 c.mount_point = (char *)optarg;
729 break;
730 case 'T':
731 c.fixed_time = strtoul(optarg, &p, 0);
732 break;
733 case 'V':
734 show_version(prog);
735 exit(0);
736 case 'P':
737 c.preserve_perms = 1;
738 break;
739 default:
740 err = EUNKNOWN_OPT;
741 break;
742 }
743 if (err != NOERROR)
744 break;
745 }
746 if (c.compress.required && !c.compress.enabled) {
747 MSG(0, "\tError: compression sub-options are used"
748 " without the compression enable (-c) option\n"
749 );
750 error_out(prog);
751 }
752 if (err == NOERROR && c.compress.enabled) {
753 c.compress.cc.cluster_size = 1
754 << c.compress.cc.log_cluster_size;
755 if (c.compress.filter == COMPR_FILTER_UNASSIGNED)
756 c.compress.filter = COMPR_FILTER_ALLOW;
757 if (c.compress.min_blocks >=
758 c.compress.cc.cluster_size) {
759 MSG(0, "\tError: minimum reduced blocks by"
760 " compression per cluster must be at"
761 " most one less than blocks per"
762 " cluster, i.e. %d\n",
763 c.compress.cc.cluster_size - 1);
764 error_out(prog);
765 }
766 }
767 #endif /* WITH_SLOAD */
768 } else if (!strcmp("f2fslabel", prog)) {
769 #ifdef WITH_LABEL
770 const char *option_string = "V";
771
772 c.func = LABEL;
773 while ((option = getopt(argc, argv, option_string)) != EOF) {
774 switch (option) {
775 case 'V':
776 show_version(prog);
777 exit(0);
778 default:
779 err = EUNKNOWN_OPT;
780 break;
781 }
782 if (err != NOERROR)
783 break;
784 }
785
786 if (argc > (optind + 2)) { /* unknown argument(s) is(are) passed */
787 optind += 2;
788 err = EUNKNOWN_ARG;
789 } else if (argc == (optind + 2)) { /* change label */
790 c.vol_label = argv[optind + 1];
791 argc--;
792 } else { /* print label */
793 /*
794 * Since vol_label was initialized as "", in order to
795 * distinguish between clear label and print, set
796 * vol_label as NULL for print case
797 */
798 c.vol_label = NULL;
799 }
800 #endif /* WITH_LABEL */
801 }
802
803 if (err == NOERROR) {
804 add_default_options();
805
806 if (optind >= argc) {
807 MSG(0, "\tError: Device not specified\n");
808 error_out(prog);
809 }
810
811 c.devices[0].path = strdup(argv[optind]);
812 if (argc > (optind + 1)) {
813 c.dbg_lv = 0;
814 err = EUNKNOWN_ARG;
815 }
816 if (err == NOERROR)
817 return;
818 }
819
820 /* print out error */
821 switch (err) {
822 case EWRONG_OPT:
823 MSG(0, "\tError: Wrong option -%c %s\n", option, optarg);
824 break;
825 case ENEED_ARG:
826 MSG(0, "\tError: Need argument for -%c\n", option);
827 break;
828 case EUNKNOWN_OPT:
829 MSG(0, "\tError: Unknown option %c\n", option);
830 break;
831 case EUNKNOWN_ARG:
832 MSG(0, "\tError: Unknown argument %s\n", argv[optind]);
833 break;
834 }
835 error_out(prog);
836 }
837
do_fsck(struct f2fs_sb_info * sbi)838 static int do_fsck(struct f2fs_sb_info *sbi)
839 {
840 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
841 u32 flag = le32_to_cpu(ckpt->ckpt_flags);
842 u32 blk_cnt;
843 struct f2fs_compr_blk_cnt cbc;
844 errcode_t ret;
845
846 fsck_init(sbi);
847
848 print_cp_state(flag);
849
850 fsck_chk_and_fix_write_pointers(sbi);
851
852 fsck_chk_curseg_info(sbi);
853
854 if (!c.fix_on && !c.bug_on) {
855 switch (c.preen_mode) {
856 case PREEN_MODE_1:
857 if (fsck_chk_meta(sbi)) {
858 MSG(0, "[FSCK] F2FS metadata [Fail]");
859 MSG(0, "\tError: meta does not match, "
860 "force check all\n");
861 } else {
862 MSG(0, "[FSCK] F2FS metadata [Ok..]");
863 fsck_free(sbi);
864 return FSCK_SUCCESS;
865 }
866
867 if (!c.ro)
868 c.fix_on = 1;
869 break;
870 }
871 } else if (c.preen_mode) {
872 /*
873 * we can hit this in 3 situations:
874 * 1. fsck -f, fix_on has already been set to 1 when
875 * parsing options;
876 * 2. fsck -a && CP_FSCK_FLAG is set, fix_on has already
877 * been set to 1 when checking CP_FSCK_FLAG;
878 * 3. fsck -p 1 && error is detected, then bug_on is set,
879 * we set fix_on = 1 here, so that fsck can fix errors
880 * automatically
881 */
882 c.fix_on = 1;
883 }
884
885 fsck_chk_checkpoint(sbi);
886
887 fsck_chk_quota_node(sbi);
888
889 /* Traverse all block recursively from root inode */
890 blk_cnt = 1;
891 cbc.cnt = 0;
892 cbc.cheader_pgofs = CHEADER_PGOFS_NONE;
893
894 if (c.feature & cpu_to_le32(F2FS_FEATURE_QUOTA_INO)) {
895 ret = quota_init_context(sbi);
896 if (ret) {
897 ASSERT_MSG("quota_init_context failure: %d", ret);
898 return FSCK_OPERATIONAL_ERROR;
899 }
900 }
901 fsck_chk_orphan_node(sbi);
902 fsck_chk_node_blk(sbi, NULL, sbi->root_ino_num,
903 F2FS_FT_DIR, TYPE_INODE, &blk_cnt, &cbc, NULL);
904 fsck_chk_quota_files(sbi);
905
906 ret = fsck_verify(sbi);
907 fsck_free(sbi);
908
909 if (!c.bug_on)
910 return FSCK_SUCCESS;
911 if (!ret)
912 return FSCK_ERROR_CORRECTED;
913 return FSCK_ERRORS_LEFT_UNCORRECTED;
914 }
915
916 #ifdef WITH_DUMP
do_dump(struct f2fs_sb_info * sbi)917 static void do_dump(struct f2fs_sb_info *sbi)
918 {
919 struct dump_option *opt = (struct dump_option *)c.private;
920 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
921 u32 flag = le32_to_cpu(ckpt->ckpt_flags);
922
923 if (opt->end_nat == -1)
924 opt->end_nat = NM_I(sbi)->max_nid;
925 if (opt->end_sit == -1)
926 opt->end_sit = SM_I(sbi)->main_segments;
927 if (opt->end_ssa == -1)
928 opt->end_ssa = SM_I(sbi)->main_segments;
929 if (opt->start_nat != -1)
930 nat_dump(sbi, opt->start_nat, opt->end_nat);
931 if (opt->start_sit != -1)
932 sit_dump(sbi, opt->start_sit, opt->end_sit);
933 if (opt->start_ssa != -1)
934 ssa_dump(sbi, opt->start_ssa, opt->end_ssa);
935 if (opt->blk_addr != -1)
936 dump_info_from_blkaddr(sbi, opt->blk_addr);
937 if (opt->nid)
938 dump_node(sbi, opt->nid, 0);
939
940 print_cp_state(flag);
941
942 }
943 #endif
944
945 #ifdef WITH_DEFRAG
do_defrag(struct f2fs_sb_info * sbi)946 static int do_defrag(struct f2fs_sb_info *sbi)
947 {
948 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
949
950 if (get_sb(feature) & cpu_to_le32(F2FS_FEATURE_RO)) {
951 MSG(0, "Not support on readonly image.\n");
952 return -1;
953 }
954
955 if (c.defrag_start > get_sb(block_count))
956 goto out_range;
957 if (c.defrag_start < SM_I(sbi)->main_blkaddr)
958 c.defrag_start = SM_I(sbi)->main_blkaddr;
959
960 if (c.defrag_len == 0)
961 c.defrag_len = sbi->blocks_per_seg;
962
963 if (c.defrag_start + c.defrag_len > get_sb(block_count))
964 c.defrag_len = get_sb(block_count) - c.defrag_start;
965
966 if (c.defrag_target == 0) {
967 c.defrag_target = c.defrag_start - 1;
968 if (!c.defrag_shrink)
969 c.defrag_target += c.defrag_len + 1;
970 }
971
972 if (c.defrag_target < SM_I(sbi)->main_blkaddr ||
973 c.defrag_target > get_sb(block_count))
974 goto out_range;
975 if (c.defrag_target >= c.defrag_start &&
976 c.defrag_target < c.defrag_start + c.defrag_len)
977 goto out_range;
978
979 if (c.defrag_start > c.defrag_target)
980 MSG(0, "Info: Move 0x%"PRIx64" <- [0x%"PRIx64"-0x%"PRIx64"]\n",
981 c.defrag_target,
982 c.defrag_start,
983 c.defrag_start + c.defrag_len - 1);
984 else
985 MSG(0, "Info: Move [0x%"PRIx64"-0x%"PRIx64"] -> 0x%"PRIx64"\n",
986 c.defrag_start,
987 c.defrag_start + c.defrag_len - 1,
988 c.defrag_target);
989
990 return f2fs_defragment(sbi, c.defrag_start, c.defrag_len,
991 c.defrag_target, c.defrag_shrink);
992 out_range:
993 ASSERT_MSG("Out-of-range [0x%"PRIx64" ~ 0x%"PRIx64"] to 0x%"PRIx64"",
994 c.defrag_start,
995 c.defrag_start + c.defrag_len - 1,
996 c.defrag_target);
997 return -1;
998 }
999 #endif
1000
1001 #ifdef WITH_RESIZE
do_resize(struct f2fs_sb_info * sbi)1002 static int do_resize(struct f2fs_sb_info *sbi)
1003 {
1004 if (!c.target_sectors)
1005 c.target_sectors = c.total_sectors;
1006
1007 if (c.target_sectors > c.total_sectors) {
1008 ASSERT_MSG("Out-of-range Target=0x%"PRIx64" / 0x%"PRIx64"",
1009 c.target_sectors, c.total_sectors);
1010 return -1;
1011 }
1012
1013 return f2fs_resize(sbi);
1014 }
1015 #endif
1016
1017 #ifdef WITH_SLOAD
init_compr(struct f2fs_sb_info * sbi)1018 static int init_compr(struct f2fs_sb_info *sbi)
1019 {
1020 if (!c.compress.enabled)
1021 return 0;
1022
1023 if (!(sbi->raw_super->feature
1024 & cpu_to_le32(F2FS_FEATURE_COMPRESSION))) {
1025 MSG(0, "Error: Compression (-c) was requested "
1026 "but the file system is not created "
1027 "with such feature.\n");
1028 return -1;
1029 }
1030 if (!supported_comp_ops[c.compress.alg].init) {
1031 MSG(0, "Error: The selected compression algorithm is not"
1032 " supported\n");
1033 return -1;
1034 }
1035 c.compress.ops = supported_comp_ops + c.compress.alg;
1036 c.compress.ops->init(&c.compress.cc);
1037 c.compress.ops->reset(&c.compress.cc);
1038 c.compress.cc.rlen = c.compress.cc.cluster_size * F2FS_BLKSIZE;
1039 return 0;
1040 }
1041
do_sload(struct f2fs_sb_info * sbi)1042 static int do_sload(struct f2fs_sb_info *sbi)
1043 {
1044 if (!c.from_dir) {
1045 MSG(0, "Info: No source directory, but it's okay.\n");
1046 return 0;
1047 }
1048 if (!c.mount_point)
1049 c.mount_point = "/";
1050
1051 if (init_compr(sbi))
1052 return -1;
1053
1054 return f2fs_sload(sbi);
1055 }
1056 #endif
1057
1058 #ifdef WITH_LABEL
do_label(struct f2fs_sb_info * sbi)1059 static int do_label(struct f2fs_sb_info *sbi)
1060 {
1061 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1062
1063 if (!c.vol_label) {
1064 char label[MAX_VOLUME_NAME];
1065
1066 utf16_to_utf8(label, sb->volume_name,
1067 MAX_VOLUME_NAME, MAX_VOLUME_NAME);
1068 MSG(0, "Info: volume label = %s\n", label);
1069 return 0;
1070 }
1071
1072 if (strlen(c.vol_label) > MAX_VOLUME_NAME) {
1073 ERR_MSG("Label should not exceed %d characters\n", MAX_VOLUME_NAME);
1074 return -1;
1075 }
1076
1077 utf8_to_utf16(sb->volume_name, (const char *)c.vol_label,
1078 MAX_VOLUME_NAME, strlen(c.vol_label));
1079
1080 update_superblock(sb, SB_MASK_ALL);
1081
1082 MSG(0, "Info: volume label is changed to %s\n", c.vol_label);
1083
1084 return 0;
1085 }
1086 #endif
1087
1088 #ifdef HAVE_MACH_TIME_H
get_boottime_ns()1089 static u64 get_boottime_ns()
1090 {
1091 return mach_absolute_time();
1092 }
1093 #elif defined(HAVE_CLOCK_GETTIME) && defined(HAVE_CLOCK_BOOTTIME)
get_boottime_ns()1094 static u64 get_boottime_ns()
1095 {
1096 struct timespec t;
1097 t.tv_sec = t.tv_nsec = 0;
1098 clock_gettime(CLOCK_BOOTTIME, &t);
1099 return (u64)t.tv_sec * 1000000000LL + t.tv_nsec;
1100 }
1101 #else
get_boottime_ns()1102 static u64 get_boottime_ns()
1103 {
1104 return 0;
1105 }
1106 #endif
1107
main(int argc,char ** argv)1108 int main(int argc, char **argv)
1109 {
1110 struct f2fs_sb_info *sbi;
1111 int ret = 0, ret2;
1112 u64 start = get_boottime_ns();
1113
1114 f2fs_init_configuration();
1115
1116 f2fs_parse_options(argc, argv);
1117
1118 if (c.func != DUMP && f2fs_devs_are_umounted() < 0) {
1119 if (errno == EBUSY) {
1120 ret = -1;
1121 if (c.func == FSCK)
1122 ret = FSCK_OPERATIONAL_ERROR;
1123 goto quick_err;
1124 }
1125 if (!c.ro || c.func == DEFRAG) {
1126 MSG(0, "\tError: Not available on mounted device!\n");
1127 ret = -1;
1128 if (c.func == FSCK)
1129 ret = FSCK_OPERATIONAL_ERROR;
1130 goto quick_err;
1131 }
1132
1133 /* allow ro-mounted partition */
1134 if (c.force) {
1135 MSG(0, "Info: Force to check/repair FS on RO mounted device\n");
1136 } else {
1137 MSG(0, "Info: Check FS only on RO mounted device\n");
1138 c.fix_on = 0;
1139 c.auto_fix = 0;
1140 }
1141 }
1142
1143 /* Get device */
1144 if (f2fs_get_device_info() < 0 || f2fs_get_f2fs_info() < 0) {
1145 ret = -1;
1146 if (c.func == FSCK)
1147 ret = FSCK_OPERATIONAL_ERROR;
1148 goto quick_err;
1149 }
1150
1151 fsck_again:
1152 memset(&gfsck, 0, sizeof(gfsck));
1153 gfsck.sbi.fsck = &gfsck;
1154 sbi = &gfsck.sbi;
1155
1156 ret = f2fs_do_mount(sbi);
1157 if (ret != 0) {
1158 if (ret == 1) {
1159 MSG(0, "Info: No error was reported\n");
1160 ret = 0;
1161 }
1162 goto out_err;
1163 }
1164
1165 switch (c.func) {
1166 case FSCK:
1167 ret = do_fsck(sbi);
1168 break;
1169 #ifdef WITH_DUMP
1170 case DUMP:
1171 do_dump(sbi);
1172 break;
1173 #endif
1174 #ifdef WITH_DEFRAG
1175 case DEFRAG:
1176 ret = do_defrag(sbi);
1177 if (ret)
1178 goto out_err;
1179 break;
1180 #endif
1181 #ifdef WITH_RESIZE
1182 case RESIZE:
1183 if (do_resize(sbi))
1184 goto out_err;
1185 break;
1186 #endif
1187 #ifdef WITH_SLOAD
1188 case SLOAD:
1189 if (do_sload(sbi))
1190 goto out_err;
1191
1192 ret = f2fs_sparse_initialize_meta(sbi);
1193 if (ret < 0)
1194 goto out_err;
1195
1196 f2fs_do_umount(sbi);
1197
1198 /* fsck to fix missing quota */
1199 c.func = FSCK;
1200 c.fix_on = 1;
1201 goto fsck_again;
1202 #endif
1203 #ifdef WITH_LABEL
1204 case LABEL:
1205 if (do_label(sbi))
1206 goto out_err;
1207 break;
1208 #endif
1209 default:
1210 ERR_MSG("Wrong program name\n");
1211 ASSERT(0);
1212 }
1213
1214 f2fs_do_umount(sbi);
1215
1216 if (c.func == FSCK && c.bug_on) {
1217 if (!c.ro && c.fix_on == 0 && c.auto_fix == 0 && !c.dry_run) {
1218 char ans[255] = {0};
1219 retry:
1220 printf("Do you want to fix this partition? [Y/N] ");
1221 ret2 = scanf("%s", ans);
1222 ASSERT(ret2 >= 0);
1223 if (!strcasecmp(ans, "y"))
1224 c.fix_on = 1;
1225 else if (!strcasecmp(ans, "n"))
1226 c.fix_on = 0;
1227 else
1228 goto retry;
1229
1230 if (c.fix_on)
1231 goto fsck_again;
1232 }
1233 }
1234 ret2 = f2fs_finalize_device();
1235 if (ret2) {
1236 if (c.func == FSCK)
1237 return FSCK_OPERATIONAL_ERROR;
1238 return ret2;
1239 }
1240
1241 if (c.func == SLOAD)
1242 c.compress.filter_ops->destroy();
1243
1244 if (!c.show_file_map)
1245 printf("\nDone: %lf secs\n", (get_boottime_ns() - start) / 1000000000.0);
1246 return ret;
1247
1248 out_err:
1249 if (sbi->ckpt)
1250 free(sbi->ckpt);
1251 if (sbi->raw_super)
1252 free(sbi->raw_super);
1253 quick_err:
1254 f2fs_release_sparse_resource();
1255 return ret;
1256 }
1257