aboutsummaryrefslogtreecommitdiffstats
path: root/fsck/main.c
blob: eda399cf0679a1c42651f0e04acdad367922dc32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
/**
 * main.c
 *
 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
 *             http://www.samsung.com/
 * Copyright (c) 2015 Jaegeuk Kim <jaegeuk@kernel.org>
 *  : implement defrag.f2fs
 * Copyright (C) 2015 Huawei Ltd.
 *   Hou Pengyang <houpengyang@huawei.com>
 *   Liu Shuoran <liushuoran@huawei.com>
 *   Jaegeuk Kim <jaegeuk@kernel.org>
 *  : add sload.f2fs
 * Copyright (c) 2019 Google Inc.
 *   Robin Hsu <robinhsu@google.com>
 *  : add cache layer
 * Copyright (c) 2020 Google Inc.
 *   Robin Hsu <robinhsu@google.com>
 *  : add sload compression support
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include "fsck.h"
#include <libgen.h>
#include <ctype.h>
#include <time.h>
#include <getopt.h>
#include <stdbool.h>
#include "quotaio.h"
#include "compress.h"

struct f2fs_fsck gfsck;

#ifdef WITH_ANDROID
#include <sparse/sparse.h>
extern struct sparse_file *f2fs_sparse_file;
#endif

INIT_FEATURE_TABLE;

static char *absolute_path(const char *file)
{
	char *ret;
	char cwd[PATH_MAX];

	if (file[0] != '/') {
		if (getcwd(cwd, PATH_MAX) == NULL) {
			fprintf(stderr, "Failed to getcwd\n");
			exit(EXIT_FAILURE);
		}
		ret = malloc(strlen(cwd) + 1 + strlen(file) + 1);
		if (ret)
			sprintf(ret, "%s/%s", cwd, file);
	} else
		ret = strdup(file);
	return ret;
}

void fsck_usage()
{
	MSG(0, "\nUsage: fsck.f2fs [options] device\n");
	MSG(0, "[options]:\n");
	MSG(0, "  -a check/fix potential corruption, reported by f2fs\n");
	MSG(0, "  -c <num-cache-entry>  set number of cache entries"
			" (default 0)\n");
	MSG(0, "  -m <max-hash-collision>  set max cache hash collision"
			" (default 16)\n");
	MSG(0, "  -C encoding[:flag1,flag2] Set options for enabling"
			" casefolding\n");
	MSG(0, "  -d debug level [default:0]\n");
	MSG(0, "  -f check/fix entire partition\n");
	MSG(0, "  -g add default options\n");
	MSG(0, "  -l show superblock/checkpoint\n");
	MSG(0, "  -M show a file map\n");
	MSG(0, "  -O feature1[feature2,feature3,...] e.g. \"encrypt\"\n");
	MSG(0, "  -p preen mode [default:0 the same as -a [0|1|2]]\n");
	MSG(0, "  -S sparse_mode\n");
	MSG(0, "  -t show directory tree\n");
	MSG(0, "  -q preserve quota limits\n");
	MSG(0, "  -y fix all the time\n");
	MSG(0, "  -V print the version number and exit\n");
	MSG(0, "  --dry-run do not really fix corruptions\n");
	MSG(0, "  --no-kernel-check skips detecting kernel change\n");
	MSG(0, "  --kernel-check checks kernel change\n");
	MSG(0, "  --debug-cache to debug cache when -c is used\n");
	exit(1);
}

void dump_usage()
{
	MSG(0, "\nUsage: dump.f2fs [options] device\n");
	MSG(0, "[options]:\n");
	MSG(0, "  -d debug level [default:0]\n");
	MSG(0, "  -i inode no (hex)\n");
	MSG(0, "  -n [NAT dump nid from #1~#2 (decimal), for all 0~-1]\n");
	MSG(0, "  -M show a block map\n");
	MSG(0, "  -s [SIT dump segno from #1~#2 (decimal), for all 0~-1]\n");
	MSG(0, "  -S sparse_mode\n");
	MSG(0, "  -a [SSA dump segno from #1~#2 (decimal), for all 0~-1]\n");
	MSG(0, "  -b blk_addr (in 4KB)\n");
	MSG(0, "  -V print the version number and exit\n");

	exit(1);
}

void defrag_usage()
{
	MSG(0, "\nUsage: defrag.f2fs [options] device\n");
	MSG(0, "[options]:\n");
	MSG(0, "  -d debug level [default:0]\n");
	MSG(0, "  -s start block address [default: main_blkaddr]\n");
	MSG(0, "  -S sparse_mode\n");
	MSG(0, "  -l length [default:512 (2MB)]\n");
	MSG(0, "  -t target block address [default: main_blkaddr + 2MB]\n");
	MSG(0, "  -i set direction as shrink [default: expand]\n");
	MSG(0, "  -V print the version number and exit\n");
	exit(1);
}

void resize_usage()
{
	MSG(0, "\nUsage: resize.f2fs [options] device\n");
	MSG(0, "[options]:\n");
	MSG(0, "  -d debug level [default:0]\n");
	MSG(0, "  -i extended node bitmap, node ratio is 20%% by default\n");
	MSG(0, "  -s safe resize (Does not resize metadata)");
	MSG(0, "  -t target sectors [default: device size]\n");
	MSG(0, "  -V print the version number and exit\n");
	exit(1);
}

void sload_usage()
{
	MSG(0, "\nUsage: sload.f2fs [options] device\n");
	MSG(0, "[options]:\n");
	MSG(0, "  -C fs_config\n");
	MSG(0, "  -f source directory [path of the source directory]\n");
	MSG(0, "  -p product out directory\n");
	MSG(0, "  -s file_contexts\n");
	MSG(0, "  -S sparse_mode\n");
	MSG(0, "  -t mount point [prefix of target fs path, default:/]\n");
	MSG(0, "  -T timestamp\n");
	MSG(0, "  -P preserve owner: user and group\n");
	MSG(0, "  -c enable compression (default allow policy)\n");
	MSG(0, "    ------------ Compression sub-options -----------------\n");
	MSG(0, "    -L <log-of-blocks-per-cluster>, default 2\n");
	MSG(0, "    -a <algorithm> compression algorithm, default LZ4\n");
	MSG(0, "    -x <ext> compress files except for these extensions.\n");
	MSG(0, "    -i <ext> compress files with these extensions only.\n");
	MSG(0, "    * -i or -x: use it many times for multiple extensions.\n");
	MSG(0, "    * -i and -x cannot be used together..\n");
	MSG(0, "    -m <num> min compressed blocks per cluster\n");
	MSG(0, "    -r read only (to release unused blocks) for compressed "
			"files\n");
	MSG(0, "    ------------------------------------------------------\n");
	MSG(0, "  -d debug level [default:0]\n");
	MSG(0, "  -V print the version number and exit\n");
	exit(1);
}

void label_usage()
{
	MSG(0, "\nUsage: f2fslabel [options] device [volume-label]\n");
	MSG(0, "[options]:\n");
	MSG(0, "  -V print the version number and exit\n");
	exit(1);
}

static int is_digits(char *optarg)
{
	unsigned int i;

	for (i = 0; i < strlen(optarg); i++)
		if (!isdigit(optarg[i]))
			break;
	return i == strlen(optarg);
}

static void error_out(char *prog)
{
	if (!strcmp("fsck.f2fs", prog))
		fsck_usage();
	else if (!strcmp("dump.f2fs", prog))
		dump_usage();
	else if (!strcmp("defrag.f2fs", prog))
		defrag_usage();
	else if (!strcmp("resize.f2fs", prog))
		resize_usage();
	else if (!strcmp("sload.f2fs", prog))
		sload_usage();
	else if (!strcmp("f2fslabel", prog))
		label_usage();
	else
		MSG(0, "\nWrong program.\n");
}

static void __add_fsck_options(void)
{
	/* -a */
	c.auto_fix = 1;
}

static void add_default_options(void)
{
	switch (c.defset) {
	case CONF_ANDROID:
		__add_fsck_options();
	}
	c.quota_fix = 1;
}

void f2fs_parse_options(int argc, char *argv[])
{
	int option = 0;
	char *prog = basename(argv[0]);
	int err = NOERROR;
#ifdef WITH_ANDROID
	int i;

	/* Allow prog names (e.g, sload_f2fs, fsck_f2fs, etc) */
	for (i = 0; i < strlen(prog); i++) {
		if (prog[i] == '_')
			prog[i] = '.';
	}
#endif
	if (argc < 2) {
		MSG(0, "\tError: Device not specified\n");
		error_out(prog);
	}

	if (!strcmp("fsck.f2fs", prog)) {
		const char *option_string = ":aC:c:m:Md:fg:lO:p:q:StyV";
		int opt = 0, val;
		char *token;
		struct option long_opt[] = {
			{"dry-run", no_argument, 0, 1},
			{"no-kernel-check", no_argument, 0, 2},
			{"kernel-check", no_argument, 0, 3},
			{"debug-cache", no_argument, 0, 4},
			{0, 0, 0, 0}
		};

		c.func = FSCK;
		c.cache_config.max_hash_collision = 16;
		c.cache_config.dbg_en = false;
		while ((option = getopt_long(argc, argv, option_string,
						long_opt, &opt)) != EOF) {
			switch (option) {
			case 1:
				c.dry_run = 1;
				MSG(0, "Info: Dry run\n");
				break;
			case 2:
				c.no_kernel_check = 1;
				MSG(0, "Info: No Kernel Check\n");
				break;
			case 3:
				c.no_kernel_check = 0;
				MSG(0, "Info: Do Kernel Check\n");
				break;
			case 4:
				c.cache_config.dbg_en = true;
				break;
			case 'a':
				c.auto_fix = 1;
				MSG(0, "Info: Fix the reported corruption.\n");
				break;
			case 'c':
				c.cache_config.num_cache_entry = atoi(optarg);
				break;
			case 'm':
				c.cache_config.max_hash_collision =
						atoi(optarg);
				break;
			case 'g':
				if (!strcmp(optarg, "android"))
					c.defset = CONF_ANDROID;
				break;
			case 'l':
				c.layout = 1;
				break;
			case 'M':
				c.show_file_map = 1;
				break;
			case 'O':
				if (parse_feature(feature_table, optarg))
					fsck_usage();
				break;
			case 'p':
				/* preen mode has different levels:
				 *  0: default level, the same as -a
				 *  1: check meta
				 *  2: same as 0, but will skip some
				 *     check for old kernel
				 */
				if (optarg[0] == '-' || !is_digits(optarg) ||
							optind == argc) {
					MSG(0, "Info: Use default preen mode\n");
					c.preen_mode = PREEN_MODE_0;
					c.auto_fix = 1;
					optind--;
					break;
				}
				c.preen_mode = atoi(optarg);
				if (c.preen_mode < 0)
					c.preen_mode = PREEN_MODE_0;
				else if (c.preen_mode >= PREEN_MODE_MAX)
					c.preen_mode = PREEN_MODE_MAX - 1;
				if (c.preen_mode == PREEN_MODE_0 ||
					c.preen_mode == PREEN_MODE_2)
					c.auto_fix = 1;
				MSG(0, "Info: Fix the reported corruption in "
					"preen mode %d\n", c.preen_mode);
				break;
			case 'd':
				if (optarg[0] == '-') {
					err = ENEED_ARG;
					break;
				} else if (!is_digits(optarg)) {
					err = EWRONG_OPT;
					break;
				}
				c.dbg_lv = atoi(optarg);
				MSG(0, "Info: Debug level = %d\n", c.dbg_lv);
				break;
			case 'f':
			case 'y':
				c.fix_on = 1;
				c.force = 1;
				MSG(0, "Info: Force to fix corruption\n");
				break;
			case 'q':
				c.preserve_limits = atoi(optarg);
				MSG(0, "Info: Preserve quota limits = %d\n",
					c.preserve_limits);
				break;
			case 'S':
				c.sparse_mode = 1;
				break;
			case 't':
				c.show_dentry = 1;
				break;
			case ':':
				if (optopt == 'p') {
					MSG(0, "Info: Use default preen mode\n");
					c.preen_mode = PREEN_MODE_0;
					c.auto_fix = 1;
				} else {
					option = optopt;
					err = ENEED_ARG;
					break;
				}
				break;
			case 'C':
				token = strtok(optarg, ":");
				val = f2fs_str2encoding(token);
				if (val < 0) {
					MSG(0, "\tError: Unknown encoding %s\n", token);
					fsck_usage();
				}
				c.s_encoding = val;
				token = strtok(NULL, "");
				val = f2fs_str2encoding_flags(&token, &c.s_encoding_flags);
				if (val) {
					MSG(0, "\tError: Unknown flag %s\n", token);
					fsck_usage();
				}
				c.feature |= cpu_to_le32(F2FS_FEATURE_CASEFOLD);
				break;
			case 'V':
				show_version(prog);
				exit(0);
			case '?':
				option = optopt;
			default:
				err = EUNKNOWN_OPT;
				break;
			}
			if (err != NOERROR)
				break;
		}
	} else if (!strcmp("dump.f2fs", prog)) {
#ifdef WITH_DUMP
		const char *option_string = "d:i:n:Ms:Sa:b:V";
		static struct dump_option dump_opt = {
			.nid = 0,	/* default root ino */
			.start_nat = -1,
			.end_nat = -1,
			.start_sit = -1,
			.end_sit = -1,
			.start_ssa = -1,
			.end_ssa = -1,
			.blk_addr = -1,
		};

		c.func = DUMP;
		while ((option = getopt(argc, argv, option_string)) != EOF) {
			int ret = 0;

			switch (option) {
			case 'd':
				if (!is_digits(optarg)) {
					err = EWRONG_OPT;
					break;
				}
				c.dbg_lv = atoi(optarg);
				MSG(0, "Info: Debug level = %d\n",
							c.dbg_lv);
				break;
			case 'g':
				if (!strcmp(optarg, "android")) {
					c.defset = CONF_ANDROID;
					MSG(0, "Info: Set conf for android\n");
					break;
				}
				err = EWRONG_OPT;
				break;
			case 'i':
				if (strncmp(optarg, "0x", 2))
					ret = sscanf(optarg, "%d",
							&dump_opt.nid);
				else
					ret = sscanf(optarg, "%x",
							&dump_opt.nid);
				break;
			case 'n':
				ret = sscanf(optarg, "%d~%d",
							&dump_opt.start_nat,
							&dump_opt.end_nat);
				break;
			case 'M':
				c.show_file_map = 1;
				break;
			case 's':
				ret = sscanf(optarg, "%d~%d",
							&dump_opt.start_sit,
							&dump_opt.end_sit);
				break;
			case 'S':
				c.sparse_mode = 1;
				break;
			case 'a':
				ret = sscanf(optarg, "%d~%d",
							&dump_opt.start_ssa,
							&dump_opt.end_ssa);
				break;
			case 'b':
				if (strncmp(optarg, "0x", 2))
					ret = sscanf(optarg, "%d",
							&dump_opt.blk_addr);
				else
					ret = sscanf(optarg, "%x",
							&dump_opt.blk_addr);
				break;
			case 'V':
				show_version(prog);
				exit(0);
			default:
				err = EUNKNOWN_OPT;
				break;
			}
			ASSERT(ret >= 0);
			if (err != NOERROR)
				break;
		}

		c.private = &dump_opt;
#endif
	} else if (!strcmp("defrag.f2fs", prog)) {
#ifdef WITH_DEFRAG
		const char *option_string = "d:s:Sl:t:iV";

		c.func = DEFRAG;
		while ((option = getopt(argc, argv, option_string)) != EOF) {
			int ret = 0;

			switch (option) {
			case 'd':
				if (!is_digits(optarg)) {
					err = EWRONG_OPT;
					break;
				}
				c.dbg_lv = atoi(optarg);
				MSG(0, "Info: Debug level = %d\n",
							c.dbg_lv);
				break;
			case 's':
				if (strncmp(optarg, "0x", 2))
					ret = sscanf(optarg, "%"PRIu64"",
							&c.defrag_start);
				else
					ret = sscanf(optarg, "%"PRIx64"",
							&c.defrag_start);
				break;
			case 'S':
				c.sparse_mode = 1;
				break;
			case 'l':
				if (strncmp(optarg, "0x", 2))
					ret = sscanf(optarg, "%"PRIu64"",
							&c.defrag_len);
				else
					ret = sscanf(optarg, "%"PRIx64"",
							&c.defrag_len);
				break;
			case 't':
				if (strncmp(optarg, "0x", 2))
					ret = sscanf(optarg, "%"PRIu64"",
							&c.defrag_target);
				else
					ret = sscanf(optarg, "%"PRIx64"",
							&c.defrag_target);
				break;
			case 'i':
				c.defrag_shrink = 1;
				break;
			case 'V':
				show_version(prog);
				exit(0);
			default:
				err = EUNKNOWN_OPT;
				break;
			}
			ASSERT(ret >= 0);
			if (err != NOERROR)
				break;
		}
#endif
	} else if (!strcmp("resize.f2fs", prog)) {
#ifdef WITH_RESIZE
		const char *option_string = "d:fst:iV";

		c.func = RESIZE;
		while ((option = getopt(argc, argv, option_string)) != EOF) {
			int ret = 0;

			switch (option) {
			case 'd':
				if (!is_digits(optarg)) {
					err = EWRONG_OPT;
					break;
				}
				c.dbg_lv = atoi(optarg);
				MSG(0, "Info: Debug level = %d\n",
							c.dbg_lv);
				break;
			case 'f':
				c.force = 1;
				MSG(0, "Info: Force to resize\n");
				break;
			case 's':
				c.safe_resize = 1;
				break;
			case 't':
				if (strncmp(optarg, "0x", 2))
					ret = sscanf(optarg, "%"PRIu64"",
							&c.target_sectors);
				else
					ret = sscanf(optarg, "%"PRIx64"",
							&c.target_sectors);
				break;
			case 'i':
				c.large_nat_bitmap = 1;
				break;
			case 'V':
				show_version(prog);
				exit(0);
			default:
				err = EUNKNOWN_OPT;
				break;
			}
			ASSERT(ret >= 0);
			if (err != NOERROR)
				break;
		}
#endif
	} else if (!strcmp("sload.f2fs", prog)) {
#ifdef WITH_SLOAD
		const char *option_string = "cL:a:i:x:m:rC:d:f:p:s:St:T:VP";
#ifdef HAVE_LIBSELINUX
		int max_nr_opt = (int)sizeof(c.seopt_file) /
			sizeof(c.seopt_file[0]);
		char *token;
#endif
		char *p;

		c.func = SLOAD;
		c.compress.cc.log_cluster_size = 2;
		c.compress.alg = COMPR_LZ4;
		c.compress.min_blocks = 1;
		c.compress.filter_ops = &ext_filter;
		while ((option = getopt(argc, argv, option_string)) != EOF) {
			unsigned int i;
			int val;

			switch (option) {
			case 'c': /* compression support */
				c.compress.enabled = true;
				break;
			case 'L': /* compression: log of blocks-per-cluster */
				c.compress.required = true;
				val = atoi(optarg);
				if (val < MIN_COMPRESS_LOG_SIZE ||
						val > MAX_COMPRESS_LOG_SIZE) {
					MSG(0, "\tError: log of blocks per"
						" cluster must be in the range"
						" of %d .. %d.\n",
						MIN_COMPRESS_LOG_SIZE,
						MAX_COMPRESS_LOG_SIZE);
					error_out(prog);
				}
				c.compress.cc.log_cluster_size = val;
				break;
			case 'a': /* compression: choose algorithm */
				c.compress.required = true;
				c.compress.alg = MAX_COMPRESS_ALGS;
				for (i = 0; i < MAX_COMPRESS_ALGS; i++) {
					if (!strcmp(supported_comp_names[i],
								optarg)) {
						c.compress.alg = i;
						break;
					}
				}
				if (c.compress.alg == MAX_COMPRESS_ALGS) {
					MSG(0, "\tError: Unknown compression"
						" algorithm %s\n", optarg);
					error_out(prog);
				}
				break;
			case 'i': /* compress only these extensions */
				c.compress.required = true;
				if (c.compress.filter == COMPR_FILTER_ALLOW) {
					MSG(0, "\tError: could not mix option"
							" -i and -x\n");
					error_out(prog);
				}
				c.compress.filter = COMPR_FILTER_DENY;
				c.compress.filter_ops->add(optarg);
				break;
			case 'x': /* compress except for these extensions */
				c.compress.required = true;
				if (c.compress.filter == COMPR_FILTER_DENY) {
					MSG(0, "\tError: could not mix option"
							" -i and -x\n");
					error_out(prog);
				}
				c.compress.filter = COMPR_FILTER_ALLOW;
				c.compress.filter_ops->add(optarg);
				break;
			case 'm': /* minimum compressed blocks per cluster */
				c.compress.required = true;
				val = atoi(optarg);
				if (val <= 0) {
					MSG(0, "\tError: minimum compressed"
						" blocks per cluster must be"
						" positive.\n");
					error_out(prog);
				}
				c.compress.min_blocks = val;
				break;
			case 'r': /* for setting FI_COMPRESS_RELEASED */
				c.compress.required = true;
				c.compress.readonly = true;
				break;
			case 'C':
				c.fs_config_file = absolute_path(optarg);
				break;
			case 'd':
				if (!is_digits(optarg)) {
					err = EWRONG_OPT;
					break;
				}
				c.dbg_lv = atoi(optarg);
				MSG(0, "Info: Debug level = %d\n",
						c.dbg_lv);
				break;
			case 'f':
				c.from_dir = absolute_path(optarg);
				break;
			case 'p':
				c.target_out_dir = absolute_path(optarg);
				break;
			case 's':
#ifdef HAVE_LIBSELINUX
				token = strtok(optarg, ",");
				while (token) {
					if (c.nr_opt == max_nr_opt) {
						MSG(0, "\tError: Expected at most %d selinux opts\n",
										max_nr_opt);
						error_out(prog);
					}
					c.seopt_file[c.nr_opt].type =
								SELABEL_OPT_PATH;
					c.seopt_file[c.nr_opt].value =
								absolute_path(token);
					c.nr_opt++;
					token = strtok(NULL, ",");
				}
#else
				MSG(0, "Info: Not support selinux opts\n");
#endif
				break;
			case 'S':
				c.sparse_mode = 1;
				break;
			case 't':
				c.mount_point = (char *)optarg;
				break;
			case 'T':
				c.fixed_time = strtoul(optarg, &p, 0);
				break;
			case 'V':
				show_version(prog);
				exit(0);
			case 'P':
				c.preserve_perms = 1;
				break;
			default:
				err = EUNKNOWN_OPT;
				break;
			}
			if (err != NOERROR)
				break;
		}
		if (c.compress.required && !c.compress.enabled) {
			MSG(0, "\tError: compression sub-options are used"
				" without the compression enable (-c) option\n"
			);
			error_out(prog);
		}
		if (err == NOERROR && c.compress.enabled) {
			c.compress.cc.cluster_size = 1
				<< c.compress.cc.log_cluster_size;
			if (c.compress.filter == COMPR_FILTER_UNASSIGNED)
				c.compress.filter = COMPR_FILTER_ALLOW;
			if (c.compress.min_blocks >=
					c.compress.cc.cluster_size) {
				MSG(0, "\tError: minimum reduced blocks by"
					" compression per cluster must be at"
					" most one less than blocks per"
					" cluster, i.e. %d\n",
					c.compress.cc.cluster_size - 1);
				error_out(prog);
			}
		}
#endif /* WITH_SLOAD */
	} else if (!strcmp("f2fslabel", prog)) {
#ifdef WITH_LABEL
		const char *option_string = "V";

		c.func = LABEL;
		while ((option = getopt(argc, argv, option_string)) != EOF) {
			switch (option) {
			case 'V':
				show_version(prog);
				exit(0);
			default:
				err = EUNKNOWN_OPT;
				break;
			}
			if (err != NOERROR)
				break;
		}

		if (argc > (optind + 2)) { /* unknown argument(s) is(are) passed */
			optind += 2;
			err = EUNKNOWN_ARG;
		} else if (argc == (optind + 2)) { /* change label */
			c.vol_label = argv[optind + 1];
			argc--;
		} else { /* print label */
			/*
			 * Since vol_label was initialized as "", in order to
			 * distinguish between clear label and print, set
			 * vol_label as NULL for print case
			 */
			c.vol_label = NULL;
		}
#endif /* WITH_LABEL */
	}

	if (err == NOERROR) {
		add_default_options();

		if (optind >= argc) {
			MSG(0, "\tError: Device not specified\n");
			error_out(prog);
		}

		c.devices[0].path = strdup(argv[optind]);
		if (argc > (optind + 1)) {
			c.dbg_lv = 0;
			err = EUNKNOWN_ARG;
		}
		if (err == NOERROR)
			return;
	}

	/* print out error */
	switch (err) {
	case EWRONG_OPT:
		MSG(0, "\tError: Wrong option -%c %s\n", option, optarg);
		break;
	case ENEED_ARG:
		MSG(0, "\tError: Need argument for -%c\n", option);
		break;
	case EUNKNOWN_OPT:
		MSG(0, "\tError: Unknown option %c\n", option);
		break;
	case EUNKNOWN_ARG:
		MSG(0, "\tError: Unknown argument %s\n", argv[optind]);
		break;
	}
	error_out(prog);
}

static int do_fsck(struct f2fs_sb_info *sbi)
{
	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
	u32 flag = le32_to_cpu(ckpt->ckpt_flags);
	u32 blk_cnt;
	struct f2fs_compr_blk_cnt cbc;
	errcode_t ret;

	fsck_init(sbi);

	print_cp_state(flag);

	fsck_chk_and_fix_write_pointers(sbi);

	fsck_chk_curseg_info(sbi);

	if (!c.fix_on && !c.bug_on) {
		switch (c.preen_mode) {
		case PREEN_MODE_1:
			if (fsck_chk_meta(sbi)) {
				MSG(0, "[FSCK] F2FS metadata   [Fail]");
				MSG(0, "\tError: meta does not match, "
					"force check all\n");
			} else {
				MSG(0, "[FSCK] F2FS metadata   [Ok..]");
				fsck_free(sbi);
				return FSCK_SUCCESS;
			}

			if (!c.ro)
				c.fix_on = 1;
			break;
		}
	} else if (c.preen_mode) {
		/*
		 * we can hit this in 3 situations:
		 *  1. fsck -f, fix_on has already been set to 1 when
		 *     parsing options;
		 *  2. fsck -a && CP_FSCK_FLAG is set, fix_on has already
		 *     been set to 1 when checking CP_FSCK_FLAG;
		 *  3. fsck -p 1 && error is detected, then bug_on is set,
		 *     we set fix_on = 1 here, so that fsck can fix errors
		 *     automatically
		*/
		c.fix_on = 1;
	}

	fsck_chk_checkpoint(sbi);

	fsck_chk_quota_node(sbi);

	/* Traverse all block recursively from root inode */
	blk_cnt = 1;
	cbc.cnt = 0;
	cbc.cheader_pgofs = CHEADER_PGOFS_NONE;

	if (c.feature & cpu_to_le32(F2FS_FEATURE_QUOTA_INO)) {
		ret = quota_init_context(sbi);
		if (ret) {
			ASSERT_MSG("quota_init_context failure: %d", ret);
			return FSCK_OPERATIONAL_ERROR;
		}
	}
	fsck_chk_orphan_node(sbi);
	fsck_chk_node_blk(sbi, NULL, sbi->root_ino_num,
			F2FS_FT_DIR, TYPE_INODE, &blk_cnt, &cbc, NULL);
	fsck_chk_quota_files(sbi);

	ret = fsck_verify(sbi);
	fsck_free(sbi);

	if (!c.bug_on)
		return FSCK_SUCCESS;
	if (!ret)
		return FSCK_ERROR_CORRECTED;
	return FSCK_ERRORS_LEFT_UNCORRECTED;
}

#ifdef WITH_DUMP
static void do_dump(struct f2fs_sb_info *sbi)
{
	struct dump_option *opt = (struct dump_option *)c.private;
	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
	u32 flag = le32_to_cpu(ckpt->ckpt_flags);

	if (opt->end_nat == -1)
		opt->end_nat = NM_I(sbi)->max_nid;
	if (opt->end_sit == -1)
		opt->end_sit = SM_I(sbi)->main_segments;
	if (opt->end_ssa == -1)
		opt->end_ssa = SM_I(sbi)->main_segments;
	if (opt->start_nat != -1)
		nat_dump(sbi, opt->start_nat, opt->end_nat);
	if (opt->start_sit != -1)
		sit_dump(sbi, opt->start_sit, opt->end_sit);
	if (opt->start_ssa != -1)
		ssa_dump(sbi, opt->start_ssa, opt->end_ssa);
	if (opt->blk_addr != -1)
		dump_info_from_blkaddr(sbi, opt->blk_addr);
	if (opt->nid)
		dump_node(sbi, opt->nid, 0);

	print_cp_state(flag);

}
#endif

#ifdef WITH_DEFRAG
static int do_defrag(struct f2fs_sb_info *sbi)
{
	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);

	if (get_sb(feature) & cpu_to_le32(F2FS_FEATURE_RO)) {
		MSG(0, "Not support on readonly image.\n");
		return -1;
	}

	if (c.defrag_start > get_sb(block_count))
		goto out_range;
	if (c.defrag_start < SM_I(sbi)->main_blkaddr)
		c.defrag_start = SM_I(sbi)->main_blkaddr;

	if (c.defrag_len == 0)
		c.defrag_len = sbi->blocks_per_seg;

	if (c.defrag_start + c.defrag_len > get_sb(block_count))
		c.defrag_len = get_sb(block_count) - c.defrag_start;

	if (c.defrag_target == 0) {
		c.defrag_target = c.defrag_start - 1;
		if (!c.defrag_shrink)
			c.defrag_target += c.defrag_len + 1;
	}

	if (c.defrag_target < SM_I(sbi)->main_blkaddr ||
			c.defrag_target > get_sb(block_count))
		goto out_range;
	if (c.defrag_target >= c.defrag_start &&
		c.defrag_target < c.defrag_start + c.defrag_len)
		goto out_range;

	if (c.defrag_start > c.defrag_target)
		MSG(0, "Info: Move 0x%"PRIx64" <- [0x%"PRIx64"-0x%"PRIx64"]\n",
				c.defrag_target,
				c.defrag_start,
				c.defrag_start + c.defrag_len - 1);
	else
		MSG(0, "Info: Move [0x%"PRIx64"-0x%"PRIx64"] -> 0x%"PRIx64"\n",
				c.defrag_start,
				c.defrag_start + c.defrag_len - 1,
				c.defrag_target);

	return f2fs_defragment(sbi, c.defrag_start, c.defrag_len,
			c.defrag_target, c.defrag_shrink);
out_range:
	ASSERT_MSG("Out-of-range [0x%"PRIx64" ~ 0x%"PRIx64"] to 0x%"PRIx64"",
				c.defrag_start,
				c.defrag_start + c.defrag_len - 1,
				c.defrag_target);
	return -1;
}
#endif

#ifdef WITH_RESIZE
static int do_resize(struct f2fs_sb_info *sbi)
{
	if (!c.target_sectors)
		c.target_sectors = c.total_sectors;

	if (c.target_sectors > c.total_sectors) {
		ASSERT_MSG("Out-of-range Target=0x%"PRIx64" / 0x%"PRIx64"",
				c.target_sectors, c.total_sectors);
		return -1;
	}

	return f2fs_resize(sbi);
}
#endif

#ifdef WITH_SLOAD
static int init_compr(struct f2fs_sb_info *sbi)
{
	if (!c.compress.enabled)
		return 0;

	if (!(sbi->raw_super->feature
			& cpu_to_le32(F2FS_FEATURE_COMPRESSION))) {
		MSG(0, "Error: Compression (-c) was requested "
			"but the file system is not created "
			"with such feature.\n");
		return -1;
	}
	if (!supported_comp_ops[c.compress.alg].init) {
		MSG(0, "Error: The selected compression algorithm is not"
				" supported\n");
		return -1;
	}
	c.compress.ops = supported_comp_ops + c.compress.alg;
	c.compress.ops->init(&c.compress.cc);
	c.compress.ops->reset(&c.compress.cc);
	c.compress.cc.rlen = c.compress.cc.cluster_size * F2FS_BLKSIZE;
	return 0;
}

static int do_sload(struct f2fs_sb_info *sbi)
{
	if (!c.from_dir) {
		MSG(0, "Info: No source directory, but it's okay.\n");
		return 0;
	}
	if (!c.mount_point)
		c.mount_point = "/";

	if (init_compr(sbi))
		return -1;

	return f2fs_sload(sbi);
}
#endif

#ifdef WITH_LABEL
static int do_label(struct f2fs_sb_info *sbi)
{
	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);

	if (!c.vol_label) {
		char label[MAX_VOLUME_NAME];

		utf16_to_utf8(label, sb->volume_name,
			      MAX_VOLUME_NAME, MAX_VOLUME_NAME);
		MSG(0, "Info: volume label = %s\n", label);
		return 0;
	}

	if (strlen(c.vol_label) > MAX_VOLUME_NAME) {
		ERR_MSG("Label should not exceed %d characters\n", MAX_VOLUME_NAME);
		return -1;
	}

	utf8_to_utf16(sb->volume_name, (const char *)c.vol_label,
		      MAX_VOLUME_NAME, strlen(c.vol_label));

	update_superblock(sb, SB_MASK_ALL);

	MSG(0, "Info: volume label is changed to %s\n", c.vol_label);

	return 0;
}
#endif

#if defined(__APPLE__)
static u64 get_boottime_ns()
{
#ifdef HAVE_MACH_TIME_H
	return mach_absolute_time();
#else
	return 0;
#endif
}
#else
static u64 get_boottime_ns()
{
	struct timespec t;
	t.tv_sec = t.tv_nsec = 0;
	clock_gettime(CLOCK_BOOTTIME, &t);
	return (u64)t.tv_sec * 1000000000LL + t.tv_nsec;
}
#endif

int main(int argc, char **argv)
{
	struct f2fs_sb_info *sbi;
	int ret = 0, ret2;
	u64 start = get_boottime_ns();

	f2fs_init_configuration();

	f2fs_parse_options(argc, argv);

	if (c.func != DUMP && f2fs_devs_are_umounted() < 0) {
		if (errno == EBUSY) {
			ret = -1;
			if (c.func == FSCK)
				ret = FSCK_OPERATIONAL_ERROR;
			goto quick_err;
		}
		if (!c.ro || c.func == DEFRAG) {
			MSG(0, "\tError: Not available on mounted device!\n");
			ret = -1;
			if (c.func == FSCK)
				ret = FSCK_OPERATIONAL_ERROR;
			goto quick_err;
		}

		/* allow ro-mounted partition */
		if (c.force) {
			MSG(0, "Info: Force to check/repair FS on RO mounted device\n");
		} else {
			MSG(0, "Info: Check FS only on RO mounted device\n");
			c.fix_on = 0;
			c.auto_fix = 0;
		}
	}

	/* Get device */
	if (f2fs_get_device_info() < 0) {
		ret = -1;
		if (c.func == FSCK)
			ret = FSCK_OPERATIONAL_ERROR;
		goto quick_err;
	}

fsck_again:
	memset(&gfsck, 0, sizeof(gfsck));
	gfsck.sbi.fsck = &gfsck;
	sbi = &gfsck.sbi;

	ret = f2fs_do_mount(sbi);
	if (ret != 0) {
		if (ret == 1) {
			MSG(0, "Info: No error was reported\n");
			ret = 0;
		}
		goto out_err;
	}

	switch (c.func) {
	case FSCK:
		ret = do_fsck(sbi);
		break;
#ifdef WITH_DUMP
	case DUMP:
		do_dump(sbi);
		break;
#endif
#ifdef WITH_DEFRAG
	case DEFRAG:
		ret = do_defrag(sbi);
		if (ret)
			goto out_err;
		break;
#endif
#ifdef WITH_RESIZE
	case RESIZE:
		if (do_resize(sbi))
			goto out_err;
		break;
#endif
#ifdef WITH_SLOAD
	case SLOAD:
		if (do_sload(sbi))
			goto out_err;

		ret = f2fs_sparse_initialize_meta(sbi);
		if (ret < 0)
			goto out_err;

		f2fs_do_umount(sbi);

		/* fsck to fix missing quota */
		c.func = FSCK;
		c.fix_on = 1;
		goto fsck_again;
#endif
#ifdef WITH_LABEL
	case LABEL:
		if (do_label(sbi))
			goto out_err;
		break;
#endif
	default:
		ERR_MSG("Wrong program name\n");
		ASSERT(0);
	}

	f2fs_do_umount(sbi);

	if (c.func == FSCK && c.bug_on) {
		if (!c.ro && c.fix_on == 0 && c.auto_fix == 0 && !c.dry_run) {
			char ans[255] = {0};
retry:
			printf("Do you want to fix this partition? [Y/N] ");
			ret2 = scanf("%s", ans);
			ASSERT(ret2 >= 0);
			if (!strcasecmp(ans, "y"))
				c.fix_on = 1;
			else if (!strcasecmp(ans, "n"))
				c.fix_on = 0;
			else
				goto retry;

			if (c.fix_on)
				goto fsck_again;
		}
	}
	ret2 = f2fs_finalize_device();
	if (ret2) {
		if (c.func == FSCK)
			return FSCK_OPERATIONAL_ERROR;
		return ret2;
	}

	if (c.func == SLOAD)
		c.compress.filter_ops->destroy();

	if (!c.show_file_map)
		printf("\nDone: %lf secs\n", (get_boottime_ns() - start) / 1000000000.0);
	return ret;

out_err:
	if (sbi->ckpt)
		free(sbi->ckpt);
	if (sbi->raw_super)
		free(sbi->raw_super);
quick_err:
	f2fs_release_sparse_resource();
	return ret;
}