]> git.hungrycats.org Git - bees/commitdiff
context: introduce s_sectorsize and s_clone_alignment statics
authorZygo Blaxell <bees@furryterror.org>
Thu, 23 Apr 2026 04:10:05 +0000 (00:10 -0400)
committerZygo Blaxell <bees@furryterror.org>
Tue, 6 Oct 2026 05:51:40 +0000 (01:51 -0400)
Add two static uint32_t members to BeesContext that hold the
filesystem's sectorsize and clone_alignment as reported by
BTRFS_IOC_FS_INFO.  These become the source of truth for new code
in place of the hardcoded BLOCK_SIZE_CLONE / BLOCK_SIZE_SUMS
constants.  Static is justified because the daemon manages exactly
one filesystem per instance.

Population happens in BeesContext::set_root_fd, the earliest point
where m_root_fd is usable — before BeesConfig parsing, hash table
init, or any other subsystem that might want to consult these
values.  An assertion guards against zero values so a misbehaving
ioctl can't silently produce a divide-by-zero in downstream code.

Log the discovered values at INFO so the daemon's startup banner
records what the filesystem actually claims.  On big-page or
non-default-sectorsize filesystems this makes the deviation from
4096 visible in the log.

Soft-deprecate BLOCK_SIZE_CLONE in its doc comment: new code must
use BeesContext::s_clone_alignment instead.  The constant is kept
because some existing call sites use it as a template parameter
or fixed array bound that needs a compile-time value; those can
migrate incrementally as their surrounding code changes.

Assisted-by: Claude-Code:claude-opus-4-7
Signed-off-by: Zygo Blaxell <bees@furryterror.org>
src/bees-context.cc
src/bees.h

index 26c0eb108357bf9f1777dd68016fc41b5f9bcbf2..1eb4ba6abc6062542002c55440ea31ebc3017eac 100644 (file)
 using namespace crucible;
 using namespace std;
 
+// Definitions for the static block-size members declared in bees.h.
+// Initialised by BeesContext::set_root_fd() from BTRFS_IOC_FS_INFO.
+uint32_t BeesContext::s_sectorsize      = 0;
+uint32_t BeesContext::s_clone_alignment = 0;
+
 void
 BeesContext::set_reporter(shared_ptr<BeesReporter> reporter)
 {
@@ -1043,6 +1048,24 @@ BeesContext::set_root_fd(const Fd &fd)
        }
        m_root_fd = fd;
 
+       // Populate filesystem-wide block-size constants from BTRFS_IOC_FS_INFO.
+       // These are static (single-instance daemon) and become the source of
+       // truth for new code in place of the hardcoded BLOCK_SIZE_CLONE /
+       // BLOCK_SIZE_SUMS constants.  Must run before any other subsystem can
+       // observe them — set_root_fd is the earliest point where m_root_fd is
+       // usable, and runs before BeesConfig parsing or hash table init.
+       {
+               BEESTRACE("fetching BTRFS_IOC_FS_INFO for sectorsize / clone_alignment");
+               BtrfsIoctlFsInfoArgs fsi;
+               fsi.do_ioctl(m_root_fd);
+               s_sectorsize      = fsi.sectorsize;
+               s_clone_alignment = fsi.clone_alignment;
+               THROW_CHECK1(runtime_error, s_sectorsize, s_sectorsize > 0);
+               THROW_CHECK1(runtime_error, s_clone_alignment, s_clone_alignment > 0);
+               BEESLOGINFO("filesystem sectorsize " << s_sectorsize
+                       << ", clone_alignment " << s_clone_alignment);
+       }
+
        // 65536 is big enough for two max-sized extents.
        // Need enough total space in the cache for the maximum number of active threads.
        m_resolve_cache.max_size(65536);
index 55422ed575c4e79a2c69faa97fd32ef6dc7a8864..62509da259c828b66f5b0734b252db4416e7b03e 100644 (file)
@@ -48,7 +48,12 @@ using namespace std;
 /// Required alignment for the FIDEDUPERANGE (FILE_EXTENT_SAME) ioctl source and
 /// destination offsets.  Should match the filesystem's clone_alignment, but is
 /// currently hardcoded to 4096.
-/// FIXME: should read this from /sys/fs/btrfs/<FS-UUID>/clone_alignment
+///
+/// Soft-deprecated for new code: use @ref BeesContext::s_clone_alignment instead,
+/// which is populated from BTRFS_IOC_FS_INFO at startup and reflects the kernel's
+/// authoritative value.  This constant is kept because some existing call sites
+/// rely on it being a compile-time value (template parameters, fixed-size arrays).
+/// Do not introduce new uses; existing uses can migrate incrementally.
 const off_t BLOCK_SIZE_CLONE = 4096;
 
 /// Granularity at which blocks are hashed for deduplication.  Must be a multiple
@@ -1278,6 +1283,23 @@ struct BeesResolveAddrResult {
  * @c shared_ptr<BeesContext>.
  */
 class BeesContext : public enable_shared_from_this<BeesContext> {
+public:
+       /// Filesystem sectorsize, in bytes (from BTRFS_IOC_FS_INFO).
+       /// Populated once by set_root_fd(); static because the daemon manages
+       /// exactly one filesystem.  Use this in new code in place of the
+       /// hardcoded BLOCK_SIZE_SUMS / BLOCK_SIZE_CLONE constants where the
+       /// kernel value is the source of truth (alignment checks, EOF tail
+       /// computation, etc.).  Existing constants remain for places that
+       /// still need a compile-time value (template parameters, array sizes).
+       static uint32_t s_sectorsize;
+
+       /// Filesystem clone-alignment, in bytes (from BTRFS_IOC_FS_INFO).
+       /// On modern btrfs this equals sectorsize, but the two are surfaced
+       /// separately by the kernel.  Use this for EXTENT_SAME alignment
+       /// checks; use s_sectorsize for I/O / read alignment.
+       static uint32_t s_clone_alignment;
+
+private:
        Fd                                              m_home_fd;    ///< FD to the bees home directory (contains state files).
 
        shared_ptr<BeesConfig>                          m_config;