| 1 | /***************************************************************************** |
| 2 | |
| 3 | Copyright (c) 1995, 2017, Oracle and/or its affiliates. All Rights Reserved. |
| 4 | Copyright (c) 2013, 2018, MariaDB Corporation. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify it under |
| 7 | the terms of the GNU General Public License as published by the Free Software |
| 8 | Foundation; version 2 of the License. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 12 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License along with |
| 15 | this program; if not, write to the Free Software Foundation, Inc., |
| 16 | 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA |
| 17 | |
| 18 | *****************************************************************************/ |
| 19 | |
| 20 | /**************************************************//** |
| 21 | @file include/fil0fil.h |
| 22 | The low-level file system |
| 23 | |
| 24 | Created 10/25/1995 Heikki Tuuri |
| 25 | *******************************************************/ |
| 26 | |
| 27 | #ifndef fil0fil_h |
| 28 | #define fil0fil_h |
| 29 | #include "univ.i" |
| 30 | |
| 31 | #ifndef UNIV_INNOCHECKSUM |
| 32 | |
| 33 | #include "log0recv.h" |
| 34 | #include "dict0types.h" |
| 35 | #include "page0size.h" |
| 36 | #include "ibuf0types.h" |
| 37 | |
| 38 | // Forward declaration |
| 39 | extern my_bool srv_use_doublewrite_buf; |
| 40 | extern struct buf_dblwr_t* buf_dblwr; |
| 41 | struct trx_t; |
| 42 | class page_id_t; |
| 43 | class truncate_t; |
| 44 | |
| 45 | /** Structure containing encryption specification */ |
| 46 | struct fil_space_crypt_t; |
| 47 | |
| 48 | /** File types */ |
| 49 | enum fil_type_t { |
| 50 | /** temporary tablespace (temporary undo log or tables) */ |
| 51 | FIL_TYPE_TEMPORARY, |
| 52 | /** a tablespace that is being imported (no logging until finished) */ |
| 53 | FIL_TYPE_IMPORT, |
| 54 | /** persistent tablespace (for system, undo log or tables) */ |
| 55 | FIL_TYPE_TABLESPACE, |
| 56 | /** redo log covering changes to files of FIL_TYPE_TABLESPACE */ |
| 57 | FIL_TYPE_LOG |
| 58 | }; |
| 59 | |
| 60 | /** Check if fil_type is any of FIL_TYPE_TEMPORARY, FIL_TYPE_IMPORT |
| 61 | or FIL_TYPE_TABLESPACE. |
| 62 | @param[in] type variable of type fil_type_t |
| 63 | @return true if any of FIL_TYPE_TEMPORARY, FIL_TYPE_IMPORT |
| 64 | or FIL_TYPE_TABLESPACE */ |
| 65 | inline |
| 66 | bool |
| 67 | fil_type_is_data( |
| 68 | fil_type_t type) |
| 69 | { |
| 70 | return(type == FIL_TYPE_TEMPORARY |
| 71 | || type == FIL_TYPE_IMPORT |
| 72 | || type == FIL_TYPE_TABLESPACE); |
| 73 | } |
| 74 | |
| 75 | struct fil_node_t; |
| 76 | |
| 77 | /** Tablespace or log data space */ |
| 78 | struct fil_space_t { |
| 79 | char* name; /*!< Tablespace name */ |
| 80 | ulint id; /*!< space id */ |
| 81 | lsn_t max_lsn; |
| 82 | /*!< LSN of the most recent |
| 83 | fil_names_write_if_was_clean(). |
| 84 | Reset to 0 by fil_names_clear(). |
| 85 | Protected by log_sys.mutex. |
| 86 | If and only if this is nonzero, the |
| 87 | tablespace will be in named_spaces. */ |
| 88 | bool stop_ios;/*!< true if we want to rename the |
| 89 | .ibd file of tablespace and want to |
| 90 | stop temporarily posting of new i/o |
| 91 | requests on the file */ |
| 92 | bool stop_new_ops; |
| 93 | /*!< we set this true when we start |
| 94 | deleting a single-table tablespace. |
| 95 | When this is set following new ops |
| 96 | are not allowed: |
| 97 | * read IO request |
| 98 | * ibuf merge |
| 99 | * file flush |
| 100 | Note that we can still possibly have |
| 101 | new write operations because we don't |
| 102 | check this flag when doing flush |
| 103 | batches. */ |
| 104 | bool is_being_truncated; |
| 105 | /*!< this is set to true when we prepare to |
| 106 | truncate a single-table tablespace and its |
| 107 | .ibd file */ |
| 108 | #ifdef UNIV_DEBUG |
| 109 | ulint redo_skipped_count; |
| 110 | /*!< reference count for operations who want |
| 111 | to skip redo log in the file space in order |
| 112 | to make fsp_space_modify_check pass. |
| 113 | Uses my_atomic_loadlint() and friends. */ |
| 114 | #endif |
| 115 | fil_type_t purpose;/*!< purpose */ |
| 116 | UT_LIST_BASE_NODE_T(fil_node_t) chain; |
| 117 | /*!< base node for the file chain */ |
| 118 | ulint size; /*!< tablespace file size in pages; |
| 119 | 0 if not known yet */ |
| 120 | ulint size_in_header; |
| 121 | /* FSP_SIZE in the tablespace header; |
| 122 | 0 if not known yet */ |
| 123 | ulint free_len; |
| 124 | /*!< length of the FSP_FREE list */ |
| 125 | ulint free_limit; |
| 126 | /*!< contents of FSP_FREE_LIMIT */ |
| 127 | ulint recv_size; |
| 128 | /*!< recovered tablespace size in pages; |
| 129 | 0 if no size change was read from the redo log, |
| 130 | or if the size change was implemented */ |
| 131 | ulint flags; /*!< FSP_SPACE_FLAGS and FSP_FLAGS_MEM_ flags; |
| 132 | see fsp0types.h, |
| 133 | fsp_flags_is_valid(), |
| 134 | page_size_t(ulint) (constructor) */ |
| 135 | ulint n_reserved_extents; |
| 136 | /*!< number of reserved free extents for |
| 137 | ongoing operations like B-tree page split */ |
| 138 | ulint n_pending_flushes; /*!< this is positive when flushing |
| 139 | the tablespace to disk; dropping of the |
| 140 | tablespace is forbidden if this is positive */ |
| 141 | /** Number of pending buffer pool operations accessing the tablespace |
| 142 | without holding a table lock or dict_operation_lock S-latch |
| 143 | that would prevent the table (and tablespace) from being |
| 144 | dropped. An example is change buffer merge. |
| 145 | The tablespace cannot be dropped while this is nonzero, |
| 146 | or while fil_node_t::n_pending is nonzero. |
| 147 | Protected by fil_system.mutex and my_atomic_loadlint() and friends. */ |
| 148 | ulint n_pending_ops; |
| 149 | /** Number of pending block read or write operations |
| 150 | (when a write is imminent or a read has recently completed). |
| 151 | The tablespace object cannot be freed while this is nonzero, |
| 152 | but it can be detached from fil_system. |
| 153 | Note that fil_node_t::n_pending tracks actual pending I/O requests. |
| 154 | Protected by fil_system.mutex and my_atomic_loadlint() and friends. */ |
| 155 | ulint n_pending_ios; |
| 156 | hash_node_t hash; /*!< hash chain node */ |
| 157 | hash_node_t name_hash;/*!< hash chain the name_hash table */ |
| 158 | rw_lock_t latch; /*!< latch protecting the file space storage |
| 159 | allocation */ |
| 160 | UT_LIST_NODE_T(fil_space_t) unflushed_spaces; |
| 161 | /*!< list of spaces with at least one unflushed |
| 162 | file we have written to */ |
| 163 | UT_LIST_NODE_T(fil_space_t) named_spaces; |
| 164 | /*!< list of spaces for which MLOG_FILE_NAME |
| 165 | records have been issued */ |
| 166 | bool is_in_unflushed_spaces; |
| 167 | /*!< true if this space is currently in |
| 168 | unflushed_spaces */ |
| 169 | UT_LIST_NODE_T(fil_space_t) space_list; |
| 170 | /*!< list of all spaces */ |
| 171 | /** other tablespaces needing key rotation */ |
| 172 | UT_LIST_NODE_T(fil_space_t) rotation_list; |
| 173 | /** whether this tablespace needs key rotation */ |
| 174 | bool is_in_rotation_list; |
| 175 | |
| 176 | /** MariaDB encryption data */ |
| 177 | fil_space_crypt_t* crypt_data; |
| 178 | |
| 179 | /** True if we have already printed compression failure */ |
| 180 | bool printed_compression_failure; |
| 181 | |
| 182 | /** True if the device this filespace is on supports atomic writes */ |
| 183 | bool atomic_write_supported; |
| 184 | |
| 185 | /** True if file system storing this tablespace supports |
| 186 | punch hole */ |
| 187 | bool punch_hole; |
| 188 | |
| 189 | ulint magic_n;/*!< FIL_SPACE_MAGIC_N */ |
| 190 | |
| 191 | /** @return whether the tablespace is about to be dropped or |
| 192 | truncated */ |
| 193 | bool is_stopping() const |
| 194 | { |
| 195 | return stop_new_ops || is_being_truncated; |
| 196 | } |
| 197 | |
| 198 | /** @return whether doublewrite buffering is needed */ |
| 199 | bool use_doublewrite() const |
| 200 | { |
| 201 | return !atomic_write_supported |
| 202 | && srv_use_doublewrite_buf && buf_dblwr; |
| 203 | } |
| 204 | |
| 205 | /** Try to reserve free extents. |
| 206 | @param[in] n_free_now current number of free extents |
| 207 | @param[in] n_to_reserve number of extents to reserve |
| 208 | @return whether the reservation succeeded */ |
| 209 | bool reserve_free_extents(ulint n_free_now, ulint n_to_reserve) |
| 210 | { |
| 211 | ut_ad(rw_lock_own(&latch, RW_LOCK_X)); |
| 212 | if (n_reserved_extents + n_to_reserve > n_free_now) { |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | n_reserved_extents += n_to_reserve; |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | /** Release the reserved free extents. |
| 221 | @param[in] n_reserved number of reserved extents */ |
| 222 | void release_free_extents(ulint n_reserved) |
| 223 | { |
| 224 | if (!n_reserved) return; |
| 225 | ut_ad(rw_lock_own(&latch, RW_LOCK_X)); |
| 226 | ut_a(n_reserved_extents >= n_reserved); |
| 227 | n_reserved_extents -= n_reserved; |
| 228 | } |
| 229 | |
| 230 | /** Rename a file. |
| 231 | @param[in] name table name after renaming |
| 232 | @param[in] path tablespace file name after renaming |
| 233 | @param[in] log whether to write redo log |
| 234 | @return error code |
| 235 | @retval DB_SUCCESS on success */ |
| 236 | dberr_t rename(const char* name, const char* path, bool log); |
| 237 | |
| 238 | /** Note that the tablespace has been imported. |
| 239 | Initially, purpose=FIL_TYPE_IMPORT so that no redo log is |
| 240 | written while the space ID is being updated in each page. */ |
| 241 | void set_imported(); |
| 242 | |
| 243 | /** Open each file. Only invoked on fil_system.temp_space. |
| 244 | @return whether all files were opened */ |
| 245 | bool open(); |
| 246 | /** Close each file. Only invoked on fil_system.temp_space. */ |
| 247 | void close(); |
| 248 | |
| 249 | /** Acquire a tablespace reference. */ |
| 250 | void acquire() { my_atomic_addlint(&n_pending_ops, 1); } |
| 251 | /** Release a tablespace reference. */ |
| 252 | void release() |
| 253 | { |
| 254 | ut_ad(referenced()); |
| 255 | my_atomic_addlint(&n_pending_ops, ulint(-1)); |
| 256 | } |
| 257 | /** @return whether references are being held */ |
| 258 | bool referenced() { return my_atomic_loadlint(&n_pending_ops); } |
| 259 | /** @return whether references are being held */ |
| 260 | bool referenced() const |
| 261 | { |
| 262 | return const_cast<fil_space_t*>(this)->referenced(); |
| 263 | } |
| 264 | |
| 265 | /** Acquire a tablespace reference for I/O. */ |
| 266 | void acquire_for_io() { my_atomic_addlint(&n_pending_ios, 1); } |
| 267 | /** Release a tablespace reference for I/O. */ |
| 268 | void release_for_io() |
| 269 | { |
| 270 | ut_ad(pending_io()); |
| 271 | my_atomic_addlint(&n_pending_ios, ulint(-1)); |
| 272 | } |
| 273 | /** @return whether I/O is pending */ |
| 274 | bool pending_io() { return my_atomic_loadlint(&n_pending_ios); } |
| 275 | /** @return whether I/O is pending */ |
| 276 | bool pending_io() const |
| 277 | { |
| 278 | return const_cast<fil_space_t*>(this)->pending_io(); |
| 279 | } |
| 280 | }; |
| 281 | |
| 282 | /** Value of fil_space_t::magic_n */ |
| 283 | #define FIL_SPACE_MAGIC_N 89472 |
| 284 | |
| 285 | /** File node of a tablespace or the log data space */ |
| 286 | struct fil_node_t { |
| 287 | /** tablespace containing this file */ |
| 288 | fil_space_t* space; |
| 289 | /** file name; protected by fil_system.mutex and log_sys.mutex. */ |
| 290 | char* name; |
| 291 | /** file handle (valid if is_open) */ |
| 292 | pfs_os_file_t handle; |
| 293 | /** event that groups and serializes calls to fsync; |
| 294 | os_event_set() and os_event_reset() are protected by |
| 295 | fil_system.mutex */ |
| 296 | os_event_t sync_event; |
| 297 | /** whether the file actually is a raw device or disk partition */ |
| 298 | bool is_raw_disk; |
| 299 | /** size of the file in database pages (0 if not known yet); |
| 300 | the possible last incomplete megabyte may be ignored |
| 301 | if space->id == 0 */ |
| 302 | ulint size; |
| 303 | /** initial size of the file in database pages; |
| 304 | FIL_IBD_FILE_INITIAL_SIZE by default */ |
| 305 | ulint init_size; |
| 306 | /** maximum size of the file in database pages (0 if unlimited) */ |
| 307 | ulint max_size; |
| 308 | /** count of pending i/o's; is_open must be true if nonzero */ |
| 309 | ulint n_pending; |
| 310 | /** count of pending flushes; is_open must be true if nonzero */ |
| 311 | ulint n_pending_flushes; |
| 312 | /** whether the file is currently being extended */ |
| 313 | bool being_extended; |
| 314 | /** number of writes to the file since the system was started */ |
| 315 | int64_t modification_counter; |
| 316 | /** the modification_counter of the latest flush to disk */ |
| 317 | int64_t flush_counter; |
| 318 | /** link to other files in this tablespace */ |
| 319 | UT_LIST_NODE_T(fil_node_t) chain; |
| 320 | /** link to the fil_system.LRU list (keeping track of open files) */ |
| 321 | UT_LIST_NODE_T(fil_node_t) LRU; |
| 322 | |
| 323 | /** whether this file could use atomic write (data file) */ |
| 324 | bool atomic_write; |
| 325 | |
| 326 | /** Filesystem block size */ |
| 327 | ulint block_size; |
| 328 | |
| 329 | /** FIL_NODE_MAGIC_N */ |
| 330 | ulint magic_n; |
| 331 | |
| 332 | /** @return whether this file is open */ |
| 333 | bool is_open() const |
| 334 | { |
| 335 | return(handle != OS_FILE_CLOSED); |
| 336 | } |
| 337 | }; |
| 338 | |
| 339 | /** Value of fil_node_t::magic_n */ |
| 340 | #define FIL_NODE_MAGIC_N 89389 |
| 341 | |
| 342 | /** Common InnoDB file extentions */ |
| 343 | enum ib_extention { |
| 344 | NO_EXT = 0, |
| 345 | IBD = 1, |
| 346 | ISL = 2, |
| 347 | CFG = 3 |
| 348 | }; |
| 349 | extern const char* dot_ext[]; |
| 350 | #define DOT_IBD dot_ext[IBD] |
| 351 | #define DOT_ISL dot_ext[ISL] |
| 352 | #define DOT_CFG dot_ext[CFG] |
| 353 | |
| 354 | /** When mysqld is run, the default directory "." is the mysqld datadir, |
| 355 | but in the MySQL Embedded Server Library and mysqlbackup it is not the default |
| 356 | directory, and we must set the base file path explicitly */ |
| 357 | extern const char* fil_path_to_mysql_datadir; |
| 358 | |
| 359 | /* Space address data type; this is intended to be used when |
| 360 | addresses accurate to a byte are stored in file pages. If the page part |
| 361 | of the address is FIL_NULL, the address is considered undefined. */ |
| 362 | |
| 363 | typedef byte fil_faddr_t; /*!< 'type' definition in C: an address |
| 364 | stored in a file page is a string of bytes */ |
| 365 | #endif /* !UNIV_INNOCHECKSUM */ |
| 366 | |
| 367 | /** Initial size of a single-table tablespace in pages */ |
| 368 | #define FIL_IBD_FILE_INITIAL_SIZE 4U |
| 369 | |
| 370 | /** 'null' (undefined) page offset in the context of file spaces */ |
| 371 | #define FIL_NULL ULINT32_UNDEFINED |
| 372 | |
| 373 | |
| 374 | #define FIL_ADDR_PAGE 0U /* first in address is the page offset */ |
| 375 | #define FIL_ADDR_BYTE 4U /* then comes 2-byte byte offset within page*/ |
| 376 | #define FIL_ADDR_SIZE 6U /* address size is 6 bytes */ |
| 377 | |
| 378 | #ifndef UNIV_INNOCHECKSUM |
| 379 | |
| 380 | /** File space address */ |
| 381 | struct fil_addr_t { |
| 382 | ulint page; /*!< page number within a space */ |
| 383 | ulint boffset; /*!< byte offset within the page */ |
| 384 | }; |
| 385 | |
| 386 | /** The null file address */ |
| 387 | extern const fil_addr_t fil_addr_null; |
| 388 | |
| 389 | #endif /* !UNIV_INNOCHECKSUM */ |
| 390 | |
| 391 | /** The byte offsets on a file page for various variables @{ */ |
| 392 | #define FIL_PAGE_SPACE_OR_CHKSUM 0 /*!< in < MySQL-4.0.14 space id the |
| 393 | page belongs to (== 0) but in later |
| 394 | versions the 'new' checksum of the |
| 395 | page */ |
| 396 | #define FIL_PAGE_OFFSET 4U /*!< page offset inside space */ |
| 397 | #define FIL_PAGE_PREV 8U /*!< if there is a 'natural' |
| 398 | predecessor of the page, its |
| 399 | offset. Otherwise FIL_NULL. |
| 400 | This field is not set on BLOB |
| 401 | pages, which are stored as a |
| 402 | singly-linked list. See also |
| 403 | FIL_PAGE_NEXT. */ |
| 404 | #define FIL_PAGE_NEXT 12U /*!< if there is a 'natural' successor |
| 405 | of the page, its offset. |
| 406 | Otherwise FIL_NULL. |
| 407 | B-tree index pages |
| 408 | (FIL_PAGE_TYPE contains FIL_PAGE_INDEX) |
| 409 | on the same PAGE_LEVEL are maintained |
| 410 | as a doubly linked list via |
| 411 | FIL_PAGE_PREV and FIL_PAGE_NEXT |
| 412 | in the collation order of the |
| 413 | smallest user record on each page. */ |
| 414 | #define FIL_PAGE_LSN 16U /*!< lsn of the end of the newest |
| 415 | modification log record to the page */ |
| 416 | #define FIL_PAGE_TYPE 24U /*!< file page type: FIL_PAGE_INDEX,..., |
| 417 | 2 bytes. |
| 418 | |
| 419 | The contents of this field can only |
| 420 | be trusted in the following case: |
| 421 | if the page is an uncompressed |
| 422 | B-tree index page, then it is |
| 423 | guaranteed that the value is |
| 424 | FIL_PAGE_INDEX. |
| 425 | The opposite does not hold. |
| 426 | |
| 427 | In tablespaces created by |
| 428 | MySQL/InnoDB 5.1.7 or later, the |
| 429 | contents of this field is valid |
| 430 | for all uncompressed pages. */ |
| 431 | #define FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION 26U /*!< for the first page |
| 432 | in a system tablespace data file |
| 433 | (ibdata*, not *.ibd): the file has |
| 434 | been flushed to disk at least up |
| 435 | to this lsn |
| 436 | for other pages: a 32-bit key version |
| 437 | used to encrypt the page + 32-bit checksum |
| 438 | or 64 bits of zero if no encryption |
| 439 | */ |
| 440 | |
| 441 | /** This overloads FIL_PAGE_FILE_FLUSH_LSN for RTREE Split Sequence Number */ |
| 442 | #define FIL_RTREE_SPLIT_SEQ_NUM FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION |
| 443 | |
| 444 | /** starting from 4.1.x this contains the space id of the page */ |
| 445 | #define FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID 34U |
| 446 | |
| 447 | #define FIL_PAGE_SPACE_ID FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID |
| 448 | |
| 449 | #define FIL_PAGE_DATA 38U /*!< start of the data on the page */ |
| 450 | |
| 451 | /* Following are used when page compression is used */ |
| 452 | #define FIL_PAGE_COMPRESSED_SIZE 2 /*!< Number of bytes used to store |
| 453 | actual payload data size on |
| 454 | compressed pages. */ |
| 455 | #define FIL_PAGE_COMPRESSION_METHOD_SIZE 2 |
| 456 | /*!< Number of bytes used to store |
| 457 | actual compression method. */ |
| 458 | /* @} */ |
| 459 | /** File page trailer @{ */ |
| 460 | #define FIL_PAGE_END_LSN_OLD_CHKSUM 8 /*!< the low 4 bytes of this are used |
| 461 | to store the page checksum, the |
| 462 | last 4 bytes should be identical |
| 463 | to the last 4 bytes of FIL_PAGE_LSN */ |
| 464 | #define FIL_PAGE_DATA_END 8 /*!< size of the page trailer */ |
| 465 | /* @} */ |
| 466 | |
| 467 | /** File page types (values of FIL_PAGE_TYPE) @{ */ |
| 468 | #define FIL_PAGE_PAGE_COMPRESSED_ENCRYPTED 37401 /*!< Page is compressed and |
| 469 | then encrypted */ |
| 470 | #define FIL_PAGE_PAGE_COMPRESSED 34354 /*!< page compressed page */ |
| 471 | #define FIL_PAGE_INDEX 17855 /*!< B-tree node */ |
| 472 | #define FIL_PAGE_RTREE 17854 /*!< R-tree node (SPATIAL INDEX) */ |
| 473 | #define FIL_PAGE_UNDO_LOG 2 /*!< Undo log page */ |
| 474 | #define FIL_PAGE_INODE 3 /*!< Index node */ |
| 475 | #define FIL_PAGE_IBUF_FREE_LIST 4 /*!< Insert buffer free list */ |
| 476 | /* File page types introduced in MySQL/InnoDB 5.1.7 */ |
| 477 | #define FIL_PAGE_TYPE_ALLOCATED 0 /*!< Freshly allocated page */ |
| 478 | #define FIL_PAGE_IBUF_BITMAP 5 /*!< Insert buffer bitmap */ |
| 479 | #define FIL_PAGE_TYPE_SYS 6 /*!< System page */ |
| 480 | #define FIL_PAGE_TYPE_TRX_SYS 7 /*!< Transaction system data */ |
| 481 | #define FIL_PAGE_TYPE_FSP_HDR 8 /*!< File space header */ |
| 482 | #define FIL_PAGE_TYPE_XDES 9 /*!< Extent descriptor page */ |
| 483 | #define FIL_PAGE_TYPE_BLOB 10 /*!< Uncompressed BLOB page */ |
| 484 | #define FIL_PAGE_TYPE_ZBLOB 11 /*!< First compressed BLOB page */ |
| 485 | #define FIL_PAGE_TYPE_ZBLOB2 12 /*!< Subsequent compressed BLOB page */ |
| 486 | #define FIL_PAGE_TYPE_UNKNOWN 13 /*!< In old tablespaces, garbage |
| 487 | in FIL_PAGE_TYPE is replaced with this |
| 488 | value when flushing pages. */ |
| 489 | |
| 490 | /* File page types introduced in MySQL 5.7, not supported in MariaDB */ |
| 491 | //#define FIL_PAGE_COMPRESSED 14 |
| 492 | //#define FIL_PAGE_ENCRYPTED 15 |
| 493 | //#define FIL_PAGE_COMPRESSED_AND_ENCRYPTED 16 |
| 494 | //#define FIL_PAGE_ENCRYPTED_RTREE 17 |
| 495 | /** Clustered index root page after instant ADD COLUMN */ |
| 496 | #define FIL_PAGE_TYPE_INSTANT 18 |
| 497 | |
| 498 | /** Used by i_s.cc to index into the text description. |
| 499 | Note: FIL_PAGE_TYPE_INSTANT maps to the same as FIL_PAGE_INDEX. */ |
| 500 | #define FIL_PAGE_TYPE_LAST FIL_PAGE_TYPE_UNKNOWN |
| 501 | /*!< Last page type */ |
| 502 | /* @} */ |
| 503 | |
| 504 | /** @return whether the page type is B-tree or R-tree index */ |
| 505 | inline bool fil_page_type_is_index(ulint page_type) |
| 506 | { |
| 507 | switch (page_type) { |
| 508 | case FIL_PAGE_TYPE_INSTANT: |
| 509 | case FIL_PAGE_INDEX: |
| 510 | case FIL_PAGE_RTREE: |
| 511 | return(true); |
| 512 | } |
| 513 | return(false); |
| 514 | } |
| 515 | |
| 516 | /** Check whether the page is index page (either regular Btree index or Rtree |
| 517 | index */ |
| 518 | #define fil_page_index_page_check(page) \ |
| 519 | fil_page_type_is_index(fil_page_get_type(page)) |
| 520 | |
| 521 | /** Enum values for encryption table option */ |
| 522 | enum fil_encryption_t { |
| 523 | /** Encrypted if innodb_encrypt_tables=ON (srv_encrypt_tables) */ |
| 524 | FIL_ENCRYPTION_DEFAULT, |
| 525 | /** Encrypted */ |
| 526 | FIL_ENCRYPTION_ON, |
| 527 | /** Not encrypted */ |
| 528 | FIL_ENCRYPTION_OFF |
| 529 | }; |
| 530 | |
| 531 | /** The number of fsyncs done to the log */ |
| 532 | extern ulint fil_n_log_flushes; |
| 533 | |
| 534 | /** Number of pending redo log flushes */ |
| 535 | extern ulint fil_n_pending_log_flushes; |
| 536 | /** Number of pending tablespace flushes */ |
| 537 | extern ulint fil_n_pending_tablespace_flushes; |
| 538 | |
| 539 | /** Number of files currently open */ |
| 540 | extern ulint fil_n_file_opened; |
| 541 | |
| 542 | #ifndef UNIV_INNOCHECKSUM |
| 543 | |
| 544 | /** Look up a tablespace. |
| 545 | The caller should hold an InnoDB table lock or a MDL that prevents |
| 546 | the tablespace from being dropped during the operation, |
| 547 | or the caller should be in single-threaded crash recovery mode |
| 548 | (no user connections that could drop tablespaces). |
| 549 | If this is not the case, fil_space_acquire() and fil_space_t::release() |
| 550 | should be used instead. |
| 551 | @param[in] id tablespace ID |
| 552 | @return tablespace, or NULL if not found */ |
| 553 | fil_space_t* |
| 554 | fil_space_get( |
| 555 | ulint id) |
| 556 | MY_ATTRIBUTE((warn_unused_result)); |
| 557 | |
| 558 | /** The tablespace memory cache; also the totality of logs (the log |
| 559 | data space) is stored here; below we talk about tablespaces, but also |
| 560 | the ib_logfiles form a 'space' and it is handled here */ |
| 561 | struct fil_system_t { |
| 562 | /** |
| 563 | Constructor. |
| 564 | |
| 565 | Some members may require late initialisation, thus we just mark object as |
| 566 | uninitialised. Real initialisation happens in create(). |
| 567 | */ |
| 568 | fil_system_t(): m_initialised(false) |
| 569 | { |
| 570 | UT_LIST_INIT(LRU, &fil_node_t::LRU); |
| 571 | UT_LIST_INIT(space_list, &fil_space_t::space_list); |
| 572 | UT_LIST_INIT(rotation_list, &fil_space_t::rotation_list); |
| 573 | UT_LIST_INIT(unflushed_spaces, &fil_space_t::unflushed_spaces); |
| 574 | UT_LIST_INIT(named_spaces, &fil_space_t::named_spaces); |
| 575 | } |
| 576 | |
| 577 | bool is_initialised() const { return m_initialised; } |
| 578 | |
| 579 | /** |
| 580 | Create the file system interface at database start. |
| 581 | |
| 582 | @param[in] hash_size hash table size |
| 583 | */ |
| 584 | void create(ulint hash_size); |
| 585 | |
| 586 | /** Close the file system interface at shutdown */ |
| 587 | void close(); |
| 588 | |
| 589 | private: |
| 590 | bool m_initialised; |
| 591 | public: |
| 592 | ib_mutex_t mutex; /*!< The mutex protecting the cache */ |
| 593 | fil_space_t* sys_space; /*!< The innodb_system tablespace */ |
| 594 | fil_space_t* temp_space; /*!< The innodb_temporary tablespace */ |
| 595 | hash_table_t* spaces; /*!< The hash table of spaces in the |
| 596 | system; they are hashed on the space |
| 597 | id */ |
| 598 | UT_LIST_BASE_NODE_T(fil_node_t) LRU; |
| 599 | /*!< base node for the LRU list of the |
| 600 | most recently used open files with no |
| 601 | pending i/o's; if we start an i/o on |
| 602 | the file, we first remove it from this |
| 603 | list, and return it to the start of |
| 604 | the list when the i/o ends; |
| 605 | log files and the system tablespace are |
| 606 | not put to this list: they are opened |
| 607 | after the startup, and kept open until |
| 608 | shutdown */ |
| 609 | UT_LIST_BASE_NODE_T(fil_space_t) unflushed_spaces; |
| 610 | /*!< base node for the list of those |
| 611 | tablespaces whose files contain |
| 612 | unflushed writes; those spaces have |
| 613 | at least one file node where |
| 614 | modification_counter > flush_counter */ |
| 615 | ulint n_open; /*!< number of files currently open */ |
| 616 | int64_t modification_counter;/*!< when we write to a file we |
| 617 | increment this by one */ |
| 618 | ulint max_assigned_id;/*!< maximum space id in the existing |
| 619 | tables, or assigned during the time |
| 620 | mysqld has been up; at an InnoDB |
| 621 | startup we scan the data dictionary |
| 622 | and set here the maximum of the |
| 623 | space id's of the tables there */ |
| 624 | UT_LIST_BASE_NODE_T(fil_space_t) space_list; |
| 625 | /*!< list of all file spaces */ |
| 626 | UT_LIST_BASE_NODE_T(fil_space_t) named_spaces; |
| 627 | /*!< list of all file spaces |
| 628 | for which a MLOG_FILE_NAME |
| 629 | record has been written since |
| 630 | the latest redo log checkpoint. |
| 631 | Protected only by log_sys.mutex. */ |
| 632 | UT_LIST_BASE_NODE_T(fil_space_t) rotation_list; |
| 633 | /*!< list of all file spaces needing |
| 634 | key rotation.*/ |
| 635 | |
| 636 | bool space_id_reuse_warned; |
| 637 | /*!< whether fil_space_create() |
| 638 | has issued a warning about |
| 639 | potential space_id reuse */ |
| 640 | }; |
| 641 | |
| 642 | /** The tablespace memory cache. */ |
| 643 | extern fil_system_t fil_system; |
| 644 | |
| 645 | #include "fil0crypt.h" |
| 646 | |
| 647 | /** Returns the latch of a file space. |
| 648 | @param[in] id space id |
| 649 | @param[out] flags tablespace flags |
| 650 | @return latch protecting storage allocation */ |
| 651 | rw_lock_t* |
| 652 | fil_space_get_latch( |
| 653 | ulint id, |
| 654 | ulint* flags); |
| 655 | |
| 656 | /** Append a file to the chain of files of a space. |
| 657 | @param[in] name file name of a file that is not open |
| 658 | @param[in] size file size in entire database blocks |
| 659 | @param[in,out] space tablespace from fil_space_create() |
| 660 | @param[in] is_raw whether this is a raw device or partition |
| 661 | @param[in] atomic_write true if atomic write could be enabled |
| 662 | @param[in] max_pages maximum number of pages in file, |
| 663 | ULINT_MAX means the file size is unlimited. |
| 664 | @return pointer to the file name |
| 665 | @retval NULL if error */ |
| 666 | char* |
| 667 | fil_node_create( |
| 668 | const char* name, |
| 669 | ulint size, |
| 670 | fil_space_t* space, |
| 671 | bool is_raw, |
| 672 | bool atomic_write, |
| 673 | ulint max_pages = ULINT_MAX) |
| 674 | MY_ATTRIBUTE((warn_unused_result)); |
| 675 | |
| 676 | /** Create a space memory object and put it to the fil_system hash table. |
| 677 | Error messages are issued to the server log. |
| 678 | @param[in] name tablespace name |
| 679 | @param[in] id tablespace identifier |
| 680 | @param[in] flags tablespace flags |
| 681 | @param[in] purpose tablespace purpose |
| 682 | @param[in,out] crypt_data encryption information |
| 683 | @param[in] mode encryption mode |
| 684 | @return pointer to created tablespace, to be filled in with fil_node_create() |
| 685 | @retval NULL on failure (such as when the same tablespace exists) */ |
| 686 | fil_space_t* |
| 687 | fil_space_create( |
| 688 | const char* name, |
| 689 | ulint id, |
| 690 | ulint flags, |
| 691 | fil_type_t purpose, |
| 692 | fil_space_crypt_t* crypt_data, |
| 693 | fil_encryption_t mode = FIL_ENCRYPTION_DEFAULT) |
| 694 | MY_ATTRIBUTE((warn_unused_result)); |
| 695 | |
| 696 | /*******************************************************************//** |
| 697 | Assigns a new space id for a new single-table tablespace. This works simply by |
| 698 | incrementing the global counter. If 4 billion id's is not enough, we may need |
| 699 | to recycle id's. |
| 700 | @return true if assigned, false if not */ |
| 701 | bool |
| 702 | fil_assign_new_space_id( |
| 703 | /*====================*/ |
| 704 | ulint* space_id); /*!< in/out: space id */ |
| 705 | |
| 706 | /** Frees a space object from the tablespace memory cache. |
| 707 | Closes the files in the chain but does not delete them. |
| 708 | There must not be any pending i/o's or flushes on the files. |
| 709 | @param[in] id tablespace identifier |
| 710 | @param[in] x_latched whether the caller holds X-mode space->latch |
| 711 | @return true if success */ |
| 712 | bool |
| 713 | fil_space_free( |
| 714 | ulint id, |
| 715 | bool x_latched); |
| 716 | |
| 717 | /** Set the recovered size of a tablespace in pages. |
| 718 | @param id tablespace ID |
| 719 | @param size recovered size in pages */ |
| 720 | UNIV_INTERN |
| 721 | void |
| 722 | fil_space_set_recv_size(ulint id, ulint size); |
| 723 | /*******************************************************************//** |
| 724 | Returns the size of the space in pages. The tablespace must be cached in the |
| 725 | memory cache. |
| 726 | @return space size, 0 if space not found */ |
| 727 | ulint |
| 728 | fil_space_get_size( |
| 729 | /*===============*/ |
| 730 | ulint id); /*!< in: space id */ |
| 731 | /*******************************************************************//** |
| 732 | Returns the flags of the space. The tablespace must be cached |
| 733 | in the memory cache. |
| 734 | @return flags, ULINT_UNDEFINED if space not found */ |
| 735 | ulint |
| 736 | fil_space_get_flags( |
| 737 | /*================*/ |
| 738 | ulint id); /*!< in: space id */ |
| 739 | |
| 740 | /** Returns the page size of the space and whether it is compressed or not. |
| 741 | The tablespace must be cached in the memory cache. |
| 742 | @param[in] id space id |
| 743 | @param[out] found true if tablespace was found |
| 744 | @return page size */ |
| 745 | const page_size_t |
| 746 | fil_space_get_page_size( |
| 747 | ulint id, |
| 748 | bool* found); |
| 749 | |
| 750 | /*******************************************************************//** |
| 751 | Opens all log files and system tablespace data files. They stay open until the |
| 752 | database server shutdown. This should be called at a server startup after the |
| 753 | space objects for the log and the system tablespace have been created. The |
| 754 | purpose of this operation is to make sure we never run out of file descriptors |
| 755 | if we need to read from the insert buffer or to write to the log. */ |
| 756 | void |
| 757 | fil_open_log_and_system_tablespace_files(void); |
| 758 | /*==========================================*/ |
| 759 | /*******************************************************************//** |
| 760 | Closes all open files. There must not be any pending i/o's or not flushed |
| 761 | modifications in the files. */ |
| 762 | void |
| 763 | fil_close_all_files(void); |
| 764 | /*=====================*/ |
| 765 | /*******************************************************************//** |
| 766 | Closes the redo log files. There must not be any pending i/o's or not |
| 767 | flushed modifications in the files. */ |
| 768 | void |
| 769 | fil_close_log_files( |
| 770 | /*================*/ |
| 771 | bool free); /*!< in: whether to free the memory object */ |
| 772 | /*******************************************************************//** |
| 773 | Sets the max tablespace id counter if the given number is bigger than the |
| 774 | previous value. */ |
| 775 | void |
| 776 | fil_set_max_space_id_if_bigger( |
| 777 | /*===========================*/ |
| 778 | ulint max_id);/*!< in: maximum known id */ |
| 779 | |
| 780 | /** Write the flushed LSN to the page header of the first page in the |
| 781 | system tablespace. |
| 782 | @param[in] lsn flushed LSN |
| 783 | @return DB_SUCCESS or error number */ |
| 784 | dberr_t |
| 785 | fil_write_flushed_lsn( |
| 786 | lsn_t lsn) |
| 787 | MY_ATTRIBUTE((warn_unused_result)); |
| 788 | |
| 789 | /** Acquire a tablespace when it could be dropped concurrently. |
| 790 | Used by background threads that do not necessarily hold proper locks |
| 791 | for concurrency control. |
| 792 | @param[in] id tablespace ID |
| 793 | @param[in] silent whether to silently ignore missing tablespaces |
| 794 | @return the tablespace |
| 795 | @retval NULL if missing or being deleted or truncated */ |
| 796 | UNIV_INTERN |
| 797 | fil_space_t* |
| 798 | fil_space_acquire_low(ulint id, bool silent) |
| 799 | MY_ATTRIBUTE((warn_unused_result)); |
| 800 | |
| 801 | /** Acquire a tablespace when it could be dropped concurrently. |
| 802 | Used by background threads that do not necessarily hold proper locks |
| 803 | for concurrency control. |
| 804 | @param[in] id tablespace ID |
| 805 | @return the tablespace |
| 806 | @retval NULL if missing or being deleted or truncated */ |
| 807 | inline |
| 808 | fil_space_t* |
| 809 | fil_space_acquire(ulint id) |
| 810 | { |
| 811 | return (fil_space_acquire_low(id, false)); |
| 812 | } |
| 813 | |
| 814 | /** Acquire a tablespace that may not exist. |
| 815 | Used by background threads that do not necessarily hold proper locks |
| 816 | for concurrency control. |
| 817 | @param[in] id tablespace ID |
| 818 | @return the tablespace |
| 819 | @retval NULL if missing or being deleted */ |
| 820 | inline |
| 821 | fil_space_t* |
| 822 | fil_space_acquire_silent(ulint id) |
| 823 | { |
| 824 | return (fil_space_acquire_low(id, true)); |
| 825 | } |
| 826 | |
| 827 | /** Acquire a tablespace for reading or writing a block, |
| 828 | when it could be dropped concurrently. |
| 829 | @param[in] id tablespace ID |
| 830 | @return the tablespace |
| 831 | @retval NULL if missing */ |
| 832 | fil_space_t* |
| 833 | fil_space_acquire_for_io(ulint id); |
| 834 | |
| 835 | /** Return the next fil_space_t. |
| 836 | Once started, the caller must keep calling this until it returns NULL. |
| 837 | fil_space_acquire() and fil_space_t::release() are invoked here which |
| 838 | blocks a concurrent operation from dropping the tablespace. |
| 839 | @param[in,out] prev_space Pointer to the previous fil_space_t. |
| 840 | If NULL, use the first fil_space_t on fil_system.space_list. |
| 841 | @return pointer to the next fil_space_t. |
| 842 | @retval NULL if this was the last */ |
| 843 | fil_space_t* |
| 844 | fil_space_next( |
| 845 | fil_space_t* prev_space) |
| 846 | MY_ATTRIBUTE((warn_unused_result)); |
| 847 | |
| 848 | /** Return the next fil_space_t from key rotation list. |
| 849 | Once started, the caller must keep calling this until it returns NULL. |
| 850 | fil_space_acquire() and fil_space_t::release() are invoked here which |
| 851 | blocks a concurrent operation from dropping the tablespace. |
| 852 | @param[in,out] prev_space Pointer to the previous fil_space_t. |
| 853 | If NULL, use the first fil_space_t on fil_system.space_list. |
| 854 | @return pointer to the next fil_space_t. |
| 855 | @retval NULL if this was the last*/ |
| 856 | fil_space_t* |
| 857 | fil_space_keyrotate_next( |
| 858 | fil_space_t* prev_space) |
| 859 | MY_ATTRIBUTE((warn_unused_result)); |
| 860 | |
| 861 | /********************************************************//** |
| 862 | Creates the database directory for a table if it does not exist yet. */ |
| 863 | void |
| 864 | fil_create_directory_for_tablename( |
| 865 | /*===============================*/ |
| 866 | const char* name); /*!< in: name in the standard |
| 867 | 'databasename/tablename' format */ |
| 868 | /** Replay a file rename operation if possible. |
| 869 | @param[in] space_id tablespace identifier |
| 870 | @param[in] first_page_no first page number in the file |
| 871 | @param[in] name old file name |
| 872 | @param[in] new_name new file name |
| 873 | @return whether the operation was successfully applied |
| 874 | (the name did not exist, or new_name did not exist and |
| 875 | name was successfully renamed to new_name) */ |
| 876 | bool |
| 877 | fil_op_replay_rename( |
| 878 | ulint space_id, |
| 879 | ulint first_page_no, |
| 880 | const char* name, |
| 881 | const char* new_name) |
| 882 | MY_ATTRIBUTE((warn_unused_result)); |
| 883 | |
| 884 | /** Determine whether a table can be accessed in operations that are |
| 885 | not (necessarily) protected by meta-data locks. |
| 886 | (Rollback would generally be protected, but rollback of |
| 887 | FOREIGN KEY CASCADE/SET NULL is not protected by meta-data locks |
| 888 | but only by InnoDB table locks, which may be broken by TRUNCATE TABLE.) |
| 889 | @param[in] table persistent table |
| 890 | checked @return whether the table is accessible */ |
| 891 | bool |
| 892 | fil_table_accessible(const dict_table_t* table) |
| 893 | MY_ATTRIBUTE((warn_unused_result, nonnull)); |
| 894 | |
| 895 | /** Delete a tablespace and associated .ibd file. |
| 896 | @param[in] id tablespace identifier |
| 897 | @return DB_SUCCESS or error */ |
| 898 | dberr_t |
| 899 | fil_delete_tablespace( |
| 900 | ulint id |
| 901 | #ifdef BTR_CUR_HASH_ADAPT |
| 902 | , bool drop_ahi = false /*!< whether to drop the adaptive hash index */ |
| 903 | #endif /* BTR_CUR_HASH_ADAPT */ |
| 904 | ); |
| 905 | |
| 906 | /** Truncate the tablespace to needed size. |
| 907 | @param[in,out] space tablespace truncate |
| 908 | @param[in] size_in_pages truncate size. |
| 909 | @return true if truncate was successful. */ |
| 910 | bool fil_truncate_tablespace(fil_space_t* space, ulint size_in_pages); |
| 911 | |
| 912 | /*******************************************************************//** |
| 913 | Prepare for truncating a single-table tablespace. The tablespace |
| 914 | must be cached in the memory cache. |
| 915 | 1) Check pending operations on a tablespace; |
| 916 | 2) Remove all insert buffer entries for the tablespace; |
| 917 | @return DB_SUCCESS or error */ |
| 918 | dberr_t |
| 919 | fil_prepare_for_truncate( |
| 920 | /*=====================*/ |
| 921 | ulint id); /*!< in: space id */ |
| 922 | |
| 923 | /*******************************************************************//** |
| 924 | Closes a single-table tablespace. The tablespace must be cached in the |
| 925 | memory cache. Free all pages used by the tablespace. |
| 926 | @return DB_SUCCESS or error */ |
| 927 | dberr_t |
| 928 | fil_close_tablespace( |
| 929 | /*=================*/ |
| 930 | trx_t* trx, /*!< in/out: Transaction covering the close */ |
| 931 | ulint id); /*!< in: space id */ |
| 932 | |
| 933 | /*******************************************************************//** |
| 934 | Allocates and builds a file name from a path, a table or tablespace name |
| 935 | and a suffix. The string must be freed by caller with ut_free(). |
| 936 | @param[in] path NULL or the direcory path or the full path and filename. |
| 937 | @param[in] name NULL if path is full, or Table/Tablespace name |
| 938 | @param[in] suffix NULL or the file extention to use. |
| 939 | @return own: file name */ |
| 940 | char* |
| 941 | fil_make_filepath( |
| 942 | const char* path, |
| 943 | const char* name, |
| 944 | ib_extention suffix, |
| 945 | bool strip_name); |
| 946 | |
| 947 | /** Create a tablespace file. |
| 948 | @param[in] space_id Tablespace ID |
| 949 | @param[in] name Tablespace name in dbname/tablename format. |
| 950 | @param[in] path Path and filename of the datafile to create. |
| 951 | @param[in] flags Tablespace flags |
| 952 | @param[in] size Initial size of the tablespace file in pages, |
| 953 | must be >= FIL_IBD_FILE_INITIAL_SIZE |
| 954 | @param[in] mode MariaDB encryption mode |
| 955 | @param[in] key_id MariaDB encryption key_id |
| 956 | @param[out] err DB_SUCCESS or error code |
| 957 | @return the created tablespace |
| 958 | @retval NULL on error */ |
| 959 | fil_space_t* |
| 960 | fil_ibd_create( |
| 961 | ulint space_id, |
| 962 | const char* name, |
| 963 | const char* path, |
| 964 | ulint flags, |
| 965 | ulint size, |
| 966 | fil_encryption_t mode, |
| 967 | uint32_t key_id, |
| 968 | dberr_t* err) |
| 969 | MY_ATTRIBUTE((nonnull(2,8), warn_unused_result)); |
| 970 | |
| 971 | /** Try to adjust FSP_SPACE_FLAGS if they differ from the expectations. |
| 972 | (Typically when upgrading from MariaDB 10.1.0..10.1.20.) |
| 973 | @param[in,out] space tablespace |
| 974 | @param[in] flags desired tablespace flags */ |
| 975 | void fsp_flags_try_adjust(fil_space_t* space, ulint flags); |
| 976 | |
| 977 | /********************************************************************//** |
| 978 | Tries to open a single-table tablespace and optionally checks the space id is |
| 979 | right in it. If does not succeed, prints an error message to the .err log. This |
| 980 | function is used to open a tablespace when we start up mysqld, and also in |
| 981 | IMPORT TABLESPACE. |
| 982 | NOTE that we assume this operation is used either at the database startup |
| 983 | or under the protection of the dictionary mutex, so that two users cannot |
| 984 | race here. This operation does not leave the file associated with the |
| 985 | tablespace open, but closes it after we have looked at the space id in it. |
| 986 | |
| 987 | If the validate boolean is set, we read the first page of the file and |
| 988 | check that the space id in the file is what we expect. We assume that |
| 989 | this function runs much faster if no check is made, since accessing the |
| 990 | file inode probably is much faster (the OS caches them) than accessing |
| 991 | the first page of the file. This boolean may be initially false, but if |
| 992 | a remote tablespace is found it will be changed to true. |
| 993 | |
| 994 | If the fix_dict boolean is set, then it is safe to use an internal SQL |
| 995 | statement to update the dictionary tables if they are incorrect. |
| 996 | |
| 997 | @param[in] validate true if we should validate the tablespace |
| 998 | @param[in] fix_dict true if the dictionary is available to be fixed |
| 999 | @param[in] purpose FIL_TYPE_TABLESPACE or FIL_TYPE_TEMPORARY |
| 1000 | @param[in] id tablespace ID |
| 1001 | @param[in] flags expected FSP_SPACE_FLAGS |
| 1002 | @param[in] tablename table name |
| 1003 | If file-per-table, it is the table name in the databasename/tablename format |
| 1004 | @param[in] path_in expected filepath, usually read from dictionary |
| 1005 | @param[out] err DB_SUCCESS or error code |
| 1006 | @return tablespace |
| 1007 | @retval NULL if the tablespace could not be opened */ |
| 1008 | fil_space_t* |
| 1009 | fil_ibd_open( |
| 1010 | bool validate, |
| 1011 | bool fix_dict, |
| 1012 | fil_type_t purpose, |
| 1013 | ulint id, |
| 1014 | ulint flags, |
| 1015 | const table_name_t& tablename, |
| 1016 | const char* path_in, |
| 1017 | dberr_t* err = NULL) |
| 1018 | MY_ATTRIBUTE((warn_unused_result)); |
| 1019 | |
| 1020 | enum fil_load_status { |
| 1021 | /** The tablespace file(s) were found and valid. */ |
| 1022 | FIL_LOAD_OK, |
| 1023 | /** The name no longer matches space_id */ |
| 1024 | FIL_LOAD_ID_CHANGED, |
| 1025 | /** The file(s) were not found */ |
| 1026 | FIL_LOAD_NOT_FOUND, |
| 1027 | /** The file(s) were not valid */ |
| 1028 | FIL_LOAD_INVALID |
| 1029 | }; |
| 1030 | |
| 1031 | /** Open a single-file tablespace and add it to the InnoDB data structures. |
| 1032 | @param[in] space_id tablespace ID |
| 1033 | @param[in] filename path/to/databasename/tablename.ibd |
| 1034 | @param[out] space the tablespace, or NULL on error |
| 1035 | @return status of the operation */ |
| 1036 | enum fil_load_status |
| 1037 | fil_ibd_load( |
| 1038 | ulint space_id, |
| 1039 | const char* filename, |
| 1040 | fil_space_t*& space) |
| 1041 | MY_ATTRIBUTE((warn_unused_result)); |
| 1042 | |
| 1043 | |
| 1044 | /***********************************************************************//** |
| 1045 | A fault-tolerant function that tries to read the next file name in the |
| 1046 | directory. We retry 100 times if os_file_readdir_next_file() returns -1. The |
| 1047 | idea is to read as much good data as we can and jump over bad data. |
| 1048 | @return 0 if ok, -1 if error even after the retries, 1 if at the end |
| 1049 | of the directory */ |
| 1050 | int |
| 1051 | fil_file_readdir_next_file( |
| 1052 | /*=======================*/ |
| 1053 | dberr_t* err, /*!< out: this is set to DB_ERROR if an error |
| 1054 | was encountered, otherwise not changed */ |
| 1055 | const char* dirname,/*!< in: directory name or path */ |
| 1056 | os_file_dir_t dir, /*!< in: directory stream */ |
| 1057 | os_file_stat_t* info); /*!< in/out: buffer where the |
| 1058 | info is returned */ |
| 1059 | /** Determine if a matching tablespace exists in the InnoDB tablespace |
| 1060 | memory cache. Note that if we have not done a crash recovery at the database |
| 1061 | startup, there may be many tablespaces which are not yet in the memory cache. |
| 1062 | @param[in] id Tablespace ID |
| 1063 | @param[in] name Tablespace name used in fil_space_create(). |
| 1064 | @param[in] print_error_if_does_not_exist |
| 1065 | Print detailed error information to the |
| 1066 | error log if a matching tablespace is not found from memory. |
| 1067 | @param[in] table_flags table flags |
| 1068 | @return the tablespace |
| 1069 | @retval NULL if no matching tablespace exists in the memory cache */ |
| 1070 | fil_space_t* |
| 1071 | fil_space_for_table_exists_in_mem( |
| 1072 | ulint id, |
| 1073 | const char* name, |
| 1074 | bool print_error_if_does_not_exist, |
| 1075 | ulint table_flags); |
| 1076 | |
| 1077 | /** Try to extend a tablespace if it is smaller than the specified size. |
| 1078 | @param[in,out] space tablespace |
| 1079 | @param[in] size desired size in pages |
| 1080 | @return whether the tablespace is at least as big as requested */ |
| 1081 | bool |
| 1082 | fil_space_extend( |
| 1083 | fil_space_t* space, |
| 1084 | ulint size); |
| 1085 | |
| 1086 | /** Reads or writes data. This operation could be asynchronous (aio). |
| 1087 | |
| 1088 | @param[in] type IO context |
| 1089 | @param[in] sync true if synchronous aio is desired |
| 1090 | @param[in] page_id page id |
| 1091 | @param[in] page_size page size |
| 1092 | @param[in] byte_offset remainder of offset in bytes; in aio this |
| 1093 | must be divisible by the OS block size |
| 1094 | @param[in] len how many bytes to read or write; this must |
| 1095 | not cross a file boundary; in aio this must |
| 1096 | be a block size multiple |
| 1097 | @param[in,out] buf buffer where to store read data or from where |
| 1098 | to write; in aio this must be appropriately |
| 1099 | aligned |
| 1100 | @param[in] message message for aio handler if non-sync aio |
| 1101 | used, else ignored |
| 1102 | @param[in] ignore_missing_space true=ignore missing space during read |
| 1103 | @return DB_SUCCESS, DB_TABLESPACE_DELETED or DB_TABLESPACE_TRUNCATED |
| 1104 | if we are trying to do i/o on a tablespace which does not exist */ |
| 1105 | dberr_t |
| 1106 | fil_io( |
| 1107 | const IORequest& type, |
| 1108 | bool sync, |
| 1109 | const page_id_t& page_id, |
| 1110 | const page_size_t& page_size, |
| 1111 | ulint byte_offset, |
| 1112 | ulint len, |
| 1113 | void* buf, |
| 1114 | void* message, |
| 1115 | bool ignore_missing_space = false); |
| 1116 | |
| 1117 | /**********************************************************************//** |
| 1118 | Waits for an aio operation to complete. This function is used to write the |
| 1119 | handler for completed requests. The aio array of pending requests is divided |
| 1120 | into segments (see os0file.cc for more info). The thread specifies which |
| 1121 | segment it wants to wait for. */ |
| 1122 | void |
| 1123 | fil_aio_wait( |
| 1124 | /*=========*/ |
| 1125 | ulint segment); /*!< in: the number of the segment in the aio |
| 1126 | array to wait for */ |
| 1127 | /**********************************************************************//** |
| 1128 | Flushes to disk possible writes cached by the OS. If the space does not exist |
| 1129 | or is being dropped, does not do anything. */ |
| 1130 | void |
| 1131 | fil_flush( |
| 1132 | /*======*/ |
| 1133 | ulint space_id); /*!< in: file space id (this can be a group of |
| 1134 | log files or a tablespace of the database) */ |
| 1135 | /** Flush a tablespace. |
| 1136 | @param[in,out] space tablespace to flush */ |
| 1137 | void |
| 1138 | fil_flush(fil_space_t* space); |
| 1139 | |
| 1140 | /** Flush to disk the writes in file spaces of the given type |
| 1141 | possibly cached by the OS. |
| 1142 | @param[in] purpose FIL_TYPE_TABLESPACE or FIL_TYPE_LOG */ |
| 1143 | void |
| 1144 | fil_flush_file_spaces( |
| 1145 | fil_type_t purpose); |
| 1146 | /******************************************************************//** |
| 1147 | Checks the consistency of the tablespace cache. |
| 1148 | @return true if ok */ |
| 1149 | bool |
| 1150 | fil_validate(void); |
| 1151 | /*==============*/ |
| 1152 | /********************************************************************//** |
| 1153 | Returns true if file address is undefined. |
| 1154 | @return true if undefined */ |
| 1155 | bool |
| 1156 | fil_addr_is_null( |
| 1157 | /*=============*/ |
| 1158 | fil_addr_t addr); /*!< in: address */ |
| 1159 | /********************************************************************//** |
| 1160 | Get the predecessor of a file page. |
| 1161 | @return FIL_PAGE_PREV */ |
| 1162 | ulint |
| 1163 | fil_page_get_prev( |
| 1164 | /*==============*/ |
| 1165 | const byte* page); /*!< in: file page */ |
| 1166 | /********************************************************************//** |
| 1167 | Get the successor of a file page. |
| 1168 | @return FIL_PAGE_NEXT */ |
| 1169 | ulint |
| 1170 | fil_page_get_next( |
| 1171 | /*==============*/ |
| 1172 | const byte* page); /*!< in: file page */ |
| 1173 | /*********************************************************************//** |
| 1174 | Sets the file page type. */ |
| 1175 | void |
| 1176 | fil_page_set_type( |
| 1177 | /*==============*/ |
| 1178 | byte* page, /*!< in/out: file page */ |
| 1179 | ulint type); /*!< in: type */ |
| 1180 | /** Reset the page type. |
| 1181 | Data files created before MySQL 5.1 may contain garbage in FIL_PAGE_TYPE. |
| 1182 | In MySQL 3.23.53, only undo log pages and index pages were tagged. |
| 1183 | Any other pages were written with uninitialized bytes in FIL_PAGE_TYPE. |
| 1184 | @param[in] page_id page number |
| 1185 | @param[in,out] page page with invalid FIL_PAGE_TYPE |
| 1186 | @param[in] type expected page type |
| 1187 | @param[in,out] mtr mini-transaction */ |
| 1188 | void |
| 1189 | fil_page_reset_type( |
| 1190 | const page_id_t& page_id, |
| 1191 | byte* page, |
| 1192 | ulint type, |
| 1193 | mtr_t* mtr); |
| 1194 | |
| 1195 | /** Get the file page type. |
| 1196 | @param[in] page file page |
| 1197 | @return page type */ |
| 1198 | inline |
| 1199 | uint16_t |
| 1200 | fil_page_get_type(const byte* page) |
| 1201 | { |
| 1202 | return(mach_read_from_2(page + FIL_PAGE_TYPE)); |
| 1203 | } |
| 1204 | |
| 1205 | /** Check (and if needed, reset) the page type. |
| 1206 | Data files created before MySQL 5.1 may contain |
| 1207 | garbage in the FIL_PAGE_TYPE field. |
| 1208 | In MySQL 3.23.53, only undo log pages and index pages were tagged. |
| 1209 | Any other pages were written with uninitialized bytes in FIL_PAGE_TYPE. |
| 1210 | @param[in] page_id page number |
| 1211 | @param[in,out] page page with possibly invalid FIL_PAGE_TYPE |
| 1212 | @param[in] type expected page type |
| 1213 | @param[in,out] mtr mini-transaction */ |
| 1214 | inline |
| 1215 | void |
| 1216 | fil_page_check_type( |
| 1217 | const page_id_t& page_id, |
| 1218 | byte* page, |
| 1219 | ulint type, |
| 1220 | mtr_t* mtr) |
| 1221 | { |
| 1222 | ulint page_type = fil_page_get_type(page); |
| 1223 | |
| 1224 | if (page_type != type) { |
| 1225 | fil_page_reset_type(page_id, page, type, mtr); |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | /** Check (and if needed, reset) the page type. |
| 1230 | Data files created before MySQL 5.1 may contain |
| 1231 | garbage in the FIL_PAGE_TYPE field. |
| 1232 | In MySQL 3.23.53, only undo log pages and index pages were tagged. |
| 1233 | Any other pages were written with uninitialized bytes in FIL_PAGE_TYPE. |
| 1234 | @param[in,out] block block with possibly invalid FIL_PAGE_TYPE |
| 1235 | @param[in] type expected page type |
| 1236 | @param[in,out] mtr mini-transaction */ |
| 1237 | #define fil_block_check_type(block, type, mtr) \ |
| 1238 | fil_page_check_type(block->page.id, block->frame, type, mtr) |
| 1239 | |
| 1240 | /********************************************************************//** |
| 1241 | Delete the tablespace file and any related files like .cfg. |
| 1242 | This should not be called for temporary tables. */ |
| 1243 | void |
| 1244 | fil_delete_file( |
| 1245 | /*============*/ |
| 1246 | const char* path); /*!< in: filepath of the ibd tablespace */ |
| 1247 | |
| 1248 | /********************************************************************//** |
| 1249 | Looks for a pre-existing fil_space_t with the given tablespace ID |
| 1250 | and, if found, returns the name and filepath in newly allocated buffers that the caller must free. |
| 1251 | @param[in] space_id The tablespace ID to search for. |
| 1252 | @param[out] name Name of the tablespace found. |
| 1253 | @param[out] fileapth The filepath of the first datafile for thtablespace found. |
| 1254 | @return true if tablespace is found, false if not. */ |
| 1255 | bool |
| 1256 | fil_space_read_name_and_filepath( |
| 1257 | ulint space_id, |
| 1258 | char** name, |
| 1259 | char** filepath); |
| 1260 | |
| 1261 | /** Convert a file name to a tablespace name. |
| 1262 | @param[in] filename directory/databasename/tablename.ibd |
| 1263 | @return database/tablename string, to be freed with ut_free() */ |
| 1264 | char* |
| 1265 | fil_path_to_space_name( |
| 1266 | const char* filename); |
| 1267 | |
| 1268 | /** Generate redo log for swapping two .ibd files |
| 1269 | @param[in] old_table old table |
| 1270 | @param[in] new_table new table |
| 1271 | @param[in] tmp_name temporary table name |
| 1272 | @param[in,out] mtr mini-transaction |
| 1273 | @return innodb error code */ |
| 1274 | dberr_t |
| 1275 | fil_mtr_rename_log( |
| 1276 | const dict_table_t* old_table, |
| 1277 | const dict_table_t* new_table, |
| 1278 | const char* tmp_name, |
| 1279 | mtr_t* mtr) |
| 1280 | MY_ATTRIBUTE((nonnull, warn_unused_result)); |
| 1281 | |
| 1282 | /** Acquire the fil_system mutex. */ |
| 1283 | #define fil_system_enter() mutex_enter(&fil_system.mutex) |
| 1284 | /** Release the fil_system mutex. */ |
| 1285 | #define fil_system_exit() mutex_exit(&fil_system.mutex) |
| 1286 | |
| 1287 | /*******************************************************************//** |
| 1288 | Returns the table space by a given id, NULL if not found. */ |
| 1289 | fil_space_t* |
| 1290 | fil_space_get_by_id( |
| 1291 | /*================*/ |
| 1292 | ulint id); /*!< in: space id */ |
| 1293 | |
| 1294 | /** Note that a non-predefined persistent tablespace has been modified |
| 1295 | by redo log. |
| 1296 | @param[in,out] space tablespace */ |
| 1297 | void |
| 1298 | fil_names_dirty( |
| 1299 | fil_space_t* space); |
| 1300 | |
| 1301 | /** Write MLOG_FILE_NAME records when a non-predefined persistent |
| 1302 | tablespace was modified for the first time since the latest |
| 1303 | fil_names_clear(). |
| 1304 | @param[in,out] space tablespace |
| 1305 | @param[in,out] mtr mini-transaction */ |
| 1306 | void |
| 1307 | fil_names_dirty_and_write( |
| 1308 | fil_space_t* space, |
| 1309 | mtr_t* mtr); |
| 1310 | |
| 1311 | /** Write MLOG_FILE_NAME records if a persistent tablespace was modified |
| 1312 | for the first time since the latest fil_names_clear(). |
| 1313 | @param[in,out] space tablespace |
| 1314 | @param[in,out] mtr mini-transaction |
| 1315 | @return whether any MLOG_FILE_NAME record was written */ |
| 1316 | inline MY_ATTRIBUTE((warn_unused_result)) |
| 1317 | bool |
| 1318 | fil_names_write_if_was_clean( |
| 1319 | fil_space_t* space, |
| 1320 | mtr_t* mtr) |
| 1321 | { |
| 1322 | ut_ad(log_mutex_own()); |
| 1323 | |
| 1324 | if (space == NULL) { |
| 1325 | return(false); |
| 1326 | } |
| 1327 | |
| 1328 | const bool was_clean = space->max_lsn == 0; |
| 1329 | ut_ad(space->max_lsn <= log_sys.lsn); |
| 1330 | space->max_lsn = log_sys.lsn; |
| 1331 | |
| 1332 | if (was_clean) { |
| 1333 | fil_names_dirty_and_write(space, mtr); |
| 1334 | } |
| 1335 | |
| 1336 | return(was_clean); |
| 1337 | } |
| 1338 | |
| 1339 | extern volatile bool recv_recovery_on; |
| 1340 | |
| 1341 | /** During crash recovery, open a tablespace if it had not been opened |
| 1342 | yet, to get valid size and flags. |
| 1343 | @param[in,out] space tablespace */ |
| 1344 | inline |
| 1345 | void |
| 1346 | fil_space_open_if_needed( |
| 1347 | fil_space_t* space) |
| 1348 | { |
| 1349 | ut_ad(recv_recovery_on); |
| 1350 | |
| 1351 | if (space->size == 0) { |
| 1352 | /* Initially, size and flags will be set to 0, |
| 1353 | until the files are opened for the first time. |
| 1354 | fil_space_get_size() will open the file |
| 1355 | and adjust the size and flags. */ |
| 1356 | #ifdef UNIV_DEBUG |
| 1357 | ulint size = |
| 1358 | #endif /* UNIV_DEBUG */ |
| 1359 | fil_space_get_size(space->id); |
| 1360 | ut_ad(size == space->size); |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | /** On a log checkpoint, reset fil_names_dirty_and_write() flags |
| 1365 | and write out MLOG_FILE_NAME and MLOG_CHECKPOINT if needed. |
| 1366 | @param[in] lsn checkpoint LSN |
| 1367 | @param[in] do_write whether to always write MLOG_CHECKPOINT |
| 1368 | @return whether anything was written to the redo log |
| 1369 | @retval false if no flags were set and nothing written |
| 1370 | @retval true if anything was written to the redo log */ |
| 1371 | bool |
| 1372 | fil_names_clear( |
| 1373 | lsn_t lsn, |
| 1374 | bool do_write); |
| 1375 | |
| 1376 | #ifdef UNIV_ENABLE_UNIT_TEST_MAKE_FILEPATH |
| 1377 | void test_make_filepath(); |
| 1378 | #endif /* UNIV_ENABLE_UNIT_TEST_MAKE_FILEPATH */ |
| 1379 | |
| 1380 | /** Determine the block size of the data file. |
| 1381 | @param[in] space tablespace |
| 1382 | @param[in] offset page number |
| 1383 | @return block size */ |
| 1384 | UNIV_INTERN |
| 1385 | ulint |
| 1386 | fil_space_get_block_size(const fil_space_t* space, unsigned offset); |
| 1387 | |
| 1388 | #include "fil0fil.ic" |
| 1389 | #endif /* UNIV_INNOCHECKSUM */ |
| 1390 | |
| 1391 | #endif /* fil0fil_h */ |
| 1392 | |