Walk through the scenario step by step, focusing on the distinction between the directory entry and the inode, and how open file descriptors keep the inode alive. Explain the behavior of each process (writer, tailer, rm) and the implications for disk space and system tools. Conclude with how lsof and the df/du discrepancy reveal the underlying mechanics.
Pro tip: Emphasize that the file is not truly deleted until all open file descriptors are closed, and mention that this is a common cause of disk space leaks in production. Suggest using lsof +L1 to quickly identify such files.
Before rm, log.txt has a directory entry pointing to an inode. The writer has an open file descriptor (fd) to the inode, and tail -f also has an open fd. The inode's link count is 1 (from the directory entry).
rm removes the directory entry for log.txt, decrementing the inode's link count to 0. However, because the writer and tailer still hold open file descriptors, the inode is not deallocated. The file data remains on disk.
The writer continues writing to the same inode via its fd; the data is appended to the file. The tailer, still reading from the same inode, continues to see new lines as they are written. Neither process is aware of the deletion.
Disk space is not reclaimed until all open file descriptors to the inode are closed. When the writer and tailer terminate (or close their fds), the inode's reference count drops to zero, and the file system frees the data blocks.
lsof | grep deleted shows the file as deleted but still open by processes. df reports the file system as full because the blocks are still allocated, while du does not count the file because it has no directory entry, leading to a discrepancy.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.