Debian Codegraph @

storage/innobase/row/row0quiesce.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 2012, 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 row/row0quiesce.cc
29 Quiesce a tablespace.
30
31 Created 2012-02-08 by Sunny Bains.
32 *******************************************************/
33
34 #include <errno.h>
35 #include <my_aes.h>
36
37 #include "dd/cache/dictionary_client.h"
38 #include "sql/dd/dictionary.h"
39 #include "sql/dd/types/column_type_element.h"
40
41 #include "dict0dd.h"
42 #include "fsp0sysspace.h"
43 #include "ha_prototypes.h"
44 #include "ibuf0ibuf.h"
45 #include "row0mysql.h"
46 #include "row0quiesce.h"
47 #include "srv0start.h"
48 #include "trx0purge.h"
49
50 #include "my_dbug.h"
51
52 /** Write the meta data (index user fields) config file.
53 @return DB_SUCCESS or error code. */
54 [[nodiscard]] static dberr_t row_quiesce_write_index_fields(
55 const dict_index_t *index, /*!< in: write the meta data for
56 this index */
57 FILE *file, /*!< in: file to write to */
58 THD *thd) /*!< in/out: session */
59 {
60 /* This row will store prefix_len, fixed_len,
61 and in IB_EXPORT_CFG_VERSION_V4, is_ascending */
62 byte row[sizeof(uint32_t) * 3];
63 size_t row_len = sizeof(row);
64
65 for (ulint i = 0; i < index->n_fields; ++i) {
66 byte *ptr = row;
67 const dict_field_t *field = &index->fields[i];
68
69 mach_write_to_4(ptr, field->prefix_len);
70 ptr += sizeof(uint32_t);
71
72 mach_write_to_4(ptr, field->fixed_len);
73 ptr += sizeof(uint32_t);
74
75 /* In IB_EXPORT_CFG_VERSION_V4 we also write the is_ascending boolean. */
76 mach_write_to_4(ptr, field->is_ascending);
77
78 DBUG_EXECUTE_IF("ib_export_use_cfg_version_3",
79 row_len = sizeof(uint32_t) * 2;);
80
81 DBUG_EXECUTE_IF("ib_export_io_write_failure_9", close(fileno(file)););
82
83 if (fwrite(row, 1, row_len, file) != row_len) {
84 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
85 strerror(errno), "while writing index fields.");
86
87 return (DB_IO_ERROR);
88 }
89
90 /* Include the NUL byte in the length. */
91 uint32_t len = static_cast<uint32_t>(strlen(field->name) + 1);
92 ut_a(len > 1);
93
94 mach_write_to_4(row, len);
95
96 DBUG_EXECUTE_IF("ib_export_io_write_failure_10", close(fileno(file)););
97
98 if (fwrite(row, 1, sizeof(len), file) != sizeof(len) ||
99 fwrite(field->name, 1, len, file) != len) {
100 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
101 strerror(errno), "while writing index column.");
102
103 return (DB_IO_ERROR);
104 }
105 }
106
107 return (DB_SUCCESS);
108 }
109 /** Write the meta data config file index information
110 @param[in] index write metadata for this index
111 @param[in,out] file file to write to
112 @param[in,out] thd session
113 @return DB_SUCCESS or error code. */
114 [[nodiscard]] static dberr_t row_quiesce_write_one_index(
115 const dict_index_t *index, FILE *file, THD *thd) {
116 dberr_t err;
117 byte *ptr;
118 byte row[sizeof(space_index_t) + sizeof(uint32_t) * 8];
119
120 ptr = row;
121
122 static_assert(sizeof(space_index_t) == 8);
123 mach_write_to_8(ptr, index->id);
124 ptr += sizeof(space_index_t);
125
126 mach_write_to_4(ptr, index->space);
127 ptr += sizeof(uint32_t);
128
129 mach_write_to_4(ptr, index->page);
130 ptr += sizeof(uint32_t);
131
132 mach_write_to_4(ptr, index->type);
133 ptr += sizeof(uint32_t);
134
135 mach_write_to_4(ptr, index->trx_id_offset);
136 ptr += sizeof(uint32_t);
137
138 mach_write_to_4(ptr, index->n_user_defined_cols);
139 ptr += sizeof(uint32_t);
140
141 mach_write_to_4(ptr, index->n_uniq);
142 ptr += sizeof(uint32_t);
143
144 mach_write_to_4(ptr, index->n_nullable);
145 ptr += sizeof(uint32_t);
146
147 mach_write_to_4(ptr, index->n_fields);
148
149 DBUG_EXECUTE_IF("ib_export_io_write_failure_12", close(fileno(file)););
150
151 if (fwrite(row, 1, sizeof(row), file) != sizeof(row)) {
152 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
153 strerror(errno), "while writing index meta-data.");
154
155 return (DB_IO_ERROR);
156 }
157
158 /* Write the length of the index name.
159 NUL byte is included in the length. */
160 uint32_t len = static_cast<uint32_t>(strlen(index->name) + 1);
161 ut_a(len > 1);
162
163 mach_write_to_4(row, len);
164
165 DBUG_EXECUTE_IF("ib_export_io_write_failure_1", close(fileno(file)););
166
167 if (fwrite(row, 1, sizeof(len), file) != sizeof(len) ||
168 fwrite(index->name, 1, len, file) != len) {
169 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
170 strerror(errno), "while writing index name.");
171
172 return (DB_IO_ERROR);
173 }
174
175 err = row_quiesce_write_index_fields(index, file, thd);
176 return (err);
177 }
178
179 /** Write the meta data config file index information.
180 @return DB_SUCCESS or error code. */
181 [[nodiscard]] static MY_ATTRIBUTE((nonnull)) dberr_t
182 row_quiesce_write_indexes(const dict_table_t *table, /*!< in: write the meta
183 data for this table */
184 FILE *file, /*!< in: file to write to */
185 THD *thd) /*!< in/out: session */
186 {
187 byte row[sizeof(uint32_t)];
188
189 /* Write the number of indexes in the table. */
190 uint32_t num_indexes = 0;
191 uint32_t flags = fil_space_get_flags(table->space);
192 bool has_sdi = FSP_FLAGS_HAS_SDI(flags);
193
194 if (has_sdi) {
195 num_indexes += 1;
196 }
197
198 num_indexes += static_cast<uint32_t>(UT_LIST_GET_LEN(table->indexes));
199 ut_ad(num_indexes != 0);
200
201 mach_write_to_4(row, num_indexes);
202
203 DBUG_EXECUTE_IF("ib_export_io_write_failure_11", close(fileno(file)););
204
205 if (fwrite(row, 1, sizeof(row), file) != sizeof(row)) {
206 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
207 strerror(errno), "while writing index count.");
208
209 return (DB_IO_ERROR);
210 }
211
212 /* Write SDI Index */
213 if (has_sdi) {
214 dict_mutex_enter_for_mysql();
215
216 dict_index_t *index = dict_sdi_get_index(table->space);
217
218 dict_mutex_exit_for_mysql();
219
220 ut_ad(index != nullptr);
221 const auto err = row_quiesce_write_one_index(index, file, thd);
222 if (err != DB_SUCCESS) {
223 return err;
224 }
225 }
226
227 /* Write the table indexes meta data. */
228 for (const dict_index_t *index : table->indexes) {
229 const auto err = row_quiesce_write_one_index(index, file, thd);
230 if (err != DB_SUCCESS) {
231 return err;
232 }
233 }
234
235 return DB_SUCCESS;
236 }
237
238 /** Write the metadata (table columns) config file. Serialise the contents
239 of dict_col_t default value part if exists.
240 @param[in] col column to which the default value belongs
241 @param[in] col_name column name
242 @param[in,out] file file to write to
243 @param[in] dict_table InnoDB table cache
244 @param[in] thd THD
245 @return DB_SUCCESS or DB_IO_ERROR. */
246 static dberr_t row_quiesce_write_dropped_col_metadata(
247 const dict_col_t *col, const char *col_name, FILE *file,
248 const dict_table_t *dict_table, THD *thd) {
249 /* Total metadata to be written
250 1 byte for is NULLABLE
251 1 byte for is_unsigned
252 4 bytes for char_length
253 4 bytes for column type
254 4 bytes for numeric scale
255 8 bytes for collation id */
256 constexpr size_t METADATA_SIZE = 22;
257
258 byte row[METADATA_SIZE];
259
260 ut_ad(col->is_instant_dropped());
261
262 const dd::Table *dd_table = nullptr;
263 dd::cache::Dictionary_client *client = dd::get_dd_client(thd);
264 dd::cache::Dictionary_client::Auto_releaser releaser(client);
265
266 /* Find schema and table name */
267 std::string dict_name(dict_table->name.m_name);
268 std::string schema_name;
269 std::string table_name;
270 size_t table_begin = dict_name.find(dict_name::SCHEMA_SEPARATOR);
271 /* schema name must be part of table name */
272 ut_ad(table_begin != std::string::npos);
273
274 schema_name.assign(dict_name.substr(0, table_begin));
275 table_begin += dict_name::SCHEMA_SEPARATOR_LEN;
276
277 /* If partition table, skip partition name. */
278 size_t part_name_begin = dict_name.find(dict_name::PART_SEPARATOR);
279 if (part_name_begin != std::string::npos) {
280 table_name.assign(dict_name, table_begin, part_name_begin - table_begin);
281 } else {
282 table_name.assign(dict_name.substr(table_begin));
283 }
284
285 if (client->acquire(dd::String_type(schema_name.c_str()),
286 dd::String_type(table_name.c_str()), &dd_table)) {
287 ut_ad(false);
288 return (DB_ERROR);
289 }
290
291 if (dd_table == nullptr) {
292 ut_ad(false);
293 return DB_ERROR;
294 }
295
296 const dd::Column *column = dd_find_column(dd_table, col_name);
297 ut_ad(column != nullptr);
298
299 byte *ptr = row;
300
301 /* 1 byte for is NULLABLE */
302 mach_write_to_1(ptr, column->is_nullable());
303 ptr++;
304
305 /* 1 byte for is_unsigned */
306 mach_write_to_1(ptr, column->is_unsigned());
307 ptr++;
308
309 /* 4 bytes for char_length() */
310 mach_write_to_4(ptr, column->char_length());
311 ptr += 4;
312
313 /* 4 bytes for column type */
314 mach_write_to_4(ptr, (uint32_t)column->type());
315 ptr += 4;
316
317 /* 4 bytes for numeric scale */
318 mach_write_to_4(ptr, column->numeric_scale());
319 ptr += 4;
320
321 /* 8 bytes for collation id */
322 mach_write_to_8(ptr, column->collation_id());
323 ptr += 8;
324
325 if (fwrite(row, 1, sizeof(row), file) != sizeof(row)) {
326 return (DB_IO_ERROR);
327 }
328
329 /* Write elements for enum column type.
330 [4] bytes : number of elements
331 For each element
332 [4] bytes : element name length (len+1)
333 [len+1] bytes : element name */
334 if (column->type() == dd::enum_column_types::ENUM ||
335 column->type() == dd::enum_column_types::SET) {
336 byte _row[sizeof(uint32_t)];
337
338 /* Write element count */
339 mach_write_to_4(_row, column->elements().size());
340 if (fwrite(_row, 1, sizeof(_row), file) != sizeof(_row)) {
341 ib_senderrf(
342 thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno, strerror(errno),
343 "while writing enum column element count for column %s.", col_name);
344
345 return (DB_IO_ERROR);
346 }
347
348 /* Write out the enum/set column element name as [len, byte array]. */
349 for (const auto *source_elem : column->elements()) {
350 const char *elem_name = source_elem->name().c_str();
351 uint32_t len = strlen(elem_name) + 1;
352 ut_a(len > 1);
353
354 mach_write_to_4(_row, len);
355
356 if (fwrite(_row, 1, sizeof(len), file) != sizeof(len) ||
357 fwrite(elem_name, 1, len, file) != len) {
358 ib_senderrf(
359 thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno, strerror(errno),
360 "while writing enum column element name for column %s.", col_name);
361
362 return (DB_IO_ERROR);
363 }
364 }
365 }
366
367 return (DB_SUCCESS);
368 }
369
370 /** Write the metadata (table columns) config file. Serialise the contents
371 of dict_col_t default value part if exists.
372 @param[in] col column to which the default value belongs
373 @param[in] file file to write to
374 @return DB_SUCCESS or DB_IO_ERROR. */
375 [[nodiscard]] static dberr_t row_quiesce_write_default_value(
376 const dict_col_t *col, FILE *file) {
377 byte row[6];
378
379 if (col->instant_default != nullptr) {
380 bool null = (col->instant_default->len == UNIV_SQL_NULL);
381 uint8_t to_write = 2;
382
383 mach_write_to_1(&row[0], 1);
384 mach_write_to_1(&row[1], null);
385
386 if (!null) {
387 mach_write_to_4(&row[2], col->instant_default->len);
388 to_write += 4;
389 }
390
391 if (fwrite(row, 1, to_write, file) != to_write ||
392 (!null &&
393 fwrite(col->instant_default->value, 1, col->instant_default->len,
394 file) != col->instant_default->len)) {
395 return (DB_IO_ERROR);
396 }
397 } else {
398 row[0] = 0;
399
400 if (fwrite(row, 1, 1, file) != 1) {
401 return (DB_IO_ERROR);
402 }
403 }
404
405 return (DB_SUCCESS);
406 }
407
408 /** Write the meta data (table columns) config file. Serialise the contents of
409 dict_col_t structure, along with the column name. All fields are serialized
410 as uint32_t.
411 @return DB_SUCCESS or error code. */
412 [[nodiscard]] static dberr_t row_quiesce_write_table(
413 const dict_table_t *table, /*!< in: write the meta
414 data for this table */
415 FILE *file, /*!< in: file to write to */
416 THD *thd) /*!< in/out: session */
417 {
418 dict_col_t *col;
419 byte row[sizeof(uint32_t) * 7];
420
421 col = table->cols;
422
423 for (ulint i = 0; i < table->get_total_cols(); ++i, ++col) {
424 byte *ptr = row;
425
426 mach_write_to_4(ptr, col->prtype);
427 ptr += sizeof(uint32_t);
428
429 mach_write_to_4(ptr, col->mtype);
430 ptr += sizeof(uint32_t);
431
432 mach_write_to_4(ptr, col->len);
433 ptr += sizeof(uint32_t);
434
435 mach_write_to_4(ptr, col->mbminmaxlen);
436 ptr += sizeof(uint32_t);
437
438 mach_write_to_4(ptr, col->ind);
439 ptr += sizeof(uint32_t);
440
441 mach_write_to_4(ptr, col->ord_part);
442 ptr += sizeof(uint32_t);
443
444 mach_write_to_4(ptr, col->max_prefix);
445
446 DBUG_EXECUTE_IF("ib_export_io_write_failure_2", close(fileno(file)););
447
448 if (fwrite(row, 1, sizeof(row), file) != sizeof(row)) {
449 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
450 strerror(errno), "while writing table column data.");
451
452 return (DB_IO_ERROR);
453 }
454
455 /* Write out the column name as [len, byte array]. The len
456 includes the NUL byte. */
457 uint32_t len;
458 const char *col_name;
459
460 col_name = table->get_col_name(dict_col_get_no(col));
461
462 /* Include the NUL byte in the length. */
463 len = static_cast<uint32_t>(strlen(col_name) + 1);
464 ut_a(len > 1);
465
466 mach_write_to_4(row, len);
467
468 DBUG_EXECUTE_IF("ib_export_io_write_failure_3", close(fileno(file)););
469
470 if (fwrite(row, 1, sizeof(len), file) != sizeof(len) ||
471 fwrite(col_name, 1, len, file) != len) {
472 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
473 strerror(errno), "while writing column name.");
474
475 return (DB_IO_ERROR);
476 }
477
478 /* Write column's INSTANT metadata */
479 uint32_t cfg_version = IB_EXPORT_CFG_VERSION_V7;
480 DBUG_EXECUTE_IF("ib_export_use_cfg_version_3",
481 cfg_version = IB_EXPORT_CFG_VERSION_V3;);
482 DBUG_EXECUTE_IF("ib_export_use_cfg_version_99",
483 cfg_version = IB_EXPORT_CFG_VERSION_V99;);
484 if (cfg_version >= IB_EXPORT_CFG_VERSION_V7) {
485 byte row[2 + sizeof(uint32_t)];
486 byte *ptr = row;
487
488 /* version added */
489 byte value =
490 col->is_instant_added() ? col->get_version_added() : UINT8_UNDEFINED;
491 mach_write_to_1(ptr, value);
492 ptr++;
493
494 /* version dropped */
495 value = col->is_instant_dropped() ? col->get_version_dropped()
496 : UINT8_UNDEFINED;
497 mach_write_to_1(ptr, value);
498 ptr++;
499
500 /* physical position */
501 mach_write_to_4(ptr, col->get_phy_pos());
502
503 if (fwrite(row, 1, sizeof(row), file) != sizeof(row)) {
504 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
505 strerror(errno),
506 "while writing table column instant metadata.");
507 return (DB_IO_ERROR);
508 }
509
510 /* Write DD::Column specific info for dropped columns */
511 if (col->is_instant_dropped()) {
512 const char *col_name = table->get_col_name(dict_col_get_no(col));
513 if (row_quiesce_write_dropped_col_metadata(col, col_name, file, table,
514 thd) != DB_SUCCESS) {
515 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
516 strerror(errno),
517 "while writing dropped column metadata.");
518 return (DB_IO_ERROR);
519 }
520 }
521 }
522
523 if (row_quiesce_write_default_value(col, file) != DB_SUCCESS) {
524 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
525 strerror(errno), "while writing table column default.");
526 return (DB_IO_ERROR);
527 }
528 }
529
530 return (DB_SUCCESS);
531 }
532
533 /** Write the meta data config file header.
534 @param[in] table write the meta data for this table
535 @param[in] file file to write to
536 @param[in,out] thd session
537 @return DB_SUCCESS or error code. */
538 [[nodiscard]] static dberr_t row_quiesce_write_header(const dict_table_t *table,
539 FILE *file, THD *thd) {
540 byte value[sizeof(uint32_t)];
541
542 /* Write the current meta-data version number. */
543 uint32_t cfg_version = IB_EXPORT_CFG_VERSION_V7;
544 DBUG_EXECUTE_IF("ib_export_use_cfg_version_3",
545 cfg_version = IB_EXPORT_CFG_VERSION_V3;);
546 DBUG_EXECUTE_IF("ib_export_use_cfg_version_99",
547 cfg_version = IB_EXPORT_CFG_VERSION_V99;);
548 mach_write_to_4(value, cfg_version);
549
550 DBUG_EXECUTE_IF("ib_export_io_write_failure_4", close(fileno(file)););
551
552 if (fwrite(&value, 1, sizeof(value), file) != sizeof(value)) {
553 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
554 strerror(errno), "while writing meta-data version number.");
555
556 return (DB_IO_ERROR);
557 }
558
559 /* Write the server hostname. */
560 const char *hostname = server_get_hostname();
561
562 /* Play it safe and check for NULL. */
563 if (hostname == nullptr) {
564 static const char NullHostname[] = "Hostname unknown";
565
566 ib::warn(ER_IB_MSG_1013) << "Unable to determine server hostname.";
567
568 hostname = NullHostname;
569 }
570
571 /* The server hostname includes the NUL byte. */
572 uint32_t len = static_cast<uint32_t>(strlen(hostname) + 1);
573 mach_write_to_4(value, len);
574
575 DBUG_EXECUTE_IF("ib_export_io_write_failure_5", close(fileno(file)););
576
577 if (fwrite(&value, 1, sizeof(value), file) != sizeof(value) ||
578 fwrite(hostname, 1, len, file) != len) {
579 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
580 strerror(errno), "while writing hostname.");
581
582 return (DB_IO_ERROR);
583 }
584
585 /* The table name includes the NUL byte. */
586 ut_a(table->name.m_name != nullptr);
587 len = static_cast<uint32_t>(strlen(table->name.m_name) + 1);
588
589 /* Write the table name. */
590 mach_write_to_4(value, len);
591
592 DBUG_EXECUTE_IF("ib_export_io_write_failure_6", close(fileno(file)););
593
594 if (fwrite(&value, 1, sizeof(value), file) != sizeof(value) ||
595 fwrite(table->name.m_name, 1, len, file) != len) {
596 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
597 strerror(errno), "while writing table name.");
598
599 return (DB_IO_ERROR);
600 }
601
602 byte row[sizeof(uint32_t) * 3];
603
604 /* Write the next autoinc value. */
605 mach_write_to_8(row, table->autoinc);
606
607 DBUG_EXECUTE_IF("ib_export_io_write_failure_7", close(fileno(file)););
608
609 if (fwrite(row, 1, sizeof(uint64_t), file) != sizeof(uint64_t)) {
610 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
611 strerror(errno), "while writing table autoinc value.");
612
613 return (DB_IO_ERROR);
614 }
615
616 byte *ptr = row;
617
618 /* Write the system page size. */
619 mach_write_to_4(ptr, UNIV_PAGE_SIZE);
620 ptr += sizeof(uint32_t);
621
622 /* Write the table->flags. */
623 mach_write_to_4(ptr, table->flags);
624 ptr += sizeof(uint32_t);
625
626 /* Write the number of columns in the table. In case of INSTANT, include
627 dropped columns as well. */
628 mach_write_to_4(ptr, table->get_total_cols());
629
630 DBUG_EXECUTE_IF("ib_export_io_write_failure_8", close(fileno(file)););
631
632 if (fwrite(row, 1, sizeof(row), file) != sizeof(row)) {
633 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
634 strerror(errno), "while writing table meta-data.");
635
636 return (DB_IO_ERROR);
637 }
638
639 if (cfg_version >= IB_EXPORT_CFG_VERSION_V5) {
640 /* write number of nullable column before first instant column */
641 mach_write_to_4(value, table->first_index()->get_instant_nullable());
642
643 if (fwrite(&value, 1, sizeof(value), file) != sizeof(value)) {
644 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
645 strerror(errno), "while writing table meta-data.");
646
647 return (DB_IO_ERROR);
648 }
649 }
650
651 /* Write table instant metadata */
652 if (cfg_version >= IB_EXPORT_CFG_VERSION_V7) {
653 byte row[sizeof(uint32_t) * 5];
654 byte *ptr = row;
655
656 /* Write initial column count */
657 mach_write_to_4(ptr, table->initial_col_count);
658 ptr += sizeof(uint32_t);
659
660 /* Write current column count */
661 mach_write_to_4(ptr, table->current_col_count);
662 ptr += sizeof(uint32_t);
663
664 /* Write total column count */
665 mach_write_to_4(ptr, table->total_col_count);
666 ptr += sizeof(uint32_t);
667
668 /* Write number of instantly dropped columns */
669 mach_write_to_4(ptr, table->get_n_instant_drop_cols());
670 ptr += sizeof(uint32_t);
671
672 /* Write current row version */
673 mach_write_to_4(ptr, table->current_row_version);
674
675 if (fwrite(row, 1, sizeof(row), file) != sizeof(row)) {
676 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
677 strerror(errno), "while writing table meta-data.");
678
679 return (DB_IO_ERROR);
680 }
681 }
682
683 /* Write the space flags */
684 uint32_t space_flags = fil_space_get_flags(table->space);
685 ut_ad(space_flags != UINT32_UNDEFINED);
686 mach_write_to_4(value, space_flags);
687
688 if (fwrite(&value, 1, sizeof(value), file) != sizeof(value)) {
689 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
690 strerror(errno), "while writing space_flags.");
691
692 return (DB_IO_ERROR);
693 }
694
695 if (cfg_version >= IB_EXPORT_CFG_VERSION_V6) {
696 /* Write compression type info. */
697 uint8_t compression_type =
698 static_cast<uint8_t>(fil_get_compression(table->space));
699 mach_write_to_1(value, compression_type);
700
701 if (fwrite(&value, 1, sizeof(uint8_t), file) != sizeof(uint8_t)) {
702 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
703 strerror(errno), "while writing compression type info.");
704
705 return DB_IO_ERROR;
706 }
707 }
708
709 return (DB_SUCCESS);
710 }
711
712 /** Write the table meta data after quiesce.
713 @return DB_SUCCESS or error code */
714 [[nodiscard]] static dberr_t row_quiesce_write_cfg(
715 dict_table_t *table, /*!< in: write the meta data for
716 this table */
717 THD *thd) /*!< in/out: session */
718 {
719 dberr_t err;
720 char name[OS_FILE_MAX_PATH];
721
722 dd_get_meta_data_filename(table, nullptr, name, sizeof(name));
723
724 ib::info(ER_IB_MSG_1014) << "Writing table metadata to '" << name << "'";
725
726 FILE *file = fopen(name, "w+b");
727
728 if (file == nullptr) {
729 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_CANT_CREATE_FILE, name, errno,
730 strerror(errno));
731
732 err = DB_IO_ERROR;
733 } else {
734 err = row_quiesce_write_header(table, file, thd);
735
736 if (err == DB_SUCCESS) {
737 err = row_quiesce_write_table(table, file, thd);
738 }
739
740 if (err == DB_SUCCESS) {
741 err = row_quiesce_write_indexes(table, file, thd);
742 }
743
744 if (fflush(file) != 0) {
745 char msg[BUFSIZ];
746
747 snprintf(msg, sizeof(msg), "%s flush() failed", name);
748
749 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
750 strerror(errno), msg);
751 }
752
753 if (fclose(file) != 0) {
754 char msg[BUFSIZ];
755
756 snprintf(msg, sizeof(msg), "%s flose() failed", name);
757
758 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
759 strerror(errno), msg);
760 }
761 }
762
763 return (err);
764 }
765
766 /** Write the transfer key to CFP file.
767 @param[in] encryption_metadata the encryption key and iv to store in file
768 @param[in] file file to write to
769 @param[in] thd session
770 @return DB_SUCCESS or error code. */
771 [[nodiscard]] static MY_ATTRIBUTE((nonnull)) dberr_t
772 row_quiesce_write_transfer_key(
773 const Encryption_metadata &encryption_metadata, FILE *file, THD *thd) {
774 byte key_size[sizeof(uint32_t)];
775 byte row[Encryption::KEY_LEN * 3];
776 byte *ptr = row;
777 byte *transfer_key = ptr;
778 lint elen;
779 ut_a(encryption_metadata.can_encrypt());
780
781 /* Write the encryption key size. */
782 mach_write_to_4(key_size, Encryption::KEY_LEN);
783
784 if (fwrite(&key_size, 1, sizeof(key_size), file) != sizeof(key_size)) {
785 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
786 strerror(errno), "while writing key size.");
787
788 return (DB_IO_ERROR);
789 }
790
791 /* Generate and write the transfer key. */
792 Encryption::random_value(transfer_key);
793 if (fwrite(transfer_key, 1, Encryption::KEY_LEN, file) !=
794 Encryption::KEY_LEN) {
795 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
796 strerror(errno), "while writing transfer key.");
797
798 return (DB_IO_ERROR);
799 }
800
801 ptr += Encryption::KEY_LEN;
802
803 /* Encrypt tablespace key. */
804 elen = my_aes_encrypt(encryption_metadata.m_key, Encryption::KEY_LEN, ptr,
805 transfer_key, Encryption::KEY_LEN, my_aes_256_ecb,
806 nullptr, false);
807
808 if (elen == MY_AES_BAD_DATA) {
809 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
810 strerror(errno), "while encrypt tablespace key.");
811 return (DB_ERROR);
812 }
813
814 /* Write encrypted tablespace key */
815 if (fwrite(ptr, 1, Encryption::KEY_LEN, file) != Encryption::KEY_LEN) {
816 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
817 strerror(errno), "while writing encrypted tablespace key.");
818
819 return (DB_IO_ERROR);
820 }
821 ptr += Encryption::KEY_LEN;
822
823 /* Encrypt tablespace iv. */
824 elen = my_aes_encrypt(encryption_metadata.m_iv, Encryption::KEY_LEN, ptr,
825 transfer_key, Encryption::KEY_LEN, my_aes_256_ecb,
826 nullptr, false);
827
828 if (elen == MY_AES_BAD_DATA) {
829 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
830 strerror(errno), "while encrypt tablespace iv.");
831 return (DB_ERROR);
832 }
833
834 /* Write encrypted tablespace iv */
835 if (fwrite(ptr, 1, Encryption::KEY_LEN, file) != Encryption::KEY_LEN) {
836 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
837 strerror(errno), "while writing encrypted tablespace iv.");
838
839 return (DB_IO_ERROR);
840 }
841
842 return (DB_SUCCESS);
843 }
844
845 /** Write the encryption data after quiesce.
846 @param[in] table write the data for this table
847 @param[in] thd session
848 @return DB_SUCCESS or error code */
849 [[nodiscard]] static MY_ATTRIBUTE((nonnull)) dberr_t
850 row_quiesce_write_cfp(dict_table_t *table, THD *thd) {
851 dberr_t err;
852 char name[OS_FILE_MAX_PATH];
853
854 /* If table is not encrypted, return. */
855 if (!dd_is_table_in_encrypted_tablespace(table)) {
856 return (DB_SUCCESS);
857 }
858
859 /* Get the encryption key and iv from space */
860 /* For encrypted table, before we discard the tablespace,
861 we need to save the encryption information into table, otherwise,
862 this information will be lost in fil_discard_tablespace along
863 with fil_space_free(). */
864
865 fil_space_t *space = fil_space_get(table->space);
866 ut_ad(space != nullptr && FSP_FLAGS_GET_ENCRYPTION(space->flags));
867
868 srv_get_encryption_data_filename(table, name, sizeof(name));
869
870 ib::info(ER_IB_MSG_1015) << "Writing table encryption data to '" << name
871 << "'";
872
873 FILE *file = fopen(name, "w+b");
874
875 if (file == nullptr) {
876 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_CANT_CREATE_FILE, name, errno,
877 strerror(errno));
878
879 err = DB_IO_ERROR;
880 } else {
881 err =
882 row_quiesce_write_transfer_key(space->m_encryption_metadata, file, thd);
883
884 if (fflush(file) != 0) {
885 char msg[BUFSIZ];
886
887 snprintf(msg, sizeof(msg), "%s flush() failed", name);
888
889 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
890 strerror(errno), msg);
891
892 err = DB_IO_ERROR;
893 }
894
895 if (fclose(file) != 0) {
896 char msg[BUFSIZ];
897
898 snprintf(msg, sizeof(msg), "%s flose() failed", name);
899
900 ib_senderrf(thd, IB_LOG_LEVEL_WARN, ER_IO_WRITE_ERROR, errno,
901 strerror(errno), msg);
902 err = DB_IO_ERROR;
903 }
904 }
905
906 return (err);
907 }
908
909 /** Check whether a table has an FTS index defined on it.
910 @return true if an FTS index exists on the table */
911 static bool row_quiesce_table_has_fts_index(
912 const dict_table_t *table) /*!< in: quiesce this table */
913 {
914 bool exists = false;
915
916 dict_mutex_enter_for_mysql();
917
918 for (const dict_index_t *index : table->indexes) {
919 if (index->type & DICT_FTS) {
920 exists = true;
921 break;
922 }
923 }
924
925 dict_mutex_exit_for_mysql();
926
927 return (exists);
928 }
929
930 /** Quiesce the tablespace that the table resides in.
931 @param[in] table Quiesce this table
932 @param[in,out] trx Transaction/session */
933 void row_quiesce_table_start(dict_table_t *table, trx_t *trx) {
934 ut_a(trx->mysql_thd != nullptr);
935 ut_a(srv_n_purge_threads > 0);
936 ut_ad(!srv_read_only_mode);
937
938 ut_a(trx->mysql_thd != nullptr);
939
940 ut_ad(fil_space_get(table->space) != nullptr);
941
942 ib::info(ER_IB_MSG_1016) << "Sync to disk of " << table->name << " started.";
943
944 if (trx_purge_state() != PURGE_STATE_DISABLED) {
945 trx_purge_stop();
946 }
947
948 for (ulint count = 0;
949 ibuf_merge_space(table->space) != 0 && !trx_is_interrupted(trx);
950 ++count) {
951 if (!(count % 20)) {
952 ib::info(ER_IB_MSG_1017)
953 << "Merging change buffer entries for " << table->name;
954 }
955 }
956
957 if (!trx_is_interrupted(trx)) {
958 extern ib_mutex_t master_key_id_mutex;
959
960 if (dd_is_table_in_encrypted_tablespace(table)) {
961 /* Take the mutex to make sure master_key_id doesn't change (eg: key
962 rotation). */
963 mutex_enter(&master_key_id_mutex);
964 }
965
966 buf_LRU_flush_or_remove_pages(table->space, BUF_REMOVE_FLUSH_WRITE, trx);
967
968 if (dd_is_table_in_encrypted_tablespace(table)) {
969 mutex_exit(&master_key_id_mutex);
970 }
971
972 if (trx_is_interrupted(trx)) {
973 ib::warn(ER_IB_MSG_1018) << "Quiesce aborted!";
974
975 } else if (row_quiesce_write_cfg(table, trx->mysql_thd) != DB_SUCCESS) {
976 ib::warn(ER_IB_MSG_1019) << "There was an error writing to the"
977 " meta data file";
978 } else if (row_quiesce_write_cfp(table, trx->mysql_thd) != DB_SUCCESS) {
979 ib::warn(ER_IB_MSG_1020) << "There was an error writing to the"
980 " encryption info file";
981 } else {
982 ib::info(ER_IB_MSG_1021) << "Table " << table->name << " flushed to disk";
983 }
984 } else {
985 ib::warn(ER_IB_MSG_1022) << "Quiesce aborted!";
986 }
987
988 dberr_t err = row_quiesce_set_state(table, QUIESCE_COMPLETE, trx);
989 ut_a(err == DB_SUCCESS);
990 }
991
992 /** Cleanup after table quiesce.
993 @param[in] table Quiesce this table
994 @param[in,out] trx Transaction/session */
995 void row_quiesce_table_complete(dict_table_t *table, trx_t *trx) {
996 ulint count = 0;
997
998 ut_a(trx->mysql_thd != nullptr);
999
1000 /* We need to wait for the operation to complete if the
1001 transaction has been killed. */
1002
1003 while (table->quiesce != QUIESCE_COMPLETE) {
1004 /* Print a warning after every minute. */
1005 if (!(count % 60)) {
1006 ib::warn(ER_IB_MSG_1023)
1007 << "Waiting for quiesce of " << table->name << " to complete";
1008 }
1009
1010 std::this_thread::sleep_for(std::chrono::seconds(1));
1011
1012 ++count;
1013 }
1014
1015 /* Remove the .cfg file now that the user has resumed
1016 normal operations. Otherwise it will cause problems when
1017 the user tries to drop the database (remove directory). */
1018 char cfg_name[OS_FILE_MAX_PATH];
1019
1020 dd_get_meta_data_filename(table, nullptr, cfg_name, sizeof(cfg_name));
1021
1022 os_file_delete_if_exists(innodb_data_file_key, cfg_name, nullptr);
1023
1024 ib::info(ER_IB_MSG_1024) << "Deleting the meta-data file '" << cfg_name
1025 << "'";
1026
1027 if (dd_is_table_in_encrypted_tablespace(table)) {
1028 char cfp_name[OS_FILE_MAX_PATH];
1029
1030 srv_get_encryption_data_filename(table, cfp_name, sizeof(cfp_name));
1031
1032 os_file_delete_if_exists(innodb_data_file_key, cfp_name, nullptr);
1033
1034 ib::info(ER_IB_MSG_1025)
1035 << "Deleting the meta-data file '" << cfp_name << "'";
1036 }
1037
1038 if (trx_purge_state() != PURGE_STATE_DISABLED) {
1039 trx_purge_run();
1040 }
1041
1042 dberr_t err = row_quiesce_set_state(table, QUIESCE_NONE, trx);
1043 ut_a(err == DB_SUCCESS);
1044 }
1045
1046 /** Set a table's quiesce state.
1047 @return DB_SUCCESS or error code. */
1048 dberr_t row_quiesce_set_state(
1049 dict_table_t *table, /*!< in: quiesce this table */
1050 ib_quiesce_t state, /*!< in: quiesce state to set */
1051 trx_t *trx) /*!< in/out: transaction */
1052 {
1053 ut_a(srv_n_purge_threads > 0);
1054
1055 if (srv_read_only_mode) {
1056 ib_senderrf(trx->mysql_thd, IB_LOG_LEVEL_WARN, ER_READ_ONLY_MODE);
1057
1058 return (DB_UNSUPPORTED);
1059
1060 } else if (table->is_temporary()) {
1061 ib_senderrf(trx->mysql_thd, IB_LOG_LEVEL_WARN,
1062 ER_CANNOT_DISCARD_TEMPORARY_TABLE);
1063
1064 return (DB_UNSUPPORTED);
1065 } else if (table->space == TRX_SYS_SPACE) {
1066 char table_name[MAX_FULL_NAME_LEN + 1];
1067
1068 innobase_format_name(table_name, sizeof(table_name), table->name.m_name);
1069
1070 ib_senderrf(trx->mysql_thd, IB_LOG_LEVEL_WARN,
1071 ER_TABLE_IN_SYSTEM_TABLESPACE, table_name);
1072
1073 return (DB_UNSUPPORTED);
1074
1075 } else if (DICT_TF_HAS_SHARED_SPACE(table->flags)) {
1076 std::ostringstream err_msg;
1077 err_msg << "FLUSH TABLES FOR EXPORT on table " << table->name
1078 << " in a general tablespace.";
1079 ib_senderrf(trx->mysql_thd, IB_LOG_LEVEL_WARN, ER_NOT_SUPPORTED_YET,
1080 err_msg.str().c_str());
1081
1082 return (DB_UNSUPPORTED);
1083 } else if (row_quiesce_table_has_fts_index(table)) {
1084 ib_senderrf(trx->mysql_thd, IB_LOG_LEVEL_WARN, ER_NOT_SUPPORTED_YET,
1085 "FLUSH TABLES on tables that have an FTS index."
1086 " FTS auxiliary tables will not be flushed.");
1087
1088 } else if (DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_HAS_DOC_ID)) {
1089 /* If this flag is set then the table may not have any active
1090 FTS indexes but it will still have the auxiliary tables. */
1091
1092 ib_senderrf(trx->mysql_thd, IB_LOG_LEVEL_WARN, ER_NOT_SUPPORTED_YET,
1093 "FLUSH TABLES on a table that had an FTS index,"
1094 " created on a hidden column, the"
1095 " auxiliary tables haven't been dropped as yet."
1096 " FTS auxiliary tables will not be flushed.");
1097 }
1098
1099 if (srv_thread_is_active(srv_threads.m_trx_recovery_rollback)) {
1100 /* We should wait until rollback after recovery end,
1101 to lock the table consistently. */
1102 srv_threads.m_trx_recovery_rollback.wait();
1103 }
1104 if (trx_purge_state() != PURGE_STATE_DISABLED) {
1105 /* We should stop purge to lock the table consistently. */
1106 trx_purge_stop();
1107 }
1108
1109 row_mysql_lock_data_dictionary(trx, UT_LOCATION_HERE);
1110
1111 dict_table_x_lock_indexes(table);
1112
1113 if (trx_purge_state() != PURGE_STATE_DISABLED) {
1114 trx_purge_run();
1115 }
1116
1117 switch (state) {
1118 case QUIESCE_START:
1119 break;
1120
1121 case QUIESCE_COMPLETE:
1122 ut_a(table->quiesce == QUIESCE_START);
1123 break;
1124
1125 case QUIESCE_NONE:
1126 ut_a(table->quiesce == QUIESCE_COMPLETE);
1127 break;
1128 }
1129
1130 table->quiesce = state;
1131
1132 dict_table_x_unlock_indexes(table);
1133
1134 row_mysql_unlock_data_dictionary(trx);
1135
1136 return (DB_SUCCESS);
1137 }