Re: Link count for directories

Urs Thuermann
2020-08-26T07:05:10Z

Chris Mason <clm@fb.com> writes:

> > Wrong, ext4 and xfs do maintain the count.
>
> Correct, at least until they hit 65536.

For ext4 the limit is actually 65000 links. For directories, nlink is
set to 1 if an additional sub-directory is created (and stays at 1
even if you remove directories), while for other file you get EMLNK.

In xfs there seems to be no limit (dirs and other files, I have seen
more than 200000 for both) and btrfs has a limit of 65535 links on
non-directories.

> Find certainly paid attention, but from an optimization point of
> view, dtype in directory entries is dramatically more helpful.

1. dtype is not in POSIX. OTOH, I seem to remember that POSIX states
st_link == 1 for directories or otherwise it has the traditional
value of 2 + sub-directory count. However, I cannot find this
anymore. Is that correct and can anyone give me a pointer?

2. If you just want to find all directories you only need stat(2) on a
directory and if it has st_nlink == 2 you don't need to read all
directory entries (with or without dtype). So this optimization is
possible with the traditional link count of directories but not
with dtype.

> I'd want to see big wins from applications before adding this
> into btrfs.

I would expect noticable but not big wins. However, I'd like adding
this to btrfs just because it looks nicer. Since ls (and readdir)
gives you the . and .. links they should be counted in the usual way.

urs