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