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.
20 #include <urcu/list.h>
21 #include <urcu/rculfhash.h>
24 #include <sys/types.h>
30 #include "common/macros.h"
31 #include "common/error.h"
32 #include "common/defaults.h"
33 #include "common/hashtable/utils.h"
34 #include "common/hashtable/hashtable.h"
36 #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
;
90 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
;
117 /* inode number of the file at the time of the handle's creation. */
120 /* Offset to which the file should be restored. */
122 struct cds_list_head handles_list_node
;
125 struct unsuspendable_fd
{
127 * Accesses are only performed through the tracker, which is protected
132 struct cds_lfht_node tracker_node
;
136 pthread_mutex_t lock
;
140 .lock
= PTHREAD_MUTEX_INITIALIZER
,
143 static int match_fd(struct cds_lfht_node
*node
, const void *key
);
144 static void unsuspendable_fd_destroy(struct unsuspendable_fd
*entry
);
145 static struct unsuspendable_fd
*unsuspendable_fd_create(const char *name
,
147 static int open_from_properties(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(struct fd_tracker
*tracker
,
154 struct fs_handle
*handle
);
155 static void fd_tracker_untrack(struct fd_tracker
*tracker
,
156 struct fs_handle
*handle
);
157 static int fd_tracker_suspend_handles(struct fd_tracker
*tracker
,
159 static int fd_tracker_restore_handle(struct fd_tracker
*tracker
,
160 struct fs_handle
*handle
);
162 /* Match function of the tracker's unsuspendable_fds hash table. */
164 int match_fd(struct cds_lfht_node
*node
, const void *key
)
166 struct unsuspendable_fd
*entry
=
167 caa_container_of(node
, struct unsuspendable_fd
, tracker_node
);
169 return hash_match_key_ulong((void *) (unsigned long) entry
->fd
,
174 void unsuspendable_fd_destroy(struct unsuspendable_fd
*entry
)
184 struct unsuspendable_fd
*unsuspendable_fd_create(const char *name
, int fd
)
186 struct unsuspendable_fd
*entry
=
187 zmalloc(sizeof(*entry
));
193 entry
->name
= strdup(name
);
198 cds_lfht_node_init(&entry
->tracker_node
);
202 unsuspendable_fd_destroy(entry
);
207 void fs_handle_log(struct fs_handle
*handle
)
209 pthread_mutex_lock(&handle
->lock
);
210 if (handle
->fd
>= 0) {
211 DBG_NO_LOC(" %s [active, fd %d%s]",
212 handle
->properties
.path
,
214 handle
->in_use
? ", in use" : "");
216 DBG_NO_LOC(" %s [suspended]", handle
->properties
.path
);
218 pthread_mutex_unlock(&handle
->lock
);
222 int fs_handle_suspend(struct fs_handle
*handle
)
227 pthread_mutex_lock(&handle
->lock
);
228 assert(handle
->fd
>= 0);
229 if (handle
->in_use
) {
230 /* This handle can't be suspended as it is currently in use. */
235 ret
= stat(handle
->properties
.path
, &fs_stat
);
237 PERROR("Filesystem handle to %s cannot be suspended as stat() failed",
238 handle
->properties
.path
);
243 if (fs_stat
.st_ino
!= handle
->ino
) {
244 /* Don't suspend as the handle would not be restorable. */
245 WARN("Filesystem handle to %s cannot be suspended as its inode changed",
246 handle
->properties
.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",
254 handle
->properties
.path
);
259 ret
= close(handle
->fd
);
261 PERROR("Filesystem handle to %s cannot be suspended as close() failed",
262 handle
->properties
.path
);
266 DBG("Suspended filesystem handle to %s (fd %i) at position %" PRId64
,
267 handle
->properties
.path
, handle
->fd
, handle
->offset
);
271 handle
->tracker
->stats
.errors
++;
273 pthread_mutex_unlock(&handle
->lock
);
277 /* Caller must hold the tracker and handle's locks. */
279 int fs_handle_restore(struct fs_handle
*handle
)
283 assert(handle
->fd
== -1);
284 ret
= open_from_properties(&handle
->properties
);
286 PERROR("Failed to restore filesystem handle to %s, open() failed",
287 handle
->properties
.path
);
293 ret
= lseek(fd
, handle
->offset
, SEEK_SET
);
295 PERROR("Failed to restore filesystem handle to %s, lseek() failed",
296 handle
->properties
.path
);
300 DBG("Restored filesystem handle to %s (fd %i) at position %" PRId64
,
301 handle
->properties
.path
, fd
, handle
->offset
);
313 int open_from_properties(struct open_properties
*properties
)
318 * open() ignores the 'flags' parameter unless the O_CREAT or O_TMPFILE
319 * flags are set. O_TMPFILE would not make sense in the context of a
320 * suspendable fs_handle as it would not be restorable (see OPEN(2)),
321 * thus it is ignored here.
323 if ((properties
->flags
& O_CREAT
) && properties
->mode
.is_set
) {
324 ret
= open(properties
->path
, properties
->flags
,
325 properties
->mode
.value
);
327 ret
= open(properties
->path
, properties
->flags
);
330 * Some flags should not be used beyond the initial open() of a
331 * restorable file system handle. O_CREAT and O_TRUNC must
332 * be cleared since it would be unexpected to re-use them
333 * when the handle is retored:
334 * - O_CREAT should not be needed as the file has been created
335 * on the initial call to open(),
336 * - O_TRUNC would destroy the file's contents by truncating it
339 properties
->flags
&= ~(O_CREAT
| O_TRUNC
);
348 struct fd_tracker
*fd_tracker_create(unsigned int capacity
)
350 struct fd_tracker
*tracker
= zmalloc(sizeof(struct fd_tracker
));
356 pthread_mutex_lock(&seed
.lock
);
357 if (!seed
.initialized
) {
358 seed
.value
= (unsigned long) time(NULL
);
359 seed
.initialized
= true;
361 pthread_mutex_unlock(&seed
.lock
);
363 CDS_INIT_LIST_HEAD(&tracker
->active_handles
);
364 CDS_INIT_LIST_HEAD(&tracker
->suspended_handles
);
365 tracker
->capacity
= capacity
;
366 tracker
->unsuspendable_fds
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
367 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
368 DBG("File descriptor tracker created with a limit of %u simultaneously-opened FDs",
374 void fd_tracker_log(struct fd_tracker
*tracker
)
376 struct fs_handle
*handle
;
377 struct unsuspendable_fd
*unsuspendable_fd
;
378 struct cds_lfht_iter iter
;
380 pthread_mutex_lock(&tracker
->lock
);
381 DBG_NO_LOC("File descriptor tracker");
382 DBG_NO_LOC(" Stats:");
383 DBG_NO_LOC(" uses: %" PRIu64
, tracker
->stats
.uses
);
384 DBG_NO_LOC(" misses: %" PRIu64
, tracker
->stats
.misses
);
385 DBG_NO_LOC(" errors: %" PRIu64
, tracker
->stats
.errors
);
386 DBG_NO_LOC(" Tracked: %u", TRACKED_COUNT(tracker
));
387 DBG_NO_LOC(" active: %u", ACTIVE_COUNT(tracker
));
388 DBG_NO_LOC(" suspendable: %u", SUSPENDABLE_COUNT(tracker
));
389 DBG_NO_LOC(" unsuspendable: %u", UNSUSPENDABLE_COUNT(tracker
));
390 DBG_NO_LOC(" suspended: %u", SUSPENDED_COUNT(tracker
));
391 DBG_NO_LOC(" capacity: %u", tracker
->capacity
);
393 DBG_NO_LOC(" Tracked suspendable file descriptors");
394 cds_list_for_each_entry(handle
, &tracker
->active_handles
,
396 fs_handle_log(handle
);
398 cds_list_for_each_entry(handle
, &tracker
->suspended_handles
,
400 fs_handle_log(handle
);
402 if (!SUSPENDABLE_COUNT(tracker
)) {
406 DBG_NO_LOC(" Tracked unsuspendable file descriptors");
408 cds_lfht_for_each_entry(tracker
->unsuspendable_fds
, &iter
,
409 unsuspendable_fd
, tracker_node
) {
410 DBG_NO_LOC(" %s [active, fd %d]", unsuspendable_fd
->name
? : "Unnamed",
411 unsuspendable_fd
->fd
);
414 if (!UNSUSPENDABLE_COUNT(tracker
)) {
418 pthread_mutex_unlock(&tracker
->lock
);
421 int fd_tracker_destroy(struct fd_tracker
*tracker
)
426 * Refuse to destroy the tracker as fs_handles may still old
427 * weak references to the tracker.
429 pthread_mutex_lock(&tracker
->lock
);
430 if (TRACKED_COUNT(tracker
)) {
431 ERR("A file descriptor leak has been detected: %u tracked file descriptors are still being tracked",
432 TRACKED_COUNT(tracker
));
433 pthread_mutex_unlock(&tracker
->lock
);
434 fd_tracker_log(tracker
);
438 pthread_mutex_unlock(&tracker
->lock
);
440 ret
= cds_lfht_destroy(tracker
->unsuspendable_fds
, NULL
);
442 pthread_mutex_destroy(&tracker
->lock
);
448 struct fs_handle
*fd_tracker_open_fs_handle(struct fd_tracker
*tracker
,
449 const char *path
, int flags
, mode_t
*mode
)
452 struct fs_handle
*handle
= NULL
;
454 struct open_properties properties
= {
455 .path
= strdup(path
),
457 .mode
.is_set
= !!mode
,
458 .mode
.value
= mode
? *mode
: 0,
461 if (!properties
.path
) {
465 pthread_mutex_lock(&tracker
->lock
);
466 if (ACTIVE_COUNT(tracker
) == tracker
->capacity
) {
467 if (tracker
->count
.suspendable
.active
> 0) {
468 ret
= fd_tracker_suspend_handles(tracker
, 1);
474 * There are not enough active suspendable file
475 * descriptors to open a new fd and still accomodate the
476 * tracker's capacity.
478 WARN("Cannot open file system handle, too many unsuspendable file descriptors are opened (%u)",
479 tracker
->count
.unsuspendable
);
485 handle
= zmalloc(sizeof(*handle
));
490 ret
= pthread_mutex_init(&handle
->lock
, NULL
);
492 PERROR("Failed to initialize handle mutex while creating fs handle");
497 handle
->fd
= open_from_properties(&properties
);
498 if (handle
->fd
< 0) {
499 PERROR("Failed to open fs handle to %s, open() returned", path
);
505 * Clear the create flag from the open flags as it would make no sense
506 * to use it when restoring a fs handle.
508 properties
.flags
&= ~O_CREAT
;
509 handle
->properties
= properties
;
510 properties
.path
= NULL
;
512 if (fstat(handle
->fd
, &fd_stat
)) {
513 PERROR("Failed to retrieve file descriptor inode while creating fs handle, fstat() returned");
517 handle
->ino
= fd_stat
.st_ino
;
519 fd_tracker_track(tracker
, handle
);
520 handle
->tracker
= tracker
;
521 pthread_mutex_unlock(&tracker
->lock
);
523 free(properties
.path
);
526 pthread_mutex_unlock(&tracker
->lock
);
527 (void) fs_handle_close(handle
);
532 /* Caller must hold the tracker's lock. */
534 int fd_tracker_suspend_handles(struct fd_tracker
*tracker
,
537 unsigned int left_to_close
= count
;
538 struct fs_handle
*handle
, *tmp
;
540 cds_list_for_each_entry_safe(handle
, tmp
, &tracker
->active_handles
,
544 fd_tracker_untrack(tracker
, handle
);
545 ret
= fs_handle_suspend(handle
);
546 fd_tracker_track(tracker
, handle
);
551 if (!left_to_close
) {
555 return left_to_close
? -EMFILE
: 0;
558 int fd_tracker_open_unsuspendable_fd(struct fd_tracker
*tracker
,
559 int *out_fds
, const char **names
, unsigned int fd_count
,
560 fd_open_cb open
, void *user_data
)
562 int ret
, user_ret
, i
, fds_to_suspend
;
563 unsigned int active_fds
;
564 struct unsuspendable_fd
*entries
[fd_count
];
566 memset(entries
, 0, sizeof(entries
));
568 pthread_mutex_lock(&tracker
->lock
);
570 active_fds
= ACTIVE_COUNT(tracker
);
571 fds_to_suspend
= (int) active_fds
+ (int) fd_count
- (int) tracker
->capacity
;
572 if (fds_to_suspend
> 0) {
573 if (fds_to_suspend
<= tracker
->count
.suspendable
.active
) {
574 ret
= fd_tracker_suspend_handles(tracker
, fds_to_suspend
);
580 * There are not enough active suspendable file
581 * descriptors to open a new fd and still accomodate the
582 * tracker's capacity.
584 WARN("Cannot open unsuspendable fd, too many unsuspendable file descriptors are opened (%u)",
585 tracker
->count
.unsuspendable
);
591 user_ret
= open(user_data
, out_fds
);
598 * Add the fds returned by the user's callback to the hashtable
599 * of unsuspendable fds.
601 for (i
= 0; i
< fd_count
; i
++) {
602 struct unsuspendable_fd
*entry
=
603 unsuspendable_fd_create(names
? names
[i
] : NULL
,
608 goto end_free_entries
;
614 for (i
= 0; i
< fd_count
; i
++) {
615 struct cds_lfht_node
*node
;
616 struct unsuspendable_fd
*entry
= entries
[i
];
618 node
= cds_lfht_add_unique(
619 tracker
->unsuspendable_fds
,
620 hash_key_ulong((void *) (unsigned long) out_fds
[i
],
623 (void *) (unsigned long) out_fds
[i
],
624 &entry
->tracker_node
);
626 if (node
!= &entry
->tracker_node
) {
629 goto end_free_entries
;
633 tracker
->count
.unsuspendable
+= fd_count
;
637 pthread_mutex_unlock(&tracker
->lock
);
640 for (i
= 0; i
< fd_count
; i
++) {
641 unsuspendable_fd_destroy(entries
[i
]);
646 int fd_tracker_close_unsuspendable_fd(struct fd_tracker
*tracker
,
647 int *fds_in
, unsigned int fd_count
, fd_close_cb close
,
650 int i
, ret
, user_ret
;
654 * Maintain a local copy of fds_in as the user's callback may modify its
655 * contents (e.g. setting the fd(s) to -1 after close).
657 memcpy(fds
, fds_in
, sizeof(*fds
) * fd_count
);
659 pthread_mutex_lock(&tracker
->lock
);
662 /* Let the user close the file descriptors. */
663 user_ret
= close(user_data
, fds_in
);
669 /* Untrack the fds that were just closed by the user's callback. */
670 for (i
= 0; i
< fd_count
; i
++) {
671 struct cds_lfht_node
*node
;
672 struct cds_lfht_iter iter
;
673 struct unsuspendable_fd
*entry
;
675 cds_lfht_lookup(tracker
->unsuspendable_fds
,
676 hash_key_ulong((void *) (unsigned long) fds
[i
],
679 (void *) (unsigned long) fds
[i
],
681 node
= cds_lfht_iter_get_node(&iter
);
683 /* Unknown file descriptor. */
684 WARN("Untracked file descriptor %d passed to fd_tracker_close_unsuspendable_fd()",
689 entry
= caa_container_of(node
,
690 struct unsuspendable_fd
,
693 cds_lfht_del(tracker
->unsuspendable_fds
, node
);
694 unsuspendable_fd_destroy(entry
);
698 tracker
->count
.unsuspendable
-= fd_count
;
702 pthread_mutex_unlock(&tracker
->lock
);
706 /* Caller must have taken the tracker's and handle's locks. */
708 void fd_tracker_track(struct fd_tracker
*tracker
, struct fs_handle
*handle
)
710 if (handle
->fd
>= 0) {
711 tracker
->count
.suspendable
.active
++;
712 cds_list_add_tail(&handle
->handles_list_node
,
713 &tracker
->active_handles
);
715 tracker
->count
.suspendable
.suspended
++;
716 cds_list_add_tail(&handle
->handles_list_node
,
717 &tracker
->suspended_handles
);
721 /* Caller must have taken the tracker's and handle's locks. */
723 void fd_tracker_untrack(struct fd_tracker
*tracker
, struct fs_handle
*handle
)
725 if (handle
->fd
>= 0) {
726 tracker
->count
.suspendable
.active
--;
728 tracker
->count
.suspendable
.suspended
--;
730 cds_list_del(&handle
->handles_list_node
);
733 /* Caller must have taken the tracker's and handle's locks. */
735 int fd_tracker_restore_handle(struct fd_tracker
*tracker
,
736 struct fs_handle
*handle
)
740 fd_tracker_untrack(tracker
, handle
);
741 if (ACTIVE_COUNT(tracker
) >= tracker
->capacity
) {
742 ret
= fd_tracker_suspend_handles(tracker
, 1);
747 ret
= fs_handle_restore(handle
);
749 fd_tracker_track(tracker
, handle
);
750 return ret
? ret
: handle
->fd
;
753 int fs_handle_get_fd(struct fs_handle
*handle
)
757 pthread_mutex_lock(&handle
->tracker
->lock
);
758 pthread_mutex_lock(&handle
->lock
);
759 assert(!handle
->in_use
);
761 handle
->tracker
->stats
.uses
++;
762 if (handle
->fd
>= 0) {
764 /* Mark as most recently used. */
765 fd_tracker_untrack(handle
->tracker
, handle
);
766 fd_tracker_track(handle
->tracker
, handle
);
768 handle
->tracker
->stats
.misses
++;
769 ret
= fd_tracker_restore_handle(handle
->tracker
, handle
);
771 handle
->tracker
->stats
.errors
++;
775 handle
->in_use
= true;
777 pthread_mutex_unlock(&handle
->lock
);
778 pthread_mutex_unlock(&handle
->tracker
->lock
);
782 void fs_handle_put_fd(struct fs_handle
*handle
)
784 pthread_mutex_lock(&handle
->lock
);
785 handle
->in_use
= false;
786 pthread_mutex_unlock(&handle
->lock
);
789 int fs_handle_close(struct fs_handle
*handle
)
798 pthread_mutex_lock(&handle
->tracker
->lock
);
799 pthread_mutex_lock(&handle
->lock
);
800 fd_tracker_untrack(handle
->tracker
, handle
);
801 if (handle
->fd
>= 0) {
802 assert(!handle
->in_use
);
804 * The return value of close() is not propagated as there
805 * isn't much the user can do about it.
807 if (close(handle
->fd
)) {
808 PERROR("Failed to close the file descritptor (%d) of fs handle to %s, close() returned",
809 handle
->fd
, handle
->properties
.path
);
813 pthread_mutex_unlock(&handle
->lock
);
814 pthread_mutex_destroy(&handle
->lock
);
815 pthread_mutex_unlock(&handle
->tracker
->lock
);
816 free(handle
->properties
.path
);