| 1 | //===- OnDiskCommon.cpp ---------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "OnDiskCommon.h" |
| 10 | #include "llvm/Support/Errno.h" |
| 11 | #include "llvm/Support/Error.h" |
| 12 | #include "llvm/Support/FileSystem.h" |
| 13 | #include "llvm/Support/Path.h" |
| 14 | #include "llvm/Support/Process.h" |
| 15 | #include <chrono> |
| 16 | #include <mutex> |
| 17 | #include <thread> |
| 18 | |
| 19 | #if __has_include(<sys/file.h>) |
| 20 | #include <sys/file.h> |
| 21 | #ifdef LOCK_SH |
| 22 | #define HAVE_FLOCK 1 |
| 23 | #else |
| 24 | #define HAVE_FLOCK 0 |
| 25 | #endif |
| 26 | #endif |
| 27 | |
| 28 | #if __has_include(<fcntl.h>) |
| 29 | #include <fcntl.h> |
| 30 | #endif |
| 31 | |
| 32 | #if __has_include(<sys/mount.h>) |
| 33 | #include <sys/mount.h> // statfs |
| 34 | #endif |
| 35 | |
| 36 | #ifdef __APPLE__ |
| 37 | #if __has_include(<sys/sysctl.h>) |
| 38 | #include <sys/sysctl.h> |
| 39 | #endif |
| 40 | #endif |
| 41 | |
| 42 | #ifdef _WIN32 |
| 43 | #include "llvm/Support/Windows/WindowsSupport.h" |
| 44 | #endif |
| 45 | |
| 46 | using namespace llvm; |
| 47 | |
| 48 | static uint64_t OnDiskCASMaxMappingSize = 0; |
| 49 | |
| 50 | Expected<std::optional<uint64_t>> cas::ondisk::getOverriddenMaxMappingSize() { |
| 51 | static std::once_flag Flag; |
| 52 | Error Err = Error::success(); |
| 53 | std::call_once(once&: Flag, f: [&Err] { |
| 54 | ErrorAsOutParameter EAO(&Err); |
| 55 | constexpr const char *EnvVar = "LLVM_CAS_MAX_MAPPING_SIZE" ; |
| 56 | auto Value = sys::Process::GetEnv(name: EnvVar); |
| 57 | if (!Value) |
| 58 | return; |
| 59 | |
| 60 | uint64_t Size; |
| 61 | if (StringRef(*Value).getAsInteger(/*auto*/ Radix: 0, Result&: Size)) |
| 62 | Err = createStringError(EC: inconvertibleErrorCode(), |
| 63 | Fmt: "invalid value for %s: expected integer" , Vals: EnvVar); |
| 64 | OnDiskCASMaxMappingSize = Size; |
| 65 | }); |
| 66 | |
| 67 | if (Err) |
| 68 | return std::move(Err); |
| 69 | |
| 70 | if (OnDiskCASMaxMappingSize == 0) |
| 71 | return std::nullopt; |
| 72 | |
| 73 | return OnDiskCASMaxMappingSize; |
| 74 | } |
| 75 | |
| 76 | void cas::ondisk::setMaxMappingSize(uint64_t Size) { |
| 77 | OnDiskCASMaxMappingSize = Size; |
| 78 | } |
| 79 | |
| 80 | std::error_code cas::ondisk::lockFileThreadSafe(int FD, |
| 81 | sys::fs::LockKind Kind) { |
| 82 | #if HAVE_FLOCK |
| 83 | #ifdef __APPLE__ |
| 84 | // flock LOCK_SH after an exclusive lock is released may only wake a single |
| 85 | // waiter on Darwin, which can cause very long waits if the first shared |
| 86 | // access is long-running. Use a retry loop so waiters wake automatically. |
| 87 | if (Kind == sys::fs::LockKind::Shared) { |
| 88 | std::chrono::milliseconds Wait(1); |
| 89 | std::chrono::milliseconds MaxWait(20); |
| 90 | while (true) { |
| 91 | std::error_code EC = tryLockFileThreadSafe(FD, {}, Kind); |
| 92 | if (EC != std::errc::no_lock_available) |
| 93 | return EC; |
| 94 | std::this_thread::sleep_for(Wait); |
| 95 | Wait = std::min(2 * Wait, MaxWait); |
| 96 | } |
| 97 | } |
| 98 | // Fallthrough for exclusive lock |
| 99 | assert(Kind == sys::fs::LockKind::Exclusive); |
| 100 | #endif |
| 101 | if (sys::RetryAfterSignal( |
| 102 | Fail: -1, F&: flock, As: FD, |
| 103 | As: Kind == sys::fs::LockKind::Exclusive ? LOCK_EX : LOCK_SH) == 0) |
| 104 | return std::error_code(); |
| 105 | return std::error_code(errno, std::generic_category()); |
| 106 | #elif defined(_WIN32) |
| 107 | // On Windows this implementation is thread-safe. |
| 108 | return sys::fs::lockFile(FD, Kind); |
| 109 | #else |
| 110 | return make_error_code(std::errc::no_lock_available); |
| 111 | #endif |
| 112 | } |
| 113 | |
| 114 | std::error_code cas::ondisk::unlockFileThreadSafe(int FD) { |
| 115 | #if HAVE_FLOCK |
| 116 | if (sys::RetryAfterSignal(Fail: -1, F&: flock, As: FD, LOCK_UN) == 0) |
| 117 | return std::error_code(); |
| 118 | return std::error_code(errno, std::generic_category()); |
| 119 | #elif defined(_WIN32) |
| 120 | // On Windows this implementation is thread-safe. |
| 121 | return sys::fs::unlockFile(FD); |
| 122 | #else |
| 123 | return make_error_code(std::errc::no_lock_available); |
| 124 | #endif |
| 125 | } |
| 126 | |
| 127 | std::error_code |
| 128 | cas::ondisk::tryLockFileThreadSafe(int FD, std::chrono::milliseconds Timeout, |
| 129 | sys::fs::LockKind Kind) { |
| 130 | #if HAVE_FLOCK |
| 131 | auto Start = std::chrono::steady_clock::now(); |
| 132 | auto End = Start + Timeout; |
| 133 | do { |
| 134 | if (sys::RetryAfterSignal( |
| 135 | Fail: -1, F&: flock, As: FD, |
| 136 | As: (Kind == sys::fs::LockKind::Exclusive ? LOCK_EX : LOCK_SH) | |
| 137 | LOCK_NB) == 0) |
| 138 | return std::error_code(); |
| 139 | int Error = errno; |
| 140 | if (Error == EWOULDBLOCK) { |
| 141 | if (Timeout.count() == 0) |
| 142 | break; |
| 143 | // Match sys::fs::tryLockFile, which sleeps for 1 ms per attempt. |
| 144 | std::this_thread::sleep_for(rtime: std::chrono::milliseconds(1)); |
| 145 | continue; |
| 146 | } |
| 147 | return std::error_code(Error, std::generic_category()); |
| 148 | } while (std::chrono::steady_clock::now() < End); |
| 149 | return make_error_code(e: std::errc::no_lock_available); |
| 150 | #elif defined(_WIN32) |
| 151 | // On Windows this implementation is thread-safe. |
| 152 | return sys::fs::tryLockFile(FD, Timeout, Kind); |
| 153 | #else |
| 154 | return make_error_code(std::errc::no_lock_available); |
| 155 | #endif |
| 156 | } |
| 157 | |
| 158 | Expected<size_t> cas::ondisk::preallocateFileTail(int FD, size_t CurrentSize, |
| 159 | size_t NewSize) { |
| 160 | auto CreateError = [&](std::error_code EC) -> Expected<size_t> { |
| 161 | if (EC == std::errc::not_supported) |
| 162 | // Ignore ENOTSUP in case the filesystem cannot preallocate. |
| 163 | return NewSize; |
| 164 | #if defined(HAVE_POSIX_FALLOCATE) |
| 165 | if (EC == std::errc::invalid_argument && CurrentSize < NewSize && // len > 0 |
| 166 | NewSize < std::numeric_limits<off_t>::max()) // 0 <= offset, len < max |
| 167 | // Prior to 2024, POSIX required EINVAL for cases that should be ENOTSUP, |
| 168 | // so handle it the same as above if it is not one of the other ways to |
| 169 | // get EINVAL. |
| 170 | return NewSize; |
| 171 | #endif |
| 172 | return createStringError(EC, |
| 173 | S: "failed to allocate to CAS file: " + EC.message()); |
| 174 | }; |
| 175 | #if defined(HAVE_POSIX_FALLOCATE) |
| 176 | // Note: posix_fallocate returns its error directly, not via errno. |
| 177 | int Err; |
| 178 | do { |
| 179 | Err = posix_fallocate(FD, CurrentSize, NewSize - CurrentSize); |
| 180 | } while (Err == EINTR); |
| 181 | if (Err) |
| 182 | return CreateError(std::error_code(Err, std::generic_category())); |
| 183 | return NewSize; |
| 184 | #elif defined(__APPLE__) |
| 185 | fstore_t FAlloc; |
| 186 | FAlloc.fst_flags = F_ALLOCATEALL; |
| 187 | #if defined(F_ALLOCATEPERSIST) && \ |
| 188 | defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \ |
| 189 | __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 130000 |
| 190 | // F_ALLOCATEPERSIST is introduced in macOS 13. |
| 191 | FAlloc.fst_flags |= F_ALLOCATEPERSIST; |
| 192 | #endif |
| 193 | FAlloc.fst_posmode = F_PEOFPOSMODE; |
| 194 | FAlloc.fst_offset = 0; |
| 195 | FAlloc.fst_length = NewSize - CurrentSize; |
| 196 | FAlloc.fst_bytesalloc = 0; |
| 197 | if (sys::RetryAfterSignal(-1, ::fcntl, FD, F_PREALLOCATE, &FAlloc) == -1) |
| 198 | return CreateError(errnoAsErrorCode()); |
| 199 | assert(CurrentSize + FAlloc.fst_bytesalloc >= NewSize); |
| 200 | return CurrentSize + FAlloc.fst_bytesalloc; |
| 201 | #else |
| 202 | (void)CreateError; // Silence unused variable. |
| 203 | return NewSize; // Pretend it worked. |
| 204 | #endif |
| 205 | } |
| 206 | |
| 207 | bool cas::ondisk::useSmallMappingSize(const Twine &P) { |
| 208 | // Add exceptions to use small database file here. |
| 209 | #if defined(__APPLE__) && __has_include(<sys/mount.h>) |
| 210 | // macOS tmpfs does not support sparse tails. |
| 211 | SmallString<128> PathStorage; |
| 212 | StringRef Path = P.toNullTerminatedStringRef(PathStorage); |
| 213 | struct statfs StatFS; |
| 214 | if (statfs(Path.data(), &StatFS) != 0) |
| 215 | return false; |
| 216 | |
| 217 | if (strcmp(StatFS.f_fstypename, "tmpfs" ) == 0) |
| 218 | return true; |
| 219 | #endif |
| 220 | // Default to use regular database file. |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | Expected<uint64_t> cas::ondisk::getBootTime() { |
| 225 | #ifdef __APPLE__ |
| 226 | #if __has_include(<sys/sysctl.h>) && defined(KERN_BOOTTIME) |
| 227 | struct timeval TV; |
| 228 | size_t TVLen = sizeof(TV); |
| 229 | int KernBoot[2] = {CTL_KERN, KERN_BOOTTIME}; |
| 230 | if (sysctl(KernBoot, 2, &TV, &TVLen, nullptr, 0) < 0) |
| 231 | return createStringError(llvm::errnoAsErrorCode(), |
| 232 | "failed to get boottime" ); |
| 233 | if (TVLen != sizeof(TV)) |
| 234 | return createStringError("sysctl kern.boottime unexpected format" ); |
| 235 | return TV.tv_sec; |
| 236 | #else |
| 237 | return 0; |
| 238 | #endif |
| 239 | #elif defined(__linux__) |
| 240 | // Use the mtime for /proc, which is recreated during system boot. |
| 241 | // We could also read /proc/stat and search for 'btime'. |
| 242 | sys::fs::file_status Status; |
| 243 | if (std::error_code EC = sys::fs::status(path: "/proc" , result&: Status)) |
| 244 | return createFileError(F: "/proc" , EC); |
| 245 | return Status.getLastModificationTime().time_since_epoch().count(); |
| 246 | #elif defined(_WIN32) |
| 247 | // Compute it from the current time and the time since boot, which includes |
| 248 | // time spent asleep. |
| 249 | auto Uptime = std::chrono::milliseconds(GetTickCount64()); |
| 250 | auto Boot = std::chrono::system_clock::now() - Uptime; |
| 251 | return std::chrono::duration_cast<std::chrono::seconds>( |
| 252 | Boot.time_since_epoch()) |
| 253 | .count(); |
| 254 | #else |
| 255 | return 0; |
| 256 | #endif |
| 257 | } |
| 258 | |
| 259 | Expected<StringRef> |
| 260 | cas::ondisk::UniqueTempFile::createAndCopyFrom(StringRef ParentPath, |
| 261 | StringRef CopyFromPath) { |
| 262 | // \c clonefile requires that the destination path doesn't exist. We create |
| 263 | // a "placeholder" temporary file, then modify its path a bit and use that |
| 264 | // for \c clonefile to write to. |
| 265 | // FIXME: Instead of creating a dummy file, add a new file system API for |
| 266 | // copying to a unique path that can loop while checking EEXIST. |
| 267 | SmallString<256> Model; |
| 268 | Model += ParentPath; |
| 269 | sys::path::append(path&: Model, a: "%%%%%%%.tmp" ); |
| 270 | if (std::error_code EC = sys::fs::createUniqueFile(Model, ResultPath&: UniqueTmpPath)) |
| 271 | return createFileError(F: Model, EC); |
| 272 | TmpPath = UniqueTmpPath; |
| 273 | TmpPath += ".tmp" ; // modify so that there's no file at that path. |
| 274 | // \c copy_file will use \c clonefile when applicable. |
| 275 | if (std::error_code EC = sys::fs::copy_file(From: CopyFromPath, To: TmpPath)) |
| 276 | return createFileError(F: TmpPath, EC); |
| 277 | |
| 278 | return TmpPath; |
| 279 | } |
| 280 | |
| 281 | Error cas::ondisk::UniqueTempFile::renameTo(StringRef RenameToPath) { |
| 282 | if (std::error_code EC = sys::fs::rename(from: TmpPath, to: RenameToPath)) |
| 283 | return createFileError(F: RenameToPath, EC); |
| 284 | TmpPath.clear(); |
| 285 | return Error::success(); |
| 286 | } |
| 287 | |
| 288 | cas::ondisk::UniqueTempFile::~UniqueTempFile() { |
| 289 | if (!TmpPath.empty()) |
| 290 | sys::fs::remove(path: TmpPath); |
| 291 | if (!UniqueTmpPath.empty()) |
| 292 | sys::fs::remove(path: UniqueTmpPath); |
| 293 | } |
| 294 | |