Commit | Line | Data |
---|---|---|
df038819 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2018-2020 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
df038819 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
df038819 | 5 | * |
df038819 JG |
6 | */ |
7 | ||
28ab034a JG |
8 | #include "fd-tracker.hpp" |
9 | #include "inode.hpp" | |
df038819 | 10 | |
c9e313bc SM |
11 | #include <common/defaults.hpp> |
12 | #include <common/error.hpp> | |
13 | #include <common/fs-handle-internal.hpp> | |
14 | #include <common/hashtable/hashtable.hpp> | |
15 | #include <common/hashtable/utils.hpp> | |
16 | #include <common/macros.hpp> | |
17 | #include <common/optional.hpp> | |
56047f5a | 18 | #include <common/urcu.hpp> |
c9e313bc | 19 | |
28ab034a JG |
20 | #include <fcntl.h> |
21 | #include <inttypes.h> | |
22 | #include <pthread.h> | |
23 | #include <stdbool.h> | |
24 | #include <sys/stat.h> | |
25 | #include <sys/types.h> | |
26 | #include <urcu.h> | |
27 | #include <urcu/list.h> | |
28 | #include <urcu/rculfhash.h> | |
df038819 JG |
29 | |
30 | /* Tracker lock must be taken by the user. */ | |
5c7248cd JG |
31 | #define TRACKED_COUNT(tracker) \ |
32 | ((tracker)->count.suspendable.active + (tracker)->count.suspendable.suspended + \ | |
33 | (tracker)->count.unsuspendable) | |
df038819 JG |
34 | |
35 | /* Tracker lock must be taken by the user. */ | |
5c7248cd | 36 | #define ACTIVE_COUNT(tracker) ((tracker)->count.suspendable.active + (tracker)->count.unsuspendable) |
df038819 JG |
37 | |
38 | /* Tracker lock must be taken by the user. */ | |
5c7248cd | 39 | #define SUSPENDED_COUNT(tracker) ((tracker)->count.suspendable.suspended) |
df038819 JG |
40 | |
41 | /* Tracker lock must be taken by the user. */ | |
28ab034a | 42 | #define SUSPENDABLE_COUNT(tracker) \ |
5c7248cd | 43 | ((tracker)->count.suspendable.active + (tracker)->count.suspendable.suspended) |
df038819 JG |
44 | |
45 | /* Tracker lock must be taken by the user. */ | |
5c7248cd | 46 | #define UNSUSPENDABLE_COUNT(tracker) ((tracker)->count.unsuspendable) |
df038819 JG |
47 | |
48 | struct fd_tracker { | |
49 | pthread_mutex_t lock; | |
50 | struct { | |
5c1f54d1 | 51 | struct { |
df038819 JG |
52 | unsigned int active; |
53 | unsigned int suspended; | |
54 | } suspendable; | |
55 | unsigned int unsuspendable; | |
56 | } count; | |
57 | unsigned int capacity; | |
58 | struct { | |
59 | uint64_t uses; | |
60 | uint64_t misses; | |
61 | /* Failures to suspend or restore fs handles. */ | |
62 | uint64_t errors; | |
63 | } stats; | |
64 | /* | |
65 | * The head of the active_handles list is always the least recently | |
66 | * used active handle. When an handle is used, it is removed from the | |
67 | * list and added to the end. When a file has to be suspended, the | |
68 | * first element in the list is "popped", suspended, and added to the | |
69 | * list of suspended handles. | |
70 | */ | |
71 | struct cds_list_head active_handles; | |
72 | struct cds_list_head suspended_handles; | |
73 | struct cds_lfht *unsuspendable_fds; | |
874a3441 | 74 | struct lttng_inode_registry *inode_registry; |
f7c3ffd7 JG |
75 | /* Unlinked files are moved in this directory under a unique name. */ |
76 | struct lttng_directory_handle *unlink_directory_handle; | |
77 | struct lttng_unlinked_file_pool *unlinked_file_pool; | |
df038819 JG |
78 | }; |
79 | ||
f1494934 | 80 | namespace { |
df038819 | 81 | struct open_properties { |
df038819 | 82 | int flags; |
1e4f5625 | 83 | LTTNG_OPTIONAL(mode_t) mode; |
df038819 JG |
84 | }; |
85 | ||
86 | /* | |
f5ea0241 | 87 | * A fs_handle_tracked is not ref-counted. Therefore, it is assumed that a |
df038819 JG |
88 | * handle is never in-use while it is being reclaimed. It can be |
89 | * shared by multiple threads, but external synchronization is required | |
90 | * to ensure it is not still being used when it is reclaimed (close method). | |
91 | * In this respect, it is not different from a regular file descriptor. | |
92 | * | |
93 | * The fs_handle lock always nests _within_ the tracker's lock. | |
94 | */ | |
f5ea0241 JG |
95 | struct fs_handle_tracked { |
96 | struct fs_handle parent; | |
df038819 JG |
97 | pthread_mutex_t lock; |
98 | /* | |
99 | * Weak reference to the tracker. All fs_handles are assumed to have | |
100 | * been closed at the moment of the destruction of the fd_tracker. | |
101 | */ | |
102 | struct fd_tracker *tracker; | |
103 | struct open_properties properties; | |
874a3441 | 104 | struct lttng_inode *inode; |
df038819 JG |
105 | int fd; |
106 | /* inode number of the file at the time of the handle's creation. */ | |
107 | uint64_t ino; | |
108 | bool in_use; | |
109 | /* Offset to which the file should be restored. */ | |
110 | off_t offset; | |
111 | struct cds_list_head handles_list_node; | |
112 | }; | |
113 | ||
114 | struct unsuspendable_fd { | |
115 | /* | |
116 | * Accesses are only performed through the tracker, which is protected | |
117 | * by its own lock. | |
118 | */ | |
119 | int fd; | |
120 | char *name; | |
121 | struct cds_lfht_node tracker_node; | |
122 | struct rcu_head rcu_head; | |
123 | }; | |
124 | ||
f1494934 | 125 | struct { |
df038819 JG |
126 | pthread_mutex_t lock; |
127 | bool initialized; | |
128 | unsigned long value; | |
129 | } seed = { | |
130 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
1c9a0b0e MJ |
131 | .initialized = false, |
132 | .value = 0, | |
df038819 | 133 | }; |
f1494934 | 134 | } /* namespace */ |
df038819 JG |
135 | |
136 | static int match_fd(struct cds_lfht_node *node, const void *key); | |
137 | static void unsuspendable_fd_destroy(struct unsuspendable_fd *entry); | |
28ab034a | 138 | static struct unsuspendable_fd *unsuspendable_fd_create(const char *name, int fd); |
f7c3ffd7 | 139 | static int open_from_properties(const struct lttng_directory_handle *dir_handle, |
28ab034a JG |
140 | const char *path, |
141 | struct open_properties *properties); | |
df038819 | 142 | |
f5ea0241 JG |
143 | static void fs_handle_tracked_log(struct fs_handle_tracked *handle); |
144 | static int fs_handle_tracked_suspend(struct fs_handle_tracked *handle); | |
145 | static int fs_handle_tracked_restore(struct fs_handle_tracked *handle); | |
146 | static int fs_handle_tracked_get_fd(struct fs_handle *_handle); | |
147 | static void fs_handle_tracked_put_fd(struct fs_handle *_handle); | |
148 | static int fs_handle_tracked_unlink(struct fs_handle *_handle); | |
149 | static int fs_handle_tracked_close(struct fs_handle *_handle); | |
df038819 | 150 | |
28ab034a JG |
151 | static void fd_tracker_track(struct fd_tracker *tracker, struct fs_handle_tracked *handle); |
152 | static void fd_tracker_untrack(struct fd_tracker *tracker, struct fs_handle_tracked *handle); | |
153 | static int fd_tracker_suspend_handles(struct fd_tracker *tracker, unsigned int count); | |
154 | static int fd_tracker_restore_handle(struct fd_tracker *tracker, struct fs_handle_tracked *handle); | |
f5ea0241 | 155 | |
df038819 | 156 | /* Match function of the tracker's unsuspendable_fds hash table. */ |
5c1f54d1 | 157 | static int match_fd(struct cds_lfht_node *node, const void *key) |
df038819 | 158 | { |
28ab034a JG |
159 | struct unsuspendable_fd *entry = |
160 | lttng::utils::container_of(node, &unsuspendable_fd::tracker_node); | |
df038819 | 161 | |
28ab034a | 162 | return hash_match_key_ulong((void *) (unsigned long) entry->fd, (void *) key); |
df038819 JG |
163 | } |
164 | ||
5c1f54d1 | 165 | static void delete_unsuspendable_fd(struct rcu_head *head) |
df038819 | 166 | { |
28ab034a | 167 | struct unsuspendable_fd *fd = caa_container_of(head, struct unsuspendable_fd, rcu_head); |
df038819 JG |
168 | |
169 | free(fd->name); | |
170 | free(fd); | |
171 | } | |
172 | ||
5c1f54d1 | 173 | static void unsuspendable_fd_destroy(struct unsuspendable_fd *entry) |
df038819 JG |
174 | { |
175 | if (!entry) { | |
176 | return; | |
177 | } | |
178 | call_rcu(&entry->rcu_head, delete_unsuspendable_fd); | |
179 | } | |
180 | ||
28ab034a | 181 | static struct unsuspendable_fd *unsuspendable_fd_create(const char *name, int fd) |
df038819 | 182 | { |
64803277 | 183 | struct unsuspendable_fd *entry = zmalloc<unsuspendable_fd>(); |
df038819 JG |
184 | |
185 | if (!entry) { | |
186 | goto error; | |
187 | } | |
188 | if (name) { | |
189 | entry->name = strdup(name); | |
190 | if (!entry->name) { | |
191 | goto error; | |
192 | } | |
193 | } | |
194 | cds_lfht_node_init(&entry->tracker_node); | |
195 | entry->fd = fd; | |
196 | return entry; | |
197 | error: | |
198 | unsuspendable_fd_destroy(entry); | |
cd9adb8b | 199 | return nullptr; |
df038819 JG |
200 | } |
201 | ||
f5ea0241 | 202 | static void fs_handle_tracked_log(struct fs_handle_tracked *handle) |
df038819 | 203 | { |
874a3441 JG |
204 | const char *path; |
205 | ||
df038819 | 206 | pthread_mutex_lock(&handle->lock); |
cd9adb8b | 207 | lttng_inode_borrow_location(handle->inode, nullptr, &path); |
874a3441 | 208 | |
df038819 | 209 | if (handle->fd >= 0) { |
28ab034a JG |
210 | DBG_NO_LOC(" %s [active, fd %d%s]", |
211 | path, | |
212 | handle->fd, | |
213 | handle->in_use ? ", in use" : ""); | |
df038819 | 214 | } else { |
874a3441 | 215 | DBG_NO_LOC(" %s [suspended]", path); |
df038819 JG |
216 | } |
217 | pthread_mutex_unlock(&handle->lock); | |
218 | } | |
219 | ||
874a3441 | 220 | /* Tracker lock must be held by the caller. */ |
f5ea0241 | 221 | static int fs_handle_tracked_suspend(struct fs_handle_tracked *handle) |
df038819 JG |
222 | { |
223 | int ret = 0; | |
224 | struct stat fs_stat; | |
874a3441 | 225 | const char *path; |
f7c3ffd7 | 226 | const struct lttng_directory_handle *node_directory_handle; |
df038819 JG |
227 | |
228 | pthread_mutex_lock(&handle->lock); | |
28ab034a | 229 | lttng_inode_borrow_location(handle->inode, &node_directory_handle, &path); |
a0377dfe | 230 | LTTNG_ASSERT(handle->fd >= 0); |
df038819 JG |
231 | if (handle->in_use) { |
232 | /* This handle can't be suspended as it is currently in use. */ | |
233 | ret = -EAGAIN; | |
234 | goto end; | |
235 | } | |
236 | ||
28ab034a | 237 | ret = lttng_directory_handle_stat(node_directory_handle, path, &fs_stat); |
df038819 | 238 | if (ret) { |
28ab034a | 239 | PERROR("Filesystem handle to %s cannot be suspended as stat() failed", path); |
df038819 JG |
240 | ret = -errno; |
241 | goto end; | |
242 | } | |
243 | ||
244 | if (fs_stat.st_ino != handle->ino) { | |
245 | /* Don't suspend as the handle would not be restorable. */ | |
28ab034a | 246 | WARN("Filesystem handle to %s cannot be suspended as its inode changed", path); |
df038819 JG |
247 | ret = -ENOENT; |
248 | goto end; | |
249 | } | |
250 | ||
5c1f54d1 | 251 | handle->offset = lseek(handle->fd, 0, SEEK_CUR); |
df038819 JG |
252 | if (handle->offset == -1) { |
253 | WARN("Filesystem handle to %s cannot be suspended as lseek() failed to sample its current position", | |
28ab034a | 254 | path); |
df038819 JG |
255 | ret = -errno; |
256 | goto end; | |
257 | } | |
258 | ||
259 | ret = close(handle->fd); | |
260 | if (ret) { | |
28ab034a | 261 | PERROR("Filesystem handle to %s cannot be suspended as close() failed", path); |
df038819 JG |
262 | ret = -errno; |
263 | goto end; | |
264 | } | |
265 | DBG("Suspended filesystem handle to %s (fd %i) at position %" PRId64, | |
28ab034a JG |
266 | path, |
267 | handle->fd, | |
268 | handle->offset); | |
df038819 JG |
269 | handle->fd = -1; |
270 | end: | |
271 | if (ret) { | |
272 | handle->tracker->stats.errors++; | |
273 | } | |
274 | pthread_mutex_unlock(&handle->lock); | |
275 | return ret; | |
276 | } | |
277 | ||
278 | /* Caller must hold the tracker and handle's locks. */ | |
f5ea0241 | 279 | static int fs_handle_tracked_restore(struct fs_handle_tracked *handle) |
df038819 JG |
280 | { |
281 | int ret, fd = -1; | |
f7c3ffd7 JG |
282 | const char *path; |
283 | const struct lttng_directory_handle *node_directory_handle; | |
284 | ||
28ab034a | 285 | lttng_inode_borrow_location(handle->inode, &node_directory_handle, &path); |
df038819 | 286 | |
a0377dfe FD |
287 | LTTNG_ASSERT(handle->fd == -1); |
288 | LTTNG_ASSERT(path); | |
28ab034a | 289 | ret = open_from_properties(node_directory_handle, path, &handle->properties); |
df038819 | 290 | if (ret < 0) { |
28ab034a | 291 | PERROR("Failed to restore filesystem handle to %s, open() failed", path); |
df038819 JG |
292 | ret = -errno; |
293 | goto end; | |
294 | } | |
295 | fd = ret; | |
296 | ||
297 | ret = lseek(fd, handle->offset, SEEK_SET); | |
298 | if (ret < 0) { | |
28ab034a | 299 | PERROR("Failed to restore filesystem handle to %s, lseek() failed", path); |
df038819 JG |
300 | ret = -errno; |
301 | goto end; | |
302 | } | |
303 | DBG("Restored filesystem handle to %s (fd %i) at position %" PRId64, | |
28ab034a JG |
304 | path, |
305 | fd, | |
306 | handle->offset); | |
df038819 JG |
307 | ret = 0; |
308 | handle->fd = fd; | |
309 | fd = -1; | |
310 | end: | |
311 | if (fd >= 0) { | |
312 | (void) close(fd); | |
313 | } | |
314 | return ret; | |
315 | } | |
316 | ||
f7c3ffd7 | 317 | static int open_from_properties(const struct lttng_directory_handle *dir_handle, |
28ab034a JG |
318 | const char *path, |
319 | struct open_properties *properties) | |
df038819 JG |
320 | { |
321 | int ret; | |
322 | ||
323 | /* | |
324 | * open() ignores the 'flags' parameter unless the O_CREAT or O_TMPFILE | |
325 | * flags are set. O_TMPFILE would not make sense in the context of a | |
326 | * suspendable fs_handle as it would not be restorable (see OPEN(2)), | |
327 | * thus it is ignored here. | |
328 | */ | |
329 | if ((properties->flags & O_CREAT) && properties->mode.is_set) { | |
28ab034a JG |
330 | ret = lttng_directory_handle_open_file( |
331 | dir_handle, path, properties->flags, properties->mode.value); | |
df038819 | 332 | } else { |
28ab034a | 333 | ret = lttng_directory_handle_open_file(dir_handle, path, properties->flags, 0); |
df038819 JG |
334 | } |
335 | /* | |
336 | * Some flags should not be used beyond the initial open() of a | |
337 | * restorable file system handle. O_CREAT and O_TRUNC must | |
338 | * be cleared since it would be unexpected to re-use them | |
339 | * when the handle is retored: | |
340 | * - O_CREAT should not be needed as the file has been created | |
341 | * on the initial call to open(), | |
342 | * - O_TRUNC would destroy the file's contents by truncating it | |
343 | * to length 0. | |
344 | */ | |
345 | properties->flags &= ~(O_CREAT | O_TRUNC); | |
346 | if (ret < 0) { | |
347 | ret = -errno; | |
348 | goto end; | |
349 | } | |
350 | end: | |
351 | return ret; | |
352 | } | |
353 | ||
28ab034a | 354 | struct fd_tracker *fd_tracker_create(const char *unlinked_file_path, unsigned int capacity) |
df038819 | 355 | { |
64803277 | 356 | struct fd_tracker *tracker = zmalloc<fd_tracker>(); |
df038819 JG |
357 | |
358 | if (!tracker) { | |
359 | goto end; | |
360 | } | |
361 | ||
362 | pthread_mutex_lock(&seed.lock); | |
363 | if (!seed.initialized) { | |
cd9adb8b | 364 | seed.value = (unsigned long) time(nullptr); |
df038819 JG |
365 | seed.initialized = true; |
366 | } | |
367 | pthread_mutex_unlock(&seed.lock); | |
368 | ||
369 | CDS_INIT_LIST_HEAD(&tracker->active_handles); | |
370 | CDS_INIT_LIST_HEAD(&tracker->suspended_handles); | |
371 | tracker->capacity = capacity; | |
28ab034a | 372 | tracker->unsuspendable_fds = cds_lfht_new( |
cd9adb8b | 373 | DEFAULT_HT_SIZE, 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, nullptr); |
874a3441 JG |
374 | if (!tracker->unsuspendable_fds) { |
375 | ERR("Failed to create fd-tracker's unsuspendable_fds hash table"); | |
376 | goto error; | |
377 | } | |
378 | tracker->inode_registry = lttng_inode_registry_create(); | |
379 | if (!tracker->inode_registry) { | |
380 | ERR("Failed to create fd-tracker's inode registry"); | |
381 | goto error; | |
382 | } | |
f7c3ffd7 | 383 | |
28ab034a | 384 | tracker->unlinked_file_pool = lttng_unlinked_file_pool_create(unlinked_file_path); |
f7c3ffd7 JG |
385 | if (!tracker->unlinked_file_pool) { |
386 | goto error; | |
387 | } | |
df038819 | 388 | DBG("File descriptor tracker created with a limit of %u simultaneously-opened FDs", |
28ab034a | 389 | capacity); |
df038819 JG |
390 | end: |
391 | return tracker; | |
874a3441 JG |
392 | error: |
393 | fd_tracker_destroy(tracker); | |
cd9adb8b | 394 | return nullptr; |
df038819 JG |
395 | } |
396 | ||
397 | void fd_tracker_log(struct fd_tracker *tracker) | |
398 | { | |
f5ea0241 | 399 | struct fs_handle_tracked *handle; |
df038819 JG |
400 | struct unsuspendable_fd *unsuspendable_fd; |
401 | struct cds_lfht_iter iter; | |
402 | ||
403 | pthread_mutex_lock(&tracker->lock); | |
404 | DBG_NO_LOC("File descriptor tracker"); | |
405 | DBG_NO_LOC(" Stats:"); | |
406 | DBG_NO_LOC(" uses: %" PRIu64, tracker->stats.uses); | |
407 | DBG_NO_LOC(" misses: %" PRIu64, tracker->stats.misses); | |
408 | DBG_NO_LOC(" errors: %" PRIu64, tracker->stats.errors); | |
409 | DBG_NO_LOC(" Tracked: %u", TRACKED_COUNT(tracker)); | |
410 | DBG_NO_LOC(" active: %u", ACTIVE_COUNT(tracker)); | |
411 | DBG_NO_LOC(" suspendable: %u", SUSPENDABLE_COUNT(tracker)); | |
412 | DBG_NO_LOC(" unsuspendable: %u", UNSUSPENDABLE_COUNT(tracker)); | |
413 | DBG_NO_LOC(" suspended: %u", SUSPENDED_COUNT(tracker)); | |
414 | DBG_NO_LOC(" capacity: %u", tracker->capacity); | |
415 | ||
416 | DBG_NO_LOC(" Tracked suspendable file descriptors"); | |
28ab034a | 417 | cds_list_for_each_entry (handle, &tracker->active_handles, handles_list_node) { |
f5ea0241 | 418 | fs_handle_tracked_log(handle); |
df038819 | 419 | } |
28ab034a | 420 | cds_list_for_each_entry (handle, &tracker->suspended_handles, handles_list_node) { |
f5ea0241 | 421 | fs_handle_tracked_log(handle); |
df038819 JG |
422 | } |
423 | if (!SUSPENDABLE_COUNT(tracker)) { | |
424 | DBG_NO_LOC(" None"); | |
425 | } | |
426 | ||
427 | DBG_NO_LOC(" Tracked unsuspendable file descriptors"); | |
56047f5a JG |
428 | |
429 | { | |
430 | lttng::urcu::read_lock_guard read_lock; | |
431 | ||
432 | cds_lfht_for_each_entry ( | |
433 | tracker->unsuspendable_fds, &iter, unsuspendable_fd, tracker_node) { | |
434 | DBG_NO_LOC(" %s [active, fd %d]", | |
435 | unsuspendable_fd->name ?: "Unnamed", | |
436 | unsuspendable_fd->fd); | |
437 | } | |
438 | } | |
439 | ||
df038819 JG |
440 | if (!UNSUSPENDABLE_COUNT(tracker)) { |
441 | DBG_NO_LOC(" None"); | |
442 | } | |
443 | ||
444 | pthread_mutex_unlock(&tracker->lock); | |
445 | } | |
446 | ||
447 | int fd_tracker_destroy(struct fd_tracker *tracker) | |
448 | { | |
449 | int ret = 0; | |
450 | ||
f6bef966 JG |
451 | if (!tracker) { |
452 | goto end; | |
453 | } | |
df038819 JG |
454 | /* |
455 | * Refuse to destroy the tracker as fs_handles may still old | |
456 | * weak references to the tracker. | |
457 | */ | |
458 | pthread_mutex_lock(&tracker->lock); | |
459 | if (TRACKED_COUNT(tracker)) { | |
460 | ERR("A file descriptor leak has been detected: %u tracked file descriptors are still being tracked", | |
28ab034a | 461 | TRACKED_COUNT(tracker)); |
df038819 JG |
462 | pthread_mutex_unlock(&tracker->lock); |
463 | fd_tracker_log(tracker); | |
464 | ret = -1; | |
465 | goto end; | |
466 | } | |
467 | pthread_mutex_unlock(&tracker->lock); | |
468 | ||
874a3441 | 469 | if (tracker->unsuspendable_fds) { |
cd9adb8b | 470 | ret = cds_lfht_destroy(tracker->unsuspendable_fds, nullptr); |
a0377dfe | 471 | LTTNG_ASSERT(!ret); |
874a3441 | 472 | } |
660a216c JG |
473 | |
474 | lttng_inode_registry_destroy(tracker->inode_registry); | |
f7c3ffd7 | 475 | lttng_unlinked_file_pool_destroy(tracker->unlinked_file_pool); |
df038819 JG |
476 | pthread_mutex_destroy(&tracker->lock); |
477 | free(tracker); | |
478 | end: | |
479 | return ret; | |
480 | } | |
481 | ||
482 | struct fs_handle *fd_tracker_open_fs_handle(struct fd_tracker *tracker, | |
28ab034a JG |
483 | struct lttng_directory_handle *directory, |
484 | const char *path, | |
485 | int flags, | |
486 | mode_t *mode) | |
df038819 JG |
487 | { |
488 | int ret; | |
cd9adb8b | 489 | struct fs_handle_tracked *handle = nullptr; |
df038819 | 490 | struct stat fd_stat; |
28ab034a JG |
491 | struct open_properties properties = { .flags = flags, |
492 | .mode = { | |
493 | .is_set = !!mode, | |
494 | .value = | |
495 | static_cast<mode_t>(mode ? *mode : 0), | |
496 | } }; | |
df038819 | 497 | |
df038819 JG |
498 | pthread_mutex_lock(&tracker->lock); |
499 | if (ACTIVE_COUNT(tracker) == tracker->capacity) { | |
500 | if (tracker->count.suspendable.active > 0) { | |
501 | ret = fd_tracker_suspend_handles(tracker, 1); | |
502 | if (ret) { | |
19380ea8 | 503 | goto end; |
df038819 JG |
504 | } |
505 | } else { | |
506 | /* | |
507 | * There are not enough active suspendable file | |
19380ea8 JG |
508 | * descriptors to open a new fd and still accommodate |
509 | * the tracker's capacity. | |
df038819 JG |
510 | */ |
511 | WARN("Cannot open file system handle, too many unsuspendable file descriptors are opened (%u)", | |
28ab034a | 512 | tracker->count.unsuspendable); |
19380ea8 | 513 | goto end; |
df038819 JG |
514 | } |
515 | } | |
516 | ||
64803277 | 517 | handle = zmalloc<fs_handle_tracked>(); |
df038819 JG |
518 | if (!handle) { |
519 | goto end; | |
520 | } | |
28ab034a | 521 | handle->parent = (typeof(handle->parent)){ |
f7c3ffd7 JG |
522 | .get_fd = fs_handle_tracked_get_fd, |
523 | .put_fd = fs_handle_tracked_put_fd, | |
524 | .unlink = fs_handle_tracked_unlink, | |
525 | .close = fs_handle_tracked_close, | |
526 | }; | |
527 | ||
660a216c | 528 | handle->tracker = tracker; |
df038819 | 529 | |
cd9adb8b | 530 | ret = pthread_mutex_init(&handle->lock, nullptr); |
df038819 JG |
531 | if (ret) { |
532 | PERROR("Failed to initialize handle mutex while creating fs handle"); | |
19380ea8 | 533 | goto error_mutex_init; |
df038819 JG |
534 | } |
535 | ||
f7c3ffd7 | 536 | handle->fd = open_from_properties(directory, path, &properties); |
df038819 JG |
537 | if (handle->fd < 0) { |
538 | PERROR("Failed to open fs handle to %s, open() returned", path); | |
19380ea8 | 539 | goto error; |
df038819 JG |
540 | } |
541 | ||
542 | handle->properties = properties; | |
874a3441 | 543 | |
28ab034a JG |
544 | handle->inode = lttng_inode_registry_get_inode( |
545 | tracker->inode_registry, directory, path, handle->fd, tracker->unlinked_file_pool); | |
874a3441 | 546 | if (!handle->inode) { |
5c1f54d1 | 547 | ERR("Failed to get lttng_inode corresponding to file %s", path); |
19380ea8 | 548 | goto error; |
874a3441 | 549 | } |
df038819 JG |
550 | |
551 | if (fstat(handle->fd, &fd_stat)) { | |
552 | PERROR("Failed to retrieve file descriptor inode while creating fs handle, fstat() returned"); | |
19380ea8 | 553 | goto error; |
df038819 JG |
554 | } |
555 | handle->ino = fd_stat.st_ino; | |
556 | ||
557 | fd_tracker_track(tracker, handle); | |
df038819 | 558 | end: |
19380ea8 | 559 | pthread_mutex_unlock(&tracker->lock); |
cd9adb8b | 560 | return handle ? &handle->parent : nullptr; |
19380ea8 | 561 | error: |
660a216c JG |
562 | if (handle->inode) { |
563 | lttng_inode_put(handle->inode); | |
564 | } | |
19380ea8 JG |
565 | pthread_mutex_destroy(&handle->lock); |
566 | error_mutex_init: | |
660a216c | 567 | free(handle); |
cd9adb8b | 568 | handle = nullptr; |
df038819 JG |
569 | goto end; |
570 | } | |
571 | ||
572 | /* Caller must hold the tracker's lock. */ | |
28ab034a | 573 | static int fd_tracker_suspend_handles(struct fd_tracker *tracker, unsigned int count) |
df038819 JG |
574 | { |
575 | unsigned int left_to_close = count; | |
f7c3ffd7 | 576 | unsigned int attempts_left = tracker->count.suspendable.active; |
f5ea0241 | 577 | struct fs_handle_tracked *handle, *tmp; |
df038819 | 578 | |
28ab034a | 579 | cds_list_for_each_entry_safe (handle, tmp, &tracker->active_handles, handles_list_node) { |
df038819 JG |
580 | int ret; |
581 | ||
582 | fd_tracker_untrack(tracker, handle); | |
f5ea0241 | 583 | ret = fs_handle_tracked_suspend(handle); |
df038819 JG |
584 | fd_tracker_track(tracker, handle); |
585 | if (!ret) { | |
586 | left_to_close--; | |
587 | } | |
f7c3ffd7 | 588 | attempts_left--; |
df038819 | 589 | |
f7c3ffd7 | 590 | if (left_to_close == 0 || attempts_left == 0) { |
df038819 JG |
591 | break; |
592 | } | |
593 | } | |
594 | return left_to_close ? -EMFILE : 0; | |
595 | } | |
596 | ||
597 | int fd_tracker_open_unsuspendable_fd(struct fd_tracker *tracker, | |
28ab034a JG |
598 | int *out_fds, |
599 | const char **names, | |
600 | unsigned int fd_count, | |
601 | fd_open_cb open, | |
602 | void *user_data) | |
df038819 JG |
603 | { |
604 | int ret, user_ret, i, fds_to_suspend; | |
605 | unsigned int active_fds; | |
b13a2b17 | 606 | struct unsuspendable_fd **entries; |
56047f5a | 607 | lttng::urcu::read_lock_guard read_lock; |
df038819 | 608 | |
64803277 | 609 | entries = calloc<unsuspendable_fd *>(fd_count); |
b13a2b17 JG |
610 | if (!entries) { |
611 | ret = -1; | |
612 | goto end; | |
613 | } | |
df038819 JG |
614 | |
615 | pthread_mutex_lock(&tracker->lock); | |
616 | ||
617 | active_fds = ACTIVE_COUNT(tracker); | |
28ab034a | 618 | fds_to_suspend = (int) active_fds + (int) fd_count - (int) tracker->capacity; |
df038819 JG |
619 | if (fds_to_suspend > 0) { |
620 | if (fds_to_suspend <= tracker->count.suspendable.active) { | |
28ab034a | 621 | ret = fd_tracker_suspend_handles(tracker, fds_to_suspend); |
df038819 | 622 | if (ret) { |
b13a2b17 | 623 | goto end_unlock; |
df038819 JG |
624 | } |
625 | } else { | |
626 | /* | |
627 | * There are not enough active suspendable file | |
d49487dc | 628 | * descriptors to open a new fd and still accommodates the |
df038819 JG |
629 | * tracker's capacity. |
630 | */ | |
631 | WARN("Cannot open unsuspendable fd, too many unsuspendable file descriptors are opened (%u)", | |
28ab034a | 632 | tracker->count.unsuspendable); |
df038819 | 633 | ret = -EMFILE; |
b13a2b17 | 634 | goto end_unlock; |
df038819 JG |
635 | } |
636 | } | |
637 | ||
638 | user_ret = open(user_data, out_fds); | |
639 | if (user_ret) { | |
640 | ret = user_ret; | |
b13a2b17 | 641 | goto end_unlock; |
df038819 JG |
642 | } |
643 | ||
644 | /* | |
645 | * Add the fds returned by the user's callback to the hashtable | |
646 | * of unsuspendable fds. | |
647 | */ | |
648 | for (i = 0; i < fd_count; i++) { | |
28ab034a | 649 | struct unsuspendable_fd *entry = |
cd9adb8b | 650 | unsuspendable_fd_create(names ? names[i] : nullptr, out_fds[i]); |
df038819 JG |
651 | |
652 | if (!entry) { | |
653 | ret = -1; | |
654 | goto end_free_entries; | |
655 | } | |
656 | entries[i] = entry; | |
657 | } | |
658 | ||
df038819 JG |
659 | for (i = 0; i < fd_count; i++) { |
660 | struct cds_lfht_node *node; | |
661 | struct unsuspendable_fd *entry = entries[i]; | |
662 | ||
5c1f54d1 | 663 | node = cds_lfht_add_unique(tracker->unsuspendable_fds, |
28ab034a JG |
664 | hash_key_ulong((void *) (unsigned long) out_fds[i], |
665 | seed.value), | |
666 | match_fd, | |
667 | (void *) (unsigned long) out_fds[i], | |
668 | &entry->tracker_node); | |
df038819 JG |
669 | |
670 | if (node != &entry->tracker_node) { | |
671 | ret = -EEXIST; | |
df038819 JG |
672 | goto end_free_entries; |
673 | } | |
cd9adb8b | 674 | entries[i] = nullptr; |
df038819 JG |
675 | } |
676 | tracker->count.unsuspendable += fd_count; | |
df038819 | 677 | ret = user_ret; |
b13a2b17 | 678 | end_unlock: |
df038819 | 679 | pthread_mutex_unlock(&tracker->lock); |
b13a2b17 JG |
680 | end: |
681 | free(entries); | |
df038819 JG |
682 | return ret; |
683 | end_free_entries: | |
684 | for (i = 0; i < fd_count; i++) { | |
685 | unsuspendable_fd_destroy(entries[i]); | |
686 | } | |
b13a2b17 | 687 | goto end_unlock; |
df038819 JG |
688 | } |
689 | ||
690 | int fd_tracker_close_unsuspendable_fd(struct fd_tracker *tracker, | |
28ab034a JG |
691 | int *fds_in, |
692 | unsigned int fd_count, | |
693 | fd_close_cb close, | |
694 | void *user_data) | |
df038819 JG |
695 | { |
696 | int i, ret, user_ret; | |
cd9adb8b | 697 | int *fds = nullptr; |
56047f5a | 698 | lttng::urcu::read_lock_guard read_lock; |
df038819 JG |
699 | |
700 | /* | |
701 | * Maintain a local copy of fds_in as the user's callback may modify its | |
702 | * contents (e.g. setting the fd(s) to -1 after close). | |
703 | */ | |
64803277 | 704 | fds = malloc<int>(sizeof(*fds) * fd_count); |
b13a2b17 JG |
705 | if (!fds) { |
706 | ret = -1; | |
707 | goto end; | |
708 | } | |
df038819 JG |
709 | memcpy(fds, fds_in, sizeof(*fds) * fd_count); |
710 | ||
711 | pthread_mutex_lock(&tracker->lock); | |
df038819 JG |
712 | |
713 | /* Let the user close the file descriptors. */ | |
714 | user_ret = close(user_data, fds_in); | |
715 | if (user_ret) { | |
716 | ret = user_ret; | |
b13a2b17 | 717 | goto end_unlock; |
df038819 JG |
718 | } |
719 | ||
720 | /* Untrack the fds that were just closed by the user's callback. */ | |
721 | for (i = 0; i < fd_count; i++) { | |
722 | struct cds_lfht_node *node; | |
723 | struct cds_lfht_iter iter; | |
724 | struct unsuspendable_fd *entry; | |
725 | ||
726 | cds_lfht_lookup(tracker->unsuspendable_fds, | |
28ab034a JG |
727 | hash_key_ulong((void *) (unsigned long) fds[i], seed.value), |
728 | match_fd, | |
729 | (void *) (unsigned long) fds[i], | |
df038819 JG |
730 | &iter); |
731 | node = cds_lfht_iter_get_node(&iter); | |
732 | if (!node) { | |
733 | /* Unknown file descriptor. */ | |
734 | WARN("Untracked file descriptor %d passed to fd_tracker_close_unsuspendable_fd()", | |
28ab034a | 735 | fds[i]); |
df038819 | 736 | ret = -EINVAL; |
b13a2b17 | 737 | goto end_unlock; |
df038819 | 738 | } |
28ab034a | 739 | entry = lttng::utils::container_of(node, &unsuspendable_fd::tracker_node); |
df038819 JG |
740 | |
741 | cds_lfht_del(tracker->unsuspendable_fds, node); | |
742 | unsuspendable_fd_destroy(entry); | |
743 | fds[i] = -1; | |
744 | } | |
745 | ||
746 | tracker->count.unsuspendable -= fd_count; | |
747 | ret = 0; | |
b13a2b17 | 748 | end_unlock: |
df038819 | 749 | pthread_mutex_unlock(&tracker->lock); |
b13a2b17 JG |
750 | free(fds); |
751 | end: | |
df038819 JG |
752 | return ret; |
753 | } | |
754 | ||
755 | /* Caller must have taken the tracker's and handle's locks. */ | |
28ab034a | 756 | static void fd_tracker_track(struct fd_tracker *tracker, struct fs_handle_tracked *handle) |
df038819 JG |
757 | { |
758 | if (handle->fd >= 0) { | |
759 | tracker->count.suspendable.active++; | |
28ab034a | 760 | cds_list_add_tail(&handle->handles_list_node, &tracker->active_handles); |
df038819 JG |
761 | } else { |
762 | tracker->count.suspendable.suspended++; | |
28ab034a | 763 | cds_list_add_tail(&handle->handles_list_node, &tracker->suspended_handles); |
df038819 JG |
764 | } |
765 | } | |
766 | ||
767 | /* Caller must have taken the tracker's and handle's locks. */ | |
28ab034a | 768 | static void fd_tracker_untrack(struct fd_tracker *tracker, struct fs_handle_tracked *handle) |
df038819 JG |
769 | { |
770 | if (handle->fd >= 0) { | |
771 | tracker->count.suspendable.active--; | |
772 | } else { | |
773 | tracker->count.suspendable.suspended--; | |
774 | } | |
775 | cds_list_del(&handle->handles_list_node); | |
776 | } | |
777 | ||
778 | /* Caller must have taken the tracker's and handle's locks. */ | |
28ab034a | 779 | static int fd_tracker_restore_handle(struct fd_tracker *tracker, struct fs_handle_tracked *handle) |
df038819 JG |
780 | { |
781 | int ret; | |
782 | ||
783 | fd_tracker_untrack(tracker, handle); | |
784 | if (ACTIVE_COUNT(tracker) >= tracker->capacity) { | |
785 | ret = fd_tracker_suspend_handles(tracker, 1); | |
786 | if (ret) { | |
787 | goto end; | |
788 | } | |
789 | } | |
f5ea0241 | 790 | ret = fs_handle_tracked_restore(handle); |
df038819 JG |
791 | end: |
792 | fd_tracker_track(tracker, handle); | |
793 | return ret ? ret : handle->fd; | |
794 | } | |
795 | ||
f5ea0241 | 796 | static int fs_handle_tracked_get_fd(struct fs_handle *_handle) |
df038819 JG |
797 | { |
798 | int ret; | |
f5ea0241 | 799 | struct fs_handle_tracked *handle = |
28ab034a | 800 | lttng::utils::container_of(_handle, &fs_handle_tracked::parent); |
df038819 JG |
801 | |
802 | /* | |
803 | * TODO This should be optimized as it is a fairly hot path. | |
804 | * The fd-tracker's lock should only be taken when a fs_handle is | |
805 | * restored (slow path). On the fast path (fs_handle is active), | |
806 | * the only effect on the fd_tracker is marking the handle as the | |
807 | * most recently used. Currently, it is done by a call to the | |
808 | * track/untrack helpers, but it should be done atomically. | |
809 | * | |
810 | * Note that the lock's nesting order must still be respected here. | |
811 | * The handle's lock nests inside the tracker's lock. | |
812 | */ | |
813 | pthread_mutex_lock(&handle->tracker->lock); | |
814 | pthread_mutex_lock(&handle->lock); | |
a0377dfe | 815 | LTTNG_ASSERT(!handle->in_use); |
df038819 JG |
816 | |
817 | handle->tracker->stats.uses++; | |
818 | if (handle->fd >= 0) { | |
819 | ret = handle->fd; | |
820 | /* Mark as most recently used. */ | |
821 | fd_tracker_untrack(handle->tracker, handle); | |
822 | fd_tracker_track(handle->tracker, handle); | |
823 | } else { | |
824 | handle->tracker->stats.misses++; | |
825 | ret = fd_tracker_restore_handle(handle->tracker, handle); | |
826 | if (ret < 0) { | |
827 | handle->tracker->stats.errors++; | |
828 | goto end; | |
829 | } | |
830 | } | |
831 | handle->in_use = true; | |
832 | end: | |
833 | pthread_mutex_unlock(&handle->lock); | |
834 | pthread_mutex_unlock(&handle->tracker->lock); | |
835 | return ret; | |
836 | } | |
837 | ||
f5ea0241 | 838 | static void fs_handle_tracked_put_fd(struct fs_handle *_handle) |
df038819 | 839 | { |
f5ea0241 | 840 | struct fs_handle_tracked *handle = |
28ab034a | 841 | lttng::utils::container_of(_handle, &fs_handle_tracked::parent); |
f5ea0241 | 842 | |
df038819 JG |
843 | pthread_mutex_lock(&handle->lock); |
844 | handle->in_use = false; | |
845 | pthread_mutex_unlock(&handle->lock); | |
846 | } | |
847 | ||
f5ea0241 | 848 | static int fs_handle_tracked_unlink(struct fs_handle *_handle) |
9d16fc7f JG |
849 | { |
850 | int ret; | |
f5ea0241 | 851 | struct fs_handle_tracked *handle = |
28ab034a | 852 | lttng::utils::container_of(_handle, &fs_handle_tracked::parent); |
9d16fc7f JG |
853 | |
854 | pthread_mutex_lock(&handle->tracker->lock); | |
855 | pthread_mutex_lock(&handle->lock); | |
f7c3ffd7 | 856 | ret = lttng_inode_unlink(handle->inode); |
9d16fc7f JG |
857 | pthread_mutex_unlock(&handle->lock); |
858 | pthread_mutex_unlock(&handle->tracker->lock); | |
859 | return ret; | |
860 | } | |
861 | ||
f5ea0241 | 862 | static int fs_handle_tracked_close(struct fs_handle *_handle) |
df038819 JG |
863 | { |
864 | int ret = 0; | |
cd9adb8b | 865 | const char *path = nullptr; |
f5ea0241 | 866 | struct fs_handle_tracked *handle = |
28ab034a | 867 | lttng::utils::container_of(_handle, &fs_handle_tracked::parent); |
cd9adb8b | 868 | struct lttng_directory_handle *inode_directory_handle = nullptr; |
df038819 JG |
869 | |
870 | if (!handle) { | |
871 | ret = -EINVAL; | |
872 | goto end; | |
873 | } | |
874 | ||
875 | pthread_mutex_lock(&handle->tracker->lock); | |
876 | pthread_mutex_lock(&handle->lock); | |
660a216c | 877 | if (handle->inode) { |
cd9adb8b | 878 | lttng_inode_borrow_location(handle->inode, nullptr, &path); |
dd95933f JG |
879 | /* |
880 | * Here a reference to the inode's directory handle is acquired | |
881 | * to prevent the last reference to it from being released while | |
882 | * the tracker's lock is taken. | |
883 | * | |
884 | * If this wasn't done, the directory handle could attempt to | |
885 | * close its underlying directory file descriptor, which would | |
886 | * attempt to lock the tracker's lock, resulting in a deadlock. | |
887 | * | |
888 | * Since a new reference to the directory handle is taken within | |
889 | * the scope of this function, it is not possible for the last | |
890 | * reference to the inode's location directory handle to be | |
891 | * released during the call to lttng_inode_put(). | |
892 | * | |
893 | * We wait until the tracker's lock is released to release the | |
894 | * reference. Hence, the call to the tracker is delayed just | |
895 | * enough to not attempt to recursively acquire the tracker's | |
896 | * lock twice. | |
897 | */ | |
28ab034a | 898 | inode_directory_handle = lttng_inode_get_location_directory_handle(handle->inode); |
660a216c | 899 | } |
df038819 JG |
900 | fd_tracker_untrack(handle->tracker, handle); |
901 | if (handle->fd >= 0) { | |
902 | /* | |
903 | * The return value of close() is not propagated as there | |
904 | * isn't much the user can do about it. | |
905 | */ | |
906 | if (close(handle->fd)) { | |
ea05fafd | 907 | PERROR("Failed to close the file descriptor (%d) of fs handle to %s, close() returned", |
28ab034a JG |
908 | handle->fd, |
909 | path ? path : "Unknown"); | |
df038819 JG |
910 | } |
911 | handle->fd = -1; | |
912 | } | |
4c372c54 JG |
913 | if (handle->inode) { |
914 | lttng_inode_put(handle->inode); | |
915 | } | |
df038819 JG |
916 | pthread_mutex_unlock(&handle->lock); |
917 | pthread_mutex_destroy(&handle->lock); | |
918 | pthread_mutex_unlock(&handle->tracker->lock); | |
df038819 | 919 | free(handle); |
dd95933f | 920 | lttng_directory_handle_put(inode_directory_handle); |
df038819 JG |
921 | end: |
922 | return ret; | |
923 | } |