| 1 |
/*********************************************************************** |
| 2 |
Copyright (c) 1995, 2026, Oracle and/or its affiliates. |
| 3 |
Copyright (c) 2009, Percona Inc. |
| 4 |
|
| 5 |
Portions of this file contain modifications contributed and copyrighted |
| 6 |
by Percona Inc.. Those modifications are |
| 7 |
gratefully acknowledged and are described briefly in the InnoDB |
| 8 |
documentation. The contributions by Percona Inc. are incorporated with |
| 9 |
their permission, and subject to the conditions contained in the file |
| 10 |
COPYING.Percona. |
| 11 |
|
| 12 |
This program is free software; you can redistribute it and/or modify |
| 13 |
it under the terms of the GNU General Public License, version 2.0, |
| 14 |
as published by the Free Software Foundation. |
| 15 |
|
| 16 |
This program is designed to work with certain software (including |
| 17 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 18 |
as designated in a particular file or component or in included license |
| 19 |
documentation. The authors of MySQL hereby grant you an additional |
| 20 |
permission to link the program and your derivative works with the |
| 21 |
separately licensed software that they have either included with |
| 22 |
the program or referenced in the documentation. |
| 23 |
|
| 24 |
This program is distributed in the hope that it will be useful, |
| 25 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 |
GNU General Public License, version 2.0, for more details. |
| 28 |
|
| 29 |
You should have received a copy of the GNU General Public License |
| 30 |
along with this program; if not, write to the Free Software |
| 31 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 32 |
|
| 33 |
***********************************************************************/ |
| 34 |
|
| 35 |
/** @file os/file.cc |
| 36 |
The interface to the operating system file i/o primitives |
| 37 |
|
| 38 |
Created 10/21/1995 Heikki Tuuri |
| 39 |
*******************************************************/ |
| 40 |
|
| 41 |
/* NOTE: The functions in this file should only use functions from |
| 42 |
other files in library. The code in this file is used to make a library for |
| 43 |
external tools. */ |
| 44 |
|
| 45 |
#include "buf0checksum.h" |
| 46 |
#include "db0err.h" |
| 47 |
#include "fil0fil.h" |
| 48 |
#include "mach0data.h" |
| 49 |
#include "os0file.h" |
| 50 |
#include "univ.i" |
| 51 |
|
| 52 |
#include <lz4.h> |
| 53 |
#include <zlib.h> |
| 54 |
|
| 55 |
/** Convert to a "string". |
| 56 |
@param[in] type The compression type |
| 57 |
@return the string representation */ |
| 58 |
const char *Compression::to_string(Type type) { |
| 59 |
switch (type) { |
| 60 |
case NONE: |
| 61 |
return ("None"); |
| 62 |
case ZLIB: |
| 63 |
return ("Zlib"); |
| 64 |
case LZ4: |
| 65 |
return ("LZ4"); |
| 66 |
} |
| 67 |
|
| 68 |
ut_d(ut_error); |
| 69 |
|
| 70 |
ut_o(return ("<UNKNOWN>")); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
@param[in] meta Page Meta data |
| 75 |
@return the string representation */ |
| 76 |
std::string Compression::to_string(const Compression::meta_t &meta) { |
| 77 |
std::ostringstream stream; |
| 78 |
|
| 79 |
stream << "version: " << int(meta.m_version) << " " |
| 80 |
<< "algorithm: " << meta.m_algorithm << " " |
| 81 |
<< "(" << to_string(meta.m_algorithm) << ") " |
| 82 |
<< "orginal_type: " << meta.m_original_type << " " |
| 83 |
<< "original_size: " << meta.m_original_size << " " |
| 84 |
<< "compressed_size: " << meta.m_compressed_size; |
| 85 |
|
| 86 |
return (stream.str()); |
| 87 |
} |
| 88 |
|
| 89 |
/** @return true if it is a compressed page */ |
| 90 |
bool Compression::is_compressed_page(const byte *page) { |
| 91 |
return (mach_read_from_2(page + FIL_PAGE_TYPE) == FIL_PAGE_COMPRESSED); |
| 92 |
} |
| 93 |
|
| 94 |
bool Compression::is_compressed_encrypted_page(const byte *page) { |
| 95 |
return (mach_read_from_2(page + FIL_PAGE_TYPE) == |
| 96 |
FIL_PAGE_COMPRESSED_AND_ENCRYPTED); |
| 97 |
} |
| 98 |
|
| 99 |
bool Compression::is_valid_page_version(uint8_t version) { |
| 100 |
return (version == FIL_PAGE_VERSION_1 || version == FIL_PAGE_VERSION_2); |
| 101 |
} |
| 102 |
|
| 103 |
/** Deserizlise the page header compression meta-data |
| 104 |
@param[in] page Pointer to the page header |
| 105 |
@param[out] control Deserialised data */ |
| 106 |
void Compression::deserialize_header(const byte *page, |
| 107 |
Compression::meta_t *control) { |
| 108 |
ut_ad(is_compressed_page(page) || is_compressed_encrypted_page(page)); |
| 109 |
|
| 110 |
control->m_version = |
| 111 |
static_cast<uint8_t>(mach_read_from_1(page + FIL_PAGE_VERSION)); |
| 112 |
|
| 113 |
control->m_original_type = |
| 114 |
static_cast<uint16_t>(mach_read_from_2(page + FIL_PAGE_ORIGINAL_TYPE_V1)); |
| 115 |
|
| 116 |
control->m_compressed_size = |
| 117 |
static_cast<uint16_t>(mach_read_from_2(page + FIL_PAGE_COMPRESS_SIZE_V1)); |
| 118 |
|
| 119 |
control->m_original_size = |
| 120 |
static_cast<uint16_t>(mach_read_from_2(page + FIL_PAGE_ORIGINAL_SIZE_V1)); |
| 121 |
|
| 122 |
control->m_algorithm = |
| 123 |
static_cast<Type>(mach_read_from_1(page + FIL_PAGE_ALGORITHM_V1)); |
| 124 |
} |
| 125 |
|
| 126 |
/** Decompress the page data contents. Page type must be FIL_PAGE_COMPRESSED, if |
| 127 |
not then the source contents are left unchanged and DB_SUCCESS is returned. |
| 128 |
@param[in] dblwr_read true if double write recovery in progress |
| 129 |
@param[in,out] src Data read from disk, decompressed data will be |
| 130 |
copied to this page |
| 131 |
@param[in,out] dst Scratch area to use for decompression or |
| 132 |
nullptr. |
| 133 |
@param[in] dst_len If dst is valid, size of the scratch area in |
| 134 |
bytes. |
| 135 |
@return DB_SUCCESS or error code */ |
| 136 |
dberr_t Compression::deserialize(bool dblwr_read, byte *src, byte *dst, |
| 137 |
ulint dst_len) { |
| 138 |
if (!is_compressed_page(src)) { |
| 139 |
/* There is nothing we can do. */ |
| 140 |
return (DB_SUCCESS); |
| 141 |
} |
| 142 |
|
| 143 |
meta_t header; |
| 144 |
|
| 145 |
deserialize_header(src, &header); |
| 146 |
|
| 147 |
byte *ptr = src + FIL_PAGE_DATA; |
| 148 |
|
| 149 |
if (!is_valid_page_version(header.m_version) || |
| 150 |
header.m_original_size < UNIV_PAGE_SIZE_MIN - (FIL_PAGE_DATA + 8) || |
| 151 |
header.m_original_size > UNIV_PAGE_SIZE_MAX - FIL_PAGE_DATA) { |
| 152 |
return DB_CORRUPTION; |
| 153 |
} |
| 154 |
|
| 155 |
if (dst != nullptr && dst_len < header.m_original_size + FIL_PAGE_DATA) { |
| 156 |
/* The caller can retry with a larger buffer. */ |
| 157 |
return DB_OVERFLOW; |
| 158 |
} |
| 159 |
|
| 160 |
ut_ad(dst == nullptr || dst_len == header.m_original_size + FIL_PAGE_DATA); |
| 161 |
|
| 162 |
// FIXME: We should use TLS for this and reduce the malloc/free |
| 163 |
bool allocated; |
| 164 |
|
| 165 |
/* The caller doesn't know what to expect */ |
| 166 |
if (dst == nullptr) { |
| 167 |
/* Add a safety margin of an additional 50% */ |
| 168 |
ulint n_bytes = header.m_original_size + (header.m_original_size / 2); |
| 169 |
|
| 170 |
dst = reinterpret_cast<byte *>( |
| 171 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, n_bytes)); |
| 172 |
|
| 173 |
if (dst == nullptr) { |
| 174 |
return (DB_OUT_OF_MEMORY); |
| 175 |
} |
| 176 |
|
| 177 |
allocated = true; |
| 178 |
} else { |
| 179 |
allocated = false; |
| 180 |
} |
| 181 |
|
| 182 |
int ret; |
| 183 |
Compression compression; |
| 184 |
ulint len = header.m_original_size; |
| 185 |
|
| 186 |
compression.m_type = static_cast<Compression::Type>(header.m_algorithm); |
| 187 |
|
| 188 |
switch (compression.m_type) { |
| 189 |
case Compression::ZLIB: { |
| 190 |
uLongf zlen = header.m_original_size; |
| 191 |
|
| 192 |
if (uncompress(dst, &zlen, ptr, header.m_compressed_size) != Z_OK) { |
| 193 |
if (allocated) { |
| 194 |
ut::free(dst); |
| 195 |
} |
| 196 |
|
| 197 |
return (DB_IO_DECOMPRESS_FAIL); |
| 198 |
} |
| 199 |
|
| 200 |
ut_ad(zlen <= len); |
| 201 |
len = static_cast<ulint>(zlen); |
| 202 |
|
| 203 |
break; |
| 204 |
} |
| 205 |
|
| 206 |
case Compression::LZ4: |
| 207 |
|
| 208 |
if (dblwr_read) { |
| 209 |
ret = LZ4_decompress_safe( |
| 210 |
reinterpret_cast<char *>(ptr), reinterpret_cast<char *>(dst), |
| 211 |
header.m_compressed_size, header.m_original_size); |
| 212 |
|
| 213 |
} else { |
| 214 |
/* This can potentially read beyond the input |
| 215 |
buffer if the data is malformed. According to |
| 216 |
the LZ4 documentation it is a little faster |
| 217 |
than the above function. When recovering from |
| 218 |
the double write buffer we can afford to us the |
| 219 |
slower function above. */ |
| 220 |
|
| 221 |
ret = LZ4_decompress_fast(reinterpret_cast<char *>(ptr), |
| 222 |
reinterpret_cast<char *>(dst), |
| 223 |
header.m_original_size); |
| 224 |
} |
| 225 |
|
| 226 |
if (ret < 0) { |
| 227 |
if (allocated) { |
| 228 |
ut::free(dst); |
| 229 |
} |
| 230 |
|
| 231 |
return (DB_IO_DECOMPRESS_FAIL); |
| 232 |
} |
| 233 |
|
| 234 |
break; |
| 235 |
|
| 236 |
default: |
| 237 |
#ifdef UNIV_NO_ERR_MSGS |
| 238 |
ib::error() |
| 239 |
#else |
| 240 |
ib::error(ER_IB_MSG_741) |
| 241 |
#endif /* UNIV_NO_ERR_MSGS */ |
| 242 |
<< "Compression algorithm support missing: " |
| 243 |
<< Compression::to_string(compression.m_type); |
| 244 |
|
| 245 |
if (allocated) { |
| 246 |
ut::free(dst); |
| 247 |
} |
| 248 |
|
| 249 |
return (DB_UNSUPPORTED); |
| 250 |
} |
| 251 |
|
| 252 |
/* Leave the header alone */ |
| 253 |
memmove(src + FIL_PAGE_DATA, dst, len); |
| 254 |
|
| 255 |
mach_write_to_2(src + FIL_PAGE_TYPE, header.m_original_type); |
| 256 |
|
| 257 |
ut_ad(dblwr_read || BlockReporter::is_lsn_valid( |
| 258 |
src, header.m_original_size + FIL_PAGE_DATA)); |
| 259 |
|
| 260 |
if (allocated) { |
| 261 |
ut::free(dst); |
| 262 |
} |
| 263 |
|
| 264 |
return (DB_SUCCESS); |
| 265 |
} |
| 266 |
|
| 267 |
/** Decompress the page data contents. Page type must be FIL_PAGE_COMPRESSED, if |
| 268 |
not then the source contents are left unchanged and DB_SUCCESS is returned. |
| 269 |
@param[in] dblwr_read true of double write recovery in progress |
| 270 |
@param[in,out] src Data read from disk, decompressed data will be |
| 271 |
copied to this page |
| 272 |
@param[in,out] dst Scratch area to use for decompression or |
| 273 |
nullptr. |
| 274 |
@param[in] dst_len If dst is valid, then size of the scratch area |
| 275 |
in bytes |
| 276 |
@return DB_SUCCESS or error code */ |
| 277 |
dberr_t os_file_decompress_page(bool dblwr_read, byte *src, byte *dst, |
| 278 |
ulint dst_len) { |
| 279 |
return (Compression::deserialize(dblwr_read, src, dst, dst_len)); |
| 280 |
} |