| 1 | /* |
| 2 | * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com> |
| 3 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> |
| 4 | * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0-only |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #define _LGPL_SOURCE |
| 11 | #include "index.hpp" |
| 12 | |
| 13 | #include <common/common.hpp> |
| 14 | #include <common/compat/endian.hpp> |
| 15 | #include <common/defaults.hpp> |
| 16 | #include <common/utils.hpp> |
| 17 | |
| 18 | #include <lttng/constant.h> |
| 19 | |
| 20 | #include <fcntl.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
| 23 | |
| 24 | #define WRITE_FILE_FLAGS (O_WRONLY | O_CREAT | O_TRUNC) |
| 25 | #define READ_ONLY_FILE_FLAGS O_RDONLY |
| 26 | |
| 27 | static enum lttng_trace_chunk_status |
| 28 | _lttng_index_file_create_from_trace_chunk(struct lttng_trace_chunk *chunk, |
| 29 | const char *channel_path, |
| 30 | const char *stream_name, |
| 31 | uint64_t stream_file_size, |
| 32 | uint64_t stream_file_index, |
| 33 | uint32_t index_major, |
| 34 | uint32_t index_minor, |
| 35 | bool unlink_existing_file, |
| 36 | int flags, |
| 37 | bool expect_no_file, |
| 38 | struct lttng_index_file **file) |
| 39 | { |
| 40 | struct lttng_index_file *index_file; |
| 41 | enum lttng_trace_chunk_status chunk_status; |
| 42 | int ret; |
| 43 | struct fs_handle *fs_handle = nullptr; |
| 44 | ssize_t size_ret; |
| 45 | struct ctf_packet_index_file_hdr hdr; |
| 46 | char index_directory_path[LTTNG_PATH_MAX]; |
| 47 | char index_file_path[LTTNG_PATH_MAX]; |
| 48 | const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; |
| 49 | const bool acquired_reference = lttng_trace_chunk_get(chunk); |
| 50 | const char *separator; |
| 51 | |
| 52 | LTTNG_ASSERT(acquired_reference); |
| 53 | |
| 54 | index_file = zmalloc<lttng_index_file>(); |
| 55 | if (!index_file) { |
| 56 | PERROR("Failed to allocate lttng_index_file"); |
| 57 | chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR; |
| 58 | goto error; |
| 59 | } |
| 60 | |
| 61 | index_file->trace_chunk = chunk; |
| 62 | if (channel_path[0] == '\0') { |
| 63 | separator = ""; |
| 64 | } else { |
| 65 | separator = "/"; |
| 66 | } |
| 67 | ret = snprintf(index_directory_path, |
| 68 | sizeof(index_directory_path), |
| 69 | "%s%s" DEFAULT_INDEX_DIR, |
| 70 | channel_path, |
| 71 | separator); |
| 72 | if (ret < 0 || ret >= sizeof(index_directory_path)) { |
| 73 | ERR("Failed to format index directory path"); |
| 74 | chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR; |
| 75 | goto error; |
| 76 | } |
| 77 | |
| 78 | ret = utils_stream_file_path(index_directory_path, |
| 79 | stream_name, |
| 80 | stream_file_size, |
| 81 | stream_file_index, |
| 82 | DEFAULT_INDEX_FILE_SUFFIX, |
| 83 | index_file_path, |
| 84 | sizeof(index_file_path)); |
| 85 | if (ret) { |
| 86 | chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR; |
| 87 | goto error; |
| 88 | } |
| 89 | |
| 90 | if (unlink_existing_file) { |
| 91 | /* |
| 92 | * For tracefile rotation. We need to unlink the old |
| 93 | * file if present to synchronize with the tail of the |
| 94 | * live viewer which could be working on this same file. |
| 95 | * By doing so, any reference to the old index file |
| 96 | * stays valid even if we re-create a new file with the |
| 97 | * same name afterwards. |
| 98 | */ |
| 99 | chunk_status = (lttng_trace_chunk_status) lttng_trace_chunk_unlink_file( |
| 100 | chunk, index_file_path); |
| 101 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK && |
| 102 | !(chunk_status == LTTNG_TRACE_CHUNK_STATUS_ERROR && errno == ENOENT)) { |
| 103 | goto error; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | chunk_status = lttng_trace_chunk_open_fs_handle( |
| 108 | chunk, index_file_path, flags, mode, &fs_handle, expect_no_file); |
| 109 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 110 | goto error; |
| 111 | } |
| 112 | |
| 113 | if (flags == WRITE_FILE_FLAGS) { |
| 114 | ctf_packet_index_file_hdr_init(&hdr, index_major, index_minor); |
| 115 | size_ret = fs_handle_write(fs_handle, &hdr, sizeof(hdr)); |
| 116 | if (size_ret < sizeof(hdr)) { |
| 117 | PERROR("Failed to write index header"); |
| 118 | chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR; |
| 119 | goto error; |
| 120 | } |
| 121 | index_file->element_len = ctf_packet_index_len(index_major, index_minor); |
| 122 | } else { |
| 123 | uint32_t element_len; |
| 124 | |
| 125 | size_ret = fs_handle_read(fs_handle, &hdr, sizeof(hdr)); |
| 126 | if (size_ret < 0) { |
| 127 | PERROR("Failed to read index header"); |
| 128 | chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR; |
| 129 | goto error; |
| 130 | } |
| 131 | if (be32toh(hdr.magic) != CTF_INDEX_MAGIC) { |
| 132 | ERR("Invalid header magic"); |
| 133 | chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR; |
| 134 | goto error; |
| 135 | } |
| 136 | if (index_major != be32toh(hdr.index_major)) { |
| 137 | ERR("Index major number mismatch: %u, expect %u", |
| 138 | be32toh(hdr.index_major), |
| 139 | index_major); |
| 140 | chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR; |
| 141 | goto error; |
| 142 | } |
| 143 | if (index_minor != be32toh(hdr.index_minor)) { |
| 144 | ERR("Index minor number mismatch: %u, expect %u", |
| 145 | be32toh(hdr.index_minor), |
| 146 | index_minor); |
| 147 | chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR; |
| 148 | goto error; |
| 149 | } |
| 150 | element_len = be32toh(hdr.packet_index_len); |
| 151 | if (element_len > sizeof(struct ctf_packet_index)) { |
| 152 | ERR("Index element length too long"); |
| 153 | chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR; |
| 154 | goto error; |
| 155 | } |
| 156 | index_file->element_len = element_len; |
| 157 | } |
| 158 | index_file->file = fs_handle; |
| 159 | index_file->major = index_major; |
| 160 | index_file->minor = index_minor; |
| 161 | urcu_ref_init(&index_file->ref); |
| 162 | |
| 163 | *file = index_file; |
| 164 | return LTTNG_TRACE_CHUNK_STATUS_OK; |
| 165 | |
| 166 | error: |
| 167 | if (fs_handle) { |
| 168 | ret = fs_handle_close(fs_handle); |
| 169 | if (ret < 0) { |
| 170 | PERROR("Failed to close file descriptor of index file"); |
| 171 | } |
| 172 | } |
| 173 | lttng_trace_chunk_put(chunk); |
| 174 | free(index_file); |
| 175 | return chunk_status; |
| 176 | } |
| 177 | |
| 178 | enum lttng_trace_chunk_status |
| 179 | lttng_index_file_create_from_trace_chunk(struct lttng_trace_chunk *chunk, |
| 180 | const char *channel_path, |
| 181 | const char *stream_name, |
| 182 | uint64_t stream_file_size, |
| 183 | uint64_t stream_file_index, |
| 184 | uint32_t index_major, |
| 185 | uint32_t index_minor, |
| 186 | bool unlink_existing_file, |
| 187 | struct lttng_index_file **file) |
| 188 | { |
| 189 | return _lttng_index_file_create_from_trace_chunk(chunk, |
| 190 | channel_path, |
| 191 | stream_name, |
| 192 | stream_file_size, |
| 193 | stream_file_index, |
| 194 | index_major, |
| 195 | index_minor, |
| 196 | unlink_existing_file, |
| 197 | WRITE_FILE_FLAGS, |
| 198 | false, |
| 199 | file); |
| 200 | } |
| 201 | |
| 202 | enum lttng_trace_chunk_status |
| 203 | lttng_index_file_create_from_trace_chunk_read_only(struct lttng_trace_chunk *chunk, |
| 204 | const char *channel_path, |
| 205 | const char *stream_name, |
| 206 | uint64_t stream_file_size, |
| 207 | uint64_t stream_file_index, |
| 208 | uint32_t index_major, |
| 209 | uint32_t index_minor, |
| 210 | bool expect_no_file, |
| 211 | struct lttng_index_file **file) |
| 212 | { |
| 213 | return _lttng_index_file_create_from_trace_chunk(chunk, |
| 214 | channel_path, |
| 215 | stream_name, |
| 216 | stream_file_size, |
| 217 | stream_file_index, |
| 218 | index_major, |
| 219 | index_minor, |
| 220 | false, |
| 221 | READ_ONLY_FILE_FLAGS, |
| 222 | expect_no_file, |
| 223 | file); |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Write index values to the given index file. |
| 228 | * |
| 229 | * Return 0 on success, -1 on error. |
| 230 | */ |
| 231 | int lttng_index_file_write(const struct lttng_index_file *index_file, |
| 232 | const struct ctf_packet_index *element) |
| 233 | { |
| 234 | ssize_t ret; |
| 235 | const size_t len = index_file->element_len; |
| 236 | ; |
| 237 | |
| 238 | LTTNG_ASSERT(index_file); |
| 239 | LTTNG_ASSERT(element); |
| 240 | |
| 241 | if (!index_file->file) { |
| 242 | goto error; |
| 243 | } |
| 244 | |
| 245 | ret = fs_handle_write(index_file->file, element, len); |
| 246 | if (ret < len) { |
| 247 | PERROR("writing index file"); |
| 248 | goto error; |
| 249 | } |
| 250 | return 0; |
| 251 | |
| 252 | error: |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * Read index values from the given index file. |
| 258 | * |
| 259 | * Return 0 on success, -1 on error. |
| 260 | */ |
| 261 | int lttng_index_file_read(const struct lttng_index_file *index_file, |
| 262 | struct ctf_packet_index *element) |
| 263 | { |
| 264 | ssize_t ret; |
| 265 | const size_t len = index_file->element_len; |
| 266 | |
| 267 | LTTNG_ASSERT(element); |
| 268 | |
| 269 | if (!index_file->file) { |
| 270 | goto error; |
| 271 | } |
| 272 | |
| 273 | ret = fs_handle_read(index_file->file, element, len); |
| 274 | if (ret < 0) { |
| 275 | PERROR("read index file"); |
| 276 | goto error; |
| 277 | } |
| 278 | if (ret < len) { |
| 279 | ERR("lttng_read expected %zu, returned %zd", len, ret); |
| 280 | goto error; |
| 281 | } |
| 282 | return 0; |
| 283 | |
| 284 | error: |
| 285 | return -1; |
| 286 | } |
| 287 | |
| 288 | void lttng_index_file_get(struct lttng_index_file *index_file) |
| 289 | { |
| 290 | urcu_ref_get(&index_file->ref); |
| 291 | } |
| 292 | |
| 293 | static void lttng_index_file_release(struct urcu_ref *ref) |
| 294 | { |
| 295 | struct lttng_index_file *index_file = caa_container_of(ref, struct lttng_index_file, ref); |
| 296 | |
| 297 | if (fs_handle_close(index_file->file)) { |
| 298 | PERROR("close index fd"); |
| 299 | } |
| 300 | lttng_trace_chunk_put(index_file->trace_chunk); |
| 301 | free(index_file); |
| 302 | } |
| 303 | |
| 304 | void lttng_index_file_put(struct lttng_index_file *index_file) |
| 305 | { |
| 306 | urcu_ref_put(&index_file->ref, lttng_index_file_release); |
| 307 | } |