Why a hard link cannot link to a directory

Source: Internet
Author: User

We can see in any directory with the ls-l command is at least 2 of the number of connections, which also shows that there is a hard connection in the system, and the command ln-d can also allow the superuser to make a hard connection to the directory, which shows that the system restricts the hard connection to the directory is only a hard rule, is not logically disallowed or technically non-line. So why should the operating system limit it?  There may be two answers. First of all, if the introduction of a hard connection to the directory is likely to introduce a loop in the directory, then the system will fall into an infinite loop when the directory is traversed. Perhaps you would say that symbolic connections can also introduce loops, so why not restrict the symbolic connection of the directory? The reason is that in the Linux system, each file (directory is also a file) corresponds to an inode structure, where the Inode data structure contains the file type (directory, ordinary files, symbolic connection files and so on) information, which means that the operating system can traverse the directory to determine the symbolic connection, Since you can determine the symbolic connection of course, you can take some measures to prevent entering a large cycle, the system in the continuous encounter 8 symbols connected to stop the traversal, which is why the directory symbol connection will not enter the cause of the dead loop.  But for the hard connection, because of the data structure and the algorithm limitation in the operating system, this kind of dead loop can't be prevented at present. Before we explain the second reason, let's take a look at what the dentry structure of the file looks like in the system space and how they are stored in the system space. The dentry structure mainly contains the file name, the inode number of the document, pointers to the parent directory Dentry structure, and other pointers unrelated to this discussion, where the key is the pointer to the parent directory, and all the dentry structures in the system are stored in the hash table by the hash value. The hashing algorithm here is important, it is to take the filename and the file's parent directory Dentry structure of the address together hash operation out the hash value. Now let's assume that there are two directories/A and/b, where/b is the hard connection to/A that we set up with the ln-d command. This time in the kernel space there will be a/a dentry structure and A/b dentry structure, from the above knowledge, each file or directory under the/b directory has a corresponding dentry structure (because although the/a directory below the file name does not change, However, because the dentry structure has pointers to the parent directory Dentry and computes the hash value, the address of the parent directory Dentry structure is taken into account, and the dentry structure is persisting at this time, and this inheritance also affects the files under all subdirectories, which can waste a lot of system space. Especially if there are a large number of files and subdirectories in the hard-connected directory. This may be the second reason.

Why hard links cannot be linked to a directory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.