You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3248 lines
88 KiB

17 years ago
17 years ago
17 years ago
16 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
Merge Google encryption commit 195158e9889365dc3298f8c1f3bcaa745992f27f Author: Minli Zhu <minliz@google.com> Date: Mon Nov 25 11:05:55 2013 -0800 Innodb redo log encryption/decryption. Use start lsn of a log block as part of AES CTR counter. Record key version with each checkpoint. Internally key version 0 means no encryption. Tests done (see test_innodb_log_encryption.sh for detail): - Verify flag innodb_encrypt_log on or off, combined with various key versions passed through CLI, and dynamically set after startup, will not corrupt database. This includes tests from being unencrypted to encrypted, and encrypted to unencrypted. - Verify start-up with no redo logs succeeds. - Verify fresh start-up succeeds. Change-Id: I4ce4c2afdf3076be2fce90ebbc2a7ce01184b612 commit c1b97273659f07866758c25f4a56f680a1fbad24 Author: Jonas Oreland <jonaso@google.com> Date: Tue Dec 3 18:47:27 2013 +0100 encryption of aria data&index files this patch implements encryption of aria data & index files. this is implemented as 1) add read/write hooks (renamed from callbacks) that does encrypt/decrypt (also add pre_read and post_write hooks) 2) modify page headers for data/index to contain key version (making the data-page header size different for with/without encryption) 3) modify index page 0 to contain IV (and crypt header) 4) AES CRT crypt functions 5) counter block is implemented using combination of page no, lsn and table specific id NOTE: 1) log files are not encrypted, this is not needed for if aria is only used for internal temporary tables and they are not transactional (i.e not logged) 2) all encrypted tables are using PAGE_CHECKSUM (crc) normal internal temporary tables are (currently) not CHECKSUM:ed 3) This patch adds insert-order semantics to aria block_format. The default behaviour of aria block-format is best-fit, meaning that rows gets allocated to page trying to fill the pages as much as possible. However, certain sql constructs materialize temporary result in tmp-tables, and expect that a table scan will later return the rows in the same order they were inserted. This implementation of insert-order is only enabled when explicitly requested by sql-layer. CHANGES: 1) found bug in ma_write that made code try to abort a record that was never written unsure why this is not exposed Change-Id: Ia82bbaa92e2c0629c08693c5add2f56b815c0509 commit 89dc1ab651fe0205d55b4eb588f62df550aa65fc Author: Jonas Oreland <jonaso@google.com> Date: Mon Feb 17 08:04:50 2014 -0800 Implement encryption of innodb datafiles. Pages are encrypted before written to disk and decrypted when read from disk. Each page except first page (page 0) in tablespace is encrypted. Page 0 is unencrypted and contains IV for the tablespace. FIL_PAGE_FILE_FLUSH_LSN on each page (except page 0) is used to store a 32-bit key-version, so that multiple keys can be active in a tablespace simultaneous. The other 32-bit of the FIL_PAGE_FILE_FLUSH_LSN field contains a checksum that is computed after encryption. This checksum is used by innochecksum and when restoring from double-write-buffer. The encryption is performed using AES CRT. Monitoring of encryption is enabled using new IS-table INNODB_TABLESPACES_ENCRYPTION. In addition to that new status variables innodb_encryption_rotation_{ pages_read_from_cache, pages_read_from_disk, pages_modified,pages_flushed } has been added. The following tunables are introduces - innodb_encrypt_tables - innodb_encryption_threads - innodb_encryption_rotate_key_age - innodb_encryption_rotation_iops Change-Id: I8f651795a30b52e71b16d6bc9cb7559be349d0b2 commit a17eef2f6948e58219c9e26fc35633d6fd4de1de Author: Andrew Ford <andrewford@google.com> Date: Thu Jan 2 15:43:09 2014 -0800 Key management skeleton with debug hooks. Change-Id: Ifd6aa3743d7ea291c70083f433a059c439aed866 commit 68a399838ad72264fd61b3dc67fecd29bbdb0af1 Author: Andrew Ford <andrewford@google.com> Date: Mon Oct 28 16:27:44 2013 -0700 Add AES-128 CTR and GCM encryption classes. Change-Id: I116305eced2a233db15306bc2ef5b9d398d1a3a2
11 years ago
Merge Google encryption commit 195158e9889365dc3298f8c1f3bcaa745992f27f Author: Minli Zhu <minliz@google.com> Date: Mon Nov 25 11:05:55 2013 -0800 Innodb redo log encryption/decryption. Use start lsn of a log block as part of AES CTR counter. Record key version with each checkpoint. Internally key version 0 means no encryption. Tests done (see test_innodb_log_encryption.sh for detail): - Verify flag innodb_encrypt_log on or off, combined with various key versions passed through CLI, and dynamically set after startup, will not corrupt database. This includes tests from being unencrypted to encrypted, and encrypted to unencrypted. - Verify start-up with no redo logs succeeds. - Verify fresh start-up succeeds. Change-Id: I4ce4c2afdf3076be2fce90ebbc2a7ce01184b612 commit c1b97273659f07866758c25f4a56f680a1fbad24 Author: Jonas Oreland <jonaso@google.com> Date: Tue Dec 3 18:47:27 2013 +0100 encryption of aria data&index files this patch implements encryption of aria data & index files. this is implemented as 1) add read/write hooks (renamed from callbacks) that does encrypt/decrypt (also add pre_read and post_write hooks) 2) modify page headers for data/index to contain key version (making the data-page header size different for with/without encryption) 3) modify index page 0 to contain IV (and crypt header) 4) AES CRT crypt functions 5) counter block is implemented using combination of page no, lsn and table specific id NOTE: 1) log files are not encrypted, this is not needed for if aria is only used for internal temporary tables and they are not transactional (i.e not logged) 2) all encrypted tables are using PAGE_CHECKSUM (crc) normal internal temporary tables are (currently) not CHECKSUM:ed 3) This patch adds insert-order semantics to aria block_format. The default behaviour of aria block-format is best-fit, meaning that rows gets allocated to page trying to fill the pages as much as possible. However, certain sql constructs materialize temporary result in tmp-tables, and expect that a table scan will later return the rows in the same order they were inserted. This implementation of insert-order is only enabled when explicitly requested by sql-layer. CHANGES: 1) found bug in ma_write that made code try to abort a record that was never written unsure why this is not exposed Change-Id: Ia82bbaa92e2c0629c08693c5add2f56b815c0509 commit 89dc1ab651fe0205d55b4eb588f62df550aa65fc Author: Jonas Oreland <jonaso@google.com> Date: Mon Feb 17 08:04:50 2014 -0800 Implement encryption of innodb datafiles. Pages are encrypted before written to disk and decrypted when read from disk. Each page except first page (page 0) in tablespace is encrypted. Page 0 is unencrypted and contains IV for the tablespace. FIL_PAGE_FILE_FLUSH_LSN on each page (except page 0) is used to store a 32-bit key-version, so that multiple keys can be active in a tablespace simultaneous. The other 32-bit of the FIL_PAGE_FILE_FLUSH_LSN field contains a checksum that is computed after encryption. This checksum is used by innochecksum and when restoring from double-write-buffer. The encryption is performed using AES CRT. Monitoring of encryption is enabled using new IS-table INNODB_TABLESPACES_ENCRYPTION. In addition to that new status variables innodb_encryption_rotation_{ pages_read_from_cache, pages_read_from_disk, pages_modified,pages_flushed } has been added. The following tunables are introduces - innodb_encrypt_tables - innodb_encryption_threads - innodb_encryption_rotate_key_age - innodb_encryption_rotation_iops Change-Id: I8f651795a30b52e71b16d6bc9cb7559be349d0b2 commit a17eef2f6948e58219c9e26fc35633d6fd4de1de Author: Andrew Ford <andrewford@google.com> Date: Thu Jan 2 15:43:09 2014 -0800 Key management skeleton with debug hooks. Change-Id: Ifd6aa3743d7ea291c70083f433a059c439aed866 commit 68a399838ad72264fd61b3dc67fecd29bbdb0af1 Author: Andrew Ford <andrewford@google.com> Date: Mon Oct 28 16:27:44 2013 -0700 Add AES-128 CTR and GCM encryption classes. Change-Id: I116305eced2a233db15306bc2ef5b9d398d1a3a2
11 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
11 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
11 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1996, 2013, Oracle and/or its affiliates. All Rights Reserved.
  3. Copyright (c) 2016, MariaDB Corporation.
  4. This program is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free Software
  6. Foundation; version 2 of the License.
  7. This program is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License along with
  11. this program; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  13. *****************************************************************************/
  14. /**************************************************//**
  15. @file dict/dict0load.cc
  16. Loads to the memory cache database object definitions
  17. from dictionary tables
  18. Created 4/24/1996 Heikki Tuuri
  19. *******************************************************/
  20. #include "dict0load.h"
  21. #include "mysql_version.h"
  22. #ifdef UNIV_NONINL
  23. #include "dict0load.ic"
  24. #endif
  25. #include "btr0pcur.h"
  26. #include "btr0btr.h"
  27. #include "page0page.h"
  28. #include "mach0data.h"
  29. #include "dict0dict.h"
  30. #include "dict0boot.h"
  31. #include "dict0stats.h"
  32. #include "rem0cmp.h"
  33. #include "srv0start.h"
  34. #include "srv0srv.h"
  35. #include "dict0crea.h"
  36. #include "dict0priv.h"
  37. #include "ha_prototypes.h" /* innobase_casedn_str() */
  38. #include "fts0priv.h"
  39. /** Following are the InnoDB system tables. The positions in
  40. this array are referenced by enum dict_system_table_id. */
  41. static const char* SYSTEM_TABLE_NAME[] = {
  42. "SYS_TABLES",
  43. "SYS_INDEXES",
  44. "SYS_COLUMNS",
  45. "SYS_FIELDS",
  46. "SYS_FOREIGN",
  47. "SYS_FOREIGN_COLS",
  48. "SYS_TABLESPACES",
  49. "SYS_DATAFILES"
  50. };
  51. /* If this flag is TRUE, then we will load the cluster index's (and tables')
  52. metadata even if it is marked as "corrupted". */
  53. UNIV_INTERN my_bool srv_load_corrupted = FALSE;
  54. #ifdef UNIV_DEBUG
  55. /****************************************************************//**
  56. Compare the name of an index column.
  57. @return TRUE if the i'th column of index is 'name'. */
  58. static
  59. ibool
  60. name_of_col_is(
  61. /*===========*/
  62. const dict_table_t* table, /*!< in: table */
  63. const dict_index_t* index, /*!< in: index */
  64. ulint i, /*!< in: index field offset */
  65. const char* name) /*!< in: name to compare to */
  66. {
  67. ulint tmp = dict_col_get_no(dict_field_get_col(
  68. dict_index_get_nth_field(
  69. index, i)));
  70. return(strcmp(name, dict_table_get_col_name(table, tmp)) == 0);
  71. }
  72. #endif /* UNIV_DEBUG */
  73. /********************************************************************//**
  74. Finds the first table name in the given database.
  75. @return own: table name, NULL if does not exist; the caller must free
  76. the memory in the string! */
  77. UNIV_INTERN
  78. char*
  79. dict_get_first_table_name_in_db(
  80. /*============================*/
  81. const char* name) /*!< in: database name which ends in '/' */
  82. {
  83. dict_table_t* sys_tables;
  84. btr_pcur_t pcur;
  85. dict_index_t* sys_index;
  86. dtuple_t* tuple;
  87. mem_heap_t* heap;
  88. dfield_t* dfield;
  89. const rec_t* rec;
  90. const byte* field;
  91. ulint len;
  92. mtr_t mtr;
  93. ut_ad(mutex_own(&(dict_sys->mutex)));
  94. heap = mem_heap_create(1000);
  95. mtr_start(&mtr);
  96. sys_tables = dict_table_get_low("SYS_TABLES");
  97. sys_index = UT_LIST_GET_FIRST(sys_tables->indexes);
  98. ut_ad(!dict_table_is_comp(sys_tables));
  99. tuple = dtuple_create(heap, 1);
  100. dfield = dtuple_get_nth_field(tuple, 0);
  101. dfield_set_data(dfield, name, ut_strlen(name));
  102. dict_index_copy_types(tuple, sys_index, 1);
  103. btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
  104. BTR_SEARCH_LEAF, &pcur, &mtr);
  105. loop:
  106. rec = btr_pcur_get_rec(&pcur);
  107. if (!btr_pcur_is_on_user_rec(&pcur)) {
  108. /* Not found */
  109. btr_pcur_close(&pcur);
  110. mtr_commit(&mtr);
  111. mem_heap_free(heap);
  112. return(NULL);
  113. }
  114. field = rec_get_nth_field_old(
  115. rec, DICT_FLD__SYS_TABLES__NAME, &len);
  116. if (len < strlen(name)
  117. || ut_memcmp(name, field, strlen(name)) != 0) {
  118. /* Not found */
  119. btr_pcur_close(&pcur);
  120. mtr_commit(&mtr);
  121. mem_heap_free(heap);
  122. return(NULL);
  123. }
  124. if (!rec_get_deleted_flag(rec, 0)) {
  125. /* We found one */
  126. char* table_name = mem_strdupl((char*) field, len);
  127. btr_pcur_close(&pcur);
  128. mtr_commit(&mtr);
  129. mem_heap_free(heap);
  130. return(table_name);
  131. }
  132. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  133. goto loop;
  134. }
  135. /********************************************************************//**
  136. Prints to the standard output information on all tables found in the data
  137. dictionary system table. */
  138. UNIV_INTERN
  139. void
  140. dict_print(void)
  141. /*============*/
  142. {
  143. dict_table_t* table;
  144. btr_pcur_t pcur;
  145. const rec_t* rec;
  146. mem_heap_t* heap;
  147. mtr_t mtr;
  148. /* Enlarge the fatal semaphore wait timeout during the InnoDB table
  149. monitor printout */
  150. os_increment_counter_by_amount(
  151. server_mutex,
  152. srv_fatal_semaphore_wait_threshold,
  153. SRV_SEMAPHORE_WAIT_EXTENSION);
  154. heap = mem_heap_create(1000);
  155. mutex_enter(&(dict_sys->mutex));
  156. mtr_start(&mtr);
  157. rec = dict_startscan_system(&pcur, &mtr, SYS_TABLES);
  158. while (rec) {
  159. const char* err_msg;
  160. err_msg = static_cast<const char*>(
  161. dict_process_sys_tables_rec_and_mtr_commit(
  162. heap, rec, &table, DICT_TABLE_LOAD_FROM_CACHE,
  163. &mtr));
  164. if (!err_msg) {
  165. dict_table_print(table);
  166. } else {
  167. ut_print_timestamp(stderr);
  168. fprintf(stderr, " InnoDB: %s\n", err_msg);
  169. }
  170. mem_heap_empty(heap);
  171. mtr_start(&mtr);
  172. rec = dict_getnext_system(&pcur, &mtr);
  173. }
  174. mtr_commit(&mtr);
  175. mutex_exit(&(dict_sys->mutex));
  176. mem_heap_free(heap);
  177. /* Restore the fatal semaphore wait timeout */
  178. os_decrement_counter_by_amount(
  179. server_mutex,
  180. srv_fatal_semaphore_wait_threshold,
  181. SRV_SEMAPHORE_WAIT_EXTENSION);
  182. }
  183. /********************************************************************//**
  184. This function gets the next system table record as it scans the table.
  185. @return the next record if found, NULL if end of scan */
  186. static
  187. const rec_t*
  188. dict_getnext_system_low(
  189. /*====================*/
  190. btr_pcur_t* pcur, /*!< in/out: persistent cursor to the
  191. record*/
  192. mtr_t* mtr) /*!< in: the mini-transaction */
  193. {
  194. rec_t* rec = NULL;
  195. while (!rec || rec_get_deleted_flag(rec, 0)) {
  196. btr_pcur_move_to_next_user_rec(pcur, mtr);
  197. rec = btr_pcur_get_rec(pcur);
  198. if (!btr_pcur_is_on_user_rec(pcur)) {
  199. /* end of index */
  200. btr_pcur_close(pcur);
  201. return(NULL);
  202. }
  203. }
  204. /* Get a record, let's save the position */
  205. btr_pcur_store_position(pcur, mtr);
  206. return(rec);
  207. }
  208. /********************************************************************//**
  209. This function opens a system table, and returns the first record.
  210. @return first record of the system table */
  211. UNIV_INTERN
  212. const rec_t*
  213. dict_startscan_system(
  214. /*==================*/
  215. btr_pcur_t* pcur, /*!< out: persistent cursor to
  216. the record */
  217. mtr_t* mtr, /*!< in: the mini-transaction */
  218. dict_system_id_t system_id) /*!< in: which system table to open */
  219. {
  220. dict_table_t* system_table;
  221. dict_index_t* clust_index;
  222. const rec_t* rec;
  223. ut_a(system_id < SYS_NUM_SYSTEM_TABLES);
  224. system_table = dict_table_get_low(SYSTEM_TABLE_NAME[system_id]);
  225. clust_index = UT_LIST_GET_FIRST(system_table->indexes);
  226. btr_pcur_open_at_index_side(true, clust_index, BTR_SEARCH_LEAF, pcur,
  227. true, 0, mtr);
  228. rec = dict_getnext_system_low(pcur, mtr);
  229. return(rec);
  230. }
  231. /********************************************************************//**
  232. This function gets the next system table record as it scans the table.
  233. @return the next record if found, NULL if end of scan */
  234. UNIV_INTERN
  235. const rec_t*
  236. dict_getnext_system(
  237. /*================*/
  238. btr_pcur_t* pcur, /*!< in/out: persistent cursor
  239. to the record */
  240. mtr_t* mtr) /*!< in: the mini-transaction */
  241. {
  242. const rec_t* rec;
  243. /* Restore the position */
  244. btr_pcur_restore_position(BTR_SEARCH_LEAF, pcur, mtr);
  245. /* Get the next record */
  246. rec = dict_getnext_system_low(pcur, mtr);
  247. return(rec);
  248. }
  249. /********************************************************************//**
  250. This function processes one SYS_TABLES record and populate the dict_table_t
  251. struct for the table. Extracted out of dict_print() to be used by
  252. both monitor table output and information schema innodb_sys_tables output.
  253. @return error message, or NULL on success */
  254. UNIV_INTERN
  255. const char*
  256. dict_process_sys_tables_rec_and_mtr_commit(
  257. /*=======================================*/
  258. mem_heap_t* heap, /*!< in/out: temporary memory heap */
  259. const rec_t* rec, /*!< in: SYS_TABLES record */
  260. dict_table_t** table, /*!< out: dict_table_t to fill */
  261. dict_table_info_t status, /*!< in: status bit controls
  262. options such as whether we shall
  263. look for dict_table_t from cache
  264. first */
  265. mtr_t* mtr) /*!< in/out: mini-transaction,
  266. will be committed */
  267. {
  268. ulint len;
  269. const char* field;
  270. const char* err_msg = NULL;
  271. char* table_name;
  272. field = (const char*) rec_get_nth_field_old(
  273. rec, DICT_FLD__SYS_TABLES__NAME, &len);
  274. ut_a(!rec_get_deleted_flag(rec, 0));
  275. ut_ad(mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_S_FIX));
  276. /* Get the table name */
  277. table_name = mem_heap_strdupl(heap, field, len);
  278. /* If DICT_TABLE_LOAD_FROM_CACHE is set, first check
  279. whether there is cached dict_table_t struct */
  280. if (status & DICT_TABLE_LOAD_FROM_CACHE) {
  281. /* Commit before load the table again */
  282. mtr_commit(mtr);
  283. *table = dict_table_get_low(table_name);
  284. if (!(*table)) {
  285. err_msg = "Table not found in cache";
  286. }
  287. } else {
  288. err_msg = dict_load_table_low(table_name, rec, table);
  289. mtr_commit(mtr);
  290. }
  291. if (err_msg) {
  292. return(err_msg);
  293. }
  294. return(NULL);
  295. }
  296. /********************************************************************//**
  297. This function parses a SYS_INDEXES record and populate a dict_index_t
  298. structure with the information from the record. For detail information
  299. about SYS_INDEXES fields, please refer to dict_boot() function.
  300. @return error message, or NULL on success */
  301. UNIV_INTERN
  302. const char*
  303. dict_process_sys_indexes_rec(
  304. /*=========================*/
  305. mem_heap_t* heap, /*!< in/out: heap memory */
  306. const rec_t* rec, /*!< in: current SYS_INDEXES rec */
  307. dict_index_t* index, /*!< out: index to be filled */
  308. table_id_t* table_id) /*!< out: index table id */
  309. {
  310. const char* err_msg;
  311. byte* buf;
  312. buf = static_cast<byte*>(mem_heap_alloc(heap, 8));
  313. /* Parse the record, and get "dict_index_t" struct filled */
  314. err_msg = dict_load_index_low(buf, NULL,
  315. heap, rec, FALSE, &index);
  316. *table_id = mach_read_from_8(buf);
  317. return(err_msg);
  318. }
  319. /********************************************************************//**
  320. This function parses a SYS_COLUMNS record and populate a dict_column_t
  321. structure with the information from the record.
  322. @return error message, or NULL on success */
  323. UNIV_INTERN
  324. const char*
  325. dict_process_sys_columns_rec(
  326. /*=========================*/
  327. mem_heap_t* heap, /*!< in/out: heap memory */
  328. const rec_t* rec, /*!< in: current SYS_COLUMNS rec */
  329. dict_col_t* column, /*!< out: dict_col_t to be filled */
  330. table_id_t* table_id, /*!< out: table id */
  331. const char** col_name) /*!< out: column name */
  332. {
  333. const char* err_msg;
  334. /* Parse the record, and get "dict_col_t" struct filled */
  335. err_msg = dict_load_column_low(NULL, heap, column,
  336. table_id, col_name, rec);
  337. return(err_msg);
  338. }
  339. /********************************************************************//**
  340. This function parses a SYS_FIELDS record and populates a dict_field_t
  341. structure with the information from the record.
  342. @return error message, or NULL on success */
  343. UNIV_INTERN
  344. const char*
  345. dict_process_sys_fields_rec(
  346. /*========================*/
  347. mem_heap_t* heap, /*!< in/out: heap memory */
  348. const rec_t* rec, /*!< in: current SYS_FIELDS rec */
  349. dict_field_t* sys_field, /*!< out: dict_field_t to be
  350. filled */
  351. ulint* pos, /*!< out: Field position */
  352. index_id_t* index_id, /*!< out: current index id */
  353. index_id_t last_id) /*!< in: previous index id */
  354. {
  355. byte* buf;
  356. byte* last_index_id;
  357. const char* err_msg;
  358. buf = static_cast<byte*>(mem_heap_alloc(heap, 8));
  359. last_index_id = static_cast<byte*>(mem_heap_alloc(heap, 8));
  360. mach_write_to_8(last_index_id, last_id);
  361. err_msg = dict_load_field_low(buf, NULL, sys_field,
  362. pos, last_index_id, heap, rec);
  363. *index_id = mach_read_from_8(buf);
  364. return(err_msg);
  365. }
  366. /********************************************************************//**
  367. This function parses a SYS_FOREIGN record and populate a dict_foreign_t
  368. structure with the information from the record. For detail information
  369. about SYS_FOREIGN fields, please refer to dict_load_foreign() function.
  370. @return error message, or NULL on success */
  371. UNIV_INTERN
  372. const char*
  373. dict_process_sys_foreign_rec(
  374. /*=========================*/
  375. mem_heap_t* heap, /*!< in/out: heap memory */
  376. const rec_t* rec, /*!< in: current SYS_FOREIGN rec */
  377. dict_foreign_t* foreign) /*!< out: dict_foreign_t struct
  378. to be filled */
  379. {
  380. ulint len;
  381. const byte* field;
  382. ulint n_fields_and_type;
  383. if (rec_get_deleted_flag(rec, 0)) {
  384. return("delete-marked record in SYS_FOREIGN");
  385. }
  386. if (rec_get_n_fields_old(rec) != DICT_NUM_FIELDS__SYS_FOREIGN) {
  387. return("wrong number of columns in SYS_FOREIGN record");
  388. }
  389. field = rec_get_nth_field_old(
  390. rec, DICT_FLD__SYS_FOREIGN__ID, &len);
  391. if (len == 0 || len == UNIV_SQL_NULL) {
  392. err_len:
  393. return("incorrect column length in SYS_FOREIGN");
  394. }
  395. /* This recieves a dict_foreign_t* that points to a stack variable.
  396. So mem_heap_free(foreign->heap) is not used as elsewhere.
  397. Since the heap used here is freed elsewhere, foreign->heap
  398. is not assigned. */
  399. foreign->id = mem_heap_strdupl(heap, (const char*) field, len);
  400. rec_get_nth_field_offs_old(
  401. rec, DICT_FLD__SYS_FOREIGN__DB_TRX_ID, &len);
  402. if (len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL) {
  403. goto err_len;
  404. }
  405. rec_get_nth_field_offs_old(
  406. rec, DICT_FLD__SYS_FOREIGN__DB_ROLL_PTR, &len);
  407. if (len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL) {
  408. goto err_len;
  409. }
  410. /* The _lookup versions of the referenced and foreign table names
  411. are not assigned since they are not used in this dict_foreign_t */
  412. field = rec_get_nth_field_old(
  413. rec, DICT_FLD__SYS_FOREIGN__FOR_NAME, &len);
  414. if (len == 0 || len == UNIV_SQL_NULL) {
  415. goto err_len;
  416. }
  417. foreign->foreign_table_name = mem_heap_strdupl(
  418. heap, (const char*) field, len);
  419. field = rec_get_nth_field_old(
  420. rec, DICT_FLD__SYS_FOREIGN__REF_NAME, &len);
  421. if (len == 0 || len == UNIV_SQL_NULL) {
  422. goto err_len;
  423. }
  424. foreign->referenced_table_name = mem_heap_strdupl(
  425. heap, (const char*) field, len);
  426. field = rec_get_nth_field_old(
  427. rec, DICT_FLD__SYS_FOREIGN__N_COLS, &len);
  428. if (len != 4) {
  429. goto err_len;
  430. }
  431. n_fields_and_type = mach_read_from_4(field);
  432. foreign->type = (unsigned int) (n_fields_and_type >> 24);
  433. foreign->n_fields = (unsigned int) (n_fields_and_type & 0x3FFUL);
  434. return(NULL);
  435. }
  436. /********************************************************************//**
  437. This function parses a SYS_FOREIGN_COLS record and extract necessary
  438. information from the record and return to caller.
  439. @return error message, or NULL on success */
  440. UNIV_INTERN
  441. const char*
  442. dict_process_sys_foreign_col_rec(
  443. /*=============================*/
  444. mem_heap_t* heap, /*!< in/out: heap memory */
  445. const rec_t* rec, /*!< in: current SYS_FOREIGN_COLS rec */
  446. const char** name, /*!< out: foreign key constraint name */
  447. const char** for_col_name, /*!< out: referencing column name */
  448. const char** ref_col_name, /*!< out: referenced column name
  449. in referenced table */
  450. ulint* pos) /*!< out: column position */
  451. {
  452. ulint len;
  453. const byte* field;
  454. if (rec_get_deleted_flag(rec, 0)) {
  455. return("delete-marked record in SYS_FOREIGN_COLS");
  456. }
  457. if (rec_get_n_fields_old(rec) != DICT_NUM_FIELDS__SYS_FOREIGN_COLS) {
  458. return("wrong number of columns in SYS_FOREIGN_COLS record");
  459. }
  460. field = rec_get_nth_field_old(
  461. rec, DICT_FLD__SYS_FOREIGN_COLS__ID, &len);
  462. if (len == 0 || len == UNIV_SQL_NULL) {
  463. err_len:
  464. return("incorrect column length in SYS_FOREIGN_COLS");
  465. }
  466. *name = mem_heap_strdupl(heap, (char*) field, len);
  467. field = rec_get_nth_field_old(
  468. rec, DICT_FLD__SYS_FOREIGN_COLS__POS, &len);
  469. if (len != 4) {
  470. goto err_len;
  471. }
  472. *pos = mach_read_from_4(field);
  473. rec_get_nth_field_offs_old(
  474. rec, DICT_FLD__SYS_FOREIGN_COLS__DB_TRX_ID, &len);
  475. if (len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL) {
  476. goto err_len;
  477. }
  478. rec_get_nth_field_offs_old(
  479. rec, DICT_FLD__SYS_FOREIGN_COLS__DB_ROLL_PTR, &len);
  480. if (len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL) {
  481. goto err_len;
  482. }
  483. field = rec_get_nth_field_old(
  484. rec, DICT_FLD__SYS_FOREIGN_COLS__FOR_COL_NAME, &len);
  485. if (len == 0 || len == UNIV_SQL_NULL) {
  486. goto err_len;
  487. }
  488. *for_col_name = mem_heap_strdupl(heap, (char*) field, len);
  489. field = rec_get_nth_field_old(
  490. rec, DICT_FLD__SYS_FOREIGN_COLS__REF_COL_NAME, &len);
  491. if (len == 0 || len == UNIV_SQL_NULL) {
  492. goto err_len;
  493. }
  494. *ref_col_name = mem_heap_strdupl(heap, (char*) field, len);
  495. return(NULL);
  496. }
  497. /********************************************************************//**
  498. This function parses a SYS_TABLESPACES record, extracts necessary
  499. information from the record and returns to caller.
  500. @return error message, or NULL on success */
  501. UNIV_INTERN
  502. const char*
  503. dict_process_sys_tablespaces(
  504. /*=========================*/
  505. mem_heap_t* heap, /*!< in/out: heap memory */
  506. const rec_t* rec, /*!< in: current SYS_TABLESPACES rec */
  507. ulint* space, /*!< out: space id */
  508. const char** name, /*!< out: tablespace name */
  509. ulint* flags) /*!< out: tablespace flags */
  510. {
  511. ulint len;
  512. const byte* field;
  513. /* Initialize the output values */
  514. *space = ULINT_UNDEFINED;
  515. *name = NULL;
  516. *flags = ULINT_UNDEFINED;
  517. if (rec_get_deleted_flag(rec, 0)) {
  518. return("delete-marked record in SYS_TABLESPACES");
  519. }
  520. if (rec_get_n_fields_old(rec) != DICT_NUM_FIELDS__SYS_TABLESPACES) {
  521. return("wrong number of columns in SYS_TABLESPACES record");
  522. }
  523. field = rec_get_nth_field_old(
  524. rec, DICT_FLD__SYS_TABLESPACES__SPACE, &len);
  525. if (len != DICT_FLD_LEN_SPACE) {
  526. err_len:
  527. return("incorrect column length in SYS_TABLESPACES");
  528. }
  529. *space = mach_read_from_4(field);
  530. rec_get_nth_field_offs_old(
  531. rec, DICT_FLD__SYS_TABLESPACES__DB_TRX_ID, &len);
  532. if (len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL) {
  533. goto err_len;
  534. }
  535. rec_get_nth_field_offs_old(
  536. rec, DICT_FLD__SYS_TABLESPACES__DB_ROLL_PTR, &len);
  537. if (len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL) {
  538. goto err_len;
  539. }
  540. field = rec_get_nth_field_old(
  541. rec, DICT_FLD__SYS_TABLESPACES__NAME, &len);
  542. if (len == 0 || len == UNIV_SQL_NULL) {
  543. goto err_len;
  544. }
  545. *name = mem_heap_strdupl(heap, (char*) field, len);
  546. field = rec_get_nth_field_old(
  547. rec, DICT_FLD__SYS_TABLESPACES__FLAGS, &len);
  548. if (len != DICT_FLD_LEN_FLAGS) {
  549. goto err_len;
  550. }
  551. *flags = mach_read_from_4(field);
  552. return(NULL);
  553. }
  554. /********************************************************************//**
  555. This function parses a SYS_DATAFILES record, extracts necessary
  556. information from the record and returns it to the caller.
  557. @return error message, or NULL on success */
  558. UNIV_INTERN
  559. const char*
  560. dict_process_sys_datafiles(
  561. /*=======================*/
  562. mem_heap_t* heap, /*!< in/out: heap memory */
  563. const rec_t* rec, /*!< in: current SYS_DATAFILES rec */
  564. ulint* space, /*!< out: space id */
  565. const char** path) /*!< out: datafile paths */
  566. {
  567. ulint len;
  568. const byte* field;
  569. if (rec_get_deleted_flag(rec, 0)) {
  570. return("delete-marked record in SYS_DATAFILES");
  571. }
  572. if (rec_get_n_fields_old(rec) != DICT_NUM_FIELDS__SYS_DATAFILES) {
  573. return("wrong number of columns in SYS_DATAFILES record");
  574. }
  575. field = rec_get_nth_field_old(
  576. rec, DICT_FLD__SYS_DATAFILES__SPACE, &len);
  577. if (len != DICT_FLD_LEN_SPACE) {
  578. err_len:
  579. return("incorrect column length in SYS_DATAFILES");
  580. }
  581. *space = mach_read_from_4(field);
  582. rec_get_nth_field_offs_old(
  583. rec, DICT_FLD__SYS_DATAFILES__DB_TRX_ID, &len);
  584. if (len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL) {
  585. goto err_len;
  586. }
  587. rec_get_nth_field_offs_old(
  588. rec, DICT_FLD__SYS_DATAFILES__DB_ROLL_PTR, &len);
  589. if (len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL) {
  590. goto err_len;
  591. }
  592. field = rec_get_nth_field_old(
  593. rec, DICT_FLD__SYS_DATAFILES__PATH, &len);
  594. if (len == 0 || len == UNIV_SQL_NULL) {
  595. goto err_len;
  596. }
  597. *path = mem_heap_strdupl(heap, (char*) field, len);
  598. return(NULL);
  599. }
  600. /********************************************************************//**
  601. Determine the flags of a table as stored in SYS_TABLES.TYPE and N_COLS.
  602. @return ULINT_UNDEFINED if error, else a valid dict_table_t::flags. */
  603. static
  604. ulint
  605. dict_sys_tables_get_flags(
  606. /*======================*/
  607. const rec_t* rec) /*!< in: a record of SYS_TABLES */
  608. {
  609. const byte* field;
  610. ulint len;
  611. ulint type;
  612. ulint n_cols;
  613. /* read the 4 byte flags from the TYPE field */
  614. field = rec_get_nth_field_old(
  615. rec, DICT_FLD__SYS_TABLES__TYPE, &len);
  616. ut_a(len == 4);
  617. type = mach_read_from_4(field);
  618. /* The low order bit of SYS_TABLES.TYPE is always set to 1. But in
  619. dict_table_t::flags the low order bit is used to determine if the
  620. row format is Redundant or Compact when the format is Antelope.
  621. Read the 4 byte N_COLS field and look at the high order bit. It
  622. should be set for COMPACT and later. It should not be set for
  623. REDUNDANT. */
  624. field = rec_get_nth_field_old(
  625. rec, DICT_FLD__SYS_TABLES__N_COLS, &len);
  626. ut_a(len == 4);
  627. n_cols = mach_read_from_4(field);
  628. /* This validation function also combines the DICT_N_COLS_COMPACT
  629. flag in n_cols into the type field to effectively make it a
  630. dict_table_t::flags. */
  631. if (ULINT_UNDEFINED == dict_sys_tables_type_validate(type, n_cols)) {
  632. return(ULINT_UNDEFINED);
  633. }
  634. return(dict_sys_tables_type_to_tf(type, n_cols));
  635. }
  636. /********************************************************************//**
  637. Gets the filepath for a spaceid from SYS_DATAFILES and checks it against
  638. the contents of a link file. This function is called when there is no
  639. fil_node_t entry for this space ID so both durable locations on disk
  640. must be checked and compared.
  641. We use a temporary heap here for the table lookup, but not for the path
  642. returned which the caller must free.
  643. This function can return NULL if the space ID is not found in SYS_DATAFILES,
  644. then the caller will assume that the ibd file is in the normal datadir.
  645. @return own: A copy of the first datafile found in SYS_DATAFILES.PATH for
  646. the given space ID. NULL if space ID is zero or not found. */
  647. UNIV_INTERN
  648. char*
  649. dict_get_first_path(
  650. /*================*/
  651. ulint space, /*!< in: space id */
  652. const char* name) /*!< in: tablespace name */
  653. {
  654. mtr_t mtr;
  655. dict_table_t* sys_datafiles;
  656. dict_index_t* sys_index;
  657. dtuple_t* tuple;
  658. dfield_t* dfield;
  659. byte* buf;
  660. btr_pcur_t pcur;
  661. const rec_t* rec;
  662. const byte* field;
  663. ulint len;
  664. char* dict_filepath = NULL;
  665. mem_heap_t* heap = mem_heap_create(1024);
  666. ut_ad(mutex_own(&(dict_sys->mutex)));
  667. mtr_start(&mtr);
  668. sys_datafiles = dict_table_get_low("SYS_DATAFILES");
  669. sys_index = UT_LIST_GET_FIRST(sys_datafiles->indexes);
  670. ut_ad(!dict_table_is_comp(sys_datafiles));
  671. ut_ad(name_of_col_is(sys_datafiles, sys_index,
  672. DICT_FLD__SYS_DATAFILES__SPACE, "SPACE"));
  673. ut_ad(name_of_col_is(sys_datafiles, sys_index,
  674. DICT_FLD__SYS_DATAFILES__PATH, "PATH"));
  675. tuple = dtuple_create(heap, 1);
  676. dfield = dtuple_get_nth_field(tuple, DICT_FLD__SYS_DATAFILES__SPACE);
  677. buf = static_cast<byte*>(mem_heap_alloc(heap, 4));
  678. mach_write_to_4(buf, space);
  679. dfield_set_data(dfield, buf, 4);
  680. dict_index_copy_types(tuple, sys_index, 1);
  681. btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
  682. BTR_SEARCH_LEAF, &pcur, &mtr);
  683. rec = btr_pcur_get_rec(&pcur);
  684. /* If the file-per-table tablespace was created with
  685. an earlier version of InnoDB, then this record is not
  686. in SYS_DATAFILES. But a link file still might exist. */
  687. if (btr_pcur_is_on_user_rec(&pcur)) {
  688. /* A record for this space ID was found. */
  689. field = rec_get_nth_field_old(
  690. rec, DICT_FLD__SYS_DATAFILES__PATH, &len);
  691. ut_a(len > 0 || len == UNIV_SQL_NULL);
  692. ut_a(len < OS_FILE_MAX_PATH);
  693. dict_filepath = mem_strdupl((char*) field, len);
  694. ut_a(dict_filepath);
  695. }
  696. btr_pcur_close(&pcur);
  697. mtr_commit(&mtr);
  698. mem_heap_free(heap);
  699. return(dict_filepath);
  700. }
  701. /********************************************************************//**
  702. Update the record for space_id in SYS_TABLESPACES to this filepath.
  703. @return DB_SUCCESS if OK, dberr_t if the insert failed */
  704. UNIV_INTERN
  705. dberr_t
  706. dict_update_filepath(
  707. /*=================*/
  708. ulint space_id, /*!< in: space id */
  709. const char* filepath) /*!< in: filepath */
  710. {
  711. dberr_t err = DB_SUCCESS;
  712. trx_t* trx;
  713. #ifdef UNIV_SYNC_DEBUG
  714. ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_EX));
  715. #endif /* UNIV_SYNC_DEBUG */
  716. ut_ad(mutex_own(&(dict_sys->mutex)));
  717. trx = trx_allocate_for_background();
  718. trx->op_info = "update filepath";
  719. trx->dict_operation_lock_mode = RW_X_LATCH;
  720. trx_start_for_ddl(trx, TRX_DICT_OP_INDEX);
  721. pars_info_t* info = pars_info_create();
  722. pars_info_add_int4_literal(info, "space", space_id);
  723. pars_info_add_str_literal(info, "path", filepath);
  724. err = que_eval_sql(info,
  725. "PROCEDURE UPDATE_FILEPATH () IS\n"
  726. "BEGIN\n"
  727. "UPDATE SYS_DATAFILES"
  728. " SET PATH = :path\n"
  729. " WHERE SPACE = :space;\n"
  730. "END;\n", FALSE, trx);
  731. trx_commit_for_mysql(trx);
  732. trx->dict_operation_lock_mode = 0;
  733. trx_free_for_background(trx);
  734. if (err == DB_SUCCESS) {
  735. /* We just updated SYS_DATAFILES due to the contents in
  736. a link file. Make a note that we did this. */
  737. ib_logf(IB_LOG_LEVEL_INFO,
  738. "The InnoDB data dictionary table SYS_DATAFILES "
  739. "for tablespace ID %lu was updated to use file %s.",
  740. (ulong) space_id, filepath);
  741. } else {
  742. ib_logf(IB_LOG_LEVEL_WARN,
  743. "Problem updating InnoDB data dictionary table "
  744. "SYS_DATAFILES for tablespace ID %lu to file %s.",
  745. (ulong) space_id, filepath);
  746. }
  747. return(err);
  748. }
  749. /********************************************************************//**
  750. Insert records into SYS_TABLESPACES and SYS_DATAFILES.
  751. @return DB_SUCCESS if OK, dberr_t if the insert failed */
  752. UNIV_INTERN
  753. dberr_t
  754. dict_insert_tablespace_and_filepath(
  755. /*================================*/
  756. ulint space, /*!< in: space id */
  757. const char* name, /*!< in: talespace name */
  758. const char* filepath, /*!< in: filepath */
  759. ulint fsp_flags) /*!< in: tablespace flags */
  760. {
  761. dberr_t err = DB_SUCCESS;
  762. trx_t* trx;
  763. #ifdef UNIV_SYNC_DEBUG
  764. ut_ad(rw_lock_own(&dict_operation_lock, RW_LOCK_EX));
  765. #endif /* UNIV_SYNC_DEBUG */
  766. ut_ad(mutex_own(&(dict_sys->mutex)));
  767. ut_ad(filepath);
  768. trx = trx_allocate_for_background();
  769. trx->op_info = "insert tablespace and filepath";
  770. trx->dict_operation_lock_mode = RW_X_LATCH;
  771. trx_start_for_ddl(trx, TRX_DICT_OP_INDEX);
  772. /* A record for this space ID was not found in
  773. SYS_DATAFILES. Assume the record is also missing in
  774. SYS_TABLESPACES. Insert records onto them both. */
  775. err = dict_create_add_tablespace_to_dictionary(
  776. space, name, fsp_flags, filepath, trx, false);
  777. trx_commit_for_mysql(trx);
  778. trx->dict_operation_lock_mode = 0;
  779. trx_free_for_background(trx);
  780. return(err);
  781. }
  782. /********************************************************************//**
  783. This function looks at each table defined in SYS_TABLES. It checks the
  784. tablespace for any table with a space_id > 0. It looks up the tablespace
  785. in SYS_DATAFILES to ensure the correct path.
  786. In a crash recovery we already have all the tablespace objects created.
  787. This function compares the space id information in the InnoDB data dictionary
  788. to what we already read with fil_load_single_table_tablespaces().
  789. In a normal startup, we create the tablespace objects for every table in
  790. InnoDB's data dictionary, if the corresponding .ibd file exists.
  791. We also scan the biggest space id, and store it to fil_system. */
  792. UNIV_INTERN
  793. void
  794. dict_check_tablespaces_and_store_max_id(
  795. /*====================================*/
  796. dict_check_t dict_check) /*!< in: how to check */
  797. {
  798. dict_table_t* sys_tables;
  799. dict_index_t* sys_index;
  800. btr_pcur_t pcur;
  801. const rec_t* rec;
  802. ulint max_space_id;
  803. mtr_t mtr;
  804. rw_lock_x_lock(&dict_operation_lock);
  805. mutex_enter(&(dict_sys->mutex));
  806. mtr_start(&mtr);
  807. sys_tables = dict_table_get_low("SYS_TABLES");
  808. sys_index = UT_LIST_GET_FIRST(sys_tables->indexes);
  809. ut_ad(!dict_table_is_comp(sys_tables));
  810. max_space_id = mtr_read_ulint(dict_hdr_get(&mtr)
  811. + DICT_HDR_MAX_SPACE_ID,
  812. MLOG_4BYTES, &mtr);
  813. fil_set_max_space_id_if_bigger(max_space_id);
  814. btr_pcur_open_at_index_side(true, sys_index, BTR_SEARCH_LEAF, &pcur,
  815. true, 0, &mtr);
  816. loop:
  817. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  818. rec = btr_pcur_get_rec(&pcur);
  819. if (!btr_pcur_is_on_user_rec(&pcur)) {
  820. /* end of index */
  821. btr_pcur_close(&pcur);
  822. mtr_commit(&mtr);
  823. /* We must make the tablespace cache aware of the biggest
  824. known space id */
  825. /* printf("Biggest space id in data dictionary %lu\n",
  826. max_space_id); */
  827. fil_set_max_space_id_if_bigger(max_space_id);
  828. mutex_exit(&(dict_sys->mutex));
  829. rw_lock_x_unlock(&dict_operation_lock);
  830. return;
  831. }
  832. if (!rec_get_deleted_flag(rec, 0)) {
  833. /* We found one */
  834. const byte* field;
  835. ulint len;
  836. ulint space_id;
  837. ulint flags;
  838. char* name;
  839. field = rec_get_nth_field_old(
  840. rec, DICT_FLD__SYS_TABLES__NAME, &len);
  841. name = mem_strdupl((char*) field, len);
  842. char table_name[MAX_FULL_NAME_LEN + 1];
  843. innobase_format_name(
  844. table_name, sizeof(table_name), name, FALSE);
  845. flags = dict_sys_tables_get_flags(rec);
  846. if (UNIV_UNLIKELY(flags == ULINT_UNDEFINED)) {
  847. /* Read again the 4 bytes from rec. */
  848. field = rec_get_nth_field_old(
  849. rec, DICT_FLD__SYS_TABLES__TYPE, &len);
  850. ut_ad(len == 4); /* this was checked earlier */
  851. flags = mach_read_from_4(field);
  852. ib_logf(IB_LOG_LEVEL_ERROR,
  853. "Table '%s' in InnoDB data dictionary"
  854. " has unknown type %lx", table_name, flags);
  855. mem_free(name);
  856. goto loop;
  857. }
  858. field = rec_get_nth_field_old(
  859. rec, DICT_FLD__SYS_TABLES__SPACE, &len);
  860. ut_a(len == 4);
  861. space_id = mach_read_from_4(field);
  862. btr_pcur_store_position(&pcur, &mtr);
  863. mtr_commit(&mtr);
  864. /* For tables created with old versions of InnoDB,
  865. SYS_TABLES.MIX_LEN may contain garbage. Such tables
  866. would always be in ROW_FORMAT=REDUNDANT. Pretend that
  867. all such tables are non-temporary. That is, do not
  868. suppress error printouts about temporary or discarded
  869. tablespaces not being found. */
  870. field = rec_get_nth_field_old(
  871. rec, DICT_FLD__SYS_TABLES__MIX_LEN, &len);
  872. bool is_temp = false;
  873. bool discarded = false;
  874. ib_uint32_t flags2 = static_cast<ib_uint32_t>(
  875. mach_read_from_4(field));
  876. /* Check that the tablespace (the .ibd file) really
  877. exists; print a warning to the .err log if not.
  878. Do not print warnings for temporary tables or for
  879. tablespaces that have been discarded. */
  880. field = rec_get_nth_field_old(
  881. rec, DICT_FLD__SYS_TABLES__N_COLS, &len);
  882. /* MIX_LEN valid only for ROW_FORMAT > REDUNDANT. */
  883. if (mach_read_from_4(field) & DICT_N_COLS_COMPACT) {
  884. is_temp = !!(flags2 & DICT_TF2_TEMPORARY);
  885. discarded = !!(flags2 & DICT_TF2_DISCARDED);
  886. }
  887. if (space_id == 0) {
  888. /* The system tablespace always exists. */
  889. ut_ad(!discarded);
  890. goto next_tablespace;
  891. }
  892. switch (dict_check) {
  893. case DICT_CHECK_ALL_LOADED:
  894. /* All tablespaces should have been found in
  895. fil_load_single_table_tablespaces(). */
  896. if (fil_space_for_table_exists_in_mem(
  897. space_id, name, TRUE, !(is_temp || discarded),
  898. false, NULL, 0)
  899. && !(is_temp || discarded)) {
  900. /* If user changes the path of .ibd files in
  901. *.isl files before doing crash recovery ,
  902. then this leads to inconsistency in
  903. SYS_DATAFILES system table because the
  904. tables are loaded from the updated path
  905. but the SYS_DATAFILES still points to the
  906. old path.Therefore after crash recovery
  907. update SYS_DATAFILES with the updated path.*/
  908. ut_ad(space_id);
  909. ut_ad(recv_needed_recovery);
  910. char *dict_path = dict_get_first_path(space_id,
  911. name);
  912. char *remote_path = fil_read_link_file(name);
  913. if(dict_path && remote_path) {
  914. if(strcmp(dict_path,remote_path)) {
  915. dict_update_filepath(space_id,
  916. remote_path);
  917. }
  918. }
  919. if(dict_path)
  920. mem_free(dict_path);
  921. if(remote_path)
  922. mem_free(remote_path);
  923. }
  924. break;
  925. case DICT_CHECK_SOME_LOADED:
  926. /* Some tablespaces may have been opened in
  927. trx_resurrect_table_locks(). */
  928. if (fil_space_for_table_exists_in_mem(
  929. space_id, name, FALSE, FALSE,
  930. false, NULL, 0)) {
  931. break;
  932. }
  933. /* fall through */
  934. case DICT_CHECK_NONE_LOADED:
  935. if (discarded) {
  936. ib_logf(IB_LOG_LEVEL_INFO,
  937. "DISCARD flag set for table '%s',"
  938. " ignored.",
  939. table_name);
  940. break;
  941. }
  942. /* It is a normal database startup: create the
  943. space object and check that the .ibd file exists.
  944. If the table uses a remote tablespace, look for the
  945. space_id in SYS_DATAFILES to find the filepath */
  946. /* Use the remote filepath if known. */
  947. char* filepath = NULL;
  948. if (DICT_TF_HAS_DATA_DIR(flags)) {
  949. filepath = dict_get_first_path(
  950. space_id, name);
  951. }
  952. /* We need to read page 0 to get (optional) IV
  953. regardless if encryptions is turned on or not,
  954. since if it's off we should decrypt a potentially
  955. already encrypted table */
  956. bool read_page_0 = true;
  957. /* We set the 2nd param (fix_dict = true)
  958. here because we already have an x-lock on
  959. dict_operation_lock and dict_sys->mutex. Besides,
  960. this is at startup and we are now single threaded.
  961. If the filepath is not known, it will need to
  962. be discovered. */
  963. dberr_t err = fil_open_single_table_tablespace(
  964. read_page_0, srv_read_only_mode ? false : true,
  965. space_id, dict_tf_to_fsp_flags(flags),
  966. name, filepath, NULL);
  967. if (err != DB_SUCCESS) {
  968. ib_logf(IB_LOG_LEVEL_ERROR,
  969. "Tablespace open failed for '%s', "
  970. "ignored.", table_name);
  971. }
  972. if (filepath) {
  973. mem_free(filepath);
  974. }
  975. break;
  976. }
  977. if (space_id > max_space_id) {
  978. max_space_id = space_id;
  979. }
  980. next_tablespace:
  981. mem_free(name);
  982. mtr_start(&mtr);
  983. btr_pcur_restore_position(BTR_SEARCH_LEAF, &pcur, &mtr);
  984. }
  985. goto loop;
  986. }
  987. /********************************************************************//**
  988. Loads a table column definition from a SYS_COLUMNS record to
  989. dict_table_t.
  990. @return error message, or NULL on success */
  991. UNIV_INTERN
  992. const char*
  993. dict_load_column_low(
  994. /*=================*/
  995. dict_table_t* table, /*!< in/out: table, could be NULL
  996. if we just populate a dict_column_t
  997. struct with information from
  998. a SYS_COLUMNS record */
  999. mem_heap_t* heap, /*!< in/out: memory heap
  1000. for temporary storage */
  1001. dict_col_t* column, /*!< out: dict_column_t to fill,
  1002. or NULL if table != NULL */
  1003. table_id_t* table_id, /*!< out: table id */
  1004. const char** col_name, /*!< out: column name */
  1005. const rec_t* rec) /*!< in: SYS_COLUMNS record */
  1006. {
  1007. char* name;
  1008. const byte* field;
  1009. ulint len;
  1010. ulint mtype;
  1011. ulint prtype;
  1012. ulint col_len;
  1013. ulint pos;
  1014. ut_ad(table || column);
  1015. if (rec_get_deleted_flag(rec, 0)) {
  1016. return("delete-marked record in SYS_COLUMNS");
  1017. }
  1018. if (rec_get_n_fields_old(rec) != DICT_NUM_FIELDS__SYS_COLUMNS) {
  1019. return("wrong number of columns in SYS_COLUMNS record");
  1020. }
  1021. field = rec_get_nth_field_old(
  1022. rec, DICT_FLD__SYS_COLUMNS__TABLE_ID, &len);
  1023. if (len != 8) {
  1024. err_len:
  1025. return("incorrect column length in SYS_COLUMNS");
  1026. }
  1027. if (table_id) {
  1028. *table_id = mach_read_from_8(field);
  1029. } else if (table->id != mach_read_from_8(field)) {
  1030. return("SYS_COLUMNS.TABLE_ID mismatch");
  1031. }
  1032. field = rec_get_nth_field_old(
  1033. rec, DICT_FLD__SYS_COLUMNS__POS, &len);
  1034. if (len != 4) {
  1035. goto err_len;
  1036. }
  1037. pos = mach_read_from_4(field);
  1038. if (table && table->n_def != pos) {
  1039. return("SYS_COLUMNS.POS mismatch");
  1040. }
  1041. rec_get_nth_field_offs_old(
  1042. rec, DICT_FLD__SYS_COLUMNS__DB_TRX_ID, &len);
  1043. if (len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL) {
  1044. goto err_len;
  1045. }
  1046. rec_get_nth_field_offs_old(
  1047. rec, DICT_FLD__SYS_COLUMNS__DB_ROLL_PTR, &len);
  1048. if (len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL) {
  1049. goto err_len;
  1050. }
  1051. field = rec_get_nth_field_old(
  1052. rec, DICT_FLD__SYS_COLUMNS__NAME, &len);
  1053. if (len == 0 || len == UNIV_SQL_NULL) {
  1054. goto err_len;
  1055. }
  1056. name = mem_heap_strdupl(heap, (const char*) field, len);
  1057. if (col_name) {
  1058. *col_name = name;
  1059. }
  1060. field = rec_get_nth_field_old(
  1061. rec, DICT_FLD__SYS_COLUMNS__MTYPE, &len);
  1062. if (len != 4) {
  1063. goto err_len;
  1064. }
  1065. mtype = mach_read_from_4(field);
  1066. field = rec_get_nth_field_old(
  1067. rec, DICT_FLD__SYS_COLUMNS__PRTYPE, &len);
  1068. if (len != 4) {
  1069. goto err_len;
  1070. }
  1071. prtype = mach_read_from_4(field);
  1072. if (dtype_get_charset_coll(prtype) == 0
  1073. && dtype_is_string_type(mtype)) {
  1074. /* The table was created with < 4.1.2. */
  1075. if (dtype_is_binary_string_type(mtype, prtype)) {
  1076. /* Use the binary collation for
  1077. string columns of binary type. */
  1078. prtype = dtype_form_prtype(
  1079. prtype,
  1080. DATA_MYSQL_BINARY_CHARSET_COLL);
  1081. } else {
  1082. /* Use the default charset for
  1083. other than binary columns. */
  1084. prtype = dtype_form_prtype(
  1085. prtype,
  1086. data_mysql_default_charset_coll);
  1087. }
  1088. }
  1089. field = rec_get_nth_field_old(
  1090. rec, DICT_FLD__SYS_COLUMNS__LEN, &len);
  1091. if (len != 4) {
  1092. goto err_len;
  1093. }
  1094. col_len = mach_read_from_4(field);
  1095. field = rec_get_nth_field_old(
  1096. rec, DICT_FLD__SYS_COLUMNS__PREC, &len);
  1097. if (len != 4) {
  1098. goto err_len;
  1099. }
  1100. if (!column) {
  1101. dict_mem_table_add_col(table, heap, name, mtype,
  1102. prtype, col_len);
  1103. } else {
  1104. dict_mem_fill_column_struct(column, pos, mtype,
  1105. prtype, col_len);
  1106. }
  1107. return(NULL);
  1108. }
  1109. /********************************************************************//**
  1110. Loads definitions for table columns. */
  1111. static
  1112. void
  1113. dict_load_columns(
  1114. /*==============*/
  1115. dict_table_t* table, /*!< in/out: table */
  1116. mem_heap_t* heap) /*!< in/out: memory heap
  1117. for temporary storage */
  1118. {
  1119. dict_table_t* sys_columns;
  1120. dict_index_t* sys_index;
  1121. btr_pcur_t pcur;
  1122. dtuple_t* tuple;
  1123. dfield_t* dfield;
  1124. const rec_t* rec;
  1125. byte* buf;
  1126. ulint i;
  1127. mtr_t mtr;
  1128. ut_ad(mutex_own(&(dict_sys->mutex)));
  1129. mtr_start(&mtr);
  1130. sys_columns = dict_table_get_low("SYS_COLUMNS");
  1131. sys_index = UT_LIST_GET_FIRST(sys_columns->indexes);
  1132. ut_ad(!dict_table_is_comp(sys_columns));
  1133. ut_ad(name_of_col_is(sys_columns, sys_index,
  1134. DICT_FLD__SYS_COLUMNS__NAME, "NAME"));
  1135. ut_ad(name_of_col_is(sys_columns, sys_index,
  1136. DICT_FLD__SYS_COLUMNS__PREC, "PREC"));
  1137. tuple = dtuple_create(heap, 1);
  1138. dfield = dtuple_get_nth_field(tuple, 0);
  1139. buf = static_cast<byte*>(mem_heap_alloc(heap, 8));
  1140. mach_write_to_8(buf, table->id);
  1141. dfield_set_data(dfield, buf, 8);
  1142. dict_index_copy_types(tuple, sys_index, 1);
  1143. btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
  1144. BTR_SEARCH_LEAF, &pcur, &mtr);
  1145. for (i = 0; i + DATA_N_SYS_COLS < (ulint) table->n_cols; i++) {
  1146. const char* err_msg;
  1147. const char* name = NULL;
  1148. rec = btr_pcur_get_rec(&pcur);
  1149. ut_a(btr_pcur_is_on_user_rec(&pcur));
  1150. err_msg = dict_load_column_low(table, heap, NULL, NULL,
  1151. &name, rec);
  1152. if (err_msg) {
  1153. fprintf(stderr, "InnoDB: %s\n", err_msg);
  1154. ut_error;
  1155. }
  1156. /* Note: Currently we have one DOC_ID column that is
  1157. shared by all FTS indexes on a table. */
  1158. if (innobase_strcasecmp(name,
  1159. FTS_DOC_ID_COL_NAME) == 0) {
  1160. dict_col_t* col;
  1161. /* As part of normal loading of tables the
  1162. table->flag is not set for tables with FTS
  1163. till after the FTS indexes are loaded. So we
  1164. create the fts_t instance here if there isn't
  1165. one already created.
  1166. This case does not arise for table create as
  1167. the flag is set before the table is created. */
  1168. if (table->fts == NULL) {
  1169. table->fts = fts_create(table);
  1170. fts_optimize_add_table(table);
  1171. }
  1172. ut_a(table->fts->doc_col == ULINT_UNDEFINED);
  1173. col = dict_table_get_nth_col(table, i);
  1174. ut_ad(col->len == sizeof(doc_id_t));
  1175. if (col->prtype & DATA_FTS_DOC_ID) {
  1176. DICT_TF2_FLAG_SET(
  1177. table, DICT_TF2_FTS_HAS_DOC_ID);
  1178. DICT_TF2_FLAG_UNSET(
  1179. table, DICT_TF2_FTS_ADD_DOC_ID);
  1180. }
  1181. table->fts->doc_col = i;
  1182. }
  1183. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  1184. }
  1185. btr_pcur_close(&pcur);
  1186. mtr_commit(&mtr);
  1187. }
  1188. /** Error message for a delete-marked record in dict_load_field_low() */
  1189. static const char* dict_load_field_del = "delete-marked record in SYS_FIELDS";
  1190. /********************************************************************//**
  1191. Loads an index field definition from a SYS_FIELDS record to
  1192. dict_index_t.
  1193. @return error message, or NULL on success */
  1194. UNIV_INTERN
  1195. const char*
  1196. dict_load_field_low(
  1197. /*================*/
  1198. byte* index_id, /*!< in/out: index id (8 bytes)
  1199. an "in" value if index != NULL
  1200. and "out" if index == NULL */
  1201. dict_index_t* index, /*!< in/out: index, could be NULL
  1202. if we just populate a dict_field_t
  1203. struct with information from
  1204. a SYS_FIELDS record */
  1205. dict_field_t* sys_field, /*!< out: dict_field_t to be
  1206. filled */
  1207. ulint* pos, /*!< out: Field position */
  1208. byte* last_index_id, /*!< in: last index id */
  1209. mem_heap_t* heap, /*!< in/out: memory heap
  1210. for temporary storage */
  1211. const rec_t* rec) /*!< in: SYS_FIELDS record */
  1212. {
  1213. const byte* field;
  1214. ulint len;
  1215. ulint pos_and_prefix_len;
  1216. ulint prefix_len;
  1217. ibool first_field;
  1218. ulint position;
  1219. /* Either index or sys_field is supplied, not both */
  1220. ut_a((!index) || (!sys_field));
  1221. if (rec_get_deleted_flag(rec, 0)) {
  1222. return(dict_load_field_del);
  1223. }
  1224. if (rec_get_n_fields_old(rec) != DICT_NUM_FIELDS__SYS_FIELDS) {
  1225. return("wrong number of columns in SYS_FIELDS record");
  1226. }
  1227. field = rec_get_nth_field_old(
  1228. rec, DICT_FLD__SYS_FIELDS__INDEX_ID, &len);
  1229. if (len != 8) {
  1230. err_len:
  1231. return("incorrect column length in SYS_FIELDS");
  1232. }
  1233. if (!index) {
  1234. ut_a(last_index_id);
  1235. memcpy(index_id, (const char*) field, 8);
  1236. first_field = memcmp(index_id, last_index_id, 8);
  1237. } else {
  1238. first_field = (index->n_def == 0);
  1239. if (memcmp(field, index_id, 8)) {
  1240. return("SYS_FIELDS.INDEX_ID mismatch");
  1241. }
  1242. }
  1243. /* The next field stores the field position in the index and a
  1244. possible column prefix length if the index field does not
  1245. contain the whole column. The storage format is like this: if
  1246. there is at least one prefix field in the index, then the HIGH
  1247. 2 bytes contain the field number (index->n_def) and the low 2
  1248. bytes the prefix length for the field. Otherwise the field
  1249. number (index->n_def) is contained in the 2 LOW bytes. */
  1250. field = rec_get_nth_field_old(
  1251. rec, DICT_FLD__SYS_FIELDS__POS, &len);
  1252. if (len != 4) {
  1253. goto err_len;
  1254. }
  1255. pos_and_prefix_len = mach_read_from_4(field);
  1256. if (index && UNIV_UNLIKELY
  1257. ((pos_and_prefix_len & 0xFFFFUL) != index->n_def
  1258. && (pos_and_prefix_len >> 16 & 0xFFFF) != index->n_def)) {
  1259. return("SYS_FIELDS.POS mismatch");
  1260. }
  1261. if (first_field || pos_and_prefix_len > 0xFFFFUL) {
  1262. prefix_len = pos_and_prefix_len & 0xFFFFUL;
  1263. position = (pos_and_prefix_len & 0xFFFF0000UL) >> 16;
  1264. } else {
  1265. prefix_len = 0;
  1266. position = pos_and_prefix_len & 0xFFFFUL;
  1267. }
  1268. rec_get_nth_field_offs_old(
  1269. rec, DICT_FLD__SYS_FIELDS__DB_TRX_ID, &len);
  1270. if (len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL) {
  1271. goto err_len;
  1272. }
  1273. rec_get_nth_field_offs_old(
  1274. rec, DICT_FLD__SYS_FIELDS__DB_ROLL_PTR, &len);
  1275. if (len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL) {
  1276. goto err_len;
  1277. }
  1278. field = rec_get_nth_field_old(
  1279. rec, DICT_FLD__SYS_FIELDS__COL_NAME, &len);
  1280. if (len == 0 || len == UNIV_SQL_NULL) {
  1281. goto err_len;
  1282. }
  1283. if (index) {
  1284. dict_mem_index_add_field(
  1285. index, mem_heap_strdupl(heap, (const char*) field, len),
  1286. prefix_len);
  1287. } else {
  1288. ut_a(sys_field);
  1289. ut_a(pos);
  1290. sys_field->name = mem_heap_strdupl(
  1291. heap, (const char*) field, len);
  1292. sys_field->prefix_len = prefix_len;
  1293. *pos = position;
  1294. }
  1295. return(NULL);
  1296. }
  1297. /********************************************************************//**
  1298. Loads definitions for index fields.
  1299. @return DB_SUCCESS if ok, DB_CORRUPTION if corruption */
  1300. static
  1301. ulint
  1302. dict_load_fields(
  1303. /*=============*/
  1304. dict_index_t* index, /*!< in/out: index whose fields to load */
  1305. mem_heap_t* heap) /*!< in: memory heap for temporary storage */
  1306. {
  1307. dict_table_t* sys_fields;
  1308. dict_index_t* sys_index;
  1309. btr_pcur_t pcur;
  1310. dtuple_t* tuple;
  1311. dfield_t* dfield;
  1312. const rec_t* rec;
  1313. byte* buf;
  1314. ulint i;
  1315. mtr_t mtr;
  1316. dberr_t error;
  1317. ut_ad(mutex_own(&(dict_sys->mutex)));
  1318. mtr_start(&mtr);
  1319. sys_fields = dict_table_get_low("SYS_FIELDS");
  1320. sys_index = UT_LIST_GET_FIRST(sys_fields->indexes);
  1321. ut_ad(!dict_table_is_comp(sys_fields));
  1322. ut_ad(name_of_col_is(sys_fields, sys_index,
  1323. DICT_FLD__SYS_FIELDS__COL_NAME, "COL_NAME"));
  1324. tuple = dtuple_create(heap, 1);
  1325. dfield = dtuple_get_nth_field(tuple, 0);
  1326. buf = static_cast<byte*>(mem_heap_alloc(heap, 8));
  1327. mach_write_to_8(buf, index->id);
  1328. dfield_set_data(dfield, buf, 8);
  1329. dict_index_copy_types(tuple, sys_index, 1);
  1330. btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
  1331. BTR_SEARCH_LEAF, &pcur, &mtr);
  1332. for (i = 0; i < index->n_fields; i++) {
  1333. const char* err_msg;
  1334. rec = btr_pcur_get_rec(&pcur);
  1335. ut_a(btr_pcur_is_on_user_rec(&pcur));
  1336. err_msg = dict_load_field_low(buf, index, NULL, NULL, NULL,
  1337. heap, rec);
  1338. if (err_msg == dict_load_field_del) {
  1339. /* There could be delete marked records in
  1340. SYS_FIELDS because SYS_FIELDS.INDEX_ID can be
  1341. updated by ALTER TABLE ADD INDEX. */
  1342. goto next_rec;
  1343. } else if (err_msg) {
  1344. fprintf(stderr, "InnoDB: %s\n", err_msg);
  1345. error = DB_CORRUPTION;
  1346. goto func_exit;
  1347. }
  1348. next_rec:
  1349. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  1350. }
  1351. error = DB_SUCCESS;
  1352. func_exit:
  1353. btr_pcur_close(&pcur);
  1354. mtr_commit(&mtr);
  1355. return(error);
  1356. }
  1357. /** Error message for a delete-marked record in dict_load_index_low() */
  1358. static const char* dict_load_index_del = "delete-marked record in SYS_INDEXES";
  1359. /** Error message for table->id mismatch in dict_load_index_low() */
  1360. static const char* dict_load_index_id_err = "SYS_INDEXES.TABLE_ID mismatch";
  1361. /********************************************************************//**
  1362. Loads an index definition from a SYS_INDEXES record to dict_index_t.
  1363. If allocate=TRUE, we will create a dict_index_t structure and fill it
  1364. accordingly. If allocated=FALSE, the dict_index_t will be supplied by
  1365. the caller and filled with information read from the record. @return
  1366. error message, or NULL on success */
  1367. UNIV_INTERN
  1368. const char*
  1369. dict_load_index_low(
  1370. /*================*/
  1371. byte* table_id, /*!< in/out: table id (8 bytes),
  1372. an "in" value if allocate=TRUE
  1373. and "out" when allocate=FALSE */
  1374. const char* table_name, /*!< in: table name */
  1375. mem_heap_t* heap, /*!< in/out: temporary memory heap */
  1376. const rec_t* rec, /*!< in: SYS_INDEXES record */
  1377. ibool allocate, /*!< in: TRUE=allocate *index,
  1378. FALSE=fill in a pre-allocated
  1379. *index */
  1380. dict_index_t** index) /*!< out,own: index, or NULL */
  1381. {
  1382. const byte* field;
  1383. ulint len;
  1384. ulint name_len;
  1385. char* name_buf;
  1386. index_id_t id;
  1387. ulint n_fields;
  1388. ulint type;
  1389. ulint space;
  1390. if (allocate) {
  1391. /* If allocate=TRUE, no dict_index_t will
  1392. be supplied. Initialize "*index" to NULL */
  1393. *index = NULL;
  1394. }
  1395. if (rec_get_deleted_flag(rec, 0)) {
  1396. return(dict_load_index_del);
  1397. }
  1398. if (rec_get_n_fields_old(rec) != DICT_NUM_FIELDS__SYS_INDEXES) {
  1399. return("wrong number of columns in SYS_INDEXES record");
  1400. }
  1401. field = rec_get_nth_field_old(
  1402. rec, DICT_FLD__SYS_INDEXES__TABLE_ID, &len);
  1403. if (len != 8) {
  1404. err_len:
  1405. return("incorrect column length in SYS_INDEXES");
  1406. }
  1407. if (!allocate) {
  1408. /* We are reading a SYS_INDEXES record. Copy the table_id */
  1409. memcpy(table_id, (const char*) field, 8);
  1410. } else if (memcmp(field, table_id, 8)) {
  1411. /* Caller supplied table_id, verify it is the same
  1412. id as on the index record */
  1413. return(dict_load_index_id_err);
  1414. }
  1415. field = rec_get_nth_field_old(
  1416. rec, DICT_FLD__SYS_INDEXES__ID, &len);
  1417. if (len != 8) {
  1418. goto err_len;
  1419. }
  1420. id = mach_read_from_8(field);
  1421. rec_get_nth_field_offs_old(
  1422. rec, DICT_FLD__SYS_INDEXES__DB_TRX_ID, &len);
  1423. if (len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL) {
  1424. goto err_len;
  1425. }
  1426. rec_get_nth_field_offs_old(
  1427. rec, DICT_FLD__SYS_INDEXES__DB_ROLL_PTR, &len);
  1428. if (len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL) {
  1429. goto err_len;
  1430. }
  1431. field = rec_get_nth_field_old(
  1432. rec, DICT_FLD__SYS_INDEXES__NAME, &name_len);
  1433. if (name_len == UNIV_SQL_NULL) {
  1434. goto err_len;
  1435. }
  1436. name_buf = mem_heap_strdupl(heap, (const char*) field,
  1437. name_len);
  1438. field = rec_get_nth_field_old(
  1439. rec, DICT_FLD__SYS_INDEXES__N_FIELDS, &len);
  1440. if (len != 4) {
  1441. goto err_len;
  1442. }
  1443. n_fields = mach_read_from_4(field);
  1444. field = rec_get_nth_field_old(
  1445. rec, DICT_FLD__SYS_INDEXES__TYPE, &len);
  1446. if (len != 4) {
  1447. goto err_len;
  1448. }
  1449. type = mach_read_from_4(field);
  1450. if (type & (~0 << DICT_IT_BITS)) {
  1451. return("unknown SYS_INDEXES.TYPE bits");
  1452. }
  1453. field = rec_get_nth_field_old(
  1454. rec, DICT_FLD__SYS_INDEXES__SPACE, &len);
  1455. if (len != 4) {
  1456. goto err_len;
  1457. }
  1458. space = mach_read_from_4(field);
  1459. field = rec_get_nth_field_old(
  1460. rec, DICT_FLD__SYS_INDEXES__PAGE_NO, &len);
  1461. if (len != 4) {
  1462. goto err_len;
  1463. }
  1464. if (allocate) {
  1465. *index = dict_mem_index_create(table_name, name_buf,
  1466. space, type, n_fields);
  1467. } else {
  1468. ut_a(*index);
  1469. dict_mem_fill_index_struct(*index, NULL, NULL, name_buf,
  1470. space, type, n_fields);
  1471. }
  1472. (*index)->id = id;
  1473. (*index)->page = mach_read_from_4(field);
  1474. btr_search_index_init(*index);
  1475. ut_ad((*index)->page);
  1476. return(NULL);
  1477. }
  1478. /********************************************************************//**
  1479. Loads definitions for table indexes. Adds them to the data dictionary
  1480. cache.
  1481. @return DB_SUCCESS if ok, DB_CORRUPTION if corruption of dictionary
  1482. table or DB_UNSUPPORTED if table has unknown index type */
  1483. static __attribute__((nonnull))
  1484. dberr_t
  1485. dict_load_indexes(
  1486. /*==============*/
  1487. dict_table_t* table, /*!< in/out: table */
  1488. mem_heap_t* heap, /*!< in: memory heap for temporary storage */
  1489. dict_err_ignore_t ignore_err)
  1490. /*!< in: error to be ignored when
  1491. loading the index definition */
  1492. {
  1493. dict_table_t* sys_indexes;
  1494. dict_index_t* sys_index;
  1495. btr_pcur_t pcur;
  1496. dtuple_t* tuple;
  1497. dfield_t* dfield;
  1498. const rec_t* rec;
  1499. byte* buf;
  1500. mtr_t mtr;
  1501. dberr_t error = DB_SUCCESS;
  1502. ut_ad(mutex_own(&(dict_sys->mutex)));
  1503. mtr_start(&mtr);
  1504. sys_indexes = dict_table_get_low("SYS_INDEXES");
  1505. sys_index = UT_LIST_GET_FIRST(sys_indexes->indexes);
  1506. ut_ad(!dict_table_is_comp(sys_indexes));
  1507. ut_ad(name_of_col_is(sys_indexes, sys_index,
  1508. DICT_FLD__SYS_INDEXES__NAME, "NAME"));
  1509. ut_ad(name_of_col_is(sys_indexes, sys_index,
  1510. DICT_FLD__SYS_INDEXES__PAGE_NO, "PAGE_NO"));
  1511. tuple = dtuple_create(heap, 1);
  1512. dfield = dtuple_get_nth_field(tuple, 0);
  1513. buf = static_cast<byte*>(mem_heap_alloc(heap, 8));
  1514. mach_write_to_8(buf, table->id);
  1515. dfield_set_data(dfield, buf, 8);
  1516. dict_index_copy_types(tuple, sys_index, 1);
  1517. btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
  1518. BTR_SEARCH_LEAF, &pcur, &mtr);
  1519. for (;;) {
  1520. dict_index_t* index = NULL;
  1521. const char* err_msg;
  1522. if (!btr_pcur_is_on_user_rec(&pcur)) {
  1523. /* We should allow the table to open even
  1524. without index when DICT_ERR_IGNORE_CORRUPT is set.
  1525. DICT_ERR_IGNORE_CORRUPT is currently only set
  1526. for drop table */
  1527. if (dict_table_get_first_index(table) == NULL
  1528. && !(ignore_err & DICT_ERR_IGNORE_CORRUPT)) {
  1529. ib_logf(IB_LOG_LEVEL_WARN,
  1530. "Cannot load table %s "
  1531. "because it has no indexes in "
  1532. "InnoDB internal data dictionary.",
  1533. table->name);
  1534. error = DB_CORRUPTION;
  1535. goto func_exit;
  1536. }
  1537. break;
  1538. }
  1539. rec = btr_pcur_get_rec(&pcur);
  1540. if ((ignore_err & DICT_ERR_IGNORE_RECOVER_LOCK)
  1541. && rec_get_n_fields_old(rec)
  1542. == DICT_NUM_FIELDS__SYS_INDEXES) {
  1543. const byte* field;
  1544. ulint len;
  1545. field = rec_get_nth_field_old(
  1546. rec, DICT_FLD__SYS_INDEXES__NAME, &len);
  1547. if (len != UNIV_SQL_NULL
  1548. && char(*field) == char(TEMP_INDEX_PREFIX)) {
  1549. /* Skip indexes whose name starts with
  1550. TEMP_INDEX_PREFIX, because they will
  1551. be dropped during crash recovery. */
  1552. goto next_rec;
  1553. }
  1554. }
  1555. err_msg = dict_load_index_low(buf, table->name, heap, rec,
  1556. TRUE, &index);
  1557. ut_ad((index == NULL && err_msg != NULL)
  1558. || (index != NULL && err_msg == NULL));
  1559. if (err_msg == dict_load_index_id_err) {
  1560. /* TABLE_ID mismatch means that we have
  1561. run out of index definitions for the table. */
  1562. if (dict_table_get_first_index(table) == NULL
  1563. && !(ignore_err & DICT_ERR_IGNORE_CORRUPT)) {
  1564. ib_logf(IB_LOG_LEVEL_WARN,
  1565. "Failed to load the "
  1566. "clustered index for table %s "
  1567. "because of the following error: %s. "
  1568. "Refusing to load the rest of the "
  1569. "indexes (if any) and the whole table "
  1570. "altogether.", table->name, err_msg);
  1571. error = DB_CORRUPTION;
  1572. goto func_exit;
  1573. }
  1574. break;
  1575. } else if (err_msg == dict_load_index_del) {
  1576. /* Skip delete-marked records. */
  1577. goto next_rec;
  1578. } else if (err_msg) {
  1579. fprintf(stderr, "InnoDB: %s\n", err_msg);
  1580. if (ignore_err & DICT_ERR_IGNORE_CORRUPT) {
  1581. goto next_rec;
  1582. }
  1583. error = DB_CORRUPTION;
  1584. goto func_exit;
  1585. }
  1586. ut_ad(index);
  1587. /* Check whether the index is corrupted */
  1588. if (dict_index_is_corrupted(index)) {
  1589. ut_print_timestamp(stderr);
  1590. fputs(" InnoDB: ", stderr);
  1591. dict_index_name_print(stderr, NULL, index);
  1592. fputs(" is corrupted\n", stderr);
  1593. if (!srv_load_corrupted
  1594. && !(ignore_err & DICT_ERR_IGNORE_CORRUPT)
  1595. && dict_index_is_clust(index)) {
  1596. dict_mem_index_free(index);
  1597. error = DB_INDEX_CORRUPT;
  1598. goto func_exit;
  1599. } else {
  1600. /* We will load the index if
  1601. 1) srv_load_corrupted is TRUE
  1602. 2) ignore_err is set with
  1603. DICT_ERR_IGNORE_CORRUPT
  1604. 3) if the index corrupted is a secondary
  1605. index */
  1606. ut_print_timestamp(stderr);
  1607. fputs(" InnoDB: load corrupted index ", stderr);
  1608. dict_index_name_print(stderr, NULL, index);
  1609. putc('\n', stderr);
  1610. }
  1611. }
  1612. if (index->type & DICT_FTS
  1613. && !DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS)) {
  1614. /* This should have been created by now. */
  1615. ut_a(table->fts != NULL);
  1616. DICT_TF2_FLAG_SET(table, DICT_TF2_FTS);
  1617. }
  1618. /* We check for unsupported types first, so that the
  1619. subsequent checks are relevant for the supported types. */
  1620. if (index->type & ~(DICT_CLUSTERED | DICT_UNIQUE
  1621. | DICT_CORRUPT | DICT_FTS)) {
  1622. ib_logf(IB_LOG_LEVEL_ERROR,
  1623. "Unknown type %lu of index %s of table %s",
  1624. (ulong) index->type, index->name, table->name);
  1625. error = DB_UNSUPPORTED;
  1626. dict_mem_index_free(index);
  1627. goto func_exit;
  1628. } else if (index->page == FIL_NULL
  1629. && !table->ibd_file_missing
  1630. && (!(index->type & DICT_FTS))) {
  1631. fprintf(stderr,
  1632. "InnoDB: Error: trying to load index %s"
  1633. " for table %s\n"
  1634. "InnoDB: but the index tree has been freed!\n",
  1635. index->name, table->name);
  1636. if (ignore_err & DICT_ERR_IGNORE_INDEX_ROOT) {
  1637. /* If caller can tolerate this error,
  1638. we will continue to load the index and
  1639. let caller deal with this error. However
  1640. mark the index and table corrupted. We
  1641. only need to mark such in the index
  1642. dictionary cache for such metadata corruption,
  1643. since we would always be able to set it
  1644. when loading the dictionary cache */
  1645. dict_set_corrupted_index_cache_only(
  1646. index, table);
  1647. fprintf(stderr,
  1648. "InnoDB: Index is corrupt but forcing"
  1649. " load into data dictionary\n");
  1650. } else {
  1651. corrupted:
  1652. dict_mem_index_free(index);
  1653. error = DB_CORRUPTION;
  1654. goto func_exit;
  1655. }
  1656. } else if (!dict_index_is_clust(index)
  1657. && NULL == dict_table_get_first_index(table)) {
  1658. fputs("InnoDB: Error: trying to load index ",
  1659. stderr);
  1660. ut_print_name(stderr, NULL, FALSE, index->name);
  1661. fputs(" for table ", stderr);
  1662. ut_print_name(stderr, NULL, TRUE, table->name);
  1663. fputs("\nInnoDB: but the first index"
  1664. " is not clustered!\n", stderr);
  1665. goto corrupted;
  1666. } else if (dict_is_sys_table(table->id)
  1667. && (dict_index_is_clust(index)
  1668. || ((table == dict_sys->sys_tables)
  1669. && !strcmp("ID_IND", index->name)))) {
  1670. /* The index was created in memory already at booting
  1671. of the database server */
  1672. dict_mem_index_free(index);
  1673. } else {
  1674. dict_load_fields(index, heap);
  1675. error = dict_index_add_to_cache(
  1676. table, index, index->page, FALSE);
  1677. /* The data dictionary tables should never contain
  1678. invalid index definitions. If we ignored this error
  1679. and simply did not load this index definition, the
  1680. .frm file would disagree with the index definitions
  1681. inside InnoDB. */
  1682. if (UNIV_UNLIKELY(error != DB_SUCCESS)) {
  1683. goto func_exit;
  1684. }
  1685. }
  1686. next_rec:
  1687. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  1688. }
  1689. /* If the table contains FTS indexes, populate table->fts->indexes */
  1690. if (DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS)) {
  1691. /* table->fts->indexes should have been created. */
  1692. ut_a(table->fts->indexes != NULL);
  1693. dict_table_get_all_fts_indexes(table, table->fts->indexes);
  1694. }
  1695. func_exit:
  1696. btr_pcur_close(&pcur);
  1697. mtr_commit(&mtr);
  1698. return(error);
  1699. }
  1700. /********************************************************************//**
  1701. Loads a table definition from a SYS_TABLES record to dict_table_t.
  1702. Does not load any columns or indexes.
  1703. @return error message, or NULL on success */
  1704. UNIV_INTERN
  1705. const char*
  1706. dict_load_table_low(
  1707. /*================*/
  1708. const char* name, /*!< in: table name */
  1709. const rec_t* rec, /*!< in: SYS_TABLES record */
  1710. dict_table_t** table) /*!< out,own: table, or NULL */
  1711. {
  1712. const byte* field;
  1713. ulint len;
  1714. ulint space;
  1715. ulint n_cols;
  1716. ulint flags = 0;
  1717. ulint flags2;
  1718. if (rec_get_deleted_flag(rec, 0)) {
  1719. return("delete-marked record in SYS_TABLES");
  1720. }
  1721. if (rec_get_n_fields_old(rec) != DICT_NUM_FIELDS__SYS_TABLES) {
  1722. return("wrong number of columns in SYS_TABLES record");
  1723. }
  1724. rec_get_nth_field_offs_old(
  1725. rec, DICT_FLD__SYS_TABLES__NAME, &len);
  1726. if (len == 0 || len == UNIV_SQL_NULL) {
  1727. err_len:
  1728. return("incorrect column length in SYS_TABLES");
  1729. }
  1730. rec_get_nth_field_offs_old(
  1731. rec, DICT_FLD__SYS_TABLES__DB_TRX_ID, &len);
  1732. if (len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL) {
  1733. goto err_len;
  1734. }
  1735. rec_get_nth_field_offs_old(
  1736. rec, DICT_FLD__SYS_TABLES__DB_ROLL_PTR, &len);
  1737. if (len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL) {
  1738. goto err_len;
  1739. }
  1740. rec_get_nth_field_offs_old(rec, DICT_FLD__SYS_TABLES__ID, &len);
  1741. if (len != 8) {
  1742. goto err_len;
  1743. }
  1744. field = rec_get_nth_field_old(
  1745. rec, DICT_FLD__SYS_TABLES__N_COLS, &len);
  1746. if (len != 4) {
  1747. goto err_len;
  1748. }
  1749. n_cols = mach_read_from_4(field);
  1750. rec_get_nth_field_offs_old(rec, DICT_FLD__SYS_TABLES__TYPE, &len);
  1751. if (len != 4) {
  1752. goto err_len;
  1753. }
  1754. rec_get_nth_field_offs_old(
  1755. rec, DICT_FLD__SYS_TABLES__MIX_ID, &len);
  1756. if (len != 8) {
  1757. goto err_len;
  1758. }
  1759. field = rec_get_nth_field_old(
  1760. rec, DICT_FLD__SYS_TABLES__MIX_LEN, &len);
  1761. if (len != 4) {
  1762. goto err_len;
  1763. }
  1764. /* MIX_LEN may hold additional flags in post-antelope file formats. */
  1765. flags2 = mach_read_from_4(field);
  1766. /* DICT_TF2_FTS will be set when indexes is being loaded */
  1767. flags2 &= ~DICT_TF2_FTS;
  1768. rec_get_nth_field_offs_old(
  1769. rec, DICT_FLD__SYS_TABLES__CLUSTER_ID, &len);
  1770. if (len != UNIV_SQL_NULL) {
  1771. goto err_len;
  1772. }
  1773. field = rec_get_nth_field_old(
  1774. rec, DICT_FLD__SYS_TABLES__SPACE, &len);
  1775. if (len != 4) {
  1776. goto err_len;
  1777. }
  1778. space = mach_read_from_4(field);
  1779. /* Check if the tablespace exists and has the right name */
  1780. flags = dict_sys_tables_get_flags(rec);
  1781. if (UNIV_UNLIKELY(flags == ULINT_UNDEFINED)) {
  1782. field = rec_get_nth_field_old(
  1783. rec, DICT_FLD__SYS_TABLES__TYPE, &len);
  1784. ut_ad(len == 4); /* this was checked earlier */
  1785. flags = mach_read_from_4(field);
  1786. ut_print_timestamp(stderr);
  1787. fputs(" InnoDB: Error: table ", stderr);
  1788. ut_print_filename(stderr, name);
  1789. fprintf(stderr, "\n"
  1790. "InnoDB: in InnoDB data dictionary"
  1791. " has unknown type %lx.\n",
  1792. (ulong) flags);
  1793. return("incorrect flags in SYS_TABLES");
  1794. }
  1795. /* The high-order bit of N_COLS is the "compact format" flag.
  1796. For tables in that format, MIX_LEN may hold additional flags. */
  1797. if (n_cols & DICT_N_COLS_COMPACT) {
  1798. ut_ad(flags & DICT_TF_COMPACT);
  1799. if (flags2 & ~DICT_TF2_BIT_MASK) {
  1800. ut_print_timestamp(stderr);
  1801. fputs(" InnoDB: Warning: table ", stderr);
  1802. ut_print_filename(stderr, name);
  1803. fprintf(stderr, "\n"
  1804. "InnoDB: in InnoDB data dictionary"
  1805. " has unknown flags %lx.\n",
  1806. (ulong) flags2);
  1807. /* Clean it up and keep going */
  1808. flags2 &= DICT_TF2_BIT_MASK;
  1809. }
  1810. } else {
  1811. /* Do not trust the MIX_LEN field when the
  1812. row format is Redundant. */
  1813. flags2 = 0;
  1814. }
  1815. /* See if the tablespace is available. */
  1816. *table = dict_mem_table_create(
  1817. name, space, n_cols & ~DICT_N_COLS_COMPACT, flags, flags2);
  1818. field = rec_get_nth_field_old(rec, DICT_FLD__SYS_TABLES__ID, &len);
  1819. ut_ad(len == 8); /* this was checked earlier */
  1820. (*table)->id = mach_read_from_8(field);
  1821. (*table)->ibd_file_missing = FALSE;
  1822. return(NULL);
  1823. }
  1824. /********************************************************************//**
  1825. Using the table->heap, copy the null-terminated filepath into
  1826. table->data_dir_path and replace the 'databasename/tablename.ibd'
  1827. portion with 'tablename'.
  1828. This allows SHOW CREATE TABLE to return the correct DATA DIRECTORY path.
  1829. Make this data directory path only if it has not yet been saved. */
  1830. UNIV_INTERN
  1831. void
  1832. dict_save_data_dir_path(
  1833. /*====================*/
  1834. dict_table_t* table, /*!< in/out: table */
  1835. char* filepath) /*!< in: filepath of tablespace */
  1836. {
  1837. ut_ad(mutex_own(&(dict_sys->mutex)));
  1838. ut_a(DICT_TF_HAS_DATA_DIR(table->flags));
  1839. ut_a(!table->data_dir_path);
  1840. ut_a(filepath);
  1841. /* Be sure this filepath is not the default filepath. */
  1842. char* default_filepath = fil_make_ibd_name(table->name, false);
  1843. if (strcmp(filepath, default_filepath)) {
  1844. ulint pathlen = strlen(filepath);
  1845. ut_a(pathlen < OS_FILE_MAX_PATH);
  1846. ut_a(0 == strcmp(filepath + pathlen - 4, ".ibd"));
  1847. table->data_dir_path = mem_heap_strdup(table->heap, filepath);
  1848. os_file_make_data_dir_path(table->data_dir_path);
  1849. } else {
  1850. /* This does not change SYS_DATAFILES or SYS_TABLES
  1851. or FSP_FLAGS on the header page of the tablespace,
  1852. but it makes dict_table_t consistent */
  1853. table->flags &= ~DICT_TF_MASK_DATA_DIR;
  1854. }
  1855. mem_free(default_filepath);
  1856. }
  1857. /*****************************************************************//**
  1858. Make sure the data_file_name is saved in dict_table_t if needed. Try to
  1859. read it from the file dictionary first, then from SYS_DATAFILES. */
  1860. UNIV_INTERN
  1861. void
  1862. dict_get_and_save_data_dir_path(
  1863. /*============================*/
  1864. dict_table_t* table, /*!< in/out: table */
  1865. bool dict_mutex_own) /*!< in: true if dict_sys->mutex
  1866. is owned already */
  1867. {
  1868. bool is_temp = DICT_TF2_FLAG_IS_SET(table, DICT_TF2_TEMPORARY);
  1869. if (!is_temp && !table->data_dir_path && table->space) {
  1870. char* path = fil_space_get_first_path(table->space);
  1871. if (!dict_mutex_own) {
  1872. dict_mutex_enter_for_mysql();
  1873. }
  1874. if (!path) {
  1875. path = dict_get_first_path(
  1876. table->space, table->name);
  1877. }
  1878. if (path) {
  1879. table->flags |= (1 << DICT_TF_POS_DATA_DIR);
  1880. dict_save_data_dir_path(table, path);
  1881. mem_free(path);
  1882. }
  1883. if (!dict_mutex_own) {
  1884. dict_mutex_exit_for_mysql();
  1885. }
  1886. }
  1887. }
  1888. /********************************************************************//**
  1889. Loads a table definition and also all its index definitions, and also
  1890. the cluster definition if the table is a member in a cluster. Also loads
  1891. all foreign key constraints where the foreign key is in the table or where
  1892. a foreign key references columns in this table. Adds all these to the data
  1893. dictionary cache.
  1894. @return table, NULL if does not exist; if the table is stored in an
  1895. .ibd file, but the file does not exist, then we set the
  1896. ibd_file_missing flag TRUE in the table object we return */
  1897. UNIV_INTERN
  1898. dict_table_t*
  1899. dict_load_table(
  1900. /*============*/
  1901. const char* name, /*!< in: table name in the
  1902. databasename/tablename format */
  1903. ibool cached, /*!< in: TRUE=add to cache, FALSE=do not */
  1904. dict_err_ignore_t ignore_err)
  1905. /*!< in: error to be ignored when loading
  1906. table and its indexes' definition */
  1907. {
  1908. dberr_t err;
  1909. dict_table_t* table;
  1910. dict_table_t* sys_tables;
  1911. btr_pcur_t pcur;
  1912. dict_index_t* sys_index;
  1913. dtuple_t* tuple;
  1914. mem_heap_t* heap;
  1915. dfield_t* dfield;
  1916. const rec_t* rec;
  1917. const byte* field;
  1918. ulint len;
  1919. char* filepath = NULL;
  1920. const char* err_msg;
  1921. mtr_t mtr;
  1922. ut_ad(mutex_own(&(dict_sys->mutex)));
  1923. heap = mem_heap_create(32000);
  1924. mtr_start(&mtr);
  1925. sys_tables = dict_table_get_low("SYS_TABLES");
  1926. sys_index = UT_LIST_GET_FIRST(sys_tables->indexes);
  1927. ut_ad(!dict_table_is_comp(sys_tables));
  1928. ut_ad(name_of_col_is(sys_tables, sys_index,
  1929. DICT_FLD__SYS_TABLES__ID, "ID"));
  1930. ut_ad(name_of_col_is(sys_tables, sys_index,
  1931. DICT_FLD__SYS_TABLES__N_COLS, "N_COLS"));
  1932. ut_ad(name_of_col_is(sys_tables, sys_index,
  1933. DICT_FLD__SYS_TABLES__TYPE, "TYPE"));
  1934. ut_ad(name_of_col_is(sys_tables, sys_index,
  1935. DICT_FLD__SYS_TABLES__MIX_LEN, "MIX_LEN"));
  1936. ut_ad(name_of_col_is(sys_tables, sys_index,
  1937. DICT_FLD__SYS_TABLES__SPACE, "SPACE"));
  1938. tuple = dtuple_create(heap, 1);
  1939. dfield = dtuple_get_nth_field(tuple, 0);
  1940. dfield_set_data(dfield, name, ut_strlen(name));
  1941. dict_index_copy_types(tuple, sys_index, 1);
  1942. btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
  1943. BTR_SEARCH_LEAF, &pcur, &mtr);
  1944. rec = btr_pcur_get_rec(&pcur);
  1945. if (!btr_pcur_is_on_user_rec(&pcur)
  1946. || rec_get_deleted_flag(rec, 0)) {
  1947. /* Not found */
  1948. err_exit:
  1949. btr_pcur_close(&pcur);
  1950. mtr_commit(&mtr);
  1951. mem_heap_free(heap);
  1952. return(NULL);
  1953. }
  1954. field = rec_get_nth_field_old(
  1955. rec, DICT_FLD__SYS_TABLES__NAME, &len);
  1956. /* Check if the table name in record is the searched one */
  1957. if (len != ut_strlen(name) || ut_memcmp(name, field, len) != 0) {
  1958. goto err_exit;
  1959. }
  1960. err_msg = dict_load_table_low(name, rec, &table);
  1961. if (err_msg) {
  1962. ut_print_timestamp(stderr);
  1963. fprintf(stderr, " InnoDB: %s\n", err_msg);
  1964. goto err_exit;
  1965. }
  1966. char table_name[MAX_FULL_NAME_LEN + 1];
  1967. innobase_format_name(table_name, sizeof(table_name), name, FALSE);
  1968. btr_pcur_close(&pcur);
  1969. mtr_commit(&mtr);
  1970. if (table->space == 0) {
  1971. /* The system tablespace is always available. */
  1972. } else if (table->flags2 & DICT_TF2_DISCARDED) {
  1973. ib_logf(IB_LOG_LEVEL_WARN,
  1974. "Table '%s' tablespace is set as discarded.",
  1975. table_name);
  1976. table->ibd_file_missing = TRUE;
  1977. } else if (!fil_space_for_table_exists_in_mem(
  1978. table->space, name, FALSE, FALSE, true, heap,
  1979. table->id)) {
  1980. if (DICT_TF2_FLAG_IS_SET(table, DICT_TF2_TEMPORARY)) {
  1981. /* Do not bother to retry opening temporary tables. */
  1982. table->ibd_file_missing = TRUE;
  1983. } else {
  1984. if (!(ignore_err & DICT_ERR_IGNORE_RECOVER_LOCK)) {
  1985. ib_logf(IB_LOG_LEVEL_ERROR,
  1986. "Failed to find tablespace for "
  1987. "table '%s' in the cache. "
  1988. "Attempting to load the tablespace "
  1989. "with space id %lu.",
  1990. table_name, (ulong) table->space);
  1991. }
  1992. /* Use the remote filepath if needed. */
  1993. /* This needs to be added to the table
  1994. from SYS_DATAFILES */
  1995. dict_get_and_save_data_dir_path(table, true);
  1996. if (table->data_dir_path) {
  1997. filepath = os_file_make_remote_pathname(
  1998. table->data_dir_path,
  1999. table->name, "ibd");
  2000. }
  2001. /* Try to open the tablespace. We set the
  2002. 2nd param (fix_dict = false) here because we
  2003. do not have an x-lock on dict_operation_lock */
  2004. err = fil_open_single_table_tablespace(
  2005. true, false, table->space,
  2006. dict_tf_to_fsp_flags(table->flags),
  2007. name, filepath, table);
  2008. if (err != DB_SUCCESS) {
  2009. /* We failed to find a sensible
  2010. tablespace file */
  2011. table->ibd_file_missing = TRUE;
  2012. }
  2013. if (filepath) {
  2014. mem_free(filepath);
  2015. }
  2016. }
  2017. }
  2018. dict_load_columns(table, heap);
  2019. if (cached) {
  2020. dict_table_add_to_cache(table, TRUE, heap);
  2021. } else {
  2022. dict_table_add_system_columns(table, heap);
  2023. }
  2024. mem_heap_empty(heap);
  2025. /* If there is no tablespace for the table then we only need to
  2026. load the index definitions. So that we can IMPORT the tablespace
  2027. later. When recovering table locks for resurrected incomplete
  2028. transactions, the tablespace should exist, because DDL operations
  2029. were not allowed while the table is being locked by a transaction. */
  2030. dict_err_ignore_t index_load_err =
  2031. !(ignore_err & DICT_ERR_IGNORE_RECOVER_LOCK)
  2032. && table->ibd_file_missing
  2033. ? DICT_ERR_IGNORE_ALL
  2034. : ignore_err;
  2035. err = dict_load_indexes(table, heap, index_load_err);
  2036. if (err == DB_INDEX_CORRUPT) {
  2037. /* Refuse to load the table if the table has a corrupted
  2038. cluster index */
  2039. if (!srv_load_corrupted) {
  2040. fprintf(stderr, "InnoDB: Error: Load table ");
  2041. ut_print_name(stderr, NULL, TRUE, table->name);
  2042. fprintf(stderr, " failed, the table has corrupted"
  2043. " clustered indexes. Turn on"
  2044. " 'innodb_force_load_corrupted'"
  2045. " to drop it\n");
  2046. dict_table_remove_from_cache(table);
  2047. table = NULL;
  2048. goto func_exit;
  2049. } else {
  2050. dict_index_t* clust_index;
  2051. clust_index = dict_table_get_first_index(table);
  2052. if (dict_index_is_corrupted(clust_index)) {
  2053. table->corrupted = TRUE;
  2054. }
  2055. }
  2056. }
  2057. /* Initialize table foreign_child value. Its value could be
  2058. changed when dict_load_foreigns() is called below */
  2059. table->fk_max_recusive_level = 0;
  2060. /* If the force recovery flag is set, we open the table irrespective
  2061. of the error condition, since the user may want to dump data from the
  2062. clustered index. However we load the foreign key information only if
  2063. all indexes were loaded. */
  2064. if (!cached || table->ibd_file_missing) {
  2065. /* Don't attempt to load the indexes from disk. */
  2066. } else if (err == DB_SUCCESS) {
  2067. err = dict_load_foreigns(table->name, NULL, true, true,
  2068. ignore_err);
  2069. if (err != DB_SUCCESS) {
  2070. ib_logf(IB_LOG_LEVEL_WARN,
  2071. "Load table '%s' failed, the table has missing "
  2072. "foreign key indexes. Turn off "
  2073. "'foreign_key_checks' and try again.",
  2074. table->name);
  2075. dict_table_remove_from_cache(table);
  2076. table = NULL;
  2077. } else {
  2078. table->fk_max_recusive_level = 0;
  2079. }
  2080. } else {
  2081. dict_index_t* index;
  2082. /* Make sure that at least the clustered index was loaded.
  2083. Otherwise refuse to load the table */
  2084. index = dict_table_get_first_index(table);
  2085. if (!srv_force_recovery
  2086. || !index
  2087. || !dict_index_is_clust(index)) {
  2088. dict_table_remove_from_cache(table);
  2089. table = NULL;
  2090. } else if (dict_index_is_corrupted(index)
  2091. && !table->ibd_file_missing) {
  2092. /* It is possible we force to load a corrupted
  2093. clustered index if srv_load_corrupted is set.
  2094. Mark the table as corrupted in this case */
  2095. table->corrupted = TRUE;
  2096. }
  2097. }
  2098. func_exit:
  2099. mem_heap_free(heap);
  2100. ut_ad(!table
  2101. || ignore_err != DICT_ERR_IGNORE_NONE
  2102. || table->ibd_file_missing
  2103. || !table->corrupted);
  2104. if (table && table->fts) {
  2105. if (!(dict_table_has_fts_index(table)
  2106. || DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_HAS_DOC_ID)
  2107. || DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_ADD_DOC_ID))) {
  2108. /* the table->fts could be created in dict_load_column
  2109. when a user defined FTS_DOC_ID is present, but no
  2110. FTS */
  2111. fts_free(table);
  2112. } else {
  2113. fts_optimize_add_table(table);
  2114. }
  2115. }
  2116. ut_ad(err != DB_SUCCESS || dict_foreign_set_validate(*table));
  2117. return(table);
  2118. }
  2119. /***********************************************************************//**
  2120. Loads a table object based on the table id.
  2121. @return table; NULL if table does not exist */
  2122. UNIV_INTERN
  2123. dict_table_t*
  2124. dict_load_table_on_id(
  2125. /*==================*/
  2126. table_id_t table_id, /*!< in: table id */
  2127. dict_err_ignore_t ignore_err) /*!< in: errors to ignore
  2128. when loading the table */
  2129. {
  2130. byte id_buf[8];
  2131. btr_pcur_t pcur;
  2132. mem_heap_t* heap;
  2133. dtuple_t* tuple;
  2134. dfield_t* dfield;
  2135. dict_index_t* sys_table_ids;
  2136. dict_table_t* sys_tables;
  2137. const rec_t* rec;
  2138. const byte* field;
  2139. ulint len;
  2140. dict_table_t* table;
  2141. mtr_t mtr;
  2142. ut_ad(mutex_own(&(dict_sys->mutex)));
  2143. table = NULL;
  2144. /* NOTE that the operation of this function is protected by
  2145. the dictionary mutex, and therefore no deadlocks can occur
  2146. with other dictionary operations. */
  2147. mtr_start(&mtr);
  2148. /*---------------------------------------------------*/
  2149. /* Get the secondary index based on ID for table SYS_TABLES */
  2150. sys_tables = dict_sys->sys_tables;
  2151. sys_table_ids = dict_table_get_next_index(
  2152. dict_table_get_first_index(sys_tables));
  2153. ut_ad(!dict_table_is_comp(sys_tables));
  2154. ut_ad(!dict_index_is_clust(sys_table_ids));
  2155. heap = mem_heap_create(256);
  2156. tuple = dtuple_create(heap, 1);
  2157. dfield = dtuple_get_nth_field(tuple, 0);
  2158. /* Write the table id in byte format to id_buf */
  2159. mach_write_to_8(id_buf, table_id);
  2160. dfield_set_data(dfield, id_buf, 8);
  2161. dict_index_copy_types(tuple, sys_table_ids, 1);
  2162. btr_pcur_open_on_user_rec(sys_table_ids, tuple, PAGE_CUR_GE,
  2163. BTR_SEARCH_LEAF, &pcur, &mtr);
  2164. check_rec:
  2165. rec = btr_pcur_get_rec(&pcur);
  2166. if (page_rec_is_user_rec(rec)) {
  2167. /*---------------------------------------------------*/
  2168. /* Now we have the record in the secondary index
  2169. containing the table ID and NAME */
  2170. field = rec_get_nth_field_old(
  2171. rec, DICT_FLD__SYS_TABLE_IDS__ID, &len);
  2172. ut_ad(len == 8);
  2173. /* Check if the table id in record is the one searched for */
  2174. if (table_id == mach_read_from_8(field)) {
  2175. if (rec_get_deleted_flag(rec, 0)) {
  2176. /* Until purge has completed, there
  2177. may be delete-marked duplicate records
  2178. for the same SYS_TABLES.ID.
  2179. Due to Bug #60049, some delete-marked
  2180. records may survive the purge forever. */
  2181. if (btr_pcur_move_to_next(&pcur, &mtr)) {
  2182. goto check_rec;
  2183. }
  2184. } else {
  2185. /* Now we get the table name from the record */
  2186. field = rec_get_nth_field_old(rec,
  2187. DICT_FLD__SYS_TABLE_IDS__NAME, &len);
  2188. /* Load the table definition to memory */
  2189. table = dict_load_table(
  2190. mem_heap_strdupl(
  2191. heap, (char*) field, len),
  2192. TRUE, ignore_err);
  2193. }
  2194. }
  2195. }
  2196. btr_pcur_close(&pcur);
  2197. mtr_commit(&mtr);
  2198. mem_heap_free(heap);
  2199. return(table);
  2200. }
  2201. /***********************************************************************//**
  2202. Loads a table id based on the index id.
  2203. @return true if found */
  2204. static
  2205. bool
  2206. dict_load_table_id_on_index_id(
  2207. /*==================*/
  2208. index_id_t index_id, /*!< in: index id */
  2209. table_id_t* table_id) /*!< out: table id */
  2210. {
  2211. /* check hard coded indexes */
  2212. switch(index_id) {
  2213. case DICT_TABLES_ID:
  2214. case DICT_COLUMNS_ID:
  2215. case DICT_INDEXES_ID:
  2216. case DICT_FIELDS_ID:
  2217. *table_id = index_id;
  2218. return true;
  2219. case DICT_TABLE_IDS_ID:
  2220. /* The following is a secondary index on SYS_TABLES */
  2221. *table_id = DICT_TABLES_ID;
  2222. return true;
  2223. }
  2224. bool found = false;
  2225. mtr_t mtr;
  2226. ut_ad(mutex_own(&(dict_sys->mutex)));
  2227. /* NOTE that the operation of this function is protected by
  2228. the dictionary mutex, and therefore no deadlocks can occur
  2229. with other dictionary operations. */
  2230. mtr_start(&mtr);
  2231. btr_pcur_t pcur;
  2232. const rec_t* rec = dict_startscan_system(&pcur, &mtr, SYS_INDEXES);
  2233. while (rec) {
  2234. ulint len;
  2235. const byte* field = rec_get_nth_field_old(
  2236. rec, DICT_FLD__SYS_INDEXES__ID, &len);
  2237. ut_ad(len == 8);
  2238. /* Check if the index id is the one searched for */
  2239. if (index_id == mach_read_from_8(field)) {
  2240. found = true;
  2241. /* Now we get the table id */
  2242. const byte* field = rec_get_nth_field_old(
  2243. rec,
  2244. DICT_FLD__SYS_INDEXES__TABLE_ID,
  2245. &len);
  2246. *table_id = mach_read_from_8(field);
  2247. break;
  2248. }
  2249. mtr_commit(&mtr);
  2250. mtr_start(&mtr);
  2251. rec = dict_getnext_system(&pcur, &mtr);
  2252. }
  2253. btr_pcur_close(&pcur);
  2254. mtr_commit(&mtr);
  2255. return(found);
  2256. }
  2257. UNIV_INTERN
  2258. dict_table_t*
  2259. dict_table_open_on_index_id(
  2260. /*==================*/
  2261. index_id_t index_id, /*!< in: index id */
  2262. bool dict_locked) /*!< in: dict locked */
  2263. {
  2264. if (!dict_locked) {
  2265. mutex_enter(&dict_sys->mutex);
  2266. }
  2267. ut_ad(mutex_own(&dict_sys->mutex));
  2268. table_id_t table_id;
  2269. dict_table_t * table = NULL;
  2270. if (dict_load_table_id_on_index_id(index_id, &table_id)) {
  2271. bool local_dict_locked = true;
  2272. table = dict_table_open_on_id(table_id,
  2273. local_dict_locked,
  2274. DICT_TABLE_OP_LOAD_TABLESPACE);
  2275. }
  2276. if (!dict_locked) {
  2277. mutex_exit(&dict_sys->mutex);
  2278. }
  2279. return table;
  2280. }
  2281. /********************************************************************//**
  2282. This function is called when the database is booted. Loads system table
  2283. index definitions except for the clustered index which is added to the
  2284. dictionary cache at booting before calling this function. */
  2285. UNIV_INTERN
  2286. void
  2287. dict_load_sys_table(
  2288. /*================*/
  2289. dict_table_t* table) /*!< in: system table */
  2290. {
  2291. mem_heap_t* heap;
  2292. ut_ad(mutex_own(&(dict_sys->mutex)));
  2293. heap = mem_heap_create(1000);
  2294. dict_load_indexes(table, heap, DICT_ERR_IGNORE_NONE);
  2295. mem_heap_free(heap);
  2296. }
  2297. /********************************************************************//**
  2298. Loads foreign key constraint col names (also for the referenced table).
  2299. Members that must be set (and valid) in foreign:
  2300. foreign->heap
  2301. foreign->n_fields
  2302. foreign->id ('\0'-terminated)
  2303. Members that will be created and set by this function:
  2304. foreign->foreign_col_names[i]
  2305. foreign->referenced_col_names[i]
  2306. (for i=0..foreign->n_fields-1) */
  2307. static
  2308. void
  2309. dict_load_foreign_cols(
  2310. /*===================*/
  2311. dict_foreign_t* foreign)/*!< in/out: foreign constraint object */
  2312. {
  2313. dict_table_t* sys_foreign_cols;
  2314. dict_index_t* sys_index;
  2315. btr_pcur_t pcur;
  2316. dtuple_t* tuple;
  2317. dfield_t* dfield;
  2318. const rec_t* rec;
  2319. const byte* field;
  2320. ulint len;
  2321. ulint i;
  2322. mtr_t mtr;
  2323. size_t id_len;
  2324. ut_ad(mutex_own(&(dict_sys->mutex)));
  2325. id_len = strlen(foreign->id);
  2326. foreign->foreign_col_names = static_cast<const char**>(
  2327. mem_heap_alloc(foreign->heap,
  2328. foreign->n_fields * sizeof(void*)));
  2329. foreign->referenced_col_names = static_cast<const char**>(
  2330. mem_heap_alloc(foreign->heap,
  2331. foreign->n_fields * sizeof(void*)));
  2332. mtr_start(&mtr);
  2333. sys_foreign_cols = dict_table_get_low("SYS_FOREIGN_COLS");
  2334. sys_index = UT_LIST_GET_FIRST(sys_foreign_cols->indexes);
  2335. ut_ad(!dict_table_is_comp(sys_foreign_cols));
  2336. tuple = dtuple_create(foreign->heap, 1);
  2337. dfield = dtuple_get_nth_field(tuple, 0);
  2338. dfield_set_data(dfield, foreign->id, id_len);
  2339. dict_index_copy_types(tuple, sys_index, 1);
  2340. btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
  2341. BTR_SEARCH_LEAF, &pcur, &mtr);
  2342. for (i = 0; i < foreign->n_fields; i++) {
  2343. rec = btr_pcur_get_rec(&pcur);
  2344. ut_a(btr_pcur_is_on_user_rec(&pcur));
  2345. ut_a(!rec_get_deleted_flag(rec, 0));
  2346. field = rec_get_nth_field_old(
  2347. rec, DICT_FLD__SYS_FOREIGN_COLS__ID, &len);
  2348. if (len != id_len || ut_memcmp(foreign->id, field, len) != 0) {
  2349. const rec_t* pos;
  2350. ulint pos_len;
  2351. const rec_t* for_col_name;
  2352. ulint for_col_name_len;
  2353. const rec_t* ref_col_name;
  2354. ulint ref_col_name_len;
  2355. pos = rec_get_nth_field_old(
  2356. rec, DICT_FLD__SYS_FOREIGN_COLS__POS,
  2357. &pos_len);
  2358. for_col_name = rec_get_nth_field_old(
  2359. rec, DICT_FLD__SYS_FOREIGN_COLS__FOR_COL_NAME,
  2360. &for_col_name_len);
  2361. ref_col_name = rec_get_nth_field_old(
  2362. rec, DICT_FLD__SYS_FOREIGN_COLS__REF_COL_NAME,
  2363. &ref_col_name_len);
  2364. ib_logf(IB_LOG_LEVEL_ERROR,
  2365. "Unable to load columns names for foreign "
  2366. "key '%s' because it was not found in "
  2367. "InnoDB internal table SYS_FOREIGN_COLS. The "
  2368. "closest entry we found is: "
  2369. "(ID='%.*s', POS=%lu, FOR_COL_NAME='%.*s', "
  2370. "REF_COL_NAME='%.*s')",
  2371. foreign->id,
  2372. (int) len, field,
  2373. mach_read_from_4(pos),
  2374. (int) for_col_name_len, for_col_name,
  2375. (int) ref_col_name_len, ref_col_name);
  2376. ut_error;
  2377. }
  2378. field = rec_get_nth_field_old(
  2379. rec, DICT_FLD__SYS_FOREIGN_COLS__POS, &len);
  2380. ut_a(len == 4);
  2381. ut_a(i == mach_read_from_4(field));
  2382. field = rec_get_nth_field_old(
  2383. rec, DICT_FLD__SYS_FOREIGN_COLS__FOR_COL_NAME, &len);
  2384. foreign->foreign_col_names[i] = mem_heap_strdupl(
  2385. foreign->heap, (char*) field, len);
  2386. field = rec_get_nth_field_old(
  2387. rec, DICT_FLD__SYS_FOREIGN_COLS__REF_COL_NAME, &len);
  2388. foreign->referenced_col_names[i] = mem_heap_strdupl(
  2389. foreign->heap, (char*) field, len);
  2390. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  2391. }
  2392. btr_pcur_close(&pcur);
  2393. mtr_commit(&mtr);
  2394. }
  2395. /***********************************************************************//**
  2396. Loads a foreign key constraint to the dictionary cache.
  2397. @return DB_SUCCESS or error code */
  2398. static __attribute__((nonnull(1), warn_unused_result))
  2399. dberr_t
  2400. dict_load_foreign(
  2401. /*==============*/
  2402. const char* id,
  2403. /*!< in: foreign constraint id, must be
  2404. '\0'-terminated */
  2405. const char** col_names,
  2406. /*!< in: column names, or NULL
  2407. to use foreign->foreign_table->col_names */
  2408. bool check_recursive,
  2409. /*!< in: whether to record the foreign table
  2410. parent count to avoid unlimited recursive
  2411. load of chained foreign tables */
  2412. bool check_charsets,
  2413. /*!< in: whether to check charset
  2414. compatibility */
  2415. dict_err_ignore_t ignore_err)
  2416. /*!< in: error to be ignored */
  2417. {
  2418. dict_foreign_t* foreign;
  2419. dict_table_t* sys_foreign;
  2420. btr_pcur_t pcur;
  2421. dict_index_t* sys_index;
  2422. dtuple_t* tuple;
  2423. mem_heap_t* heap2;
  2424. dfield_t* dfield;
  2425. const rec_t* rec;
  2426. const byte* field;
  2427. ulint len;
  2428. ulint n_fields_and_type;
  2429. mtr_t mtr;
  2430. dict_table_t* for_table;
  2431. dict_table_t* ref_table;
  2432. size_t id_len;
  2433. ut_ad(mutex_own(&(dict_sys->mutex)));
  2434. id_len = strlen(id);
  2435. heap2 = mem_heap_create(1000);
  2436. mtr_start(&mtr);
  2437. sys_foreign = dict_table_get_low("SYS_FOREIGN");
  2438. sys_index = UT_LIST_GET_FIRST(sys_foreign->indexes);
  2439. ut_ad(!dict_table_is_comp(sys_foreign));
  2440. tuple = dtuple_create(heap2, 1);
  2441. dfield = dtuple_get_nth_field(tuple, 0);
  2442. dfield_set_data(dfield, id, id_len);
  2443. dict_index_copy_types(tuple, sys_index, 1);
  2444. btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
  2445. BTR_SEARCH_LEAF, &pcur, &mtr);
  2446. rec = btr_pcur_get_rec(&pcur);
  2447. if (!btr_pcur_is_on_user_rec(&pcur)
  2448. || rec_get_deleted_flag(rec, 0)) {
  2449. /* Not found */
  2450. fprintf(stderr,
  2451. "InnoDB: Error: cannot load foreign constraint "
  2452. "%s: could not find the relevant record in "
  2453. "SYS_FOREIGN\n", id);
  2454. btr_pcur_close(&pcur);
  2455. mtr_commit(&mtr);
  2456. mem_heap_free(heap2);
  2457. return(DB_ERROR);
  2458. }
  2459. field = rec_get_nth_field_old(rec, DICT_FLD__SYS_FOREIGN__ID, &len);
  2460. /* Check if the id in record is the searched one */
  2461. if (len != id_len || ut_memcmp(id, field, len) != 0) {
  2462. fprintf(stderr,
  2463. "InnoDB: Error: cannot load foreign constraint "
  2464. "%s: found %.*s instead in SYS_FOREIGN\n",
  2465. id, (int) len, field);
  2466. btr_pcur_close(&pcur);
  2467. mtr_commit(&mtr);
  2468. mem_heap_free(heap2);
  2469. return(DB_ERROR);
  2470. }
  2471. /* Read the table names and the number of columns associated
  2472. with the constraint */
  2473. mem_heap_free(heap2);
  2474. foreign = dict_mem_foreign_create();
  2475. n_fields_and_type = mach_read_from_4(
  2476. rec_get_nth_field_old(
  2477. rec, DICT_FLD__SYS_FOREIGN__N_COLS, &len));
  2478. ut_a(len == 4);
  2479. /* We store the type in the bits 24..29 of n_fields_and_type. */
  2480. foreign->type = (unsigned int) (n_fields_and_type >> 24);
  2481. foreign->n_fields = (unsigned int) (n_fields_and_type & 0x3FFUL);
  2482. foreign->id = mem_heap_strdupl(foreign->heap, id, id_len);
  2483. field = rec_get_nth_field_old(
  2484. rec, DICT_FLD__SYS_FOREIGN__FOR_NAME, &len);
  2485. foreign->foreign_table_name = mem_heap_strdupl(
  2486. foreign->heap, (char*) field, len);
  2487. dict_mem_foreign_table_name_lookup_set(foreign, TRUE);
  2488. field = rec_get_nth_field_old(
  2489. rec, DICT_FLD__SYS_FOREIGN__REF_NAME, &len);
  2490. foreign->referenced_table_name = mem_heap_strdupl(
  2491. foreign->heap, (char*) field, len);
  2492. dict_mem_referenced_table_name_lookup_set(foreign, TRUE);
  2493. btr_pcur_close(&pcur);
  2494. mtr_commit(&mtr);
  2495. dict_load_foreign_cols(foreign);
  2496. ref_table = dict_table_check_if_in_cache_low(
  2497. foreign->referenced_table_name_lookup);
  2498. /* We could possibly wind up in a deep recursive calls if
  2499. we call dict_table_get_low() again here if there
  2500. is a chain of tables concatenated together with
  2501. foreign constraints. In such case, each table is
  2502. both a parent and child of the other tables, and
  2503. act as a "link" in such table chains.
  2504. To avoid such scenario, we would need to check the
  2505. number of ancesters the current table has. If that
  2506. exceeds DICT_FK_MAX_CHAIN_LEN, we will stop loading
  2507. the child table.
  2508. Foreign constraints are loaded in a Breath First fashion,
  2509. that is, the index on FOR_NAME is scanned first, and then
  2510. index on REF_NAME. So foreign constrains in which
  2511. current table is a child (foreign table) are loaded first,
  2512. and then those constraints where current table is a
  2513. parent (referenced) table.
  2514. Thus we could check the parent (ref_table) table's
  2515. reference count (fk_max_recusive_level) to know how deep the
  2516. recursive call is. If the parent table (ref_table) is already
  2517. loaded, and its fk_max_recusive_level is larger than
  2518. DICT_FK_MAX_CHAIN_LEN, we will stop the recursive loading
  2519. by skipping loading the child table. It will not affect foreign
  2520. constraint check for DMLs since child table will be loaded
  2521. at that time for the constraint check. */
  2522. if (!ref_table
  2523. || ref_table->fk_max_recusive_level < DICT_FK_MAX_RECURSIVE_LOAD) {
  2524. /* If the foreign table is not yet in the dictionary cache, we
  2525. have to load it so that we are able to make type comparisons
  2526. in the next function call. */
  2527. for_table = dict_table_get_low(foreign->foreign_table_name_lookup);
  2528. if (for_table && ref_table && check_recursive) {
  2529. /* This is to record the longest chain of ancesters
  2530. this table has, if the parent has more ancesters
  2531. than this table has, record it after add 1 (for this
  2532. parent */
  2533. if (ref_table->fk_max_recusive_level
  2534. >= for_table->fk_max_recusive_level) {
  2535. for_table->fk_max_recusive_level =
  2536. ref_table->fk_max_recusive_level + 1;
  2537. }
  2538. }
  2539. }
  2540. /* Note that there may already be a foreign constraint object in
  2541. the dictionary cache for this constraint: then the following
  2542. call only sets the pointers in it to point to the appropriate table
  2543. and index objects and frees the newly created object foreign.
  2544. Adding to the cache should always succeed since we are not creating
  2545. a new foreign key constraint but loading one from the data
  2546. dictionary. */
  2547. return(dict_foreign_add_to_cache(foreign, col_names, check_charsets,
  2548. ignore_err));
  2549. }
  2550. /***********************************************************************//**
  2551. Loads foreign key constraints where the table is either the foreign key
  2552. holder or where the table is referenced by a foreign key. Adds these
  2553. constraints to the data dictionary. Note that we know that the dictionary
  2554. cache already contains all constraints where the other relevant table is
  2555. already in the dictionary cache.
  2556. @return DB_SUCCESS or error code */
  2557. UNIV_INTERN
  2558. dberr_t
  2559. dict_load_foreigns(
  2560. /*===============*/
  2561. const char* table_name, /*!< in: table name */
  2562. const char** col_names, /*!< in: column names, or NULL
  2563. to use table->col_names */
  2564. bool check_recursive,/*!< in: Whether to check
  2565. recursive load of tables
  2566. chained by FK */
  2567. bool check_charsets, /*!< in: whether to check
  2568. charset compatibility */
  2569. dict_err_ignore_t ignore_err) /*!< in: error to be ignored */
  2570. {
  2571. ulint tuple_buf[(DTUPLE_EST_ALLOC(1) + sizeof(ulint) - 1)
  2572. / sizeof(ulint)];
  2573. btr_pcur_t pcur;
  2574. dtuple_t* tuple;
  2575. dfield_t* dfield;
  2576. dict_index_t* sec_index;
  2577. dict_table_t* sys_foreign;
  2578. const rec_t* rec;
  2579. const byte* field;
  2580. ulint len;
  2581. dberr_t err;
  2582. mtr_t mtr;
  2583. ut_ad(mutex_own(&(dict_sys->mutex)));
  2584. sys_foreign = dict_table_get_low("SYS_FOREIGN");
  2585. if (sys_foreign == NULL) {
  2586. /* No foreign keys defined yet in this database */
  2587. fprintf(stderr,
  2588. "InnoDB: Error: no foreign key system tables"
  2589. " in the database\n");
  2590. return(DB_ERROR);
  2591. }
  2592. ut_ad(!dict_table_is_comp(sys_foreign));
  2593. mtr_start(&mtr);
  2594. /* Get the secondary index based on FOR_NAME from table
  2595. SYS_FOREIGN */
  2596. sec_index = dict_table_get_next_index(
  2597. dict_table_get_first_index(sys_foreign));
  2598. ut_ad(!dict_index_is_clust(sec_index));
  2599. start_load:
  2600. tuple = dtuple_create_from_mem(tuple_buf, sizeof(tuple_buf), 1);
  2601. dfield = dtuple_get_nth_field(tuple, 0);
  2602. dfield_set_data(dfield, table_name, ut_strlen(table_name));
  2603. dict_index_copy_types(tuple, sec_index, 1);
  2604. btr_pcur_open_on_user_rec(sec_index, tuple, PAGE_CUR_GE,
  2605. BTR_SEARCH_LEAF, &pcur, &mtr);
  2606. loop:
  2607. rec = btr_pcur_get_rec(&pcur);
  2608. if (!btr_pcur_is_on_user_rec(&pcur)) {
  2609. /* End of index */
  2610. goto load_next_index;
  2611. }
  2612. /* Now we have the record in the secondary index containing a table
  2613. name and a foreign constraint ID */
  2614. field = rec_get_nth_field_old(
  2615. rec, DICT_FLD__SYS_FOREIGN_FOR_NAME__NAME, &len);
  2616. /* Check if the table name in the record is the one searched for; the
  2617. following call does the comparison in the latin1_swedish_ci
  2618. charset-collation, in a case-insensitive way. */
  2619. if (0 != cmp_data_data(dfield_get_type(dfield)->mtype,
  2620. dfield_get_type(dfield)->prtype,
  2621. static_cast<const byte*>(
  2622. dfield_get_data(dfield)),
  2623. dfield_get_len(dfield),
  2624. field, len)) {
  2625. goto load_next_index;
  2626. }
  2627. /* Since table names in SYS_FOREIGN are stored in a case-insensitive
  2628. order, we have to check that the table name matches also in a binary
  2629. string comparison. On Unix, MySQL allows table names that only differ
  2630. in character case. If lower_case_table_names=2 then what is stored
  2631. may not be the same case, but the previous comparison showed that they
  2632. match with no-case. */
  2633. if (rec_get_deleted_flag(rec, 0)) {
  2634. goto next_rec;
  2635. }
  2636. if ((innobase_get_lower_case_table_names() != 2)
  2637. && (0 != ut_memcmp(field, table_name, len))) {
  2638. goto next_rec;
  2639. }
  2640. /* Now we get a foreign key constraint id */
  2641. field = rec_get_nth_field_old(
  2642. rec, DICT_FLD__SYS_FOREIGN_FOR_NAME__ID, &len);
  2643. /* Copy the string because the page may be modified or evicted
  2644. after mtr_commit() below. */
  2645. char fk_id[MAX_TABLE_NAME_LEN + 1];
  2646. ut_a(len <= MAX_TABLE_NAME_LEN);
  2647. memcpy(fk_id, field, len);
  2648. fk_id[len] = '\0';
  2649. btr_pcur_store_position(&pcur, &mtr);
  2650. mtr_commit(&mtr);
  2651. /* Load the foreign constraint definition to the dictionary cache */
  2652. err = dict_load_foreign(fk_id, col_names,
  2653. check_recursive, check_charsets, ignore_err);
  2654. if (err != DB_SUCCESS) {
  2655. btr_pcur_close(&pcur);
  2656. return(err);
  2657. }
  2658. mtr_start(&mtr);
  2659. btr_pcur_restore_position(BTR_SEARCH_LEAF, &pcur, &mtr);
  2660. next_rec:
  2661. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  2662. goto loop;
  2663. load_next_index:
  2664. btr_pcur_close(&pcur);
  2665. mtr_commit(&mtr);
  2666. sec_index = dict_table_get_next_index(sec_index);
  2667. if (sec_index != NULL) {
  2668. mtr_start(&mtr);
  2669. /* Switch to scan index on REF_NAME, fk_max_recusive_level
  2670. already been updated when scanning FOR_NAME index, no need to
  2671. update again */
  2672. check_recursive = FALSE;
  2673. goto start_load;
  2674. }
  2675. return(DB_SUCCESS);
  2676. }