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