2 * Copyright (C) 2018-2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
10 #include <urcu/rculfhash.h>
17 #include <sys/types.h>
19 #include <common/defaults.h>
20 #include <common/error.h>
21 #include <common/fs-handle-internal.h>
22 #include <common/hashtable/hashtable.h>
23 #include <common/hashtable/utils.h>
24 #include <common/macros.h>
25 #include <common/optional.h>
27 #include "fd-tracker.h"
30 /* Tracker lock must be taken by the user. */
31 #define TRACKED_COUNT(tracker) \
32 (tracker->count.suspendable.active + \
33 tracker->count.suspendable.suspended + \
34 tracker->count.unsuspendable)
36 /* Tracker lock must be taken by the user. */
37 #define ACTIVE_COUNT(tracker) \
38 (tracker->count.suspendable.active + tracker->count.unsuspendable)
40 /* Tracker lock must be taken by the user. */
41 #define SUSPENDED_COUNT(tracker) (tracker->count.suspendable.suspended)
43 /* Tracker lock must be taken by the user. */
44 #define SUSPENDABLE_COUNT(tracker) \
45 (tracker->count.suspendable.active + \
46 tracker->count.suspendable.suspended)
48 /* Tracker lock must be taken by the user. */
49 #define UNSUSPENDABLE_COUNT(tracker) (tracker->count.unsuspendable)
56 unsigned int suspended
;
58 unsigned int unsuspendable
;
60 unsigned int capacity
;
64 /* Failures to suspend or restore fs handles. */
68 * The head of the active_handles list is always the least recently
69 * used active handle. When an handle is used, it is removed from the
70 * list and added to the end. When a file has to be suspended, the
71 * first element in the list is "popped", suspended, and added to the
72 * list of suspended handles.
74 struct cds_list_head active_handles
;
75 struct cds_list_head suspended_handles
;
76 struct cds_lfht
*unsuspendable_fds
;
77 struct lttng_inode_registry
*inode_registry
;
78 /* Unlinked files are moved in this directory under a unique name. */
79 struct lttng_directory_handle
*unlink_directory_handle
;
80 struct lttng_unlinked_file_pool
*unlinked_file_pool
;
83 struct open_properties
{
85 LTTNG_OPTIONAL(mode_t
) mode
;
89 * A fs_handle_tracked is not ref-counted. Therefore, it is assumed that a
90 * handle is never in-use while it is being reclaimed. It can be
91 * shared by multiple threads, but external synchronization is required
92 * to ensure it is not still being used when it is reclaimed (close method).
93 * In this respect, it is not different from a regular file descriptor.
95 * The fs_handle lock always nests _within_ the tracker's lock.
97 struct fs_handle_tracked
{
98 struct fs_handle parent
;
101 * Weak reference to the tracker. All fs_handles are assumed to have
102 * been closed at the moment of the destruction of the fd_tracker.
104 struct fd_tracker
*tracker
;
105 struct open_properties properties
;
106 struct lttng_inode
*inode
;
108 /* inode number of the file at the time of the handle's creation. */
111 /* Offset to which the file should be restored. */
113 struct cds_list_head handles_list_node
;
116 struct unsuspendable_fd
{
118 * Accesses are only performed through the tracker, which is protected
123 struct cds_lfht_node tracker_node
;
124 struct rcu_head rcu_head
;
128 pthread_mutex_t lock
;
132 .lock
= PTHREAD_MUTEX_INITIALIZER
,
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(
138 const char *name
, int fd
);
139 static int open_from_properties(const struct lttng_directory_handle
*dir_handle
,
140 const char *path
, 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(
151 struct fd_tracker
*tracker
, struct fs_handle_tracked
*handle
);
152 static void fd_tracker_untrack(
153 struct fd_tracker
*tracker
, struct fs_handle_tracked
*handle
);
154 static int fd_tracker_suspend_handles(
155 struct fd_tracker
*tracker
, unsigned int count
);
156 static int fd_tracker_restore_handle(
157 struct fd_tracker
*tracker
, struct fs_handle_tracked
*handle
);
159 /* Match function of the tracker's unsuspendable_fds hash table. */
160 static int match_fd(struct cds_lfht_node
*node
, const void *key
)
162 struct unsuspendable_fd
*entry
= caa_container_of(
163 node
, struct unsuspendable_fd
, tracker_node
);
165 return hash_match_key_ulong(
166 (void *) (unsigned long) entry
->fd
, (void *) key
);
169 static void delete_unsuspendable_fd(struct rcu_head
*head
)
171 struct unsuspendable_fd
*fd
= caa_container_of(
172 head
, struct unsuspendable_fd
, rcu_head
);
178 static void unsuspendable_fd_destroy(struct unsuspendable_fd
*entry
)
183 call_rcu(&entry
->rcu_head
, delete_unsuspendable_fd
);
186 static struct unsuspendable_fd
*unsuspendable_fd_create(
187 const char *name
, int fd
)
189 struct unsuspendable_fd
*entry
= (unsuspendable_fd
*) zmalloc(sizeof(*entry
));
195 entry
->name
= strdup(name
);
200 cds_lfht_node_init(&entry
->tracker_node
);
204 unsuspendable_fd_destroy(entry
);
208 static void fs_handle_tracked_log(struct fs_handle_tracked
*handle
)
212 pthread_mutex_lock(&handle
->lock
);
213 lttng_inode_borrow_location(handle
->inode
, NULL
, &path
);
215 if (handle
->fd
>= 0) {
216 DBG_NO_LOC(" %s [active, fd %d%s]", path
, handle
->fd
,
217 handle
->in_use
? ", in use" : "");
219 DBG_NO_LOC(" %s [suspended]", path
);
221 pthread_mutex_unlock(&handle
->lock
);
224 /* Tracker lock must be held by the caller. */
225 static int fs_handle_tracked_suspend(struct fs_handle_tracked
*handle
)
230 const struct lttng_directory_handle
*node_directory_handle
;
232 pthread_mutex_lock(&handle
->lock
);
233 lttng_inode_borrow_location(
234 handle
->inode
, &node_directory_handle
, &path
);
235 LTTNG_ASSERT(handle
->fd
>= 0);
236 if (handle
->in_use
) {
237 /* This handle can't be suspended as it is currently in use. */
242 ret
= lttng_directory_handle_stat(
243 node_directory_handle
, path
, &fs_stat
);
245 PERROR("Filesystem handle to %s cannot be suspended as stat() failed",
251 if (fs_stat
.st_ino
!= handle
->ino
) {
252 /* Don't suspend as the handle would not be restorable. */
253 WARN("Filesystem handle to %s cannot be suspended as its inode changed",
259 handle
->offset
= lseek(handle
->fd
, 0, SEEK_CUR
);
260 if (handle
->offset
== -1) {
261 WARN("Filesystem handle to %s cannot be suspended as lseek() failed to sample its current position",
267 ret
= close(handle
->fd
);
269 PERROR("Filesystem handle to %s cannot be suspended as close() failed",
274 DBG("Suspended filesystem handle to %s (fd %i) at position %" PRId64
,
275 path
, handle
->fd
, handle
->offset
);
279 handle
->tracker
->stats
.errors
++;
281 pthread_mutex_unlock(&handle
->lock
);
285 /* Caller must hold the tracker and handle's locks. */
286 static int fs_handle_tracked_restore(struct fs_handle_tracked
*handle
)
290 const struct lttng_directory_handle
*node_directory_handle
;
292 lttng_inode_borrow_location(
293 handle
->inode
, &node_directory_handle
, &path
);
295 LTTNG_ASSERT(handle
->fd
== -1);
297 ret
= open_from_properties(
298 node_directory_handle
, path
, &handle
->properties
);
300 PERROR("Failed to restore filesystem handle to %s, open() failed",
307 ret
= lseek(fd
, handle
->offset
, SEEK_SET
);
309 PERROR("Failed to restore filesystem handle to %s, lseek() failed",
314 DBG("Restored filesystem handle to %s (fd %i) at position %" PRId64
,
315 path
, fd
, handle
->offset
);
326 static int open_from_properties(const struct lttng_directory_handle
*dir_handle
,
327 const char *path
, struct open_properties
*properties
)
332 * open() ignores the 'flags' parameter unless the O_CREAT or O_TMPFILE
333 * flags are set. O_TMPFILE would not make sense in the context of a
334 * suspendable fs_handle as it would not be restorable (see OPEN(2)),
335 * thus it is ignored here.
337 if ((properties
->flags
& O_CREAT
) && properties
->mode
.is_set
) {
338 ret
= lttng_directory_handle_open_file(dir_handle
, path
,
339 properties
->flags
, properties
->mode
.value
);
341 ret
= lttng_directory_handle_open_file(dir_handle
, path
,
342 properties
->flags
, 0);
345 * Some flags should not be used beyond the initial open() of a
346 * restorable file system handle. O_CREAT and O_TRUNC must
347 * be cleared since it would be unexpected to re-use them
348 * when the handle is retored:
349 * - O_CREAT should not be needed as the file has been created
350 * on the initial call to open(),
351 * - O_TRUNC would destroy the file's contents by truncating it
354 properties
->flags
&= ~(O_CREAT
| O_TRUNC
);
363 struct fd_tracker
*fd_tracker_create(const char *unlinked_file_path
,
364 unsigned int capacity
)
366 struct fd_tracker
*tracker
= (fd_tracker
*) zmalloc(sizeof(struct fd_tracker
));
372 pthread_mutex_lock(&seed
.lock
);
373 if (!seed
.initialized
) {
374 seed
.value
= (unsigned long) time(NULL
);
375 seed
.initialized
= true;
377 pthread_mutex_unlock(&seed
.lock
);
379 CDS_INIT_LIST_HEAD(&tracker
->active_handles
);
380 CDS_INIT_LIST_HEAD(&tracker
->suspended_handles
);
381 tracker
->capacity
= capacity
;
382 tracker
->unsuspendable_fds
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
383 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
384 if (!tracker
->unsuspendable_fds
) {
385 ERR("Failed to create fd-tracker's unsuspendable_fds hash table");
388 tracker
->inode_registry
= lttng_inode_registry_create();
389 if (!tracker
->inode_registry
) {
390 ERR("Failed to create fd-tracker's inode registry");
394 tracker
->unlinked_file_pool
=
395 lttng_unlinked_file_pool_create(unlinked_file_path
);
396 if (!tracker
->unlinked_file_pool
) {
399 DBG("File descriptor tracker created with a limit of %u simultaneously-opened FDs",
404 fd_tracker_destroy(tracker
);
408 void fd_tracker_log(struct fd_tracker
*tracker
)
410 struct fs_handle_tracked
*handle
;
411 struct unsuspendable_fd
*unsuspendable_fd
;
412 struct cds_lfht_iter iter
;
414 pthread_mutex_lock(&tracker
->lock
);
415 DBG_NO_LOC("File descriptor tracker");
416 DBG_NO_LOC(" Stats:");
417 DBG_NO_LOC(" uses: %" PRIu64
, tracker
->stats
.uses
);
418 DBG_NO_LOC(" misses: %" PRIu64
, tracker
->stats
.misses
);
419 DBG_NO_LOC(" errors: %" PRIu64
, tracker
->stats
.errors
);
420 DBG_NO_LOC(" Tracked: %u", TRACKED_COUNT(tracker
));
421 DBG_NO_LOC(" active: %u", ACTIVE_COUNT(tracker
));
422 DBG_NO_LOC(" suspendable: %u", SUSPENDABLE_COUNT(tracker
));
423 DBG_NO_LOC(" unsuspendable: %u", UNSUSPENDABLE_COUNT(tracker
));
424 DBG_NO_LOC(" suspended: %u", SUSPENDED_COUNT(tracker
));
425 DBG_NO_LOC(" capacity: %u", tracker
->capacity
);
427 DBG_NO_LOC(" Tracked suspendable file descriptors");
428 cds_list_for_each_entry(
429 handle
, &tracker
->active_handles
, handles_list_node
) {
430 fs_handle_tracked_log(handle
);
432 cds_list_for_each_entry(handle
, &tracker
->suspended_handles
,
434 fs_handle_tracked_log(handle
);
436 if (!SUSPENDABLE_COUNT(tracker
)) {
440 DBG_NO_LOC(" Tracked unsuspendable file descriptors");
442 cds_lfht_for_each_entry(tracker
->unsuspendable_fds
, &iter
,
443 unsuspendable_fd
, tracker_node
) {
444 DBG_NO_LOC(" %s [active, fd %d]",
445 unsuspendable_fd
->name
?: "Unnamed",
446 unsuspendable_fd
->fd
);
449 if (!UNSUSPENDABLE_COUNT(tracker
)) {
453 pthread_mutex_unlock(&tracker
->lock
);
456 int fd_tracker_destroy(struct fd_tracker
*tracker
)
464 * Refuse to destroy the tracker as fs_handles may still old
465 * weak references to the tracker.
467 pthread_mutex_lock(&tracker
->lock
);
468 if (TRACKED_COUNT(tracker
)) {
469 ERR("A file descriptor leak has been detected: %u tracked file descriptors are still being tracked",
470 TRACKED_COUNT(tracker
));
471 pthread_mutex_unlock(&tracker
->lock
);
472 fd_tracker_log(tracker
);
476 pthread_mutex_unlock(&tracker
->lock
);
478 if (tracker
->unsuspendable_fds
) {
479 ret
= cds_lfht_destroy(tracker
->unsuspendable_fds
, NULL
);
483 lttng_inode_registry_destroy(tracker
->inode_registry
);
484 lttng_unlinked_file_pool_destroy(tracker
->unlinked_file_pool
);
485 pthread_mutex_destroy(&tracker
->lock
);
491 struct fs_handle
*fd_tracker_open_fs_handle(struct fd_tracker
*tracker
,
492 struct lttng_directory_handle
*directory
,
498 struct fs_handle_tracked
*handle
= NULL
;
500 struct open_properties properties
= {
504 .value
= mode
? *mode
: 0,
508 pthread_mutex_lock(&tracker
->lock
);
509 if (ACTIVE_COUNT(tracker
) == tracker
->capacity
) {
510 if (tracker
->count
.suspendable
.active
> 0) {
511 ret
= fd_tracker_suspend_handles(tracker
, 1);
517 * There are not enough active suspendable file
518 * descriptors to open a new fd and still accommodate
519 * the tracker's capacity.
521 WARN("Cannot open file system handle, too many unsuspendable file descriptors are opened (%u)",
522 tracker
->count
.unsuspendable
);
527 handle
= (fs_handle_tracked
*) zmalloc(sizeof(*handle
));
531 handle
->parent
= (typeof(handle
->parent
)) {
532 .get_fd
= fs_handle_tracked_get_fd
,
533 .put_fd
= fs_handle_tracked_put_fd
,
534 .unlink
= fs_handle_tracked_unlink
,
535 .close
= fs_handle_tracked_close
,
538 handle
->tracker
= tracker
;
540 ret
= pthread_mutex_init(&handle
->lock
, NULL
);
542 PERROR("Failed to initialize handle mutex while creating fs handle");
543 goto error_mutex_init
;
546 handle
->fd
= open_from_properties(directory
, path
, &properties
);
547 if (handle
->fd
< 0) {
548 PERROR("Failed to open fs handle to %s, open() returned", path
);
552 handle
->properties
= properties
;
554 handle
->inode
= lttng_inode_registry_get_inode(tracker
->inode_registry
,
555 directory
, path
, handle
->fd
,
556 tracker
->unlinked_file_pool
);
557 if (!handle
->inode
) {
558 ERR("Failed to get lttng_inode corresponding to file %s", path
);
562 if (fstat(handle
->fd
, &fd_stat
)) {
563 PERROR("Failed to retrieve file descriptor inode while creating fs handle, fstat() returned");
566 handle
->ino
= fd_stat
.st_ino
;
568 fd_tracker_track(tracker
, handle
);
570 pthread_mutex_unlock(&tracker
->lock
);
571 return handle
? &handle
->parent
: NULL
;
574 lttng_inode_put(handle
->inode
);
576 pthread_mutex_destroy(&handle
->lock
);
583 /* Caller must hold the tracker's lock. */
584 static int fd_tracker_suspend_handles(
585 struct fd_tracker
*tracker
, unsigned int count
)
587 unsigned int left_to_close
= count
;
588 unsigned int attempts_left
= tracker
->count
.suspendable
.active
;
589 struct fs_handle_tracked
*handle
, *tmp
;
591 cds_list_for_each_entry_safe(handle
, tmp
, &tracker
->active_handles
,
595 fd_tracker_untrack(tracker
, handle
);
596 ret
= fs_handle_tracked_suspend(handle
);
597 fd_tracker_track(tracker
, handle
);
603 if (left_to_close
== 0 || attempts_left
== 0) {
607 return left_to_close
? -EMFILE
: 0;
610 int fd_tracker_open_unsuspendable_fd(struct fd_tracker
*tracker
,
613 unsigned int fd_count
,
617 int ret
, user_ret
, i
, fds_to_suspend
;
618 unsigned int active_fds
;
619 struct unsuspendable_fd
**entries
;
621 entries
= (unsuspendable_fd
**) zmalloc(fd_count
* sizeof(*entries
));
627 pthread_mutex_lock(&tracker
->lock
);
629 active_fds
= ACTIVE_COUNT(tracker
);
630 fds_to_suspend
= (int) active_fds
+ (int) fd_count
-
631 (int) tracker
->capacity
;
632 if (fds_to_suspend
> 0) {
633 if (fds_to_suspend
<= tracker
->count
.suspendable
.active
) {
634 ret
= fd_tracker_suspend_handles(
635 tracker
, fds_to_suspend
);
641 * There are not enough active suspendable file
642 * descriptors to open a new fd and still accommodates the
643 * tracker's capacity.
645 WARN("Cannot open unsuspendable fd, too many unsuspendable file descriptors are opened (%u)",
646 tracker
->count
.unsuspendable
);
652 user_ret
= open(user_data
, out_fds
);
659 * Add the fds returned by the user's callback to the hashtable
660 * of unsuspendable fds.
662 for (i
= 0; i
< fd_count
; i
++) {
663 struct unsuspendable_fd
*entry
= unsuspendable_fd_create(
664 names
? names
[i
] : NULL
, out_fds
[i
]);
668 goto end_free_entries
;
674 for (i
= 0; i
< fd_count
; i
++) {
675 struct cds_lfht_node
*node
;
676 struct unsuspendable_fd
*entry
= entries
[i
];
678 node
= cds_lfht_add_unique(tracker
->unsuspendable_fds
,
679 hash_key_ulong((void *) (unsigned long)
682 match_fd
, (void *) (unsigned long) out_fds
[i
],
683 &entry
->tracker_node
);
685 if (node
!= &entry
->tracker_node
) {
688 goto end_free_entries
;
692 tracker
->count
.unsuspendable
+= fd_count
;
696 pthread_mutex_unlock(&tracker
->lock
);
701 for (i
= 0; i
< fd_count
; i
++) {
702 unsuspendable_fd_destroy(entries
[i
]);
707 int fd_tracker_close_unsuspendable_fd(struct fd_tracker
*tracker
,
709 unsigned int fd_count
,
713 int i
, ret
, user_ret
;
717 * Maintain a local copy of fds_in as the user's callback may modify its
718 * contents (e.g. setting the fd(s) to -1 after close).
720 fds
= (int *) malloc(sizeof(*fds
) * fd_count
);
725 memcpy(fds
, fds_in
, sizeof(*fds
) * fd_count
);
727 pthread_mutex_lock(&tracker
->lock
);
730 /* Let the user close the file descriptors. */
731 user_ret
= close(user_data
, fds_in
);
737 /* Untrack the fds that were just closed by the user's callback. */
738 for (i
= 0; i
< fd_count
; i
++) {
739 struct cds_lfht_node
*node
;
740 struct cds_lfht_iter iter
;
741 struct unsuspendable_fd
*entry
;
743 cds_lfht_lookup(tracker
->unsuspendable_fds
,
744 hash_key_ulong((void *) (unsigned long) fds
[i
],
746 match_fd
, (void *) (unsigned long) fds
[i
],
748 node
= cds_lfht_iter_get_node(&iter
);
750 /* Unknown file descriptor. */
751 WARN("Untracked file descriptor %d passed to fd_tracker_close_unsuspendable_fd()",
756 entry
= caa_container_of(
757 node
, struct unsuspendable_fd
, tracker_node
);
759 cds_lfht_del(tracker
->unsuspendable_fds
, node
);
760 unsuspendable_fd_destroy(entry
);
764 tracker
->count
.unsuspendable
-= fd_count
;
768 pthread_mutex_unlock(&tracker
->lock
);
774 /* Caller must have taken the tracker's and handle's locks. */
775 static void fd_tracker_track(
776 struct fd_tracker
*tracker
, struct fs_handle_tracked
*handle
)
778 if (handle
->fd
>= 0) {
779 tracker
->count
.suspendable
.active
++;
780 cds_list_add_tail(&handle
->handles_list_node
,
781 &tracker
->active_handles
);
783 tracker
->count
.suspendable
.suspended
++;
784 cds_list_add_tail(&handle
->handles_list_node
,
785 &tracker
->suspended_handles
);
789 /* Caller must have taken the tracker's and handle's locks. */
790 static void fd_tracker_untrack(
791 struct fd_tracker
*tracker
, struct fs_handle_tracked
*handle
)
793 if (handle
->fd
>= 0) {
794 tracker
->count
.suspendable
.active
--;
796 tracker
->count
.suspendable
.suspended
--;
798 cds_list_del(&handle
->handles_list_node
);
801 /* Caller must have taken the tracker's and handle's locks. */
802 static int fd_tracker_restore_handle(
803 struct fd_tracker
*tracker
, struct fs_handle_tracked
*handle
)
807 fd_tracker_untrack(tracker
, handle
);
808 if (ACTIVE_COUNT(tracker
) >= tracker
->capacity
) {
809 ret
= fd_tracker_suspend_handles(tracker
, 1);
814 ret
= fs_handle_tracked_restore(handle
);
816 fd_tracker_track(tracker
, handle
);
817 return ret
? ret
: handle
->fd
;
820 static int fs_handle_tracked_get_fd(struct fs_handle
*_handle
)
823 struct fs_handle_tracked
*handle
=
824 container_of(_handle
, struct fs_handle_tracked
, parent
);
827 * TODO This should be optimized as it is a fairly hot path.
828 * The fd-tracker's lock should only be taken when a fs_handle is
829 * restored (slow path). On the fast path (fs_handle is active),
830 * the only effect on the fd_tracker is marking the handle as the
831 * most recently used. Currently, it is done by a call to the
832 * track/untrack helpers, but it should be done atomically.
834 * Note that the lock's nesting order must still be respected here.
835 * The handle's lock nests inside the tracker's lock.
837 pthread_mutex_lock(&handle
->tracker
->lock
);
838 pthread_mutex_lock(&handle
->lock
);
839 LTTNG_ASSERT(!handle
->in_use
);
841 handle
->tracker
->stats
.uses
++;
842 if (handle
->fd
>= 0) {
844 /* Mark as most recently used. */
845 fd_tracker_untrack(handle
->tracker
, handle
);
846 fd_tracker_track(handle
->tracker
, handle
);
848 handle
->tracker
->stats
.misses
++;
849 ret
= fd_tracker_restore_handle(handle
->tracker
, handle
);
851 handle
->tracker
->stats
.errors
++;
855 handle
->in_use
= true;
857 pthread_mutex_unlock(&handle
->lock
);
858 pthread_mutex_unlock(&handle
->tracker
->lock
);
862 static void fs_handle_tracked_put_fd(struct fs_handle
*_handle
)
864 struct fs_handle_tracked
*handle
=
865 container_of(_handle
, struct fs_handle_tracked
, parent
);
867 pthread_mutex_lock(&handle
->lock
);
868 handle
->in_use
= false;
869 pthread_mutex_unlock(&handle
->lock
);
872 static int fs_handle_tracked_unlink(struct fs_handle
*_handle
)
875 struct fs_handle_tracked
*handle
=
876 container_of(_handle
, struct fs_handle_tracked
, parent
);
878 pthread_mutex_lock(&handle
->tracker
->lock
);
879 pthread_mutex_lock(&handle
->lock
);
880 ret
= lttng_inode_unlink(handle
->inode
);
881 pthread_mutex_unlock(&handle
->lock
);
882 pthread_mutex_unlock(&handle
->tracker
->lock
);
886 static int fs_handle_tracked_close(struct fs_handle
*_handle
)
889 const char *path
= NULL
;
890 struct fs_handle_tracked
*handle
=
891 container_of(_handle
, struct fs_handle_tracked
, parent
);
892 struct lttng_directory_handle
*inode_directory_handle
= NULL
;
899 pthread_mutex_lock(&handle
->tracker
->lock
);
900 pthread_mutex_lock(&handle
->lock
);
902 lttng_inode_borrow_location(handle
->inode
, NULL
, &path
);
904 * Here a reference to the inode's directory handle is acquired
905 * to prevent the last reference to it from being released while
906 * the tracker's lock is taken.
908 * If this wasn't done, the directory handle could attempt to
909 * close its underlying directory file descriptor, which would
910 * attempt to lock the tracker's lock, resulting in a deadlock.
912 * Since a new reference to the directory handle is taken within
913 * the scope of this function, it is not possible for the last
914 * reference to the inode's location directory handle to be
915 * released during the call to lttng_inode_put().
917 * We wait until the tracker's lock is released to release the
918 * reference. Hence, the call to the tracker is delayed just
919 * enough to not attempt to recursively acquire the tracker's
922 inode_directory_handle
=
923 lttng_inode_get_location_directory_handle(
926 fd_tracker_untrack(handle
->tracker
, handle
);
927 if (handle
->fd
>= 0) {
929 * The return value of close() is not propagated as there
930 * isn't much the user can do about it.
932 if (close(handle
->fd
)) {
933 PERROR("Failed to close the file descriptor (%d) of fs handle to %s, close() returned",
934 handle
->fd
, path
? path
: "Unknown");
939 lttng_inode_put(handle
->inode
);
941 pthread_mutex_unlock(&handle
->lock
);
942 pthread_mutex_destroy(&handle
->lock
);
943 pthread_mutex_unlock(&handle
->tracker
->lock
);
945 lttng_directory_handle_put(inode_directory_handle
);