| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1995, 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 |
/** @file include/fil0fil.h |
| 29 |
The low-level file system |
| 30 |
|
| 31 |
Created 10/25/1995 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef fil0fil_h |
| 35 |
#define fil0fil_h |
| 36 |
|
| 37 |
#include "univ.i" |
| 38 |
|
| 39 |
#include "dict0types.h" |
| 40 |
#include "fil0types.h" |
| 41 |
#include "log0recv.h" |
| 42 |
#include "page0size.h" |
| 43 |
#ifndef UNIV_HOTBACKUP |
| 44 |
#include "ibuf0types.h" |
| 45 |
#endif /* !UNIV_HOTBACKUP */ |
| 46 |
#include "ut0new.h" |
| 47 |
|
| 48 |
#include "m_ctype.h" |
| 49 |
#include "sql/dd/object_id.h" |
| 50 |
|
| 51 |
#include <atomic> |
| 52 |
#include <cstdint> |
| 53 |
#include <list> |
| 54 |
#include <vector> |
| 55 |
|
| 56 |
extern ulong srv_fast_shutdown; |
| 57 |
|
| 58 |
/** Maximum number of tablespaces to be scanned by a thread while scanning |
| 59 |
for available tablespaces during server startup. This is a hard maximum. |
| 60 |
If the number of files to be scanned is more than |
| 61 |
FIL_SCAN_MAX_TABLESPACES_PER_THREAD, |
| 62 |
then additional threads will be spawned to scan the additional files in |
| 63 |
parallel. */ |
| 64 |
constexpr size_t FIL_SCAN_MAX_TABLESPACES_PER_THREAD = 8000; |
| 65 |
|
| 66 |
/** Maximum number of threads that will be used for scanning the tablespace |
| 67 |
files. This can be further adjusted depending on the number of available |
| 68 |
cores. */ |
| 69 |
constexpr size_t FIL_SCAN_MAX_THREADS = 16; |
| 70 |
|
| 71 |
/** Number of threads per core. */ |
| 72 |
constexpr size_t FIL_SCAN_THREADS_PER_CORE = 2; |
| 73 |
|
| 74 |
/** Calculate the number of threads that can be spawned to scan the given |
| 75 |
number of files taking into the consideration, number of cores available |
| 76 |
on the machine. |
| 77 |
@param[in] num_files Number of files to be scanned |
| 78 |
@return number of threads to be spawned for scanning the files */ |
| 79 |
size_t fil_get_scan_threads(size_t num_files); |
| 80 |
|
| 81 |
/** This tablespace name is used internally during file discovery to open a |
| 82 |
general tablespace before the data dictionary is recovered and available. */ |
| 83 |
static constexpr char general_space_name[] = "innodb_general"; |
| 84 |
|
| 85 |
/** This tablespace name is used as the prefix for implicit undo tablespaces |
| 86 |
and during file discovery to open an undo tablespace before the DD is |
| 87 |
recovered and available. */ |
| 88 |
static constexpr char undo_space_name[] = "innodb_undo"; |
| 89 |
|
| 90 |
extern volatile bool recv_recovery_on; |
| 91 |
|
| 92 |
/** Initial size of an UNDO tablespace when it is created new |
| 93 |
or truncated under low load. |
| 94 |
page size | FSP_EXTENT_SIZE | Initial Size | Pages |
| 95 |
----------+------------------+--------------+------- |
| 96 |
4 KB | 256 pages = 1 MB | 16 MB | 4096 |
| 97 |
8 KB | 128 pages = 1 MB | 16 MB | 2048 |
| 98 |
16 KB | 64 pages = 1 MB | 16 MB | 1024 |
| 99 |
32 KB | 64 pages = 2 MB | 16 MB | 512 |
| 100 |
64 KB | 64 pages = 4 MB | 16 MB | 256 */ |
| 101 |
constexpr uint32_t UNDO_INITIAL_SIZE = 16 * 1024 * 1024; |
| 102 |
#define UNDO_INITIAL_SIZE_IN_PAGES \ |
| 103 |
os_offset_t { UNDO_INITIAL_SIZE / srv_page_size } |
| 104 |
|
| 105 |
#ifdef UNIV_HOTBACKUP |
| 106 |
#include <unordered_set> |
| 107 |
using Dir_set = std::unordered_set<std::string>; |
| 108 |
extern Dir_set rem_gen_ts_dirs; |
| 109 |
extern bool replay_in_datadir; |
| 110 |
#endif /* UNIV_HOTBACKUP */ |
| 111 |
|
| 112 |
// Forward declaration |
| 113 |
struct trx_t; |
| 114 |
class page_id_t; |
| 115 |
|
| 116 |
using Filenames = std::vector<std::string, ut::allocator<std::string>>; |
| 117 |
using Space_ids = std::vector<space_id_t, ut::allocator<space_id_t>>; |
| 118 |
|
| 119 |
/** File types */ |
| 120 |
enum fil_type_t : uint8_t { |
| 121 |
/** temporary tablespace (temporary undo log or tables) */ |
| 122 |
FIL_TYPE_TEMPORARY = 1, |
| 123 |
/** a tablespace that is being imported (no logging until finished) */ |
| 124 |
FIL_TYPE_IMPORT = 2, |
| 125 |
/** persistent tablespace (for system, undo log or tables) */ |
| 126 |
FIL_TYPE_TABLESPACE = 4, |
| 127 |
}; |
| 128 |
|
| 129 |
/** Result of comparing a path. */ |
| 130 |
enum class Fil_state { |
| 131 |
/** The path matches what was found during the scan. */ |
| 132 |
MATCHES, |
| 133 |
|
| 134 |
/** No MLOG_FILE_DELETE record and the file could not be found. */ |
| 135 |
MISSING, |
| 136 |
|
| 137 |
/** A MLOG_FILE_DELETE was found, file was deleted. */ |
| 138 |
DELETED, |
| 139 |
|
| 140 |
/** Space ID matches but the paths don't match. */ |
| 141 |
MOVED, |
| 142 |
|
| 143 |
/** Space ID and paths match but dd_table data dir flag is false despite the |
| 144 |
file being outside default data dir */ |
| 145 |
MOVED_PREV, |
| 146 |
|
| 147 |
/** Tablespace and/or filename was renamed. The DDL log will handle |
| 148 |
this case. */ |
| 149 |
RENAMED, |
| 150 |
|
| 151 |
/** In case of error during comparison. */ |
| 152 |
COMPARE_ERROR |
| 153 |
}; |
| 154 |
|
| 155 |
struct fil_space_t; |
| 156 |
|
| 157 |
/** File node of a tablespace or the log data space */ |
| 158 |
struct fil_node_t { |
| 159 |
/** Returns true if the file can be closed. */ |
| 160 |
bool can_be_closed() const; |
| 161 |
/** Returns true if the file is flushed. */ |
| 162 |
bool is_flushed() const { |
| 163 |
ut_ad(modification_counter >= flush_counter); |
| 164 |
return modification_counter == flush_counter; |
| 165 |
} |
| 166 |
/** Sets file to flushed state. */ |
| 167 |
void set_flushed() { flush_counter = modification_counter; } |
| 168 |
|
| 169 |
using List_node = UT_LIST_NODE_T(fil_node_t); |
| 170 |
|
| 171 |
/** tablespace containing this file */ |
| 172 |
fil_space_t *space; |
| 173 |
|
| 174 |
/** file name; protected by Fil_shard::m_mutex and log_sys->mutex. */ |
| 175 |
char *name; |
| 176 |
|
| 177 |
/** whether this file is open. Note: We set the is_open flag after |
| 178 |
we increase the write the MLOG_FILE_OPEN record to redo log. Therefore |
| 179 |
we increment the in_use reference count before setting the OPEN flag. */ |
| 180 |
bool is_open; |
| 181 |
|
| 182 |
/** file handle (valid if is_open) */ |
| 183 |
pfs_os_file_t handle; |
| 184 |
|
| 185 |
/** event that groups and serializes calls to fsync */ |
| 186 |
os_event_t sync_event; |
| 187 |
|
| 188 |
/** whether the file actually is a raw device or disk partition */ |
| 189 |
bool is_raw_disk; |
| 190 |
|
| 191 |
/** size of the file in database pages (0 if not known yet); |
| 192 |
the possible last incomplete megabyte may be ignored |
| 193 |
if space->id == 0 */ |
| 194 |
page_no_t size; |
| 195 |
|
| 196 |
/** Size of the file when last flushed, used to force the flush when file |
| 197 |
grows to keep the filesystem metadata synced when using O_DIRECT_NO_FSYNC */ |
| 198 |
page_no_t flush_size; |
| 199 |
|
| 200 |
/** initial size of the file in database pages; |
| 201 |
FIL_IBD_FILE_INITIAL_SIZE by default */ |
| 202 |
page_no_t init_size; |
| 203 |
|
| 204 |
/** maximum size of the file in database pages */ |
| 205 |
page_no_t max_size; |
| 206 |
|
| 207 |
/** count of pending I/O's; is_open must be true if nonzero */ |
| 208 |
size_t n_pending_ios; |
| 209 |
|
| 210 |
/** count of pending flushes; is_open must be true if nonzero */ |
| 211 |
size_t n_pending_flushes; |
| 212 |
|
| 213 |
/** Set to true when a file is being extended. */ |
| 214 |
bool is_being_extended; |
| 215 |
|
| 216 |
/** number of writes to the file since the system was started */ |
| 217 |
int64_t modification_counter; |
| 218 |
|
| 219 |
/** the modification_counter of the latest flush to disk */ |
| 220 |
int64_t flush_counter; |
| 221 |
|
| 222 |
/** link to the fil_system->LRU list (keeping track of open files) */ |
| 223 |
List_node LRU; |
| 224 |
|
| 225 |
/** whether the file system of this file supports PUNCH HOLE */ |
| 226 |
bool punch_hole; |
| 227 |
|
| 228 |
/** block size to use for punching holes */ |
| 229 |
size_t block_size; |
| 230 |
|
| 231 |
/** whether atomic write is enabled for this file */ |
| 232 |
bool atomic_write; |
| 233 |
|
| 234 |
/** FIL_NODE_MAGIC_N */ |
| 235 |
size_t magic_n; |
| 236 |
}; |
| 237 |
|
| 238 |
/** Tablespace or log data space */ |
| 239 |
struct fil_space_t { |
| 240 |
using List_node = UT_LIST_NODE_T(fil_space_t); |
| 241 |
using Files = std::vector<fil_node_t, ut::allocator<fil_node_t>>; |
| 242 |
|
| 243 |
/** Release the reserved free extents. |
| 244 |
@param[in] n_reserved number of reserved extents */ |
| 245 |
void release_free_extents(ulint n_reserved); |
| 246 |
|
| 247 |
/** @return true if the instance is queued for deletion. Guarantees the space |
| 248 |
is not deleted as long as the fil_shard mutex is not released. */ |
| 249 |
bool is_deleted() const; |
| 250 |
|
| 251 |
/** @return true if the instance was not queued for deletion. It does not |
| 252 |
guarantee it is not queued for deletion at the moment. */ |
| 253 |
bool was_not_deleted() const; |
| 254 |
|
| 255 |
/** Marks the space object for deletion. It will bump the space object version |
| 256 |
and cause all pages in buffer pool that reference to the current space |
| 257 |
object version to be stale and be freed on first encounter. */ |
| 258 |
void set_deleted(); |
| 259 |
|
| 260 |
#ifndef UNIV_HOTBACKUP |
| 261 |
/** Returns current version of the space object. It is being bumped when the |
| 262 |
space is truncated or deleted. Guarantees the version returned is up to date |
| 263 |
as long as fil_shard mutex is not released.*/ |
| 264 |
uint32_t get_current_version() const; |
| 265 |
|
| 266 |
/** Returns current version of the space object. It is being bumped when the |
| 267 |
space is truncated or deleted. It does not guarantee the version is current |
| 268 |
one.*/ |
| 269 |
uint32_t get_recent_version() const; |
| 270 |
|
| 271 |
/** Bumps the space object version and cause all pages in buffer pool that |
| 272 |
reference the current space object version to be stale and be freed on |
| 273 |
first encounter. */ |
| 274 |
void bump_version(); |
| 275 |
|
| 276 |
/** @return true if this space does not have any more references. Guarantees |
| 277 |
the result only if true was returned. */ |
| 278 |
bool has_no_references() const; |
| 279 |
|
| 280 |
/** @return Current number of references to the space. This method |
| 281 |
should be called only while shutting down the server. Only when there is no |
| 282 |
background nor user session activity the returned value will be valid. */ |
| 283 |
size_t get_reference_count() const; |
| 284 |
|
| 285 |
/** Increment the page reference count. */ |
| 286 |
void inc_ref() noexcept { |
| 287 |
/* We assume space is protected from being removed either through |
| 288 |
n_pending_ops or m_n_ref_count already bumped, OR MDL latch is |
| 289 |
protecting it, OR it is a private space. Bumping the n_pending_ops |
| 290 |
can be done only under fil shard mutex and stopping new bumps is also |
| 291 |
done under this mutex */ |
| 292 |
const auto o = m_n_ref_count.fetch_add(1); |
| 293 |
ut_a(o != std::numeric_limits<size_t>::max()); |
| 294 |
} |
| 295 |
|
| 296 |
/** Decrement the page reference count. */ |
| 297 |
void dec_ref() noexcept { |
| 298 |
const auto o = m_n_ref_count.fetch_sub(1); |
| 299 |
ut_a(o >= 1); |
| 300 |
} |
| 301 |
#endif /* !UNIV_HOTBACKUP */ |
| 302 |
|
| 303 |
#ifdef UNIV_DEBUG |
| 304 |
/** Print the extent descriptor pages of this tablespace into |
| 305 |
the given output stream. |
| 306 |
@param[in] out the output stream. |
| 307 |
@return the output stream. */ |
| 308 |
std::ostream &print_xdes_pages(std::ostream &out) const; |
| 309 |
|
| 310 |
/** Print the extent descriptor pages of this tablespace into |
| 311 |
the given file. |
| 312 |
@param[in] filename the output file name. */ |
| 313 |
void print_xdes_pages(const char *filename) const; |
| 314 |
#endif /* UNIV_DEBUG */ |
| 315 |
|
| 316 |
public: |
| 317 |
using Observer = Flush_observer; |
| 318 |
using Flush_observers = std::vector<Observer *, ut::allocator<Observer *>>; |
| 319 |
|
| 320 |
/** When the tablespace was extended last. */ |
| 321 |
ib::Timer m_last_extended{}; |
| 322 |
|
| 323 |
/** Extend undo tablespaces by so many pages. */ |
| 324 |
page_no_t m_undo_extend{}; |
| 325 |
|
| 326 |
/** When an undo tablespace has been initialized with required header pages, |
| 327 |
that size is recorded here. Auto-truncation happens when the file size |
| 328 |
becomes bigger than both this and srv_max_undo_log_size. */ |
| 329 |
page_no_t m_undo_initial{}; |
| 330 |
|
| 331 |
/** Tablespace name */ |
| 332 |
char *name{}; |
| 333 |
|
| 334 |
/** Tablespace ID */ |
| 335 |
space_id_t id; |
| 336 |
|
| 337 |
/** Initializes fields. This could be replaced by a constructor if SunPro is |
| 338 |
compiling it correctly. */ |
| 339 |
void initialize() noexcept { |
| 340 |
new (&m_last_extended) ib::Timer; |
| 341 |
new (&files) fil_space_t::Files(); |
| 342 |
|
| 343 |
#ifndef UNIV_HOTBACKUP |
| 344 |
new (&m_version) std::atomic<uint32_t>; |
| 345 |
new (&m_n_ref_count) std::atomic_size_t; |
| 346 |
new (&m_deleted) std::atomic<bool>; |
| 347 |
#endif /* !UNIV_HOTBACKUP */ |
| 348 |
} |
| 349 |
|
| 350 |
private: |
| 351 |
#ifndef UNIV_HOTBACKUP |
| 352 |
/** All pages in the buffer pool that reference this fil_space_t instance with |
| 353 |
version before this version can be lazily freed or reused as free pages. |
| 354 |
They should be rejected if there is an attempt to write them to disk. |
| 355 |
|
| 356 |
Writes to m_version are guarded by the exclusive MDL/table lock latches |
| 357 |
acquired by the caller, as stated in docs. Note that the Fil_shard mutex seems |
| 358 |
to be latched in 2 of 3 usages only, so is not really an alternative. |
| 359 |
|
| 360 |
Existence of the space object during reads is assured during these operations: |
| 361 |
1. when read by the buf_page_init_low on page read/creation - the caller must |
| 362 |
have acquired shared MDL/table lock latches. |
| 363 |
2. when read on buf_page_t::is_stale() on page access for a query or for purge |
| 364 |
operation. The caller must have acquired shared MDL/table lock latches. |
| 365 |
3. when read on buf_page_t::is_stale() on page access from LRU list, flush |
| 366 |
list or whatever else. Here, the fact that the page has latched the space |
| 367 |
using the reference counting system is what guards the space existence. |
| 368 |
|
| 369 |
When reading the value for the page being created with buf_page_init_low we |
| 370 |
have the MDL latches on table that is in tablespace or the tablespace alone, |
| 371 |
so we won't be able to bump m_version until they are released, so we will |
| 372 |
read the current value of the version. When reading the value for the page |
| 373 |
validation with buf_page_t::is_stale(), we will either: |
| 374 |
a) have the MDL latches required in at least S mode in case we need to be |
| 375 |
certain if the page is stale, to use it in a query or in purge operation, or |
| 376 |
b) in case we don't not have the MDL latches, we may read an outdated value. |
| 377 |
This happens for pages that are seen during for example LRU or flush page |
| 378 |
scans. These pages are not needed for the query itself. The read is to decide |
| 379 |
if the page can be safely discarded. Reading incorrect value can lead to no |
| 380 |
action being executed. Reading incorrect value can't lead to page being |
| 381 |
incorrectly evicted. |
| 382 |
*/ |
| 383 |
std::atomic<uint32_t> m_version{}; |
| 384 |
|
| 385 |
/** Number of buf_page_t entries that point to this instance. |
| 386 |
|
| 387 |
This field is guarded by the Fil_shard mutex and the "reference |
| 388 |
count system". The reference count system here is allowing to take a "latch" |
| 389 |
on the space by incrementing the reference count, and release it by |
| 390 |
decrementing it. |
| 391 |
|
| 392 |
The increments are possible from two places: |
| 393 |
1. buf_page_init_low is covered by the existing MDL/table lock latches only |
| 394 |
and the fact that the space it is using is a current version of the space |
| 395 |
(the version itself is also guarded by these MDL/table lock latches). It |
| 396 |
implicitly acquires the "reference count system" latch after this operation. |
| 397 |
2. buf_page_t::buf_page_t(const buf_page_t&) copy constructor - increases the |
| 398 |
value, but it assumes the page being copied from has "reference count system" |
| 399 |
latch so the reference count is greater than 0 during this constructor call. |
| 400 |
|
| 401 |
For decrementing the reference count is itself a latch allowing for the safe |
| 402 |
decrement. |
| 403 |
|
| 404 |
The value is checked for being 0 in Fil_shard::checkpoint under the Fil_shard |
| 405 |
mutex, and only if the space is deleted. |
| 406 |
Observing m_n_ref_count==0 might trigger freeing the object. No other thread |
| 407 |
can be during the process of incrementing m_n_ref_count from 0 to 1 in |
| 408 |
parallel to this check. This is impossible for following reasons. Recall the |
| 409 |
only two places where we do increments listed above: |
| 410 |
1. If the space is deleted, then MDL/table lock latches guarantee there are |
| 411 |
no users that would be able to see it as the current version of space and thus |
| 412 |
will not attempt to increase the reference value from 0. |
| 413 |
2. The buf_page_t copy constructor can increase it, but it assumes the page |
| 414 |
being copied from has "reference count system" latch so the reference count is |
| 415 |
greater than 0 during this constructor call. |
| 416 |
|
| 417 |
There is also an opposite race possible: while we check for ref count being |
| 418 |
zero, another thread may be decrementing it in parallel, and we might miss |
| 419 |
that if we check too soon. This is benign, as it will result in us not |
| 420 |
reclaiming the memory we could (but not have to) free, and will return to the |
| 421 |
check on next checkpoint. |
| 422 |
*/ |
| 423 |
std::atomic_size_t m_n_ref_count{}; |
| 424 |
#endif /* !UNIV_HOTBACKUP */ |
| 425 |
|
| 426 |
/** true if the tablespace is marked for deletion. */ |
| 427 |
std::atomic_bool m_deleted{}; |
| 428 |
|
| 429 |
public: |
| 430 |
/** true if we want to rename the .ibd file of tablespace and |
| 431 |
want to temporarily prevent other threads from opening the file that is being |
| 432 |
renamed. */ |
| 433 |
bool prevent_file_open{}; |
| 434 |
|
| 435 |
/** Throttles writing to log a message about long waiting for file to perform |
| 436 |
rename. */ |
| 437 |
ib::Throttler m_prevent_file_open_wait_message_throttler; |
| 438 |
|
| 439 |
/** We set this true when we start deleting a single-table |
| 440 |
tablespace. When this is set following new ops are not allowed: |
| 441 |
* read IO request |
| 442 |
* ibuf merge |
| 443 |
* file flush |
| 444 |
Note that we can still possibly have new write operations because we |
| 445 |
don't check this flag when doing flush batches. */ |
| 446 |
bool stop_new_ops{}; |
| 447 |
|
| 448 |
#ifdef UNIV_DEBUG |
| 449 |
/** Reference count for operations who want to skip redo log in |
| 450 |
the file space in order to make fsp_space_modify_check pass. */ |
| 451 |
ulint redo_skipped_count{}; |
| 452 |
#endif /* UNIV_DEBUG */ |
| 453 |
|
| 454 |
/** Purpose */ |
| 455 |
fil_type_t purpose; |
| 456 |
|
| 457 |
/** Files attached to this tablespace. Note: Only the system tablespace |
| 458 |
can have multiple files, this is a legacy issue. */ |
| 459 |
Files files{}; |
| 460 |
|
| 461 |
/** Tablespace file size in pages; 0 if not known yet */ |
| 462 |
page_no_t size{}; |
| 463 |
|
| 464 |
/** FSP_SIZE in the tablespace header; 0 if not known yet */ |
| 465 |
page_no_t size_in_header{}; |
| 466 |
|
| 467 |
/** Autoextend size */ |
| 468 |
uint64_t autoextend_size_in_bytes{}; |
| 469 |
|
| 470 |
/** Length of the FSP_FREE list */ |
| 471 |
uint32_t free_len{}; |
| 472 |
|
| 473 |
/** Contents of FSP_FREE_LIMIT */ |
| 474 |
page_no_t free_limit{}; |
| 475 |
|
| 476 |
/** Tablespace flags; see fsp_flags_is_valid() and |
| 477 |
page_size_t(ulint) (constructor). |
| 478 |
This is protected by space->latch and tablespace MDL */ |
| 479 |
uint32_t flags{}; |
| 480 |
|
| 481 |
/** Number of reserved free extents for ongoing operations like |
| 482 |
B-tree page split */ |
| 483 |
uint32_t n_reserved_extents{}; |
| 484 |
|
| 485 |
/** This is positive when flushing the tablespace to disk; |
| 486 |
dropping of the tablespace is forbidden if this is positive */ |
| 487 |
uint32_t n_pending_flushes{}; |
| 488 |
|
| 489 |
/** This is positive when we have pending operations against this |
| 490 |
tablespace. The pending operations can be ibuf merges or lock |
| 491 |
validation code trying to read a block. Dropping of the tablespace |
| 492 |
is forbidden if this is positive. Protected by Fil_shard::m_mutex. */ |
| 493 |
uint32_t n_pending_ops{}; |
| 494 |
|
| 495 |
#ifndef UNIV_HOTBACKUP |
| 496 |
/** Latch protecting the file space storage allocation */ |
| 497 |
rw_lock_t latch; |
| 498 |
#endif /* !UNIV_HOTBACKUP */ |
| 499 |
|
| 500 |
/** List of spaces with at least one unflushed file we have |
| 501 |
written to */ |
| 502 |
List_node unflushed_spaces; |
| 503 |
|
| 504 |
/** true if this space is currently in unflushed_spaces */ |
| 505 |
bool is_in_unflushed_spaces{}; |
| 506 |
|
| 507 |
/** Compression algorithm */ |
| 508 |
Compression::Type compression_type; |
| 509 |
|
| 510 |
/** Encryption metadata */ |
| 511 |
Encryption_metadata m_encryption_metadata; |
| 512 |
|
| 513 |
/** Encryption is in progress */ |
| 514 |
Encryption::Progress encryption_op_in_progress{Encryption::Progress::NONE}; |
| 515 |
|
| 516 |
/** Flush lsn of header page. It is used only during recovery */ |
| 517 |
lsn_t m_header_page_flush_lsn; |
| 518 |
|
| 519 |
/** FIL_SPACE_MAGIC_N */ |
| 520 |
ulint magic_n; |
| 521 |
|
| 522 |
/** System tablespace */ |
| 523 |
static fil_space_t *s_sys_space; |
| 524 |
|
| 525 |
/** Check if the tablespace is compressed. |
| 526 |
@return true if compressed, false otherwise. */ |
| 527 |
[[nodiscard]] bool is_compressed() const noexcept { |
| 528 |
return compression_type != Compression::NONE; |
| 529 |
} |
| 530 |
|
| 531 |
/** Check if the tablespace is encrypted. |
| 532 |
@return true if encrypted, false otherwise. */ |
| 533 |
[[nodiscard]] bool is_encrypted() const noexcept { |
| 534 |
return FSP_FLAGS_GET_ENCRYPTION(flags); |
| 535 |
} |
| 536 |
|
| 537 |
/** Check if the encryption details, like the encryption key, type and |
| 538 |
other details, that are needed to carry out encryption are available. |
| 539 |
@return true if encryption can be done, false otherwise. */ |
| 540 |
[[nodiscard]] bool can_encrypt() const noexcept { |
| 541 |
return m_encryption_metadata.m_type != Encryption::Type::NONE; |
| 542 |
} |
| 543 |
|
| 544 |
public: |
| 545 |
/** Get the file node corresponding to the given page number of the |
| 546 |
tablespace. |
| 547 |
@param[in,out] page_no Caller passes the page number within a tablespace. |
| 548 |
After return, it contains the page number within |
| 549 |
the returned file node. For tablespaces containing |
| 550 |
only one file, the given page_no will not change. |
| 551 |
@return the file node object. */ |
| 552 |
[[nodiscard]] fil_node_t *get_file_node(page_no_t *page_no) noexcept; |
| 553 |
}; |
| 554 |
|
| 555 |
/** Value of fil_space_t::magic_n */ |
| 556 |
constexpr size_t FIL_SPACE_MAGIC_N = 89472; |
| 557 |
|
| 558 |
/** Value of fil_node_t::magic_n */ |
| 559 |
constexpr size_t FIL_NODE_MAGIC_N = 89389; |
| 560 |
|
| 561 |
/** Common InnoDB file extensions */ |
| 562 |
enum ib_file_suffix { |
| 563 |
NO_EXT = 0, |
| 564 |
IBD = 1, |
| 565 |
CFG = 2, |
| 566 |
CFP = 3, |
| 567 |
IBT = 4, |
| 568 |
IBU = 5, |
| 569 |
DWR = 6, |
| 570 |
BWR = 7 |
| 571 |
}; |
| 572 |
|
| 573 |
extern const char *dot_ext[]; |
| 574 |
|
| 575 |
#define DOT_IBD dot_ext[IBD] |
| 576 |
#define DOT_CFG dot_ext[CFG] |
| 577 |
#define DOT_CFP dot_ext[CFP] |
| 578 |
#define DOT_IBT dot_ext[IBT] |
| 579 |
#define DOT_IBU dot_ext[IBU] |
| 580 |
#define DOT_DWR dot_ext[DWR] |
| 581 |
|
| 582 |
#ifdef _WIN32 |
| 583 |
/* Initialization of m_abs_path() produces warning C4351: |
| 584 |
"new behavior: elements of array '...' will be default initialized." |
| 585 |
See https://msdn.microsoft.com/en-us/library/1ywe7hcy.aspx */ |
| 586 |
#pragma warning(disable : 4351) |
| 587 |
#endif /* _WIN32 */ |
| 588 |
|
| 589 |
/** Wrapper for a path to a directory that may or may not exist. */ |
| 590 |
class Fil_path { |
| 591 |
public: |
| 592 |
/** schema '/' table separator */ |
| 593 |
static constexpr auto DB_SEPARATOR = '/'; |
| 594 |
|
| 595 |
/** OS specific path separator. */ |
| 596 |
static constexpr auto OS_SEPARATOR = OS_PATH_SEPARATOR; |
| 597 |
|
| 598 |
/** Directory separators that are supported. */ |
| 599 |
static constexpr auto SEPARATOR = "\\/"; |
| 600 |
#ifdef _WIN32 |
| 601 |
static constexpr auto DOT_SLASH = ".\\"; |
| 602 |
static constexpr auto DOT_DOT_SLASH = "..\\"; |
| 603 |
static constexpr auto SLASH_DOT_DOT_SLASH = "\\..\\"; |
| 604 |
#else |
| 605 |
static constexpr auto DOT_SLASH = "./"; |
| 606 |
static constexpr auto DOT_DOT_SLASH = "../"; |
| 607 |
static constexpr auto SLASH_DOT_DOT_SLASH = "/../"; |
| 608 |
#endif /* _WIN32 */ |
| 609 |
|
| 610 |
/** Various types of file paths. */ |
| 611 |
enum path_type { absolute, relative, file_name_only, invalid }; |
| 612 |
|
| 613 |
/** Default constructor. Defaults to MySQL_datadir_path. */ |
| 614 |
Fil_path(); |
| 615 |
|
| 616 |
/** Constructor |
| 617 |
@param[in] path Path, not necessarily NUL terminated |
| 618 |
@param[in] len Length of path |
| 619 |
@param[in] normalize_path If false, it's the callers responsibility to |
| 620 |
ensure that the path is normalized. */ |
| 621 |
explicit Fil_path(const char *path, size_t len, bool normalize_path = false); |
| 622 |
|
| 623 |
/** Constructor |
| 624 |
@param[in] path Path, not necessarily NUL terminated |
| 625 |
@param[in] normalize_path If false, it's the callers responsibility to |
| 626 |
ensure that the path is normalized. */ |
| 627 |
explicit Fil_path(const char *path, bool normalize_path = false); |
| 628 |
|
| 629 |
/** Constructor |
| 630 |
@param[in] path pathname (may also include the file basename) |
| 631 |
@param[in] normalize_path If false, it's the callers responsibility to |
| 632 |
ensure that the path is normalized. */ |
| 633 |
explicit Fil_path(const std::string &path, bool normalize_path = false); |
| 634 |
|
| 635 |
/** Implicit type conversion |
| 636 |
@return pointer to m_path.c_str() */ |
| 637 |
[[nodiscard]] operator const char *() const { return m_path.c_str(); } |
| 638 |
|
| 639 |
/** Explicit type conversion |
| 640 |
@return pointer to m_path.c_str() */ |
| 641 |
[[nodiscard]] const char *operator()() const { return m_path.c_str(); } |
| 642 |
|
| 643 |
/** @return the value of m_path */ |
| 644 |
[[nodiscard]] const std::string &path() const { return (m_path); } |
| 645 |
|
| 646 |
/** @return the length of m_path */ |
| 647 |
[[nodiscard]] size_t len() const { return (m_path.length()); } |
| 648 |
|
| 649 |
/** Return the absolute path by value. If m_abs_path is null, calculate |
| 650 |
it and return it by value without trying to reset this const object. |
| 651 |
m_abs_path can be empty if the path did not exist when this object |
| 652 |
was constructed. |
| 653 |
@return the absolute path by value. */ |
| 654 |
[[nodiscard]] const std::string abs_path() const { |
| 655 |
if (m_abs_path.empty()) { |
| 656 |
return (get_real_path(m_path)); |
| 657 |
} |
| 658 |
|
| 659 |
return (m_abs_path); |
| 660 |
} |
| 661 |
|
| 662 |
/** @return the length of m_abs_path */ |
| 663 |
[[nodiscard]] size_t abs_len() const { return (m_abs_path.length()); } |
| 664 |
|
| 665 |
/** Determine if this path is equal to the other path. |
| 666 |
@param[in] other path to compare to |
| 667 |
@return true if the paths are the same */ |
| 668 |
bool operator==(const Fil_path &other) const { return (is_same_as(other)); } |
| 669 |
|
| 670 |
/** Check if m_path is the same as this other path. |
| 671 |
@param[in] other directory path to compare to |
| 672 |
@return true if m_path is the same as path */ |
| 673 |
[[nodiscard]] bool is_same_as(const Fil_path &other) const; |
| 674 |
|
| 675 |
/** Check if this path is the same as the other path. |
| 676 |
@param[in] other directory path to compare to |
| 677 |
@return true if this path is the same as the other path */ |
| 678 |
[[nodiscard]] bool is_same_as(const std::string &other) const; |
| 679 |
|
| 680 |
/** Get the absolute directory of this path */ |
| 681 |
[[nodiscard]] Fil_path get_abs_directory() const; |
| 682 |
|
| 683 |
/** Check if the directory to path is same as directory as the other path. |
| 684 |
@param[in] other directory path to compare to |
| 685 |
@return true if this path directory is the same as the other path directory */ |
| 686 |
[[nodiscard]] bool is_dir_same_as(const Fil_path &other) const; |
| 687 |
|
| 688 |
/** Check if two path strings are equal. Put them into Fil_path objects |
| 689 |
so that they can be compared correctly. |
| 690 |
@param[in] first first path to check |
| 691 |
@param[in] second socond path to check |
| 692 |
@return true if these two paths are the same */ |
| 693 |
[[nodiscard]] static bool is_same_as(const std::string &first, |
| 694 |
const std::string &second) { |
| 695 |
if (first.empty() || second.empty()) { |
| 696 |
return (false); |
| 697 |
} |
| 698 |
|
| 699 |
Fil_path first_path(first); |
| 700 |
std::string first_abs = first_path.abs_path(); |
| 701 |
trim_separator(first_abs); |
| 702 |
|
| 703 |
Fil_path second_path(second); |
| 704 |
std::string second_abs = second_path.abs_path(); |
| 705 |
trim_separator(second_abs); |
| 706 |
|
| 707 |
return (first_abs == second_abs); |
| 708 |
} |
| 709 |
|
| 710 |
/** Splits the path into directory and file name parts. |
| 711 |
@param[in] path path to split |
| 712 |
@return [directory, file] for the path */ |
| 713 |
[[nodiscard]] static std::pair<std::string, std::string> split( |
| 714 |
const std::string &path); |
| 715 |
|
| 716 |
/** Check if m_path is the parent of the other path. |
| 717 |
@param[in] other path to compare to |
| 718 |
@return true if m_path is an ancestor of name */ |
| 719 |
[[nodiscard]] bool is_ancestor(const Fil_path &other) const; |
| 720 |
|
| 721 |
/** Check if this Fil_path is an ancestor of the other path. |
| 722 |
@param[in] other path to compare to |
| 723 |
@return true if this Fil_path is an ancestor of the other path */ |
| 724 |
[[nodiscard]] bool is_ancestor(const std::string &other) const; |
| 725 |
|
| 726 |
/** Check if the first path is an ancestor of the second. |
| 727 |
Do not assume that these paths have been converted to real paths |
| 728 |
and are ready to compare. If the two paths are the same |
| 729 |
we will return false. |
| 730 |
@param[in] first Parent path to check |
| 731 |
@param[in] second Descendent path to check |
| 732 |
@return true if the first path is an ancestor of the second */ |
| 733 |
[[nodiscard]] static bool is_ancestor(const std::string &first, |
| 734 |
const std::string &second) { |
| 735 |
if (first.empty() || second.empty()) { |
| 736 |
return (false); |
| 737 |
} |
| 738 |
|
| 739 |
Fil_path ancestor(first); |
| 740 |
Fil_path descendant(second); |
| 741 |
|
| 742 |
return (ancestor.is_ancestor(descendant)); |
| 743 |
} |
| 744 |
|
| 745 |
/** @return true if m_path exists and is a file. */ |
| 746 |
[[nodiscard]] bool is_file_and_exists() const; |
| 747 |
|
| 748 |
/** @return true if m_path exists and is a directory. */ |
| 749 |
[[nodiscard]] bool is_directory_and_exists() const; |
| 750 |
|
| 751 |
/** This validation is only for ':'. |
| 752 |
@return true if the path is valid. */ |
| 753 |
[[nodiscard]] bool is_valid() const; |
| 754 |
|
| 755 |
/** Determine if m_path contains a circular section like "/anydir/../" |
| 756 |
Fil_path::normalize() must be run before this. |
| 757 |
@return true if a circular section if found, false if not */ |
| 758 |
[[nodiscard]] bool is_circular() const; |
| 759 |
|
| 760 |
/** Determine if the file or directory is considered HIDDEN. |
| 761 |
Most file systems identify the HIDDEN attribute by a '.' preceding the |
| 762 |
basename. On Windows, a HIDDEN path is identified by a file attribute. |
| 763 |
We will use the preceding '.' to indicate a HIDDEN attribute on ALL |
| 764 |
file systems so that InnoDB tablespaces and their directory structure |
| 765 |
remain portable. |
| 766 |
@param[in] path The full or relative path of a file or directory. |
| 767 |
@return true if the directory or path is HIDDEN. */ |
| 768 |
static bool is_hidden(std::string path); |
| 769 |
|
| 770 |
#ifdef _WIN32 |
| 771 |
/** Use the WIN32_FIND_DATA struncture to determine if the file or |
| 772 |
directory is HIDDEN. Consider a SYSTEM attribute also as an indicator |
| 773 |
that it is HIDDEN to InnoDB. |
| 774 |
@param[in] dirent A directory entry obtained from a call to FindFirstFile() |
| 775 |
or FindNextFile() |
| 776 |
@return true if the directory or path is HIDDEN. */ |
| 777 |
static bool is_hidden(WIN32_FIND_DATA &dirent); |
| 778 |
#endif /* WIN32 */ |
| 779 |
|
| 780 |
/** Remove quotes e.g., 'a;b' or "a;b" -> a;b. |
| 781 |
This will only remove the quotes if they are matching on the whole string. |
| 782 |
This will not work if each delimited string is quoted since this is called |
| 783 |
before the string is parsed. |
| 784 |
@return pathspec with the quotes stripped */ |
| 785 |
static std::string remove_quotes(const char *pathspec) { |
| 786 |
std::string path(pathspec); |
| 787 |
|
| 788 |
ut_ad(!path.empty()); |
| 789 |
|
| 790 |
if (path.size() >= 2 && ((path.front() == '\'' && path.back() == '\'') || |
| 791 |
(path.front() == '"' && path.back() == '"'))) { |
| 792 |
path.erase(0, 1); |
| 793 |
path.erase(path.size() - 1); |
| 794 |
} |
| 795 |
|
| 796 |
return (path); |
| 797 |
} |
| 798 |
|
| 799 |
/** Determine if a path is a relative path or not. |
| 800 |
@param[in] path OS directory or file path to evaluate |
| 801 |
@retval true if the path is relative |
| 802 |
@retval false if the path is absolute or file_name_only */ |
| 803 |
[[nodiscard]] static bool is_relative_path(const std::string &path) { |
| 804 |
return (type_of_path(path) == relative); |
| 805 |
} |
| 806 |
|
| 807 |
/** @return true if the path is an absolute path. */ |
| 808 |
[[nodiscard]] bool is_absolute_path() const { |
| 809 |
return (type_of_path(m_path) == absolute); |
| 810 |
} |
| 811 |
|
| 812 |
/** Determine if a path is an absolute path or not. |
| 813 |
@param[in] path OS directory or file path to evaluate |
| 814 |
@retval true if the path is absolute |
| 815 |
@retval false if the path is relative or file_name_only */ |
| 816 |
[[nodiscard]] static bool is_absolute_path(const std::string &path) { |
| 817 |
return (type_of_path(path) == absolute); |
| 818 |
} |
| 819 |
|
| 820 |
/** Determine what type of path is provided. |
| 821 |
@param[in] path OS directory or file path to evaluate |
| 822 |
@return the type of filepath; 'absolute', 'relative', |
| 823 |
'file_name_only', or 'invalid' if the path is empty. */ |
| 824 |
[[nodiscard]] static path_type type_of_path(const std::string &path) { |
| 825 |
if (path.empty()) { |
| 826 |
return (invalid); |
| 827 |
} |
| 828 |
|
| 829 |
/* The most likely type is a file name only with no separators. */ |
| 830 |
auto first_separator = path.find_first_of(SEPARATOR); |
| 831 |
if (first_separator == std::string::npos) { |
| 832 |
return (file_name_only); |
| 833 |
} |
| 834 |
|
| 835 |
/* Any string that starts with an OS_SEPARATOR is |
| 836 |
an absolute path. This includes any OS and even |
| 837 |
paths like "\\Host\share" on Windows. */ |
| 838 |
if (first_separator == 0) { |
| 839 |
return (absolute); |
| 840 |
} |
| 841 |
|
| 842 |
#ifdef _WIN32 |
| 843 |
/* Windows may have an absolute path like 'A:\' */ |
| 844 |
if (path.length() >= 3 && isalpha(path.at(0)) && path.at(1) == ':' && |
| 845 |
(path.at(2) == '\\' || path.at(2) == '/')) { |
| 846 |
return (absolute); |
| 847 |
} |
| 848 |
#endif /* _WIN32 */ |
| 849 |
|
| 850 |
/* Since it contains separators and is not an absolute path, |
| 851 |
it must be a relative path. */ |
| 852 |
return (relative); |
| 853 |
} |
| 854 |
|
| 855 |
/* Check if the path is prefixed with pattern. |
| 856 |
@return true if prefix matches */ |
| 857 |
[[nodiscard]] static bool has_prefix(const std::string &path, |
| 858 |
const std::string prefix) { |
| 859 |
return (path.size() >= prefix.size() && |
| 860 |
std::equal(prefix.begin(), prefix.end(), path.begin())); |
| 861 |
} |
| 862 |
|
| 863 |
/** Normalize a directory path for the current OS: |
| 864 |
On Windows, we convert '/' to '\', else we convert '\' to '/'. |
| 865 |
@param[in,out] path Directory and file path */ |
| 866 |
static void normalize(std::string &path) { |
| 867 |
for (auto &c : path) { |
| 868 |
if (c == OS_PATH_SEPARATOR_ALT) { |
| 869 |
c = OS_SEPARATOR; |
| 870 |
} |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
/** Normalize a directory path for the current OS: |
| 875 |
On Windows, we convert '/' to '\', else we convert '\' to '/'. |
| 876 |
@param[in,out] path A NUL terminated path */ |
| 877 |
static void normalize(char *path) { |
| 878 |
for (auto ptr = path; *ptr; ++ptr) { |
| 879 |
if (*ptr == OS_PATH_SEPARATOR_ALT) { |
| 880 |
*ptr = OS_SEPARATOR; |
| 881 |
} |
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
/** Convert a path string to lower case using the CHARSET my_charset_filename. |
| 886 |
@param[in,out] path Directory and file path */ |
| 887 |
static void to_lower(std::string &path) { |
| 888 |
for (auto &c : path) { |
| 889 |
c = my_tolower(&my_charset_filename, c); |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
/** @return true if the path exists and is a file . */ |
| 894 |
[[nodiscard]] static os_file_type_t get_file_type(const std::string &path); |
| 895 |
|
| 896 |
/** Return a string to display the file type of a path. |
| 897 |
@param[in] path path name |
| 898 |
@return true if the path exists and is a file . */ |
| 899 |
static const char *get_file_type_string(const std::string &path); |
| 900 |
|
| 901 |
/** Return a string to display the file type of a path. |
| 902 |
@param[in] type OS file type |
| 903 |
@return true if the path exists and is a file . */ |
| 904 |
static const char *get_file_type_string(os_file_type_t type); |
| 905 |
|
| 906 |
/** Get the real path for a directory or a file name. This path can be |
| 907 |
used to compare with another absolute path. It will be converted to |
| 908 |
lower case on case insensitive file systems and if it is a directory, |
| 909 |
it will end with a directory separator. The call to my_realpath() may |
| 910 |
fail on non-Windows platforms if the path does not exist. If so, the |
| 911 |
parameter 'force' determines what to return. |
| 912 |
@param[in] path directory or filename to convert to a real path |
| 913 |
@param[in] force if true and my_realpath() fails, use the path provided. |
| 914 |
if false and my_realpath() fails, return a null string. |
| 915 |
@return the absolute path prepared for making comparisons with other real |
| 916 |
paths. */ |
| 917 |
[[nodiscard]] static std::string get_real_path(const std::string &path, |
| 918 |
bool force = true); |
| 919 |
|
| 920 |
/** Get the basename of the file path. This is the file name without any |
| 921 |
directory separators. In other words, the file name after the last separator. |
| 922 |
@param[in] filepath The name of a file, optionally with a path. */ |
| 923 |
[[nodiscard]] static std::string get_basename(const std::string &filepath); |
| 924 |
|
| 925 |
/** Separate the portion of a directory path that exists and the portion that |
| 926 |
does not exist. |
| 927 |
@param[in] path Path to evaluate |
| 928 |
@param[in,out] ghost The portion of the path that does not exist. |
| 929 |
@return the existing portion of a path. */ |
| 930 |
[[nodiscard]] static std::string get_existing_path(const std::string &path, |
| 931 |
std::string &ghost); |
| 932 |
|
| 933 |
/** Check if the name is an undo tablespace name. |
| 934 |
@param[in] name Tablespace name |
| 935 |
@return true if it is an undo tablespace name */ |
| 936 |
[[nodiscard]] static bool is_undo_tablespace_name(const std::string &name); |
| 937 |
|
| 938 |
/** Check if the file has the the specified suffix |
| 939 |
@param[in] sfx suffix to look for |
| 940 |
@param[in] path Filename to check |
| 941 |
@return true if it has the the ".ibd" suffix. */ |
| 942 |
static bool has_suffix(ib_file_suffix sfx, const std::string &path) { |
| 943 |
const auto suffix = dot_ext[sfx]; |
| 944 |
size_t len = strlen(suffix); |
| 945 |
|
| 946 |
return (path.size() >= len && |
| 947 |
path.compare(path.size() - len, len, suffix) == 0); |
| 948 |
} |
| 949 |
|
| 950 |
/** Check if the file has the the specified suffix and truncate |
| 951 |
@param[in] sfx suffix to look for |
| 952 |
@param[in,out] path Filename to check |
| 953 |
@return true if the suffix is found and truncated. */ |
| 954 |
static bool truncate_suffix(ib_file_suffix sfx, std::string &path) { |
| 955 |
const auto suffix = dot_ext[sfx]; |
| 956 |
size_t len = strlen(suffix); |
| 957 |
|
| 958 |
if (path.size() < len || |
| 959 |
path.compare(path.size() - len, len, suffix) != 0) { |
| 960 |
return (false); |
| 961 |
} |
| 962 |
|
| 963 |
path.resize(path.size() - len); |
| 964 |
return (true); |
| 965 |
} |
| 966 |
|
| 967 |
/** Check if a character is a path separator ('\' or '/') |
| 968 |
@param[in] c Character to check |
| 969 |
@return true if it is a separator */ |
| 970 |
static bool is_separator(char c) { return (c == '\\' || c == '/'); } |
| 971 |
|
| 972 |
/** If the last character of a directory path is a separator ('\' or '/') |
| 973 |
trim it off the string. |
| 974 |
@param[in] path file system path */ |
| 975 |
static void trim_separator(std::string &path) { |
| 976 |
if (!path.empty() && is_separator(path.back())) { |
| 977 |
path.resize(path.size() - 1); |
| 978 |
} |
| 979 |
} |
| 980 |
|
| 981 |
/** If the last character of a directory path is NOT a separator, |
| 982 |
append a separator to the path. |
| 983 |
NOTE: We leave it up to the caller to assure that the path is a directory |
| 984 |
and not a file since if that directory does not yet exist, this function |
| 985 |
cannot tell the difference. |
| 986 |
@param[in] path file system path */ |
| 987 |
static void append_separator(std::string &path) { |
| 988 |
if (!path.empty() && !is_separator(path.back())) { |
| 989 |
path.push_back(OS_SEPARATOR); |
| 990 |
} |
| 991 |
} |
| 992 |
|
| 993 |
/** Allocate and build a file name from a path, a table or |
| 994 |
tablespace name and a suffix. |
| 995 |
@param[in] path_in nullptr or the directory path or |
| 996 |
the full path and filename |
| 997 |
@param[in] name_in nullptr if path is full, or |
| 998 |
Table/Tablespace name |
| 999 |
@param[in] ext the file extension to use |
| 1000 |
@param[in] trim whether last name on the path should |
| 1001 |
be trimmed |
| 1002 |
@return own: file name; must be freed by ut::free() */ |
| 1003 |
[[nodiscard]] static char *make(const std::string &path_in, |
| 1004 |
const std::string &name_in, |
| 1005 |
ib_file_suffix ext, bool trim = false); |
| 1006 |
|
| 1007 |
/** Allocate and build a CFG file name from a path. |
| 1008 |
@param[in] path_in Full path to the filename |
| 1009 |
@return own: file name; must be freed by ut::free() */ |
| 1010 |
[[nodiscard]] static char *make_cfg(const std::string &path_in) { |
| 1011 |
return (make(path_in, "", CFG)); |
| 1012 |
} |
| 1013 |
|
| 1014 |
/** Allocate and build a CFP file name from a path. |
| 1015 |
@param[in] path_in Full path to the filename |
| 1016 |
@return own: file name; must be freed by ut::free() */ |
| 1017 |
[[nodiscard]] static char *make_cfp(const std::string &path_in) { |
| 1018 |
return (make(path_in, "", CFP)); |
| 1019 |
} |
| 1020 |
|
| 1021 |
/** Allocate and build a file name from a path, a table or |
| 1022 |
tablespace name and a suffix. |
| 1023 |
@param[in] path_in nullptr or the directory path or |
| 1024 |
the full path and filename |
| 1025 |
@param[in] name_in nullptr if path is full, or |
| 1026 |
Table/Tablespace name |
| 1027 |
@return own: file name; must be freed by ut::free() */ |
| 1028 |
[[nodiscard]] static char *make_ibd(const std::string &path_in, |
| 1029 |
const std::string &name_in) { |
| 1030 |
return (make(path_in, name_in, IBD)); |
| 1031 |
} |
| 1032 |
|
| 1033 |
/** Allocate and build a file name from a path, a table or |
| 1034 |
tablespace name and a suffix. |
| 1035 |
@param[in] name_in Table/Tablespace name |
| 1036 |
@return own: file name; must be freed by ut::free() */ |
| 1037 |
[[nodiscard]] static char *make_ibd_from_table_name( |
| 1038 |
const std::string &name_in) { |
| 1039 |
return (make("", name_in, IBD)); |
| 1040 |
} |
| 1041 |
|
| 1042 |
/** Create an IBD path name after replacing the basename in an old path |
| 1043 |
with a new basename. The old_path is a full path name including the |
| 1044 |
extension. The tablename is in the normal form "schema/tablename". |
| 1045 |
@param[in] path_in Pathname |
| 1046 |
@param[in] name_in Contains new base name |
| 1047 |
@param[in] extn File extension |
| 1048 |
@return new full pathname */ |
| 1049 |
[[nodiscard]] static std::string make_new_path(const std::string &path_in, |
| 1050 |
const std::string &name_in, |
| 1051 |
ib_file_suffix extn); |
| 1052 |
|
| 1053 |
/** Parse file-per-table file name and build Innodb dictionary table name. |
| 1054 |
@param[in] file_path File name with complete path |
| 1055 |
@param[in] extn File extension |
| 1056 |
@param[out] dict_name Innodb dictionary table name |
| 1057 |
@return true, if successful. */ |
| 1058 |
static bool parse_file_path(const std::string &file_path, ib_file_suffix extn, |
| 1059 |
std::string &dict_name); |
| 1060 |
|
| 1061 |
/** This function reduces a null-terminated full remote path name |
| 1062 |
into the path that is sent by MySQL for DATA DIRECTORY clause. |
| 1063 |
It replaces the 'databasename/tablename.ibd' found at the end of the |
| 1064 |
path with just 'tablename'. |
| 1065 |
|
| 1066 |
Since the result is always smaller than the path sent in, no new |
| 1067 |
memory is allocated. The caller should allocate memory for the path |
| 1068 |
sent in. This function manipulates that path in place. If the path |
| 1069 |
format is not as expected, set data_dir_path to "" and return. |
| 1070 |
|
| 1071 |
The result is used to inform a SHOW CREATE TABLE command. |
| 1072 |
@param[in,out] data_dir_path Full path/data_dir_path */ |
| 1073 |
static void make_data_dir_path(char *data_dir_path); |
| 1074 |
|
| 1075 |
#ifndef UNIV_HOTBACKUP |
| 1076 |
/** Check if the filepath provided is in a valid placement. |
| 1077 |
This routine is run during file discovery at startup. |
| 1078 |
1) File-per-table must be in a dir named for the schema. |
| 1079 |
2) File-per-table must not be in the datadir. |
| 1080 |
3) General tablespace must not be under the datadir. |
| 1081 |
@param[in] space_name tablespace name |
| 1082 |
@param[in] space_id tablespace ID |
| 1083 |
@param[in] fsp_flags tablespace flags |
| 1084 |
@param[in] path scanned realpath to an existing file to validate |
| 1085 |
@retval true if the filepath is a valid datafile location */ |
| 1086 |
static bool is_valid_location(const char *space_name, space_id_t space_id, |
| 1087 |
uint32_t fsp_flags, const std::string &path); |
| 1088 |
|
| 1089 |
/** Check if the implicit filepath is immediately within a dir named for |
| 1090 |
the schema. |
| 1091 |
@param[in] space_name tablespace name |
| 1092 |
@param[in] path scanned realpath to an existing file to validate |
| 1093 |
@retval true if the filepath is valid */ |
| 1094 |
static bool is_valid_location_within_db(const char *space_name, |
| 1095 |
const std::string &path); |
| 1096 |
|
| 1097 |
/** Convert filename to the file system charset format. |
| 1098 |
@param[in,out] name Filename to convert */ |
| 1099 |
static void convert_to_filename_charset(std::string &name); |
| 1100 |
|
| 1101 |
/** Convert to lower case using the file system charset. |
| 1102 |
@param[in,out] path Filepath to convert */ |
| 1103 |
static void convert_to_lower_case(std::string &path); |
| 1104 |
|
| 1105 |
#endif /* !UNIV_HOTBACKUP */ |
| 1106 |
|
| 1107 |
protected: |
| 1108 |
/** Path to a file or directory. */ |
| 1109 |
std::string m_path; |
| 1110 |
|
| 1111 |
/** A full absolute path to the same file. */ |
| 1112 |
std::string m_abs_path; |
| 1113 |
}; |
| 1114 |
|
| 1115 |
/** The MySQL server --datadir value */ |
| 1116 |
extern Fil_path MySQL_datadir_path; |
| 1117 |
|
| 1118 |
/** The MySQL server --innodb-undo-directory value */ |
| 1119 |
extern Fil_path MySQL_undo_path; |
| 1120 |
|
| 1121 |
/** The undo path is different from any other known directory. */ |
| 1122 |
extern bool MySQL_undo_path_is_unique; |
| 1123 |
|
| 1124 |
/** Initial size of a single-table tablespace in pages */ |
| 1125 |
constexpr size_t FIL_IBD_FILE_INITIAL_SIZE = 7; |
| 1126 |
constexpr size_t FIL_IBT_FILE_INITIAL_SIZE = 5; |
| 1127 |
|
| 1128 |
/** An empty tablespace (CREATE TABLESPACE) has minimum |
| 1129 |
of 4 pages and an empty CREATE TABLE (file_per_table) has 6 pages. |
| 1130 |
Minimum of these two is 4 */ |
| 1131 |
constexpr size_t FIL_IBD_FILE_INITIAL_SIZE_5_7 = 4; |
| 1132 |
|
| 1133 |
/** 'null' (undefined) page offset in the context of file spaces */ |
| 1134 |
constexpr page_no_t FIL_NULL = std::numeric_limits<page_no_t>::max(); |
| 1135 |
|
| 1136 |
/** Maximum Page Number, one less than FIL_NULL */ |
| 1137 |
constexpr page_no_t PAGE_NO_MAX = std::numeric_limits<page_no_t>::max() - 1; |
| 1138 |
|
| 1139 |
/** Unknown space id */ |
| 1140 |
constexpr space_id_t SPACE_UNKNOWN = std::numeric_limits<space_id_t>::max(); |
| 1141 |
|
| 1142 |
/* Space address data type; this is intended to be used when |
| 1143 |
addresses accurate to a byte are stored in file pages. If the page part |
| 1144 |
of the address is FIL_NULL, the address is considered undefined. */ |
| 1145 |
|
| 1146 |
/** 'type' definition in C: an address stored in a file page is a |
| 1147 |
string of bytes */ |
| 1148 |
using fil_faddr_t = byte; |
| 1149 |
|
| 1150 |
/** File space address */ |
| 1151 |
struct fil_addr_t { |
| 1152 |
/* Default constructor */ |
| 1153 |
fil_addr_t() : page(FIL_NULL), boffset(0) {} |
| 1154 |
|
| 1155 |
/** Constructor |
| 1156 |
@param[in] p Logical page number |
| 1157 |
@param[in] boff Offset within the page */ |
| 1158 |
fil_addr_t(page_no_t p, uint32_t boff) : page(p), boffset(boff) {} |
| 1159 |
|
| 1160 |
/** Compare to instances |
| 1161 |
@param[in] rhs Instance to compare with |
| 1162 |
@return true if the page number and page offset are equal */ |
| 1163 |
bool is_equal(const fil_addr_t &rhs) const { |
| 1164 |
return (page == rhs.page && boffset == rhs.boffset); |
| 1165 |
} |
| 1166 |
|
| 1167 |
/** Check if the file address is null. |
| 1168 |
@return true if null */ |
| 1169 |
bool is_null() const { return (page == FIL_NULL && boffset == 0); } |
| 1170 |
|
| 1171 |
/** Print a string representation. |
| 1172 |
@param[in,out] out Stream to write to */ |
| 1173 |
std::ostream &print(std::ostream &out) const { |
| 1174 |
out << "[fil_addr_t: page=" << page << ", boffset=" << boffset << "]"; |
| 1175 |
|
| 1176 |
return (out); |
| 1177 |
} |
| 1178 |
|
| 1179 |
/** Page number within a space */ |
| 1180 |
page_no_t page; |
| 1181 |
|
| 1182 |
/** Byte offset within the page */ |
| 1183 |
uint32_t boffset; |
| 1184 |
}; |
| 1185 |
|
| 1186 |
/* For printing fil_addr_t to a stream. |
| 1187 |
@param[in,out] out Stream to write to |
| 1188 |
@param[in] obj fil_addr_t instance to write */ |
| 1189 |
inline std::ostream &operator<<(std::ostream &out, const fil_addr_t &obj) { |
| 1190 |
return (obj.print(out)); |
| 1191 |
} |
| 1192 |
|
| 1193 |
/** The null file address */ |
| 1194 |
extern fil_addr_t fil_addr_null; |
| 1195 |
|
| 1196 |
using page_type_t = uint16_t; |
| 1197 |
|
| 1198 |
/** File page types (values of FIL_PAGE_TYPE) @{ */ |
| 1199 |
/** B-tree node */ |
| 1200 |
constexpr page_type_t FIL_PAGE_INDEX = 17855; |
| 1201 |
|
| 1202 |
/** R-tree node */ |
| 1203 |
constexpr page_type_t FIL_PAGE_RTREE = 17854; |
| 1204 |
|
| 1205 |
/** Tablespace SDI Index page */ |
| 1206 |
constexpr page_type_t FIL_PAGE_SDI = 17853; |
| 1207 |
|
| 1208 |
/** This page type is unused. */ |
| 1209 |
constexpr page_type_t FIL_PAGE_TYPE_UNUSED = 1; |
| 1210 |
|
| 1211 |
/** Undo log page */ |
| 1212 |
constexpr page_type_t FIL_PAGE_UNDO_LOG = 2; |
| 1213 |
|
| 1214 |
/** Index node */ |
| 1215 |
constexpr page_type_t FIL_PAGE_INODE = 3; |
| 1216 |
|
| 1217 |
/** Insert buffer free list */ |
| 1218 |
constexpr page_type_t FIL_PAGE_IBUF_FREE_LIST = 4; |
| 1219 |
|
| 1220 |
/* File page types introduced in MySQL/InnoDB 5.1.7 */ |
| 1221 |
/** Freshly allocated page */ |
| 1222 |
constexpr page_type_t FIL_PAGE_TYPE_ALLOCATED = 0; |
| 1223 |
|
| 1224 |
/** Insert buffer bitmap */ |
| 1225 |
constexpr page_type_t FIL_PAGE_IBUF_BITMAP = 5; |
| 1226 |
|
| 1227 |
/** System page */ |
| 1228 |
constexpr page_type_t FIL_PAGE_TYPE_SYS = 6; |
| 1229 |
|
| 1230 |
/** Transaction system data */ |
| 1231 |
constexpr page_type_t FIL_PAGE_TYPE_TRX_SYS = 7; |
| 1232 |
|
| 1233 |
/** File space header */ |
| 1234 |
constexpr page_type_t FIL_PAGE_TYPE_FSP_HDR = 8; |
| 1235 |
|
| 1236 |
/** Extent descriptor page */ |
| 1237 |
constexpr page_type_t FIL_PAGE_TYPE_XDES = 9; |
| 1238 |
|
| 1239 |
/** Uncompressed BLOB page */ |
| 1240 |
constexpr page_type_t FIL_PAGE_TYPE_BLOB = 10; |
| 1241 |
|
| 1242 |
/** First compressed BLOB page */ |
| 1243 |
constexpr page_type_t FIL_PAGE_TYPE_ZBLOB = 11; |
| 1244 |
|
| 1245 |
/** Subsequent compressed BLOB page */ |
| 1246 |
constexpr page_type_t FIL_PAGE_TYPE_ZBLOB2 = 12; |
| 1247 |
|
| 1248 |
/** In old tablespaces, garbage in FIL_PAGE_TYPE is replaced with |
| 1249 |
this value when flushing pages. */ |
| 1250 |
constexpr page_type_t FIL_PAGE_TYPE_UNKNOWN = 13; |
| 1251 |
|
| 1252 |
/** Compressed page */ |
| 1253 |
constexpr page_type_t FIL_PAGE_COMPRESSED = 14; |
| 1254 |
|
| 1255 |
/** Encrypted page */ |
| 1256 |
constexpr page_type_t FIL_PAGE_ENCRYPTED = 15; |
| 1257 |
|
| 1258 |
/** Compressed and Encrypted page */ |
| 1259 |
constexpr page_type_t FIL_PAGE_COMPRESSED_AND_ENCRYPTED = 16; |
| 1260 |
|
| 1261 |
/** Encrypted R-tree page */ |
| 1262 |
constexpr page_type_t FIL_PAGE_ENCRYPTED_RTREE = 17; |
| 1263 |
|
| 1264 |
/** Uncompressed SDI BLOB page */ |
| 1265 |
constexpr page_type_t FIL_PAGE_SDI_BLOB = 18; |
| 1266 |
|
| 1267 |
/** Compressed SDI BLOB page */ |
| 1268 |
constexpr page_type_t FIL_PAGE_SDI_ZBLOB = 19; |
| 1269 |
|
| 1270 |
/** Legacy doublewrite buffer page. */ |
| 1271 |
constexpr page_type_t FIL_PAGE_TYPE_LEGACY_DBLWR = 20; |
| 1272 |
|
| 1273 |
/** Rollback Segment Array page */ |
| 1274 |
constexpr page_type_t FIL_PAGE_TYPE_RSEG_ARRAY = 21; |
| 1275 |
|
| 1276 |
/** Index pages of uncompressed LOB */ |
| 1277 |
constexpr page_type_t FIL_PAGE_TYPE_LOB_INDEX = 22; |
| 1278 |
|
| 1279 |
/** Data pages of uncompressed LOB */ |
| 1280 |
constexpr page_type_t FIL_PAGE_TYPE_LOB_DATA = 23; |
| 1281 |
|
| 1282 |
/** The first page of an uncompressed LOB */ |
| 1283 |
constexpr page_type_t FIL_PAGE_TYPE_LOB_FIRST = 24; |
| 1284 |
|
| 1285 |
/** The first page of a compressed LOB */ |
| 1286 |
constexpr page_type_t FIL_PAGE_TYPE_ZLOB_FIRST = 25; |
| 1287 |
|
| 1288 |
/** Data pages of compressed LOB */ |
| 1289 |
constexpr page_type_t FIL_PAGE_TYPE_ZLOB_DATA = 26; |
| 1290 |
|
| 1291 |
/** Index pages of compressed LOB. This page contains an array of |
| 1292 |
z_index_entry_t objects.*/ |
| 1293 |
constexpr page_type_t FIL_PAGE_TYPE_ZLOB_INDEX = 27; |
| 1294 |
|
| 1295 |
/** Fragment pages of compressed LOB. */ |
| 1296 |
constexpr page_type_t FIL_PAGE_TYPE_ZLOB_FRAG = 28; |
| 1297 |
|
| 1298 |
/** Index pages of fragment pages (compressed LOB). */ |
| 1299 |
constexpr page_type_t FIL_PAGE_TYPE_ZLOB_FRAG_ENTRY = 29; |
| 1300 |
|
| 1301 |
/** Note the highest valid non-index page_type_t. */ |
| 1302 |
constexpr page_type_t FIL_PAGE_TYPE_LAST = FIL_PAGE_TYPE_ZLOB_FRAG_ENTRY; |
| 1303 |
|
| 1304 |
/** Check whether the page type is index (Btree or Rtree or SDI) type */ |
| 1305 |
inline bool fil_page_type_is_index(page_type_t page_type) { |
| 1306 |
return page_type == FIL_PAGE_INDEX || page_type == FIL_PAGE_SDI || |
| 1307 |
page_type == FIL_PAGE_RTREE; |
| 1308 |
} |
| 1309 |
|
| 1310 |
page_type_t fil_page_get_type(const byte *page); |
| 1311 |
|
| 1312 |
/** Check whether the page is index page (either regular Btree index or Rtree |
| 1313 |
index */ |
| 1314 |
inline bool fil_page_index_page_check(const byte *page) { |
| 1315 |
return fil_page_type_is_index(fil_page_get_type(page)); |
| 1316 |
} |
| 1317 |
|
| 1318 |
/** @} */ |
| 1319 |
|
| 1320 |
/** Number of pending tablespace flushes */ |
| 1321 |
extern std::atomic<std::uint64_t> fil_n_pending_tablespace_flushes; |
| 1322 |
|
| 1323 |
/** Number of files currently open */ |
| 1324 |
extern std::atomic_size_t fil_n_files_open; |
| 1325 |
|
| 1326 |
/** Look up a tablespace. |
| 1327 |
The caller should hold an InnoDB table lock or a MDL that prevents |
| 1328 |
the tablespace from being dropped during the operation, |
| 1329 |
or the caller should be in single-threaded crash recovery mode |
| 1330 |
(no user connections that could drop tablespaces). |
| 1331 |
If this is not the case, fil_space_acquire() and fil_space_release() |
| 1332 |
should be used instead. |
| 1333 |
@param[in] space_id Tablespace ID |
| 1334 |
@return tablespace, or nullptr if not found */ |
| 1335 |
[[nodiscard]] fil_space_t *fil_space_get(space_id_t space_id); |
| 1336 |
|
| 1337 |
#ifndef UNIV_HOTBACKUP |
| 1338 |
/** Returns the latch of a file space. |
| 1339 |
@param[in] space_id Tablespace ID |
| 1340 |
@return latch protecting storage allocation */ |
| 1341 |
[[nodiscard]] rw_lock_t *fil_space_get_latch(space_id_t space_id); |
| 1342 |
|
| 1343 |
#ifdef UNIV_DEBUG |
| 1344 |
/** Gets the type of a file space. |
| 1345 |
@param[in] space_id Tablespace ID |
| 1346 |
@return file type */ |
| 1347 |
[[nodiscard]] fil_type_t fil_space_get_type(space_id_t space_id); |
| 1348 |
#endif /* UNIV_DEBUG */ |
| 1349 |
|
| 1350 |
/** Note that a tablespace has been imported. |
| 1351 |
It is initially marked as FIL_TYPE_IMPORT so that no logging is |
| 1352 |
done during the import process when the space ID is stamped to each page. |
| 1353 |
Now we change it to FIL_TYPE_TABLESPACE to start redo and undo logging. |
| 1354 |
NOTE: temporary tablespaces are never imported. |
| 1355 |
@param[in] space_id Tablespace ID */ |
| 1356 |
void fil_space_set_imported(space_id_t space_id); |
| 1357 |
#endif /* !UNIV_HOTBACKUP */ |
| 1358 |
|
| 1359 |
/** Attach a file to a tablespace. File must be closed. |
| 1360 |
@param[in] name file name (file must be closed) |
| 1361 |
@param[in] size file size in database blocks, rounded |
| 1362 |
downwards to an integer |
| 1363 |
@param[in,out] space space where to append |
| 1364 |
@param[in] is_raw true if a raw device or a raw disk partition |
| 1365 |
@param[in] atomic_write true if the file has atomic write enabled |
| 1366 |
@param[in] max_pages maximum number of pages in file |
| 1367 |
@return pointer to the file name |
| 1368 |
@retval nullptr if error */ |
| 1369 |
[[nodiscard]] char *fil_node_create(const char *name, page_no_t size, |
| 1370 |
fil_space_t *space, bool is_raw, |
| 1371 |
bool atomic_write, |
| 1372 |
page_no_t max_pages = PAGE_NO_MAX); |
| 1373 |
|
| 1374 |
/** Create a space memory object and put it to the fil_system hash table. |
| 1375 |
The tablespace name is independent from the tablespace file-name. |
| 1376 |
Error messages are issued to the server log. |
| 1377 |
@param[in] name Tablespace name |
| 1378 |
@param[in] space_id Tablespace ID |
| 1379 |
@param[in] flags Tablespace flags |
| 1380 |
@param[in] purpose Tablespace purpose |
| 1381 |
@return pointer to created tablespace, to be filled in with fil_node_create() |
| 1382 |
@retval nullptr on failure (such as when the same tablespace exists) */ |
| 1383 |
[[nodiscard]] fil_space_t *fil_space_create(const char *name, |
| 1384 |
space_id_t space_id, uint32_t flags, |
| 1385 |
fil_type_t purpose); |
| 1386 |
|
| 1387 |
/** Assigns a new space id for a new single-table tablespace. This works |
| 1388 |
simply by incrementing the global counter. If 4 billion id's is not enough, |
| 1389 |
we may need to recycle id's. |
| 1390 |
@param[out] space_id Set this to the new tablespace ID |
| 1391 |
@return true if assigned, false if not */ |
| 1392 |
[[nodiscard]] bool fil_assign_new_space_id(space_id_t *space_id); |
| 1393 |
|
| 1394 |
/** Returns the path from the first fil_node_t found with this space ID. |
| 1395 |
The caller is responsible for freeing the memory allocated here for the |
| 1396 |
value returned. |
| 1397 |
@param[in] space_id Tablespace ID |
| 1398 |
@return own: A copy of fil_node_t::path, nullptr if space ID is zero |
| 1399 |
or not found. */ |
| 1400 |
[[nodiscard]] char *fil_space_get_first_path(space_id_t space_id); |
| 1401 |
|
| 1402 |
/** Returns the size of the space in pages. The tablespace must be cached |
| 1403 |
in the memory cache. |
| 1404 |
@param[in] space_id Tablespace ID |
| 1405 |
@return space size, 0 if space not found */ |
| 1406 |
[[nodiscard]] page_no_t fil_space_get_size(space_id_t space_id); |
| 1407 |
|
| 1408 |
/** Returns the size of an undo space just after it was initialized. |
| 1409 |
@param[in] space_id Tablespace ID |
| 1410 |
@return initial space size, 0 if space not found */ |
| 1411 |
[[nodiscard]] page_no_t fil_space_get_undo_initial_size(space_id_t space_id); |
| 1412 |
|
| 1413 |
/** This is called for an undo tablespace after it has been initialized |
| 1414 |
or opened. It sets the minimum size in pages at which it should be truncated |
| 1415 |
and the number of pages that it should be extended. An undo tablespace is |
| 1416 |
extended by larger amounts than normal tablespaces. It starts at 16Mb and |
| 1417 |
is increased during aggressive growth and decreased when the growth is slower. |
| 1418 |
@param[in] space_id Tablespace ID |
| 1419 |
@param[in] use_current If true, use the current size in pages as the initial |
| 1420 |
size. If false, use UNDO_INITIAL_SIZE_IN_PAGES. */ |
| 1421 |
void fil_space_set_undo_size(space_id_t space_id, bool use_current); |
| 1422 |
|
| 1423 |
/** Returns the flags of the space. The tablespace must be cached |
| 1424 |
in the memory cache. |
| 1425 |
@param[in] space_id Tablespace ID for which to get the flags |
| 1426 |
@return flags, ULINT_UNDEFINED if space not found */ |
| 1427 |
[[nodiscard]] uint32_t fil_space_get_flags(space_id_t space_id); |
| 1428 |
|
| 1429 |
/** Sets the flags of the tablespace. The tablespace must be locked |
| 1430 |
in MDL_EXCLUSIVE MODE. |
| 1431 |
@param[in] space tablespace in-memory struct |
| 1432 |
@param[in] flags tablespace flags */ |
| 1433 |
void fil_space_set_flags(fil_space_t *space, uint32_t flags); |
| 1434 |
|
| 1435 |
/** Open each file of a tablespace if not already open. |
| 1436 |
@param[in] space_id Tablespace ID |
| 1437 |
@retval true if all file nodes were opened |
| 1438 |
@retval false on failure */ |
| 1439 |
[[nodiscard]] bool fil_space_open(space_id_t space_id); |
| 1440 |
|
| 1441 |
/** Close each file of a tablespace if open. |
| 1442 |
@param[in] space_id Tablespace ID */ |
| 1443 |
void fil_space_close(space_id_t space_id); |
| 1444 |
|
| 1445 |
/** Returns the page size of the space and whether it is compressed or not. |
| 1446 |
The tablespace must be cached in the memory cache. |
| 1447 |
@param[in] space_id Tablespace ID |
| 1448 |
@param[out] found true if tablespace was found |
| 1449 |
@return page size */ |
| 1450 |
[[nodiscard]] const page_size_t fil_space_get_page_size(space_id_t space_id, |
| 1451 |
bool *found); |
| 1452 |
|
| 1453 |
/** Initializes the tablespace memory cache. |
| 1454 |
@param[in] max_n_open Maximum number of open files */ |
| 1455 |
void fil_init(ulint max_n_open); |
| 1456 |
|
| 1457 |
/** Changes the maximum opened files limit. |
| 1458 |
@param[in, out] new_max_open_files New value for the open files limit. If the |
| 1459 |
limit cannot be changed, the value is changed to a minimum value recommended. |
| 1460 |
@return true if the new limit was set. */ |
| 1461 |
bool fil_open_files_limit_update(size_t &new_max_open_files); |
| 1462 |
|
| 1463 |
/** Initializes the tablespace memory cache. */ |
| 1464 |
void fil_close(); |
| 1465 |
|
| 1466 |
/** Opens all log files and system tablespace data files. |
| 1467 |
They stay open until the database server shutdown. This should be called |
| 1468 |
at a server startup after the space objects for the log and the system |
| 1469 |
tablespace have been created. The purpose of this operation is to make |
| 1470 |
sure we never run out of file descriptors if we need to read from the |
| 1471 |
insert buffer or to write to the log. */ |
| 1472 |
void fil_open_system_tablespace_files(); |
| 1473 |
|
| 1474 |
/** Closes all open files. There must not be any pending i/o's or not flushed |
| 1475 |
modifications in the files. */ |
| 1476 |
void fil_close_all_files(); |
| 1477 |
|
| 1478 |
/** Iterate over the files in all the tablespaces. */ |
| 1479 |
class Fil_iterator { |
| 1480 |
public: |
| 1481 |
using Function = std::function<dberr_t(fil_node_t *)>; |
| 1482 |
|
| 1483 |
/** For each data file. |
| 1484 |
@param[in] f Callback */ |
| 1485 |
template <typename F> |
| 1486 |
static dberr_t for_each_file(F &&f) { |
| 1487 |
return iterate([=](fil_node_t *file) { return (f(file)); }); |
| 1488 |
} |
| 1489 |
|
| 1490 |
/** Iterate through all persistent tablespace files (FIL_TYPE_TABLESPACE) |
| 1491 |
returning the nodes via callback function f. |
| 1492 |
@param[in] f Callback |
| 1493 |
@return any error returned by the callback function. */ |
| 1494 |
static dberr_t iterate(Function &&f); |
| 1495 |
}; |
| 1496 |
|
| 1497 |
/** Sets the max tablespace id counter if the given number is bigger than the |
| 1498 |
previous value. |
| 1499 |
@param[in] max_id Maximum known tablespace ID */ |
| 1500 |
void fil_set_max_space_id_if_bigger(space_id_t max_id); |
| 1501 |
|
| 1502 |
#ifndef UNIV_HOTBACKUP |
| 1503 |
|
| 1504 |
/** Write the flushed LSN to the page header of the first page in the |
| 1505 |
system tablespace. |
| 1506 |
@param[in] lsn Flushed LSN |
| 1507 |
@return DB_SUCCESS or error number */ |
| 1508 |
[[nodiscard]] dberr_t fil_write_flushed_lsn(lsn_t lsn); |
| 1509 |
|
| 1510 |
#else /* !UNIV_HOTBACKUP */ |
| 1511 |
/** Frees a space object from the tablespace memory cache. |
| 1512 |
Closes a tablespaces' files but does not delete them. |
| 1513 |
There must not be any pending i/o's or flushes on the files. |
| 1514 |
@param[in] space_id Tablespace ID |
| 1515 |
@return true if success */ |
| 1516 |
bool meb_fil_space_free(space_id_t space_id); |
| 1517 |
|
| 1518 |
/** Extends all tablespaces to the size stored in the space header. During the |
| 1519 |
mysqlbackup --apply-log phase we extended the spaces on-demand so that log |
| 1520 |
records could be applied, but that may have left spaces still too small |
| 1521 |
compared to the size stored in the space header. */ |
| 1522 |
void meb_extend_tablespaces_to_stored_len(); |
| 1523 |
|
| 1524 |
/** Process a file name passed as an input |
| 1525 |
@param[in] name absolute path of tablespace file |
| 1526 |
@param[in] space_id the tablespace ID */ |
| 1527 |
void meb_fil_name_process(const char *name, space_id_t space_id); |
| 1528 |
|
| 1529 |
#endif /* !UNIV_HOTBACKUP */ |
| 1530 |
|
| 1531 |
/** Acquire a tablespace when it could be dropped concurrently. |
| 1532 |
Used by background threads that do not necessarily hold proper locks |
| 1533 |
for concurrency control. |
| 1534 |
@param[in] space_id Tablespace ID |
| 1535 |
@return the tablespace, or nullptr if missing or being deleted */ |
| 1536 |
[[nodiscard]] fil_space_t *fil_space_acquire(space_id_t space_id); |
| 1537 |
|
| 1538 |
/** Acquire a tablespace that may not exist. |
| 1539 |
Used by background threads that do not necessarily hold proper locks |
| 1540 |
for concurrency control. |
| 1541 |
@param[in] space_id Tablespace ID |
| 1542 |
@return the tablespace, or nullptr if missing or being deleted */ |
| 1543 |
[[nodiscard]] fil_space_t *fil_space_acquire_silent(space_id_t space_id); |
| 1544 |
|
| 1545 |
/** Release a tablespace acquired with fil_space_acquire(). |
| 1546 |
@param[in,out] space Tablespace to release */ |
| 1547 |
void fil_space_release(fil_space_t *space); |
| 1548 |
|
| 1549 |
/** Fetch the file name opened for a space_id from the file map. |
| 1550 |
@param[in] space_id tablespace ID |
| 1551 |
@param[out] name the scanned filename |
| 1552 |
@return true if the space_id is found. The name is set to an |
| 1553 |
empty string if the space_id is not found. */ |
| 1554 |
bool fil_system_get_file_by_space_id(space_id_t space_id, std::string &name); |
| 1555 |
|
| 1556 |
/** Fetch the file name opened for an undo space number from the file map. |
| 1557 |
@param[in] space_num Undo tablespace Number |
| 1558 |
@param[out] space_id Undo tablespace ID |
| 1559 |
@param[out] name the scanned filename |
| 1560 |
@return true if the space_num was found. The name is set to an |
| 1561 |
empty string if the space_num is not found. */ |
| 1562 |
bool fil_system_get_file_by_space_num(space_id_t space_num, |
| 1563 |
space_id_t &space_id, std::string &name); |
| 1564 |
|
| 1565 |
/** Truncate the tablespace to needed size. |
| 1566 |
@param[in] space_id Tablespace ID to truncate |
| 1567 |
@param[in] size_in_pages Truncate size. |
| 1568 |
@return true if truncate was successful. */ |
| 1569 |
[[nodiscard]] bool fil_truncate_tablespace(space_id_t space_id, |
| 1570 |
page_no_t size_in_pages); |
| 1571 |
|
| 1572 |
/** Closes a single-table tablespace. The tablespace must be cached in the |
| 1573 |
memory cache. Free all pages used by the tablespace. |
| 1574 |
@param[in] space_id Tablespace ID |
| 1575 |
@return DB_SUCCESS or error */ |
| 1576 |
[[nodiscard]] dberr_t fil_close_tablespace(space_id_t space_id); |
| 1577 |
|
| 1578 |
/** Discards a single-table tablespace. The tablespace must be cached in the |
| 1579 |
memory cache. Discarding is like deleting a tablespace, but |
| 1580 |
|
| 1581 |
1. We do not drop the table from the data dictionary; |
| 1582 |
|
| 1583 |
2. We remove all insert buffer entries for the tablespace immediately; |
| 1584 |
in DROP TABLE they are only removed gradually in the background; |
| 1585 |
|
| 1586 |
3. When the user does IMPORT TABLESPACE, the tablespace will have the |
| 1587 |
same id as it originally had. |
| 1588 |
|
| 1589 |
4. Free all the pages in use by the tablespace if rename=true. |
| 1590 |
@param[in] space_id Tablespace ID |
| 1591 |
@return DB_SUCCESS or error */ |
| 1592 |
[[nodiscard]] dberr_t fil_discard_tablespace(space_id_t space_id); |
| 1593 |
|
| 1594 |
/** Test if a tablespace file can be renamed to a new filepath by checking |
| 1595 |
if that the old filepath exists and the new filepath does not exist. |
| 1596 |
@param[in] space_id Tablespace ID |
| 1597 |
@param[in] old_path Old filepath |
| 1598 |
@param[in] new_path New filepath |
| 1599 |
@param[in] is_discarded Whether the tablespace is discarded |
| 1600 |
@return innodb error code */ |
| 1601 |
[[nodiscard]] dberr_t fil_rename_tablespace_check(space_id_t space_id, |
| 1602 |
const char *old_path, |
| 1603 |
const char *new_path, |
| 1604 |
bool is_discarded); |
| 1605 |
|
| 1606 |
/** Rename a single-table tablespace. |
| 1607 |
The tablespace must exist in the memory cache. |
| 1608 |
@param[in] space_id Tablespace ID |
| 1609 |
@param[in] old_path Old file name |
| 1610 |
@param[in] new_name New tablespace name in the schema/name format |
| 1611 |
@param[in] new_path_in New file name, or nullptr if it is located in |
| 1612 |
the normal data directory |
| 1613 |
@return InnoDB error code */ |
| 1614 |
[[nodiscard]] dberr_t fil_rename_tablespace(space_id_t space_id, |
| 1615 |
const char *old_path, |
| 1616 |
const char *new_name, |
| 1617 |
const char *new_path_in); |
| 1618 |
|
| 1619 |
/** Create an IBD tablespace file. |
| 1620 |
@param[in] space_id Tablespace ID |
| 1621 |
@param[in] name Tablespace name in dbname/tablename format. |
| 1622 |
For general tablespaces, the 'dbname/' part |
| 1623 |
may be missing. |
| 1624 |
@param[in] path Path and filename of the datafile to create. |
| 1625 |
@param[in] flags Tablespace flags |
| 1626 |
@param[in] size Initial size of the tablespace file in pages, |
| 1627 |
must be >= FIL_IBD_FILE_INITIAL_SIZE |
| 1628 |
@return DB_SUCCESS or error code */ |
| 1629 |
[[nodiscard]] dberr_t fil_ibd_create(space_id_t space_id, const char *name, |
| 1630 |
const char *path, uint32_t flags, |
| 1631 |
page_no_t size); |
| 1632 |
|
| 1633 |
/** Create a session temporary tablespace (IBT) file. |
| 1634 |
@param[in] space_id Tablespace ID |
| 1635 |
@param[in] name Tablespace name |
| 1636 |
@param[in] path Path and filename of the datafile to create. |
| 1637 |
@param[in] flags Tablespace flags |
| 1638 |
@param[in] size Initial size of the tablespace file in pages, |
| 1639 |
must be >= FIL_IBT_FILE_INITIAL_SIZE |
| 1640 |
@return DB_SUCCESS or error code */ |
| 1641 |
[[nodiscard]] dberr_t fil_ibt_create(space_id_t space_id, const char *name, |
| 1642 |
const char *path, uint32_t flags, |
| 1643 |
page_no_t size); |
| 1644 |
|
| 1645 |
/** Deletes an IBD or IBU tablespace. |
| 1646 |
The tablespace must be cached in the memory cache. This will delete the |
| 1647 |
datafile, fil_space_t & fil_node_t entries from the file_system_t cache. |
| 1648 |
@param[in] space_id Tablespace ID |
| 1649 |
@param[in] buf_remove Specify the action to take on the pages |
| 1650 |
for this table in the buffer pool. |
| 1651 |
@return DB_SUCCESS, DB_TABLESPCE_NOT_FOUND or DB_IO_ERROR */ |
| 1652 |
[[nodiscard]] dberr_t fil_delete_tablespace(space_id_t space_id, |
| 1653 |
buf_remove_t buf_remove); |
| 1654 |
|
| 1655 |
/** Open a single-table tablespace and optionally do some validation such |
| 1656 |
as checking that the space id is correct. If the file is already open, |
| 1657 |
the validation will be done before reporting success. |
| 1658 |
If not successful, print an error message to the error log. |
| 1659 |
This function is used to open a tablespace when we start up mysqld, |
| 1660 |
and also in IMPORT TABLESPACE. |
| 1661 |
NOTE that we assume this operation is used either at the database startup |
| 1662 |
or under the protection of the dictionary mutex, so that two users cannot |
| 1663 |
race here. |
| 1664 |
|
| 1665 |
The fil_node_t::handle will not be left open. |
| 1666 |
|
| 1667 |
@param[in] validate whether we should validate the tablespace |
| 1668 |
(read the first page of the file and |
| 1669 |
check that the space id in it matches id) |
| 1670 |
@param[in] purpose FIL_TYPE_TABLESPACE or FIL_TYPE_TEMPORARY |
| 1671 |
@param[in] space_id Tablespace ID |
| 1672 |
@param[in] flags tablespace flags |
| 1673 |
@param[in] space_name tablespace name of the datafile |
| 1674 |
If file-per-table, it is the table name in the |
| 1675 |
databasename/tablename format |
| 1676 |
@param[in] path_in expected filepath, usually read from dictionary |
| 1677 |
@param[in] strict whether to report error when open ibd failed |
| 1678 |
@param[in] old_space whether it is a 5.7 tablespace opening |
| 1679 |
by upgrade |
| 1680 |
@return DB_SUCCESS or error code */ |
| 1681 |
[[nodiscard]] dberr_t fil_ibd_open(bool validate, fil_type_t purpose, |
| 1682 |
space_id_t space_id, uint32_t flags, |
| 1683 |
const char *space_name, const char *path_in, |
| 1684 |
bool strict, bool old_space); |
| 1685 |
|
| 1686 |
/** Returns true if a matching tablespace exists in the InnoDB tablespace |
| 1687 |
memory cache. |
| 1688 |
@param[in] space_id Tablespace ID |
| 1689 |
@param[in] name Tablespace name used in space_create(). |
| 1690 |
@param[in] print_err Print detailed error information to the |
| 1691 |
error log if a matching tablespace is |
| 1692 |
not found from memory. |
| 1693 |
@param[in] adjust_space Whether to adjust space id on mismatch |
| 1694 |
@return true if a matching tablespace exists in the memory cache */ |
| 1695 |
[[nodiscard]] bool fil_space_exists_in_mem(space_id_t space_id, |
| 1696 |
const char *name, bool print_err, |
| 1697 |
bool adjust_space); |
| 1698 |
|
| 1699 |
/** Extends all tablespaces to the size stored in the space header. During the |
| 1700 |
mysqlbackup --apply-log phase we extended the spaces on-demand so that log |
| 1701 |
records could be appllied, but that may have left spaces still too small |
| 1702 |
compared to the size stored in the space header. */ |
| 1703 |
void fil_extend_tablespaces_to_stored_len(); |
| 1704 |
|
| 1705 |
/** Try to extend a tablespace if it is smaller than the specified size. |
| 1706 |
@param[in,out] space Tablespace ID |
| 1707 |
@param[in] size desired size in pages |
| 1708 |
@return whether the tablespace is at least as big as requested */ |
| 1709 |
[[nodiscard]] bool fil_space_extend(fil_space_t *space, page_no_t size); |
| 1710 |
|
| 1711 |
/** Tries to reserve free extents in a file space. |
| 1712 |
@param[in] space_id Tablespace ID |
| 1713 |
@param[in] n_free_now Number of free extents now |
| 1714 |
@param[in] n_to_reserve How many one wants to reserve |
| 1715 |
@return true if succeed */ |
| 1716 |
[[nodiscard]] bool fil_space_reserve_free_extents(space_id_t space_id, |
| 1717 |
ulint n_free_now, |
| 1718 |
ulint n_to_reserve); |
| 1719 |
|
| 1720 |
/** Releases free extents in a file space. |
| 1721 |
@param[in] space_id Tablespace ID |
| 1722 |
@param[in] n_reserved How many were reserved */ |
| 1723 |
void fil_space_release_free_extents(space_id_t space_id, ulint n_reserved); |
| 1724 |
|
| 1725 |
/** Gets the number of reserved extents. If the database is silent, this |
| 1726 |
number should be zero. |
| 1727 |
@param[in] space_id Tablespace ID |
| 1728 |
@return the number of reserved extents */ |
| 1729 |
[[nodiscard]] ulint fil_space_get_n_reserved_extents(space_id_t space_id); |
| 1730 |
|
| 1731 |
/** Read or write data from a file. |
| 1732 |
@param[in] type IO context |
| 1733 |
@param[in] sync If true then do synchronous IO |
| 1734 |
@param[in] page_id page id |
| 1735 |
@param[in] page_size page size |
| 1736 |
@param[in] byte_offset remainder of offset in bytes; in aio this |
| 1737 |
must be divisible by the OS block size |
| 1738 |
@param[in] len how many bytes to read or write; this must |
| 1739 |
not cross a file boundary; in AIO this must |
| 1740 |
be a block size multiple |
| 1741 |
@param[in,out] buf buffer where to store read data or from where |
| 1742 |
to write; in AIO this must be appropriately |
| 1743 |
aligned |
| 1744 |
@param[in] message message for AIO handler if !sync, else ignored |
| 1745 |
@return error code |
| 1746 |
@retval DB_SUCCESS on success |
| 1747 |
@retval DB_TABLESPACE_DELETED if the tablespace does not exist */ |
| 1748 |
[[nodiscard]] dberr_t fil_io(const IORequest &type, bool sync, |
| 1749 |
const page_id_t &page_id, |
| 1750 |
const page_size_t &page_size, ulint byte_offset, |
| 1751 |
ulint len, void *buf, void *message); |
| 1752 |
|
| 1753 |
/** Waits for an AIO operation to complete. This function is used to write the |
| 1754 |
handler for completed requests. The aio array of pending requests is divided |
| 1755 |
into segments (see os0file.cc for more info). The thread specifies which |
| 1756 |
segment it wants to wait for. |
| 1757 |
@param[in] segment The number of the segment in the AIO array |
| 1758 |
to wait for */ |
| 1759 |
void fil_aio_wait(ulint segment); |
| 1760 |
|
| 1761 |
/** Flushes to disk possible writes cached by the OS. If the space does |
| 1762 |
not exist or is being dropped, does not do anything. |
| 1763 |
@param[in] space_id Tablespace ID */ |
| 1764 |
void fil_flush(space_id_t space_id); |
| 1765 |
|
| 1766 |
/** Flush to disk the writes in file spaces possibly cached by the OS |
| 1767 |
(note: spaces of type FIL_TYPE_TEMPORARY are skipped) */ |
| 1768 |
void fil_flush_file_spaces(); |
| 1769 |
|
| 1770 |
#ifdef UNIV_DEBUG |
| 1771 |
/** Checks the consistency of the tablespace cache. |
| 1772 |
@return true if ok */ |
| 1773 |
bool fil_validate(); |
| 1774 |
#endif /* UNIV_DEBUG */ |
| 1775 |
|
| 1776 |
/** Returns true if file address is undefined. |
| 1777 |
@param[in] addr File address to check |
| 1778 |
@return true if undefined */ |
| 1779 |
[[nodiscard]] bool fil_addr_is_null(const fil_addr_t &addr); |
| 1780 |
|
| 1781 |
/** Get the predecessor of a file page. |
| 1782 |
@param[in] page File page |
| 1783 |
@return FIL_PAGE_PREV */ |
| 1784 |
[[nodiscard]] page_no_t fil_page_get_prev(const byte *page); |
| 1785 |
|
| 1786 |
/** Get the successor of a file page. |
| 1787 |
@param[in] page File page |
| 1788 |
@return FIL_PAGE_NEXT */ |
| 1789 |
[[nodiscard]] page_no_t fil_page_get_next(const byte *page); |
| 1790 |
|
| 1791 |
/** Sets the file page type. |
| 1792 |
@param[in,out] page File page |
| 1793 |
@param[in] type File page type to set */ |
| 1794 |
void fil_page_set_type(byte *page, ulint type); |
| 1795 |
|
| 1796 |
/** Reset the page type. |
| 1797 |
Data files created before MySQL 5.1 may contain garbage in FIL_PAGE_TYPE. |
| 1798 |
In MySQL 3.23.53, only undo log pages and index pages were tagged. |
| 1799 |
Any other pages were written with uninitialized bytes in FIL_PAGE_TYPE. |
| 1800 |
@param[in] page_id Page number |
| 1801 |
@param[in,out] page Page with invalid FIL_PAGE_TYPE |
| 1802 |
@param[in] type Expected page type |
| 1803 |
@param[in,out] mtr Mini-transaction */ |
| 1804 |
void fil_page_reset_type(const page_id_t &page_id, byte *page, ulint type, |
| 1805 |
mtr_t *mtr); |
| 1806 |
|
| 1807 |
/** Get the file page type. |
| 1808 |
@param[in] page File page |
| 1809 |
@return page type */ |
| 1810 |
inline page_type_t fil_page_get_type(const byte *page) { |
| 1811 |
return (static_cast<page_type_t>(mach_read_from_2(page + FIL_PAGE_TYPE))); |
| 1812 |
} |
| 1813 |
|
| 1814 |
#ifdef UNIV_DEBUG |
| 1815 |
/** Increase redo skipped count for a tablespace. |
| 1816 |
@param[in] space_id Tablespace ID */ |
| 1817 |
void fil_space_inc_redo_skipped_count(space_id_t space_id); |
| 1818 |
|
| 1819 |
/** Decrease redo skipped count for a tablespace. |
| 1820 |
@param[in] space_id Tablespace ID */ |
| 1821 |
void fil_space_dec_redo_skipped_count(space_id_t space_id); |
| 1822 |
|
| 1823 |
/** Check whether a single-table tablespace is redo skipped. |
| 1824 |
@param[in] space_id Tablespace ID |
| 1825 |
@return true if redo skipped */ |
| 1826 |
[[nodiscard]] bool fil_space_is_redo_skipped(space_id_t space_id); |
| 1827 |
#endif /* UNIV_DEBUG */ |
| 1828 |
|
| 1829 |
/** Delete the tablespace file and any related files like .cfg. |
| 1830 |
This should not be called for temporary tables. |
| 1831 |
@param[in] path File path of the IBD tablespace |
| 1832 |
@return true on success */ |
| 1833 |
[[nodiscard]] bool fil_delete_file(const char *path); |
| 1834 |
|
| 1835 |
/** Callback functor. */ |
| 1836 |
struct PageCallback { |
| 1837 |
/** Default constructor */ |
| 1838 |
PageCallback() : m_page_size(0, 0, false), m_filepath() UNIV_NOTHROW {} |
| 1839 |
|
| 1840 |
virtual ~PageCallback() UNIV_NOTHROW = default; |
| 1841 |
|
| 1842 |
/** Called for page 0 in the tablespace file at the start. |
| 1843 |
@param file_size size of the file in bytes |
| 1844 |
@param block contents of the first page in the tablespace file |
| 1845 |
@retval DB_SUCCESS or error code. */ |
| 1846 |
[[nodiscard]] virtual dberr_t init(os_offset_t file_size, |
| 1847 |
const buf_block_t *block) UNIV_NOTHROW = 0; |
| 1848 |
|
| 1849 |
/** Called for every page in the tablespace. If the page was not |
| 1850 |
updated then its state must be set to BUF_PAGE_NOT_USED. For |
| 1851 |
compressed tables the page descriptor memory will be at offset: |
| 1852 |
block->frame + UNIV_PAGE_SIZE; |
| 1853 |
@param offset physical offset within the file |
| 1854 |
@param block block read from file, note it is not from the buffer pool |
| 1855 |
@retval DB_SUCCESS or error code. */ |
| 1856 |
[[nodiscard]] virtual dberr_t operator()(os_offset_t offset, |
| 1857 |
buf_block_t *block) UNIV_NOTHROW = 0; |
| 1858 |
|
| 1859 |
/** Set the name of the physical file and the file handle that is used |
| 1860 |
to open it for the file that is being iterated over. |
| 1861 |
@param filename then physical name of the tablespace file. |
| 1862 |
@param file OS file handle */ |
| 1863 |
void set_file(const char *filename, pfs_os_file_t file) UNIV_NOTHROW { |
| 1864 |
m_file = file; |
| 1865 |
m_filepath = filename; |
| 1866 |
} |
| 1867 |
|
| 1868 |
/** @return the space id of the tablespace */ |
| 1869 |
[[nodiscard]] virtual space_id_t get_space_id() const UNIV_NOTHROW = 0; |
| 1870 |
|
| 1871 |
/** |
| 1872 |
@retval the space flags of the tablespace being iterated over */ |
| 1873 |
[[nodiscard]] virtual ulint get_space_flags() const UNIV_NOTHROW = 0; |
| 1874 |
|
| 1875 |
/** Set the tablespace table size. |
| 1876 |
@param[in] page a page belonging to the tablespace */ |
| 1877 |
void set_page_size(const buf_frame_t *page) UNIV_NOTHROW; |
| 1878 |
|
| 1879 |
/** The compressed page size |
| 1880 |
@return the compressed page size */ |
| 1881 |
[[nodiscard]] const page_size_t &get_page_size() const { |
| 1882 |
return (m_page_size); |
| 1883 |
} |
| 1884 |
|
| 1885 |
/** The tablespace page size. */ |
| 1886 |
page_size_t m_page_size; |
| 1887 |
|
| 1888 |
/** File handle to the tablespace */ |
| 1889 |
pfs_os_file_t m_file; |
| 1890 |
|
| 1891 |
/** Physical file path. */ |
| 1892 |
const char *m_filepath; |
| 1893 |
|
| 1894 |
// Disable copying |
| 1895 |
PageCallback(PageCallback &&) = delete; |
| 1896 |
PageCallback(const PageCallback &) = delete; |
| 1897 |
PageCallback &operator=(const PageCallback &) = delete; |
| 1898 |
}; |
| 1899 |
|
| 1900 |
/** Iterate over all the pages in the tablespace. |
| 1901 |
@param[in] encryption_metadata the encryption metadata to use for reading |
| 1902 |
@param[in] table the table definition in the server |
| 1903 |
@param[in] n_io_buffers number of blocks to read and write together |
| 1904 |
@param[in] compression_type compression type if compression is enabled, |
| 1905 |
else Compression::Type::NONE |
| 1906 |
@param[in,out] callback functor that will do the page updates |
| 1907 |
@return DB_SUCCESS or error code */ |
| 1908 |
[[nodiscard]] dberr_t fil_tablespace_iterate( |
| 1909 |
const Encryption_metadata &encryption_metadata, dict_table_t *table, |
| 1910 |
ulint n_io_buffers, Compression::Type compression_type, |
| 1911 |
PageCallback &callback); |
| 1912 |
|
| 1913 |
/** Looks for a pre-existing fil_space_t with the given tablespace ID |
| 1914 |
and, if found, returns the name and filepath in newly allocated buffers |
| 1915 |
that the caller must free. |
| 1916 |
@param[in] space_id The tablespace ID to search for. |
| 1917 |
@param[out] name Name of the tablespace found. |
| 1918 |
@param[out] filepath The filepath of the first datafile for the |
| 1919 |
tablespace. |
| 1920 |
@return true if tablespace is found, false if not. */ |
| 1921 |
[[nodiscard]] bool fil_space_read_name_and_filepath(space_id_t space_id, |
| 1922 |
char **name, |
| 1923 |
char **filepath); |
| 1924 |
|
| 1925 |
/** Convert a file name to a tablespace name. Strip the file name |
| 1926 |
prefix and suffix, leaving only databasename/tablename. |
| 1927 |
@param[in] filename directory/databasename/tablename.ibd |
| 1928 |
@return database/tablename string, to be freed with ut::free() */ |
| 1929 |
[[nodiscard]] char *fil_path_to_space_name(const char *filename); |
| 1930 |
|
| 1931 |
/** Returns the space ID based on the tablespace name. |
| 1932 |
The tablespace must be found in the tablespace memory cache. |
| 1933 |
This call is made from external to this module, so the mutex is not owned. |
| 1934 |
@param[in] name Tablespace name |
| 1935 |
@return space ID if tablespace found, SPACE_UNKNOWN if space not. */ |
| 1936 |
[[nodiscard]] space_id_t fil_space_get_id_by_name(const char *name); |
| 1937 |
|
| 1938 |
/** Check if swapping two .ibd files can be done without failure |
| 1939 |
@param[in] old_table old table |
| 1940 |
@param[in] new_table new table |
| 1941 |
@param[in] tmp_name temporary table name |
| 1942 |
@return innodb error code */ |
| 1943 |
[[nodiscard]] dberr_t fil_rename_precheck(const dict_table_t *old_table, |
| 1944 |
const dict_table_t *new_table, |
| 1945 |
const char *tmp_name); |
| 1946 |
|
| 1947 |
/** Set the compression type for the tablespace |
| 1948 |
@param[in] space_id Space ID of the tablespace |
| 1949 |
@param[in] algorithm Text representation of the algorithm |
| 1950 |
@return DB_SUCCESS or error code */ |
| 1951 |
[[nodiscard]] dberr_t fil_set_compression(space_id_t space_id, |
| 1952 |
const char *algorithm); |
| 1953 |
|
| 1954 |
/** Get the compression algorithm for a tablespace. |
| 1955 |
@param[in] space_id Space ID to check |
| 1956 |
@return the compression algorithm */ |
| 1957 |
[[nodiscard]] Compression::Type fil_get_compression(space_id_t space_id); |
| 1958 |
|
| 1959 |
/** Set encryption information for IORequest. |
| 1960 |
@param[in,out] req_type IO request |
| 1961 |
@param[in] page_id page id |
| 1962 |
@param[in] space table space */ |
| 1963 |
void fil_io_set_encryption(IORequest &req_type, const page_id_t &page_id, |
| 1964 |
fil_space_t *space); |
| 1965 |
|
| 1966 |
/** Set the encryption type for the tablespace |
| 1967 |
@param[in] space_id Space ID of tablespace for which to set |
| 1968 |
@param[in] algorithm Encryption algorithm |
| 1969 |
@param[in] key Encryption key |
| 1970 |
@param[in] iv Encryption iv |
| 1971 |
@return DB_SUCCESS or error code */ |
| 1972 |
[[nodiscard]] dberr_t fil_set_encryption(space_id_t space_id, |
| 1973 |
Encryption::Type algorithm, byte *key, |
| 1974 |
byte *iv); |
| 1975 |
|
| 1976 |
/** Set the autoextend_size attribute for the tablespace |
| 1977 |
@param[in] space_id Space ID of tablespace for which to set |
| 1978 |
@param[in] autoextend_size Value of autoextend_size attribute |
| 1979 |
@return DB_SUCCESS or error code */ |
| 1980 |
dberr_t fil_set_autoextend_size(space_id_t space_id, uint64_t autoextend_size); |
| 1981 |
|
| 1982 |
/** Reset the encryption type for the tablespace |
| 1983 |
@param[in] space_id Space ID of tablespace for which to set |
| 1984 |
@return DB_SUCCESS or error code */ |
| 1985 |
[[nodiscard]] dberr_t fil_reset_encryption(space_id_t space_id); |
| 1986 |
|
| 1987 |
/** Rotate the tablespace keys by new master key. |
| 1988 |
@return the number of tablespaces that failed to rotate. */ |
| 1989 |
[[nodiscard]] size_t fil_encryption_rotate(); |
| 1990 |
|
| 1991 |
/** Roencrypt the tablespace keys by current master key. */ |
| 1992 |
void fil_encryption_reencrypt(std::vector<space_id_t> &sid_vector); |
| 1993 |
|
| 1994 |
/** During crash recovery, open a tablespace if it had not been opened |
| 1995 |
yet, to get valid size and flags. |
| 1996 |
@param[in,out] space Tablespace instance */ |
| 1997 |
inline void fil_space_open_if_needed(fil_space_t *space) { |
| 1998 |
if (space->size == 0) { |
| 1999 |
/* Initially, size and flags will be set to 0, |
| 2000 |
until the files are opened for the first time. |
| 2001 |
fil_space_get_size() will open the file |
| 2002 |
and adjust the size and flags. */ |
| 2003 |
page_no_t size = fil_space_get_size(space->id); |
| 2004 |
|
| 2005 |
ut_a(size == space->size); |
| 2006 |
} |
| 2007 |
} |
| 2008 |
|
| 2009 |
#if !defined(NO_FALLOCATE) && defined(UNIV_LINUX) |
| 2010 |
/** |
| 2011 |
Try and enable FusionIO atomic writes. |
| 2012 |
@param[in] file OS file handle |
| 2013 |
@return true if successful */ |
| 2014 |
[[nodiscard]] bool fil_fusionio_enable_atomic_write(pfs_os_file_t file); |
| 2015 |
#endif /* !NO_FALLOCATE && UNIV_LINUX */ |
| 2016 |
|
| 2017 |
/** Note that the file system where the file resides doesn't support PUNCH HOLE. |
| 2018 |
Called from AIO handlers when IO returns DB_IO_NO_PUNCH_HOLE |
| 2019 |
@param[in,out] file file to set */ |
| 2020 |
void fil_no_punch_hole(fil_node_t *file); |
| 2021 |
|
| 2022 |
#ifdef UNIV_ENABLE_UNIT_TEST_MAKE_FILEPATH |
| 2023 |
void test_make_filepath(); |
| 2024 |
#endif /* UNIV_ENABLE_UNIT_TEST_MAKE_FILEPATH */ |
| 2025 |
|
| 2026 |
/** @return the system tablespace instance */ |
| 2027 |
inline fil_space_t *fil_space_get_sys_space() { |
| 2028 |
return fil_space_t::s_sys_space; |
| 2029 |
} |
| 2030 |
|
| 2031 |
/** Redo a tablespace create. |
| 2032 |
@param[in] ptr redo log record |
| 2033 |
@param[in] end end of the redo log buffer |
| 2034 |
@param[in] page_id Tablespace Id and first page in file |
| 2035 |
@param[in] parsed_bytes Number of bytes parsed so far |
| 2036 |
@param[in] parse_only Don't apply, parse only |
| 2037 |
@return pointer to next redo log record |
| 2038 |
@retval nullptr if this log record was truncated */ |
| 2039 |
[[nodiscard]] byte *fil_tablespace_redo_create(byte *ptr, const byte *end, |
| 2040 |
const page_id_t &page_id, |
| 2041 |
ulint parsed_bytes, |
| 2042 |
bool parse_only); |
| 2043 |
|
| 2044 |
/** Redo a tablespace delete. |
| 2045 |
@param[in] ptr redo log record |
| 2046 |
@param[in] end end of the redo log buffer |
| 2047 |
@param[in] page_id Tablespace Id and first page in file |
| 2048 |
@param[in] parsed_bytes Number of bytes parsed so far |
| 2049 |
@param[in] parse_only Don't apply, parse only |
| 2050 |
@return pointer to next redo log record |
| 2051 |
@retval nullptr if this log record was truncated */ |
| 2052 |
[[nodiscard]] byte *fil_tablespace_redo_delete(byte *ptr, const byte *end, |
| 2053 |
const page_id_t &page_id, |
| 2054 |
ulint parsed_bytes, |
| 2055 |
bool parse_only); |
| 2056 |
|
| 2057 |
/** Redo a tablespace rename. |
| 2058 |
This function doesn't do anything, simply parses the redo log record. |
| 2059 |
@param[in] ptr redo log record |
| 2060 |
@param[in] end end of the redo log buffer |
| 2061 |
@param[in] page_id Tablespace Id and first page in file |
| 2062 |
@param[in] parsed_bytes Number of bytes parsed so far |
| 2063 |
@param[in] parse_only Don't apply, parse only |
| 2064 |
@return pointer to next redo log record |
| 2065 |
@retval nullptr if this log record was truncated */ |
| 2066 |
[[nodiscard]] byte *fil_tablespace_redo_rename(byte *ptr, const byte *end, |
| 2067 |
const page_id_t &page_id, |
| 2068 |
ulint parsed_bytes, |
| 2069 |
bool parse_only); |
| 2070 |
|
| 2071 |
/** Redo a tablespace extend |
| 2072 |
@param[in] ptr redo log record |
| 2073 |
@param[in] end end of the redo log buffer |
| 2074 |
@param[in] page_id Tablespace Id and first page in file |
| 2075 |
@param[in] parsed_bytes Number of bytes parsed so far |
| 2076 |
@param[in] parse_only Don't apply the log if true |
| 2077 |
@return pointer to next redo log record |
| 2078 |
@retval nullptr if this log record was truncated */ |
| 2079 |
[[nodiscard]] byte *fil_tablespace_redo_extend(byte *ptr, const byte *end, |
| 2080 |
const page_id_t &page_id, |
| 2081 |
ulint parsed_bytes, |
| 2082 |
bool parse_only); |
| 2083 |
|
| 2084 |
/** Parse and process an encryption redo record. |
| 2085 |
@param[in] ptr redo log record |
| 2086 |
@param[in] end end of the redo log buffer |
| 2087 |
@param[in] space_id the tablespace ID |
| 2088 |
@param[in] lsn lsn for REDO record |
| 2089 |
@return log record end, nullptr if not a complete record */ |
| 2090 |
[[nodiscard]] byte *fil_tablespace_redo_encryption(byte *ptr, const byte *end, |
| 2091 |
space_id_t space_id, |
| 2092 |
lsn_t lsn); |
| 2093 |
|
| 2094 |
/** Read the tablespace id to path mapping from the file |
| 2095 |
@param[in] recovery true if called from crash recovery */ |
| 2096 |
void fil_tablespace_open_init_for_recovery(bool recovery); |
| 2097 |
|
| 2098 |
/** Lookup the tablespace ID. |
| 2099 |
@param[in] space_id Tablespace ID to lookup |
| 2100 |
@return true if the space ID is known. */ |
| 2101 |
[[nodiscard]] bool fil_tablespace_lookup_for_recovery(space_id_t space_id); |
| 2102 |
|
| 2103 |
/** Compare and update space name and dd path for partitioned table. Uniformly |
| 2104 |
converts partition separators and names to lower case. |
| 2105 |
@param[in] space_id tablespace ID |
| 2106 |
@param[in] fsp_flags tablespace flags |
| 2107 |
@param[in] update_space update space name |
| 2108 |
@param[in,out] space_name tablespace name |
| 2109 |
@param[in,out] dd_path file name with complete path |
| 2110 |
@return true, if names are updated. */ |
| 2111 |
bool fil_update_partition_name(space_id_t space_id, uint32_t fsp_flags, |
| 2112 |
bool update_space, std::string &space_name, |
| 2113 |
std::string &dd_path); |
| 2114 |
|
| 2115 |
/** Add tablespace to the set of tablespaces to be updated in DD. |
| 2116 |
@param[in] dd_object_id Server DD tablespace ID |
| 2117 |
@param[in] space_id Innodb tablespace ID |
| 2118 |
@param[in] space_name New tablespace name |
| 2119 |
@param[in] old_path Old Path in the data dictionary |
| 2120 |
@param[in] new_path New path to be update in dictionary |
| 2121 |
@param[in] dd_flag_missing This tablespace is outside default data |
| 2122 |
directory, yet it is missing |
| 2123 |
DD_TABLE_DATA_DIRECTORY flag. This could |
| 2124 |
happen in versions earlier than |
| 2125 |
8.0.38/8.4.1/9.0.0 */ |
| 2126 |
void fil_add_moved_space(dd::Object_id dd_object_id, space_id_t space_id, |
| 2127 |
const char *space_name, const std::string &old_path, |
| 2128 |
const std::string &new_path, bool dd_flag_missing); |
| 2129 |
|
| 2130 |
/** Lookup the tablespace ID and return the path to the file. The filename |
| 2131 |
is ignored when testing for equality. Only the path up to the file name is |
| 2132 |
considered for matching: e.g. ./test/a.ibd == ./test/b.ibd. |
| 2133 |
@param[in] space_id tablespace ID to lookup |
| 2134 |
@param[in] space_name tablespace name |
| 2135 |
@param[in] fsp_flags tablespace flags |
| 2136 |
@param[in] old_path the path found in dd:Tablespace_files |
| 2137 |
@param[out] new_path the scanned path for this space_id |
| 2138 |
@return status of the match. */ |
| 2139 |
[[nodiscard]] Fil_state fil_tablespace_path_equals(space_id_t space_id, |
| 2140 |
const char *space_name, |
| 2141 |
ulint fsp_flags, |
| 2142 |
std::string old_path, |
| 2143 |
std::string *new_path); |
| 2144 |
|
| 2145 |
/** This function should be called after recovery has completed. |
| 2146 |
Check for tablespace files for which we did not see any MLOG_FILE_DELETE |
| 2147 |
or MLOG_FILE_RENAME record. These could not be recovered |
| 2148 |
@return true if there were some filenames missing for which we had to |
| 2149 |
ignore redo log records during the apply phase */ |
| 2150 |
[[nodiscard]] bool fil_check_missing_tablespaces(); |
| 2151 |
|
| 2152 |
/** Normalize and save a directory to scan for datafiles. |
| 2153 |
@param[in] directory directory to scan for ibd and ibu files |
| 2154 |
@param[in] is_undo_dir true for an undo directory */ |
| 2155 |
void fil_set_scan_dir(const std::string &directory, bool is_undo_dir = false); |
| 2156 |
|
| 2157 |
/** Normalize and save a list of directories to scan for datafiles. |
| 2158 |
@param[in] directories Directories to scan for ibd and ibu files |
| 2159 |
in the form: "dir1;dir2; ... dirN" */ |
| 2160 |
void fil_set_scan_dirs(const std::string &directories); |
| 2161 |
|
| 2162 |
/** Discover tablespaces by reading the header from .ibd files. |
| 2163 |
@return DB_SUCCESS if all goes well */ |
| 2164 |
dberr_t fil_scan_for_tablespaces(); |
| 2165 |
|
| 2166 |
/** Open the tablespace and also get the tablespace filenames, space_id must |
| 2167 |
already be known. |
| 2168 |
@param[in] space_id Tablespace ID to lookup |
| 2169 |
@return DB_SUCCESS if open was successful */ |
| 2170 |
[[nodiscard]] dberr_t fil_tablespace_open_for_recovery(space_id_t space_id); |
| 2171 |
|
| 2172 |
/** Replay a file rename operation for ddl replay. |
| 2173 |
@param[in] page_id Space ID and first page number in the file |
| 2174 |
@param[in] old_name old file name |
| 2175 |
@param[in] new_name new file name |
| 2176 |
@return whether the operation was successfully applied |
| 2177 |
(the name did not exist, or new_name did not exist and |
| 2178 |
name was successfully renamed to new_name) */ |
| 2179 |
bool fil_op_replay_rename_for_ddl(const page_id_t &page_id, |
| 2180 |
const char *old_name, const char *new_name); |
| 2181 |
|
| 2182 |
/** Free the Tablespace_files instance. |
| 2183 |
@param[in] read_only_mode true if InnoDB is started in read only mode. |
| 2184 |
@return DB_SUCCESS if all OK */ |
| 2185 |
[[nodiscard]] dberr_t fil_open_for_business(bool read_only_mode); |
| 2186 |
|
| 2187 |
/** Check if a path is known to InnoDB meaning that it is in or under |
| 2188 |
one of the four path settings scanned at startup for file discovery. |
| 2189 |
@param[in] path Path to check |
| 2190 |
@return true if path is known to InnoDB */ |
| 2191 |
[[nodiscard]] bool fil_path_is_known(const std::string &path); |
| 2192 |
|
| 2193 |
/** Get the list of directories that datafiles can reside in. |
| 2194 |
@return the list of directories 'dir1;dir2;....;dirN' */ |
| 2195 |
[[nodiscard]] std::string fil_get_dirs(); |
| 2196 |
|
| 2197 |
/** Rename a tablespace. Use the space_id to find the shard. |
| 2198 |
@param[in] space_id tablespace ID |
| 2199 |
@param[in] old_name old tablespace name |
| 2200 |
@param[in] new_name new tablespace name |
| 2201 |
@return DB_SUCCESS on success */ |
| 2202 |
[[nodiscard]] dberr_t fil_rename_tablespace_by_id(space_id_t space_id, |
| 2203 |
const char *old_name, |
| 2204 |
const char *new_name); |
| 2205 |
|
| 2206 |
/** Write initial pages for a new tablespace file created. |
| 2207 |
@param[in] file open file handle |
| 2208 |
@param[in] path path and filename of the datafile |
| 2209 |
@param[in] type file type |
| 2210 |
@param[in] size Initial size of the tablespace file in pages |
| 2211 |
@param[in] encrypt_info encryption key information |
| 2212 |
@param[in] space_id tablespace ID |
| 2213 |
@param[in,out] space_flags tablespace flags |
| 2214 |
@param[out] atomic_write if atomic write is used |
| 2215 |
@param[out] punch_hole if punch hole is used |
| 2216 |
@return DB_SUCCESS on success */ |
| 2217 |
[[nodiscard]] dberr_t fil_write_initial_pages( |
| 2218 |
pfs_os_file_t file, const char *path, fil_type_t type, page_no_t size, |
| 2219 |
const byte *encrypt_info, space_id_t space_id, uint32_t &space_flags, |
| 2220 |
bool &atomic_write, bool &punch_hole); |
| 2221 |
|
| 2222 |
/** Free the data structures required for recovery. */ |
| 2223 |
void fil_free_scanned_files(); |
| 2224 |
|
| 2225 |
/** Update the tablespace name. In case, the new name |
| 2226 |
and old name are same, no update done. |
| 2227 |
@param[in,out] space tablespace object on which name |
| 2228 |
will be updated |
| 2229 |
@param[in] name new name for tablespace */ |
| 2230 |
void fil_space_update_name(fil_space_t *space, const char *name); |
| 2231 |
|
| 2232 |
/** Adjust file name for import for partition files in different letter case. |
| 2233 |
@param[in] table Innodb dict table |
| 2234 |
@param[in] path file path to open |
| 2235 |
@param[in] extn file extension */ |
| 2236 |
void fil_adjust_name_import(dict_table_t *table, const char *path, |
| 2237 |
ib_file_suffix extn); |
| 2238 |
|
| 2239 |
#ifndef UNIV_HOTBACKUP |
| 2240 |
|
| 2241 |
/** Allows fil system to do periodical cleanup. */ |
| 2242 |
void fil_purge(); |
| 2243 |
|
| 2244 |
/** Count how many truncated undo space IDs are still tracked in |
| 2245 |
the buffer pool and the file_system cache. |
| 2246 |
@param[in] undo_num undo tablespace number. |
| 2247 |
@return number of undo tablespaces that are still in memory. */ |
| 2248 |
size_t fil_count_undo_deleted(space_id_t undo_num); |
| 2249 |
|
| 2250 |
#endif /* !UNIV_HOTBACKUP */ |
| 2251 |
|
| 2252 |
/** Get the page type as a string. |
| 2253 |
@param[in] type page type to be converted to string. |
| 2254 |
@return the page type as a string. */ |
| 2255 |
[[nodiscard]] const char *fil_get_page_type_str(page_type_t type) noexcept; |
| 2256 |
|
| 2257 |
/** Check if the given page type is valid. |
| 2258 |
@param[in] type the page type to be checked for validity. |
| 2259 |
@return true if it is valid page type, false otherwise. */ |
| 2260 |
[[nodiscard]] bool fil_is_page_type_valid(page_type_t type) noexcept; |
| 2261 |
#endif /* fil0fil_h */ |