Reading Files
To read files, programs need to open them first. An open(<path-to-file>)
system call is issued, and the file system starts finding the required file. For this, we require the file's inode
and inode number
. The file system thus traverses through different blocks to find the desired inode for the file.
Consider an example where you wanted to read a file example.txt
. The file system starts traversing from the root directory (all traversals start from here), whose inode number is already known by the file system. In most file systems, this inode number is 2. The file system accesses the corresponding inode and looks inside it to find references to data blocks for its contents. It keeps traversing through these references until the desired file is found.
Once the file is opened, the program would issue a read()
system call. The inode is used to find the corresponding block for the file contents, and the data is read from the first block of the file (a single file can occupy multiple blocks!). Reading files may update the inode with some of its metadata, such as access times.