aboutsummaryrefslogtreecommitdiffstats
path: root/common/rc
blob: aa35c9bd7405fa3768080bd79aaf3cad8e5db419 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
##/bin/bash
# SPDX-License-Identifier: GPL-2.0+
# Copyright (c) 2000-2006 Silicon Graphics, Inc.  All Rights Reserved.

. common/config

BC="$(type -P bc)" || BC=

_require_math()
{
	if [ -z "$BC" ]; then
		_notrun "this test requires 'bc' tool for doing math operations"
	fi
}

_math()
{
	[ $# -le 0 ] && return
	LANG=C echo "scale=0; $@" | "$BC" -q 2> /dev/null
}

dd()
{
	command dd --help 2>&1 | grep noxfer >/dev/null
	if [ "$?" -eq 0 ]
	    then
		command dd status=noxfer $@
	    else
		command dd $@
	fi
}

# Prints the md5 checksum of a given file
_md5_checksum()
{
	md5sum $1 | cut -d ' ' -f1
}

# Write a byte into a range of a file
_pwrite_byte() {
	local pattern="$1"
	local offset="$2"
	local len="$3"
	local file="$4"
	local xfs_io_args="$5"

	$XFS_IO_PROG $xfs_io_args -f -c "pwrite -S $pattern $offset $len" "$file"
}

# mmap-write a byte into a range of a file
_mwrite_byte() {
	local pattern="$1"
	local offset="$2"
	local len="$3"
	local mmap_len="$4"
	local file="$5"

	$XFS_IO_PROG -f -c "mmap -rw 0 $mmap_len" -c "mwrite -S $pattern $offset $len" "$file"
}

# ls -l w/ selinux sometimes puts a dot at the end:
# -rwxrw-r--. id1 id2 file1
# Also filter out lost+found directory on extN file system if present

_ls_l()
{
	ls -l $* | sed "s/\(^[-rwxdlbcpsStT]*\)\. /\1 /" | grep -v 'lost+found'
}

_dump_err()
{
    _err_msg="$*"
    echo "$_err_msg"
}

_dump_err_cont()
{
    _err_msg="$*"
    echo -n "$_err_msg"
}

_dump_err2()
{
    _err_msg="$*"
    >&2 echo "$_err_msg"
}

_log_err()
{
    _err_msg="$*"
    echo "$_err_msg" | tee -a $seqres.full
    echo "(see $seqres.full for details)"
}

# make sure we have a standard umask
umask 022

# check for correct setup and source the $FSTYP specific functions now
_source_specific_fs $FSTYP

if [ ! -z "$REPORT_LIST" ]; then
	. ./common/report
	_assert_report_list
fi

_get_filesize()
{
    stat -c %s "$1"
}

# Does this kernel support huge pages?
_require_hugepages()
{
	awk '/Hugepagesize/ {print $2}' /proc/meminfo | grep -E -q ^[0-9]+$ || \
		_notrun "Kernel does not report huge page size"
}

# Get hugepagesize in bytes
_get_hugepagesize()
{
	awk '/Hugepagesize/ {print $2 * 1024}' /proc/meminfo
}

_mount()
{
    $MOUNT_PROG $*
}

# Call _mount to do mount operation but also save mountpoint to
# MOUNTED_POINT_STACK. Note that the mount point must be the last parameter
_get_mount()
{
	local mnt_point=${!#}
	local mnt_dev=${@:(-2):1}
	local scratch_opts=""
	if [ "$mnt_dev" = "$SCRATCH_DEV" ]; then
		_scratch_options mount
		scratch_opts="$SCRATCH_OPTIONS"
	fi

	_mount $scratch_opts $*
	if [ $? -eq 0 ]; then
		# mount --move operation updates the mountpoint, so remove
		# the old one and insert the new one
		if [[ "$*" =~ --move|-M ]]; then
			MOUNTED_POINT_STACK=`echo $MOUNTED_POINT_STACK | \
						cut -d\  -f2-`
		fi
		MOUNTED_POINT_STACK="$mnt_point $MOUNTED_POINT_STACK"
	else
		return 1
	fi
}

# Unmount the last mounted mountpoint in MOUNTED_POINT_STACK
# and return it to caller
_put_mount()
{
	local last_mnt=`echo $MOUNTED_POINT_STACK | awk '{print $1}'`

	if [ -n "$last_mnt" ]; then
		$UMOUNT_PROG $last_mnt
	fi
	MOUNTED_POINT_STACK=`echo $MOUNTED_POINT_STACK | cut -d\  -f2-`
}

# Unmount all mountpoints in MOUNTED_POINT_STACK and clear the stack
_clear_mount_stack()
{
	if [ -n "$MOUNTED_POINT_STACK" ]; then
		$UMOUNT_PROG $MOUNTED_POINT_STACK
	fi
	MOUNTED_POINT_STACK=""
}

_scratch_options()
{
    SCRATCH_OPTIONS=""

    case "$FSTYP" in
    "xfs")
	_scratch_xfs_options "$@"
	;;
    ext2|ext3|ext4|ext4dev)
	_scratch_ext4_options "$@"
	;;
    esac
}

_test_options()
{
    local type=$1
    local rt_opt=""
    local log_opt=""
    TEST_OPTIONS=""

    if [ "$FSTYP" != "xfs" ]; then
        return
    fi

    case $type in
    mkfs)
	rt_opt="-r"
        log_opt="-l"
	;;
    mount)
	rt_opt="-o"
        log_opt="-o"
	;;
    esac
    [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
	TEST_OPTIONS="$TEST_OPTIONS ${rt_opt}rtdev=$TEST_RTDEV"
    [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
	TEST_OPTIONS="$TEST_OPTIONS ${log_opt}logdev=$TEST_LOGDEV"
}

# Used for mounting non-scratch devices (e.g. loop, dm constructs)
# with the safe set of scratch mount options (e.g. loop image may be
# hosted on $SCRATCH_DEV, so can't use external scratch devices).
_common_dev_mount_options()
{
	echo $MOUNT_OPTIONS $SELINUX_MOUNT_OPTIONS $*
}

_scratch_mount_options()
{
	_scratch_options mount

	echo `_common_dev_mount_options $*` $SCRATCH_OPTIONS \
					$SCRATCH_DEV $SCRATCH_MNT
}

_supports_filetype()
{
	local dir=$1

	local fstyp=`$DF_PROG $dir | tail -1 | $AWK_PROG '{print $2}'`
	case "$fstyp" in
	xfs)
		_xfs_has_feature $dir ftype
		;;
	ext2|ext3|ext4)
		local dev=`$DF_PROG $dir | tail -1 | $AWK_PROG '{print $1}'`
		tune2fs -l $dev | grep -q filetype
		;;
	*)
		local testfile=$dir/$$.ftype
		touch $testfile
		# look for DT_UNKNOWN files
		local unknowns=$($here/src/t_dir_type $dir u | wc -l)
		rm $testfile
		# 0 unknowns is success
		return $unknowns
		;;
	esac
}

# mount scratch device with given options but don't check mount status
_try_scratch_mount()
{
	local mount_ret

	if [ "$FSTYP" == "overlay" ]; then
		_overlay_scratch_mount $*
		return $?
	fi
	_mount -t $FSTYP$FUSE_SUBTYP `_scratch_mount_options $*`
	mount_ret=$?
	[ $mount_ret -ne 0 ] && return $mount_ret
	_idmapped_mount $SCRATCH_DEV $SCRATCH_MNT
}

# mount scratch device with given options and _fail if mount fails
_scratch_mount()
{
	_try_scratch_mount $* || _fail "mount $(_scratch_mount_options $*) failed"
}

_scratch_mount_idmapped()
{
	local type="$1"
	local id="$2"

	if [ "$type" = "u" ]; then
		# This means root will be able to create files as uid %id in
		# the underlying filesystem by going through the idmapped mount.
		$here/src/vfs/mount-idmapped --map-mount u:0:$id:1 \
					     --map-mount u:$id:0:1 \
					     --map-mount g:0:0:1 \
					     "$SCRATCH_MNT" "$SCRATCH_MNT" || _fail "mount-idmapped failed"
	elif [ "$type" = "g" ]; then
		# This means root will be able to create files as gid %id in
		# the underlying filesystem by going through the idmapped mount.
		$here/src/vfs/mount-idmapped --map-mount g:0:$id:1 \
					     --map-mount g:$id:0:1 \
					     --map-mount u:0:0:1 \
					     "$SCRATCH_MNT" "$SCRATCH_MNT" || _fail "mount-idmapped failed"
	elif [ "$type" = "b" ]; then
		# This means root will be able to create files as uid and gid
		# %id in the underlying filesystem by going through the idmapped mount.
		$here/src/vfs/mount-idmapped --map-mount b:0:$id:1 \
					     --map-mount b:$id:0:1 \
					     "$SCRATCH_MNT" "$SCRATCH_MNT" || _fail "mount-idmapped failed"
	else
		_fail "usage: either \"u\" (uid), \"g\" (gid), or \"b\" (uid and gid) must be specified "
	fi
}

_scratch_unmount()
{
	case "$FSTYP" in
	overlay)
		_overlay_scratch_unmount
		;;
	btrfs)
		$UMOUNT_PROG $SCRATCH_MNT
		;;
	*)
		$UMOUNT_PROG $SCRATCH_DEV
		;;
	esac
}

_scratch_umount_idmapped()
{
	$UMOUNT_PROG $SCRATCH_MNT
}

_scratch_remount()
{
    local opts="$1"

    if test -n "$opts"; then
	_try_scratch_mount "-o remount,$opts"
    fi
}

_scratch_cycle_mount()
{
    local opts="$1"
    local unmounted

    case "$FSTYP" in
    tmpfs)
        _scratch_remount "$opts"
        return
        ;;
    overlay)
        if [ "$OVL_BASE_FSTYP" = tmpfs ]; then
            $UMOUNT_PROG $SCRATCH_MNT
            unmounted=true
        fi
        ;;
    esac

    if test -n "$opts"; then
	opts="-o $opts"
    fi
    [ "$unmounted" = true ] || _scratch_unmount
    _try_scratch_mount "$opts" || _fail "cycle mount failed"
}

_scratch_shutdown()
{
	if [ $FSTYP = "overlay" ]; then
		# In lagacy overlay usage, it may specify directory as
		# SCRATCH_DEV, in this case OVL_BASE_SCRATCH_DEV
		# will be null, so check OVL_BASE_SCRATCH_DEV before
		# running shutdown to avoid shutting down base fs accidently.
		if [ -z $OVL_BASE_SCRATCH_DEV ]; then
			_fail "_scratch_shutdown: call _require_scratch_shutdown first in test"
		else
			$here/src/godown $* $OVL_BASE_SCRATCH_MNT
		fi
	else
		$here/src/godown $* $SCRATCH_MNT
	fi
}

# Return a file path that can be used to shut down the scratch filesystem.
# Caller should _require_scratch_shutdown before using this.
_scratch_shutdown_handle()
{
	if [ $FSTYP = "overlay" ]; then
		echo $OVL_BASE_SCRATCH_MNT
	else
		echo $SCRATCH_MNT
	fi
}

_move_mount()
{
	local mnt=$1
	local tmp=$2

	# Replace $mnt with $tmp. Use a temporary bind-mount because
	# mount --move will fail with certain mount propagation layouts.
	$UMOUNT_PROG $mnt || _fail "Failed to unmount $mnt"
	_mount --bind $tmp $mnt || _fail "Failed to bind-mount $tmp to $mnt"
	$UMOUNT_PROG $tmp || _fail "Failed to unmount $tmp"
	rmdir $tmp
}

_idmapped_mount()
{
	[ "$IDMAPPED_MOUNTS" = "true" ] || return 0

	local dev=$1
	local mnt=$2
	local status=0
	local tmp=`mktemp -d`

	local mount_rec=`findmnt -rncv -S $dev -o OPTIONS`
	# We create an idmapped mount where {g,u}id 0 writes to disk as
	# {g,u}id 10000000 and $(id -u fsgqa) + 10000000. We change ownership
	# of $mnt, provided it's not read-only, so {g,u} id 0 can actually
	# create objects in there.
	if [[ "$mount_rec" != *"ro,"* && "$mount_rec" != *",ro"* ]]; then
		chown 10000000:10000000 $mnt || return 1
	fi
	# But if the mount is already idmapped, then there's nothing more to do.
	if [[ "$mount_rec" == *"idmapped"* ]]; then
		return 0
	fi

	$here/src/vfs/mount-idmapped \
		--map-mount b:10000000:0:100000000000 \
		$mnt $tmp
	if [ $? -ne 0 ]; then
		rmdir $tmp
		return 1
	fi

	# The next call ensures we don't end up stacking an idmapped mount on
	# top of the original mount. Instead we fully replace the original
	# mount with the idmapped mount. This will not just allow a clean mount
        # layout it also makes unmount and remounting way simpler.
	_move_mount $mnt $tmp
	return $?
}

_test_mount()
{
    local mount_ret

    if [ "$FSTYP" == "ceph-fuse" ]; then
        $CEPH_FUSE_BIN_PATH $TEST_FS_MOUNT_OPTS $TEST_DIR 2> /dev/null
        return $?
    elif [ "$FSTYP" == "overlay" ]; then
        _overlay_test_mount $*
        return $?
    fi

    _test_options mount
    _mount -t $FSTYP$FUSE_SUBTYP $TEST_OPTIONS $TEST_FS_MOUNT_OPTS $SELINUX_MOUNT_OPTIONS $* $TEST_DEV $TEST_DIR
    mount_ret=$?
    [ $mount_ret -ne 0 ] && return $mount_ret
    _idmapped_mount $TEST_DEV $TEST_DIR
}

_test_unmount()
{
	if [ "$FSTYP" == "overlay" ]; then
		_overlay_test_unmount
	else
		$UMOUNT_PROG $TEST_DEV
	fi
}

_test_cycle_mount()
{
    local unmounted

    case "$FSTYP" in
    tmpfs)
        return
        ;;
    overlay)
        if [ "$OVL_BASE_FSTYP" = tmpfs ]; then
            $UMOUNT_PROG $TEST_DIR
            unmounted=true
        fi
        ;;
    esac

    [ "$unmounted" = true ] || _test_unmount
    _test_mount
}

_scratch_mkfs_options()
{
    _scratch_options mkfs
    echo $SCRATCH_OPTIONS $MKFS_OPTIONS $* $SCRATCH_DEV
}

# Do the actual mkfs work on SCRATCH_DEV. Firstly mkfs with both MKFS_OPTIONS
# and user specified mkfs options, if that fails (due to conflicts between mkfs
# options), do a second mkfs with only user provided mkfs options.
#
# First param is the mkfs command without any mkfs options and device.
# Second param is the filter to remove unnecessary messages from mkfs stderr.
# Other extra mkfs options are followed.
_scratch_do_mkfs()
{
	local mkfs_cmd=$1
	local mkfs_filter=$2
	shift 2
	local extra_mkfs_options=$*
	local mkfs_status
	local tmp=`mktemp -u`

	# save mkfs output in case conflict means we need to run again.
	# only the output for the mkfs that applies should be shown
	eval "$mkfs_cmd $MKFS_OPTIONS $extra_mkfs_options $SCRATCH_DEV" \
		2>$tmp.mkfserr 1>$tmp.mkfsstd
	mkfs_status=$?

	# a mkfs failure may be caused by conflicts between $MKFS_OPTIONS and
	# $extra_mkfs_options
	if [ $mkfs_status -ne 0 -a -n "$extra_mkfs_options" ]; then
		(
		echo -n "** mkfs failed with extra mkfs options "
		echo "added to \"$MKFS_OPTIONS\" by test $seq **"
		echo -n "** attempting to mkfs using only test $seq "
		echo "options: $extra_mkfs_options **"
		) >> $seqres.full

		# running mkfs again. overwrite previous mkfs output files
		eval "$mkfs_cmd $extra_mkfs_options $SCRATCH_DEV" \
			2>$tmp.mkfserr 1>$tmp.mkfsstd
		mkfs_status=$?
	fi

	# output stored mkfs output, filtering unnecessary output from stderr
	cat $tmp.mkfsstd
	eval "cat $tmp.mkfserr | $mkfs_filter" >&2

	rm -f $tmp.mkfserr $tmp.mkfsstd
	return $mkfs_status
}

# Capture the metadata of a filesystem in a dump file for offline analysis.
# This is not supported by all filesystem types, so this function should only
# be used after a test has already failed.
_metadump_dev() {
	local device="$1"
	local dumpfile="$2"
	local compressopt="$3"

	test "$DUMP_CORRUPT_FS" = 1 || return 0

	case "$FSTYP" in
	btrfs)
		_btrfs_metadump $device $dumpfile
		;;
	ext*)
		_ext4_metadump $device $dumpfile $compressopt
		;;
	xfs)
		_xfs_metadump $dumpfile $device none $compressopt
		;;
	*)
		echo "Don't know how to metadump $FSTYP"
		return 1
		;;
	esac
}

_test_mkfs()
{
    case $FSTYP in
    nfs*)
	# do nothing for nfs
	;;
    afs*)
	# do nothing for afs
	;;
    cifs)
	# do nothing for cifs
	;;
    9p)
	# do nothing for 9p
	;;
    fuse)
	# do nothing for fuse
	;;
    virtiofs)
	# do nothing for virtiofs
	;;
    ceph)
	# do nothing for ceph
	;;
    glusterfs)
	# do nothing for glusterfs
	;;
    overlay)
	# do nothing for overlay
	;;
    pvfs2)
	# do nothing for pvfs2
	;;
    udf)
        $MKFS_UDF_PROG $MKFS_OPTIONS $* $TEST_DEV > /dev/null
	;;
    btrfs)
        $MKFS_BTRFS_PROG $MKFS_OPTIONS $* $TEST_DEV > /dev/null
	;;
    ext2|ext3|ext4)
	$MKFS_PROG -t $FSTYP -- -F $MKFS_OPTIONS $* $TEST_DEV
	;;
    xfs)
	$MKFS_PROG -t $FSTYP -- -f $MKFS_OPTIONS $* $TEST_DEV
	;;
    bcachefs)
	$MKFS_BCACHEFS_PROG $MKFS_OPTIONS $* $TEST_DEV > /dev/null
	;;
    *)
	yes | $MKFS_PROG -t $FSTYP -- $MKFS_OPTIONS $* $TEST_DEV
	;;
    esac
}

_try_mkfs_dev()
{
    case $FSTYP in
    nfs*)
	# do nothing for nfs
	;;
    afs*)
	# do nothing for afs
	;;
    9p)
	# do nothing for 9p
	;;
    fuse)
	# do nothing for fuse
	;;
    virtiofs)
	# do nothing for virtiofs
	;;
    overlay)
	# do nothing for overlay
	;;
    pvfs2)
	# do nothing for pvfs2
	;;
    udf)
        $MKFS_UDF_PROG $MKFS_OPTIONS $*
	;;
    btrfs)
        $MKFS_BTRFS_PROG $MKFS_OPTIONS $*
	;;
    ext2|ext3|ext4)
	$MKFS_PROG -t $FSTYP -- -F $MKFS_OPTIONS $*
	;;
    xfs)
	$MKFS_PROG -t $FSTYP -- -f $MKFS_OPTIONS $*
	;;
    *)
	yes | $MKFS_PROG -t $FSTYP -- $MKFS_OPTIONS $*
	;;
    esac
}

_mkfs_dev()
{
    local tmp=`mktemp -u`
    if ! _try_mkfs_dev "$@" 2>$tmp.mkfserr 1>$tmp.mkfsstd; then
	# output stored mkfs output
	cat $tmp.mkfserr >&2
	cat $tmp.mkfsstd
	status=1
	exit 1
    fi
    rm -f $tmp.mkfserr $tmp.mkfsstd
}

# remove all files in $SCRATCH_MNT, useful when testing on NFS/AFS/CIFS
_scratch_cleanup_files()
{
	case $FSTYP in
	overlay)
		# Avoid rm -rf /* if we messed up
		[ -n "$OVL_BASE_SCRATCH_MNT" ] || return 1
		_overlay_base_scratch_mount || return 1
		rm -rf $OVL_BASE_SCRATCH_MNT/* || return 1
		_overlay_mkdirs $OVL_BASE_SCRATCH_MNT
		# leave base fs mouted so tests can setup lower/upper dir files
		;;
	*)
		[ -n "$SCRATCH_MNT" ] || return 1
		_scratch_mount
		rm -rf $SCRATCH_MNT/*
		_scratch_unmount
		;;
	esac
}

_scratch_mkfs()
{
	local mkfs_cmd=""
	local mkfs_filter=""
	local mkfs_status

	case $FSTYP in
	nfs*|afs|cifs|ceph|overlay|glusterfs|pvfs2|9p|fuse|virtiofs)
		# unable to re-create this fstyp, just remove all files in
		# $SCRATCH_MNT to avoid EEXIST caused by the leftover files
		# created in previous runs
		_scratch_cleanup_files
		return $?
		;;
	tmpfs)
		# do nothing for tmpfs
		return 0
		;;
	ubifs)
		# erase the UBI volume; reformated automatically on next mount
		$UBIUPDATEVOL_PROG ${SCRATCH_DEV} -t
		return 0
		;;
	ext4)
		_scratch_mkfs_ext4 $*
		return $?
		;;
	xfs)
		_scratch_mkfs_xfs $*
		return $?
		;;
	udf)
		mkfs_cmd="$MKFS_UDF_PROG"
		mkfs_filter="cat"
		;;
	btrfs)
		mkfs_cmd="$MKFS_BTRFS_PROG"
		mkfs_filter="cat"
		;;
	ext3)
		mkfs_cmd="$MKFS_PROG -t $FSTYP -- -F"
		mkfs_filter="grep -v -e ^Warning: -e \"^mke2fs \""

		# put journal on separate device?
		[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
		$mkfs_cmd -O journal_dev $MKFS_OPTIONS $SCRATCH_LOGDEV && \
		mkfs_cmd="$mkfs_cmd -J device=$SCRATCH_LOGDEV"
		;;
	ext2)
		mkfs_cmd="$MKFS_PROG -t $FSTYP -- -F"
		mkfs_filter="grep -v -e ^Warning: -e \"^mke2fs \""
		;;
	f2fs)
		mkfs_cmd="$MKFS_F2FS_PROG"
		mkfs_filter="cat"
		;;
	ocfs2)
		mkfs_cmd="yes | $MKFS_PROG -t $FSTYP --"
		mkfs_filter="grep -v -e ^mkfs\.ocfs2"
		;;
    bcachefs)
		mkfs_cmd="$MKFS_BCACHEFS_PROG"
		mkfs_filter="cat"
		;;
	*)
		mkfs_cmd="yes | $MKFS_PROG -t $FSTYP --"
		mkfs_filter="cat"
		;;
	esac

	_scratch_do_mkfs "$mkfs_cmd" "$mkfs_filter" $*
	return $?
}

# Helper function to get a spare or replace-target device from
# configured SCRATCH_DEV_POLL, must call _scratch_dev_pool_get()
# before _spare_dev_get(). Replace-target-device/Spare-device will
# be assigned to SPARE_DEV.
# As of now only one replace-target-device/spare-device can be
# assigned.
#
# Usage:
#  _scratch_dev_pool_get() <ndevs>
#     _spare_dev_get()
#     :: do stuff
#     _spare_dev_put()
#  _scratch_dev_pool_put()
#
_spare_dev_get()
{
	typeset -p SCRATCH_DEV_POOL_SAVED >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		_fail "Bug: unset val, must call _scratch_dev_pool_get before _spare_dev_get"
	fi

	if [ -z "$SCRATCH_DEV_POOL_SAVED" ]; then
		_fail "Bug: str empty, must call _scratch_dev_pool_get before _spare_dev_get"
	fi

	# Check if the spare is already assigned
	typeset -p SPARE_DEV >/dev/null 2>&1
	if [ $? -eq 0 ]; then
		if [ ! -z "$SPARE_DEV" ]; then
			_fail "Bug: SPARE_DEV = $SPARE_DEV already assigned"
		fi
	fi

	local ndevs=`echo $SCRATCH_DEV_POOL| wc -w`
	local config_ndevs=`echo $SCRATCH_DEV_POOL_SAVED| wc -w`

	if [ $ndevs -eq $config_ndevs ]; then
		_notrun "All devs used no spare"
	fi
	# Get a dev that is not used
	local -a devs="( $SCRATCH_DEV_POOL_SAVED )"
	SPARE_DEV=${devs[@]:$ndevs:1}
	export SPARE_DEV
}

_spare_dev_put()
{
	typeset -p SPARE_DEV >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		_fail "Bug: unset val, must call _spare_dev_get before its put"
	fi

	if [ -z "$SPARE_DEV" ]; then
		_fail "Bug: str empty, must call _spare_dev_get before its put"
	fi

	export SPARE_DEV=""
}

#
# Generally test cases will have..
#   _require_scratch_dev_pool X
# to make sure it has the enough scratch devices including
# replace-target and spare device. Now arg1 here is the
# required number of scratch devices by a-test-case excluding
# the replace-target and spare device. So this function will
# set SCRATCH_DEV_POOL to the specified number of devices.
#
# Usage:
#  _scratch_dev_pool_get() <ndevs>
#     :: do stuff
#
#  _scratch_dev_pool_put()
#
_scratch_dev_pool_get()
{
	if [ $# -ne 1 ]; then
		_fail "Usage: _scratch_dev_pool_get ndevs"
	fi

	typeset -p SCRATCH_DEV_POOL >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		_fail "Bug: cant find SCRATCH_DEV_POOL ndevs"
	fi

	local test_ndevs=$1
	local config_ndevs=`echo $SCRATCH_DEV_POOL| wc -w`
	local -a devs="( $SCRATCH_DEV_POOL )"

	if [ $config_ndevs -lt $test_ndevs ]; then
		_notrun "Need at least test requested number of ndevs $test_ndevs"
	fi

	SCRATCH_DEV_POOL_SAVED=${SCRATCH_DEV_POOL}
	export SCRATCH_DEV_POOL_SAVED
	SCRATCH_DEV_POOL=${devs[@]:0:$test_ndevs}
	export SCRATCH_DEV_POOL
}

_scratch_dev_pool_put()
{
	typeset -p SCRATCH_DEV_POOL_SAVED >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		_fail "Bug: unset val, must call _scratch_dev_pool_get before _scratch_dev_pool_put"
	fi

	if [ -z "$SCRATCH_DEV_POOL_SAVED" ]; then
		_fail "Bug: str empty, must call _scratch_dev_pool_get before _scratch_dev_pool_put"
	fi

	export SCRATCH_DEV_POOL=$SCRATCH_DEV_POOL_SAVED
	export SCRATCH_DEV_POOL_SAVED=""
}

_scratch_pool_mkfs()
{
    case $FSTYP in
    btrfs)
        $MKFS_BTRFS_PROG $MKFS_OPTIONS $* $SCRATCH_DEV_POOL > /dev/null
        ;;
    *)
        echo "_scratch_pool_mkfs is not implemented for $FSTYP" 1>&2
        ;;
    esac
}

# Return the amount of free memory available on the system
_free_memory_bytes()
{
    free -b | grep ^Mem | awk '{print $4}'
}

_available_memory_bytes()
{
	nf=`free -b | grep ^Mem | awk '{print NF}'`
	if [[ nf -lt 7 ]]; then
		# Doesn't have available field. Fallback.
		_free_memory_bytes
	else
		free -b | grep ^Mem | awk '{print $7}'
	fi
}

_check_minimal_fs_size()
{
	local fssize=$1

	if [ -n "$MIN_FSSIZE" ]; then
		[ $MIN_FSSIZE -gt "$fssize" ] &&
			_notrun "specified filesystem size is too small"
	fi
}

# Create fs of certain size on scratch device
# _scratch_mkfs_sized <size in bytes> [optional blocksize]
_scratch_mkfs_sized()
{
	local fssize=$1
	local blocksize=$2
	local def_blksz
	local blocksize_opt

	case $FSTYP in
	xfs)
		def_blksz=`echo $MKFS_OPTIONS | sed -rn 's/.*-b ?size= ?+([0-9]+).*/\1/p'`
		;;
	btrfs)
		def_blksz=`echo $MKFS_OPTIONS | sed -rn 's/.*-s ?+([0-9]+).*/\1/p'`
		;;
	ext2|ext3|ext4|ext4dev|reiser4|ocfs2|reiserfs)
		def_blksz=`echo $MKFS_OPTIONS | sed -rn 's/.*-b ?+([0-9]+).*/\1/p'`
		;;
	udf)
		def_blksz=`echo $MKFS_OPTIONS | sed -rn 's/.*(-b|--blocksize)[ =]?+([0-9]+).*/\2/p'`
		if [ -z "$def_blksz" ]; then
			def_blksz=512
		fi
		;;
	jfs)
		def_blksz=4096
		;;
	bcachefs)
		def_blksz=`echo $MKFS_OPTIONS | sed -rn 's/.*(--block_size)[ =]?+([0-9]+).*/\2/p'`
		[ -n "$blocksize" ] && blocksize_opt="--block_size=$blocksize"
		[ -n "$def_blksize" ] && blocksize_opt="--block_size=$def_blksize"
		# If no block size is given by local.confg or parameter, blocksize_opt is empty.
		# Let MKFS_BCACHEFS_PROG decide block size on its own.
		;;
	esac

	[ -n "$def_blksz" ] && blocksize=$def_blksz
	[ -z "$blocksize" ] && blocksize=4096

	local re='^[0-9]+$'
	if ! [[ $fssize =~ $re ]] ; then
		_notrun "error: _scratch_mkfs_sized: fs size \"$fssize\" not an integer."
	fi
	if ! [[ $blocksize =~ $re ]] ; then
		_notrun "error: _scratch_mkfs_sized: block size \"$blocksize\" not an integer."
	fi

	local blocks=`expr $fssize / $blocksize`

	_check_minimal_fs_size $fssize

	if [ -b "$SCRATCH_DEV" ]; then
		local devsize=`blockdev --getsize64 $SCRATCH_DEV`
		[ "$fssize" -gt "$devsize" ] && _notrun "Scratch device too small"
	fi

	if [ "$FSTYP" = "xfs" ] && [ -b "$SCRATCH_RTDEV" ]; then
		local rtdevsize=`blockdev --getsize64 $SCRATCH_RTDEV`
		[ "$fssize" -gt "$rtdevsize" ] && _notrun "Scratch rt device too small"
		rt_ops="-r size=$fssize"
	fi

	case $FSTYP in
	xfs)
		# don't override MKFS_OPTIONS that set a block size.
		echo $MKFS_OPTIONS |grep -E -q "b\s*size="
		if [ $? -eq 0 ]; then
			_scratch_mkfs_xfs -d size=$fssize $rt_ops
		else
			_scratch_mkfs_xfs -d size=$fssize $rt_ops -b size=$blocksize
		fi
		;;
	ext2|ext3|ext4|ext4dev)
		# Can't use _scratch_mkfs_ext4 here because the block count has
		# to come after the device path.
		if [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ]; then
			${MKFS_PROG} -F -O journal_dev $MKFS_OPTIONS $SCRATCH_LOGDEV || \
				_notrun "Could not make scratch logdev"
			MKFS_OPTIONS="$MKFS_OPTIONS -J device=$SCRATCH_LOGDEV"
		fi
		${MKFS_PROG} -t $FSTYP -F $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV $blocks
		;;
	gfs2)
		# mkfs.gfs2 doesn't automatically shrink journal files on small
		# filesystems, so the journal files may end up being bigger than the
		# filesystem, which will cause mkfs.gfs2 to fail.  Until that's fixed,
		# shrink the journal size to at most one eigth of the filesystem and at
		# least 8 MiB, the minimum size allowed.
		local min_journal_size=8
		local default_journal_size=128
		if (( fssize/8 / (1024*1024) < default_journal_size )); then
			local journal_size=$(( fssize/8 / (1024*1024) ))
			(( journal_size >= min_journal_size )) || journal_size=$min_journal_size
			MKFS_OPTIONS="-J $journal_size $MKFS_OPTIONS"
		fi
		${MKFS_PROG} -t $FSTYP $MKFS_OPTIONS -O -b $blocksize $SCRATCH_DEV $blocks
		;;
	ocfs2)
		yes | ${MKFS_PROG} -t $FSTYP -F $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV $blocks
		;;
	udf)
		$MKFS_UDF_PROG $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV $blocks
		;;
	btrfs)
		local mixed_opt=
		# Mixed option is required when the filesystem size is small and
		# the device is not zoned. Ref: btrfs-progs: btrfs_min_dev_size()
		(( fssize < $((256 * 1024 * 1024)) )) &&
			! _scratch_btrfs_is_zoned && mixed_opt='--mixed'
		$MKFS_BTRFS_PROG $MKFS_OPTIONS $mixed_opt -b $fssize $SCRATCH_DEV
		;;
	jfs)
		${MKFS_PROG} -t $FSTYP $MKFS_OPTIONS $SCRATCH_DEV $blocks
		;;
	reiserfs)
		${MKFS_PROG} -t $FSTYP $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV $blocks
		;;
	reiser4)
		# mkfs.resier4 requires size in KB as input for creating filesystem
		$MKFS_REISER4_PROG $MKFS_OPTIONS -y -b $blocksize $SCRATCH_DEV \
				   `expr $fssize / 1024`
		;;
	f2fs)
		# mkfs.f2fs requires # of sectors as an input for the size
		local sector_size=`blockdev --getss $SCRATCH_DEV`
		$MKFS_F2FS_PROG $MKFS_OPTIONS $SCRATCH_DEV `expr $fssize / $sector_size`
		;;
	tmpfs)
		local free_mem=`_free_memory_bytes`
		if [ "$free_mem" -lt "$fssize" ] ; then
		   _notrun "Not enough memory ($free_mem) for tmpfs with $fssize bytes"
		fi
		export MOUNT_OPTIONS="-o size=$fssize $TMPFS_MOUNT_OPTIONS"
		;;
	bcachefs)
		$MKFS_BCACHEFS_PROG $MKFS_OPTIONS --fs_size=$fssize $blocksize_opt $SCRATCH_DEV
		;;
	*)
		_notrun "Filesystem $FSTYP not supported in _scratch_mkfs_sized"
		;;
	esac
}

# Emulate an N-data-disk stripe w/ various stripe units
# _scratch_mkfs_geom <sunit bytes> <swidth multiplier> [optional blocksize]
_scratch_mkfs_geom()
{
    local sunit_bytes=$1
    local swidth_mult=$2
    local blocksize=$3
    [ -z "$blocksize" ] && blocksize=4096

    local sunit_blocks=$(( sunit_bytes / blocksize ))
    local swidth_blocks=$(( sunit_blocks * swidth_mult ))

    case $FSTYP in
    xfs)
	if echo "$MKFS_OPTIONS" | grep -E -q "b\s*size="; then
		MKFS_OPTIONS=$(echo "$MKFS_OPTIONS" | sed -r "s/(b\s*size=)[0-9]+k?/\1$blocksize/")
	else
		MKFS_OPTIONS+=" -b size=$blocksize"
	fi

	if echo "$MKFS_OPTIONS" | grep -E -q "(su|sunit|sw|swidth)="; then
		MKFS_OPTIONS=$(echo "$MKFS_OPTIONS" | sed -r \
			-e "s/(su|sunit)=[0-9kmg]+/su=$sunit_bytes/" \
			-e "s/(sw|swidth)=[0-9kmg]+/sw=$swidth_mult/")
	else
		MKFS_OPTIONS+=" -d su=$sunit_bytes,sw=$swidth_mult"
	fi
	;;
    ext4|ext4dev)
	MKFS_OPTIONS+=" -b $blocksize -E stride=$sunit_blocks,stripe_width=$swidth_blocks"
	;;
    *)
	_notrun "can't mkfs $FSTYP with geometry"
	;;
    esac
    _scratch_mkfs
}

# Create fs of certain blocksize on scratch device
# _scratch_mkfs_blocksized blocksize
_scratch_mkfs_blocksized()
{
	local blocksize=$1

	local re='^[0-9]+$'
	if ! [[ $blocksize =~ $re ]] ; then
		_notrun "error: _scratch_mkfs_sized: block size \"$blocksize\" not an integer."
	fi
	if [ $blocksize -lt $(_get_page_size) ]; then
		_exclude_scratch_mount_option dax
	fi

	case $FSTYP in
	btrfs)
		test -f /sys/fs/btrfs/features/supported_sectorsizes || \
		_notrun "Subpage sectorsize support is not found in $FSTYP"

		grep -wq $blocksize /sys/fs/btrfs/features/supported_sectorsizes || \
		_notrun "$FSTYP does not support sectorsize=$blocksize yet"

		_scratch_mkfs --sectorsize=$blocksize
		;;
	xfs)
		_scratch_mkfs_xfs $MKFS_OPTIONS -b size=$blocksize
		;;
	ext2|ext3|ext4)
		_scratch_mkfs_ext4 $MKFS_OPTIONS -b $blocksize
		;;
	gfs2)
		${MKFS_PROG} -t $FSTYP $MKFS_OPTIONS -O -b $blocksize $SCRATCH_DEV
		;;
	ocfs2)
		yes | ${MKFS_PROG} -t $FSTYP -F $MKFS_OPTIONS -b $blocksize \
						-C $blocksize $SCRATCH_DEV
		;;
	bcachefs)
		_scratch_mkfs --block_size=$blocksize
		;;
	udf)
		_scratch_mkfs -b $blocksize
		;;
	*)
		_notrun "Filesystem $FSTYP not supported in _scratch_mkfs_blocksized"
		;;
	esac
}

_scratch_resvblks()
{
	case $FSTYP in
	xfs)
		xfs_io -x -c "resblks $1" $SCRATCH_MNT
		;;
	*)
		;;
	esac
}


# Repair scratch filesystem.  Returns 0 if the FS is good to go (either no
# errors found or errors were fixed) and nonzero otherwise; also spits out
# a complaint on stderr if fsck didn't tell us that the FS is good to go.
_repair_scratch_fs()
{
    case $FSTYP in
    xfs)
        _scratch_xfs_repair "$@" 2>&1
	local res=$?
	if [ "$res" -ne 0 ]; then
		echo "xfs_repair returns $res; replay log?"
		_try_scratch_mount
		res=$?
		if [ "$res" -gt 0 ]; then
			echo "mount returns $res; zap log?"
			_scratch_xfs_repair -L 2>&1
			echo "log zap returns $?"
		else
			umount "$SCRATCH_MNT"
		fi
		_scratch_xfs_repair "$@" 2>&1
		res=$?
	fi
	if [ $res -ne 0 ]; then
		_dump_err2 "xfs_repair failed, err=$res"
	fi
	return $res
        ;;
    btrfs)
	echo "yes|$BTRFS_UTIL_PROG check --repair --force $SCRATCH_DEV"
	yes | $BTRFS_UTIL_PROG check --repair --force $SCRATCH_DEV 2>&1
	local res=$?
	if [ $res -ne 0 ]; then
		_dump_err2 "btrfs repair failed, err=$res"
	fi
	return $res
	;;
    bcachefs)
	# With bcachefs, if fsck detects any errors we consider it a bug and we
	# want the test to fail:
	_check_scratch_fs
	;;
    *)
	local dev=$SCRATCH_DEV
	local fstyp=$FSTYP
	if [ $FSTYP = "overlay" -a -n "$OVL_BASE_SCRATCH_DEV" ]; then
		_repair_overlay_scratch_fs
		# Fall through to repair base fs
		dev=$OVL_BASE_SCRATCH_DEV
		fstyp=$OVL_BASE_FSTYP
		$UMOUNT_PROG $OVL_BASE_SCRATCH_MNT
	fi
	# Let's hope fsck -y suffices...
	fsck -t $fstyp -y $dev 2>&1
	local res=$?
	case $res in
	$FSCK_OK|$FSCK_NONDESTRUCT|$FSCK_REBOOT)
		res=0
		;;
	*)
		_dump_err2 "fsck.$FSTYP failed, err=$res"
		;;
	esac
	return $res
        ;;
    esac
}

_repair_test_fs()
{
	case $FSTYP in
	xfs)
		_test_xfs_repair "$@" >$tmp.repair 2>&1
		res=$?
		if [ "$res" -ne 0 ]; then
			echo "xfs_repair returns $res; replay log?" >>$tmp.repair
			_test_mount
			res=$?
			if [ $res -gt 0 ]; then
				echo "mount returns $res; zap log?" >>$tmp.repair
				_xfs_repair_test_fs -L >>$tmp.repair 2>&1
				echo "log zap returns $?" >> $tmp.repair
			else
				umount "$TEST_DEV"
			fi
			_xfs_repair_test_fs "$@" >>$tmp.repair 2>&1
			res=$?
		fi
		;;
	btrfs)
	echo 'yes|$BTRFS_UTIL_PROG check --repair --force "$TEST_DEV"' > \
								/tmp.repair 2>&1
	yes | $BTRFS_UTIL_PROG check --repair --force "$TEST_DEV" >> \
								/tmp.repair 2>&1
		res=$?
		;;
	*)
		# Let's hope fsck -y suffices...
		fsck -t $FSTYP -y $TEST_DEV >$tmp.repair 2>&1
		res=$?
		if test "$res" -lt 4 ; then
			res=0
		fi
		;;
	esac
	if [ $res -ne 0 ]; then
		_log_err "_repair_test_fs: failed, err=$res"
		echo "*** fsck.$FSTYP output ***"	>>$seqres.full
		cat $tmp.repair				>>$seqres.full
		echo "*** end fsck.$FSTYP output"	>>$seqres.full

	fi
	rm -f $tmp.repair
	return $res
}

_get_pids_by_name()
{
    if [ $# -ne 1 ]
    then
	echo "Usage: _get_pids_by_name process-name" 1>&2
	exit 1
    fi

    # Algorithm ... all ps(1) variants have a time of the form MM:SS or
    # HH:MM:SS before the psargs field, use this as the search anchor.
    #
    # Matches with $1 (process-name) occur if the first psarg is $1
    # or ends in /$1 ... the matching uses sed's regular expressions,
    # so passing a regex into $1 will work.

    ps $PS_ALL_FLAGS \
    | sed -n \
	-e 's/$/ /' \
	-e 's/[ 	][ 	]*/ /g' \
	-e 's/^ //' \
	-e 's/^[^ ]* //' \
	-e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
	-e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
}

#
# _df_device : get an IRIX style df line for a given device
#
#       - returns "" if not mounted
#       - returns fs type in field two (ala IRIX)
#       - joins line together if split by fancy df formatting
#       - strips header etc
#

_df_device()
{
    if [ $# -ne 1 ]
    then
	echo "Usage: _df_device device" 1>&2
	exit 1
    fi

    # Note that we use "==" here so awk doesn't try to interpret an NFS over
    # IPv6 server as a regular expression.
    $DF_PROG 2>/dev/null | $AWK_PROG -v what=$1 '
        ($1==what) && (NF==1) {
            v=$1
            getline
            print v, $0
            exit
        }
        ($1==what) {
            print
            exit
        }
    '
}

#
# _df_dir : get an IRIX style df line for device where a directory resides
#
#       - returns fs type in field two (ala IRIX)
#       - joins line together if split by fancy df formatting
#       - strips header etc
#

_df_dir()
{
    if [ $# -ne 1 ]
    then
	echo "Usage: _df_dir device" 1>&2
	exit 1
    fi

    $DF_PROG $1 2>/dev/null | $AWK_PROG -v what=$1 '
        NR == 2 && NF==1 {
            v=$1
            getline
            print v, $0;
            exit 0
        }
        NR == 2 {
            print;
            exit 0
        }
        {}
    '
    # otherwise, nada
}

# return percentage used disk space for mounted device

_used()
{
    if [ $# -ne 1 ]
    then
	echo "Usage: _used device" 1>&2
	exit 1
    fi

    _df_device $1 | $AWK_PROG '{ sub("%", "") ; print $6 }'
}

# return the FS type of a mounted device
#
_fs_type()
{
    if [ $# -ne 1 ]
    then
	echo "Usage: _fs_type device" 1>&2
	exit 1
    fi

    #
    # The Linux kernel shows NFSv4 filesystems in df output as
    # filesystem type nfs4, although we mounted it as nfs earlier.
    # Fix the filesystem type up here so that the callers don't
    # have to bother with this quirk.
    #
    _df_device $1 | $AWK_PROG '{ print $2 }' | \
        sed -e 's/nfs4/nfs/' -e 's/fuse.glusterfs/glusterfs/' \
            -e 's/fuse.ceph-fuse/ceph-fuse/'
}

# return the FS mount options of a mounted device
#
# should write a version which just parses the output of mount for IRIX
# compatibility, but since this isn't used at all, at the moment I'll leave
# this for now
#
_fs_options()
{
    if [ $# -ne 1 ]
    then
	echo "Usage: _fs_options device" 1>&2
	exit 1
    fi

    $AWK_PROG -v dev=$1 '
        match($1,dev) { print $4 }
    ' </proc/mounts
}

# returns device number if a file is a block device
#
_is_block_dev()
{
    if [ $# -ne 1 ]
    then
	echo "Usage: _is_block_dev dev" 1>&2
	exit 1
    fi

    local dev=$1
    if [ -L "$dev" ]; then
        dev=`readlink -f "$dev"`
    fi

    if [ -b "$dev" ]; then
        $here/src/lstat64 "$dev" | $AWK_PROG '/Device type:/ { print $9 }'
    fi
}

# returns device number if a file is a character device
#
_is_char_dev()
{
	if [ $# -ne 1 ]; then
		echo "Usage: _is_char_dev dev" 1>&2
		exit 1
	fi

	local dev=$1
	if [ -L "$dev" ]; then
		dev=`readlink -f "$dev"`
	fi

	if [ -c "$dev" ]; then
		$here/src/lstat64 "$dev" | $AWK_PROG '/Device type:/ { print $9 }'
	fi
}

# Do a command, log it to $seqres.full, optionally test return status
# and die if command fails. If called with one argument _do executes the
# command, logs it, and returns its exit status. With two arguments _do
# first prints the message passed in the first argument, and then "done"
# or "fail" depending on the return status of the command passed in the
# second argument. If the command fails and the variable _do_die_on_error
# is set to "always" or the two argument form is used and _do_die_on_error
# is set to "message_only" _do will print an error message to
# $seqres.out and exit.

_do()
{
    if [ $# -eq 1 ]; then
	local cmd=$1
    elif [ $# -eq 2 ]; then
	local note=$1
	local cmd=$2
	echo -n "$note... "
    else
	echo "Usage: _do [note] cmd" 1>&2
	status=1; exit
    fi

    (eval "echo '---' \"$cmd\"") >>$seqres.full
    (eval "$cmd") >$tmp._out 2>&1
    local ret=$?
    cat $tmp._out >>$seqres.full
    rm -f $tmp._out
    if [ $# -eq 2 ]; then
	if [ $ret -eq 0 ]; then
	    echo "done"
	else
	    echo "fail"
	fi
    fi
    if [ $ret -ne 0  ] \
	&& [ "$_do_die_on_error" = "always" \
	    -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
    then
	[ $# -ne 2 ] && echo
	eval "echo \"$cmd\" failed \(returned $ret\): see $seqres.full"
	status=1; exit
    fi

    return $ret
}

# bail out, setting up .notrun file. Need to kill the filesystem check files
# here, otherwise they are set incorrectly for the next test.
#
_notrun()
{
    echo "$*" > $seqres.notrun
    echo "$seq not run: $*"
    rm -f ${RESULT_DIR}/require_test*
    rm -f ${RESULT_DIR}/require_scratch*

    status=0
    exit
}

# just plain bail out
#
_fail()
{
    echo "$*" | tee -a $seqres.full
    echo "(see $seqres.full for details)"
    status=1
    exit 1
}

# tests whether $FSTYP is one of the supported filesystems for a test
#
_check_supported_fs()
{
	local res=1
	local f

	for f; do
		# ^FS means black listed fs
		if [ "$f" = "^$FSTYP" ]; then
			return 1
		elif [ "$f" = "generic" ] || [[ "$f" == "^"* ]]; then
			# ^FS implies "generic ^FS"
			res=0
		elif [ "$f" = "$FSTYP" ]; then
			return 0
		fi
	done
	return $res
}

_supported_fs()
{
	_check_supported_fs $* || \
		_notrun "not suitable for this filesystem type: $FSTYP"
}

_fixed_in_version()
{
	local pkg=$1
	local ver=$2

	echo "HINT: You _MAY_ be hit by a known issue on $pkg version < $ver." >> $seqres.hints
	echo >> $seqres.hints
}

_fixed_in_kernel_version()
{
	_fixed_in_version kernel $*
}

_fixed_by_git_commit()
{
	local pkg=$1
	shift

	echo "HINT: You _MAY_ be missing $pkg fix:" >> $seqres.hints
	echo "      $*" >> $seqres.hints
	echo >> $seqres.hints
}

_fixed_by_kernel_commit()
{
	_fixed_by_git_commit kernel $*
}

# Compare with _fixed_by_* helpers, this helper is used for test cases
# are not regression tests, e.g. functional tests or maintainer tests,
# this helper suggests git commits that should be applied to source trees
# to avoid test failures.
_wants_git_commit()
{
	local pkg=$1
	shift

	echo "This test wants $pkg fix:" >> $seqres.hints
	echo "      $*" >> $seqres.hints
	echo >> $seqres.hints
}

# Refer to _wants_git_commit
_wants_kernel_commit()
{
	_wants_git_commit kernel $*
}

_check_if_dev_already_mounted()
{
	local dev=$1
	local mnt=$2

	# find $dev as the source, and print result in "$dev $mnt" format
	local mount_rec=`findmnt -rncv -S $dev -o SOURCE,TARGET`
	[ -n "$mount_rec" ] || return 1 # 1 = not mounted

	# if it's mounted, make sure its on $mnt
	if [ "$mount_rec" != "$dev $mnt" ]; then
		echo "$devname=$dev is mounted but not on $mntname=$mnt - aborting"
		echo "Already mounted result:"
		echo $mount_rec
		return 2 # 2 = mounted on wrong mnt
	fi
}

# check if a FS on a device is mounted
# if so, verify that it is mounted on mount point
# if fstype is given as argument, verify that it is also
# mounted with correct fs type
#
_check_mounted_on()
{
	local devname=$1
	local dev=$2
	local mntname=$3
	local mnt=$4
	local type=$5

	# this check doesn't work on ceph-fuse
	if [ "$dev" != "ceph-fuse" ]; then
		_check_if_dev_already_mounted $dev $mnt
		dev_already_mounted=$?

		if [ $dev_already_mounted -ne 0 ]; then
			return $dev_already_mounted
		fi
	fi

	if [ -n "$type" -a "`_fs_type $dev`" != "$type" ]; then
		echo "$devname=$dev is mounted but not a type $type filesystem"
		# raw $DF_PROG cannot handle NFS/AFS/CIFS/overlay correctly
		_df_device $dev
		return 3 # 3 = mounted as wrong type
	fi
	return 0 # 0 = mounted as expected
}

# this test needs a scratch partition - check we're ok & unmount it
# No post-test check of the device is required. e.g. the test intentionally
# finishes the test with the filesystem in a corrupt state
_require_scratch_nocheck()
{
    case "$FSTYP" in
	glusterfs)
		echo $SCRATCH_DEV | grep -E -q ":/?" > /dev/null 2>&1
		if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]; then
			_notrun "this test requires a valid \$SCRATCH_DEV"
		fi
		if [ ! -d "$SCRATCH_MNT" ]; then
			_notrun "this test requires a valid \$SCRATCH_MNT"
		fi
		;;
	9p|fuse|virtiofs)
		if [ -z "$SCRATCH_DEV" ]; then
			_notrun "this test requires a valid \$SCRATCH_DEV"
		fi
		if [ ! -d "$SCRATCH_MNT" ]; then
			_notrun "this test requires a valid \$SCRATCH_MNT"
		fi
		;;
	nfs*)
		echo $SCRATCH_DEV | grep -q ":/" > /dev/null 2>&1
		if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]; then
			_notrun "this test requires a valid \$SCRATCH_DEV"
		fi
		if [ ! -d "$SCRATCH_MNT" ]; then
			_notrun "this test requires a valid \$SCRATCH_MNT"
		fi
		;;
	afs)
		# We only support RW volumes (marked with a '%')
		# We don't support RO volumes (marked with a '#')
		echo $SCRATCH_DEV | grep -q "^%" > /dev/null 2>&1
		if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]; then
			_notrun "this test requires a valid \$SCRATCH_DEV (must be a RW volume)"
		fi
		if [ ! -d "$SCRATCH_MNT" ]; then
			_notrun "this test requires a valid \$SCRATCH_MNT"
		fi
		;;
	ceph)
		echo $SCRATCH_DEV | grep -qE "=/|:/" > /dev/null 2>&1
		if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]; then
			_notrun "this test requires a valid \$SCRATCH_DEV"
		fi
		if [ ! -d "$SCRATCH_MNT" ]; then
			_notrun "this test requires a valid \$SCRATCH_MNT"
		fi
		;;
	pvfs2)
		echo $SCRATCH_DEV | grep -q "://" > /dev/null 2>&1
		if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]; then
			_notrun "this test requires a valid \$SCRATCH_DEV"
		fi
		if [ ! -d "$SCRATCH_MNT" ]; then
			_notrun "this test requires a valid \$SCRATCH_MNT"
		fi
		;;
	cifs)
		echo $SCRATCH_DEV | grep -q "//" > /dev/null 2>&1
		if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]; then
			_notrun "this test requires a valid \$SCRATCH_DEV"
		fi
		if [ ! -d "$SCRATCH_MNT" ]; then
		     _notrun "this test requires a valid \$SCRATCH_MNT"
		fi
		;;
	overlay)
		if [ -z "$OVL_BASE_SCRATCH_MNT" -o ! -d "$OVL_BASE_SCRATCH_MNT" ]; then
			_notrun "this test requires a valid \$OVL_BASE_SCRATCH_MNT as ovl base dir"
		fi
		# if $SCRATCH_MNT is derived from $OVL_BASE_SCRATCH_MNT then
		# don't check $SCRATCH_MNT dir here because base fs may not be mounted
		# and we will create the mount point anyway on _overlay_mount
		if [ "$SCRATCH_MNT" != "$OVL_BASE_SCRATCH_MNT/$OVL_MNT" -a ! -d "$SCRATCH_MNT" ]; then
			_notrun "this test requires a valid \$SCRATCH_MNT"
		fi
		;;
	tmpfs)
		if [ -z "$SCRATCH_DEV" -o ! -d "$SCRATCH_MNT" ];
		then
		    _notrun "this test requires a valid \$SCRATCH_MNT and unique \$SCRATCH_DEV"
		fi
		;;
	ubifs)
		# ubifs needs an UBI volume. This will be a char device, not a block device.
		if [ ! -c "$SCRATCH_DEV" ]; then
			_notrun "this test requires a valid UBI volume for \$SCRATCH_DEV"
		fi
		if [ ! -d "$SCRATCH_MNT" ]; then
			_notrun "this test requires a valid \$SCRATCH_MNT"
		fi
		;;
	*)
		 if [ -z "$SCRATCH_DEV" -o "`_is_block_dev "$SCRATCH_DEV"`" = "" ]
		 then
		     _notrun "this test requires a valid \$SCRATCH_DEV"
		 fi
		 if [ "`_is_block_dev "$SCRATCH_DEV"`" = "`_is_block_dev "$TEST_DEV"`" ]
		 then
		     _notrun "this test requires a valid \$SCRATCH_DEV"
		 fi
		if [ ! -d "$SCRATCH_MNT" ]
		then
		     _notrun "this test requires a valid \$SCRATCH_MNT"
		fi
		 ;;
    esac

    _check_mounted_on SCRATCH_DEV $SCRATCH_DEV SCRATCH_MNT $SCRATCH_MNT
    local err=$?
    [ $err -le 1 ] || exit 1
    if [ $err -eq 0 ]
    then
        # if it's mounted, unmount it
        if ! _scratch_unmount
        then
            echo "failed to unmount $SCRATCH_DEV"
            exit 1
        fi
    fi
    rm -f ${RESULT_DIR}/require_scratch "$RESULT_DIR/.skip_orebuild" "$RESULT_DIR/.skip_rebuild"
}

# we need the scratch device and it needs to not be an lvm device
_require_scratch_nolvm()
{
	_require_scratch_nocheck

	# This works if we don't have LVM, all we want is to skip if the scratch
	# device is an lvm device.
	$LVM_PROG lvdisplay $SCRATCH_DEV > /dev/null 2>&1
	[ $? -eq 0 ] && _notrun "test requires a non-lvm scratch device"
}

_require_no_compress()
{
	case "$FSTYP" in
	btrfs)
		_require_btrfs_no_compress
		;;
	*)
		;;
	esac
}

# we need the scratch device and it should be checked post test.
_require_scratch()
{
	_require_scratch_nocheck
	touch ${RESULT_DIR}/require_scratch
}

# require a scratch dev of a minimum size (in kb)
_require_scratch_size()
{
	[ $# -eq 1 ] || _fail "_require_scratch_size: expected size param"

	_require_scratch
	local devsize=`_get_device_size $SCRATCH_DEV`
	[ $devsize -lt $1 ] && _notrun "scratch dev too small"
}

# require a scratch dev of a minimum size (in kb) and should not be checked
# post test
_require_scratch_size_nocheck()
{
	[ $# -eq 1 ] || _fail "_require_scratch_size: expected size param"

	_require_scratch_nocheck
	local devsize=`_get_device_size $SCRATCH_DEV`
	[ $devsize -lt $1 ] && _notrun "scratch dev too small"
}

# Require scratch fs which supports >16T of filesystem size.
# _require_scratch must be called before this function is called.
_require_scratch_16T_support()
{
	case $FSTYP in
	ext2|ext3|f2fs)
		_notrun "$FSTYP doesn't support >16T filesystem"
		;;
	ext4)
		_scratch_mkfs >> $seqres.full 2>&1
		_scratch_mount
		local blocksize=$(_get_block_size $SCRATCH_MNT)
		if [ $blocksize -lt 4096 ]; then
			_notrun "This test requires >16T fs support"
		fi
		_scratch_unmount
		;;
	*)
		;;
	esac
}

# Require scratch fs supports delay allocation.
_require_scratch_delalloc()
{
	_require_command "$FILEFRAG_PROG" filefrag

	_scratch_mkfs > $seqres.full
	_scratch_mount
	$XFS_IO_PROG -f -c 'pwrite 0 64k' $SCRATCH_MNT/testy &> /dev/null
	$FILEFRAG_PROG -v $SCRATCH_MNT/testy 2>&1 | grep -q delalloc || \
		_notrun "test requires delayed allocation buffered writes"
	_scratch_unmount
}

# this test needs a test partition - check we're ok & mount it
#
_require_test()
{
    case "$FSTYP" in
	glusterfs)
		echo $TEST_DEV | grep -E -q ":/?" > /dev/null 2>&1
		if [ -z "$TEST_DEV" -o "$?" != "0" ]; then
			_notrun "this test requires a valid \$TEST_DEV"
		fi
		if [ ! -d "$TEST_DIR" ]; then
			_notrun "this test requires a valid \$TEST_DIR"
		fi
		;;
	9p|fuse|virtiofs)
		if [ -z "$TEST_DEV" ]; then
			_notrun "this test requires a valid \$TEST_DEV"
		fi
		if [ ! -d "$TEST_DIR" ]; then
			_notrun "this test requires a valid \$TEST_DIR"
		fi
		;;
	nfs*)
		echo $TEST_DEV | grep -q ":/" > /dev/null 2>&1
		if [ -z "$TEST_DEV" -o "$?" != "0" ]; then
			_notrun "this test requires a valid \$TEST_DEV"
		fi
		if [ ! -d "$TEST_DIR" ]; then
			_notrun "this test requires a valid \$TEST_DIR"
		fi
		;;
	afs)
		# We only support RW volumes (marked with a '%')
		# We don't support RO volumes (marked with a '#')
		echo $TEST_DEV | grep -q "^%" > /dev/null 2>&1
		if [ -z "$TEST_DEV" -o "$?" != "0" ]; then
			_notrun "this test requires a valid \$TEST_DEV"
		fi
		if [ ! -d "$TEST_DIR" ]; then
			_notrun "this test requires a valid \$TEST_DIR"
		fi
		;;
	ceph)
		echo $TEST_DEV | grep -qE "=/|:/" > /dev/null 2>&1
		if [ -z "$TEST_DEV" -o "$?" != "0" ]; then
			_notrun "this test requires a valid \$TEST_DEV"
		fi
		if [ ! -d "$TEST_DIR" ]; then
			_notrun "this test requires a valid \$TEST_DIR"
		fi
		;;
	ceph-fuse)
		;;
	cifs)
		echo $TEST_DEV | grep -q "//" > /dev/null 2>&1
		if [ -z "$TEST_DEV" -o "$?" != "0" ]; then
			_notrun "this test requires a valid \$TEST_DEV"
		fi
		if [ ! -d "$TEST_DIR" ]; then
		     _notrun "this test requires a valid \$TEST_DIR"
		fi
		;;
	pvfs2)
		echo $TEST_DEV | grep -q "://" > /dev/null 2>&1
		if [ -z "$TEST_DEV" -o "$?" != "0" ]; then
			_notrun "this test requires a valid \$TEST_DIR"
		fi
		if [ ! -d "$TEST_DIR" ]; then
			_notrun "this test requires a valid \$TEST_DIR"
		fi
		;;
	overlay)
		if [ -z "$OVL_BASE_TEST_DIR" -o ! -d "$OVL_BASE_TEST_DIR" ]; then
			_notrun "this test requires a valid \$TEST_DIR as ovl base dir"
		fi
		if [ ! -d "$TEST_DIR" ]; then
			_notrun "this test requires a valid \$TEST_DIR"
		fi
		;;
	tmpfs)
		if [ -z "$TEST_DEV" -o ! -d "$TEST_DIR" ];
		then
		    _notrun "this test requires a valid \$TEST_DIR and unique $TEST_DEV"
		fi
		;;
	ubifs)
		# ubifs needs an UBI volume. This will be a char device, not a block device.
		if [ ! -c "$TEST_DEV" ]; then
			_notrun "this test requires a valid UBI volume for \$TEST_DEV"
		fi
		if [ ! -d "$TEST_DIR" ]; then
			_notrun "this test requires a valid \$TEST_DIR"
		fi
		;;
	*)
		 if [ -z "$TEST_DEV" ] || [ "`_is_block_dev "$TEST_DEV"`" = "" ]
		 then
		     _notrun "this test requires a valid \$TEST_DEV"
		 fi
		 if [ "`_is_block_dev "$SCRATCH_DEV"`" = "`_is_block_dev "$TEST_DEV"`" ]
		 then
		     _notrun "this test requires a valid \$TEST_DEV"
		 fi
		if [ ! -d "$TEST_DIR" ]
		then
		     _notrun "this test requires a valid \$TEST_DIR"
		fi
		 ;;
    esac

    _check_mounted_on TEST_DEV $TEST_DEV TEST_DIR $TEST_DIR
    local err=$?
    [ $err -le 1 ] || exit 1
    if [ $err -ne 0 ]
    then
	if ! _test_mount
	then
		echo "!!! failed to mount $TEST_DEV on $TEST_DIR"
		exit 1
	fi
    fi
    touch ${RESULT_DIR}/require_test
}

_has_logdev()
{
	local ret=0
	[ -z "$SCRATCH_LOGDEV" -o ! -b "$SCRATCH_LOGDEV" ] && ret=1
	[ "$USE_EXTERNAL" != yes ] && ret=1

	return $ret
}

# this test needs a logdev
#
_require_logdev()
{
    [ -z "$SCRATCH_LOGDEV" -o ! -b "$SCRATCH_LOGDEV" ] && \
        _notrun "This test requires a valid \$SCRATCH_LOGDEV"
    [ "$USE_EXTERNAL" != yes ] && \
        _notrun "This test requires USE_EXTERNAL to be enabled"

    # ensure its not mounted
    $UMOUNT_PROG $SCRATCH_LOGDEV 2>/dev/null
}

# This test requires that an external log device is not in use
#
_require_no_logdev()
{
	[ "$USE_EXTERNAL" = "yes" ] && [ -n "$SCRATCH_LOGDEV" ] && \
		_notrun "Test not compatible with external logs, skipped this test"
}

# this test requires loopback device support
#
_require_loop()
{
    modprobe loop >/dev/null 2>&1
    if grep loop /proc/devices >/dev/null 2>&1
    then
	:
    else
	_notrun "This test requires loopback device support"
    fi

    # loop device does not handle zone information
    _require_non_zoned_device ${TEST_DEV}
}

# this test requires kernel support for a secondary filesystem
#
_require_extra_fs()
{
	modprobe "$1" >/dev/null 2>&1
	grep -q -w "$1" /proc/filesystems ||
		_notrun "this test requires $1 support"
}

# this test requires that (large) loopback device files are not in use
#
_require_no_large_scratch_dev()
{
    [ "$LARGE_SCRATCH_DEV" = yes ] && \
	_notrun "Large filesystem testing in progress, skipped this test"
}

# this test requires that a realtime subvolume is in use, and
# that the kernel supports realtime as well.
#
_require_realtime()
{
    [ "$USE_EXTERNAL" = yes ] || \
	_notrun "External volumes not in use, skipped this test"
    [ "$SCRATCH_RTDEV" = "" ] && \
	_notrun "Realtime device required, skipped this test"
}

# This test requires that a realtime subvolume is not in use
#
_require_no_realtime()
{
	[ "$USE_EXTERNAL" = "yes" ] && [ -n "$SCRATCH_RTDEV" ] && \
		_notrun "Test not compatible with realtime subvolumes, skipped this test"
}

# this test requires that a specified command (executable) exists
# $1 - command, $2 - name for error message
#
# Note: the command string might have parameters, so strip them before checking
# whether it is executable.
_require_command()
{
	if [ $# -eq 2 ]; then
		local name="$2"
	elif [ $# -eq 1 ]; then
		local name="$1"
	else
		_fail "usage: _require_command <command> [<name>]"
	fi

	local command=`echo "$1" | awk '{ print $1 }'`
	if [ ! -x "$command" ]; then
		_notrun "$name utility required, skipped this test"
	fi
}

# this test requires the device to be valid block device
# $1 - device
_require_block_device()
{
	if [ -z "$1" ]; then
		echo "Usage: _require_block_device <dev>" 1>&2
		exit 1
	fi
	if [ "`_is_block_dev "$1"`" == "" ]; then
		_notrun "require $1 to be valid block disk"
	fi
}

# this test requires a path to refere to a local block or character device
# $1 - device
_require_local_device()
{
	if [ -z "$1" ]; then
		echo "Usage: _require_local_device <dev>" 1>&2
		exit 1
	fi
	if [ "`_is_block_dev "$1"`" != "" ]; then
		return 0
	fi
	if [ "`_is_char_dev "$1"`" != "" ]; then
		return 0
	fi
	_notrun "require $1 to be local device"
}

# brd based ram disks erase the device when they receive a flush command when no
# active references are present. This causes problems for DM devices sitting on
# top of brd devices as DM doesn't hold active references to the brd device.
_require_sane_bdev_flush()
{
	echo $1 | grep -q "^/dev/ram[0-9]\+$"
	if [ $? -eq 0 ]; then
		_notrun "This test requires a sane block device flush"
	fi
}

# Decide if the scratch filesystem is likely to be mounted in fsdax mode.
# It goes 3 ways based on mount options::
#	1. "dax" or "dax=always" means always test using DAX
#	2. "dax=never" means we'll never use DAX.
#	3. "dax=inode" or nothing means "use scratch dev capability" to
#	    determine whether DAX is going to be used.
#
# Case 2 and 3 basically mean the same thing for the purpose of
# _require_dm_target(). If the fs is not forcing the use of DAX, then DAX
# can only be enabled if the underlying block device supports it.
#
# Returns 0 if the filesytem will use DAX, 1 if it won't.
__scratch_uses_fsdax()
{
	local ops=$(_normalize_mount_options "$MOUNT_OPTIONS")

	echo $ops | grep -E -qw "dax(=always| |$)" && return 0
	return 1
}

# Determine if the scratch device is DAX capable. Even if the fs is not
# using DAX, we still can't use certain device mapper targets if the block
# device is DAX capable. Hence the check needs to be separat from the FS
# capability.
__scratch_dev_has_dax()
{
	local sysfs="/sys/block/$(_short_dev $SCRATCH_DEV)"
	test -e "${sysfs}/dax" && return 0
	test "$(cat "${sysfs}/queue/dax" 2>/dev/null)" = "1" && return 0

	return 1
}

# this test requires a specific device mapper target
_require_dm_target()
{
	local target=$1
	local fsdax
	local bdevdax

	# require SCRATCH_DEV to be a valid block device with sane BLKFLSBUF
	# behaviour
	_require_block_device $SCRATCH_DEV
	_require_sane_bdev_flush $SCRATCH_DEV
	_require_command "$DMSETUP_PROG" dmsetup

	case $target in
	stripe|linear|log-writes)
		;;
	*)
		if __scratch_uses_fsdax; then
			_notrun "Cannot run tests with fsdax on $target devices."
		fi
		if __scratch_dev_has_dax; then
			_notrun "Cannot use $target devices on DAX capable block devices."
		fi
		;;
	esac

	modprobe dm-$target >/dev/null 2>&1

	$DMSETUP_PROG targets 2>&1 | grep -q ^$target
	if [ $? -ne 0 ]; then
		_notrun "This test requires dm $target support"
	fi

	# dm-error cannot handle the zone information
	#
	# dm-snapshot and dm-thin-pool cannot ensure sequential writes on
	# the backing device
	case $target in
	error|snapshot|thin-pool)
		_require_non_zoned_device ${SCRATCH_DEV}
		;;
	esac
}

_zone_type()
{
	local target=$1
	if [ -z $target ]; then
		echo "Usage: _zone_type <device>"
		exit 1
	fi
	local sdev=`_short_dev $target`

	if [ -e /sys/block/${sdev}/queue/zoned ]; then
		cat /sys/block/${sdev}/queue/zoned
	else
		echo none
	fi
}

_require_zoned_device()
{
	local target=$1
	if [ -z $target ]; then
		echo "Usage: _require_zoned_device <device>"
		exit 1
	fi

	local type=`_zone_type ${target}`
	if [ "${type}" = "none" ]; then
		_notrun "this test require zoned block device"
	fi
}

_require_non_zoned_device()
{
	local target=$1
	if [ -z $target ]; then
		echo "Usage: _require_non_zoned_device <device>"
		exit 1
	fi

	local type=`_zone_type ${target}`
	if [ "${type}" != "none" ]; then
		_notrun "this test require non-zoned block device"
	fi
}

# this test requires that external log/realtime devices are not in use
#
_require_nonexternal()
{
    [ "$USE_EXTERNAL" = yes ] && \
	_notrun "External device testing in progress, skipped this test"
}

# this test requires that the kernel supports asynchronous I/O
_require_aio()
{
	$here/src/feature -A
	case $? in
	0)
		;;
	1)
		_notrun "kernel does not support asynchronous I/O"
		;;
	*)
		_fail "unexpected error testing for asynchronous I/O support"
		;;
	esac
}

# this test requires that a (specified) aio-dio executable exists
# and that the kernel supports asynchronous I/O.
# $1 - command (optional)
#
_require_aiodio()
{
    if [ -z "$1" ]
    then
        AIO_TEST=$here/src/aio-dio-regress/aiodio_sparse2
        [ -x $AIO_TEST ] || _notrun "aio-dio utilities required"
    else
        AIO_TEST=$here/src/aio-dio-regress/$1
        [ -x $AIO_TEST ] || _notrun "$AIO_TEST not built"
    fi
    _require_aio
    _require_odirect
}

# this test requires that the kernel supports IO_URING
_require_io_uring()
{
	$here/src/feature -R
	case $? in
	0)
		;;
	1)
		_notrun "kernel does not support IO_URING"
		;;
	*)
		_fail "unexpected error testing for IO_URING support"
		;;
	esac
}

# test whether the mount_setattr syscall is available
_require_mount_setattr()
{
	$here/src/feature -r
	case $? in
	0)
		;;
	1)
		_notrun "kernel does not support mount_setattr syscall"
		;;
	*)
		_fail "unexpected error testing for mount_setattr support"
		;;
	esac
}

# test whether idmapped mounts are supported
_require_idmapped_mounts()
{
        IDMAPPED_MOUNTS_TEST=$here/src/vfs/vfstest
        [ -x $IDMAPPED_MOUNTS_TEST ] || _notrun "vfstest utilities required"

	_require_mount_setattr

	$here/src/vfs/vfstest --idmapped-mounts-supported \
		--device "$TEST_DEV" \
		--mount "$TEST_DIR" \
		--fstype "$FSTYP"

	if [ $? -ne 0 ]; then
		_notrun "vfstest not support by $FSTYP"
	fi
}

# this test requires that a test program exists under src/
# $1 - command (require)
#
_require_test_program()
{
    local prog=$here/src/$1
    [ -x $prog ] || _notrun "$prog not built"
}

# run an aio-dio program
# $1 - command
_run_aiodio()
{
    if [ -z "$1" ]
    then
        echo "usage: _run_aiodio command_name" 2>&1
        status=1; exit 1
    fi

    _require_aiodio $1

    local testtemp=$TEST_DIR/aio-testfile
    rm -f $testtemp
    $AIO_TEST $testtemp 2>&1
    status=$?
    rm -f $testtemp

    return $status
}

_require_timestamp_range()
{
	local device=${1:-$TEST_DEV}

	local tsmin tsmax
	read tsmin tsmax <<<$(_filesystem_timestamp_range $device)
	if [ $tsmin -eq -1 -a $tsmax -eq -1 ]; then
		_notrun "filesystem $FSTYP timestamp bounds are unknown"
	fi

	# expect console warning from rw scratch mount if fs limit is near
	if [ $tsmax -le $((1<<31)) ] && \
		! _check_dmesg_for "filesystem being mounted at .* supports timestamps until"
	then
		_notrun "Kernel does not support timestamp limits"
	fi
}

_filesystem_timestamp_range()
{
	local device=${1:-$TEST_DEV}
	local fstyp=${2:-$FSTYP}
	u32max=$(((1<<32)-1))
	s32min=-$((1<<31))
	s32max=$(((1<<31)-1))
	s64max=$(((1<<63)-1))
	s64min=$((1<<63))

	case $fstyp in
	ext2)
		echo "$s32min $s32max"
		;;
	ext3|ext4)
		if [ $(dumpe2fs -h $device 2>/dev/null | grep "Inode size:" | cut -d: -f2) -gt 128 ]; then
			printf "%d %d\n" $s32min 0x37fffffff
		else
			echo "$s32min $s32max"
		fi
		;;

	jfs)
		echo "0 $u32max"
		;;
	xfs)
		_xfs_timestamp_range "$device"
		;;
	btrfs)
		echo "$s64min $s64max"
		;;
	overlay)
		if [ ! -z $OVL_BASE_FSTYP -a $OVL_BASE_FSTYP != "overlay" ]; then
			_filesystem_timestamp_range $OVL_BASE_TEST_DEV $OVL_BASE_FSTYP
		else
			echo "-1 -1"
		fi
		;;
	*)
		echo "-1 -1"
		;;
	esac
}

# indicate whether YP/NIS is active or not
#
_yp_active()
{
	local dn
	dn=$(domainname 2>/dev/null)
	local ypcat=$(type -P ypcat)
	local rpcinfo=$(type -P rpcinfo)
	test -n "${dn}" -a "${dn}" != "(none)" -a "${dn}" != "localdomain" -a -n "${ypcat}" -a -n "${rpcinfo}" && \
		"${rpcinfo}" -p localhost 2>/dev/null | grep -q ypbind
	echo $?
}

# cat the password file
#
_cat_passwd()
{
	[ $(_yp_active) -eq 0 ] && ypcat passwd
	cat /etc/passwd
}

# cat the group file
#
_cat_group()
{
	[ $(_yp_active) -eq 0 ] && ypcat group
	cat /etc/group
}

# check if a user exists in the system
#
_require_user_exists()
{
	local user=$1
	_cat_passwd | grep -q "^$user:"
	[ "$?" == "0" ] || _notrun "$user user not defined."
}

# check if a user exists and is able to execute commands.
# Uses 'fsgqa' user as default.
#
_require_user()
{
	qa_user=fsgqa
	if [ -n "$1" ];then
		qa_user=$1
	fi
	_require_user_exists $qa_user
	echo /bin/true | su $qa_user
	[ "$?" == "0" ] || _notrun "$qa_user cannot execute commands."
}

# check for a chown support
#
_require_chown()
{
	local rnd_uid=4242
	local file="$TEST_DIR/chown_testfile"

	rm -f $file
	touch $file
	chown ${rnd_uid}:${rnd_uid} $file >/dev/null 2>&1 \
		|| _notrun "chown is not supported ${FSTYP}"
}


# check for a chmod support
# Since chmod sometimes fails silently actual functionality test is done
#
_require_chmod()
{
	local file="$TEST_DIR/chmod_testfile"

	rm -f $file
	touch $file

	# get original file mode
	local mode=`stat --format="0%a" $file`
	# flip the user's read bit
	let mode^=0400
	chmod `printf '%o' "$mode"` $file
	# check that the chmod actually flipped the bit
	[ `stat --format="0%a" $file` == `printf '0%o' "$mode"` ] \
		|| _notrun "chmod is not supported ${FSTYP}"
}

# check for a group on the machine, fsgqa as default
#
_require_group()
{
    qa_group=fsgqa
    if [ -n "$1" ];then
        qa_group=$1
    fi
    _cat_group | grep -q "^$qa_group:"
    [ "$?" == "0" ] || _notrun "$qa_group group not defined."
}

_filter_user_do()
{
        perl -ne "
s,.*Permission\sdenied.*,Permission denied,;
s,.*no\saccess\sto\stty.*,,;
s,.*no\sjob\scontrol\sin\sthis\sshell.*,,;
s,^\s*$,,;
        print;"
}

_user_do()
{
	echo $1 | su -s /bin/bash $qa_user 2>&1 | _filter_user_do
}

_require_xfs_io_command()
{
	if [ -z "$1" ]
	then
		echo "Usage: _require_xfs_io_command command [switch]" 1>&2
		exit 1
	fi
	local command=$1
	shift
	local param="$*"
	local param_checked=""
	local opts=""
	local attr_info=""

	local testfile=$TEST_DIR/$$.xfs_io
	local testio
	case $command in
	"lsattr")
		# Test xfs_io lsattr support and filesystem FS_IOC_FSSETXATTR
		# support.
		testio=`$XFS_IO_PROG -F -f -c "lsattr $param" $testfile 2>&1`
		param_checked="$param"
		;;
	"chattr")
		local testdir=$TEST_DIR/$$.attr_dir
		mkdir $TEST_DIR/$$.attr_dir
		if [ -z "$param" ]; then
			param=s
		fi
		# Test xfs_io chattr support AND
		# filesystem FS_IOC_FSSETXATTR support
		# 'tPnE' flags are only valid for a directory so check them on a directory.
		if echo "$param" | grep -E -q 't|P|n|E'; then
			testio=`$XFS_IO_PROG -F -c "chattr +$param" $testdir 2>&1`
			attr_info=`$XFS_IO_PROG -F -r -c "lsattr" $testdir | awk '{print $1}'`
			$XFS_IO_PROG -F -r -c "chattr -$param" $testdir 2>&1
		else
			testio=`$XFS_IO_PROG -F -f -c "chattr +$param" $testfile 2>&1`
			attr_info=`$XFS_IO_PROG -F -r -c "lsattr" $testfile | awk '{print $1}'`
			$XFS_IO_PROG -F -r -c "chattr -$param" $testfile 2>&1
		fi
		param_checked="+$param"
		rm -rf $testdir 2>&1 > /dev/null
		;;
	"chproj")
		testio=`$XFS_IO_PROG -F -f -c "chproj 0" $testfile 2>&1`
		;;
	"copy_range")
		local testcopy=$TEST_DIR/$$.copy.xfs_io
		local copy_opts=$testfile
		if [ "$param" == "-f" ]; then
			# source file is the open destination file
			testcopy=$testfile
			copy_opts="0 -d 4k -l 4k"
		fi
		$XFS_IO_PROG -F -f -c "pwrite 0 4k" $testfile > /dev/null 2>&1
		testio=`$XFS_IO_PROG -F -f -c "copy_range $param $copy_opts" $testcopy 2>&1`
		rm -f $testcopy > /dev/null 2>&1
		param_checked="$param"
		;;
	"falloc"|"allocsp")
		testio=`$XFS_IO_PROG -F -f -c "$command $param 0 1m" $testfile 2>&1`
		param_checked="$param"
		;;
	"fpunch" | "fcollapse" | "zero" | "fzero" | "finsert" | "funshare")
		local blocksize=$(_get_file_block_size $TEST_DIR)
		testio=`$XFS_IO_PROG -F -f -c "pwrite 0 $((5 * $blocksize))" \
			-c "fsync" -c "$command $blocksize $((2 * $blocksize))" \
			$testfile 2>&1`
		;;
	"fiemap")
		# If 'ranged' is passed as argument then we check to see if fiemap supports
		# ranged query params
		if echo "$param" | grep -q "ranged"; then
			param=$(echo "$param" | sed "s/ranged//")
			$XFS_IO_PROG -c "help fiemap" | grep -q "\[offset \[len\]\]"
			[ $? -eq 0 ] || _notrun "xfs_io $command ranged support is missing"
		fi
		testio=`$XFS_IO_PROG -F -f -c "pwrite 0 20k" -c "fsync" \
			-c "fiemap -v $param" $testfile 2>&1`
		param_checked="$param"
		;;
	"flink")
		local testlink=$TEST_DIR/$$.link.xfs_io
		testio=`$XFS_IO_PROG -F -f -c "flink $testlink" $testfile 2>&1`
		rm -f $testlink > /dev/null 2>&1
		;;
	"-T")
		# Check O_TMPFILE support in xfs_io, kernel and fs
		testio=`$XFS_IO_PROG -T -c quit $TEST_DIR 2>&1`
		echo $testio | grep -E -q "invalid option|Is a directory" && \
			_notrun "xfs_io $command support is missing"
		echo $testio | grep -q "Operation not supported" && \
			_notrun "O_TMPFILE is not supported"
		;;
	"fsmap")
		testio=`$XFS_IO_PROG -f -c "fsmap" $testfile 2>&1`
		echo $testio | grep -q "Inappropriate ioctl" && \
			_notrun "xfs_io $command support is missing"
		;;
	"label")
		testio=`$XFS_IO_PROG -c "label" $TEST_DIR 2>&1`
		;;
	"open")
		# -c "open $f" is broken in xfs_io <= 4.8. Along with the fix,
		# a new -C flag was introduced to execute one shot commands.
		# Check for -C flag support as an indication for the bug fix.
		testio=`$XFS_IO_PROG -F -f -C "open $testfile" $testfile 2>&1`
		echo $testio | grep -q "invalid option" && \
			_notrun "xfs_io $command support is missing"
		;;
	"pwrite")
		# -N (RWF_NOWAIT) only works with direct vectored I/O writes
		local pwrite_opts=" "
		if [ "$param" == "-N" ]; then
			opts+=" -d"
			pwrite_opts+="-V 1 -b 4k"
		fi
		testio=`$XFS_IO_PROG -f $opts -c \
		        "pwrite $pwrite_opts $param 0 4k" $testfile 2>&1`
		param_checked="$pwrite_opts $param"
		;;
	"scrub"|"repair")
		test -z "$param" && param="probe"
		testio=`$XFS_IO_PROG -x -c "$command $param" $TEST_DIR 2>&1`
		echo $testio | grep -q "Inappropriate ioctl" && \
			_notrun "xfs_io $command support is missing"
		param_checked="$param"
		;;
	"startupdate"|"commitupdate"|"cancelupdate")
		$XFS_IO_PROG -f -c 'pwrite -S 0x58 0 128k -b 128k' $testfile > /dev/null
		testio=$($XFS_IO_PROG -c "startupdate $param" \
				-c 'pwrite -S 0x59 0 192k' \
				-c 'commitupdate' $testfile 2>&1)
		echo $testio | grep -q "Inappropriate ioctl" && \
			_notrun "xfs_io $command $param support is missing"
		echo $testio | grep -q "Operation not supported" && \
			_notrun "xfs_io $command $param kernel support is missing"
		param_checked="$param"
		;;
	"swapext")
		$XFS_IO_PROG -f -c 'pwrite -S 0x58 0 128k -b 128k' $testfile > /dev/null
		$XFS_IO_PROG -f -c 'truncate 128k' $testfile.1 > /dev/null
		testio=`$XFS_IO_PROG -c "$command $param $testfile.1" $testfile 2>&1`
		echo $testio | grep -q "bad argument count" && \
			_notrun "xfs_io $command $param support is missing"
		echo $testio | grep -q "Inappropriate ioctl" && \
			_notrun "xfs_io $command $param ioctl support is missing"
		echo $testio | grep -q "Operation not supported" && \
			_notrun "xfs_io $command $param kernel support is missing"
		rm -f $testfile.1
		param_checked="$param"
		;;
	"utimes" )
		testio=`$XFS_IO_PROG -f -c "utimes 0 0 0 0" $testfile 2>&1`
		;;
	"syncfs")
		touch $testfile
		testio=`$XFS_IO_PROG -c "syncfs" $testfile 2>&1`
		;;
	*)
		testio=`$XFS_IO_PROG -c "help $command" 2>&1`
	esac

	rm -f $testfile 2>&1 > /dev/null
	echo $testio | grep -q "not found" && \
		_notrun "xfs_io $command $param_checked support is missing"
	echo $testio | grep -q "Operation not supported\|Inappropriate ioctl" && \
		_notrun "xfs_io $command $param_checked failed (old kernel/wrong fs?)"
	echo $testio | grep -q "Invalid" && \
		_notrun "xfs_io $command $param_checked failed (old kernel/wrong fs/bad args?)"
	echo $testio | grep -q "foreign file active" && \
		_notrun "xfs_io $command $param_checked not supported on $FSTYP"
	echo $testio | grep -q "Function not implemented" && \
		_notrun "xfs_io $command $param_checked support is missing (missing syscall?)"
	echo $testio | grep -q "unknown flag" && \
		_notrun "xfs_io $command $param_checked support is missing (unknown flag)"

	[ -n "$param" ] || return

	if [ -z "$param_checked" ]; then
		$XFS_IO_PROG -c "help $command" | grep -E -q "^ $param ([a-zA-Z0-9_]+ )?--" || \
			_notrun "xfs_io $command doesn't support $param"
	else
		# xfs_io could result in "command %c not supported" if it was
		# built on kernels not supporting pwritev2() calls
		echo $testio | grep -q "\(invalid option\|not supported\)" && \
			_notrun "xfs_io $command doesn't support $param"
	fi

	# On XFS, ioctl(FSSETXATTR)(called by xfs_io -c "chattr") maskes off unsupported
	# or invalid flags silently so need to check these flags by extra ioctl(FSGETXATTR)
	# (called by xfs_io -c "lsattr").
	# The following URL explains why we don't change the behavior of XFS.
	# https://www.spinics.net/lists/linux-xfs/msg44725.html
	if [ -n "$attr_info" -a "$FSTYP" = "xfs" ]; then
		local num=${#param}
		for i in $(seq 0 $((num-1))); do
			echo $attr_info | grep -q "${param:$i:1}" || \
				_notrun "xfs_io $command +${param:$i:1} support is missing (unknown flag in kernel)"
		done
	fi
}

# check that kernel and filesystem support direct I/O, and check if "$1" size
# aligned (optional) is supported
_require_odirect()
{
	local blocksize=$1
	local align_args=${1:+"-b $1"}

	if [ $FSTYP = "ext4" ] || [ $FSTYP = "f2fs" ] ; then
		if echo "$MOUNT_OPTIONS" | grep -q "test_dummy_encryption"; then
			_notrun "$FSTYP encryption doesn't support O_DIRECT"
		fi
	fi
	if [ $FSTYP = "ext4" ] ; then
		if echo "$MOUNT_OPTIONS" | grep -q "data=journal"; then
			_notrun "ext4 data journaling doesn't support O_DIRECT"
		fi
	fi
	local testfile=$TEST_DIR/$$.direct
	$XFS_IO_PROG -F -f -d -c "pwrite ${align_args} 0 20k" $testfile > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		if [ -n "$blocksize" ]; then
			_notrun "O_DIRECT aligned to $blocksize bytes is not supported"
		else
			_notrun "O_DIRECT is not supported"
		fi
	fi
	rm -f $testfile 2>&1 > /dev/null
}

# Format a swapfile and return its size in bytes
_format_swapfile() {
	local fname="$1"
	local sz="$2"
	local swap_log=""

	rm -f "$fname"
	touch "$fname"
	chmod 0600 "$fname"
	# Swap files must be nocow on Btrfs.
	$CHATTR_PROG +C "$fname" > /dev/null 2>&1
	_pwrite_byte 0x61 0 "$sz" "$fname" >> $seqres.full
	# Ignore permission complaints on filesystems that don't support perms
	swap_log=$($MKSWAP_PROG "$fname" 2>&1 | grep -v "insecure permission")
	echo $swap_log >> $seqres.full

	echo $swap_log | grep -oP '\w+(?= bytes)'
}

_swapon_file() {
	local fname="$1"

	# Ignore permission complaints on filesystems that don't support perms
	$(swapon "$fname" 2> >(grep -v "insecure permissions" >&2))
}

# Check that the filesystem supports swapfiles
_require_scratch_swapfile()
{
	_require_scratch
	_require_command "$MKSWAP_PROG" "mkswap"

	_scratch_mkfs >/dev/null 2>&1

	# With mounting SELinux context(e.g. system_u:object_r:root_t:s0),
	# standard mkswap tried to reset the type of default context to
	# swapfile_t if it's not swapfile_t, and then it failed and returned
	# ENOTSUP expectedly as we don't want to create any SELinux attr on
	# purpose.  standard mkswap ignored this relabel error by commit
	# d97dc0e of util-linux, but it still reported the error before
	# commit d97dc0e.  We mount swapfile context directly to skip the
	# reset step.
	[ -n "$SELINUX_MOUNT_OPTIONS" ] && export \
		SELINUX_MOUNT_OPTIONS="-o context=system_u:object_r:swapfile_t:s0"

	_scratch_mount

	# Minimum size for mkswap is 10 pages
	_format_swapfile "$SCRATCH_MNT/swap" $(($(_get_page_size) * 10)) > /dev/null

	# ext* has supported all variants of swap files since their
	# introduction, so swapon should not fail.
	case "$FSTYP" in
	ext2|ext3|ext4)
		if ! swapon "$SCRATCH_MNT/swap" >/dev/null 2>&1; then
			if _check_s_dax "$SCRATCH_MNT/swap" 1 >/dev/null; then
				_scratch_unmount
				_notrun "swapfiles are not supported"
			else
				_scratch_unmount
				_fail "swapon failed for $FSTYP"
			fi
		fi
		;;
	*)
		if ! swapon "$SCRATCH_MNT/swap" >/dev/null 2>&1; then
			_scratch_unmount
			_notrun "swapfiles are not supported"
		fi
		;;
	esac

	swapoff "$SCRATCH_MNT/swap" >/dev/null 2>&1
	_scratch_unmount
}

# Check that a fs has enough free space (in 1024b blocks)
#
_require_fs_space()
{
	local mnt=$1
	local blocks=$2	# in units of 1024
	local gb=$(( blocks / 1024 / 1024 ))

	local free_blocks=`df -kP $mnt | grep -v Filesystem | awk '{print $4}'`
	[ $free_blocks -lt $blocks ] && \
		_notrun "This test requires at least ${gb}GB free on $mnt to run"
}

#
# Check if the filesystem supports sparse files.
#
# Filesystems (such as CIFS mounted from a Windows server) that generally
# support sparse files but are tricked into creating a non-sparse file by one
# of the tests are treated here as not supporting sparse files. This special
# treatment is done due to media wear-out concerns -- e.g., generic/129 would
# write multiple terabytes of zeros if allowed to run on a filesystem that
# ignores the request to make a file sparse.
#
_require_sparse_files()
{
    local testfile="$TEST_DIR/$$.sparsefiletest"
    rm -f "$testfile"

    # The write size and offset are specifically chosen to trick the Windows
    # SMB server implementation into dishonoring the request to create a sparse
    # file, while still fitting into the 64 kb SMB1 maximum request size.
    # This also creates a non-sparse file on vfat, exfat, and hfsplus.
    $XFS_IO_PROG -f -c 'pwrite -b 51200 -S 0x61 1638400 51200' "$testfile" >/dev/null

    resulting_file_size_kb=$( du -sk "$testfile" | cut -f 1 )
    rm -f "$testfile"

    # The threshold of 1 MB allows for filesystems with such large clusters.
    [ $resulting_file_size_kb -gt 1024 ] && \
	_notrun "Sparse files are not supported or do not work as expected"
}

_require_debugfs()
{
    #boot_params always present in debugfs
    [ -d "$DEBUGFS_MNT/boot_params" ] || _notrun "Debugfs not mounted"
}

# The default behavior of SEEK_HOLE is to always return EOF.
# Filesystems that implement non-default behavior return the offset
# of holes with SEEK_HOLE. There is no way to query the filesystem
# of which behavior it is implementing.
# We use this whitelist FSTYP, to set expectation and avoid silent
# regression of filesystem seek hole behavior.
#
# Return 0 for true
_fstyp_has_non_default_seek_data_hole()
{
	if [ -z $1 ]; then
		local fstyp=$FSTYP
	else
		local fstyp=$1
	fi

	case "$fstyp" in
	btrfs|ext4|xfs|cifs|f2fs|gfs2|ocfs2|tmpfs)
		return 0
		;;
	nfs*)
		# NFSv2, NFSv3, and NFSv4.0/4.1 only support default behavior of SEEK_HOLE,
		# while NFSv4.2 supports non-default behavior
		local nfsvers=`_mount() | grep $TEST_DEV | sed -n 's/^.*vers=\([0-9.]*\).*$/\1/p'`
		[ "$nfsvers" = "4.2" ]
		return $?
		;;
	overlay)
		if [ ! -z $OVL_BASE_FSTYP -a $OVL_BASE_FSTYP != "overlay" ]; then
			_fstyp_has_non_default_seek_data_hole $OVL_BASE_FSTYP
			return $?
		else
			# Assume that base fs has default behavior
			return 1
		fi
		;;
	*)
		# by default fstyp has default SEEK_HOLE behavior;
		# if your fs has non-default behavior, add it to whitelist above!
		return 1
		;;
	esac
}

# Run seek sanity test with predefined expectation for SEEK_DATA/HOLE behavior
_run_seek_sanity_test()
{
	local testseekargs
	if _fstyp_has_non_default_seek_data_hole; then
		testseekargs+="-f"
	fi
	$here/src/seek_sanity_test $testseekargs $*
}

# Check if the file system supports seek_data/hole
_require_seek_data_hole()
{
	local dev=${1:-$TEST_DEV}
	local testfile=$TEST_DIR/$$.seek
	local testseek=`$here/src/seek_sanity_test -t $testfile 2>&1`

	rm -f $testfile &>/dev/null
	echo $testseek | grep -q "Kernel does not support" && \
		_notrun "File system does not support llseek(2) SEEK_DATA/HOLE"
	# Disable extent zeroing for ext4 as that change where holes are
	# created
	if [ "$FSTYP" = "ext4" ]; then
		_ext4_disable_extent_zeroout $dev
	fi
}

_require_runas()
{
	_require_test_program "runas"
}

_runas()
{
	"$here/src/runas" "$@"
}

_require_richacl_prog()
{
	_require_command "$GETRICHACL_PROG" getrichacl
	_require_command "$SETRICHACL_PROG" setrichacl
}

_require_scratch_richacl_xfs()
{
	_scratch_mkfs_xfs_supported -m richacl=1 >/dev/null 2>&1 \
		|| _notrun "mkfs.xfs doesn't have richacl feature"
	_scratch_mkfs_xfs -m richacl=1 >/dev/null 2>&1
	_try_scratch_mount >/dev/null 2>&1 \
		|| _notrun "kernel doesn't support richacl feature on $FSTYP"
	_scratch_unmount
}

_require_scratch_richacl_support()
{
	_scratch_mount
	$GETFATTR_PROG -n system.richacl >/dev/null 2>&1 \
		|| _notrun "this test requires richacl support on \$SCRATCH_DEV"
	_scratch_unmount
}

_require_scratch_richacl()
{
	case "$FSTYP" in
	xfs)    _require_scratch_richacl_xfs
		;;
	ext4)   _require_scratch_richacl_ext4
		;;
	nfs*|cifs|overlay)
		_require_scratch_richacl_support
		;;
	*)      _notrun "this test requires richacl support on \$SCRATCH_DEV"
		;;
	esac
}

_scratch_mkfs_richacl()
{
	case "$FSTYP" in
	xfs)    _scratch_mkfs_xfs -m richacl=1
		;;
	ext4)   _scratch_mkfs -O richacl
		;;
	nfs*|afs|cifs|overlay)
		_scratch_mkfs
		;;
	esac
}

# check if the given device is mounted, if so, return mount point
_is_dev_mounted()
{
	local dev=$1
	local fstype=${2:-$FSTYP}

	if [ $# -lt 1 ]; then
		echo "Usage: _is_dev_mounted <device> [fstype]" 1>&2
		exit 1
	fi

	findmnt -rncv -S $dev -t $fstype -o TARGET | head -1
}

# check if the given dir is a mount point, if so, return mount point
_is_dir_mountpoint()
{
	local dir=$1
	local fstype=${2:-$FSTYP}

	if [ $# -lt 1 ]; then
		echo "Uasge: _is_dir_mountpoint <dir> [fstype]" 1>&2
		exit 1
	fi

	findmnt -rncv -t $fstype -o TARGET $dir | head -1
}

# remount a FS to a new mode (ro or rw)
#
_remount()
{
    if [ $# -ne 2 ]
    then
	echo "Usage: _remount device ro/rw" 1>&2
	exit 1
    fi
    local device=$1
    local mode=$2

    if ! mount -o remount,$mode $device
    then
        echo "_remount: failed to remount filesystem on $device as $mode"
        exit 1
    fi
}

# Run the appropriate repair/check on a filesystem
#
# if the filesystem is mounted, it's either remounted ro before being
# checked or it's unmounted and then remounted
#

# If set, we remount ro instead of unmounting for fsck
USE_REMOUNT=0

_umount_or_remount_ro()
{
    if [ $# -ne 1 ]
    then
	echo "Usage: _umount_or_remount_ro <device>" 1>&2
	exit 1
    fi

    local device=$1
    local mountpoint=`_is_dev_mounted $device`

    if [ $USE_REMOUNT -eq 0 ]; then
        $UMOUNT_PROG $device
    else
        _remount $device ro
    fi
    echo "$mountpoint"
}

_mount_or_remount_rw()
{
	if [ $# -ne 3 ]; then
		echo "Usage: _mount_or_remount_rw <opts> <dev> <mnt>" 1>&2
		exit 1
	fi
	local mount_opts=$1
	local device=$2
	local mountpoint=$3

	if [ $USE_REMOUNT -eq 0 ]; then
		if [ "$FSTYP" != "overlay" ]; then
			_mount -t $FSTYP$FUSE_SUBTYP $mount_opts $device $mountpoint
			_idmapped_mount $device $mountpoint
		else
			_overlay_mount $device $mountpoint
		fi
		if [ $? -ne 0 ]; then
			_dump_err "!!! failed to remount $device on $mountpoint"
			return 0 # ok=0
		fi
	else
		_remount $device rw
	fi

	return 1 # ok=1
}

# Check a generic filesystem in no-op mode; this assumes that the
# underlying fsck program accepts "-n" for a no-op (check-only) run,
# and that it will still return an errno for corruption in this mode.
#
# Filesystems which don't support this will need to define their
# own check routine.
#
_check_generic_filesystem()
{
    local device=$1

    # If type is set, we're mounted
    local type=`_fs_type $device`
    local ok=1

    if [ "$type" = "$FSTYP" ]
    then
        # mounted ...
        local mountpoint=`_umount_or_remount_ro $device`
    fi

    fsck -t $FSTYP $FSCK_OPTIONS $device >$tmp.fsck 2>&1
    if [ $? -ne 0 ]
    then
	_log_err "_check_generic_filesystem: filesystem on $device is inconsistent"
        echo "*** fsck.$FSTYP output ***"	>>$seqres.full
        cat $tmp.fsck				>>$seqres.full
        echo "*** end fsck.$FSTYP output"	>>$seqres.full

        ok=0
    fi
    rm -f $tmp.fsck

    if [ $ok -eq 0 ] && [ -n "$DUMP_CORRUPT_FS" ]; then
        case "$FSTYP" in
        ext*)
            local flatdev="$(basename "$device")"
            _ext4_metadump "$seqres.$flatdev.check.qcow2" "$device" compress
            ;;
        esac
    fi

    if [ $ok -eq 0 ]
    then
        echo "*** mount output ***"		>>$seqres.full
        _mount					>>$seqres.full
        echo "*** end mount output"		>>$seqres.full
    elif [ "$type" = "$FSTYP" ]
    then
	# was mounted ...
	_mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
	ok=$?
    fi

    if [ $ok -eq 0 ]; then
	status=1
	if [ "$iam" != "check" ]; then
		exit 1
	fi
	return 1
    fi

    return 0
}

# Filter the knowen errors the UDF Verifier reports.
_udf_test_known_error_filter()
{
	grep -E -v "PVD  60  Error: Interchange Level: 1, Maximum Interchange Level: 0|FSD  28  Error: Interchange Level: 1, Maximum Interchange Level: 1,|PVD  72  Warning: Volume Set Identifier: \"\*IRIX UDF\",|Warning: [0-9]+ unused blocks NOT marked as unallocated."

}

_check_udf_filesystem()
{
    [ "$DISABLE_UDF_TEST" == "1" ] && return

    if [ $# -ne 1 -a $# -ne 2 ]
    then
	echo "Usage: _check_udf_filesystem device [last_block]" 1>&2
	exit 1
    fi

    if [ ! -x $here/src/udf_test ]
    then
	echo "udf_test not installed, please download and build the Philips"
	echo "UDF Verification Software from http://www.extra.research.philips.com/udf/."
	echo "Then copy the udf_test binary to $here/src/."
	echo "If you do not wish to run udf_test then set environment variable DISABLE_UDF_TEST"
	echo "to 1."
	return
    fi

    local device=$1
    local blksz=`echo $MKFS_OPTIONS | sed -rn 's/.*(-b|--blocksize)[ =]?+([0-9]+).*/\2/p'`
    if [ -z "$blksz" ]; then
	blksz=512
    fi
    # Is the filesystem mounted?
    local type=`_fs_type $device`
    if [ "$type" = "$FSTYP" ]; then
	blksz=`blockdev --getbsz $device`
	local mountpoint=`_umount_or_remount_ro $device`
    fi

    local opt_arg="-ecclength 1 -blocksize $blksz"
    if [ $# -eq 2 ]; then
	opt_arg+=" -lastvalidblock $(( $2 - 1 ))"
    fi

    rm -f $seqres.checkfs
    sleep 1 # Due to a problem with time stamps in udf_test
    $here/src/udf_test $opt_arg $device | tee $seqres.checkfs | grep -E "Error|Warning" | \
	_udf_test_known_error_filter | \
	grep -E -iv "Error count:.*[0-9]+.*total occurrences:.*[0-9]+|Warning count:.*[0-9]+.*total occurrences:.*[0-9]+" && \
        echo "Warning UDF Verifier reported errors see $seqres.checkfs." && return 1
    # Remount the filesystem
    if [ "$type" = "$FSTYP" ]; then
	_mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
    fi
    return 0
}

_check_test_fs()
{
    case $FSTYP in
    xfs)
	_check_xfs_test_fs
	;;
    nfs)
	# no way to check consistency for nfs
	;;
    afs)
	# no way to check consistency for afs
	;;
    cifs)
	# no way to check consistency for cifs
	;;
    9p)
	# no way to check consistency for 9p
	;;
    fuse)
	# no way to check consistency for fuse
	;;
    virtiofs)
	# no way to check consistency for virtiofs
	;;
    ceph|ceph-fuse)
	# no way to check consistency for CephFS
	;;
    glusterfs)
	# no way to check consistency for GlusterFS
	;;
    overlay)
	_check_overlay_test_fs
	;;
    pvfs2)
	;;
    udf)
	# do nothing for now
	;;
    btrfs)
	_check_btrfs_filesystem $TEST_DEV
	;;
    tmpfs)
	# no way to check consistency for tmpfs
	;;
    ubifs)
	# there is no fsck program for ubifs yet
	;;
    *)
	_check_generic_filesystem $TEST_DEV
	;;
    esac
}

_check_scratch_fs()
{
    local device=$SCRATCH_DEV
    [ $# -eq 1 ] && device=$1

    case $FSTYP in
    xfs)
	local scratch_log="none"
	local scratch_rt="none"
	[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
	    scratch_log="$SCRATCH_LOGDEV"

	[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
	    scratch_rt="$SCRATCH_RTDEV"

	_check_xfs_filesystem $device $scratch_log $scratch_rt
	;;
    udf)
	_check_udf_filesystem $device $udf_fsize
	;;
    nfs*)
	# Don't know how to check an NFS filesystem, yet.
	;;
    afs*)
	# Don't know how to check an AFS filesystem, yet.
	;;
    cifs)
	# Don't know how to check a CIFS filesystem, yet.
	;;
    9p)
	# no way to check consistency for 9p
	;;
    fuse)
	# no way to check consistency for fuse
	;;
    virtiofs)
	# no way to check consistency for virtiofs
	;;
    ceph)
	# no way to check consistency for CephFS
	;;
    glusterfs)
	# no way to check consistency for GlusterFS
	;;
    overlay)
	_check_overlay_scratch_fs
	;;
    pvfs2)
	;;
    btrfs)
	_check_btrfs_filesystem $device
	;;
    tmpfs)
	# no way to check consistency for tmpfs
	;;
    ubifs)
	# there is no fsck program for ubifs yet
	;;
    *)
	_check_generic_filesystem $device
	;;
    esac
}

_full_fstyp_details()
{
     [ -z "$FSTYP" ] && FSTYP=xfs
     if [ $FSTYP = xfs ]; then
	if [ -d /proc/fs/xfs ]; then
	    if grep -q 'debug 0' /proc/fs/xfs/stat; then
		FSTYP="$FSTYP (non-debug)"
	    elif grep -q 'debug 1' /proc/fs/xfs/stat; then
		FSTYP="$FSTYP (debug)"
	    fi
	else
	    if uname -a | grep -qi 'debug'; then
		FSTYP="$FSTYP (debug)"
	    else
		FSTYP="$FSTYP (non-debug)"
	    fi
	fi
     fi
     echo $FSTYP
}

_full_platform_details()
{
     local os=`uname -s`
     local host=`hostname -s`
     local kernel=`uname -rv`
     local platform=`uname -m`
     echo "$os/$platform $host $kernel"
}

_get_os_name()
{
	if [ "`uname`" == "Linux" ]; then
		echo 'linux'
	else
		echo Unknown operating system: `uname`
		exit
	fi
}

_link_out_file_named()
{
	local features=$2
	local suffix=$(FEATURES="$features" perl -e '
		my %feathash;
		my $feature, $result, $suffix, $opts;

		foreach $feature (split(/,/, $ENV{"FEATURES"})) {
			$feathash{$feature} = 1;
		}
		$result = "default";
		while (<>) {
			my $found = 1;

			chomp;
			($opts, $suffix) = split(/ *: */);
			foreach my $opt (split(/,/, $opts)) {
				if (!exists($feathash{$opt})) {
					$found = 0;
					last;
				}
			}
			if ($found == 1) {
				$result = $suffix;
				last;
			}
		}
		print $result
		' <$seqfull.cfg)
	rm -f $1 || _fail "_link_out_file_named: failed to remove existing output file"
	ln -fs $(basename $1).$suffix $1 || _fail "$(basename $1).$suffix: could not setup output file"
}

_link_out_file()
{
	local features

	if [ $# -eq 0 ]; then
		features="$(_get_os_name),$FSTYP"
		if [ -n "$MOUNT_OPTIONS" ]; then
			features=$features,${MOUNT_OPTIONS##"-o "}
		fi
	else
		features=$1
	fi

	_link_out_file_named $seqfull.out "$features"
}

_die()
{
        echo $@
        exit 1
}

# convert urandom incompressible data to compressible text data
_ddt()
{
	od /dev/urandom | dd iflag=fullblock ${*}
}

#takes files, randomdata
_nfiles()
{
        local f=0
        while [ $f -lt $1 ]
        do
                local file=f$f
                echo > $file
                if [ $size -gt 0 ]; then
                    if [ "$2" == "false" ]; then
                        dd if=/dev/zero of=$file bs=1024 count=$size 2>&1 | _filter_dd
                    elif [ "$2" == "comp" ]; then
                        _ddt of=$file bs=1024 count=$size 2>&1 | _filter_dd
                    else
                        dd if=/dev/urandom of=$file bs=1024 count=$size 2>&1 | _filter_dd
                    fi
                fi
		let f=$f+1
        done
}

# takes dirname, depth, randomdata
_descend()
{
        local dirname=$1 depth=$2 randomdata=$3
        mkdir $dirname  || die "mkdir $dirname failed"
        cd $dirname

        _nfiles $files $randomdata          # files for this dir and data type

        [ $depth -eq 0 ] && return
        local deep=$(( depth - 1 )) # go 1 down

        [ $verbose = true ] && echo "descending, depth from leaves = $deep"

        local d=0
        while [ $d -lt $dirs ]
        do
                _descend d$d $deep &
		let d=$d+1
                wait
        done
}

# Populate a filesystem with inodes for performance experiments
#
# usage: populate [-v] [-n ndirs] [-f nfiles] [-d depth] [-r root] [-s size] [-x]
#
_populate_fs()
{
    local here=`pwd`
    local dirs=5          # ndirs in each subdir till leaves
    local size=0          # sizeof files in K
    local files=100       # num files in _each_ subdir
    local depth=2         # depth of tree from root to leaves
    local verbose=false
    local root=root       # path of initial root of directory tree
    local randomdata=false # -x data type urandom, zero or compressible
    local c

    OPTIND=1
    while getopts "d:f:n:r:s:v:x:c" c
    do
        case $c in
        d)      depth=$OPTARG;;
        n)      dirs=$OPTARG;;
        f)      files=$OPTARG;;
        s)      size=$OPTARG;;
        v)      verbose=true;;
        r)      root=$OPTARG;;
        x)      randomdata=true;;
        c)      randomdata=comp;;
        esac
    done

    _descend $root $depth $randomdata
    wait

    cd $here

    [ $verbose = true ] && echo done
}

# query whether the given file has the given inode flag set
#
_test_inode_flag()
{
	local flag=$1
	local file=$2

	if $XFS_IO_PROG -r -c 'lsattr -v' "$file" | grep -q "$flag" ; then
		return 0
	fi
	return 1
}

# query the given files extsize allocator hint in bytes (if any)
#
_test_inode_extsz()
{
	local file=$1
	local blocks=""

	blocks=`$XFS_IO_PROG -r -c 'stat' "$file" | \
		awk '/^xattr.extsize =/ { print $3 }'`
	[ -z "$blocks" ] && blocks="0"
	echo $blocks
}

# scratch_dev_pool should contain the disks pool for the btrfs raid
_require_scratch_dev_pool()
{
	local i
	local ndevs

	if [ -z "$SCRATCH_DEV_POOL" ]; then
		_notrun "this test requires a valid \$SCRATCH_DEV_POOL"
	fi

	if [ -z "$1" ]; then
		ndevs=2
	else
		ndevs=$1
	fi

	# btrfs test case needs ndevs or more scratch_dev_pool; other FS not sure
	# so fail it
	case $FSTYP in
	btrfs)
		if [ "`echo $SCRATCH_DEV_POOL|wc -w`" -lt $ndevs ]; then
			_notrun "btrfs and this test needs $ndevs or more disks in SCRATCH_DEV_POOL"
		fi
	;;
	*)
		_notrun "dev_pool is not supported by fstype \"$FSTYP\""
	;;
	esac

	for i in $SCRATCH_DEV_POOL; do
		if [ "`_is_block_dev "$i"`" = "" ]; then
			_notrun "this test requires valid block disk $i"
		fi
		if [ "`_is_block_dev "$i"`" = "`_is_block_dev "$TEST_DEV"`" ]; then
			_notrun "$i is part of TEST_DEV, this test requires unique disks"
		fi
		if _mount | grep -q $i; then
			if ! $UMOUNT_PROG $i; then
		            echo "failed to unmount $i - aborting"
		            exit 1
		        fi
		fi
		# To help better debug when something fails, we remove
		# traces of previous btrfs FS on the dev. For zoned devices we
		# can't use dd as it'll lead to unaligned writes so simply
		# reset the first two zones.
		if [ "`_zone_type "$i"`" = "none" ]; then
			dd if=/dev/zero of=$i bs=4096 count=100 > /dev/null 2>&1
		else
			$BLKZONE_PROG reset -c 2 $i
		fi
	done
}

# ensure devices in SCRATCH_DEV_POOL are of the same size
# must be called after _require_scratch_dev_pool
_require_scratch_dev_pool_equal_size()
{
	local size
	local newsize
	local dev

	# SCRATCH_DEV has been set to the first device in SCRATCH_DEV_POOL
	size=`_get_device_size $SCRATCH_DEV`
	for dev in $SCRATCH_DEV_POOL; do
		newsize=`_get_device_size $dev`
		if [ $size -ne $newsize ]; then
			_notrun "This test requires devices in SCRATCH_DEV_POOL have the same size"
		fi
	done
}


# Check that fio is present, and it is able to execute given jobfile
_require_fio()
{
	local job=$1

	_require_command "$FIO_PROG" fio
	if [ -z "$1" ]; then
		return 1;
	fi

	$FIO_PROG --warnings-fatal --showcmd $job >> $seqres.full 2>&1
	[ $? -eq 0 ] || _notrun "$FIO_PROG too old, see $seqres.full"
}

# Does freeze work on this fs?
_require_freeze()
{
	xfs_freeze -f "$TEST_DIR" >/dev/null 2>&1
	local result=$?
	xfs_freeze -u "$TEST_DIR" >/dev/null 2>&1
	[ $result -eq 0 ] || _notrun "$FSTYP does not support freezing"
}

# Does NFS export work on this fs?
_require_exportfs()
{
	_require_test_program "open_by_handle"

	# virtiofs doesn't support open_by_handle_at(2) yet, though the syscall
	# doesn't return error. It requires stable FUSE nodeids.
	# This feature is expected to be implemented in the future in virtiofs,
	# and this check should be removed by then.
	[ $FSTYP == "virtiofs" ] && \
		_notrun "$FSTYP doesn't support open_by_handle_at(2)"

	mkdir -p "$TEST_DIR"/exportfs_test
	$here/src/open_by_handle -c "$TEST_DIR"/exportfs_test 2>&1 \
		|| _notrun "$FSTYP does not support NFS export"
}


# Does shutdown work on this fs?
_require_scratch_shutdown()
{
	[ -x $here/src/godown ] || _notrun "src/godown executable not found"

	_scratch_mkfs > /dev/null 2>&1 || _notrun "_scratch_mkfs failed on $SCRATCH_DEV"
	_scratch_mount

	if [ $FSTYP = "overlay" ]; then
		if [ -z $OVL_BASE_SCRATCH_DEV ]; then
			# In lagacy overlay usage, it may specify directory as
			# SCRATCH_DEV, in this case OVL_BASE_SCRATCH_DEV
			# will be null, so check OVL_BASE_SCRATCH_DEV before
			# running shutdown to avoid shutting down base fs accidently.
			_notrun "This test requires a valid $OVL_BASE_SCRATCH_DEV as ovl base fs"
		else
			$here/src/godown -f $OVL_BASE_SCRATCH_MNT 2>&1 \
			|| _notrun "Underlying filesystem does not support shutdown"
		fi
	else
		$here/src/godown -f $SCRATCH_MNT 2>&1 \
			|| _notrun "$FSTYP does not support shutdown"
	fi

	_scratch_unmount
}

_check_s_dax()
{
	local target=$1
	local exp_s_dax=$2
	local ret=0

	local attributes=$($XFS_IO_PROG -c 'statx -r' $target | awk '/stat.attributes / { print $3 }')

	# The original attribute bit value, STATX_ATTR_DAX (0x2000), conflicted
	# with STATX_ATTR_MOUNT_ROOT.  Therefore, STATX_ATTR_DAX was changed to
	# 0x00200000.
	#
	# Because DAX tests do not run on root mounts, STATX_ATTR_MOUNT_ROOT
	# should always be 0.  Check for the old flag and fail the test if that
	# occurs.

	if [ $(( attributes & 0x2000 )) -ne 0 ]; then
		echo "$target has an unexpected STATX_ATTR_MOUNT_ROOT flag set"
		echo "which used to be STATX_ATTR_DAX"
		echo "     This test should not be running on the root inode..."
		echo "     Does the kernel have the following patch?"
		echo "     72d1249e2ffd uapi: fix statx attribute value overlap for DAX & MOUNT_ROOT"
	fi

	if [ $exp_s_dax -eq 0 ]; then
		if (( attributes & 0x00200000 )); then
			echo "$target has unexpected S_DAX flag"
			ret=1
		fi
	else
		if ! (( attributes & 0x00200000 )); then
			echo "$target doesn't have expected S_DAX flag"
			ret=2
		fi
	fi
	return $ret
}

_check_xflag()
{
	local target=$1
	local exp_xflag=$2

	if [ $exp_xflag -eq 0 ]; then
		_test_inode_flag dax $target && echo "$target has unexpected FS_XFLAG_DAX flag"
	else
		_test_inode_flag dax $target || echo "$target doesn't have expected FS_XFLAG_DAX flag"
	fi
}

# Make sure the given file access mode is set to use the pagecache.  If
# userspace or kernel don't support statx or STATX_ATTR_DAX, we assume that
# means pagecache.  The sole parameter must be a directory.
_require_pagecache_access() {
	local testfile="$1/testfile"

	touch "$testfile"
	if ! _check_s_dax "$testfile" 0 &>> $seqres.full; then
		rm -f "$testfile"
		_notrun 'test requires pagecache access'
	fi

	rm -f "$testfile"
}

# Check if dax mount options are supported
#
# $1 can be either 'dax=always' or 'dax'
#
# dax=always
#      Check for the new dax options (dax=inode, dax=always or dax=never)
#      by passing "dax=always".
# dax
#      Check for the old dax or new dax=always by passing "dax".
#
# This only accepts 'dax=always' because dax=always, dax=inode and
# dax=never are always supported together.  So if the other options are
# required checking for 'dax=always' indicates support for the other 2.
#
# Return 0 if filesystem/device supports the specified dax option.
# Return 1 if mount fails with the specified dax option.
# Return 2 if /proc/mounts shows wrong dax option.
_check_scratch_dax_mountopt()
{
	local option=$1

	_require_scratch
	_scratch_mkfs > /dev/null 2>&1

	_try_scratch_mount "-o $option" > /dev/null 2>&1 || return 1

	if _fs_options $SCRATCH_DEV | grep -E -q "dax(=always|,|$)"; then
		_scratch_unmount
		return 0
	else
		_scratch_unmount
		return 2
	fi
}

# Throw notrun if _check_scratch_dax_mountopt() returns a non-zero value.
_require_scratch_dax_mountopt()
{
	local mountopt=$1

	_check_scratch_dax_mountopt "$mountopt"
	local res=$?

	[ $res -eq 1 ] && _notrun "mount $SCRATCH_DEV with $mountopt failed"
	[ $res -eq 2 ] && _notrun "$SCRATCH_DEV $FSTYP does not support -o $mountopt"
}

_require_dax_iflag()
{
	_require_xfs_io_command "chattr" "x"
}

# Does norecovery support by this fs?
_require_norecovery()
{
	_try_scratch_mount -o ro,norecovery || \
		_notrun "$FSTYP does not support norecovery"
	_scratch_unmount
}

# Does this filesystem support metadata journaling?
# We exclude ones here that don't; otherwise we assume that it does, so the
# test will run, fail, and motivate someone to update this test for a new
# filesystem.
#
# It's possible that TEST_DEV and SCRATCH_DEV have different features (it'd be
# odd, but possible) so check $TEST_DEV by default, but we can optionall pass
# any dev we want.
_has_metadata_journaling()
{
	if [ -z $1 ]; then
		local dev=$TEST_DEV
	else
		local dev=$1
	fi

	case "$FSTYP" in
	ext2|vfat|msdos|udf|exfat|tmpfs)
		echo "$FSTYP does not support metadata journaling"
		return 1
		;;
	ext4)
		# ext4 could be mkfs'd without a journal...
		_require_dumpe2fs
		$DUMPE2FS_PROG -h $dev 2>&1 | grep -q has_journal || {
			echo "$FSTYP on $dev not configured with metadata journaling"
			return 1
		}
		# ext4 might not load a journal
		if _normalize_mount_options "$MOUNT_OPTIONS" | grep -qw "noload"; then
			echo "mount option \"noload\" not allowed in this test"
			return 1
		fi
		;;
	overlay)
		# metadata journaling check is based on base filesystem configurations
		# and  because -overlay option saves those configurations to OVL_BASE_*,
		# adding restore/override the configurations before/after the check.
		if [ ! -z $OVL_BASE_FSTYP -a $OVL_BASE_FSTYP != "overlay" ]; then
			local ret

			_overlay_config_restore
			_has_metadata_journaling
			ret=$?
			_overlay_config_override
			return $ret
		else
			echo "No metadata journaling support for legacy overlay setup"
			return 1
		fi
		;;
	*)
		# by default we pass; if you need to, add your fs above!
		;;
	esac
	return 0
}

_require_metadata_journaling()
{
	local msg=$(_has_metadata_journaling $@)
	if [ -n "$msg" ]; then
		_notrun "$msg"
	fi
}

_count_extents()
{
	$XFS_IO_PROG -r -c "fiemap" $1 | tail -n +2 | grep -v hole | wc -l
}

# Similar to _count_extents() but if any extent is shared multiples times in
# the file (reflinked to different file offsets), it is accounted as 1 extent
# instead of N extents.
_count_exclusive_extents()
{
	$XFS_IO_PROG -r -c "fiemap" $1 | tail -n +2 | grep -v hole | \
		cut -d ' ' -f 3 | sort | uniq | wc -l
}

_count_holes()
{
	$XFS_IO_PROG -r -c "fiemap" $1 | tail -n +2 | grep hole | wc -l
}

_count_attr_extents()
{
	$XFS_IO_PROG -c "fiemap -a" $1 | tail -n +2 | grep -v hole | wc -l
}

# Get the sector number of the extent at @offset of @file
_get_file_extent_sector()
{
	local file=$1
	local offset=$2
	local result

	result=$($XFS_IO_PROG -c "fiemap $offset" "$file" | \
		 _filter_xfs_io_fiemap | head -n1 | $AWK_PROG '{print $3}')

	# xfs_io fiemap will output nothing if there is only hole, so here
	# to replace the empty string with "hole" instead
	if [ -z "$result" ]; then
		result="hole"
	fi
	echo "$result"
}

# arg 1 is dev to remove and is output of the below eg.
# ls -l /sys/class/block/sdd | rev | cut -d "/" -f 3 | rev
_devmgt_remove()
{
	local lun=$1
	local disk=$2

	echo 1 > /sys/class/scsi_device/${lun}/device/delete || _fail "Remove disk failed"

	stat $disk > /dev/null 2>&1
	while [ $? -eq 0 ]; do
		sleep 1
		stat $disk > /dev/null 2>&1
	done
}

# arg 1 is dev to add and is output of the below eg.
# ls -l /sys/class/block/sdd | rev | cut -d "/" -f 3 | rev
_devmgt_add()
{
	local h
	local tdl
	# arg 1 will be in h:t:d:l format now in the h and "t d l" format
	h=`echo ${1} | cut -d":" -f 1`
	tdl=`echo ${1} | cut -d":" -f 2-|sed 's/:/ /g'`

	echo ${tdl} >  /sys/class/scsi_host/host${h}/scan || _fail "Add disk failed"

	# ensure the device comes online
	local dev_back_oneline=0
	local i
	for i in `seq 1 10`; do
		if [ -d /sys/class/scsi_device/${1}/device/block ]; then
			local dev=`ls /sys/class/scsi_device/${1}/device/block`
			local j
			for j in `seq 1 10`;
			do
				stat /dev/$dev > /dev/null 2>&1
				if [ $? -eq 0 ]; then
					dev_back_oneline=1
					break
				fi
				sleep 1
			done
			break
		else
			sleep 1
		fi
	done
	if [ $dev_back_oneline -eq 0 ]; then
		echo "/dev/$dev online failed" >> $seqres.full
	else
		echo "/dev/$dev is back online" >> $seqres.full
	fi
}

_require_fstrim()
{
	if [ -z "$FSTRIM_PROG" ]; then
		_notrun "This test requires fstrim utility."
	fi
}

_require_batched_discard()
{
	if [ $# -ne 1 ]; then
		echo "Usage: _require_batched_discard mnt_point" 1>&2
		exit 1
	fi
	_require_fstrim

	grep -q -E "not supported|not implemented" <($FSTRIM_PROG $1 2>&1)
	if [ "$?" = "0" ]
	then
		_notrun "FITRIM not supported on $1"
	fi
}

# Given a mountpoint and the device associated with that mountpoint, return the
# maximum start offset that the FITRIM command will accept, in units of 1024
# byte blocks.
_discard_max_offset_kb()
{
	case "$FSTYP" in
	xfs)
		_xfs_discard_max_offset_kb "$1"
		;;
	*)
		$DF_PROG -k | awk -v dev="$2" -v mnt="$1" '$1 == dev && $7 == mnt { print $3 }'
		;;
	esac
}

_require_dumpe2fs()
{
	if [ -z "$DUMPE2FS_PROG" ]; then
		_notrun "This test requires dumpe2fs utility."
	fi
}

_require_ugid_map()
{
	if [ ! -e /proc/self/uid_map ]; then
		_notrun "This test requires procfs uid_map support."
	fi
	if [ ! -e /proc/self/gid_map ]; then
		_notrun "This test requires procfs gid_map support."
	fi
}

_require_fssum()
{
	FSSUM_PROG=$here/src/fssum
	[ -x $FSSUM_PROG ] || _notrun "fssum not built"
}

_require_cloner()
{
	CLONER_PROG=$here/src/cloner
	[ -x $CLONER_PROG ] || \
		_notrun "cloner binary not present at $CLONER_PROG"
}

# Normalize mount options from the option string in $1
# Convert options like "-o opt1,opt2 -oopt3" to "opt1 opt2 opt3"
_normalize_mount_options()
{
	echo "$1" | sed -n 's/-o\s*\(\S*\)/\1/gp'| sed 's/,/ /g'
}

# skip test if $1 contains the given strings in trailing arguments
# Both dax and dax=always are excluded if dax or dax=always is passed
_exclude_mount_option()
{
	local mnt_opts=$(_normalize_mount_options "$1")

	shift
	while [ $# -gt 0 ]; do
		local pattern=$1
		echo "$pattern" | grep -E -q "dax(=always|$)" && \
			pattern="dax(=always| |$)"
		if echo $mnt_opts | grep -E -q "$pattern"; then
			_notrun "mount option \"$1\" not allowed in this test"
		fi
		shift
	done
}

_exclude_scratch_mount_option()
{
	_exclude_mount_option "$MOUNT_OPTIONS" $@
}

_exclude_test_mount_option()
{
	_exclude_mount_option "$TEST_FS_MOUNT_OPTS" $@
}

_require_atime()
{
	_exclude_scratch_mount_option "noatime"
	case $FSTYP in
	nfs|afs|cifs|virtiofs)
		_notrun "atime related mount options have no effect on $FSTYP"
		;;
	ceph|ceph-fuse)
		_notrun "atime not maintained by $FSTYP"
		;;
	esac

}

_require_relatime()
{
        _scratch_mkfs > /dev/null 2>&1
        _try_scratch_mount -o relatime || \
                _notrun "relatime not supported by the current kernel"
	_scratch_unmount
}

_require_userns()
{
	[ -x $here/src/nsexec ] || _notrun "src/nsexec executable not found"
	$here/src/nsexec -U true 2>/dev/null || _notrun "userns not supported by this kernel"
}

_create_loop_device()
{
	local file=$1 dev
	dev=`losetup -f --show $file` || _fail "Cannot assign $file to a loop device"

	# Try to enable asynchronous directio mode on the loopback device so
	# that writeback started by a filesystem mounted on the loop device
	# won't be throttled by buffered writes to the lower filesystem.  This
	# is a performance optimization for tests that want to write a lot of
	# data, so it isn't required to work.
	test -b "$dev" && losetup --direct-io=on $dev 2> /dev/null

	echo $dev
}

_destroy_loop_device()
{
	local dev=$1
	losetup -d $dev || _fail "Cannot destroy loop device $dev"
}

_scale_fsstress_args()
{
    local args=""
    while [ $# -gt 0 ]; do
        case "$1" in
            -n) args="$args $1 $(($2 * $TIME_FACTOR))"; shift ;;
            -p) args="$args $1 $(($2 * $LOAD_FACTOR))"; shift ;;
            *) args="$args $1" ;;
        esac
        shift
    done
    printf '%s\n' "$args"
}

#
# Return the logical block size if running on a block device,
# else substitute the page size.
#
_min_dio_alignment()
{
    local dev=$1

    if [ -b "$dev" ]; then
        blockdev --getss $dev
    else
        $here/src/feature -s
    fi
}

run_check()
{
	echo "# $@" >> $seqres.full 2>&1
	"$@" >> $seqres.full 2>&1 || _fail "failed: '$@'"
}

_require_symlinks()
{
	local target=`mktemp -p $TEST_DIR`
	local link=`mktemp -p $TEST_DIR -u`
	ln -s `basename $target` $link
	if [ "$?" -ne 0 ]; then
		rm -f $target
		_notrun "Require symlinks support"
	fi
	rm -f $target $link
}

_require_hardlinks()
{
	local target=`mktemp -p $TEST_DIR`
	local link=`mktemp -p $TEST_DIR -u`
	ln $target $link
	if [ "$?" -ne 0 ]; then
		rm -f $target
		_notrun "No hardlink support"
	fi
	rm -f $target $link
}

_require_test_fcntl_advisory_locks()
{
	[ "$FSTYP" != "cifs" ] && return 0
	cat /proc/mounts | grep $TEST_DEV | grep cifs | grep -q "nobrl" && return 0
	cat /proc/mounts | grep $TEST_DEV | grep cifs | grep -qE "nounix|forcemand" && \
		_notrun "Require fcntl advisory locks support"
}

_require_test_fcntl_setlease()
{
	_require_test_program "locktest"
	touch $TEST_DIR/setlease_testfile
	$here/src/locktest -t $TEST_DIR/setlease_testfile >/dev/null 2>&1
	local ret=$?
	[ $ret -eq 22 ] && _notrun "Require fcntl setlease support"
	[ "$FSTYP" == "nfs" -a $ret -eq 11 ] && \
		_notrun "NFS requires delegation before setlease"
}

_require_ofd_locks()
{
	# Give a test run by getlk wrlck on testfile.
	# If the running kernel does not support OFD locks,
	# EINVAL will be returned.
	_require_test_program "t_ofd_locks"
	touch $TEST_DIR/ofd_testfile
	$here/src/t_ofd_locks -t $TEST_DIR/ofd_testfile > /dev/null 2>&1
	[ $? -eq 22 ] && _notrun "Require OFD locks support"
}

_require_test_lsattr()
{
	local testio=$(lsattr -d $TEST_DIR 2>&1)
	echo $testio | grep -q "Operation not supported" && \
		_notrun "lsattr not supported by test filesystem type: $FSTYP"
	echo $testio | grep -q "Inappropriate ioctl for device" && \
		_notrun "lsattr not supported by test filesystem type: $FSTYP"
}

_require_chattr()
{
	if [ -z "$1" ]; then
		echo "Usage: _require_chattr <attr>"
		exit 1
	fi
	local attribute=$1

	touch $TEST_DIR/syscalltest
	chattr "+$attribute" $TEST_DIR/syscalltest > $TEST_DIR/syscalltest.out 2>&1
	local ret=$?
	chattr "-$attribute" $TEST_DIR/syscalltest > $TEST_DIR/syscalltest.out 2>&1
	if [ "$ret" -ne 0 ]; then
		_notrun "file system doesn't support chattr +$attribute"
	fi
	cat $TEST_DIR/syscalltest.out >> $seqres.full
	rm -f $TEST_DIR/syscalltest.out
}

_get_total_inode()
{
	if [ -z "$1" ]; then
		echo "Usage: _get_total_inode <mnt>"
		exit 1
	fi
	local nr_inode;
	nr_inode=`$DF_PROG -i $1 | tail -1 | awk '{print $3}'`
	echo $nr_inode
}

_get_used_inode()
{
	if [ -z "$1" ]; then
		echo "Usage: _get_used_inode <mnt>"
		exit 1
	fi
	local nr_inode;
	nr_inode=`$DF_PROG -i $1 | tail -1 | awk '{print $4}'`
	echo $nr_inode
}

_get_used_inode_percent()
{
	if [ -z "$1" ]; then
		echo "Usage: _get_used_inode_percent <mnt>"
		exit 1
	fi
	local pct_inode;
	pct_inode=`$DF_PROG -i $1 | tail -1 | awk '{ print $6 }' | \
		   sed -e 's/%//'`
	echo $pct_inode
}

_get_free_inode()
{
	if [ -z "$1" ]; then
		echo "Usage: _get_free_inode <mnt>"
		exit 1
	fi
	local nr_inode;
	nr_inode=`$DF_PROG -i $1 | tail -1 | awk '{print $5}'`
	echo $nr_inode
}

# get the available space in bytes
#
_get_available_space()
{
	if [ -z "$1" ]; then
		echo "Usage: _get_available_space <mnt>"
		exit 1
	fi
	$DF_PROG -B 1 $1 | tail -n1 | awk '{ print $5 }'
}

# get the total space in bytes
#
_get_total_space()
{
	if [ -z "$1" ]; then
		echo "Usage: _get_total_space <mnt>"
		exit 1
	fi
	$DF_PROG -B 1 $1 | tail -n1 | awk '{ print $3 }'
}

# return device size in kb
_get_device_size()
{
	echo $(($(blockdev --getsz $1) >> 1))
}

# Make sure we actually have dmesg checking set up.
_require_check_dmesg()
{
	test -w /dev/kmsg || \
		_notrun "Test requires writable /dev/kmsg."
}

# Return the dmesg log since the start of this test.  Caller must ensure that
# /dev/kmsg was writable when the test was started so that we can find the
# beginning of this test's log messages; _require_check_dmesg does this.
_dmesg_since_test_start()
{
	# search the dmesg log of last run of $seqnum for possible failures
	# use sed \cregexpc address type, since $seqnum contains "/"
	dmesg | tac | sed -ne "0,\#run fstests $seqnum at $date_time#p" | \
		tac
}

# check dmesg log for a specific string, subject to the same requirements as
# _dmesg_since_test_start.
_check_dmesg_for()
{
	_dmesg_since_test_start | grep -E -q "$1"
}

# Default filter for dmesg scanning.
# Ignore lockdep complaining about its own bugginess when scanning dmesg
# output, because we shouldn't be failing filesystem tests on account of
# lockdep.
_check_dmesg_filter()
{
	local extra_filter=
	local filter_file="${RESULT_DIR}/dmesg_filter"

	test -e "$filter_file" && extra_filter="-f $filter_file"

	grep -E -v -e "BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low" \
		-e "BUG: MAX_STACK_TRACE_ENTRIES too low" \
		$extra_filter
}

# Add a simple expression to the default dmesg filter
_add_dmesg_filter()
{
	local regexp="$1"
	local filter_file="${RESULT_DIR}/dmesg_filter"

	if [ ! -e "$filter_file" ] || ! grep -q "$regexp" "$filter_file"; then
		echo "$regexp" >> "${RESULT_DIR}/dmesg_filter"
	fi
}

# check dmesg log for WARNING/Oops/etc.
_check_dmesg()
{
	if [ ! -f ${RESULT_DIR}/check_dmesg ]; then
		return 0
	fi
	rm -f ${RESULT_DIR}/check_dmesg

	# default filter is a simple cat command, caller could provide a
	# customized filter and pass the name through the first argument, to
	# filter out intentional WARNINGs or Oopses
	local filter=${1:-_check_dmesg_filter}

	_dmesg_since_test_start | $filter >$seqres.dmesg
	grep -E -q -e "kernel BUG at" \
	     -e "WARNING:" \
	     -e "\bBUG:" \
	     -e "Oops:" \
	     -e "possible recursive locking detected" \
	     -e "Internal error" \
	     -e "(INFO|ERR): suspicious RCU usage" \
	     -e "INFO: possible circular locking dependency detected" \
	     -e "general protection fault:" \
	     -e "BUG .* remaining" \
	     -e "UBSAN:" \
	     $seqres.dmesg
	if [ $? -eq 0 ]; then
		_dump_err "_check_dmesg: something found in dmesg (see $seqres.dmesg)"
		return 1
	else
		if [ "$KEEP_DMESG" != "yes" ]; then
			rm -f $seqres.dmesg
		fi
		return 0
	fi
}

# Log the arguments to the kernel log with userspace annotation, if possible.
# Output is not sent to stdout.
_kernlog()
{
	if [ -w /dev/ttyprintk ]; then
		echo "$*" >> /dev/ttyprintk
		return
	fi

	if [ -w /dev/kmsg ]; then
		echo "[U] $*" >> /dev/kmsg
		return
	fi
}

# Convey stdin to the kernel log with userspace annotation, if possible.
# Output will be appended to any file paths provided as arguments.
_tee_kernlog()
{
	if [ -w /dev/ttyprintk ]; then
		tee -a /dev/ttyprintk "$@"
		return
	fi

	if [ -w /dev/kmsg ]; then
		awk '{printf("[U] %s\n", $0) >> "/dev/kmsg"; printf("%s\n", $0);}' | tee -a "$@"
		return
	fi

	tee -a "$@"
}

# Make whatever configuration changes we need ahead of testing fs shutdowns due
# to unexpected IO errors while updating metadata.  The sole parameter should
# be the fs device, e.g.  $SCRATCH_DEV.
_prepare_for_eio_shutdown()
{
	local dev="$1"

	case "$FSTYP" in
	"xfs")
		_xfs_prepare_for_eio_shutdown "$dev"
		;;
	esac
}

# capture the kmemleak report
_capture_kmemleak()
{
	local kern_knob="$DEBUGFS_MNT/kmemleak"
	local leak_file="$1"

	# Some callers pass in /dev/null when they want to clear the
	# kernel's leak report file and do not care what was in that.
	[ -f "$leak_file" ] && rm -f "$leak_file"

	# Tell the kernel to scan for memory leaks.  Apparently the write
	# returns before the scan is complete, so do it twice in the hopes
	# that twice is enough to capture all the leaks.
	echo "scan" > "$kern_knob"
	cat "$kern_knob" > /dev/null
	echo "scan" > "$kern_knob"
	cat "$kern_knob" > "$leak_file.tmp"
	if [ -s "$leak_file.tmp" ]; then
		cat > "$leak_file" << ENDL
EXPERIMENTAL kmemleak reported some memory leaks!  Due to the way kmemleak
works, the leak might be from an earlier test, or something totally unrelated.
ENDL
		cat "$leak_file.tmp" >> "$leak_file"
	fi
	rm -rf "$leak_file.tmp"
	echo "clear" > "$kern_knob"
}

# Figure out if the running kernel supports kmemleak; if it does, clear out
# anything that leaked before we even started testing.  The leak checker only
# needs to be primed like this once per ./check invocation.
_detect_kmemleak()
{
	local kern_knob="$DEBUGFS_MNT/kmemleak"
	KMEMLEAK_CHECK_FILE="/tmp/check_kmemleak"

	# Since kernel v4.19-rc3, the kmemleak knob exists even if kmemleak is
	# disabled, but returns EBUSY on write. So instead of relying on
	# existance of writable knob file, we use a test file to indicate that
	# _check_kmemleak() is enabled only if we actually managed to write to
	# the knob file.
	rm -f "$KMEMLEAK_CHECK_FILE"

	if [ ! -w "$kern_knob" ]; then
		return 0
	fi

	# Disable the automatic scan so that we can control it completely,
	# then dump all the leaks recorded so far.
	if echo "scan=off" > "$kern_knob" 2>/dev/null; then
		_capture_kmemleak /dev/null
		touch "$KMEMLEAK_CHECK_FILE"
	fi
}

# Kick the kmemleak checker to scan for leaks.  Background leak scan mode is
# not enabled, so we must call the kernel to ask for a scan and deal with the
# results appropriately.  This we do after every test completes, whether or not
# it was successful.
_check_kmemleak()
{
	local kern_knob="$DEBUGFS_MNT/kmemleak"
	local leak_file="$seqres.kmemleak"

	if [ ! -f "$KMEMLEAK_CHECK_FILE" ]; then
		return 0
	fi

	# Not enabled, so discard any report of leaks found.
	if [ "$USE_KMEMLEAK" != "yes" ]; then
		_capture_kmemleak /dev/null
		return 0
	fi

	# Capture and report any leaks
	_capture_kmemleak "$leak_file"
	if [ -s "$leak_file" ]; then
		_dump_err "_check_kmemleak: something found in kmemleak (see $leak_file)"
		return 1
	else
		rm -f "$leak_file"
		return 0
	fi
}

# don't check dmesg log after test
_disable_dmesg_check()
{
	rm -f ${RESULT_DIR}/check_dmesg
}

init_rc()
{
	# make some further configuration checks here
	if [ "$TEST_DEV" = ""  ]
	then
		echo "common/rc: Error: \$TEST_DEV is not set"
		exit 1
	fi

	# if $TEST_DEV is not mounted, mount it now as XFS
	if [ -z "`_fs_type $TEST_DEV`" ]
	then
		# $TEST_DEV is not mounted
		if ! _test_mount
		then
			echo "common/rc: retrying test device mount with external set"
			[ "$USE_EXTERNAL" != "yes" ] && export USE_EXTERNAL=yes
			if ! _test_mount
			then
				echo "common/rc: could not mount $TEST_DEV on $TEST_DIR"
				exit 1
			fi
		fi
	fi

	# Sanity check that TEST partition is not mounted at another mount point
	# or as another fs type
	_check_mounted_on TEST_DEV $TEST_DEV TEST_DIR $TEST_DIR $FSTYP || exit 1
	if [ -n "$SCRATCH_DEV" ]; then
		# Sanity check that SCRATCH partition is not mounted at another
		# mount point, because it is about to be unmounted and formatted.
		# Another fs type for scratch is fine (bye bye old fs type).
		_check_mounted_on SCRATCH_DEV $SCRATCH_DEV SCRATCH_MNT $SCRATCH_MNT
		[ $? -le 1 ] || exit 1
	fi

	# Figure out if we need to add -F ("foreign", deprecated) option to xfs_io
	$XFS_IO_PROG -c stat $TEST_DIR 2>&1 | grep -q "is not on an XFS filesystem" && \
		export XFS_IO_PROG="$XFS_IO_PROG -F"

	# xfs_io -i option starts an idle thread for xfs_io.
	# With single threaded process, the file table is not shared
	# and file structs are not reference counted.
	# Spawning an idle thread can help detecting file struct
	# reference leaks, so we want to enable the option whenever
	# it is supported.
	$XFS_IO_PROG -i -c quit 2>/dev/null && \
		export XFS_IO_PROG="$XFS_IO_PROG -i"
}

# get real device path name by following link
_real_dev()
{
	local dev=$1
	if [ -b "$dev" ] && [ -L "$dev" ]; then
		dev=`readlink -f "$dev"`
	fi
	echo $dev
}

# basename of a device
_short_dev()
{
	echo `basename $(_real_dev $1)`
}

_sysfs_dev()
{
	local dev=`_real_dev $1`
	local maj=$(stat -c%t $dev | tr [:lower:] [:upper:])
	local min=$(stat -c%T $dev | tr [:lower:] [:upper:])
	maj=$(echo "ibase=16; $maj" | bc)
	min=$(echo "ibase=16; $min" | bc)
	echo /sys/dev/block/$maj:$min
}

# Get the minimum block size of a file.  Usually this is the
# minimum fs block size, but some filesystems (ocfs2) do block
# mappings in larger units.
_get_file_block_size()
{
	if [ -z $1 ] || [ ! -d $1 ]; then
		echo "Missing mount point argument for _get_file_block_size"
		exit 1
	fi

	case "$FSTYP" in
	"ocfs2")
		stat -c '%o' $1
		;;
	"xfs")
		_xfs_get_file_block_size $1
		;;
	*)
		_get_block_size $1
		;;
	esac
}

# Given a file path and a byte length of a file operation under test, ensure
# that the length is an integer multiple of the file's allocation unit size.
# In other words, skip the test unless (oplen ≡ alloc_unit mod 0).  This is
# intended for file remapping operations.
_require_congruent_file_oplen()
{
	local file="$1"
	local alloc_unit=$(_get_file_block_size "$file")
	local oplen="$2"

	case $FSTYP in
	nfs*|afs|cifs|9p|virtiofs|ceph|glusterfs|overlay|pvfs2)
		# Network filesystems don't know about (or tell the client
		# about) the underlying file allocation unit and they generally
		# pass the file IO request to the underlying filesystem, so we
		# don't have anything to check here.
		return
		;;
	esac

	test $alloc_unit -gt $oplen && \
		_notrun "$1: file alloc unit $alloc_unit larger than op length $oplen"
	test $((oplen % alloc_unit)) -eq 0 || \
		_notrun "$1: file alloc unit $alloc_unit not congruent with op length $oplen"
}

# Get the minimum block size of an fs.
_get_block_size()
{
	if [ -z $1 ] || [ ! -d $1 ]; then
		echo "Missing mount point argument for _get_block_size"
		exit 1
	fi
	stat -f -c %S $1
}

# Require that the fundamental allocation unit of a file is the same as the
# filesystem block size.  The sole parameter must be the root dir of a
# filesystem.
_require_file_block_size_equals_fs_block_size()
{
	local file_alloc_unit="$(_get_file_block_size $1)"
	local fs_block_size="$(_get_block_size $1)"
	test "$file_alloc_unit" != "$fs_block_size" && \
		_notrun "File allocation unit is larger than a filesystem block"
}

_get_page_size()
{
	echo $(getconf PAGE_SIZE)
}


run_fsx()
{
	echo fsx $@
	local args=`echo $@ | sed -e "s/ BSIZE / $bsize /g" -e "s/ PSIZE / $psize /g"`
	set -- $here/ltp/fsx $args $FSX_AVOID $TEST_DIR/junk
	echo "$@" >>$seqres.full
	rm -f $TEST_DIR/junk
	"$@" 2>&1 | tee -a $seqres.full >$tmp.fsx
	if [ ${PIPESTATUS[0]} -ne 0 ]; then
		cat $tmp.fsx
		rm -f $tmp.fsx
		exit 1
	fi
	rm -f $tmp.fsx
}

_require_statx()
{
	$here/src/stat_test --check-statx ||
	_notrun "This test requires the statx system call"
}

# Get the path to the sysfs directory for the fs on a device
#
# Only one argument is needed:
#  - dev: mounted block device for the fs
#
# Usage example:
#   _fs_sysfs_dname /dev/mapper/scratch-dev
_fs_sysfs_dname()
{
	local dev=$1

	if [ ! -b "$dev" ]; then
		_fail "Usage: _fs_sysfs_dname <mounted_device>"
	fi

	case "$FSTYP" in
	btrfs)
		_btrfs_get_fsid $dev ;;
	*)
		_short_dev $dev ;;
	esac
}

# Write "content" into /sys/fs/$FSTYP/$DEV/$ATTR
#
# All arguments are necessary, and in this order:
#  - dev: device name, e.g. $SCRATCH_DEV
#  - attr: path name under /sys/fs/$FSTYP/$dev
#  - content: the content of $attr
#
# Usage example:
#   _set_fs_sysfs_attr /dev/mapper/scratch-dev error/fail_at_unmount 0
_set_fs_sysfs_attr()
{
	local dev=$1
	shift
	local attr=$1
	shift
	local content="$*"

	if [ ! -b "$dev" -o -z "$attr" -o -z "$content" ];then
		_fail "Usage: _set_fs_sysfs_attr <mounted_device> <attr> <content>"
	fi

	local dname=$(_fs_sysfs_dname $dev)

	echo "$content" > /sys/fs/${FSTYP}/${dname}/${attr}
}

# Print the content of /sys/fs/$FSTYP/$DEV/$ATTR
#
# All arguments are necessary, and in this order:
#  - dev: device name, e.g. $SCRATCH_DEV
#  - attr: path name under /sys/fs/$FSTYP/$dev
#
# Usage example:
#   _get_fs_sysfs_attr /dev/mapper/scratch-dev error/fail_at_unmount
_get_fs_sysfs_attr()
{
	local dev=$1
	local attr=$2

	if [ ! -b "$dev" -o -z "$attr" ];then
		_fail "Usage: _get_fs_sysfs_attr <mounted_device> <attr>"
	fi

	local dname=$(_fs_sysfs_dname $dev)

	cat /sys/fs/${FSTYP}/${dname}/${attr}
}

# Test for the existence of a sysfs entry at /sys/fs/$FSTYP/$DEV/$ATTR
#
# All arguments are necessary, and in this order:
#  - dev: device name, e.g. $SCRATCH_DEV
#  - attr: path name under /sys/fs/$FSTYP/$dev
#
# Usage example:
#   _has_fs_sysfs_attr /dev/mapper/scratch-dev error/fail_at_unmount
_has_fs_sysfs_attr()
{
	local dev=$1
	local attr=$2

	if [ ! -b "$dev" -o -z "$attr" ];then
		_fail "Usage: _has_fs_sysfs_attr <mounted_device> <attr>"
	fi

	local dname=$(_fs_sysfs_dname $dev)

	test -e /sys/fs/${FSTYP}/${dname}/${attr}
}

# Require the existence of a sysfs entry at /sys/fs/$FSTYP/$DEV/$ATTR
# All arguments are necessary, and in this order:
#  - dev: device name, e.g. $SCRATCH_DEV
#  - attr: path name under /sys/fs/$FSTYP/$dev
#
# Usage example:
#   _require_fs_sysfs_attr /dev/mapper/scratch-dev error/fail_at_unmount
_require_fs_sysfs_attr()
{
	_has_fs_sysfs_attr "$@" && return

	local dev=$1
	local attr=$2
	local dname=$(_fs_sysfs_dname $dev)

	_notrun "This test requires /sys/fs/${FSTYP}/${dname}/${attr}"
}

# Test for the existence of a sysfs entry at /sys/fs/$FSTYP/DEV/$ATTR
#
# Only one argument is needed:
#  - attr: path name under /sys/fs/$FSTYP/DEV
#
# Usage example:
#   _has_fs_sysfs error/fail_at_unmount
_has_fs_sysfs()
{
	_has_fs_sysfs_attr $TEST_DEV "$@"
}

# Require the existence of a sysfs entry at /sys/fs/$FSTYP/DEV/$ATTR
_require_fs_sysfs()
{
	_has_fs_sysfs "$@" && return

	local attr=$1
	local dname=$(_short_dev $TEST_DEV)

	_notrun "This test requires /sys/fs/${FSTYP}/${dname}/${attr}"
}

# Generic test for specific filesystem feature.
# Currently only implemented to test overlayfs features.
_require_scratch_feature()
{
	local feature=$1

	case "$FSTYP" in
	overlay)
		_require_scratch_overlay_features ${feature}
		;;
	*)
		_fail "Test for feature '${feature}' of ${FSTYP} is not implemented"
		;;
	esac
}

# Get the maximum size of a file in $TEST_DIR (s_maxbytes).  On ext4 this will
# be UINT32_MAX * block_size, but other filesystems may allow up to LLONG_MAX.
_get_max_file_size()
{
	if [ -z $1 ] || [ ! -d $1 ]; then
		echo "Missing mount point argument for _get_max_file_size"
		exit 1
	fi

	local mnt=$1
	local testfile=$mnt/maxfilesize.$seq
	local l=0
	local r=9223372036854775807 # LLONG_MAX

	rm -f $testfile
	while (( l < r )); do
		# Use _math() to avoid signed integer overflow.
		local m=$(_math "($l + $r + 1) / 2")
		if $XFS_IO_PROG -f -c "truncate $m" $testfile \
			|& grep -q 'File too large'
		then
			r=$(( m - 1 ))
		else
			l=$m
		fi
	done
	rm -f $testfile
	echo $l
}

# get MAX_LFS_FILESIZE
_get_max_lfs_filesize()
{
	case "$(getconf LONG_BIT)" in
	"32")
		local ulong_max=$(getconf ULONG_MAX)
		local page_size=$(getconf PAGE_SIZE)
		echo $(( ulong_max * page_size ))
		;;
	"64")
		echo 9223372036854775807
		;;
	*)
		_fail "sizeof(long) == $(getconf LONG_BIT)?"
		;;
	esac
}

# The maximum filesystem label length, /not/ including terminating NULL
_label_get_max()
{
	case $FSTYP in
	xfs)
		echo 12
		;;
	btrfs)
		echo 255
		;;
	f2fs)
		echo 255
		;;
	ext2|ext3|ext4)
		echo 16
		;;
	*)
		_notrun "$FSTYP does not define maximum label length"
		;;
	esac
}

# Helper to check above early in a script
_require_label_get_max()
{
	# Just call _label_get_max which will notrun if appropriate
	dummy=$(_label_get_max)
}

_dmsetup_remove()
{
	$UDEV_SETTLE_PROG >/dev/null 2>&1
	$DMSETUP_PROG remove "$@" >>$seqres.full 2>&1
	$DMSETUP_PROG mknodes >/dev/null 2>&1
}

_dmsetup_create()
{
	# Wait for udev to settle so that the dm creation doesn't fail because
	# some udev subprogram opened one of the block devices mentioned in the
	# table string w/ O_EXCL.  Do it again at the end so that an immediate
	# device open won't also fail.
	$UDEV_SETTLE_PROG >/dev/null 2>&1
	$DMSETUP_PROG create "$@" >>$seqres.full 2>&1 || return 1
	$DMSETUP_PROG mknodes >/dev/null 2>&1
	$UDEV_SETTLE_PROG >/dev/null 2>&1
}

_require_btime()
{
	# Note: filesystems are not required to report btime (creation time)
	# if the caller doesn't ask for it, so we define STATX_BTIME here and
	# pass it in to the statx command.
	export STATX_BTIME=0x800
	$XFS_IO_PROG -f $TEST_DIR/test_creation_time -c "statx -m $STATX_BTIME -v" \
		| grep btime >>$seqres.full 2>&1 || \
		_notrun "inode creation time not supported by this filesystem"
	rm -f $TEST_DIR/test_creation_time
}

_require_scratch_btime()
{
	_require_scratch
	_scratch_mkfs > /dev/null 2>&1
	_scratch_mount

	# Note: filesystems are not required to report btime (creation time)
	# if the caller doesn't ask for it, so we define STATX_BTIME here and
	# pass it in to the statx command.
	export STATX_BTIME=0x800
	$XFS_IO_PROG -f $SCRATCH_MNT/test_creation_time -c "statx -m $STATX_BTIME -v" \
		| grep btime >>$seqres.full 2>&1 || \
		_notrun "inode creation time not supported by this filesystem"

	_scratch_unmount
}

_require_inode_limits()
{
	if [ $(_get_free_inode $TEST_DIR) -eq 0 ]; then
		_notrun "$FSTYP does not have a fixed number of inodes available"
	fi
}

_require_filefrag_options()
{
	_require_command "$FILEFRAG_PROG" filefrag

	local options=$1
	local file="$TEST_DIR/options_testfile"

	echo "XX" > $file
	${FILEFRAG_PROG} -$options $file 2>&1 | grep -q "invalid option" && \
		_notrun "filefrag doesn't support $options option"
	rm -f $file
}

_require_fibmap()
{
	_require_filefrag_options "B"

	local file="$TEST_DIR/fibmap_testfile"

	echo "XX" > $file
	${FILEFRAG_PROG} -B $file 2>&1 | grep -q "FIBMAP[[:space:]]*unsupported"
	if [ $? -eq 0 ]; then
		_notrun "FIBMAP not supported by this filesystem"
	fi
	rm -f $file
}

_try_wipe_scratch_devs()
{
	test -x "$WIPEFS_PROG" || return 0

	# Do specified filesystem wipe at first
	case "$FSTYP" in
	"xfs")
		_try_wipe_scratch_xfs
		;;
	esac

	# Then do wipefs on all scratch devices
	for dev in $SCRATCH_DEV_POOL $SCRATCH_DEV $SCRATCH_LOGDEV $SCRATCH_RTDEV; do
		test -b $dev && $WIPEFS_PROG -a $dev
	done
}

# Only run this on xfs if xfs_scrub is available and has the unicode checker
_check_xfs_scrub_does_unicode() {
	[ "${FSTYP}" == "xfs" ] || return 1

	local mount="$1"
	local dev="$2"

	_supports_xfs_scrub "${mount}" "${dev}" || return 1

	# Newer versions of xfs_scrub advertise whether or not it supports
	# Unicode name checks.
	local xfs_scrub_ver="$("${XFS_SCRUB_PROG}" -VV)"

	if echo "${xfs_scrub_ver}" | grep -q -- '-Unicode'; then
		return 1
	fi

	if echo "${xfs_scrub_ver}" | grep -q -- '+Unicode'; then
		return 0
	fi

	# If the xfs_scrub binary contains the string "Unicode name.*%s", then
	# we know that it has the ability to complain about improper Unicode
	# names.
	if strings "${XFS_SCRUB_PROG}" | grep -q 'Unicode name.*%s'; then
		return 0
	fi

	# If the xfs_scrub binary is linked against the libicui18n Unicode
	# library, then we surmise that it contains the Unicode name checker.
	if type ldd > /dev/null 2>&1 && \
	   ldd "${XFS_SCRUB_PROG}" 2> /dev/null | grep -q libicui18n; then
		return 0
	fi

	# We could not establish that xfs_scrub supports unicode names.
	return 1
}

# exfat timestamps start at 1980 and cannot be prior to epoch
_require_negative_timestamps() {
	case "$FSTYP" in
	ceph|exfat)
		_notrun "$FSTYP does not support negative timestamps"
		;;
	esac
}

# Require the 'accton' userspace tool and CONFIG_BSD_PROCESS_ACCT=y.
_require_bsd_process_accounting()
{
	_require_command "$ACCTON_PROG" accton
	$ACCTON_PROG on &> $tmp.test_accton
	cat $tmp.test_accton >> $seqres.full
	if grep 'Function not implemented' $tmp.test_accton; then
		_notrun "BSD process accounting support unavailable"
	fi
	$ACCTON_PROG off >> $seqres.full
}

_require_sysctl_variable()
{
	local name=$1
	sysctl $name &>/dev/null || _notrun "$name sysctl unavailable"
}

_require_mknod()
{
	mknod $TEST_DIR/$seq.null c 1 3 \
		|| _notrun "$FSTYP does not support mknod/mkfifo"
	rm -f $TEST_DIR/$seq.null
}

_getcap()
{
	$GETCAP_PROG "$@" | _filter_getcap
	return ${PIPESTATUS[0]}
}

_require_od_endian_flag()
{
	od --endian=little < /dev/null > /dev/null 2>&1 || \
		_notrun "od does not support endian flag"
}

# Skip this test unless the filesystem treats names (directory entries,
# fs labels, and extended attribute names) as raw byte sequences.
_require_names_are_bytes() {
        case "$FSTYP" in
        ext2|ext3|ext4|f2fs|xfs|btrfs)
		# do nothing
	        ;;
	*)
                _notrun "$FSTYP does not allow unrestricted byte streams for names"
		;;
        esac
}

_has_kernel_config()
{
	local option=$1
	local uname=$(uname -r)
	local config_list="$KCONFIG_PATH
		     /proc/config.gz
		     /lib/modules/$uname/build/.config
		     /boot/config-$uname
		     /lib/kernel/config-$uname"

	for config in $config_list; do
		[ ! -f $config ] && continue
		[ $config = "/proc/config.gz" ] && break
		grep -qE "^${option}=[my]" $config
		return
	done

	[ ! -f $config ] && _notrun "Could not locate kernel config file"

	# We can only get here with /proc/config.gz
	_require_command "$GZIP_PROG" gzip
	$GZIP_PROG -cd $config | grep -qE "^${option}=[my]"
}

_require_kernel_config()
{
	_has_kernel_config $1 || _notrun "Installed kernel not built with $1"
}

_hexdump()
{
	# Hex format address and data output
	od -Ax -t x1z $*
}

# Disable hexdump, turn to use "od" command in _hexdump
hexdump()
{
	_fail "Use _hexdump(), please!"
}

# Try to create a file with inode->i_blocks >= (length / blocksize).
# There may be some small overhead, e.g. ext2 filesystem allocates a
# substantial number of blocks to store block mappings. Those are accounted
# to i_blocks.
_create_file_sized()
{
	local length=$1
	local file=$2
	local tmp=`mktemp -u`
	local ret=0

	$XFS_IO_PROG -ft -c "falloc 0 $length" $file >$tmp.out 2>&1
	ret=$?
	if (grep -Eq "Operation not supported|command .* not found" $tmp.out);then
		# fallocate isn't supported, fallback to general buffer write
		$XFS_IO_PROG -ft -c "pwrite 0 $length" $file >$tmp.out 2>&1
		ret=$?
	fi
	[ $ret -ne 0 ] && cat $tmp.out
	rm -f $tmp.out
	return $ret
}

# Save an arbitrary coredump to the report directory.
_save_coredump()
{
	local path="$1"

	if [ -z "$seqres" ]; then
		echo "$path: seqres is not defined; ignoring coredump!"
		return 1
	fi

	local core_hash="$(_md5_checksum "$path")"
	local out_file="${seqres}.core.${core_hash}"

	for dump in "$out_file"*; do
		if [ -s "$dump" ]; then
			rm -f "$path"
			return 0
		fi
	done

	mv "$path" "$out_file"
	test -z "$COREDUMP_COMPRESSOR" && return 0

	$COREDUMP_COMPRESSOR -f "$out_file"
}

_require_sgid_inheritance()
{
	case $FSTYP in
	afs)
		_notrun "SGID-based group ID inheritance is not supported on $FSTYP"
		;;
	esac
}

_require_use_local_uidgid()
{
	case $FSTYP in
	afs)
		_notrun "$FSTYP doesn't honour local uid and gid"
		;;
	esac
}

_require_unix_perm_checking()
{
	case $FSTYP in
	afs)
		_notrun "$FSTYP doesn't perform traditional UNIX perm checking"
		;;
	esac
}

# Decide if a soak test should continue looping.  The sole parameter is the
# number of soak loops that the test wants to run by default.  The actual
# loop iteration number is stored in SOAK_LOOPIDX until the loop completes.
#
# If the test runner set a SOAK_DURATION value, this predicate will keep
# looping until it has run for at least that long.
_soak_loop_running() {
	local max_soak_loops="$1"

	test -z "$SOAK_LOOPIDX" && SOAK_LOOPIDX=1

	if [ -n "$SOAK_DURATION" ]; then
		if [ -z "$SOAK_DEADLINE" ]; then
			SOAK_DEADLINE="$(( $(date +%s) + SOAK_DURATION))"
		fi

		local now="$(date +%s)"
		if [ "$now" -gt "$SOAK_DEADLINE" ]; then
			unset SOAK_DEADLINE
			unset SOAK_LOOPIDX
			return 1
		fi
		SOAK_LOOPIDX=$((SOAK_LOOPIDX + 1))
		return 0
	fi

	if [ "$SOAK_LOOPIDX" -gt "$max_soak_loops" ]; then
		unset SOAK_LOOPIDX
		return 1
	fi
	SOAK_LOOPIDX=$((SOAK_LOOPIDX + 1))
	return 0
}

_require_unshare() {
	unshare -f -r -m -p -U $@ true &>/dev/null || \
		_notrun "unshare $*: command not found, should be in util-linux"
}

# Return a random file in a directory. A directory is *not* followed
# recursively.
_random_file() {
	local basedir=$1
	echo "$basedir/$(ls -U $basedir | shuf -n 1)"
}

init_rc

################################################################################
# make sure this script returns success
/bin/true