317e491a336284268a6f827ae82d7aec1841e4ae
[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, uint32_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                 uint32_t index,
82                 uint32_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, uint32_t len, uint32_t fpos)
102 {
103     uint32_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         uint32_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         buf_off += wr_bytes;
119         fpos += wr_bytes;
120     }
121
122     return buf_off;
123 }
124
125 int
126 pcache_read(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos)
127 {
128     uint32_t pg_off, buf_off = 0, new_pg = 0;
129     int errno = 0;
130     struct pcache* pcache = inode->pg_cache;
131     struct pcache_pg* pg;
132
133     while (buf_off < len) {
134         if (pcache_get_page(pcache, fpos, &pg_off, &pg)) {
135
136             if (!pg) {
137                 return ENOMEM;
138             }
139
140             // Filling up the page
141             errno =
142               inode->default_fops->read_page(inode, pg->pg, PG_SIZE, pg->fpos);
143             if (errno >= 0 && errno < PG_SIZE) {
144                 // EOF
145                 len = MIN(len, buf_off + errno);
146             } else if (errno < 0) {
147                 break;
148             }
149         }
150         uint32_t rd_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
151         memcpy((data + buf_off), pg->pg + pg_off, rd_bytes);
152
153         buf_off += rd_bytes;
154         fpos += rd_bytes;
155     }
156
157     return errno < 0 ? errno : buf_off;
158 }
159
160 void
161 pcache_release(struct pcache* pcache)
162 {
163     struct pcache_pg *pos, *n;
164     llist_for_each(pos, n, &pcache->pages, pg_list)
165     {
166         lru_remove(pcache_zone, &pos->lru);
167         vfree(pos);
168     }
169
170     btrie_release(&pcache->tree);
171 }
172
173 int
174 pcache_commit(struct v_inode* inode, struct pcache_pg* page)
175 {
176     if (!(page->flags & PCACHE_DIRTY)) {
177         return;
178     }
179
180     int errno =
181       inode->default_fops->write_page(inode, page->pg, PG_SIZE, page->fpos);
182
183     if (!errno) {
184         page->flags &= ~PCACHE_DIRTY;
185         llist_delete(&page->dirty_list);
186         inode->pg_cache->n_dirty--;
187     }
188
189     return errno;
190 }
191
192 void
193 pcache_commit_all(struct v_inode* inode)
194 {
195     if (!inode->pg_cache) {
196         return;
197     }
198
199     struct pcache* cache = inode->pg_cache;
200     struct pcache_pg *pos, *n;
201
202     llist_for_each(pos, n, &cache->dirty, dirty_list)
203     {
204         pcache_commit(inode, pos);
205     }
206 }
207
208 void
209 pcache_invalidate(struct pcache* pcache, struct pcache_pg* page)
210 {
211     pcache_commit(pcache->master, page);
212     pcache_release_page(pcache, page);
213 }