Search code examples
linux

types used to store the block size of linux `struct super_block`


struct super_block {
    struct list_head    s_list;        /* Keep this first */
    dev_t            s_dev;        /* search index; _not_ kdev_t */
    unsigned char        s_blocksize_bits;
    unsigned long        s_blocksize;
    ...

from what I read, s_blocksize and s_blocksize_bits contain the size of the block in bytes and bits respectively. the number of bits are bigger than the number of bytes, so wouldn't it make more sense if their types were the opposite way around? Like this:

    unsigned char        s_blocksize_bits;
    unsigned long        s_blocksize;

Solution

  • s_blocksize_bits is not the size of the block in bits. It's the number of bits in the blocksize. If you search the code to see how it's used, it's often in bit shifting, e.g.

                elen = (etype << 30) |
                        (elen +
                        (count << sb->s_blocksize_bits));
    

    So, given a block number, you shift it by this many bits to get the corresponding offset (they assumed that blocks are always a power of 2 in size).