fix: separate any i/o to sequential device from caching layer
[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 void
13 pcache_init(struct pcache* pcache)
14 {
15     btrie_init(&pcache->tree, PG_SIZE_BITS);
16     llist_init_head(&pcache->dirty);
17     llist_init_head(&pcache->pages);
18 }
19
20 void
21 pcache_release_page(struct pcache* pcache, struct pcache_pg* page)
22 {
23     pmm_free_page(KERNEL_PID, page->pg);
24     vmm_del_mapping(PD_REFERENCED, page->pg);
25
26     llist_delete(&page->pg_list);
27
28     vfree(page);
29
30     pcache->n_pages--;
31 }
32
33 struct pcache_pg*
34 pcache_new_page(struct pcache* pcache, uint32_t index)
35 {
36     void* pg = pmm_alloc_page(KERNEL_PID, 0);
37     void* pg_v = vmm_vmap(pg, PG_SIZE, PG_PREM_URW);
38     struct pcache_pg* ppg = vzalloc(sizeof(struct pcache_pg));
39     ppg->pg = pg_v;
40
41     llist_append(&pcache->pages, &ppg->pg_list);
42     btrie_set(&pcache->tree, index, ppg);
43
44     return ppg;
45 }
46
47 void
48 pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg)
49 {
50     if (!(pg->flags & PCACHE_DIRTY)) {
51         pg->flags |= PCACHE_DIRTY;
52         pcache->n_dirty++;
53         llist_append(&pcache->dirty, &pg->dirty_list);
54     }
55 }
56
57 struct pcache_pg*
58 pcache_get_page(struct pcache* pcache,
59                 uint32_t index,
60                 uint32_t* offset,
61                 struct pcache_pg** page)
62 {
63     struct pcache_pg* pg = btrie_get(&pcache->tree, index);
64     int is_new = 0;
65     *offset = index & ((1 << pcache->tree.truncated) - 1);
66     if (!pg) {
67         pg = pcache_new_page(pcache, index);
68         pg->fpos = index - *offset;
69         pcache->n_pages++;
70         is_new = 1;
71     }
72     *page = pg;
73     return is_new;
74 }
75
76 int
77 pcache_write(struct v_file* file, void* data, uint32_t len, uint32_t fpos)
78 {
79     uint32_t pg_off, buf_off = 0;
80     struct pcache* pcache = file->inode->pg_cache;
81     struct pcache_pg* pg;
82
83     while (buf_off < len) {
84         pcache_get_page(pcache, fpos, &pg_off, &pg);
85         uint32_t wr_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
86         memcpy(pg->pg + pg_off, (data + buf_off), wr_bytes);
87
88         pcache_set_dirty(pcache, pg);
89
90         buf_off += wr_bytes;
91         fpos += wr_bytes;
92     }
93
94     return buf_off;
95 }
96
97 int
98 pcache_read(struct v_file* file, void* data, uint32_t len, uint32_t fpos)
99 {
100     uint32_t pg_off, buf_off = 0, new_pg = 0;
101     int errno = 0;
102     struct pcache* pcache = file->inode->pg_cache;
103     struct pcache_pg* pg;
104     struct v_inode* inode = file->inode;
105
106     while (buf_off < len) {
107         if (pcache_get_page(pcache, fpos, &pg_off, &pg)) {
108             // Filling up the page
109             errno = inode->default_fops.read(file, pg->pg, PG_SIZE, pg->fpos);
110             if (errno >= 0 && errno < PG_SIZE) {
111                 // EOF
112                 len = buf_off + errno;
113             } else if (errno < 0) {
114                 break;
115             }
116         }
117         uint32_t rd_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
118         memcpy((data + buf_off), pg->pg + pg_off, rd_bytes);
119
120         buf_off += rd_bytes;
121         fpos += rd_bytes;
122     }
123
124     return errno < 0 ? errno : buf_off;
125 }
126
127 void
128 pcache_release(struct pcache* pcache)
129 {
130     struct pcache_pg *pos, *n;
131     llist_for_each(pos, n, &pcache->pages, pg_list)
132     {
133         vfree(pos);
134     }
135
136     btrie_release(&pcache->tree);
137 }
138
139 int
140 pcache_commit(struct v_file* file, struct pcache_pg* page)
141 {
142     if (!(page->flags & PCACHE_DIRTY)) {
143         return;
144     }
145
146     struct v_inode* inode = file->inode;
147     int errno = inode->default_fops.write(file, page->pg, PG_SIZE, page->fpos);
148
149     if (!errno) {
150         page->flags &= ~PCACHE_DIRTY;
151         llist_delete(&page->dirty_list);
152         file->inode->pg_cache->n_dirty--;
153     }
154
155     return errno;
156 }
157
158 void
159 pcache_commit_all(struct v_file* file)
160 {
161     struct pcache* cache = file->inode->pg_cache;
162     struct pcache_pg *pos, *n;
163     llist_for_each(pos, n, &cache->dirty, dirty_list)
164     {
165         pcache_commit(file, pos);
166     }
167 }
168
169 void
170 pcache_invalidate(struct v_file* file, struct pcache_pg* page)
171 {
172     pcache_commit(file, page);
173     pcache_release_page(&file->inode->pg_cache, page);
174 }