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 u32_t mask = ((1 << pcache->tree.truncated) - 1);
88 *offset = index & mask;
89 if (!pg && (pg = pcache_new_page(pcache, index))) {
90 pg->fpos = index & ~mask;
95 lru_use_one(pcache_zone, &pg->lru);
101 pcache_write(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos)
103 uint32_t pg_off, buf_off = 0;
104 struct pcache* pcache = inode->pg_cache;
105 struct pcache_pg* pg;
107 while (buf_off < len) {
108 pcache_get_page(pcache, fpos, &pg_off, &pg);
113 uint32_t wr_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
114 memcpy(pg->pg + pg_off, (data + buf_off), wr_bytes);
116 pcache_set_dirty(pcache, pg);
118 pg->len = pg_off + wr_bytes;
127 pcache_read(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos)
129 uint32_t pg_off, buf_off = 0, new_pg = 0;
131 struct pcache* pcache = inode->pg_cache;
132 struct pcache_pg* pg;
134 while (buf_off < len) {
135 if (pcache_get_page(pcache, fpos, &pg_off, &pg)) {
141 // Filling up the page
143 inode->default_fops->read_page(inode, pg->pg, PG_SIZE, pg->fpos);
144 if (errno >= 0 && errno < PG_SIZE) {
146 len = MIN(len, buf_off + errno);
147 } else if (errno < 0) {
152 uint32_t rd_bytes = MIN(pg->len - pg_off, len - buf_off);
157 memcpy((data + buf_off), pg->pg + pg_off, rd_bytes);
163 return errno < 0 ? errno : buf_off;
167 pcache_release(struct pcache* pcache)
169 struct pcache_pg *pos, *n;
170 llist_for_each(pos, n, &pcache->pages, pg_list)
172 lru_remove(pcache_zone, &pos->lru);
176 btrie_release(&pcache->tree);
180 pcache_commit(struct v_inode* inode, struct pcache_pg* page)
182 if (!(page->flags & PCACHE_DIRTY)) {
187 inode->default_fops->write_page(inode, page->pg, PG_SIZE, page->fpos);
190 page->flags &= ~PCACHE_DIRTY;
191 llist_delete(&page->dirty_list);
192 inode->pg_cache->n_dirty--;
199 pcache_commit_all(struct v_inode* inode)
201 if (!inode->pg_cache) {
205 struct pcache* cache = inode->pg_cache;
206 struct pcache_pg *pos, *n;
208 llist_for_each(pos, n, &cache->dirty, dirty_list)
210 pcache_commit(inode, pos);
215 pcache_invalidate(struct pcache* pcache, struct pcache_pg* page)
217 pcache_commit(pcache->master, page);
218 pcache_release_page(pcache, page);