/*
- * Copyright (C) 2018, 2020 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ * Copyright (C) 2018-2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
*
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License, version 2 only, as
- * published by the Free Software Foundation.
+ * SPDX-License-Identifier: GPL-2.0-only
*
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 51
- * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <urcu.h>
struct cds_list_head suspended_handles;
struct cds_lfht *unsuspendable_fds;
struct lttng_inode_registry *inode_registry;
+ /* Unlinked files are moved in this directory under a unique name. */
+ struct lttng_directory_handle *unlink_directory_handle;
+ struct lttng_unlinked_file_pool *unlinked_file_pool;
};
struct open_properties {
static void unsuspendable_fd_destroy(struct unsuspendable_fd *entry);
static struct unsuspendable_fd *unsuspendable_fd_create(
const char *name, int fd);
-static int open_from_properties(
+static int open_from_properties(const struct lttng_directory_handle *dir_handle,
const char *path, struct open_properties *properties);
static void fs_handle_tracked_log(struct fs_handle_tracked *handle);
static int fd_tracker_restore_handle(
struct fd_tracker *tracker, struct fs_handle_tracked *handle);
-static const struct fs_handle fs_handle_tracked_callbacks = {
- .get_fd = fs_handle_tracked_get_fd,
- .put_fd = fs_handle_tracked_put_fd,
- .unlink = fs_handle_tracked_unlink,
- .close = fs_handle_tracked_close,
-};
-
/* Match function of the tracker's unsuspendable_fds hash table. */
static int match_fd(struct cds_lfht_node *node, const void *key)
{
const char *path;
pthread_mutex_lock(&handle->lock);
- path = lttng_inode_get_path(handle->inode);
+ lttng_inode_borrow_location(handle->inode, NULL, &path);
if (handle->fd >= 0) {
DBG_NO_LOC(" %s [active, fd %d%s]", path, handle->fd,
int ret = 0;
struct stat fs_stat;
const char *path;
+ const struct lttng_directory_handle *node_directory_handle;
pthread_mutex_lock(&handle->lock);
- path = lttng_inode_get_path(handle->inode);
- assert(handle->fd >= 0);
+ lttng_inode_borrow_location(
+ handle->inode, &node_directory_handle, &path);
+ LTTNG_ASSERT(handle->fd >= 0);
if (handle->in_use) {
/* This handle can't be suspended as it is currently in use. */
ret = -EAGAIN;
goto end;
}
- ret = stat(path, &fs_stat);
+ ret = lttng_directory_handle_stat(
+ node_directory_handle, path, &fs_stat);
if (ret) {
PERROR("Filesystem handle to %s cannot be suspended as stat() failed",
path);
static int fs_handle_tracked_restore(struct fs_handle_tracked *handle)
{
int ret, fd = -1;
- const char *path = lttng_inode_get_path(handle->inode);
+ const char *path;
+ const struct lttng_directory_handle *node_directory_handle;
- assert(handle->fd == -1);
- assert(path);
- ret = open_from_properties(path, &handle->properties);
+ lttng_inode_borrow_location(
+ handle->inode, &node_directory_handle, &path);
+
+ LTTNG_ASSERT(handle->fd == -1);
+ LTTNG_ASSERT(path);
+ ret = open_from_properties(
+ node_directory_handle, path, &handle->properties);
if (ret < 0) {
PERROR("Failed to restore filesystem handle to %s, open() failed",
path);
return ret;
}
-static int open_from_properties(
+static int open_from_properties(const struct lttng_directory_handle *dir_handle,
const char *path, struct open_properties *properties)
{
int ret;
* thus it is ignored here.
*/
if ((properties->flags & O_CREAT) && properties->mode.is_set) {
- ret = open(path, properties->flags, properties->mode.value);
+ ret = lttng_directory_handle_open_file(dir_handle, path,
+ properties->flags, properties->mode.value);
} else {
- ret = open(path, properties->flags);
+ ret = lttng_directory_handle_open_file(dir_handle, path,
+ properties->flags, 0);
}
/*
* Some flags should not be used beyond the initial open() of a
return ret;
}
-struct fd_tracker *fd_tracker_create(unsigned int capacity)
+LTTNG_HIDDEN
+struct fd_tracker *fd_tracker_create(const char *unlinked_file_path,
+ unsigned int capacity)
{
struct fd_tracker *tracker = zmalloc(sizeof(struct fd_tracker));
ERR("Failed to create fd-tracker's inode registry");
goto error;
}
+
+ tracker->unlinked_file_pool =
+ lttng_unlinked_file_pool_create(unlinked_file_path);
+ if (!tracker->unlinked_file_pool) {
+ goto error;
+ }
DBG("File descriptor tracker created with a limit of %u simultaneously-opened FDs",
capacity);
end:
return NULL;
}
+LTTNG_HIDDEN
void fd_tracker_log(struct fd_tracker *tracker)
{
struct fs_handle_tracked *handle;
pthread_mutex_unlock(&tracker->lock);
}
+LTTNG_HIDDEN
int fd_tracker_destroy(struct fd_tracker *tracker)
{
int ret = 0;
+ if (!tracker) {
+ goto end;
+ }
/*
* Refuse to destroy the tracker as fs_handles may still old
* weak references to the tracker.
if (tracker->unsuspendable_fds) {
ret = cds_lfht_destroy(tracker->unsuspendable_fds, NULL);
- assert(!ret);
+ LTTNG_ASSERT(!ret);
}
lttng_inode_registry_destroy(tracker->inode_registry);
+ lttng_unlinked_file_pool_destroy(tracker->unlinked_file_pool);
pthread_mutex_destroy(&tracker->lock);
free(tracker);
end:
return ret;
}
+LTTNG_HIDDEN
struct fs_handle *fd_tracker_open_fs_handle(struct fd_tracker *tracker,
+ struct lttng_directory_handle *directory,
const char *path,
int flags,
mode_t *mode)
if (!handle) {
goto end;
}
- handle->parent = fs_handle_tracked_callbacks;
+ handle->parent = (typeof(handle->parent)) {
+ .get_fd = fs_handle_tracked_get_fd,
+ .put_fd = fs_handle_tracked_put_fd,
+ .unlink = fs_handle_tracked_unlink,
+ .close = fs_handle_tracked_close,
+ };
+
handle->tracker = tracker;
ret = pthread_mutex_init(&handle->lock, NULL);
goto error_mutex_init;
}
- handle->fd = open_from_properties(path, &properties);
+ handle->fd = open_from_properties(directory, path, &properties);
if (handle->fd < 0) {
PERROR("Failed to open fs handle to %s, open() returned", path);
goto error;
handle->properties = properties;
- handle->inode = lttng_inode_registry_get_inode(
- tracker->inode_registry, handle->fd, path);
+ handle->inode = lttng_inode_registry_get_inode(tracker->inode_registry,
+ directory, path, handle->fd,
+ tracker->unlinked_file_pool);
if (!handle->inode) {
ERR("Failed to get lttng_inode corresponding to file %s", path);
goto error;
struct fd_tracker *tracker, unsigned int count)
{
unsigned int left_to_close = count;
+ unsigned int attempts_left = tracker->count.suspendable.active;
struct fs_handle_tracked *handle, *tmp;
cds_list_for_each_entry_safe(handle, tmp, &tracker->active_handles,
if (!ret) {
left_to_close--;
}
+ attempts_left--;
- if (!left_to_close) {
+ if (left_to_close == 0 || attempts_left == 0) {
break;
}
}
return left_to_close ? -EMFILE : 0;
}
+LTTNG_HIDDEN
int fd_tracker_open_unsuspendable_fd(struct fd_tracker *tracker,
int *out_fds,
const char **names,
} else {
/*
* There are not enough active suspendable file
- * descriptors to open a new fd and still accomodate the
+ * descriptors to open a new fd and still accommodates the
* tracker's capacity.
*/
WARN("Cannot open unsuspendable fd, too many unsuspendable file descriptors are opened (%u)",
goto end_unlock;
}
+LTTNG_HIDDEN
int fd_tracker_close_unsuspendable_fd(struct fd_tracker *tracker,
int *fds_in,
unsigned int fd_count,
*/
pthread_mutex_lock(&handle->tracker->lock);
pthread_mutex_lock(&handle->lock);
- assert(!handle->in_use);
+ LTTNG_ASSERT(!handle->in_use);
handle->tracker->stats.uses++;
if (handle->fd >= 0) {
pthread_mutex_lock(&handle->tracker->lock);
pthread_mutex_lock(&handle->lock);
- ret = lttng_inode_defer_unlink(handle->inode);
+ ret = lttng_inode_unlink(handle->inode);
pthread_mutex_unlock(&handle->lock);
pthread_mutex_unlock(&handle->tracker->lock);
return ret;
const char *path = NULL;
struct fs_handle_tracked *handle =
container_of(_handle, struct fs_handle_tracked, parent);
+ struct lttng_directory_handle *inode_directory_handle = NULL;
if (!handle) {
ret = -EINVAL;
pthread_mutex_lock(&handle->tracker->lock);
pthread_mutex_lock(&handle->lock);
if (handle->inode) {
- path = lttng_inode_get_path(handle->inode);
+ lttng_inode_borrow_location(handle->inode, NULL, &path);
+ /*
+ * Here a reference to the inode's directory handle is acquired
+ * to prevent the last reference to it from being released while
+ * the tracker's lock is taken.
+ *
+ * If this wasn't done, the directory handle could attempt to
+ * close its underlying directory file descriptor, which would
+ * attempt to lock the tracker's lock, resulting in a deadlock.
+ *
+ * Since a new reference to the directory handle is taken within
+ * the scope of this function, it is not possible for the last
+ * reference to the inode's location directory handle to be
+ * released during the call to lttng_inode_put().
+ *
+ * We wait until the tracker's lock is released to release the
+ * reference. Hence, the call to the tracker is delayed just
+ * enough to not attempt to recursively acquire the tracker's
+ * lock twice.
+ */
+ inode_directory_handle =
+ lttng_inode_get_location_directory_handle(
+ handle->inode);
}
fd_tracker_untrack(handle->tracker, handle);
if (handle->fd >= 0) {
* isn't much the user can do about it.
*/
if (close(handle->fd)) {
- PERROR("Failed to close the file descritptor (%d) of fs handle to %s, close() returned",
+ PERROR("Failed to close the file descriptor (%d) of fs handle to %s, close() returned",
handle->fd, path ? path : "Unknown");
}
handle->fd = -1;
pthread_mutex_destroy(&handle->lock);
pthread_mutex_unlock(&handle->tracker->lock);
free(handle);
+ lttng_directory_handle_put(inode_directory_handle);
end:
return ret;
}