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
13 #include <sys/types.h>
16 #include <lttng/constant.h>
17 #include <common/common.h>
18 #include <common/defaults.h>
19 #include <common/compat/endian.h>
20 #include <common/utils.h>
24 #define WRITE_FILE_FLAGS (O_WRONLY | O_CREAT | O_TRUNC)
25 #define READ_ONLY_FILE_FLAGS O_RDONLY
27 static enum lttng_trace_chunk_status
_lttng_index_file_create_from_trace_chunk(
28 struct lttng_trace_chunk
*chunk
,
29 const char *channel_path
, const char *stream_name
,
30 uint64_t stream_file_size
, uint64_t stream_file_index
,
31 uint32_t index_major
, uint32_t index_minor
,
32 bool unlink_existing_file
,
33 int flags
, bool expect_no_file
, struct lttng_index_file
**file
)
35 struct lttng_index_file
*index_file
;
36 enum lttng_trace_chunk_status chunk_status
;
38 struct fs_handle
*fs_handle
= NULL
;
40 struct ctf_packet_index_file_hdr hdr
;
41 char index_directory_path
[LTTNG_PATH_MAX
];
42 char index_file_path
[LTTNG_PATH_MAX
];
43 const mode_t mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
44 const bool acquired_reference
= lttng_trace_chunk_get(chunk
);
45 const char *separator
;
47 assert(acquired_reference
);
49 index_file
= zmalloc(sizeof(*index_file
));
51 PERROR("Failed to allocate lttng_index_file");
52 chunk_status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
56 index_file
->trace_chunk
= chunk
;
57 if (channel_path
[0] == '\0') {
62 ret
= snprintf(index_directory_path
, sizeof(index_directory_path
),
63 "%s%s" DEFAULT_INDEX_DIR
, channel_path
, separator
);
64 if (ret
< 0 || ret
>= sizeof(index_directory_path
)) {
65 ERR("Failed to format index directory path");
66 chunk_status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
70 ret
= utils_stream_file_path(index_directory_path
, stream_name
,
71 stream_file_size
, stream_file_index
,
72 DEFAULT_INDEX_FILE_SUFFIX
,
73 index_file_path
, sizeof(index_file_path
));
75 chunk_status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
79 if (unlink_existing_file
) {
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.
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
&&
97 chunk_status
= lttng_trace_chunk_open_fs_handle(chunk
, index_file_path
,
98 flags
, mode
, &fs_handle
, expect_no_file
);
99 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
103 if (flags
== WRITE_FILE_FLAGS
) {
104 ctf_packet_index_file_hdr_init(&hdr
, index_major
, index_minor
);
105 size_ret
= fs_handle_write(fs_handle
, &hdr
, sizeof(hdr
));
106 if (size_ret
< sizeof(hdr
)) {
107 PERROR("Failed to write index header");
108 chunk_status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
111 index_file
->element_len
= ctf_packet_index_len(index_major
, index_minor
);
113 uint32_t element_len
;
115 size_ret
= fs_handle_read(fs_handle
, &hdr
, sizeof(hdr
));
117 PERROR("Failed to read index header");
118 chunk_status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
121 if (be32toh(hdr
.magic
) != CTF_INDEX_MAGIC
) {
122 ERR("Invalid header magic");
123 chunk_status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
126 if (index_major
!= be32toh(hdr
.index_major
)) {
127 ERR("Index major number mismatch: %u, expect %u",
128 be32toh(hdr
.index_major
), index_major
);
129 chunk_status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
132 if (index_minor
!= be32toh(hdr
.index_minor
)) {
133 ERR("Index minor number mismatch: %u, expect %u",
134 be32toh(hdr
.index_minor
), index_minor
);
135 chunk_status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
138 element_len
= be32toh(hdr
.packet_index_len
);
139 if (element_len
> sizeof(struct ctf_packet_index
)) {
140 ERR("Index element length too long");
141 chunk_status
= LTTNG_TRACE_CHUNK_STATUS_ERROR
;
144 index_file
->element_len
= element_len
;
146 index_file
->file
= fs_handle
;
147 index_file
->major
= index_major
;
148 index_file
->minor
= index_minor
;
149 urcu_ref_init(&index_file
->ref
);
152 return LTTNG_TRACE_CHUNK_STATUS_OK
;
156 ret
= fs_handle_close(fs_handle
);
158 PERROR("Failed to close file descriptor of index file");
161 lttng_trace_chunk_put(chunk
);
166 enum lttng_trace_chunk_status
lttng_index_file_create_from_trace_chunk(
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
,
171 bool unlink_existing_file
, struct lttng_index_file
**file
)
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
,
176 WRITE_FILE_FLAGS
, false, file
);
179 enum lttng_trace_chunk_status
lttng_index_file_create_from_trace_chunk_read_only(
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
,
183 uint32_t index_major
, uint32_t index_minor
,
184 bool expect_no_file
, struct lttng_index_file
**file
)
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,
189 READ_ONLY_FILE_FLAGS
, expect_no_file
, file
);
193 * Write index values to the given index file.
195 * Return 0 on success, -1 on error.
197 int lttng_index_file_write(const struct lttng_index_file
*index_file
,
198 const struct ctf_packet_index
*element
)
201 const size_t len
= index_file
->element_len
;;
206 if (!index_file
->file
) {
210 ret
= fs_handle_write(index_file
->file
, element
, len
);
212 PERROR("writing index file");
222 * Read index values from the given index file.
224 * Return 0 on success, -1 on error.
226 int lttng_index_file_read(const struct lttng_index_file
*index_file
,
227 struct ctf_packet_index
*element
)
230 const size_t len
= index_file
->element_len
;
234 if (!index_file
->file
) {
238 ret
= fs_handle_read(index_file
->file
, element
, len
);
240 PERROR("read index file");
244 ERR("lttng_read expected %zu, returned %zd", len
, ret
);
253 void lttng_index_file_get(struct lttng_index_file
*index_file
)
255 urcu_ref_get(&index_file
->ref
);
258 static void lttng_index_file_release(struct urcu_ref
*ref
)
260 struct lttng_index_file
*index_file
= caa_container_of(ref
,
261 struct lttng_index_file
, ref
);
263 if (fs_handle_close(index_file
->file
)) {
264 PERROR("close index fd");
266 lttng_trace_chunk_put(index_file
->trace_chunk
);
270 void lttng_index_file_put(struct lttng_index_file
*index_file
)
272 urcu_ref_put(&index_file
->ref
, lttng_index_file_release
);