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_try_evict(struct lru_node* obj)
17 struct pcache_pg* page = container_of(obj, struct pcache_pg, lru);
18 pcache_invalidate(page->holder, page);
23 pcache_init(struct pcache* pcache)
25 btrie_init(&pcache->tree, PG_SIZE_BITS);
26 llist_init_head(&pcache->dirty);
27 llist_init_head(&pcache->pages);
28 pcache_zone = lru_new_zone(__pcache_try_evict);
32 pcache_release_page(struct pcache* pcache, struct pcache_pg* page)
36 llist_delete(&page->pg_list);
44 pcache_new_page(struct pcache* pcache, uint32_t index)
46 struct pcache_pg* ppg = vzalloc(sizeof(struct pcache_pg));
47 void* pg = valloc(PG_SIZE);
50 lru_evict_one(pcache_zone);
51 if (!ppg && !(ppg = vzalloc(sizeof(struct pcache_pg)))) {
55 if (!pg && !(pg = valloc(PG_SIZE))) {
63 llist_append(&pcache->pages, &ppg->pg_list);
64 btrie_set(&pcache->tree, index, ppg);
70 pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg)
72 if (!(pg->flags & PCACHE_DIRTY)) {
73 pg->flags |= PCACHE_DIRTY;
75 llist_append(&pcache->dirty, &pg->dirty_list);
80 pcache_get_page(struct pcache* pcache,
83 struct pcache_pg** page)
85 struct pcache_pg* pg = btrie_get(&pcache->tree, index);
87 *offset = index & ((1 << pcache->tree.truncated) - 1);
88 if (!pg && (pg = pcache_new_page(pcache, index))) {
89 pg->fpos = index - *offset;
94 lru_use_one(pcache_zone, &pg->lru);
100 pcache_write(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos)
102 uint32_t pg_off, buf_off = 0;
103 struct pcache* pcache = inode->pg_cache;
104 struct pcache_pg* pg;
106 while (buf_off < len) {
107 pcache_get_page(pcache, fpos, &pg_off, &pg);
112 uint32_t wr_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
113 memcpy(pg->pg + pg_off, (data + buf_off), wr_bytes);
115 pcache_set_dirty(pcache, pg);
125 pcache_read(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos)
127 uint32_t pg_off, buf_off = 0, new_pg = 0;
129 struct pcache* pcache = inode->pg_cache;
130 struct pcache_pg* pg;
132 while (buf_off < len) {
133 if (pcache_get_page(pcache, fpos, &pg_off, &pg)) {
139 // Filling up the page
140 errno = inode->default_fops.read(inode, pg->pg, PG_SIZE, pg->fpos);
141 if (errno >= 0 && errno < PG_SIZE) {
143 len = buf_off + errno;
144 } else if (errno < 0) {
148 uint32_t rd_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
149 memcpy((data + buf_off), pg->pg + pg_off, rd_bytes);
155 return errno < 0 ? errno : buf_off;
159 pcache_release(struct pcache* pcache)
161 struct pcache_pg *pos, *n;
162 llist_for_each(pos, n, &pcache->pages, pg_list)
164 lru_remove(pcache_zone, &pos->lru);
168 btrie_release(&pcache->tree);
172 pcache_commit(struct v_inode* inode, struct pcache_pg* page)
174 if (!(page->flags & PCACHE_DIRTY)) {
178 int errno = inode->default_fops.write(inode, page->pg, PG_SIZE, page->fpos);
181 page->flags &= ~PCACHE_DIRTY;
182 llist_delete(&page->dirty_list);
183 inode->pg_cache->n_dirty--;
190 pcache_commit_all(struct v_inode* inode)
192 if (!inode->pg_cache) {
196 struct pcache* cache = inode->pg_cache;
197 struct pcache_pg *pos, *n;
199 llist_for_each(pos, n, &cache->dirty, dirty_list)
201 pcache_commit(inode, pos);
206 pcache_invalidate(struct pcache* pcache, struct pcache_pg* page)
208 pcache_commit(pcache->master, page);
209 pcache_release_page(pcache, page);