23

Is there a limit of number of hardlinks for one file? Is it specified anywhere? What are safe limits for Linux? And what for other POSIX systems?

15

Posix requires that the operating system understand the concept of hard links but not that hard links can actually be used in any particular circumstance. You can find out how many hard links are permitted at a particular location (this can vary by filesystem type) by calling pathconf(filename, _PC_LINK_MAX). The minimum limit (_POSIX_LINK_MAX) is 8, but this is rather meaningless as link() can report many other errors anyway (permission denied, disk full, …).

The stat structure stores the link count in a field of type nlink_t, so the type of this field gives an upper limit on your system. But there's a good chance you'll never be able to reach that far: it's common to have a 32-bit nlink_t but only 16 bits in many filesystems (a quick grep in the Linux source shows that ext[234], NTFS, UFS and XFS use 16-bit link counts in the kernel data structures).

  • As a supplement: BTRFS seems to use 8 bit link count. – Maciej Piechotka Jan 3 '12 at 17:15
  • @Rather, 8-bit link count for hard links in the same directory, but 32-bit count in total, according to this blog post/ – Gilles Jul 15 '16 at 14:00
10

Looking at the ext3 inode structure disk format in the linux kernel sources (*include/linux/ext3_fs.h*) that lists the links count as being a 16 bit number

struct ext3_inode {
    ... snip ...
    __le16  i_links_count;  /* Links count */

}

I guess that means that an ext3 filesystem can have up to 65535 links.

I haven't checked the values for other filesystems.

10

This is file system dependent.

ext2/3/4 limit is 65k links

ext4 source line 643, struct ext4_inode -> __le16 i_links_count

  • 1
    The actual limit is 65534. The last counter entry is used for a transient in the rename() system call. – Joshua Jan 11 '11 at 0:03
  • 4
    Even less (found in same ext4.h): /* Maximal count of links to a file */ #define EXT4_LINK_MAX 65000 – PoltoS Apr 23 '12 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.