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>
6 * SPDX-License-Identifier: GPL-2.0-only
12 #include <sys/types.h>
15 #include <lttng/constant.h>
16 #include <common/common.hpp>
17 #include <common/defaults.hpp>
18 #include <common/compat/endian.hpp>
19 #include <common/utils.hpp>
23 #define WRITE_FILE_FLAGS (O_WRONLY | O_CREAT | O_TRUNC)
24 #define READ_ONLY_FILE_FLAGS O_RDONLY
26 static enum lttng_trace_chunk_status
_lttng_index_file_create_from_trace_chunk(
27 struct lttng_trace_chunk
*chunk
,
28 const char *channel_path
, const char *stream_name
,
29 uint64_t stream_file_size
, uint64_t stream_file_index
,
30 uint32_t index_major
, uint32_t index_minor
,
31 bool unlink_existing_file
,
32 int flags
, bool expect_no_file
, struct lttng_index_file
**file
)
34 struct lttng_index_file
*index_file
;
35 enum lttng_trace_chunk_status chunk_status
;
37 struct fs_handle
*fs_handle
= NULL
;
39 struct ctf_packet_index_file_hdr hdr
;
40 char index_directory_path
[LTTNG_PATH_MAX
];
41 char index_file_path
[LTTNG_PATH_MAX
];
42 const mode_t mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
43 const bool acquired_reference
= lttng_trace_chunk_get(chunk
);
44 const char *separator
;
46 LTTNG_ASSERT(acquired_reference
);
48 index_file
= zmalloc
<lttng_index_file
>();
50 PERROR("Failed to allocate lttng_index_file");
51 chunk_status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
55 index_file
->trace_chunk
= chunk
;
56 if (channel_path
[0] == '\0') {
61 ret
= snprintf(index_directory_path
, sizeof(index_directory_path
),
62 "%s%s" DEFAULT_INDEX_DIR
, channel_path
, separator
);
63 if (ret
< 0 || ret
>= sizeof(index_directory_path
)) {
64 ERR("Failed to format index directory path");
65 chunk_status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
69 ret
= utils_stream_file_path(index_directory_path
, stream_name
,
70 stream_file_size
, stream_file_index
,
71 DEFAULT_INDEX_FILE_SUFFIX
,
72 index_file_path
, sizeof(index_file_path
));
74 chunk_status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
78 if (unlink_existing_file
) {
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.
87 chunk_status
= (lttng_trace_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
&&
96 chunk_status
= lttng_trace_chunk_open_fs_handle(chunk
, index_file_path
,
97 flags
, mode
, &fs_handle
, expect_no_file
);
98 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
102 if (flags
== WRITE_FILE_FLAGS
) {
103 ctf_packet_index_file_hdr_init(&hdr
, index_major
, index_minor
);
104 size_ret
= fs_handle_write(fs_handle
, &hdr
, sizeof(hdr
));
105 if (size_ret
< sizeof(hdr
)) {
106 PERROR("Failed to write index header");
107 chunk_status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
110 index_file
->element_len
= ctf_packet_index_len(index_major
, index_minor
);
112 uint32_t element_len
;
114 size_ret
= fs_handle_read(fs_handle
, &hdr
, sizeof(hdr
));
116 PERROR("Failed to read index header");
117 chunk_status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
120 if (be32toh(hdr
.magic
) != CTF_INDEX_MAGIC
) {
121 ERR("Invalid header magic");
122 chunk_status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
125 if (index_major
!= be32toh(hdr
.index_major
)) {
126 ERR("Index major number mismatch: %u, expect %u",
127 be32toh(hdr
.index_major
), index_major
);
128 chunk_status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
131 if (index_minor
!= be32toh(hdr
.index_minor
)) {
132 ERR("Index minor number mismatch: %u, expect %u",
133 be32toh(hdr
.index_minor
), index_minor
);
134 chunk_status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
137 element_len
= be32toh(hdr
.packet_index_len
);
138 if (element_len
> sizeof(struct ctf_packet_index
)) {
139 ERR("Index element length too long");
140 chunk_status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
143 index_file
->element_len
= element_len
;
145 index_file
->file
= fs_handle
;
146 index_file
->major
= index_major
;
147 index_file
->minor
= index_minor
;
148 urcu_ref_init(&index_file
->ref
);
151 return LTTNG_TRACE_CHUNK_STATUS_OK
;
155 ret
= fs_handle_close(fs_handle
);
157 PERROR("Failed to close file descriptor of index file");
160 lttng_trace_chunk_put(chunk
);
165 enum lttng_trace_chunk_status
lttng_index_file_create_from_trace_chunk(
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
,
170 bool unlink_existing_file
, struct lttng_index_file
**file
)
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
,
175 WRITE_FILE_FLAGS
, false, file
);
178 enum lttng_trace_chunk_status
lttng_index_file_create_from_trace_chunk_read_only(
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
,
182 uint32_t index_major
, uint32_t index_minor
,
183 bool expect_no_file
, struct lttng_index_file
**file
)
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,
188 READ_ONLY_FILE_FLAGS
, expect_no_file
, file
);
192 * Write index values to the given index file.
194 * Return 0 on success, -1 on error.
196 int lttng_index_file_write(const struct lttng_index_file
*index_file
,
197 const struct ctf_packet_index
*element
)
200 const size_t len
= index_file
->element_len
;;
202 LTTNG_ASSERT(index_file
);
203 LTTNG_ASSERT(element
);
205 if (!index_file
->file
) {
209 ret
= fs_handle_write(index_file
->file
, element
, len
);
211 PERROR("writing index file");
221 * Read index values from the given index file.
223 * Return 0 on success, -1 on error.
225 int lttng_index_file_read(const struct lttng_index_file
*index_file
,
226 struct ctf_packet_index
*element
)
229 const size_t len
= index_file
->element_len
;
231 LTTNG_ASSERT(element
);
233 if (!index_file
->file
) {
237 ret
= fs_handle_read(index_file
->file
, element
, len
);
239 PERROR("read index file");
243 ERR("lttng_read expected %zu, returned %zd", len
, ret
);
252 void lttng_index_file_get(struct lttng_index_file
*index_file
)
254 urcu_ref_get(&index_file
->ref
);
257 static void lttng_index_file_release(struct urcu_ref
*ref
)
259 struct lttng_index_file
*index_file
= caa_container_of(ref
,
260 struct lttng_index_file
, ref
);
262 if (fs_handle_close(index_file
->file
)) {
263 PERROR("close index fd");
265 lttng_trace_chunk_put(index_file
->trace_chunk
);
269 void lttng_index_file_put(struct lttng_index_file
*index_file
)
271 urcu_ref_put(&index_file
->ref
, lttng_index_file_release
);