8

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?

user567879
  • 5,139
  • 20
  • 71
  • 105
  • @bmargulies Then why is it possible with softlinks – user567879 Oct 11 '11 at 01:54
  • Because the kernel doesn't follow them in the fundamental traversal of the directory hierarchy. It can walk the tree, because it doesn't traverse soft links when walking the tree. – bmargulies Oct 11 '11 at 01:55
  • http://stackoverflow.com/questions/4416308/file-systems-with-support-to-directory-hard-linking – Brian Roach Oct 11 '11 at 01:59
  • @bmargulies Could you please tell why kernel does traversal on hardlinks and not in softlinks. I mean the reason behind it. Or can I get a simple example depicting that (traversal) – user567879 Oct 11 '11 at 01:59
  • I don't understand I see both hard and soft links in linux often. – Gabriel Fair Apr 17 '12 at 16:13
  • 1
    You can use bind mount to simulate hard linking directories sudo mount --bind /some/existing_real_contents /else/dummy_but_existing_directory sudo umount /else/dummy_but_existing_directory – zainengineer Apr 11 '16 at 04:47

1 Answers1

14

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.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
  • I got the first and third paragraph right. But couldn't understand the second one. COuld you please give a more clear explanation – user567879 Oct 11 '11 at 02:04
  • 2
    @user567879, in other words, if you have a soft link to a directory, it doesn't prevent the directory from being deleted and removed from disk – bdonlan Oct 11 '11 at 02:33
  • 1
    +1. A more detailed explanation of the 3rd paragraph: http://unix.stackexchange.com/questions/22394 – ignis Oct 11 '12 at 05:21
  • I disagree about the necessity for mark-and-sweep garbage collection. Even if cycles are allowed, you can never get into a situation where you have a cycle of objects which are not pointed to by anything except each other. This is because directories always have to be deleted depth first. – Lqueryvg Nov 21 '14 at 13:06
  • In an act of necromancy: Directories may need to be deleted depth-first, but this is something that is done in userland, not in the FS. (I think) the FS simply refuses to delete if the directory has children -- and where foo/bar mutually contains foo/baz, both have children -- each other. – Mr. B Dec 04 '17 at 17:22
  • Paragraph 3 is confusing because find is not an appropriate example for your point In fact, keeping track of inodes is precisely what find does do, because POSIX requires loop avoidance (and a diagnostic). Take a look at the code. – James Youngman Oct 11 '18 at 13:14