Inode File system

3 minute read

Published:

In this post, we are going to discuss the internals of an inode based file system. There are different variants of inode: i-node, I-node, and, of course, inode. We are going to be using “inode” for this post.

What is an inode?

Inode is a data structure, used by modern file systems, such as ext4, to track the contents of a file across the disk. An inode is associated with every file and directory. Technically, from the point of view of a file system, there is no difference between the inode of a file or a directory. They are basically the same, with a flag in the inode stating whether it is pointing to a file or a directory.

What was used before this, and what is wrong with that?

File system, prior to inode, used a fat-based design to track the files. It uses a simple structure to track the blocks, and is ideal for small file systems. You can read more about this here. It has a pre-defined area to save the meta-data and an array-based linked-list structure, called the fat table, to track the blocks of a file across the disks. A file is divided into fixed sized blocks, prior to be stored on the disk. This fixed sized chunk is called a block. Its size varies from 512B to 4KB, depending on the implementation. However, the fixed nature of the structure, also the linked-list traversal to find a block, made it unsuitable for large file systems that span in TBs.

inode.

The size of a block pointer is typically uint32_t i.e., 8 Bytes or \(2^{32}}. Assuming a 4KB chunk\)2{12}\(, this is enough to hold\)2^{44}$$ or 16 TB.

struct inode_block_pointer{
    uint32_t block_id;
}

The typical structure of an inode is something like this:

inode
Source: Wikipedia

or more succinctly, like this

image
Source: slashroot.in

Typically, an inode will contain 12 direct blocks, 1 single-indirect blocks, 1 double-indirect block, and 1 triple-indirect blocks. Assuming a block size of 4KB and a block entry of size 4B, an inode can track a file of maximum size:

\[(12+2^{10}+2^{10}*2^{10}+2^{10}*2^{10}*2^{10})*4KB \approx 4TB\]

A more detailed explanation regarding size calculation can be found here.

inode file system layout

The layout of an inode based file system is something like this:

image
Source: unsw.edu.au

An inode file system is divided into groups, with each group managing a portion of the chunk. The superblock is stored, redundantly, in all these groups, in case of a corruption of the original one.

The structure of each of group remains the same.

A group

As can be shown in the figure, a group has:

  • Super block: Redundant copy of the super block.
  • Group descriptors: Information of this current group. Size, inode size, block counts etc.
  • Block bitmap: Free table for all the blocks. 0: free 1: occupied
  • Inode bitmap: Free table for all inodes.
  • Inode table: Actual storage of the inodes. A 4KB chunk can contain 4 inodes.
  • Data blocks: Location of data chunks.

Leave a Comment