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 = valloc(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)
79 uint32_t pg_off, buf_off = 0, fpos = file->f_pos;
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)
100 uint32_t pg_off, buf_off = 0, new_pg = 0, fpos = file->f_pos;
102 struct pcache* pcache = file->inode->pg_cache;
103 struct pcache_pg* pg;
105 while (buf_off < len) {
106 if (pcache_get_page(pcache, fpos, &pg_off, &pg)) {
107 // Filling up the page
108 errno = file->ops.read(file, pg->pg, PG_SIZE, pg->fpos);
109 if (errno > 0 && errno < PG_SIZE) {
111 len = buf_off + errno;
112 } else if (errno < 0) {
116 uint32_t rd_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
117 memcpy((data + buf_off), pg->pg + pg_off, rd_bytes);
123 return errno < 0 ? errno : buf_off;
127 pcache_release(struct pcache* pcache)
129 struct pcache_pg *pos, *n;
130 llist_for_each(pos, n, &pcache->pages, pg_list)
135 btrie_release(&pcache->tree);
139 pcache_commit(struct v_file* file, struct pcache_pg* page)
141 if (!(page->flags & PCACHE_DIRTY)) {
145 int errno = file->ops.write(file, page->pg, PG_SIZE, page->fpos);
148 page->flags &= ~PCACHE_DIRTY;
149 llist_delete(&page->dirty_list);
150 file->inode->pg_cache->n_dirty--;
157 pcache_commit_all(struct v_file* file)
159 struct pcache* cache = file->inode->pg_cache;
160 struct pcache_pg *pos, *n;
161 llist_for_each(pos, n, &cache->dirty, dirty_list)
163 pcache_commit(file, pos);
168 pcache_invalidate(struct v_file* file, struct pcache_pg* page)
170 pcache_commit(file, page);
171 pcache_release_page(&file->inode->pg_cache, page);