Trying to read the root inode

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
BlueSillyDragon
Posts: 5
Joined: Sat Jan 06, 2024 8:01 pm
Libera.chat IRC: BlueSilly
GitHub: https://github.com/BlueSillyDragon

Trying to read the root inode

Post by BlueSillyDragon »

Heya, me again! (lord I haven't been here in a while lol)

So, question, how *exactly* does the i_block field of an inode work? Can I use it directly to read the block (eg. read(offset: i_block[0])) or do I need to do some sort of calculation first? (for context, this is in a UEFI Bootloader, although I'm not sure that matters) I know that the ext2 docs say:

" Since this value represents 512-byte blocks and not file system blocks, this value should not be directly used as an index to the i_block array. Rather, the maximum index of the i_block array should be computed from i_blocks / ((1024<<s_log_block_size)/512), or once simplified, i_blocks/(2<<s_log_block_size). "

under the i_blocks section, but I'm kind of having trouble understanding what it means by this.

Any help would be greatly appreciated! :D
Developer of AquaOS
nullplan
Member
Member
Posts: 1867
Joined: Wed Aug 30, 2017 8:24 am

Re: Trying to read the root inode

Post by nullplan »

Basically, i_blocks*512 is an upper bound of the file size. But since it counts 512-byte blocks and not file-system blocks (which tend to be 4k), it does not give the highest logical block index in the file. But the formula given, "i_blocks /(2<<s_log_block_size)", that gives the number of logical blocks, so one more than the highest index.

The formula isn't even optimized all that much. If the block size is 1k, then the number of logical blocks is i_blocks >> 1. If the block size is 4k, then it is i_blocks >> 3. You figure out the formula in terms of s_log_block_size yourself.
Carpe diem!
User avatar
iansjack
Member
Member
Posts: 4759
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Trying to read the root inode

Post by iansjack »

It’s all a question of what size blocks your disk driver uses and what size blocks the filesystem uses. They aren’t necessarily the same. s_log_blocksize tells you how big a filesystem block is. Only you know how big your disk read blocks are (1 sector, 512 bytes, 8 sectors, 4K, whatever - your choice). You have to convert between the two when determining which disk block to read for a given filesystem block. And the filesystem block size may vary for individual mounted filesystems.
User avatar
BlueSillyDragon
Posts: 5
Joined: Sat Jan 06, 2024 8:01 pm
Libera.chat IRC: BlueSilly
GitHub: https://github.com/BlueSillyDragon

Re: Trying to read the root inode

Post by BlueSillyDragon »

iansjack wrote: Sun Apr 06, 2025 12:18 am It’s all a question of what size blocks your disk driver uses and what size blocks the filesystem uses. They aren’t necessarily the same. s_log_blocksize tells you how big a filesystem block is. Only you know how big your disk read blocks are (1 sector, 512 bytes, 8 sectors, 4K, whatever - your choice). You have to convert between the two when determining which disk block to read for a given filesystem block. And the filesystem block size may vary for individual mounted filesystems.
Okay, quick question before I continue. What does my disk read's block size have to do with this? Again, I'm working with UEFI, and I'm mainly just using the DiskIO protocol and not the BlockIO protocol.

But anyhow, I guess to get back to my main point, according the the OSDev Wiki and the Ext2 Docs, i_block (which is not the same as i_blocks, very important distinction lol) is a 15 element array of uint32, which point to the blocks the inode uses, elements 0-11 are direct blocks, and 12-14 are indirect blocks, so I'm wondering if I can simply use the value in i_block to read the block starting from it's address. To give an example with my read_part function (which just reads from a partition using the DiskIO protocol)

read_part(part_to_read_from: ext2_parts[0], offset: (inode->i_block[1]), bytes_to_read: 512, buffer);
Developer of AquaOS
User avatar
iansjack
Member
Member
Posts: 4759
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Trying to read the root inode

Post by iansjack »

It looks like you are reading your disk in 512 byte chunks (which is pretty inefficient, but that’s another subject). So, if your filesystem block size is 4K (just as an example) that means that each block corresponds to 8 of your reads (sectors). You need to make the appropriate conversion in your calculations.
User avatar
BlueSillyDragon
Posts: 5
Joined: Sat Jan 06, 2024 8:01 pm
Libera.chat IRC: BlueSilly
GitHub: https://github.com/BlueSillyDragon

Re: Trying to read the root inode

Post by BlueSillyDragon »

iansjack wrote: Sun Apr 06, 2025 11:40 am It looks like you are reading your disk in 512 byte chunks (which is pretty inefficient, but that’s another subject). So, if your filesystem block size is 4K (just as an example) that means that each block corresponds to 8 of your reads (sectors). You need to make the appropriate conversion in your calculations.
...oh...OHHHHHHH!

Okay yeah I completely misunderstood what the Ext2 docs meant. Thanks alot!
Developer of AquaOS
Post Reply