mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / storage / innodb_plugin / srv / srv0start.c
blob8684d7d7aa7d06f51b5a9b2a4257fca8fb3e59dd
1 /*****************************************************************************
3 Copyright (c) 1996, 2010, Innobase Oy. All Rights Reserved.
4 Copyright (c) 2008, Google Inc.
5 Copyright (c) 2009, Percona Inc.
7 Portions of this file contain modifications contributed and copyrighted by
8 Google, Inc. Those modifications are gratefully acknowledged and are described
9 briefly in the InnoDB documentation. The contributions by Google are
10 incorporated with their permission, and subject to the conditions contained in
11 the file COPYING.Google.
13 Portions of this file contain modifications contributed and copyrighted
14 by Percona Inc.. Those modifications are
15 gratefully acknowledged and are described briefly in the InnoDB
16 documentation. The contributions by Percona Inc. are incorporated with
17 their permission, and subject to the conditions contained in the file
18 COPYING.Percona.
20 This program is free software; you can redistribute it and/or modify it under
21 the terms of the GNU General Public License as published by the Free Software
22 Foundation; version 2 of the License.
24 This program is distributed in the hope that it will be useful, but WITHOUT
25 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
26 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License along with
29 this program; if not, write to the Free Software Foundation, Inc.,
30 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 *****************************************************************************/
34 /********************************************************************//**
35 @file srv/srv0start.c
36 Starts the InnoDB database server
38 Created 2/16/1996 Heikki Tuuri
39 *************************************************************************/
41 #include "ut0mem.h"
42 #include "mem0mem.h"
43 #include "data0data.h"
44 #include "data0type.h"
45 #include "dict0dict.h"
46 #include "buf0buf.h"
47 #include "os0file.h"
48 #include "os0thread.h"
49 #include "fil0fil.h"
50 #include "fsp0fsp.h"
51 #include "rem0rec.h"
52 #include "mtr0mtr.h"
53 #include "log0log.h"
54 #include "log0recv.h"
55 #include "page0page.h"
56 #include "page0cur.h"
57 #include "trx0trx.h"
58 #include "trx0sys.h"
59 #include "btr0btr.h"
60 #include "btr0cur.h"
61 #include "rem0rec.h"
62 #include "ibuf0ibuf.h"
63 #include "srv0start.h"
64 #include "srv0srv.h"
65 #ifndef UNIV_HOTBACKUP
66 # include "os0proc.h"
67 # include "sync0sync.h"
68 # include "buf0flu.h"
69 # include "buf0rea.h"
70 # include "dict0boot.h"
71 # include "dict0load.h"
72 # include "que0que.h"
73 # include "usr0sess.h"
74 # include "lock0lock.h"
75 # include "trx0roll.h"
76 # include "trx0purge.h"
77 # include "lock0lock.h"
78 # include "pars0pars.h"
79 # include "btr0sea.h"
80 # include "rem0cmp.h"
81 # include "dict0crea.h"
82 # include "row0ins.h"
83 # include "row0sel.h"
84 # include "row0upd.h"
85 # include "row0row.h"
86 # include "row0mysql.h"
87 # include "btr0pcur.h"
88 # include "thr0loc.h"
89 # include "os0sync.h" /* for INNODB_RW_LOCKS_USE_ATOMICS */
90 # include "zlib.h" /* for ZLIB_VERSION */
92 /** Log sequence number immediately after startup */
93 UNIV_INTERN ib_uint64_t srv_start_lsn;
94 /** Log sequence number at shutdown */
95 UNIV_INTERN ib_uint64_t srv_shutdown_lsn;
97 #ifdef HAVE_DARWIN_THREADS
98 # include <sys/utsname.h>
99 /** TRUE if the F_FULLFSYNC option is available */
100 UNIV_INTERN ibool srv_have_fullfsync = FALSE;
101 #endif
103 /** TRUE if a raw partition is in use */
104 UNIV_INTERN ibool srv_start_raw_disk_in_use = FALSE;
106 /** TRUE if the server is being started, before rolling back any
107 incomplete transactions */
108 UNIV_INTERN ibool srv_startup_is_before_trx_rollback_phase = FALSE;
109 /** TRUE if the server is being started */
110 UNIV_INTERN ibool srv_is_being_started = FALSE;
111 /** TRUE if the server was successfully started */
112 UNIV_INTERN ibool srv_was_started = FALSE;
113 /** TRUE if innobase_start_or_create_for_mysql() has been called */
114 static ibool srv_start_has_been_called = FALSE;
116 /** At a shutdown this value climbs from SRV_SHUTDOWN_NONE to
117 SRV_SHUTDOWN_CLEANUP and then to SRV_SHUTDOWN_LAST_PHASE, and so on */
118 UNIV_INTERN enum srv_shutdown_state srv_shutdown_state = SRV_SHUTDOWN_NONE;
120 /** Files comprising the system tablespace */
121 static os_file_t files[1000];
123 /** Mutex protecting the ios count */
124 static mutex_t ios_mutex;
125 /** Count of I/O operations in io_handler_thread() */
126 static ulint ios;
128 /** io_handler_thread parameters for thread identification */
129 static ulint n[SRV_MAX_N_IO_THREADS + 6];
130 /** io_handler_thread identifiers */
131 static os_thread_id_t thread_ids[SRV_MAX_N_IO_THREADS + 6];
133 /** We use this mutex to test the return value of pthread_mutex_trylock
134 on successful locking. HP-UX does NOT return 0, though Linux et al do. */
135 static os_fast_mutex_t srv_os_test_mutex;
137 /** Name of srv_monitor_file */
138 static char* srv_monitor_file_name;
139 #endif /* !UNIV_HOTBACKUP */
141 /** */
142 #define SRV_N_PENDING_IOS_PER_THREAD OS_AIO_N_PENDING_IOS_PER_THREAD
143 #define SRV_MAX_N_PENDING_SYNC_IOS 100
146 /*********************************************************************//**
147 Convert a numeric string that optionally ends in G or M, to a number
148 containing megabytes.
149 @return next character in string */
150 static
151 char*
152 srv_parse_megabytes(
153 /*================*/
154 char* str, /*!< in: string containing a quantity in bytes */
155 ulint* megs) /*!< out: the number in megabytes */
157 char* endp;
158 ulint size;
160 size = strtoul(str, &endp, 10);
162 str = endp;
164 switch (*str) {
165 case 'G': case 'g':
166 size *= 1024;
167 /* fall through */
168 case 'M': case 'm':
169 str++;
170 break;
171 default:
172 size /= 1024 * 1024;
173 break;
176 *megs = size;
177 return(str);
180 /*********************************************************************//**
181 Reads the data files and their sizes from a character string given in
182 the .cnf file.
183 @return TRUE if ok, FALSE on parse error */
184 UNIV_INTERN
185 ibool
186 srv_parse_data_file_paths_and_sizes(
187 /*================================*/
188 char* str) /*!< in/out: the data file path string */
190 char* input_str;
191 char* path;
192 ulint size;
193 ulint i = 0;
195 srv_auto_extend_last_data_file = FALSE;
196 srv_last_file_size_max = 0;
197 srv_data_file_names = NULL;
198 srv_data_file_sizes = NULL;
199 srv_data_file_is_raw_partition = NULL;
201 input_str = str;
203 /* First calculate the number of data files and check syntax:
204 path:size[M | G];path:size[M | G]... . Note that a Windows path may
205 contain a drive name and a ':'. */
207 while (*str != '\0') {
208 path = str;
210 while ((*str != ':' && *str != '\0')
211 || (*str == ':'
212 && (*(str + 1) == '\\' || *(str + 1) == '/'
213 || *(str + 1) == ':'))) {
214 str++;
217 if (*str == '\0') {
218 return(FALSE);
221 str++;
223 str = srv_parse_megabytes(str, &size);
225 if (0 == strncmp(str, ":autoextend",
226 (sizeof ":autoextend") - 1)) {
228 str += (sizeof ":autoextend") - 1;
230 if (0 == strncmp(str, ":max:",
231 (sizeof ":max:") - 1)) {
233 str += (sizeof ":max:") - 1;
235 str = srv_parse_megabytes(str, &size);
238 if (*str != '\0') {
240 return(FALSE);
244 if (strlen(str) >= 6
245 && *str == 'n'
246 && *(str + 1) == 'e'
247 && *(str + 2) == 'w') {
248 str += 3;
251 if (*str == 'r' && *(str + 1) == 'a' && *(str + 2) == 'w') {
252 str += 3;
255 if (size == 0) {
256 return(FALSE);
259 i++;
261 if (*str == ';') {
262 str++;
263 } else if (*str != '\0') {
265 return(FALSE);
269 if (i == 0) {
270 /* If innodb_data_file_path was defined it must contain
271 at least one data file definition */
273 return(FALSE);
276 srv_data_file_names = malloc(i * sizeof *srv_data_file_names);
277 srv_data_file_sizes = malloc(i * sizeof *srv_data_file_sizes);
278 srv_data_file_is_raw_partition = malloc(
279 i * sizeof *srv_data_file_is_raw_partition);
281 srv_n_data_files = i;
283 /* Then store the actual values to our arrays */
285 str = input_str;
286 i = 0;
288 while (*str != '\0') {
289 path = str;
291 /* Note that we must step over the ':' in a Windows path;
292 a Windows path normally looks like C:\ibdata\ibdata1:1G, but
293 a Windows raw partition may have a specification like
294 \\.\C::1Gnewraw or \\.\PHYSICALDRIVE2:1Gnewraw */
296 while ((*str != ':' && *str != '\0')
297 || (*str == ':'
298 && (*(str + 1) == '\\' || *(str + 1) == '/'
299 || *(str + 1) == ':'))) {
300 str++;
303 if (*str == ':') {
304 /* Make path a null-terminated string */
305 *str = '\0';
306 str++;
309 str = srv_parse_megabytes(str, &size);
311 srv_data_file_names[i] = path;
312 srv_data_file_sizes[i] = size;
314 if (0 == strncmp(str, ":autoextend",
315 (sizeof ":autoextend") - 1)) {
317 srv_auto_extend_last_data_file = TRUE;
319 str += (sizeof ":autoextend") - 1;
321 if (0 == strncmp(str, ":max:",
322 (sizeof ":max:") - 1)) {
324 str += (sizeof ":max:") - 1;
326 str = srv_parse_megabytes(
327 str, &srv_last_file_size_max);
330 if (*str != '\0') {
332 return(FALSE);
336 (srv_data_file_is_raw_partition)[i] = 0;
338 if (strlen(str) >= 6
339 && *str == 'n'
340 && *(str + 1) == 'e'
341 && *(str + 2) == 'w') {
342 str += 3;
343 (srv_data_file_is_raw_partition)[i] = SRV_NEW_RAW;
346 if (*str == 'r' && *(str + 1) == 'a' && *(str + 2) == 'w') {
347 str += 3;
349 if ((srv_data_file_is_raw_partition)[i] == 0) {
350 (srv_data_file_is_raw_partition)[i] = SRV_OLD_RAW;
354 i++;
356 if (*str == ';') {
357 str++;
361 return(TRUE);
364 /*********************************************************************//**
365 Reads log group home directories from a character string given in
366 the .cnf file.
367 @return TRUE if ok, FALSE on parse error */
368 UNIV_INTERN
369 ibool
370 srv_parse_log_group_home_dirs(
371 /*==========================*/
372 char* str) /*!< in/out: character string */
374 char* input_str;
375 char* path;
376 ulint i = 0;
378 srv_log_group_home_dirs = NULL;
380 input_str = str;
382 /* First calculate the number of directories and check syntax:
383 path;path;... */
385 while (*str != '\0') {
386 path = str;
388 while (*str != ';' && *str != '\0') {
389 str++;
392 i++;
394 if (*str == ';') {
395 str++;
396 } else if (*str != '\0') {
398 return(FALSE);
402 if (i != 1) {
403 /* If innodb_log_group_home_dir was defined it must
404 contain exactly one path definition under current MySQL */
406 return(FALSE);
409 srv_log_group_home_dirs = malloc(i * sizeof *srv_log_group_home_dirs);
411 /* Then store the actual values to our array */
413 str = input_str;
414 i = 0;
416 while (*str != '\0') {
417 path = str;
419 while (*str != ';' && *str != '\0') {
420 str++;
423 if (*str == ';') {
424 *str = '\0';
425 str++;
428 srv_log_group_home_dirs[i] = path;
430 i++;
433 return(TRUE);
436 /*********************************************************************//**
437 Frees the memory allocated by srv_parse_data_file_paths_and_sizes()
438 and srv_parse_log_group_home_dirs(). */
439 UNIV_INTERN
440 void
441 srv_free_paths_and_sizes(void)
442 /*==========================*/
444 free(srv_data_file_names);
445 srv_data_file_names = NULL;
446 free(srv_data_file_sizes);
447 srv_data_file_sizes = NULL;
448 free(srv_data_file_is_raw_partition);
449 srv_data_file_is_raw_partition = NULL;
450 free(srv_log_group_home_dirs);
451 srv_log_group_home_dirs = NULL;
454 #ifndef UNIV_HOTBACKUP
455 /********************************************************************//**
456 I/o-handler thread function.
457 @return OS_THREAD_DUMMY_RETURN */
458 static
459 os_thread_ret_t
460 io_handler_thread(
461 /*==============*/
462 void* arg) /*!< in: pointer to the number of the segment in
463 the aio array */
465 ulint segment;
467 segment = *((ulint*)arg);
469 #ifdef UNIV_DEBUG_THREAD_CREATION
470 fprintf(stderr, "Io handler thread %lu starts, id %lu\n", segment,
471 os_thread_pf(os_thread_get_curr_id()));
472 #endif
473 while (srv_shutdown_state != SRV_SHUTDOWN_EXIT_THREADS) {
474 fil_aio_wait(segment);
476 mutex_enter(&ios_mutex);
477 ios++;
478 mutex_exit(&ios_mutex);
481 /* We count the number of threads in os_thread_exit(). A created
482 thread should always use that to exit and not use return() to exit.
483 The thread actually never comes here because it is exited in an
484 os_event_wait(). */
486 os_thread_exit(NULL);
488 OS_THREAD_DUMMY_RETURN;
490 #endif /* !UNIV_HOTBACKUP */
492 #ifdef __WIN__
493 #define SRV_PATH_SEPARATOR '\\'
494 #else
495 #define SRV_PATH_SEPARATOR '/'
496 #endif
498 /*********************************************************************//**
499 Normalizes a directory path for Windows: converts slashes to backslashes. */
500 UNIV_INTERN
501 void
502 srv_normalize_path_for_win(
503 /*=======================*/
504 char* str __attribute__((unused))) /*!< in/out: null-terminated
505 character string */
507 #ifdef __WIN__
508 for (; *str; str++) {
510 if (*str == '/') {
511 *str = '\\';
514 #endif
517 #ifndef UNIV_HOTBACKUP
518 /*********************************************************************//**
519 Calculates the low 32 bits when a file size which is given as a number
520 database pages is converted to the number of bytes.
521 @return low 32 bytes of file size when expressed in bytes */
522 static
523 ulint
524 srv_calc_low32(
525 /*===========*/
526 ulint file_size) /*!< in: file size in database pages */
528 return(0xFFFFFFFFUL & (file_size << UNIV_PAGE_SIZE_SHIFT));
531 /*********************************************************************//**
532 Calculates the high 32 bits when a file size which is given as a number
533 database pages is converted to the number of bytes.
534 @return high 32 bytes of file size when expressed in bytes */
535 static
536 ulint
537 srv_calc_high32(
538 /*============*/
539 ulint file_size) /*!< in: file size in database pages */
541 return(file_size >> (32 - UNIV_PAGE_SIZE_SHIFT));
544 /*********************************************************************//**
545 Creates or opens the log files and closes them.
546 @return DB_SUCCESS or error code */
547 static
548 ulint
549 open_or_create_log_file(
550 /*====================*/
551 ibool create_new_db, /*!< in: TRUE if we should create a
552 new database */
553 ibool* log_file_created, /*!< out: TRUE if new log file
554 created */
555 ibool log_file_has_been_opened,/*!< in: TRUE if a log file has been
556 opened before: then it is an error
557 to try to create another log file */
558 ulint k, /*!< in: log group number */
559 ulint i) /*!< in: log file number in group */
561 ibool ret;
562 ulint size;
563 ulint size_high;
564 char name[10000];
565 ulint dirnamelen;
567 UT_NOT_USED(create_new_db);
569 *log_file_created = FALSE;
571 srv_normalize_path_for_win(srv_log_group_home_dirs[k]);
573 dirnamelen = strlen(srv_log_group_home_dirs[k]);
574 ut_a(dirnamelen < (sizeof name) - 10 - sizeof "ib_logfile");
575 memcpy(name, srv_log_group_home_dirs[k], dirnamelen);
577 /* Add a path separator if needed. */
578 if (dirnamelen && name[dirnamelen - 1] != SRV_PATH_SEPARATOR) {
579 name[dirnamelen++] = SRV_PATH_SEPARATOR;
582 sprintf(name + dirnamelen, "%s%lu", "ib_logfile", (ulong) i);
584 files[i] = os_file_create(name, OS_FILE_CREATE, OS_FILE_NORMAL,
585 OS_LOG_FILE, &ret);
586 if (ret == FALSE) {
587 if (os_file_get_last_error(FALSE) != OS_FILE_ALREADY_EXISTS
588 #ifdef UNIV_AIX
589 /* AIX 5.1 after security patch ML7 may have errno set
590 to 0 here, which causes our function to return 100;
591 work around that AIX problem */
592 && os_file_get_last_error(FALSE) != 100
593 #endif
595 fprintf(stderr,
596 "InnoDB: Error in creating"
597 " or opening %s\n", name);
599 return(DB_ERROR);
602 files[i] = os_file_create(name, OS_FILE_OPEN, OS_FILE_AIO,
603 OS_LOG_FILE, &ret);
604 if (!ret) {
605 fprintf(stderr,
606 "InnoDB: Error in opening %s\n", name);
608 return(DB_ERROR);
611 ret = os_file_get_size(files[i], &size, &size_high);
612 ut_a(ret);
614 if (size != srv_calc_low32(srv_log_file_size)
615 || size_high != srv_calc_high32(srv_log_file_size)) {
617 fprintf(stderr,
618 "InnoDB: Error: log file %s is"
619 " of different size %lu %lu bytes\n"
620 "InnoDB: than specified in the .cnf"
621 " file %lu %lu bytes!\n",
622 name, (ulong) size_high, (ulong) size,
623 (ulong) srv_calc_high32(srv_log_file_size),
624 (ulong) srv_calc_low32(srv_log_file_size));
626 return(DB_ERROR);
628 } else {
629 *log_file_created = TRUE;
631 ut_print_timestamp(stderr);
633 fprintf(stderr,
634 " InnoDB: Log file %s did not exist:"
635 " new to be created\n",
636 name);
637 if (log_file_has_been_opened) {
639 return(DB_ERROR);
642 fprintf(stderr, "InnoDB: Setting log file %s size to %lu MB\n",
643 name, (ulong) srv_log_file_size
644 >> (20 - UNIV_PAGE_SIZE_SHIFT));
646 fprintf(stderr,
647 "InnoDB: Database physically writes the file"
648 " full: wait...\n");
650 ret = os_file_set_size(name, files[i],
651 srv_calc_low32(srv_log_file_size),
652 srv_calc_high32(srv_log_file_size));
653 if (!ret) {
654 fprintf(stderr,
655 "InnoDB: Error in creating %s:"
656 " probably out of disk space\n",
657 name);
659 return(DB_ERROR);
663 ret = os_file_close(files[i]);
664 ut_a(ret);
666 if (i == 0) {
667 /* Create in memory the file space object
668 which is for this log group */
670 fil_space_create(name,
671 2 * k + SRV_LOG_SPACE_FIRST_ID, 0, FIL_LOG);
674 ut_a(fil_validate());
676 fil_node_create(name, srv_log_file_size,
677 2 * k + SRV_LOG_SPACE_FIRST_ID, FALSE);
678 #ifdef UNIV_LOG_ARCHIVE
679 /* If this is the first log group, create the file space object
680 for archived logs.
681 Under MySQL, no archiving ever done. */
683 if (k == 0 && i == 0) {
684 arch_space_id = 2 * k + 1 + SRV_LOG_SPACE_FIRST_ID;
686 fil_space_create("arch_log_space", arch_space_id, 0, FIL_LOG);
687 } else {
688 arch_space_id = ULINT_UNDEFINED;
690 #endif /* UNIV_LOG_ARCHIVE */
691 if (i == 0) {
692 log_group_init(k, srv_n_log_files,
693 srv_log_file_size * UNIV_PAGE_SIZE,
694 2 * k + SRV_LOG_SPACE_FIRST_ID,
695 SRV_LOG_SPACE_FIRST_ID + 1); /* dummy arch
696 space id */
699 return(DB_SUCCESS);
702 /*********************************************************************//**
703 Creates or opens database data files and closes them.
704 @return DB_SUCCESS or error code */
705 static
706 ulint
707 open_or_create_data_files(
708 /*======================*/
709 ibool* create_new_db, /*!< out: TRUE if new database should be
710 created */
711 #ifdef UNIV_LOG_ARCHIVE
712 ulint* min_arch_log_no,/*!< out: min of archived log
713 numbers in data files */
714 ulint* max_arch_log_no,/*!< out: max of archived log
715 numbers in data files */
716 #endif /* UNIV_LOG_ARCHIVE */
717 ib_uint64_t* min_flushed_lsn,/*!< out: min of flushed lsn
718 values in data files */
719 ib_uint64_t* max_flushed_lsn,/*!< out: max of flushed lsn
720 values in data files */
721 ulint* sum_of_new_sizes)/*!< out: sum of sizes of the
722 new files added */
724 ibool ret;
725 ulint i;
726 ibool one_opened = FALSE;
727 ibool one_created = FALSE;
728 ulint size;
729 ulint size_high;
730 ulint rounded_size_pages;
731 char name[10000];
733 if (srv_n_data_files >= 1000) {
734 fprintf(stderr, "InnoDB: can only have < 1000 data files\n"
735 "InnoDB: you have defined %lu\n",
736 (ulong) srv_n_data_files);
737 return(DB_ERROR);
740 *sum_of_new_sizes = 0;
742 *create_new_db = FALSE;
744 srv_normalize_path_for_win(srv_data_home);
746 for (i = 0; i < srv_n_data_files; i++) {
747 ulint dirnamelen;
749 srv_normalize_path_for_win(srv_data_file_names[i]);
750 dirnamelen = strlen(srv_data_home);
752 ut_a(dirnamelen + strlen(srv_data_file_names[i])
753 < (sizeof name) - 1);
754 memcpy(name, srv_data_home, dirnamelen);
755 /* Add a path separator if needed. */
756 if (dirnamelen && name[dirnamelen - 1] != SRV_PATH_SEPARATOR) {
757 name[dirnamelen++] = SRV_PATH_SEPARATOR;
760 strcpy(name + dirnamelen, srv_data_file_names[i]);
762 if (srv_data_file_is_raw_partition[i] == 0) {
764 /* First we try to create the file: if it already
765 exists, ret will get value FALSE */
767 files[i] = os_file_create(name, OS_FILE_CREATE,
768 OS_FILE_NORMAL,
769 OS_DATA_FILE, &ret);
771 if (ret == FALSE && os_file_get_last_error(FALSE)
772 != OS_FILE_ALREADY_EXISTS
773 #ifdef UNIV_AIX
774 /* AIX 5.1 after security patch ML7 may have
775 errno set to 0 here, which causes our function
776 to return 100; work around that AIX problem */
777 && os_file_get_last_error(FALSE) != 100
778 #endif
780 fprintf(stderr,
781 "InnoDB: Error in creating"
782 " or opening %s\n",
783 name);
785 return(DB_ERROR);
787 } else if (srv_data_file_is_raw_partition[i] == SRV_NEW_RAW) {
788 /* The partition is opened, not created; then it is
789 written over */
791 srv_start_raw_disk_in_use = TRUE;
792 srv_created_new_raw = TRUE;
794 files[i] = os_file_create(name, OS_FILE_OPEN_RAW,
795 OS_FILE_NORMAL,
796 OS_DATA_FILE, &ret);
797 if (!ret) {
798 fprintf(stderr,
799 "InnoDB: Error in opening %s\n", name);
801 return(DB_ERROR);
803 } else if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {
804 srv_start_raw_disk_in_use = TRUE;
806 ret = FALSE;
807 } else {
808 ut_a(0);
811 if (ret == FALSE) {
812 /* We open the data file */
814 if (one_created) {
815 fprintf(stderr,
816 "InnoDB: Error: data files can only"
817 " be added at the end\n");
818 fprintf(stderr,
819 "InnoDB: of a tablespace, but"
820 " data file %s existed beforehand.\n",
821 name);
822 return(DB_ERROR);
825 if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {
826 files[i] = os_file_create(
827 name, OS_FILE_OPEN_RAW,
828 OS_FILE_NORMAL, OS_DATA_FILE, &ret);
829 } else if (i == 0) {
830 files[i] = os_file_create(
831 name, OS_FILE_OPEN_RETRY,
832 OS_FILE_NORMAL, OS_DATA_FILE, &ret);
833 } else {
834 files[i] = os_file_create(
835 name, OS_FILE_OPEN, OS_FILE_NORMAL,
836 OS_DATA_FILE, &ret);
839 if (!ret) {
840 fprintf(stderr,
841 "InnoDB: Error in opening %s\n", name);
842 os_file_get_last_error(TRUE);
844 return(DB_ERROR);
847 if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {
849 goto skip_size_check;
852 ret = os_file_get_size(files[i], &size, &size_high);
853 ut_a(ret);
854 /* Round size downward to megabytes */
856 rounded_size_pages
857 = (size / (1024 * 1024) + 4096 * size_high)
858 << (20 - UNIV_PAGE_SIZE_SHIFT);
860 if (i == srv_n_data_files - 1
861 && srv_auto_extend_last_data_file) {
863 if (srv_data_file_sizes[i] > rounded_size_pages
864 || (srv_last_file_size_max > 0
865 && srv_last_file_size_max
866 < rounded_size_pages)) {
868 fprintf(stderr,
869 "InnoDB: Error: auto-extending"
870 " data file %s is"
871 " of a different size\n"
872 "InnoDB: %lu pages (rounded"
873 " down to MB) than specified"
874 " in the .cnf file:\n"
875 "InnoDB: initial %lu pages,"
876 " max %lu (relevant if"
877 " non-zero) pages!\n",
878 name,
879 (ulong) rounded_size_pages,
880 (ulong) srv_data_file_sizes[i],
881 (ulong)
882 srv_last_file_size_max);
884 return(DB_ERROR);
887 srv_data_file_sizes[i] = rounded_size_pages;
890 if (rounded_size_pages != srv_data_file_sizes[i]) {
892 fprintf(stderr,
893 "InnoDB: Error: data file %s"
894 " is of a different size\n"
895 "InnoDB: %lu pages"
896 " (rounded down to MB)\n"
897 "InnoDB: than specified"
898 " in the .cnf file %lu pages!\n",
899 name,
900 (ulong) rounded_size_pages,
901 (ulong) srv_data_file_sizes[i]);
903 return(DB_ERROR);
905 skip_size_check:
906 fil_read_flushed_lsn_and_arch_log_no(
907 files[i], one_opened,
908 #ifdef UNIV_LOG_ARCHIVE
909 min_arch_log_no, max_arch_log_no,
910 #endif /* UNIV_LOG_ARCHIVE */
911 min_flushed_lsn, max_flushed_lsn);
912 one_opened = TRUE;
913 } else {
914 /* We created the data file and now write it full of
915 zeros */
917 one_created = TRUE;
919 if (i > 0) {
920 ut_print_timestamp(stderr);
921 fprintf(stderr,
922 " InnoDB: Data file %s did not"
923 " exist: new to be created\n",
924 name);
925 } else {
926 fprintf(stderr,
927 "InnoDB: The first specified"
928 " data file %s did not exist:\n"
929 "InnoDB: a new database"
930 " to be created!\n", name);
931 *create_new_db = TRUE;
934 ut_print_timestamp(stderr);
935 fprintf(stderr,
936 " InnoDB: Setting file %s size to %lu MB\n",
937 name,
938 (ulong) (srv_data_file_sizes[i]
939 >> (20 - UNIV_PAGE_SIZE_SHIFT)));
941 fprintf(stderr,
942 "InnoDB: Database physically writes the"
943 " file full: wait...\n");
945 ret = os_file_set_size(
946 name, files[i],
947 srv_calc_low32(srv_data_file_sizes[i]),
948 srv_calc_high32(srv_data_file_sizes[i]));
950 if (!ret) {
951 fprintf(stderr,
952 "InnoDB: Error in creating %s:"
953 " probably out of disk space\n", name);
955 return(DB_ERROR);
958 *sum_of_new_sizes = *sum_of_new_sizes
959 + srv_data_file_sizes[i];
962 ret = os_file_close(files[i]);
963 ut_a(ret);
965 if (i == 0) {
966 fil_space_create(name, 0, 0, FIL_TABLESPACE);
969 ut_a(fil_validate());
971 fil_node_create(name, srv_data_file_sizes[i], 0,
972 srv_data_file_is_raw_partition[i] != 0);
975 ios = 0;
977 mutex_create(&ios_mutex, SYNC_NO_ORDER_CHECK);
979 return(DB_SUCCESS);
982 /********************************************************************
983 Starts InnoDB and creates a new database if database files
984 are not found and the user wants.
985 @return DB_SUCCESS or error code */
986 UNIV_INTERN
988 innobase_start_or_create_for_mysql(void)
989 /*====================================*/
991 buf_pool_t* ret;
992 ibool create_new_db;
993 ibool log_file_created;
994 ibool log_created = FALSE;
995 ibool log_opened = FALSE;
996 ib_uint64_t min_flushed_lsn;
997 ib_uint64_t max_flushed_lsn;
998 #ifdef UNIV_LOG_ARCHIVE
999 ulint min_arch_log_no;
1000 ulint max_arch_log_no;
1001 #endif /* UNIV_LOG_ARCHIVE */
1002 ulint sum_of_new_sizes;
1003 ulint sum_of_data_file_sizes;
1004 ulint tablespace_size_in_header;
1005 ulint err;
1006 ulint i;
1007 ulint io_limit;
1008 my_bool srv_file_per_table_original_value
1009 = srv_file_per_table;
1010 mtr_t mtr;
1011 #ifdef HAVE_DARWIN_THREADS
1012 # ifdef F_FULLFSYNC
1013 /* This executable has been compiled on Mac OS X 10.3 or later.
1014 Assume that F_FULLFSYNC is available at run-time. */
1015 srv_have_fullfsync = TRUE;
1016 # else /* F_FULLFSYNC */
1017 /* This executable has been compiled on Mac OS X 10.2
1018 or earlier. Determine if the executable is running
1019 on Mac OS X 10.3 or later. */
1020 struct utsname utsname;
1021 if (uname(&utsname)) {
1022 fputs("InnoDB: cannot determine Mac OS X version!\n", stderr);
1023 } else {
1024 srv_have_fullfsync = strcmp(utsname.release, "7.") >= 0;
1026 if (!srv_have_fullfsync) {
1027 fputs("InnoDB: On Mac OS X, fsync() may be"
1028 " broken on internal drives,\n"
1029 "InnoDB: making transactions unsafe!\n", stderr);
1031 # endif /* F_FULLFSYNC */
1032 #endif /* HAVE_DARWIN_THREADS */
1034 if (sizeof(ulint) != sizeof(void*)) {
1035 fprintf(stderr,
1036 "InnoDB: Error: size of InnoDB's ulint is %lu,"
1037 " but size of void* is %lu.\n"
1038 "InnoDB: The sizes should be the same"
1039 " so that on a 64-bit platform you can\n"
1040 "InnoDB: allocate more than 4 GB of memory.",
1041 (ulong)sizeof(ulint), (ulong)sizeof(void*));
1044 /* System tables are created in tablespace 0. Thus, we must
1045 temporarily clear srv_file_per_table. This is ok, because the
1046 server will not accept connections (which could modify
1047 innodb_file_per_table) until this function has returned. */
1048 srv_file_per_table = FALSE;
1049 #ifdef UNIV_DEBUG
1050 fprintf(stderr,
1051 "InnoDB: !!!!!!!! UNIV_DEBUG switched on !!!!!!!!!\n");
1052 #endif
1054 #ifdef UNIV_IBUF_DEBUG
1055 fprintf(stderr,
1056 "InnoDB: !!!!!!!! UNIV_IBUF_DEBUG switched on !!!!!!!!!\n"
1057 # ifdef UNIV_IBUF_COUNT_DEBUG
1058 "InnoDB: !!!!!!!! UNIV_IBUF_COUNT_DEBUG switched on !!!!!!!!!\n"
1059 "InnoDB: Crash recovery will fail with UNIV_IBUF_COUNT_DEBUG\n"
1060 # endif
1062 #endif
1064 #ifdef UNIV_BLOB_DEBUG
1065 fprintf(stderr,
1066 "InnoDB: !!!!!!!! UNIV_BLOB_DEBUG switched on !!!!!!!!!\n"
1067 "InnoDB: Server restart may fail with UNIV_BLOB_DEBUG\n");
1068 #endif /* UNIV_BLOB_DEBUG */
1070 #ifdef UNIV_SYNC_DEBUG
1071 fprintf(stderr,
1072 "InnoDB: !!!!!!!! UNIV_SYNC_DEBUG switched on !!!!!!!!!\n");
1073 #endif
1075 #ifdef UNIV_SEARCH_DEBUG
1076 fprintf(stderr,
1077 "InnoDB: !!!!!!!! UNIV_SEARCH_DEBUG switched on !!!!!!!!!\n");
1078 #endif
1080 #ifdef UNIV_LOG_LSN_DEBUG
1081 fprintf(stderr,
1082 "InnoDB: !!!!!!!! UNIV_LOG_LSN_DEBUG switched on !!!!!!!!!\n");
1083 #endif /* UNIV_LOG_LSN_DEBUG */
1084 #ifdef UNIV_MEM_DEBUG
1085 fprintf(stderr,
1086 "InnoDB: !!!!!!!! UNIV_MEM_DEBUG switched on !!!!!!!!!\n");
1087 #endif
1089 if (UNIV_LIKELY(srv_use_sys_malloc)) {
1090 fprintf(stderr,
1091 "InnoDB: The InnoDB memory heap is disabled\n");
1094 fputs("InnoDB: " IB_ATOMICS_STARTUP_MSG
1095 "\nInnoDB: Compressed tables use zlib " ZLIB_VERSION
1096 #ifdef UNIV_ZIP_DEBUG
1097 " with validation"
1098 #endif /* UNIV_ZIP_DEBUG */
1099 #ifdef UNIV_ZIP_COPY
1100 " and extra copying"
1101 #endif /* UNIV_ZIP_COPY */
1102 "\n" , stderr);
1104 /* Since InnoDB does not currently clean up all its internal data
1105 structures in MySQL Embedded Server Library server_end(), we
1106 print an error message if someone tries to start up InnoDB a
1107 second time during the process lifetime. */
1109 if (srv_start_has_been_called) {
1110 fprintf(stderr,
1111 "InnoDB: Error: startup called second time"
1112 " during the process lifetime.\n"
1113 "InnoDB: In the MySQL Embedded Server Library"
1114 " you cannot call server_init()\n"
1115 "InnoDB: more than once during"
1116 " the process lifetime.\n");
1119 srv_start_has_been_called = TRUE;
1121 #ifdef UNIV_DEBUG
1122 log_do_write = TRUE;
1123 #endif /* UNIV_DEBUG */
1124 /* yydebug = TRUE; */
1126 srv_is_being_started = TRUE;
1127 srv_startup_is_before_trx_rollback_phase = TRUE;
1128 os_aio_use_native_aio = FALSE;
1130 #ifdef __WIN__
1131 switch (os_get_os_version()) {
1132 case OS_WIN95:
1133 case OS_WIN31:
1134 case OS_WINNT:
1135 /* On Win 95, 98, ME, Win32 subsystem for Windows 3.1,
1136 and NT use simulated aio. In NT Windows provides async i/o,
1137 but when run in conjunction with InnoDB Hot Backup, it seemed
1138 to corrupt the data files. */
1140 os_aio_use_native_aio = FALSE;
1141 break;
1142 default:
1143 /* On Win 2000 and XP use async i/o */
1144 os_aio_use_native_aio = TRUE;
1145 break;
1147 #endif
1148 if (srv_file_flush_method_str == NULL) {
1149 /* These are the default options */
1151 srv_unix_file_flush_method = SRV_UNIX_FSYNC;
1153 srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
1154 #ifndef __WIN__
1155 } else if (0 == ut_strcmp(srv_file_flush_method_str, "fsync")) {
1156 srv_unix_file_flush_method = SRV_UNIX_FSYNC;
1158 } else if (0 == ut_strcmp(srv_file_flush_method_str, "O_DSYNC")) {
1159 srv_unix_file_flush_method = SRV_UNIX_O_DSYNC;
1161 } else if (0 == ut_strcmp(srv_file_flush_method_str, "O_DIRECT")) {
1162 srv_unix_file_flush_method = SRV_UNIX_O_DIRECT;
1164 } else if (0 == ut_strcmp(srv_file_flush_method_str, "littlesync")) {
1165 srv_unix_file_flush_method = SRV_UNIX_LITTLESYNC;
1167 } else if (0 == ut_strcmp(srv_file_flush_method_str, "nosync")) {
1168 srv_unix_file_flush_method = SRV_UNIX_NOSYNC;
1169 #else
1170 } else if (0 == ut_strcmp(srv_file_flush_method_str, "normal")) {
1171 srv_win_file_flush_method = SRV_WIN_IO_NORMAL;
1172 os_aio_use_native_aio = FALSE;
1174 } else if (0 == ut_strcmp(srv_file_flush_method_str, "unbuffered")) {
1175 srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
1176 os_aio_use_native_aio = FALSE;
1178 } else if (0 == ut_strcmp(srv_file_flush_method_str,
1179 "async_unbuffered")) {
1180 srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
1181 #endif
1182 } else {
1183 fprintf(stderr,
1184 "InnoDB: Unrecognized value %s for"
1185 " innodb_flush_method\n",
1186 srv_file_flush_method_str);
1187 return(DB_ERROR);
1190 /* Note that the call srv_boot() also changes the values of
1191 some variables to the units used by InnoDB internally */
1193 /* Set the maximum number of threads which can wait for a semaphore
1194 inside InnoDB: this is the 'sync wait array' size, as well as the
1195 maximum number of threads that can wait in the 'srv_conc array' for
1196 their time to enter InnoDB. */
1198 #if defined(__NETWARE__)
1200 /* Create less event semaphores because Win 98/ME had
1201 difficulty creating 40000 event semaphores. Comment from
1202 Novell, Inc.: also, these just take a lot of memory on
1203 NetWare. */
1204 srv_max_n_threads = 1000;
1205 #else
1206 if (srv_buf_pool_size >= 1000 * 1024 * 1024) {
1207 /* If buffer pool is less than 1000 MB,
1208 assume fewer threads. */
1209 srv_max_n_threads = 50000;
1211 } else if (srv_buf_pool_size >= 8 * 1024 * 1024) {
1213 srv_max_n_threads = 10000;
1214 } else {
1215 srv_max_n_threads = 1000; /* saves several MB of memory,
1216 especially in 64-bit
1217 computers */
1219 #endif
1220 err = srv_boot();
1222 if (err != DB_SUCCESS) {
1224 return((int) err);
1227 mutex_create(&srv_monitor_file_mutex, SYNC_NO_ORDER_CHECK);
1229 if (srv_innodb_status) {
1230 srv_monitor_file_name = mem_alloc(
1231 strlen(fil_path_to_mysql_datadir)
1232 + 20 + sizeof "/innodb_status.");
1233 sprintf(srv_monitor_file_name, "%s/innodb_status.%lu",
1234 fil_path_to_mysql_datadir, os_proc_get_number());
1235 srv_monitor_file = fopen(srv_monitor_file_name, "w+");
1236 if (!srv_monitor_file) {
1237 fprintf(stderr, "InnoDB: unable to create %s: %s\n",
1238 srv_monitor_file_name, strerror(errno));
1239 return(DB_ERROR);
1241 } else {
1242 srv_monitor_file_name = NULL;
1243 srv_monitor_file = os_file_create_tmpfile();
1244 if (!srv_monitor_file) {
1245 return(DB_ERROR);
1249 mutex_create(&srv_dict_tmpfile_mutex, SYNC_DICT_OPERATION);
1251 srv_dict_tmpfile = os_file_create_tmpfile();
1252 if (!srv_dict_tmpfile) {
1253 return(DB_ERROR);
1256 mutex_create(&srv_misc_tmpfile_mutex, SYNC_ANY_LATCH);
1258 srv_misc_tmpfile = os_file_create_tmpfile();
1259 if (!srv_misc_tmpfile) {
1260 return(DB_ERROR);
1263 /* If user has set the value of innodb_file_io_threads then
1264 we'll emit a message telling the user that this parameter
1265 is now deprecated. */
1266 if (srv_n_file_io_threads != 4) {
1267 fprintf(stderr, "InnoDB: Warning:"
1268 " innodb_file_io_threads is deprecated."
1269 " Please use innodb_read_io_threads and"
1270 " innodb_write_io_threads instead\n");
1273 /* Now overwrite the value on srv_n_file_io_threads */
1274 srv_n_file_io_threads = 2 + srv_n_read_io_threads
1275 + srv_n_write_io_threads;
1277 ut_a(srv_n_file_io_threads <= SRV_MAX_N_IO_THREADS);
1279 /* TODO: Investigate if SRV_N_PENDING_IOS_PER_THREAD (32) limit
1280 still applies to windows. */
1281 if (!os_aio_use_native_aio) {
1282 io_limit = 8 * SRV_N_PENDING_IOS_PER_THREAD;
1283 } else {
1284 io_limit = SRV_N_PENDING_IOS_PER_THREAD;
1287 os_aio_init(io_limit,
1288 srv_n_read_io_threads,
1289 srv_n_write_io_threads,
1290 SRV_MAX_N_PENDING_SYNC_IOS);
1292 fil_init(srv_file_per_table ? 50000 : 5000,
1293 srv_max_n_open_files);
1295 /* Print time to initialize the buffer pool */
1296 ut_print_timestamp(stderr);
1297 fprintf(stderr,
1298 " InnoDB: Initializing buffer pool, size =");
1300 if (srv_buf_pool_size >= 1024 * 1024 * 1024) {
1301 fprintf(stderr,
1302 " %.1fG\n",
1303 ((double) srv_buf_pool_size) / (1024 * 1024 * 1024));
1304 } else {
1305 fprintf(stderr,
1306 " %.1fM\n",
1307 ((double) srv_buf_pool_size) / (1024 * 1024));
1310 ret = buf_pool_init();
1312 ut_print_timestamp(stderr);
1314 if (ret == NULL) {
1315 fprintf(stderr,
1316 "InnoDB: Fatal error: cannot allocate the memory"
1317 " for the buffer pool\n");
1319 return(DB_ERROR);
1322 fprintf(stderr,
1323 " InnoDB: Completed initialization of buffer pool\n");
1325 #ifdef UNIV_DEBUG
1326 /* We have observed deadlocks with a 5MB buffer pool but
1327 the actual lower limit could very well be a little higher. */
1329 if (srv_buf_pool_size <= 5 * 1024 * 1024) {
1331 fprintf(stderr, "InnoDB: Warning: Small buffer pool size "
1332 "(%luM), the flst_validate() debug function "
1333 "can cause a deadlock if the buffer pool fills up.\n",
1334 srv_buf_pool_size / 1024 / 1024);
1336 #endif
1338 fsp_init();
1339 log_init();
1341 lock_sys_create(srv_lock_table_size);
1343 /* Create i/o-handler threads: */
1345 for (i = 0; i < srv_n_file_io_threads; i++) {
1346 n[i] = i;
1348 os_thread_create(io_handler_thread, n + i, thread_ids + i);
1351 #ifdef UNIV_LOG_ARCHIVE
1352 if (0 != ut_strcmp(srv_log_group_home_dirs[0], srv_arch_dir)) {
1353 fprintf(stderr,
1354 "InnoDB: Error: you must set the log group"
1355 " home dir in my.cnf the\n"
1356 "InnoDB: same as log arch dir.\n");
1358 return(DB_ERROR);
1360 #endif /* UNIV_LOG_ARCHIVE */
1362 if (srv_n_log_files * srv_log_file_size >= 262144) {
1363 fprintf(stderr,
1364 "InnoDB: Error: combined size of log files"
1365 " must be < 4 GB\n");
1367 return(DB_ERROR);
1370 sum_of_new_sizes = 0;
1372 for (i = 0; i < srv_n_data_files; i++) {
1373 #ifndef __WIN__
1374 if (sizeof(off_t) < 5 && srv_data_file_sizes[i] >= 262144) {
1375 fprintf(stderr,
1376 "InnoDB: Error: file size must be < 4 GB"
1377 " with this MySQL binary\n"
1378 "InnoDB: and operating system combination,"
1379 " in some OS's < 2 GB\n");
1381 return(DB_ERROR);
1383 #endif
1384 sum_of_new_sizes += srv_data_file_sizes[i];
1387 if (sum_of_new_sizes < 10485760 / UNIV_PAGE_SIZE) {
1388 fprintf(stderr,
1389 "InnoDB: Error: tablespace size must be"
1390 " at least 10 MB\n");
1392 return(DB_ERROR);
1395 err = open_or_create_data_files(&create_new_db,
1396 #ifdef UNIV_LOG_ARCHIVE
1397 &min_arch_log_no, &max_arch_log_no,
1398 #endif /* UNIV_LOG_ARCHIVE */
1399 &min_flushed_lsn, &max_flushed_lsn,
1400 &sum_of_new_sizes);
1401 if (err != DB_SUCCESS) {
1402 fprintf(stderr,
1403 "InnoDB: Could not open or create data files.\n"
1404 "InnoDB: If you tried to add new data files,"
1405 " and it failed here,\n"
1406 "InnoDB: you should now edit innodb_data_file_path"
1407 " in my.cnf back\n"
1408 "InnoDB: to what it was, and remove the"
1409 " new ibdata files InnoDB created\n"
1410 "InnoDB: in this failed attempt. InnoDB only wrote"
1411 " those files full of\n"
1412 "InnoDB: zeros, but did not yet use them in any way."
1413 " But be careful: do not\n"
1414 "InnoDB: remove old data files"
1415 " which contain your precious data!\n");
1417 return((int) err);
1420 #ifdef UNIV_LOG_ARCHIVE
1421 srv_normalize_path_for_win(srv_arch_dir);
1422 srv_arch_dir = srv_add_path_separator_if_needed(srv_arch_dir);
1423 #endif /* UNIV_LOG_ARCHIVE */
1425 for (i = 0; i < srv_n_log_files; i++) {
1426 err = open_or_create_log_file(create_new_db, &log_file_created,
1427 log_opened, 0, i);
1428 if (err != DB_SUCCESS) {
1430 return((int) err);
1433 if (log_file_created) {
1434 log_created = TRUE;
1435 } else {
1436 log_opened = TRUE;
1438 if ((log_opened && create_new_db)
1439 || (log_opened && log_created)) {
1440 fprintf(stderr,
1441 "InnoDB: Error: all log files must be"
1442 " created at the same time.\n"
1443 "InnoDB: All log files must be"
1444 " created also in database creation.\n"
1445 "InnoDB: If you want bigger or smaller"
1446 " log files, shut down the\n"
1447 "InnoDB: database and make sure there"
1448 " were no errors in shutdown.\n"
1449 "InnoDB: Then delete the existing log files."
1450 " Edit the .cnf file\n"
1451 "InnoDB: and start the database again.\n");
1453 return(DB_ERROR);
1457 /* Open all log files and data files in the system tablespace: we
1458 keep them open until database shutdown */
1460 fil_open_log_and_system_tablespace_files();
1462 if (log_created && !create_new_db
1463 #ifdef UNIV_LOG_ARCHIVE
1464 && !srv_archive_recovery
1465 #endif /* UNIV_LOG_ARCHIVE */
1467 if (max_flushed_lsn != min_flushed_lsn
1468 #ifdef UNIV_LOG_ARCHIVE
1469 || max_arch_log_no != min_arch_log_no
1470 #endif /* UNIV_LOG_ARCHIVE */
1472 fprintf(stderr,
1473 "InnoDB: Cannot initialize created"
1474 " log files because\n"
1475 "InnoDB: data files were not in sync"
1476 " with each other\n"
1477 "InnoDB: or the data files are corrupt.\n");
1479 return(DB_ERROR);
1482 if (max_flushed_lsn < (ib_uint64_t) 1000) {
1483 fprintf(stderr,
1484 "InnoDB: Cannot initialize created"
1485 " log files because\n"
1486 "InnoDB: data files are corrupt,"
1487 " or new data files were\n"
1488 "InnoDB: created when the database"
1489 " was started previous\n"
1490 "InnoDB: time but the database"
1491 " was not shut down\n"
1492 "InnoDB: normally after that.\n");
1494 return(DB_ERROR);
1497 mutex_enter(&(log_sys->mutex));
1499 #ifdef UNIV_LOG_ARCHIVE
1500 /* Do not + 1 arch_log_no because we do not use log
1501 archiving */
1502 recv_reset_logs(max_flushed_lsn, max_arch_log_no, TRUE);
1503 #else
1504 recv_reset_logs(max_flushed_lsn, TRUE);
1505 #endif /* UNIV_LOG_ARCHIVE */
1507 mutex_exit(&(log_sys->mutex));
1510 trx_sys_file_format_init();
1512 if (create_new_db) {
1513 mtr_start(&mtr);
1514 fsp_header_init(0, sum_of_new_sizes, &mtr);
1516 mtr_commit(&mtr);
1518 trx_sys_create();
1519 dict_create();
1520 srv_startup_is_before_trx_rollback_phase = FALSE;
1522 #ifdef UNIV_LOG_ARCHIVE
1523 } else if (srv_archive_recovery) {
1524 fprintf(stderr,
1525 "InnoDB: Starting archive"
1526 " recovery from a backup...\n");
1527 err = recv_recovery_from_archive_start(
1528 min_flushed_lsn, srv_archive_recovery_limit_lsn,
1529 min_arch_log_no);
1530 if (err != DB_SUCCESS) {
1532 return(DB_ERROR);
1534 /* Since ibuf init is in dict_boot, and ibuf is needed
1535 in any disk i/o, first call dict_boot */
1537 dict_boot();
1538 trx_sys_init_at_db_start();
1539 srv_startup_is_before_trx_rollback_phase = FALSE;
1541 /* Initialize the fsp free limit global variable in the log
1542 system */
1543 fsp_header_get_free_limit();
1545 recv_recovery_from_archive_finish();
1546 #endif /* UNIV_LOG_ARCHIVE */
1547 } else {
1549 /* Check if we support the max format that is stamped
1550 on the system tablespace.
1551 Note: We are NOT allowed to make any modifications to
1552 the TRX_SYS_PAGE_NO page before recovery because this
1553 page also contains the max_trx_id etc. important system
1554 variables that are required for recovery. We need to
1555 ensure that we return the system to a state where normal
1556 recovery is guaranteed to work. We do this by
1557 invalidating the buffer cache, this will force the
1558 reread of the page and restoration to its last known
1559 consistent state, this is REQUIRED for the recovery
1560 process to work. */
1561 err = trx_sys_file_format_max_check(
1562 srv_check_file_format_at_startup);
1564 if (err != DB_SUCCESS) {
1565 return(err);
1568 /* Invalidate the buffer pool to ensure that we reread
1569 the page that we read above, during recovery.
1570 Note that this is not as heavy weight as it seems. At
1571 this point there will be only ONE page in the buf_LRU
1572 and there must be no page in the buf_flush list. */
1573 buf_pool_invalidate();
1575 /* We always try to do a recovery, even if the database had
1576 been shut down normally: this is the normal startup path */
1578 err = recv_recovery_from_checkpoint_start(LOG_CHECKPOINT,
1579 IB_ULONGLONG_MAX,
1580 min_flushed_lsn,
1581 max_flushed_lsn);
1582 if (err != DB_SUCCESS) {
1584 return(DB_ERROR);
1587 /* Since the insert buffer init is in dict_boot, and the
1588 insert buffer is needed in any disk i/o, first we call
1589 dict_boot(). Note that trx_sys_init_at_db_start() only needs
1590 to access space 0, and the insert buffer at this stage already
1591 works for space 0. */
1593 dict_boot();
1594 trx_sys_init_at_db_start();
1596 /* Initialize the fsp free limit global variable in the log
1597 system */
1598 fsp_header_get_free_limit();
1600 /* recv_recovery_from_checkpoint_finish needs trx lists which
1601 are initialized in trx_sys_init_at_db_start(). */
1603 recv_recovery_from_checkpoint_finish();
1604 if (srv_force_recovery < SRV_FORCE_NO_IBUF_MERGE) {
1605 /* The following call is necessary for the insert
1606 buffer to work with multiple tablespaces. We must
1607 know the mapping between space id's and .ibd file
1608 names.
1610 In a crash recovery, we check that the info in data
1611 dictionary is consistent with what we already know
1612 about space id's from the call of
1613 fil_load_single_table_tablespaces().
1615 In a normal startup, we create the space objects for
1616 every table in the InnoDB data dictionary that has
1617 an .ibd file.
1619 We also determine the maximum tablespace id used. */
1621 dict_check_tablespaces_and_store_max_id(
1622 recv_needed_recovery);
1625 srv_startup_is_before_trx_rollback_phase = FALSE;
1626 recv_recovery_rollback_active();
1628 /* It is possible that file_format tag has never
1629 been set. In this case we initialize it to minimum
1630 value. Important to note that we can do it ONLY after
1631 we have finished the recovery process so that the
1632 image of TRX_SYS_PAGE_NO is not stale. */
1633 trx_sys_file_format_tag_init();
1636 if (!create_new_db && sum_of_new_sizes > 0) {
1637 /* New data file(s) were added */
1638 mtr_start(&mtr);
1640 fsp_header_inc_size(0, sum_of_new_sizes, &mtr);
1642 mtr_commit(&mtr);
1644 /* Immediately write the log record about increased tablespace
1645 size to disk, so that it is durable even if mysqld would crash
1646 quickly */
1648 log_buffer_flush_to_disk();
1651 #ifdef UNIV_LOG_ARCHIVE
1652 /* Archiving is always off under MySQL */
1653 if (!srv_log_archive_on) {
1654 ut_a(DB_SUCCESS == log_archive_noarchivelog());
1655 } else {
1656 mutex_enter(&(log_sys->mutex));
1658 start_archive = FALSE;
1660 if (log_sys->archiving_state == LOG_ARCH_OFF) {
1661 start_archive = TRUE;
1664 mutex_exit(&(log_sys->mutex));
1666 if (start_archive) {
1667 ut_a(DB_SUCCESS == log_archive_archivelog());
1670 #endif /* UNIV_LOG_ARCHIVE */
1672 /* fprintf(stderr, "Max allowed record size %lu\n",
1673 page_get_free_space_of_empty() / 2); */
1675 /* Create the thread which watches the timeouts for lock waits */
1676 os_thread_create(&srv_lock_timeout_thread, NULL,
1677 thread_ids + 2 + SRV_MAX_N_IO_THREADS);
1679 /* Create the thread which warns of long semaphore waits */
1680 os_thread_create(&srv_error_monitor_thread, NULL,
1681 thread_ids + 3 + SRV_MAX_N_IO_THREADS);
1683 /* Create the thread which prints InnoDB monitor info */
1684 os_thread_create(&srv_monitor_thread, NULL,
1685 thread_ids + 4 + SRV_MAX_N_IO_THREADS);
1687 srv_is_being_started = FALSE;
1689 if (trx_doublewrite == NULL) {
1690 /* Create the doublewrite buffer to a new tablespace */
1692 trx_sys_create_doublewrite_buf();
1695 err = dict_create_or_check_foreign_constraint_tables();
1697 if (err != DB_SUCCESS) {
1698 return((int)DB_ERROR);
1701 /* Create the master thread which does purge and other utility
1702 operations */
1704 os_thread_create(&srv_master_thread, NULL, thread_ids
1705 + (1 + SRV_MAX_N_IO_THREADS));
1706 #ifdef UNIV_DEBUG
1707 /* buf_debug_prints = TRUE; */
1708 #endif /* UNIV_DEBUG */
1709 sum_of_data_file_sizes = 0;
1711 for (i = 0; i < srv_n_data_files; i++) {
1712 sum_of_data_file_sizes += srv_data_file_sizes[i];
1715 tablespace_size_in_header = fsp_header_get_tablespace_size();
1717 if (!srv_auto_extend_last_data_file
1718 && sum_of_data_file_sizes != tablespace_size_in_header) {
1720 fprintf(stderr,
1721 "InnoDB: Error: tablespace size"
1722 " stored in header is %lu pages, but\n"
1723 "InnoDB: the sum of data file sizes is %lu pages\n",
1724 (ulong) tablespace_size_in_header,
1725 (ulong) sum_of_data_file_sizes);
1727 if (srv_force_recovery == 0
1728 && sum_of_data_file_sizes < tablespace_size_in_header) {
1729 /* This is a fatal error, the tail of a tablespace is
1730 missing */
1732 fprintf(stderr,
1733 "InnoDB: Cannot start InnoDB."
1734 " The tail of the system tablespace is\n"
1735 "InnoDB: missing. Have you edited"
1736 " innodb_data_file_path in my.cnf in an\n"
1737 "InnoDB: inappropriate way, removing"
1738 " ibdata files from there?\n"
1739 "InnoDB: You can set innodb_force_recovery=1"
1740 " in my.cnf to force\n"
1741 "InnoDB: a startup if you are trying"
1742 " to recover a badly corrupt database.\n");
1744 return(DB_ERROR);
1748 if (srv_auto_extend_last_data_file
1749 && sum_of_data_file_sizes < tablespace_size_in_header) {
1751 fprintf(stderr,
1752 "InnoDB: Error: tablespace size stored in header"
1753 " is %lu pages, but\n"
1754 "InnoDB: the sum of data file sizes"
1755 " is only %lu pages\n",
1756 (ulong) tablespace_size_in_header,
1757 (ulong) sum_of_data_file_sizes);
1759 if (srv_force_recovery == 0) {
1761 fprintf(stderr,
1762 "InnoDB: Cannot start InnoDB. The tail of"
1763 " the system tablespace is\n"
1764 "InnoDB: missing. Have you edited"
1765 " innodb_data_file_path in my.cnf in an\n"
1766 "InnoDB: inappropriate way, removing"
1767 " ibdata files from there?\n"
1768 "InnoDB: You can set innodb_force_recovery=1"
1769 " in my.cnf to force\n"
1770 "InnoDB: a startup if you are trying to"
1771 " recover a badly corrupt database.\n");
1773 return(DB_ERROR);
1777 /* Check that os_fast_mutexes work as expected */
1778 os_fast_mutex_init(&srv_os_test_mutex);
1780 if (0 != os_fast_mutex_trylock(&srv_os_test_mutex)) {
1781 fprintf(stderr,
1782 "InnoDB: Error: pthread_mutex_trylock returns"
1783 " an unexpected value on\n"
1784 "InnoDB: success! Cannot continue.\n");
1785 exit(1);
1788 os_fast_mutex_unlock(&srv_os_test_mutex);
1790 os_fast_mutex_lock(&srv_os_test_mutex);
1792 os_fast_mutex_unlock(&srv_os_test_mutex);
1794 os_fast_mutex_free(&srv_os_test_mutex);
1796 if (srv_print_verbose_log) {
1797 ut_print_timestamp(stderr);
1798 fprintf(stderr,
1799 " InnoDB Plugin %s started; "
1800 "log sequence number %llu\n",
1801 INNODB_VERSION_STR, srv_start_lsn);
1804 if (srv_force_recovery > 0) {
1805 fprintf(stderr,
1806 "InnoDB: !!! innodb_force_recovery"
1807 " is set to %lu !!!\n",
1808 (ulong) srv_force_recovery);
1811 fflush(stderr);
1813 if (trx_doublewrite_must_reset_space_ids) {
1814 /* Actually, we did not change the undo log format between
1815 4.0 and 4.1.1, and we would not need to run purge to
1816 completion. Note also that the purge algorithm in 4.1.1
1817 can process the history list again even after a full
1818 purge, because our algorithm does not cut the end of the
1819 history list in all cases so that it would become empty
1820 after a full purge. That mean that we may purge 4.0 type
1821 undo log even after this phase.
1823 The insert buffer record format changed between 4.0 and
1824 4.1.1. It is essential that the insert buffer is emptied
1825 here! */
1827 fprintf(stderr,
1828 "InnoDB: You are upgrading to an"
1829 " InnoDB version which allows multiple\n"
1830 "InnoDB: tablespaces. Wait that purge"
1831 " and insert buffer merge run to\n"
1832 "InnoDB: completion...\n");
1833 for (;;) {
1834 os_thread_sleep(1000000);
1836 if (0 == strcmp(srv_main_thread_op_info,
1837 "waiting for server activity")) {
1839 ut_a(ibuf_is_empty());
1841 break;
1844 fprintf(stderr,
1845 "InnoDB: Full purge and insert buffer merge"
1846 " completed.\n");
1848 trx_sys_mark_upgraded_to_multiple_tablespaces();
1850 fprintf(stderr,
1851 "InnoDB: You have now successfully upgraded"
1852 " to the multiple tablespaces\n"
1853 "InnoDB: format. You should NOT DOWNGRADE"
1854 " to an earlier version of\n"
1855 "InnoDB: InnoDB! But if you absolutely need to"
1856 " downgrade, see\n"
1857 "InnoDB: " REFMAN "multiple-tablespaces.html\n"
1858 "InnoDB: for instructions.\n");
1861 if (srv_force_recovery == 0) {
1862 /* In the insert buffer we may have even bigger tablespace
1863 id's, because we may have dropped those tablespaces, but
1864 insert buffer merge has not had time to clean the records from
1865 the ibuf tree. */
1867 ibuf_update_max_tablespace_id();
1870 srv_file_per_table = srv_file_per_table_original_value;
1872 srv_was_started = TRUE;
1874 return((int) DB_SUCCESS);
1877 /****************************************************************//**
1878 Shuts down the InnoDB database.
1879 @return DB_SUCCESS or error code */
1880 UNIV_INTERN
1882 innobase_shutdown_for_mysql(void)
1883 /*=============================*/
1885 ulint i;
1886 #ifdef __NETWARE__
1887 extern ibool panic_shutdown;
1888 #endif
1889 if (!srv_was_started) {
1890 if (srv_is_being_started) {
1891 ut_print_timestamp(stderr);
1892 fprintf(stderr,
1893 " InnoDB: Warning: shutting down"
1894 " a not properly started\n"
1895 "InnoDB: or created database!\n");
1898 return(DB_SUCCESS);
1901 /* 1. Flush the buffer pool to disk, write the current lsn to
1902 the tablespace header(s), and copy all log data to archive.
1903 The step 1 is the real InnoDB shutdown. The remaining steps 2 - ...
1904 just free data structures after the shutdown. */
1907 if (srv_fast_shutdown == 2) {
1908 ut_print_timestamp(stderr);
1909 fprintf(stderr,
1910 " InnoDB: MySQL has requested a very fast shutdown"
1911 " without flushing "
1912 "the InnoDB buffer pool to data files."
1913 " At the next mysqld startup "
1914 "InnoDB will do a crash recovery!\n");
1917 #ifdef __NETWARE__
1918 if (!panic_shutdown)
1919 #endif
1920 logs_empty_and_mark_files_at_shutdown();
1922 if (srv_conc_n_threads != 0) {
1923 fprintf(stderr,
1924 "InnoDB: Warning: query counter shows %ld queries"
1925 " still\n"
1926 "InnoDB: inside InnoDB at shutdown\n",
1927 srv_conc_n_threads);
1930 /* 2. Make all threads created by InnoDB to exit */
1932 srv_shutdown_state = SRV_SHUTDOWN_EXIT_THREADS;
1934 /* In a 'very fast' shutdown, we do not need to wait for these threads
1935 to die; all which counts is that we flushed the log; a 'very fast'
1936 shutdown is essentially a crash. */
1938 if (srv_fast_shutdown == 2) {
1939 return(DB_SUCCESS);
1942 /* All threads end up waiting for certain events. Put those events
1943 to the signaled state. Then the threads will exit themselves in
1944 os_thread_event_wait(). */
1946 for (i = 0; i < 1000; i++) {
1947 /* NOTE: IF YOU CREATE THREADS IN INNODB, YOU MUST EXIT THEM
1948 HERE OR EARLIER */
1950 /* a. Let the lock timeout thread exit */
1951 os_event_set(srv_lock_timeout_thread_event);
1953 /* b. srv error monitor thread exits automatically, no need
1954 to do anything here */
1956 /* c. We wake the master thread so that it exits */
1957 srv_wake_master_thread();
1959 /* d. Exit the i/o threads */
1961 os_aio_wake_all_threads_at_shutdown();
1963 os_mutex_enter(os_sync_mutex);
1965 if (os_thread_count == 0) {
1966 /* All the threads have exited or are just exiting;
1967 NOTE that the threads may not have completed their
1968 exit yet. Should we use pthread_join() to make sure
1969 they have exited? If we did, we would have to
1970 remove the pthread_detach() from
1971 os_thread_exit(). Now we just sleep 0.1
1972 seconds and hope that is enough! */
1974 os_mutex_exit(os_sync_mutex);
1976 os_thread_sleep(100000);
1978 break;
1981 os_mutex_exit(os_sync_mutex);
1983 os_thread_sleep(100000);
1986 if (i == 1000) {
1987 fprintf(stderr,
1988 "InnoDB: Warning: %lu threads created by InnoDB"
1989 " had not exited at shutdown!\n",
1990 (ulong) os_thread_count);
1993 if (srv_monitor_file) {
1994 fclose(srv_monitor_file);
1995 srv_monitor_file = 0;
1996 if (srv_monitor_file_name) {
1997 unlink(srv_monitor_file_name);
1998 mem_free(srv_monitor_file_name);
2001 if (srv_dict_tmpfile) {
2002 fclose(srv_dict_tmpfile);
2003 srv_dict_tmpfile = 0;
2006 if (srv_misc_tmpfile) {
2007 fclose(srv_misc_tmpfile);
2008 srv_misc_tmpfile = 0;
2011 /* This must be disabled before closing the buffer pool
2012 and closing the data dictionary. */
2013 btr_search_disable();
2015 ibuf_close();
2016 log_shutdown();
2017 lock_sys_close();
2018 thr_local_close();
2019 trx_sys_file_format_close();
2020 trx_sys_close();
2022 mutex_free(&srv_monitor_file_mutex);
2023 mutex_free(&srv_dict_tmpfile_mutex);
2024 mutex_free(&srv_misc_tmpfile_mutex);
2025 dict_close();
2026 btr_search_sys_free();
2028 /* 3. Free all InnoDB's own mutexes and the os_fast_mutexes inside
2029 them */
2030 os_aio_free();
2031 sync_close();
2032 srv_free();
2033 fil_close();
2035 /* 4. Free the os_conc_mutex and all os_events and os_mutexes */
2037 os_sync_free();
2039 /* 5. Free all allocated memory */
2041 pars_lexer_close();
2042 log_mem_free();
2043 buf_pool_free();
2044 mem_close();
2046 /* ut_free_all_mem() frees all allocated memory not freed yet
2047 in shutdown, and it will also free the ut_list_mutex, so it
2048 should be the last one for all operation */
2049 ut_free_all_mem();
2051 if (os_thread_count != 0
2052 || os_event_count != 0
2053 || os_mutex_count != 0
2054 || os_fast_mutex_count != 0) {
2055 fprintf(stderr,
2056 "InnoDB: Warning: some resources were not"
2057 " cleaned up in shutdown:\n"
2058 "InnoDB: threads %lu, events %lu,"
2059 " os_mutexes %lu, os_fast_mutexes %lu\n",
2060 (ulong) os_thread_count, (ulong) os_event_count,
2061 (ulong) os_mutex_count, (ulong) os_fast_mutex_count);
2064 if (dict_foreign_err_file) {
2065 fclose(dict_foreign_err_file);
2067 if (lock_latest_err_file) {
2068 fclose(lock_latest_err_file);
2071 if (srv_print_verbose_log) {
2072 ut_print_timestamp(stderr);
2073 fprintf(stderr,
2074 " InnoDB: Shutdown completed;"
2075 " log sequence number %llu\n",
2076 srv_shutdown_lsn);
2079 srv_was_started = FALSE;
2080 srv_start_has_been_called = FALSE;
2082 return((int) DB_SUCCESS);
2085 #ifdef __NETWARE__
2086 void set_panic_flag_for_netware()
2088 extern ibool panic_shutdown;
2089 panic_shutdown = TRUE;
2091 #endif /* __NETWARE__ */
2092 #endif /* !UNIV_HOTBACKUP */