+static void stream_complete_rotation(struct relay_stream *stream)
+{
+ DBG("Rotation completed for stream %" PRIu64, stream->stream_handle);
+ lttng_trace_chunk_put(stream->trace_chunk);
+ stream->trace_chunk = stream->ongoing_rotation.value.next_trace_chunk;
+ stream->ongoing_rotation = (typeof(stream->ongoing_rotation)) {};
+}
+
+static int stream_create_data_output_file_from_trace_chunk(
+ struct relay_stream *stream,
+ struct lttng_trace_chunk *trace_chunk,
+ bool force_unlink,
+ struct stream_fd **out_stream_fd)
+{
+ int ret, fd;
+ char stream_path[LTTNG_PATH_MAX];
+ enum lttng_trace_chunk_status status;
+ const int flags = O_RDWR | O_CREAT | O_TRUNC;
+ const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
+
+ ASSERT_LOCKED(stream->lock);
+ assert(stream->trace_chunk);
+
+ ret = utils_stream_file_path(stream->path_name, stream->channel_name,
+ stream->tracefile_size, stream->tracefile_current_index,
+ NULL, stream_path, sizeof(stream_path));
+ if (ret < 0) {
+ goto end;
+ }
+
+ if (stream->tracefile_wrapped_around || force_unlink) {
+ /*
+ * The on-disk ring-buffer has wrapped around.
+ * Newly created stream files will replace existing files. Since
+ * live clients may be consuming existing files, the file about
+ * to be replaced is unlinked in order to not overwrite its
+ * content.
+ */
+ status = lttng_trace_chunk_unlink_file(trace_chunk,
+ stream_path);
+ if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
+ PERROR("Failed to unlink stream file \"%s\" during trace file rotation",
+ stream_path);
+ /*
+ * Don't abort if the file doesn't exist, it is
+ * unexpected, but should not be a fatal error.
+ */
+ if (errno != ENOENT) {
+ ret = -1;
+ goto end;
+ }
+ }
+ }
+
+ status = lttng_trace_chunk_open_file(
+ trace_chunk, stream_path, flags, mode, &fd);
+ if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
+ ERR("Failed to open stream file \"%s\"", stream->channel_name);
+ ret = -1;
+ goto end;
+ }
+
+ *out_stream_fd = stream_fd_create(fd);
+ if (!*out_stream_fd) {
+ if (close(ret)) {
+ PERROR("Error closing stream file descriptor %d", ret);
+ }
+ ret = -1;
+ goto end;
+ }
+end:
+ return ret;
+}
+
+static int stream_rotate_data_file(struct relay_stream *stream)
+{
+ int ret = 0;
+
+ DBG("Rotating stream %" PRIu64 " data file",
+ stream->stream_handle);
+
+ if (stream->stream_fd) {
+ stream_fd_put(stream->stream_fd);
+ stream->stream_fd = NULL;
+ }
+
+ stream->tracefile_wrapped_around = false;
+ stream->tracefile_current_index = 0;
+
+ if (stream->ongoing_rotation.value.next_trace_chunk) {
+ struct stream_fd *new_stream_fd = NULL;
+ enum lttng_trace_chunk_status chunk_status;
+
+ chunk_status = lttng_trace_chunk_create_subdirectory(
+ stream->ongoing_rotation.value.next_trace_chunk,
+ stream->path_name);
+ if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
+ ret = -1;
+ goto end;
+ }
+
+ /* Rotate the data file. */
+ ret = stream_create_data_output_file_from_trace_chunk(stream,
+ stream->ongoing_rotation.value.next_trace_chunk,
+ false, &new_stream_fd);
+ stream->stream_fd = new_stream_fd;
+ if (ret < 0) {
+ ERR("Failed to rotate stream data file");
+ goto end;
+ }
+ }
+ stream->tracefile_size_current = 0;
+ stream->pos_after_last_complete_data_index = 0;
+ stream->ongoing_rotation.value.data_rotated = true;
+
+ if (stream->ongoing_rotation.value.index_rotated) {
+ /* Rotation completed; reset its state. */
+ stream_complete_rotation(stream);
+ }
+end:
+ return ret;
+}
+
+/*
+ * If too much data has been written in a tracefile before we received the
+ * rotation command, we have to move the excess data to the new tracefile and
+ * perform the rotation. This can happen because the control and data
+ * connections are separate, the indexes as well as the commands arrive from
+ * the control connection and we have no control over the order so we could be
+ * in a situation where too much data has been received on the data connection
+ * before the rotation command on the control connection arrives.
+ */
+static int rotate_truncate_stream(struct relay_stream *stream)
+{
+ int ret;
+ off_t lseek_ret, previous_stream_copy_origin;
+ uint64_t copy_bytes_left, misplaced_data_size;
+ bool acquired_reference;
+ struct stream_fd *previous_stream_fd = NULL;
+ struct lttng_trace_chunk *previous_chunk = NULL;
+
+ if (!LTTNG_OPTIONAL_GET(&stream->ongoing_rotation)->next_trace_chunk) {
+ ERR("Protocol error encoutered in %s(): stream rotation "
+ "sequence number is before the current sequence number "
+ "and the next trace chunk is unset. Honoring this "
+ "rotation command would result in data loss",
+ __FUNCTION__);
+ ret = -1;
+ goto end;
+ }
+
+ ASSERT_LOCKED(stream->lock);
+ /*
+ * Acquire a reference to the current trace chunk to ensure
+ * it is not reclaimed when `stream_rotate_data_file` is called.
+ * Failing to do so would violate the contract of the trace
+ * chunk API as an active file descriptor would outlive the
+ * trace chunk.
+ */
+ acquired_reference = lttng_trace_chunk_get(stream->trace_chunk);
+ assert(acquired_reference);
+ previous_chunk = stream->trace_chunk;
+
+ /*
+ * Steal the stream's reference to its stream_fd. A new
+ * stream_fd will be created when the rotation completes and
+ * the orinal stream_fd will be used to copy the "extra" data
+ * to the new file.
+ */
+ assert(stream->stream_fd);
+ previous_stream_fd = stream->stream_fd;
+ stream->stream_fd = NULL;
+
+ assert(!stream->is_metadata);
+ assert(stream->tracefile_size_current >
+ stream->pos_after_last_complete_data_index);
+ misplaced_data_size = stream->tracefile_size_current -
+ stream->pos_after_last_complete_data_index;
+ copy_bytes_left = misplaced_data_size;
+ previous_stream_copy_origin = stream->pos_after_last_complete_data_index;
+
+ ret = stream_rotate_data_file(stream);
+ if (ret) {
+ goto end;
+ }
+
+ assert(stream->stream_fd);
+ /*
+ * Seek the current tracefile to the position at which the rotation
+ * should have occurred.
+ */
+ lseek_ret = lseek(previous_stream_fd->fd, previous_stream_copy_origin,
+ SEEK_SET);
+ if (lseek_ret < 0) {
+ PERROR("Failed to seek to offset %" PRIu64
+ " while copying extra data received before a stream rotation",
+ (uint64_t) previous_stream_copy_origin);
+ ret = -1;
+ goto end;
+ }
+
+ /* Move data from the old file to the new file. */
+ while (copy_bytes_left) {
+ ssize_t io_ret;
+ char copy_buffer[FILE_IO_STACK_BUFFER_SIZE];
+ const off_t copy_size_this_pass = min_t(
+ off_t, copy_bytes_left, sizeof(copy_buffer));
+
+ io_ret = lttng_read(previous_stream_fd->fd, copy_buffer,
+ copy_size_this_pass);
+ if (io_ret < (ssize_t) copy_size_this_pass) {
+ if (io_ret == -1) {
+ PERROR("Failed to read %" PRIu64
+ " bytes from fd %i in %s(), returned %zi",
+ copy_size_this_pass,
+ previous_stream_fd->fd,
+ __FUNCTION__, io_ret);
+ } else {
+ ERR("Failed to read %" PRIu64
+ " bytes from fd %i in %s(), returned %zi",
+ copy_size_this_pass,
+ previous_stream_fd->fd,
+ __FUNCTION__, io_ret);
+ }
+ ret = -1;
+ goto end;
+ }
+
+ io_ret = lttng_write(stream->stream_fd->fd, copy_buffer,
+ copy_size_this_pass);
+ if (io_ret < (ssize_t) copy_size_this_pass) {
+ if (io_ret == -1) {
+ PERROR("Failed to write %" PRIu64
+ " bytes from fd %i in %s(), returned %zi",
+ copy_size_this_pass,
+ stream->stream_fd->fd,
+ __FUNCTION__, io_ret);
+ } else {
+ ERR("Failed to write %" PRIu64
+ " bytes from fd %i in %s(), returned %zi",
+ copy_size_this_pass,
+ stream->stream_fd->fd,
+ __FUNCTION__, io_ret);
+ }
+ ret = -1;
+ goto end;
+ }
+ copy_bytes_left -= copy_size_this_pass;
+ }
+
+ /* Truncate the file to get rid of the excess data. */
+ ret = ftruncate(previous_stream_fd->fd, previous_stream_copy_origin);
+ if (ret) {
+ PERROR("Failed to truncate current stream file to offset %" PRIu64,
+ previous_stream_copy_origin);
+ goto end;
+ }
+
+ /*
+ * Update the offset and FD of all the eventual indexes created by the
+ * data connection before the rotation command arrived.
+ */
+ ret = relay_index_switch_all_files(stream);
+ if (ret < 0) {
+ ERR("Failed to rotate index file");
+ goto end;
+ }
+
+ stream->tracefile_size_current = misplaced_data_size;
+ /* Index and data contents are back in sync. */
+ stream->pos_after_last_complete_data_index = 0;
+ ret = 0;
+end:
+ lttng_trace_chunk_put(previous_chunk);
+ stream_fd_put(previous_stream_fd);
+ return ret;
+}
+