feat: page caching layer for vfs
[lunaix-os.git] / lunaix-os / kernel / fs / pcache.c
1 #include <klibc/string.h>
2 #include <lunaix/ds/btrie.h>
3 #include <lunaix/fs.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>
9
10 #define PCACHE_DIRTY 0x1
11
12 void
13 pcache_init(struct pcache* pcache)
14 {
15     btrie_init(&pcache->tree, PG_SIZE_BITS);
16     llist_init_head(&pcache->dirty);
17     llist_init_head(&pcache->pages);
18 }
19
20 void
21 pcache_release_page(struct pcache* pcache, struct pcache_pg* page)
22 {
23     pmm_free_page(KERNEL_PID, page->pg);
24     vmm_del_mapping(PD_REFERENCED, page->pg);
25
26     llist_delete(&page->pg_list);
27
28     vfree(page);
29
30     pcache->n_pages--;
31 }
32
33 struct pcache_pg*
34 pcache_new_page(struct pcache* pcache, uint32_t index)
35 {
36     void* pg = pmm_alloc_page(KERNEL_PID, 0);
37     void* pg_v = vmm_vmap(pg, PG_SIZE, PG_PREM_URW);
38     struct pcache_pg* ppg = valloc(sizeof(struct pcache_pg));
39     ppg->pg = pg_v;
40
41     llist_append(&pcache->pages, &ppg->pg_list);
42     btrie_set(&pcache->tree, index, ppg);
43
44     return ppg;
45 }
46
47 void
48 pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg)
49 {
50     if (!(pg->flags & PCACHE_DIRTY)) {
51         pg->flags |= PCACHE_DIRTY;
52         pcache->n_dirty++;
53         llist_append(&pcache->dirty, &pg->dirty_list);
54     }
55 }
56
57 struct pcache_pg*
58 pcache_get_page(struct pcache* pcache,
59                 uint32_t index,
60                 uint32_t* offset,
61                 struct pcache_pg** page)
62 {
63     struct pcache_pg* pg = btrie_get(&pcache->tree, index);
64     int is_new = 0;
65     *offset = index & ((1 << pcache->tree.truncated) - 1);
66     if (!pg) {
67         pg = pcache_new_page(pcache, index);
68         pg->fpos = index - *offset;
69         pcache->n_pages++;
70         is_new = 1;
71     }
72     *page = pg;
73     return is_new;
74 }
75
76 int
77 pcache_write(struct v_file* file, void* data, uint32_t len)
78 {
79     uint32_t pg_off, buf_off = 0, fpos = file->f_pos;
80     struct pcache* pcache = file->inode->pg_cache;
81     struct pcache_pg* pg;
82
83     while (buf_off < len) {
84         pcache_get_page(pcache, fpos, &pg_off, &pg);
85         uint32_t wr_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
86         memcpy(pg->pg + pg_off, (data + buf_off), wr_bytes);
87
88         pcache_set_dirty(pcache, pg);
89
90         buf_off += wr_bytes;
91         fpos += wr_bytes;
92     }
93
94     return buf_off;
95 }
96
97 int
98 pcache_read(struct v_file* file, void* data, uint32_t len)
99 {
100     uint32_t pg_off, buf_off = 0, new_pg = 0, fpos = file->f_pos;
101     int errno = 0;
102     struct pcache* pcache = file->inode->pg_cache;
103     struct pcache_pg* pg;
104
105     while (buf_off < len) {
106         if (pcache_get_page(pcache, fpos, &pg_off, &pg)) {
107             // Filling up the page
108             errno = file->ops.read(file, pg->pg, PG_SIZE, pg->fpos);
109             if (errno > 0 && errno < PG_SIZE) {
110                 // EOF
111                 len = buf_off + errno;
112             } else if (errno < 0) {
113                 break;
114             }
115         }
116         uint32_t rd_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
117         memcpy((data + buf_off), pg->pg + pg_off, rd_bytes);
118
119         buf_off += rd_bytes;
120         fpos += rd_bytes;
121     }
122
123     return errno < 0 ? errno : buf_off;
124 }
125
126 void
127 pcache_release(struct pcache* pcache)
128 {
129     struct pcache_pg *pos, *n;
130     llist_for_each(pos, n, &pcache->pages, pg_list)
131     {
132         vfree(pos);
133     }
134
135     btrie_release(&pcache->tree);
136 }
137
138 int
139 pcache_commit(struct v_file* file, struct pcache_pg* page)
140 {
141     if (!(page->flags & PCACHE_DIRTY)) {
142         return;
143     }
144
145     int errno = file->ops.write(file, page->pg, PG_SIZE, page->fpos);
146
147     if (!errno) {
148         page->flags &= ~PCACHE_DIRTY;
149         llist_delete(&page->dirty_list);
150         file->inode->pg_cache->n_dirty--;
151     }
152
153     return errno;
154 }
155
156 void
157 pcache_commit_all(struct v_file* file)
158 {
159     struct pcache* cache = file->inode->pg_cache;
160     struct pcache_pg *pos, *n;
161     llist_for_each(pos, n, &cache->dirty, dirty_list)
162     {
163         pcache_commit(file, pos);
164     }
165 }
166
167 void
168 pcache_invalidate(struct v_file* file, struct pcache_pg* page)
169 {
170     pcache_commit(file, page);
171     pcache_release_page(&file->inode->pg_cache, page);
172 }