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
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
|
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2007 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# Copyright (c) 2018, Joyent, Inc.
FSTYPE= ufs
LIBPROG= fsck
ATTMK= $(LIBPROG)
include ../../Makefile.fstype
include ../Makefile.roll
FSCKOBJS= main.o dir.o dup_avl.o inode.o pass1.o pass1b.o \
pass2.o \
pass3.o pass3b.o pass4.o pass5.o setup.o \
utilities.o
FSCKSRCS= $(FSCKOBJS:%.o=%.c)
UFSDIR= ../../../../uts/common/fs/ufs
UFSOBJS= ufs_subr.o ufs_tables.o
UFSSRCS= $(UFSOBJS:%.o=$(UFSDIR)/%.c)
ROLLDIR= ../roll_log
OBJS= $(FSCKOBJS) $(UFSOBJS) $(ROLLOBJS) $(FSLIB)
SRCS= $(FSCKSRCS) $(UFSSRCS) $(ROLLSRCS) $(FSLIBSRC)
CPPFLAGS += -D_LARGEFILE64_SOURCE -I../../ -I../../../../lib/libadm/inc
LDLIBS += -lefi -lavl
CERRWARN += -Wno-parentheses
CERRWARN += -Wno-implicit-function-declaration
CERRWARN += $(CNOWARN_UNINIT)
# Hammerhead: Suppress pointer/int cast warnings in legacy code
CERRWARN += -Wno-pointer-to-int-cast
CERRWARN += -Wno-int-to-pointer-cast
# not linted
SMATCH=off
$(LIBPROG): $(OBJS)
$(LINK.c) -o $@ $(OBJS) $(LDLIBS) $(CTFMERGE_HOOK)
$(POST_PROCESS)
%.o: $(UFSDIR)/%.c
$(COMPILE.c) $< $(CTFCONVERT_HOOK)
clean:
$(RM) $(FSCKOBJS) $(UFSOBJS) $(FSLIB)
include ../../../../Makefile.xref
XREFFLAGS= -f -x
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that: (1) source distributions retain this entire copyright
* notice and comment, and (2) distributions including binaries display
* the following acknowledgement: ``This product includes software
* developed by the University of California, Berkeley and its contributors''
* in the documentation or other materials provided with the distribution
* and in all advertising materials mentioning features or use of this
* software. Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/mntent.h>
#include <string.h>
#include <stdarg.h>
#include <sys/fs/ufs_fs.h>
#include <sys/vnode.h>
#include <sys/fs/ufs_inode.h>
#define _KERNEL
#include <sys/fs/ufs_fsdir.h>
#undef _KERNEL
#include "fsck.h"
struct rc_queue {
struct rc_queue *rc_next;
fsck_ino_t rc_orphan;
fsck_ino_t rc_parent;
caddr_t rc_name;
};
caddr_t lfname = "lost+found"; /* name to use for l+f dir */
static int lfmode = 01700; /* mode to use when creating l+f dir */
static struct dirtemplate emptydir = { 0, DIRBLKSIZ };
static struct dirtemplate dirhead = {
0, 12, 1, ".", 0, DIRBLKSIZ - 12, 2, ".."
};
static void lftempname(char *, fsck_ino_t);
static int do_reconnect(fsck_ino_t, fsck_ino_t, caddr_t);
static caddr_t mkuniqname(caddr_t, caddr_t, fsck_ino_t, fsck_ino_t);
static int chgino(struct inodesc *);
static int dircheck(struct inodesc *, struct direct *);
static int expanddir(fsck_ino_t, char *);
static void freedir(fsck_ino_t, fsck_ino_t);
static struct direct *fsck_readdir(struct inodesc *);
static struct bufarea *getdirblk(daddr32_t, size_t);
static int mkentry(struct inodesc *);
static fsck_ino_t newdir(fsck_ino_t, fsck_ino_t, int, caddr_t);
static fsck_ino_t reallocdir(fsck_ino_t, fsck_ino_t, int, caddr_t);
/*
* Propagate connected state through the tree.
*/
void
propagate(void)
{
struct inoinfo **inpp, *inp;
struct inoinfo **inpend;
int change, inorphan;
inpend = &inpsort[inplast];
do {
change = 0;
for (inpp = inpsort; inpp < inpend; inpp++) {
inp = *inpp;
if (inp->i_parent == 0)
continue;
if (statemap[inp->i_parent] == DFOUND &&
INO_IS_DUNFOUND(inp->i_number)) {
inorphan = statemap[inp->i_number] & INORPHAN;
statemap[inp->i_number] = DFOUND | inorphan;
change++;
}
}
} while (change > 0);
}
/*
* Scan each entry in a directory block.
*/
int
dirscan(struct inodesc *idesc)
{
struct direct *dp;
struct bufarea *bp;
uint_t dsize, n;
size_t blksiz;
union { /* keep lint happy about alignment */
char dbuf[DIRBLKSIZ];
struct direct dir;
} u;
if (idesc->id_type != DATA)
errexit("wrong type to dirscan %d\n", idesc->id_type);
if (idesc->id_entryno == 0 &&
(idesc->id_filesize & (DIRBLKSIZ - 1)) != 0)
idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ);
blksiz = idesc->id_numfrags * sblock.fs_fsize;
if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
idesc->id_filesize -= (offset_t)blksiz;
return (SKIP);
}
idesc->id_loc = 0;
for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
/*
* If we were just passed a corrupt directory entry with
* d_reclen > DIRBLKSIZ, we don't want to memmove() all over
* our stack. This directory gets cleaned up later.
*/
dsize = MIN(dp->d_reclen, sizeof (u.dbuf));
(void) memmove((void *)u.dbuf, (void *)dp, (size_t)dsize);
idesc->id_dirp = &u.dir;
if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
/*
* We can ignore errors from getdirblk() here,
* as the block is still in memory thanks to
* buffering and fsck_readdir(). If there was
* an error reading it before, then all decisions
* leading to getting us here were based on the
* resulting zeros. As such, we have nothing
* to worry about at this point.
*/
bp = getdirblk(idesc->id_blkno, blksiz);
(void) memmove((void *)(bp->b_un.b_buf +
idesc->id_loc - dsize),
(void *)u.dbuf, (size_t)dsize);
dirty(bp);
sbdirty();
}
if (n & STOP)
return (n);
}
return (idesc->id_filesize > 0 ? KEEPON : STOP);
}
/*
* Get current entry in a directory (and peek at the next entry).
*/
static struct direct *
fsck_readdir(struct inodesc *idesc)
{
struct direct *dp, *ndp = 0;
struct bufarea *bp;
ushort_t size; /* of directory entry */
size_t blksiz;
int dofixret;
int salvaged; /* when to report SALVAGED in preen mode */
int origloc = idesc->id_loc;
blksiz = idesc->id_numfrags * sblock.fs_fsize;
/*
* Sanity check id_filesize and id_loc fields. The latter
* has to be within the block we're looking at, as well as
* aligned to a four-byte boundary. The alignment is due to
* a struct direct containing four-byte integers. It's
* unfortunate that the four is a magic number, but there's
* really no good way to derive it from the ufs header files.
*/
if ((idesc->id_filesize <= 0) || (idesc->id_loc >= blksiz) ||
((idesc->id_loc & 3) != 0))
return (NULL);
/*
* We don't have to worry about holes in the directory's
* block list, because that was checked for when the
* inode was first encountered during pass1. We never
* scan a directory until after we've vetted its block list.
*/
/*
* We can ignore errors from getdirblk() here, as dircheck()
* will reject any entries that would have been in the bad
* sectors (fsck_bread() fills in zeros on failures). The main
* reject keys are that d_reclen would be zero and/or that it
* is less than the minimal size of a directory entry. Since
* entries can't span sectors, there's no worry about having
* a good beginning in one sector and the rest in the next,
* where that second sector was unreadable and therefore
* replaced with zeros.
*/
bp = getdirblk(idesc->id_blkno, blksiz);
/* LINTED b_buf is aligned and id_loc was verified above */
dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
/*
* Check the current entry in the directory.
*/
if (dircheck(idesc, dp) == 0) {
/*
* If we are in here, then either the current directory
* entry is bad or the next directory entry is bad.
*/
next_is_bad:
/*
* Find the amount of space left to the end of the
* directory block for either directory entry.
*/
size = DIRBLKSIZ - (idesc->id_loc & (DIRBLKSIZ - 1));
/*
* Advance to the end of the directory block.
*/
idesc->id_loc += size;
idesc->id_filesize -= (offset_t)size;
/*
* Ask the question before we fix the in-core directory
* block because dofix() may reuse the buffer.
*/
salvaged = (idesc->id_fix == DONTKNOW);
dofixret = dofix(idesc, "DIRECTORY CORRUPTED");
/*
* If there was an error reading the block, then that
* same error can reasonably be expected to have occurred
* when it was read previously. As such, the decision
* to come here was based on the results of that partially-
* zerod block, and so anything we change should be
* based on it as well. Upshot: no need to check for
* errors here.
*/
bp = getdirblk(idesc->id_blkno, blksiz);
/* LINTED b_buf is aligned and id_loc/origloc was verified */
dp = (struct direct *)(bp->b_un.b_buf + origloc);
/*
* This is the current directory entry and since it is
* corrupt we cannot trust the rest of the directory
* block so change the current directory entry to
* contain nothing and encompass the rest of the block.
*/
if (ndp == NULL) {
dp->d_reclen = size;
dp->d_ino = 0;
dp->d_namlen = 0;
dp->d_name[0] = '\0';
}
/*
* This is the next directory entry, i.e., we got here
* via a "goto next_is_bad". That directory entry is
* corrupt. However, the current directory entry is okay
* so if we are in fix mode, just extend its record size
* to encompass the rest of the block.
*/
else if (dofixret) {
dp->d_reclen += size;
}
/*
* If the user said to fix the directory corruption, then
* mark the block as dirty. Otherwise, our "repairs" only
* apply to the in-core copy so we don't hand back trash
* to the caller.
*
* Note: It is possible that saying "no" to a change in
* one part of the I/O buffer and "yes" to a later change
* in the same I/O buffer may still flush the change to
* which we said "no". This is the pathological case and
* no fix is planned at this time.
*/
if (dofixret) {
dirty(bp);
if (preen && salvaged)
(void) printf(" (SALVAGED)\n");
if (idesc->id_number == lfdir)
lfdir = 0;
}
/*
* dp points into bp, which will get re-used at some
* arbitrary time in the future. We rely on the fact
* that we're singled-threaded, and that we'll be done
* with this directory entry by the time the next one
* is needed.
*/
return (dp);
}
/*
* The current directory entry checked out so advance past it.
*/
idesc->id_loc += dp->d_reclen;
idesc->id_filesize -= (offset_t)dp->d_reclen;
/*
* If we are not at the directory block boundary, then peek
* at the next directory entry and if it is bad we can add
* its space to the current directory entry (compression).
* Again, we sanity check the id_loc and id_filesize fields
* since we modified them above.
*/
if ((idesc->id_loc & (DIRBLKSIZ - 1)) && /* not at start */
(idesc->id_loc < blksiz) && /* within block */
((idesc->id_loc & 3) == 0) && /* properly aligned */
(idesc->id_filesize > 0)) { /* data follows */
/* LINTED b_buf is aligned and id_loc verified to be ok */
ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
if (dircheck(idesc, ndp) == 0)
goto next_is_bad;
}
/*
* See comment above about dp pointing into bp.
*/
return (dp);
}
/*
* Verify that a directory entry is valid.
* This is a superset of the checks made in the kernel.
*/
static int
dircheck(struct inodesc *idesc, struct direct *dp)
{
size_t size;
char *cp;
int spaceleft;
/*
* Recall that id_filesize is the number of bytes left to
* process in the directory. We check id_filesize >= size
* instead of id_filesize >= d_reclen because all that the
* directory is actually required to contain is the entry
* itself (and it's how the kernel does the allocation).
*
* We indirectly check for d_reclen going past the end of
* the allocated space by comparing it against spaceleft.
*/
size = DIRSIZ(dp);
spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
if (dp->d_ino < maxino &&
dp->d_reclen != 0 &&
(int)dp->d_reclen <= spaceleft &&
(dp->d_reclen & 0x3) == 0 &&
(int)dp->d_reclen >= size &&
idesc->id_filesize >= (offset_t)size &&
dp->d_namlen <= MAXNAMLEN) {
if (dp->d_ino == 0)
return (1);
for (cp = dp->d_name, size = 0; size < (size_t)dp->d_namlen;
size++, cp++)
if ((*cp == '\0') || (*cp == '/'))
goto bad;
if (*cp == '\0')
return (1);
}
bad:
if (debug) {
(void) printf("Bad dir in inode %d at lbn %d, loc %d:\n",
idesc->id_number, idesc->id_lbn, idesc->id_loc);
(void) printf(" ino %d reclen %d namlen %d name `%s'\n",
dp->d_ino, dp->d_reclen, dp->d_namlen, dp->d_name);
}
return (0);
}
void
adjust(struct inodesc *idesc, int lcnt)
{
struct dinode *dp;
caddr_t flow;
int saveiscorrupt;
struct inodesc lcidesc;
dp = ginode(idesc->id_number);
if (dp->di_nlink == lcnt) {
/*
* If we have not hit any unresolved problems, are running
* in preen mode, and are on a file system using logging,
* then just toss any partially allocated files, as they are
* an expected occurrence.
*/
if (!iscorrupt && preen && islog) {
clri(idesc, "UNREF", CLRI_VERBOSE, CLRI_NOP_OK);
return;
} else {
/*
* The file system can be considered clean even if
* a file is not linked up, but is cleared. In
* other words, the kernel won't panic over it.
* Hence, iscorrupt should not be set when
* linkup is answered no, but clri is answered yes.
*
* If neither is answered yes, then we have a
* non-panic-inducing known corruption that the
* user needs to be reminded of when we exit.
*/
saveiscorrupt = iscorrupt;
if (linkup(idesc->id_number, (fsck_ino_t)0,
NULL) == 0) {
iscorrupt = saveiscorrupt;
clri(idesc, "UNREF", CLRI_QUIET, CLRI_NOP_OK);
if (statemap[idesc->id_number] != USTATE)
iscorrupt = 1;
return;
}
dp = ginode(idesc->id_number);
}
lcnt = lncntp[idesc->id_number];
}
/*
* It doesn't happen often, but it's possible to get a true
* excess of links (especially if a lot of directories got
* orphaned and reattached to lost+found). Instead of wrapping
* around, do something semi-useful (i.e., give progress towards
* a less-broken filesystem) when this happens.
*/
LINK_RANGE(flow, dp->di_nlink, -lcnt);
if (flow != NULL) {
LINK_CLEAR(flow, idesc->id_number, dp->di_mode, &lcidesc);
if (statemap[idesc->id_number] == USTATE)
return;
}
dp = ginode(idesc->id_number);
if (lcnt && dp->di_nlink != lcnt) {
pwarn("LINK COUNT %s",
file_id(idesc->id_number, dp->di_mode));
pinode(idesc->id_number);
dp = ginode(idesc->id_number);
(void) printf(" COUNT %d SHOULD BE %d",
dp->di_nlink, dp->di_nlink - lcnt);
/*
* Even lost+found is subject to this, as whenever
* we modify it, we update both the in-memory and
* on-disk counts. Thus, they should still be in
* sync.
*/
if (preen) {
if (lcnt < 0) {
(void) printf("\n");
if ((dp->di_mode & IFMT) == IFSHAD)
pwarn("LINK COUNT INCREASING");
else
pfatal("LINK COUNT INCREASING");
}
}
if (preen || reply("ADJUST") == 1) {
dp->di_nlink -= lcnt;
inodirty();
if (preen)
(void) printf(" (ADJUSTED)\n");
} else if (((dp->di_mode & IFMT) == IFDIR) ||
((dp->di_mode & IFMT) == IFATTRDIR)) {
/*
* File counts can be off relatively harmlessly,
* but a bad directory count can cause the
* kernel to lose its mind.
*/
iscorrupt = 1;
}
}
}
static int
mkentry(struct inodesc *idesc)
{
struct direct *dirp = idesc->id_dirp;
struct direct newent;
int newlen, oldlen;
newent.d_namlen = strlen(idesc->id_name);
newlen = DIRSIZ(&newent);
if (dirp->d_ino != 0)
oldlen = DIRSIZ(dirp);
else
oldlen = 0;
if ((int)dirp->d_reclen - oldlen < newlen)
return (KEEPON);
newent.d_reclen = dirp->d_reclen - (ushort_t)oldlen;
dirp->d_reclen = (ushort_t)oldlen;
/* LINTED dirp is aligned and DIRSIZ() forces oldlen to be aligned */
dirp = (struct direct *)(((char *)dirp) + oldlen);
dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
dirp->d_reclen = newent.d_reclen;
dirp->d_namlen = newent.d_namlen;
(void) memmove(dirp->d_name, idesc->id_name,
(size_t)newent.d_namlen + 1);
return (ALTERED|STOP);
}
static int
chgino(struct inodesc *idesc)
{
struct direct *dirp = idesc->id_dirp;
if (memcmp(dirp->d_name, idesc->id_name,
(size_t)dirp->d_namlen + 1) != 0)
return (KEEPON);
dirp->d_ino = idesc->id_parent;
return (ALTERED|STOP);
}
int
linkup(fsck_ino_t orphan, fsck_ino_t parentdir, caddr_t name)
{
int rval;
struct dinode *dp;
int lostdir;
int lostshadow;
fsck_ino_t oldlfdir;
fsck_ino_t *intree;
struct inodesc idesc;
init_inodesc(&idesc);
dp = ginode(orphan);
lostdir = (((dp->di_mode & IFMT) == IFDIR) ||
((dp->di_mode & IFMT) == IFATTRDIR));
if (debug && lostdir && dp->di_nlink <= 0 && lncntp[orphan] == -1)
(void) printf(
"old fsck would have left inode %d for reclaim thread\n",
orphan);
lostshadow = (dp->di_mode & IFMT) == IFSHAD;
pwarn("UNREF %s ", file_id(orphan, dp->di_mode));
pinode(orphan);
if (lostshadow || (dp->di_size == 0 && dp->di_oeftflag == 0))
return (0);
if (!preen && (reply("RECONNECT") == 0))
goto noconnect;
if (lfdir == 0) {
dp = ginode(UFSROOTINO);
idesc.id_name = lfname;
idesc.id_type = DATA;
idesc.id_func = findino;
idesc.id_number = UFSROOTINO;
idesc.id_fix = DONTKNOW;
if ((ckinode(dp, &idesc, CKI_TRAVERSE) & FOUND) != 0) {
lfdir = idesc.id_parent;
} else {
pwarn("NO %s DIRECTORY", lfname);
if (preen || reply("CREATE") == 1) {
lfdir = newdir(UFSROOTINO, (fsck_ino_t)0,
lfmode, lfname);
if (lfdir != 0) {
if (preen)
(void) printf(" (CREATED)\n");
else
(void) printf("\n");
statemap[lfdir] |= INFOUND;
/*
* XXX What if we allocate an inode
* that's already been scanned? Then
* we need to leave lnctnp[] alone.
*/
TRACK_LNCNTP(UFSROOTINO,
lncntp[UFSROOTINO]++);
}
}
}
if (lfdir == 0) {
pfatal("SORRY. CANNOT CREATE %s DIRECTORY\n", lfname);
pwarn("Could not reconnect inode %d\n", orphan);
goto noconnect;
} else {
/*
* We searched for it via the namespace, so by
* definition it's been found. We have to do this
* because it is possible that we're called before
* the full namespace mapping is complete (especially
* from pass 1, if it encounters a corrupt directory
* that has to be cleared).
*/
statemap[lfdir] |= INFOUND;
}
}
dp = ginode(lfdir);
if ((dp->di_mode & IFMT) != IFDIR) {
pfatal("%s IS NOT A DIRECTORY", lfname);
if (reply("REALLOCATE") == 0) {
iscorrupt = 1;
goto noconnect;
}
oldlfdir = lfdir;
lfdir = reallocdir(UFSROOTINO, (fsck_ino_t)0, lfmode, lfname);
if (lfdir == 0) {
iscorrupt = 1;
pfatal("SORRY. CANNOT CREATE %s DIRECTORY\n\n",
lfname);
goto noconnect;
}
inodirty();
statemap[lfdir] |= INFOUND;
freeino(oldlfdir, TI_PARENT);
}
if (statemap[lfdir] != DFOUND) {
/*
* Not a consistency problem of the sort that'll
* cause the kernel heartburn, so don't set iscorrupt.
*/
if (debug)
(void) printf("lfdir %d is in state 0x%x\n",
lfdir, (int)statemap[lfdir]);
lfdir = 0;
pfatal("SORRY. %s DIRECTORY DISAPPEARED\n\n", lfname);
pwarn("Could not reconnect inode %d\n", orphan);
goto noconnect;
}
rval = do_reconnect(orphan, parentdir, name);
return (rval);
/*
* Leaving things unconnected is harmless as far as trying to
* use the filesystem later, so don't set iscorrupt yet (it's
* just lost blocks and inodes, after all).
*
* Lost directories get noted for reporting after all checks
* are done - they may get cleared later.
*/
noconnect:
if (lostdir) {
intree = tsearch((void *)orphan, &limbo_dirs,
ino_t_cmp);
if (intree == NULL)
errexit("linkup: out of memory");
}
return (0);
}
/*
* Connect an orphaned inode to lost+found.
*
* Returns non-zero for success, zero for failure.
*/
static int
do_reconnect(fsck_ino_t orphan, fsck_ino_t parentdir, caddr_t name)
{
caddr_t flow_msg;
struct dinode *dp;
int lostdir;
mode_t mode;
fsck_ino_t *intree;
struct inodesc idesc;
dp = ginode(orphan);
mode = dp->di_mode & IFMT;
lostdir = (mode == IFDIR) || (mode == IFATTRDIR);
name = mkuniqname(name, lfname, lfdir, orphan);
if (name == NULL)
goto noconnect;
if (makeentry(lfdir, orphan, name) == 0) {
pfatal("SORRY. NO SPACE IN %s DIRECTORY\n", lfname);
pwarn("Could not reconnect inode %d\n", orphan);
goto noconnect;
}
dp = ginode(orphan);
LINK_RANGE(flow_msg, lncntp[orphan], -1);
if (flow_msg != NULL) {
LINK_CLEAR(flow_msg, orphan, dp->di_mode, &idesc);
if (statemap[orphan] == USTATE)
goto noconnect;
}
TRACK_LNCNTP(orphan, lncntp[orphan]--);
/*
* Make sure that anything we put into the normal namespace
* looks like it belongs there. Attributes can only be in
* attribute directories, not the normal directory lost+found.
*/
maybe_convert_attrdir_to_dir(orphan);
if (lostdir) {
/*
* Can't be creating a duplicate entry with makeentry(),
* because changeino() will succeed if ".." already
* exists.
*/
if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
parentdir != (fsck_ino_t)-1)
(void) makeentry(orphan, lfdir, "..");
/*
* If we were half-detached, don't try to get
* inode 0 later on.
*/
if (parentdir == 0)
parentdir = -1;
/*
* Fix up link counts.
*
* XXX This section is getting pretty byzantine, espcially
* when combined with changeino()/chgino()'s link manipulation.
*/
LFDIR_LINK_RANGE_RVAL(flow_msg, lncntp[lfdir], 1, &idesc, 0);
TRACK_LNCNTP(lfdir, lncntp[lfdir]--);
pwarn("DIR I=%lu CONNECTED. ", (long)orphan);
reattached_dir = 1;
if (parentdir != (fsck_ino_t)-1) {
/*
* Have to clear the parent's reference. Otherwise,
* if it's an orphan, then we may clear this orphan
* in pass 4 even though we've reconnected it.
*
* We already have the reference count
* allowing for a parent link, so undo the
* adjustment done above. Otherwise we come
* out high by one.
*/
(void) printf("PARENT WAS I=%lu\n", (long)parentdir);
(void) cleardirentry(parentdir, orphan);
}
if (!preen)
(void) printf("\n");
} else if (preen) {
(void) printf(" (RECONNECTED)\n");
}
statemap[orphan] &= ~INDELAYD;
return (1);
/*
* Leaving things unconnected is harmless as far as trying to
* use the filesystem later, so don't set iscorrupt yet (it's
* just lost blocks and inodes, after all).
*
* Lost directories get noted for reporting after all checks
* are done - they may get cleared later.
*/
noconnect:
if (lostdir) {
intree = tsearch((void *)orphan, &limbo_dirs,
ino_t_cmp);
if (intree == NULL)
errexit("linkup: out of memory");
}
return (0);
}
/*
* fix an entry in a directory.
*/
int
changeino(fsck_ino_t dir, char *name, fsck_ino_t newnum)
{
struct inodesc idesc;
init_inodesc(&idesc);
idesc.id_type = DATA;
idesc.id_func = chgino;
idesc.id_number = dir;
idesc.id_fix = DONTKNOW;
idesc.id_name = name;
idesc.id_parent = newnum; /* new value for name */
return (ckinode(ginode(dir), &idesc, CKI_TRAVERSE));
}
/*
* make an entry in a directory
*/
int
makeentry(fsck_ino_t parent, fsck_ino_t ino, char *name)
{
int repeat;
struct dinode *dp;
struct inoinfo *iip;
struct inodesc idesc;
char pathbuf[MAXPATHLEN + 1];
if (parent < UFSROOTINO || parent >= maxino ||
ino < UFSROOTINO || ino >= maxino)
return (0);
init_inodesc(&idesc);
idesc.id_type = DATA;
idesc.id_func = mkentry;
idesc.id_number = parent;
idesc.id_parent = ino; /* this is the inode to enter */
idesc.id_fix = DONTKNOW;
idesc.id_name = name;
repeat = 0;
again:
dp = ginode(parent);
if ((dp->di_size % DIRBLKSIZ) != 0) {
dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
inodirty();
iip = getinoinfo(ino);
if (iip != NULL)
iip->i_isize = dp->di_size;
}
if ((ckinode(dp, &idesc, CKI_TRAVERSE) & ALTERED) != 0) {
iip = getinoinfo(ino);
if (iip != NULL)
iip->i_isize = dp->di_size;
return (1);
}
if (repeat == 0) {
getpathname(pathbuf, parent, parent);
if (expanddir(parent, pathbuf) == 0)
return (0);
repeat = 1;
goto again;
}
return (0);
}
/*
* Attempt to expand the size of a directory
*/
static int
expanddir(fsck_ino_t ino, char *name)
{
struct bufarea *bpback, *bp[2];
daddr32_t nxtibn, nxtbn;
daddr32_t newblk[2];
struct dinode *dp;
char *cp;
int bc, f;
int n;
int allocIndir;
int frag2blks;
int lffragsz = 0;
int c = 0;
int retval = 0;
bp[0] = bp[1] = NULL;
dp = ginode(ino);
if (dp->di_size == 0) {
goto bail;
}
nxtbn = lblkno(&sblock, dp->di_size - 1) + 1;
/*
* Check that none of the nominally in-use direct block
* addresses for the directory are bogus.
*/
for (bc = 0; ((nxtbn > 0) && (bc < nxtbn) && (bc < NDADDR)); bc++) {
if (dp->di_db[bc] == 0) {
goto bail;
}
}
/*
* Determine our data block allocation needs. We always need to
* allocate at least one data block. We may need a second, the
* indirect block itself.
*/
allocIndir = 0;
nxtibn = -1;
n = 0;
if (nxtbn <= NDADDR) {
/*
* Still in direct blocks. Check for the unlikely
* case where the last block is a frag rather than
* a full block. This would only happen if someone had
* created a file in lost+found, and then that caused
* the dynamic directory shrinking capabilities of ufs
* to kick in.
*
* Note that we test nxtbn <= NDADDR, as it's the
* next block (i.e., one greater than the current/
* actual block being examined).
*/
lffragsz = dp->di_size % sblock.fs_bsize;
}
if (nxtbn >= NDADDR && !lffragsz) {
n = sblock.fs_bsize / sizeof (daddr32_t);
nxtibn = nxtbn - NDADDR;
/*
* Only go one level of indirection
*/
if (nxtibn >= n) {
goto bail;
}
/*
* First indirect block means we need to pick up
* the actual indirect pointer block as well.
*/
if (nxtibn == 0)
allocIndir++;
}
/*
* Allocate all the new blocks we need.
*/
if ((newblk[0] = allocblk(sblock.fs_frag)) == 0) {
goto bail;
}
c++;
if (allocIndir) {
if ((newblk[1] = allocblk(sblock.fs_frag)) == 0) {
goto bail;
}
c++;
}
/*
* Take care of the block that will hold new directory entries.
* This one is always allocated.
*/
bp[0] = getdirblk(newblk[0], (size_t)sblock.fs_bsize);
if (bp[0]->b_errs) {
goto bail;
}
if (lffragsz) {
/*
* Preserve the partially-populated existing directory.
*/
bpback = getdirblk(dp->di_db[nxtbn - 1],
(size_t)dblksize(&sblock, dp, nxtbn - 1));
if (!bpback->b_errs) {
(void) memmove(bp[0]->b_un.b_buf, bpback->b_un.b_buf,
(size_t)lffragsz);
}
}
/*
* Initialize the new fragments. lffragsz is zero if this
* is a completely-new block.
*/
for (cp = &(bp[0]->b_un.b_buf[lffragsz]);
cp < &(bp[0]->b_un.b_buf[sblock.fs_bsize]);
cp += DIRBLKSIZ) {
(void) memmove(cp, (char *)&emptydir,
sizeof (emptydir));
}
dirty(bp[0]);
/*
* If we allocated the indirect block, zero it out. Otherwise
* read it in if we're using one.
*/
if (allocIndir) {
bp[1] = getdatablk(newblk[1], (size_t)sblock.fs_bsize);
if (bp[1]->b_errs) {
goto bail;
}
(void) memset(bp[1]->b_un.b_buf, 0, sblock.fs_bsize);
dirty(bp[1]);
} else if (nxtibn >= 0) {
/* Check that the indirect block pointer looks okay */
if (dp->di_ib[0] == 0) {
goto bail;
}
bp[1] = getdatablk(dp->di_ib[0], (size_t)sblock.fs_bsize);
if (bp[1]->b_errs) {
goto bail;
}
for (bc = 0; ((bc < nxtibn) && (bc < n)); bc++) {
/* LINTED pointer cast alignment */
if (((daddr32_t *)bp[1]->b_un.b_buf)[bc] == 0) {
goto bail;
}
}
}
/*
* Since the filesystem's consistency isn't affected by
* whether or not we actually do the expansion, iscorrupt
* is left alone for any of the approval paths.
*/
pwarn("NO SPACE LEFT IN %s", name);
if (!preen && (reply("EXPAND") == 0))
goto bail;
/*
* Now that everything we need is gathered up and the
* necessary approvals acquired, we can make our provisional
* changes permanent.
*/
if (lffragsz) {
/*
* We've saved the data from the old end fragment(s) in
* our new block, so we can just swap the new one in.
* Make sure the size reflects the expansion of the
* final fragments/block.
*/
frag2blks = roundup(lffragsz, sblock.fs_fsize);
freeblk(ino, dp->di_db[nxtbn - 1],
frag2blks / sblock.fs_fsize);
frag2blks = btodb(frag2blks);
dp->di_size -= (u_offset_t)lffragsz;
dp->di_blocks = dp->di_blocks - frag2blks;
dp->di_db[nxtbn - 1] = newblk[0];
dp->di_size += (u_offset_t)sblock.fs_bsize;
dp->di_blocks += btodb(sblock.fs_bsize);
inodirty();
retval = 1;
goto done;
}
/*
* Full-block addition's much easier. It's just an append.
*/
dp->di_size += (u_offset_t)sblock.fs_bsize;
dp->di_blocks += btodb(sblock.fs_bsize);
if (allocIndir) {
dp->di_blocks += btodb(sblock.fs_bsize);
}
inodirty();
if (nxtibn < 0) {
/*
* Still in direct blocks
*/
dp->di_db[nxtbn] = newblk[0];
} else {
/*
* Last indirect is always going to point at the
* new directory buffer
*/
if (allocIndir)
dp->di_ib[0] = newblk[1];
/* LINTED pointer case alignment */
((daddr32_t *)bp[1]->b_un.b_buf)[nxtibn] = newblk[0];
dirty(bp[1]);
}
if (preen)
(void) printf(" (EXPANDED)\n");
retval = 1;
goto done;
bail:
for (f = 0; f < c; f++)
freeblk(ino, newblk[f], sblock.fs_frag);
done:
/*
* bp[0] is handled by the directory cache's auto-release.
*/
if (bp[1] != NULL)
brelse(bp[1]);
return (retval);
}
static fsck_ino_t
newdir(fsck_ino_t parent, fsck_ino_t request, int mode, caddr_t name)
{
fsck_ino_t dino;
char pname[BUFSIZ];
/*
* This function creates a new directory and populates it with
* "." and "..", then links to it as NAME in PARENT.
*/
dino = allocdir(parent, request, mode, 1);
if (dino != 0) {
getpathname(pname, parent, parent);
name = mkuniqname(name, pname, parent, dino);
/*
* We don't touch numdirs, because it's just a cache of
* what the filesystem claimed originally and is used
* to calculate hash keys.
*/
if (makeentry(parent, dino, name) == 0) {
freedir(dino, parent);
dino = 0;
}
}
return (dino);
}
/*
* Replace whatever NAME refers to in PARENT with a new directory.
* Note that if the old inode REQUEST is a directory, all of its
* contents will be freed and reaped.
*/
static fsck_ino_t
reallocdir(fsck_ino_t parent, fsck_ino_t request, int mode, caddr_t name)
{
int retval;
fsck_ino_t newino;
if ((request != 0) && (statemap[request] != USTATE))
freeino(request, TI_PARENT);
newino = allocdir(parent, request, mode, 0);
if (newino != 0) {
retval = changeino(parent, name, newino);
if ((retval & ALTERED) == 0) {
/*
* No change made, so name doesn't exist, so
* unwind allocation rather than leak it.
*/
freedir(newino, parent);
newino = 0;
}
}
return (newino);
}
/*
* allocate a new directory
*/
fsck_ino_t
allocdir(fsck_ino_t parent, fsck_ino_t request, int mode, int update_parent)
{
fsck_ino_t ino;
caddr_t cp;
caddr_t flow;
struct dinode *dp;
struct bufarea *bp;
struct inoinfo *inp;
struct inodesc idesc;
struct dirtemplate *dirp;
ino = allocino(request, IFDIR|mode);
if (ino == 0)
return (0);
dirp = &dirhead;
dirp->dot_ino = ino;
dirp->dotdot_ino = parent;
dp = ginode(ino);
bp = getdirblk(dp->di_db[0], (size_t)sblock.fs_fsize);
if (bp->b_errs) {
freeino(ino, TI_PARENT);
return (0);
}
(void) memmove(bp->b_un.b_buf, (void *)dirp,
sizeof (struct dirtemplate));
for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
cp < &bp->b_un.b_buf[sblock.fs_fsize];
cp += DIRBLKSIZ)
(void) memmove(cp, (void *)&emptydir, sizeof (emptydir));
dirty(bp);
dp->di_nlink = 2;
inodirty();
if (!inocached(ino)) {
cacheino(dp, ino);
} else {
/*
* re-using an old directory inode
*/
inp = getinoinfo(ino);
if (inp == NULL) {
if (debug)
errexit("allocdir got NULL from getinoinfo "
"for existing entry I=%d\n",
ino);
cacheino(dp, ino);
} else {
init_inoinfo(inp, dp, ino);
inp->i_parent = parent;
inp->i_dotdot = parent;
}
}
/*
* Short-circuit all the dancing around below if it's the
* root inode. The net effect's the same.
*/
if (ino == UFSROOTINO) {
TRACK_LNCNTP(ino, lncntp[ino] = dp->di_nlink);
return (ino);
}
if (!update_parent)
return (ino);
/*
* We never create attribute directories, which can have
* non-directory parents. So, the parent of the directory
* we're creating must itself be a directory.
*/
if (!INO_IS_DVALID(parent)) {
freeino(ino, TI_PARENT);
return (0);
}
/*
* Make sure the parent can handle another link.
* Since we might only update one version of the
* count (disk versus in-memory), we have to check both.
*/
LINK_RANGE(flow, lncntp[parent], -1);
if (flow == NULL)
LINK_RANGE(flow, (int)dp->di_nlink, 1);
if (flow != NULL) {
LINK_CLEAR(flow, parent, dp->di_mode, &idesc);
if (statemap[parent] == USTATE) {
/*
* No parent any more, so bail out. Callers
* are expected to handle this possibility.
* Since most just throw up their hands if
* we return 0, this just happens to work.
*/
freeino(ino, TI_PARENT);
return (0);
}
}
/*
* We've created a directory with two entries, "." and "..",
* and a link count of two ("." and one from its parent). If
* the parent's not been scanned yet, which means this inode
* will get scanned later as well, then make our in-core count
* match what we pushed out to disk. Similarly, update the
* parent. On the other hand, if the parent's already been
* looked at (statemap[ino] == DFOUND), the discrepancy
* between lncntp[] and di_nlink will be noted later, with
* appropriate reporting and propagation, in pass2.
*
* We're explicitly skipping where the parent was DZLINK or
* DFOUND. If it has zero links, it can't be gotten to, so
* we want a discrepancy set up that will be caught in pass2.
* DFOUND was discussed above.
*
* Regarding the claim of a link from the parent: we've not
* done anything to create such a link here. We depend on the
* semantics of our callers attaching the inode we return to
* an existing entry in the directory or creating the entry
* themselves, but in either case, not modifying the link
* count.
*
* Note that setting lncntp[ino] to zero means that both claimed
* links have been ``found''.
*/
statemap[ino] = statemap[parent];
if (INO_IS_DVALID(parent)) {
TRACK_LNCNTP(ino, lncntp[ino] = 0);
TRACK_LNCNTP(parent, lncntp[parent]--);
}
dp = ginode(parent);
dp->di_nlink++;
inodirty();
return (ino);
}
/*
* free a directory inode
*/
static void
freedir(fsck_ino_t ino, fsck_ino_t parent)
{
struct inoinfo *iip;
if (ino != parent) {
/*
* Make sure that the desired parent gets a link
* count update from freeino()/truncino(). If
* we can't look it up, then it's not really a
* directory, so there's nothing to worry about.
*/
iip = getinoinfo(ino);
if (iip != NULL)
iip->i_parent = parent;
}
freeino(ino, TI_PARENT);
}
/*
* generate a temporary name for use in the lost+found directory.
*/
static void
lftempname(char *bufp, fsck_ino_t ino)
{
fsck_ino_t in;
caddr_t cp;
int namlen;
cp = bufp + 2;
for (in = maxino; in > 0; in /= 10)
cp++;
*--cp = '\0';
/* LINTED difference will not overflow an int */
namlen = cp - bufp;
if ((namlen > BUFSIZ) || (namlen > MAXPATHLEN)) {
errexit("buffer overflow in lftempname()\n");
}
in = ino;
while (cp > bufp) {
*--cp = (in % 10) + '0';
in /= 10;
}
*cp = '#';
}
/*
* Get a directory block.
* Insure that it is held until another is requested.
*
* Our callers are expected to check for errors and/or be
* prepared to handle blocks of zeros in the middle of a
* directory.
*/
static struct bufarea *
getdirblk(daddr32_t blkno, size_t size)
{
if (pdirbp != 0) {
brelse(pdirbp);
}
pdirbp = getdatablk(blkno, size);
return (pdirbp);
}
/*
* Create a unique name for INODE to be created in directory PARENT.
* Use NAME if it is provided (non-NULL) and doesn't already exist.
* Returning NULL indicates no unique name could be generated.
*
* If we were given a name, and it conflicts with an existing
* entry, use our usual temp name instead. Without this check,
* we could end up creating duplicate entries for multiple
* orphaned directories in lost+found with the same name (but
* different parents). Of course, our usual name might already
* be in use as well, so be paranoid.
*
* We could do something like keep tacking something onto the
* end of tempname until we come up with something that's not
* in use, but that has liabilities as well. This is a
* sufficiently rare case that it's not worth going that
* overboard for.
*/
static caddr_t
mkuniqname(caddr_t name, caddr_t pname, fsck_ino_t parent, fsck_ino_t inode)
{
fsck_ino_t oldino;
struct dinode *dp;
caddr_t flow_msg;
struct inodesc idesc;
static char tempname[BUFSIZ];
lftempname(tempname, inode);
if ((name != NULL) &&
(lookup_named_ino(parent, name) != 0)) {
name = NULL;
}
if (name == NULL) {
/*
* No name given, or it wasn't unique.
*/
name = tempname;
if ((oldino = lookup_named_ino(parent, name)) != 0) {
pfatal(
"Name ``%s'' for inode %d already exists in %s \n",
name, oldino, pname);
if (reply("REMOVE OLD ENTRY") == 0) {
if (parent == lfdir)
pwarn(
"Could not reconnect inode %d\n\n",
inode);
else
pwarn(
"Could not create entry for %d\n\n",
inode);
name = NULL;
goto noconnect;
}
(void) changeino(parent, name, inode);
LINK_RANGE(flow_msg, lncntp[oldino], 1);
if (flow_msg != NULL) {
/*
* Do a best-effort, but if we're not
* allowed to do the clear, the fs is
* corrupt in any case, so just carry on.
*/
dp = ginode(oldino);
LINK_CLEAR(flow_msg, oldino, dp->di_mode,
&idesc);
if (statemap[oldino] != USTATE)
iscorrupt = 1;
} else {
TRACK_LNCNTP(oldino, lncntp[oldino]++);
}
}
}
noconnect:
return (name);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Keep track of duplicate fragment references (elsewhere called
* blocks for ancient historical reasons).
*
* The duplicates are kept in a binary tree to attempt to minimize
* search times when checking the block lists of all active inodes
* for multiple uses. This is opposed to using a simple linear list
* that is traversed for every block, as is used in the traditional
* fsck. It can be very time-expensive if there's more than just a
* very few duplicates, and typically there are either none or lots.
*
* For each multiply-claimed fragment, we note all of the claiming
* inodes and their corresponding logical block numbers. This allows
* reporting exactly which parts of which files were damaged, which
* provides at least a chance of recovering the bulk of the data on
* a seriously-corrupted filesystem.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/avl.h>
#define _KERNEL
#include <sys/fs/ufs_fsdir.h> /* for struct direct */
#undef _KERNEL
#include <sys/debug.h>
#include "fsck.h"
#define OFFSETOF(type, elt) ((size_t)(&((type *)NULL)->elt))
/*
* For each physical fragment with multiple claimants, the specifics
* of each claim are recorded. This means there are N+1 AVL trees in
* use: one for each fragment's claimant table, plus one that orders
* the fragments themselves.
*
* The table of fragments simply has the physical fragment number
* (pfn) and has the root of the tree of the associated claimants. It
* is keyed by the pfn and called dup_frags.
*
* The subsidiary trees list inodes and logical fragment number (lfn)
* for each claimant. They are keyed first by inode number and then
* by lfn. Both are needed, as it is possible for one inode to have
* multiple claims on the same fragment.
*/
typedef struct claimant {
fsck_ino_t cl_inode;
daddr32_t cl_lfn;
avl_node_t cl_avl;
} claimant_t;
typedef struct fragment {
daddr32_t fr_pfn;
avl_tree_t fr_claimants;
avl_node_t fr_avl;
} fragment_t;
typedef struct reference {
daddr32_t ref_lfn;
daddr32_t ref_pfn;
avl_node_t ref_avl;
} reference_t;
typedef struct inode_dup {
fsck_ino_t id_ino;
avl_tree_t id_fragments;
avl_node_t id_avl;
} inode_dup_t;
static avl_tree_t dup_frags;
static void free_invert_frags(avl_tree_t *);
static void report_dup_lfn_pfn(daddr32_t, daddr32_t, daddr32_t, daddr32_t);
static inode_dup_t *new_inode_dup(fsck_ino_t);
static void invert_frags(avl_tree_t *, avl_tree_t *);
static void report_inode_dups(inode_dup_t *);
static int by_ino_cmp(const void *, const void *);
static int by_lfn_cmp(const void *, const void *);
static claimant_t *alloc_claimant(fsck_ino_t, daddr32_t);
static fragment_t *alloc_dup(daddr32_t);
static int claimant_cmp(const void *, const void *);
static int fragment_cmp(const void *, const void *);
static int decrement_claimant(fragment_t *, fsck_ino_t, daddr32_t);
static int increment_claimant(fragment_t *, fsck_ino_t, daddr32_t);
/*
* Simple accessor function for the outside world so only we need to
* see and interpret our data structures.
*/
int
have_dups(void)
{
return (avl_numnodes(&dup_frags) > 0);
}
/*
* Locates, creates, and deletes a record of a duplicate reference.
*
* For DB_INCR, returns true if the dup was added to the tree.
* For DB_DECR, returns true if the dup was in the tree.
*/
int
find_dup_ref(daddr32_t fragno, fsck_ino_t ino, daddr32_t lfn, int flags)
{
fragment_t key;
fragment_t *dup;
avl_index_t where;
int added = 0;
int removed = 0;
if (avl_first(&dup_frags) == NULL) {
if (flags & DB_CREATE)
avl_create(&dup_frags, fragment_cmp,
sizeof (fragment_t),
OFFSETOF(fragment_t, fr_avl));
else
return (0);
}
key.fr_pfn = fragno;
dup = avl_find(&dup_frags, (void *)&key, &where);
if ((dup == NULL) & (flags & DB_CREATE)) {
dup = alloc_dup(fragno);
avl_insert(&dup_frags, (void *)dup, where);
}
if (dup != NULL) {
if (flags & DB_INCR) {
if (debug)
(void) printf(
"adding claim by ino %d as lfn %d\n",
ino, lfn);
added = increment_claimant(dup, ino, lfn);
} else if (flags & DB_DECR) {
/*
* Note that dup may be invalidated by this call.
*/
removed = decrement_claimant(dup, ino, lfn);
if (debug)
(void) printf(
"check for claimant ino %d lfn %d returned %d\n",
ino, lfn, removed);
}
}
return (added || removed || (dup != NULL));
}
/*
* Dump the duplicates table in a relatively user-friendly form.
* The idea is that the output can be useful when trying to manually
* work out which block belongs to which of the claiming inodes.
*
* What we have is a tree of duplicates indexed by physical
* fragment number. What we want to report is:
*
* Inode %d:
* Logical Offset 0x%08llx, Physical Fragment %d
* Logical Offsets 0x%08llx - 0x%08llx, Physical Fragments %d - %d
* ...
* Inode %d:
* Logical Offsets 0x%08llx - 0x%08llx, Physical Fragments %d - %d
* ...
*/
int
report_dups(int quiet)
{
int overlaps;
inode_dup_t *inode;
fragment_t *dup;
avl_tree_t inode_frags;
overlaps = 0;
ASSERT(have_dups());
/*
* Figure out how many actual dups are still around.
* This tells us whether or not we can mark the
* filesystem clean.
*/
dup = avl_first(&dup_frags);
while (dup != NULL) {
if (avl_numnodes(&dup->fr_claimants) > 1) {
overlaps++;
break;
}
dup = AVL_NEXT(&dup_frags, dup);
}
/*
* Now report on every object that still exists that
* had *any* dups associated with it.
*/
if (!quiet) {
(void) puts("\nSome blocks that were found to be in "
"multiple files are still\nassigned to "
"file(s).\nFragments sorted by inode and "
"logical offsets:");
invert_frags(&dup_frags, &inode_frags);
inode = avl_first(&inode_frags);
while (inode != NULL) {
report_inode_dups(inode);
inode = AVL_NEXT(&inode_frags, inode);
}
(void) printf("\n");
free_invert_frags(&inode_frags);
}
return (overlaps);
}
static void
report_inode_dups(inode_dup_t *inode)
{
reference_t *dup;
daddr32_t first_lfn, last_lfn, first_pfn, last_pfn;
(void) printf("Inode %d:\n", inode->id_ino);
dup = avl_first(&inode->id_fragments);
first_lfn = last_lfn = dup->ref_lfn;
first_pfn = last_pfn = dup->ref_pfn;
while ((dup = AVL_NEXT(&inode->id_fragments, dup)) != NULL) {
if (((last_lfn + 1) != dup->ref_lfn) ||
((last_pfn + 1) != dup->ref_pfn)) {
report_dup_lfn_pfn(first_lfn, last_lfn,
first_pfn, last_pfn);
first_lfn = last_lfn = dup->ref_lfn;
first_pfn = last_pfn = dup->ref_pfn;
}
}
report_dup_lfn_pfn(first_lfn, last_lfn, first_pfn, last_pfn);
}
static void
report_dup_lfn_pfn(daddr32_t first_lfn, daddr32_t last_lfn,
daddr32_t first_pfn, daddr32_t last_pfn)
{
if ((first_lfn == last_lfn) && (first_pfn == last_pfn)) {
(void) printf(
" Logical Offset 0x%08llx Physical Fragment %d\n",
(longlong_t)first_lfn * sblock.fs_fsize, first_pfn);
} else {
(void) printf(
" Logical Offsets 0x%08llx - 0x%08llx, "
"Physical Fragments %d - %d\n",
(longlong_t)first_lfn * sblock.fs_fsize,
(longlong_t)last_lfn * sblock.fs_fsize,
first_pfn, last_pfn);
}
}
/*
* Given a tree of fragment_ts, each element of which has an integral
* sub-tree of claimant_ts, produce a tree of inode_dup_ts, each element
* of which has an integral sub-tree of reference_ts.
*/
static void
invert_frags(avl_tree_t *source, avl_tree_t *target)
{
fragment_t *src_frag;
claimant_t *src_claim;
inode_dup_t *tgt_inode;
inode_dup_t tgt_inode_key;
reference_t *tgt_ref;
reference_t tgt_ref_key;
avl_index_t where;
avl_create(target, by_ino_cmp, sizeof (inode_dup_t),
OFFSETOF(inode_dup_t, id_avl));
src_frag = avl_first(source);
while (src_frag != NULL) {
src_claim = avl_first(&src_frag->fr_claimants);
while (src_claim != NULL) {
/*
* Have we seen this inode before?
*/
tgt_inode_key.id_ino = src_claim->cl_inode;
tgt_inode = avl_find(target, (void *)&tgt_inode_key,
&where);
if (tgt_inode == NULL) {
/*
* No, so set up a record for it.
*/
tgt_inode = new_inode_dup(src_claim->cl_inode);
avl_insert(target, (void *)tgt_inode, where);
}
/*
* Now, how about this logical fragment? In
* theory, we should never see a duplicate, since
* a given lfn only exists once for a given inode.
* As such, we ignore duplicate hits.
*/
tgt_ref_key.ref_lfn = src_claim->cl_lfn;
tgt_ref = avl_find(&tgt_inode->id_fragments,
(void *)&tgt_ref_key, &where);
if (tgt_ref == NULL) {
/*
* Haven't seen it, add it.
*/
tgt_ref = (reference_t *)malloc(
sizeof (reference_t));
if (tgt_ref == NULL)
errexit("Out of memory in "
"invert_frags\n");
tgt_ref->ref_lfn = src_claim->cl_lfn;
tgt_ref->ref_pfn = src_frag->fr_pfn;
avl_insert(&tgt_inode->id_fragments,
(void *)tgt_ref, where);
}
src_claim = AVL_NEXT(&src_frag->fr_claimants,
src_claim);
}
src_frag = AVL_NEXT(source, src_frag);
}
}
/*
* Discard memory associated with the inverted fragments tree created
* by report_dups() via invert_frags().
*/
static void
free_invert_frags(avl_tree_t *tree)
{
void *outer = NULL; /* traversal cookie */
void *inner; /* traversal cookie */
inode_dup_t *inode_dup;
reference_t *ref_dup;
while ((inode_dup = avl_destroy_nodes(tree, &outer)) != NULL) {
inner = NULL;
while ((ref_dup = avl_destroy_nodes(&inode_dup->id_fragments,
&inner)) != NULL) {
free((void *)ref_dup);
}
avl_destroy(&inode_dup->id_fragments);
free((void *)inode_dup);
}
avl_destroy(tree);
}
/*
* Discard all memory allocations associated with the current duplicates
* table.
*/
void
free_dup_state(void)
{
void *dup_cookie = NULL;
void *claim_cookie;
fragment_t *fragv;
claimant_t *claimv;
while ((fragv = avl_destroy_nodes(&dup_frags, &dup_cookie)) != NULL) {
claim_cookie = NULL;
while ((claimv = avl_destroy_nodes(&fragv->fr_claimants,
&claim_cookie)) != NULL) {
free((void *)claimv);
}
avl_destroy(&fragv->fr_claimants);
free((void *)fragv);
}
avl_destroy(&dup_frags);
}
/*
* If the given claimant has not been seen before, add it to DUP's
* list of them. It's not fatal for the same PFN/INODE/LFN to get
* added twice, because pass1b() will add the same dups that pass1()
* did, plus one.
*/
static int
increment_claimant(fragment_t *dup, fsck_ino_t ino, daddr32_t lfn)
{
avl_index_t where;
claimant_t *claimant;
claimant_t key;
int added = 0;
key.cl_inode = ino;
key.cl_lfn = lfn;
claimant = avl_find(&dup->fr_claimants, &key, &where);
if (claimant == NULL) {
if (debug)
(void) printf("inserting claimant\n");
claimant = alloc_claimant(ino, lfn);
avl_insert(&dup->fr_claimants, (void *)claimant, where);
statemap[ino] |= INCLEAR;
/*
* If the inode is to be cleared and has zero links then remove
* the zero link bit as it will be cleared anyway. If INZLINK
* is being removed and it's a directory inode then add the
* inode to the orphan directory list.
*/
if (statemap[ino] & INZLINK) {
statemap[ino] &= ~INZLINK;
if (statemap[ino] & DSTATE) {
add_orphan_dir(ino);
}
}
added = 1;
}
return (added);
}
/*
* If the given claimant is on DUP's list, remove it. It is not
* an error for the claimant to not be on the list.
*/
static int
decrement_claimant(fragment_t *dup, fsck_ino_t ino, daddr32_t lfn)
{
avl_index_t where;
claimant_t *claimant;
claimant_t key;
int busy = 0;
key.cl_inode = ino;
key.cl_lfn = lfn;
claimant = avl_find(&dup->fr_claimants, &key, &where);
if (claimant != NULL) {
avl_remove(&dup->fr_claimants, claimant);
if (avl_numnodes(&dup->fr_claimants) == 0) {
avl_destroy(&dup->fr_claimants);
avl_remove(&dup_frags, (void *)dup);
free((void *)dup);
} else {
busy = 1;
}
}
return (busy);
}
static claimant_t *
alloc_claimant(fsck_ino_t inode, daddr32_t lfn)
{
claimant_t *new = (claimant_t *)malloc(sizeof (claimant_t));
if (new == NULL)
errexit("Out of memory in alloc_claimant()\n");
new->cl_inode = inode;
new->cl_lfn = lfn;
return (new);
}
static fragment_t *
alloc_dup(daddr32_t pfn)
{
fragment_t *new = (fragment_t *)malloc(sizeof (fragment_t));
if (new == NULL)
errexit("Out of memory in alloc_dup()\n");
new->fr_pfn = pfn;
avl_create(&new->fr_claimants, claimant_cmp, sizeof (fragment_t),
OFFSETOF(claimant_t, cl_avl));
return (new);
}
/*
* Compare two fragment_t instances for avl_find(). It requires a
* return value of -1/0/1, so we can't just hand back left - right.
*/
static int
fragment_cmp(const void *vlp, const void *vrp)
{
const fragment_t *lp = (const fragment_t *)vlp;
const fragment_t *rp = (const fragment_t *)vrp;
int cmp = lp->fr_pfn - rp->fr_pfn;
if (cmp < 0)
cmp = -1;
else if (cmp > 0)
cmp = 1;
return (cmp);
}
/*
* Compare two claimant_t instances for avl_find(). It requires a
* return value of -1/0/1, so we can't just hand back left - right.
*/
static int
claimant_cmp(const void *vlp, const void *vrp)
{
const claimant_t *lp = (const claimant_t *)vlp;
const claimant_t *rp = (const claimant_t *)vrp;
int cmp;
cmp = lp->cl_inode - rp->cl_inode;
if (cmp == 0) {
/*
* lfn < 0 is a wildcard lfn match.
*/
if ((lp->cl_lfn >= 0) && (rp->cl_lfn >= 0))
cmp = lp->cl_lfn - rp->cl_lfn;
}
if (cmp < 0)
cmp = -1;
else if (cmp > 0)
cmp = 1;
return (cmp);
}
static int
by_ino_cmp(const void *vlp, const void *vrp)
{
const inode_dup_t *lp = (const inode_dup_t *)vlp;
const inode_dup_t *rp = (const inode_dup_t *)vrp;
int cmp;
cmp = lp->id_ino - rp->id_ino;
if (cmp < 0)
cmp = -1;
else if (cmp > 0)
cmp = 1;
return (cmp);
}
static int
by_lfn_cmp(const void *vlp, const void *vrp)
{
const reference_t *lp = (const reference_t *)vlp;
const reference_t *rp = (const reference_t *)vrp;
int cmp;
cmp = lp->ref_lfn - rp->ref_lfn;
if (cmp < 0)
cmp = -1;
else if (cmp > 0)
cmp = 1;
return (cmp);
}
static inode_dup_t *
new_inode_dup(fsck_ino_t inode)
{
inode_dup_t *new;
new = (inode_dup_t *)malloc(sizeof (inode_dup_t));
if (new == NULL)
errexit("Out of memory in new_inode_dup\n");
new->id_ino = inode;
avl_create(&new->id_fragments, by_lfn_cmp, sizeof (reference_t),
OFFSETOF(reference_t, ref_avl));
return (new);
}
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that: (1) source distributions retain this entire copyright
* notice and comment, and (2) distributions including binaries display
* the following acknowledgement: ``This product includes software
* developed by the University of California, Berkeley and its contributors''
* in the documentation or other materials provided with the distribution
* and in all advertising materials mentioning features or use of this
* software. Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ifndef _FSCK_FSCK_H
#define _FSCK_FSCK_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdarg.h>
#include <search.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/mnttab.h>
#include <sys/vfstab.h>
#include <sys/fs/ufs_fs.h>
#include <sys/fs/ufs_inode.h>
#define MAXDUP 10 /* limit on dup blks (per inode) */
#define MAXBAD 10 /* limit on bad blks (per inode) */
#define MAXBUFSPACE 40*1024 /* initial space to allocate to buffers */
#define INOBUFSIZE 56*1024 /* size of buffer to read inodes in pass1 */
#ifndef BUFSIZ
#define BUFSIZ MAXPATHLEN
#endif
/*
* Inode states in statemap[].
*/
#define USTATE 0x01 /* inode not allocated */
#define FSTATE 0x02 /* inode is file */
#define DSTATE 0x04 /* inode is directory */
#define SSTATE 0x08 /* inode is a shadow/acl */
#define STMASK 0x0f /* pick off the basic state/type */
/* flags OR'd into the above */
#define INZLINK 0x0010 /* inode has zero links */
#define INFOUND 0x0020 /* inode was found during descent */
#define INCLEAR 0x0040 /* inode is to be cleared */
#define INORPHAN 0x0080 /* inode is a known orphan (pass3 only) */
#define INDELAYD 0x0200 /* link count update delayed */
#define INMASK 0xfff0 /* pick off the modifiers */
#define FZLINK (FSTATE | INZLINK)
#define DZLINK (DSTATE | INZLINK)
#define SZLINK (SSTATE | INZLINK)
#define DFOUND (DSTATE | INFOUND)
#define DCLEAR (DSTATE | INCLEAR)
#define FCLEAR (FSTATE | INCLEAR)
#define SCLEAR (SSTATE | INCLEAR)
/*
* These tests depend on the state/type defines above not overlapping bits.
*
* DUNFOUND === (state == DSTATE || state == DZLINK)
* INCLEAR is irrelevant to the determination of
* connectedness, so it's not included in this test.
*
* DVALID === (state == DSTATE || state == DZLINK || state == DFOUND)
*/
#define S_IS_DUNFOUND(state) (((state) & (DSTATE | INZLINK)) \
== (state))
#define S_IS_DVALID(state) (((state) & (DSTATE | INZLINK | INFOUND | \
INORPHAN)) == (state))
#define S_IS_ZLINK(state) (((state) & INZLINK) != 0)
#define INO_IS_DUNFOUND(ino) S_IS_DUNFOUND(statemap[ino])
#define INO_IS_DVALID(ino) S_IS_DVALID(statemap[ino])
/*
* buffer cache structure.
*/
struct bufarea {
struct bufarea *b_next; /* free list queue */
struct bufarea *b_prev; /* free list queue */
diskaddr_t b_bno; /* physical sector number */
int b_size;
int b_errs;
int b_flags;
int b_cnt; /* reference cnt */
union {
char *b_buf; /* buffer space */
daddr32_t *b_indir; /* indirect block */
struct fs *b_fs; /* super block */
struct cg *b_cg; /* cylinder group */
struct dinode *b_dinode; /* inode block */
} b_un;
char b_dirty;
};
#define B_INUSE 1
#define MINBUFS 5 /* minimum number of buffers required */
extern struct bufarea sblk; /* file system superblock */
extern struct bufarea cgblk; /* cylinder group blocks */
extern struct bufarea *pbp; /* pointer to inode data in buffer pool */
extern struct bufarea *pdirbp; /* pointer to directory data in buffer pool */
#define sbdirty() dirty(&sblk)
#define cgdirty() dirty(&cgblk)
#define sblock (*sblk.b_un.b_fs)
#define cgrp (*cgblk.b_un.b_cg)
/*
* inodesc.id_fix values. See inode.c for a description of their usage.
*/
enum fixstate {
DONTKNOW, NOFIX, FIX, IGNORE
};
/*
* Tells truncino() whether or not to attempt to update the parent
* directory's link count. Also, TI_NODUP flags when we're discarding
* fragments that are beyond the original end of the file, and so
* should not be considered duplicate-claim candidates.
*/
#define TI_NOPARENT 0x0001 /* leave parent's di_nlink alone */
#define TI_PARENT 0x0002 /* update parent's di_nlink */
#define TI_NODUP 0x0004 /* not a dup candidate */
/*
* Modes for ckinode() and ckinode_common().
*
* CKI_TRAVERSE is the common case, and requests a traditional
* traversal of blocks or directory entries.
*
* CKI_TRUNCATE indicates that we're truncating the file, and that any
* block indices beyond the end of the target length should be cleared
* after the callback has returned (i.e., this is a superset of
* CKI_TRAVERSE). idesc->id_truncto is the first logical block number
* to clear. If it is less than zero, then the traversal will be
* equivalent to a simple CKI_TRAVERSE.
*/
enum cki_action { CKI_TRAVERSE, CKI_TRUNCATE };
/*
* The general definition of an ino_t is an unsigned quantity.
* However, the on-disk version is an int32_t, which is signed.
* Since we really want to be able to detect wrapped-around
* inode numbers and such, we'll use something that's compatible
* with what's on disk since that's the only context that really
* matters. If an int32_t is found not to be sufficiently large,
* this will make it much easier to change later.
*
* Note that there is one unsigned inode field in the on-disk
* inode, ic_oeftflag. Since all other inode fields are signed,
* no legitimate inode number can be put into ic_oeftflag that
* would overflow into the high bit. Essentially, it should
* actually be declared as int32_t just like all the others, and
* we're going to pretend that it was.
*
* None of the routines that we use in ufs_subr.c do anything with
* inode numbers. If that changes, then great care will be needed
* to deal with the differences in definition of ino_t and fsck_ino_t.
* Lint is your friend.
*/
typedef int32_t fsck_ino_t;
/*
* See the full discussion of the interactions between struct inodesc
* and ckinode() in inode.c
*/
struct inodesc {
enum fixstate id_fix; /* policy on fixing errors */
int (*id_func)(struct inodesc *);
/* function to be applied to blocks of inode */
fsck_ino_t id_number; /* inode number described */
fsck_ino_t id_parent; /* for DATA nodes, their parent */
/* also used for extra (*id_func) parameter */
/* and return values */
daddr32_t id_lbn; /* logical fragment number of current block */
daddr32_t id_blkno; /* physical fragment number being examined */
int id_numfrags; /* number of frags contained in block */
daddr32_t id_truncto; /* # blocks to truncate to, -1 for no trunc. */
offset_t id_filesize; /* for DATA nodes, the size of the directory */
uint_t id_loc; /* for DATA nodes, current location in dir */
daddr32_t id_entryno; /* for DATA nodes, current dir entry number */
daddr32_t id_firsthole; /* for DATA inode, logical block that is */
/* zero but shouldn't be, -1 for no holes */
struct direct *id_dirp; /* for DATA nodes, ptr to current entry */
caddr_t id_name; /* for DATA nodes, name to find or enter */
char id_type; /* type of descriptor, DATA or ADDR */
};
/* file types (0 is reserved for catching bugs) */
#define DATA 1 /* a directory */
#define ACL 2 /* an acl/shadow */
#define ADDR 3 /* anything but a directory or an acl/shadow */
/*
* OR'd flags for find_dup_ref()'s mode argument
*/
#define DB_CREATE 0x01 /* if dup record found, make one */
#define DB_INCR 0x02 /* increment block's reference count */
#define DB_DECR 0x04 /* decrement block's reference count */
/*
* Cache data structures
*/
struct inoinfo {
struct inoinfo *i_nextlist; /* next inode/acl cache entry */
fsck_ino_t i_number; /* inode number of this entry */
fsck_ino_t i_parent; /* inode number of parent */
fsck_ino_t i_dotdot; /* inode number of .. */
fsck_ino_t i_extattr; /* inode of hidden attr dir */
offset_t i_isize; /* size of inode */
size_t i_blkssize; /* size of block array in bytes */
daddr32_t i_blks[1]; /* actually longer */
};
/*
* Inode cache
*/
extern struct inoinfo **inphead, **inpsort;
extern int64_t numdirs, listmax, inplast;
/*
* ACL cache
*/
extern struct inoinfo **aclphead, **aclpsort;
extern int64_t numacls, aclmax, aclplast;
/*
* Tree of directories we haven't reconnected or cleared. Any
* dir inode that linkup() fails on gets added, any that clri()
* succeeds on gets removed. If there are any left at the end of
* pass four, then we have a user-forced corrupt filesystem, and
* need to set iscorrupt.
*
* Elements are fsck_ino_t instances (not pointers).
*/
extern void *limbo_dirs;
/*
* Number of directories we actually found in the filesystem,
* as opposed to how many the superblock claims there are.
*/
extern fsck_ino_t countdirs;
/*
* shadowclients and shadowclientinfo are structures for keeping track of
* shadow inodes that exist, and which regular inodes use them (i.e. are
* their clients).
*/
struct shadowclients {
fsck_ino_t *client; /* an array of inode numbers */
int nclients; /* how many inodes in the array are in use (valid) */
struct shadowclients *next; /* link to more client inode numbers */
};
struct shadowclientinfo {
fsck_ino_t shadow; /* the shadow inode that this info is for */
int totalClients; /* how many inodes total refer to this */
struct shadowclients *clients; /* a linked list of wads of clients */
struct shadowclientinfo *next; /* link to the next shadow inode */
};
/* global pointer to this shadow/client information */
extern struct shadowclientinfo *shadowclientinfo;
extern struct shadowclientinfo *attrclientinfo;
/*
* In ufs_inode.h ifdef _KERNEL, this is defined as `/@/'. However,
* to avoid all sorts of potential confusion (you can't actually use
* `foo/@/bar' to get to an attribute), we use something that doesn't
* look quite so much like a simple pathname.
*/
#define XATTR_DIR_NAME " <xattr> "
/*
* granularity -- how many client inodes do we make space for at a time
* initialized in setup.c;
*/
extern int maxshadowclients;
/*
* Initialized global variables.
*/
extern caddr_t lfname;
/*
* Unitialized globals.
*/
extern char *devname; /* name of device being checked */
extern size_t dev_bsize; /* computed value of DEV_BSIZE */
extern int secsize; /* actual disk sector size */
extern char nflag; /* assume a no response */
extern char yflag; /* assume a yes response */
extern daddr32_t bflag; /* location of alternate super block */
extern int debug; /* output debugging info */
extern int rflag; /* check raw file systems */
extern int fflag; /* check regardless of clean flag (force) */
extern int mflag; /* sanity check only */
extern int verbose; /* be chatty */
extern char preen; /* just fix normal inconsistencies */
extern char mountedfs; /* checking mounted device */
extern int exitstat; /* exit status (see EX* defines below) */
extern char hotroot; /* checking root device */
extern char rerun; /* rerun fsck. Only used in non-preen mode */
extern int interrupted; /* 1 => exit EXSIGNAL on exit */
extern char havesb; /* superblock has been read */
extern int fsmodified; /* 1 => write done to file system */
extern int fsreadfd; /* file descriptor for reading file system */
extern int fswritefd; /* file descriptor for writing file system */
extern int iscorrupt; /* known to be corrupt/inconsistent */
/* -1 means mark clean so user can mount+fix */
extern int isdirty; /* 1 => write pending to file system */
extern int islog; /* logging file system */
extern int islogok; /* log is okay */
extern int errorlocked; /* set => mounted fs has been error-locked */
/* implies fflag "force check flag" */
extern char *elock_combuf; /* error lock comment buffer */
extern char *elock_mountp; /* mount point; used to unlock error-lock */
extern int pid; /* fsck's process id (put in lockfs comment) */
extern int mountfd; /* fd of mount point */
extern daddr32_t maxfsblock; /* number of blocks in the file system */
extern uint_t largefile_count; /* global largefile counter */
extern char *mount_point; /* if mounted, this is where */
extern char *blockmap; /* ptr to primary blk allocation map */
extern fsck_ino_t maxino; /* number of inodes in file system */
extern fsck_ino_t lastino; /* last inode in use */
extern ushort_t *statemap; /* ptr to inode state table */
extern short *lncntp; /* ptr to link count table */
extern fsck_ino_t lfdir; /* lost & found directory inode number */
extern int overflowed_lf; /* tried to wrap lost & found's link count */
extern int reattached_dir; /* reconnected at least one directory */
extern int broke_dir_link; /* broke at least one directory hardlink */
extern daddr32_t n_blks; /* number of blocks in use */
extern fsck_ino_t n_files; /* number of files in use */
#define clearinode(dp) { \
*(dp) = zino; \
}
extern struct dinode zino;
#define testbmap(blkno) isset(blockmap, blkno)
#define setbmap(blkno) setbit(blockmap, blkno)
#define clrbmap(blkno) clrbit(blockmap, blkno)
#define STOP 0x01
#define SKIP 0x02
#define KEEPON 0x04
#define ALTERED 0x08
#define FOUND 0x10
/*
* Support relatively easy debugging of lncntp[] updates. This can't
* be a function, because of the (_op) step. Normally, we just do that.
*/
#define TRACK_LNCNTP(_ino, _op) (_op)
/*
* See if the net link count for an inode has gone outside
* what can be represented on disk. Returning text as NULL
* indicates no.
*
* Remember that link counts are effectively inverted, so
* underflow and overflow are reversed as well.
*
* This check should be done before modifying the actual link
* count.
*/
#define LINK_RANGE(text, current, offset) { \
int net = ((int)(current)) + ((int)(offset)); \
text = NULL; \
if (net > (MAXLINK)) \
text = "UNDERFLOW"; \
else if (net < -(MAXLINK)) \
text = "OVERFLOW"; \
}
/*
* If LINK_RANGE() indicated a problem, this is the boiler-plate
* for dealing with it. Usage is:
*
* LINK_RANGE(text, current, offset);
* if (text != NULL) {
* LINK_CLEAR(text, ino, mode, idp);
* if (statemap[ino] == USTATE)
* ...inode was cleared...
* }
*
* Note that clri() will set iscorrupt if the user elects not to
* clear the problem inode, so the filesystem won't get reported
* as clean when it shouldn't be.
*/
#define LINK_CLEAR(text, ino, mode, idp) { \
pwarn("%s LINK COUNT %s", file_id((ino), (mode)), (text)); \
pinode((ino)); \
pfatal(""); \
init_inodesc((idp)); \
(idp)->id_type = ADDR; \
(idp)->id_func = pass4check; \
(idp)->id_number = ino; \
(idp)->id_fix = DONTKNOW; \
clri((idp), (text), CLRI_QUIET, CLRI_NOP_CORRUPT); \
}
/*
* Used for checking link count under/overflow specifically on
* the lost+found directory. If the user decides not to do the
* clri(), then flag that we've hit this problem and refuse to do
* the reconnect.
*/
#define LFDIR_LINK_RANGE_RVAL(text, current, offset, idp, rval) { \
LINK_RANGE(text, current, offset); \
if (text != NULL) { \
LINK_CLEAR(text, lfdir, IFDIR, idp); \
if (statemap[lfdir] == USTATE) { \
lfdir = 0; \
return (rval); \
} else { \
overflowed_lf++; \
} \
} \
}
#define LFDIR_LINK_RANGE_NORVAL(text, current, offset, idp) { \
LINK_RANGE(text, current, offset); \
if (text != NULL) { \
LINK_CLEAR(text, lfdir, IFDIR, idp); \
if (statemap[lfdir] == USTATE) { \
lfdir = 0; \
return; \
} else { \
overflowed_lf++; \
} \
} \
}
/*
* Values for mounted() and mountedfs.
*/
#define M_NOMNT 0 /* filesystem is not mounted */
#define M_RO 1 /* filesystem is mounted read-only */
#define M_RW 2 /* filesystem is mounted read-write */
#define EXOKAY 0 /* file system is unmounted and ok */
#define EXBADPARM 1 /* bad parameter(s) given */
#define EXUMNTCHK 32 /* fsck -m: unmounted, needs checking */
#define EXMOUNTED 33 /* file system already mounted, not magic, */
/* or it is magic and mounted read/write */
#define EXNOSTAT 34 /* cannot stat device */
#define EXREBOOTNOW 35 /* modified root or something equally scary */
#define EXFNDERRS 36 /* uncorrectable errors, terminate normally */
#define EXSIGNAL 37 /* a signal was caught during processing */
#define EXERRFATAL 39 /* uncorrectable errors, exit immediately */
#define EXROOTOKAY 40 /* for root, same as 0 */
/*
* Values for clri()'s `verbose' and `corrupting' arguments (third
* and fourth, respectively).
*/
#define CLRI_QUIET 1
#define CLRI_VERBOSE 2
#define CLRI_NOP_OK 1
#define CLRI_NOP_CORRUPT 2
/*
* Filesystems that are `magical' - if they exist in vfstab,
* then they have to be mounted for the system to have gotten
* far enough to be able to run fsck. Thus, don't get all
* bent out of shape if we're asked to check it and it is mounted.
* Actual initialization of the array is in main.c
*/
enum magic {
MAGIC_NONE = 0,
MAGIC_ROOT = 1,
MAGIC_USR = 2,
MAGIC_LIMIT = 3
};
extern char *magic_fs[];
/*
* Paths needed by calcsb().
*/
#define MKFS_PATH "/usr/lib/fs/ufs/mkfs"
#define NEWFS_PATH "/usr/lib/fs/ufs/newfs"
int acltypeok(struct dinode *);
void add_orphan_dir(fsck_ino_t);
void adjust(struct inodesc *, int);
daddr32_t allocblk(int);
fsck_ino_t allocdir(fsck_ino_t, fsck_ino_t, int, int);
fsck_ino_t allocino(fsck_ino_t, int);
void blkerror(fsck_ino_t, caddr_t, daddr32_t, daddr32_t);
void brelse(struct bufarea *);
void bufinit(void);
void bwrite(int, caddr_t, diskaddr_t, int64_t);
void cacheacl(struct dinode *, fsck_ino_t);
void cacheino(struct dinode *, fsck_ino_t);
void catch(int);
void catchquit(int);
caddr_t cg_sanity(struct cg *, int);
void cgflush(void);
int cgisdirty(void);
int changeino(fsck_ino_t, caddr_t, fsck_ino_t);
int check_mnttab(caddr_t, caddr_t, size_t);
int check_vfstab(caddr_t, caddr_t, size_t);
int chkrange(daddr32_t, int);
void ckfini(void);
int ckinode(struct dinode *, struct inodesc *, enum cki_action);
void clearattrref(fsck_ino_t);
int cleardirentry(fsck_ino_t, fsck_ino_t);
void clearshadow(fsck_ino_t, struct shadowclientinfo **);
void clri(struct inodesc *, caddr_t, int, int);
void deshadow(struct shadowclientinfo *, void (*)(fsck_ino_t));
void direrror(fsck_ino_t, caddr_t, ...);
int dirscan(struct inodesc *);
void dirty(struct bufarea *);
int do_errorlock(int);
int dofix(struct inodesc *, caddr_t, ...);
void examinelog(void (*)(daddr32_t));
void errexit(caddr_t, ...);
void fileerror(fsck_ino_t, fsck_ino_t, caddr_t, ...);
caddr_t file_id(fsck_ino_t, mode_t);
int find_dup_ref(daddr32_t, fsck_ino_t, daddr32_t, int);
int findino(struct inodesc *);
int findname(struct inodesc *);
void fix_cg(struct cg *, int);
void flush(int, struct bufarea *);
void free_dup_state(void);
void freeblk(fsck_ino_t, daddr32_t, int);
void freeino(fsck_ino_t, int);
void freeinodebuf(void);
int fsck_asprintf(caddr_t *, caddr_t, ...);
int fsck_bread(int, caddr_t, diskaddr_t, size_t);
int ftypeok(struct dinode *);
struct bufarea *getblk(struct bufarea *, daddr32_t, size_t);
struct bufarea *getdatablk(daddr32_t, size_t size);
diskaddr_t getdisksize(caddr_t, int);
struct inoinfo *getinoinfo(fsck_ino_t);
struct dinode *getnextinode(fsck_ino_t);
struct dinode *getnextrefresh(void);
void getpathname(caddr_t, fsck_ino_t, fsck_ino_t);
struct dinode *ginode(fsck_ino_t);
caddr_t hasvfsopt(struct vfstab *, caddr_t);
int have_dups(void);
void init_inodesc(struct inodesc *);
void init_inoinfo(struct inoinfo *, struct dinode *, fsck_ino_t);
void initbarea(struct bufarea *);
int ino_t_cmp(const void *, const void *);
int inocached(fsck_ino_t);
void inocleanup(void);
void inodirty(void);
int is_errorlocked(caddr_t);
int linkup(fsck_ino_t, fsck_ino_t, caddr_t);
int lookup_named_ino(fsck_ino_t, caddr_t);
int makeentry(fsck_ino_t, fsck_ino_t, caddr_t);
void maybe_convert_attrdir_to_dir(fsck_ino_t);
int mounted(caddr_t, caddr_t, size_t);
void pass1(void);
void pass1b(void);
int pass1check(struct inodesc *);
void pass2(void);
void pass3a(void);
void pass3b(void);
int pass3bcheck(struct inodesc *);
void pass4(void);
int pass4check(struct inodesc *);
void pass5(void);
void pfatal(caddr_t, ...);
void pinode(fsck_ino_t);
void printclean(void);
void propagate(void);
void pwarn(caddr_t, ...);
caddr_t rawname(caddr_t);
void registershadowclient(fsck_ino_t, fsck_ino_t,
struct shadowclientinfo **);
void remove_orphan_dir(fsck_ino_t);
int reply(caddr_t, ...);
int report_dups(int);
void resetinodebuf(void);
char *setup(caddr_t);
void truncino(fsck_ino_t, offset_t, int);
void unbufinit(void);
caddr_t unrawname(caddr_t);
void unregistershadow(fsck_ino_t, struct shadowclientinfo **);
int updateclean(void);
int writable(caddr_t);
void write_altsb(int);
/*
* Functions from the kernel sources (ufs_subr.c, etc).
*/
extern void fragacct(struct fs *, int, int32_t *, int);
#ifdef __cplusplus
}
#endif
#endif /* _FSCK_FSCK_H */
#!/bin/sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License"). You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
find_files "s.*"\
usr/src/uts/common/fs/ufs
/*
* Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that: (1) source distributions retain this entire copyright
* notice and comment, and (2) distributions including binaries display
* the following acknowledgement: ``This product includes software
* developed by the University of California, Berkeley and its contributors''
* in the documentation or other materials provided with the distribution
* and in all advertising materials mentioning features or use of this
* software. Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <limits.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/mntent.h>
#include <sys/vnode.h>
#include <sys/fs/ufs_inode.h>
#include <sys/fs/ufs_fs.h>
#define _KERNEL
#include <sys/fs/ufs_fsdir.h>
#undef _KERNEL
#include <pwd.h>
#include "fsck.h"
uint_t largefile_count = 0;
fsck_ino_t lastino;
struct bufarea cgblk;
struct inoinfo **aclphead, **aclpsort;
struct dinode zino;
static int get_indir_offsets(int, daddr_t, int *, int *);
static int clearanentry(struct inodesc *);
static void pdinode(struct dinode *);
static void inoflush(void);
static void mark_delayed_inodes(fsck_ino_t, daddr32_t);
static int iblock(struct inodesc *, int, u_offset_t, enum cki_action);
static struct inoinfo *search_cache(struct inoinfo *, fsck_ino_t);
static int ckinode_common(struct dinode *, struct inodesc *, enum cki_action);
static int lookup_dotdot_ino(fsck_ino_t);
/*
* ckinode() essentially traverses the blocklist of the provided
* inode. For each block either the caller-supplied callback (id_func
* in the provided struct inodesc) or dirscan() is invoked. Which is
* chosen is controlled by what type of traversal was requested
* (id_type) - if it was for an ADDR or ACL, use the callback,
* otherwise it is assumed to be DATA (i.e., a directory) whose
* contents need to be scanned.
*
* Note that a directory inode can get passed in with a type of ADDR;
* the type field is orthogonal to the IFMT value. This is so that
* the file aspects (no duplicate blocks, etc) of a directory can be
* verified just like is done for any other file, or the actual
* contents can be scanned so that connectivity and such can be
* investigated.
*
* The traversal is controlled by flags in the return value of
* dirscan() or the callback. Five flags are defined, STOP, SKIP,
* KEEPON, ALTERED, and FOUND. Their semantics are:
*
* STOP - no further processing of this inode is desired/possible/
* feasible/etc. This can mean that whatever the scan
* was searching for was found, or a serious
* inconsistency was encountered, or anything else
* appropriate.
*
* SKIP - something that made it impossible to continue was
* encountered, and the caller should go on to the next
* inode. This is more for i/o failures than for
* logical inconsistencies. Nothing actually looks for
* this.
*
* KEEPON - no more blocks of this inode need to be scanned, but
* nothing's wrong, so keep on going with the next
* inode. It is similar to STOP, except that
* ckinode()'s caller will typically advance to the next
* inode for KEEPON, whereas it ceases scanning through
* the inodes completely for STOP.
*
* ALTERED - a change was made to the inode. If the caller sees
* this set, it should make sure to flush out the
* changes. Note that any data blocks read in by the
* function need to be marked dirty by it directly;
* flushing of those will happen automatically later.
*
* FOUND - whatever was being searched for was located.
* Typically combined with STOP to avoid wasting time
* doing additional looking.
*
* During a traversal, some state needs to be carried around. At the
* least, the callback functions need to know what inode they're
* working on, which logical block, and whether or not fixing problems
* when they're encountered is desired. Rather than try to guess what
* else might be needed (and thus end up passing way more arguments
* than is reasonable), all the possibilities have been bundled in
* struct inodesc. About half of the fields are specific to directory
* traversals, and the rest are pretty much generic to any traversal.
*
* The general fields are:
*
* id_fix What to do when an error is found. Generally, this
* is set to DONTKNOW before a traversal. If a
* problem is encountered, it is changed to either FIX
* or NOFIX by the dofix() query function. If id_fix
* has already been set to FIX when dofix() is called, then
* it includes the ALTERED flag (see above) in its return
* value; the net effect is that the inode's buffer
* will get marked dirty and written to disk at some
* point. If id_fix is DONTKNOW, then dofix() will
* query the user. If it is NOFIX, then dofix()
* essentially does nothing. A few routines set NOFIX
* as the initial value, as they are performing a best-
* effort informational task, rather than an actual
* repair operation.
*
* id_func This is the function that will be called for every
* logical block in the file (assuming id_type is not
* DATA). The logical block may represent a hole, so
* the callback needs to be prepared to handle that
* case. Its return value is a combination of the flags
* described above (SKIP, ALTERED, etc).
*
* id_number The inode number whose block list or data is being
* scanned.
*
* id_parent When id_type is DATA, this is the inode number for
* the parent of id_number. Otherwise, it is
* available for use as an extra parameter or return
* value between the callback and ckinode()'s caller.
* Which, if either, of those is left completely up to
* the two routines involved, so nothing can generally
* be assumed about the id_parent value for non-DATA
* traversals.
*
* id_lbn This is the current logical block (not fragment)
* number being visited by the traversal.
*
* id_blkno This is the physical block corresponding to id_lbn.
*
* id_numfrags This defines how large a block is being processed in
* this particular invocation of the callback.
* Usually, it will be the same as sblock.fs_frag.
* However, if a direct block is being processed and
* it is less than a full filesystem block,
* id_numfrags will indicate just how many fragments
* (starting from id_lbn) are actually part of the
* file.
*
* id_truncto The pass 4 callback is used in several places to
* free the blocks of a file (the `FILE HAS PROBLEM
* FOO; CLEAR?' scenario). This has been generalized
* to allow truncating a file to a particular length
* rather than always completely discarding it. If
* id_truncto is -1, then the entire file is released,
* otherwise it is logical block number to truncate
* to. This generalized interface was motivated by a
* desire to be able to discard everything after a
* hole in a directory, rather than the entire
* directory.
*
* id_type Selects the type of traversal. DATA for dirscan(),
* ADDR or ACL for using the provided callback.
*
* There are several more fields used just for dirscan() traversals:
*
* id_filesize The number of bytes in the overall directory left to
* process.
*
* id_loc Byte position within the directory block. Should always
* point to the start of a directory entry.
*
* id_entryno Which logical directory entry is being processed (0
* is `.', 1 is `..', 2 and on are normal entries).
* This field is primarily used to enable special
* checks when looking at the first two entries.
*
* The exception (there's always an exception in fsck)
* is that in pass 1, it tracks how many fragments are
* being used by a particular inode.
*
* id_firsthole The first logical block number that was found to
* be zero. As directories are not supposed to have
* holes, this marks where a directory should be
* truncated down to. A value of -1 indicates that
* no holes were found.
*
* id_dirp A pointer to the in-memory copy of the current
* directory entry (as identified by id_loc).
*
* id_name This is a directory entry name to either create
* (callback is mkentry) or locate (callback is
* chgino, findino, or findname).
*/
int
ckinode(struct dinode *dp, struct inodesc *idesc, enum cki_action action)
{
struct inodesc cleardesc;
mode_t mode;
if (idesc->id_filesize == 0)
idesc->id_filesize = (offset_t)dp->di_size;
/*
* Our caller should be filtering out completely-free inodes
* (mode == zero), so we'll work on the assumption that what
* we're given has some basic validity.
*
* The kernel is inconsistent about MAXPATHLEN including the
* trailing \0, so allow the more-generous length for symlinks.
*/
mode = dp->di_mode & IFMT;
if (mode == IFBLK || mode == IFCHR)
return (KEEPON);
if (mode == IFLNK && dp->di_size > MAXPATHLEN) {
pwarn("I=%d Symlink longer than supported maximum\n",
idesc->id_number);
init_inodesc(&cleardesc);
cleardesc.id_type = ADDR;
cleardesc.id_number = idesc->id_number;
cleardesc.id_fix = DONTKNOW;
clri(&cleardesc, "BAD", CLRI_VERBOSE, CLRI_NOP_CORRUPT);
return (STOP);
}
return (ckinode_common(dp, idesc, action));
}
/*
* This was split out from ckinode() to allow it to be used
* without having to pass in kludge flags to suppress the
* wrong-for-deletion initialization and irrelevant checks.
* This feature is no longer needed, but is being kept in case
* the need comes back.
*/
static int
ckinode_common(struct dinode *dp, struct inodesc *idesc,
enum cki_action action)
{
offset_t offset;
struct dinode dino;
daddr_t ndb;
int indir_data_blks, last_indir_blk;
int ret, i, frags;
(void) memmove(&dino, dp, sizeof (struct dinode));
ndb = howmany(dino.di_size, (u_offset_t)sblock.fs_bsize);
for (i = 0; i < NDADDR; i++) {
idesc->id_lbn++;
offset = blkoff(&sblock, dino.di_size);
if ((--ndb == 0) && (offset != 0)) {
idesc->id_numfrags =
numfrags(&sblock, fragroundup(&sblock, offset));
} else {
idesc->id_numfrags = sblock.fs_frag;
}
if (dino.di_db[i] == 0) {
if ((ndb > 0) && (idesc->id_firsthole < 0)) {
idesc->id_firsthole = i;
}
continue;
}
idesc->id_blkno = dino.di_db[i];
if (idesc->id_type == ADDR || idesc->id_type == ACL)
ret = (*idesc->id_func)(idesc);
else
ret = dirscan(idesc);
/*
* Need to clear the entry, now that we're done with
* it. We depend on freeblk() ignoring a request to
* free already-free fragments to handle the problem of
* a partial block.
*/
if ((action == CKI_TRUNCATE) &&
(idesc->id_truncto >= 0) &&
(idesc->id_lbn >= idesc->id_truncto)) {
dp = ginode(idesc->id_number);
/*
* The (int) cast is safe, in that if di_size won't
* fit, it'll be a multiple of any legal fs_frag,
* thus giving a zero result. That value, in turn
* means we're doing an entire block.
*/
frags = howmany((int)dp->di_size, sblock.fs_fsize) %
sblock.fs_frag;
if (frags == 0)
frags = sblock.fs_frag;
freeblk(idesc->id_number, dp->di_db[i],
frags);
dp = ginode(idesc->id_number);
dp->di_db[i] = 0;
inodirty();
ret |= ALTERED;
}
if (ret & STOP)
return (ret);
}
#ifdef lint
/*
* Cure a lint complaint of ``possible use before set''.
* Apparently it can't quite figure out the switch statement.
*/
indir_data_blks = 0;
#endif
/*
* indir_data_blks contains the number of data blocks in all
* the previous levels for this iteration. E.g., for the
* single indirect case (i = 0, di_ib[i] != 0), NDADDR's worth
* of blocks have already been covered by the direct blocks
* (di_db[]). At the triple indirect level (i = NIADDR - 1),
* it is all of the number of data blocks that were covered
* by the second indirect, single indirect, and direct block
* levels.
*/
idesc->id_numfrags = sblock.fs_frag;
ndb = howmany(dino.di_size, (u_offset_t)sblock.fs_bsize);
for (i = 0; i < NIADDR; i++) {
(void) get_indir_offsets(i, ndb, &indir_data_blks,
&last_indir_blk);
if (dino.di_ib[i] != 0) {
/*
* We'll only clear di_ib[i] if the first entry (and
* therefore all of them) is to be cleared, since we
* only go through this code on the first entry of
* each level of indirection. The +1 is to account
* for the fact that we don't modify id_lbn until
* we actually start processing on a data block.
*/
idesc->id_blkno = dino.di_ib[i];
ret = iblock(idesc, i + 1,
(u_offset_t)howmany(dino.di_size,
(u_offset_t)sblock.fs_bsize) - indir_data_blks,
action);
if ((action == CKI_TRUNCATE) &&
(idesc->id_truncto <= indir_data_blks) &&
((idesc->id_lbn + 1) >= indir_data_blks) &&
((idesc->id_lbn + 1) <= last_indir_blk)) {
dp = ginode(idesc->id_number);
if (dp->di_ib[i] != 0) {
freeblk(idesc->id_number, dp->di_ib[i],
sblock.fs_frag);
}
}
if (ret & STOP)
return (ret);
} else {
/*
* Need to know which of the file's logical blocks
* reside in the missing indirect block. However, the
* precise location is only needed for truncating
* directories, and level-of-indirection precision is
* sufficient for that.
*/
if ((indir_data_blks < ndb) &&
(idesc->id_firsthole < 0)) {
idesc->id_firsthole = indir_data_blks;
}
}
}
return (KEEPON);
}
static int
get_indir_offsets(int ilevel_wanted, daddr_t ndb, int *data_blks,
int *last_blk)
{
int ndb_ilevel = -1;
int ilevel;
int dblks, lblk;
for (ilevel = 0; ilevel < NIADDR; ilevel++) {
switch (ilevel) {
case 0: /* SINGLE */
dblks = NDADDR;
lblk = dblks + NINDIR(&sblock) - 1;
break;
case 1: /* DOUBLE */
dblks = NDADDR + NINDIR(&sblock);
lblk = dblks + (NINDIR(&sblock) * NINDIR(&sblock)) - 1;
break;
case 2: /* TRIPLE */
dblks = NDADDR + NINDIR(&sblock) +
(NINDIR(&sblock) * NINDIR(&sblock));
lblk = dblks + (NINDIR(&sblock) * NINDIR(&sblock) *
NINDIR(&sblock)) - 1;
break;
default:
exitstat = EXERRFATAL;
/*
* Translate from zero-based array to
* one-based human-style counting.
*/
errexit("panic: indirection level %d not 1, 2, or 3",
ilevel + 1);
/* NOTREACHED */
}
if (dblks < ndb && ndb <= lblk)
ndb_ilevel = ilevel;
if (ilevel == ilevel_wanted) {
if (data_blks != NULL)
*data_blks = dblks;
if (last_blk != NULL)
*last_blk = lblk;
}
}
return (ndb_ilevel);
}
static int
iblock(struct inodesc *idesc, int ilevel, u_offset_t iblks,
enum cki_action action)
{
struct bufarea *bp;
int i, n;
int (*func)(struct inodesc *) = NULL;
u_offset_t fsbperindirb;
daddr32_t last_lbn;
int nif;
char buf[BUFSIZ];
n = KEEPON;
switch (idesc->id_type) {
case ADDR:
func = idesc->id_func;
if (((n = (*func)(idesc)) & KEEPON) == 0)
return (n);
break;
case ACL:
func = idesc->id_func;
break;
case DATA:
func = dirscan;
break;
default:
errexit("unknown inodesc type %d in iblock()", idesc->id_type);
/* NOTREACHED */
}
if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
return ((idesc->id_type == ACL) ? STOP : SKIP);
}
bp = getdatablk(idesc->id_blkno, (size_t)sblock.fs_bsize);
if (bp->b_errs != 0) {
brelse(bp);
return (SKIP);
}
ilevel--;
/*
* Trivia note: the BSD fsck has the number of bytes remaining
* as the third argument to iblock(), so the equivalent of
* fsbperindirb starts at fs_bsize instead of one. We're
* working in units of filesystem blocks here, not bytes or
* fragments.
*/
for (fsbperindirb = 1, i = 0; i < ilevel; i++) {
fsbperindirb *= (u_offset_t)NINDIR(&sblock);
}
/*
* nif indicates the next "free" pointer (as an array index) in this
* indirect block, based on counting the blocks remaining in the
* file after subtracting all previously processed blocks.
* This figure is based on the size field of the inode.
*
* Note that in normal operation, nif may initially be calculated
* as larger than the number of pointers in this block (as when
* there are more indirect blocks following); if that is
* the case, nif is limited to the max number of pointers per
* indirect block.
*
* Also note that if an inode is inconsistent (has more blocks
* allocated to it than the size field would indicate), the sweep
* through any indirect blocks directly pointed at by the inode
* continues. Since the block offset of any data blocks referenced
* by these indirect blocks is greater than the size of the file,
* the index nif may be computed as a negative value.
* In this case, we reset nif to indicate that all pointers in
* this retrieval block should be zeroed and the resulting
* unreferenced data and/or retrieval blocks will be recovered
* through garbage collection later.
*/
nif = (offset_t)howmany(iblks, fsbperindirb);
if (nif > NINDIR(&sblock))
nif = NINDIR(&sblock);
else if (nif < 0)
nif = 0;
/*
* first pass: all "free" retrieval pointers (from [nif] thru
* the end of the indirect block) should be zero. (This
* assertion does not hold for directories, which may be
* truncated without releasing their allocated space)
*/
if (nif < NINDIR(&sblock) && (idesc->id_func == pass1check ||
idesc->id_func == pass3bcheck)) {
for (i = nif; i < NINDIR(&sblock); i++) {
if (bp->b_un.b_indir[i] == 0)
continue;
(void) sprintf(buf, "PARTIALLY TRUNCATED INODE I=%lu",
(ulong_t)idesc->id_number);
if (preen) {
pfatal(buf);
} else if (dofix(idesc, buf)) {
freeblk(idesc->id_number,
bp->b_un.b_indir[i],
sblock.fs_frag);
bp->b_un.b_indir[i] = 0;
dirty(bp);
}
}
flush(fswritefd, bp);
}
/*
* second pass: all retrieval pointers referring to blocks within
* a valid range [0..filesize] (both indirect and data blocks)
* are examined in the same manner as ckinode() checks the
* direct blocks in the inode. Sweep through from
* the first pointer in this retrieval block to [nif-1].
*/
last_lbn = howmany(idesc->id_filesize, sblock.fs_bsize);
for (i = 0; i < nif; i++) {
if (ilevel == 0)
idesc->id_lbn++;
if (bp->b_un.b_indir[i] != 0) {
idesc->id_blkno = bp->b_un.b_indir[i];
if (ilevel > 0) {
n = iblock(idesc, ilevel, iblks, action);
/*
* Each iteration decreases "remaining block
* count" by the number of blocks accessible
* by a pointer at this indirect block level.
*/
iblks -= fsbperindirb;
} else {
/*
* If we're truncating, func will discard
* the data block for us.
*/
n = (*func)(idesc);
}
if ((action == CKI_TRUNCATE) &&
(idesc->id_truncto >= 0) &&
(idesc->id_lbn >= idesc->id_truncto)) {
freeblk(idesc->id_number, bp->b_un.b_indir[i],
sblock.fs_frag);
}
/*
* Note that truncation never gets STOP back
* under normal circumstances. Abnormal would
* be a bad acl short-circuit in iblock() or
* an out-of-range failure in pass4check().
* We still want to keep going when truncating
* under those circumstances, since the whole
* point of truncating is to get rid of all
* that.
*/
if ((n & STOP) && (action != CKI_TRUNCATE)) {
brelse(bp);
return (n);
}
} else {
if ((idesc->id_lbn < last_lbn) &&
(idesc->id_firsthole < 0)) {
idesc->id_firsthole = idesc->id_lbn;
}
if (idesc->id_type == DATA) {
/*
* No point in continuing in the indirect
* blocks of a directory, since they'll just
* get freed anyway.
*/
brelse(bp);
return ((n & ~KEEPON) | STOP);
}
}
}
brelse(bp);
return (KEEPON);
}
/*
* Check that a block is a legal block number.
* Return 0 if in range, 1 if out of range.
*/
int
chkrange(daddr32_t blk, int cnt)
{
int c;
if (cnt <= 0 || blk <= 0 || ((unsigned)blk >= (unsigned)maxfsblock) ||
((cnt - 1) > (maxfsblock - blk))) {
if (debug)
(void) printf(
"Bad fragment range: should be 1 <= %d..%d < %d\n",
blk, blk + cnt, maxfsblock);
return (1);
}
if ((cnt > sblock.fs_frag) ||
((fragnum(&sblock, blk) + cnt) > sblock.fs_frag)) {
if (debug)
(void) printf("Bad fragment size: size %d\n", cnt);
return (1);
}
c = dtog(&sblock, blk);
if (blk < cgdmin(&sblock, c)) {
if ((unsigned)(blk + cnt) > (unsigned)cgsblock(&sblock, c)) {
if (debug)
(void) printf(
"Bad fragment position: %d..%d spans start of cg metadata\n",
blk, blk + cnt);
return (1);
}
} else {
if ((unsigned)(blk + cnt) > (unsigned)cgbase(&sblock, c+1)) {
if (debug)
(void) printf(
"Bad frag pos: %d..%d crosses end of cg\n",
blk, blk + cnt);
return (1);
}
}
return (0);
}
/*
* General purpose interface for reading inodes.
*/
/*
* Note that any call to ginode() can potentially invalidate any
* dinode pointers previously acquired from it. To avoid pain,
* make sure to always call inodirty() immediately after modifying
* an inode, if there's any chance of ginode() being called after
* that. Also, always call ginode() right before you need to access
* an inode, so that there won't be any surprises from functions
* called between the previous ginode() invocation and the dinode
* use.
*
* Despite all that, we aren't doing the amount of i/o that's implied,
* as we use the buffer cache that getdatablk() and friends maintain.
*/
static fsck_ino_t startinum = -1;
struct dinode *
ginode(fsck_ino_t inum)
{
daddr32_t iblk;
struct dinode *dp;
if (inum < UFSROOTINO || inum > maxino) {
errexit("bad inode number %d to ginode\n", inum);
}
if (startinum == -1 ||
pbp == NULL ||
inum < startinum ||
inum >= (fsck_ino_t)(startinum + (fsck_ino_t)INOPB(&sblock))) {
iblk = itod(&sblock, inum);
if (pbp != NULL) {
brelse(pbp);
}
/*
* We don't check for errors here, because we can't
* tell our caller about it, and the zeros that will
* be in the buffer are just as good as anything we
* could fake.
*/
pbp = getdatablk(iblk, (size_t)sblock.fs_bsize);
startinum =
(fsck_ino_t)((inum / INOPB(&sblock)) * INOPB(&sblock));
}
dp = &pbp->b_un.b_dinode[inum % INOPB(&sblock)];
if (dp->di_suid != UID_LONG)
dp->di_uid = dp->di_suid;
if (dp->di_sgid != GID_LONG)
dp->di_gid = dp->di_sgid;
return (dp);
}
/*
* Special purpose version of ginode used to optimize first pass
* over all the inodes in numerical order. It bypasses the buffer
* system used by ginode(), etc in favour of reading the bulk of a
* cg's inodes at one time.
*/
static fsck_ino_t nextino, lastinum;
static int64_t readcnt, readpercg, fullcnt, inobufsize;
static int64_t partialcnt, partialsize;
static size_t lastsize;
static struct dinode *inodebuf;
static diskaddr_t currentdblk;
static struct dinode *currentinode;
struct dinode *
getnextinode(fsck_ino_t inum)
{
size_t size;
diskaddr_t dblk;
static struct dinode *dp;
if (inum != nextino++ || inum > maxino)
errexit("bad inode number %d to nextinode\n", inum);
/*
* Will always go into the if() the first time we're called,
* so dp will always be valid.
*/
if (inum >= lastinum) {
readcnt++;
dblk = fsbtodb(&sblock, itod(&sblock, lastinum));
currentdblk = dblk;
if (readcnt % readpercg == 0) {
if (partialsize > SIZE_MAX)
errexit(
"Internal error: partialsize overflow");
size = (size_t)partialsize;
lastinum += partialcnt;
} else {
if (inobufsize > SIZE_MAX)
errexit("Internal error: inobufsize overflow");
size = (size_t)inobufsize;
lastinum += fullcnt;
}
/*
* If fsck_bread() returns an error, it will already have
* zeroed out the buffer, so we do not need to do so here.
*/
(void) fsck_bread(fsreadfd, (caddr_t)inodebuf, dblk, size);
lastsize = size;
dp = inodebuf;
}
currentinode = dp;
return (dp++);
}
/*
* Reread the current getnext() buffer. This allows for changing inodes
* other than the current one via ginode()/inodirty()/inoflush().
*
* Just reuses all the interesting variables that getnextinode() set up
* last time it was called. This shouldn't get called often, so we don't
* try to figure out if the caller's actually touched an inode in the
* range we have cached. There could have been an arbitrary number of
* them, after all.
*/
struct dinode *
getnextrefresh(void)
{
if (inodebuf == NULL) {
return (NULL);
}
inoflush();
(void) fsck_bread(fsreadfd, (caddr_t)inodebuf, currentdblk, lastsize);
return (currentinode);
}
void
resetinodebuf(void)
{
startinum = 0;
nextino = 0;
lastinum = 0;
readcnt = 0;
inobufsize = blkroundup(&sblock, INOBUFSIZE);
fullcnt = inobufsize / sizeof (struct dinode);
readpercg = sblock.fs_ipg / fullcnt;
partialcnt = sblock.fs_ipg % fullcnt;
partialsize = partialcnt * sizeof (struct dinode);
if (partialcnt != 0) {
readpercg++;
} else {
partialcnt = fullcnt;
partialsize = inobufsize;
}
if (inodebuf == NULL &&
(inodebuf = (struct dinode *)malloc((unsigned)inobufsize)) == NULL)
errexit("Cannot allocate space for inode buffer\n");
while (nextino < UFSROOTINO)
(void) getnextinode(nextino);
}
void
freeinodebuf(void)
{
if (inodebuf != NULL) {
free((void *)inodebuf);
}
inodebuf = NULL;
}
/*
* Routines to maintain information about directory inodes.
* This is built during the first pass and used during the
* second and third passes.
*
* Enter inodes into the cache.
*/
void
cacheino(struct dinode *dp, fsck_ino_t inum)
{
struct inoinfo *inp;
struct inoinfo **inpp;
uint_t blks;
blks = NDADDR + NIADDR;
inp = (struct inoinfo *)
malloc(sizeof (*inp) + (blks - 1) * sizeof (daddr32_t));
if (inp == NULL)
errexit("Cannot increase directory list\n");
init_inoinfo(inp, dp, inum); /* doesn't touch i_nextlist or i_number */
inpp = &inphead[inum % numdirs];
inp->i_nextlist = *inpp;
*inpp = inp;
inp->i_number = inum;
if (inplast == listmax) {
listmax += 100;
inpsort = (struct inoinfo **)realloc((void *)inpsort,
(unsigned)listmax * sizeof (struct inoinfo *));
if (inpsort == NULL)
errexit("cannot increase directory list");
}
inpsort[inplast++] = inp;
}
/*
* Look up an inode cache structure.
*/
struct inoinfo *
getinoinfo(fsck_ino_t inum)
{
struct inoinfo *inp;
inp = search_cache(inphead[inum % numdirs], inum);
return (inp);
}
/*
* Determine whether inode is in cache.
*/
int
inocached(fsck_ino_t inum)
{
return (search_cache(inphead[inum % numdirs], inum) != NULL);
}
/*
* Clean up all the inode cache structure.
*/
void
inocleanup(void)
{
struct inoinfo **inpp;
if (inphead == NULL)
return;
for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--) {
free((void *)(*inpp));
}
free((void *)inphead);
free((void *)inpsort);
inphead = inpsort = NULL;
}
/*
* Routines to maintain information about acl inodes.
* This is built during the first pass and used during the
* second and third passes.
*
* Enter acl inodes into the cache.
*/
void
cacheacl(struct dinode *dp, fsck_ino_t inum)
{
struct inoinfo *aclp;
struct inoinfo **aclpp;
uint_t blks;
blks = NDADDR + NIADDR;
aclp = (struct inoinfo *)
malloc(sizeof (*aclp) + (blks - 1) * sizeof (daddr32_t));
if (aclp == NULL)
return;
aclpp = &aclphead[inum % numacls];
aclp->i_nextlist = *aclpp;
*aclpp = aclp;
aclp->i_number = inum;
aclp->i_isize = (offset_t)dp->di_size;
aclp->i_blkssize = (size_t)(blks * sizeof (daddr32_t));
(void) memmove(&aclp->i_blks[0], &dp->di_db[0], aclp->i_blkssize);
if (aclplast == aclmax) {
aclmax += 100;
aclpsort = (struct inoinfo **)realloc((char *)aclpsort,
(unsigned)aclmax * sizeof (struct inoinfo *));
if (aclpsort == NULL)
errexit("cannot increase acl list");
}
aclpsort[aclplast++] = aclp;
}
/*
* Generic cache search function.
* ROOT is the first entry in a hash chain (the caller is expected
* to have done the initial bucket lookup). KEY is what's being
* searched for.
*
* Returns a pointer to the entry if it is found, NULL otherwise.
*/
static struct inoinfo *
search_cache(struct inoinfo *element, fsck_ino_t key)
{
while (element != NULL) {
if (element->i_number == key)
break;
element = element->i_nextlist;
}
return (element);
}
void
inodirty(void)
{
dirty(pbp);
}
static void
inoflush(void)
{
if (pbp != NULL)
flush(fswritefd, pbp);
}
/*
* Interactive wrapper for freeino(), for those times when we're
* not sure if we should throw something away.
*/
void
clri(struct inodesc *idesc, char *type, int verbose, int corrupting)
{
int need_parent;
struct dinode *dp;
if (statemap[idesc->id_number] == USTATE)
return;
dp = ginode(idesc->id_number);
if (verbose == CLRI_VERBOSE) {
pwarn("%s %s", type, file_id(idesc->id_number, dp->di_mode));
pinode(idesc->id_number);
}
if (preen || (reply("CLEAR") == 1)) {
need_parent = (corrupting == CLRI_NOP_OK) ?
TI_NOPARENT : TI_PARENT;
freeino(idesc->id_number, need_parent);
if (preen)
(void) printf(" (CLEARED)\n");
remove_orphan_dir(idesc->id_number);
} else if (corrupting == CLRI_NOP_CORRUPT) {
iscorrupt = 1;
}
(void) printf("\n");
}
/*
* Find the directory entry for the inode noted in id_parent (which is
* not necessarily the parent of anything, we're just using a convenient
* field.
*/
int
findname(struct inodesc *idesc)
{
struct direct *dirp = idesc->id_dirp;
if (dirp->d_ino != idesc->id_parent)
return (KEEPON);
(void) memmove(idesc->id_name, dirp->d_name,
MIN(dirp->d_namlen, MAXNAMLEN) + 1);
return (STOP|FOUND);
}
/*
* Find the inode number associated with the given name.
*/
int
findino(struct inodesc *idesc)
{
struct direct *dirp = idesc->id_dirp;
if (dirp->d_ino == 0)
return (KEEPON);
if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
dirp->d_ino >= UFSROOTINO && dirp->d_ino <= maxino) {
idesc->id_parent = dirp->d_ino;
return (STOP|FOUND);
}
return (KEEPON);
}
int
cleardirentry(fsck_ino_t parentdir, fsck_ino_t target)
{
struct inodesc idesc;
struct dinode *dp;
dp = ginode(parentdir);
init_inodesc(&idesc);
idesc.id_func = clearanentry;
idesc.id_parent = target;
idesc.id_type = DATA;
idesc.id_fix = NOFIX;
return (ckinode(dp, &idesc, CKI_TRAVERSE));
}
static int
clearanentry(struct inodesc *idesc)
{
struct direct *dirp = idesc->id_dirp;
if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
idesc->id_entryno++;
return (KEEPON);
}
dirp->d_ino = 0;
return (STOP|FOUND|ALTERED);
}
void
pinode(fsck_ino_t ino)
{
struct dinode *dp;
(void) printf(" I=%lu ", (ulong_t)ino);
if (ino < UFSROOTINO || ino > maxino)
return;
dp = ginode(ino);
pdinode(dp);
}
static void
pdinode(struct dinode *dp)
{
char *p;
struct passwd *pw;
time_t t;
(void) printf(" OWNER=");
if ((pw = getpwuid((int)dp->di_uid)) != 0)
(void) printf("%s ", pw->pw_name);
else
(void) printf("%lu ", (ulong_t)dp->di_uid);
(void) printf("MODE=%o\n", dp->di_mode);
if (preen)
(void) printf("%s: ", devname);
(void) printf("SIZE=%lld ", (longlong_t)dp->di_size);
/* ctime() ignores LOCALE, so this is safe */
t = (time_t)dp->di_mtime;
p = ctime(&t);
(void) printf("MTIME=%12.12s %4.4s ", p + 4, p + 20);
}
void
blkerror(fsck_ino_t ino, char *type, daddr32_t blk, daddr32_t lbn)
{
pfatal("FRAGMENT %d %s I=%u LFN %d", blk, type, ino, lbn);
(void) printf("\n");
switch (statemap[ino] & ~INDELAYD) {
case FSTATE:
case FZLINK:
statemap[ino] = FCLEAR;
return;
case DFOUND:
case DSTATE:
case DZLINK:
statemap[ino] = DCLEAR;
add_orphan_dir(ino);
return;
case SSTATE:
statemap[ino] = SCLEAR;
return;
case FCLEAR:
case DCLEAR:
case SCLEAR:
return;
default:
errexit("BAD STATE 0x%x TO BLKERR\n", statemap[ino]);
/* NOTREACHED */
}
}
/*
* allocate an unused inode
*/
fsck_ino_t
allocino(fsck_ino_t request, int type)
{
fsck_ino_t ino;
struct dinode *dp;
struct cg *cgp = &cgrp;
int cg;
time_t t;
caddr_t err;
if (debug && (request != 0) && (request != UFSROOTINO))
errexit("assertion failed: allocino() asked for "
"inode %d instead of 0 or %d",
(int)request, (int)UFSROOTINO);
/*
* We know that we're only going to get requests for UFSROOTINO
* or 0. If UFSROOTINO is wanted, then it better be available
* because our caller is trying to recreate the root directory.
* If we're asked for 0, then which one we return doesn't matter.
* We know that inodes 0 and 1 are never valid to return, so we
* the start at the lowest-legal inode number.
*
* If we got a request for UFSROOTINO, then request != 0, and
* this pair of conditionals is the only place that treats
* UFSROOTINO specially.
*/
if (request == 0)
request = UFSROOTINO;
else if (statemap[request] != USTATE)
return (0);
/*
* Doesn't do wrapping, since we know we started at
* the smallest inode.
*/
for (ino = request; ino < maxino; ino++)
if (statemap[ino] == USTATE)
break;
if (ino == maxino)
return (0);
/*
* In pass5, we'll calculate the bitmaps and counts all again from
* scratch and do a comparison, but for that to work the cg has
* to know what in-memory changes we've made to it. If we have
* trouble reading the cg, cg_sanity() should kick it out so
* we can skip explicit i/o error checking here.
*/
cg = itog(&sblock, ino);
(void) getblk(&cgblk, cgtod(&sblock, cg), (size_t)sblock.fs_cgsize);
err = cg_sanity(cgp, cg);
if (err != NULL) {
pfatal("CG %d: %s\n", cg, err);
free((void *)err);
if (reply("REPAIR") == 0)
errexit("Program terminated.");
fix_cg(cgp, cg);
}
setbit(cg_inosused(cgp), ino % sblock.fs_ipg);
cgp->cg_cs.cs_nifree--;
cgdirty();
if (lastino < ino)
lastino = ino;
/*
* Don't currently support IFATTRDIR or any of the other
* types, as they aren't needed.
*/
switch (type & IFMT) {
case IFDIR:
statemap[ino] = DSTATE;
cgp->cg_cs.cs_ndir++;
break;
case IFREG:
case IFLNK:
statemap[ino] = FSTATE;
break;
default:
/*
* Pretend nothing ever happened. This clears the
* dirty flag, among other things.
*/
initbarea(&cgblk);
if (debug)
(void) printf("allocino: unknown type 0%o\n",
type & IFMT);
return (0);
}
/*
* We're allocating what should be a completely-unused inode,
* so make sure we don't inherit anything from any previous
* incarnations.
*/
dp = ginode(ino);
(void) memset((void *)dp, 0, sizeof (struct dinode));
dp->di_db[0] = allocblk(1);
if (dp->di_db[0] == 0) {
statemap[ino] = USTATE;
return (0);
}
dp->di_mode = (mode_t)type;
(void) time(&t);
dp->di_atime = (time32_t)t;
dp->di_ctime = dp->di_atime;
dp->di_mtime = dp->di_ctime;
dp->di_size = (u_offset_t)sblock.fs_fsize;
dp->di_blocks = btodb(sblock.fs_fsize);
n_files++;
inodirty();
return (ino);
}
/*
* Release some or all of the blocks of an inode.
* Only truncates down. Assumes new_length is appropriately aligned
* to a block boundary (or a directory block boundary, if it's a
* directory).
*
* If this is a directory, discard all of its contents first, so
* we don't create a bunch of orphans that would need another fsck
* run to clean up.
*
* Even if truncating to zero length, the inode remains allocated.
*/
void
truncino(fsck_ino_t ino, offset_t new_length, int update)
{
struct inodesc idesc;
struct inoinfo *iip;
struct dinode *dp;
fsck_ino_t parent;
mode_t mode;
caddr_t message;
int isdir, islink;
int ilevel, dblk;
dp = ginode(ino);
mode = (dp->di_mode & IFMT);
isdir = (mode == IFDIR) || (mode == IFATTRDIR);
islink = (mode == IFLNK);
if (isdir) {
/*
* Go with the parent we found by chasing references,
* if we've gotten that far. Otherwise, use what the
* directory itself claims. If there's no ``..'' entry
* in it, give up trying to get the link counts right.
*/
if (update == TI_NOPARENT) {
parent = -1;
} else {
iip = getinoinfo(ino);
if (iip != NULL) {
parent = iip->i_parent;
} else {
parent = lookup_dotdot_ino(ino);
if (parent != 0) {
/*
* Make sure that the claimed
* parent actually has a
* reference to us.
*/
dp = ginode(parent);
idesc.id_name = lfname;
idesc.id_type = DATA;
idesc.id_func = findino;
idesc.id_number = ino;
idesc.id_fix = DONTKNOW;
if ((ckinode(dp, &idesc,
CKI_TRAVERSE) & FOUND) == 0)
parent = 0;
}
}
}
mark_delayed_inodes(ino, numfrags(&sblock, new_length));
if (parent > 0) {
dp = ginode(parent);
LINK_RANGE(message, dp->di_nlink, -1);
if (message != NULL) {
LINK_CLEAR(message, parent, dp->di_mode,
&idesc);
if (statemap[parent] == USTATE)
goto no_parent_update;
}
TRACK_LNCNTP(parent, lncntp[parent]--);
} else if ((mode == IFDIR) && (parent == 0)) {
/*
* Currently don't have a good way to
* handle this, so throw up our hands.
* However, we know that we can still
* do some good if we continue, so
* don't actually exit yet.
*
* We don't do it for attrdirs,
* because there aren't link counts
* between them and their parents.
*/
pwarn("Could not determine former parent of "
"inode %d, link counts are possibly\n"
"incorrect. Please rerun fsck(8) to "
"correct this.\n",
ino);
iscorrupt = 1;
}
/*
* ...else if it's a directory with parent == -1, then
* we've not gotten far enough to know connectivity,
* and it'll get handled automatically later.
*/
}
no_parent_update:
init_inodesc(&idesc);
idesc.id_type = ADDR;
idesc.id_func = pass4check;
idesc.id_number = ino;
idesc.id_fix = DONTKNOW;
idesc.id_truncto = howmany(new_length, sblock.fs_bsize);
dp = ginode(ino);
if (!islink && ckinode(dp, &idesc, CKI_TRUNCATE) & ALTERED)
inodirty();
/*
* This has to be done after ckinode(), so that all of
* the fragments get visited. Note that we assume we're
* always truncating to a block boundary, rather than a
* fragment boundary.
*/
dp = ginode(ino);
dp->di_size = new_length;
/*
* Clear now-obsolete pointers.
*/
for (dblk = idesc.id_truncto + 1; dblk < NDADDR; dblk++) {
dp->di_db[dblk] = 0;
}
ilevel = get_indir_offsets(-1, idesc.id_truncto, NULL, NULL);
for (ilevel++; ilevel < NIADDR; ilevel++) {
dp->di_ib[ilevel] = 0;
}
inodirty();
}
/*
* Release an inode's resources, then release the inode itself.
*/
void
freeino(fsck_ino_t ino, int update_parent)
{
int cg;
struct dinode *dp;
struct cg *cgp;
n_files--;
dp = ginode(ino);
/*
* We need to make sure that the file is really a large file.
* Everything bigger than UFS_MAXOFFSET_T is treated as a file with
* negative size, which shall be cleared. (see verify_inode() in
* pass1.c)
*/
if (dp->di_size > (u_offset_t)MAXOFF_T &&
dp->di_size <= (u_offset_t)UFS_MAXOFFSET_T &&
ftypeok(dp) &&
(dp->di_mode & IFMT) != IFBLK &&
(dp->di_mode & IFMT) != IFCHR) {
largefile_count--;
}
truncino(ino, 0, update_parent);
dp = ginode(ino);
if ((dp->di_mode & IFMT) == IFATTRDIR) {
clearshadow(ino, &attrclientinfo);
dp = ginode(ino);
}
clearinode(dp);
inodirty();
statemap[ino] = USTATE;
/*
* Keep the disk in sync with us so that pass5 doesn't get
* upset about spurious inconsistencies.
*/
cg = itog(&sblock, ino);
(void) getblk(&cgblk, (diskaddr_t)cgtod(&sblock, cg),
(size_t)sblock.fs_cgsize);
cgp = cgblk.b_un.b_cg;
clrbit(cg_inosused(cgp), ino % sblock.fs_ipg);
cgp->cg_cs.cs_nifree += 1;
cgdirty();
sblock.fs_cstotal.cs_nifree += 1;
sbdirty();
}
void
init_inoinfo(struct inoinfo *inp, struct dinode *dp, fsck_ino_t inum)
{
inp->i_parent = ((inum == UFSROOTINO) ? UFSROOTINO : (fsck_ino_t)0);
inp->i_dotdot = (fsck_ino_t)0;
inp->i_isize = (offset_t)dp->di_size;
inp->i_blkssize = (NDADDR + NIADDR) * sizeof (daddr32_t);
inp->i_extattr = dp->di_oeftflag;
(void) memmove((void *)&inp->i_blks[0], (void *)&dp->di_db[0],
inp->i_blkssize);
}
/*
* Return the inode number in the ".." entry of the provided
* directory inode.
*/
static int
lookup_dotdot_ino(fsck_ino_t ino)
{
struct inodesc idesc;
init_inodesc(&idesc);
idesc.id_type = DATA;
idesc.id_func = findino;
idesc.id_name = "..";
idesc.id_number = ino;
idesc.id_fix = NOFIX;
if ((ckinode(ginode(ino), &idesc, CKI_TRAVERSE) & FOUND) != 0) {
return (idesc.id_parent);
}
return (0);
}
/*
* Convenience wrapper around ckinode(findino()).
*/
int
lookup_named_ino(fsck_ino_t dir, caddr_t name)
{
struct inodesc idesc;
init_inodesc(&idesc);
idesc.id_type = DATA;
idesc.id_func = findino;
idesc.id_name = name;
idesc.id_number = dir;
idesc.id_fix = NOFIX;
if ((ckinode(ginode(dir), &idesc, CKI_TRAVERSE) & FOUND) != 0) {
return (idesc.id_parent);
}
return (0);
}
/*
* Marks inodes that are being orphaned and might need to be reconnected
* by pass4(). The inode we're traversing is the directory whose
* contents will be reconnected later. id_parent is the lfn at which
* to start looking at said contents.
*/
static int
mark_a_delayed_inode(struct inodesc *idesc)
{
struct direct *dirp = idesc->id_dirp;
if (idesc->id_lbn < idesc->id_parent) {
return (KEEPON);
}
if (dirp->d_ino != 0 &&
strcmp(dirp->d_name, ".") != 0 &&
strcmp(dirp->d_name, "..") != 0) {
statemap[dirp->d_ino] &= ~INFOUND;
statemap[dirp->d_ino] |= INDELAYD;
}
return (KEEPON);
}
static void
mark_delayed_inodes(fsck_ino_t ino, daddr32_t first_lfn)
{
struct dinode *dp;
struct inodesc idelayed;
init_inodesc(&idelayed);
idelayed.id_number = ino;
idelayed.id_type = DATA;
idelayed.id_fix = NOFIX;
idelayed.id_func = mark_a_delayed_inode;
idelayed.id_parent = first_lfn;
idelayed.id_entryno = 2;
dp = ginode(ino);
(void) ckinode(dp, &idelayed, CKI_TRAVERSE);
}
/*
* Clear the i_oeftflag/extended attribute pointer from INO.
*/
void
clearattrref(fsck_ino_t ino)
{
struct dinode *dp;
dp = ginode(ino);
if (debug) {
if (dp->di_oeftflag == 0)
(void) printf("clearattref: no attr to clear on %d\n",
ino);
}
dp->di_oeftflag = 0;
inodirty();
}
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that: (1) source distributions retain this entire copyright
* notice and comment, and (2) distributions including binaries display
* the following acknowledgement: ``This product includes software
* developed by the University of California, Berkeley and its contributors''
* in the documentation or other materials provided with the distribution
* and in all advertising materials mentioning features or use of this
* software. Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED '`AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/*
* In-core structures:
* blockmap[]
* A bitmap of block usage very similar to what's on disk, but
* for the entire filesystem rather than just a cylinder group.
* Zero indicates free, one indicates allocated. Note that this
* is opposite the interpretation of a cylinder group's free block
* bitmap.
*
* statemap[]
* Tracks what is known about each inode in the filesystem.
* The fundamental state value is one of USTATE, FSTATE, DSTATE,
* or SSTATE (unallocated, file, directory, shadow/acl).
*
* There are optional modifying attributes as well: INZLINK,
* INFOUND, INCLEAR, INORPHAN, and INDELAYD. The IN prefix
* stands for inode. INZLINK declares that no links (di_nlink ==
* 0) to the inode have been found. It is used instead of
* examining di_nlink because we've always got the statemap[] in
* memory, and on average the odds are against having any given
* inode in the cache. INFOUND flags that an inode was
* encountered during the descent of the filesystem. In other
* words, it's reachable, either by name or by being an acl or
* attribute. INCLEAR declares an intent to call clri() on an
* inode. The INCLEAR and INZLINK attributes are treated in a
* mutually exclusive manner with INCLEAR taking higher precedence
* as the intent is to clear the inode.
*
* INORPHAN indicates that the inode has already been seen once
* in pass3 and determined to be an orphan, so any additional
* encounters don't need to waste cycles redetermining that status.
* It also means we don't ask the user about doing something to the
* inode N times.
*
* INDELAYD marks inodes that pass1 determined needed to be truncated.
* They can't be truncated during that pass, because it depends on
* having a stable world for building the block and inode tables from.
*
* The IN flags rarely used directly, but instead are
* pre-combined through the {D,F,S}ZLINK, DFOUND, and
* {D,F,S}CLEAR convenience macros. This mainly matters when
* trying to use grep on the source.
*
* Three state-test macros are provided: S_IS_DUNFOUND(),
* S_IS_DVALID(), and S_IS_ZLINK(). The first is true when an
* inode's state indicates that it is either a simple directory
* (DSTATE without the INFOUND or INCLEAR modifiers) or a
* directory with the INZLINK modifier set. By definition, if a
* directory has zero links, then it can't be found. As for
* S_IS_DVALID(), it decides if a directory inode is alive.
* Effectively, this translates to whether or not it's been
* flagged for clearing. If not, then it's valid for current
* purposes. This is true even if INZLINK is set, as we may find
* a reference to it later. Finally, S_IS_ZLINK() just picks out
* the INZLINK flag from the state.
*
* The S_*() macros all work on a state value. To simplify a
* bit, the INO_IS_{DUNFOUND,DVALID}() macros take an inode
* number argument. The inode is looked up in the statemap[] and
* the result handed off to the corresponding S_*() macro. This
* is partly a holdover from working with different data
* structures (with the same net intent) in the BSD fsck.
*
* lncntp
* Each entry is initialized to the di_link from the on-disk
* inode. Each time we find one of those links, we decrement it.
* Once all the traversing is done, we should have a zero. If we
* have a positive value, then some reference disappeared
* (probably from a directory that got nuked); deal with it by
* fixing the count. If we have a negative value, then we found
* an extra reference. This is a can't-happen, except in the
* special case of when we reconnect a directory to its parent or
* to lost+found. An exact match between lncntp[] and the on-disk
* inode means it's completely unreferenced.
*
* aclphead
* This is a hash table of the acl inodes in the filesystem.
*
* aclpsort
* The same acls as in aclphead, but as a simple linear array.
* It is used to hold the acl pointers for sorting and scanning
* in pass3b.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/int_types.h>
#include <sys/mntent.h>
#include <sys/fs/ufs_fs.h>
#include <sys/vnode.h>
#include <sys/fs/ufs_inode.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/mnttab.h>
#include <signal.h>
#include <string.h>
#include <sys/vfstab.h>
#include <sys/statvfs.h>
#include <sys/filio.h>
#include <ustat.h>
#include <errno.h>
#include "fsck.h"
static void usage(void) __NORETURN;
static long argtol(int, char *, char *, int);
static void checkfilesys(char *);
static void check_sanity(char *);
static void report_limbo(const void *, VISIT, int);
#define QUICK_CHECK 'm' /* are things ok according to superblock? */
#define ALL_no 'n' /* auto-answer interactive questions `no' */
#define ALL_NO 'N' /* auto-answer interactive questions `no' */
#define UFS_OPTS 'o' /* ufs-specific options, see subopts[] */
#define ECHO_CMD 'V' /* echo the command line */
#define ALL_yes 'y' /* auto-answer interactive questions `yes' */
#define ALL_YES 'Y' /* auto-answer interactive questions `yes' */
#define VERBOSE 'v' /* be chatty */
static char *subopts[] = {
#define PREEN 0 /* non-interactive mode (parent is parallel) */
"p",
#define BLOCK 1 /* alternate superblock */
"b",
#define DEBUG 2 /* yammer */
"d",
#define ONLY_WRITES 3 /* check all writable filesystems */
"w",
#define FORCE 4 /* force checking, even if clean */
"f",
NULL
};
/*
* Filesystems that are `magical' - if they exist in vfstab,
* then they have to be mounted for the system to have gotten
* far enough to be able to run fsck. Thus, don't get all
* bent out of shape if we're asked to check it and it is mounted.
*/
char *magic_fs[] = {
"", /* MAGIC_NONE, for normal filesystems */
"/", /* MAGIC_ROOT */
"/usr", /* MAGIC_USR */
NULL /* MAGIC_LIMIT */
};
daddr32_t bflag;
daddr32_t n_blks;
daddr32_t maxfsblock;
int debug;
int errorlocked;
int exitstat;
int fflag;
int fsmodified;
int fswritefd;
int iscorrupt;
int islog;
int islogok;
int interrupted;
int mflag;
int mountfd;
int overflowed_lf;
int rflag;
int reattached_dir;
int broke_dir_link;
int verbose;
char hotroot;
char mountedfs;
char nflag;
char preen;
char rerun;
char *blockmap;
char *devname;
char yflag;
short *lncntp;
ushort_t *statemap;
fsck_ino_t maxino;
fsck_ino_t countdirs;
fsck_ino_t n_files;
void *limbo_dirs;
int
main(int argc, char *argv[])
{
int c;
int wflag = 0;
char *suboptions, *value;
struct rlimit rlimit;
extern int optind;
extern char *optarg;
while ((c = getopt(argc, argv, "mnNo:VvyY")) != EOF) {
switch (c) {
case QUICK_CHECK:
mflag++;
break;
case ALL_no:
case ALL_NO:
nflag++;
yflag = 0;
break;
case VERBOSE:
verbose++;
break;
case UFS_OPTS:
/*
* ufs specific options.
*/
if (optarg == NULL) {
usage();
}
suboptions = optarg;
while (*suboptions != '\0') {
switch (getsubopt(&suboptions, subopts,
&value)) {
case PREEN:
preen++;
break;
case BLOCK:
bflag = argtol(BLOCK, "block",
value, 10);
(void) printf("Alternate super block "
"location: %ld.\n",
(long)bflag);
break;
case DEBUG:
debug++;
verbose++;
break;
case ONLY_WRITES:
/* check only writable filesystems */
wflag++;
break;
case FORCE:
fflag++;
break;
default:
usage();
}
}
break;
case ECHO_CMD:
{
int opt_count;
char *opt_text;
(void) printf("fsck -F ufs ");
for (opt_count = 1; opt_count < argc;
opt_count++) {
opt_text = argv[opt_count];
if (opt_text)
(void) printf("%s ", opt_text);
}
(void) printf("\n");
}
break;
case ALL_yes:
case ALL_YES:
yflag++;
nflag = 0;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc == 0)
usage();
rflag++; /* check raw devices where we can */
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
(void) signal(SIGINT, catch);
if (preen)
(void) signal(SIGQUIT, catchquit);
/*
* Push up our allowed memory limit so we can cope
* with huge file systems.
*/
if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
rlimit.rlim_cur = rlimit.rlim_max;
(void) setrlimit(RLIMIT_DATA, &rlimit);
}
/*
* There are a lot of places where we just exit if a problem is
* found. This means that we won't necessarily check everything
* we were asked to. It would be nice to do everything, and
* then provide a summary when we're done. However, the
* interface doesn't really allow us to do that in any useful
* way. So, we'll just bail on the first unrecoverable
* problem encountered. If we've been run by the generic
* wrapper, we were only given one filesystem to check, so the
* multi-fs case implies being run manually; that means the
* user can rerun us on the remaining filesystems when it's
* convenient for them.
*/
while (argc-- > 0) {
if (wflag && !writable(*argv)) {
(void) fprintf(stderr, "not writeable '%s'\n", *argv);
argv++;
if (exitstat == 0)
exitstat = EXBADPARM;
} else {
checkfilesys(*argv++);
}
}
if (interrupted)
exitstat = EXSIGNAL;
exit(exitstat);
}
/*
* A relatively intelligent strtol(). Note that if str is NULL, we'll
* exit, so ret does not actually need to be pre-initialized. Lint
* doesn't believe this, and it's harmless enough to make lint happy here.
*/
static long
argtol(int flag, char *req, char *str, int base)
{
char *cp = str;
long ret = -1;
errno = 0;
if (str != NULL)
ret = strtol(str, &cp, base);
if (cp == str || *cp) {
(void) fprintf(stderr, "-%c flag requires a %s\n", flag, req);
exit(EXBADPARM);
}
if (errno != 0) {
(void) fprintf(stderr, "-%c %s value out of range\n",
flag, req);
}
return (ret);
}
/*
* Check the specified file system.
*/
static void
checkfilesys(char *filesys)
{
daddr32_t n_ffree, n_bfree;
char *devstr;
fsck_ino_t files;
daddr32_t blks;
fsck_ino_t inumber;
int zlinks_printed;
fsck_ino_t limbo_victim;
double dbl_nffree, dbl_dsize;
int quiet_dups;
mountfd = -1;
hotroot = 0;
mountedfs = M_NOMNT;
reattached_dir = 0;
broke_dir_link = 0;
iscorrupt = 1; /* assume failure in setup() */
islog = 0;
islogok = 0;
overflowed_lf = 0;
errorlocked = is_errorlocked(filesys);
limbo_dirs = NULL;
if ((devstr = setup(filesys)) == NULL) {
if (!iscorrupt) {
return;
}
if (preen)
pfatal("CAN'T CHECK FILE SYSTEM.");
if (exitstat == 0)
exitstat = mflag ? EXUMNTCHK : EXERRFATAL;
exit(exitstat);
} else {
devname = devstr;
}
if (mflag) {
check_sanity(filesys);
/* NOTREACHED */
}
if (debug)
printclean();
iscorrupt = 0; /* setup() succeeded, assume good filesystem */
/*
* 1: scan inodes tallying blocks used
*/
if (!preen) {
/* hotroot is reported as such in setup() if debug is on */
if (mountedfs != M_NOMNT)
(void) printf("** Currently Mounted on %s\n",
sblock.fs_fsmnt);
else
(void) printf("** Last Mounted on %s\n",
sblock.fs_fsmnt);
(void) printf("** Phase 1 - Check Blocks and Sizes\n");
}
pass1();
/*
* 1b: locate first references to duplicates, if any
*/
if (have_dups()) {
if (preen)
pfatal("INTERNAL ERROR: dups with -o p");
(void) printf("** Phase 1b - Rescan For More DUPS\n");
pass1b();
}
/*
* 2: traverse directories from root to mark all connected directories
*/
if (!preen)
(void) printf("** Phase 2 - Check Pathnames\n");
pass2();
/*
* 3a: scan inodes looking for disconnected directories.
*/
if (!preen)
(void) printf("** Phase 3a - Check Connectivity\n");
pass3a();
/*
* 3b: check acls
*/
if (!preen)
(void) printf("** Phase 3b - Verify Shadows/ACLs\n");
pass3b();
/*
* 4: scan inodes looking for disconnected files; check reference counts
*/
if (!preen)
(void) printf("** Phase 4 - Check Reference Counts\n");
pass4();
/*
* 5: check and repair resource counts in cylinder groups
*/
if (!preen)
(void) printf("** Phase 5 - Check Cylinder Groups\n");
recount:
pass5();
if (overflowed_lf) {
iscorrupt = 1;
}
if (!nflag && mountedfs == M_RW) {
(void) printf("FILESYSTEM MAY STILL BE INCONSISTENT.\n");
rerun = 1;
}
if (have_dups()) {
quiet_dups = (reply("LIST REMAINING DUPS") == 0);
if (report_dups(quiet_dups) > 0)
iscorrupt = 1;
(void) printf("WARNING: DATA LOSS MAY HAVE OCCURRED DUE TO "
"DUP BLOCKS.\nVERIFY FILE CONTENTS BEFORE USING.\n");
}
if (limbo_dirs != NULL) {
/*
* Don't force iscorrupt, as this is sufficiently
* harmless that the filesystem can be mounted and
* used. We just leak some inodes and/or blocks.
*/
pwarn("Orphan directories not cleared or reconnected:\n");
twalk(limbo_dirs, report_limbo);
while (limbo_dirs != NULL) {
limbo_victim = *(fsck_ino_t *)limbo_dirs;
if (limbo_victim != 0) {
(void) tdelete((void *)limbo_victim,
&limbo_dirs,
ino_t_cmp);
}
}
rerun = 1;
}
if (iscorrupt) {
if (mountedfs == M_RW)
(void) printf("FS IS MOUNTED R/W AND"
" FSCK DID ITS BEST TO FIX"
" INCONSISTENCIES.\n");
else
(void) printf("FILESYSTEM MAY STILL BE"
" INCONSISTENT.\n");
rerun = 1;
}
/*
* iscorrupt must be stable at this point.
* updateclean() returns true when it had to discard the log.
* This can only happen once, since sblock.fs_logbno gets
* cleared as part of that operation.
*/
if (updateclean()) {
if (!preen)
(void) printf(
"Log was discarded, updating cyl groups\n");
goto recount;
}
if (debug)
printclean();
ckfini();
/*
* print out summary statistics
*/
n_ffree = sblock.fs_cstotal.cs_nffree;
n_bfree = sblock.fs_cstotal.cs_nbfree;
files = maxino - UFSROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
blks = n_blks +
sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
if (debug && (files > 0 || blks > 0)) {
countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
pwarn("Reclaimed: %d directories, %d files, %lld fragments\n",
countdirs, files - countdirs,
(longlong_t)blks);
}
dbl_nffree = (double)n_ffree;
dbl_dsize = (double)sblock.fs_dsize;
if (!verbose) {
/*
* Done as one big string to try for a single write,
* so the output doesn't get interleaved with other
* preening fscks.
*/
pwarn("%ld files, %lld used, %lld free "
"(%lld frags, %lld blocks, %.1f%% fragmentation)\n",
(long)n_files, (longlong_t)n_blks,
(longlong_t)n_ffree + sblock.fs_frag * n_bfree,
(longlong_t)n_ffree, (longlong_t)n_bfree,
(dbl_nffree * 100.0) / dbl_dsize);
} else {
pwarn("\nFilesystem summary:\n");
pwarn("Inodes in use: %ld\n", (long)n_files);
pwarn("Blocks in use: %lld\n", (longlong_t)n_blks);
pwarn("Total free fragments: %lld\n",
(longlong_t)n_ffree + sblock.fs_frag * n_bfree);
pwarn("Free fragments not in blocks: %lld\n",
(longlong_t)n_ffree);
pwarn("Total free blocks: %lld\n", (longlong_t)n_bfree);
pwarn("Fragment/block fragmentation: %.1f%%\n",
(dbl_nffree * 100.0) / dbl_dsize);
pwarn("");
if (files < 0)
pwarn("%d inodes missing\n", -files);
if (blks < 0)
pwarn("%lld blocks missing\n", -(longlong_t)blks);
zlinks_printed = 0;
for (inumber = UFSROOTINO; inumber < maxino; inumber++) {
if (S_IS_ZLINK(statemap[inumber])) {
if (zlinks_printed == 0) {
pwarn("The following zero "
"link count inodes remain:");
}
if (zlinks_printed) {
if ((zlinks_printed % 9) == 0)
(void) puts(",\n");
else
(void) puts(", ");
}
(void) printf("%u", inumber);
zlinks_printed++;
}
}
if ((zlinks_printed != 0) && ((zlinks_printed % 9) != 0))
(void) putchar('\n');
}
/*
* Clean up after ourselves, so we can do the next filesystem.
*/
free_dup_state();
inocleanup();
free(blockmap);
free(statemap);
free((void *)lncntp);
lncntp = NULL;
blockmap = NULL;
statemap = NULL;
if (iscorrupt && exitstat == 0)
exitstat = EXFNDERRS;
if (fsmodified)
(void) printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
if (overflowed_lf)
(void) printf("\n***** %s FULL, MUST REMOVE ENTRIES *****\n",
lfname);
if (reattached_dir) {
(void) printf("ORPHANED DIRECTORIES REATTACHED; DIR LINK "
"COUNTS MAY NOT BE CORRECT.\n");
rerun = 1;
}
if (broke_dir_link) {
(void) printf(
"DIRECTORY HARDLINK BROKEN; LOOPS MAY STILL EXIST.\n");
rerun = 1;
}
if (iscorrupt)
(void) printf("***** FILE SYSTEM IS BAD *****\n");
if (rerun) {
if (mountedfs == M_RW)
(void) printf("\n***** PLEASE RERUN FSCK ON UNMOUNTED"
" FILE SYSTEM *****\n");
else
(void) printf("\n***** PLEASE RERUN FSCK *****\n");
}
if ((exitstat == 0) &&
(((mountedfs != M_NOMNT) && !errorlocked) || hotroot)) {
exitstat = EXROOTOKAY;
}
if ((exitstat == 0) && rerun)
exitstat = EXFNDERRS;
if (mountedfs != M_NOMNT) {
if (!fsmodified)
return;
/*
* _FIOFFS is much more effective than a simple sync().
* Note that the original fswritefd was discarded in
* ckfini().
*/
fswritefd = open(devstr, O_RDWR, 0);
if (fswritefd != -1) {
(void) ioctl(fswritefd, _FIOFFS, NULL);
(void) close(fswritefd);
}
if (!preen)
(void) printf("\n***** REBOOT NOW *****\n");
exitstat = EXREBOOTNOW;
}
}
/*
* fsck -m: does the filesystem pass cursory examination
*
* XXX This is very redundant with setup(). The right thing would be
* for setup() to modify its behaviour when mflag is set (less
* chatty, exit instead of return, etc).
*/
void
check_sanity(char *filename)
{
struct stat64 stbd, stbr;
char *devname;
struct ustat usb;
char vfsfilename[MAXPATHLEN];
struct vfstab vfsbuf;
FILE *vfstab;
struct statvfs vfs_stat;
int found_magic[MAGIC_LIMIT];
int magic_cnt;
int is_magic = 0;
int is_block = 0;
int is_file = 0;
(void) memset((void *)found_magic, 0, sizeof (found_magic));
if (stat64(filename, &stbd) < 0) {
(void) fprintf(stderr,
"ufs fsck: sanity check failed : cannot stat %s\n", filename);
exit(EXNOSTAT);
}
if (S_ISBLK(stbd.st_mode)) {
is_block = 1;
} else if (S_ISCHR(stbd.st_mode)) {
is_block = 0;
} else if (S_ISREG(stbd.st_mode)) {
is_file = 1;
}
/*
* Determine if this is the root file system via vfstab. Give up
* silently on failures. The whole point of this is to be tolerant
* of the magic file systems being already mounted.
*/
if (!is_file && (vfstab = fopen(VFSTAB, "r")) != NULL) {
for (magic_cnt = 0; magic_cnt < MAGIC_LIMIT; magic_cnt++) {
if (magic_cnt == MAGIC_NONE)
continue;
if (getvfsfile(vfstab, &vfsbuf,
magic_fs[magic_cnt]) == 0) {
if (is_block)
devname = vfsbuf.vfs_special;
else
devname = vfsbuf.vfs_fsckdev;
if (stat64(devname, &stbr) == 0) {
if (stbr.st_rdev == stbd.st_rdev) {
found_magic[magic_cnt] = 1;
is_magic = magic_cnt;
break;
}
}
}
}
}
/*
* Only works if filename is a block device or if
* character and block device has the same dev_t value.
* This is currently true, but nothing really forces it.
*/
if (!is_magic && (ustat(stbd.st_rdev, &usb) == 0)) {
(void) fprintf(stderr,
"ufs fsck: sanity check: %s already mounted\n", filename);
exit(EXMOUNTED);
}
if (is_magic) {
(void) strcpy(vfsfilename, magic_fs[is_magic]);
if (statvfs(vfsfilename, &vfs_stat) != 0) {
(void) fprintf(stderr, "ufs fsck: Cannot stat %s\n",
vfsfilename);
exit(EXNOSTAT);
}
if (!(vfs_stat.f_flag & ST_RDONLY)) {
/*
* The file system is mounted read/write
* We need to exit saying this. If it's only
* mounted readonly, we can continue.
*/
(void) fprintf(stderr,
"ufs fsck: sanity check:"
"%s already mounted read/write\n", filename);
exit(EXMOUNTED);
}
}
/*
* We know that at boot, the ufs root file system is mounted
* read-only first. After fsck runs, it is remounted as
* read-write. Therefore, we do not need to check for different
* values for fs_state between the root file system and the
* rest of the file systems.
*/
if (islog && !islogok) {
(void) fprintf(stderr,
"ufs fsck: sanity check: %s needs checking\n", filename);
exit(EXUMNTCHK);
}
if ((sblock.fs_state + (long)sblock.fs_time == FSOKAY) &&
(sblock.fs_clean == FSCLEAN || sblock.fs_clean == FSSTABLE ||
(sblock.fs_clean == FSLOG && islog))) {
(void) fprintf(stderr,
"ufs fsck: sanity check: %s okay\n", filename);
} else {
(void) fprintf(stderr,
"ufs fsck: sanity check: %s needs checking\n", filename);
exit(EXUMNTCHK);
}
exit(EXOKAY);
}
caddr_t
hasvfsopt(struct vfstab *vfs, char *opt)
{
struct mnttab mtab;
if (vfs->vfs_mntopts == NULL)
return (NULL);
mtab.mnt_mntopts = vfs->vfs_mntopts;
return (hasmntopt(&mtab, opt));
}
static void __NORETURN
usage(void)
{
(void) fprintf(stderr,
"ufs usage: fsck [-F ufs] [-m] [-n] [-V] [-v] [-y] "
"[-o p,b=#,w,f] [special ....]\n");
exit(EXBADPARM);
}
/*ARGSUSED*/
static void
report_limbo(const void *node, VISIT order, int level)
{
fsck_ino_t ino = *(fsck_ino_t *)node;
if ((order == postorder) || (order == leaf)) {
(void) printf(" Inode %d\n", ino);
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that: (1) source distributions retain this entire copyright
* notice and comment, and (2) distributions including binaries display
* the following acknowledgement: ``This product includes software
* developed by the University of California, Berkeley and its contributors''
* in the documentation or other materials provided with the distribution
* and in all advertising materials mentioning features or use of this
* software. Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/mntent.h>
#include <sys/fs/ufs_fs.h>
#include <sys/vnode.h>
#define _KERNEL
#include <sys/fs/ufs_fsdir.h>
#undef _KERNEL
#include <sys/fs/ufs_inode.h>
#include "fsck.h"
/*
* for each large file (size > MAXOFF_T), the global largefile_count
* gets incremented during this pass.
*/
static uint32_t badblk; /* number seen for the current inode */
static uint32_t dupblk; /* number seen for the current inode */
static void clear_attr_acl(fsck_ino_t, fsck_ino_t, char *);
static void verify_inode(fsck_ino_t, struct inodesc *, fsck_ino_t);
static void check_dirholes(fsck_ino_t, struct inodesc *);
static void collapse_dirhole(fsck_ino_t, struct inodesc *);
static void note_used(daddr32_t);
void
pass1(void)
{
uint_t c, i;
daddr32_t cgd;
struct inodesc idesc;
fsck_ino_t inumber;
fsck_ino_t maxinumber;
/*
* Set file system reserved blocks in used block map.
*/
for (c = 0; c < sblock.fs_ncg; c++) {
cgd = cgdmin(&sblock, c);
if (c == 0) {
/*
* Doing the first cylinder group, account for
* the cg summaries as well.
*/
i = cgbase(&sblock, c);
cgd += howmany(sblock.fs_cssize, sblock.fs_fsize);
} else {
i = cgsblock(&sblock, c);
}
for (; i < cgd; i++) {
note_used(i);
}
}
/*
* Note blocks being used by the log, so we don't declare
* them as available and some time in the future we get a
* freeing free block panic.
*/
if (islog && islogok && sblock.fs_logbno)
examinelog(¬e_used);
/*
* Find all allocated blocks. This must be completed before
* we read the contents of any directories, as dirscan() et al
* don't want to know about block allocation holes. So, part
* of this pass is to truncate any directories with holes to
* just before those holes, so dirscan() can remain blissfully
* ignorant.
*/
inumber = 0;
n_files = n_blks = 0;
resetinodebuf();
maxinumber = sblock.fs_ncg * sblock.fs_ipg;
for (c = 0; c < sblock.fs_ncg; c++) {
for (i = 0; i < sblock.fs_ipg; i++, inumber++) {
if (inumber < UFSROOTINO)
continue;
init_inodesc(&idesc);
idesc.id_type = ADDR;
idesc.id_func = pass1check;
verify_inode(inumber, &idesc, maxinumber);
}
}
freeinodebuf();
}
/*
* Perform checks on an inode and setup/track the state of the inode
* in maps (statemap[], lncntp[]) for future reference and validation.
* Initiate the calls to ckinode and in turn pass1check() to handle
* further validation.
*/
static void
verify_inode(fsck_ino_t inumber, struct inodesc *idesc, fsck_ino_t maxinumber)
{
int j, clear, flags;
int isdir;
char *err;
fsck_ino_t shadow, attrinode;
daddr32_t ndb;
struct dinode *dp;
struct inoinfo *iip;
dp = getnextinode(inumber);
if ((dp->di_mode & IFMT) == 0) {
/* mode and type of file is not set */
if ((memcmp((void *)dp->di_db, (void *)zino.di_db,
NDADDR * sizeof (daddr32_t)) != 0) ||
(memcmp((void *)dp->di_ib, (void *)zino.di_ib,
NIADDR * sizeof (daddr32_t)) != 0) ||
(dp->di_mode != 0) || (dp->di_size != 0)) {
pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
if (reply("CLEAR") == 1) {
dp = ginode(inumber);
clearinode(dp);
inodirty();
} else {
iscorrupt = 1;
}
}
statemap[inumber] = USTATE;
return;
}
isdir = ((dp->di_mode & IFMT) == IFDIR) ||
((dp->di_mode & IFMT) == IFATTRDIR);
lastino = inumber;
if (dp->di_size > (u_offset_t)UFS_MAXOFFSET_T) {
pfatal("NEGATIVE SIZE %lld I=%d",
(longlong_t)dp->di_size, inumber);
goto bogus;
}
/*
* A more precise test of the type is done later on. Just get
* rid of the blatantly-wrong ones before we do any
* significant work.
*/
if ((dp->di_mode & IFMT) == IFMT) {
pfatal("BAD MODE 0%o I=%d",
dp->di_mode & IFMT, inumber);
if (reply("BAD MODE: MAKE IT A FILE") == 1) {
statemap[inumber] = FSTATE;
dp = ginode(inumber);
dp->di_mode = IFREG | 0600;
inodirty();
truncino(inumber, sblock.fs_fsize, TI_NOPARENT);
dp = getnextrefresh();
} else {
iscorrupt = 1;
}
}
ndb = howmany(dp->di_size, (u_offset_t)sblock.fs_bsize);
if (ndb < 0) {
/* extra space to distinguish from previous pfatal() */
pfatal("NEGATIVE SIZE %lld I=%d",
(longlong_t)dp->di_size, inumber);
goto bogus;
}
if ((dp->di_mode & IFMT) == IFBLK ||
(dp->di_mode & IFMT) == IFCHR) {
if (dp->di_size != 0) {
pfatal("SPECIAL FILE WITH NON-ZERO LENGTH %lld I=%d",
(longlong_t)dp->di_size, inumber);
goto bogus;
}
for (j = 0; j < NDADDR; j++) {
/*
* It's a device, so all the block pointers
* should be zero except for di_ordev.
* di_ordev is overlayed on the block array,
* but where varies between big and little
* endian, so make sure that the only non-zero
* element is the correct one. There can be
* a device whose ordev is zero, so we can't
* check for the reverse.
*/
if (dp->di_db[j] != 0 &&
&dp->di_db[j] != &dp->di_ordev) {
if (debug) {
(void) printf(
"spec file di_db[%d] has %d\n",
j, dp->di_db[j]);
}
pfatal(
"SPECIAL FILE WITH NON-ZERO FRAGMENT LIST I=%d",
inumber);
goto bogus;
}
}
for (j = 0; j < NIADDR; j++) {
if (dp->di_ib[j] != 0) {
if (debug)
(void) printf(
"special has %d at ib[%d]\n",
dp->di_ib[j], j);
pfatal(
"SPECIAL FILE WITH NON-ZERO FRAGMENT LIST I=%d",
inumber);
goto bogus;
}
}
} else {
/*
* This assignment is mostly here to appease lint, but
* doesn't hurt.
*/
err = "Internal error: unexpected variant of having "
"blocks past end of file I=%d";
clear = 0;
/*
* If it's not a device, it has to follow the
* rules for files. In particular, no blocks after
* the last one that di_size says is in use.
*/
for (j = ndb; j < NDADDR; j++) {
if (dp->di_db[j] != 0) {
if (debug) {
(void) printf("bad file direct "
"addr[%d]: block 0x%x "
"format: 0%o\n",
j, dp->di_db[j],
dp->di_mode & IFMT);
}
err = "FILE WITH FRAGMENTS PAST END I=%d";
clear = 1;
break;
}
}
/*
* Find last indirect pointer that should be in use,
* and make sure any after it are clear.
*/
if (!clear) {
for (j = 0, ndb -= NDADDR; ndb > 0; j++) {
ndb /= NINDIR(&sblock);
}
for (; j < NIADDR; j++) {
if (dp->di_ib[j] != 0) {
if (debug) {
(void) printf("bad file "
"indirect addr: block %d\n",
dp->di_ib[j]);
}
err =
"FILE WITH FRAGMENTS PAST END I=%d";
clear = 2;
break;
}
}
}
if (clear) {
/*
* The discarded blocks will be garbage-
* collected in pass5. If we're told not to
* discard them, it's just lost blocks, which
* isn't worth setting iscorrupt for.
*/
pwarn(err, inumber);
if (preen || reply("DISCARD EXCESS FRAGMENTS") == 1) {
dp = ginode(inumber);
if (clear == 1) {
for (; j < NDADDR; j++)
dp->di_db[j] = 0;
j = 0;
}
for (; j < NIADDR; j++)
dp->di_ib[j] = 0;
inodirty();
dp = getnextrefresh();
if (preen)
(void) printf(" (TRUNCATED)");
}
}
}
if (ftypeok(dp) == 0) {
pfatal("UNKNOWN FILE TYPE 0%o I=%d", dp->di_mode, inumber);
goto bogus;
}
n_files++;
TRACK_LNCNTP(inumber, lncntp[inumber] = dp->di_nlink);
/*
* We can't do anything about it right now, so note that its
* processing is being delayed. Otherwise, we'd be changing
* the block allocations out from under ourselves, which causes
* no end of confusion.
*/
flags = statemap[inumber] & INDELAYD;
/*
* if errorlocked or logging, then open deleted files will
* manifest as di_nlink <= 0 and di_mode != 0
* so skip them; they're ok.
* Also skip anything already marked to be cleared.
*/
if (dp->di_nlink <= 0 &&
!((errorlocked || islog) && dp->di_mode == 0) &&
!(flags & INCLEAR)) {
flags |= INZLINK;
if (debug)
(void) printf(
"marking i=%d INZLINK; nlink %d, mode 0%o, islog %d\n",
inumber, dp->di_nlink, dp->di_mode, islog);
}
switch (dp->di_mode & IFMT) {
case IFDIR:
case IFATTRDIR:
if (dp->di_size == 0) {
/*
* INCLEAR means it will be ignored by passes 2 & 3.
*/
if ((dp->di_mode & IFMT) == IFDIR)
(void) printf("ZERO-LENGTH DIR I=%d\n",
inumber);
else
(void) printf("ZERO-LENGTH ATTRDIR I=%d\n",
inumber);
add_orphan_dir(inumber);
flags |= INCLEAR;
flags &= ~INZLINK; /* It will be cleared anyway */
}
statemap[inumber] = DSTATE | flags;
cacheino(dp, inumber);
countdirs++;
break;
case IFSHAD:
if (dp->di_size == 0) {
(void) printf("ZERO-LENGTH SHADOW I=%d\n", inumber);
flags |= INCLEAR;
flags &= ~INZLINK; /* It will be cleared anyway */
}
statemap[inumber] = SSTATE | flags;
cacheacl(dp, inumber);
break;
default:
statemap[inumber] = FSTATE | flags;
}
badblk = 0;
dupblk = 0;
idesc->id_number = inumber;
idesc->id_fix = DONTKNOW;
if (dp->di_size > (u_offset_t)MAXOFF_T) {
largefile_count++;
}
(void) ckinode(dp, idesc, CKI_TRAVERSE);
if (isdir && (idesc->id_firsthole >= 0))
check_dirholes(inumber, idesc);
if (dp->di_blocks != idesc->id_entryno) {
/*
* The kernel releases any blocks it finds in the lists,
* ignoring the block count itself. So, a bad count is
* not grounds for setting iscorrupt.
*/
pwarn("INCORRECT DISK BLOCK COUNT I=%u (%d should be %d)",
inumber, (uint32_t)dp->di_blocks, idesc->id_entryno);
if (!preen && (reply("CORRECT") == 0))
return;
dp = ginode(inumber);
dp->di_blocks = idesc->id_entryno;
iip = getinoinfo(inumber);
if (iip != NULL)
iip->i_isize = dp->di_size;
inodirty();
if (preen)
(void) printf(" (CORRECTED)\n");
}
if (isdir && (dp->di_blocks == 0)) {
/*
* INCLEAR will cause passes 2 and 3 to skip it.
*/
(void) printf("DIR WITH ZERO BLOCKS I=%d\n", inumber);
statemap[inumber] = DCLEAR;
add_orphan_dir(inumber);
}
/*
* Check that the ACL is on a valid file type
*/
shadow = dp->di_shadow;
if (shadow != 0) {
if (acltypeok(dp) == 0) {
clear_attr_acl(inumber, -1,
"NON-ZERO ACL REFERENCE, I=%d\n");
} else if ((shadow <= UFSROOTINO) ||
(shadow > maxinumber)) {
clear_attr_acl(inumber, -1,
"BAD ACL REFERENCE I=%d\n");
} else {
registershadowclient(shadow,
inumber, &shadowclientinfo);
}
}
attrinode = dp->di_oeftflag;
if (attrinode != 0) {
if ((attrinode <= UFSROOTINO) ||
(attrinode > maxinumber)) {
clear_attr_acl(attrinode, inumber,
"BAD ATTRIBUTE REFERENCE TO I=%d FROM I=%d\n");
} else {
dp = ginode(attrinode);
if ((dp->di_mode & IFMT) != IFATTRDIR) {
clear_attr_acl(attrinode, inumber,
"BAD ATTRIBUTE DIR REF TO I=%d FROM I=%d\n");
} else if (dp->di_size == 0) {
clear_attr_acl(attrinode, inumber,
"REFERENCE TO ZERO-LENGTH ATTRIBUTE DIR I=%d from I=%d\n");
} else {
registershadowclient(attrinode, inumber,
&attrclientinfo);
}
}
}
return;
/*
* If we got here, we've not had the chance to see if a
* directory has holes, but we know the directory's bad,
* so it's safe to always return false (no holes found).
*
* Also, a pfatal() is always done before jumping here, so
* we know we're not in preen mode.
*/
bogus:
if (isdir) {
/*
* INCLEAR makes passes 2 & 3 skip it.
*/
statemap[inumber] = DCLEAR;
add_orphan_dir(inumber);
cacheino(dp, inumber);
} else {
statemap[inumber] = FCLEAR;
}
if (reply("CLEAR") == 1) {
(void) tdelete((void *)inumber, &limbo_dirs, ino_t_cmp);
freeino(inumber, TI_PARENT);
inodirty();
} else {
iscorrupt = 1;
}
}
/*
* Do fixup for bad acl/attr references. If PARENT is -1, then
* we assume we're working on a shadow, otherwise an extended attribute.
* FMT must be a printf format string, with one %d directive for
* the inode number.
*/
static void
clear_attr_acl(fsck_ino_t inumber, fsck_ino_t parent, char *fmt)
{
fsck_ino_t victim = inumber;
struct dinode *dp;
if (parent != -1)
victim = parent;
if (fmt != NULL) {
if (parent == -1)
pwarn(fmt, (int)inumber);
else
pwarn(fmt, (int)inumber, (int)parent);
}
if (debug)
(void) printf("parent file/dir I=%d\nvictim I=%d",
(int)parent, (int)victim);
if (!preen && (reply("REMOVE REFERENCE") == 0)) {
iscorrupt = 1;
return;
}
dp = ginode(victim);
if (parent == -1) {
/*
* The file had a bad shadow/acl, so lock it down
* until someone can protect it the way they need it
* to be (i.e., be conservatively paranoid).
*/
dp->di_shadow = 0;
dp->di_mode &= IFMT;
} else {
dp->di_oeftflag = 0;
}
inodirty();
if (preen)
(void) printf(" (CORRECTED)\n");
}
/*
* Check if we have holes in the directory's indirect
* blocks. If there are, get rid of everything after
* the first hole.
*/
static void
check_dirholes(fsck_ino_t inumber, struct inodesc *idesc)
{
char pathbuf[MAXPATHLEN + 1];
getpathname(pathbuf, idesc->id_number, idesc->id_number);
pfatal("I=%d DIRECTORY %s: CONTAINS EMPTY BLOCKS",
idesc->id_number, pathbuf);
if (reply("TRUNCATE AT FIRST EMPTY BLOCK") == 1) {
/*
* We found a hole, so get rid of it.
*/
collapse_dirhole(inumber, idesc);
if (preen)
(void) printf(" (TRUNCATED)\n");
} else {
iscorrupt = 1;
}
}
/*
* Truncate a directory to its first hole. If there are non-holes
* in the direct blocks after the problem block, move them down so
* that there's somewhat less lossage. Doing this for indirect blocks
* is left as an exercise for the reader.
*/
static void
collapse_dirhole(fsck_ino_t inumber, struct inodesc *idesc)
{
offset_t new_size;
int blocks;
if (idesc->id_firsthole < 0) {
return;
}
/*
* Since truncino() adjusts the size, we don't need to do that here,
* but we have to tell it what final size we want.
*
* We need to count from block zero up through the last block
* before the hole. If the hole is in the indirect blocks, chop at
* the start of the nearest level of indirection. Orphans will
* get reconnected, so we're not actually losing anything by doing
* it this way, and we're simplifying truncation significantly.
*/
new_size = idesc->id_firsthole * (offset_t)sblock.fs_bsize;
blocks = howmany(new_size, sblock.fs_bsize);
if (blocks > NDADDR) {
if (blocks < (NDADDR + NINDIR(&sblock)))
blocks = NDADDR;
else if (blocks < (NDADDR + NINDIR(&sblock) +
(NINDIR(&sblock) * NINDIR(&sblock))))
blocks = NDADDR + NINDIR(&sblock);
else
blocks = NDADDR + NINDIR(&sblock) +
(NINDIR(&sblock) * NINDIR(&sblock));
new_size = blocks * sblock.fs_bsize;
if (debug)
(void) printf("to %lld (blocks %d)\n",
(longlong_t)new_size, blocks);
}
truncino(inumber, new_size, TI_NOPARENT);
/*
* Technically, there are still the original number of fragments
* associated with the object. However, that number is not used
* to control anything, so we can do the in-memory truncation of
* it without bad things happening.
*/
idesc->id_entryno = btodb(new_size);
}
int
pass1check(struct inodesc *idesc)
{
int res = KEEPON;
int anyout;
int nfrags;
daddr32_t lbn;
daddr32_t fragno = idesc->id_blkno;
struct dinode *dp;
/*
* If this is a fallocate'd file, block numbers may be stored
* as negative. In that case negate the negative numbers.
*/
dp = ginode(idesc->id_number);
if (dp->di_cflags & IFALLOCATE && fragno < 0)
fragno = -fragno;
if ((anyout = chkrange(fragno, idesc->id_numfrags)) != 0) {
/*
* Note that blkerror() exits when preening.
*/
blkerror(idesc->id_number, "OUT OF RANGE",
fragno, idesc->id_lbn * sblock.fs_frag);
dp = ginode(idesc->id_number);
if ((((dp->di_mode & IFMT) == IFDIR) ||
((dp->di_mode & IFMT) == IFATTRDIR)) &&
(idesc->id_firsthole < 0)) {
idesc->id_firsthole = idesc->id_lbn;
}
if (++badblk >= MAXBAD) {
pwarn("EXCESSIVE BAD FRAGMENTS I=%u",
idesc->id_number);
if (reply("CONTINUE") == 0)
errexit("Program terminated.");
/*
* See discussion below as to why we don't
* want to short-circuit the processing of
* this inode. However, we know that this
* particular block is bad, so we don't need
* to go through the dup check loop.
*/
return (SKIP | STOP);
}
}
/*
* For each fragment, verify that it is a legal one (either
* by having already found the entire run to be legal, or by
* individual inspection), and if it is legal, see if we've
* seen it before or not. If we haven't, note that we've seen
* it and continue on. If we have (our in-core bitmap shows
* it as already being busy), then this must be a duplicate
* allocation. Whine and moan accordingly.
*
* Note that for full-block allocations, this will produce
* a complaint for each fragment making up the block (i.e.,
* fs_frags' worth). Among other things, this could be
* considered artificially inflating the dup-block count.
* However, since it is possible that one file has a full
* fs block allocated, but another is only claiming a frag
* or two out of the middle, we'll just live it.
*/
for (nfrags = 0; nfrags < idesc->id_numfrags; fragno++, nfrags++) {
if (anyout && chkrange(fragno, 1)) {
/* bad fragment number */
res = SKIP;
} else if (!testbmap(fragno)) {
/* no other claims seen as yet */
note_used(fragno);
} else {
/*
* We have a duplicate claim for the same fragment.
*
* blkerror() exits when preening.
*
* We want to report all the dups up until
* hitting MAXDUP. Fortunately, blkerror()'s
* side-effects on statemap[] are idempotent,
* so the ``extra'' calls are harmless.
*/
lbn = idesc->id_lbn * sblock.fs_frag + nfrags;
if (dupblk < MAXDUP)
blkerror(idesc->id_number, "DUP", fragno, lbn);
/*
* Use ==, so we only complain once, no matter
* how far over the limit we end up going.
*/
if (++dupblk == MAXDUP) {
pwarn("EXCESSIVE DUPLICATE FRAGMENTS I=%u",
idesc->id_number);
if (reply("CONTINUE") == 0)
errexit("Program terminated.");
/*
* If we stop the traversal here, then
* there may be more dups in the
* inode's block list that don't get
* flagged. Later, if we're told to
* clear one of the files claiming
* these blocks, but not the other, we
* will release blocks that are
* actually still in use. An additional
* fsck run would be necessary to undo
* the damage. So, instead of the
* traditional return (STOP) when told
* to continue, we really do just continue.
*/
}
(void) find_dup_ref(fragno, idesc->id_number, lbn,
DB_CREATE | DB_INCR);
}
/*
* id_entryno counts the number of disk blocks found.
*/
idesc->id_entryno += btodb(sblock.fs_fsize);
}
return (res);
}
static void
note_used(daddr32_t frag)
{
n_blks++;
setbmap(frag);
}
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that: (1) source distributions retain this entire copyright
* notice and comment, and (2) distributions including binaries display
* the following acknowledgement: ``This product includes software
* developed by the University of California, Berkeley and its contributors''
* in the documentation or other materials provided with the distribution
* and in all advertising materials mentioning features or use of this
* software. Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/mntent.h>
#include <sys/fs/ufs_fs.h>
#include <sys/vnode.h>
#include <sys/fs/ufs_inode.h>
#include "fsck.h"
static int pass1bcheck(struct inodesc *);
void
pass1b(void)
{
struct dinode *dp;
struct inodesc idesc;
fsck_ino_t inumber;
/*
* We can get STOP failures from ckinode() that
* are completely independent of our dup checks.
* If that were not the case, then we could track
* when we've seen all of the dups and short-
* circuit our search. As it is, we need to
* keep going, so there's no point in looking
* at what ckinode() returns to us.
*/
for (inumber = UFSROOTINO; inumber < maxino; inumber++) {
init_inodesc(&idesc);
idesc.id_type = ADDR;
idesc.id_func = pass1bcheck;
idesc.id_number = inumber;
idesc.id_fix = DONTKNOW;
dp = ginode(inumber);
if (statemap[inumber] != USTATE)
(void) ckinode(dp, &idesc, CKI_TRAVERSE);
}
}
static int
pass1bcheck(struct inodesc *idesc)
{
int res = KEEPON;
int nfrags;
daddr32_t lbn;
daddr32_t blkno = idesc->id_blkno;
for (nfrags = 0; nfrags < idesc->id_numfrags; blkno++, nfrags++) {
if (chkrange(blkno, 1)) {
res = SKIP;
} else {
/*
* Note that we only report additional dup claimants
* in this pass, as the first claimant found was
* listed during pass 1.
*/
lbn = idesc->id_lbn * sblock.fs_frag + nfrags;
if (find_dup_ref(blkno, idesc->id_number, lbn, DB_INCR))
blkerror(idesc->id_number, "DUP", blkno, lbn);
}
}
return (res);
}
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that: (1) source distributions retain this entire copyright
* notice and comment, and (2) distributions including binaries display
* the following acknowledgement: ``This product includes software
* developed by the University of California, Berkeley and its contributors''
* in the documentation or other materials provided with the distribution
* and in all advertising materials mentioning features or use of this
* software. Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/mntent.h>
#include <sys/fs/ufs_fs.h>
#include <sys/vnode.h>
#include <sys/fs/ufs_inode.h>
#define _KERNEL
#include <sys/fs/ufs_fsdir.h>
#undef _KERNEL
#include <string.h>
#include "fsck.h"
#define MINDIRSIZE (sizeof (struct dirtemplate))
static int blksort(const void *, const void *);
static int pass2check(struct inodesc *);
void
pass2(void)
{
struct dinode *dp, *dp2, *dpattr;
struct inoinfo **inpp, *inp;
struct inoinfo **inpend;
struct inodesc curino;
struct inodesc ldesc;
struct dinode dino;
char pathbuf[MAXPATHLEN + 1];
int found;
int dirtype;
caddr_t errmsg;
struct shadowclientinfo *sci;
switch (statemap[UFSROOTINO] & ~INDELAYD) {
case USTATE:
pfatal("ROOT INODE UNALLOCATED");
if (reply("ALLOCATE") == 0) {
errexit("Program terminated.");
}
if (allocdir(UFSROOTINO, UFSROOTINO, 0755, 0) != UFSROOTINO)
errexit("CANNOT ALLOCATE ROOT INODE\n");
break;
case DCLEAR:
pfatal("DUPS/BAD IN ROOT INODE");
if (reply("REALLOCATE") == 1) {
freeino(UFSROOTINO, TI_NOPARENT);
if (allocdir(UFSROOTINO, UFSROOTINO,
0755, 0) != UFSROOTINO)
errexit("CANNOT ALLOCATE ROOT INODE\n");
break;
}
if (reply("CONTINUE") == 0) {
errexit("Program terminated.");
}
break;
case FSTATE:
case FCLEAR:
case FZLINK:
case SSTATE:
case SCLEAR:
pfatal("ROOT INODE NOT DIRECTORY");
if (reply("REALLOCATE") == 1) {
freeino(UFSROOTINO, TI_NOPARENT);
if (allocdir(UFSROOTINO, UFSROOTINO, 0755, 0) !=
UFSROOTINO)
errexit("CANNOT ALLOCATE ROOT INODE\n");
break;
}
if (reply("FIX") == 0) {
ckfini();
errexit("Program terminated.");
}
dp = ginode(UFSROOTINO);
dp->di_mode &= ~IFMT;
dp->di_mode |= IFDIR;
inodirty();
break;
case DSTATE:
case DZLINK:
break;
default:
errexit("BAD STATE 0x%x FOR ROOT INODE\n",
statemap[UFSROOTINO]);
}
statemap[UFSROOTINO] = DFOUND;
/*
* Technically, we do know who the parent is. However,
* if this is set, then we'll get confused during the
* second-dir-entry-is-dotdot test for the root inode.
*/
inp = getinoinfo(UFSROOTINO);
if (inp != NULL && inp->i_dotdot != 0)
inp->i_dotdot = 0;
/*
* Sort the directory list into disk block order. There's no
* requirement to do this, but it may help improve our i/o times
* somewhat.
*/
qsort((void *)inpsort, (size_t)inplast, sizeof (*inpsort), blksort);
/*
* Check the integrity of each directory. In general, we treat
* attribute directories just like normal ones. Only the handling
* of .. is really different.
*/
(void) memset(&dino, 0, sizeof (struct dinode));
dino.di_mode = IFDIR;
inpend = &inpsort[inplast];
for (inpp = inpsort; inpp < inpend; inpp++) {
inp = *inpp;
if (inp->i_isize == 0)
continue;
/* != DSTATE also covers case of == USTATE */
if (((statemap[inp->i_number] & STMASK) != DSTATE) ||
((statemap[inp->i_number] & INCLEAR) == INCLEAR))
continue;
if (inp->i_isize < (offset_t)MINDIRSIZE) {
direrror(inp->i_number, "DIRECTORY TOO SHORT");
inp->i_isize = (offset_t)roundup(MINDIRSIZE, DIRBLKSIZ);
if (reply("FIX") == 1) {
dp = ginode(inp->i_number);
dp->di_size = (u_offset_t)inp->i_isize;
inodirty();
} else {
iscorrupt = 1;
}
}
if ((inp->i_isize & (offset_t)(DIRBLKSIZ - 1)) != 0) {
getpathname(pathbuf, inp->i_number, inp->i_number);
pwarn("DIRECTORY %s: LENGTH %lld NOT MULTIPLE OF %d",
pathbuf, (longlong_t)inp->i_isize, DIRBLKSIZ);
inp->i_isize = roundup(inp->i_isize,
(offset_t)DIRBLKSIZ);
if (preen || reply("ADJUST") == 1) {
dp = ginode(inp->i_number);
dp->di_size =
(u_offset_t)roundup(inp->i_isize,
(offset_t)DIRBLKSIZ);
inodirty();
if (preen)
(void) printf(" (ADJUSTED)\n");
} else {
iscorrupt = 1;
}
}
dp = ginode(inp->i_number);
if ((dp->di_mode & IFMT) == IFATTRDIR &&
(dp->di_cflags & IXATTR) == 0) {
pwarn("ATTRIBUTE DIRECTORY I=%d MISSING IXATTR FLAG",
inp->i_number);
if (preen || reply("CORRECT") == 1) {
dp->di_cflags |= IXATTR;
inodirty();
if (preen)
(void) printf(" (CORRECTED)\n");
}
}
dp = &dino;
dp->di_size = (u_offset_t)inp->i_isize;
(void) memmove((void *)&dp->di_db[0], (void *)&inp->i_blks[0],
inp->i_blkssize);
init_inodesc(&curino);
curino.id_type = DATA;
curino.id_func = pass2check;
curino.id_number = inp->i_number;
curino.id_parent = inp->i_parent;
curino.id_fix = DONTKNOW;
(void) ckinode(dp, &curino, CKI_TRAVERSE);
/*
* Make sure we mark attrdirs as DFOUND, since they won't
* be located during normal scan of standard directories.
*/
if (curino.id_parent == 0) {
dpattr = ginode(inp->i_number);
if ((dpattr->di_mode & IFMT) == IFATTRDIR) {
for (sci = attrclientinfo; sci != NULL;
sci = sci->next) {
if (sci->shadow == inp->i_number) {
curino.id_parent =
sci->clients->client[0];
statemap[inp->i_number] =
DFOUND;
inp->i_parent =
curino.id_parent;
}
}
}
}
}
/*
* Now that the parents of all directories have been found,
* make another pass to verify the value of ..
*/
for (inpp = inpsort; inpp < inpend; inpp++) {
inp = *inpp;
if (inp->i_parent == 0 || inp->i_isize == 0)
continue;
/*
* There are only directories in inpsort[], so only
* directory-related states need to be checked. There
* should never be any flags associated with USTATE.
*/
if ((statemap[inp->i_number] & (STMASK | INCLEAR)) == DCLEAR ||
statemap[inp->i_number] == USTATE) {
continue;
}
if (statemap[inp->i_parent] == DFOUND &&
S_IS_DUNFOUND(statemap[inp->i_number])) {
statemap[inp->i_number] = DFOUND |
(statemap[inp->i_number] & INCLEAR);
}
if (inp->i_dotdot == inp->i_parent ||
inp->i_dotdot == (fsck_ino_t)-1) {
continue;
}
if (inp->i_dotdot == 0) {
inp->i_dotdot = inp->i_parent;
fileerror(inp->i_parent, inp->i_number,
"MISSING '..'");
if (reply("FIX") == 0) {
iscorrupt = 1;
continue;
}
dp = ginode(inp->i_number);
found = 0;
dirtype = (dp->di_mode & IFMT);
/*
* See if this is an attrdir that we located in pass1.
* i.e. it was on an i_oeftflag of some other inode.
* if it isn't found then we have an orphaned attrdir
* that needs to be tossed into lost+found.
*/
if (dirtype == IFATTRDIR) {
for (sci = attrclientinfo;
sci != NULL;
sci = sci->next) {
if (sci->shadow == inp->i_number) {
inp->i_parent =
sci->clients->client[0];
found = 1;
}
}
}
/*
* We've already proven there's no "..", so this
* can't create a duplicate.
*/
if (makeentry(inp->i_number, inp->i_parent, "..")) {
/*
* is it an orphaned attrdir?
*/
if (dirtype == IFATTRDIR && found == 0) {
/*
* Throw it into lost+found
*/
if (linkup(inp->i_number, lfdir,
NULL) == 0) {
pwarn(
"Unable to move attrdir I=%d to lost+found\n",
inp->i_number);
iscorrupt = 1;
}
maybe_convert_attrdir_to_dir(
inp->i_number);
}
if (dirtype == IFDIR) {
LINK_RANGE(errmsg,
lncntp[inp->i_parent], -1);
if (errmsg != NULL) {
LINK_CLEAR(errmsg,
inp->i_parent, IFDIR,
&ldesc);
if (statemap[inp->i_parent] !=
USTATE) {
/*
* iscorrupt is
* already set
*/
continue;
}
}
TRACK_LNCNTP(inp->i_parent,
lncntp[inp->i_parent]--);
}
continue;
}
pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
iscorrupt = 1;
inp->i_dotdot = (fsck_ino_t)-1;
continue;
}
dp2 = ginode(inp->i_parent);
if ((dp2->di_mode & IFMT) == IFATTRDIR) {
continue;
}
fileerror(inp->i_parent, inp->i_number,
"BAD INODE NUMBER FOR '..'");
if (reply("FIX") == 0) {
iscorrupt = 1;
continue;
}
LINK_RANGE(errmsg, lncntp[inp->i_dotdot], 1);
if (errmsg != NULL) {
LINK_CLEAR(errmsg, inp->i_dotdot, IFDIR, &ldesc);
if (statemap[inp->i_dotdot] != USTATE) {
/* iscorrupt is already set */
continue;
}
}
TRACK_LNCNTP(inp->i_dotdot, lncntp[inp->i_dotdot]++);
LINK_RANGE(errmsg, lncntp[inp->i_parent], -1);
if (errmsg != NULL) {
LINK_CLEAR(errmsg, inp->i_parent, IFDIR, &ldesc);
if (statemap[inp->i_parent] != USTATE) {
/* iscorrupt is already set */
continue;
}
}
TRACK_LNCNTP(inp->i_parent, lncntp[inp->i_parent]--);
inp->i_dotdot = inp->i_parent;
(void) changeino(inp->i_number, "..", inp->i_parent);
}
/*
* Mark all the directories that can be found from the root.
*/
propagate();
}
/*
* Sanity-check a single directory entry. Which entry is being
* examined is tracked via idesc->id_entryno. There are two
* special ones, 0 (.) and 1 (..). Those have to exist in order
* in the first two locations in the directory, and have the usual
* properties. All other entries have to not be for either of
* the special two, and the inode they reference has to be
* reasonable.
*
* This is only called from dirscan(), which looks for the
* ALTERED flag after each invocation. If it finds it, the
* relevant buffer gets pushed out, so we don't have to worry
* about it here.
*/
#define PASS2B_PROMPT "REMOVE DIRECTORY ENTRY FROM I=%d"
static int
pass2check(struct inodesc *idesc)
{
struct direct *dirp = idesc->id_dirp;
struct inodesc ldesc;
struct inoinfo *inp;
short reclen, entrysize;
int ret = 0;
int act, update_lncntp;
struct dinode *dp, *pdirp, *attrdirp;
caddr_t errmsg;
struct direct proto;
char namebuf[MAXPATHLEN + 1];
char pathbuf[MAXPATHLEN + 1];
int isattr;
int pdirtype;
int breakout = 0;
int dontreconnect;
if (idesc->id_entryno != 0)
goto chk1;
/*
* check for "."
*/
if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
if (dirp->d_ino != idesc->id_number) {
direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
dirp->d_ino = idesc->id_number;
if (reply("FIX") == 1) {
ret |= ALTERED;
} else {
iscorrupt = 1;
}
}
goto chk1;
}
/*
* Build up a new one, and make sure there's room to put
* it where it belongs.
*/
direrror(idesc->id_number, "MISSING '.'");
proto.d_ino = idesc->id_number;
proto.d_namlen = 1;
(void) strcpy(proto.d_name, ".");
entrysize = DIRSIZ(&proto);
if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
dirp->d_name);
iscorrupt = 1;
} else if ((int)dirp->d_reclen < entrysize) {
pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
iscorrupt = 1;
} else if ((int)dirp->d_reclen < 2 * entrysize) {
/*
* No room for another entry after us ("." is the
* smallest entry you can have), so just put all
* of the old entry's space into the new entry.
*
* Because we don't touch id_entryno, we end up going
* through the chk2 tests as well.
*/
proto.d_reclen = dirp->d_reclen;
(void) memmove((void *)dirp, (void *)&proto,
(size_t)entrysize);
if (reply("FIX") == 1) {
ret |= ALTERED;
} else {
iscorrupt = 1;
}
} else {
/*
* There's enough room for an entire additional entry
* after this, so create the "." entry and follow it
* with an empty entry that covers the rest of the
* space.
*
* The increment of id_entryno means we'll skip the
* "." case of chk1, doing the ".." tests instead.
* Since we know that there's not a ".." where it
* should be (because we just created an empty entry
* there), that's the best way of getting it recreated
* as well.
*/
reclen = dirp->d_reclen - entrysize;
proto.d_reclen = entrysize;
(void) memmove((void *)dirp, (void *)&proto,
(size_t)entrysize);
idesc->id_entryno++;
/*
* Make sure the link count is in range before updating
* it. This makes the assumption that the link count
* for this inode included one for ".", even though
* there wasn't a "." entry. Even if that's not true,
* it's a reasonable working hypothesis, and the link
* count verification done in pass4 will fix it for
* us anyway.
*/
LINK_RANGE(errmsg, lncntp[dirp->d_ino], -1);
if (errmsg != NULL) {
LINK_CLEAR(errmsg, dirp->d_ino, IFDIR, &ldesc);
if (statemap[dirp->d_ino] == USTATE) {
/*
* The inode got zapped, so reset the
* directory entry. Extend it to also
* cover the space we were going to make
* into a new entry.
*/
dirp->d_ino = 0;
dirp->d_reclen += reclen;
ret |= ALTERED;
return (ret);
}
}
/*
* Create the new empty entry.
*/
/* LINTED pointer cast alignment (entrysize is valid) */
dirp = (struct direct *)((char *)(dirp) + entrysize);
(void) memset((void *)dirp, 0, (size_t)reclen);
dirp->d_reclen = reclen;
/*
* Did the user want us to create a new "."? This
* query assumes that the direrror(MISSING) was the
* last thing printed, so if the LINK_RANGE() check
* fails, it can't pass through here.
*/
if (reply("FIX") == 1) {
TRACK_LNCNTP(idesc->id_number,
lncntp[idesc->id_number]--);
ret |= ALTERED;
} else {
iscorrupt = 1;
}
}
/*
* XXX The next few lines are needed whether we're processing "."
* or "..". However, there are some extra steps still needed
* for the former, hence the big block of code for
* id_entryno == 0. Alternatively, there could be a label just
* before this comment, and everything through the end of that
* block moved there. In some ways, that might make the
* control flow more logical (factoring out to separate functions
* would be even better).
*/
chk1:
if (idesc->id_entryno > 1)
goto chk2;
inp = getinoinfo(idesc->id_number);
if (inp == NULL) {
/*
* This is a can't-happen, since inodes get cached before
* we get called on them.
*/
errexit("pass2check got NULL from getinoinfo at chk1 I=%d\n",
idesc->id_number);
}
proto.d_ino = inp->i_parent;
proto.d_namlen = 2;
(void) strcpy(proto.d_name, "..");
entrysize = DIRSIZ(&proto);
if (idesc->id_entryno == 0) {
/*
* We may not actually need to split things up, but if
* there's room to do so, we should, as that implies
* that the "." entry is larger than it is supposed
* to be, and therefore there's something wrong, albeit
* possibly harmlessly so.
*/
reclen = DIRSIZ(dirp);
if ((int)dirp->d_reclen < reclen + entrysize) {
/*
* Not enough room for inserting a ".." after
* the "." entry.
*/
goto chk2;
}
/*
* There's enough room for an entire additional entry
* after "."'s, so split it up. There's no reason "."
* should be bigger than the minimum, so shrink it to
* fit, too. Since by the time we're done with this
* part, dirp will be pointing at where ".." should be,
* update id_entryno to show that that's the entry
* we're on.
*/
proto.d_reclen = dirp->d_reclen - reclen;
dirp->d_reclen = reclen;
idesc->id_entryno++;
if (dirp->d_ino > 0 && dirp->d_ino <= maxino) {
/*
* Account for the link to ourselves.
*/
LINK_RANGE(errmsg, lncntp[dirp->d_ino], -1);
if (errmsg != NULL) {
LINK_CLEAR(errmsg, dirp->d_ino, IFDIR, &ldesc);
if (statemap[dirp->d_ino] == USTATE) {
/*
* We were going to split the entry
* up, but the link count overflowed.
* Since we got rid of the inode,
* we need to also zap the directory
* entry, and restoring the original
* state of things is the least-bad
* result.
*/
dirp->d_ino = 0;
dirp->d_reclen += proto.d_reclen;
ret |= ALTERED;
return (ret);
}
}
TRACK_LNCNTP(dirp->d_ino, lncntp[dirp->d_ino]--);
/*
* Make sure the new entry doesn't get interpreted
* as having actual content.
*/
/* LINTED pointer cast alignment (reclen is valid) */
dirp = (struct direct *)((char *)(dirp) + reclen);
(void) memset((void *)dirp, 0, (size_t)proto.d_reclen);
dirp->d_reclen = proto.d_reclen;
} else {
/*
* Everything was fine, up until we realized that
* the indicated inode was impossible. By clearing
* d_ino here, we'll trigger the recreation of it
* down below, using i_parent. Unlike the other
* half of this if(), we're everything so it shows
* that we're still on the "." entry.
*/
fileerror(idesc->id_number, dirp->d_ino,
"I OUT OF RANGE");
dirp->d_ino = 0;
if (reply("FIX") == 1) {
ret |= ALTERED;
} else {
iscorrupt = 1;
}
}
}
/*
* Record this ".." inode, but only if we haven't seen one before.
* If this isn't the first, it'll get cleared below, and so we
* want to remember the entry that'll still be around later.
*/
if (dirp->d_ino != 0 && inp->i_dotdot == 0 &&
strcmp(dirp->d_name, "..") == 0) {
inp->i_dotdot = dirp->d_ino;
goto chk2;
}
if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
dirp->d_name);
iscorrupt = 1;
inp->i_dotdot = (fsck_ino_t)-1;
} else if ((int)dirp->d_reclen < entrysize) {
fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
/* XXX Same consideration as immediately above. */
iscorrupt = 1;
inp->i_dotdot = (fsck_ino_t)-1;
} else if (inp->i_parent != 0) {
/*
* We know the parent, so fix now.
*/
proto.d_ino = inp->i_dotdot = inp->i_parent;
fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
/*
* Lint won't be quiet about d_reclen being set but not
* used. It apparently doesn't understand the implications
* of calling memmove(), and won't believe us that it's ok.
*/
proto.d_reclen = dirp->d_reclen;
(void) memmove((void *)dirp, (void *)&proto,
(size_t)entrysize);
if (reply("FIX") == 1) {
ret |= ALTERED;
} else {
iscorrupt = 1;
}
} else if (inp->i_number == UFSROOTINO) {
/*
* Always know parent of root inode, so fix now.
*/
proto.d_ino = inp->i_dotdot = inp->i_parent = UFSROOTINO;
fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
/*
* Lint won't be quiet about d_reclen being set but not
* used. It apparently doesn't understand the implications
* of calling memmove(), and won't believe us that it's ok.
*/
proto.d_reclen = dirp->d_reclen;
(void) memmove((void *)dirp, (void *)&proto, (size_t)entrysize);
if (reply("FIX") == 1) {
ret |= ALTERED;
} else {
iscorrupt = 1;
}
}
idesc->id_entryno++;
if (dirp->d_ino != 0) {
LINK_RANGE(errmsg, lncntp[dirp->d_ino], -1);
if (errmsg != NULL) {
LINK_CLEAR(errmsg, dirp->d_ino, IFDIR, &ldesc);
if (statemap[dirp->d_ino] == USTATE) {
dirp->d_ino = 0;
ret |= ALTERED;
}
}
TRACK_LNCNTP(dirp->d_ino, lncntp[dirp->d_ino]--);
}
return (ret|KEEPON);
chk2:
if (dirp->d_ino == 0)
return (ret|KEEPON);
if (dirp->d_namlen <= 2 &&
dirp->d_name[0] == '.' &&
idesc->id_entryno >= 2) {
if (dirp->d_namlen == 1) {
direrror(idesc->id_number, "EXTRA '.' ENTRY");
dirp->d_ino = 0;
if (reply("FIX") == 1) {
ret |= ALTERED;
} else {
iscorrupt = 1;
}
return (KEEPON | ret);
}
if (dirp->d_name[1] == '.') {
direrror(idesc->id_number, "EXTRA '..' ENTRY");
dirp->d_ino = 0;
if (reply("FIX") == 1) {
ret |= ALTERED;
} else {
iscorrupt = 1;
}
return (KEEPON | ret);
}
}
/*
* Because of this increment, all tests for skipping . and ..
* below are ``> 2'', not ``> 1'' as would logically be expected.
*/
idesc->id_entryno++;
act = -1;
/*
* The obvious check would be for d_ino < UFSROOTINO. However,
* 1 is a valid inode number. Although it isn't currently used,
* as it was once the bad block list, there's nothing to prevent
* it from acquiring a new purpose in the future. So, don't
* arbitrarily disallow it. We don't test for <= zero, because
* d_ino is unsigned.
*/
update_lncntp = 0;
if (dirp->d_ino > maxino || dirp->d_ino == 0) {
fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
act = (reply(PASS2B_PROMPT, idesc->id_number) == 1);
} else {
again:
update_lncntp = 0;
switch (statemap[dirp->d_ino] & ~(INDELAYD)) {
case USTATE:
if (idesc->id_entryno <= 2)
break;
fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
act = (reply(PASS2B_PROMPT, idesc->id_number) == 1);
break;
case DCLEAR:
case FCLEAR:
case SCLEAR:
if (idesc->id_entryno <= 2)
break;
dp = ginode(dirp->d_ino);
if (statemap[dirp->d_ino] == DCLEAR) {
errmsg = ((dp->di_mode & IFMT) == IFATTRDIR) ?
"REFERENCE TO ZERO LENGTH ATTRIBUTE DIRECTORY" :
"REFERENCE TO ZERO LENGTH DIRECTORY";
inp = getinoinfo(dirp->d_ino);
if (inp == NULL) {
/*
* The inode doesn't exist, as all
* should be cached by now. This
* gets caught by the range check
* above, and so it is a can't-happen
* at this point.
*/
errexit("pass2check found a zero-len "
"reference to bad I=%d\n",
dirp->d_ino);
}
if (inp->i_parent != 0) {
(void) printf(
"Multiple links to I=%d, link counts wrong, rerun fsck\n",
inp->i_number);
iscorrupt = 1;
}
} else if (statemap[dirp->d_ino] == SCLEAR) {
/*
* In theory, this is a can't-happen,
* because shadows don't appear in directory
* entries. However, an inode might've
* been reused without a stale directory
* entry having been cleared, so check
* for it just in case. We'll check for
* the no-dir-entry shadows in pass3b().
*/
errmsg = "ZERO LENGTH SHADOW";
} else {
errmsg = "DUP/BAD";
}
fileerror(idesc->id_number, dirp->d_ino, errmsg);
if ((act = reply(PASS2B_PROMPT, idesc->id_number)) == 1)
break;
/*
* Not doing anything about it, so just try
* again as whatever the base type was.
*
* fileerror() invalidated dp. Lint thinks this
* is unnecessary, but we know better.
*/
dp = ginode(dirp->d_ino);
statemap[dirp->d_ino] &= STMASK;
TRACK_LNCNTP(dirp->d_ino, lncntp[dirp->d_ino] = 0);
goto again;
case DSTATE:
case DZLINK:
if (statemap[idesc->id_number] == DFOUND) {
statemap[dirp->d_ino] = DFOUND;
}
/* FALLTHROUGH */
case DFOUND:
/*
* This is encouraging the best-practice of not
* hard-linking directories. It's legal (see POSIX),
* but not a good idea. So, don't consider it an
* instance of corruption, but offer to nuke it.
*/
inp = getinoinfo(dirp->d_ino);
if (inp == NULL) {
/*
* Same can't-happen argument as in the
* zero-len case above.
*/
errexit("pass2check found bad reference to "
"hard-linked directory I=%d\n",
dirp->d_ino);
}
dp = ginode(idesc->id_number);
if (inp->i_parent != 0 && idesc->id_entryno > 2 &&
((dp->di_mode & IFMT) != IFATTRDIR)) {
/*
* XXX For nested dirs, this can report
* the same name for both paths.
*/
getpathname(pathbuf, idesc->id_number,
dirp->d_ino);
getpathname(namebuf, dirp->d_ino, dirp->d_ino);
pwarn(
"%s IS AN EXTRANEOUS HARD LINK TO DIRECTORY %s\n",
pathbuf, namebuf);
if (preen) {
(void) printf(" (IGNORED)\n");
} else {
act = reply(PASS2B_PROMPT,
idesc->id_number);
if (act == 1) {
update_lncntp = 1;
broke_dir_link = 1;
break;
}
}
}
if ((idesc->id_entryno > 2) &&
(inp->i_extattr != idesc->id_number)) {
inp->i_parent = idesc->id_number;
}
/* FALLTHROUGH */
case FSTATE:
case FZLINK:
/*
* There's nothing to do for normal file-like
* things. Extended attributes come through
* here as well, though, and for them, .. may point
* to a file. In this situation we don't want
* to decrement link count as it was already
* decremented when the entry was seen in the
* directory it actually lives in.
*/
pdirp = ginode(idesc->id_number);
pdirtype = (pdirp->di_mode & IFMT);
dp = ginode(dirp->d_ino);
isattr = (dp->di_cflags & IXATTR);
act = -1;
if (pdirtype == IFATTRDIR &&
(strcmp(dirp->d_name, "..") == 0)) {
dontreconnect = 0;
if (dp->di_oeftflag != 0) {
attrdirp = ginode(dp->di_oeftflag);
/*
* is it really an attrdir?
* if so, then don't do anything.
*/
if ((attrdirp->di_mode & IFMT) ==
IFATTRDIR)
dontreconnect = 1;
dp = ginode(dirp->d_ino);
}
/*
* Rare corner case - the attrdir's ..
* points to the attrdir itself.
*/
if (dirp->d_ino == idesc->id_number) {
dontreconnect = 1;
TRACK_LNCNTP(idesc->id_number,
lncntp[idesc->id_number]--);
}
/*
* Lets see if we have an orphaned attrdir
* that thinks it belongs to this file.
* Only re-connect it if the current
* attrdir is 0 or not an attrdir.
*/
if ((dp->di_oeftflag != idesc->id_number) &&
(dontreconnect == 0)) {
fileerror(idesc->id_number,
dirp->d_ino,
"Attribute directory I=%d not "
"attached to file I=%d\n",
idesc->id_number, dirp->d_ino);
if ((act = reply("FIX")) == 1) {
dp = ginode(dirp->d_ino);
if (debug)
(void) printf(
"debug: changing i=%d's oeft from %d ",
dirp->d_ino,
dp->di_oeftflag);
dp->di_oeftflag =
idesc->id_number;
if (debug)
(void) printf("to %d\n",
dp->di_oeftflag);
inodirty();
registershadowclient(
idesc->id_number,
dirp->d_ino,
&attrclientinfo);
}
dp = ginode(dirp->d_ino);
}
/*
* This can only be true if we've modified
* an inode/xattr connection, and we
* don't keep track of those in the link
* counts. So, skipping the checks just
* after this is not a problem.
*/
if (act > 0)
return (KEEPON | ALTERED);
/*
* Don't screw up link counts for directories.
* If we aren't careful we can perform
* an extra decrement, since the .. of
* an attrdir could be either a file or a
* directory. If it's a file then its link
* should be correct after it is seen when the
* directory it lives in scanned.
*/
if ((pdirtype == IFATTRDIR) &&
((dp->di_mode & IFMT) == IFDIR))
breakout = 1;
if ((dp->di_mode & IFMT) != IFDIR)
breakout = 1;
} else if ((pdirtype != IFATTRDIR) ||
(strcmp(dirp->d_name, ".") != 0)) {
if ((pdirtype == IFDIR) && isattr) {
fileerror(idesc->id_number,
dirp->d_ino,
"File should NOT be marked as "
"extended attribute\n");
if ((act = reply("FIX")) == 1) {
dp = ginode(dirp->d_ino);
if (debug)
(void) printf(
"changing i=%d's cflags from 0x%x to ",
dirp->d_ino,
dp->di_cflags);
dp->di_cflags &= ~IXATTR;
if (debug)
(void) printf("0x%x\n",
dp->di_cflags);
inodirty();
if ((dp->di_mode & IFMT) ==
IFATTRDIR) {
dp->di_mode &=
~IFATTRDIR;
dp->di_mode |= IFDIR;
inodirty();
pdirp = ginode(
idesc->id_number);
if (pdirp->di_oeftflag
!= 0) {
pdirp->di_oeftflag = 0;
inodirty();
}
}
}
} else {
if (pdirtype == IFATTRDIR &&
(isattr == 0)) {
fileerror(idesc->id_number,
dirp->d_ino,
"File should BE marked as "
"extended attribute\n");
if ((act = reply("FIX")) == 1) {
dp = ginode(
dirp->d_ino);
dp->di_cflags |= IXATTR;
/*
* Make sure it's a file
* while we're at it.
*/
dp->di_mode &= ~IFMT;
dp->di_mode |= IFREG;
inodirty();
}
}
}
}
if (breakout == 0 || dontreconnect == 0) {
TRACK_LNCNTP(dirp->d_ino,
lncntp[dirp->d_ino]--);
if (act > 0)
return (KEEPON | ALTERED);
}
break;
case SSTATE:
errmsg = "ACL IN DIRECTORY";
fileerror(idesc->id_number, dirp->d_ino, errmsg);
act = (reply(PASS2B_PROMPT, idesc->id_number) == 1);
break;
default:
errexit("BAD STATE 0x%x FOR INODE I=%d",
statemap[dirp->d_ino], dirp->d_ino);
}
}
if (act == 0) {
iscorrupt = 1;
}
if (act <= 0)
return (ret|KEEPON);
if (update_lncntp) {
LINK_RANGE(errmsg, lncntp[idesc->id_number], 1);
if (errmsg != NULL) {
LINK_CLEAR(errmsg, idesc->id_number, IFDIR, &ldesc);
if (statemap[idesc->id_number] == USTATE) {
idesc->id_number = 0;
ret |= ALTERED;
}
}
TRACK_LNCNTP(idesc->id_number, lncntp[idesc->id_number]++);
}
dirp->d_ino = 0;
return (ret|KEEPON|ALTERED);
}
#undef PASS2B_PROMPT
/*
* Routine to sort disk blocks.
*/
static int
blksort(const void *arg1, const void *arg2)
{
const struct inoinfo **inpp1 = (const struct inoinfo **)arg1;
const struct inoinfo **inpp2 = (const struct inoinfo **)arg2;
return ((*inpp1)->i_blks[0] - (*inpp2)->i_blks[0]);
}
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that: (1) source distributions retain this entire copyright
* notice and comment, and (2) distributions including binaries display
* the following acknowledgement: ``This product includes software
* developed by the University of California, Berkeley and its contributors''
* in the documentation or other materials provided with the distribution
* and in all advertising materials mentioning features or use of this
* software. Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/mntent.h>
#include <sys/fs/ufs_fs.h>
#include <sys/vnode.h>
#include <sys/fs/ufs_inode.h>
#define _KERNEL
#include <sys/fs/ufs_fsdir.h>
#undef _KERNEL
#include "fsck.h"
static int pass3acheck(struct inodesc *);
static void setcurino(struct inodesc *, struct dinode *, struct inoinfo *);
void
pass3a(void)
{
caddr_t flow;
struct inoinfo **inpp, *inp;
fsck_ino_t orphan;
int loopcnt;
int state;
struct shadowclientinfo *sci, *sci_victim, *sci_prev, **sci_rootp;
struct inodesc curino;
struct dinode *dp;
struct inodesc idesc;
char namebuf[MAXNAMLEN + 1];
for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--) {
inp = *inpp;
state = statemap[inp->i_number];
if (inp->i_number == UFSROOTINO ||
(inp->i_parent != 0 && !S_IS_DUNFOUND(state)))
continue;
if (state == DCLEAR || state == USTATE || (state & INORPHAN))
continue;
/*
* If we are running with logging and we come
* across unreferenced directories, we just leave
* them in DSTATE which will cause them to be pitched
* in pass 4.
*/
if (preen && !iscorrupt && islog && S_IS_DUNFOUND(state)) {
if (inp->i_dotdot >= UFSROOTINO) {
LINK_RANGE(flow, lncntp[inp->i_dotdot], 1);
if (flow != NULL) {
dp = ginode(inp->i_dotdot);
LINK_CLEAR(flow, inp->i_dotdot,
dp->di_mode, &idesc);
if (statemap[inp->i_dotdot] == USTATE)
continue;
}
TRACK_LNCNTP(inp->i_dotdot,
lncntp[inp->i_dotdot]++);
}
continue;
}
for (loopcnt = 0; ; loopcnt++) {
orphan = inp->i_number;
/*
* Skip out if we aren't connected to the name
* space, or our parent is connected, or we've
* looked at too many directories. Our parent
* being connected means that orphan is the
* first ancestor of *inpp with questionable
* antecedents.
*/
if (inp->i_parent == 0 ||
!INO_IS_DUNFOUND(inp->i_parent) ||
loopcnt > numdirs)
break;
inp = getinoinfo(inp->i_parent);
/*
* Can't happen, because a non-zero parent's already
* been seen and therefore cached.
*/
if (inp == NULL)
errexit("pass3 could not find cached "
"inode I=%d\n",
inp->i_parent);
}
/*
* Already did this one. Don't bother the user
* with redundant questions.
*/
if (statemap[orphan] & INORPHAN)
continue;
/*
* A link count of 0 with parent and .. inodes of 0
* indicates a partly deleted directory.
* Clear it.
*/
dp = ginode(orphan);
if (dp->di_nlink == 0 && inp->i_dotdot == 0 &&
inp->i_parent == 0) {
/*
* clri() just uses curino.id_number; in other
* words, it won't use the callback that setcurino()
* puts in.
*/
setcurino(&curino, dp, inp);
clri(&curino, "UNREF", CLRI_VERBOSE, CLRI_NOP_OK);
/*
* If we didn't clear it, at least mark it so
* we don't waste time on it again.
*/
if (statemap[orphan] != USTATE) {
statemap[orphan] |= INORPHAN;
}
continue;
}
/*
* We can call linkup() multiple times on the same directory
* inode, if we were told not to reconnect it the first time.
* This is because we find it as a disconnected parent of
* of its children (and mark it found), and then finally get
* to it in the inpsort array. This is better than in the
* past, where we'd call it every time we found it as a
* child's parent. Ideally, we'd suppress even the second
* query, but that confuses pass 4's interpretation of
* the state flags.
*/
if (loopcnt <= countdirs) {
if (linkup(orphan, inp->i_dotdot, NULL)) {
/*
* Bookkeeping for any sort of relinked
* directory.
*/
inp->i_dotdot = lfdir;
inp->i_parent = inp->i_dotdot;
statemap[orphan] &= ~(INORPHAN);
} else {
statemap[orphan] |= INORPHAN;
}
propagate();
continue;
}
/*
* We visited more directories than exist in the
* filesystem. The only way to do that is if there's
* a loop.
*/
pfatal("ORPHANED DIRECTORY LOOP DETECTED I=%d\n", orphan);
/*
* Can never get here with inp->i_parent zero, because
* of the interactions between the for() and the
* if (loopcnt <= countdirs) above.
*/
init_inodesc(&idesc);
idesc.id_type = DATA;
idesc.id_number = inp->i_parent;
idesc.id_parent = orphan;
idesc.id_func = findname;
idesc.id_name = namebuf;
namebuf[0] = '\0';
/*
* Theoretically, this lookup via ckinode can't fail
* (if orphan doesn't exist in i_parent, then i_parent
* would not have been filled in by pass2check()).
* However, if we're interactive, we want to at least
* attempt to continue. The worst case is that it
* gets reconnected as #nnn into lost+found instead of
* to its old parent with its old name.
*/
if ((ckinode(ginode(inp->i_parent),
&idesc, CKI_TRAVERSE) & FOUND) == 0)
pfatal("COULD NOT FIND NAME IN PARENT DIRECTORY");
if (linkup(orphan, inp->i_parent, namebuf)) {
if (cleardirentry(inp->i_parent, orphan) & FOUND) {
LFDIR_LINK_RANGE_NORVAL(flow, lncntp[lfdir], 1,
&idesc);
TRACK_LNCNTP(orphan, lncntp[orphan]++);
}
inp->i_parent = inp->i_dotdot = lfdir;
LFDIR_LINK_RANGE_NORVAL(flow, lncntp[lfdir], -1,
&idesc);
TRACK_LNCNTP(lfdir, lncntp[lfdir]--);
statemap[orphan] = DFOUND;
} else {
/*
* Represents a on-disk leak, not an inconsistency,
* so don't set iscorrupt. Such leaks are harmless
* in the context of discrepancies that the kernel
* will panic over.
*
* We don't care if tsearch() returns non-NULL
* != orphan, since there's no dynamic memory
* to free here.
*/
if (tsearch((void *)orphan, &limbo_dirs,
ino_t_cmp) == NULL)
errexit("out of memory");
statemap[orphan] |= INORPHAN;
continue;
}
propagate();
}
/*
* The essence of the inner loop is to update the inode of
* every shadow or attribute inode's lncntp[] by the number of
* links we've found to them in pass 2 and above. Logically,
* all that is needed is just the one line:
*
* lncntp[sci->shadow] -= sci->totalclients;
*
* However, there's the possibility of wrapping the link count
* (this is especially true for shadows, which are expected to
* be shared amongst many files). This means that we have to
* range-check before changing anything, and if the check
* fails, offer to clear the shadow or attribute. If we do
* clear it, then we have to remove it from the linked list of
* all of the type of inodes that we're going through.
*
* Just to make things a little more complicated, these are
* singly-linked lists, so we have to do all the extra
* bookkeeping that goes along with that as well.
*
* The only connection between the shadowclientinfo and
* attrclientinfo lists is that they use the same underlying
* struct. Both need this scan, so the outer loop is just to
* pick which one we're working on at the moment. There is no
* requirement as to which of these lists is scanned first.
*/
for (loopcnt = 0; loopcnt < 2; loopcnt++) {
if (loopcnt == 0)
sci_rootp = &shadowclientinfo;
else
sci_rootp = &attrclientinfo;
sci = *sci_rootp;
sci_prev = NULL;
while (sci != NULL) {
sci_victim = NULL;
LINK_RANGE(flow, lncntp[sci->shadow],
-(sci->totalClients));
if (flow != NULL) {
/*
* Overflowed the link count.
*/
dp = ginode(sci->shadow);
LINK_CLEAR(flow, sci->shadow, dp->di_mode,
&idesc);
if (statemap[sci->shadow] == USTATE) {
/*
* It's been cleared, fix the
* lists.
*/
if (sci_prev == NULL) {
*sci_rootp = sci->next;
} else {
sci_prev->next = sci->next;
}
sci_victim = sci;
}
}
/*
* If we did not clear the shadow, then we
* need to update the count and advance the
* previous pointer. Otherwise, finish the
* clean up once we're done with the struct.
*/
if (sci_victim == NULL) {
TRACK_LNCNTP(sci->shadow,
lncntp[sci->shadow] -= sci->totalClients);
sci_prev = sci;
}
sci = sci->next;
if (sci_victim != NULL)
deshadow(sci_victim, NULL);
}
}
}
/*
* This is used to verify the cflags of files
* under a directory that used to be an attrdir.
*/
static int
pass3acheck(struct inodesc *idesc)
{
struct direct *dirp = idesc->id_dirp;
int n = 0, ret = 0;
struct dinode *dp, *pdirp;
int isattr;
int dirtype;
int inotype;
if (dirp->d_ino == 0)
return (KEEPON);
idesc->id_entryno++;
if ((strcmp(dirp->d_name, ".") == 0) ||
(strcmp(dirp->d_name, "..") == 0)) {
return (KEEPON);
}
switch (statemap[dirp->d_ino] & ~(INDELAYD)) {
case DSTATE:
case DFOUND:
case FSTATE:
/*
* Accept DSTATE and DFOUND so we can handle normal
* directories as well as xattr directories.
*
* For extended attribute directories .. may point
* to a file. In this situation we don't want
* to decrement link count as it was already
* decremented when the entry was seen and decremented
* in the directory it actually lives in.
*/
dp = ginode(dirp->d_ino);
isattr = (dp->di_cflags & IXATTR);
inotype = (dp->di_mode & IFMT);
pdirp = ginode(idesc->id_number);
dirtype = (pdirp->di_mode & IFMT);
/*
* IXATTR indicates that an object is itself an extended
* attribute. An IFMT of IFATTRDIR means we are looking
* at a directory which contains files which should all
* have IXATTR set. The IFATTRDIR case was handled in
* pass 2b.
*
* Note that the following code actually handles
* anything that's marked as an extended attribute but
* in a regular directory, not just files.
*/
if ((dirtype == IFDIR) && isattr) {
fileerror(idesc->id_number, dirp->d_ino,
"%s I=%d should NOT be marked as extended attribute\n",
(inotype == IFDIR) ? "Directory" : "File",
dirp->d_ino);
dp = ginode(dirp->d_ino);
dp->di_cflags &= ~IXATTR;
if ((n = reply("FIX")) == 1) {
inodirty();
} else {
iscorrupt = 1;
}
if (n != 0)
return (KEEPON | ALTERED);
}
break;
default:
errexit("PASS3: BAD STATE %d FOR INODE I=%d",
statemap[dirp->d_ino], dirp->d_ino);
/* NOTREACHED */
}
if (n == 0)
return (ret|KEEPON);
return (ret|KEEPON|ALTERED);
}
static void
setcurino(struct inodesc *idesc, struct dinode *dp, struct inoinfo *inp)
{
(void) memmove((void *)&dp->di_db[0], (void *)&inp->i_blks[0],
inp->i_blkssize);
init_inodesc(idesc);
idesc->id_number = inp->i_number;
idesc->id_parent = inp->i_parent;
idesc->id_fix = DONTKNOW;
idesc->id_type = DATA;
idesc->id_func = pass3acheck;
}
void
maybe_convert_attrdir_to_dir(fsck_ino_t orphan)
{
struct dinode *dp = ginode(orphan);
struct inoinfo *inp = getinoinfo(orphan);
struct inodesc idesc;
if (dp->di_cflags & IXATTR) {
dp->di_cflags &= ~IXATTR;
inodirty();
}
if ((dp->di_mode & IFMT) == IFATTRDIR) {
dp->di_mode &= ~IFATTRDIR;
dp->di_mode |= IFDIR;
inodirty();
setcurino(&idesc, dp, inp);
idesc.id_fix = FIX;
idesc.id_filesize = dp->di_size;
(void) ckinode(dp, &idesc, CKI_TRAVERSE);
}
}
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that: (1) source distributions retain this entire copyright
* notice and comment, and (2) distributions including binaries display
* the following acknowledgement: ``This product includes software
* developed by the University of California, Berkeley and its contributors''
* in the documentation or other materials provided with the distribution
* and in all advertising materials mentioning features or use of this
* software. Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/mntent.h>
#include <sys/acl.h>
#include <sys/fs/ufs_acl.h>
#include <sys/fs/ufs_fs.h>
#include <sys/vnode.h>
#include <string.h>
#include <sys/fs/ufs_inode.h>
#include "fsck.h"
/*
* We can be run on multiple filesystems (processed serially), so
* these need to be re-initialized each time we start the pass.
*/
static caddr_t aclbuf; /* hold acl's for parsing */
static int64_t aclbufoff; /* offset into aclbuf */
static int64_t maxaclsize; /* how big aclbuf is */
static int aclblksort(const void *, const void *);
static int bufchk(char *, int64_t, fsck_ino_t);
static void clear_shadow_client(struct shadowclientinfo *,
struct shadowclients *, int);
void
pass3b(void)
{
fsck_ino_t inumber;
struct dinode *dp;
struct inoinfo *aclp;
struct inodesc curino;
struct shadowclientinfo *sci;
struct shadowclients *scc;
int64_t acl_size_limit;
int i;
/*
* Sort the acl list into disk block order.
*/
qsort((char *)aclpsort, (int)aclplast, sizeof (*aclpsort), aclblksort);
/*
* Scan all the acl inodes, finding the largest acl file.
*
* The largest legal size is (4 * MAX_ACL_ENTRIES + 8) entries.
* The four are the categories of specific users, specific
* groups, default specific users, and default specific groups.
* The eight are the entries for the owning user/group/other/class
* plus the equivalent defaults.
*
* We double this to allow for a truly worst-case but legal
* situation of every single acl having its own fsd_t wrapper.
* Doubling is a bit pessimistic (sizeof (acl_t) > sizeof (fsd_t)).
*/
acl_size_limit = sizeof (ufs_acl_t) * (4 * MAX_ACL_ENTRIES + 8);
acl_size_limit *= 2;
maxaclsize = 0;
for (inumber = 0; inumber < aclplast; inumber++) {
aclp = aclpsort[inumber];
if ((int64_t)aclp->i_isize > acl_size_limit) {
(void) printf(
"ACL I=%d is excessively large (%lld > %lld)",
inumber,
(longlong_t)aclp->i_isize,
(longlong_t)acl_size_limit);
if (preen) {
(void) printf(" (IGNORING)\n");
} else if (reply("CLEAR") == 1) {
freeino(inumber, TI_PARENT);
} else {
iscorrupt = 1;
(void) printf("IGNORING SHADOW I=%d\n",
inumber);
}
continue;
}
if ((int64_t)aclp->i_isize > maxaclsize)
maxaclsize = (int64_t)aclp->i_isize;
}
maxaclsize = ((maxaclsize / sblock.fs_bsize) + 1) * sblock.fs_bsize;
if (maxaclsize == 0)
goto noacls;
if (aclbuf != NULL) {
free((void *)aclbuf);
}
if ((aclbuf = malloc(maxaclsize)) == NULL) {
errexit("cannot alloc %lld bytes for aclbuf\n",
(longlong_t)maxaclsize);
}
/*
* Scan all the acl inodes, checking contents
*/
for (inumber = 0; inumber < aclplast; inumber++) {
aclp = aclpsort[inumber];
if ((int64_t)aclp->i_isize > acl_size_limit) {
continue;
}
if ((statemap[aclp->i_number] & STMASK) != SSTATE) {
continue;
}
dp = ginode(aclp->i_number);
init_inodesc(&curino);
curino.id_fix = FIX;
curino.id_type = ACL;
curino.id_func = pass3bcheck;
curino.id_number = aclp->i_number;
curino.id_filesize = aclp->i_isize;
aclbufoff = 0;
(void) memset(aclbuf, 0, (size_t)maxaclsize);
if ((ckinode(dp, &curino, CKI_TRAVERSE) & KEEPON) == 0 ||
bufchk(aclbuf, (int64_t)aclp->i_isize, aclp->i_number)) {
dp = ginode(aclp->i_number); /* defensive no-op */
if (dp->di_nlink <= 0) {
statemap[aclp->i_number] = FSTATE;
continue;
}
(void) printf("ACL I=%d BAD/CORRUPT", aclp->i_number);
if (preen || reply("CLEAR") == 1) {
if (preen)
(void) printf("\n");
freeino(aclp->i_number, TI_PARENT);
} else {
iscorrupt = 1;
}
}
}
/*
* Now scan all shadow inodes, checking that any inodes that previously
* had an acl still have an acl.
*/
noacls:
for (sci = shadowclientinfo; sci; sci = sci->next) {
if ((statemap[sci->shadow] & STMASK) != SSTATE) {
for (scc = sci->clients; scc; scc = scc->next) {
for (i = 0; i < scc->nclients; i++) {
clear_shadow_client(sci, scc, i);
}
}
}
}
free((void *)aclbuf);
aclbuf = NULL;
}
static void
clear_shadow_client(struct shadowclientinfo *sci, struct shadowclients *scc,
int client)
{
int suppress_update = 0;
caddr_t flow;
struct inodesc ldesc;
struct dinode *dp;
(void) printf("I=%d HAS BAD/CLEARED ACL I=%d",
scc->client[client], sci->shadow);
if (preen || reply("FIX") == 1) {
if (preen)
(void) printf("\n");
/*
* If we clear the ACL, then the permissions should
* be as restrictive as possible until the user can
* set it to something reasonable. If we keep the
* ACL, then the permissions are pretty much
* irrelevant. So, just always clear the permission
* bits.
*/
dp = ginode(scc->client[client]);
dp->di_mode &= IFMT;
dp->di_shadow = 0;
inodirty();
/*
* Decrement in-memory link count - pass1 made sure
* the shadow inode # is a valid inode number. But
* first, see if we're going to overflow our sixteen
* bits.
*/
LINK_RANGE(flow, lncntp[dp->di_shadow], 1);
if (flow != NULL) {
LINK_CLEAR(flow, scc->client[client], dp->di_mode,
&ldesc);
if (statemap[scc->client[client]] == USTATE)
suppress_update = 1;
}
/*
* We don't touch the shadow's on-disk link count,
* because we've already cleared its state in pass3b().
* Here we're just trying to keep lncntp[] in sync, so
* we can detect spurious links.
*/
if (!suppress_update)
TRACK_LNCNTP(sci->shadow, lncntp[sci->shadow]++);
} else {
iscorrupt = 1;
}
}
/*
* Collect all the (data) blocks of an acl file into a buffer.
* Later we'll scan the buffer and validate the acl data.
*/
int
pass3bcheck(struct inodesc *idesc)
{
struct bufarea *bp;
size_t size, bsize;
if (aclbufoff == idesc->id_filesize) {
return (STOP);
}
bsize = size = sblock.fs_fsize * idesc->id_numfrags;
if ((size + aclbufoff) > idesc->id_filesize)
size = idesc->id_filesize - aclbufoff;
if (aclbufoff + size > maxaclsize)
errexit("acl size %lld exceeds maximum calculated "
"size of %lld bytes",
(longlong_t)aclbufoff + size, (longlong_t)maxaclsize);
bp = getdatablk(idesc->id_blkno, bsize);
if (bp->b_errs != 0) {
brelse(bp);
return (STOP);
}
(void) memmove((void *)(aclbuf + aclbufoff), (void *)bp->b_un.b_buf,
(size_t)size);
aclbufoff += size;
brelse(bp);
return (KEEPON);
}
/*
* Routine to sort disk blocks.
*/
static int
aclblksort(const void *pp1, const void *pp2)
{
const struct inoinfo **aclpp1 = (const struct inoinfo **)pp1;
const struct inoinfo **aclpp2 = (const struct inoinfo **)pp2;
return ((*aclpp1)->i_blks[0] - (*aclpp2)->i_blks[0]);
}
/*
* Scan a chunk of a shadow file. Return zero if no ACLs were found,
* or when all that were found were valid.
*/
static int
bufchk(char *buf, int64_t len, fsck_ino_t inum)
{
ufs_fsd_t *fsdp;
ufs_acl_t *ufsaclp = NULL;
int numacls;
int curacl;
struct type_counts_s {
int nuser_objs;
int ngroup_objs;
int nother_objs;
int nclass_objs;
int ndef_user_objs;
int ndef_group_objs;
int ndef_other_objs;
int ndef_class_objs;
int nusers;
int ngroups;
int ndef_users;
int ndef_groups;
} type_counts[3]; /* indexed by FSD_ACL and FSD_DFACL */
struct type_counts_s *tcp, *tcp_all, *tcp_def, *tcp_norm;
int numdefs;
caddr_t bad;
caddr_t end = buf + len;
int64_t recsz = 0;
int64_t min_recsz = FSD_RECSZ(fsdp, sizeof (*fsdp));
struct shadowclientinfo *sci;
struct shadowclients *scc;
fsck_ino_t target;
int numtargets = 0;
/*
* check we have a non-zero length for this shadow inode
*/
if (len == 0) {
pwarn("ACL I=%d HAS ZERO LENGTH\n", inum);
return (1);
}
(void) memset(type_counts, 0, sizeof (type_counts));
/* LINTED pointer cast alignment (aligned buffer always passed in) */
for (fsdp = (ufs_fsd_t *)buf;
(caddr_t)fsdp < end;
/* LINTED as per the above */
fsdp = (ufs_fsd_t *)((caddr_t)fsdp + recsz)) {
recsz = FSD_RECSZ(fsdp, fsdp->fsd_size);
if ((recsz < min_recsz) ||
(((caddr_t)fsdp + recsz) > (buf + len))) {
pwarn("Bad FSD entry size %lld in shadow inode %d",
recsz, inum);
if (reply("CLEAR SHADOW INODE") == 1) {
freeino(inum, TI_PARENT);
} else {
/*
* Bad size can cause the kernel to
* go traipsing off into never-never land.
*/
iscorrupt = 1;
}
return (0);
}
switch (fsdp->fsd_type) {
case FSD_FREE: /* ignore empty slots */
break;
case FSD_ACL:
case FSD_DFACL:
/*
* Subtract out the two ints in the fsd_type,
* leaving us just the size of fsd_data[].
*/
numacls = (fsdp->fsd_size - 2 * sizeof (int)) /
sizeof (ufs_acl_t);
tcp = &type_counts[fsdp->fsd_type];
curacl = 0;
/* LINTED pointer cast alignment */
for (ufsaclp = (ufs_acl_t *)fsdp->fsd_data;
numacls; ufsaclp++, curacl++) {
switch (ufsaclp->acl_tag) {
case USER_OBJ: /* Owner */
tcp->nuser_objs++;
break;
case GROUP_OBJ: /* Group */
tcp->ngroup_objs++;
break;
case OTHER_OBJ: /* Other */
tcp->nother_objs++;
break;
case CLASS_OBJ: /* Mask */
tcp->nclass_objs++;
break;
case DEF_USER_OBJ: /* Default Owner */
tcp->ndef_user_objs++;
break;
case DEF_GROUP_OBJ: /* Default Group */
tcp->ndef_group_objs++;
break;
case DEF_OTHER_OBJ: /* Default Other */
tcp->ndef_other_objs++;
break;
case DEF_CLASS_OBJ: /* Default Mask */
tcp->ndef_class_objs++;
break;
case USER: /* Users */
tcp->nusers++;
break;
case GROUP: /* Groups */
tcp->ngroups++;
break;
case DEF_USER: /* Default Users */
tcp->ndef_users++;
break;
case DEF_GROUP: /* Default Groups */
tcp->ndef_groups++;
break;
default:
return (1);
}
if ((ufsaclp->acl_perm & ~07) != 0) {
/*
* Caller will report inode, etc
*/
pwarn("Bad permission 0%o in ACL\n",
ufsaclp->acl_perm);
return (1);
}
numacls--;
}
break;
default:
if (fsdp->fsd_type >= FSD_RESERVED3 &&
fsdp->fsd_type <= FSD_RESERVED7)
bad = "Unexpected";
else
bad = "Unknown";
pwarn("%s FSD type %d in shadow inode %d",
bad, fsdp->fsd_type, inum);
/*
* This is relatively harmless, since the
* kernel will ignore any entries it doesn't
* recognize. Don't bother with iscorrupt.
*/
if (preen) {
(void) printf(" (IGNORED)\n");
} else if (reply("IGNORE") == 0) {
if (reply("CLEAR SHADOW INODE") == 1) {
freeino(inum, TI_PARENT);
}
return (0);
}
break;
}
}
if ((caddr_t)fsdp != (buf + len)) {
return (1);
}
/* If we didn't find any acls, ignore the unknown attribute */
if (ufsaclp == NULL)
return (0);
/*
* Should only have default ACLs in FSD_DFACL records.
* However, the kernel can handle it, so just report that
* something odd might be going on.
*/
tcp = &type_counts[FSD_DFACL];
if (verbose &&
(tcp->nuser_objs != 0 ||
tcp->ngroup_objs != 0 ||
tcp->nother_objs != 0 ||
tcp->nclass_objs != 0 ||
tcp->nusers != 0 ||
tcp->ngroups != 0)) {
(void) printf("NOTE: ACL I=%d has miscategorized ACLs. ",
inum);
(void) printf("This is harmless, but not normal.\n");
}
/*
* Similarly for default ACLs in FSD_ACL records.
*/
tcp = &type_counts[FSD_ACL];
if (verbose &&
(tcp->ndef_user_objs != 0 ||
tcp->ndef_group_objs != 0 ||
tcp->ndef_other_objs != 0 ||
tcp->ndef_class_objs != 0 ||
tcp->ndef_users != 0 ||
tcp->ndef_groups != 0)) {
(void) printf("NOTE: ACL I=%d has miscategorized ACLs.",
inum);
(void) printf(" This is harmless, but not normal.\n");
}
/*
* Get consolidated totals, now that we're done with checking
* the segregation above. Assumes that neither FSD_ACL nor
* FSD_DFACL are zero.
*/
tcp_all = &type_counts[0];
tcp_norm = &type_counts[FSD_ACL];
tcp_def = &type_counts[FSD_DFACL];
tcp_all->nuser_objs = tcp_def->nuser_objs + tcp_norm->nuser_objs;
tcp_all->ngroup_objs = tcp_def->ngroup_objs + tcp_norm->ngroup_objs;
tcp_all->nother_objs = tcp_def->nother_objs + tcp_norm->nother_objs;
tcp_all->nclass_objs = tcp_def->nclass_objs + tcp_norm->nclass_objs;
tcp_all->ndef_user_objs =
tcp_def->ndef_user_objs + tcp_norm->ndef_user_objs;
tcp_all->ndef_group_objs =
tcp_def->ndef_group_objs + tcp_norm->ndef_group_objs;
tcp_all->ndef_other_objs =
tcp_def->ndef_other_objs + tcp_norm->ndef_other_objs;
tcp_all->ndef_class_objs =
tcp_def->ndef_class_objs + tcp_norm->ndef_class_objs;
tcp_all->nusers = tcp_def->nusers + tcp_norm->nusers;
tcp_all->ngroups = tcp_def->ngroups + tcp_norm->ngroups;
tcp_all->ndef_users = tcp_def->ndef_users + tcp_norm->ndef_users;
tcp_all->ndef_groups = tcp_def->ndef_groups + tcp_norm->ndef_groups;
/*
* Check relationships among acls
*/
if (tcp_all->nuser_objs != 1 ||
tcp_all->ngroup_objs != 1 ||
tcp_all->nother_objs != 1 ||
tcp_all->nclass_objs > 1) {
return (1);
}
if (tcp_all->ngroups && !tcp_all->nclass_objs) {
return (1);
}
if (tcp_all->ndef_user_objs > 1 ||
tcp_all->ndef_group_objs > 1 ||
tcp_all->ndef_other_objs > 1 ||
tcp_all->ndef_class_objs > 1) {
return (1);
}
/*
* Check relationships among default acls
*/
numdefs = tcp_all->ndef_other_objs + tcp_all->ndef_user_objs +
tcp_all->ndef_group_objs;
if (numdefs != 0 && numdefs != 3) {
return (1);
}
/*
* If there are default acls, then the shadow inode's clients
* must be a directory or an xattr directory.
*/
if (numdefs != 0) {
/* This is an ACL so find it's clients */
for (sci = shadowclientinfo; sci != NULL; sci = sci->next)
if (sci->shadow == inum)
break;
if ((sci == NULL) || (sci->clients == NULL))
return (1);
/* Got shadow info, now look at clients */
for (scc = sci->clients; scc != NULL; scc = scc->next) {
for (numtargets = 0; numtargets < scc->nclients;
numtargets++) {
target = scc->client[numtargets];
if (!INO_IS_DVALID(target))
return (1);
}
}
}
if (tcp_all->ndef_groups && !tcp_all->ndef_class_objs) {
return (1);
}
if ((tcp_all->ndef_users || tcp_all->ndef_groups) &&
((numdefs != 3) && !tcp_all->ndef_class_objs)) {
return (1);
}
return (0);
}
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that: (1) source distributions retain this entire copyright
* notice and comment, and (2) distributions including binaries display
* the following acknowledgement: ``This product includes software
* developed by the University of California, Berkeley and its contributors''
* in the documentation or other materials provided with the distribution
* and in all advertising materials mentioning features or use of this
* software. Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/mntent.h>
#include <sys/fs/ufs_fs.h>
#include <sys/vnode.h>
#include <sys/fs/ufs_inode.h>
#include "fsck.h"
void
pass4(void)
{
fsck_ino_t inumber;
struct dinode *dp;
struct inodesc idesc;
int n, was_dir;
int need_rescan;
int scan_pass = 0;
/*
* If we clear a directory, it may have produced orphans which
* we need to go pick up. So, do this until done. It can be
* proven that the loop terminates because at most there can
* be lastino directories, and we only rescan if we clear a
* directory.
*/
do {
if (debug)
(void) printf("pass4 scan %d\n", scan_pass++);
need_rescan = 0;
for (inumber = UFSROOTINO; inumber <= lastino; inumber++) {
init_inodesc(&idesc);
idesc.id_type = ADDR;
idesc.id_func = pass4check;
idesc.id_number = inumber;
was_dir = (statemap[inumber] & DSTATE) == DSTATE;
switch (statemap[inumber] & ~(INORPHAN | INDELAYD
| INZLINK)) {
case FZLINK:
case DZLINK:
/*
* INZLINK gets set if the inode claimed zero
* links when we first looked at it in pass 1.
* If lncntp[] also claims it has zero links,
* it really is unreferenced. However, we
* could have found a link to it during one of
* the other passes, so we have to check the
* final count in lncntp[].
*/
if (lncntp[inumber] == 0) {
clri(&idesc, "UNREF", CLRI_VERBOSE,
CLRI_NOP_OK);
if (was_dir &&
(statemap[inumber] == USTATE))
need_rescan = 1;
break;
}
/* FALLTHROUGH */
case FSTATE:
case DFOUND:
case SSTATE:
n = lncntp[inumber];
if (n || (statemap[inumber] &
(INDELAYD | INZLINK))) {
/*
* adjust() will clear the inode if
* the link count goes to zero. If
* it isn't cleared, we need to note
* that we've adjusted the count
* already, so we don't do it again
* on a rescan.
*/
adjust(&idesc, n);
if (was_dir &&
(statemap[inumber] == USTATE)) {
need_rescan = 1;
} else {
TRACK_LNCNTP(inumber,
lncntp[inumber] = 0);
}
}
break;
case DSTATE:
clri(&idesc, "UNREF", CLRI_VERBOSE,
CLRI_NOP_OK);
if (was_dir && (statemap[inumber] == USTATE))
need_rescan = 1;
break;
case DCLEAR:
dp = ginode(inumber);
if (dp->di_size == 0) {
clri(&idesc, "ZERO LENGTH",
CLRI_VERBOSE, CLRI_NOP_CORRUPT);
break;
}
/* FALLTHROUGH */
case FCLEAR:
clri(&idesc, "BAD/DUP", CLRI_VERBOSE,
CLRI_NOP_CORRUPT);
break;
case SCLEAR:
clri(&idesc, "BAD", CLRI_VERBOSE,
CLRI_NOP_CORRUPT);
break;
case USTATE:
break;
default:
errexit("BAD STATE 0x%x FOR INODE I=%d",
(int)statemap[inumber], inumber);
}
}
} while (need_rescan);
}
int
pass4check(struct inodesc *idesc)
{
int fragnum, cg_frag;
int res = KEEPON;
daddr32_t blkno = idesc->id_blkno;
int cylno;
struct cg *cgp = &cgrp;
caddr_t err;
if ((idesc->id_truncto >= 0) && (idesc->id_lbn < idesc->id_truncto)) {
if (debug)
(void) printf(
"pass4check: skipping inode %d lbn %d with truncto %d\n",
idesc->id_number, idesc->id_lbn,
idesc->id_truncto);
return (KEEPON);
}
for (fragnum = 0; fragnum < idesc->id_numfrags; fragnum++) {
if (chkrange(blkno + fragnum, 1)) {
res = SKIP;
} else if (testbmap(blkno + fragnum)) {
/*
* The block's in use. Remove our reference
* from it.
*
* If it wasn't a dup, or everybody's done with
* it, then this is the last reference and it's
* safe to actually deallocate the on-disk block.
*
* We depend on pass 5 resolving the on-disk bitmap
* effects.
*/
cg_frag = blkno + fragnum;
if (!find_dup_ref(cg_frag, idesc->id_number,
idesc->id_lbn * sblock.fs_frag + fragnum,
DB_DECR)) {
if (debug)
(void) printf("p4c marking %d avail\n",
cg_frag);
clrbmap(cg_frag);
n_blks--;
/*
* Do the same for the on-disk bitmap, so
* that we don't need another pass to figure
* out what's really being used. We'll let
* pass5() work out the fragment/block
* accounting.
*/
cylno = dtog(&sblock, cg_frag);
(void) getblk(&cgblk, cgtod(&sblock, cylno),
(size_t)sblock.fs_cgsize);
err = cg_sanity(cgp, cylno);
if (err != NULL) {
pfatal("CG %d: %s\n", cylno, err);
free((void *)err);
if (reply("REPAIR") == 0)
errexit("Program terminated.");
fix_cg(cgp, cylno);
}
clrbit(cg_blksfree(cgp),
dtogd(&sblock, cg_frag));
cgdirty();
res |= ALTERED;
}
}
}
return (res);
}
/*
* Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that: (1) source distributions retain this entire copyright
* notice and comment, and (2) distributions including binaries display
* the following acknowledgement: ``This product includes software
* developed by the University of California, Berkeley and its contributors''
* in the documentation or other materials provided with the distribution
* and in all advertising materials mentioning features or use of this
* software. Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/param.h>
#include <sys/mntent.h>
#include <sys/fs/ufs_fs.h>
#include <sys/vnode.h>
#include <sys/fs/ufs_inode.h>
#include "fsck.h"
static int check_maps(uchar_t *, uchar_t *, int, int, char *, int, int);
void
pass5(void)
{
caddr_t err;
int32_t c, blk, frags;
size_t basesize, sumsize, mapsize;
int excessdirs;
int inomapsize, blkmapsize;
int update_csums, update_bitmaps;
int bad_csum_sb, bad_csum_cg, bad_cgblks_cg, bad_cgblktot_cg;
struct fs *fs = &sblock;
struct cg *cg = &cgrp;
diskaddr_t dbase, dmax;
diskaddr_t d;
uint64_t i, j;
struct csum *cs;
struct csum backup_cs;
time_t now;
struct csum cstotal;
struct inodesc idesc;
union { /* keep lint happy about alignment */
struct cg cg; /* the rest of buf has the bitmaps */
char buf[MAXBSIZE];
} u;
caddr_t buf = u.buf;
struct cg *newcg = &u.cg;
(void) memset((void *)buf, 0, sizeof (u.buf));
newcg->cg_niblk = fs->fs_ipg;
if (fs->fs_postblformat != FS_DYNAMICPOSTBLFMT) {
pfatal("UNSUPPORTED ROTATIONAL TABLE FORMAT %d\n",
fs->fs_postblformat);
errexit("Program terminated.");
/* NOTREACHED */
}
/* LINTED this subtraction can't overflow and is int32-aligned */
basesize = &newcg->cg_space[0] - (uchar_t *)newcg;
/*
* We reserve the space for the old rotation summary
* tables for the benefit of old kernels, but do not
* maintain them in modern kernels. In time, they could
* theoretically go away, if we wanted to deal with
* changing the on-disk format.
*/
/*
* Note that we don't use any of the cg_*() macros until
* after cg_sanity() has approved of what we've got.
*/
newcg->cg_btotoff = basesize;
newcg->cg_boff = newcg->cg_btotoff + fs->fs_cpg * sizeof (daddr32_t);
newcg->cg_iusedoff = newcg->cg_boff +
fs->fs_cpg * fs->fs_nrpos * sizeof (uint16_t);
(void) memset(&newcg->cg_space[0], 0, newcg->cg_iusedoff - basesize);
inomapsize = howmany(fs->fs_ipg, NBBY);
newcg->cg_freeoff = newcg->cg_iusedoff + inomapsize;
blkmapsize = howmany(fs->fs_fpg, NBBY);
newcg->cg_nextfreeoff = newcg->cg_freeoff + blkmapsize;
newcg->cg_magic = CG_MAGIC;
sumsize = newcg->cg_iusedoff - newcg->cg_btotoff;
mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff;
init_inodesc(&idesc);
idesc.id_type = ADDR;
(void) memset((void *)&cstotal, 0, sizeof (struct csum));
now = time(NULL);
/*
* If the last fragments in the file system don't make up a
* full file system block, mark the bits in the blockmap
* that correspond to those missing fragments as "allocated",
* so that the last block doesn't get counted as a free block
* and those missing fragments don't get counted as free frags.
*/
j = blknum(fs, (uint64_t)fs->fs_size + fs->fs_frag - 1);
for (i = fs->fs_size; i < j; i++)
setbmap(i);
/*
* The cg summaries are not always updated when using
* logging. Since we're really concerned with getting a
* sane filesystem, rather than in trying to debug UFS
* corner cases, logically we would just always recompute
* them. However, it is disconcerting to users to be asked
* about updating the summaries when, from their point of
* view, there's been no indication of a problem up to this
* point. So, only do it if we find a discrepancy.
*/
update_csums = -1;
update_bitmaps = 0;
for (c = 0; c < fs->fs_ncg; c++) {
backup_cs = cstotal;
/*
* cg_sanity() will catch i/o errors for us.
*/
(void) getblk(&cgblk, (diskaddr_t)cgtod(fs, c),
(size_t)fs->fs_cgsize);
err = cg_sanity(cg, c);
if (err != NULL) {
pfatal("CG %d: %s\n", c, err);
free((void *)err);
if (reply("REPAIR") == 0)
errexit("Program terminated.");
fix_cg(cg, c);
}
/*
* If the on-disk timestamp is in the future, then it
* by definition is wrong. Otherwise, if it's in
* the past, then use that value so that we don't
* declare a spurious mismatch.
*/
if (now > cg->cg_time)
newcg->cg_time = cg->cg_time;
else
newcg->cg_time = now;
newcg->cg_cgx = c;
dbase = cgbase(fs, c);
dmax = dbase + fs->fs_fpg;
if (dmax > fs->fs_size)
dmax = fs->fs_size;
newcg->cg_ndblk = dmax - dbase;
if (c == fs->fs_ncg - 1)
newcg->cg_ncyl = fs->fs_ncyl - (fs->fs_cpg * c);
else
newcg->cg_ncyl = fs->fs_cpg;
newcg->cg_niblk = sblock.fs_ipg;
newcg->cg_cs.cs_ndir = 0;
newcg->cg_cs.cs_nffree = 0;
newcg->cg_cs.cs_nbfree = 0;
newcg->cg_cs.cs_nifree = fs->fs_ipg;
if ((cg->cg_rotor >= 0) && (cg->cg_rotor < newcg->cg_ndblk))
newcg->cg_rotor = cg->cg_rotor;
else
newcg->cg_rotor = 0;
if ((cg->cg_frotor >= 0) && (cg->cg_frotor < newcg->cg_ndblk))
newcg->cg_frotor = cg->cg_frotor;
else
newcg->cg_frotor = 0;
if ((cg->cg_irotor >= 0) && (cg->cg_irotor < newcg->cg_niblk))
newcg->cg_irotor = cg->cg_irotor;
else
newcg->cg_irotor = 0;
(void) memset((void *)&newcg->cg_frsum[0], 0,
sizeof (newcg->cg_frsum));
(void) memset((void *)cg_inosused(newcg), 0, (size_t)mapsize);
/* LINTED macro is int32-aligned per newcg->cg_btotoff above */
(void) memset((void *)&cg_blktot(newcg)[0], 0,
sumsize + mapsize);
j = fs->fs_ipg * c;
for (i = 0; i < fs->fs_ipg; j++, i++) {
switch (statemap[j] & ~(INORPHAN | INDELAYD)) {
case USTATE:
break;
case DSTATE:
case DCLEAR:
case DFOUND:
case DZLINK:
newcg->cg_cs.cs_ndir++;
/* FALLTHROUGH */
case FSTATE:
case FCLEAR:
case FZLINK:
case SSTATE:
case SCLEAR:
newcg->cg_cs.cs_nifree--;
setbit(cg_inosused(newcg), i);
break;
default:
if (j < UFSROOTINO)
break;
errexit("BAD STATE 0x%x FOR INODE I=%d",
statemap[j], (int)j);
}
}
if (c == 0) {
for (i = 0; i < UFSROOTINO; i++) {
setbit(cg_inosused(newcg), i);
newcg->cg_cs.cs_nifree--;
}
}
/*
* Count up what fragments and blocks are free, and
* reflect the relevant section of blockmap[] into
* newcg's map.
*/
for (i = 0, d = dbase;
d < dmax;
d += fs->fs_frag, i += fs->fs_frag) {
frags = 0;
for (j = 0; j < fs->fs_frag; j++) {
if (testbmap(d + j))
continue;
setbit(cg_blksfree(newcg), i + j);
frags++;
}
if (frags == fs->fs_frag) {
newcg->cg_cs.cs_nbfree++;
j = cbtocylno(fs, i);
/* LINTED macro is int32-aligned per above */
cg_blktot(newcg)[j]++;
/* LINTED cg_blks(newcg) is aligned */
cg_blks(fs, newcg, j)[cbtorpos(fs, i)]++;
} else if (frags > 0) {
newcg->cg_cs.cs_nffree += frags;
blk = blkmap(fs, cg_blksfree(newcg), i);
fragacct(fs, blk, newcg->cg_frsum, 1);
}
}
cstotal.cs_nffree += newcg->cg_cs.cs_nffree;
cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree;
cstotal.cs_nifree += newcg->cg_cs.cs_nifree;
cstotal.cs_ndir += newcg->cg_cs.cs_ndir;
/*
* Note that, just like the kernel, we dynamically
* allocated an array to hold the csums and stuffed
* the pointer into the in-core superblock's fs_u.fs_csp
* field. This means that the fs_u field contains a
* random value when the disk version is examined, but
* fs_cs() gives us a valid pointer nonetheless.
* We need to compare the recalculated summaries to
* both the superblock version and the on disk version.
* If either is bad, copy the calculated version over
* the corrupt values.
*/
cs = &fs->fs_cs(fs, c);
bad_csum_sb = (memcmp((void *)cs, (void *)&newcg->cg_cs,
sizeof (*cs)) != 0);
bad_csum_cg = (memcmp((void *)&cg->cg_cs, (void *)&newcg->cg_cs,
sizeof (struct csum)) != 0);
/*
* Has the user told us what to do yet? If not, find out.
*/
if ((bad_csum_sb || bad_csum_cg) && (update_csums == -1)) {
if (preen) {
update_csums = 1;
(void) printf("CORRECTING BAD CG SUMMARIES"
" FOR CG %d\n", c);
} else if (update_csums == -1) {
update_csums = (reply(
"CORRECT BAD CG SUMMARIES FOR CG %d",
c) == 1);
}
}
if (bad_csum_sb && (update_csums == 1)) {
(void) memmove((void *)cs, (void *)&newcg->cg_cs,
sizeof (*cs));
sbdirty();
(void) printf("CORRECTED SUPERBLOCK SUMMARIES FOR"
" CG %d\n", c);
}
if (bad_csum_cg && (update_csums == 1)) {
(void) memmove((void *)cg, (void *)newcg,
(size_t)basesize);
/* LINTED per cg_sanity() */
(void) memmove((void *)&cg_blktot(cg)[0],
/* LINTED macro aligned as above */
(void *)&cg_blktot(newcg)[0], sumsize);
cgdirty();
(void) printf("CORRECTED SUMMARIES FOR CG %d\n", c);
}
excessdirs = cg->cg_cs.cs_ndir - newcg->cg_cs.cs_ndir;
if (excessdirs < 0) {
pfatal("LOST %d DIRECTORIES IN CG %d\n",
-excessdirs, c);
excessdirs = 0;
}
if (excessdirs > 0) {
if (check_maps((uchar_t *)cg_inosused(newcg),
(uchar_t *)cg_inosused(cg), inomapsize,
cg->cg_cgx * fs->fs_ipg, "DIR", 0, excessdirs)) {
if (!verbose)
(void) printf("DIR BITMAP WRONG ");
if (preen || update_bitmaps ||
reply("FIX") == 1) {
(void) memmove((void *)cg_inosused(cg),
(void *)cg_inosused(newcg),
inomapsize);
cgdirty();
if (preen ||
(!verbose && update_bitmaps))
(void) printf("(CORRECTED)\n");
update_bitmaps = 1;
}
}
}
if (check_maps((uchar_t *)cg_inosused(newcg),
(uchar_t *)cg_inosused(cg), inomapsize,
cg->cg_cgx * fs->fs_ipg, "FILE", excessdirs, fs->fs_ipg)) {
if (!verbose)
(void) printf("FILE BITMAP WRONG ");
if (preen || update_bitmaps || reply("FIX") == 1) {
(void) memmove((void *)cg_inosused(cg),
(void *)cg_inosused(newcg), inomapsize);
cgdirty();
if (preen ||
(!verbose && update_bitmaps))
(void) printf("(CORRECTED)\n");
update_bitmaps = 1;
}
}
if (check_maps((uchar_t *)cg_blksfree(cg),
(uchar_t *)cg_blksfree(newcg), blkmapsize,
cg->cg_cgx * fs->fs_fpg, "FRAG", 0, fs->fs_fpg)) {
if (!verbose)
(void) printf("FRAG BITMAP WRONG ");
if (preen || update_bitmaps || reply("FIX") == 1) {
(void) memmove((void *)cg_blksfree(cg),
(void *)cg_blksfree(newcg), blkmapsize);
cgdirty();
if (preen ||
(!verbose && update_bitmaps))
(void) printf("(CORRECTED)\n");
update_bitmaps = 1;
}
}
bad_cgblks_cg = (memcmp((void *)&cg_blks(fs, cg, 0)[0],
(void *)&cg_blks(fs, newcg, 0)[0],
fs->fs_cpg * fs->fs_nrpos * sizeof (int16_t)) != 0);
if (bad_cgblks_cg) {
if (!verbose)
(void) printf("ROTATIONAL POSITIONS "
"BLOCK COUNT WRONG ");
if (preen || update_bitmaps || reply("FIX") == 1) {
(void) memmove((void *)&cg_blks(fs, cg, 0)[0],
(void *)&cg_blks(fs, newcg, 0)[0],
fs->fs_cpg * fs->fs_nrpos *
sizeof (int16_t));
cgdirty();
if (preen ||
(!verbose && update_bitmaps))
(void) printf("(CORRECTED)\n");
update_bitmaps = 1;
}
}
bad_cgblktot_cg = (memcmp((void *)&cg_blktot(cg)[0],
(void *)&cg_blktot(newcg)[0],
fs->fs_cpg * sizeof (int32_t)) != 0);
if (bad_cgblktot_cg) {
if (!verbose)
(void) printf("ROTATIONAL POSITIONS "
"BLOCK TOTAL WRONG ");
if (preen || update_bitmaps || reply("FIX") == 1) {
(void) memmove((void *)&cg_blktot(cg)[0],
(void *)&cg_blktot(newcg)[0],
fs->fs_cpg * sizeof (int32_t));
cgdirty();
if (preen ||
(!verbose && update_bitmaps))
(void) printf("(CORRECTED)\n");
update_bitmaps = 1;
}
}
/*
* Fixing one set of problems often shows up more in the
* same cg. Just to make sure, go back and check it
* again if we found something this time through.
*/
if (cgisdirty()) {
cgflush();
cstotal = backup_cs;
c--;
}
}
if ((fflag || !(islog && islogok)) &&
(memcmp((void *)&cstotal, (void *)&fs->fs_cstotal,
sizeof (struct csum)) != 0)) {
if (dofix(&idesc, "CORRECT GLOBAL SUMMARY")) {
(void) memmove((void *)&fs->fs_cstotal,
(void *)&cstotal, sizeof (struct csum));
fs->fs_ronly = 0;
fs->fs_fmod = 0;
sbdirty();
} else {
iscorrupt = 1;
}
}
}
/*
* Compare two allocation bitmaps, reporting any discrepancies.
*
* If a mismatch is found, if the bit is set in map1, it's considered
* to be an indication that the corresponding resource is supposed
* to be free, but isn't. Otherwise, it's considered marked as allocated
* but not found to be so. In other words, if the two maps being compared
* use a set bit to indicate something is free, pass the on-disk map
* first. Otherwise, pass the calculated map first.
*/
static int
check_maps(
uchar_t *map1, /* map of claimed allocations */
uchar_t *map2, /* map of determined allocations */
int mapsize, /* size of above two maps */
int startvalue, /* resource value for first element in map */
char *name, /* name of resource found in maps */
int skip, /* number of entries to skip before starting to free */
int limit) /* limit on number of entries to free */
{
long i, j, k, l, m, n, size;
int astart, aend, ustart, uend;
int mismatch;
mismatch = 0;
astart = ustart = aend = uend = -1;
for (i = 0; i < mapsize; i++) {
j = *map1++;
k = *map2++;
if (j == k)
continue;
for (m = 0, l = 1; m < NBBY; m++, l <<= 1) {
if ((j & l) == (k & l))
continue;
n = startvalue + i * NBBY + m;
if ((j & l) != 0) {
if (astart == -1) {
astart = aend = n;
continue;
}
if (aend + 1 == n) {
aend = n;
continue;
}
if (verbose) {
if (astart == aend)
pwarn(
"ALLOCATED %s %d WAS MARKED FREE ON DISK\n",
name, astart);
else
pwarn(
"ALLOCATED %sS %d-%d WERE MARKED FREE ON DISK\n",
name, astart, aend);
}
mismatch = 1;
astart = aend = n;
} else {
if (ustart == -1) {
ustart = uend = n;
continue;
}
if (uend + 1 == n) {
uend = n;
continue;
}
size = uend - ustart + 1;
if (size <= skip) {
skip -= size;
ustart = uend = n;
continue;
}
if (skip > 0) {
ustart += skip;
size -= skip;
skip = 0;
}
if (size > limit)
size = limit;
if (verbose) {
if (size == 1)
pwarn(
"UNALLOCATED %s %d WAS MARKED USED ON DISK\n",
name, ustart);
else
pwarn(
"UNALLOCATED %sS %d-%ld WERE MARKED USED ON DISK\n",
name, ustart,
ustart + size - 1);
}
mismatch = 1;
limit -= size;
if (limit <= 0)
return (mismatch);
ustart = uend = n;
}
}
}
if (astart != -1) {
if (verbose) {
if (astart == aend)
pwarn(
"ALLOCATED %s %d WAS MARKED FREE ON DISK\n",
name, astart);
else
pwarn(
"ALLOCATED %sS %d-%d WERE MARKED FREE ON DISK\n",
name, astart, aend);
}
mismatch = 1;
}
if (ustart != -1) {
size = uend - ustart + 1;
if (size <= skip)
return (mismatch);
if (skip > 0) {
ustart += skip;
size -= skip;
}
if (size > limit)
size = limit;
if (verbose) {
if (size == 1)
pwarn(
"UNALLOCATED %s %d WAS MARKED USED ON DISK\n",
name, ustart);
else
pwarn(
"UNALLOCATED %sS %d-%ld WERE MARKED USED ON DISK\n",
name, ustart, ustart + size - 1);
}
mismatch = 1;
}
return (mismatch);
}
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that: (1) source distributions retain this entire copyright
* notice and comment, and (2) distributions including binaries display
* the following acknowledgement: ``This product includes software
* developed by the University of California, Berkeley and its contributors''
* in the documentation or other materials provided with the distribution
* and in all advertising materials mentioning features or use of this
* software. Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#define DKTYPENAMES
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <malloc.h>
#include <limits.h>
#include <wait.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/mntent.h>
#include <sys/dkio.h>
#include <sys/filio.h>
#include <sys/isa_defs.h> /* for ENDIAN defines */
#include <sys/int_const.h>
#include <sys/fs/ufs_fs.h>
#include <sys/vnode.h>
#include <sys/fs/ufs_fs.h>
#include <sys/fs/ufs_inode.h>
#include <sys/fs/ufs_log.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/vfstab.h>
#include "roll_log.h"
#include "fsck.h"
/*
* The size of a cylinder group is calculated by CGSIZE. The maximum size
* is limited by the fact that cylinder groups are at most one block.
* Its size is derived from the size of the maps maintained in the
* cylinder group and the (struct cg) size.
*/
#define CGSIZE(fs) \
/* base cg */ (sizeof (struct cg) + \
/* blktot size */ (fs)->fs_cpg * sizeof (int32_t) + \
/* blks size */ (fs)->fs_cpg * (fs)->fs_nrpos * sizeof (short) + \
/* inode map */ howmany((fs)->fs_ipg, NBBY) + \
/* block map */ howmany((fs)->fs_cpg * (fs)->fs_spc / NSPF(fs), NBBY))
#define altsblock (*asblk.b_un.b_fs)
#define POWEROF2(num) (((num) & ((num) - 1)) == 0)
/*
* Methods of determining where alternate superblocks should
* be. MAX_SB_STYLES must be the last one, and the others need
* to be positive.
*/
typedef enum {
MKFS_STYLE = 1, NEWFS_STYLE, MAX_SB_STYLES
} calcsb_t;
static caddr_t calcsb_names[] = {
"<UNKNOWN>", "MKFS", "NEWFS", "<OUT OF RANGE>"
};
fsck_ino_t lfdir;
int64_t numacls, aclmax, aclplast;
int64_t numdirs, listmax, inplast;
char havesb;
int fsreadfd;
int isdirty;
int pid;
int secsize;
size_t dev_bsize;
struct bufarea sblk;
static struct bufarea asblk; /* alternate superblock */
struct inoinfo **inphead, **inpsort;
struct shadowclientinfo *shadowclientinfo = NULL;
struct shadowclientinfo *attrclientinfo = NULL;
int maxshadowclients = 1024; /* allocation size, not limit */
static void badsb(int, caddr_t);
static int calcsb(calcsb_t, caddr_t, int, struct fs *);
static int checksb(int);
static void flush_fs(void);
static void sblock_init(void);
static void uncreate_maps(void);
static int
read_super_block(int listerr)
{
int fd;
caddr_t err;
if (mount_point != NULL) {
fd = open(mount_point, O_RDONLY);
if (fd == -1) {
errexit("fsck: open mount point error: %s",
strerror(errno));
/* NOTREACHED */
}
/* get the latest super block */
if (ioctl(fd, _FIOGETSUPERBLOCK, &sblock)) {
errexit("fsck: ioctl _FIOGETSUPERBLOCK error: %s",
strerror(errno));
/* NOTREACHED */
}
(void) close(fd);
} else {
(void) fsck_bread(fsreadfd, (caddr_t)&sblock,
bflag != 0 ? (diskaddr_t)bflag : (diskaddr_t)SBLOCK,
SBSIZE);
}
/*
* Don't let trash from the disk trip us up later
* in ungetsummaryinfo().
*/
sblock.fs_u.fs_csp = NULL;
/*
* Rudimentary consistency checks. Can't really call
* checksb() here, because there may be outstanding
* deltas that still need to be applied.
*/
if ((sblock.fs_magic != FS_MAGIC) &&
(sblock.fs_magic != MTB_UFS_MAGIC)) {
err = "MAGIC NUMBER WRONG";
goto fail;
}
if (sblock.fs_magic == FS_MAGIC &&
(sblock.fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
sblock.fs_version != UFS_VERSION_MIN)) {
err = "UNRECOGNIZED VERSION";
goto fail;
}
if (sblock.fs_magic == MTB_UFS_MAGIC &&
(sblock.fs_version > MTB_UFS_VERSION_1 ||
sblock.fs_version < MTB_UFS_VERSION_MIN)) {
err = "UNRECOGNIZED VERSION";
goto fail;
}
if (sblock.fs_ncg < 1) {
err = "NCG OUT OF RANGE";
goto fail;
}
if (sblock.fs_cpg < 1) {
err = "CPG OUT OF RANGE";
goto fail;
}
if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
(sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) {
err = "NCYL IS INCONSISTENT WITH NCG*CPG";
goto fail;
}
if (sblock.fs_sbsize < 0 || sblock.fs_sbsize > SBSIZE) {
err = "SIZE OUT OF RANGE";
goto fail;
}
return (1);
fail:
badsb(listerr, err);
return (0);
}
static void
flush_fs()
{
int fd;
if (mount_point != NULL) {
fd = open(mount_point, O_RDONLY);
if (fd == -1) {
errexit("fsck: open mount point error: %s",
strerror(errno));
/* NOTREACHED */
}
if (ioctl(fd, _FIOFFS, NULL)) { /* flush file system */
errexit("fsck: ioctl _FIOFFS error: %s",
strerror(errno));
/* NOTREACHED */
}
(void) close(fd);
}
}
/*
* Roll the embedded log, if any, and set up the global variables
* islog and islogok.
*/
static int
logsetup(caddr_t devstr)
{
void *buf;
extent_block_t *ebp;
ml_unit_t *ul;
ml_odunit_t *ud;
void *ud_buf;
int badlog;
islog = islogok = 0;
if (bflag != 0)
return (1); /* can't roll log while alternate sb specified */
/*
* Roll the log, if any. A bad sb implies we'll be using
* an alternate sb as far as logging goes, so just fail back
* to the caller if we can't read the default sb. Suppress
* complaints, because the caller will be reading the same
* superblock again and running full verification on it, so
* whatever is bad will be reported then.
*/
sblock.fs_logbno = 0;
badlog = 0;
if (!read_super_block(0))
return (1);
/*
* Roll the log in 3 cases:
* 1. If it's unmounted (mount_point == NULL) and it's not marked
* as fully rolled (sblock.fs_rolled != FS_ALL_ROLLED)
* 2. If it's mounted and anything other than a sanity
* check fsck (mflag) is being done, as we have the current
* super block. Note, only a sanity check is done for
* root/usr at boot. If a roll were done then the expensive
* ufs_flush() gets called, leading to a slower boot.
* 3. If anything other then a sanity check (mflag) is being done
* to a mounted filesystem while it is in read-only state
* (e.g. root during early boot stages) we have to detect this
* and have to roll the log as well. NB. the read-only mount
* will flip fs_clean from FSLOG to FSSTABLE and marks the
* log as FS_NEED_ROLL.
*/
if (sblock.fs_logbno &&
(((mount_point == NULL) && (sblock.fs_rolled != FS_ALL_ROLLED)) ||
((mount_point != NULL) && !mflag))) {
int roll_log_err = 0;
if (sblock.fs_ronly && (sblock.fs_clean == FSSTABLE) &&
(sblock.fs_state + sblock.fs_time == FSOKAY)) {
/*
* roll the log without a mount
*/
flush_fs();
}
if (sblock.fs_clean == FSLOG &&
(sblock.fs_state + sblock.fs_time == FSOKAY)) {
if (rl_roll_log(devstr) != RL_SUCCESS)
roll_log_err = 1;
}
if (roll_log_err) {
(void) printf("Can't roll the log for %s.\n", devstr);
/*
* There are two cases where we want to set
* an error code and return:
* - We're preening
* - We're not on a live root and the user
* chose *not* to ignore the log
* Otherwise, we want to mark the log as bad
* and continue to check the filesystem. This
* has the side effect of destroying the log.
*/
if (preen || (!hotroot &&
reply(
"DISCARDING THE LOG MAY DISCARD PENDING TRANSACTIONS.\n"
"DISCARD THE LOG AND CONTINUE") == 0)) {
exitstat = EXERRFATAL;
return (0);
}
++badlog;
}
}
/* Logging UFS may be enabled */
if (sblock.fs_logbno) {
++islog;
/* log is not okay; check the fs */
if (FSOKAY != (sblock.fs_state + sblock.fs_time))
return (1);
/*
* If logging or (stable and mounted) then continue
*/
if (!((sblock.fs_clean == FSLOG) ||
(sblock.fs_clean == FSSTABLE) && (mount_point != NULL)))
return (1);
/* get the log allocation block */
buf = malloc(dev_bsize);
if (buf == NULL) {
return (1);
}
ud_buf = malloc(dev_bsize);
if (ud_buf == NULL) {
free(buf);
return (1);
}
(void) fsck_bread(fsreadfd, buf,
logbtodb(&sblock, sblock.fs_logbno),
dev_bsize);
ebp = (extent_block_t *)buf;
/* log allocation block is not okay; check the fs */
if (ebp->type != LUFS_EXTENTS) {
free(buf);
free(ud_buf);
return (1);
}
/* get the log state block(s) */
if (fsck_bread(fsreadfd, ud_buf,
(logbtodb(&sblock, ebp->extents[0].pbno)),
dev_bsize)) {
(void) fsck_bread(fsreadfd, ud_buf,
(logbtodb(&sblock, ebp->extents[0].pbno)) + 1,
dev_bsize);
}
ud = (ml_odunit_t *)ud_buf;
ul = (ml_unit_t *)malloc(sizeof (*ul));
if (ul == NULL) {
free(buf);
free(ud_buf);
return (1);
}
ul->un_ondisk = *ud;
/* log state is okay; don't need to check the fs */
if ((ul->un_chksum == ul->un_head_ident + ul->un_tail_ident) &&
(ul->un_version == LUFS_VERSION_LATEST) &&
(ul->un_badlog == 0) && (!badlog)) {
++islogok;
}
free(ud_buf);
free(buf);
free(ul);
}
return (1);
}
/*
* - given a pathname, determine the pathname to actually check
* - if a directory
* - if it is in mnttab, set devstr to the special (block) name
* - if it is in vfstab, set devstr to the special (block) name
* - if it has not been found, bail
* - a file is used as-is, clear rflag
* - a device is converted to block version (so can search mnttab)
*/
static void
derive_devstr(const caddr_t dev, caddr_t devstr, size_t str_size)
{
mode_t mode;
struct stat statb;
if (stat(dev, &statb) < 0) {
exitstat = EXNOSTAT;
errexit("fsck: could not stat %s: %s", dev, strerror(errno));
}
mode = statb.st_mode & S_IFMT;
switch (mode) {
case S_IFDIR:
/*
* The check_*() routines update devstr with the name.
*/
devstr[0] = '\0';
if (!(check_mnttab(dev, devstr, str_size) ||
check_vfstab(dev, devstr, str_size))) {
exitstat = EXBADPARM;
errexit(
"fsck: could not find mountpoint %s in mnttab nor vfstab",
dev);
}
break;
case S_IFREG:
rflag = 0;
(void) strlcpy(devstr, dev, str_size);
break;
case S_IFCHR:
case S_IFBLK:
(void) strlcpy(devstr, unrawname(dev), str_size);
break;
default:
exitstat = EXBADPARM;
errexit("fsck: %s must be a mountpoint, device, or file", dev);
/* NOTREACHED */
}
}
/*
* Reports the index of the magic filesystem that mntp names.
* If it does not correspond any of them, returns zero (hence
* the backwards loop).
*/
static int
which_corefs(const caddr_t mntp)
{
int corefs;
for (corefs = MAGIC_LIMIT - 1; corefs > 0; corefs--)
if (strcmp(mntp, magic_fs[corefs]) == 0)
break;
return (corefs);
}
/*
* - set mount_point to NULL
* - if name is mounted (search mnttab)
* - if it is a device, clear rflag
* - if mounted on /, /usr, or /var, set corefs
* - if corefs and read-only, set hotroot and continue
* - if errorlocked, continue
* - if preening, bail
* - ask user whether to continue, bail if not
* - if it is a device and not mounted and rflag, convert
* name to raw version
*/
static int
check_mount_state(caddr_t devstr, size_t str_size)
{
int corefs = 0;
int is_dev = 0;
struct stat statb;
if (stat(devstr, &statb) < 0) {
exitstat = EXNOSTAT;
errexit("fsck: could not stat %s: %s", devstr, strerror(errno));
}
if (S_ISCHR(statb.st_mode) || S_ISBLK(statb.st_mode))
is_dev = 1;
/*
* mounted() will update mount_point when returning true.
*/
mount_point = NULL;
if ((mountedfs = mounted(devstr, devstr, str_size)) != M_NOMNT) {
if (is_dev)
rflag = 0;
corefs = which_corefs(mount_point);
if (corefs && (mountedfs == M_RO)) {
hotroot++;
} else if (errorlocked) {
goto carry_on;
} else if (preen) {
exitstat = EXMOUNTED;
pfatal("%s IS CURRENTLY MOUNTED%s.",
devstr, mountedfs == M_RW ? " READ/WRITE" : "");
} else {
if (!nflag && !mflag) {
pwarn("%s IS CURRENTLY MOUNTED READ/%s.",
devstr, mountedfs == M_RW ? "WRITE" :
"ONLY");
if (reply("CONTINUE") == 0) {
exitstat = EXMOUNTED;
errexit("Program terminated");
}
}
}
} else if (is_dev && rflag) {
(void) strlcpy(devstr, rawname(devstr), str_size);
}
carry_on:
return (corefs);
}
static int
open_and_intro(caddr_t devstr, int corefs)
{
int retval = 0;
if ((fsreadfd = open64(devstr, O_RDONLY)) < 0) {
(void) printf("Can't open %s: %s\n", devstr, strerror(errno));
exitstat = EXNOSTAT;
retval = -1;
goto finish;
}
if (!preen || debug != 0)
(void) printf("** %s", devstr);
if (errorlocked) {
if (debug && elock_combuf != NULL)
(void) printf(" error-lock comment: \"%s\" ",
elock_combuf);
fflag = 1;
}
pid = getpid();
if (nflag || (fswritefd = open64(devstr, O_WRONLY)) < 0) {
fswritefd = -1;
if (preen && !debug)
pfatal("(NO WRITE ACCESS)\n");
(void) printf(" (NO WRITE)");
}
if (!preen)
(void) printf("\n");
else if (debug)
(void) printf(" pid %d\n", pid);
if (debug && (hotroot || (mountedfs != M_NOMNT))) {
(void) printf("** %s", devstr);
if (hotroot)
(void) printf(" is %s fs", magic_fs[corefs]);
if (mountedfs != M_NOMNT)
(void) printf(" and is mounted read-%s",
(mountedfs == M_RO) ? "only" : "write");
if (errorlocked)
(void) printf(" and is error-locked");
(void) printf(".\n");
}
finish:
return (retval);
}
static int
find_superblock(caddr_t devstr)
{
int cg = 0;
int retval = 0;
int first;
int found;
calcsb_t style;
struct fs proto;
/*
* Check the superblock, looking for alternates if necessary.
* In more-recent times, some UFS instances get created with
* only the first ten and last ten superblock backups. Since
* if we can't get the necessary information from any of those,
* the odds are also against us for the ones in between, we'll
* just look at those twenty to save time.
*/
if (!read_super_block(1) || !checksb(1)) {
if (bflag || preen) {
retval = -1;
goto finish;
}
for (style = MKFS_STYLE; style < MAX_SB_STYLES; style++) {
if (reply("LOOK FOR ALTERNATE SUPERBLOCKS WITH %s",
calcsb_names[style]) == 0)
continue;
first = 1;
found = 0;
if (!calcsb(style, devstr, fsreadfd, &proto)) {
cg = proto.fs_ncg;
continue;
}
if (debug) {
(void) printf(
"debug: calcsb(%s) gave fpg %d, cgoffset %d, ",
calcsb_names[style],
proto.fs_fpg, proto.fs_cgoffset);
(void) printf("cgmask 0x%x, sblk %d, ncg %d\n",
proto.fs_cgmask, proto.fs_sblkno,
proto.fs_ncg);
}
for (cg = 0; cg < proto.fs_ncg; cg++) {
bflag = fsbtodb(&proto, cgsblock(&proto, cg));
if (debug)
(void) printf(
"debug: trying block %lld\n",
(longlong_t)bflag);
if (read_super_block(0) && checksb(0)) {
(void) printf(
"FOUND ALTERNATE SUPERBLOCK %d WITH %s\n",
bflag, calcsb_names[style]);
if (reply(
"USE ALTERNATE SUPERBLOCK") == 1) {
found = 1;
break;
}
}
if (first && (cg >= 9)) {
first = 0;
if (proto.fs_ncg <= 9)
cg = proto.fs_ncg;
else if (proto.fs_ncg <= 19)
cg = 9;
else
cg = proto.fs_ncg - 10;
}
}
if (found)
break;
}
/*
* Didn't find one? Try to fake it.
*/
if (style >= MAX_SB_STYLES) {
pwarn("SEARCH FOR ALTERNATE SUPERBLOCKS FAILED.\n");
for (style = MKFS_STYLE; style < MAX_SB_STYLES;
style++) {
if (reply("USE GENERIC SUPERBLOCK FROM %s",
calcsb_names[style]) == 1 &&
calcsb(style, devstr, fsreadfd, &sblock)) {
break;
}
}
/*
* We got something from mkfs/newfs, so use it.
*/
if (style < MAX_SB_STYLES) {
proto.fs_ncg = sblock.fs_ncg;
bflag = 0;
}
}
/*
* Still no luck? Tell the user they're on their own.
*/
if (style >= MAX_SB_STYLES) {
pwarn("SEARCH FOR ALTERNATE SUPERBLOCKS FAILED. "
"YOU MUST USE THE -o b OPTION\n"
"TO FSCK TO SPECIFY THE LOCATION OF A VALID "
"ALTERNATE SUPERBLOCK TO\n"
"SUPPLY NEEDED INFORMATION; SEE fsck(8).\n");
bflag = 0;
retval = -1;
goto finish;
}
/*
* Need to make sure a human really wants us to use
* this. -y mode could've gotten us this far, so
* we need to ask something that has to be answered
* in the negative.
*
* Note that we can't get here when preening.
*/
if (!found) {
pwarn("CALCULATED GENERIC SUPERBLOCK WITH %s\n",
calcsb_names[style]);
} else {
pwarn("FOUND ALTERNATE SUPERBLOCK AT %d USING %s\n",
bflag, calcsb_names[style]);
}
pwarn("If filesystem was created with manually-specified ");
pwarn("geometry, using\nauto-discovered superblock may ");
pwarn("result in irrecoverable damage to\nfilesystem and ");
pwarn("user data.\n");
if (reply("CANCEL FILESYSTEM CHECK") == 1) {
if (cg >= 0) {
pwarn("Please verify that the indicated block "
"contains a proper\nsuperblock for the "
"filesystem (see fsdb(8)).\n");
if (yflag)
pwarn("\nFSCK was running in YES "
"mode. If you wish to run in "
"that mode using\nthe alternate "
"superblock, run "
"`fsck -y -o b=%d %s'.\n",
bflag, devstr);
}
retval = -1;
goto finish;
}
/*
* Pretend we found it as an alternate, so everything
* gets updated when we clean up at the end.
*/
if (!found) {
havesb = 1;
sblk.b_bno = fsbtodb(&sblock, cgsblock(&sblock, 0));
bwrite(fswritefd, (caddr_t)&sblock, SBLOCK, SBSIZE);
write_altsb(fswritefd);
}
}
finish:
return (retval);
}
/*
* Check and potentially fix certain fields in the super block.
*/
static void
fixup_superblock(void)
{
/*
* Kernel looks for FS_OPTTIME, and assumes that if that's not
* what's there, it must be FS_OPTSPACE, so not fixing does not
* require setting iscorrupt.
*/
if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
if (reply("SET TO DEFAULT") == 1) {
sblock.fs_optim = FS_OPTTIME;
sbdirty();
}
}
if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
sblock.fs_minfree);
if (reply("SET TO DEFAULT") == 1) {
sblock.fs_minfree = 10;
sbdirty();
} else if (sblock.fs_minfree < 0) {
/*
* Kernel uses minfree without verification,
* and a negative value would do bad things.
*/
iscorrupt = 1;
}
}
}
static int
initial_error_state_adjust(void)
{
int retval = 0;
/* do this right away to prevent any other fscks on this fs */
switch (sblock.fs_clean) {
case FSBAD:
break;
case FSFIX:
if (preen)
errexit("ERROR-LOCKED; MARKED \"FSFIX\"\n");
if (reply("marked FSFIX, CONTINUE") == 0) {
retval = -1;
goto finish;
}
break;
case FSCLEAN:
if (preen)
errexit("ERROR-LOCKED; MARKED \"FSCLEAN\"\n");
if (reply("marked FSCLEAN, CONTINUE") == 0) {
retval = -1;
goto finish;
}
break;
default:
if (preen) {
if (debug)
pwarn("ERRORLOCKED; NOT MARKED \"FSBAD\"\n");
else
errexit("ERRORLOCKED; NOT MARKED \"FSBAD\"\n");
} else {
(void) printf("error-locked but not marked \"FSBAD\";");
if (reply(" CONTINUE") == 0) {
retval = -1;
goto finish;
}
}
break;
}
if (!do_errorlock(LOCKFS_ELOCK)) {
if (preen) {
retval = -1;
goto finish;
}
if (reply("error-lock reset failed; CONTINUE") == 0) {
retval = -1;
goto finish;
}
}
sblock.fs_state = FSOKAY - (long)sblock.fs_time;
sblock.fs_clean = FSFIX;
sbdirty();
write_altsb(fswritefd);
finish:
return (retval);
}
static void
getsummaryinfo(void)
{
size_t size;
int failed;
int asked;
int i, j;
caddr_t sip;
/*
* read in the summary info.
*/
sblock.fs_u.fs_csp = calloc(1, sblock.fs_cssize);
if (sblock.fs_u.fs_csp == NULL)
errexit(
"cannot allocate %u bytes for cylinder group summary info\n",
(unsigned)sblock.fs_cssize);
sip = (caddr_t)sblock.fs_u.fs_csp;
asked = 0;
for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
size = sblock.fs_cssize - i < sblock.fs_bsize ?
sblock.fs_cssize - i : sblock.fs_bsize;
failed = fsck_bread(fsreadfd, sip,
fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
size);
if (failed && !asked) {
pfatal("BAD SUMMARY INFORMATION");
if (reply("CONTINUE") == 0) {
ckfini();
exit(EXFNDERRS);
}
asked = 1;
}
sip += size;
}
}
/*
* Reverses the effects of getsummaryinfo().
*/
static void
ungetsummaryinfo(void)
{
if ((sblk.b_un.b_fs != NULL) &&
(sblk.b_un.b_fs->fs_u.fs_csp != NULL)) {
free(sblk.b_un.b_fs->fs_u.fs_csp);
sblk.b_un.b_fs->fs_u.fs_csp = NULL;
}
}
/*
* Allocate and initialize the global tables.
* It is the responsibility of the caller to clean up and allocations
* if an error is returned.
*/
static int
create_and_init_maps(void)
{
int64_t bmapsize;
int retval = 0;
maxfsblock = sblock.fs_size;
maxino = sblock.fs_ncg * sblock.fs_ipg;
bmapsize = roundup(howmany((uint64_t)maxfsblock, NBBY),
sizeof (short));
blockmap = calloc((size_t)bmapsize, sizeof (char));
if (blockmap == NULL) {
(void) printf("cannot alloc %lld bytes for blockmap\n",
(longlong_t)bmapsize);
retval = -1;
goto finish;
}
statemap = calloc((size_t)(maxino + 1), sizeof (*statemap));
if (statemap == NULL) {
(void) printf("cannot alloc %lld bytes for statemap\n",
(longlong_t)(maxino + 1) * sizeof (*statemap));
retval = -1;
goto finish;
}
lncntp = (short *)calloc((size_t)(maxino + 1), sizeof (short));
if (lncntp == NULL) {
(void) printf("cannot alloc %lld bytes for lncntp\n",
(longlong_t)(maxino + 1) * sizeof (short));
retval = -1;
goto finish;
}
/*
* If we had to fake up a superblock, it won't show that there
* are any directories at all. This causes problems when we
* use numdirs to calculate hash keys, so use something at least
* vaguely plausible.
*/
numdirs = sblock.fs_cstotal.cs_ndir;
if (numdirs == 0)
numdirs = sblock.fs_ipg * sblock.fs_ncg / 2;
listmax = numdirs + 10;
inpsort = (struct inoinfo **)calloc((unsigned)listmax,
sizeof (struct inoinfo *));
inphead = (struct inoinfo **)calloc((unsigned)numdirs,
sizeof (struct inoinfo *));
if (inpsort == NULL || inphead == NULL) {
(void) printf("cannot alloc %lld bytes for inphead\n",
(longlong_t)numdirs * sizeof (struct inoinfo *));
retval = -1;
goto finish;
}
if (debug) {
if (listmax > ULONG_MAX)
errexit("create_and_init_maps: listmax overflowed\n");
if (numdirs > ULONG_MAX)
errexit("create_and_init_maps: numdirs overflowed\n");
}
numacls = numdirs;
aclmax = numdirs + 10;
aclpsort = (struct inoinfo **)calloc((unsigned)aclmax,
sizeof (struct inoinfo *));
aclphead = (struct inoinfo **)calloc((unsigned)numacls,
sizeof (struct inoinfo *));
if (aclpsort == NULL || aclphead == NULL) {
(void) printf("cannot alloc %lld bytes for aclphead\n",
(longlong_t)numacls * sizeof (struct inoinfo *));
retval = -1;
goto finish;
}
if (debug) {
if (aclmax > ULONG_MAX)
errexit("create_and_init_maps: aclmax overflowed\n");
if (numacls > ULONG_MAX)
errexit("create_and_init_maps: numacls overflowed\n");
}
aclplast = 0L;
inplast = 0L;
finish:
return (retval);
}
caddr_t
setup(caddr_t dev)
{
int corefs;
static char devstr[MAXPATHLEN + 1];
havesb = 0;
devname = devstr;
derive_devstr(dev, devstr, sizeof (devstr));
errorlocked = is_errorlocked(devstr);
corefs = check_mount_state(devstr, sizeof (devstr));
sblock_init();
if (open_and_intro(devstr, corefs) == -1)
goto cleanup;
if (mflag && mounted(devstr, devstr,
sizeof (devstr)) == M_RW)
return (devstr);
/*
* Check log state
*/
if (!logsetup(devstr))
goto cleanup;
/*
* Flush fs if we're going to do anything other than a sanity check.
* Note, if logging then the fs was already flushed in logsetup().
*/
if (!islog && !mflag)
flush_fs();
if (find_superblock(devstr) == -1)
goto cleanup;
fixup_superblock();
if (errorlocked &&
(initial_error_state_adjust() == -1))
goto cleanup;
/*
* asblk could be dirty because we found a mismatch between
* the primary superblock and one of its backups in checksb().
*/
if (asblk.b_dirty && !bflag) {
(void) memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
flush(fswritefd, &asblk);
}
getsummaryinfo();
/*
* if not error-locked, using the standard superblock,
* not bad log, not forced, preening, and is clean;
* stop checking
*/
if (!errorlocked && (bflag == 0) &&
((!islog || islogok) &&
(fflag == 0) && preen &&
(FSOKAY == (sblock.fs_state + sblock.fs_time)) &&
((sblock.fs_clean == FSLOG && islog) ||
((sblock.fs_clean == FSCLEAN) || (sblock.fs_clean == FSSTABLE))))) {
iscorrupt = 0;
printclean();
goto cleanup;
}
if (create_and_init_maps() == -1)
goto nomaps;
bufinit();
return (devstr);
nomaps:
ckfini();
exitstat = EXERRFATAL;
/* FALLTHROUGH */
cleanup:
unbufinit();
uncreate_maps();
ungetsummaryinfo();
/*
* Can't get rid of the superblock buffer, because our
* caller references it to generate the summary statistics.
*/
return (NULL);
}
/*
* Undoes the allocations in create_and_init_maps()
*/
static void
uncreate_maps(void)
{
/*
* No ordering dependency amongst these, so they are here in
* the same order they were calculated.
*/
if (blockmap != NULL)
free(blockmap);
if (statemap != NULL)
free(statemap);
if (lncntp != NULL)
free(lncntp);
if (inpsort != NULL)
free(inpsort);
if (inphead != NULL)
free(inphead);
if (aclpsort != NULL)
free(aclpsort);
if (aclphead != NULL)
free(aclphead);
}
/*
* mkfs limits the size of the inode map to be no more than a third of
* the cylinder group space. We'll use that value for sanity checking
* the superblock's inode per group value.
*/
#define MAXIpG (roundup(sblock.fs_bsize * NBBY / 3, sblock.fs_inopb))
/*
* Check the super block and its summary info.
*/
static int
checksb(int listerr)
{
caddr_t err;
/*
* When the fs check is successfully completed, the alternate super
* block at sblk.b_bno will be overwritten by ckfini() with the
* repaired super block.
*/
sblk.b_bno = bflag ? bflag : (SBOFF / dev_bsize);
sblk.b_size = SBSIZE;
/*
* Sanity-check some of the values we are going to use later
* in allocation requests.
*/
if (sblock.fs_cstotal.cs_ndir < 1 ||
sblock.fs_cstotal.cs_ndir > sblock.fs_ncg * sblock.fs_ipg) {
if (verbose)
(void) printf(
"Found %d directories, should be between 1 and %d inclusive.\n",
sblock.fs_cstotal.cs_ndir,
sblock.fs_ncg * sblock.fs_ipg);
err = "NUMBER OF DIRECTORIES OUT OF RANGE";
goto failedsb;
}
if (sblock.fs_nrpos <= 0 || sblock.fs_postbloff < 0 ||
sblock.fs_cpc < 0 ||
(sblock.fs_postbloff +
(sblock.fs_nrpos * sblock.fs_cpc * sizeof (short))) >
sblock.fs_sbsize) {
err = "ROTATIONAL POSITION TABLE SIZE OUT OF RANGE";
goto failedsb;
}
if (sblock.fs_cssize !=
fragroundup(&sblock, sblock.fs_ncg * sizeof (struct csum))) {
err = "SIZE OF CYLINDER GROUP SUMMARY AREA WRONG";
goto failedsb;
}
if (sblock.fs_inopb != (sblock.fs_bsize / sizeof (struct dinode))) {
err = "INOPB NONSENSICAL RELATIVE TO BSIZE";
goto failedsb;
}
if (sblock.fs_bsize > MAXBSIZE) {
err = "BLOCK SIZE LARGER THAN MAXIMUM SUPPORTED";
goto failedsb;
}
if (sblock.fs_bsize != (sblock.fs_frag * sblock.fs_fsize)) {
err = "FRAGS PER BLOCK OR FRAG SIZE WRONG";
goto failedsb;
}
if (sblock.fs_dsize >= sblock.fs_size) {
err = "NUMBER OF DATA BLOCKS OUT OF RANGE";
goto failedsb;
}
#if 0
if (sblock.fs_size >
(sblock.fs_nsect * sblock.fs_ntrak * sblock.fs_ncyl)) {
err = "FILESYSTEM SIZE LARGER THAN DEVICE";
goto failedsb;
}
#endif
/*
* Check that the number of inodes per group isn't less than or
* equal to zero. Also makes sure it isn't more than the
* maximum number mkfs enforces.
*/
if (sblock.fs_ipg <= 0 || sblock.fs_ipg > MAXIpG) {
err = "INODES PER GROUP OUT OF RANGE";
goto failedsb;
}
if (sblock.fs_cgsize > sblock.fs_bsize) {
err = "CG HEADER LARGER THAN ONE BLOCK";
goto failedsb;
}
/*
* Set all possible fields that could differ, then do check
* of whole super block against an alternate super block.
* When an alternate super-block is specified this check is skipped.
*/
(void) getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1),
(size_t)sblock.fs_sbsize);
if (asblk.b_errs != 0) {
brelse(&asblk);
return (0);
}
if (bflag != 0) {
/*
* Invalidate clean flag and state information.
* Note that we couldn't return until after the
* above getblk(), because we're going to want to
* update asblk when everything's done.
*/
sblock.fs_clean = FSACTIVE;
sblock.fs_state = (long)sblock.fs_time;
sblock.fs_reclaim = 0;
sbdirty();
havesb = 1;
return (1);
}
altsblock.fs_link = sblock.fs_link;
altsblock.fs_rolled = sblock.fs_rolled;
altsblock.fs_time = sblock.fs_time;
altsblock.fs_state = sblock.fs_state;
altsblock.fs_cstotal = sblock.fs_cstotal;
altsblock.fs_cgrotor = sblock.fs_cgrotor;
altsblock.fs_fmod = sblock.fs_fmod;
altsblock.fs_clean = sblock.fs_clean;
altsblock.fs_ronly = sblock.fs_ronly;
altsblock.fs_flags = sblock.fs_flags;
altsblock.fs_maxcontig = sblock.fs_maxcontig;
altsblock.fs_minfree = sblock.fs_minfree;
altsblock.fs_optim = sblock.fs_optim;
altsblock.fs_rotdelay = sblock.fs_rotdelay;
altsblock.fs_maxbpg = sblock.fs_maxbpg;
altsblock.fs_logbno = sblock.fs_logbno;
altsblock.fs_reclaim = sblock.fs_reclaim;
altsblock.fs_si = sblock.fs_si;
(void) memmove((void *)altsblock.fs_fsmnt, (void *)sblock.fs_fsmnt,
sizeof (sblock.fs_fsmnt));
/*
* The following should not have to be copied.
*/
(void) memmove((void *)altsblock.fs_u.fs_csp_pad,
(void *)sblock.fs_u.fs_csp_pad, sizeof (sblock.fs_u.fs_csp_pad));
altsblock.fs_fsbtodb = sblock.fs_fsbtodb;
altsblock.fs_npsect = sblock.fs_npsect;
altsblock.fs_nrpos = sblock.fs_nrpos;
if (memcmp((void *)&sblock, (void *)&altsblock,
(size_t)sblock.fs_sbsize) != 0) {
err = "BAD VALUES IN SUPER BLOCK";
goto failedsb;
}
havesb = 1;
return (1);
failedsb:
badsb(listerr, err);
return (0);
}
static void
badsb(int listerr, caddr_t s)
{
if (!listerr)
return;
if (preen)
(void) printf("%s: ", devname);
(void) printf("BAD SUPERBLOCK AT BLOCK %d: %s\n",
bflag != 0 ? bflag : SBLOCK, s);
if (preen) {
pwarn(
"USE AN ALTERNATE SUPERBLOCK TO SUPPLY NEEDED INFORMATION;\n");
pwarn("e.g. fsck [-F ufs] -o b=# [special ...] \n");
exitstat = EXERRFATAL;
pfatal(
"where # is the alternate super block. SEE fsck_ufs(8). \n");
}
/* we're expected to return if not preening */
}
/*
* Write out the super block into each of the alternate super blocks.
*/
void
write_altsb(int fd)
{
int cylno;
for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
bwrite(fd, (caddr_t)&sblock, fsbtodb(&sblock,
cgsblock(&sblock, cylno)), sblock.fs_sbsize);
}
static void
sblock_init(void)
{
fsmodified = 0;
if (errorlocked)
isdirty = 1;
lfdir = 0;
initbarea(&sblk);
initbarea(&asblk);
/*
* May have buffer left over from previous filesystem check.
*/
if (sblk.b_un.b_buf == NULL)
sblk.b_un.b_buf = calloc(1, SBSIZE);
if (asblk.b_un.b_buf == NULL)
asblk.b_un.b_buf = calloc(1, SBSIZE);
if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
errexit("cannot allocate space for superblock\n");
/*
* Could get the actual sector size from the device here,
* but considering how much would need to change in the rest
* of the system before it'd be a problem for us, it's not
* worth worrying about right now.
*/
dev_bsize = secsize = DEV_BSIZE;
}
/*
* Calculate a prototype superblock based on information in the disk label.
* When done the cgsblock macro can be calculated and the fs_ncg field
* can be used. Do NOT attempt to use other macros without verifying that
* their needed information is available!
*
* In BSD, the disk label includes all sorts of useful information,
* like cpg. Solaris doesn't have that, and deriving it (as well as
* some other parameters) is difficult. Rather than duplicate the
* code, just ask mkfs what it would've come up with by default.
* Ideally, we'd just link in the code, but given the source base
* involved, it's more practical to just get a binary dump.
*
* The one minor drawback to the above approach is that newfs and mkfs
* will produce vastly different layouts for the same partition if
* they're allowed to default everything. So, if the superblock that
* mkfs gives us doesn't work for guessing where the alternates are,
* we need to try newfs.
*/
static int
calcsb(calcsb_t style, caddr_t dev, int devfd, struct fs *fs)
{
#define FROM_CHILD 0
#define TO_FSCK 1
#define CMD_IDX 0
#define DEV_IDX 3
#define SIZE_IDX 4
int child_pipe[2];
caddr_t mkfsline[] = {
"", /* CMD_IDX */
"-o",
"calcbinsb,N",
NULL, /* DEV_IDX */
NULL, /* SIZE_IDX */
NULL
};
caddr_t newfsline[] = {
"", /* CMD_IDX */
"-B",
"-N",
NULL, /* DEV_IDX */
NULL
};
int pending, transferred;
caddr_t *cmdline;
caddr_t target;
caddr_t sizestr = NULL;
caddr_t path_old, path_new, mkfs_dir, mkfs_path, newfs_path;
caddr_t slash;
diskaddr_t size;
int devnull;
switch (style) {
case MKFS_STYLE:
if (debug)
(void) printf("calcsb() going with style MKFS\n");
cmdline = mkfsline;
break;
case NEWFS_STYLE:
if (debug)
(void) printf("calcsb() going with style NEWFS\n");
cmdline = newfsline;
break;
default:
if (debug)
(void) printf("calcsb() doesn't undestand style %d\n",
style);
return (0);
}
cmdline[DEV_IDX] = dev;
/*
* Normally, only use the stock versions of the utilities.
* However, if we're debugging, the odds are that we're
* using experimental versions of them as well, so allow
* some flexibility.
*/
mkfs_path = getenv("MKFS_PATH");
if (!debug || (mkfs_path == NULL))
mkfs_path = MKFS_PATH;
newfs_path = getenv("NEWFS_PATH");
if (!debug || (newfs_path == NULL))
newfs_path = NEWFS_PATH;
if (style == MKFS_STYLE) {
cmdline[CMD_IDX] = mkfs_path;
size = getdisksize(dev, devfd);
if (size == 0)
return (0);
(void) fsck_asprintf(&sizestr, "%lld", (longlong_t)size);
cmdline[SIZE_IDX] = sizestr;
} else if (style == NEWFS_STYLE) {
/*
* Make sure that newfs will find the right version of mkfs.
*/
cmdline[CMD_IDX] = newfs_path;
path_old = getenv("PATH");
/* mkfs_path is always initialized, despite lint's concerns */
mkfs_dir = strdup(mkfs_path);
if (mkfs_dir == NULL)
return (0);
/*
* If no location data for mkfs, don't need to do
* anything about PATH.
*/
slash = strrchr(mkfs_dir, '/');
if (slash != NULL) {
/*
* Just want the dir, so discard the executable name.
*/
*slash = '\0';
/*
* newfs uses system() to find mkfs, so make sure
* that the one we want to use is first on the
* list. Don't free path_new upon success, as it
* has become part of the environment.
*/
(void) fsck_asprintf(&path_new, "PATH=%s:%s",
mkfs_dir, path_old);
if (putenv(path_new) != 0) {
free(mkfs_dir);
free(path_new);
return (0);
}
}
free(mkfs_dir);
} else {
/*
* Bad search style, quietly return failure.
*/
if (debug) {
(void) printf("calcsb: got bad style number %d\n",
(int)style);
}
return (0);
}
if (pipe(child_pipe) < 0) {
pfatal("calcsb: could not create pipe: %s\n", strerror(errno));
if (sizestr != NULL)
free(sizestr);
return (0);
}
switch (fork()) {
case -1:
pfatal("calcsb: fork failed: %s\n", strerror(errno));
if (sizestr != NULL)
free(sizestr);
return (0);
case 0:
if (dup2(child_pipe[TO_FSCK], fileno(stdout)) < 0) {
(void) printf(
"calcsb: could not rename file descriptor: %s\n",
strerror(errno));
exit(EXBADPARM);
}
devnull = open("/dev/null", O_WRONLY);
if (devnull == -1) {
(void) printf("calcsb: could not open /dev/null: %s\n",
strerror(errno));
exit(EXBADPARM);
}
if (dup2(devnull, fileno(stderr)) < 0) {
(void) printf(
"calcsb: could not rename file descriptor: %s\n",
strerror(errno));
exit(EXBADPARM);
}
(void) close(child_pipe[FROM_CHILD]);
(void) execv(cmdline[CMD_IDX], cmdline);
(void) printf("calcsb: could not exec %s: %s\n",
cmdline[CMD_IDX], strerror(errno));
exit(EXBADPARM);
/* NOTREACHED */
default:
break;
}
(void) close(child_pipe[TO_FSCK]);
if (sizestr != NULL)
free(sizestr);
pending = sizeof (struct fs);
target = (caddr_t)fs;
do {
transferred = read(child_pipe[FROM_CHILD], target, pending);
pending -= transferred;
target += transferred;
} while ((pending > 0) && (transferred > 0));
if (pending > 0) {
if (transferred < 0)
pfatal(
"calcsb: binary read of superblock from %s failed: %s\n",
(style == MKFS_STYLE) ? "mkfs" : "newfs",
(transferred < 0) ? strerror(errno) : "");
else
pfatal(
"calcsb: short read of superblock from %s\n",
(style == MKFS_STYLE) ? "mkfs" : "newfs");
return (0);
}
(void) close(child_pipe[FROM_CHILD]);
(void) wait(NULL);
if ((fs->fs_magic != FS_MAGIC) &&
(fs->fs_magic != MTB_UFS_MAGIC))
return (0);
return (1);
}
/*
* Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016 by Delphix. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that: (1) source distributions retain this entire copyright
* notice and comment, and (2) distributions including binaries display
* the following acknowledgement: ``This product includes software
* developed by the University of California, Berkeley and its contributors''
* in the documentation or other materials provided with the distribution
* and in all advertising materials mentioning features or use of this
* software. Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <libadm.h>
#include <note.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/mntent.h>
#include <sys/filio.h>
#include <sys/fs/ufs_fs.h>
#include <sys/vnode.h>
#include <sys/fs/ufs_acl.h>
#include <sys/fs/ufs_inode.h>
#include <sys/fs/ufs_log.h>
#define _KERNEL
#include <sys/fs/ufs_fsdir.h>
#undef _KERNEL
#include <sys/mnttab.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <ctype.h>
#include <sys/vfstab.h>
#include <sys/lockfs.h>
#include <errno.h>
#include <sys/cmn_err.h>
#include <sys/dkio.h>
#include <sys/vtoc.h>
#include <sys/efi_partition.h>
#include <fslib.h>
#include <inttypes.h>
#include "fsck.h"
struct bufarea *pbp;
struct bufarea *pdirbp;
caddr_t mount_point = NULL;
static struct bufarea bufhead; /* head of list of other blks in filesys */
char *elock_combuf;
char *elock_mountp;
static struct lockfs *lfp; /* current lockfs status */
static int64_t diskreads, totalreads; /* Disk cache statistics */
static int log_checksum(int32_t *, int32_t *, int);
static void vdirerror(fsck_ino_t, caddr_t, va_list);
static struct mnttab *search_mnttab(caddr_t, caddr_t, caddr_t, size_t);
static struct vfstab *search_vfstab(caddr_t, caddr_t, caddr_t, size_t);
static void vpwarn(caddr_t, va_list);
static int getaline(FILE *, caddr_t, int);
static struct bufarea *alloc_bufarea(void);
static void rwerror(caddr_t, diskaddr_t, int rval);
static void debugclean(void);
static void report_io_prob(caddr_t, diskaddr_t, size_t, ssize_t);
static void freelogblk(daddr32_t);
static void verrexit(caddr_t, va_list);
static void vpfatal(caddr_t, va_list);
static diskaddr_t get_device_size(int, caddr_t);
static diskaddr_t brute_force_get_device_size(int);
static void cg_constants(int, daddr32_t *, daddr32_t *, daddr32_t *,
daddr32_t *, daddr32_t *, daddr32_t *);
int
ftypeok(struct dinode *dp)
{
switch (dp->di_mode & IFMT) {
case IFDIR:
case IFREG:
case IFBLK:
case IFCHR:
case IFLNK:
case IFSOCK:
case IFIFO:
case IFSHAD:
case IFATTRDIR:
return (1);
default:
if (debug)
(void) printf("bad file type 0%o\n", dp->di_mode);
return (0);
}
}
int
acltypeok(struct dinode *dp)
{
if (CHECK_ACL_ALLOWED(dp->di_mode & IFMT))
return (1);
if (debug)
(void) printf("bad file type for acl I=%d: 0%o\n",
dp->di_shadow, dp->di_mode);
return (0);
}
NOTE(PRINTFLIKE(1))
int
reply(caddr_t fmt, ...)
{
va_list ap;
char line[80];
if (preen)
pfatal("INTERNAL ERROR: GOT TO reply() in preen mode");
if (mflag) {
/*
* We don't know what's going on, so don't potentially
* make things worse by having errexit() write stuff
* out to disk.
*/
(void) printf(
"\n%s: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.\n",
devname);
exit(EXERRFATAL);
}
va_start(ap, fmt);
(void) putchar('\n');
(void) vprintf(fmt, ap);
(void) putchar('?');
(void) putchar(' ');
va_end(ap);
if (nflag || fswritefd < 0) {
(void) printf(" no\n\n");
return (0);
}
if (yflag) {
(void) printf(" yes\n\n");
return (1);
}
(void) fflush(stdout);
if (getaline(stdin, line, sizeof (line)) == EOF)
errexit("\n");
(void) printf("\n");
if (line[0] == 'y' || line[0] == 'Y') {
return (1);
} else {
return (0);
}
}
int
getaline(FILE *fp, caddr_t loc, int maxlen)
{
int n;
caddr_t p, lastloc;
p = loc;
lastloc = &p[maxlen-1];
while ((n = getc(fp)) != '\n') {
if (n == EOF)
return (EOF);
if (!isspace(n) && p < lastloc)
*p++ = (char)n;
}
*p = '\0';
/* LINTED pointer difference won't overflow */
return (p - loc);
}
/*
* Malloc buffers and set up cache.
*/
void
bufinit(void)
{
struct bufarea *bp;
int bufcnt, i;
caddr_t bufp;
bufp = malloc((size_t)sblock.fs_bsize);
if (bufp == NULL)
goto nomem;
initbarea(&cgblk);
cgblk.b_un.b_buf = bufp;
bufhead.b_next = bufhead.b_prev = &bufhead;
bufcnt = MAXBUFSPACE / sblock.fs_bsize;
if (bufcnt < MINBUFS)
bufcnt = MINBUFS;
for (i = 0; i < bufcnt; i++) {
bp = (struct bufarea *)malloc(sizeof (struct bufarea));
if (bp == NULL) {
if (i >= MINBUFS)
goto noalloc;
goto nomem;
}
bufp = malloc((size_t)sblock.fs_bsize);
if (bufp == NULL) {
free((void *)bp);
if (i >= MINBUFS)
goto noalloc;
goto nomem;
}
initbarea(bp);
bp->b_un.b_buf = bufp;
bp->b_prev = &bufhead;
bp->b_next = bufhead.b_next;
bufhead.b_next->b_prev = bp;
bufhead.b_next = bp;
}
noalloc:
bufhead.b_size = i; /* save number of buffers */
pbp = pdirbp = NULL;
return;
nomem:
errexit("cannot allocate buffer pool\n");
/* NOTREACHED */
}
/*
* Undo a bufinit().
*/
void
unbufinit(void)
{
int cnt;
struct bufarea *bp, *nbp;
cnt = 0;
for (bp = bufhead.b_prev; bp != NULL && bp != &bufhead; bp = nbp) {
cnt++;
flush(fswritefd, bp);
nbp = bp->b_prev;
/*
* We're discarding the entire chain, so this isn't
* technically necessary. However, it doesn't hurt
* and lint's data flow analysis is much happier
* (this prevents it from thinking there's a chance
* of our using memory elsewhere after it's been released).
*/
nbp->b_next = bp->b_next;
bp->b_next->b_prev = nbp;
free((void *)bp->b_un.b_buf);
free((void *)bp);
}
if (bufhead.b_size != cnt)
errexit("Panic: cache lost %d buffers\n",
bufhead.b_size - cnt);
}
/*
* Manage a cache of directory blocks.
*/
struct bufarea *
getdatablk(daddr32_t blkno, size_t size)
{
struct bufarea *bp;
for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
if (bp->b_bno == fsbtodb(&sblock, blkno)) {
goto foundit;
}
for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
if ((bp->b_flags & B_INUSE) == 0)
break;
if (bp == &bufhead) {
bp = alloc_bufarea();
if (bp == NULL) {
errexit("deadlocked buffer pool\n");
/* NOTREACHED */
}
}
/*
* We're at the same logical level as getblk(), so if there
* are any errors, we'll let our caller handle them.
*/
diskreads++;
(void) getblk(bp, blkno, size);
foundit:
totalreads++;
bp->b_cnt++;
/*
* Move the buffer to head of linked list if it isn't
* already there.
*/
if (bufhead.b_next != bp) {
bp->b_prev->b_next = bp->b_next;
bp->b_next->b_prev = bp->b_prev;
bp->b_prev = &bufhead;
bp->b_next = bufhead.b_next;
bufhead.b_next->b_prev = bp;
bufhead.b_next = bp;
}
bp->b_flags |= B_INUSE;
return (bp);
}
void
brelse(struct bufarea *bp)
{
bp->b_cnt--;
if (bp->b_cnt == 0) {
bp->b_flags &= ~B_INUSE;
}
}
struct bufarea *
getblk(struct bufarea *bp, daddr32_t blk, size_t size)
{
diskaddr_t dblk;
dblk = fsbtodb(&sblock, blk);
if (bp->b_bno == dblk)
return (bp);
flush(fswritefd, bp);
bp->b_errs = fsck_bread(fsreadfd, bp->b_un.b_buf, dblk, size);
bp->b_bno = dblk;
bp->b_size = size;
return (bp);
}
void
flush(int fd, struct bufarea *bp)
{
int i, j;
caddr_t sip;
long size;
if (!bp->b_dirty)
return;
/*
* It's not our buf, so if there are errors, let whoever
* acquired it deal with the actual problem.
*/
if (bp->b_errs != 0)
pfatal("WRITING ZERO'ED BLOCK %lld TO DISK\n", bp->b_bno);
bp->b_dirty = 0;
bp->b_errs = 0;
bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
if (bp != &sblk) {
return;
}
/*
* We're flushing the superblock, so make sure all the
* ancillary bits go out as well.
*/
sip = (caddr_t)sblock.fs_u.fs_csp;
for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
size = sblock.fs_cssize - i < sblock.fs_bsize ?
sblock.fs_cssize - i : sblock.fs_bsize;
bwrite(fswritefd, sip,
fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
size);
sip += size;
}
}
static void
rwerror(caddr_t mesg, diskaddr_t blk, int rval)
{
int olderr = errno;
if (!preen)
(void) printf("\n");
if (rval == -1)
pfatal("CANNOT %s: DISK BLOCK %lld: %s",
mesg, blk, strerror(olderr));
else
pfatal("CANNOT %s: DISK BLOCK %lld", mesg, blk);
if (reply("CONTINUE") == 0) {
exitstat = EXERRFATAL;
errexit("Program terminated\n");
}
}
void
ckfini(void)
{
int64_t percentage;
if (fswritefd < 0)
return;
flush(fswritefd, &sblk);
/*
* Were we using a backup superblock?
*/
if (havesb && sblk.b_bno != SBOFF / dev_bsize) {
if (preen || reply("UPDATE STANDARD SUPERBLOCK") == 1) {
sblk.b_bno = SBOFF / dev_bsize;
sbdirty();
flush(fswritefd, &sblk);
}
}
flush(fswritefd, &cgblk);
if (cgblk.b_un.b_buf != NULL) {
free((void *)cgblk.b_un.b_buf);
cgblk.b_un.b_buf = NULL;
}
unbufinit();
pbp = NULL;
pdirbp = NULL;
if (debug) {
/*
* Note that we only count cache-related reads.
* Anything that called fsck_bread() or getblk()
* directly are explicitly not cached, so they're not
* included here.
*/
if (totalreads != 0)
percentage = diskreads * 100 / totalreads;
else
percentage = 0;
(void) printf("cache missed %lld of %lld reads (%lld%%)\n",
(longlong_t)diskreads, (longlong_t)totalreads,
(longlong_t)percentage);
}
(void) close(fsreadfd);
(void) close(fswritefd);
fsreadfd = -1;
fswritefd = -1;
}
int
fsck_bread(int fd, caddr_t buf, diskaddr_t blk, size_t size)
{
caddr_t cp;
int i;
int errs;
offset_t offset = ldbtob(blk);
offset_t addr;
/*
* In our universe, nothing exists before the superblock, so
* just pretend it's always zeros. This is the complement of
* bwrite()'s ignoring write requests into that space.
*/
if (blk < SBLOCK) {
if (debug)
(void) printf(
"WARNING: fsck_bread() passed blkno < %d (%lld)\n",
SBLOCK, (longlong_t)blk);
(void) memset(buf, 0, (size_t)size);
return (1);
}
if (llseek(fd, offset, SEEK_SET) < 0) {
rwerror("SEEK", blk, -1);
}
if ((i = read(fd, buf, size)) == size) {
return (0);
}
rwerror("READ", blk, i);
if (llseek(fd, offset, SEEK_SET) < 0) {
rwerror("SEEK", blk, -1);
}
errs = 0;
(void) memset(buf, 0, (size_t)size);
pwarn("THE FOLLOWING SECTORS COULD NOT BE READ:");
for (cp = buf, i = 0; i < btodb(size); i++, cp += DEV_BSIZE) {
addr = ldbtob(blk + i);
if (llseek(fd, addr, SEEK_SET) < 0 ||
read(fd, cp, (int)secsize) < 0) {
iscorrupt = 1;
(void) printf(" %llu", blk + (u_longlong_t)i);
errs++;
}
}
(void) printf("\n");
return (errs);
}
void
bwrite(int fd, caddr_t buf, diskaddr_t blk, int64_t size)
{
int i;
int n;
caddr_t cp;
offset_t offset = ldbtob(blk);
offset_t addr;
if (fd < 0)
return;
if (blk < SBLOCK) {
if (debug)
(void) printf(
"WARNING: Attempt to write illegal blkno %lld on %s\n",
(longlong_t)blk, devname);
return;
}
if (llseek(fd, offset, SEEK_SET) < 0) {
rwerror("SEEK", blk, -1);
}
if ((i = write(fd, buf, (int)size)) == size) {
fsmodified = 1;
return;
}
rwerror("WRITE", blk, i);
if (llseek(fd, offset, SEEK_SET) < 0) {
rwerror("SEEK", blk, -1);
}
pwarn("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
for (cp = buf, i = 0; i < btodb(size); i++, cp += DEV_BSIZE) {
n = 0;
addr = ldbtob(blk + i);
if (llseek(fd, addr, SEEK_SET) < 0 ||
(n = write(fd, cp, DEV_BSIZE)) < 0) {
iscorrupt = 1;
(void) printf(" %llu", blk + (u_longlong_t)i);
} else if (n > 0) {
fsmodified = 1;
}
}
(void) printf("\n");
}
/*
* Allocates the specified number of contiguous fragments.
*/
daddr32_t
allocblk(int wantedfrags)
{
int block, leadfrag, tailfrag;
daddr32_t selected;
size_t size;
struct bufarea *bp;
/*
* It's arguable whether we should just fail, or instead
* error out here. Since we should only ever be asked for
* a single fragment or an entire block (i.e., sblock.fs_frag),
* we'll fail out because anything else means somebody
* changed code without considering all of the ramifications.
*/
if (wantedfrags <= 0 || wantedfrags > sblock.fs_frag) {
exitstat = EXERRFATAL;
errexit("allocblk() asked for %d frags. "
"Legal range is 1 to %d",
wantedfrags, sblock.fs_frag);
}
/*
* For each filesystem block, look at every possible starting
* offset within the block such that we can get the number of
* contiguous fragments that we need. This is a drastically
* simplified version of the kernel's mapsearch() and alloc*().
* It's also correspondingly slower.
*/
for (block = 0; block < maxfsblock - sblock.fs_frag;
block += sblock.fs_frag) {
for (leadfrag = 0; leadfrag <= sblock.fs_frag - wantedfrags;
leadfrag++) {
/*
* Is first fragment of candidate run available?
*/
if (testbmap(block + leadfrag))
continue;
/*
* Are the rest of them available?
*/
for (tailfrag = 1; tailfrag < wantedfrags; tailfrag++)
if (testbmap(block + leadfrag + tailfrag))
break;
if (tailfrag < wantedfrags) {
/*
* No, skip the known-unusable run.
*/
leadfrag += tailfrag;
continue;
}
/*
* Found what we need, so claim them.
*/
for (tailfrag = 0; tailfrag < wantedfrags; tailfrag++)
setbmap(block + leadfrag + tailfrag);
n_blks += wantedfrags;
size = wantedfrags * sblock.fs_fsize;
selected = block + leadfrag;
bp = getdatablk(selected, size);
(void) memset((void *)bp->b_un.b_buf, 0, size);
dirty(bp);
brelse(bp);
if (debug)
(void) printf(
"allocblk: selected %d (in block %d), frags %d, size %d\n",
selected, selected % sblock.fs_bsize,
wantedfrags, (int)size);
return (selected);
}
}
return (0);
}
/*
* Free a previously allocated block
*/
void
freeblk(fsck_ino_t ino, daddr32_t blkno, int frags)
{
struct inodesc idesc;
if (debug)
(void) printf("debug: freeing %d fragments starting at %d\n",
frags, blkno);
init_inodesc(&idesc);
idesc.id_number = ino;
idesc.id_blkno = blkno;
idesc.id_numfrags = frags;
idesc.id_truncto = -1;
/*
* Nothing in the return status has any relevance to how
* we're using pass4check(), so just ignore it.
*/
(void) pass4check(&idesc);
}
/*
* Fill NAMEBUF with a path starting in CURDIR for INO. Assumes
* that the given buffer is at least MAXPATHLEN + 1 characters.
*/
void
getpathname(caddr_t namebuf, fsck_ino_t curdir, fsck_ino_t ino)
{
int len;
caddr_t cp;
struct dinode *dp;
struct inodesc idesc;
struct inoinfo *inp;
if (debug)
(void) printf("debug: getpathname(curdir %d, ino %d)\n",
curdir, ino);
if ((curdir == 0) || (!INO_IS_DVALID(curdir))) {
(void) strcpy(namebuf, "?");
return;
}
if ((curdir == UFSROOTINO) && (ino == UFSROOTINO)) {
(void) strcpy(namebuf, "/");
return;
}
init_inodesc(&idesc);
idesc.id_type = DATA;
cp = &namebuf[MAXPATHLEN - 1];
*cp = '\0';
/*
* In the case of extended attributes, our
* parent won't necessarily be a directory, so just
* return what we've found with a prefix indicating
* that it's an XATTR. Presumably our caller will
* know what's going on and do something useful, like
* work out the path of the parent and then combine
* the two names.
*
* Can't use strcpy(), etc, because we've probably
* already got some name information in the buffer and
* the usual trailing \0 would lose it.
*/
dp = ginode(curdir);
if ((dp->di_mode & IFMT) == IFATTRDIR) {
idesc.id_number = curdir;
idesc.id_parent = ino;
idesc.id_func = findname;
idesc.id_name = namebuf;
idesc.id_fix = NOFIX;
if ((ckinode(dp, &idesc, CKI_TRAVERSE) & FOUND) == 0) {
*cp-- = '?';
}
len = sizeof (XATTR_DIR_NAME) - 1;
cp -= len;
(void) memmove(cp, XATTR_DIR_NAME, len);
goto attrname;
}
/*
* If curdir == ino, need to get a handle on .. so we
* can search it for ino's name. Otherwise, just search
* the given directory for ino. Repeat until out of space
* or a full path has been built.
*/
if (curdir != ino) {
idesc.id_parent = curdir;
goto namelookup;
}
while (ino != UFSROOTINO && ino != 0) {
idesc.id_number = ino;
idesc.id_func = findino;
idesc.id_name = "..";
idesc.id_fix = NOFIX;
if ((ckinode(ginode(ino), &idesc, CKI_TRAVERSE) & FOUND) == 0) {
inp = getinoinfo(ino);
if ((inp == NULL) || (inp->i_parent == 0)) {
break;
}
idesc.id_parent = inp->i_parent;
}
/*
* To get this far, id_parent must have the inode
* number for `..' in it. By definition, that's got
* to be a directory, so search it for the inode of
* interest.
*/
namelookup:
idesc.id_number = idesc.id_parent;
idesc.id_parent = ino;
idesc.id_func = findname;
idesc.id_name = namebuf;
idesc.id_fix = NOFIX;
if ((ckinode(ginode(idesc.id_number),
&idesc, CKI_TRAVERSE) & FOUND) == 0) {
break;
}
/*
* Prepend to what we've accumulated so far. If
* there's not enough room for even one more path element
* (of the worst-case length), then bail out.
*/
len = strlen(namebuf);
cp -= len;
if (cp < &namebuf[MAXNAMLEN])
break;
(void) memmove(cp, namebuf, len);
*--cp = '/';
/*
* Corner case for a looped-to-itself directory.
*/
if (ino == idesc.id_number)
break;
/*
* Climb one level of the hierarchy. In other words,
* the current .. becomes the inode to search for and
* its parent becomes the directory to search in.
*/
ino = idesc.id_number;
}
/*
* If we hit a discontinuity in the hierarchy, indicate it by
* prefixing the path so far with `?'. Otherwise, the first
* character will be `/' as a side-effect of the *--cp above.
*
* The special case is to handle the situation where we're
* trying to look something up in UFSROOTINO, but didn't find
* it.
*/
if (ino != UFSROOTINO || cp == &namebuf[MAXPATHLEN - 1]) {
if (cp > namebuf)
cp--;
*cp = '?';
}
/*
* The invariants being used for buffer integrity are:
* - namebuf[] is terminated with \0 before anything else
* - cp is always <= the last element of namebuf[]
* - the new path element is always stored at the
* beginning of namebuf[], and is no more than MAXNAMLEN-1
* characters
* - cp is is decremented by the number of characters in
* the new path element
* - if, after the above accounting for the new element's
* size, there is no longer enough room at the beginning of
* namebuf[] for a full-sized path element and a slash,
* terminate the loop. cp is in the range
* &namebuf[0]..&namebuf[MAXNAMLEN - 1]
*/
attrname:
/* LINTED per the above discussion */
(void) memmove(namebuf, cp, &namebuf[MAXPATHLEN] - cp);
}
/* ARGSUSED */
void
catch(int dummy)
{
ckfini();
exit(EXSIGNAL);
}
/*
* When preening, allow a single quit to signal
* a special exit after filesystem checks complete
* so that reboot sequence may be interrupted.
*/
/* ARGSUSED */
void
catchquit(int dummy)
{
(void) printf("returning to single-user after filesystem check\n");
interrupted = 1;
(void) signal(SIGQUIT, SIG_DFL);
}
/*
* determine whether an inode should be fixed.
*/
NOTE(PRINTFLIKE(2))
int
dofix(struct inodesc *idesc, caddr_t msg, ...)
{
int rval = 0;
va_list ap;
va_start(ap, msg);
switch (idesc->id_fix) {
case DONTKNOW:
if (idesc->id_type == DATA)
vdirerror(idesc->id_number, msg, ap);
else
vpwarn(msg, ap);
if (preen) {
idesc->id_fix = FIX;
rval = ALTERED;
break;
}
if (reply("SALVAGE") == 0) {
idesc->id_fix = NOFIX;
break;
}
idesc->id_fix = FIX;
rval = ALTERED;
break;
case FIX:
rval = ALTERED;
break;
case NOFIX:
break;
default:
errexit("UNKNOWN INODESC FIX MODE %d\n", (int)idesc->id_fix);
}
va_end(ap);
return (rval);
}
NOTE(PRINTFLIKE(1))
void
errexit(caddr_t fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verrexit(fmt, ap);
/* NOTREACHED */
}
NOTE(PRINTFLIKE(1))
static void
verrexit(caddr_t fmt, va_list ap)
{
static int recursing = 0;
if (!recursing) {
recursing = 1;
if (errorlocked || iscorrupt) {
if (havesb && fswritefd >= 0) {
sblock.fs_clean = FSBAD;
sblock.fs_state = FSOKAY - (long)sblock.fs_time;
sblock.fs_state = -sblock.fs_state;
sbdirty();
write_altsb(fswritefd);
flush(fswritefd, &sblk);
}
}
ckfini();
recursing = 0;
}
(void) vprintf(fmt, ap);
if (fmt[strlen(fmt) - 1] != '\n')
(void) putchar('\n');
exit((exitstat != 0) ? exitstat : EXERRFATAL);
}
/*
* An unexpected inconsistency occured.
* Die if preening, otherwise just print message and continue.
*/
NOTE(PRINTFLIKE(1))
void
pfatal(caddr_t fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vpfatal(fmt, ap);
va_end(ap);
}
NOTE(PRINTFLIKE(1))
static void
vpfatal(caddr_t fmt, va_list ap)
{
if (preen) {
if (*fmt != '\0') {
(void) printf("%s: ", devname);
(void) vprintf(fmt, ap);
(void) printf("\n");
}
(void) printf(
"%s: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.\n",
devname);
if (havesb && fswritefd >= 0) {
sblock.fs_clean = FSBAD;
sblock.fs_state = -(FSOKAY - (long)sblock.fs_time);
sbdirty();
flush(fswritefd, &sblk);
}
/*
* We're exiting, it doesn't really matter that our
* caller doesn't get to call va_end().
*/
if (exitstat == 0)
exitstat = EXFNDERRS;
exit(exitstat);
}
if (*fmt != '\0') {
(void) vprintf(fmt, ap);
}
}
/*
* Pwarn just prints a message when not preening,
* or a warning (preceded by filename) when preening.
*/
NOTE(PRINTFLIKE(1))
void
pwarn(caddr_t fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vpwarn(fmt, ap);
va_end(ap);
}
NOTE(PRINTFLIKE(1))
static void
vpwarn(caddr_t fmt, va_list ap)
{
if (*fmt != '\0') {
if (preen)
(void) printf("%s: ", devname);
(void) vprintf(fmt, ap);
}
}
/*
* Like sprintf(), except the buffer is dynamically allocated
* and returned, instead of being passed in. A pointer to the
* buffer is stored in *RET, and FMT is the usual format string.
* The number of characters in *RET (excluding the trailing \0,
* to be consistent with the other *printf() routines) is returned.
*
* Solaris doesn't have asprintf(3C) yet, unfortunately.
*/
NOTE(PRINTFLIKE(2))
int
fsck_asprintf(caddr_t *ret, caddr_t fmt, ...)
{
int len;
caddr_t buffer;
va_list ap;
va_start(ap, fmt);
len = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
buffer = malloc((len + 1) * sizeof (char));
if (buffer == NULL) {
errexit("Out of memory in asprintf\n");
/* NOTREACHED */
}
va_start(ap, fmt);
(void) vsnprintf(buffer, len + 1, fmt, ap);
va_end(ap);
*ret = buffer;
return (len);
}
/*
* So we can take advantage of kernel routines in ufs_subr.c.
*/
/* PRINTFLIKE2 */
void
cmn_err(int level, caddr_t fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (level == CE_PANIC) {
(void) printf("INTERNAL INCONSISTENCY:");
verrexit(fmt, ap);
} else {
(void) vprintf(fmt, ap);
}
va_end(ap);
}
/*
* Check to see if unraw version of name is already mounted.
* Updates devstr with the device name if devstr is not NULL
* and str_size is positive.
*/
int
mounted(caddr_t name, caddr_t devstr, size_t str_size)
{
int found;
struct mnttab *mntent;
mntent = search_mnttab(NULL, unrawname(name), devstr, str_size);
if (mntent == NULL)
return (M_NOMNT);
/*
* It's mounted. With or without write access?
*/
if (hasmntopt(mntent, MNTOPT_RO) != 0)
found = M_RO; /* mounted as RO */
else
found = M_RW; /* mounted as R/W */
if (mount_point == NULL) {
mount_point = strdup(mntent->mnt_mountp);
if (mount_point == NULL) {
errexit("fsck: memory allocation failure: %s",
strerror(errno));
/* NOTREACHED */
}
if (devstr != NULL && str_size > 0)
(void) strlcpy(devstr, mntent->mnt_special, str_size);
}
return (found);
}
/*
* Check to see if name corresponds to an entry in vfstab, and that the entry
* does not have option ro.
*/
int
writable(caddr_t name)
{
int rw = 1;
struct vfstab vfsbuf, vfskey;
FILE *vfstab;
vfstab = fopen(VFSTAB, "r");
if (vfstab == NULL) {
(void) printf("can't open %s\n", VFSTAB);
return (1);
}
(void) memset((void *)&vfskey, 0, sizeof (vfskey));
vfsnull(&vfskey);
vfskey.vfs_special = unrawname(name);
vfskey.vfs_fstype = MNTTYPE_UFS;
if ((getvfsany(vfstab, &vfsbuf, &vfskey) == 0) &&
(hasvfsopt(&vfsbuf, MNTOPT_RO))) {
rw = 0;
}
(void) fclose(vfstab);
return (rw);
}
/*
* debugclean
*/
static void
debugclean(void)
{
if (!debug)
return;
if ((iscorrupt == 0) && (isdirty == 0))
return;
if ((sblock.fs_clean == FSSTABLE) || (sblock.fs_clean == FSCLEAN) ||
(sblock.fs_clean == FSLOG && islog && islogok) ||
((FSOKAY == (sblock.fs_state + sblock.fs_time)) && !errorlocked))
return;
(void) printf("WARNING: inconsistencies detected on %s filesystem %s\n",
sblock.fs_clean == FSSTABLE ? "stable" :
sblock.fs_clean == FSLOG ? "logging" :
sblock.fs_clean == FSFIX ? "being fixed" : "clean",
devname);
}
/*
* updateclean
* Carefully and transparently update the clean flag.
*
* `iscorrupt' has to be in its final state before this is called.
*/
int
updateclean(void)
{
int freedlog = 0;
struct bufarea cleanbuf;
size_t size;
ssize_t io_res;
diskaddr_t bno;
char fsclean;
int fsreclaim;
char fsflags;
int flags_ok = 1;
daddr32_t fslogbno;
offset_t sblkoff;
time_t t;
/*
* debug stuff
*/
debugclean();
/*
* set fsclean to its appropriate value
*/
fslogbno = sblock.fs_logbno;
fsclean = sblock.fs_clean;
fsreclaim = sblock.fs_reclaim;
fsflags = sblock.fs_flags;
if (FSOKAY != (sblock.fs_state + sblock.fs_time) && !errorlocked) {
fsclean = FSACTIVE;
}
/*
* If ufs log is not okay, note that we need to clear it.
*/
examinelog(NULL);
if (fslogbno && !(islog && islogok)) {
fsclean = FSACTIVE;
fslogbno = 0;
}
/*
* if necessary, update fs_clean and fs_state
*/
switch (fsclean) {
case FSACTIVE:
if (!iscorrupt) {
fsclean = FSSTABLE;
fsreclaim = 0;
}
break;
case FSCLEAN:
case FSSTABLE:
if (iscorrupt) {
fsclean = FSACTIVE;
} else {
fsreclaim = 0;
}
break;
case FSLOG:
if (iscorrupt) {
fsclean = FSACTIVE;
} else if (!islog || fslogbno == 0) {
fsclean = FSSTABLE;
fsreclaim = 0;
} else if (fflag) {
fsreclaim = 0;
}
break;
case FSFIX:
fsclean = FSBAD;
if (errorlocked && !iscorrupt) {
fsclean = islog ? FSLOG : FSCLEAN;
}
break;
default:
if (iscorrupt) {
fsclean = FSACTIVE;
} else {
fsclean = FSSTABLE;
fsreclaim = 0;
}
}
if (largefile_count > 0)
fsflags |= FSLARGEFILES;
else
fsflags &= ~FSLARGEFILES;
/*
* There can be two discrepencies here. A) The superblock
* shows no largefiles but we found some while scanning.
* B) The superblock indicates the presence of largefiles,
* but none are present. Note that if preening, the superblock
* is silently corrected.
*/
if ((fsflags == FSLARGEFILES && sblock.fs_flags != FSLARGEFILES) ||
(fsflags != FSLARGEFILES && sblock.fs_flags == FSLARGEFILES))
flags_ok = 0;
if (debug)
(void) printf(
"** largefile count=%d, fs.fs_flags=%x, flags_ok %d\n",
largefile_count, sblock.fs_flags, flags_ok);
/*
* If fs is unchanged, do nothing.
*/
if ((!isdirty) && (flags_ok) &&
(fslogbno == sblock.fs_logbno) &&
(sblock.fs_clean == fsclean) &&
(sblock.fs_reclaim == fsreclaim) &&
(FSOKAY == (sblock.fs_state + sblock.fs_time))) {
if (errorlocked) {
if (!do_errorlock(LOCKFS_ULOCK))
pwarn(
"updateclean(unchanged): unlock(LOCKFS_ULOCK) failed\n");
}
return (freedlog);
}
/*
* if user allows, update superblock state
*/
if (debug) {
(void) printf(
"superblock: flags 0x%x logbno %d clean %d reclaim %d state 0x%x\n",
sblock.fs_flags, sblock.fs_logbno,
sblock.fs_clean, sblock.fs_reclaim,
sblock.fs_state + sblock.fs_time);
(void) printf(
"calculated: flags 0x%x logbno %d clean %d reclaim %d state 0x%x\n",
fsflags, fslogbno, fsclean, fsreclaim, FSOKAY);
}
if (!isdirty && !preen && !rerun &&
(reply("FILE SYSTEM STATE IN SUPERBLOCK IS WRONG; FIX") == 0))
return (freedlog);
(void) time(&t);
sblock.fs_time = (time32_t)t;
if (debug)
printclean();
if (sblock.fs_logbno != fslogbno) {
examinelog(&freelogblk);
freedlog++;
}
sblock.fs_logbno = fslogbno;
sblock.fs_clean = fsclean;
sblock.fs_state = FSOKAY - (long)sblock.fs_time;
sblock.fs_reclaim = fsreclaim;
sblock.fs_flags = fsflags;
/*
* if superblock can't be written, return
*/
if (fswritefd < 0)
return (freedlog);
/*
* Read private copy of superblock, update clean flag, and write it.
*/
bno = sblk.b_bno;
size = sblk.b_size;
sblkoff = ldbtob(bno);
if ((cleanbuf.b_un.b_buf = malloc(size)) == NULL)
errexit("out of memory");
if (llseek(fsreadfd, sblkoff, SEEK_SET) == -1) {
(void) printf("COULD NOT SEEK TO SUPERBLOCK AT %lld: %s\n",
(longlong_t)bno, strerror(errno));
goto out;
}
if ((io_res = read(fsreadfd, cleanbuf.b_un.b_buf, size)) != size) {
report_io_prob("READ FROM", bno, size, io_res);
goto out;
}
cleanbuf.b_un.b_fs->fs_logbno = sblock.fs_logbno;
cleanbuf.b_un.b_fs->fs_clean = sblock.fs_clean;
cleanbuf.b_un.b_fs->fs_state = sblock.fs_state;
cleanbuf.b_un.b_fs->fs_time = sblock.fs_time;
cleanbuf.b_un.b_fs->fs_reclaim = sblock.fs_reclaim;
cleanbuf.b_un.b_fs->fs_flags = sblock.fs_flags;
if (llseek(fswritefd, sblkoff, SEEK_SET) == -1) {
(void) printf("COULD NOT SEEK TO SUPERBLOCK AT %lld: %s\n",
(longlong_t)bno, strerror(errno));
goto out;
}
if ((io_res = write(fswritefd, cleanbuf.b_un.b_buf, size)) != size) {
report_io_prob("WRITE TO", bno, size, io_res);
goto out;
}
/*
* 1208040
* If we had to use -b to grab an alternate superblock, then we
* likely had to do so because of unacceptable differences between
* the main and alternate superblocks. So, we had better update
* the alternate superblock as well, or we'll just fail again
* the next time we attempt to run fsck!
*/
if (bflag != 0) {
write_altsb(fswritefd);
}
if (errorlocked) {
if (!do_errorlock(LOCKFS_ULOCK))
pwarn(
"updateclean(changed): unlock(LOCKFS_ULOCK) failed\n");
}
out:
if (cleanbuf.b_un.b_buf != NULL) {
free((void *)cleanbuf.b_un.b_buf);
}
return (freedlog);
}
static void
report_io_prob(caddr_t what, diskaddr_t bno, size_t expected, ssize_t failure)
{
if (failure < 0)
(void) printf("COULD NOT %s SUPERBLOCK AT %d: %s\n",
what, (int)bno, strerror(errno));
else if (failure == 0)
(void) printf("COULD NOT %s SUPERBLOCK AT %d: EOF\n",
what, (int)bno);
else
(void) printf("SHORT %s SUPERBLOCK AT %d: %u out of %u bytes\n",
what, (int)bno, (unsigned)failure, (unsigned)expected);
}
/*
* print out clean info
*/
void
printclean(void)
{
caddr_t s;
if (FSOKAY != (sblock.fs_state + sblock.fs_time) && !errorlocked)
s = "unknown";
else
switch (sblock.fs_clean) {
case FSACTIVE:
s = "active";
break;
case FSCLEAN:
s = "clean";
break;
case FSSTABLE:
s = "stable";
break;
case FSLOG:
s = "logging";
break;
case FSBAD:
s = "is bad";
break;
case FSFIX:
s = "being fixed";
break;
default:
s = "unknown";
}
if (preen)
pwarn("is %s.\n", s);
else
(void) printf("** %s is %s.\n", devname, s);
}
int
is_errorlocked(caddr_t fs)
{
int retval;
struct stat64 statb;
caddr_t mountp;
struct mnttab *mntent;
retval = 0;
if (!fs)
return (0);
if (stat64(fs, &statb) < 0)
return (0);
if (S_ISDIR(statb.st_mode)) {
mountp = fs;
} else if (S_ISBLK(statb.st_mode) || S_ISCHR(statb.st_mode)) {
mntent = search_mnttab(NULL, fs, NULL, 0);
if (mntent == NULL)
return (0);
mountp = mntent->mnt_mountp;
if (mountp == NULL) /* theoretically a can't-happen */
return (0);
} else {
return (0);
}
/*
* From here on, must `goto out' to avoid memory leakage.
*/
if (elock_combuf == NULL)
elock_combuf =
(caddr_t)calloc(LOCKFS_MAXCOMMENTLEN, sizeof (char));
else
elock_combuf =
(caddr_t)realloc(elock_combuf, LOCKFS_MAXCOMMENTLEN);
if (elock_combuf == NULL)
goto out;
(void) memset((void *)elock_combuf, 0, LOCKFS_MAXCOMMENTLEN);
if (elock_mountp != NULL) {
free(elock_mountp);
}
elock_mountp = strdup(mountp);
if (elock_mountp == NULL)
goto out;
if (mountfd < 0) {
if ((mountfd = open64(mountp, O_RDONLY)) == -1)
goto out;
}
if (lfp == NULL) {
lfp = (struct lockfs *)malloc(sizeof (struct lockfs));
if (lfp == NULL)
goto out;
(void) memset((void *)lfp, 0, sizeof (struct lockfs));
}
lfp->lf_comlen = LOCKFS_MAXCOMMENTLEN;
lfp->lf_comment = elock_combuf;
if (ioctl(mountfd, _FIOLFSS, lfp) == -1)
goto out;
/*
* lint believes that the ioctl() (or any other function
* taking lfp as an arg) could free lfp. This is not the
* case, however.
*/
retval = LOCKFS_IS_ELOCK(lfp);
out:
return (retval);
}
/*
* Given a name which is known to be a directory, see if it appears
* in the vfstab. If so, return the entry's block (special) device
* field via devstr.
*/
int
check_vfstab(caddr_t name, caddr_t devstr, size_t str_size)
{
return (NULL != search_vfstab(name, NULL, devstr, str_size));
}
/*
* Given a name which is known to be a directory, see if it appears
* in the mnttab. If so, return the entry's block (special) device
* field via devstr.
*/
int
check_mnttab(caddr_t name, caddr_t devstr, size_t str_size)
{
return (NULL != search_mnttab(name, NULL, devstr, str_size));
}
/*
* Search for mount point and/or special device in the given file.
* The first matching entry is returned.
*
* If an entry is found and str_size is greater than zero, then
* up to size_str bytes of the special device name from the entry
* are copied to devstr.
*/
#define SEARCH_TAB_BODY(st_type, st_file, st_mount, st_special, \
st_nuller, st_init, st_searcher) \
{ \
FILE *fp; \
struct st_type *retval = NULL; \
struct st_type key; \
static struct st_type buffer; \
\
/* LINTED ``assigned value never used'' */ \
st_nuller(&key); \
key.st_mount = mountp; \
key.st_special = special; \
st_init; \
\
if ((fp = fopen(st_file, "r")) == NULL) \
return (NULL); \
\
if (st_searcher(fp, &buffer, &key) == 0) { \
retval = &buffer; \
if (devstr != NULL && str_size > 0 && \
buffer.st_special != NULL) { \
(void) strlcpy(devstr, buffer.st_special, \
str_size); \
} \
} \
(void) fclose(fp); \
return (retval); \
}
static struct vfstab *
search_vfstab(caddr_t mountp, caddr_t special, caddr_t devstr, size_t str_size)
SEARCH_TAB_BODY(vfstab, VFSTAB, vfs_mountp, vfs_special, vfsnull,
(retval = retval), getvfsany)
static struct mnttab *
search_mnttab(caddr_t mountp, caddr_t special, caddr_t devstr, size_t str_size)
SEARCH_TAB_BODY(mnttab, MNTTAB, mnt_mountp, mnt_special, mntnull,
(key.mnt_fstype = MNTTYPE_UFS), getmntany)
int
do_errorlock(int lock_type)
{
caddr_t buf;
time_t now;
struct tm *local;
int rc;
if (elock_combuf == NULL)
errexit("do_errorlock(%s, %d): unallocated elock_combuf\n",
elock_mountp ? elock_mountp : "<null>",
lock_type);
if ((buf = (caddr_t)calloc(LOCKFS_MAXCOMMENTLEN, sizeof (char))) ==
NULL) {
errexit("Couldn't alloc memory for temp. lock status buffer\n");
}
if (lfp == NULL) {
errexit("do_errorlock(%s, %d): lockfs status unallocated\n",
elock_mountp, lock_type);
}
(void) memmove((void *)buf, (void *)elock_combuf,
LOCKFS_MAXCOMMENTLEN-1);
switch (lock_type) {
case LOCKFS_ELOCK:
/*
* Note that if it is error-locked, we won't get an
* error back if we try to error-lock it again.
*/
if (time(&now) != (time_t)-1) {
if ((local = localtime(&now)) != NULL)
(void) snprintf(buf, LOCKFS_MAXCOMMENTLEN,
"%s [pid:%d fsck start:%02d/%02d/%02d %02d:%02d:%02d",
elock_combuf, (int)pid,
local->tm_mon + 1, local->tm_mday,
(local->tm_year % 100), local->tm_hour,
local->tm_min, local->tm_sec);
else
(void) snprintf(buf, LOCKFS_MAXCOMMENTLEN,
"%s [fsck pid %d", elock_combuf, pid);
} else {
(void) snprintf(buf, LOCKFS_MAXCOMMENTLEN,
"%s [fsck pid %d", elock_combuf, pid);
}
break;
case LOCKFS_ULOCK:
if (time(&now) != (time_t)-1) {
if ((local = localtime(&now)) != NULL) {
(void) snprintf(buf, LOCKFS_MAXCOMMENTLEN,
"%s, done:%02d/%02d/%02d %02d:%02d:%02d]",
elock_combuf,
local->tm_mon + 1, local->tm_mday,
(local->tm_year % 100), local->tm_hour,
local->tm_min, local->tm_sec);
} else {
(void) snprintf(buf, LOCKFS_MAXCOMMENTLEN,
"%s]", elock_combuf);
}
} else {
(void) snprintf(buf, LOCKFS_MAXCOMMENTLEN,
"%s]", elock_combuf);
}
if ((rc = ioctl(mountfd, _FIOLFSS, lfp)) == -1) {
pwarn("do_errorlock: unlock failed: %s\n",
strerror(errno));
goto out;
}
break;
default:
break;
}
(void) memmove((void *)elock_combuf, (void *)buf,
LOCKFS_MAXCOMMENTLEN - 1);
lfp->lf_lock = lock_type;
lfp->lf_comlen = LOCKFS_MAXCOMMENTLEN;
lfp->lf_comment = elock_combuf;
lfp->lf_flags = 0;
errno = 0;
if ((rc = ioctl(mountfd, _FIOLFS, lfp)) == -1) {
if (errno == EINVAL) {
pwarn("Another fsck active?\n");
iscorrupt = 0; /* don't go away mad, just go away */
} else {
pwarn("do_errorlock(lock_type:%d, %s) failed: %s\n",
lock_type, elock_combuf, strerror(errno));
}
}
out:
if (buf != NULL) {
free((void *)buf);
}
return (rc != -1);
}
/*
* Shadow inode support. To register a shadow with a client is to note
* that an inode (the client) refers to the shadow.
*/
static struct shadowclients *
newshadowclient(struct shadowclients *prev)
{
struct shadowclients *rc;
rc = (struct shadowclients *)malloc(sizeof (*rc));
if (rc == NULL)
errexit("newshadowclient: cannot malloc shadow client");
rc->next = prev;
rc->nclients = 0;
rc->client = (fsck_ino_t *)malloc(sizeof (fsck_ino_t) *
maxshadowclients);
if (rc->client == NULL)
errexit("newshadowclient: cannot malloc client array");
return (rc);
}
void
registershadowclient(fsck_ino_t shadow, fsck_ino_t client,
struct shadowclientinfo **info)
{
struct shadowclientinfo *sci;
struct shadowclients *scc;
/*
* Already have a record for this shadow?
*/
for (sci = *info; sci != NULL; sci = sci->next)
if (sci->shadow == shadow)
break;
if (sci == NULL) {
/*
* It's a new shadow, add it to the list
*/
sci = (struct shadowclientinfo *)malloc(sizeof (*sci));
if (sci == NULL)
errexit("registershadowclient: cannot malloc");
sci->next = *info;
*info = sci;
sci->shadow = shadow;
sci->totalClients = 0;
sci->clients = newshadowclient(NULL);
}
sci->totalClients++;
scc = sci->clients;
if (scc->nclients >= maxshadowclients) {
scc = newshadowclient(sci->clients);
sci->clients = scc;
}
scc->client[scc->nclients++] = client;
}
/*
* Locate and discard a shadow.
*/
void
clearshadow(fsck_ino_t shadow, struct shadowclientinfo **info)
{
struct shadowclientinfo *sci, *prev;
/*
* Do we have a record for this shadow?
*/
prev = NULL;
for (sci = *info; sci != NULL; sci = sci->next) {
if (sci->shadow == shadow)
break;
prev = sci;
}
if (sci != NULL) {
/*
* First, pull it off the list, since we know there
* shouldn't be any future references to this one.
*/
if (prev == NULL)
*info = sci->next;
else
prev->next = sci->next;
deshadow(sci, clearattrref);
}
}
/*
* Discard all memory used to track clients of a shadow.
*/
void
deshadow(struct shadowclientinfo *sci, void (*cb)(fsck_ino_t))
{
struct shadowclients *clients, *discard;
int idx;
clients = sci->clients;
while (clients != NULL) {
discard = clients;
clients = clients->next;
if (discard->client != NULL) {
if (cb != NULL) {
for (idx = 0; idx < discard->nclients; idx++)
(*cb)(discard->client[idx]);
}
free((void *)discard->client);
}
free((void *)discard);
}
free((void *)sci);
}
/*
* Allocate more buffer as need arises but allocate one at a time.
* This is done to make sure that fsck does not exit with error if it
* needs more buffer to complete its task.
*/
static struct bufarea *
alloc_bufarea(void)
{
struct bufarea *newbp;
caddr_t bufp;
bufp = malloc((unsigned int)sblock.fs_bsize);
if (bufp == NULL)
return (NULL);
newbp = (struct bufarea *)malloc(sizeof (struct bufarea));
if (newbp == NULL) {
free((void *)bufp);
return (NULL);
}
initbarea(newbp);
newbp->b_un.b_buf = bufp;
newbp->b_prev = &bufhead;
newbp->b_next = bufhead.b_next;
bufhead.b_next->b_prev = newbp;
bufhead.b_next = newbp;
bufhead.b_size++;
return (newbp);
}
/*
* We length-limit in both unrawname() and rawname() to avoid
* overflowing our arrays or those of our naive, trusting callers.
*/
caddr_t
unrawname(caddr_t name)
{
caddr_t dp;
static char fullname[MAXPATHLEN + 1];
if ((dp = getfullblkname(name)) == NULL)
return ("");
(void) strlcpy(fullname, dp, sizeof (fullname));
/*
* Not reporting under debug, as the allocation isn't
* reported by getfullblkname. The idea is that we
* produce balanced alloc/free instances.
*/
free(dp);
return (fullname);
}
caddr_t
rawname(caddr_t name)
{
caddr_t dp;
static char fullname[MAXPATHLEN + 1];
if ((dp = getfullrawname(name)) == NULL)
return ("");
(void) strlcpy(fullname, dp, sizeof (fullname));
/*
* Not reporting under debug, as the allocation isn't
* reported by getfullblkname. The idea is that we
* produce balanced alloc/free instances.
*/
free(dp);
return (fullname);
}
/*
* Make sure that a cg header looks at least moderately reasonable.
* We want to be able to trust the contents enough to be able to use
* the standard accessor macros. So, besides looking at the obvious
* such as the magic number, we verify that the offset field values
* are properly aligned and not too big or small.
*
* Returns a NULL pointer if the cg is sane enough for our needs, else
* a dynamically-allocated string describing all of its faults.
*/
#define Append_Error(full, full_len, addition, addition_len) \
if (full == NULL) { \
full = addition; \
full_len = addition_len; \
} else { \
/* lint doesn't think realloc() understands NULLs */ \
full = realloc(full, full_len + addition_len + 1); \
if (full == NULL) { \
errexit("Out of memory in cg_sanity"); \
/* NOTREACHED */ \
} \
(void) strcpy(full + full_len, addition); \
full_len += addition_len; \
free(addition); \
}
caddr_t
cg_sanity(struct cg *cgp, int cgno)
{
caddr_t full_err;
caddr_t this_err = NULL;
int full_len, this_len;
daddr32_t ndblk;
daddr32_t exp_btotoff, exp_boff, exp_iusedoff;
daddr32_t exp_freeoff, exp_nextfreeoff;
cg_constants(cgno, &exp_btotoff, &exp_boff, &exp_iusedoff,
&exp_freeoff, &exp_nextfreeoff, &ndblk);
full_err = NULL;
full_len = 0;
if (!cg_chkmagic(cgp)) {
this_len = fsck_asprintf(&this_err,
"BAD CG MAGIC NUMBER (0x%x should be 0x%x)\n",
cgp->cg_magic, CG_MAGIC);
Append_Error(full_err, full_len, this_err, this_len);
}
if (cgp->cg_cgx != cgno) {
this_len = fsck_asprintf(&this_err,
"WRONG CG NUMBER (%d should be %d)\n",
cgp->cg_cgx, cgno);
Append_Error(full_err, full_len, this_err, this_len);
}
if ((cgp->cg_btotoff & 3) != 0) {
this_len = fsck_asprintf(&this_err,
"BLOCK TOTALS OFFSET %d NOT FOUR-BYTE ALIGNED\n",
cgp->cg_btotoff);
Append_Error(full_err, full_len, this_err, this_len);
}
if ((cgp->cg_boff & 1) != 0) {
this_len = fsck_asprintf(&this_err,
"FREE BLOCK POSITIONS TABLE OFFSET %d NOT TWO-BYTE ALIGNED\n",
cgp->cg_boff);
Append_Error(full_err, full_len, this_err, this_len);
}
if ((cgp->cg_ncyl < 1) || (cgp->cg_ncyl > sblock.fs_cpg)) {
if (cgp->cg_ncyl < 1) {
this_len = fsck_asprintf(&this_err,
"IMPOSSIBLE NUMBER OF CYLINDERS IN GROUP (%d is less than 1)\n",
cgp->cg_ncyl);
} else {
this_len = fsck_asprintf(&this_err,
"IMPOSSIBLE NUMBER OF CYLINDERS IN GROUP (%d is greater than %d)\n",
cgp->cg_ncyl, sblock.fs_cpg);
}
Append_Error(full_err, full_len, this_err, this_len);
}
if (cgp->cg_niblk != sblock.fs_ipg) {
this_len = fsck_asprintf(&this_err,
"INCORRECT NUMBER OF INODES IN GROUP (%d should be %d)\n",
cgp->cg_niblk, sblock.fs_ipg);
Append_Error(full_err, full_len, this_err, this_len);
}
if (cgp->cg_ndblk != ndblk) {
this_len = fsck_asprintf(&this_err,
"INCORRECT NUMBER OF DATA BLOCKS IN GROUP (%d should be %d)\n",
cgp->cg_ndblk, ndblk);
Append_Error(full_err, full_len, this_err, this_len);
}
if ((cgp->cg_rotor < 0) || (cgp->cg_rotor >= ndblk)) {
this_len = fsck_asprintf(&this_err,
"IMPOSSIBLE BLOCK ALLOCATION ROTOR POSITION "
"(%d should be at least 0 and less than %d)\n",
cgp->cg_rotor, ndblk);
Append_Error(full_err, full_len, this_err, this_len);
}
if ((cgp->cg_frotor < 0) || (cgp->cg_frotor >= ndblk)) {
this_len = fsck_asprintf(&this_err,
"IMPOSSIBLE FRAGMENT ALLOCATION ROTOR POSITION "
"(%d should be at least 0 and less than %d)\n",
cgp->cg_frotor, ndblk);
Append_Error(full_err, full_len, this_err, this_len);
}
if ((cgp->cg_irotor < 0) || (cgp->cg_irotor >= sblock.fs_ipg)) {
this_len = fsck_asprintf(&this_err,
"IMPOSSIBLE INODE ALLOCATION ROTOR POSITION "
"(%d should be at least 0 and less than %d)\n",
cgp->cg_irotor, sblock.fs_ipg);
Append_Error(full_err, full_len, this_err, this_len);
}
if (cgp->cg_btotoff != exp_btotoff) {
this_len = fsck_asprintf(&this_err,
"INCORRECT BLOCK TOTALS OFFSET (%d should be %d)\n",
cgp->cg_btotoff, exp_btotoff);
Append_Error(full_err, full_len, this_err, this_len);
}
if (cgp->cg_boff != exp_boff) {
this_len = fsck_asprintf(&this_err,
"BAD FREE BLOCK POSITIONS TABLE OFFSET (%d should %d)\n",
cgp->cg_boff, exp_boff);
Append_Error(full_err, full_len, this_err, this_len);
}
if (cgp->cg_iusedoff != exp_iusedoff) {
this_len = fsck_asprintf(&this_err,
"INCORRECT USED INODE MAP OFFSET (%d should be %d)\n",
cgp->cg_iusedoff, exp_iusedoff);
Append_Error(full_err, full_len, this_err, this_len);
}
if (cgp->cg_freeoff != exp_freeoff) {
this_len = fsck_asprintf(&this_err,
"INCORRECT FREE FRAGMENT MAP OFFSET (%d should be %d)\n",
cgp->cg_freeoff, exp_freeoff);
Append_Error(full_err, full_len, this_err, this_len);
}
if (cgp->cg_nextfreeoff != exp_nextfreeoff) {
this_len = fsck_asprintf(&this_err,
"END OF HEADER POSITION INCORRECT (%d should be %d)\n",
cgp->cg_nextfreeoff, exp_nextfreeoff);
Append_Error(full_err, full_len, this_err, this_len);
}
return (full_err);
}
#undef Append_Error
/*
* This is taken from mkfs, and is what is used to come up with the
* original values for a struct cg. This implies that, since these
* are all constants, recalculating them now should give us the same
* thing as what's on disk.
*/
static void
cg_constants(int cgno, daddr32_t *btotoff, daddr32_t *boff,
daddr32_t *iusedoff, daddr32_t *freeoff, daddr32_t *nextfreeoff,
daddr32_t *ndblk)
{
daddr32_t cbase, dmax;
struct cg *cgp;
(void) getblk(&cgblk, (diskaddr_t)cgtod(&sblock, cgno),
(size_t)sblock.fs_cgsize);
cgp = cgblk.b_un.b_cg;
cbase = cgbase(&sblock, cgno);
dmax = cbase + sblock.fs_fpg;
if (dmax > sblock.fs_size)
dmax = sblock.fs_size;
/* LINTED pointer difference won't overflow */
*btotoff = &cgp->cg_space[0] - (uchar_t *)(&cgp->cg_link);
*boff = *btotoff + sblock.fs_cpg * sizeof (daddr32_t);
*iusedoff = *boff + sblock.fs_cpg * sblock.fs_nrpos * sizeof (int16_t);
*freeoff = *iusedoff + howmany(sblock.fs_ipg, NBBY);
*nextfreeoff = *freeoff +
howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY);
*ndblk = dmax - cbase;
}
/*
* Corrects all fields in the cg that can be done with the available
* redundant data.
*/
void
fix_cg(struct cg *cgp, int cgno)
{
daddr32_t exp_btotoff, exp_boff, exp_iusedoff;
daddr32_t exp_freeoff, exp_nextfreeoff;
daddr32_t ndblk;
cg_constants(cgno, &exp_btotoff, &exp_boff, &exp_iusedoff,
&exp_freeoff, &exp_nextfreeoff, &ndblk);
if (cgp->cg_cgx != cgno) {
cgp->cg_cgx = cgno;
}
if ((cgp->cg_ncyl < 1) || (cgp->cg_ncyl > sblock.fs_cpg)) {
if (cgno == (sblock.fs_ncg - 1)) {
cgp->cg_ncyl = sblock.fs_ncyl -
(sblock.fs_cpg * cgno);
} else {
cgp->cg_ncyl = sblock.fs_cpg;
}
}
if (cgp->cg_niblk != sblock.fs_ipg) {
/*
* This is not used by the kernel, so it's pretty
* harmless if it's wrong.
*/
cgp->cg_niblk = sblock.fs_ipg;
}
if (cgp->cg_ndblk != ndblk) {
cgp->cg_ndblk = ndblk;
}
/*
* For the rotors, any position's valid, so pick the one we know
* will always exist.
*/
if ((cgp->cg_rotor < 0) || (cgp->cg_rotor >= cgp->cg_ndblk)) {
cgp->cg_rotor = 0;
}
if ((cgp->cg_frotor < 0) || (cgp->cg_frotor >= cgp->cg_ndblk)) {
cgp->cg_frotor = 0;
}
if ((cgp->cg_irotor < 0) || (cgp->cg_irotor >= sblock.fs_ipg)) {
cgp->cg_irotor = 0;
}
/*
* For btotoff and boff, if they're misaligned they won't
* match the expected values, so we're catching both cases
* here. Of course, if any of these are off, it seems likely
* that the tables really won't be where we calculate they
* should be anyway.
*/
if (cgp->cg_btotoff != exp_btotoff) {
cgp->cg_btotoff = exp_btotoff;
}
if (cgp->cg_boff != exp_boff) {
cgp->cg_boff = exp_boff;
}
if (cgp->cg_iusedoff != exp_iusedoff) {
cgp->cg_iusedoff = exp_iusedoff;
}
if (cgp->cg_freeoff != exp_freeoff) {
cgp->cg_freeoff = exp_freeoff;
}
if (cgp->cg_nextfreeoff != exp_nextfreeoff) {
cgp->cg_nextfreeoff = exp_nextfreeoff;
}
/*
* Reset the magic, as we've recreated this cg, also
* update the cg_time, as we're writing out the cg
*/
cgp->cg_magic = CG_MAGIC;
cgp->cg_time = time(NULL);
/*
* We know there was at least one correctable problem,
* or else we wouldn't have been called. So instead of
* marking the buffer dirty N times above, just do it
* once here.
*/
cgdirty();
}
void
examinelog(void (*cb)(daddr32_t))
{
struct bufarea *bp;
extent_block_t *ebp;
extent_t *ep;
daddr32_t nfno, fno;
int i;
int j;
/*
* Since ufs stores fs_logbno as blocks and MTBufs stores it as frags
* we need to translate accordingly using logbtodb()
*/
if (logbtodb(&sblock, sblock.fs_logbno) < SBLOCK) {
if (debug) {
(void) printf("fs_logbno < SBLOCK: %ld < %ld\n" \
"Aborting log examination\n", \
logbtodb(&sblock, sblock.fs_logbno), SBLOCK);
}
return;
}
/*
* Read errors will return zeros, which will cause us
* to do nothing harmful, so don't need to handle it.
*/
bp = getdatablk(logbtofrag(&sblock, sblock.fs_logbno),
(size_t)sblock.fs_bsize);
ebp = (void *)bp->b_un.b_buf;
/*
* Does it look like a log allocation table?
*/
/* LINTED pointer cast is aligned */
if (!log_checksum(&ebp->chksum, (int32_t *)bp->b_un.b_buf,
sblock.fs_bsize))
return;
if (ebp->type != LUFS_EXTENTS || ebp->nextents == 0)
return;
ep = &ebp->extents[0];
for (i = 0; i < ebp->nextents; ++i, ++ep) {
fno = logbtofrag(&sblock, ep->pbno);
nfno = dbtofsb(&sblock, ep->nbno);
for (j = 0; j < nfno; ++j, ++fno) {
/*
* Invoke the callback first, so that pass1 can
* mark the log blocks in-use. Then, if any
* subsequent pass over the log shows us that a
* block got freed (say, it was also claimed by
* an inode that we cleared), we can safely declare
* the log bad.
*/
if (cb != NULL)
(*cb)(fno);
if (!testbmap(fno))
islogok = 0;
}
}
brelse(bp);
if (cb != NULL) {
fno = logbtofrag(&sblock, sblock.fs_logbno);
for (j = 0; j < sblock.fs_frag; ++j, ++fno)
(*cb)(fno);
}
}
static void
freelogblk(daddr32_t frag)
{
freeblk(sblock.fs_logbno, frag, 1);
}
caddr_t
file_id(fsck_ino_t inum, mode_t mode)
{
static char name[MAXPATHLEN + 1];
if (lfdir == inum) {
return (lfname);
}
if ((mode & IFMT) == IFDIR) {
(void) strcpy(name, "DIR");
} else if ((mode & IFMT) == IFATTRDIR) {
(void) strcpy(name, "ATTR DIR");
} else if ((mode & IFMT) == IFSHAD) {
(void) strcpy(name, "ACL");
} else {
(void) strcpy(name, "FILE");
}
return (name);
}
/*
* Simple initializer for inodesc structures, so users of only a few
* fields don't have to worry about getting the right defaults for
* everything out.
*/
void
init_inodesc(struct inodesc *idesc)
{
/*
* Most fields should be zero, just hit the special cases.
*/
(void) memset((void *)idesc, 0, sizeof (struct inodesc));
idesc->id_fix = DONTKNOW;
idesc->id_lbn = -1;
idesc->id_truncto = -1;
idesc->id_firsthole = -1;
}
/*
* Compare routine for tsearch(C) to use on ino_t instances.
*/
int
ino_t_cmp(const void *left, const void *right)
{
const fsck_ino_t lino = (const fsck_ino_t)left;
const fsck_ino_t rino = (const fsck_ino_t)right;
return (lino - rino);
}
int
cgisdirty(void)
{
return (cgblk.b_dirty);
}
void
cgflush(void)
{
flush(fswritefd, &cgblk);
}
void
dirty(struct bufarea *bp)
{
if (fswritefd < 0) {
/*
* No one should call dirty() in read only mode.
* But if one does, it's not fatal issue. Just warn them.
*/
pwarn("WON'T SET DIRTY FLAG IN READ_ONLY MODE\n");
} else {
(bp)->b_dirty = 1;
isdirty = 1;
}
}
void
initbarea(struct bufarea *bp)
{
(bp)->b_dirty = 0;
(bp)->b_bno = (diskaddr_t)-1LL;
(bp)->b_flags = 0;
(bp)->b_cnt = 0;
(bp)->b_errs = 0;
}
/*
* Partition-sizing routines adapted from ../newfs/newfs.c.
* Needed because calcsb() needs to use mkfs to work out what the
* superblock should be, and mkfs insists on being told how many
* sectors to use.
*
* Error handling assumes we're never called while preening.
*
* XXX This should be extracted into a ../ufslib.{c,h},
* in the same spirit to ../../fslib.{c,h}. Once that is
* done, both fsck and newfs should be modified to link
* against it.
*/
static int label_type;
#define LABEL_TYPE_VTOC 1
#define LABEL_TYPE_EFI 2
#define LABEL_TYPE_OTHER 3
#define MB (1024 * 1024)
#define SECTORS_PER_TERABYTE (1LL << 31)
#define FS_SIZE_UPPER_LIMIT 0x100000000000LL
diskaddr_t
getdisksize(caddr_t disk, int fd)
{
int rpm;
struct dk_geom g;
struct dk_cinfo ci;
diskaddr_t actual_size;
/*
* get_device_size() determines the actual size of the
* device, and also the disk's attributes, such as geometry.
*/
actual_size = get_device_size(fd, disk);
if (label_type == LABEL_TYPE_VTOC) {
if (ioctl(fd, DKIOCGGEOM, &g)) {
pwarn("%s: Unable to read Disk geometry", disk);
return (0);
}
if (sblock.fs_nsect == 0)
sblock.fs_nsect = g.dkg_nsect;
if (sblock.fs_ntrak == 0)
sblock.fs_ntrak = g.dkg_nhead;
if (sblock.fs_rps == 0) {
rpm = ((int)g.dkg_rpm <= 0) ? 3600: g.dkg_rpm;
sblock.fs_rps = rpm / 60;
}
}
if (sblock.fs_bsize == 0)
sblock.fs_bsize = MAXBSIZE;
/*
* Adjust maxcontig by the device's maxtransfer. If maxtransfer
* information is not available, default to the min of a MB and
* maxphys.
*/
if (sblock.fs_maxcontig == -1 && ioctl(fd, DKIOCINFO, &ci) == 0) {
sblock.fs_maxcontig = ci.dki_maxtransfer * DEV_BSIZE;
if (sblock.fs_maxcontig < 0) {
int gotit, maxphys;
gotit = fsgetmaxphys(&maxphys, NULL);
/*
* If we cannot get the maxphys value, default
* to ufs_maxmaxphys (MB).
*/
if (gotit) {
sblock.fs_maxcontig = MIN(maxphys, MB);
} else {
sblock.fs_maxcontig = MB;
}
}
sblock.fs_maxcontig /= sblock.fs_bsize;
}
return (actual_size);
}
/*
* Figure out how big the partition we're dealing with is.
*/
static diskaddr_t
get_device_size(int fd, caddr_t name)
{
struct extvtoc vtoc;
struct dk_gpt *efi_vtoc;
diskaddr_t slicesize = 0;
int index = read_extvtoc(fd, &vtoc);
if (index >= 0) {
label_type = LABEL_TYPE_VTOC;
} else {
if (index == VT_ENOTSUP || index == VT_ERROR) {
/* it might be an EFI label */
index = efi_alloc_and_read(fd, &efi_vtoc);
if (index >= 0)
label_type = LABEL_TYPE_EFI;
}
}
if (index < 0) {
/*
* Since both attempts to read the label failed, we're
* going to fall back to a brute force approach to
* determining the device's size: see how far out we can
* perform reads on the device.
*/
slicesize = brute_force_get_device_size(fd);
if (slicesize == 0) {
switch (index) {
case VT_ERROR:
pwarn("%s: %s\n", name, strerror(errno));
break;
case VT_EIO:
pwarn("%s: I/O error accessing VTOC", name);
break;
case VT_EINVAL:
pwarn("%s: Invalid field in VTOC", name);
break;
default:
pwarn("%s: unknown error %d accessing VTOC",
name, index);
break;
}
return (0);
} else {
label_type = LABEL_TYPE_OTHER;
}
}
if (label_type == LABEL_TYPE_EFI) {
slicesize = efi_vtoc->efi_parts[index].p_size;
efi_free(efi_vtoc);
} else if (label_type == LABEL_TYPE_VTOC) {
slicesize = vtoc.v_part[index].p_size;
}
return (slicesize);
}
/*
* brute_force_get_device_size
*
* Determine the size of the device by seeing how far we can
* read. Doing an llseek( , , SEEK_END) would probably work
* in most cases, but we've seen at least one third-party driver
* which doesn't correctly support the SEEK_END option when the
* the device is greater than a terabyte.
*/
static diskaddr_t
brute_force_get_device_size(int fd)
{
diskaddr_t min_fail = 0;
diskaddr_t max_succeed = 0;
diskaddr_t cur_db_off;
char buf[DEV_BSIZE];
/*
* First, see if we can read the device at all, just to
* eliminate errors that have nothing to do with the
* device's size.
*/
if (((llseek(fd, (offset_t)0, SEEK_SET)) == -1) ||
((read(fd, buf, DEV_BSIZE)) == -1))
return (0); /* can't determine size */
/*
* Now, go sequentially through the multiples of 4TB
* to find the first read that fails (this isn't strictly
* the most efficient way to find the actual size if the
* size really could be anything between 0 and 2**64 bytes.
* We expect the sizes to be less than 16 TB for some time,
* so why do a bunch of reads that are larger than that?
* However, this algorithm *will* work for sizes of greater
* than 16 TB. We're just not optimizing for those sizes.)
*/
/*
* XXX lint uses 32-bit arithmetic for doing flow analysis.
* We're using > 32-bit constants here. Therefore, its flow
* analysis is wrong. For the time being, ignore complaints
* from it about the body of the for() being unreached.
*/
for (cur_db_off = SECTORS_PER_TERABYTE * 4;
(min_fail == 0) && (cur_db_off < FS_SIZE_UPPER_LIMIT);
cur_db_off += 4 * SECTORS_PER_TERABYTE) {
if ((llseek(fd, (offset_t)(cur_db_off * DEV_BSIZE),
SEEK_SET) == -1) ||
(read(fd, buf, DEV_BSIZE) != DEV_BSIZE))
min_fail = cur_db_off;
else
max_succeed = cur_db_off;
}
/*
* XXX Same lint flow analysis problem as above.
*/
if (min_fail == 0)
return (0);
/*
* We now know that the size of the device is less than
* min_fail and greater than or equal to max_succeed. Now
* keep splitting the difference until the actual size in
* sectors in known. We also know that the difference
* between max_succeed and min_fail at this time is
* 4 * SECTORS_PER_TERABYTE, which is a power of two, which
* simplifies the math below.
*/
while (min_fail - max_succeed > 1) {
cur_db_off = max_succeed + (min_fail - max_succeed)/2;
if (((llseek(fd, (offset_t)(cur_db_off * DEV_BSIZE),
SEEK_SET)) == -1) ||
((read(fd, buf, DEV_BSIZE)) != DEV_BSIZE))
min_fail = cur_db_off;
else
max_succeed = cur_db_off;
}
/* the size is the last successfully read sector offset plus one */
return (max_succeed + 1);
}
static void
vfileerror(fsck_ino_t cwd, fsck_ino_t ino, caddr_t fmt, va_list ap)
{
struct dinode *dp;
char pathbuf[MAXPATHLEN + 1];
vpwarn(fmt, ap);
(void) putchar(' ');
pinode(ino);
(void) printf("\n");
getpathname(pathbuf, cwd, ino);
if (ino < UFSROOTINO || ino > maxino) {
pfatal("NAME=%s\n", pathbuf);
return;
}
dp = ginode(ino);
if (ftypeok(dp))
pfatal("%s=%s\n", file_id(ino, dp->di_mode), pathbuf);
else
pfatal("NAME=%s\n", pathbuf);
}
void
direrror(fsck_ino_t ino, caddr_t fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfileerror(ino, ino, fmt, ap);
va_end(ap);
}
static void
vdirerror(fsck_ino_t ino, caddr_t fmt, va_list ap)
{
vfileerror(ino, ino, fmt, ap);
}
void
fileerror(fsck_ino_t cwd, fsck_ino_t ino, caddr_t fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfileerror(cwd, ino, fmt, ap);
va_end(ap);
}
/*
* Adds the given inode to the orphaned-directories list, limbo_dirs.
* Assumes that the caller has set INCLEAR in the inode's statemap[]
* entry.
*
* With INCLEAR set, the inode will get ignored by passes 2 and 3,
* meaning it's effectively an orphan. It needs to be noted now, so
* it will be remembered in pass 4.
*/
void
add_orphan_dir(fsck_ino_t ino)
{
if (tsearch((void *)ino, &limbo_dirs, ino_t_cmp) == NULL)
errexit("add_orphan_dir: out of memory");
}
/*
* Remove an inode from the orphaned-directories list, presumably
* because it's been cleared.
*/
void
remove_orphan_dir(fsck_ino_t ino)
{
(void) tdelete((void *)ino, &limbo_dirs, ino_t_cmp);
}
/*
* log_setsum() and log_checksum() are equivalent to lufs.c:setsum()
* and lufs.c:checksum().
*/
static void
log_setsum(int32_t *sp, int32_t *lp, int nb)
{
int32_t csum = 0;
*sp = 0;
nb /= sizeof (int32_t);
while (nb--)
csum += *lp++;
*sp = csum;
}
static int
log_checksum(int32_t *sp, int32_t *lp, int nb)
{
int32_t ssum = *sp;
log_setsum(sp, lp, nb);
if (ssum != *sp) {
*sp = ssum;
return (0);
}
return (1);
}
|