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
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 /********************************************************************//**
36 Starts the InnoDB database server
38 Created 2/16/1996 Heikki Tuuri
39 *************************************************************************/
43 #include "data0data.h"
44 #include "data0type.h"
45 #include "dict0dict.h"
48 #include "os0thread.h"
55 #include "page0page.h"
62 #include "ibuf0ibuf.h"
63 #include "srv0start.h"
65 #ifndef UNIV_HOTBACKUP
67 # include "sync0sync.h"
70 # include "dict0boot.h"
71 # include "dict0load.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"
81 # include "dict0crea.h"
86 # include "row0mysql.h"
87 # include "btr0pcur.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
;
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() */
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 */
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 */
154 char* str
, /*!< in: string containing a quantity in bytes */
155 ulint
* megs
) /*!< out: the number in megabytes */
160 size
= strtoul(str
, &endp
, 10);
180 /*********************************************************************//**
181 Reads the data files and their sizes from a character string given in
183 @return TRUE if ok, FALSE on parse error */
186 srv_parse_data_file_paths_and_sizes(
187 /*================================*/
188 char* str
) /*!< in/out: the data file path string */
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
;
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') {
210 while ((*str
!= ':' && *str
!= '\0')
212 && (*(str
+ 1) == '\\' || *(str
+ 1) == '/'
213 || *(str
+ 1) == ':'))) {
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
);
247 && *(str
+ 2) == 'w') {
251 if (*str
== 'r' && *(str
+ 1) == 'a' && *(str
+ 2) == 'w') {
263 } else if (*str
!= '\0') {
270 /* If innodb_data_file_path was defined it must contain
271 at least one data file definition */
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 */
288 while (*str
!= '\0') {
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')
298 && (*(str
+ 1) == '\\' || *(str
+ 1) == '/'
299 || *(str
+ 1) == ':'))) {
304 /* Make path a null-terminated string */
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
);
336 (srv_data_file_is_raw_partition
)[i
] = 0;
341 && *(str
+ 2) == 'w') {
343 (srv_data_file_is_raw_partition
)[i
] = SRV_NEW_RAW
;
346 if (*str
== 'r' && *(str
+ 1) == 'a' && *(str
+ 2) == 'w') {
349 if ((srv_data_file_is_raw_partition
)[i
] == 0) {
350 (srv_data_file_is_raw_partition
)[i
] = SRV_OLD_RAW
;
364 /*********************************************************************//**
365 Reads log group home directories from a character string given in
367 @return TRUE if ok, FALSE on parse error */
370 srv_parse_log_group_home_dirs(
371 /*==========================*/
372 char* str
) /*!< in/out: character string */
378 srv_log_group_home_dirs
= NULL
;
382 /* First calculate the number of directories and check syntax:
385 while (*str
!= '\0') {
388 while (*str
!= ';' && *str
!= '\0') {
396 } else if (*str
!= '\0') {
403 /* If innodb_log_group_home_dir was defined it must
404 contain exactly one path definition under current MySQL */
409 srv_log_group_home_dirs
= malloc(i
* sizeof *srv_log_group_home_dirs
);
411 /* Then store the actual values to our array */
416 while (*str
!= '\0') {
419 while (*str
!= ';' && *str
!= '\0') {
428 srv_log_group_home_dirs
[i
] = path
;
436 /*********************************************************************//**
437 Frees the memory allocated by srv_parse_data_file_paths_and_sizes()
438 and srv_parse_log_group_home_dirs(). */
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 */
462 void* arg
) /*!< in: pointer to the number of the segment in
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()));
473 while (srv_shutdown_state
!= SRV_SHUTDOWN_EXIT_THREADS
) {
474 fil_aio_wait(segment
);
476 mutex_enter(&ios_mutex
);
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
486 os_thread_exit(NULL
);
488 OS_THREAD_DUMMY_RETURN
;
490 #endif /* !UNIV_HOTBACKUP */
493 #define SRV_PATH_SEPARATOR '\\'
495 #define SRV_PATH_SEPARATOR '/'
498 /*********************************************************************//**
499 Normalizes a directory path for Windows: converts slashes to backslashes. */
502 srv_normalize_path_for_win(
503 /*=======================*/
504 char* str
__attribute__((unused
))) /*!< in/out: null-terminated
508 for (; *str
; str
++) {
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 */
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 */
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 */
549 open_or_create_log_file(
550 /*====================*/
551 ibool create_new_db
, /*!< in: TRUE if we should create a
553 ibool
* log_file_created
, /*!< out: TRUE if new log file
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 */
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
,
587 if (os_file_get_last_error(FALSE
) != OS_FILE_ALREADY_EXISTS
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
596 "InnoDB: Error in creating"
597 " or opening %s\n", name
);
602 files
[i
] = os_file_create(name
, OS_FILE_OPEN
, OS_FILE_AIO
,
606 "InnoDB: Error in opening %s\n", name
);
611 ret
= os_file_get_size(files
[i
], &size
, &size_high
);
614 if (size
!= srv_calc_low32(srv_log_file_size
)
615 || size_high
!= srv_calc_high32(srv_log_file_size
)) {
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
));
629 *log_file_created
= TRUE
;
631 ut_print_timestamp(stderr
);
634 " InnoDB: Log file %s did not exist:"
635 " new to be created\n",
637 if (log_file_has_been_opened
) {
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
));
647 "InnoDB: Database physically writes the file"
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
));
655 "InnoDB: Error in creating %s:"
656 " probably out of disk space\n",
663 ret
= os_file_close(files
[i
]);
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
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
);
688 arch_space_id
= ULINT_UNDEFINED
;
690 #endif /* UNIV_LOG_ARCHIVE */
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
702 /*********************************************************************//**
703 Creates or opens database data files and closes them.
704 @return DB_SUCCESS or error code */
707 open_or_create_data_files(
708 /*======================*/
709 ibool
* create_new_db
, /*!< out: TRUE if new database should be
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
726 ibool one_opened
= FALSE
;
727 ibool one_created
= FALSE
;
730 ulint rounded_size_pages
;
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
);
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
++) {
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
,
771 if (ret
== FALSE
&& os_file_get_last_error(FALSE
)
772 != OS_FILE_ALREADY_EXISTS
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
781 "InnoDB: Error in creating"
787 } else if (srv_data_file_is_raw_partition
[i
] == SRV_NEW_RAW
) {
788 /* The partition is opened, not created; then it is
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
,
799 "InnoDB: Error in opening %s\n", name
);
803 } else if (srv_data_file_is_raw_partition
[i
] == SRV_OLD_RAW
) {
804 srv_start_raw_disk_in_use
= TRUE
;
812 /* We open the data file */
816 "InnoDB: Error: data files can only"
817 " be added at the end\n");
819 "InnoDB: of a tablespace, but"
820 " data file %s existed beforehand.\n",
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
);
830 files
[i
] = os_file_create(
831 name
, OS_FILE_OPEN_RETRY
,
832 OS_FILE_NORMAL
, OS_DATA_FILE
, &ret
);
834 files
[i
] = os_file_create(
835 name
, OS_FILE_OPEN
, OS_FILE_NORMAL
,
841 "InnoDB: Error in opening %s\n", name
);
842 os_file_get_last_error(TRUE
);
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
);
854 /* Round size downward to megabytes */
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
)) {
869 "InnoDB: Error: auto-extending"
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",
879 (ulong
) rounded_size_pages
,
880 (ulong
) srv_data_file_sizes
[i
],
882 srv_last_file_size_max
);
887 srv_data_file_sizes
[i
] = rounded_size_pages
;
890 if (rounded_size_pages
!= srv_data_file_sizes
[i
]) {
893 "InnoDB: Error: data file %s"
894 " is of a different size\n"
896 " (rounded down to MB)\n"
897 "InnoDB: than specified"
898 " in the .cnf file %lu pages!\n",
900 (ulong
) rounded_size_pages
,
901 (ulong
) srv_data_file_sizes
[i
]);
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
);
914 /* We created the data file and now write it full of
920 ut_print_timestamp(stderr
);
922 " InnoDB: Data file %s did not"
923 " exist: new to be created\n",
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
);
936 " InnoDB: Setting file %s size to %lu MB\n",
938 (ulong
) (srv_data_file_sizes
[i
]
939 >> (20 - UNIV_PAGE_SIZE_SHIFT
)));
942 "InnoDB: Database physically writes the"
943 " file full: wait...\n");
945 ret
= os_file_set_size(
947 srv_calc_low32(srv_data_file_sizes
[i
]),
948 srv_calc_high32(srv_data_file_sizes
[i
]));
952 "InnoDB: Error in creating %s:"
953 " probably out of disk space\n", name
);
958 *sum_of_new_sizes
= *sum_of_new_sizes
959 + srv_data_file_sizes
[i
];
962 ret
= os_file_close(files
[i
]);
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);
977 mutex_create(&ios_mutex
, SYNC_NO_ORDER_CHECK
);
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 */
988 innobase_start_or_create_for_mysql(void)
989 /*====================================*/
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
;
1008 my_bool srv_file_per_table_original_value
1009 = srv_file_per_table
;
1011 #ifdef HAVE_DARWIN_THREADS
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
);
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*)) {
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
;
1051 "InnoDB: !!!!!!!! UNIV_DEBUG switched on !!!!!!!!!\n");
1054 #ifdef UNIV_IBUF_DEBUG
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"
1064 #ifdef UNIV_BLOB_DEBUG
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
1072 "InnoDB: !!!!!!!! UNIV_SYNC_DEBUG switched on !!!!!!!!!\n");
1075 #ifdef UNIV_SEARCH_DEBUG
1077 "InnoDB: !!!!!!!! UNIV_SEARCH_DEBUG switched on !!!!!!!!!\n");
1080 #ifdef UNIV_LOG_LSN_DEBUG
1082 "InnoDB: !!!!!!!! UNIV_LOG_LSN_DEBUG switched on !!!!!!!!!\n");
1083 #endif /* UNIV_LOG_LSN_DEBUG */
1084 #ifdef UNIV_MEM_DEBUG
1086 "InnoDB: !!!!!!!! UNIV_MEM_DEBUG switched on !!!!!!!!!\n");
1089 if (UNIV_LIKELY(srv_use_sys_malloc
)) {
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
1098 #endif /* UNIV_ZIP_DEBUG */
1099 #ifdef UNIV_ZIP_COPY
1100 " and extra copying"
1101 #endif /* UNIV_ZIP_COPY */
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
) {
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
;
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
;
1131 switch (os_get_os_version()) {
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
;
1143 /* On Win 2000 and XP use async i/o */
1144 os_aio_use_native_aio
= TRUE
;
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
;
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
;
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
;
1184 "InnoDB: Unrecognized value %s for"
1185 " innodb_flush_method\n",
1186 srv_file_flush_method_str
);
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
1204 srv_max_n_threads
= 1000;
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;
1215 srv_max_n_threads
= 1000; /* saves several MB of memory,
1216 especially in 64-bit
1222 if (err
!= DB_SUCCESS
) {
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
));
1242 srv_monitor_file_name
= NULL
;
1243 srv_monitor_file
= os_file_create_tmpfile();
1244 if (!srv_monitor_file
) {
1249 mutex_create(&srv_dict_tmpfile_mutex
, SYNC_DICT_OPERATION
);
1251 srv_dict_tmpfile
= os_file_create_tmpfile();
1252 if (!srv_dict_tmpfile
) {
1256 mutex_create(&srv_misc_tmpfile_mutex
, SYNC_ANY_LATCH
);
1258 srv_misc_tmpfile
= os_file_create_tmpfile();
1259 if (!srv_misc_tmpfile
) {
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
;
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
);
1298 " InnoDB: Initializing buffer pool, size =");
1300 if (srv_buf_pool_size
>= 1024 * 1024 * 1024) {
1303 ((double) srv_buf_pool_size
) / (1024 * 1024 * 1024));
1307 ((double) srv_buf_pool_size
) / (1024 * 1024));
1310 ret
= buf_pool_init();
1312 ut_print_timestamp(stderr
);
1316 "InnoDB: Fatal error: cannot allocate the memory"
1317 " for the buffer pool\n");
1323 " InnoDB: Completed initialization of buffer pool\n");
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);
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
++) {
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
)) {
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");
1360 #endif /* UNIV_LOG_ARCHIVE */
1362 if (srv_n_log_files
* srv_log_file_size
>= 262144) {
1364 "InnoDB: Error: combined size of log files"
1365 " must be < 4 GB\n");
1370 sum_of_new_sizes
= 0;
1372 for (i
= 0; i
< srv_n_data_files
; i
++) {
1374 if (sizeof(off_t
) < 5 && srv_data_file_sizes
[i
] >= 262144) {
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");
1384 sum_of_new_sizes
+= srv_data_file_sizes
[i
];
1387 if (sum_of_new_sizes
< 10485760 / UNIV_PAGE_SIZE
) {
1389 "InnoDB: Error: tablespace size must be"
1390 " at least 10 MB\n");
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
,
1401 if (err
!= DB_SUCCESS
) {
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"
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");
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
,
1428 if (err
!= DB_SUCCESS
) {
1433 if (log_file_created
) {
1438 if ((log_opened
&& create_new_db
)
1439 || (log_opened
&& log_created
)) {
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");
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 */
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");
1482 if (max_flushed_lsn
< (ib_uint64_t
) 1000) {
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");
1497 mutex_enter(&(log_sys
->mutex
));
1499 #ifdef UNIV_LOG_ARCHIVE
1500 /* Do not + 1 arch_log_no because we do not use log
1502 recv_reset_logs(max_flushed_lsn
, max_arch_log_no
, TRUE
);
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
) {
1514 fsp_header_init(0, sum_of_new_sizes
, &mtr
);
1520 srv_startup_is_before_trx_rollback_phase
= FALSE
;
1522 #ifdef UNIV_LOG_ARCHIVE
1523 } else if (srv_archive_recovery
) {
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
,
1530 if (err
!= DB_SUCCESS
) {
1534 /* Since ibuf init is in dict_boot, and ibuf is needed
1535 in any disk i/o, first call 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
1543 fsp_header_get_free_limit();
1545 recv_recovery_from_archive_finish();
1546 #endif /* UNIV_LOG_ARCHIVE */
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
1561 err
= trx_sys_file_format_max_check(
1562 srv_check_file_format_at_startup
);
1564 if (err
!= DB_SUCCESS
) {
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
,
1582 if (err
!= DB_SUCCESS
) {
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. */
1594 trx_sys_init_at_db_start();
1596 /* Initialize the fsp free limit global variable in the log
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
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
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 */
1640 fsp_header_inc_size(0, sum_of_new_sizes
, &mtr
);
1644 /* Immediately write the log record about increased tablespace
1645 size to disk, so that it is durable even if mysqld would crash
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());
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
1704 os_thread_create(&srv_master_thread
, NULL
, thread_ids
1705 + (1 + SRV_MAX_N_IO_THREADS
));
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
) {
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
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");
1748 if (srv_auto_extend_last_data_file
1749 && sum_of_data_file_sizes
< tablespace_size_in_header
) {
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) {
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");
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
)) {
1782 "InnoDB: Error: pthread_mutex_trylock returns"
1783 " an unexpected value on\n"
1784 "InnoDB: success! Cannot continue.\n");
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
);
1799 " InnoDB Plugin %s started; "
1800 "log sequence number %llu\n",
1801 INNODB_VERSION_STR
, srv_start_lsn
);
1804 if (srv_force_recovery
> 0) {
1806 "InnoDB: !!! innodb_force_recovery"
1807 " is set to %lu !!!\n",
1808 (ulong
) srv_force_recovery
);
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
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");
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());
1845 "InnoDB: Full purge and insert buffer merge"
1848 trx_sys_mark_upgraded_to_multiple_tablespaces();
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"
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
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 */
1882 innobase_shutdown_for_mysql(void)
1883 /*=============================*/
1887 extern ibool panic_shutdown
;
1889 if (!srv_was_started
) {
1890 if (srv_is_being_started
) {
1891 ut_print_timestamp(stderr
);
1893 " InnoDB: Warning: shutting down"
1894 " a not properly started\n"
1895 "InnoDB: or created database!\n");
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
);
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");
1918 if (!panic_shutdown
)
1920 logs_empty_and_mark_files_at_shutdown();
1922 if (srv_conc_n_threads
!= 0) {
1924 "InnoDB: Warning: query counter shows %ld queries"
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) {
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
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);
1981 os_mutex_exit(os_sync_mutex
);
1983 os_thread_sleep(100000);
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();
2019 trx_sys_file_format_close();
2022 mutex_free(&srv_monitor_file_mutex
);
2023 mutex_free(&srv_dict_tmpfile_mutex
);
2024 mutex_free(&srv_misc_tmpfile_mutex
);
2026 btr_search_sys_free();
2028 /* 3. Free all InnoDB's own mutexes and the os_fast_mutexes inside
2035 /* 4. Free the os_conc_mutex and all os_events and os_mutexes */
2039 /* 5. Free all allocated memory */
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 */
2051 if (os_thread_count
!= 0
2052 || os_event_count
!= 0
2053 || os_mutex_count
!= 0
2054 || os_fast_mutex_count
!= 0) {
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
);
2074 " InnoDB: Shutdown completed;"
2075 " log sequence number %llu\n",
2079 srv_was_started
= FALSE
;
2080 srv_start_has_been_called
= FALSE
;
2082 return((int) DB_SUCCESS
);
2086 void set_panic_flag_for_netware()
2088 extern ibool panic_shutdown
;
2089 panic_shutdown
= TRUE
;
2091 #endif /* __NETWARE__ */
2092 #endif /* !UNIV_HOTBACKUP */