|
7 | 7 |
|
8 | 8 | #include <lxfs/lxfs.h>
|
9 | 9 | #include <unistd.h>
|
| 10 | +#include <stdlib.h> |
| 11 | +#include <string.h> |
10 | 12 |
|
11 | 13 | /* lxfsReadBlock(): reads a block on a mounted lxfs partition
|
12 | 14 | * params: mp - mountpoint
|
|
16 | 18 | */
|
17 | 19 |
|
18 | 20 | 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 | + |
19 | 44 | 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; |
21 | 53 | }
|
22 | 54 |
|
23 | 55 | /* lxfsWriteBlock(): writes a block to a mounted lxfs partition
|
|
0 commit comments