BarryServer : Git

All the code for all my projects
// BarryServer : Git / Orion / blob / 1628fcfdfdf2978ed9ccac96ee7d13bb3dc43a01 / vfs / ext2fs / block.c

// Related

Orion

Barry Adding pipes e59e4fe (2 years, 4 months ago)
/*
 * This file contains the Ext2 block implementation.  It reads a backing file
 * from the super block in the intervals specified by the superblock.  It also
 * contains functions for reading blocks from files.
 */

#include <stdint.h>
#include "fs.h"

/* Read a block of data */
void
ext2_read_block(SuperBlock *super, uint32_t index, char *buf)
{
	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
ext2_get_data_addr(SuperBlock *super, Ext2Inode *node, uint32_t index)
{
	uint32_t tmp;
	char block[4096];
	Ext2Super *rsuper = super->data;
	/* Blocks per indirect block */
	uint32_t bpib = (1024 << rsuper->blockSize) / sizeof(uint32_t);

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

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

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

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

	return 0;
}