How does Linux know the Location of File Data on Disk

filesfilesystems

related to: What is a Superblock, Inode, Dentry and a File?

None of the famous metadata structures hold onto to location data for the actual file. Dentry maps names to inodes, and inodes store information about the files — how does the system know where the file's actual data bits are located on the disk? Is there some sort of default mapping of inode integers to disk location?

Best Answer

Given the vast array of file systems out there, I'm certain that exceptions exist, but traditionally, the inode had an array of disk block numbers in it.

For example, in /usr/include/linux/ext3_fs.h, I see a definition of struct ext3_inode.

Inside struct ext3_inode, I see a member i_block[EXT3_N_BLOCKS];/* Pointers to blocks */

Different file systems have had different ways of keeping track of which disk blocks belong to an inode (the on-disk data structure that represents the file's data). Some have an array of block numbers, some have an array of runs or extents, a count plus the beginning block number of a run of contiguous blocks. The Berkeley FFS inode had an array of block numbers, and array of block numbers, each of those blocks contained data block numbers, and a block number that contained block numbers that contained data block numbers.

The whole thing gets a bit weirder for "log structured file systems", but those are the exception rather than the rule.

Related Question