MySQL  8.0.19
Source Code Documentation
buf0checksum.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 1995, 2018, Oracle and/or its affiliates. All Rights Reserved.
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 also distributed with certain software (including but not
10 limited to OpenSSL) that is licensed under separate terms, as designated in a
11 particular file or component or in included license documentation. The authors
12 of MySQL hereby grant you an additional permission to link the program and
13 your derivative works with the separately licensed software that they have
14 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 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
41 when it is written to a file and also checked for a match when reading from
42 the file. When reading we allow both normal CRC32 and CRC-legacy-big-endian
43 variants. Note that we must be careful to calculate the same value on 32-bit
44 and 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
47 byteorder when converting byte strings to integers
48 @return checksum */
49 uint32_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 */
56 ulint 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 */
65 ulint 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 */
69 const char *buf_checksum_algorithm_name(
70  srv_checksum_algorithm_t algo); /*!< in: algorithm */
71 
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() {}
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  bool MY_ATTRIBUTE((warn_unused_result)) is_corrupted() const;
97 
98  /** Print message if page is empty.
99  @param[in] empty true if page is empty */
100  virtual inline void report_empty_page(bool empty) const {}
101 
102  /** Print crc32 checksum and the checksum fields in page.
103  @param[in] checksum_field1 Checksum in page header
104  @param[in] checksum_field2 Checksum in page trailer
105  @param[in] crc32 Calculated crc32 checksum
106  @param[in] algo Current checksum algorithm */
107  virtual inline void print_strict_crc32(ulint checksum_field1,
108  ulint checksum_field2, uint32_t crc32,
109  srv_checksum_algorithm_t algo) const {}
110 
111  /** Print innodb checksum and the checksum fields in page.
112  @param[in] checksum_field1 Checksum in page header
113  @param[in] checksum_field2 Checksum in page trailer */
114  virtual inline void print_strict_innodb(ulint checksum_field1,
115  ulint checksum_field2) const {}
116 
117  /** Print none checksum and the checksum fields in page.
118  @param[in] checksum_field1 Checksum in page header
119  @param[in] checksum_field2 Checksum in page trailer
120  @param[in] algo Current checksum algorithm */
121  virtual inline void print_strict_none(ulint checksum_field1,
122  ulint checksum_field2,
123  srv_checksum_algorithm_t algo) const {}
124 
125  /** Print innodb checksum value stored in page trailer.
126  @param[in] old_checksum checksum value according to old style
127  @param[in] new_checksum checksum value according to new style
128  @param[in] checksum_field1 Checksum in page header
129  @param[in] checksum_field2 Checksum in page trailer
130  @param[in] algo current checksum algorithm */
131  virtual inline void print_innodb_checksum(
132  ulint old_checksum, ulint new_checksum, ulint checksum_field1,
133  ulint checksum_field2, srv_checksum_algorithm_t algo) const {}
134 
135  /** Print the message that checksum mismatch happened in
136  page header. */
137  virtual inline void print_innodb_fail() const {}
138 
139  /** Print both new-style, old-style & crc32 checksum values.
140  @param[in] checksum_field1 Checksum in page header
141  @param[in] checksum_field2 Checksum in page trailer */
142  virtual inline void print_crc32_checksum(ulint checksum_field1,
143  ulint checksum_field2) const {}
144 
145  /** Print a message that crc32 check failed. */
146  virtual inline void print_crc32_fail() const {}
147 
148  /** Print a message that none check failed. */
149  virtual inline void print_none_fail() const {}
150 
151  /** Print checksum values on a compressed page.
152  @param[in] calc the calculated checksum value
153  @param[in] stored the stored checksum in header. */
154  virtual inline void print_compressed_checksum(ib_uint32_t calc,
155  ib_uint32_t stored) const {}
156 
157  /** Verify a compressed page's checksum.
158  @retval true if stored checksum is valid
159  according to the value of srv_checksum_algorithm
160  @retval false if stored schecksum is not valid
161  according to the value of srv_checksum_algorithm */
162  bool verify_zip_checksum() const;
163 
164  /** Calculate the compressed page checksum. This variant
165  should be used when only the page_size_t is unknown and
166  only physical page_size of compressed page is available.
167  @param[in] read_buf buffer holding the page
168  @param[in] phys_page_size physical page size
169  @param[in] algo checksum algorithm to use
170  @param[in] use_legacy_big_endian only used if algo is
171  SRV_CHECKSUM_ALGORITHM_CRC32 or SRV_CHECKSUM_ALGORITHM_STRICT_CRC32 -
172  if true then use big endian byteorder when converting byte strings to
173  integers.
174  @return page checksum */
175  uint32_t calc_zip_checksum(const byte *read_buf, ulint phys_page_size,
177  bool use_legacy_big_endian = false) const;
178 
179  /** Calculate the compressed page checksum.
180  @param[in] algo checksum algorithm to use
181  @param[in] use_legacy_big_endian only used if algo is
182  SRV_CHECKSUM_ALGORITHM_CRC32 or SRV_CHECKSUM_ALGORITHM_STRICT_CRC32 -
183  if true then use big endian byteorder when converting byte strings to
184  integers.
185  @return page checksum */
187  bool use_legacy_big_endian = false) const;
188 
189  private:
190  /** Checks if the page is in innodb checksum format.
191  @param[in] checksum_field1 new checksum field
192  @param[in] checksum_field2 old checksum field
193  @param[in] algo current checksum algorithm
194  @return true if the page is in innodb checksum format. */
195  bool is_checksum_valid_innodb(ulint checksum_field1, ulint checksum_field2,
196  const srv_checksum_algorithm_t algo) const;
197 
198  /** Checks if the page is in none checksum format.
199  @param[in] checksum_field1 new checksum field
200  @param[in] checksum_field2 old checksum field
201  @param[in] algo current checksum algorithm
202  @return true if the page is in none checksum format. */
203  bool is_checksum_valid_none(ulint checksum_field1, ulint checksum_field2,
204  const srv_checksum_algorithm_t algo) const;
205 
206  /** Checks if the page is in crc32 checksum format.
207  @param[in] checksum_field1 new checksum field
208  @param[in] checksum_field2 old checksum field
209  @param[in] algo current checksum algorithm
210  @param[in] use_legacy_big_endian big endian algorithm
211  @return true if the page is in crc32 checksum format. */
212  bool is_checksum_valid_crc32(ulint checksum_field1, ulint checksum_field2,
213  const srv_checksum_algorithm_t algo,
214  bool use_legacy_big_endian) const;
215 
216  /** Issue a warning when the checksum that is stored in the page is
217  valid, but different than the global setting innodb_checksum_algorithm.
218  @param[in] curr_algo current checksum algorithm
219  @param[in] page_checksum page valid checksum
220  @param[in] page_id page identifier */
222  srv_checksum_algorithm_t page_checksum,
223  const page_id_t &page_id) const;
224 
225  protected:
226  /** If true, do a LSN check during innodb recovery. */
228  /** Buffer holding the page. */
229  const byte *m_read_buf;
230  /** Page size. */
232  /** Skip checksum verification but compare only data. */
234 };
235 
236 #endif /* buf0checksum_h */
BlockReporter::print_none_fail
virtual void print_none_fail() const
Print a message that none check failed.
Definition: buf0checksum.h:149
BlockReporter::print_strict_none
virtual void print_strict_none(ulint checksum_field1, ulint checksum_field2, srv_checksum_algorithm_t algo) const
Print none checksum and the checksum fields in page.
Definition: buf0checksum.h:121
BlockReporter::print_crc32_checksum
virtual void print_crc32_checksum(ulint checksum_field1, ulint checksum_field2) const
Print both new-style, old-style & crc32 checksum values.
Definition: buf0checksum.h:142
BlockReporter::report_empty_page
virtual void report_empty_page(bool empty) const
Print message if page is empty.
Definition: buf0checksum.h:100
srv_checksum_algorithm
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:54
BlockReporter::BlockReporter
BlockReporter(bool check_lsn, const byte *read_buf, const page_size_t &page_size, bool skip_checksum)
Constructor.
Definition: buf0checksum.h:83
BlockReporter::m_read_buf
const byte * m_read_buf
Buffer holding the page.
Definition: buf0checksum.h:229
BlockReporter::calc_zip_checksum
uint32_t calc_zip_checksum(const byte *read_buf, ulint phys_page_size, srv_checksum_algorithm_t algo, bool use_legacy_big_endian=false) const
Calculate the compressed page checksum.
Definition: checksum.cc:506
BlockReporter::~BlockReporter
virtual ~BlockReporter()
Definition: buf0checksum.h:90
BlockReporter::print_compressed_checksum
virtual void print_compressed_checksum(ib_uint32_t calc, ib_uint32_t stored) const
Print checksum values on a compressed page.
Definition: buf0checksum.h:154
BlockReporter::is_checksum_valid_none
bool is_checksum_valid_none(ulint checksum_field1, ulint checksum_field2, const srv_checksum_algorithm_t algo) const
Checks if the page is in none checksum format.
Definition: checksum.cc:243
BlockReporter
Class to print checksums to log file.
Definition: buf0checksum.h:75
page_size_t
Page size descriptor.
Definition: page0size.h:50
page0size.h
BlockReporter::m_skip_checksum
bool m_skip_checksum
Skip checksum verification but compare only data.
Definition: buf0checksum.h:233
BlockReporter::m_page_size
const page_size_t & m_page_size
Page size.
Definition: buf0checksum.h:231
BlockReporter::is_checksum_valid_innodb
bool is_checksum_valid_innodb(ulint checksum_field1, ulint checksum_field2, const srv_checksum_algorithm_t algo) const
Checks if the page is in innodb checksum format.
Definition: checksum.cc:206
page
int page
Definition: ctype-mb.cc:1232
BlockReporter::print_innodb_checksum
virtual void print_innodb_checksum(ulint old_checksum, ulint new_checksum, ulint checksum_field1, ulint checksum_field2, srv_checksum_algorithm_t algo) const
Print innodb checksum value stored in page trailer.
Definition: buf0checksum.h:131
BlockReporter::print_strict_innodb
virtual void print_strict_innodb(ulint checksum_field1, ulint checksum_field2) const
Print innodb checksum and the checksum fields in page.
Definition: buf0checksum.h:114
buf_calc_page_crc32
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:68
BlockReporter::is_corrupted
bool is_corrupted() const
Checks if a page is corrupt.
Definition: checksum.cc:276
buf0types.h
BlockReporter::m_check_lsn
bool m_check_lsn
If true, do a LSN check during innodb recovery.
Definition: buf0checksum.h:227
BlockReporter::is_checksum_valid_crc32
bool is_checksum_valid_crc32(ulint checksum_field1, ulint 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:258
page_id_t
Page identifier.
Definition: buf0types.h:153
buf_checksum_algorithm_name
const char * buf_checksum_algorithm_name(srv_checksum_algorithm_t algo)
Return a printable string describing the checksum algorithm.
Definition: checksum.cc:137
BlockReporter::print_strict_crc32
virtual void print_strict_crc32(ulint checksum_field1, ulint checksum_field2, uint32_t crc32, srv_checksum_algorithm_t algo) const
Print crc32 checksum and the checksum fields in page.
Definition: buf0checksum.h:107
BlockReporter::print_crc32_fail
virtual void print_crc32_fail() const
Print a message that crc32 check failed.
Definition: buf0checksum.h:146
BlockReporter::page_warn_strict_checksum
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:699
buf_calc_page_old_checksum
ulint 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:124
ulong
unsigned long ulong
Definition: my_inttypes.h:48
BlockReporter::print_innodb_fail
virtual void print_innodb_fail() const
Print the message that checksum mismatch happened in page header.
Definition: buf0checksum.h:137
buf_calc_page_new_checksum
ulint 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:95
srv_checksum_algorithm_t
srv_checksum_algorithm_t
Alternatives for srv_checksum_algorithm, which can be changed by setting innodb_checksum_algorithm.
Definition: buf0types.h:97
BlockReporter::verify_zip_checksum
bool verify_zip_checksum() const
Verify a compressed page's checksum.
Definition: checksum.cc:555