Debian Codegraph @

storage/innobase/buf/checksum.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 1995, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 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, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file buf/checksum.cc
29 Buffer pool checksum functions, also linked from /extra/innochecksum.cc
30
31 Created Aug 11, 2011 Vasil Dimov
32 *******************************************************/
33
34 #include <sys/types.h>
35 #include <zlib.h>
36
37 #include "buf0buf.h"
38 #include "buf0types.h"
39 #include "fil0fil.h"
40 #include "fil0types.h"
41 #include "log0log.h" /* log_get_lsn */
42 #include "mach0data.h"
43 #include "my_dbug.h"
44 #include "page0size.h"
45 #include "srv0srv.h"
46 #include "univ.i"
47 #include "ut0crc32.h"
48 #include "ut0rnd.h"
49 #ifdef UNIV_HOTBACKUP
50 #include "buf0checksum.h"
51 #endif /* UNIV_HOTBACKUP */
52
53 /** the macro MYSQL_SYSVAR_ENUM() requires "long unsigned int" and if we
54 use srv_checksum_algorithm_t here then we get a compiler error:
55 ha_innodb.cc:12251: error: cannot convert 'srv_checksum_algorithm_t*' to
56 'long unsigned int*' in initialization */
57 ulong srv_checksum_algorithm = SRV_CHECKSUM_ALGORITHM_INNODB;
58
59 /** set if we have found pages matching legacy big endian checksum */
60 static bool legacy_big_endian_checksum = false;
61
62 /** Calculates the CRC32 checksum of a page. The value is stored to the page
63 when it is written to a file and also checked for a match when reading from
64 the file. When reading we allow both normal CRC32 and CRC-legacy-big-endian
65 variants. Note that we must be careful to calculate the same value on 32-bit
66 and 64-bit architectures.
67 @param[in] page buffer page (UNIV_PAGE_SIZE bytes)
68 @param[in] use_legacy_big_endian if true then use big endian
69 byteorder when converting byte strings to integers
70 @return checksum */
71 uint32_t buf_calc_page_crc32(const byte *page,
72 bool use_legacy_big_endian /* = false */) {
73 /* Since the field FIL_PAGE_FILE_FLUSH_LSN, and in versions <= 4.1.x
74 FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID, are written outside the buffer pool
75 to the first pages of data files, we have to skip them in the page
76 checksum calculation.
77 We must also skip the field FIL_PAGE_SPACE_OR_CHKSUM where the
78 checksum is stored, and also the last 8 bytes of page because
79 there we store the old formula checksum. */
80
81 ut_crc32_func_t crc32_func =
82 use_legacy_big_endian ? ut_crc32_legacy_big_endian : ut_crc32;
83
84 const uint32_t c1 = crc32_func(page + FIL_PAGE_OFFSET,
85 FIL_PAGE_FILE_FLUSH_LSN - FIL_PAGE_OFFSET);
86
87 const uint32_t c2 =
88 crc32_func(page + FIL_PAGE_DATA,
89 UNIV_PAGE_SIZE - FIL_PAGE_DATA - FIL_PAGE_END_LSN_OLD_CHKSUM);
90
91 return (c1 ^ c2);
92 }
93
94 /** Calculates a page checksum which is stored to the page when it is written
95 to a file. Note that we must be careful to calculate the same value on
96 32-bit and 64-bit architectures.
97 @return checksum */
98 uint32_t buf_calc_page_new_checksum(const byte *page) /*!< in: buffer page */
99 {
100 /* Since the field FIL_PAGE_FILE_FLUSH_LSN, and in versions <= 4.1.x
101 FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID, are written outside the buffer pool
102 to the first pages of data files, we have to skip them in the page
103 checksum calculation.
104 We must also skip the field FIL_PAGE_SPACE_OR_CHKSUM where the
105 checksum is stored, and also the last 8 bytes of page because
106 there we store the old formula checksum. */
107 return ut::hash_binary_ib(page + FIL_PAGE_OFFSET,
108 FIL_PAGE_FILE_FLUSH_LSN - FIL_PAGE_OFFSET) +
109 ut::hash_binary_ib(
110 page + FIL_PAGE_DATA,
111 UNIV_PAGE_SIZE - FIL_PAGE_DATA - FIL_PAGE_END_LSN_OLD_CHKSUM);
112 }
113
114 /** In versions < 4.0.14 and < 4.1.1 there was a bug that the checksum only
115 looked at the first few bytes of the page. This calculates that old
116 checksum.
117 NOTE: we must first store the new formula checksum to
118 FIL_PAGE_SPACE_OR_CHKSUM before calculating and storing this old checksum
119 because this takes that field as an input!
120 @return checksum */
121 uint32_t buf_calc_page_old_checksum(const byte *page) /*!< in: buffer page */
122 {
123 return ut::hash_binary_ib(page, FIL_PAGE_FILE_FLUSH_LSN);
124 }
125
126 /** Return a printable string describing the checksum algorithm.
127 @return algorithm name */
128 const char *buf_checksum_algorithm_name(
129 srv_checksum_algorithm_t algo) /*!< in: algorithm */
130 {
131 switch (algo) {
132 case SRV_CHECKSUM_ALGORITHM_CRC32:
133 return ("crc32");
134 case SRV_CHECKSUM_ALGORITHM_STRICT_CRC32:
135 return ("strict_crc32");
136 case SRV_CHECKSUM_ALGORITHM_INNODB:
137 return ("innodb");
138 case SRV_CHECKSUM_ALGORITHM_STRICT_INNODB:
139 return ("strict_innodb");
140 case SRV_CHECKSUM_ALGORITHM_NONE:
141 return ("none");
142 case SRV_CHECKSUM_ALGORITHM_STRICT_NONE:
143 return ("strict_none");
144 }
145
146 ut_error;
147 }
148
149 /** Do lsn checks on a page during innodb recovery.
150 @param[in] check_lsn if recv_lsn_checks_on & check_lsn
151 perform lsn check
152 @param[in] read_buf buffer containing the page. */
153 inline void buf_page_lsn_check(bool check_lsn, const byte *read_buf) {
154 #if !defined(UNIV_HOTBACKUP) && !defined(UNIV_LIBRARY)
155 if (check_lsn && recv_lsn_checks_on) {
156 lsn_t current_lsn;
157 const lsn_t page_lsn = mach_read_from_8(read_buf + FIL_PAGE_LSN);
158
159 /* Since we are going to reset the page LSN during the import
160 phase it makes no sense to spam the log with error messages. */
161 current_lsn = log_get_lsn(*log_sys);
162
163 if (current_lsn < page_lsn) {
164 const space_id_t space_id =
165 mach_read_from_4(read_buf + FIL_PAGE_SPACE_ID);
166 const page_no_t page_no = mach_read_from_4(read_buf + FIL_PAGE_OFFSET);
167
168 auto space = fil_space_get(space_id);
169
170 #ifdef UNIV_NO_ERR_MSGS
171 ib::error()
172 #else
173 ib::error(ER_IB_MSG_146)
174 #endif /* UNIV_NO_ERR_MSGS */
175 << "Tablespace '" << space->name << "'"
176 << " Page " << page_id_t(space_id, page_no) << " log sequence number "
177 << page_lsn << " is in the future! Current system"
178 << " log sequence number " << current_lsn << ".";
179
180 #ifdef UNIV_NO_ERR_MSGS
181 ib::error()
182 #else
183 ib::error(ER_IB_MSG_147)
184 #endif /* UNIV_NO_ERR_MSGS */
185 << "Your database may be corrupt or you may have copied the InnoDB"
186 << " tablespace but not the InnoDB redo log files. "
187 << FORCE_RECOVERY_MSG;
188 }
189 }
190 #endif /* !UNIV_HOTBACKUP && !UNIV_LIBRARY */
191 }
192
193 /** Checks if the page is in innodb checksum format.
194 @param[in] checksum_field1 new checksum field
195 @param[in] checksum_field2 old checksum field
196 @param[in] algo current checksum algorithm
197 @return true if the page is in innodb checksum format. */
198 bool BlockReporter::is_checksum_valid_innodb(
199 uint32_t checksum_field1, uint32_t checksum_field2,
200 const srv_checksum_algorithm_t algo) const {
201 /* There are 2 valid formulas for
202 checksum_field2 (old checksum field) which algo=innodb could have
203 written to the page:
204
205 1. Very old versions of InnoDB only stored 8 byte lsn to the
206 start and the end of the page.
207
208 2. Newer InnoDB versions store the old formula checksum
209 (buf_calc_page_old_checksum()). */
210
211 const auto old_checksum = buf_calc_page_old_checksum(m_read_buf);
212 const auto new_checksum = buf_calc_page_new_checksum(m_read_buf);
213
214 print_innodb_checksum(old_checksum, new_checksum, checksum_field1,
215 checksum_field2, algo);
216
217 if (checksum_field2 != mach_read_from_4(m_read_buf + FIL_PAGE_LSN) &&
218 checksum_field2 != old_checksum) {
219 return (false);
220 }
221
222 /* old field is fine, check the new field */
223
224 /* InnoDB versions < 4.0.14 and < 4.1.1 stored the space id
225 (always equal to 0), to FIL_PAGE_SPACE_OR_CHKSUM */
226
227 return (checksum_field1 == 0 || checksum_field1 == new_checksum);
228 }
229
230 /** Checks if the page is in none checksum format.
231 @param[in] checksum_field1 new checksum field
232 @param[in] checksum_field2 old checksum field
233 @param[in] algo current checksum algorithm
234 @return true if the page is in none checksum format. */
235 bool BlockReporter::is_checksum_valid_none(
236 uint32_t checksum_field1, uint32_t checksum_field2,
237 const srv_checksum_algorithm_t algo) const {
238 print_strict_none(checksum_field1, checksum_field2, algo);
239
240 return (checksum_field1 == checksum_field2 &&
241 checksum_field1 == BUF_NO_CHECKSUM_MAGIC);
242 }
243
244 /** Checks if the page is in crc32 checksum format.
245 @param[in] checksum_field1 new checksum field
246 @param[in] checksum_field2 old checksum field
247 @param[in] algo current checksum algorithm
248 @param[in] use_legacy_big_endian big endian algorithm
249 @return true if the page is in crc32 checksum format. */
250 bool BlockReporter::is_checksum_valid_crc32(uint32_t checksum_field1,
251 uint32_t checksum_field2,
252 const srv_checksum_algorithm_t algo,
253 bool use_legacy_big_endian) const {
254 if (checksum_field1 != checksum_field2) {
255 return (false);
256 }
257
258 const auto crc32 = buf_calc_page_crc32(m_read_buf, use_legacy_big_endian);
259
260 print_strict_crc32(checksum_field1, checksum_field2, crc32, algo);
261
262 return (checksum_field1 == crc32);
263 }
264
265 bool BlockReporter::is_encrypted() const noexcept {
266 const auto page_type = mach_read_from_2(m_read_buf + FIL_PAGE_TYPE);
267
268 return page_type == FIL_PAGE_ENCRYPTED ||
269 page_type == FIL_PAGE_COMPRESSED_AND_ENCRYPTED ||
270 page_type == FIL_PAGE_ENCRYPTED_RTREE;
271 }
272
273 /** Checks if a page is corrupt.
274 @retval true if page is corrupt
275 @retval false if page is not corrupt */
276 bool BlockReporter::is_corrupted() const {
277 /* This function should be used for unencrypted pages. During recovery it is
278 possible that this function is called for encrypted pages, when decryption
279 failed. So report it as corrupted. */
280 if (is_encrypted()) {
281 return true;
282 }
283
284 if (!m_page_size.is_compressed() &&
285 memcmp(
286 m_read_buf + FIL_PAGE_LSN + 4,
287 m_read_buf + m_page_size.logical() - FIL_PAGE_END_LSN_OLD_CHKSUM + 4,
288 4)) {
289 /* Stored log sequence numbers at the start and the end
290 of page do not match */
291
292 return (true);
293 }
294
295 buf_page_lsn_check(m_check_lsn, m_read_buf);
296
297 /* Check whether the checksum fields have correct values */
298
299 if (srv_checksum_algorithm == SRV_CHECKSUM_ALGORITHM_NONE ||
300 m_skip_checksum) {
301 return (false);
302 }
303
304 if (m_page_size.is_compressed()) {
305 return (!verify_zip_checksum());
306 }
307
308 const auto checksum_field1 =
309 mach_read_from_4(m_read_buf + FIL_PAGE_SPACE_OR_CHKSUM);
310
311 const auto checksum_field2 = mach_read_from_4(
312 m_read_buf + m_page_size.logical() - FIL_PAGE_END_LSN_OLD_CHKSUM);
313
314 static_assert(FIL_PAGE_LSN % 8 == 0, "FIL_PAGE_LSN must be 64 bit aligned");
315
316 /* declare empty pages non-corrupted */
317 if (checksum_field1 == 0 && checksum_field2 == 0 &&
318 mach_read_from_8(m_read_buf + FIL_PAGE_LSN) == 0) {
319 /* make sure that the page is really empty */
320
321 bool empty = true;
322 #ifndef UNIV_HOTBACKUP
323 for (ulint i = 0; i < m_page_size.logical(); i++) {
324 /* The FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID has been
325 repurposed for page compression. It can be
326 set for uncompressed empty pages. */
327
328 if ((i < FIL_PAGE_FILE_FLUSH_LSN ||
329 i >= FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID) &&
330 m_read_buf[i] != 0) {
331 empty = false;
332 break;
333 }
334 }
335
336 report_empty_page(empty);
337
338 #endif /* !UNIV_HOTBACKUP */
339 return (!empty);
340 }
341
342 const page_id_t page_id(mach_read_from_4(m_read_buf + FIL_PAGE_SPACE_ID),
343 mach_read_from_4(m_read_buf + FIL_PAGE_OFFSET));
344
345 DBUG_EXECUTE_IF("buf_page_import_corrupt_failure", return (true););
346 const srv_checksum_algorithm_t curr_algo =
347 static_cast<srv_checksum_algorithm_t>(srv_checksum_algorithm);
348
349 bool legacy_checksum_checked = false;
350
351 switch (curr_algo) {
352 case SRV_CHECKSUM_ALGORITHM_CRC32:
353 case SRV_CHECKSUM_ALGORITHM_STRICT_CRC32:
354
355 if (is_checksum_valid_crc32(checksum_field1, checksum_field2, curr_algo,
356 false)) {
357 return (false);
358 }
359
360 if (is_checksum_valid_none(checksum_field1, checksum_field2, curr_algo)) {
361 #ifndef UNIV_HOTBACKUP
362 if (curr_algo == SRV_CHECKSUM_ALGORITHM_STRICT_CRC32) {
363 page_warn_strict_checksum(curr_algo, SRV_CHECKSUM_ALGORITHM_NONE,
364 page_id);
365 }
366
367 print_crc32_checksum(checksum_field1, checksum_field2);
368 #endif /* !UNIV_HOTBACKUP */
369 return (false);
370 }
371
372 /* We need to check whether the stored checksum matches legacy
373 big endian checksum or Innodb checksum. We optimize the order
374 based on earlier results. if earlier we have found pages
375 matching legacy big endian checksum, we try to match it first.
376 Otherwise we check innodb checksum first. */
377 if (legacy_big_endian_checksum) {
378 if (is_checksum_valid_crc32(checksum_field1, checksum_field2, curr_algo,
379 true)) {
380 return (false);
381 }
382 legacy_checksum_checked = true;
383 }
384
385 if (is_checksum_valid_innodb(checksum_field1, checksum_field2,
386 curr_algo)) {
387 #ifndef UNIV_HOTBACKUP
388 if (curr_algo == SRV_CHECKSUM_ALGORITHM_STRICT_CRC32) {
389 page_warn_strict_checksum(curr_algo, SRV_CHECKSUM_ALGORITHM_INNODB,
390 page_id);
391 }
392 #endif /* !UNIV_HOTBACKUP */
393 return (false);
394 }
395
396 /* If legacy checksum is not checked, do it now. */
397 if (!legacy_checksum_checked &&
398 is_checksum_valid_crc32(checksum_field1, checksum_field2, curr_algo,
399 true)) {
400 legacy_big_endian_checksum = true;
401 return (false);
402 }
403
404 print_crc32_fail();
405 return (true);
406
407 case SRV_CHECKSUM_ALGORITHM_INNODB:
408 case SRV_CHECKSUM_ALGORITHM_STRICT_INNODB:
409
410 if (is_checksum_valid_innodb(checksum_field1, checksum_field2,
411 curr_algo)) {
412 return (false);
413 }
414
415 if (is_checksum_valid_none(checksum_field1, checksum_field2, curr_algo)) {
416 #ifndef UNIV_HOTBACKUP
417 if (curr_algo == SRV_CHECKSUM_ALGORITHM_STRICT_INNODB) {
418 page_warn_strict_checksum(curr_algo, SRV_CHECKSUM_ALGORITHM_NONE,
419 page_id);
420 }
421
422 print_strict_innodb(checksum_field1, checksum_field2);
423 #endif /* !UNIV_HOTBACKUP */
424 return (false);
425 }
426
427 if (is_checksum_valid_crc32(checksum_field1, checksum_field2, curr_algo,
428 false) ||
429 is_checksum_valid_crc32(checksum_field1, checksum_field2, curr_algo,
430 true)) {
431 #ifndef UNIV_HOTBACKUP
432 if (curr_algo == SRV_CHECKSUM_ALGORITHM_STRICT_INNODB) {
433 page_warn_strict_checksum(curr_algo, SRV_CHECKSUM_ALGORITHM_CRC32,
434 page_id);
435 }
436 #endif /* !UNIV_HOTBACKUP */
437
438 return (false);
439 }
440
441 print_innodb_fail();
442 return (true);
443
444 case SRV_CHECKSUM_ALGORITHM_STRICT_NONE:
445
446 if (is_checksum_valid_none(checksum_field1, checksum_field2, curr_algo)) {
447 return (false);
448 }
449
450 if (is_checksum_valid_crc32(checksum_field1, checksum_field2, curr_algo,
451 false) ||
452 is_checksum_valid_crc32(checksum_field1, checksum_field2, curr_algo,
453 true)) {
454 #ifndef UNIV_HOTBACKUP
455 page_warn_strict_checksum(curr_algo, SRV_CHECKSUM_ALGORITHM_CRC32,
456 page_id);
457 #endif /* !UNIV_HOTBACKUP */
458 return (false);
459 }
460
461 if (is_checksum_valid_innodb(checksum_field1, checksum_field2,
462 curr_algo)) {
463 #ifndef UNIV_HOTBACKUP
464 page_warn_strict_checksum(curr_algo, SRV_CHECKSUM_ALGORITHM_INNODB,
465 page_id);
466 #endif /* !UNIV_HOTBACKUP */
467 return (false);
468 }
469
470 print_none_fail();
471 return (true);
472
473 case SRV_CHECKSUM_ALGORITHM_NONE:
474 /* should have returned false earlier */
475 break;
476 /* no default so the compiler will emit a warning if new enum
477 is added and not handled here */
478 }
479
480 ut_error;
481 }
482
483 uint32_t BlockReporter::calc_zip_checksum(srv_checksum_algorithm_t algo) const {
484 return (calc_zip_checksum(m_read_buf, m_page_size.physical(), algo));
485 }
486
487 uint32_t BlockReporter::calc_zip_checksum(const byte *read_buf,
488 ulint phys_page_size,
489 srv_checksum_algorithm_t algo) const {
490 uLong adler;
491 uint32_t crc32;
492 const Bytef *s = read_buf;
493 const ulint size = phys_page_size;
494
495 /* Exclude FIL_PAGE_SPACE_OR_CHKSUM, FIL_PAGE_LSN,
496 and FIL_PAGE_FILE_FLUSH_LSN from the checksum. */
497
498 switch (algo) {
499 case SRV_CHECKSUM_ALGORITHM_CRC32:
500 case SRV_CHECKSUM_ALGORITHM_STRICT_CRC32:
501
502 ut_ad(size > FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);
503
504 crc32 = ut_crc32(s + FIL_PAGE_OFFSET, FIL_PAGE_LSN - FIL_PAGE_OFFSET) ^
505 ut_crc32(s + FIL_PAGE_TYPE, 2) ^
506 ut_crc32(s + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID,
507 size - FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);
508
509 return (crc32);
510 case SRV_CHECKSUM_ALGORITHM_INNODB:
511 case SRV_CHECKSUM_ALGORITHM_STRICT_INNODB:
512 ut_ad(size > FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);
513
514 adler = adler32(0L, s + FIL_PAGE_OFFSET, FIL_PAGE_LSN - FIL_PAGE_OFFSET);
515 adler = adler32(adler, s + FIL_PAGE_TYPE, 2);
516 adler =
517 adler32(adler, s + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID,
518 static_cast<uInt>(size) - FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);
519
520 return ((uint32_t)adler);
521 case SRV_CHECKSUM_ALGORITHM_NONE:
522 case SRV_CHECKSUM_ALGORITHM_STRICT_NONE:
523 return (BUF_NO_CHECKSUM_MAGIC);
524 /* no default so the compiler will emit a warning if new enum
525 is added and not handled here */
526 }
527
528 ut_error;
529 }
530
531 bool BlockReporter::verify_zip_checksum() const {
532 const uint32_t stored = static_cast<uint32_t>(
533 mach_read_from_4(m_read_buf + FIL_PAGE_SPACE_OR_CHKSUM));
534
535 static_assert(FIL_PAGE_LSN % 8 == 0, "FIL_PAGE_LSN must be 64 bit aligned");
536
537 /* Check if page is empty */
538 if (stored == 0 && mach_read_from_8(m_read_buf + FIL_PAGE_LSN) == 0) {
539 /* make sure that the page is really empty */
540
541 ulint i;
542 bool empty = true;
543 for (i = 0; i < m_page_size.physical(); i++) {
544 if (*((const char *)m_read_buf + i) != 0) {
545 empty = false;
546 break;
547 }
548 }
549
550 report_empty_page(empty);
551
552 /* Empty page */
553 return (empty);
554 }
555
556 const srv_checksum_algorithm_t curr_algo =
557 static_cast<srv_checksum_algorithm_t>(srv_checksum_algorithm);
558 if (curr_algo == SRV_CHECKSUM_ALGORITHM_NONE) {
559 return (true);
560 }
561
562 page_no_t page_no = mach_read_from_4(m_read_buf + FIL_PAGE_OFFSET);
563 space_id_t space_id = mach_read_from_4(m_read_buf + FIL_PAGE_SPACE_ID);
564 const page_id_t page_id(space_id, page_no);
565
566 const uint32_t calc = calc_zip_checksum(curr_algo);
567
568 print_compressed_checksum(calc, stored);
569
570 if (stored == calc) {
571 return (true);
572 }
573
574 bool legacy_checksum_checked = false;
575
576 switch (curr_algo) {
577 case SRV_CHECKSUM_ALGORITHM_STRICT_CRC32:
578 case SRV_CHECKSUM_ALGORITHM_CRC32:
579
580 if (stored == BUF_NO_CHECKSUM_MAGIC) {
581 if (curr_algo == SRV_CHECKSUM_ALGORITHM_STRICT_CRC32) {
582 page_warn_strict_checksum(curr_algo, SRV_CHECKSUM_ALGORITHM_NONE,
583 page_id);
584 }
585
586 return (true);
587 }
588
589 /* We need to check whether the stored checksum matches legacy
590 big endian checksum or Innodb checksum. We optimize the order
591 based on earlier results. if earlier we have found pages
592 matching legacy big endian checksum, we try to match it first.
593 Otherwise we check innodb checksum first. */
594 if (legacy_big_endian_checksum) {
595 if (stored == calc_zip_checksum(SRV_CHECKSUM_ALGORITHM_CRC32)) {
596 return (true);
597 }
598 legacy_checksum_checked = true;
599 }
600
601 if (stored == calc_zip_checksum(SRV_CHECKSUM_ALGORITHM_INNODB)) {
602 if (curr_algo == SRV_CHECKSUM_ALGORITHM_STRICT_CRC32) {
603 page_warn_strict_checksum(curr_algo, SRV_CHECKSUM_ALGORITHM_INNODB,
604 page_id);
605 }
606
607 return (true);
608 }
609
610 /* If legacy checksum is not checked, do it now. */
611 if (!legacy_checksum_checked &&
612 stored == calc_zip_checksum(SRV_CHECKSUM_ALGORITHM_CRC32)) {
613 /* This page's checksum has been created by the
614 legacy software CRC32 implementation on big endian
615 CPUs which generates a different result than the
616 normal CRC32. */
617 legacy_big_endian_checksum = true;
618 return (true);
619 }
620
621 break;
622 case SRV_CHECKSUM_ALGORITHM_STRICT_INNODB:
623 case SRV_CHECKSUM_ALGORITHM_INNODB:
624
625 if (stored == BUF_NO_CHECKSUM_MAGIC) {
626 if (curr_algo == SRV_CHECKSUM_ALGORITHM_STRICT_INNODB) {
627 page_warn_strict_checksum(curr_algo, SRV_CHECKSUM_ALGORITHM_NONE,
628 page_id);
629 }
630 return (true);
631 }
632
633 if (stored == calc_zip_checksum(SRV_CHECKSUM_ALGORITHM_CRC32)) {
634 if (curr_algo == SRV_CHECKSUM_ALGORITHM_STRICT_INNODB) {
635 page_warn_strict_checksum(curr_algo, SRV_CHECKSUM_ALGORITHM_CRC32,
636 page_id);
637 }
638 return (true);
639 }
640
641 break;
642 case SRV_CHECKSUM_ALGORITHM_STRICT_NONE:
643
644 if (stored == calc_zip_checksum(SRV_CHECKSUM_ALGORITHM_CRC32)) {
645 page_warn_strict_checksum(curr_algo, SRV_CHECKSUM_ALGORITHM_CRC32,
646 page_id);
647 return (true);
648 }
649
650 if (stored == calc_zip_checksum(SRV_CHECKSUM_ALGORITHM_INNODB)) {
651 page_warn_strict_checksum(curr_algo, SRV_CHECKSUM_ALGORITHM_INNODB,
652 page_id);
653 return (true);
654 }
655
656 break;
657 case SRV_CHECKSUM_ALGORITHM_NONE:
658 ut_error;
659 /* no default so the compiler will emit a warning if new enum
660 is added and not handled here */
661 }
662
663 return (false);
664 }
665
666 /** Issue a warning when the checksum that is stored in the page is valid,
667 but different than the global setting innodb_checksum_algorithm.
668 @param[in] curr_algo current checksum algorithm
669 @param[in] page_checksum page valid checksum
670 @param[in] page_id page identifier */
671 void BlockReporter::page_warn_strict_checksum(
672 srv_checksum_algorithm_t curr_algo, srv_checksum_algorithm_t page_checksum,
673 const page_id_t &page_id) const {
674 srv_checksum_algorithm_t curr_algo_nonstrict;
675 switch (curr_algo) {
676 case SRV_CHECKSUM_ALGORITHM_STRICT_CRC32:
677 curr_algo_nonstrict = SRV_CHECKSUM_ALGORITHM_CRC32;
678 break;
679 case SRV_CHECKSUM_ALGORITHM_STRICT_INNODB:
680 curr_algo_nonstrict = SRV_CHECKSUM_ALGORITHM_INNODB;
681 break;
682 case SRV_CHECKSUM_ALGORITHM_STRICT_NONE:
683 curr_algo_nonstrict = SRV_CHECKSUM_ALGORITHM_NONE;
684 break;
685 default:
686 ut_error;
687 }
688
689 #ifdef UNIV_NO_ERR_MSGS
690 ib::warn()
691 #else
692 ib::warn(ER_IB_MSG_148)
693 #endif /* UNIV_NO_ERR_MSGS */
694
695 << "innodb_checksum_algorithm is set to \""
696 << buf_checksum_algorithm_name(curr_algo) << "\""
697 << " but the page " << page_id << " contains a valid checksum \""
698 << buf_checksum_algorithm_name(page_checksum) << "\". "
699 << " Accepting the page as valid. Change"
700 << " innodb_checksum_algorithm to \""
701 << buf_checksum_algorithm_name(curr_algo_nonstrict)
702 << "\" to silently accept such pages or rewrite all pages"
703 << " so that they contain \""
704 << buf_checksum_algorithm_name(curr_algo_nonstrict) << "\" checksum.";
705 }
706
707 /** Print the given page_id_t object.
708 @param[in,out] out the output stream
709 @param[in] page_id the page_id_t object to be printed
710 @return the output stream */
711 std::ostream &operator<<(std::ostream &out, const page_id_t &page_id) {
712 out << "[page id: space=" << page_id.m_space
713 << ", page number=" << page_id.m_page_no << "]";
714 return (out);
715 }
716
717 bool BlockReporter::is_lsn_valid(const byte *frame,
718 uint32_t page_size) noexcept {
719 const uint32_t lsn1 = mach_read_from_4(frame + FIL_PAGE_LSN + 4);
720 const uint32_t lsn2 = mach_read_from_4(frame + page_size - 4);
721
722 #ifdef UNIV_DEBUG
723 if (lsn1 != lsn2) {
724 ut_ad(lsn1 == lsn2);
725 }
726 #endif /* UNIV_DEBUG */
727
728 return lsn1 == lsn2;
729 }
730
731 space_id_t BlockReporter::space_id() const noexcept {
732 return mach_read_from_4(m_read_buf + FIL_PAGE_SPACE_ID);
733 }
734
735 page_no_t BlockReporter::page_no() const noexcept {
736 return mach_read_from_4(m_read_buf + FIL_PAGE_OFFSET);
737 }