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_free_page(void* va)
25 ptr_t pa = vmm_del_mapping(VMS_SELF, (ptr_t)va);
26 pmm_free_page(KERNEL_PID, pa);
33 ptr_t pp = pmm_alloc_page(KERNEL_PID, 0), va = 0;
39 if (!(va = (ptr_t)vmap(pp, PG_SIZE, PG_PREM_RW, 0))) {
40 pmm_free_page(KERNEL_PID, pp);
48 pcache_init(struct pcache* pcache)
50 btrie_init(&pcache->tree, PG_SIZE_BITS);
51 llist_init_head(&pcache->dirty);
52 llist_init_head(&pcache->pages);
54 pcache_zone = lru_new_zone(__pcache_try_evict);
58 pcache_release_page(struct pcache* pcache, struct pcache_pg* page)
60 pcache_free_page(page->pg);
62 llist_delete(&page->pg_list);
64 btrie_remove(&pcache->tree, page->fpos);
72 pcache_new_page(struct pcache* pcache, u32_t index)
74 struct pcache_pg* ppg = vzalloc(sizeof(struct pcache_pg));
75 void* pg = pcache_alloc_page();
78 lru_evict_one(pcache_zone);
79 if (!ppg && !(ppg = vzalloc(sizeof(struct pcache_pg)))) {
83 if (!pg && !(pg = pcache_alloc_page())) {
91 llist_append(&pcache->pages, &ppg->pg_list);
92 btrie_set(&pcache->tree, index, ppg);
98 pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg)
100 if (!(pg->flags & PCACHE_DIRTY)) {
101 pg->flags |= PCACHE_DIRTY;
103 llist_append(&pcache->dirty, &pg->dirty_list);
108 pcache_get_page(struct pcache* pcache,
111 struct pcache_pg** page)
113 struct pcache_pg* pg = btrie_get(&pcache->tree, index);
115 u32_t mask = ((1 << pcache->tree.truncated) - 1);
116 *offset = index & mask;
117 if (!pg && (pg = pcache_new_page(pcache, index))) {
118 pg->fpos = index & ~mask;
123 lru_use_one(pcache_zone, &pg->lru);
129 pcache_write(struct v_inode* inode, void* data, u32_t len, u32_t fpos)
132 u32_t pg_off, buf_off = 0;
133 struct pcache* pcache = inode->pg_cache;
134 struct pcache_pg* pg;
136 while (buf_off < len && errno >= 0) {
137 u32_t wr_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
139 int new_page = pcache_get_page(pcache, fpos, &pg_off, &pg);
142 // Filling up the page
144 inode->default_fops->read_page(inode, pg->pg, PG_SIZE, pg->fpos);
149 if (errno < PG_SIZE) {
151 len = MIN(len, buf_off + errno);
154 errno = inode->default_fops->write(inode, data, wr_bytes, fpos);
158 memcpy(pg->pg + pg_off, (data + buf_off), wr_bytes);
159 pcache_set_dirty(pcache, pg);
161 pg->len = pg_off + wr_bytes;
166 return errno < 0 ? errno : (int)buf_off;
170 pcache_read(struct v_inode* inode, void* data, u32_t len, u32_t fpos)
172 u32_t pg_off, buf_off = 0, new_pg = 0;
174 struct pcache* pcache = inode->pg_cache;
175 struct pcache_pg* pg;
177 while (buf_off < len) {
178 int new_page = pcache_get_page(pcache, fpos, &pg_off, &pg);
180 // Filling up the page
182 inode->default_fops->read_page(inode, pg->pg, PG_SIZE, pg->fpos);
187 if (errno < PG_SIZE) {
189 len = MIN(len, buf_off + errno);
194 errno = inode->default_fops->read_page(
195 inode, (data + buf_off), len - buf_off, pg->fpos);
200 u32_t rd_bytes = MIN(pg->len - pg_off, len - buf_off);
205 memcpy((data + buf_off), pg->pg + pg_off, rd_bytes);
211 return errno < 0 ? errno : (int)buf_off;
215 pcache_release(struct pcache* pcache)
217 struct pcache_pg *pos, *n;
218 llist_for_each(pos, n, &pcache->pages, pg_list)
220 lru_remove(pcache_zone, &pos->lru);
224 btrie_release(&pcache->tree);
228 pcache_commit(struct v_inode* inode, struct pcache_pg* page)
230 if (!(page->flags & PCACHE_DIRTY)) {
235 inode->default_fops->write_page(inode, page->pg, PG_SIZE, page->fpos);
238 page->flags &= ~PCACHE_DIRTY;
239 llist_delete(&page->dirty_list);
240 inode->pg_cache->n_dirty--;
247 pcache_commit_all(struct v_inode* inode)
249 if (!inode->pg_cache) {
253 struct pcache* cache = inode->pg_cache;
254 struct pcache_pg *pos, *n;
256 llist_for_each(pos, n, &cache->dirty, dirty_list)
258 pcache_commit(inode, pos);
263 pcache_invalidate(struct pcache* pcache, struct pcache_pg* page)
265 pcache_commit(pcache->master, page);
266 pcache_release_page(pcache, page);