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?
3 Answers
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).
-
-
@Rather, 8-bit link count for hard links in the same directory, but 32-bit count in total, according to this blog post/ Jul 15, 2016 at 14:00
-
In a quick test on an XFS, I'm seeing python3 -c 'import os; print(hex(os.pathconf("./t", "PC_LINK_MAX")))' give 0x7fffffff. That's quite a lot more links than 8 or 16 bit. May 21, 2020 at 5:32
-
@dst And can you create that many hard links? git.kernel.org/pub/scm/fs/xfs/xfs-documentation.git/tree/design/… says that the link count on XFS is a 32-bit type. There has been at least one new version of the XFS format since I wrote this answer, maybe the limit had increased. May 21, 2020 at 6:05
-
>>> import os >>> sb = os.stat('65536') >>> sb os.stat_result(st_mode=33188, st_ino=3775483279, st_dev=2049, st_nlink=65538, st_uid=0, st_gid=0, st_size=0, st_atime=1590074443, st_mtime=1590074443, st_ctime=1590074561) So that file has a link count of 65538. May 21, 2020 at 16:09
This is file system dependent.
ext2/3/4 limit is 65k links
ext4 source line 643, struct ext4_inode
-> __le16 i_links_count
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.