summaryrefslogtreecommitdiff
path: root/firmware/common/dircache.c
blob: 96b06eb547c9ae5258c33e4378f26f5a2c747f46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 by Miika Pekkarinen
 * Copyright (C) 2014 by Michael Sevakis
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "config.h"
#include <stdio.h>
#include <errno.h>
#include "string-extra.h"
#include <stdbool.h>
#include <stdlib.h>
#include "debug.h"
#include "system.h"
#include "logf.h"
#include "fileobj_mgr.h"
#include "pathfuncs.h"
#include "dircache.h"
#include "thread.h"
#include "kernel.h"
#include "usb.h"
#include "file.h"
#include "core_alloc.h"
#include "dir.h"
#include "storage.h"
#include "audio.h"
#include "rbpaths.h"
#include "linked_list.h"
#ifdef HAVE_EEPROM_SETTINGS
#include "crc32.h"
#endif

/**
 * Cache memory layout:
 * x - array of struct dircache_entry
 * r - reserved buffer
 * d - name buffer for the name entry of the struct dircache_entry
 * 0 - zero bytes to assist free name block sentinel scanning (not 0xfe or 0xff)
 * |xxxxxx|rrrrrrrrr|0|dddddd|0|
 *
 * Subsequent x are allocated from the front, d are allocated from the back,
 * using the reserve buffer for entries added after initial scan.
 *
 * After a while the cache may look like:
 * |xxxxxxxx|rrrrr|0|dddddddd|0|
 *
 * After a reboot, the reserve buffer is restored in it's size, so that the
 * total allocation size grows:
 * |xxxxxxxx|rrrrrrrrr|0|dddddddd|0|
 *
 *
 * Cache structure:
 * Format is memory position independent and uses only indexes as links. The
 * buffer pointers are offset back by one entry to make the array 1-based so
 * that an index of 0 may be considered an analog of a NULL pointer.
 *
 * Entry elements are linked together analagously to the filesystem directory
 * structure with minor variations that are helpful to the cache's algorithms.
 * Each volume has a special root structure in the dircache structure, not an
 * entry in the cache, comprising a forest of volume trees which facilitates
 * mounting or dismounting of specified volumes on the fly.
 *
 * Indexes identifying a volume are computed as: index = -volume - 1
 * Returning the volume from these indexes is thus: volume = -index - 1
 * Such indexes are used in root binding and as the 'up' index for an entry
 * who's parent is the root directory.
 *
 * Open files list:
 * When dircache is made it is the maintainer of the main volume open files
 * lists, even when it is off. Any files open before dircache is enabled or
 * initialized must be bound to cache entries by the scan and build operation.
 * It maintains these lists in a special way.
 *
 * Queued (unresolved) bindings are at the back and resolved at the front.
 * A pointer to the first of each kind of binding is provided to skip to help
 * iterating one sublist or another.
 *
 * r0->r1->r2->q0->q1->q2->NULL
 * ^resolved0  ^queued0
 */

#ifdef DIRCACHE_NATIVE
#define dircache_lock()   file_internal_lock_WRITER()
#define dircache_unlock() file_internal_unlock_WRITER()

/* scan and build parameter data */
struct sab_component;
struct sab
{
    struct filestr_base   stream;    /* scan directory stream */
    struct file_base_info info;      /* scanned entry info */
    bool volatile         quit;      /* halt all scanning */
    struct sab_component  *stackend; /* end of stack pointer */
    struct sab_component  *top;      /* current top of stack */
    struct sab_component
    {
        int volatile      idx;       /* cache index of directory */
        int               *downp;    /* pointer to ce->down */
        int *volatile     prevp;     /* previous item accessed */
    } stack[];                       /* "recursion" stack */
};

#else /* !DIRCACHE_NATIVE */

#error need locking scheme
#define FILESYS_WRITER_LOCK()
#define FILESYS_WRITER_UNLOCK()

struct sab_component
{
};

struct sab
{
#ifdef HAVE_MUTLIVOLUME
    int volume;
#endif /* HAVE_MULTIVOLUME */
    char path[MAX_PATH];
    unsigned int append;
};
#endif /* DIRCACHE_NATIVE */

enum
{
    FRONTIER_SETTLED = 0x0, /* dir entry contents are complete */
    FRONTIER_NEW     = 0x1, /* dir entry contents are in-progress */
    FRONTIER_ZONED   = 0x2, /* frontier entry permanent mark (very sticky!) */
    FRONTIER_RENEW   = 0x4, /* override FRONTIER_ZONED sticky (not stored) */
};

enum
{
    DCM_BUILD,              /* build a volume */
    DCM_PROCEED,            /* merged DCM_BUILD messages */
    DCM_FIRST = DCM_BUILD,
    DCM_LAST  = DCM_PROCEED,
};

#define MAX_TINYNAME   sizeof (uint32_t)
#define DC_MAX_NAME    MIN(MAX_NAME, UINT8_MAX)

/* Throw some warnings if about the limits if things may not work */
#if MAX_NAME > UINT8_MAX
#warning Need more than 8 bits in name length bitfield
#endif

#if DIRCACHE_LIMIT > ((1 << 24)-255)
#warning Names may not be addressable with 24 bits
#endif

/* data structure used by cache entries */
struct dircache_entry
{
    int         next;              /* next at same level */
    union {
    int         down;              /* first at child level (if directory) */
    file_size_t filesize;          /* size of file in bytes (if file) */
    };
    int         up;                /* parent index (-volume-1 if root) */
    union {
    struct {
    uint32_t    name         : 24; /* indirect storage (.tinyname == 0) */
    uint32_t    length       :  8; /* length of name indexed by 'name' */
    };
    unsigned char namebuf[MAX_TINYNAME]; /* direct storage (.tinyname == 1) */
    };
    uint32_t    direntry     : 16; /* entry # in parent - max 0xffff */
    uint32_t    direntries   :  5; /* # of entries used - max 21 */
    uint32_t    tinyname     :  1; /* if == 1, name fits in .namebuf */
    uint32_t    frontier     :  2; /* (FRONTIER_* bitflags) */
    uint32_t    attr         :  8; /* entry file attributes */
#ifdef DIRCACHE_NATIVE
    long        firstcluster;      /* first file cluster - max 0x0ffffff4 */
    uint16_t    wrtdate;           /* FAT write date */
    uint16_t    wrttime;           /* FAT write time */
#else
    time_t      mtime;             /* file last-modified time */
#endif
    dc_serial_t serialnum;         /* entry serial number */
};

/* spare us some tedium */
#define ENTRYSIZE   (sizeof (struct dircache_entry))

/* thread and kernel stuff */
static struct event_queue dircache_queue SHAREDBSS_ATTR;
static uintptr_t dircache_stack[DIRCACHE_STACK_SIZE / sizeof (uintptr_t)];
static const char dircache_thread_name[] = "dircache";

/* struct that is both used during run time and for persistent storage */
static struct dircache
{
    /* cache-wide data */
    int          free_list;           /* first index of free entry list */
    size_t       size;                /* total size of data (including holes) */
    size_t       sizeused;            /* bytes of .size bytes actually used */
    union {
    unsigned int numentries;          /* entry count (including holes) */
#ifdef HAVE_EEPROM_SETTINGS
    size_t       sizeentries;         /* used when persisting */
#endif
    };
    int          names;               /* index of first name in name block */
    size_t       sizenames;           /* size of all names (including holes) */
    size_t       namesfree;           /* amount of wasted name space */
    int          nextnamefree;        /* hint of next free name in buffer */
    /* per-volume data */
    struct dircache_volume            /* per volume cache data */
    {
        uint32_t       status   : 2;  /* cache status of this volume */
        uint32_t       frontier : 2;  /* (FRONTIER_* bitflags) */
        dc_serial_t    serialnum;     /* dircache serial number of root */
        int            root_down;     /* index of first entry of volume root */
        union {
        long           start_tick;    /* when did scan start (scanning) */
        long           build_ticks;   /* how long to build volume? (ready) */
        };
    } dcvol[NUM_VOLUMES];
    /* these remain unchanged between cache resets */
    size_t       last_size;           /* last reported size at boot */
    size_t       reserve_used;        /* reserved used at last build */
    dc_serial_t  last_serialnum;      /* last serialnumber generated */
} dircache;

/* struct that is used only for the cache in main memory */
static struct dircache_runinfo
{
    /* cache setting and build info */
    int          suspended;        /* dircache suspend count */
    bool         enabled;          /* dircache master enable switch */
    unsigned int thread_id;        /* current/last thread id */
    bool         thread_done;      /* thread has exited */
    /* cache buffer info */
    int          handle;           /* buflib buffer handle */
    size_t       bufsize;          /* size of buflib allocation - 1 */
    int          buflocked;        /* don't move due to other allocs */
    union {
    void                  *p;      /* address of buffer - ENTRYSIZE */
    struct dircache_entry *pentry; /* alias of .p to assist entry resolution */
    unsigned char         *pname;  /* alias of .p to assist name resolution */
    };
    struct buflib_callbacks ops;   /* buflib ops callbacks */
    /* per-volume data */
    struct dircache_runinfo_volume
    {
        struct file_base_binding *resolved0; /* first resolved binding in list */
        struct file_base_binding *queued0;   /* first queued binding in list */
        struct sab               *sabp;      /* if building, struct sab in use */
    } dcrivol[NUM_VOLUMES];
} dircache_runinfo;

#define BINDING_NEXT(bindp) \
    ((struct file_base_binding *)(bindp)->node.next)

#define FOR_EACH_BINDING(start, p) \
    for (struct file_base_binding *p = (start); p; p = BINDING_NEXT(p))

#define FOR_EACH_CACHE_ENTRY(ce) \
    for (struct dircache_entry *ce = &dircache_runinfo.pentry[1],  \
                               *_ceend = ce + dircache.numentries; \
         ce < _ceend; ce++) if (ce->serialnum)

#define FOR_EACH_SAB_COMP(sabp, p) \
    for (struct sab_component *p = (sabp)->top; p < (sabp)->stackend; p++)

/* "overloaded" macros to get volume structures */
#define DCVOL_i(i)               (&dircache.dcvol[i])
#define DCVOL_volume(volume)     (&dircache.dcvol[volume])
#define DCVOL_infop(infop)       (&dircache.dcvol[BASEINFO_VOL(infop)])
#define DCVOL_dirinfop(dirinfop) (&dircache.dcvol[BASEINFO_VOL(dirinfop)])
#define DCVOL(x)                 DCVOL_##x(x)

#define DCRIVOL_i(i)             (&dircache_runinfo.dcrivol[i])
#define DCRIVOL_infop(infop)     (&dircache_runinfo.dcrivol[BASEINFO_VOL(infop)])
#define DCRIVOL_bindp(bindp)     (&dircache_runinfo.dcrivol[BASEBINDING_VOL(bindp)])
#define DCRIVOL(x)               DCRIVOL_##x(x)

/* reserve over 75% full? */
#define DIRCACHE_STUFFED(reserve_used) \
    ((reserve_used) > 3*DIRCACHE_RESERVE / 4)

#ifdef HAVE_EEPROM_SETTINGS
/**
 * remove the snapshot file
 */
static int remove_dircache_file(void)
{
    return remove(DIRCACHE_FILE);
}

/**
 * open or create the snapshot file
 */
static int open_dircache_file(int oflag)
{
    return open(DIRCACHE_FILE, oflag, 0666);
}
#endif /* HAVE_EEPROM_SETTINGS */

#ifdef DIRCACHE_DUMPSTER
/**
 * clean up the memory allocation to make viewing in a hex editor more friendly
 * and highlight problems
 */
static inline void dumpster_clean_buffer(void *p, size_t size)
{
    memset(p, 0xAA, size);
}
#endif /* DIRCACHE_DUMPSTER */

/**
 * relocate the cache when the buffer has moved
 */
static int move_callback(int handle, void *current, void *new)
{
    if (dircache_runinfo.buflocked)
        return BUFLIB_CB_CANNOT_MOVE;

    dircache_runinfo.p = new - ENTRYSIZE;

    return BUFLIB_CB_OK;
    (void)handle; (void)current;
}

/**
 * add a "don't move" lock count
 */
static inline void buffer_lock(void)
{
    dircache_runinfo.buflocked++;
}

/**
 * remove a "don't move" lock count
 */
static inline void buffer_unlock(void)
{
    dircache_runinfo.buflocked--;
}


/** Open file bindings management **/

/* compare the basic file information and return 'true' if they are logically
   equivalent or the same item, else return 'false' if not */
static inline bool binding_compare(const struct file_base_info *infop1,
                                   const struct file_base_info *infop2)
{
#ifdef DIRCACHE_NATIVE
    return fat_file_is_same(&infop1->fatfile, &infop2->fatfile);
#else
    #error hey watch it!
#endif
}

/**
 * bind a file to the cache; "queued" or "resolved" depending upon whether or
 * not it has entry information
 */
static void binding_open(struct file_base_binding *bindp)
{
    struct dircache_runinfo_volume *dcrivolp = DCRIVOL(bindp);
    if (bindp->info.dcfile.serialnum)
    {
        /* already resolved */
        dcrivolp->resolved0 = bindp;
        file_binding_insert_first(bindp);
    }
    else
    {
        if (dcrivolp->queued0 == NULL)
            dcrivolp->queued0 = bindp;

        file_binding_insert_last(bindp);
    }
}

/**
 * remove a binding from the cache
 */
static void binding_close(struct file_base_binding *bindp)
{
    struct dircache_runinfo_volume *dcrivolp = DCRIVOL(bindp);

    if (bindp == dcrivolp->queued0)
        dcrivolp->queued0 = BINDING_NEXT(bindp);
    else if (bindp == dcrivolp->resolved0)
    {
        struct file_base_binding *nextp = BINDING_NEXT(bindp);
        dcrivolp->resolved0 = (nextp == dcrivolp->queued0) ? NULL : nextp;
    }

    file_binding_remove(bindp);
    /* no need to reset it */
}

/**
 * resolve a queued binding with the information from the given source file
 */
static void binding_resolve(const struct file_base_info *infop)
{
    struct dircache_runinfo_volume *dcrivolp = DCRIVOL(infop);

    /* quickly check the queued list to see if it's there */
    struct file_base_binding *prevp = NULL;
    FOR_EACH_BINDING(dcrivolp->queued0, p)
    {
        if (!binding_compare(infop, &p->info))
        {
            prevp = p;
            continue;
        }

        if (p == dcrivolp->queued0)
            dcrivolp->queued0 = BINDING_NEXT(p);
        else
        {
            file_binding_remove_next(prevp, p);
            file_binding_insert_first(p);
        }

        dcrivolp->resolved0 = p;

        /* srcinfop may be the actual one */
        if (&p->info != infop)
            p->info.dcfile = infop->dcfile;

        break;
    }
}

/**
 * dissolve a resolved binding on its volume
 */
static void binding_dissolve(struct file_base_binding *prevp,
                             struct file_base_binding *bindp)
{
    struct dircache_runinfo_volume *dcrivolp = DCRIVOL(bindp);

    if (bindp == dcrivolp->resolved0)
    {
        struct file_base_binding *nextp = BINDING_NEXT(bindp);
        dcrivolp->resolved0 = (nextp == dcrivolp->queued0) ? NULL : nextp;
    }

    if (dcrivolp->queued0 == NULL)
        dcrivolp->queued0 = bindp;

    file_binding_remove_next(prevp, bindp);
    file_binding_insert_last(bindp);

    dircache_dcfile_init(&bindp->info.dcfile);
}

/**
 * dissolve all resolved bindings on a given volume
 */
static void binding_dissolve_volume(struct dircache_runinfo_volume *dcrivolp)
{
    if (!dcrivolp->resolved0)
        return;

    FOR_EACH_BINDING(dcrivolp->resolved0, p)
    {
        if (p == dcrivolp->queued0)
            break;

        dircache_dcfile_init(&p->info.dcfile);
    }

    dcrivolp->queued0 = dcrivolp->resolved0;
    dcrivolp->resolved0 = NULL;
}


/** Dircache buffer management **/

/**
 * allocate the cache's memory block
 */
static int alloc_cache(size_t size)
{
    /* pad with one extra-- see alloc_name() and free_name() */
    return core_alloc_ex("dircache", size + 1, &dircache_runinfo.ops);
}

/**
 * put the allocation in dircache control
 */
static void set_buffer(int handle, size_t size)
{
    void *p = core_get_data(handle);

#ifdef DIRCACHE_DUMPSTER
    dumpster_clean_buffer(p, size);
#endif /* DIRCACHE_DUMPSTER */

    /* set it up as a 1-based array */
    dircache_runinfo.p = p - ENTRYSIZE;

    if (dircache_runinfo.handle != handle)
    {
        /* new buffer */
        dircache_runinfo.handle = handle;
        dircache_runinfo.bufsize = size;
        dircache.names = size + ENTRYSIZE;
        dircache_runinfo.pname[dircache.names - 1] = 0;
        dircache_runinfo.pname[dircache.names    ] = 0;
    }
}

/**
 * remove the allocation from dircache control and return the handle
 */
static int reset_buffer(void)
{
    int handle = dircache_runinfo.handle;
    if (handle > 0)
    {
        /* don't mind .p; it might get changed by the callback even after
           this call; buffer presence is determined by the following: */
        dircache_runinfo.handle  = 0;
        dircache_runinfo.bufsize = 0;
    }

    return handle;
}

/**
 * return the number of bytes remaining in the buffer
 */
static size_t dircache_buf_remaining(void)
{
    if (!dircache_runinfo.handle)
        return 0;

    return dircache_runinfo.bufsize - dircache.size;
}

/**
 * return the amount of reserve space used
 */
static size_t reserve_buf_used(void)
{
    size_t remaining = dircache_buf_remaining();
    return (remaining < DIRCACHE_RESERVE) ?
                DIRCACHE_RESERVE - remaining : 0;
}


/** Internal cache structure control functions **/

/**
 * generate the next serial number in the sequence
 */
static dc_serial_t next_serialnum(void)
{
    dc_serial_t serialnum = MAX(dircache.last_serialnum + 1, 1);
    dircache.last_serialnum = serialnum;
    return serialnum;
}

/**
 * return the dircache volume pointer for the special index
 */
static struct dircache_volume * get_idx_dcvolp(int idx)
{
    if (idx >= 0)
        return NULL;

    return &dircache.dcvol[IF_MV_VOL(-idx - 1)];
}

/**
 * return the cache entry referenced by idx (NULL if outside buffer)
 */
static struct dircache_entry * get_entry(int idx)
{
    if (idx <= 0 || (unsigned int)idx > dircache.numentries)
        return NULL;

    return &dircache_runinfo.pentry[idx];
}

/**
 * return the index of the cache entry (0 if outside buffer)
 */
static int get_index(const struct dircache_entry *ce)
{
    if (!PTR_IN_ARRAY(dircache_runinfo.pentry + 1, ce,
                      dircache.numentries + 1))
    {
        return 0;
    }

    return ce - dircache_runinfo.pentry;
}

/**
 *  return the sublist down pointer for the sublist that contains entry 'idx'
 */
static int * get_downidxp(int idx)
{
    /* NOTE: 'idx' must refer to a directory or the result is undefined */
    if (idx == 0 || idx < -NUM_VOLUMES)
        return NULL;

    if (idx > 0)
    {
        /* a normal entry */
        struct dircache_entry *ce = get_entry(idx);
        return ce ? &ce->down : NULL;
    }
    else
    {
        /* a volume root */
        return &get_idx_dcvolp(idx)->root_down;
    }
}

/**
 * return a pointer to the index referencing the cache entry that 'idx'
 * references
 */
static int * get_previdxp(int idx)
{
    struct dircache_entry *ce = get_entry(idx);

    int *prevp = get_downidxp(ce->up);
    if (!prevp)
        return NULL;

    while (1)
    {
        int next = *prevp;
        if (!next || next == idx)
            break;

        prevp = &get_entry(next)->next;
    }

    return prevp;
}

/**
 * if required, adjust the lists and directory read of any scan and build in
 * progress
 */
static void sab_sync_scan(struct sab *sabp, int *prevp, int *nextp)
{
    struct sab_component *abovep = NULL;
    FOR_EACH_SAB_COMP(sabp, p)
    {
        if (nextp != p->prevp)
        {
            abovep = p;
            continue;
        }

        /* removing an item being scanned; set the component position to the
           entry before this */
        p->prevp = prevp;

        if (p == sabp->top)
        {
            /* removed at item in the directory who's immediate contents are
               being scanned */
            if (prevp == p->downp)
            {
                /* was first item; rewind it */
                dircache_rewinddir_internal(&sabp->info);
            }
            else
            {
                struct dircache_entry *ceprev =
                    container_of(prevp, struct dircache_entry, next);
            #ifdef DIRCACHE_NATIVE
                sabp->info.fatfile.e.entry   = ceprev->direntry;
                sabp->info.fatfile.e.entries = ceprev->direntries;
            #endif
            }
        }
        else if (abovep)
        {
            /* the directory being scanned or a parent of it has been removed;
               abort its build or cache traversal */
            abovep->idx = 0;
        }

        break;
    }
}

/**
 * get a pointer to an allocated name given a cache index
 */
static inline unsigned char * get_name(int nameidx)
{
    return &dircache_runinfo.pname[nameidx];
}

/**
 * get the cache buffer index of the given name
 */
static inline int get_nameidx(const unsigned char *pname)
{
    return pname - dircache_runinfo.pname;
}

/**
 * copy the entry's name to a buffer (which assumed to be of sufficient size)
 */
static void entry_name_copy(char *dst, const struct dircache_entry *ce)
{
    if (LIKELY(!ce->tinyname))
    {
        strmemcpy(dst, get_name(ce->name), ce->length);
        return;
    }

    const unsigned char *src = ce->namebuf;
    size_t len = 0;
    while (len++ < MAX_TINYNAME && *src)
        *dst++ = *src++;

    *dst = '\0';
}

/**
 * set the namesfree hint to a new position
 */
static void set_namesfree_hint(const unsigned char *hintp)
{
    int hintidx = get_nameidx(hintp);

    if (hintidx >= (int)(dircache.names + dircache.sizenames))
        hintidx = dircache.names;

    dircache.nextnamefree = hintidx;
}

/**
 * allocate a buffer to use for a new name
 */
static int alloc_name(size_t size)
{
    int nameidx = 0;

    if (dircache.namesfree >= size)
    {
        /* scan for a free gap starting at the hint point - first fit */
        unsigned char *start = get_name(dircache.nextnamefree), *p = start;
        unsigned char *namesend = get_name(dircache.names + dircache.sizenames);
        size_t gapsize = 0;

        while (gapsize < size)
        {
            if ((p = memchr(p, 0xff, namesend - p)))
            {
                /* found a sentinel; see if there are enough in a row */
                gapsize = 1;
                while (*++p == 0xff && gapsize < size)
                    gapsize++;
            }
            else
            {
                if (namesend == start)
                    break; /* exhausted */

                /* wrap */
                namesend = start;
                p = get_name(dircache.names);

                if (p == namesend)
                    break; /* initial hint was at names start */
            }
        }

        if (gapsize >= size)
        {
            unsigned char *namep = p - gapsize;
            nameidx = get_nameidx(namep);

            if (*p == 0xff)
            {
                /* if only a tiny block remains after buffer, claim it too */
                size_t tinysize = 1;
                while (*++p == 0xff && tinysize <= MAX_TINYNAME)
                    tinysize++;

                if (tinysize <= MAX_TINYNAME)
                {
                    /* mark with tiny block sentinel */
                    memset(p - tinysize, 0xfe, tinysize);
                    size += tinysize;
                }
            }

            dircache.namesfree -= size;
            dircache.sizeused += size;
            set_namesfree_hint(namep + size);
        }
    }

    if (!nameidx)
    {
        /* no sufficiently long free gaps; allocate anew */
        if (dircache_buf_remaining() <= size)
        {
            dircache.last_size = 0;
            return 0;
        }

        dircache.names     -= size;
        dircache.sizenames += size;
        nameidx             = dircache.names;
        dircache.size      += size;
        dircache.sizeused  += size;
        *get_name(dircache.names - 1) = 0;
    }

    return nameidx;
}

/**
 * mark a name as free and note that its bytes are available
 */
static void free_name(int nameidx, size_t size)
{
    unsigned char *beg = get_name(nameidx);
    unsigned char *end = beg + size;

    /* merge with any adjacent tiny blocks */
    while (beg[-1] == 0xfe)
        --beg;

    while (end[1] == 0xfe)
        ++end;

    size = end - beg;
    memset(beg, 0xff, size);
    dircache.namesfree += size;
    dircache.sizeused -= size;
    set_namesfree_hint(beg);
}

/**
 * allocate and assign a name to the entry
 */
static bool entry_assign_name(struct dircache_entry *ce,
                              const unsigned char *name, size_t size)
{
    unsigned char *copyto;

    if (size <= MAX_TINYNAME)
    {
        copyto = ce->namebuf;

        if (size < MAX_TINYNAME)
            copyto[size] = '\0';

        ce->tinyname = 1;
    }
    else
    {
        if (size > DC_MAX_NAME)
            size = DC_MAX_NAME;

        int nameidx = alloc_name(size);
        if (!nameidx)
            return false;

        copyto = get_name(nameidx);

        ce->tinyname = 0;
        ce->name     = nameidx;
        ce->length   = size;
    }

    memcpy(copyto, name, size);
    return true;
}

/**
 * free the name for the entry
 */
static void entry_unassign_name(struct dircache_entry *ce)
{
    if (!ce->tinyname)
        free_name(ce->name, ce->length);
}

/**
 * assign a new name to the entry
 */
static bool entry_reassign_name(struct dircache_entry *ce,
                                const unsigned char *newname)
{
    size_t oldlen = ce->tinyname ? 0 : ce->length;
    size_t newlen = strlen(newname);

    if (oldlen == newlen || (oldlen == 0 && newlen <= MAX_TINYNAME))
    {
        char *p = mempcpy(oldlen == 0 ? ce->namebuf : get_name(ce->name),
                          newname, newlen);
        if (newlen < MAX_TINYNAME)
            *p = '\0';
        return true;
    }

    /* needs a new name allocation; if the new name fits in the freed block,
       it will use it immediately without a lengthy search */
    entry_unassign_name(ce);
    return entry_assign_name(ce, newname, newlen);
}

/**
 * allocate a dircache_entry from memory using freed ones if available
 */
static int alloc_entry(struct dircache_entry **res)
{
    struct dircache_entry *ce;
    int idx = dircache.free_list;

    if (idx)
    {
        /* reuse a freed entry */
        ce = get_entry(idx);
        dircache.free_list = ce->next;
    }
    else if (dircache_buf_remaining() > ENTRYSIZE)
    {
        /* allocate a new one */
        idx = ++dircache.numentries;
        dircache.size += ENTRYSIZE;
        ce = get_entry(idx);
    }
    else
    {
        dircache.last_size = 0;
        *res = NULL;
        return 0;
    }

    dircache.sizeused += ENTRYSIZE;

    ce->next      = 0;
    ce->up        = 0;
    ce->down      = 0;
    ce->length    = 0;
    ce->frontier  = FRONTIER_SETTLED;
    ce->serialnum = next_serialnum();

    *res = ce;
    return idx;
}

/**
 * free an entry's allocations in the cache; must not be linked to anything
 * by this time (orphan!)
 */
static void free_orphan_entry(struct dircache_runinfo_volume *dcrivolp,
                              struct dircache_entry *ce, int idx)
{
    if (dcrivolp)
    {
        /* was an established entry; find any associated resolved binding and
           dissolve it;  bindings are kept strictly synchronized with changes
           to the storage so a simple serial number comparison is sufficient */
        struct file_base_binding *prevp = NULL;
        FOR_EACH_BINDING(dcrivolp->resolved0, p)
        {
            if (p == dcrivolp->queued0)
                break;

            if (ce->serialnum == p->info.dcfile.serialnum)
            {
                binding_dissolve(prevp, p);
                break;
            }

            prevp = p;
        }
    }

    entry_unassign_name(ce);

    /* no serialnum says "it's free" (for cache-wide iterators) */
    ce->serialnum = 0;

    /* add to free list */
    ce->next = dircache.free_list;
    dircache.free_list = idx;
    dircache.sizeused -= ENTRYSIZE;
}

/**
 * allocates a new entry of with the name specified by 'basename'
 */
static int create_entry(const char *basename,
                        struct dircache_entry **res)
{
    int idx = alloc_entry(res);
    if (!idx)
    {
        logf("size limit reached (entry)");
        return 0; /* full */
    }

    if (!entry_assign_name(*res, basename, strlen(basename)))
    {
        free_orphan_entry(NULL, *res, idx);
        logf("size limit reached (name)");
        return 0; /* really full! */
    }

    return idx;
}

/**
 * unlink the entry at *prevp and adjust the scanner if needed
 */
static void remove_entry(struct dircache_runinfo_volume *dcrivolp,
                         struct dircache_entry *ce, int *prevp)
{
    /* unlink it from its list */
    *prevp = ce->next;

    if (dcrivolp)
    {
        /* adjust scanner iterator if needed */
        struct sab *sabp = dcrivolp->sabp;
        if (sabp)
            sab_sync_scan(sabp, prevp, &ce->next);
    }
}

/**
 * free the entire subtree in the referenced parent down index
 */
static void free_subentries(struct dircache_runinfo_volume *dcrivolp, int *downp)
{
    while (1)
    {
        int idx = *downp;
        struct dircache_entry *ce = get_entry(idx);
        if (!ce)
            break;

        if ((ce->attr & ATTR_DIRECTORY) && ce->down)
            free_subentries(dcrivolp, &ce->down);

        remove_entry(dcrivolp, ce, downp);
        free_orphan_entry(dcrivolp, ce, idx);
    }
}

/**
 * free the specified file entry and its children
 */
static void free_file_entry(struct file_base_info *infop)
{
    int idx = infop->dcfile.idx;
    if (idx <= 0)
        return; /* can't remove a root/invalid */

    struct dircache_runinfo_volume *dcrivolp = DCRIVOL(infop);

    struct dircache_entry *ce = get_entry(idx);
    if ((ce->attr & ATTR_DIRECTORY) && ce->down)
    {
        /* gonna get all this contents (normally the "." and "..") */
        free_subentries(dcrivolp, &ce->down);
    }

    remove_entry(dcrivolp, ce, get_previdxp(idx));
    free_orphan_entry(dcrivolp, ce, idx);
}

/**
 * insert the new entry into the parent, sorted into position
 */
static void insert_file_entry(struct file_base_info *dirinfop,
                              struct dircache_entry *ce)
{
    /* DIRCACHE_NATIVE: the entires are sorted into the spot it would be on
     * the storage medium based upon the directory entry number, in-progress
     * scans will catch it or miss it in just the same way they would if
     * directly scanning the disk. If this is behind an init scan, it gets
     * added anyway; if in front of it, then scanning will compare what it
     * finds in order to avoid adding a duplicate.
     *
     * All others: the order of entries of the host filesystem is not known so
     * this must be placed at the end so that a build scan won't miss it and
     * add a duplicate since it will be comparing any entries it finds in front
     * of it.
     */
    int diridx = dirinfop->dcfile.idx;
    int *nextp = get_downidxp(diridx);

    while (8675309)
    {
        struct dircache_entry *nextce = get_entry(*nextp);
        if (!nextce)
            break;

#ifdef DIRCACHE_NATIVE
        if (nextce->direntry > ce->direntry)
            break;

        /* now, nothing should be equal to ours or that is a bug since it
           would already exist (and it shouldn't because it's just been
           created or moved) */
#endif /* DIRCACHE_NATIVE */

        nextp = &nextce->next;
    }

    ce->up   = diridx;
    ce->next = *nextp;
    *nextp   = get_index(ce);
}

/**
 * unlink the entry from its parent and return its pointer to the caller
 */
static struct dircache_entry * remove_file_entry(struct file_base_info *infop)
{
    struct dircache_runinfo_volume *dcrivolp = DCRIVOL(infop);
    struct dircache_entry *ce = get_entry(infop->dcfile.idx);
    remove_entry(dcrivolp, ce, get_previdxp(infop->dcfile.idx));
    return ce;
}

/**
 * set the frontier indicator for the given cache index
 */
static void establish_frontier(int idx, uint32_t code)
{
    if (idx < 0)
    {
        int volume = IF_MV_VOL(-idx - 1);
        uint32_t val = dircache.dcvol[volume].frontier;
        if (code & FRONTIER_RENEW)
            val &= ~FRONTIER_ZONED;
        dircache.dcvol[volume].frontier = code | (val & FRONTIER_ZONED);
    }
    else if (idx > 0)
    {
        struct dircache_entry *ce = get_entry(idx);
        uint32_t val = ce->frontier;
        if (code & FRONTIER_RENEW)
            val &= ~FRONTIER_ZONED;
        ce->frontier = code | (val & FRONTIER_ZONED);
    }
}

/**
 * remove all messages from the queue, responding to anyone waiting
 */
static void clear_dircache_queue(void)
{
    struct queue_event ev;

    while (1)
    {
        queue_wait_w_tmo(&dircache_queue, &ev, 0);
        if (ev.id == SYS_TIMEOUT)
            break;

        /* respond to any synchronous build queries; since we're already
           building and thusly allocated, any additional requests can be
           processed async */
        if (ev.id == DCM_BUILD)
        {
            int *rcp = (int *)ev.data;
            if (rcp)
                *rcp = 0;
        }
    }
}

/**
 * service dircache_queue during a scan and build
 */
static void process_events(void)
{
    yield();

    /* only count externally generated commands */
    if (!queue_peek_ex(&dircache_queue, NULL, 0, QPEEK_FILTER1(DCM_BUILD)))
        return;

    clear_dircache_queue();

    /* this reminds us to keep moving after we're done here; a volume we passed
       up earlier could have been mounted and need refreshing; it just condenses
       a slew of requests into one and isn't mistaken for an externally generated
       command */
    queue_post(&dircache_queue, DCM_PROCEED, 0);
}

#if defined (DIRCACHE_NATIVE)
/**
 * scan and build the contents of a subdirectory
 */
static void sab_process_sub(struct sab *sabp)
{
    struct fat_direntry *const fatentp = get_dir_fatent();
    struct filestr_base *const streamp = &sabp->stream;
    struct file_base_info *const infop = &sabp->info;

    int idx = infop->dcfile.idx;
    int *downp = get_downidxp(idx);
    if (!downp)
        return;

    while (1)
    {
        struct sab_component *compp = --sabp->top;
        compp->idx   = idx;
        compp->downp = downp;
        compp->prevp = downp;

        /* open directory stream */
        filestr_base_init(streamp);
        fileobj_fileop_open(streamp, infop, FO_DIRECTORY);
        fat_rewind(&streamp->fatstr);
        uncached_rewinddir_internal(infop);

        const long dircluster = streamp->infop->fatfile.firstcluster;

        /* first pass: read directory */
        while (1)
        {
            if (sabp->stack + 1 < sabp->stackend)
            {
                /* release control and process queued events */
                dircache_unlock();
                process_events();
                dircache_lock();

                if (sabp->quit || !compp->idx)
                    break;
            }
            /* else an immediate-contents directory scan */

            int rc = uncached_readdir_internal(streamp, infop, fatentp);
            if (rc <= 0)
            {
                if (rc < 0)
                    sabp->quit = true;
                else
                    compp->prevp = downp; /* rewind list */

                break;
            }

            struct dircache_entry *ce;
            int prev = *compp->prevp;

            if (prev)
            {
                /* there are entries ahead of us; they will be what was just
                   read or something to be subsequently read; if it belongs
                   ahead of this one, insert a new entry before it; if it's
                   the entry just scanned, do nothing further and continue
                   with the next */
                ce = get_entry(prev);
                if (ce->direntry == infop->fatfile.e.entry)
                {
                    compp->prevp = &ce->next;
                    continue; /* already there */
                }
            }

            int idx = create_entry(fatentp->name, &ce);
            if (!idx)
            {
                sabp->quit = true;
                break;
            }

            /* link it in */
            ce->up = compp->idx;
            ce->next = prev;
            *compp->prevp = idx;
            compp->prevp = &ce->next;

            if (!(fatentp->attr & ATTR_DIRECTORY))
                ce->filesize = fatentp->filesize;
            else if (!is_dotdir_name(fatentp->name))
                ce->frontier = FRONTIER_NEW; /* this needs scanning */

            /* copy remaining FS info */
            ce->direntry     = infop->fatfile.e.entry;
            ce->direntries   = infop->fatfile.e.entries;
            ce->attr         = fatentp->attr;
            ce->firstcluster = fatentp->firstcluster;
            ce->wrtdate      = fatentp->wrtdate;
            ce->wrttime      = fatentp->wrttime;

            /* resolve queued user bindings */
            infop->fatfile.firstcluster = fatentp->firstcluster;
            infop->fatfile.dircluster   = dircluster;
            infop->dcfile.idx           = idx;
            infop->dcfile.serialnum     = ce->serialnum;
            binding_resolve(infop);
        } /* end while */

        close_stream_internal(streamp);

        if (sabp->quit)
            return;

        establish_frontier(compp->idx, FRONTIER_SETTLED);

        /* second pass: "recurse!" */
        struct dircache_entry *ce = NULL;

        while (1)
        {
            idx = compp->idx && compp > sabp->stack ? *compp->prevp : 0;
            if (idx)
            {
                ce = get_entry(idx);
                compp->prevp = &ce->next;

                if (ce->frontier != FRONTIER_SETTLED)
                    break;
            }
            else
            {
                /* directory completed or removed/deepest level */
                compp = ++sabp->top;
                if (compp >= sabp->stackend)
                    return; /* scan completed/initial directory removed */
            }
        }

        /* even if it got zoned from outside it is about to be scanned in
           its entirety and may be considered new again */
        ce->frontier = FRONTIER_NEW;
        downp = &ce->down;

        /* set up info for next open
         * IF_MV: "volume" was set when scan began */
        infop->fatfile.firstcluster = ce->firstcluster;
        infop->fatfile.dircluster   = dircluster;
        infop->fatfile.e.entry      = ce->direntry;
        infop->fatfile.e.entries    = ce->direntries;
    } /* end while */
}

/**
 * scan and build the contents of a directory or volume root
 */
static void sab_process_dir(struct file_base_info *infop, bool issab)
{
    /* infop should have been fully opened meaning that all its parent
       directory information is filled in and intact; the binding information
       should also filled in beforehand */

    /* allocate the stack right now to the max demand */
    struct dirsab
    {
        struct sab           sab;
        struct sab_component stack[issab ? DIRCACHE_MAX_DEPTH : 1];
    } dirsab;
    struct sab *sabp = &dirsab.sab;

    sabp->quit     = false;
    sabp->stackend = &sabp->stack[ARRAYLEN(dirsab.stack)];
    sabp->top      = sabp->stackend;
    sabp->info     = *infop;

    if (issab)
        DCRIVOL(infop)->sabp = sabp;

    establish_frontier(infop->dcfile.idx, FRONTIER_NEW | FRONTIER_RENEW);
    sab_process_sub(sabp);

    if (issab)
        DCRIVOL(infop)->sabp = NULL;
}

/**
 * scan and build the entire tree for a volume
 */
static void sab_process_volume(struct dircache_volume *dcvolp)
{
    int rc;

    int volume = IF_MV_VOL(dcvolp - dircache.dcvol);
    int idx = -volume - 1;

    logf("dircache - building volume %d", volume);

    /* gather everything sab_process_dir() needs in order to begin a scan */
    struct file_base_info info;
    rc = fat_open_rootdir(IF_MV(volume,) &info.fatfile);
    if (rc < 0)
    {
        /* probably not mounted */
        logf("SAB - no root %d: %d", volume, rc);
        establish_frontier(idx, FRONTIER_NEW);
        return;
    }

    info.dcfile.idx       = idx;
    info.dcfile.serialnum = dcvolp->serialnum;
    sab_process_dir(&info, true);
}

/**
 * this function is the back end to the public API's like readdir()
 */
int dircache_readdir_dirent(struct filestr_base *stream,
                            struct dirscan_info *scanp,
                            struct dirent *entry)
{
    struct file_base_info *dirinfop = stream->infop;

    if (!dirinfop->dcfile.serialnum)
        goto read_uncached; /* no parent cached => no entries cached */

    struct dircache_volume *dcvolp = DCVOL(dirinfop);

    int diridx = dirinfop->dcfile.idx;
    unsigned int frontier = diridx <= 0 ? dcvolp->frontier :
                                          get_entry(diridx)->frontier;

    /* if not settled, just readthrough; no binding information is needed for
       this; if it becomes settled, we'll get scan->dcfile caught up and do
       subsequent reads with the cache */
    if (frontier != FRONTIER_SETTLED)
        goto read_uncached;

    int idx = scanp->dcscan.idx;
    struct dircache_entry *ce;

    unsigned int direntry = scanp->fatscan.entry;
    while (1)
    {
        if (idx == 0 || direntry == FAT_RW_VAL) /* rewound? */
        {
            idx = diridx <= 0 ? dcvolp->root_down : get_entry(diridx)->down;
            break;
        }

        /* validate last entry scanned; it might have been replaced between
           calls or not there at all any more; if so, start the cache reader
           at the beginning and fast-forward to the correct point as indicated
           by the FS scanner */
        ce = get_entry(idx);
        if (ce && ce->serialnum == scanp->dcscan.serialnum)
        {
            idx = ce->next;
            break;
        }

        idx = 0;
    }

    while (1)
    {
        ce = get_entry(idx);
        if (!ce)
        {
            empty_dirent(entry);
            scanp->fatscan.entries = 0;
            return 0; /* end of dir */
        }

        if (ce->direntry > direntry || direntry == FAT_RW_VAL)
            break; /* cache reader is caught up to FS scan */

        idx = ce->next;
    }

    /* basic dirent information */
    entry_name_copy(entry->d_name, ce);
    entry->info.attr    = ce->attr;
    entry->info.size    = (ce->attr & ATTR_DIRECTORY) ? 0 : ce->filesize;
    entry->info.wrtdate = ce->wrtdate;
    entry->info.wrttime = ce->wrttime;

    /* FS scan information */
    scanp->fatscan.entry    = ce->direntry;
    scanp->fatscan.entries  = ce->direntries;

    /* dircache scan information */
    scanp->dcscan.idx       = idx;
    scanp->dcscan.serialnum = ce->serialnum;

    /* return whether this needs decoding */
    int rc = ce->direntries == 1 ? 2 : 1;

    yield();
    return rc;

read_uncached:
    dircache_dcfile_init(&scanp->dcscan);
    return uncached_readdir_dirent(stream, scanp, entry);
}

/**
 * rewind the directory scan cursor
 */
void dircache_rewinddir_dirent(struct dirscan_info *scanp)
{
    uncached_rewinddir_dirent(scanp);
    dircache_dcfile_init(&scanp->dcscan);
}

/**
 * this function is the back end to file API internal scanning, which requires
 * much more detail about the directory entries; this is allowed to make
 * assumptions about cache state because the cache will not be altered during
 * the scan process; an additional important property of internal scanning is
 * that any available binding information is not ignored even when a scan
 * directory is frontier zoned.
 */
int dircache_readdir_internal(struct filestr_base *stream,
                              struct file_base_info *infop,
                              struct fat_direntry *fatent)
{
    /* call with writer exclusion */
    struct file_base_info *dirinfop = stream->infop;
    struct dircache_volume *dcvolp = DCVOL(dirinfop);

    /* assume binding "not found" */
    infop->dcfile.serialnum = 0;

    /* is parent cached? if not, readthrough because nothing is here yet */
    if (!dirinfop->dcfile.serialnum)
        return uncached_readdir_internal(stream, infop, fatent);

    int diridx = dirinfop->dcfile.idx;
    unsigned int frontier = diridx < 0 ?
        dcvolp->frontier : get_entry(diridx)->frontier;

    int idx = infop->dcfile.idx;
    if (idx == 0) /* rewound? */
        idx = diridx <= 0 ? dcvolp->root_down : get_entry(diridx)->down;
    else
        idx = get_entry(idx)->next;

    struct dircache_entry *ce = get_entry(idx);
    if (frontier != FRONTIER_SETTLED)
    {
        /* the directory being read is reported to be incompletely cached;
           readthrough and if the entry exists, return it with its binding
           information; otherwise return the uncached read result while
           maintaining the last index */
        int rc = uncached_readdir_internal(stream, infop, fatent);
        if (rc <= 0 || !ce || ce->direntry > infop->fatfile.e.entry)
            return rc;

        /* entry matches next one to read */
    }
    else if (!ce)
    {
        /* end of dir */
        fat_empty_fat_direntry(fatent);
        infop->fatfile.e.entries = 0;
        return 0;
    }

    /* FS entry information that we maintain */
    entry_name_copy(fatent->name, ce);
    fatent->shortname[0]     = '\0';
    fatent->attr             = ce->attr;
    /* file code file scanning does not need time information */
    fatent->filesize         = (ce->attr & ATTR_DIRECTORY) ? 0 : ce->filesize;
    fatent->firstcluster     = ce->firstcluster;

    /* FS entry directory information */
    infop->fatfile.e.entry   = ce->direntry;
    infop->fatfile.e.entries = ce->direntries;

    /* dircache file binding information */
    infop->dcfile.idx        = idx;
    infop->dcfile.serialnum  = ce->serialnum;

    /* return whether this needs decoding */
    int rc = ce->direntries == 1 ? 2 : 1;

    if (frontier == FRONTIER_SETTLED)
    {
        static long next_yield;
        if (TIME_AFTER(current_tick, next_yield))
        {
            yield();
            next_yield = current_tick + HZ/50;
        }
    }

    return rc;
}

/**
 * rewind the scan position for an internal scan
 */
void dircache_rewinddir_internal(struct file_base_info *infop)
{
    uncached_rewinddir_internal(infop);
    dircache_dcfile_init(&infop->dcfile);
}

#else /* !DIRCACHE_NATIVE (for all others) */

#####################
/* we require access to the host functions */
#undef opendir
#undef readdir
#undef closedir
#undef rewinddir

static char sab_path[MAX_PATH];

static int sab_process_dir(struct dircache_entry *ce)
{
    struct dirent_uncached *entry;
    struct dircache_entry *first_ce = ce;
    DIR *dir = opendir(sab_path);
    if(dir == NULL)
    {
        logf("Failed to opendir_uncached(%s)", sab_path);
        return -1;
    }

    while (1)
    {
        if (!(entry = readdir(dir)))
            break;

        if (IS_DOTDIR_NAME(entry->d_name))
        {
            /* add "." and ".." */
            ce->info.attribute = ATTR_DIRECTORY;
            ce->info.size = 0;
            ce->down = entry->d_name[1] == '\0' ? first_ce : first_ce->up;
            strcpy(ce->dot_d_name, entry->d_name);
            continue;
        }

        size_t size = strlen(entry->d_name) + 1;
        ce->d_name = (d_names_start -= size);
        ce->info = entry->info;

        strcpy(ce->d_name, entry->d_name);
        dircache_size += size;

        if(entry->info.attribute & ATTR_DIRECTORY)
        {
            dircache_gen_down(ce, ce);
            if(ce->down == NULL)
            {
                closedir_uncached(dir);
                return -1;
            }
            /* save current paths size */
            int pathpos = strlen(sab_path);
            /* append entry */
            strlcpy(&sab_path[pathpos], "/", sizeof(sab_path) - pathpos);
            strlcpy(&sab_path[pathpos+1], entry->d_name, sizeof(sab_path) - pathpos - 1);

            int rc = sab_process_dir(ce->down);
            /* restore path */
            sab_path[pathpos] = '\0';

            if(rc < 0)
            {
                closedir_uncached(dir);
                return rc;
            }
        }

        ce = dircache_gen_entry(ce);
        if (ce == NULL)
            return -5;

        yield();
    }

    closedir_uncached(dir);
    return 1;
}

static int sab_process_volume(IF_MV(int volume,) struct dircache_entry *ce)
{
    memset(ce, 0, sizeof(struct dircache_entry));
    strlcpy(sab_path, "/", sizeof sab_path);
    return sab_process_dir(ce);
}

int dircache_readdir_r(struct dircache_dirscan *dir, struct dirent *result)
{
    if (dircache_state != DIRCACHE_READY)
        return readdir_r(dir->###########3, result, &result);

    bool first = dir->dcinfo.scanidx == REWIND_INDEX;
    struct dircache_entry *ce = get_entry(first ? dir->dcinfo.index :
                                                  dir->dcinfo.scanidx);

    ce = first ? ce->down : ce->next;

    if (ce == NULL)
        return 0;

    dir->scanidx = ce - dircache_root;

    strlcpy(result->d_name, ce->d_name, sizeof (result->d_name));
    result->info = ce->dirinfo;

    return 1;
}

#endif /* DIRCACHE_* */

/**
 * reset the cache for the specified volume
 */
static void reset_volume(IF_MV_NONVOID(int volume))
{
    FOR_EACH_VOLUME(volume, i)
    {
        struct dircache_volume *dcvolp = DCVOL(i);

        if (dcvolp->status == DIRCACHE_IDLE)
            continue; /* idle => nothing happening there */

        struct dircache_runinfo_volume *dcrivolp = DCRIVOL(i);

        /* stop any scan and build on this one */
        if (dcrivolp->sabp)
            dcrivolp->sabp->quit = true;

    #ifdef HAVE_MULTIVOLUME
        /* if this call is for all volumes, subsequent code will just reset
           the cache memory usage and the freeing of individual entries may
           be skipped */
        if (volume >= 0)
            free_subentries(NULL, &dcvolp->root_down);
        else
    #endif
            binding_dissolve_volume(dcrivolp);

        /* set it back to unscanned */
        dcvolp->status      = DIRCACHE_IDLE;
        dcvolp->frontier    = FRONTIER_NEW;
        dcvolp->root_down   = 0;
        dcvolp->build_ticks = 0;
        dcvolp->serialnum   = 0;
    }
}

/**
 * reset the entire cache state for all volumes
 */
static void reset_cache(void)
{
    if (!dircache_runinfo.handle)
        return; /* no buffer => nothing cached */

    /* blast all the volumes */
    reset_volume(IF_MV(-1));

#ifdef DIRCACHE_DUMPSTER
    dumpster_clean_buffer(dircache_runinfo.p + ENTRYSIZE,
                          dircache_runinfo.bufsize);
#endif /* DIRCACHE_DUMPSTER */

    /* reset the memory */
    dircache.free_list    = 0;
    dircache.size         = 0;
    dircache.sizeused     = 0;
    dircache.numentries   = 0;
    dircache.names        = dircache_runinfo.bufsize + ENTRYSIZE;
    dircache.sizenames    = 0;
    dircache.namesfree    = 0;
    dircache.nextnamefree = 0;
    *get_name(dircache.names - 1) = 0;
    /* dircache.last_serialnum stays */
    /* dircache.reserve_used stays */
    /* dircache.last_size stays */
}

/**
 * checks each "idle" volume and builds it
 */
static void build_volumes(void)
{
    buffer_lock();

    for (int i = 0; i < NUM_VOLUMES; i++)
    {
        /* this does reader locking but we already own that */
        if (!volume_ismounted(IF_MV(i)))
            continue;

        struct dircache_volume *dcvolp = DCVOL(i);

        /* can't already be "scanning" because that's us; doesn't retry
           "ready" volumes */
        if (dcvolp->status == DIRCACHE_READY)
            continue;

        /* measure how long it takes to build the cache for each volume */
        if (!dcvolp->serialnum)
            dcvolp->serialnum = next_serialnum();

        dcvolp->status = DIRCACHE_SCANNING;
        dcvolp->start_tick = current_tick;

        sab_process_volume(dcvolp);

        if (dircache_runinfo.suspended)
            break;

        /* whatever happened, it's ready unless reset */
        dcvolp->build_ticks = current_tick - dcvolp->start_tick;
        dcvolp->status = DIRCACHE_READY;
    }

    size_t reserve_used = reserve_buf_used();
    if (reserve_used > dircache.reserve_used)
        dircache.reserve_used = reserve_used;

    if (DIRCACHE_STUFFED(reserve_used))
        dircache.last_size = 0; /* reset */
    else if (dircache.size > dircache.last_size ||
        dircache.last_size - dircache.size > DIRCACHE_RESERVE)
        dircache.last_size = dircache.size;

    logf("Done, %ld KiB used", dircache.size / 1024);

    buffer_unlock();
}

/**
 * allocate buffer and return whether or not a synchronous build should take
 * place; if 'realloced' is NULL, it's just a query about what will happen
 */
static int prepare_build(bool *realloced)
{
    /* called holding dircache lock */
    size_t size = dircache.last_size;

#ifdef HAVE_EEPROM_SETTINGS
    if (realloced)
    {
        dircache_unlock();
        remove_dircache_file();
        dircache_lock();

        if (dircache_runinfo.suspended)
            return -1;
    }
#endif /* HAVE_EEPROM_SETTINGS */

    bool stuffed = DIRCACHE_STUFFED(dircache.reserve_used);
    if (dircache_runinfo.bufsize > size && !stuffed)
    {
        if (realloced)
            *realloced = false;

        return 0; /* start a transparent rebuild */
    }

    int syncbuild = size > 0 && !stuffed ? 0 : 1;

    if (!realloced)
        return syncbuild;

    if (syncbuild)
    {
        /* start a non-transparent rebuild */
        /* we'll use the entire audiobuf to allocate the dircache */
        size = audio_buffer_available() + dircache_runinfo.bufsize;
        /* try to allocate at least the min and no more than the limit */
        size = MAX(DIRCACHE_MIN, MIN(size, DIRCACHE_LIMIT));
    }
    else
    {
        /* start a transparent rebuild */
        size = MAX(size, DIRCACHE_RESERVE) + DIRCACHE_RESERVE*2;
    }

    *realloced = true;
    reset_cache();

    buffer_lock();

    int handle = reset_buffer();
    dircache_unlock();

    if (handle > 0)
        core_free(handle);

    handle = alloc_cache(size);

    dircache_lock();

    if (dircache_runinfo.suspended && handle > 0)
    {
        /* if we got suspended, don't keep this huge buffer around */
        dircache_unlock();
        core_free(handle);
        handle = 0;
        dircache_lock();
    }

    if (handle <= 0)
    {
        buffer_unlock();
        return -1;
    }

    set_buffer(handle, size);
    buffer_unlock();

    return syncbuild;
}

/**
 * compact the dircache buffer after a successful full build
 */
static void compact_cache(void)
{
    /* called holding dircache lock */
    if (dircache_runinfo.suspended)
        return;

    void *p = dircache_runinfo.p + ENTRYSIZE;
    size_t leadsize = dircache.numentries * ENTRYSIZE + DIRCACHE_RESERVE;

    void *dst = p + leadsize;
    void *src = get_name(dircache.names - 1);
    if (dst >= src)
        return; /* cache got bigger than expected; never mind that */

    /* slide the names up in memory */
    memmove(dst, src, dircache.sizenames + 2);

    /* fix up name indexes */
    ptrdiff_t offset = dst - src;

    FOR_EACH_CACHE_ENTRY(ce)
    {
        if (!ce->tinyname)
             ce->name += offset;
    }

    dircache.names += offset;

    /* assumes beelzelib doesn't do things like calling callbacks or changing
       the pointer as a result of the shrink operation; it doesn't as of now
       but if it ever does that may very well cause deadlock problems since
       we're holding filesystem locks */
    size_t newsize = leadsize + dircache.sizenames + 1;
    core_shrink(dircache_runinfo.handle, p, newsize + 1);
    dircache_runinfo.bufsize = newsize;
    dircache.reserve_used = 0;
}

/**
 * internal thread that controls cache building; exits when no more requests
 * are pending or the cache is suspended
 */
static void dircache_thread(void)
{
    struct queue_event ev;

    /* calls made within the loop reopen the lock */
    dircache_lock();

    while (1)
    {
        queue_wait_w_tmo(&dircache_queue, &ev, 0);
        if (ev.id == SYS_TIMEOUT || dircache_runinfo.suspended)
        {
            /* nothing left to do/suspended */
            if (dircache_runinfo.suspended)
                clear_dircache_queue();
            dircache_runinfo.thread_done = true;
            break;
        }

        /* background-only builds are not allowed if a synchronous build is
           required first; test what needs to be done and if it checks out,
           do it for real below */
        int *rcp = (int *)ev.data;

        if (!rcp && prepare_build(NULL) > 0)
            continue;

        bool realloced;
        int rc = prepare_build(&realloced);
        if (rcp)
            *rcp = rc;

        if (rc < 0)
            continue;

        trigger_cpu_boost();
        build_volumes();

        /* if it was reallocated, compact it */
        if (realloced)
            compact_cache();
     }

     dircache_unlock();
}

/**
 * post a scan and build message to the thread, starting it if required
 */
static bool dircache_thread_post(int volatile *rcp)
{
    if (dircache_runinfo.thread_done)
    {
        /* mustn't recreate until it exits so that the stack isn't reused */
        thread_wait(dircache_runinfo.thread_id);
        dircache_runinfo.thread_done = false;
        dircache_runinfo.thread_id = create_thread(
            dircache_thread, dircache_stack, sizeof (dircache_stack), 0,
            dircache_thread_name IF_PRIO(, PRIORITY_BACKGROUND)
            IF_COP(, CPU));
    }

    bool started = dircache_runinfo.thread_id != 0;

    if (started)
        queue_post(&dircache_queue, DCM_BUILD, (intptr_t)rcp);

    return started;
}

/**
 * wait for the dircache thread to finish building; intended for waiting for a
 * non-transparent build to finish when dircache_resume() returns > 0
 */
void dircache_wait(void)
{
    thread_wait(dircache_runinfo.thread_id);
}

/**
 * call after mounting a volume or all volumes
 */
void dircache_mount(void)
{
    /* call with writer exclusion */
    if (dircache_runinfo.suspended)
        return;

    dircache_thread_post(NULL);
}

/**
 * call after unmounting a volume; specifying < 0 for all or >= 0 for the
 * specific one
 */
void dircache_unmount(IF_MV_NONVOID(int volume))
{
    /* call with writer exclusion */
    if (dircache_runinfo.suspended)
        return;

#ifdef HAVE_MULTIVOLUME
    if (volume >= 0)
        reset_volume(volume);
    else
#endif /* HAVE_MULTIVOLUME */
        reset_cache();
}

/* backend to dircache_suspend() and dircache_disable() */
static void dircache_suspend_internal(bool freeit)
{
    if (dircache_runinfo.suspended++ > 0 &&
        (!freeit || dircache_runinfo.handle <= 0))
        return;

    unsigned int thread_id = dircache_runinfo.thread_id;

    reset_cache();
    clear_dircache_queue();

    /* grab the buffer away into our control; the cache won't need it now */
    int handle = 0;
    if (freeit)
        handle = reset_buffer();

    dircache_unlock();

    if (handle > 0)
        core_free(handle);

    thread_wait(thread_id);

    dircache_lock();
}

/* backend to dircache_resume() and dircache_enable() */
static int dircache_resume_internal(bool build_now)
{
    int volatile rc = 0;

    if (dircache_runinfo.suspended == 0 || --dircache_runinfo.suspended == 0)
        rc = build_now && dircache_runinfo.enabled ? 1 : 0;

    if (rc)
        rc = dircache_thread_post(&rc) ? INT_MIN : -1;

    if (rc == INT_MIN)
    {
        dircache_unlock();

        while (rc == INT_MIN) /* poll for response */
            sleep(0);

        dircache_lock();
    }

    return rc < 0 ? rc * 10 - 2 : rc;
}

/**
 * service to dircache_enable() and dircache_load(); "build_now" starts a build
 * immediately if the cache was not enabled
 */
static int dircache_enable_internal(bool build_now)
{
    int rc = 0;

    if (!dircache_runinfo.enabled)
    {
        dircache_runinfo.enabled = true;
        rc = dircache_resume_internal(build_now);
    }

    return rc;
}

/**
 * service to dircache_disable()
 */
static void dircache_disable_internal(void)
{
    if (dircache_runinfo.enabled)
    {
        dircache_runinfo.enabled = false;
        dircache_suspend_internal(true);
    }
}

/**
 * disables dircache without freeing the buffer (so it can be re-enabled
 * afterwards with dircache_resume(); usually called when accepting an USB
 * connection
 */
void dircache_suspend(void)
{
    dircache_lock();
    dircache_suspend_internal(false);
    dircache_unlock();
}

/**
 * re-enables the dircache if previously suspended by dircache_suspend
 * or dircache_steal_buffer(), re-using the already allocated buffer if
 * available
 *
 * returns: 0 if the background build is started or dircache is still
 *          suspended
 *          > 0 if the build is non-background
 *          < 0 upon failure
 */
int dircache_resume(void)
{
    dircache_lock();
    int rc = dircache_resume_internal(true);
    dircache_unlock();
    return rc;
}

/**
 * as dircache_resume() but globally enables it; called by settings and init
 */
int dircache_enable(void)
{
    dircache_lock();
    int rc = dircache_enable_internal(true);
    dircache_unlock();
    return rc;
}

/**
 * as dircache_suspend() but also frees the buffer; usually called on shutdown
 * or when deactivated
 */
void dircache_disable(void)
{
    dircache_lock();
    dircache_disable_internal();
    dircache_unlock();
}

/**
 * have dircache give up its allocation; call dircache_resume() to restart it
 */
void dircache_free_buffer(void)
{
    dircache_lock();
    dircache_suspend_internal(true);
    dircache_unlock();
}


/** Dircache live updating **/

/**
 * obtain binding information for the file's root volume; this is the starting
 * point for internal path parsing and binding
 */
void dircache_get_rootinfo(struct file_base_info *infop)
{
    int volume = BASEINFO_VOL(infop);
    struct dircache_volume *dcvolp = DCVOL(volume);

    if (dcvolp->serialnum)
    {
        /* root has a binding */
        infop->dcfile.idx       = -volume - 1;
        infop->dcfile.serialnum = dcvolp->serialnum;
    }
    else
    {
        /* root is idle */
        dircache_dcfile_init(&infop->dcfile);
    }
}

/**
 * called by file code when the first reference to a file or directory is
 * opened
 */
void dircache_bind_file(struct file_base_binding *bindp)
{
    /* requires write exclusion */
    logf("dc open: %u", (unsigned int)bindp->info.dcfile.serialnum);
    binding_open(bindp);
}

/**
 * called by file code when the last reference to a file or directory is
 * closed
 */
void dircache_unbind_file(struct file_base_binding *bindp)
{
    /* requires write exclusion */
    logf("dc close: %u", (unsigned int)bindp->info.dcfile.serialnum);
    binding_close(bindp);
}

/**
 * called by file code when a file is newly created
 */
void dircache_fileop_create(struct file_base_info *dirinfop,
                            struct file_base_binding *bindp,
                            const char *basename,
                            const struct dirinfo_native *dinp)
{
    /* requires write exclusion */
    logf("dc create: %u \"%s\"",
         (unsigned int)bindp->info.dcfile.serialnum, basename);

    if (!dirinfop->dcfile.serialnum)
    {
        /* no parent binding => no child binding */
        return;
    }

    struct dircache_entry *ce;
    int idx = create_entry(basename, &ce);
    if (idx == 0)
    {
        /* failed allocation; parent cache contents are not complete */
        establish_frontier(dirinfop->dcfile.idx, FRONTIER_ZONED);
        return;
    }

    struct file_base_info *infop = &bindp->info;

#ifdef DIRCACHE_NATIVE
    ce->firstcluster = infop->fatfile.firstcluster;
    ce->direntry     = infop->fatfile.e.entry;
    ce->direntries   = infop->fatfile.e.entries;
    ce->wrtdate      = dinp->wrtdate;
    ce->wrttime      = dinp->wrttime;
#else
    ce->mtime        = dinp->mtime;
#endif
    ce->attr         = dinp->attr;
    if (!(dinp->attr & ATTR_DIRECTORY))
        ce->filesize = dinp->size;

    insert_file_entry(dirinfop, ce);

    /* file binding will have been queued when it was opened; just resolve */
    infop->dcfile.idx       = idx;
    infop->dcfile.serialnum = ce->serialnum;
    binding_resolve(infop);

    if ((dinp->attr & ATTR_DIRECTORY) && !is_dotdir_name(basename))
    {
        /* scan-in the contents of the new directory at this level only */
        buffer_lock();
        sab_process_dir(infop, false);
        buffer_unlock();
    }
}

/**
 * called by file code when a file or directory is removed
 */
void dircache_fileop_remove(struct file_base_binding *bindp)
{
    /* requires write exclusion */
    logf("dc remove: %u\n", (unsigned int)bindp->info.dcfile.serialnum);

    if (!bindp->info.dcfile.serialnum)
        return; /* no binding yet */

    free_file_entry(&bindp->info);

    /* if binding was resolved; it should now be queued via above call */
}

/**
 * called by file code when a file is renamed
 */
void dircache_fileop_rename(struct file_base_info *dirinfop,
                            struct file_base_binding *bindp,
                            const char *basename)
{
    /* requires write exclusion */
    logf("dc rename: %u \"%s\"",
         (unsigned int)bindp->info.dcfile.serialnum, basename);

    if (!dirinfop->dcfile.serialnum)
    {
        /* new parent directory not cached; there is nowhere to put it so
           nuke it */
        if (bindp->info.dcfile.serialnum)
            free_file_entry(&bindp->info);
        /* else no entry anyway */

        return;
    }

    if (!bindp->info.dcfile.serialnum)
    {
        /* binding not resolved on the old file but it's going into a resolved
           parent which means the parent would be missing an entry in the cache;
           downgrade the parent */
        establish_frontier(dirinfop->dcfile.idx, FRONTIER_ZONED);
        return;
    }

    /* unlink the entry but keep it; it needs to be re-sorted since the
       underlying FS probably changed the order */
    struct dircache_entry *ce = remove_file_entry(&bindp->info);

#ifdef DIRCACHE_NATIVE
    /* update other name-related information before inserting */
    ce->direntry   = bindp->info.fatfile.e.entry;
    ce->direntries = bindp->info.fatfile.e.entries;
#endif

    /* place it into its new home */
    insert_file_entry(dirinfop, ce);

    /* lastly, update the entry name itself */
    if (entry_reassign_name(ce, basename))
    {
        /* it's not really the same one now so re-stamp it */
        dc_serial_t serialnum = next_serialnum();
        ce->serialnum = serialnum;
        bindp->info.dcfile.serialnum = serialnum;
    }
    else
    {
        /* it cannot be kept around without a valid name */
        free_file_entry(&bindp->info);
        establish_frontier(dirinfop->dcfile.idx, FRONTIER_ZONED);
    }
}

/**
 * called by file code to synchronize file entry information
 */
void dircache_fileop_sync(struct file_base_binding *bindp,
                          const struct dirinfo_native *dinp)
{
    /* requires write exclusion */
    struct file_base_info *infop = &bindp->info;
    logf("dc sync: %u\n", (unsigned int)infop->dcfile.serialnum);

    if (!infop->dcfile.serialnum)
        return; /* binding unresolved */

    struct dircache_entry *ce = get_entry(infop->dcfile.idx);
    if (!ce)
    {
        logf("  bad index %d", infop->dcfile.idx);
        return; /* a root (should never be called for this) */
    }

#ifdef DIRCACHE_NATIVE
    ce->firstcluster = infop->fatfile.firstcluster;
    ce->wrtdate      = dinp->wrtdate;
    ce->wrttime      = dinp->wrttime;
#else
    ce->mtime        = dinp->mtime;
#endif
    ce->attr         = dinp->attr;
    if (!(dinp->attr & ATTR_DIRECTORY))
        ce->filesize = dinp->size;
}


/** Dircache paths and files **/

#ifdef DIRCACHE_DUMPSTER
/* helper for dircache_get_path() */
static ssize_t get_path_sub(int idx, char *buf, size_t size)
{
    if (idx == 0)
        return -2; /* entry is an orphan split from any root */

    ssize_t offset;
    char *cename;

    if (idx > 0)
    {
        /* go all the way up then move back down from the root */
        struct dircache_entry *ce = get_entry(idx);
        offset = get_path_sub(ce->up, buf, size) - 1;
        if (offset < 0)
            return -3;

        cename = alloca(MAX_NAME + 1);
        entry_name_copy(cename, ce);
    }
    else /* idx < 0 */
    {
        offset = 0;
        cename = "";

    #ifdef HAVE_MULTIVOLUME
        int volume = IF_MV_VOL(-idx - 1);
        if (volume > 0)
        {
            /* prepend the volume specifier for volumes > 0 */
            cename = alloca(VOL_MAX_LEN+1);
            get_volume_name(volume, cename);
        }
    #endif /* HAVE_MULTIVOLUME */
    }

    return offset + path_append(buf + offset, PA_SEP_HARD, cename,
                                size > (size_t)offset ? size - offset : 0);
}
#endif /* DIRCACHE_DUMPSTER */

#if 0

/**
 * retrieve and validate the file's entry/binding serial number
 * the dircache file's serial number must match the indexed entry's or the
 * file reference is stale
 */
static dc_serial_t get_file_serialnum(const struct dircache_file *dcfilep)
{
    int idx = dcfilep->idx;

    if (idx == 0 || idx < -NUM_VOLUMES)
        return 0;

    dc_serial_t serialnum;

    if (idx > 0)
    {
        struct dircache_entry *ce = get_entry(idx);
        serialnum = ce ? ce->serialnum : 0;
    }
    else
    {
        serialnum = get_idx_dcvolp(idx)->serialnum;
    }

    return serialnum == dcfilep->serialnum ? serialnum : 0;
}

/**
 * usermode function to construct a full absolute path from dircache into the
 * given buffer given the dircache file info
 *
 * returns:
 *   success - the length of the string, not including the trailing null
 *   failure - a negative value
 *
 * successful return value is as strlcpy()
 *
 * errors:
 *   ENOENT - the file or directory does not exist
 */
ssize_t dircache_get_path(const struct dircache_file *dcfilep, char *buf,
                          size_t size)
{
    /* if missing buffer space, still return what's needed a la strlcpy */
    if (!buf)
        size = 0;
    else if (size)
        *buf = '\0';

    ssize_t len = -1;

    dircache_lock();

    /* first and foremost, there must be a cache and the serial number must
       check out */
    if (dircache_runinfo.handle && get_file_serialnum(dcfilep))
        len = get_path_sub(dcfilep->idx, buf, size);

    if (len < 0)
        errno = ENOENT;

    dircache_unlock();
    return len;
}

/**
 * searches the sublist starting at 'idx' for the named component
 */

/* helper for get_file_sub() */
static struct dircache_entry *
get_file_sub_scan(int idx, const char *name, size_t length, int *idxp)
{
    struct dircache_entry *ce = get_entry(idx);
    if (ce)
    {
        char entname[MAX_NAME+1];
        name = strmemdupa(name, length);

        do
        {
            entry_name_copy(entname, ce);
            if (!strcasecmp(entname, name))
            {
                *idxp = idx;
                break;
            }

            idx = ce->next;
        }
        while ((ce = get_entry(idx)));
    }

    return ce;
}

/**
 * searches for the subcomponent of *pathp
 */

/* helper for dircache_get_file() */
static int get_file_sub(const char **pathp, int *downp, int *idxp)
{
    int rc;
    const char *name;
    rc = parse_path_component(pathp, &name, false);
    if (rc <= 0)
        return rc;
    else if (rc >= MAX_PATH)
        return ENAMETOOLONG; /* that's just unpossible, man */

    struct dircache_entry *ce = get_file_sub_scan(*downp, name, rc, idxp);

    if (!ce)
        rc = RC_NOT_FOUND;   /* not there; tellibry solly */
    else if (!*pathp)
        rc = RC_PATH_ENDED;  /* done */
    else if (!(ce->attr & ATTR_DIRECTORY))
        rc = ENOTDIR;        /* a parent component must be a directory */
    else
        while ((rc = get_file_sub(pathp, &ce->down, idxp)) == RC_CONTINUE);

    switch (rc)
    {
    case RC_GO_UP:           /* hit ".."; drop to previous level */
        return RC_CONTINUE;
    case RC_PATH_ENDED:      /* success! */
        return RC_FOUND;
    default:                 /* component not found or error */
        return rc;
    }
}

/**
 * usermode function to return dircache file info for the given path
 *
 * returns:
 *   success: the volume number that is specified for the file
 *   failure: a negative value
 *
 * errors:
 *   ENOENT       - the file or directory does not exist or path is empty
 *   ENAMETOOLONG - a component of the path is too long
 *   ENOTDIR      - a component of the path is not a directory
 */
int dircache_get_file(const char *path, struct dircache_file *dcfilep)
{
    if (!path_is_absolute(path) || !dcfilep)
    {
        errno = ENOENT;
        return -1;
    }

    dircache_lock();

    if (!dircache_runinfo.handle)
    {
        dircache_unlock();
        errno = ENOENT;
        return -2;
    }

    int volume = 0;
    int idx = 0;
    dc_serial_t serialnum = 0;
    struct dircache_volume *dcvolp = NULL;
    struct dircache_entry *ce = NULL;

    int rc = RC_GO_UP;

    while (rc == RC_CONTINUE || rc == RC_GO_UP)
    {
    #ifdef HAVE_MULTIVOLUME
        if (rc == RC_GO_UP)
        {
            volume = path_strip_volume(path, &path, false);
            if (!CHECK_VOL(volume))
            {
                rc = ENXIO;
                break;
            }
        }
    #endif /* HAVE_MULTIVOLUME */

        dcvolp = DCVOL(volume);

        int *downp = &dcvolp->root_down;
        if (*downp <= 0)
        {
            rc = ENXIO;
            break;
        }

        rc = get_file_sub(&path, downp, &idx);
    }

    switch (rc)
    {
    case RC_FOUND:      /* hit: component found */
        serialnum = ce->serialnum;
        rc = volume;
        break;
    case RC_PATH_ENDED: /* hit: it's a root (volume or system) */
        idx = -volume - 1;
        serialnum = dcvolp->serialnum;
        rc = volume;
        break;
    case RC_NOT_FOUND:  /* miss */
        rc = ENOENT;
    default:
        idx = 0;
        errno = rc;
        rc = -3;
        break;
    }

    dcfilep->idx       = idx;
    dcfilep->serialnum = serialnum;

    dircache_unlock();
    return rc;
}
#endif /* 0 */


/** Debug screen/info stuff **/

/**
 * return cache state parameters
 */
void dircache_get_info(struct dircache_info *info)
{
    static const char * const status_descriptions[] =
    {
        [DIRCACHE_IDLE]     = "Idle",
        [DIRCACHE_SCANNING] = "Scanning",
        [DIRCACHE_READY]    = "Ready",
    };

    if (!info)
        return;

    memset(info, 0, sizeof (*info));

    dircache_lock();

    enum dircache_status status = DIRCACHE_IDLE;

    for (unsigned int volume = 0; volume < NUM_VOLUMES; volume++)
    {
        struct dircache_volume *dcvolp = DCVOL(volume);
        enum dircache_status volstatus = dcvolp->status;

        switch (volstatus)
        {
        case DIRCACHE_SCANNING:
            /* if any one is scanning then overall status is "scanning" */
            status = volstatus;

            /* sum the time the scanning has taken so far */
            info->build_ticks += current_tick - dcvolp->start_tick;
            break;
        case DIRCACHE_READY:
            /* if all the rest are idle and at least one is ready, then
               status is "ready". */
            if (status == DIRCACHE_IDLE)
                status = DIRCACHE_READY;

            /* sum the build ticks of all "ready" volumes */
            info->build_ticks += dcvolp->build_ticks;
            break;
        case DIRCACHE_IDLE:
            /* if all are idle; then the whole cache is "idle" */
            break;
        }
    }

    info->status     = status;
    info->statusdesc = status_descriptions[status];
    info->last_size  = dircache.last_size;
    info->size_limit = DIRCACHE_LIMIT;
    info->reserve    = DIRCACHE_RESERVE;

    /* report usage only if there is something ready or being built */
    if (status != DIRCACHE_IDLE)
    {
        info->reserve_used = reserve_buf_used();
        info->size         = dircache.size;
        info->sizeused     = dircache.sizeused;
        info->entry_count  = dircache.numentries;
    }

    dircache_unlock();
}

#ifdef DIRCACHE_DUMPSTER
/**
 * dump RAW binary of buffer and CSV of all valid paths and volumes to disk
 */
void dircache_dump(void)
{
    /* open both now so they're in the cache */
    int fdbin = open(DIRCACHE_DUMPSTER_BIN, O_WRONLY|O_CREAT|O_TRUNC, 0666);
    int fdcsv = open(DIRCACHE_DUMPSTER_CSV, O_WRONLY|O_CREAT|O_TRUNC, 0666);
    if (fdbin < 0 || fdcsv < 0)
    {
        close(fdbin);
        return;
    }

    trigger_cpu_boost();
    dircache_lock();

    if (dircache_runinfo.handle)
    {
        buffer_lock();

        /* bin */
        write(fdbin, dircache_runinfo.p + ENTRYSIZE,
              dircache_runinfo.bufsize + 1);

        /* CSV */
        fdprintf(fdcsv, "\"Index\",\"Serialnum\","
                        "\"Path\",\"Frontier\","
                        "\"Attribute\",\"File Size\","
                        "\"Mod Date\",\"Mod Time\"\n");

        FOR_EACH_VOLUME(-1, volume)
        {
            struct dircache_volume *dcvolp = DCVOL(volume);
            if (dcvolp->status == DIRCACHE_IDLE)
                continue;

        #ifdef HAVE_MULTIVOLUME
            char name[VOL_MAX_LEN+1];
            get_volume_name(volume, name);
        #endif
            fdprintf(fdcsv,
                     "%d,%lu,"
                     "\"%c" IF_MV("%s") "\",%u,"
                     "0x%08X,0,"
                     "\"\",\"\"\n",
                     -volume-1, dcvolp->serialnum,
                     PATH_SEPCH, IF_MV(name,) dcvolp->frontier,
                     ATTR_DIRECTORY | ATTR_VOLUME);
        }

        FOR_EACH_CACHE_ENTRY(ce)
        {
        #ifdef DIRCACHE_NATIVE
            time_t mtime = fattime_mktime(ce->wrtdate, ce->wrttime);
        #else
            time_t mtime = ce->mtime;
        #endif
            struct tm tm;
            gmtime_r(&mtime, &tm);

            char buf[DC_MAX_NAME + 2];
            *buf = '\0';
            int idx = get_index(ce);
            get_path_sub(idx, buf, sizeof (buf));

            fdprintf(fdcsv,
                     "%d,%lu,"
                     "\"%s\",%u,"
                     "0x%08X,%lu,"
                     "%04d/%02d/%02d,"
                     "%02d:%02d:%02d\n",
                     idx, ce->serialnum,
                     buf, ce->frontier,
                     ce->attr, (ce->attr & ATTR_DIRECTORY) ?
                                0ul : (unsigned long)ce->filesize,
                     tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
                     tm.tm_hour, tm.tm_min, tm.tm_sec);
        }

        buffer_unlock();
    }

    dircache_unlock();
    cancel_cpu_boost();

    close(fdbin);
    close(fdcsv);
}
#endif /* DIRCACHE_DUMPSTER */


/** Misc. stuff **/

/**
 * set the dircache file to initial values
 */
void dircache_dcfile_init(struct dircache_file *dcfilep)
{
    dcfilep->idx       = 0;
    dcfilep->serialnum = 0;
}

#ifdef HAVE_EEPROM_SETTINGS

#ifdef HAVE_HOTSWAP
/* NOTE: This is hazardous to the filesystem of any sort of removable
         storage unless it may be determined that the filesystem from save
         to load is identical. If it's not possible to do so in a timely
         manner, it's not worth persisting the cache. */
  #warning "Don't do this; you'll find the consequences unpleasant."
#endif

/* dircache persistence file header magic */
#define DIRCACHE_MAGIC  0x00d0c0a1

/* dircache persistence file header */
struct dircache_maindata
{
    uint32_t        magic;      /* DIRCACHE_MAGIC */
    struct dircache dircache;   /* metadata of the cache! */
    uint32_t        datacrc;    /* CRC32 of data */
    uint32_t        hdrcrc;     /* CRC32 of header through datacrc */
} __attribute__((packed, aligned (4)));

/**
 * verify that the clean status is A-ok
 */
static bool dircache_is_clean(bool saving)
{
    if (saving)
        return dircache.dcvol[0].status == DIRCACHE_READY;
    else
    {
        return dircache.dcvol[0].status == DIRCACHE_IDLE &&
               !dircache_runinfo.enabled;
    }
}

/**
 * function to load the internal cache structure from disk to initialize
 * the dircache really fast with little disk access.
 */
int dircache_load(void)
{
    logf("Loading directory cache");
    int fd = open_dircache_file(O_RDONLY);
    if (fd < 0)
        return -1;

    int rc = -1;

    ssize_t size;
    struct dircache_maindata maindata;
    uint32_t crc;
    int handle = 0;
    bool hasbuffer = false;

    size = sizeof (maindata);
    if (read(fd, &maindata, size) != size)
    {
        logf("dircache: header read failed");
        goto error_nolock;
    }

    /* sanity check the header */
    if (maindata.magic != DIRCACHE_MAGIC)
    {
        logf("dircache: invalid header magic");
        goto error_nolock;
    }

    crc = crc_32(&maindata, offsetof(struct dircache_maindata, hdrcrc),
                 0xffffffff);
    if (crc != maindata.hdrcrc)
    {
        logf("dircache: invalid header CRC32");
        goto error_nolock;
    }

    if (maindata.dircache.size !=
            maindata.dircache.sizeentries + maindata.dircache.sizenames ||
        ALIGN_DOWN(maindata.dircache.size, ENTRYSIZE) != maindata.dircache.size ||
        filesize(fd) - sizeof (maindata) != maindata.dircache.size)
    {
        logf("dircache: file header error");
        goto error_nolock;
    }

    /* allocate so that exactly the reserve size remains */
    size_t bufsize = maindata.dircache.size + DIRCACHE_RESERVE + 1;
    handle = alloc_cache(bufsize);
    if (handle <= 0)
    {
        logf("dircache: failed load allocation");
        goto error_nolock;
    }

    dircache_lock();
    buffer_lock();

    if (!dircache_is_clean(false))
        goto error;

    /* from this point on, we're actually dealing with the cache in RAM */
    dircache = maindata.dircache;

    set_buffer(handle, bufsize);
    hasbuffer = true;

    /* convert back to in-RAM representation */
    dircache.numentries = maindata.dircache.sizeentries / ENTRYSIZE;

    /* read the dircache file into memory; start with the entries */
    size = maindata.dircache.sizeentries;
    if (read(fd, dircache_runinfo.pentry + 1, size) != size)
    {
        logf("dircache read failed #1");
        goto error;
    }

    crc = crc_32(dircache_runinfo.pentry + 1, size, 0xffffffff);

    /* continue with the names; fix up indexes to them if needed */
    dircache.names -= maindata.dircache.sizenames;
    *get_name(dircache.names - 1) = 0;

    size = maindata.dircache.sizenames;
    if (read(fd, get_name(dircache.names), size) != size)
    {
        logf("dircache read failed #2");
        goto error;
    }

    crc = crc_32(get_name(dircache.names), size, crc);
    if (crc != maindata.datacrc)
    {
        logf("dircache: data failed CRC32");
        goto error;
    }

    /* only names will be changed in relative position so fix up those
       references */
    ssize_t offset = dircache.names - maindata.dircache.names;
    if (offset != 0)
    {
        /* nothing should be open besides the dircache file itself therefore
           no bindings need be resolved; the cache will have its own entry
           but that should get cleaned up when removing the file */
        FOR_EACH_CACHE_ENTRY(ce)
        {
            if (!ce->tinyname)
                ce->name += offset;
        }
    }

    dircache.reserve_used = 0;

    /* enable the cache but do not try to build it */
    dircache_enable_internal(false);

    /* cache successfully loaded */
    logf("Done, %ld KiB used", dircache.size / 1024);
    rc = 0;
error:
    if (rc < 0 && hasbuffer)
        reset_buffer();

    buffer_unlock();
    dircache_unlock();

error_nolock:
    if (rc < 0 && handle > 0)
        core_free(handle);

    if (fd >= 0)
        close(fd);

    remove_dircache_file();
    return rc;
}

/**
 * function to save the internal cache stucture to disk for fast loading
 * on boot
 */
int dircache_save(void)
{
    logf("Saving directory cache");

    int fd = open_dircache_file(O_WRONLY|O_CREAT|O_TRUNC|O_APPEND);
    if (fd < 0)
        return -1;

    dircache_lock();
    buffer_lock();

    int rc = -1;

    if (!dircache_is_clean(true))
        goto error;

    /* save the header structure along with the cache metadata */
    ssize_t size;
    uint32_t crc;
    struct dircache_maindata maindata =
    {
        .magic    = DIRCACHE_MAGIC,
        .dircache = dircache,
    };

    /* store the size since it better detects an invalid header */
    maindata.dircache.sizeentries = maindata.dircache.numentries * ENTRYSIZE;

    /* write the template header */
    size = sizeof (maindata);
    if (write(fd, &maindata, size) != size)
    {
        logf("dircache: write failed #1");
        goto error;
    }

    /* write the dircache entries */
    size = maindata.dircache.sizeentries;
    if (write(fd, dircache_runinfo.pentry + 1, size) != size)
    {
        logf("dircache: write failed #2");
        goto error;
    }

    crc = crc_32(dircache_runinfo.pentry + 1, size, 0xffffffff);

    /* continue with the names */
    size = maindata.dircache.sizenames;
    if (write(fd, get_name(dircache.names), size) != size)
    {
        logf("dircache: write failed #3");
        goto error;
    }

    crc = crc_32(get_name(dircache.names), size, crc);
    maindata.datacrc = crc;

    /* rewrite the header with CRC info */
    if (lseek(fd, 0, SEEK_SET) != 0)
    {
        logf("dircache: seek failed");
        goto error;
    }

    crc = crc_32(&maindata, offsetof(struct dircache_maindata, hdrcrc),
                 0xffffffff);
    maindata.hdrcrc = crc;

    if (write(fd, &maindata, sizeof (maindata)) != sizeof (maindata))
    {
        logf("dircache: write failed #4");
        goto error;
    }

    /* as of now, no changes to the volumes should be allowed at all since
       that makes what was saved completely invalid */
    rc = 0;
error:
    buffer_unlock();
    dircache_unlock();

    if (rc < 0)
        remove_dircache_file();

    close(fd);
    return rc;
}
#endif /* HAVE_EEPROM_SETTINGS */

/**
 * main one-time initialization function that must be called before any other
 * operations within the dircache
 */
void dircache_init(size_t last_size)
{
    queue_init(&dircache_queue, false);

    dircache.last_size = MIN(last_size, DIRCACHE_LIMIT);

    struct dircache_runinfo *dcrip = &dircache_runinfo;
    dcrip->suspended         = 1;
    dcrip->thread_done       = true;
    dcrip->ops.move_callback = move_callback;
}