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
|
/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* The objective of this program is to provide a DMU/ZAP/SPA stress test
* that runs entirely in userland, is easy to use, and easy to extend.
*
* The overall design of the ztest program is as follows:
*
* (1) For each major functional area (e.g. adding vdevs to a pool,
* creating and destroying datasets, reading and writing objects, etc)
* we have a simple routine to test that functionality. These
* individual routines do not have to do anything "stressful".
*
* (2) We turn these simple functionality tests into a stress test by
* running them all in parallel, with as many threads as desired,
* and spread across as many datasets, objects, and vdevs as desired.
*
* (3) While all this is happening, we inject faults into the pool to
* verify that self-healing data really works.
*
* (4) Every time we open a dataset, we change its checksum and compression
* functions. Thus even individual objects vary from block to block
* in which checksum they use and whether they're compressed.
*
* (5) To verify that we never lose on-disk consistency after a crash,
* we run the entire test in a child of the main process.
* At random times, the child self-immolates with a SIGKILL.
* This is the software equivalent of pulling the power cord.
* The parent then runs the test again, using the existing
* storage pool, as many times as desired.
*
* (6) To verify that we don't have future leaks or temporal incursions,
* many of the functional tests record the transaction group number
* as part of their data. When reading old data, they verify that
* the transaction group number is less than the current, open txg.
* If you add a new test, please do this if applicable.
*
* When run with no arguments, ztest runs for about five minutes and
* produces no output if successful. To get a little bit of information,
* specify -V. To get more information, specify -VV, and so on.
*
* To turn this into an overnight stress test, use -T to specify run time.
*
* You can ask more more vdevs [-v], datasets [-d], or threads [-t]
* to increase the pool capacity, fanout, and overall stress level.
*
* The -N(okill) option will suppress kills, so each child runs to completion.
* This can be useful when you're trying to distinguish temporal incursions
* from plain old race conditions.
*/
#include <sys/zfs_context.h>
#include <sys/spa.h>
#include <sys/dmu.h>
#include <sys/txg.h>
#include <sys/dbuf.h>
#include <sys/zap.h>
#include <sys/dmu_objset.h>
#include <sys/poll.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/zio.h>
#include <sys/zio_checksum.h>
#include <sys/zio_compress.h>
#include <sys/zil.h>
#include <sys/vdev_impl.h>
#include <sys/vdev_file.h>
#include <sys/spa_impl.h>
#include <sys/dsl_prop.h>
#include <sys/dsl_dataset.h>
#include <sys/refcount.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <umem.h>
#include <dlfcn.h>
#include <ctype.h>
#include <math.h>
#include <sys/fs/zfs.h>
static char cmdname[] = "ztest";
static char *zopt_pool = cmdname;
static uint64_t zopt_vdevs = 5;
static uint64_t zopt_vdevtime;
static int zopt_ashift = SPA_MINBLOCKSHIFT;
static int zopt_mirrors = 2;
static int zopt_raidz = 4;
static int zopt_raidz_parity = 1;
static size_t zopt_vdev_size = SPA_MINDEVSIZE;
static int zopt_datasets = 7;
static int zopt_threads = 23;
static uint64_t zopt_passtime = 60; /* 60 seconds */
static uint64_t zopt_killrate = 70; /* 70% kill rate */
static int zopt_verbose = 0;
static int zopt_init = 1;
static char *zopt_dir = "/tmp";
static uint64_t zopt_time = 300; /* 5 minutes */
static int zopt_maxfaults;
typedef struct ztest_block_tag {
uint64_t bt_objset;
uint64_t bt_object;
uint64_t bt_offset;
uint64_t bt_txg;
uint64_t bt_thread;
uint64_t bt_seq;
} ztest_block_tag_t;
typedef struct ztest_args {
char za_pool[MAXNAMELEN];
spa_t *za_spa;
objset_t *za_os;
zilog_t *za_zilog;
kthread_t *za_thread;
kt_did_t za_threadid;
uint64_t za_instance;
uint64_t za_random;
uint64_t za_diroff;
uint64_t za_diroff_shared;
uint64_t za_zil_seq;
hrtime_t za_start;
hrtime_t za_stop;
hrtime_t za_kill;
/*
* Thread-local variables can go here to aid debugging.
*/
ztest_block_tag_t za_rbt;
ztest_block_tag_t za_wbt;
dmu_object_info_t za_doi;
dmu_buf_t *za_dbuf;
} ztest_args_t;
typedef void ztest_func_t(ztest_args_t *);
/*
* Note: these aren't static because we want dladdr() to work.
*/
ztest_func_t ztest_dmu_read_write;
ztest_func_t ztest_dmu_read_write_zcopy;
ztest_func_t ztest_dmu_write_parallel;
ztest_func_t ztest_dmu_object_alloc_free;
ztest_func_t ztest_dmu_commit_callbacks;
ztest_func_t ztest_zap;
ztest_func_t ztest_zap_parallel;
ztest_func_t ztest_traverse;
ztest_func_t ztest_dsl_prop_get_set;
ztest_func_t ztest_dmu_objset_create_destroy;
ztest_func_t ztest_dmu_snapshot_create_destroy;
ztest_func_t ztest_dsl_dataset_promote_busy;
ztest_func_t ztest_spa_create_destroy;
ztest_func_t ztest_fault_inject;
ztest_func_t ztest_spa_rename;
ztest_func_t ztest_vdev_attach_detach;
ztest_func_t ztest_vdev_LUN_growth;
ztest_func_t ztest_vdev_add_remove;
ztest_func_t ztest_vdev_aux_add_remove;
ztest_func_t ztest_scrub;
typedef struct ztest_info {
ztest_func_t *zi_func; /* test function */
uint64_t zi_iters; /* iterations per execution */
uint64_t *zi_interval; /* execute every <interval> seconds */
uint64_t zi_calls; /* per-pass count */
uint64_t zi_call_time; /* per-pass time */
uint64_t zi_call_total; /* cumulative total */
uint64_t zi_call_target; /* target cumulative total */
} ztest_info_t;
uint64_t zopt_always = 0; /* all the time */
uint64_t zopt_often = 1; /* every second */
uint64_t zopt_sometimes = 10; /* every 10 seconds */
uint64_t zopt_rarely = 60; /* every 60 seconds */
ztest_info_t ztest_info[] = {
{ ztest_dmu_read_write, 1, &zopt_always },
{ ztest_dmu_read_write_zcopy, 1, &zopt_always },
{ ztest_dmu_write_parallel, 30, &zopt_always },
{ ztest_dmu_object_alloc_free, 1, &zopt_always },
{ ztest_dmu_commit_callbacks, 10, &zopt_always },
{ ztest_zap, 30, &zopt_always },
{ ztest_zap_parallel, 100, &zopt_always },
{ ztest_dsl_prop_get_set, 1, &zopt_sometimes },
{ ztest_dmu_objset_create_destroy, 1, &zopt_sometimes },
{ ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
{ ztest_spa_create_destroy, 1, &zopt_sometimes },
{ ztest_fault_inject, 1, &zopt_sometimes },
{ ztest_spa_rename, 1, &zopt_rarely },
{ ztest_vdev_attach_detach, 1, &zopt_rarely },
{ ztest_vdev_LUN_growth, 1, &zopt_rarely },
{ ztest_dsl_dataset_promote_busy, 1, &zopt_rarely },
{ ztest_vdev_add_remove, 1, &zopt_vdevtime },
{ ztest_vdev_aux_add_remove, 1, &zopt_vdevtime },
{ ztest_scrub, 1, &zopt_vdevtime },
};
#define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
#define ZTEST_SYNC_LOCKS 16
/*
* The following struct is used to hold a list of uncalled commit callbacks.
*
* The callbacks are ordered by txg number.
*/
typedef struct ztest_cb_list {
kmutex_t zcl_callbacks_lock;
list_t zcl_callbacks;
} ztest_cb_list_t;
/*
* Stuff we need to share writably between parent and child.
*/
typedef struct ztest_shared {
kmutex_t zs_vdev_lock;
krwlock_t zs_name_lock;
uint64_t zs_vdev_primaries;
uint64_t zs_vdev_aux;
uint64_t zs_enospc_count;
hrtime_t zs_start_time;
hrtime_t zs_stop_time;
uint64_t zs_alloc;
uint64_t zs_space;
ztest_info_t zs_info[ZTEST_FUNCS];
kmutex_t zs_sync_lock[ZTEST_SYNC_LOCKS];
uint64_t zs_seq[ZTEST_SYNC_LOCKS];
ztest_cb_list_t zs_cb_list;
} ztest_shared_t;
static char ztest_dev_template[] = "%s/%s.%llua";
static char ztest_aux_template[] = "%s/%s.%s.%llu";
static ztest_shared_t *ztest_shared;
static int ztest_random_fd;
static int ztest_dump_core = 1;
static uint64_t metaslab_sz;
static boolean_t ztest_exiting;
extern uint64_t metaslab_gang_bang;
extern uint64_t metaslab_df_alloc_threshold;
#define ZTEST_DIROBJ 1
#define ZTEST_MICROZAP_OBJ 2
#define ZTEST_FATZAP_OBJ 3
#define ZTEST_DIROBJ_BLOCKSIZE (1 << 10)
#define ZTEST_DIRSIZE 256
static void usage(boolean_t) __NORETURN;
/*
* These libumem hooks provide a reasonable set of defaults for the allocator's
* debugging facilities.
*/
const char *
_umem_debug_init(void)
{
return ("default,verbose"); /* $UMEM_DEBUG setting */
}
const char *
_umem_logging_init(void)
{
return ("fail,contents"); /* $UMEM_LOGGING setting */
}
#define FATAL_MSG_SZ 1024
char *fatal_msg;
static void
fatal(int do_perror, char *message, ...)
{
va_list args;
int save_errno = errno;
char buf[FATAL_MSG_SZ];
(void) fflush(stdout);
va_start(args, message);
(void) sprintf(buf, "ztest: ");
/* LINTED */
(void) vsprintf(buf + strlen(buf), message, args);
va_end(args);
if (do_perror) {
(void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
": %s", strerror(save_errno));
}
(void) fprintf(stderr, "%s\n", buf);
fatal_msg = buf; /* to ease debugging */
if (ztest_dump_core)
abort();
exit(3);
}
static int
str2shift(const char *buf)
{
const char *ends = "BKMGTPEZ";
int i;
if (buf[0] == '\0')
return (0);
for (i = 0; i < strlen(ends); i++) {
if (toupper(buf[0]) == ends[i])
break;
}
if (i == strlen(ends)) {
(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
buf);
usage(B_FALSE);
}
if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
return (10*i);
}
(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
usage(B_FALSE);
/* NOTREACHED */
}
static uint64_t
nicenumtoull(const char *buf)
{
char *end;
uint64_t val;
val = strtoull(buf, &end, 0);
if (end == buf) {
(void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
usage(B_FALSE);
} else if (end[0] == '.') {
double fval = strtod(buf, &end);
fval *= pow(2, str2shift(end));
if (fval > UINT64_MAX) {
(void) fprintf(stderr, "ztest: value too large: %s\n",
buf);
usage(B_FALSE);
}
val = (uint64_t)fval;
} else {
int shift = str2shift(end);
if (shift >= 64 || (val << shift) >> shift != val) {
(void) fprintf(stderr, "ztest: value too large: %s\n",
buf);
usage(B_FALSE);
}
val <<= shift;
}
return (val);
}
static void
usage(boolean_t requested)
{
char nice_vdev_size[10];
char nice_gang_bang[10];
FILE *fp = requested ? stdout : stderr;
nicenum(zopt_vdev_size, nice_vdev_size);
nicenum(metaslab_gang_bang, nice_gang_bang);
(void) fprintf(fp, "Usage: %s\n"
"\t[-v vdevs (default: %llu)]\n"
"\t[-s size_of_each_vdev (default: %s)]\n"
"\t[-a alignment_shift (default: %d) (use 0 for random)]\n"
"\t[-m mirror_copies (default: %d)]\n"
"\t[-r raidz_disks (default: %d)]\n"
"\t[-R raidz_parity (default: %d)]\n"
"\t[-d datasets (default: %d)]\n"
"\t[-t threads (default: %d)]\n"
"\t[-g gang_block_threshold (default: %s)]\n"
"\t[-i initialize pool i times (default: %d)]\n"
"\t[-k kill percentage (default: %llu%%)]\n"
"\t[-p pool_name (default: %s)]\n"
"\t[-f file directory for vdev files (default: %s)]\n"
"\t[-V(erbose)] (use multiple times for ever more blather)\n"
"\t[-E(xisting)] (use existing pool instead of creating new one)\n"
"\t[-T time] total run time (default: %llu sec)\n"
"\t[-P passtime] time per pass (default: %llu sec)\n"
"\t[-h] (print help)\n"
"",
cmdname,
(u_longlong_t)zopt_vdevs, /* -v */
nice_vdev_size, /* -s */
zopt_ashift, /* -a */
zopt_mirrors, /* -m */
zopt_raidz, /* -r */
zopt_raidz_parity, /* -R */
zopt_datasets, /* -d */
zopt_threads, /* -t */
nice_gang_bang, /* -g */
zopt_init, /* -i */
(u_longlong_t)zopt_killrate, /* -k */
zopt_pool, /* -p */
zopt_dir, /* -f */
(u_longlong_t)zopt_time, /* -T */
(u_longlong_t)zopt_passtime); /* -P */
exit(requested ? 0 : 1);
}
static uint64_t
ztest_random(uint64_t range)
{
uint64_t r;
if (range == 0)
return (0);
if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r))
fatal(1, "short read from /dev/urandom");
return (r % range);
}
/* ARGSUSED */
static void
ztest_record_enospc(char *s)
{
ztest_shared->zs_enospc_count++;
}
static void
process_options(int argc, char **argv)
{
int opt;
uint64_t value;
/* By default, test gang blocks for blocks 32K and greater */
metaslab_gang_bang = 32 << 10;
while ((opt = getopt(argc, argv,
"v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:h")) != EOF) {
value = 0;
switch (opt) {
case 'v':
case 's':
case 'a':
case 'm':
case 'r':
case 'R':
case 'd':
case 't':
case 'g':
case 'i':
case 'k':
case 'T':
case 'P':
value = nicenumtoull(optarg);
}
switch (opt) {
case 'v':
zopt_vdevs = value;
break;
case 's':
zopt_vdev_size = MAX(SPA_MINDEVSIZE, value);
break;
case 'a':
zopt_ashift = value;
break;
case 'm':
zopt_mirrors = value;
break;
case 'r':
zopt_raidz = MAX(1, value);
break;
case 'R':
zopt_raidz_parity = MIN(MAX(value, 1), 2);
break;
case 'd':
zopt_datasets = MAX(1, value);
break;
case 't':
zopt_threads = MAX(1, value);
break;
case 'g':
metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value);
break;
case 'i':
zopt_init = value;
break;
case 'k':
zopt_killrate = value;
break;
case 'p':
zopt_pool = strdup(optarg);
break;
case 'f':
zopt_dir = strdup(optarg);
break;
case 'V':
zopt_verbose++;
break;
case 'E':
zopt_init = 0;
break;
case 'T':
zopt_time = value;
break;
case 'P':
zopt_passtime = MAX(1, value);
break;
case 'h':
usage(B_TRUE);
break;
case '?':
default:
usage(B_FALSE);
break;
}
}
zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1);
zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time / zopt_vdevs : UINT64_MAX);
zopt_maxfaults = MAX(zopt_mirrors, 1) * (zopt_raidz_parity + 1) - 1;
}
static uint64_t
ztest_get_ashift(void)
{
if (zopt_ashift == 0)
return (SPA_MINBLOCKSHIFT + ztest_random(3));
return (zopt_ashift);
}
static nvlist_t *
make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
{
char pathbuf[MAXPATHLEN];
uint64_t vdev;
nvlist_t *file;
if (ashift == 0)
ashift = ztest_get_ashift();
if (path == NULL) {
path = pathbuf;
if (aux != NULL) {
vdev = ztest_shared->zs_vdev_aux;
(void) sprintf(path, ztest_aux_template,
zopt_dir, zopt_pool, aux, vdev);
} else {
vdev = ztest_shared->zs_vdev_primaries++;
(void) sprintf(path, ztest_dev_template,
zopt_dir, zopt_pool, vdev);
}
}
if (size != 0) {
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
if (fd == -1)
fatal(1, "can't open %s", path);
if (ftruncate(fd, size) != 0)
fatal(1, "can't ftruncate %s", path);
(void) close(fd);
}
VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
return (file);
}
static nvlist_t *
make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
{
nvlist_t *raidz, **child;
int c;
if (r < 2)
return (make_vdev_file(path, aux, size, ashift));
child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
for (c = 0; c < r; c++)
child[c] = make_vdev_file(path, aux, size, ashift);
VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
VDEV_TYPE_RAIDZ) == 0);
VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
zopt_raidz_parity) == 0);
VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
child, r) == 0);
for (c = 0; c < r; c++)
nvlist_free(child[c]);
umem_free(child, r * sizeof (nvlist_t *));
return (raidz);
}
static nvlist_t *
make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
int r, int m)
{
nvlist_t *mirror, **child;
int c;
if (m < 1)
return (make_vdev_raidz(path, aux, size, ashift, r));
child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
for (c = 0; c < m; c++)
child[c] = make_vdev_raidz(path, aux, size, ashift, r);
VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
VDEV_TYPE_MIRROR) == 0);
VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
child, m) == 0);
for (c = 0; c < m; c++)
nvlist_free(child[c]);
umem_free(child, m * sizeof (nvlist_t *));
return (mirror);
}
static nvlist_t *
make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
int log, int r, int m, int t)
{
nvlist_t *root, **child;
int c;
ASSERT(t > 0);
child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
for (c = 0; c < t; c++) {
child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
log) == 0);
}
VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
child, t) == 0);
for (c = 0; c < t; c++)
nvlist_free(child[c]);
umem_free(child, t * sizeof (nvlist_t *));
return (root);
}
static void
ztest_set_random_blocksize(objset_t *os, uint64_t object, dmu_tx_t *tx)
{
int bs = SPA_MINBLOCKSHIFT +
ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1);
int ibs = DN_MIN_INDBLKSHIFT +
ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1);
int error;
error = dmu_object_set_blocksize(os, object, 1ULL << bs, ibs, tx);
if (error) {
char osname[300];
dmu_objset_name(os, osname);
fatal(0, "dmu_object_set_blocksize('%s', %llu, %d, %d) = %d",
osname, object, 1 << bs, ibs, error);
}
}
static uint8_t
ztest_random_checksum(void)
{
uint8_t checksum;
do {
checksum = ztest_random(ZIO_CHECKSUM_FUNCTIONS);
} while (zio_checksum_table[checksum].ci_zbt);
if (checksum == ZIO_CHECKSUM_OFF)
checksum = ZIO_CHECKSUM_ON;
return (checksum);
}
static uint8_t
ztest_random_compress(void)
{
return ((uint8_t)ztest_random(ZIO_COMPRESS_FUNCTIONS));
}
static int
ztest_replay_create(objset_t *os, lr_create_t *lr, boolean_t byteswap)
{
dmu_tx_t *tx;
int error;
if (byteswap)
byteswap_uint64_array(lr, sizeof (*lr));
tx = dmu_tx_create(os);
dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
error = dmu_tx_assign(tx, TXG_WAIT);
if (error) {
dmu_tx_abort(tx);
return (error);
}
error = dmu_object_claim(os, lr->lr_doid, lr->lr_mode, 0,
DMU_OT_NONE, 0, tx);
ASSERT3U(error, ==, 0);
dmu_tx_commit(tx);
if (zopt_verbose >= 5) {
char osname[MAXNAMELEN];
dmu_objset_name(os, osname);
(void) printf("replay create of %s object %llu"
" in txg %llu = %d\n",
osname, (u_longlong_t)lr->lr_doid,
(u_longlong_t)dmu_tx_get_txg(tx), error);
}
return (error);
}
static int
ztest_replay_remove(objset_t *os, lr_remove_t *lr, boolean_t byteswap)
{
dmu_tx_t *tx;
int error;
if (byteswap)
byteswap_uint64_array(lr, sizeof (*lr));
tx = dmu_tx_create(os);
dmu_tx_hold_free(tx, lr->lr_doid, 0, DMU_OBJECT_END);
error = dmu_tx_assign(tx, TXG_WAIT);
if (error) {
dmu_tx_abort(tx);
return (error);
}
error = dmu_object_free(os, lr->lr_doid, tx);
dmu_tx_commit(tx);
return (error);
}
zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
NULL, /* 0 no such transaction type */
(zil_replay_func_t *)ztest_replay_create,/* TX_CREATE */
NULL, /* TX_MKDIR */
NULL, /* TX_MKXATTR */
NULL, /* TX_SYMLINK */
(zil_replay_func_t *)ztest_replay_remove,/* TX_REMOVE */
NULL, /* TX_RMDIR */
NULL, /* TX_LINK */
NULL, /* TX_RENAME */
NULL, /* TX_WRITE */
NULL, /* TX_TRUNCATE */
NULL, /* TX_SETATTR */
NULL, /* TX_ACL */
};
/*
* Verify that we can't destroy an active pool, create an existing pool,
* or create a pool with a bad vdev spec.
*/
void
ztest_spa_create_destroy(ztest_args_t *za)
{
int error;
spa_t *spa;
nvlist_t *nvroot;
/*
* Attempt to create using a bad file.
*/
nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
error = spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL);
nvlist_free(nvroot);
if (error != ENOENT)
fatal(0, "spa_create(bad_file) = %d", error);
/*
* Attempt to create using a bad mirror.
*/
nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
error = spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL);
nvlist_free(nvroot);
if (error != ENOENT)
fatal(0, "spa_create(bad_mirror) = %d", error);
/*
* Attempt to create an existing pool. It shouldn't matter
* what's in the nvroot; we should fail with EEXIST.
*/
rw_enter(&ztest_shared->zs_name_lock, RW_READER);
nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
error = spa_create(za->za_pool, nvroot, NULL, NULL, NULL);
nvlist_free(nvroot);
if (error != EEXIST)
fatal(0, "spa_create(whatever) = %d", error);
error = spa_open(za->za_pool, &spa, FTAG);
if (error)
fatal(0, "spa_open() = %d", error);
error = spa_destroy(za->za_pool);
if (error != EBUSY)
fatal(0, "spa_destroy() = %d", error);
spa_close(spa, FTAG);
rw_exit(&ztest_shared->zs_name_lock);
}
static vdev_t *
vdev_lookup_by_path(vdev_t *vd, const char *path)
{
vdev_t *mvd;
int c;
if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
return (vd);
for (c = 0; c < vd->vdev_children; c++)
if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
NULL)
return (mvd);
return (NULL);
}
/*
* Verify that vdev_add() works as expected.
*/
void
ztest_vdev_add_remove(ztest_args_t *za)
{
spa_t *spa = za->za_spa;
uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
nvlist_t *nvroot;
int error;
mutex_enter(&ztest_shared->zs_vdev_lock);
spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
ztest_shared->zs_vdev_primaries =
spa->spa_root_vdev->vdev_children * leaves;
spa_config_exit(spa, SCL_VDEV, FTAG);
/*
* Make 1/4 of the devices be log devices.
*/
nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
ztest_random(4) == 0, zopt_raidz, zopt_mirrors, 1);
error = spa_vdev_add(spa, nvroot);
nvlist_free(nvroot);
mutex_exit(&ztest_shared->zs_vdev_lock);
if (error == ENOSPC)
ztest_record_enospc("spa_vdev_add");
else if (error != 0)
fatal(0, "spa_vdev_add() = %d", error);
}
/*
* Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
*/
void
ztest_vdev_aux_add_remove(ztest_args_t *za)
{
spa_t *spa = za->za_spa;
vdev_t *rvd = spa->spa_root_vdev;
spa_aux_vdev_t *sav;
char *aux;
uint64_t guid = 0;
int error;
if (ztest_random(2) == 0) {
sav = &spa->spa_spares;
aux = ZPOOL_CONFIG_SPARES;
} else {
sav = &spa->spa_l2cache;
aux = ZPOOL_CONFIG_L2CACHE;
}
mutex_enter(&ztest_shared->zs_vdev_lock);
spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
if (sav->sav_count != 0 && ztest_random(4) == 0) {
/*
* Pick a random device to remove.
*/
guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
} else {
/*
* Find an unused device we can add.
*/
ztest_shared->zs_vdev_aux = 0;
for (;;) {
char path[MAXPATHLEN];
int c;
(void) sprintf(path, ztest_aux_template, zopt_dir,
zopt_pool, aux, ztest_shared->zs_vdev_aux);
for (c = 0; c < sav->sav_count; c++)
if (strcmp(sav->sav_vdevs[c]->vdev_path,
path) == 0)
break;
if (c == sav->sav_count &&
vdev_lookup_by_path(rvd, path) == NULL)
break;
ztest_shared->zs_vdev_aux++;
}
}
spa_config_exit(spa, SCL_VDEV, FTAG);
if (guid == 0) {
/*
* Add a new device.
*/
nvlist_t *nvroot = make_vdev_root(NULL, aux,
(zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
error = spa_vdev_add(spa, nvroot);
if (error != 0)
fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
nvlist_free(nvroot);
} else {
/*
* Remove an existing device. Sometimes, dirty its
* vdev state first to make sure we handle removal
* of devices that have pending state changes.
*/
if (ztest_random(2) == 0)
(void) vdev_online(spa, guid, 0, NULL);
error = spa_vdev_remove(spa, guid, B_FALSE);
if (error != 0 && error != EBUSY)
fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
}
mutex_exit(&ztest_shared->zs_vdev_lock);
}
/*
* Verify that we can attach and detach devices.
*/
void
ztest_vdev_attach_detach(ztest_args_t *za)
{
spa_t *spa = za->za_spa;
spa_aux_vdev_t *sav = &spa->spa_spares;
vdev_t *rvd = spa->spa_root_vdev;
vdev_t *oldvd, *newvd, *pvd;
nvlist_t *root;
uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
uint64_t leaf, top;
uint64_t ashift = ztest_get_ashift();
uint64_t oldguid, pguid;
size_t oldsize, newsize;
char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
int replacing;
int oldvd_has_siblings = B_FALSE;
int newvd_is_spare = B_FALSE;
int oldvd_is_log;
int error, expected_error;
mutex_enter(&ztest_shared->zs_vdev_lock);
spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
/*
* Decide whether to do an attach or a replace.
*/
replacing = ztest_random(2);
/*
* Pick a random top-level vdev.
*/
top = ztest_random(rvd->vdev_children);
/*
* Pick a random leaf within it.
*/
leaf = ztest_random(leaves);
/*
* Locate this vdev.
*/
oldvd = rvd->vdev_child[top];
if (zopt_mirrors >= 1) {
ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
ASSERT(oldvd->vdev_children >= zopt_mirrors);
oldvd = oldvd->vdev_child[leaf / zopt_raidz];
}
if (zopt_raidz > 1) {
ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
ASSERT(oldvd->vdev_children == zopt_raidz);
oldvd = oldvd->vdev_child[leaf % zopt_raidz];
}
/*
* If we're already doing an attach or replace, oldvd may be a
* mirror vdev -- in which case, pick a random child.
*/
while (oldvd->vdev_children != 0) {
oldvd_has_siblings = B_TRUE;
ASSERT(oldvd->vdev_children >= 2);
oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
}
oldguid = oldvd->vdev_guid;
oldsize = vdev_get_min_asize(oldvd);
oldvd_is_log = oldvd->vdev_top->vdev_islog;
(void) strcpy(oldpath, oldvd->vdev_path);
pvd = oldvd->vdev_parent;
pguid = pvd->vdev_guid;
/*
* If oldvd has siblings, then half of the time, detach it.
*/
if (oldvd_has_siblings && ztest_random(2) == 0) {
spa_config_exit(spa, SCL_VDEV, FTAG);
error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
if (error != 0 && error != ENODEV && error != EBUSY &&
error != ENOTSUP)
fatal(0, "detach (%s) returned %d", oldpath, error);
mutex_exit(&ztest_shared->zs_vdev_lock);
return;
}
/*
* For the new vdev, choose with equal probability between the two
* standard paths (ending in either 'a' or 'b') or a random hot spare.
*/
if (sav->sav_count != 0 && ztest_random(3) == 0) {
newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
newvd_is_spare = B_TRUE;
(void) strcpy(newpath, newvd->vdev_path);
} else {
(void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
zopt_dir, zopt_pool, top * leaves + leaf);
if (ztest_random(2) == 0)
newpath[strlen(newpath) - 1] = 'b';
newvd = vdev_lookup_by_path(rvd, newpath);
}
if (newvd) {
newsize = vdev_get_min_asize(newvd);
} else {
/*
* Make newsize a little bigger or smaller than oldsize.
* If it's smaller, the attach should fail.
* If it's larger, and we're doing a replace,
* we should get dynamic LUN growth when we're done.
*/
newsize = 10 * oldsize / (9 + ztest_random(3));
}
/*
* If pvd is not a mirror or root, the attach should fail with ENOTSUP,
* unless it's a replace; in that case any non-replacing parent is OK.
*
* If newvd is already part of the pool, it should fail with EBUSY.
*
* If newvd is too small, it should fail with EOVERFLOW.
*/
if (pvd->vdev_ops != &vdev_mirror_ops &&
pvd->vdev_ops != &vdev_root_ops && (!replacing ||
pvd->vdev_ops == &vdev_replacing_ops ||
pvd->vdev_ops == &vdev_spare_ops))
expected_error = ENOTSUP;
else if (newvd_is_spare && (!replacing || oldvd_is_log))
expected_error = ENOTSUP;
else if (newvd == oldvd)
expected_error = replacing ? 0 : EBUSY;
else if (vdev_lookup_by_path(rvd, newpath) != NULL)
expected_error = EBUSY;
else if (newsize < oldsize)
expected_error = EOVERFLOW;
else if (ashift > oldvd->vdev_top->vdev_ashift)
expected_error = EDOM;
else
expected_error = 0;
spa_config_exit(spa, SCL_VDEV, FTAG);
/*
* Build the nvlist describing newpath.
*/
root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
ashift, 0, 0, 0, 1);
error = spa_vdev_attach(spa, oldguid, root, replacing);
nvlist_free(root);
/*
* If our parent was the replacing vdev, but the replace completed,
* then instead of failing with ENOTSUP we may either succeed,
* fail with ENODEV, or fail with EOVERFLOW.
*/
if (expected_error == ENOTSUP &&
(error == 0 || error == ENODEV || error == EOVERFLOW))
expected_error = error;
/*
* If someone grew the LUN, the replacement may be too small.
*/
if (error == EOVERFLOW || error == EBUSY)
expected_error = error;
/* XXX workaround 6690467 */
if (error != expected_error && expected_error != EBUSY) {
fatal(0, "attach (%s %llu, %s %llu, %d) "
"returned %d, expected %d",
oldpath, (longlong_t)oldsize, newpath,
(longlong_t)newsize, replacing, error, expected_error);
}
mutex_exit(&ztest_shared->zs_vdev_lock);
}
/*
* Callback function which expands the physical size of the vdev.
*/
vdev_t *
grow_vdev(vdev_t *vd, void *arg)
{
ASSERTV(spa_t *spa = vd->vdev_spa);
size_t *newsize = arg;
size_t fsize;
int fd;
ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
ASSERT(vd->vdev_ops->vdev_op_leaf);
if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
return (vd);
fsize = lseek(fd, 0, SEEK_END);
VERIFY(ftruncate(fd, *newsize) == 0);
if (zopt_verbose >= 6) {
(void) printf("%s grew from %lu to %lu bytes\n",
vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
}
(void) close(fd);
return (NULL);
}
/*
* Callback function which expands a given vdev by calling vdev_online().
*/
/* ARGSUSED */
vdev_t *
online_vdev(vdev_t *vd, void *arg)
{
spa_t *spa = vd->vdev_spa;
vdev_t *tvd = vd->vdev_top;
vdev_t *pvd = vd->vdev_parent;
uint64_t guid = vd->vdev_guid;
ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
ASSERT(vd->vdev_ops->vdev_op_leaf);
/* Calling vdev_online will initialize the new metaslabs */
spa_config_exit(spa, SCL_STATE, spa);
(void) vdev_online(spa, guid, ZFS_ONLINE_EXPAND, NULL);
spa_config_enter(spa, SCL_STATE, spa, RW_READER);
/*
* Since we dropped the lock we need to ensure that we're
* still talking to the original vdev. It's possible this
* vdev may have been detached/replaced while we were
* trying to online it.
*/
if (vd != vdev_lookup_by_guid(tvd, guid) || vd->vdev_parent != pvd) {
if (zopt_verbose >= 6) {
(void) printf("vdev %p has disappeared, was "
"guid %llu\n", (void *)vd, (u_longlong_t)guid);
}
return (vd);
}
return (NULL);
}
/*
* Traverse the vdev tree calling the supplied function.
* We continue to walk the tree until we either have walked all
* children or we receive a non-NULL return from the callback.
* If a NULL callback is passed, then we just return back the first
* leaf vdev we encounter.
*/
vdev_t *
vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
{
uint_t c;
if (vd->vdev_ops->vdev_op_leaf) {
if (func == NULL)
return (vd);
else
return (func(vd, arg));
}
for (c = 0; c < vd->vdev_children; c++) {
vdev_t *cvd = vd->vdev_child[c];
if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
return (cvd);
}
return (NULL);
}
/*
* Verify that dynamic LUN growth works as expected.
*/
void
ztest_vdev_LUN_growth(ztest_args_t *za)
{
spa_t *spa = za->za_spa;
vdev_t *vd, *tvd = NULL;
size_t psize, newsize;
uint64_t spa_newsize, spa_cursize, ms_count;
mutex_enter(&ztest_shared->zs_vdev_lock);
mutex_enter(&spa_namespace_lock);
spa_config_enter(spa, SCL_STATE, spa, RW_READER);
while (tvd == NULL || tvd->vdev_islog) {
uint64_t vdev;
vdev = ztest_random(spa->spa_root_vdev->vdev_children);
tvd = spa->spa_root_vdev->vdev_child[vdev];
}
/*
* Determine the size of the first leaf vdev associated with
* our top-level device.
*/
vd = vdev_walk_tree(tvd, NULL, NULL);
ASSERT3P(vd, !=, NULL);
ASSERT(vd->vdev_ops->vdev_op_leaf);
psize = vd->vdev_psize;
/*
* We only try to expand the vdev if it's less than 4x its
* original size and it has a valid psize.
*/
if (psize == 0 || psize >= 4 * zopt_vdev_size) {
spa_config_exit(spa, SCL_STATE, spa);
mutex_exit(&spa_namespace_lock);
mutex_exit(&ztest_shared->zs_vdev_lock);
return;
}
ASSERT(psize > 0);
newsize = psize + psize / 8;
ASSERT3U(newsize, >, psize);
if (zopt_verbose >= 6) {
(void) printf("Expanding vdev %s from %lu to %lu\n",
vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
}
spa_cursize = spa_get_space(spa);
ms_count = tvd->vdev_ms_count;
/*
* Growing the vdev is a two step process:
* 1). expand the physical size (i.e. relabel)
* 2). online the vdev to create the new metaslabs
*/
if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
tvd->vdev_state != VDEV_STATE_HEALTHY) {
if (zopt_verbose >= 5) {
(void) printf("Could not expand LUN because "
"some vdevs were not healthy\n");
}
(void) spa_config_exit(spa, SCL_STATE, spa);
mutex_exit(&spa_namespace_lock);
mutex_exit(&ztest_shared->zs_vdev_lock);
return;
}
(void) spa_config_exit(spa, SCL_STATE, spa);
mutex_exit(&spa_namespace_lock);
/*
* Expanding the LUN will update the config asynchronously,
* thus we must wait for the async thread to complete any
* pending tasks before proceeding.
*/
mutex_enter(&spa->spa_async_lock);
while (spa->spa_async_thread != NULL || spa->spa_async_tasks)
cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
mutex_exit(&spa->spa_async_lock);
spa_config_enter(spa, SCL_STATE, spa, RW_READER);
spa_newsize = spa_get_space(spa);
/*
* Make sure we were able to grow the pool.
*/
if (ms_count >= tvd->vdev_ms_count ||
spa_cursize >= spa_newsize) {
(void) printf("Top-level vdev metaslab count: "
"before %llu, after %llu\n",
(u_longlong_t)ms_count,
(u_longlong_t)tvd->vdev_ms_count);
fatal(0, "LUN expansion failed: before %llu, "
"after %llu\n", spa_cursize, spa_newsize);
} else if (zopt_verbose >= 5) {
char oldnumbuf[6], newnumbuf[6];
nicenum(spa_cursize, oldnumbuf);
nicenum(spa_newsize, newnumbuf);
(void) printf("%s grew from %s to %s\n",
spa->spa_name, oldnumbuf, newnumbuf);
}
spa_config_exit(spa, SCL_STATE, spa);
mutex_exit(&ztest_shared->zs_vdev_lock);
}
/* ARGSUSED */
static void
ztest_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
{
/*
* Create the directory object.
*/
VERIFY(dmu_object_claim(os, ZTEST_DIROBJ,
DMU_OT_UINT64_OTHER, ZTEST_DIROBJ_BLOCKSIZE,
DMU_OT_UINT64_OTHER, 5 * sizeof (ztest_block_tag_t), tx) == 0);
VERIFY(zap_create_claim(os, ZTEST_MICROZAP_OBJ,
DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
VERIFY(zap_create_claim(os, ZTEST_FATZAP_OBJ,
DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
}
static int
ztest_destroy_cb(char *name, void *arg)
{
ztest_args_t *za = arg;
objset_t *os;
dmu_object_info_t *doi = &za->za_doi;
int error;
/*
* Verify that the dataset contains a directory object.
*/
error = dmu_objset_open(name, DMU_OST_OTHER,
DS_MODE_USER | DS_MODE_READONLY, &os);
ASSERT3U(error, ==, 0);
error = dmu_object_info(os, ZTEST_DIROBJ, doi);
if (error != ENOENT) {
/* We could have crashed in the middle of destroying it */
ASSERT3U(error, ==, 0);
ASSERT3U(doi->doi_type, ==, DMU_OT_UINT64_OTHER);
ASSERT3S(doi->doi_physical_blks, >=, 0);
}
dmu_objset_close(os);
/*
* Destroy the dataset.
*/
error = dmu_objset_destroy(name);
if (error) {
(void) dmu_objset_open(name, DMU_OST_OTHER,
DS_MODE_USER | DS_MODE_READONLY, &os);
fatal(0, "dmu_objset_destroy(os=%p) = %d\n", &os, error);
}
return (0);
}
/*
* Verify that dmu_objset_{create,destroy,open,close} work as expected.
*/
static uint64_t
ztest_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t object, int mode)
{
itx_t *itx;
lr_create_t *lr;
size_t namesize;
char name[24];
(void) sprintf(name, "ZOBJ_%llu", (u_longlong_t)object);
namesize = strlen(name) + 1;
itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize +
ztest_random(ZIL_MAX_BLKSZ));
lr = (lr_create_t *)&itx->itx_lr;
bzero(lr + 1, lr->lr_common.lrc_reclen - sizeof (*lr));
lr->lr_doid = object;
lr->lr_foid = 0;
lr->lr_mode = mode;
lr->lr_uid = 0;
lr->lr_gid = 0;
lr->lr_gen = dmu_tx_get_txg(tx);
lr->lr_crtime[0] = time(NULL);
lr->lr_crtime[1] = 0;
lr->lr_rdev = 0;
bcopy(name, (char *)(lr + 1), namesize);
return (zil_itx_assign(zilog, itx, tx));
}
void
ztest_dmu_objset_create_destroy(ztest_args_t *za)
{
int error;
objset_t *os, *os2;
char name[100];
int basemode, expected_error;
zilog_t *zilog;
uint64_t seq;
uint64_t objects;
rw_enter(&ztest_shared->zs_name_lock, RW_READER);
(void) snprintf(name, 100, "%s/%s_temp_%llu", za->za_pool, za->za_pool,
(u_longlong_t)za->za_instance);
basemode = DS_MODE_TYPE(za->za_instance);
if (basemode != DS_MODE_USER && basemode != DS_MODE_OWNER)
basemode = DS_MODE_USER;
/*
* If this dataset exists from a previous run, process its replay log
* half of the time. If we don't replay it, then dmu_objset_destroy()
* (invoked from ztest_destroy_cb() below) should just throw it away.
*/
if (ztest_random(2) == 0 &&
dmu_objset_open(name, DMU_OST_OTHER, DS_MODE_OWNER, &os) == 0) {
zil_replay(os, os, ztest_replay_vector);
dmu_objset_close(os);
}
/*
* There may be an old instance of the dataset we're about to
* create lying around from a previous run. If so, destroy it
* and all of its snapshots.
*/
(void) dmu_objset_find(name, ztest_destroy_cb, za,
DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
/*
* Verify that the destroyed dataset is no longer in the namespace.
*/
error = dmu_objset_open(name, DMU_OST_OTHER, basemode, &os);
if (error != ENOENT)
fatal(1, "dmu_objset_open(%s) found destroyed dataset %p",
name, os);
/*
* Verify that we can create a new dataset.
*/
error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 0,
ztest_create_cb, NULL);
if (error) {
if (error == ENOSPC) {
ztest_record_enospc("dmu_objset_create");
rw_exit(&ztest_shared->zs_name_lock);
return;
}
fatal(0, "dmu_objset_create(%s) = %d", name, error);
}
error = dmu_objset_open(name, DMU_OST_OTHER, basemode, &os);
if (error) {
fatal(0, "dmu_objset_open(%s) = %d", name, error);
}
/*
* Open the intent log for it.
*/
zilog = zil_open(os, NULL);
/*
* Put a random number of objects in there.
*/
objects = ztest_random(20);
seq = 0;
while (objects-- != 0) {
uint64_t object = 0;
dmu_tx_t *tx = dmu_tx_create(os);
dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, sizeof (name));
error = dmu_tx_assign(tx, TXG_WAIT);
if (error) {
dmu_tx_abort(tx);
} else {
object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
DMU_OT_NONE, 0, tx);
ztest_set_random_blocksize(os, object, tx);
seq = ztest_log_create(zilog, tx, object,
DMU_OT_UINT64_OTHER);
dmu_write(os, object, 0, sizeof (name), name, tx);
dmu_tx_commit(tx);
}
if (ztest_random(5) == 0) {
zil_commit(zilog, seq, object);
}
if (ztest_random(100) == 0) {
error = zil_suspend(zilog);
if (error == 0) {
zil_resume(zilog);
}
}
}
/*
* Verify that we cannot create an existing dataset.
*/
error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 0, NULL, NULL);
if (error != EEXIST)
fatal(0, "created existing dataset, error = %d", error);
/*
* Verify that multiple dataset holds are allowed, but only when
* the new access mode is compatible with the base mode.
*/
if (basemode == DS_MODE_OWNER) {
error = dmu_objset_open(name, DMU_OST_OTHER, DS_MODE_USER,
&os2);
if (error)
fatal(0, "dmu_objset_open('%s') = %d", name, error);
else
dmu_objset_close(os2);
}
error = dmu_objset_open(name, DMU_OST_OTHER, DS_MODE_OWNER, &os2);
expected_error = (basemode == DS_MODE_OWNER) ? EBUSY : 0;
if (error != expected_error)
fatal(0, "dmu_objset_open('%s') = %d, expected %d",
name, error, expected_error);
if (error == 0)
dmu_objset_close(os2);
zil_close(zilog);
dmu_objset_close(os);
error = dmu_objset_destroy(name);
if (error)
fatal(0, "dmu_objset_destroy(%s) = %d", name, error);
rw_exit(&ztest_shared->zs_name_lock);
}
/*
* Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
*/
void
ztest_dmu_snapshot_create_destroy(ztest_args_t *za)
{
int error;
objset_t *os = za->za_os;
char snapname[100];
char osname[MAXNAMELEN];
rw_enter(&ztest_shared->zs_name_lock, RW_READER);
dmu_objset_name(os, osname);
(void) snprintf(snapname, 100, "%s@%llu", osname,
(u_longlong_t)za->za_instance);
error = dmu_objset_destroy(snapname);
if (error != 0 && error != ENOENT)
fatal(0, "dmu_objset_destroy() = %d", error);
error = dmu_objset_snapshot(osname, strchr(snapname, '@')+1,
NULL, FALSE);
if (error == ENOSPC)
ztest_record_enospc("dmu_take_snapshot");
else if (error != 0 && error != EEXIST)
fatal(0, "dmu_take_snapshot() = %d", error);
rw_exit(&ztest_shared->zs_name_lock);
}
/*
* Cleanup non-standard snapshots and clones.
*/
void
ztest_dsl_dataset_cleanup(char *osname, uint64_t curval)
{
char snap1name[100];
char clone1name[100];
char snap2name[100];
char clone2name[100];
char snap3name[100];
int error;
(void) snprintf(snap1name, 100, "%s@s1_%llu",
osname, (u_longlong_t)curval);
(void) snprintf(clone1name, 100, "%s/c1_%llu",
osname, (u_longlong_t)curval);
(void) snprintf(snap2name, 100, "%s@s2_%llu",
clone1name, (u_longlong_t)curval);
(void) snprintf(clone2name, 100, "%s/c2_%llu",
osname, (u_longlong_t)curval);
(void) snprintf(snap3name, 100, "%s@s3_%llu",
clone1name, (u_longlong_t)curval);
error = dmu_objset_destroy(clone2name);
if (error && error != ENOENT)
fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error);
error = dmu_objset_destroy(snap3name);
if (error && error != ENOENT)
fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error);
error = dmu_objset_destroy(snap2name);
if (error && error != ENOENT)
fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
error = dmu_objset_destroy(clone1name);
if (error && error != ENOENT)
fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error);
error = dmu_objset_destroy(snap1name);
if (error && error != ENOENT)
fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error);
}
/*
* Verify dsl_dataset_promote handles EBUSY
*/
void
ztest_dsl_dataset_promote_busy(ztest_args_t *za)
{
int error;
objset_t *os = za->za_os;
objset_t *clone;
dsl_dataset_t *ds;
char snap1name[100];
char clone1name[100];
char snap2name[100];
char clone2name[100];
char snap3name[100];
char osname[MAXNAMELEN];
uint64_t curval = za->za_instance;
rw_enter(&ztest_shared->zs_name_lock, RW_READER);
dmu_objset_name(os, osname);
ztest_dsl_dataset_cleanup(osname, curval);
(void) snprintf(snap1name, 100, "%s@s1_%llu",
osname, (u_longlong_t)curval);
(void) snprintf(clone1name, 100, "%s/c1_%llu",
osname, (u_longlong_t)curval);
(void) snprintf(snap2name, 100, "%s@s2_%llu",
clone1name, (u_longlong_t)curval);
(void) snprintf(clone2name, 100, "%s/c2_%llu",
osname, (u_longlong_t)curval);
(void) snprintf(snap3name, 100, "%s@s3_%llu",
clone1name, (u_longlong_t)curval);
error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1,
NULL, FALSE);
if (error && error != EEXIST) {
if (error == ENOSPC) {
ztest_record_enospc("dmu_take_snapshot");
goto out;
}
fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
}
error = dmu_objset_open(snap1name, DMU_OST_OTHER,
DS_MODE_USER | DS_MODE_READONLY, &clone);
if (error)
fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error);
error = dmu_objset_create(clone1name, DMU_OST_OTHER, clone, 0,
NULL, NULL);
dmu_objset_close(clone);
if (error) {
if (error == ENOSPC) {
ztest_record_enospc("dmu_objset_create");
goto out;
}
fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
}
error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1,
NULL, FALSE);
if (error && error != EEXIST) {
if (error == ENOSPC) {
ztest_record_enospc("dmu_take_snapshot");
goto out;
}
fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
}
error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1,
NULL, FALSE);
if (error && error != EEXIST) {
if (error == ENOSPC) {
ztest_record_enospc("dmu_take_snapshot");
goto out;
}
fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
}
error = dmu_objset_open(snap3name, DMU_OST_OTHER,
DS_MODE_USER | DS_MODE_READONLY, &clone);
if (error)
fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
error = dmu_objset_create(clone2name, DMU_OST_OTHER, clone, 0,
NULL, NULL);
dmu_objset_close(clone);
if (error) {
if (error == ENOSPC) {
ztest_record_enospc("dmu_objset_create");
goto out;
}
fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
}
error = dsl_dataset_own(snap1name, DS_MODE_READONLY, FTAG, &ds);
if (error)
fatal(0, "dsl_dataset_own(%s) = %d", snap1name, error);
error = dsl_dataset_promote(clone2name);
if (error != EBUSY)
fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
error);
dsl_dataset_disown(ds, FTAG);
out:
ztest_dsl_dataset_cleanup(osname, curval);
rw_exit(&ztest_shared->zs_name_lock);
}
/*
* Verify that dmu_object_{alloc,free} work as expected.
*/
void
ztest_dmu_object_alloc_free(ztest_args_t *za)
{
objset_t *os = za->za_os;
dmu_buf_t *db;
dmu_tx_t *tx;
uint64_t batchobj, object, batchsize, endoff, temp;
int b, c, error, bonuslen;
dmu_object_info_t *doi = &za->za_doi;
char osname[MAXNAMELEN];
dmu_objset_name(os, osname);
endoff = -8ULL;
batchsize = 2;
/*
* Create a batch object if necessary, and record it in the directory.
*/
VERIFY3U(0, ==, dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
sizeof (uint64_t), &batchobj, DMU_READ_PREFETCH));
if (batchobj == 0) {
tx = dmu_tx_create(os);
dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff,
sizeof (uint64_t));
dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
error = dmu_tx_assign(tx, TXG_WAIT);
if (error) {
ztest_record_enospc("create a batch object");
dmu_tx_abort(tx);
return;
}
batchobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
DMU_OT_NONE, 0, tx);
ztest_set_random_blocksize(os, batchobj, tx);
dmu_write(os, ZTEST_DIROBJ, za->za_diroff,
sizeof (uint64_t), &batchobj, tx);
dmu_tx_commit(tx);
}
/*
* Destroy the previous batch of objects.
*/
for (b = 0; b < batchsize; b++) {
VERIFY3U(0, ==, dmu_read(os, batchobj, b * sizeof (uint64_t),
sizeof (uint64_t), &object, DMU_READ_PREFETCH));
if (object == 0)
continue;
/*
* Read and validate contents.
* We expect the nth byte of the bonus buffer to be n.
*/
VERIFY(0 == dmu_bonus_hold(os, object, FTAG, &db));
za->za_dbuf = db;
dmu_object_info_from_db(db, doi);
ASSERT(doi->doi_type == DMU_OT_UINT64_OTHER);
ASSERT(doi->doi_bonus_type == DMU_OT_PLAIN_OTHER);
ASSERT3S(doi->doi_physical_blks, >=, 0);
bonuslen = doi->doi_bonus_size;
for (c = 0; c < bonuslen; c++) {
if (((uint8_t *)db->db_data)[c] !=
(uint8_t)(c + bonuslen)) {
fatal(0,
"bad bonus: %s, obj %llu, off %d: %u != %u",
osname, object, c,
((uint8_t *)db->db_data)[c],
(uint8_t)(c + bonuslen));
}
}
dmu_buf_rele(db, FTAG);
za->za_dbuf = NULL;
/*
* We expect the word at endoff to be our object number.
*/
VERIFY(0 == dmu_read(os, object, endoff,
sizeof (uint64_t), &temp, DMU_READ_PREFETCH));
if (temp != object) {
fatal(0, "bad data in %s, got %llu, expected %llu",
osname, temp, object);
}
/*
* Destroy old object and clear batch entry.
*/
tx = dmu_tx_create(os);
dmu_tx_hold_write(tx, batchobj,
b * sizeof (uint64_t), sizeof (uint64_t));
dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
error = dmu_tx_assign(tx, TXG_WAIT);
if (error) {
ztest_record_enospc("free object");
dmu_tx_abort(tx);
return;
}
error = dmu_object_free(os, object, tx);
if (error) {
fatal(0, "dmu_object_free('%s', %llu) = %d",
osname, object, error);
}
object = 0;
dmu_object_set_checksum(os, batchobj,
ztest_random_checksum(), tx);
dmu_object_set_compress(os, batchobj,
ztest_random_compress(), tx);
dmu_write(os, batchobj, b * sizeof (uint64_t),
sizeof (uint64_t), &object, tx);
dmu_tx_commit(tx);
}
/*
* Before creating the new batch of objects, generate a bunch of churn.
*/
for (b = ztest_random(100); b > 0; b--) {
tx = dmu_tx_create(os);
dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
error = dmu_tx_assign(tx, TXG_WAIT);
if (error) {
ztest_record_enospc("churn objects");
dmu_tx_abort(tx);
return;
}
object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
DMU_OT_NONE, 0, tx);
ztest_set_random_blocksize(os, object, tx);
error = dmu_object_free(os, object, tx);
if (error) {
fatal(0, "dmu_object_free('%s', %llu) = %d",
osname, object, error);
}
dmu_tx_commit(tx);
}
/*
* Create a new batch of objects with randomly chosen
* blocksizes and record them in the batch directory.
*/
for (b = 0; b < batchsize; b++) {
uint32_t va_blksize;
u_longlong_t va_nblocks;
tx = dmu_tx_create(os);
dmu_tx_hold_write(tx, batchobj, b * sizeof (uint64_t),
sizeof (uint64_t));
dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
dmu_tx_hold_write(tx, DMU_NEW_OBJECT, endoff,
sizeof (uint64_t));
error = dmu_tx_assign(tx, TXG_WAIT);
if (error) {
ztest_record_enospc("create batchobj");
dmu_tx_abort(tx);
return;
}
bonuslen = (int)ztest_random(dmu_bonus_max()) + 1;
object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
DMU_OT_PLAIN_OTHER, bonuslen, tx);
ztest_set_random_blocksize(os, object, tx);
dmu_object_set_checksum(os, object,
ztest_random_checksum(), tx);
dmu_object_set_compress(os, object,
ztest_random_compress(), tx);
dmu_write(os, batchobj, b * sizeof (uint64_t),
sizeof (uint64_t), &object, tx);
/*
* Write to both the bonus buffer and the regular data.
*/
VERIFY(dmu_bonus_hold(os, object, FTAG, &db) == 0);
za->za_dbuf = db;
ASSERT3U(bonuslen, <=, db->db_size);
dmu_object_size_from_db(db, &va_blksize, &va_nblocks);
ASSERT3S(va_nblocks, >=, 0);
dmu_buf_will_dirty(db, tx);
/*
* See comments above regarding the contents of
* the bonus buffer and the word at endoff.
*/
for (c = 0; c < bonuslen; c++)
((uint8_t *)db->db_data)[c] = (uint8_t)(c + bonuslen);
dmu_buf_rele(db, FTAG);
za->za_dbuf = NULL;
/*
* Write to a large offset to increase indirection.
*/
dmu_write(os, object, endoff, sizeof (uint64_t), &object, tx);
dmu_tx_commit(tx);
}
}
/*
* Verify that dmu_{read,write} work as expected.
*/
typedef struct bufwad {
uint64_t bw_index;
uint64_t bw_txg;
uint64_t bw_data;
} bufwad_t;
typedef struct dmu_read_write_dir {
uint64_t dd_packobj;
uint64_t dd_bigobj;
uint64_t dd_chunk;
} dmu_read_write_dir_t;
void
ztest_dmu_read_write(ztest_args_t *za)
{
objset_t *os = za->za_os;
dmu_read_write_dir_t dd;
dmu_tx_t *tx;
int i, freeit, error;
uint64_t n, s, txg;
bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
uint64_t packoff, packsize, bigoff, bigsize;
uint64_t regions = 997;
uint64_t stride = 123456789ULL;
uint64_t width = 40;
int free_percent = 5;
/*
* This test uses two objects, packobj and bigobj, that are always
* updated together (i.e. in the same tx) so that their contents are
* in sync and can be compared. Their contents relate to each other
* in a simple way: packobj is a dense array of 'bufwad' structures,
* while bigobj is a sparse array of the same bufwads. Specifically,
* for any index n, there are three bufwads that should be identical:
*
* packobj, at offset n * sizeof (bufwad_t)
* bigobj, at the head of the nth chunk
* bigobj, at the tail of the nth chunk
*
* The chunk size is arbitrary. It doesn't have to be a power of two,
* and it doesn't have any relation to the object blocksize.
* The only requirement is that it can hold at least two bufwads.
*
* Normally, we write the bufwad to each of these locations.
* However, free_percent of the time we instead write zeroes to
* packobj and perform a dmu_free_range() on bigobj. By comparing
* bigobj to packobj, we can verify that the DMU is correctly
* tracking which parts of an object are allocated and free,
* and that the contents of the allocated blocks are correct.
*/
/*
* Read the directory info. If it's the first time, set things up.
*/
VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
sizeof (dd), &dd, DMU_READ_PREFETCH));
if (dd.dd_chunk == 0) {
ASSERT(dd.dd_packobj == 0);
ASSERT(dd.dd_bigobj == 0);
tx = dmu_tx_create(os);
dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (dd));
dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
error = dmu_tx_assign(tx, TXG_WAIT);
if (error) {
ztest_record_enospc("create r/w directory");
dmu_tx_abort(tx);
return;
}
dd.dd_packobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
DMU_OT_NONE, 0, tx);
dd.dd_bigobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
DMU_OT_NONE, 0, tx);
dd.dd_chunk = (1000 + ztest_random(1000)) * sizeof (uint64_t);
ztest_set_random_blocksize(os, dd.dd_packobj, tx);
ztest_set_random_blocksize(os, dd.dd_bigobj, tx);
dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (dd), &dd,
tx);
dmu_tx_commit(tx);
}
/*
* Prefetch a random chunk of the big object.
* Our aim here is to get some async reads in flight
* for blocks that we may free below; the DMU should
* handle this race correctly.
*/
n = ztest_random(regions) * stride + ztest_random(width);
s = 1 + ztest_random(2 * width - 1);
dmu_prefetch(os, dd.dd_bigobj, n * dd.dd_chunk, s * dd.dd_chunk);
/*
* Pick a random index and compute the offsets into packobj and bigobj.
*/
n = ztest_random(regions) * stride + ztest_random(width);
s = 1 + ztest_random(width - 1);
packoff = n * sizeof (bufwad_t);
packsize = s * sizeof (bufwad_t);
bigoff = n * dd.dd_chunk;
bigsize = s * dd.dd_chunk;
packbuf = umem_alloc(packsize, UMEM_NOFAIL);
bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
/*
* free_percent of the time, free a range of bigobj rather than
* overwriting it.
*/
freeit = (ztest_random(100) < free_percent);
/*
* Read the current contents of our objects.
*/
error = dmu_read(os, dd.dd_packobj, packoff, packsize, packbuf,
DMU_READ_PREFETCH);
ASSERT3U(error, ==, 0);
error = dmu_read(os, dd.dd_bigobj, bigoff, bigsize, bigbuf,
DMU_READ_PREFETCH);
ASSERT3U(error, ==, 0);
/*
* Get a tx for the mods to both packobj and bigobj.
*/
tx = dmu_tx_create(os);
dmu_tx_hold_write(tx, dd.dd_packobj, packoff, packsize);
if (freeit)
dmu_tx_hold_free(tx, dd.dd_bigobj, bigoff, bigsize);
else
dmu_tx_hold_write(tx, dd.dd_bigobj, bigoff, bigsize);
error = dmu_tx_assign(tx, TXG_WAIT);
if (error) {
ztest_record_enospc("dmu r/w range");
dmu_tx_abort(tx);
umem_free(packbuf, packsize);
umem_free(bigbuf, bigsize);
return;
}
txg = dmu_tx_get_txg(tx);
/*
* For each index from n to n + s, verify that the existing bufwad
* in packobj matches the bufwads at the head and tail of the
* corresponding chunk in bigobj. Then update all three bufwads
* with the new values we want to write out.
*/
for (i = 0; i < s; i++) {
/* LINTED */
pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
/* LINTED */
bigH = (bufwad_t *)((char *)bigbuf + i * dd.dd_chunk);
/* LINTED */
bigT = (bufwad_t *)((char *)bigH + dd.dd_chunk) - 1;
ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
if (pack->bw_txg > txg)
fatal(0, "future leak: got %llx, open txg is %llx",
pack->bw_txg, txg);
if (pack->bw_data != 0 && pack->bw_index != n + i)
fatal(0, "wrong index: got %llx, wanted %llx+%llx",
pack->bw_index, n, i);
if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
if (freeit) {
bzero(pack, sizeof (bufwad_t));
} else {
pack->bw_index = n + i;
pack->bw_txg = txg;
pack->bw_data = 1 + ztest_random(-2ULL);
}
*bigH = *pack;
*bigT = *pack;
}
/*
* We've verified all the old bufwads, and made new ones.
* Now write them out.
*/
dmu_write(os, dd.dd_packobj, packoff, packsize, packbuf, tx);
if (freeit) {
if (zopt_verbose >= 6) {
(void) printf("freeing offset %llx size %llx"
" txg %llx\n",
(u_longlong_t)bigoff,
(u_longlong_t)bigsize,
(u_longlong_t)txg);
}
VERIFY(0 == dmu_free_range(os, dd.dd_bigobj, bigoff,
bigsize, tx));
} else {
if (zopt_verbose >= 6) {
(void) printf("writing offset %llx size %llx"
" txg %llx\n",
(u_longlong_t)bigoff,
(u_longlong_t)bigsize,
(u_longlong_t)txg);
}
dmu_write(os, dd.dd_bigobj, bigoff, bigsize, bigbuf, tx);
}
dmu_tx_commit(tx);
/*
* Sanity check the stuff we just wrote.
*/
{
void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
VERIFY(0 == dmu_read(os, dd.dd_packobj, packoff,
packsize, packcheck, DMU_READ_PREFETCH));
VERIFY(0 == dmu_read(os, dd.dd_bigobj, bigoff,
bigsize, bigcheck, DMU_READ_PREFETCH));
ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
umem_free(packcheck, packsize);
umem_free(bigcheck, bigsize);
}
umem_free(packbuf, packsize);
umem_free(bigbuf, bigsize);
}
void
compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
uint64_t bigsize, uint64_t n, dmu_read_write_dir_t dd, uint64_t txg)
{
uint64_t i;
bufwad_t *pack;
bufwad_t *bigH;
bufwad_t *bigT;
/*
* For each index from n to n + s, verify that the existing bufwad
* in packobj matches the bufwads at the head and tail of the
* corresponding chunk in bigobj. Then update all three bufwads
* with the new values we want to write out.
*/
for (i = 0; i < s; i++) {
/* LINTED */
pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
/* LINTED */
bigH = (bufwad_t *)((char *)bigbuf + i * dd.dd_chunk);
/* LINTED */
bigT = (bufwad_t *)((char *)bigH + dd.dd_chunk) - 1;
ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
if (pack->bw_txg > txg)
fatal(0, "future leak: got %llx, open txg is %llx",
pack->bw_txg, txg);
if (pack->bw_data != 0 && pack->bw_index != n + i)
fatal(0, "wrong index: got %llx, wanted %llx+%llx",
pack->bw_index, n, i);
if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
pack->bw_index = n + i;
pack->bw_txg = txg;
pack->bw_data = 1 + ztest_random(-2ULL);
*bigH = *pack;
*bigT = *pack;
}
}
void
ztest_dmu_read_write_zcopy(ztest_args_t *za)
{
objset_t *os = za->za_os;
dmu_read_write_dir_t dd;
dmu_tx_t *tx;
uint64_t i;
int error;
uint64_t n, s, txg;
bufwad_t *packbuf, *bigbuf;
uint64_t packoff, packsize, bigoff, bigsize;
uint64_t regions = 997;
uint64_t stride = 123456789ULL;
uint64_t width = 9;
dmu_buf_t *bonus_db;
arc_buf_t **bigbuf_arcbufs;
dmu_object_info_t *doi = &za->za_doi;
/*
* This test uses two objects, packobj and bigobj, that are always
* updated together (i.e. in the same tx) so that their contents are
* in sync and can be compared. Their contents relate to each other
* in a simple way: packobj is a dense array of 'bufwad' structures,
* while bigobj is a sparse array of the same bufwads. Specifically,
* for any index n, there are three bufwads that should be identical:
*
* packobj, at offset n * sizeof (bufwad_t)
* bigobj, at the head of the nth chunk
* bigobj, at the tail of the nth chunk
*
* The chunk size is set equal to bigobj block size so that
* dmu_assign_arcbuf() can be tested for object updates.
*/
/*
* Read the directory info. If it's the first time, set things up.
*/
VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
sizeof (dd), &dd, DMU_READ_PREFETCH));
if (dd.dd_chunk == 0) {
ASSERT(dd.dd_packobj == 0);
ASSERT(dd.dd_bigobj == 0);
tx = dmu_tx_create(os);
dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (dd));
dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
error = dmu_tx_assign(tx, TXG_WAIT);
if (error) {
ztest_record_enospc("create r/w directory");
dmu_tx_abort(tx);
return;
}
dd.dd_packobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
DMU_OT_NONE, 0, tx);
dd.dd_bigobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
DMU_OT_NONE, 0, tx);
ztest_set_random_blocksize(os, dd.dd_packobj, tx);
ztest_set_random_blocksize(os, dd.dd_bigobj, tx);
VERIFY(dmu_object_info(os, dd.dd_bigobj, doi) == 0);
ASSERT(doi->doi_data_block_size >= 2 * sizeof (bufwad_t));
ASSERT(ISP2(doi->doi_data_block_size));
dd.dd_chunk = doi->doi_data_block_size;
dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (dd), &dd,
tx);
dmu_tx_commit(tx);
} else {
VERIFY(dmu_object_info(os, dd.dd_bigobj, doi) == 0);
VERIFY(ISP2(doi->doi_data_block_size));
VERIFY(dd.dd_chunk == doi->doi_data_block_size);
VERIFY(dd.dd_chunk >= 2 * sizeof (bufwad_t));
}
/*
* Pick a random index and compute the offsets into packobj and bigobj.
*/
n = ztest_random(regions) * stride + ztest_random(width);
s = 1 + ztest_random(width - 1);
packoff = n * sizeof (bufwad_t);
packsize = s * sizeof (bufwad_t);
bigoff = n * dd.dd_chunk;
bigsize = s * dd.dd_chunk;
packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
VERIFY(dmu_bonus_hold(os, dd.dd_bigobj, FTAG, &bonus_db) == 0);
bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
/*
* Iteration 0 test zcopy for DB_UNCACHED dbufs.
* Iteration 1 test zcopy to already referenced dbufs.
* Iteration 2 test zcopy to dirty dbuf in the same txg.
* Iteration 3 test zcopy to dbuf dirty in previous txg.
* Iteration 4 test zcopy when dbuf is no longer dirty.
* Iteration 5 test zcopy when it can't be done.
* Iteration 6 one more zcopy write.
*/
for (i = 0; i < 7; i++) {
uint64_t j;
uint64_t off;
/*
* In iteration 5 (i == 5) use arcbufs
* that don't match bigobj blksz to test
* dmu_assign_arcbuf() when it can't directly
* assign an arcbuf to a dbuf.
*/
for (j = 0; j < s; j++) {
if (i != 5) {
bigbuf_arcbufs[j] =
dmu_request_arcbuf(bonus_db,
dd.dd_chunk);
} else {
bigbuf_arcbufs[2 * j] =
dmu_request_arcbuf(bonus_db,
dd.dd_chunk / 2);
bigbuf_arcbufs[2 * j + 1] =
dmu_request_arcbuf(bonus_db,
dd.dd_chunk / 2);
}
}
/*
* Get a tx for the mods to both packobj and bigobj.
*/
tx = dmu_tx_create(os);
dmu_tx_hold_write(tx, dd.dd_packobj, packoff, packsize);
dmu_tx_hold_write(tx, dd.dd_bigobj, bigoff, bigsize);
if (ztest_random(100) == 0) {
error = -1;
} else {
error = dmu_tx_assign(tx, TXG_WAIT);
}
if (error) {
if (error != -1) {
ztest_record_enospc("dmu r/w range");
}
dmu_tx_abort(tx);
umem_free(packbuf, packsize);
umem_free(bigbuf, bigsize);
for (j = 0; j < s; j++) {
if (i != 5) {
dmu_return_arcbuf(bigbuf_arcbufs[j]);
} else {
dmu_return_arcbuf(
bigbuf_arcbufs[2 * j]);
dmu_return_arcbuf(
bigbuf_arcbufs[2 * j + 1]);
}
}
umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
dmu_buf_rele(bonus_db, FTAG);
return;
}
txg = dmu_tx_get_txg(tx);
/*
* 50% of the time don't read objects in the 1st iteration to
* test dmu_assign_arcbuf() for the case when there're no
* existing dbufs for the specified offsets.
*/
if (i != 0 || ztest_random(2) != 0) {
error = dmu_read(os, dd.dd_packobj, packoff,
packsize, packbuf, DMU_READ_PREFETCH);
ASSERT3U(error, ==, 0);
error = dmu_read(os, dd.dd_bigobj, bigoff, bigsize,
bigbuf, DMU_READ_PREFETCH);
ASSERT3U(error, ==, 0);
}
compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
n, dd, txg);
/*
* We've verified all the old bufwads, and made new ones.
* Now write them out.
*/
dmu_write(os, dd.dd_packobj, packoff, packsize, packbuf, tx);
if (zopt_verbose >= 6) {
(void) printf("writing offset %llx size %llx"
" txg %llx\n",
(u_longlong_t)bigoff,
(u_longlong_t)bigsize,
(u_longlong_t)txg);
}
for (off = bigoff, j = 0; j < s; j++, off += dd.dd_chunk) {
dmu_buf_t *dbt;
if (i != 5) {
bcopy((caddr_t)bigbuf + (off - bigoff),
bigbuf_arcbufs[j]->b_data, dd.dd_chunk);
} else {
bcopy((caddr_t)bigbuf + (off - bigoff),
bigbuf_arcbufs[2 * j]->b_data,
dd.dd_chunk / 2);
bcopy((caddr_t)bigbuf + (off - bigoff) +
dd.dd_chunk / 2,
bigbuf_arcbufs[2 * j + 1]->b_data,
dd.dd_chunk / 2);
}
if (i == 1) {
VERIFY(dmu_buf_hold(os, dd.dd_bigobj, off,
FTAG, &dbt) == 0);
}
if (i != 5) {
dmu_assign_arcbuf(bonus_db, off,
bigbuf_arcbufs[j], tx);
} else {
dmu_assign_arcbuf(bonus_db, off,
bigbuf_arcbufs[2 * j], tx);
dmu_assign_arcbuf(bonus_db,
off + dd.dd_chunk / 2,
bigbuf_arcbufs[2 * j + 1], tx);
}
if (i == 1) {
dmu_buf_rele(dbt, FTAG);
}
}
dmu_tx_commit(tx);
/*
* Sanity check the stuff we just wrote.
*/
{
void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
VERIFY(0 == dmu_read(os, dd.dd_packobj, packoff,
packsize, packcheck, DMU_READ_PREFETCH));
VERIFY(0 == dmu_read(os, dd.dd_bigobj, bigoff,
bigsize, bigcheck, DMU_READ_PREFETCH));
ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
umem_free(packcheck, packsize);
umem_free(bigcheck, bigsize);
}
if (i == 2) {
txg_wait_open(dmu_objset_pool(os), 0);
} else if (i == 3) {
txg_wait_synced(dmu_objset_pool(os), 0);
}
}
dmu_buf_rele(bonus_db, FTAG);
umem_free(packbuf, packsize);
umem_free(bigbuf, bigsize);
umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
}
void
ztest_dmu_check_future_leak(ztest_args_t *za)
{
objset_t *os = za->za_os;
dmu_buf_t *db;
ztest_block_tag_t *bt;
dmu_object_info_t *doi = &za->za_doi;
/*
* Make sure that, if there is a write record in the bonus buffer
* of the ZTEST_DIROBJ, that the txg for this record is <= the
* last synced txg of the pool.
*/
VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0);
za->za_dbuf = db;
VERIFY(dmu_object_info(os, ZTEST_DIROBJ, doi) == 0);
ASSERT3U(doi->doi_bonus_size, >=, sizeof (*bt));
ASSERT3U(doi->doi_bonus_size, <=, db->db_size);
ASSERT3U(doi->doi_bonus_size % sizeof (*bt), ==, 0);
bt = (void *)((char *)db->db_data + doi->doi_bonus_size - sizeof (*bt));
if (bt->bt_objset != 0) {
ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
ASSERT3U(bt->bt_object, ==, ZTEST_DIROBJ);
ASSERT3U(bt->bt_offset, ==, -1ULL);
ASSERT3U(bt->bt_txg, <, spa_first_txg(za->za_spa));
}
dmu_buf_rele(db, FTAG);
za->za_dbuf = NULL;
}
void
ztest_dmu_write_parallel(ztest_args_t *za)
{
objset_t *os = za->za_os;
ztest_block_tag_t *rbt = &za->za_rbt;
ztest_block_tag_t *wbt = &za->za_wbt;
const size_t btsize = sizeof (ztest_block_tag_t);
dmu_buf_t *db;
int b, error;
int bs = ZTEST_DIROBJ_BLOCKSIZE;
int do_free = 0;
uint64_t off, txg, txg_how;
kmutex_t *lp;
char osname[MAXNAMELEN];
char iobuf[SPA_MAXBLOCKSIZE];
blkptr_t blk;
uint64_t blkoff;
zbookmark_t zb;
dmu_tx_t *tx = dmu_tx_create(os);
dmu_buf_t *bonus_db;
arc_buf_t *abuf = NULL;
bzero(&blk, sizeof(blkptr_t));
dmu_objset_name(os, osname);
/*
* Have multiple threads write to large offsets in ZTEST_DIROBJ
* to verify that having multiple threads writing to the same object
* in parallel doesn't cause any trouble.
*/
if (ztest_random(4) == 0) {
/*
* Do the bonus buffer instead of a regular block.
* We need a lock to serialize resize vs. others,
* so we hash on the objset ID.
*/
b = dmu_objset_id(os) % ZTEST_SYNC_LOCKS;
off = -1ULL;
dmu_tx_hold_bonus(tx, ZTEST_DIROBJ);
} else {
b = ztest_random(ZTEST_SYNC_LOCKS);
off = za->za_diroff_shared + (b << SPA_MAXBLOCKSHIFT);
if (ztest_random(4) == 0) {
do_free = 1;
dmu_tx_hold_free(tx, ZTEST_DIROBJ, off, bs);
} else {
dmu_tx_hold_write(tx, ZTEST_DIROBJ, off, bs);
}
}
if (off != -1ULL && P2PHASE(off, bs) == 0 && !do_free &&
ztest_random(8) == 0) {
VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &bonus_db) == 0);
abuf = dmu_request_arcbuf(bonus_db, bs);
}
txg_how = ztest_random(2) == 0 ? TXG_WAIT : TXG_NOWAIT;
error = dmu_tx_assign(tx, txg_how);
if (error) {
if (error == ERESTART) {
ASSERT(txg_how == TXG_NOWAIT);
dmu_tx_wait(tx);
} else {
ztest_record_enospc("dmu write parallel");
}
dmu_tx_abort(tx);
if (abuf != NULL) {
dmu_return_arcbuf(abuf);
dmu_buf_rele(bonus_db, FTAG);
}
return;
}
txg = dmu_tx_get_txg(tx);
lp = &ztest_shared->zs_sync_lock[b];
mutex_enter(lp);
wbt->bt_objset = dmu_objset_id(os);
wbt->bt_object = ZTEST_DIROBJ;
wbt->bt_offset = off;
wbt->bt_txg = txg;
wbt->bt_thread = za->za_instance;
wbt->bt_seq = ztest_shared->zs_seq[b]++; /* protected by lp */
/*
* Occasionally, write an all-zero block to test the behavior
* of blocks that compress into holes.
*/
if (off != -1ULL && ztest_random(8) == 0)
bzero(wbt, btsize);
if (off == -1ULL) {
dmu_object_info_t *doi = &za->za_doi;
char *dboff;
VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0);
za->za_dbuf = db;
dmu_object_info_from_db(db, doi);
ASSERT3U(doi->doi_bonus_size, <=, db->db_size);
ASSERT3U(doi->doi_bonus_size, >=, btsize);
ASSERT3U(doi->doi_bonus_size % btsize, ==, 0);
dboff = (char *)db->db_data + doi->doi_bonus_size - btsize;
bcopy(dboff, rbt, btsize);
if (rbt->bt_objset != 0) {
ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset);
ASSERT3U(rbt->bt_object, ==, wbt->bt_object);
ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset);
ASSERT3U(rbt->bt_txg, <=, wbt->bt_txg);
}
if (ztest_random(10) == 0) {
int newsize = (ztest_random(db->db_size /
btsize) + 1) * btsize;
ASSERT3U(newsize, >=, btsize);
ASSERT3U(newsize, <=, db->db_size);
VERIFY3U(dmu_set_bonus(db, newsize, tx), ==, 0);
dboff = (char *)db->db_data + newsize - btsize;
}
dmu_buf_will_dirty(db, tx);
bcopy(wbt, dboff, btsize);
dmu_buf_rele(db, FTAG);
za->za_dbuf = NULL;
} else if (do_free) {
VERIFY(dmu_free_range(os, ZTEST_DIROBJ, off, bs, tx) == 0);
} else if (abuf == NULL) {
dmu_write(os, ZTEST_DIROBJ, off, btsize, wbt, tx);
} else {
bcopy(wbt, abuf->b_data, btsize);
dmu_assign_arcbuf(bonus_db, off, abuf, tx);
dmu_buf_rele(bonus_db, FTAG);
}
mutex_exit(lp);
if (ztest_random(1000) == 0)
(void) poll(NULL, 0, 1); /* open dn_notxholds window */
dmu_tx_commit(tx);
if (ztest_random(10000) == 0)
txg_wait_synced(dmu_objset_pool(os), txg);
if (off == -1ULL || do_free)
return;
if (ztest_random(2) != 0)
return;
/*
* dmu_sync() the block we just wrote.
*/
mutex_enter(lp);
blkoff = P2ALIGN_TYPED(off, bs, uint64_t);
error = dmu_buf_hold(os, ZTEST_DIROBJ, blkoff, FTAG, &db);
za->za_dbuf = db;
if (error) {
mutex_exit(lp);
return;
}
blkoff = off - blkoff;
error = dmu_sync(NULL, db, &blk, txg, NULL, NULL);
dmu_buf_rele(db, FTAG);
za->za_dbuf = NULL;
if (error) {
mutex_exit(lp);
return;
}
if (blk.blk_birth == 0) { /* concurrent free */
mutex_exit(lp);
return;
}
txg_suspend(dmu_objset_pool(os));
mutex_exit(lp);
ASSERT(blk.blk_fill == 1);
ASSERT3U(BP_GET_TYPE(&blk), ==, DMU_OT_UINT64_OTHER);
ASSERT3U(BP_GET_LEVEL(&blk), ==, 0);
ASSERT3U(BP_GET_LSIZE(&blk), ==, bs);
/*
* Read the block that dmu_sync() returned to make sure its contents
* match what we wrote. We do this while still txg_suspend()ed
* to ensure that the block can't be reused before we read it.
*/
zb.zb_objset = dmu_objset_id(os);
zb.zb_object = ZTEST_DIROBJ;
zb.zb_level = 0;
zb.zb_blkid = off / bs;
error = zio_wait(zio_read(NULL, za->za_spa, &blk, iobuf, bs,
NULL, NULL, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_MUSTSUCCEED, &zb));
ASSERT3U(error, ==, 0);
txg_resume(dmu_objset_pool(os));
bcopy(&iobuf[blkoff], rbt, btsize);
if (rbt->bt_objset == 0) /* concurrent free */
return;
if (wbt->bt_objset == 0) /* all-zero overwrite */
return;
ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset);
ASSERT3U(rbt->bt_object, ==, wbt->bt_object);
ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset);
/*
* The semantic of dmu_sync() is that we always push the most recent
* version of the data, so in the face of concurrent updates we may
* see a newer version of the block. That's OK.
*/
ASSERT3U(rbt->bt_txg, >=, wbt->bt_txg);
if (rbt->bt_thread == wbt->bt_thread)
ASSERT3U(rbt->bt_seq, ==, wbt->bt_seq);
else
ASSERT3U(rbt->bt_seq, >, wbt->bt_seq);
}
/*
* Verify that zap_{create,destroy,add,remove,update} work as expected.
*/
#define ZTEST_ZAP_MIN_INTS 1
#define ZTEST_ZAP_MAX_INTS 4
#define ZTEST_ZAP_MAX_PROPS 1000
void
ztest_zap(ztest_args_t *za)
{
objset_t *os = za->za_os;
uint64_t object;
uint64_t txg, last_txg;
uint64_t value[ZTEST_ZAP_MAX_INTS];
uint64_t zl_ints, zl_intsize, prop;
int i, ints;
dmu_tx_t *tx;
char propname[100], txgname[100];
int error;
char osname[MAXNAMELEN];
char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
dmu_objset_name(os, osname);
/*
* Create a new object if necessary, and record it in the directory.
*/
VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
sizeof (uint64_t), &object, DMU_READ_PREFETCH));
if (object == 0) {
tx = dmu_tx_create(os);
dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff,
sizeof (uint64_t));
dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL);
error = dmu_tx_assign(tx, TXG_WAIT);
if (error) {
ztest_record_enospc("create zap test obj");
dmu_tx_abort(tx);
return;
}
object = zap_create(os, DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx);
if (error) {
fatal(0, "zap_create('%s', %llu) = %d",
osname, object, error);
}
ASSERT(object != 0);
dmu_write(os, ZTEST_DIROBJ, za->za_diroff,
sizeof (uint64_t), &object, tx);
/*
* Generate a known hash collision, and verify that
* we can lookup and remove both entries.
*/
for (i = 0; i < 2; i++) {
value[i] = i;
error = zap_add(os, object, hc[i], sizeof (uint64_t),
1, &value[i], tx);
ASSERT3U(error, ==, 0);
}
for (i = 0; i < 2; i++) {
error = zap_add(os, object, hc[i], sizeof (uint64_t),
1, &value[i], tx);
ASSERT3U(error, ==, EEXIST);
error = zap_length(os, object, hc[i],
&zl_intsize, &zl_ints);
ASSERT3U(error, ==, 0);
ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
ASSERT3U(zl_ints, ==, 1);
}
for (i = 0; i < 2; i++) {
error = zap_remove(os, object, hc[i], tx);
ASSERT3U(error, ==, 0);
}
dmu_tx_commit(tx);
}
ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
bzero(value, sizeof (value));
last_txg = 0;
/*
* If these zap entries already exist, validate their contents.
*/
error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
if (error == 0) {
ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
ASSERT3U(zl_ints, ==, 1);
VERIFY(zap_lookup(os, object, txgname, zl_intsize,
zl_ints, &last_txg) == 0);
VERIFY(zap_length(os, object, propname, &zl_intsize,
&zl_ints) == 0);
ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
ASSERT3U(zl_ints, ==, ints);
VERIFY(zap_lookup(os, object, propname, zl_intsize,
zl_ints, value) == 0);
for (i = 0; i < ints; i++) {
ASSERT3U(value[i], ==, last_txg + object + i);
}
} else {
ASSERT3U(error, ==, ENOENT);
}
/*
* Atomically update two entries in our zap object.
* The first is named txg_%llu, and contains the txg
* in which the property was last updated. The second
* is named prop_%llu, and the nth element of its value
* should be txg + object + n.
*/
tx = dmu_tx_create(os);
dmu_tx_hold_zap(tx, object, TRUE, NULL);
error = dmu_tx_assign(tx, TXG_WAIT);
if (error) {
ztest_record_enospc("create zap entry");
dmu_tx_abort(tx);
return;
}
txg = dmu_tx_get_txg(tx);
if (last_txg > txg)
fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
for (i = 0; i < ints; i++)
value[i] = txg + object + i;
error = zap_update(os, object, txgname, sizeof (uint64_t), 1, &txg, tx);
if (error)
fatal(0, "zap_update('%s', %llu, '%s') = %d",
osname, object, txgname, error);
error = zap_update(os, object, propname, sizeof (uint64_t),
ints, value, tx);
if (error)
fatal(0, "zap_update('%s', %llu, '%s') = %d",
osname, object, propname, error);
dmu_tx_commit(tx);
/*
* Remove a random pair of entries.
*/
prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
if (error == ENOENT)
return;
ASSERT3U(error, ==, 0);
tx = dmu_tx_create(os);
dmu_tx_hold_zap(tx, object, TRUE, NULL);
error = dmu_tx_assign(tx, TXG_WAIT);
if (error) {
ztest_record_enospc("remove zap entry");
dmu_tx_abort(tx);
return;
}
error = zap_remove(os, object, txgname, tx);
if (error)
fatal(0, "zap_remove('%s', %llu, '%s') = %d",
osname, object, txgname, error);
error = zap_remove(os, object, propname, tx);
if (error)
fatal(0, "zap_remove('%s', %llu, '%s') = %d",
osname, object, propname, error);
dmu_tx_commit(tx);
/*
* Once in a while, destroy the object.
*/
if (ztest_random(1000) != 0)
return;
tx = dmu_tx_create(os);
dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t));
dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
error = dmu_tx_assign(tx, TXG_WAIT);
if (error) {
ztest_record_enospc("destroy zap object");
dmu_tx_abort(tx);
return;
}
error = zap_destroy(os, object, tx);
if (error)
fatal(0, "zap_destroy('%s', %llu) = %d",
osname, object, error);
object = 0;
dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t),
&object, tx);
dmu_tx_commit(tx);
}
void
ztest_zap_parallel(ztest_args_t *za)
{
objset_t *os = za->za_os;
uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
dmu_tx_t *tx;
int i, namelen, error;
char name[20], string_value[20];
void *data;
/*
* Generate a random name of the form 'xxx.....' where each
* x is a random printable character and the dots are dots.
* There are 94 such characters, and the name length goes from
* 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
*/
namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
for (i = 0; i < 3; i++)
name[i] = '!' + ztest_random('~' - '!' + 1);
for (; i < namelen - 1; i++)
name[i] = '.';
name[i] = '\0';
if (ztest_random(2) == 0)
object = ZTEST_MICROZAP_OBJ;
else
object = ZTEST_FATZAP_OBJ;
if ((namelen & 1) || object == ZTEST_MICROZAP_OBJ) {
wsize = sizeof (txg);
wc = 1;
data = &txg;
} else {
wsize = 1;
wc = namelen;
data = string_value;
}
count = -1ULL;
VERIFY(zap_count(os, object, &count) == 0);
ASSERT(count != -1ULL);
/*
* Select an operation: length, lookup, add, update, remove.
*/
i = ztest_random(5);
if (i >= 2) {
tx = dmu_tx_create(os);
dmu_tx_hold_zap(tx, object, TRUE, NULL);
error = dmu_tx_assign(tx, TXG_WAIT);
if (error) {
ztest_record_enospc("zap parallel");
dmu_tx_abort(tx);
return;
}
txg = dmu_tx_get_txg(tx);
bcopy(name, string_value, namelen);
} else {
tx = NULL;
txg = 0;
bzero(string_value, namelen);
}
switch (i) {
case 0:
error = zap_length(os, object, name, &zl_wsize, &zl_wc);
if (error == 0) {
ASSERT3U(wsize, ==, zl_wsize);
ASSERT3U(wc, ==, zl_wc);
} else {
ASSERT3U(error, ==, ENOENT);
}
break;
case 1:
error = zap_lookup(os, object, name, wsize, wc, data);
if (error == 0) {
if (data == string_value &&
bcmp(name, data, namelen) != 0)
fatal(0, "name '%s' != val '%s' len %d",
name, data, namelen);
} else {
ASSERT3U(error, ==, ENOENT);
}
break;
case 2:
error = zap_add(os, object, name, wsize, wc, data, tx);
ASSERT(error == 0 || error == EEXIST);
break;
case 3:
VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
break;
case 4:
error = zap_remove(os, object, name, tx);
ASSERT(error == 0 || error == ENOENT);
break;
}
if (tx != NULL)
dmu_tx_commit(tx);
}
/*
* Commit callback data.
*/
typedef struct ztest_cb_data {
list_node_t zcd_node;
ztest_cb_list_t *zcd_zcl;
uint64_t zcd_txg;
int zcd_expected_err;
boolean_t zcd_added;
boolean_t zcd_called;
spa_t *zcd_spa;
} ztest_cb_data_t;
static void
ztest_commit_callback(void *arg, int error)
{
ztest_cb_data_t *data = arg;
ztest_cb_list_t *zcl;
uint64_t synced_txg;
VERIFY(data != NULL);
VERIFY3S(data->zcd_expected_err, ==, error);
VERIFY(!data->zcd_called);
synced_txg = spa_last_synced_txg(data->zcd_spa);
if (data->zcd_txg > synced_txg)
fatal(0, "commit callback of txg %llu prematurely called, last"
" synced txg = %llu\n", data->zcd_txg, synced_txg);
zcl = data->zcd_zcl;
data->zcd_called = B_TRUE;
if (error == ECANCELED) {
ASSERT3U(data->zcd_txg, ==, 0);
ASSERT(!data->zcd_added);
/*
* The private callback data should be destroyed here, but
* since we are going to check the zcd_called field after
* dmu_tx_abort(), we will destroy it there.
*/
return;
}
if (!data->zcd_added)
goto out;
ASSERT3U(data->zcd_txg, !=, 0);
/* Remove our callback from the list */
mutex_enter(&zcl->zcl_callbacks_lock);
list_remove(&zcl->zcl_callbacks, data);
mutex_exit(&zcl->zcl_callbacks_lock);
out:
umem_free(data, sizeof (ztest_cb_data_t));
}
static ztest_cb_data_t *
ztest_create_cb_data(objset_t *os, ztest_cb_list_t *zcl, uint64_t txg)
{
ztest_cb_data_t *cb_data;
cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
cb_data->zcd_zcl = zcl;
cb_data->zcd_txg = txg;
cb_data->zcd_spa = os->os->os_spa;
return (cb_data);
}
/*
* If a number of txgs equal to this threshold have been created after a commit
* callback has been registered but not called, then we assume there is an
* implementation bug.
*/
#define ZTEST_COMMIT_CALLBACK_THRESH 3
/*
* Commit callback test.
*/
void
ztest_dmu_commit_callbacks(ztest_args_t *za)
{
objset_t *os = za->za_os;
dmu_tx_t *tx;
ztest_cb_list_t *zcl = &ztest_shared->zs_cb_list;
ztest_cb_data_t *cb_data[3], *tmp_cb;
uint64_t old_txg, txg;
int i, error = 0;
tx = dmu_tx_create(os);
cb_data[0] = ztest_create_cb_data(os, zcl, 0);
dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t));
/* Every once in a while, abort the transaction on purpose */
if (ztest_random(100) == 0)
error = -1;
if (!error)
error = dmu_tx_assign(tx, TXG_NOWAIT);
txg = error ? 0 : dmu_tx_get_txg(tx);
cb_data[1] = ztest_create_cb_data(os, zcl, txg);
dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
if (error) {
/*
* It's not a strict requirement to call the registered
* callbacks from inside dmu_tx_abort(), but that's what
* happens in the current implementation so we will check for
* that.
*/
for (i = 0; i < 2; i++) {
cb_data[i]->zcd_expected_err = ECANCELED;
VERIFY(!cb_data[i]->zcd_called);
}
dmu_tx_abort(tx);
for (i = 0; i < 2; i++) {
VERIFY(cb_data[i]->zcd_called);
umem_free(cb_data[i], sizeof (ztest_cb_data_t));
}
return;
}
cb_data[0]->zcd_txg = txg;
cb_data[2] = ztest_create_cb_data(os, zcl, txg);
dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
/*
* Read existing data to make sure there isn't a future leak.
*/
VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
sizeof (uint64_t), &old_txg, DMU_READ_PREFETCH));
if (old_txg > txg)
fatal(0, "future leak: got %llx, open txg is %llx", old_txg,
txg);
dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t), &txg, tx);
mutex_enter(&zcl->zcl_callbacks_lock);
/*
* Since commit callbacks don't have any ordering requirement and since
* it is theoretically possible for a commit callback to be called
* after an arbitrary amount of time has elapsed since its txg has been
* synced, it is difficult to reliably determine whether a commit
* callback hasn't been called due to high load or due to a flawed
* implementation.
*
* In practice, we will assume that if after a certain number of txgs a
* commit callback hasn't been called, then most likely there's an
* implementation bug..
*/
tmp_cb = list_head(&zcl->zcl_callbacks);
if (tmp_cb != NULL)
VERIFY3U(tmp_cb->zcd_txg, >,
txg - ZTEST_COMMIT_CALLBACK_THRESH);
/*
* Let's find the place to insert our callbacks.
*
* Even though the list is ordered by txg, it is possible for the
* insertion point to not be the end because our txg may already be
* quiescing at this point and other callbacks in the open txg may
* have sneaked in.
*/
tmp_cb = list_tail(&zcl->zcl_callbacks);
while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
tmp_cb = list_prev(&zcl->zcl_callbacks, tmp_cb);
/* Add the 3 callbacks to the list */
for (i = 0; i < 3; i++) {
if (tmp_cb == NULL)
list_insert_head(&zcl->zcl_callbacks, cb_data[i]);
else
list_insert_after(&zcl->zcl_callbacks, tmp_cb,
cb_data[i]);
cb_data[i]->zcd_added = B_TRUE;
VERIFY(!cb_data[i]->zcd_called);
tmp_cb = cb_data[i];
}
mutex_exit(&zcl->zcl_callbacks_lock);
dmu_tx_commit(tx);
}
void
ztest_dsl_prop_get_set(ztest_args_t *za)
{
objset_t *os = za->za_os;
int i, inherit;
uint64_t value;
const char *prop, *valname;
char setpoint[MAXPATHLEN];
char osname[MAXNAMELEN];
int error;
rw_enter(&ztest_shared->zs_name_lock, RW_READER);
dmu_objset_name(os, osname);
for (i = 0; i < 2; i++) {
if (i == 0) {
prop = "checksum";
value = ztest_random_checksum();
inherit = (value == ZIO_CHECKSUM_INHERIT);
} else {
prop = "compression";
value = ztest_random_compress();
inherit = (value == ZIO_COMPRESS_INHERIT);
}
error = dsl_prop_set(osname, prop, sizeof (value),
!inherit, &value);
if (error == ENOSPC) {
ztest_record_enospc("dsl_prop_set");
break;
}
ASSERT3U(error, ==, 0);
VERIFY3U(dsl_prop_get(osname, prop, sizeof (value),
1, &value, setpoint), ==, 0);
if (i == 0)
valname = zio_checksum_table[value].ci_name;
else
valname = zio_compress_table[value].ci_name;
if (zopt_verbose >= 6) {
(void) printf("%s %s = %s for '%s'\n",
osname, prop, valname, setpoint);
}
}
rw_exit(&ztest_shared->zs_name_lock);
}
/*
* Inject random faults into the on-disk data.
*/
void
ztest_fault_inject(ztest_args_t *za)
{
int fd;
uint64_t offset;
uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
uint64_t bad = 0x1990c0ffeedecadeull;
uint64_t top, leaf;
char path0[MAXPATHLEN];
char pathrand[MAXPATHLEN];
size_t fsize;
spa_t *spa = za->za_spa;
int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
int iters = 1000;
int maxfaults = zopt_maxfaults;
vdev_t *vd0 = NULL;
uint64_t guid0 = 0;
ASSERT(leaves >= 1);
/*
* We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
*/
spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
if (ztest_random(2) == 0) {
/*
* Inject errors on a normal data device.
*/
top = ztest_random(spa->spa_root_vdev->vdev_children);
leaf = ztest_random(leaves);
/*
* Generate paths to the first leaf in this top-level vdev,
* and to the random leaf we selected. We'll induce transient
* write failures and random online/offline activity on leaf 0,
* and we'll write random garbage to the randomly chosen leaf.
*/
(void) snprintf(path0, sizeof (path0), ztest_dev_template,
zopt_dir, zopt_pool, top * leaves + 0);
(void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
zopt_dir, zopt_pool, top * leaves + leaf);
vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
if (vd0 != NULL && maxfaults != 1) {
/*
* Make vd0 explicitly claim to be unreadable,
* or unwriteable, or reach behind its back
* and close the underlying fd. We can do this if
* maxfaults == 0 because we'll fail and reexecute,
* and we can do it if maxfaults >= 2 because we'll
* have enough redundancy. If maxfaults == 1, the
* combination of this with injection of random data
* corruption below exceeds the pool's fault tolerance.
*/
vdev_file_t *vf = vd0->vdev_tsd;
if (vf != NULL && ztest_random(3) == 0) {
(void) close(vf->vf_vnode->v_fd);
vf->vf_vnode->v_fd = -1;
} else if (ztest_random(2) == 0) {
vd0->vdev_cant_read = B_TRUE;
} else {
vd0->vdev_cant_write = B_TRUE;
}
guid0 = vd0->vdev_guid;
}
} else {
/*
* Inject errors on an l2cache device.
*/
spa_aux_vdev_t *sav = &spa->spa_l2cache;
if (sav->sav_count == 0) {
spa_config_exit(spa, SCL_STATE, FTAG);
return;
}
vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
guid0 = vd0->vdev_guid;
(void) strcpy(path0, vd0->vdev_path);
(void) strcpy(pathrand, vd0->vdev_path);
leaf = 0;
leaves = 1;
maxfaults = INT_MAX; /* no limit on cache devices */
}
spa_config_exit(spa, SCL_STATE, FTAG);
if (maxfaults == 0)
return;
/*
* If we can tolerate two or more faults, randomly online/offline vd0.
*/
if (maxfaults >= 2 && guid0 != 0) {
if (ztest_random(10) < 6) {
int flags = (ztest_random(2) == 0 ?
ZFS_OFFLINE_TEMPORARY : 0);
VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
} else {
(void) vdev_online(spa, guid0, 0, NULL);
}
}
/*
* We have at least single-fault tolerance, so inject data corruption.
*/
fd = open(pathrand, O_RDWR);
if (fd == -1) /* we hit a gap in the device namespace */
return;
fsize = lseek(fd, 0, SEEK_END);
while (--iters != 0) {
offset = ztest_random(fsize / (leaves << bshift)) *
(leaves << bshift) + (leaf << bshift) +
(ztest_random(1ULL << (bshift - 1)) & -8ULL);
if (offset >= fsize)
continue;
if (zopt_verbose >= 6)
(void) printf("injecting bad word into %s,"
" offset 0x%llx\n", pathrand, (u_longlong_t)offset);
if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
fatal(1, "can't inject bad word at 0x%llx in %s",
offset, pathrand);
}
(void) close(fd);
}
/*
* Scrub the pool.
*/
void
ztest_scrub(ztest_args_t *za)
{
spa_t *spa = za->za_spa;
(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
(void) poll(NULL, 0, 1000); /* wait a second, then force a restart */
(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
}
/*
* Rename the pool to a different name and then rename it back.
*/
void
ztest_spa_rename(ztest_args_t *za)
{
char *oldname, *newname;
int error;
spa_t *spa;
rw_enter(&ztest_shared->zs_name_lock, RW_WRITER);
oldname = za->za_pool;
newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
(void) strcpy(newname, oldname);
(void) strcat(newname, "_tmp");
/*
* Do the rename
*/
error = spa_rename(oldname, newname);
if (error)
fatal(0, "spa_rename('%s', '%s') = %d", oldname,
newname, error);
/*
* Try to open it under the old name, which shouldn't exist
*/
error = spa_open(oldname, &spa, FTAG);
if (error != ENOENT)
fatal(0, "spa_open('%s') = %d", oldname, error);
/*
* Open it under the new name and make sure it's still the same spa_t.
*/
error = spa_open(newname, &spa, FTAG);
if (error != 0)
fatal(0, "spa_open('%s') = %d", newname, error);
ASSERT(spa == za->za_spa);
spa_close(spa, FTAG);
/*
* Rename it back to the original
*/
error = spa_rename(newname, oldname);
if (error)
fatal(0, "spa_rename('%s', '%s') = %d", newname,
oldname, error);
/*
* Make sure it can still be opened
*/
error = spa_open(oldname, &spa, FTAG);
if (error != 0)
fatal(0, "spa_open('%s') = %d", oldname, error);
ASSERT(spa == za->za_spa);
spa_close(spa, FTAG);
umem_free(newname, strlen(newname) + 1);
rw_exit(&ztest_shared->zs_name_lock);
}
/*
* Completely obliterate one disk.
*/
static void
ztest_obliterate_one_disk(uint64_t vdev)
{
int fd;
char dev_name[MAXPATHLEN], copy_name[MAXPATHLEN];
size_t fsize;
if (zopt_maxfaults < 2)
return;
(void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
(void) snprintf(copy_name, MAXPATHLEN, "%s.old", dev_name);
fd = open(dev_name, O_RDWR);
if (fd == -1)
fatal(1, "can't open %s", dev_name);
/*
* Determine the size.
*/
fsize = lseek(fd, 0, SEEK_END);
(void) close(fd);
/*
* Rename the old device to dev_name.old (useful for debugging).
*/
VERIFY(rename(dev_name, copy_name) == 0);
/*
* Create a new one.
*/
VERIFY((fd = open(dev_name, O_RDWR | O_CREAT | O_TRUNC, 0666)) >= 0);
VERIFY(ftruncate(fd, fsize) == 0);
(void) close(fd);
}
static void
ztest_replace_one_disk(spa_t *spa, uint64_t vdev)
{
char dev_name[MAXPATHLEN];
nvlist_t *root;
int error;
uint64_t guid;
vdev_t *vd;
(void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
/*
* Build the nvlist describing dev_name.
*/
root = make_vdev_root(dev_name, NULL, 0, 0, 0, 0, 0, 1);
spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
if ((vd = vdev_lookup_by_path(spa->spa_root_vdev, dev_name)) == NULL)
guid = 0;
else
guid = vd->vdev_guid;
spa_config_exit(spa, SCL_VDEV, FTAG);
error = spa_vdev_attach(spa, guid, root, B_TRUE);
if (error != 0 &&
error != EBUSY &&
error != ENOTSUP &&
error != ENODEV &&
error != EDOM)
fatal(0, "spa_vdev_attach(in-place) = %d", error);
nvlist_free(root);
}
static void
ztest_verify_blocks(char *pool)
{
int status;
char zdb[MAXPATHLEN + MAXNAMELEN + 20];
char zbuf[1024];
char *bin;
char *ztest;
char *isa;
int isalen;
FILE *fp;
(void) realpath(getexecname(), zdb);
/* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
bin = strstr(zdb, "/usr/bin/");
ztest = strstr(bin, "/ztest");
isa = bin + 8;
isalen = ztest - isa;
isa = strdup(isa);
/* LINTED */
(void) sprintf(bin,
"/usr/sbin%.*s/zdb -bcc%s%s -U /tmp/zpool.cache %s",
isalen,
isa,
zopt_verbose >= 3 ? "s" : "",
zopt_verbose >= 4 ? "v" : "",
pool);
free(isa);
if (zopt_verbose >= 5)
(void) printf("Executing %s\n", strstr(zdb, "zdb "));
fp = popen(zdb, "r");
while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
if (zopt_verbose >= 3)
(void) printf("%s", zbuf);
status = pclose(fp);
if (status == 0)
return;
ztest_dump_core = 0;
if (WIFEXITED(status))
fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
else
fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
}
static void
ztest_walk_pool_directory(char *header)
{
spa_t *spa = NULL;
if (zopt_verbose >= 6)
(void) printf("%s\n", header);
mutex_enter(&spa_namespace_lock);
while ((spa = spa_next(spa)) != NULL)
if (zopt_verbose >= 6)
(void) printf("\t%s\n", spa_name(spa));
mutex_exit(&spa_namespace_lock);
}
static void
ztest_spa_import_export(char *oldname, char *newname)
{
nvlist_t *config, *newconfig;
uint64_t pool_guid;
spa_t *spa;
int error;
if (zopt_verbose >= 4) {
(void) printf("import/export: old = %s, new = %s\n",
oldname, newname);
}
/*
* Clean up from previous runs.
*/
(void) spa_destroy(newname);
/*
* Get the pool's configuration and guid.
*/
error = spa_open(oldname, &spa, FTAG);
if (error)
fatal(0, "spa_open('%s') = %d", oldname, error);
/*
* Kick off a scrub to tickle scrub/export races.
*/
if (ztest_random(2) == 0)
(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
pool_guid = spa_guid(spa);
spa_close(spa, FTAG);
ztest_walk_pool_directory("pools before export");
/*
* Export it.
*/
error = spa_export(oldname, &config, B_FALSE, B_FALSE);
if (error)
fatal(0, "spa_export('%s') = %d", oldname, error);
ztest_walk_pool_directory("pools after export");
/*
* Try to import it.
*/
newconfig = spa_tryimport(config);
ASSERT(newconfig != NULL);
nvlist_free(newconfig);
/*
* Import it under the new name.
*/
error = spa_import(newname, config, NULL);
if (error)
fatal(0, "spa_import('%s') = %d", newname, error);
ztest_walk_pool_directory("pools after import");
/*
* Try to import it again -- should fail with EEXIST.
*/
error = spa_import(newname, config, NULL);
if (error != EEXIST)
fatal(0, "spa_import('%s') twice", newname);
/*
* Try to import it under a different name -- should fail with EEXIST.
*/
error = spa_import(oldname, config, NULL);
if (error != EEXIST)
fatal(0, "spa_import('%s') under multiple names", newname);
/*
* Verify that the pool is no longer visible under the old name.
*/
error = spa_open(oldname, &spa, FTAG);
if (error != ENOENT)
fatal(0, "spa_open('%s') = %d", newname, error);
/*
* Verify that we can open and close the pool using the new name.
*/
error = spa_open(newname, &spa, FTAG);
if (error)
fatal(0, "spa_open('%s') = %d", newname, error);
ASSERT(pool_guid == spa_guid(spa));
spa_close(spa, FTAG);
nvlist_free(config);
}
static void
ztest_resume(spa_t *spa)
{
if (spa_suspended(spa)) {
spa_vdev_state_enter(spa);
vdev_clear(spa, NULL);
(void) spa_vdev_state_exit(spa, NULL, 0);
(void) zio_resume(spa);
}
}
static void *
ztest_resume_thread(void *arg)
{
spa_t *spa = arg;
while (!ztest_exiting) {
(void) poll(NULL, 0, 1000);
ztest_resume(spa);
}
thread_exit();
return (NULL);
}
static void *
ztest_thread(void *arg)
{
ztest_args_t *za = arg;
ztest_shared_t *zs = ztest_shared;
hrtime_t now, functime;
ztest_info_t *zi;
int f, i;
while ((now = gethrtime()) < za->za_stop) {
/*
* See if it's time to force a crash.
*/
if (now > za->za_kill) {
zs->zs_alloc = spa_get_alloc(za->za_spa);
zs->zs_space = spa_get_space(za->za_spa);
(void) kill(getpid(), SIGKILL);
}
/*
* Pick a random function.
*/
f = ztest_random(ZTEST_FUNCS);
zi = &zs->zs_info[f];
/*
* Decide whether to call it, based on the requested frequency.
*/
if (zi->zi_call_target == 0 ||
(double)zi->zi_call_total / zi->zi_call_target >
(double)(now - zs->zs_start_time) / (zopt_time * NANOSEC))
continue;
atomic_add_64(&zi->zi_calls, 1);
atomic_add_64(&zi->zi_call_total, 1);
za->za_diroff = (za->za_instance * ZTEST_FUNCS + f) *
ZTEST_DIRSIZE;
za->za_diroff_shared = (1ULL << 63);
for (i = 0; i < zi->zi_iters; i++)
zi->zi_func(za);
functime = gethrtime() - now;
atomic_add_64(&zi->zi_call_time, functime);
if (zopt_verbose >= 4) {
Dl_info dli;
(void) dladdr((void *)zi->zi_func, &dli);
(void) printf("%6.2f sec in %s\n",
(double)functime / NANOSEC, dli.dli_sname);
}
/*
* If we're getting ENOSPC with some regularity, stop.
*/
if (zs->zs_enospc_count > 10)
break;
}
thread_exit();
return (NULL);
}
/*
* Kick off threads to run tests on all datasets in parallel.
*/
static void
ztest_run(char *pool)
{
int t, d, error;
ztest_shared_t *zs = ztest_shared;
ztest_args_t *za;
spa_t *spa;
char name[100];
kthread_t *resume_thread;
kt_did_t resume_id;
ztest_exiting = B_FALSE;
mutex_init(&zs->zs_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
rw_init(&zs->zs_name_lock, NULL, RW_DEFAULT, NULL);
mutex_init(&zs->zs_cb_list.zcl_callbacks_lock,NULL,MUTEX_DEFAULT,NULL);
list_create(&zs->zs_cb_list.zcl_callbacks, sizeof (ztest_cb_data_t),
offsetof(ztest_cb_data_t, zcd_node));
for (t = 0; t < ZTEST_SYNC_LOCKS; t++)
mutex_init(&zs->zs_sync_lock[t], NULL, MUTEX_DEFAULT, NULL);
/*
* Destroy one disk before we even start.
* It's mirrored, so everything should work just fine.
* This makes us exercise fault handling very early in spa_load().
*/
ztest_obliterate_one_disk(0);
/*
* Verify that the sum of the sizes of all blocks in the pool
* equals the SPA's allocated space total.
*/
ztest_verify_blocks(pool);
/*
* Kick off a replacement of the disk we just obliterated.
*/
kernel_init(FREAD | FWRITE);
VERIFY(spa_open(pool, &spa, FTAG) == 0);
ztest_replace_one_disk(spa, 0);
if (zopt_verbose >= 5)
show_pool_stats(spa);
spa_close(spa, FTAG);
kernel_fini();
kernel_init(FREAD | FWRITE);
/*
* Verify that we can export the pool and reimport it under a
* different name.
*/
if (ztest_random(2) == 0) {
(void) snprintf(name, 100, "%s_import", pool);
ztest_spa_import_export(pool, name);
ztest_spa_import_export(name, pool);
}
/*
* Verify that we can loop over all pools.
*/
mutex_enter(&spa_namespace_lock);
for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa)) {
if (zopt_verbose > 3) {
(void) printf("spa_next: found %s\n", spa_name(spa));
}
}
mutex_exit(&spa_namespace_lock);
/*
* Open our pool.
*/
VERIFY(spa_open(pool, &spa, FTAG) == 0);
/*
* We don't expect the pool to suspend unless maxfaults == 0,
* in which case ztest_fault_inject() temporarily takes away
* the only valid replica.
*/
if (zopt_maxfaults == 0)
spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
else
spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
/*
* Create a thread to periodically resume suspended I/O.
*/
VERIFY3P((resume_thread = thread_create(NULL, 0, ztest_resume_thread,
spa, THR_BOUND, NULL, 0, 0)), !=, NULL);
resume_id = resume_thread->t_tid;
/*
* Verify that we can safely inquire about about any object,
* whether it's allocated or not. To make it interesting,
* we probe a 5-wide window around each power of two.
* This hits all edge cases, including zero and the max.
*/
for (t = 0; t < 64; t++) {
for (d = -5; d <= 5; d++) {
error = dmu_object_info(spa->spa_meta_objset,
(1ULL << t) + d, NULL);
ASSERT(error == 0 || error == ENOENT ||
error == EINVAL);
}
}
/*
* Now kick off all the tests that run in parallel.
*/
zs->zs_enospc_count = 0;
za = umem_zalloc(zopt_threads * sizeof (ztest_args_t), UMEM_NOFAIL);
if (zopt_verbose >= 4)
(void) printf("starting main threads...\n");
za[0].za_start = gethrtime();
za[0].za_stop = za[0].za_start + zopt_passtime * NANOSEC;
za[0].za_stop = MIN(za[0].za_stop, zs->zs_stop_time);
za[0].za_kill = za[0].za_stop;
if (ztest_random(100) < zopt_killrate)
za[0].za_kill -= ztest_random(zopt_passtime * NANOSEC);
for (t = 0; t < zopt_threads; t++) {
d = t % zopt_datasets;
(void) strcpy(za[t].za_pool, pool);
za[t].za_os = za[d].za_os;
za[t].za_spa = spa;
za[t].za_zilog = za[d].za_zilog;
za[t].za_instance = t;
za[t].za_random = ztest_random(-1ULL);
za[t].za_start = za[0].za_start;
za[t].za_stop = za[0].za_stop;
za[t].za_kill = za[0].za_kill;
if (t < zopt_datasets) {
int test_future = FALSE;
rw_enter(&ztest_shared->zs_name_lock, RW_READER);
(void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 0,
ztest_create_cb, NULL);
if (error == EEXIST) {
test_future = TRUE;
} else if (error == ENOSPC) {
zs->zs_enospc_count++;
rw_exit(&ztest_shared->zs_name_lock);
break;
} else if (error != 0) {
fatal(0, "dmu_objset_create(%s) = %d",
name, error);
}
error = dmu_objset_open(name, DMU_OST_OTHER,
DS_MODE_USER, &za[d].za_os);
if (error)
fatal(0, "dmu_objset_open('%s') = %d",
name, error);
rw_exit(&ztest_shared->zs_name_lock);
if (test_future)
ztest_dmu_check_future_leak(&za[t]);
zil_replay(za[d].za_os, za[d].za_os,
ztest_replay_vector);
za[d].za_zilog = zil_open(za[d].za_os, NULL);
}
VERIFY3P((za[t].za_thread = thread_create(NULL, 0, ztest_thread,
&za[t], THR_BOUND, NULL, 0, 0)), !=, NULL);
za[t].za_threadid = za[t].za_thread->t_tid;
}
while (--t >= 0) {
VERIFY(thread_join(za[t].za_threadid, NULL, NULL) == 0);
if (t < zopt_datasets) {
zil_close(za[t].za_zilog);
dmu_objset_close(za[t].za_os);
}
}
if (zopt_verbose >= 3)
show_pool_stats(spa);
txg_wait_synced(spa_get_dsl(spa), 0);
zs->zs_alloc = spa_get_alloc(spa);
zs->zs_space = spa_get_space(spa);
/*
* If we had out-of-space errors, destroy a random objset.
*/
if (zs->zs_enospc_count != 0) {
rw_enter(&ztest_shared->zs_name_lock, RW_READER);
d = (int)ztest_random(zopt_datasets);
(void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
if (zopt_verbose >= 3)
(void) printf("Destroying %s to free up space\n", name);
/* Cleanup any non-standard clones and snapshots */
ztest_dsl_dataset_cleanup(name, za[d].za_instance);
(void) dmu_objset_find(name, ztest_destroy_cb, &za[d],
DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
rw_exit(&ztest_shared->zs_name_lock);
}
txg_wait_synced(spa_get_dsl(spa), 0);
umem_free(za, zopt_threads * sizeof (ztest_args_t));
/* Kill the resume thread */
ztest_exiting = B_TRUE;
VERIFY(thread_join(resume_id, NULL, NULL) == 0);
ztest_resume(spa);
/*
* Right before closing the pool, kick off a bunch of async I/O;
* spa_close() should wait for it to complete.
*/
for (t = 1; t < 50; t++)
dmu_prefetch(spa->spa_meta_objset, t, 0, 1 << 15);
spa_close(spa, FTAG);
kernel_fini();
list_destroy(&zs->zs_cb_list.zcl_callbacks);
mutex_destroy(&zs->zs_cb_list.zcl_callbacks_lock);
rw_destroy(&zs->zs_name_lock);
mutex_destroy(&zs->zs_vdev_lock);
}
void
print_time(hrtime_t t, char *timebuf)
{
hrtime_t s = t / NANOSEC;
hrtime_t m = s / 60;
hrtime_t h = m / 60;
hrtime_t d = h / 24;
s -= m * 60;
m -= h * 60;
h -= d * 24;
timebuf[0] = '\0';
if (d)
(void) sprintf(timebuf,
"%llud%02lluh%02llum%02llus", d, h, m, s);
else if (h)
(void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
else if (m)
(void) sprintf(timebuf, "%llum%02llus", m, s);
else
(void) sprintf(timebuf, "%llus", s);
}
/*
* Create a storage pool with the given name and initial vdev size.
* Then create the specified number of datasets in the pool.
*/
static void
ztest_init(char *pool)
{
spa_t *spa;
int error;
nvlist_t *nvroot;
kernel_init(FREAD | FWRITE);
/*
* Create the storage pool.
*/
(void) spa_destroy(pool);
ztest_shared->zs_vdev_primaries = 0;
nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
0, zopt_raidz, zopt_mirrors, 1);
error = spa_create(pool, nvroot, NULL, NULL, NULL);
nvlist_free(nvroot);
if (error)
fatal(0, "spa_create() = %d", error);
error = spa_open(pool, &spa, FTAG);
if (error)
fatal(0, "spa_open() = %d", error);
metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
if (zopt_verbose >= 3)
show_pool_stats(spa);
spa_close(spa, FTAG);
kernel_fini();
}
int
main(int argc, char **argv)
{
int kills = 0;
int iters = 0;
int i, f;
ztest_shared_t *zs;
ztest_info_t *zi;
char timebuf[100];
char numbuf[6];
(void) setvbuf(stdout, NULL, _IOLBF, 0);
/* Override location of zpool.cache */
spa_config_path = "/tmp/zpool.cache";
ztest_random_fd = open("/dev/urandom", O_RDONLY);
process_options(argc, argv);
/*
* Blow away any existing copy of zpool.cache
*/
if (zopt_init != 0)
(void) remove("/tmp/zpool.cache");
zs = ztest_shared = (void *)mmap(0,
P2ROUNDUP(sizeof (ztest_shared_t), getpagesize()),
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
if (zopt_verbose >= 1) {
(void) printf("%llu vdevs, %d datasets, %d threads,"
" %llu seconds...\n",
(u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
(u_longlong_t)zopt_time);
}
/*
* Create and initialize our storage pool.
*/
for (i = 1; i <= zopt_init; i++) {
bzero(zs, sizeof (ztest_shared_t));
if (zopt_verbose >= 3 && zopt_init != 1)
(void) printf("ztest_init(), pass %d\n", i);
ztest_init(zopt_pool);
}
/*
* Initialize the call targets for each function.
*/
for (f = 0; f < ZTEST_FUNCS; f++) {
zi = &zs->zs_info[f];
*zi = ztest_info[f];
if (*zi->zi_interval == 0)
zi->zi_call_target = UINT64_MAX;
else
zi->zi_call_target = zopt_time / *zi->zi_interval;
}
zs->zs_start_time = gethrtime();
zs->zs_stop_time = zs->zs_start_time + zopt_time * NANOSEC;
/*
* Run the tests in a loop. These tests include fault injection
* to verify that self-healing data works, and forced crashes
* to verify that we never lose on-disk consistency.
*/
while (gethrtime() < zs->zs_stop_time) {
int status;
pid_t pid;
char *tmp;
/*
* Initialize the workload counters for each function.
*/
for (f = 0; f < ZTEST_FUNCS; f++) {
zi = &zs->zs_info[f];
zi->zi_calls = 0;
zi->zi_call_time = 0;
}
/* Set the allocation switch size */
metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1;
pid = fork();
if (pid == -1)
fatal(1, "fork failed");
if (pid == 0) { /* child */
struct rlimit rl = { 1024, 1024 };
(void) setrlimit(RLIMIT_NOFILE, &rl);
(void) enable_extended_FILE_stdio(-1, -1);
ztest_run(zopt_pool);
exit(0);
}
while (waitpid(pid, &status, 0) != pid)
continue;
if (WIFEXITED(status)) {
if (WEXITSTATUS(status) != 0) {
(void) fprintf(stderr,
"child exited with code %d\n",
WEXITSTATUS(status));
exit(2);
}
} else if (WIFSIGNALED(status)) {
if (WTERMSIG(status) != SIGKILL) {
(void) fprintf(stderr,
"child died with signal %d\n",
WTERMSIG(status));
exit(3);
}
kills++;
} else {
(void) fprintf(stderr, "something strange happened "
"to child\n");
exit(4);
}
iters++;
if (zopt_verbose >= 1) {
hrtime_t now = gethrtime();
now = MIN(now, zs->zs_stop_time);
print_time(zs->zs_stop_time - now, timebuf);
nicenum(zs->zs_space, numbuf);
(void) printf("Pass %3d, %8s, %3llu ENOSPC, "
"%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
iters,
WIFEXITED(status) ? "Complete" : "SIGKILL",
(u_longlong_t)zs->zs_enospc_count,
100.0 * zs->zs_alloc / zs->zs_space,
numbuf,
100.0 * (now - zs->zs_start_time) /
(zopt_time * NANOSEC), timebuf);
}
if (zopt_verbose >= 2) {
(void) printf("\nWorkload summary:\n\n");
(void) printf("%7s %9s %s\n",
"Calls", "Time", "Function");
(void) printf("%7s %9s %s\n",
"-----", "----", "--------");
for (f = 0; f < ZTEST_FUNCS; f++) {
Dl_info dli;
zi = &zs->zs_info[f];
print_time(zi->zi_call_time, timebuf);
(void) dladdr((void *)zi->zi_func, &dli);
(void) printf("%7llu %9s %s\n",
(u_longlong_t)zi->zi_calls, timebuf,
dli.dli_sname);
}
(void) printf("\n");
}
/*
* It's possible that we killed a child during a rename test, in
* which case we'll have a 'ztest_tmp' pool lying around instead
* of 'ztest'. Do a blind rename in case this happened.
*/
tmp = umem_alloc(strlen(zopt_pool) + 5, UMEM_NOFAIL);
(void) strcpy(tmp, zopt_pool);
(void) strcat(tmp, "_tmp");
kernel_init(FREAD | FWRITE);
(void) spa_rename(tmp, zopt_pool);
kernel_fini();
umem_free(tmp, strlen(tmp) + 1);
}
ztest_verify_blocks(zopt_pool);
if (zopt_verbose >= 1) {
(void) printf("%d killed, %d completed, %.0f%% kill rate\n",
kills, iters - kills, (100.0 * kills) / MAX(1, iters));
}
return (0);
}
|