diff --git a/vfs/dir.go b/vfs/dir.go index 465b2bc03..1d9d47609 100644 --- a/vfs/dir.go +++ b/vfs/dir.go @@ -1142,6 +1142,15 @@ func (d *Dir) Remove() error { fs.Errorf(d, "Dir.Remove failed to remove directory: %v", err) return err } + // The directory is gone, so no listing will resolve its virtual + // entries, which can only be deletes as it was empty. They must be + // removed now, or they will be counted in every parent and + // cache forgets blocked as a consequence + d.mu.Lock() + for leaf := range d.virtual { + d._deleteVirtual(leaf) + } + d.mu.Unlock() // Remove the item from the parent directory listing if d.parent != nil { d.parent.delObject(d.Name()) diff --git a/vfs/dir_test.go b/vfs/dir_test.go index c3ac395bb..12fcc5dc0 100644 --- a/vfs/dir_test.go +++ b/vfs/dir_test.go @@ -553,6 +553,24 @@ func TestDirRemove(t *testing.T) { assert.Equal(t, EROFS, err) } +// Removing a directory leaves no virtual entries counted in its parent, so the +// parent can still forget its cache. +func TestDirRemoveVirtuals(t *testing.T) { + _, vfs, dir, _ := dirCreate(t) + root, err := vfs.Root() + require.NoError(t, err) + + // Remove the directory's contents and then the directory, before a + // listing has confirmed the contents are gone. + require.NoError(t, dir.RemoveName("file1")) + require.NoError(t, root.RemoveName("dir")) + + assert.False(t, root.ForgetAll(), "root holds virtual entries after rmdir") + + dir.ForgetAll() + assert.False(t, root.ForgetAll(), "root holds virtual entries after the removed dir is forgotten") +} + func TestDirRemoveAll(t *testing.T) { r, vfs, dir, _ := dirCreate(t)