GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/fuse_evict.cc
Date: 2026-08-30 02:40:36
Exec Total Coverage
Lines: 109 153 71.2%
Branches: 62 127 48.8%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #include "fuse_evict.h"
6
7 #include <inttypes.h>
8 #include <stdint.h>
9
10 #include <cassert>
11 #include <cstdlib>
12 #include <cstring>
13 #include <new>
14 #include <vector>
15
16 #include "glue_buffer.h"
17 #include "mountpoint.h"
18 #include "shortstring.h"
19 #include "util/logging.h"
20 #include "util/posix.h"
21 #include "util/smalloc.h"
22
23 using namespace std; // NOLINT
24
25 176 FuseInvalidator::Handle::Handle(unsigned timeout_s)
26
2/2
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 88 times.
176 : timeout_s_((timeout_s == 0) ? 0 : (timeout_s + kTimeoutSafetyMarginSec)) {
27 176 status_ = reinterpret_cast<atomic_int32 *>(smalloc(sizeof(atomic_int32)));
28 176 atomic_init32(status_);
29 176 }
30
31
32 176 FuseInvalidator::Handle::~Handle() { free(status_); }
33
34
35 220 void FuseInvalidator::Handle::WaitFor() {
36
2/2
✓ Branch 1 taken 264 times.
✓ Branch 2 taken 220 times.
484 while (!IsDone())
37 264 SafeSleepMs(FuseInvalidator::kCheckTimeoutFreqMs);
38 220 }
39
40
41 //------------------------------------------------------------------------------
42
43
44 const unsigned FuseInvalidator::kTimeoutSafetyMarginSec = 1;
45 const unsigned FuseInvalidator::kCheckTimeoutFreqMs = 100;
46 const unsigned FuseInvalidator::kCheckTimeoutFreqOps = 256;
47
48 bool FuseInvalidator::g_fuse_notify_invalidation_ = true;
49
50 132 bool FuseInvalidator::HasFuseNotifyInval() {
51 132 return FuseInvalidator::g_fuse_notify_invalidation_;
52 }
53
54
55 FuseInvalidator::FuseInvalidator(MountPoint *mount_point,
56 struct fuse_session **fuse_session,
57 bool fuse_notify_invalidation)
58 : mount_point_(mount_point)
59 , inode_tracker_(mount_point->inode_tracker())
60 , dentry_tracker_(mount_point->dentry_tracker())
61 , fuse_session_(fuse_session)
62 , spawned_(false) {
63 g_fuse_notify_invalidation_ = fuse_notify_invalidation;
64 memset(&thread_invalidator_, 0, sizeof(thread_invalidator_));
65 atomic_init32(&terminated_);
66 }
67
68 220 FuseInvalidator::FuseInvalidator(glue::InodeTracker *inode_tracker,
69 glue::DentryTracker *dentry_tracker,
70 struct fuse_session **fuse_session,
71 220 bool fuse_notify_invalidation)
72 220 : mount_point_(NULL)
73 220 , inode_tracker_(inode_tracker)
74 220 , dentry_tracker_(dentry_tracker)
75 220 , fuse_session_(fuse_session)
76
1/2
✓ Branch 3 taken 220 times.
✗ Branch 4 not taken.
220 , spawned_(false) {
77 220 g_fuse_notify_invalidation_ = fuse_notify_invalidation;
78 220 memset(&thread_invalidator_, 0, sizeof(thread_invalidator_));
79 220 atomic_init32(&terminated_);
80 220 }
81
82
83 220 FuseInvalidator::~FuseInvalidator() {
84 220 atomic_cas32(&terminated_, 0, 1);
85
2/2
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 44 times.
220 if (spawned_) {
86 176 QuitCommand *cmd = new (smalloc(sizeof(QuitCommand))) QuitCommand();
87 176 channel_.PushBack(cmd);
88 176 pthread_join(thread_invalidator_, NULL);
89 }
90 220 }
91
92
93 220 void FuseInvalidator::InvalidateInodes(Handle *handle) {
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 220 times.
220 assert(handle != NULL);
95 InvalInodesCommand *inval_inodes_command = new (
96 220 smalloc(sizeof(InvalInodesCommand))) InvalInodesCommand();
97 220 inval_inodes_command->handle = handle;
98 220 channel_.PushBack(inval_inodes_command);
99 220 }
100
101 void FuseInvalidator::InvalidateDentry(uint64_t parent_ino,
102 const NameString &name) {
103 InvalDentryCommand *inval_dentry_command;
104 vector<Command *> *items = channel_.StartEnqueueing();
105 for (size_t i = 0; i < items->size(); ++i) {
106 inval_dentry_command = dynamic_cast<InvalDentryCommand *>(items->at(i));
107 if (!inval_dentry_command)
108 continue;
109 if (inval_dentry_command->parent_ino != parent_ino)
110 continue;
111 if (inval_dentry_command->name != name)
112 continue;
113 channel_.AbortEnqueueing();
114 return;
115 }
116
117 inval_dentry_command = new (smalloc(sizeof(InvalDentryCommand)))
118 InvalDentryCommand();
119 inval_dentry_command->parent_ino = parent_ino;
120 inval_dentry_command->name = name;
121 items->push_back(inval_dentry_command);
122 channel_.CommitEnqueueing();
123 }
124
125 176 void *FuseInvalidator::MainInvalidator(void *data) {
126 176 FuseInvalidator *invalidator = reinterpret_cast<FuseInvalidator *>(data);
127 176 LogCvmfs(kLogCvmfs, kLogDebug, "starting dentry invalidator thread");
128
129 176 bool reported_missing_inval_support = false;
130 while (true) {
131
1/2
✓ Branch 1 taken 396 times.
✗ Branch 2 not taken.
396 Command *command = invalidator->channel_.PopFront();
132
133
3/4
✓ Branch 0 taken 396 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 176 times.
✓ Branch 3 taken 220 times.
396 if (dynamic_cast<QuitCommand *>(command)) {
134 176 command->~Command();
135 176 free(command);
136 176 break;
137 }
138
139 InvalDentryCommand
140
1/2
✓ Branch 0 taken 220 times.
✗ Branch 1 not taken.
220 *inval_dentry_command = dynamic_cast<InvalDentryCommand *>(command);
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 220 times.
220 if (inval_dentry_command) {
142 if (invalidator->fuse_session_ == NULL) {
143 if (!reported_missing_inval_support) {
144 LogCvmfs(kLogCvmfs, kLogSyslogWarn,
145 "missing fuse support for dentry invalidation "
146 "(%" PRIu64 "/%s)",
147 inval_dentry_command->parent_ino,
148 inval_dentry_command->name.ToString().c_str());
149 reported_missing_inval_support = true;
150 }
151 inval_dentry_command->~InvalDentryCommand();
152 free(inval_dentry_command);
153 88 continue;
154 }
155 LogCvmfs(kLogCvmfs, kLogDebug, "evicting single dentry %" PRIu64 "/%s",
156 inval_dentry_command->parent_ino,
157 inval_dentry_command->name.ToString().c_str());
158 fuse_lowlevel_notify_inval_entry(
159 *invalidator->fuse_session_, inval_dentry_command->parent_ino,
160 inval_dentry_command->name.GetChars(),
161 inval_dentry_command->name.GetLength());
162 inval_dentry_command->~InvalDentryCommand();
163 free(inval_dentry_command);
164 continue;
165 }
166
167 InvalInodesCommand
168
1/2
✓ Branch 0 taken 220 times.
✗ Branch 1 not taken.
220 *inval_inodes_command = dynamic_cast<InvalInodesCommand *>(command);
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 220 times.
220 assert(inval_inodes_command);
170
171 220 Handle *handle = inval_inodes_command->handle;
172
1/2
✓ Branch 1 taken 220 times.
✗ Branch 2 not taken.
220 LogCvmfs(kLogCvmfs, kLogDebug, "invalidating kernel caches, timeout %u",
173 handle->timeout_s_);
174 220 inval_inodes_command->~InvalInodesCommand();
175 220 free(inval_inodes_command);
176
177 220 const uint64_t deadline = platform_monotonic_time() + handle->timeout_s_;
178
179 // Fallback: drainout by timeout
180
5/6
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 88 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 132 times.
✓ Branch 5 taken 88 times.
✓ Branch 6 taken 132 times.
220 if ((invalidator->fuse_session_ == NULL) || !HasFuseNotifyInval()) {
181
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 44 times.
88 while (platform_monotonic_time() < deadline) {
182
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 SafeSleepMs(kCheckTimeoutFreqMs);
183
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 if (atomic_read32(&invalidator->terminated_) == 1) {
184
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 LogCvmfs(kLogCvmfs, kLogDebug,
185 "cancel cache eviction due to termination");
186 44 break;
187 }
188 }
189 88 handle->SetDone();
190 88 continue;
191 }
192
193 // We must not hold a lock when calling fuse_lowlevel_notify_inval_entry.
194 // Therefore, we first copy all the inodes into a temporary data structure.
195 glue::InodeTracker::Cursor inode_cursor(
196
1/2
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
132 invalidator->inode_tracker_->BeginEnumerate());
197 uint64_t inode;
198
3/4
✓ Branch 1 taken 135300 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 135168 times.
✓ Branch 4 taken 132 times.
135300 while (invalidator->inode_tracker_->NextInode(&inode_cursor, &inode)) {
199
1/2
✓ Branch 1 taken 135168 times.
✗ Branch 2 not taken.
135168 invalidator->evict_list_.PushBack(inode);
200 }
201
1/2
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
132 invalidator->inode_tracker_->EndEnumerate(&inode_cursor);
202
203 132 unsigned i = 0;
204 132 const unsigned N = invalidator->evict_list_.size();
205
2/2
✓ Branch 0 taken 67584 times.
✓ Branch 1 taken 44 times.
67628 while (i < N) {
206 67584 uint64_t inode = invalidator->evict_list_.At(i);
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67584 times.
67584 if (inode == 0)
208 inode = FUSE_ROOT_ID;
209 // Can fail, e.g. the inode might be already evicted
210
211 67584 const int dbg_retval = fuse_lowlevel_notify_inval_inode(
212
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
67584 *invalidator->fuse_session_, inode, 0, 0);
213
1/2
✓ Branch 1 taken 67584 times.
✗ Branch 2 not taken.
67584 LogCvmfs(kLogCvmfs, kLogDebug,
214 "evicting inode %" PRIu64 " with retval: %d", inode, dbg_retval);
215
216 (void)dbg_retval; // prevent compiler complaining
217
218
2/2
✓ Branch 0 taken 264 times.
✓ Branch 1 taken 67320 times.
67584 if ((++i % kCheckTimeoutFreqOps) == 0) {
219
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 220 times.
264 if (platform_monotonic_time() >= deadline) {
220
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 LogCvmfs(kLogCvmfs, kLogDebug,
221 "cancel cache eviction after %u entries due to timeout", i);
222 44 break;
223 }
224
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 176 times.
220 if (atomic_read32(&invalidator->terminated_) == 1) {
225
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 LogCvmfs(kLogCvmfs, kLogDebug,
226 "cancel cache eviction due to termination");
227 44 break;
228 }
229 }
230 }
231
232 // Do the dentry tracker last to increase the effectiveness of pruning
233
1/2
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
132 invalidator->dentry_tracker_->Prune();
234 // Copy and empty the dentry tracker in a single atomic operation
235
1/2
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
132 glue::DentryTracker *dentries_copy = invalidator->dentry_tracker_->Move();
236
1/2
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
132 glue::DentryTracker::Cursor dentry_cursor = dentries_copy->BeginEnumerate();
237 uint64_t entry_parent;
238 132 NameString entry_name;
239 132 i = 0;
240
241 int (*notify_func)(struct fuse_session *, fuse_ino_t, const char *, size_t);
242 132 notify_func = &fuse_lowlevel_notify_inval_entry;
243 #if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 16)
244 // must be libfuse >= 3.16, otherwise the signature is wrong and it
245 // will fail building
246 // mount_point can only be NULL for unittests
247 if (invalidator->mount_point_ != NULL
248 && invalidator->mount_point_->fuse_expire_entry()) {
249 notify_func = &fuse_lowlevel_notify_expire_entry;
250 }
251 #endif
252
253 132 while (
254
3/4
✓ Branch 1 taken 56408 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 56320 times.
✓ Branch 4 taken 88 times.
56408 dentries_copy->NextEntry(&dentry_cursor, &entry_parent, &entry_name)) {
255
1/2
✓ Branch 2 taken 56320 times.
✗ Branch 3 not taken.
56320 LogCvmfs(kLogCvmfs, kLogDebug, "evicting dentry %lu --> %s", entry_parent,
256 entry_name.c_str());
257 // Can fail, e.g. the entry might be already evicted
258
1/2
✓ Branch 2 taken 56320 times.
✗ Branch 3 not taken.
56320 notify_func(*invalidator->fuse_session_, entry_parent,
259 56320 entry_name.GetChars(), entry_name.GetLength());
260
261
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 56100 times.
56320 if ((++i % kCheckTimeoutFreqOps) == 0) {
262
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 176 times.
220 if (atomic_read32(&invalidator->terminated_) == 1) {
263
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 LogCvmfs(kLogCvmfs, kLogDebug,
264 "cancel cache eviction due to termination");
265 44 break;
266 }
267 }
268 }
269
1/2
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
132 dentries_copy->EndEnumerate(&dentry_cursor);
270
1/2
✓ Branch 0 taken 132 times.
✗ Branch 1 not taken.
132 delete dentries_copy;
271
272 132 handle->SetDone();
273
1/2
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
132 invalidator->evict_list_.Clear();
274 352 }
275
276 176 LogCvmfs(kLogCvmfs, kLogDebug, "stopping dentry invalidator thread");
277 176 return NULL;
278 }
279
280
281 176 void FuseInvalidator::Spawn() {
282 int retval;
283 176 retval = pthread_create(&thread_invalidator_, NULL, MainInvalidator, this);
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 176 times.
176 assert(retval == 0);
285 176 spawned_ = true;
286 176 }
287