| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2019, 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 |
/**************************************************/ /** |
| 29 |
@file include/log0files_io.h |
| 30 |
|
| 31 |
The log0files_io.{cc,h} is a low-level independent library for IO operations |
| 32 |
performed directly on redo log files. |
| 33 |
|
| 34 |
The library provides functions which allow to: |
| 35 |
- serialize, |
| 36 |
- deserialize, |
| 37 |
- read, |
| 38 |
- or write |
| 39 |
each kind of header stored in redo log files, and individual redo log blocks. |
| 40 |
|
| 41 |
NOTE: Parsing of individual redo records is NOT in scope of this library. |
| 42 |
|
| 43 |
@remarks |
| 44 |
|
| 45 |
Main goal for this library is to make IO operations simpler, no matter what |
| 46 |
is the motivation behind reading or writing fragments of redo log files. |
| 47 |
|
| 48 |
That's why: |
| 49 |
|
| 50 |
1. Functions defined in this library form a set of simple independent tools. |
| 51 |
They are state-less (they do not change state of the library, but obviously |
| 52 |
"write" functions might change redo log files). |
| 53 |
|
| 54 |
2. For each kind of redo header, three forms of the header are recognized: |
| 55 |
- structure with typed fields (e.g. struct Log_checkpoint_header), |
| 56 |
- array of bytes representing the serialized header, |
| 57 |
- data stored on disk. |
| 58 |
Functions to translate between any two of these forms are provided for each |
| 59 |
kind of header. In order to make life easier (when having to remind yourself |
| 60 |
what was the name for the given function), the following naming convention |
| 61 |
has been defined: |
| 62 |
- structure -> array of bytes: log_X_header_serialize, |
| 63 |
- array of bytes -> structure: log_X_header_deserialize, |
| 64 |
- structure -> disk: log_X_header_write(Log_file_id, ..., const The_struct& ), |
| 65 |
- disk -> structure: log_X_header_read(Log_file_id, ..., The_struct& ), |
| 66 |
- array of bytes -> disk: log_X_header_write(Log_file_id, ..., const byte* ), |
| 67 |
- disk -> array of bytes: log_X_header_read(Log_file_id, ..., byte* ). |
| 68 |
|
| 69 |
@note There is no structure with typed fields for encryption header (yet) and |
| 70 |
for redo data blocks. |
| 71 |
|
| 72 |
3. The functions defined in this library MUST NOT depend on log_t or recovery |
| 73 |
implementation, because this library is designed to be lightweight and easy |
| 74 |
to use. |
| 75 |
|
| 76 |
@note |
| 77 |
|
| 78 |
Functions that operate on set of redo files are also part of this library. |
| 79 |
This includes functions to: |
| 80 |
- build a path to the redo log file with the given id, |
| 81 |
- list existing redo files, |
| 82 |
- remove redo files, |
| 83 |
- create empty redo files, |
| 84 |
- mark/unmark individual redo files as unused. |
| 85 |
|
| 86 |
*******************************************************/ |
| 87 |
|
| 88 |
#ifndef log0files_io_h |
| 89 |
#define log0files_io_h |
| 90 |
|
| 91 |
#include <string> |
| 92 |
|
| 93 |
/* Log_file_header, Log_checkpoint_header, LOG_BLOCK_HDR_NO */ |
| 94 |
#include "log0types.h" |
| 95 |
|
| 96 |
/* mach_read_from_X, mach_write_to_X */ |
| 97 |
#include "mach0data.h" |
| 98 |
|
| 99 |
/* ut_crc32() */ |
| 100 |
#include "ut0crc32.h" |
| 101 |
|
| 102 |
/* ut::vector */ |
| 103 |
#include "ut0new.h" |
| 104 |
|
| 105 |
/** Atomic pointer to the log checksum calculation function. This is actually |
| 106 |
the only remaining "state" of the library. Hopefully can become removed. */ |
| 107 |
extern Log_checksum_algorithm_atomic_ptr log_checksum_algorithm_ptr; |
| 108 |
|
| 109 |
/** Computes checksum of the given header and verifies if the checksum |
| 110 |
is the same as the one stored in that header. |
| 111 |
@param[in] buf header to verify |
| 112 |
@return true iff checksums are the same */ |
| 113 |
bool log_header_checksum_is_ok(const byte *buf); |
| 114 |
|
| 115 |
/**************************************************/ /** |
| 116 |
|
| 117 |
@name Log - file header read/write. |
| 118 |
|
| 119 |
*******************************************************/ |
| 120 |
|
| 121 |
/** @{ */ |
| 122 |
|
| 123 |
/** Serializes the log file header to the buffer. |
| 124 |
@param[in] header the header to serialize |
| 125 |
@param[out] buf the allocated buffer */ |
| 126 |
void log_file_header_serialize(const Log_file_header &header, byte *buf); |
| 127 |
|
| 128 |
/** Deserializes the log file header stored in the buffer. |
| 129 |
@param[in] buf the buffer to deserialize |
| 130 |
@param[out] header the deserialized header |
| 131 |
@return true iff checksum is correct */ |
| 132 |
bool log_file_header_deserialize(const byte *buf, Log_file_header &header); |
| 133 |
|
| 134 |
/** Serializes and writes the log file header to the log file. |
| 135 |
@param[in] file_handle handle for the opened log file |
| 136 |
@param[in] header the file header |
| 137 |
@return DB_SUCCESS or error */ |
| 138 |
dberr_t log_file_header_write(Log_file_handle &file_handle, |
| 139 |
const Log_file_header &header); |
| 140 |
|
| 141 |
/** Writes the serialized log file header to the log file. |
| 142 |
@param[in] file_handle handle for the opened log file |
| 143 |
@param[in] buf the serialized file header |
| 144 |
@return DB_SUCCESS or error */ |
| 145 |
dberr_t log_file_header_write(Log_file_handle &file_handle, const byte *buf); |
| 146 |
|
| 147 |
/** Reads the serialized log file header to the buffer. |
| 148 |
@param[in] file_handle handle for the opened log file |
| 149 |
@param[out] buf the allocated buffer for read |
| 150 |
@return DB_SUCCESS or error */ |
| 151 |
dberr_t log_file_header_read(Log_file_handle &file_handle, byte *buf); |
| 152 |
|
| 153 |
/** Reads and deserializes the log file header. |
| 154 |
@param[in] file_handle handle for the opened log file |
| 155 |
@param[out] header the file header read |
| 156 |
@return DB_SUCCESS or error */ |
| 157 |
dberr_t log_file_header_read(Log_file_handle &file_handle, |
| 158 |
Log_file_header &header); |
| 159 |
|
| 160 |
/** Sets a specific flag in the mask built of redo log flags. |
| 161 |
@param[in] log_flags mask of log flags |
| 162 |
@param[in] bit bit number to set (flag) */ |
| 163 |
void log_file_header_set_flag(Log_flags &log_flags, uint32_t bit); |
| 164 |
|
| 165 |
/** Resets a specific flag in the mask built of redo log flags. |
| 166 |
@param[in] log_flags mask of log flags |
| 167 |
@param[in] bit bit number to set (flag) */ |
| 168 |
void log_file_header_reset_flag(Log_flags &log_flags, uint32_t bit); |
| 169 |
|
| 170 |
/** Checks if a specific flag is set in the mask built of redo log flags. |
| 171 |
@param[in] log_flags mask of log flags |
| 172 |
@param[in] bit bit number to check (flag) |
| 173 |
@return true, iff flag is set */ |
| 174 |
bool log_file_header_check_flag(Log_flags log_flags, uint32_t bit); |
| 175 |
|
| 176 |
/** @} */ |
| 177 |
|
| 178 |
/**************************************************/ /** |
| 179 |
|
| 180 |
@name Log - encryption header read/write. |
| 181 |
|
| 182 |
*******************************************************/ |
| 183 |
|
| 184 |
/** @{ */ |
| 185 |
|
| 186 |
/** Writes the serialized encryption meta data to the log file. |
| 187 |
@param[in] file_handle handle for the opened log file |
| 188 |
@param[in] buf the filled encryption buffer to write |
| 189 |
@return DB_SUCCESS or error */ |
| 190 |
dberr_t log_encryption_header_write(Log_file_handle &file_handle, |
| 191 |
const byte *buf); |
| 192 |
|
| 193 |
/** Reads the serialized encryption meta data from the log file. |
| 194 |
@param[in] file_handle handle for the opened log file |
| 195 |
@param[out] buf the allocated buffer for read |
| 196 |
@return DB_SUCCESS or error */ |
| 197 |
dberr_t log_encryption_header_read(Log_file_handle &file_handle, byte *buf); |
| 198 |
|
| 199 |
/** @} */ |
| 200 |
|
| 201 |
/**************************************************/ /** |
| 202 |
|
| 203 |
@name Log - checkpoint header read/write. |
| 204 |
|
| 205 |
*******************************************************/ |
| 206 |
|
| 207 |
/** @{ */ |
| 208 |
|
| 209 |
/** Serializes the log checkpoint header to the buffer. |
| 210 |
@param[in] header the header to serialize |
| 211 |
@param[out] buf the allocated buffer */ |
| 212 |
void log_checkpoint_header_serialize(const Log_checkpoint_header &header, |
| 213 |
byte *buf); |
| 214 |
|
| 215 |
/** Deserializes the log checkpoint header stored in the buffer. |
| 216 |
@param[in] buf the buffer to deserialize |
| 217 |
@param[out] header the deserialized header |
| 218 |
@return true iff checksum is correct */ |
| 219 |
bool log_checkpoint_header_deserialize(const byte *buf, |
| 220 |
Log_checkpoint_header &header); |
| 221 |
|
| 222 |
/** Serializes and writes the log checkpoint header to the log file. |
| 223 |
@param[in] file_handle handle for the opened log file |
| 224 |
@param[in] checkpoint_header_no checkpoint header to be written |
| 225 |
@param[in] header the checkpoint header |
| 226 |
@return DB_SUCCESS or error */ |
| 227 |
dberr_t log_checkpoint_header_write( |
| 228 |
Log_file_handle &file_handle, Log_checkpoint_header_no checkpoint_header_no, |
| 229 |
const Log_checkpoint_header &header); |
| 230 |
|
| 231 |
/** Writes the serialized checkpoint header to the log file. |
| 232 |
@param[in] file_handle handle for the opened log file |
| 233 |
@param[in] checkpoint_header_no checkpoint header to be written |
| 234 |
@param[in] buf buffer containing the serialized checkpoint |
| 235 |
header to write |
| 236 |
@return DB_SUCCESS or error */ |
| 237 |
dberr_t log_checkpoint_header_write( |
| 238 |
Log_file_handle &file_handle, Log_checkpoint_header_no checkpoint_header_no, |
| 239 |
const byte *buf); |
| 240 |
|
| 241 |
/** Reads the serialized log checkpoint header to the buffer. |
| 242 |
@param[in] file_handle handle for the opened log file |
| 243 |
@param[in] checkpoint_header_no checkpoint header to read |
| 244 |
@param[out] buf the allocated buffer for read |
| 245 |
@return DB_SUCCESS or error */ |
| 246 |
dberr_t log_checkpoint_header_read( |
| 247 |
Log_file_handle &file_handle, Log_checkpoint_header_no checkpoint_header_no, |
| 248 |
byte *buf); |
| 249 |
|
| 250 |
/** Reads and deserializes the log checkpoint header. |
| 251 |
@param[in] file_handle handle for the opened log file |
| 252 |
@param[in] checkpoint_header_no checkpoint header to read |
| 253 |
@param[out] header the checkpoint header read |
| 254 |
@return DB_SUCCESS or error */ |
| 255 |
dberr_t log_checkpoint_header_read( |
| 256 |
Log_file_handle &file_handle, Log_checkpoint_header_no checkpoint_header_no, |
| 257 |
Log_checkpoint_header &header); |
| 258 |
|
| 259 |
/** @} */ |
| 260 |
|
| 261 |
/**************************************************/ /** |
| 262 |
|
| 263 |
@name Log functions - data blocks read/write. |
| 264 |
|
| 265 |
*******************************************************/ |
| 266 |
|
| 267 |
/** @{ */ |
| 268 |
|
| 269 |
/** Writes the formatted log blocks with redo records to the log file. |
| 270 |
The given log blocks must fit within the same single log file. |
| 271 |
@param[in] file_handle handle for the opened log file |
| 272 |
@param[in] write_offset offset from the beginning of the given file |
| 273 |
@param[in] write_size size of the data to write (must be divisible |
| 274 |
by OS_FILE_LOG_BLOCK_SIZE) |
| 275 |
@param[in] buf formatted log blocks with the data to write |
| 276 |
@return DB_SUCCESS or error */ |
| 277 |
dberr_t log_data_blocks_write(Log_file_handle &file_handle, |
| 278 |
os_offset_t write_offset, size_t write_size, |
| 279 |
const byte *buf); |
| 280 |
|
| 281 |
/** Reads log blocks with redo records from the log file, starting at |
| 282 |
the given offset. The log blocks must exist within single log file. |
| 283 |
@param[in] file_handle handle for the opened log file |
| 284 |
@param[in] read_offset offset from the beginning of the given file |
| 285 |
@param[in] read_size size of the data to read (must be divisible |
| 286 |
by OS_FILE_LOG_BLOCK_SIZE) |
| 287 |
@param[out] buf allocated buffer to fill by the read |
| 288 |
@return DB_SUCCESS or error */ |
| 289 |
dberr_t log_data_blocks_read(Log_file_handle &file_handle, |
| 290 |
os_offset_t read_offset, size_t read_size, |
| 291 |
byte *buf); |
| 292 |
|
| 293 |
/** @} */ |
| 294 |
|
| 295 |
/**************************************************/ /** |
| 296 |
|
| 297 |
@name Log - files creation/deletion, path computation. |
| 298 |
|
| 299 |
*******************************************************/ |
| 300 |
|
| 301 |
/** @{ */ |
| 302 |
|
| 303 |
/** Provides path to directory with redo log files. |
| 304 |
@param[in] ctx context within which files exist |
| 305 |
@return path to #innodb_redo directory */ |
| 306 |
std::string log_directory_path(const Log_files_context &ctx); |
| 307 |
|
| 308 |
/** Provides name of the log file with the given file id, e.g. '#ib_redo0'. |
| 309 |
@param[in] ctx context within which files exist |
| 310 |
@param[in] file_id id of the log file |
| 311 |
@return file name */ |
| 312 |
std::string log_file_name(const Log_files_context &ctx, Log_file_id file_id); |
| 313 |
|
| 314 |
/** Provides full path to the log file, e.g. '/data/#innodb_redo/#ib_redo2'. |
| 315 |
@param[in] ctx context within which files exist |
| 316 |
@param[in] file_id id of the log file |
| 317 |
@return path to the log file (including file name) */ |
| 318 |
std::string log_file_path(const Log_files_context &ctx, Log_file_id file_id); |
| 319 |
|
| 320 |
/** Provides full path to the temporary log file, |
| 321 |
e.g. '/data/#innodb_redo/#ib_redo2_tmp'. |
| 322 |
@param[in] ctx context within which files exist |
| 323 |
@param[in] file_id id of the file |
| 324 |
@return path to the temporary log file (including file name) */ |
| 325 |
std::string log_file_path_for_unused_file(const Log_files_context &ctx, |
| 326 |
Log_file_id file_id); |
| 327 |
|
| 328 |
/** List existing log files in the directory (does not include unused files). |
| 329 |
@param[in] ctx context within which files exist |
| 330 |
@param[out] ret identifiers of existing log files |
| 331 |
@return DB_SUCCESS or DB_ERROR */ |
| 332 |
dberr_t log_list_existing_files(const Log_files_context &ctx, |
| 333 |
ut::vector<Log_file_id> &ret); |
| 334 |
|
| 335 |
/** List existing unused log files in the directory. |
| 336 |
@param[in] ctx context within which files exist |
| 337 |
@param[out] ret identifiers of existing unused log files |
| 338 |
@return DB_SUCCESS or DB_ERROR */ |
| 339 |
dberr_t log_list_existing_unused_files(const Log_files_context &ctx, |
| 340 |
ut::vector<Log_file_id> &ret); |
| 341 |
|
| 342 |
/** Renames the unused file to another unused file. |
| 343 |
@param[in] ctx context within which files exist |
| 344 |
@param[in] old_unused_file_id id of file to rename |
| 345 |
@param[in] new_unused_file_id new file id |
| 346 |
@return DB_SUCCESS or DB_ERROR */ |
| 347 |
dberr_t log_rename_unused_file(const Log_files_context &ctx, |
| 348 |
Log_file_id old_unused_file_id, |
| 349 |
Log_file_id new_unused_file_id); |
| 350 |
|
| 351 |
/** Renames a temporary log file to the non-temporary log file. |
| 352 |
@param[in] ctx context within which files exist |
| 353 |
@param[in] file_id id of the file to rename |
| 354 |
@return DB_SUCCESS or DB_ERROR */ |
| 355 |
dberr_t log_mark_file_as_in_use(const Log_files_context &ctx, |
| 356 |
Log_file_id file_id); |
| 357 |
|
| 358 |
/** Renames a non-temporary log file to the temporary log file. |
| 359 |
@param[in] ctx context within which files exist |
| 360 |
@param[in] file_id id of the file to rename |
| 361 |
@return DB_SUCCESS or DB_ERROR */ |
| 362 |
dberr_t log_mark_file_as_unused(const Log_files_context &ctx, |
| 363 |
Log_file_id file_id); |
| 364 |
|
| 365 |
/** Removes a temporary log file, if it existed. |
| 366 |
@param[in] ctx context within which files exist |
| 367 |
@param[in] file_id id of the file to remove |
| 368 |
@return DB_SUCCESS, DB_NOT_FOUND or DB_ERROR */ |
| 369 |
dberr_t log_remove_unused_file(const Log_files_context &ctx, |
| 370 |
Log_file_id file_id); |
| 371 |
|
| 372 |
/** Removes all temporary log files in the directory. When failed to |
| 373 |
remove a file, stops and returns error. In such case the last element |
| 374 |
of the returned identifiers of files, represents the file for which |
| 375 |
error has been encountered when trying to remove it. |
| 376 |
@param[in] ctx context within which files exist |
| 377 |
@return first: DB_SUCCESS or DB_ERROR |
| 378 |
second: identifiers of files for which remove has been called */ |
| 379 |
std::pair<dberr_t, ut::vector<Log_file_id>> log_remove_unused_files( |
| 380 |
const Log_files_context &ctx); |
| 381 |
|
| 382 |
/** Removes a log file, if it existed. |
| 383 |
@param[in] ctx context within which files exist |
| 384 |
@param[in] file_id id of the file to remove |
| 385 |
@return DB_SUCCESS, DB_NOT_FOUND or DB_ERROR */ |
| 386 |
dberr_t log_remove_file(const Log_files_context &ctx, Log_file_id file_id); |
| 387 |
|
| 388 |
/** Removes a single existing log file (if it existed). |
| 389 |
@param[in] ctx context within which files exist |
| 390 |
@return first: DB_SUCCESS, DB_NOT_FOUND or DB_ERROR |
| 391 |
second: id of the removed file (if removed) */ |
| 392 |
std::pair<dberr_t, Log_file_id> log_remove_file(const Log_files_context &ctx); |
| 393 |
|
| 394 |
/** Removes existing log files. When failed to remove a file, stops and |
| 395 |
returns error. In such case the last element of the returned identifiers |
| 396 |
of files, represents the file for which error has been encountered when |
| 397 |
trying to remove it. |
| 398 |
@param[in] ctx context within which files exist |
| 399 |
@return first: DB_SUCCESS or DB_ERROR |
| 400 |
second: identifiers of files for which remove has been called */ |
| 401 |
std::pair<dberr_t, ut::vector<Log_file_id>> log_remove_files( |
| 402 |
const Log_files_context &ctx); |
| 403 |
|
| 404 |
/** Creates a new temporary log file and resizes the file to the given size. |
| 405 |
@param[in] ctx context within which files exist |
| 406 |
@param[in] file_id id of the file to create |
| 407 |
@param[in] size_in_bytes size of the file, in bytes |
| 408 |
@return DB_SUCCESS or DB_ERROR */ |
| 409 |
dberr_t log_create_unused_file(const Log_files_context &ctx, |
| 410 |
Log_file_id file_id, os_offset_t size_in_bytes); |
| 411 |
|
| 412 |
/** Resizes an existing temporary log file to the given size. |
| 413 |
@param[in] ctx context within which files exist |
| 414 |
@param[in] file_id id of the file to resize |
| 415 |
@param[in] size_in_bytes requested size of the file, in bytes |
| 416 |
@return DB_SUCCESS, DB_NOT_FOUND, DB_OUT_OF_DISK_SPACE or DB_ERROR */ |
| 417 |
dberr_t log_resize_unused_file(const Log_files_context &ctx, |
| 418 |
Log_file_id file_id, os_offset_t size_in_bytes); |
| 419 |
|
| 420 |
/** Resizes an existing log file to the given size. |
| 421 |
@param[in] ctx context within which files exist |
| 422 |
@param[in] file_id id of the file to resize |
| 423 |
@param[in] size_in_bytes requested size of the file, in bytes |
| 424 |
@return DB_SUCCESS, DB_NOT_FOUND, DB_OUT_OF_DISK_SPACE or DB_ERROR */ |
| 425 |
dberr_t log_resize_file(const Log_files_context &ctx, Log_file_id file_id, |
| 426 |
os_offset_t size_in_bytes); |
| 427 |
|
| 428 |
/** Searches for all possible log files existing on disk in the log directory. |
| 429 |
Performs only very minimal validation of the files, checking if files could be |
| 430 |
opened and have valid file size. |
| 431 |
@param[in] ctx context within which files exist |
| 432 |
@param[in] read_only true: check file permissions only for reading, |
| 433 |
false: check for both reading and writing |
| 434 |
@param[out] found list of <file_id, size of file> for each file found |
| 435 |
@return DB_SUCCESS, DB_NOT_FOUND or DB_ERROR */ |
| 436 |
dberr_t log_collect_existing_files(const Log_files_context &ctx, bool read_only, |
| 437 |
ut::vector<Log_file_id_and_size> &found); |
| 438 |
|
| 439 |
/** Generate unique identifier for the redo log files. |
| 440 |
@return random uuid > 0 */ |
| 441 |
Log_uuid log_generate_uuid(); |
| 442 |
|
| 443 |
/** @} */ |
| 444 |
|
| 445 |
/**************************************************/ /** |
| 446 |
|
| 447 |
@name Log - log blocks format. |
| 448 |
|
| 449 |
*******************************************************/ |
| 450 |
|
| 451 |
/** @{ */ |
| 452 |
|
| 453 |
/* Definition of inline functions. */ |
| 454 |
|
| 455 |
/** Gets a log block number stored in the header. The number corresponds |
| 456 |
to lsn range for data stored in the block. |
| 457 |
|
| 458 |
During recovery, when a next block is being parsed, a next range of lsn |
| 459 |
values is expected to be read. This corresponds to a log block number |
| 460 |
increased by one (modulo LOG_BLOCK_MAX_NO). However, if an unexpected |
| 461 |
number is read from the header, it is then considered the end of the |
| 462 |
redo log and recovery is finished. In such case, the next block is most |
| 463 |
likely an empty block or a block from the past, because the redo log |
| 464 |
files might be reused. |
| 465 |
|
| 466 |
@param[in] log_block log block (may be invalid or empty block) |
| 467 |
@return log block number stored in the block header */ |
| 468 |
inline uint32_t log_block_get_hdr_no(const byte *log_block) { |
| 469 |
return ~LOG_BLOCK_FLUSH_BIT_MASK & |
| 470 |
mach_read_from_4(log_block + LOG_BLOCK_HDR_NO); |
| 471 |
} |
| 472 |
|
| 473 |
/** Sets the log block number stored in the header. |
| 474 |
NOTE that this must be set before the flush bit! |
| 475 |
|
| 476 |
@param[in,out] log_block log block |
| 477 |
@param[in] n log block number: must be in (0, 1G] */ |
| 478 |
inline void log_block_set_hdr_no(byte *log_block, uint32_t n) { |
| 479 |
ut_a(n > 0); |
| 480 |
ut_a(n < LOG_BLOCK_FLUSH_BIT_MASK); |
| 481 |
ut_a(n <= LOG_BLOCK_MAX_NO); |
| 482 |
mach_write_to_4(log_block + LOG_BLOCK_HDR_NO, n); |
| 483 |
} |
| 484 |
|
| 485 |
/** Gets a log block data length. |
| 486 |
@param[in] log_block log block |
| 487 |
@return log block data length measured as a byte offset from the block start */ |
| 488 |
inline uint32_t log_block_get_data_len(const byte *log_block) { |
| 489 |
return mach_read_from_2(log_block + LOG_BLOCK_HDR_DATA_LEN); |
| 490 |
} |
| 491 |
|
| 492 |
/** Sets the log block data length. |
| 493 |
@param[in,out] log_block log block |
| 494 |
@param[in] len data length (@see log_block_get_data_len) */ |
| 495 |
inline void log_block_set_data_len(byte *log_block, uint32_t len) { |
| 496 |
mach_write_to_2(log_block + LOG_BLOCK_HDR_DATA_LEN, len); |
| 497 |
} |
| 498 |
|
| 499 |
/** Gets an offset to the beginning of the first group of log records |
| 500 |
in a given log block. |
| 501 |
@param[in] log_block log block |
| 502 |
@return first mtr log record group byte offset from the block start, |
| 503 |
0 if none. */ |
| 504 |
inline uint32_t log_block_get_first_rec_group(const byte *log_block) { |
| 505 |
return mach_read_from_2(log_block + LOG_BLOCK_FIRST_REC_GROUP); |
| 506 |
} |
| 507 |
|
| 508 |
/** Sets an offset to the beginning of the first group of log records |
| 509 |
in a given log block. |
| 510 |
@param[in,out] log_block log block |
| 511 |
@param[in] offset offset, 0 if none */ |
| 512 |
inline void log_block_set_first_rec_group(byte *log_block, uint32_t offset) { |
| 513 |
mach_write_to_2(log_block + LOG_BLOCK_FIRST_REC_GROUP, offset); |
| 514 |
} |
| 515 |
|
| 516 |
/** Gets a log block epoch_no. For details: @see LOG_BLOCK_EPOCH_NO. |
| 517 |
@param[in] log_block log block |
| 518 |
@return epoch number */ |
| 519 |
inline uint32_t log_block_get_epoch_no(const byte *log_block) { |
| 520 |
return mach_read_from_4(log_block + LOG_BLOCK_EPOCH_NO); |
| 521 |
} |
| 522 |
|
| 523 |
/** Sets a log block epoch_no. For details: @see LOG_BLOCK_EPOCH_NO. |
| 524 |
@param[in,out] log_block log block |
| 525 |
@param[in] no epoch number */ |
| 526 |
inline void log_block_set_epoch_no(byte *log_block, uint32_t no) { |
| 527 |
mach_write_to_4(log_block + LOG_BLOCK_EPOCH_NO, no); |
| 528 |
} |
| 529 |
|
| 530 |
/** Converts a lsn to a log block epoch number. |
| 531 |
For details @see LOG_BLOCK_EPOCH_NO. |
| 532 |
@param[in] lsn lsn of a byte within the block |
| 533 |
@return log block epoch number, it is > 0 */ |
| 534 |
inline uint32_t log_block_convert_lsn_to_epoch_no(lsn_t lsn) { |
| 535 |
return 1 + |
| 536 |
static_cast<uint32_t>(lsn / OS_FILE_LOG_BLOCK_SIZE / LOG_BLOCK_MAX_NO); |
| 537 |
} |
| 538 |
|
| 539 |
/** Converts a lsn to a log block number. Consecutive log blocks have |
| 540 |
consecutive numbers (unless the sequence wraps). It is guaranteed that |
| 541 |
the calculated number is greater than zero. |
| 542 |
|
| 543 |
@param[in] lsn lsn of a byte within the block |
| 544 |
@return log block number, it is > 0 and <= 1G */ |
| 545 |
inline uint32_t log_block_convert_lsn_to_hdr_no(lsn_t lsn) { |
| 546 |
return 1 + |
| 547 |
static_cast<uint32_t>(lsn / OS_FILE_LOG_BLOCK_SIZE % LOG_BLOCK_MAX_NO); |
| 548 |
} |
| 549 |
|
| 550 |
/** Calculates the checksum for a log block. |
| 551 |
@param[in] log_block log block |
| 552 |
@return checksum */ |
| 553 |
inline uint32_t log_block_calc_checksum(const byte *log_block) { |
| 554 |
return log_checksum_algorithm_ptr.load()(log_block); |
| 555 |
} |
| 556 |
|
| 557 |
/** Calculates the checksum for a log block using the MySQL 5.7 algorithm. |
| 558 |
@param[in] log_block log block |
| 559 |
@return checksum */ |
| 560 |
inline uint32_t log_block_calc_checksum_crc32(const byte *log_block) { |
| 561 |
return ut_crc32(log_block, OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE); |
| 562 |
} |
| 563 |
|
| 564 |
/** Calculates the checksum for a log block using the "no-op" algorithm. |
| 565 |
@return checksum */ |
| 566 |
inline uint32_t log_block_calc_checksum_none(const byte *) { |
| 567 |
return LOG_NO_CHECKSUM_MAGIC; |
| 568 |
} |
| 569 |
|
| 570 |
/** Gets value of a log block checksum field. |
| 571 |
@param[in] log_block log block |
| 572 |
@return checksum */ |
| 573 |
inline uint32_t log_block_get_checksum(const byte *log_block) { |
| 574 |
return mach_read_from_4(log_block + OS_FILE_LOG_BLOCK_SIZE - |
| 575 |
LOG_BLOCK_CHECKSUM); |
| 576 |
} |
| 577 |
|
| 578 |
/** Sets value of a log block checksum field. |
| 579 |
@param[in,out] log_block log block |
| 580 |
@param[in] checksum checksum */ |
| 581 |
inline void log_block_set_checksum(byte *log_block, uint32_t checksum) { |
| 582 |
mach_write_to_4(log_block + OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_CHECKSUM, |
| 583 |
checksum); |
| 584 |
} |
| 585 |
|
| 586 |
/** Stores a 4-byte checksum to the trailer checksum field of a log block. |
| 587 |
This is used before writing the log block to disk. The checksum in a log |
| 588 |
block is used in recovery to check the consistency of the log block. |
| 589 |
@param[in] log_block log block (completely filled in!) */ |
| 590 |
inline void log_block_store_checksum(byte *log_block) { |
| 591 |
log_block_set_checksum(log_block, log_block_calc_checksum(log_block)); |
| 592 |
} |
| 593 |
|
| 594 |
/** Gets value of a log block encrypt bit (true or false). |
| 595 |
@param[in] log_block log block |
| 596 |
@return true iff encrypt bit is set */ |
| 597 |
inline bool log_block_get_encrypt_bit(const byte *log_block) { |
| 598 |
if (LOG_BLOCK_ENCRYPT_BIT_MASK & |
| 599 |
mach_read_from_2(log_block + LOG_BLOCK_HDR_DATA_LEN)) { |
| 600 |
return true; |
| 601 |
} |
| 602 |
|
| 603 |
return false; |
| 604 |
} |
| 605 |
|
| 606 |
/** Sets value of a log block encrypt bit (true or false). |
| 607 |
@param[in] log_block log block to modify |
| 608 |
@param[in] val the value to set (true or false) */ |
| 609 |
inline void log_block_set_encrypt_bit(byte *log_block, bool val) { |
| 610 |
uint32_t field; |
| 611 |
|
| 612 |
field = mach_read_from_2(log_block + LOG_BLOCK_HDR_DATA_LEN); |
| 613 |
|
| 614 |
if (val) { |
| 615 |
field = field | LOG_BLOCK_ENCRYPT_BIT_MASK; |
| 616 |
} else { |
| 617 |
field = field & ~LOG_BLOCK_ENCRYPT_BIT_MASK; |
| 618 |
} |
| 619 |
|
| 620 |
mach_write_to_2(log_block + LOG_BLOCK_HDR_DATA_LEN, field); |
| 621 |
} |
| 622 |
|
| 623 |
/** Serializes the log data block header to the redo log block buffer which |
| 624 |
already contains redo log data (must have the redo data before this call). |
| 625 |
@param[in] header the header to serialize |
| 626 |
@param[out] buf the buffer containing the redo log block with the data */ |
| 627 |
inline void log_data_block_header_serialize(const Log_data_block_header &header, |
| 628 |
byte *buf) { |
| 629 |
log_block_set_epoch_no(buf, header.m_epoch_no); |
| 630 |
log_block_set_hdr_no(buf, header.m_hdr_no); |
| 631 |
log_block_set_data_len(buf, header.m_data_len); |
| 632 |
log_block_set_first_rec_group(buf, header.m_first_rec_group); |
| 633 |
log_block_store_checksum(buf); |
| 634 |
} |
| 635 |
|
| 636 |
/** Deserializes the log data block header stored in the buffer. |
| 637 |
@param[in] buf the buffer to deserialize |
| 638 |
@param[out] header the deserialized header |
| 639 |
@return true iff checksum is correct */ |
| 640 |
inline bool log_data_block_header_deserialize(const byte *buf, |
| 641 |
Log_data_block_header &header) { |
| 642 |
header.m_epoch_no = log_block_get_epoch_no(buf); |
| 643 |
header.m_hdr_no = log_block_get_hdr_no(buf); |
| 644 |
header.m_data_len = log_block_get_data_len(buf); |
| 645 |
header.m_first_rec_group = log_block_get_first_rec_group(buf); |
| 646 |
return log_block_calc_checksum(buf) == log_block_get_checksum(buf); |
| 647 |
} |
| 648 |
|
| 649 |
/** @} */ |
| 650 |
|
| 651 |
#endif /* !log0files_io_h */ |