IntroductionUntil recently, I thought that on ext file system, inodes have reference counters which count the number of times the file is referenced by a directory entry or a file descriptor.Then, I learned that the reference counter only counts the number of directory entries referencing it. To falsify this, I read the reference count of a video file using ls -l. It was 1 as I expected because I didn't create any additional hard links to it. I then opened the video file with a video player and executed the same command again. To my surprise, the reference count was still 1. Therefore, I failed at falsifying.However, I can definitely continue watching the video after removing its only directory entry. When opening a big video file and deleting its directory entry, the amount of free storage space on the file system does not change. It only changes (by the size of the video file) when the player reached the end of the video and closes the file descriptor or the player terminates itself (depending on the video player used).QuestionWhat are the exact conditions for a file to be freed on an ext file system? I'm interested in how it is handled in ext2, ext3, and ext4. Are there differences depending on the kernel used or other parts of the operating system?
When is a file freed in an ext file system?
linux;files;filesystems;ext4;open files
You are confusing two different counters: the file system link counter and the file descriptor reference counter.The file system link counter counts how many links to an inode are in the file system itself. The inode is the structure that contains the file metadata. In ext* file systems this counter is stored in the file system itself.You can verify how many links has a inode using ls -l. In addition, you can use ls -i to get the inode number of a file. E.g. try to multiply the links to a file using ln and verify that all links have the same inode number.andcoz@tseenfoo:~/refcount> ls -litotal 402248813 -rw-r--r-- 1 andcoz users 40960  7 feb 21.34 testandcoz@tseenfoo:~/refcount> ln test test2andcoz@tseenfoo:~/refcount> ln test test3andcoz@tseenfoo:~/refcount> ls -litotal 1202248813 -rw-r--r-- 3 andcoz users 40960  7 feb 21.34 test2248813 -rw-r--r-- 3 andcoz users 40960  7 feb 21.34 test22248813 -rw-r--r-- 3 andcoz users 40960  7 feb 21.34 test3The file descriptor reference counter counts how many times a file is open by a process or, more formally, how many file descriptors reference that inode. This information is stored in kernel memory.You can get an approximation of this value using fuser command. This command lists all the processes that have a file open. Note that a single process could open the same file multiple times, so fuser list size is less or, usually, equal to reference counter.andcoz@tseenfoo:~/refcount> tail -f test &[3] 4226andcoz@tseenfoo:~/refcount> fuser test/home/andcoz/refcount/test:  4226andcoz@tseenfoo:~/refcount> tail -f test2 &[4] 4354andcoz@tseenfoo:~/refcount> fuser test/home/andcoz/refcount/test:  4226  4354A file is removed from the file system when both the counters are zero.