Why can't directories be hard-linked on Unix filesystems (e.g. HFS)?

On both my MacBook (OS 10.5.8) and Linux (RHEL4) I've tried to do some directory organiztion using hard links (ln, not ln -s). When I try to hard-link a directory, I get an error message:

{yakut05:81} mkdir tempo
{yakut05:83} ln tempo tempo-link
ln: `tempo': hard link not allowed for directory

The man page for ln doesn't specify this limitation. I wonder if any filesystem gurus out there could explain why it's not allowed? I'm sure it has to do with the inode structure, but I'd like to be clear on the details.

sort by: active | newest | oldest
ganglion5 years ago
I think this is a deliberate design choice, not a bug. If hard linked directories were allowed, then you would have the possibility of creating circular loops in the filesystem structure. This make it much harder for filesystem search algorithms to produce a comprehensive list of files and directories under a certain point.

With a hard link, there is literally no difference between one link name and the other - they are both pointers to inodes - so the search algorithm would have no way of knowing which was the 'real' name and which was the link.
kelseymh (author)  ganglion5 years ago
Ah, ha! Thank you :-) I see your point about the possibility of completely invisible circular loops. With symlinks, the circularlity is explicit, and can be excluded with careful coding (find handles it, for example), but that wouldn't be the case with hard links.
Yes - that's why you can only soft link directories.

I'm glad this was useful - I found the comment from last year after looking your page, and I wasn't sure whether you would have forgotten about it or found the answer anyway.
lemonie7 years ago
You won't have overlooked anything obvious or made any basic error. Might the permissions or path be "funny" on the target drive / directory?

kelseymh (author)  lemonie7 years ago
I don't think so.  The same ln command, in the same directory, works perfectly well if I apply it to a simple file.  That's why I'm confused.