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>
23 #include <sys/types.h>
29 #include "common/macros.h"
30 #include "common/error.h"
31 #include "common/defaults.h"
32 #include "common/hashtable/utils.h"
33 #include "common/hashtable/hashtable.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 + \
47 tracker->count.unsuspendable)
49 /* Tracker lock must be taken by the user. */
50 #define SUSPENDED_COUNT(tracker) \
51 (tracker->count.suspendable.suspended)
53 /* Tracker lock must be taken by the user. */
54 #define SUSPENDABLE_COUNT(tracker) \
55 (tracker->count.suspendable.active + \
56 tracker->count.suspendable.suspended)
58 /* Tracker lock must be taken by the user. */
59 #define UNSUSPENDABLE_COUNT(tracker) \
60 (tracker->count.unsuspendable)
67 unsigned int suspended
;
69 unsigned int unsuspendable
;
71 unsigned int capacity
;
75 /* Failures to suspend or restore fs handles. */
79 * The head of the active_handles list is always the least recently
80 * used active handle. When an handle is used, it is removed from the
81 * list and added to the end. When a file has to be suspended, the
82 * first element in the list is "popped", suspended, and added to the
83 * list of suspended handles.
85 struct cds_list_head active_handles
;
86 struct cds_list_head suspended_handles
;
87 struct cds_lfht
*unsuspendable_fds
;
88 struct lttng_inode_registry
*inode_registry
;
91 struct open_properties
{
100 * A fs_handle is not ref-counted. Therefore, it is assumed that a
101 * handle is never in-use while it is being reclaimed. It can be
102 * shared by multiple threads, but external synchronization is required
103 * to ensure it is not still being used when it is reclaimed (close method).
104 * In this respect, it is not different from a regular file descriptor.
106 * The fs_handle lock always nests _within_ the tracker's lock.
109 pthread_mutex_t lock
;
111 * Weak reference to the tracker. All fs_handles are assumed to have
112 * been closed at the moment of the destruction of the fd_tracker.
114 struct fd_tracker
*tracker
;
115 struct open_properties properties
;
116 struct lttng_inode
*inode
;
118 /* inode number of the file at the time of the handle's creation. */
121 /* Offset to which the file should be restored. */
123 struct cds_list_head handles_list_node
;
126 struct unsuspendable_fd
{
128 * Accesses are only performed through the tracker, which is protected
133 struct cds_lfht_node tracker_node
;
134 struct rcu_head rcu_head
;
138 pthread_mutex_t lock
;
142 .lock
= PTHREAD_MUTEX_INITIALIZER
,
145 static int match_fd(struct cds_lfht_node
*node
, const void *key
);
146 static void unsuspendable_fd_destroy(struct unsuspendable_fd
*entry
);
147 static struct unsuspendable_fd
*unsuspendable_fd_create(const char *name
,
149 static int open_from_properties(const char *path
,
150 struct open_properties
*properties
);
152 static void fs_handle_log(struct fs_handle
*handle
);
153 static int fs_handle_suspend(struct fs_handle
*handle
);
154 static int fs_handle_restore(struct fs_handle
*handle
);
156 static void fd_tracker_track(struct fd_tracker
*tracker
,
157 struct fs_handle
*handle
);
158 static void fd_tracker_untrack(struct fd_tracker
*tracker
,
159 struct fs_handle
*handle
);
160 static int fd_tracker_suspend_handles(struct fd_tracker
*tracker
,
162 static int fd_tracker_restore_handle(struct fd_tracker
*tracker
,
163 struct fs_handle
*handle
);
165 /* Match function of the tracker's unsuspendable_fds hash table. */
167 int match_fd(struct cds_lfht_node
*node
, const void *key
)
169 struct unsuspendable_fd
*entry
=
170 caa_container_of(node
, struct unsuspendable_fd
, tracker_node
);
172 return hash_match_key_ulong((void *) (unsigned long) entry
->fd
,
177 void delete_unsuspendable_fd(struct rcu_head
*head
)
179 struct unsuspendable_fd
*fd
= caa_container_of(head
,
180 struct unsuspendable_fd
, rcu_head
);
187 void unsuspendable_fd_destroy(struct unsuspendable_fd
*entry
)
192 call_rcu(&entry
->rcu_head
, delete_unsuspendable_fd
);
196 struct unsuspendable_fd
*unsuspendable_fd_create(const char *name
, int fd
)
198 struct unsuspendable_fd
*entry
=
199 zmalloc(sizeof(*entry
));
205 entry
->name
= strdup(name
);
210 cds_lfht_node_init(&entry
->tracker_node
);
214 unsuspendable_fd_destroy(entry
);
219 void fs_handle_log(struct fs_handle
*handle
)
223 pthread_mutex_lock(&handle
->lock
);
224 path
= lttng_inode_get_path(handle
->inode
);
226 if (handle
->fd
>= 0) {
227 DBG_NO_LOC(" %s [active, fd %d%s]",
230 handle
->in_use
? ", in use" : "");
232 DBG_NO_LOC(" %s [suspended]", path
);
234 pthread_mutex_unlock(&handle
->lock
);
237 /* Tracker lock must be held by the caller. */
239 int fs_handle_suspend(struct fs_handle
*handle
)
245 pthread_mutex_lock(&handle
->lock
);
246 path
= lttng_inode_get_path(handle
->inode
);
247 assert(handle
->fd
>= 0);
248 if (handle
->in_use
) {
249 /* This handle can't be suspended as it is currently in use. */
254 ret
= stat(path
, &fs_stat
);
256 PERROR("Filesystem handle to %s cannot be suspended as stat() failed",
262 if (fs_stat
.st_ino
!= handle
->ino
) {
263 /* Don't suspend as the handle would not be restorable. */
264 WARN("Filesystem handle to %s cannot be suspended as its inode changed",
270 handle
->offset
= lseek(handle
->fd
, 0, SEEK_CUR
);
271 if (handle
->offset
== -1) {
272 WARN("Filesystem handle to %s cannot be suspended as lseek() failed to sample its current position",
278 ret
= close(handle
->fd
);
280 PERROR("Filesystem handle to %s cannot be suspended as close() failed",
285 DBG("Suspended filesystem handle to %s (fd %i) at position %" PRId64
,
286 path
, handle
->fd
, handle
->offset
);
290 handle
->tracker
->stats
.errors
++;
292 pthread_mutex_unlock(&handle
->lock
);
296 /* Caller must hold the tracker and handle's locks. */
298 int fs_handle_restore(struct fs_handle
*handle
)
301 const char *path
= lttng_inode_get_path(handle
->inode
);
303 assert(handle
->fd
== -1);
305 ret
= open_from_properties(path
,
306 &handle
->properties
);
308 PERROR("Failed to restore filesystem handle to %s, open() failed",
315 ret
= lseek(fd
, handle
->offset
, SEEK_SET
);
317 PERROR("Failed to restore filesystem handle to %s, lseek() failed",
322 DBG("Restored filesystem handle to %s (fd %i) at position %" PRId64
,
323 path
, fd
, handle
->offset
);
335 int open_from_properties(const char *path
, struct open_properties
*properties
)
340 * open() ignores the 'flags' parameter unless the O_CREAT or O_TMPFILE
341 * flags are set. O_TMPFILE would not make sense in the context of a
342 * suspendable fs_handle as it would not be restorable (see OPEN(2)),
343 * thus it is ignored here.
345 if ((properties
->flags
& O_CREAT
) && properties
->mode
.is_set
) {
346 ret
= open(path
, properties
->flags
,
347 properties
->mode
.value
);
349 ret
= open(path
, properties
->flags
);
352 * Some flags should not be used beyond the initial open() of a
353 * restorable file system handle. O_CREAT and O_TRUNC must
354 * be cleared since it would be unexpected to re-use them
355 * when the handle is retored:
356 * - O_CREAT should not be needed as the file has been created
357 * on the initial call to open(),
358 * - O_TRUNC would destroy the file's contents by truncating it
361 properties
->flags
&= ~(O_CREAT
| O_TRUNC
);
370 struct fd_tracker
*fd_tracker_create(unsigned int capacity
)
372 struct fd_tracker
*tracker
= zmalloc(sizeof(struct fd_tracker
));
378 pthread_mutex_lock(&seed
.lock
);
379 if (!seed
.initialized
) {
380 seed
.value
= (unsigned long) time(NULL
);
381 seed
.initialized
= true;
383 pthread_mutex_unlock(&seed
.lock
);
385 CDS_INIT_LIST_HEAD(&tracker
->active_handles
);
386 CDS_INIT_LIST_HEAD(&tracker
->suspended_handles
);
387 tracker
->capacity
= capacity
;
388 tracker
->unsuspendable_fds
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
389 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
390 if (!tracker
->unsuspendable_fds
) {
391 ERR("Failed to create fd-tracker's unsuspendable_fds hash table");
394 tracker
->inode_registry
= lttng_inode_registry_create();
395 if (!tracker
->inode_registry
) {
396 ERR("Failed to create fd-tracker's inode registry");
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
*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(handle
, &tracker
->active_handles
,
430 fs_handle_log(handle
);
432 cds_list_for_each_entry(handle
, &tracker
->suspended_handles
,
434 fs_handle_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]", unsuspendable_fd
->name
? : "Unnamed",
445 unsuspendable_fd
->fd
);
448 if (!UNSUSPENDABLE_COUNT(tracker
)) {
452 pthread_mutex_unlock(&tracker
->lock
);
455 int fd_tracker_destroy(struct fd_tracker
*tracker
)
460 * Refuse to destroy the tracker as fs_handles may still old
461 * weak references to the tracker.
463 pthread_mutex_lock(&tracker
->lock
);
464 if (TRACKED_COUNT(tracker
)) {
465 ERR("A file descriptor leak has been detected: %u tracked file descriptors are still being tracked",
466 TRACKED_COUNT(tracker
));
467 pthread_mutex_unlock(&tracker
->lock
);
468 fd_tracker_log(tracker
);
472 pthread_mutex_unlock(&tracker
->lock
);
474 if (tracker
->unsuspendable_fds
) {
475 ret
= cds_lfht_destroy(tracker
->unsuspendable_fds
, NULL
);
479 lttng_inode_registry_destroy(tracker
->inode_registry
);
480 pthread_mutex_destroy(&tracker
->lock
);
486 struct fs_handle
*fd_tracker_open_fs_handle(struct fd_tracker
*tracker
,
487 const char *path
, int flags
, mode_t
*mode
)
490 struct fs_handle
*handle
= NULL
;
492 struct open_properties properties
= {
494 .mode
.is_set
= !!mode
,
495 .mode
.value
= 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(sizeof(*handle
));
521 handle
->tracker
= tracker
;
523 ret
= pthread_mutex_init(&handle
->lock
, NULL
);
525 PERROR("Failed to initialize handle mutex while creating fs handle");
526 goto error_mutex_init
;
529 handle
->fd
= open_from_properties(path
, &properties
);
530 if (handle
->fd
< 0) {
531 PERROR("Failed to open fs handle to %s, open() returned", path
);
535 handle
->properties
= properties
;
537 handle
->inode
= lttng_inode_registry_get_inode(tracker
->inode_registry
,
539 if (!handle
->inode
) {
540 ERR("Failed to get lttng_inode corresponding to file %s",
545 if (fstat(handle
->fd
, &fd_stat
)) {
546 PERROR("Failed to retrieve file descriptor inode while creating fs handle, fstat() returned");
549 handle
->ino
= fd_stat
.st_ino
;
551 fd_tracker_track(tracker
, handle
);
553 pthread_mutex_unlock(&tracker
->lock
);
557 lttng_inode_put(handle
->inode
);
559 pthread_mutex_destroy(&handle
->lock
);
566 /* Caller must hold the tracker's lock. */
568 int fd_tracker_suspend_handles(struct fd_tracker
*tracker
,
571 unsigned int left_to_close
= count
;
572 struct fs_handle
*handle
, *tmp
;
574 cds_list_for_each_entry_safe(handle
, tmp
, &tracker
->active_handles
,
578 fd_tracker_untrack(tracker
, handle
);
579 ret
= fs_handle_suspend(handle
);
580 fd_tracker_track(tracker
, handle
);
585 if (!left_to_close
) {
589 return left_to_close
? -EMFILE
: 0;
592 int fd_tracker_open_unsuspendable_fd(struct fd_tracker
*tracker
,
593 int *out_fds
, const char **names
, unsigned int fd_count
,
594 fd_open_cb open
, void *user_data
)
596 int ret
, user_ret
, i
, fds_to_suspend
;
597 unsigned int active_fds
;
598 struct unsuspendable_fd
*entries
[fd_count
];
600 memset(entries
, 0, sizeof(entries
));
602 pthread_mutex_lock(&tracker
->lock
);
604 active_fds
= ACTIVE_COUNT(tracker
);
605 fds_to_suspend
= (int) active_fds
+ (int) fd_count
- (int) tracker
->capacity
;
606 if (fds_to_suspend
> 0) {
607 if (fds_to_suspend
<= tracker
->count
.suspendable
.active
) {
608 ret
= fd_tracker_suspend_handles(tracker
, fds_to_suspend
);
614 * There are not enough active suspendable file
615 * descriptors to open a new fd and still accomodate the
616 * tracker's capacity.
618 WARN("Cannot open unsuspendable fd, too many unsuspendable file descriptors are opened (%u)",
619 tracker
->count
.unsuspendable
);
625 user_ret
= open(user_data
, out_fds
);
632 * Add the fds returned by the user's callback to the hashtable
633 * of unsuspendable fds.
635 for (i
= 0; i
< fd_count
; i
++) {
636 struct unsuspendable_fd
*entry
=
637 unsuspendable_fd_create(names
? names
[i
] : NULL
,
642 goto end_free_entries
;
648 for (i
= 0; i
< fd_count
; i
++) {
649 struct cds_lfht_node
*node
;
650 struct unsuspendable_fd
*entry
= entries
[i
];
652 node
= cds_lfht_add_unique(
653 tracker
->unsuspendable_fds
,
654 hash_key_ulong((void *) (unsigned long) out_fds
[i
],
657 (void *) (unsigned long) out_fds
[i
],
658 &entry
->tracker_node
);
660 if (node
!= &entry
->tracker_node
) {
663 goto end_free_entries
;
667 tracker
->count
.unsuspendable
+= fd_count
;
671 pthread_mutex_unlock(&tracker
->lock
);
674 for (i
= 0; i
< fd_count
; i
++) {
675 unsuspendable_fd_destroy(entries
[i
]);
680 int fd_tracker_close_unsuspendable_fd(struct fd_tracker
*tracker
,
681 int *fds_in
, unsigned int fd_count
, fd_close_cb close
,
684 int i
, ret
, user_ret
;
688 * Maintain a local copy of fds_in as the user's callback may modify its
689 * contents (e.g. setting the fd(s) to -1 after close).
691 memcpy(fds
, fds_in
, sizeof(*fds
) * fd_count
);
693 pthread_mutex_lock(&tracker
->lock
);
696 /* Let the user close the file descriptors. */
697 user_ret
= close(user_data
, fds_in
);
703 /* Untrack the fds that were just closed by the user's callback. */
704 for (i
= 0; i
< fd_count
; i
++) {
705 struct cds_lfht_node
*node
;
706 struct cds_lfht_iter iter
;
707 struct unsuspendable_fd
*entry
;
709 cds_lfht_lookup(tracker
->unsuspendable_fds
,
710 hash_key_ulong((void *) (unsigned long) fds
[i
],
713 (void *) (unsigned long) fds
[i
],
715 node
= cds_lfht_iter_get_node(&iter
);
717 /* Unknown file descriptor. */
718 WARN("Untracked file descriptor %d passed to fd_tracker_close_unsuspendable_fd()",
723 entry
= caa_container_of(node
,
724 struct unsuspendable_fd
,
727 cds_lfht_del(tracker
->unsuspendable_fds
, node
);
728 unsuspendable_fd_destroy(entry
);
732 tracker
->count
.unsuspendable
-= fd_count
;
736 pthread_mutex_unlock(&tracker
->lock
);
740 /* Caller must have taken the tracker's and handle's locks. */
742 void fd_tracker_track(struct fd_tracker
*tracker
, struct fs_handle
*handle
)
744 if (handle
->fd
>= 0) {
745 tracker
->count
.suspendable
.active
++;
746 cds_list_add_tail(&handle
->handles_list_node
,
747 &tracker
->active_handles
);
749 tracker
->count
.suspendable
.suspended
++;
750 cds_list_add_tail(&handle
->handles_list_node
,
751 &tracker
->suspended_handles
);
755 /* Caller must have taken the tracker's and handle's locks. */
757 void fd_tracker_untrack(struct fd_tracker
*tracker
, struct fs_handle
*handle
)
759 if (handle
->fd
>= 0) {
760 tracker
->count
.suspendable
.active
--;
762 tracker
->count
.suspendable
.suspended
--;
764 cds_list_del(&handle
->handles_list_node
);
767 /* Caller must have taken the tracker's and handle's locks. */
769 int fd_tracker_restore_handle(struct fd_tracker
*tracker
,
770 struct fs_handle
*handle
)
774 fd_tracker_untrack(tracker
, handle
);
775 if (ACTIVE_COUNT(tracker
) >= tracker
->capacity
) {
776 ret
= fd_tracker_suspend_handles(tracker
, 1);
781 ret
= fs_handle_restore(handle
);
783 fd_tracker_track(tracker
, handle
);
784 return ret
? ret
: handle
->fd
;
787 int fs_handle_get_fd(struct fs_handle
*handle
)
792 * TODO This should be optimized as it is a fairly hot path.
793 * The fd-tracker's lock should only be taken when a fs_handle is
794 * restored (slow path). On the fast path (fs_handle is active),
795 * the only effect on the fd_tracker is marking the handle as the
796 * most recently used. Currently, it is done by a call to the
797 * track/untrack helpers, but it should be done atomically.
799 * Note that the lock's nesting order must still be respected here.
800 * The handle's lock nests inside the tracker's lock.
802 pthread_mutex_lock(&handle
->tracker
->lock
);
803 pthread_mutex_lock(&handle
->lock
);
804 assert(!handle
->in_use
);
806 handle
->tracker
->stats
.uses
++;
807 if (handle
->fd
>= 0) {
809 /* Mark as most recently used. */
810 fd_tracker_untrack(handle
->tracker
, handle
);
811 fd_tracker_track(handle
->tracker
, handle
);
813 handle
->tracker
->stats
.misses
++;
814 ret
= fd_tracker_restore_handle(handle
->tracker
, handle
);
816 handle
->tracker
->stats
.errors
++;
820 handle
->in_use
= true;
822 pthread_mutex_unlock(&handle
->lock
);
823 pthread_mutex_unlock(&handle
->tracker
->lock
);
827 void fs_handle_put_fd(struct fs_handle
*handle
)
829 pthread_mutex_lock(&handle
->lock
);
830 handle
->in_use
= false;
831 pthread_mutex_unlock(&handle
->lock
);
834 int fs_handle_unlink(struct fs_handle
*handle
)
838 pthread_mutex_lock(&handle
->tracker
->lock
);
839 pthread_mutex_lock(&handle
->lock
);
840 ret
= lttng_inode_defer_unlink(handle
->inode
);
841 pthread_mutex_unlock(&handle
->lock
);
842 pthread_mutex_unlock(&handle
->tracker
->lock
);
846 int fs_handle_close(struct fs_handle
*handle
)
849 const char *path
= NULL
;
856 pthread_mutex_lock(&handle
->tracker
->lock
);
857 pthread_mutex_lock(&handle
->lock
);
859 path
= lttng_inode_get_path(handle
->inode
);
861 fd_tracker_untrack(handle
->tracker
, handle
);
862 if (handle
->fd
>= 0) {
864 * The return value of close() is not propagated as there
865 * isn't much the user can do about it.
867 if (close(handle
->fd
)) {
868 PERROR("Failed to close the file descritptor (%d) of fs handle to %s, close() returned",
869 handle
->fd
, path
? path
: "Unknown");
873 lttng_inode_put(handle
->inode
);
874 pthread_mutex_unlock(&handle
->lock
);
875 pthread_mutex_destroy(&handle
->lock
);
876 pthread_mutex_unlock(&handle
->tracker
->lock
);