Re: Link count for directories

David Sterba
2020-08-31T13:23:48Z

On Wed, Aug 26, 2020 at 09:04:21AM +0200, Urs Thuermann wrote:
> > 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.

It is not but is this a problem in practice? Linux supports d_type and
that's what matters right now (manual page says that some BSDs support
that too).

> 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?

I can't find a reliable source for that but I remember reading it
somewhere.

> 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.

The usecasee you describe skips readdir in case there are no
directories, ie. if there are, full readdir will need to be done anyway.
I can see that this saves some calls but otherwise it's IMHO quite narrow.

> 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.

As said elsewhere, this comes with a cost of backward compatibility
issues and unfortunatelly overrides a 'nice to have' feature.