5-malloc.md (#25)
[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(pa);
27 }
28
29 static void*
30 pcache_alloc_page()
31 {
32     int i = 0;
33     ptr_t pp = pmm_alloc_page(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(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 && errno >= 0) {
137         u32_t wr_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
138
139         int new_page = pcache_get_page(pcache, fpos, &pg_off, &pg);
140
141         if (new_page) {
142             // Filling up the page
143             errno = inode->default_fops->read_page(inode, pg->pg, pg->fpos);
144
145             if (errno < 0) {
146                 break;
147             }
148             if (errno < PG_SIZE) {
149                 // EOF
150                 len = MIN(len, buf_off + errno);
151             }
152         } else if (!pg) {
153             errno = inode->default_fops->write(inode, data, wr_bytes, fpos);
154             continue;
155         }
156
157         memcpy(pg->pg + pg_off, (data + buf_off), wr_bytes);
158         pcache_set_dirty(pcache, pg);
159
160         pg->len = pg_off + wr_bytes;
161         buf_off += wr_bytes;
162         fpos += wr_bytes;
163     }
164
165     return errno < 0 ? errno : (int)buf_off;
166 }
167
168 int
169 pcache_read(struct v_inode* inode, void* data, u32_t len, u32_t fpos)
170 {
171     u32_t pg_off, buf_off = 0, new_pg = 0;
172     int errno = 0;
173     struct pcache* pcache = inode->pg_cache;
174     struct pcache_pg* pg;
175
176     while (buf_off < len) {
177         int new_page = pcache_get_page(pcache, fpos, &pg_off, &pg);
178         if (new_page) {
179             // Filling up the page
180             errno = inode->default_fops->read_page(inode, pg->pg, pg->fpos);
181
182             if (errno < 0) {
183                 break;
184             }
185             if (errno < PG_SIZE) {
186                 // EOF
187                 len = MIN(len, buf_off + errno);
188             }
189
190             pg->len = errno;
191         } else if (!pg) {
192             errno = inode->default_fops->read(
193               inode, (data + buf_off), len - buf_off, pg->fpos);
194             buf_off = len;
195             break;
196         }
197
198         u32_t rd_bytes = MIN(pg->len - pg_off, len - buf_off);
199
200         if (!rd_bytes)
201             break;
202
203         memcpy((data + buf_off), pg->pg + pg_off, rd_bytes);
204
205         buf_off += rd_bytes;
206         fpos += rd_bytes;
207     }
208
209     return errno < 0 ? errno : (int)buf_off;
210 }
211
212 void
213 pcache_release(struct pcache* pcache)
214 {
215     struct pcache_pg *pos, *n;
216     llist_for_each(pos, n, &pcache->pages, pg_list)
217     {
218         lru_remove(pcache_zone, &pos->lru);
219         vfree(pos);
220     }
221
222     btrie_release(&pcache->tree);
223 }
224
225 int
226 pcache_commit(struct v_inode* inode, struct pcache_pg* page)
227 {
228     if (!(page->flags & PCACHE_DIRTY)) {
229         return 0;
230     }
231
232     int errno = inode->default_fops->write_page(inode, page->pg, page->fpos);
233
234     if (!errno) {
235         page->flags &= ~PCACHE_DIRTY;
236         llist_delete(&page->dirty_list);
237         inode->pg_cache->n_dirty--;
238     }
239
240     return errno;
241 }
242
243 void
244 pcache_commit_all(struct v_inode* inode)
245 {
246     if (!inode->pg_cache) {
247         return;
248     }
249
250     struct pcache* cache = inode->pg_cache;
251     struct pcache_pg *pos, *n;
252
253     llist_for_each(pos, n, &cache->dirty, dirty_list)
254     {
255         pcache_commit(inode, pos);
256     }
257 }
258
259 void
260 pcache_invalidate(struct pcache* pcache, struct pcache_pg* page)
261 {
262     pcache_commit(pcache->master, page);
263     pcache_release_page(pcache, page);
264 }