1 #include <klibc/string.h>
2 #include <lunaix/ds/btrie.h>
4 #include <lunaix/mm/pmm.h>
5 #include <lunaix/mm/valloc.h>
6 #include <lunaix/mm/vmm.h>
7 #include <lunaix/spike.h>
9 #define PCACHE_DIRTY 0x1
11 static struct lru_zone* pcache_zone;
14 __pcache_try_evict(struct lru_node* obj)
16 struct pcache_pg* page = container_of(obj, struct pcache_pg, lru);
17 pcache_invalidate(page->holder, page);
22 pcache_free_page(void* va)
24 ptr_t pa = vmm_del_mapping(VMS_SELF, (ptr_t)va);
32 ptr_t pp = pmm_alloc_page(0), va = 0;
38 if (!(va = (ptr_t)vmap(pp, PAGE_SIZE, KERNEL_DATA))) {
47 pcache_init(struct pcache* pcache)
49 btrie_init(&pcache->tree, PAGE_SHIFT);
50 llist_init_head(&pcache->dirty);
51 llist_init_head(&pcache->pages);
53 pcache_zone = lru_new_zone(__pcache_try_evict);
57 pcache_release_page(struct pcache* pcache, struct pcache_pg* page)
59 pcache_free_page(page->pg);
61 llist_delete(&page->pg_list);
63 btrie_remove(&pcache->tree, page->fpos);
71 pcache_new_page(struct pcache* pcache, u32_t index)
73 struct pcache_pg* ppg = vzalloc(sizeof(struct pcache_pg));
74 void* pg = pcache_alloc_page();
77 lru_evict_one(pcache_zone);
78 if (!ppg && !(ppg = vzalloc(sizeof(struct pcache_pg)))) {
82 if (!pg && !(pg = pcache_alloc_page())) {
90 llist_append(&pcache->pages, &ppg->pg_list);
91 btrie_set(&pcache->tree, index, ppg);
97 pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg)
99 if (!(pg->flags & PCACHE_DIRTY)) {
100 pg->flags |= PCACHE_DIRTY;
102 llist_append(&pcache->dirty, &pg->dirty_list);
107 pcache_get_page(struct pcache* pcache,
110 struct pcache_pg** page)
112 struct pcache_pg* pg = btrie_get(&pcache->tree, index);
114 u32_t mask = ((1 << pcache->tree.truncated) - 1);
115 *offset = index & mask;
116 if (!pg && (pg = pcache_new_page(pcache, index))) {
117 pg->fpos = index & ~mask;
122 lru_use_one(pcache_zone, &pg->lru);
128 pcache_write(struct v_inode* inode, void* data, u32_t len, u32_t fpos)
131 u32_t pg_off, buf_off = 0;
132 struct pcache* pcache = inode->pg_cache;
133 struct pcache_pg* pg;
135 while (buf_off < len && errno >= 0) {
136 u32_t wr_bytes = MIN(PAGE_SIZE - pg_off, len - buf_off);
138 int new_page = pcache_get_page(pcache, fpos, &pg_off, &pg);
141 // Filling up the page
142 errno = inode->default_fops->read_page(inode, pg->pg, pg->fpos);
147 if (errno < (int)PAGE_SIZE) {
149 len = MIN(len, buf_off + errno);
152 errno = inode->default_fops->write(inode, data, wr_bytes, fpos);
156 memcpy(pg->pg + pg_off, (data + buf_off), wr_bytes);
157 pcache_set_dirty(pcache, pg);
159 pg->len = pg_off + wr_bytes;
164 return errno < 0 ? errno : (int)buf_off;
168 pcache_read(struct v_inode* inode, void* data, u32_t len, u32_t fpos)
170 u32_t pg_off, buf_off = 0, new_pg = 0;
172 struct pcache* pcache = inode->pg_cache;
173 struct pcache_pg* pg;
175 while (buf_off < len) {
176 int new_page = pcache_get_page(pcache, fpos, &pg_off, &pg);
178 // Filling up the page
179 errno = inode->default_fops->read_page(inode, pg->pg, pg->fpos);
184 if (errno < (int)PAGE_SIZE) {
186 len = MIN(len, buf_off + errno);
191 errno = inode->default_fops->read(
192 inode, (data + buf_off), len - buf_off, pg->fpos);
197 u32_t rd_bytes = MIN(pg->len - pg_off, len - buf_off);
202 memcpy((data + buf_off), pg->pg + pg_off, rd_bytes);
208 return errno < 0 ? errno : (int)buf_off;
212 pcache_release(struct pcache* pcache)
214 struct pcache_pg *pos, *n;
215 llist_for_each(pos, n, &pcache->pages, pg_list)
217 lru_remove(pcache_zone, &pos->lru);
221 btrie_release(&pcache->tree);
225 pcache_commit(struct v_inode* inode, struct pcache_pg* page)
227 if (!(page->flags & PCACHE_DIRTY)) {
231 int errno = inode->default_fops->write_page(inode, page->pg, page->fpos);
234 page->flags &= ~PCACHE_DIRTY;
235 llist_delete(&page->dirty_list);
236 inode->pg_cache->n_dirty--;
243 pcache_commit_all(struct v_inode* inode)
245 if (!inode->pg_cache) {
249 struct pcache* cache = inode->pg_cache;
250 struct pcache_pg *pos, *n;
252 llist_for_each(pos, n, &cache->dirty, dirty_list)
254 pcache_commit(inode, pos);
259 pcache_invalidate(struct pcache* pcache, struct pcache_pg* page)
261 pcache_commit(pcache->master, page);
262 pcache_release_page(pcache, page);