- Moges
- 2026-10-29
The Linux Disk Is Not Full, Why Can't a Single Byte Be Written (Troubleshooting inode Exhaustion)
The call that day was typical: the internal management system would not let anyone log in. The page loaded, the login box appeared normally, you typed the account and password, it spun for a moment and returned to the login page. Different browsers, cleared cache, different accounts, all tried; the user's description was precise: it never logs in.
The cause turned out to have nothing to do with logging in, with the system or with the database: a mail service nobody was minding on this Linux server had used up the file system's inodes. At the time df -h showed more than half the disk free.
That is what makes this class of fault so deceptive on Linux. The error says No space left on device, you look at disk space and there is plenty; you look for large files and find none; so you start suspecting the application, the database, the password. The command you should have run first is often remembered only after a long detour.
This article takes the process and the mechanism apart, including three experiments you can reproduce yourself, and a decision sequence to follow the next time something cannot be written.
![]()
1. The Fault Started with Cannot Log In
The page loads, so the web service is alive; the login box displays, so the application still responds. Those two facts rule out the service being down. Then you type the account and password, click login, it spins and returns to the login page. That symptom points to the session: after a successful login a session file must be written, and every later request uses it to identify the user. If the session cannot be written, login cannot complete logically: the password check passes, the session is never created, the next request still considers you logged out, and back you go to the login page.
Following that thread: restart the application service, no use; look at the application log, no errors in it; check the database connection, normal; run the login-related queries directly, they return results; the account status and password are both fine.
In hindsight one detail was overlooked: the log had no errors because the errors could not be written. A system that cannot write its log looks remarkably healthy.
2. The First Misjudgment: Assuming the Disk Was Full
The most common reason for cannot write is a full disk, so check space first:
df -h
The result was unexpected: the root partition still had tens of gigabytes free and was under sixty percent used. Since there was space, the disk was not the problem, or so we judged at the time, and that judgment cost nearly an extra hour.
Next came finding who had eaten the space: large files, log directories, upload directories, database files. du scanned down level by level, the largest directory was only a few gigabytes, and the total did not add up to anything abnormal. Meanwhile the system kept reporting that it could not write.
What brought us back was a very plain question: if there really is space, why can a new file not be created?
Following that question we tried another command:
df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/vda1 6553600 6553600 0 100% /
The inodes were used up, 100 percent. At the same moment df -h still showed plenty of space. Two commands, two worlds.
3. What an inode Is and Why It Can Run Out
In the ext4 file system on Linux, one region of the disk records the file list, each file taking one entry, and the number of that entry is the inode (index node). A file actually occupies two things:
Data blocks: where the content itself lives, which is what df -h counts; the inode: the file name, permissions, owner, size, times and the location of its data blocks, which is what df -i counts.
So whether there is enough space and whether a new file can still be created are two independent questions. Enough space with inodes exhausted means existing files can still be read, not one new byte can be written, and the error is still the most misleading sentence of all: No space left on device. That sentence contains neither disk nor inode; it only says the device has no space.
The difference between the two file systems is worth remembering too: ext4 fixes the total number of inodes at format time, roughly one per 16KB of space by default, about six million on a 100G disk, and that number cannot be increased online; xfs allocates inodes dynamically and generally does not run out because of file count, though its space reclamation after deletion behaves differently from ext4 (delayed allocation and preallocation).
The key conclusion: inode consumption depends only on the number of files, not their size. A zero-byte empty file and a 4G video take exactly the same inode. That explains why du found nothing abnormal: the files eating the inodes may add up to a few hundred megabytes, trivial against tens of gigabytes free.
4. Where Six Hundred Thousand Small Files Came From
Count files per directory (a read-only operation, which still runs when inodes are full):
du --inodes -d 1 /var | sort -rn | head
The result was blunt: /var/spool/postfix alone held over six hundred thousand inodes, and every other directory combined was a fraction of it.
postfix is a mail service. The machine ran an internal management system, and the mail service had been left over from the default full installation when the system was set up: the panel brought it, the OS brought it, nobody turned it off. It was quiet in normal times, using no CPU and no memory, invisible in df -h, so nobody had ever noticed it.
How it ate the inodes takes three points:
First, a mail queue is a place with one file per message. If delivery fails the message stays in deferred waiting for a retry. postfix hashes it into several levels of subdirectories, precisely so that no single directory holds hundreds of thousands of files; the design itself shows that queue buildup was anticipated and needs guarding against.
Second, undeliverable mail keeps retrying. The default policy retries after an interval, with longer intervals later, so a message can sit in the queue for days.
Third, and the most lethal: bounces generate bounces. The system has scheduled jobs, and the output of some jobs is mailed to local accounts; if the account does not exist or local delivery fails, the mail system generates a bounce notifying the original sender; if that bounce also cannot be delivered, for example because the recipient is root@hostname and the hostname does not resolve, or outbound mail is blocked by network policy, it enters the queue, retries, and generates new bounces. One becomes two, two become four.
This is a classic avalanche, and a silent one: no alert, no log anomaly, CPU and memory normal, only the inodes falling steadily. When they reach zero, every operation that needs to create a file stops at once: no session file, so login never completes; no log, so even the reason cannot be recorded; no captcha image; uploads, exports and temporary files all fail.
What the user sees is still only cannot log in. The first thing to break in a system is often the least conspicuous write.
Knowing the queue directories makes the next diagnosis immediate:
| Directory | Meaning | Safe to delete |
|---|---|---|
| maildrop / incoming | Just received, not yet written | Do not delete |
| active | Being delivered | Do not delete |
| deferred | Delivery failed, awaiting retry | Yes (oldest first) |
| bounce | Bounce being processed | Careful; leave ones in progress |
| corrupt / hold | Corrupt or manually held | Yes after confirmation |
For day-to-day queue checks, three commands are enough:
mailq # list the queue
postqueue -p | tail -1 # summary only: -- 632847 Kbytes in 612304 Requests
postsuper -d ALL deferred # empty the deferred queue (careful, check the count first)
The summary line that day showed over six hundred thousand messages. That number did not grow in a day: a few dozen a day at first, undeliverable ones accumulating, bounces generating new bounces, until thousands were added daily. It had been growing for months, and in all that time no monitoring item could see it.
5. Restore Writes First, Then Talk About the Root Cause
Inode exhaustion has only one cure: delete. But deleting in a queue directory carries risk: a direct rm -rf takes away the directory structure, ownership and permissions postfix maintains itself, and the service will probably not come up cleanly; deleting files being delivered corrupts the queue state. The order we followed was this:
Step one, preserve evidence. Save df -i, the queue size and the directory listing before touching anything. Delete first and there is no data left for a review.
df -i > /root/inode-before.txt
mailq | tail -3 > /root/mailq-before.txt
du --inodes -d 1 /var/spool/postfix | sort -rn > /root/postfix-inodes.txt
Step two, clean in scripted batches, not all at once. Not postsuper -d ALL, because that would also delete the mail in active and incoming that could still be delivered normally, and deleting hundreds of thousands of files in one go would drive the already unhealthy machine to a standstill on IO. The approach was to delete the least valuable first: the oldest in deferred (bounces that had sat for days and long since lost meaning) and corrupt.
#!/bin/bash
Q=/var/spool/postfix/deferred
BATCH=2000
DRY=1 # 1 = count only, do not delete; set to 0 once confirmed
echo "before:" && df -i / | tail -1
for d in "$Q"/*/; do
cnt=$(find "$d" -type f -mtime +2 | wc -l)
[ "$cnt" -eq 0 ] && continue
echo "$d has $cnt to clean"
[ "$DRY" -eq 1 ] && continue
find "$d" -type f -mtime +2 -print0 | xargs -0 -n "$BATCH" rm -f
done
echo "after:" && df -i / | tail -1
A few details: -mtime +2 deletes only files older than two days and leaves newly arrived ones still retrying; -print0 with xargs -0 handles special characters in file names; -n 2000 works in batches so a single delete does not saturate IO; DRY=1 runs through the list first, and only after the directories and counts look right is it set to 0 to actually delete. Watch df -i during the run: if the usage rate starts falling, it is working.
Step three, verify writes immediately. No need to wait for user feedback; create a file yourself: touch /tmp/inode-check. If it can be created, the write path is open, and only then look at login.
Step four, stop the mail service; that is the real cure. Clearing the queue only lowers the water level while the service keeps running and bounces keep breeding. On a new system use systemctl disable --now postfix; on an older system without systemd use chkconfig postfix off plus service postfix stop. Ten or so minutes later check df -i again; there should be no further change in magnitude.
Why this counts as the cure: the source of the problem was not a full queue but that the machine never needed a mail service at all. It carries no business function, is only a default product of installation, and its sole purpose is to send scheduled jobs' mail that nobody reads, which piles up when it cannot go out. Stopping it shuts down a line nobody uses that keeps producing garbage.
The reverse case needs stating too: if the business really does need to send mail, such as alert notifications or password recovery, it cannot simply be stopped; three things are needed instead, a relay that can actually deliver, a queue cap and cleanup policy, and queue length in monitoring. Mail that can go out does not pile up, and every queue buildup is essentially still sending what cannot be sent.
6. Three Experiments That Reproduce It
Reproduce the key mechanism on its own; all three were done in a container without touching production.
Experiment one: half the space left, inodes exhausted, writes fail. Using a 50M temporary file system with only 800 inodes (tmpfs nr_inodes caps the inode count exactly):
mkdir -p /mnt/a
mount -t tmpfs -o size=50m,nr_inodes=800 tmpfs /mnt/a
for i in $(seq 1 900); do touch /mnt/a/f_$i 2>/dev/null || break; done
df -h /mnt/a ; df -i /mnt/a ; touch /mnt/a/zzz
files created: 799
Filesystem Size Used Avail Use% Mounted on
tmpfs 50.0M 0 50.0M 0% /mnt/a
Filesystem Inodes Used Avail Use% Mounted on
tmpfs 800 800 0 100% /mnt/a
touch: /mnt/a/zzz: No space left on device
Space usage 0 percent, inode usage 100 percent, error No space left on device. Three lines together are the whole point of this article. All 799 files are zero bytes, yet they used 799 of the 800 inodes.
Experiment two: the file is deleted but the space does not come back. This and inodes are two different kinds of deleted but no use, easily confused in troubleshooting:
mkdir -p /mnt/b
mount -t tmpfs -o size=10m tmpfs /mnt/b
dd if=/dev/zero of=/mnt/b/big.bin bs=1M count=5 2>/dev/null
sh -c 'exec 3</mnt/b/big.bin; sleep 30' &
sleep 1 ; rm -f /mnt/b/big.bin
df -h /mnt/b ; ls -l /proc/$!/fd | grep big
--- after rm (space did not come back) ---
tmpfs 10.0M 5.0M 5.0M 50% /mnt/b
--- who still holds the deleted file ---
lr-x------ 1 root root 64 Oct 29 00:55 3 -> /mnt/b/big.bin (deleted)
The file is held open by a process; rm removes it from the directory but the space is not released until the process exits or closes the handle. This is the most common reason df says full while du finds no large file (lsof +L1 or /proc/<pid>/fd shows (deleted)). With inode exhaustion, by contrast, deleting a file takes effect at once, one out for one back, so the remedies differ completely: the first needs the process restarted, the second only allows deletion.
Experiment three: batch deletion restores writes immediately. After deleting half, usage fell from 100 percent to 50 percent and the exit code of touch turned 0 at once.
--- before cleanup ---
tmpfs 800 800 0 100% /mnt/a
deleted 400 files this round
--- after cleanup ---
tmpfs 800 400 400 50% /mnt/a
--- write one more file ---
touch exit code: 0
In production the effect is the same: not wait for it to fix itself, but delete and it takes effect.
7. How to Find It Early Instead of Waiting for a Call
The reason this fault dragged on until the user called was that monitoring covered CPU, memory and disk space, but not inodes. Three additions:
One, put df -i into monitoring, alongside df -h, with an alert at 80 percent. Inode exhaustion usually climbs slowly and then hits bottom suddenly, and 80 percent is the window to act.
Two, put queue length into monitoring. On machines with a mail service, watch the count from mailq | tail -1, or count the files under deferred directly. A steadily rising number is the signal of still sending what cannot be sent, and it surfaces the problem earlier than inodes.
Three, build per-directory inode baselines. Record once how many files each major directory holds in normal times, and compare directly when something goes wrong. /var/spool, /tmp, session directories, cache directories and upload temporary directories are the usual suspects.
One more easily overlooked point: an inode alert cannot be replaced by whether space is sufficient. One disk may be 90 percent used with few files and only 5 percent of its inodes gone; another may be 30 percent used with 95 percent of its inodes gone. In df -h one looks more dangerous and the other safer, when the truth is the opposite.
8. Several Wrong Moves, and Why Not to Make Them
rm -rf /var/spool/postfix. The queue directory structure, ownership and permissions are maintained by postfix itself. Delete the whole tree and the service will most likely fail to start after a restart, or start with a corrupted queue state. Clear the files inside and keep the tree.
Deleting active and incoming directly. These are the queues being delivered and just received; deleting here throws away mail that could have been delivered. Delete deferred and corrupt first.
One rm for six hundred thousand files. The argument list is too long and it reports Argument list too long; even with find -delete, the deletion itself updates directories and journals, IO saturates, and a machine already fragile from inode exhaustion easily stalls.
Only scaling up. Adding disks and extending partitions solves space, not inodes. ext4 adds inodes at a fixed ratio on resize, millions are missing, doubling the space only doubles them and is usually not enough; meanwhile the business keeps producing garbage during the resize, like adding water to a leaking bucket.
Tuning inode parameters with tune2fs. ext4's inode total is fixed at format time and cannot be increased online; what tune2fs can adjust does not solve a shortage of numbers. A large increase means rebuilding the file system, which is a migration with downtime, not an emergency measure.
Leaving the service running and only adding a scheduled cleanup script. This was the choice we nearly made at first: since one cleanup fixes it, clean once a day. That amounts to admitting a line nobody uses keeps producing garbage and dealing with it by robot vacuum. A cleanup script should exist as a fallback, but the cure is stopping the service.
9. This and Minimal Installation Are the Same Thing
In the article on application environment setup we wrote the principle for installing a system: minimal installation, do not install services you do not need; after installation go back and subtract, turning off what came by default and is not used. This fault is a counterexample to that principle, and it exposed a cost that is usually invisible: the cost of one extra service is not only memory and CPU but how it behaves when nobody is watching.
The mail service is entirely invisible in df -h and barely visible in top, yet it writes files into a queue every day. By the time it exhausts the inodes, the symptom is that the business system cannot be logged into, a symptom with no relation to mail at all.
The same logic applies to security: one more listening port, one more service that can send mail, is one more entry that can be exploited. The article on security hardening talks about closing unneeded services and narrowing the attack surface, which is the same action seen from the other side: turning off unneeded services solves both resources being quietly consumed and an oversized attack surface.
The test is one sentence: if this service died today, would the business be affected? If not, consider stopping it; if unsure, check whether it has done any real work lately (for mail, the queue and logs; for others, connection counts and logs), and stop it once confirmed idle.
10. Next Time Something Cannot Be Written, Follow This Order
Compress this path into a decision table. Order matters, because the first two steps lead people astray and the third is the real culprit this time:
| Order | What to check | Command | What the result means |
|---|---|---|---|
| 1 | Space | df -h | Full, so hunt large files level by level |
| 2 | Who holds deleted files | lsof +L1 | (deleted) present, restart that process to release |
| 3 | inodes | df -i | 100%, the case in this article, delete only |
| 4 | Who is creating files | du --inodes -d1 /var | Locate the directory, then drill down |
| 5 | Do writes work now | touch /tmp/x | Creates, so the path is open, then check the app |
| 6 | Will it recur | df -i again after 10 minutes | Still rising, the source is not stopped, back to step 4 |
Two reminders: steps 1 and 3 must be done together, and do not skip df -i because df -h looks good; treat no errors in the application log as a clue, not a conclusion.
No single link in this kind of fault is complicated; what is hard is that it appears in the wrong place, with every surface symptom pointing at the application while the culprit sits in the system layer, in a service nobody remembers installing. When Moges handles this kind of emergency, the first step is always to confirm clearly whether a write can succeed before looking upward, because a system that cannot even write its log is not to be trusted about anything it says. Such problems are usually cured by one on-site response plus a round of service consolidation, with the remaining effort in monitoring: with inodes, queue length and per-directory file counts visible, the next incident will not wait for a user to call.




