MySQL 8.4.2
Source Code Documentation
fsp0file.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2013, 2024, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28/** @file include/fsp0file.h
29 Tablespace data file implementation.
30
31 Created 2013-7-26 by Kevin Lewis
32 *******************************************************/
33
34#ifndef fsp0file_h
35#define fsp0file_h
36
37#include <vector>
38#include "fil0fil.h" /* SPACE_UNKNOWN */
39#include "ha_prototypes.h"
40#include "mem0mem.h"
41#include "os0file.h"
42
43#ifdef UNIV_HOTBACKUP
44#include "fil0fil.h"
45#include "fsp0types.h"
46
47/** MEB routine to get the master key. MEB will extract
48the key from the keyring encrypted file stored in backup.
49@param[in] key_id the id of the master key
50@param[in] key_type master key type
51@param[out] key the master key being returned
52@param[out] key_length the length of the returned key
53@retval 0 if the key is being returned, 1 otherwise. */
54extern int meb_key_fetch(const char *key_id, char **key_type,
55 const char *user_id, void **key, size_t *key_length);
56#endif /* UNIV_HOTBACKUP */
57
58/** Types of raw partitions in innodb_data_file_path */
60
61 /** Not a raw partition */
63
64 /** A 'newraw' partition, only to be initialized */
66
67 /** An initialized raw partition */
69};
70
71/** Data file control information. */
72class Datafile {
73 friend class Tablespace;
74 friend class SysTablespace;
75
76 public:
78 : m_name(),
79 m_filename(),
81 m_size(),
82 m_order(),
85 m_flags(),
86 m_exists(),
87 m_is_valid(),
90 m_filepath(),
98 }
99
100 Datafile(const char *name, uint32_t flags, page_no_t size, ulint order)
102 m_filename(),
104 m_size(size),
105 m_order(order),
108 m_flags(flags),
109 m_exists(),
110 m_is_valid(),
111 m_first_page(),
113 m_filepath(),
115 m_file_info(),
120 ut_ad(m_name != nullptr);
122 /* No op */
123 }
124
135 m_first_page(),
138 m_file_info(),
143 m_name = mem_strdup(file.m_name);
144 ut_ad(m_name != nullptr);
145
146 if (file.m_filepath != nullptr) {
147 m_filepath = mem_strdup(file.m_filepath);
148 ut_a(m_filepath != nullptr);
149 set_filename();
150 } else {
151 m_filepath = nullptr;
152 m_filename = nullptr;
153 }
154 }
155
157
159 ut_a(this != &file);
160
161 ut_ad(m_name == nullptr);
162 m_name = mem_strdup(file.m_name);
163 ut_a(m_name != nullptr);
164
165 m_size = file.m_size;
166 m_order = file.m_order;
167 m_type = file.m_type;
168
170 m_handle = file.m_handle;
171
172 m_exists = file.m_exists;
173 m_is_valid = file.m_is_valid;
174 m_open_flags = file.m_open_flags;
175 m_space_id = file.m_space_id;
176 m_flags = file.m_flags;
177 m_last_os_error = 0;
178
179 if (m_filepath != nullptr) {
181 m_filepath = nullptr;
182 m_filename = nullptr;
183 }
184
185 if (file.m_filepath != nullptr) {
186 m_filepath = mem_strdup(file.m_filepath);
187 ut_a(m_filepath != nullptr);
188 set_filename();
189 }
190
191 /* Do not make a copy of the first page,
192 it should be reread if needed */
193 m_first_page = nullptr;
194 m_encryption_key = nullptr;
195 m_encryption_iv = nullptr;
198
199 m_atomic_write = file.m_atomic_write;
200
201 return (*this);
202 }
203
204 /** Initialize the name and flags of this datafile.
205 @param[in] name tablespace name, will be copied
206 @param[in] flags tablespace flags */
207 void init(const char *name, uint32_t flags);
208
209 /** Release the resources. */
210 void shutdown();
211
212 /** Open a data file in read-only mode to check if it exists
213 so that it can be validated.
214 @param[in] strict whether to issue error messages
215 @return DB_SUCCESS or error code */
216 [[nodiscard]] dberr_t open_read_only(bool strict);
217
218 /** Open a data file in read-write mode during start-up so that
219 doublewrite pages can be restored and then it can be validated.
220 @param[in] read_only_mode if true, then readonly mode checks
221 are enforced.
222 @return DB_SUCCESS or error code */
223 [[nodiscard]] dberr_t open_read_write(bool read_only_mode);
224
225 /** Initialize OS specific file info. */
226 void init_file_info();
227
228 /** Close a data file.
229 @return DB_SUCCESS or error code */
230 dberr_t close();
231
232 /** Returns if the Datafile is created in raw partition
233 @return true if partition used is raw , false otherwise */
234 bool is_raw_type() {
235 return (m_type == SRV_NEW_RAW || m_type == SRV_OLD_RAW);
236 }
237
238 /** Make a full filepath from a directory path and a filename.
239 Prepend the dirpath to filename using the extension given.
240 If dirpath is nullptr, prepend the default datadir to filepath.
241 Store the result in m_filepath.
242 @param[in] dirpath directory path
243 @param[in] filename filename or filepath
244 @param[in] ext filename extension */
245 void make_filepath(const char *dirpath, const char *filename,
247
248 /** Set the filepath by duplicating the filepath sent in */
249 void set_filepath(const char *filepath);
250
251 /** Allocate and set the datafile or tablespace name in m_name.
252 If a name is provided, use it; else if the datafile is file-per-table,
253 extract a file-per-table tablespace name from m_filepath; else it is a
254 general tablespace, so just call it that for now. The value of m_name
255 will be freed in the destructor.
256 @param[in] name Tablespace Name if known, nullptr if not */
257 void set_name(const char *name);
258
259 /** Validates the datafile and checks that it conforms with the expected
260 space ID and flags. The file should exist and be successfully opened
261 in order for this function to validate it.
262 @param[in] space_id The expected tablespace ID.
263 @param[in] flags The expected tablespace flags.
264 @param[in] for_import if it is for importing
265 @retval DB_SUCCESS if tablespace is valid, DB_ERROR if not.
266 m_is_valid is also set true on success, else false. */
267 [[nodiscard]] dberr_t validate_to_dd(space_id_t space_id, uint32_t flags,
268 bool for_import);
269
270 /** Validates this datafile for the purpose of recovery. The file should
271 exist and be successfully opened. We initially open it in read-only mode
272 because we just want to read the SpaceID. However, if the first page is
273 corrupt and needs to be restored from the doublewrite buffer, we will reopen
274 it in write mode and try to restore that page. The file will be closed when
275 returning from this method.
276 @param[in] space_id Expected space ID
277 @retval DB_SUCCESS on success
278 m_is_valid is also set true on success, else false. */
280
281 /** Checks the consistency of the first page of a datafile when the
282 tablespace is opened. This occurs before the fil_space_t is created so the
283 Space ID found here must not already be open. m_is_valid is set true on
284 success, else false. The datafile is always closed when returning from this
285 method.
286 @param[in] space_id Expected space ID
287 @param[out] flush_lsn contents of FIL_PAGE_FILE_FLUSH_LSN
288 @param[in] for_import if it is for importing
289 (only valid for the first file of the system tablespace)
290 @retval DB_WRONG_FILE_NAME tablespace in file header doesn't match
291 expected value
292 @retval DB_SUCCESS on if the datafile is valid
293 @retval DB_CORRUPTION if the datafile is not readable
294 @retval DB_INVALID_ENCRYPTION_META if the encryption meta data
295 is not readable
296 @retval DB_TABLESPACE_EXISTS if there is a duplicate space_id */
298 lsn_t *flush_lsn, bool for_import);
299
300 /** Get LSN of first page */
302 ut_ad(m_first_page != nullptr);
304 }
305
306 /** Get Datafile::m_name.
307 @return m_name */
308 const char *name() const { return (m_name); }
309
310 /** Get Datafile::m_filepath.
311 @return m_filepath */
312 const char *filepath() const { return (m_filepath); }
313
314 /** Get Datafile::m_handle.
315 @return m_handle */
317 ut_ad(is_open());
318 return (m_handle);
319 }
320
321 /** Get Datafile::m_order.
322 @return m_order */
323 ulint order() const { return (m_order); }
324
325 /** Get Datafile::m_server_version.
326 @return m_server_version */
328
329 /** Get Datafile::m_space_version.
330 @return m_space_version */
331 ulint space_version() const { return (m_space_version); }
332
333 /** Get Datafile::m_space_id.
334 @return m_space_id */
335 space_id_t space_id() const { return (m_space_id); }
336
337 /** Get Datafile::m_flags.
338 @return m_flags */
339 uint32_t flags() const { return (m_flags); }
340
341 /**
342 @return true if m_handle is open, false if not */
343 bool is_open() const { return (m_handle.m_file != OS_FILE_CLOSED); }
344
345 /** Get Datafile::m_is_valid.
346 @return m_is_valid */
347 bool is_valid() const { return (m_is_valid); }
348
349 /** Get the last OS error reported
350 @return m_last_os_error */
351 ulint last_os_error() const { return (m_last_os_error); }
352
353 /** Do a quick test if the filepath provided looks the same as this filepath
354 byte by byte. If they are two different looking paths to the same file,
355 same_as() will be used to show that after the files are opened.
356 @param[in] other filepath to compare with
357 @retval true if it is the same filename by byte comparison
358 @retval false if it looks different */
359 bool same_filepath_as(const char *other) const;
360
361 /** Test if another opened datafile is the same file as this object.
362 @param[in] other Datafile to compare with
363 @return true if it is the same file, else false */
364 bool same_as(const Datafile &other) const;
365
366 /** Determine the space id of the given file descriptor by reading
367 a few pages from the beginning of the .ibd file.
368 @return DB_SUCCESS if space id was successfully identified,
369 else DB_ERROR. */
371
372 /** @return file size in number of pages */
373 page_no_t size() const { return (m_size); }
374
375#ifdef UNIV_HOTBACKUP
376 /** Set the tablespace ID.
377 @param[in] space_id Tablespace ID to set */
379 ut_ad(space_id <= 0xFFFFFFFFU);
381 }
382
383 /** Set th tablespace flags
384 @param[in] flags Tablespace flags */
385 void set_flags(uint32_t flags) { m_flags = flags; }
386#endif /* UNIV_HOTBACKUP */
387
388 private:
389 /** Free the filepath buffer. */
390 void free_filepath();
391
392 /** Set the filename pointer to the start of the file name
393 in the filepath. */
395 if (m_filepath == nullptr) {
396 return;
397 }
398
399 char *last_slash = strrchr(m_filepath, OS_PATH_SEPARATOR);
400
401 m_filename = last_slash ? last_slash + 1 : m_filepath;
402 }
403
404 /** Create/open a data file.
405 @param[in] read_only_mode if true, then readonly mode checks
406 are enforced.
407 @return DB_SUCCESS or error code */
408 [[nodiscard]] dberr_t open_or_create(bool read_only_mode);
409
410 /** Reads a few significant fields from the first page of the
411 datafile, which must already be open.
412 @return DB_SUCCESS or DB_IO_ERROR if page cannot be read */
413 [[nodiscard]] dberr_t read_first_page();
414
415 /** Free the first page from memory when it is no longer needed. */
416 void free_first_page();
417
418 /** Set the Datafile::m_open_flags.
419 @param open_flags The Open flags to set. */
421 m_open_flags = open_flags;
422 }
423
424 /** Finds a given page of the given space id from the double write buffer
425 and copies it to the corresponding .ibd file.
426 @param[in] restore_page_no Page number to restore
427 @return DB_SUCCESS if page was restored from doublewrite, else DB_ERROR */
429
430 private:
431 /** Datafile name at the tablespace location.
432 This is either the basename of the file if an absolute path
433 was entered, or it is the relative path to the datadir or
434 Tablespace::m_path. */
435 char *m_name;
436
437 /** Points into m_filepath to the file name with extension */
439
440 /** Open file handle */
442
443 /** Flags to use for opening the data file */
445
446 /** size in pages */
448
449 /** ordinal position of this datafile in the tablespace */
451
452 /** The type of the data file */
454
455 /** Tablespace ID. Contained in the datafile header.
456 If this is a system tablespace, FSP_SPACE_ID is only valid
457 in the first datafile. */
459
460 /** Server version */
462
463 /** Space version */
465
466 /** Tablespace flags. Contained in the datafile header.
467 If this is a system tablespace, FSP_SPACE_FLAGS are only valid
468 in the first datafile. */
469 uint32_t m_flags;
470
471 /** true if file already existed on startup */
473
474 /* true if the tablespace is valid */
476
477 /** Buffer to hold first page */
479
480 /** true if atomic writes enabled for this file */
482
483 protected:
484 /** Physical file path with base name and extension */
486
487 /** Last OS error received so it can be reported if needed. */
489
490 public:
491 /** Use the following to determine the uniqueness of this datafile. */
492#ifdef _WIN32
493 using WIN32_FILE_INFO = BY_HANDLE_FILE_INFORMATION;
494
495 /** Use fields dwVolumeSerialNumber, nFileIndexLow, nFileIndexHigh. */
496 WIN32_FILE_INFO m_file_info;
497#else
498 /** Use field st_ino. */
499 struct stat m_file_info;
500#endif /* WIN32 */
501
502 /** Encryption key read from first page */
504
505 /** Encryption iv read from first page */
507
508 /** Encryption operation in progress */
510
511 /** Master key id read from first page */
513};
514#endif /* fsp0file_h */
uint32_t space_id_t
Tablespace identifier.
Definition: api0api.h:47
uint32_t page_no_t
Page number.
Definition: api0api.h:45
Data file control information.
Definition: fsp0file.h:72
Datafile()
Definition: fsp0file.h:77
space_id_t space_id() const
Get Datafile::m_space_id.
Definition: fsp0file.h:335
byte * m_encryption_iv
Encryption iv read from first page.
Definition: fsp0file.h:506
byte * m_encryption_key
Encryption key read from first page.
Definition: fsp0file.h:503
dberr_t find_space_id()
Determine the space id of the given file descriptor by reading a few pages from the beginning of the ...
Definition: fsp0file.cc:713
dberr_t open_read_write(bool read_only_mode)
Open a data file in read-write mode during start-up so that doublewrite pages can be restored and the...
Definition: fsp0file.cc:148
uint32_t m_server_version
Server version.
Definition: fsp0file.h:461
bool is_valid() const
Get Datafile::m_is_valid.
Definition: fsp0file.h:347
space_id_t m_space_id
Tablespace ID.
Definition: fsp0file.h:458
const char * filepath() const
Get Datafile::m_filepath.
Definition: fsp0file.h:312
void free_first_page()
Free the first page from memory when it is no longer needed.
Definition: fsp0file.cc:363
bool m_atomic_write
true if atomic writes enabled for this file
Definition: fsp0file.h:481
dberr_t validate_for_recovery(space_id_t space_id)
Validates this datafile for the purpose of recovery.
Definition: fsp0file.cc:450
ulint m_last_os_error
Last OS error received so it can be reported if needed.
Definition: fsp0file.h:488
bool m_exists
true if file already existed on startup
Definition: fsp0file.h:472
bool is_raw_type()
Returns if the Datafile is created in raw partition.
Definition: fsp0file.h:234
Datafile(const Datafile &file)
Definition: fsp0file.h:125
void make_filepath(const char *dirpath, const char *filename, ib_file_suffix ext)
Make a full filepath from a directory path and a filename.
Definition: fsp0file.cc:206
ulint m_order
ordinal position of this datafile in the tablespace
Definition: fsp0file.h:450
uint32_t flags() const
Get Datafile::m_flags.
Definition: fsp0file.h:339
void set_open_flags(os_file_create_t open_flags)
Set the Datafile::m_open_flags.
Definition: fsp0file.h:420
bool same_filepath_as(const char *other) const
Do a quick test if the filepath provided looks the same as this filepath byte by byte.
Definition: fsp0file.cc:254
dberr_t validate_to_dd(space_id_t space_id, uint32_t flags, bool for_import)
Validates the datafile and checks that it conforms with the expected space ID and flags.
Definition: fsp0file.cc:376
dberr_t restore_from_doublewrite(page_no_t restore_page_no)
Finds a given page of the given space id from the double write buffer and copies it to the correspond...
Definition: fsp0file.cc:867
void set_filename()
Set the filename pointer to the start of the file name in the filepath.
Definition: fsp0file.h:394
page_no_t m_size
size in pages
Definition: fsp0file.h:447
device_t m_type
The type of the data file.
Definition: fsp0file.h:453
pfs_os_file_t handle() const
Get Datafile::m_handle.
Definition: fsp0file.h:316
dberr_t validate_first_page(space_id_t space_id, lsn_t *flush_lsn, bool for_import)
Checks the consistency of the first page of a datafile when the tablespace is opened.
Definition: fsp0file.cc:513
lsn_t get_flush_lsn()
Get LSN of first page.
Definition: fsp0file.h:301
dberr_t open_or_create(bool read_only_mode)
Create/open a data file.
Definition: fsp0file.cc:91
void init_file_info()
Initialize OS specific file info.
Definition: fsp0file.cc:178
void shutdown()
Release the resources.
Definition: fsp0file.cc:67
ulint order() const
Get Datafile::m_order.
Definition: fsp0file.h:323
~Datafile()
Definition: fsp0file.h:156
struct stat m_file_info
Use the following to determine the uniqueness of this datafile.
Definition: fsp0file.h:499
dberr_t open_read_only(bool strict)
Open a data file in read-only mode to check if it exists so that it can be validated.
Definition: fsp0file.cc:113
Datafile(const char *name, uint32_t flags, page_no_t size, ulint order)
Definition: fsp0file.h:100
Datafile & operator=(const Datafile &file)
Definition: fsp0file.h:158
void free_filepath()
Free the filepath buffer.
Definition: fsp0file.cc:240
void init(const char *name, uint32_t flags)
Initialize the name and flags of this datafile.
Definition: fsp0file.cc:56
ulint server_version() const
Get Datafile::m_server_version.
Definition: fsp0file.h:327
bool same_as(const Datafile &other) const
Test if another opened datafile is the same file as this object.
Definition: fsp0file.cc:261
char * m_name
Datafile name at the tablespace location.
Definition: fsp0file.h:435
byte * m_first_page
Buffer to hold first page.
Definition: fsp0file.h:478
pfs_os_file_t m_handle
Open file handle.
Definition: fsp0file.h:441
bool is_open() const
Definition: fsp0file.h:343
uint32_t m_encryption_master_key_id
Master key id read from first page.
Definition: fsp0file.h:512
uint32_t m_space_version
Space version.
Definition: fsp0file.h:464
void set_filepath(const char *filepath)
Set the filepath by duplicating the filepath sent in.
Definition: fsp0file.cc:231
uint32_t m_flags
Tablespace flags.
Definition: fsp0file.h:469
dberr_t close()
Close a data file.
Definition: fsp0file.cc:188
Encryption::Progress m_encryption_op_in_progress
Encryption operation in progress.
Definition: fsp0file.h:509
ulint last_os_error() const
Get the last OS error reported.
Definition: fsp0file.h:351
dberr_t read_first_page()
Reads a few significant fields from the first page of the datafile, which must already be open.
Definition: fsp0file.cc:314
os_file_create_t m_open_flags
Flags to use for opening the data file.
Definition: fsp0file.h:444
ulint space_version() const
Get Datafile::m_space_version.
Definition: fsp0file.h:331
char * m_filepath
Physical file path with base name and extension.
Definition: fsp0file.h:485
const char * name() const
Get Datafile::m_name.
Definition: fsp0file.h:308
char * m_filename
Points into m_filepath to the file name with extension.
Definition: fsp0file.h:438
bool m_is_valid
Definition: fsp0file.h:475
page_no_t size() const
Definition: fsp0file.h:373
void set_name(const char *name)
Allocate and set the datafile or tablespace name in m_name.
Definition: fsp0file.cc:279
Encryption algorithm.
Definition: os0enc.h:54
Progress
Encryption progress type.
Definition: os0enc.h:80
Data structure that contains the information about shared tablespaces.
Definition: fsp0sysspace.h:58
Data structure that contains the information about shared tablespaces.
Definition: fsp0space.h:47
void set_space_id(space_id_t space_id)
Set the space id of the tablespace.
Definition: fsp0space.h:116
void set_flags(uint32_t fsp_flags)
Set the tablespace flags.
Definition: fsp0space.h:127
dberr_t
Definition: db0err.h:39
The low-level file system.
constexpr space_id_t SPACE_UNKNOWN
Unknown space id.
Definition: fil0fil.h:1165
ib_file_suffix
Common InnoDB file extensions.
Definition: fil0fil.h:587
constexpr uint32_t FIL_PAGE_LSN
lsn of the end of the newest modification log record to the page
Definition: fil0types.h:67
device_t
Types of raw partitions in innodb_data_file_path.
Definition: fsp0file.h:59
@ SRV_NOT_RAW
Not a raw partition.
Definition: fsp0file.h:62
@ SRV_NEW_RAW
A 'newraw' partition, only to be initialized.
Definition: fsp0file.h:65
@ SRV_OLD_RAW
An initialized raw partition.
Definition: fsp0file.h:68
Prototypes for global functions in ha_innodb.cc that are called by InnoDB C code.
uint64_t lsn_t
Type used for all log sequence number storage and arithmetic.
Definition: log0types.h:63
static uint64_t mach_read_from_8(const byte *b)
The following function is used to fetch data from 8 consecutive bytes.
The memory management.
static char * mem_strdup(const char *str)
Duplicates a NUL-terminated string.
Definition: os0file.h:89
int key_type
Definition: method.h:38
Json_data_extension ext
Definition: backend.cc:52
void free(void *ptr) noexcept
Releases storage which has been dynamically allocated through any of the ut::malloc*(),...
Definition: ut0new.h:717
The interface to the operating system file io.
os_file_create_t
Options for os_file_create_func.
Definition: os0file.h:199
@ OS_FILE_OPEN
to open an existing file (if doesn't exist, error)
Definition: os0file.h:200
static constexpr os_fd_t OS_FILE_CLOSED
Definition: os0file.h:155
const char * filename
Definition: pfs_example_component_population.cc:67
required string key
Definition: replication_asynchronous_connection_failover.proto:60
Common file descriptor for file IO instrumentation with PFS on windows and other platforms.
Definition: os0file.h:176
os_file_t m_file
Definition: os0file.h:186
#define OS_PATH_SEPARATOR
Definition: univ.i:538
unsigned long int ulint
Definition: univ.i:406
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:105
#define ut_a(EXPR)
Abort execution if EXPR does not evaluate to nonzero.
Definition: ut0dbg.h:93