| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/cache_posix.cc |
| Date: | 2025-11-30 02:35:17 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 284 | 322 | 88.2% |
| Branches: | 168 | 267 | 62.9% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | * | ||
| 4 | * The cache module maintains the local file cache. Files are | ||
| 5 | * staged into the cache by Fetch(). The cache stores files with a name | ||
| 6 | * according to their content hash. | ||
| 7 | * | ||
| 8 | * The procedure is | ||
| 9 | * -# Look in the catalog for content hash | ||
| 10 | * -# If it is in local cache: return file descriptor | ||
| 11 | * -# Otherwise download, store in cache and return fd | ||
| 12 | * | ||
| 13 | * Each running CVMFS instance has to have a separate cache directory. | ||
| 14 | * The local cache directory (directories 00..ff) can be accessed | ||
| 15 | * in parallel to a running CVMFS, i.e. files can be deleted for instance | ||
| 16 | * anytime. However, this will confuse the cache database managed by the lru | ||
| 17 | * module. | ||
| 18 | * | ||
| 19 | * Files are created in txn directory first. At the very latest | ||
| 20 | * point they are renamed into their "real" content hash names atomically by | ||
| 21 | * rename(). This concept is taken over from GROW-FS. | ||
| 22 | * | ||
| 23 | * Identical URLs won't be concurrently downloaded. The first thread performs | ||
| 24 | * the download and informs the other, waiting threads on pipes. | ||
| 25 | */ | ||
| 26 | |||
| 27 | #define __STDC_FORMAT_MACROS | ||
| 28 | |||
| 29 | |||
| 30 | #include "cache_posix.h" | ||
| 31 | |||
| 32 | #include <dirent.h> | ||
| 33 | #include <errno.h> | ||
| 34 | #include <fcntl.h> | ||
| 35 | #include <inttypes.h> | ||
| 36 | #include <pthread.h> | ||
| 37 | #include <sys/stat.h> | ||
| 38 | #include <sys/types.h> | ||
| 39 | #ifndef __APPLE__ | ||
| 40 | #include <sys/statfs.h> | ||
| 41 | #endif | ||
| 42 | #include <unistd.h> | ||
| 43 | |||
| 44 | #include <algorithm> | ||
| 45 | #include <cassert> | ||
| 46 | #include <cstdio> | ||
| 47 | #include <cstdlib> | ||
| 48 | #include <cstring> | ||
| 49 | #include <map> | ||
| 50 | #include <vector> | ||
| 51 | |||
| 52 | #include "crypto/hash.h" | ||
| 53 | #include "crypto/signature.h" | ||
| 54 | #include "directory_entry.h" | ||
| 55 | #include "manifest.h" | ||
| 56 | #include "manifest_fetch.h" | ||
| 57 | #include "network/download.h" | ||
| 58 | #include "quota.h" | ||
| 59 | #include "shortstring.h" | ||
| 60 | #include "statistics.h" | ||
| 61 | #include "util/atomic.h" | ||
| 62 | #include "util/logging.h" | ||
| 63 | #include "util/platform.h" | ||
| 64 | #include "util/posix.h" | ||
| 65 | #include "util/smalloc.h" | ||
| 66 | |||
| 67 | using namespace std; // NOLINT | ||
| 68 | |||
| 69 | namespace { | ||
| 70 | |||
| 71 | /** | ||
| 72 | * A CallGuard object can be placed at the beginning of a function. It counts | ||
| 73 | * the number of so-annotated functions that are in flight. The Drainout() call | ||
| 74 | * will wait until all functions that have been called so far are finished. | ||
| 75 | * | ||
| 76 | * The class is used in order to wait for remaining calls when switching into | ||
| 77 | * the read-only cache mode. | ||
| 78 | */ | ||
| 79 | class CallGuard { | ||
| 80 | public: | ||
| 81 | CallGuard() { | ||
| 82 | const int32_t global_drainout = atomic_read32(&global_drainout_); | ||
| 83 | drainout_ = (global_drainout != 0); | ||
| 84 | if (!drainout_) | ||
| 85 | atomic_inc32(&num_inflight_calls_); | ||
| 86 | } | ||
| 87 | ~CallGuard() { | ||
| 88 | if (!drainout_) | ||
| 89 | atomic_dec32(&num_inflight_calls_); | ||
| 90 | } | ||
| 91 | static void Drainout() { | ||
| 92 | atomic_cas32(&global_drainout_, 0, 1); | ||
| 93 | while (atomic_read32(&num_inflight_calls_) != 0) | ||
| 94 | SafeSleepMs(50); | ||
| 95 | } | ||
| 96 | |||
| 97 | private: | ||
| 98 | bool drainout_; | ||
| 99 | static atomic_int32 global_drainout_; | ||
| 100 | static atomic_int32 num_inflight_calls_; | ||
| 101 | }; | ||
| 102 | atomic_int32 CallGuard::num_inflight_calls_ = 0; | ||
| 103 | atomic_int32 CallGuard::global_drainout_ = 0; | ||
| 104 | |||
| 105 | } // anonymous namespace | ||
| 106 | |||
| 107 | |||
| 108 | //------------------------------------------------------------------------------ | ||
| 109 | |||
| 110 | |||
| 111 | const uint64_t PosixCacheManager::kBigFile = 25 * 1024 * 1024; // 25M | ||
| 112 | |||
| 113 | |||
| 114 | 166 | int PosixCacheManager::AbortTxn(void *txn) { | |
| 115 | 166 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 116 | 166 | LogCvmfs(kLogCache, kLogDebug, "abort %s", transaction->tmp_path.c_str()); | |
| 117 | 166 | close(transaction->fd); | |
| 118 | 166 | const int result = unlink(transaction->tmp_path.c_str()); | |
| 119 | 166 | transaction->~Transaction(); | |
| 120 | 166 | atomic_dec32(&no_inflight_txns_); | |
| 121 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 150 times.
|
166 | if (result == -1) |
| 122 | 16 | return -errno; | |
| 123 | 150 | return 0; | |
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | /** | ||
| 128 | * This should only be used to replace the default NoopQuotaManager by a | ||
| 129 | * PosixQuotaManager. The cache manager takes the ownership of the passed | ||
| 130 | * quota manager. | ||
| 131 | */ | ||
| 132 | 574 | bool PosixCacheManager::AcquireQuotaManager(QuotaManager *quota_mgr) { | |
| 133 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 574 times.
|
574 | if (quota_mgr == NULL) |
| 134 | ✗ | return false; | |
| 135 |
1/2✓ Branch 0 taken 574 times.
✗ Branch 1 not taken.
|
574 | delete quota_mgr_; |
| 136 | 574 | quota_mgr_ = quota_mgr; | |
| 137 | 574 | return true; | |
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | 657 | int PosixCacheManager::Close(int fd) { | |
| 142 |
1/2✓ Branch 0 taken 657 times.
✗ Branch 1 not taken.
|
657 | const int retval = do_refcount_ ? fd_mgr_->Close(fd) : close(fd); |
| 143 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 649 times.
|
657 | if (retval != 0) |
| 144 | 8 | return -errno; | |
| 145 | 649 | return 0; | |
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | 1328 | int PosixCacheManager::CommitTxn(void *txn) { | |
| 150 | 1328 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 151 | int result; | ||
| 152 | 1328 | LogCvmfs(kLogCache, kLogDebug, "commit %s %s", | |
| 153 | transaction->final_path.c_str(), transaction->tmp_path.c_str()); | ||
| 154 | |||
| 155 | 1328 | result = Flush(transaction); | |
| 156 | 1328 | close(transaction->fd); | |
| 157 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1320 times.
|
1328 | if (result < 0) { |
| 158 | 8 | unlink(transaction->tmp_path.c_str()); | |
| 159 | 8 | transaction->~Transaction(); | |
| 160 | 8 | atomic_dec32(&no_inflight_txns_); | |
| 161 | 8 | return result; | |
| 162 | } | ||
| 163 | |||
| 164 | // To support debugging, move files into quarantine on file size mismatch | ||
| 165 |
2/2✓ Branch 0 taken 406 times.
✓ Branch 1 taken 914 times.
|
1320 | if (transaction->size != transaction->expected_size) { |
| 166 | // Allow size to be zero if alien cache, because hadoop-fuse-dfs returns | ||
| 167 | // size zero for a while | ||
| 168 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 394 times.
|
406 | if ((transaction->expected_size != kSizeUnknown) |
| 169 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
12 | && (reports_correct_filesize_ || (transaction->size != 0))) { |
| 170 |
1/3✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
12 | LogCvmfs(kLogCache, kLogDebug | kLogSyslogErr, |
| 171 | "size check failure for %s, expected %lu, got %lu", | ||
| 172 | 24 | transaction->id.ToString().c_str(), transaction->expected_size, | |
| 173 | transaction->size); | ||
| 174 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | CopyPath2Path(transaction->tmp_path, |
| 175 |
2/4✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
|
24 | cache_path_ + "/quarantaine/" + transaction->id.ToString()); |
| 176 | 12 | unlink(transaction->tmp_path.c_str()); | |
| 177 | 12 | transaction->~Transaction(); | |
| 178 | 12 | atomic_dec32(&no_inflight_txns_); | |
| 179 | 12 | return -EIO; | |
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 |
2/2✓ Branch 0 taken 1300 times.
✓ Branch 1 taken 8 times.
|
1308 | if ((transaction->label.flags & kLabelPinned) |
| 184 |
2/2✓ Branch 0 taken 397 times.
✓ Branch 1 taken 903 times.
|
1300 | || (transaction->label.flags & kLabelCatalog)) { |
| 185 | 1215 | const bool retval = quota_mgr_->Pin( | |
| 186 |
1/2✓ Branch 1 taken 405 times.
✗ Branch 2 not taken.
|
405 | transaction->id, transaction->size, transaction->label.GetDescription(), |
| 187 | 405 | (transaction->label.flags & kLabelCatalog)); | |
| 188 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 397 times.
|
405 | if (!retval) { |
| 189 |
1/2✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 | LogCvmfs(kLogCache, kLogDebug, "commit failed: cannot pin %s", |
| 190 | 16 | transaction->id.ToString().c_str()); | |
| 191 | 8 | unlink(transaction->tmp_path.c_str()); | |
| 192 | 8 | transaction->~Transaction(); | |
| 193 | 8 | atomic_dec32(&no_inflight_txns_); | |
| 194 | 8 | return -ENOSPC; | |
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | // Move the temporary file into its final location | ||
| 199 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1292 times.
|
1300 | if (alien_cache_) { |
| 200 | 8 | const int retval = chmod(transaction->tmp_path.c_str(), 0660); | |
| 201 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | assert(retval == 0); |
| 202 | } | ||
| 203 | 1300 | result = Rename(transaction->tmp_path.c_str(), | |
| 204 | transaction->final_path.c_str()); | ||
| 205 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1284 times.
|
1300 | if (result < 0) { |
| 206 | 16 | LogCvmfs(kLogCache, kLogDebug, "commit failed: %s", strerror(errno)); | |
| 207 | 16 | unlink(transaction->tmp_path.c_str()); | |
| 208 |
1/2✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 | if ((transaction->label.flags & kLabelPinned) |
| 209 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
|
16 | || (transaction->label.flags & kLabelCatalog)) { |
| 210 | 8 | quota_mgr_->Remove(transaction->id); | |
| 211 | } | ||
| 212 | } else { | ||
| 213 | // Success, inform quota manager | ||
| 214 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1276 times.
|
1284 | if (transaction->label.flags & kLabelVolatile) { |
| 215 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | quota_mgr_->InsertVolatile(transaction->id, transaction->size, |
| 216 | 16 | transaction->label.GetDescription()); | |
| 217 | 1276 | } else if (!transaction->label.IsCatalog() | |
| 218 |
6/6✓ Branch 0 taken 895 times.
✓ Branch 1 taken 381 times.
✓ Branch 3 taken 887 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 887 times.
✓ Branch 6 taken 389 times.
|
1276 | && !transaction->label.IsPinned()) { |
| 219 |
1/2✓ Branch 1 taken 887 times.
✗ Branch 2 not taken.
|
887 | quota_mgr_->Insert(transaction->id, transaction->size, |
| 220 | 1774 | transaction->label.GetDescription()); | |
| 221 | } | ||
| 222 | } | ||
| 223 | 1300 | transaction->~Transaction(); | |
| 224 | 1300 | atomic_dec32(&no_inflight_txns_); | |
| 225 | 1300 | return result; | |
| 226 | } | ||
| 227 | |||
| 228 | 1831 | bool PosixCacheManager::InitCacheDirectory(const string &cache_path) { | |
| 229 |
1/2✓ Branch 1 taken 1831 times.
✗ Branch 2 not taken.
|
1831 | const FileSystemInfo fs_info = GetFileSystemInfo(cache_path); |
| 230 | |||
| 231 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1821 times.
|
1831 | if (fs_info.type == kFsTypeTmpfs) { |
| 232 | 10 | is_tmpfs_ = true; | |
| 233 | } | ||
| 234 | |||
| 235 |
2/2✓ Branch 0 taken 270 times.
✓ Branch 1 taken 1561 times.
|
1831 | if (alien_cache_) { |
| 236 |
3/4✓ Branch 1 taken 270 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 265 times.
|
270 | if (!MakeCacheDirectories(cache_path, 0770)) { |
| 237 | 5 | return false; | |
| 238 | } | ||
| 239 |
1/2✓ Branch 1 taken 265 times.
✗ Branch 2 not taken.
|
265 | LogCvmfs(kLogCache, kLogDebug | kLogSyslog, |
| 240 | "Cache directory structure created."); | ||
| 241 |
1/3✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 265 times.
|
265 | switch (fs_info.type) { |
| 242 | ✗ | case kFsTypeNFS: | |
| 243 | ✗ | rename_workaround_ = kRenameLink; | |
| 244 | ✗ | LogCvmfs(kLogCache, kLogDebug | kLogSyslog, "Alien cache is on NFS."); | |
| 245 | ✗ | break; | |
| 246 | ✗ | case kFsTypeBeeGFS: | |
| 247 | ✗ | rename_workaround_ = kRenameSamedir; | |
| 248 | ✗ | LogCvmfs(kLogCache, kLogDebug | kLogSyslog, | |
| 249 | "Alien cache is on BeeGFS."); | ||
| 250 | ✗ | break; | |
| 251 | 265 | default: | |
| 252 | 265 | break; | |
| 253 | } | ||
| 254 | } else { | ||
| 255 |
3/4✓ Branch 1 taken 1561 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 1556 times.
|
1561 | if (!MakeCacheDirectories(cache_path, 0700)) |
| 256 | 5 | return false; | |
| 257 | } | ||
| 258 | |||
| 259 | // TODO(jblomer): we might not need to look anymore for cvmfs 2.0 relicts | ||
| 260 |
4/7✓ Branch 1 taken 1821 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1821 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 5 times.
✓ Branch 8 taken 1816 times.
|
1821 | if (FileExists(cache_path + "/cvmfscatalog.cache")) { |
| 261 |
1/2✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
5 | LogCvmfs(kLogCache, kLogDebug | kLogSyslogErr, |
| 262 | "Not mounting on cvmfs 2.0.X cache"); | ||
| 263 | 5 | return false; | |
| 264 | } | ||
| 265 | 1816 | return true; | |
| 266 | } | ||
| 267 | |||
| 268 | 1831 | PosixCacheManager *PosixCacheManager::Create( | |
| 269 | const string &cache_path, const bool alien_cache, | ||
| 270 | const RenameWorkarounds rename_workaround, const bool do_refcount, | ||
| 271 | const bool cleanup_unused_first) { | ||
| 272 | UniquePtr<PosixCacheManager> cache_manager(new PosixCacheManager( | ||
| 273 |
3/6✓ Branch 1 taken 1831 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1831 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1831 times.
✗ Branch 8 not taken.
|
1831 | cache_path, alien_cache, do_refcount, cleanup_unused_first)); |
| 274 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1831 times.
|
1831 | assert(cache_manager.IsValid()); |
| 275 | |||
| 276 | 1831 | cache_manager->rename_workaround_ = rename_workaround; | |
| 277 | |||
| 278 |
1/2✓ Branch 2 taken 1831 times.
✗ Branch 3 not taken.
|
1831 | const bool result_ = cache_manager->InitCacheDirectory(cache_path); |
| 279 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1816 times.
|
1831 | if (!result_) { |
| 280 | 15 | return NULL; | |
| 281 | } | ||
| 282 | |||
| 283 | 1816 | return cache_manager.Release(); | |
| 284 | 1831 | } | |
| 285 | |||
| 286 | |||
| 287 | 1289 | void PosixCacheManager::CtrlTxn(const Label &label, | |
| 288 | const int flags, | ||
| 289 | void *txn) { | ||
| 290 | 1289 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 291 | 1289 | transaction->label = label; | |
| 292 | 1289 | } | |
| 293 | |||
| 294 | |||
| 295 | ✗ | string PosixCacheManager::Describe() { | |
| 296 | ✗ | string msg; | |
| 297 | ✗ | if (do_refcount_) { | |
| 298 | msg = "Refcounting Posix cache manager" | ||
| 299 | "(cache directory: " | ||
| 300 | ✗ | + cache_path_ + ")\n"; | |
| 301 | } else { | ||
| 302 | ✗ | msg = "Posix cache manager (cache directory: " + cache_path_ + ")\n"; | |
| 303 | } | ||
| 304 | ✗ | return msg; | |
| 305 | } | ||
| 306 | |||
| 307 | |||
| 308 | /** | ||
| 309 | * If not refcounting, nothing to do, the kernel keeps the state | ||
| 310 | * of open file descriptors. Return a dummy memory location. | ||
| 311 | */ | ||
| 312 | 5 | void *PosixCacheManager::DoSaveState() { | |
| 313 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (do_refcount_) { |
| 314 |
1/2✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 | SavedState *state = new SavedState(); |
| 315 | 5 | state->fd_mgr = fd_mgr_->Clone(); | |
| 316 | 5 | return state; | |
| 317 | } | ||
| 318 | ✗ | char *c = reinterpret_cast<char *>(smalloc(1)); | |
| 319 | ✗ | *c = kMagicNoRefcount; | |
| 320 | ✗ | return c; | |
| 321 | } | ||
| 322 | |||
| 323 | |||
| 324 | 5 | int PosixCacheManager::DoRestoreState(void *data) { | |
| 325 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | assert(data); |
| 326 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (do_refcount_) { |
| 327 | 5 | SavedState *state = reinterpret_cast<SavedState *>(data); | |
| 328 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (state->magic_number == kMagicRefcount) { |
| 329 | 5 | LogCvmfs(kLogCache, kLogDebug, | |
| 330 | "Restoring refcount cache manager from " | ||
| 331 | "refcounted posix cache manager"); | ||
| 332 | |||
| 333 | 5 | fd_mgr_->AssignFrom(state->fd_mgr.weak_ref()); | |
| 334 | } else { | ||
| 335 | ✗ | LogCvmfs(kLogCache, kLogDebug, | |
| 336 | "Restoring refcount cache manager from " | ||
| 337 | "non-refcounted posix cache manager"); | ||
| 338 | } | ||
| 339 | 5 | return -1; | |
| 340 | } | ||
| 341 | |||
| 342 | ✗ | char *c = reinterpret_cast<char *>(data); | |
| 343 | ✗ | assert(*c == kMagicNoRefcount || *c == kMagicRefcount); | |
| 344 | ✗ | if (*c == kMagicRefcount) { | |
| 345 | ✗ | SavedState *state = reinterpret_cast<SavedState *>(data); | |
| 346 | ✗ | LogCvmfs(kLogCache, kLogDebug, | |
| 347 | "Restoring non-refcount cache manager from " | ||
| 348 | "refcounted posix cache manager - this " | ||
| 349 | " is not possible, keep refcounting."); | ||
| 350 | ✗ | fd_mgr_->AssignFrom(state->fd_mgr.weak_ref()); | |
| 351 | ✗ | do_refcount_ = true; | |
| 352 | } | ||
| 353 | ✗ | return -1; | |
| 354 | } | ||
| 355 | |||
| 356 | |||
| 357 | 5 | bool PosixCacheManager::DoFreeState(void *data) { | |
| 358 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | assert(data); |
| 359 | 5 | SavedState *state = reinterpret_cast<SavedState *>(data); | |
| 360 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (state->magic_number == kMagicRefcount) { |
| 361 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | delete state; |
| 362 | } else { | ||
| 363 | // If not refcounted, the state is the dummy SavedState | ||
| 364 | // of the regular posix cache manager | ||
| 365 | ✗ | free(data); | |
| 366 | } | ||
| 367 | 5 | return true; | |
| 368 | } | ||
| 369 | |||
| 370 | |||
| 371 | 20 | int PosixCacheManager::Dup(int fd) { | |
| 372 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | const int new_fd = do_refcount_ ? fd_mgr_->Dup(fd) : dup(fd); |
| 373 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | if (new_fd < 0) |
| 374 | 10 | return -errno; | |
| 375 | 10 | return new_fd; | |
| 376 | } | ||
| 377 | |||
| 378 | |||
| 379 | 3075 | int PosixCacheManager::Flush(Transaction *transaction) { | |
| 380 |
2/2✓ Branch 0 taken 659 times.
✓ Branch 1 taken 2416 times.
|
3075 | if (transaction->buf_pos == 0) |
| 381 | 659 | return 0; | |
| 382 | 4832 | const int written = write(transaction->fd, transaction->buffer, | |
| 383 | 2416 | transaction->buf_pos); | |
| 384 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 2392 times.
|
2416 | if (written < 0) |
| 385 | 24 | return -errno; | |
| 386 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2392 times.
|
2392 | if (static_cast<unsigned>(written) != transaction->buf_pos) { |
| 387 | ✗ | transaction->buf_pos -= written; | |
| 388 | ✗ | return -EIO; | |
| 389 | } | ||
| 390 | 2392 | transaction->buf_pos = 0; | |
| 391 | 2392 | return 0; | |
| 392 | } | ||
| 393 | |||
| 394 | |||
| 395 | 3294 | inline string PosixCacheManager::GetPathInCache(const shash::Any &id) { | |
| 396 |
2/4✓ Branch 2 taken 3294 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 3294 times.
✗ Branch 6 not taken.
|
6588 | return cache_path_ + "/" + id.MakePathWithoutSuffix(); |
| 397 | } | ||
| 398 | |||
| 399 | |||
| 400 | 892 | int64_t PosixCacheManager::GetSize(int fd) { | |
| 401 | platform_stat64 info; | ||
| 402 | 892 | const int retval = platform_fstat(fd, &info); | |
| 403 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 884 times.
|
892 | if (retval != 0) |
| 404 | 8 | return -errno; | |
| 405 | 884 | return info.st_size; | |
| 406 | } | ||
| 407 | |||
| 408 | |||
| 409 | 1790 | int PosixCacheManager::Open(const LabeledObject &object) { | |
| 410 |
1/2✓ Branch 1 taken 1790 times.
✗ Branch 2 not taken.
|
1790 | const string path = GetPathInCache(object.id); |
| 411 | int result; | ||
| 412 |
1/2✓ Branch 0 taken 1790 times.
✗ Branch 1 not taken.
|
1790 | if (do_refcount_) { |
| 413 |
1/2✓ Branch 2 taken 1790 times.
✗ Branch 3 not taken.
|
1790 | result = fd_mgr_->Open(object.id, path); |
| 414 | } else { | ||
| 415 | ✗ | result = open(path.c_str(), O_RDONLY); | |
| 416 | } | ||
| 417 |
2/2✓ Branch 0 taken 543 times.
✓ Branch 1 taken 1247 times.
|
1790 | if (result >= 0) { |
| 418 |
1/2✓ Branch 2 taken 543 times.
✗ Branch 3 not taken.
|
543 | LogCvmfs(kLogCache, kLogDebug, "hit %s", path.c_str()); |
| 419 | // platform_disable_kcache(result); | ||
| 420 |
1/2✓ Branch 1 taken 543 times.
✗ Branch 2 not taken.
|
543 | quota_mgr_->Touch(object.id); |
| 421 | } else { | ||
| 422 | 1247 | result = -errno; | |
| 423 |
1/2✓ Branch 2 taken 1247 times.
✗ Branch 3 not taken.
|
1247 | LogCvmfs(kLogCache, kLogDebug, "miss %s (%d)", path.c_str(), result); |
| 424 | } | ||
| 425 | 1790 | return result; | |
| 426 | 1790 | } | |
| 427 | |||
| 428 | |||
| 429 | 434 | int PosixCacheManager::OpenFromTxn(void *txn) { | |
| 430 | 434 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 431 | 434 | const int retval = Flush(transaction); | |
| 432 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 426 times.
|
434 | if (retval < 0) |
| 433 | 8 | return retval; | |
| 434 | int fd_rdonly; | ||
| 435 | |||
| 436 |
1/2✓ Branch 0 taken 426 times.
✗ Branch 1 not taken.
|
426 | if (do_refcount_) { |
| 437 |
2/4✓ Branch 4 taken 426 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 426 times.
✗ Branch 8 not taken.
|
426 | fd_rdonly = fd_mgr_->Open(transaction->id, transaction->tmp_path.c_str()); |
| 438 | } else { | ||
| 439 | ✗ | fd_rdonly = open(transaction->tmp_path.c_str(), O_RDONLY); | |
| 440 | } | ||
| 441 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 418 times.
|
426 | if (fd_rdonly == -1) |
| 442 | 8 | return -errno; | |
| 443 | 418 | return fd_rdonly; | |
| 444 | } | ||
| 445 | |||
| 446 | |||
| 447 | 6166 | int64_t PosixCacheManager::Pread(int fd, | |
| 448 | void *buf, | ||
| 449 | uint64_t size, | ||
| 450 | uint64_t offset) { | ||
| 451 | int64_t result; | ||
| 452 | do { | ||
| 453 | 6166 | errno = 0; | |
| 454 | 6166 | result = pread(fd, buf, size, offset); | |
| 455 |
3/4✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6150 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
|
6166 | } while ((result == -1) && (errno == EINTR)); |
| 456 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6150 times.
|
6166 | if (result < 0) |
| 457 | 16 | return -errno; | |
| 458 | 6150 | return result; | |
| 459 | } | ||
| 460 | |||
| 461 | |||
| 462 | 1330 | int PosixCacheManager::Rename(const char *oldpath, const char *newpath) { | |
| 463 | int result; | ||
| 464 |
2/2✓ Branch 0 taken 1315 times.
✓ Branch 1 taken 15 times.
|
1330 | if (rename_workaround_ != kRenameLink) { |
| 465 | 1315 | result = rename(oldpath, newpath); | |
| 466 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 1294 times.
|
1315 | if (result < 0) |
| 467 | 21 | return -errno; | |
| 468 | 1294 | return 0; | |
| 469 | } | ||
| 470 | |||
| 471 | 15 | result = link(oldpath, newpath); | |
| 472 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
|
15 | if (result < 0) { |
| 473 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | if (errno == EEXIST) { |
| 474 | 5 | LogCvmfs(kLogCache, kLogDebug, "%s already existed, ignoring", newpath); | |
| 475 | } else { | ||
| 476 | 5 | return -errno; | |
| 477 | } | ||
| 478 | } | ||
| 479 | 10 | result = unlink(oldpath); | |
| 480 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (result < 0) |
| 481 | ✗ | return -errno; | |
| 482 | 10 | return 0; | |
| 483 | } | ||
| 484 | |||
| 485 | |||
| 486 | /** | ||
| 487 | * Used by the sqlite vfs in order to preload file catalogs into the file system | ||
| 488 | * buffers. | ||
| 489 | * | ||
| 490 | * No-op if the fd is to a file that is on a tmpfs, and so already in page cache | ||
| 491 | */ | ||
| 492 | 430 | int PosixCacheManager::Readahead(int fd) { | |
| 493 | unsigned char *buf[4096]; | ||
| 494 | int nbytes; | ||
| 495 | 430 | uint64_t pos = 0; | |
| 496 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 430 times.
|
430 | if (is_tmpfs()) { |
| 497 | ✗ | return 0; | |
| 498 | } | ||
| 499 | do { | ||
| 500 |
1/2✓ Branch 1 taken 2030 times.
✗ Branch 2 not taken.
|
2030 | nbytes = Pread(fd, buf, 4096, pos); |
| 501 | 2030 | pos += nbytes; | |
| 502 |
2/2✓ Branch 0 taken 1600 times.
✓ Branch 1 taken 430 times.
|
2030 | } while (nbytes == 4096); |
| 503 |
1/2✓ Branch 1 taken 430 times.
✗ Branch 2 not taken.
|
430 | LogCvmfs(kLogCache, kLogDebug, "read-ahead %d, %" PRIu64, fd, pos); |
| 504 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 430 times.
|
430 | if (nbytes < 0) |
| 505 | ✗ | return nbytes; | |
| 506 | 430 | return 0; | |
| 507 | } | ||
| 508 | |||
| 509 | |||
| 510 | 80 | int PosixCacheManager::Reset(void *txn) { | |
| 511 | 80 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 512 | 80 | transaction->buf_pos = 0; | |
| 513 | 80 | transaction->size = 0; | |
| 514 | 80 | int retval = lseek(transaction->fd, 0, SEEK_SET); | |
| 515 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 72 times.
|
80 | if (retval < 0) |
| 516 | 8 | return -errno; | |
| 517 | 72 | retval = ftruncate(transaction->fd, 0); | |
| 518 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
|
72 | if (retval < 0) |
| 519 | ✗ | return -errno; | |
| 520 | 72 | return 0; | |
| 521 | } | ||
| 522 | |||
| 523 | |||
| 524 | 1520 | int PosixCacheManager::StartTxn(const shash::Any &id, | |
| 525 | uint64_t size, | ||
| 526 | void *txn) { | ||
| 527 | 1520 | atomic_inc32(&no_inflight_txns_); | |
| 528 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1512 times.
|
1520 | if (cache_mode_ == kCacheReadOnly) { |
| 529 | 8 | atomic_dec32(&no_inflight_txns_); | |
| 530 | 8 | return -EROFS; | |
| 531 | } | ||
| 532 | |||
| 533 |
2/2✓ Branch 0 taken 1054 times.
✓ Branch 1 taken 458 times.
|
1512 | if (size != kSizeUnknown) { |
| 534 |
3/4✓ Branch 1 taken 1054 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 1046 times.
|
1054 | if (size > quota_mgr_->GetMaxFileSize()) { |
| 535 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | LogCvmfs(kLogCache, kLogDebug, |
| 536 | "file too big for lru cache (%" PRIu64 " " | ||
| 537 | "requested but only %" PRIu64 " bytes free)", | ||
| 538 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | size, quota_mgr_->GetMaxFileSize()); |
| 539 | 8 | atomic_dec32(&no_inflight_txns_); | |
| 540 | 8 | return -ENOSPC; | |
| 541 | } | ||
| 542 | |||
| 543 | // For large files, ensure enough free cache space before writing the chunk | ||
| 544 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1038 times.
|
1046 | if (size > kBigFile) { |
| 545 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | const uint64_t cache_size = quota_mgr_->GetSize(); |
| 546 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | const uint64_t cache_capacity = quota_mgr_->GetCapacity(); |
| 547 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | assert(cache_capacity >= size); |
| 548 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if ((cache_size + size) > cache_capacity) { |
| 549 | 16 | const uint64_t leave_size = std::min(cache_capacity / 2, | |
| 550 | 8 | cache_capacity - size); | |
| 551 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | quota_mgr_->Cleanup(leave_size); |
| 552 | } | ||
| 553 | } | ||
| 554 | } | ||
| 555 | |||
| 556 |
1/2✓ Branch 1 taken 1504 times.
✗ Branch 2 not taken.
|
1504 | string path_in_cache = GetPathInCache(id); |
| 557 |
1/2✓ Branch 2 taken 1504 times.
✗ Branch 3 not taken.
|
1504 | Transaction *transaction = new (txn) Transaction(id, path_in_cache); |
| 558 | |||
| 559 | 1504 | char *template_path = NULL; | |
| 560 | 1504 | unsigned temp_path_len = 0; | |
| 561 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1488 times.
|
1504 | if (rename_workaround_ == kRenameSamedir) { |
| 562 | 16 | temp_path_len = path_in_cache.length() + 6; | |
| 563 | 16 | template_path = reinterpret_cast<char *>(alloca(temp_path_len + 1)); | |
| 564 | 16 | memcpy(template_path, path_in_cache.data(), path_in_cache.length()); | |
| 565 | 16 | memset(template_path + path_in_cache.length(), 'X', 6); | |
| 566 | } else { | ||
| 567 | 1488 | temp_path_len = txn_template_path_.length(); | |
| 568 | 1488 | template_path = reinterpret_cast<char *>(alloca(temp_path_len + 1)); | |
| 569 |
1/2✓ Branch 1 taken 1488 times.
✗ Branch 2 not taken.
|
1488 | memcpy(template_path, &txn_template_path_[0], temp_path_len); |
| 570 | } | ||
| 571 | 1504 | template_path[temp_path_len] = '\0'; | |
| 572 | |||
| 573 |
1/2✓ Branch 1 taken 1504 times.
✗ Branch 2 not taken.
|
1504 | transaction->fd = mkstemp(template_path); |
| 574 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1494 times.
|
1504 | if (transaction->fd == -1) { |
| 575 | 10 | transaction->~Transaction(); | |
| 576 | 10 | atomic_dec32(&no_inflight_txns_); | |
| 577 | 10 | return -errno; | |
| 578 | } | ||
| 579 | |||
| 580 |
1/2✓ Branch 1 taken 1494 times.
✗ Branch 2 not taken.
|
1494 | LogCvmfs(kLogCache, kLogDebug, "start transaction on %s has result %d", |
| 581 | template_path, transaction->fd); | ||
| 582 |
1/2✓ Branch 1 taken 1494 times.
✗ Branch 2 not taken.
|
1494 | transaction->tmp_path = template_path; |
| 583 | 1494 | transaction->expected_size = size; | |
| 584 | 1494 | return transaction->fd; | |
| 585 | 1504 | } | |
| 586 | |||
| 587 | |||
| 588 | 419 | manifest::Breadcrumb PosixCacheManager::LoadBreadcrumb( | |
| 589 | const std::string &fqrn) { | ||
| 590 | 419 | return manifest::Manifest::ReadBreadcrumb(fqrn, cache_path_); | |
| 591 | } | ||
| 592 | |||
| 593 | |||
| 594 | 216 | bool PosixCacheManager::StoreBreadcrumb(const manifest::Manifest &manifest) { | |
| 595 | 216 | return manifest.ExportBreadcrumb(cache_path_, 0600); | |
| 596 | } | ||
| 597 | |||
| 598 | |||
| 599 | ✗ | bool PosixCacheManager::StoreBreadcrumb(std::string fqrn, | |
| 600 | manifest::Breadcrumb breadcrumb) { | ||
| 601 | ✗ | return breadcrumb.Export(fqrn, cache_path_, 0600); | |
| 602 | } | ||
| 603 | |||
| 604 | |||
| 605 | 32 | void PosixCacheManager::TearDown2ReadOnly() { | |
| 606 | 32 | cache_mode_ = kCacheReadOnly; | |
| 607 |
2/2✓ Branch 1 taken 120 times.
✓ Branch 2 taken 24 times.
|
144 | while (atomic_read32(&no_inflight_txns_) != 0) |
| 608 | 120 | SafeSleepMs(50); | |
| 609 | |||
| 610 | 24 | QuotaManager *old_manager = quota_mgr_; | |
| 611 |
1/2✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
|
24 | quota_mgr_ = new NoopQuotaManager(); |
| 612 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | delete old_manager; |
| 613 | 24 | } | |
| 614 | |||
| 615 | |||
| 616 | 1727 | int64_t PosixCacheManager::Write(const void *buf, uint64_t size, void *txn) { | |
| 617 | 1727 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 618 | |||
| 619 |
2/2✓ Branch 0 taken 958 times.
✓ Branch 1 taken 769 times.
|
1727 | if (transaction->expected_size != kSizeUnknown) { |
| 620 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 950 times.
|
958 | if (transaction->size + size > transaction->expected_size) { |
| 621 | 8 | LogCvmfs(kLogCache, kLogDebug, | |
| 622 | "Transaction size (%" PRIu64 ") > expected size (%" PRIu64 ")", | ||
| 623 | 8 | transaction->size + size, transaction->expected_size); | |
| 624 | 8 | return -EFBIG; | |
| 625 | } | ||
| 626 | } | ||
| 627 | |||
| 628 | 1719 | uint64_t written = 0; | |
| 629 | 1719 | const unsigned char *read_pos = reinterpret_cast<const unsigned char *>(buf); | |
| 630 |
2/2✓ Branch 0 taken 2444 times.
✓ Branch 1 taken 1711 times.
|
4155 | while (written < size) { |
| 631 |
2/2✓ Branch 0 taken 1313 times.
✓ Branch 1 taken 1131 times.
|
2444 | if (transaction->buf_pos == sizeof(transaction->buffer)) { |
| 632 |
1/2✓ Branch 1 taken 1313 times.
✗ Branch 2 not taken.
|
1313 | const int retval = Flush(transaction); |
| 633 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1305 times.
|
1313 | if (retval != 0) { |
| 634 | 8 | transaction->size += written; | |
| 635 | 8 | return retval; | |
| 636 | } | ||
| 637 | } | ||
| 638 | 2436 | const uint64_t remaining = size - written; | |
| 639 | 2436 | const uint64_t space_in_buffer = sizeof(transaction->buffer) | |
| 640 | 2436 | - transaction->buf_pos; | |
| 641 | 2436 | const uint64_t batch_size = std::min(remaining, space_in_buffer); | |
| 642 | 2436 | memcpy(transaction->buffer + transaction->buf_pos, read_pos, batch_size); | |
| 643 | 2436 | transaction->buf_pos += batch_size; | |
| 644 | 2436 | written += batch_size; | |
| 645 | 2436 | read_pos += batch_size; | |
| 646 | } | ||
| 647 | 1711 | transaction->size += written; | |
| 648 | 1711 | return written; | |
| 649 | } | ||
| 650 | |||
| 651 |