aboutsummaryrefslogtreecommitdiff
path: root/fsck/fsck.c
blob: d38ad1e2b86e1bf84de06793bc47d9dccf5b4384 (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
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
/**
 * fsck.c
 *
 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
 *             http://www.samsung.com/
 *
 * 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 "xattr.h"
#include "quotaio.h"
#include <time.h>

char *tree_mark;
uint32_t tree_mark_size = 256;

int f2fs_set_main_bitmap(struct f2fs_sb_info *sbi, u32 blk, int type)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	struct seg_entry *se;
	int fix = 0;

	se = get_seg_entry(sbi, GET_SEGNO(sbi, blk));
	if (se->type >= NO_CHECK_TYPE)
		fix = 1;
	else if (IS_DATASEG(se->type) != IS_DATASEG(type))
		fix = 1;

	/* just check data and node types */
	if (fix) {
		DBG(1, "Wrong segment type [0x%x] %x -> %x",
				GET_SEGNO(sbi, blk), se->type, type);
		se->type = type;
	}
	return f2fs_set_bit(BLKOFF_FROM_MAIN(sbi, blk), fsck->main_area_bitmap);
}

static inline int f2fs_test_main_bitmap(struct f2fs_sb_info *sbi, u32 blk)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);

	return f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, blk),
						fsck->main_area_bitmap);
}

static inline int f2fs_clear_main_bitmap(struct f2fs_sb_info *sbi, u32 blk)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);

	return f2fs_clear_bit(BLKOFF_FROM_MAIN(sbi, blk),
						fsck->main_area_bitmap);
}

static inline int f2fs_test_sit_bitmap(struct f2fs_sb_info *sbi, u32 blk)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);

	return f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, blk), fsck->sit_area_bitmap);
}

int f2fs_set_sit_bitmap(struct f2fs_sb_info *sbi, u32 blk)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);

	return f2fs_set_bit(BLKOFF_FROM_MAIN(sbi, blk), fsck->sit_area_bitmap);
}

static int add_into_hard_link_list(struct f2fs_sb_info *sbi,
						u32 nid, u32 link_cnt)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	struct hard_link_node *node = NULL, *tmp = NULL, *prev = NULL;

	node = calloc(sizeof(struct hard_link_node), 1);
	ASSERT(node != NULL);

	node->nid = nid;
	node->links = link_cnt;
	node->actual_links = 1;
	node->next = NULL;

	if (fsck->hard_link_list_head == NULL) {
		fsck->hard_link_list_head = node;
		goto out;
	}

	tmp = fsck->hard_link_list_head;

	/* Find insertion position */
	while (tmp && (nid < tmp->nid)) {
		ASSERT(tmp->nid != nid);
		prev = tmp;
		tmp = tmp->next;
	}

	if (tmp == fsck->hard_link_list_head) {
		node->next = tmp;
		fsck->hard_link_list_head = node;
	} else {
		prev->next = node;
		node->next = tmp;
	}

out:
	DBG(2, "ino[0x%x] has hard links [0x%x]\n", nid, link_cnt);
	return 0;
}

static int find_and_dec_hard_link_list(struct f2fs_sb_info *sbi, u32 nid)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	struct hard_link_node *node = NULL, *prev = NULL;

	if (fsck->hard_link_list_head == NULL)
		return -EINVAL;

	node = fsck->hard_link_list_head;

	while (node && (nid < node->nid)) {
		prev = node;
		node = node->next;
	}

	if (node == NULL || (nid != node->nid))
		return -EINVAL;

	/* Decrease link count */
	node->links = node->links - 1;
	node->actual_links++;

	/* if link count becomes one, remove the node */
	if (node->links == 1) {
		if (fsck->hard_link_list_head == node)
			fsck->hard_link_list_head = node->next;
		else
			prev->next = node->next;
		free(node);
	}
	return 0;
}

static int is_valid_ssa_node_blk(struct f2fs_sb_info *sbi, u32 nid,
							u32 blk_addr)
{
	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
	struct f2fs_summary_block *sum_blk;
	struct f2fs_summary *sum_entry;
	struct seg_entry * se;
	u32 segno, offset;
	int need_fix = 0, ret = 0;
	int type;

	if (get_sb(feature) & cpu_to_le32(F2FS_FEATURE_RO))
		return 0;

	segno = GET_SEGNO(sbi, blk_addr);
	offset = OFFSET_IN_SEG(sbi, blk_addr);

	sum_blk = get_sum_block(sbi, segno, &type);

	if (type != SEG_TYPE_NODE && type != SEG_TYPE_CUR_NODE) {
		/* can't fix current summary, then drop the block */
		if (!c.fix_on || type < 0) {
			ASSERT_MSG("Summary footer is not for node segment");
			ret = -EINVAL;
			goto out;
		}

		need_fix = 1;
		se = get_seg_entry(sbi, segno);
		if(IS_NODESEG(se->type)) {
			FIX_MSG("Summary footer indicates a node segment: 0x%x", segno);
			sum_blk->footer.entry_type = SUM_TYPE_NODE;
		} else {
			ret = -EINVAL;
			goto out;
		}
	}

	sum_entry = &(sum_blk->entries[offset]);

	if (le32_to_cpu(sum_entry->nid) != nid) {
		if (!c.fix_on || type < 0) {
			DBG(0, "nid                       [0x%x]\n", nid);
			DBG(0, "target blk_addr           [0x%x]\n", blk_addr);
			DBG(0, "summary blk_addr          [0x%x]\n",
						GET_SUM_BLKADDR(sbi,
						GET_SEGNO(sbi, blk_addr)));
			DBG(0, "seg no / offset           [0x%x / 0x%x]\n",
						GET_SEGNO(sbi, blk_addr),
						OFFSET_IN_SEG(sbi, blk_addr));
			DBG(0, "summary_entry.nid         [0x%x]\n",
						le32_to_cpu(sum_entry->nid));
			DBG(0, "--> node block's nid      [0x%x]\n", nid);
			ASSERT_MSG("Invalid node seg summary\n");
			ret = -EINVAL;
		} else {
			FIX_MSG("Set node summary 0x%x -> [0x%x] [0x%x]",
						segno, nid, blk_addr);
			sum_entry->nid = cpu_to_le32(nid);
			need_fix = 1;
		}
	}
	if (need_fix && f2fs_dev_is_writable()) {
		u64 ssa_blk;
		int ret2;

		ssa_blk = GET_SUM_BLKADDR(sbi, segno);
		ret2 = dev_write_block(sum_blk, ssa_blk);
		ASSERT(ret2 >= 0);
	}
out:
	if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA ||
					type == SEG_TYPE_MAX)
		free(sum_blk);
	return ret;
}

static int is_valid_summary(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
							u32 blk_addr)
{
	u16 ofs_in_node = le16_to_cpu(sum->ofs_in_node);
	u32 nid = le32_to_cpu(sum->nid);
	struct f2fs_node *node_blk = NULL;
	__le32 target_blk_addr;
	struct node_info ni;
	int ret = 0;

	node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
	ASSERT(node_blk != NULL);

	if (!IS_VALID_NID(sbi, nid))
		goto out;

	get_node_info(sbi, nid, &ni);

	if (!IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
		goto out;

	/* read node_block */
	ret = dev_read_block(node_blk, ni.blk_addr);
	ASSERT(ret >= 0);

	if (le32_to_cpu(node_blk->footer.nid) != nid)
		goto out;

	/* check its block address */
	if (node_blk->footer.nid == node_blk->footer.ino) {
		int ofs = get_extra_isize(node_blk);

		if (ofs + ofs_in_node >= DEF_ADDRS_PER_INODE)
			goto out;
		target_blk_addr = node_blk->i.i_addr[ofs + ofs_in_node];
	} else {
		if (ofs_in_node >= DEF_ADDRS_PER_BLOCK)
			goto out;
		target_blk_addr = node_blk->dn.addr[ofs_in_node];
	}

	if (blk_addr == le32_to_cpu(target_blk_addr))
		ret = 1;
out:
	free(node_blk);
	return ret;
}

static int is_valid_ssa_data_blk(struct f2fs_sb_info *sbi, u32 blk_addr,
		u32 parent_nid, u16 idx_in_node, u8 version)
{
	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
	struct f2fs_summary_block *sum_blk;
	struct f2fs_summary *sum_entry;
	struct seg_entry * se;
	u32 segno, offset;
	int need_fix = 0, ret = 0;
	int type;

	if (get_sb(feature) & cpu_to_le32(F2FS_FEATURE_RO))
		return 0;

	segno = GET_SEGNO(sbi, blk_addr);
	offset = OFFSET_IN_SEG(sbi, blk_addr);

	sum_blk = get_sum_block(sbi, segno, &type);

	if (type != SEG_TYPE_DATA && type != SEG_TYPE_CUR_DATA) {
		/* can't fix current summary, then drop the block */
		if (!c.fix_on || type < 0) {
			ASSERT_MSG("Summary footer is not for data segment");
			ret = -EINVAL;
			goto out;
		}

		need_fix = 1;
		se = get_seg_entry(sbi, segno);
		if (IS_DATASEG(se->type)) {
			FIX_MSG("Summary footer indicates a data segment: 0x%x", segno);
			sum_blk->footer.entry_type = SUM_TYPE_DATA;
		} else {
			ret = -EINVAL;
			goto out;
		}
	}

	sum_entry = &(sum_blk->entries[offset]);

	if (le32_to_cpu(sum_entry->nid) != parent_nid ||
			sum_entry->version != version ||
			le16_to_cpu(sum_entry->ofs_in_node) != idx_in_node) {
		if (!c.fix_on || type < 0) {
			DBG(0, "summary_entry.nid         [0x%x]\n",
					le32_to_cpu(sum_entry->nid));
			DBG(0, "summary_entry.version     [0x%x]\n",
					sum_entry->version);
			DBG(0, "summary_entry.ofs_in_node [0x%x]\n",
					le16_to_cpu(sum_entry->ofs_in_node));
			DBG(0, "parent nid                [0x%x]\n",
					parent_nid);
			DBG(0, "version from nat          [0x%x]\n", version);
			DBG(0, "idx in parent node        [0x%x]\n",
					idx_in_node);

			DBG(0, "Target data block addr    [0x%x]\n", blk_addr);
			ASSERT_MSG("Invalid data seg summary\n");
			ret = -EINVAL;
		} else if (is_valid_summary(sbi, sum_entry, blk_addr)) {
			/* delete wrong index */
			ret = -EINVAL;
		} else {
			FIX_MSG("Set data summary 0x%x -> [0x%x] [0x%x] [0x%x]",
					segno, parent_nid, version, idx_in_node);
			sum_entry->nid = cpu_to_le32(parent_nid);
			sum_entry->version = version;
			sum_entry->ofs_in_node = cpu_to_le16(idx_in_node);
			need_fix = 1;
		}
	}
	if (need_fix && f2fs_dev_is_writable()) {
		u64 ssa_blk;
		int ret2;

		ssa_blk = GET_SUM_BLKADDR(sbi, segno);
		ret2 = dev_write_block(sum_blk, ssa_blk);
		ASSERT(ret2 >= 0);
	}
out:
	if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA ||
					type == SEG_TYPE_MAX)
		free(sum_blk);
	return ret;
}

static int __check_inode_mode(u32 nid, enum FILE_TYPE ftype, u16 mode)
{
	if (ftype >= F2FS_FT_MAX)
		return 0;
	/* f2fs_iget will return -EIO if mode is not valid file type */
	if (!S_ISLNK(mode) && !S_ISREG(mode) && !S_ISDIR(mode) &&
	    !S_ISCHR(mode) && !S_ISBLK(mode) && !S_ISFIFO(mode) &&
	    !S_ISSOCK(mode)) {
		ASSERT_MSG("inode [0x%x] unknown file type i_mode [0x%x]",
			   nid, mode);
		return -1;
	}

	if (S_ISLNK(mode) && ftype != F2FS_FT_SYMLINK)
		goto err;
	if (S_ISREG(mode) && ftype != F2FS_FT_REG_FILE)
		goto err;
	if (S_ISDIR(mode) && ftype != F2FS_FT_DIR)
		goto err;
	if (S_ISCHR(mode) && ftype != F2FS_FT_CHRDEV)
		goto err;
	if (S_ISBLK(mode) && ftype != F2FS_FT_BLKDEV)
		goto err;
	if (S_ISFIFO(mode) && ftype != F2FS_FT_FIFO)
		goto err;
	if (S_ISSOCK(mode) && ftype != F2FS_FT_SOCK)
		goto err;
	return 0;
err:
	ASSERT_MSG("inode [0x%x] mismatch i_mode [0x%x vs. 0x%x]",
		   nid, ftype, mode);
	return -1;
}

static int sanity_check_nid(struct f2fs_sb_info *sbi, u32 nid,
			struct f2fs_node *node_blk,
			enum FILE_TYPE ftype, enum NODE_TYPE ntype,
			struct node_info *ni)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	int ret;

	if (!IS_VALID_NID(sbi, nid)) {
		ASSERT_MSG("nid is not valid. [0x%x]", nid);
		return -EINVAL;
	}

	get_node_info(sbi, nid, ni);
	if (ni->ino == 0) {
		ASSERT_MSG("nid[0x%x] ino is 0", nid);
		return -EINVAL;
	}

	if (ni->blk_addr == NEW_ADDR) {
		ASSERT_MSG("nid is NEW_ADDR. [0x%x]", nid);
		return -EINVAL;
	}

	if (!IS_VALID_BLK_ADDR(sbi, ni->blk_addr)) {
		ASSERT_MSG("blkaddress is not valid. [0x%x]", ni->blk_addr);
		return -EINVAL;
	}

	ret = dev_read_block(node_blk, ni->blk_addr);
	ASSERT(ret >= 0);

	if (ntype == TYPE_INODE &&
			node_blk->footer.nid != node_blk->footer.ino) {
		ASSERT_MSG("nid[0x%x] footer.nid[0x%x] footer.ino[0x%x]",
				nid, le32_to_cpu(node_blk->footer.nid),
				le32_to_cpu(node_blk->footer.ino));
		return -EINVAL;
	}
	if (ni->ino != le32_to_cpu(node_blk->footer.ino)) {
		ASSERT_MSG("nid[0x%x] nat_entry->ino[0x%x] footer.ino[0x%x]",
				nid, ni->ino, le32_to_cpu(node_blk->footer.ino));
		return -EINVAL;
	}
	if (ntype != TYPE_INODE &&
			node_blk->footer.nid == node_blk->footer.ino) {
		ASSERT_MSG("nid[0x%x] footer.nid[0x%x] footer.ino[0x%x]",
				nid, le32_to_cpu(node_blk->footer.nid),
				le32_to_cpu(node_blk->footer.ino));
		return -EINVAL;
	}

	if (le32_to_cpu(node_blk->footer.nid) != nid) {
		ASSERT_MSG("nid[0x%x] blk_addr[0x%x] footer.nid[0x%x]",
				nid, ni->blk_addr,
				le32_to_cpu(node_blk->footer.nid));
		return -EINVAL;
	}

	if (ntype == TYPE_XATTR) {
		u32 flag = le32_to_cpu(node_blk->footer.flag);

		if ((flag >> OFFSET_BIT_SHIFT) != XATTR_NODE_OFFSET) {
			ASSERT_MSG("xnid[0x%x] has wrong ofs:[0x%x]",
					nid, flag);
			return -EINVAL;
		}
	}

	if ((ntype == TYPE_INODE && ftype == F2FS_FT_DIR) ||
			(ntype == TYPE_XATTR && ftype == F2FS_FT_XATTR)) {
		/* not included '.' & '..' */
		if (f2fs_test_main_bitmap(sbi, ni->blk_addr) != 0) {
			ASSERT_MSG("Duplicated node blk. nid[0x%x][0x%x]\n",
					nid, ni->blk_addr);
			return -EINVAL;
		}
	}

	/* this if only from fix_hard_links */
	if (ftype == F2FS_FT_MAX)
		return 0;

	if (ntype == TYPE_INODE &&
		__check_inode_mode(nid, ftype, le16_to_cpu(node_blk->i.i_mode)))
		return -EINVAL;

	/* workaround to fix later */
	if (ftype != F2FS_FT_ORPHAN ||
			f2fs_test_bit(nid, fsck->nat_area_bitmap) != 0) {
		f2fs_clear_bit(nid, fsck->nat_area_bitmap);
		/* avoid reusing nid when reconnecting files */
		f2fs_set_bit(nid, NM_I(sbi)->nid_bitmap);
	} else
		ASSERT_MSG("orphan or xattr nid is duplicated [0x%x]\n",
				nid);

	if (is_valid_ssa_node_blk(sbi, nid, ni->blk_addr)) {
		ASSERT_MSG("summary node block is not valid. [0x%x]", nid);
		return -EINVAL;
	}

	if (f2fs_test_sit_bitmap(sbi, ni->blk_addr) == 0)
		ASSERT_MSG("SIT bitmap is 0x0. blk_addr[0x%x]",
				ni->blk_addr);

	if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0) {

		fsck->chk.valid_blk_cnt++;
		fsck->chk.valid_node_cnt++;

		/* Progress report */
		if (!c.show_file_map && sbi->total_valid_node_count > 1000) {
			unsigned int p10 = sbi->total_valid_node_count / 10;

			if (sbi->fsck->chk.checked_node_cnt++ % p10)
				return 0;

			printf("[FSCK] Check node %"PRIu64" / %u (%.2f%%)\n",
				sbi->fsck->chk.checked_node_cnt,
				sbi->total_valid_node_count,
				10 * (float)sbi->fsck->chk.checked_node_cnt /
				p10);
		}
	}
	return 0;
}

int fsck_sanity_check_nid(struct f2fs_sb_info *sbi, u32 nid,
			struct f2fs_node *node_blk,
			enum FILE_TYPE ftype, enum NODE_TYPE ntype,
			struct node_info *ni)
{
	return sanity_check_nid(sbi, nid, node_blk, ftype, ntype, ni);
}

static int fsck_chk_xattr_blk(struct f2fs_sb_info *sbi, u32 ino,
					u32 x_nid, u32 *blk_cnt)
{
	struct f2fs_node *node_blk = NULL;
	struct node_info ni;
	int ret = 0;

	if (x_nid == 0x0)
		return 0;

	node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
	ASSERT(node_blk != NULL);

	/* Sanity check */
	if (sanity_check_nid(sbi, x_nid, node_blk,
				F2FS_FT_XATTR, TYPE_XATTR, &ni)) {
		ret = -EINVAL;
		goto out;
	}

	*blk_cnt = *blk_cnt + 1;
	f2fs_set_main_bitmap(sbi, ni.blk_addr, CURSEG_COLD_NODE);
	DBG(2, "ino[0x%x] x_nid[0x%x]\n", ino, x_nid);
out:
	free(node_blk);
	return ret;
}

int fsck_chk_node_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
		u32 nid, enum FILE_TYPE ftype, enum NODE_TYPE ntype,
		u32 *blk_cnt, struct f2fs_compr_blk_cnt *cbc,
		struct child_info *child)
{
	struct node_info ni;
	struct f2fs_node *node_blk = NULL;

	node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
	ASSERT(node_blk != NULL);

	if (sanity_check_nid(sbi, nid, node_blk, ftype, ntype, &ni))
		goto err;

	if (ntype == TYPE_INODE) {
		struct f2fs_fsck *fsck = F2FS_FSCK(sbi);

		fsck_chk_inode_blk(sbi, nid, ftype, node_blk, blk_cnt, cbc,
				&ni, child);
		quota_add_inode_usage(fsck->qctx, nid, &node_blk->i);
	} else {
		switch (ntype) {
		case TYPE_DIRECT_NODE:
			f2fs_set_main_bitmap(sbi, ni.blk_addr,
							CURSEG_WARM_NODE);
			fsck_chk_dnode_blk(sbi, inode, nid, ftype, node_blk,
					blk_cnt, cbc, child, &ni);
			break;
		case TYPE_INDIRECT_NODE:
			f2fs_set_main_bitmap(sbi, ni.blk_addr,
							CURSEG_COLD_NODE);
			fsck_chk_idnode_blk(sbi, inode, ftype, node_blk,
					blk_cnt, cbc, child);
			break;
		case TYPE_DOUBLE_INDIRECT_NODE:
			f2fs_set_main_bitmap(sbi, ni.blk_addr,
							CURSEG_COLD_NODE);
			fsck_chk_didnode_blk(sbi, inode, ftype, node_blk,
					blk_cnt, cbc, child);
			break;
		default:
			ASSERT(0);
		}
	}
	free(node_blk);
	return 0;
err:
	free(node_blk);
	return -EINVAL;
}

static inline void get_extent_info(struct extent_info *ext,
					struct f2fs_extent *i_ext)
{
	ext->fofs = le32_to_cpu(i_ext->fofs);
	ext->blk = le32_to_cpu(i_ext->blk_addr);
	ext->len = le32_to_cpu(i_ext->len);
}

static void check_extent_info(struct child_info *child,
						block_t blkaddr, int last)
{
	struct extent_info *ei = &child->ei;
	u32 pgofs = child->pgofs;
	int is_hole = 0;

	if (!ei->len)
		return;

	if (child->state & FSCK_UNMATCHED_EXTENT)
		return;

	if ((child->state & FSCK_INLINE_INODE) && ei->len)
		goto unmatched;

	if (last) {
		/* hole exist in the back of extent */
		if (child->last_blk != ei->blk + ei->len - 1)
			child->state |= FSCK_UNMATCHED_EXTENT;
		return;
	}

	if (blkaddr == NULL_ADDR || blkaddr == NEW_ADDR)
		is_hole = 1;

	if (pgofs >= ei->fofs && pgofs < ei->fofs + ei->len) {
		/* unmatched blkaddr */
		if (is_hole || (blkaddr != pgofs - ei->fofs + ei->blk))
			goto unmatched;

		if (!child->last_blk) {
			/* hole exists in the front of extent */
			if (pgofs != ei->fofs)
				goto unmatched;
		} else if (child->last_blk + 1 != blkaddr) {
			/* hole exists in the middle of extent */
			goto unmatched;
		}
		child->last_blk = blkaddr;
		return;
	}

	if (is_hole)
		return;

	if (blkaddr < ei->blk || blkaddr >= ei->blk + ei->len)
		return;
	/* unmatched file offset */
unmatched:
	child->state |= FSCK_UNMATCHED_EXTENT;
}

void fsck_reada_node_block(struct f2fs_sb_info *sbi, u32 nid)
{
	struct node_info ni;

	if (nid != 0 && IS_VALID_NID(sbi, nid)) {
		get_node_info(sbi, nid, &ni);
		if (IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
			dev_reada_block(ni.blk_addr);
	}
}

void fsck_reada_all_direct_node_blocks(struct f2fs_sb_info *sbi,
						struct f2fs_node *node_blk)
{
	int i;

	for (i = 0; i < NIDS_PER_BLOCK; i++) {
		u32 nid = le32_to_cpu(node_blk->in.nid[i]);

		fsck_reada_node_block(sbi, nid);
	}
}

static bool is_zeroed(const u8 *p, size_t size)
{
	size_t i;

	for (i = 0; i < size; i++) {
		if (p[i])
			return false;
	}
	return true;
}

int chk_extended_attributes(struct f2fs_sb_info *sbi, u32 nid,
		struct f2fs_node *inode)
{
	void *xattr;
	void *last_base_addr;
	struct f2fs_xattr_entry *ent;
	__u32 xattr_size = XATTR_SIZE(&inode->i);
	bool need_fix = false;

	if (xattr_size == 0)
		return 0;

	xattr = read_all_xattrs(sbi, inode, false);
	ASSERT(xattr);

	last_base_addr = (void *)xattr + xattr_size;

	list_for_each_xattr(ent, xattr) {
		if ((void *)(ent) + sizeof(__u32) > last_base_addr ||
			(void *)XATTR_NEXT_ENTRY(ent) > last_base_addr) {
			ASSERT_MSG("[0x%x] last xattr entry (offset: %lx) "
					"crosses the boundary",
					nid, (long int)((void *)ent - xattr));
			need_fix = true;
			break;
		}
	}
	if (!need_fix &&
	    !is_zeroed((u8 *)ent, (u8 *)last_base_addr - (u8 *)ent)) {
		ASSERT_MSG("[0x%x] nonzero bytes in xattr space after "
				"end of list", nid);
		need_fix = true;
	}
	if (need_fix && c.fix_on) {
		memset(ent, 0, (u8 *)last_base_addr - (u8 *)ent);
		write_all_xattrs(sbi, inode, xattr_size, xattr);
		FIX_MSG("[0x%x] nullify wrong xattr entries", nid);
		free(xattr);
		return 1;
	}
	free(xattr);
	return 0;
}

/* start with valid nid and blkaddr */
void fsck_chk_inode_blk(struct f2fs_sb_info *sbi, u32 nid,
		enum FILE_TYPE ftype, struct f2fs_node *node_blk,
		u32 *blk_cnt, struct f2fs_compr_blk_cnt *cbc,
		struct node_info *ni, struct child_info *child_d)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	struct child_info child;
	enum NODE_TYPE ntype;
	u32 i_links = le32_to_cpu(node_blk->i.i_links);
	u64 i_size = le64_to_cpu(node_blk->i.i_size);
	u64 i_blocks = le64_to_cpu(node_blk->i.i_blocks);
	bool compr_supported = c.feature & cpu_to_le32(F2FS_FEATURE_COMPRESSION);
	u32 i_flags = le32_to_cpu(node_blk->i.i_flags);
	bool compressed = i_flags & F2FS_COMPR_FL;
	bool compr_rel = node_blk->i.i_inline & F2FS_COMPRESS_RELEASED;
	u64 i_compr_blocks = le64_to_cpu(node_blk->i.i_compr_blocks);
	nid_t i_xattr_nid = le32_to_cpu(node_blk->i.i_xattr_nid);
	int ofs;
	char *en;
	u32 namelen;
	unsigned int addrs, idx = 0;
	unsigned short i_gc_failures;
	int need_fix = 0;
	int ret;
	u32 cluster_size = 1 << node_blk->i.i_log_cluster_size;

	if (!compressed)
		goto check_next;

	if (!compr_supported || (node_blk->i.i_inline & F2FS_INLINE_DATA)) {
		/*
		 * The 'compression' flag in i_flags affects the traverse of
		 * the node tree.  Thus, it must be fixed unconditionally
		 * in the memory (node_blk).
		 */
		node_blk->i.i_flags &= ~cpu_to_le32(F2FS_COMPR_FL);
		compressed = false;
		if (c.fix_on) {
			need_fix = 1;
			FIX_MSG("[0x%x] i_flags=0x%x -> 0x%x",
					nid, i_flags, node_blk->i.i_flags);
		}
		i_flags &= ~F2FS_COMPR_FL;
	}
check_next:
	memset(&child, 0, sizeof(child));
	child.links = 2;
	child.p_ino = nid;
	child.pp_ino = le32_to_cpu(node_blk->i.i_pino);
	child.dir_level = node_blk->i.i_dir_level;

	if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0)
		fsck->chk.valid_inode_cnt++;

	if (ftype == F2FS_FT_DIR) {
		f2fs_set_main_bitmap(sbi, ni->blk_addr, CURSEG_HOT_NODE);
		namelen = le32_to_cpu(node_blk->i.i_namelen);
		if (namelen > F2FS_NAME_LEN)
			namelen = F2FS_NAME_LEN;
		memcpy(child.p_name, node_blk->i.i_name, namelen);
	} else {
		if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0) {
			f2fs_set_main_bitmap(sbi, ni->blk_addr,
							CURSEG_WARM_NODE);
			if (i_links > 1 && ftype != F2FS_FT_ORPHAN &&
					!is_qf_ino(F2FS_RAW_SUPER(sbi), nid)) {
				/* First time. Create new hard link node */
				add_into_hard_link_list(sbi, nid, i_links);
				fsck->chk.multi_hard_link_files++;
			}
		} else {
			DBG(3, "[0x%x] has hard links [0x%x]\n", nid, i_links);
			if (find_and_dec_hard_link_list(sbi, nid)) {
				ASSERT_MSG("[0x%x] needs more i_links=0x%x",
						nid, i_links);
				if (c.fix_on) {
					node_blk->i.i_links =
						cpu_to_le32(i_links + 1);
					need_fix = 1;
					FIX_MSG("File: 0x%x "
						"i_links= 0x%x -> 0x%x",
						nid, i_links, i_links + 1);
				}
				goto skip_blkcnt_fix;
			}
			/* No need to go deep into the node */
			return;
		}
	}

	/* readahead xattr node block */
	fsck_reada_node_block(sbi, i_xattr_nid);

	if (fsck_chk_xattr_blk(sbi, nid, i_xattr_nid, blk_cnt)) {
		if (c.fix_on) {
			node_blk->i.i_xattr_nid = 0;
			need_fix = 1;
			FIX_MSG("Remove xattr block: 0x%x, x_nid = 0x%x",
							nid, i_xattr_nid);
		}
	}

	if (ftype == F2FS_FT_CHRDEV || ftype == F2FS_FT_BLKDEV ||
			ftype == F2FS_FT_FIFO || ftype == F2FS_FT_SOCK)
		goto check;

	/* init extent info */
	get_extent_info(&child.ei, &node_blk->i.i_ext);
	child.last_blk = 0;

	if (f2fs_has_extra_isize(&node_blk->i)) {
		if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
			unsigned int isize =
				le16_to_cpu(node_blk->i.i_extra_isize);
			if (isize > 4 * DEF_ADDRS_PER_INODE) {
				ASSERT_MSG("[0x%x] wrong i_extra_isize=0x%x",
						nid, isize);
				if (c.fix_on) {
					FIX_MSG("ino[0x%x] recover i_extra_isize "
						"from %u to %u",
						nid, isize,
						calc_extra_isize());
					node_blk->i.i_extra_isize =
						cpu_to_le16(calc_extra_isize());
					need_fix = 1;
				}
			}
		} else {
			ASSERT_MSG("[0x%x] wrong extra_attr flag", nid);
			if (c.fix_on) {
				FIX_MSG("ino[0x%x] remove F2FS_EXTRA_ATTR "
					"flag in i_inline:%u",
					nid, node_blk->i.i_inline);
				/* we don't support tuning F2FS_FEATURE_EXTRA_ATTR now */
				node_blk->i.i_inline &= ~F2FS_EXTRA_ATTR;
				need_fix = 1;
			}
		}

		if ((c.feature &
			cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR)) &&
			(node_blk->i.i_inline & F2FS_INLINE_XATTR)) {
			unsigned int inline_size =
				le16_to_cpu(node_blk->i.i_inline_xattr_size);

			if (!inline_size ||
					inline_size > MAX_INLINE_XATTR_SIZE) {
				ASSERT_MSG("[0x%x] wrong inline_xattr_size:%u",
						nid, inline_size);
				if (c.fix_on) {
					FIX_MSG("ino[0x%x] recover inline xattr size "
						"from %u to %u",
						nid, inline_size,
						DEFAULT_INLINE_XATTR_ADDRS);
					node_blk->i.i_inline_xattr_size =
						cpu_to_le16(DEFAULT_INLINE_XATTR_ADDRS);
					need_fix = 1;
				}
			}
		}
	}
	ofs = get_extra_isize(node_blk);

	if ((node_blk->i.i_flags & cpu_to_le32(F2FS_CASEFOLD_FL)) &&
	    (ftype != F2FS_FT_DIR ||
	     !(c.feature & cpu_to_le32(F2FS_FEATURE_CASEFOLD)))) {
		ASSERT_MSG("[0x%x] unexpected casefold flag", nid);
		if (c.fix_on) {
			FIX_MSG("ino[0x%x] clear casefold flag", nid);
			node_blk->i.i_flags &= ~cpu_to_le32(F2FS_CASEFOLD_FL);
			need_fix = 1;
		}
	}

	if (chk_extended_attributes(sbi, nid, node_blk))
		need_fix = 1;

	if ((node_blk->i.i_inline & F2FS_INLINE_DATA)) {
		unsigned int inline_size = MAX_INLINE_DATA(node_blk);
		if (cur_qtype != -1)
			qf_szchk_type[cur_qtype] = QF_SZCHK_INLINE;
		block_t blkaddr = le32_to_cpu(node_blk->i.i_addr[ofs]);

		if (blkaddr != 0) {
			ASSERT_MSG("[0x%x] wrong inline reserve blkaddr:%u",
					nid, blkaddr);
			if (c.fix_on) {
				FIX_MSG("inline_data has wrong 0'th block = %x",
								blkaddr);
				node_blk->i.i_addr[ofs] = 0;
				node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
				need_fix = 1;
			}
		}
		if (i_size > inline_size) {
			ASSERT_MSG("[0x%x] wrong inline size:%lu",
					nid, (unsigned long)i_size);
			if (c.fix_on) {
				node_blk->i.i_size = cpu_to_le64(inline_size);
				FIX_MSG("inline_data has wrong i_size %lu",
							(unsigned long)i_size);
				need_fix = 1;
			}
		}
		if (!(node_blk->i.i_inline & F2FS_DATA_EXIST)) {
			char buf[MAX_INLINE_DATA(node_blk)];
			memset(buf, 0, MAX_INLINE_DATA(node_blk));

			if (memcmp(buf, inline_data_addr(node_blk),
						MAX_INLINE_DATA(node_blk))) {
				ASSERT_MSG("[0x%x] junk inline data", nid);
				if (c.fix_on) {
					FIX_MSG("inline_data has DATA_EXIST");
					node_blk->i.i_inline |= F2FS_DATA_EXIST;
					need_fix = 1;
				}
			}
		}
		DBG(3, "ino[0x%x] has inline data!\n", nid);
		child.state |= FSCK_INLINE_INODE;
		goto check;
	}

	if ((node_blk->i.i_inline & F2FS_INLINE_DENTRY)) {
		block_t blkaddr = le32_to_cpu(node_blk->i.i_addr[ofs]);

		DBG(3, "ino[0x%x] has inline dentry!\n", nid);
		if (blkaddr != 0) {
			ASSERT_MSG("[0x%x] wrong inline reserve blkaddr:%u",
								nid, blkaddr);
			if (c.fix_on) {
				FIX_MSG("inline_dentry has wrong 0'th block = %x",
								blkaddr);
				node_blk->i.i_addr[ofs] = 0;
				node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
				need_fix = 1;
			}
		}

		ret = fsck_chk_inline_dentries(sbi, node_blk, &child);
		if (ret < 0) {
			if (c.fix_on)
				need_fix = 1;
		}
		child.state |= FSCK_INLINE_INODE;
		goto check;
	}

	/* check data blocks in inode */
	addrs = ADDRS_PER_INODE(&node_blk->i);
	if (cur_qtype != -1) {
		u64 addrs_per_blk = (u64)ADDRS_PER_BLOCK(&node_blk->i);
		qf_szchk_type[cur_qtype] = QF_SZCHK_REGFILE;
		qf_maxsize[cur_qtype] = (u64)(addrs + 2 * addrs_per_blk +
				2 * addrs_per_blk * NIDS_PER_BLOCK +
				addrs_per_blk * NIDS_PER_BLOCK *
				NIDS_PER_BLOCK) * F2FS_BLKSIZE;
	}
	for (idx = 0; idx < addrs; idx++, child.pgofs++) {
		block_t blkaddr = le32_to_cpu(node_blk->i.i_addr[ofs + idx]);

		/* check extent info */
		check_extent_info(&child, blkaddr, 0);

		if (blkaddr == NULL_ADDR)
			continue;
		if (blkaddr == COMPRESS_ADDR) {
			if (!compressed || (child.pgofs &
					(cluster_size - 1)) != 0) {
				if (c.fix_on) {
					node_blk->i.i_addr[ofs + idx] =
							NULL_ADDR;
					need_fix = 1;
					FIX_MSG("[0x%x] i_addr[%d] = 0", nid,
							ofs + idx);
				}
				continue;
			}
			if (!compr_rel) {
				fsck->chk.valid_blk_cnt++;
				*blk_cnt = *blk_cnt + 1;
				cbc->cheader_pgofs = child.pgofs;
				cbc->cnt++;
			}
			continue;
		}
		if (!compr_rel && blkaddr == NEW_ADDR &&
				child.pgofs - cbc->cheader_pgofs < cluster_size)
			cbc->cnt++;
		ret = fsck_chk_data_blk(sbi,
				IS_CASEFOLDED(&node_blk->i),
				blkaddr,
				&child, (i_blocks == *blk_cnt),
				ftype, nid, idx, ni->version,
				file_is_encrypt(&node_blk->i));
		if (!ret) {
			*blk_cnt = *blk_cnt + 1;
			if (cur_qtype != -1 && blkaddr != NEW_ADDR)
				qf_last_blkofs[cur_qtype] = child.pgofs;
		} else if (c.fix_on) {
			node_blk->i.i_addr[ofs + idx] = 0;
			need_fix = 1;
			FIX_MSG("[0x%x] i_addr[%d] = 0", nid, ofs + idx);
		}
	}

	/* readahead node blocks */
	for (idx = 0; idx < 5; idx++) {
		u32 nid = le32_to_cpu(node_blk->i.i_nid[idx]);
		fsck_reada_node_block(sbi, nid);
	}

	/* check node blocks in inode */
	for (idx = 0; idx < 5; idx++) {
		nid_t i_nid = le32_to_cpu(node_blk->i.i_nid[idx]);

		if (idx == 0 || idx == 1)
			ntype = TYPE_DIRECT_NODE;
		else if (idx == 2 || idx == 3)
			ntype = TYPE_INDIRECT_NODE;
		else if (idx == 4)
			ntype = TYPE_DOUBLE_INDIRECT_NODE;
		else
			ASSERT(0);

		if (i_nid == 0x0)
			goto skip;

		ret = fsck_chk_node_blk(sbi, &node_blk->i, i_nid,
				ftype, ntype, blk_cnt, cbc, &child);
		if (!ret) {
			*blk_cnt = *blk_cnt + 1;
		} else if (ret == -EINVAL) {
			if (c.fix_on) {
				node_blk->i.i_nid[idx] = 0;
				need_fix = 1;
				FIX_MSG("[0x%x] i_nid[%d] = 0", nid, idx);
			}
skip:
			if (ntype == TYPE_DIRECT_NODE)
				child.pgofs += ADDRS_PER_BLOCK(&node_blk->i);
			else if (ntype == TYPE_INDIRECT_NODE)
				child.pgofs += ADDRS_PER_BLOCK(&node_blk->i) *
								NIDS_PER_BLOCK;
			else
				child.pgofs += ADDRS_PER_BLOCK(&node_blk->i) *
						NIDS_PER_BLOCK * NIDS_PER_BLOCK;
		}

	}

check:
	/* check uncovered range in the back of extent */
	check_extent_info(&child, 0, 1);

	if (child.state & FSCK_UNMATCHED_EXTENT) {
		ASSERT_MSG("ino: 0x%x has wrong ext: [pgofs:%u, blk:%u, len:%u]",
				nid, child.ei.fofs, child.ei.blk, child.ei.len);
		if (c.fix_on)
			need_fix = 1;
	}

	if (i_blocks != *blk_cnt) {
		ASSERT_MSG("ino: 0x%x has i_blocks: %08"PRIx64", "
				"but has %u blocks",
				nid, i_blocks, *blk_cnt);
		if (c.fix_on) {
			node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
			need_fix = 1;
			FIX_MSG("[0x%x] i_blocks=0x%08"PRIx64" -> 0x%x",
					nid, i_blocks, *blk_cnt);
		}
	}

	if (compressed && i_compr_blocks != cbc->cnt) {
		if (c.fix_on) {
			node_blk->i.i_compr_blocks = cpu_to_le64(cbc->cnt);
			need_fix = 1;
			FIX_MSG("[0x%x] i_compr_blocks=0x%08"PRIx64" -> 0x%x",
					nid, i_compr_blocks, cbc->cnt);
		}
	}

skip_blkcnt_fix:
	en = malloc(F2FS_PRINT_NAMELEN);
	ASSERT(en);

	namelen = le32_to_cpu(node_blk->i.i_namelen);
	if (namelen > F2FS_NAME_LEN) {
		if (child_d && child_d->i_namelen <= F2FS_NAME_LEN) {
			ASSERT_MSG("ino: 0x%x has i_namelen: 0x%x, "
					"but has %d characters for name",
					nid, namelen, child_d->i_namelen);
			if (c.fix_on) {
				FIX_MSG("[0x%x] i_namelen=0x%x -> 0x%x", nid, namelen,
					child_d->i_namelen);
				node_blk->i.i_namelen = cpu_to_le32(child_d->i_namelen);
				need_fix = 1;
			}
			namelen = child_d->i_namelen;
		} else
			namelen = F2FS_NAME_LEN;
	}
	pretty_print_filename(node_blk->i.i_name, namelen, en,
			      file_enc_name(&node_blk->i));
	if (ftype == F2FS_FT_ORPHAN)
		DBG(1, "Orphan Inode: 0x%x [%s] i_blocks: %u\n\n",
				le32_to_cpu(node_blk->footer.ino),
				en, (u32)i_blocks);

	if (is_qf_ino(F2FS_RAW_SUPER(sbi), nid))
		DBG(1, "Quota Inode: 0x%x [%s] i_blocks: %u\n\n",
				le32_to_cpu(node_blk->footer.ino),
				en, (u32)i_blocks);

	if (ftype == F2FS_FT_DIR) {
		DBG(1, "Directory Inode: 0x%x [%s] depth: %d has %d files\n\n",
				le32_to_cpu(node_blk->footer.ino), en,
				le32_to_cpu(node_blk->i.i_current_depth),
				child.files);

		if (i_links != child.links) {
			ASSERT_MSG("ino: 0x%x i_links: %u, real links: %u",
					nid, i_links, child.links);
			if (c.fix_on) {
				node_blk->i.i_links = cpu_to_le32(child.links);
				need_fix = 1;
				FIX_MSG("Dir: 0x%x i_links= 0x%x -> 0x%x",
						nid, i_links, child.links);
			}
		}
		if (child.dots < 2 &&
				!(node_blk->i.i_inline & F2FS_INLINE_DOTS)) {
			ASSERT_MSG("ino: 0x%x dots: %u",
					nid, child.dots);
			if (c.fix_on) {
				node_blk->i.i_inline |= F2FS_INLINE_DOTS;
				need_fix = 1;
				FIX_MSG("Dir: 0x%x set inline_dots", nid);
			}
		}
	}

	i_gc_failures = le16_to_cpu(node_blk->i.i_gc_failures);

	/*
	 * old kernel initialized i_gc_failures as 0x01, in preen mode 2,
	 * let's skip repairing.
	 */
	if (ftype == F2FS_FT_REG_FILE && i_gc_failures &&
		(c.preen_mode != PREEN_MODE_2 || i_gc_failures != 0x01)) {

		DBG(1, "Regular Inode: 0x%x [%s] depth: %d\n\n",
				le32_to_cpu(node_blk->footer.ino), en,
				i_gc_failures);

		if (c.fix_on) {
			node_blk->i.i_gc_failures = cpu_to_le16(0);
			need_fix = 1;
			FIX_MSG("Regular: 0x%x reset i_gc_failures from 0x%x to 0x00",
					nid, i_gc_failures);
		}
	}

	free(en);

	if (ftype == F2FS_FT_SYMLINK && i_size == 0 &&
			i_blocks == (i_xattr_nid ? 3 : 2)) {
		node_blk->i.i_size = cpu_to_le64(F2FS_BLKSIZE);
		need_fix = 1;
		FIX_MSG("Symlink: recover 0x%x with i_size=%lu",
					nid, (unsigned long)F2FS_BLKSIZE);
	}

	if (ftype == F2FS_FT_ORPHAN && i_links) {
		ASSERT_MSG("ino: 0x%x is orphan inode, but has i_links: %u",
				nid, i_links);
		if (c.fix_on) {
			node_blk->i.i_links = 0;
			need_fix = 1;
			FIX_MSG("ino: 0x%x orphan_inode, i_links= 0x%x -> 0",
					nid, i_links);
		}
	}

	/* drop extent information to avoid potential wrong access */
	if (need_fix && f2fs_dev_is_writable())
		node_blk->i.i_ext.len = 0;

	if ((c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM)) &&
				f2fs_has_extra_isize(&node_blk->i)) {
		__u32 provided, calculated;

		provided = le32_to_cpu(node_blk->i.i_inode_checksum);
		calculated = f2fs_inode_chksum(node_blk);

		if (provided != calculated) {
			ASSERT_MSG("ino: 0x%x chksum:0x%x, but calculated one is: 0x%x",
				nid, provided, calculated);
			if (c.fix_on) {
				node_blk->i.i_inode_checksum =
							cpu_to_le32(calculated);
				need_fix = 1;
				FIX_MSG("ino: 0x%x recover, i_inode_checksum= 0x%x -> 0x%x",
						nid, provided, calculated);
			}
		}
	}

	if (need_fix && f2fs_dev_is_writable()) {
		ret = dev_write_block(node_blk, ni->blk_addr);
		ASSERT(ret >= 0);
	}
}

int fsck_chk_dnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
		u32 nid, enum FILE_TYPE ftype, struct f2fs_node *node_blk,
		u32 *blk_cnt, struct f2fs_compr_blk_cnt *cbc,
		struct child_info *child, struct node_info *ni)
{
	int idx, ret;
	int need_fix = 0;
	child->p_ino = nid;
	child->pp_ino = le32_to_cpu(inode->i_pino);
	u32 i_flags = le32_to_cpu(inode->i_flags);
	bool compressed = i_flags & F2FS_COMPR_FL;
	bool compr_rel = inode->i_inline & F2FS_COMPRESS_RELEASED;
	u32 cluster_size = 1 << inode->i_log_cluster_size;

	for (idx = 0; idx < ADDRS_PER_BLOCK(inode); idx++, child->pgofs++) {
		block_t blkaddr = le32_to_cpu(node_blk->dn.addr[idx]);

		check_extent_info(child, blkaddr, 0);

		if (blkaddr == NULL_ADDR)
			continue;
		if (blkaddr == COMPRESS_ADDR) {
			if (!compressed || (child->pgofs &
					(cluster_size - 1)) != 0) {
				if (c.fix_on) {
					node_blk->dn.addr[idx] = NULL_ADDR;
					need_fix = 1;
					FIX_MSG("[0x%x] dn.addr[%d] = 0", nid,
							idx);
				}
				continue;
			}
			if (!compr_rel) {
				F2FS_FSCK(sbi)->chk.valid_blk_cnt++;
				*blk_cnt = *blk_cnt + 1;
				cbc->cheader_pgofs = child->pgofs;
				cbc->cnt++;
			}
			continue;
		}
		if (!compr_rel && blkaddr == NEW_ADDR && child->pgofs -
				cbc->cheader_pgofs < cluster_size)
			cbc->cnt++;
		ret = fsck_chk_data_blk(sbi, IS_CASEFOLDED(inode),
			blkaddr, child,
			le64_to_cpu(inode->i_blocks) == *blk_cnt, ftype,
			nid, idx, ni->version,
			file_is_encrypt(inode));
		if (!ret) {
			*blk_cnt = *blk_cnt + 1;
			if (cur_qtype != -1 && blkaddr != NEW_ADDR)
				qf_last_blkofs[cur_qtype] = child->pgofs;
		} else if (c.fix_on) {
			node_blk->dn.addr[idx] = NULL_ADDR;
			need_fix = 1;
			FIX_MSG("[0x%x] dn.addr[%d] = 0", nid, idx);
		}
	}
	if (need_fix && f2fs_dev_is_writable()) {
		ret = dev_write_block(node_blk, ni->blk_addr);
		ASSERT(ret >= 0);
	}
	return 0;
}

int fsck_chk_idnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
		enum FILE_TYPE ftype, struct f2fs_node *node_blk, u32 *blk_cnt,
		struct f2fs_compr_blk_cnt *cbc, struct child_info *child)
{
	int need_fix = 0, ret;
	int i = 0;

	fsck_reada_all_direct_node_blocks(sbi, node_blk);

	for (i = 0; i < NIDS_PER_BLOCK; i++) {
		if (le32_to_cpu(node_blk->in.nid[i]) == 0x0)
			goto skip;
		ret = fsck_chk_node_blk(sbi, inode,
				le32_to_cpu(node_blk->in.nid[i]),
				ftype, TYPE_DIRECT_NODE, blk_cnt,
				cbc, child);
		if (!ret)
			*blk_cnt = *blk_cnt + 1;
		else if (ret == -EINVAL) {
			if (!c.fix_on)
				printf("should delete in.nid[i] = 0;\n");
			else {
				node_blk->in.nid[i] = 0;
				need_fix = 1;
				FIX_MSG("Set indirect node 0x%x -> 0", i);
			}
skip:
			child->pgofs += ADDRS_PER_BLOCK(&node_blk->i);
		}
	}

	if (need_fix && f2fs_dev_is_writable()) {
		struct node_info ni;
		nid_t nid = le32_to_cpu(node_blk->footer.nid);

		get_node_info(sbi, nid, &ni);
		ret = dev_write_block(node_blk, ni.blk_addr);
		ASSERT(ret >= 0);
	}

	return 0;
}

int fsck_chk_didnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
		enum FILE_TYPE ftype, struct f2fs_node *node_blk, u32 *blk_cnt,
		struct f2fs_compr_blk_cnt *cbc, struct child_info *child)
{
	int i = 0;
	int need_fix = 0, ret = 0;

	fsck_reada_all_direct_node_blocks(sbi, node_blk);

	for (i = 0; i < NIDS_PER_BLOCK; i++) {
		if (le32_to_cpu(node_blk->in.nid[i]) == 0x0)
			goto skip;
		ret = fsck_chk_node_blk(sbi, inode,
				le32_to_cpu(node_blk->in.nid[i]),
				ftype, TYPE_INDIRECT_NODE, blk_cnt, cbc, child);
		if (!ret)
			*blk_cnt = *blk_cnt + 1;
		else if (ret == -EINVAL) {
			if (!c.fix_on)
				printf("should delete in.nid[i] = 0;\n");
			else {
				node_blk->in.nid[i] = 0;
				need_fix = 1;
				FIX_MSG("Set double indirect node 0x%x -> 0", i);
			}
skip:
			child->pgofs += ADDRS_PER_BLOCK(&node_blk->i) *
							NIDS_PER_BLOCK;
		}
	}

	if (need_fix && f2fs_dev_is_writable()) {
		struct node_info ni;
		nid_t nid = le32_to_cpu(node_blk->footer.nid);

		get_node_info(sbi, nid, &ni);
		ret = dev_write_block(node_blk, ni.blk_addr);
		ASSERT(ret >= 0);
	}

	return 0;
}

static const char *lookup_table =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";

/**
 * base64_encode() -
 *
 * Encodes the input string using characters from the set [A-Za-z0-9+,].
 * The encoded string is roughly 4/3 times the size of the input string.
 */
static int base64_encode(const u8 *src, int len, char *dst)
{
	int i, bits = 0, ac = 0;
	char *cp = dst;

	for (i = 0; i < len; i++) {
		ac += src[i] << bits;
		bits += 8;
		do {
			*cp++ = lookup_table[ac & 0x3f];
			ac >>= 6;
			bits -= 6;
		} while (bits >= 6);
	}
	if (bits)
		*cp++ = lookup_table[ac & 0x3f];
	return cp - dst;
}

void pretty_print_filename(const u8 *raw_name, u32 len,
			   char out[F2FS_PRINT_NAMELEN], int enc_name)
{
	len = min(len, (u32)F2FS_NAME_LEN);

	if (enc_name)
		len = base64_encode(raw_name, len, out);
	else
		memcpy(out, raw_name, len);
	out[len] = 0;
}

static void print_dentry(struct f2fs_sb_info *sbi, __u8 *name,
		u8 *bitmap, struct f2fs_dir_entry *dentry,
		int max, int idx, int last_blk, int enc_name)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	u32 depth = fsck->dentry_depth;
	int last_de = 0;
	int next_idx = 0;
	u32 name_len;
	unsigned int i;
	int bit_offset;
	char new[F2FS_PRINT_NAMELEN];

	if (!c.show_dentry && !c.show_file_map)
		return;

	name_len = le16_to_cpu(dentry[idx].name_len);
	next_idx = idx + (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;

	bit_offset = find_next_bit_le(bitmap, max, next_idx);
	if (bit_offset >= max && last_blk)
		last_de = 1;

	if (tree_mark_size <= depth) {
		tree_mark_size *= 2;
		ASSERT(tree_mark_size != 0);
		tree_mark = realloc(tree_mark, tree_mark_size);
		ASSERT(tree_mark != NULL);
	}

	if (last_de)
		tree_mark[depth] = '`';
	else
		tree_mark[depth] = '|';

	if (tree_mark[depth - 1] == '`')
		tree_mark[depth - 1] = ' ';

	pretty_print_filename(name, name_len, new, enc_name);

	if (c.show_file_map) {
		struct f2fs_dentry *d = fsck->dentry;

		if (dentry[idx].file_type != F2FS_FT_REG_FILE)
			return;

		while (d) {
			if (d->depth > 1)
				printf("/%s", d->name);
			d = d->next;
		}
		printf("/%s", new);
		if (dump_node(sbi, le32_to_cpu(dentry[idx].ino), 0))
			printf("\33[2K\r");
	} else {
		for (i = 1; i < depth; i++)
			printf("%c   ", tree_mark[i]);

		printf("%c-- %s <ino = 0x%x>, <encrypted (%d)>\n",
			last_de ? '`' : '|',
			new, le32_to_cpu(dentry[idx].ino),
			enc_name);
	}
}

static int f2fs_check_hash_code(int encoding, int casefolded,
			struct f2fs_dir_entry *dentry,
			const unsigned char *name, u32 len, int enc_name)
{
	/* Casefolded Encrypted names require a key to compute siphash */
	if (enc_name && casefolded)
		return 0;

	f2fs_hash_t hash_code = f2fs_dentry_hash(encoding, casefolded, name, len);
	/* fix hash_code made by old buggy code */
	if (dentry->hash_code != hash_code) {
		char new[F2FS_PRINT_NAMELEN];

		pretty_print_filename(name, len, new, enc_name);
		FIX_MSG("Mismatch hash_code for \"%s\" [%x:%x]",
				new, le32_to_cpu(dentry->hash_code),
				hash_code);
		dentry->hash_code = cpu_to_le32(hash_code);
		return 1;
	}
	return 0;
}


static int __get_current_level(int dir_level, u32 pgofs)
{
	unsigned int bidx = 0;
	int i;

	for (i = 0; i < MAX_DIR_HASH_DEPTH; i++) {
		bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
		if (bidx > pgofs)
			break;
	}
	return i;
}

static int f2fs_check_dirent_position(const struct f2fs_dir_entry *dentry,
				      const char *printable_name,
				      u32 pgofs, u8 dir_level, u32 pino)
{
	unsigned int nbucket, nblock;
	unsigned int bidx, end_block;
	int level;

	level = __get_current_level(dir_level, pgofs);

	nbucket = dir_buckets(level, dir_level);
	nblock = bucket_blocks(level);

	bidx = dir_block_index(level, dir_level,
			       le32_to_cpu(dentry->hash_code) % nbucket);
	end_block = bidx + nblock;

	if (pgofs >= bidx && pgofs < end_block)
		return 0;

	ASSERT_MSG("Wrong position of dirent pino:%u, name:%s, level:%d, "
		"dir_level:%d, pgofs:%u, correct range:[%u, %u]\n",
		pino, printable_name, level, dir_level, pgofs, bidx,
		end_block - 1);
	return 1;
}

static int __chk_dots_dentries(struct f2fs_sb_info *sbi,
			       int casefolded,
			       struct f2fs_dir_entry *dentry,
			       struct child_info *child,
			       u8 *name, int len,
			       __u8 (*filename)[F2FS_SLOT_LEN],
			       int enc_name)
{
	int fixed = 0;

	if ((name[0] == '.' && len == 1)) {
		if (le32_to_cpu(dentry->ino) != child->p_ino) {
			ASSERT_MSG("Bad inode number[0x%x] for '.', parent_ino is [0x%x]\n",
				le32_to_cpu(dentry->ino), child->p_ino);
			dentry->ino = cpu_to_le32(child->p_ino);
			fixed = 1;
		}
	}

	if (name[0] == '.' && name[1] == '.' && len == 2) {
		if (child->p_ino == F2FS_ROOT_INO(sbi)) {
			if (le32_to_cpu(dentry->ino) != F2FS_ROOT_INO(sbi)) {
				ASSERT_MSG("Bad inode number[0x%x] for '..'\n",
					le32_to_cpu(dentry->ino));
				dentry->ino = cpu_to_le32(F2FS_ROOT_INO(sbi));
				fixed = 1;
			}
		} else if (le32_to_cpu(dentry->ino) != child->pp_ino) {
			ASSERT_MSG("Bad inode number[0x%x] for '..', parent parent ino is [0x%x]\n",
				le32_to_cpu(dentry->ino), child->pp_ino);
			dentry->ino = cpu_to_le32(child->pp_ino);
			fixed = 1;
		}
	}

	if (f2fs_check_hash_code(get_encoding(sbi), casefolded, dentry, name, len, enc_name))
		fixed = 1;

	if (name[len] != '\0') {
		ASSERT_MSG("'.' is not NULL terminated\n");
		name[len] = '\0';
		memcpy(*filename, name, len);
		fixed = 1;
	}
	return fixed;
}

static void nullify_dentry(struct f2fs_dir_entry *dentry, int offs,
			   __u8 (*filename)[F2FS_SLOT_LEN], u8 **bitmap)
{
	memset(dentry, 0, sizeof(struct f2fs_dir_entry));
	test_and_clear_bit_le(offs, *bitmap);
	memset(*filename, 0, F2FS_SLOT_LEN);
}

static int __chk_dentries(struct f2fs_sb_info *sbi, int casefolded,
			struct child_info *child,
			u8 *bitmap, struct f2fs_dir_entry *dentry,
			__u8 (*filenames)[F2FS_SLOT_LEN],
			int max, int last_blk, int enc_name)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	enum FILE_TYPE ftype;
	int dentries = 0;
	u32 blk_cnt;
	struct f2fs_compr_blk_cnt cbc;
	u8 *name;
	char en[F2FS_PRINT_NAMELEN];
	u16 name_len;
	int ret = 0;
	int fixed = 0;
	int i, slots;

	/* readahead inode blocks */
	for (i = 0; i < max; i++) {
		u32 ino;

		if (test_bit_le(i, bitmap) == 0)
			continue;

		ino = le32_to_cpu(dentry[i].ino);

		if (IS_VALID_NID(sbi, ino)) {
			struct node_info ni;

			get_node_info(sbi, ino, &ni);
			if (IS_VALID_BLK_ADDR(sbi, ni.blk_addr)) {
				dev_reada_block(ni.blk_addr);
				name_len = le16_to_cpu(dentry[i].name_len);
				if (name_len > 0)
					i += (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN - 1;
			}
		}
	}

	for (i = 0; i < max;) {
		if (test_bit_le(i, bitmap) == 0) {
			i++;
			continue;
		}
		if (!IS_VALID_NID(sbi, le32_to_cpu(dentry[i].ino))) {
			ASSERT_MSG("Bad dentry 0x%x with invalid NID/ino 0x%x",
				    i, le32_to_cpu(dentry[i].ino));
			if (c.fix_on) {
				FIX_MSG("Clear bad dentry 0x%x with bad ino 0x%x",
					i, le32_to_cpu(dentry[i].ino));
				test_and_clear_bit_le(i, bitmap);
				fixed = 1;
			}
			i++;
			continue;
		}

		ftype = dentry[i].file_type;
		if ((ftype <= F2FS_FT_UNKNOWN || ftype > F2FS_FT_LAST_FILE_TYPE)) {
			ASSERT_MSG("Bad dentry 0x%x with unexpected ftype 0x%x",
						le32_to_cpu(dentry[i].ino), ftype);
			if (c.fix_on) {
				FIX_MSG("Clear bad dentry 0x%x with bad ftype 0x%x",
					i, ftype);
				test_and_clear_bit_le(i, bitmap);
				fixed = 1;
			}
			i++;
			continue;
		}

		name_len = le16_to_cpu(dentry[i].name_len);

		if (name_len == 0 || name_len > F2FS_NAME_LEN) {
			ASSERT_MSG("Bad dentry 0x%x with invalid name_len", i);
			if (c.fix_on) {
				FIX_MSG("Clear bad dentry 0x%x", i);
				test_and_clear_bit_le(i, bitmap);
				fixed = 1;
			}
			i++;
			continue;
		}
		name = calloc(name_len + 1, 1);
		ASSERT(name);

		memcpy(name, filenames[i], name_len);
		slots = (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;

		/* Becareful. 'dentry.file_type' is not imode. */
		if (ftype == F2FS_FT_DIR) {
			if ((name[0] == '.' && name_len == 1) ||
				(name[0] == '.' && name[1] == '.' &&
							name_len == 2)) {
				ret = __chk_dots_dentries(sbi, casefolded, &dentry[i],
					child, name, name_len, &filenames[i],
					enc_name);
				switch (ret) {
				case 1:
					fixed = 1;
					fallthrough;
				case 0:
					child->dots++;
					break;
				}

				if (child->dots > 2) {
					ASSERT_MSG("More than one '.' or '..', should delete the extra one\n");
					nullify_dentry(&dentry[i], i,
						       &filenames[i], &bitmap);
					child->dots--;
					fixed = 1;
				}

				i++;
				free(name);
				continue;
			}
		}

		if (f2fs_check_hash_code(get_encoding(sbi), casefolded, dentry + i, name, name_len, enc_name))
			fixed = 1;

		pretty_print_filename(name, name_len, en, enc_name);

		if (max == NR_DENTRY_IN_BLOCK) {
			ret = f2fs_check_dirent_position(dentry + i, en,
					child->pgofs, child->dir_level,
					child->p_ino);
			if (ret) {
				if (c.fix_on) {
					FIX_MSG("Clear bad dentry 0x%x", i);
					test_and_clear_bit_le(i, bitmap);
					fixed = 1;
				}
				i++;
				free(name);
				continue;
			}
		}

		DBG(1, "[%3u]-[0x%x] name[%s] len[0x%x] ino[0x%x] type[0x%x]\n",
				fsck->dentry_depth, i, en, name_len,
				le32_to_cpu(dentry[i].ino),
				dentry[i].file_type);

		print_dentry(sbi, name, bitmap,
				dentry, max, i, last_blk, enc_name);

		blk_cnt = 1;
		cbc.cnt = 0;
		cbc.cheader_pgofs = CHEADER_PGOFS_NONE;
		child->i_namelen = name_len;
		ret = fsck_chk_node_blk(sbi,
				NULL, le32_to_cpu(dentry[i].ino),
				ftype, TYPE_INODE, &blk_cnt, &cbc, child);

		if (ret && c.fix_on) {
			int j;

			for (j = 0; j < slots; j++)
				test_and_clear_bit_le(i + j, bitmap);
			FIX_MSG("Unlink [0x%x] - %s len[0x%x], type[0x%x]",
					le32_to_cpu(dentry[i].ino),
					en, name_len,
					dentry[i].file_type);
			fixed = 1;
		} else if (ret == 0) {
			if (ftype == F2FS_FT_DIR)
				child->links++;
			dentries++;
			child->files++;
		}

		i += slots;
		free(name);
	}
	return fixed ? -1 : dentries;
}

int fsck_chk_inline_dentries(struct f2fs_sb_info *sbi,
		struct f2fs_node *node_blk, struct child_info *child)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	struct f2fs_dentry *cur_dentry = fsck->dentry_end;
	struct f2fs_dentry *new_dentry;
	struct f2fs_dentry_ptr d;
	void *inline_dentry;
	int dentries;

	inline_dentry = inline_data_addr(node_blk);
	ASSERT(inline_dentry != NULL);

	make_dentry_ptr(&d, node_blk, inline_dentry, 2);

	fsck->dentry_depth++;
	new_dentry = calloc(sizeof(struct f2fs_dentry), 1);
	ASSERT(new_dentry != NULL);

	new_dentry->depth = fsck->dentry_depth;
	memcpy(new_dentry->name, child->p_name, F2FS_NAME_LEN);
	cur_dentry->next = new_dentry;
	fsck->dentry_end = new_dentry;

	dentries = __chk_dentries(sbi, IS_CASEFOLDED(&node_blk->i), child,
			d.bitmap, d.dentry, d.filename, d.max, 1,
			file_is_encrypt(&node_blk->i));// pass through
	if (dentries < 0) {
		DBG(1, "[%3d] Inline Dentry Block Fixed hash_codes\n\n",
			fsck->dentry_depth);
	} else {
		DBG(1, "[%3d] Inline Dentry Block Done : "
				"dentries:%d in %d slots (len:%d)\n\n",
			fsck->dentry_depth, dentries,
			d.max, F2FS_NAME_LEN);
	}
	fsck->dentry = cur_dentry;
	fsck->dentry_end = cur_dentry;
	cur_dentry->next = NULL;
	free(new_dentry);
	fsck->dentry_depth--;
	return dentries;
}

int fsck_chk_dentry_blk(struct f2fs_sb_info *sbi, int casefolded, u32 blk_addr,
		struct child_info *child, int last_blk, int enc_name)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	struct f2fs_dentry_block *de_blk;
	struct f2fs_dentry *cur_dentry = fsck->dentry_end;
	struct f2fs_dentry *new_dentry;
	int dentries, ret;

	de_blk = (struct f2fs_dentry_block *)calloc(BLOCK_SZ, 1);
	ASSERT(de_blk != NULL);

	ret = dev_read_block(de_blk, blk_addr);
	ASSERT(ret >= 0);

	fsck->dentry_depth++;
	new_dentry = calloc(sizeof(struct f2fs_dentry), 1);
	ASSERT(new_dentry != NULL);
	new_dentry->depth = fsck->dentry_depth;
	memcpy(new_dentry->name, child->p_name, F2FS_NAME_LEN);
	cur_dentry->next = new_dentry;
	fsck->dentry_end = new_dentry;

	dentries = __chk_dentries(sbi, casefolded, child,
			de_blk->dentry_bitmap,
			de_blk->dentry, de_blk->filename,
			NR_DENTRY_IN_BLOCK, last_blk, enc_name);

	if (dentries < 0 && f2fs_dev_is_writable()) {
		ret = dev_write_block(de_blk, blk_addr);
		ASSERT(ret >= 0);
		DBG(1, "[%3d] Dentry Block [0x%x] Fixed hash_codes\n\n",
			fsck->dentry_depth, blk_addr);
	} else {
		DBG(1, "[%3d] Dentry Block [0x%x] Done : "
				"dentries:%d in %d slots (len:%d)\n\n",
			fsck->dentry_depth, blk_addr, dentries,
			NR_DENTRY_IN_BLOCK, F2FS_NAME_LEN);
	}
	fsck->dentry = cur_dentry;
	fsck->dentry_end = cur_dentry;
	cur_dentry->next = NULL;
	free(new_dentry);
	fsck->dentry_depth--;
	free(de_blk);
	return 0;
}

int fsck_chk_data_blk(struct f2fs_sb_info *sbi, int casefolded,
		u32 blk_addr, struct child_info *child, int last_blk,
		enum FILE_TYPE ftype, u32 parent_nid, u16 idx_in_node, u8 ver,
		int enc_name)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);

	/* Is it reserved block? */
	if (blk_addr == NEW_ADDR) {
		fsck->chk.valid_blk_cnt++;
		return 0;
	}

	if (!IS_VALID_BLK_ADDR(sbi, blk_addr)) {
		ASSERT_MSG("blkaddress is not valid. [0x%x]", blk_addr);
		return -EINVAL;
	}

	if (is_valid_ssa_data_blk(sbi, blk_addr, parent_nid,
						idx_in_node, ver)) {
		ASSERT_MSG("summary data block is not valid. [0x%x]",
						parent_nid);
		return -EINVAL;
	}

	if (f2fs_test_sit_bitmap(sbi, blk_addr) == 0)
		ASSERT_MSG("SIT bitmap is 0x0. blk_addr[0x%x]", blk_addr);

	if (f2fs_test_main_bitmap(sbi, blk_addr) != 0)
		ASSERT_MSG("Duplicated data [0x%x]. pnid[0x%x] idx[0x%x]",
				blk_addr, parent_nid, idx_in_node);

	fsck->chk.valid_blk_cnt++;

	if (ftype == F2FS_FT_DIR) {
		f2fs_set_main_bitmap(sbi, blk_addr, CURSEG_HOT_DATA);
		return fsck_chk_dentry_blk(sbi, casefolded, blk_addr, child,
						last_blk, enc_name);
	} else {
		f2fs_set_main_bitmap(sbi, blk_addr, CURSEG_WARM_DATA);
	}
	return 0;
}

int fsck_chk_orphan_node(struct f2fs_sb_info *sbi)
{
	u32 blk_cnt = 0;
	struct f2fs_compr_blk_cnt cbc = {0, CHEADER_PGOFS_NONE};
	block_t start_blk, orphan_blkaddr, i, j;
	struct f2fs_orphan_block *orphan_blk, *new_blk;
	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
	u32 entry_count;

	if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
		return 0;

	start_blk = __start_cp_addr(sbi) + 1 + get_sb(cp_payload);
	orphan_blkaddr = __start_sum_addr(sbi) - 1 - get_sb(cp_payload);

	f2fs_ra_meta_pages(sbi, start_blk, orphan_blkaddr, META_CP);

	orphan_blk = calloc(BLOCK_SZ, 1);
	ASSERT(orphan_blk);

	new_blk = calloc(BLOCK_SZ, 1);
	ASSERT(new_blk);

	for (i = 0; i < orphan_blkaddr; i++) {
		int ret = dev_read_block(orphan_blk, start_blk + i);
		u32 new_entry_count = 0;

		ASSERT(ret >= 0);
		entry_count = le32_to_cpu(orphan_blk->entry_count);

		for (j = 0; j < entry_count; j++) {
			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
			DBG(1, "[%3d] ino [0x%x]\n", i, ino);
			struct node_info ni;
			blk_cnt = 1;
			cbc.cnt = 0;
			cbc.cheader_pgofs = CHEADER_PGOFS_NONE;

			if (c.preen_mode == PREEN_MODE_1 && !c.fix_on) {
				get_node_info(sbi, ino, &ni);
				if (!IS_VALID_NID(sbi, ino) ||
				    !IS_VALID_BLK_ADDR(sbi, ni.blk_addr)) {
					free(orphan_blk);
					free(new_blk);
					return -EINVAL;
				}

				continue;
			}

			ret = fsck_chk_node_blk(sbi, NULL, ino,
					F2FS_FT_ORPHAN, TYPE_INODE, &blk_cnt,
					&cbc, NULL);
			if (!ret)
				new_blk->ino[new_entry_count++] =
							orphan_blk->ino[j];
			else if (ret && c.fix_on)
				FIX_MSG("[0x%x] remove from orphan list", ino);
			else if (ret)
				ASSERT_MSG("[0x%x] wrong orphan inode", ino);
		}
		if (f2fs_dev_is_writable() && c.fix_on &&
				entry_count != new_entry_count) {
			new_blk->entry_count = cpu_to_le32(new_entry_count);
			ret = dev_write_block(new_blk, start_blk + i);
			ASSERT(ret >= 0);
		}
		memset(orphan_blk, 0, BLOCK_SZ);
		memset(new_blk, 0, BLOCK_SZ);
	}
	free(orphan_blk);
	free(new_blk);

	return 0;
}

int fsck_chk_quota_node(struct f2fs_sb_info *sbi)
{
	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
	enum quota_type qtype;
	int ret = 0;
	u32 blk_cnt = 0;
	struct f2fs_compr_blk_cnt cbc = {0, CHEADER_PGOFS_NONE};

	for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
		cur_qtype = qtype;
		if (sb->qf_ino[qtype] == 0)
			continue;
		nid_t ino = QUOTA_INO(sb, qtype);
		struct node_info ni;

		DBG(1, "qtype [%d] ino [0x%x]\n", qtype, ino);
		blk_cnt = 1;
		cbc.cnt = 0;
		cbc.cheader_pgofs = CHEADER_PGOFS_NONE;

		if (c.preen_mode == PREEN_MODE_1 && !c.fix_on) {
			get_node_info(sbi, ino, &ni);
			if (!IS_VALID_NID(sbi, ino) ||
					!IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
				return -EINVAL;
			continue;
		}
		ret = fsck_chk_node_blk(sbi, NULL, ino,
				F2FS_FT_REG_FILE, TYPE_INODE, &blk_cnt,
				&cbc, NULL);
		if (ret) {
			ASSERT_MSG("wrong quota inode, qtype [%d] ino [0x%x]",
								qtype, ino);
			qf_szchk_type[qtype] = QF_SZCHK_ERR;
			if (c.fix_on)
				f2fs_rebuild_qf_inode(sbi, qtype);
		}
	}
	cur_qtype = -1;
	return ret;
}

int fsck_chk_quota_files(struct f2fs_sb_info *sbi)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
	enum quota_type qtype;
	f2fs_ino_t ino;
	int ret = 0;
	int needs_writeout;

	/* Return if quota feature is disabled */
	if (!fsck->qctx)
		return 0;

	for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
		ino = sb->qf_ino[qtype];
		if (!ino)
			continue;

	        DBG(1, "Checking Quota file ([%3d] ino [0x%x])\n", qtype, ino);
		needs_writeout = 0;
		ret = quota_compare_and_update(sbi, qtype, &needs_writeout,
						c.preserve_limits);
		if (ret == 0 && needs_writeout == 0) {
			DBG(1, "OK\n");
			continue;
		}

		/* Something is wrong */
		if (c.fix_on) {
			DBG(0, "Fixing Quota file ([%3d] ino [0x%x])\n",
							qtype, ino);
			f2fs_filesize_update(sbi, ino, 0);
			ret = quota_write_inode(sbi, qtype);
			if (!ret) {
				c.quota_fixed = true;
				DBG(1, "OK\n");
			} else {
				ASSERT_MSG("Unable to write quota file");
			}
		} else {
			ASSERT_MSG("Quota file is missing or invalid"
					" quota file content found.");
		}
	}
	return ret;
}

int fsck_chk_meta(struct f2fs_sb_info *sbi)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
	struct seg_entry *se;
	unsigned int sit_valid_segs = 0, sit_node_blks = 0;
	unsigned int i;

	/* 1. check sit usage with CP: curseg is lost? */
	for (i = 0; i < MAIN_SEGS(sbi); i++) {
		se = get_seg_entry(sbi, i);
		if (se->valid_blocks != 0)
			sit_valid_segs++;
		else if (IS_CUR_SEGNO(sbi, i)) {
			/* curseg has not been written back to device */
			MSG(1, "\tInfo: curseg %u is counted in valid segs\n", i);
			sit_valid_segs++;
		}
		if (IS_NODESEG(se->type))
			sit_node_blks += se->valid_blocks;
	}
	if (fsck->chk.sit_free_segs + sit_valid_segs !=
				get_usable_seg_count(sbi)) {
		ASSERT_MSG("SIT usage does not match: sit_free_segs %u, "
				"sit_valid_segs %u, total_segs %u",
			fsck->chk.sit_free_segs, sit_valid_segs,
			get_usable_seg_count(sbi));
		return -EINVAL;
	}

	/* 2. check node count */
	if (fsck->chk.valid_nat_entry_cnt != sit_node_blks) {
		ASSERT_MSG("node count does not match: valid_nat_entry_cnt %u,"
			" sit_node_blks %u",
			fsck->chk.valid_nat_entry_cnt, sit_node_blks);
		return -EINVAL;
	}

	/* 3. check SIT with CP */
	if (fsck->chk.sit_free_segs != le32_to_cpu(cp->free_segment_count)) {
		ASSERT_MSG("free segs does not match: sit_free_segs %u, "
				"free_segment_count %u",
				fsck->chk.sit_free_segs,
				le32_to_cpu(cp->free_segment_count));
		return -EINVAL;
	}

	/* 4. check NAT with CP */
	if (fsck->chk.valid_nat_entry_cnt !=
					le32_to_cpu(cp->valid_node_count)) {
		ASSERT_MSG("valid node does not match: valid_nat_entry_cnt %u,"
				" valid_node_count %u",
				fsck->chk.valid_nat_entry_cnt,
				le32_to_cpu(cp->valid_node_count));
		return -EINVAL;
	}

	/* 4. check orphan inode simply */
	if (fsck_chk_orphan_node(sbi))
		return -EINVAL;

	/* 5. check nat entry -- must be done before quota check */
	for (i = 0; i < fsck->nr_nat_entries; i++) {
		u32 blk = le32_to_cpu(fsck->entries[i].block_addr);
		nid_t ino = le32_to_cpu(fsck->entries[i].ino);

		if (!blk)
			/*
			 * skip entry whose ino is 0, otherwise, we will
			 * get a negative number by BLKOFF_FROM_MAIN(sbi, blk)
			 */
			continue;

		if (!IS_VALID_BLK_ADDR(sbi, blk)) {
			MSG(0, "\tError: nat entry[ino %u block_addr 0x%x]"
				" is in valid\n",
				ino, blk);
			return -EINVAL;
		}

		if (!f2fs_test_sit_bitmap(sbi, blk)) {
			MSG(0, "\tError: nat entry[ino %u block_addr 0x%x]"
				" not find it in sit_area_bitmap\n",
				ino, blk);
			return -EINVAL;
		}

		if (!IS_VALID_NID(sbi, ino)) {
			MSG(0, "\tError: nat_entry->ino %u exceeds the range"
				" of nat entries %u\n",
				ino, fsck->nr_nat_entries);
			return -EINVAL;
		}

		if (!f2fs_test_bit(ino, fsck->nat_area_bitmap)) {
			MSG(0, "\tError: nat_entry->ino %u is not set in"
				" nat_area_bitmap\n", ino);
			return -EINVAL;
		}
	}

	/* 6. check quota inode simply */
	if (fsck_chk_quota_node(sbi))
		return -EINVAL;

	if (fsck->nat_valid_inode_cnt != le32_to_cpu(cp->valid_inode_count)) {
		ASSERT_MSG("valid inode does not match: nat_valid_inode_cnt %u,"
				" valid_inode_count %u",
				fsck->nat_valid_inode_cnt,
				le32_to_cpu(cp->valid_inode_count));
		return -EINVAL;
	}

	return 0;
}

void fsck_chk_checkpoint(struct f2fs_sb_info *sbi)
{
	struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);

	if (get_cp(ckpt_flags) & CP_LARGE_NAT_BITMAP_FLAG) {
		if (get_cp(checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
			ASSERT_MSG("Deprecated layout of large_nat_bitmap, "
				"chksum_offset:%u", get_cp(checksum_offset));
			c.fix_chksum = 1;
		}
	}
}

void fsck_init(struct f2fs_sb_info *sbi)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	struct f2fs_sm_info *sm_i = SM_I(sbi);

	/*
	 * We build three bitmap for main/sit/nat so that may check consistency
	 * of filesystem.
	 * 1. main_area_bitmap will be used to check whether all blocks of main
	 *    area is used or not.
	 * 2. nat_area_bitmap has bitmap information of used nid in NAT.
	 * 3. sit_area_bitmap has bitmap information of used main block.
	 * At Last sequence, we compare main_area_bitmap with sit_area_bitmap.
	 */
	fsck->nr_main_blks = sm_i->main_segments << sbi->log_blocks_per_seg;
	fsck->main_area_bitmap_sz = (fsck->nr_main_blks + 7) / 8;
	fsck->main_area_bitmap = calloc(fsck->main_area_bitmap_sz, 1);
	ASSERT(fsck->main_area_bitmap != NULL);

	build_nat_area_bitmap(sbi);

	build_sit_area_bitmap(sbi);

	ASSERT(tree_mark_size != 0);
	tree_mark = calloc(tree_mark_size, 1);
	ASSERT(tree_mark != NULL);
	fsck->dentry = calloc(sizeof(struct f2fs_dentry), 1);
	ASSERT(fsck->dentry != NULL);
	memcpy(fsck->dentry->name, "/", 1);
	fsck->dentry_end = fsck->dentry;

	c.quota_fixed = false;
}

static void fix_hard_links(struct f2fs_sb_info *sbi)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	struct hard_link_node *tmp, *node;
	struct f2fs_node *node_blk = NULL;
	struct node_info ni;
	int ret;

	if (fsck->hard_link_list_head == NULL)
		return;

	node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
	ASSERT(node_blk != NULL);

	node = fsck->hard_link_list_head;
	while (node) {
		/* Sanity check */
		if (sanity_check_nid(sbi, node->nid, node_blk,
					F2FS_FT_MAX, TYPE_INODE, &ni))
			FIX_MSG("Failed to fix, rerun fsck.f2fs");

		node_blk->i.i_links = cpu_to_le32(node->actual_links);

		FIX_MSG("File: 0x%x i_links= 0x%x -> 0x%x",
				node->nid, node->links, node->actual_links);

		ret = dev_write_block(node_blk, ni.blk_addr);
		ASSERT(ret >= 0);
		tmp = node;
		node = node->next;
		free(tmp);
	}
	free(node_blk);
}

static void fix_nat_entries(struct f2fs_sb_info *sbi)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	u32 i;

	for (i = 0; i < fsck->nr_nat_entries; i++)
		if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0)
			nullify_nat_entry(sbi, i);
}

static void flush_curseg_sit_entries(struct f2fs_sb_info *sbi)
{
	struct sit_info *sit_i = SIT_I(sbi);
	struct f2fs_sit_block *sit_blk;
	int i;

	sit_blk = calloc(BLOCK_SZ, 1);
	ASSERT(sit_blk);
	/* update curseg sit entries, since we may change
	 * a segment type in move_curseg_info
	 */
	for (i = 0; i < NO_CHECK_TYPE; i++) {
		struct curseg_info *curseg = CURSEG_I(sbi, i);
		struct f2fs_sit_entry *sit;
		struct seg_entry *se;

		se = get_seg_entry(sbi, curseg->segno);
		get_current_sit_page(sbi, curseg->segno, sit_blk);
		sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, curseg->segno)];
		sit->vblocks = cpu_to_le16((se->type << SIT_VBLOCKS_SHIFT) |
							se->valid_blocks);
		rewrite_current_sit_page(sbi, curseg->segno, sit_blk);
	}

	free(sit_blk);
}

static void fix_checksum(struct f2fs_sb_info *sbi)
{
	struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
	struct f2fs_nm_info *nm_i = NM_I(sbi);
	struct sit_info *sit_i = SIT_I(sbi);
	void *bitmap_offset;

	if (!c.fix_chksum)
		return;

	bitmap_offset = cp->sit_nat_version_bitmap + sizeof(__le32);

	memcpy(bitmap_offset, nm_i->nat_bitmap, nm_i->bitmap_size);
	memcpy(bitmap_offset + nm_i->bitmap_size,
			sit_i->sit_bitmap, sit_i->bitmap_size);
}

static void fix_checkpoint(struct f2fs_sb_info *sbi)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
	struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
	unsigned long long cp_blk_no;
	u32 flags = c.alloc_failed ? CP_FSCK_FLAG: CP_UMOUNT_FLAG;
	block_t orphan_blks = 0;
	block_t cp_blocks;
	u32 i;
	int ret;
	uint32_t crc = 0;

	/* should call from fsck */
	ASSERT(c.func == FSCK);

	if (is_set_ckpt_flags(cp, CP_ORPHAN_PRESENT_FLAG)) {
		orphan_blks = __start_sum_addr(sbi) - 1;
		flags |= CP_ORPHAN_PRESENT_FLAG;
	}
	if (is_set_ckpt_flags(cp, CP_TRIMMED_FLAG))
		flags |= CP_TRIMMED_FLAG;
	if (is_set_ckpt_flags(cp, CP_DISABLED_FLAG))
		flags |= CP_DISABLED_FLAG;
	if (is_set_ckpt_flags(cp, CP_LARGE_NAT_BITMAP_FLAG)) {
		flags |= CP_LARGE_NAT_BITMAP_FLAG;
		set_cp(checksum_offset, CP_MIN_CHKSUM_OFFSET);
	} else {
		set_cp(checksum_offset, CP_CHKSUM_OFFSET);
	}

	if (flags & CP_UMOUNT_FLAG)
		cp_blocks = 8;
	else
		cp_blocks = 5;

	set_cp(cp_pack_total_block_count, cp_blocks +
				orphan_blks + get_sb(cp_payload));

	flags = update_nat_bits_flags(sb, cp, flags);
	flags |= CP_NOCRC_RECOVERY_FLAG;
	set_cp(ckpt_flags, flags);

	set_cp(free_segment_count, get_free_segments(sbi));
	set_cp(valid_block_count, fsck->chk.valid_blk_cnt);
	set_cp(valid_node_count, fsck->chk.valid_node_cnt);
	set_cp(valid_inode_count, fsck->chk.valid_inode_cnt);

	crc = f2fs_checkpoint_chksum(cp);
	*((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
							cpu_to_le32(crc);

	cp_blk_no = get_sb(cp_blkaddr);
	if (sbi->cur_cp == 2)
		cp_blk_no += 1 << get_sb(log_blocks_per_seg);

	ret = dev_write_block(cp, cp_blk_no++);
	ASSERT(ret >= 0);

	for (i = 0; i < get_sb(cp_payload); i++) {
		ret = dev_write_block(((unsigned char *)cp) +
					(i + 1) * F2FS_BLKSIZE, cp_blk_no++);
		ASSERT(ret >= 0);
	}

	cp_blk_no += orphan_blks;

	for (i = 0; i < NO_CHECK_TYPE; i++) {
		struct curseg_info *curseg = CURSEG_I(sbi, i);

		if (!(flags & CP_UMOUNT_FLAG) && IS_NODESEG(i))
			continue;

		ret = dev_write_block(curseg->sum_blk, cp_blk_no++);
		ASSERT(ret >= 0);
	}

	/* Write nat bits */
	if (flags & CP_NAT_BITS_FLAG)
		write_nat_bits(sbi, sb, cp, sbi->cur_cp);

	ret = f2fs_fsync_device();
	ASSERT(ret >= 0);

	ret = dev_write_block(cp, cp_blk_no++);
	ASSERT(ret >= 0);

	ret = f2fs_fsync_device();
	ASSERT(ret >= 0);
}

static void fix_checkpoints(struct f2fs_sb_info *sbi)
{
	/* copy valid checkpoint to its mirror position */
	duplicate_checkpoint(sbi);

	/* repair checkpoint at CP #0 position */
	sbi->cur_cp = 1;
	fix_checkpoint(sbi);
}

#ifdef HAVE_LINUX_BLKZONED_H

/*
 * Refer valid block map and return offset of the last valid block in the zone.
 * Obtain valid block map from SIT and fsync data.
 * If there is no valid block in the zone, return -1.
 */
static int last_vblk_off_in_zone(struct f2fs_sb_info *sbi,
				 unsigned int zone_segno)
{
	int s, b;
	unsigned int segs_per_zone = sbi->segs_per_sec * sbi->secs_per_zone;
	struct seg_entry *se;

	for (s = segs_per_zone - 1; s >= 0; s--) {
		se = get_seg_entry(sbi, zone_segno + s);

		/*
		 * Refer not cur_valid_map but ckpt_valid_map which reflects
		 * fsync data.
		 */
		ASSERT(se->ckpt_valid_map);
		for (b = sbi->blocks_per_seg - 1; b >= 0; b--)
			if (f2fs_test_bit(b, (const char*)se->ckpt_valid_map))
				return b + (s << sbi->log_blocks_per_seg);
	}

	return -1;
}

static int check_curseg_write_pointer(struct f2fs_sb_info *sbi, int type)
{
	struct curseg_info *curseg = CURSEG_I(sbi, type);
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	struct blk_zone blkz;
	block_t cs_block, wp_block, zone_last_vblock;
	uint64_t cs_sector, wp_sector;
	int i, ret;
	unsigned int zone_segno;
	int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;

	/* get the device the curseg points to */
	cs_block = START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff;
	for (i = 0; i < MAX_DEVICES; i++) {
		if (!c.devices[i].path)
			break;
		if (c.devices[i].start_blkaddr <= cs_block &&
		    cs_block <= c.devices[i].end_blkaddr)
			break;
	}

	if (i >= MAX_DEVICES)
		return -EINVAL;

	if (c.devices[i].zoned_model != F2FS_ZONED_HM)
		return 0;

	/* get write pointer position of the zone the curseg points to */
	cs_sector = (cs_block - c.devices[i].start_blkaddr)
		<< log_sectors_per_block;
	ret = f2fs_report_zone(i, cs_sector, &blkz);
	if (ret)
		return ret;

	if (blk_zone_type(&blkz) != BLK_ZONE_TYPE_SEQWRITE_REQ)
		return 0;

	/* check consistency between the curseg and the write pointer */
	wp_block = c.devices[i].start_blkaddr +
		(blk_zone_wp_sector(&blkz) >> log_sectors_per_block);
	wp_sector = blk_zone_wp_sector(&blkz);

	if (cs_sector == wp_sector)
		return 0;

	if (cs_sector > wp_sector) {
		MSG(0, "Inconsistent write pointer with curseg %d: "
		    "curseg %d[0x%x,0x%x] > wp[0x%x,0x%x]\n",
		    type, type, curseg->segno, curseg->next_blkoff,
		    GET_SEGNO(sbi, wp_block), OFFSET_IN_SEG(sbi, wp_block));
		fsck->chk.wp_inconsistent_zones++;
		return -EINVAL;
	}

	MSG(0, "Write pointer goes advance from curseg %d: "
	    "curseg %d[0x%x,0x%x] wp[0x%x,0x%x]\n",
	    type, type, curseg->segno, curseg->next_blkoff,
	    GET_SEGNO(sbi, wp_block), OFFSET_IN_SEG(sbi, wp_block));

	zone_segno = GET_SEG_FROM_SEC(sbi,
				      GET_SEC_FROM_SEG(sbi, curseg->segno));
	zone_last_vblock = START_BLOCK(sbi, zone_segno) +
		last_vblk_off_in_zone(sbi, zone_segno);

	/*
	 * If valid blocks exist between the curseg position and the write
	 * pointer, they are fsync data. This is not an error to fix. Leave it
	 * for kernel to recover later.
	 * If valid blocks exist between the curseg's zone start and the curseg
	 * position, or if there is no valid block in the curseg's zone, fix
	 * the inconsistency between the curseg and the writ pointer.
	 * Of Note is that if there is no valid block in the curseg's zone,
	 * last_vblk_off_in_zone() returns -1 and zone_last_vblock is always
	 * smaller than cs_block.
	 */
	if (cs_block <= zone_last_vblock && zone_last_vblock < wp_block) {
		MSG(0, "Curseg has fsync data: curseg %d[0x%x,0x%x] "
		    "last valid block in zone[0x%x,0x%x]\n",
		    type, curseg->segno, curseg->next_blkoff,
		    GET_SEGNO(sbi, zone_last_vblock),
		    OFFSET_IN_SEG(sbi, zone_last_vblock));
		return 0;
	}

	fsck->chk.wp_inconsistent_zones++;
	return -EINVAL;
}

#else

static int check_curseg_write_pointer(struct f2fs_sb_info *UNUSED(sbi),
					int UNUSED(type))
{
	return 0;
}

#endif

int check_curseg_offset(struct f2fs_sb_info *sbi, int type)
{
	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
	struct curseg_info *curseg = CURSEG_I(sbi, type);
	struct seg_entry *se;
	int j, nblocks;

	if (get_sb(feature) & cpu_to_le32(F2FS_FEATURE_RO) &&
			type != CURSEG_HOT_DATA && type != CURSEG_HOT_NODE)
		return 0;

	if ((curseg->next_blkoff >> 3) >= SIT_VBLOCK_MAP_SIZE) {
		ASSERT_MSG("Next block offset:%u is invalid, type:%d",
			curseg->next_blkoff, type);
		return -EINVAL;
	}
	se = get_seg_entry(sbi, curseg->segno);
	if (f2fs_test_bit(curseg->next_blkoff,
				(const char *)se->cur_valid_map)) {
		ASSERT_MSG("Next block offset is not free, type:%d", type);
		return -EINVAL;
	}
	if (curseg->alloc_type == SSR)
		return 0;

	nblocks = sbi->blocks_per_seg;
	for (j = curseg->next_blkoff + 1; j < nblocks; j++) {
		if (f2fs_test_bit(j, (const char *)se->cur_valid_map)) {
			ASSERT_MSG("For LFS curseg, space after .next_blkoff "
				"should be unused, type:%d", type);
			return -EINVAL;
		}
	}

	if (c.zoned_model == F2FS_ZONED_HM)
		return check_curseg_write_pointer(sbi, type);

	return 0;
}

int check_curseg_offsets(struct f2fs_sb_info *sbi)
{
	int i, ret;

	for (i = 0; i < NO_CHECK_TYPE; i++) {
		ret = check_curseg_offset(sbi, i);
		if (ret)
			return ret;
	}
	return 0;
}

static void fix_curseg_info(struct f2fs_sb_info *sbi)
{
	int i, need_update = 0;

	for (i = 0; i < NO_CHECK_TYPE; i++) {
		if (check_curseg_offset(sbi, i)) {
			update_curseg_info(sbi, i);
			need_update = 1;
		}
	}

	if (need_update) {
		write_curseg_info(sbi);
		flush_curseg_sit_entries(sbi);
	}
}

int check_sit_types(struct f2fs_sb_info *sbi)
{
	unsigned int i;
	int err = 0;

	for (i = 0; i < MAIN_SEGS(sbi); i++) {
		struct seg_entry *se;

		se = get_seg_entry(sbi, i);
		if (se->orig_type != se->type) {
			if (se->orig_type == CURSEG_COLD_DATA &&
					se->type <= CURSEG_COLD_DATA) {
				se->type = se->orig_type;
			} else {
				FIX_MSG("Wrong segment type [0x%x] %x -> %x",
						i, se->orig_type, se->type);
				err = -EINVAL;
			}
		}
	}
	return err;
}

static struct f2fs_node *fsck_get_lpf(struct f2fs_sb_info *sbi)
{
	struct f2fs_node *node;
	struct node_info ni;
	nid_t lpf_ino;
	int err;

	/* read root inode first */
	node = calloc(F2FS_BLKSIZE, 1);
	ASSERT(node);
	get_node_info(sbi, F2FS_ROOT_INO(sbi), &ni);
	err = dev_read_block(node, ni.blk_addr);
	ASSERT(err >= 0);

	/* lookup lost+found in root directory */
	lpf_ino = f2fs_lookup(sbi, node, (u8 *)LPF, strlen(LPF));
	if (lpf_ino) { /* found */
		get_node_info(sbi, lpf_ino, &ni);
		err = dev_read_block(node, ni.blk_addr);
		ASSERT(err >= 0);
		DBG(1, "Found lost+found 0x%x at blkaddr [0x%x]\n",
		    lpf_ino, ni.blk_addr);
		if (!S_ISDIR(le16_to_cpu(node->i.i_mode))) {
			ASSERT_MSG("lost+found is not directory [0%o]\n",
				   le16_to_cpu(node->i.i_mode));
			/* FIXME: give up? */
			goto out;
		}
	} else { /* not found, create it */
		struct dentry de;

		memset(&de, 0, sizeof(de));
		de.name = (u8 *) LPF;
		de.len = strlen(LPF);
		de.mode = 0x41c0;
		de.pino = F2FS_ROOT_INO(sbi),
		de.file_type = F2FS_FT_DIR,
		de.uid = getuid();
		de.gid = getgid();
		de.mtime = time(NULL);

		err = f2fs_mkdir(sbi, &de);
		if (err) {
			ASSERT_MSG("Failed create lost+found");
			goto out;
		}

		get_node_info(sbi, de.ino, &ni);
		err = dev_read_block(node, ni.blk_addr);
		ASSERT(err >= 0);
		DBG(1, "Create lost+found 0x%x at blkaddr [0x%x]\n",
		    de.ino, ni.blk_addr);
	}

	c.lpf_ino = le32_to_cpu(node->footer.ino);
	return node;
out:
	free(node);
	return NULL;
}

static int fsck_do_reconnect_file(struct f2fs_sb_info *sbi,
				  struct f2fs_node *lpf,
				  struct f2fs_node *fnode)
{
	char name[80];
	size_t namelen;
	nid_t ino = le32_to_cpu(fnode->footer.ino);
	struct node_info ni;
	int ftype, ret;

	namelen = snprintf(name, 80, "%u", ino);
	if (namelen >= 80)
		/* ignore terminating '\0', should never happen */
		namelen = 79;

	if (f2fs_lookup(sbi, lpf, (u8 *)name, namelen)) {
		ASSERT_MSG("Name %s already exist in lost+found", name);
		return -EEXIST;
	}

	get_node_info(sbi, le32_to_cpu(lpf->footer.ino), &ni);
	ftype = map_de_type(le16_to_cpu(fnode->i.i_mode));
	ret = f2fs_add_link(sbi, lpf, (unsigned char *)name, namelen,
			    ino, ftype, ni.blk_addr, 0);
	if (ret) {
		ASSERT_MSG("Failed to add inode [0x%x] to lost+found", ino);
		return -EINVAL;
	}

	/* update fnode */
	memcpy(fnode->i.i_name, name, namelen);
	fnode->i.i_namelen = cpu_to_le32(namelen);
	fnode->i.i_pino = c.lpf_ino;
	get_node_info(sbi, le32_to_cpu(fnode->footer.ino), &ni);
	ret = dev_write_block(fnode, ni.blk_addr);
	ASSERT(ret >= 0);

	DBG(1, "Reconnect inode [0x%x] to lost+found\n", ino);
	return 0;
}

static void fsck_failed_reconnect_file_dnode(struct f2fs_sb_info *sbi,
					     nid_t nid)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	struct f2fs_node *node;
	struct node_info ni;
	u32 addr;
	int i, err;

	node = calloc(F2FS_BLKSIZE, 1);
	ASSERT(node);

	get_node_info(sbi, nid, &ni);
	err = dev_read_block(node, ni.blk_addr);
	ASSERT(err >= 0);

	fsck->chk.valid_node_cnt--;
	fsck->chk.valid_blk_cnt--;
	f2fs_clear_main_bitmap(sbi, ni.blk_addr);

	for (i = 0; i < ADDRS_PER_BLOCK(&node->i); i++) {
		addr = le32_to_cpu(node->dn.addr[i]);
		if (!addr)
			continue;
		fsck->chk.valid_blk_cnt--;
		if (addr == NEW_ADDR)
			continue;
		f2fs_clear_main_bitmap(sbi, addr);
	}

	free(node);
}

static void fsck_failed_reconnect_file_idnode(struct f2fs_sb_info *sbi,
					      nid_t nid)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	struct f2fs_node *node;
	struct node_info ni;
	nid_t tmp;
	int i, err;

	node = calloc(F2FS_BLKSIZE, 1);
	ASSERT(node);

	get_node_info(sbi, nid, &ni);
	err = dev_read_block(node, ni.blk_addr);
	ASSERT(err >= 0);

	fsck->chk.valid_node_cnt--;
	fsck->chk.valid_blk_cnt--;
	f2fs_clear_main_bitmap(sbi, ni.blk_addr);

	for (i = 0; i < NIDS_PER_BLOCK; i++) {
		tmp = le32_to_cpu(node->in.nid[i]);
		if (!tmp)
			continue;
		fsck_failed_reconnect_file_dnode(sbi, tmp);
	}

	free(node);
}

static void fsck_failed_reconnect_file_didnode(struct f2fs_sb_info *sbi,
					       nid_t nid)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	struct f2fs_node *node;
	struct node_info ni;
	nid_t tmp;
	int i, err;

	node = calloc(F2FS_BLKSIZE, 1);
	ASSERT(node);

	get_node_info(sbi, nid, &ni);
	err = dev_read_block(node, ni.blk_addr);
	ASSERT(err >= 0);

	fsck->chk.valid_node_cnt--;
	fsck->chk.valid_blk_cnt--;
	f2fs_clear_main_bitmap(sbi, ni.blk_addr);

	for (i = 0; i < NIDS_PER_BLOCK; i++) {
		tmp = le32_to_cpu(node->in.nid[i]);
		if (!tmp)
			continue;
		fsck_failed_reconnect_file_idnode(sbi, tmp);
	}

	free(node);
}

/*
 * Counters and main_area_bitmap are already changed during checking
 * inode block, so clear them. There is no need to clear new blocks
 * allocted to lost+found.
 */
static void fsck_failed_reconnect_file(struct f2fs_sb_info *sbi, nid_t ino)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	struct f2fs_node *node;
	struct node_info ni;
	nid_t nid;
	int ofs, i, err;

	node = calloc(F2FS_BLKSIZE, 1);
	ASSERT(node);

	get_node_info(sbi, ino, &ni);
	err = dev_read_block(node, ni.blk_addr);
	ASSERT(err >= 0);

	/* clear inode counters */
	fsck->chk.valid_inode_cnt--;
	fsck->chk.valid_node_cnt--;
	fsck->chk.valid_blk_cnt--;
	f2fs_clear_main_bitmap(sbi, ni.blk_addr);

	/* clear xnid counters */
	if (node->i.i_xattr_nid) {
		nid = le32_to_cpu(node->i.i_xattr_nid);
		fsck->chk.valid_node_cnt--;
		fsck->chk.valid_blk_cnt--;
		get_node_info(sbi, nid, &ni);
		f2fs_clear_main_bitmap(sbi, ni.blk_addr);
	}

	/* clear data counters */
	if(!(node->i.i_inline & F2FS_INLINE_DATA)) {
		ofs = get_extra_isize(node);
		for (i = 0; i < ADDRS_PER_INODE(&node->i); i++) {
			block_t addr = le32_to_cpu(node->i.i_addr[ofs + i]);
			if (!addr)
				continue;
			fsck->chk.valid_blk_cnt--;
			if (addr == NEW_ADDR)
				continue;
			f2fs_clear_main_bitmap(sbi, addr);
		}
	}

	for (i = 0; i < 5; i++) {
		nid = le32_to_cpu(node->i.i_nid[i]);
		if (!nid)
			continue;

		switch (i) {
		case 0: /* direct node */
		case 1:
			fsck_failed_reconnect_file_dnode(sbi, nid);
			break;
		case 2: /* indirect node */
		case 3:
			fsck_failed_reconnect_file_idnode(sbi, nid);
			break;
		case 4: /* double indirect node */
			fsck_failed_reconnect_file_didnode(sbi, nid);
			break;
		}
	}

	free(node);
}

/*
 * Scan unreachable nids and find only regular file inodes. If these files
 * are not corrupted, reconnect them to lost+found.
 *
 * Since all unreachable nodes are already checked, we can allocate new
 * blocks safely.
 *
 * This function returns the number of files been reconnected.
 */
static int fsck_reconnect_file(struct f2fs_sb_info *sbi)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	struct f2fs_node *lpf_node, *node;
	struct node_info ni;
	char *reconnect_bitmap;
	u32 blk_cnt;
	struct f2fs_compr_blk_cnt cbc;
	nid_t nid;
	int err, cnt = 0, ftype;

	node = calloc(F2FS_BLKSIZE, 1);
	ASSERT(node);

	reconnect_bitmap = calloc(fsck->nat_area_bitmap_sz, 1);
	ASSERT(reconnect_bitmap);

	for (nid = 0; nid < fsck->nr_nat_entries; nid++) {
		if (f2fs_test_bit(nid, fsck->nat_area_bitmap)) {
			if (is_qf_ino(F2FS_RAW_SUPER(sbi), nid)) {
				DBG(1, "Not support quota inode [0x%x]\n",
				    nid);
				continue;
			}

			get_node_info(sbi, nid, &ni);
			err = dev_read_block(node, ni.blk_addr);
			ASSERT(err >= 0);

			/* reconnection will restore these nodes if needed */
			if (node->footer.ino != node->footer.nid) {
				DBG(1, "Not support non-inode node [0x%x]\n",
				    nid);
				continue;
			}

			if (S_ISDIR(le16_to_cpu(node->i.i_mode))) {
				DBG(1, "Not support directory inode [0x%x]\n",
				    nid);
				continue;
			}

			ftype = map_de_type(le16_to_cpu(node->i.i_mode));
			if (sanity_check_nid(sbi, nid, node, ftype,
					     TYPE_INODE, &ni)) {
				ASSERT_MSG("Invalid nid [0x%x]\n", nid);
				continue;
			}

			DBG(1, "Check inode 0x%x\n", nid);
			blk_cnt = 1;
			cbc.cnt = 0;
			cbc.cheader_pgofs = CHEADER_PGOFS_NONE;
			fsck_chk_inode_blk(sbi, nid, ftype, node,
					   &blk_cnt, &cbc, &ni, NULL);

			f2fs_set_bit(nid, reconnect_bitmap);
		}
	}

	lpf_node = fsck_get_lpf(sbi);
	if (!lpf_node)
		goto out;

	for (nid = 0; nid < fsck->nr_nat_entries; nid++) {
		if (f2fs_test_bit(nid, reconnect_bitmap)) {
			get_node_info(sbi, nid, &ni);
			err = dev_read_block(node, ni.blk_addr);
			ASSERT(err >= 0);

			if (fsck_do_reconnect_file(sbi, lpf_node, node)) {
				DBG(1, "Failed to reconnect inode [0x%x]\n",
				    nid);
				fsck_failed_reconnect_file(sbi, nid);
				continue;
			}

			quota_add_inode_usage(fsck->qctx, nid, &node->i);

			DBG(1, "Reconnected inode [0x%x] to lost+found\n", nid);
			cnt++;
		}
	}

out:
	free(node);
	free(lpf_node);
	free(reconnect_bitmap);
	return cnt;
}

#ifdef HAVE_LINUX_BLKZONED_H

struct write_pointer_check_data {
	struct f2fs_sb_info *sbi;
	int dev_index;
};

static int chk_and_fix_wp_with_sit(int UNUSED(i), void *blkzone, void *opaque)
{
	struct blk_zone *blkz = (struct blk_zone *)blkzone;
	struct write_pointer_check_data *wpd = opaque;
	struct f2fs_sb_info *sbi = wpd->sbi;
	struct device_info *dev = c.devices + wpd->dev_index;
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	block_t zone_block, wp_block, wp_blkoff;
	unsigned int zone_segno, wp_segno;
	struct curseg_info *cs;
	int cs_index, ret, last_valid_blkoff;
	int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
	unsigned int segs_per_zone = sbi->segs_per_sec * sbi->secs_per_zone;

	if (blk_zone_conv(blkz))
		return 0;

	zone_block = dev->start_blkaddr
		+ (blk_zone_sector(blkz) >> log_sectors_per_block);
	zone_segno = GET_SEGNO(sbi, zone_block);
	if (zone_segno >= MAIN_SEGS(sbi))
		return 0;

	wp_block = dev->start_blkaddr
		+ (blk_zone_wp_sector(blkz) >> log_sectors_per_block);
	wp_segno = GET_SEGNO(sbi, wp_block);
	wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno);

	/* if a curseg points to the zone, skip the check */
	for (cs_index = 0; cs_index < NO_CHECK_TYPE; cs_index++) {
		cs = &SM_I(sbi)->curseg_array[cs_index];
		if (zone_segno <= cs->segno &&
		    cs->segno < zone_segno + segs_per_zone)
			return 0;
	}

	last_valid_blkoff = last_vblk_off_in_zone(sbi, zone_segno);

	/*
	 * When there is no valid block in the zone, check write pointer is
	 * at zone start. If not, reset the write pointer.
	 */
	if (last_valid_blkoff < 0 &&
	    blk_zone_wp_sector(blkz) != blk_zone_sector(blkz)) {
		if (!c.fix_on) {
			MSG(0, "Inconsistent write pointer: wp[0x%x,0x%x]\n",
			    wp_segno, wp_blkoff);
			fsck->chk.wp_inconsistent_zones++;
			return 0;
		}

		FIX_MSG("Reset write pointer of zone at segment 0x%x",
			zone_segno);
		ret = f2fs_reset_zone(wpd->dev_index, blkz);
		if (ret) {
			printf("[FSCK] Write pointer reset failed: %s\n",
			       dev->path);
			return ret;
		}
		fsck->chk.wp_fixed = 1;
		return 0;
	}

	/*
	 * If valid blocks exist in the zone beyond the write pointer, it
	 * is a bug. No need to fix because the zone is not selected for the
	 * write. Just report it.
	 */
	if (last_valid_blkoff + zone_block > wp_block) {
		MSG(0, "Unexpected invalid write pointer: wp[0x%x,0x%x]\n",
		    wp_segno, wp_blkoff);
		return 0;
	}

	return 0;
}

static void fix_wp_sit_alignment(struct f2fs_sb_info *sbi)
{
	unsigned int i;
	struct write_pointer_check_data wpd = {	sbi, 0 };

	if (c.zoned_model != F2FS_ZONED_HM)
		return;

	for (i = 0; i < MAX_DEVICES; i++) {
		if (!c.devices[i].path)
			break;
		if (c.devices[i].zoned_model != F2FS_ZONED_HM)
			break;

		wpd.dev_index = i;
		if (f2fs_report_zones(i, chk_and_fix_wp_with_sit, &wpd)) {
			printf("[FSCK] Write pointer check failed: %s\n",
			       c.devices[i].path);
			return;
		}
	}
}

#else

static void fix_wp_sit_alignment(struct f2fs_sb_info *UNUSED(sbi))
{
	return;
}

#endif

/*
 * Check and fix consistency with write pointers at the beginning of
 * fsck so that following writes by fsck do not fail.
 */
void fsck_chk_and_fix_write_pointers(struct f2fs_sb_info *sbi)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);

	if (c.zoned_model != F2FS_ZONED_HM)
		return;

	if (check_curseg_offsets(sbi) && c.fix_on) {
		fix_curseg_info(sbi);
		fsck->chk.wp_fixed = 1;
	}

	fix_wp_sit_alignment(sbi);
}

int fsck_chk_curseg_info(struct f2fs_sb_info *sbi)
{
	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
	struct curseg_info *curseg;
	struct seg_entry *se;
	struct f2fs_summary_block *sum_blk;
	int i, ret = 0;

	for (i = 0; i < NO_CHECK_TYPE; i++) {
		curseg = CURSEG_I(sbi, i);
		se = get_seg_entry(sbi, curseg->segno);
		sum_blk = curseg->sum_blk;

		if ((get_sb(feature) & cpu_to_le32(F2FS_FEATURE_RO)) &&
			(i != CURSEG_HOT_DATA && i != CURSEG_HOT_NODE))
			continue;

		if (se->type != i) {
			ASSERT_MSG("Incorrect curseg [%d]: segno [0x%x] "
				   "type(SIT) [%d]", i, curseg->segno,
				   se->type);
			if (c.fix_on || c.preen_mode)
				se->type = i;
			ret = -1;
		}
		if (i <= CURSEG_COLD_DATA && IS_SUM_DATA_SEG(sum_blk->footer)) {
			continue;
		} else if (i > CURSEG_COLD_DATA && IS_SUM_NODE_SEG(sum_blk->footer)) {
			continue;
		} else {
			ASSERT_MSG("Incorrect curseg [%d]: segno [0x%x] "
				   "type(SSA) [%d]", i, curseg->segno,
				   sum_blk->footer.entry_type);
			if (c.fix_on || c.preen_mode)
				sum_blk->footer.entry_type =
					i <= CURSEG_COLD_DATA ?
					SUM_TYPE_DATA : SUM_TYPE_NODE;
			ret = -1;
		}
	}

	return ret;
}

int fsck_verify(struct f2fs_sb_info *sbi)
{
	unsigned int i = 0;
	int ret = 0;
	int force = 0;
	u32 nr_unref_nid = 0;
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
	struct hard_link_node *node = NULL;
	bool verify_failed = false;
	uint64_t max_blks, data_secs, node_secs, free_blks;

	if (c.show_file_map)
		return 0;

	printf("\n");

	if (c.zoned_model == F2FS_ZONED_HM) {
		printf("[FSCK] Write pointers consistency                    ");
		if (fsck->chk.wp_inconsistent_zones == 0x0) {
			printf(" [Ok..]\n");
		} else {
			printf(" [Fail] [0x%x]\n",
			       fsck->chk.wp_inconsistent_zones);
			verify_failed = true;
		}

		if (fsck->chk.wp_fixed && c.fix_on)
			force = 1;
	}

	if (c.feature & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
		for (i = 0; i < fsck->nr_nat_entries; i++)
			if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0)
				break;
		if (i < fsck->nr_nat_entries) {
			i = fsck_reconnect_file(sbi);
			printf("[FSCK] Reconnect %u files to lost+found\n", i);
		}
	}

	for (i = 0; i < fsck->nr_nat_entries; i++) {
		if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0) {
			struct node_info ni;

			get_node_info(sbi, i, &ni);
			printf("NID[0x%x] is unreachable, blkaddr:0x%x\n",
							i, ni.blk_addr);
			nr_unref_nid++;
		}
	}

	if (fsck->hard_link_list_head != NULL) {
		node = fsck->hard_link_list_head;
		while (node) {
			printf("NID[0x%x] has [0x%x] more unreachable links\n",
					node->nid, node->links);
			node = node->next;
		}
		c.bug_on = 1;
	}

	data_secs = round_up(sbi->total_valid_node_count, BLKS_PER_SEC(sbi));
	node_secs = round_up(sbi->total_valid_block_count -
				sbi->total_valid_node_count, BLKS_PER_SEC(sbi));
	free_blks = (sbi->total_sections - data_secs - node_secs) *
							BLKS_PER_SEC(sbi);
	max_blks = SM_I(sbi)->main_blkaddr + (data_secs + node_secs) *
							BLKS_PER_SEC(sbi);
	printf("[FSCK] Max image size: %"PRIu64" MB, Free space: %"PRIu64" MB\n",
						max_blks >> 8, free_blks >> 8);
	printf("[FSCK] Unreachable nat entries                       ");
	if (nr_unref_nid == 0x0) {
		printf(" [Ok..] [0x%x]\n", nr_unref_nid);
	} else {
		printf(" [Fail] [0x%x]\n", nr_unref_nid);
		verify_failed = true;
	}

	printf("[FSCK] SIT valid block bitmap checking                ");
	if (memcmp(fsck->sit_area_bitmap, fsck->main_area_bitmap,
					fsck->sit_area_bitmap_sz) == 0x0) {
		printf("[Ok..]\n");
	} else {
		printf("[Fail]\n");
		verify_failed = true;
	}

	printf("[FSCK] Hard link checking for regular file           ");
	if (fsck->hard_link_list_head == NULL) {
		printf(" [Ok..] [0x%x]\n", fsck->chk.multi_hard_link_files);
	} else {
		printf(" [Fail] [0x%x]\n", fsck->chk.multi_hard_link_files);
		verify_failed = true;
	}

	printf("[FSCK] valid_block_count matching with CP            ");
	if (sbi->total_valid_block_count == fsck->chk.valid_blk_cnt) {
		printf(" [Ok..] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt);
	} else {
		printf(" [Fail] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt);
		verify_failed = true;
	}

	printf("[FSCK] valid_node_count matching with CP (de lookup) ");
	if (sbi->total_valid_node_count == fsck->chk.valid_node_cnt) {
		printf(" [Ok..] [0x%x]\n", fsck->chk.valid_node_cnt);
	} else {
		printf(" [Fail] [0x%x]\n", fsck->chk.valid_node_cnt);
		verify_failed = true;
	}

	printf("[FSCK] valid_node_count matching with CP (nat lookup)");
	if (sbi->total_valid_node_count == fsck->chk.valid_nat_entry_cnt) {
		printf(" [Ok..] [0x%x]\n", fsck->chk.valid_nat_entry_cnt);
	} else {
		printf(" [Fail] [0x%x]\n", fsck->chk.valid_nat_entry_cnt);
		verify_failed = true;
	}

	printf("[FSCK] valid_inode_count matched with CP             ");
	if (sbi->total_valid_inode_count == fsck->chk.valid_inode_cnt) {
		printf(" [Ok..] [0x%x]\n", fsck->chk.valid_inode_cnt);
	} else {
		printf(" [Fail] [0x%x]\n", fsck->chk.valid_inode_cnt);
		verify_failed = true;
	}

	printf("[FSCK] free segment_count matched with CP            ");
	if (le32_to_cpu(F2FS_CKPT(sbi)->free_segment_count) ==
						fsck->chk.sit_free_segs) {
		printf(" [Ok..] [0x%x]\n", fsck->chk.sit_free_segs);
	} else {
		printf(" [Fail] [0x%x]\n", fsck->chk.sit_free_segs);
		verify_failed = true;
	}

	printf("[FSCK] next block offset is free                     ");
	if (check_curseg_offsets(sbi) == 0) {
		printf(" [Ok..]\n");
	} else {
		printf(" [Fail]\n");
		verify_failed = true;
	}

	printf("[FSCK] fixing SIT types\n");
	if (check_sit_types(sbi) != 0)
		force = 1;

	printf("[FSCK] other corrupted bugs                          ");
	if (c.bug_on == 0) {
		printf(" [Ok..]\n");
	} else {
		printf(" [Fail]\n");
		ret = EXIT_ERR_CODE;
	}

	if (verify_failed) {
		ret = EXIT_ERR_CODE;
		c.bug_on = 1;
	}

#ifndef WITH_ANDROID
	if (nr_unref_nid && !c.ro) {
		char ans[255] = {0};
		int res;

		printf("\nDo you want to restore lost files into ./lost_found/? [Y/N] ");
		res = scanf("%s", ans);
		ASSERT(res >= 0);
		if (!strcasecmp(ans, "y")) {
			for (i = 0; i < fsck->nr_nat_entries; i++) {
				if (f2fs_test_bit(i, fsck->nat_area_bitmap))
					dump_node(sbi, i, 1);
			}
		}
	}
#endif

	/* fix global metadata */
	if (force || (c.fix_on && f2fs_dev_is_writable())) {
		struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
		struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);

		if (force || c.bug_on || c.bug_nat_bits || c.quota_fixed) {
			/* flush nats to write_nit_bits below */
			flush_journal_entries(sbi);
			fix_hard_links(sbi);
			fix_nat_entries(sbi);
			rewrite_sit_area_bitmap(sbi);
			fix_wp_sit_alignment(sbi);
			fix_curseg_info(sbi);
			fix_checksum(sbi);
			fix_checkpoints(sbi);
		} else if (is_set_ckpt_flags(cp, CP_FSCK_FLAG) ||
			is_set_ckpt_flags(cp, CP_QUOTA_NEED_FSCK_FLAG)) {
			write_checkpoints(sbi);
		}

		if (c.abnormal_stop)
			memset(sb->s_stop_reason, 0, MAX_STOP_REASON);

		if (c.fs_errors)
			memset(sb->s_errors, 0, MAX_F2FS_ERRORS);

		if (c.abnormal_stop || c.fs_errors)
			update_superblock(sb, SB_MASK_ALL);

		/* to return FSCK_ERROR_CORRECTED */
		ret = 0;
	}
	return ret;
}

void fsck_free(struct f2fs_sb_info *sbi)
{
	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);

	if (fsck->qctx)
		quota_release_context(&fsck->qctx);

	if (fsck->main_area_bitmap)
		free(fsck->main_area_bitmap);

	if (fsck->nat_area_bitmap)
		free(fsck->nat_area_bitmap);

	if (fsck->sit_area_bitmap)
		free(fsck->sit_area_bitmap);

	if (fsck->entries)
		free(fsck->entries);

	if (tree_mark)
		free(tree_mark);

	while (fsck->dentry) {
		struct f2fs_dentry *dentry = fsck->dentry;

		fsck->dentry = fsck->dentry->next;
		free(dentry);
	}
}