MySQL 9.1.0
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(),
89 m_filepath(),
97 }
98
99 Datafile(const char *name, uint32_t flags, page_no_t size, ulint order)
101 m_filename(),
103 m_size(size),
104 m_order(order),
107 m_flags(flags),
108 m_exists(),
109 m_is_valid(),
110 m_first_page(),
111 m_filepath(),
113 m_file_info(),
118 ut_ad(m_name != nullptr);
120 /* No op */
121 }
122
133 m_first_page(),
135 m_file_info(),
140 m_name = mem_strdup(file.m_name);
141 ut_ad(m_name != nullptr);
142
143 if (file.m_filepath != nullptr) {
144 m_filepath = mem_strdup(file.m_filepath);
145 ut_a(m_filepath != nullptr);
146 set_filename();
147 } else {
148 m_filepath = nullptr;
149 m_filename = nullptr;
150 }
151 }
152
154
156 ut_a(this != &file);
157
158 ut_ad(m_name == nullptr);
159 m_name = mem_strdup(file.m_name);
160 ut_a(m_name != nullptr);
161
162 m_size = file.m_size;
163 m_order = file.m_order;
164 m_type = file.m_type;
165
167 m_handle = file.m_handle;
168
169 m_exists = file.m_exists;
170 m_is_valid = file.m_is_valid;
171 m_open_flags = file.m_open_flags;
172 m_space_id = file.m_space_id;
173 m_flags = file.m_flags;
174 m_last_os_error = 0;
175
176 if (m_filepath != nullptr) {
178 m_filepath = nullptr;
179 m_filename = nullptr;
180 }
181
182 if (file.m_filepath != nullptr) {
183 m_filepath = mem_strdup(file.m_filepath);
184 ut_a(m_filepath != nullptr);
185 set_filename();
186 }
187
188 /* Do not make a copy of the first page,
189 it should be reread if needed */
190 m_first_page = nullptr;
191 m_encryption_key = nullptr;
192 m_encryption_iv = nullptr;
195
196 return (*this);
197 }
198
199 /** Initialize the name and flags of this datafile.
200 @param[in] name tablespace name, will be copied
201 @param[in] flags tablespace flags */
202 void init(const char *name, uint32_t flags);
203
204 /** Release the resources. */
205 void shutdown();
206
207 /** Open a data file in read-only mode to check if it exists
208 so that it can be validated.
209 @param[in] strict whether to issue error messages
210 @return DB_SUCCESS or error code */
211 [[nodiscard]] dberr_t open_read_only(bool strict);
212
213 /** Open a data file in read-write mode during start-up so that
214 doublewrite pages can be restored and then it can be validated.
215 @param[in] read_only_mode if true, then readonly mode checks
216 are enforced.
217 @return DB_SUCCESS or error code */
218 [[nodiscard]] dberr_t open_read_write(bool read_only_mode);
219
220 /** Initialize OS specific file info. */
221 void init_file_info();
222
223 /** Close a data file.
224 @return DB_SUCCESS or error code */
225 dberr_t close();
226
227 /** Returns if the Datafile is created in raw partition
228 @return true if partition used is raw , false otherwise */
229 bool is_raw_type() {
230 return (m_type == SRV_NEW_RAW || m_type == SRV_OLD_RAW);
231 }
232
233 /** Make a full filepath from a directory path and a filename.
234 Prepend the dirpath to filename using the extension given.
235 If dirpath is nullptr, prepend the default datadir to filepath.
236 Store the result in m_filepath.
237 @param[in] dirpath directory path
238 @param[in] filename filename or filepath
239 @param[in] ext filename extension */
240 void make_filepath(const char *dirpath, const char *filename,
242
243 /** Set the filepath by duplicating the filepath sent in */
244 void set_filepath(const char *filepath);
245
246 /** Allocate and set the datafile or tablespace name in m_name.
247 If a name is provided, use it; else if the datafile is file-per-table,
248 extract a file-per-table tablespace name from m_filepath; else it is a
249 general tablespace, so just call it that for now. The value of m_name
250 will be freed in the destructor.
251 @param[in] name Tablespace Name if known, nullptr if not */
252 void set_name(const char *name);
253
254 /** Validates the datafile and checks that it conforms with the expected
255 space ID and flags. The file should exist and be successfully opened
256 in order for this function to validate it.
257 @param[in] space_id The expected tablespace ID.
258 @param[in] flags The expected tablespace flags.
259 @param[in] for_import if it is for importing
260 @retval DB_SUCCESS if tablespace is valid, DB_ERROR if not.
261 m_is_valid is also set true on success, else false. */
262 [[nodiscard]] dberr_t validate_to_dd(space_id_t space_id, uint32_t flags,
263 bool for_import);
264
265 /** Validates this datafile for the purpose of recovery. The file should
266 exist and be successfully opened. We initially open it in read-only mode
267 because we just want to read the SpaceID. However, if the first page is
268 corrupt and needs to be restored from the doublewrite buffer, we will reopen
269 it in write mode and try to restore that page. The file will be closed when
270 returning from this method.
271 @param[in] space_id Expected space ID
272 @retval DB_SUCCESS on success
273 m_is_valid is also set true on success, else false. */
275
276 /** Checks the consistency of the first page of a datafile when the
277 tablespace is opened. This occurs before the fil_space_t is created so the
278 Space ID found here must not already be open. m_is_valid is set true on
279 success, else false. The datafile is always closed when returning from this
280 method.
281 @param[in] space_id Expected space ID
282 @param[out] flush_lsn contents of FIL_PAGE_FILE_FLUSH_LSN
283 @param[in] for_import if it is for importing
284 (only valid for the first file of the system tablespace)
285 @retval DB_WRONG_FILE_NAME tablespace in file header doesn't match
286 expected value
287 @retval DB_SUCCESS on if the datafile is valid
288 @retval DB_CORRUPTION if the datafile is not readable
289 @retval DB_INVALID_ENCRYPTION_META if the encryption meta data
290 is not readable
291 @retval DB_TABLESPACE_EXISTS if there is a duplicate space_id */
293 lsn_t *flush_lsn, bool for_import);
294
295 /** Get LSN of first page */
297 ut_ad(m_first_page != nullptr);
299 }
300
301 /** Get Datafile::m_name.
302 @return m_name */
303 const char *name() const { return (m_name); }
304
305 /** Get Datafile::m_filepath.
306 @return m_filepath */
307 const char *filepath() const { return (m_filepath); }
308
309 /** Get Datafile::m_handle.
310 @return m_handle */
312 ut_ad(is_open());
313 return (m_handle);
314 }
315
316 /** Get Datafile::m_order.
317 @return m_order */
318 ulint order() const { return (m_order); }
319
320 /** Get Datafile::m_server_version.
321 @return m_server_version */
323
324 /** Get Datafile::m_space_version.
325 @return m_space_version */
326 ulint space_version() const { return (m_space_version); }
327
328 /** Get Datafile::m_space_id.
329 @return m_space_id */
330 space_id_t space_id() const { return (m_space_id); }
331
332 /** Get Datafile::m_flags.
333 @return m_flags */
334 uint32_t flags() const { return (m_flags); }
335
336 /**
337 @return true if m_handle is open, false if not */
338 bool is_open() const { return (m_handle.m_file != OS_FILE_CLOSED); }
339
340 /** Get Datafile::m_is_valid.
341 @return m_is_valid */
342 bool is_valid() const { return (m_is_valid); }
343
344 /** Get the last OS error reported
345 @return m_last_os_error */
346 ulint last_os_error() const { return (m_last_os_error); }
347
348 /** Do a quick test if the filepath provided looks the same as this filepath
349 byte by byte. If they are two different looking paths to the same file,
350 same_as() will be used to show that after the files are opened.
351 @param[in] other filepath to compare with
352 @retval true if it is the same filename by byte comparison
353 @retval false if it looks different */
354 bool same_filepath_as(const char *other) const;
355
356 /** Test if another opened datafile is the same file as this object.
357 @param[in] other Datafile to compare with
358 @return true if it is the same file, else false */
359 bool same_as(const Datafile &other) const;
360
361 /** Determine the space id of the given file descriptor by reading
362 a few pages from the beginning of the .ibd file.
363 @return DB_SUCCESS if space id was successfully identified,
364 else DB_ERROR. */
366
367 /** @return file size in number of pages */
368 page_no_t size() const { return (m_size); }
369
370#ifdef UNIV_HOTBACKUP
371 /** Set the tablespace ID.
372 @param[in] space_id Tablespace ID to set */
374 ut_ad(space_id <= 0xFFFFFFFFU);
376 }
377
378 /** Set th tablespace flags
379 @param[in] flags Tablespace flags */
380 void set_flags(uint32_t flags) { m_flags = flags; }
381#endif /* UNIV_HOTBACKUP */
382
383 private:
384 /** Free the filepath buffer. */
385 void free_filepath();
386
387 /** Set the filename pointer to the start of the file name
388 in the filepath. */
390 if (m_filepath == nullptr) {
391 return;
392 }
393
394 char *last_slash = strrchr(m_filepath, OS_PATH_SEPARATOR);
395
396 m_filename = last_slash ? last_slash + 1 : m_filepath;
397 }
398
399 /** Create/open a data file.
400 @param[in] read_only_mode if true, then readonly mode checks
401 are enforced.
402 @return DB_SUCCESS or error code */
403 [[nodiscard]] dberr_t open_or_create(bool read_only_mode);
404
405 /** Reads a few significant fields from the first page of the
406 datafile, which must already be open.
407 @return DB_SUCCESS or DB_IO_ERROR if page cannot be read */
408 [[nodiscard]] dberr_t read_first_page();
409
410 /** Free the first page from memory when it is no longer needed. */
411 void free_first_page();
412
413 /** Set the Datafile::m_open_flags.
414 @param open_flags The Open flags to set. */
416 m_open_flags = open_flags;
417 }
418
419 /** Finds a given page of the given space id from the double write buffer
420 and copies it to the corresponding .ibd file.
421 @param[in] restore_page_no Page number to restore
422 @return DB_SUCCESS if page was restored from doublewrite, else DB_ERROR */
424
425 private:
426 /** Datafile name at the tablespace location.
427 This is either the basename of the file if an absolute path
428 was entered, or it is the relative path to the datadir or
429 Tablespace::m_path. */
430 char *m_name;
431
432 /** Points into m_filepath to the file name with extension */
434
435 /** Open file handle */
437
438 /** Flags to use for opening the data file */
440
441 /** size in pages */
443
444 /** ordinal position of this datafile in the tablespace */
446
447 /** The type of the data file */
449
450 /** Tablespace ID. Contained in the datafile header.
451 If this is a system tablespace, FSP_SPACE_ID is only valid
452 in the first datafile. */
454
455 /** Server version */
457
458 /** Space version */
460
461 /** Tablespace flags. Contained in the datafile header.
462 If this is a system tablespace, FSP_SPACE_FLAGS are only valid
463 in the first datafile. */
464 uint32_t m_flags;
465
466 /** true if file already existed on startup */
468
469 /* true if the tablespace is valid */
471
472 /** Buffer to hold first page */
474
475 protected:
476 /** Physical file path with base name and extension */
478
479 /** Last OS error received so it can be reported if needed. */
481
482 public:
483 /** Use the following to determine the uniqueness of this datafile. */
484#ifdef _WIN32
485 using WIN32_FILE_INFO = BY_HANDLE_FILE_INFORMATION;
486
487 /** Use fields dwVolumeSerialNumber, nFileIndexLow, nFileIndexHigh. */
488 WIN32_FILE_INFO m_file_info;
489#else
490 /** Use field st_ino. */
491 struct stat m_file_info;
492#endif /* WIN32 */
493
494 /** Encryption key read from first page */
496
497 /** Encryption iv read from first page */
499
500 /** Encryption operation in progress */
502
503 /** Master key id read from first page */
505};
506#endif /* fsp0file_h */
uint32_t space_id_t
Tablespace identifier.
Definition: api0api.h:48
uint32_t page_no_t
Page number.
Definition: api0api.h:46
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:330
byte * m_encryption_iv
Encryption iv read from first page.
Definition: fsp0file.h:498
byte * m_encryption_key
Encryption key read from first page.
Definition: fsp0file.h:495
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:712
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:147
uint32_t m_server_version
Server version.
Definition: fsp0file.h:456
bool is_valid() const
Get Datafile::m_is_valid.
Definition: fsp0file.h:342
space_id_t m_space_id
Tablespace ID.
Definition: fsp0file.h:453
const char * filepath() const
Get Datafile::m_filepath.
Definition: fsp0file.h:307
void free_first_page()
Free the first page from memory when it is no longer needed.
Definition: fsp0file.cc:362
dberr_t validate_for_recovery(space_id_t space_id)
Validates this datafile for the purpose of recovery.
Definition: fsp0file.cc:449
ulint m_last_os_error
Last OS error received so it can be reported if needed.
Definition: fsp0file.h:480
bool m_exists
true if file already existed on startup
Definition: fsp0file.h:467
bool is_raw_type()
Returns if the Datafile is created in raw partition.
Definition: fsp0file.h:229
Datafile(const Datafile &file)
Definition: fsp0file.h:123
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:205
ulint m_order
ordinal position of this datafile in the tablespace
Definition: fsp0file.h:445
uint32_t flags() const
Get Datafile::m_flags.
Definition: fsp0file.h:334
void set_open_flags(os_file_create_t open_flags)
Set the Datafile::m_open_flags.
Definition: fsp0file.h:415
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:253
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:375
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:866
void set_filename()
Set the filename pointer to the start of the file name in the filepath.
Definition: fsp0file.h:389
page_no_t m_size
size in pages
Definition: fsp0file.h:442
device_t m_type
The type of the data file.
Definition: fsp0file.h:448
pfs_os_file_t handle() const
Get Datafile::m_handle.
Definition: fsp0file.h:311
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:512
lsn_t get_flush_lsn()
Get LSN of first page.
Definition: fsp0file.h:296
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:177
void shutdown()
Release the resources.
Definition: fsp0file.cc:67
ulint order() const
Get Datafile::m_order.
Definition: fsp0file.h:318
~Datafile()
Definition: fsp0file.h:153
struct stat m_file_info
Use the following to determine the uniqueness of this datafile.
Definition: fsp0file.h:491
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:112
Datafile(const char *name, uint32_t flags, page_no_t size, ulint order)
Definition: fsp0file.h:99
Datafile & operator=(const Datafile &file)
Definition: fsp0file.h:155
void free_filepath()
Free the filepath buffer.
Definition: fsp0file.cc:239
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:322
bool same_as(const Datafile &other) const
Test if another opened datafile is the same file as this object.
Definition: fsp0file.cc:260
char * m_name
Datafile name at the tablespace location.
Definition: fsp0file.h:430
byte * m_first_page
Buffer to hold first page.
Definition: fsp0file.h:473
pfs_os_file_t m_handle
Open file handle.
Definition: fsp0file.h:436
bool is_open() const
Definition: fsp0file.h:338
uint32_t m_encryption_master_key_id
Master key id read from first page.
Definition: fsp0file.h:504
uint32_t m_space_version
Space version.
Definition: fsp0file.h:459
void set_filepath(const char *filepath)
Set the filepath by duplicating the filepath sent in.
Definition: fsp0file.cc:230
uint32_t m_flags
Tablespace flags.
Definition: fsp0file.h:464
dberr_t close()
Close a data file.
Definition: fsp0file.cc:187
Encryption::Progress m_encryption_op_in_progress
Encryption operation in progress.
Definition: fsp0file.h:501
ulint last_os_error() const
Get the last OS error reported.
Definition: fsp0file.h:346
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:313
os_file_create_t m_open_flags
Flags to use for opening the data file.
Definition: fsp0file.h:439
ulint space_version() const
Get Datafile::m_space_version.
Definition: fsp0file.h:326
char * m_filepath
Physical file path with base name and extension.
Definition: fsp0file.h:477
const char * name() const
Get Datafile::m_name.
Definition: fsp0file.h:303
char * m_filename
Points into m_filepath to the file name with extension.
Definition: fsp0file.h:433
bool m_is_valid
Definition: fsp0file.h:470
page_no_t size() const
Definition: fsp0file.h:368
void set_name(const char *name)
Allocate and set the datafile or tablespace name in m_name.
Definition: fsp0file.cc:278
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:1162
ib_file_suffix
Common InnoDB file extensions.
Definition: fil0fil.h:584
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:50
void free(void *ptr) noexcept
Releases storage which has been dynamically allocated through any of the ut::malloc*(),...
Definition: ut0new.h:719
The interface to the operating system file io.
os_file_create_t
Options for os_file_create_func.
Definition: os0file.h:195
@ OS_FILE_OPEN
to open an existing file (if doesn't exist, error)
Definition: os0file.h:196
static constexpr os_fd_t OS_FILE_CLOSED
Definition: os0file.h:151
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:172
os_file_t m_file
Definition: os0file.h:182
#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