1 #include <klibc/string.h>
2 #include <lunaix/ds/btrie.h>
4 #include <lunaix/mm/page.h>
5 #include <lunaix/mm/pmm.h>
6 #include <lunaix/mm/valloc.h>
7 #include <lunaix/mm/vmm.h>
8 #include <lunaix/spike.h>
10 #define PCACHE_DIRTY 0x1
13 pcache_init(struct pcache* pcache)
15 btrie_init(&pcache->tree, PG_SIZE_BITS);
16 llist_init_head(&pcache->dirty);
17 llist_init_head(&pcache->pages);
21 pcache_release_page(struct pcache* pcache, struct pcache_pg* page)
23 pmm_free_page(KERNEL_PID, page->pg);
24 vmm_del_mapping(PD_REFERENCED, page->pg);
26 llist_delete(&page->pg_list);
34 pcache_new_page(struct pcache* pcache, uint32_t index)
36 void* pg = pmm_alloc_page(KERNEL_PID, 0);
37 void* pg_v = vmm_vmap(pg, PG_SIZE, PG_PREM_URW);
38 struct pcache_pg* ppg = vzalloc(sizeof(struct pcache_pg));
41 llist_append(&pcache->pages, &ppg->pg_list);
42 btrie_set(&pcache->tree, index, ppg);
48 pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg)
50 if (!(pg->flags & PCACHE_DIRTY)) {
51 pg->flags |= PCACHE_DIRTY;
53 llist_append(&pcache->dirty, &pg->dirty_list);
58 pcache_get_page(struct pcache* pcache,
61 struct pcache_pg** page)
63 struct pcache_pg* pg = btrie_get(&pcache->tree, index);
65 *offset = index & ((1 << pcache->tree.truncated) - 1);
67 pg = pcache_new_page(pcache, index);
68 pg->fpos = index - *offset;
77 pcache_write(struct v_file* file, void* data, uint32_t len, uint32_t fpos)
79 uint32_t pg_off, buf_off = 0;
80 struct pcache* pcache = file->inode->pg_cache;
83 while (buf_off < len) {
84 pcache_get_page(pcache, fpos, &pg_off, &pg);
85 uint32_t wr_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
86 memcpy(pg->pg + pg_off, (data + buf_off), wr_bytes);
88 pcache_set_dirty(pcache, pg);
98 pcache_read(struct v_file* file, void* data, uint32_t len, uint32_t fpos)
100 uint32_t pg_off, buf_off = 0, new_pg = 0;
102 struct pcache* pcache = file->inode->pg_cache;
103 struct pcache_pg* pg;
104 struct v_inode* inode = file->inode;
106 while (buf_off < len) {
107 if (pcache_get_page(pcache, fpos, &pg_off, &pg)) {
108 // Filling up the page
109 errno = inode->default_fops.read(file, pg->pg, PG_SIZE, pg->fpos);
110 if (errno >= 0 && errno < PG_SIZE) {
112 len = buf_off + errno;
113 } else if (errno < 0) {
117 uint32_t rd_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
118 memcpy((data + buf_off), pg->pg + pg_off, rd_bytes);
124 return errno < 0 ? errno : buf_off;
128 pcache_release(struct pcache* pcache)
130 struct pcache_pg *pos, *n;
131 llist_for_each(pos, n, &pcache->pages, pg_list)
136 btrie_release(&pcache->tree);
140 pcache_commit(struct v_file* file, struct pcache_pg* page)
142 if (!(page->flags & PCACHE_DIRTY)) {
146 struct v_inode* inode = file->inode;
147 int errno = inode->default_fops.write(file, page->pg, PG_SIZE, page->fpos);
150 page->flags &= ~PCACHE_DIRTY;
151 llist_delete(&page->dirty_list);
152 file->inode->pg_cache->n_dirty--;
159 pcache_commit_all(struct v_file* file)
161 struct pcache* cache = file->inode->pg_cache;
162 struct pcache_pg *pos, *n;
163 llist_for_each(pos, n, &cache->dirty, dirty_list)
165 pcache_commit(file, pos);
170 pcache_invalidate(struct v_file* file, struct pcache_pg* page)
172 pcache_commit(file, page);
173 pcache_release_page(&file->inode->pg_cache, page);