I read in text books that UNIX/Linux doesn't allows hard links to directories but soft links do? Is it because when we have cycles and if we create a hardlinks, it will point to some garbage values?
If cycles were the sole reason behind not allowing hardlinks, then why softlinks are allowed to directories?
Allowing hard links to directories would break the directed acyclic graph structure of the filesystem, possibly creating directory loops and dangling directory subtrees, which would make fsck and any other file tree walkers error prone.
Hardlinks are not permitted because they would lead to cycles. Once you allow cycles to form, you must perform a mark-and-sweep garbage collection to detect when isolated cycles of directories (no longer reachable from the root) can be finally deleted - this is extremely expensive on disk.
To create a hard links on a Linux or Unix-like system: Create hard link between sfile1file and link1file, run: ln sfile1file link1file. To make symbolic links instead of hard links, use: ln -s source link. To verify soft or hard links on Linux, run: ls -l source link.
In the case of a hard link, it can only exist in the same filesystem, while the symbolic link will persist cross-filesystems. Moreover, it can only be performed on regular files. You also can't create directory hard links, so it doesn't create a directory loop.
Hardlinks are not permitted because they would lead to cycles. Once you allow cycles to form, you must perform a mark-and-sweep garbage collection to detect when isolated cycles of directories (no longer reachable from the root) can be finally deleted - this is extremely expensive on disk.
Soft links do not cause this problem because they do not raise the reference count of the targeted directory; thus you can still get away with reference counting (with a maximum of one reference :).
The other issue is that programs which traverse the file system (eg, find
) need to avoid cycles. They could do this by remembering every inode number they've seen, but this is expensive - if they can distinguish between links which could lead to cycles (ie, softlinks) and links which will not lead to cycles (normal directory entries), and skip the softlinks, they don't need to track inode numbers anymore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With