common: index and trace-chunk file creation/open API change
[lttng-tools.git] / src / common / index / index.c
CommitLineData
309167d2
JD
1/*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
f8f3885c 4 * 2016 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
309167d2
JD
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
6c1c0768 20#define _LGPL_SOURCE
309167d2 21#include <assert.h>
1c20f0e2 22#include <sys/stat.h>
2f8f53af
DG
23#include <sys/types.h>
24#include <fcntl.h>
309167d2 25
d2956687 26#include <lttng/constant.h>
309167d2
JD
27#include <common/common.h>
28#include <common/defaults.h>
f263b7fd 29#include <common/compat/endian.h>
309167d2
JD
30#include <common/utils.h>
31
32#include "index.h"
33
2ef0da38 34#define WRITE_FILE_FLAGS (O_WRONLY | O_CREAT | O_TRUNC)
ebb29c10
JG
35#define READ_ONLY_FILE_FLAGS O_RDONLY
36
3ff5c5db 37static enum lttng_trace_chunk_status _lttng_index_file_create_from_trace_chunk(
d2956687 38 struct lttng_trace_chunk *chunk,
ebb29c10 39 const char *channel_path, const char *stream_name,
c35f9726 40 uint64_t stream_file_size, uint64_t stream_file_index,
d2956687 41 uint32_t index_major, uint32_t index_minor,
ebb29c10 42 bool unlink_existing_file,
3ff5c5db 43 int flags, bool expect_no_file, struct lttng_index_file **file)
d2956687
JG
44{
45 struct lttng_index_file *index_file;
46 enum lttng_trace_chunk_status chunk_status;
47 int ret, fd = -1;
48 ssize_t size_ret;
49 struct ctf_packet_index_file_hdr hdr;
50 char index_directory_path[LTTNG_PATH_MAX];
51 char index_file_path[LTTNG_PATH_MAX];
d2956687 52 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
ebb29c10 53 const bool acquired_reference = lttng_trace_chunk_get(chunk);
cc280027 54 const char *separator;
c35f9726
JG
55
56 assert(acquired_reference);
d2956687
JG
57
58 index_file = zmalloc(sizeof(*index_file));
59 if (!index_file) {
60 PERROR("Failed to allocate lttng_index_file");
3ff5c5db 61 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
d2956687
JG
62 goto error;
63 }
64
c35f9726 65 index_file->trace_chunk = chunk;
cc280027
MD
66 if (channel_path[0] == '\0') {
67 separator = "";
68 } else {
69 separator = "/";
70 }
d2956687 71 ret = snprintf(index_directory_path, sizeof(index_directory_path),
cc280027 72 "%s%s" DEFAULT_INDEX_DIR, channel_path, separator);
d2956687
JG
73 if (ret < 0 || ret >= sizeof(index_directory_path)) {
74 ERR("Failed to format index directory path");
3ff5c5db 75 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
d2956687
JG
76 goto error;
77 }
78
79 ret = utils_stream_file_path(index_directory_path, stream_name,
c35f9726 80 stream_file_size, stream_file_index,
d2956687
JG
81 DEFAULT_INDEX_FILE_SUFFIX,
82 index_file_path, sizeof(index_file_path));
83 if (ret) {
3ff5c5db 84 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
d2956687
JG
85 goto error;
86 }
87
88 if (unlink_existing_file) {
89 /*
90 * For tracefile rotation. We need to unlink the old
91 * file if present to synchronize with the tail of the
92 * live viewer which could be working on this same file.
93 * By doing so, any reference to the old index file
94 * stays valid even if we re-create a new file with the
95 * same name afterwards.
96 */
348a81dc
JG
97 chunk_status = lttng_trace_chunk_unlink_file(
98 chunk, index_file_path);
99 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK &&
100 !(chunk_status == LTTNG_TRACE_CHUNK_STATUS_ERROR &&
101 errno == ENOENT)) {
d2956687
JG
102 goto error;
103 }
348a81dc 104 }
d2956687
JG
105
106 chunk_status = lttng_trace_chunk_open_file(chunk, index_file_path,
3ff5c5db 107 flags, mode, &fd, expect_no_file);
d2956687
JG
108 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
109 goto error;
110 }
111
84546278
MD
112 if (flags == WRITE_FILE_FLAGS) {
113 ctf_packet_index_file_hdr_init(&hdr, index_major, index_minor);
114 size_ret = lttng_write(fd, &hdr, sizeof(hdr));
115 if (size_ret < sizeof(hdr)) {
116 PERROR("Failed to write index header");
3ff5c5db 117 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
84546278
MD
118 goto error;
119 }
120 index_file->element_len = ctf_packet_index_len(index_major, index_minor);
121 } else {
122 uint32_t element_len;
123
124 size_ret = lttng_read(fd, &hdr, sizeof(hdr));
125 if (size_ret < 0) {
126 PERROR("Failed to read index header");
3ff5c5db 127 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
84546278
MD
128 goto error;
129 }
130 if (be32toh(hdr.magic) != CTF_INDEX_MAGIC) {
131 ERR("Invalid header magic");
3ff5c5db 132 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
84546278
MD
133 goto error;
134 }
135 if (index_major != be32toh(hdr.index_major)) {
136 ERR("Index major number mismatch: %u, expect %u",
137 be32toh(hdr.index_major), index_major);
3ff5c5db 138 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
84546278
MD
139 goto error;
140 }
141 if (index_minor != be32toh(hdr.index_minor)) {
142 ERR("Index minor number mismatch: %u, expect %u",
143 be32toh(hdr.index_minor), index_minor);
3ff5c5db 144 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
84546278
MD
145 goto error;
146 }
147 element_len = be32toh(hdr.packet_index_len);
148 if (element_len > sizeof(struct ctf_packet_index)) {
149 ERR("Index element length too long");
3ff5c5db 150 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
84546278
MD
151 goto error;
152 }
153 index_file->element_len = element_len;
d2956687
JG
154 }
155 index_file->fd = fd;
156 index_file->major = index_major;
157 index_file->minor = index_minor;
d2956687
JG
158 urcu_ref_init(&index_file->ref);
159
3ff5c5db
MD
160 *file = index_file;
161 return LTTNG_TRACE_CHUNK_STATUS_OK;
d2956687
JG
162
163error:
164 if (fd >= 0) {
165 ret = close(fd);
166 if (ret < 0) {
167 PERROR("Failed to close file descriptor of index file");
168 }
169 }
307db950 170 lttng_trace_chunk_put(chunk);
d2956687 171 free(index_file);
3ff5c5db 172 return chunk_status;
d2956687
JG
173}
174
3ff5c5db 175enum lttng_trace_chunk_status lttng_index_file_create_from_trace_chunk(
ebb29c10
JG
176 struct lttng_trace_chunk *chunk,
177 const char *channel_path, const char *stream_name,
178 uint64_t stream_file_size, uint64_t stream_file_index,
179 uint32_t index_major, uint32_t index_minor,
3ff5c5db 180 bool unlink_existing_file, struct lttng_index_file **file)
ebb29c10
JG
181{
182 return _lttng_index_file_create_from_trace_chunk(chunk, channel_path,
183 stream_name, stream_file_size, stream_file_index,
184 index_major, index_minor, unlink_existing_file,
3ff5c5db 185 WRITE_FILE_FLAGS, false, file);
ebb29c10
JG
186}
187
3ff5c5db 188enum lttng_trace_chunk_status lttng_index_file_create_from_trace_chunk_read_only(
ebb29c10
JG
189 struct lttng_trace_chunk *chunk,
190 const char *channel_path, const char *stream_name,
191 uint64_t stream_file_size, uint64_t stream_file_index,
3ff5c5db
MD
192 uint32_t index_major, uint32_t index_minor,
193 bool expect_no_file, struct lttng_index_file **file)
ebb29c10
JG
194{
195 return _lttng_index_file_create_from_trace_chunk(chunk, channel_path,
196 stream_name, stream_file_size, stream_file_index,
197 index_major, index_minor, false,
3ff5c5db 198 READ_ONLY_FILE_FLAGS, expect_no_file, file);
ebb29c10
JG
199}
200
309167d2 201/*
f8f3885c 202 * Write index values to the given index file.
309167d2 203 *
f8f3885c 204 * Return 0 on success, -1 on error.
309167d2 205 */
f8f3885c
MD
206int lttng_index_file_write(const struct lttng_index_file *index_file,
207 const struct ctf_packet_index *element)
309167d2 208{
0b0ac4a9
JR
209 int fd;
210 size_t len;
6cd525e8 211 ssize_t ret;
309167d2 212
0b0ac4a9 213 assert(index_file);
f8f3885c 214 assert(element);
309167d2 215
0b0ac4a9
JR
216 fd = index_file->fd;
217 len = index_file->element_len;
218
183f6fa2 219 if (fd < 0) {
183f6fa2
DG
220 goto error;
221 }
222
f8f3885c 223 ret = lttng_write(fd, element, len);
6cd525e8 224 if (ret < len) {
309167d2 225 PERROR("writing index file");
f8f3885c
MD
226 goto error;
227 }
228 return 0;
229
230error:
231 return -1;
232}
233
234/*
235 * Read index values from the given index file.
236 *
237 * Return 0 on success, -1 on error.
238 */
239int lttng_index_file_read(const struct lttng_index_file *index_file,
240 struct ctf_packet_index *element)
241{
242 ssize_t ret;
243 int fd = index_file->fd;
244 size_t len = index_file->element_len;
245
246 assert(element);
247
248 if (fd < 0) {
249 goto error;
250 }
251
252 ret = lttng_read(fd, element, len);
2239b15a 253 if (ret < 0) {
f8f3885c
MD
254 PERROR("read index file");
255 goto error;
309167d2 256 }
2239b15a
MD
257 if (ret < len) {
258 ERR("lttng_read expected %zu, returned %zd", len, ret);
259 goto error;
260 }
f8f3885c 261 return 0;
309167d2 262
183f6fa2 263error:
f8f3885c 264 return -1;
309167d2 265}
2f8f53af 266
f8f3885c
MD
267void lttng_index_file_get(struct lttng_index_file *index_file)
268{
269 urcu_ref_get(&index_file->ref);
270}
271
272static void lttng_index_file_release(struct urcu_ref *ref)
273{
274 struct lttng_index_file *index_file = caa_container_of(ref,
275 struct lttng_index_file, ref);
276
277 if (close(index_file->fd)) {
278 PERROR("close index fd");
279 }
c35f9726 280 lttng_trace_chunk_put(index_file->trace_chunk);
f8f3885c
MD
281 free(index_file);
282}
283
284void lttng_index_file_put(struct lttng_index_file *index_file)
285{
286 urcu_ref_put(&index_file->ref, lttng_index_file_release);
2f8f53af 287}
This page took 0.053585 seconds and 4 git commands to generate.