Linux – File system block size differs between different ext4 partitions

e2fsprogsgptlinuxmkfspartition

we have BBB based custom board with 256MB RAM and 4GB eMMC,

I have partitioned it using below code,

parted --script -a optimal /dev/mmcblk0 \
  mklabel gpt \
  mkpart primary 128KiB 255KiB \
  mkpart primary 256KiB 383KiB \
  mkpart primary 384KiB 511KiB \
  mkpart primary 1MiB 2MiB \
  mkpart primary 2MiB 3MiB \ 
  mkpart primary 3MiB 4MiB \
  mkpart primary 4MiB 5MiB \ 
  mkpart primary 5MiB 10MiB \
  mkpart primary 10MiB 15MiB \
  mkpart primary 15MiB 20MiB \
  mkpart primary 20MiB 21MiB \
  mkpart primary 21MiB 22MiB \
  mkpart primary 22MiB 23MiB \
  mkpart primary 23MiB 28MiB \
  mkpart primary ext4 28MiB 528MiB \
  mkpart primary ext4 528MiB 1028MiB \
  mkpart primary ext4 1028MiB 1128MiB \
  mkpart primary ext4 1128MiB 1188MiB \
  mkpart primary ext4 1188MiB 2212MiB \
  mkpart primary ext4 2212MiB 2603MiB \
  mkpart primary ext4 2603MiB 2639MiB \
  mkpart primary ext4 2639MiB 100% \

And then formatted file system partitions using below command

mkfs.ext4 -j -L $LABEL $PARTITION

Now when I read file system block size using tune2fs, I see different value for partitions less than 1GiB and partition greater or equal to 1GiB partition.

# tune2fs -l /dev/mmcblk0p15  | grep Block
Block count:              512000
Block size:               1024
Blocks per group:         8192
# 
# 
# tune2fs -l /dev/mmcblk0p16  | grep Block
Block count:              512000
Block size:               1024
Blocks per group:         8192
# 
# 
# tune2fs -l /dev/mmcblk0p19  | grep Block
Block count:              262144
Block size:               4096
Blocks per group:         32768
# tune2fs -l /dev/mmcblk0p22  | grep Block
Block count:              1191936
Block size:               4096
Blocks per group:         32768

I am not able to understand why block sizes are different.
moreover mke2fs.conf is having all default values only and blocksize mentioned there is 4096.

[defaults]
    base_features = sparse_super,filetype,resize_inode,dir_index,ext_attr
    default_mntopts = acl,user_xattr
    enable_periodic_fsck = 0
    blocksize = 4096
    inode_size = 256
    inode_ratio = 16384

[fs_types]
    ext3 = {
        features = has_journal
    }
    ext4 = {
        features = has_journal,extent,huge_file,flex_bg,uninit_bg,dir_nlink,extra_isize
        auto_64-bit_support = 1
        inode_size = 256
    }
    ext4dev = {
        features = has_journal,extent,huge_file,flex_bg,uninit_bg,dir_nlink,extra_isize
        inode_size = 256
        options = test_fs=1
    }
    small = {
        blocksize = 1024
        inode_size = 128
        inode_ratio = 4096
    }
    floppy = {
        blocksize = 1024
        inode_size = 128
        inode_ratio = 8192
    }
    big = {
        inode_ratio = 32768
    }
    huge = {
        inode_ratio = 65536
    }
    news = {
        inode_ratio = 4096
    }
    largefile = {
        inode_ratio = 1048576
        blocksize = -1
    }
    largefile4 = {
        inode_ratio = 4194304
        blocksize = -1
    }
    hurd = {
         blocksize = 4096
         inode_size = 128
    }

Can someone explain/suggest a doc/hint why block sizes are different for different partitions?

Best Answer

As @derobert mentioned in the comment.

mkfs.ext4/mke2fs refers to /etc/mke2fs.conf and formats the partition.

mke2fs chooses block size based on the partition size if not explicitly mentioned. Read -b block-size and -T usage-type in mke2fs man page for the same.

So when partition size is less than 512MB mkfs.ext4 formats it as small with following settings from mke2fs.conf file.

 small = {
        blocksize = 1024
        inode_size = 128
        inode_ratio = 4096
    }

However when partition size is more than 512MB mkfs.ext4 or mke2fs formats partition using defaults from mke2fs.conf file

[defaults]
    base_features = sparse_super,filetype,resize_inode,dir_index,ext_attr
    default_mntopts = acl,user_xattr
    enable_periodic_fsck = 0
    blocksize = 4096
    inode_size = 256
    inode_ratio = 16384

That's what was causing different block sizes in the different partitions for me.

One more note. To get total number of inode you will get after formatting can be calculated as follows,

Total number of inodes = partition size / inode_ration
e.g. 
for 500MB partition
total number of inodes = (500 * 1024 * 1024) / 4096
                       = 128000

NOTE: I think I am missing something here, because for the calculations I have shown above, actual value shown by tune2fs is Inode count: 128016 which nearly matches but not exact.

Related Question