Sub topic 2
Blocks and inodes: two separate limits
This chapter assumes you can use a Linux shell over SSH, read basic df output, and know what files, directories, and permissions are. You do not need to know filesystem internals. By the end you will understand why a filesystem can report "No space left on device" while df -h shows plenty of free space.
Two resources, not one
A Linux filesystem keeps track of two different things for every file:
- The data: the bytes you wrote. These live in fixed-size units called blocks. On most ext4 and XFS filesystems a block is 4 KiB.
- The metadata: who owns the file, its permissions, its size, its timestamps, and where its data blocks are. This lives in a record called an inode (index node).
A filesystem has a limited supply of each. Running out of either one stops you from creating or growing files. The two limits are independent, so one can be full while the other is nearly empty.
A file's name is not stored in its inode. Names live in directory entries, which map a name to an inode number. This is why one inode can have several names (hard links), and why deleting a name does not always free the file. A later chapter covers that case.
How blocks run out
Blocks run out when the total size of your data fills the filesystem. This is the familiar case: a log file grows without limit, a backup lands on the wrong disk, or a database expands.
Two details matter when you read the numbers.
Files use whole blocks. A 1-byte file still occupies one 4 KiB block. A file of 5,000 bytes occupies two blocks (8 KiB). The space lost in the last block of each file is called slack. With a few large files, slack is negligible. With millions of tiny files, it adds up and du totals can exceed the sum of the file sizes.
Sparse files report more than they use. A sparse file has holes: ranges that read as zeros but have no blocks allocated. ls -l shows the apparent size, while du shows the blocks actually used. Virtual machine images and some databases are often sparse.
You can see the difference yourself:
truncate -s 1G sparse.img
ls -lh sparse.img
du -h sparse.img
du -h --apparent-size sparse.img
rm sparse.imgls -lh and du --apparent-size report 1.0G. Plain du -h reports 0, because no blocks were allocated.
How inodes run out
Every file, directory, and symbolic link consumes one inode. Inode count is not usually dynamic. On ext4, the number of inodes is fixed when the filesystem is created with mkfs.ext4. The default ratio is about one inode per 16 KiB of capacity (the inode_ratio setting in /etc/mke2fs.conf). That is plenty for ordinary data, but not for a directory holding millions of 200-byte files.
When the inode table is full, the kernel cannot create any new file, even an empty one, regardless of free blocks. The error is the same as for a full disk:
touch newfiletouch: cannot touch 'newfile': No space left on deviceThat message is why the first diagnostic step is always to check both resources. If df -h shows free space but you still get this error, check inodes next.
Comparing the two limits
| Row | Blocks | Inodes |
|---|---|---|
| What they store | File contents | File metadata |
| Unit | Fixed-size block (often 4 KiB) | One per file, directory, or link |
| Consumed by | Total data size, rounded up per file | Number of files, regardless of size |
| Typical cause of exhaustion | Large or growing files | Millions of small files |
| Check with | `df -h` | `df -i` |
| Can be changed after creation? | Yes, by growing the filesystem | Depends on filesystem type |
The last row is important. On ext4, you cannot add inodes to an existing filesystem without recreating it. XFS allocates inodes dynamically and limits them by a percentage (imaxpct) of the filesystem, so true inode exhaustion is rare there. Btrfs allocates metadata dynamically and does not use a fixed inode table. The final chapters of this tutorial compare these types in more detail.
Seeing both numbers for one filesystem
You can read both limits for a single mount point with one command:
df -h --output=source,size,used,avail,pcent /var
df -i --output=source,itotal,iused,iavail,ipcent /varTypical output:
Filesystem Size Used Avail Use%
/dev/sda2 50G 21G 27G 44%
Filesystem Inodes IUsed IFree IUse%
/dev/sda2 3276800 3198412 78388 98%This filesystem is only 44% full by space, but 98% full by inodes. Writes will start failing within a few thousand new files, even though 27 GB is free.
You can also inspect a single file's inode number and block use with stat:
stat /etc/hostnameThe output includes Size, Blocks, IO Block, and Inode. Note that stat reports Blocks in 512-byte units, not in the filesystem's block size. This often confuses people comparing it with du.
Common mistakes
- Checking only
df -h. It tells you about blocks only. Always rundf -iwhen you see a "No space left" error. - Assuming small files are free. Each one costs an inode and at least one block.
- Trusting
ls -lsizes for space. Apparent size and allocated size differ for sparse files and for small files. - Mixing up units.
df -huses powers of 1024 (KiB, MiB, GiB).df -Huses powers of 1000. The two outputs differ by several percent on large disks. - Reading
Use%for a different mount. Directories like/var/logmay be on their own filesystem. Usedf /pathto see which filesystem actually holds a path.
Summary of the model
Every file needs both an inode and at least one block (empty files need only an inode). Space problems come from the size of your data. Inode problems come from the count of your files. A complete disk check always reports both.
Exercise
A server returns "No space left on device" when an application tries to create a session file. df -h /var shows 38G free of 100G (62% used). What is the next command you run, and what result would confirm that inodes are the cause?
Show answer
Run df -i /var. If the IUse% column is at or near 100% (or IFree is close to 0), the filesystem has run out of inodes. Free blocks do not help in that case, because creating any new file needs a free inode.
Exercise
You create a 1 GiB sparse file with truncate -s 1G test.img. ls -lh test.img shows 1.0G, but du -h test.img shows 0. Which of the two numbers tells you how many blocks the file uses, and why?
Show answer
du -h does. It reports allocated blocks, and a sparse file has no blocks allocated for its holes. ls -l reports the apparent size (the logical length of the file), which includes the unallocated ranges.