f1969bd524473f225a9beffa0c3b6d411f8053ea
[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 static struct lru_zone* pcache_zone;
13
14 static int
15 __pcache_try_evict(struct lru_node* obj)
16 {
17     struct pcache_pg* page = container_of(obj, struct pcache_pg, lru);
18     pcache_invalidate(page->holder, page);
19     return 1;
20 }
21
22 static void
23 pcache_free_page(void* va)
24 {
25     ptr_t pa = vmm_del_mapping(VMS_SELF, (ptr_t)va);
26     pmm_free_page(KERNEL_PID, pa);
27 }
28
29 static void*
30 pcache_alloc_page()
31 {
32     int i = 0;
33     ptr_t pp = pmm_alloc_page(KERNEL_PID, 0), va = 0;
34
35     if (!pp) {
36         return NULL;
37     }
38
39     if (!(va = (ptr_t)vmap(pp, PG_SIZE, PG_PREM_RW, 0))) {
40         pmm_free_page(KERNEL_PID, pp);
41         return NULL;
42     }
43
44     return (void*)va;
45 }
46
47 void
48 pcache_init(struct pcache* pcache)
49 {
50     btrie_init(&pcache->tree, PG_SIZE_BITS);
51     llist_init_head(&pcache->dirty);
52     llist_init_head(&pcache->pages);
53
54     pcache_zone = lru_new_zone(__pcache_try_evict);
55 }
56
57 void
58 pcache_release_page(struct pcache* pcache, struct pcache_pg* page)
59 {
60     pcache_free_page(page->pg);
61
62     llist_delete(&page->pg_list);
63
64     btrie_remove(&pcache->tree, page->fpos);
65
66     vfree(page);
67
68     pcache->n_pages--;
69 }
70
71 struct pcache_pg*
72 pcache_new_page(struct pcache* pcache, u32_t index)
73 {
74     struct pcache_pg* ppg = vzalloc(sizeof(struct pcache_pg));
75     void* pg = pcache_alloc_page();
76
77     if (!ppg || !pg) {
78         lru_evict_one(pcache_zone);
79         if (!ppg && !(ppg = vzalloc(sizeof(struct pcache_pg)))) {
80             return NULL;
81         }
82
83         if (!pg && !(pg = pcache_alloc_page())) {
84             return NULL;
85         }
86     }
87
88     ppg->pg = pg;
89     ppg->holder = pcache;
90
91     llist_append(&pcache->pages, &ppg->pg_list);
92     btrie_set(&pcache->tree, index, ppg);
93
94     return ppg;
95 }
96
97 void
98 pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg)
99 {
100     if (!(pg->flags & PCACHE_DIRTY)) {
101         pg->flags |= PCACHE_DIRTY;
102         pcache->n_dirty++;
103         llist_append(&pcache->dirty, &pg->dirty_list);
104     }
105 }
106
107 int
108 pcache_get_page(struct pcache* pcache,
109                 u32_t index,
110                 u32_t* offset,
111                 struct pcache_pg** page)
112 {
113     struct pcache_pg* pg = btrie_get(&pcache->tree, index);
114     int is_new = 0;
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;
119         pcache->n_pages++;
120         is_new = 1;
121     }
122     if (pg)
123         lru_use_one(pcache_zone, &pg->lru);
124     *page = pg;
125     return is_new;
126 }
127
128 int
129 pcache_write(struct v_inode* inode, void* data, u32_t len, u32_t fpos)
130 {
131     int errno = 0;
132     u32_t pg_off, buf_off = 0;
133     struct pcache* pcache = inode->pg_cache;
134     struct pcache_pg* pg;
135
136     while (buf_off < len) {
137         u32_t wr_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
138
139         pcache_get_page(pcache, fpos, &pg_off, &pg);
140
141         if (!pg) {
142             errno = inode->default_fops->write(inode, data, wr_bytes, fpos);
143             if (errno < 0) {
144                 break;
145             }
146         } else {
147             memcpy(pg->pg + pg_off, (data + buf_off), wr_bytes);
148             pcache_set_dirty(pcache, pg);
149
150             pg->len = pg_off + wr_bytes;
151         }
152
153         buf_off += wr_bytes;
154         fpos += wr_bytes;
155     }
156
157     return errno < 0 ? errno : (int)buf_off;
158 }
159
160 int
161 pcache_read(struct v_inode* inode, void* data, u32_t len, u32_t fpos)
162 {
163     u32_t pg_off, buf_off = 0, new_pg = 0;
164     int errno = 0;
165     struct pcache* pcache = inode->pg_cache;
166     struct pcache_pg* pg;
167
168     while (buf_off < len) {
169         int new_page = pcache_get_page(pcache, fpos, &pg_off, &pg);
170         if (new_page) {
171             // Filling up the page
172             errno =
173               inode->default_fops->read_page(inode, pg->pg, PG_SIZE, pg->fpos);
174
175             if (errno < 0) {
176                 break;
177             }
178             if (errno < PG_SIZE) {
179                 // EOF
180                 len = MIN(len, buf_off + errno);
181             }
182
183             pg->len = errno;
184         } else if (!pg) {
185             errno = inode->default_fops->read_page(
186               inode, (data + buf_off), len - buf_off, pg->fpos);
187             buf_off = len;
188             break;
189         }
190
191         u32_t rd_bytes = MIN(pg->len - pg_off, len - buf_off);
192
193         if (!rd_bytes)
194             break;
195
196         memcpy((data + buf_off), pg->pg + pg_off, rd_bytes);
197
198         buf_off += rd_bytes;
199         fpos += rd_bytes;
200     }
201
202     return errno < 0 ? errno : (int)buf_off;
203 }
204
205 void
206 pcache_release(struct pcache* pcache)
207 {
208     struct pcache_pg *pos, *n;
209     llist_for_each(pos, n, &pcache->pages, pg_list)
210     {
211         lru_remove(pcache_zone, &pos->lru);
212         vfree(pos);
213     }
214
215     btrie_release(&pcache->tree);
216 }
217
218 int
219 pcache_commit(struct v_inode* inode, struct pcache_pg* page)
220 {
221     if (!(page->flags & PCACHE_DIRTY)) {
222         return 0;
223     }
224
225     int errno =
226       inode->default_fops->write_page(inode, page->pg, PG_SIZE, page->fpos);
227
228     if (!errno) {
229         page->flags &= ~PCACHE_DIRTY;
230         llist_delete(&page->dirty_list);
231         inode->pg_cache->n_dirty--;
232     }
233
234     return errno;
235 }
236
237 void
238 pcache_commit_all(struct v_inode* inode)
239 {
240     if (!inode->pg_cache) {
241         return;
242     }
243
244     struct pcache* cache = inode->pg_cache;
245     struct pcache_pg *pos, *n;
246
247     llist_for_each(pos, n, &cache->dirty, dirty_list)
248     {
249         pcache_commit(inode, pos);
250     }
251 }
252
253 void
254 pcache_invalidate(struct pcache* pcache, struct pcache_pg* page)
255 {
256     pcache_commit(pcache->master, page);
257     pcache_release_page(pcache, page);
258 }