BarryServer : Git

All the code for all my projects
// BarryServer : Git / Nucleus / blob / master / vfs / ext2fs / block.c

// Related

Nucleus

Barry Directory listing in VFS 2d05318 (3 years, 2 months ago)
#include <stdint.h>
#include <nucleus/kernel.h>
#include <nucleus/vfs.h>
#include "ext2fs.h"

/* Read a block of data */
void
ext2fs_read_block(SuperBlock *super, uint32_t index, char *buf)
{
	struct Ext2Super *rsuper = super->data;
	super->back->pos = index * (1024 << rsuper->blockSize);
	file_read(super->back, buf, 1024 << rsuper->blockSize);
}

/* Get the data block address from inode block index */
uint32_t
ext2fs_get_data_addr(SuperBlock *super, struct Ext2Inode *inode, uint32_t index)
{
	uint32_t tmp;
	char block[PAGE_SIZE];
	struct Ext2Super *rsuper = super->data;
	/* Blocks per indirect block */
	uint32_t bpib = (1024 << rsuper->blockSize) / sizeof(uint32_t);

	/* Direct blocks */
	if (index < 12)
		return inode->directBlock[index];
	index -= 12;

	/* Single indirect block */
	if (index < bpib) {
		ext2fs_read_block(super, inode->singleBlock, block);
		return *((uint32_t *) block + index);
	}
	index -= bpib;

	/* Double indirect block */
	if (index < bpib*bpib) {
		panic("Attempted to read from double indirect block");
	}
	index -= bpib*bpib;

	/* Triple indirect block */
	if (index < bpib*bpib*bpib) {
		panic("Attempted to read from triple indirect block");
	}

	return 0;
}