2 * Copyright (C) 2018-2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "fd-tracker.hpp"
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>
24 #include <sys/types.h>
26 #include <urcu/list.h>
27 #include <urcu/rculfhash.h>
29 /* Tracker lock must be taken by the user. */
30 #define TRACKED_COUNT(tracker) \
31 ((tracker)->count.suspendable.active + (tracker)->count.suspendable.suspended + \
32 (tracker)->count.unsuspendable)
34 /* Tracker lock must be taken by the user. */
35 #define ACTIVE_COUNT(tracker) ((tracker)->count.suspendable.active + (tracker)->count.unsuspendable)
37 /* Tracker lock must be taken by the user. */
38 #define SUSPENDED_COUNT(tracker) ((tracker)->count.suspendable.suspended)
40 /* Tracker lock must be taken by the user. */
41 #define SUSPENDABLE_COUNT(tracker) \
42 ((tracker)->count.suspendable.active + (tracker)->count.suspendable.suspended)
44 /* Tracker lock must be taken by the user. */
45 #define UNSUSPENDABLE_COUNT(tracker) ((tracker)->count.unsuspendable)
52 unsigned int suspended
;
54 unsigned int unsuspendable
;
56 unsigned int capacity
;
60 /* Failures to suspend or restore fs handles. */
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.
70 struct cds_list_head active_handles
;
71 struct cds_list_head suspended_handles
;
72 struct cds_lfht
*unsuspendable_fds
;
73 struct lttng_inode_registry
*inode_registry
;
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
;
80 struct open_properties
{
82 LTTNG_OPTIONAL(mode_t
) mode
;
86 * A fs_handle_tracked is not ref-counted. Therefore, it is assumed that a
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.
92 * The fs_handle lock always nests _within_ the tracker's lock.
94 struct fs_handle_tracked
{
95 struct fs_handle parent
;
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.
101 struct fd_tracker
*tracker
;
102 struct open_properties properties
;
103 struct lttng_inode
*inode
;
105 /* inode number of the file at the time of the handle's creation. */
108 /* Offset to which the file should be restored. */
110 struct cds_list_head handles_list_node
;
113 struct unsuspendable_fd
{
115 * Accesses are only performed through the tracker, which is protected
120 struct cds_lfht_node tracker_node
;
121 struct rcu_head rcu_head
;
125 pthread_mutex_t lock
;
129 .lock
= PTHREAD_MUTEX_INITIALIZER
,
130 .initialized
= false,
135 static int match_fd(struct cds_lfht_node
*node
, const void *key
);
136 static void unsuspendable_fd_destroy(struct unsuspendable_fd
*entry
);
137 static struct unsuspendable_fd
*unsuspendable_fd_create(const char *name
, int fd
);
138 static int open_from_properties(const struct lttng_directory_handle
*dir_handle
,
140 struct open_properties
*properties
);
142 static void fs_handle_tracked_log(struct fs_handle_tracked
*handle
);
143 static int fs_handle_tracked_suspend(struct fs_handle_tracked
*handle
);
144 static int fs_handle_tracked_restore(struct fs_handle_tracked
*handle
);
145 static int fs_handle_tracked_get_fd(struct fs_handle
*_handle
);
146 static void fs_handle_tracked_put_fd(struct fs_handle
*_handle
);
147 static int fs_handle_tracked_unlink(struct fs_handle
*_handle
);
148 static int fs_handle_tracked_close(struct fs_handle
*_handle
);
150 static void fd_tracker_track(struct fd_tracker
*tracker
, struct fs_handle_tracked
*handle
);
151 static void fd_tracker_untrack(struct fd_tracker
*tracker
, struct fs_handle_tracked
*handle
);
152 static int fd_tracker_suspend_handles(struct fd_tracker
*tracker
, unsigned int count
);
153 static int fd_tracker_restore_handle(struct fd_tracker
*tracker
, struct fs_handle_tracked
*handle
);
155 /* Match function of the tracker's unsuspendable_fds hash table. */
156 static int match_fd(struct cds_lfht_node
*node
, const void *key
)
158 struct unsuspendable_fd
*entry
=
159 lttng::utils::container_of(node
, &unsuspendable_fd::tracker_node
);
161 return hash_match_key_ulong((void *) (unsigned long) entry
->fd
, (void *) key
);
164 static void delete_unsuspendable_fd(struct rcu_head
*head
)
166 struct unsuspendable_fd
*fd
= caa_container_of(head
, struct unsuspendable_fd
, rcu_head
);
172 static void unsuspendable_fd_destroy(struct unsuspendable_fd
*entry
)
177 call_rcu(&entry
->rcu_head
, delete_unsuspendable_fd
);
180 static struct unsuspendable_fd
*unsuspendable_fd_create(const char *name
, int fd
)
182 struct unsuspendable_fd
*entry
= zmalloc
<unsuspendable_fd
>();
188 entry
->name
= strdup(name
);
193 cds_lfht_node_init(&entry
->tracker_node
);
197 unsuspendable_fd_destroy(entry
);
201 static void fs_handle_tracked_log(struct fs_handle_tracked
*handle
)
205 pthread_mutex_lock(&handle
->lock
);
206 lttng_inode_borrow_location(handle
->inode
, nullptr, &path
);
208 if (handle
->fd
>= 0) {
209 DBG_NO_LOC(" %s [active, fd %d%s]",
212 handle
->in_use
? ", in use" : "");
214 DBG_NO_LOC(" %s [suspended]", path
);
216 pthread_mutex_unlock(&handle
->lock
);
219 /* Tracker lock must be held by the caller. */
220 static int fs_handle_tracked_suspend(struct fs_handle_tracked
*handle
)
225 const struct lttng_directory_handle
*node_directory_handle
;
227 pthread_mutex_lock(&handle
->lock
);
228 lttng_inode_borrow_location(handle
->inode
, &node_directory_handle
, &path
);
229 LTTNG_ASSERT(handle
->fd
>= 0);
230 if (handle
->in_use
) {
231 /* This handle can't be suspended as it is currently in use. */
236 ret
= lttng_directory_handle_stat(node_directory_handle
, path
, &fs_stat
);
238 PERROR("Filesystem handle to %s cannot be suspended as stat() failed", path
);
243 if (fs_stat
.st_ino
!= handle
->ino
) {
244 /* Don't suspend as the handle would not be restorable. */
245 WARN("Filesystem handle to %s cannot be suspended as its inode changed", path
);
250 handle
->offset
= lseek(handle
->fd
, 0, SEEK_CUR
);
251 if (handle
->offset
== -1) {
252 WARN("Filesystem handle to %s cannot be suspended as lseek() failed to sample its current position",
258 ret
= close(handle
->fd
);
260 PERROR("Filesystem handle to %s cannot be suspended as close() failed", path
);
264 DBG("Suspended filesystem handle to %s (fd %i) at position %" PRId64
,
271 handle
->tracker
->stats
.errors
++;
273 pthread_mutex_unlock(&handle
->lock
);
277 /* Caller must hold the tracker and handle's locks. */
278 static int fs_handle_tracked_restore(struct fs_handle_tracked
*handle
)
282 const struct lttng_directory_handle
*node_directory_handle
;
284 lttng_inode_borrow_location(handle
->inode
, &node_directory_handle
, &path
);
286 LTTNG_ASSERT(handle
->fd
== -1);
288 ret
= open_from_properties(node_directory_handle
, path
, &handle
->properties
);
290 PERROR("Failed to restore filesystem handle to %s, open() failed", path
);
296 ret
= lseek(fd
, handle
->offset
, SEEK_SET
);
298 PERROR("Failed to restore filesystem handle to %s, lseek() failed", path
);
302 DBG("Restored filesystem handle to %s (fd %i) at position %" PRId64
,
316 static int open_from_properties(const struct lttng_directory_handle
*dir_handle
,
318 struct open_properties
*properties
)
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.
328 if ((properties
->flags
& O_CREAT
) && properties
->mode
.is_set
) {
329 ret
= lttng_directory_handle_open_file(
330 dir_handle
, path
, properties
->flags
, properties
->mode
.value
);
332 ret
= lttng_directory_handle_open_file(dir_handle
, path
, properties
->flags
, 0);
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
344 properties
->flags
&= ~(O_CREAT
| O_TRUNC
);
353 struct fd_tracker
*fd_tracker_create(const char *unlinked_file_path
, unsigned int capacity
)
355 struct fd_tracker
*tracker
= zmalloc
<fd_tracker
>();
361 pthread_mutex_lock(&seed
.lock
);
362 if (!seed
.initialized
) {
363 seed
.value
= (unsigned long) time(nullptr);
364 seed
.initialized
= true;
366 pthread_mutex_unlock(&seed
.lock
);
368 CDS_INIT_LIST_HEAD(&tracker
->active_handles
);
369 CDS_INIT_LIST_HEAD(&tracker
->suspended_handles
);
370 tracker
->capacity
= capacity
;
371 tracker
->unsuspendable_fds
= cds_lfht_new(
372 DEFAULT_HT_SIZE
, 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, nullptr);
373 if (!tracker
->unsuspendable_fds
) {
374 ERR("Failed to create fd-tracker's unsuspendable_fds hash table");
377 tracker
->inode_registry
= lttng_inode_registry_create();
378 if (!tracker
->inode_registry
) {
379 ERR("Failed to create fd-tracker's inode registry");
383 tracker
->unlinked_file_pool
= lttng_unlinked_file_pool_create(unlinked_file_path
);
384 if (!tracker
->unlinked_file_pool
) {
387 DBG("File descriptor tracker created with a limit of %u simultaneously-opened FDs",
392 fd_tracker_destroy(tracker
);
396 void fd_tracker_log(struct fd_tracker
*tracker
)
398 struct fs_handle_tracked
*handle
;
399 struct unsuspendable_fd
*unsuspendable_fd
;
400 struct cds_lfht_iter iter
;
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
);
415 DBG_NO_LOC(" Tracked suspendable file descriptors");
416 cds_list_for_each_entry (handle
, &tracker
->active_handles
, handles_list_node
) {
417 fs_handle_tracked_log(handle
);
419 cds_list_for_each_entry (handle
, &tracker
->suspended_handles
, handles_list_node
) {
420 fs_handle_tracked_log(handle
);
422 if (!SUSPENDABLE_COUNT(tracker
)) {
426 DBG_NO_LOC(" Tracked unsuspendable file descriptors");
428 cds_lfht_for_each_entry (
429 tracker
->unsuspendable_fds
, &iter
, unsuspendable_fd
, tracker_node
) {
430 DBG_NO_LOC(" %s [active, fd %d]",
431 unsuspendable_fd
->name
?: "Unnamed",
432 unsuspendable_fd
->fd
);
435 if (!UNSUSPENDABLE_COUNT(tracker
)) {
439 pthread_mutex_unlock(&tracker
->lock
);
442 int fd_tracker_destroy(struct fd_tracker
*tracker
)
450 * Refuse to destroy the tracker as fs_handles may still old
451 * weak references to the tracker.
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",
456 TRACKED_COUNT(tracker
));
457 pthread_mutex_unlock(&tracker
->lock
);
458 fd_tracker_log(tracker
);
462 pthread_mutex_unlock(&tracker
->lock
);
464 if (tracker
->unsuspendable_fds
) {
465 ret
= cds_lfht_destroy(tracker
->unsuspendable_fds
, nullptr);
469 lttng_inode_registry_destroy(tracker
->inode_registry
);
470 lttng_unlinked_file_pool_destroy(tracker
->unlinked_file_pool
);
471 pthread_mutex_destroy(&tracker
->lock
);
477 struct fs_handle
*fd_tracker_open_fs_handle(struct fd_tracker
*tracker
,
478 struct lttng_directory_handle
*directory
,
484 struct fs_handle_tracked
*handle
= nullptr;
486 struct open_properties properties
= { .flags
= flags
,
490 static_cast<mode_t
>(mode
? *mode
: 0),
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);
502 * There are not enough active suspendable file
503 * descriptors to open a new fd and still accommodate
504 * the tracker's capacity.
506 WARN("Cannot open file system handle, too many unsuspendable file descriptors are opened (%u)",
507 tracker
->count
.unsuspendable
);
512 handle
= zmalloc
<fs_handle_tracked
>();
516 handle
->parent
= (typeof(handle
->parent
)){
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
,
523 handle
->tracker
= tracker
;
525 ret
= pthread_mutex_init(&handle
->lock
, nullptr);
527 PERROR("Failed to initialize handle mutex while creating fs handle");
528 goto error_mutex_init
;
531 handle
->fd
= open_from_properties(directory
, path
, &properties
);
532 if (handle
->fd
< 0) {
533 PERROR("Failed to open fs handle to %s, open() returned", path
);
537 handle
->properties
= properties
;
539 handle
->inode
= lttng_inode_registry_get_inode(
540 tracker
->inode_registry
, directory
, path
, handle
->fd
, tracker
->unlinked_file_pool
);
541 if (!handle
->inode
) {
542 ERR("Failed to get lttng_inode corresponding to file %s", path
);
546 if (fstat(handle
->fd
, &fd_stat
)) {
547 PERROR("Failed to retrieve file descriptor inode while creating fs handle, fstat() returned");
550 handle
->ino
= fd_stat
.st_ino
;
552 fd_tracker_track(tracker
, handle
);
554 pthread_mutex_unlock(&tracker
->lock
);
555 return handle
? &handle
->parent
: nullptr;
558 lttng_inode_put(handle
->inode
);
560 pthread_mutex_destroy(&handle
->lock
);
567 /* Caller must hold the tracker's lock. */
568 static int fd_tracker_suspend_handles(struct fd_tracker
*tracker
, unsigned int count
)
570 unsigned int left_to_close
= count
;
571 unsigned int attempts_left
= tracker
->count
.suspendable
.active
;
572 struct fs_handle_tracked
*handle
, *tmp
;
574 cds_list_for_each_entry_safe (handle
, tmp
, &tracker
->active_handles
, handles_list_node
) {
577 fd_tracker_untrack(tracker
, handle
);
578 ret
= fs_handle_tracked_suspend(handle
);
579 fd_tracker_track(tracker
, handle
);
585 if (left_to_close
== 0 || attempts_left
== 0) {
589 return left_to_close
? -EMFILE
: 0;
592 int fd_tracker_open_unsuspendable_fd(struct fd_tracker
*tracker
,
595 unsigned int fd_count
,
599 int ret
, user_ret
, i
, fds_to_suspend
;
600 unsigned int active_fds
;
601 struct unsuspendable_fd
**entries
;
603 entries
= calloc
<unsuspendable_fd
*>(fd_count
);
609 pthread_mutex_lock(&tracker
->lock
);
611 active_fds
= ACTIVE_COUNT(tracker
);
612 fds_to_suspend
= (int) active_fds
+ (int) fd_count
- (int) tracker
->capacity
;
613 if (fds_to_suspend
> 0) {
614 if (fds_to_suspend
<= tracker
->count
.suspendable
.active
) {
615 ret
= fd_tracker_suspend_handles(tracker
, fds_to_suspend
);
621 * There are not enough active suspendable file
622 * descriptors to open a new fd and still accommodates the
623 * tracker's capacity.
625 WARN("Cannot open unsuspendable fd, too many unsuspendable file descriptors are opened (%u)",
626 tracker
->count
.unsuspendable
);
632 user_ret
= open(user_data
, out_fds
);
639 * Add the fds returned by the user's callback to the hashtable
640 * of unsuspendable fds.
642 for (i
= 0; i
< fd_count
; i
++) {
643 struct unsuspendable_fd
*entry
=
644 unsuspendable_fd_create(names
? names
[i
] : nullptr, out_fds
[i
]);
648 goto end_free_entries
;
654 for (i
= 0; i
< fd_count
; i
++) {
655 struct cds_lfht_node
*node
;
656 struct unsuspendable_fd
*entry
= entries
[i
];
658 node
= cds_lfht_add_unique(tracker
->unsuspendable_fds
,
659 hash_key_ulong((void *) (unsigned long) out_fds
[i
],
662 (void *) (unsigned long) out_fds
[i
],
663 &entry
->tracker_node
);
665 if (node
!= &entry
->tracker_node
) {
668 goto end_free_entries
;
670 entries
[i
] = nullptr;
672 tracker
->count
.unsuspendable
+= fd_count
;
676 pthread_mutex_unlock(&tracker
->lock
);
681 for (i
= 0; i
< fd_count
; i
++) {
682 unsuspendable_fd_destroy(entries
[i
]);
687 int fd_tracker_close_unsuspendable_fd(struct fd_tracker
*tracker
,
689 unsigned int fd_count
,
693 int i
, ret
, user_ret
;
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).
700 fds
= malloc
<int>(sizeof(*fds
) * fd_count
);
705 memcpy(fds
, fds_in
, sizeof(*fds
) * fd_count
);
707 pthread_mutex_lock(&tracker
->lock
);
710 /* Let the user close the file descriptors. */
711 user_ret
= close(user_data
, fds_in
);
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
;
723 cds_lfht_lookup(tracker
->unsuspendable_fds
,
724 hash_key_ulong((void *) (unsigned long) fds
[i
], seed
.value
),
726 (void *) (unsigned long) fds
[i
],
728 node
= cds_lfht_iter_get_node(&iter
);
730 /* Unknown file descriptor. */
731 WARN("Untracked file descriptor %d passed to fd_tracker_close_unsuspendable_fd()",
736 entry
= lttng::utils::container_of(node
, &unsuspendable_fd::tracker_node
);
738 cds_lfht_del(tracker
->unsuspendable_fds
, node
);
739 unsuspendable_fd_destroy(entry
);
743 tracker
->count
.unsuspendable
-= fd_count
;
747 pthread_mutex_unlock(&tracker
->lock
);
753 /* Caller must have taken the tracker's and handle's locks. */
754 static void fd_tracker_track(struct fd_tracker
*tracker
, struct fs_handle_tracked
*handle
)
756 if (handle
->fd
>= 0) {
757 tracker
->count
.suspendable
.active
++;
758 cds_list_add_tail(&handle
->handles_list_node
, &tracker
->active_handles
);
760 tracker
->count
.suspendable
.suspended
++;
761 cds_list_add_tail(&handle
->handles_list_node
, &tracker
->suspended_handles
);
765 /* Caller must have taken the tracker's and handle's locks. */
766 static void fd_tracker_untrack(struct fd_tracker
*tracker
, struct fs_handle_tracked
*handle
)
768 if (handle
->fd
>= 0) {
769 tracker
->count
.suspendable
.active
--;
771 tracker
->count
.suspendable
.suspended
--;
773 cds_list_del(&handle
->handles_list_node
);
776 /* Caller must have taken the tracker's and handle's locks. */
777 static int fd_tracker_restore_handle(struct fd_tracker
*tracker
, struct fs_handle_tracked
*handle
)
781 fd_tracker_untrack(tracker
, handle
);
782 if (ACTIVE_COUNT(tracker
) >= tracker
->capacity
) {
783 ret
= fd_tracker_suspend_handles(tracker
, 1);
788 ret
= fs_handle_tracked_restore(handle
);
790 fd_tracker_track(tracker
, handle
);
791 return ret
? ret
: handle
->fd
;
794 static int fs_handle_tracked_get_fd(struct fs_handle
*_handle
)
797 struct fs_handle_tracked
*handle
=
798 lttng::utils::container_of(_handle
, &fs_handle_tracked::parent
);
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.
808 * Note that the lock's nesting order must still be respected here.
809 * The handle's lock nests inside the tracker's lock.
811 pthread_mutex_lock(&handle
->tracker
->lock
);
812 pthread_mutex_lock(&handle
->lock
);
813 LTTNG_ASSERT(!handle
->in_use
);
815 handle
->tracker
->stats
.uses
++;
816 if (handle
->fd
>= 0) {
818 /* Mark as most recently used. */
819 fd_tracker_untrack(handle
->tracker
, handle
);
820 fd_tracker_track(handle
->tracker
, handle
);
822 handle
->tracker
->stats
.misses
++;
823 ret
= fd_tracker_restore_handle(handle
->tracker
, handle
);
825 handle
->tracker
->stats
.errors
++;
829 handle
->in_use
= true;
831 pthread_mutex_unlock(&handle
->lock
);
832 pthread_mutex_unlock(&handle
->tracker
->lock
);
836 static void fs_handle_tracked_put_fd(struct fs_handle
*_handle
)
838 struct fs_handle_tracked
*handle
=
839 lttng::utils::container_of(_handle
, &fs_handle_tracked::parent
);
841 pthread_mutex_lock(&handle
->lock
);
842 handle
->in_use
= false;
843 pthread_mutex_unlock(&handle
->lock
);
846 static int fs_handle_tracked_unlink(struct fs_handle
*_handle
)
849 struct fs_handle_tracked
*handle
=
850 lttng::utils::container_of(_handle
, &fs_handle_tracked::parent
);
852 pthread_mutex_lock(&handle
->tracker
->lock
);
853 pthread_mutex_lock(&handle
->lock
);
854 ret
= lttng_inode_unlink(handle
->inode
);
855 pthread_mutex_unlock(&handle
->lock
);
856 pthread_mutex_unlock(&handle
->tracker
->lock
);
860 static int fs_handle_tracked_close(struct fs_handle
*_handle
)
863 const char *path
= nullptr;
864 struct fs_handle_tracked
*handle
=
865 lttng::utils::container_of(_handle
, &fs_handle_tracked::parent
);
866 struct lttng_directory_handle
*inode_directory_handle
= nullptr;
873 pthread_mutex_lock(&handle
->tracker
->lock
);
874 pthread_mutex_lock(&handle
->lock
);
876 lttng_inode_borrow_location(handle
->inode
, nullptr, &path
);
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.
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.
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().
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
896 inode_directory_handle
= lttng_inode_get_location_directory_handle(handle
->inode
);
898 fd_tracker_untrack(handle
->tracker
, handle
);
899 if (handle
->fd
>= 0) {
901 * The return value of close() is not propagated as there
902 * isn't much the user can do about it.
904 if (close(handle
->fd
)) {
905 PERROR("Failed to close the file descriptor (%d) of fs handle to %s, close() returned",
907 path
? path
: "Unknown");
912 lttng_inode_put(handle
->inode
);
914 pthread_mutex_unlock(&handle
->lock
);
915 pthread_mutex_destroy(&handle
->lock
);
916 pthread_mutex_unlock(&handle
->tracker
->lock
);
918 lttng_directory_handle_put(inode_directory_handle
);