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
|
/* Block Device Driver Test driver, by D.C. van Moolenbroek */
#include <stdlib.h>
#include <stdarg.h>
#include <minix/blockdriver.h>
#include <minix/drvlib.h>
#include <minix/ds.h>
#include <minix/optset.h>
#include <sys/ioc_disk.h>
#include <sys/mman.h>
#include <assert.h>
enum {
RESULT_OK, /* exactly as expected */
RESULT_DEATH, /* driver died */
RESULT_COMMFAIL, /* communication failed */
RESULT_BADTYPE, /* bad type in message */
RESULT_BADID, /* bad request ID in message */
RESULT_BADSTATUS, /* bad/unexpected status in message */
RESULT_TRUNC, /* request truncated unexpectedly */
RESULT_CORRUPT, /* buffer touched erroneously */
RESULT_MISSING, /* buffer left untouched erroneously */
RESULT_OVERFLOW, /* area around buffer touched */
RESULT_BADVALUE /* bad/unexpected return value */
};
typedef struct {
int type;
ssize_t value;
} result_t;
static char driver_label[32] = ""; /* driver DS label */
static devminor_t driver_minor = -1; /* driver's partition minor to use */
static endpoint_t driver_endpt; /* driver endpoint */
static int may_write = FALSE; /* may we write to the device? */
static int sector_size = 512; /* size of a single disk sector */
static int min_read = 512; /* minimum total size of read req */
static int min_write = 0; /* minimum total size of write req */
static int element_size = 512; /* minimum I/O vector element size */
static int max_size = 131072; /* maximum total size of any req */
/* Note that we do not test exceeding the max_size limit, so it is safe to set
* it to a value lower than the driver supports.
*/
/* These settings are used for automated test runs. */
static int contig = TRUE; /* allocate contiguous DMA memory? */
static int silent = FALSE; /* do not produce console output? */
static struct part_geom part; /* base and size of target partition */
#define NR_OPENED 10 /* maximum number of opened devices */
static dev_t opened[NR_OPENED]; /* list of currently opened devices */
static int nr_opened = 0; /* current number of opened devices */
static int total_tests = 0; /* total number of tests performed */
static int failed_tests = 0; /* number of tests that failed */
static int failed_groups = 0; /* nr of groups that had failures */
static int group_failure; /* has this group had a failure yet? */
static int driver_deaths = 0; /* number of restarts that we saw */
/* Options supported by this driver. */
static struct optset optset_table[] = {
{ "label", OPT_STRING, driver_label, sizeof(driver_label) },
{ "minor", OPT_INT, &driver_minor, 10 },
{ "rw", OPT_BOOL, &may_write, TRUE },
{ "ro", OPT_BOOL, &may_write, FALSE },
{ "sector", OPT_INT, §or_size, 10 },
{ "element", OPT_INT, &element_size, 10 },
{ "min_read", OPT_INT, &min_read, 10 },
{ "min_write", OPT_INT, &min_write, 10 },
{ "max", OPT_INT, &max_size, 10 },
{ "nocontig", OPT_BOOL, &contig, FALSE },
{ "silent", OPT_BOOL, &silent, TRUE },
{ NULL, 0, NULL, 0 }
};
static void output(char *fmt, ...)
{
/* Print debugging information, unless configured to be silent.
*/
va_list argp;
if (silent)
return;
va_start(argp, fmt);
vprintf(fmt, argp);
va_end(argp);
}
static void *alloc_dma_memory(size_t size)
{
/* Allocate memory that may be used for direct DMA. For most drivers,
* this means that the memory has to be physically contiguous. For some
* drivers (e.g. VND) we allow non-contiguous allocation, because VM is
* currently flaky and does not always manage to provide contiguous
* memory even when it should, thus causing needless test failures.
*/
void *ptr;
if (contig)
ptr = alloc_contig(size, 0, NULL);
else
ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_PREALLOC | MAP_ANON, -1, 0);
if (ptr == MAP_FAILED)
panic("unable to allocate %zu bytes of memory", size);
return ptr;
}
static void free_dma_memory(void *ptr, size_t size)
{
/* Free memory previously allocated for direct DMA. */
if (contig)
free_contig(ptr, size);
else
munmap(ptr, size);
}
static int set_result(result_t *res, int type, ssize_t value)
{
/* Set the result to the given result type and with the given optional
* extra value. Return the type.
*/
res->type = type;
res->value = value;
return type;
}
static int accept_result(result_t *res, int type, ssize_t value)
{
/* If the result is of the given type and value, reset it to a success
* result. This allows for a logical OR on error codes. Return whether
* the result was indeed reset.
*/
if (res->type == type && res->value == value) {
set_result(res, RESULT_OK, 0);
return TRUE;
}
return FALSE;
}
static void got_result(result_t *res, char *desc)
{
/* Process the result of a test. Keep statistics.
*/
static int i = 0;
total_tests++;
if (res->type != RESULT_OK) {
failed_tests++;
if (group_failure == FALSE) {
failed_groups++;
group_failure = TRUE;
}
}
output("#%02d: %-38s\t[%s]\n", ++i, desc,
(res->type == RESULT_OK) ? "PASS" : "FAIL");
switch (res->type) {
case RESULT_DEATH:
output("- driver died\n");
break;
case RESULT_COMMFAIL:
output("- communication failed; ipc_sendrec returned %d\n",
res->value);
break;
case RESULT_BADTYPE:
output("- bad type %d in reply message\n", res->value);
break;
case RESULT_BADID:
output("- mismatched ID %d in reply message\n", res->value);
break;
case RESULT_BADSTATUS:
output("- bad or unexpected status %d in reply message\n",
res->value);
break;
case RESULT_TRUNC:
output("- result size not as expected (%u bytes left)\n",
res->value);
break;
case RESULT_CORRUPT:
output("- buffer has been modified erroneously\n");
break;
case RESULT_MISSING:
output("- buffer has been left untouched erroneously\n");
break;
case RESULT_OVERFLOW:
output("- area around target buffer modified\n");
break;
case RESULT_BADVALUE:
output("- bad or unexpected return value %d from call\n",
res->value);
break;
}
}
static void test_group(char *name, int exec)
{
/* Start a new group of tests.
*/
output("Test group: %s%s\n", name, exec ? "" : " (skipping)");
group_failure = FALSE;
}
static void reopen_device(dev_t minor)
{
/* Reopen a device after we were notified that the driver has died.
* Explicitly ignore any errors here; this is a feeble attempt to get
* ourselves back into business again.
*/
message m;
memset(&m, 0, sizeof(m));
m.m_type = BDEV_OPEN;
m.m_lbdev_lblockdriver_msg.minor = minor;
m.m_lbdev_lblockdriver_msg.access = (may_write) ? (BDEV_R_BIT | BDEV_W_BIT) : BDEV_R_BIT;
m.m_lbdev_lblockdriver_msg.id = 0;
(void) ipc_sendrec(driver_endpt, &m);
}
static int sendrec_driver(message *m_ptr, ssize_t exp, result_t *res)
{
/* Make a call to the driver, and perform basic checks on the return
* message. Fill in the result structure, wiping out what was in there
* before. If the driver dies in the process, attempt to recover but
* fail the request.
*/
message m_orig;
endpoint_t last_endpt;
int i, r;
m_orig = *m_ptr;
r = ipc_sendrec(driver_endpt, m_ptr);
if (r == EDEADSRCDST) {
/* The driver has died. Find its new endpoint, and reopen all
* devices that we opened earlier. Then return failure.
*/
output("WARNING: driver has died, attempting to proceed\n");
driver_deaths++;
/* Keep trying until we get a new endpoint. */
last_endpt = driver_endpt;
for (;;) {
r = ds_retrieve_label_endpt(driver_label,
&driver_endpt);
if (r == OK && last_endpt != driver_endpt)
break;
micro_delay(100000);
}
for (i = 0; i < nr_opened; i++)
reopen_device(opened[i]);
return set_result(res, RESULT_DEATH, 0);
}
if (r != OK)
return set_result(res, RESULT_COMMFAIL, r);
if (m_ptr->m_type != BDEV_REPLY)
return set_result(res, RESULT_BADTYPE, m_ptr->m_type);
if (m_ptr->m_lblockdriver_lbdev_reply.id != m_orig.m_lbdev_lblockdriver_msg.id)
return set_result(res, RESULT_BADID,
m_ptr->m_lblockdriver_lbdev_reply.id);
if ((exp < 0 && m_ptr->m_lblockdriver_lbdev_reply.status >= 0) ||
(exp >= 0 &&
m_ptr->m_lblockdriver_lbdev_reply.status < 0))
return set_result(res, RESULT_BADSTATUS,
m_ptr->m_lblockdriver_lbdev_reply.status);
return set_result(res, RESULT_OK, 0);
}
static void raw_xfer(dev_t minor, u64_t pos, iovec_s_t *iovec, int nr_req,
int write, ssize_t exp, result_t *res)
{
/* Perform a transfer with a safecopy iovec already supplied.
*/
cp_grant_id_t grant;
message m;
int r;
assert(nr_req <= NR_IOREQS);
assert(!write || may_write);
if ((grant = cpf_grant_direct(driver_endpt, (vir_bytes) iovec,
sizeof(*iovec) * nr_req, CPF_READ)) == GRANT_INVALID)
panic("unable to allocate grant");
memset(&m, 0, sizeof(m));
m.m_type = write ? BDEV_SCATTER : BDEV_GATHER;
m.m_lbdev_lblockdriver_msg.minor = minor;
m.m_lbdev_lblockdriver_msg.pos = pos;
m.m_lbdev_lblockdriver_msg.count = nr_req;
m.m_lbdev_lblockdriver_msg.grant = grant;
m.m_lbdev_lblockdriver_msg.id = lrand48();
r = sendrec_driver(&m, exp, res);
if (cpf_revoke(grant) == -1)
panic("unable to revoke grant");
if (r != RESULT_OK)
return;
if (m.m_lblockdriver_lbdev_reply.status == exp)
return;
if (exp < 0)
set_result(res, RESULT_BADSTATUS,
m.m_lblockdriver_lbdev_reply.status);
else
set_result(res, RESULT_TRUNC,
exp - m.m_lblockdriver_lbdev_reply.status);
}
static void vir_xfer(dev_t minor, u64_t pos, iovec_t *iovec, int nr_req,
int write, ssize_t exp, result_t *res)
{
/* Perform a transfer, creating and revoking grants for the I/O vector.
*/
iovec_s_t iov_s[NR_IOREQS];
int i;
assert(nr_req <= NR_IOREQS);
for (i = 0; i < nr_req; i++) {
iov_s[i].iov_size = iovec[i].iov_size;
if ((iov_s[i].iov_grant = cpf_grant_direct(driver_endpt,
(vir_bytes) iovec[i].iov_addr, iovec[i].iov_size,
write ? CPF_READ : CPF_WRITE)) == GRANT_INVALID)
panic("unable to allocate grant");
}
raw_xfer(minor, pos, iov_s, nr_req, write, exp, res);
for (i = 0; i < nr_req; i++) {
iovec[i].iov_size = iov_s[i].iov_size;
if (cpf_revoke(iov_s[i].iov_grant) == -1)
panic("unable to revoke grant");
}
}
static void simple_xfer(dev_t minor, u64_t pos, u8_t *buf, size_t size,
int write, ssize_t exp, result_t *res)
{
/* Perform a transfer involving a single buffer.
*/
iovec_t iov;
iov.iov_addr = (vir_bytes) buf;
iov.iov_size = size;
vir_xfer(minor, pos, &iov, 1, write, exp, res);
}
static void alloc_buf_and_grant(u8_t **ptr, cp_grant_id_t *grant,
size_t size, int perms)
{
/* Allocate a buffer suitable for DMA (i.e. contiguous) and create a
* grant for it with the requested CPF_* grant permissions.
*/
*ptr = alloc_dma_memory(size);
if ((*grant = cpf_grant_direct(driver_endpt, (vir_bytes) *ptr, size,
perms)) == GRANT_INVALID)
panic("unable to allocate grant");
}
static void free_buf_and_grant(u8_t *ptr, cp_grant_id_t grant, size_t size)
{
/* Revoke a grant and free a buffer.
*/
cpf_revoke(grant);
free_dma_memory(ptr, size);
}
static void bad_read1(void)
{
/* Test various illegal read transfer requests, part 1.
*/
message mt, m;
iovec_s_t iovt, iov;
cp_grant_id_t grant, grant2, grant3;
u8_t *buf_ptr;
vir_bytes buf_size;
result_t res;
test_group("bad read requests, part one", TRUE);
#define BUF_SIZE 4096
buf_size = BUF_SIZE;
alloc_buf_and_grant(&buf_ptr, &grant2, buf_size, CPF_WRITE);
if ((grant = cpf_grant_direct(driver_endpt, (vir_bytes) &iov,
sizeof(iov), CPF_READ)) == GRANT_INVALID)
panic("unable to allocate grant");
/* Initialize the defaults for some of the tests.
* This is a legitimate request for the first block of the partition.
*/
memset(&mt, 0, sizeof(mt));
mt.m_type = BDEV_GATHER;
mt.m_lbdev_lblockdriver_msg.minor = driver_minor;
mt.m_lbdev_lblockdriver_msg.pos = 0LL;
mt.m_lbdev_lblockdriver_msg.count = 1;
mt.m_lbdev_lblockdriver_msg.grant = grant;
mt.m_lbdev_lblockdriver_msg.id = lrand48();
memset(&iovt, 0, sizeof(iovt));
iovt.iov_grant = grant2;
iovt.iov_size = buf_size;
/* Test normal request. */
m = mt;
iov = iovt;
sendrec_driver(&m, OK, &res);
if (res.type == RESULT_OK &&
m.m_lblockdriver_lbdev_reply.status != (ssize_t) iov.iov_size) {
res.type = RESULT_TRUNC;
res.value = m.m_lblockdriver_lbdev_reply.status;
}
got_result(&res, "normal request");
/* Test zero iovec elements. */
m = mt;
iov = iovt;
m.m_lbdev_lblockdriver_msg.count = 0;
sendrec_driver(&m, EINVAL, &res);
got_result(&res, "zero iovec elements");
/* Test bad iovec grant. */
m = mt;
m.m_lbdev_lblockdriver_msg.grant = GRANT_INVALID;
sendrec_driver(&m, EINVAL, &res);
got_result(&res, "bad iovec grant");
/* Test revoked iovec grant. */
m = mt;
iov = iovt;
if ((grant3 = cpf_grant_direct(driver_endpt, (vir_bytes) &iov,
sizeof(iov), CPF_READ)) == GRANT_INVALID)
panic("unable to allocate grant");
cpf_revoke(grant3);
m.m_lbdev_lblockdriver_msg.grant = grant3;
sendrec_driver(&m, EINVAL, &res);
accept_result(&res, RESULT_BADSTATUS, EPERM);
got_result(&res, "revoked iovec grant");
/* Test normal request (final check). */
m = mt;
iov = iovt;
sendrec_driver(&m, OK, &res);
if (res.type == RESULT_OK &&
m.m_lblockdriver_lbdev_reply.status != (ssize_t) iov.iov_size) {
res.type = RESULT_TRUNC;
res.value = m.m_lblockdriver_lbdev_reply.status;
}
got_result(&res, "normal request");
/* Clean up. */
free_buf_and_grant(buf_ptr, grant2, buf_size);
cpf_revoke(grant);
}
static u32_t get_sum(u8_t *ptr, size_t size)
{
/* Compute a checksum over the given buffer.
*/
u32_t sum;
for (sum = 0; size > 0; size--, ptr++)
sum = sum ^ (sum << 5) ^ *ptr;
return sum;
}
static u32_t fill_rand(u8_t *ptr, size_t size)
{
/* Fill the given buffer with random data. Return a checksum over the
* resulting data.
*/
size_t i;
for (i = 0; i < size; i++)
ptr[i] = lrand48() % 256;
return get_sum(ptr, size);
}
static void test_sum(u8_t *ptr, size_t size, u32_t sum, int should_match,
result_t *res)
{
/* If the test succeeded so far, check whether the given buffer does
* or does not match the given checksum, and adjust the test result
* accordingly.
*/
u32_t sum2;
if (res->type != RESULT_OK)
return;
sum2 = get_sum(ptr, size);
if ((sum == sum2) != should_match) {
res->type = should_match ? RESULT_CORRUPT : RESULT_MISSING;
res->value = 0; /* not much that's useful here */
}
}
static void bad_read2(void)
{
/* Test various illegal read transfer requests, part 2.
*
* Consider allowing this test to be run twice, with different buffer
* sizes. It appears that we can make at_wini misbehave by making the
* size exceed the per-operation size (128KB ?). On the other hand, we
* then need to start checking partition sizes, possibly.
*/
u8_t *buf_ptr, *buf2_ptr, *buf3_ptr, c1, c2;
size_t buf_size, buf2_size, buf3_size;
cp_grant_id_t buf_grant, buf2_grant, buf3_grant, grant;
u32_t buf_sum, buf2_sum, buf3_sum;
iovec_s_t iov[3], iovt[3];
result_t res;
test_group("bad read requests, part two", TRUE);
buf_size = buf2_size = buf3_size = BUF_SIZE;
alloc_buf_and_grant(&buf_ptr, &buf_grant, buf_size, CPF_WRITE);
alloc_buf_and_grant(&buf2_ptr, &buf2_grant, buf2_size, CPF_WRITE);
alloc_buf_and_grant(&buf3_ptr, &buf3_grant, buf3_size, CPF_WRITE);
iovt[0].iov_grant = buf_grant;
iovt[0].iov_size = buf_size;
iovt[1].iov_grant = buf2_grant;
iovt[1].iov_size = buf2_size;
iovt[2].iov_grant = buf3_grant;
iovt[2].iov_size = buf3_size;
/* Test normal vector request. */
memcpy(iov, iovt, sizeof(iovt));
buf_sum = fill_rand(buf_ptr, buf_size);
buf2_sum = fill_rand(buf2_ptr, buf2_size);
buf3_sum = fill_rand(buf3_ptr, buf3_size);
raw_xfer(driver_minor, 0ULL, iov, 3, FALSE,
buf_size + buf2_size + buf3_size, &res);
test_sum(buf_ptr, buf_size, buf_sum, FALSE, &res);
test_sum(buf2_ptr, buf2_size, buf2_sum, FALSE, &res);
test_sum(buf3_ptr, buf3_size, buf3_sum, FALSE, &res);
got_result(&res, "normal vector request");
/* Test zero sized iovec element. */
memcpy(iov, iovt, sizeof(iovt));
iov[1].iov_size = 0;
buf_sum = fill_rand(buf_ptr, buf_size);
buf2_sum = fill_rand(buf2_ptr, buf2_size);
buf3_sum = fill_rand(buf3_ptr, buf3_size);
raw_xfer(driver_minor, 0ULL, iov, 3, FALSE, EINVAL, &res);
test_sum(buf_ptr, buf_size, buf_sum, TRUE, &res);
test_sum(buf2_ptr, buf2_size, buf2_sum, TRUE, &res);
test_sum(buf3_ptr, buf3_size, buf3_sum, TRUE, &res);
got_result(&res, "zero size in iovec element");
/* Test negative sized iovec element. */
memcpy(iov, iovt, sizeof(iovt));
iov[1].iov_size = (vir_bytes) LONG_MAX + 1;
raw_xfer(driver_minor, 0ULL, iov, 3, FALSE, EINVAL, &res);
test_sum(buf_ptr, buf_size, buf_sum, TRUE, &res);
test_sum(buf2_ptr, buf2_size, buf2_sum, TRUE, &res);
test_sum(buf3_ptr, buf3_size, buf3_sum, TRUE, &res);
got_result(&res, "negative size in iovec element");
/* Test iovec with negative total size. */
memcpy(iov, iovt, sizeof(iovt));
iov[0].iov_size = LONG_MAX / 2 - 1;
iov[1].iov_size = LONG_MAX / 2 - 1;
raw_xfer(driver_minor, 0ULL, iov, 3, FALSE, EINVAL, &res);
test_sum(buf_ptr, buf_size, buf_sum, TRUE, &res);
test_sum(buf2_ptr, buf2_size, buf2_sum, TRUE, &res);
test_sum(buf3_ptr, buf3_size, buf3_sum, TRUE, &res);
got_result(&res, "negative total size");
/* Test iovec with wrapping total size. */
memcpy(iov, iovt, sizeof(iovt));
iov[0].iov_size = LONG_MAX - 1;
iov[1].iov_size = LONG_MAX - 1;
raw_xfer(driver_minor, 0ULL, iov, 3, FALSE, EINVAL, &res);
test_sum(buf_ptr, buf_size, buf_sum, TRUE, &res);
test_sum(buf2_ptr, buf2_size, buf2_sum, TRUE, &res);
test_sum(buf3_ptr, buf3_size, buf3_sum, TRUE, &res);
got_result(&res, "wrapping total size");
/* Test word-unaligned iovec element size. */
memcpy(iov, iovt, sizeof(iovt));
iov[1].iov_size--;
buf_sum = fill_rand(buf_ptr, buf_size);
buf2_sum = fill_rand(buf2_ptr, buf2_size);
buf3_sum = fill_rand(buf3_ptr, buf3_size);
c1 = buf2_ptr[buf2_size - 1];
raw_xfer(driver_minor, 0ULL, iov, 3, FALSE, BUF_SIZE * 3 - 1,
&res);
if (accept_result(&res, RESULT_BADSTATUS, EINVAL)) {
/* Do not test the first buffer, as it may contain a partial
* result.
*/
test_sum(buf2_ptr, buf2_size, buf2_sum, TRUE, &res);
test_sum(buf3_ptr, buf3_size, buf3_sum, TRUE, &res);
} else {
test_sum(buf_ptr, buf_size, buf_sum, FALSE, &res);
test_sum(buf2_ptr, buf2_size, buf2_sum, FALSE, &res);
test_sum(buf3_ptr, buf3_size, buf3_sum, FALSE, &res);
if (c1 != buf2_ptr[buf2_size - 1])
set_result(&res, RESULT_CORRUPT, 0);
}
got_result(&res, "word-unaligned size in iovec element");
/* Test invalid grant in iovec element. */
memcpy(iov, iovt, sizeof(iovt));
iov[1].iov_grant = GRANT_INVALID;
fill_rand(buf_ptr, buf_size);
buf2_sum = fill_rand(buf2_ptr, buf2_size);
buf3_sum = fill_rand(buf3_ptr, buf3_size);
raw_xfer(driver_minor, 0ULL, iov, 3, FALSE, EINVAL, &res);
/* Do not test the first buffer, as it may contain a partial result. */
test_sum(buf2_ptr, buf2_size, buf2_sum, TRUE, &res);
test_sum(buf3_ptr, buf3_size, buf3_sum, TRUE, &res);
got_result(&res, "invalid grant in iovec element");
/* Test revoked grant in iovec element. */
memcpy(iov, iovt, sizeof(iovt));
if ((grant = cpf_grant_direct(driver_endpt, (vir_bytes) buf2_ptr,
buf2_size, CPF_WRITE)) == GRANT_INVALID)
panic("unable to allocate grant");
cpf_revoke(grant);
iov[1].iov_grant = grant;
buf_sum = fill_rand(buf_ptr, buf_size);
buf2_sum = fill_rand(buf2_ptr, buf2_size);
buf3_sum = fill_rand(buf3_ptr, buf3_size);
raw_xfer(driver_minor, 0ULL, iov, 3, FALSE, EINVAL, &res);
accept_result(&res, RESULT_BADSTATUS, EPERM);
/* Do not test the first buffer, as it may contain a partial result. */
test_sum(buf2_ptr, buf2_size, buf2_sum, TRUE, &res);
test_sum(buf3_ptr, buf3_size, buf3_sum, TRUE, &res);
got_result(&res, "revoked grant in iovec element");
/* Test read-only grant in iovec element. */
memcpy(iov, iovt, sizeof(iovt));
if ((grant = cpf_grant_direct(driver_endpt, (vir_bytes) buf2_ptr,
buf2_size, CPF_READ)) == GRANT_INVALID)
panic("unable to allocate grant");
iov[1].iov_grant = grant;
buf_sum = fill_rand(buf_ptr, buf_size);
buf2_sum = fill_rand(buf2_ptr, buf2_size);
buf3_sum = fill_rand(buf3_ptr, buf3_size);
raw_xfer(driver_minor, 0ULL, iov, 3, FALSE, EINVAL, &res);
accept_result(&res, RESULT_BADSTATUS, EPERM);
/* Do not test the first buffer, as it may contain a partial result. */
test_sum(buf2_ptr, buf2_size, buf2_sum, TRUE, &res);
test_sum(buf3_ptr, buf3_size, buf3_sum, TRUE, &res);
got_result(&res, "read-only grant in iovec element");
cpf_revoke(grant);
/* Test word-unaligned iovec element buffer. */
memcpy(iov, iovt, sizeof(iovt));
if ((grant = cpf_grant_direct(driver_endpt, (vir_bytes) (buf2_ptr + 1),
buf2_size - 2, CPF_WRITE)) == GRANT_INVALID)
panic("unable to allocate grant");
iov[1].iov_grant = grant;
iov[1].iov_size = buf2_size - 2;
buf_sum = fill_rand(buf_ptr, buf_size);
buf2_sum = fill_rand(buf2_ptr, buf2_size);
buf3_sum = fill_rand(buf3_ptr, buf3_size);
c1 = buf2_ptr[0];
c2 = buf2_ptr[buf2_size - 1];
raw_xfer(driver_minor, 0ULL, iov, 3, FALSE, BUF_SIZE * 3 - 2, &res);
if (accept_result(&res, RESULT_BADSTATUS, EINVAL)) {
/* Do not test the first buffer, as it may contain a partial
* result.
*/
test_sum(buf2_ptr, buf2_size, buf2_sum, TRUE, &res);
test_sum(buf3_ptr, buf3_size, buf3_sum, TRUE, &res);
} else {
test_sum(buf_ptr, buf_size, buf_sum, FALSE, &res);
test_sum(buf2_ptr, buf2_size, buf2_sum, FALSE, &res);
test_sum(buf3_ptr, buf3_size, buf3_sum, FALSE, &res);
if (c1 != buf2_ptr[0] || c2 != buf2_ptr[buf2_size - 1])
set_result(&res, RESULT_CORRUPT, 0);
}
got_result(&res, "word-unaligned buffer in iovec element");
cpf_revoke(grant);
/* Test word-unaligned position. */
/* Only perform this test if the minimum read size is not 1, in which
* case it is safe to assume that the driver expects no position
* alignment either. These tests are indeed not exhaustive yet. For now
* we assume that if no alignment is required at all, the driver does
* not implement special logic to achieve this, so we don't need to
* test all possible positions and sizes either (yes, laziness..).
*/
if (min_read > 1) {
memcpy(iov, iovt, sizeof(iovt));
buf_sum = fill_rand(buf_ptr, buf_size);
buf2_sum = fill_rand(buf2_ptr, buf2_size);
buf3_sum = fill_rand(buf3_ptr, buf3_size);
raw_xfer(driver_minor, 1ULL, iov, 3, FALSE, EINVAL, &res);
test_sum(buf_ptr, buf_size, buf_sum, TRUE, &res);
test_sum(buf2_ptr, buf2_size, buf2_sum, TRUE, &res);
test_sum(buf3_ptr, buf3_size, buf3_sum, TRUE, &res);
got_result(&res, "word-unaligned position");
}
/* Test normal vector request (final check). */
memcpy(iov, iovt, sizeof(iovt));
buf_sum = fill_rand(buf_ptr, buf_size);
buf2_sum = fill_rand(buf2_ptr, buf2_size);
buf3_sum = fill_rand(buf3_ptr, buf3_size);
raw_xfer(driver_minor, 0ULL, iov, 3, FALSE,
buf_size + buf2_size + buf3_size, &res);
test_sum(buf_ptr, buf_size, buf_sum, FALSE, &res);
test_sum(buf2_ptr, buf2_size, buf2_sum, FALSE, &res);
test_sum(buf3_ptr, buf3_size, buf3_sum, FALSE, &res);
got_result(&res, "normal vector request");
/* Clean up. */
free_buf_and_grant(buf3_ptr, buf3_grant, buf3_size);
free_buf_and_grant(buf2_ptr, buf2_grant, buf2_size);
free_buf_and_grant(buf_ptr, buf_grant, buf_size);
}
static void bad_write(void)
{
/* Test various illegal write transfer requests, if writing is allowed.
* If handled correctly, these requests will not actually write data.
* This part of the test set is in need of further expansion.
*/
u8_t *buf_ptr, *buf2_ptr, *buf3_ptr;
size_t buf_size, buf2_size, buf3_size, sector_unalign;
cp_grant_id_t buf_grant, buf2_grant, buf3_grant;
cp_grant_id_t grant;
u32_t buf_sum, buf2_sum, buf3_sum;
iovec_s_t iov[3], iovt[3];
result_t res;
test_group("bad write requests", may_write);
if (!may_write)
return;
buf_size = buf2_size = buf3_size = BUF_SIZE;
alloc_buf_and_grant(&buf_ptr, &buf_grant, buf_size, CPF_READ);
alloc_buf_and_grant(&buf2_ptr, &buf2_grant, buf2_size, CPF_READ);
alloc_buf_and_grant(&buf3_ptr, &buf3_grant, buf3_size, CPF_READ);
iovt[0].iov_grant = buf_grant;
iovt[0].iov_size = buf_size;
iovt[1].iov_grant = buf2_grant;
iovt[1].iov_size = buf2_size;
iovt[2].iov_grant = buf3_grant;
iovt[2].iov_size = buf3_size;
/* Only perform write alignment tests if writes require alignment. */
if (min_write == 0)
min_write = sector_size;
if (min_write > 1) {
/* If min_write is larger than 2, use 2 as sector-unaligned
* size, as word-unaligned values (e.g., 1) may be filtered out
* on another code path.
*/
sector_unalign = (min_write > 2) ? 2 : 1;
/* Test sector-unaligned write position. */
memcpy(iov, iovt, sizeof(iovt));
buf_sum = fill_rand(buf_ptr, buf_size);
buf2_sum = fill_rand(buf2_ptr, buf2_size);
buf3_sum = fill_rand(buf3_ptr, buf3_size);
raw_xfer(driver_minor, (u64_t)sector_unalign, iov, 3, TRUE,
EINVAL, &res);
test_sum(buf_ptr, buf_size, buf_sum, TRUE, &res);
test_sum(buf2_ptr, buf2_size, buf2_sum, TRUE, &res);
test_sum(buf3_ptr, buf3_size, buf3_sum, TRUE, &res);
got_result(&res, "sector-unaligned write position");
/* Test sector-unaligned write size. */
memcpy(iov, iovt, sizeof(iovt));
iov[1].iov_size -= sector_unalign;
buf_sum = fill_rand(buf_ptr, buf_size);
buf2_sum = fill_rand(buf2_ptr, buf2_size);
buf3_sum = fill_rand(buf3_ptr, buf3_size);
raw_xfer(driver_minor, 0ULL, iov, 3, TRUE, EINVAL, &res);
test_sum(buf_ptr, buf_size, buf_sum, TRUE, &res);
test_sum(buf2_ptr, buf2_size, buf2_sum, TRUE, &res);
test_sum(buf3_ptr, buf3_size, buf3_sum, TRUE, &res);
got_result(&res, "sector-unaligned write size");
}
/* Test write-only grant in iovec element. */
memcpy(iov, iovt, sizeof(iovt));
if ((grant = cpf_grant_direct(driver_endpt, (vir_bytes) buf2_ptr,
buf2_size, CPF_WRITE)) == GRANT_INVALID)
panic("unable to allocate grant");
iov[1].iov_grant = grant;
buf_sum = fill_rand(buf_ptr, buf_size);
buf2_sum = fill_rand(buf2_ptr, buf2_size);
buf3_sum = fill_rand(buf3_ptr, buf3_size);
raw_xfer(driver_minor, 0ULL, iov, 3, TRUE, EINVAL, &res);
accept_result(&res, RESULT_BADSTATUS, EPERM);
test_sum(buf_ptr, buf_size, buf_sum, TRUE, &res);
test_sum(buf2_ptr, buf2_size, buf2_sum, TRUE, &res);
test_sum(buf3_ptr, buf3_size, buf3_sum, TRUE, &res);
got_result(&res, "write-only grant in iovec element");
cpf_revoke(grant);
/* Clean up. */
free_buf_and_grant(buf3_ptr, buf3_grant, buf3_size);
free_buf_and_grant(buf2_ptr, buf2_grant, buf2_size);
free_buf_and_grant(buf_ptr, buf_grant, buf_size);
}
static void vector_and_large_sub(size_t small_size)
{
/* Check whether large vectored requests, and large single requests,
* succeed.
*/
size_t large_size, buf_size, buf2_size;
u8_t *buf_ptr, *buf2_ptr;
iovec_t iovec[NR_IOREQS];
u64_t base_pos;
result_t res;
int i;
base_pos = (u64_t)sector_size;
large_size = small_size * NR_IOREQS;
buf_size = large_size + sizeof(u32_t) * 2;
buf2_size = large_size + sizeof(u32_t) * (NR_IOREQS + 1);
buf_ptr = alloc_dma_memory(buf_size);
buf2_ptr = alloc_dma_memory(buf2_size);
/* The first buffer has one large chunk with dword-sized guards on each
* side. LPTR(n) points to the start of the nth small data chunk within
* the large chunk. The second buffer contains several small chunks. It
* has dword-sized guards before each chunk and after the last chunk.
* SPTR(n) points to the start of the nth small chunk.
*/
#define SPTR(n) (buf2_ptr + sizeof(u32_t) + (n) * (sizeof(u32_t) + small_size))
#define LPTR(n) (buf_ptr + sizeof(u32_t) + small_size * (n))
/* Write one large chunk, if writing is allowed. */
if (may_write) {
fill_rand(buf_ptr, buf_size); /* don't need the checksum */
iovec[0].iov_addr = (vir_bytes) (buf_ptr + sizeof(u32_t));
iovec[0].iov_size = large_size;
vir_xfer(driver_minor, base_pos, iovec, 1, TRUE, large_size,
&res);
got_result(&res, "large write");
}
/* Read back in many small chunks. If writing is not allowed, do not
* check checksums.
*/
for (i = 0; i < NR_IOREQS; i++) {
* (((u32_t *) SPTR(i)) - 1) = 0xDEADBEEFL + i;
iovec[i].iov_addr = (vir_bytes) SPTR(i);
iovec[i].iov_size = small_size;
}
* (((u32_t *) SPTR(i)) - 1) = 0xFEEDFACEL;
vir_xfer(driver_minor, base_pos, iovec, NR_IOREQS, FALSE, large_size,
&res);
if (res.type == RESULT_OK) {
for (i = 0; i < NR_IOREQS; i++) {
if (* (((u32_t *) SPTR(i)) - 1) != 0xDEADBEEFL + i)
set_result(&res, RESULT_OVERFLOW, 0);
}
if (* (((u32_t *) SPTR(i)) - 1) != 0xFEEDFACEL)
set_result(&res, RESULT_OVERFLOW, 0);
}
if (res.type == RESULT_OK && may_write) {
for (i = 0; i < NR_IOREQS; i++) {
test_sum(SPTR(i), small_size,
get_sum(LPTR(i), small_size), TRUE, &res);
}
}
got_result(&res, "vectored read");
/* Write new data in many small chunks, if writing is allowed. */
if (may_write) {
fill_rand(buf2_ptr, buf2_size); /* don't need the checksum */
for (i = 0; i < NR_IOREQS; i++) {
iovec[i].iov_addr = (vir_bytes) SPTR(i);
iovec[i].iov_size = small_size;
}
vir_xfer(driver_minor, base_pos, iovec, NR_IOREQS, TRUE,
large_size, &res);
got_result(&res, "vectored write");
}
/* Read back in one large chunk. If writing is allowed, the checksums
* must match the last write; otherwise, they must match the last read.
* In both cases, the expected content is in the second buffer.
*/
* (u32_t *) buf_ptr = 0xCAFEBABEL;
* (u32_t *) (buf_ptr + sizeof(u32_t) + large_size) = 0xDECAFBADL;
iovec[0].iov_addr = (vir_bytes) (buf_ptr + sizeof(u32_t));
iovec[0].iov_size = large_size;
vir_xfer(driver_minor, base_pos, iovec, 1, FALSE, large_size, &res);
if (res.type == RESULT_OK) {
if (* (u32_t *) buf_ptr != 0xCAFEBABEL)
set_result(&res, RESULT_OVERFLOW, 0);
if (* (u32_t *) (buf_ptr + sizeof(u32_t) + large_size) !=
0xDECAFBADL)
set_result(&res, RESULT_OVERFLOW, 0);
}
if (res.type == RESULT_OK) {
for (i = 0; i < NR_IOREQS; i++) {
test_sum(SPTR(i), small_size,
get_sum(LPTR(i), small_size), TRUE, &res);
}
}
got_result(&res, "large read");
#undef LPTR
#undef SPTR
/* Clean up. */
free_dma_memory(buf2_ptr, buf2_size);
free_dma_memory(buf_ptr, buf_size);
}
static void vector_and_large(void)
{
/* Check whether large vectored requests, and large single requests,
* succeed. These are request patterns commonly used by MFS and the
* filter driver, respectively. We try the same test twice: once with
* a common block size, and once to push against the max request size.
*/
size_t max_block;
/* Make sure that the maximum size does not exceed the target device
* size, minus the margins we need for testing here and there.
*/
if (max_size > part.size - sector_size * 4)
max_size = part.size - sector_size * 4;
/* Compute the largest sector multiple which, when multiplied by
* NR_IOREQS, is no more than the maximum transfer size. Note that if
* max_size is not a multiple of sector_size, we're not going up to the
* limit entirely this way.
*/
max_block = max_size / NR_IOREQS;
max_block -= max_block % sector_size;
#define COMMON_BLOCK_SIZE 4096
test_group("vector and large, common block", TRUE);
vector_and_large_sub(COMMON_BLOCK_SIZE);
if (max_block != COMMON_BLOCK_SIZE) {
test_group("vector and large, large block", TRUE);
vector_and_large_sub(max_block);
}
}
static void open_device(dev_t minor)
{
/* Open a partition or subpartition. Remember that it has been opened,
* so that we can reopen it later in the event of a driver crash.
*/
message m;
result_t res;
memset(&m, 0, sizeof(m));
m.m_type = BDEV_OPEN;
m.m_lbdev_lblockdriver_msg.minor = minor;
m.m_lbdev_lblockdriver_msg.access = may_write ? (BDEV_R_BIT | BDEV_W_BIT) : BDEV_R_BIT;
m.m_lbdev_lblockdriver_msg.id = lrand48();
sendrec_driver(&m, OK, &res);
/* We assume that this call is supposed to succeed. We pretend it
* always succeeds, so that close_device() won't get confused later.
*/
assert(nr_opened < NR_OPENED);
opened[nr_opened++] = minor;
got_result(&res, minor == driver_minor ? "opening the main partition" :
"opening a subpartition");
}
static void close_device(dev_t minor)
{
/* Close a partition or subpartition. Remove it from the list of opened
* devices.
*/
message m;
result_t res;
int i;
memset(&m, 0, sizeof(m));
m.m_type = BDEV_CLOSE;
m.m_lbdev_lblockdriver_msg.minor = minor;
m.m_lbdev_lblockdriver_msg.id = lrand48();
sendrec_driver(&m, OK, &res);
assert(nr_opened > 0);
for (i = 0; i < nr_opened; i++) {
if (opened[i] == minor) {
opened[i] = opened[--nr_opened];
break;
}
}
got_result(&res, minor == driver_minor ? "closing the main partition" :
"closing a subpartition");
}
static int vir_ioctl(dev_t minor, unsigned long req, void *ptr, ssize_t exp,
result_t *res)
{
/* Perform an I/O control request, using a local buffer.
*/
cp_grant_id_t grant;
message m;
int r, perm;
assert(!_MINIX_IOCTL_BIG(req)); /* not supported */
perm = 0;
if (_MINIX_IOCTL_IOR(req)) perm |= CPF_WRITE;
if (_MINIX_IOCTL_IOW(req)) perm |= CPF_READ;
if ((grant = cpf_grant_direct(driver_endpt, (vir_bytes) ptr,
_MINIX_IOCTL_SIZE(req), perm)) == GRANT_INVALID)
panic("unable to allocate grant");
memset(&m, 0, sizeof(m));
m.m_type = BDEV_IOCTL;
m.m_lbdev_lblockdriver_msg.minor = minor;
m.m_lbdev_lblockdriver_msg.request = req;
m.m_lbdev_lblockdriver_msg.grant = grant;
m.m_lbdev_lblockdriver_msg.user = NONE;
m.m_lbdev_lblockdriver_msg.id = lrand48();
r = sendrec_driver(&m, exp, res);
if (cpf_revoke(grant) == -1)
panic("unable to revoke grant");
return r;
}
static void misc_ioctl(void)
{
/* Test some ioctls.
*/
result_t res;
int openct;
test_group("test miscellaneous ioctls", TRUE);
/* Retrieve the main partition's base and size. Save for later. */
vir_ioctl(driver_minor, DIOCGETP, &part, OK, &res);
got_result(&res, "ioctl to get partition");
/* The other tests do not check whether there is sufficient room. */
if (res.type == RESULT_OK && part.size < (u64_t)max_size * 2)
output("WARNING: small partition, some tests may fail\n");
/* Test retrieving global driver open count. */
openct = 0x0badcafe;
vir_ioctl(driver_minor, DIOCOPENCT, &openct, OK, &res);
/* We assume that we're the only client to the driver right now. */
if (res.type == RESULT_OK && openct != 1) {
res.type = RESULT_BADVALUE;
res.value = openct;
}
got_result(&res, "ioctl to get open count");
/* Test increasing and re-retrieving open count. */
open_device(driver_minor);
openct = 0x0badcafe;
vir_ioctl(driver_minor, DIOCOPENCT, &openct, OK, &res);
if (res.type == RESULT_OK && openct != 2) {
res.type = RESULT_BADVALUE;
res.value = openct;
}
got_result(&res, "increased open count after opening");
/* Test decreasing and re-retrieving open count. */
close_device(driver_minor);
openct = 0x0badcafe;
vir_ioctl(driver_minor, DIOCOPENCT, &openct, OK, &res);
if (res.type == RESULT_OK && openct != 1) {
res.type = RESULT_BADVALUE;
res.value = openct;
}
got_result(&res, "decreased open count after closing");
}
static void read_limits(dev_t sub0_minor, dev_t sub1_minor, size_t sub_size)
{
/* Test reads up to, across, and beyond partition limits.
*/
u8_t *buf_ptr;
size_t buf_size;
u32_t sum, sum2, sum3;
result_t res;
test_group("read around subpartition limits", TRUE);
buf_size = sector_size * 3;
buf_ptr = alloc_dma_memory(buf_size);
/* Read one sector up to the partition limit. */
fill_rand(buf_ptr, buf_size);
simple_xfer(sub0_minor, (u64_t)sub_size - sector_size, buf_ptr,
sector_size, FALSE, sector_size, &res);
sum = get_sum(buf_ptr, sector_size);
got_result(&res, "one sector read up to partition end");
/* Read three sectors up to the partition limit. */
fill_rand(buf_ptr, buf_size);
simple_xfer(sub0_minor, (u64_t)sub_size - buf_size, buf_ptr, buf_size,
FALSE, buf_size, &res);
test_sum(buf_ptr + sector_size * 2, sector_size, sum, TRUE, &res);
sum2 = get_sum(buf_ptr + sector_size, sector_size * 2);
got_result(&res, "multisector read up to partition end");
/* Read three sectors, two up to and one beyond the partition end. */
fill_rand(buf_ptr, buf_size);
sum3 = get_sum(buf_ptr + sector_size * 2, sector_size);
simple_xfer(sub0_minor, (u64_t)sub_size - sector_size * 2, buf_ptr,
buf_size, FALSE, sector_size * 2, &res);
test_sum(buf_ptr, sector_size * 2, sum2, TRUE, &res);
test_sum(buf_ptr + sector_size * 2, sector_size, sum3, TRUE, &res);
got_result(&res, "read somewhat across partition end");
/* Read three sectors, one up to and two beyond the partition end. */
fill_rand(buf_ptr, buf_size);
sum2 = get_sum(buf_ptr + sector_size, sector_size * 2);
simple_xfer(sub0_minor, (u64_t)sub_size - sector_size, buf_ptr,
buf_size, FALSE, sector_size, &res);
test_sum(buf_ptr, sector_size, sum, TRUE, &res);
test_sum(buf_ptr + sector_size, sector_size * 2, sum2, TRUE, &res);
got_result(&res, "read mostly across partition end");
/* Read one sector starting at the partition end. */
sum = fill_rand(buf_ptr, buf_size);
sum2 = get_sum(buf_ptr, sector_size);
simple_xfer(sub0_minor, (u64_t)sub_size, buf_ptr, sector_size, FALSE,
0, &res);
test_sum(buf_ptr, sector_size, sum2, TRUE, &res);
got_result(&res, "one sector read at partition end");
/* Read three sectors starting at the partition end. */
simple_xfer(sub0_minor, (u64_t)sub_size, buf_ptr, buf_size, FALSE, 0,
&res);
test_sum(buf_ptr, buf_size, sum, TRUE, &res);
got_result(&res, "multisector read at partition end");
/* Read one sector beyond the partition end. */
simple_xfer(sub0_minor, (u64_t)sub_size + sector_size, buf_ptr,
buf_size, FALSE, 0, &res);
test_sum(buf_ptr, sector_size, sum2, TRUE, &res);
got_result(&res, "single sector read beyond partition end");
/* Read three sectors way beyond the partition end. */
simple_xfer(sub0_minor, 0x1000000000000000ULL, buf_ptr, buf_size,
FALSE, 0, &res);
test_sum(buf_ptr, buf_size, sum, TRUE, &res);
/* Test negative offsets. This request should return EOF or fail; we
* assume that it return EOF here (because that is what the AHCI driver
* does, to avoid producing errors for requests close to the 2^64 byte
* position limit [yes, this will indeed never happen anyway]). This is
* more or less a bad requests test, but we cannot do it without
* setting up subpartitions first.
*/
simple_xfer(sub1_minor, 0xffffffffffffffffULL - sector_size + 1,
buf_ptr, sector_size, FALSE, 0, &res);
test_sum(buf_ptr, sector_size, sum2, TRUE, &res);
got_result(&res, "read with negative offset");
/* Clean up. */
free_dma_memory(buf_ptr, buf_size);
}
static void write_limits(dev_t sub0_minor, dev_t sub1_minor, size_t sub_size)
{
/* Test writes up to, across, and beyond partition limits. Use the
* first given subpartition to test, and the second to make sure there
* are no overruns. The given size is the size of each of the
* subpartitions. Note that the necessity to check the results using
* readback, makes this more or less a superset of the read test.
*/
u8_t *buf_ptr;
size_t buf_size;
u32_t sum, sum2, sum3, sub1_sum;
result_t res;
test_group("write around subpartition limits", may_write);
if (!may_write)
return;
buf_size = sector_size * 3;
buf_ptr = alloc_dma_memory(buf_size);
/* Write to the start of the second subpartition, so that we can
* reliably check whether the contents have changed later.
*/
sub1_sum = fill_rand(buf_ptr, buf_size);
simple_xfer(sub1_minor, 0ULL, buf_ptr, buf_size, TRUE, buf_size, &res);
got_result(&res, "write to second subpartition");
/* Write one sector, up to the partition limit. */
sum = fill_rand(buf_ptr, sector_size);
simple_xfer(sub0_minor, (u64_t)sub_size - sector_size, buf_ptr,
sector_size, TRUE, sector_size, &res);
got_result(&res, "write up to partition end");
/* Read back to make sure the results have persisted. */
fill_rand(buf_ptr, sector_size * 2);
simple_xfer(sub0_minor, (u64_t)sub_size - sector_size * 2, buf_ptr,
sector_size * 2, FALSE, sector_size * 2, &res);
test_sum(buf_ptr + sector_size, sector_size, sum, TRUE, &res);
got_result(&res, "read up to partition end");
/* Write three sectors, two up to and one beyond the partition end. */
fill_rand(buf_ptr, buf_size);
sum = get_sum(buf_ptr + sector_size, sector_size);
sum3 = get_sum(buf_ptr, sector_size);
simple_xfer(sub0_minor, (u64_t)sub_size - sector_size * 2, buf_ptr,
buf_size, TRUE, sector_size * 2, &res);
got_result(&res, "write somewhat across partition end");
/* Read three sectors, one up to and two beyond the partition end. */
fill_rand(buf_ptr, buf_size);
sum2 = get_sum(buf_ptr + sector_size, sector_size * 2);
simple_xfer(sub0_minor, (u64_t)sub_size - sector_size, buf_ptr,
buf_size, FALSE, sector_size, &res);
test_sum(buf_ptr, sector_size, sum, TRUE, &res);
test_sum(buf_ptr + sector_size, sector_size * 2, sum2, TRUE, &res);
got_result(&res, "read mostly across partition end");
/* Repeat this but with write and read start positions swapped. */
fill_rand(buf_ptr, buf_size);
sum = get_sum(buf_ptr, sector_size);
simple_xfer(sub0_minor, (u64_t)sub_size - sector_size, buf_ptr,
buf_size, TRUE, sector_size, &res);
got_result(&res, "write mostly across partition end");
fill_rand(buf_ptr, buf_size);
sum2 = get_sum(buf_ptr + sector_size * 2, sector_size);
simple_xfer(sub0_minor, (u64_t)sub_size - sector_size * 2, buf_ptr,
buf_size, FALSE, sector_size * 2, &res);
test_sum(buf_ptr, sector_size, sum3, TRUE, &res);
test_sum(buf_ptr + sector_size, sector_size, sum, TRUE, &res);
test_sum(buf_ptr + sector_size * 2, sector_size, sum2, TRUE, &res);
got_result(&res, "read somewhat across partition end");
/* Write one sector at the end of the partition. */
fill_rand(buf_ptr, sector_size);
simple_xfer(sub0_minor, (u64_t)sub_size, buf_ptr, sector_size, TRUE, 0,
&res);
got_result(&res, "write at partition end");
/* Write one sector beyond the end of the partition. */
simple_xfer(sub0_minor, (u64_t)sub_size + sector_size, buf_ptr,
sector_size, TRUE, 0, &res);
got_result(&res, "write beyond partition end");
/* Read from the start of the second subpartition, and see if it
* matches what we wrote into it earlier.
*/
fill_rand(buf_ptr, buf_size);
simple_xfer(sub1_minor, 0ULL, buf_ptr, buf_size, FALSE, buf_size,
&res);
test_sum(buf_ptr, buf_size, sub1_sum, TRUE, &res);
got_result(&res, "read from second subpartition");
/* Test offset wrapping, but this time for writes. */
fill_rand(buf_ptr, sector_size);
simple_xfer(sub1_minor, 0xffffffffffffffffULL - sector_size + 1,
buf_ptr, sector_size, TRUE, 0, &res);
got_result(&res, "write with negative offset");
/* If the last request erroneously succeeded, it would have overwritten
* the last sector of the first subpartition.
*/
simple_xfer(sub0_minor, (u64_t)sub_size - sector_size, buf_ptr,
sector_size, FALSE, sector_size, &res);
test_sum(buf_ptr, sector_size, sum, TRUE, &res);
got_result(&res, "read up to partition end");
/* Clean up. */
free_dma_memory(buf_ptr, buf_size);
}
static void vir_limits(dev_t sub0_minor, dev_t sub1_minor, int part_secs)
{
/* Create virtual, temporary subpartitions through the DIOCSETP ioctl,
* and perform tests on the resulting subpartitions.
*/
struct part_geom subpart, subpart2;
size_t sub_size;
result_t res;
test_group("virtual subpartition limits", TRUE);
/* Open the subpartitions. This is somewhat dodgy; we rely on the
* driver allowing this even if no subpartitions exist. We cannot do
* this test without doing a DIOCSETP on an open subdevice, though.
*/
open_device(sub0_minor);
open_device(sub1_minor);
sub_size = sector_size * part_secs;
/* Set, and check, the size of the first subpartition. */
subpart = part;
subpart.size = (u64_t)sub_size;
vir_ioctl(sub0_minor, DIOCSETP, &subpart, OK, &res);
got_result(&res, "ioctl to set first subpartition");
vir_ioctl(sub0_minor, DIOCGETP, &subpart2, OK, &res);
if (res.type == RESULT_OK && (subpart.base != subpart2.base ||
subpart.size != subpart2.size)) {
res.type = RESULT_BADVALUE;
res.value = 0;
}
got_result(&res, "ioctl to get first subpartition");
/* Set, and check, the base and size of the second subpartition. */
subpart = part;
subpart.base += sub_size;
subpart.size = (u64_t)sub_size;
vir_ioctl(sub1_minor, DIOCSETP, &subpart, OK, &res);
got_result(&res, "ioctl to set second subpartition");
vir_ioctl(sub1_minor, DIOCGETP, &subpart2, OK, &res);
if (res.type == RESULT_OK && (subpart.base != subpart2.base ||
subpart.size != subpart2.size)) {
res.type = RESULT_BADVALUE;
res.value = 0;
}
got_result(&res, "ioctl to get second subpartition");
/* Perform the actual I/O tests. */
read_limits(sub0_minor, sub1_minor, sub_size);
write_limits(sub0_minor, sub1_minor, sub_size);
/* Clean up. */
close_device(sub1_minor);
close_device(sub0_minor);
}
static void real_limits(dev_t sub0_minor, dev_t sub1_minor, int part_secs)
{
/* Create our own subpartitions by writing a partition table, and
* perform tests on the resulting real subpartitions.
*/
u8_t *buf_ptr;
size_t buf_size, sub_size;
struct part_geom subpart;
struct part_entry *entry;
result_t res;
test_group("real subpartition limits", may_write);
if (!may_write)
return;
sub_size = sector_size * part_secs;
/* Technically, we should be using 512 instead of sector_size in
* various places, because even on CD-ROMs, the partition tables are
* 512 bytes and the sector counts are based on 512-byte sectors in it.
* We ignore this subtlety because CD-ROMs are assumed to be read-only
* anyway.
*/
buf_size = sector_size;
buf_ptr = alloc_dma_memory(buf_size);
memset(buf_ptr, 0, buf_size);
/* Write an invalid partition table. */
simple_xfer(driver_minor, 0ULL, buf_ptr, buf_size, TRUE, buf_size,
&res);
got_result(&res, "write of invalid partition table");
/* Get the disk driver to reread the partition table. This should
* happen (at least) when the device is fully closed and then reopened.
* The ioctl test already made sure that we're the only client.
*/
close_device(driver_minor);
open_device(driver_minor);
/* See if our changes are visible. We expect the subpartitions to have
* a size of zero now, indicating that they're not there. For actual
* subpartitions (as opposed to normal partitions), this requires the
* driver to zero them out, because the partition code does not do so.
*/
open_device(sub0_minor);
open_device(sub1_minor);
vir_ioctl(sub0_minor, DIOCGETP, &subpart, 0, &res);
if (res.type == RESULT_OK && subpart.size != 0) {
res.type = RESULT_BADVALUE;
res.value = ex64lo(subpart.size);
}
got_result(&res, "ioctl to get first subpartition");
vir_ioctl(sub1_minor, DIOCGETP, &subpart, 0, &res);
if (res.type == RESULT_OK && subpart.size != 0) {
res.type = RESULT_BADVALUE;
res.value = ex64lo(subpart.size);
}
got_result(&res, "ioctl to get second subpartition");
close_device(sub1_minor);
close_device(sub0_minor);
/* Now write a valid partition table. */
memset(buf_ptr, 0, buf_size);
entry = (struct part_entry *) &buf_ptr[PART_TABLE_OFF];
entry[0].sysind = MINIX_PART;
entry[0].lowsec = part.base / sector_size + 1;
entry[0].size = part_secs;
entry[1].sysind = MINIX_PART;
entry[1].lowsec = entry[0].lowsec + entry[0].size;
entry[1].size = part_secs;
buf_ptr[510] = 0x55;
buf_ptr[511] = 0xAA;
simple_xfer(driver_minor, 0ULL, buf_ptr, buf_size, TRUE, buf_size,
&res);
got_result(&res, "write of valid partition table");
/* Same as above. */
close_device(driver_minor);
open_device(driver_minor);
/* Again, see if our changes are visible. This time the proper base and
* size should be there.
*/
open_device(sub0_minor);
open_device(sub1_minor);
vir_ioctl(sub0_minor, DIOCGETP, &subpart, 0, &res);
if (res.type == RESULT_OK &&
(subpart.base != part.base + sector_size ||
subpart.size != (u64_t)part_secs * sector_size)) {
res.type = RESULT_BADVALUE;
res.value = 0;
}
got_result(&res, "ioctl to get first subpartition");
vir_ioctl(sub1_minor, DIOCGETP, &subpart, 0, &res);
if (res.type == RESULT_OK &&
(subpart.base != part.base + (1 + part_secs) * sector_size ||
subpart.size != (u64_t)part_secs * sector_size)) {
res.type = RESULT_BADVALUE;
res.value = 0;
}
got_result(&res, "ioctl to get second subpartition");
/* Now perform the actual I/O tests. */
read_limits(sub0_minor, sub1_minor, sub_size);
write_limits(sub0_minor, sub1_minor, sub_size);
/* Clean up. */
close_device(sub0_minor);
close_device(sub1_minor);
free_dma_memory(buf_ptr, buf_size);
}
static void part_limits(void)
{
/* Test reads and writes up to, across, and beyond partition limits.
* As a side effect, test reading and writing partition sizes and
* rereading partition tables.
*/
dev_t par, sub0_minor, sub1_minor;
/* First determine the first two subpartitions of the partition that we
* are operating on. If we are already operating on a subpartition, we
* cannot conduct this test.
*/
if (driver_minor >= MINOR_d0p0s0) {
output("WARNING: operating on subpartition, "
"skipping partition tests\n");
return;
}
par = driver_minor % DEV_PER_DRIVE;
if (par > 0) /* adapted from libdriver's drvlib code */
sub0_minor = MINOR_d0p0s0 + ((driver_minor / DEV_PER_DRIVE) *
NR_PARTITIONS + par - 1) * NR_PARTITIONS;
else
sub0_minor = driver_minor + 1;
sub1_minor = sub0_minor + 1;
#define PART_SECS 9 /* sectors in each partition. must be >= 4. */
/* First try the test with temporarily specified subpartitions. */
vir_limits(sub0_minor, sub1_minor, PART_SECS);
/* Then, if we're allowed to write, try the test with real, persisted
* subpartitions.
*/
real_limits(sub0_minor, sub1_minor, PART_SECS - 1);
}
static void unaligned_size_io(u64_t base_pos, u8_t *buf_ptr, size_t buf_size,
u8_t *sec_ptr[2], int sectors, int pattern, u32_t ssum[5])
{
/* Perform a single small-element I/O read, write, readback test.
* The number of sectors and the pattern varies with each call.
* The ssum array has to be updated to reflect the five sectors'
* checksums on disk, if writing is enabled. Note that for
*/
iovec_t iov[3], iovt[3];
u32_t rsum[3];
result_t res;
size_t total_size;
int i, nr_req;
base_pos += sector_size;
total_size = sector_size * sectors;
/* If the limit is two elements per sector, we cannot test three
* elements in a single sector.
*/
if (sector_size / element_size == 2 && sectors == 1 && pattern == 2)
return;
/* Set up the buffers and I/O vector. We use different buffers for the
* elements to minimize the chance that something "accidentally" goes
* right, but that means we have to do memory copying to do checksum
* computation.
*/
fill_rand(sec_ptr[0], sector_size);
rsum[0] =
get_sum(sec_ptr[0] + element_size, sector_size - element_size);
fill_rand(buf_ptr, buf_size);
switch (pattern) {
case 0:
/* First pattern: a small element on the left. */
iovt[0].iov_addr = (vir_bytes) sec_ptr[0];
iovt[0].iov_size = element_size;
iovt[1].iov_addr = (vir_bytes) buf_ptr;
iovt[1].iov_size = total_size - element_size;
rsum[1] = get_sum(buf_ptr + iovt[1].iov_size, element_size);
nr_req = 2;
break;
case 1:
/* Second pattern: a small element on the right. */
iovt[0].iov_addr = (vir_bytes) buf_ptr;
iovt[0].iov_size = total_size - element_size;
rsum[1] = get_sum(buf_ptr + iovt[0].iov_size, element_size);
iovt[1].iov_addr = (vir_bytes) sec_ptr[0];
iovt[1].iov_size = element_size;
nr_req = 2;
break;
case 2:
/* Third pattern: a small element on each side. */
iovt[0].iov_addr = (vir_bytes) sec_ptr[0];
iovt[0].iov_size = element_size;
iovt[1].iov_addr = (vir_bytes) buf_ptr;
iovt[1].iov_size = total_size - element_size * 2;
rsum[1] = get_sum(buf_ptr + iovt[1].iov_size,
element_size * 2);
fill_rand(sec_ptr[1], sector_size);
iovt[2].iov_addr = (vir_bytes) sec_ptr[1];
iovt[2].iov_size = element_size;
rsum[2] = get_sum(sec_ptr[1] + element_size,
sector_size - element_size);
nr_req = 3;
break;
default:
assert(0);
}
/* Perform a read with small elements, and test whether the result is
* as expected.
*/
memcpy(iov, iovt, sizeof(iov));
vir_xfer(driver_minor, base_pos, iov, nr_req, FALSE, total_size, &res);
test_sum(sec_ptr[0] + element_size, sector_size - element_size,
rsum[0], TRUE, &res);
switch (pattern) {
case 0:
test_sum(buf_ptr + iovt[1].iov_size, element_size, rsum[1],
TRUE, &res);
memmove(buf_ptr + element_size, buf_ptr, iovt[1].iov_size);
memcpy(buf_ptr, sec_ptr[0], element_size);
break;
case 1:
test_sum(buf_ptr + iovt[0].iov_size, element_size, rsum[1],
TRUE, &res);
memcpy(buf_ptr + iovt[0].iov_size, sec_ptr[0], element_size);
break;
case 2:
test_sum(buf_ptr + iovt[1].iov_size, element_size * 2, rsum[1],
TRUE, &res);
test_sum(sec_ptr[1] + element_size, sector_size - element_size,
rsum[2], TRUE, &res);
memmove(buf_ptr + element_size, buf_ptr, iovt[1].iov_size);
memcpy(buf_ptr, sec_ptr[0], element_size);
memcpy(buf_ptr + element_size + iovt[1].iov_size, sec_ptr[1],
element_size);
break;
}
for (i = 0; i < sectors; i++)
test_sum(buf_ptr + sector_size * i, sector_size, ssum[1 + i],
TRUE, &res);
got_result(&res, "read with small elements");
/* In read-only mode, we have nothing more to do. */
if (!may_write)
return;
/* Use the same I/O vector to perform a write with small elements.
* This will cause the checksums of the target sectors to change,
* so we need to update those for both verification and later usage.
*/
for (i = 0; i < sectors; i++)
ssum[1 + i] =
fill_rand(buf_ptr + sector_size * i, sector_size);
switch (pattern) {
case 0:
memcpy(sec_ptr[0], buf_ptr, element_size);
memmove(buf_ptr, buf_ptr + element_size, iovt[1].iov_size);
fill_rand(buf_ptr + iovt[1].iov_size, element_size);
break;
case 1:
memcpy(sec_ptr[0], buf_ptr + iovt[0].iov_size, element_size);
fill_rand(buf_ptr + iovt[0].iov_size, element_size);
break;
case 2:
memcpy(sec_ptr[0], buf_ptr, element_size);
memcpy(sec_ptr[1], buf_ptr + element_size + iovt[1].iov_size,
element_size);
memmove(buf_ptr, buf_ptr + element_size, iovt[1].iov_size);
fill_rand(buf_ptr + iovt[1].iov_size, element_size * 2);
break;
}
memcpy(iov, iovt, sizeof(iov));
vir_xfer(driver_minor, base_pos, iov, nr_req, TRUE, total_size, &res);
got_result(&res, "write with small elements");
/* Now perform normal readback verification. */
fill_rand(buf_ptr, sector_size * 3);
simple_xfer(driver_minor, base_pos, buf_ptr, sector_size * 3, FALSE,
sector_size * 3, &res);
for (i = 0; i < 3; i++)
test_sum(buf_ptr + sector_size * i, sector_size, ssum[1 + i],
TRUE, &res);
got_result(&res, "readback verification");
}
static void unaligned_size(void)
{
/* Test sector-unaligned sizes in I/O vector elements. The total size
* of the request, however, has to add up to the sector size.
*/
u8_t *buf_ptr, *sec_ptr[2];
size_t buf_size;
u32_t sum = 0L, ssum[5];
u64_t base_pos;
result_t res;
int i;
test_group("sector-unaligned elements", sector_size != element_size);
/* We can only do this test if the driver allows small elements. */
if (sector_size == element_size)
return;
/* Crashing on bad user input, terrible! */
assert(sector_size % element_size == 0);
/* Establish a baseline by writing and reading back five sectors; or
* by reading only, if writing is disabled.
*/
buf_size = sector_size * 5;
base_pos = (u64_t)sector_size * 2;
buf_ptr = alloc_dma_memory(buf_size);
sec_ptr[0] = alloc_dma_memory(sector_size);
sec_ptr[1] = alloc_dma_memory(sector_size);
if (may_write) {
sum = fill_rand(buf_ptr, buf_size);
for (i = 0; i < 5; i++)
ssum[i] = get_sum(buf_ptr + sector_size * i,
sector_size);
simple_xfer(driver_minor, base_pos, buf_ptr, buf_size, TRUE,
buf_size, &res);
got_result(&res, "write several sectors");
}
fill_rand(buf_ptr, buf_size);
simple_xfer(driver_minor, base_pos, buf_ptr, buf_size, FALSE, buf_size,
&res);
if (may_write) {
test_sum(buf_ptr, buf_size, sum, TRUE, &res);
}
else {
for (i = 0; i < 5; i++)
ssum[i] = get_sum(buf_ptr + sector_size * i,
sector_size);
}
got_result(&res, "read several sectors");
/* We do nine subtests. The first three involve only the second sector;
* the second three involve the second and third sectors, and the third
* three involve all of the middle sectors. Each triplet tests small
* elements at the left, at the right, and at both the left and the
* right of the area. For each operation, we first do an unaligned
* read, and if writing is enabled, an unaligned write and an aligned
* read.
*/
for (i = 0; i < 9; i++) {
unaligned_size_io(base_pos, buf_ptr, buf_size, sec_ptr,
i / 3 + 1, i % 3, ssum);
}
/* If writing was enabled, make sure that the first and fifth sector
* have remained untouched.
*/
if (may_write) {
fill_rand(buf_ptr, buf_size);
simple_xfer(driver_minor, base_pos, buf_ptr, buf_size, FALSE,
buf_size, &res);
test_sum(buf_ptr, sector_size, ssum[0], TRUE, &res);
test_sum(buf_ptr + sector_size * 4, sector_size, ssum[4], TRUE,
&res);
got_result(&res, "check first and last sectors");
}
/* Clean up. */
free_dma_memory(sec_ptr[1], sector_size);
free_dma_memory(sec_ptr[0], sector_size);
free_dma_memory(buf_ptr, buf_size);
}
static void unaligned_pos1(void)
{
/* Test sector-unaligned positions and total sizes for requests. This
* is a read-only test for now. Write support should be added later.
* In the current context, the term "lead" means an unwanted first part
* of a sector, and "trail" means an unwanted last part of a sector.
*/
u8_t *buf_ptr, *buf2_ptr;
size_t buf_size, buf2_size, size;
u32_t sum, sum2;
u64_t base_pos;
result_t res;
test_group("sector-unaligned positions, part one",
min_read != sector_size);
/* We can only do this test if the driver allows small read requests.
*/
if (min_read == sector_size)
return;
assert(sector_size % min_read == 0);
assert(min_read % element_size == 0);
/* Establish a baseline by writing and reading back three sectors; or
* by reading only, if writing is disabled.
*/
buf_size = buf2_size = sector_size * 3;
base_pos = (u64_t)sector_size * 3;
buf_ptr = alloc_dma_memory(buf_size);
buf2_ptr = alloc_dma_memory(buf2_size);
if (may_write) {
sum = fill_rand(buf_ptr, buf_size);
simple_xfer(driver_minor, base_pos, buf_ptr, buf_size, TRUE,
buf_size, &res);
got_result(&res, "write several sectors");
}
fill_rand(buf_ptr, buf_size);
simple_xfer(driver_minor, base_pos, buf_ptr, buf_size, FALSE, buf_size,
&res);
if (may_write)
test_sum(buf_ptr, buf_size, sum, TRUE, &res);
got_result(&res, "read several sectors");
/* Start with a simple test that operates within a single sector,
* first using a lead.
*/
fill_rand(buf2_ptr, sector_size);
sum = get_sum(buf2_ptr + min_read, sector_size - min_read);
simple_xfer(driver_minor, base_pos + sector_size - min_read,
buf2_ptr, min_read, FALSE, min_read, &res);
test_sum(buf2_ptr, min_read, get_sum(buf_ptr + sector_size - min_read,
min_read), TRUE, &res);
test_sum(buf2_ptr + min_read, sector_size - min_read, sum, TRUE,
&res);
got_result(&res, "single sector read with lead");
/* Then a trail. */
fill_rand(buf2_ptr, sector_size);
sum = get_sum(buf2_ptr, sector_size - min_read);
simple_xfer(driver_minor, base_pos, buf2_ptr + sector_size - min_read,
min_read, FALSE, min_read, &res);
test_sum(buf2_ptr + sector_size - min_read, min_read, get_sum(buf_ptr,
min_read), TRUE, &res);
test_sum(buf2_ptr, sector_size - min_read, sum, TRUE, &res);
got_result(&res, "single sector read with trail");
/* And then a lead and a trail, unless min_read is half the sector
* size, in which case this will be another lead test.
*/
fill_rand(buf2_ptr, sector_size);
sum = get_sum(buf2_ptr, min_read);
sum2 = get_sum(buf2_ptr + min_read * 2, sector_size - min_read * 2);
simple_xfer(driver_minor, base_pos + min_read, buf2_ptr + min_read,
min_read, FALSE, min_read, &res);
test_sum(buf2_ptr + min_read, min_read, get_sum(buf_ptr + min_read,
min_read), TRUE, &res);
test_sum(buf2_ptr, min_read, sum, TRUE, &res);
test_sum(buf2_ptr + min_read * 2, sector_size - min_read * 2, sum2,
TRUE, &res);
got_result(&res, "single sector read with lead and trail");
/* Now do the same but with three sectors, and still only one I/O
* vector element. First up: lead.
*/
size = min_read + sector_size * 2;
fill_rand(buf2_ptr, buf2_size);
sum = get_sum(buf2_ptr + size, buf2_size - size);
simple_xfer(driver_minor, base_pos + sector_size - min_read, buf2_ptr,
size, FALSE, size, &res);
test_sum(buf2_ptr, size, get_sum(buf_ptr + sector_size - min_read,
size), TRUE, &res);
test_sum(buf2_ptr + size, buf2_size - size, sum, TRUE, &res);
got_result(&res, "multisector read with lead");
/* Then trail. */
fill_rand(buf2_ptr, buf2_size);
sum = get_sum(buf2_ptr + size, buf2_size - size);
simple_xfer(driver_minor, base_pos, buf2_ptr, size, FALSE, size, &res);
test_sum(buf2_ptr, size, get_sum(buf_ptr, size), TRUE, &res);
test_sum(buf2_ptr + size, buf2_size - size, sum, TRUE, &res);
got_result(&res, "multisector read with trail");
/* Then lead and trail. Use sector size as transfer unit to throw off
* simplistic lead/trail detection.
*/
fill_rand(buf2_ptr, buf2_size);
sum = get_sum(buf2_ptr + sector_size, buf2_size - sector_size);
simple_xfer(driver_minor, base_pos + min_read, buf2_ptr, sector_size,
FALSE, sector_size, &res);
test_sum(buf2_ptr, sector_size, get_sum(buf_ptr + min_read,
sector_size), TRUE, &res);
test_sum(buf2_ptr + sector_size, buf2_size - sector_size, sum, TRUE,
&res);
got_result(&res, "multisector read with lead and trail");
/* Clean up. */
free_dma_memory(buf2_ptr, buf2_size);
free_dma_memory(buf_ptr, buf_size);
}
static void unaligned_pos2(void)
{
/* Test sector-unaligned positions and total sizes for requests, second
* part. This one tests the use of multiple I/O vector elements, and
* tries to push the limits of the driver by completely filling an I/O
* vector and going up to the maximum request size.
*/
u8_t *buf_ptr, *buf2_ptr;
size_t buf_size, buf2_size, max_block;
u32_t sum = 0L, sum2 = 0L, rsum[NR_IOREQS];
u64_t base_pos;
iovec_t iov[NR_IOREQS];
result_t res;
int i;
test_group("sector-unaligned positions, part two",
min_read != sector_size);
/* We can only do this test if the driver allows small read requests.
*/
if (min_read == sector_size)
return;
buf_size = buf2_size = max_size + sector_size;
base_pos = (u64_t)sector_size * 3;
buf_ptr = alloc_dma_memory(buf_size);
buf2_ptr = alloc_dma_memory(buf2_size);
/* First establish a baseline. We need two requests for this, as the
* total area intentionally exceeds the max request size.
*/
if (may_write) {
sum = fill_rand(buf_ptr, max_size);
simple_xfer(driver_minor, base_pos, buf_ptr, max_size, TRUE,
max_size, &res);
got_result(&res, "large baseline write");
sum2 = fill_rand(buf_ptr + max_size, sector_size);
simple_xfer(driver_minor, base_pos + max_size,
buf_ptr + max_size, sector_size, TRUE, sector_size,
&res);
got_result(&res, "small baseline write");
}
fill_rand(buf_ptr, buf_size);
simple_xfer(driver_minor, base_pos, buf_ptr, max_size, FALSE, max_size,
&res);
if (may_write)
test_sum(buf_ptr, max_size, sum, TRUE, &res);
got_result(&res, "large baseline read");
simple_xfer(driver_minor, base_pos + max_size, buf_ptr + max_size,
sector_size, FALSE, sector_size, &res);
if (may_write)
test_sum(buf_ptr + max_size, sector_size, sum2, TRUE, &res);
got_result(&res, "small baseline read");
/* First construct a full vector with minimal sizes. The resulting area
* may well fall within a single sector, if min_read is small enough.
*/
fill_rand(buf2_ptr, buf2_size);
for (i = 0; i < NR_IOREQS; i++) {
iov[i].iov_addr = (vir_bytes) buf2_ptr + i * sector_size;
iov[i].iov_size = min_read;
rsum[i] = get_sum(buf2_ptr + i * sector_size + min_read,
sector_size - min_read);
}
vir_xfer(driver_minor, base_pos + min_read, iov, NR_IOREQS, FALSE,
min_read * NR_IOREQS, &res);
for (i = 0; i < NR_IOREQS; i++) {
test_sum(buf2_ptr + i * sector_size + min_read,
sector_size - min_read, rsum[i], TRUE, &res);
memmove(buf2_ptr + i * min_read, buf2_ptr + i * sector_size,
min_read);
}
test_sum(buf2_ptr, min_read * NR_IOREQS, get_sum(buf_ptr + min_read,
min_read * NR_IOREQS), TRUE, &res);
got_result(&res, "small fully unaligned filled vector");
/* Sneak in a maximum sized request with a single I/O vector element,
* unaligned. If the driver splits up such large requests into smaller
* chunks, this tests whether it does so correctly in the presence of
* leads and trails.
*/
fill_rand(buf2_ptr, buf2_size);
simple_xfer(driver_minor, base_pos + min_read, buf2_ptr, max_size,
FALSE, max_size, &res);
test_sum(buf2_ptr, max_size, get_sum(buf_ptr + min_read, max_size),
TRUE, &res);
got_result(&res, "large fully unaligned single element");
/* Then try with a vector where each element is as large as possible.
* We don't have room to do bounds integrity checking here (we could
* make room, but this may be a lot of memory already).
*/
/* Compute the largest sector multiple which, when multiplied by
* NR_IOREQS, is no more than the maximum transfer size.
*/
max_block = max_size / NR_IOREQS;
max_block -= max_block % sector_size;
fill_rand(buf2_ptr, buf2_size);
for (i = 0; i < NR_IOREQS; i++) {
iov[i].iov_addr = (vir_bytes) buf2_ptr + i * max_block;
iov[i].iov_size = max_block;
}
vir_xfer(driver_minor, base_pos + min_read, iov, NR_IOREQS, FALSE,
max_block * NR_IOREQS, &res);
test_sum(buf2_ptr, max_block * NR_IOREQS, get_sum(buf_ptr + min_read,
max_block * NR_IOREQS), TRUE, &res);
got_result(&res, "large fully unaligned filled vector");
/* Clean up. */
free_dma_memory(buf2_ptr, buf2_size);
free_dma_memory(buf_ptr, buf_size);
}
static void sweep_area(u64_t base_pos)
{
/* Go over an eight-sector area from left (low address) to right (high
* address), reading and optionally writing in three-sector chunks, and
* advancing one sector at a time.
*/
u8_t *buf_ptr;
size_t buf_size;
u32_t sum = 0L, ssum[8];
result_t res;
int i, j;
buf_size = sector_size * 8;
buf_ptr = alloc_dma_memory(buf_size);
/* First (write to, if allowed, and) read from the entire area in one
* go, so that we know the (initial) contents of the area.
*/
if (may_write) {
sum = fill_rand(buf_ptr, buf_size);
simple_xfer(driver_minor, base_pos, buf_ptr, buf_size, TRUE,
buf_size, &res);
got_result(&res, "write to full area");
}
fill_rand(buf_ptr, buf_size);
simple_xfer(driver_minor, base_pos, buf_ptr, buf_size, FALSE, buf_size,
&res);
if (may_write)
test_sum(buf_ptr, buf_size, sum, TRUE, &res);
for (i = 0; i < 8; i++)
ssum[i] = get_sum(buf_ptr + sector_size * i, sector_size);
got_result(&res, "read from full area");
/* For each of the six three-sector subareas, first read from the
* subarea, check its checksum, and then (if allowed) write new content
* to it.
*/
for (i = 0; i < 6; i++) {
fill_rand(buf_ptr, sector_size * 3);
simple_xfer(driver_minor, base_pos + sector_size * i, buf_ptr,
sector_size * 3, FALSE, sector_size * 3, &res);
for (j = 0; j < 3; j++)
test_sum(buf_ptr + sector_size * j, sector_size,
ssum[i + j], TRUE, &res);
got_result(&res, "read from subarea");
if (!may_write)
continue;
fill_rand(buf_ptr, sector_size * 3);
simple_xfer(driver_minor, base_pos + sector_size * i, buf_ptr,
sector_size * 3, TRUE, sector_size * 3, &res);
for (j = 0; j < 3; j++)
ssum[i + j] = get_sum(buf_ptr + sector_size * j,
sector_size);
got_result(&res, "write to subarea");
}
/* Finally, if writing was enabled, do one final readback. */
if (may_write) {
fill_rand(buf_ptr, buf_size);
simple_xfer(driver_minor, base_pos, buf_ptr, buf_size, FALSE,
buf_size, &res);
for (i = 0; i < 8; i++)
test_sum(buf_ptr + sector_size * i, sector_size,
ssum[i], TRUE, &res);
got_result(&res, "readback from full area");
}
/* Clean up. */
free_dma_memory(buf_ptr, buf_size);
}
static void sweep_and_check(u64_t pos, int check_integ)
{
/* Perform an area sweep at the given position. If asked for, get an
* integrity checksum over the beginning of the disk (first writing
* known data into it if that is allowed) before doing the sweep, and
* test the integrity checksum against the disk contents afterwards.
*/
u8_t *buf_ptr;
size_t buf_size;
u32_t sum = 0L;
result_t res;
if (check_integ) {
buf_size = sector_size * 3;
buf_ptr = alloc_dma_memory(buf_size);
if (may_write) {
sum = fill_rand(buf_ptr, buf_size);
simple_xfer(driver_minor, 0ULL, buf_ptr, buf_size,
TRUE, buf_size, &res);
got_result(&res, "write integrity zone");
}
fill_rand(buf_ptr, buf_size);
simple_xfer(driver_minor, 0ULL, buf_ptr, buf_size, FALSE,
buf_size, &res);
if (may_write)
test_sum(buf_ptr, buf_size, sum, TRUE, &res);
else
sum = get_sum(buf_ptr, buf_size);
got_result(&res, "read integrity zone");
}
sweep_area(pos);
if (check_integ) {
fill_rand(buf_ptr, buf_size);
simple_xfer(driver_minor, 0ULL, buf_ptr, buf_size, FALSE,
buf_size, &res);
test_sum(buf_ptr, buf_size, sum, TRUE, &res);
got_result(&res, "check integrity zone");
free_dma_memory(buf_ptr, buf_size);
}
}
static void basic_sweep(void)
{
/* Perform a basic area sweep.
*/
test_group("basic area sweep", TRUE);
sweep_area((u64_t)sector_size);
}
static void high_disk_pos(void)
{
/* Test 64-bit absolute disk positions. This means that after adding
* partition base to the given position, the driver will be dealing
* with a position above 32 bit. We want to test the transition area
* only; if the entire partition base is above 32 bit, we have already
* effectively performed this test many times over. In other words, for
* this test, the partition must start below 4GB and end above 4GB,
* with at least four sectors on each side.
*/
u64_t base_pos;
base_pos = 0x100000000ULL | (sector_size * 4);
base_pos -= base_pos % sector_size;
/* The partition end must exceed 32 bits. */
if (part.base + part.size < base_pos) {
test_group("high disk positions", FALSE);
return;
}
base_pos -= sector_size * 8;
/* The partition start must not. */
if (base_pos < part.base) {
test_group("high disk positions", FALSE);
return;
}
test_group("high disk positions", TRUE);
base_pos -= part.base;
sweep_and_check(base_pos, part.base == 0ULL);
}
static void high_part_pos(void)
{
/* Test 64-bit partition-relative disk positions. In other words, use
* within the current partition a position that exceeds a 32-bit value.
* This requires the partition to be more than 4GB in size; we need an
* additional 4 sectors, to be exact.
*/
u64_t base_pos;
/* If the partition starts at the beginning of the disk, this test is
* no different from the high disk position test.
*/
if (part.base == 0ULL) {
/* don't complain: the test is simply superfluous now */
return;
}
base_pos = 0x100000000ULL | (sector_size * 4);
base_pos -= base_pos % sector_size;
if (part.size < base_pos) {
test_group("high partition positions", FALSE);
return;
}
test_group("high partition positions", TRUE);
base_pos -= sector_size * 8;
sweep_and_check(base_pos, TRUE);
}
static void high_lba_pos1(void)
{
/* Test 48-bit LBA positions, as opposed to *24-bit*. Drivers that only
* support 48-bit LBA ATA transfers, will treat the lower and upper 24
* bits differently. This is again relative to the disk start, not the
* partition start. For 512-byte sectors, the lowest position exceeding
* 24 bit is at 8GB. As usual, we need four sectors more, and fewer, on
* the other side. The partition that we're operating on, must cover
* this area.
*/
u64_t base_pos;
base_pos = (1ULL << 24) * sector_size;
/* The partition end must exceed the 24-bit sector point. */
if (part.base + part.size < base_pos) {
test_group("high LBA positions, part one", FALSE);
return;
}
base_pos -= sector_size * 8;
/* The partition start must not. */
if (base_pos < part.base) {
test_group("high LBA positions, part one", FALSE);
return;
}
test_group("high LBA positions, part one", TRUE);
base_pos -= part.base;
sweep_and_check(base_pos, part.base == 0ULL);
}
static void high_lba_pos2(void)
{
/* Test 48-bit LBA positions, as opposed to *28-bit*. That means sector
* numbers in excess of 28-bit values; the old ATA upper limit. The
* same considerations as above apply, except that we now need a 128+GB
* partition.
*/
u64_t base_pos;
base_pos = (1ULL << 28) * sector_size;
/* The partition end must exceed the 28-bit sector point. */
if (part.base + part.size < base_pos) {
test_group("high LBA positions, part two", FALSE);
return;
}
base_pos -= sector_size * 8;
/* The partition start must not. */
if (base_pos < part.base) {
test_group("high LBA positions, part two", FALSE);
return;
}
test_group("high LBA positions, part two", TRUE);
base_pos -= part.base;
sweep_and_check(base_pos, part.base == 0ULL);
}
static void high_pos(void)
{
/* Check whether the driver deals well with 64-bit positions and
* 48-bit LBA addresses. We test three cases: disk byte position beyond
* what fits in 32 bit, in-partition byte position beyond what fits in
* 32 bit, and disk sector position beyond what fits in 24 bit. With
* the partition we've been given, we may not be able to test all of
* them (or any, for that matter).
*/
/* In certain rare cases, we might be able to perform integrity
* checking on the area that would be affected if a 32-bit/24-bit
* counter were to wrap. More specifically: we can do that if we can
* access the start of the disk. This is why we should be given the
* entire disk as test area if at all possible.
*/
basic_sweep();
high_disk_pos();
high_part_pos();
high_lba_pos1();
high_lba_pos2();
}
static void open_primary(void)
{
/* Open the primary device. This call has its own test group.
*/
test_group("device open", TRUE);
open_device(driver_minor);
}
static void close_primary(void)
{
/* Close the primary device. This call has its own test group.
*/
test_group("device close", TRUE);
close_device(driver_minor);
assert(nr_opened == 0);
}
static void do_tests(void)
{
/* Perform all the tests.
*/
open_primary();
misc_ioctl();
bad_read1();
bad_read2();
/* It is assumed that the driver implementation uses shared
* code paths for read and write for the basic checks, so we do
* not repeat those for writes.
*/
bad_write();
vector_and_large();
part_limits();
unaligned_size();
unaligned_pos1();
unaligned_pos2();
high_pos();
close_primary();
}
static int sef_cb_init_fresh(int UNUSED(type), sef_init_info_t *UNUSED(info))
{
/* Initialize.
*/
if (env_argc > 1)
optset_parse(optset_table, env_argv[1]);
if (driver_label[0] == '\0')
panic("no driver label given");
if (ds_retrieve_label_endpt(driver_label, &driver_endpt))
panic("unable to resolve driver label");
if (driver_minor > 255)
panic("invalid or no driver minor given");
srand48(getticks());
output("BLOCKTEST: driver label '%s' (endpt %d), minor %d\n",
driver_label, driver_endpt, driver_minor);
do_tests();
output("BLOCKTEST: summary: %d out of %d tests failed "
"across %d group%s; %d driver deaths\n",
failed_tests, total_tests, failed_groups,
failed_groups == 1 ? "" : "s", driver_deaths);
/* The returned code will determine the outcome of the RS call, and
* thus the entire test. The actual error code does not matter.
*/
return (failed_tests) ? EINVAL : OK;
}
static void sef_local_startup(void)
{
/* Initialize the SEF framework.
*/
sef_setcb_init_fresh(sef_cb_init_fresh);
sef_startup();
}
int main(int argc, char **argv)
{
/* Driver task.
*/
env_setargs(argc, argv);
sef_local_startup();
return 0;
}
|