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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023-2025 SUSE LLC
// Author: Nicolai Stange <nstange@suse.de>
//! Implementation of [`ReadBuffer`] and the related
//! [`BufferedReadAuthenticateDataFuture`].
extern crate alloc;
use crate::{
blkdev::{self, ChunkedIoRegion, ChunkedIoRegionChunkRange, ChunkedIoRegionError},
fs::{
NvFsError,
cocoonfs::{FormatError, alloc_bitmap, auth_tree, layout},
},
nvblkdev_err_internal, nvfs_err_internal,
utils_async::sync_types::{self, ConstructibleLock as _, Lock as _},
utils_common::{bitmanip::BitManip as _, fixed_vec::FixedVec},
};
use core::{mem, pin, sync::atomic, task};
#[cfg(doc)]
use layout::ImageLayout;
/// Buffered [Allocation Block](ImageLayout::allocation_block_size_128b_log2)
/// update specification.
pub enum ReadBufferAllocationBlockUpdate {
/// The [Allocation Block](ImageLayout::allocation_block_size_128b_log2) is
/// known to be unallocated.
Unallocated,
/// Invalidate any state buffered for the [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2).
Invalidate,
/// Retain what's buffered for the [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2).
Retain,
/// Update the [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2)'s buffered data.
Update {
/// The updated data.
///
/// Must match the size as determined by
/// [`ImageLayout::allocation_block_size_128b_log2`].
data: FixedVec<u8, 7>,
},
}
/// Buffer for some power-of-two size block's individual [Allocation
/// Blocks](ImageLayout::allocation_block_size_128b_log2).
///
/// The [`ReadBuffer`] maintains one [`BlockAllocationBlocksReadBuffer`] for the
/// most recently read [Authentication Tree Data
/// Block](ImageLayout::auth_tree_data_block_allocation_blocks_log2) as well as
/// [Device IO Block](blkdev::NvBlkDev::io_block_size_128b_log2) respectively
/// each.
struct BlockAllocationBlocksReadBuffer {
/// Beginning of the currently buffered block on storage, if any.
buffered_block_allocation_blocks_begin: Option<layout::PhysicalAllocBlockIndex>,
/// Buffered Allocation Blocks.
///
/// - `None` means the Allocation Block has been consumed,
/// - an empty `FixedVec` means the Allocation Block has not been consumed,
/// but is unallocated.
buffered_allocation_blocks: FixedVec<Option<FixedVec<u8, 7>>, 0>,
}
impl BlockAllocationBlocksReadBuffer {
/// Instantiate a new [`BlockAllocationBlocksReadBuffer`].
///
/// # Arguments:
///
/// `block_allocation_blocks_log2` - Base-2 logarithm of the buffered
/// block's size in units of [Allocation
/// Blocks](ImageLayout::allocation_block_size_128b_log2).
fn new(block_allocation_blocks_log2: u32) -> Result<Self, NvFsError> {
if block_allocation_blocks_log2 >= usize::BITS {
return Err(NvFsError::DimensionsNotSupported);
}
let buffered_allocation_blocks = FixedVec::new_with_default(1usize << block_allocation_blocks_log2)?;
Ok(Self {
buffered_block_allocation_blocks_begin: None,
buffered_allocation_blocks,
})
}
/// Take a contiguous sequence of buffered [Allocation
/// Blocks](ImageLayout::allocation_block_size_128b_log2).
///
/// Transfer any of the buffered block's [Allocation
/// Blocks](ImageLayout::allocation_block_size_128b_log2) starting
/// at index `take_begin` to the corresponding entry from
/// `dst_allocation_blocks_bufs`. For any Allocation Block
/// buffered as unallocated, the `FixedVec` from the corresponding
/// `dst_allocation_blocks_bufs` entry will get reset to zero length.
/// Any entry from `dst_allocation_blocks_bufs`, for which no [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) is
/// found in the buffer anymore, will be left unmodified.
///
/// A pair of two `bool`s is returned:
/// * The first entry is set to `true` if and only if all entries from
/// `dst_allocation_blocks_bufs` received an assignment from the buffer.
/// * The second entry is set to `true` if no more entries are remaining in
/// the buffer after the transfer.
///
/// # Arguments:
///
/// * `take_begin` - Index of the first [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) in the buffered
/// block to transfer (if present).
/// * `dst_allocation_blocks_bufs` - Iterator over `&mut FixedVec` items to
/// transfer the respective [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) buffers to.
fn take_buffers<'a, DI: Iterator<Item = &'a mut FixedVec<u8, 7>>>(
&mut self,
take_begin: usize,
dst_allocation_blocks_bufs: DI,
) -> (bool, bool) {
let mut any_missing = false;
// Cast to usize does not overflow, as per the above it is known that
// range_begin is in range.
let mut i = take_begin;
for dst_allocation_block_buf in dst_allocation_blocks_bufs {
if i == self.buffered_allocation_blocks.len() {
any_missing = true;
break;
}
match self.buffered_allocation_blocks[i].take() {
Some(buffered_allocation_block) => {
*dst_allocation_block_buf = buffered_allocation_block;
}
None => {
any_missing = true;
}
};
i += 1;
}
let any_remaining = self.buffered_allocation_blocks[..take_begin]
.iter()
.chain(self.buffered_allocation_blocks[i..].iter())
.any(|buffered_allocation_block| buffered_allocation_block.is_some());
if !any_remaining {
self.buffered_block_allocation_blocks_begin = None;
}
(!any_missing, !any_remaining)
}
/// Clear the buffer.
fn clear(&mut self) {
for buffered_allocation_block in &mut self.buffered_allocation_blocks {
*buffered_allocation_block = None;
}
self.buffered_block_allocation_blocks_begin = None;
}
/// Insert a contiguous sequence of [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) buffers.
///
/// Reset any existing [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) buffer
/// entries and transfer the ones from `src_allocation_blocks_bufs` into the
/// buffer. The first entry from `src_allocation_blocks_bufs`
/// corresponds to the storage location specified by
/// `src_allocation_blocks_begin`.
///
/// If an entry from `src_allocation_blocks_bufs` is `None`, then the
/// [Allocation Block](ImageLayout::allocation_block_size_128b_log2)
/// will get tracked as unavailable. Otherwise, if the `FixedVec` is empty,
/// it get buffered as unallocated. Otherwise the [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) will
/// get buffered with the data from the `FixedVec` taken.
///
/// On success, the location of the buffer's first buffered [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) will get
/// returned, if any.
///
/// # Arguments:
///
/// * `src_allocation_blocks_begin` - Location of the first entry from
/// `src_allocation_blocks_bufs` on storage.
/// * `src_allocation_blocks_bufs` - Iterator over the [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) buffers to
/// insert.
fn insert_buffers<'a, SI: Iterator<Item = Option<&'a mut FixedVec<u8, 7>>>>(
&mut self,
src_allocation_blocks_begin: layout::PhysicalAllocBlockIndex,
src_allocation_blocks_bufs: SI,
) -> Option<layout::PhysicalAllocBlockIndex> {
debug_assert!(self.buffered_allocation_blocks.len().is_pow2());
let buffered_block_allocation_blocks_begin = layout::PhysicalAllocBlockIndex::from(
u64::from(src_allocation_blocks_begin) & !(self.buffered_allocation_blocks.len() as u64 - 1),
);
self.buffered_block_allocation_blocks_begin = Some(buffered_block_allocation_blocks_begin);
// Does not overflow, the subtrahend is the minuend aligned downwards by an
// usize above.
let i = u64::from(src_allocation_blocks_begin - buffered_block_allocation_blocks_begin) as usize;
debug_assert!(i < self.buffered_allocation_blocks.len());
let mut j = i;
let mut any_inserted = false;
for src_allocation_block_buf in src_allocation_blocks_bufs.take(self.buffered_allocation_blocks.len() - i) {
if let Some(src_allocation_block_buf) = src_allocation_block_buf {
any_inserted = true;
self.buffered_allocation_blocks[j] = Some(mem::take(src_allocation_block_buf));
} else {
self.buffered_allocation_blocks[j] = None;
}
j += 1;
if j == self.buffered_allocation_blocks.len() {
break;
}
}
self.buffered_allocation_blocks[..i].fill(None);
self.buffered_allocation_blocks[j..].fill(None);
if !any_inserted {
self.buffered_block_allocation_blocks_begin = None;
}
self.buffered_block_allocation_blocks_begin
}
/// Update buffered [Allocation
/// Blocks](ImageLayout::allocation_block_size_128b_log2).
///
/// Update [Allocation Block](ImageLayout::allocation_block_size_128b_log2)
/// buffer entries as specified by `src_allocation_block_bufs`. The
/// first entry from `src_allocation_blocks_bufs` corresponds to the
/// storage location specified by `src_allocation_blocks_begin`. Existing
/// entries not in range of the `src_allocation_blocks_bufs` will be left
/// unmodified.
///
/// On success, the location of the buffer's first buffered [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) will get returned,
/// if any.
///
/// # Arguments:
///
/// * `src_allocation_blocks_begin` - Location of the first entry from
/// `src_allocation_blocks_bufs` on storage.
/// * `src_allocation_blocks_bufs` - Iterator over the [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) buffer update
/// specifications.
fn update_buffers<SI: Iterator<Item = ReadBufferAllocationBlockUpdate>>(
&mut self,
mut src_allocation_blocks_begin: layout::PhysicalAllocBlockIndex,
mut src_allocation_blocks_bufs: SI,
) -> Option<layout::PhysicalAllocBlockIndex> {
// If no block is currently being buffered, return.
let buffered_block_allocation_blocks_begin = self.buffered_block_allocation_blocks_begin?;
debug_assert!(self.buffered_allocation_blocks.len().is_pow2());
if (u64::from(src_allocation_blocks_begin) ^ u64::from(buffered_block_allocation_blocks_begin))
& !(self.buffered_allocation_blocks.len() as u64 - 1)
!= 0
{
// The src_allocation_blocks_begin and the buffer_allocation_blocks_begin are in
// different blocks.
if src_allocation_blocks_begin > buffered_block_allocation_blocks_begin {
return Some(buffered_block_allocation_blocks_begin);
}
// Advance the source to the beginning of the buffered block.
while src_allocation_blocks_begin != buffered_block_allocation_blocks_begin {
if src_allocation_blocks_bufs.next().is_none() {
return Some(buffered_block_allocation_blocks_begin);
}
src_allocation_blocks_begin += layout::AllocBlockCount::from(1);
}
}
// Does not overflow, the subtrahend is the minuend aligned downwards by an
// usize.
let i = u64::from(src_allocation_blocks_begin - buffered_block_allocation_blocks_begin) as usize;
debug_assert!(i < self.buffered_allocation_blocks.len());
let mut j = i;
let mut any_remaining = false;
for allocation_block_update in src_allocation_blocks_bufs.take(self.buffered_allocation_blocks.len() - i) {
match allocation_block_update {
ReadBufferAllocationBlockUpdate::Unallocated => {
// An empty FixedVec represents an unallocated Allocation Block.
self.buffered_allocation_blocks[j] = Some(FixedVec::new_empty());
any_remaining = true;
}
ReadBufferAllocationBlockUpdate::Invalidate => {
self.buffered_allocation_blocks[j] = None;
}
ReadBufferAllocationBlockUpdate::Retain => {
any_remaining |= self.buffered_allocation_blocks[j].is_some();
}
ReadBufferAllocationBlockUpdate::Update { data } => {
self.buffered_allocation_blocks[j] = Some(data);
any_remaining = true;
}
}
j += 1;
if j == self.buffered_allocation_blocks.len() {
break;
}
}
any_remaining |= self.buffered_allocation_blocks[..i]
.iter()
.any(|buffered_allocation_block| buffered_allocation_block.is_some());
any_remaining |= self.buffered_allocation_blocks[j..]
.iter()
.any(|buffered_allocation_block| buffered_allocation_block.is_some());
if !any_remaining {
self.buffered_block_allocation_blocks_begin = None;
}
self.buffered_block_allocation_blocks_begin
}
}
/// A [`ReadBuffer`]'s buffered data.
struct ReadBufferBufferedData {
/// Unauthenticated data buffered from most recently read [Device IO
/// block](blkdev::NvBlkDev::io_block_size_128b_log2).
min_io_block_buf: BlockAllocationBlocksReadBuffer,
/// Authenticated data buffered from the most recently read and
/// authenticated [Authentication
/// Tree Data Block](ImageLayout::auth_tree_data_block_allocation_blocks_log2).
auth_tree_data_block_buf: BlockAllocationBlocksReadBuffer,
}
impl ReadBufferBufferedData {
/// Instantiate a [`ReadBufferBufferedData`].
///
/// # Arguments:
///
/// * `min_io_block_allocation_blocks_log2` - Base-2 logarithm of the
/// [Device IO Block](blkdev::NvBlkDev::io_block_size_128b_log2) size in
/// units of [Allocation
/// Blocks](ImageLayout::allocation_block_size_128b_log2).
/// * `auth_tree_data_block_allocation_blocks_log2` - Verbatim value of
/// [`ImageLayout::auth_tree_data_block_allocation_blocks_log2`].
fn new(
min_io_block_allocation_blocks_log2: u32,
auth_tree_data_block_allocation_blocks_log2: u32,
) -> Result<Self, NvFsError> {
Ok(Self {
min_io_block_buf: BlockAllocationBlocksReadBuffer::new(min_io_block_allocation_blocks_log2)?,
auth_tree_data_block_buf: BlockAllocationBlocksReadBuffer::new(
auth_tree_data_block_allocation_blocks_log2,
)?,
})
}
}
/// Data read buffer.
///
/// In general, read requests don't align with [Device IO
/// Block](blkdev::NvBlkDev::io_block_size_128b_log2) or [Authentication Tree
/// Data Block](ImageLayout::auth_tree_data_block_allocation_blocks_log2)
/// boundaries, yet reads from physical storage must get processed at a
/// granularity of the former, and authentication at that of the latter.
///
/// In order to exploit spatial locality, a `ReadBuffer` buffers the unused
/// portions from prior reads for potential consumption from subsequent ones.
///
/// More specifically, it maintains a buffer of [Allocation
/// Blocks](ImageLayout::allocation_block_size_128b_log2) authenticated in the
/// course of proecessing a prior read request but not consumed yet.
/// Furthermore, if the [Device IO
/// Block](blkdev::NvBlkDev::io_block_size_128b_log2) happens to exceed the
/// [Authentication Tree
/// Data Block](ImageLayout::auth_tree_data_block_allocation_blocks_log2) size,
/// then it also buffers not yet authenticated [Allocation
/// Blocks](ImageLayout::allocation_block_size_128b_log2) read in as a byproduct
/// in the course of processing a prior read request.
///
/// No data is ever inserted directly into a `ReadBuffer` nor consumed from it,
/// that's all handled transparently through the
/// [`BufferedReadAuthenticateDataFuture`]. Interfaces are provided to supersede
/// or invalidate buffered data at
/// [`Transaction`](super::transaction::Transaction) commit though.
///
/// # See also:
///
/// * [`BufferedReadAuthenticateDataFuture`].
pub struct ReadBuffer<ST: sync_types::SyncTypes> {
/// Verbatim copy of
/// [`ImageLayout::allocation_block_size_128b_log2`].
allocation_block_size_128b_log2: u8,
/// Value of [`NvBlKDev::io_block_size_128b_log2`](blkdev::NvBlkDev::io_block_size_128b_log2) converted to units
/// of [Allocation Blocks](ImageLayout::allocation_block_size_128b_log2).
min_io_block_allocation_blocks_log2: u8,
/// Verbatim copy of
/// [`ImageLayout::auth_tree_data_block_allocation_blocks_log2`].
auth_tree_data_block_allocation_blocks_log2: u8,
/// The buffered data.
buf: ST::Lock<ReadBufferBufferedData>,
/// Copy of the
/// [`buffered_block_allocation_blocks_begin`](BlockAllocationBlocksReadBuffer::buffered_block_allocation_blocks_begin)
/// value from [`buf`](Self::buf)'s
/// [`min_io_block_buf`](ReadBufferBufferedData::min_io_block_buf).
///
/// A value of `None` is mapped to `u64::MAX`.
/// Modified only under the [`Lock`](sync_types::Lock) wrapping
/// [`buf`](Self::buf).
buffered_min_io_block_allocation_blocks_begin: atomic::AtomicU64,
/// Copy of the
/// [`buffered_block_allocation_blocks_begin`](BlockAllocationBlocksReadBuffer::buffered_block_allocation_blocks_begin)
/// value from [`buf`](Self::buf)'s
/// [`auth_tree_data_block_buf`](ReadBufferBufferedData::auth_tree_data_block_buf).
///
/// A value of `None` is mapped to `u64::MAX`.
/// Modified only under the [`Lock`](sync_types::Lock) wrapping
/// [`buf`](Self::buf).
buffered_auth_tree_data_block_allocation_blocks_begin: atomic::AtomicU64,
}
impl<ST: sync_types::SyncTypes> ReadBuffer<ST> {
/// Instantiate a [`ReadBuffer`].
///
/// # Arguments:
///
/// * `image_layout` - The filesystem's [`ImageLayout`].
/// * `blkdev` - The filesystem image backing storage.
pub fn new<B: blkdev::NvBlkDev>(image_layout: &layout::ImageLayout, blkdev: &B) -> Result<Self, NvFsError> {
let allocation_block_size_128b_log2 = image_layout.allocation_block_size_128b_log2;
let blkdev_io_block_size_128b_log2 = blkdev.io_block_size_128b_log2();
let min_io_block_allocation_blocks_log2 =
blkdev_io_block_size_128b_log2.saturating_sub(allocation_block_size_128b_log2 as u32);
let min_io_block_allocation_blocks_log2 =
u8::try_from(min_io_block_allocation_blocks_log2).map_err(|_| nvfs_err_internal!())?;
let auth_tree_data_block_allocation_blocks_log2 = image_layout.auth_tree_data_block_allocation_blocks_log2;
Ok(Self {
allocation_block_size_128b_log2,
min_io_block_allocation_blocks_log2,
auth_tree_data_block_allocation_blocks_log2,
buf: ST::Lock::from(ReadBufferBufferedData::new(
min_io_block_allocation_blocks_log2 as u32,
auth_tree_data_block_allocation_blocks_log2 as u32,
)?),
buffered_min_io_block_allocation_blocks_begin: atomic::AtomicU64::new(u64::MAX),
buffered_auth_tree_data_block_allocation_blocks_begin: atomic::AtomicU64::new(u64::MAX),
})
}
/// Take a contiguous sequence of buffered authenticated [Allocation
/// Blocks](ImageLayout::allocation_block_size_128b_log2), if any.
///
/// Transfer any of the buffered authenticated [Allocation
/// Blocks](ImageLayout::allocation_block_size_128b_log2) to the
/// corresponding entry from `dst_allocation_blocks_bufs`. For any
/// Allocation Block buffered as unallocated, the `FixedVec`
/// from the corresponding `dst_allocation_blocks_bufs` entry will get reset
/// to zero length. Any entry from `dst_allocation_blocks_bufs`, for
/// which no [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) is found in the
/// buffer anymore, will be left unmodified.
///
/// If no [Allocation Block](ImageLayout::allocation_block_size_128b_log2)
/// in the requested `range` was buffered, `None` will get returned.
/// Otherwise, a pair of the subrange of `range` possibly populated with
/// data from the buffer and a bool indicating whether buffered data for
/// all of that subrange was transferred is returned, wrapped in a `Some`.
///
/// # Arguments:
///
/// * `range` - The requested storage range.
/// * `dst_allocation_blocks_bufs` - Iterator over `&mut FixedVec` items to
/// transfer the respective [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) buffers to. Must
/// yield one entry for each Allocation Block in `range`.
fn take_authenticated_buffers<'a, DI: Iterator<Item = &'a mut FixedVec<u8, 7>>>(
&self,
range: &layout::PhysicalAllocBlockRange,
dst_allocation_blocks_bufs: DI,
) -> Option<(layout::PhysicalAllocBlockRange, bool)> {
let auth_tree_data_block_allocation_blocks_log2 = self.auth_tree_data_block_allocation_blocks_log2 as u32;
if !self.auth_tree_blocks_are_buffered()
|| !Self::range_overlaps_block(
range,
layout::PhysicalAllocBlockIndex::from(
self.buffered_auth_tree_data_block_allocation_blocks_begin
.load(atomic::Ordering::Relaxed),
),
auth_tree_data_block_allocation_blocks_log2,
)
{
return None;
}
let mut locked_buf = self.buf.lock();
if let Some((buffered_subrange, take_buffered_allocation_blocks_begin)) = locked_buf
.auth_tree_data_block_buf
.buffered_block_allocation_blocks_begin
.as_ref()
.and_then(|buffered_block_allocation_blocks_begin| {
Self::trim_range_to_block(
range,
*buffered_block_allocation_blocks_begin,
auth_tree_data_block_allocation_blocks_log2,
)
.map(|buffered_subrange| {
(
buffered_subrange,
// Does not overflow, it's been checked above that the complete
// range's allocation block count
// fits an usize.
u64::from(buffered_subrange.begin() - *buffered_block_allocation_blocks_begin) as usize,
)
})
})
{
// Does not overflow, it's been checked above that the complete range's
// allocation block count fits an usize.
let dst_allocation_blocks_bufs_begin = u64::from(buffered_subrange.begin() - range.begin()) as usize;
let dst_allocation_blocks_bufs_end =
dst_allocation_blocks_bufs_begin + u64::from(buffered_subrange.block_count()) as usize;
let (all_in_subrange_found, buf_emptied) = locked_buf.auth_tree_data_block_buf.take_buffers(
take_buffered_allocation_blocks_begin,
dst_allocation_blocks_bufs
.skip(dst_allocation_blocks_bufs_begin)
.take(dst_allocation_blocks_bufs_end - dst_allocation_blocks_bufs_begin),
);
if buf_emptied {
self.buffered_auth_tree_data_block_allocation_blocks_begin
.store(u64::MAX, atomic::Ordering::Relaxed);
}
return Some((buffered_subrange, all_in_subrange_found));
}
None
}
/// Take a contiguous sequence of buffered unauthenticated [Allocation
/// Blocks](ImageLayout::allocation_block_size_128b_log2), if any.
///
/// Transfer any of the buffered unauthenticated [Allocation
/// Blocks](ImageLayout::allocation_block_size_128b_log2) to the
/// corresponding entry from `dst_allocation_blocks_bufs`. For any
/// Allocation Block buffered as unallocated, the `FixedVec`
/// from the corresponding `dst_allocation_blocks_bufs` entry will get reset
/// to zero length. Any entry from `dst_allocation_blocks_bufs`, for
/// which no [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) is found in the
/// buffer anymore, will be left unmodified.
///
/// If no [Allocation Block](ImageLayout::allocation_block_size_128b_log2)
/// in the requested `range` was buffered, `None` will get returned.
/// Otherwise, a pair of the subrange of `range` possibly populated with
/// data from the buffer and a bool indicating whether buffered data for
/// all of that subrange was transferred is returned, wrapped in a `Some`.
///
/// # Arguments:
///
/// * `range` - The requested storage range.
/// * `dst_allocation_blocks_bufs` - Iterator over `&mut FixedVec` items to
/// transfer the respective [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) buffers to. Must
/// yield one entry for each Allocation Block in `range`.
fn take_unauthenticated_buffers<'a, DI: Iterator<Item = &'a mut FixedVec<u8, 7>>>(
&self,
range: &layout::PhysicalAllocBlockRange,
dst_allocation_blocks_bufs: DI,
) -> Option<(layout::PhysicalAllocBlockRange, bool)> {
let min_io_block_allocation_blocks_log2 = self.min_io_block_allocation_blocks_log2 as u32;
if !self.min_io_blocks_are_buffered()
|| !Self::range_overlaps_block(
range,
layout::PhysicalAllocBlockIndex::from(
self.buffered_min_io_block_allocation_blocks_begin
.load(atomic::Ordering::Relaxed),
),
min_io_block_allocation_blocks_log2,
)
{
return None;
}
let mut locked_buf = self.buf.lock();
if let Some((buffered_subrange, take_buffered_allocation_blocks_begin)) = locked_buf
.min_io_block_buf
.buffered_block_allocation_blocks_begin
.as_ref()
.and_then(|buffered_block_allocation_blocks_begin| {
Self::trim_range_to_block(
range,
*buffered_block_allocation_blocks_begin,
min_io_block_allocation_blocks_log2,
)
.map(|buffered_subrange| {
(
buffered_subrange,
// Does not overflow, it's been checked above that the complete
// range's allocation block count
// fits an usize.
u64::from(buffered_subrange.begin() - *buffered_block_allocation_blocks_begin) as usize,
)
})
})
{
// Does not overflow, it's been checked above that the complete range's
// allocation block count fits an usize.
let dst_allocation_blocks_bufs_begin = u64::from(buffered_subrange.begin() - range.begin()) as usize;
let dst_allocation_blocks_bufs_end =
dst_allocation_blocks_bufs_begin + u64::from(buffered_subrange.block_count()) as usize;
let (all_in_subrange_found, buf_emptied) = locked_buf.min_io_block_buf.take_buffers(
take_buffered_allocation_blocks_begin,
dst_allocation_blocks_bufs
.skip(dst_allocation_blocks_bufs_begin)
.take(dst_allocation_blocks_bufs_end - dst_allocation_blocks_bufs_begin),
);
if buf_emptied {
self.buffered_min_io_block_allocation_blocks_begin
.store(u64::MAX, atomic::Ordering::Relaxed);
}
return Some((buffered_subrange, all_in_subrange_found));
}
None
}
/// Insert a contiguous sequence of authenticated [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) buffers.
///
/// Reset any existing authenticated [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) buffer entries and
/// transfer the ones from `src_allocation_blocks_bufs` into the buffer.
/// The first entry from `src_allocation_blocks_bufs` corresponds to the
/// storage location specified by `src_allocation_blocks_begin`.
///
/// If an entry from `src_allocation_blocks_bufs` is `None`, then the
/// [Allocation Block](ImageLayout::allocation_block_size_128b_log2)
/// will get tracked as unavailable. Otherwise, if the `FixedVec` is empty,
/// it get buffered as unallocated. Otherwise the [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) will get buffered
/// with the data from the `FixedVec` taken.
///
/// # Arguments:
///
/// * `src_allocation_blocks_begin` - Location of the first entry from
/// `src_allocation_blocks_bufs` on storage.
/// * `src_allocation_blocks_bufs` - Iterator over the [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) buffers to
/// insert. The buffers must all have been authenticated!
fn insert_authenticated_buffers<'a, SI: Iterator<Item = Option<&'a mut FixedVec<u8, 7>>>>(
&self,
src_allocation_blocks_begin: layout::PhysicalAllocBlockIndex,
src_allocation_blocks_bufs: SI,
) {
if !self.auth_tree_blocks_are_buffered() {
return;
}
let mut locked_buf = self.buf.lock();
self.buffered_auth_tree_data_block_allocation_blocks_begin.store(
locked_buf
.auth_tree_data_block_buf
.insert_buffers(src_allocation_blocks_begin, src_allocation_blocks_bufs)
.map(u64::from)
.unwrap_or(u64::MAX),
atomic::Ordering::Relaxed,
);
}
/// Insert a contiguous sequence of unauthenticated [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) buffers.
///
/// Reset any existing unauthenticated [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) buffer entries and
/// transfer the ones from `src_allocation_blocks_bufs` into the buffer.
/// The first entry from `src_allocation_blocks_bufs` corresponds to the
/// storage location specified by `src_allocation_blocks_begin`.
///
/// If an entry from `src_allocation_blocks_bufs` is `None`, then the
/// [Allocation Block](ImageLayout::allocation_block_size_128b_log2)
/// will get tracked as unavailable. Otherwise, if the `FixedVec` is empty,
/// it get buffered as unallocated. Otherwise the [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) will get buffered
/// with the data from the `FixedVec` taken.
///
/// # Arguments:
///
/// * `src_allocation_blocks_begin` - Location of the first entry from
/// `src_allocation_blocks_bufs` on storage.
/// * `src_allocation_blocks_bufs` - Iterator over the [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) buffers to
/// insert.
fn insert_unauthenticated_buffers<'a, SI: Iterator<Item = Option<&'a mut FixedVec<u8, 7>>>>(
&self,
src_allocation_blocks_begin: layout::PhysicalAllocBlockIndex,
src_allocation_blocks_bufs: SI,
) {
if !self.min_io_blocks_are_buffered() {
return;
}
let mut locked_buf = self.buf.lock();
self.buffered_min_io_block_allocation_blocks_begin.store(
locked_buf
.min_io_block_buf
.insert_buffers(src_allocation_blocks_begin, src_allocation_blocks_bufs)
.map(u64::from)
.unwrap_or(u64::MAX),
atomic::Ordering::Relaxed,
);
}
/// Get the current storage range window spanning all possibly buffered
/// authenticated [Allocation
/// Blocks](ImageLayout::allocation_block_size_128b_log2), if any.
pub fn get_buffered_authenticated_range(&self) -> Option<layout::PhysicalAllocBlockRange> {
if !self.auth_tree_blocks_are_buffered() {
return None;
}
let buffered_auth_tree_data_block_allocation_blocks_begin = self
.buffered_auth_tree_data_block_allocation_blocks_begin
.load(atomic::Ordering::Relaxed);
if buffered_auth_tree_data_block_allocation_blocks_begin == u64::MAX {
return None;
}
let buffered_auth_tree_data_block_allocation_blocks_begin =
layout::PhysicalAllocBlockIndex::from(buffered_auth_tree_data_block_allocation_blocks_begin);
let auth_tree_data_block_allocation_blocks =
layout::AllocBlockCount::from(1u64 << (self.auth_tree_data_block_allocation_blocks_log2 as u32));
Some(layout::PhysicalAllocBlockRange::from((
buffered_auth_tree_data_block_allocation_blocks_begin,
auth_tree_data_block_allocation_blocks,
)))
}
/// Get the current storage range window spanning all possibly buffered
/// unauthenticated [Allocation
/// Blocks](ImageLayout::allocation_block_size_128b_log2), if any.
pub fn get_buffered_unauthenticated_range(&self) -> Option<layout::PhysicalAllocBlockRange> {
if !self.min_io_blocks_are_buffered() {
return None;
}
let buffered_min_io_block_allocation_blocks_begin = self
.buffered_min_io_block_allocation_blocks_begin
.load(atomic::Ordering::Relaxed);
if buffered_min_io_block_allocation_blocks_begin == u64::MAX {
return None;
}
let buffered_min_block_allocation_blocks_begin =
layout::PhysicalAllocBlockIndex::from(buffered_min_io_block_allocation_blocks_begin);
let min_io_block_allocation_blocks =
layout::AllocBlockCount::from(1u64 << (self.min_io_block_allocation_blocks_log2 as u32));
Some(layout::PhysicalAllocBlockRange::from((
buffered_min_block_allocation_blocks_begin,
min_io_block_allocation_blocks,
)))
}
/// Update buffered authenticated [Allocation
/// Blocks](ImageLayout::allocation_block_size_128b_log2).
///
/// Update authenticated [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) buffer
/// entries as specified by `src_allocation_block_bufs`. The first entry
/// from `src_allocation_blocks_bufs` corresponds to the storage
/// location specified by `src_allocation_blocks_begin`. Existing
/// entries not in range of the `src_allocation_blocks_bufs` will be
/// left unmodified.
///
/// # Arguments:
///
/// * `src_allocation_blocks_begin` - Location of the first entry from
/// `src_allocation_blocks_bufs` on storage.
/// * `src_allocation_blocks_bufs` - Iterator over the [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) buffer update
/// specifications. Except for the
/// [`Invalidate`](ReadBufferAllocationBlockUpdate::Invalidate) case, all
/// update entries must be authenticated!
pub fn update_authenticated_buffers<SI: Iterator<Item = ReadBufferAllocationBlockUpdate>>(
&mut self,
src_allocation_blocks_begin: layout::PhysicalAllocBlockIndex,
src_allocation_blocks_bufs: SI,
) {
if !self.auth_tree_blocks_are_buffered() {
return;
}
self.buffered_auth_tree_data_block_allocation_blocks_begin.store(
self.buf
.get_mut()
.auth_tree_data_block_buf
.update_buffers(src_allocation_blocks_begin, src_allocation_blocks_bufs)
.map(u64::from)
.unwrap_or(u64::MAX),
atomic::Ordering::Relaxed,
);
}
/// Update buffered unauthenticated [Allocation
/// Blocks](ImageLayout::allocation_block_size_128b_log2).
///
/// Update unauthenticated [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) buffer entries
/// as specified by `src_allocation_block_bufs`. The first entry from
/// `src_allocation_blocks_bufs` corresponds to the storage location
/// specified by `src_allocation_blocks_begin`. Existing entries not in
/// range of the `src_allocation_blocks_bufs` will be left unmodified.
///
/// # Arguments:
///
/// * `src_allocation_blocks_begin` - Location of the first entry from
/// `src_allocation_blocks_bufs` on storage.
/// * `src_allocation_blocks_bufs` - Iterator over the [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) buffer update
/// specifications.
pub fn update_unauthenticated_buffers<SI: Iterator<Item = ReadBufferAllocationBlockUpdate>>(
&mut self,
src_allocation_blocks_begin: layout::PhysicalAllocBlockIndex,
src_allocation_blocks_bufs: SI,
) {
if !self.min_io_blocks_are_buffered() {
return;
}
self.buffered_min_io_block_allocation_blocks_begin.store(
self.buf
.get_mut()
.min_io_block_buf
.update_buffers(src_allocation_blocks_begin, src_allocation_blocks_bufs)
.map(u64::from)
.unwrap_or(u64::MAX),
atomic::Ordering::Relaxed,
);
}
/// Clear all buffered data.
pub fn clear_caches(&self) {
let mut locked_buf = self.buf.lock();
locked_buf.auth_tree_data_block_buf.clear();
self.buffered_auth_tree_data_block_allocation_blocks_begin
.store(u64::MAX, atomic::Ordering::Relaxed);
locked_buf.min_io_block_buf.clear();
self.buffered_min_io_block_allocation_blocks_begin
.store(u64::MAX, atomic::Ordering::Relaxed);
}
/// Determine whether unauthenticated data from read
/// [Device IO Blocks](blkdev::NvBlkDev::io_block_size_128b_log2) reads is
/// to be buffered.
fn min_io_blocks_are_buffered(&self) -> bool {
self.min_io_block_allocation_blocks_log2 > self.auth_tree_data_block_allocation_blocks_log2
}
/// Determine whether data from authenticated [Authentication Tree Data
/// Blocks](ImageLayout::auth_tree_data_block_allocation_blocks_log2) is to
/// be buffered.
fn auth_tree_blocks_are_buffered(&self) -> bool {
self.auth_tree_data_block_allocation_blocks_log2 != 0
}
/// Test if a given storage range overlaps with some aligned, power-of-two
/// sized block.
///
/// # Arguments:
///
/// * `range` - The extent on storage to test for an overlap with the block.
/// * `block_allocation_blocks_begin` - The block's beginning on storage.
/// Must be aligned to the size as determined by
/// `block_allocation_blocks_log2`.
/// * `block_allocation_blocks_log2` - Base-2 logarithm of the block's size
/// in units of [Allocation
/// Blocks](ImageLayout::allocation_block_size_128b_log2).
fn range_overlaps_block(
range: &layout::PhysicalAllocBlockRange,
block_allocation_blocks_begin: layout::PhysicalAllocBlockIndex,
block_allocation_blocks_log2: u32,
) -> bool {
if range.end() <= block_allocation_blocks_begin {
return false;
}
if range.begin() > block_allocation_blocks_begin
&& (u64::from(range.begin()) ^ u64::from(block_allocation_blocks_begin)) >> block_allocation_blocks_log2
!= 0
{
// range comes after the block's beginning and there's a block boundary crossing
// inbetween.
return false;
}
true
}
/// Trim a given storage range to its overlap with some aligned,
/// power-of-two sized block, if any.
///
/// If the `range` doesn't overlap with the block, return `None`. Otherwise
/// return the overlapping subrange wrapped in a `Some`.
///
/// # Arguments:
///
/// * `range` - The extent on storage to trim to overlap with the block.
/// * `block_allocation_blocks_begin` - The block's beginning on storage.
/// Must be aligned to the size as determined by
/// `block_allocation_blocks_log2`.
/// * `block_allocation_blocks_log2` - Base-2 logarithm of the block's size
/// in units of [Allocation
/// Blocks](ImageLayout::allocation_block_size_128b_log2).
fn trim_range_to_block(
range: &layout::PhysicalAllocBlockRange,
block_allocation_blocks_begin: layout::PhysicalAllocBlockIndex,
block_allocation_blocks_log2: u32,
) -> Option<layout::PhysicalAllocBlockRange> {
if !Self::range_overlaps_block(range, block_allocation_blocks_begin, block_allocation_blocks_log2) {
return None;
}
let trimmed_range_begin = range.begin().max(block_allocation_blocks_begin);
let trimmed_range_end =
if (u64::from(range.end()) ^ u64::from(block_allocation_blocks_begin)) >> block_allocation_blocks_log2 == 0
{
range.end()
} else {
// Cannot overflow, it is known that range.end() comes after or at the block's
// end, which means the latter is representable.
block_allocation_blocks_begin + layout::AllocBlockCount::from(1u64 << block_allocation_blocks_log2)
};
Some(layout::PhysicalAllocBlockRange::new(
trimmed_range_begin,
trimmed_range_end,
))
}
}
/// Read and authenticate committed data through a [`ReadBuffer`].
pub struct BufferedReadAuthenticateDataFuture<B: blkdev::NvBlkDev> {
/// All other data bundled together to allow for independenr borrowing from
/// [`fut_state`](Self::fut_state).
d: BufferedReadAuthenticatedDataFutureData,
fut_state: BufferedReadAuthenticateDataFutureState<B>,
}
/// Internal [`BufferedReadAuthenticateDataFuture`] state.
struct BufferedReadAuthenticatedDataFutureData {
request_range: layout::PhysicalAllocBlockRange,
/// The request region aligned to the [Authentication Tree Data
/// Block](ImageLayout::auth_tree_data_block_allocation_blocks_log2) size.
auth_tree_data_block_aligned_request_range: layout::PhysicalAllocBlockRange,
/// The beginning of the `auth_tree_data_block_aligned_request_range`
/// as represented in the Authentication Tree Data domain.
request_range_auth_tree_data_allocation_blocks_begin: auth_tree::AuthTreeDataAllocBlockIndex,
/// The request range aligned to the larger of a [Device IO
/// Block](blkdev::NvBlkDev::io_block_size_128b_log2)) and an
/// [Authentication Tree Data
/// Block](ImageLayout::auth_tree_data_block_allocation_blocks_log2).
///
/// This is the area of operation.
aligned_request_range: layout::PhysicalAllocBlockRange,
/// Destination buffers for the `request_range`, one for each [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2).
dst_allocation_blocks_bufs: FixedVec<FixedVec<u8, 7>, 0>,
/// Head and tail portions of the scratch Allocation Block buffers needed to
/// fill up the [`request_range`](Self::request_range) to
/// [`aligned_request_range`](Self::aligned_request_range) fused together in
/// a single array.
alignment_scratch_allocation_blocks_bufs: FixedVec<FixedVec<u8, 7>, 0>,
/// Subrange of [`request_range`](Self::request_range) for which
/// authenticated data has been obtained from the [`ReadBuffer`].
authenticated_subrange_from_read_buf: Option<layout::PhysicalAllocBlockRange>,
/// Subrange of
/// [`auth_tree_data_block_aligned_request_range`](Self::auth_tree_data_block_aligned_request_range)
/// for which unauthenticated data has been obtained from the
/// [`ReadBuffer`].
unauthenticated_subrange_from_read_buf: Option<layout::PhysicalAllocBlockRange>,
/// End of the
/// [`auth_tree_data_block_aligned_request_range`](Self::auth_tree_data_block_aligned_request_range)
/// head subrange authenticated so far.
authenticated_allocation_blocks_end: layout::PhysicalAllocBlockIndex,
/// [Device IO Block](blkdev::NvBlkDev::io_block_size_128b_log2) in units of
/// [Allocation Blocks](ImageLayout::allocation_block_size_128b_log2).
min_io_block_allocation_blocks_log2: u8,
/// [Preferred Device IO bulk
/// size](blkdev::NvBlkDev::preferred_io_blocks_bulk_log2) in units of
/// [Allocation Blocks](ImageLayout::allocation_block_size_128b_log2).
preferred_blkdev_io_bulk_allocation_blocks_log2: u8,
/// Verbatim value of
/// [`ImageLayout::auth_tree_data_block_allocation_blocks_log2`].
auth_tree_data_block_allocation_blocks_log2: u8,
/// Verbatim value of [`ImageLayout::allocation_block_size_128b_log2`].
allocation_block_size_128b_log2: u8,
}
impl<B: blkdev::NvBlkDev> BufferedReadAuthenticateDataFuture<B> {
/// Instantiate a [`BufferedReadAuthenticateDataFuture`].
///
/// # Arguments:
///
/// * `request_range` - The data storage range to read and authenticate.
/// Must all be allocated.
/// * `image_layout` - The filesystem's [`ImageLayout`].
/// * `auth_tree_config` - The filesystem's
/// [`AuthTreeConfig`](auth_tree::AuthTreeConfig).
/// * `blkdev` - The filesystem image backing storage.
pub fn new(
request_range: &layout::PhysicalAllocBlockRange,
image_layout: &layout::ImageLayout,
auth_tree_config: &auth_tree::AuthTreeConfig,
blkdev: &B,
) -> Result<Self, NvFsError> {
let request_range_allocation_blocks = match usize::try_from(u64::from(request_range.block_count())) {
Ok(request_range_allocation_blocks) => request_range_allocation_blocks,
Err(_) => return Err(NvFsError::DimensionsNotSupported),
};
let auth_tree_data_block_allocation_blocks_log2 = image_layout.auth_tree_data_block_allocation_blocks_log2;
let allocation_block_size_128b_log2 = image_layout.allocation_block_size_128b_log2;
let blkdev_io_block_size_128b = blkdev.io_block_size_128b_log2();
let preferred_bkdev_io_blocks_bulk_log2 = blkdev.preferred_io_blocks_bulk_log2();
let min_io_block_allocation_blocks_log2 =
u8::try_from(blkdev_io_block_size_128b.saturating_sub(allocation_block_size_128b_log2 as u32))
.map_err(|_| nvfs_err_internal!())?;
// Determine the preferred Device IO request block size in units of Allocation
// Blocks. Possibly ramp it up to some reasonable value to the reduce
// the overall number of IO requests.
let preferred_blkdev_io_bulk_allocation_blocks_log2 = preferred_bkdev_io_blocks_bulk_log2
.saturating_add(blkdev_io_block_size_128b)
.min(usize::BITS - 1 + allocation_block_size_128b_log2 as u32)
.saturating_sub(allocation_block_size_128b_log2 as u32)
.max(auth_tree_data_block_allocation_blocks_log2 as u32)
as u8;
let auth_tree_data_block_aligned_request_range =
match request_range.align(auth_tree_data_block_allocation_blocks_log2 as u32) {
Some(auth_tree_data_block_aligned_request_range) => auth_tree_data_block_aligned_request_range,
None => {
return Err(NvFsError::from(FormatError::BlockOutOfRange));
}
};
let request_range_auth_tree_data_allocation_blocks_begin =
auth_tree::AuthTreeDataAllocBlockIndex::new_from_data_block_index(
auth_tree_config
.translate_physical_to_data_block_index(auth_tree_data_block_aligned_request_range.begin()),
auth_tree_data_block_allocation_blocks_log2 as u32,
);
// The request range aligned to the larger of the Device IO Block and
// Authenication Tree Data Block size, this is the area of operation.
let aligned_request_range =
match auth_tree_data_block_aligned_request_range.align(min_io_block_allocation_blocks_log2 as u32) {
Some(min_io_block_aligned_request_range) => min_io_block_aligned_request_range,
None => {
return Err(NvFsError::from(FormatError::BlockOutOfRange));
}
};
// Check that the total IO range's length in units of Allocation Blocks fits an
// usize, the rest of the code relies on that without conducting any
// further checks.
if usize::try_from(u64::from(aligned_request_range.block_count())).is_err() {
return Err(NvFsError::DimensionsNotSupported);
}
let dst_allocation_blocks_bufs = FixedVec::new_with_default(request_range_allocation_blocks)?;
// Will get allocated lazily if needed.
let alignment_scratch_allocation_blocks_bufs = FixedVec::new_empty();
Ok(Self {
d: BufferedReadAuthenticatedDataFutureData {
request_range: *request_range,
auth_tree_data_block_aligned_request_range,
request_range_auth_tree_data_allocation_blocks_begin,
aligned_request_range,
dst_allocation_blocks_bufs,
alignment_scratch_allocation_blocks_bufs,
authenticated_subrange_from_read_buf: None,
unauthenticated_subrange_from_read_buf: None,
authenticated_allocation_blocks_end: auth_tree_data_block_aligned_request_range.begin(),
min_io_block_allocation_blocks_log2,
preferred_blkdev_io_bulk_allocation_blocks_log2,
auth_tree_data_block_allocation_blocks_log2,
allocation_block_size_128b_log2,
},
fut_state: BufferedReadAuthenticateDataFutureState::Init,
})
}
/// Poll the [`BufferedReadAuthenticateDataFuture`] to completion.
///
/// On successful completion, a `FixedVec` of buffers is being returned, one
/// for each [Allocation
/// Block](ImageLayout::allocation_block_size_128b_log2) in the requested
/// read range.
///
/// # Arguments:
///
/// * `blkdev` - The filesystem image backing storage.
/// * `image_layout` - The filesystem's [`ImageLayout`].
/// * `image_header_end` - [End of the filesystem image header on
/// storage](super::image_header::MutableImageHeader::physical_location).
/// * `fs_sync_state_alloc_bitmap` - The [filesystem instance's allocation
/// bitmap](super::fs::CocoonFsSyncState::alloc_bitmap).
/// * `fs_sync_state_auth_tree` - The [filesystem instance's authentication
/// tree](super::fs::CocoonFsSyncState::auth_tree).
/// * `fs_sync_state_read_buffer` - The [filesystem instance's read
/// buffer](super::fs::CocoonFsSyncState::read_buffer).
/// * `cx` - The context of the asynchronous task on whose behalf the future
/// is being polled.
#[allow(clippy::too_many_arguments)]
pub fn poll<ST: sync_types::SyncTypes>(
self: pin::Pin<&mut Self>,
blkdev: &B,
image_layout: &layout::ImageLayout,
image_header_end: layout::PhysicalAllocBlockIndex,
fs_sync_state_alloc_bitmap: &alloc_bitmap::AllocBitmap,
fs_sync_state_auth_tree: &mut auth_tree::AuthTreeRef<'_, ST>,
fs_sync_state_read_buffer: &ReadBuffer<ST>,
cx: &mut task::Context<'_>,
) -> task::Poll<Result<FixedVec<FixedVec<u8, 7>, 0>, NvFsError>> {
let this = pin::Pin::into_inner(self);
{
debug_assert_eq!(
(
this.d.min_io_block_allocation_blocks_log2,
this.d.auth_tree_data_block_allocation_blocks_log2,
this.d.allocation_block_size_128b_log2
),
(
fs_sync_state_read_buffer.min_io_block_allocation_blocks_log2,
fs_sync_state_read_buffer.auth_tree_data_block_allocation_blocks_log2,
fs_sync_state_read_buffer.allocation_block_size_128b_log2
)
);
}
let min_io_block_allocation_blocks_log2 = this.d.min_io_block_allocation_blocks_log2 as u32;
let auth_tree_data_block_allocation_blocks_log2 = this.d.auth_tree_data_block_allocation_blocks_log2 as u32;
let allocation_block_size_128b_log2 = this.d.allocation_block_size_128b_log2 as u32;
loop {
match &mut this.fut_state {
BufferedReadAuthenticateDataFutureState::Init => {
// First try to obtain anything already authenticated from the read buffer.
if let Some((found_subrange, auth_tree_data_block_complete)) = fs_sync_state_read_buffer
.take_authenticated_buffers(&this.d.request_range, this.d.dst_allocation_blocks_bufs.iter_mut())
{
if !auth_tree_data_block_complete {
// Some authenticated allocation blocks had been found in the read
// buffer, but some from the containing Authentication Tree Data Block
// which fall within the request range had been missing. Partial
// Authentication Tree Data Blocks don't help, because the
// authentication of the containing Authentication Tree Data Block needs
// to be re-done anyway. Clear the range again to simplify matters
// everywhere else.
this.d.dst_allocation_blocks_bufs[u64::from(
found_subrange.begin() - this.d.request_range.begin(),
) as usize
..u64::from(found_subrange.end() - this.d.request_range.begin()) as usize]
.fill(FixedVec::new_empty());
} else {
if found_subrange == this.d.request_range {
// All found authenticated in the read buffer -> done.
this.fut_state = BufferedReadAuthenticateDataFutureState::Done;
return task::Poll::Ready(Ok(mem::take(&mut this.d.dst_allocation_blocks_bufs)));
}
this.d.authenticated_subrange_from_read_buf = Some(found_subrange);
}
}
// Now it is known that the alignment scratch buffers will probably be
// needed. Allocate them (not the buffers themselves yet, but the FixedVec
// holding the buffer FixedVecs).
this.d.allocate_alignment_scratch_allocation_blocks_buf()?;
// Try to obtain unauthenticated data overlapping with the request range from
// the read buffer. Note that in practice this is relevant only
// - if the Device IO Block size is larger than an Authentication Tree Data
// Block and
// - only for the range's head and tail regions, as it is (almost) impossible to
// find a complete Device IO block still in the read buffer -- in the common
// case, parts of it would have been consumed already at insertion time.
// So try to gather any unauthenticated data for the
// auth_tree_data_block_aligned_request_range, which in general is not aligned
// to the Device IO Block size.
let (
head_auth_tree_data_block_alignment_scratch_allocation_blocks_bufs,
tail_auth_tree_data_block_alignment_scratch_allocation_blocks_bufs,
) = BufferedReadAuthenticatedDataFutureData
::get_auth_tree_data_block_alignment_scratch_allocation_blocks_bufs(
&mut this.d.alignment_scratch_allocation_blocks_bufs,
&this.d.request_range,
&this.d.auth_tree_data_block_aligned_request_range,
&this.d.aligned_request_range,
);
if let Some((found_subrange, min_io_block_complete)) = fs_sync_state_read_buffer
.take_unauthenticated_buffers(
&this.d.auth_tree_data_block_aligned_request_range,
head_auth_tree_data_block_alignment_scratch_allocation_blocks_bufs
.iter_mut()
.chain(this.d.dst_allocation_blocks_bufs.iter_mut())
.chain(tail_auth_tree_data_block_alignment_scratch_allocation_blocks_bufs),
)
{
// Some unauthenticated Allocation Blocks from a certain Device IO Block
// overlapping with the request range had been found in the read buffer.
// Note that this implies that the Minimum IO Block size is larger than the
// Authentication Tree Data Block size, because otherwise unauthenticated
// allocation blocks would not have been kept in the read buffer.
if !min_io_block_complete {
// There are some Allocation Blocks missing in some of the containing
// Minimum IO Block's Authentication Tree Data Blocks overlapping with
// the request_range. That means that the containing Minimum IO Block
// needs to get re-read anyway, and what has just been obtained from the
// read buffer will be of no value. Clear that out again in order to
// simplify the logic.
this.d
.alignment_scratch_allocation_blocks_bufs
.fill(FixedVec::new_empty());
this.d.dst_allocation_blocks_bufs.fill(FixedVec::new_empty());
} else {
this.d.unauthenticated_subrange_from_read_buf = Some(found_subrange);
}
// In the highly unlikely event that the unauthenticated buffers just
// retrieved alias with any authenticated ones obtained above, invalidate
// the authentication status -- the authenticated buffers would have been
// overwritten with unauthenticated ones then.
if this
.d
.authenticated_subrange_from_read_buf
.map(|authenticated_subrange_from_read_buf| {
authenticated_subrange_from_read_buf.overlaps_with(&found_subrange)
})
.unwrap_or(false)
{
this.d.authenticated_subrange_from_read_buf = None;
}
if this
.d
.unauthenticated_subrange_from_read_buf
.as_ref()
.map(|unauthenticated_subrange_from_read_buf| {
unauthenticated_subrange_from_read_buf
== &this.d.auth_tree_data_block_aligned_request_range
})
.unwrap_or(false)
{
// All the data is there, proceed to the authentication.
this.fut_state = BufferedReadAuthenticateDataFutureState::AuthenticateSubrange {
auth_subrange_fut_state: BufferedReadAuthenticateDataFutureAuthenticateState::Init,
};
continue;
}
}
// Finally allocate Allocation Block destination buffers for anything not
// obtained from the read buffer.
let (head_alignment_scratch_allocation_blocks_bufs, tail_alignment_scratch_allocation_blocks_bufs) =
BufferedReadAuthenticatedDataFutureData::get_alignment_scratch_allocation_blocks_bufs(
&mut this.d.alignment_scratch_allocation_blocks_bufs,
&this.d.request_range,
&this.d.aligned_request_range,
);
let ((unused_head_alignment_scratch_allocation_blocks_bufs,
used_head_alignment_scratch_allocation_blocks_bufs),
(used_tail_alignment_scratch_allocation_blocks_bufs,
_unused_tail_alignment_scratch_allocation_blocks_bufs)) =
BufferedReadAuthenticatedDataFutureData
::split_off_unused_alignment_scratch_allocation_blocks_bufs(
head_alignment_scratch_allocation_blocks_bufs,
tail_alignment_scratch_allocation_blocks_bufs,
&this.d.request_range,
&this.d.auth_tree_data_block_aligned_request_range,
this.d.authenticated_subrange_from_read_buf.as_ref(),
this.d.unauthenticated_subrange_from_read_buf.as_ref(),
min_io_block_allocation_blocks_log2
);
let mut cur_allocation_block_index = this.d.aligned_request_range.begin()
+ layout::AllocBlockCount::from(
unused_head_alignment_scratch_allocation_blocks_bufs.len() as u64
);
debug_assert!(
cur_allocation_block_index == this.d.aligned_request_range.begin()
|| cur_allocation_block_index == this.d.request_range.begin()
);
debug_assert!(cur_allocation_block_index < this.d.auth_tree_data_block_aligned_request_range.end());
let allocation_block_size = 1usize << (image_layout.allocation_block_size_128b_log2 as u32 + 7);
let empty_sparse_alloc_bitmap = alloc_bitmap::SparseAllocBitmapUnion::new(&[]);
let mut alloc_bitmap_iter = fs_sync_state_alloc_bitmap.iter_at_allocation_block(
&empty_sparse_alloc_bitmap,
&empty_sparse_alloc_bitmap,
cur_allocation_block_index,
);
for allocation_block_buf in used_head_alignment_scratch_allocation_blocks_bufs
.iter_mut()
.chain(this.d.dst_allocation_blocks_bufs.iter_mut())
.chain(used_tail_alignment_scratch_allocation_blocks_bufs.iter_mut())
{
debug_assert!(cur_allocation_block_index < this.d.aligned_request_range.end());
let allocation_block_is_allocated = alloc_bitmap_iter.next().unwrap_or(false);
// All Allocation Blocks in the requested range are assumed to be allocated.
debug_assert!(
cur_allocation_block_index < this.d.request_range.begin()
|| cur_allocation_block_index >= this.d.request_range.end()
|| allocation_block_is_allocated
);
if allocation_block_is_allocated && allocation_block_buf.is_empty() {
*allocation_block_buf = match FixedVec::new_with_default(allocation_block_size) {
Ok(allocation_block_buf) => allocation_block_buf,
Err(e) => {
this.fut_state = BufferedReadAuthenticateDataFutureState::Done;
return task::Poll::Ready(Err(NvFsError::from(e)));
}
};
}
cur_allocation_block_index += layout::AllocBlockCount::from(1u64);
}
this.fut_state = BufferedReadAuthenticateDataFutureState::PrepareNextSubrangeDataRead {
read_subrange_allocation_blocks_begin: this.d.authenticated_allocation_blocks_end,
};
}
BufferedReadAuthenticateDataFutureState::PrepareNextSubrangeDataRead {
read_subrange_allocation_blocks_begin,
} => {
let auth_tree_config = fs_sync_state_auth_tree.get_config();
let read_range = match this
.d
.determine_next_read_region(*read_subrange_allocation_blocks_begin, auth_tree_config)
{
Ok(next_read_range) => next_read_range,
Err(e) => {
this.fut_state = BufferedReadAuthenticateDataFutureState::Done;
return task::Poll::Ready(Err(e));
}
};
// If a batch worth authenticating has been read to this point,
// determine_next_read_region() from above returns None, otherwise the next
// physical region to read in.
let read_range = match read_range {
Some(read_range) => read_range,
None => {
// Everything needed for authenticating a batch of data is there.
this.fut_state = BufferedReadAuthenticateDataFutureState::AuthenticateSubrange {
auth_subrange_fut_state: BufferedReadAuthenticateDataFutureAuthenticateState::Init,
};
continue;
}
};
let read_request_io_region = ChunkedIoRegion::new(
u64::from(read_range.begin()) << allocation_block_size_128b_log2,
u64::from(read_range.end()) << allocation_block_size_128b_log2,
allocation_block_size_128b_log2,
)
.map_err(|e| match e {
ChunkedIoRegionError::ChunkSizeOverflow | ChunkedIoRegionError::ChunkIndexOverflow => {
NvFsError::DimensionsNotSupported
}
ChunkedIoRegionError::InvalidBounds | ChunkedIoRegionError::RegionUnaligned => {
nvfs_err_internal!()
}
})?;
// The read request assumes ownership of all IO buffers for the duration it's
// pending. hey'll get returned upon completion.
let read_request = BufferedReadAuthenticateDataFutureNvBlkDevReadRequest {
aligned_request_range_allocation_blocks_begin: this.d.aligned_request_range.begin(),
dst_allocation_blocks_bufs: mem::take(&mut this.d.dst_allocation_blocks_bufs),
alignment_scratch_allocation_blocks_bufs: mem::take(
&mut this.d.alignment_scratch_allocation_blocks_bufs,
),
head_alignment_scratch_allocation_blocks: u64::from(
this.d.request_range.begin() - this.d.aligned_request_range.begin(),
) as usize,
authenticated_subrange_from_read_buf: this.d.authenticated_subrange_from_read_buf,
read_request_allocation_blocks_begin: read_range.begin(),
read_request_io_region,
};
let read_fut = match blkdev.read(read_request).and_then(|r| r.map_err(|(_, e)| e)) {
Ok(read_fut) => read_fut,
Err(e) => {
this.fut_state = BufferedReadAuthenticateDataFutureState::Done;
return task::Poll::Ready(Err(NvFsError::from(e)));
}
};
this.fut_state = BufferedReadAuthenticateDataFutureState::ReadSubrangeData { read_range, read_fut };
}
BufferedReadAuthenticateDataFutureState::ReadSubrangeData { read_range, read_fut } => {
match blkdev::NvBlkDevFuture::poll(pin::Pin::new(read_fut), blkdev, cx) {
task::Poll::Pending => return task::Poll::Pending,
task::Poll::Ready(Ok((mut completed_read_request, Ok(())))) => {
// Return IO buffer ownership back from the request.
this.d.dst_allocation_blocks_bufs =
mem::take(&mut completed_read_request.dst_allocation_blocks_bufs);
this.d.alignment_scratch_allocation_blocks_bufs =
mem::take(&mut completed_read_request.alignment_scratch_allocation_blocks_bufs);
this.fut_state = BufferedReadAuthenticateDataFutureState::PrepareNextSubrangeDataRead {
read_subrange_allocation_blocks_begin: read_range.end(),
};
}
task::Poll::Ready(Ok((_, Err(e))) | Err(e)) => {
this.fut_state = BufferedReadAuthenticateDataFutureState::Done;
return task::Poll::Ready(Err(NvFsError::from(e)));
}
}
}
BufferedReadAuthenticateDataFutureState::AuthenticateSubrange {
auth_subrange_fut_state,
} => {
let auth_tree_config = fs_sync_state_auth_tree.get_config();
let auth_tree_covered_data_blocks_per_leaf_node_log2 =
auth_tree_config.covered_data_blocks_per_leaf_node_log2() as u32;
let auth_tree_covered_allocation_blocks_per_leaf_node_log2 =
auth_tree_covered_data_blocks_per_leaf_node_log2 + auth_tree_data_block_allocation_blocks_log2;
match auth_subrange_fut_state {
BufferedReadAuthenticateDataFutureAuthenticateState::Init => {
debug_assert!(this.d.authenticated_allocation_blocks_end < this.d.request_range.end());
if let Some(authenticated_subrange_from_read_buf) =
this.d.authenticated_subrange_from_read_buf.as_ref()
{
// An authenticated subrange initially obtained from the read buffer
// aligning to the left boundary of the request range is not
// necessarily aligned to the Authentication Tree Data Block size,
// check for this case explictly.
if this.d.authenticated_allocation_blocks_end
== authenticated_subrange_from_read_buf.begin()
|| (this.d.authenticated_allocation_blocks_end
== this.d.auth_tree_data_block_aligned_request_range.begin()
&& authenticated_subrange_from_read_buf.begin() == this.d.request_range.begin())
{
this.d.authenticated_allocation_blocks_end =
authenticated_subrange_from_read_buf.end();
if this.d.authenticated_allocation_blocks_end >= this.d.request_range.end() {
this.fut_state = BufferedReadAuthenticateDataFutureState::Finish;
continue;
} else if (u64::from(
this.d.translate_physical_to_auth_tree_data_allocation_block_index(
authenticated_subrange_from_read_buf.begin(),
),
) ^ u64::from(
this.d.translate_physical_to_auth_tree_data_allocation_block_index(
authenticated_subrange_from_read_buf.end(),
),
)) >> auth_tree_covered_allocation_blocks_per_leaf_node_log2
!= 0
{
// Exhausted the region covered by a single Authentication
// Tree Leaf Node, read the next one, if any.
this.fut_state =
BufferedReadAuthenticateDataFutureState::PrepareNextSubrangeDataRead {
read_subrange_allocation_blocks_begin: this
.d
.authenticated_allocation_blocks_end,
};
continue;
}
}
}
debug_assert_eq!(
this.d
.authenticated_allocation_blocks_end
.align_down(auth_tree_data_block_allocation_blocks_log2),
this.d.authenticated_allocation_blocks_end
);
if this.d.authenticated_allocation_blocks_end
== this.d.auth_tree_data_block_aligned_request_range.end()
{
this.fut_state = BufferedReadAuthenticateDataFutureState::Finish;
continue;
}
let auth_tree_data_block_index = auth_tree_config
.translate_physical_to_data_block_index(this.d.authenticated_allocation_blocks_end);
let auth_tree_leaf_node_id =
auth_tree_config.covering_leaf_node_id(auth_tree_data_block_index);
let auth_tree_leaf_node_load_fut =
auth_tree::AuthTreeNodeLoadFuture::new(auth_tree_leaf_node_id);
*auth_subrange_fut_state =
BufferedReadAuthenticateDataFutureAuthenticateState::LoadAuthTreeLeafNode {
auth_tree_data_block_index,
auth_tree_leaf_node_load_fut,
};
}
BufferedReadAuthenticateDataFutureAuthenticateState::LoadAuthTreeLeafNode {
auth_tree_data_block_index,
auth_tree_leaf_node_load_fut,
} => {
let (auth_tree_config, auth_tree_root_hmac_digest, mut auth_tree_node_cache) =
fs_sync_state_auth_tree.destructure_borrow();
let leaf_node = match auth_tree::AuthTreeNodeLoadFuture::poll(
pin::Pin::new(auth_tree_leaf_node_load_fut),
blkdev,
auth_tree_config,
auth_tree_root_hmac_digest,
&mut auth_tree_node_cache,
cx,
) {
task::Poll::Ready(Ok(leaf_node)) => leaf_node,
task::Poll::Ready(Err(e)) => {
this.fut_state = BufferedReadAuthenticateDataFutureState::Done;
return task::Poll::Ready(Err(e));
}
task::Poll::Pending => return task::Poll::Pending,
};
// We've got the Authentication Tree Leaf node and all data covered by
// it has been read in by now, as per the logic of
// determine_next_read_region() and the PrepareNextSubrangeDataRead
// state handling. Authenticate it. Note that the request region is
// contiguous on physical storage, hence not interspersed by the
// Authenication Tree Nodes themselves and thus, the Authentication Tree
// Data Block indices, i.e. those obtained from he
// AuthTreeConfig::translate_physical_data_block(), corresponding to the
// request region are contiguous as well.
loop {
debug_assert!({
let cur_auth_tree_data_block_index = auth_tree_config
.translate_physical_to_data_block_index(
this.d.authenticated_allocation_blocks_end,
);
let leaf_node_id = leaf_node.get_node_id();
*auth_tree_data_block_index == cur_auth_tree_data_block_index
&& cur_auth_tree_data_block_index >= leaf_node_id.first_covered_data_block()
&& cur_auth_tree_data_block_index <= leaf_node_id.last_covered_data_block()
});
let (
head_alignment_scratch_allocation_blocks,
tail_alignment_scratch_allocation_blocks_bufs,
) = BufferedReadAuthenticatedDataFutureData
::get_auth_tree_data_block_alignment_scratch_allocation_blocks_bufs(
&mut this.d.alignment_scratch_allocation_blocks_bufs,
&this.d.request_range,
&this.d.auth_tree_data_block_aligned_request_range,
&this.d.aligned_request_range,
);
// Prepare an iterator over the current Authentication Tree Data
// Block's individual Allocation Blocks' buffers. No usize overflows
// possible here, the whole aligned_range in units of Allocation
// Blocks fits an usize.
let cur_auth_tree_data_block_allocation_blocks_iter =
head_alignment_scratch_allocation_blocks
.iter()
.chain(this.d.dst_allocation_blocks_bufs.iter())
.chain(tail_alignment_scratch_allocation_blocks_bufs.iter())
.skip(u64::from(
this.d.authenticated_allocation_blocks_end
- this.d.auth_tree_data_block_aligned_request_range.begin(),
) as usize)
.take(1usize << auth_tree_data_block_allocation_blocks_log2)
.map(|allocation_block_buf| {
Ok((!allocation_block_buf.is_empty())
.then_some(allocation_block_buf.as_slice()))
});
if let Err(e) = auth_tree_config.authenticate_data_block_from_tree(
&leaf_node,
*auth_tree_data_block_index,
cur_auth_tree_data_block_allocation_blocks_iter,
image_header_end,
) {
this.fut_state = BufferedReadAuthenticateDataFutureState::Done;
return task::Poll::Ready(Err(e));
}
// Advance the current position.
debug_assert_eq!(
this.d
.authenticated_allocation_blocks_end
.align_down(auth_tree_data_block_allocation_blocks_log2),
this.d.authenticated_allocation_blocks_end
);
this.d.authenticated_allocation_blocks_end +=
layout::AllocBlockCount::from(1u64 << auth_tree_data_block_allocation_blocks_log2);
*auth_tree_data_block_index += auth_tree::AuthTreeDataBlockCount::from(1);
// Skip over the authenticated region at point initially obtained from the
// read buffer, if any.
if let Some(authenticated_subrange_from_read_buf) =
this.d.authenticated_subrange_from_read_buf.as_ref()
{
if this.d.authenticated_allocation_blocks_end
== authenticated_subrange_from_read_buf.begin()
{
this.d.authenticated_allocation_blocks_end =
authenticated_subrange_from_read_buf.end();
// If the request is not complete yet,
// authenticated_allocation_blocks_end is still aligned to
// a Authentication Tree Data Block boundary.
debug_assert!(
this.d.authenticated_allocation_blocks_end >= this.d.request_range.end()
|| this
.d
.authenticated_allocation_blocks_end
.align_down(auth_tree_data_block_allocation_blocks_log2)
== this.d.authenticated_allocation_blocks_end
);
// Advance the auth_tree_data_block_index position needed
// for authentication accordingly. The authenticated range
// initially obtained from the read buffer spans one
// Autehntication Tree Data Block at most.
debug_assert!(
u64::from(
authenticated_subrange_from_read_buf.end()
- authenticated_subrange_from_read_buf.begin()
) >> auth_tree_data_block_allocation_blocks_log2
<= 1
);
*auth_tree_data_block_index += auth_tree::AuthTreeDataBlockCount::from(1);
}
}
if this.d.authenticated_allocation_blocks_end >= this.d.request_range.end() {
break;
}
let leaf_node_id = leaf_node.get_node_id();
if *auth_tree_data_block_index < leaf_node_id.first_covered_data_block()
|| *auth_tree_data_block_index > leaf_node_id.last_covered_data_block()
{
// Exhausted the region covered by a single Authentication
// Tree leaf node. Stop authenticating for now and proceeed
// with reading data for the next one.
break;
}
}
if this.d.authenticated_allocation_blocks_end >= this.d.request_range.end() {
this.fut_state = BufferedReadAuthenticateDataFutureState::Finish;
} else {
this.fut_state = BufferedReadAuthenticateDataFutureState::PrepareNextSubrangeDataRead {
read_subrange_allocation_blocks_begin: this.d.authenticated_allocation_blocks_end,
};
}
}
}
}
BufferedReadAuthenticateDataFutureState::Finish => {
// All done. Insert anything from the alignment scratch buffers, i.e. everything
// not needed for fulfilling the request_range, into the read buffer for
// consumption in future requests.
// First do any Allocation Buffers not in the original request range
// authenticated as a by-product.
let (
authenticated_head_alignment_scratch_allocation_blocks_bufs,
authenticated_tail_alignment_scratch_allocation_blocks_bufs,
) = BufferedReadAuthenticatedDataFutureData
::get_auth_tree_data_block_alignment_scratch_allocation_blocks_bufs(
&mut this.d.alignment_scratch_allocation_blocks_bufs,
&this.d.request_range,
&this.d.auth_tree_data_block_aligned_request_range,
&this.d.aligned_request_range,
);
// Careful, careful, the alignment scratch buffers at either end might not have
// been authenticated if an authenticated region aligning with the original left
// or right end had initially been obtained from the read buffer.
let (head_is_authenticated, tail_is_authenticated) = this
.d
.authenticated_subrange_from_read_buf
.as_ref()
.map(|authenticated_subrange_from_read_buf| {
(
authenticated_subrange_from_read_buf.begin() != this.d.request_range.begin(),
authenticated_subrange_from_read_buf.end() != this.d.request_range.end(),
)
})
.unwrap_or((true, true));
let mut authenticated_head_alignment_scratch_allocation_blocks_bufs_taken = false;
let mut authenticated_tail_alignment_scratch_allocation_blocks_bufs_taken = false;
if (!authenticated_head_alignment_scratch_allocation_blocks_bufs.is_empty()
&& head_is_authenticated)
|| (!authenticated_tail_alignment_scratch_allocation_blocks_bufs.is_empty()
&& tail_is_authenticated)
{
if u64::from(
this.d.auth_tree_data_block_aligned_request_range.end()
- this.d.auth_tree_data_block_aligned_request_range.begin(),
) >> auth_tree_data_block_allocation_blocks_log2
== 1
{
// The head and tail are contained in the same Authentication Tree Data
// Block, insert them together at once.
fs_sync_state_read_buffer.insert_authenticated_buffers(
this.d.auth_tree_data_block_aligned_request_range.begin(),
(authenticated_head_alignment_scratch_allocation_blocks_bufs
.iter_mut()
.map(|allocation_block_buf| head_is_authenticated.then_some(allocation_block_buf)))
.chain(this.d.dst_allocation_blocks_bufs.iter().map(|_| None))
.chain(
authenticated_tail_alignment_scratch_allocation_blocks_bufs
.iter_mut()
.map(|allocation_block_buf| {
tail_is_authenticated.then_some(allocation_block_buf)
}),
),
);
authenticated_head_alignment_scratch_allocation_blocks_bufs_taken |= head_is_authenticated;
authenticated_tail_alignment_scratch_allocation_blocks_bufs_taken |= tail_is_authenticated;
} else if !authenticated_tail_alignment_scratch_allocation_blocks_bufs.is_empty()
&& tail_is_authenticated
{
fs_sync_state_read_buffer.insert_authenticated_buffers(
this.d.request_range.end(),
authenticated_tail_alignment_scratch_allocation_blocks_bufs
.iter_mut()
.map(Some),
);
authenticated_head_alignment_scratch_allocation_blocks_bufs_taken = true;
} else if !authenticated_head_alignment_scratch_allocation_blocks_bufs.is_empty()
&& head_is_authenticated
{
fs_sync_state_read_buffer.insert_authenticated_buffers(
this.d.auth_tree_data_block_aligned_request_range.begin(),
authenticated_head_alignment_scratch_allocation_blocks_bufs
.iter_mut()
.map(Some),
);
authenticated_tail_alignment_scratch_allocation_blocks_bufs_taken = true;
}
}
// And insert the remaining alignment scratch Allocation Blocks read but not
// authenticated into the read buffer. Don't bother inserting if all are
// unallocated.
if fs_sync_state_read_buffer.min_io_blocks_are_buffered() {
let (
head_alignment_scratch_allocation_blocks_bufs,
tail_alignment_scratch_allocation_blocks_bufs,
) = BufferedReadAuthenticatedDataFutureData::get_alignment_scratch_allocation_blocks_bufs(
&mut this.d.alignment_scratch_allocation_blocks_bufs,
&this.d.request_range,
&this.d.aligned_request_range,
);
let (
(unused_head_alignment_scratch_allocation_blocks_bufs,
used_head_alignment_scratch_allocation_blocks_bufs),
(used_tail_alignment_scratch_allocation_blocks_bufs,
_unused_tail_alignment_scratch_allocation_blocks_bufs)
) = BufferedReadAuthenticatedDataFutureData
::split_off_unused_alignment_scratch_allocation_blocks_bufs(
head_alignment_scratch_allocation_blocks_bufs,
tail_alignment_scratch_allocation_blocks_bufs,
&this.d.request_range,
&this.d.auth_tree_data_block_aligned_request_range,
this.d.authenticated_subrange_from_read_buf.as_ref(),
this.d.unauthenticated_subrange_from_read_buf.as_ref(),
min_io_block_allocation_blocks_log2
);
let any_allocated_at_head = used_head_alignment_scratch_allocation_blocks_bufs
.iter()
.any(|allocation_block_buf| !allocation_block_buf.is_empty());
let any_allocated_at_tail = used_tail_alignment_scratch_allocation_blocks_bufs
.iter()
.any(|allocation_block_buf| !allocation_block_buf.is_empty());
if any_allocated_at_head || any_allocated_at_tail {
// If any of the authenticated Allocation Block buffers had been taken
// and inserted above, they are empty buffers now. Be careful not to
// insert those as such, but as None, because otherwise the entry in the
// read buffer might errorneously be mistaken as to representing an
// unallocated Allocation Block in the future.
let (
remaining_head_alignment_scratch_allocation_blocks_bufs,
taken_head_alignment_scratch_allocation_blocks_bufs,
) = if authenticated_head_alignment_scratch_allocation_blocks_bufs_taken {
used_head_alignment_scratch_allocation_blocks_bufs.split_at_mut(u64::from(
this.d.auth_tree_data_block_aligned_request_range.begin()
- this.d.aligned_request_range.begin(),
)
as usize)
} else {
used_head_alignment_scratch_allocation_blocks_bufs
.split_at_mut(used_head_alignment_scratch_allocation_blocks_bufs.len())
};
let (
taken_tail_alignment_scratch_allocation_blocks_bufs,
remaining_tail_alignment_scratch_allocation_blocks_bufs,
) = if authenticated_tail_alignment_scratch_allocation_blocks_bufs_taken {
used_tail_alignment_scratch_allocation_blocks_bufs.split_at_mut(u64::from(
this.d.auth_tree_data_block_aligned_request_range.end()
- this.d.request_range.end(),
)
as usize)
} else {
used_tail_alignment_scratch_allocation_blocks_bufs.split_at_mut(0)
};
if u64::from(this.d.aligned_request_range.end() - this.d.aligned_request_range.begin())
>> min_io_block_allocation_blocks_log2
== 1
{
// The head and tail are contained in the same Authentication Tree Data
// Block, insert them together at once.
fs_sync_state_read_buffer.insert_unauthenticated_buffers(
this.d.aligned_request_range.begin(),
(unused_head_alignment_scratch_allocation_blocks_bufs
.iter()
.map(|_| None))
.chain(
remaining_head_alignment_scratch_allocation_blocks_bufs
.iter_mut()
.map(Some),
)
.chain(taken_head_alignment_scratch_allocation_blocks_bufs.iter().map(|_| None))
.chain(this.d.dst_allocation_blocks_bufs.iter().map(|_| None))
.chain(taken_tail_alignment_scratch_allocation_blocks_bufs.iter().map(|_| None))
.chain(
remaining_tail_alignment_scratch_allocation_blocks_bufs
.iter_mut()
.map(Some),
),
);
} else if any_allocated_at_tail {
fs_sync_state_read_buffer.insert_unauthenticated_buffers(
this.d.request_range.end(),
taken_tail_alignment_scratch_allocation_blocks_bufs
.iter()
.map(|_| None)
.chain(
remaining_tail_alignment_scratch_allocation_blocks_bufs
.iter_mut()
.map(Some),
),
);
} else if any_allocated_at_head {
fs_sync_state_read_buffer.insert_authenticated_buffers(
this.d.aligned_request_range.begin(),
(unused_head_alignment_scratch_allocation_blocks_bufs
.iter()
.map(|_| None))
.chain(
remaining_head_alignment_scratch_allocation_blocks_bufs
.iter_mut()
.map(Some),
),
);
}
}
}
let dst_allocation_blocks_bufs = mem::take(&mut this.d.dst_allocation_blocks_bufs);
this.fut_state = BufferedReadAuthenticateDataFutureState::Done;
return task::Poll::Ready(Ok(dst_allocation_blocks_bufs));
}
BufferedReadAuthenticateDataFutureState::Done => unreachable!(),
}
}
}
}
impl BufferedReadAuthenticatedDataFutureData {
/// Convenience wrapper to
/// [`_translate_physical_to_auth_tree_data_allocation_block_index()`](Self::_translate_physical_to_auth_tree_data_allocation_block_index).
///
/// Translate the `request_range_allocation_block_index`
/// [`PhysicalAllocBlockIndex`](layout::PhysicalAllocBlockIndex)
/// within the (aligned) request range into the
/// [`AuthTreeDataAllocBlockIndex`](auth_tree::AuthTreeDataAllocBlockIndex)
/// domain.
///
/// # Arguments:
///
/// * `request_range_allocation_block_index` - The
/// [`PhysicalAllocBlockIndex`](layout::PhysicalAllocBlockIndex) to
/// translate. Must be within the [Authentication Tree Data
/// Block](ImageLayout::auth_tree_data_block_allocation_blocks_log2)
/// aligned request range.
fn translate_physical_to_auth_tree_data_allocation_block_index(
&self,
request_range_allocation_block_index: layout::PhysicalAllocBlockIndex,
) -> auth_tree::AuthTreeDataAllocBlockIndex {
Self::_translate_physical_to_auth_tree_data_allocation_block_index(
request_range_allocation_block_index,
self.request_range_auth_tree_data_allocation_blocks_begin,
&self.auth_tree_data_block_aligned_request_range,
)
}
/// Translate a [`PhysicalAllocBlockIndex`](layout::PhysicalAllocBlockIndex)
/// within the (aligned) request range into the
/// [`AuthTreeDataAllocBlockIndex`](auth_tree::AuthTreeDataAllocBlockIndex)
/// domain.
///
/// # Arguments:
///
/// * `request_range_allocation_block_index` - The
/// [`PhysicalAllocBlockIndex`](layout::PhysicalAllocBlockIndex) to
/// translate. Must be within the bounds of
/// `auth_tree_data_block_aligned_request_range`.
/// * `request_range_auth_tree_data_allocation_blocks_begin` - The
/// [`AuthTreeDataAllocBlockIndex`](auth_tree::AuthTreeDataAllocBlockIndex)
/// corresponding to the beginning of the
/// `auth_tree_data_block_aligned_request_range`.
/// * `auth_tree_data_block_aligned_request_range` - The [Authentication
/// Tree Data Block](ImageLayout::auth_tree_data_block_allocation_blocks_log2)
/// aligned request range.
fn _translate_physical_to_auth_tree_data_allocation_block_index(
request_range_allocation_block_index: layout::PhysicalAllocBlockIndex,
request_range_auth_tree_data_allocation_blocks_begin: auth_tree::AuthTreeDataAllocBlockIndex,
auth_tree_data_block_aligned_request_range: &layout::PhysicalAllocBlockRange,
) -> auth_tree::AuthTreeDataAllocBlockIndex {
// The request extent is contiguous on physical storage, hence not interspersed
// with extents from the Authentication Tree and the mapping from
// physical to the Authentication Tree Data domain is linear.
debug_assert!(
request_range_allocation_block_index >= auth_tree_data_block_aligned_request_range.begin()
&& request_range_allocation_block_index <= auth_tree_data_block_aligned_request_range.end()
);
request_range_auth_tree_data_allocation_blocks_begin
+ (request_range_allocation_block_index - auth_tree_data_block_aligned_request_range.begin())
}
/// Allocate the [`Self::alignment_scratch_allocation_blocks_bufs`].
fn allocate_alignment_scratch_allocation_blocks_buf(&mut self) -> Result<(), NvFsError> {
debug_assert!(
self.aligned_request_range
.contains(&self.auth_tree_data_block_aligned_request_range)
);
// Does not overflow, it's been checked in Self::new() that the whole
// aligned_request_range's Allocation Block Count fits an usize.
let head_alignment_scratch_allocation_blocks =
u64::from(self.request_range.begin() - self.aligned_request_range.begin()) as usize;
let tail_alignment_scratch_allocation_blocks =
u64::from(self.aligned_request_range.end() - self.request_range.end()) as usize;
let alignment_scratch_allocation_blocks =
head_alignment_scratch_allocation_blocks + tail_alignment_scratch_allocation_blocks;
self.alignment_scratch_allocation_blocks_bufs =
FixedVec::new_with_default(alignment_scratch_allocation_blocks)?;
Ok(())
}
/// Get the alignment padding scratch buffers.
///
/// Return the head and tail scratch buffers corresponding to the
/// head and tail alignment padding from the
/// [`aligned_request_range`](Self::aligned_request_range).
///
/// # Arguments:
///
/// * `alignment_scratch_allocation_blocks_bufs` - `mut` reference to
/// [`Self::alignment_scratch_allocation_blocks_bufs`].
/// * `request_range` - Reference to [`Self::request_range`].
/// * `aligned_request_range` - Reference to
/// [`Self::aligned_request_range`].
fn get_alignment_scratch_allocation_blocks_bufs<'a>(
alignment_scratch_allocation_blocks_bufs: &'a mut [FixedVec<u8, 7>],
request_range: &layout::PhysicalAllocBlockRange,
aligned_request_range: &layout::PhysicalAllocBlockRange,
) -> (&'a mut [FixedVec<u8, 7>], &'a mut [FixedVec<u8, 7>]) {
// The aligned_request_range is the request_range aligned to the larger of the
// Device IO Block and the Authentication Tree Data Block size. Return the
// head and tail scratch buffer portions needed for aligning to the
// larger of the two.
// Note that the complete aligned_request_range length in units of Allocation
// Blocks is guaranteed to fit an usize.
debug_assert!(aligned_request_range.contains(request_range));
let head_alignment_scratch_allocation_blocks =
u64::from(request_range.begin() - aligned_request_range.begin()) as usize;
let tail_alignment_scratch_allocation_blocks =
u64::from(aligned_request_range.end() - request_range.end()) as usize;
debug_assert_eq!(
head_alignment_scratch_allocation_blocks + tail_alignment_scratch_allocation_blocks,
alignment_scratch_allocation_blocks_bufs.len()
);
let (head_alignment_scratch_allocation_blocks_bufs, tail_alignment_scratch_allocation_blocks_bufs) =
alignment_scratch_allocation_blocks_bufs.split_at_mut(head_alignment_scratch_allocation_blocks);
(
head_alignment_scratch_allocation_blocks_bufs,
tail_alignment_scratch_allocation_blocks_bufs,
)
}
/// Split off the unused parts of the alignment padding scratch buffers.
///
/// In some specific constellations of data obtained from the [`ReadBuffer`]
/// it is known that certain parts of the alignment padding scratch
/// buffers wouldn't ever get accessed. Split these parts off.
/// More specifically, return a quadruplet of buffers,
/// with the outer entries corresponding to the unused, and the inner two
/// entries to the used parts of the head and tail padding scratch
/// buffers respectively.
///
/// # Arguments:
/// * `head_alignment_scratch_allocation_blocks_bufs` - Head part obtained
/// from [`get_alignment_scratch_allocation_blocks_bufs()`](Self::get_alignment_scratch_allocation_blocks_bufs).
/// * `tail_alignment_scratch_allocation_blocks_bufs` - Tail part obtained
/// from [`get_alignment_scratch_allocation_blocks_bufs()`](Self::get_alignment_scratch_allocation_blocks_bufs).
/// * `request_range` - Reference to [`Self::request_range`].
/// * `auth_tree_data_block_aligned_request_range` - Reference to
/// [`Self::auth_tree_data_block_aligned_request_range`].
/// * `authenticated_subrange_from_read_buf` - Reference to
/// [`Self::authenticated_subrange_from_read_buf`].
/// * `unauthenticated_subrange_from_read_buf` - Reference to
/// [`Self::unauthenticated_subrange_from_read_buf`].
/// * `min_io_block_allocation_blocks_log2` - Value of
/// [`Self::min_io_block_allocation_blocks_log2`].
#[allow(clippy::type_complexity)]
fn split_off_unused_alignment_scratch_allocation_blocks_bufs<'a>(
head_alignment_scratch_allocation_blocks_bufs: &'a mut [FixedVec<u8, 7>],
tail_alignment_scratch_allocation_blocks_bufs: &'a mut [FixedVec<u8, 7>],
request_range: &layout::PhysicalAllocBlockRange,
auth_tree_data_block_aligned_request_range: &layout::PhysicalAllocBlockRange,
authenticated_subrange_from_read_buf: Option<&layout::PhysicalAllocBlockRange>,
unauthenticated_subrange_from_read_buf: Option<&layout::PhysicalAllocBlockRange>,
min_io_block_allocation_blocks_log2: u32,
) -> (
(&'a mut [FixedVec<u8, 7>], &'a mut [FixedVec<u8, 7>]),
(&'a mut [FixedVec<u8, 7>], &'a mut [FixedVec<u8, 7>]),
) {
debug_assert!(
authenticated_subrange_from_read_buf
.map(|authenticated_subrange_from_read_buf| authenticated_subrange_from_read_buf != request_range)
.unwrap_or(true)
);
// If some unauthenticated data had been obtained from the read buffer and that
// range aligns to the left or right of the
// auth_tree_data_block_aligned_request_range, then the
// alignment scratch buffers won't be needed for that end -- note that
// unauthenticated data is buffered only if the Minimum IO block size is
// > the Authentication Tree Data Block size, so an unauthenticated
// region at the head or tail, if any, always extends up to some
// boundary which is both, Minimum IO Block as well as Authentication
// Tree Data Block aligned within the original request region.
let (head_scratch_is_unused, tail_scratch_is_unused) = unauthenticated_subrange_from_read_buf
.map(|unauthenticated_subrange_from_read_buf| {
debug_assert!(
authenticated_subrange_from_read_buf
.as_ref()
.map(|authenticated_subrange_from_read_buf| {
!authenticated_subrange_from_read_buf.overlaps_with(unauthenticated_subrange_from_read_buf)
})
.unwrap_or(true)
);
(
unauthenticated_subrange_from_read_buf.begin()
== auth_tree_data_block_aligned_request_range.begin(),
unauthenticated_subrange_from_read_buf.end() == auth_tree_data_block_aligned_request_range.end(),
)
})
.unwrap_or((false, false));
// Likewise, if some authenticated data had been obtained from the read buffer
// and that authenticated range aligns to either the beginning or the
// end of the request range, and that range's boundary within the
// interior of the request range happens to be aligned to the Device IO
// Block size, then the alignment scratch buffers for that end will not
// be neeeded either.
let (head_scratch_is_unused, tail_scratch_is_unused) = authenticated_subrange_from_read_buf
.map(|authenticated_subrange_from_read_buf| {
(
head_scratch_is_unused
|| (authenticated_subrange_from_read_buf.begin() == request_range.begin()
&& authenticated_subrange_from_read_buf
.end()
.align_down(min_io_block_allocation_blocks_log2)
== authenticated_subrange_from_read_buf.end()),
tail_scratch_is_unused
|| (authenticated_subrange_from_read_buf.end() == request_range.end()
&& authenticated_subrange_from_read_buf
.begin()
.align_down(min_io_block_allocation_blocks_log2)
== authenticated_subrange_from_read_buf.begin()),
)
})
.unwrap_or((head_scratch_is_unused, tail_scratch_is_unused));
let head_alignment_scratch_allocation_blocks_bufs_len = head_alignment_scratch_allocation_blocks_bufs.len();
let tail_alignment_scratch_allocation_blocks_bufs_len = tail_alignment_scratch_allocation_blocks_bufs.len();
(
head_alignment_scratch_allocation_blocks_bufs.split_at_mut(if head_scratch_is_unused {
head_alignment_scratch_allocation_blocks_bufs_len
} else {
0
}),
tail_alignment_scratch_allocation_blocks_bufs.split_at_mut(if tail_scratch_is_unused {
0
} else {
tail_alignment_scratch_allocation_blocks_bufs_len
}),
)
}
/// Get the [Authentication Tree Data
/// Block](ImageLayout::auth_tree_data_block_allocation_blocks_log2)
/// alignment padding scratch buffers.
///
/// Return the head and tail scratch buffers corresponding to the
/// head and tail alignment padding from the
/// [`auth_tree_data_block_aligned_request_range`](Self::auth_tree_data_block_aligned_request_range).
///
/// # Arguments:
///
/// * `alignment_scratch_allocation_blocks_bufs` - `mut` reference to
/// [`Self::alignment_scratch_allocation_blocks_bufs`].
/// * `request_range` - Reference to [`Self::request_range`].
/// * `auth_tree_data_block_aligned_request_range` - Reference to
/// [`Self::auth_tree_data_block_aligned_request_range`].
/// * `aligned_request_range` - Reference to
/// [`Self::aligned_request_range`].
fn get_auth_tree_data_block_alignment_scratch_allocation_blocks_bufs<'a>(
alignment_scratch_allocation_blocks_bufs: &'a mut [FixedVec<u8, 7>],
request_range: &layout::PhysicalAllocBlockRange,
auth_tree_data_block_aligned_request_range: &layout::PhysicalAllocBlockRange,
aligned_request_range: &layout::PhysicalAllocBlockRange,
) -> (&'a mut [FixedVec<u8, 7>], &'a mut [FixedVec<u8, 7>]) {
// The aligned_request_range is the request_range aligned to the larger of the
// Device IO Block and the Authentication Tree Data Block size. Obtain
// only the head and tail portions needed to align the request_range to
// the Authentication Tree Data Block size.
// Note that the complete aligned_request_range length in units of Allocation
// Blocks is guaranteed to fit an usize.
debug_assert!(aligned_request_range.contains(auth_tree_data_block_aligned_request_range));
debug_assert!(auth_tree_data_block_aligned_request_range.contains(request_range));
let (head_alignment_scratch_allocation_blocks_bufs, tail_alignment_scratch_allocation_blocks_bufs) =
Self::get_alignment_scratch_allocation_blocks_bufs(
alignment_scratch_allocation_blocks_bufs,
request_range,
aligned_request_range,
);
let (_, head_auth_tree_data_block_alignment_scratch_allocation_blocks_bufs) =
head_alignment_scratch_allocation_blocks_bufs.split_at_mut(u64::from(
auth_tree_data_block_aligned_request_range.begin() - aligned_request_range.begin(),
) as usize);
let (tail_auth_tree_data_block_alignment_scratch_allocation_blocks_bufs, _) =
tail_alignment_scratch_allocation_blocks_bufs.split_at_mut(u64::from(
auth_tree_data_block_aligned_request_range.end() - request_range.end(),
) as usize);
(
head_auth_tree_data_block_alignment_scratch_allocation_blocks_bufs,
tail_auth_tree_data_block_alignment_scratch_allocation_blocks_bufs,
)
}
/// Determine the next region to read from physical storage.
///
/// Depending on the current progress, return the next region to read in
/// from storage. If `None` is returned, the caller is supposed to
/// authenticate the currently accumulated batch read in so far but not
/// authenticated yet and invoke `determine_next_read_region()` again
/// afterwards in case the the request hasn't been completed by then.
/// Otherwise the caller will proceed to read the returned range from
/// storage and invoke `determine_next_read_region()` again.
///
/// # Arguments:
///
/// * `cur_request_allocation_block_index` - Current read position in the
/// [`Self::aligned_request_range`].
/// * `auth_tree_config` - The filesystem's
/// [`AuthTreeConfig`](auth_tree::AuthTreeConfig).
fn determine_next_read_region(
&mut self,
mut cur_request_allocation_block_index: layout::PhysicalAllocBlockIndex,
auth_tree_config: &auth_tree::AuthTreeConfig,
) -> Result<Option<layout::PhysicalAllocBlockRange>, NvFsError> {
debug_assert!(cur_request_allocation_block_index >= self.authenticated_allocation_blocks_end);
debug_assert!(self.authenticated_allocation_blocks_end < self.auth_tree_data_block_aligned_request_range.end());
if cur_request_allocation_block_index >= self.auth_tree_data_block_aligned_request_range.end() {
// All data has been read by now. Let the caller proceed with authenticating
// that and be done.
return Ok(None);
}
debug_assert!(
cur_request_allocation_block_index >= self.auth_tree_data_block_aligned_request_range.begin()
&& cur_request_allocation_block_index < self.auth_tree_data_block_aligned_request_range.end()
);
let min_io_block_allocation_blocks_log2 = self.min_io_block_allocation_blocks_log2 as u32;
let preferred_blkdev_io_bulk_allocation_blocks_log2 =
self.preferred_blkdev_io_bulk_allocation_blocks_log2 as u32;
let auth_tree_data_block_allocation_blocks_log2 = self.auth_tree_data_block_allocation_blocks_log2 as u32;
let auth_tree_covered_data_blocks_per_leaf_node_log2 =
auth_tree_config.covered_data_blocks_per_leaf_node_log2() as u32;
let auth_tree_covered_allocation_blocks_per_leaf_node_log2 =
auth_tree_covered_data_blocks_per_leaf_node_log2 + self.auth_tree_data_block_allocation_blocks_log2 as u32;
if (u64::from(
self.translate_physical_to_auth_tree_data_allocation_block_index(cur_request_allocation_block_index),
) ^ u64::from(
self.translate_physical_to_auth_tree_data_allocation_block_index(self.authenticated_allocation_blocks_end),
)) >> auth_tree_covered_allocation_blocks_per_leaf_node_log2
!= 0
{
// The current read position would advance past the region of what's covered by
// a single Authentication Tree Leaf node. Return and let the caller
// authenticate what's there.
return Ok(None);
}
// Start by skipping over regions which
// - correspond to fully unallocated Device IO Blocks, possible only if the
// Device IO Block size is less than that of an Authentication Tree Data
// Block,
// - over the head alignment scratch if unneeded (because an authenticated area
// whose end aligns with Device IO Block size had been retrieved from the read
// buffer),
// - over any authenticated or unauthenticated regions initially retrieved from
// the read buffer.
let (head_alignment_scratch_allocation_blocks_bufs, tail_alignment_scratch_allocation_blocks_bufs) =
Self::get_alignment_scratch_allocation_blocks_bufs(
&mut self.alignment_scratch_allocation_blocks_bufs,
&self.request_range,
&self.aligned_request_range,
);
let allocation_blocks_bufs = head_alignment_scratch_allocation_blocks_bufs
.iter()
.chain(self.dst_allocation_blocks_bufs.iter())
.chain(tail_alignment_scratch_allocation_blocks_bufs.iter());
// Advance the allocation_blocks_bufs iterator to the position corresponding to
// cur_request_allocation_block_index. The usize does not overflow, the whole
// aligned_request_range's length in units of Allocation Blocks fits an usize,
// as per the check in Self::new(). Unfortunately,
// Iterator::advance_by() is unstable.
let mut allocation_blocks_bufs = allocation_blocks_bufs
.skip(u64::from(cur_request_allocation_block_index - self.aligned_request_range.begin()) as usize);
while cur_request_allocation_block_index != self.auth_tree_data_block_aligned_request_range.end() {
// Clippy does not undestand the allocation_blocks_bufs iterator cannot get
// cosumed here.
#[allow(clippy::while_let_on_iterator)]
while let Some(allocation_block_buf) = allocation_blocks_bufs.next() {
if !allocation_block_buf.is_empty() {
break;
} else {
cur_request_allocation_block_index += layout::AllocBlockCount::from(1);
if cur_request_allocation_block_index == self.auth_tree_data_block_aligned_request_range.end() {
break;
}
}
}
if let Some(authenticated_subrange_from_read_buf) = self.authenticated_subrange_from_read_buf.as_ref() {
if cur_request_allocation_block_index >= authenticated_subrange_from_read_buf.begin()
&& cur_request_allocation_block_index < authenticated_subrange_from_read_buf.end()
{
// If at the request end, or at the end of what's covered by a single
// Authentication Tree Leaf node, return and let the caller proceed with
// authenticating what has been read so far.
if authenticated_subrange_from_read_buf.end() == self.request_range.end()
|| (u64::from(Self::_translate_physical_to_auth_tree_data_allocation_block_index(
cur_request_allocation_block_index,
self.request_range_auth_tree_data_allocation_blocks_begin,
&self.auth_tree_data_block_aligned_request_range,
)) ^ u64::from(Self::_translate_physical_to_auth_tree_data_allocation_block_index(
authenticated_subrange_from_read_buf.end(),
self.request_range_auth_tree_data_allocation_blocks_begin,
&self.auth_tree_data_block_aligned_request_range,
))) >> auth_tree_covered_allocation_blocks_per_leaf_node_log2
!= 0
{
debug_assert!(
authenticated_subrange_from_read_buf.end() == self.request_range.end()
|| u64::from(cur_request_allocation_block_index)
.is_aligned_pow2(auth_tree_data_block_allocation_blocks_log2)
);
return Ok(None);
}
// Otherwise advance the iterator over the found region.
// Iterator::advance_by() is unstable.
// The allocation_blocks_buf has already been iterated past the element pointed
// to by current cur_request_allocation_block_index.
cur_request_allocation_block_index += layout::AllocBlockCount::from(1);
while cur_request_allocation_block_index != authenticated_subrange_from_read_buf.end() {
allocation_blocks_bufs.next().ok_or_else(|| nvfs_err_internal!())?;
cur_request_allocation_block_index += layout::AllocBlockCount::from(1);
}
continue;
}
} else if let Some(unauthenticated_subrange_from_read_buf) =
self.unauthenticated_subrange_from_read_buf.as_ref()
{
// The read buffer retains unauthenticated data only if the Device IO Block size
// exceeds that of an Authentication Tree Data Block.
debug_assert!(min_io_block_allocation_blocks_log2 > auth_tree_data_block_allocation_blocks_log2);
debug_assert!(
unauthenticated_subrange_from_read_buf.begin()
>= self.auth_tree_data_block_aligned_request_range.begin()
&& unauthenticated_subrange_from_read_buf.end()
<= self.auth_tree_data_block_aligned_request_range.end()
);
debug_assert!(
unauthenticated_subrange_from_read_buf.begin()
== self.auth_tree_data_block_aligned_request_range.begin()
|| unauthenticated_subrange_from_read_buf
.begin()
.align_down(min_io_block_allocation_blocks_log2)
== unauthenticated_subrange_from_read_buf.begin()
);
debug_assert!(
unauthenticated_subrange_from_read_buf.end()
== self.auth_tree_data_block_aligned_request_range.end()
|| unauthenticated_subrange_from_read_buf
.end()
.align_down(min_io_block_allocation_blocks_log2)
== unauthenticated_subrange_from_read_buf.end()
);
if cur_request_allocation_block_index >= unauthenticated_subrange_from_read_buf.begin()
&& cur_request_allocation_block_index < unauthenticated_subrange_from_read_buf.end()
{
if (u64::from(Self::_translate_physical_to_auth_tree_data_allocation_block_index(
cur_request_allocation_block_index,
self.request_range_auth_tree_data_allocation_blocks_begin,
&self.auth_tree_data_block_aligned_request_range,
)) ^ u64::from(Self::_translate_physical_to_auth_tree_data_allocation_block_index(
unauthenticated_subrange_from_read_buf.end(),
self.request_range_auth_tree_data_allocation_blocks_begin,
&self.auth_tree_data_block_aligned_request_range,
))) >> auth_tree_covered_allocation_blocks_per_leaf_node_log2
!= 0
|| unauthenticated_subrange_from_read_buf.end()
== self.auth_tree_data_block_aligned_request_range.end()
{
// Either crossing the boundary of an Authentication Tree Data Block's
// covered range or everything needed for authenticating the original
// request range is available, return and let the caller proceed with
// authenticating what's there.
return Ok(None);
}
// Otherwise advance the iterator over the found region.
// Iterator::advance_by() is unstable.
// The allocation_blocks_buf has already been iterated past the element pointed
// to by current cur_request_allocation_block_index.
cur_request_allocation_block_index += layout::AllocBlockCount::from(1);
while cur_request_allocation_block_index != unauthenticated_subrange_from_read_buf.end() {
allocation_blocks_bufs.next().ok_or_else(|| nvfs_err_internal!())?;
cur_request_allocation_block_index += layout::AllocBlockCount::from(1);
}
debug_assert_eq!(
cur_request_allocation_block_index.align_down(min_io_block_allocation_blocks_log2),
cur_request_allocation_block_index
);
continue;
}
}
break;
}
debug_assert!(cur_request_allocation_block_index <= self.auth_tree_data_block_aligned_request_range.end());
if cur_request_allocation_block_index == self.auth_tree_data_block_aligned_request_range.end() {
debug_assert!(allocation_blocks_bufs.all(|allocation_block_buf| allocation_block_buf.is_empty()));
return Ok(None);
}
debug_assert_eq!(
(u64::from(Self::_translate_physical_to_auth_tree_data_allocation_block_index(
cur_request_allocation_block_index,
self.request_range_auth_tree_data_allocation_blocks_begin,
&self.auth_tree_data_block_aligned_request_range,
)) ^ u64::from(Self::_translate_physical_to_auth_tree_data_allocation_block_index(
self.authenticated_allocation_blocks_end,
self.request_range_auth_tree_data_allocation_blocks_begin,
&self.auth_tree_data_block_aligned_request_range,
))) >> auth_tree_covered_allocation_blocks_per_leaf_node_log2,
0
);
// It is known that the Device IO block cur_request_allocation_block_index
// points into needs a read by now.
let read_range_allocation_blocks_begin =
cur_request_allocation_block_index.align_down(min_io_block_allocation_blocks_log2);
// Be extra cautious to never ever invalidate any authentication status by
// re-reading from storage.
if self.authenticated_allocation_blocks_end != self.auth_tree_data_block_aligned_request_range.begin()
&& self.authenticated_allocation_blocks_end > read_range_allocation_blocks_begin
{
return Err(nvfs_err_internal!());
}
debug_assert!(
self.unauthenticated_subrange_from_read_buf
.as_ref()
.map(
|unauthenticated_subrange_from_read_buf| read_range_allocation_blocks_begin
< unauthenticated_subrange_from_read_buf.begin()
|| read_range_allocation_blocks_begin >= unauthenticated_subrange_from_read_buf.end()
)
.unwrap_or(true)
);
// Search for the read range's end. Advance the current position to past the
// first Device IO Block.
cur_request_allocation_block_index = read_range_allocation_blocks_begin
+ layout::AllocBlockCount::from(1u64 << (min_io_block_allocation_blocks_log2));
debug_assert!(cur_request_allocation_block_index <= self.aligned_request_range.end());
debug_assert!(
self.unauthenticated_subrange_from_read_buf
.as_ref()
.map(
|unauthenticated_subrange_from_read_buf| cur_request_allocation_block_index
<= unauthenticated_subrange_from_read_buf.begin()
|| cur_request_allocation_block_index > unauthenticated_subrange_from_read_buf.end()
)
.unwrap_or(true)
);
if cur_request_allocation_block_index == self.aligned_request_range.end() {
return Ok(Some(layout::PhysicalAllocBlockRange::new(
read_range_allocation_blocks_begin,
cur_request_allocation_block_index,
)));
}
let mut read_range_allocation_blocks_end = cur_request_allocation_block_index;
// Reset the allocation_blocks_bufs iterator to the current position.
let allocation_blocks_bufs = head_alignment_scratch_allocation_blocks_bufs
.iter()
.chain(self.dst_allocation_blocks_bufs.iter())
.chain(tail_alignment_scratch_allocation_blocks_bufs.iter());
// Advance the allocation_blocks_bufs iterator to the position corresponding to
// cur_request_allocation_block_index. The usize does not overflow, the whole
// aligned_request_range's length in units of Allocation Blocks fits an usize,
// as per the check in Self::new(). Unfortunately,
// Iterator::advance_by() is unstable.
let mut allocation_blocks_bufs = allocation_blocks_bufs
.skip(u64::from(cur_request_allocation_block_index - self.aligned_request_range.begin()) as usize);
while cur_request_allocation_block_index != self.aligned_request_range.end() {
if (u64::from(Self::_translate_physical_to_auth_tree_data_allocation_block_index(
read_range_allocation_blocks_end,
self.request_range_auth_tree_data_allocation_blocks_begin,
&self.auth_tree_data_block_aligned_request_range,
)) ^ u64::from(Self::_translate_physical_to_auth_tree_data_allocation_block_index(
self.authenticated_allocation_blocks_end,
self.request_range_auth_tree_data_allocation_blocks_begin,
&self.auth_tree_data_block_aligned_request_range,
))) >> auth_tree_covered_allocation_blocks_per_leaf_node_log2
!= 0
|| (u64::from(read_range_allocation_blocks_end) ^ u64::from(read_range_allocation_blocks_begin))
>> preferred_blkdev_io_bulk_allocation_blocks_log2
!= 0
{
// Don't cross preferred bulk IO block boundaries or leave a region covered by a
// single Authentication Tree Leaf Block. Stop and let the caller process what's
// been found so far.
break;
}
if let Some(unauthenticated_subrange_from_read_buf) = self.unauthenticated_subrange_from_read_buf.as_ref() {
if cur_request_allocation_block_index == unauthenticated_subrange_from_read_buf.begin() {
// Data had been initially retrieved from the read buffer, don't re-read it
// then. Let the caller read what's missing up to the current position.
debug_assert_eq!(
cur_request_allocation_block_index.align_down(min_io_block_allocation_blocks_log2),
cur_request_allocation_block_index
);
return Ok(Some(layout::PhysicalAllocBlockRange::new(
read_range_allocation_blocks_begin,
read_range_allocation_blocks_end,
)));
}
debug_assert!(
cur_request_allocation_block_index < unauthenticated_subrange_from_read_buf.begin()
|| cur_request_allocation_block_index >= unauthenticated_subrange_from_read_buf.end()
)
}
let cur_min_io_block_allocation_blocks_end = cur_request_allocation_block_index
+ layout::AllocBlockCount::from(1u64 << min_io_block_allocation_blocks_log2);
while cur_request_allocation_block_index != cur_min_io_block_allocation_blocks_end {
if let Some(authenticated_subrange_from_read_buf) = self.authenticated_subrange_from_read_buf.as_ref() {
if authenticated_subrange_from_read_buf.begin() == cur_request_allocation_block_index {
if authenticated_subrange_from_read_buf.end() >= cur_min_io_block_allocation_blocks_end {
break;
}
while cur_request_allocation_block_index != authenticated_subrange_from_read_buf.end() {
// Iterator::advance_by() is unstable.
allocation_blocks_bufs.next().ok_or_else(|| nvfs_err_internal!())?;
cur_request_allocation_block_index += layout::AllocBlockCount::from(1);
}
continue;
}
debug_assert!(
cur_request_allocation_block_index < authenticated_subrange_from_read_buf.begin()
|| cur_request_allocation_block_index >= authenticated_subrange_from_read_buf.end()
);
}
let allocation_block_buf = allocation_blocks_bufs.next().ok_or_else(|| nvfs_err_internal!())?;
if !allocation_block_buf.is_empty() {
// The Allocation Block destination buffer is not empty, meaning the Allocation
// Block is allocated and needs a read.
read_range_allocation_blocks_end = cur_min_io_block_allocation_blocks_end;
// Advance the allocation_blocks_bufs iterator to past the current Device IO
// block. One entry has already been popped right above.
cur_request_allocation_block_index += layout::AllocBlockCount::from(1);
// Iterator::advance_by() is unstable.
while cur_request_allocation_block_index != cur_min_io_block_allocation_blocks_end {
allocation_blocks_bufs.next().ok_or_else(|| nvfs_err_internal!())?;
cur_request_allocation_block_index += layout::AllocBlockCount::from(1);
}
break;
}
cur_request_allocation_block_index += layout::AllocBlockCount::from(1);
}
if read_range_allocation_blocks_end != cur_min_io_block_allocation_blocks_end {
// The current Device IO block doesn't need a read, stop and let the caller read
// what's missing up the current point.
break;
}
}
Ok(Some(layout::PhysicalAllocBlockRange::new(
read_range_allocation_blocks_begin,
read_range_allocation_blocks_end,
)))
}
}
/// [`BufferedReadAuthenticateDataFuture`] state-machine state.
enum BufferedReadAuthenticateDataFutureState<B: blkdev::NvBlkDev> {
Init,
PrepareNextSubrangeDataRead {
read_subrange_allocation_blocks_begin: layout::PhysicalAllocBlockIndex,
},
ReadSubrangeData {
read_range: layout::PhysicalAllocBlockRange,
read_fut: B::ReadFuture<BufferedReadAuthenticateDataFutureNvBlkDevReadRequest>,
},
AuthenticateSubrange {
auth_subrange_fut_state: BufferedReadAuthenticateDataFutureAuthenticateState<B>,
},
Finish,
Done,
}
/// [`BufferedReadAuthenticateDataFutureState::AuthenticateSubrange`]
/// sub-state-machine state.
enum BufferedReadAuthenticateDataFutureAuthenticateState<B: blkdev::NvBlkDev> {
Init,
LoadAuthTreeLeafNode {
auth_tree_data_block_index: auth_tree::AuthTreeDataBlockIndex,
auth_tree_leaf_node_load_fut: auth_tree::AuthTreeNodeLoadFuture<B>,
},
}
/// [`NvBlKDevReadRequest`](blkdev::NvBlkDevReadRequest) implementation used
/// internally by [`BufferedReadAuthenticateDataFuture`].
struct BufferedReadAuthenticateDataFutureNvBlkDevReadRequest {
aligned_request_range_allocation_blocks_begin: layout::PhysicalAllocBlockIndex,
dst_allocation_blocks_bufs: FixedVec<FixedVec<u8, 7>, 0>,
alignment_scratch_allocation_blocks_bufs: FixedVec<FixedVec<u8, 7>, 0>,
head_alignment_scratch_allocation_blocks: usize,
authenticated_subrange_from_read_buf: Option<layout::PhysicalAllocBlockRange>,
read_request_allocation_blocks_begin: layout::PhysicalAllocBlockIndex,
read_request_io_region: ChunkedIoRegion,
}
impl blkdev::NvBlkDevReadRequest for BufferedReadAuthenticateDataFutureNvBlkDevReadRequest {
fn region(&self) -> &ChunkedIoRegion {
&self.read_request_io_region
}
fn get_destination_buffer(
&mut self,
range: &ChunkedIoRegionChunkRange,
) -> Result<Option<&mut [u8]>, blkdev::NvBlkDevIoError> {
let (allocation_block_index, _) = range.chunk().decompose_to_hierarchic_indices([]);
let allocation_block_index =
self.read_request_allocation_blocks_begin + layout::AllocBlockCount::from(allocation_block_index as u64);
// Do not overwrite already authenticated allocation blocks initially obtained
// from the read buffer.
if self
.authenticated_subrange_from_read_buf
.map(|authenticated_subrange_from_read_buf| {
allocation_block_index >= authenticated_subrange_from_read_buf.begin()
&& allocation_block_index < authenticated_subrange_from_read_buf.end()
})
.unwrap_or(false)
{
return Ok(None);
}
// Does not overflow an usize, the full aligned_request_range length in units of
// Allocation Blocks fits an usize, as per the check in
// BufferedReadAuthenticateDataFuture::new().
let allocation_block_index_in_aligned_request_range =
u64::from(allocation_block_index - self.aligned_request_range_allocation_blocks_begin) as usize;
let (head_alignment_scratch_allocation_blocks_bufs, tail_alignment_scratch_allocation_blocks_bufs) = self
.alignment_scratch_allocation_blocks_bufs
.split_at_mut(self.head_alignment_scratch_allocation_blocks);
let head_alignment_scratch_allocation_blocks = head_alignment_scratch_allocation_blocks_bufs.len();
let tail_alignment_scratch_allocation_blocks = tail_alignment_scratch_allocation_blocks_bufs.len();
let dst_allocation_blocks_bufs = &mut self.dst_allocation_blocks_bufs;
let dst_allocation_blocks = dst_allocation_blocks_bufs.len();
let allocation_block_buf =
if allocation_block_index_in_aligned_request_range < head_alignment_scratch_allocation_blocks {
head_alignment_scratch_allocation_blocks_bufs[allocation_block_index_in_aligned_request_range]
.as_mut_slice()
} else if allocation_block_index_in_aligned_request_range
< head_alignment_scratch_allocation_blocks + dst_allocation_blocks
{
dst_allocation_blocks_bufs
[allocation_block_index_in_aligned_request_range - head_alignment_scratch_allocation_blocks]
.as_mut_slice()
} else if allocation_block_index_in_aligned_request_range
< head_alignment_scratch_allocation_blocks
+ dst_allocation_blocks
+ tail_alignment_scratch_allocation_blocks
{
tail_alignment_scratch_allocation_blocks_bufs[allocation_block_index_in_aligned_request_range
- dst_allocation_blocks
- head_alignment_scratch_allocation_blocks]
.as_mut_slice()
} else {
return Err(nvblkdev_err_internal!());
};
if allocation_block_buf.is_empty() {
// The Allocation Block is unallocated and its contents are not needed.
return Ok(None);
}
Ok(Some(&mut allocation_block_buf[range.range_in_chunk().clone()]))
}
}