MySQL 26.7.0
Source Code Documentation
fsp0file.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2013, 2026, 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 nodes specifed in innodb_data_file_path */
60
61 /** Not a raw partition, a regular file */
63
64 /** A 'newraw' partition, only to be initialized */
65 NEW_RAW,
66
67 /** An initialized raw partition */
69};
70
71/** Data file control information. */
72class Datafile {
73 friend class SysTablespace;
74
75 public:
77
78 Datafile(const Datafile &file) = delete;
79
81
82 Datafile &operator=(const Datafile &file) = delete;
83
84 /** Release the resources. */
85 void shutdown();
86
87 /** Open a data file in read-only mode to check if it exists
88 so that it can be validated.
89 @return DB_SUCCESS or error code */
90 [[nodiscard]] dberr_t open_read_only();
91
92 /** Close a data file.
93 @return DB_SUCCESS or error code */
94 dberr_t close();
95
96 /** Extracts basic space fields from the first page header.
97 @param[in] page The content of the first page of the tablespace.
98 */
99 void extract_fields_from_first_page(const byte *page);
100
101 /** Returns if the Datafile is created in raw partition
102 @return true if partition used is raw , false otherwise */
103 bool is_raw_type() {
106 }
107
108 /** Make a full filepath from a directory path and a filename.
109 Prepend the dirpath to filename using the extension given.
110 If dirpath is nullptr, prepend the default datadir to filepath.
111 Store the result in m_filepath.
112 @param[in] dirpath directory path
113 @param[in] filename filename or filepath
114 @param[in] ext filename extension */
115 void make_filepath(const char *dirpath, const char *filename,
117
118 /** Set the filepath by duplicating the filepath sent in */
119 void set_filepath(const char *filepath);
120
121 /** Validates the first page of tablespace supplied and checks that it
122 conforms with the expected space ID and flags. The datafile will not be
123 attempted to be opened.
124 @param[in] page The content of the first page of the tablespace.
125 @param[in] space_id The expected tablespace ID.
126 @param[in] flags The expected tablespace flags.
127 @param[in] filepath The file path to the first node in the
128 tablespace, used for error message printing.
129 @param[in] for_import if it is for importing
130 @retval DB_SUCCESS if tablespace is valid, DB_ERROR if not.
131 m_is_valid is also set true on success, else false. */
132 [[nodiscard]] dberr_t validate_to_dd(const byte *page, space_id_t space_id,
133 uint32_t flags,
134 const std::string &filepath,
135 bool for_import);
136
137 /** Validates this datafile for the purpose of recovery. The file must
138 exist and be already opened. The datafile is always closed when returning from
139 this method.
140 @param[in] space_id Expected space ID
141 @param[in] expected_physical_page_size Physical page size of the
142 tablespace.
143 @retval DB_WRONG_FILE_NAME tablespace in file header doesn't match
144 expected value
145 @retval DB_SUCCESS on if the datafile is valid
146 @retval DB_CORRUPTION if the datafile is not readable
147 @retval DB_INVALID_ENCRYPTION_META if the encryption meta data
148 is not readable
149 @retval DB_TABLESPACE_EXISTS if there is a duplicate space_id
150 m_is_valid is also set true on success, else false. */
151 [[nodiscard]] dberr_t validate_for_recovery(
152 space_id_t space_id, uint32_t expected_physical_page_size);
153
156 }
157
158 /** Get Datafile::m_filepath.
159 @return m_filepath */
160 const char *filepath() const { return (m_filepath); }
161
162 /** Get Datafile::m_handle.
163 @return m_handle */
165 ut_ad(is_open());
166 return (m_handle);
167 }
168
169 /** Returns cached server version read from the first page during validation.
170 */
171 uint32_t get_cached_server_version() const {
174 }
175
176 /** Returns cached space version read from the first page during validation.
177 */
178 uint32_t get_cached_space_version() const {
181 }
182
183 /** Returns cached space ID read from the first page during validation. */
187 }
188
189 /** Returns cached space FSP flags read from the first page during validation.
190 */
191 uint32_t get_cached_space_flags() const {
194 }
195
196 /** Returns cached space flush LSN read from the first page during validation.
197 */
198 uint32_t get_cached_space_flush_lsn() const {
201 }
202
203 /** Get the physical page size used by the tablespace. */
204 size_t physical_page_size() const {
205 return page_size_t{get_cached_space_flags()}.physical();
206 }
207
208 /**
209 @return true if m_handle is open, false if not */
210 bool is_open() const { return (m_handle.m_file != OS_FILE_CLOSED); }
211
212 /** Get Datafile::m_is_valid.
213 @return m_is_valid */
214 bool is_valid() const { return (m_is_valid); }
215
216 /** @return file size in number of pages */
217 page_no_t size() const { return (m_size); }
218
219 private:
220 /** Checks the consistency of the first page supplied when the tablespace is
221 opened, and verifies the space is not already added to the `fil` mappings with
222 a different datafile path. m_is_valid is set true on success, else false. The
223 datafile will not be attempted to be opened.
224 @param[in] page The content of the first page of the tablespace.
225 @param[in] space_id Expected space ID
226 @param[in] filepath The file path to the first node in the
227 tablespace, used for error message printing.
228 @param[in] for_import if it is for importing
229 @retval DB_WRONG_FILE_NAME tablespace in file header doesn't match
230 expected value
231 @retval DB_SUCCESS on if the datafile is valid
232 @retval DB_CORRUPTION if the datafile is not readable
233 @retval DB_INVALID_ENCRYPTION_META if the encryption meta data
234 is not readable
235 @retval DB_TABLESPACE_EXISTS if there is a duplicate space_id */
236 [[nodiscard]] dberr_t validate_first_page(const byte *page,
237 space_id_t space_id,
238 const std::string &filepath,
239 bool for_import);
240
241 /** Free the filepath buffer. */
242 void free_filepath();
243
244 /** Set the filename pointer to the start of the file name
245 in the filepath. */
247 if (m_filepath == nullptr) {
248 return;
249 }
250
251 char *last_slash = strrchr(m_filepath, OS_PATH_SEPARATOR);
252
253 m_filename = last_slash ? last_slash + 1 : m_filepath;
254 }
255
256 /** Reads the first page of the datafile. The datafile must be already opened.
257 @param[in] space_id Expected space ID
258 @param[in] physical_page_size Physical page size of the tablespace.
259 @return DB_SUCCESS or DB_IO_ERROR if page cannot be read */
261 space_id_t space_id, uint32_t physical_page_size);
262
263 private:
264 /** Points into m_filepath to the file name with extension */
265 char *m_filename{};
266
267 /** Open file handle */
269
270 /** size in pages */
272
273 /** The type of the data file */
275
276 /** Cache of fields extracted from the first page. */
278 /** Tablespace ID. Contained in the datafile header.
279 If this is a system tablespace, FSP_SPACE_ID is only valid
280 in the first datafile. */
282
283 /** Server version */
285
286 /** Space version */
287 uint32_t m_space_version{};
288
289 /** Tablespace flags. Contained in the datafile header.
290 If this is a system tablespace, FSP_SPACE_FLAGS are only valid
291 in the first datafile. */
292 uint32_t m_space_flags{};
293
294 /** Flush LSN stored in the tablespace header. */
296
297 /** Specifies if the cache values have already values assigned. */
299 };
300
301 /** Cache of fields extracted from the first page. */
303
304 /** true if file already existed on startup */
305 bool m_exists{};
306
307 /* true if the tablespace is valid */
309
310 protected:
311 /** Physical file path with base name and extension */
312 char *m_filepath{};
313
314 public:
315 /** Encryption key read from first page */
317
318 /** Encryption iv read from first page */
320
321 /** Encryption operation in progress */
323
324 /** Master key id read from first page */
326};
327#endif /* fsp0file_h */
uint32_t space_id_t
Tablespace identifier.
Definition: api0api.h:49
uint32_t page_no_t
Page number.
Definition: api0api.h:47
Data file control information.
Definition: fsp0file.h:72
uint32_t get_cached_server_version() const
Returns cached server version read from the first page during validation.
Definition: fsp0file.h:171
Datafile()
Definition: fsp0file.h:76
space_id_t get_cached_space_id() const
Returns cached space ID read from the first page during validation.
Definition: fsp0file.h:184
dberr_t validate_for_recovery(space_id_t space_id, uint32_t expected_physical_page_size)
Validates this datafile for the purpose of recovery.
Definition: fsp0file.cc:307
ut::Expected< ut::unique_ptr_aligned< byte[]> > read_first_page(space_id_t space_id, uint32_t physical_page_size)
Reads the first page of the datafile.
Definition: fsp0file.cc:154
byte * m_encryption_iv
Encryption iv read from first page.
Definition: fsp0file.h:319
byte * m_encryption_key
Encryption key read from first page.
Definition: fsp0file.h:316
bool is_valid() const
Get Datafile::m_is_valid.
Definition: fsp0file.h:214
const char * filepath() const
Get Datafile::m_filepath.
Definition: fsp0file.h:160
bool m_exists
true if file already existed on startup
Definition: fsp0file.h:305
bool is_raw_type()
Returns if the Datafile is created in raw partition.
Definition: fsp0file.h:103
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:112
dberr_t validate_first_page(const byte *page, space_id_t space_id, const std::string &filepath, bool for_import)
Checks the consistency of the first page supplied when the tablespace is opened, and verifies the spa...
Definition: fsp0file.cc:352
void reset_first_page_fields_cache()
Definition: fsp0file.h:154
node_device_type_t m_type
The type of the data file.
Definition: fsp0file.h:274
dberr_t validate_to_dd(const byte *page, space_id_t space_id, uint32_t flags, const std::string &filepath, bool for_import)
Validates the first page of tablespace supplied and checks that it conforms with the expected space I...
Definition: fsp0file.cc:234
void set_filename()
Set the filename pointer to the start of the file name in the filepath.
Definition: fsp0file.h:246
page_no_t m_size
size in pages
Definition: fsp0file.h:271
pfs_os_file_t handle() const
Get Datafile::m_handle.
Definition: fsp0file.h:164
void shutdown()
Release the resources.
Definition: fsp0file.cc:54
~Datafile()
Definition: fsp0file.h:80
uint32_t get_cached_space_flush_lsn() const
Returns cached space flush LSN read from the first page during validation.
Definition: fsp0file.h:198
Datafile & operator=(const Datafile &file)=delete
void extract_fields_from_first_page(const byte *page)
Extracts basic space fields from the first page header.
Definition: fsp0file.cc:223
dberr_t open_read_only()
Open a data file in read-only mode to check if it exists so that it can be validated.
Definition: fsp0file.cc:69
void free_filepath()
Free the filepath buffer.
Definition: fsp0file.cc:146
size_t physical_page_size() const
Get the physical page size used by the tablespace.
Definition: fsp0file.h:204
pfs_os_file_t m_handle
Open file handle.
Definition: fsp0file.h:268
bool is_open() const
Definition: fsp0file.h:210
uint32_t m_encryption_master_key_id
Master key id read from first page.
Definition: fsp0file.h:325
void set_filepath(const char *filepath)
Set the filepath by duplicating the filepath sent in.
Definition: fsp0file.cc:137
dberr_t close()
Close a data file.
Definition: fsp0file.cc:94
uint32_t get_cached_space_flags() const
Returns cached space FSP flags read from the first page during validation.
Definition: fsp0file.h:191
Encryption::Progress m_encryption_op_in_progress
Encryption operation in progress.
Definition: fsp0file.h:322
char * m_filepath
Physical file path with base name and extension.
Definition: fsp0file.h:312
first_page_fields_cache_t m_first_page_fields_cache
Cache of fields extracted from the first page.
Definition: fsp0file.h:302
friend class SysTablespace
Definition: fsp0file.h:73
Datafile(const Datafile &file)=delete
char * m_filename
Points into m_filepath to the file name with extension.
Definition: fsp0file.h:265
uint32_t get_cached_space_version() const
Returns cached space version read from the first page during validation.
Definition: fsp0file.h:178
bool m_is_valid
Definition: fsp0file.h:308
page_no_t size() const
Definition: fsp0file.h:217
Progress
Encryption progress type.
Definition: os0enc.h:80
Page size descriptor.
Definition: page0size.h:50
C++23 std::expected.
Definition: ut0expected.h:74
int page
Definition: ctype-mb.cc:1226
dberr_t
Definition: db0err.h:39
The low-level file system.
ib_file_suffix
Common InnoDB file extensions.
Definition: fil0fil.h:943
node_device_type_t
Types of nodes specifed in innodb_data_file_path.
Definition: fsp0file.h:59
@ NEW_RAW
A 'newraw' partition, only to be initialized.
@ OLD_RAW
An initialized raw partition.
@ REGULAR_FILE
Not a raw partition, a regular file.
Prototypes for global functions in ha_innodb.cc that are called by InnoDB C code.
static int flags[50]
Definition: hp_test1.cc:40
uint64_t lsn_t
Type used for all log sequence number storage and arithmetic.
Definition: log0types.h:63
#define OS_PATH_SEPARATOR
Definition: log_sink_syseventlog.cc:94
The memory management.
Definition: os0file.h:89
int key_type
Definition: method.h:38
Json_data_extension ext
Definition: backend.cc:50
The interface to the operating system file io.
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
Cache of fields extracted from the first page.
Definition: fsp0file.h:277
lsn_t m_flush_lsn
Flush LSN stored in the tablespace header.
Definition: fsp0file.h:295
uint32_t m_server_version
Server version.
Definition: fsp0file.h:284
space_id_t m_space_id
Tablespace ID.
Definition: fsp0file.h:281
uint32_t m_space_flags
Tablespace flags.
Definition: fsp0file.h:292
uint32_t m_space_version
Space version.
Definition: fsp0file.h:287
bool m_is_valid
Specifies if the cache values have already values assigned.
Definition: fsp0file.h:298
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
constexpr space_id_t SPACE_UNKNOWN
Unknown space id.
Definition: univ.i:452
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:109
#define ut_a(EXPR)
Abort execution if EXPR does not evaluate to nonzero.
Definition: ut0dbg.h:97