MySQL 8.1.0
Source Code Documentation
buf0checksum.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1995, 2023, 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 also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25*****************************************************************************/
26
27/** @file include/buf0checksum.h
28 Buffer pool checksum functions, also linked from /extra/innochecksum.cc
29
30 Created Aug 11, 2011 Vasil Dimov
31 *******************************************************/
32
33#ifndef buf0checksum_h
34#define buf0checksum_h
35
36#include "buf0types.h"
37#include "page0size.h"
38#include "univ.i"
39
40/** Calculates the CRC32 checksum of a page. The value is stored to the page
41when it is written to a file and also checked for a match when reading from
42the file. When reading we allow both normal CRC32 and CRC-legacy-big-endian
43variants. Note that we must be careful to calculate the same value on 32-bit
44and 64-bit architectures.
45@param[in] page buffer page (UNIV_PAGE_SIZE bytes)
46@param[in] use_legacy_big_endian if true then use big endian
47byteorder when converting byte strings to integers
48@return checksum */
49uint32_t buf_calc_page_crc32(const byte *page,
50 bool use_legacy_big_endian = false);
51
52/** Calculates a page checksum which is stored to the page when it is written
53 to a file. Note that we must be careful to calculate the same value on
54 32-bit and 64-bit architectures.
55 @return checksum */
56uint32_t buf_calc_page_new_checksum(const byte *page); /*!< in: buffer page */
57
58/** In versions < 4.0.14 and < 4.1.1 there was a bug that the checksum only
59 looked at the first few bytes of the page. This calculates that old
60 checksum.
61 NOTE: we must first store the new formula checksum to
62 FIL_PAGE_SPACE_OR_CHKSUM before calculating and storing this old checksum
63 because this takes that field as an input!
64 @return checksum */
65uint32_t buf_calc_page_old_checksum(const byte *page); /*!< in: buffer page */
66
67/** Return a printable string describing the checksum algorithm.
68 @return algorithm name */
70 srv_checksum_algorithm_t algo); /*!< in: algorithm */
71
72extern ulong srv_checksum_algorithm;
73
74/** Class to print checksums to log file. */
76 public:
77 /** Constructor
78 @param[in] check_lsn check lsn of the page with the
79 current lsn (only in recovery)
80 @param[in] read_buf buffer holding the page
81 @param[in] page_size page size
82 @param[in] skip_checksum skip checksum verification */
83 BlockReporter(bool check_lsn, const byte *read_buf,
84 const page_size_t &page_size, bool skip_checksum)
85 : m_check_lsn(check_lsn),
86 m_read_buf(read_buf),
87 m_page_size(page_size),
88 m_skip_checksum(skip_checksum) {}
89
90 virtual ~BlockReporter() = default;
91 BlockReporter(const BlockReporter &) = default;
92
93 /** Checks if a page is corrupt.
94 @retval true if page is corrupt
95 @retval false if page is not corrupt */
96 [[nodiscard]] bool is_corrupted() const;
97
98 /** Checks if a page is encrypted.
99 @retval true if page is encrypted
100 @retval false if page is not encrypted */
101 [[nodiscard]] bool is_encrypted() const noexcept;
102
103 /** Print message if page is empty.
104 @param[in] empty true if page is empty */
105 virtual inline void report_empty_page(bool empty [[maybe_unused]]) const {}
106
107 /** Print crc32 checksum and the checksum fields in page.
108 @param[in] checksum_field1 Checksum in page header
109 @param[in] checksum_field2 Checksum in page trailer
110 @param[in] crc32 Calculated crc32 checksum
111 @param[in] algo Current checksum algorithm */
112 virtual inline void print_strict_crc32(uint32_t checksum_field1
113 [[maybe_unused]],
114 uint32_t checksum_field2
115 [[maybe_unused]],
116 uint32_t crc32 [[maybe_unused]],
118 [[maybe_unused]]) const {}
119
120 /** Print innodb checksum and the checksum fields in page.
121 @param[in] checksum_field1 Checksum in page header
122 @param[in] checksum_field2 Checksum in page trailer */
123 virtual inline void print_strict_innodb(uint32_t checksum_field1
124 [[maybe_unused]],
125 uint32_t checksum_field2
126 [[maybe_unused]]) const {}
127
128 /** Print none checksum and the checksum fields in page.
129 @param[in] checksum_field1 Checksum in page header
130 @param[in] checksum_field2 Checksum in page trailer
131 @param[in] algo Current checksum algorithm */
132 virtual inline void print_strict_none(uint32_t checksum_field1
133 [[maybe_unused]],
134 uint32_t checksum_field2
135 [[maybe_unused]],
137 [[maybe_unused]]) const {}
138
139 /** Print innodb checksum value stored in page trailer.
140 @param[in] old_checksum checksum value according to old style
141 @param[in] new_checksum checksum value according to new style
142 @param[in] checksum_field1 Checksum in page header
143 @param[in] checksum_field2 Checksum in page trailer
144 @param[in] algo current checksum algorithm */
145 virtual inline void print_innodb_checksum(
146 uint32_t old_checksum [[maybe_unused]],
147 uint32_t new_checksum [[maybe_unused]],
148 uint32_t checksum_field1 [[maybe_unused]],
149 uint32_t checksum_field2 [[maybe_unused]],
150 srv_checksum_algorithm_t algo [[maybe_unused]]) const {}
151
152 /** Print the message that checksum mismatch happened in
153 page header. */
154 virtual inline void print_innodb_fail() const {}
155
156 /** Print both new-style, old-style & crc32 checksum values.
157 @param[in] checksum_field1 Checksum in page header
158 @param[in] checksum_field2 Checksum in page trailer */
159 virtual inline void print_crc32_checksum(uint32_t checksum_field1
160 [[maybe_unused]],
161 uint32_t checksum_field2
162 [[maybe_unused]]) const {}
163
164 /** Print a message that crc32 check failed. */
165 virtual inline void print_crc32_fail() const {}
166
167 /** Print a message that none check failed. */
168 virtual inline void print_none_fail() const {}
169
170 /** Print checksum values on a compressed page.
171 @param[in] calc the calculated checksum value
172 @param[in] stored the stored checksum in header. */
173 virtual inline void print_compressed_checksum(uint32_t calc [[maybe_unused]],
174 uint32_t stored
175 [[maybe_unused]]) const {}
176
177 /** Verify a compressed page's checksum.
178 @retval true if stored checksum is valid
179 according to the value of srv_checksum_algorithm
180 @retval false if stored checksum is not valid
181 according to the value of srv_checksum_algorithm */
182 bool verify_zip_checksum() const;
183
184 /** Calculate the compressed page checksum. This variant
185 should be used when only the page_size_t is unknown and
186 only physical page_size of compressed page is available.
187 @param[in] read_buf buffer holding the page
188 @param[in] phys_page_size physical page size
189 @param[in] algo checksum algorithm to use
190 @return page checksum */
191 uint32_t calc_zip_checksum(const byte *read_buf, ulint phys_page_size,
192 srv_checksum_algorithm_t algo) const;
193
194 /** Calculate the compressed page checksum.
195 @param[in] algo checksum algorithm to use
196 @return page checksum */
197 uint32_t calc_zip_checksum(srv_checksum_algorithm_t algo) const;
198
199 [[nodiscard]] static bool is_lsn_valid(const byte *frame,
200 uint32_t page_size) noexcept;
201
202 private:
203 /** Checks if the page is in innodb checksum format.
204 @param[in] checksum_field1 new checksum field
205 @param[in] checksum_field2 old checksum field
206 @param[in] algo current checksum algorithm
207 @return true if the page is in innodb checksum format. */
208 bool is_checksum_valid_innodb(uint32_t checksum_field1,
209 uint32_t checksum_field2,
210 const srv_checksum_algorithm_t algo) const;
211
212 /** Checks if the page is in none checksum format.
213 @param[in] checksum_field1 new checksum field
214 @param[in] checksum_field2 old checksum field
215 @param[in] algo current checksum algorithm
216 @return true if the page is in none checksum format. */
217 bool is_checksum_valid_none(uint32_t checksum_field1,
218 uint32_t checksum_field2,
219 const srv_checksum_algorithm_t algo) const;
220
221 /** Checks if the page is in crc32 checksum format.
222 @param[in] checksum_field1 new checksum field
223 @param[in] checksum_field2 old checksum field
224 @param[in] algo current checksum algorithm
225 @param[in] use_legacy_big_endian big endian algorithm
226 @return true if the page is in crc32 checksum format. */
227 bool is_checksum_valid_crc32(uint32_t checksum_field1,
228 uint32_t checksum_field2,
229 const srv_checksum_algorithm_t algo,
230 bool use_legacy_big_endian) const;
231
232 /** Issue a warning when the checksum that is stored in the page is
233 valid, but different than the global setting innodb_checksum_algorithm.
234 @param[in] curr_algo current checksum algorithm
235 @param[in] page_checksum page valid checksum
236 @param[in] page_id page identifier */
238 srv_checksum_algorithm_t page_checksum,
239 const page_id_t &page_id) const;
240
241 [[nodiscard]] space_id_t space_id() const noexcept;
242 [[nodiscard]] page_no_t page_no() const noexcept;
243
244 protected:
245 /** If true, do a LSN check during innodb recovery. */
247 /** Buffer holding the page. */
248 const byte *m_read_buf;
249 /** Page size. */
251 /** Skip checksum verification but compare only data. */
253};
254
255#endif /* buf0checksum_h */
uint32_t space_id_t
Tablespace identifier.
Definition: api0api.h:50
uint32_t page_no_t
Page number.
Definition: api0api.h:48
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:56
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:97
const char * buf_checksum_algorithm_name(srv_checksum_algorithm_t algo)
Return a printable string describing the checksum algorithm.
Definition: checksum.cc:127
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:120
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:70
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:114
Class to print checksums to log file.
Definition: buf0checksum.h:75
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:159
bool is_encrypted() const noexcept
Checks if a page is encrypted.
Definition: checksum.cc:264
virtual void report_empty_page(bool empty) const
Print message if page is empty.
Definition: buf0checksum.h:105
page_no_t page_no() const noexcept
Definition: checksum.cc:734
BlockReporter(bool check_lsn, const byte *read_buf, const page_size_t &page_size, bool skip_checksum)
Constructor.
Definition: buf0checksum.h:83
virtual void print_compressed_checksum(uint32_t calc, uint32_t stored) const
Print checksum values on a compressed page.
Definition: buf0checksum.h:173
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:132
bool verify_zip_checksum() const
Verify a compressed page's checksum.
Definition: checksum.cc:530
bool is_corrupted() const
Checks if a page is corrupt.
Definition: checksum.cc:275
bool m_check_lsn
If true, do a LSN check during innodb recovery.
Definition: buf0checksum.h:246
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:486
virtual void print_none_fail() const
Print a message that none check failed.
Definition: buf0checksum.h:168
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:249
const page_size_t & m_page_size
Page size.
Definition: buf0checksum.h:250
bool m_skip_checksum
Skip checksum verification but compare only data.
Definition: buf0checksum.h:252
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:234
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:145
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:112
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:123
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:197
static bool is_lsn_valid(const byte *frame, uint32_t page_size) noexcept
Definition: checksum.cc:716
virtual void print_crc32_fail() const
Print a message that crc32 check failed.
Definition: buf0checksum.h:165
space_id_t space_id() const noexcept
Definition: checksum.cc:730
const byte * m_read_buf
Buffer holding the page.
Definition: buf0checksum.h:248
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:670
virtual void print_innodb_fail() const
Print the message that checksum mismatch happened in page header.
Definition: buf0checksum.h:154
Page identifier.
Definition: buf0types.h:206
Page size descriptor.
Definition: page0size.h:49
int page
Definition: ctype-mb.cc:1233
bool empty(const Histogram &histogram)
Return true if 'histogram' was built on an empty table.
Definition: histogram.h:651
uint32_t crc32(const byte *buf, size_t len)
Computes CRC32-C hash not using any hardware acceleration.
Definition: crc32.cc:281
A class describing a page size.
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:405