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
= 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 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 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
= 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
)
461 * Refuse to destroy the tracker as fs_handles may still old
462 * weak references to the tracker.
464 pthread_mutex_lock(&tracker
->lock
);
465 if (TRACKED_COUNT(tracker
)) {
466 ERR("A file descriptor leak has been detected: %u tracked file descriptors are still being tracked",
467 TRACKED_COUNT(tracker
));
468 pthread_mutex_unlock(&tracker
->lock
);
469 fd_tracker_log(tracker
);
473 pthread_mutex_unlock(&tracker
->lock
);
475 if (tracker
->unsuspendable_fds
) {
476 ret
= cds_lfht_destroy(tracker
->unsuspendable_fds
, NULL
);
480 lttng_inode_registry_destroy(tracker
->inode_registry
);
481 lttng_unlinked_file_pool_destroy(tracker
->unlinked_file_pool
);
482 pthread_mutex_destroy(&tracker
->lock
);
488 struct fs_handle
*fd_tracker_open_fs_handle(struct fd_tracker
*tracker
,
489 struct lttng_directory_handle
*directory
,
495 struct fs_handle_tracked
*handle
= NULL
;
497 struct open_properties properties
= {
499 .mode
.is_set
= !!mode
,
500 .mode
.value
= mode
? *mode
: 0,
503 pthread_mutex_lock(&tracker
->lock
);
504 if (ACTIVE_COUNT(tracker
) == tracker
->capacity
) {
505 if (tracker
->count
.suspendable
.active
> 0) {
506 ret
= fd_tracker_suspend_handles(tracker
, 1);
512 * There are not enough active suspendable file
513 * descriptors to open a new fd and still accommodate
514 * the tracker's capacity.
516 WARN("Cannot open file system handle, too many unsuspendable file descriptors are opened (%u)",
517 tracker
->count
.unsuspendable
);
522 handle
= zmalloc(sizeof(*handle
));
526 handle
->parent
= (typeof(handle
->parent
)) {
527 .get_fd
= fs_handle_tracked_get_fd
,
528 .put_fd
= fs_handle_tracked_put_fd
,
529 .unlink
= fs_handle_tracked_unlink
,
530 .close
= fs_handle_tracked_close
,
533 handle
->tracker
= tracker
;
535 ret
= pthread_mutex_init(&handle
->lock
, NULL
);
537 PERROR("Failed to initialize handle mutex while creating fs handle");
538 goto error_mutex_init
;
541 handle
->fd
= open_from_properties(directory
, path
, &properties
);
542 if (handle
->fd
< 0) {
543 PERROR("Failed to open fs handle to %s, open() returned", path
);
547 handle
->properties
= properties
;
549 handle
->inode
= lttng_inode_registry_get_inode(tracker
->inode_registry
,
550 directory
, path
, handle
->fd
,
551 tracker
->unlinked_file_pool
);
552 if (!handle
->inode
) {
553 ERR("Failed to get lttng_inode corresponding to file %s", path
);
557 if (fstat(handle
->fd
, &fd_stat
)) {
558 PERROR("Failed to retrieve file descriptor inode while creating fs handle, fstat() returned");
561 handle
->ino
= fd_stat
.st_ino
;
563 fd_tracker_track(tracker
, handle
);
565 pthread_mutex_unlock(&tracker
->lock
);
566 return handle
? &handle
->parent
: NULL
;
569 lttng_inode_put(handle
->inode
);
571 pthread_mutex_destroy(&handle
->lock
);
578 /* Caller must hold the tracker's lock. */
579 static int fd_tracker_suspend_handles(
580 struct fd_tracker
*tracker
, unsigned int count
)
582 unsigned int left_to_close
= count
;
583 unsigned int attempts_left
= tracker
->count
.suspendable
.active
;
584 struct fs_handle_tracked
*handle
, *tmp
;
586 cds_list_for_each_entry_safe(handle
, tmp
, &tracker
->active_handles
,
590 fd_tracker_untrack(tracker
, handle
);
591 ret
= fs_handle_tracked_suspend(handle
);
592 fd_tracker_track(tracker
, handle
);
598 if (left_to_close
== 0 || attempts_left
== 0) {
602 return left_to_close
? -EMFILE
: 0;
605 int fd_tracker_open_unsuspendable_fd(struct fd_tracker
*tracker
,
608 unsigned int fd_count
,
612 int ret
, user_ret
, i
, fds_to_suspend
;
613 unsigned int active_fds
;
614 struct unsuspendable_fd
**entries
;
616 entries
= zmalloc(fd_count
* sizeof(*entries
));
622 pthread_mutex_lock(&tracker
->lock
);
624 active_fds
= ACTIVE_COUNT(tracker
);
625 fds_to_suspend
= (int) active_fds
+ (int) fd_count
-
626 (int) tracker
->capacity
;
627 if (fds_to_suspend
> 0) {
628 if (fds_to_suspend
<= tracker
->count
.suspendable
.active
) {
629 ret
= fd_tracker_suspend_handles(
630 tracker
, fds_to_suspend
);
636 * There are not enough active suspendable file
637 * descriptors to open a new fd and still accomodate the
638 * tracker's capacity.
640 WARN("Cannot open unsuspendable fd, too many unsuspendable file descriptors are opened (%u)",
641 tracker
->count
.unsuspendable
);
647 user_ret
= open(user_data
, out_fds
);
654 * Add the fds returned by the user's callback to the hashtable
655 * of unsuspendable fds.
657 for (i
= 0; i
< fd_count
; i
++) {
658 struct unsuspendable_fd
*entry
= unsuspendable_fd_create(
659 names
? names
[i
] : NULL
, out_fds
[i
]);
663 goto end_free_entries
;
669 for (i
= 0; i
< fd_count
; i
++) {
670 struct cds_lfht_node
*node
;
671 struct unsuspendable_fd
*entry
= entries
[i
];
673 node
= cds_lfht_add_unique(tracker
->unsuspendable_fds
,
674 hash_key_ulong((void *) (unsigned long)
677 match_fd
, (void *) (unsigned long) out_fds
[i
],
678 &entry
->tracker_node
);
680 if (node
!= &entry
->tracker_node
) {
683 goto end_free_entries
;
687 tracker
->count
.unsuspendable
+= fd_count
;
691 pthread_mutex_unlock(&tracker
->lock
);
696 for (i
= 0; i
< fd_count
; i
++) {
697 unsuspendable_fd_destroy(entries
[i
]);
702 int fd_tracker_close_unsuspendable_fd(struct fd_tracker
*tracker
,
704 unsigned int fd_count
,
708 int i
, ret
, user_ret
;
712 * Maintain a local copy of fds_in as the user's callback may modify its
713 * contents (e.g. setting the fd(s) to -1 after close).
715 fds
= malloc(sizeof(*fds
) * fd_count
);
720 memcpy(fds
, fds_in
, sizeof(*fds
) * fd_count
);
722 pthread_mutex_lock(&tracker
->lock
);
725 /* Let the user close the file descriptors. */
726 user_ret
= close(user_data
, fds_in
);
732 /* Untrack the fds that were just closed by the user's callback. */
733 for (i
= 0; i
< fd_count
; i
++) {
734 struct cds_lfht_node
*node
;
735 struct cds_lfht_iter iter
;
736 struct unsuspendable_fd
*entry
;
738 cds_lfht_lookup(tracker
->unsuspendable_fds
,
739 hash_key_ulong((void *) (unsigned long) fds
[i
],
741 match_fd
, (void *) (unsigned long) fds
[i
],
743 node
= cds_lfht_iter_get_node(&iter
);
745 /* Unknown file descriptor. */
746 WARN("Untracked file descriptor %d passed to fd_tracker_close_unsuspendable_fd()",
751 entry
= caa_container_of(
752 node
, struct unsuspendable_fd
, tracker_node
);
754 cds_lfht_del(tracker
->unsuspendable_fds
, node
);
755 unsuspendable_fd_destroy(entry
);
759 tracker
->count
.unsuspendable
-= fd_count
;
763 pthread_mutex_unlock(&tracker
->lock
);
769 /* Caller must have taken the tracker's and handle's locks. */
770 static void fd_tracker_track(
771 struct fd_tracker
*tracker
, struct fs_handle_tracked
*handle
)
773 if (handle
->fd
>= 0) {
774 tracker
->count
.suspendable
.active
++;
775 cds_list_add_tail(&handle
->handles_list_node
,
776 &tracker
->active_handles
);
778 tracker
->count
.suspendable
.suspended
++;
779 cds_list_add_tail(&handle
->handles_list_node
,
780 &tracker
->suspended_handles
);
784 /* Caller must have taken the tracker's and handle's locks. */
785 static void fd_tracker_untrack(
786 struct fd_tracker
*tracker
, struct fs_handle_tracked
*handle
)
788 if (handle
->fd
>= 0) {
789 tracker
->count
.suspendable
.active
--;
791 tracker
->count
.suspendable
.suspended
--;
793 cds_list_del(&handle
->handles_list_node
);
796 /* Caller must have taken the tracker's and handle's locks. */
797 static int fd_tracker_restore_handle(
798 struct fd_tracker
*tracker
, struct fs_handle_tracked
*handle
)
802 fd_tracker_untrack(tracker
, handle
);
803 if (ACTIVE_COUNT(tracker
) >= tracker
->capacity
) {
804 ret
= fd_tracker_suspend_handles(tracker
, 1);
809 ret
= fs_handle_tracked_restore(handle
);
811 fd_tracker_track(tracker
, handle
);
812 return ret
? ret
: handle
->fd
;
815 static int fs_handle_tracked_get_fd(struct fs_handle
*_handle
)
818 struct fs_handle_tracked
*handle
=
819 container_of(_handle
, struct fs_handle_tracked
, parent
);
822 * TODO This should be optimized as it is a fairly hot path.
823 * The fd-tracker's lock should only be taken when a fs_handle is
824 * restored (slow path). On the fast path (fs_handle is active),
825 * the only effect on the fd_tracker is marking the handle as the
826 * most recently used. Currently, it is done by a call to the
827 * track/untrack helpers, but it should be done atomically.
829 * Note that the lock's nesting order must still be respected here.
830 * The handle's lock nests inside the tracker's lock.
832 pthread_mutex_lock(&handle
->tracker
->lock
);
833 pthread_mutex_lock(&handle
->lock
);
834 assert(!handle
->in_use
);
836 handle
->tracker
->stats
.uses
++;
837 if (handle
->fd
>= 0) {
839 /* Mark as most recently used. */
840 fd_tracker_untrack(handle
->tracker
, handle
);
841 fd_tracker_track(handle
->tracker
, handle
);
843 handle
->tracker
->stats
.misses
++;
844 ret
= fd_tracker_restore_handle(handle
->tracker
, handle
);
846 handle
->tracker
->stats
.errors
++;
850 handle
->in_use
= true;
852 pthread_mutex_unlock(&handle
->lock
);
853 pthread_mutex_unlock(&handle
->tracker
->lock
);
857 static void fs_handle_tracked_put_fd(struct fs_handle
*_handle
)
859 struct fs_handle_tracked
*handle
=
860 container_of(_handle
, struct fs_handle_tracked
, parent
);
862 pthread_mutex_lock(&handle
->lock
);
863 handle
->in_use
= false;
864 pthread_mutex_unlock(&handle
->lock
);
867 static int fs_handle_tracked_unlink(struct fs_handle
*_handle
)
870 struct fs_handle_tracked
*handle
=
871 container_of(_handle
, struct fs_handle_tracked
, parent
);
873 pthread_mutex_lock(&handle
->tracker
->lock
);
874 pthread_mutex_lock(&handle
->lock
);
875 ret
= lttng_inode_unlink(handle
->inode
);
876 pthread_mutex_unlock(&handle
->lock
);
877 pthread_mutex_unlock(&handle
->tracker
->lock
);
881 static int fs_handle_tracked_close(struct fs_handle
*_handle
)
884 const char *path
= NULL
;
885 struct fs_handle_tracked
*handle
=
886 container_of(_handle
, struct fs_handle_tracked
, parent
);
887 struct lttng_directory_handle
*inode_directory_handle
= NULL
;
894 pthread_mutex_lock(&handle
->tracker
->lock
);
895 pthread_mutex_lock(&handle
->lock
);
897 lttng_inode_borrow_location(handle
->inode
, NULL
, &path
);
899 * Here a reference to the inode's directory handle is acquired
900 * to prevent the last reference to it from being released while
901 * the tracker's lock is taken.
903 * If this wasn't done, the directory handle could attempt to
904 * close its underlying directory file descriptor, which would
905 * attempt to lock the tracker's lock, resulting in a deadlock.
907 * Since a new reference to the directory handle is taken within
908 * the scope of this function, it is not possible for the last
909 * reference to the inode's location directory handle to be
910 * released during the call to lttng_inode_put().
912 * We wait until the tracker's lock is released to release the
913 * reference. Hence, the call to the tracker is delayed just
914 * enough to not attempt to recursively acquire the tracker's
917 inode_directory_handle
=
918 lttng_inode_get_location_directory_handle(
921 fd_tracker_untrack(handle
->tracker
, handle
);
922 if (handle
->fd
>= 0) {
924 * The return value of close() is not propagated as there
925 * isn't much the user can do about it.
927 if (close(handle
->fd
)) {
928 PERROR("Failed to close the file descritptor (%d) of fs handle to %s, close() returned",
929 handle
->fd
, path
? path
: "Unknown");
934 lttng_inode_put(handle
->inode
);
936 pthread_mutex_unlock(&handle
->lock
);
937 pthread_mutex_destroy(&handle
->lock
);
938 pthread_mutex_unlock(&handle
->tracker
->lock
);
940 lttng_directory_handle_put(inode_directory_handle
);