Skip to content

Commit

Permalink
lxfs: read/write blocks and block chains
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Sep 29, 2024
1 parent 74d0863 commit 03c3361
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions fs/lxfs/src/blockio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* luxOS - a unix-like operating system
* Omar Elghoul, 2024
*
* lxfs: Driver for the lxfs file system
*/

#include <lxfs/lxfs.h>
#include <unistd.h>

/* lxfsReadBlock(): reads a block on a mounted lxfs partition
* params: mp - mountpoint
* params: block - block number
* params: buffer - buffer to read into
* returns: zero on success
*/

int lxfsReadBlock(Mountpoint *mp, uint64_t block, void *buffer) {
lseek(mp->fd, block * mp->blockSizeBytes, SEEK_SET);
return !(read(mp->fd, buffer, mp->blockSizeBytes) == mp->blockSizeBytes);
}

/* lxfsWriteBlock(): writes a block to a mounted lxfs partition
* params: mp - mountpoint
* params: block - block number
* params: buffer - buffer to write from
* returns: zero on success
*/

int lxfsWriteBlock(Mountpoint *mp, uint64_t block, const void *buffer) {
lseek(mp->fd, block * mp->blockSizeBytes, SEEK_SET);
return !(write(mp->fd, buffer, mp->blockSizeBytes) == mp->blockSizeBytes);
}

/* lxfsNextBlock(): returns the next block in a chain of blocks
* params: mp - mountpoint
* params: block - current block number
* returns: next block number, zero on fail
*/

uint64_t lxfsNextBlock(Mountpoint *mp, uint64_t block) {
// read the relevant part of the block table
uint64_t tableBlock = block / (mp->blockSizeBytes / 8);
tableBlock += 33; // the first 33 blocks are reserved
uint64_t tableIndex = block % (mp->blockSizeBytes % 8);

if(lxfsReadBlock(mp, tableBlock, mp->blockTableBuffer)) return 0;

uint64_t *data = (uint64_t *) mp->blockTableBuffer;
return data[tableIndex];
}

/* lxfsReadNextBlock(): reads a block and returns the next block in its chain
* params: mp - mountpoint
* params: block - block number
* params: buffer - buffer to read into
* returns: next block number, zero on fail
*/

uint64_t lxfsReadNextBlock(Mountpoint *mp, uint64_t block, void *buffer) {
if(lxfsReadBlock(mp, block, buffer)) return 0;
return lxfsNextBlock(mp, block);
}

/* lxfsWriteNextBlock(): writes a block and returns the next block in its chain
* params: mp - mountpoint
* params: block - block number
* params: buffer - buffer to write from
* returns: next block number, zero on fail
*/

uint64_t lxfsWriteNextBlock(Mountpoint *mp, uint64_t block, const void *buffer) {
if(lxfsWriteBlock(mp, block, buffer)) return 0;
return lxfsNextBlock(mp, block);
}

0 comments on commit 03c3361

Please sign in to comment.