| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/cache_posix.cc |
| Date: | 2026-02-01 02:35:56 |
| 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 | 813 | int PosixCacheManager::AbortTxn(void *txn) { | |
| 115 | 813 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 116 | 813 | LogCvmfs(kLogCache, kLogDebug, "abort %s", transaction->tmp_path.c_str()); | |
| 117 | 813 | close(transaction->fd); | |
| 118 | 813 | const int result = unlink(transaction->tmp_path.c_str()); | |
| 119 | 813 | transaction->~Transaction(); | |
| 120 | 813 | atomic_dec32(&no_inflight_txns_); | |
| 121 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 725 times.
|
813 | if (result == -1) |
| 122 | 88 | return -errno; | |
| 123 | 725 | 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 | 926 | bool PosixCacheManager::AcquireQuotaManager(QuotaManager *quota_mgr) { | |
| 133 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 926 times.
|
926 | if (quota_mgr == NULL) |
| 134 | ✗ | return false; | |
| 135 |
1/2✓ Branch 0 taken 926 times.
✗ Branch 1 not taken.
|
926 | delete quota_mgr_; |
| 136 | 926 | quota_mgr_ = quota_mgr; | |
| 137 | 926 | return true; | |
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | 2280 | int PosixCacheManager::Close(int fd) { | |
| 142 |
1/2✓ Branch 0 taken 2280 times.
✗ Branch 1 not taken.
|
2280 | const int retval = do_refcount_ ? fd_mgr_->Close(fd) : close(fd); |
| 143 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 2236 times.
|
2280 | if (retval != 0) |
| 144 | 44 | return -errno; | |
| 145 | 2236 | return 0; | |
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | 5259 | int PosixCacheManager::CommitTxn(void *txn) { | |
| 150 | 5259 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 151 | int result; | ||
| 152 | 5259 | LogCvmfs(kLogCache, kLogDebug, "commit %s %s", | |
| 153 | transaction->final_path.c_str(), transaction->tmp_path.c_str()); | ||
| 154 | |||
| 155 | 5259 | result = Flush(transaction); | |
| 156 | 5259 | close(transaction->fd); | |
| 157 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 5215 times.
|
5259 | if (result < 0) { |
| 158 | 44 | unlink(transaction->tmp_path.c_str()); | |
| 159 | 44 | transaction->~Transaction(); | |
| 160 | 44 | atomic_dec32(&no_inflight_txns_); | |
| 161 | 44 | return result; | |
| 162 | } | ||
| 163 | |||
| 164 | // To support debugging, move files into quarantine on file size mismatch | ||
| 165 |
2/2✓ Branch 0 taken 757 times.
✓ Branch 1 taken 4458 times.
|
5215 | 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 118 times.
✓ Branch 1 taken 639 times.
|
757 | if ((transaction->expected_size != kSizeUnknown) |
| 169 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 118 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
118 | && (reports_correct_filesize_ || (transaction->size != 0))) { |
| 170 |
1/3✓ Branch 2 taken 118 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
118 | LogCvmfs(kLogCache, kLogDebug | kLogSyslogErr, |
| 171 | "size check failure for %s, expected %lu, got %lu", | ||
| 172 | 236 | transaction->id.ToString().c_str(), transaction->expected_size, | |
| 173 | transaction->size); | ||
| 174 |
1/2✓ Branch 1 taken 118 times.
✗ Branch 2 not taken.
|
118 | CopyPath2Path(transaction->tmp_path, |
| 175 |
2/4✓ Branch 2 taken 118 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 118 times.
✗ Branch 6 not taken.
|
236 | cache_path_ + "/quarantaine/" + transaction->id.ToString()); |
| 176 | 118 | unlink(transaction->tmp_path.c_str()); | |
| 177 | 118 | transaction->~Transaction(); | |
| 178 | 118 | atomic_dec32(&no_inflight_txns_); | |
| 179 | 118 | return -EIO; | |
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 |
2/2✓ Branch 0 taken 5053 times.
✓ Branch 1 taken 44 times.
|
5097 | if ((transaction->label.flags & kLabelPinned) |
| 184 |
2/2✓ Branch 0 taken 634 times.
✓ Branch 1 taken 4419 times.
|
5053 | || (transaction->label.flags & kLabelCatalog)) { |
| 185 | 2034 | const bool retval = quota_mgr_->Pin( | |
| 186 |
1/2✓ Branch 1 taken 678 times.
✗ Branch 2 not taken.
|
678 | transaction->id, transaction->size, transaction->label.GetDescription(), |
| 187 | 678 | (transaction->label.flags & kLabelCatalog)); | |
| 188 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 634 times.
|
678 | if (!retval) { |
| 189 |
1/2✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
|
44 | LogCvmfs(kLogCache, kLogDebug, "commit failed: cannot pin %s", |
| 190 | 88 | transaction->id.ToString().c_str()); | |
| 191 | 44 | unlink(transaction->tmp_path.c_str()); | |
| 192 | 44 | transaction->~Transaction(); | |
| 193 | 44 | atomic_dec32(&no_inflight_txns_); | |
| 194 | 44 | return -ENOSPC; | |
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | // Move the temporary file into its final location | ||
| 199 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 5009 times.
|
5053 | if (alien_cache_) { |
| 200 | 44 | const int retval = chmod(transaction->tmp_path.c_str(), 0660); | |
| 201 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | assert(retval == 0); |
| 202 | } | ||
| 203 | 5053 | result = Rename(transaction->tmp_path.c_str(), | |
| 204 | transaction->final_path.c_str()); | ||
| 205 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 4965 times.
|
5053 | if (result < 0) { |
| 206 | 88 | LogCvmfs(kLogCache, kLogDebug, "commit failed: %s", strerror(errno)); | |
| 207 | 88 | unlink(transaction->tmp_path.c_str()); | |
| 208 |
1/2✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
|
88 | if ((transaction->label.flags & kLabelPinned) |
| 209 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 44 times.
|
88 | || (transaction->label.flags & kLabelCatalog)) { |
| 210 | 44 | quota_mgr_->Remove(transaction->id); | |
| 211 | } | ||
| 212 | } else { | ||
| 213 | // Success, inform quota manager | ||
| 214 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 4921 times.
|
4965 | if (transaction->label.flags & kLabelVolatile) { |
| 215 |
1/2✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
|
44 | quota_mgr_->InsertVolatile(transaction->id, transaction->size, |
| 216 | 88 | transaction->label.GetDescription()); | |
| 217 | 4921 | } else if (!transaction->label.IsCatalog() | |
| 218 |
6/6✓ Branch 0 taken 4375 times.
✓ Branch 1 taken 546 times.
✓ Branch 3 taken 4331 times.
✓ Branch 4 taken 44 times.
✓ Branch 5 taken 4331 times.
✓ Branch 6 taken 590 times.
|
4921 | && !transaction->label.IsPinned()) { |
| 219 |
1/2✓ Branch 1 taken 4331 times.
✗ Branch 2 not taken.
|
4331 | quota_mgr_->Insert(transaction->id, transaction->size, |
| 220 | 8662 | transaction->label.GetDescription()); | |
| 221 | } | ||
| 222 | } | ||
| 223 | 5053 | transaction->~Transaction(); | |
| 224 | 5053 | atomic_dec32(&no_inflight_txns_); | |
| 225 | 5053 | return result; | |
| 226 | } | ||
| 227 | |||
| 228 | 5114 | bool PosixCacheManager::InitCacheDirectory(const string &cache_path) { | |
| 229 |
1/2✓ Branch 1 taken 5114 times.
✗ Branch 2 not taken.
|
5114 | const FileSystemInfo fs_info = GetFileSystemInfo(cache_path); |
| 230 | |||
| 231 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 5026 times.
|
5114 | if (fs_info.type == kFsTypeTmpfs) { |
| 232 | 88 | is_tmpfs_ = true; | |
| 233 | } | ||
| 234 | |||
| 235 |
2/2✓ Branch 0 taken 1321 times.
✓ Branch 1 taken 3793 times.
|
5114 | if (alien_cache_) { |
| 236 |
3/4✓ Branch 1 taken 1321 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 44 times.
✓ Branch 4 taken 1277 times.
|
1321 | if (!MakeCacheDirectories(cache_path, 0770)) { |
| 237 | 44 | return false; | |
| 238 | } | ||
| 239 |
1/2✓ Branch 1 taken 1277 times.
✗ Branch 2 not taken.
|
1277 | LogCvmfs(kLogCache, kLogDebug | kLogSyslog, |
| 240 | "Cache directory structure created."); | ||
| 241 |
1/3✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 1277 times.
|
1277 | 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 | 1277 | default: | |
| 252 | 1277 | break; | |
| 253 | } | ||
| 254 | } else { | ||
| 255 |
3/4✓ Branch 1 taken 3793 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 44 times.
✓ Branch 4 taken 3749 times.
|
3793 | if (!MakeCacheDirectories(cache_path, 0700)) |
| 256 | 44 | 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 5026 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 5026 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 44 times.
✓ Branch 8 taken 4982 times.
|
5026 | if (FileExists(cache_path + "/cvmfscatalog.cache")) { |
| 261 |
1/2✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
|
44 | LogCvmfs(kLogCache, kLogDebug | kLogSyslogErr, |
| 262 | "Not mounting on cvmfs 2.0.X cache"); | ||
| 263 | 44 | return false; | |
| 264 | } | ||
| 265 | 4982 | return true; | |
| 266 | } | ||
| 267 | |||
| 268 | 5114 | 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 5114 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5114 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 5114 times.
✗ Branch 8 not taken.
|
5114 | cache_path, alien_cache, do_refcount, cleanup_unused_first)); |
| 274 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 5114 times.
|
5114 | assert(cache_manager.IsValid()); |
| 275 | |||
| 276 | 5114 | cache_manager->rename_workaround_ = rename_workaround; | |
| 277 | |||
| 278 |
1/2✓ Branch 2 taken 5114 times.
✗ Branch 3 not taken.
|
5114 | const bool result_ = cache_manager->InitCacheDirectory(cache_path); |
| 279 |
2/2✓ Branch 0 taken 132 times.
✓ Branch 1 taken 4982 times.
|
5114 | if (!result_) { |
| 280 | 132 | return NULL; | |
| 281 | } | ||
| 282 | |||
| 283 | 4982 | return cache_manager.Release(); | |
| 284 | 5114 | } | |
| 285 | |||
| 286 | |||
| 287 | 4928 | void PosixCacheManager::CtrlTxn(const Label &label, | |
| 288 | const int flags, | ||
| 289 | void *txn) { | ||
| 290 | 4928 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 291 | 4928 | transaction->label = label; | |
| 292 | 4928 | } | |
| 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 | 44 | void *PosixCacheManager::DoSaveState() { | |
| 313 |
1/2✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
|
44 | if (do_refcount_) { |
| 314 |
1/2✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
|
44 | SavedState *state = new SavedState(); |
| 315 | 44 | state->fd_mgr = fd_mgr_->Clone(); | |
| 316 | 44 | return state; | |
| 317 | } | ||
| 318 | ✗ | char *c = reinterpret_cast<char *>(smalloc(1)); | |
| 319 | ✗ | *c = kMagicNoRefcount; | |
| 320 | ✗ | return c; | |
| 321 | } | ||
| 322 | |||
| 323 | |||
| 324 | 44 | int PosixCacheManager::DoRestoreState(void *data) { | |
| 325 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | assert(data); |
| 326 |
1/2✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
|
44 | if (do_refcount_) { |
| 327 | 44 | SavedState *state = reinterpret_cast<SavedState *>(data); | |
| 328 |
1/2✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
|
44 | if (state->magic_number == kMagicRefcount) { |
| 329 | 44 | LogCvmfs(kLogCache, kLogDebug, | |
| 330 | "Restoring refcount cache manager from " | ||
| 331 | "refcounted posix cache manager"); | ||
| 332 | |||
| 333 | 44 | 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 | 44 | 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 | 44 | bool PosixCacheManager::DoFreeState(void *data) { | |
| 358 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | assert(data); |
| 359 | 44 | SavedState *state = reinterpret_cast<SavedState *>(data); | |
| 360 |
1/2✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
|
44 | if (state->magic_number == kMagicRefcount) { |
| 361 |
1/2✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
|
44 | 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 | 44 | return true; | |
| 368 | } | ||
| 369 | |||
| 370 | |||
| 371 | 162 | int PosixCacheManager::Dup(int fd) { | |
| 372 |
1/2✓ Branch 0 taken 162 times.
✗ Branch 1 not taken.
|
162 | const int new_fd = do_refcount_ ? fd_mgr_->Dup(fd) : dup(fd); |
| 373 |
2/2✓ Branch 0 taken 81 times.
✓ Branch 1 taken 81 times.
|
162 | if (new_fd < 0) |
| 374 | 81 | return -errno; | |
| 375 | 81 | return new_fd; | |
| 376 | } | ||
| 377 | |||
| 378 | |||
| 379 | 8990 | int PosixCacheManager::Flush(Transaction *transaction) { | |
| 380 |
2/2✓ Branch 0 taken 2279 times.
✓ Branch 1 taken 6711 times.
|
8990 | if (transaction->buf_pos == 0) |
| 381 | 2279 | return 0; | |
| 382 | 13422 | const int written = write(transaction->fd, transaction->buffer, | |
| 383 | 6711 | transaction->buf_pos); | |
| 384 |
2/2✓ Branch 0 taken 132 times.
✓ Branch 1 taken 6579 times.
|
6711 | if (written < 0) |
| 385 | 132 | return -errno; | |
| 386 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6579 times.
|
6579 | if (static_cast<unsigned>(written) != transaction->buf_pos) { |
| 387 | ✗ | transaction->buf_pos -= written; | |
| 388 | ✗ | return -EIO; | |
| 389 | } | ||
| 390 | 6579 | transaction->buf_pos = 0; | |
| 391 | 6579 | return 0; | |
| 392 | } | ||
| 393 | |||
| 394 | |||
| 395 | 10607 | inline string PosixCacheManager::GetPathInCache(const shash::Any &id) { | |
| 396 |
2/4✓ Branch 2 taken 10607 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 10607 times.
✗ Branch 6 not taken.
|
21214 | return cache_path_ + "/" + id.MakePathWithoutSuffix(); |
| 397 | } | ||
| 398 | |||
| 399 | |||
| 400 | 1819 | int64_t PosixCacheManager::GetSize(int fd) { | |
| 401 | platform_stat64 info; | ||
| 402 | 1819 | const int retval = platform_fstat(fd, &info); | |
| 403 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 1775 times.
|
1819 | if (retval != 0) |
| 404 | 44 | return -errno; | |
| 405 | 1775 | return info.st_size; | |
| 406 | } | ||
| 407 | |||
| 408 | |||
| 409 | 4454 | int PosixCacheManager::Open(const LabeledObject &object) { | |
| 410 |
1/2✓ Branch 1 taken 4454 times.
✗ Branch 2 not taken.
|
4454 | const string path = GetPathInCache(object.id); |
| 411 | int result; | ||
| 412 |
1/2✓ Branch 0 taken 4454 times.
✗ Branch 1 not taken.
|
4454 | if (do_refcount_) { |
| 413 |
1/2✓ Branch 2 taken 4454 times.
✗ Branch 3 not taken.
|
4454 | result = fd_mgr_->Open(object.id, path); |
| 414 | } else { | ||
| 415 | ✗ | result = open(path.c_str(), O_RDONLY); | |
| 416 | } | ||
| 417 |
2/2✓ Branch 0 taken 1623 times.
✓ Branch 1 taken 2831 times.
|
4454 | if (result >= 0) { |
| 418 |
1/2✓ Branch 2 taken 1623 times.
✗ Branch 3 not taken.
|
1623 | LogCvmfs(kLogCache, kLogDebug, "hit %s", path.c_str()); |
| 419 | // platform_disable_kcache(result); | ||
| 420 |
1/2✓ Branch 1 taken 1623 times.
✗ Branch 2 not taken.
|
1623 | quota_mgr_->Touch(object.id); |
| 421 | } else { | ||
| 422 | 2831 | result = -errno; | |
| 423 |
1/2✓ Branch 2 taken 2831 times.
✗ Branch 3 not taken.
|
2831 | LogCvmfs(kLogCache, kLogDebug, "miss %s (%d)", path.c_str(), result); |
| 424 | } | ||
| 425 | 4454 | return result; | |
| 426 | 4454 | } | |
| 427 | |||
| 428 | |||
| 429 | 963 | int PosixCacheManager::OpenFromTxn(void *txn) { | |
| 430 | 963 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 431 | 963 | const int retval = Flush(transaction); | |
| 432 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 919 times.
|
963 | if (retval < 0) |
| 433 | 44 | return retval; | |
| 434 | int fd_rdonly; | ||
| 435 | |||
| 436 |
1/2✓ Branch 0 taken 919 times.
✗ Branch 1 not taken.
|
919 | if (do_refcount_) { |
| 437 |
2/4✓ Branch 4 taken 919 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 919 times.
✗ Branch 8 not taken.
|
919 | 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 44 times.
✓ Branch 1 taken 875 times.
|
919 | if (fd_rdonly == -1) |
| 442 | 44 | return -errno; | |
| 443 | 875 | return fd_rdonly; | |
| 444 | } | ||
| 445 | |||
| 446 | |||
| 447 | 9336 | int64_t PosixCacheManager::Pread(int fd, | |
| 448 | void *buf, | ||
| 449 | uint64_t size, | ||
| 450 | uint64_t offset) { | ||
| 451 | int64_t result; | ||
| 452 | do { | ||
| 453 | 9336 | errno = 0; | |
| 454 | 9336 | result = pread(fd, buf, size, offset); | |
| 455 |
3/4✓ Branch 0 taken 88 times.
✓ Branch 1 taken 9248 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 88 times.
|
9336 | } while ((result == -1) && (errno == EINTR)); |
| 456 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 9248 times.
|
9336 | if (result < 0) |
| 457 | 88 | return -errno; | |
| 458 | 9248 | return result; | |
| 459 | } | ||
| 460 | |||
| 461 | |||
| 462 | 5317 | int PosixCacheManager::Rename(const char *oldpath, const char *newpath) { | |
| 463 | int result; | ||
| 464 |
2/2✓ Branch 0 taken 5185 times.
✓ Branch 1 taken 132 times.
|
5317 | if (rename_workaround_ != kRenameLink) { |
| 465 | 5185 | result = rename(oldpath, newpath); | |
| 466 |
2/2✓ Branch 0 taken 132 times.
✓ Branch 1 taken 5053 times.
|
5185 | if (result < 0) |
| 467 | 132 | return -errno; | |
| 468 | 5053 | return 0; | |
| 469 | } | ||
| 470 | |||
| 471 | 132 | result = link(oldpath, newpath); | |
| 472 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 44 times.
|
132 | if (result < 0) { |
| 473 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 44 times.
|
88 | if (errno == EEXIST) { |
| 474 | 44 | LogCvmfs(kLogCache, kLogDebug, "%s already existed, ignoring", newpath); | |
| 475 | } else { | ||
| 476 | 44 | return -errno; | |
| 477 | } | ||
| 478 | } | ||
| 479 | 88 | result = unlink(oldpath); | |
| 480 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
|
88 | if (result < 0) |
| 481 | ✗ | return -errno; | |
| 482 | 88 | 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 | 628 | int PosixCacheManager::Readahead(int fd) { | |
| 493 | unsigned char *buf[4096]; | ||
| 494 | int nbytes; | ||
| 495 | 628 | uint64_t pos = 0; | |
| 496 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 628 times.
|
628 | if (is_tmpfs()) { |
| 497 | ✗ | return 0; | |
| 498 | } | ||
| 499 | do { | ||
| 500 |
1/2✓ Branch 1 taken 2932 times.
✗ Branch 2 not taken.
|
2932 | nbytes = Pread(fd, buf, 4096, pos); |
| 501 | 2932 | pos += nbytes; | |
| 502 |
2/2✓ Branch 0 taken 2304 times.
✓ Branch 1 taken 628 times.
|
2932 | } while (nbytes == 4096); |
| 503 |
1/2✓ Branch 1 taken 628 times.
✗ Branch 2 not taken.
|
628 | LogCvmfs(kLogCache, kLogDebug, "read-ahead %d, %" PRIu64, fd, pos); |
| 504 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 628 times.
|
628 | if (nbytes < 0) |
| 505 | ✗ | return nbytes; | |
| 506 | 628 | return 0; | |
| 507 | } | ||
| 508 | |||
| 509 | |||
| 510 | 366 | int PosixCacheManager::Reset(void *txn) { | |
| 511 | 366 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 512 | 366 | transaction->buf_pos = 0; | |
| 513 | 366 | transaction->size = 0; | |
| 514 | 366 | int retval = lseek(transaction->fd, 0, SEEK_SET); | |
| 515 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 322 times.
|
366 | if (retval < 0) |
| 516 | 44 | return -errno; | |
| 517 | 322 | retval = ftruncate(transaction->fd, 0); | |
| 518 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 322 times.
|
322 | if (retval < 0) |
| 519 | ✗ | return -errno; | |
| 520 | 322 | return 0; | |
| 521 | } | ||
| 522 | |||
| 523 | |||
| 524 | 6241 | int PosixCacheManager::StartTxn(const shash::Any &id, | |
| 525 | uint64_t size, | ||
| 526 | void *txn) { | ||
| 527 | 6241 | atomic_inc32(&no_inflight_txns_); | |
| 528 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 6197 times.
|
6241 | if (cache_mode_ == kCacheReadOnly) { |
| 529 | 44 | atomic_dec32(&no_inflight_txns_); | |
| 530 | 44 | return -EROFS; | |
| 531 | } | ||
| 532 | |||
| 533 |
2/2✓ Branch 0 taken 5280 times.
✓ Branch 1 taken 917 times.
|
6197 | if (size != kSizeUnknown) { |
| 534 |
3/4✓ Branch 1 taken 5280 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 44 times.
✓ Branch 4 taken 5236 times.
|
5280 | if (size > quota_mgr_->GetMaxFileSize()) { |
| 535 |
1/2✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
|
44 | LogCvmfs(kLogCache, kLogDebug, |
| 536 | "file too big for lru cache (%" PRIu64 " " | ||
| 537 | "requested but only %" PRIu64 " bytes free)", | ||
| 538 |
1/2✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
|
44 | size, quota_mgr_->GetMaxFileSize()); |
| 539 | 44 | atomic_dec32(&no_inflight_txns_); | |
| 540 | 44 | return -ENOSPC; | |
| 541 | } | ||
| 542 | |||
| 543 | // For large files, ensure enough free cache space before writing the chunk | ||
| 544 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 5192 times.
|
5236 | if (size > kBigFile) { |
| 545 |
1/2✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
|
44 | const uint64_t cache_size = quota_mgr_->GetSize(); |
| 546 |
1/2✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
|
44 | const uint64_t cache_capacity = quota_mgr_->GetCapacity(); |
| 547 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | assert(cache_capacity >= size); |
| 548 |
1/2✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
|
44 | if ((cache_size + size) > cache_capacity) { |
| 549 | 88 | const uint64_t leave_size = std::min(cache_capacity / 2, | |
| 550 | 44 | cache_capacity - size); | |
| 551 |
1/2✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
|
44 | quota_mgr_->Cleanup(leave_size); |
| 552 | } | ||
| 553 | } | ||
| 554 | } | ||
| 555 | |||
| 556 |
1/2✓ Branch 1 taken 6153 times.
✗ Branch 2 not taken.
|
6153 | string path_in_cache = GetPathInCache(id); |
| 557 |
1/2✓ Branch 2 taken 6153 times.
✗ Branch 3 not taken.
|
6153 | Transaction *transaction = new (txn) Transaction(id, path_in_cache); |
| 558 | |||
| 559 | 6153 | char *template_path = NULL; | |
| 560 | 6153 | unsigned temp_path_len = 0; | |
| 561 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 6065 times.
|
6153 | if (rename_workaround_ == kRenameSamedir) { |
| 562 | 88 | temp_path_len = path_in_cache.length() + 6; | |
| 563 | 88 | template_path = reinterpret_cast<char *>(alloca(temp_path_len + 1)); | |
| 564 | 88 | memcpy(template_path, path_in_cache.data(), path_in_cache.length()); | |
| 565 | 88 | memset(template_path + path_in_cache.length(), 'X', 6); | |
| 566 | } else { | ||
| 567 | 6065 | temp_path_len = txn_template_path_.length(); | |
| 568 | 6065 | template_path = reinterpret_cast<char *>(alloca(temp_path_len + 1)); | |
| 569 |
1/2✓ Branch 1 taken 6065 times.
✗ Branch 2 not taken.
|
6065 | memcpy(template_path, &txn_template_path_[0], temp_path_len); |
| 570 | } | ||
| 571 | 6153 | template_path[temp_path_len] = '\0'; | |
| 572 | |||
| 573 |
1/2✓ Branch 1 taken 6153 times.
✗ Branch 2 not taken.
|
6153 | transaction->fd = mkstemp(template_path); |
| 574 |
2/2✓ Branch 0 taken 81 times.
✓ Branch 1 taken 6072 times.
|
6153 | if (transaction->fd == -1) { |
| 575 | 81 | transaction->~Transaction(); | |
| 576 | 81 | atomic_dec32(&no_inflight_txns_); | |
| 577 | 81 | return -errno; | |
| 578 | } | ||
| 579 | |||
| 580 |
1/2✓ Branch 1 taken 6072 times.
✗ Branch 2 not taken.
|
6072 | LogCvmfs(kLogCache, kLogDebug, "start transaction on %s has result %d", |
| 581 | template_path, transaction->fd); | ||
| 582 |
1/2✓ Branch 1 taken 6072 times.
✗ Branch 2 not taken.
|
6072 | transaction->tmp_path = template_path; |
| 583 | 6072 | transaction->expected_size = size; | |
| 584 | 6072 | return transaction->fd; | |
| 585 | 6153 | } | |
| 586 | |||
| 587 | |||
| 588 | 564 | manifest::Breadcrumb PosixCacheManager::LoadBreadcrumb( | |
| 589 | const std::string &fqrn) { | ||
| 590 | 564 | return manifest::Manifest::ReadBreadcrumb(fqrn, cache_path_); | |
| 591 | } | ||
| 592 | |||
| 593 | |||
| 594 | 280 | bool PosixCacheManager::StoreBreadcrumb(const manifest::Manifest &manifest) { | |
| 595 | 280 | 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 | 176 | void PosixCacheManager::TearDown2ReadOnly() { | |
| 606 | 176 | cache_mode_ = kCacheReadOnly; | |
| 607 |
2/2✓ Branch 1 taken 616 times.
✓ Branch 2 taken 132 times.
|
748 | while (atomic_read32(&no_inflight_txns_) != 0) |
| 608 | 616 | SafeSleepMs(50); | |
| 609 | |||
| 610 | 132 | QuotaManager *old_manager = quota_mgr_; | |
| 611 |
1/2✓ Branch 2 taken 132 times.
✗ Branch 3 not taken.
|
132 | quota_mgr_ = new NoopQuotaManager(); |
| 612 |
1/2✓ Branch 0 taken 132 times.
✗ Branch 1 not taken.
|
132 | delete old_manager; |
| 613 | 132 | } | |
| 614 | |||
| 615 | |||
| 616 | 5930 | int64_t PosixCacheManager::Write(const void *buf, uint64_t size, void *txn) { | |
| 617 | 5930 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 618 | |||
| 619 |
2/2✓ Branch 0 taken 4752 times.
✓ Branch 1 taken 1178 times.
|
5930 | if (transaction->expected_size != kSizeUnknown) { |
| 620 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 4708 times.
|
4752 | if (transaction->size + size > transaction->expected_size) { |
| 621 | 44 | LogCvmfs(kLogCache, kLogDebug, | |
| 622 | "Transaction size (%" PRIu64 ") > expected size (%" PRIu64 ")", | ||
| 623 | 44 | transaction->size + size, transaction->expected_size); | |
| 624 | 44 | return -EFBIG; | |
| 625 | } | ||
| 626 | } | ||
| 627 | |||
| 628 | 5886 | uint64_t written = 0; | |
| 629 | 5886 | const unsigned char *read_pos = reinterpret_cast<const unsigned char *>(buf); | |
| 630 |
2/2✓ Branch 0 taken 6917 times.
✓ Branch 1 taken 5842 times.
|
12759 | while (written < size) { |
| 631 |
2/2✓ Branch 0 taken 2768 times.
✓ Branch 1 taken 4149 times.
|
6917 | if (transaction->buf_pos == sizeof(transaction->buffer)) { |
| 632 |
1/2✓ Branch 1 taken 2768 times.
✗ Branch 2 not taken.
|
2768 | const int retval = Flush(transaction); |
| 633 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 2724 times.
|
2768 | if (retval != 0) { |
| 634 | 44 | transaction->size += written; | |
| 635 | 44 | return retval; | |
| 636 | } | ||
| 637 | } | ||
| 638 | 6873 | const uint64_t remaining = size - written; | |
| 639 | 6873 | const uint64_t space_in_buffer = sizeof(transaction->buffer) | |
| 640 | 6873 | - transaction->buf_pos; | |
| 641 | 6873 | const uint64_t batch_size = std::min(remaining, space_in_buffer); | |
| 642 | 6873 | memcpy(transaction->buffer + transaction->buf_pos, read_pos, batch_size); | |
| 643 | 6873 | transaction->buf_pos += batch_size; | |
| 644 | 6873 | written += batch_size; | |
| 645 | 6873 | read_pos += batch_size; | |
| 646 | } | ||
| 647 | 5842 | transaction->size += written; | |
| 648 | 5842 | return written; | |
| 649 | } | ||
| 650 | |||
| 651 |