MySQL 9.1.0
Source Code Documentation
buf0checksum.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1995, 2024, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 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
42when it is written to a file and also checked for a match when reading from
43the file. When reading we allow both normal CRC32 and CRC-legacy-big-endian
44variants. Note that we must be careful to calculate the same value on 32-bit
45and 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
48byteorder when converting byte strings to integers
49@return checksum */
50uint32_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 */
57uint32_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 */
66uint32_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 */
71 srv_checksum_algorithm_t algo); /*!< in: algorithm */
72
73extern ulong srv_checksum_algorithm;
74
75/** Class to print checksums to log file. */
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]],
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]],
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 */
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. */
248 /** Buffer holding the page. */
249 const byte *m_read_buf;
250 /** Page size. */
252 /** Skip checksum verification but compare only data. */
254};
255
256#endif /* buf0checksum_h */
uint32_t space_id_t
Tablespace identifier.
Definition: api0api.h:48
uint32_t page_no_t
Page number.
Definition: api0api.h:46
ulong srv_checksum_algorithm
the macro MYSQL_SYSVAR_ENUM() requires "long unsigned int" and if we use srv_checksum_algorithm_t her...
Definition: checksum.cc:57
uint32_t buf_calc_page_new_checksum(const byte *page)
Calculates a page checksum which is stored to the page when it is written to a file.
Definition: checksum.cc:98
const char * buf_checksum_algorithm_name(srv_checksum_algorithm_t algo)
Return a printable string describing the checksum algorithm.
Definition: checksum.cc:128
uint32_t buf_calc_page_old_checksum(const byte *page)
In versions < 4.0.14 and < 4.1.1 there was a bug that the checksum only looked at the first few bytes...
Definition: checksum.cc:121
uint32_t buf_calc_page_crc32(const byte *page, bool use_legacy_big_endian=false)
Calculates the CRC32 checksum of a page.
Definition: checksum.cc:71
The database buffer pool global types for the directory.
srv_checksum_algorithm_t
Alternatives for srv_checksum_algorithm, which can be changed by setting innodb_checksum_algorithm.
Definition: buf0types.h:115
Class to print checksums to log file.
Definition: buf0checksum.h:76
virtual void print_crc32_checksum(uint32_t checksum_field1, uint32_t checksum_field2) const
Print both new-style, old-style & crc32 checksum values.
Definition: buf0checksum.h:160
bool is_encrypted() const noexcept
Checks if a page is encrypted.
Definition: checksum.cc:265
virtual void report_empty_page(bool empty) const
Print message if page is empty.
Definition: buf0checksum.h:106
page_no_t page_no() const noexcept
Definition: checksum.cc:727
BlockReporter(bool check_lsn, const byte *read_buf, const page_size_t &page_size, bool skip_checksum)
Constructor.
Definition: buf0checksum.h:84
virtual void print_compressed_checksum(uint32_t calc, uint32_t stored) const
Print checksum values on a compressed page.
Definition: buf0checksum.h:174
virtual void print_strict_none(uint32_t checksum_field1, uint32_t checksum_field2, srv_checksum_algorithm_t algo) const
Print none checksum and the checksum fields in page.
Definition: buf0checksum.h:133
bool verify_zip_checksum() const
Verify a compressed page's checksum.
Definition: checksum.cc:531
bool is_corrupted() const
Checks if a page is corrupt.
Definition: checksum.cc:276
bool m_check_lsn
If true, do a LSN check during innodb recovery.
Definition: buf0checksum.h:247
uint32_t calc_zip_checksum(const byte *read_buf, ulint phys_page_size, srv_checksum_algorithm_t algo) const
Calculate the compressed page checksum.
Definition: checksum.cc:487
virtual void print_none_fail() const
Print a message that none check failed.
Definition: buf0checksum.h:169
BlockReporter(const BlockReporter &)=default
bool is_checksum_valid_crc32(uint32_t checksum_field1, uint32_t checksum_field2, const srv_checksum_algorithm_t algo, bool use_legacy_big_endian) const
Checks if the page is in crc32 checksum format.
Definition: checksum.cc:250
const page_size_t & m_page_size
Page size.
Definition: buf0checksum.h:251
bool m_skip_checksum
Skip checksum verification but compare only data.
Definition: buf0checksum.h:253
bool is_checksum_valid_none(uint32_t checksum_field1, uint32_t checksum_field2, const srv_checksum_algorithm_t algo) const
Checks if the page is in none checksum format.
Definition: checksum.cc:235
virtual void print_innodb_checksum(uint32_t old_checksum, uint32_t new_checksum, uint32_t checksum_field1, uint32_t checksum_field2, srv_checksum_algorithm_t algo) const
Print innodb checksum value stored in page trailer.
Definition: buf0checksum.h:146
virtual ~BlockReporter()=default
virtual void print_strict_crc32(uint32_t checksum_field1, uint32_t checksum_field2, uint32_t crc32, srv_checksum_algorithm_t algo) const
Print crc32 checksum and the checksum fields in page.
Definition: buf0checksum.h:113
virtual void print_strict_innodb(uint32_t checksum_field1, uint32_t checksum_field2) const
Print innodb checksum and the checksum fields in page.
Definition: buf0checksum.h:124
bool is_checksum_valid_innodb(uint32_t checksum_field1, uint32_t checksum_field2, const srv_checksum_algorithm_t algo) const
Checks if the page is in innodb checksum format.
Definition: checksum.cc:198
static bool is_lsn_valid(const byte *frame, uint32_t page_size) noexcept
Definition: checksum.cc:709
virtual void print_crc32_fail() const
Print a message that crc32 check failed.
Definition: buf0checksum.h:166
space_id_t space_id() const noexcept
Definition: checksum.cc:723
const byte * m_read_buf
Buffer holding the page.
Definition: buf0checksum.h:249
void page_warn_strict_checksum(srv_checksum_algorithm_t curr_algo, srv_checksum_algorithm_t page_checksum, const page_id_t &page_id) const
Issue a warning when the checksum that is stored in the page is valid, but different than the global ...
Definition: checksum.cc:663
virtual void print_innodb_fail() const
Print the message that checksum mismatch happened in page header.
Definition: buf0checksum.h:155
Page identifier.
Definition: buf0types.h:207
Page size descriptor.
Definition: page0size.h:50
int page
Definition: ctype-mb.cc:1224
bool empty(const Histogram &histogram)
Return true if 'histogram' was built on an empty table.
Definition: histogram.h:693
uint32_t crc32(const byte *buf, size_t len)
Computes CRC32-C hash not using any hardware acceleration.
Definition: crc32.cc:282
A class describing a page size.
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:406