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