]> git.hungrycats.org Git - bees/commitdiff
fdcache: throw on open errors other than ENOENT
authorZygo Blaxell <bees@furryterror.org>
Sat, 10 Oct 2026 23:55:27 +0000 (19:55 -0400)
committerZygo Blaxell <bees@furryterror.org>
Sun, 11 Oct 2026 00:48:00 +0000 (20:48 -0400)
open_root and open_root_ino returned an empty Fd for every failure, so a
caller could not tell a deleted file from an I/O error, and the scan-next
executor skipped a dst ref that failed with EIO instead of aborting the
dst plan.

ENOENT stays a quiet empty Fd: deleted files and subvols are common, and
logging each one would flood the log.  Every other INO_PATHS or openat
error now throws, and so does a validation failure (wrong inode, root, or
device).  For a file with several paths, a failed path is remembered and
the rest are tried; the error is thrown only if none opens.

Assisted-by: Claude-Code:claude-opus-5-5
docs/event-counters.md
src/bees-fdcache.cc

index 853b5549128ebd21b265d8d263ac39f4b570719e..6ed9290994e012d09adee57b28ac86081dc8ddd0 100644 (file)
@@ -293,21 +293,21 @@ The `open` event group consists of operations related to translating `(root, ino
 
  * `open_clear`: The open FD cache was cleared to avoid keeping file descriptors open too long.
  * `open_fail_enoent`: A file could not be opened because it no longer exists (i.e. it was deleted or renamed during the lookup/resolve operations).
- * `open_fail_error`: A file could not be opened for other reasons (e.g. IO error, permission denied, out of resources).
+ * `open_fail_error`: A file could not be opened for other reasons (e.g. IO error, permission denied, out of resources).  The open throws if no other path to the inode opens.
  * `open_file`: A file was successfully opened.  This counts only the `open()` system call, not other reasons why the opened FD might not be usable.
  * `open_hit`: A file was successfully opened and the FD was acceptable.
  * `open_ino_ms`: Total time spent executing the `open()` system call.
  * `open_lookup_empty`: No paths were found for the inode in the `INO_PATHS` ioctl.
  * `open_lookup_enoent`: The `INO_PATHS` ioctl returned ENOENT.
- * `open_lookup_error`: The `INO_PATHS` ioctl returned a different error.
+ * `open_lookup_error`: The `INO_PATHS` ioctl returned a different error.  The open throws.
  * `open_lookup_ok`: The `INO_PATHS` ioctl successfully returned a list of one or more filenames.
  * `open_no_path`: All attempts to open a file by `(root, inode)` pair failed.
  * `open_no_root`: An attempt to open a file by `(root, inode)` pair failed because the `root` could not be opened.
  * `open_root_ms`: Total time spent opening subvol root FDs.
- * `open_wrong_dev`: A FD returned by `open()` did not match the device belonging to the filesystem subvol.
+ * `open_wrong_dev`: A FD returned by `open()` did not match the device belonging to the filesystem subvol.  The open throws.
  * `open_wrong_flags`: A FD returned by `open()` had incompatible flags (`NODATASUM` / `NODATACOW`).
- * `open_wrong_ino`: A FD returned by `open()` did not match the expected inode (i.e. the file was renamed or replaced during the lookup/resolve operations).
- * `open_wrong_root`: A FD returned by `open()` did not match the expected subvol ID (i.e. `root`).
+ * `open_wrong_ino`: A FD returned by `open()` did not match the expected inode (i.e. the file was renamed or replaced during the lookup/resolve operations).  The open throws.
+ * `open_wrong_root`: A FD returned by `open()` did not match the expected subvol ID (i.e. `root`).  The open throws.
 
 pairbackward
 ------------
@@ -506,13 +506,13 @@ The `root` event group consists of operations related to translating a btrfs roo
  * `root_found`: A root FD was successfully opened.
  * `root_notfound`: A root FD could not be opened because all candidate paths could not be opened, or there were no paths available.
  * `root_ok`: A root FD was opened and its correctness verified.
- * `root_open_fail`: A root FD `open()` attempt returned an error.
+ * `root_open_fail`: A root FD `open()` attempt returned an error.  Any error but ENOENT throws.
  * `root_parent_open_fail`: A recursive call to open the parent of a subvol failed.
  * `root_parent_open_ok`: A recursive call to open the parent of a subvol succeeded.
  * `root_parent_open_try`: A recursive call to open the parent of a subvol was attempted.
  * `root_parent_path_empty`: No path could be found to connect a parent root FD to its child.
- * `root_parent_path_fail`: The `INO_PATH` ioctl failed to find a name for a child subvol relative to its parent.
- * `root_parent_path_open_fail`: The `open()` call in a recursive call to open the parent of a subvol returned an error.
+ * `root_parent_path_fail`: The `INO_PATH` ioctl failed to find a name for a child subvol relative to its parent.  Any error but ENOENT throws.
+ * `root_parent_path_open_fail`: The `open()` call in a recursive call to open the parent of a subvol returned an error.  Any error but ENOENT throws.
  * `root_workaround_btrfs_send`: A subvol was determined to be read-only and disabled to implement the btrfs send workaround.
 
 scan
index ed6429f84ce9ee22c33e9f71f4670d391263b3ce..f1c793ccecf30ee2aa6059bf76cf1c8b617d56ed 100644 (file)
@@ -91,10 +91,12 @@ BeesFdCache::open_root_nocache(uint64_t rootid)
                BEESTRACE("dirid " << dirid << " root " << rootid << " INO_PATH");
                BtrfsIoctlInoPathArgs ino(dirid);
                if (!ino.do_ioctl_nothrow(parent_fd)) {
-                       BEESLOGDEBUG("dirid " << dirid
-                               << " inode path lookup failed in parent_fd "
-                               << name_fd(parent_fd) << ": " << strerror(errno));
+                       const auto e = errno;
                        BEESCOUNT(root_parent_path_fail);
+                       if (e != ENOENT) {
+                               THROW_ERRNO_VALUE(e, "dirid " << dirid
+                                       << " inode path lookup in parent_fd " << name_fd(parent_fd));
+                       }
                        return Fd();
                }
                if (ino.m_paths.empty()) {
@@ -111,9 +113,12 @@ BeesFdCache::open_root_nocache(uint64_t rootid)
                        parent_fd
                );
                if (!parent_fd) {
-                       BEESLOGDEBUG("no parent_fd from dirid " << dirid
-                               << " in parent_rootid " << parent_rootid);
+                       const auto e = errno;
                        BEESCOUNT(root_parent_path_open_fail);
+                       if (e != ENOENT) {
+                               THROW_ERRNO_VALUE(e, "open dirid " << dirid
+                                       << " path " << dir_path << " in parent_rootid " << parent_rootid);
+                       }
                        return Fd();
                }
                dir_and_name = dir_path + "/" + name;
@@ -123,9 +128,12 @@ BeesFdCache::open_root_nocache(uint64_t rootid)
        Fd rv(bees_openat(parent_fd, name.c_str(), FLAGS_OPEN_DIR),
                dir_and_name, parent_root_fd);
        if (!rv) {
-               BEESLOGDEBUG("open failed for name " << name << " in parent_fd "
-                       << name_fd(parent_fd) << ": " << strerror(errno));
+               const auto e = errno;
                BEESCOUNT(root_open_fail);
+               if (e != ENOENT) {
+                       THROW_ERRNO_VALUE(e, "open root " << rootid << " name " << name
+                               << " in parent_fd " << name_fd(parent_fd));
+               }
                return rv;
        }
        BEESCOUNT(root_found);
@@ -170,18 +178,19 @@ BeesFdCache::open_root_ino_nocache(uint64_t root, uint64_t ino)
        BEESTRACE("looking up ino " << ino);
        BtrfsIoctlInoPathArgs ipa(ino);
        if (!ipa.do_ioctl_nothrow(root_fd)) {
-               if (errno == ENOENT) {
-                       BEESCOUNT(open_lookup_enoent);
-               } else {
-                       BEESLOGINFO("Lookup root " << root << " ino " << ino
-                               << " failed: " << strerror(errno));
+               const auto e = errno;
+               if (e != ENOENT) {
                        BEESCOUNT(open_lookup_error);
+                       THROW_ERRNO_VALUE(e, "lookup root " << root << " ino " << ino);
                }
+               BEESCOUNT(open_lookup_enoent);
                return Fd();
        }
 
        BEESTRACE("searching paths for root " << root << " ino " << ino);
        Fd rv;
+       int open_errno = 0;
+       string open_errno_path;
        if (ipa.m_paths.empty()) {
                BEESCOUNT(open_lookup_empty);
        }
@@ -194,44 +203,42 @@ BeesFdCache::open_root_ino_nocache(uint64_t root, uint64_t ino)
                rv = Fd(bees_openat(root_fd, file_path.c_str(), FLAGS_OPEN_FILE),
                        file_path, root_fd);
                if (!rv) {
-                       if (errno == ENOENT) {
-                               BEESCOUNT(open_fail_enoent);
-                       } else {
-                               BEESLOGWARN("Could not open path '" << file_path << "' at root "
-                                       << root << " " << name_fd(root_fd) << ": "
-                                       << strerror(errno));
+                       const auto e = errno;
+                       if (e != ENOENT) {
+                               // Another hardlink may still open, so try the rest
+                               // before throwing.
                                BEESCOUNT(open_fail_error);
+                               if (!open_errno) {
+                                       open_errno = e;
+                                       open_errno_path = file_path;
+                               }
+                       } else {
+                               BEESCOUNT(open_fail_enoent);
                        }
                        continue;
                }
 
                Stat file_stat(rv);
                if (file_stat.st_ino != ino) {
-                       BEESLOGWARN("Opening " << name_fd(root_fd) << "/" << file_path
+                       BEESCOUNT(open_wrong_ino);
+                       THROW_ERROR(runtime_error, "opening " << name_fd(root_fd) << "/" << file_path
                                << " found wrong inode " << file_stat.st_ino
                                << " instead of " << ino);
-                       rv = Fd();
-                       BEESCOUNT(open_wrong_ino);
-                       break;
                }
 
                auto file_root = btrfs_get_root_id(rv);
                if (file_root != root) {
-                       BEESLOGWARN("Opening " << name_fd(root_fd) << "/" << file_path
-                               << " found wrong root " << file_root << " instead of " << root);
-                       rv = Fd();
                        BEESCOUNT(open_wrong_root);
-                       break;
+                       THROW_ERROR(runtime_error, "opening " << name_fd(root_fd) << "/" << file_path
+                               << " found wrong root " << file_root << " instead of " << root);
                }
 
                Stat root_stat(root_fd);
                if (root_stat.st_dev != file_stat.st_dev) {
-                       BEESLOGWARN("Opening root " << name_fd(root_fd) << " path "
+                       BEESCOUNT(open_wrong_dev);
+                       THROW_ERROR(runtime_error, "opening root " << name_fd(root_fd) << " path "
                                << file_path << " found path st_dev " << file_stat.st_dev
                                << " but root st_dev is " << root_stat.st_dev);
-                       rv = Fd();
-                       BEESCOUNT(open_wrong_dev);
-                       break;
                }
 
                BEESCOUNT(open_hit);
@@ -239,6 +246,10 @@ BeesFdCache::open_root_ino_nocache(uint64_t root, uint64_t ino)
        }
 
        BEESCOUNT(open_no_path);
+       if (open_errno) {
+               THROW_ERRNO_VALUE(open_errno, "open path '" << open_errno_path << "' at root "
+                       << root << " " << name_fd(root_fd));
+       }
        return Fd();
 }