Debian Codegraph @

storage/innobase/log/log0files_finder.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 2019, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /**************************************************/ /**
29 @file log/log0files_finder.cc
30
31 Redo log - finding log files and inspecting their meta data.
32
33 *******************************************************/
34
35 #include <string>
36
37 /* Log_files_find_result */
38 #include "log0files_finder.h"
39
40 /* log_collect_existing_files, ... */
41 #include "log0files_io.h"
42
43 /* log_pre_8_0_30::files_validate_format */
44 #include "log0pre_8_0_30.h"
45
46 /* Log_file_id, constants */
47 #include "log0types.h"
48
49 /* os_offset_t */
50 #include "os0file.h"
51
52 /**************************************************/ /**
53
54 @name Log - multiple files analysis
55
56 *******************************************************/
57
58 /** @{ */
59
60 /** Validates that all redo files have the same format and the format
61 is in range [Log_format::VERSION_8_0_30, Log_format::CURRENT].
62 @param[in] files_ctx context within which files exist
63 @param[in] files non-empty list of file headers of existing log
64 files, ordered by file_id
65 @param[out] format discovered format if true was returned
66 @return true iff all files have the same format and the format is neither
67 older than Log_format::VERSION_8_0_30 nor newer than Log_format::CURRENT */
68 static bool log_files_validate_format(
69 const Log_files_context &files_ctx,
70 const ut::vector<Log_file_id_and_header> &files, Log_format &format) {
71 ut_a(!files.empty());
72 const uint32_t first_file_format_int = files.front().m_header.m_format;
73 for (const auto &file : files) {
74 const uint32_t curr_format_int = file.m_header.m_format;
75 if (curr_format_int > to_int(Log_format::CURRENT)) {
76 const auto file_path = log_file_path(files_ctx, file.m_id);
77 ib::error(ER_IB_MSG_LOG_FILE_FORMAT_UNKNOWN, ulong{curr_format_int},
78 file_path.c_str(), REFMAN "upgrading.html");
79 return false;
80 }
81 if (curr_format_int < to_int(Log_format::VERSION_8_0_30)) {
82 /* Format of redo file is too old for the configured ruleset. */
83 const auto file_path = log_file_path(files_ctx, file.m_id);
84 ib::error(ER_IB_MSG_LOG_FILE_FORMAT_TOO_OLD, file_path.c_str(),
85 ulong{curr_format_int});
86 return false;
87 }
88 if (curr_format_int != first_file_format_int) {
89 /* Two existing redo files have different format. */
90 const auto first_file_path = log_file_path(files_ctx, files.front().m_id);
91 const auto other_file_path = log_file_path(files_ctx, file.m_id);
92 ib::error(ER_IB_MSG_LOG_FILE_DIFFERENT_FORMATS, first_file_path.c_str(),
93 ulong{first_file_format_int}, other_file_path.c_str(),
94 ulong{curr_format_int});
95 return false;
96 }
97 }
98 format = static_cast<Log_format>(first_file_format_int);
99 return true;
100 }
101
102 /** Validates that all log files have start_lsn % OS_FILE_LOG_BLOCK_SIZE == 0,
103 @param[in] files_ctx context within which files exist
104 @param[in] file_headers non-empty list of file headers of existing log files,
105 ordered by file_id
106 @return true if all files have possibly proper value of start_lsn */
107 static bool log_files_validate_start_lsn(
108 const Log_files_context &files_ctx,
109 const ut::vector<Log_file_id_and_header> &file_headers) {
110 for (const auto &file : file_headers) {
111 if (file.m_header.m_start_lsn % OS_FILE_LOG_BLOCK_SIZE != 0) {
112 const auto file_path = log_file_path(files_ctx, file.m_id);
113 ib::error(ER_IB_MSG_LOG_FILE_INVALID_START_LSN, file_path.c_str(),
114 ulonglong{file.m_header.m_start_lsn});
115 return false;
116 }
117 }
118 return true;
119 }
120
121 /** Validates that all log files create a chain of consecutive lsn ranges.
122 @param[in] files_ctx context within which files exist
123 @param[in] file_sizes non-empty list of file sizes of existing log files,
124 ordered by file_id
125 @param[in] file_headers non-empty list of file headers of existing log files,
126 ordered by file_id
127 @return true iff lsn ranges seem to be consistent and form a single chain */
128 static bool log_files_validate_lsn_chain(
129 const Log_files_context &files_ctx,
130 const ut::vector<Log_file_id_and_size> &file_sizes,
131 const ut::vector<Log_file_id_and_header> &file_headers) {
132 ut_a(!file_sizes.empty());
133 ut_a(file_sizes.size() == file_headers.size());
134
135 /* Start at min_start_lsn and traverse all redo files.
136 Check that file's end_lsn is equal to start_lsn of the next file. */
137 lsn_t expected_start_lsn = file_headers.front().m_header.m_start_lsn;
138 Log_file_id expected_file_id = file_headers.front().m_id;
139 for (size_t i = 0; i < file_headers.size(); ++i) {
140 const auto &file = file_headers[i];
141 ut_ad(file_sizes[i].m_id == file.m_id);
142
143 if (file.m_id != expected_file_id) {
144 /* We are missing file with id = expected_file_id. */
145 const auto file_path = log_file_path(files_ctx, expected_file_id);
146 ib::error(ER_IB_MSG_LOG_FILE_MISSING_FOR_ID, file_path.c_str(),
147 ulonglong{expected_start_lsn});
148 return false;
149 }
150
151 if (file.m_header.m_start_lsn != expected_start_lsn) {
152 const auto file_path = log_file_path(files_ctx, expected_file_id);
153 ib::error(ER_IB_MSG_LOG_FILE_INVALID_LSN_RANGES, file_path.c_str(),
154 ulonglong{file.m_header.m_start_lsn},
155 ulonglong{expected_start_lsn});
156 return false;
157 }
158
159 const bool ret = log_file_compute_end_lsn(file.m_header.m_start_lsn,
160 file_sizes[i].m_size_in_bytes,
161 expected_start_lsn);
162 ut_a(ret);
163
164 expected_file_id = Log_file::next_id(expected_file_id);
165 }
166 return true;
167 }
168
169 /** Validates if all existing redo log files have the same log_uuid.
170 @param[in] files_ctx context within which files exist
171 @param[in] files non-empty list of file headers of existing log files,
172 ordered by file_id
173 @param[out] log_uuid the log_uuid that has been discovered
174 @return true iff all existing log files had the same log_uuid value */
175 static bool log_files_validate_log_uuid(
176 const Log_files_context &files_ctx,
177 const ut::vector<Log_file_id_and_header> &files, Log_uuid &log_uuid) {
178 ut_a(!files.empty());
179 log_uuid = files.front().m_header.m_log_uuid;
180 for (const auto &file : files) {
181 if (log_uuid != file.m_header.m_log_uuid) {
182 const std::string file_path = log_file_path(files_ctx, file.m_id);
183 const std::string first_file_path =
184 log_file_path(files_ctx, files.front().m_id);
185 ib::error(ER_IB_MSG_LOG_FILE_FOREIGN_UUID, file_path.c_str(),
186 first_file_path.c_str());
187 return false;
188 }
189 }
190 return true;
191 }
192
193 /** Validates if a set of redo log files consists of files of equal size.
194 This is used for files with older redo format.
195 @param[in] files_ctx context within which files exist
196 @param[in] files non-empty list of file headers of existing log files,
197 ordered by file_id
198 @return true iff all redo log files have the same size */
199 static bool log_files_validate_file_sizes_equal(
200 const Log_files_context &files_ctx,
201 const ut::vector<Log_file_id_and_size> &files) {
202 ut_a(!files.empty());
203 const os_offset_t first_file_size = files.front().m_size_in_bytes;
204 for (const auto &file : files) {
205 if (file.m_size_in_bytes != first_file_size) {
206 const auto file_path = log_file_path(files_ctx, file.m_id);
207 ib::error(ER_IB_MSG_LOG_FILES_DIFFERENT_SIZES, file_path.c_str(),
208 ulonglong{file.m_size_in_bytes}, ulonglong{first_file_size});
209 return false;
210 }
211 }
212 return true;
213 }
214
215 /** @} */
216
217 /**************************************************/ /**
218
219 @name Log - files finder
220
221 *******************************************************/
222
223 /** @{ */
224
225 Log_files_find_result log_files_find_and_analyze(
226 bool read_only, Encryption_metadata &encryption_metadata,
227 Log_files_dict &files, Log_format &format, std::string &creator_name,
228 Log_flags &log_flags, Log_uuid &log_uuid) {
229 ut::vector<Log_file_id_and_size> file_sizes;
230
231 switch (log_collect_existing_files(files.ctx(), read_only, file_sizes)) {
232 case DB_NOT_FOUND:
233 return Log_files_find_result::FOUND_NO_FILES;
234 case DB_SUCCESS:
235 ut_a(!file_sizes.empty());
236 break;
237 case DB_ERROR:
238 /* Error message emitted in log_collect_existing_files. */
239 return Log_files_find_result::SYSTEM_ERROR;
240 default:
241 ut_error;
242 }
243
244 /* Read headers of all log files. */
245 ut::vector<Log_file_id_and_header> file_headers;
246 for (const auto &file : file_sizes) {
247 Log_file_header file_header;
248
249 /* Redo log file header is never encrypted. */
250 Encryption_metadata unused_encryption_metadata;
251
252 auto file_handle =
253 Log_file::open(files.ctx(), file.m_id, Log_file_access_mode::READ_ONLY,
254 unused_encryption_metadata, Log_file_type::NORMAL);
255
256 /* The file can be opened - checked by
257 the log_collect_existing_files() */
258 if (!file_handle.is_open()) {
259 return Log_files_find_result::SYSTEM_ERROR;
260 }
261
262 if (log_file_header_read(file_handle, file_header) != DB_SUCCESS) {
263 ib::error(ER_IB_MSG_LOG_FILE_HEADER_READ_FAILED,
264 file_handle.file_path().c_str());
265 return Log_files_find_result::SYSTEM_ERROR;
266 }
267 file_headers.emplace_back(file.m_id, file_header);
268 }
269
270 /* Read properties global to the whole set of redo log files:
271 - format,
272 - creator_name,
273 - log_flags,
274 - log_uuid. */
275
276 if (files.ctx().m_files_ruleset > Log_files_ruleset::PRE_8_0_30) {
277 const auto &newest_file_header = file_headers.back().m_header;
278 log_flags = newest_file_header.m_log_flags;
279 creator_name = newest_file_header.m_creator_name;
280 format = Log_format::LEGACY;
281
282 if (!log_files_validate_format(files.ctx(), file_headers, format)) {
283 /* Error message emitted in log_files_validate_format. */
284 return Log_files_find_result::FOUND_CORRUPTED_FILES;
285 }
286 ut_a(format >= Log_format::VERSION_8_0_30);
287
288 if (!log_files_validate_log_uuid(files.ctx(), file_headers, log_uuid)) {
289 /* Error message emitted in log_files_validate_log_uuid. */
290 return Log_files_find_result::FOUND_CORRUPTED_FILES;
291 }
292 if (!log_files_validate_start_lsn(files.ctx(), file_headers)) {
293 /* Error message emitted in log_files_validate_start_lsn. */
294 return Log_files_find_result::FOUND_CORRUPTED_FILES;
295 }
296 if (!log_files_validate_lsn_chain(files.ctx(), file_sizes, file_headers)) {
297 /* Error message emitted in log_files_validate_lsn_chain. */
298 return Log_files_find_result::FOUND_CORRUPTED_FILES;
299 }
300 } else {
301 const auto &file0_header = file_headers.front().m_header;
302 creator_name = file0_header.m_creator_name;
303 log_flags = file0_header.m_log_flags;
304 log_uuid = Log_uuid{};
305 format = Log_format::LEGACY;
306
307 if (!log_pre_8_0_30::files_validate_format(files.ctx(), file_headers,
308 format)) {
309 /* Error message emitted in log_pre_8_0_30::files_validate_format. */
310 return Log_files_find_result::FOUND_CORRUPTED_FILES;
311 }
312 ut_a(format < Log_format::VERSION_8_0_30);
313 ut_a(file_sizes.front().m_id == 0);
314
315 if (file_sizes.size() < 2) {
316 /* Must have at least 2 log files. */
317 ib::error(ER_IB_MSG_LOG_FILES_INVALID_SET);
318 return Log_files_find_result::FOUND_CORRUPTED_FILES;
319 }
320 if (!log_files_validate_file_sizes_equal(files.ctx(), file_sizes)) {
321 /* Error message emitted in log_files_validate_file_sizes_equal. */
322 return Log_files_find_result::FOUND_CORRUPTED_FILES;
323 }
324 }
325
326 if (Log_format::VERSION_8_0_19 <= format) {
327 /* Check if log files have been initialized. */
328 if (log_file_header_check_flag(log_flags,
329 LOG_HEADER_FLAG_NOT_INITIALIZED)) {
330 if (format == Log_format::CURRENT) {
331 return Log_files_find_result::FOUND_UNINITIALIZED_FILES;
332 } else {
333 ib::error(ER_IB_MSG_LOG_UPGRADE_UNINITIALIZED_FILES,
334 ulong{to_int(format)});
335 return Log_files_find_result::FOUND_CORRUPTED_FILES;
336 }
337 }
338
339 /* Exit if server is crashed while running without redo logging. */
340 if (log_file_header_check_flag(log_flags, LOG_HEADER_FLAG_CRASH_UNSAFE)) {
341 /* As of today, the only scenario which leads us here is that
342 log_persist_disable() was called and then we crashed. If we
343 ever introduce more possibilities, then we need to update
344 the error message. */
345 ut_ad(log_file_header_check_flag(log_flags, LOG_HEADER_FLAG_NO_LOGGING));
346 ib::error(ER_IB_ERR_RECOVERY_REDO_DISABLED);
347 return Log_files_find_result::FOUND_DISABLED_FILES;
348 }
349 }
350
351 /* The newest log file must not be marked as full. If the existing newest
352 file is marked as such, it means that the real newest log file was lost. */
353 if (log_file_header_check_flag(log_flags, LOG_HEADER_FLAG_FILE_FULL)) {
354 ib::error(ER_IB_MSG_LOG_FILES_FOUND_MISSING);
355 return Log_files_find_result::FOUND_VALID_FILES_BUT_MISSING_NEWEST;
356 }
357
358 lsn_t size_capacity = 0;
359 for (const auto &file : file_sizes) {
360 ut_a(file.m_size_in_bytes >= LOG_FILE_HDR_SIZE);
361 lsn_t file_lsn_capacity;
362 const bool ret = log_file_compute_logical_capacity(file.m_size_in_bytes,
363 file_lsn_capacity);
364 ut_a(ret);
365 size_capacity += file_lsn_capacity;
366 }
367
368 /* Filling dictionary. */
369
370 files.clear();
371 for (size_t i = 0; i < file_sizes.size(); ++i) {
372 ut_a(i == 0 ||
373 Log_file::next_id(file_sizes[i - 1].m_id) == file_sizes[i].m_id);
374 ut_a(file_sizes[i].m_id == file_headers[i].m_id);
375
376 if (format >= Log_format::VERSION_8_0_30) {
377 ut_a(i == 0 || file_headers[i - 1].m_header.m_start_lsn <
378 file_headers[i].m_header.m_start_lsn);
379
380 files.add(file_sizes[i].m_id, file_sizes[i].m_size_in_bytes,
381 file_headers[i].m_header.m_start_lsn,
382 log_file_header_check_flag(file_headers[i].m_header.m_log_flags,
383 LOG_HEADER_FLAG_FILE_FULL),
384 encryption_metadata);
385 } else {
386 files.add(file_sizes[i].m_id, file_sizes[i].m_size_in_bytes, 0, true,
387 encryption_metadata);
388 }
389 }
390
391 /** The size_capacity was computed by iterating files_list, in which we
392 potentially could have two files with the same m_id if some bug was
393 introduced. This should be caught easily by this check. */
394 ut_a(size_capacity == log_files_capacity_of_existing_files(files));
395
396 return Log_files_find_result::FOUND_VALID_FILES;
397 }
398
399 /** @} */