How does linux work with symbolic links

filesystemssymlink

I mean what's going on when some process wants to read a symlink? What's going on when something changes a symlink during a read or even write process?

For example: I have 2 huge, similar 100G files /mnt/1 and /mnt/2. /mnt/1 is available via the symlink /home/user/file.
Some program A starts reading /home/user/file. And after a while something changes the link from /mnt/1 to /mnt/2, but A is still reading the file.

Does the program cache the absolute path?

Will it fail and error, because the symlink was changed or will it work fine, like nothing happened?

Will it differ in case /home/user/file is linked to a block device (for example 2 replicated iscsi disks)?

Best Answer

The symlink points to the name of the real file (inode) in the file system. When the system resolves that symlink to find the actual file and open it, it finds and uses the file's inode. At that point, the path you used to get to the file doesn't matter. What the OS doesn't cache, it reads from the file by its inode. You could, as I understand, start reading the file through a hard link and remove that hard link (as long as the file is still linked from somewhere else), and it wouldn't cause problems as long as the file has been resolved (name string->inode).

Related Question