Learning center

df says the disk is full, and du finds far less. Where is the space?

Two things take space that du cannot reach: a deleted file that a process still holds open, and files hidden under a mount point. Compare du -x with the Used column of df: lsof +L1 lists deleted files still held open, and a bind mount of the parent shows what a mount hides. When Used matches du and only Avail is zero, the gap to Size is blocks reserved for root, and when writes fail while df shows free blocks, df -i shows whether the inodes ran out.

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-api was 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/data onto its own volume. If the service wrote there before the volume was mounted, those files sit on the /var filesystem, under the new mount, where du -x cannot 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#

ObservationDeleted but openHidden under a mountReserved blocksInodes exhausted
df use, the alert100%100%100%blocks may be free, writes fail
df Used against du -xUsed largerUsed largerabout equalabout equal
lsof +L1 on the filesystemlists large filesnothing largenothing largenothing large
du of the parent through a bind mountsame as du -xlarger by the gapsamesame
Restarting the holder frees spaceyesnonono
df -inormalnormalnormal100%

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.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading