Skip to contentSkip to conversation

deleted subvolume isn't cleaned while an inotify watch is on its root

DISCUSSION [email protected] 2 participants 2 messages Nov 2 – Nov 6 Btrfs file system

lore
Share & tools
lore
#0 lore raw

Hi,

A subvolume deleted while something has an inotify watch on its root directory stays in "btrfs subvolume list -d" until the watch is removed. The watch never gets IN_DELETE_SELF or IN_IGNORED, so the watcher has no reason to drop it. Same with rmdir of an empty subvolume. I hit this with syncthing, which had watches on a few subvolumes inside a synced folder; they were only cleaned once syncthing let go.

To reproduce:

truncate -s 512M /var/tmp/btrfs.img
mkfs.btrfs -q /var/tmp/btrfs.img
mkdir /var/tmp/t
mount -o loop /var/tmp/btrfs.img /var/tmp/t
btrfs subvolume create /var/tmp/t/sv
inotifywait -m /var/tmp/t/sv &
sleep 1
btrfs subvolume delete /var/tmp/t/sv
sleep 30; btrfs filesystem sync /var/tmp/t
btrfs subvolume list -d /var/tmp/t
kill %1
sleep 5; btrfs filesystem sync /var/tmp/t
btrfs subvolume list -d /var/tmp/t

Output on 7.3.0 (CachyOS build):

Create subvolume '/var/tmp/t/sv'
Setting up watches.
Watches established.
/var/tmp/t/sv/ OPEN,ISDIR
/var/tmp/t/sv/ CLOSE_NOWRITE,CLOSE,ISDIR Delete subvolume 256 (no-commit): '/var/tmp/t/sv' ID 256 gen 9 top level 0 path DELETED

The first "list -d" is 30 seconds after the delete; the second, after killing inotifywait, prints nothing. Without the inotifywait the subvolume is cleaned right away.

It looks like btrfs_delete_subvolume() leaves i_nlink of the root inode at 1, so dentry_unlink_inode() never calls fsnotify_inoderemove(), and the mark keeps the inode, and with it the dead root, alive. kernfs had the same problem and fixed it in 7.1 by clearing i_nlink on removal (eea5d2bb34ba).

Thanks,
Alex

#1 lore raw
Quoted 31 lines · On Mon, Nov 2, 2026 at 5:20 PM <[email protected]> wrote:

Hi,

A subvolume deleted while something has an inotify watch on its root directory stays in "btrfs subvolume list -d" until the watch is removed. The watch never gets IN_DELETE_SELF or IN_IGNORED, so the watcher has no reason to drop it. Same with rmdir of an empty subvolume. I hit this with syncthing, which had watches on a few subvolumes inside a synced folder; they were only cleaned once syncthing let go.

To reproduce:

truncate -s 512M /var/tmp/btrfs.img
mkfs.btrfs -q /var/tmp/btrfs.img
mkdir /var/tmp/t
mount -o loop /var/tmp/btrfs.img /var/tmp/t
btrfs subvolume create /var/tmp/t/sv
inotifywait -m /var/tmp/t/sv &
sleep 1
btrfs subvolume delete /var/tmp/t/sv
sleep 30; btrfs filesystem sync /var/tmp/t
btrfs subvolume list -d /var/tmp/t
kill %1
sleep 5; btrfs filesystem sync /var/tmp/t
btrfs subvolume list -d /var/tmp/t

Output on 7.3.0 (CachyOS build):

Create subvolume '/var/tmp/t/sv'
Setting up watches.
Watches established.
/var/tmp/t/sv/ OPEN,ISDIR
/var/tmp/t/sv/ CLOSE_NOWRITE,CLOSE,ISDIR Delete subvolume 256 (no-commit): '/var/tmp/t/sv' ID 256 gen 9 top level 0 path DELETED

Quoted 3 lines

The first "list -d" is 30 seconds after the delete; the second, after killing inotifywait, prints nothing. Without the inotifywait the subvolume is cleaned right away.

It looks like btrfs_delete_subvolume() leaves i_nlink of the root inode at 1, so dentry_unlink_inode() never calls fsnotify_inoderemove(), and the mark keeps the inode, and with it the dead root, alive. kernfs had the same problem and fixed it in 7.1 by clearing i_nlink on removal (eea5d2bb34ba).

Thanks for the report.

Fix sent here:

https://lore.kernel.org/linux-btrfs/de103b6af03cec8d98b21e0734fb482f9244bed6.1793988522.git.fdmanana@suse.com/

Quoted 5 lines

Thanks,
Alex