Skip to content

Commit 9a9bd38

Browse files
committed
lxfs: implemented read cache
1 parent 3acdc12 commit 9a9bd38

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

fs/lxfs/src/blockio.c

+33-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include <lxfs/lxfs.h>
99
#include <unistd.h>
10+
#include <stdlib.h>
11+
#include <string.h>
1012

1113
/* lxfsReadBlock(): reads a block on a mounted lxfs partition
1214
* params: mp - mountpoint
@@ -16,8 +18,38 @@
1618
*/
1719

1820
int lxfsReadBlock(Mountpoint *mp, uint64_t block, void *buffer) {
21+
// check if the block is already in the cache
22+
uint64_t tag = block / CACHE_SIZE;
23+
uint64_t index = block % CACHE_SIZE;
24+
25+
if(mp->cache[index].valid && (mp->cache[index].tag == tag)) {
26+
memcpy(buffer, mp->cache[index].data, mp->blockSizeBytes);
27+
return 0;
28+
}
29+
30+
// else bring it into the cache
31+
if(mp->cache[index].valid && mp->cache[index].dirty) {
32+
// TODO: flush cache
33+
luxLogf(KPRINT_LEVEL_WARNING, "TODO: flush file system cache in read()\n");
34+
return -1;
35+
}
36+
37+
mp->cache[index].valid = 1;
38+
mp->cache[index].dirty = 0;
39+
mp->cache[index].tag = tag;
40+
41+
if(!mp->cache[index].data) mp->cache[index].data = malloc(mp->blockSizeBytes);
42+
if(!mp->cache[index].data) return -1;
43+
1944
lseek(mp->fd, block * mp->blockSizeBytes, SEEK_SET);
20-
return !(read(mp->fd, buffer, mp->blockSizeBytes) == mp->blockSizeBytes);
45+
ssize_t s = read(mp->fd, mp->cache[index].data, mp->blockSizeBytes);
46+
if(s != mp->blockSizeBytes) {
47+
mp->cache[index].valid = 0;
48+
return 1;
49+
}
50+
51+
memcpy(buffer, mp->cache[index].data, mp->blockSizeBytes);
52+
return 0;
2153
}
2254

2355
/* lxfsWriteBlock(): writes a block to a mounted lxfs partition

fs/lxfs/src/include/lxfs/lxfs.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
#include <sys/types.h>
1111
#include <liblux/liblux.h>
1212

13-
#define CACHE_SIZE 128 /* todo: maybe don't hard code this */
13+
/* with a block size of 2 KB, this will give us 8 MB of cache */
14+
#define CACHE_SIZE 4096
1415

1516
typedef struct {
1617
int valid, dirty;

0 commit comments

Comments
 (0)