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
|
/*
* Copyright (c) 2006-2025 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-05-05 RTT Implement mnt in dfs v2.0
* 2023-10-23 Shell fix synchronization of data to icache
*/
#define DBG_TAG "dfs.pcache"
#define DBG_LVL DBG_WARNING
#include <rtdbg.h>
#include <dfs_pcache.h>
#include <dfs_dentry.h>
#include <dfs_mnt.h>
#include <rthw.h>
#ifdef RT_USING_PAGECACHE
#include <mm_page.h>
#include <mm_private.h>
#include <mmu.h>
#include <tlb.h>
#ifndef RT_PAGECACHE_COUNT
#define RT_PAGECACHE_COUNT 4096
#endif
#ifndef RT_PAGECACHE_ASPACE_COUNT
#define RT_PAGECACHE_ASPACE_COUNT 1024
#endif
#ifndef RT_PAGECACHE_PRELOAD
#define RT_PAGECACHE_PRELOAD 4
#endif
#ifndef RT_PAGECACHE_GC_WORK_LEVEL
#define RT_PAGECACHE_GC_WORK_LEVEL 90
#endif
#ifndef RT_PAGECACHE_GC_STOP_LEVEL
#define RT_PAGECACHE_GC_STOP_LEVEL 70
#endif
#define PCACHE_MQ_GC 1
#define PCACHE_MQ_WB 2
struct dfs_aspace_mmap_obj
{
rt_uint32_t cmd;
struct rt_mailbox *ack;
struct dfs_file *file;
struct rt_varea *varea;
void *data;
};
struct dfs_pcache_mq_obj
{
struct rt_mailbox *ack;
rt_uint32_t cmd;
};
static struct dfs_page *dfs_page_lookup(struct dfs_file *file, off_t pos);
static void dfs_page_ref(struct dfs_page *page);
static int dfs_page_inactive(struct dfs_page *page);
static int dfs_page_remove(struct dfs_page *page);
static void dfs_page_release(struct dfs_page *page);
static int dfs_page_dirty(struct dfs_page *page);
static int dfs_aspace_release(struct dfs_aspace *aspace);
static int dfs_aspace_lock(struct dfs_aspace *aspace);
static int dfs_aspace_unlock(struct dfs_aspace *aspace);
static int dfs_pcache_lock(void);
static int dfs_pcache_unlock(void);
static struct dfs_pcache __pcache;
/**
* @brief Perform garbage collection on an address space to release pages
*
* This function attempts to release a specified number of pages from both inactive
* and active lists of the given address space. It prioritizes releasing pages from
* the inactive list first before moving to the active list.
*
* @param[in] aspace Pointer to the address space structure to perform GC on
* @param[in] count Number of pages to attempt to release
*
* @return Number of pages actually released (count - remaining)
*/
static int dfs_aspace_gc(struct dfs_aspace *aspace, int count)
{
int cnt = count;
if (aspace)
{
dfs_aspace_lock(aspace);
if (aspace->pages_count > 0)
{
struct dfs_page *page = RT_NULL;
rt_list_t *node = aspace->list_inactive.next;
while (cnt && node != &aspace->list_active)
{
page = rt_list_entry(node, struct dfs_page, space_node);
node = node->next;
if (dfs_page_remove(page) == 0)
{
cnt --;
}
}
node = aspace->list_active.next;
while (cnt && node != &aspace->list_inactive)
{
page = rt_list_entry(node, struct dfs_page, space_node);
node = node->next;
if (dfs_page_remove(page) == 0)
{
cnt --;
}
}
}
dfs_aspace_unlock(aspace);
}
return count - cnt;
}
/**
* @brief Release page cache entries to free up memory
*
* This function attempts to release a specified number of page cache entries.
* If count is 0, it calculates the number of pages to release based on the
* current cache size and GC stop level. It first tries to release from inactive
* list, then from active list if needed.
*
* @param[in] count Number of pages to release. If 0, calculates automatically
* based on current cache size and GC stop level.
*
* @note The function uses LRU (Least Recently Used) policy by prioritizing
* inactive list over active list.
*/
void dfs_pcache_release(size_t count)
{
rt_list_t *node = RT_NULL;
struct dfs_aspace *aspace = RT_NULL;
dfs_pcache_lock();
if (count == 0)
{
count = rt_atomic_load(&(__pcache.pages_count)) - RT_PAGECACHE_COUNT * RT_PAGECACHE_GC_STOP_LEVEL / 100;
}
node = __pcache.list_inactive.next;
while (count && node != &__pcache.list_active)
{
aspace = rt_list_entry(node, struct dfs_aspace, cache_node);
node = node->next;
if (aspace)
{
count -= dfs_aspace_gc(aspace, count);
dfs_aspace_release(aspace);
}
}
node = __pcache.list_active.next;
while (count && node != &__pcache.list_inactive)
{
aspace = rt_list_entry(node, struct dfs_aspace, cache_node);
node = node->next;
if (aspace)
{
count -= dfs_aspace_gc(aspace, count);
}
}
dfs_pcache_unlock();
}
/**
* @brief Clean up page cache entries for a specific mount point
*
* This function iterates through both inactive and active lists of the page cache
* to clean up entries associated with the given mount point. It performs cleanup
* and calls the provided callback function for each matching address space.
*
* @param[in] mnt Pointer to the mount point structure to clean up
* @param[in] cb Callback function to be called for each matching address space
* The callback takes an address space pointer and returns an integer
*/
static void _pcache_clean(struct dfs_mnt *mnt, int (*cb)(struct dfs_aspace *aspace))
{
rt_list_t *node = RT_NULL;
struct dfs_aspace *aspace = RT_NULL;
dfs_pcache_lock();
node = __pcache.list_inactive.next;
while (node != &__pcache.list_active)
{
aspace = rt_list_entry(node, struct dfs_aspace, cache_node);
node = node->next;
if (aspace && aspace->mnt == mnt)
{
dfs_aspace_clean(aspace);
cb(aspace);
}
}
node = __pcache.list_active.next;
while (node != &__pcache.list_inactive)
{
aspace = rt_list_entry(node, struct dfs_aspace, cache_node);
node = node->next;
if (aspace && aspace->mnt == mnt)
{
dfs_aspace_clean(aspace);
cb(aspace);
}
}
dfs_pcache_unlock();
}
/**
* @brief Unmount and clean up page cache for a specific mount point
*
* This function cleans up all page cache entries associated with the given mount point
* by calling _pcache_clean() with dfs_aspace_release as the callback function.
* It will release all address spaces and their pages belonging to this mount point.
*
* @param[in] mnt Pointer to the mount point structure to be unmounted
*
* @note This function is typically called during filesystem unmount operation
* @see _pcache_clean()
*/
void dfs_pcache_unmount(struct dfs_mnt *mnt)
{
_pcache_clean(mnt, dfs_aspace_release);
}
static int _dummy_cb(struct dfs_aspace *mnt)
{
return 0;
}
/**
* @brief Clean page cache for a specific mount point without releasing address spaces
*
* This function cleans up all page cache entries associated with the given mount point
* but keeps the address spaces intact by using a dummy callback function.
*
* @param[in] mnt Pointer to the mount point structure to be cleaned
*
* @note Typical usage scenarios:
* - Filesystem maintenance operations that require cache invalidation
* - Force refreshing cached data without unmounting
* - Handling external modifications to mounted filesystems
*
* @see _pcache_clean()
*/
void dfs_pcache_clean(struct dfs_mnt *mnt)
{
_pcache_clean(mnt, _dummy_cb);
}
/**
* @brief Check and enforce page cache memory limit
*
* This function checks if the current page cache usage exceeds the working level threshold.
* If exceeded, it will trigger page cache release up to 4 times to reduce cache size.
*
* @return Always returns 0 indicating success
*/
static int dfs_pcache_limit_check(void)
{
int index = 4;
while (index && rt_atomic_load(&(__pcache.pages_count)) > RT_PAGECACHE_COUNT * RT_PAGECACHE_GC_WORK_LEVEL / 100)
{
dfs_pcache_release(0);
index --;
}
return 0;
}
/**
* @brief Page cache management thread
*
* This is the main worker thread for page cache management. It handles:
* - Garbage collection (GC) requests to free up memory
* - Write-back (WB) requests to flush dirty pages to storage
*
* @param[in] parameter Thread parameter (unused)
*
* @note The thread runs in an infinite loop processing messages from the cache message queue:
* - For GC commands: calls dfs_pcache_limit_check() to free pages when cache is full
* - For WB commands: flushes dirty pages that have been dirty for at least 500ms
* - Processes up to 4 dirty pages per WB command to prevent thread starvation
*/
static void dfs_pcache_thread(void *parameter)
{
struct dfs_pcache_mq_obj work;
while (1)
{
if (rt_mq_recv(__pcache.mqueue, &work, sizeof(work), RT_WAITING_FOREVER) == sizeof(work))
{
if (work.cmd == PCACHE_MQ_GC)
{
dfs_pcache_limit_check();
}
else if (work.cmd == PCACHE_MQ_WB)
{
int count = 0;
rt_list_t *node;
struct dfs_page *page = 0;
while (1)
{
/* try to get dirty page */
dfs_pcache_lock();
page = 0;
rt_list_for_each(node, &__pcache.list_active)
{
if (node != &__pcache.list_inactive)
{
struct dfs_aspace *aspace = rt_list_entry(node, struct dfs_aspace, cache_node);
dfs_aspace_lock(aspace);
if (aspace->list_dirty.next != &aspace->list_dirty)
{
page = rt_list_entry(aspace->list_dirty.next, struct dfs_page, dirty_node);
dfs_page_ref(page);
dfs_aspace_unlock(aspace);
break;
}
else
{
page = RT_NULL;
}
dfs_aspace_unlock(aspace);
}
}
dfs_pcache_unlock();
if (page)
{
struct dfs_aspace *aspace = page->aspace;
dfs_aspace_lock(aspace);
if (page->is_dirty == 1 && aspace->vnode)
{
if (rt_tick_get_millisecond() - page->tick_ms >= 500)
{
if (aspace->vnode->size < page->fpos + page->size)
{
page->len = aspace->vnode->size - page->fpos;
}
else
{
page->len = page->size;
}
if (aspace->ops->write)
{
aspace->ops->write(page);
}
page->is_dirty = 0;
if (page->dirty_node.next != RT_NULL)
{
rt_list_remove(&page->dirty_node);
page->dirty_node.next = RT_NULL;
}
}
}
dfs_page_release(page);
dfs_aspace_unlock(aspace);
}
else
{
break;
}
rt_thread_mdelay(5);
count ++;
if (count >= 4)
{
break;
}
}
}
}
}
}
/**
* @brief Initialize the page cache system
*
* This function initializes the global page cache structure including:
* - Hash table for address space lookup
* - Active and inactive page lists
* - Page count tracking
* - Mutex for thread safety
* - Message queue for cache operations
* - Worker thread for background tasks
*
* @return 0 on success, negative error code on failure
*
* @note This function is automatically called during system initialization
* via INIT_PREV_EXPORT macro. It sets up all necessary infrastructure
* for page cache management.
*/
static int dfs_pcache_init(void)
{
rt_thread_t tid;
for (int i = 0; i < RT_PAGECACHE_HASH_NR; i++)
{
rt_list_init(&__pcache.head[i]);
}
rt_list_init(&__pcache.list_active);
rt_list_init(&__pcache.list_inactive);
rt_list_insert_after(&__pcache.list_active, &__pcache.list_inactive);
rt_atomic_store(&(__pcache.pages_count), 0);
rt_mutex_init(&__pcache.lock, "pcache", RT_IPC_FLAG_PRIO);
__pcache.mqueue = rt_mq_create("pcache", sizeof(struct dfs_pcache_mq_obj), 1024, RT_IPC_FLAG_FIFO);
tid = rt_thread_create("pcache", dfs_pcache_thread, 0, 8192, 25, 5);
if (tid)
{
rt_thread_startup(tid);
}
__pcache.last_time_wb = rt_tick_get_millisecond();
return 0;
}
INIT_PREV_EXPORT(dfs_pcache_init);
/**
* @brief Send a command to page cache message queue
*
* This function sends a command to the page cache message queue for processing
* by the cache management thread. It waits for the message to be sent.
*
* @param[in] cmd The command to send (PCACHE_MQ_GC or PCACHE_MQ_WB)
*
* @return RT_EOK on success, error code on failure
*
* @note This is used to trigger garbage collection or write-back operations
* asynchronously through the cache management thread.
*/
static rt_ubase_t dfs_pcache_mq_work(rt_uint32_t cmd)
{
rt_err_t err;
struct dfs_pcache_mq_obj work = { 0 };
work.cmd = cmd;
err = rt_mq_send_wait(__pcache.mqueue, (const void *)&work, sizeof(struct dfs_pcache_mq_obj), 0);
return err;
}
/**
* @brief Lock the page cache global mutex
*
* @return Always returns 0.
*/
static int dfs_pcache_lock(void)
{
rt_mutex_take(&__pcache.lock, RT_WAITING_FOREVER);
return 0;
}
/**
* @brief Unlock the page cache global mutex
*
* @return Always returns 0.
*/
static int dfs_pcache_unlock(void)
{
rt_mutex_release(&__pcache.lock);
return 0;
}
/**
* @brief Calculate hash value for address space lookup
*
* This function computes a hash value based on mount point and path string.
* It uses a simple string hashing algorithm combined with mount point pointer.
*
* @param[in] mnt Pointer to the mount point structure
* @param[in] path Path string to be hashed (can be NULL)
*
* @return Computed hash value within range [0, RT_PAGECACHE_HASH_NR-1]
*
* @note The hash algorithm combines:
* - DJB2 hash algorithm for the path string
* - XOR with mount point pointer
* - Modulo operation to fit hash table size
*/
static uint32_t dfs_aspace_hash(struct dfs_mnt *mnt, const char *path)
{
uint32_t val = 0;
if (path)
{
while (*path)
{
val = ((val << 5) + val) + *path++;
}
}
return (val ^ (unsigned long)mnt) & (RT_PAGECACHE_HASH_NR - 1);
}
/**
* @brief Look up an address space in the page cache hash table
*
* This function searches for an address space matching the given dentry and operations
* in the page cache hash table. If found, it increments the reference count of the
* address space before returning it.
*
* @param[in] dentry Directory entry containing mount point and path information
* @param[in] ops Pointer to address space operations structure
*
* @return Pointer to the found address space on success, NULL if not found
*/
static struct dfs_aspace *dfs_aspace_hash_lookup(struct dfs_dentry *dentry, const struct dfs_aspace_ops *ops)
{
struct dfs_aspace *aspace = RT_NULL;
dfs_pcache_lock();
rt_list_for_each_entry(aspace, &__pcache.head[dfs_aspace_hash(dentry->mnt, dentry->pathname)], hash_node)
{
if (aspace->mnt == dentry->mnt
&& aspace->ops == ops
&& !strcmp(aspace->pathname, dentry->pathname))
{
rt_atomic_add(&aspace->ref_count, 1);
dfs_pcache_unlock();
return aspace;
}
}
dfs_pcache_unlock();
return RT_NULL;
}
/**
* @brief Insert an address space into page cache
*
* This function inserts the given address space into both the hash table and
* inactive list of the page cache. It also increments the reference count of
* the address space.
*
* @param[in,out] aspace Pointer to the address space to be inserted
*/
static void dfs_aspace_insert(struct dfs_aspace *aspace)
{
uint32_t val = 0;
val = dfs_aspace_hash(aspace->mnt, aspace->pathname);
dfs_pcache_lock();
rt_atomic_add(&aspace->ref_count, 1);
rt_list_insert_after(&__pcache.head[val], &aspace->hash_node);
rt_list_insert_before(&__pcache.list_inactive, &aspace->cache_node);
dfs_pcache_unlock();
}
/**
* @brief Remove an address space from page cache
*
* This function removes the given address space from both the hash table and
* active/inactive lists of the page cache.
*
* @param[in,out] aspace Pointer to the address space to be removed
*/
static void dfs_aspace_remove(struct dfs_aspace *aspace)
{
dfs_pcache_lock();
if (aspace->hash_node.next != RT_NULL)
{
rt_list_remove(&aspace->hash_node);
}
if (aspace->cache_node.next != RT_NULL)
{
rt_list_remove(&aspace->cache_node);
}
dfs_pcache_unlock();
}
/**
* @brief Move an address space to active list
*
* This function moves the specified address space from its current position
* to the active list in the page cache. The active list contains frequently
* accessed address spaces.
*
* @param[in,out] aspace Pointer to the address space to be activated
*
* @note Insert the address space before inactive list's head, means putting it
* to the end of the active list.
*
* @see dfs_aspace_inactive() for the opposite operation
*/
static void dfs_aspace_active(struct dfs_aspace *aspace)
{
dfs_pcache_lock();
if (aspace->cache_node.next != RT_NULL)
{
rt_list_remove(&aspace->cache_node);
rt_list_insert_before(&__pcache.list_inactive, &aspace->cache_node);
}
dfs_pcache_unlock();
}
/**
* @brief Move an address space to inactive list
*
* This function moves the specified address space from its current position
* to the inactive list in the page cache. The inactive list contains less
* frequently accessed address spaces that are candidates for eviction.
*
* @param[in,out] aspace Pointer to the address space to be deactivated
*/
static void dfs_aspace_inactive(struct dfs_aspace *aspace)
{
dfs_pcache_lock();
if (aspace->cache_node.next != RT_NULL)
{
rt_list_remove(&aspace->cache_node);
rt_list_insert_before(&__pcache.list_active, &aspace->cache_node);
}
dfs_pcache_unlock();
}
/**
* @brief Internal function to create a new address space for page cache
*
* This function allocates and initializes a new address space structure for page caching.
* It sets up all necessary lists, locks, and initial values for the address space.
*
* @param[in] dentry Directory entry containing mount point and path information (can be NULL)
* @param[in] vnode Pointer to the vnode structure this address space will be associated with
* @param[in] ops Pointer to address space operations structure
*
* @return Pointer to the newly created address space on success, NULL on failure
*
* @note The created address space will be automatically inserted into the page cache
* @see dfs_aspace_create() for the public interface to create address spaces
*/
static struct dfs_aspace *_dfs_aspace_create(struct dfs_dentry *dentry,
struct dfs_vnode *vnode,
const struct dfs_aspace_ops *ops)
{
struct dfs_aspace *aspace;
aspace = rt_calloc(1, sizeof(struct dfs_aspace));
if (aspace)
{
rt_list_init(&aspace->list_active);
rt_list_init(&aspace->list_inactive);
rt_list_init(&aspace->list_dirty);
rt_list_insert_after(&aspace->list_active, &aspace->list_inactive);
aspace->avl_root.root_node = 0;
aspace->avl_page = 0;
rt_mutex_init(&aspace->lock, rt_thread_self()->parent.name, RT_IPC_FLAG_PRIO);
rt_atomic_store(&aspace->ref_count, 1);
aspace->pages_count = 0;
aspace->vnode = vnode;
aspace->ops = ops;
if (dentry && dentry->mnt)
{
aspace->mnt = dentry->mnt;
aspace->fullpath = rt_strdup(dentry->mnt->fullpath);
aspace->pathname = rt_strdup(dentry->pathname);
}
dfs_aspace_insert(aspace);
}
return aspace;
}
/**
* @brief Create or lookup an address space for page caching
*
* This function either creates a new address space or looks up an existing one
* in the page cache hash table. If found, it updates the vnode reference and
* activates the address space.
*
* @param[in] dentry Directory entry containing mount point and path info (can be NULL)
* @param[in] vnode Pointer to the vnode structure to associate with
* @param[in] ops Pointer to address space operations structure
*
* @return Pointer to the found/created address space on success, NULL on failure
*/
struct dfs_aspace *dfs_aspace_create(struct dfs_dentry *dentry,
struct dfs_vnode *vnode,
const struct dfs_aspace_ops *ops)
{
struct dfs_aspace *aspace = RT_NULL;
RT_ASSERT(vnode && ops);
dfs_pcache_lock();
if (dentry)
{
aspace = dfs_aspace_hash_lookup(dentry, ops);
}
if (!aspace)
{
aspace = _dfs_aspace_create(dentry, vnode, ops);
}
else
{
aspace->vnode = vnode;
dfs_aspace_active(aspace);
}
dfs_pcache_unlock();
return aspace;
}
/**
* @brief Destroy an address space and release its resources
*
* This function decrements the reference count of the address space and marks it as inactive.
* If the reference count reaches 1 and there are no pages left, it will be fully released.
*
* @param[in] aspace Pointer to the address space to be destroyed
*
* @return 0 on successful release, -EINVAL if aspace is NULL
*/
int dfs_aspace_destroy(struct dfs_aspace *aspace)
{
int ret = -EINVAL;
if (aspace)
{
dfs_pcache_lock();
dfs_aspace_lock(aspace);
rt_atomic_sub(&aspace->ref_count, 1);
RT_ASSERT(rt_atomic_load(&aspace->ref_count) > 0);
dfs_aspace_inactive(aspace);
aspace->vnode = RT_NULL;
if (dfs_aspace_release(aspace) != 0)
{
dfs_aspace_unlock(aspace);
}
dfs_pcache_unlock();
}
return ret;
}
/**
* @brief Release an address space when its reference count reaches 1
*
* This function checks if the address space can be safely released by verifying:
* - Reference count is 1 (only caller holds reference)
* - No pages remain in the address space
* If conditions are met, it removes the space from cache and frees all resources.
*
* @param[in] aspace Pointer to the address space to be released
*
* @return 0 on successful release, -1 if space cannot be released yet
*/
static int dfs_aspace_release(struct dfs_aspace *aspace)
{
int ret = -1;
if (aspace)
{
dfs_pcache_lock();
dfs_aspace_lock(aspace);
if (rt_atomic_load(&aspace->ref_count) == 1 && aspace->pages_count == 0)
{
dfs_aspace_remove(aspace);
if (aspace->fullpath)
{
rt_free(aspace->fullpath);
}
if (aspace->pathname)
{
rt_free(aspace->pathname);
}
rt_mutex_detach(&aspace->lock);
rt_free(aspace);
ret = 0;
}
else
{
dfs_aspace_unlock(aspace);
}
dfs_pcache_unlock();
}
return ret;
}
/**
* @brief Dump address space page information for debugging
*
* This function prints detailed information about pages in the given address space.
* It can optionally filter to show only dirty pages or all pages.
*
* @param[in] aspace Pointer to the address space to dump
* @param[in] is_dirty Flag indicating whether to show only dirty pages (1) or all pages (0)
*
* @return Always returns 0
*/
static int _dfs_aspace_dump(struct dfs_aspace *aspace, int is_dirty)
{
if (aspace)
{
rt_list_t *next;
struct dfs_page *page;
dfs_aspace_lock(aspace);
if (aspace->pages_count > 0)
{
rt_list_for_each(next, &aspace->list_inactive)
{
if (next != &aspace->list_active)
{
page = rt_list_entry(next, struct dfs_page, space_node);
if (is_dirty && page->is_dirty)
{
rt_kprintf(" pages >> fpos: %d index :%d is_dirty: %d\n", page->fpos, page->fpos / ARCH_PAGE_SIZE, page->is_dirty);
}
else if (is_dirty == 0)
{
rt_kprintf(" pages >> fpos: %d index :%d is_dirty: %d\n", page->fpos, page->fpos / ARCH_PAGE_SIZE, page->is_dirty);
}
}
}
}
else
{
rt_kprintf(" pages >> empty\n");
}
dfs_aspace_unlock(aspace);
}
return 0;
}
/**
* @brief Dump page cache information for debugging purposes
*
* This function prints detailed information about the page cache, including:
* - Total page count and capacity
* - File paths and page counts for each address space
* - Optional detailed page information (with --dump or --dirty flags)
*
* @param[in] argc Number of command line arguments
* @param[in] argv Command line arguments array
*
* @return Always returns 0
*
* @see _dfs_aspace_dump() for the actual page dumping implementation
*/
static int dfs_pcache_dump(int argc, char **argv)
{
int dump = 0;
rt_list_t *node;
struct dfs_aspace *aspace;
if (argc == 2)
{
if (strcmp(argv[1], "--dump") == 0)
{
dump = 1;
}
else if (strcmp(argv[1], "--dirty") == 0)
{
dump = 2;
}
else
{
rt_kprintf("dfs page cache dump\n");
rt_kprintf("usage: dfs_cache\n");
rt_kprintf(" dfs_cache --dump\n");
rt_kprintf(" dfs_cache --dirty\n");
return 0;
}
}
dfs_pcache_lock();
rt_kprintf("total pages count: %d / %d\n", rt_atomic_load(&(__pcache.pages_count)), RT_PAGECACHE_COUNT);
rt_list_for_each(node, &__pcache.list_active)
{
if (node != &__pcache.list_inactive)
{
aspace = rt_list_entry(node, struct dfs_aspace, cache_node);
if (aspace->mnt)
{
rt_kprintf("file: %s%s pages: %d\n", aspace->fullpath, aspace->pathname, aspace->pages_count);
}
else
{
rt_kprintf("unknown type, pages: %d\n", aspace->pages_count);
}
if (dump > 0)
{
_dfs_aspace_dump(aspace, dump == 2 ? 1 : 0);
}
}
}
dfs_pcache_unlock();
return 0;
}
MSH_CMD_EXPORT_ALIAS(dfs_pcache_dump, dfs_cache, dump dfs page cache);
/**
* @brief Unmap all memory mappings for a page
*
* This function unmaps all virtual memory areas that have mapped this physical page.
* It also marks the page as dirty if it contains valid data that hasn't been written back.
*
* @param[in,out] page Pointer to the page structure to unmap
*
* @return Always returns 0
*/
static int dfs_page_unmap(struct dfs_page *page)
{
rt_list_t *next;
struct dfs_mmap *map;
next = page->mmap_head.next;
if (next != &page->mmap_head && page->fpos < page->aspace->vnode->size)
{
dfs_page_dirty(page);
}
while (next != &page->mmap_head)
{
map = rt_list_entry(next, struct dfs_mmap, mmap_node);
next = next->next;
if (map)
{
rt_varea_t varea;
void *vaddr;
varea = rt_aspace_query(map->aspace, map->vaddr);
RT_ASSERT(varea);
vaddr = dfs_aspace_vaddr(varea, page->fpos);
rt_varea_unmap_page(varea, vaddr);
rt_free(map);
}
}
rt_list_init(&page->mmap_head);
return 0;
}
/**
* @brief Create a new page structure for page cache
*
* This function allocates and initializes a new page structure for the page cache.
* It allocates physical memory for the page and initializes its metadata including:
* - Memory mapping list head
* - Reference count
* - Physical page allocation with affinity hint
*
* @param[in] pos File position used to determine page allocation affinity
*
* @return Pointer to the newly created page structure on success, NULL on failure
*/
static struct dfs_page *dfs_page_create(off_t pos)
{
struct dfs_page *page = RT_NULL;
int affid = RT_PAGE_PICK_AFFID(pos);
page = rt_calloc(1, sizeof(struct dfs_page));
if (page)
{
page->page = rt_pages_alloc_tagged(0, affid, PAGE_ANY_AVAILABLE);
if (page->page)
{
/* memset(page->page, 0x00, ARCH_PAGE_SIZE); */
rt_list_init(&page->mmap_head);
rt_atomic_store(&(page->ref_count), 1);
}
else
{
LOG_E("page alloc failed!\n");
rt_free(page);
page = RT_NULL;
}
}
return page;
}
/**
* @brief Increment the reference count of a page
*
* This function atomically increases the reference count of the specified page.
* It is used to track how many times the page is being referenced/used.
*
* @param[in,out] page Pointer to the page structure whose reference count will be incremented
*/
static void dfs_page_ref(struct dfs_page *page)
{
rt_atomic_add(&(page->ref_count), 1);
}
/**
* @brief Release a page from page cache when reference count reaches zero
*
* This function decrements the reference count of a page and performs cleanup
* when the count reaches zero. It handles:
* - Unmapping all virtual mappings of the page
* - Writing back dirty pages to storage
* - Freeing physical memory and page structure
*
* @param[in,out] page Pointer to the page structure to be released
*/
static void dfs_page_release(struct dfs_page *page)
{
struct dfs_aspace *aspace = page->aspace;
dfs_aspace_lock(aspace);
rt_atomic_sub(&(page->ref_count), 1);
if (rt_atomic_load(&(page->ref_count)) == 0)
{
dfs_page_unmap(page);
if (page->is_dirty == 1 && aspace->vnode)
{
if (aspace->vnode->size < page->fpos + page->size)
{
page->len = aspace->vnode->size - page->fpos;
}
else
{
page->len = page->size;
}
if (aspace->ops->write)
{
aspace->ops->write(page);
}
page->is_dirty = 0;
}
RT_ASSERT(page->is_dirty == 0);
rt_pages_free(page->page, 0);
page->page = RT_NULL;
rt_free(page);
}
dfs_aspace_unlock(aspace);
}
/**
* @brief Compare file positions for page alignment
*
* This function compares two file positions to determine if they belong to the same page.
* It aligns both positions to page boundaries before comparison.
*
* @param[in] fpos File position to compare (byte offset)
* @param[in] value Reference file position to compare against (byte offset)
*
* @return 0 if positions are in the same page, negative if fpos is before value,
* positive if fpos is after value
*/
static int dfs_page_compare(off_t fpos, off_t value)
{
return fpos / ARCH_PAGE_SIZE * ARCH_PAGE_SIZE - value;
}
/**
* @brief Insert a page into the AVL tree of an address space
*
* This function inserts a page into the AVL tree of the specified address space.
* The tree is ordered by the file position (fpos) of pages. If a page with the
* same fpos already exists, the insertion fails.
*
* @param[in] aspace Pointer to the address space containing the AVL tree
* @param[in,out] page Pointer to the page structure to be inserted
*
* @return 0 on successful insertion, -1 if a page with same fpos already exists
*
* @note The function:
* - Maintains AVL tree balance after insertion
* - Updates the aspace's avl_page pointer to the newly inserted page
* - Uses file position (fpos) as the ordering key
*/
static int _dfs_page_insert(struct dfs_aspace *aspace, struct dfs_page *page)
{
struct dfs_page *tmp;
struct util_avl_struct *current = NULL;
struct util_avl_struct **next = &(aspace->avl_root.root_node);
/* Figure out where to put new node */
while (*next)
{
current = *next;
tmp = rt_container_of(current, struct dfs_page, avl_node);
if (page->fpos < tmp->fpos)
next = &(current->avl_left);
else if (page->fpos > tmp->fpos)
next = &(current->avl_right);
else
return -1;
}
/* Add new node and rebalance tree. */
util_avl_link(&page->avl_node, current, next);
util_avl_rebalance(current, &aspace->avl_root);
aspace->avl_page = page;
return 0;
}
/**
* @brief Remove a page from the AVL tree of an address space
*
* This function removes a page from the AVL tree of the specified address space.
* It also clears the cached AVL page pointer if it points to the page being removed.
*
* @param[in,out] aspace Pointer to the address space containing the AVL tree
* @param[in,out] page Pointer to the page structure to be removed
*/
static void _dfs_page_remove(struct dfs_aspace *aspace, struct dfs_page *page)
{
if (aspace->avl_page && aspace->avl_page == page)
{
aspace->avl_page = 0;
}
util_avl_remove(&page->avl_node, &aspace->avl_root);
}
/**
* @brief Lock an address space for thread-safe operations
*
* @param[in,out] aspace Pointer to the address space structure to be locked
*
* @return Always returns 0 indicating success
*
* @note The lock must be released using dfs_aspace_unlock()
* @see dfs_aspace_unlock()
*/
static int dfs_aspace_lock(struct dfs_aspace *aspace)
{
rt_mutex_take(&aspace->lock, RT_WAITING_FOREVER);
return 0;
}
/**
* @brief Unlock an address space after thread-safe operations
*
* @param[in,out] aspace Pointer to the address space structure to be unlocked
*
* @return Always returns 0 indicating success
*
* @note Must be called after dfs_aspace_lock() to release the lock
* @see dfs_aspace_lock()
*/
static int dfs_aspace_unlock(struct dfs_aspace *aspace)
{
rt_mutex_release(&aspace->lock);
return 0;
}
/**
* @brief Insert a page into the address space's page cache
*
* This function inserts a page into the active list of the address space's page cache.
* It maintains the page count and performs eviction if the cache exceeds its capacity.
*
* @param[in] page Pointer to the page structure to be inserted
*
* @return Always returns 0 indicating success
*/
static int dfs_page_insert(struct dfs_page *page)
{
struct dfs_aspace *aspace = page->aspace;
dfs_aspace_lock(aspace);
rt_list_insert_before(&aspace->list_inactive, &page->space_node);
aspace->pages_count ++;
if (_dfs_page_insert(aspace, page))
{
RT_ASSERT(0);
}
if (aspace->pages_count > RT_PAGECACHE_ASPACE_COUNT)
{
rt_list_t *next = aspace->list_active.next;
if (next != &aspace->list_inactive)
{
struct dfs_page *tmp = rt_list_entry(next, struct dfs_page, space_node);
dfs_page_inactive(tmp);
}
}
rt_atomic_add(&(__pcache.pages_count), 1);
dfs_aspace_unlock(aspace);
return 0;
}
/**
* @brief Remove a page from the address space's page cache
*
* This function safely removes a page from both the space and dirty lists of the address space.
* It decrements the reference count and releases the page if it's the last reference.
*
* @param[in] page Pointer to the page structure to be removed
*
* @return 0 if the page was successfully removed, -1 if the page is still referenced
*/
static int dfs_page_remove(struct dfs_page *page)
{
int ret = -1;
struct dfs_aspace *aspace = page->aspace;
dfs_aspace_lock(aspace);
if (rt_atomic_load(&(page->ref_count)) == 1)
{
if (page->space_node.next != RT_NULL)
{
rt_list_remove(&page->space_node);
page->space_node.next = RT_NULL;
aspace->pages_count--;
_dfs_page_remove(aspace, page);
}
if (page->dirty_node.next != RT_NULL)
{
rt_list_remove(&page->dirty_node);
page->dirty_node.next = RT_NULL;
}
rt_atomic_sub(&(__pcache.pages_count), 1);
dfs_page_release(page);
ret = 0;
}
dfs_aspace_unlock(aspace);
return ret;
}
/**
* @brief Move a page to active list
*
* This function moves a page to the active list
* within its associated address space.
*
* @param[in] page The page to be moved to active list
* @return int Always returns 0 on success
*/
static int dfs_page_active(struct dfs_page *page)
{
struct dfs_aspace *aspace = page->aspace;
dfs_aspace_lock(aspace);
if (page->space_node.next != RT_NULL)
{
rt_list_remove(&page->space_node);
rt_list_insert_before(&aspace->list_inactive, &page->space_node);
}
dfs_aspace_unlock(aspace);
return 0;
}
/**
* @brief Move a page to inactive list
*
* This function moves a page to the inactive list
* within its associated address space.
*
* @param[in] page The page to be moved to inactive list
* @return int Always returns 0 on success
*/
static int dfs_page_inactive(struct dfs_page *page)
{
struct dfs_aspace *aspace = page->aspace;
dfs_aspace_lock(aspace);
if (page->space_node.next != RT_NULL)
{
rt_list_remove(&page->space_node);
rt_list_insert_before(&aspace->list_active, &page->space_node);
}
dfs_aspace_unlock(aspace);
return 0;
}
/**
* @brief Mark a page as dirty and manage dirty list
*
* This function marks a page as dirty and adds it to the dirty list if not already present.
* It also triggers a write-back operation if more than 1 second has passed since last write-back.
*
* @param[in] page The page to be marked as dirty
* @return int Always returns 0 on success
*/
static int dfs_page_dirty(struct dfs_page *page)
{
struct dfs_aspace *aspace = page->aspace;
dfs_aspace_lock(aspace);
if (page->dirty_node.next == RT_NULL && page->space_node.next != RT_NULL)
{
rt_list_insert_before(&aspace->list_dirty, &page->dirty_node);
}
page->is_dirty = 1;
page->tick_ms = rt_tick_get_millisecond();
if (rt_tick_get_millisecond() - __pcache.last_time_wb >= 1000)
{
dfs_pcache_mq_work(PCACHE_MQ_WB);
__pcache.last_time_wb = rt_tick_get_millisecond();
}
dfs_aspace_unlock(aspace);
return 0;
}
/**
* @brief Search for a page in the address space AVL tree
*
* This function searches for a page at the specified file position in the address space's AVL tree.
* If found, it marks the page as active and increments its reference count.
*
* @param[in] aspace The address space to search in
* @param[in] fpos The file position to search for
* @return struct dfs_page* The found page, or RT_NULL if not found
*/
static struct dfs_page *dfs_page_search(struct dfs_aspace *aspace, off_t fpos)
{
int cmp;
struct dfs_page *page;
struct util_avl_struct *avl_node;
dfs_aspace_lock(aspace);
if (aspace->avl_page && dfs_page_compare(fpos, aspace->avl_page->fpos) == 0)
{
page = aspace->avl_page;
dfs_page_active(page);
dfs_page_ref(page);
dfs_aspace_unlock(aspace);
return page;
}
avl_node = aspace->avl_root.root_node;
while (avl_node)
{
page = rt_container_of(avl_node, struct dfs_page, avl_node);
cmp = dfs_page_compare(fpos, page->fpos);
if (cmp < 0)
{
avl_node = avl_node->avl_left;
}
else if (cmp > 0)
{
avl_node = avl_node->avl_right;
}
else
{
aspace->avl_page = page;
dfs_page_active(page);
dfs_page_ref(page);
dfs_aspace_unlock(aspace);
return page;
}
}
dfs_aspace_unlock(aspace);
return RT_NULL;
}
/**
* @brief Load a page from file into address space cache
*
* This function creates a new page cache entry for the specified file position,
* reads the content from the file into the page, and inserts it into the cache.
* The page's reference count is incremented to prevent c eviction.
*
* @param[in] file Pointer to the file structure containing the vnode and aspace
* @param[in] pos File position to load (will be page-aligned)
*
* @return Pointer to the newly created and loaded page on success,
* NULL on failure or invalid parameters
*/
static struct dfs_page *dfs_aspace_load_page(struct dfs_file *file, off_t pos)
{
struct dfs_page *page = RT_NULL;
if (file && file->vnode && file->vnode->aspace)
{
struct dfs_vnode *vnode = file->vnode;
struct dfs_aspace *aspace = vnode->aspace;
page = dfs_page_create(pos);
if (page)
{
page->aspace = aspace;
page->size = ARCH_PAGE_SIZE;
page->fpos = RT_ALIGN_DOWN(pos, ARCH_PAGE_SIZE);
aspace->ops->read(file, page);
page->ref_count ++;
dfs_page_insert(page);
}
}
return page;
}
/**
* @brief Look up a page in the cache and load it if not found
*
* This function searches for a page at the specified position in the file's address space.
* If the page isn't found, it preloads multiple pages (RT_PAGECACHE_PRELOAD count) next to the requested position.
* It also triggers garbage collection when the cache reaches certain thresholds.
*
* @param[in] file Pointer to the file structure containing the vnode and aspace
* @param[in] pos File position to look up (will be page-aligned)
*
* @return Pointer to the found or newly loaded page on success,
* NULL if the page couldn't be found or loaded
*/
static struct dfs_page *dfs_page_lookup(struct dfs_file *file, off_t pos)
{
struct dfs_page *page = RT_NULL;
struct dfs_aspace *aspace = file->vnode->aspace;
dfs_aspace_lock(aspace);
page = dfs_page_search(aspace, pos);
if (!page)
{
int count = RT_PAGECACHE_PRELOAD;
struct dfs_page *tmp = RT_NULL;
off_t fpos = pos / ARCH_PAGE_SIZE * ARCH_PAGE_SIZE;
do
{
page = dfs_aspace_load_page(file, fpos);
if (page)
{
if (tmp == RT_NULL)
{
tmp = page;
}
else
{
dfs_page_release(page);
}
}
else
{
break;
}
fpos += ARCH_PAGE_SIZE;
page = dfs_page_search(aspace, fpos);
if (page)
{
dfs_page_release(page);
}
count --;
} while (count && page == RT_NULL);
page = tmp;
if (page)
{
dfs_aspace_unlock(aspace);
if (rt_atomic_load(&(__pcache.pages_count)) >= RT_PAGECACHE_COUNT)
{
dfs_pcache_limit_check();
}
else if (rt_atomic_load(&(__pcache.pages_count)) >= RT_PAGECACHE_COUNT * RT_PAGECACHE_GC_WORK_LEVEL / 100)
{
dfs_pcache_mq_work(PCACHE_MQ_GC);
}
return page;
}
}
dfs_aspace_unlock(aspace);
return page;
}
/**
* @brief Read data from file through address space page cache
*
* This function reads data from a file using its address space page cache. It handles
* the lookup of pages containing the requested data, copies the data to the provided
* buffer, and manages page references.
*
* @param[in] file Pointer to the file structure containing vnode and aspace
* @param[in] buf Buffer to store the read data
* @param[in] count Number of bytes to read
* @param[in,out] pos Pointer to the file position (updated during reading)
*
* @return Number of bytes successfully read, or negative error code
*/
int dfs_aspace_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
{
int ret = -EINVAL;
if (file && file->vnode && file->vnode->aspace)
{
if (!(file->vnode->aspace->ops->read))
return ret;
struct dfs_vnode *vnode = file->vnode;
struct dfs_aspace *aspace = vnode->aspace;
struct dfs_page *page;
char *ptr = (char *)buf;
ret = 0;
while (count)
{
page = dfs_page_lookup(file, *pos);
if (page)
{
off_t len;
dfs_aspace_lock(aspace);
if (aspace->vnode->size < page->fpos + ARCH_PAGE_SIZE)
{
len = aspace->vnode->size - *pos;
}
else
{
len = page->fpos + ARCH_PAGE_SIZE - *pos;
}
len = count > len ? len : count;
if (len > 0)
{
rt_memcpy(ptr, page->page + *pos - page->fpos, len);
ptr += len;
*pos += len;
count -= len;
ret += len;
}
else
{
dfs_page_release(page);
dfs_aspace_unlock(aspace);
break;
}
dfs_page_release(page);
dfs_aspace_unlock(aspace);
}
else
{
break;
}
}
}
return ret;
}
/**
* @brief Write data to file through address space page cache
*
* This function writes data to a file using its address space page cache. It handles
* page lookup, data copying, dirty page marking, and synchronization operations.
*
* @param[in] file Pointer to the file structure containing vnode and aspace
* @param[in] buf Buffer containing data to write
* @param[in] count Number of bytes to write
* @param[in,out] pos Pointer to the file position (updated during writing)
*
* @return Number of bytes successfully written, or negative error code
*/
int dfs_aspace_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos)
{
int ret = -EINVAL;
if (file && file->vnode && file->vnode->aspace)
{
struct dfs_vnode *vnode = file->vnode;
struct dfs_aspace *aspace = vnode->aspace;
struct dfs_page *page;
char *ptr = (char *)buf;
if (!(aspace->ops->write))
{
return ret;
}
else if (aspace->mnt && (aspace->mnt->flags & MNT_RDONLY))
{
return -EROFS;
}
ret = 0;
while (count)
{
page = dfs_page_lookup(file, *pos);
if (page)
{
off_t len;
dfs_aspace_lock(aspace);
len = page->fpos + ARCH_PAGE_SIZE - *pos;
len = count > len ? len : count;
rt_memcpy(page->page + *pos - page->fpos, ptr, len);
ptr += len;
*pos += len;
count -= len;
ret += len;
if (*pos > aspace->vnode->size)
{
aspace->vnode->size = *pos;
}
if (file->flags & O_SYNC)
{
if (aspace->vnode->size < page->fpos + page->size)
{
page->len = aspace->vnode->size - page->fpos;
}
else
{
page->len = page->size;
}
aspace->ops->write(page);
page->is_dirty = 0;
}
else
{
dfs_page_dirty(page);
}
dfs_page_release(page);
dfs_aspace_unlock(aspace);
}
else
{
break;
}
}
}
return ret;
}
/**
* @brief Flush dirty pages in an address space to storage
*
* This function writes all dirty pages in the specified address space to storage,
* ensuring data persistence. It handles page size adjustments and clears dirty flags
* after successful writes.
*
* @param[in] aspace Pointer to the address space containing dirty pages
*
* @return Always returns 0 (success)
*/
int dfs_aspace_flush(struct dfs_aspace *aspace)
{
if (aspace)
{
rt_list_t *next;
struct dfs_page *page;
dfs_aspace_lock(aspace);
if (aspace->pages_count > 0 && aspace->vnode)
{
rt_list_for_each(next, &aspace->list_dirty)
{
page = rt_list_entry(next, struct dfs_page, dirty_node);
if (page->is_dirty == 1 && aspace->vnode)
{
if (aspace->vnode->size < page->fpos + page->size)
{
page->len = aspace->vnode->size - page->fpos;
}
else
{
page->len = page->size;
}
if (aspace->ops->write)
{
aspace->ops->write(page);
}
page->is_dirty = 0;
}
RT_ASSERT(page->is_dirty == 0);
}
}
dfs_aspace_unlock(aspace);
}
return 0;
}
/**
* @brief Clean all pages from an address space
*
* This function removes all active pages from the specified address space while
* maintaining thread safety through proper locking. It skips inactive pages
* during the cleanup process.
*
* @param[in] aspace Pointer to the address space structure to clean
*
* @return 0 on success, negative value on error
*/
int dfs_aspace_clean(struct dfs_aspace *aspace)
{
if (aspace)
{
dfs_aspace_lock(aspace);
if (aspace->pages_count > 0)
{
rt_list_t *next = aspace->list_active.next;
struct dfs_page *page;
while (next && next != &aspace->list_active)
{
if (next == &aspace->list_inactive)
{
next = next->next;
continue;
}
page = rt_list_entry(next, struct dfs_page, space_node);
next = next->next;
dfs_page_remove(page);
}
}
dfs_aspace_unlock(aspace);
}
return 0;
}
/**
* @brief Map a file page into virtual address space
*
* This function maps a file page into the specified virtual address space, handling
* memory allocation, page lookup, and cache synchronization. It ensures proper
* memory visibility across different CPU architectures with cache operations.
*
* @param[in] file Pointer to the file structure
* @param[in] varea Pointer to the virtual address area structure
* @param[in] vaddr Virtual address to map the page to
*
* @return Pointer to the mapped page on success, NULL on failure
*
* @note This function handles cache synchronization for architectures with weak
* memory models or Harvard architectures to ensure data visibility. It also
* manages the mapping structure lifecycle through proper allocation/free.
*/
void *dfs_aspace_mmap(struct dfs_file *file, struct rt_varea *varea, void *vaddr)
{
void *ret = RT_NULL;
struct dfs_page *page;
struct dfs_aspace *aspace = file->vnode->aspace;
rt_aspace_t target_aspace = varea->aspace;
page = dfs_page_lookup(file, dfs_aspace_fpos(varea, vaddr));
if (page)
{
struct dfs_mmap *map = (struct dfs_mmap *)rt_calloc(1, sizeof(struct dfs_mmap));
if (map)
{
void *pg_vaddr = page->page;
void *pg_paddr = rt_kmem_v2p(pg_vaddr);
int err = rt_varea_map_range(varea, vaddr, pg_paddr, page->size);
if (err == RT_EOK)
{
/**
* Note: While the page is mapped into user area, the data writing into the page
* is not guaranteed to be visible for machines with the *weak* memory model and
* those Harvard architecture (especially for those ARM64) cores for their
* out-of-order pipelines of data buffer. Besides if the instruction cache in the
* L1 memory system is a VIPT cache, there are chances to have the alias matching
* entry if we reuse the same page frame and map it into the same virtual address
* of the previous one.
*
* That's why we have to do synchronization and cleanup manually to ensure that
* fetching of the next instruction can see the coherent data with the data cache,
* TLB, MMU, main memory, and all the other observers in the computer system.
*/
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, vaddr, ARCH_PAGE_SIZE);
rt_hw_cpu_icache_ops(RT_HW_CACHE_INVALIDATE, vaddr, ARCH_PAGE_SIZE);
ret = pg_vaddr;
map->aspace = target_aspace;
map->vaddr = vaddr;
dfs_aspace_lock(aspace);
rt_list_insert_after(&page->mmap_head, &map->mmap_node);
dfs_page_release(page);
dfs_aspace_unlock(aspace);
}
else
{
dfs_page_release(page);
rt_free(map);
}
}
else
{
dfs_page_release(page);
}
}
return ret;
}
/**
* @brief Unmap pages from virtual address space
*
* This function removes mappings of file pages within the specified virtual address range.
* It handles cache synchronization and maintains page dirty status when unmapping.
*
* @param[in] file Pointer to the file structure
* @param[in] varea Pointer to the virtual address area to unmap
*
* @return 0 on success
*
* @note This function handles both private and shared mappings, ensuring proper
* cache synchronization and page dirty status maintenance during unmapping.
*/
int dfs_aspace_unmap(struct dfs_file *file, struct rt_varea *varea)
{
struct dfs_vnode *vnode = file->vnode;
struct dfs_aspace *aspace = vnode->aspace;
void *unmap_start = varea->start;
void *unmap_end = (char *)unmap_start + varea->size;
if (aspace)
{
rt_list_t *next;
struct dfs_page *page;
dfs_aspace_lock(aspace);
if (aspace->pages_count > 0)
{
rt_list_for_each(next, &aspace->list_active)
{
if (next != &aspace->list_inactive)
{
page = rt_list_entry(next, struct dfs_page, space_node);
if (page)
{
rt_list_t *node, *tmp;
struct dfs_mmap *map;
rt_varea_t map_varea = RT_NULL;
node = page->mmap_head.next;
while (node != &page->mmap_head)
{
rt_aspace_t map_aspace;
map = rt_list_entry(node, struct dfs_mmap, mmap_node);
tmp = node;
node = node->next;
if (map && varea->aspace == map->aspace
&& map->vaddr >= unmap_start && map->vaddr < unmap_end)
{
void *vaddr = map->vaddr;
map_aspace = map->aspace;
if (!map_varea || map_varea->aspace != map_aspace ||
vaddr < map_varea->start ||
vaddr >= map_varea->start + map_varea->size)
{
/* lock the tree so we don't access uncompleted data */
map_varea = rt_aspace_query(map_aspace, vaddr);
}
rt_varea_unmap_page(map_varea, vaddr);
if (!rt_varea_is_private_locked(varea) &&
page->fpos < page->aspace->vnode->size)
{
dfs_page_dirty(page);
}
rt_list_remove(tmp);
rt_free(map);
break;
}
}
}
}
}
}
dfs_aspace_unlock(aspace);
}
return 0;
}
/**
* Unmap a page from virtual address space.
*
* @param[in] file The file object containing the page
* @param[in] varea The virtual memory area
* @param[in] vaddr The virtual address to unmap
*
* @return Always returns 0 on success
*
* @note This function removes the mapping between a virtual address and a physical page.
* It handles cleanup of mmap structures and marks pages dirty if needed.
*/
int dfs_aspace_page_unmap(struct dfs_file *file, struct rt_varea *varea, void *vaddr)
{
struct dfs_page *page;
struct dfs_aspace *aspace = file->vnode->aspace;
if (aspace)
{
dfs_aspace_lock(aspace);
page = dfs_page_search(aspace, dfs_aspace_fpos(varea, vaddr));
if (page)
{
rt_list_t *node, *tmp;
struct dfs_mmap *map;
rt_varea_unmap_page(varea, vaddr);
node = page->mmap_head.next;
while (node != &page->mmap_head)
{
map = rt_list_entry(node, struct dfs_mmap, mmap_node);
tmp = node;
node = node->next;
if (map && varea->aspace == map->aspace && vaddr == map->vaddr)
{
if (!rt_varea_is_private_locked(varea))
{
dfs_page_dirty(page);
}
rt_list_remove(tmp);
rt_free(map);
break;
}
}
dfs_page_release(page);
}
dfs_aspace_unlock(aspace);
}
return 0;
}
/**
* Mark a page as dirty in the address space.
*
* @param[in] file The file object containing the page
* @param[in] varea The virtual memory area
* @param[in] vaddr The virtual address of the page
*
* @return Always returns 0 on success
*
* @note This function marks a specific page as dirty in the file's address space.
* The page is released after being marked dirty.
*/
int dfs_aspace_page_dirty(struct dfs_file *file, struct rt_varea *varea, void *vaddr)
{
struct dfs_page *page;
struct dfs_aspace *aspace = file->vnode->aspace;
if (aspace)
{
dfs_aspace_lock(aspace);
page = dfs_page_search(aspace, dfs_aspace_fpos(varea, vaddr));
if (page)
{
dfs_page_dirty(page);
dfs_page_release(page);
}
dfs_aspace_unlock(aspace);
}
return 0;
}
/**
* Calculate file position from virtual address.
*
* @param[in] varea The virtual memory area
* @param[in] vaddr The virtual address to convert
*
* @return The calculated file position offset
*/
off_t dfs_aspace_fpos(struct rt_varea *varea, void *vaddr)
{
return (off_t)(intptr_t)vaddr - (off_t)(intptr_t)varea->start + varea->offset * ARCH_PAGE_SIZE;
}
/**
* Get the virtual address corresponding to a file position in a virtual area.
*
* @param[in] varea The virtual area structure
* @param[in] fpos The file position to convert
*
* @return The virtual address corresponding to the file position
*/
void *dfs_aspace_vaddr(struct rt_varea *varea, off_t fpos)
{
return varea->start + fpos - varea->offset * ARCH_PAGE_SIZE;
}
/**
* @brief Read data from memory-mapped file space
*
* This function handles read operations for memory-mapped file regions by
* translating virtual addresses to file positions and performing the actual
* read operation through dfs_aspace_read.
*
* @param[in] file Pointer to the file structure being mapped
* @param[in] varea Pointer to the virtual memory area structure
* @param[in] data Pointer to the I/O message containing read details
* (includes fault address and buffer address)
*
* @return Number of bytes successfully read (ARCH_PAGE_SIZE on success)
* 0 if any parameter is invalid
*/
int dfs_aspace_mmap_read(struct dfs_file *file, struct rt_varea *varea, void *data)
{
int ret = 0;
if (file && varea)
{
struct rt_aspace_io_msg *msg = (struct rt_aspace_io_msg *)data;
if (msg)
{
off_t fpos = dfs_aspace_fpos(varea, msg->fault_vaddr);
return dfs_aspace_read(file, msg->buffer_vaddr, ARCH_PAGE_SIZE, &fpos);
}
}
return ret;
}
/**
* @brief Write data to memory-mapped file space
*
* This function handles write operations for memory-mapped file regions by
* translating virtual addresses to file positions and performing the actual
* write operation through dfs_aspace_write.
*
* @param[in] file Pointer to the file structure being mapped
* @param[in] varea Pointer to the virtual memory area structure
* @param[in] data Pointer to the I/O message containing write details
* (includes fault address and buffer address)
*
* @return Number of bytes successfully written (ARCH_PAGE_SIZE on success)
* 0 if any parameter is invalid
*/
int dfs_aspace_mmap_write(struct dfs_file *file, struct rt_varea *varea, void *data)
{
int ret = 0;
if (file && varea)
{
struct rt_aspace_io_msg *msg = (struct rt_aspace_io_msg *)data;
if (msg)
{
off_t fpos = dfs_aspace_fpos(varea, msg->fault_vaddr);
return dfs_aspace_write(file, msg->buffer_vaddr, ARCH_PAGE_SIZE, &fpos);
}
}
return ret;
}
#endif
|