How file system block size works?

All Linux file systems have 4kb block size. Let's say I have 10mb of hard disk storage. That means I have 2560 blocks available and let's say I copied 2560 files each having 1kb of size. Each 1 kb block will occupy 1 block though it is not filling entire block.

So my entire disk is now filled but still I have 2560x3kb of free space. If I want to store another file of say 1mb will the file system allow me to store? Will it write in the free space left in the individual blocks? Is there any concept addressing this problem?

I would appreciate some clarification. Thanks in advance


Solution 1:

The default block size was 512 bytes since the first Linux ever. Except until recently 4k block sizes where introduced to cater for the ever increasing size of disks. This is actually even nearly dictated by the disk hardware (see more:https://unix.stackexchange.com/questions/178899/optimizing-logical-sector-size-for-physical-sector-size-4096-hdd).

Yet for your specific question: you are right that all files waste the unused bytes of the last block on many types of file systems, which is particularly wasteful for small files. But btfrs and reiserfs seem to be able to cope, acvording to https://en.m.wikipedia.org/wiki/Block_suballocation

Solution 2:

  1. File systems can have block size from 512 to (usually) 65536 bytes. The block size can be specified as mkfs.xxx option. The default blocksize in most Linux filesystems - 4096 bytes.

  2. Some filesystems support tail-packing and/or subblock allocation. ext2/3/4 - does, xfs - doesn't. If you plan to fill a partition with several small files, you can reduce wasted "tail" space by shrinking blocksize to 512 or 1024.