Saturday, December 24, 2011

How files are stored/retrieved in ext2 filesystem

The space in ext2 is split into blocks. Block is sequence of bits and bytes. These blocks are grouped into block groups.

+------------------------+------------+---------------------+------------+
|Boot Sector|Block Group1|.....................|Block Groupn|
+------------------------+------------+---------------------+------------+

Each block group contains Super block, Filesystem Descriptor, Block bitmap, Inode bitmap, Inode table, Data blocks. Inode contains file permission, ownership, size and its location on the disk.

When a new file is created, the ext2 FS tries to put the file in its parent directory's block group so referencing is easy. If no block is free in the block group, then FS will place the file in new block group.

If you run ls or find command to list/search a particular file, the command inturn reads /proc/mounts file to get the list of mounts and calls getdents64 system call to get the struct linux_dirents which will get the directory/file starting with the parent of the given directory/file.

External reference: http://en.wikipedia.org/wiki/Ext2; http://linux.die.net/man/2/getdents64

No comments: