Re: Link count for directories

David Sterba
2020-08-31T13:19:41Z

On Fri, Aug 21, 2020 at 07:40:19PM +0200, Steve Keller wrote:
> Are there any plans to implement the traditional link count behavior in btrfs,
> as described in the following URL?
>
> https://btrfs.wiki.kernel.org/index.php/Project_ideas#Track_link_count_for_directories
>
> Would it be a major effort to do so? I'd really like that feature.

So the main concern is backward compatibility and what would happen if a
filesystem with nlink tracking gets mounted by a kernel without the
support. The wiki project entry seems to be too optimistic regarding
that (and I think it was me adding it there):

It seems that the link count can be tracked like the other filesystems
do. This will be even backward compatible:

* for new directories and subvolumes , set the initial link count to 2
* a mkdir/rmdir/move/snapshot will update the link count accordingly
iff the current link count is not 1

Bad scenario:

* new kernel creates directory with many sudirectories, with nlink eg 100
* reboot to an older kernel
* delete some of the subdirectories, nlink untouched and silently out of
sync
* reboot to new kernel
* creating more subdirectories will not fix nlink, only add the new
entries, and deletion can go below zero (though it would stay stop at
1)

This does not sound unrealistic to me, eg booting a new kernel after
update and then going back because some random driver does not work.
All the directories created meanwhile will be affected.

Usually such incompatibilies are shielded by incompat bits but in this
case it sounds like a too heavy measure for a minor performance
optimization.

I'll move the project idea away with explanation why it's not
implemented.