2 * Copyright (C) 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <urcu/list.h>
20 #include <urcu/rculfhash.h>
27 #include <sys/types.h>
29 #include "common/defaults.h"
30 #include "common/error.h"
31 #include "common/hashtable/hashtable.h"
32 #include "common/hashtable/utils.h"
33 #include "common/macros.h"
35 #include "fd-tracker.h"
38 /* Tracker lock must be taken by the user. */
39 #define TRACKED_COUNT(tracker) \
40 (tracker->count.suspendable.active + \
41 tracker->count.suspendable.suspended + \
42 tracker->count.unsuspendable)
44 /* Tracker lock must be taken by the user. */
45 #define ACTIVE_COUNT(tracker) \
46 (tracker->count.suspendable.active + tracker->count.unsuspendable)
48 /* Tracker lock must be taken by the user. */
49 #define SUSPENDED_COUNT(tracker) (tracker->count.suspendable.suspended)
51 /* Tracker lock must be taken by the user. */
52 #define SUSPENDABLE_COUNT(tracker) \
53 (tracker->count.suspendable.active + \
54 tracker->count.suspendable.suspended)
56 /* Tracker lock must be taken by the user. */
57 #define UNSUSPENDABLE_COUNT(tracker) (tracker->count.unsuspendable)
64 unsigned int suspended
;
66 unsigned int unsuspendable
;
68 unsigned int capacity
;
72 /* Failures to suspend or restore fs handles. */
76 * The head of the active_handles list is always the least recently
77 * used active handle. When an handle is used, it is removed from the
78 * list and added to the end. When a file has to be suspended, the
79 * first element in the list is "popped", suspended, and added to the
80 * list of suspended handles.
82 struct cds_list_head active_handles
;
83 struct cds_list_head suspended_handles
;
84 struct cds_lfht
*unsuspendable_fds
;
85 struct lttng_inode_registry
*inode_registry
;
88 struct open_properties
{
97 * A fs_handle is not ref-counted. Therefore, it is assumed that a
98 * handle is never in-use while it is being reclaimed. It can be
99 * shared by multiple threads, but external synchronization is required
100 * to ensure it is not still being used when it is reclaimed (close method).
101 * In this respect, it is not different from a regular file descriptor.
103 * The fs_handle lock always nests _within_ the tracker's lock.
106 pthread_mutex_t lock
;
108 * Weak reference to the tracker. All fs_handles are assumed to have
109 * been closed at the moment of the destruction of the fd_tracker.
111 struct fd_tracker
*tracker
;
112 struct open_properties properties
;
113 struct lttng_inode
*inode
;
115 /* inode number of the file at the time of the handle's creation. */
118 /* Offset to which the file should be restored. */
120 struct cds_list_head handles_list_node
;
123 struct unsuspendable_fd
{
125 * Accesses are only performed through the tracker, which is protected
130 struct cds_lfht_node tracker_node
;
131 struct rcu_head rcu_head
;
135 pthread_mutex_t lock
;
139 .lock
= PTHREAD_MUTEX_INITIALIZER
,
142 static int match_fd(struct cds_lfht_node
*node
, const void *key
);
143 static void unsuspendable_fd_destroy(struct unsuspendable_fd
*entry
);
144 static struct unsuspendable_fd
*unsuspendable_fd_create(
145 const char *name
, int fd
);
146 static int open_from_properties(
147 const char *path
, struct open_properties
*properties
);
149 static void fs_handle_log(struct fs_handle
*handle
);
150 static int fs_handle_suspend(struct fs_handle
*handle
);
151 static int fs_handle_restore(struct fs_handle
*handle
);
153 static void fd_tracker_track(
154 struct fd_tracker
*tracker
, struct fs_handle
*handle
);
155 static void fd_tracker_untrack(
156 struct fd_tracker
*tracker
, struct fs_handle
*handle
);
157 static int fd_tracker_suspend_handles(
158 struct fd_tracker
*tracker
, unsigned int count
);
159 static int fd_tracker_restore_handle(
160 struct fd_tracker
*tracker
, struct fs_handle
*handle
);
162 /* Match function of the tracker's unsuspendable_fds hash table. */
163 static int match_fd(struct cds_lfht_node
*node
, const void *key
)
165 struct unsuspendable_fd
*entry
= caa_container_of(
166 node
, struct unsuspendable_fd
, tracker_node
);
168 return hash_match_key_ulong(
169 (void *) (unsigned long) entry
->fd
, (void *) key
);
172 static void delete_unsuspendable_fd(struct rcu_head
*head
)
174 struct unsuspendable_fd
*fd
= caa_container_of(
175 head
, struct unsuspendable_fd
, rcu_head
);
181 static void unsuspendable_fd_destroy(struct unsuspendable_fd
*entry
)
186 call_rcu(&entry
->rcu_head
, delete_unsuspendable_fd
);
189 static struct unsuspendable_fd
*unsuspendable_fd_create(
190 const char *name
, int fd
)
192 struct unsuspendable_fd
*entry
= zmalloc(sizeof(*entry
));
198 entry
->name
= strdup(name
);
203 cds_lfht_node_init(&entry
->tracker_node
);
207 unsuspendable_fd_destroy(entry
);
211 static void fs_handle_log(struct fs_handle
*handle
)
215 pthread_mutex_lock(&handle
->lock
);
216 path
= lttng_inode_get_path(handle
->inode
);
218 if (handle
->fd
>= 0) {
219 DBG_NO_LOC(" %s [active, fd %d%s]", path
, handle
->fd
,
220 handle
->in_use
? ", in use" : "");
222 DBG_NO_LOC(" %s [suspended]", path
);
224 pthread_mutex_unlock(&handle
->lock
);
227 /* Tracker lock must be held by the caller. */
228 static int fs_handle_suspend(struct fs_handle
*handle
)
234 pthread_mutex_lock(&handle
->lock
);
235 path
= lttng_inode_get_path(handle
->inode
);
236 assert(handle
->fd
>= 0);
237 if (handle
->in_use
) {
238 /* This handle can't be suspended as it is currently in use. */
243 ret
= stat(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_restore(struct fs_handle
*handle
)
289 const char *path
= lttng_inode_get_path(handle
->inode
);
291 assert(handle
->fd
== -1);
293 ret
= open_from_properties(path
, &handle
->properties
);
295 PERROR("Failed to restore filesystem handle to %s, open() failed",
302 ret
= lseek(fd
, handle
->offset
, SEEK_SET
);
304 PERROR("Failed to restore filesystem handle to %s, lseek() failed",
309 DBG("Restored filesystem handle to %s (fd %i) at position %" PRId64
,
310 path
, fd
, handle
->offset
);
321 static int open_from_properties(
322 const char *path
, struct open_properties
*properties
)
327 * open() ignores the 'flags' parameter unless the O_CREAT or O_TMPFILE
328 * flags are set. O_TMPFILE would not make sense in the context of a
329 * suspendable fs_handle as it would not be restorable (see OPEN(2)),
330 * thus it is ignored here.
332 if ((properties
->flags
& O_CREAT
) && properties
->mode
.is_set
) {
333 ret
= open(path
, properties
->flags
, properties
->mode
.value
);
335 ret
= open(path
, properties
->flags
);
338 * Some flags should not be used beyond the initial open() of a
339 * restorable file system handle. O_CREAT and O_TRUNC must
340 * be cleared since it would be unexpected to re-use them
341 * when the handle is retored:
342 * - O_CREAT should not be needed as the file has been created
343 * on the initial call to open(),
344 * - O_TRUNC would destroy the file's contents by truncating it
347 properties
->flags
&= ~(O_CREAT
| O_TRUNC
);
356 struct fd_tracker
*fd_tracker_create(unsigned int capacity
)
358 struct fd_tracker
*tracker
= zmalloc(sizeof(struct fd_tracker
));
364 pthread_mutex_lock(&seed
.lock
);
365 if (!seed
.initialized
) {
366 seed
.value
= (unsigned long) time(NULL
);
367 seed
.initialized
= true;
369 pthread_mutex_unlock(&seed
.lock
);
371 CDS_INIT_LIST_HEAD(&tracker
->active_handles
);
372 CDS_INIT_LIST_HEAD(&tracker
->suspended_handles
);
373 tracker
->capacity
= capacity
;
374 tracker
->unsuspendable_fds
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
375 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
376 if (!tracker
->unsuspendable_fds
) {
377 ERR("Failed to create fd-tracker's unsuspendable_fds hash table");
380 tracker
->inode_registry
= lttng_inode_registry_create();
381 if (!tracker
->inode_registry
) {
382 ERR("Failed to create fd-tracker's inode registry");
385 DBG("File descriptor tracker created with a limit of %u simultaneously-opened FDs",
390 fd_tracker_destroy(tracker
);
394 void fd_tracker_log(struct fd_tracker
*tracker
)
396 struct fs_handle
*handle
;
397 struct unsuspendable_fd
*unsuspendable_fd
;
398 struct cds_lfht_iter iter
;
400 pthread_mutex_lock(&tracker
->lock
);
401 DBG_NO_LOC("File descriptor tracker");
402 DBG_NO_LOC(" Stats:");
403 DBG_NO_LOC(" uses: %" PRIu64
, tracker
->stats
.uses
);
404 DBG_NO_LOC(" misses: %" PRIu64
, tracker
->stats
.misses
);
405 DBG_NO_LOC(" errors: %" PRIu64
, tracker
->stats
.errors
);
406 DBG_NO_LOC(" Tracked: %u", TRACKED_COUNT(tracker
));
407 DBG_NO_LOC(" active: %u", ACTIVE_COUNT(tracker
));
408 DBG_NO_LOC(" suspendable: %u", SUSPENDABLE_COUNT(tracker
));
409 DBG_NO_LOC(" unsuspendable: %u", UNSUSPENDABLE_COUNT(tracker
));
410 DBG_NO_LOC(" suspended: %u", SUSPENDED_COUNT(tracker
));
411 DBG_NO_LOC(" capacity: %u", tracker
->capacity
);
413 DBG_NO_LOC(" Tracked suspendable file descriptors");
414 cds_list_for_each_entry (
415 handle
, &tracker
->active_handles
, handles_list_node
) {
416 fs_handle_log(handle
);
418 cds_list_for_each_entry (handle
, &tracker
->suspended_handles
,
420 fs_handle_log(handle
);
422 if (!SUSPENDABLE_COUNT(tracker
)) {
426 DBG_NO_LOC(" Tracked unsuspendable file descriptors");
428 cds_lfht_for_each_entry (tracker
->unsuspendable_fds
, &iter
,
429 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
)
447 * Refuse to destroy the tracker as fs_handles may still old
448 * weak references to the tracker.
450 pthread_mutex_lock(&tracker
->lock
);
451 if (TRACKED_COUNT(tracker
)) {
452 ERR("A file descriptor leak has been detected: %u tracked file descriptors are still being tracked",
453 TRACKED_COUNT(tracker
));
454 pthread_mutex_unlock(&tracker
->lock
);
455 fd_tracker_log(tracker
);
459 pthread_mutex_unlock(&tracker
->lock
);
461 if (tracker
->unsuspendable_fds
) {
462 ret
= cds_lfht_destroy(tracker
->unsuspendable_fds
, NULL
);
466 lttng_inode_registry_destroy(tracker
->inode_registry
);
467 pthread_mutex_destroy(&tracker
->lock
);
473 struct fs_handle
*fd_tracker_open_fs_handle(struct fd_tracker
*tracker
,
479 struct fs_handle
*handle
= NULL
;
481 struct open_properties properties
= {
483 .mode
.is_set
= !!mode
,
484 .mode
.value
= mode
? *mode
: 0,
487 pthread_mutex_lock(&tracker
->lock
);
488 if (ACTIVE_COUNT(tracker
) == tracker
->capacity
) {
489 if (tracker
->count
.suspendable
.active
> 0) {
490 ret
= fd_tracker_suspend_handles(tracker
, 1);
496 * There are not enough active suspendable file
497 * descriptors to open a new fd and still accommodate
498 * the tracker's capacity.
500 WARN("Cannot open file system handle, too many unsuspendable file descriptors are opened (%u)",
501 tracker
->count
.unsuspendable
);
506 handle
= zmalloc(sizeof(*handle
));
510 handle
->tracker
= tracker
;
512 ret
= pthread_mutex_init(&handle
->lock
, NULL
);
514 PERROR("Failed to initialize handle mutex while creating fs handle");
515 goto error_mutex_init
;
518 handle
->fd
= open_from_properties(path
, &properties
);
519 if (handle
->fd
< 0) {
520 PERROR("Failed to open fs handle to %s, open() returned", path
);
524 handle
->properties
= properties
;
526 handle
->inode
= lttng_inode_registry_get_inode(
527 tracker
->inode_registry
, handle
->fd
, path
);
528 if (!handle
->inode
) {
529 ERR("Failed to get lttng_inode corresponding to file %s", path
);
533 if (fstat(handle
->fd
, &fd_stat
)) {
534 PERROR("Failed to retrieve file descriptor inode while creating fs handle, fstat() returned");
537 handle
->ino
= fd_stat
.st_ino
;
539 fd_tracker_track(tracker
, handle
);
541 pthread_mutex_unlock(&tracker
->lock
);
545 lttng_inode_put(handle
->inode
);
547 pthread_mutex_destroy(&handle
->lock
);
554 /* Caller must hold the tracker's lock. */
555 static int fd_tracker_suspend_handles(
556 struct fd_tracker
*tracker
, unsigned int count
)
558 unsigned int left_to_close
= count
;
559 struct fs_handle
*handle
, *tmp
;
561 cds_list_for_each_entry_safe (handle
, tmp
, &tracker
->active_handles
,
565 fd_tracker_untrack(tracker
, handle
);
566 ret
= fs_handle_suspend(handle
);
567 fd_tracker_track(tracker
, handle
);
572 if (!left_to_close
) {
576 return left_to_close
? -EMFILE
: 0;
579 int fd_tracker_open_unsuspendable_fd(struct fd_tracker
*tracker
,
582 unsigned int fd_count
,
586 int ret
, user_ret
, i
, fds_to_suspend
;
587 unsigned int active_fds
;
588 struct unsuspendable_fd
*entries
[fd_count
];
590 memset(entries
, 0, sizeof(entries
));
592 pthread_mutex_lock(&tracker
->lock
);
594 active_fds
= ACTIVE_COUNT(tracker
);
595 fds_to_suspend
= (int) active_fds
+ (int) fd_count
-
596 (int) tracker
->capacity
;
597 if (fds_to_suspend
> 0) {
598 if (fds_to_suspend
<= tracker
->count
.suspendable
.active
) {
599 ret
= fd_tracker_suspend_handles(
600 tracker
, fds_to_suspend
);
606 * There are not enough active suspendable file
607 * descriptors to open a new fd and still accomodate the
608 * tracker's capacity.
610 WARN("Cannot open unsuspendable fd, too many unsuspendable file descriptors are opened (%u)",
611 tracker
->count
.unsuspendable
);
617 user_ret
= open(user_data
, out_fds
);
624 * Add the fds returned by the user's callback to the hashtable
625 * of unsuspendable fds.
627 for (i
= 0; i
< fd_count
; i
++) {
628 struct unsuspendable_fd
*entry
= unsuspendable_fd_create(
629 names
? names
[i
] : NULL
, out_fds
[i
]);
633 goto end_free_entries
;
639 for (i
= 0; i
< fd_count
; i
++) {
640 struct cds_lfht_node
*node
;
641 struct unsuspendable_fd
*entry
= entries
[i
];
643 node
= cds_lfht_add_unique(tracker
->unsuspendable_fds
,
644 hash_key_ulong((void *) (unsigned long)
647 match_fd
, (void *) (unsigned long) out_fds
[i
],
648 &entry
->tracker_node
);
650 if (node
!= &entry
->tracker_node
) {
653 goto end_free_entries
;
657 tracker
->count
.unsuspendable
+= fd_count
;
661 pthread_mutex_unlock(&tracker
->lock
);
664 for (i
= 0; i
< fd_count
; i
++) {
665 unsuspendable_fd_destroy(entries
[i
]);
670 int fd_tracker_close_unsuspendable_fd(struct fd_tracker
*tracker
,
672 unsigned int fd_count
,
676 int i
, ret
, user_ret
;
680 * Maintain a local copy of fds_in as the user's callback may modify its
681 * contents (e.g. setting the fd(s) to -1 after close).
683 memcpy(fds
, fds_in
, sizeof(*fds
) * fd_count
);
685 pthread_mutex_lock(&tracker
->lock
);
688 /* Let the user close the file descriptors. */
689 user_ret
= close(user_data
, fds_in
);
695 /* Untrack the fds that were just closed by the user's callback. */
696 for (i
= 0; i
< fd_count
; i
++) {
697 struct cds_lfht_node
*node
;
698 struct cds_lfht_iter iter
;
699 struct unsuspendable_fd
*entry
;
701 cds_lfht_lookup(tracker
->unsuspendable_fds
,
702 hash_key_ulong((void *) (unsigned long) fds
[i
],
704 match_fd
, (void *) (unsigned long) fds
[i
],
706 node
= cds_lfht_iter_get_node(&iter
);
708 /* Unknown file descriptor. */
709 WARN("Untracked file descriptor %d passed to fd_tracker_close_unsuspendable_fd()",
714 entry
= caa_container_of(
715 node
, struct unsuspendable_fd
, tracker_node
);
717 cds_lfht_del(tracker
->unsuspendable_fds
, node
);
718 unsuspendable_fd_destroy(entry
);
722 tracker
->count
.unsuspendable
-= fd_count
;
726 pthread_mutex_unlock(&tracker
->lock
);
730 /* Caller must have taken the tracker's and handle's locks. */
731 static void fd_tracker_track(
732 struct fd_tracker
*tracker
, struct fs_handle
*handle
)
734 if (handle
->fd
>= 0) {
735 tracker
->count
.suspendable
.active
++;
736 cds_list_add_tail(&handle
->handles_list_node
,
737 &tracker
->active_handles
);
739 tracker
->count
.suspendable
.suspended
++;
740 cds_list_add_tail(&handle
->handles_list_node
,
741 &tracker
->suspended_handles
);
745 /* Caller must have taken the tracker's and handle's locks. */
746 static void fd_tracker_untrack(
747 struct fd_tracker
*tracker
, struct fs_handle
*handle
)
749 if (handle
->fd
>= 0) {
750 tracker
->count
.suspendable
.active
--;
752 tracker
->count
.suspendable
.suspended
--;
754 cds_list_del(&handle
->handles_list_node
);
757 /* Caller must have taken the tracker's and handle's locks. */
758 static int fd_tracker_restore_handle(
759 struct fd_tracker
*tracker
, struct fs_handle
*handle
)
763 fd_tracker_untrack(tracker
, handle
);
764 if (ACTIVE_COUNT(tracker
) >= tracker
->capacity
) {
765 ret
= fd_tracker_suspend_handles(tracker
, 1);
770 ret
= fs_handle_restore(handle
);
772 fd_tracker_track(tracker
, handle
);
773 return ret
? ret
: handle
->fd
;
776 int fs_handle_get_fd(struct fs_handle
*handle
)
781 * TODO This should be optimized as it is a fairly hot path.
782 * The fd-tracker's lock should only be taken when a fs_handle is
783 * restored (slow path). On the fast path (fs_handle is active),
784 * the only effect on the fd_tracker is marking the handle as the
785 * most recently used. Currently, it is done by a call to the
786 * track/untrack helpers, but it should be done atomically.
788 * Note that the lock's nesting order must still be respected here.
789 * The handle's lock nests inside the tracker's lock.
791 pthread_mutex_lock(&handle
->tracker
->lock
);
792 pthread_mutex_lock(&handle
->lock
);
793 assert(!handle
->in_use
);
795 handle
->tracker
->stats
.uses
++;
796 if (handle
->fd
>= 0) {
798 /* Mark as most recently used. */
799 fd_tracker_untrack(handle
->tracker
, handle
);
800 fd_tracker_track(handle
->tracker
, handle
);
802 handle
->tracker
->stats
.misses
++;
803 ret
= fd_tracker_restore_handle(handle
->tracker
, handle
);
805 handle
->tracker
->stats
.errors
++;
809 handle
->in_use
= true;
811 pthread_mutex_unlock(&handle
->lock
);
812 pthread_mutex_unlock(&handle
->tracker
->lock
);
816 void fs_handle_put_fd(struct fs_handle
*handle
)
818 pthread_mutex_lock(&handle
->lock
);
819 handle
->in_use
= false;
820 pthread_mutex_unlock(&handle
->lock
);
823 int fs_handle_unlink(struct fs_handle
*handle
)
827 pthread_mutex_lock(&handle
->tracker
->lock
);
828 pthread_mutex_lock(&handle
->lock
);
829 ret
= lttng_inode_defer_unlink(handle
->inode
);
830 pthread_mutex_unlock(&handle
->lock
);
831 pthread_mutex_unlock(&handle
->tracker
->lock
);
835 int fs_handle_close(struct fs_handle
*handle
)
838 const char *path
= NULL
;
845 pthread_mutex_lock(&handle
->tracker
->lock
);
846 pthread_mutex_lock(&handle
->lock
);
848 path
= lttng_inode_get_path(handle
->inode
);
850 fd_tracker_untrack(handle
->tracker
, handle
);
851 if (handle
->fd
>= 0) {
853 * The return value of close() is not propagated as there
854 * isn't much the user can do about it.
856 if (close(handle
->fd
)) {
857 PERROR("Failed to close the file descritptor (%d) of fs handle to %s, close() returned",
858 handle
->fd
, path
? path
: "Unknown");
863 lttng_inode_put(handle
->inode
);
865 pthread_mutex_unlock(&handle
->lock
);
866 pthread_mutex_destroy(&handle
->lock
);
867 pthread_mutex_unlock(&handle
->tracker
->lock
);