| 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/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_SH |
| 19 | #define HAVE_FLOCK 1 |
| 20 | #else |
| 21 | #define HAVE_FLOCK 0 |
| 22 | #endif |
| 23 | #endif |
| 24 | |
| 25 | #if __has_include(<fcntl.h>) |
| 26 | #include <fcntl.h> |
| 27 | #endif |
| 28 | |
| 29 | #if __has_include(<sys/mount.h>) |
| 30 | #include <sys/mount.h> // statfs |
| 31 | #endif |
| 32 | |
| 33 | #ifdef __APPLE__ |
| 34 | #if __has_include(<sys/sysctl.h>) |
| 35 | #include <sys/sysctl.h> |
| 36 | #endif |
| 37 | #endif |
| 38 | |
| 39 | using namespace llvm; |
| 40 | |
| 41 | static uint64_t OnDiskCASMaxMappingSize = 0; |
| 42 | |
| 43 | Expected<std::optional<uint64_t>> cas::ondisk::getOverriddenMaxMappingSize() { |
| 44 | static std::once_flag Flag; |
| 45 | Error Err = Error::success(); |
| 46 | std::call_once(once&: Flag, f: [&Err] { |
| 47 | ErrorAsOutParameter EAO(&Err); |
| 48 | constexpr const char *EnvVar = "LLVM_CAS_MAX_MAPPING_SIZE" ; |
| 49 | auto Value = sys::Process::GetEnv(name: EnvVar); |
| 50 | if (!Value) |
| 51 | return; |
| 52 | |
| 53 | uint64_t Size; |
| 54 | if (StringRef(*Value).getAsInteger(/*auto*/ Radix: 0, Result&: Size)) |
| 55 | Err = createStringError(EC: inconvertibleErrorCode(), |
| 56 | Fmt: "invalid value for %s: expected integer" , Vals: EnvVar); |
| 57 | OnDiskCASMaxMappingSize = Size; |
| 58 | }); |
| 59 | |
| 60 | if (Err) |
| 61 | return std::move(Err); |
| 62 | |
| 63 | if (OnDiskCASMaxMappingSize == 0) |
| 64 | return std::nullopt; |
| 65 | |
| 66 | return OnDiskCASMaxMappingSize; |
| 67 | } |
| 68 | |
| 69 | void cas::ondisk::setMaxMappingSize(uint64_t Size) { |
| 70 | OnDiskCASMaxMappingSize = Size; |
| 71 | } |
| 72 | |
| 73 | std::error_code cas::ondisk::lockFileThreadSafe(int FD, |
| 74 | sys::fs::LockKind Kind) { |
| 75 | #if HAVE_FLOCK |
| 76 | if (flock(fd: FD, operation: Kind == sys::fs::LockKind::Exclusive ? LOCK_EX : LOCK_SH) == 0) |
| 77 | return std::error_code(); |
| 78 | return std::error_code(errno, std::generic_category()); |
| 79 | #elif defined(_WIN32) |
| 80 | // On Windows this implementation is thread-safe. |
| 81 | return sys::fs::lockFile(FD, Kind); |
| 82 | #else |
| 83 | return make_error_code(std::errc::no_lock_available); |
| 84 | #endif |
| 85 | } |
| 86 | |
| 87 | std::error_code cas::ondisk::unlockFileThreadSafe(int FD) { |
| 88 | #if HAVE_FLOCK |
| 89 | if (flock(fd: FD, LOCK_UN) == 0) |
| 90 | return std::error_code(); |
| 91 | return std::error_code(errno, std::generic_category()); |
| 92 | #elif defined(_WIN32) |
| 93 | // On Windows this implementation is thread-safe. |
| 94 | return sys::fs::unlockFile(FD); |
| 95 | #else |
| 96 | return make_error_code(std::errc::no_lock_available); |
| 97 | #endif |
| 98 | } |
| 99 | |
| 100 | std::error_code |
| 101 | cas::ondisk::tryLockFileThreadSafe(int FD, std::chrono::milliseconds Timeout, |
| 102 | sys::fs::LockKind Kind) { |
| 103 | #if HAVE_FLOCK |
| 104 | auto Start = std::chrono::steady_clock::now(); |
| 105 | auto End = Start + Timeout; |
| 106 | do { |
| 107 | if (flock(fd: FD, operation: (Kind == sys::fs::LockKind::Exclusive ? LOCK_EX : LOCK_SH) | |
| 108 | LOCK_NB) == 0) |
| 109 | return std::error_code(); |
| 110 | int Error = errno; |
| 111 | if (Error == EWOULDBLOCK) { |
| 112 | if (Timeout.count() == 0) |
| 113 | break; |
| 114 | // Match sys::fs::tryLockFile, which sleeps for 1 ms per attempt. |
| 115 | std::this_thread::sleep_for(rtime: std::chrono::milliseconds(1)); |
| 116 | continue; |
| 117 | } |
| 118 | return std::error_code(Error, std::generic_category()); |
| 119 | } while (std::chrono::steady_clock::now() < End); |
| 120 | return make_error_code(e: std::errc::no_lock_available); |
| 121 | #elif defined(_WIN32) |
| 122 | // On Windows this implementation is thread-safe. |
| 123 | return sys::fs::tryLockFile(FD, Timeout, Kind); |
| 124 | #else |
| 125 | return make_error_code(std::errc::no_lock_available); |
| 126 | #endif |
| 127 | } |
| 128 | |
| 129 | Expected<size_t> cas::ondisk::preallocateFileTail(int FD, size_t CurrentSize, |
| 130 | size_t NewSize) { |
| 131 | auto CreateError = [&](std::error_code EC) -> Expected<size_t> { |
| 132 | if (EC == std::errc::not_supported) |
| 133 | // Ignore ENOTSUP in case the filesystem cannot preallocate. |
| 134 | return NewSize; |
| 135 | #if defined(HAVE_POSIX_FALLOCATE) |
| 136 | if (EC == std::errc::invalid_argument && CurrentSize < NewSize && // len > 0 |
| 137 | NewSize < std::numeric_limits<off_t>::max()) // 0 <= offset, len < max |
| 138 | // Prior to 2024, POSIX required EINVAL for cases that should be ENOTSUP, |
| 139 | // so handle it the same as above if it is not one of the other ways to |
| 140 | // get EINVAL. |
| 141 | return NewSize; |
| 142 | #endif |
| 143 | return createStringError(EC, |
| 144 | S: "failed to allocate to CAS file: " + EC.message()); |
| 145 | }; |
| 146 | #if defined(HAVE_POSIX_FALLOCATE) |
| 147 | // Note: posix_fallocate returns its error directly, not via errno. |
| 148 | if (int Err = posix_fallocate(FD, CurrentSize, NewSize - CurrentSize)) |
| 149 | return CreateError(std::error_code(Err, std::generic_category())); |
| 150 | return NewSize; |
| 151 | #elif defined(__APPLE__) |
| 152 | fstore_t FAlloc; |
| 153 | FAlloc.fst_flags = F_ALLOCATEALL; |
| 154 | #if defined(F_ALLOCATEPERSIST) && \ |
| 155 | defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \ |
| 156 | __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 130000 |
| 157 | // F_ALLOCATEPERSIST is introduced in macOS 13. |
| 158 | FAlloc.fst_flags |= F_ALLOCATEPERSIST; |
| 159 | #endif |
| 160 | FAlloc.fst_posmode = F_PEOFPOSMODE; |
| 161 | FAlloc.fst_offset = 0; |
| 162 | FAlloc.fst_length = NewSize - CurrentSize; |
| 163 | FAlloc.fst_bytesalloc = 0; |
| 164 | if (fcntl(FD, F_PREALLOCATE, &FAlloc)) |
| 165 | return CreateError(errnoAsErrorCode()); |
| 166 | assert(CurrentSize + FAlloc.fst_bytesalloc >= NewSize); |
| 167 | return CurrentSize + FAlloc.fst_bytesalloc; |
| 168 | #else |
| 169 | (void)CreateError; // Silence unused variable. |
| 170 | return NewSize; // Pretend it worked. |
| 171 | #endif |
| 172 | } |
| 173 | |
| 174 | bool cas::ondisk::useSmallMappingSize(const Twine &P) { |
| 175 | // Add exceptions to use small database file here. |
| 176 | #if defined(__APPLE__) && __has_include(<sys/mount.h>) |
| 177 | // macOS tmpfs does not support sparse tails. |
| 178 | SmallString<128> PathStorage; |
| 179 | StringRef Path = P.toNullTerminatedStringRef(PathStorage); |
| 180 | struct statfs StatFS; |
| 181 | if (statfs(Path.data(), &StatFS) != 0) |
| 182 | return false; |
| 183 | |
| 184 | if (strcmp(StatFS.f_fstypename, "tmpfs" ) == 0) |
| 185 | return true; |
| 186 | #endif |
| 187 | // Default to use regular database file. |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | Expected<uint64_t> cas::ondisk::getBootTime() { |
| 192 | #ifdef __APPLE__ |
| 193 | #if __has_include(<sys/sysctl.h>) && defined(KERN_BOOTTIME) |
| 194 | struct timeval TV; |
| 195 | size_t TVLen = sizeof(TV); |
| 196 | int KernBoot[2] = {CTL_KERN, KERN_BOOTTIME}; |
| 197 | if (sysctl(KernBoot, 2, &TV, &TVLen, nullptr, 0) < 0) |
| 198 | return createStringError(llvm::errnoAsErrorCode(), |
| 199 | "failed to get boottime" ); |
| 200 | if (TVLen != sizeof(TV)) |
| 201 | return createStringError("sysctl kern.boottime unexpected format" ); |
| 202 | return TV.tv_sec; |
| 203 | #else |
| 204 | return 0; |
| 205 | #endif |
| 206 | #elif defined(__linux__) |
| 207 | // Use the mtime for /proc, which is recreated during system boot. |
| 208 | // We could also read /proc/stat and search for 'btime'. |
| 209 | sys::fs::file_status Status; |
| 210 | if (std::error_code EC = sys::fs::status(path: "/proc" , result&: Status)) |
| 211 | return createFileError(F: "/proc" , EC); |
| 212 | return Status.getLastModificationTime().time_since_epoch().count(); |
| 213 | #else |
| 214 | return 0; |
| 215 | #endif |
| 216 | } |
| 217 | |