feat: a pseudo shell environment for basic interacting and testing purpose
[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 struct pcache_pg*
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     *offset = index & ((1 << pcache->tree.truncated) - 1);
88     if (!pg && (pg = pcache_new_page(pcache, index))) {
89         pg->fpos = index - *offset;
90         pcache->n_pages++;
91         is_new = 1;
92     }
93     if (pg)
94         lru_use_one(pcache_zone, &pg->lru);
95     *page = pg;
96     return is_new;
97 }
98
99 int
100 pcache_write(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos)
101 {
102     uint32_t pg_off, buf_off = 0;
103     struct pcache* pcache = inode->pg_cache;
104     struct pcache_pg* pg;
105
106     while (buf_off < len) {
107         pcache_get_page(pcache, fpos, &pg_off, &pg);
108         if (!pg) {
109             return ENOMEM;
110         }
111
112         uint32_t wr_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
113         memcpy(pg->pg + pg_off, (data + buf_off), wr_bytes);
114
115         pcache_set_dirty(pcache, pg);
116
117         buf_off += wr_bytes;
118         fpos += wr_bytes;
119     }
120
121     return buf_off;
122 }
123
124 int
125 pcache_read(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos)
126 {
127     uint32_t pg_off, buf_off = 0, new_pg = 0;
128     int errno = 0;
129     struct pcache* pcache = inode->pg_cache;
130     struct pcache_pg* pg;
131
132     while (buf_off < len) {
133         if (pcache_get_page(pcache, fpos, &pg_off, &pg)) {
134
135             if (!pg) {
136                 return ENOMEM;
137             }
138
139             // Filling up the page
140             errno = inode->default_fops->read(inode, pg->pg, PG_SIZE, pg->fpos);
141             if (errno >= 0 && errno < PG_SIZE) {
142                 // EOF
143                 len = buf_off + errno;
144             } else if (errno < 0) {
145                 break;
146             }
147         }
148         uint32_t rd_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
149         memcpy((data + buf_off), pg->pg + pg_off, rd_bytes);
150
151         buf_off += rd_bytes;
152         fpos += rd_bytes;
153     }
154
155     return errno < 0 ? errno : buf_off;
156 }
157
158 void
159 pcache_release(struct pcache* pcache)
160 {
161     struct pcache_pg *pos, *n;
162     llist_for_each(pos, n, &pcache->pages, pg_list)
163     {
164         lru_remove(pcache_zone, &pos->lru);
165         vfree(pos);
166     }
167
168     btrie_release(&pcache->tree);
169 }
170
171 int
172 pcache_commit(struct v_inode* inode, struct pcache_pg* page)
173 {
174     if (!(page->flags & PCACHE_DIRTY)) {
175         return;
176     }
177
178     int errno =
179       inode->default_fops->write(inode, page->pg, PG_SIZE, page->fpos);
180
181     if (!errno) {
182         page->flags &= ~PCACHE_DIRTY;
183         llist_delete(&page->dirty_list);
184         inode->pg_cache->n_dirty--;
185     }
186
187     return errno;
188 }
189
190 void
191 pcache_commit_all(struct v_inode* inode)
192 {
193     if (!inode->pg_cache) {
194         return;
195     }
196
197     struct pcache* cache = inode->pg_cache;
198     struct pcache_pg *pos, *n;
199
200     llist_for_each(pos, n, &cache->dirty, dirty_list)
201     {
202         pcache_commit(inode, pos);
203     }
204 }
205
206 void
207 pcache_invalidate(struct pcache* pcache, struct pcache_pg* page)
208 {
209     pcache_commit(pcache->master, page);
210     pcache_release_page(pcache, page);
211 }