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