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>
18 #include <common/urcu.hpp>
25 #include <sys/types.h>
27 #include <urcu/list.h>
28 #include <urcu/rculfhash.h>
30 /* Tracker lock must be taken by the user. */
31 #define TRACKED_COUNT(tracker) \
32 ((tracker)->count.suspendable.active + (tracker)->count.suspendable.suspended + \
33 (tracker)->count.unsuspendable)
35 /* Tracker lock must be taken by the user. */
36 #define ACTIVE_COUNT(tracker) ((tracker)->count.suspendable.active + (tracker)->count.unsuspendable)
38 /* Tracker lock must be taken by the user. */
39 #define SUSPENDED_COUNT(tracker) ((tracker)->count.suspendable.suspended)
41 /* Tracker lock must be taken by the user. */
42 #define SUSPENDABLE_COUNT(tracker) \
43 ((tracker)->count.suspendable.active + (tracker)->count.suspendable.suspended)
45 /* Tracker lock must be taken by the user. */
46 #define UNSUSPENDABLE_COUNT(tracker) ((tracker)->count.unsuspendable)
53 unsigned int suspended
;
55 unsigned int unsuspendable
;
57 unsigned int capacity
;
61 /* Failures to suspend or restore fs handles. */
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.
71 struct cds_list_head active_handles
;
72 struct cds_list_head suspended_handles
;
73 struct cds_lfht
*unsuspendable_fds
;
74 struct lttng_inode_registry
*inode_registry
;
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
;
81 struct open_properties
{
83 LTTNG_OPTIONAL(mode_t
) mode
;
87 * A fs_handle_tracked is not ref-counted. Therefore, it is assumed that a
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.
93 * The fs_handle lock always nests _within_ the tracker's lock.
95 struct fs_handle_tracked
{
96 struct fs_handle parent
;
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.
102 struct fd_tracker
*tracker
;
103 struct open_properties properties
;
104 struct lttng_inode
*inode
;
106 /* inode number of the file at the time of the handle's creation. */
109 /* Offset to which the file should be restored. */
111 struct cds_list_head handles_list_node
;
114 struct unsuspendable_fd
{
116 * Accesses are only performed through the tracker, which is protected
121 struct cds_lfht_node tracker_node
;
122 struct rcu_head rcu_head
;
126 pthread_mutex_t lock
;
130 .lock
= PTHREAD_MUTEX_INITIALIZER
,
131 .initialized
= false,
136 static int match_fd(struct cds_lfht_node
*node
, const void *key
);
137 static void unsuspendable_fd_destroy(struct unsuspendable_fd
*entry
);
138 static struct unsuspendable_fd
*unsuspendable_fd_create(const char *name
, int fd
);
139 static int open_from_properties(const struct lttng_directory_handle
*dir_handle
,
141 struct open_properties
*properties
);
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
);
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
);
156 /* Match function of the tracker's unsuspendable_fds hash table. */
157 static int match_fd(struct cds_lfht_node
*node
, const void *key
)
159 struct unsuspendable_fd
*entry
=
160 lttng::utils::container_of(node
, &unsuspendable_fd::tracker_node
);
162 return hash_match_key_ulong((void *) (unsigned long) entry
->fd
, (void *) key
);
165 static void delete_unsuspendable_fd(struct rcu_head
*head
)
167 struct unsuspendable_fd
*fd
= caa_container_of(head
, struct unsuspendable_fd
, rcu_head
);
173 static void unsuspendable_fd_destroy(struct unsuspendable_fd
*entry
)
178 call_rcu(&entry
->rcu_head
, delete_unsuspendable_fd
);
181 static struct unsuspendable_fd
*unsuspendable_fd_create(const char *name
, int fd
)
183 struct unsuspendable_fd
*entry
= zmalloc
<unsuspendable_fd
>();
189 entry
->name
= strdup(name
);
194 cds_lfht_node_init(&entry
->tracker_node
);
198 unsuspendable_fd_destroy(entry
);
202 static void fs_handle_tracked_log(struct fs_handle_tracked
*handle
)
206 pthread_mutex_lock(&handle
->lock
);
207 lttng_inode_borrow_location(handle
->inode
, nullptr, &path
);
209 if (handle
->fd
>= 0) {
210 DBG_NO_LOC(" %s [active, fd %d%s]",
213 handle
->in_use
? ", in use" : "");
215 DBG_NO_LOC(" %s [suspended]", path
);
217 pthread_mutex_unlock(&handle
->lock
);
220 /* Tracker lock must be held by the caller. */
221 static int fs_handle_tracked_suspend(struct fs_handle_tracked
*handle
)
226 const struct lttng_directory_handle
*node_directory_handle
;
228 pthread_mutex_lock(&handle
->lock
);
229 lttng_inode_borrow_location(handle
->inode
, &node_directory_handle
, &path
);
230 LTTNG_ASSERT(handle
->fd
>= 0);
231 if (handle
->in_use
) {
232 /* This handle can't be suspended as it is currently in use. */
237 ret
= lttng_directory_handle_stat(node_directory_handle
, path
, &fs_stat
);
239 PERROR("Filesystem handle to %s cannot be suspended as stat() failed", path
);
244 if (fs_stat
.st_ino
!= handle
->ino
) {
245 /* Don't suspend as the handle would not be restorable. */
246 WARN("Filesystem handle to %s cannot be suspended as its inode changed", path
);
251 handle
->offset
= lseek(handle
->fd
, 0, SEEK_CUR
);
252 if (handle
->offset
== -1) {
253 WARN("Filesystem handle to %s cannot be suspended as lseek() failed to sample its current position",
259 ret
= close(handle
->fd
);
261 PERROR("Filesystem handle to %s cannot be suspended as close() failed", path
);
265 DBG("Suspended filesystem handle to %s (fd %i) at position %" PRId64
,
272 handle
->tracker
->stats
.errors
++;
274 pthread_mutex_unlock(&handle
->lock
);
278 /* Caller must hold the tracker and handle's locks. */
279 static int fs_handle_tracked_restore(struct fs_handle_tracked
*handle
)
283 const struct lttng_directory_handle
*node_directory_handle
;
285 lttng_inode_borrow_location(handle
->inode
, &node_directory_handle
, &path
);
287 LTTNG_ASSERT(handle
->fd
== -1);
289 ret
= open_from_properties(node_directory_handle
, path
, &handle
->properties
);
291 PERROR("Failed to restore filesystem handle to %s, open() failed", path
);
297 ret
= lseek(fd
, handle
->offset
, SEEK_SET
);
299 PERROR("Failed to restore filesystem handle to %s, lseek() failed", path
);
303 DBG("Restored filesystem handle to %s (fd %i) at position %" PRId64
,
317 static int open_from_properties(const struct lttng_directory_handle
*dir_handle
,
319 struct open_properties
*properties
)
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.
329 if ((properties
->flags
& O_CREAT
) && properties
->mode
.is_set
) {
330 ret
= lttng_directory_handle_open_file(
331 dir_handle
, path
, properties
->flags
, properties
->mode
.value
);
333 ret
= lttng_directory_handle_open_file(dir_handle
, path
, properties
->flags
, 0);
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
345 properties
->flags
&= ~(O_CREAT
| O_TRUNC
);
354 struct fd_tracker
*fd_tracker_create(const char *unlinked_file_path
, unsigned int capacity
)
356 struct fd_tracker
*tracker
= zmalloc
<fd_tracker
>();
362 pthread_mutex_lock(&seed
.lock
);
363 if (!seed
.initialized
) {
364 seed
.value
= (unsigned long) time(nullptr);
365 seed
.initialized
= true;
367 pthread_mutex_unlock(&seed
.lock
);
369 CDS_INIT_LIST_HEAD(&tracker
->active_handles
);
370 CDS_INIT_LIST_HEAD(&tracker
->suspended_handles
);
371 tracker
->capacity
= capacity
;
372 tracker
->unsuspendable_fds
= cds_lfht_new(
373 DEFAULT_HT_SIZE
, 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, nullptr);
374 if (!tracker
->unsuspendable_fds
) {
375 ERR("Failed to create fd-tracker's unsuspendable_fds hash table");
378 tracker
->inode_registry
= lttng_inode_registry_create();
379 if (!tracker
->inode_registry
) {
380 ERR("Failed to create fd-tracker's inode registry");
384 tracker
->unlinked_file_pool
= lttng_unlinked_file_pool_create(unlinked_file_path
);
385 if (!tracker
->unlinked_file_pool
) {
388 DBG("File descriptor tracker created with a limit of %u simultaneously-opened FDs",
393 fd_tracker_destroy(tracker
);
397 void fd_tracker_log(struct fd_tracker
*tracker
)
399 struct fs_handle_tracked
*handle
;
400 struct unsuspendable_fd
*unsuspendable_fd
;
401 struct cds_lfht_iter iter
;
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
);
416 DBG_NO_LOC(" Tracked suspendable file descriptors");
417 cds_list_for_each_entry (handle
, &tracker
->active_handles
, handles_list_node
) {
418 fs_handle_tracked_log(handle
);
420 cds_list_for_each_entry (handle
, &tracker
->suspended_handles
, handles_list_node
) {
421 fs_handle_tracked_log(handle
);
423 if (!SUSPENDABLE_COUNT(tracker
)) {
427 DBG_NO_LOC(" Tracked unsuspendable file descriptors");
430 lttng::urcu::read_lock_guard read_lock
;
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
);
440 if (!UNSUSPENDABLE_COUNT(tracker
)) {
444 pthread_mutex_unlock(&tracker
->lock
);
447 int fd_tracker_destroy(struct fd_tracker
*tracker
)
455 * Refuse to destroy the tracker as fs_handles may still old
456 * weak references to the tracker.
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",
461 TRACKED_COUNT(tracker
));
462 pthread_mutex_unlock(&tracker
->lock
);
463 fd_tracker_log(tracker
);
467 pthread_mutex_unlock(&tracker
->lock
);
469 if (tracker
->unsuspendable_fds
) {
470 ret
= cds_lfht_destroy(tracker
->unsuspendable_fds
, nullptr);
474 lttng_inode_registry_destroy(tracker
->inode_registry
);
475 lttng_unlinked_file_pool_destroy(tracker
->unlinked_file_pool
);
476 pthread_mutex_destroy(&tracker
->lock
);
482 struct fs_handle
*fd_tracker_open_fs_handle(struct fd_tracker
*tracker
,
483 struct lttng_directory_handle
*directory
,
489 struct fs_handle_tracked
*handle
= nullptr;
491 struct open_properties properties
= { .flags
= flags
,
495 static_cast<mode_t
>(mode
? *mode
: 0),
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);
507 * There are not enough active suspendable file
508 * descriptors to open a new fd and still accommodate
509 * the tracker's capacity.
511 WARN("Cannot open file system handle, too many unsuspendable file descriptors are opened (%u)",
512 tracker
->count
.unsuspendable
);
517 handle
= zmalloc
<fs_handle_tracked
>();
521 handle
->parent
= (typeof(handle
->parent
)){
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
,
528 handle
->tracker
= tracker
;
530 ret
= pthread_mutex_init(&handle
->lock
, nullptr);
532 PERROR("Failed to initialize handle mutex while creating fs handle");
533 goto error_mutex_init
;
536 handle
->fd
= open_from_properties(directory
, path
, &properties
);
537 if (handle
->fd
< 0) {
538 PERROR("Failed to open fs handle to %s, open() returned", path
);
542 handle
->properties
= properties
;
544 handle
->inode
= lttng_inode_registry_get_inode(
545 tracker
->inode_registry
, directory
, path
, handle
->fd
, tracker
->unlinked_file_pool
);
546 if (!handle
->inode
) {
547 ERR("Failed to get lttng_inode corresponding to file %s", path
);
551 if (fstat(handle
->fd
, &fd_stat
)) {
552 PERROR("Failed to retrieve file descriptor inode while creating fs handle, fstat() returned");
555 handle
->ino
= fd_stat
.st_ino
;
557 fd_tracker_track(tracker
, handle
);
559 pthread_mutex_unlock(&tracker
->lock
);
560 return handle
? &handle
->parent
: nullptr;
563 lttng_inode_put(handle
->inode
);
565 pthread_mutex_destroy(&handle
->lock
);
572 /* Caller must hold the tracker's lock. */
573 static int fd_tracker_suspend_handles(struct fd_tracker
*tracker
, unsigned int count
)
575 unsigned int left_to_close
= count
;
576 unsigned int attempts_left
= tracker
->count
.suspendable
.active
;
577 struct fs_handle_tracked
*handle
, *tmp
;
579 cds_list_for_each_entry_safe (handle
, tmp
, &tracker
->active_handles
, handles_list_node
) {
582 fd_tracker_untrack(tracker
, handle
);
583 ret
= fs_handle_tracked_suspend(handle
);
584 fd_tracker_track(tracker
, handle
);
590 if (left_to_close
== 0 || attempts_left
== 0) {
594 return left_to_close
? -EMFILE
: 0;
597 int fd_tracker_open_unsuspendable_fd(struct fd_tracker
*tracker
,
600 unsigned int fd_count
,
604 int ret
, user_ret
, i
, fds_to_suspend
;
605 unsigned int active_fds
;
606 struct unsuspendable_fd
**entries
;
607 lttng::urcu::read_lock_guard read_lock
;
609 entries
= calloc
<unsuspendable_fd
*>(fd_count
);
615 pthread_mutex_lock(&tracker
->lock
);
617 active_fds
= ACTIVE_COUNT(tracker
);
618 fds_to_suspend
= (int) active_fds
+ (int) fd_count
- (int) tracker
->capacity
;
619 if (fds_to_suspend
> 0) {
620 if (fds_to_suspend
<= tracker
->count
.suspendable
.active
) {
621 ret
= fd_tracker_suspend_handles(tracker
, fds_to_suspend
);
627 * There are not enough active suspendable file
628 * descriptors to open a new fd and still accommodates the
629 * tracker's capacity.
631 WARN("Cannot open unsuspendable fd, too many unsuspendable file descriptors are opened (%u)",
632 tracker
->count
.unsuspendable
);
638 user_ret
= open(user_data
, out_fds
);
645 * Add the fds returned by the user's callback to the hashtable
646 * of unsuspendable fds.
648 for (i
= 0; i
< fd_count
; i
++) {
649 struct unsuspendable_fd
*entry
=
650 unsuspendable_fd_create(names
? names
[i
] : nullptr, out_fds
[i
]);
654 goto end_free_entries
;
659 for (i
= 0; i
< fd_count
; i
++) {
660 struct cds_lfht_node
*node
;
661 struct unsuspendable_fd
*entry
= entries
[i
];
663 node
= cds_lfht_add_unique(tracker
->unsuspendable_fds
,
664 hash_key_ulong((void *) (unsigned long) out_fds
[i
],
667 (void *) (unsigned long) out_fds
[i
],
668 &entry
->tracker_node
);
670 if (node
!= &entry
->tracker_node
) {
672 goto end_free_entries
;
674 entries
[i
] = nullptr;
676 tracker
->count
.unsuspendable
+= fd_count
;
679 pthread_mutex_unlock(&tracker
->lock
);
684 for (i
= 0; i
< fd_count
; i
++) {
685 unsuspendable_fd_destroy(entries
[i
]);
690 int fd_tracker_close_unsuspendable_fd(struct fd_tracker
*tracker
,
692 unsigned int fd_count
,
696 int i
, ret
, user_ret
;
698 lttng::urcu::read_lock_guard read_lock
;
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).
704 fds
= malloc
<int>(sizeof(*fds
) * fd_count
);
709 memcpy(fds
, fds_in
, sizeof(*fds
) * fd_count
);
711 pthread_mutex_lock(&tracker
->lock
);
713 /* Let the user close the file descriptors. */
714 user_ret
= close(user_data
, fds_in
);
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
;
726 cds_lfht_lookup(tracker
->unsuspendable_fds
,
727 hash_key_ulong((void *) (unsigned long) fds
[i
], seed
.value
),
729 (void *) (unsigned long) fds
[i
],
731 node
= cds_lfht_iter_get_node(&iter
);
733 /* Unknown file descriptor. */
734 WARN("Untracked file descriptor %d passed to fd_tracker_close_unsuspendable_fd()",
739 entry
= lttng::utils::container_of(node
, &unsuspendable_fd::tracker_node
);
741 cds_lfht_del(tracker
->unsuspendable_fds
, node
);
742 unsuspendable_fd_destroy(entry
);
746 tracker
->count
.unsuspendable
-= fd_count
;
749 pthread_mutex_unlock(&tracker
->lock
);
755 /* Caller must have taken the tracker's and handle's locks. */
756 static void fd_tracker_track(struct fd_tracker
*tracker
, struct fs_handle_tracked
*handle
)
758 if (handle
->fd
>= 0) {
759 tracker
->count
.suspendable
.active
++;
760 cds_list_add_tail(&handle
->handles_list_node
, &tracker
->active_handles
);
762 tracker
->count
.suspendable
.suspended
++;
763 cds_list_add_tail(&handle
->handles_list_node
, &tracker
->suspended_handles
);
767 /* Caller must have taken the tracker's and handle's locks. */
768 static void fd_tracker_untrack(struct fd_tracker
*tracker
, struct fs_handle_tracked
*handle
)
770 if (handle
->fd
>= 0) {
771 tracker
->count
.suspendable
.active
--;
773 tracker
->count
.suspendable
.suspended
--;
775 cds_list_del(&handle
->handles_list_node
);
778 /* Caller must have taken the tracker's and handle's locks. */
779 static int fd_tracker_restore_handle(struct fd_tracker
*tracker
, struct fs_handle_tracked
*handle
)
783 fd_tracker_untrack(tracker
, handle
);
784 if (ACTIVE_COUNT(tracker
) >= tracker
->capacity
) {
785 ret
= fd_tracker_suspend_handles(tracker
, 1);
790 ret
= fs_handle_tracked_restore(handle
);
792 fd_tracker_track(tracker
, handle
);
793 return ret
? ret
: handle
->fd
;
796 static int fs_handle_tracked_get_fd(struct fs_handle
*_handle
)
799 struct fs_handle_tracked
*handle
=
800 lttng::utils::container_of(_handle
, &fs_handle_tracked::parent
);
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.
810 * Note that the lock's nesting order must still be respected here.
811 * The handle's lock nests inside the tracker's lock.
813 pthread_mutex_lock(&handle
->tracker
->lock
);
814 pthread_mutex_lock(&handle
->lock
);
815 LTTNG_ASSERT(!handle
->in_use
);
817 handle
->tracker
->stats
.uses
++;
818 if (handle
->fd
>= 0) {
820 /* Mark as most recently used. */
821 fd_tracker_untrack(handle
->tracker
, handle
);
822 fd_tracker_track(handle
->tracker
, handle
);
824 handle
->tracker
->stats
.misses
++;
825 ret
= fd_tracker_restore_handle(handle
->tracker
, handle
);
827 handle
->tracker
->stats
.errors
++;
831 handle
->in_use
= true;
833 pthread_mutex_unlock(&handle
->lock
);
834 pthread_mutex_unlock(&handle
->tracker
->lock
);
838 static void fs_handle_tracked_put_fd(struct fs_handle
*_handle
)
840 struct fs_handle_tracked
*handle
=
841 lttng::utils::container_of(_handle
, &fs_handle_tracked::parent
);
843 pthread_mutex_lock(&handle
->lock
);
844 handle
->in_use
= false;
845 pthread_mutex_unlock(&handle
->lock
);
848 static int fs_handle_tracked_unlink(struct fs_handle
*_handle
)
851 struct fs_handle_tracked
*handle
=
852 lttng::utils::container_of(_handle
, &fs_handle_tracked::parent
);
854 pthread_mutex_lock(&handle
->tracker
->lock
);
855 pthread_mutex_lock(&handle
->lock
);
856 ret
= lttng_inode_unlink(handle
->inode
);
857 pthread_mutex_unlock(&handle
->lock
);
858 pthread_mutex_unlock(&handle
->tracker
->lock
);
862 static int fs_handle_tracked_close(struct fs_handle
*_handle
)
865 const char *path
= nullptr;
866 struct fs_handle_tracked
*handle
=
867 lttng::utils::container_of(_handle
, &fs_handle_tracked::parent
);
868 struct lttng_directory_handle
*inode_directory_handle
= nullptr;
875 pthread_mutex_lock(&handle
->tracker
->lock
);
876 pthread_mutex_lock(&handle
->lock
);
878 lttng_inode_borrow_location(handle
->inode
, nullptr, &path
);
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.
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.
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().
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
898 inode_directory_handle
= lttng_inode_get_location_directory_handle(handle
->inode
);
900 fd_tracker_untrack(handle
->tracker
, handle
);
901 if (handle
->fd
>= 0) {
903 * The return value of close() is not propagated as there
904 * isn't much the user can do about it.
906 if (close(handle
->fd
)) {
907 PERROR("Failed to close the file descriptor (%d) of fs handle to %s, close() returned",
909 path
? path
: "Unknown");
914 lttng_inode_put(handle
->inode
);
916 pthread_mutex_unlock(&handle
->lock
);
917 pthread_mutex_destroy(&handle
->lock
);
918 pthread_mutex_unlock(&handle
->tracker
->lock
);
920 lttng_directory_handle_put(inode_directory_handle
);