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

2036 lines
51 KiB

20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
branches/zip: Merge 2437:2485 from branches/5.1: (r2478 was skipped for the obvious reason) ------------------------------------------------------------------------ r2464 | vasil | 2008-05-19 17:59:42 +0300 (Mon, 19 May 2008) | 9 lines branches/5.1: Fix Bug#36600 SHOW STATUS takes a lot of CPU in buf_get_latched_pages_number by removing the Innodb_buffer_pool_pages_latched variable from SHOW STATUS output in non-UNIV_DEBUG compilation. Approved by: Heikki ------------------------------------------------------------------------ r2466 | calvin | 2008-05-20 01:37:14 +0300 (Tue, 20 May 2008) | 12 lines branches/5.1: Fix Bug#11894 innodb_file_per_table crashes w/ Windows .sym symbolic link hack The crash was due to un-handled error 3 (path not found). In the case of file per table, change the call to os_file_handle_error_no_exit() from os_file_handle_error(). Also, checks for full path pattern during table create (Windows only), which is used in symbolic link and temp table creation. Approved by: Heikki ------------------------------------------------------------------------ r2478 | sunny | 2008-05-23 08:29:08 +0300 (Fri, 23 May 2008) | 3 lines branches/5.1: Fix for bug# 36793. This is a back port from branches/zip. This code has been tested on a big-endian machine too. ------------------------------------------------------------------------ r2480 | vasil | 2008-05-27 11:40:07 +0300 (Tue, 27 May 2008) | 11 lines branches/5.1: Fix Bug#36819 ut_usectime does not handle errors from gettimeofday by retrying gettimeofday() several times if it fails in ut_usectime(). If it fails on all calls then return error to the caller to be handled at higher level. Update the variable innodb_row_lock_time_max in SHOW STATUS output only if ut_usectime() was successful. ------------------------------------------------------------------------ r2482 | sunny | 2008-05-28 12:18:35 +0300 (Wed, 28 May 2008) | 5 lines branches/5.1: Fix for Bug#35602, "Failed to read auto-increment value from storage engine". The test for REPLACE was an error of ommission since it's classified as a simple INSERT. For REPLACE statements we don't acquire the special AUTOINC lock for AUTOINC_NEW_STYLE_LOCKING with this fix. ------------------------------------------------------------------------ r2485 | vasil | 2008-05-28 16:01:14 +0300 (Wed, 28 May 2008) | 9 lines branches/5.1: Fix Bug#36149 Read buffer overflow in srv0start.c found during "make test" Use strncmp(3) instead of memcmp(3) to avoid reading past end of the string if it is empty (*str == '\0'). This bug is _not_ a buffer overflow. Discussed with: Sunny (via IM) ------------------------------------------------------------------------
18 years ago
20 years ago
branches/zip: Merge 2437:2485 from branches/5.1: (r2478 was skipped for the obvious reason) ------------------------------------------------------------------------ r2464 | vasil | 2008-05-19 17:59:42 +0300 (Mon, 19 May 2008) | 9 lines branches/5.1: Fix Bug#36600 SHOW STATUS takes a lot of CPU in buf_get_latched_pages_number by removing the Innodb_buffer_pool_pages_latched variable from SHOW STATUS output in non-UNIV_DEBUG compilation. Approved by: Heikki ------------------------------------------------------------------------ r2466 | calvin | 2008-05-20 01:37:14 +0300 (Tue, 20 May 2008) | 12 lines branches/5.1: Fix Bug#11894 innodb_file_per_table crashes w/ Windows .sym symbolic link hack The crash was due to un-handled error 3 (path not found). In the case of file per table, change the call to os_file_handle_error_no_exit() from os_file_handle_error(). Also, checks for full path pattern during table create (Windows only), which is used in symbolic link and temp table creation. Approved by: Heikki ------------------------------------------------------------------------ r2478 | sunny | 2008-05-23 08:29:08 +0300 (Fri, 23 May 2008) | 3 lines branches/5.1: Fix for bug# 36793. This is a back port from branches/zip. This code has been tested on a big-endian machine too. ------------------------------------------------------------------------ r2480 | vasil | 2008-05-27 11:40:07 +0300 (Tue, 27 May 2008) | 11 lines branches/5.1: Fix Bug#36819 ut_usectime does not handle errors from gettimeofday by retrying gettimeofday() several times if it fails in ut_usectime(). If it fails on all calls then return error to the caller to be handled at higher level. Update the variable innodb_row_lock_time_max in SHOW STATUS output only if ut_usectime() was successful. ------------------------------------------------------------------------ r2482 | sunny | 2008-05-28 12:18:35 +0300 (Wed, 28 May 2008) | 5 lines branches/5.1: Fix for Bug#35602, "Failed to read auto-increment value from storage engine". The test for REPLACE was an error of ommission since it's classified as a simple INSERT. For REPLACE statements we don't acquire the special AUTOINC lock for AUTOINC_NEW_STYLE_LOCKING with this fix. ------------------------------------------------------------------------ r2485 | vasil | 2008-05-28 16:01:14 +0300 (Wed, 28 May 2008) | 9 lines branches/5.1: Fix Bug#36149 Read buffer overflow in srv0start.c found during "make test" Use strncmp(3) instead of memcmp(3) to avoid reading past end of the string if it is empty (*str == '\0'). This bug is _not_ a buffer overflow. Discussed with: Sunny (via IM) ------------------------------------------------------------------------
18 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
branches/zip: Merge 2437:2485 from branches/5.1: (r2478 was skipped for the obvious reason) ------------------------------------------------------------------------ r2464 | vasil | 2008-05-19 17:59:42 +0300 (Mon, 19 May 2008) | 9 lines branches/5.1: Fix Bug#36600 SHOW STATUS takes a lot of CPU in buf_get_latched_pages_number by removing the Innodb_buffer_pool_pages_latched variable from SHOW STATUS output in non-UNIV_DEBUG compilation. Approved by: Heikki ------------------------------------------------------------------------ r2466 | calvin | 2008-05-20 01:37:14 +0300 (Tue, 20 May 2008) | 12 lines branches/5.1: Fix Bug#11894 innodb_file_per_table crashes w/ Windows .sym symbolic link hack The crash was due to un-handled error 3 (path not found). In the case of file per table, change the call to os_file_handle_error_no_exit() from os_file_handle_error(). Also, checks for full path pattern during table create (Windows only), which is used in symbolic link and temp table creation. Approved by: Heikki ------------------------------------------------------------------------ r2478 | sunny | 2008-05-23 08:29:08 +0300 (Fri, 23 May 2008) | 3 lines branches/5.1: Fix for bug# 36793. This is a back port from branches/zip. This code has been tested on a big-endian machine too. ------------------------------------------------------------------------ r2480 | vasil | 2008-05-27 11:40:07 +0300 (Tue, 27 May 2008) | 11 lines branches/5.1: Fix Bug#36819 ut_usectime does not handle errors from gettimeofday by retrying gettimeofday() several times if it fails in ut_usectime(). If it fails on all calls then return error to the caller to be handled at higher level. Update the variable innodb_row_lock_time_max in SHOW STATUS output only if ut_usectime() was successful. ------------------------------------------------------------------------ r2482 | sunny | 2008-05-28 12:18:35 +0300 (Wed, 28 May 2008) | 5 lines branches/5.1: Fix for Bug#35602, "Failed to read auto-increment value from storage engine". The test for REPLACE was an error of ommission since it's classified as a simple INSERT. For REPLACE statements we don't acquire the special AUTOINC lock for AUTOINC_NEW_STYLE_LOCKING with this fix. ------------------------------------------------------------------------ r2485 | vasil | 2008-05-28 16:01:14 +0300 (Wed, 28 May 2008) | 9 lines branches/5.1: Fix Bug#36149 Read buffer overflow in srv0start.c found during "make test" Use strncmp(3) instead of memcmp(3) to avoid reading past end of the string if it is empty (*str == '\0'). This bug is _not_ a buffer overflow. Discussed with: Sunny (via IM) ------------------------------------------------------------------------
18 years ago
20 years ago
branches/zip: Merge 2437:2485 from branches/5.1: (r2478 was skipped for the obvious reason) ------------------------------------------------------------------------ r2464 | vasil | 2008-05-19 17:59:42 +0300 (Mon, 19 May 2008) | 9 lines branches/5.1: Fix Bug#36600 SHOW STATUS takes a lot of CPU in buf_get_latched_pages_number by removing the Innodb_buffer_pool_pages_latched variable from SHOW STATUS output in non-UNIV_DEBUG compilation. Approved by: Heikki ------------------------------------------------------------------------ r2466 | calvin | 2008-05-20 01:37:14 +0300 (Tue, 20 May 2008) | 12 lines branches/5.1: Fix Bug#11894 innodb_file_per_table crashes w/ Windows .sym symbolic link hack The crash was due to un-handled error 3 (path not found). In the case of file per table, change the call to os_file_handle_error_no_exit() from os_file_handle_error(). Also, checks for full path pattern during table create (Windows only), which is used in symbolic link and temp table creation. Approved by: Heikki ------------------------------------------------------------------------ r2478 | sunny | 2008-05-23 08:29:08 +0300 (Fri, 23 May 2008) | 3 lines branches/5.1: Fix for bug# 36793. This is a back port from branches/zip. This code has been tested on a big-endian machine too. ------------------------------------------------------------------------ r2480 | vasil | 2008-05-27 11:40:07 +0300 (Tue, 27 May 2008) | 11 lines branches/5.1: Fix Bug#36819 ut_usectime does not handle errors from gettimeofday by retrying gettimeofday() several times if it fails in ut_usectime(). If it fails on all calls then return error to the caller to be handled at higher level. Update the variable innodb_row_lock_time_max in SHOW STATUS output only if ut_usectime() was successful. ------------------------------------------------------------------------ r2482 | sunny | 2008-05-28 12:18:35 +0300 (Wed, 28 May 2008) | 5 lines branches/5.1: Fix for Bug#35602, "Failed to read auto-increment value from storage engine". The test for REPLACE was an error of ommission since it's classified as a simple INSERT. For REPLACE statements we don't acquire the special AUTOINC lock for AUTOINC_NEW_STYLE_LOCKING with this fix. ------------------------------------------------------------------------ r2485 | vasil | 2008-05-28 16:01:14 +0300 (Wed, 28 May 2008) | 9 lines branches/5.1: Fix Bug#36149 Read buffer overflow in srv0start.c found during "make test" Use strncmp(3) instead of memcmp(3) to avoid reading past end of the string if it is empty (*str == '\0'). This bug is _not_ a buffer overflow. Discussed with: Sunny (via IM) ------------------------------------------------------------------------
18 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
branches/zip: Implement the configuration parameter and settable global variable innodb_file_format. Implement file format version stamping of *.ibd files and SYS_TABLES.TYPE. This change breaks introduces an incompatible change for for compressed tables. We can do this, as we have not released yet. innodb-zip.test: Add tests for stricter KEY_BLOCK_SIZE and ROW_FORMAT checks. DICT_TF_COMPRESSED_MASK, DICT_TF_COMPRESSED_SHIFT: Replace with DICT_TF_ZSSIZE_MASK, DICT_TF_ZSSIZE_SHIFT. DICT_TF_FORMAT_MASK, DICT_TF_FORMAT_SHIFT, DICT_TF_FORMAT_51, DICT_TF_FORMAT_ZIP: File format version, stored in table->flags, in the .ibd file header, and in SYS_TABLES.TYPE. dict_create_sys_tables_tuple(): Write the table flags to SYS_TABLES.TYPE if the format is at least DICT_TF_FORMAT_ZIP. For old formats (DICT_TF_FORMAT_51), write DICT_TABLE_ORDINARY as the table type. DB_TABLE_ZIP_NO_IBD: Remove the error code. The error handling is done in ha_innodb.cc; as a failsafe measure, dict_build_table_def_step() will silently clear the compression and format flags instead of returning this error. dict_mem_table_create(): Assert that no extra bits are set in the flags. dict_sys_tables_get_zip_size(): Rename to dict_sys_tables_get_flags(). Check all flag bits, and return ULINT_UNDEFINED if the combination is unsupported. dict_boot(): Document the SYS_TABLES columns N_COLS and TYPE. dict_table_get_format(), dict_table_set_format(), dict_table_flags_to_zip_size(): New accessors to table->flags. dtuple_convert_big_rec(): Introduce the auxiliary variables local_len, local_prefix_len. Store a 768-byte prefix locally if the file format is less than DICT_TF_FORMAT_ZIP. dtuple_convert_back_big_rec(): Restore the columns. srv_file_format: New variable: innodb_file_format. fil_create_new_single_table_tablespace(): Replace the parameter zip_size with table->flags. fil_open_single_table_tablespace(): Replace the parameter zip_size_in_k with table->flags. Check the flags. fil_space_struct, fil_space_create(), fil_op_write_log(): Replace zip_size with flags. fil_node_open_file(): Note a TODO item for InnoDB Hot Backup. Check that the tablespace flags match. fil_space_get_zip_size(): Rename to fil_space_get_flags(). Add a wrapper for fil_space_get_zip_size(). fsp_header_get_flags(): New function. fsp_header_init_fields(): Replace zip_size with flags. FSP_SPACE_FLAGS: New name for the tablespace flags. This field used to be called FSP_PAGE_ZIP_SIZE, or FSP_LOWEST_NO_WRITE. It has always been written as 0 in MySQL/InnoDB versions 4.1 to 5.1. MLOG_ZIP_FILE_CREATE: Rename to MLOG_FILE_CREATE2. Add a 32-bit parameter for the tablespace flags. ha_innobase::create(): Check the table attributes ROW_FORMAT and KEY_BLOCK_SIZE. Issue errors if they are inappropriate, or warnings if the inherited attributes (in ALTER TABLE) will be ignored. PAGE_ZIP_MIN_SIZE_SHIFT: New constant: the 2-logarithm of PAGE_ZIP_MIN_SIZE.
18 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1996, 2009, Innobase Oy. All Rights Reserved.
  3. Copyright (c) 2008, Google Inc.
  4. Portions of this file contain modifications contributed and copyrighted by
  5. Google, Inc. Those modifications are gratefully acknowledged and are described
  6. briefly in the InnoDB documentation. The contributions by Google are
  7. incorporated with their permission, and subject to the conditions contained in
  8. the file COPYING.Google.
  9. This program is free software; you can redistribute it and/or modify it under
  10. the terms of the GNU General Public License as published by the Free Software
  11. Foundation; version 2 of the License.
  12. This program is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  14. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License along with
  16. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  17. Place, Suite 330, Boston, MA 02111-1307 USA
  18. *****************************************************************************/
  19. /************************************************************************
  20. Starts the InnoDB database server
  21. Created 2/16/1996 Heikki Tuuri
  22. *************************************************************************/
  23. #include "os0proc.h"
  24. #include "sync0sync.h"
  25. #include "ut0mem.h"
  26. #include "mem0mem.h"
  27. #include "mem0pool.h"
  28. #include "data0data.h"
  29. #include "data0type.h"
  30. #include "dict0dict.h"
  31. #include "buf0buf.h"
  32. #include "buf0flu.h"
  33. #include "buf0rea.h"
  34. #include "os0file.h"
  35. #include "os0thread.h"
  36. #include "fil0fil.h"
  37. #include "fsp0fsp.h"
  38. #include "rem0rec.h"
  39. #include "rem0cmp.h"
  40. #include "mtr0mtr.h"
  41. #include "log0log.h"
  42. #include "log0recv.h"
  43. #include "page0page.h"
  44. #include "page0cur.h"
  45. #include "trx0trx.h"
  46. #include "dict0boot.h"
  47. #include "dict0load.h"
  48. #include "trx0sys.h"
  49. #include "dict0crea.h"
  50. #include "btr0btr.h"
  51. #include "btr0pcur.h"
  52. #include "btr0cur.h"
  53. #include "btr0sea.h"
  54. #include "rem0rec.h"
  55. #include "srv0srv.h"
  56. #include "que0que.h"
  57. #include "usr0sess.h"
  58. #include "lock0lock.h"
  59. #include "trx0roll.h"
  60. #include "trx0purge.h"
  61. #include "row0ins.h"
  62. #include "row0sel.h"
  63. #include "row0upd.h"
  64. #include "row0row.h"
  65. #include "row0mysql.h"
  66. #include "lock0lock.h"
  67. #include "ibuf0ibuf.h"
  68. #include "pars0pars.h"
  69. #include "btr0sea.h"
  70. #include "srv0start.h"
  71. #include "que0que.h"
  72. /* Log sequence number immediately after startup */
  73. UNIV_INTERN ib_uint64_t srv_start_lsn;
  74. /* Log sequence number at shutdown */
  75. UNIV_INTERN ib_uint64_t srv_shutdown_lsn;
  76. #ifdef HAVE_DARWIN_THREADS
  77. # include <sys/utsname.h>
  78. UNIV_INTERN ibool srv_have_fullfsync = FALSE;
  79. #endif
  80. UNIV_INTERN ibool srv_start_raw_disk_in_use = FALSE;
  81. UNIV_INTERN ibool srv_startup_is_before_trx_rollback_phase = FALSE;
  82. UNIV_INTERN ibool srv_is_being_started = FALSE;
  83. UNIV_INTERN ibool srv_was_started = FALSE;
  84. #ifndef UNIV_HOTBACKUP
  85. static ibool srv_start_has_been_called = FALSE;
  86. #endif /* !UNIV_HOTBACKUP */
  87. /* At a shutdown the value first climbs to SRV_SHUTDOWN_CLEANUP
  88. and then to SRV_SHUTDOWN_LAST_PHASE */
  89. UNIV_INTERN ulint srv_shutdown_state = 0;
  90. #ifndef UNIV_HOTBACKUP
  91. static os_file_t files[1000];
  92. static mutex_t ios_mutex;
  93. static ulint ios;
  94. static ulint n[SRV_MAX_N_IO_THREADS + 5];
  95. static os_thread_id_t thread_ids[SRV_MAX_N_IO_THREADS + 5];
  96. /* We use this mutex to test the return value of pthread_mutex_trylock
  97. on successful locking. HP-UX does NOT return 0, though Linux et al do. */
  98. static os_fast_mutex_t srv_os_test_mutex;
  99. /* Name of srv_monitor_file */
  100. static char* srv_monitor_file_name;
  101. #endif /* !UNIV_HOTBACKUP */
  102. #define SRV_N_PENDING_IOS_PER_THREAD OS_AIO_N_PENDING_IOS_PER_THREAD
  103. #define SRV_MAX_N_PENDING_SYNC_IOS 100
  104. /* Avoid warnings when using purify */
  105. #ifdef HAVE_purify
  106. static int inno_bcmp(register const char *s1, register const char *s2,
  107. register uint len)
  108. {
  109. while ((len-- != 0) && (*s1++ == *s2++))
  110. ;
  111. return(len + 1);
  112. }
  113. #define memcmp(A,B,C) inno_bcmp((A),(B),(C))
  114. #endif
  115. static
  116. char*
  117. srv_parse_megabytes(
  118. /*================*/
  119. /* out: next character in string */
  120. char* str, /* in: string containing a quantity in bytes */
  121. ulint* megs) /* out: the number in megabytes */
  122. {
  123. char* endp;
  124. ulint size;
  125. size = strtoul(str, &endp, 10);
  126. str = endp;
  127. switch (*str) {
  128. case 'G': case 'g':
  129. size *= 1024;
  130. /* fall through */
  131. case 'M': case 'm':
  132. str++;
  133. break;
  134. default:
  135. size /= 1024 * 1024;
  136. break;
  137. }
  138. *megs = size;
  139. return(str);
  140. }
  141. /*************************************************************************
  142. Reads the data files and their sizes from a character string given in
  143. the .cnf file. */
  144. UNIV_INTERN
  145. ibool
  146. srv_parse_data_file_paths_and_sizes(
  147. /*================================*/
  148. /* out: TRUE if ok, FALSE if parsing
  149. error */
  150. char* str, /* in: the data file path string */
  151. char*** data_file_names, /* out, own: array of data file
  152. names */
  153. ulint** data_file_sizes, /* out, own: array of data file sizes
  154. in megabytes */
  155. ulint** data_file_is_raw_partition,/* out, own: array of flags
  156. showing which data files are raw
  157. partitions */
  158. ulint* n_data_files, /* out: number of data files */
  159. ibool* is_auto_extending, /* out: TRUE if the last data file is
  160. auto-extending */
  161. ulint* max_auto_extend_size) /* out: max auto extend size for the
  162. last file if specified, 0 if not */
  163. {
  164. char* input_str;
  165. char* path;
  166. ulint size;
  167. ulint i = 0;
  168. *is_auto_extending = FALSE;
  169. *max_auto_extend_size = 0;
  170. input_str = str;
  171. /* First calculate the number of data files and check syntax:
  172. path:size[M | G];path:size[M | G]... . Note that a Windows path may
  173. contain a drive name and a ':'. */
  174. while (*str != '\0') {
  175. path = str;
  176. while ((*str != ':' && *str != '\0')
  177. || (*str == ':'
  178. && (*(str + 1) == '\\' || *(str + 1) == '/'
  179. || *(str + 1) == ':'))) {
  180. str++;
  181. }
  182. if (*str == '\0') {
  183. return(FALSE);
  184. }
  185. str++;
  186. str = srv_parse_megabytes(str, &size);
  187. if (0 == strncmp(str, ":autoextend",
  188. (sizeof ":autoextend") - 1)) {
  189. str += (sizeof ":autoextend") - 1;
  190. if (0 == strncmp(str, ":max:",
  191. (sizeof ":max:") - 1)) {
  192. str += (sizeof ":max:") - 1;
  193. str = srv_parse_megabytes(str, &size);
  194. }
  195. if (*str != '\0') {
  196. return(FALSE);
  197. }
  198. }
  199. if (strlen(str) >= 6
  200. && *str == 'n'
  201. && *(str + 1) == 'e'
  202. && *(str + 2) == 'w') {
  203. str += 3;
  204. }
  205. if (*str == 'r' && *(str + 1) == 'a' && *(str + 2) == 'w') {
  206. str += 3;
  207. }
  208. if (size == 0) {
  209. return(FALSE);
  210. }
  211. i++;
  212. if (*str == ';') {
  213. str++;
  214. } else if (*str != '\0') {
  215. return(FALSE);
  216. }
  217. }
  218. if (i == 0) {
  219. /* If innodb_data_file_path was defined it must contain
  220. at least one data file definition */
  221. return(FALSE);
  222. }
  223. *data_file_names = (char**)ut_malloc(i * sizeof(void*));
  224. *data_file_sizes = (ulint*)ut_malloc(i * sizeof(ulint));
  225. *data_file_is_raw_partition = (ulint*)ut_malloc(i * sizeof(ulint));
  226. *n_data_files = i;
  227. /* Then store the actual values to our arrays */
  228. str = input_str;
  229. i = 0;
  230. while (*str != '\0') {
  231. path = str;
  232. /* Note that we must step over the ':' in a Windows path;
  233. a Windows path normally looks like C:\ibdata\ibdata1:1G, but
  234. a Windows raw partition may have a specification like
  235. \\.\C::1Gnewraw or \\.\PHYSICALDRIVE2:1Gnewraw */
  236. while ((*str != ':' && *str != '\0')
  237. || (*str == ':'
  238. && (*(str + 1) == '\\' || *(str + 1) == '/'
  239. || *(str + 1) == ':'))) {
  240. str++;
  241. }
  242. if (*str == ':') {
  243. /* Make path a null-terminated string */
  244. *str = '\0';
  245. str++;
  246. }
  247. str = srv_parse_megabytes(str, &size);
  248. (*data_file_names)[i] = path;
  249. (*data_file_sizes)[i] = size;
  250. if (0 == strncmp(str, ":autoextend",
  251. (sizeof ":autoextend") - 1)) {
  252. *is_auto_extending = TRUE;
  253. str += (sizeof ":autoextend") - 1;
  254. if (0 == strncmp(str, ":max:",
  255. (sizeof ":max:") - 1)) {
  256. str += (sizeof ":max:") - 1;
  257. str = srv_parse_megabytes(
  258. str, max_auto_extend_size);
  259. }
  260. if (*str != '\0') {
  261. return(FALSE);
  262. }
  263. }
  264. (*data_file_is_raw_partition)[i] = 0;
  265. if (strlen(str) >= 6
  266. && *str == 'n'
  267. && *(str + 1) == 'e'
  268. && *(str + 2) == 'w') {
  269. str += 3;
  270. (*data_file_is_raw_partition)[i] = SRV_NEW_RAW;
  271. }
  272. if (*str == 'r' && *(str + 1) == 'a' && *(str + 2) == 'w') {
  273. str += 3;
  274. if ((*data_file_is_raw_partition)[i] == 0) {
  275. (*data_file_is_raw_partition)[i] = SRV_OLD_RAW;
  276. }
  277. }
  278. i++;
  279. if (*str == ';') {
  280. str++;
  281. }
  282. }
  283. return(TRUE);
  284. }
  285. /*************************************************************************
  286. Reads log group home directories from a character string given in
  287. the .cnf file. */
  288. UNIV_INTERN
  289. ibool
  290. srv_parse_log_group_home_dirs(
  291. /*==========================*/
  292. /* out: TRUE if ok, FALSE if parsing
  293. error */
  294. char* str, /* in: character string */
  295. char*** log_group_home_dirs) /* out, own: log group home dirs */
  296. {
  297. char* input_str;
  298. char* path;
  299. ulint i = 0;
  300. input_str = str;
  301. /* First calculate the number of directories and check syntax:
  302. path;path;... */
  303. while (*str != '\0') {
  304. path = str;
  305. while (*str != ';' && *str != '\0') {
  306. str++;
  307. }
  308. i++;
  309. if (*str == ';') {
  310. str++;
  311. } else if (*str != '\0') {
  312. return(FALSE);
  313. }
  314. }
  315. if (i != 1) {
  316. /* If innodb_log_group_home_dir was defined it must
  317. contain exactly one path definition under current MySQL */
  318. return(FALSE);
  319. }
  320. *log_group_home_dirs = (char**) ut_malloc(i * sizeof(void*));
  321. /* Then store the actual values to our array */
  322. str = input_str;
  323. i = 0;
  324. while (*str != '\0') {
  325. path = str;
  326. while (*str != ';' && *str != '\0') {
  327. str++;
  328. }
  329. if (*str == ';') {
  330. *str = '\0';
  331. str++;
  332. }
  333. (*log_group_home_dirs)[i] = path;
  334. i++;
  335. }
  336. return(TRUE);
  337. }
  338. #ifndef UNIV_HOTBACKUP
  339. /************************************************************************
  340. I/o-handler thread function. */
  341. static
  342. os_thread_ret_t
  343. io_handler_thread(
  344. /*==============*/
  345. void* arg)
  346. {
  347. ulint segment;
  348. ulint i;
  349. segment = *((ulint*)arg);
  350. #ifdef UNIV_DEBUG_THREAD_CREATION
  351. fprintf(stderr, "Io handler thread %lu starts, id %lu\n", segment,
  352. os_thread_pf(os_thread_get_curr_id()));
  353. #endif
  354. for (i = 0;; i++) {
  355. fil_aio_wait(segment);
  356. mutex_enter(&ios_mutex);
  357. ios++;
  358. mutex_exit(&ios_mutex);
  359. }
  360. /* We count the number of threads in os_thread_exit(). A created
  361. thread should always use that to exit and not use return() to exit.
  362. The thread actually never comes here because it is exited in an
  363. os_event_wait(). */
  364. os_thread_exit(NULL);
  365. OS_THREAD_DUMMY_RETURN;
  366. }
  367. #endif /* !UNIV_HOTBACKUP */
  368. #ifdef __WIN__
  369. #define SRV_PATH_SEPARATOR '\\'
  370. #else
  371. #define SRV_PATH_SEPARATOR '/'
  372. #endif
  373. /*************************************************************************
  374. Normalizes a directory path for Windows: converts slashes to backslashes. */
  375. UNIV_INTERN
  376. void
  377. srv_normalize_path_for_win(
  378. /*=======================*/
  379. char* str __attribute__((unused))) /* in/out: null-terminated
  380. character string */
  381. {
  382. #ifdef __WIN__
  383. for (; *str; str++) {
  384. if (*str == '/') {
  385. *str = '\\';
  386. }
  387. }
  388. #endif
  389. }
  390. /*************************************************************************
  391. Adds a slash or a backslash to the end of a string if it is missing
  392. and the string is not empty. */
  393. UNIV_INTERN
  394. char*
  395. srv_add_path_separator_if_needed(
  396. /*=============================*/
  397. /* out: string which has the separator if the
  398. string is not empty */
  399. char* str) /* in: null-terminated character string */
  400. {
  401. char* out_str;
  402. ulint len = ut_strlen(str);
  403. if (len == 0 || str[len - 1] == SRV_PATH_SEPARATOR) {
  404. return(str);
  405. }
  406. out_str = ut_malloc(len + 2);
  407. memcpy(out_str, str, len);
  408. out_str[len] = SRV_PATH_SEPARATOR;
  409. out_str[len + 1] = 0;
  410. return(out_str);
  411. }
  412. #ifndef UNIV_HOTBACKUP
  413. /*************************************************************************
  414. Calculates the low 32 bits when a file size which is given as a number
  415. database pages is converted to the number of bytes. */
  416. static
  417. ulint
  418. srv_calc_low32(
  419. /*===========*/
  420. /* out: low 32 bytes of file size when
  421. expressed in bytes */
  422. ulint file_size) /* in: file size in database pages */
  423. {
  424. return(0xFFFFFFFFUL & (file_size << UNIV_PAGE_SIZE_SHIFT));
  425. }
  426. /*************************************************************************
  427. Calculates the high 32 bits when a file size which is given as a number
  428. database pages is converted to the number of bytes. */
  429. static
  430. ulint
  431. srv_calc_high32(
  432. /*============*/
  433. /* out: high 32 bytes of file size when
  434. expressed in bytes */
  435. ulint file_size) /* in: file size in database pages */
  436. {
  437. return(file_size >> (32 - UNIV_PAGE_SIZE_SHIFT));
  438. }
  439. /*************************************************************************
  440. Creates or opens the log files and closes them. */
  441. static
  442. ulint
  443. open_or_create_log_file(
  444. /*====================*/
  445. /* out: DB_SUCCESS or error code */
  446. ibool create_new_db, /* in: TRUE if we should create a
  447. new database */
  448. ibool* log_file_created, /* out: TRUE if new log file
  449. created */
  450. ibool log_file_has_been_opened,/* in: TRUE if a log file has been
  451. opened before: then it is an error
  452. to try to create another log file */
  453. ulint k, /* in: log group number */
  454. ulint i) /* in: log file number in group */
  455. {
  456. ibool ret;
  457. ulint size;
  458. ulint size_high;
  459. char name[10000];
  460. UT_NOT_USED(create_new_db);
  461. *log_file_created = FALSE;
  462. srv_normalize_path_for_win(srv_log_group_home_dirs[k]);
  463. srv_log_group_home_dirs[k] = srv_add_path_separator_if_needed(
  464. srv_log_group_home_dirs[k]);
  465. ut_a(strlen(srv_log_group_home_dirs[k])
  466. < (sizeof name) - 10 - sizeof "ib_logfile");
  467. sprintf(name, "%s%s%lu", srv_log_group_home_dirs[k],
  468. "ib_logfile", (ulong) i);
  469. files[i] = os_file_create(name, OS_FILE_CREATE, OS_FILE_NORMAL,
  470. OS_LOG_FILE, &ret);
  471. if (ret == FALSE) {
  472. if (os_file_get_last_error(FALSE) != OS_FILE_ALREADY_EXISTS
  473. #ifdef UNIV_AIX
  474. /* AIX 5.1 after security patch ML7 may have errno set
  475. to 0 here, which causes our function to return 100;
  476. work around that AIX problem */
  477. && os_file_get_last_error(FALSE) != 100
  478. #endif
  479. ) {
  480. fprintf(stderr,
  481. "InnoDB: Error in creating"
  482. " or opening %s\n", name);
  483. return(DB_ERROR);
  484. }
  485. files[i] = os_file_create(name, OS_FILE_OPEN, OS_FILE_AIO,
  486. OS_LOG_FILE, &ret);
  487. if (!ret) {
  488. fprintf(stderr,
  489. "InnoDB: Error in opening %s\n", name);
  490. return(DB_ERROR);
  491. }
  492. ret = os_file_get_size(files[i], &size, &size_high);
  493. ut_a(ret);
  494. if (size != srv_calc_low32(srv_log_file_size)
  495. || size_high != srv_calc_high32(srv_log_file_size)) {
  496. fprintf(stderr,
  497. "InnoDB: Error: log file %s is"
  498. " of different size %lu %lu bytes\n"
  499. "InnoDB: than specified in the .cnf"
  500. " file %lu %lu bytes!\n",
  501. name, (ulong) size_high, (ulong) size,
  502. (ulong) srv_calc_high32(srv_log_file_size),
  503. (ulong) srv_calc_low32(srv_log_file_size));
  504. return(DB_ERROR);
  505. }
  506. } else {
  507. *log_file_created = TRUE;
  508. ut_print_timestamp(stderr);
  509. fprintf(stderr,
  510. " InnoDB: Log file %s did not exist:"
  511. " new to be created\n",
  512. name);
  513. if (log_file_has_been_opened) {
  514. return(DB_ERROR);
  515. }
  516. fprintf(stderr, "InnoDB: Setting log file %s size to %lu MB\n",
  517. name, (ulong) srv_log_file_size
  518. >> (20 - UNIV_PAGE_SIZE_SHIFT));
  519. fprintf(stderr,
  520. "InnoDB: Database physically writes the file"
  521. " full: wait...\n");
  522. ret = os_file_set_size(name, files[i],
  523. srv_calc_low32(srv_log_file_size),
  524. srv_calc_high32(srv_log_file_size));
  525. if (!ret) {
  526. fprintf(stderr,
  527. "InnoDB: Error in creating %s:"
  528. " probably out of disk space\n",
  529. name);
  530. return(DB_ERROR);
  531. }
  532. }
  533. ret = os_file_close(files[i]);
  534. ut_a(ret);
  535. if (i == 0) {
  536. /* Create in memory the file space object
  537. which is for this log group */
  538. fil_space_create(name,
  539. 2 * k + SRV_LOG_SPACE_FIRST_ID, 0, FIL_LOG);
  540. }
  541. ut_a(fil_validate());
  542. fil_node_create(name, srv_log_file_size,
  543. 2 * k + SRV_LOG_SPACE_FIRST_ID, FALSE);
  544. #ifdef UNIV_LOG_ARCHIVE
  545. /* If this is the first log group, create the file space object
  546. for archived logs.
  547. Under MySQL, no archiving ever done. */
  548. if (k == 0 && i == 0) {
  549. arch_space_id = 2 * k + 1 + SRV_LOG_SPACE_FIRST_ID;
  550. fil_space_create("arch_log_space", arch_space_id, 0, FIL_LOG);
  551. } else {
  552. arch_space_id = ULINT_UNDEFINED;
  553. }
  554. #endif /* UNIV_LOG_ARCHIVE */
  555. if (i == 0) {
  556. log_group_init(k, srv_n_log_files,
  557. srv_log_file_size * UNIV_PAGE_SIZE,
  558. 2 * k + SRV_LOG_SPACE_FIRST_ID,
  559. SRV_LOG_SPACE_FIRST_ID + 1); /* dummy arch
  560. space id */
  561. }
  562. return(DB_SUCCESS);
  563. }
  564. /*************************************************************************
  565. Creates or opens database data files and closes them. */
  566. static
  567. ulint
  568. open_or_create_data_files(
  569. /*======================*/
  570. /* out: DB_SUCCESS or error code */
  571. ibool* create_new_db, /* out: TRUE if new database should be
  572. created */
  573. #ifdef UNIV_LOG_ARCHIVE
  574. ulint* min_arch_log_no,/* out: min of archived log
  575. numbers in data files */
  576. ulint* max_arch_log_no,/* out: max of archived log
  577. numbers in data files */
  578. #endif /* UNIV_LOG_ARCHIVE */
  579. ib_uint64_t* min_flushed_lsn,/* out: min of flushed lsn
  580. values in data files */
  581. ib_uint64_t* max_flushed_lsn,/* out: max of flushed lsn
  582. values in data files */
  583. ulint* sum_of_new_sizes)/* out: sum of sizes of the
  584. new files added */
  585. {
  586. ibool ret;
  587. ulint i;
  588. ibool one_opened = FALSE;
  589. ibool one_created = FALSE;
  590. ulint size;
  591. ulint size_high;
  592. ulint rounded_size_pages;
  593. char name[10000];
  594. if (srv_n_data_files >= 1000) {
  595. fprintf(stderr, "InnoDB: can only have < 1000 data files\n"
  596. "InnoDB: you have defined %lu\n",
  597. (ulong) srv_n_data_files);
  598. return(DB_ERROR);
  599. }
  600. *sum_of_new_sizes = 0;
  601. *create_new_db = FALSE;
  602. srv_normalize_path_for_win(srv_data_home);
  603. srv_data_home = srv_add_path_separator_if_needed(srv_data_home);
  604. for (i = 0; i < srv_n_data_files; i++) {
  605. srv_normalize_path_for_win(srv_data_file_names[i]);
  606. ut_a(strlen(srv_data_home) + strlen(srv_data_file_names[i])
  607. < (sizeof name) - 1);
  608. sprintf(name, "%s%s", srv_data_home, srv_data_file_names[i]);
  609. if (srv_data_file_is_raw_partition[i] == 0) {
  610. /* First we try to create the file: if it already
  611. exists, ret will get value FALSE */
  612. files[i] = os_file_create(name, OS_FILE_CREATE,
  613. OS_FILE_NORMAL,
  614. OS_DATA_FILE, &ret);
  615. if (ret == FALSE && os_file_get_last_error(FALSE)
  616. != OS_FILE_ALREADY_EXISTS
  617. #ifdef UNIV_AIX
  618. /* AIX 5.1 after security patch ML7 may have
  619. errno set to 0 here, which causes our function
  620. to return 100; work around that AIX problem */
  621. && os_file_get_last_error(FALSE) != 100
  622. #endif
  623. ) {
  624. fprintf(stderr,
  625. "InnoDB: Error in creating"
  626. " or opening %s\n",
  627. name);
  628. return(DB_ERROR);
  629. }
  630. } else if (srv_data_file_is_raw_partition[i] == SRV_NEW_RAW) {
  631. /* The partition is opened, not created; then it is
  632. written over */
  633. srv_start_raw_disk_in_use = TRUE;
  634. srv_created_new_raw = TRUE;
  635. files[i] = os_file_create(name, OS_FILE_OPEN_RAW,
  636. OS_FILE_NORMAL,
  637. OS_DATA_FILE, &ret);
  638. if (!ret) {
  639. fprintf(stderr,
  640. "InnoDB: Error in opening %s\n", name);
  641. return(DB_ERROR);
  642. }
  643. } else if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {
  644. srv_start_raw_disk_in_use = TRUE;
  645. ret = FALSE;
  646. } else {
  647. ut_a(0);
  648. }
  649. if (ret == FALSE) {
  650. /* We open the data file */
  651. if (one_created) {
  652. fprintf(stderr,
  653. "InnoDB: Error: data files can only"
  654. " be added at the end\n");
  655. fprintf(stderr,
  656. "InnoDB: of a tablespace, but"
  657. " data file %s existed beforehand.\n",
  658. name);
  659. return(DB_ERROR);
  660. }
  661. if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {
  662. files[i] = os_file_create(
  663. name, OS_FILE_OPEN_RAW,
  664. OS_FILE_NORMAL, OS_DATA_FILE, &ret);
  665. } else if (i == 0) {
  666. files[i] = os_file_create(
  667. name, OS_FILE_OPEN_RETRY,
  668. OS_FILE_NORMAL, OS_DATA_FILE, &ret);
  669. } else {
  670. files[i] = os_file_create(
  671. name, OS_FILE_OPEN, OS_FILE_NORMAL,
  672. OS_DATA_FILE, &ret);
  673. }
  674. if (!ret) {
  675. fprintf(stderr,
  676. "InnoDB: Error in opening %s\n", name);
  677. os_file_get_last_error(TRUE);
  678. return(DB_ERROR);
  679. }
  680. if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {
  681. goto skip_size_check;
  682. }
  683. ret = os_file_get_size(files[i], &size, &size_high);
  684. ut_a(ret);
  685. /* Round size downward to megabytes */
  686. rounded_size_pages
  687. = (size / (1024 * 1024) + 4096 * size_high)
  688. << (20 - UNIV_PAGE_SIZE_SHIFT);
  689. if (i == srv_n_data_files - 1
  690. && srv_auto_extend_last_data_file) {
  691. if (srv_data_file_sizes[i] > rounded_size_pages
  692. || (srv_last_file_size_max > 0
  693. && srv_last_file_size_max
  694. < rounded_size_pages)) {
  695. fprintf(stderr,
  696. "InnoDB: Error: auto-extending"
  697. " data file %s is"
  698. " of a different size\n"
  699. "InnoDB: %lu pages (rounded"
  700. " down to MB) than specified"
  701. " in the .cnf file:\n"
  702. "InnoDB: initial %lu pages,"
  703. " max %lu (relevant if"
  704. " non-zero) pages!\n",
  705. name,
  706. (ulong) rounded_size_pages,
  707. (ulong) srv_data_file_sizes[i],
  708. (ulong)
  709. srv_last_file_size_max);
  710. return(DB_ERROR);
  711. }
  712. srv_data_file_sizes[i] = rounded_size_pages;
  713. }
  714. if (rounded_size_pages != srv_data_file_sizes[i]) {
  715. fprintf(stderr,
  716. "InnoDB: Error: data file %s"
  717. " is of a different size\n"
  718. "InnoDB: %lu pages"
  719. " (rounded down to MB)\n"
  720. "InnoDB: than specified"
  721. " in the .cnf file %lu pages!\n",
  722. name,
  723. (ulong) rounded_size_pages,
  724. (ulong) srv_data_file_sizes[i]);
  725. return(DB_ERROR);
  726. }
  727. skip_size_check:
  728. fil_read_flushed_lsn_and_arch_log_no(
  729. files[i], one_opened,
  730. #ifdef UNIV_LOG_ARCHIVE
  731. min_arch_log_no, max_arch_log_no,
  732. #endif /* UNIV_LOG_ARCHIVE */
  733. min_flushed_lsn, max_flushed_lsn);
  734. one_opened = TRUE;
  735. } else {
  736. /* We created the data file and now write it full of
  737. zeros */
  738. one_created = TRUE;
  739. if (i > 0) {
  740. ut_print_timestamp(stderr);
  741. fprintf(stderr,
  742. " InnoDB: Data file %s did not"
  743. " exist: new to be created\n",
  744. name);
  745. } else {
  746. fprintf(stderr,
  747. "InnoDB: The first specified"
  748. " data file %s did not exist:\n"
  749. "InnoDB: a new database"
  750. " to be created!\n", name);
  751. *create_new_db = TRUE;
  752. }
  753. ut_print_timestamp(stderr);
  754. fprintf(stderr,
  755. " InnoDB: Setting file %s size to %lu MB\n",
  756. name,
  757. (ulong) (srv_data_file_sizes[i]
  758. >> (20 - UNIV_PAGE_SIZE_SHIFT)));
  759. fprintf(stderr,
  760. "InnoDB: Database physically writes the"
  761. " file full: wait...\n");
  762. ret = os_file_set_size(
  763. name, files[i],
  764. srv_calc_low32(srv_data_file_sizes[i]),
  765. srv_calc_high32(srv_data_file_sizes[i]));
  766. if (!ret) {
  767. fprintf(stderr,
  768. "InnoDB: Error in creating %s:"
  769. " probably out of disk space\n", name);
  770. return(DB_ERROR);
  771. }
  772. *sum_of_new_sizes = *sum_of_new_sizes
  773. + srv_data_file_sizes[i];
  774. }
  775. ret = os_file_close(files[i]);
  776. ut_a(ret);
  777. if (i == 0) {
  778. fil_space_create(name, 0, 0, FIL_TABLESPACE);
  779. }
  780. ut_a(fil_validate());
  781. fil_node_create(name, srv_data_file_sizes[i], 0,
  782. srv_data_file_is_raw_partition[i] != 0);
  783. }
  784. ios = 0;
  785. mutex_create(&ios_mutex, SYNC_NO_ORDER_CHECK);
  786. return(DB_SUCCESS);
  787. }
  788. /********************************************************************
  789. Starts InnoDB and creates a new database if database files
  790. are not found and the user wants. */
  791. UNIV_INTERN
  792. int
  793. innobase_start_or_create_for_mysql(void)
  794. /*====================================*/
  795. /* out: DB_SUCCESS or error code */
  796. {
  797. buf_pool_t* ret;
  798. ibool create_new_db;
  799. ibool log_file_created;
  800. ibool log_created = FALSE;
  801. ibool log_opened = FALSE;
  802. ib_uint64_t min_flushed_lsn;
  803. ib_uint64_t max_flushed_lsn;
  804. #ifdef UNIV_LOG_ARCHIVE
  805. ulint min_arch_log_no;
  806. ulint max_arch_log_no;
  807. #endif /* UNIV_LOG_ARCHIVE */
  808. ulint sum_of_new_sizes;
  809. ulint sum_of_data_file_sizes;
  810. ulint tablespace_size_in_header;
  811. ulint err;
  812. ulint i;
  813. my_bool srv_file_per_table_original_value
  814. = srv_file_per_table;
  815. mtr_t mtr;
  816. #ifdef HAVE_DARWIN_THREADS
  817. # ifdef F_FULLFSYNC
  818. /* This executable has been compiled on Mac OS X 10.3 or later.
  819. Assume that F_FULLFSYNC is available at run-time. */
  820. srv_have_fullfsync = TRUE;
  821. # else /* F_FULLFSYNC */
  822. /* This executable has been compiled on Mac OS X 10.2
  823. or earlier. Determine if the executable is running
  824. on Mac OS X 10.3 or later. */
  825. struct utsname utsname;
  826. if (uname(&utsname)) {
  827. fputs("InnoDB: cannot determine Mac OS X version!\n", stderr);
  828. } else {
  829. srv_have_fullfsync = strcmp(utsname.release, "7.") >= 0;
  830. }
  831. if (!srv_have_fullfsync) {
  832. fputs("InnoDB: On Mac OS X, fsync() may be"
  833. " broken on internal drives,\n"
  834. "InnoDB: making transactions unsafe!\n", stderr);
  835. }
  836. # endif /* F_FULLFSYNC */
  837. #endif /* HAVE_DARWIN_THREADS */
  838. if (sizeof(ulint) != sizeof(void*)) {
  839. fprintf(stderr,
  840. "InnoDB: Error: size of InnoDB's ulint is %lu,"
  841. " but size of void* is %lu.\n"
  842. "InnoDB: The sizes should be the same"
  843. " so that on a 64-bit platform you can\n"
  844. "InnoDB: allocate more than 4 GB of memory.",
  845. (ulong)sizeof(ulint), (ulong)sizeof(void*));
  846. }
  847. /* System tables are created in tablespace 0. Thus, we must
  848. temporarily clear srv_file_per_table. This is ok, because the
  849. server will not accept connections (which could modify
  850. innodb_file_per_table) until this function has returned. */
  851. srv_file_per_table = FALSE;
  852. #ifdef UNIV_DEBUG
  853. fprintf(stderr,
  854. "InnoDB: !!!!!!!! UNIV_DEBUG switched on !!!!!!!!!\n");
  855. #endif
  856. #ifdef UNIV_IBUF_DEBUG
  857. fprintf(stderr,
  858. "InnoDB: !!!!!!!! UNIV_IBUF_DEBUG switched on !!!!!!!!!\n"
  859. "InnoDB: Crash recovery will fail with UNIV_IBUF_DEBUG\n");
  860. #endif
  861. #ifdef UNIV_SYNC_DEBUG
  862. fprintf(stderr,
  863. "InnoDB: !!!!!!!! UNIV_SYNC_DEBUG switched on !!!!!!!!!\n");
  864. #endif
  865. #ifdef UNIV_SEARCH_DEBUG
  866. fprintf(stderr,
  867. "InnoDB: !!!!!!!! UNIV_SEARCH_DEBUG switched on !!!!!!!!!\n");
  868. #endif
  869. #ifdef UNIV_MEM_DEBUG
  870. fprintf(stderr,
  871. "InnoDB: !!!!!!!! UNIV_MEM_DEBUG switched on !!!!!!!!!\n");
  872. #endif
  873. if (UNIV_LIKELY(srv_use_sys_malloc)) {
  874. fprintf(stderr,
  875. "InnoDB: The InnoDB memory heap is disabled\n");
  876. }
  877. #ifdef HAVE_GCC_ATOMIC_BUILTINS
  878. #ifdef INNODB_RW_LOCKS_USE_ATOMICS
  879. fprintf(stderr,
  880. "InnoDB: Mutex and rw_lock use GCC atomic builtins.\n");
  881. #else
  882. fprintf(stderr,
  883. "InnoDB: Mutex use GCC atomic builtins.\n");
  884. #endif
  885. #endif
  886. /* Since InnoDB does not currently clean up all its internal data
  887. structures in MySQL Embedded Server Library server_end(), we
  888. print an error message if someone tries to start up InnoDB a
  889. second time during the process lifetime. */
  890. if (srv_start_has_been_called) {
  891. fprintf(stderr,
  892. "InnoDB: Error:startup called second time"
  893. " during the process lifetime.\n"
  894. "InnoDB: In the MySQL Embedded Server Library"
  895. " you cannot call server_init()\n"
  896. "InnoDB: more than once during"
  897. " the process lifetime.\n");
  898. }
  899. srv_start_has_been_called = TRUE;
  900. #ifdef UNIV_DEBUG
  901. log_do_write = TRUE;
  902. #endif /* UNIV_DEBUG */
  903. /* yydebug = TRUE; */
  904. srv_is_being_started = TRUE;
  905. srv_startup_is_before_trx_rollback_phase = TRUE;
  906. os_aio_use_native_aio = FALSE;
  907. #ifdef __WIN__
  908. if (os_get_os_version() == OS_WIN95
  909. || os_get_os_version() == OS_WIN31
  910. || os_get_os_version() == OS_WINNT) {
  911. /* On Win 95, 98, ME, Win32 subsystem for Windows 3.1,
  912. and NT use simulated aio. In NT Windows provides async i/o,
  913. but when run in conjunction with InnoDB Hot Backup, it seemed
  914. to corrupt the data files. */
  915. os_aio_use_native_aio = FALSE;
  916. } else {
  917. /* On Win 2000 and XP use async i/o */
  918. os_aio_use_native_aio = TRUE;
  919. }
  920. #endif
  921. if (srv_file_flush_method_str == NULL) {
  922. /* These are the default options */
  923. srv_unix_file_flush_method = SRV_UNIX_FSYNC;
  924. srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
  925. #ifndef __WIN__
  926. } else if (0 == ut_strcmp(srv_file_flush_method_str, "fsync")) {
  927. srv_unix_file_flush_method = SRV_UNIX_FSYNC;
  928. } else if (0 == ut_strcmp(srv_file_flush_method_str, "O_DSYNC")) {
  929. srv_unix_file_flush_method = SRV_UNIX_O_DSYNC;
  930. } else if (0 == ut_strcmp(srv_file_flush_method_str, "O_DIRECT")) {
  931. srv_unix_file_flush_method = SRV_UNIX_O_DIRECT;
  932. } else if (0 == ut_strcmp(srv_file_flush_method_str, "littlesync")) {
  933. srv_unix_file_flush_method = SRV_UNIX_LITTLESYNC;
  934. } else if (0 == ut_strcmp(srv_file_flush_method_str, "nosync")) {
  935. srv_unix_file_flush_method = SRV_UNIX_NOSYNC;
  936. #else
  937. } else if (0 == ut_strcmp(srv_file_flush_method_str, "normal")) {
  938. srv_win_file_flush_method = SRV_WIN_IO_NORMAL;
  939. os_aio_use_native_aio = FALSE;
  940. } else if (0 == ut_strcmp(srv_file_flush_method_str, "unbuffered")) {
  941. srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
  942. os_aio_use_native_aio = FALSE;
  943. } else if (0 == ut_strcmp(srv_file_flush_method_str,
  944. "async_unbuffered")) {
  945. srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
  946. #endif
  947. } else {
  948. fprintf(stderr,
  949. "InnoDB: Unrecognized value %s for"
  950. " innodb_flush_method\n",
  951. srv_file_flush_method_str);
  952. return(DB_ERROR);
  953. }
  954. /* Note that the call srv_boot() also changes the values of
  955. some variables to the units used by InnoDB internally */
  956. /* Set the maximum number of threads which can wait for a semaphore
  957. inside InnoDB: this is the 'sync wait array' size, as well as the
  958. maximum number of threads that can wait in the 'srv_conc array' for
  959. their time to enter InnoDB. */
  960. #if defined(__NETWARE__)
  961. /* Create less event semaphores because Win 98/ME had
  962. difficulty creating 40000 event semaphores. Comment from
  963. Novell, Inc.: also, these just take a lot of memory on
  964. NetWare. */
  965. srv_max_n_threads = 1000;
  966. #else
  967. if (srv_buf_pool_size >= 1000 * 1024 * 1024) {
  968. /* If buffer pool is less than 1000 MB,
  969. assume fewer threads. */
  970. srv_max_n_threads = 50000;
  971. } else if (srv_buf_pool_size >= 8 * 1024 * 1024) {
  972. srv_max_n_threads = 10000;
  973. } else {
  974. srv_max_n_threads = 1000; /* saves several MB of memory,
  975. especially in 64-bit
  976. computers */
  977. }
  978. #endif
  979. err = srv_boot();
  980. if (err != DB_SUCCESS) {
  981. return((int) err);
  982. }
  983. mutex_create(&srv_monitor_file_mutex, SYNC_NO_ORDER_CHECK);
  984. if (srv_innodb_status) {
  985. srv_monitor_file_name = mem_alloc(
  986. strlen(fil_path_to_mysql_datadir)
  987. + 20 + sizeof "/innodb_status.");
  988. sprintf(srv_monitor_file_name, "%s/innodb_status.%lu",
  989. fil_path_to_mysql_datadir, os_proc_get_number());
  990. srv_monitor_file = fopen(srv_monitor_file_name, "w+");
  991. if (!srv_monitor_file) {
  992. fprintf(stderr, "InnoDB: unable to create %s: %s\n",
  993. srv_monitor_file_name, strerror(errno));
  994. return(DB_ERROR);
  995. }
  996. } else {
  997. srv_monitor_file_name = NULL;
  998. srv_monitor_file = os_file_create_tmpfile();
  999. if (!srv_monitor_file) {
  1000. return(DB_ERROR);
  1001. }
  1002. }
  1003. mutex_create(&srv_dict_tmpfile_mutex, SYNC_DICT_OPERATION);
  1004. srv_dict_tmpfile = os_file_create_tmpfile();
  1005. if (!srv_dict_tmpfile) {
  1006. return(DB_ERROR);
  1007. }
  1008. mutex_create(&srv_misc_tmpfile_mutex, SYNC_ANY_LATCH);
  1009. srv_misc_tmpfile = os_file_create_tmpfile();
  1010. if (!srv_misc_tmpfile) {
  1011. return(DB_ERROR);
  1012. }
  1013. /* Restrict the maximum number of file i/o threads */
  1014. if (srv_n_file_io_threads > SRV_MAX_N_IO_THREADS) {
  1015. srv_n_file_io_threads = SRV_MAX_N_IO_THREADS;
  1016. }
  1017. if (!os_aio_use_native_aio) {
  1018. /* In simulated aio we currently have use only for 4 threads */
  1019. srv_n_file_io_threads = 4;
  1020. os_aio_init(8 * SRV_N_PENDING_IOS_PER_THREAD
  1021. * srv_n_file_io_threads,
  1022. srv_n_file_io_threads,
  1023. SRV_MAX_N_PENDING_SYNC_IOS);
  1024. } else {
  1025. os_aio_init(SRV_N_PENDING_IOS_PER_THREAD
  1026. * srv_n_file_io_threads,
  1027. srv_n_file_io_threads,
  1028. SRV_MAX_N_PENDING_SYNC_IOS);
  1029. }
  1030. fil_init(srv_max_n_open_files);
  1031. ret = buf_pool_init();
  1032. if (ret == NULL) {
  1033. fprintf(stderr,
  1034. "InnoDB: Fatal error: cannot allocate the memory"
  1035. " for the buffer pool\n");
  1036. return(DB_ERROR);
  1037. }
  1038. #ifdef UNIV_DEBUG
  1039. /* We have observed deadlocks with a 5MB buffer pool but
  1040. the actual lower limit could very well be a little higher. */
  1041. if (srv_buf_pool_size <= 5 * 1024 * 1024) {
  1042. fprintf(stderr, "InnoDB: Warning: Small buffer pool size "
  1043. "(%luM), the flst_validate() debug function "
  1044. "can cause a deadlock if the buffer pool fills up.\n",
  1045. srv_buf_pool_size / 1024 / 1024);
  1046. }
  1047. #endif
  1048. fsp_init();
  1049. log_init();
  1050. lock_sys_create(srv_lock_table_size);
  1051. /* Create i/o-handler threads: */
  1052. for (i = 0; i < srv_n_file_io_threads; i++) {
  1053. n[i] = i;
  1054. os_thread_create(io_handler_thread, n + i, thread_ids + i);
  1055. }
  1056. #ifdef UNIV_LOG_ARCHIVE
  1057. if (0 != ut_strcmp(srv_log_group_home_dirs[0], srv_arch_dir)) {
  1058. fprintf(stderr,
  1059. "InnoDB: Error: you must set the log group"
  1060. " home dir in my.cnf the\n"
  1061. "InnoDB: same as log arch dir.\n");
  1062. return(DB_ERROR);
  1063. }
  1064. #endif /* UNIV_LOG_ARCHIVE */
  1065. if (srv_n_log_files * srv_log_file_size >= 262144) {
  1066. fprintf(stderr,
  1067. "InnoDB: Error: combined size of log files"
  1068. " must be < 4 GB\n");
  1069. return(DB_ERROR);
  1070. }
  1071. sum_of_new_sizes = 0;
  1072. for (i = 0; i < srv_n_data_files; i++) {
  1073. #ifndef __WIN__
  1074. if (sizeof(off_t) < 5 && srv_data_file_sizes[i] >= 262144) {
  1075. fprintf(stderr,
  1076. "InnoDB: Error: file size must be < 4 GB"
  1077. " with this MySQL binary\n"
  1078. "InnoDB: and operating system combination,"
  1079. " in some OS's < 2 GB\n");
  1080. return(DB_ERROR);
  1081. }
  1082. #endif
  1083. sum_of_new_sizes += srv_data_file_sizes[i];
  1084. }
  1085. if (sum_of_new_sizes < 640) {
  1086. fprintf(stderr,
  1087. "InnoDB: Error: tablespace size must be"
  1088. " at least 10 MB\n");
  1089. return(DB_ERROR);
  1090. }
  1091. err = open_or_create_data_files(&create_new_db,
  1092. #ifdef UNIV_LOG_ARCHIVE
  1093. &min_arch_log_no, &max_arch_log_no,
  1094. #endif /* UNIV_LOG_ARCHIVE */
  1095. &min_flushed_lsn, &max_flushed_lsn,
  1096. &sum_of_new_sizes);
  1097. if (err != DB_SUCCESS) {
  1098. fprintf(stderr,
  1099. "InnoDB: Could not open or create data files.\n"
  1100. "InnoDB: If you tried to add new data files,"
  1101. " and it failed here,\n"
  1102. "InnoDB: you should now edit innodb_data_file_path"
  1103. " in my.cnf back\n"
  1104. "InnoDB: to what it was, and remove the"
  1105. " new ibdata files InnoDB created\n"
  1106. "InnoDB: in this failed attempt. InnoDB only wrote"
  1107. " those files full of\n"
  1108. "InnoDB: zeros, but did not yet use them in any way."
  1109. " But be careful: do not\n"
  1110. "InnoDB: remove old data files"
  1111. " which contain your precious data!\n");
  1112. return((int) err);
  1113. }
  1114. #ifdef UNIV_LOG_ARCHIVE
  1115. srv_normalize_path_for_win(srv_arch_dir);
  1116. srv_arch_dir = srv_add_path_separator_if_needed(srv_arch_dir);
  1117. #endif /* UNIV_LOG_ARCHIVE */
  1118. for (i = 0; i < srv_n_log_files; i++) {
  1119. err = open_or_create_log_file(create_new_db, &log_file_created,
  1120. log_opened, 0, i);
  1121. if (err != DB_SUCCESS) {
  1122. return((int) err);
  1123. }
  1124. if (log_file_created) {
  1125. log_created = TRUE;
  1126. } else {
  1127. log_opened = TRUE;
  1128. }
  1129. if ((log_opened && create_new_db)
  1130. || (log_opened && log_created)) {
  1131. fprintf(stderr,
  1132. "InnoDB: Error: all log files must be"
  1133. " created at the same time.\n"
  1134. "InnoDB: All log files must be"
  1135. " created also in database creation.\n"
  1136. "InnoDB: If you want bigger or smaller"
  1137. " log files, shut down the\n"
  1138. "InnoDB: database and make sure there"
  1139. " were no errors in shutdown.\n"
  1140. "InnoDB: Then delete the existing log files."
  1141. " Edit the .cnf file\n"
  1142. "InnoDB: and start the database again.\n");
  1143. return(DB_ERROR);
  1144. }
  1145. }
  1146. /* Open all log files and data files in the system tablespace: we
  1147. keep them open until database shutdown */
  1148. fil_open_log_and_system_tablespace_files();
  1149. if (log_created && !create_new_db
  1150. #ifdef UNIV_LOG_ARCHIVE
  1151. && !srv_archive_recovery
  1152. #endif /* UNIV_LOG_ARCHIVE */
  1153. ) {
  1154. if (max_flushed_lsn != min_flushed_lsn
  1155. #ifdef UNIV_LOG_ARCHIVE
  1156. || max_arch_log_no != min_arch_log_no
  1157. #endif /* UNIV_LOG_ARCHIVE */
  1158. ) {
  1159. fprintf(stderr,
  1160. "InnoDB: Cannot initialize created"
  1161. " log files because\n"
  1162. "InnoDB: data files were not in sync"
  1163. " with each other\n"
  1164. "InnoDB: or the data files are corrupt.\n");
  1165. return(DB_ERROR);
  1166. }
  1167. if (max_flushed_lsn < (ib_uint64_t) 1000) {
  1168. fprintf(stderr,
  1169. "InnoDB: Cannot initialize created"
  1170. " log files because\n"
  1171. "InnoDB: data files are corrupt,"
  1172. " or new data files were\n"
  1173. "InnoDB: created when the database"
  1174. " was started previous\n"
  1175. "InnoDB: time but the database"
  1176. " was not shut down\n"
  1177. "InnoDB: normally after that.\n");
  1178. return(DB_ERROR);
  1179. }
  1180. mutex_enter(&(log_sys->mutex));
  1181. #ifdef UNIV_LOG_ARCHIVE
  1182. /* Do not + 1 arch_log_no because we do not use log
  1183. archiving */
  1184. recv_reset_logs(max_flushed_lsn, max_arch_log_no, TRUE);
  1185. #else
  1186. recv_reset_logs(max_flushed_lsn, TRUE);
  1187. #endif /* UNIV_LOG_ARCHIVE */
  1188. mutex_exit(&(log_sys->mutex));
  1189. }
  1190. trx_sys_file_format_init();
  1191. if (create_new_db) {
  1192. mtr_start(&mtr);
  1193. fsp_header_init(0, sum_of_new_sizes, &mtr);
  1194. mtr_commit(&mtr);
  1195. trx_sys_create();
  1196. dict_create();
  1197. srv_startup_is_before_trx_rollback_phase = FALSE;
  1198. #ifdef UNIV_LOG_ARCHIVE
  1199. } else if (srv_archive_recovery) {
  1200. fprintf(stderr,
  1201. "InnoDB: Starting archive"
  1202. " recovery from a backup...\n");
  1203. err = recv_recovery_from_archive_start(
  1204. min_flushed_lsn, srv_archive_recovery_limit_lsn,
  1205. min_arch_log_no);
  1206. if (err != DB_SUCCESS) {
  1207. return(DB_ERROR);
  1208. }
  1209. /* Since ibuf init is in dict_boot, and ibuf is needed
  1210. in any disk i/o, first call dict_boot */
  1211. dict_boot();
  1212. trx_sys_init_at_db_start();
  1213. srv_startup_is_before_trx_rollback_phase = FALSE;
  1214. /* Initialize the fsp free limit global variable in the log
  1215. system */
  1216. fsp_header_get_free_limit();
  1217. recv_recovery_from_archive_finish();
  1218. #endif /* UNIV_LOG_ARCHIVE */
  1219. } else {
  1220. /* Check if we support the max format that is stamped
  1221. on the system tablespace.
  1222. Note: We are NOT allowed to make any modifications to
  1223. the TRX_SYS_PAGE_NO page before recovery because this
  1224. page also contains the max_trx_id etc. important system
  1225. variables that are required for recovery. We need to
  1226. ensure that we return the system to a state where normal
  1227. recovery is guaranteed to work. We do this by
  1228. invalidating the buffer cache, this will force the
  1229. reread of the page and restoration to its last known
  1230. consistent state, this is REQUIRED for the recovery
  1231. process to work. */
  1232. err = trx_sys_file_format_max_check(
  1233. srv_check_file_format_at_startup);
  1234. if (err != DB_SUCCESS) {
  1235. return(err);
  1236. }
  1237. /* Invalidate the buffer pool to ensure that we reread
  1238. the page that we read above, during recovery.
  1239. Note that this is not as heavy weight as it seems. At
  1240. this point there will be only ONE page in the buf_LRU
  1241. and there must be no page in the buf_flush list. */
  1242. buf_pool_invalidate();
  1243. /* We always try to do a recovery, even if the database had
  1244. been shut down normally: this is the normal startup path */
  1245. err = recv_recovery_from_checkpoint_start(LOG_CHECKPOINT,
  1246. IB_ULONGLONG_MAX,
  1247. min_flushed_lsn,
  1248. max_flushed_lsn);
  1249. if (err != DB_SUCCESS) {
  1250. return(DB_ERROR);
  1251. }
  1252. /* Since the insert buffer init is in dict_boot, and the
  1253. insert buffer is needed in any disk i/o, first we call
  1254. dict_boot(). Note that trx_sys_init_at_db_start() only needs
  1255. to access space 0, and the insert buffer at this stage already
  1256. works for space 0. */
  1257. dict_boot();
  1258. trx_sys_init_at_db_start();
  1259. if (srv_force_recovery < SRV_FORCE_NO_IBUF_MERGE) {
  1260. /* The following call is necessary for the insert
  1261. buffer to work with multiple tablespaces. We must
  1262. know the mapping between space id's and .ibd file
  1263. names.
  1264. In a crash recovery, we check that the info in data
  1265. dictionary is consistent with what we already know
  1266. about space id's from the call of
  1267. fil_load_single_table_tablespaces().
  1268. In a normal startup, we create the space objects for
  1269. every table in the InnoDB data dictionary that has
  1270. an .ibd file.
  1271. We also determine the maximum tablespace id used.
  1272. TODO: We may have incomplete transactions in the
  1273. data dictionary tables. Does that harm the scanning of
  1274. the data dictionary below? */
  1275. dict_check_tablespaces_and_store_max_id(
  1276. recv_needed_recovery);
  1277. }
  1278. srv_startup_is_before_trx_rollback_phase = FALSE;
  1279. /* Initialize the fsp free limit global variable in the log
  1280. system */
  1281. fsp_header_get_free_limit();
  1282. /* recv_recovery_from_checkpoint_finish needs trx lists which
  1283. are initialized in trx_sys_init_at_db_start(). */
  1284. recv_recovery_from_checkpoint_finish();
  1285. /* It is possible that file_format tag has never
  1286. been set. In this case we initialize it to minimum
  1287. value. Important to note that we can do it ONLY after
  1288. we have finished the recovery process so that the
  1289. image of TRX_SYS_PAGE_NO is not stale. */
  1290. trx_sys_file_format_tag_init();
  1291. }
  1292. if (!create_new_db && sum_of_new_sizes > 0) {
  1293. /* New data file(s) were added */
  1294. mtr_start(&mtr);
  1295. fsp_header_inc_size(0, sum_of_new_sizes, &mtr);
  1296. mtr_commit(&mtr);
  1297. /* Immediately write the log record about increased tablespace
  1298. size to disk, so that it is durable even if mysqld would crash
  1299. quickly */
  1300. log_buffer_flush_to_disk();
  1301. }
  1302. #ifdef UNIV_LOG_ARCHIVE
  1303. /* Archiving is always off under MySQL */
  1304. if (!srv_log_archive_on) {
  1305. ut_a(DB_SUCCESS == log_archive_noarchivelog());
  1306. } else {
  1307. mutex_enter(&(log_sys->mutex));
  1308. start_archive = FALSE;
  1309. if (log_sys->archiving_state == LOG_ARCH_OFF) {
  1310. start_archive = TRUE;
  1311. }
  1312. mutex_exit(&(log_sys->mutex));
  1313. if (start_archive) {
  1314. ut_a(DB_SUCCESS == log_archive_archivelog());
  1315. }
  1316. }
  1317. #endif /* UNIV_LOG_ARCHIVE */
  1318. /* fprintf(stderr, "Max allowed record size %lu\n",
  1319. page_get_free_space_of_empty() / 2); */
  1320. /* Create the thread which watches the timeouts for lock waits
  1321. and prints InnoDB monitor info */
  1322. os_thread_create(&srv_lock_timeout_and_monitor_thread, NULL,
  1323. thread_ids + 2 + SRV_MAX_N_IO_THREADS);
  1324. /* Create the thread which warns of long semaphore waits */
  1325. os_thread_create(&srv_error_monitor_thread, NULL,
  1326. thread_ids + 3 + SRV_MAX_N_IO_THREADS);
  1327. srv_is_being_started = FALSE;
  1328. if (trx_doublewrite == NULL) {
  1329. /* Create the doublewrite buffer to a new tablespace */
  1330. trx_sys_create_doublewrite_buf();
  1331. }
  1332. err = dict_create_or_check_foreign_constraint_tables();
  1333. if (err != DB_SUCCESS) {
  1334. return((int)DB_ERROR);
  1335. }
  1336. /* Create the master thread which does purge and other utility
  1337. operations */
  1338. os_thread_create(&srv_master_thread, NULL, thread_ids
  1339. + (1 + SRV_MAX_N_IO_THREADS));
  1340. #ifdef UNIV_DEBUG
  1341. /* buf_debug_prints = TRUE; */
  1342. #endif /* UNIV_DEBUG */
  1343. sum_of_data_file_sizes = 0;
  1344. for (i = 0; i < srv_n_data_files; i++) {
  1345. sum_of_data_file_sizes += srv_data_file_sizes[i];
  1346. }
  1347. tablespace_size_in_header = fsp_header_get_tablespace_size();
  1348. if (!srv_auto_extend_last_data_file
  1349. && sum_of_data_file_sizes != tablespace_size_in_header) {
  1350. fprintf(stderr,
  1351. "InnoDB: Error: tablespace size"
  1352. " stored in header is %lu pages, but\n"
  1353. "InnoDB: the sum of data file sizes is %lu pages\n",
  1354. (ulong) tablespace_size_in_header,
  1355. (ulong) sum_of_data_file_sizes);
  1356. if (srv_force_recovery == 0
  1357. && sum_of_data_file_sizes < tablespace_size_in_header) {
  1358. /* This is a fatal error, the tail of a tablespace is
  1359. missing */
  1360. fprintf(stderr,
  1361. "InnoDB: Cannot start InnoDB."
  1362. " The tail of the system tablespace is\n"
  1363. "InnoDB: missing. Have you edited"
  1364. " innodb_data_file_path in my.cnf in an\n"
  1365. "InnoDB: inappropriate way, removing"
  1366. " ibdata files from there?\n"
  1367. "InnoDB: You can set innodb_force_recovery=1"
  1368. " in my.cnf to force\n"
  1369. "InnoDB: a startup if you are trying"
  1370. " to recover a badly corrupt database.\n");
  1371. return(DB_ERROR);
  1372. }
  1373. }
  1374. if (srv_auto_extend_last_data_file
  1375. && sum_of_data_file_sizes < tablespace_size_in_header) {
  1376. fprintf(stderr,
  1377. "InnoDB: Error: tablespace size stored in header"
  1378. " is %lu pages, but\n"
  1379. "InnoDB: the sum of data file sizes"
  1380. " is only %lu pages\n",
  1381. (ulong) tablespace_size_in_header,
  1382. (ulong) sum_of_data_file_sizes);
  1383. if (srv_force_recovery == 0) {
  1384. fprintf(stderr,
  1385. "InnoDB: Cannot start InnoDB. The tail of"
  1386. " the system tablespace is\n"
  1387. "InnoDB: missing. Have you edited"
  1388. " innodb_data_file_path in my.cnf in an\n"
  1389. "InnoDB: inappropriate way, removing"
  1390. " ibdata files from there?\n"
  1391. "InnoDB: You can set innodb_force_recovery=1"
  1392. " in my.cnf to force\n"
  1393. "InnoDB: a startup if you are trying to"
  1394. " recover a badly corrupt database.\n");
  1395. return(DB_ERROR);
  1396. }
  1397. }
  1398. /* Check that os_fast_mutexes work as expected */
  1399. os_fast_mutex_init(&srv_os_test_mutex);
  1400. if (0 != os_fast_mutex_trylock(&srv_os_test_mutex)) {
  1401. fprintf(stderr,
  1402. "InnoDB: Error: pthread_mutex_trylock returns"
  1403. " an unexpected value on\n"
  1404. "InnoDB: success! Cannot continue.\n");
  1405. exit(1);
  1406. }
  1407. os_fast_mutex_unlock(&srv_os_test_mutex);
  1408. os_fast_mutex_lock(&srv_os_test_mutex);
  1409. os_fast_mutex_unlock(&srv_os_test_mutex);
  1410. os_fast_mutex_free(&srv_os_test_mutex);
  1411. if (srv_print_verbose_log) {
  1412. ut_print_timestamp(stderr);
  1413. fprintf(stderr,
  1414. " InnoDB Plugin %s started; "
  1415. "log sequence number %llu\n",
  1416. INNODB_VERSION_STR, srv_start_lsn);
  1417. }
  1418. if (srv_force_recovery > 0) {
  1419. fprintf(stderr,
  1420. "InnoDB: !!! innodb_force_recovery"
  1421. " is set to %lu !!!\n",
  1422. (ulong) srv_force_recovery);
  1423. }
  1424. fflush(stderr);
  1425. if (trx_doublewrite_must_reset_space_ids) {
  1426. /* Actually, we did not change the undo log format between
  1427. 4.0 and 4.1.1, and we would not need to run purge to
  1428. completion. Note also that the purge algorithm in 4.1.1
  1429. can process the the history list again even after a full
  1430. purge, because our algorithm does not cut the end of the
  1431. history list in all cases so that it would become empty
  1432. after a full purge. That mean that we may purge 4.0 type
  1433. undo log even after this phase.
  1434. The insert buffer record format changed between 4.0 and
  1435. 4.1.1. It is essential that the insert buffer is emptied
  1436. here! */
  1437. fprintf(stderr,
  1438. "InnoDB: You are upgrading to an"
  1439. " InnoDB version which allows multiple\n"
  1440. "InnoDB: tablespaces. Wait that purge"
  1441. " and insert buffer merge run to\n"
  1442. "InnoDB: completion...\n");
  1443. for (;;) {
  1444. os_thread_sleep(1000000);
  1445. if (0 == strcmp(srv_main_thread_op_info,
  1446. "waiting for server activity")) {
  1447. ut_a(ibuf_is_empty());
  1448. break;
  1449. }
  1450. }
  1451. fprintf(stderr,
  1452. "InnoDB: Full purge and insert buffer merge"
  1453. " completed.\n");
  1454. trx_sys_mark_upgraded_to_multiple_tablespaces();
  1455. fprintf(stderr,
  1456. "InnoDB: You have now successfully upgraded"
  1457. " to the multiple tablespaces\n"
  1458. "InnoDB: format. You should NOT DOWNGRADE"
  1459. " to an earlier version of\n"
  1460. "InnoDB: InnoDB! But if you absolutely need to"
  1461. " downgrade, see\n"
  1462. "InnoDB: http://dev.mysql.com/doc/refman/5.1/en/"
  1463. "multiple-tablespaces.html\n"
  1464. "InnoDB: for instructions.\n");
  1465. }
  1466. if (srv_force_recovery == 0) {
  1467. /* In the insert buffer we may have even bigger tablespace
  1468. id's, because we may have dropped those tablespaces, but
  1469. insert buffer merge has not had time to clean the records from
  1470. the ibuf tree. */
  1471. ibuf_update_max_tablespace_id();
  1472. }
  1473. srv_file_per_table = srv_file_per_table_original_value;
  1474. srv_was_started = TRUE;
  1475. return((int) DB_SUCCESS);
  1476. }
  1477. /********************************************************************
  1478. Shuts down the InnoDB database. */
  1479. UNIV_INTERN
  1480. int
  1481. innobase_shutdown_for_mysql(void)
  1482. /*=============================*/
  1483. /* out: DB_SUCCESS or error code */
  1484. {
  1485. ulint i;
  1486. #ifdef __NETWARE__
  1487. extern ibool panic_shutdown;
  1488. #endif
  1489. if (!srv_was_started) {
  1490. if (srv_is_being_started) {
  1491. ut_print_timestamp(stderr);
  1492. fprintf(stderr,
  1493. " InnoDB: Warning: shutting down"
  1494. " a not properly started\n"
  1495. "InnoDB: or created database!\n");
  1496. }
  1497. return(DB_SUCCESS);
  1498. }
  1499. /* 1. Flush the buffer pool to disk, write the current lsn to
  1500. the tablespace header(s), and copy all log data to archive.
  1501. The step 1 is the real InnoDB shutdown. The remaining steps 2 - ...
  1502. just free data structures after the shutdown. */
  1503. if (srv_fast_shutdown == 2) {
  1504. ut_print_timestamp(stderr);
  1505. fprintf(stderr,
  1506. " InnoDB: MySQL has requested a very fast shutdown"
  1507. " without flushing "
  1508. "the InnoDB buffer pool to data files."
  1509. " At the next mysqld startup "
  1510. "InnoDB will do a crash recovery!\n");
  1511. }
  1512. #ifdef __NETWARE__
  1513. if(!panic_shutdown)
  1514. #endif
  1515. logs_empty_and_mark_files_at_shutdown();
  1516. if (srv_conc_n_threads != 0) {
  1517. fprintf(stderr,
  1518. "InnoDB: Warning: query counter shows %ld queries"
  1519. " still\n"
  1520. "InnoDB: inside InnoDB at shutdown\n",
  1521. srv_conc_n_threads);
  1522. }
  1523. /* 2. Make all threads created by InnoDB to exit */
  1524. srv_shutdown_state = SRV_SHUTDOWN_EXIT_THREADS;
  1525. /* In a 'very fast' shutdown, we do not need to wait for these threads
  1526. to die; all which counts is that we flushed the log; a 'very fast'
  1527. shutdown is essentially a crash. */
  1528. if (srv_fast_shutdown == 2) {
  1529. return(DB_SUCCESS);
  1530. }
  1531. /* All threads end up waiting for certain events. Put those events
  1532. to the signaled state. Then the threads will exit themselves in
  1533. os_thread_event_wait(). */
  1534. for (i = 0; i < 1000; i++) {
  1535. /* NOTE: IF YOU CREATE THREADS IN INNODB, YOU MUST EXIT THEM
  1536. HERE OR EARLIER */
  1537. /* a. Let the lock timeout thread exit */
  1538. os_event_set(srv_lock_timeout_thread_event);
  1539. /* b. srv error monitor thread exits automatically, no need
  1540. to do anything here */
  1541. /* c. We wake the master thread so that it exits */
  1542. srv_wake_master_thread();
  1543. /* d. Exit the i/o threads */
  1544. os_aio_wake_all_threads_at_shutdown();
  1545. os_mutex_enter(os_sync_mutex);
  1546. if (os_thread_count == 0) {
  1547. /* All the threads have exited or are just exiting;
  1548. NOTE that the threads may not have completed their
  1549. exit yet. Should we use pthread_join() to make sure
  1550. they have exited? Now we just sleep 0.1 seconds and
  1551. hope that is enough! */
  1552. os_mutex_exit(os_sync_mutex);
  1553. os_thread_sleep(100000);
  1554. break;
  1555. }
  1556. os_mutex_exit(os_sync_mutex);
  1557. os_thread_sleep(100000);
  1558. }
  1559. if (i == 1000) {
  1560. fprintf(stderr,
  1561. "InnoDB: Warning: %lu threads created by InnoDB"
  1562. " had not exited at shutdown!\n",
  1563. (ulong) os_thread_count);
  1564. }
  1565. if (srv_monitor_file) {
  1566. fclose(srv_monitor_file);
  1567. srv_monitor_file = 0;
  1568. if (srv_monitor_file_name) {
  1569. unlink(srv_monitor_file_name);
  1570. mem_free(srv_monitor_file_name);
  1571. }
  1572. }
  1573. if (srv_dict_tmpfile) {
  1574. fclose(srv_dict_tmpfile);
  1575. srv_dict_tmpfile = 0;
  1576. }
  1577. if (srv_misc_tmpfile) {
  1578. fclose(srv_misc_tmpfile);
  1579. srv_misc_tmpfile = 0;
  1580. }
  1581. trx_sys_file_format_close();
  1582. mutex_free(&srv_monitor_file_mutex);
  1583. mutex_free(&srv_dict_tmpfile_mutex);
  1584. mutex_free(&srv_misc_tmpfile_mutex);
  1585. /* 3. Free all InnoDB's own mutexes and the os_fast_mutexes inside
  1586. them */
  1587. sync_close();
  1588. /* 4. Free the os_conc_mutex and all os_events and os_mutexes */
  1589. srv_free();
  1590. os_sync_free();
  1591. /* Check that all read views are closed except read view owned
  1592. by a purge. */
  1593. if (UT_LIST_GET_LEN(trx_sys->view_list) > 1) {
  1594. fprintf(stderr,
  1595. "InnoDB: Error: all read views were not closed"
  1596. " before shutdown:\n"
  1597. "InnoDB: %lu read views open \n",
  1598. UT_LIST_GET_LEN(trx_sys->view_list) - 1);
  1599. }
  1600. /* 5. Free all allocated memory and the os_fast_mutex created in
  1601. ut0mem.c */
  1602. buf_pool_free();
  1603. ut_free_all_mem();
  1604. if (os_thread_count != 0
  1605. || os_event_count != 0
  1606. || os_mutex_count != 0
  1607. || os_fast_mutex_count != 0) {
  1608. fprintf(stderr,
  1609. "InnoDB: Warning: some resources were not"
  1610. " cleaned up in shutdown:\n"
  1611. "InnoDB: threads %lu, events %lu,"
  1612. " os_mutexes %lu, os_fast_mutexes %lu\n",
  1613. (ulong) os_thread_count, (ulong) os_event_count,
  1614. (ulong) os_mutex_count, (ulong) os_fast_mutex_count);
  1615. }
  1616. if (dict_foreign_err_file) {
  1617. fclose(dict_foreign_err_file);
  1618. }
  1619. if (lock_latest_err_file) {
  1620. fclose(lock_latest_err_file);
  1621. }
  1622. if (srv_print_verbose_log) {
  1623. ut_print_timestamp(stderr);
  1624. fprintf(stderr,
  1625. " InnoDB: Shutdown completed;"
  1626. " log sequence number %llu\n",
  1627. srv_shutdown_lsn);
  1628. }
  1629. srv_was_started = FALSE;
  1630. return((int) DB_SUCCESS);
  1631. }
  1632. #ifdef __NETWARE__
  1633. void set_panic_flag_for_netware()
  1634. {
  1635. extern ibool panic_shutdown;
  1636. panic_shutdown = TRUE;
  1637. }
  1638. #endif /* __NETWARE__ */
  1639. #endif /* !UNIV_HOTBACKUP */