btrfs: record new subvolume in parent dir earlier to avoid dir logging races
authorFilipe Manana <fdmanana@suse.com>
Thu, 19 Jun 2025 12:13:38 +0000 (13:13 +0100)
committerDavid Sterba <dsterba@suse.com>
Fri, 27 Jun 2025 17:57:24 +0000 (19:57 +0200)
commitbf5bcf9a6fa070ec8a725b08db63fb1318f77366
treee1dcbd792495e0b231ddd817079f15f58a74482a
parent5f61b961599acbd2bed028d3089105a1f7d224b8
btrfs: record new subvolume in parent dir earlier to avoid dir logging races

Instead of recording that a new subvolume was created in a directory after
we add the entry do the directory, record it before adding the entry. This
is to avoid races where after creating the entry and before recording the
new subvolume in the directory (the call to btrfs_record_new_subvolume()),
another task logs the directory, so we end up with a log tree where we
logged a directory that has an entry pointing to a root that was not yet
committed, resulting in an invalid entry if the log is persisted and
replayed later due to a power failure or crash.

Also state this requirement in the function comment for
btrfs_record_new_subvolume(), similar to what we do for the
btrfs_record_unlink_dir() and btrfs_record_snapshot_destroy().

Fixes: 45c4102f0d82 ("btrfs: avoid transaction commit on any fsync after subvolume creation")
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ioctl.c
fs/btrfs/tree-log.c