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
12 static struct lru_zone* pcache_zone;
15 pcache_init(struct pcache* pcache)
17 btrie_init(&pcache->tree, PG_SIZE_BITS);
18 llist_init_head(&pcache->dirty);
19 llist_init_head(&pcache->pages);
20 pcache_zone = lru_new_zone();
24 pcache_release_page(struct pcache* pcache, struct pcache_pg* page)
28 llist_delete(&page->pg_list);
36 pcache_evict(struct pcache* pcache)
38 struct pcache_pg* page =
39 container_of(lru_evict_one(pcache_zone), struct pcache_pg, lru);
44 pcache_invalidate(pcache, page);
48 pcache_new_page(struct pcache* pcache, uint32_t index)
50 struct pcache_pg* ppg = vzalloc(sizeof(struct pcache_pg));
51 void* pg = valloc(PG_SIZE);
55 if (!ppg && !(ppg = vzalloc(sizeof(struct pcache_pg)))) {
59 if (!pg && !(pg = valloc(PG_SIZE))) {
66 llist_append(&pcache->pages, &ppg->pg_list);
67 btrie_set(&pcache->tree, index, ppg);
73 pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg)
75 if (!(pg->flags & PCACHE_DIRTY)) {
76 pg->flags |= PCACHE_DIRTY;
78 llist_append(&pcache->dirty, &pg->dirty_list);
83 pcache_get_page(struct pcache* pcache,
86 struct pcache_pg** page)
88 struct pcache_pg* pg = btrie_get(&pcache->tree, index);
90 *offset = index & ((1 << pcache->tree.truncated) - 1);
91 if (!pg && (pg = pcache_new_page(pcache, index))) {
92 pg->fpos = index - *offset;
97 lru_use_one(pcache_zone, &pg->lru);
103 pcache_write(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos)
105 uint32_t pg_off, buf_off = 0;
106 struct pcache* pcache = inode->pg_cache;
107 struct pcache_pg* pg;
109 while (buf_off < len) {
110 pcache_get_page(pcache, fpos, &pg_off, &pg);
115 uint32_t wr_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
116 memcpy(pg->pg + pg_off, (data + buf_off), wr_bytes);
118 pcache_set_dirty(pcache, pg);
128 pcache_read(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos)
130 uint32_t pg_off, buf_off = 0, new_pg = 0;
132 struct pcache* pcache = inode->pg_cache;
133 struct pcache_pg* pg;
135 while (buf_off < len) {
136 if (pcache_get_page(pcache, fpos, &pg_off, &pg)) {
142 // Filling up the page
143 errno = inode->default_fops.read(inode, pg->pg, PG_SIZE, pg->fpos);
144 if (errno >= 0 && errno < PG_SIZE) {
146 len = buf_off + errno;
147 } else if (errno < 0) {
151 uint32_t rd_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
152 memcpy((data + buf_off), pg->pg + pg_off, rd_bytes);
158 return errno < 0 ? errno : buf_off;
162 pcache_release(struct pcache* pcache)
164 struct pcache_pg *pos, *n;
165 llist_for_each(pos, n, &pcache->pages, pg_list)
167 lru_remove(&pos->lru);
171 btrie_release(&pcache->tree);
175 pcache_commit(struct v_inode* inode, struct pcache_pg* page)
177 if (!(page->flags & PCACHE_DIRTY)) {
181 int errno = inode->default_fops.write(inode, page->pg, PG_SIZE, page->fpos);
184 page->flags &= ~PCACHE_DIRTY;
185 llist_delete(&page->dirty_list);
186 inode->pg_cache->n_dirty--;
193 pcache_commit_all(struct v_inode* inode)
195 struct pcache* cache = inode->pg_cache;
196 struct pcache_pg *pos, *n;
198 llist_for_each(pos, n, &cache->dirty, dirty_list)
200 pcache_commit(inode, pos);
205 pcache_invalidate(struct pcache* pcache, struct pcache_pg* page)
207 pcache_commit(pcache->master, page);
208 pcache_release_page(pcache, page);