relayd: replace uses of block FDs by the fs_handle interface
[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;
8bb66c3c
JG
47 int ret;
48 struct fs_handle *fs_handle = NULL;
d2956687
JG
49 ssize_t size_ret;
50 struct ctf_packet_index_file_hdr hdr;
51 char index_directory_path[LTTNG_PATH_MAX];
52 char index_file_path[LTTNG_PATH_MAX];
d2956687 53 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
ebb29c10 54 const bool acquired_reference = lttng_trace_chunk_get(chunk);
cc280027 55 const char *separator;
c35f9726
JG
56
57 assert(acquired_reference);
d2956687
JG
58
59 index_file = zmalloc(sizeof(*index_file));
60 if (!index_file) {
61 PERROR("Failed to allocate lttng_index_file");
3ff5c5db 62 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
d2956687
JG
63 goto error;
64 }
65
c35f9726 66 index_file->trace_chunk = chunk;
cc280027
MD
67 if (channel_path[0] == '\0') {
68 separator = "";
69 } else {
70 separator = "/";
71 }
d2956687 72 ret = snprintf(index_directory_path, sizeof(index_directory_path),
cc280027 73 "%s%s" DEFAULT_INDEX_DIR, channel_path, separator);
d2956687
JG
74 if (ret < 0 || ret >= sizeof(index_directory_path)) {
75 ERR("Failed to format index directory path");
3ff5c5db 76 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
d2956687
JG
77 goto error;
78 }
79
80 ret = utils_stream_file_path(index_directory_path, stream_name,
c35f9726 81 stream_file_size, stream_file_index,
d2956687
JG
82 DEFAULT_INDEX_FILE_SUFFIX,
83 index_file_path, sizeof(index_file_path));
84 if (ret) {
3ff5c5db 85 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
d2956687
JG
86 goto error;
87 }
88
89 if (unlink_existing_file) {
90 /*
91 * For tracefile rotation. We need to unlink the old
92 * file if present to synchronize with the tail of the
93 * live viewer which could be working on this same file.
94 * By doing so, any reference to the old index file
95 * stays valid even if we re-create a new file with the
96 * same name afterwards.
97 */
348a81dc
JG
98 chunk_status = lttng_trace_chunk_unlink_file(
99 chunk, index_file_path);
100 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK &&
101 !(chunk_status == LTTNG_TRACE_CHUNK_STATUS_ERROR &&
102 errno == ENOENT)) {
d2956687
JG
103 goto error;
104 }
348a81dc 105 }
d2956687 106
8bb66c3c
JG
107 chunk_status = lttng_trace_chunk_open_fs_handle(chunk, index_file_path,
108 flags, mode, &fs_handle, expect_no_file);
d2956687
JG
109 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
110 goto error;
111 }
112
84546278
MD
113 if (flags == WRITE_FILE_FLAGS) {
114 ctf_packet_index_file_hdr_init(&hdr, index_major, index_minor);
8bb66c3c 115 size_ret = fs_handle_write(fs_handle, &hdr, sizeof(hdr));
84546278
MD
116 if (size_ret < sizeof(hdr)) {
117 PERROR("Failed to write index header");
3ff5c5db 118 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
84546278
MD
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
8bb66c3c 125 size_ret = fs_handle_read(fs_handle, &hdr, sizeof(hdr));
84546278
MD
126 if (size_ret < 0) {
127 PERROR("Failed to read index header");
3ff5c5db 128 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
84546278
MD
129 goto error;
130 }
131 if (be32toh(hdr.magic) != CTF_INDEX_MAGIC) {
132 ERR("Invalid header magic");
3ff5c5db 133 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
84546278
MD
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), index_major);
3ff5c5db 139 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
84546278
MD
140 goto error;
141 }
142 if (index_minor != be32toh(hdr.index_minor)) {
143 ERR("Index minor number mismatch: %u, expect %u",
144 be32toh(hdr.index_minor), index_minor);
3ff5c5db 145 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
84546278
MD
146 goto error;
147 }
148 element_len = be32toh(hdr.packet_index_len);
149 if (element_len > sizeof(struct ctf_packet_index)) {
150 ERR("Index element length too long");
3ff5c5db 151 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
84546278
MD
152 goto error;
153 }
154 index_file->element_len = element_len;
d2956687 155 }
8bb66c3c 156 index_file->file = fs_handle;
d2956687
JG
157 index_file->major = index_major;
158 index_file->minor = index_minor;
d2956687
JG
159 urcu_ref_init(&index_file->ref);
160
3ff5c5db
MD
161 *file = index_file;
162 return LTTNG_TRACE_CHUNK_STATUS_OK;
d2956687
JG
163
164error:
8bb66c3c
JG
165 if (fs_handle) {
166 ret = fs_handle_close(fs_handle);
d2956687
JG
167 if (ret < 0) {
168 PERROR("Failed to close file descriptor of index file");
169 }
170 }
307db950 171 lttng_trace_chunk_put(chunk);
d2956687 172 free(index_file);
3ff5c5db 173 return chunk_status;
d2956687
JG
174}
175
3ff5c5db 176enum lttng_trace_chunk_status lttng_index_file_create_from_trace_chunk(
ebb29c10
JG
177 struct lttng_trace_chunk *chunk,
178 const char *channel_path, const char *stream_name,
179 uint64_t stream_file_size, uint64_t stream_file_index,
180 uint32_t index_major, uint32_t index_minor,
3ff5c5db 181 bool unlink_existing_file, struct lttng_index_file **file)
ebb29c10
JG
182{
183 return _lttng_index_file_create_from_trace_chunk(chunk, channel_path,
184 stream_name, stream_file_size, stream_file_index,
185 index_major, index_minor, unlink_existing_file,
3ff5c5db 186 WRITE_FILE_FLAGS, false, file);
ebb29c10
JG
187}
188
3ff5c5db 189enum lttng_trace_chunk_status lttng_index_file_create_from_trace_chunk_read_only(
ebb29c10
JG
190 struct lttng_trace_chunk *chunk,
191 const char *channel_path, const char *stream_name,
192 uint64_t stream_file_size, uint64_t stream_file_index,
3ff5c5db
MD
193 uint32_t index_major, uint32_t index_minor,
194 bool expect_no_file, struct lttng_index_file **file)
ebb29c10
JG
195{
196 return _lttng_index_file_create_from_trace_chunk(chunk, channel_path,
197 stream_name, stream_file_size, stream_file_index,
198 index_major, index_minor, false,
3ff5c5db 199 READ_ONLY_FILE_FLAGS, expect_no_file, file);
ebb29c10
JG
200}
201
309167d2 202/*
f8f3885c 203 * Write index values to the given index file.
309167d2 204 *
f8f3885c 205 * Return 0 on success, -1 on error.
309167d2 206 */
f8f3885c
MD
207int lttng_index_file_write(const struct lttng_index_file *index_file,
208 const struct ctf_packet_index *element)
309167d2 209{
6cd525e8 210 ssize_t ret;
8bb66c3c 211 const size_t len = index_file->element_len;;
309167d2 212
0b0ac4a9 213 assert(index_file);
f8f3885c 214 assert(element);
309167d2 215
8bb66c3c 216 if (!index_file->file) {
183f6fa2
DG
217 goto error;
218 }
219
8bb66c3c 220 ret = fs_handle_write(index_file->file, element, len);
6cd525e8 221 if (ret < len) {
309167d2 222 PERROR("writing index file");
f8f3885c
MD
223 goto error;
224 }
225 return 0;
226
227error:
228 return -1;
229}
230
231/*
232 * Read index values from the given index file.
233 *
234 * Return 0 on success, -1 on error.
235 */
236int lttng_index_file_read(const struct lttng_index_file *index_file,
237 struct ctf_packet_index *element)
238{
239 ssize_t ret;
8bb66c3c 240 const size_t len = index_file->element_len;
f8f3885c
MD
241
242 assert(element);
243
8bb66c3c 244 if (!index_file->file) {
f8f3885c
MD
245 goto error;
246 }
247
8bb66c3c 248 ret = fs_handle_read(index_file->file, element, len);
2239b15a 249 if (ret < 0) {
f8f3885c
MD
250 PERROR("read index file");
251 goto error;
309167d2 252 }
2239b15a
MD
253 if (ret < len) {
254 ERR("lttng_read expected %zu, returned %zd", len, ret);
255 goto error;
256 }
f8f3885c 257 return 0;
309167d2 258
183f6fa2 259error:
f8f3885c 260 return -1;
309167d2 261}
2f8f53af 262
f8f3885c
MD
263void lttng_index_file_get(struct lttng_index_file *index_file)
264{
265 urcu_ref_get(&index_file->ref);
266}
267
268static void lttng_index_file_release(struct urcu_ref *ref)
269{
270 struct lttng_index_file *index_file = caa_container_of(ref,
271 struct lttng_index_file, ref);
272
8bb66c3c 273 if (fs_handle_close(index_file->file)) {
f8f3885c
MD
274 PERROR("close index fd");
275 }
c35f9726 276 lttng_trace_chunk_put(index_file->trace_chunk);
f8f3885c
MD
277 free(index_file);
278}
279
280void lttng_index_file_put(struct lttng_index_file *index_file)
281{
282 urcu_ref_put(&index_file->ref, lttng_index_file_release);
2f8f53af 283}
This page took 0.054599 seconds and 4 git commands to generate.