|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This software may be used and distributed according to the terms of the |
| 5 | + * GNU General Public License version 2. |
| 6 | + */ |
| 7 | + |
| 8 | +#include "eden/fs/utils/MountHealthCheck.h" |
| 9 | + |
| 10 | +#include <atomic> |
| 11 | +#include <cerrno> |
| 12 | +#include <optional> |
| 13 | +#include <string> |
| 14 | + |
| 15 | +#include <folly/String.h> |
| 16 | +#include <folly/logging/xlog.h> |
| 17 | +#include <folly/portability/SysStat.h> |
| 18 | +#include <folly/portability/Unistd.h> |
| 19 | + |
| 20 | +#ifdef __linux__ |
| 21 | +#include "eden/common/utils/FSDetect.h" |
| 22 | +#include "eden/fs/utils/MountInfoTable.h" |
| 23 | +#endif |
| 24 | + |
| 25 | +namespace facebook::eden { |
| 26 | +namespace { |
| 27 | + |
| 28 | +bool isNotConnectedErrno(int err) { |
| 29 | + return err == ENOTCONN || err == ENXIO; |
| 30 | +} |
| 31 | + |
| 32 | +#ifdef __linux__ |
| 33 | +bool isEdenMountInfo( |
| 34 | + const std::string& mountSource, |
| 35 | + const std::string& fsType) { |
| 36 | + return is_edenfs_fs_type(mountSource) || is_edenfs_fs_type(fsType) || |
| 37 | + fsType == "fuse.edenfs"; |
| 38 | +} |
| 39 | +#endif |
| 40 | + |
| 41 | +std::optional<int> lstatError(const std::string& path) { |
| 42 | + struct stat st{}; |
| 43 | + if (lstat(path.c_str(), &st) == 0) { |
| 44 | + return std::nullopt; |
| 45 | + } |
| 46 | + return errno; |
| 47 | +} |
| 48 | + |
| 49 | +std::optional<EdenMountHealthCheckIssue> issueForPathAccessError( |
| 50 | + const std::string& path, |
| 51 | + int err) { |
| 52 | + if (isNotConnectedErrno(err)) { |
| 53 | + return EdenMountHealthCheckIssue{ |
| 54 | + EdenMountHealthIssueReason::DaemonRunningMountNotConnected, |
| 55 | + path + ": " + folly::errnoStr(err)}; |
| 56 | + } |
| 57 | + if (err == ETIMEDOUT) { |
| 58 | + return EdenMountHealthCheckIssue{ |
| 59 | + EdenMountHealthIssueReason::DaemonRunningMountTimedOut, |
| 60 | + path + ": " + folly::errnoStr(err)}; |
| 61 | + } |
| 62 | + |
| 63 | + XLOGF( |
| 64 | + WARN, |
| 65 | + "Unexpected EdenFS mount health check lstat error for {}: {}", |
| 66 | + path, |
| 67 | + folly::errnoStr(err)); |
| 68 | + return EdenMountHealthCheckIssue{ |
| 69 | + EdenMountHealthIssueReason::DaemonRunningMountAccessError, |
| 70 | + path + ": " + folly::errnoStr(err)}; |
| 71 | +} |
| 72 | + |
| 73 | +std::optional<bool> hasEdenMountInKernelMountTable( |
| 74 | + const std::string& mountPath) { |
| 75 | +#ifdef __linux__ |
| 76 | + MountInfoOptions options; |
| 77 | + options.includeMountSource = true; |
| 78 | + auto result = getMountInfoForPath(mountPath.c_str(), options); |
| 79 | + if (result.hasError()) { |
| 80 | + XLOGF( |
| 81 | + WARN, |
| 82 | + "Failed to read kernel mount table for {}: {}", |
| 83 | + mountPath, |
| 84 | + folly::errnoStr(result.error())); |
| 85 | + // The mount table check is inconclusive. Continue with the .eden probes |
| 86 | + // instead of reporting a mount-health issue from the failed lookup itself. |
| 87 | + return std::nullopt; |
| 88 | + } |
| 89 | + |
| 90 | + const auto& mountInfo = result.value(); |
| 91 | + if (!mountInfo.has_value()) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + return isEdenMountInfo(mountInfo->mountSource, mountInfo->fsType); |
| 95 | +#else |
| 96 | + (void)mountPath; |
| 97 | + return true; |
| 98 | +#endif |
| 99 | +} |
| 100 | + |
| 101 | +std::string makeExpectedMissingPath(const std::string& mountPath) { |
| 102 | + // Use a fresh negative lookup path each time so kernel/FUSE negative-entry |
| 103 | + // caching does not hide whether EdenFS can answer a lookup now. |
| 104 | + static std::atomic<uint64_t> nextHealthCheckPathId{0}; |
| 105 | + auto id = nextHealthCheckPathId.fetch_add(1, std::memory_order_relaxed); |
| 106 | + return mountPath + "/.eden/edenfs-mount-health-" + std::to_string(id); |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace |
| 110 | + |
| 111 | +std::string_view edenMountHealthIssueReasonString( |
| 112 | + EdenMountHealthIssueReason reason) { |
| 113 | + switch (reason) { |
| 114 | + case EdenMountHealthIssueReason::DaemonRunningKernelMountMissing: |
| 115 | + return "daemon_running_kernel_mount_missing"; |
| 116 | + case EdenMountHealthIssueReason::DaemonRunningDotEdenMissing: |
| 117 | + return "daemon_running_dot_eden_missing"; |
| 118 | + case EdenMountHealthIssueReason::DaemonRunningMountNotConnected: |
| 119 | + return "daemon_running_mount_not_connected"; |
| 120 | + case EdenMountHealthIssueReason::DaemonRunningMountTimedOut: |
| 121 | + return "daemon_running_mount_timed_out"; |
| 122 | + case EdenMountHealthIssueReason::DaemonRunningMountAccessError: |
| 123 | + return "daemon_running_mount_access_error"; |
| 124 | + } |
| 125 | + return "unknown"; |
| 126 | +} |
| 127 | + |
| 128 | +std::optional<EdenMountHealthCheckIssue> checkRunningEdenMountHealth( |
| 129 | + const std::string& mountPath) { |
| 130 | + auto hasKernelMount = hasEdenMountInKernelMountTable(mountPath); |
| 131 | + if (hasKernelMount.has_value() && !hasKernelMount.value()) { |
| 132 | + return EdenMountHealthCheckIssue{ |
| 133 | + EdenMountHealthIssueReason::DaemonRunningKernelMountMissing, |
| 134 | + "EdenFS mount is missing from the kernel mount table"}; |
| 135 | + } |
| 136 | + // If the mount table lookup failed, keep going. The path probes below can |
| 137 | + // still identify a disconnected, missing, or hanging Eden mount. |
| 138 | + |
| 139 | + auto dotEdenPath = mountPath + "/.eden"; |
| 140 | + auto dotEdenError = lstatError(dotEdenPath); |
| 141 | + if (dotEdenError.has_value()) { |
| 142 | + auto err = dotEdenError.value(); |
| 143 | + if (err == ENOENT) { |
| 144 | + return EdenMountHealthCheckIssue{ |
| 145 | + EdenMountHealthIssueReason::DaemonRunningDotEdenMissing, |
| 146 | + dotEdenPath + ": " + folly::errnoStr(err)}; |
| 147 | + } |
| 148 | + return issueForPathAccessError(dotEdenPath, err); |
| 149 | + } |
| 150 | + |
| 151 | + // Since .eden was readable, probe a child path that should not exist. A |
| 152 | + // healthy mount should answer ENOENT; disconnected or hanging mounts can |
| 153 | + // surface as ENOTCONN/ENXIO/ETIMEDOUT. |
| 154 | + auto expectedMissingPath = makeExpectedMissingPath(mountPath); |
| 155 | + auto expectedMissingError = lstatError(expectedMissingPath); |
| 156 | + if (!expectedMissingError.has_value()) { |
| 157 | + // The probe path unexpectedly exists, so this check is inconclusive. |
| 158 | + return std::nullopt; |
| 159 | + } |
| 160 | + |
| 161 | + auto err = expectedMissingError.value(); |
| 162 | + if (err == ENOENT) { |
| 163 | + // The mount handled lookup normally and reported the probe path missing. |
| 164 | + return std::nullopt; |
| 165 | + } |
| 166 | + return issueForPathAccessError(expectedMissingPath, err); |
| 167 | +} |
| 168 | + |
| 169 | +} // namespace facebook::eden |
0 commit comments