Merge branch 'master' into prog-loader
[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 void
23 pcache_init(struct pcache* pcache)
24 {
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);
29 }
30
31 void
32 pcache_release_page(struct pcache* pcache, struct pcache_pg* page)
33 {
34     vfree(page->pg);
35
36     llist_delete(&page->pg_list);
37
38     vfree(page);
39
40     pcache->n_pages--;
41 }
42
43 struct pcache_pg*
44 pcache_new_page(struct pcache* pcache, u32_t index)
45 {
46     struct pcache_pg* ppg = vzalloc(sizeof(struct pcache_pg));
47     void* pg = valloc(PG_SIZE);
48
49     if (!ppg || !pg) {
50         lru_evict_one(pcache_zone);
51         if (!ppg && !(ppg = vzalloc(sizeof(struct pcache_pg)))) {
52             return NULL;
53         }
54
55         if (!pg && !(pg = valloc(PG_SIZE))) {
56             return NULL;
57         }
58     }
59
60     ppg->pg = pg;
61     ppg->holder = pcache;
62
63     llist_append(&pcache->pages, &ppg->pg_list);
64     btrie_set(&pcache->tree, index, ppg);
65
66     return ppg;
67 }
68
69 void
70 pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg)
71 {
72     if (!(pg->flags & PCACHE_DIRTY)) {
73         pg->flags |= PCACHE_DIRTY;
74         pcache->n_dirty++;
75         llist_append(&pcache->dirty, &pg->dirty_list);
76     }
77 }
78
79 int
80 pcache_get_page(struct pcache* pcache,
81                 u32_t index,
82                 u32_t* offset,
83                 struct pcache_pg** page)
84 {
85     struct pcache_pg* pg = btrie_get(&pcache->tree, index);
86     int is_new = 0;
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;
91         pcache->n_pages++;
92         is_new = 1;
93     }
94     if (pg)
95         lru_use_one(pcache_zone, &pg->lru);
96     *page = pg;
97     return is_new;
98 }
99
100 int
101 pcache_write(struct v_inode* inode, void* data, u32_t len, u32_t fpos)
102 {
103     u32_t pg_off, buf_off = 0;
104     struct pcache* pcache = inode->pg_cache;
105     struct pcache_pg* pg;
106
107     while (buf_off < len) {
108         pcache_get_page(pcache, fpos, &pg_off, &pg);
109         if (!pg) {
110             return ENOMEM;
111         }
112
113         u32_t wr_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
114         memcpy(pg->pg + pg_off, (data + buf_off), wr_bytes);
115
116         pcache_set_dirty(pcache, pg);
117
118         pg->len = pg_off + wr_bytes;
119         buf_off += wr_bytes;
120         fpos += wr_bytes;
121     }
122
123     return buf_off;
124 }
125
126 int
127 pcache_read(struct v_inode* inode, void* data, u32_t len, u32_t fpos)
128 {
129     u32_t pg_off, buf_off = 0, new_pg = 0;
130     int errno = 0;
131     struct pcache* pcache = inode->pg_cache;
132     struct pcache_pg* pg;
133
134     while (buf_off < len) {
135         if (pcache_get_page(pcache, fpos, &pg_off, &pg)) {
136
137             if (!pg) {
138                 return ENOMEM;
139             }
140
141             // Filling up the page
142             errno =
143               inode->default_fops->read_page(inode, pg->pg, PG_SIZE, pg->fpos);
144             if (errno >= 0 && errno < PG_SIZE) {
145                 // EOF
146                 len = MIN(len, buf_off + errno);
147             } else if (errno < 0) {
148                 break;
149             }
150             pg->len = errno;
151         }
152         u32_t rd_bytes = MIN(pg->len - pg_off, len - buf_off);
153
154         if (!rd_bytes)
155             break;
156
157         memcpy((data + buf_off), pg->pg + pg_off, rd_bytes);
158
159         buf_off += rd_bytes;
160         fpos += rd_bytes;
161     }
162
163     return errno < 0 ? errno : buf_off;
164 }
165
166 void
167 pcache_release(struct pcache* pcache)
168 {
169     struct pcache_pg *pos, *n;
170     llist_for_each(pos, n, &pcache->pages, pg_list)
171     {
172         lru_remove(pcache_zone, &pos->lru);
173         vfree(pos);
174     }
175
176     btrie_release(&pcache->tree);
177 }
178
179 int
180 pcache_commit(struct v_inode* inode, struct pcache_pg* page)
181 {
182     if (!(page->flags & PCACHE_DIRTY)) {
183         return;
184     }
185
186     int errno =
187       inode->default_fops->write_page(inode, page->pg, PG_SIZE, page->fpos);
188
189     if (!errno) {
190         page->flags &= ~PCACHE_DIRTY;
191         llist_delete(&page->dirty_list);
192         inode->pg_cache->n_dirty--;
193     }
194
195     return errno;
196 }
197
198 void
199 pcache_commit_all(struct v_inode* inode)
200 {
201     if (!inode->pg_cache) {
202         return;
203     }
204
205     struct pcache* cache = inode->pg_cache;
206     struct pcache_pg *pos, *n;
207
208     llist_for_each(pos, n, &cache->dirty, dirty_list)
209     {
210         pcache_commit(inode, pos);
211     }
212 }
213
214 void
215 pcache_invalidate(struct pcache* pcache, struct pcache_pg* page)
216 {
217     pcache_commit(pcache->master, page);
218     pcache_release_page(pcache, page);
219 }