Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why hard links not allowed to directories in UNIX/Linux [closed]

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?

like image 712
user567879 Avatar asked Oct 11 '11 01:10

user567879


People also ask

Why are hard links not allowed for 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.

Why doesn't the Unix file system allow hard links a to directories and B across mounted file systems?

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.

How do I allow a hard link to a directory in Linux?

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.

Are you allowed to create a hard link for a directory?

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.


1 Answers

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.

like image 167
bdonlan Avatar answered Oct 01 '22 10:10

bdonlan