182 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "OnDiskCommon.h"10#include "llvm/Support/Error.h"11#include "llvm/Support/FileSystem.h"12#include "llvm/Support/Process.h"13#include <mutex>14#include <thread>15 16#if __has_include(<sys/file.h>)17#include <sys/file.h>18#ifdef LOCK_SH19#define HAVE_FLOCK 120#else21#define HAVE_FLOCK 022#endif23#endif24 25#if __has_include(<fcntl.h>)26#include <fcntl.h>27#endif28 29#if __has_include(<sys/mount.h>)30#include <sys/mount.h> // statfs31#endif32 33using namespace llvm;34 35static uint64_t OnDiskCASMaxMappingSize = 0;36 37Expected<std::optional<uint64_t>> cas::ondisk::getOverriddenMaxMappingSize() {38 static std::once_flag Flag;39 Error Err = Error::success();40 std::call_once(Flag, [&Err] {41 ErrorAsOutParameter EAO(&Err);42 constexpr const char *EnvVar = "LLVM_CAS_MAX_MAPPING_SIZE";43 auto Value = sys::Process::GetEnv(EnvVar);44 if (!Value)45 return;46 47 uint64_t Size;48 if (StringRef(*Value).getAsInteger(/*auto*/ 0, Size))49 Err = createStringError(inconvertibleErrorCode(),50 "invalid value for %s: expected integer", EnvVar);51 OnDiskCASMaxMappingSize = Size;52 });53 54 if (Err)55 return std::move(Err);56 57 if (OnDiskCASMaxMappingSize == 0)58 return std::nullopt;59 60 return OnDiskCASMaxMappingSize;61}62 63void cas::ondisk::setMaxMappingSize(uint64_t Size) {64 OnDiskCASMaxMappingSize = Size;65}66 67std::error_code cas::ondisk::lockFileThreadSafe(int FD,68 sys::fs::LockKind Kind) {69#if HAVE_FLOCK70 if (flock(FD, Kind == sys::fs::LockKind::Exclusive ? LOCK_EX : LOCK_SH) == 0)71 return std::error_code();72 return std::error_code(errno, std::generic_category());73#elif defined(_WIN32)74 // On Windows this implementation is thread-safe.75 return sys::fs::lockFile(FD, Kind);76#else77 return make_error_code(std::errc::no_lock_available);78#endif79}80 81std::error_code cas::ondisk::unlockFileThreadSafe(int FD) {82#if HAVE_FLOCK83 if (flock(FD, LOCK_UN) == 0)84 return std::error_code();85 return std::error_code(errno, std::generic_category());86#elif defined(_WIN32)87 // On Windows this implementation is thread-safe.88 return sys::fs::unlockFile(FD);89#else90 return make_error_code(std::errc::no_lock_available);91#endif92}93 94std::error_code95cas::ondisk::tryLockFileThreadSafe(int FD, std::chrono::milliseconds Timeout,96 sys::fs::LockKind Kind) {97#if HAVE_FLOCK98 auto Start = std::chrono::steady_clock::now();99 auto End = Start + Timeout;100 do {101 if (flock(FD, (Kind == sys::fs::LockKind::Exclusive ? LOCK_EX : LOCK_SH) |102 LOCK_NB) == 0)103 return std::error_code();104 int Error = errno;105 if (Error == EWOULDBLOCK) {106 // Match sys::fs::tryLockFile, which sleeps for 1 ms per attempt.107 std::this_thread::sleep_for(std::chrono::milliseconds(1));108 continue;109 }110 return std::error_code(Error, std::generic_category());111 } while (std::chrono::steady_clock::now() < End);112 return make_error_code(std::errc::no_lock_available);113#elif defined(_WIN32)114 // On Windows this implementation is thread-safe.115 return sys::fs::tryLockFile(FD, Timeout, Kind);116#else117 return make_error_code(std::errc::no_lock_available);118#endif119}120 121Expected<size_t> cas::ondisk::preallocateFileTail(int FD, size_t CurrentSize,122 size_t NewSize) {123 auto CreateError = [&](std::error_code EC) -> Expected<size_t> {124 if (EC == std::errc::not_supported)125 // Ignore ENOTSUP in case the filesystem cannot preallocate.126 return NewSize;127#if defined(HAVE_POSIX_FALLOCATE)128 if (EC == std::errc::invalid_argument && CurrentSize < NewSize && // len > 0129 NewSize < std::numeric_limits<off_t>::max()) // 0 <= offset, len < max130 // Prior to 2024, POSIX required EINVAL for cases that should be ENOTSUP,131 // so handle it the same as above if it is not one of the other ways to132 // get EINVAL.133 return NewSize;134#endif135 return createStringError(EC,136 "failed to allocate to CAS file: " + EC.message());137 };138#if defined(HAVE_POSIX_FALLOCATE)139 // Note: posix_fallocate returns its error directly, not via errno.140 if (int Err = posix_fallocate(FD, CurrentSize, NewSize - CurrentSize))141 return CreateError(std::error_code(Err, std::generic_category()));142 return NewSize;143#elif defined(__APPLE__)144 fstore_t FAlloc;145 FAlloc.fst_flags = F_ALLOCATEALL;146#if defined(F_ALLOCATEPERSIST) && \147 defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \148 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 130000149 // F_ALLOCATEPERSIST is introduced in macOS 13.150 FAlloc.fst_flags |= F_ALLOCATEPERSIST;151#endif152 FAlloc.fst_posmode = F_PEOFPOSMODE;153 FAlloc.fst_offset = 0;154 FAlloc.fst_length = NewSize - CurrentSize;155 FAlloc.fst_bytesalloc = 0;156 if (fcntl(FD, F_PREALLOCATE, &FAlloc))157 return CreateError(errnoAsErrorCode());158 assert(CurrentSize + FAlloc.fst_bytesalloc >= NewSize);159 return CurrentSize + FAlloc.fst_bytesalloc;160#else161 (void)CreateError; // Silence unused variable.162 return NewSize; // Pretend it worked.163#endif164}165 166bool cas::ondisk::useSmallMappingSize(const Twine &P) {167 // Add exceptions to use small database file here.168#if defined(__APPLE__) && __has_include(<sys/mount.h>)169 // macOS tmpfs does not support sparse tails.170 SmallString<128> PathStorage;171 StringRef Path = P.toNullTerminatedStringRef(PathStorage);172 struct statfs StatFS;173 if (statfs(Path.data(), &StatFS) != 0)174 return false;175 176 if (strcmp(StatFS.f_fstypename, "tmpfs") == 0)177 return true;178#endif179 // Default to use regular database file.180 return false;181}182