Inode Content
So far, we have been talking about how to allocate an inode, how to release an inode, how to get an inode, and locks around inode. In this section, we will look closely at the inode data.

Find data block address in inode
Given the inode, and the block number. Begin from Direct nodes, allocate (use balloc) if not existing. If block number is beyond direct nodes, search in indirect nodes, allocate if not existing. Return the address of the data block. (Aka the block index)
Read and write data for a file
Read
Starts by making sure that the offset and count are not beyond the end of the file. The main loop processes each block of the file, copying data from the buffer into dst.
Cool Stuff
off/BSIZE gets the data block’s index. bmap gets the actual address of that block. bread gets the buffer cache. The real data offset is bp->data + (off % BSIZE).
As you can see, X/Y for which block contains the data, X % Y for which position the data sits after finding the block.
Write
Write is similar to read, except writing can grow the file size, so we might need to update inode's size.
Get stats of a file
stati is just copying inode metadata to user program via the stat system call.
Last updated
Was this helpful?