Skip to content

[Lower Layer] Block

Hu, Hsien-Wen edited this page Dec 15, 2017 · 19 revisions

Why we need block?

Dividing a file to several parts.

What's the size of a block?

In EXT2 file system, block size can be 1KB, 2KB, or 4KB. We may choose 1KB(1024B) to be our block size.

APIs

Create new file system

This function can create blocks. It means that this function can create a space for our file system. This function should be only executed once.

Function:

int create_filesystem(const char *path, const int size)

Input:

  • const char *path: where user will like to place the whole file system.
  • const int size: The size of the whole file system.

Return value:

  • return value > 0, it means that create_block works fine. Return value is also equal to how many blocks you created.
  • return value = -1, it means that create_block fails to request a space on disk.
  • return value = -2, it means that create_block get a bad file system.

Load filesystem

This function can load blocks from existing file system. Either load block or create block should be called only once.

Function:

int load_filesystem(const char *path)

Input:

  • const char *path: the path of existing file system.

Return value:

  • return value = 0: it means that load_block works fine.
  • return value = -1, file system does not exist.
  • return value = -2, file system fails to load because of some reason.
  • return value = -3, file system fails to read.
  • return value = -4, bad header, invalid file.
  • to be continued.......

Write block:

This function can let other functions write something to a specific block. Function will find a proper block to put data. The main purpose of this function is assign a block for user and write data to block at same time.

Function:

int write_block(const int *block_ID, void *block, int block_input_length)

Input:

  • const int *block_ID: Block's ID. This number is a special number only for a block. Because this parameter is a pointer type, it can bring block ID to the function which called write_block.
  • void *block: the message which is going to write to block.
  • int block_input_length: the length of block

Return value:

  • return value >= 0: write successfully, return value is also same as how many bytes does function write.
  • return value = -1: no file system exit.
  • return value = -2: block_ID is larger that the number of blocks.
  • return value = -3: fail move to the blockID.
  • return value = -4: fails to write
  • to be continue......

Modify super block:

This function can let other functions modify super block. The main purpose of this function is modify data in a block.

Function:

int modify_super_block(void *block, int block_input_length)

Input:

  • void *block: the message which is going to write to block.
  • int block_input_length: the length of block

Return value:

  • return value >= 0: write successfully, return value is also same as how many bytes does function write.
  • return value = -1: no file system exit.
  • return value = -3: fail move to super block.
  • return value = -4: fails to write
  • to be continue......

Read super block:

This function can let other functions read super block.

Function:

int read_block(void *block, int block_output_length)

Input:

  • void *block: To get the message read from block.
  • int block_output_length: How many bytes does user wants to read from super block

Return value:

  • return value >= 0: works fine, return value is also same as how many bytes does function read.
  • return value = -1: Filesystem does not exist.
  • return value = -3: fail to move to super block.
  • return value = -4: fail to read.
  • to be continue......

Modify block:

This function can let other functions modify something to a specific block.

Function:

int modify_block(const int *block_ID, void *block, int block_input_length)

Input:

  • const int *block_ID: Block's ID. This number is a special number only for a block.
  • void *block: the message which is going to write to block.
  • int block_input_length: the length of block

Return value:

  • return value >= 0: write successfully, return value is also same as how many bytes does function write.
  • return value = -1: no file system exit.
  • return value = -2: block_ID is larger that the number of blocks.
  • return value = -3: fail move to the blockID.
  • return value = -4: fails to write
  • to be continue......

Read block:

This function can let other functions read the specific block.

Function:

int read_block(const int block_ID, void *block)

Input:

  • const int block_ID: Block's ID. This number is a special number only for a block.
  • void *block: To get the message read from block.

Return value:

  • return value >= 0: works fine, return value is also same as how many bytes does function read.
  • return value = -1: Filesystem doest not exist.
  • return value = -2: block_ID is larger that the number of blocks.
  • return value = -3: fail to move to blockID.
  • return value = -4: fail to read.
  • to be continue......

Delete block:

This function can let other functions delete the whole message which is stored in a specific block.

Function:

int delete_block(const int *block_ID)

Input:

  • const int *block_ID: Block's ID. This number is a special number only for a block.

Return value:

  • return value = 0: delete successfully.
  • to be continue......

Working flow

Super block

Super block will be located at block[0]. Because of block map and identify header, there are 502 empty bytes.

Inode

The upper layer of block is inode, The blocks which are number from 1 to 512 are reservered for inodes(512 * 1024 = 524288 bytes, 524288 / 128(size of one inode) = 4096).