The symptom and the explanations#
The example is fictional. At 02:10 UTC a disk alert fired on ledger-api-3: /var at 100%. df -h /var reported a 50 GiB filesystem with 50 GiB used and nothing available. du -xsh /var added up the files and found 31 GiB. Nineteen gibibytes were somewhere du did not look, and the incident channel had three explanations:
- A deleted file is still open. logrotate runs at midnight. If
ledger-apiwas never told to reopen its log, it kept writing to a file that the next rotation deleted, and the kernel keeps a deleted file until the last descriptor on it is closed. unlink(2) - Files sit under a mount point. Yesterday's deploy moved
/var/lib/ledger/dataonto its own volume. If the service wrote there before the volume was mounted, those files sit on the/varfilesystem, under the new mount, wheredu -xcannot reach them. - The space is reserved, or the inodes ran out. ext4 reserves a share of blocks for root, 5% by default, which counts as neither used nor available to other users. An inode table that runs out stops new files while blocks remain. mke2fs(8)
The disk usage graph that fired the alert rises under every one of them. Choose the observation where explanations disagree
What each explanation predicts#
| Observation | Deleted but open | Hidden under a mount | Reserved blocks | Inodes exhausted |
|---|---|---|---|---|
df use, the alert | 100% | 100% | 100% | blocks may be free, writes fail |
df Used against du -x | Used larger | Used larger | about equal | about equal |
lsof +L1 on the filesystem | lists large files | nothing large | nothing large | nothing large |
du of the parent through a bind mount | same as du -x | larger by the gap | same | same |
| Restarting the holder frees space | yes | no | no | no |
df -i | normal | normal | normal | 100% |
The second row splits the explanations into two pairs: a gap between Used and du points to space du cannot see, and no gap means the files du counts are the whole story. The third and fourth rows then split the first pair. Comparing du with Size instead of Used mixes the reserved blocks into the gap and sends people looking for a deleted file that does not exist.
df itself reports both views: --output selects fields such as used, avail, iused and ipcent. df invocation du -x stays on one filesystem and skips directories mounted from others. du invocation
The lab: three ways to lose space#
The script builds a 32 MiB tmpfs with room for 200 inodes and produces each explanation on it in turn. It uses root, GNU coreutils and util-linux on a disposable Linux VM. Run it in a private mount namespace with the command below. Save it as lab.sh.
#!/bin/sh
set -eu
[ "$(id -u)" -eq 0 ] || { echo "Run in a private mount namespace as root." >&2; exit 1; }
L=$(mktemp -d)
P=
cleanup() {
set +e
exec 3>&-
for path in "$P" "$L/data" "$L"; do
if [ -n "$path" ] && mountpoint -q "$path"; then umount "$path"; fi
done
if [ -n "$P" ]; then rmdir "$P"; fi
rmdir "$L"
}
trap cleanup EXIT
trap 'exit 1' HUP INT TERM
P=$(mktemp -d)
mount -t tmpfs -o size=32m,nr_inodes=200 lab "$L"
[ "$(findmnt -n -M "$L" -o FSTYPE)" = tmpfs ]
report() { echo "$1: df used $(df -m --output=used "$L" | tail -1 | tr -d ' ') MiB, du $(du -smx "$L" | cut -f1) MiB"; }
# World 1: a log file deleted while a process still holds it open
exec 3>"$L/app.log"
head -c $((24 << 20)) /dev/zero >&3
rm "$L/app.log"
report "deleted, still open"
echo " held by /proc/$$/fd/3 -> $(basename "$(readlink /proc/$$/fd/3)")"
exec 3>&-
report "after closing the descriptor"
# World 2: a file written into a directory before a filesystem was mounted over it
mkdir "$L/data"
head -c $((24 << 20)) /dev/zero > "$L/data/old.bin"
mount -t tmpfs -o size=8m data "$L/data"
report "hidden under a mount"
mount --bind "$L" "$P"
echo " a bind mount of the parent shows $(du -sm "$P/data" | cut -f1) MiB under data/"
umount "$P" "$L/data"; rm "$L/data/old.bin"
# World 3: many small files use up the inodes before the blocks
i=0
while [ "$i" -lt 200 ] && touch "$L/f$i" 2>/dev/null; do i=$((i + 1)); done
report "after $i empty files"
echo " inodes: $(df --output=iused,iavail,ipcent "$L" | tail -1)"
Before you run it, write down df and du for each line. The prediction: in the first world df counts the 24 MiB and du does not until the shell closes its descriptor; in the second, du misses the 24 MiB until the bind mount shows it; in the third, both stay near zero while the inodes reach 100%. sudo unshare --mount --propagation private sh lab.sh printed this on Linux 7.0 with GNU coreutils 9.4:
deleted, still open: df used 24 MiB, du 0 MiB
held by /proc/146102/fd/3 -> app.log (deleted)
after closing the descriptor: df used 0 MiB, du 0 MiB
hidden under a mount: df used 24 MiB, du 0 MiB
a bind mount of the parent shows 24 MiB under data/
after 198 empty files: df used 0 MiB, du 0 MiB
inodes: 200 0 100%
The prediction fails if df drops at the rm while the descriptor is open, if the bind mount shows nothing under data/, or if the empty files fill blocks before inodes. Repeat with 12 MiB and compare the gap in each case. The bind mount works because it attaches a single filesystem without the filesystems mounted inside it. mount(8)
Read it on a real system#
These commands only read, except where marked. Start with the two numbers that split the table:
df -h --output=source,size,used,avail,pcent,target /var
du -xsh /var
df -i /var
For deleted files still held open, lsof +aL1 with a filesystem selects unlinked open files on that filesystem; without lsof, the descriptor links in /proc carry the same answer. lsof(8) ยท proc_pid_fd(5)
lsof -nP +aL1 /var
find /proc/[0-9]*/fd -lname '/var/*(deleted)' -printf '%h -> %l\n' 2>/dev/null
stat -L -c '%s bytes' /proc/PID/fd/FD
The fix is to make the holder close the file: restart it, or send the signal its logrotate postrotate script should have sent. logrotate's copytruncate exists for programs that cannot be told to reopen their log. logrotate(8) Both change state. So does truncating through the descriptor with : > /proc/PID/fd/FD, which frees the space at once and discards the log's contents.
To look under a mount point, bind-mount the parent filesystem somewhere else. This adds a mount, so remove it afterwards:
mkdir /mnt/var-under && mount --bind /var /mnt/var-under # changes the mount table
du -sh /mnt/var-under/lib/ledger/data
umount /mnt/var-under && rmdir /mnt/var-under
For reserved blocks on ext4, tune2fs -l lists the superblock, including Reserved block count and Block count. tune2fs(8) For inodes, du --inodes -x finds the directories that hold the most files. du invocation
tune2fs -l /dev/nvme1n1 | grep -Ei '^(block count|reserved block count|block size)'
du --inodes -x /var | sort -n | tail
A process that writes its log into a deleted file is also why the log lines stop arriving where people look. A log line is missing
Where the check misleads#
Containers have their own mount namespaces. du inside a container sees the container's view, and a deleted file held by a container's process still occupies the host filesystem. Run the /proc search from the host to see every process's descriptors.
Reading another process's descriptor links passes a ptrace access check, so lsof and the find above run as an ordinary user miss other users' processes, and their empty result proves nothing. Run them as root. proc_pid_fd(5)
A gap between Used and du that disappears and comes back is often both explanations: a deleted log freed by a restart, then refilled because the rotation still does not signal the process. Fix the rotation, or the next night repeats it.
Filesystem metadata takes some blocks that no file owns, so Used sits slightly above du on a healthy filesystem. In the lab an empty 32 MiB tmpfs reported 0 MiB, and an empty 100 MiB ext4 image reported 1 MiB.
Questions#
Why does deleting a large log file not free disk space?
The kernel removes the file's name at once, and its data only when the last open descriptor on it closes. A running process that still writes to the log keeps the space allocated. lsof +L1 shows the process; restarting it or making it reopen the log frees the space.
Why does df show 100% when Used is less than Size?
On ext4, blocks reserved for root are counted as neither used nor available to other users. When Avail reaches zero, df reports the filesystem full, while Used stays short of Size by the reserved amount. Root can still write into the reservation.
What does "No space left on device" mean when df shows free space?
The message is the text of ENOSPC, and a filesystem returns it when it has no inode left for a new file as well as when it has no blocks; the lab's touch loop stopped on it after 198 empty files, with every block free. errno(3) df -i shows IUse% at 100%, and du --inodes -x finds the directory full of small files.