| 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 include/buf0checksum.h |
| 29 |
Buffer pool checksum functions, also linked from /extra/innochecksum.cc |
| 30 |
|
| 31 |
Created Aug 11, 2011 Vasil Dimov |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef buf0checksum_h |
| 35 |
#define buf0checksum_h |
| 36 |
|
| 37 |
#include "buf0types.h" |
| 38 |
#include "page0size.h" |
| 39 |
#include "univ.i" |
| 40 |
|
| 41 |
/** Calculates the CRC32 checksum of a page. The value is stored to the page |
| 42 |
when it is written to a file and also checked for a match when reading from |
| 43 |
the file. When reading we allow both normal CRC32 and CRC-legacy-big-endian |
| 44 |
variants. Note that we must be careful to calculate the same value on 32-bit |
| 45 |
and 64-bit architectures. |
| 46 |
@param[in] page buffer page (UNIV_PAGE_SIZE bytes) |
| 47 |
@param[in] use_legacy_big_endian if true then use big endian |
| 48 |
byteorder when converting byte strings to integers |
| 49 |
@return checksum */ |
| 50 |
uint32_t buf_calc_page_crc32(const byte *page, |
| 51 |
bool use_legacy_big_endian = false); |
| 52 |
|
| 53 |
/** Calculates a page checksum which is stored to the page when it is written |
| 54 |
to a file. Note that we must be careful to calculate the same value on |
| 55 |
32-bit and 64-bit architectures. |
| 56 |
@return checksum */ |
| 57 |
uint32_t buf_calc_page_new_checksum(const byte *page); /*!< in: buffer page */ |
| 58 |
|
| 59 |
/** In versions < 4.0.14 and < 4.1.1 there was a bug that the checksum only |
| 60 |
looked at the first few bytes of the page. This calculates that old |
| 61 |
checksum. |
| 62 |
NOTE: we must first store the new formula checksum to |
| 63 |
FIL_PAGE_SPACE_OR_CHKSUM before calculating and storing this old checksum |
| 64 |
because this takes that field as an input! |
| 65 |
@return checksum */ |
| 66 |
uint32_t buf_calc_page_old_checksum(const byte *page); /*!< in: buffer page */ |
| 67 |
|
| 68 |
/** Return a printable string describing the checksum algorithm. |
| 69 |
@return algorithm name */ |
| 70 |
const char *buf_checksum_algorithm_name( |
| 71 |
srv_checksum_algorithm_t algo); /*!< in: algorithm */ |
| 72 |
|
| 73 |
extern ulong srv_checksum_algorithm; |
| 74 |
|
| 75 |
/** Class to print checksums to log file. */ |
| 76 |
class BlockReporter { |
| 77 |
public: |
| 78 |
/** Constructor |
| 79 |
@param[in] check_lsn check lsn of the page with the |
| 80 |
current lsn (only in recovery) |
| 81 |
@param[in] read_buf buffer holding the page |
| 82 |
@param[in] page_size page size |
| 83 |
@param[in] skip_checksum skip checksum verification */ |
| 84 |
BlockReporter(bool check_lsn, const byte *read_buf, |
| 85 |
const page_size_t &page_size, bool skip_checksum) |
| 86 |
: m_check_lsn(check_lsn), |
| 87 |
m_read_buf(read_buf), |
| 88 |
m_page_size(page_size), |
| 89 |
m_skip_checksum(skip_checksum) {} |
| 90 |
|
| 91 |
virtual ~BlockReporter() = default; |
| 92 |
BlockReporter(const BlockReporter &) = default; |
| 93 |
|
| 94 |
/** Checks if a page is corrupt. |
| 95 |
@retval true if page is corrupt |
| 96 |
@retval false if page is not corrupt */ |
| 97 |
[[nodiscard]] bool is_corrupted() const; |
| 98 |
|
| 99 |
/** Checks if a page is encrypted. |
| 100 |
@retval true if page is encrypted |
| 101 |
@retval false if page is not encrypted */ |
| 102 |
[[nodiscard]] bool is_encrypted() const noexcept; |
| 103 |
|
| 104 |
/** Print message if page is empty. |
| 105 |
@param[in] empty true if page is empty */ |
| 106 |
virtual inline void report_empty_page(bool empty [[maybe_unused]]) const {} |
| 107 |
|
| 108 |
/** Print crc32 checksum and the checksum fields in page. |
| 109 |
@param[in] checksum_field1 Checksum in page header |
| 110 |
@param[in] checksum_field2 Checksum in page trailer |
| 111 |
@param[in] crc32 Calculated crc32 checksum |
| 112 |
@param[in] algo Current checksum algorithm */ |
| 113 |
virtual inline void print_strict_crc32(uint32_t checksum_field1 |
| 114 |
[[maybe_unused]], |
| 115 |
uint32_t checksum_field2 |
| 116 |
[[maybe_unused]], |
| 117 |
uint32_t crc32 [[maybe_unused]], |
| 118 |
srv_checksum_algorithm_t algo |
| 119 |
[[maybe_unused]]) const {} |
| 120 |
|
| 121 |
/** Print innodb checksum and the checksum fields in page. |
| 122 |
@param[in] checksum_field1 Checksum in page header |
| 123 |
@param[in] checksum_field2 Checksum in page trailer */ |
| 124 |
virtual inline void print_strict_innodb(uint32_t checksum_field1 |
| 125 |
[[maybe_unused]], |
| 126 |
uint32_t checksum_field2 |
| 127 |
[[maybe_unused]]) const {} |
| 128 |
|
| 129 |
/** Print none checksum and the checksum fields in page. |
| 130 |
@param[in] checksum_field1 Checksum in page header |
| 131 |
@param[in] checksum_field2 Checksum in page trailer |
| 132 |
@param[in] algo Current checksum algorithm */ |
| 133 |
virtual inline void print_strict_none(uint32_t checksum_field1 |
| 134 |
[[maybe_unused]], |
| 135 |
uint32_t checksum_field2 |
| 136 |
[[maybe_unused]], |
| 137 |
srv_checksum_algorithm_t algo |
| 138 |
[[maybe_unused]]) const {} |
| 139 |
|
| 140 |
/** Print innodb checksum value stored in page trailer. |
| 141 |
@param[in] old_checksum checksum value according to old style |
| 142 |
@param[in] new_checksum checksum value according to new style |
| 143 |
@param[in] checksum_field1 Checksum in page header |
| 144 |
@param[in] checksum_field2 Checksum in page trailer |
| 145 |
@param[in] algo current checksum algorithm */ |
| 146 |
virtual inline void print_innodb_checksum( |
| 147 |
uint32_t old_checksum [[maybe_unused]], |
| 148 |
uint32_t new_checksum [[maybe_unused]], |
| 149 |
uint32_t checksum_field1 [[maybe_unused]], |
| 150 |
uint32_t checksum_field2 [[maybe_unused]], |
| 151 |
srv_checksum_algorithm_t algo [[maybe_unused]]) const {} |
| 152 |
|
| 153 |
/** Print the message that checksum mismatch happened in |
| 154 |
page header. */ |
| 155 |
virtual inline void print_innodb_fail() const {} |
| 156 |
|
| 157 |
/** Print both new-style, old-style & crc32 checksum values. |
| 158 |
@param[in] checksum_field1 Checksum in page header |
| 159 |
@param[in] checksum_field2 Checksum in page trailer */ |
| 160 |
virtual inline void print_crc32_checksum(uint32_t checksum_field1 |
| 161 |
[[maybe_unused]], |
| 162 |
uint32_t checksum_field2 |
| 163 |
[[maybe_unused]]) const {} |
| 164 |
|
| 165 |
/** Print a message that crc32 check failed. */ |
| 166 |
virtual inline void print_crc32_fail() const {} |
| 167 |
|
| 168 |
/** Print a message that none check failed. */ |
| 169 |
virtual inline void print_none_fail() const {} |
| 170 |
|
| 171 |
/** Print checksum values on a compressed page. |
| 172 |
@param[in] calc the calculated checksum value |
| 173 |
@param[in] stored the stored checksum in header. */ |
| 174 |
virtual inline void print_compressed_checksum(uint32_t calc [[maybe_unused]], |
| 175 |
uint32_t stored |
| 176 |
[[maybe_unused]]) const {} |
| 177 |
|
| 178 |
/** Verify a compressed page's checksum. |
| 179 |
@retval true if stored checksum is valid |
| 180 |
according to the value of srv_checksum_algorithm |
| 181 |
@retval false if stored checksum is not valid |
| 182 |
according to the value of srv_checksum_algorithm */ |
| 183 |
bool verify_zip_checksum() const; |
| 184 |
|
| 185 |
/** Calculate the compressed page checksum. This variant |
| 186 |
should be used when only the page_size_t is unknown and |
| 187 |
only physical page_size of compressed page is available. |
| 188 |
@param[in] read_buf buffer holding the page |
| 189 |
@param[in] phys_page_size physical page size |
| 190 |
@param[in] algo checksum algorithm to use |
| 191 |
@return page checksum */ |
| 192 |
uint32_t calc_zip_checksum(const byte *read_buf, ulint phys_page_size, |
| 193 |
srv_checksum_algorithm_t algo) const; |
| 194 |
|
| 195 |
/** Calculate the compressed page checksum. |
| 196 |
@param[in] algo checksum algorithm to use |
| 197 |
@return page checksum */ |
| 198 |
uint32_t calc_zip_checksum(srv_checksum_algorithm_t algo) const; |
| 199 |
|
| 200 |
[[nodiscard]] static bool is_lsn_valid(const byte *frame, |
| 201 |
uint32_t page_size) noexcept; |
| 202 |
|
| 203 |
private: |
| 204 |
/** Checks if the page is in innodb checksum format. |
| 205 |
@param[in] checksum_field1 new checksum field |
| 206 |
@param[in] checksum_field2 old checksum field |
| 207 |
@param[in] algo current checksum algorithm |
| 208 |
@return true if the page is in innodb checksum format. */ |
| 209 |
bool is_checksum_valid_innodb(uint32_t checksum_field1, |
| 210 |
uint32_t checksum_field2, |
| 211 |
const srv_checksum_algorithm_t algo) const; |
| 212 |
|
| 213 |
/** Checks if the page is in none checksum format. |
| 214 |
@param[in] checksum_field1 new checksum field |
| 215 |
@param[in] checksum_field2 old checksum field |
| 216 |
@param[in] algo current checksum algorithm |
| 217 |
@return true if the page is in none checksum format. */ |
| 218 |
bool is_checksum_valid_none(uint32_t checksum_field1, |
| 219 |
uint32_t checksum_field2, |
| 220 |
const srv_checksum_algorithm_t algo) const; |
| 221 |
|
| 222 |
/** Checks if the page is in crc32 checksum format. |
| 223 |
@param[in] checksum_field1 new checksum field |
| 224 |
@param[in] checksum_field2 old checksum field |
| 225 |
@param[in] algo current checksum algorithm |
| 226 |
@param[in] use_legacy_big_endian big endian algorithm |
| 227 |
@return true if the page is in crc32 checksum format. */ |
| 228 |
bool is_checksum_valid_crc32(uint32_t checksum_field1, |
| 229 |
uint32_t checksum_field2, |
| 230 |
const srv_checksum_algorithm_t algo, |
| 231 |
bool use_legacy_big_endian) const; |
| 232 |
|
| 233 |
/** Issue a warning when the checksum that is stored in the page is |
| 234 |
valid, but different than the global setting innodb_checksum_algorithm. |
| 235 |
@param[in] curr_algo current checksum algorithm |
| 236 |
@param[in] page_checksum page valid checksum |
| 237 |
@param[in] page_id page identifier */ |
| 238 |
void page_warn_strict_checksum(srv_checksum_algorithm_t curr_algo, |
| 239 |
srv_checksum_algorithm_t page_checksum, |
| 240 |
const page_id_t &page_id) const; |
| 241 |
|
| 242 |
[[nodiscard]] space_id_t space_id() const noexcept; |
| 243 |
[[nodiscard]] page_no_t page_no() const noexcept; |
| 244 |
|
| 245 |
protected: |
| 246 |
/** If true, do a LSN check during innodb recovery. */ |
| 247 |
bool m_check_lsn; |
| 248 |
/** Buffer holding the page. */ |
| 249 |
const byte *m_read_buf; |
| 250 |
/** Page size. */ |
| 251 |
const page_size_t &m_page_size; |
| 252 |
/** Skip checksum verification but compare only data. */ |
| 253 |
bool m_skip_checksum; |
| 254 |
}; |
| 255 |
|
| 256 |
#endif /* buf0checksum_h */ |