Debian Codegraph @

storage/innobase/log/log0files_io.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_io.cc
30
31 *******************************************************/
32
33 #ifndef WIN32
34 #include <fcntl.h>
35 #include <unistd.h>
36 #else
37 #include <windows.h>
38 #endif /* WIN32 */
39
40 /* std::sort */
41 #include <algorithm>
42
43 /* strncpy */
44 #include <cstring>
45
46 /* std::numeric_limits */
47 #include <limits>
48
49 /* std::ostringstream */
50 #include <sstream>
51
52 /* log_block_set_checksum, ... */
53 #include "log0files_io.h"
54
55 /* log_pre_8_0_30::FILE_BASE_NAME, ... */
56 #include "log0pre_8_0_30.h"
57
58 /* LOG_HEADER_FORMAT, ... */
59 #include "log0types.h"
60
61 /* mach_write_to_4, ... */
62 #include "mach0data.h"
63
64 /* DBUG_EXECUTE_IF, ... */
65 #include "my_dbug.h"
66
67 /* os_file_scan_directory */
68 #include "os0file.h"
69
70 /* ut::vector */
71 #include "ut0new.h"
72
73 /* ut::random_from_interval */
74 #include "ut0rnd.h"
75
76 /* srv_redo_log_encrypt */
77 #include "srv0srv.h"
78
79 Log_checksum_algorithm_atomic_ptr log_checksum_algorithm_ptr;
80
81 bool log_header_checksum_is_ok(const byte *buf) {
82 DBUG_EXECUTE_IF("log_header_checksum_disabled", return true;);
83 return log_block_get_checksum(buf) == log_block_calc_checksum_crc32(buf);
84 }
85
86 /** Used for reads/writes to redo files within this module.
87 Note that OS_FILE_LOG_BLOCK_SIZE can be smaller than a sector size,
88 and indeed redo file is being accessed at offsets and sizes not aligned
89 to sector size, which is fine, as it always uses buffered IO. */
90 struct Log_file_block {
91 byte data[OS_FILE_LOG_BLOCK_SIZE];
92 };
93
94 /** Asserts that the provided file header seems correct.
95 @param[in] header file header data to validate */
96 static void log_file_header_validate(const Log_file_header &header);
97
98 /** Computes offset from the beginning of the redo file to the checkpoint
99 header for provided checkpoint header number.
100 @param[in] checkpoint_header_no checkpoint header for which offset to compute
101 @return offset from the beginning of the redo file */
102 static os_offset_t log_checkpoint_header_offset(
103 Log_checkpoint_header_no checkpoint_header_no);
104
105 /** Asserts that the provided <offset, size> defines one or more redo
106 data blocks within a log file.
107 @param[in] offset expected offset to the beginning of data block(s)
108 @param[in] size size in bytes of data blocks starting from the offset */
109 static void log_data_blocks_validate(os_offset_t offset, os_offset_t size);
110
111 /** Converts flag number to the mask with this flag turned on.
112 @param[in] bit flag bit number (but > 0)
113 @return bit mask value (2^(bit-1)) */
114 static Log_flags log_file_header_flag_bit(uint32_t bit);
115
116 /** Renames the log file.
117 @param[in] context redo log files context
118 @param[in] old_file_path path to the existing log file to rename
119 @param[in] new_file_path path to the renamed file
120 @param[in] err_msg_id id of the error message to print (if needed)
121 @return DB_SUCCESS or DB_ERROR */
122 static dberr_t log_rename_file_low(const Log_files_context &context
123 [[maybe_unused]],
124 const std::string &old_file_path,
125 const std::string &new_file_path,
126 int err_msg_id);
127
128 /** Delete the log file at the provided file path. Asserts that the
129 file has been deleted or does not exist.
130 @param[in] context redo log files context
131 @param[in] file_path path to the log file that should be removed
132 @param[in] err_msg_id id of the error message to print (if needed)
133 @return DB_SUCCESS, DB_NOT_FOUND or DB_ERROR */
134 static dberr_t log_remove_file_low(const Log_files_context &context,
135 const std::string &file_path,
136 int err_msg_id);
137
138 /** Resizes the log file at the provided file path.
139 @param[in] file_path path to the log file that should be resized
140 @param[in] size_in_bytes size to which resize, in bytes
141 @param[in] err_msg_id id of the error message to print (if needed)
142 @return DB_SUCCESS, DB_NOT_FOUND, DB_OUT_OF_DISK_SPACE or DB_ERROR */
143 static dberr_t log_resize_file_low(const std::string &file_path,
144 os_offset_t size_in_bytes, int err_msg_id);
145
146 /** Checks if a log file exists and can be opened with srv_read_only_mode mode.
147 If that is successful, reads the size of the file and provides it.
148 @param[in] ctx redo log files context
149 @param[in] file_id id of the file to check and open
150 @param[in] read_only true: check file permissions only for reading
151 false: check for both reading and writing
152 @param[out] size_in_bytes size of the file, in bytes
153 @return DB_SUCCESS, DB_NOT_FOUND or DB_ERROR */
154 static dberr_t log_check_file(const Log_files_context &ctx, Log_file_id file_id,
155 bool read_only, os_offset_t &size_in_bytes);
156
157 /**************************************************/ /**
158
159 @name Log_file_handle implementation
160
161 *******************************************************/
162
163 /** @{ */
164
165 Log_file_io_callback Log_file_handle::s_on_before_read;
166 Log_file_io_callback Log_file_handle::s_on_before_write;
167 bool Log_file_handle::s_skip_fsyncs = false;
168
169 #ifdef UNIV_DEBUG
170 std::atomic<size_t> Log_file_handle::s_n_open{0};
171 #endif /* UNIV_DEBUG */
172
173 std::atomic<uint64_t> Log_file_handle::s_total_fsyncs{0};
174 std::atomic<uint64_t> Log_file_handle::s_fsyncs_in_progress{0};
175
176 Log_file_handle::Log_file_handle(Encryption_metadata &encryption_metadata)
177 : m_file_id{},
178 m_access_mode{},
179 m_encryption_metadata{encryption_metadata},
180 m_file_type{},
181 m_is_open{},
182 m_is_modified{},
183 m_file_path{},
184 m_raw_handle{},
185 m_block_size{},
186 m_file_size{} {}
187
188 Log_file_handle::Log_file_handle(Log_file_handle &&other)
189 : m_file_id{other.m_file_id},
190 m_access_mode{other.m_access_mode},
191 m_encryption_metadata{other.m_encryption_metadata},
192 m_file_type{other.m_file_type},
193 m_is_open{other.m_is_open},
194 m_is_modified{other.m_is_modified},
195 m_file_path{other.m_file_path},
196 m_raw_handle{other.m_raw_handle},
197 m_block_size{other.m_block_size},
198 m_file_size{other.m_file_size} {
199 other.m_is_modified = false;
200 other.m_is_open = false;
201 other.m_raw_handle = {};
202 }
203
204 Log_file_handle &Log_file_handle::operator=(Log_file_handle &&rhs) {
205 if (m_is_open) {
206 close();
207 }
208
209 m_file_id = rhs.m_file_id;
210 m_access_mode = rhs.m_access_mode;
211 m_file_type = rhs.m_file_type;
212 m_file_path = rhs.m_file_path;
213 m_block_size = rhs.m_block_size;
214 m_file_size = rhs.m_file_size;
215 ut_a(&m_encryption_metadata == &rhs.m_encryption_metadata);
216
217 m_is_modified = rhs.m_is_modified;
218 rhs.m_is_modified = false;
219 m_is_open = rhs.m_is_open;
220 rhs.m_is_open = false;
221 m_raw_handle = rhs.m_raw_handle;
222 rhs.m_raw_handle = {};
223 return *this;
224 }
225
226 Log_file_handle::Log_file_handle(const Log_files_context &ctx, Log_file_id id,
227 Log_file_access_mode access_mode,
228 Encryption_metadata &encryption_metadata,
229 Log_file_type file_type)
230 : m_file_id(id),
231 m_access_mode(access_mode),
232 m_encryption_metadata(encryption_metadata),
233 m_file_type(file_type),
234 m_is_open(false),
235 m_is_modified(false),
236 m_file_path(file_type == Log_file_type::UNUSED
237 ? log_file_path_for_unused_file(ctx, id)
238 : log_file_path(ctx, id)) {
239 const dberr_t err = open();
240 if (err != DB_SUCCESS) {
241 ut_a(!m_is_open);
242 ib::error(ER_IB_MSG_LOG_FILE_OPEN_FAILED, m_file_path.c_str(),
243 static_cast<int>(err));
244 }
245 }
246
247 Log_file_handle::~Log_file_handle() {
248 if (m_is_open) {
249 close();
250 }
251 }
252
253 const std::string &Log_file_handle::file_path() const { return m_file_path; }
254
255 dberr_t Log_file_handle::open() {
256 const bool read_only = m_access_mode == Log_file_access_mode::READ_ONLY;
257 os_file_stat_t stat_info;
258 dberr_t err;
259 err = os_file_get_status(m_file_path.c_str(), &stat_info, false, read_only);
260 if (err != DB_SUCCESS) {
261 return err;
262 }
263
264 m_block_size = stat_info.block_size;
265 ut_a(m_block_size > 0);
266
267 m_file_size = stat_info.size;
268
269 ut_ad(s_n_open.fetch_add(1) + 1 <= LOG_MAX_OPEN_FILES);
270
271 m_raw_handle =
272 os_file_create(innodb_log_file_key, m_file_path.c_str(), OS_FILE_OPEN,
273 OS_LOG_FILE, read_only, &m_is_open);
274 if (m_is_open) {
275 return DB_SUCCESS;
276 }
277
278 ut_ad(s_n_open.fetch_sub(1) > 0);
279 return DB_ERROR;
280 }
281
282 void Log_file_handle::close() {
283 ut_ad(is_open());
284 if (m_is_modified) {
285 fsync();
286 m_is_modified = false;
287 }
288 os_file_close(m_raw_handle);
289 m_is_open = false;
290
291 ut_ad(s_n_open.fetch_sub(1) > 0);
292 }
293
294 bool Log_file_handle::is_open() const { return m_is_open; }
295
296 void Log_file_handle::fsync() {
297 ut_ad(is_open());
298 ut_ad(m_access_mode != Log_file_access_mode::READ_ONLY);
299
300 if (s_skip_fsyncs) {
301 return;
302 }
303
304 s_total_fsyncs.fetch_add(1, std::memory_order_relaxed);
305 s_fsyncs_in_progress.fetch_add(1);
306
307 const bool success = os_file_flush(m_raw_handle);
308
309 s_fsyncs_in_progress.fetch_sub(1);
310 ut_a(success);
311 }
312
313 Log_file_id Log_file_handle::file_id() const { return m_file_id; }
314
315 os_offset_t Log_file_handle::file_size() const { return m_file_size; }
316
317 IORequest Log_file_handle::prepare_io_request(int req_type, os_offset_t offset,
318 os_offset_t size,
319 bool can_use_encryption) {
320 ut_a(size > 0);
321 ut_a(size % OS_FILE_LOG_BLOCK_SIZE == 0);
322 ut_a(offset % OS_FILE_LOG_BLOCK_SIZE == 0);
323 ut_a(req_type == IORequest::READ || req_type == IORequest::WRITE);
324 ut_a(m_block_size > 0);
325
326 IORequest io_request{IORequest::LOG | req_type};
327 io_request.block_size(m_block_size);
328
329 // Finally, set up encryption related fields, if needed
330
331 if (!(can_use_encryption && m_encryption_metadata.can_encrypt())) {
332 return io_request; // There is no ecryption involved
333 }
334
335 if (offset + size <= LOG_FILE_HDR_SIZE) {
336 return io_request; // Never use encryption in the header
337 }
338
339 // Assume the whole encrypted region is in the body, none of it in the header
340 ut_a(offset >= LOG_FILE_HDR_SIZE);
341 io_request.get_encryption_info().set(m_encryption_metadata);
342
343 return io_request;
344 }
345
346 dberr_t Log_file_handle::read(os_offset_t read_offset, os_offset_t read_size,
347 byte *buf) {
348 if (!is_open()) return DB_ERROR;
349
350 auto io_request =
351 prepare_io_request(IORequest::READ, read_offset, read_size, true);
352
353 ut_ad(m_access_mode != Log_file_access_mode::WRITE_ONLY);
354
355 if (s_on_before_read) {
356 s_on_before_read(m_file_id, m_file_type, read_offset, read_size);
357 }
358
359 return os_file_read(io_request, m_file_path.c_str(), m_raw_handle, buf,
360 read_offset, static_cast<ulint>(read_size));
361 }
362
363 dberr_t Log_file_handle::write(os_offset_t write_offset, os_offset_t write_size,
364 const byte *buf) {
365 if (!is_open()) return DB_ERROR;
366
367 auto io_request = prepare_io_request(IORequest::WRITE, write_offset,
368 write_size, srv_redo_log_encrypt);
369
370 ut_ad(m_access_mode != Log_file_access_mode::READ_ONLY);
371
372 if (s_on_before_write) {
373 s_on_before_write(m_file_id, m_file_type, write_offset, write_size);
374 }
375
376 m_is_modified = true;
377
378 return os_file_write(io_request, m_file_path.c_str(), m_raw_handle, buf,
379 write_offset, static_cast<ulint>(write_size));
380 }
381
382 Log_file_handle Log_file::open(Log_file_access_mode access_mode) const {
383 return open(m_files_ctx, m_id, access_mode, m_encryption_metadata,
384 Log_file_type::NORMAL);
385 }
386
387 Log_file_handle Log_file::open(const Log_files_context &files_ctx,
388 Log_file_id file_id,
389 Log_file_access_mode access_mode,
390 Encryption_metadata &encryption_metadata,
391 Log_file_type file_type) {
392 return Log_file_handle{files_ctx, file_id, access_mode, encryption_metadata,
393 file_type};
394 }
395
396 Log_file::Log_file(const Log_files_context &files_ctx,
397 Encryption_metadata &encryption_metadata)
398 : m_files_ctx{files_ctx},
399 m_id{},
400 m_consumed{},
401 m_full{},
402 m_size_in_bytes{},
403 m_start_lsn{},
404 m_end_lsn{},
405 m_encryption_metadata{encryption_metadata} {}
406
407 Log_file::Log_file(const Log_files_context &files_ctx, Log_file_id id,
408 bool consumed, bool full, os_offset_t size_in_bytes,
409 lsn_t start_lsn, lsn_t end_lsn,
410 Encryption_metadata &encryption_metadata)
411 : m_files_ctx{files_ctx},
412 m_id{id},
413 m_consumed{consumed},
414 m_full{full},
415 m_size_in_bytes{size_in_bytes},
416 m_start_lsn{start_lsn},
417 m_end_lsn{end_lsn},
418 m_encryption_metadata{encryption_metadata} {}
419
420 Log_file &Log_file::operator=(const Log_file &other) {
421 m_id = other.m_id;
422 m_consumed = other.m_consumed;
423 m_full = other.m_full;
424 m_size_in_bytes = other.m_size_in_bytes;
425 m_start_lsn = other.m_start_lsn;
426 m_end_lsn = other.m_end_lsn;
427 ut_a(&m_files_ctx == &other.m_files_ctx);
428 ut_a(&m_encryption_metadata == &other.m_encryption_metadata);
429 return *this;
430 }
431
432 /** @} */
433
434 /**************************************************/ /**
435
436 @name Log - file header read/write.
437
438 *******************************************************/
439
440 /** @{ */
441
442 static void log_file_header_validate(const Log_file_header &header) {
443 ut_a(header.m_start_lsn >= LOG_START_LSN);
444 ut_a(header.m_start_lsn < LSN_MAX);
445 ut_a(header.m_start_lsn % OS_FILE_LOG_BLOCK_SIZE == 0);
446
447 ut_a(!header.m_creator_name.empty());
448 ut_a(header.m_creator_name.size() <= LOG_HEADER_CREATOR_MAX_LENGTH);
449
450 ut_a(header.m_format <= to_int(Log_format::CURRENT));
451 }
452
453 void log_file_header_serialize(const Log_file_header &header, byte *buf) {
454 log_file_header_validate(header);
455
456 std::memset(buf, 0x00, OS_FILE_LOG_BLOCK_SIZE);
457
458 mach_write_to_4(buf + LOG_HEADER_FORMAT, header.m_format);
459
460 mach_write_to_8(buf + LOG_HEADER_START_LSN, header.m_start_lsn);
461
462 strncpy(reinterpret_cast<char *>(buf) + LOG_HEADER_CREATOR,
463 header.m_creator_name.c_str(), LOG_HEADER_CREATOR_MAX_LENGTH);
464
465 mach_write_to_4(buf + LOG_HEADER_FLAGS, header.m_log_flags);
466
467 mach_write_to_4(buf + LOG_HEADER_LOG_UUID, header.m_log_uuid);
468
469 log_block_set_checksum(buf, log_block_calc_checksum_crc32(buf));
470 }
471
472 bool log_file_header_deserialize(const byte *buf, Log_file_header &header) {
473 header.m_format = mach_read_from_4(buf + LOG_HEADER_FORMAT);
474
475 header.m_start_lsn = mach_read_from_8(buf + LOG_HEADER_START_LSN);
476
477 /* Do not assume there is any null terminator after buf + LOG_HEADER_CREATOR
478 because there could be no and it would then read outside bytes dedicated for
479 the creator name. */
480 auto creator_name =
481 std::string{reinterpret_cast<const char *>(buf + LOG_HEADER_CREATOR),
482 LOG_HEADER_CREATOR_MAX_LENGTH};
483
484 /* Copy only up to the first null terminator. */
485 header.m_creator_name = std::string{creator_name.c_str()};
486
487 header.m_log_flags = mach_read_from_4(buf + LOG_HEADER_FLAGS);
488
489 header.m_log_uuid = mach_read_from_4(buf + LOG_HEADER_LOG_UUID);
490
491 /* Check the header page checksum. There was no
492 checksum in the first redo log format (version 0). */
493 return header.m_format == to_int(Log_format::LEGACY) ||
494 log_header_checksum_is_ok(buf);
495 }
496
497 dberr_t log_file_header_write(Log_file_handle &file_handle,
498 const Log_file_header &header) {
499 Log_file_block buf;
500 log_file_header_serialize(header, buf.data);
501 return log_file_header_write(file_handle, buf.data);
502 }
503
504 dberr_t log_file_header_write(Log_file_handle &file_handle, const byte *buf) {
505 return file_handle.write(0, OS_FILE_LOG_BLOCK_SIZE, buf);
506 }
507
508 dberr_t log_file_header_read(Log_file_handle &file_handle, byte *buf) {
509 return file_handle.read(0, OS_FILE_LOG_BLOCK_SIZE, buf);
510 }
511
512 dberr_t log_file_header_read(Log_file_handle &file_handle,
513 Log_file_header &header) {
514 Log_file_block buf;
515
516 const dberr_t err = log_file_header_read(file_handle, buf.data);
517 if (err != DB_SUCCESS) {
518 return err;
519 }
520
521 if (!log_file_header_deserialize(buf.data, header)) {
522 ib::error(ER_IB_MSG_LOG_FILE_HEADER_INVALID_CHECKSUM);
523 return DB_CORRUPTION;
524 }
525
526 return DB_SUCCESS;
527 }
528
529 static Log_flags log_file_header_flag_bit(uint32_t bit) {
530 ut_a(bit > 0);
531 ut_a(bit <= LOG_HEADER_FLAG_MAX);
532 return 1UL << (bit - 1);
533 }
534
535 void log_file_header_set_flag(Log_flags &log_flags, uint32_t bit) {
536 log_flags |= log_file_header_flag_bit(bit);
537 }
538
539 void log_file_header_reset_flag(Log_flags &log_flags, uint32_t bit) {
540 log_flags &= ~log_file_header_flag_bit(bit);
541 }
542
543 bool log_file_header_check_flag(Log_flags log_flags, uint32_t bit) {
544 return log_flags & log_file_header_flag_bit(bit);
545 }
546
547 /** @} */
548
549 /**************************************************/ /**
550
551 @name Log - encryption header read/write.
552
553 *******************************************************/
554
555 /** @{ */
556
557 dberr_t log_encryption_header_write(Log_file_handle &file_handle,
558 const byte *buf) {
559 return file_handle.write(LOG_ENCRYPTION, OS_FILE_LOG_BLOCK_SIZE, buf);
560 }
561
562 dberr_t log_encryption_header_read(Log_file_handle &file_handle, byte *buf) {
563 return file_handle.read(LOG_ENCRYPTION, OS_FILE_LOG_BLOCK_SIZE, buf);
564 }
565
566 /** @} */
567
568 /**************************************************/ /**
569
570 @name Log - checkpoint header read/write.
571
572 *******************************************************/
573
574 /** @{ */
575
576 void log_checkpoint_header_serialize(const Log_checkpoint_header &header,
577 byte *buf) {
578 memset(buf, 0x00, OS_FILE_LOG_BLOCK_SIZE);
579
580 mach_write_to_8(buf + LOG_CHECKPOINT_LSN, header.m_checkpoint_lsn);
581
582 log_block_set_checksum(buf, log_block_calc_checksum_crc32(buf));
583 }
584
585 bool log_checkpoint_header_deserialize(const byte *buf,
586 Log_checkpoint_header &header) {
587 header.m_checkpoint_lsn = mach_read_from_8(buf + LOG_CHECKPOINT_LSN);
588
589 return log_header_checksum_is_ok(buf);
590 }
591
592 dberr_t log_checkpoint_header_write(
593 Log_file_handle &file_handle, Log_checkpoint_header_no checkpoint_header_no,
594 const Log_checkpoint_header &header) {
595 Log_file_block buf;
596 log_checkpoint_header_serialize(header, buf.data);
597 return log_checkpoint_header_write(file_handle, checkpoint_header_no,
598 buf.data);
599 }
600
601 static os_offset_t log_checkpoint_header_offset(
602 Log_checkpoint_header_no checkpoint_header_no) {
603 switch (checkpoint_header_no) {
604 case Log_checkpoint_header_no::HEADER_1:
605 return LOG_CHECKPOINT_1;
606 case Log_checkpoint_header_no::HEADER_2:
607 return LOG_CHECKPOINT_2;
608 default:
609 ut_error;
610 }
611 }
612
613 dberr_t log_checkpoint_header_write(
614 Log_file_handle &file_handle, Log_checkpoint_header_no checkpoint_header_no,
615 const byte *buf) {
616 const os_offset_t checkpoint_header_offset =
617 log_checkpoint_header_offset(checkpoint_header_no);
618 return file_handle.write(checkpoint_header_offset, OS_FILE_LOG_BLOCK_SIZE,
619 buf);
620 }
621
622 dberr_t log_checkpoint_header_read(
623 Log_file_handle &file_handle, Log_checkpoint_header_no checkpoint_header_no,
624 byte *buf) {
625 const os_offset_t checkpoint_header_offset =
626 log_checkpoint_header_offset(checkpoint_header_no);
627 return file_handle.read(checkpoint_header_offset, OS_FILE_LOG_BLOCK_SIZE,
628 buf);
629 }
630
631 dberr_t log_checkpoint_header_read(
632 Log_file_handle &file_handle, Log_checkpoint_header_no checkpoint_header_no,
633 Log_checkpoint_header &header) {
634 Log_file_block buf;
635
636 const dberr_t err =
637 log_checkpoint_header_read(file_handle, checkpoint_header_no, buf.data);
638 if (err != DB_SUCCESS) {
639 return err;
640 }
641
642 if (!log_checkpoint_header_deserialize(buf.data, header)) {
643 DBUG_PRINT("ib_log", ("invalid checkpoint " UINT32PF " checksum %lx",
644 uint32_t{to_int(checkpoint_header_no)},
645 ulong{log_block_get_checksum(buf.data)}));
646 return DB_CORRUPTION;
647 }
648
649 return DB_SUCCESS;
650 }
651
652 /** @} */
653
654 /**************************************************/ /**
655
656 @name Log data blocks
657
658 *******************************************************/
659
660 /** @{ */
661
662 static void log_data_blocks_validate(os_offset_t offset, os_offset_t size) {
663 ut_a(offset >= LOG_FILE_HDR_SIZE);
664 ut_a(offset % OS_FILE_LOG_BLOCK_SIZE == 0);
665 ut_a(size % OS_FILE_LOG_BLOCK_SIZE == 0);
666 ut_a(size > 0);
667 }
668
669 dberr_t log_data_blocks_write(Log_file_handle &file_handle,
670 os_offset_t write_offset, size_t write_size,
671 const byte *buf) {
672 log_data_blocks_validate(write_offset, write_size);
673 return file_handle.write(write_offset, write_size, buf);
674 }
675
676 dberr_t log_data_blocks_read(Log_file_handle &file_handle,
677 os_offset_t read_offset, size_t read_size,
678 byte *buf) {
679 log_data_blocks_validate(read_offset, read_size);
680 return file_handle.read(read_offset, read_size, buf);
681 }
682
683 /** @} */
684
685 /**************************************************/ /**
686
687 @name Log - file names and paths.
688
689 *******************************************************/
690
691 /** @{ */
692
693 std::string log_directory_path(const Log_files_context &ctx) {
694 std::ostringstream str;
695 if (!ctx.m_root_path.empty()) {
696 str << ctx.m_root_path;
697 /* Add a path separator if needed. */
698 if (ctx.m_root_path.back() != OS_PATH_SEPARATOR) {
699 str << OS_PATH_SEPARATOR;
700 }
701 } else {
702 str << "." << OS_PATH_SEPARATOR;
703 }
704 switch (ctx.m_files_ruleset) {
705 case Log_files_ruleset::CURRENT:
706 str << LOG_DIRECTORY_NAME << OS_PATH_SEPARATOR;
707 break;
708 case Log_files_ruleset::PRE_8_0_30:
709 break;
710 default:
711 ut_error;
712 }
713 return str.str();
714 }
715
716 std::string log_file_name(const Log_files_context &ctx, Log_file_id file_id) {
717 switch (ctx.m_files_ruleset) {
718 case Log_files_ruleset::CURRENT:
719 break;
720 case Log_files_ruleset::PRE_8_0_30:
721 return log_pre_8_0_30::file_name(file_id);
722 default:
723 ut_error;
724 }
725 std::ostringstream str;
726 str << LOG_FILE_BASE_NAME << file_id;
727 return str.str();
728 }
729
730 std::string log_file_path(const Log_files_context &ctx, Log_file_id file_id) {
731 return log_directory_path(ctx) + log_file_name(ctx, file_id);
732 }
733
734 std::string log_file_path_for_unused_file(const Log_files_context &ctx,
735 Log_file_id file_id) {
736 return log_file_path(ctx, file_id) + "_tmp";
737 }
738
739 /** Extracts identifier of the redo log file from its file name.
740 Function might be called for non-redo log files in which case it
741 should return false instead of extracting the identifier.
742 @param[in] ctx redo log files context
743 @param[in] file_name file name (can be wrong)
744 @param[in] expected_suffix suffix that should exist after id
745 @param[out] extracted_id extracted redo file identifier
746 @return true iff extracted the identifier properly */
747 static bool log_extract_id_from_file_name(const Log_files_context &ctx,
748 const char *file_name,
749 const char *expected_suffix,
750 Log_file_id &extracted_id) {
751 std::string file_base_name;
752 switch (ctx.m_files_ruleset) {
753 case Log_files_ruleset::PRE_8_0_30:
754 file_base_name = log_pre_8_0_30::FILE_BASE_NAME;
755 break;
756 case Log_files_ruleset::CURRENT:
757 file_base_name = LOG_FILE_BASE_NAME;
758 break;
759 default:
760 ut_error;
761 }
762
763 const size_t file_name_len = strlen(file_name);
764 const size_t expected_suffix_len = strlen(expected_suffix);
765
766 if (file_name_len <= file_base_name.size() + expected_suffix_len) {
767 return false;
768 }
769 if (memcmp(file_base_name.c_str(), file_name, file_base_name.size()) != 0) {
770 return false;
771 }
772 size_t n_processed = 0;
773 try {
774 if (!isdigit(file_name[file_base_name.size()])) {
775 return false;
776 }
777 auto id = std::stoll(file_name + file_base_name.size(), &n_processed);
778 if (file_base_name.size() + n_processed + expected_suffix_len !=
779 file_name_len) {
780 return false;
781 }
782 if (memcmp(file_name + file_base_name.size() + n_processed, expected_suffix,
783 expected_suffix_len) != 0) {
784 return false;
785 }
786 extracted_id = id;
787 return true;
788 } catch (const std::invalid_argument &) {
789 return false;
790 } catch (const std::out_of_range &) {
791 return false;
792 } catch (...) {
793 ut_error;
794 }
795 }
796
797 /** Lists existing redo files in the configured redo log directory, selecting
798 redo log files which have a given suffix in their name. Produces list of
799 identifiers of the listed and selected files. If a system error occurs when
800 listing the redo directory, error is emitted to the error log and DB_ERROR
801 is returned.
802 @param[in] ctx redo log files context
803 @param[in] suffix suffix used for the selection of files (could be "")
804 @param[out] ret identifiers of the listed and selected files
805 @return DB_SUCCESS or DB_ERROR */
806 static dberr_t log_list_existing_files_low(const Log_files_context &ctx,
807 const char *suffix,
808 ut::vector<Log_file_id> &ret) {
809 ret.clear();
810
811 const auto dir_path = log_directory_path(ctx);
812
813 const bool success = os_file_scan_directory(
814 dir_path.c_str(),
815 [&](const char *, const char *file_name) {
816 Log_file_id file_id;
817 if (log_extract_id_from_file_name(ctx, file_name, suffix, file_id)) {
818 ret.push_back(file_id);
819 }
820 },
821 false);
822
823 std::sort(ret.begin(), ret.end());
824
825 return success ? DB_SUCCESS : DB_ERROR;
826 }
827
828 dberr_t log_list_existing_unused_files(const Log_files_context &ctx,
829 ut::vector<Log_file_id> &ret) {
830 /* Possible error is emitted to the log inside function called below. */
831 return log_list_existing_files_low(ctx, "_tmp", ret);
832 }
833
834 dberr_t log_list_existing_files(const Log_files_context &ctx,
835 ut::vector<Log_file_id> &ret) {
836 /* Possible error is emitted to the log inside function called below. */
837 return log_list_existing_files_low(ctx, "", ret);
838 }
839
840 /** @} */
841
842 /**************************************************/ /**
843
844 @name Log - file creation / deletion
845
846 *******************************************************/
847
848 /** @{ */
849
850 static dberr_t log_rename_file_low(const Log_files_context &ctx
851 [[maybe_unused]],
852 const std::string &old_file_path,
853 const std::string &new_file_path,
854 int err_msg_id) {
855 const bool success = os_file_rename(
856 innodb_log_file_key, old_file_path.c_str(), new_file_path.c_str());
857
858 if (!success) {
859 ib::error(err_msg_id, old_file_path.c_str(), new_file_path.c_str());
860 return DB_ERROR;
861 }
862
863 return DB_SUCCESS;
864 }
865
866 dberr_t log_rename_unused_file(const Log_files_context &ctx,
867 Log_file_id old_unused_file_id,
868 Log_file_id new_unused_file_id) {
869 ut_a(old_unused_file_id != new_unused_file_id);
870 return log_rename_file_low(
871 ctx, log_file_path_for_unused_file(ctx, old_unused_file_id),
872 log_file_path_for_unused_file(ctx, new_unused_file_id),
873 ER_IB_MSG_LOG_FILE_UNUSED_RENAME_FAILED);
874 }
875
876 dberr_t log_mark_file_as_in_use(const Log_files_context &ctx,
877 Log_file_id file_id) {
878 return log_rename_file_low(ctx, log_file_path_for_unused_file(ctx, file_id),
879 log_file_path(ctx, file_id),
880 ER_IB_MSG_LOG_FILE_UNUSED_MARK_AS_IN_USE_FAILED);
881 }
882
883 dberr_t log_mark_file_as_unused(const Log_files_context &ctx,
884 Log_file_id file_id) {
885 return log_rename_file_low(ctx, log_file_path(ctx, file_id),
886 log_file_path_for_unused_file(ctx, file_id),
887 ER_IB_MSG_LOG_FILE_MARK_AS_UNUSED_FAILED);
888 }
889
890 static dberr_t log_remove_file_low(const Log_files_context &,
891 const std::string &file_path,
892 int err_msg_id) {
893 os_file_type_t file_type;
894 os_file_status(file_path.c_str(), nullptr, &file_type);
895 if (file_type == OS_FILE_TYPE_MISSING) {
896 /* File does not exist (note: there is no reason to use the "exists"
897 argument of the os_file_status(), because the "file_type" argument
898 provides information about the missing file by OS_FILE_TYPE_MISSING. */
899 return DB_NOT_FOUND;
900 }
901 ut_a(file_type == OS_FILE_TYPE_FILE);
902 const bool deleted =
903 os_file_delete_if_exists(innodb_log_file_key, file_path.c_str(), nullptr);
904 if (!deleted) {
905 ib::error(err_msg_id, file_path.c_str());
906 return DB_ERROR;
907 }
908 return DB_SUCCESS;
909 }
910
911 dberr_t log_remove_unused_file(const Log_files_context &ctx,
912 Log_file_id file_id) {
913 return log_remove_file_low(ctx, log_file_path_for_unused_file(ctx, file_id),
914 ER_IB_MSG_LOG_FILE_UNUSED_REMOVE_FAILED);
915 }
916
917 std::pair<dberr_t, ut::vector<Log_file_id>> log_remove_unused_files(
918 const Log_files_context &ctx) {
919 ut::vector<Log_file_id> listed_files, removed_files;
920
921 dberr_t err = log_list_existing_unused_files(ctx, listed_files);
922 if (err != DB_SUCCESS) {
923 ut_a(err != DB_NOT_FOUND);
924 return {err, {}};
925 }
926
927 for (Log_file_id id : listed_files) {
928 removed_files.push_back(id);
929 err = log_remove_unused_file(ctx, id);
930 if (err != DB_SUCCESS) {
931 if (err == DB_NOT_FOUND) {
932 err = DB_ERROR;
933 }
934 return {err, removed_files};
935 }
936 }
937
938 /* In older versions of format, ib_logfile101 was used as
939 a temporary file, which marked non-finished initialization. */
940 if (ctx.m_files_ruleset <= Log_files_ruleset::PRE_8_0_30) {
941 removed_files.push_back(101);
942 err = log_remove_file_low(ctx, log_directory_path(ctx) + "ib_logfile101",
943 ER_IB_MSG_LOG_FILE_UNUSED_REMOVE_FAILED);
944 if (err == DB_NOT_FOUND) {
945 removed_files.pop_back();
946 } else if (err != DB_SUCCESS) {
947 return {err, removed_files};
948 }
949 }
950
951 return {DB_SUCCESS, removed_files};
952 }
953
954 dberr_t log_remove_file(const Log_files_context &ctx, Log_file_id file_id) {
955 return log_remove_file_low(ctx, log_file_path(ctx, file_id),
956 ER_IB_MSG_LOG_FILE_REMOVE_FAILED);
957 }
958
959 std::pair<dberr_t, Log_file_id> log_remove_file(const Log_files_context &ctx) {
960 ut::vector<Log_file_id> listed_files;
961
962 dberr_t err = log_list_existing_files(ctx, listed_files);
963 if (err != DB_SUCCESS) {
964 return {err, Log_file_id{}};
965 }
966
967 if (listed_files.empty()) {
968 return {DB_NOT_FOUND, Log_file_id{}};
969 } else {
970 return {log_remove_file(ctx, listed_files.front()), listed_files.front()};
971 }
972 }
973
974 std::pair<dberr_t, ut::vector<Log_file_id>> log_remove_files(
975 const Log_files_context &ctx) {
976 ut::vector<Log_file_id> listed_files, removed_files;
977
978 dberr_t err = log_list_existing_files(ctx, listed_files);
979 if (err != DB_SUCCESS) {
980 return {err, {}};
981 }
982
983 for (Log_file_id id : listed_files) {
984 removed_files.push_back(id);
985 err = log_remove_file(ctx, id);
986 if (err != DB_SUCCESS) {
987 if (err == DB_NOT_FOUND) {
988 err = DB_ERROR;
989 }
990 return {err, removed_files};
991 }
992 }
993 return {DB_SUCCESS, removed_files};
994 }
995
996 dberr_t log_create_unused_file(const Log_files_context &ctx,
997 Log_file_id file_id, os_offset_t size_in_bytes) {
998 ut_a(size_in_bytes >= LOG_FILE_HDR_SIZE);
999
1000 const auto file_path = log_file_path_for_unused_file(ctx, file_id);
1001
1002 bool ret;
1003 auto file = os_file_create(innodb_log_file_key, file_path.c_str(),
1004 OS_FILE_CREATE | OS_FILE_ON_ERROR_NO_EXIT,
1005 OS_LOG_FILE_RESIZING, false, &ret);
1006
1007 if (!ret) {
1008 ib::error(ER_IB_MSG_LOG_FILE_OS_CREATE_FAILED, file_path.c_str());
1009 return DB_ERROR;
1010 }
1011
1012 ret = os_file_set_size_fast(file_path.c_str(), file, 0, size_in_bytes, true);
1013
1014 if (!ret) {
1015 ib::error(ER_IB_MSG_LOG_FILE_RESIZE_FAILED, file_path.c_str(),
1016 ulonglong{size_in_bytes / (1024 * 1024UL)}, "Failed to set size");
1017
1018 /* Delete incomplete file if OOM */
1019 if (os_has_said_disk_full) {
1020 ret = os_file_close(file);
1021 ut_a(ret);
1022 os_file_delete(innodb_log_file_key, file_path.c_str());
1023 }
1024
1025 return DB_ERROR;
1026 }
1027
1028 ret = os_file_close(file);
1029 ut_a(ret);
1030
1031 return DB_SUCCESS;
1032 }
1033
1034 static dberr_t log_resize_file_low(const std::string &file_path,
1035 os_offset_t size_in_bytes, int err_msg_id) {
1036 os_file_stat_t stat_info;
1037 const dberr_t err =
1038 os_file_get_status(file_path.c_str(), &stat_info, false, false);
1039 if (err != DB_SUCCESS) {
1040 ib::error(err_msg_id, file_path.c_str(),
1041 ulonglong{size_in_bytes / (1024 * 1024UL)},
1042 err == DB_NOT_FOUND ? "Failed to find the file"
1043 : "Failed to retrieve status of the file");
1044 return err == DB_NOT_FOUND ? DB_NOT_FOUND : DB_ERROR;
1045 }
1046
1047 if (size_in_bytes == stat_info.size) {
1048 return DB_SUCCESS;
1049 }
1050
1051 bool ret;
1052 auto file = os_file_create(innodb_log_file_key, file_path.c_str(),
1053 OS_FILE_OPEN | OS_FILE_ON_ERROR_NO_EXIT,
1054 OS_LOG_FILE_RESIZING, false, &ret);
1055 if (!ret) {
1056 ib::error(err_msg_id, file_path.c_str(),
1057 ulonglong{size_in_bytes / (1024 * 1024UL)},
1058 "Failed to open the file");
1059 return DB_ERROR;
1060 }
1061
1062 if (size_in_bytes > stat_info.size) {
1063 ret =
1064 os_file_set_size_fast(file_path.c_str(), file, 0, size_in_bytes, true);
1065
1066 } else if (size_in_bytes < stat_info.size) {
1067 ret = os_file_truncate(file_path.c_str(), file, size_in_bytes);
1068 os_file_flush(file);
1069
1070 } else {
1071 ret = true;
1072 }
1073
1074 const bool close_ret = os_file_close(file);
1075 ut_a(close_ret);
1076
1077 if (!ret) {
1078 if (os_has_said_disk_full) {
1079 ib::error(err_msg_id, file_path.c_str(),
1080 ulonglong{size_in_bytes / (1024 * 1024UL)},
1081 "Missing space on disk");
1082 return DB_OUT_OF_DISK_SPACE;
1083 }
1084 ib::error(err_msg_id, file_path.c_str(),
1085 ulonglong{size_in_bytes / (1024 * 1024UL)},
1086 "Failed to resize the file");
1087 return DB_ERROR;
1088 }
1089 return DB_SUCCESS;
1090 }
1091
1092 dberr_t log_resize_unused_file(const Log_files_context &ctx,
1093 Log_file_id file_id, os_offset_t size_in_bytes) {
1094 return log_resize_file_low(log_file_path_for_unused_file(ctx, file_id),
1095 size_in_bytes,
1096 ER_IB_MSG_LOG_FILE_UNUSED_RESIZE_FAILED);
1097 }
1098
1099 dberr_t log_resize_file(const Log_files_context &ctx, Log_file_id file_id,
1100 os_offset_t size_in_bytes) {
1101 return log_resize_file_low(log_file_path(ctx, file_id), size_in_bytes,
1102 ER_IB_MSG_LOG_FILE_RESIZE_FAILED);
1103 }
1104
1105 static dberr_t log_check_file(const Log_files_context &ctx, Log_file_id file_id,
1106 bool read_only, os_offset_t &size_in_bytes) {
1107 const auto file_path = log_file_path(ctx, file_id);
1108
1109 if (!os_file_exists(file_path.c_str())) {
1110 return DB_NOT_FOUND;
1111 }
1112
1113 if (!os_file_check_mode(file_path.c_str(), read_only)) {
1114 /* Error has been emitted in os_file_check_mode */
1115 return DB_ERROR;
1116 }
1117
1118 bool ret;
1119 auto file = os_file_create(innodb_log_file_key, file_path.c_str(),
1120 OS_FILE_OPEN, OS_LOG_FILE, read_only, &ret);
1121 if (!ret) {
1122 ib::error(ER_IB_MSG_LOG_FILE_OPEN_FAILED, file_path.c_str(),
1123 static_cast<int>(DB_ERROR));
1124 return DB_ERROR;
1125 }
1126
1127 size_in_bytes = os_file_get_size(file);
1128
1129 ret = os_file_close(file);
1130 ut_a(ret);
1131
1132 if (size_in_bytes == 0) {
1133 ib::error(ER_IB_MSG_LOG_FILE_IS_EMPTY, file_path.c_str());
1134 return DB_ERROR;
1135 }
1136 if (size_in_bytes < LOG_FILE_MIN_SIZE) {
1137 ib::error(ER_IB_MSG_LOG_FILE_TOO_SMALL, file_path.c_str(),
1138 ulonglong{LOG_FILE_MIN_SIZE});
1139 return DB_ERROR;
1140 }
1141 if (ctx.m_files_ruleset > Log_files_ruleset::PRE_8_0_30) {
1142 if (size_in_bytes > LOG_FILE_MAX_SIZE) {
1143 ib::error(ER_IB_MSG_LOG_FILE_TOO_BIG, file_path.c_str(),
1144 ulonglong{LOG_FILE_MAX_SIZE});
1145 return DB_ERROR;
1146 }
1147 }
1148 if (size_in_bytes % UNIV_PAGE_SIZE != 0) {
1149 /* Even though we tolerate different sizes of log files, still
1150 we require that each of them has size divisible by page size. */
1151 ib::error(ER_IB_MSG_LOG_FILE_SIZE_INVALID, file_path.c_str(),
1152 ulonglong{size_in_bytes});
1153 return DB_ERROR;
1154 }
1155
1156 return DB_SUCCESS;
1157 }
1158
1159 dberr_t log_collect_existing_files(const Log_files_context &ctx, bool read_only,
1160 ut::vector<Log_file_id_and_size> &found) {
1161 ut::vector<Log_file_id> listed_files;
1162 dberr_t err = log_list_existing_files(ctx, listed_files);
1163 if (err != DB_SUCCESS) {
1164 /* Error emitted in log_list_existing_files */
1165 return err;
1166 }
1167 ut::vector<Log_file_id_and_size> result;
1168 for (Log_file_id id : listed_files) {
1169 os_offset_t file_size_in_bytes;
1170 err = log_check_file(ctx, id, read_only, file_size_in_bytes);
1171 switch (err) {
1172 case DB_SUCCESS:
1173 result.emplace_back(id, file_size_in_bytes);
1174 break;
1175 case DB_ERROR:
1176 /* Error has been emitted in log_check_file */
1177 return DB_ERROR;
1178 case DB_NOT_FOUND:
1179 continue;
1180 default:
1181 ut_error;
1182 }
1183 }
1184 found = std::move(result);
1185 return found.empty() ? DB_NOT_FOUND : DB_SUCCESS;
1186 }
1187
1188 Log_uuid log_generate_uuid() {
1189 return ut::random_from_interval(1, std::numeric_limits<Log_uuid>::max());
1190 }
1191
1192 /** @} */
1193
1194 /**************************************************/ /**
1195
1196 @name Log - files context
1197
1198 *******************************************************/
1199
1200 /** @{ */
1201
1202 Log_files_context::Log_files_context(const std::string &root_path,
1203 Log_files_ruleset files_ruleset)
1204 : m_root_path{root_path}, m_files_ruleset{files_ruleset} {}
1205
1206 /** @} */
1207
1208 void Log_data_block_header::set_lsn(lsn_t lsn) {
1209 m_epoch_no = log_block_convert_lsn_to_epoch_no(lsn);
1210 m_hdr_no = log_block_convert_lsn_to_hdr_no(lsn);
1211 }