]> git.hungrycats.org Git - bees/commitdiff
crucible: support LOGICAL_INO_V2 commit-root resolution
authorZygo Blaxell <bees@furryterror.org>
Sat, 19 Sep 2026 08:09:05 +0000 (04:09 -0400)
committerZygo Blaxell <bees@furryterror.org>
Tue, 6 Oct 2026 05:51:40 +0000 (01:51 -0400)
The live-root backref walk attaches to the running transaction and holds
a tree mod log sequence number for the duration of the walk.  While any
sequence is live, every node-level tree change anywhere in the filesystem
is recorded in the tree mod log, which is allocated from unreclaimable
slab and trimmed only below the oldest live sequence.  A walk of a
heavily shared block group can therefore hold the log open long enough
for it to consume most of a machine's memory, while the walking threads
sit behind the commit thread.

BTRFS_LOGICAL_INO_ARGS_COMMIT_ROOT asks for the answer as of the last
committed transaction instead.  No sequence is taken, so nothing is
logged, and the walk does not contend with the commit thread for tree
locks or delayed ref head mutexes.  The results are otherwise identical,
up to one transaction stale.

Kernels without the flag reject it with EINVAL before starting the walk,
exactly as they reject any undefined flag bit, so do_ioctl_nothrow()
retries once without it and remembers not to set it again for the life
of the process.  Callers can therefore set the flag unconditionally;
commit_root_supported() only reports whether a rejection has happened.

Assisted-by: Claude-Code:claude-opus-5
include/crucible/btrfs.h
include/crucible/fs.h
lib/fs.cc

index d247db18bee7a43c0b1e5275a0b84fa4bfedeb4e..158ef617d0995daa63c42a9d0efaddf955ec6d65 100644 (file)
@@ -236,6 +236,13 @@ enum btrfs_compression_type {
        #define BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET (1ULL << 0)
 #endif
 
+#ifndef BTRFS_LOGICAL_INO_ARGS_COMMIT_ROOT
+       /* Resolve against the commit roots:  the answer is as of the last
+          committed transaction instead of the running one.  Kernels without
+          this flag reject it with EINVAL, like any other undefined flag bit. */
+       #define BTRFS_LOGICAL_INO_ARGS_COMMIT_ROOT (1ULL << 1)
+#endif
+
 #ifndef BTRFS_FS_INFO_FLAG_CSUM_INFO
        /* Request information about checksum type and size */
        #define BTRFS_FS_INFO_FLAG_CSUM_INFO                    (1 << 0)
index 5bbaa9ba9baf27b811a1153fa06b27f52a7961c5..e900d606fa60358fcb7fafb38256090c093ea879 100644 (file)
@@ -90,6 +90,12 @@ namespace crucible {
                void do_ioctl(int fd);
                bool do_ioctl_nothrow(int fd);
 
+               /// True unless a kernel in this process has already rejected
+               /// BTRFS_LOGICAL_INO_ARGS_COMMIT_ROOT with EINVAL.  The flag may
+               /// always be set:  it is dropped automatically after the first
+               /// rejection, and this only reports whether that has happened.
+               static bool commit_root_supported();
+
                struct BtrfsInodeOffsetRootSpan {
                        using iterator = BtrfsInodeOffsetRoot*;
                        using const_iterator = const BtrfsInodeOffsetRoot*;
index 45c0d8413331cdfba3e2a480443e70484bb146e0..79e0d6bef23963a846b965047dea66f982c07d4c 100644 (file)
--- a/lib/fs.cc
+++ b/lib/fs.cc
@@ -345,62 +345,96 @@ namespace crucible {
                m_container_size = new_size;
        }
 
+       /// Set when a kernel rejects BTRFS_LOGICAL_INO_ARGS_COMMIT_ROOT with
+       /// EINVAL.  A kernel will not learn the flag while this process runs,
+       /// so once that happens the flag is dropped from every later call.
+       /// Raced like bili_version below, and harmlessly:  the worst case is
+       /// one extra rejected ioctl per racing thread.
+       static bool bili_commit_root_unsupported = false;
+
        bool
-       BtrfsIoctlLogicalInoArgs::do_ioctl_nothrow(int fd)
+       BtrfsIoctlLogicalInoArgs::commit_root_supported()
        {
-               btrfs_ioctl_logical_ino_args args = (btrfs_ioctl_logical_ino_args) {
-                       .logical = m_logical,
-                       .size = m_container_size,
-                       .inodes = reinterpret_cast<uintptr_t>(m_container.prepare(m_container_size)),
-               };
-               // We are still supporting building with old headers that don't have .flags yet
-               *(&args.reserved[0] + 3) = m_flags;
-
-               btrfs_ioctl_logical_ino_args *const p = &args;
-
-               m_iors.clear();
+               return !bili_commit_root_unsupported;
+       }
 
+       bool
+       BtrfsIoctlLogicalInoArgs::do_ioctl_nothrow(int fd)
+       {
                static unsigned long bili_version = 0;
 
-               if (get_flags() == 0) {
-                       // Could use either V1 or V2
-                       if (bili_version) {
-                               // We tested both versions and came to a decision
-                               if (ioctl(fd, bili_version, p)) {
-                                       return false;
-                               }
-                       }  else {
-                               // Try V2
-                               if (ioctl(fd, BTRFS_IOC_LOGICAL_INO_V2, p)) {
-                                       // V2 failed, try again with V1
-                                       if (ioctl(fd, BTRFS_IOC_LOGICAL_INO, p)) {
+               uint64_t flags = m_flags;
+               if (bili_commit_root_unsupported) {
+                       flags &= ~BTRFS_LOGICAL_INO_ARGS_COMMIT_ROOT;
+               }
+
+               // A kernel that does not implement COMMIT_ROOT rejects it the same
+               // way it rejects any undefined flag bit:  EINVAL, before the walk
+               // starts.  Retry once without the flag, and remember not to ask
+               // again.
+               while (true) {
+                       btrfs_ioctl_logical_ino_args args = (btrfs_ioctl_logical_ino_args) {
+                               .logical = m_logical,
+                               .size = m_container_size,
+                               .inodes = reinterpret_cast<uintptr_t>(m_container.prepare(m_container_size)),
+                       };
+                       // We are still supporting building with old headers that don't have .flags yet
+                       *(&args.reserved[0] + 3) = flags;
+
+                       btrfs_ioctl_logical_ino_args *const p = &args;
+
+                       m_iors.clear();
+
+                       bool ok = false;
+
+                       if (flags == 0) {
+                               // Could use either V1 or V2
+                               if (bili_version) {
+                                       // We tested both versions and came to a decision
+                                       ok = !ioctl(fd, bili_version, p);
+                               }  else {
+                                       // Try V2
+                                       if (ioctl(fd, BTRFS_IOC_LOGICAL_INO_V2, p)) {
+                                               // V2 failed, try again with V1
+                                               if (!ioctl(fd, BTRFS_IOC_LOGICAL_INO, p)) {
+                                                       // V1 and V2 both tested with same arguments, V1 OK, and V2 failed
+                                                       bili_version = BTRFS_IOC_LOGICAL_INO;
+                                                       ok = true;
+                                               }
                                                // both V1 and V2 failed, doesn't tell us which one to choose
-                                               return false;
+                                       } else {
+                                               // V2 succeeded, don't use V1 any more
+                                               bili_version = BTRFS_IOC_LOGICAL_INO_V2;
+                                               ok = true;
                                        }
-                                       // V1 and V2 both tested with same arguments, V1 OK, and V2 failed
-                                       bili_version = BTRFS_IOC_LOGICAL_INO;
-                               } else {
-                                       // V2 succeeded, don't use V1 any more
+                               }
+                       } else {
+                               // Flags/size require a V2 feature, no fallback to V1 possible
+                               if (!ioctl(fd, BTRFS_IOC_LOGICAL_INO_V2, p)) {
+                                       // V2 succeeded so we don't need to probe any more
                                        bili_version = BTRFS_IOC_LOGICAL_INO_V2;
+                                       ok = true;
                                }
                        }
-               } else {
-                       // Flags/size require a V2 feature, no fallback to V1 possible
-                       if (ioctl(fd, BTRFS_IOC_LOGICAL_INO_V2, p)) {
+
+                       if (!ok) {
+                               if ((flags & BTRFS_LOGICAL_INO_ARGS_COMMIT_ROOT) && errno == EINVAL) {
+                                       bili_commit_root_unsupported = true;
+                                       flags &= ~BTRFS_LOGICAL_INO_ARGS_COMMIT_ROOT;
+                                       continue;
+                               }
                                return false;
                        }
-                       // V2 succeeded so we don't need to probe any more
-                       bili_version = BTRFS_IOC_LOGICAL_INO_V2;
-               }
 
-               btrfs_data_container *const bdc = reinterpret_cast<btrfs_data_container *>(p->inodes);
-               BtrfsInodeOffsetRoot *const ior_iter = reinterpret_cast<BtrfsInodeOffsetRoot *>(bdc->val);
+                       btrfs_data_container *const bdc = reinterpret_cast<btrfs_data_container *>(p->inodes);
+                       BtrfsInodeOffsetRoot *const ior_iter = reinterpret_cast<BtrfsInodeOffsetRoot *>(bdc->val);
 
-               // elem_cnt counts uint64_t, but BtrfsInodeOffsetRoot is 3x uint64_t
-               THROW_CHECK1(runtime_error, bdc->elem_cnt, bdc->elem_cnt % 3 == 0);
-               m_iors.m_begin = ior_iter;
-               m_iors.m_end = ior_iter + bdc->elem_cnt / 3;
-               return true;
+                       // elem_cnt counts uint64_t, but BtrfsInodeOffsetRoot is 3x uint64_t
+                       THROW_CHECK1(runtime_error, bdc->elem_cnt, bdc->elem_cnt % 3 == 0);
+                       m_iors.m_begin = ior_iter;
+                       m_iors.m_end = ior_iter + bdc->elem_cnt / 3;
+                       return true;
+               }
        }
 
        void