| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/cache_posix.cc |
| Date: | 2025-11-09 02:35:23 |
| 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 | 353 | int PosixCacheManager::AbortTxn(void *txn) { | |
| 115 | 353 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 116 | 353 | LogCvmfs(kLogCache, kLogDebug, "abort %s", transaction->tmp_path.c_str()); | |
| 117 | 353 | close(transaction->fd); | |
| 118 | 353 | const int result = unlink(transaction->tmp_path.c_str()); | |
| 119 | 353 | transaction->~Transaction(); | |
| 120 | 353 | atomic_dec32(&no_inflight_txns_); | |
| 121 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 333 times.
|
353 | if (result == -1) |
| 122 | 20 | return -errno; | |
| 123 | 333 | 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 | 653 | bool PosixCacheManager::AcquireQuotaManager(QuotaManager *quota_mgr) { | |
| 133 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 653 times.
|
653 | if (quota_mgr == NULL) |
| 134 | ✗ | return false; | |
| 135 |
1/2✓ Branch 0 taken 653 times.
✗ Branch 1 not taken.
|
653 | delete quota_mgr_; |
| 136 | 653 | quota_mgr_ = quota_mgr; | |
| 137 | 653 | return true; | |
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | 1585 | int PosixCacheManager::Close(int fd) { | |
| 142 |
1/2✓ Branch 0 taken 1585 times.
✗ Branch 1 not taken.
|
1585 | const int retval = do_refcount_ ? fd_mgr_->Close(fd) : close(fd); |
| 143 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1575 times.
|
1585 | if (retval != 0) |
| 144 | 10 | return -errno; | |
| 145 | 1575 | return 0; | |
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | 2042 | int PosixCacheManager::CommitTxn(void *txn) { | |
| 150 | 2042 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 151 | int result; | ||
| 152 | 2042 | LogCvmfs(kLogCache, kLogDebug, "commit %s %s", | |
| 153 | transaction->final_path.c_str(), transaction->tmp_path.c_str()); | ||
| 154 | |||
| 155 | 2042 | result = Flush(transaction); | |
| 156 | 2042 | close(transaction->fd); | |
| 157 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2032 times.
|
2042 | if (result < 0) { |
| 158 | 10 | unlink(transaction->tmp_path.c_str()); | |
| 159 | 10 | transaction->~Transaction(); | |
| 160 | 10 | atomic_dec32(&no_inflight_txns_); | |
| 161 | 10 | return result; | |
| 162 | } | ||
| 163 | |||
| 164 | // To support debugging, move files into quarantine on file size mismatch | ||
| 165 |
2/2✓ Branch 0 taken 762 times.
✓ Branch 1 taken 1270 times.
|
2032 | 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 102 times.
✓ Branch 1 taken 660 times.
|
762 | if ((transaction->expected_size != kSizeUnknown) |
| 169 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 102 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
102 | && (reports_correct_filesize_ || (transaction->size != 0))) { |
| 170 |
1/3✓ Branch 2 taken 102 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
102 | LogCvmfs(kLogCache, kLogDebug | kLogSyslogErr, |
| 171 | "size check failure for %s, expected %lu, got %lu", | ||
| 172 | 204 | transaction->id.ToString().c_str(), transaction->expected_size, | |
| 173 | transaction->size); | ||
| 174 |
1/2✓ Branch 1 taken 102 times.
✗ Branch 2 not taken.
|
102 | CopyPath2Path(transaction->tmp_path, |
| 175 |
2/4✓ Branch 2 taken 102 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 102 times.
✗ Branch 6 not taken.
|
204 | cache_path_ + "/quarantaine/" + transaction->id.ToString()); |
| 176 | 102 | unlink(transaction->tmp_path.c_str()); | |
| 177 | 102 | transaction->~Transaction(); | |
| 178 | 102 | atomic_dec32(&no_inflight_txns_); | |
| 179 | 102 | return -EIO; | |
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 |
2/2✓ Branch 0 taken 1920 times.
✓ Branch 1 taken 10 times.
|
1930 | if ((transaction->label.flags & kLabelPinned) |
| 184 |
2/2✓ Branch 0 taken 535 times.
✓ Branch 1 taken 1385 times.
|
1920 | || (transaction->label.flags & kLabelCatalog)) { |
| 185 | 1635 | const bool retval = quota_mgr_->Pin( | |
| 186 |
1/2✓ Branch 1 taken 545 times.
✗ Branch 2 not taken.
|
545 | transaction->id, transaction->size, transaction->label.GetDescription(), |
| 187 | 545 | (transaction->label.flags & kLabelCatalog)); | |
| 188 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 535 times.
|
545 | if (!retval) { |
| 189 |
1/2✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
10 | LogCvmfs(kLogCache, kLogDebug, "commit failed: cannot pin %s", |
| 190 | 20 | transaction->id.ToString().c_str()); | |
| 191 | 10 | unlink(transaction->tmp_path.c_str()); | |
| 192 | 10 | transaction->~Transaction(); | |
| 193 | 10 | atomic_dec32(&no_inflight_txns_); | |
| 194 | 10 | return -ENOSPC; | |
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | // Move the temporary file into its final location | ||
| 199 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1910 times.
|
1920 | if (alien_cache_) { |
| 200 | 10 | const int retval = chmod(transaction->tmp_path.c_str(), 0660); | |
| 201 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | assert(retval == 0); |
| 202 | } | ||
| 203 | 1920 | result = Rename(transaction->tmp_path.c_str(), | |
| 204 | transaction->final_path.c_str()); | ||
| 205 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1900 times.
|
1920 | if (result < 0) { |
| 206 | 20 | LogCvmfs(kLogCache, kLogDebug, "commit failed: %s", strerror(errno)); | |
| 207 | 20 | unlink(transaction->tmp_path.c_str()); | |
| 208 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | if ((transaction->label.flags & kLabelPinned) |
| 209 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | || (transaction->label.flags & kLabelCatalog)) { |
| 210 | 10 | quota_mgr_->Remove(transaction->id); | |
| 211 | } | ||
| 212 | } else { | ||
| 213 | // Success, inform quota manager | ||
| 214 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1890 times.
|
1900 | if (transaction->label.flags & kLabelVolatile) { |
| 215 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | quota_mgr_->InsertVolatile(transaction->id, transaction->size, |
| 216 | 20 | transaction->label.GetDescription()); | |
| 217 | 1890 | } else if (!transaction->label.IsCatalog() | |
| 218 |
6/6✓ Branch 0 taken 1375 times.
✓ Branch 1 taken 515 times.
✓ Branch 3 taken 1365 times.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 1365 times.
✓ Branch 6 taken 525 times.
|
1890 | && !transaction->label.IsPinned()) { |
| 219 |
1/2✓ Branch 1 taken 1365 times.
✗ Branch 2 not taken.
|
1365 | quota_mgr_->Insert(transaction->id, transaction->size, |
| 220 | 2730 | transaction->label.GetDescription()); | |
| 221 | } | ||
| 222 | } | ||
| 223 | 1920 | transaction->~Transaction(); | |
| 224 | 1920 | atomic_dec32(&no_inflight_txns_); | |
| 225 | 1920 | return result; | |
| 226 | } | ||
| 227 | |||
| 228 | 2508 | bool PosixCacheManager::InitCacheDirectory(const string &cache_path) { | |
| 229 |
1/2✓ Branch 1 taken 2508 times.
✗ Branch 2 not taken.
|
2508 | const FileSystemInfo fs_info = GetFileSystemInfo(cache_path); |
| 230 | |||
| 231 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2488 times.
|
2508 | if (fs_info.type == kFsTypeTmpfs) { |
| 232 | 20 | is_tmpfs_ = true; | |
| 233 | } | ||
| 234 | |||
| 235 |
2/2✓ Branch 0 taken 392 times.
✓ Branch 1 taken 2116 times.
|
2508 | if (alien_cache_) { |
| 236 |
3/4✓ Branch 1 taken 392 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 382 times.
|
392 | if (!MakeCacheDirectories(cache_path, 0770)) { |
| 237 | 10 | return false; | |
| 238 | } | ||
| 239 |
1/2✓ Branch 1 taken 382 times.
✗ Branch 2 not taken.
|
382 | LogCvmfs(kLogCache, kLogDebug | kLogSyslog, |
| 240 | "Cache directory structure created."); | ||
| 241 |
1/3✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 382 times.
|
382 | 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 | 382 | default: | |
| 252 | 382 | break; | |
| 253 | } | ||
| 254 | } else { | ||
| 255 |
3/4✓ Branch 1 taken 2116 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 2106 times.
|
2116 | if (!MakeCacheDirectories(cache_path, 0700)) |
| 256 | 10 | 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 2488 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2488 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 10 times.
✓ Branch 8 taken 2478 times.
|
2488 | if (FileExists(cache_path + "/cvmfscatalog.cache")) { |
| 261 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | LogCvmfs(kLogCache, kLogDebug | kLogSyslogErr, |
| 262 | "Not mounting on cvmfs 2.0.X cache"); | ||
| 263 | 10 | return false; | |
| 264 | } | ||
| 265 | 2478 | return true; | |
| 266 | } | ||
| 267 | |||
| 268 | 2508 | PosixCacheManager *PosixCacheManager::Create( | |
| 269 | const string &cache_path, | ||
| 270 | const bool alien_cache, | ||
| 271 | const RenameWorkarounds rename_workaround, | ||
| 272 | const bool do_refcount) { | ||
| 273 | UniquePtr<PosixCacheManager> cache_manager( | ||
| 274 |
3/6✓ Branch 1 taken 2508 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2508 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2508 times.
✗ Branch 8 not taken.
|
2508 | new PosixCacheManager(cache_path, alien_cache, do_refcount)); |
| 275 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2508 times.
|
2508 | assert(cache_manager.IsValid()); |
| 276 | |||
| 277 | 2508 | cache_manager->rename_workaround_ = rename_workaround; | |
| 278 | |||
| 279 |
1/2✓ Branch 2 taken 2508 times.
✗ Branch 3 not taken.
|
2508 | const bool result_ = cache_manager->InitCacheDirectory(cache_path); |
| 280 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 2478 times.
|
2508 | if (!result_) { |
| 281 | 30 | return NULL; | |
| 282 | } | ||
| 283 | |||
| 284 | 2478 | return cache_manager.Release(); | |
| 285 | 2508 | } | |
| 286 | |||
| 287 | |||
| 288 | 2135 | void PosixCacheManager::CtrlTxn(const Label &label, | |
| 289 | const int flags, | ||
| 290 | void *txn) { | ||
| 291 | 2135 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 292 | 2135 | transaction->label = label; | |
| 293 | 2135 | } | |
| 294 | |||
| 295 | |||
| 296 | ✗ | string PosixCacheManager::Describe() { | |
| 297 | ✗ | string msg; | |
| 298 | ✗ | if (do_refcount_) { | |
| 299 | msg = "Refcounting Posix cache manager" | ||
| 300 | "(cache directory: " | ||
| 301 | ✗ | + cache_path_ + ")\n"; | |
| 302 | } else { | ||
| 303 | ✗ | msg = "Posix cache manager (cache directory: " + cache_path_ + ")\n"; | |
| 304 | } | ||
| 305 | ✗ | return msg; | |
| 306 | } | ||
| 307 | |||
| 308 | |||
| 309 | /** | ||
| 310 | * If not refcounting, nothing to do, the kernel keeps the state | ||
| 311 | * of open file descriptors. Return a dummy memory location. | ||
| 312 | */ | ||
| 313 | 10 | void *PosixCacheManager::DoSaveState() { | |
| 314 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if (do_refcount_) { |
| 315 |
1/2✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
10 | SavedState *state = new SavedState(); |
| 316 | 10 | state->fd_mgr = fd_mgr_->Clone(); | |
| 317 | 10 | return state; | |
| 318 | } | ||
| 319 | ✗ | char *c = reinterpret_cast<char *>(smalloc(1)); | |
| 320 | ✗ | *c = kMagicNoRefcount; | |
| 321 | ✗ | return c; | |
| 322 | } | ||
| 323 | |||
| 324 | |||
| 325 | 10 | int PosixCacheManager::DoRestoreState(void *data) { | |
| 326 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | assert(data); |
| 327 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if (do_refcount_) { |
| 328 | 10 | SavedState *state = reinterpret_cast<SavedState *>(data); | |
| 329 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if (state->magic_number == kMagicRefcount) { |
| 330 | 10 | LogCvmfs(kLogCache, kLogDebug, | |
| 331 | "Restoring refcount cache manager from " | ||
| 332 | "refcounted posix cache manager"); | ||
| 333 | |||
| 334 | 10 | fd_mgr_->AssignFrom(state->fd_mgr.weak_ref()); | |
| 335 | } else { | ||
| 336 | ✗ | LogCvmfs(kLogCache, kLogDebug, | |
| 337 | "Restoring refcount cache manager from " | ||
| 338 | "non-refcounted posix cache manager"); | ||
| 339 | } | ||
| 340 | 10 | return -1; | |
| 341 | } | ||
| 342 | |||
| 343 | ✗ | char *c = reinterpret_cast<char *>(data); | |
| 344 | ✗ | assert(*c == kMagicNoRefcount || *c == kMagicRefcount); | |
| 345 | ✗ | if (*c == kMagicRefcount) { | |
| 346 | ✗ | SavedState *state = reinterpret_cast<SavedState *>(data); | |
| 347 | ✗ | LogCvmfs(kLogCache, kLogDebug, | |
| 348 | "Restoring non-refcount cache manager from " | ||
| 349 | "refcounted posix cache manager - this " | ||
| 350 | " is not possible, keep refcounting."); | ||
| 351 | ✗ | fd_mgr_->AssignFrom(state->fd_mgr.weak_ref()); | |
| 352 | ✗ | do_refcount_ = true; | |
| 353 | } | ||
| 354 | ✗ | return -1; | |
| 355 | } | ||
| 356 | |||
| 357 | |||
| 358 | 10 | bool PosixCacheManager::DoFreeState(void *data) { | |
| 359 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | assert(data); |
| 360 | 10 | SavedState *state = reinterpret_cast<SavedState *>(data); | |
| 361 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if (state->magic_number == kMagicRefcount) { |
| 362 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | delete state; |
| 363 | } else { | ||
| 364 | // If not refcounted, the state is the dummy SavedState | ||
| 365 | // of the regular posix cache manager | ||
| 366 | ✗ | free(data); | |
| 367 | } | ||
| 368 | 10 | return true; | |
| 369 | } | ||
| 370 | |||
| 371 | |||
| 372 | 112 | int PosixCacheManager::Dup(int fd) { | |
| 373 |
1/2✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
|
112 | const int new_fd = do_refcount_ ? fd_mgr_->Dup(fd) : dup(fd); |
| 374 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 56 times.
|
112 | if (new_fd < 0) |
| 375 | 56 | return -errno; | |
| 376 | 56 | return new_fd; | |
| 377 | } | ||
| 378 | |||
| 379 | |||
| 380 | 4655 | int PosixCacheManager::Flush(Transaction *transaction) { | |
| 381 |
2/2✓ Branch 0 taken 1223 times.
✓ Branch 1 taken 3432 times.
|
4655 | if (transaction->buf_pos == 0) |
| 382 | 1223 | return 0; | |
| 383 | 6864 | const int written = write(transaction->fd, transaction->buffer, | |
| 384 | 3432 | transaction->buf_pos); | |
| 385 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 3402 times.
|
3432 | if (written < 0) |
| 386 | 30 | return -errno; | |
| 387 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3402 times.
|
3402 | if (static_cast<unsigned>(written) != transaction->buf_pos) { |
| 388 | ✗ | transaction->buf_pos -= written; | |
| 389 | ✗ | return -EIO; | |
| 390 | } | ||
| 391 | 3402 | transaction->buf_pos = 0; | |
| 392 | 3402 | return 0; | |
| 393 | } | ||
| 394 | |||
| 395 | |||
| 396 | 5891 | inline string PosixCacheManager::GetPathInCache(const shash::Any &id) { | |
| 397 |
2/4✓ Branch 2 taken 5891 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 5891 times.
✗ Branch 6 not taken.
|
11782 | return cache_path_ + "/" + id.MakePathWithoutSuffix(); |
| 398 | } | ||
| 399 | |||
| 400 | |||
| 401 | 1073 | int64_t PosixCacheManager::GetSize(int fd) { | |
| 402 | platform_stat64 info; | ||
| 403 | 1073 | const int retval = platform_fstat(fd, &info); | |
| 404 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1063 times.
|
1073 | if (retval != 0) |
| 405 | 10 | return -errno; | |
| 406 | 1063 | return info.st_size; | |
| 407 | } | ||
| 408 | |||
| 409 | |||
| 410 | 3440 | int PosixCacheManager::Open(const LabeledObject &object) { | |
| 411 |
1/2✓ Branch 1 taken 3440 times.
✗ Branch 2 not taken.
|
3440 | const string path = GetPathInCache(object.id); |
| 412 | int result; | ||
| 413 |
1/2✓ Branch 0 taken 3440 times.
✗ Branch 1 not taken.
|
3440 | if (do_refcount_) { |
| 414 |
1/2✓ Branch 2 taken 3440 times.
✗ Branch 3 not taken.
|
3440 | result = fd_mgr_->Open(object.id, path); |
| 415 | } else { | ||
| 416 | ✗ | result = open(path.c_str(), O_RDONLY); | |
| 417 | } | ||
| 418 |
2/2✓ Branch 0 taken 858 times.
✓ Branch 1 taken 2582 times.
|
3440 | if (result >= 0) { |
| 419 |
1/2✓ Branch 2 taken 858 times.
✗ Branch 3 not taken.
|
858 | LogCvmfs(kLogCache, kLogDebug, "hit %s", path.c_str()); |
| 420 | // platform_disable_kcache(result); | ||
| 421 |
1/2✓ Branch 1 taken 858 times.
✗ Branch 2 not taken.
|
858 | quota_mgr_->Touch(object.id); |
| 422 | } else { | ||
| 423 | 2582 | result = -errno; | |
| 424 |
1/2✓ Branch 2 taken 2582 times.
✗ Branch 3 not taken.
|
2582 | LogCvmfs(kLogCache, kLogDebug, "miss %s (%d)", path.c_str(), result); |
| 425 | } | ||
| 426 | 3440 | return result; | |
| 427 | 3440 | } | |
| 428 | |||
| 429 | |||
| 430 | 884 | int PosixCacheManager::OpenFromTxn(void *txn) { | |
| 431 | 884 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 432 | 884 | const int retval = Flush(transaction); | |
| 433 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 874 times.
|
884 | if (retval < 0) |
| 434 | 10 | return retval; | |
| 435 | int fd_rdonly; | ||
| 436 | |||
| 437 |
1/2✓ Branch 0 taken 874 times.
✗ Branch 1 not taken.
|
874 | if (do_refcount_) { |
| 438 |
2/4✓ Branch 4 taken 874 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 874 times.
✗ Branch 8 not taken.
|
874 | fd_rdonly = fd_mgr_->Open(transaction->id, transaction->tmp_path.c_str()); |
| 439 | } else { | ||
| 440 | ✗ | fd_rdonly = open(transaction->tmp_path.c_str(), O_RDONLY); | |
| 441 | } | ||
| 442 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 864 times.
|
874 | if (fd_rdonly == -1) |
| 443 | 10 | return -errno; | |
| 444 | 864 | return fd_rdonly; | |
| 445 | } | ||
| 446 | |||
| 447 | |||
| 448 | 8594 | int64_t PosixCacheManager::Pread(int fd, | |
| 449 | void *buf, | ||
| 450 | uint64_t size, | ||
| 451 | uint64_t offset) { | ||
| 452 | int64_t result; | ||
| 453 | do { | ||
| 454 | 8594 | errno = 0; | |
| 455 | 8594 | result = pread(fd, buf, size, offset); | |
| 456 |
3/4✓ Branch 0 taken 20 times.
✓ Branch 1 taken 8574 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
|
8594 | } while ((result == -1) && (errno == EINTR)); |
| 457 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 8574 times.
|
8594 | if (result < 0) |
| 458 | 20 | return -errno; | |
| 459 | 8574 | return result; | |
| 460 | } | ||
| 461 | |||
| 462 | |||
| 463 | 1980 | int PosixCacheManager::Rename(const char *oldpath, const char *newpath) { | |
| 464 | int result; | ||
| 465 |
2/2✓ Branch 0 taken 1950 times.
✓ Branch 1 taken 30 times.
|
1980 | if (rename_workaround_ != kRenameLink) { |
| 466 | 1950 | result = rename(oldpath, newpath); | |
| 467 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1920 times.
|
1950 | if (result < 0) |
| 468 | 30 | return -errno; | |
| 469 | 1920 | return 0; | |
| 470 | } | ||
| 471 | |||
| 472 | 30 | result = link(oldpath, newpath); | |
| 473 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10 times.
|
30 | if (result < 0) { |
| 474 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | if (errno == EEXIST) { |
| 475 | 10 | LogCvmfs(kLogCache, kLogDebug, "%s already existed, ignoring", newpath); | |
| 476 | } else { | ||
| 477 | 10 | return -errno; | |
| 478 | } | ||
| 479 | } | ||
| 480 | 20 | result = unlink(oldpath); | |
| 481 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | if (result < 0) |
| 482 | ✗ | return -errno; | |
| 483 | 20 | return 0; | |
| 484 | } | ||
| 485 | |||
| 486 | |||
| 487 | /** | ||
| 488 | * Used by the sqlite vfs in order to preload file catalogs into the file system | ||
| 489 | * buffers. | ||
| 490 | * | ||
| 491 | * No-op if the fd is to a file that is on a tmpfs, and so already in page cache | ||
| 492 | */ | ||
| 493 | 572 | int PosixCacheManager::Readahead(int fd) { | |
| 494 | unsigned char *buf[4096]; | ||
| 495 | int nbytes; | ||
| 496 | 572 | uint64_t pos = 0; | |
| 497 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 572 times.
|
572 | if (is_tmpfs()) { |
| 498 | ✗ | return 0; | |
| 499 | } | ||
| 500 | do { | ||
| 501 |
1/2✓ Branch 1 taken 2724 times.
✗ Branch 2 not taken.
|
2724 | nbytes = Pread(fd, buf, 4096, pos); |
| 502 | 2724 | pos += nbytes; | |
| 503 |
2/2✓ Branch 0 taken 2152 times.
✓ Branch 1 taken 572 times.
|
2724 | } while (nbytes == 4096); |
| 504 |
1/2✓ Branch 1 taken 572 times.
✗ Branch 2 not taken.
|
572 | LogCvmfs(kLogCache, kLogDebug, "read-ahead %d, %" PRIu64, fd, pos); |
| 505 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 572 times.
|
572 | if (nbytes < 0) |
| 506 | ✗ | return nbytes; | |
| 507 | 572 | return 0; | |
| 508 | } | ||
| 509 | |||
| 510 | |||
| 511 | 289 | int PosixCacheManager::Reset(void *txn) { | |
| 512 | 289 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 513 | 289 | transaction->buf_pos = 0; | |
| 514 | 289 | transaction->size = 0; | |
| 515 | 289 | int retval = lseek(transaction->fd, 0, SEEK_SET); | |
| 516 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 279 times.
|
289 | if (retval < 0) |
| 517 | 10 | return -errno; | |
| 518 | 279 | retval = ftruncate(transaction->fd, 0); | |
| 519 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
|
279 | if (retval < 0) |
| 520 | ✗ | return -errno; | |
| 521 | 279 | return 0; | |
| 522 | } | ||
| 523 | |||
| 524 | |||
| 525 | 2471 | int PosixCacheManager::StartTxn(const shash::Any &id, | |
| 526 | uint64_t size, | ||
| 527 | void *txn) { | ||
| 528 | 2471 | atomic_inc32(&no_inflight_txns_); | |
| 529 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2461 times.
|
2471 | if (cache_mode_ == kCacheReadOnly) { |
| 530 | 10 | atomic_dec32(&no_inflight_txns_); | |
| 531 | 10 | return -EROFS; | |
| 532 | } | ||
| 533 | |||
| 534 |
2/2✓ Branch 0 taken 1532 times.
✓ Branch 1 taken 929 times.
|
2461 | if (size != kSizeUnknown) { |
| 535 |
3/4✓ Branch 1 taken 1532 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 1522 times.
|
1532 | if (size > quota_mgr_->GetMaxFileSize()) { |
| 536 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | LogCvmfs(kLogCache, kLogDebug, |
| 537 | "file too big for lru cache (%" PRIu64 " " | ||
| 538 | "requested but only %" PRIu64 " bytes free)", | ||
| 539 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | size, quota_mgr_->GetMaxFileSize()); |
| 540 | 10 | atomic_dec32(&no_inflight_txns_); | |
| 541 | 10 | return -ENOSPC; | |
| 542 | } | ||
| 543 | |||
| 544 | // For large files, ensure enough free cache space before writing the chunk | ||
| 545 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1512 times.
|
1522 | if (size > kBigFile) { |
| 546 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | const uint64_t cache_size = quota_mgr_->GetSize(); |
| 547 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | const uint64_t cache_capacity = quota_mgr_->GetCapacity(); |
| 548 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | assert(cache_capacity >= size); |
| 549 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if ((cache_size + size) > cache_capacity) { |
| 550 | 20 | const uint64_t leave_size = std::min(cache_capacity / 2, | |
| 551 | 10 | cache_capacity - size); | |
| 552 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | quota_mgr_->Cleanup(leave_size); |
| 553 | } | ||
| 554 | } | ||
| 555 | } | ||
| 556 | |||
| 557 |
1/2✓ Branch 1 taken 2451 times.
✗ Branch 2 not taken.
|
2451 | string path_in_cache = GetPathInCache(id); |
| 558 |
1/2✓ Branch 2 taken 2451 times.
✗ Branch 3 not taken.
|
2451 | Transaction *transaction = new (txn) Transaction(id, path_in_cache); |
| 559 | |||
| 560 | 2451 | char *template_path = NULL; | |
| 561 | 2451 | unsigned temp_path_len = 0; | |
| 562 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2431 times.
|
2451 | if (rename_workaround_ == kRenameSamedir) { |
| 563 | 20 | temp_path_len = path_in_cache.length() + 6; | |
| 564 | 20 | template_path = reinterpret_cast<char *>(alloca(temp_path_len + 1)); | |
| 565 | 20 | memcpy(template_path, path_in_cache.data(), path_in_cache.length()); | |
| 566 | 20 | memset(template_path + path_in_cache.length(), 'X', 6); | |
| 567 | } else { | ||
| 568 | 2431 | temp_path_len = txn_template_path_.length(); | |
| 569 | 2431 | template_path = reinterpret_cast<char *>(alloca(temp_path_len + 1)); | |
| 570 |
1/2✓ Branch 1 taken 2431 times.
✗ Branch 2 not taken.
|
2431 | memcpy(template_path, &txn_template_path_[0], temp_path_len); |
| 571 | } | ||
| 572 | 2451 | template_path[temp_path_len] = '\0'; | |
| 573 | |||
| 574 |
1/2✓ Branch 1 taken 2451 times.
✗ Branch 2 not taken.
|
2451 | transaction->fd = mkstemp(template_path); |
| 575 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 2395 times.
|
2451 | if (transaction->fd == -1) { |
| 576 | 56 | transaction->~Transaction(); | |
| 577 | 56 | atomic_dec32(&no_inflight_txns_); | |
| 578 | 56 | return -errno; | |
| 579 | } | ||
| 580 | |||
| 581 |
1/2✓ Branch 1 taken 2395 times.
✗ Branch 2 not taken.
|
2395 | LogCvmfs(kLogCache, kLogDebug, "start transaction on %s has result %d", |
| 582 | template_path, transaction->fd); | ||
| 583 |
1/2✓ Branch 1 taken 2395 times.
✗ Branch 2 not taken.
|
2395 | transaction->tmp_path = template_path; |
| 584 | 2395 | transaction->expected_size = size; | |
| 585 | 2395 | return transaction->fd; | |
| 586 | 2451 | } | |
| 587 | |||
| 588 | |||
| 589 | 390 | manifest::Breadcrumb PosixCacheManager::LoadBreadcrumb( | |
| 590 | const std::string &fqrn) { | ||
| 591 | 390 | return manifest::Manifest::ReadBreadcrumb(fqrn, cache_path_); | |
| 592 | } | ||
| 593 | |||
| 594 | |||
| 595 | 175 | bool PosixCacheManager::StoreBreadcrumb(const manifest::Manifest &manifest) { | |
| 596 | 175 | return manifest.ExportBreadcrumb(cache_path_, 0600); | |
| 597 | } | ||
| 598 | |||
| 599 | |||
| 600 | ✗ | bool PosixCacheManager::StoreBreadcrumb(std::string fqrn, | |
| 601 | manifest::Breadcrumb breadcrumb) { | ||
| 602 | ✗ | return breadcrumb.Export(fqrn, cache_path_, 0600); | |
| 603 | } | ||
| 604 | |||
| 605 | |||
| 606 | 40 | void PosixCacheManager::TearDown2ReadOnly() { | |
| 607 | 40 | cache_mode_ = kCacheReadOnly; | |
| 608 |
2/2✓ Branch 1 taken 130 times.
✓ Branch 2 taken 30 times.
|
160 | while (atomic_read32(&no_inflight_txns_) != 0) |
| 609 | 130 | SafeSleepMs(50); | |
| 610 | |||
| 611 | 30 | QuotaManager *old_manager = quota_mgr_; | |
| 612 |
1/2✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
|
30 | quota_mgr_ = new NoopQuotaManager(); |
| 613 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | delete old_manager; |
| 614 | 30 | } | |
| 615 | |||
| 616 | |||
| 617 | 2623 | int64_t PosixCacheManager::Write(const void *buf, uint64_t size, void *txn) { | |
| 618 | 2623 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 619 | |||
| 620 |
2/2✓ Branch 0 taken 1412 times.
✓ Branch 1 taken 1211 times.
|
2623 | if (transaction->expected_size != kSizeUnknown) { |
| 621 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1402 times.
|
1412 | if (transaction->size + size > transaction->expected_size) { |
| 622 | 10 | LogCvmfs(kLogCache, kLogDebug, | |
| 623 | "Transaction size (%" PRIu64 ") > expected size (%" PRIu64 ")", | ||
| 624 | 10 | transaction->size + size, transaction->expected_size); | |
| 625 | 10 | return -EFBIG; | |
| 626 | } | ||
| 627 | } | ||
| 628 | |||
| 629 | 2613 | uint64_t written = 0; | |
| 630 | 2613 | const unsigned char *read_pos = reinterpret_cast<const unsigned char *>(buf); | |
| 631 |
2/2✓ Branch 0 taken 3554 times.
✓ Branch 1 taken 2603 times.
|
6157 | while (written < size) { |
| 632 |
2/2✓ Branch 0 taken 1729 times.
✓ Branch 1 taken 1825 times.
|
3554 | if (transaction->buf_pos == sizeof(transaction->buffer)) { |
| 633 |
1/2✓ Branch 1 taken 1729 times.
✗ Branch 2 not taken.
|
1729 | const int retval = Flush(transaction); |
| 634 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1719 times.
|
1729 | if (retval != 0) { |
| 635 | 10 | transaction->size += written; | |
| 636 | 10 | return retval; | |
| 637 | } | ||
| 638 | } | ||
| 639 | 3544 | const uint64_t remaining = size - written; | |
| 640 | 3544 | const uint64_t space_in_buffer = sizeof(transaction->buffer) | |
| 641 | 3544 | - transaction->buf_pos; | |
| 642 | 3544 | const uint64_t batch_size = std::min(remaining, space_in_buffer); | |
| 643 | 3544 | memcpy(transaction->buffer + transaction->buf_pos, read_pos, batch_size); | |
| 644 | 3544 | transaction->buf_pos += batch_size; | |
| 645 | 3544 | written += batch_size; | |
| 646 | 3544 | read_pos += batch_size; | |
| 647 | } | ||
| 648 | 2603 | transaction->size += written; | |
| 649 | 2603 | return written; | |
| 650 | } | ||
| 651 |