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