Debian Codegraph @

storage/innobase/include/fsp0file.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 2013, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file include/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
48 the 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. */
54 extern 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 */
59 enum device_t {
60
61 /** Not a raw partition */
62 SRV_NOT_RAW = 0,
63
64 /** A 'newraw' partition, only to be initialized */
65 SRV_NEW_RAW,
66
67 /** An initialized raw partition */
68 SRV_OLD_RAW
69 };
70
71 /** Data file control information. */
72 class Datafile {
73 friend class Tablespace;
74 friend class SysTablespace;
75
76 public:
77 Datafile()
78 : m_name(),
79 m_filename(),
80 m_open_flags(OS_FILE_OPEN),
81 m_size(),
82 m_order(),
83 m_type(SRV_NOT_RAW),
84 m_space_id(SPACE_UNKNOWN),
85 m_flags(),
86 m_exists(),
87 m_is_valid(),
88 m_first_page(),
89 m_atomic_write(),
90 m_filepath(),
91 m_last_os_error(),
92 m_file_info(),
93 m_encryption_key(),
94 m_encryption_iv(),
95 m_encryption_op_in_progress(Encryption::Progress::NONE),
96 m_encryption_master_key_id(0) {
97 m_handle.m_file = OS_FILE_CLOSED;
98 }
99
100 Datafile(const char *name, uint32_t flags, page_no_t size, ulint order)
101 : m_name(mem_strdup(name)),
102 m_filename(),
103 m_open_flags(OS_FILE_OPEN),
104 m_size(size),
105 m_order(order),
106 m_type(SRV_NOT_RAW),
107 m_space_id(SPACE_UNKNOWN),
108 m_flags(flags),
109 m_exists(),
110 m_is_valid(),
111 m_first_page(),
112 m_atomic_write(),
113 m_filepath(),
114 m_last_os_error(),
115 m_file_info(),
116 m_encryption_key(),
117 m_encryption_iv(),
118 m_encryption_op_in_progress(Encryption::Progress::NONE),
119 m_encryption_master_key_id(0) {
120 ut_ad(m_name != nullptr);
121 m_handle.m_file = OS_FILE_CLOSED;
122 /* No op */
123 }
124
125 Datafile(const Datafile &file)
126 : m_handle(file.m_handle),
127 m_open_flags(file.m_open_flags),
128 m_size(file.m_size),
129 m_order(file.m_order),
130 m_type(file.m_type),
131 m_space_id(file.m_space_id),
132 m_flags(file.m_flags),
133 m_exists(file.m_exists),
134 m_is_valid(file.m_is_valid),
135 m_first_page(),
136 m_atomic_write(file.m_atomic_write),
137 m_last_os_error(),
138 m_file_info(),
139 m_encryption_key(),
140 m_encryption_iv(),
141 m_encryption_op_in_progress(Encryption::Progress::NONE),
142 m_encryption_master_key_id(0) {
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
156 ~Datafile() { shutdown(); }
157
158 Datafile &operator=(const Datafile &file) {
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
169 ut_a(m_handle.m_file == OS_FILE_CLOSED);
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) {
180 ut::free(m_filepath);
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;
196 m_encryption_op_in_progress = Encryption::Progress::NONE;
197 m_encryption_master_key_id = 0;
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,
246 ib_file_suffix ext);
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. */
279 [[nodiscard]] dberr_t validate_for_recovery(space_id_t space_id);
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 */
297 [[nodiscard]] dberr_t validate_first_page(space_id_t space_id,
298 lsn_t *flush_lsn, bool for_import);
299
300 /** Get LSN of first page */
301 lsn_t get_flush_lsn() {
302 ut_ad(m_first_page != nullptr);
303 return mach_read_from_8(m_first_page + FIL_PAGE_LSN);
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 */
316 pfs_os_file_t handle() const {
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 */
327 ulint server_version() const { 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. */
370 dberr_t find_space_id();
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 */
378 void set_space_id(space_id_t space_id) {
379 ut_ad(space_id <= 0xFFFFFFFFU);
380 m_space_id = space_id;
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. */
394 void set_filename() {
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. */
420 void set_open_flags(os_file_create_t open_flags) {
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 */
428 dberr_t restore_from_doublewrite(page_no_t restore_page_no);
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 */
438 char *m_filename;
439
440 /** Open file handle */
441 pfs_os_file_t m_handle;
442
443 /** Flags to use for opening the data file */
444 os_file_create_t m_open_flags;
445
446 /** size in pages */
447 page_no_t m_size;
448
449 /** ordinal position of this datafile in the tablespace */
450 ulint m_order;
451
452 /** The type of the data file */
453 device_t m_type;
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. */
458 space_id_t m_space_id;
459
460 /** Server version */
461 uint32_t m_server_version;
462
463 /** Space version */
464 uint32_t m_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 */
472 bool m_exists;
473
474 /* true if the tablespace is valid */
475 bool m_is_valid;
476
477 /** Buffer to hold first page */
478 byte *m_first_page;
479
480 /** true if atomic writes enabled for this file */
481 bool m_atomic_write;
482
483 protected:
484 /** Physical file path with base name and extension */
485 char *m_filepath;
486
487 /** Last OS error received so it can be reported if needed. */
488 ulint m_last_os_error;
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 */
503 byte *m_encryption_key;
504
505 /** Encryption iv read from first page */
506 byte *m_encryption_iv;
507
508 /** Encryption operation in progress */
509 Encryption::Progress m_encryption_op_in_progress;
510
511 /** Master key id read from first page */
512 uint32_t m_encryption_master_key_id;
513 };
514 #endif /* fsp0file_h */