Typo: 'Descritptor' -> 'Descriptor'
[lttng-tools.git] / src / common / fd-tracker / fd-tracker.c
index fd557393081946b944a958983dc43643f4a2ba55..772bf04bc5994efa5fc351a2ed933514506de36c 100644 (file)
@@ -1,18 +1,8 @@
 /*
- * 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>
 
 #include <common/defaults.h>
 #include <common/error.h>
+#include <common/fs-handle-internal.h>
 #include <common/hashtable/hashtable.h>
 #include <common/hashtable/utils.h>
 #include <common/macros.h>
-#include <common/fs-handle-internal.h>
+#include <common/optional.h>
 
 #include "fd-tracker.h"
 #include "inode.h"
@@ -84,14 +75,14 @@ struct fd_tracker {
        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 {
        int flags;
-       struct {
-               bool is_set;
-               mode_t value;
-       } mode;
+       LTTNG_OPTIONAL(mode_t) mode;
 };
 
 /*
@@ -145,7 +136,7 @@ static int match_fd(struct cds_lfht_node *node, const void *key);
 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);
@@ -165,13 +156,6 @@ static int fd_tracker_suspend_handles(
 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)
 {
@@ -226,7 +210,7 @@ static void fs_handle_tracked_log(struct fs_handle_tracked *handle)
        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,
@@ -243,9 +227,11 @@ static int fs_handle_tracked_suspend(struct fs_handle_tracked *handle)
        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);
+       lttng_inode_borrow_location(
+                       handle->inode, &node_directory_handle, &path);
        assert(handle->fd >= 0);
        if (handle->in_use) {
                /* This handle can't be suspended as it is currently in use. */
@@ -253,7 +239,8 @@ static int fs_handle_tracked_suspend(struct fs_handle_tracked *handle)
                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);
@@ -299,11 +286,16 @@ end:
 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;
+
+       lttng_inode_borrow_location(
+                       handle->inode, &node_directory_handle, &path);
 
        assert(handle->fd == -1);
        assert(path);
-       ret = open_from_properties(path, &handle->properties);
+       ret = open_from_properties(
+                       node_directory_handle, path, &handle->properties);
        if (ret < 0) {
                PERROR("Failed to restore filesystem handle to %s, open() failed",
                                path);
@@ -331,7 +323,7 @@ end:
        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;
@@ -343,9 +335,11 @@ static int open_from_properties(
         * 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
@@ -366,7 +360,9 @@ end:
        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));
 
@@ -395,6 +391,12 @@ struct fd_tracker *fd_tracker_create(unsigned int capacity)
                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:
@@ -404,6 +406,7 @@ error:
        return NULL;
 }
 
+LTTNG_HIDDEN
 void fd_tracker_log(struct fd_tracker *tracker)
 {
        struct fs_handle_tracked *handle;
@@ -452,10 +455,14 @@ void fd_tracker_log(struct fd_tracker *tracker)
        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.
@@ -477,13 +484,16 @@ int fd_tracker_destroy(struct fd_tracker *tracker)
        }
 
        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)
@@ -520,7 +530,13 @@ struct fs_handle *fd_tracker_open_fs_handle(struct fd_tracker *tracker,
        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);
@@ -529,7 +545,7 @@ struct fs_handle *fd_tracker_open_fs_handle(struct fd_tracker *tracker,
                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;
@@ -537,8 +553,9 @@ struct fs_handle *fd_tracker_open_fs_handle(struct fd_tracker *tracker,
 
        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;
@@ -570,6 +587,7 @@ static int fd_tracker_suspend_handles(
                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,
@@ -582,14 +600,16 @@ static int fd_tracker_suspend_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,
@@ -622,7 +642,7 @@ int fd_tracker_open_unsuspendable_fd(struct fd_tracker *tracker,
                } 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)",
@@ -687,6 +707,7 @@ end_free_entries:
        goto end_unlock;
 }
 
+LTTNG_HIDDEN
 int fd_tracker_close_unsuspendable_fd(struct fd_tracker *tracker,
                int *fds_in,
                unsigned int fd_count,
@@ -860,7 +881,7 @@ static int fs_handle_tracked_unlink(struct fs_handle *_handle)
 
        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;
@@ -872,6 +893,7 @@ static int fs_handle_tracked_close(struct fs_handle *_handle)
        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;
@@ -881,7 +903,29 @@ static int fs_handle_tracked_close(struct fs_handle *_handle)
        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) {
@@ -890,7 +934,7 @@ static int fs_handle_tracked_close(struct fs_handle *_handle)
                 * 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;
@@ -902,6 +946,7 @@ static int fs_handle_tracked_close(struct fs_handle *_handle)
        pthread_mutex_destroy(&handle->lock);
        pthread_mutex_unlock(&handle->tracker->lock);
        free(handle);
+       lttng_directory_handle_put(inode_directory_handle);
 end:
        return ret;
 }
This page took 0.029214 seconds and 4 git commands to generate.