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