From d5a99c2f5e189694dfc1d8b55d1a69548f7f358d Mon Sep 17 00:00:00 2001 From: Zygo Blaxell Date: Mon, 20 Feb 2023 11:13:18 -0500 Subject: [PATCH] roots: don't share a RootFetcher between threads If the send workaround is enabled, it is possible for two threads (a thread running the crawl_new task, and a thread attempting to apply the send workaround) to access the same RootFetcher object at the same time. That never ends well. Give each function its own BtrfsRootFetcher object. Fixes: https://github.com/Zygo/bees/issues/250 Signed-off-by: Zygo Blaxell --- src/bees-roots.cc | 7 ++++--- src/bees.h | 1 - 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/bees-roots.cc b/src/bees-roots.cc index 5a29c5e..0276b51 100644 --- a/src/bees-roots.cc +++ b/src/bees-roots.cc @@ -500,7 +500,8 @@ BeesRoots::transid_max_nocache() // We look for the root of the extent tree and read its transid. // Should run in O(1) time and be fairly reliable. - const auto bti = m_root_fetcher.root(BTRFS_EXTENT_TREE_OBJECTID); + BtrfsRootFetcher root_fetcher(m_ctx->root_fd()); + const auto bti = root_fetcher.root(BTRFS_EXTENT_TREE_OBJECTID); BEESTRACE("extracting transid from " << bti); const auto rv = bti.transid(); @@ -927,7 +928,6 @@ BeesRoots::state_load() BeesRoots::BeesRoots(shared_ptr ctx) : m_ctx(ctx), - m_root_fetcher(ctx->root_fd()), m_crawl_state_file(ctx->home_fd(), crawl_state_filename()), m_crawl_thread("crawl_transid"), m_writeback_thread("crawl_writeback") @@ -1101,7 +1101,8 @@ BeesRoots::is_root_ro(uint64_t root) BEESTRACE("checking subvol flags on root " << root); - const auto item = m_root_fetcher.root(root); + BtrfsRootFetcher root_fetcher(m_ctx->root_fd()); + const auto item = root_fetcher.root(root); // If we can't access the subvol's root item...guess it's ro? if (!item || item.root_flags() & BTRFS_ROOT_SUBVOL_RDONLY) { return true; diff --git a/src/bees.h b/src/bees.h index 7a7868d..30175e4 100644 --- a/src/bees.h +++ b/src/bees.h @@ -534,7 +534,6 @@ class BeesScanMode; class BeesRoots : public enable_shared_from_this { shared_ptr m_ctx; - BtrfsRootFetcher m_root_fetcher; BeesStringFile m_crawl_state_file; map> m_root_crawl_map; mutex m_mutex;